]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/touchscreen/ads7846.c
ads7846: filtering based on pressure value
[linux-2.6-omap-h63xx.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 Imre Deak <imre.deak@nokia.com>
6  *
7  * Using code from:
8  *  - corgi_ts.c
9  *      Copyright (C) 2004-2005 Richard Purdie
10  *  - omap_ts.[hc], ads7846.h, ts_osk.c
11  *      Copyright (C) 2002 MontaVista Software
12  *      Copyright (C) 2004 Texas Instruments
13  *      Copyright (C) 2005 Dirk Behme
14  *
15  *  This program is free software; you can redistribute it and/or modify
16  *  it under the terms of the GNU General Public License version 2 as
17  *  published by the Free Software Foundation.
18  */
19 #include <linux/device.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/input.h>
23 #include <linux/interrupt.h>
24 #include <linux/slab.h>
25 #include <linux/spi/spi.h>
26 #include <linux/spi/ads7846.h>
27
28 #ifdef  CONFIG_ARM
29 #include <asm/mach-types.h>
30 #ifdef  CONFIG_ARCH_OMAP
31 #include <asm/arch/gpio.h>
32 #endif
33 #endif
34
35
36 /*
37  * This code has been heavily tested on a Nokia 770, and lightly
38  * tested on an other ads7846 device.
39  * Support for ads7843 and ads7845 has only been stubbed in.
40  *
41  * Not yet done:  How accurate are the temperature and voltage
42  * readings? (System-specific calibration should support
43  * accuracy of 0.3 degrees C; otherwise it's 2.0 degrees.)
44  *
45  * IRQ handling needs a workaround because of a shortcoming in handling
46  * edge triggered IRQs on some platforms like the OMAP1/2. These
47  * platforms don't handle the ARM lazy IRQ disabling properly, thus we
48  * have to maintain our own SW IRQ disabled status. This should be
49  * removed as soon as the affected platforms' IRQ handling is fixed.
50  *
51  * app note sbaa036 talks in more detail about accurate sampling...
52  * that ought to help in situations like LCDs inducing noise (which
53  * can also be helped by using synch signals) and more generally.
54  * This driver tries to utilize the measures described in the app
55  * note. The strength of filtering can be set in the board-* specific
56  * files.
57  */
58
59 #define TS_POLL_PERIOD  msecs_to_jiffies(10)
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 then range adjustment.
69          */
70         __be16 x;
71         __be16 y;
72         __be16 z1, z2;
73 };
74
75 struct ads7846 {
76         struct input_dev        *input;
77         char                    phys[32];
78
79         struct spi_device       *spi;
80         u16                     model;
81         u16                     vref_delay_usecs;
82         u16                     x_plate_ohms;
83         u16                     pressure_max;
84
85         u8                      read_x, read_y, read_z1, read_z2, pwrdown;
86         u16                     dummy;          /* for the pwrdown read */
87         struct ts_event         tc;
88         u16                     last_x;
89         u16                     last_y;
90         u16                     last_pressure;
91
92         struct spi_transfer     xfer[10];
93         struct spi_message      msg[5];
94         int                     msg_idx;
95         int                     read_cnt;
96         int                     last_read;
97
98         u16                     debounce_max;
99         u16                     debounce_tol;
100
101         spinlock_t              lock;
102         struct timer_list       timer;          /* P: lock */
103         unsigned                pendown:1;      /* P: lock */
104         unsigned                pending:1;      /* P: lock */
105 // FIXME remove "irq_disabled"
106         unsigned                irq_disabled:1; /* P: lock */
107         unsigned                disabled:1;
108 };
109
110 /* leave chip selected when we're done, for quicker re-select? */
111 #if     0
112 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
113 #else
114 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
115 #endif
116
117 /*--------------------------------------------------------------------------*/
118
119 /* The ADS7846 has touchscreen and other sensors.
120  * Earlier ads784x chips are somewhat compatible.
121  */
122 #define ADS_START               (1 << 7)
123 #define ADS_A2A1A0_d_y          (1 << 4)        /* differential */
124 #define ADS_A2A1A0_d_z1         (3 << 4)        /* differential */
125 #define ADS_A2A1A0_d_z2         (4 << 4)        /* differential */
126 #define ADS_A2A1A0_d_x          (5 << 4)        /* differential */
127 #define ADS_A2A1A0_temp0        (0 << 4)        /* non-differential */
128 #define ADS_A2A1A0_vbatt        (2 << 4)        /* non-differential */
129 #define ADS_A2A1A0_vaux         (6 << 4)        /* non-differential */
130 #define ADS_A2A1A0_temp1        (7 << 4)        /* non-differential */
131 #define ADS_8_BIT               (1 << 3)
132 #define ADS_12_BIT              (0 << 3)
133 #define ADS_SER                 (1 << 2)        /* non-differential */
134 #define ADS_DFR                 (0 << 2)        /* differential */
135 #define ADS_PD10_PDOWN          (0 << 0)        /* lowpower mode + penirq */
136 #define ADS_PD10_ADC_ON         (1 << 0)        /* ADC on */
137 #define ADS_PD10_REF_ON         (2 << 0)        /* vREF on + penirq */
138 #define ADS_PD10_ALL_ON         (3 << 0)        /* ADC + vREF on */
139
140 #define MAX_12BIT       ((1<<12)-1)
141
142 /* leave ADC powered up (disables penirq) between differential samples */
143 #define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
144         | ADS_12_BIT | ADS_DFR)
145
146 #define READ_Y  (READ_12BIT_DFR(y)  | ADS_PD10_ADC_ON)
147 #define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
148 #define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
149
150 #define READ_X  (READ_12BIT_DFR(x)  | ADS_PD10_ADC_ON)
151 #define PWRDOWN (READ_12BIT_DFR(y)  | ADS_PD10_PDOWN)   /* LAST */
152
153 /* single-ended samples need to first power up reference voltage;
154  * we leave both ADC and VREF powered
155  */
156 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
157         | ADS_12_BIT | ADS_SER)
158
159 #define REF_ON  (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
160 #define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
161
162 /*--------------------------------------------------------------------------*/
163
164 /*
165  * Non-touchscreen sensors only use single-ended conversions.
166  */
167
168 struct ser_req {
169         u8                      ref_on;
170         u8                      command;
171         u8                      ref_off;
172         u16                     scratch;
173         __be16                  sample;
174         struct spi_message      msg;
175         struct spi_transfer     xfer[6];
176 };
177
178 static void ads7846_enable(struct ads7846 *ts);
179 static void ads7846_disable(struct ads7846 *ts);
180
181 static int ads7846_read12_ser(struct device *dev, unsigned command)
182 {
183         struct spi_device       *spi = to_spi_device(dev);
184         struct ads7846          *ts = dev_get_drvdata(dev);
185         struct ser_req          *req = kzalloc(sizeof *req, SLAB_KERNEL);
186         int                     status;
187         int                     sample;
188         int                     i;
189
190         if (!req)
191                 return -ENOMEM;
192
193         spi_message_init(&req->msg);
194
195         /* activate reference, so it has time to settle; */
196         req->ref_on = REF_ON;
197         req->xfer[0].tx_buf = &req->ref_on;
198         req->xfer[0].len = 1;
199         req->xfer[1].rx_buf = &req->scratch;
200         req->xfer[1].len = 2;
201
202         /*
203          * for external VREF, 0 usec (and assume it's always on);
204          * for 1uF, use 800 usec;
205          * no cap, 100 usec.
206          */
207         req->xfer[1].delay_usecs = ts->vref_delay_usecs;
208
209         /* take sample */
210         req->command = (u8) command;
211         req->xfer[2].tx_buf = &req->command;
212         req->xfer[2].len = 1;
213         req->xfer[3].rx_buf = &req->sample;
214         req->xfer[3].len = 2;
215
216         /* REVISIT:  take a few more samples, and compare ... */
217
218         /* turn off reference */
219         req->ref_off = REF_OFF;
220         req->xfer[4].tx_buf = &req->ref_off;
221         req->xfer[4].len = 1;
222         req->xfer[5].rx_buf = &req->scratch;
223         req->xfer[5].len = 2;
224
225         CS_CHANGE(req->xfer[5]);
226
227         /* group all the transfers together, so we can't interfere with
228          * reading touchscreen state; disable penirq while sampling
229          */
230         for (i = 0; i < 6; i++)
231                 spi_message_add_tail(&req->xfer[i], &req->msg);
232
233         disable_irq(spi->irq);
234         status = spi_sync(spi, &req->msg);
235         enable_irq(spi->irq);
236
237         if (req->msg.status)
238                 status = req->msg.status;
239         sample = be16_to_cpu(req->sample);
240         sample = sample >> 4;
241         kfree(req);
242
243         return status ? status : sample;
244 }
245
246 #define SHOW(name) static ssize_t \
247 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
248 { \
249         ssize_t v = ads7846_read12_ser(dev, \
250                         READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
251         if (v < 0) \
252                 return v; \
253         return sprintf(buf, "%u\n", (unsigned) v); \
254 } \
255 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
256
257 SHOW(temp0)
258 SHOW(temp1)
259 SHOW(vaux)
260 SHOW(vbatt)
261
262 static int is_pen_down(struct device *dev)
263 {
264         struct ads7846          *ts = dev_get_drvdata(dev);
265
266         return ts->pendown;
267 }
268
269 static ssize_t ads7846_pen_down_show(struct device *dev,
270                                      struct device_attribute *attr, char *buf)
271 {
272         return sprintf(buf, "%u\n", is_pen_down(dev));
273 }
274
275 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
276
277 static ssize_t ads7846_disable_show(struct device *dev,
278                                      struct device_attribute *attr, char *buf)
279 {
280         struct ads7846  *ts = dev_get_drvdata(dev);
281
282         return sprintf(buf, "%u\n", ts->disabled);
283 }
284
285 static ssize_t ads7846_disable_store(struct device *dev,
286                                      struct device_attribute *attr,
287                                      const char *buf, size_t count)
288 {
289         struct ads7846 *ts = dev_get_drvdata(dev);
290         unsigned long flags;
291         char *endp;
292         int i;
293
294         i = simple_strtoul(buf, &endp, 10);
295         spin_lock_irqsave(&ts->lock, flags);
296
297         if (i)
298                 ads7846_disable(ts);
299         else
300                 ads7846_enable(ts);
301
302         spin_unlock_irqrestore(&ts->lock, flags);
303
304         return count;
305 }
306
307 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
308
309 /*--------------------------------------------------------------------------*/
310
311 /*
312  * PENIRQ only kicks the timer.  The timer only reissues the SPI transfer,
313  * to retrieve touchscreen status.
314  *
315  * The SPI transfer completion callback does the real work.  It reports
316  * touchscreen events and reactivates the timer (or IRQ) as appropriate.
317  */
318
319 static void ads7846_rx(void *ads)
320 {
321         struct ads7846          *ts = ads;
322         struct input_dev        *input_dev = ts->input;
323         unsigned                Rt;
324         unsigned                sync = 0;
325         u16                     x, y, z1, z2;
326         unsigned long           flags;
327
328         /* adjust:  12 bit samples (left aligned), built from
329          * two 8 bit values writen msb-first.
330          */
331         x = ts->tc.x >> 3;
332         y = ts->tc.y >> 3;
333         z1 = ts->tc.z1 >> 3;
334         z2 = ts->tc.z2 >> 3;
335
336         /* range filtering */
337         if (x == MAX_12BIT)
338                 x = 0;
339
340         if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
341                 /* compute touch pressure resistance using equation #2 */
342                 Rt = z2;
343                 Rt -= z1;
344                 Rt *= x;
345                 Rt *= ts->x_plate_ohms;
346                 Rt /= z1;
347                 Rt = (Rt + 2047) >> 12;
348         } else
349                 Rt = 0;
350
351         if (Rt > ts->pressure_max) {
352                 if (ts->last_pressure) {
353                         x = ts->last_x;
354                         y = ts->last_y;
355                 }
356                 Rt = ts->pressure_max;
357         }
358
359         ts->last_x = x;
360         ts->last_y = y;
361         ts->last_pressure = Rt;
362
363         /* NOTE:  "pendown" is inferred from pressure; we don't rely on
364          * being able to check nPENIRQ status, or "friendly" trigger modes
365          * (both-edges is much better than just-falling or low-level).
366          *
367          * REVISIT:  some boards may require reading nPENIRQ; it's
368          * needed on 7843.  and 7845 reads pressure differently...
369          *
370          * REVISIT:  the touchscreen might not be connected; this code
371          * won't notice that, even if nPENIRQ never fires ...
372          */
373         if (!ts->pendown && Rt != 0) {
374                 input_report_key(input_dev, BTN_TOUCH, 1);
375                 sync = 1;
376         } else if (ts->pendown && Rt == 0) {
377                 input_report_key(input_dev, BTN_TOUCH, 0);
378                 sync = 1;
379         }
380
381         if (Rt) {
382                 input_report_abs(input_dev, ABS_X, x);
383                 input_report_abs(input_dev, ABS_Y, y);
384                 input_report_abs(input_dev, ABS_PRESSURE, Rt);
385                 sync = 1;
386         }
387         if (sync)
388                 input_sync(input_dev);
389
390 #ifdef  VERBOSE
391         if (Rt || ts->pendown)
392                 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
393                         x, y, Rt, Rt ? "" : " UP");
394 #endif
395
396         /* don't retrigger while we're suspended */
397         spin_lock_irqsave(&ts->lock, flags);
398
399         ts->pendown = (Rt != 0);
400         ts->pending = 0;
401
402         if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
403                 if (ts->pendown)
404                         mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
405                 else if (ts->irq_disabled) {
406                         ts->irq_disabled = 0;
407                         enable_irq(ts->spi->irq);
408                 }
409         }
410
411         spin_unlock_irqrestore(&ts->lock, flags);
412 }
413
414 static void ads7846_debounce(void *ads)
415 {
416         struct ads7846          *ts = ads;
417         struct spi_message      *m;
418         struct spi_transfer     *t;
419         u16                     val;
420         int                     status;
421
422         m = &ts->msg[ts->msg_idx];
423         t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
424         val = (*(u16 *)t->rx_buf) >> 3;
425
426         if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol &&
427                               ts->read_cnt < ts->debounce_max)) {
428                 /* Repeat it, if this was the first read or the read wasn't
429                  * consistent enough */
430                 ts->read_cnt++;
431                 ts->last_read = val;
432         } else {
433                 /* Go for the next read */
434                 ts->msg_idx++;
435                 ts->read_cnt = 0;
436                 m++;
437         }
438         status = spi_async(ts->spi, m);
439         if (status)
440                 dev_err(&ts->spi->dev, "spi_async --> %d\n",
441                                 status);
442 }
443
444 static void ads7846_timer(unsigned long handle)
445 {
446         struct ads7846  *ts = (void *)handle;
447         int             status = 0;
448
449         ts->msg_idx = 0;
450         status = spi_async(ts->spi, &ts->msg[0]);
451         if (status)
452                 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
453 }
454
455 static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
456 {
457         struct ads7846 *ts = handle;
458         unsigned long flags;
459
460         spin_lock_irqsave(&ts->lock, flags);
461         if (likely(!ts->irq_disabled && !ts->disabled)) {
462                 if (!ts->irq_disabled) {
463                         ts->irq_disabled = 1;
464                         /* The following has at the moment no effect whatsoever
465                          * on OMAP, that's why we maintain the disabled
466                          * state ourselves */
467                         disable_irq(ts->spi->irq);
468                 }
469                 if (!ts->pending) {
470                         ts->pending = 1;
471                         mod_timer(&ts->timer, jiffies);
472                 }
473         }
474         spin_unlock_irqrestore(&ts->lock, flags);
475
476         return IRQ_HANDLED;
477 }
478
479 /*--------------------------------------------------------------------------*/
480
481 /* Must be called with ts->lock held */
482 static void ads7846_disable(struct ads7846 *ts)
483 {
484         if (ts->disabled)
485                 return;
486
487         /* are we waiting for IRQ, or polling? */
488         if (!ts->pendown) {
489                 if (!ts->irq_disabled) {
490                         ts->irq_disabled = 1;
491                         disable_irq(ts->spi->irq);
492                 }
493         } else {
494                 unsigned long flags;
495
496                 /* polling; force a final SPI completion;
497                  * that will clean things up neatly
498                  */
499                 if (!ts->pending)
500                         mod_timer(&ts->timer, jiffies);
501
502                 while (ts->pendown || ts->pending) {
503                         spin_unlock_irqrestore(&ts->lock, flags);
504                         msleep(1);
505                         spin_lock_irqsave(&ts->lock, flags);
506                 }
507         }
508
509         /* we know the chip's in lowpower mode since we always
510          * leave it that way after every request
511          */
512
513         ts->disabled = 1;
514 }
515
516 /* Must be called with ts->lock held */
517 static void ads7846_enable(struct ads7846 *ts)
518 {
519         if (!ts->disabled)
520                 return;
521
522         ts->disabled = 0;
523         ts->irq_disabled = 0;
524         enable_irq(ts->spi->irq);
525 }
526
527 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
528 {
529         struct ads7846 *ts = dev_get_drvdata(&spi->dev);
530         unsigned long   flags;
531
532         spin_lock_irqsave(&ts->lock, flags);
533
534         spi->dev.power.power_state = message;
535         ads7846_disable(ts);
536
537         spin_unlock_irqrestore(&ts->lock, flags);
538
539         return 0;
540
541 }
542
543 static int ads7846_resume(struct spi_device *spi)
544 {
545         struct ads7846 *ts = dev_get_drvdata(&spi->dev);
546         unsigned long flags;
547
548         spin_lock_irqsave(&ts->lock, flags);
549
550         spi->dev.power.power_state = PMSG_ON;
551         ads7846_enable(ts);
552
553         spin_unlock_irqrestore(&ts->lock, flags);
554
555         return 0;
556 }
557
558 static int __devinit ads7846_probe(struct spi_device *spi)
559 {
560         struct ads7846                  *ts;
561         struct input_dev                *input_dev;
562         struct ads7846_platform_data    *pdata = spi->dev.platform_data;
563         struct spi_message              *m;
564         struct spi_transfer             *x;
565         int                             err;
566
567         if (!spi->irq) {
568                 dev_dbg(&spi->dev, "no IRQ?\n");
569                 return -ENODEV;
570         }
571
572         if (!pdata) {
573                 dev_dbg(&spi->dev, "no platform data?\n");
574                 return -ENODEV;
575         }
576
577         /* don't exceed max specified sample rate */
578         if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
579                 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
580                                 (spi->max_speed_hz/SAMPLE_BITS)/1000);
581                 return -EINVAL;
582         }
583
584         /* We'd set the wordsize to 12 bits ... except that some controllers
585          * will then treat the 8 bit command words as 12 bits (and drop the
586          * four MSBs of the 12 bit result).  Result: inputs must be shifted
587          * to discard the four garbage LSBs.
588          */
589
590         ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
591         input_dev = input_allocate_device();
592         if (!ts || !input_dev) {
593                 err = -ENOMEM;
594                 goto err_free_mem;
595         }
596
597         dev_set_drvdata(&spi->dev, ts);
598         spi->dev.power.power_state = PMSG_ON;
599
600         ts->spi = spi;
601         ts->input = input_dev;
602
603         init_timer(&ts->timer);
604         ts->timer.data = (unsigned long) ts;
605         ts->timer.function = ads7846_timer;
606
607         spin_lock_init(&ts->lock);
608
609         ts->model = pdata->model ? : 7846;
610         ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
611         ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
612         ts->pressure_max = pdata->pressure_max ? : ~0;
613         ts->debounce_max = pdata->debounce_max ? : 1;
614         ts->debounce_tol = pdata->debounce_tol ? : 10;
615
616         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
617
618         input_dev->name = "ADS784x Touchscreen";
619         input_dev->phys = ts->phys;
620         input_dev->cdev.dev = &spi->dev;
621
622         input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
623         input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
624         input_set_abs_params(input_dev, ABS_X,
625                         pdata->x_min ? : 0,
626                         pdata->x_max ? : MAX_12BIT,
627                         0, 0);
628         input_set_abs_params(input_dev, ABS_Y,
629                         pdata->y_min ? : 0,
630                         pdata->y_max ? : MAX_12BIT,
631                         0, 0);
632         input_set_abs_params(input_dev, ABS_PRESSURE,
633                         pdata->pressure_min, pdata->pressure_max, 0, 0);
634
635         /* set up the transfers to read touchscreen state; this assumes we
636          * use formula #2 for pressure, not #3.
637          */
638         m = &ts->msg[0];
639         x = ts->xfer;
640
641         spi_message_init(m);
642
643         /* y- still on; turn on only y+ (and ADC) */
644         ts->read_y = READ_Y;
645         x->tx_buf = &ts->read_y;
646         x->len = 1;
647         spi_message_add_tail(x, m);
648
649         x++;
650         x->rx_buf = &ts->tc.y;
651         x->len = 2;
652         spi_message_add_tail(x, m);
653
654         m->complete = ads7846_debounce;
655         m->context = ts;
656
657         m++;
658         spi_message_init(m);
659
660         /* turn y- off, x+ on, then leave in lowpower */
661         x++;
662         ts->read_x = READ_X;
663         x->tx_buf = &ts->read_x;
664         x->len = 1;
665         spi_message_add_tail(x, m);
666
667         x++;
668         x->rx_buf = &ts->tc.x;
669         x->len = 2;
670         spi_message_add_tail(x, m);
671
672         m->complete = ads7846_debounce;
673         m->context = ts;
674
675         /* turn y+ off, x- on; we'll use formula #2 */
676         if (ts->model == 7846) {
677                 m++;
678                 spi_message_init(m);
679
680                 x++;
681                 ts->read_z1 = READ_Z1;
682                 x->tx_buf = &ts->read_z1;
683                 x->len = 1;
684                 spi_message_add_tail(x, m);
685
686                 x++;
687                 x->rx_buf = &ts->tc.z1;
688                 x->len = 2;
689                 spi_message_add_tail(x, m);
690
691                 m->complete = ads7846_debounce;
692                 m->context = ts;
693
694                 m++;
695                 spi_message_init(m);
696
697                 x++;
698                 ts->read_z2 = READ_Z2;
699                 x->tx_buf = &ts->read_z2;
700                 x->len = 1;
701                 spi_message_add_tail(x, m);
702
703                 x++;
704                 x->rx_buf = &ts->tc.z2;
705                 x->len = 2;
706                 spi_message_add_tail(x, m);
707
708                 m->complete = ads7846_debounce;
709                 m->context = ts;
710         }
711
712         /* power down */
713         m++;
714         spi_message_init(m);
715
716         x++;
717         ts->pwrdown = PWRDOWN;
718         x->tx_buf = &ts->pwrdown;
719         x->len = 1;
720         spi_message_add_tail(x, m);
721
722         x++;
723         x->rx_buf = &ts->dummy;
724         x->len = 2;
725         CS_CHANGE(*x);
726         spi_message_add_tail(x, m);
727
728         m->complete = ads7846_rx;
729         m->context = ts;
730
731         if (request_irq(spi->irq, ads7846_irq,
732                         SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
733                         spi->dev.bus_id, ts)) {
734                 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
735                 err = -EBUSY;
736                 goto err_free_mem;
737         }
738
739         dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
740
741         /* take a first sample, leaving nPENIRQ active; avoid
742          * the touchscreen, in case it's not connected.
743          */
744         (void) ads7846_read12_ser(&spi->dev,
745                           READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
746
747         /* ads7843/7845 don't have temperature sensors, and
748          * use the other sensors a bit differently too
749          */
750         if (ts->model == 7846) {
751                 device_create_file(&spi->dev, &dev_attr_temp0);
752                 device_create_file(&spi->dev, &dev_attr_temp1);
753         }
754         if (ts->model != 7845)
755                 device_create_file(&spi->dev, &dev_attr_vbatt);
756         device_create_file(&spi->dev, &dev_attr_vaux);
757
758         device_create_file(&spi->dev, &dev_attr_pen_down);
759
760         device_create_file(&spi->dev, &dev_attr_disable);
761
762         err = input_register_device(input_dev);
763         if (err)
764                 goto err_remove_attr;
765
766         return 0;
767
768  err_remove_attr:
769         device_remove_file(&spi->dev, &dev_attr_disable);
770         device_remove_file(&spi->dev, &dev_attr_pen_down);
771         if (ts->model == 7846) {
772                 device_remove_file(&spi->dev, &dev_attr_temp1);
773                 device_remove_file(&spi->dev, &dev_attr_temp0);
774         }
775         if (ts->model != 7845)
776                 device_remove_file(&spi->dev, &dev_attr_vbatt);
777         device_remove_file(&spi->dev, &dev_attr_vaux);
778
779         free_irq(spi->irq, ts);
780  err_free_mem:
781         input_free_device(input_dev);
782         kfree(ts);
783         return err;
784 }
785
786 static int __devexit ads7846_remove(struct spi_device *spi)
787 {
788         struct ads7846          *ts = dev_get_drvdata(&spi->dev);
789
790         input_unregister_device(ts->input);
791
792         ads7846_suspend(spi, PMSG_SUSPEND);
793
794         device_remove_file(&spi->dev, &dev_attr_disable);
795         device_remove_file(&spi->dev, &dev_attr_pen_down);
796         if (ts->model == 7846) {
797                 device_remove_file(&spi->dev, &dev_attr_temp1);
798                 device_remove_file(&spi->dev, &dev_attr_temp0);
799         }
800         if (ts->model != 7845)
801                 device_remove_file(&spi->dev, &dev_attr_vbatt);
802         device_remove_file(&spi->dev, &dev_attr_vaux);
803
804         free_irq(ts->spi->irq, ts);
805         if (ts->irq_disabled)
806                 enable_irq(ts->spi->irq);
807
808         kfree(ts);
809
810         dev_dbg(&spi->dev, "unregistered touchscreen\n");
811         return 0;
812 }
813
814 static struct spi_driver ads7846_driver = {
815         .driver = {
816                 .name   = "ads7846",
817                 .bus    = &spi_bus_type,
818                 .owner  = THIS_MODULE,
819         },
820         .probe          = ads7846_probe,
821         .remove         = __devexit_p(ads7846_remove),
822         .suspend        = ads7846_suspend,
823         .resume         = ads7846_resume,
824 };
825
826 static int __init ads7846_init(void)
827 {
828         /* grr, board-specific init should stay out of drivers!! */
829
830 #ifdef  CONFIG_ARCH_OMAP
831         if (machine_is_omap_osk()) {
832                 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
833                 omap_request_gpio(4);
834                 omap_set_gpio_direction(4, 1);
835                 omap_request_gpio(6);
836                 omap_set_gpio_direction(6, 1);
837         }
838         // also TI 1510 Innovator, bitbanging through FPGA
839         // also Nokia 770
840         // also Palm Tungsten T2
841 #endif
842
843         // PXA:
844         // also Dell Axim X50
845         // also HP iPaq H191x/H192x/H415x/H435x
846         // also Intel Lubbock (additional to UCB1400; as temperature sensor)
847         // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
848
849         // Atmel at91sam9261-EK uses ads7843
850
851         // also various AMD Au1x00 devel boards
852
853         return spi_register_driver(&ads7846_driver);
854 }
855 module_init(ads7846_init);
856
857 static void __exit ads7846_exit(void)
858 {
859         spi_unregister_driver(&ads7846_driver);
860
861 #ifdef  CONFIG_ARCH_OMAP
862         if (machine_is_omap_osk()) {
863                 omap_free_gpio(4);
864                 omap_free_gpio(6);
865         }
866 #endif
867
868 }
869 module_exit(ads7846_exit);
870
871 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
872 MODULE_LICENSE("GPL");