]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/touchscreen/ads7846.c
Input: ads7846 - convert to to dynamic input_dev allocation
[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  *
6  * Using code from:
7  *  - corgi_ts.c
8  *      Copyright (C) 2004-2005 Richard Purdie
9  *  - omap_ts.[hc], ads7846.h, ts_osk.c
10  *      Copyright (C) 2002 MontaVista Software
11  *      Copyright (C) 2004 Texas Instruments
12  *      Copyright (C) 2005 Dirk Behme
13  *
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License version 2 as
16  *  published by the Free Software Foundation.
17  */
18 #include <linux/device.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/input.h>
22 #include <linux/interrupt.h>
23 #include <linux/slab.h>
24 #include <linux/spi/spi.h>
25 #include <linux/spi/ads7846.h>
26
27 #ifdef  CONFIG_ARM
28 #include <asm/mach-types.h>
29 #ifdef  CONFIG_ARCH_OMAP
30 #include <asm/arch/gpio.h>
31 #endif
32 #endif
33
34
35 /*
36  * This code has been lightly tested on an ads7846.
37  * Support for ads7843 and ads7845 has only been stubbed in.
38  *
39  * Not yet done:  investigate the values reported.  Are x/y/pressure
40  * event values sane enough for X11?  How accurate are the temperature
41  * and voltage readings?  (System-specific calibration should support
42  * accuracy of 0.3 degrees C; otherwise it's 2.0 degrees.)
43  *
44  * app note sbaa036 talks in more detail about accurate sampling...
45  * that ought to help in situations like LCDs inducing noise (which
46  * can also be helped by using synch signals) and more generally.
47  */
48
49 #define TS_POLL_PERIOD  msecs_to_jiffies(10)
50
51 struct ts_event {
52         /* For portability, we can't read 12 bit values using SPI (which
53          * would make the controller deliver them as native byteorder u16
54          * with msbs zeroed).  Instead, we read them as two 8-byte values,
55          * which need byteswapping then range adjustment.
56          */
57         __be16 x;
58         __be16 y;
59         __be16 z1, z2;
60 };
61
62 struct ads7846 {
63         struct input_dev        *input;
64         char                    phys[32];
65
66         struct spi_device       *spi;
67         u16                     model;
68         u16                     vref_delay_usecs;
69         u16                     x_plate_ohms;
70
71         struct ts_event         tc;
72
73         struct spi_transfer     xfer[8];
74         struct spi_message      msg;
75
76         spinlock_t              lock;
77         struct timer_list       timer;          /* P: lock */
78         unsigned                pendown:1;      /* P: lock */
79         unsigned                pending:1;      /* P: lock */
80 // FIXME remove "irq_disabled"
81         unsigned                irq_disabled:1; /* P: lock */
82 };
83
84 /* leave chip selected when we're done, for quicker re-select? */
85 #if     0
86 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
87 #else
88 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
89 #endif
90
91 /*--------------------------------------------------------------------------*/
92
93 /* The ADS7846 has touchscreen and other sensors.
94  * Earlier ads784x chips are somewhat compatible.
95  */
96 #define ADS_START               (1 << 7)
97 #define ADS_A2A1A0_d_y          (1 << 4)        /* differential */
98 #define ADS_A2A1A0_d_z1         (3 << 4)        /* differential */
99 #define ADS_A2A1A0_d_z2         (4 << 4)        /* differential */
100 #define ADS_A2A1A0_d_x          (5 << 4)        /* differential */
101 #define ADS_A2A1A0_temp0        (0 << 4)        /* non-differential */
102 #define ADS_A2A1A0_vbatt        (2 << 4)        /* non-differential */
103 #define ADS_A2A1A0_vaux         (6 << 4)        /* non-differential */
104 #define ADS_A2A1A0_temp1        (7 << 4)        /* non-differential */
105 #define ADS_8_BIT               (1 << 3)
106 #define ADS_12_BIT              (0 << 3)
107 #define ADS_SER                 (1 << 2)        /* non-differential */
108 #define ADS_DFR                 (0 << 2)        /* differential */
109 #define ADS_PD10_PDOWN          (0 << 0)        /* lowpower mode + penirq */
110 #define ADS_PD10_ADC_ON         (1 << 0)        /* ADC on */
111 #define ADS_PD10_REF_ON         (2 << 0)        /* vREF on + penirq */
112 #define ADS_PD10_ALL_ON         (3 << 0)        /* ADC + vREF on */
113
114 #define MAX_12BIT       ((1<<12)-1)
115
116 /* leave ADC powered up (disables penirq) between differential samples */
117 #define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
118         | ADS_12_BIT | ADS_DFR)
119
120 static const u8 read_y  = READ_12BIT_DFR(y)  | ADS_PD10_ADC_ON;
121 static const u8 read_z1 = READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON;
122 static const u8 read_z2 = READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON;
123 static const u8 read_x  = READ_12BIT_DFR(x)  | ADS_PD10_PDOWN;  /* LAST */
124
125 /* single-ended samples need to first power up reference voltage;
126  * we leave both ADC and VREF powered
127  */
128 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
129         | ADS_12_BIT | ADS_SER)
130
131 static const u8 ref_on = READ_12BIT_DFR(x) | ADS_PD10_ALL_ON;
132 static const u8 ref_off = READ_12BIT_DFR(y) | ADS_PD10_PDOWN;
133
134 /*--------------------------------------------------------------------------*/
135
136 /*
137  * Non-touchscreen sensors only use single-ended conversions.
138  */
139
140 struct ser_req {
141         u8                      command;
142         u16                     scratch;
143         __be16                  sample;
144         struct spi_message      msg;
145         struct spi_transfer     xfer[6];
146 };
147
148 static int ads7846_read12_ser(struct device *dev, unsigned command)
149 {
150         struct spi_device       *spi = to_spi_device(dev);
151         struct ads7846          *ts = dev_get_drvdata(dev);
152         struct ser_req          *req = kzalloc(sizeof *req, SLAB_KERNEL);
153         int                     status;
154         int                     sample;
155         int                     i;
156
157         if (!req)
158                 return -ENOMEM;
159
160         INIT_LIST_HEAD(&req->msg.transfers);
161
162         /* activate reference, so it has time to settle; */
163         req->xfer[0].tx_buf = &ref_on;
164         req->xfer[0].len = 1;
165         req->xfer[1].rx_buf = &req->scratch;
166         req->xfer[1].len = 2;
167
168         /*
169          * for external VREF, 0 usec (and assume it's always on);
170          * for 1uF, use 800 usec;
171          * no cap, 100 usec.
172          */
173         req->xfer[1].delay_usecs = ts->vref_delay_usecs;
174
175         /* take sample */
176         req->command = (u8) command;
177         req->xfer[2].tx_buf = &req->command;
178         req->xfer[2].len = 1;
179         req->xfer[3].rx_buf = &req->sample;
180         req->xfer[3].len = 2;
181
182         /* REVISIT:  take a few more samples, and compare ... */
183
184         /* turn off reference */
185         req->xfer[4].tx_buf = &ref_off;
186         req->xfer[4].len = 1;
187         req->xfer[5].rx_buf = &req->scratch;
188         req->xfer[5].len = 2;
189
190         CS_CHANGE(req->xfer[5]);
191
192         /* group all the transfers together, so we can't interfere with
193          * reading touchscreen state; disable penirq while sampling
194          */
195         for (i = 0; i < 6; i++)
196                 spi_message_add_tail(&req->xfer[i], &req->msg);
197
198         disable_irq(spi->irq);
199         status = spi_sync(spi, &req->msg);
200         enable_irq(spi->irq);
201
202         if (req->msg.status)
203                 status = req->msg.status;
204         sample = be16_to_cpu(req->sample);
205         sample = sample >> 4;
206         kfree(req);
207
208         return status ? status : sample;
209 }
210
211 #define SHOW(name) static ssize_t \
212 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
213 { \
214         ssize_t v = ads7846_read12_ser(dev, \
215                         READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
216         if (v < 0) \
217                 return v; \
218         return sprintf(buf, "%u\n", (unsigned) v); \
219 } \
220 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
221
222 SHOW(temp0)
223 SHOW(temp1)
224 SHOW(vaux)
225 SHOW(vbatt)
226
227 /*--------------------------------------------------------------------------*/
228
229 /*
230  * PENIRQ only kicks the timer.  The timer only reissues the SPI transfer,
231  * to retrieve touchscreen status.
232  *
233  * The SPI transfer completion callback does the real work.  It reports
234  * touchscreen events and reactivates the timer (or IRQ) as appropriate.
235  */
236
237 static void ads7846_rx(void *ads)
238 {
239         struct ads7846          *ts = ads;
240         struct input_dev        *input_dev = ts->input;
241         unsigned                Rt;
242         unsigned                sync = 0;
243         u16                     x, y, z1, z2;
244         unsigned long           flags;
245
246         /* adjust:  12 bit samples (left aligned), built from
247          * two 8 bit values writen msb-first.
248          */
249         x = be16_to_cpu(ts->tc.x) >> 4;
250         y = be16_to_cpu(ts->tc.y) >> 4;
251         z1 = be16_to_cpu(ts->tc.z1) >> 4;
252         z2 = be16_to_cpu(ts->tc.z2) >> 4;
253
254         /* range filtering */
255         if (x == MAX_12BIT)
256                 x = 0;
257
258         if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
259                 /* compute touch pressure resistance using equation #2 */
260                 Rt = z2;
261                 Rt -= z1;
262                 Rt *= x;
263                 Rt *= ts->x_plate_ohms;
264                 Rt /= z1;
265                 Rt = (Rt + 2047) >> 12;
266         } else
267                 Rt = 0;
268
269         /* NOTE:  "pendown" is inferred from pressure; we don't rely on
270          * being able to check nPENIRQ status, or "friendly" trigger modes
271          * (both-edges is much better than just-falling or low-level).
272          *
273          * REVISIT:  some boards may require reading nPENIRQ; it's
274          * needed on 7843.  and 7845 reads pressure differently...
275          *
276          * REVISIT:  the touchscreen might not be connected; this code
277          * won't notice that, even if nPENIRQ never fires ...
278          */
279         if (!ts->pendown && Rt != 0) {
280                 input_report_key(input_dev, BTN_TOUCH, 1);
281                 sync = 1;
282         } else if (ts->pendown && Rt == 0) {
283                 input_report_key(input_dev, BTN_TOUCH, 0);
284                 sync = 1;
285         }
286
287         if (Rt) {
288                 input_report_abs(input_dev, ABS_X, x);
289                 input_report_abs(input_dev, ABS_Y, y);
290                 input_report_abs(input_dev, ABS_PRESSURE, Rt);
291                 sync = 1;
292         }
293         if (sync)
294                 input_sync(input_dev);
295
296 #ifdef  VERBOSE
297         if (Rt || ts->pendown)
298                 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
299                         x, y, Rt, Rt ? "" : " UP");
300 #endif
301
302         /* don't retrigger while we're suspended */
303         spin_lock_irqsave(&ts->lock, flags);
304
305         ts->pendown = (Rt != 0);
306         ts->pending = 0;
307
308         if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
309                 if (ts->pendown)
310                         mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
311                 else if (ts->irq_disabled) {
312                         ts->irq_disabled = 0;
313                         enable_irq(ts->spi->irq);
314                 }
315         }
316
317         spin_unlock_irqrestore(&ts->lock, flags);
318 }
319
320 static void ads7846_timer(unsigned long handle)
321 {
322         struct ads7846  *ts = (void *)handle;
323         int             status = 0;
324         unsigned long   flags;
325
326         spin_lock_irqsave(&ts->lock, flags);
327         if (!ts->pending) {
328                 ts->pending = 1;
329                 if (!ts->irq_disabled) {
330                         ts->irq_disabled = 1;
331                         disable_irq(ts->spi->irq);
332                 }
333                 status = spi_async(ts->spi, &ts->msg);
334                 if (status)
335                         dev_err(&ts->spi->dev, "spi_async --> %d\n",
336                                         status);
337         }
338         spin_unlock_irqrestore(&ts->lock, flags);
339 }
340
341 static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
342 {
343         ads7846_timer((unsigned long) handle);
344         return IRQ_HANDLED;
345 }
346
347 /*--------------------------------------------------------------------------*/
348
349 static int
350 ads7846_suspend(struct spi_device *spi, pm_message_t message)
351 {
352         struct ads7846 *ts = dev_get_drvdata(&spi->dev);
353         unsigned long   flags;
354
355         spin_lock_irqsave(&ts->lock, flags);
356
357         spi->dev.power.power_state = message;
358
359         /* are we waiting for IRQ, or polling? */
360         if (!ts->pendown) {
361                 if (!ts->irq_disabled) {
362                         ts->irq_disabled = 1;
363                         disable_irq(ts->spi->irq);
364                 }
365         } else {
366                 /* polling; force a final SPI completion;
367                  * that will clean things up neatly
368                  */
369                 if (!ts->pending)
370                         mod_timer(&ts->timer, jiffies);
371
372                 while (ts->pendown || ts->pending) {
373                         spin_unlock_irqrestore(&ts->lock, flags);
374                         udelay(10);
375                         spin_lock_irqsave(&ts->lock, flags);
376                 }
377         }
378
379         /* we know the chip's in lowpower mode since we always
380          * leave it that way after every request
381          */
382
383         spin_unlock_irqrestore(&ts->lock, flags);
384         return 0;
385 }
386
387 static int ads7846_resume(struct spi_device *spi)
388 {
389         struct ads7846 *ts = dev_get_drvdata(&spi->dev);
390
391         ts->irq_disabled = 0;
392         enable_irq(ts->spi->irq);
393         spi->dev.power.power_state = PMSG_ON;
394         return 0;
395 }
396
397 static int __devinit ads7846_probe(struct spi_device *spi)
398 {
399         struct ads7846                  *ts;
400         struct input_dev                *input_dev;
401         struct ads7846_platform_data    *pdata = spi->dev.platform_data;
402         struct spi_transfer             *x;
403         int                             i;
404         int                             err;
405
406         if (!spi->irq) {
407                 dev_dbg(&spi->dev, "no IRQ?\n");
408                 return -ENODEV;
409         }
410
411         if (!pdata) {
412                 dev_dbg(&spi->dev, "no platform data?\n");
413                 return -ENODEV;
414         }
415
416         /* don't exceed max specified sample rate */
417         if (spi->max_speed_hz > (125000 * 16)) {
418                 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
419                                 (spi->max_speed_hz/16)/1000);
420                 return -EINVAL;
421         }
422
423         /* We'd set the wordsize to 12 bits ... except that some controllers
424          * will then treat the 8 bit command words as 12 bits (and drop the
425          * four MSBs of the 12 bit result).  Result: inputs must be shifted
426          * to discard the four garbage LSBs.
427          */
428
429         ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
430         input_dev = input_allocate_device();
431         if (!ts || !input_dev) {
432                 err = -ENOMEM;
433                 goto err_free_mem;
434         }
435
436         dev_set_drvdata(&spi->dev, ts);
437         spi->dev.power.power_state = PMSG_ON;
438
439         ts->spi = spi;
440         ts->input = input_dev;
441
442         init_timer(&ts->timer);
443         ts->timer.data = (unsigned long) ts;
444         ts->timer.function = ads7846_timer;
445
446         ts->model = pdata->model ? : 7846;
447         ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
448         ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
449
450         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
451
452         input_dev->name = "ADS784x Touchscreen";
453         input_dev->phys = ts->phys;
454         input_dev->cdev.dev = &spi->dev;
455
456         input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
457         input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
458         input_set_abs_params(input_dev, ABS_X,
459                         pdata->x_min ? : 0,
460                         pdata->x_max ? : MAX_12BIT,
461                         0, 0);
462         input_set_abs_params(input_dev, ABS_Y,
463                         pdata->y_min ? : 0,
464                         pdata->y_max ? : MAX_12BIT,
465                         0, 0);
466         input_set_abs_params(input_dev, ABS_PRESSURE,
467                         pdata->pressure_min, pdata->pressure_max, 0, 0);
468
469         /* set up the transfers to read touchscreen state; this assumes we
470          * use formula #2 for pressure, not #3.
471          */
472         x = ts->xfer;
473
474         /* y- still on; turn on only y+ (and ADC) */
475         x->tx_buf = &read_y;
476         x->len = 1;
477         x++;
478         x->rx_buf = &ts->tc.y;
479         x->len = 2;
480         x++;
481
482         /* turn y+ off, x- on; we'll use formula #2 */
483         if (ts->model == 7846) {
484                 x->tx_buf = &read_z1;
485                 x->len = 1;
486                 x++;
487                 x->rx_buf = &ts->tc.z1;
488                 x->len = 2;
489                 x++;
490
491                 x->tx_buf = &read_z2;
492                 x->len = 1;
493                 x++;
494                 x->rx_buf = &ts->tc.z2;
495                 x->len = 2;
496                 x++;
497         }
498
499         /* turn y- off, x+ on, then leave in lowpower */
500         x->tx_buf = &read_x;
501         x->len = 1;
502         x++;
503         x->rx_buf = &ts->tc.x;
504         x->len = 2;
505         x++;
506
507         CS_CHANGE(x[-1]);
508
509         for (i = 0; i < x - ts->xfer; i++)
510                 spi_message_add_tail(&ts->xfer[i], &ts->msg);
511         ts->msg.complete = ads7846_rx;
512         ts->msg.context = ts;
513
514         if (request_irq(spi->irq, ads7846_irq,
515                         SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
516                         spi->dev.bus_id, ts)) {
517                 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
518                 err = -EBUSY;
519                 goto err_free_mem;
520         }
521
522         dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
523
524         /* take a first sample, leaving nPENIRQ active; avoid
525          * the touchscreen, in case it's not connected.
526          */
527         (void) ads7846_read12_ser(&spi->dev,
528                           READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
529
530         /* ads7843/7845 don't have temperature sensors, and
531          * use the other sensors a bit differently too
532          */
533         if (ts->model == 7846) {
534                 device_create_file(&spi->dev, &dev_attr_temp0);
535                 device_create_file(&spi->dev, &dev_attr_temp1);
536         }
537         if (ts->model != 7845)
538                 device_create_file(&spi->dev, &dev_attr_vbatt);
539         device_create_file(&spi->dev, &dev_attr_vaux);
540
541         err = input_register_device(input_dev);
542         if (err)
543                 goto err_free_irq;
544
545         return 0;
546
547  err_free_irq:
548         free_irq(spi->irq, ts);
549  err_free_mem:
550         input_free_device(input_dev);
551         kfree(ts);
552         return err;
553 }
554
555 static int __devexit ads7846_remove(struct spi_device *spi)
556 {
557         struct ads7846          *ts = dev_get_drvdata(&spi->dev);
558
559         ads7846_suspend(spi, PMSG_SUSPEND);
560         free_irq(ts->spi->irq, ts);
561         if (ts->irq_disabled)
562                 enable_irq(ts->spi->irq);
563
564         if (ts->model == 7846) {
565                 device_remove_file(&spi->dev, &dev_attr_temp0);
566                 device_remove_file(&spi->dev, &dev_attr_temp1);
567         }
568         if (ts->model != 7845)
569                 device_remove_file(&spi->dev, &dev_attr_vbatt);
570         device_remove_file(&spi->dev, &dev_attr_vaux);
571
572         input_unregister_device(ts->input);
573         kfree(ts);
574
575         dev_dbg(&spi->dev, "unregistered touchscreen\n");
576         return 0;
577 }
578
579 static struct spi_driver ads7846_driver = {
580         .driver = {
581                 .name   = "ads7846",
582                 .bus    = &spi_bus_type,
583                 .owner  = THIS_MODULE,
584         },
585         .probe          = ads7846_probe,
586         .remove         = __devexit_p(ads7846_remove),
587         .suspend        = ads7846_suspend,
588         .resume         = ads7846_resume,
589 };
590
591 static int __init ads7846_init(void)
592 {
593         /* grr, board-specific init should stay out of drivers!! */
594
595 #ifdef  CONFIG_ARCH_OMAP
596         if (machine_is_omap_osk()) {
597                 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
598                 omap_request_gpio(4);
599                 omap_set_gpio_direction(4, 1);
600                 omap_request_gpio(6);
601                 omap_set_gpio_direction(6, 1);
602         }
603         // also TI 1510 Innovator, bitbanging through FPGA
604         // also Nokia 770
605         // also Palm Tungsten T2
606 #endif
607
608         // PXA:
609         // also Dell Axim X50
610         // also HP iPaq H191x/H192x/H415x/H435x
611         // also Intel Lubbock (additional to UCB1400; as temperature sensor)
612         // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
613
614         // Atmel at91sam9261-EK uses ads7843
615
616         // also various AMD Au1x00 devel boards
617
618         return spi_register_driver(&ads7846_driver);
619 }
620 module_init(ads7846_init);
621
622 static void __exit ads7846_exit(void)
623 {
624         spi_unregister_driver(&ads7846_driver);
625
626 #ifdef  CONFIG_ARCH_OMAP
627         if (machine_is_omap_osk()) {
628                 omap_free_gpio(4);
629                 omap_free_gpio(6);
630         }
631 #endif
632
633 }
634 module_exit(ads7846_exit);
635
636 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
637 MODULE_LICENSE("GPL");