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/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/gpio.h>
28 #include <linux/spi/spi.h>
29 #include <linux/spi/ads7846.h>
34 * This code has been heavily tested on a Nokia 770, and lightly
35 * tested on other ads7846 devices (OSK/Mistral, Lubbock).
36 * TSC2046 is just newer ads7846 silicon.
37 * Support for ads7843 tested on Atmel at91sam926x-EK.
38 * Support for ads7845 has only been stubbed in.
40 * IRQ handling needs a workaround because of a shortcoming in handling
41 * edge triggered IRQs on some platforms like the OMAP1/2. These
42 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
43 * have to maintain our own SW IRQ disabled status. This should be
44 * removed as soon as the affected platform's IRQ handling is fixed.
46 * app note sbaa036 talks in more detail about accurate sampling...
47 * that ought to help in situations like LCDs inducing noise (which
48 * can also be helped by using synch signals) and more generally.
49 * This driver tries to utilize the measures described in the app
50 * note. The strength of filtering can be set in the board-* specific
54 #define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
55 #define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
57 /* this driver doesn't aim at the peak continuous sample rate */
58 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
61 /* For portability, we can't read 12 bit values using SPI (which
62 * would make the controller deliver them as native byteorder u16
63 * with msbs zeroed). Instead, we read them as two 8-bit values,
64 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
73 struct input_dev *input;
76 struct spi_device *spi;
78 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
79 struct attribute_group *attr_group;
89 u8 read_x, read_y, read_z1, read_z2, pwrdown;
90 u16 dummy; /* for the pwrdown read */
93 struct spi_transfer xfer[18];
94 struct spi_message msg[5];
95 struct spi_message *last_msg;
105 u16 penirq_recheck_delay_usecs;
108 struct hrtimer timer;
109 unsigned pendown:1; /* P: lock */
110 unsigned pending:1; /* P: lock */
111 // FIXME remove "irq_disabled"
112 unsigned irq_disabled:1; /* P: lock */
114 unsigned is_suspended:1;
116 int (*filter)(void *data, int data_idx, int *val);
118 void (*filter_cleanup)(void *data);
119 int (*get_pendown_state)(void);
123 /* leave chip selected when we're done, for quicker re-select? */
125 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
127 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
130 /*--------------------------------------------------------------------------*/
132 /* The ADS7846 has touchscreen and other sensors.
133 * Earlier ads784x chips are somewhat compatible.
135 #define ADS_START (1 << 7)
136 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
137 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
138 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
139 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
140 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
141 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
142 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
143 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
144 #define ADS_8_BIT (1 << 3)
145 #define ADS_12_BIT (0 << 3)
146 #define ADS_SER (1 << 2) /* non-differential */
147 #define ADS_DFR (0 << 2) /* differential */
148 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
149 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
150 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
151 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
153 #define MAX_12BIT ((1<<12)-1)
155 /* leave ADC powered up (disables penirq) between differential samples */
156 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
157 | ADS_12_BIT | ADS_DFR | \
158 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
160 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
161 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
162 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
164 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
165 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
167 /* single-ended samples need to first power up reference voltage;
168 * we leave both ADC and VREF powered
170 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
171 | ADS_12_BIT | ADS_SER)
173 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
174 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
176 /*--------------------------------------------------------------------------*/
179 * Non-touchscreen sensors only use single-ended conversions.
180 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
181 * ads7846 lets that pin be unconnected, to use internal vREF.
190 struct spi_message msg;
191 struct spi_transfer xfer[6];
194 static void ads7846_enable(struct ads7846 *ts);
195 static void ads7846_disable(struct ads7846 *ts);
197 static int device_suspended(struct device *dev)
199 struct ads7846 *ts = dev_get_drvdata(dev);
200 return ts->is_suspended || ts->disabled;
203 static int ads7846_read12_ser(struct device *dev, unsigned command)
205 struct spi_device *spi = to_spi_device(dev);
206 struct ads7846 *ts = dev_get_drvdata(dev);
207 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
214 spi_message_init(&req->msg);
216 /* FIXME boards with ads7846 might use external vref instead ... */
217 use_internal = (ts->model == 7846);
219 /* maybe turn on internal vREF, and let it settle */
221 req->ref_on = REF_ON;
222 req->xfer[0].tx_buf = &req->ref_on;
223 req->xfer[0].len = 1;
224 spi_message_add_tail(&req->xfer[0], &req->msg);
226 req->xfer[1].rx_buf = &req->scratch;
227 req->xfer[1].len = 2;
229 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
230 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
231 spi_message_add_tail(&req->xfer[1], &req->msg);
235 req->command = (u8) command;
236 req->xfer[2].tx_buf = &req->command;
237 req->xfer[2].len = 1;
238 spi_message_add_tail(&req->xfer[2], &req->msg);
240 req->xfer[3].rx_buf = &req->sample;
241 req->xfer[3].len = 2;
242 spi_message_add_tail(&req->xfer[3], &req->msg);
244 /* REVISIT: take a few more samples, and compare ... */
246 /* converter in low power mode & enable PENIRQ */
247 req->ref_off = PWRDOWN;
248 req->xfer[4].tx_buf = &req->ref_off;
249 req->xfer[4].len = 1;
250 spi_message_add_tail(&req->xfer[4], &req->msg);
252 req->xfer[5].rx_buf = &req->scratch;
253 req->xfer[5].len = 2;
254 CS_CHANGE(req->xfer[5]);
255 spi_message_add_tail(&req->xfer[5], &req->msg);
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);
264 /* on-wire is a must-ignore bit, a BE12 value, then padding */
265 status = be16_to_cpu(req->sample);
266 status = status >> 3;
274 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
276 #define SHOW(name, var, adjust) static ssize_t \
277 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
279 struct ads7846 *ts = dev_get_drvdata(dev); \
280 ssize_t v = ads7846_read12_ser(dev, \
281 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
284 return sprintf(buf, "%u\n", adjust(ts, v)); \
286 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
289 /* Sysfs conventions report temperatures in millidegrees Celcius.
290 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
291 * accuracy scheme without calibration data. For now we won't try either;
292 * userspace sees raw sensor values, and must scale/calibrate appropriately.
294 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
299 SHOW(temp0, temp0, null_adjust) /* temp1_input */
300 SHOW(temp1, temp1, null_adjust) /* temp2_input */
303 /* sysfs conventions report voltages in millivolts. We can convert voltages
304 * if we know vREF. userspace may need to scale vAUX to match the board's
305 * external resistors; we assume that vBATT only uses the internal ones.
307 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
311 /* external resistors may scale vAUX into 0..vREF */
312 retval *= ts->vref_mv;
313 retval = retval >> 12;
317 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
319 unsigned retval = vaux_adjust(ts, v);
321 /* ads7846 has a resistor ladder to scale this signal down */
322 if (ts->model == 7846)
327 SHOW(in0_input, vaux, vaux_adjust)
328 SHOW(in1_input, vbatt, vbatt_adjust)
331 static struct attribute *ads7846_attributes[] = {
332 &dev_attr_temp0.attr,
333 &dev_attr_temp1.attr,
334 &dev_attr_in0_input.attr,
335 &dev_attr_in1_input.attr,
339 static struct attribute_group ads7846_attr_group = {
340 .attrs = ads7846_attributes,
343 static struct attribute *ads7843_attributes[] = {
344 &dev_attr_in0_input.attr,
345 &dev_attr_in1_input.attr,
349 static struct attribute_group ads7843_attr_group = {
350 .attrs = ads7843_attributes,
353 static struct attribute *ads7845_attributes[] = {
354 &dev_attr_in0_input.attr,
358 static struct attribute_group ads7845_attr_group = {
359 .attrs = ads7845_attributes,
362 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
364 struct device *hwmon;
367 /* hwmon sensors need a reference voltage */
371 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
379 "external vREF for ADS%d not specified\n",
386 /* different chips have different sensor groups */
389 ts->attr_group = &ads7846_attr_group;
392 ts->attr_group = &ads7845_attr_group;
395 ts->attr_group = &ads7843_attr_group;
398 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
402 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
406 hwmon = hwmon_device_register(&spi->dev);
408 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
409 return PTR_ERR(hwmon);
416 static void ads784x_hwmon_unregister(struct spi_device *spi,
420 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
421 hwmon_device_unregister(ts->hwmon);
426 static inline int ads784x_hwmon_register(struct spi_device *spi,
432 static inline void ads784x_hwmon_unregister(struct spi_device *spi,
438 static int is_pen_down(struct device *dev)
440 struct ads7846 *ts = dev_get_drvdata(dev);
445 static ssize_t ads7846_pen_down_show(struct device *dev,
446 struct device_attribute *attr, char *buf)
448 return sprintf(buf, "%u\n", is_pen_down(dev));
451 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
453 static ssize_t ads7846_disable_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
456 struct ads7846 *ts = dev_get_drvdata(dev);
458 return sprintf(buf, "%u\n", ts->disabled);
461 static ssize_t ads7846_disable_store(struct device *dev,
462 struct device_attribute *attr,
463 const char *buf, size_t count)
465 struct ads7846 *ts = dev_get_drvdata(dev);
469 i = simple_strtoul(buf, &endp, 10);
470 spin_lock_irq(&ts->lock);
477 spin_unlock_irq(&ts->lock);
482 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
484 static struct attribute *ads784x_attributes[] = {
485 &dev_attr_pen_down.attr,
486 &dev_attr_disable.attr,
490 static struct attribute_group ads784x_attr_group = {
491 .attrs = ads784x_attributes,
494 /*--------------------------------------------------------------------------*/
496 static int get_pendown_state(struct ads7846 *ts)
498 if (ts->get_pendown_state)
499 return ts->get_pendown_state();
501 return !gpio_get_value(ts->gpio_pendown);
505 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
506 * to retrieve touchscreen status.
508 * The SPI transfer completion callback does the real work. It reports
509 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
512 static void ads7846_rx(void *ads)
514 struct ads7846 *ts = ads;
518 /* ads7846_rx_val() did in-place conversion (including byteswap) from
519 * on-the-wire format as part of debouncing to get stable readings.
526 /* range filtering */
530 if (ts->model == 7843) {
531 Rt = ts->pressure_max / 2;
532 } else if (likely(x && z1)) {
533 /* compute touch pressure resistance using equation #2 */
537 Rt *= ts->x_plate_ohms;
539 Rt = (Rt + 2047) >> 12;
544 /* Sample found inconsistent by debouncing or pressure is beyond
545 * the maximum. Don't report it to user space, repeat at least
546 * once more the measurement
548 if (ts->tc.ignore || Rt > ts->pressure_max) {
550 pr_debug("%s: ignored %d pressure %d\n",
551 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
553 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
558 /* Maybe check the pendown state before reporting. This discards
559 * false readings when the pen is lifted.
561 if (ts->penirq_recheck_delay_usecs) {
562 udelay(ts->penirq_recheck_delay_usecs);
563 if (!get_pendown_state(ts))
567 /* NOTE: We can't rely on the pressure to determine the pen down
568 * state, even this controller has a pressure sensor. The pressure
569 * value can fluctuate for quite a while after lifting the pen and
570 * in some cases may not even settle at the expected value.
572 * The only safe way to check for the pen up condition is in the
573 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
576 struct input_dev *input = ts->input;
579 input_report_key(input, BTN_TOUCH, 1);
582 dev_dbg(&ts->spi->dev, "DOWN\n");
585 input_report_abs(input, ABS_X, x);
586 input_report_abs(input, ABS_Y, y);
587 input_report_abs(input, ABS_PRESSURE, Rt);
591 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
595 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
599 static int ads7846_debounce(void *ads, int data_idx, int *val)
601 struct ads7846 *ts = ads;
603 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
604 /* Start over collecting consistent readings. */
606 /* Repeat it, if this was the first read or the read
607 * wasn't consistent enough. */
608 if (ts->read_cnt < ts->debounce_max) {
609 ts->last_read = *val;
611 return ADS7846_FILTER_REPEAT;
613 /* Maximum number of debouncing reached and still
614 * not enough number of consistent readings. Abort
615 * the whole sample, repeat it in the next sampling
619 return ADS7846_FILTER_IGNORE;
622 if (++ts->read_rep > ts->debounce_rep) {
623 /* Got a good reading for this coordinate,
624 * go for the next one. */
627 return ADS7846_FILTER_OK;
629 /* Read more values that are consistent. */
631 return ADS7846_FILTER_REPEAT;
636 static int ads7846_no_filter(void *ads, int data_idx, int *val)
638 return ADS7846_FILTER_OK;
641 static void ads7846_rx_val(void *ads)
643 struct ads7846 *ts = ads;
644 struct spi_message *m;
645 struct spi_transfer *t;
650 m = &ts->msg[ts->msg_idx];
651 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
653 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
654 * built from two 8 bit values written msb-first.
656 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
658 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
660 case ADS7846_FILTER_REPEAT:
662 case ADS7846_FILTER_IGNORE:
664 /* Last message will contain ads7846_rx() as the
665 * completion function.
669 case ADS7846_FILTER_OK:
670 *(u16 *)t->rx_buf = val;
672 m = &ts->msg[++ts->msg_idx];
677 status = spi_async(ts->spi, m);
679 dev_err(&ts->spi->dev, "spi_async --> %d\n",
683 static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
685 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
688 spin_lock_irq(&ts->lock);
690 if (unlikely(!get_pendown_state(ts) ||
691 device_suspended(&ts->spi->dev))) {
693 struct input_dev *input = ts->input;
695 input_report_key(input, BTN_TOUCH, 0);
696 input_report_abs(input, ABS_PRESSURE, 0);
701 dev_dbg(&ts->spi->dev, "UP\n");
705 /* measurement cycle ended */
706 if (!device_suspended(&ts->spi->dev)) {
707 ts->irq_disabled = 0;
708 enable_irq(ts->spi->irq);
712 /* pen is still down, continue with the measurement */
714 status = spi_async(ts->spi, &ts->msg[0]);
716 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
719 spin_unlock_irq(&ts->lock);
720 return HRTIMER_NORESTART;
723 static irqreturn_t ads7846_irq(int irq, void *handle)
725 struct ads7846 *ts = handle;
728 spin_lock_irqsave(&ts->lock, flags);
729 if (likely(get_pendown_state(ts))) {
730 if (!ts->irq_disabled) {
731 /* The ARM do_simple_IRQ() dispatcher doesn't act
732 * like the other dispatchers: it will report IRQs
733 * even after they've been disabled. We work around
734 * that here. (The "generic irq" framework may help...)
736 ts->irq_disabled = 1;
737 disable_irq(ts->spi->irq);
739 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
743 spin_unlock_irqrestore(&ts->lock, flags);
748 /*--------------------------------------------------------------------------*/
750 /* Must be called with ts->lock held */
751 static void ads7846_disable(struct ads7846 *ts)
758 /* are we waiting for IRQ, or polling? */
760 ts->irq_disabled = 1;
761 disable_irq(ts->spi->irq);
763 /* the timer will run at least once more, and
764 * leave everything in a clean state, IRQ disabled
766 while (ts->pending) {
767 spin_unlock_irq(&ts->lock);
769 spin_lock_irq(&ts->lock);
773 /* we know the chip's in lowpower mode since we always
774 * leave it that way after every request
779 /* Must be called with ts->lock held */
780 static void ads7846_enable(struct ads7846 *ts)
786 ts->irq_disabled = 0;
787 enable_irq(ts->spi->irq);
790 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
792 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
794 spin_lock_irq(&ts->lock);
796 ts->is_suspended = 1;
799 spin_unlock_irq(&ts->lock);
805 static int ads7846_resume(struct spi_device *spi)
807 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
809 spin_lock_irq(&ts->lock);
811 ts->is_suspended = 0;
814 spin_unlock_irq(&ts->lock);
819 static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts)
821 struct ads7846_platform_data *pdata = spi->dev.platform_data;
824 /* REVISIT when the irq can be triggered active-low, or if for some
825 * reason the touchscreen isn't hooked up, we don't need to access
828 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
829 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
833 if (pdata->get_pendown_state) {
834 ts->get_pendown_state = pdata->get_pendown_state;
838 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
840 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
841 pdata->gpio_pendown);
845 ts->gpio_pendown = pdata->gpio_pendown;
849 static int __devinit ads7846_probe(struct spi_device *spi)
852 struct input_dev *input_dev;
853 struct ads7846_platform_data *pdata = spi->dev.platform_data;
854 struct spi_message *m;
855 struct spi_transfer *x;
860 dev_dbg(&spi->dev, "no IRQ?\n");
865 dev_dbg(&spi->dev, "no platform data?\n");
869 /* don't exceed max specified sample rate */
870 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
871 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
872 (spi->max_speed_hz/SAMPLE_BITS)/1000);
876 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
877 * that even if the hardware can do that, the SPI controller driver
878 * may not. So we stick to very-portable 8 bit words, both RX and TX.
880 spi->bits_per_word = 8;
881 spi->mode = SPI_MODE_0;
882 err = spi_setup(spi);
886 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
887 input_dev = input_allocate_device();
888 if (!ts || !input_dev) {
893 dev_set_drvdata(&spi->dev, ts);
896 ts->input = input_dev;
897 ts->vref_mv = pdata->vref_mv;
899 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
900 ts->timer.function = ads7846_timer;
902 spin_lock_init(&ts->lock);
904 ts->model = pdata->model ? : 7846;
905 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
906 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
907 ts->pressure_max = pdata->pressure_max ? : ~0;
909 if (pdata->filter != NULL) {
910 if (pdata->filter_init != NULL) {
911 err = pdata->filter_init(pdata, &ts->filter_data);
915 ts->filter = pdata->filter;
916 ts->filter_cleanup = pdata->filter_cleanup;
917 } else if (pdata->debounce_max) {
918 ts->debounce_max = pdata->debounce_max;
919 if (ts->debounce_max < 2)
920 ts->debounce_max = 2;
921 ts->debounce_tol = pdata->debounce_tol;
922 ts->debounce_rep = pdata->debounce_rep;
923 ts->filter = ads7846_debounce;
924 ts->filter_data = ts;
926 ts->filter = ads7846_no_filter;
928 err = setup_pendown(spi, ts);
930 goto err_cleanup_filter;
932 if (pdata->penirq_recheck_delay_usecs)
933 ts->penirq_recheck_delay_usecs =
934 pdata->penirq_recheck_delay_usecs;
936 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
938 input_dev->name = "ADS784x Touchscreen";
939 input_dev->phys = ts->phys;
940 input_dev->dev.parent = &spi->dev;
942 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
943 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
944 input_set_abs_params(input_dev, ABS_X,
946 pdata->x_max ? : MAX_12BIT,
948 input_set_abs_params(input_dev, ABS_Y,
950 pdata->y_max ? : MAX_12BIT,
952 input_set_abs_params(input_dev, ABS_PRESSURE,
953 pdata->pressure_min, pdata->pressure_max, 0, 0);
955 vref = pdata->keep_vref_on;
957 /* set up the transfers to read touchscreen state; this assumes we
958 * use formula #2 for pressure, not #3.
965 /* y- still on; turn on only y+ (and ADC) */
966 ts->read_y = READ_Y(vref);
967 x->tx_buf = &ts->read_y;
969 spi_message_add_tail(x, m);
972 x->rx_buf = &ts->tc.y;
974 spi_message_add_tail(x, m);
976 /* the first sample after switching drivers can be low quality;
977 * optionally discard it, using a second one after the signals
978 * have had enough time to stabilize.
980 if (pdata->settle_delay_usecs) {
981 x->delay_usecs = pdata->settle_delay_usecs;
984 x->tx_buf = &ts->read_y;
986 spi_message_add_tail(x, m);
989 x->rx_buf = &ts->tc.y;
991 spi_message_add_tail(x, m);
994 m->complete = ads7846_rx_val;
1000 /* turn y- off, x+ on, then leave in lowpower */
1002 ts->read_x = READ_X(vref);
1003 x->tx_buf = &ts->read_x;
1005 spi_message_add_tail(x, m);
1008 x->rx_buf = &ts->tc.x;
1010 spi_message_add_tail(x, m);
1012 /* ... maybe discard first sample ... */
1013 if (pdata->settle_delay_usecs) {
1014 x->delay_usecs = pdata->settle_delay_usecs;
1017 x->tx_buf = &ts->read_x;
1019 spi_message_add_tail(x, m);
1022 x->rx_buf = &ts->tc.x;
1024 spi_message_add_tail(x, m);
1027 m->complete = ads7846_rx_val;
1030 /* turn y+ off, x- on; we'll use formula #2 */
1031 if (ts->model == 7846) {
1033 spi_message_init(m);
1036 ts->read_z1 = READ_Z1(vref);
1037 x->tx_buf = &ts->read_z1;
1039 spi_message_add_tail(x, m);
1042 x->rx_buf = &ts->tc.z1;
1044 spi_message_add_tail(x, m);
1046 /* ... maybe discard first sample ... */
1047 if (pdata->settle_delay_usecs) {
1048 x->delay_usecs = pdata->settle_delay_usecs;
1051 x->tx_buf = &ts->read_z1;
1053 spi_message_add_tail(x, m);
1056 x->rx_buf = &ts->tc.z1;
1058 spi_message_add_tail(x, m);
1061 m->complete = ads7846_rx_val;
1065 spi_message_init(m);
1068 ts->read_z2 = READ_Z2(vref);
1069 x->tx_buf = &ts->read_z2;
1071 spi_message_add_tail(x, m);
1074 x->rx_buf = &ts->tc.z2;
1076 spi_message_add_tail(x, m);
1078 /* ... maybe discard first sample ... */
1079 if (pdata->settle_delay_usecs) {
1080 x->delay_usecs = pdata->settle_delay_usecs;
1083 x->tx_buf = &ts->read_z2;
1085 spi_message_add_tail(x, m);
1088 x->rx_buf = &ts->tc.z2;
1090 spi_message_add_tail(x, m);
1093 m->complete = ads7846_rx_val;
1099 spi_message_init(m);
1102 ts->pwrdown = PWRDOWN;
1103 x->tx_buf = &ts->pwrdown;
1105 spi_message_add_tail(x, m);
1108 x->rx_buf = &ts->dummy;
1111 spi_message_add_tail(x, m);
1113 m->complete = ads7846_rx;
1118 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
1119 spi->dev.driver->name, ts)) {
1120 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1125 err = ads784x_hwmon_register(spi, ts);
1129 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1131 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1132 * the touchscreen, in case it's not connected.
1134 (void) ads7846_read12_ser(&spi->dev,
1135 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1137 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1139 goto err_remove_hwmon;
1141 err = input_register_device(input_dev);
1143 goto err_remove_attr_group;
1147 err_remove_attr_group:
1148 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1150 ads784x_hwmon_unregister(spi, ts);
1152 free_irq(spi->irq, ts);
1154 if (ts->gpio_pendown != -1)
1155 gpio_free(ts->gpio_pendown);
1157 if (ts->filter_cleanup)
1158 ts->filter_cleanup(ts->filter_data);
1160 input_free_device(input_dev);
1165 static int __devexit ads7846_remove(struct spi_device *spi)
1167 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
1169 ads784x_hwmon_unregister(spi, ts);
1170 input_unregister_device(ts->input);
1172 ads7846_suspend(spi, PMSG_SUSPEND);
1174 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1176 free_irq(ts->spi->irq, ts);
1177 /* suspend left the IRQ disabled */
1178 enable_irq(ts->spi->irq);
1180 if (ts->gpio_pendown != -1)
1181 gpio_free(ts->gpio_pendown);
1183 if (ts->filter_cleanup)
1184 ts->filter_cleanup(ts->filter_data);
1188 dev_dbg(&spi->dev, "unregistered touchscreen\n");
1192 static struct spi_driver ads7846_driver = {
1195 .bus = &spi_bus_type,
1196 .owner = THIS_MODULE,
1198 .probe = ads7846_probe,
1199 .remove = __devexit_p(ads7846_remove),
1200 .suspend = ads7846_suspend,
1201 .resume = ads7846_resume,
1204 static int __init ads7846_init(void)
1206 return spi_register_driver(&ads7846_driver);
1208 module_init(ads7846_init);
1210 static void __exit ads7846_exit(void)
1212 spi_unregister_driver(&ads7846_driver);
1214 module_exit(ads7846_exit);
1216 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1217 MODULE_LICENSE("GPL");