ramips: rt305x: add support for the Netcore NW718 board
[openwrt.git] / target / linux / omap24xx / patches-2.6.38 / 600-tsc2005.patch
1 ---
2 drivers/input/touchscreen/Kconfig | 11
3 drivers/input/touchscreen/Makefile | 1
4 drivers/input/touchscreen/tsc2005.c | 958 ++++++++++++++++++++++++++++++++++++
5 include/linux/spi/tsc2005.h | 30 +
6 4 files changed, 1000 insertions(+)
7
8 --- a/drivers/input/touchscreen/Kconfig
9 +++ b/drivers/input/touchscreen/Kconfig
10 @@ -629,6 +629,17 @@ config TOUCHSCREEN_TOUCHIT213
11 To compile this driver as a module, choose M here: the
12 module will be called touchit213.
13
14 +config TOUCHSCREEN_TSC2005
15 + tristate "TSC2005 based touchscreens"
16 + depends on SPI_MASTER
17 + help
18 + Say Y here if you have a TSC2005 based touchscreen.
19 +
20 + If unsure, say N.
21 +
22 + To compile this driver as a module, choose M here: the
23 + module will be called tsc2005.
24 +
25 config TOUCHSCREEN_TSC2007
26 tristate "TSC2007 based touchscreens"
27 depends on I2C
28 --- a/drivers/input/touchscreen/Makefile
29 +++ b/drivers/input/touchscreen/Makefile
30 @@ -45,6 +45,7 @@ obj-$(CONFIG_TOUCHSCREEN_TNETV107X) += t
31 obj-$(CONFIG_TOUCHSCREEN_TOUCHIT213) += touchit213.o
32 obj-$(CONFIG_TOUCHSCREEN_TOUCHRIGHT) += touchright.o
33 obj-$(CONFIG_TOUCHSCREEN_TOUCHWIN) += touchwin.o
34 +obj-$(CONFIG_TOUCHSCREEN_TSC2005) += tsc2005.o
35 obj-$(CONFIG_TOUCHSCREEN_TSC2007) += tsc2007.o
36 obj-$(CONFIG_TOUCHSCREEN_UCB1400) += ucb1400_ts.o
37 obj-$(CONFIG_TOUCHSCREEN_WACOM_W8001) += wacom_w8001.o
38 --- /dev/null
39 +++ b/drivers/input/touchscreen/tsc2005.c
40 @@ -0,0 +1,958 @@
41 +/*
42 + * TSC2005 touchscreen driver
43 + *
44 + * Copyright (C) 2006-2008 Nokia Corporation
45 + *
46 + * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com>
47 + * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com>
48 + *
49 + * This program is free software; you can redistribute it and/or modify
50 + * it under the terms of the GNU General Public License as published by
51 + * the Free Software Foundation; either version 2 of the License, or
52 + * (at your option) any later version.
53 + *
54 + * This program is distributed in the hope that it will be useful,
55 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
56 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57 + * GNU General Public License for more details.
58 + *
59 + * You should have received a copy of the GNU General Public License
60 + * along with this program; if not, write to the Free Software
61 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
62 + *
63 + */
64 +
65 +#include <linux/kernel.h>
66 +#include <linux/module.h>
67 +#include <linux/input.h>
68 +#include <linux/interrupt.h>
69 +#include <linux/delay.h>
70 +#include <linux/spi/spi.h>
71 +
72 +#include <linux/spi/tsc2005.h>
73 +
74 +/**
75 + * The touchscreen interface operates as follows:
76 + *
77 + * Initialize:
78 + * Request access to GPIO103 (DAV)
79 + * tsc2005_ts_irq_handler will trigger when DAV line goes down
80 + *
81 + * 1) Pen is pressed against touchscreeen
82 + * 2) TSC2005 performs AD conversion
83 + * 3) After the conversion is done TSC2005 drives DAV line down
84 + * 4) GPIO IRQ is received and tsc2005_ts_irq_handler is called
85 + * 5) tsc2005_ts_irq_handler queues up an spi transfer to fetch
86 + * the x, y, z1, z2 values
87 + * 6) tsc2005_ts_rx() reports coordinates to input layer and
88 + * sets up tsc2005_ts_timer() to be called after TSC2005_TS_SCAN_TIME
89 + * 7) When the penup_timer expires, there have not been DAV interrupts
90 + * during the last 20ms which means the pen has been lifted.
91 + */
92 +
93 +#define TSC2005_VDD_LOWER_27
94 +
95 +#ifdef TSC2005_VDD_LOWER_27
96 +#define TSC2005_HZ (10000000)
97 +#else
98 +#define TSC2005_HZ (25000000)
99 +#endif
100 +
101 +#define TSC2005_CMD (0x80)
102 +#define TSC2005_REG (0x00)
103 +
104 +#define TSC2005_CMD_STOP (1)
105 +#define TSC2005_CMD_10BIT (0 << 2)
106 +#define TSC2005_CMD_12BIT (1 << 2)
107 +
108 +#define TSC2005_CMD_SCAN_XYZZ (0 << 3)
109 +#define TSC2005_CMD_SCAN_XY (1 << 3)
110 +#define TSC2005_CMD_SCAN_X (2 << 3)
111 +#define TSC2005_CMD_SCAN_Y (3 << 3)
112 +#define TSC2005_CMD_SCAN_ZZ (4 << 3)
113 +#define TSC2005_CMD_AUX_SINGLE (5 << 3)
114 +#define TSC2005_CMD_TEMP1 (6 << 3)
115 +#define TSC2005_CMD_TEMP2 (7 << 3)
116 +#define TSC2005_CMD_AUX_CONT (8 << 3)
117 +#define TSC2005_CMD_TEST_X_CONN (9 << 3)
118 +#define TSC2005_CMD_TEST_Y_CONN (10 << 3)
119 +#define TSC2005_CMD_TEST_SHORT (11 << 3)
120 +/* command 12 reserved, according to 2008-03 erratum */
121 +#define TSC2005_CMD_DRIVE_XX (13 << 3)
122 +#define TSC2005_CMD_DRIVE_YY (14 << 3)
123 +#define TSC2005_CMD_DRIVE_YX (15 << 3)
124 +
125 +#define TSC2005_REG_X (0 << 3)
126 +#define TSC2005_REG_Y (1 << 3)
127 +#define TSC2005_REG_Z1 (2 << 3)
128 +#define TSC2005_REG_Z2 (3 << 3)
129 +#define TSC2005_REG_AUX (4 << 3)
130 +#define TSC2005_REG_TEMP1 (5 << 3)
131 +#define TSC2005_REG_TEMP2 (6 << 3)
132 +#define TSC2005_REG_STATUS (7 << 3)
133 +#define TSC2005_REG_AUX_HIGH (8 << 3)
134 +#define TSC2005_REG_AUX_LOW (9 << 3)
135 +#define TSC2005_REG_TEMP_HIGH (10 << 3)
136 +#define TSC2005_REG_TEMP_LOW (11 << 3)
137 +#define TSC2005_REG_CFR0 (12 << 3)
138 +#define TSC2005_REG_CFR1 (13 << 3)
139 +#define TSC2005_REG_CFR2 (14 << 3)
140 +#define TSC2005_REG_FUNCTION (15 << 3)
141 +
142 +#define TSC2005_REG_PND0 (1 << 1)
143 +#define TSC2005_REG_READ (0x01)
144 +#define TSC2005_REG_WRITE (0x00)
145 +
146 +
147 +#define TSC2005_CFR0_LONGSAMPLING (1)
148 +#define TSC2005_CFR0_DETECTINWAIT (1 << 1)
149 +#define TSC2005_CFR0_SENSETIME_32US (0)
150 +#define TSC2005_CFR0_SENSETIME_96US (1 << 2)
151 +#define TSC2005_CFR0_SENSETIME_544US (1 << 3)
152 +#define TSC2005_CFR0_SENSETIME_2080US (1 << 4)
153 +#define TSC2005_CFR0_SENSETIME_2656US (0x001C)
154 +#define TSC2005_CFR0_PRECHARGE_20US (0x0000)
155 +#define TSC2005_CFR0_PRECHARGE_84US (0x0020)
156 +#define TSC2005_CFR0_PRECHARGE_276US (0x0040)
157 +#define TSC2005_CFR0_PRECHARGE_1044US (0x0080)
158 +#define TSC2005_CFR0_PRECHARGE_1364US (0x00E0)
159 +#define TSC2005_CFR0_STABTIME_0US (0x0000)
160 +#define TSC2005_CFR0_STABTIME_100US (0x0100)
161 +#define TSC2005_CFR0_STABTIME_500US (0x0200)
162 +#define TSC2005_CFR0_STABTIME_1MS (0x0300)
163 +#define TSC2005_CFR0_STABTIME_5MS (0x0400)
164 +#define TSC2005_CFR0_STABTIME_100MS (0x0700)
165 +#define TSC2005_CFR0_CLOCK_4MHZ (0x0000)
166 +#define TSC2005_CFR0_CLOCK_2MHZ (0x0800)
167 +#define TSC2005_CFR0_CLOCK_1MHZ (0x1000)
168 +#define TSC2005_CFR0_RESOLUTION12 (0x2000)
169 +#define TSC2005_CFR0_STATUS (0x4000)
170 +#define TSC2005_CFR0_PENMODE (0x8000)
171 +
172 +#define TSC2005_CFR0_INITVALUE (TSC2005_CFR0_STABTIME_1MS | \
173 + TSC2005_CFR0_CLOCK_1MHZ | \
174 + TSC2005_CFR0_RESOLUTION12 | \
175 + TSC2005_CFR0_PRECHARGE_276US | \
176 + TSC2005_CFR0_PENMODE)
177 +
178 +/* Bits common to both read and write of config register 0 */
179 +#define TSC2005_CFR0_RW_MASK 0x3fff
180 +
181 +#define TSC2005_CFR1_BATCHDELAY_0MS (0x0000)
182 +#define TSC2005_CFR1_BATCHDELAY_1MS (0x0001)
183 +#define TSC2005_CFR1_BATCHDELAY_2MS (0x0002)
184 +#define TSC2005_CFR1_BATCHDELAY_4MS (0x0003)
185 +#define TSC2005_CFR1_BATCHDELAY_10MS (0x0004)
186 +#define TSC2005_CFR1_BATCHDELAY_20MS (0x0005)
187 +#define TSC2005_CFR1_BATCHDELAY_40MS (0x0006)
188 +#define TSC2005_CFR1_BATCHDELAY_100MS (0x0007)
189 +
190 +#define TSC2005_CFR1_INITVALUE (TSC2005_CFR1_BATCHDELAY_4MS)
191 +
192 +#define TSC2005_CFR2_MAVE_TEMP (0x0001)
193 +#define TSC2005_CFR2_MAVE_AUX (0x0002)
194 +#define TSC2005_CFR2_MAVE_Z (0x0004)
195 +#define TSC2005_CFR2_MAVE_Y (0x0008)
196 +#define TSC2005_CFR2_MAVE_X (0x0010)
197 +#define TSC2005_CFR2_AVG_1 (0x0000)
198 +#define TSC2005_CFR2_AVG_3 (0x0400)
199 +#define TSC2005_CFR2_AVG_7 (0x0800)
200 +#define TSC2005_CFR2_MEDIUM_1 (0x0000)
201 +#define TSC2005_CFR2_MEDIUM_3 (0x1000)
202 +#define TSC2005_CFR2_MEDIUM_7 (0x2000)
203 +#define TSC2005_CFR2_MEDIUM_15 (0x3000)
204 +
205 +#define TSC2005_CFR2_IRQ_MASK (0xC000)
206 +#define TSC2005_CFR2_IRQ_DAV (0x4000)
207 +#define TSC2005_CFR2_IRQ_PEN (0x8000)
208 +#define TSC2005_CFR2_IRQ_PENDAV (0x0000)
209 +
210 +#define TSC2005_CFR2_INITVALUE (TSC2005_CFR2_IRQ_PENDAV | \
211 + TSC2005_CFR2_MAVE_X | \
212 + TSC2005_CFR2_MAVE_Y | \
213 + TSC2005_CFR2_MAVE_Z | \
214 + TSC2005_CFR2_MEDIUM_15 | \
215 + TSC2005_CFR2_AVG_7)
216 +
217 +#define MAX_12BIT ((1 << 12) - 1)
218 +#define TS_SAMPLES 4
219 +#define TSC2005_TS_PENUP_TIME 40
220 +
221 +static const u32 tsc2005_read_reg[] = {
222 + (TSC2005_REG | TSC2005_REG_X | TSC2005_REG_READ) << 16,
223 + (TSC2005_REG | TSC2005_REG_Y | TSC2005_REG_READ) << 16,
224 + (TSC2005_REG | TSC2005_REG_Z1 | TSC2005_REG_READ) << 16,
225 + (TSC2005_REG | TSC2005_REG_Z2 | TSC2005_REG_READ) << 16,
226 +};
227 +#define NUM_READ_REGS (sizeof(tsc2005_read_reg)/sizeof(tsc2005_read_reg[0]))
228 +
229 +struct tsc2005 {
230 + struct spi_device *spi;
231 +
232 + struct input_dev *idev;
233 + char phys[32];
234 + struct timer_list penup_timer;
235 +
236 + /* ESD recovery via a hardware reset if the tsc2005
237 + * doesn't respond after a configurable period (in ms) of
238 + * IRQ/SPI inactivity. If esd_timeout is 0, timer and work
239 + * fields are used.
240 + */
241 + u32 esd_timeout;
242 + struct timer_list esd_timer;
243 + struct work_struct esd_work;
244 +
245 + spinlock_t lock;
246 + struct mutex mutex;
247 +
248 + struct spi_message read_msg;
249 + struct spi_transfer read_xfer[NUM_READ_REGS];
250 + u32 data[NUM_READ_REGS];
251 +
252 + /* previously reported x,y,p (if pen_down) */
253 + int out_x;
254 + int out_y;
255 + int out_p;
256 + /* fudge parameters - changes must exceed one of these. */
257 + int fudge_x;
258 + int fudge_y;
259 + int fudge_p;
260 + /* raw copy of previous x,y,z */
261 + int in_x;
262 + int in_y;
263 + int in_z1;
264 + int in_z2;
265 + /* average accumulators for each component */
266 + int sample_cnt;
267 + int avg_x;
268 + int avg_y;
269 + int avg_z1;
270 + int avg_z2;
271 + /* configuration */
272 + int x_plate_ohm;
273 + int hw_avg_max;
274 + int stab_time;
275 + int p_max;
276 + int touch_pressure;
277 + /* status */
278 + u8 sample_sent;
279 + u8 pen_down;
280 + u8 disabled;
281 + u8 disable_depth;
282 + u8 spi_pending;
283 +
284 + void (*set_reset)(bool enable);
285 +};
286 +
287 +static void tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
288 +{
289 + u8 data = TSC2005_CMD | TSC2005_CMD_12BIT | cmd;
290 + struct spi_message msg;
291 + struct spi_transfer xfer = { 0 };
292 +
293 + xfer.tx_buf = &data;
294 + xfer.rx_buf = NULL;
295 + xfer.len = 1;
296 + xfer.bits_per_word = 8;
297 +
298 + spi_message_init(&msg);
299 + spi_message_add_tail(&xfer, &msg);
300 + spi_sync(ts->spi, &msg);
301 +}
302 +
303 +static void tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
304 +{
305 + u32 tx;
306 + struct spi_message msg;
307 + struct spi_transfer xfer = { 0 };
308 +
309 + tx = (TSC2005_REG | reg | TSC2005_REG_PND0 |
310 + TSC2005_REG_WRITE) << 16;
311 + tx |= value;
312 +
313 + xfer.tx_buf = &tx;
314 + xfer.rx_buf = NULL;
315 + xfer.len = 4;
316 + xfer.bits_per_word = 24;
317 +
318 + spi_message_init(&msg);
319 + spi_message_add_tail(&xfer, &msg);
320 + spi_sync(ts->spi, &msg);
321 +}
322 +
323 +static void tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value)
324 +{
325 + u32 tx;
326 + u32 rx = 0;
327 + struct spi_message msg;
328 + struct spi_transfer xfer = { 0 };
329 +
330 + tx = (TSC2005_REG | reg | TSC2005_REG_READ) << 16;
331 +
332 + xfer.tx_buf = &tx;
333 + xfer.rx_buf = &rx;
334 + xfer.len = 4;
335 + xfer.bits_per_word = 24;
336 +
337 + spi_message_init(&msg);
338 + spi_message_add_tail(&xfer, &msg);
339 + spi_sync(ts->spi, &msg);
340 + *value = rx;
341 +}
342 +
343 +static void tsc2005_ts_update_pen_state(struct tsc2005 *ts,
344 + int x, int y, int pressure)
345 +{
346 + if (pressure) {
347 + input_report_abs(ts->idev, ABS_X, x);
348 + input_report_abs(ts->idev, ABS_Y, y);
349 + input_report_abs(ts->idev, ABS_PRESSURE, pressure);
350 + if (!ts->pen_down) {
351 + input_report_key(ts->idev, BTN_TOUCH, 1);
352 + ts->pen_down = 1;
353 + }
354 + } else {
355 + input_report_abs(ts->idev, ABS_PRESSURE, 0);
356 + if (ts->pen_down) {
357 + input_report_key(ts->idev, BTN_TOUCH, 0);
358 + ts->pen_down = 0;
359 + }
360 + }
361 +
362 + input_sync(ts->idev);
363 +}
364 +
365 +/*
366 + * This function is called by the SPI framework after the coordinates
367 + * have been read from TSC2005
368 + */
369 +static void tsc2005_ts_rx(void *arg)
370 +{
371 + struct tsc2005 *ts = arg;
372 + unsigned long flags;
373 + int inside_rect, pressure_limit;
374 + int x, y, z1, z2, pressure;
375 +
376 + spin_lock_irqsave(&ts->lock, flags);
377 +
378 + if (ts->disable_depth) {
379 + ts->spi_pending = 0;
380 + goto out;
381 + }
382 +
383 + x = ts->data[0];
384 + y = ts->data[1];
385 + z1 = ts->data[2];
386 + z2 = ts->data[3];
387 +
388 + /* validate pressure and position */
389 + if (x > MAX_12BIT || y > MAX_12BIT)
390 + goto out;
391 +
392 + /* skip coords if the pressure-components are out of range */
393 + if (z1 < 100 || z2 > MAX_12BIT || z1 >= z2)
394 + goto out;
395 +
396 + /* skip point if this is a pen down with the exact same values as
397 + * the value before pen-up - that implies SPI fed us stale data
398 + */
399 + if (!ts->pen_down &&
400 + ts->in_x == x &&
401 + ts->in_y == y &&
402 + ts->in_z1 == z1 &&
403 + ts->in_z2 == z2)
404 + goto out;
405 +
406 + /* At this point we are happy we have a valid and useful reading.
407 + * Remember it for later comparisons. We may now begin downsampling
408 + */
409 + ts->in_x = x;
410 + ts->in_y = y;
411 + ts->in_z1 = z1;
412 + ts->in_z2 = z2;
413 +
414 + /* don't run average on the "pen down" event */
415 + if (ts->sample_sent) {
416 + ts->avg_x += x;
417 + ts->avg_y += y;
418 + ts->avg_z1 += z1;
419 + ts->avg_z2 += z2;
420 +
421 + if (++ts->sample_cnt < TS_SAMPLES)
422 + goto out;
423 +
424 + x = ts->avg_x / TS_SAMPLES;
425 + y = ts->avg_y / TS_SAMPLES;
426 + z1 = ts->avg_z1 / TS_SAMPLES;
427 + z2 = ts->avg_z2 / TS_SAMPLES;
428 + }
429 +
430 + ts->sample_cnt = 0;
431 + ts->avg_x = 0;
432 + ts->avg_y = 0;
433 + ts->avg_z1 = 0;
434 + ts->avg_z2 = 0;
435 +
436 + pressure = x * (z2 - z1) / z1;
437 + pressure = pressure * ts->x_plate_ohm / 4096;
438 +
439 + pressure_limit = ts->sample_sent ? ts->p_max : ts->touch_pressure;
440 + if (pressure > pressure_limit)
441 + goto out;
442 +
443 + /* Discard the event if it still is within the previous rect -
444 + * unless the pressure is clearly harder, but then use previous
445 + * x,y position. If any coordinate deviates enough, fudging
446 + * of all three will still take place in the input layer.
447 + */
448 + inside_rect = (ts->sample_sent &&
449 + x > (int)ts->out_x - ts->fudge_x &&
450 + x < (int)ts->out_x + ts->fudge_x &&
451 + y > (int)ts->out_y - ts->fudge_y &&
452 + y < (int)ts->out_y + ts->fudge_y);
453 + if (inside_rect)
454 + x = ts->out_x, y = ts->out_y;
455 +
456 + if (!inside_rect || pressure < (ts->out_p - ts->fudge_p)) {
457 + tsc2005_ts_update_pen_state(ts, x, y, pressure);
458 + ts->sample_sent = 1;
459 + ts->out_x = x;
460 + ts->out_y = y;
461 + ts->out_p = pressure;
462 + }
463 +out:
464 + if (ts->spi_pending > 1) {
465 + /* One or more interrupts (sometimes several dozens)
466 + * occured while waiting for the SPI read - get
467 + * another read going.
468 + */
469 + ts->spi_pending = 1;
470 + if (spi_async(ts->spi, &ts->read_msg)) {
471 + dev_err(&ts->spi->dev, "ts: spi_async() failed");
472 + ts->spi_pending = 0;
473 + }
474 + } else
475 + ts->spi_pending = 0;
476 +
477 + /* kick pen up timer - to make sure it expires again(!) */
478 + if (ts->sample_sent) {
479 + mod_timer(&ts->penup_timer,
480 + jiffies + msecs_to_jiffies(TSC2005_TS_PENUP_TIME));
481 + /* Also kick the watchdog, as we still think we're alive */
482 + if (ts->esd_timeout && ts->disable_depth == 0) {
483 + unsigned long wdj = msecs_to_jiffies(ts->esd_timeout);
484 + mod_timer(&ts->esd_timer, round_jiffies(jiffies+wdj));
485 + }
486 + }
487 + spin_unlock_irqrestore(&ts->lock, flags);
488 +}
489 +
490 +/* This penup timer is very forgiving of delayed SPI reads. The
491 + * (ESD) watchdog will rescue us if spi_pending remains set, unless
492 + * we are enterring the disabled state. In that case we must just
493 + * handle the pen up, and let disabling complete.
494 + */
495 +static void tsc2005_ts_penup_timer_handler(unsigned long data)
496 +{
497 + struct tsc2005 *ts = (struct tsc2005 *)data;
498 + if ((!ts->spi_pending || ts->disable_depth) &&
499 + ts->sample_sent) {
500 + tsc2005_ts_update_pen_state(ts, 0, 0, 0);
501 + ts->sample_sent = 0;
502 + }
503 +}
504 +
505 +/*
506 + * This interrupt is called when pen is down and coordinates are
507 + * available. That is indicated by a either:
508 + * a) a rising edge on PINTDAV or (PENDAV mode)
509 + * b) a falling edge on DAV line (DAV mode)
510 + * depending on the setting of the IRQ bits in the CFR2 setting above.
511 + */
512 +static irqreturn_t tsc2005_ts_irq_handler(int irq, void *dev_id)
513 +{
514 + struct tsc2005 *ts = dev_id;
515 + if (ts->disable_depth)
516 + goto out;
517 +
518 + if (!ts->spi_pending) {
519 + if (spi_async(ts->spi, &ts->read_msg)) {
520 + dev_err(&ts->spi->dev, "ts: spi_async() failed");
521 + goto out;
522 + }
523 + }
524 + /* By shifting in 1s we can never wrap */
525 + ts->spi_pending = (ts->spi_pending<<1)+1;
526 +
527 + /* Kick pen up timer only if it's not been started yet. Strictly,
528 + * it isn't even necessary to start it at all here, but doing so
529 + * keeps an equivalence between pen state and timer state.
530 + * The SPI read loop will keep pushing it into the future.
531 + * If it times out with an SPI pending, it's ignored anyway.
532 + */
533 + if (!timer_pending(&ts->penup_timer)) {
534 + unsigned long pu = msecs_to_jiffies(TSC2005_TS_PENUP_TIME);
535 + ts->penup_timer.expires = jiffies + pu;
536 + add_timer(&ts->penup_timer);
537 + }
538 +out:
539 + return IRQ_HANDLED;
540 +}
541 +
542 +static void tsc2005_ts_setup_spi_xfer(struct tsc2005 *ts)
543 +{
544 + struct spi_message *m = &ts->read_msg;
545 + struct spi_transfer *x = &ts->read_xfer[0];
546 + int i;
547 +
548 + spi_message_init(m);
549 +
550 + for (i = 0; i < NUM_READ_REGS; i++, x++) {
551 + x->tx_buf = &tsc2005_read_reg[i];
552 + x->rx_buf = &ts->data[i];
553 + x->len = 4;
554 + x->bits_per_word = 24;
555 + x->cs_change = i < (NUM_READ_REGS - 1);
556 + spi_message_add_tail(x, m);
557 + }
558 +
559 + m->complete = tsc2005_ts_rx;
560 + m->context = ts;
561 +}
562 +
563 +static ssize_t tsc2005_ts_pen_down_show(struct device *dev,
564 + struct device_attribute *attr,
565 + char *buf)
566 +{
567 + struct tsc2005 *ts = dev_get_drvdata(dev);
568 +
569 + return sprintf(buf, "%u\n", ts->pen_down);
570 +}
571 +
572 +static DEVICE_ATTR(pen_down, S_IRUGO, tsc2005_ts_pen_down_show, NULL);
573 +
574 +static int tsc2005_configure(struct tsc2005 *ts, int flags)
575 +{
576 + tsc2005_write(ts, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE);
577 + tsc2005_write(ts, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE);
578 + tsc2005_write(ts, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE);
579 + tsc2005_cmd(ts, flags);
580 +
581 + return 0;
582 +}
583 +
584 +static void tsc2005_start_scan(struct tsc2005 *ts)
585 +{
586 + tsc2005_configure(ts, TSC2005_CMD_SCAN_XYZZ);
587 +}
588 +
589 +static void tsc2005_stop_scan(struct tsc2005 *ts)
590 +{
591 + tsc2005_cmd(ts, TSC2005_CMD_STOP);
592 +}
593 +
594 +/* Must be called with mutex held */
595 +static void tsc2005_disable(struct tsc2005 *ts)
596 +{
597 + if (ts->disable_depth++ != 0)
598 + return;
599 +
600 + disable_irq(ts->spi->irq);
601 + if (ts->esd_timeout)
602 + del_timer(&ts->esd_timer);
603 +
604 + /* wait until penup timer expire normally */
605 + do {
606 + msleep(4);
607 + } while (ts->sample_sent);
608 +
609 + tsc2005_stop_scan(ts);
610 +}
611 +
612 +static void tsc2005_enable(struct tsc2005 *ts)
613 +{
614 + if (ts->disable_depth != 1)
615 + goto out;
616 +
617 + if (ts->esd_timeout) {
618 + unsigned long wdj = msecs_to_jiffies(ts->esd_timeout);
619 + ts->esd_timer.expires = round_jiffies(jiffies+wdj);
620 + add_timer(&ts->esd_timer);
621 + }
622 + tsc2005_start_scan(ts);
623 + enable_irq(ts->spi->irq);
624 +out:
625 + --ts->disable_depth;
626 +}
627 +
628 +static ssize_t tsc2005_disable_show(struct device *dev,
629 + struct device_attribute *attr, char *buf)
630 +{
631 + struct tsc2005 *ts = dev_get_drvdata(dev);
632 +
633 + return sprintf(buf, "%u\n", ts->disabled);
634 +}
635 +
636 +static ssize_t tsc2005_disable_store(struct device *dev,
637 + struct device_attribute *attr,
638 + const char *buf, size_t count)
639 +{
640 + struct tsc2005 *ts = dev_get_drvdata(dev);
641 + unsigned long res;
642 + int i;
643 +
644 + if (strict_strtoul(buf, 10, &res) < 0)
645 + return -EINVAL;
646 + i = res ? 1 : 0;
647 +
648 + mutex_lock(&ts->mutex);
649 + if (i == ts->disabled)
650 + goto out;
651 + ts->disabled = i;
652 +
653 + if (i)
654 + tsc2005_disable(ts);
655 + else
656 + tsc2005_enable(ts);
657 +out:
658 + mutex_unlock(&ts->mutex);
659 + return count;
660 +}
661 +
662 +static DEVICE_ATTR(disable_ts, 0664, tsc2005_disable_show,
663 + tsc2005_disable_store);
664 +
665 +static ssize_t tsc2005_ctrl_selftest_show(struct device *dev,
666 + struct device_attribute *attr,
667 + char *buf)
668 +{
669 + u16 temp_high_orig, temp_high_test, temp_high;
670 + unsigned int result = 1;
671 + struct tsc2005 *ts = dev_get_drvdata(dev);
672 +
673 + if (!ts->set_reset) {
674 + dev_warn(&ts->spi->dev,
675 + "unable to selftest: reset not configured\n");
676 + result = 0;
677 + goto out;
678 + }
679 +
680 + mutex_lock(&ts->mutex);
681 + tsc2005_disable(ts);
682 +
683 + /* Test ctrl communications via temp high / low registers */
684 + tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig);
685 +
686 + temp_high_test = (temp_high_orig - 1) & 0x0FFF;
687 +
688 + tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test);
689 +
690 + tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
691 +
692 + if (temp_high != temp_high_test) {
693 + result = 0;
694 + dev_warn(dev, "selftest failed: %d != %d\n",
695 + temp_high, temp_high_test);
696 + }
697 +
698 + /* HW Reset */
699 + ts->set_reset(0);
700 + msleep(1); /* only 10us required */
701 + ts->set_reset(1);
702 +
703 + tsc2005_enable(ts);
704 +
705 + /* Test that reset really happened */
706 + tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
707 +
708 + if (temp_high != temp_high_orig) {
709 + result = 0;
710 + dev_warn(dev, "selftest failed after reset: "
711 + "%d != %d\n",
712 + temp_high, temp_high_orig);
713 + }
714 +
715 + mutex_unlock(&ts->mutex);
716 +
717 +out:
718 + return sprintf(buf, "%u\n", result);
719 +}
720 +
721 +static DEVICE_ATTR(ts_ctrl_selftest, S_IRUGO, tsc2005_ctrl_selftest_show, NULL);
722 +
723 +static void tsc2005_esd_timer_handler(unsigned long data)
724 +{
725 + struct tsc2005 *ts = (struct tsc2005 *)data;
726 + if (!ts->disable_depth)
727 + schedule_work(&ts->esd_work);
728 +}
729 +
730 +static void tsc2005_rst_handler(struct work_struct *work)
731 +{
732 + u16 reg_val;
733 + struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work);
734 + unsigned long wdj;
735 +
736 + mutex_lock(&ts->mutex);
737 +
738 + /* If we are disabled, or the a touch has been detected,
739 + * then ignore this timeout. The enable will restart the
740 + * watchdog, as it restarts scanning
741 + */
742 + if (ts->disable_depth)
743 + goto out;
744 +
745 + /* If we cannot read our known value from configuration register 0
746 + * then reset the controller as if from power-up and start
747 + * scanning again. Always re-arm the watchdog.
748 + */
749 + tsc2005_read(ts, TSC2005_REG_CFR0, &reg_val);
750 + if ((reg_val ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK) {
751 + dev_info(&ts->spi->dev, "TSC not responding, resetting.\n");
752 + /* If this timer kicked in, the penup timer, if ever active
753 + * at all, must have expired ages ago, so no need to del it.
754 + */
755 + ts->set_reset(0);
756 + if (ts->sample_sent) {
757 + tsc2005_ts_update_pen_state(ts, 0, 0, 0);
758 + ts->sample_sent = 0;
759 + }
760 + ts->spi_pending = 0;
761 + msleep(1); /* only 10us required */
762 + ts->set_reset(1);
763 + tsc2005_start_scan(ts);
764 + }
765 + wdj = msecs_to_jiffies(ts->esd_timeout);
766 + mod_timer(&ts->esd_timer, round_jiffies(jiffies+wdj));
767 +
768 +out:
769 + mutex_unlock(&ts->mutex);
770 +}
771 +
772 +static int __devinit tsc2005_ts_init(struct tsc2005 *ts,
773 + struct tsc2005_platform_data *pdata)
774 +{
775 + struct input_dev *idev;
776 + int r;
777 + int x_max, y_max;
778 +
779 + init_timer(&ts->penup_timer);
780 + setup_timer(&ts->penup_timer, tsc2005_ts_penup_timer_handler,
781 + (unsigned long)ts);
782 +
783 + spin_lock_init(&ts->lock);
784 + mutex_init(&ts->mutex);
785 +
786 + ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
787 + ts->hw_avg_max = pdata->ts_hw_avg;
788 + ts->stab_time = pdata->ts_stab_time;
789 + x_max = pdata->ts_x_max ? : 4096;
790 + ts->fudge_x = pdata->ts_x_fudge ? : 4;
791 + y_max = pdata->ts_y_max ? : 4096;
792 + ts->fudge_y = pdata->ts_y_fudge ? : 8;
793 + ts->p_max = pdata->ts_pressure_max ? : MAX_12BIT;
794 + ts->touch_pressure = pdata->ts_touch_pressure ? : ts->p_max;
795 + ts->fudge_p = pdata->ts_pressure_fudge ? : 2;
796 +
797 + ts->set_reset = pdata->set_reset;
798 +
799 + idev = input_allocate_device();
800 + if (idev == NULL) {
801 + r = -ENOMEM;
802 + goto err1;
803 + }
804 +
805 + idev->name = "TSC2005 touchscreen";
806 + snprintf(ts->phys, sizeof(ts->phys), "%s/input-ts",
807 + dev_name(&ts->spi->dev));
808 + idev->phys = ts->phys;
809 +
810 + idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
811 + idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
812 + idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
813 + ts->idev = idev;
814 +
815 + tsc2005_ts_setup_spi_xfer(ts);
816 +
817 + input_set_abs_params(idev, ABS_X, 0, x_max, ts->fudge_x, 0);
818 + input_set_abs_params(idev, ABS_Y, 0, y_max, ts->fudge_y, 0);
819 + input_set_abs_params(idev, ABS_PRESSURE, 0, ts->p_max, ts->fudge_p, 0);
820 +
821 + tsc2005_start_scan(ts);
822 +
823 + r = request_irq(ts->spi->irq, tsc2005_ts_irq_handler,
824 + (((TSC2005_CFR2_INITVALUE & TSC2005_CFR2_IRQ_MASK) ==
825 + TSC2005_CFR2_IRQ_PENDAV)
826 + ? IRQF_TRIGGER_RISING
827 + : IRQF_TRIGGER_FALLING) |
828 + IRQF_DISABLED, "tsc2005", ts);
829 + if (r < 0) {
830 + dev_err(&ts->spi->dev, "unable to get DAV IRQ");
831 + goto err2;
832 + }
833 +
834 + set_irq_wake(ts->spi->irq, 1);
835 +
836 + r = input_register_device(idev);
837 + if (r < 0) {
838 + dev_err(&ts->spi->dev, "can't register touchscreen device\n");
839 + goto err3;
840 + }
841 +
842 + /* We can tolerate these failing */
843 + r = device_create_file(&ts->spi->dev, &dev_attr_ts_ctrl_selftest);
844 + if (r < 0)
845 + dev_warn(&ts->spi->dev, "can't create sysfs file for %s: %d\n",
846 + dev_attr_ts_ctrl_selftest.attr.name, r);
847 +
848 + r = device_create_file(&ts->spi->dev, &dev_attr_pen_down);
849 + if (r < 0)
850 + dev_warn(&ts->spi->dev, "can't create sysfs file for %s: %d\n",
851 + dev_attr_pen_down.attr.name, r);
852 +
853 + r = device_create_file(&ts->spi->dev, &dev_attr_disable_ts);
854 + if (r < 0)
855 + dev_warn(&ts->spi->dev, "can't create sysfs file for %s: %d\n",
856 + dev_attr_disable_ts.attr.name, r);
857 +
858 + /* Finally, configure and start the optional EDD watchdog. */
859 + ts->esd_timeout = pdata->esd_timeout;
860 + if (ts->esd_timeout && ts->set_reset) {
861 + unsigned long wdj;
862 + setup_timer(&ts->esd_timer, tsc2005_esd_timer_handler,
863 + (unsigned long)ts);
864 + INIT_WORK(&ts->esd_work, tsc2005_rst_handler);
865 + wdj = msecs_to_jiffies(ts->esd_timeout);
866 + ts->esd_timer.expires = round_jiffies(jiffies+wdj);
867 + add_timer(&ts->esd_timer);
868 + }
869 +
870 + return 0;
871 +err3:
872 + free_irq(ts->spi->irq, ts);
873 +err2:
874 + tsc2005_stop_scan(ts);
875 + input_free_device(idev);
876 +err1:
877 + return r;
878 +}
879 +
880 +static int __devinit tsc2005_probe(struct spi_device *spi)
881 +{
882 + struct tsc2005 *ts;
883 + struct tsc2005_platform_data *pdata = spi->dev.platform_data;
884 + int r;
885 +
886 + if (spi->irq < 0) {
887 + dev_dbg(&spi->dev, "no irq?\n");
888 + return -ENODEV;
889 + }
890 + if (!pdata) {
891 + dev_dbg(&spi->dev, "no platform data?\n");
892 + return -ENODEV;
893 + }
894 +
895 + ts = kzalloc(sizeof(*ts), GFP_KERNEL);
896 + if (ts == NULL)
897 + return -ENOMEM;
898 +
899 + dev_set_drvdata(&spi->dev, ts);
900 + ts->spi = spi;
901 + spi->dev.power.power_state = PMSG_ON;
902 +
903 + spi->mode = SPI_MODE_0;
904 + spi->bits_per_word = 8;
905 + /* The max speed might've been defined by the board-specific
906 + * struct */
907 + if (!spi->max_speed_hz)
908 + spi->max_speed_hz = TSC2005_HZ;
909 +
910 + spi_setup(spi);
911 +
912 + r = tsc2005_ts_init(ts, pdata);
913 + if (r)
914 + goto err1;
915 +
916 + return 0;
917 +
918 +err1:
919 + kfree(ts);
920 + return r;
921 +}
922 +
923 +static int __devexit tsc2005_remove(struct spi_device *spi)
924 +{
925 + struct tsc2005 *ts = dev_get_drvdata(&spi->dev);
926 +
927 + mutex_lock(&ts->mutex);
928 + tsc2005_disable(ts);
929 + mutex_unlock(&ts->mutex);
930 +
931 + device_remove_file(&ts->spi->dev, &dev_attr_disable_ts);
932 + device_remove_file(&ts->spi->dev, &dev_attr_pen_down);
933 + device_remove_file(&ts->spi->dev, &dev_attr_ts_ctrl_selftest);
934 +
935 + free_irq(ts->spi->irq, ts);
936 + input_unregister_device(ts->idev);
937 +
938 + if (ts->esd_timeout)
939 + del_timer(&ts->esd_timer);
940 + kfree(ts);
941 +
942 + return 0;
943 +}
944 +
945 +#ifdef CONFIG_PM
946 +static int tsc2005_suspend(struct spi_device *spi, pm_message_t mesg)
947 +{
948 + struct tsc2005 *ts = dev_get_drvdata(&spi->dev);
949 +
950 + mutex_lock(&ts->mutex);
951 + tsc2005_disable(ts);
952 + mutex_unlock(&ts->mutex);
953 +
954 + return 0;
955 +}
956 +
957 +static int tsc2005_resume(struct spi_device *spi)
958 +{
959 + struct tsc2005 *ts = dev_get_drvdata(&spi->dev);
960 +
961 + mutex_lock(&ts->mutex);
962 + tsc2005_enable(ts);
963 + mutex_unlock(&ts->mutex);
964 +
965 + return 0;
966 +}
967 +#endif
968 +
969 +static struct spi_driver tsc2005_driver = {
970 + .driver = {
971 + .name = "tsc2005",
972 + .owner = THIS_MODULE,
973 + },
974 +#ifdef CONFIG_PM
975 + .suspend = tsc2005_suspend,
976 + .resume = tsc2005_resume,
977 +#endif
978 + .probe = tsc2005_probe,
979 + .remove = __devexit_p(tsc2005_remove),
980 +};
981 +
982 +static int __init tsc2005_init(void)
983 +{
984 + printk(KERN_INFO "TSC2005 driver initializing\n");
985 +
986 + return spi_register_driver(&tsc2005_driver);
987 +}
988 +module_init(tsc2005_init);
989 +
990 +static void __exit tsc2005_exit(void)
991 +{
992 + spi_unregister_driver(&tsc2005_driver);
993 +}
994 +module_exit(tsc2005_exit);
995 +
996 +MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
997 +MODULE_LICENSE("GPL");
998 +MODULE_ALIAS("platform:tsc2005");
999 --- /dev/null
1000 +++ b/include/linux/spi/tsc2005.h
1001 @@ -0,0 +1,30 @@
1002 +#ifndef _LINUX_SPI_TSC2005_H
1003 +#define _LINUX_SPI_TSC2005_H
1004 +
1005 +#include <linux/types.h>
1006 +
1007 +struct tsc2005_platform_data {
1008 + u16 ts_x_plate_ohm;
1009 + u32 ts_stab_time; /* voltage settling time */
1010 + u8 ts_hw_avg; /* HW assiseted averaging. Can be
1011 + 0, 4, 8, 16 samples per reading */
1012 + u32 ts_touch_pressure; /* Pressure limit until we report a
1013 + touch event. After that we switch
1014 + to ts_max_pressure. */
1015 + u32 ts_pressure_max;/* Samples with bigger pressure value will
1016 + be ignored, since the corresponding X, Y
1017 + values are unreliable */
1018 + u32 ts_pressure_fudge;
1019 + u32 ts_x_max;
1020 + u32 ts_x_fudge;
1021 + u32 ts_y_max;
1022 + u32 ts_y_fudge;
1023 +
1024 + u32 esd_timeout; /* msec of inactivity before we check */
1025 +
1026 + unsigned ts_ignore_last:1;
1027 +
1028 + void (*set_reset)(bool enable);
1029 +};
1030 +
1031 +#endif
This page took 0.090669 seconds and 5 git commands to generate.