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