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