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