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/spi/spi.h>
28 #include <linux/spi/ads7846.h>
33 * This code has been heavily tested on a Nokia 770, and lightly
34 * tested on other ads7846 devices (OSK/Mistral, Lubbock).
35 * TSC2046 is just newer ads7846 silicon.
36 * Support for ads7843 tested on Atmel at91sam926x-EK.
37 * Support for ads7845 has only been stubbed in.
39 * IRQ handling needs a workaround because of a shortcoming in handling
40 * edge triggered IRQs on some platforms like the OMAP1/2. These
41 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
42 * have to maintain our own SW IRQ disabled status. This should be
43 * removed as soon as the affected platform's IRQ handling is fixed.
45 * app note sbaa036 talks in more detail about accurate sampling...
46 * that ought to help in situations like LCDs inducing noise (which
47 * can also be helped by using synch signals) and more generally.
48 * This driver tries to utilize the measures described in the app
49 * note. The strength of filtering can be set in the board-* specific
53 #define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
54 #define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
56 /* this driver doesn't aim at the peak continuous sample rate */
57 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
60 /* For portability, we can't read 12 bit values using SPI (which
61 * would make the controller deliver them as native byteorder u16
62 * with msbs zeroed). Instead, we read them as two 8-bit values,
63 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
72 struct input_dev *input;
75 struct spi_device *spi;
77 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
78 struct attribute_group *attr_group;
87 u8 read_x, read_y, read_z1, read_z2, pwrdown;
88 u16 dummy; /* for the pwrdown read */
91 struct spi_transfer xfer[18];
92 struct spi_message msg[5];
93 struct spi_message *last_msg;
103 u16 penirq_recheck_delay_usecs;
106 struct hrtimer timer;
107 unsigned pendown:1; /* P: lock */
108 unsigned pending:1; /* P: lock */
109 // FIXME remove "irq_disabled"
110 unsigned irq_disabled:1; /* P: lock */
112 unsigned is_suspended:1;
114 int (*filter)(void *data, int data_idx, int *val);
116 void (*filter_cleanup)(void *data);
117 int (*get_pendown_state)(void);
120 /* leave chip selected when we're done, for quicker re-select? */
122 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
124 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
127 /*--------------------------------------------------------------------------*/
129 /* The ADS7846 has touchscreen and other sensors.
130 * Earlier ads784x chips are somewhat compatible.
132 #define ADS_START (1 << 7)
133 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
134 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
135 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
136 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
137 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
138 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
139 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
140 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
141 #define ADS_8_BIT (1 << 3)
142 #define ADS_12_BIT (0 << 3)
143 #define ADS_SER (1 << 2) /* non-differential */
144 #define ADS_DFR (0 << 2) /* differential */
145 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
146 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
147 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
148 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
150 #define MAX_12BIT ((1<<12)-1)
152 /* leave ADC powered up (disables penirq) between differential samples */
153 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
154 | ADS_12_BIT | ADS_DFR | \
155 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
157 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
158 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
159 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
161 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
162 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
164 /* single-ended samples need to first power up reference voltage;
165 * we leave both ADC and VREF powered
167 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
168 | ADS_12_BIT | ADS_SER)
170 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
171 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
173 /*--------------------------------------------------------------------------*/
176 * Non-touchscreen sensors only use single-ended conversions.
177 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
178 * ads7846 lets that pin be unconnected, to use internal vREF.
180 static unsigned vREF_mV;
181 module_param(vREF_mV, uint, 0);
182 MODULE_PARM_DESC(vREF_mV, "external vREF voltage, in milliVolts");
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);
209 int uninitialized_var(sample);
215 spi_message_init(&req->msg);
217 /* FIXME boards with ads7846 might use external vref instead ... */
218 use_internal = (ts->model == 7846);
220 /* maybe turn on internal vREF, and let it settle */
222 req->ref_on = REF_ON;
223 req->xfer[0].tx_buf = &req->ref_on;
224 req->xfer[0].len = 1;
225 spi_message_add_tail(&req->xfer[0], &req->msg);
227 req->xfer[1].rx_buf = &req->scratch;
228 req->xfer[1].len = 2;
230 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
231 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
232 spi_message_add_tail(&req->xfer[1], &req->msg);
236 req->command = (u8) command;
237 req->xfer[2].tx_buf = &req->command;
238 req->xfer[2].len = 1;
239 spi_message_add_tail(&req->xfer[2], &req->msg);
241 req->xfer[3].rx_buf = &req->sample;
242 req->xfer[3].len = 2;
243 spi_message_add_tail(&req->xfer[3], &req->msg);
245 /* REVISIT: take a few more samples, and compare ... */
247 /* converter in low power mode & enable PENIRQ */
248 req->ref_off = PWRDOWN;
249 req->xfer[4].tx_buf = &req->ref_off;
250 req->xfer[4].len = 1;
251 spi_message_add_tail(&req->xfer[4], &req->msg);
253 req->xfer[5].rx_buf = &req->scratch;
254 req->xfer[5].len = 2;
255 CS_CHANGE(req->xfer[5]);
256 spi_message_add_tail(&req->xfer[5], &req->msg);
258 ts->irq_disabled = 1;
259 disable_irq(spi->irq);
260 status = spi_sync(spi, &req->msg);
261 ts->irq_disabled = 0;
262 enable_irq(spi->irq);
265 /* on-wire is a must-ignore bit, a BE12 value, then padding */
266 sample = be16_to_cpu(req->sample);
267 sample = sample >> 3;
272 return status ? status : sample;
275 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
277 #define SHOW(name, var, adjust) static ssize_t \
278 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
280 struct ads7846 *ts = dev_get_drvdata(dev); \
281 ssize_t v = ads7846_read12_ser(dev, \
282 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
285 return sprintf(buf, "%u\n", adjust(ts, v)); \
287 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
290 /* Sysfs conventions report temperatures in millidegrees Celcius.
291 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
292 * accuracy scheme without calibration data. For now we won't try either;
293 * userspace sees raw sensor values, and must scale/calibrate appropriately.
295 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
300 SHOW(temp0, temp0, null_adjust) /* temp1_input */
301 SHOW(temp1, temp1, null_adjust) /* temp2_input */
304 /* sysfs conventions report voltages in millivolts. We can convert voltages
305 * if we know vREF. userspace may need to scale vAUX to match the board's
306 * external resistors; we assume that vBATT only uses the internal ones.
308 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
312 /* external resistors may scale vAUX into 0..vREF */
314 retval = retval >> 12;
318 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
320 unsigned retval = vaux_adjust(ts, v);
322 /* ads7846 has a resistor ladder to scale this signal down */
323 if (ts->model == 7846)
328 SHOW(in0_input, vaux, vaux_adjust)
329 SHOW(in1_input, vbatt, vbatt_adjust)
332 static struct attribute *ads7846_attributes[] = {
333 &dev_attr_temp0.attr,
334 &dev_attr_temp1.attr,
335 &dev_attr_in0_input.attr,
336 &dev_attr_in1_input.attr,
340 static struct attribute_group ads7846_attr_group = {
341 .attrs = ads7846_attributes,
344 static struct attribute *ads7843_attributes[] = {
345 &dev_attr_in0_input.attr,
346 &dev_attr_in1_input.attr,
350 static struct attribute_group ads7843_attr_group = {
351 .attrs = ads7843_attributes,
354 static struct attribute *ads7845_attributes[] = {
355 &dev_attr_in0_input.attr,
359 static struct attribute_group ads7845_attr_group = {
360 .attrs = ads7845_attributes,
363 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
365 struct device *hwmon;
368 /* hwmon sensors need a reference voltage */
372 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
380 "external vREF for ADS%d not specified\n",
387 /* different chips have different sensor groups */
390 ts->attr_group = &ads7846_attr_group;
393 ts->attr_group = &ads7845_attr_group;
396 ts->attr_group = &ads7843_attr_group;
399 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
403 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
407 hwmon = hwmon_device_register(&spi->dev);
409 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
410 return PTR_ERR(hwmon);
417 static void ads784x_hwmon_unregister(struct spi_device *spi,
421 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
422 hwmon_device_unregister(ts->hwmon);
427 static inline int ads784x_hwmon_register(struct spi_device *spi,
433 static inline void ads784x_hwmon_unregister(struct spi_device *spi,
439 static int is_pen_down(struct device *dev)
441 struct ads7846 *ts = dev_get_drvdata(dev);
446 static ssize_t ads7846_pen_down_show(struct device *dev,
447 struct device_attribute *attr, char *buf)
449 return sprintf(buf, "%u\n", is_pen_down(dev));
452 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
454 static ssize_t ads7846_disable_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
457 struct ads7846 *ts = dev_get_drvdata(dev);
459 return sprintf(buf, "%u\n", ts->disabled);
462 static ssize_t ads7846_disable_store(struct device *dev,
463 struct device_attribute *attr,
464 const char *buf, size_t count)
466 struct ads7846 *ts = dev_get_drvdata(dev);
470 i = simple_strtoul(buf, &endp, 10);
471 spin_lock_irq(&ts->lock);
478 spin_unlock_irq(&ts->lock);
483 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
485 static struct attribute *ads784x_attributes[] = {
486 &dev_attr_pen_down.attr,
487 &dev_attr_disable.attr,
491 static struct attribute_group ads784x_attr_group = {
492 .attrs = ads784x_attributes,
495 /*--------------------------------------------------------------------------*/
498 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
499 * to retrieve touchscreen status.
501 * The SPI transfer completion callback does the real work. It reports
502 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
505 static void ads7846_rx(void *ads)
507 struct ads7846 *ts = ads;
511 /* ads7846_rx_val() did in-place conversion (including byteswap) from
512 * on-the-wire format as part of debouncing to get stable readings.
519 /* range filtering */
523 if (likely(x && z1)) {
524 /* compute touch pressure resistance using equation #2 */
528 Rt *= ts->x_plate_ohms;
530 Rt = (Rt + 2047) >> 12;
534 if (ts->model == 7843)
535 Rt = ts->pressure_max / 2;
537 /* Sample found inconsistent by debouncing or pressure is beyond
538 * the maximum. Don't report it to user space, repeat at least
539 * once more the measurement
541 if (ts->tc.ignore || Rt > ts->pressure_max) {
543 pr_debug("%s: ignored %d pressure %d\n",
544 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
546 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
551 /* Maybe check the pendown state before reporting. This discards
552 * false readings when the pen is lifted.
554 if (ts->penirq_recheck_delay_usecs) {
555 udelay(ts->penirq_recheck_delay_usecs);
556 if (!ts->get_pendown_state())
560 /* NOTE: We can't rely on the pressure to determine the pen down
561 * state, even this controller has a pressure sensor. The pressure
562 * value can fluctuate for quite a while after lifting the pen and
563 * in some cases may not even settle at the expected value.
565 * The only safe way to check for the pen up condition is in the
566 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
569 struct input_dev *input = ts->input;
572 input_report_key(input, BTN_TOUCH, 1);
575 dev_dbg(&ts->spi->dev, "DOWN\n");
578 input_report_abs(input, ABS_X, x);
579 input_report_abs(input, ABS_Y, y);
580 input_report_abs(input, ABS_PRESSURE, Rt);
584 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
588 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
592 static int ads7846_debounce(void *ads, int data_idx, int *val)
594 struct ads7846 *ts = ads;
596 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
597 /* Start over collecting consistent readings. */
599 /* Repeat it, if this was the first read or the read
600 * wasn't consistent enough. */
601 if (ts->read_cnt < ts->debounce_max) {
602 ts->last_read = *val;
604 return ADS7846_FILTER_REPEAT;
606 /* Maximum number of debouncing reached and still
607 * not enough number of consistent readings. Abort
608 * the whole sample, repeat it in the next sampling
612 return ADS7846_FILTER_IGNORE;
615 if (++ts->read_rep > ts->debounce_rep) {
616 /* Got a good reading for this coordinate,
617 * go for the next one. */
620 return ADS7846_FILTER_OK;
622 /* Read more values that are consistent. */
624 return ADS7846_FILTER_REPEAT;
629 static int ads7846_no_filter(void *ads, int data_idx, int *val)
631 return ADS7846_FILTER_OK;
634 static void ads7846_rx_val(void *ads)
636 struct ads7846 *ts = ads;
637 struct spi_message *m;
638 struct spi_transfer *t;
644 m = &ts->msg[ts->msg_idx];
645 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
648 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
649 * built from two 8 bit values written msb-first.
651 val = be16_to_cpu(*rx_val) >> 3;
653 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
655 case ADS7846_FILTER_REPEAT:
657 case ADS7846_FILTER_IGNORE:
659 /* Last message will contain ads7846_rx() as the
660 * completion function.
664 case ADS7846_FILTER_OK:
667 m = &ts->msg[++ts->msg_idx];
672 status = spi_async(ts->spi, m);
674 dev_err(&ts->spi->dev, "spi_async --> %d\n",
678 static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
680 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
683 spin_lock_irq(&ts->lock);
685 if (unlikely(!ts->get_pendown_state() ||
686 device_suspended(&ts->spi->dev))) {
688 struct input_dev *input = ts->input;
690 input_report_key(input, BTN_TOUCH, 0);
691 input_report_abs(input, ABS_PRESSURE, 0);
696 dev_dbg(&ts->spi->dev, "UP\n");
700 /* measurement cycle ended */
701 if (!device_suspended(&ts->spi->dev)) {
702 ts->irq_disabled = 0;
703 enable_irq(ts->spi->irq);
707 /* pen is still down, continue with the measurement */
709 status = spi_async(ts->spi, &ts->msg[0]);
711 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
714 spin_unlock_irq(&ts->lock);
715 return HRTIMER_NORESTART;
718 static irqreturn_t ads7846_irq(int irq, void *handle)
720 struct ads7846 *ts = handle;
723 spin_lock_irqsave(&ts->lock, flags);
724 if (likely(ts->get_pendown_state())) {
725 if (!ts->irq_disabled) {
726 /* The ARM do_simple_IRQ() dispatcher doesn't act
727 * like the other dispatchers: it will report IRQs
728 * even after they've been disabled. We work around
729 * that here. (The "generic irq" framework may help...)
731 ts->irq_disabled = 1;
732 disable_irq(ts->spi->irq);
734 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
738 spin_unlock_irqrestore(&ts->lock, flags);
743 /*--------------------------------------------------------------------------*/
745 /* Must be called with ts->lock held */
746 static void ads7846_disable(struct ads7846 *ts)
753 /* are we waiting for IRQ, or polling? */
755 ts->irq_disabled = 1;
756 disable_irq(ts->spi->irq);
758 /* the timer will run at least once more, and
759 * leave everything in a clean state, IRQ disabled
761 while (ts->pending) {
762 spin_unlock_irq(&ts->lock);
764 spin_lock_irq(&ts->lock);
768 /* we know the chip's in lowpower mode since we always
769 * leave it that way after every request
774 /* Must be called with ts->lock held */
775 static void ads7846_enable(struct ads7846 *ts)
781 ts->irq_disabled = 0;
782 enable_irq(ts->spi->irq);
785 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
787 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
789 spin_lock_irq(&ts->lock);
791 ts->is_suspended = 1;
794 spin_unlock_irq(&ts->lock);
800 static int ads7846_resume(struct spi_device *spi)
802 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
804 spin_lock_irq(&ts->lock);
806 ts->is_suspended = 0;
809 spin_unlock_irq(&ts->lock);
814 static int __devinit ads7846_probe(struct spi_device *spi)
817 struct input_dev *input_dev;
818 struct ads7846_platform_data *pdata = spi->dev.platform_data;
819 struct spi_message *m;
820 struct spi_transfer *x;
825 dev_dbg(&spi->dev, "no IRQ?\n");
830 dev_dbg(&spi->dev, "no platform data?\n");
835 if (pdata->vaux_control != NULL) {
836 err = pdata->vaux_control(VAUX_ENABLE);
838 dev_dbg(&spi->dev, "TS vaux enable failed\n");
843 /* don't exceed max specified sample rate */
844 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
845 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
846 (spi->max_speed_hz/SAMPLE_BITS)/1000);
850 /* REVISIT when the irq can be triggered active-low, or if for some
851 * reason the touchscreen isn't hooked up, we don't need to access
854 if (pdata->get_pendown_state == NULL) {
855 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
859 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
860 * that even if the hardware can do that, the SPI controller driver
861 * may not. So we stick to very-portable 8 bit words, both RX and TX.
863 spi->bits_per_word = 8;
864 spi->mode = SPI_MODE_0;
865 err = spi_setup(spi);
869 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
870 input_dev = input_allocate_device();
871 if (!ts || !input_dev) {
876 dev_set_drvdata(&spi->dev, ts);
879 ts->input = input_dev;
881 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
882 ts->timer.function = ads7846_timer;
884 spin_lock_init(&ts->lock);
886 ts->model = pdata->model ? : 7846;
887 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
888 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
889 ts->pressure_max = pdata->pressure_max ? : ~0;
891 if (pdata->filter != NULL) {
892 if (pdata->filter_init != NULL) {
893 err = pdata->filter_init(pdata, &ts->filter_data);
897 ts->filter = pdata->filter;
898 ts->filter_cleanup = pdata->filter_cleanup;
899 } else if (pdata->debounce_max) {
900 ts->debounce_max = pdata->debounce_max;
901 if (ts->debounce_max < 2)
902 ts->debounce_max = 2;
903 ts->debounce_tol = pdata->debounce_tol;
904 ts->debounce_rep = pdata->debounce_rep;
905 ts->filter = ads7846_debounce;
906 ts->filter_data = ts;
908 ts->filter = ads7846_no_filter;
909 ts->get_pendown_state = pdata->get_pendown_state;
911 if (pdata->penirq_recheck_delay_usecs)
912 ts->penirq_recheck_delay_usecs =
913 pdata->penirq_recheck_delay_usecs;
915 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
917 input_dev->name = "ADS784x Touchscreen";
918 input_dev->phys = ts->phys;
919 input_dev->dev.parent = &spi->dev;
921 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
922 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
923 input_set_abs_params(input_dev, ABS_X,
925 pdata->x_max ? : MAX_12BIT,
927 input_set_abs_params(input_dev, ABS_Y,
929 pdata->y_max ? : MAX_12BIT,
931 input_set_abs_params(input_dev, ABS_PRESSURE,
932 pdata->pressure_min, pdata->pressure_max, 0, 0);
934 vref = pdata->keep_vref_on;
936 /* set up the transfers to read touchscreen state; this assumes we
937 * use formula #2 for pressure, not #3.
944 /* y- still on; turn on only y+ (and ADC) */
945 ts->read_y = READ_Y(vref);
946 x->tx_buf = &ts->read_y;
948 spi_message_add_tail(x, m);
951 x->rx_buf = &ts->tc.y;
953 spi_message_add_tail(x, m);
955 /* the first sample after switching drivers can be low quality;
956 * optionally discard it, using a second one after the signals
957 * have had enough time to stabilize.
959 if (pdata->settle_delay_usecs) {
960 x->delay_usecs = pdata->settle_delay_usecs;
963 x->tx_buf = &ts->read_y;
965 spi_message_add_tail(x, m);
968 x->rx_buf = &ts->tc.y;
970 spi_message_add_tail(x, m);
973 m->complete = ads7846_rx_val;
979 /* turn y- off, x+ on, then leave in lowpower */
981 ts->read_x = READ_X(vref);
982 x->tx_buf = &ts->read_x;
984 spi_message_add_tail(x, m);
987 x->rx_buf = &ts->tc.x;
989 spi_message_add_tail(x, m);
991 /* ... maybe discard first sample ... */
992 if (pdata->settle_delay_usecs) {
993 x->delay_usecs = pdata->settle_delay_usecs;
996 x->tx_buf = &ts->read_x;
998 spi_message_add_tail(x, m);
1001 x->rx_buf = &ts->tc.x;
1003 spi_message_add_tail(x, m);
1006 m->complete = ads7846_rx_val;
1009 /* turn y+ off, x- on; we'll use formula #2 */
1010 if (ts->model == 7846) {
1012 spi_message_init(m);
1015 ts->read_z1 = READ_Z1(vref);
1016 x->tx_buf = &ts->read_z1;
1018 spi_message_add_tail(x, m);
1021 x->rx_buf = &ts->tc.z1;
1023 spi_message_add_tail(x, m);
1025 /* ... maybe discard first sample ... */
1026 if (pdata->settle_delay_usecs) {
1027 x->delay_usecs = pdata->settle_delay_usecs;
1030 x->tx_buf = &ts->read_z1;
1032 spi_message_add_tail(x, m);
1035 x->rx_buf = &ts->tc.z1;
1037 spi_message_add_tail(x, m);
1040 m->complete = ads7846_rx_val;
1044 spi_message_init(m);
1047 ts->read_z2 = READ_Z2(vref);
1048 x->tx_buf = &ts->read_z2;
1050 spi_message_add_tail(x, m);
1053 x->rx_buf = &ts->tc.z2;
1055 spi_message_add_tail(x, m);
1057 /* ... maybe discard first sample ... */
1058 if (pdata->settle_delay_usecs) {
1059 x->delay_usecs = pdata->settle_delay_usecs;
1062 x->tx_buf = &ts->read_z2;
1064 spi_message_add_tail(x, m);
1067 x->rx_buf = &ts->tc.z2;
1069 spi_message_add_tail(x, m);
1072 m->complete = ads7846_rx_val;
1078 spi_message_init(m);
1081 ts->pwrdown = PWRDOWN;
1082 x->tx_buf = &ts->pwrdown;
1084 spi_message_add_tail(x, m);
1087 x->rx_buf = &ts->dummy;
1090 spi_message_add_tail(x, m);
1092 m->complete = ads7846_rx;
1097 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
1098 spi->dev.driver->name, ts)) {
1099 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1101 goto err_cleanup_filter;
1104 err = ads784x_hwmon_register(spi, ts);
1108 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1110 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1111 * the touchscreen, in case it's not connected.
1113 (void) ads7846_read12_ser(&spi->dev,
1114 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1116 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1118 goto err_remove_hwmon;
1120 err = input_register_device(input_dev);
1122 goto err_remove_attr_group;
1126 err_remove_attr_group:
1127 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1129 ads784x_hwmon_unregister(spi, ts);
1131 free_irq(spi->irq, ts);
1133 if (ts->filter_cleanup)
1134 ts->filter_cleanup(ts->filter_data);
1136 input_free_device(input_dev);
1141 static int __devexit ads7846_remove(struct spi_device *spi)
1143 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
1145 ads784x_hwmon_unregister(spi, ts);
1146 input_unregister_device(ts->input);
1148 ads7846_suspend(spi, PMSG_SUSPEND);
1150 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1152 free_irq(ts->spi->irq, ts);
1153 /* suspend left the IRQ disabled */
1154 enable_irq(ts->spi->irq);
1156 if (ts->filter_cleanup)
1157 ts->filter_cleanup(ts->filter_data);
1161 dev_dbg(&spi->dev, "unregistered touchscreen\n");
1165 static struct spi_driver ads7846_driver = {
1168 .bus = &spi_bus_type,
1169 .owner = THIS_MODULE,
1171 .probe = ads7846_probe,
1172 .remove = __devexit_p(ads7846_remove),
1173 .suspend = ads7846_suspend,
1174 .resume = ads7846_resume,
1177 static int __init ads7846_init(void)
1179 return spi_register_driver(&ads7846_driver);
1181 module_init(ads7846_init);
1183 static void __exit ads7846_exit(void)
1185 spi_unregister_driver(&ads7846_driver);
1187 module_exit(ads7846_exit);
1189 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1190 MODULE_LICENSE("GPL");