summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/cyttsp_spi.c
blob: d4f7ffeed1bac3f9031be0542954f0b47537f0a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* Source for:
 * Cypress TrueTouch(TM) Standard Product I2C touchscreen driver.
 * drivers/input/touchscreen/cyttsp_spi.c
 *
 * Copyright (C) 2009-2011 Cypress Semiconductor, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, and only version 2, as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Cypress reserves the right to make changes without further notice
 * to the materials described herein. Cypress does not assume any
 * liability arising out of the application described herein.
 *
 * Contact Cypress Semiconductor at www.cypress.com
 *
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/spi/spi.h>
#include <linux/delay.h>
#include <linux/cyttsp.h>
#include "cyttsp_core.h"

#define DBG(x)

#define CY_SPI_WR_OP      0x00 /* r/~w */
#define CY_SPI_RD_OP      0x01
#define CY_SPI_CMD_BYTES  4
#define CY_SPI_SYNC_BYTE  2
#define CY_SPI_SYNC_ACK1  0x62
#define CY_SPI_SYNC_ACK2  0x9D
#define CY_SPI_DATA_SIZE  128
#define CY_SPI_DATA_BUF_SIZE (CY_SPI_CMD_BYTES + CY_SPI_DATA_SIZE)
#define CY_SPI_BITS_PER_WORD 8

struct cyttsp_spi {
	struct cyttsp_bus_ops ops;
	struct spi_device *spi_client;
	void *ttsp_client;
	u8 wr_buf[CY_SPI_DATA_BUF_SIZE];
	u8 rd_buf[CY_SPI_DATA_BUF_SIZE];
};

static int cyttsp_spi_xfer_(u8 op, struct cyttsp_spi *ts_spi,
			    u8 reg, u8 *buf, int length)
{
	struct spi_message msg;
	struct spi_transfer xfer[2];
	u8 *wr_buf = ts_spi->wr_buf;
	u8 *rd_buf = ts_spi->rd_buf;
	int retval;
	DBG(printk(KERN_INFO "%s: Enter\n", __func__);)
	if (length > CY_SPI_DATA_SIZE) {
		printk(KERN_ERR "%s: length %d is too big.\n",
			__func__, length);
		return -EINVAL;
	}
	DBG(printk(KERN_INFO "%s: OP=%s length=%d\n", __func__,
		   op == CY_SPI_RD_OP ? "Read" : "Write", length);)

	memset(wr_buf, 0, CY_SPI_DATA_BUF_SIZE);
	memset(rd_buf, 0, CY_SPI_DATA_BUF_SIZE);

	wr_buf[0] = 0x00; /* header byte 0 */
	wr_buf[1] = 0xFF; /* header byte 1 */
	wr_buf[2] = reg;  /* reg index */
	wr_buf[3] = op;   /* r/~w */
	if (op == CY_SPI_WR_OP)
		memcpy(wr_buf + CY_SPI_CMD_BYTES, buf, length);
	DBG(
	if (op == CY_SPI_RD_OP)
		memset(rd_buf, CY_SPI_SYNC_NACK, CY_SPI_DATA_BUF_SIZE);)
	DBG(
	for (i = 0; i < (length + CY_SPI_CMD_BYTES); i++) {
		if ((op == CY_SPI_RD_OP) && (i < CY_SPI_CMD_BYTES))
			printk(KERN_INFO "%s: read op. wr[%d]:0x%02x\n",
				__func__, i, wr_buf[i]);
		if (op == CY_SPI_WR_OP)
			printk(KERN_INFO "%s: write op. wr[%d]:0x%02x\n",
				__func__, i, wr_buf[i]);
	})

	memset((void *)xfer, 0, sizeof(xfer));
	spi_message_init(&msg);
	xfer[0].tx_buf = wr_buf;
	xfer[0].rx_buf = rd_buf;
	if (op == CY_SPI_WR_OP) {
		xfer[0].len = length + CY_SPI_CMD_BYTES;
		spi_message_add_tail(&xfer[0], &msg);
	} else if (op == CY_SPI_RD_OP) {
		xfer[0].len = CY_SPI_CMD_BYTES;
		spi_message_add_tail(&xfer[0], &msg);

		xfer[1].rx_buf = buf;
		xfer[1].len = length;
		spi_message_add_tail(&xfer[1], &msg);
	}

	retval = spi_sync(ts_spi->spi_client, &msg);
	if (retval < 0) {
		printk(KERN_ERR "%s: spi sync error %d, len=%d, op=%d\n",
			__func__, retval, xfer[1].len, op);
		retval = 0;
	}

	if ((rd_buf[CY_SPI_SYNC_BYTE] == CY_SPI_SYNC_ACK1) &&
		(rd_buf[CY_SPI_SYNC_BYTE+1] == CY_SPI_SYNC_ACK2))
		retval = 0;
	else {
		DBG(
		for (i = 0; i < (CY_SPI_CMD_BYTES); i++)
			printk(KERN_INFO "%s: test rd_buf[%d]:0x%02x\n",
				__func__, i, rd_buf[i]);
		for (i = 0; i < (length); i++)
			printk(KERN_INFO "%s: test buf[%d]:0x%02x\n",
				__func__, i, buf[i]);)
		retval = 1;
	}
	return retval;
}

static int cyttsp_spi_xfer(u8 op, struct cyttsp_spi *ts,
			    u8 reg, u8 *buf, int length)
{
	int tries;
	int retval;
	DBG(printk(KERN_INFO "%s: Enter\n", __func__);)

	if (op == CY_SPI_RD_OP) {
		for (tries = CY_NUM_RETRY; tries; tries--) {
			retval = cyttsp_spi_xfer_(op, ts, reg, buf, length);
			if (retval == 0)
				break;
			else
				msleep(10);
		}
	} else {
		retval = cyttsp_spi_xfer_(op, ts, reg, buf, length);
	}
	return retval;
}

static s32 ttsp_spi_read_block_data(void *handle, u8 addr,
				    u8 length, void *data)
{
	int retval;
	struct cyttsp_spi *ts = container_of(handle, struct cyttsp_spi, ops);

	DBG(printk(KERN_INFO "%s: Enter\n", __func__);)

	retval = cyttsp_spi_xfer(CY_SPI_RD_OP, ts, addr, data, length);
	if (retval < 0)
		printk(KERN_ERR "%s: ttsp_spi_read_block_data failed\n",
			__func__);

	/* Do not print the above error if the data sync bytes were not found.
	   This is a normal condition for the bootloader loader startup and need
	   to retry until data sync bytes are found. */
	if (retval > 0)
		retval = -1;	/* now signal fail; so retry can be done */

	return retval;
}

static s32 ttsp_spi_write_block_data(void *handle, u8 addr,
				     u8 length, const void *data)
{
	int retval;
	struct cyttsp_spi *ts = container_of(handle, struct cyttsp_spi, ops);

	DBG(printk(KERN_INFO "%s: Enter\n", __func__);)

	retval = cyttsp_spi_xfer(CY_SPI_WR_OP, ts, addr, (void *)data, length);
	if (retval < 0)
		printk(KERN_ERR "%s: ttsp_spi_write_block_data failed\n",
			__func__);

	/* Do not print the above error if the data sync bytes were not found.
	   This is a normal condition for the bootloader loader startup and need
	   to retry until data sync bytes are found. */
	if (retval > 0)
		retval = -1;	/* now signal fail; so retry can be done */

	return retval;
}

static s32 ttsp_spi_tch_ext(void *handle, void *values)
{
	int retval = 0;
	struct cyttsp_spi *ts = container_of(handle, struct cyttsp_spi, ops);

	DBG(printk(KERN_INFO "%s: Enter\n", __func__);)

	/* Add custom touch extension handling code here */
	/* set: retval < 0 for any returned system errors,
		retval = 0 if normal touch handling is required,
		retval > 0 if normal touch handling is *not* required */
	if (!ts || !values)
		retval = -EIO;

	return retval;
}

static int __devinit cyttsp_spi_probe(struct spi_device *spi)
{
	struct cyttsp_spi *ts_spi;
	int retval;
	DBG(printk(KERN_INFO"%s: Enter\n", __func__);)

	/* Set up SPI*/
	spi->bits_per_word = CY_SPI_BITS_PER_WORD;
	spi->mode = SPI_MODE_0;
	retval = spi_setup(spi);
	if (retval < 0) {
		printk(KERN_ERR "%s: SPI setup error %d\n", __func__, retval);
		return retval;
	}
	ts_spi = kzalloc(sizeof(*ts_spi), GFP_KERNEL);
	if (ts_spi == NULL) {
		printk(KERN_ERR "%s: Error, kzalloc\n", __func__);
		retval = -ENOMEM;
		goto error_alloc_data_failed;
	}
	ts_spi->spi_client = spi;
	dev_set_drvdata(&spi->dev, ts_spi);
	ts_spi->ops.write = ttsp_spi_write_block_data;
	ts_spi->ops.read = ttsp_spi_read_block_data;
	ts_spi->ops.ext = ttsp_spi_tch_ext;

	ts_spi->ttsp_client = cyttsp_core_init(&ts_spi->ops, &spi->dev);
	if (!ts_spi->ttsp_client) {
		retval = -ENODEV;
		goto ttsp_core_err;
	}
	printk(KERN_INFO "%s: Successful registration %s\n",
	       __func__, CY_SPI_NAME);

	return 0;

ttsp_core_err:
	kfree(ts_spi);
error_alloc_data_failed:
	return retval;
}

/* registered in driver struct */
static int __devexit cyttsp_spi_remove(struct spi_device *spi)
{
	struct cyttsp_spi *ts_spi = dev_get_drvdata(&spi->dev);
	DBG(printk(KERN_INFO"%s: Enter\n", __func__);)
	cyttsp_core_release(ts_spi->ttsp_client);
	kfree(ts_spi);
	return 0;
}


static struct spi_driver cyttsp_spi_driver = {
	.driver = {
		.name = CY_SPI_NAME,
		.bus = &spi_bus_type,
		.owner = THIS_MODULE,
	},
	.probe = cyttsp_spi_probe,
	.remove = __devexit_p(cyttsp_spi_remove),
};

static int __init cyttsp_spi_init(void)
{
	int err;

	err = spi_register_driver(&cyttsp_spi_driver);
	printk(KERN_INFO "%s: Cypress TrueTouch(R) Standard Product SPI "
		"Touchscreen Driver (Built %s @ %s) returned %d\n",
		 __func__, __DATE__, __TIME__, err);

	return err;
}
module_init(cyttsp_spi_init);

static void __exit cyttsp_spi_exit(void)
{
	spi_unregister_driver(&cyttsp_spi_driver);
	printk(KERN_INFO "%s: module exit\n", __func__);
}
module_exit(cyttsp_spi_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard Product SPI driver");
MODULE_AUTHOR("Cypress");