Merge current mainline tree into linux-omap tree
[pandora-kernel.git] / drivers / input / touchscreen / ads7846.c
1 /*
2  * ADS7846 based touchscreen and sensor driver
3  *
4  * Copyright (c) 2005 David Brownell
5  * Copyright (c) 2006 Nokia Corporation
6  * Various changes: Imre Deak <imre.deak@nokia.com>
7  *
8  * Using code from:
9  *  - corgi_ts.c
10  *      Copyright (C) 2004-2005 Richard Purdie
11  *  - omap_ts.[hc], ads7846.h, ts_osk.c
12  *      Copyright (C) 2002 MontaVista Software
13  *      Copyright (C) 2004 Texas Instruments
14  *      Copyright (C) 2005 Dirk Behme
15  *
16  *  This program is free software; you can redistribute it and/or modify
17  *  it under the terms of the GNU General Public License version 2 as
18  *  published by the Free Software Foundation.
19  */
20 #include <linux/hwmon.h>
21 #include <linux/init.h>
22 #include <linux/err.h>
23 #include <linux/delay.h>
24 #include <linux/input.h>
25 #include <linux/interrupt.h>
26 #include <linux/slab.h>
27 #include <linux/spi/spi.h>
28 #include <linux/spi/ads7846.h>
29 #include <asm/irq.h>
30
31
32 /*
33  * This code has been heavily tested on a Nokia 770, and lightly
34  * tested on other ads7846 devices (OSK/Mistral, Lubbock).
35  * TSC2046 is just newer ads7846 silicon.
36  * Support for ads7843 tested on Atmel at91sam926x-EK.
37  * Support for ads7845 has only been stubbed in.
38  *
39  * IRQ handling needs a workaround because of a shortcoming in handling
40  * edge triggered IRQs on some platforms like the OMAP1/2. These
41  * platforms don't handle the ARM lazy IRQ disabling properly, thus we
42  * have to maintain our own SW IRQ disabled status. This should be
43  * removed as soon as the affected platform's IRQ handling is fixed.
44  *
45  * app note sbaa036 talks in more detail about accurate sampling...
46  * that ought to help in situations like LCDs inducing noise (which
47  * can also be helped by using synch signals) and more generally.
48  * This driver tries to utilize the measures described in the app
49  * note. The strength of filtering can be set in the board-* specific
50  * files.
51  */
52
53 #define TS_POLL_DELAY   (1 * 1000000)   /* ns delay before the first sample */
54 #define TS_POLL_PERIOD  (5 * 1000000)   /* ns delay between samples */
55
56 /* this driver doesn't aim at the peak continuous sample rate */
57 #define SAMPLE_BITS     (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
58
59 struct ts_event {
60         /* For portability, we can't read 12 bit values using SPI (which
61          * would make the controller deliver them as native byteorder u16
62          * with msbs zeroed).  Instead, we read them as two 8-bit values,
63          * *** WHICH NEED BYTESWAPPING *** and range adjustment.
64          */
65         u16     x;
66         u16     y;
67         u16     z1, z2;
68         int     ignore;
69 };
70
71 struct ads7846 {
72         struct input_dev        *input;
73         char                    phys[32];
74
75         struct spi_device       *spi;
76
77 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
78         struct attribute_group  *attr_group;
79         struct device           *hwmon;
80 #endif
81
82         u16                     model;
83         u16                     vref_mv;
84         u16                     vref_delay_usecs;
85         u16                     x_plate_ohms;
86         u16                     pressure_max;
87
88         u8                      read_x, read_y, read_z1, read_z2, pwrdown;
89         u16                     dummy;          /* for the pwrdown read */
90         struct ts_event         tc;
91
92         struct spi_transfer     xfer[18];
93         struct spi_message      msg[5];
94         struct spi_message      *last_msg;
95         int                     msg_idx;
96         int                     read_cnt;
97         int                     read_rep;
98         int                     last_read;
99
100         u16                     debounce_max;
101         u16                     debounce_tol;
102         u16                     debounce_rep;
103
104         u16                     penirq_recheck_delay_usecs;
105
106         spinlock_t              lock;
107         struct hrtimer          timer;
108         unsigned                pendown:1;      /* P: lock */
109         unsigned                pending:1;      /* P: lock */
110 // FIXME remove "irq_disabled"
111         unsigned                irq_disabled:1; /* P: lock */
112         unsigned                disabled:1;
113         unsigned                is_suspended:1;
114
115         int                     (*filter)(void *data, int data_idx, int *val);
116         void                    *filter_data;
117         void                    (*filter_cleanup)(void *data);
118         int                     (*get_pendown_state)(void);
119 };
120
121 /* leave chip selected when we're done, for quicker re-select? */
122 #if     0
123 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
124 #else
125 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
126 #endif
127
128 /*--------------------------------------------------------------------------*/
129
130 /* The ADS7846 has touchscreen and other sensors.
131  * Earlier ads784x chips are somewhat compatible.
132  */
133 #define ADS_START               (1 << 7)
134 #define ADS_A2A1A0_d_y          (1 << 4)        /* differential */
135 #define ADS_A2A1A0_d_z1         (3 << 4)        /* differential */
136 #define ADS_A2A1A0_d_z2         (4 << 4)        /* differential */
137 #define ADS_A2A1A0_d_x          (5 << 4)        /* differential */
138 #define ADS_A2A1A0_temp0        (0 << 4)        /* non-differential */
139 #define ADS_A2A1A0_vbatt        (2 << 4)        /* non-differential */
140 #define ADS_A2A1A0_vaux         (6 << 4)        /* non-differential */
141 #define ADS_A2A1A0_temp1        (7 << 4)        /* non-differential */
142 #define ADS_8_BIT               (1 << 3)
143 #define ADS_12_BIT              (0 << 3)
144 #define ADS_SER                 (1 << 2)        /* non-differential */
145 #define ADS_DFR                 (0 << 2)        /* differential */
146 #define ADS_PD10_PDOWN          (0 << 0)        /* lowpower mode + penirq */
147 #define ADS_PD10_ADC_ON         (1 << 0)        /* ADC on */
148 #define ADS_PD10_REF_ON         (2 << 0)        /* vREF on + penirq */
149 #define ADS_PD10_ALL_ON         (3 << 0)        /* ADC + vREF on */
150
151 #define MAX_12BIT       ((1<<12)-1)
152
153 /* leave ADC powered up (disables penirq) between differential samples */
154 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
155         | ADS_12_BIT | ADS_DFR | \
156         (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
157
158 #define READ_Y(vref)    (READ_12BIT_DFR(y,  1, vref))
159 #define READ_Z1(vref)   (READ_12BIT_DFR(z1, 1, vref))
160 #define READ_Z2(vref)   (READ_12BIT_DFR(z2, 1, vref))
161
162 #define READ_X(vref)    (READ_12BIT_DFR(x,  1, vref))
163 #define PWRDOWN         (READ_12BIT_DFR(y,  0, 0))      /* LAST */
164
165 /* single-ended samples need to first power up reference voltage;
166  * we leave both ADC and VREF powered
167  */
168 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
169         | ADS_12_BIT | ADS_SER)
170
171 #define REF_ON  (READ_12BIT_DFR(x, 1, 1))
172 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
173
174 /*--------------------------------------------------------------------------*/
175
176 /*
177  * Non-touchscreen sensors only use single-ended conversions.
178  * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
179  * ads7846 lets that pin be unconnected, to use internal vREF.
180  */
181
182 struct ser_req {
183         u8                      ref_on;
184         u8                      command;
185         u8                      ref_off;
186         u16                     scratch;
187         __be16                  sample;
188         struct spi_message      msg;
189         struct spi_transfer     xfer[6];
190 };
191
192 static void ads7846_enable(struct ads7846 *ts);
193 static void ads7846_disable(struct ads7846 *ts);
194
195 static int device_suspended(struct device *dev)
196 {
197         struct ads7846 *ts = dev_get_drvdata(dev);
198         return ts->is_suspended || ts->disabled;
199 }
200
201 static int ads7846_read12_ser(struct device *dev, unsigned command)
202 {
203         struct spi_device       *spi = to_spi_device(dev);
204         struct ads7846          *ts = dev_get_drvdata(dev);
205         struct ser_req          *req = kzalloc(sizeof *req, GFP_KERNEL);
206         int                     status;
207         int                     use_internal;
208
209         if (!req)
210                 return -ENOMEM;
211
212         spi_message_init(&req->msg);
213
214         /* FIXME boards with ads7846 might use external vref instead ... */
215         use_internal = (ts->model == 7846);
216
217         /* maybe turn on internal vREF, and let it settle */
218         if (use_internal) {
219                 req->ref_on = REF_ON;
220                 req->xfer[0].tx_buf = &req->ref_on;
221                 req->xfer[0].len = 1;
222                 spi_message_add_tail(&req->xfer[0], &req->msg);
223
224                 req->xfer[1].rx_buf = &req->scratch;
225                 req->xfer[1].len = 2;
226
227                 /* for 1uF, settle for 800 usec; no cap, 100 usec.  */
228                 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
229                 spi_message_add_tail(&req->xfer[1], &req->msg);
230         }
231
232         /* take sample */
233         req->command = (u8) command;
234         req->xfer[2].tx_buf = &req->command;
235         req->xfer[2].len = 1;
236         spi_message_add_tail(&req->xfer[2], &req->msg);
237
238         req->xfer[3].rx_buf = &req->sample;
239         req->xfer[3].len = 2;
240         spi_message_add_tail(&req->xfer[3], &req->msg);
241
242         /* REVISIT:  take a few more samples, and compare ... */
243
244         /* converter in low power mode & enable PENIRQ */
245         req->ref_off = PWRDOWN;
246         req->xfer[4].tx_buf = &req->ref_off;
247         req->xfer[4].len = 1;
248         spi_message_add_tail(&req->xfer[4], &req->msg);
249
250         req->xfer[5].rx_buf = &req->scratch;
251         req->xfer[5].len = 2;
252         CS_CHANGE(req->xfer[5]);
253         spi_message_add_tail(&req->xfer[5], &req->msg);
254
255         ts->irq_disabled = 1;
256         disable_irq(spi->irq);
257         status = spi_sync(spi, &req->msg);
258         ts->irq_disabled = 0;
259         enable_irq(spi->irq);
260
261         if (status == 0) {
262                 /* on-wire is a must-ignore bit, a BE12 value, then padding */
263                 status = be16_to_cpu(req->sample);
264                 status = status >> 3;
265                 status &= 0x0fff;
266         }
267
268         kfree(req);
269         return status;
270 }
271
272 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
273
274 #define SHOW(name, var, adjust) static ssize_t \
275 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
276 { \
277         struct ads7846 *ts = dev_get_drvdata(dev); \
278         ssize_t v = ads7846_read12_ser(dev, \
279                         READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
280         if (v < 0) \
281                 return v; \
282         return sprintf(buf, "%u\n", adjust(ts, v)); \
283 } \
284 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
285
286
287 /* Sysfs conventions report temperatures in millidegrees Celcius.
288  * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
289  * accuracy scheme without calibration data.  For now we won't try either;
290  * userspace sees raw sensor values, and must scale/calibrate appropriately.
291  */
292 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
293 {
294         return v;
295 }
296
297 SHOW(temp0, temp0, null_adjust)         /* temp1_input */
298 SHOW(temp1, temp1, null_adjust)         /* temp2_input */
299
300
301 /* sysfs conventions report voltages in millivolts.  We can convert voltages
302  * if we know vREF.  userspace may need to scale vAUX to match the board's
303  * external resistors; we assume that vBATT only uses the internal ones.
304  */
305 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
306 {
307         unsigned retval = v;
308
309         /* external resistors may scale vAUX into 0..vREF */
310         retval *= ts->vref_mv;
311         retval = retval >> 12;
312         return retval;
313 }
314
315 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
316 {
317         unsigned retval = vaux_adjust(ts, v);
318
319         /* ads7846 has a resistor ladder to scale this signal down */
320         if (ts->model == 7846)
321                 retval *= 4;
322         return retval;
323 }
324
325 SHOW(in0_input, vaux, vaux_adjust)
326 SHOW(in1_input, vbatt, vbatt_adjust)
327
328
329 static struct attribute *ads7846_attributes[] = {
330         &dev_attr_temp0.attr,
331         &dev_attr_temp1.attr,
332         &dev_attr_in0_input.attr,
333         &dev_attr_in1_input.attr,
334         NULL,
335 };
336
337 static struct attribute_group ads7846_attr_group = {
338         .attrs = ads7846_attributes,
339 };
340
341 static struct attribute *ads7843_attributes[] = {
342         &dev_attr_in0_input.attr,
343         &dev_attr_in1_input.attr,
344         NULL,
345 };
346
347 static struct attribute_group ads7843_attr_group = {
348         .attrs = ads7843_attributes,
349 };
350
351 static struct attribute *ads7845_attributes[] = {
352         &dev_attr_in0_input.attr,
353         NULL,
354 };
355
356 static struct attribute_group ads7845_attr_group = {
357         .attrs = ads7845_attributes,
358 };
359
360 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
361 {
362         struct device *hwmon;
363         int err;
364
365         /* hwmon sensors need a reference voltage */
366         switch (ts->model) {
367         case 7846:
368                 if (!ts->vref_mv) {
369                         dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
370                         ts->vref_mv = 2500;
371                 }
372                 break;
373         case 7845:
374         case 7843:
375                 if (!ts->vref_mv) {
376                         dev_warn(&spi->dev,
377                                 "external vREF for ADS%d not specified\n",
378                                 ts->model);
379                         return 0;
380                 }
381                 break;
382         }
383
384         /* different chips have different sensor groups */
385         switch (ts->model) {
386         case 7846:
387                 ts->attr_group = &ads7846_attr_group;
388                 break;
389         case 7845:
390                 ts->attr_group = &ads7845_attr_group;
391                 break;
392         case 7843:
393                 ts->attr_group = &ads7843_attr_group;
394                 break;
395         default:
396                 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
397                 return 0;
398         }
399
400         err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
401         if (err)
402                 return err;
403
404         hwmon = hwmon_device_register(&spi->dev);
405         if (IS_ERR(hwmon)) {
406                 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
407                 return PTR_ERR(hwmon);
408         }
409
410         ts->hwmon = hwmon;
411         return 0;
412 }
413
414 static void ads784x_hwmon_unregister(struct spi_device *spi,
415                                      struct ads7846 *ts)
416 {
417         if (ts->hwmon) {
418                 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
419                 hwmon_device_unregister(ts->hwmon);
420         }
421 }
422
423 #else
424 static inline int ads784x_hwmon_register(struct spi_device *spi,
425                                          struct ads7846 *ts)
426 {
427         return 0;
428 }
429
430 static inline void ads784x_hwmon_unregister(struct spi_device *spi,
431                                             struct ads7846 *ts)
432 {
433 }
434 #endif
435
436 static int is_pen_down(struct device *dev)
437 {
438         struct ads7846  *ts = dev_get_drvdata(dev);
439
440         return ts->pendown;
441 }
442
443 static ssize_t ads7846_pen_down_show(struct device *dev,
444                                      struct device_attribute *attr, char *buf)
445 {
446         return sprintf(buf, "%u\n", is_pen_down(dev));
447 }
448
449 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
450
451 static ssize_t ads7846_disable_show(struct device *dev,
452                                      struct device_attribute *attr, char *buf)
453 {
454         struct ads7846  *ts = dev_get_drvdata(dev);
455
456         return sprintf(buf, "%u\n", ts->disabled);
457 }
458
459 static ssize_t ads7846_disable_store(struct device *dev,
460                                      struct device_attribute *attr,
461                                      const char *buf, size_t count)
462 {
463         struct ads7846 *ts = dev_get_drvdata(dev);
464         char *endp;
465         int i;
466
467         i = simple_strtoul(buf, &endp, 10);
468         spin_lock_irq(&ts->lock);
469
470         if (i)
471                 ads7846_disable(ts);
472         else
473                 ads7846_enable(ts);
474
475         spin_unlock_irq(&ts->lock);
476
477         return count;
478 }
479
480 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
481
482 static struct attribute *ads784x_attributes[] = {
483         &dev_attr_pen_down.attr,
484         &dev_attr_disable.attr,
485         NULL,
486 };
487
488 static struct attribute_group ads784x_attr_group = {
489         .attrs = ads784x_attributes,
490 };
491
492 /*--------------------------------------------------------------------------*/
493
494 /*
495  * PENIRQ only kicks the timer.  The timer only reissues the SPI transfer,
496  * to retrieve touchscreen status.
497  *
498  * The SPI transfer completion callback does the real work.  It reports
499  * touchscreen events and reactivates the timer (or IRQ) as appropriate.
500  */
501
502 static void ads7846_rx(void *ads)
503 {
504         struct ads7846          *ts = ads;
505         unsigned                Rt;
506         u16                     x, y, z1, z2;
507
508         /* ads7846_rx_val() did in-place conversion (including byteswap) from
509          * on-the-wire format as part of debouncing to get stable readings.
510          */
511         x = ts->tc.x;
512         y = ts->tc.y;
513         z1 = ts->tc.z1;
514         z2 = ts->tc.z2;
515
516         /* range filtering */
517         if (x == MAX_12BIT)
518                 x = 0;
519
520         if (ts->model == 7843) {
521                 Rt = ts->pressure_max / 2;
522         } else if (likely(x && z1)) {
523                 /* compute touch pressure resistance using equation #2 */
524                 Rt = z2;
525                 Rt -= z1;
526                 Rt *= x;
527                 Rt *= ts->x_plate_ohms;
528                 Rt /= z1;
529                 Rt = (Rt + 2047) >> 12;
530         } else {
531                 Rt = 0;
532         }
533
534         /* Sample found inconsistent by debouncing or pressure is beyond
535          * the maximum. Don't report it to user space, repeat at least
536          * once more the measurement
537          */
538         if (ts->tc.ignore || Rt > ts->pressure_max) {
539 #ifdef VERBOSE
540                 pr_debug("%s: ignored %d pressure %d\n",
541                         ts->spi->dev.bus_id, ts->tc.ignore, Rt);
542 #endif
543                 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
544                               HRTIMER_MODE_REL);
545                 return;
546         }
547
548         /* Maybe check the pendown state before reporting. This discards
549          * false readings when the pen is lifted.
550          */
551         if (ts->penirq_recheck_delay_usecs) {
552                 udelay(ts->penirq_recheck_delay_usecs);
553                 if (!ts->get_pendown_state())
554                         Rt = 0;
555         }
556
557         /* NOTE: We can't rely on the pressure to determine the pen down
558          * state, even this controller has a pressure sensor.  The pressure
559          * value can fluctuate for quite a while after lifting the pen and
560          * in some cases may not even settle at the expected value.
561          *
562          * The only safe way to check for the pen up condition is in the
563          * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
564          */
565         if (Rt) {
566                 struct input_dev *input = ts->input;
567
568                 if (!ts->pendown) {
569                         input_report_key(input, BTN_TOUCH, 1);
570                         ts->pendown = 1;
571 #ifdef VERBOSE
572                         dev_dbg(&ts->spi->dev, "DOWN\n");
573 #endif
574                 }
575                 input_report_abs(input, ABS_X, x);
576                 input_report_abs(input, ABS_Y, y);
577                 input_report_abs(input, ABS_PRESSURE, Rt);
578
579                 input_sync(input);
580 #ifdef VERBOSE
581                 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
582 #endif
583         }
584
585         hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
586                         HRTIMER_MODE_REL);
587 }
588
589 static int ads7846_debounce(void *ads, int data_idx, int *val)
590 {
591         struct ads7846          *ts = ads;
592
593         if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
594                 /* Start over collecting consistent readings. */
595                 ts->read_rep = 0;
596                 /* Repeat it, if this was the first read or the read
597                  * wasn't consistent enough. */
598                 if (ts->read_cnt < ts->debounce_max) {
599                         ts->last_read = *val;
600                         ts->read_cnt++;
601                         return ADS7846_FILTER_REPEAT;
602                 } else {
603                         /* Maximum number of debouncing reached and still
604                          * not enough number of consistent readings. Abort
605                          * the whole sample, repeat it in the next sampling
606                          * period.
607                          */
608                         ts->read_cnt = 0;
609                         return ADS7846_FILTER_IGNORE;
610                 }
611         } else {
612                 if (++ts->read_rep > ts->debounce_rep) {
613                         /* Got a good reading for this coordinate,
614                          * go for the next one. */
615                         ts->read_cnt = 0;
616                         ts->read_rep = 0;
617                         return ADS7846_FILTER_OK;
618                 } else {
619                         /* Read more values that are consistent. */
620                         ts->read_cnt++;
621                         return ADS7846_FILTER_REPEAT;
622                 }
623         }
624 }
625
626 static int ads7846_no_filter(void *ads, int data_idx, int *val)
627 {
628         return ADS7846_FILTER_OK;
629 }
630
631 static void ads7846_rx_val(void *ads)
632 {
633         struct ads7846 *ts = ads;
634         struct spi_message *m;
635         struct spi_transfer *t;
636         int val;
637         int action;
638         int status;
639
640         m = &ts->msg[ts->msg_idx];
641         t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
642
643         /* adjust:  on-wire is a must-ignore bit, a BE12 value, then padding;
644          * built from two 8 bit values written msb-first.
645          */
646         val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
647
648         action = ts->filter(ts->filter_data, ts->msg_idx, &val);
649         switch (action) {
650         case ADS7846_FILTER_REPEAT:
651                 break;
652         case ADS7846_FILTER_IGNORE:
653                 ts->tc.ignore = 1;
654                 /* Last message will contain ads7846_rx() as the
655                  * completion function.
656                  */
657                 m = ts->last_msg;
658                 break;
659         case ADS7846_FILTER_OK:
660                 *(u16 *)t->rx_buf = val;
661                 ts->tc.ignore = 0;
662                 m = &ts->msg[++ts->msg_idx];
663                 break;
664         default:
665                 BUG();
666         }
667         status = spi_async(ts->spi, m);
668         if (status)
669                 dev_err(&ts->spi->dev, "spi_async --> %d\n",
670                                 status);
671 }
672
673 static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
674 {
675         struct ads7846  *ts = container_of(handle, struct ads7846, timer);
676         int             status = 0;
677
678         spin_lock_irq(&ts->lock);
679
680         if (unlikely(!ts->get_pendown_state() ||
681                      device_suspended(&ts->spi->dev))) {
682                 if (ts->pendown) {
683                         struct input_dev *input = ts->input;
684
685                         input_report_key(input, BTN_TOUCH, 0);
686                         input_report_abs(input, ABS_PRESSURE, 0);
687                         input_sync(input);
688
689                         ts->pendown = 0;
690 #ifdef VERBOSE
691                         dev_dbg(&ts->spi->dev, "UP\n");
692 #endif
693                 }
694
695                 /* measurement cycle ended */
696                 if (!device_suspended(&ts->spi->dev)) {
697                         ts->irq_disabled = 0;
698                         enable_irq(ts->spi->irq);
699                 }
700                 ts->pending = 0;
701         } else {
702                 /* pen is still down, continue with the measurement */
703                 ts->msg_idx = 0;
704                 status = spi_async(ts->spi, &ts->msg[0]);
705                 if (status)
706                         dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
707         }
708
709         spin_unlock_irq(&ts->lock);
710         return HRTIMER_NORESTART;
711 }
712
713 static irqreturn_t ads7846_irq(int irq, void *handle)
714 {
715         struct ads7846 *ts = handle;
716         unsigned long flags;
717
718         spin_lock_irqsave(&ts->lock, flags);
719         if (likely(ts->get_pendown_state())) {
720                 if (!ts->irq_disabled) {
721                         /* The ARM do_simple_IRQ() dispatcher doesn't act
722                          * like the other dispatchers:  it will report IRQs
723                          * even after they've been disabled.  We work around
724                          * that here.  (The "generic irq" framework may help...)
725                          */
726                         ts->irq_disabled = 1;
727                         disable_irq(ts->spi->irq);
728                         ts->pending = 1;
729                         hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
730                                         HRTIMER_MODE_REL);
731                 }
732         }
733         spin_unlock_irqrestore(&ts->lock, flags);
734
735         return IRQ_HANDLED;
736 }
737
738 /*--------------------------------------------------------------------------*/
739
740 /* Must be called with ts->lock held */
741 static void ads7846_disable(struct ads7846 *ts)
742 {
743         if (ts->disabled)
744                 return;
745
746         ts->disabled = 1;
747
748         /* are we waiting for IRQ, or polling? */
749         if (!ts->pending) {
750                 ts->irq_disabled = 1;
751                 disable_irq(ts->spi->irq);
752         } else {
753                 /* the timer will run at least once more, and
754                  * leave everything in a clean state, IRQ disabled
755                  */
756                 while (ts->pending) {
757                         spin_unlock_irq(&ts->lock);
758                         msleep(1);
759                         spin_lock_irq(&ts->lock);
760                 }
761         }
762
763         /* we know the chip's in lowpower mode since we always
764          * leave it that way after every request
765          */
766
767 }
768
769 /* Must be called with ts->lock held */
770 static void ads7846_enable(struct ads7846 *ts)
771 {
772         if (!ts->disabled)
773                 return;
774
775         ts->disabled = 0;
776         ts->irq_disabled = 0;
777         enable_irq(ts->spi->irq);
778 }
779
780 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
781 {
782         struct ads7846 *ts = dev_get_drvdata(&spi->dev);
783
784         spin_lock_irq(&ts->lock);
785
786         ts->is_suspended = 1;
787         ads7846_disable(ts);
788
789         spin_unlock_irq(&ts->lock);
790
791         return 0;
792
793 }
794
795 static int ads7846_resume(struct spi_device *spi)
796 {
797         struct ads7846 *ts = dev_get_drvdata(&spi->dev);
798
799         spin_lock_irq(&ts->lock);
800
801         ts->is_suspended = 0;
802         ads7846_enable(ts);
803
804         spin_unlock_irq(&ts->lock);
805
806         return 0;
807 }
808
809 static int __devinit ads7846_probe(struct spi_device *spi)
810 {
811         struct ads7846                  *ts;
812         struct input_dev                *input_dev;
813         struct ads7846_platform_data    *pdata = spi->dev.platform_data;
814         struct spi_message              *m;
815         struct spi_transfer             *x;
816         int                             vref;
817         int                             err;
818
819         if (!spi->irq) {
820                 dev_dbg(&spi->dev, "no IRQ?\n");
821                 return -ENODEV;
822         }
823
824         if (!pdata) {
825                 dev_dbg(&spi->dev, "no platform data?\n");
826                 return -ENODEV;
827         }
828
829         /* enable voltage */
830         if (pdata->vaux_control != NULL) {
831                 err = pdata->vaux_control(VAUX_ENABLE);
832                 if (err != 0) {
833                         dev_dbg(&spi->dev, "TS vaux enable failed\n");
834                         return err;
835                 }
836         }
837
838         /* don't exceed max specified sample rate */
839         if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
840                 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
841                                 (spi->max_speed_hz/SAMPLE_BITS)/1000);
842                 return -EINVAL;
843         }
844
845         /* REVISIT when the irq can be triggered active-low, or if for some
846          * reason the touchscreen isn't hooked up, we don't need to access
847          * the pendown state.
848          */
849         if (pdata->get_pendown_state == NULL) {
850                 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
851                 return -EINVAL;
852         }
853
854         /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
855          * that even if the hardware can do that, the SPI controller driver
856          * may not.  So we stick to very-portable 8 bit words, both RX and TX.
857          */
858         spi->bits_per_word = 8;
859         spi->mode = SPI_MODE_0;
860         err = spi_setup(spi);
861         if (err < 0)
862                 return err;
863
864         ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
865         input_dev = input_allocate_device();
866         if (!ts || !input_dev) {
867                 err = -ENOMEM;
868                 goto err_free_mem;
869         }
870
871         dev_set_drvdata(&spi->dev, ts);
872
873         ts->spi = spi;
874         ts->input = input_dev;
875         ts->vref_mv = pdata->vref_mv;
876
877         hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
878         ts->timer.function = ads7846_timer;
879
880         spin_lock_init(&ts->lock);
881
882         ts->model = pdata->model ? : 7846;
883         ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
884         ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
885         ts->pressure_max = pdata->pressure_max ? : ~0;
886
887         if (pdata->filter != NULL) {
888                 if (pdata->filter_init != NULL) {
889                         err = pdata->filter_init(pdata, &ts->filter_data);
890                         if (err < 0)
891                                 goto err_free_mem;
892                 }
893                 ts->filter = pdata->filter;
894                 ts->filter_cleanup = pdata->filter_cleanup;
895         } else if (pdata->debounce_max) {
896                 ts->debounce_max = pdata->debounce_max;
897                 if (ts->debounce_max < 2)
898                         ts->debounce_max = 2;
899                 ts->debounce_tol = pdata->debounce_tol;
900                 ts->debounce_rep = pdata->debounce_rep;
901                 ts->filter = ads7846_debounce;
902                 ts->filter_data = ts;
903         } else
904                 ts->filter = ads7846_no_filter;
905         ts->get_pendown_state = pdata->get_pendown_state;
906
907         if (pdata->penirq_recheck_delay_usecs)
908                 ts->penirq_recheck_delay_usecs =
909                                 pdata->penirq_recheck_delay_usecs;
910
911         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
912
913         input_dev->name = "ADS784x Touchscreen";
914         input_dev->phys = ts->phys;
915         input_dev->dev.parent = &spi->dev;
916
917         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
918         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
919         input_set_abs_params(input_dev, ABS_X,
920                         pdata->x_min ? : 0,
921                         pdata->x_max ? : MAX_12BIT,
922                         0, 0);
923         input_set_abs_params(input_dev, ABS_Y,
924                         pdata->y_min ? : 0,
925                         pdata->y_max ? : MAX_12BIT,
926                         0, 0);
927         input_set_abs_params(input_dev, ABS_PRESSURE,
928                         pdata->pressure_min, pdata->pressure_max, 0, 0);
929
930         vref = pdata->keep_vref_on;
931
932         /* set up the transfers to read touchscreen state; this assumes we
933          * use formula #2 for pressure, not #3.
934          */
935         m = &ts->msg[0];
936         x = ts->xfer;
937
938         spi_message_init(m);
939
940         /* y- still on; turn on only y+ (and ADC) */
941         ts->read_y = READ_Y(vref);
942         x->tx_buf = &ts->read_y;
943         x->len = 1;
944         spi_message_add_tail(x, m);
945
946         x++;
947         x->rx_buf = &ts->tc.y;
948         x->len = 2;
949         spi_message_add_tail(x, m);
950
951         /* the first sample after switching drivers can be low quality;
952          * optionally discard it, using a second one after the signals
953          * have had enough time to stabilize.
954          */
955         if (pdata->settle_delay_usecs) {
956                 x->delay_usecs = pdata->settle_delay_usecs;
957
958                 x++;
959                 x->tx_buf = &ts->read_y;
960                 x->len = 1;
961                 spi_message_add_tail(x, m);
962
963                 x++;
964                 x->rx_buf = &ts->tc.y;
965                 x->len = 2;
966                 spi_message_add_tail(x, m);
967         }
968
969         m->complete = ads7846_rx_val;
970         m->context = ts;
971
972         m++;
973         spi_message_init(m);
974
975         /* turn y- off, x+ on, then leave in lowpower */
976         x++;
977         ts->read_x = READ_X(vref);
978         x->tx_buf = &ts->read_x;
979         x->len = 1;
980         spi_message_add_tail(x, m);
981
982         x++;
983         x->rx_buf = &ts->tc.x;
984         x->len = 2;
985         spi_message_add_tail(x, m);
986
987         /* ... maybe discard first sample ... */
988         if (pdata->settle_delay_usecs) {
989                 x->delay_usecs = pdata->settle_delay_usecs;
990
991                 x++;
992                 x->tx_buf = &ts->read_x;
993                 x->len = 1;
994                 spi_message_add_tail(x, m);
995
996                 x++;
997                 x->rx_buf = &ts->tc.x;
998                 x->len = 2;
999                 spi_message_add_tail(x, m);
1000         }
1001
1002         m->complete = ads7846_rx_val;
1003         m->context = ts;
1004
1005         /* turn y+ off, x- on; we'll use formula #2 */
1006         if (ts->model == 7846) {
1007                 m++;
1008                 spi_message_init(m);
1009
1010                 x++;
1011                 ts->read_z1 = READ_Z1(vref);
1012                 x->tx_buf = &ts->read_z1;
1013                 x->len = 1;
1014                 spi_message_add_tail(x, m);
1015
1016                 x++;
1017                 x->rx_buf = &ts->tc.z1;
1018                 x->len = 2;
1019                 spi_message_add_tail(x, m);
1020
1021                 /* ... maybe discard first sample ... */
1022                 if (pdata->settle_delay_usecs) {
1023                         x->delay_usecs = pdata->settle_delay_usecs;
1024
1025                         x++;
1026                         x->tx_buf = &ts->read_z1;
1027                         x->len = 1;
1028                         spi_message_add_tail(x, m);
1029
1030                         x++;
1031                         x->rx_buf = &ts->tc.z1;
1032                         x->len = 2;
1033                         spi_message_add_tail(x, m);
1034                 }
1035
1036                 m->complete = ads7846_rx_val;
1037                 m->context = ts;
1038
1039                 m++;
1040                 spi_message_init(m);
1041
1042                 x++;
1043                 ts->read_z2 = READ_Z2(vref);
1044                 x->tx_buf = &ts->read_z2;
1045                 x->len = 1;
1046                 spi_message_add_tail(x, m);
1047
1048                 x++;
1049                 x->rx_buf = &ts->tc.z2;
1050                 x->len = 2;
1051                 spi_message_add_tail(x, m);
1052
1053                 /* ... maybe discard first sample ... */
1054                 if (pdata->settle_delay_usecs) {
1055                         x->delay_usecs = pdata->settle_delay_usecs;
1056
1057                         x++;
1058                         x->tx_buf = &ts->read_z2;
1059                         x->len = 1;
1060                         spi_message_add_tail(x, m);
1061
1062                         x++;
1063                         x->rx_buf = &ts->tc.z2;
1064                         x->len = 2;
1065                         spi_message_add_tail(x, m);
1066                 }
1067
1068                 m->complete = ads7846_rx_val;
1069                 m->context = ts;
1070         }
1071
1072         /* power down */
1073         m++;
1074         spi_message_init(m);
1075
1076         x++;
1077         ts->pwrdown = PWRDOWN;
1078         x->tx_buf = &ts->pwrdown;
1079         x->len = 1;
1080         spi_message_add_tail(x, m);
1081
1082         x++;
1083         x->rx_buf = &ts->dummy;
1084         x->len = 2;
1085         CS_CHANGE(*x);
1086         spi_message_add_tail(x, m);
1087
1088         m->complete = ads7846_rx;
1089         m->context = ts;
1090
1091         ts->last_msg = m;
1092
1093         if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
1094                         spi->dev.driver->name, ts)) {
1095                 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1096                 err = -EBUSY;
1097                 goto err_cleanup_filter;
1098         }
1099
1100         err = ads784x_hwmon_register(spi, ts);
1101         if (err)
1102                 goto err_free_irq;
1103
1104         dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1105
1106         /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1107          * the touchscreen, in case it's not connected.
1108          */
1109         (void) ads7846_read12_ser(&spi->dev,
1110                           READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1111
1112         err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1113         if (err)
1114                 goto err_remove_hwmon;
1115
1116         err = input_register_device(input_dev);
1117         if (err)
1118                 goto err_remove_attr_group;
1119
1120         return 0;
1121
1122  err_remove_attr_group:
1123         sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1124  err_remove_hwmon:
1125         ads784x_hwmon_unregister(spi, ts);
1126  err_free_irq:
1127         free_irq(spi->irq, ts);
1128  err_cleanup_filter:
1129         if (ts->filter_cleanup)
1130                 ts->filter_cleanup(ts->filter_data);
1131  err_free_mem:
1132         input_free_device(input_dev);
1133         kfree(ts);
1134         return err;
1135 }
1136
1137 static int __devexit ads7846_remove(struct spi_device *spi)
1138 {
1139         struct ads7846          *ts = dev_get_drvdata(&spi->dev);
1140
1141         ads784x_hwmon_unregister(spi, ts);
1142         input_unregister_device(ts->input);
1143
1144         ads7846_suspend(spi, PMSG_SUSPEND);
1145
1146         sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1147
1148         free_irq(ts->spi->irq, ts);
1149         /* suspend left the IRQ disabled */
1150         enable_irq(ts->spi->irq);
1151
1152         if (ts->filter_cleanup)
1153                 ts->filter_cleanup(ts->filter_data);
1154
1155         kfree(ts);
1156
1157         dev_dbg(&spi->dev, "unregistered touchscreen\n");
1158         return 0;
1159 }
1160
1161 static struct spi_driver ads7846_driver = {
1162         .driver = {
1163                 .name   = "ads7846",
1164                 .bus    = &spi_bus_type,
1165                 .owner  = THIS_MODULE,
1166         },
1167         .probe          = ads7846_probe,
1168         .remove         = __devexit_p(ads7846_remove),
1169         .suspend        = ads7846_suspend,
1170         .resume         = ads7846_resume,
1171 };
1172
1173 static int __init ads7846_init(void)
1174 {
1175         return spi_register_driver(&ads7846_driver);
1176 }
1177 module_init(ads7846_init);
1178
1179 static void __exit ads7846_exit(void)
1180 {
1181         spi_unregister_driver(&ads7846_driver);
1182 }
1183 module_exit(ads7846_exit);
1184
1185 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1186 MODULE_LICENSE("GPL");