2 * ADS7846 based touchscreen and sensor driver
4 * Copyright (c) 2005 David Brownell
5 * Copyright (c) 2006 Nokia Corporation
6 * Various changes: Imre Deak <imre.deak@nokia.com>
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
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.
20 #include <linux/device.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/input.h>
24 #include <linux/interrupt.h>
25 #include <linux/slab.h>
26 #include <linux/spi/spi.h>
27 #include <linux/spi/ads7846.h>
31 #include <asm/mach-types.h>
32 #ifdef CONFIG_ARCH_OMAP
33 #include <asm/arch/gpio.h>
39 * This code has been tested on an ads7846 / N770 device.
40 * Support for ads7843 and ads7845 has only been stubbed in.
42 * Not yet done: How accurate are the temperature and voltage
43 * readings? (System-specific calibration should support
44 * accuracy of 0.3 degrees C; otherwise it's 2.0 degrees.)
46 * IRQ handling needs a workaround because of a shortcoming in handling
47 * edge triggered IRQs on some platforms like the OMAP1/2. These
48 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
49 * have to maintain our own SW IRQ disabled status. This should be
50 * removed as soon as the affected platform's IRQ handling is fixed.
52 * app note sbaa036 talks in more detail about accurate sampling...
53 * that ought to help in situations like LCDs inducing noise (which
54 * can also be helped by using synch signals) and more generally.
55 * This driver tries to utilize the measures described in the app
56 * note. The strength of filtering can be set in the board-* specific
60 #define TS_POLL_PERIOD msecs_to_jiffies(10)
62 /* this driver doesn't aim at the peak continuous sample rate */
63 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
66 /* For portability, we can't read 12 bit values using SPI (which
67 * would make the controller deliver them as native byteorder u16
68 * with msbs zeroed). Instead, we read them as two 8-bit values,
69 * which need byteswapping then range adjustment.
77 struct input_dev *input;
80 struct spi_device *spi;
85 u8 read_x, read_y, read_z1, read_z2, pwrdown;
86 u16 dummy; /* for the pwrdown read */
89 struct spi_transfer xfer[10];
90 struct spi_message msg[5];
99 struct timer_list timer; /* P: lock */
100 unsigned pendown:1; /* P: lock */
101 unsigned pending:1; /* P: lock */
102 // FIXME remove "irq_disabled"
103 unsigned irq_disabled:1; /* P: lock */
107 /* leave chip selected when we're done, for quicker re-select? */
109 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
111 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
114 /*--------------------------------------------------------------------------*/
116 /* The ADS7846 has touchscreen and other sensors.
117 * Earlier ads784x chips are somewhat compatible.
119 #define ADS_START (1 << 7)
120 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
121 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
122 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
123 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
124 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
125 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
126 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
127 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
128 #define ADS_8_BIT (1 << 3)
129 #define ADS_12_BIT (0 << 3)
130 #define ADS_SER (1 << 2) /* non-differential */
131 #define ADS_DFR (0 << 2) /* differential */
132 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
133 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
134 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
135 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
137 #define MAX_12BIT ((1<<12)-1)
139 /* leave ADC powered up (disables penirq) between differential samples */
140 #define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
141 | ADS_12_BIT | ADS_DFR)
143 #define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON)
144 #define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
145 #define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
147 #define READ_X (READ_12BIT_DFR(x) | ADS_PD10_ADC_ON)
148 #define PWRDOWN (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) /* LAST */
150 /* single-ended samples need to first power up reference voltage;
151 * we leave both ADC and VREF powered
153 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
154 | ADS_12_BIT | ADS_SER)
156 #define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
157 #define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
159 /*--------------------------------------------------------------------------*/
162 * Non-touchscreen sensors only use single-ended conversions.
171 struct spi_message msg;
172 struct spi_transfer xfer[6];
175 static void ads7846_enable(struct ads7846 *ts);
176 static void ads7846_disable(struct ads7846 *ts);
178 static int ads7846_read12_ser(struct device *dev, unsigned command)
180 struct spi_device *spi = to_spi_device(dev);
181 struct ads7846 *ts = dev_get_drvdata(dev);
182 struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL);
190 spi_message_init(&req->msg);
192 /* activate reference, so it has time to settle; */
193 req->ref_on = REF_ON;
194 req->xfer[0].tx_buf = &req->ref_on;
195 req->xfer[0].len = 1;
196 req->xfer[1].rx_buf = &req->scratch;
197 req->xfer[1].len = 2;
200 * for external VREF, 0 usec (and assume it's always on);
201 * for 1uF, use 800 usec;
204 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
207 req->command = (u8) command;
208 req->xfer[2].tx_buf = &req->command;
209 req->xfer[2].len = 1;
210 req->xfer[3].rx_buf = &req->sample;
211 req->xfer[3].len = 2;
213 /* REVISIT: take a few more samples, and compare ... */
215 /* turn off reference */
216 req->ref_off = REF_OFF;
217 req->xfer[4].tx_buf = &req->ref_off;
218 req->xfer[4].len = 1;
219 req->xfer[5].rx_buf = &req->scratch;
220 req->xfer[5].len = 2;
222 CS_CHANGE(req->xfer[5]);
224 /* group all the transfers together, so we can't interfere with
225 * reading touchscreen state; disable penirq while sampling
227 for (i = 0; i < 6; i++)
228 spi_message_add_tail(&req->xfer[i], &req->msg);
230 disable_irq(spi->irq);
231 status = spi_sync(spi, &req->msg);
232 enable_irq(spi->irq);
235 status = req->msg.status;
236 sample = be16_to_cpu(req->sample);
237 sample = sample >> 4;
240 return status ? status : sample;
243 #define SHOW(name) static ssize_t \
244 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
246 ssize_t v = ads7846_read12_ser(dev, \
247 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
250 return sprintf(buf, "%u\n", (unsigned) v); \
252 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
259 static int is_pen_down(struct device *dev)
261 struct ads7846 *ts = dev_get_drvdata(dev);
266 static ssize_t ads7846_pen_down_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
269 return sprintf(buf, "%u\n", is_pen_down(dev));
272 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
274 static ssize_t ads7846_disable_show(struct device *dev,
275 struct device_attribute *attr, char *buf)
277 struct ads7846 *ts = dev_get_drvdata(dev);
279 return sprintf(buf, "%u\n", ts->disabled);
282 static ssize_t ads7846_disable_store(struct device *dev,
283 struct device_attribute *attr,
284 const char *buf, size_t count)
286 struct ads7846 *ts = dev_get_drvdata(dev);
290 i = simple_strtoul(buf, &endp, 10);
291 spin_lock_irq(&ts->lock);
298 spin_unlock_irq(&ts->lock);
303 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
305 /*--------------------------------------------------------------------------*/
308 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
309 * to retrieve touchscreen status.
311 * The SPI transfer completion callback does the real work. It reports
312 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
315 static void ads7846_rx(void *ads)
317 struct ads7846 *ts = ads;
318 struct input_dev *input_dev = ts->input;
324 /* adjust: 12 bit samples (left aligned), built from
325 * two 8 bit values writen msb-first.
327 x = be16_to_cpu(ts->tc.x) >> 4;
328 y = be16_to_cpu(ts->tc.y) >> 4;
329 z1 = be16_to_cpu(ts->tc.z1) >> 4;
330 z2 = be16_to_cpu(ts->tc.z2) >> 4;
332 /* range filtering */
336 if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
337 /* compute touch pressure resistance using equation #2 */
341 Rt *= ts->x_plate_ohms;
343 Rt = (Rt + 2047) >> 12;
347 /* NOTE: "pendown" is inferred from pressure; we don't rely on
348 * being able to check nPENIRQ status, or "friendly" trigger modes
349 * (both-edges is much better than just-falling or low-level).
351 * REVISIT: some boards may require reading nPENIRQ; it's
352 * needed on 7843. and 7845 reads pressure differently...
354 * REVISIT: the touchscreen might not be connected; this code
355 * won't notice that, even if nPENIRQ never fires ...
357 if (!ts->pendown && Rt != 0) {
358 input_report_key(input_dev, BTN_TOUCH, 1);
360 } else if (ts->pendown && Rt == 0) {
361 input_report_key(input_dev, BTN_TOUCH, 0);
366 input_report_abs(input_dev, ABS_X, x);
367 input_report_abs(input_dev, ABS_Y, y);
368 input_report_abs(input_dev, ABS_PRESSURE, Rt);
372 input_sync(input_dev);
375 if (Rt || ts->pendown)
376 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
377 x, y, Rt, Rt ? "" : " UP");
380 /* don't retrigger while we're suspended */
381 spin_lock_irqsave(&ts->lock, flags);
383 ts->pendown = (Rt != 0);
386 if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
388 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
389 else if (ts->irq_disabled) {
390 ts->irq_disabled = 0;
391 enable_irq(ts->spi->irq);
395 spin_unlock_irqrestore(&ts->lock, flags);
398 static void ads7846_debounce(void *ads)
400 struct ads7846 *ts = ads;
401 struct spi_message *m;
402 struct spi_transfer *t;
406 m = &ts->msg[ts->msg_idx];
407 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
408 val = (*(u16 *)t->rx_buf) >> 3;
410 if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol
411 && ts->read_cnt < ts->debounce_max)) {
412 /* Repeat it, if this was the first read or the read wasn't
418 /* Go for the next read */
423 status = spi_async(ts->spi, m);
425 dev_err(&ts->spi->dev, "spi_async --> %d\n",
429 static void ads7846_timer(unsigned long handle)
431 struct ads7846 *ts = (void *)handle;
435 status = spi_async(ts->spi, &ts->msg[0]);
437 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
440 static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
442 struct ads7846 *ts = handle;
445 spin_lock_irqsave(&ts->lock, flags);
446 if (likely(!ts->irq_disabled && !ts->disabled)) {
447 if (!ts->irq_disabled) {
448 /* REVISIT irq logic for many ARM chips has cloned a
449 * bug wherein disabling an irq in its handler won't
450 * work;(it's disabled lazily, and too late to work.
451 * until all their irq logic is fixed, we must shadow
454 ts->irq_disabled = 1;
456 disable_irq(ts->spi->irq);
460 mod_timer(&ts->timer, jiffies);
463 spin_unlock_irqrestore(&ts->lock, flags);
468 /*--------------------------------------------------------------------------*/
470 /* Must be called with ts->lock held */
471 static void ads7846_disable(struct ads7846 *ts)
476 /* are we waiting for IRQ, or polling? */
478 if (!ts->irq_disabled) {
479 ts->irq_disabled = 1;
480 disable_irq(ts->spi->irq);
483 /* polling; force a final SPI completion;
484 * that will clean things up neatly
487 mod_timer(&ts->timer, jiffies);
489 while (ts->pendown || ts->pending) {
490 spin_unlock_irq(&ts->lock);
492 spin_lock_irq(&ts->lock);
496 /* we know the chip's in lowpower mode since we always
497 * leave it that way after every request
503 /* Must be called with ts->lock held */
504 static void ads7846_enable(struct ads7846 *ts)
510 ts->irq_disabled = 0;
511 enable_irq(ts->spi->irq);
514 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
516 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
518 spin_lock_irq(&ts->lock);
520 spi->dev.power.power_state = message;
523 spin_unlock_irq(&ts->lock);
529 static int ads7846_resume(struct spi_device *spi)
531 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
533 spin_lock_irq(&ts->lock);
535 spi->dev.power.power_state = PMSG_ON;
538 spin_unlock_irq(&ts->lock);
543 static int __devinit ads7846_probe(struct spi_device *spi)
546 struct input_dev *input_dev;
547 struct ads7846_platform_data *pdata = spi->dev.platform_data;
548 struct spi_message *m;
549 struct spi_transfer *x;
553 dev_dbg(&spi->dev, "no IRQ?\n");
558 dev_dbg(&spi->dev, "no platform data?\n");
562 /* don't exceed max specified sample rate */
563 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
564 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
565 (spi->max_speed_hz/SAMPLE_BITS)/1000);
569 /* We'd set the wordsize to 12 bits ... except that some controllers
570 * will then treat the 8 bit command words as 12 bits (and drop the
571 * four MSBs of the 12 bit result). Result: inputs must be shifted
572 * to discard the four garbage LSBs.
575 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
576 input_dev = input_allocate_device();
577 if (!ts || !input_dev) {
582 dev_set_drvdata(&spi->dev, ts);
583 spi->dev.power.power_state = PMSG_ON;
586 ts->input = input_dev;
588 init_timer(&ts->timer);
589 ts->timer.data = (unsigned long) ts;
590 ts->timer.function = ads7846_timer;
592 spin_lock_init(&ts->lock);
594 ts->model = pdata->model ? : 7846;
595 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
596 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
597 ts->debounce_max = pdata->debounce_max ? : 1;
598 ts->debounce_tol = pdata->debounce_tol ? : 10;
600 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
602 input_dev->name = "ADS784x Touchscreen";
603 input_dev->phys = ts->phys;
604 input_dev->cdev.dev = &spi->dev;
606 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
607 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
608 input_set_abs_params(input_dev, ABS_X,
610 pdata->x_max ? : MAX_12BIT,
612 input_set_abs_params(input_dev, ABS_Y,
614 pdata->y_max ? : MAX_12BIT,
616 input_set_abs_params(input_dev, ABS_PRESSURE,
617 pdata->pressure_min, pdata->pressure_max, 0, 0);
619 /* set up the transfers to read touchscreen state; this assumes we
620 * use formula #2 for pressure, not #3.
627 /* y- still on; turn on only y+ (and ADC) */
629 x->tx_buf = &ts->read_y;
631 spi_message_add_tail(x, m);
634 x->rx_buf = &ts->tc.y;
636 spi_message_add_tail(x, m);
638 m->complete = ads7846_debounce;
644 /* turn y- off, x+ on, then leave in lowpower */
647 x->tx_buf = &ts->read_x;
649 spi_message_add_tail(x, m);
652 x->rx_buf = &ts->tc.x;
654 spi_message_add_tail(x, m);
656 m->complete = ads7846_debounce;
659 /* turn y+ off, x- on; we'll use formula #2 */
660 if (ts->model == 7846) {
665 ts->read_z1 = READ_Z1;
666 x->tx_buf = &ts->read_z1;
668 spi_message_add_tail(x, m);
671 x->rx_buf = &ts->tc.z1;
673 spi_message_add_tail(x, m);
675 m->complete = ads7846_debounce;
682 ts->read_z2 = READ_Z2;
683 x->tx_buf = &ts->read_z2;
685 spi_message_add_tail(x, m);
688 x->rx_buf = &ts->tc.z2;
690 spi_message_add_tail(x, m);
692 m->complete = ads7846_debounce;
701 ts->pwrdown = PWRDOWN;
702 x->tx_buf = &ts->pwrdown;
704 spi_message_add_tail(x, m);
707 x->rx_buf = &ts->dummy;
710 spi_message_add_tail(x, m);
712 m->complete = ads7846_rx;
715 if (request_irq(spi->irq, ads7846_irq,
716 SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
717 spi->dev.bus_id, ts)) {
718 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
723 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
725 /* take a first sample, leaving nPENIRQ active; avoid
726 * the touchscreen, in case it's not connected.
728 (void) ads7846_read12_ser(&spi->dev,
729 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
731 /* ads7843/7845 don't have temperature sensors, and
732 * use the other sensors a bit differently too
734 if (ts->model == 7846) {
735 device_create_file(&spi->dev, &dev_attr_temp0);
736 device_create_file(&spi->dev, &dev_attr_temp1);
738 if (ts->model != 7845)
739 device_create_file(&spi->dev, &dev_attr_vbatt);
740 device_create_file(&spi->dev, &dev_attr_vaux);
742 device_create_file(&spi->dev, &dev_attr_pen_down);
744 device_create_file(&spi->dev, &dev_attr_disable);
746 err = input_register_device(input_dev);
748 goto err_remove_attr;
753 device_remove_file(&spi->dev, &dev_attr_disable);
754 device_remove_file(&spi->dev, &dev_attr_pen_down);
755 if (ts->model == 7846) {
756 device_remove_file(&spi->dev, &dev_attr_temp1);
757 device_remove_file(&spi->dev, &dev_attr_temp0);
759 if (ts->model != 7845)
760 device_remove_file(&spi->dev, &dev_attr_vbatt);
761 device_remove_file(&spi->dev, &dev_attr_vaux);
763 free_irq(spi->irq, ts);
765 input_free_device(input_dev);
770 static int __devexit ads7846_remove(struct spi_device *spi)
772 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
774 input_unregister_device(ts->input);
776 ads7846_suspend(spi, PMSG_SUSPEND);
778 device_remove_file(&spi->dev, &dev_attr_disable);
779 device_remove_file(&spi->dev, &dev_attr_pen_down);
780 if (ts->model == 7846) {
781 device_remove_file(&spi->dev, &dev_attr_temp1);
782 device_remove_file(&spi->dev, &dev_attr_temp0);
784 if (ts->model != 7845)
785 device_remove_file(&spi->dev, &dev_attr_vbatt);
786 device_remove_file(&spi->dev, &dev_attr_vaux);
788 free_irq(ts->spi->irq, ts);
789 if (ts->irq_disabled)
790 enable_irq(ts->spi->irq);
794 dev_dbg(&spi->dev, "unregistered touchscreen\n");
798 static struct spi_driver ads7846_driver = {
801 .bus = &spi_bus_type,
802 .owner = THIS_MODULE,
804 .probe = ads7846_probe,
805 .remove = __devexit_p(ads7846_remove),
806 .suspend = ads7846_suspend,
807 .resume = ads7846_resume,
810 static int __init ads7846_init(void)
812 /* grr, board-specific init should stay out of drivers!! */
814 #ifdef CONFIG_ARCH_OMAP
815 if (machine_is_omap_osk()) {
816 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
817 omap_request_gpio(4);
818 omap_set_gpio_direction(4, 1);
819 omap_request_gpio(6);
820 omap_set_gpio_direction(6, 1);
822 // also TI 1510 Innovator, bitbanging through FPGA
824 // also Palm Tungsten T2
828 // also Dell Axim X50
829 // also HP iPaq H191x/H192x/H415x/H435x
830 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
831 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
833 // Atmel at91sam9261-EK uses ads7843
835 // also various AMD Au1x00 devel boards
837 return spi_register_driver(&ads7846_driver);
839 module_init(ads7846_init);
841 static void __exit ads7846_exit(void)
843 spi_unregister_driver(&ads7846_driver);
845 #ifdef CONFIG_ARCH_OMAP
846 if (machine_is_omap_osk()) {
853 module_exit(ads7846_exit);
855 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
856 MODULE_LICENSE("GPL");