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