blob: 915af4cfe2b2ed95af9f1c5883e2b74e5ff2869d [file] [log] [blame]
Javier Martinez Canillas4065d1e2012-01-31 00:18:00 -08001/*
2 * Source for:
3 * Cypress TrueTouch(TM) Standard Product (TTSP) SPI touchscreen driver.
4 * For use with Cypress Txx3xx parts.
5 * Supported parts include:
6 * CY8CTST341
7 * CY8CTMA340
8 *
9 * Copyright (C) 2009, 2010, 2011 Cypress Semiconductor, Inc.
10 * Copyright (C) 2012 Javier Martinez Canillas <javier@dowhile0.org>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * version 2, and only version 2, as published by the
15 * Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 *
26 * Contact Cypress Semiconductor at www.cypress.com <kev@cypress.com>
27 *
28 */
29
30#include "cyttsp_core.h"
31
32#include <linux/delay.h>
33#include <linux/input.h>
34#include <linux/spi/spi.h>
35
36#define CY_SPI_WR_OP 0x00 /* r/~w */
37#define CY_SPI_RD_OP 0x01
38#define CY_SPI_CMD_BYTES 4
39#define CY_SPI_SYNC_BYTE 2
40#define CY_SPI_SYNC_ACK1 0x62 /* from protocol v.2 */
41#define CY_SPI_SYNC_ACK2 0x9D /* from protocol v.2 */
42#define CY_SPI_DATA_SIZE 128
43#define CY_SPI_DATA_BUF_SIZE (CY_SPI_CMD_BYTES + CY_SPI_DATA_SIZE)
44#define CY_SPI_BITS_PER_WORD 8
45
46static int cyttsp_spi_xfer(struct cyttsp *ts,
47 u8 op, u8 reg, u8 *buf, int length)
48{
49 struct spi_device *spi = to_spi_device(ts->dev);
50 struct spi_message msg;
51 struct spi_transfer xfer[2];
52 u8 *wr_buf = &ts->xfer_buf[0];
53 u8 *rd_buf = &ts->xfer_buf[CY_SPI_DATA_BUF_SIZE];
54 int retval;
55 int i;
56
57 if (length > CY_SPI_DATA_SIZE) {
58 dev_err(ts->dev, "%s: length %d is too big.\n",
59 __func__, length);
60 return -EINVAL;
61 }
62
63 memset(wr_buf, 0, CY_SPI_DATA_BUF_SIZE);
64 memset(rd_buf, 0, CY_SPI_DATA_BUF_SIZE);
65
66 wr_buf[0] = 0x00; /* header byte 0 */
67 wr_buf[1] = 0xFF; /* header byte 1 */
68 wr_buf[2] = reg; /* reg index */
69 wr_buf[3] = op; /* r/~w */
70 if (op == CY_SPI_WR_OP)
71 memcpy(wr_buf + CY_SPI_CMD_BYTES, buf, length);
72
73 memset(xfer, 0, sizeof(xfer));
74 spi_message_init(&msg);
75
76 /*
77 We set both TX and RX buffers because Cypress TTSP
78 requires full duplex operation.
79 */
80 xfer[0].tx_buf = wr_buf;
81 xfer[0].rx_buf = rd_buf;
82 switch (op) {
83 case CY_SPI_WR_OP:
84 xfer[0].len = length + CY_SPI_CMD_BYTES;
85 spi_message_add_tail(&xfer[0], &msg);
86 break;
87
88 case CY_SPI_RD_OP:
89 xfer[0].len = CY_SPI_CMD_BYTES;
90 spi_message_add_tail(&xfer[0], &msg);
91
92 xfer[1].rx_buf = buf;
93 xfer[1].len = length;
94 spi_message_add_tail(&xfer[1], &msg);
95 break;
96
97 default:
98 dev_err(ts->dev, "%s: bad operation code=%d\n", __func__, op);
99 return -EINVAL;
100 }
101
102 retval = spi_sync(spi, &msg);
103 if (retval < 0) {
104 dev_dbg(ts->dev, "%s: spi_sync() error %d, len=%d, op=%d\n",
105 __func__, retval, xfer[1].len, op);
106
107 /*
108 * do not return here since was a bad ACK sequence
109 * let the following ACK check handle any errors and
110 * allow silent retries
111 */
112 }
113
114 if (rd_buf[CY_SPI_SYNC_BYTE] != CY_SPI_SYNC_ACK1 ||
115 rd_buf[CY_SPI_SYNC_BYTE + 1] != CY_SPI_SYNC_ACK2) {
116
117 dev_dbg(ts->dev, "%s: operation %d failed\n", __func__, op);
118
119 for (i = 0; i < CY_SPI_CMD_BYTES; i++)
120 dev_dbg(ts->dev, "%s: test rd_buf[%d]:0x%02x\n",
121 __func__, i, rd_buf[i]);
122 for (i = 0; i < length; i++)
123 dev_dbg(ts->dev, "%s: test buf[%d]:0x%02x\n",
124 __func__, i, buf[i]);
125
126 return -EIO;
127 }
128
129 return 0;
130}
131
132static int cyttsp_spi_read_block_data(struct cyttsp *ts,
133 u8 addr, u8 length, void *data)
134{
135 return cyttsp_spi_xfer(ts, CY_SPI_RD_OP, addr, data, length);
136}
137
138static int cyttsp_spi_write_block_data(struct cyttsp *ts,
139 u8 addr, u8 length, const void *data)
140{
141 return cyttsp_spi_xfer(ts, CY_SPI_WR_OP, addr, (void *)data, length);
142}
143
144static const struct cyttsp_bus_ops cyttsp_spi_bus_ops = {
145 .bustype = BUS_SPI,
146 .write = cyttsp_spi_write_block_data,
147 .read = cyttsp_spi_read_block_data,
148};
149
150static int __devinit cyttsp_spi_probe(struct spi_device *spi)
151{
152 struct cyttsp *ts;
153 int error;
154
155 /* Set up SPI*/
156 spi->bits_per_word = CY_SPI_BITS_PER_WORD;
157 spi->mode = SPI_MODE_0;
158 error = spi_setup(spi);
159 if (error < 0) {
160 dev_err(&spi->dev, "%s: SPI setup error %d\n",
161 __func__, error);
162 return error;
163 }
164
165 ts = cyttsp_probe(&cyttsp_spi_bus_ops, &spi->dev, spi->irq,
166 CY_SPI_DATA_BUF_SIZE * 2);
167 if (IS_ERR(ts))
168 return PTR_ERR(ts);
169
170 spi_set_drvdata(spi, ts);
171
172 return 0;
173}
174
175static int __devexit cyttsp_spi_remove(struct spi_device *spi)
176{
177 struct cyttsp *ts = spi_get_drvdata(spi);
178
179 cyttsp_remove(ts);
180
181 return 0;
182}
183
184static struct spi_driver cyttsp_spi_driver = {
185 .driver = {
186 .name = CY_SPI_NAME,
187 .owner = THIS_MODULE,
188 .pm = &cyttsp_pm_ops,
189 },
190 .probe = cyttsp_spi_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800191 .remove = cyttsp_spi_remove,
Javier Martinez Canillas4065d1e2012-01-31 00:18:00 -0800192};
193
Axel Linca839222012-03-16 23:05:26 -0700194module_spi_driver(cyttsp_spi_driver);
Javier Martinez Canillas4065d1e2012-01-31 00:18:00 -0800195
196MODULE_ALIAS("spi:cyttsp");
197MODULE_LICENSE("GPL");
198MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard Product (TTSP) SPI driver");
199MODULE_AUTHOR("Cypress");
200MODULE_ALIAS("spi:cyttsp");