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