2 * ADS7846 based touchscreen and sensor driver
4 * Copyright (c) 2005 David Brownell
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
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.
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>
28 #include <asm/mach-types.h>
29 #ifdef CONFIG_ARCH_OMAP
30 #include <asm/arch/gpio.h>
36 * This code has been lightly tested on an ads7846.
37 * Support for ads7843 and ads7845 has only been stubbed in.
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.)
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.
49 #define TS_POLL_PERIOD msecs_to_jiffies(10)
51 /* this driver doesn't aim at the peak continuous sample rate */
52 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
55 /* For portability, we can't read 12 bit values using SPI (which
56 * would make the controller deliver them as native byteorder u16
57 * with msbs zeroed). Instead, we read them as two 8-bit values,
58 * which need byteswapping then range adjustment.
66 struct input_dev *input;
69 struct spi_device *spi;
74 u8 read_x, read_y, read_z1, read_z2, pwrdown;
75 u16 dummy; /* for the pwrdown read */
78 struct spi_transfer xfer[10];
79 struct spi_message msg[5];
88 struct timer_list timer; /* P: lock */
89 unsigned pendown:1; /* P: lock */
90 unsigned pending:1; /* P: lock */
91 // FIXME remove "irq_disabled"
92 unsigned irq_disabled:1; /* P: lock */
95 /* leave chip selected when we're done, for quicker re-select? */
97 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
99 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
102 /*--------------------------------------------------------------------------*/
104 /* The ADS7846 has touchscreen and other sensors.
105 * Earlier ads784x chips are somewhat compatible.
107 #define ADS_START (1 << 7)
108 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
109 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
110 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
111 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
112 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
113 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
114 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
115 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
116 #define ADS_8_BIT (1 << 3)
117 #define ADS_12_BIT (0 << 3)
118 #define ADS_SER (1 << 2) /* non-differential */
119 #define ADS_DFR (0 << 2) /* differential */
120 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
121 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
122 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
123 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
125 #define MAX_12BIT ((1<<12)-1)
127 /* leave ADC powered up (disables penirq) between differential samples */
128 #define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
129 | ADS_12_BIT | ADS_DFR)
131 #define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON)
132 #define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
133 #define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
135 #define READ_X (READ_12BIT_DFR(x) | ADS_PD10_ADC_ON)
136 #define PWRDOWN (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) /* LAST */
138 /* single-ended samples need to first power up reference voltage;
139 * we leave both ADC and VREF powered
141 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
142 | ADS_12_BIT | ADS_SER)
144 #define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
145 #define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
147 /*--------------------------------------------------------------------------*/
150 * Non-touchscreen sensors only use single-ended conversions.
159 struct spi_message msg;
160 struct spi_transfer xfer[6];
163 static int ads7846_read12_ser(struct device *dev, unsigned command)
165 struct spi_device *spi = to_spi_device(dev);
166 struct ads7846 *ts = dev_get_drvdata(dev);
167 struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL);
175 spi_message_init(&req->msg);
177 /* activate reference, so it has time to settle; */
178 req->ref_on = REF_ON;
179 req->xfer[0].tx_buf = &req->ref_on;
180 req->xfer[0].len = 1;
181 req->xfer[1].rx_buf = &req->scratch;
182 req->xfer[1].len = 2;
185 * for external VREF, 0 usec (and assume it's always on);
186 * for 1uF, use 800 usec;
189 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
192 req->command = (u8) command;
193 req->xfer[2].tx_buf = &req->command;
194 req->xfer[2].len = 1;
195 req->xfer[3].rx_buf = &req->sample;
196 req->xfer[3].len = 2;
198 /* REVISIT: take a few more samples, and compare ... */
200 /* turn off reference */
201 req->ref_off = REF_OFF;
202 req->xfer[4].tx_buf = &req->ref_off;
203 req->xfer[4].len = 1;
204 req->xfer[5].rx_buf = &req->scratch;
205 req->xfer[5].len = 2;
207 CS_CHANGE(req->xfer[5]);
209 /* group all the transfers together, so we can't interfere with
210 * reading touchscreen state; disable penirq while sampling
212 for (i = 0; i < 6; i++)
213 spi_message_add_tail(&req->xfer[i], &req->msg);
215 disable_irq(spi->irq);
216 status = spi_sync(spi, &req->msg);
217 enable_irq(spi->irq);
220 status = req->msg.status;
221 sample = be16_to_cpu(req->sample);
222 sample = sample >> 4;
225 return status ? status : sample;
228 #define SHOW(name) static ssize_t \
229 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
231 ssize_t v = ads7846_read12_ser(dev, \
232 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
235 return sprintf(buf, "%u\n", (unsigned) v); \
237 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
244 static int is_pen_down(struct device *dev)
246 struct ads7846 *ts = dev_get_drvdata(dev);
251 static ssize_t ads7846_pen_down_show(struct device *dev,
252 struct device_attribute *attr, char *buf)
254 return sprintf(buf, "%u\n", is_pen_down(dev));
257 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
259 /*--------------------------------------------------------------------------*/
262 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
263 * to retrieve touchscreen status.
265 * The SPI transfer completion callback does the real work. It reports
266 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
269 static void ads7846_rx(void *ads)
271 struct ads7846 *ts = ads;
272 struct input_dev *input_dev = ts->input;
278 /* adjust: 12 bit samples (left aligned), built from
279 * two 8 bit values writen msb-first.
286 /* range filtering */
290 if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
291 /* compute touch pressure resistance using equation #2 */
295 Rt *= ts->x_plate_ohms;
297 Rt = (Rt + 2047) >> 12;
301 /* NOTE: "pendown" is inferred from pressure; we don't rely on
302 * being able to check nPENIRQ status, or "friendly" trigger modes
303 * (both-edges is much better than just-falling or low-level).
305 * REVISIT: some boards may require reading nPENIRQ; it's
306 * needed on 7843. and 7845 reads pressure differently...
308 * REVISIT: the touchscreen might not be connected; this code
309 * won't notice that, even if nPENIRQ never fires ...
311 if (!ts->pendown && Rt != 0) {
312 input_report_key(input_dev, BTN_TOUCH, 1);
314 } else if (ts->pendown && Rt == 0) {
315 input_report_key(input_dev, BTN_TOUCH, 0);
320 input_report_abs(input_dev, ABS_X, x);
321 input_report_abs(input_dev, ABS_Y, y);
322 input_report_abs(input_dev, ABS_PRESSURE, Rt);
326 input_sync(input_dev);
329 if (Rt || ts->pendown)
330 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
331 x, y, Rt, Rt ? "" : " UP");
334 /* don't retrigger while we're suspended */
335 spin_lock_irqsave(&ts->lock, flags);
337 ts->pendown = (Rt != 0);
340 if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
342 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
343 else if (ts->irq_disabled) {
344 ts->irq_disabled = 0;
345 enable_irq(ts->spi->irq);
349 spin_unlock_irqrestore(&ts->lock, flags);
352 static void ads7846_debounce(void *ads)
354 struct ads7846 *ts = ads;
355 struct spi_message *m;
356 struct spi_transfer *t;
360 m = &ts->msg[ts->msg_idx];
361 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
362 val = (*(u16 *)t->rx_buf) >> 3;
364 if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol &&
365 ts->read_cnt < ts->debounce_max)) {
366 /* Repeat it, if this was the first read or the read wasn't
367 * consistent enough */
371 /* Go for the next read */
376 status = spi_async(ts->spi, m);
378 dev_err(&ts->spi->dev, "spi_async --> %d\n",
382 static void ads7846_timer(unsigned long handle)
384 struct ads7846 *ts = (void *)handle;
388 status = spi_async(ts->spi, &ts->msg[0]);
390 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
393 static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
395 struct ads7846 *ts = handle;
399 spin_lock_irqsave(&ts->lock, flags);
400 if (ts->irq_disabled)
403 if (!ts->irq_disabled) {
404 ts->irq_disabled = 1;
405 /* The following has at the moment no effect whatsoever
406 * on OMAP, that's why we maintain the disabled
408 disable_irq(ts->spi->irq);
412 mod_timer(&ts->timer, jiffies);
415 spin_unlock_irqrestore(&ts->lock, flags);
419 /*--------------------------------------------------------------------------*/
422 ads7846_suspend(struct spi_device *spi, pm_message_t message)
424 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
427 spin_lock_irqsave(&ts->lock, flags);
429 spi->dev.power.power_state = message;
431 /* are we waiting for IRQ, or polling? */
433 if (!ts->irq_disabled) {
434 ts->irq_disabled = 1;
435 disable_irq(ts->spi->irq);
438 /* polling; force a final SPI completion;
439 * that will clean things up neatly
442 mod_timer(&ts->timer, jiffies);
444 while (ts->pendown || ts->pending) {
445 spin_unlock_irqrestore(&ts->lock, flags);
447 spin_lock_irqsave(&ts->lock, flags);
451 /* we know the chip's in lowpower mode since we always
452 * leave it that way after every request
455 spin_unlock_irqrestore(&ts->lock, flags);
459 static int ads7846_resume(struct spi_device *spi)
461 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
463 ts->irq_disabled = 0;
464 enable_irq(ts->spi->irq);
465 spi->dev.power.power_state = PMSG_ON;
469 static int __devinit ads7846_probe(struct spi_device *spi)
472 struct input_dev *input_dev;
473 struct ads7846_platform_data *pdata = spi->dev.platform_data;
474 struct spi_message *m;
475 struct spi_transfer *x;
479 dev_dbg(&spi->dev, "no IRQ?\n");
484 dev_dbg(&spi->dev, "no platform data?\n");
488 /* don't exceed max specified sample rate */
489 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
490 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
491 (spi->max_speed_hz/SAMPLE_BITS)/1000);
495 /* We'd set the wordsize to 12 bits ... except that some controllers
496 * will then treat the 8 bit command words as 12 bits (and drop the
497 * four MSBs of the 12 bit result). Result: inputs must be shifted
498 * to discard the four garbage LSBs.
501 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
502 input_dev = input_allocate_device();
503 if (!ts || !input_dev) {
508 dev_set_drvdata(&spi->dev, ts);
509 spi->dev.power.power_state = PMSG_ON;
512 ts->input = input_dev;
514 init_timer(&ts->timer);
515 ts->timer.data = (unsigned long) ts;
516 ts->timer.function = ads7846_timer;
518 ts->model = pdata->model ? : 7846;
519 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
520 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
521 ts->debounce_max = pdata->debounce_max ? : 1;
522 ts->debounce_tol = pdata->debounce_tol ? : 10;
524 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
526 input_dev->name = "ADS784x Touchscreen";
527 input_dev->phys = ts->phys;
528 input_dev->cdev.dev = &spi->dev;
530 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
531 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
532 input_set_abs_params(input_dev, ABS_X,
534 pdata->x_max ? : MAX_12BIT,
536 input_set_abs_params(input_dev, ABS_Y,
538 pdata->y_max ? : MAX_12BIT,
540 input_set_abs_params(input_dev, ABS_PRESSURE,
541 pdata->pressure_min, pdata->pressure_max, 0, 0);
543 /* set up the transfers to read touchscreen state; this assumes we
544 * use formula #2 for pressure, not #3.
551 /* y- still on; turn on only y+ (and ADC) */
553 x->tx_buf = &ts->read_y;
555 spi_message_add_tail(x, m);
558 x->rx_buf = &ts->tc.y;
560 spi_message_add_tail(x, m);
562 m->complete = ads7846_debounce;
568 /* turn y- off, x+ on, then leave in lowpower */
571 x->tx_buf = &ts->read_x;
573 spi_message_add_tail(x, m);
576 x->rx_buf = &ts->tc.x;
578 spi_message_add_tail(x, m);
580 m->complete = ads7846_debounce;
583 /* turn y+ off, x- on; we'll use formula #2 */
584 if (ts->model == 7846) {
589 ts->read_z1 = READ_Z1;
590 x->tx_buf = &ts->read_z1;
592 spi_message_add_tail(x, m);
595 x->rx_buf = &ts->tc.z1;
597 spi_message_add_tail(x, m);
599 m->complete = ads7846_debounce;
606 ts->read_z2 = READ_Z2;
607 x->tx_buf = &ts->read_z2;
609 spi_message_add_tail(x, m);
612 x->rx_buf = &ts->tc.z2;
614 spi_message_add_tail(x, m);
616 m->complete = ads7846_debounce;
625 ts->pwrdown = PWRDOWN;
626 x->tx_buf = &ts->pwrdown;
628 spi_message_add_tail(x, m);
631 x->rx_buf = &ts->dummy;
634 spi_message_add_tail(x, m);
636 m->complete = ads7846_rx;
639 if (request_irq(spi->irq, ads7846_irq,
640 SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
641 spi->dev.bus_id, ts)) {
642 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
647 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
649 /* take a first sample, leaving nPENIRQ active; avoid
650 * the touchscreen, in case it's not connected.
652 (void) ads7846_read12_ser(&spi->dev,
653 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
655 /* ads7843/7845 don't have temperature sensors, and
656 * use the other sensors a bit differently too
658 if (ts->model == 7846) {
659 device_create_file(&spi->dev, &dev_attr_temp0);
660 device_create_file(&spi->dev, &dev_attr_temp1);
662 if (ts->model != 7845)
663 device_create_file(&spi->dev, &dev_attr_vbatt);
664 device_create_file(&spi->dev, &dev_attr_vaux);
666 device_create_file(&spi->dev, &dev_attr_pen_down);
668 err = input_register_device(input_dev);
675 free_irq(spi->irq, ts);
677 input_free_device(input_dev);
682 static int __devexit ads7846_remove(struct spi_device *spi)
684 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
686 ads7846_suspend(spi, PMSG_SUSPEND);
687 free_irq(ts->spi->irq, ts);
688 if (ts->irq_disabled)
689 enable_irq(ts->spi->irq);
691 device_remove_file(&spi->dev, &dev_attr_pen_down);
693 if (ts->model == 7846) {
694 device_remove_file(&spi->dev, &dev_attr_temp0);
695 device_remove_file(&spi->dev, &dev_attr_temp1);
697 if (ts->model != 7845)
698 device_remove_file(&spi->dev, &dev_attr_vbatt);
699 device_remove_file(&spi->dev, &dev_attr_vaux);
701 input_unregister_device(ts->input);
704 dev_dbg(&spi->dev, "unregistered touchscreen\n");
708 static struct spi_driver ads7846_driver = {
711 .bus = &spi_bus_type,
712 .owner = THIS_MODULE,
714 .probe = ads7846_probe,
715 .remove = __devexit_p(ads7846_remove),
716 .suspend = ads7846_suspend,
717 .resume = ads7846_resume,
720 static int __init ads7846_init(void)
722 /* grr, board-specific init should stay out of drivers!! */
724 #ifdef CONFIG_ARCH_OMAP
725 if (machine_is_omap_osk()) {
726 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
727 omap_request_gpio(4);
728 omap_set_gpio_direction(4, 1);
729 omap_request_gpio(6);
730 omap_set_gpio_direction(6, 1);
732 // also TI 1510 Innovator, bitbanging through FPGA
734 // also Palm Tungsten T2
738 // also Dell Axim X50
739 // also HP iPaq H191x/H192x/H415x/H435x
740 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
741 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
743 // Atmel at91sam9261-EK uses ads7843
745 // also various AMD Au1x00 devel boards
747 return spi_register_driver(&ads7846_driver);
749 module_init(ads7846_init);
751 static void __exit ads7846_exit(void)
753 spi_unregister_driver(&ads7846_driver);
755 #ifdef CONFIG_ARCH_OMAP
756 if (machine_is_omap_osk()) {
763 module_exit(ads7846_exit);
765 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
766 MODULE_LICENSE("GPL");