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