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