summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/synaptics_dsx/synaptics_dsx_spi.c
blob: dd797eef3be1886dee327144e209d6c60c96a90d (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * Synaptics DSX touchscreen driver
 *
 * Copyright (C) 2012 Synaptics Incorporated
 *
 * Copyright (C) 2012 Alexandra Chin <alexandra.chin@tw.synaptics.com>
 * Copyright (C) 2012 Scott Lin <scott.lin@tw.synaptics.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/spi/spi.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/types.h>
#include <linux/platform_device.h>
#include <linux/input/synaptics_dsx.h>
#include "synaptics_dsx_core.h"

#define SPI_READ 0x80
#define SPI_WRITE 0x00

static int synaptics_rmi4_spi_set_page(struct synaptics_rmi4_data *rmi4_data,
		unsigned short addr)
{
	int retval;
	unsigned int index;
	unsigned int xfer_count = PAGE_SELECT_LEN + 1;
	unsigned char txbuf[xfer_count];
	unsigned char page;
	struct spi_message msg;
	struct spi_transfer xfers[xfer_count];
	struct spi_device *spi = to_spi_device(rmi4_data->pdev->dev.parent);
	const struct synaptics_dsx_board_data *bdata =
			rmi4_data->hw_if->board_data;

	page = ((addr >> 8) & ~MASK_7BIT);
	if (page != rmi4_data->current_page) {
		spi_message_init(&msg);

		txbuf[0] = SPI_WRITE;
		txbuf[1] = MASK_8BIT;
		txbuf[2] = page;

		for (index = 0; index < xfer_count; index++) {
			memset(&xfers[index], 0, sizeof(struct spi_transfer));
			xfers[index].len = 1;
			xfers[index].delay_usecs = bdata->byte_delay_us;
			xfers[index].tx_buf = &txbuf[index];
			spi_message_add_tail(&xfers[index], &msg);
		}

		if (bdata->block_delay_us)
			xfers[index - 1].delay_usecs = bdata->block_delay_us;

		retval = spi_sync(spi, &msg);
		if (retval == 0) {
			rmi4_data->current_page = page;
			retval = PAGE_SELECT_LEN;
		} else {
			dev_err(rmi4_data->pdev->dev.parent,
					"%s: Failed to complete SPI transfer, error = %d\n",
					__func__, retval);
		}
	} else {
		retval = PAGE_SELECT_LEN;
	}

	return retval;
}

static int synaptics_rmi4_spi_read(struct synaptics_rmi4_data *rmi4_data,
		unsigned short addr, unsigned char *data, unsigned short length)
{
	int retval;
	unsigned int index;
	unsigned int xfer_count = length + ADDRESS_WORD_LEN;
	unsigned char txbuf[ADDRESS_WORD_LEN];
	unsigned char *rxbuf = NULL;
	struct spi_message msg;
	struct spi_transfer *xfers = NULL;
	struct spi_device *spi = to_spi_device(rmi4_data->pdev->dev.parent);
	const struct synaptics_dsx_board_data *bdata =
			rmi4_data->hw_if->board_data;

	spi_message_init(&msg);

	xfers = kcalloc(xfer_count, sizeof(struct spi_transfer), GFP_KERNEL);
	if (!xfers) {
		dev_err(rmi4_data->pdev->dev.parent,
				"%s: Failed to allocate memory for xfers\n",
				__func__);
		retval = -ENOMEM;
		goto exit;
	}

	txbuf[0] = (addr >> 8) | SPI_READ;
	txbuf[1] = addr & MASK_8BIT;

	rxbuf = kmalloc(length, GFP_KERNEL);
	if (!rxbuf) {
		dev_err(rmi4_data->pdev->dev.parent,
				"%s: Failed to allocate memory for rxbuf\n",
				__func__);
		retval = -ENOMEM;
		goto exit;
	}

	mutex_lock(&rmi4_data->rmi4_io_ctrl_mutex);

	retval = synaptics_rmi4_spi_set_page(rmi4_data, addr);
	if (retval != PAGE_SELECT_LEN) {
		retval = -EIO;
		goto exit;
	}

	for (index = 0; index < xfer_count; index++) {
		xfers[index].len = 1;
		xfers[index].delay_usecs = bdata->byte_delay_us;
		if (index < ADDRESS_WORD_LEN)
			xfers[index].tx_buf = &txbuf[index];
		else
			xfers[index].rx_buf = &rxbuf[index - ADDRESS_WORD_LEN];
		spi_message_add_tail(&xfers[index], &msg);
	}

	if (bdata->block_delay_us)
		xfers[index - 1].delay_usecs = bdata->block_delay_us;

	retval = spi_sync(spi, &msg);
	if (retval == 0) {
		retval = length;
		memcpy(data, rxbuf, length);
	} else {
		dev_err(rmi4_data->pdev->dev.parent,
				"%s: Failed to complete SPI transfer, error = %d\n",
				__func__, retval);
	}

	mutex_unlock(&rmi4_data->rmi4_io_ctrl_mutex);

exit:
	kfree(rxbuf);
	kfree(xfers);

	return retval;
}

static int synaptics_rmi4_spi_write(struct synaptics_rmi4_data *rmi4_data,
		unsigned short addr, unsigned char *data, unsigned short length)
{
	int retval;
	unsigned int index;
	unsigned int xfer_count = length + ADDRESS_WORD_LEN;
	unsigned char *txbuf = NULL;
	struct spi_message msg;
	struct spi_transfer *xfers = NULL;
	struct spi_device *spi = to_spi_device(rmi4_data->pdev->dev.parent);
	const struct synaptics_dsx_board_data *bdata =
			rmi4_data->hw_if->board_data;

	spi_message_init(&msg);

	xfers = kcalloc(xfer_count, sizeof(struct spi_transfer), GFP_KERNEL);
	if (!xfers) {
		dev_err(rmi4_data->pdev->dev.parent,
				"%s: Failed to allocate memory for xfers\n",
				__func__);
		retval = -ENOMEM;
		goto exit;
	}

	txbuf = kmalloc(xfer_count, GFP_KERNEL);
	if (!txbuf) {
		dev_err(rmi4_data->pdev->dev.parent,
				"%s: Failed to allocate memory for txbuf\n",
				__func__);
		retval = -ENOMEM;
		goto exit;
	}

	txbuf[0] = (addr >> 8) & ~SPI_READ;
	txbuf[1] = addr & MASK_8BIT;
	memcpy(&txbuf[ADDRESS_WORD_LEN], data, length);

	mutex_lock(&rmi4_data->rmi4_io_ctrl_mutex);

	retval = synaptics_rmi4_spi_set_page(rmi4_data, addr);
	if (retval != PAGE_SELECT_LEN) {
		retval = -EIO;
		goto exit;
	}

	for (index = 0; index < xfer_count; index++) {
		xfers[index].len = 1;
		xfers[index].delay_usecs = bdata->byte_delay_us;
		xfers[index].tx_buf = &txbuf[index];
		spi_message_add_tail(&xfers[index], &msg);
	}

	if (bdata->block_delay_us)
		xfers[index - 1].delay_usecs = bdata->block_delay_us;

	retval = spi_sync(spi, &msg);
	if (retval == 0) {
		retval = length;
	} else {
		dev_err(rmi4_data->pdev->dev.parent,
				"%s: Failed to complete SPI transfer, error = %d\n",
				__func__, retval);
	}

	mutex_unlock(&rmi4_data->rmi4_io_ctrl_mutex);

exit:
	kfree(txbuf);
	kfree(xfers);

	return retval;
}

static struct synaptics_dsx_bus_access bus_access = {
	.type = BUS_SPI,
	.read = synaptics_rmi4_spi_read,
	.write = synaptics_rmi4_spi_write,
};

static struct synaptics_dsx_hw_interface hw_if;

static struct platform_device *synaptics_dsx_spi_device;

static void synaptics_rmi4_spi_dev_release(struct device *dev)
{
	kfree(synaptics_dsx_spi_device);

	return;
}

static int synaptics_rmi4_spi_probe(struct spi_device *spi)
{
	int retval;

	if (spi->master->flags & SPI_MASTER_HALF_DUPLEX) {
		dev_err(&spi->dev,
				"%s: Full duplex not supported by host\n",
				__func__);
		return -EIO;
	}

	synaptics_dsx_spi_device = kzalloc(
			sizeof(struct platform_device),
			GFP_KERNEL);
	if (!synaptics_dsx_spi_device) {
		dev_err(&spi->dev,
				"%s: Failed to allocate memory for synaptics_dsx_spi_device\n",
				__func__);
		return -ENOMEM;
	}

	spi->bits_per_word = 8;
	spi->mode = SPI_MODE_3;

	retval = spi_setup(spi);
	if (retval < 0) {
		dev_err(&spi->dev,
				"%s: Failed to perform SPI setup\n",
				__func__);
		return retval;
	}

	hw_if.board_data = spi->dev.platform_data;
	hw_if.bus_access = &bus_access;

	synaptics_dsx_spi_device->name = PLATFORM_DRIVER_NAME;
	synaptics_dsx_spi_device->id = 0;
	synaptics_dsx_spi_device->num_resources = 0;
	synaptics_dsx_spi_device->dev.parent = &spi->dev;
	synaptics_dsx_spi_device->dev.platform_data = &hw_if;
	synaptics_dsx_spi_device->dev.release = synaptics_rmi4_spi_dev_release;

	retval = platform_device_register(synaptics_dsx_spi_device);
	if (retval) {
		dev_err(&spi->dev,
				"%s: Failed to register platform device\n",
				__func__);
		return -ENODEV;
	}

	return 0;
}

static int synaptics_rmi4_spi_remove(struct spi_device *spi)
{
	platform_device_unregister(synaptics_dsx_spi_device);

	return 0;
}

static struct spi_driver synaptics_rmi4_spi_driver = {
	.driver = {
		.name = SPI_DRIVER_NAME,
		.owner = THIS_MODULE,
	},
	.probe = synaptics_rmi4_spi_probe,
	.remove = __devexit_p(synaptics_rmi4_spi_remove),
};


int synaptics_rmi4_bus_init(void)
{
	return spi_register_driver(&synaptics_rmi4_spi_driver);
}
EXPORT_SYMBOL(synaptics_rmi4_bus_init);

void synaptics_rmi4_bus_exit(void)
{
	spi_unregister_driver(&synaptics_rmi4_spi_driver);

	return;
}
EXPORT_SYMBOL(synaptics_rmi4_bus_exit);

MODULE_AUTHOR("Synaptics, Inc.");
MODULE_DESCRIPTION("Synaptics DSX SPI Bus Support Module");
MODULE_LICENSE("GPL v2");