Input: convert drivers to use strict_strtoul()
[safe/jmp/linux-2.6] / 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         long i;
465
466         if (strict_strtoul(buf, 10, &i))
467                 return -EINVAL;
468
469         spin_lock_irq(&ts->lock);
470
471         if (i)
472                 ads7846_disable(ts);
473         else
474                 ads7846_enable(ts);
475
476         spin_unlock_irq(&ts->lock);
477
478         return count;
479 }
480
481 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
482
483 static struct attribute *ads784x_attributes[] = {
484         &dev_attr_pen_down.attr,
485         &dev_attr_disable.attr,
486         NULL,
487 };
488
489 static struct attribute_group ads784x_attr_group = {
490         .attrs = ads784x_attributes,
491 };
492
493 /*--------------------------------------------------------------------------*/
494
495 /*
496  * PENIRQ only kicks the timer.  The timer only reissues the SPI transfer,
497  * to retrieve touchscreen status.
498  *
499  * The SPI transfer completion callback does the real work.  It reports
500  * touchscreen events and reactivates the timer (or IRQ) as appropriate.
501  */
502
503 static void ads7846_rx(void *ads)
504 {
505         struct ads7846          *ts = ads;
506         unsigned                Rt;
507         u16                     x, y, z1, z2;
508
509         /* ads7846_rx_val() did in-place conversion (including byteswap) from
510          * on-the-wire format as part of debouncing to get stable readings.
511          */
512         x = ts->tc.x;
513         y = ts->tc.y;
514         z1 = ts->tc.z1;
515         z2 = ts->tc.z2;
516
517         /* range filtering */
518         if (x == MAX_12BIT)
519                 x = 0;
520
521         if (ts->model == 7843) {
522                 Rt = ts->pressure_max / 2;
523         } else if (likely(x && z1)) {
524                 /* compute touch pressure resistance using equation #2 */
525                 Rt = z2;
526                 Rt -= z1;
527                 Rt *= x;
528                 Rt *= ts->x_plate_ohms;
529                 Rt /= z1;
530                 Rt = (Rt + 2047) >> 12;
531         } else {
532                 Rt = 0;
533         }
534
535         /* Sample found inconsistent by debouncing or pressure is beyond
536          * the maximum. Don't report it to user space, repeat at least
537          * once more the measurement
538          */
539         if (ts->tc.ignore || Rt > ts->pressure_max) {
540 #ifdef VERBOSE
541                 pr_debug("%s: ignored %d pressure %d\n",
542                         ts->spi->dev.bus_id, ts->tc.ignore, Rt);
543 #endif
544                 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
545                               HRTIMER_MODE_REL);
546                 return;
547         }
548
549         /* Maybe check the pendown state before reporting. This discards
550          * false readings when the pen is lifted.
551          */
552         if (ts->penirq_recheck_delay_usecs) {
553                 udelay(ts->penirq_recheck_delay_usecs);
554                 if (!ts->get_pendown_state())
555                         Rt = 0;
556         }
557
558         /* NOTE: We can't rely on the pressure to determine the pen down
559          * state, even this controller has a pressure sensor.  The pressure
560          * value can fluctuate for quite a while after lifting the pen and
561          * in some cases may not even settle at the expected value.
562          *
563          * The only safe way to check for the pen up condition is in the
564          * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
565          */
566         if (Rt) {
567                 struct input_dev *input = ts->input;
568
569                 if (!ts->pendown) {
570                         input_report_key(input, BTN_TOUCH, 1);
571                         ts->pendown = 1;
572 #ifdef VERBOSE
573                         dev_dbg(&ts->spi->dev, "DOWN\n");
574 #endif
575                 }
576                 input_report_abs(input, ABS_X, x);
577                 input_report_abs(input, ABS_Y, y);
578                 input_report_abs(input, ABS_PRESSURE, Rt);
579
580                 input_sync(input);
581 #ifdef VERBOSE
582                 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
583 #endif
584         }
585
586         hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
587                         HRTIMER_MODE_REL);
588 }
589
590 static int ads7846_debounce(void *ads, int data_idx, int *val)
591 {
592         struct ads7846          *ts = ads;
593
594         if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
595                 /* Start over collecting consistent readings. */
596                 ts->read_rep = 0;
597                 /* Repeat it, if this was the first read or the read
598                  * wasn't consistent enough. */
599                 if (ts->read_cnt < ts->debounce_max) {
600                         ts->last_read = *val;
601                         ts->read_cnt++;
602                         return ADS7846_FILTER_REPEAT;
603                 } else {
604                         /* Maximum number of debouncing reached and still
605                          * not enough number of consistent readings. Abort
606                          * the whole sample, repeat it in the next sampling
607                          * period.
608                          */
609                         ts->read_cnt = 0;
610                         return ADS7846_FILTER_IGNORE;
611                 }
612         } else {
613                 if (++ts->read_rep > ts->debounce_rep) {
614                         /* Got a good reading for this coordinate,
615                          * go for the next one. */
616                         ts->read_cnt = 0;
617                         ts->read_rep = 0;
618                         return ADS7846_FILTER_OK;
619                 } else {
620                         /* Read more values that are consistent. */
621                         ts->read_cnt++;
622                         return ADS7846_FILTER_REPEAT;
623                 }
624         }
625 }
626
627 static int ads7846_no_filter(void *ads, int data_idx, int *val)
628 {
629         return ADS7846_FILTER_OK;
630 }
631
632 static void ads7846_rx_val(void *ads)
633 {
634         struct ads7846 *ts = ads;
635         struct spi_message *m;
636         struct spi_transfer *t;
637         int val;
638         int action;
639         int status;
640
641         m = &ts->msg[ts->msg_idx];
642         t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
643
644         /* adjust:  on-wire is a must-ignore bit, a BE12 value, then padding;
645          * built from two 8 bit values written msb-first.
646          */
647         val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
648
649         action = ts->filter(ts->filter_data, ts->msg_idx, &val);
650         switch (action) {
651         case ADS7846_FILTER_REPEAT:
652                 break;
653         case ADS7846_FILTER_IGNORE:
654                 ts->tc.ignore = 1;
655                 /* Last message will contain ads7846_rx() as the
656                  * completion function.
657                  */
658                 m = ts->last_msg;
659                 break;
660         case ADS7846_FILTER_OK:
661                 *(u16 *)t->rx_buf = val;
662                 ts->tc.ignore = 0;
663                 m = &ts->msg[++ts->msg_idx];
664                 break;
665         default:
666                 BUG();
667         }
668         status = spi_async(ts->spi, m);
669         if (status)
670                 dev_err(&ts->spi->dev, "spi_async --> %d\n",
671                                 status);
672 }
673
674 static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
675 {
676         struct ads7846  *ts = container_of(handle, struct ads7846, timer);
677         int             status = 0;
678
679         spin_lock_irq(&ts->lock);
680
681         if (unlikely(!ts->get_pendown_state() ||
682                      device_suspended(&ts->spi->dev))) {
683                 if (ts->pendown) {
684                         struct input_dev *input = ts->input;
685
686                         input_report_key(input, BTN_TOUCH, 0);
687                         input_report_abs(input, ABS_PRESSURE, 0);
688                         input_sync(input);
689
690                         ts->pendown = 0;
691 #ifdef VERBOSE
692                         dev_dbg(&ts->spi->dev, "UP\n");
693 #endif
694                 }
695
696                 /* measurement cycle ended */
697                 if (!device_suspended(&ts->spi->dev)) {
698                         ts->irq_disabled = 0;
699                         enable_irq(ts->spi->irq);
700                 }
701                 ts->pending = 0;
702         } else {
703                 /* pen is still down, continue with the measurement */
704                 ts->msg_idx = 0;
705                 status = spi_async(ts->spi, &ts->msg[0]);
706                 if (status)
707                         dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
708         }
709
710         spin_unlock_irq(&ts->lock);
711         return HRTIMER_NORESTART;
712 }
713
714 static irqreturn_t ads7846_irq(int irq, void *handle)
715 {
716         struct ads7846 *ts = handle;
717         unsigned long flags;
718
719         spin_lock_irqsave(&ts->lock, flags);
720         if (likely(ts->get_pendown_state())) {
721                 if (!ts->irq_disabled) {
722                         /* The ARM do_simple_IRQ() dispatcher doesn't act
723                          * like the other dispatchers:  it will report IRQs
724                          * even after they've been disabled.  We work around
725                          * that here.  (The "generic irq" framework may help...)
726                          */
727                         ts->irq_disabled = 1;
728                         disable_irq(ts->spi->irq);
729                         ts->pending = 1;
730                         hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
731                                         HRTIMER_MODE_REL);
732                 }
733         }
734         spin_unlock_irqrestore(&ts->lock, flags);
735
736         return IRQ_HANDLED;
737 }
738
739 /*--------------------------------------------------------------------------*/
740
741 /* Must be called with ts->lock held */
742 static void ads7846_disable(struct ads7846 *ts)
743 {
744         if (ts->disabled)
745                 return;
746
747         ts->disabled = 1;
748
749         /* are we waiting for IRQ, or polling? */
750         if (!ts->pending) {
751                 ts->irq_disabled = 1;
752                 disable_irq(ts->spi->irq);
753         } else {
754                 /* the timer will run at least once more, and
755                  * leave everything in a clean state, IRQ disabled
756                  */
757                 while (ts->pending) {
758                         spin_unlock_irq(&ts->lock);
759                         msleep(1);
760                         spin_lock_irq(&ts->lock);
761                 }
762         }
763
764         /* we know the chip's in lowpower mode since we always
765          * leave it that way after every request
766          */
767
768 }
769
770 /* Must be called with ts->lock held */
771 static void ads7846_enable(struct ads7846 *ts)
772 {
773         if (!ts->disabled)
774                 return;
775
776         ts->disabled = 0;
777         ts->irq_disabled = 0;
778         enable_irq(ts->spi->irq);
779 }
780
781 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
782 {
783         struct ads7846 *ts = dev_get_drvdata(&spi->dev);
784
785         spin_lock_irq(&ts->lock);
786
787         ts->is_suspended = 1;
788         ads7846_disable(ts);
789
790         spin_unlock_irq(&ts->lock);
791
792         return 0;
793
794 }
795
796 static int ads7846_resume(struct spi_device *spi)
797 {
798         struct ads7846 *ts = dev_get_drvdata(&spi->dev);
799
800         spin_lock_irq(&ts->lock);
801
802         ts->is_suspended = 0;
803         ads7846_enable(ts);
804
805         spin_unlock_irq(&ts->lock);
806
807         return 0;
808 }
809
810 static int __devinit ads7846_probe(struct spi_device *spi)
811 {
812         struct ads7846                  *ts;
813         struct input_dev                *input_dev;
814         struct ads7846_platform_data    *pdata = spi->dev.platform_data;
815         struct spi_message              *m;
816         struct spi_transfer             *x;
817         int                             vref;
818         int                             err;
819
820         if (!spi->irq) {
821                 dev_dbg(&spi->dev, "no IRQ?\n");
822                 return -ENODEV;
823         }
824
825         if (!pdata) {
826                 dev_dbg(&spi->dev, "no platform data?\n");
827                 return -ENODEV;
828         }
829
830         /* don't exceed max specified sample rate */
831         if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
832                 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
833                                 (spi->max_speed_hz/SAMPLE_BITS)/1000);
834                 return -EINVAL;
835         }
836
837         /* REVISIT when the irq can be triggered active-low, or if for some
838          * reason the touchscreen isn't hooked up, we don't need to access
839          * the pendown state.
840          */
841         if (pdata->get_pendown_state == NULL) {
842                 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
843                 return -EINVAL;
844         }
845
846         /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
847          * that even if the hardware can do that, the SPI controller driver
848          * may not.  So we stick to very-portable 8 bit words, both RX and TX.
849          */
850         spi->bits_per_word = 8;
851         spi->mode = SPI_MODE_0;
852         err = spi_setup(spi);
853         if (err < 0)
854                 return err;
855
856         ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
857         input_dev = input_allocate_device();
858         if (!ts || !input_dev) {
859                 err = -ENOMEM;
860                 goto err_free_mem;
861         }
862
863         dev_set_drvdata(&spi->dev, ts);
864
865         ts->spi = spi;
866         ts->input = input_dev;
867         ts->vref_mv = pdata->vref_mv;
868
869         hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
870         ts->timer.function = ads7846_timer;
871
872         spin_lock_init(&ts->lock);
873
874         ts->model = pdata->model ? : 7846;
875         ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
876         ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
877         ts->pressure_max = pdata->pressure_max ? : ~0;
878
879         if (pdata->filter != NULL) {
880                 if (pdata->filter_init != NULL) {
881                         err = pdata->filter_init(pdata, &ts->filter_data);
882                         if (err < 0)
883                                 goto err_free_mem;
884                 }
885                 ts->filter = pdata->filter;
886                 ts->filter_cleanup = pdata->filter_cleanup;
887         } else if (pdata->debounce_max) {
888                 ts->debounce_max = pdata->debounce_max;
889                 if (ts->debounce_max < 2)
890                         ts->debounce_max = 2;
891                 ts->debounce_tol = pdata->debounce_tol;
892                 ts->debounce_rep = pdata->debounce_rep;
893                 ts->filter = ads7846_debounce;
894                 ts->filter_data = ts;
895         } else
896                 ts->filter = ads7846_no_filter;
897         ts->get_pendown_state = pdata->get_pendown_state;
898
899         if (pdata->penirq_recheck_delay_usecs)
900                 ts->penirq_recheck_delay_usecs =
901                                 pdata->penirq_recheck_delay_usecs;
902
903         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
904
905         input_dev->name = "ADS784x Touchscreen";
906         input_dev->phys = ts->phys;
907         input_dev->dev.parent = &spi->dev;
908
909         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
910         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
911         input_set_abs_params(input_dev, ABS_X,
912                         pdata->x_min ? : 0,
913                         pdata->x_max ? : MAX_12BIT,
914                         0, 0);
915         input_set_abs_params(input_dev, ABS_Y,
916                         pdata->y_min ? : 0,
917                         pdata->y_max ? : MAX_12BIT,
918                         0, 0);
919         input_set_abs_params(input_dev, ABS_PRESSURE,
920                         pdata->pressure_min, pdata->pressure_max, 0, 0);
921
922         vref = pdata->keep_vref_on;
923
924         /* set up the transfers to read touchscreen state; this assumes we
925          * use formula #2 for pressure, not #3.
926          */
927         m = &ts->msg[0];
928         x = ts->xfer;
929
930         spi_message_init(m);
931
932         /* y- still on; turn on only y+ (and ADC) */
933         ts->read_y = READ_Y(vref);
934         x->tx_buf = &ts->read_y;
935         x->len = 1;
936         spi_message_add_tail(x, m);
937
938         x++;
939         x->rx_buf = &ts->tc.y;
940         x->len = 2;
941         spi_message_add_tail(x, m);
942
943         /* the first sample after switching drivers can be low quality;
944          * optionally discard it, using a second one after the signals
945          * have had enough time to stabilize.
946          */
947         if (pdata->settle_delay_usecs) {
948                 x->delay_usecs = pdata->settle_delay_usecs;
949
950                 x++;
951                 x->tx_buf = &ts->read_y;
952                 x->len = 1;
953                 spi_message_add_tail(x, m);
954
955                 x++;
956                 x->rx_buf = &ts->tc.y;
957                 x->len = 2;
958                 spi_message_add_tail(x, m);
959         }
960
961         m->complete = ads7846_rx_val;
962         m->context = ts;
963
964         m++;
965         spi_message_init(m);
966
967         /* turn y- off, x+ on, then leave in lowpower */
968         x++;
969         ts->read_x = READ_X(vref);
970         x->tx_buf = &ts->read_x;
971         x->len = 1;
972         spi_message_add_tail(x, m);
973
974         x++;
975         x->rx_buf = &ts->tc.x;
976         x->len = 2;
977         spi_message_add_tail(x, m);
978
979         /* ... maybe discard first sample ... */
980         if (pdata->settle_delay_usecs) {
981                 x->delay_usecs = pdata->settle_delay_usecs;
982
983                 x++;
984                 x->tx_buf = &ts->read_x;
985                 x->len = 1;
986                 spi_message_add_tail(x, m);
987
988                 x++;
989                 x->rx_buf = &ts->tc.x;
990                 x->len = 2;
991                 spi_message_add_tail(x, m);
992         }
993
994         m->complete = ads7846_rx_val;
995         m->context = ts;
996
997         /* turn y+ off, x- on; we'll use formula #2 */
998         if (ts->model == 7846) {
999                 m++;
1000                 spi_message_init(m);
1001
1002                 x++;
1003                 ts->read_z1 = READ_Z1(vref);
1004                 x->tx_buf = &ts->read_z1;
1005                 x->len = 1;
1006                 spi_message_add_tail(x, m);
1007
1008                 x++;
1009                 x->rx_buf = &ts->tc.z1;
1010                 x->len = 2;
1011                 spi_message_add_tail(x, m);
1012
1013                 /* ... maybe discard first sample ... */
1014                 if (pdata->settle_delay_usecs) {
1015                         x->delay_usecs = pdata->settle_delay_usecs;
1016
1017                         x++;
1018                         x->tx_buf = &ts->read_z1;
1019                         x->len = 1;
1020                         spi_message_add_tail(x, m);
1021
1022                         x++;
1023                         x->rx_buf = &ts->tc.z1;
1024                         x->len = 2;
1025                         spi_message_add_tail(x, m);
1026                 }
1027
1028                 m->complete = ads7846_rx_val;
1029                 m->context = ts;
1030
1031                 m++;
1032                 spi_message_init(m);
1033
1034                 x++;
1035                 ts->read_z2 = READ_Z2(vref);
1036                 x->tx_buf = &ts->read_z2;
1037                 x->len = 1;
1038                 spi_message_add_tail(x, m);
1039
1040                 x++;
1041                 x->rx_buf = &ts->tc.z2;
1042                 x->len = 2;
1043                 spi_message_add_tail(x, m);
1044
1045                 /* ... maybe discard first sample ... */
1046                 if (pdata->settle_delay_usecs) {
1047                         x->delay_usecs = pdata->settle_delay_usecs;
1048
1049                         x++;
1050                         x->tx_buf = &ts->read_z2;
1051                         x->len = 1;
1052                         spi_message_add_tail(x, m);
1053
1054                         x++;
1055                         x->rx_buf = &ts->tc.z2;
1056                         x->len = 2;
1057                         spi_message_add_tail(x, m);
1058                 }
1059
1060                 m->complete = ads7846_rx_val;
1061                 m->context = ts;
1062         }
1063
1064         /* power down */
1065         m++;
1066         spi_message_init(m);
1067
1068         x++;
1069         ts->pwrdown = PWRDOWN;
1070         x->tx_buf = &ts->pwrdown;
1071         x->len = 1;
1072         spi_message_add_tail(x, m);
1073
1074         x++;
1075         x->rx_buf = &ts->dummy;
1076         x->len = 2;
1077         CS_CHANGE(*x);
1078         spi_message_add_tail(x, m);
1079
1080         m->complete = ads7846_rx;
1081         m->context = ts;
1082
1083         ts->last_msg = m;
1084
1085         if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
1086                         spi->dev.driver->name, ts)) {
1087                 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1088                 err = -EBUSY;
1089                 goto err_cleanup_filter;
1090         }
1091
1092         err = ads784x_hwmon_register(spi, ts);
1093         if (err)
1094                 goto err_free_irq;
1095
1096         dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1097
1098         /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1099          * the touchscreen, in case it's not connected.
1100          */
1101         (void) ads7846_read12_ser(&spi->dev,
1102                           READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1103
1104         err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1105         if (err)
1106                 goto err_remove_hwmon;
1107
1108         err = input_register_device(input_dev);
1109         if (err)
1110                 goto err_remove_attr_group;
1111
1112         return 0;
1113
1114  err_remove_attr_group:
1115         sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1116  err_remove_hwmon:
1117         ads784x_hwmon_unregister(spi, ts);
1118  err_free_irq:
1119         free_irq(spi->irq, ts);
1120  err_cleanup_filter:
1121         if (ts->filter_cleanup)
1122                 ts->filter_cleanup(ts->filter_data);
1123  err_free_mem:
1124         input_free_device(input_dev);
1125         kfree(ts);
1126         return err;
1127 }
1128
1129 static int __devexit ads7846_remove(struct spi_device *spi)
1130 {
1131         struct ads7846          *ts = dev_get_drvdata(&spi->dev);
1132
1133         ads784x_hwmon_unregister(spi, ts);
1134         input_unregister_device(ts->input);
1135
1136         ads7846_suspend(spi, PMSG_SUSPEND);
1137
1138         sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1139
1140         free_irq(ts->spi->irq, ts);
1141         /* suspend left the IRQ disabled */
1142         enable_irq(ts->spi->irq);
1143
1144         if (ts->filter_cleanup)
1145                 ts->filter_cleanup(ts->filter_data);
1146
1147         kfree(ts);
1148
1149         dev_dbg(&spi->dev, "unregistered touchscreen\n");
1150         return 0;
1151 }
1152
1153 static struct spi_driver ads7846_driver = {
1154         .driver = {
1155                 .name   = "ads7846",
1156                 .bus    = &spi_bus_type,
1157                 .owner  = THIS_MODULE,
1158         },
1159         .probe          = ads7846_probe,
1160         .remove         = __devexit_p(ads7846_remove),
1161         .suspend        = ads7846_suspend,
1162         .resume         = ads7846_resume,
1163 };
1164
1165 static int __init ads7846_init(void)
1166 {
1167         return spi_register_driver(&ads7846_driver);
1168 }
1169 module_init(ads7846_init);
1170
1171 static void __exit ads7846_exit(void)
1172 {
1173         spi_unregister_driver(&ads7846_driver);
1174 }
1175 module_exit(ads7846_exit);
1176
1177 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1178 MODULE_LICENSE("GPL");