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