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 * We allocate this separately to avoid cache line sharing issues when
74 * driver is used with DMA-based SPI controllers (like atmel_spi) on
75 * systems where main memory is not DMA-coherent (most non-x86 boards).
77 struct ads7846_packet {
78 u8 read_x, read_y, read_z1, read_z2, pwrdown;
79 u16 dummy; /* for the pwrdown read */
84 struct input_dev *input;
87 struct spi_device *spi;
89 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
90 struct attribute_group *attr_group;
100 struct ads7846_packet *packet;
102 struct spi_transfer xfer[18];
103 struct spi_message msg[5];
104 struct spi_message *last_msg;
114 u16 penirq_recheck_delay_usecs;
117 struct hrtimer timer;
118 unsigned pendown:1; /* P: lock */
119 unsigned pending:1; /* P: lock */
120 // FIXME remove "irq_disabled"
121 unsigned irq_disabled:1; /* P: lock */
123 unsigned is_suspended:1;
125 int (*filter)(void *data, int data_idx, int *val);
127 void (*filter_cleanup)(void *data);
128 int (*get_pendown_state)(void);
131 void (*wait_for_sync)(void);
134 /* leave chip selected when we're done, for quicker re-select? */
136 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
138 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
141 /*--------------------------------------------------------------------------*/
143 /* The ADS7846 has touchscreen and other sensors.
144 * Earlier ads784x chips are somewhat compatible.
146 #define ADS_START (1 << 7)
147 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
148 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
149 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
150 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
151 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
152 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
153 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
154 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
155 #define ADS_8_BIT (1 << 3)
156 #define ADS_12_BIT (0 << 3)
157 #define ADS_SER (1 << 2) /* non-differential */
158 #define ADS_DFR (0 << 2) /* differential */
159 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
160 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
161 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
162 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
164 #define MAX_12BIT ((1<<12)-1)
166 /* leave ADC powered up (disables penirq) between differential samples */
167 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
168 | ADS_12_BIT | ADS_DFR | \
169 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
171 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
172 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
173 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
175 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
176 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
178 /* single-ended samples need to first power up reference voltage;
179 * we leave both ADC and VREF powered
181 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
182 | ADS_12_BIT | ADS_SER)
184 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
185 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
187 /*--------------------------------------------------------------------------*/
190 * Non-touchscreen sensors only use single-ended conversions.
191 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
192 * ads7846 lets that pin be unconnected, to use internal vREF.
201 struct spi_message msg;
202 struct spi_transfer xfer[6];
205 static void ads7846_enable(struct ads7846 *ts);
206 static void ads7846_disable(struct ads7846 *ts);
208 static int device_suspended(struct device *dev)
210 struct ads7846 *ts = dev_get_drvdata(dev);
211 return ts->is_suspended || ts->disabled;
214 static int ads7846_read12_ser(struct device *dev, unsigned command)
216 struct spi_device *spi = to_spi_device(dev);
217 struct ads7846 *ts = dev_get_drvdata(dev);
218 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
225 spi_message_init(&req->msg);
227 /* FIXME boards with ads7846 might use external vref instead ... */
228 use_internal = (ts->model == 7846);
230 /* maybe turn on internal vREF, and let it settle */
232 req->ref_on = REF_ON;
233 req->xfer[0].tx_buf = &req->ref_on;
234 req->xfer[0].len = 1;
235 spi_message_add_tail(&req->xfer[0], &req->msg);
237 req->xfer[1].rx_buf = &req->scratch;
238 req->xfer[1].len = 2;
240 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
241 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
242 spi_message_add_tail(&req->xfer[1], &req->msg);
246 req->command = (u8) command;
247 req->xfer[2].tx_buf = &req->command;
248 req->xfer[2].len = 1;
249 spi_message_add_tail(&req->xfer[2], &req->msg);
251 req->xfer[3].rx_buf = &req->sample;
252 req->xfer[3].len = 2;
253 spi_message_add_tail(&req->xfer[3], &req->msg);
255 /* REVISIT: take a few more samples, and compare ... */
257 /* converter in low power mode & enable PENIRQ */
258 req->ref_off = PWRDOWN;
259 req->xfer[4].tx_buf = &req->ref_off;
260 req->xfer[4].len = 1;
261 spi_message_add_tail(&req->xfer[4], &req->msg);
263 req->xfer[5].rx_buf = &req->scratch;
264 req->xfer[5].len = 2;
265 CS_CHANGE(req->xfer[5]);
266 spi_message_add_tail(&req->xfer[5], &req->msg);
268 ts->irq_disabled = 1;
269 disable_irq(spi->irq);
270 status = spi_sync(spi, &req->msg);
271 ts->irq_disabled = 0;
272 enable_irq(spi->irq);
275 /* on-wire is a must-ignore bit, a BE12 value, then padding */
276 status = be16_to_cpu(req->sample);
277 status = status >> 3;
285 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
287 #define SHOW(name, var, adjust) static ssize_t \
288 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
290 struct ads7846 *ts = dev_get_drvdata(dev); \
291 ssize_t v = ads7846_read12_ser(dev, \
292 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
295 return sprintf(buf, "%u\n", adjust(ts, v)); \
297 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
300 /* Sysfs conventions report temperatures in millidegrees Celsius.
301 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
302 * accuracy scheme without calibration data. For now we won't try either;
303 * userspace sees raw sensor values, and must scale/calibrate appropriately.
305 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
310 SHOW(temp0, temp0, null_adjust) /* temp1_input */
311 SHOW(temp1, temp1, null_adjust) /* temp2_input */
314 /* sysfs conventions report voltages in millivolts. We can convert voltages
315 * if we know vREF. userspace may need to scale vAUX to match the board's
316 * external resistors; we assume that vBATT only uses the internal ones.
318 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
322 /* external resistors may scale vAUX into 0..vREF */
323 retval *= ts->vref_mv;
324 retval = retval >> 12;
328 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
330 unsigned retval = vaux_adjust(ts, v);
332 /* ads7846 has a resistor ladder to scale this signal down */
333 if (ts->model == 7846)
338 SHOW(in0_input, vaux, vaux_adjust)
339 SHOW(in1_input, vbatt, vbatt_adjust)
342 static struct attribute *ads7846_attributes[] = {
343 &dev_attr_temp0.attr,
344 &dev_attr_temp1.attr,
345 &dev_attr_in0_input.attr,
346 &dev_attr_in1_input.attr,
350 static struct attribute_group ads7846_attr_group = {
351 .attrs = ads7846_attributes,
354 static struct attribute *ads7843_attributes[] = {
355 &dev_attr_in0_input.attr,
356 &dev_attr_in1_input.attr,
360 static struct attribute_group ads7843_attr_group = {
361 .attrs = ads7843_attributes,
364 static struct attribute *ads7845_attributes[] = {
365 &dev_attr_in0_input.attr,
369 static struct attribute_group ads7845_attr_group = {
370 .attrs = ads7845_attributes,
373 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
375 struct device *hwmon;
378 /* hwmon sensors need a reference voltage */
382 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
390 "external vREF for ADS%d not specified\n",
397 /* different chips have different sensor groups */
400 ts->attr_group = &ads7846_attr_group;
403 ts->attr_group = &ads7845_attr_group;
406 ts->attr_group = &ads7843_attr_group;
409 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
413 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
417 hwmon = hwmon_device_register(&spi->dev);
419 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
420 return PTR_ERR(hwmon);
427 static void ads784x_hwmon_unregister(struct spi_device *spi,
431 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
432 hwmon_device_unregister(ts->hwmon);
437 static inline int ads784x_hwmon_register(struct spi_device *spi,
443 static inline void ads784x_hwmon_unregister(struct spi_device *spi,
449 static int is_pen_down(struct device *dev)
451 struct ads7846 *ts = dev_get_drvdata(dev);
456 static ssize_t ads7846_pen_down_show(struct device *dev,
457 struct device_attribute *attr, char *buf)
459 return sprintf(buf, "%u\n", is_pen_down(dev));
462 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
464 static ssize_t ads7846_disable_show(struct device *dev,
465 struct device_attribute *attr, char *buf)
467 struct ads7846 *ts = dev_get_drvdata(dev);
469 return sprintf(buf, "%u\n", ts->disabled);
472 static ssize_t ads7846_disable_store(struct device *dev,
473 struct device_attribute *attr,
474 const char *buf, size_t count)
476 struct ads7846 *ts = dev_get_drvdata(dev);
479 if (strict_strtoul(buf, 10, &i))
482 spin_lock_irq(&ts->lock);
489 spin_unlock_irq(&ts->lock);
494 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
496 static struct attribute *ads784x_attributes[] = {
497 &dev_attr_pen_down.attr,
498 &dev_attr_disable.attr,
502 static struct attribute_group ads784x_attr_group = {
503 .attrs = ads784x_attributes,
506 /*--------------------------------------------------------------------------*/
508 static int get_pendown_state(struct ads7846 *ts)
510 if (ts->get_pendown_state)
511 return ts->get_pendown_state();
513 return !gpio_get_value(ts->gpio_pendown);
516 static void null_wait_for_sync(void)
521 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
522 * to retrieve touchscreen status.
524 * The SPI transfer completion callback does the real work. It reports
525 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
528 static void ads7846_rx(void *ads)
530 struct ads7846 *ts = ads;
531 struct ads7846_packet *packet = ts->packet;
535 /* ads7846_rx_val() did in-place conversion (including byteswap) from
536 * on-the-wire format as part of debouncing to get stable readings.
543 /* range filtering */
547 if (ts->model == 7843) {
548 Rt = ts->pressure_max / 2;
549 } else if (likely(x && z1)) {
550 /* compute touch pressure resistance using equation #2 */
554 Rt *= ts->x_plate_ohms;
556 Rt = (Rt + 2047) >> 12;
561 /* Sample found inconsistent by debouncing or pressure is beyond
562 * the maximum. Don't report it to user space, repeat at least
563 * once more the measurement
565 if (packet->tc.ignore || Rt > ts->pressure_max) {
567 pr_debug("%s: ignored %d pressure %d\n",
568 dev_name(&ts->spi->dev), packet->tc.ignore, Rt);
570 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
575 /* Maybe check the pendown state before reporting. This discards
576 * false readings when the pen is lifted.
578 if (ts->penirq_recheck_delay_usecs) {
579 udelay(ts->penirq_recheck_delay_usecs);
580 if (!get_pendown_state(ts))
584 /* NOTE: We can't rely on the pressure to determine the pen down
585 * state, even this controller has a pressure sensor. The pressure
586 * value can fluctuate for quite a while after lifting the pen and
587 * in some cases may not even settle at the expected value.
589 * The only safe way to check for the pen up condition is in the
590 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
593 struct input_dev *input = ts->input;
596 input_report_key(input, BTN_TOUCH, 1);
599 dev_dbg(&ts->spi->dev, "DOWN\n");
602 input_report_abs(input, ABS_X, x);
603 input_report_abs(input, ABS_Y, y);
604 input_report_abs(input, ABS_PRESSURE, Rt);
608 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
612 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
616 static int ads7846_debounce(void *ads, int data_idx, int *val)
618 struct ads7846 *ts = ads;
620 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
621 /* Start over collecting consistent readings. */
623 /* Repeat it, if this was the first read or the read
624 * wasn't consistent enough. */
625 if (ts->read_cnt < ts->debounce_max) {
626 ts->last_read = *val;
628 return ADS7846_FILTER_REPEAT;
630 /* Maximum number of debouncing reached and still
631 * not enough number of consistent readings. Abort
632 * the whole sample, repeat it in the next sampling
636 return ADS7846_FILTER_IGNORE;
639 if (++ts->read_rep > ts->debounce_rep) {
640 /* Got a good reading for this coordinate,
641 * go for the next one. */
644 return ADS7846_FILTER_OK;
646 /* Read more values that are consistent. */
648 return ADS7846_FILTER_REPEAT;
653 static int ads7846_no_filter(void *ads, int data_idx, int *val)
655 return ADS7846_FILTER_OK;
658 static void ads7846_rx_val(void *ads)
660 struct ads7846 *ts = ads;
661 struct ads7846_packet *packet = ts->packet;
662 struct spi_message *m;
663 struct spi_transfer *t;
668 m = &ts->msg[ts->msg_idx];
669 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
671 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
672 * built from two 8 bit values written msb-first.
674 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
676 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
678 case ADS7846_FILTER_REPEAT:
680 case ADS7846_FILTER_IGNORE:
681 packet->tc.ignore = 1;
682 /* Last message will contain ads7846_rx() as the
683 * completion function.
687 case ADS7846_FILTER_OK:
688 *(u16 *)t->rx_buf = val;
689 packet->tc.ignore = 0;
690 m = &ts->msg[++ts->msg_idx];
696 status = spi_async(ts->spi, m);
698 dev_err(&ts->spi->dev, "spi_async --> %d\n",
702 static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
704 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
707 spin_lock(&ts->lock);
709 if (unlikely(!get_pendown_state(ts) ||
710 device_suspended(&ts->spi->dev))) {
712 struct input_dev *input = ts->input;
714 input_report_key(input, BTN_TOUCH, 0);
715 input_report_abs(input, ABS_PRESSURE, 0);
720 dev_dbg(&ts->spi->dev, "UP\n");
724 /* measurement cycle ended */
725 if (!device_suspended(&ts->spi->dev)) {
726 ts->irq_disabled = 0;
727 enable_irq(ts->spi->irq);
731 /* pen is still down, continue with the measurement */
734 status = spi_async(ts->spi, &ts->msg[0]);
736 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
739 spin_unlock(&ts->lock);
740 return HRTIMER_NORESTART;
743 static irqreturn_t ads7846_irq(int irq, void *handle)
745 struct ads7846 *ts = handle;
748 spin_lock_irqsave(&ts->lock, flags);
749 if (likely(get_pendown_state(ts))) {
750 if (!ts->irq_disabled) {
751 /* The ARM do_simple_IRQ() dispatcher doesn't act
752 * like the other dispatchers: it will report IRQs
753 * even after they've been disabled. We work around
754 * that here. (The "generic irq" framework may help...)
756 ts->irq_disabled = 1;
757 disable_irq_nosync(ts->spi->irq);
759 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
763 spin_unlock_irqrestore(&ts->lock, flags);
768 /*--------------------------------------------------------------------------*/
770 /* Must be called with ts->lock held */
771 static void ads7846_disable(struct ads7846 *ts)
778 /* are we waiting for IRQ, or polling? */
780 ts->irq_disabled = 1;
781 disable_irq(ts->spi->irq);
783 /* the timer will run at least once more, and
784 * leave everything in a clean state, IRQ disabled
786 while (ts->pending) {
787 spin_unlock_irq(&ts->lock);
789 spin_lock_irq(&ts->lock);
793 /* we know the chip's in lowpower mode since we always
794 * leave it that way after every request
798 /* Must be called with ts->lock held */
799 static void ads7846_enable(struct ads7846 *ts)
805 ts->irq_disabled = 0;
806 enable_irq(ts->spi->irq);
809 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
811 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
813 spin_lock_irq(&ts->lock);
815 ts->is_suspended = 1;
818 spin_unlock_irq(&ts->lock);
824 static int ads7846_resume(struct spi_device *spi)
826 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
828 spin_lock_irq(&ts->lock);
830 ts->is_suspended = 0;
833 spin_unlock_irq(&ts->lock);
838 static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts)
840 struct ads7846_platform_data *pdata = spi->dev.platform_data;
843 /* REVISIT when the irq can be triggered active-low, or if for some
844 * reason the touchscreen isn't hooked up, we don't need to access
847 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
848 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
852 if (pdata->get_pendown_state) {
853 ts->get_pendown_state = pdata->get_pendown_state;
857 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
859 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
860 pdata->gpio_pendown);
864 ts->gpio_pendown = pdata->gpio_pendown;
868 static int __devinit ads7846_probe(struct spi_device *spi)
871 struct ads7846_packet *packet;
872 struct input_dev *input_dev;
873 struct ads7846_platform_data *pdata = spi->dev.platform_data;
874 struct spi_message *m;
875 struct spi_transfer *x;
880 dev_dbg(&spi->dev, "no IRQ?\n");
885 dev_dbg(&spi->dev, "no platform data?\n");
889 /* don't exceed max specified sample rate */
890 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
891 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
892 (spi->max_speed_hz/SAMPLE_BITS)/1000);
896 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
897 * that even if the hardware can do that, the SPI controller driver
898 * may not. So we stick to very-portable 8 bit words, both RX and TX.
900 spi->bits_per_word = 8;
901 spi->mode = SPI_MODE_0;
902 err = spi_setup(spi);
906 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
907 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
908 input_dev = input_allocate_device();
909 if (!ts || !packet || !input_dev) {
914 dev_set_drvdata(&spi->dev, ts);
918 ts->input = input_dev;
919 ts->vref_mv = pdata->vref_mv;
921 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
922 ts->timer.function = ads7846_timer;
924 spin_lock_init(&ts->lock);
926 ts->model = pdata->model ? : 7846;
927 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
928 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
929 ts->pressure_max = pdata->pressure_max ? : ~0;
931 if (pdata->filter != NULL) {
932 if (pdata->filter_init != NULL) {
933 err = pdata->filter_init(pdata, &ts->filter_data);
937 ts->filter = pdata->filter;
938 ts->filter_cleanup = pdata->filter_cleanup;
939 } else if (pdata->debounce_max) {
940 ts->debounce_max = pdata->debounce_max;
941 if (ts->debounce_max < 2)
942 ts->debounce_max = 2;
943 ts->debounce_tol = pdata->debounce_tol;
944 ts->debounce_rep = pdata->debounce_rep;
945 ts->filter = ads7846_debounce;
946 ts->filter_data = ts;
948 ts->filter = ads7846_no_filter;
950 err = setup_pendown(spi, ts);
952 goto err_cleanup_filter;
954 if (pdata->penirq_recheck_delay_usecs)
955 ts->penirq_recheck_delay_usecs =
956 pdata->penirq_recheck_delay_usecs;
958 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
960 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
962 input_dev->name = "ADS784x Touchscreen";
963 input_dev->phys = ts->phys;
964 input_dev->dev.parent = &spi->dev;
966 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
967 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
968 input_set_abs_params(input_dev, ABS_X,
970 pdata->x_max ? : MAX_12BIT,
972 input_set_abs_params(input_dev, ABS_Y,
974 pdata->y_max ? : MAX_12BIT,
976 input_set_abs_params(input_dev, ABS_PRESSURE,
977 pdata->pressure_min, pdata->pressure_max, 0, 0);
979 vref = pdata->keep_vref_on;
981 /* set up the transfers to read touchscreen state; this assumes we
982 * use formula #2 for pressure, not #3.
989 /* y- still on; turn on only y+ (and ADC) */
990 packet->read_y = READ_Y(vref);
991 x->tx_buf = &packet->read_y;
993 spi_message_add_tail(x, m);
996 x->rx_buf = &packet->tc.y;
998 spi_message_add_tail(x, m);
1000 /* the first sample after switching drivers can be low quality;
1001 * optionally discard it, using a second one after the signals
1002 * have had enough time to stabilize.
1004 if (pdata->settle_delay_usecs) {
1005 x->delay_usecs = pdata->settle_delay_usecs;
1008 x->tx_buf = &packet->read_y;
1010 spi_message_add_tail(x, m);
1013 x->rx_buf = &packet->tc.y;
1015 spi_message_add_tail(x, m);
1018 m->complete = ads7846_rx_val;
1022 spi_message_init(m);
1024 /* turn y- off, x+ on, then leave in lowpower */
1026 packet->read_x = READ_X(vref);
1027 x->tx_buf = &packet->read_x;
1029 spi_message_add_tail(x, m);
1032 x->rx_buf = &packet->tc.x;
1034 spi_message_add_tail(x, m);
1036 /* ... maybe discard first sample ... */
1037 if (pdata->settle_delay_usecs) {
1038 x->delay_usecs = pdata->settle_delay_usecs;
1041 x->tx_buf = &packet->read_x;
1043 spi_message_add_tail(x, m);
1046 x->rx_buf = &packet->tc.x;
1048 spi_message_add_tail(x, m);
1051 m->complete = ads7846_rx_val;
1054 /* turn y+ off, x- on; we'll use formula #2 */
1055 if (ts->model == 7846) {
1057 spi_message_init(m);
1060 packet->read_z1 = READ_Z1(vref);
1061 x->tx_buf = &packet->read_z1;
1063 spi_message_add_tail(x, m);
1066 x->rx_buf = &packet->tc.z1;
1068 spi_message_add_tail(x, m);
1070 /* ... maybe discard first sample ... */
1071 if (pdata->settle_delay_usecs) {
1072 x->delay_usecs = pdata->settle_delay_usecs;
1075 x->tx_buf = &packet->read_z1;
1077 spi_message_add_tail(x, m);
1080 x->rx_buf = &packet->tc.z1;
1082 spi_message_add_tail(x, m);
1085 m->complete = ads7846_rx_val;
1089 spi_message_init(m);
1092 packet->read_z2 = READ_Z2(vref);
1093 x->tx_buf = &packet->read_z2;
1095 spi_message_add_tail(x, m);
1098 x->rx_buf = &packet->tc.z2;
1100 spi_message_add_tail(x, m);
1102 /* ... maybe discard first sample ... */
1103 if (pdata->settle_delay_usecs) {
1104 x->delay_usecs = pdata->settle_delay_usecs;
1107 x->tx_buf = &packet->read_z2;
1109 spi_message_add_tail(x, m);
1112 x->rx_buf = &packet->tc.z2;
1114 spi_message_add_tail(x, m);
1117 m->complete = ads7846_rx_val;
1123 spi_message_init(m);
1126 packet->pwrdown = PWRDOWN;
1127 x->tx_buf = &packet->pwrdown;
1129 spi_message_add_tail(x, m);
1132 x->rx_buf = &packet->dummy;
1135 spi_message_add_tail(x, m);
1137 m->complete = ads7846_rx;
1142 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
1143 spi->dev.driver->name, ts)) {
1144 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1149 err = ads784x_hwmon_register(spi, ts);
1153 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1155 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1156 * the touchscreen, in case it's not connected.
1158 (void) ads7846_read12_ser(&spi->dev,
1159 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1161 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1163 goto err_remove_hwmon;
1165 err = input_register_device(input_dev);
1167 goto err_remove_attr_group;
1171 err_remove_attr_group:
1172 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1174 ads784x_hwmon_unregister(spi, ts);
1176 free_irq(spi->irq, ts);
1178 if (ts->gpio_pendown != -1)
1179 gpio_free(ts->gpio_pendown);
1181 if (ts->filter_cleanup)
1182 ts->filter_cleanup(ts->filter_data);
1184 input_free_device(input_dev);
1190 static int __devexit ads7846_remove(struct spi_device *spi)
1192 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
1194 ads784x_hwmon_unregister(spi, ts);
1195 input_unregister_device(ts->input);
1197 ads7846_suspend(spi, PMSG_SUSPEND);
1199 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1201 free_irq(ts->spi->irq, ts);
1202 /* suspend left the IRQ disabled */
1203 enable_irq(ts->spi->irq);
1205 if (ts->gpio_pendown != -1)
1206 gpio_free(ts->gpio_pendown);
1208 if (ts->filter_cleanup)
1209 ts->filter_cleanup(ts->filter_data);
1214 dev_dbg(&spi->dev, "unregistered touchscreen\n");
1218 static struct spi_driver ads7846_driver = {
1221 .bus = &spi_bus_type,
1222 .owner = THIS_MODULE,
1224 .probe = ads7846_probe,
1225 .remove = __devexit_p(ads7846_remove),
1226 .suspend = ads7846_suspend,
1227 .resume = ads7846_resume,
1230 static int __init ads7846_init(void)
1232 return spi_register_driver(&ads7846_driver);
1234 module_init(ads7846_init);
1236 static void __exit ads7846_exit(void)
1238 spi_unregister_driver(&ads7846_driver);
1240 module_exit(ads7846_exit);
1242 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1243 MODULE_LICENSE("GPL");