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>
30 #include <linux/regulator/consumer.h>
34 * This code has been heavily tested on a Nokia 770, and lightly
35 * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz).
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;
88 struct spi_device *spi;
89 struct regulator *reg;
91 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
92 struct attribute_group *attr_group;
104 struct ads7846_packet *packet;
106 struct spi_transfer xfer[18];
107 struct spi_message msg[5];
108 struct spi_message *last_msg;
118 u16 penirq_recheck_delay_usecs;
121 struct hrtimer timer;
122 unsigned pendown:1; /* P: lock */
123 unsigned pending:1; /* P: lock */
124 // FIXME remove "irq_disabled"
125 unsigned irq_disabled:1; /* P: lock */
127 unsigned is_suspended:1;
129 int (*filter)(void *data, int data_idx, int *val);
131 void (*filter_cleanup)(void *data);
132 int (*get_pendown_state)(void);
135 void (*wait_for_sync)(void);
138 /* leave chip selected when we're done, for quicker re-select? */
140 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
142 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
145 /*--------------------------------------------------------------------------*/
147 /* The ADS7846 has touchscreen and other sensors.
148 * Earlier ads784x chips are somewhat compatible.
150 #define ADS_START (1 << 7)
151 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
152 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
153 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
154 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
155 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
156 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
157 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
158 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
159 #define ADS_8_BIT (1 << 3)
160 #define ADS_12_BIT (0 << 3)
161 #define ADS_SER (1 << 2) /* non-differential */
162 #define ADS_DFR (0 << 2) /* differential */
163 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
164 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
165 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
166 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
168 #define MAX_12BIT ((1<<12)-1)
170 /* leave ADC powered up (disables penirq) between differential samples */
171 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
172 | ADS_12_BIT | ADS_DFR | \
173 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
175 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
176 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
177 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
179 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
180 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
182 /* single-ended samples need to first power up reference voltage;
183 * we leave both ADC and VREF powered
185 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
186 | ADS_12_BIT | ADS_SER)
188 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
189 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
191 /*--------------------------------------------------------------------------*/
194 * Non-touchscreen sensors only use single-ended conversions.
195 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
196 * ads7846 lets that pin be unconnected, to use internal vREF.
205 struct spi_message msg;
206 struct spi_transfer xfer[6];
209 static void ads7846_enable(struct ads7846 *ts);
210 static void ads7846_disable(struct ads7846 *ts);
212 static int device_suspended(struct device *dev)
214 struct ads7846 *ts = dev_get_drvdata(dev);
215 return ts->is_suspended || ts->disabled;
218 static int ads7846_read12_ser(struct device *dev, unsigned command)
220 struct spi_device *spi = to_spi_device(dev);
221 struct ads7846 *ts = dev_get_drvdata(dev);
222 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
229 spi_message_init(&req->msg);
231 /* FIXME boards with ads7846 might use external vref instead ... */
232 use_internal = (ts->model == 7846);
234 /* maybe turn on internal vREF, and let it settle */
236 req->ref_on = REF_ON;
237 req->xfer[0].tx_buf = &req->ref_on;
238 req->xfer[0].len = 1;
239 spi_message_add_tail(&req->xfer[0], &req->msg);
241 req->xfer[1].rx_buf = &req->scratch;
242 req->xfer[1].len = 2;
244 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
245 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
246 spi_message_add_tail(&req->xfer[1], &req->msg);
250 req->command = (u8) command;
251 req->xfer[2].tx_buf = &req->command;
252 req->xfer[2].len = 1;
253 spi_message_add_tail(&req->xfer[2], &req->msg);
255 req->xfer[3].rx_buf = &req->sample;
256 req->xfer[3].len = 2;
257 spi_message_add_tail(&req->xfer[3], &req->msg);
259 /* REVISIT: take a few more samples, and compare ... */
261 /* converter in low power mode & enable PENIRQ */
262 req->ref_off = PWRDOWN;
263 req->xfer[4].tx_buf = &req->ref_off;
264 req->xfer[4].len = 1;
265 spi_message_add_tail(&req->xfer[4], &req->msg);
267 req->xfer[5].rx_buf = &req->scratch;
268 req->xfer[5].len = 2;
269 CS_CHANGE(req->xfer[5]);
270 spi_message_add_tail(&req->xfer[5], &req->msg);
272 ts->irq_disabled = 1;
273 disable_irq(spi->irq);
274 status = spi_sync(spi, &req->msg);
275 ts->irq_disabled = 0;
276 enable_irq(spi->irq);
279 /* on-wire is a must-ignore bit, a BE12 value, then padding */
280 status = be16_to_cpu(req->sample);
281 status = status >> 3;
289 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
291 #define SHOW(name, var, adjust) static ssize_t \
292 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
294 struct ads7846 *ts = dev_get_drvdata(dev); \
295 ssize_t v = ads7846_read12_ser(dev, \
296 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
299 return sprintf(buf, "%u\n", adjust(ts, v)); \
301 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
304 /* Sysfs conventions report temperatures in millidegrees Celsius.
305 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
306 * accuracy scheme without calibration data. For now we won't try either;
307 * userspace sees raw sensor values, and must scale/calibrate appropriately.
309 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
314 SHOW(temp0, temp0, null_adjust) /* temp1_input */
315 SHOW(temp1, temp1, null_adjust) /* temp2_input */
318 /* sysfs conventions report voltages in millivolts. We can convert voltages
319 * if we know vREF. userspace may need to scale vAUX to match the board's
320 * external resistors; we assume that vBATT only uses the internal ones.
322 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
326 /* external resistors may scale vAUX into 0..vREF */
327 retval *= ts->vref_mv;
328 retval = retval >> 12;
332 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
334 unsigned retval = vaux_adjust(ts, v);
336 /* ads7846 has a resistor ladder to scale this signal down */
337 if (ts->model == 7846)
342 SHOW(in0_input, vaux, vaux_adjust)
343 SHOW(in1_input, vbatt, vbatt_adjust)
346 static struct attribute *ads7846_attributes[] = {
347 &dev_attr_temp0.attr,
348 &dev_attr_temp1.attr,
349 &dev_attr_in0_input.attr,
350 &dev_attr_in1_input.attr,
354 static struct attribute_group ads7846_attr_group = {
355 .attrs = ads7846_attributes,
358 static struct attribute *ads7843_attributes[] = {
359 &dev_attr_in0_input.attr,
360 &dev_attr_in1_input.attr,
364 static struct attribute_group ads7843_attr_group = {
365 .attrs = ads7843_attributes,
368 static struct attribute *ads7845_attributes[] = {
369 &dev_attr_in0_input.attr,
373 static struct attribute_group ads7845_attr_group = {
374 .attrs = ads7845_attributes,
377 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
379 struct device *hwmon;
382 /* hwmon sensors need a reference voltage */
386 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
394 "external vREF for ADS%d not specified\n",
401 /* different chips have different sensor groups */
404 ts->attr_group = &ads7846_attr_group;
407 ts->attr_group = &ads7845_attr_group;
410 ts->attr_group = &ads7843_attr_group;
413 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
417 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
421 hwmon = hwmon_device_register(&spi->dev);
423 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
424 return PTR_ERR(hwmon);
431 static void ads784x_hwmon_unregister(struct spi_device *spi,
435 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
436 hwmon_device_unregister(ts->hwmon);
441 static inline int ads784x_hwmon_register(struct spi_device *spi,
447 static inline void ads784x_hwmon_unregister(struct spi_device *spi,
453 static int is_pen_down(struct device *dev)
455 struct ads7846 *ts = dev_get_drvdata(dev);
460 static ssize_t ads7846_pen_down_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
463 return sprintf(buf, "%u\n", is_pen_down(dev));
466 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
468 static ssize_t ads7846_disable_show(struct device *dev,
469 struct device_attribute *attr, char *buf)
471 struct ads7846 *ts = dev_get_drvdata(dev);
473 return sprintf(buf, "%u\n", ts->disabled);
476 static ssize_t ads7846_disable_store(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
480 struct ads7846 *ts = dev_get_drvdata(dev);
483 if (strict_strtoul(buf, 10, &i))
486 spin_lock_irq(&ts->lock);
493 spin_unlock_irq(&ts->lock);
498 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
500 static struct attribute *ads784x_attributes[] = {
501 &dev_attr_pen_down.attr,
502 &dev_attr_disable.attr,
506 static struct attribute_group ads784x_attr_group = {
507 .attrs = ads784x_attributes,
510 /*--------------------------------------------------------------------------*/
512 static int get_pendown_state(struct ads7846 *ts)
514 if (ts->get_pendown_state)
515 return ts->get_pendown_state();
517 return !gpio_get_value(ts->gpio_pendown);
520 static void null_wait_for_sync(void)
525 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
526 * to retrieve touchscreen status.
528 * The SPI transfer completion callback does the real work. It reports
529 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
532 static void ads7846_rx(void *ads)
534 struct ads7846 *ts = ads;
535 struct ads7846_packet *packet = ts->packet;
539 /* ads7846_rx_val() did in-place conversion (including byteswap) from
540 * on-the-wire format as part of debouncing to get stable readings.
547 /* range filtering */
551 if (ts->model == 7843) {
552 Rt = ts->pressure_max / 2;
553 } else if (likely(x && z1)) {
554 /* compute touch pressure resistance using equation #2 */
558 Rt *= ts->x_plate_ohms;
560 Rt = (Rt + 2047) >> 12;
565 /* Sample found inconsistent by debouncing or pressure is beyond
566 * the maximum. Don't report it to user space, repeat at least
567 * once more the measurement
569 if (packet->tc.ignore || Rt > ts->pressure_max) {
570 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
571 packet->tc.ignore, Rt);
572 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
577 /* Maybe check the pendown state before reporting. This discards
578 * false readings when the pen is lifted.
580 if (ts->penirq_recheck_delay_usecs) {
581 udelay(ts->penirq_recheck_delay_usecs);
582 if (!get_pendown_state(ts))
586 /* NOTE: We can't rely on the pressure to determine the pen down
587 * state, even this controller has a pressure sensor. The pressure
588 * value can fluctuate for quite a while after lifting the pen and
589 * in some cases may not even settle at the expected value.
591 * The only safe way to check for the pen up condition is in the
592 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
595 struct input_dev *input = ts->input;
598 input_report_key(input, BTN_TOUCH, 1);
600 dev_vdbg(&ts->spi->dev, "DOWN\n");
606 input_report_abs(input, ABS_X, x);
607 input_report_abs(input, ABS_Y, y);
608 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
611 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
614 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
618 static int ads7846_debounce(void *ads, int data_idx, int *val)
620 struct ads7846 *ts = ads;
622 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
623 /* Start over collecting consistent readings. */
625 /* Repeat it, if this was the first read or the read
626 * wasn't consistent enough. */
627 if (ts->read_cnt < ts->debounce_max) {
628 ts->last_read = *val;
630 return ADS7846_FILTER_REPEAT;
632 /* Maximum number of debouncing reached and still
633 * not enough number of consistent readings. Abort
634 * the whole sample, repeat it in the next sampling
638 return ADS7846_FILTER_IGNORE;
641 if (++ts->read_rep > ts->debounce_rep) {
642 /* Got a good reading for this coordinate,
643 * go for the next one. */
646 return ADS7846_FILTER_OK;
648 /* Read more values that are consistent. */
650 return ADS7846_FILTER_REPEAT;
655 static int ads7846_no_filter(void *ads, int data_idx, int *val)
657 return ADS7846_FILTER_OK;
660 static void ads7846_rx_val(void *ads)
662 struct ads7846 *ts = ads;
663 struct ads7846_packet *packet = ts->packet;
664 struct spi_message *m;
665 struct spi_transfer *t;
670 m = &ts->msg[ts->msg_idx];
671 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
673 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
674 * built from two 8 bit values written msb-first.
676 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
678 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
680 case ADS7846_FILTER_REPEAT:
682 case ADS7846_FILTER_IGNORE:
683 packet->tc.ignore = 1;
684 /* Last message will contain ads7846_rx() as the
685 * completion function.
689 case ADS7846_FILTER_OK:
690 *(u16 *)t->rx_buf = val;
691 packet->tc.ignore = 0;
692 m = &ts->msg[++ts->msg_idx];
698 status = spi_async(ts->spi, m);
700 dev_err(&ts->spi->dev, "spi_async --> %d\n",
704 static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
706 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
709 spin_lock(&ts->lock);
711 if (unlikely(!get_pendown_state(ts) ||
712 device_suspended(&ts->spi->dev))) {
714 struct input_dev *input = ts->input;
716 input_report_key(input, BTN_TOUCH, 0);
717 input_report_abs(input, ABS_PRESSURE, 0);
721 dev_vdbg(&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 regulator_disable(ts->reg);
795 /* we know the chip's in lowpower mode since we always
796 * leave it that way after every request
800 /* Must be called with ts->lock held */
801 static void ads7846_enable(struct ads7846 *ts)
806 regulator_enable(ts->reg);
809 ts->irq_disabled = 0;
810 enable_irq(ts->spi->irq);
813 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
815 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
817 spin_lock_irq(&ts->lock);
819 ts->is_suspended = 1;
822 spin_unlock_irq(&ts->lock);
828 static int ads7846_resume(struct spi_device *spi)
830 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
832 spin_lock_irq(&ts->lock);
834 ts->is_suspended = 0;
837 spin_unlock_irq(&ts->lock);
842 static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts)
844 struct ads7846_platform_data *pdata = spi->dev.platform_data;
847 /* REVISIT when the irq can be triggered active-low, or if for some
848 * reason the touchscreen isn't hooked up, we don't need to access
851 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
852 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
856 if (pdata->get_pendown_state) {
857 ts->get_pendown_state = pdata->get_pendown_state;
861 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
863 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
864 pdata->gpio_pendown);
868 ts->gpio_pendown = pdata->gpio_pendown;
872 static int __devinit ads7846_probe(struct spi_device *spi)
875 struct ads7846_packet *packet;
876 struct input_dev *input_dev;
877 struct ads7846_platform_data *pdata = spi->dev.platform_data;
878 struct spi_message *m;
879 struct spi_transfer *x;
884 dev_dbg(&spi->dev, "no IRQ?\n");
889 dev_dbg(&spi->dev, "no platform data?\n");
893 /* don't exceed max specified sample rate */
894 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
895 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
896 (spi->max_speed_hz/SAMPLE_BITS)/1000);
900 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
901 * that even if the hardware can do that, the SPI controller driver
902 * may not. So we stick to very-portable 8 bit words, both RX and TX.
904 spi->bits_per_word = 8;
905 spi->mode = SPI_MODE_0;
906 err = spi_setup(spi);
910 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
911 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
912 input_dev = input_allocate_device();
913 if (!ts || !packet || !input_dev) {
918 dev_set_drvdata(&spi->dev, ts);
922 ts->input = input_dev;
923 ts->vref_mv = pdata->vref_mv;
924 ts->swap_xy = pdata->swap_xy;
926 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
927 ts->timer.function = ads7846_timer;
929 spin_lock_init(&ts->lock);
931 ts->model = pdata->model ? : 7846;
932 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
933 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
934 ts->pressure_max = pdata->pressure_max ? : ~0;
936 if (pdata->filter != NULL) {
937 if (pdata->filter_init != NULL) {
938 err = pdata->filter_init(pdata, &ts->filter_data);
942 ts->filter = pdata->filter;
943 ts->filter_cleanup = pdata->filter_cleanup;
944 } else if (pdata->debounce_max) {
945 ts->debounce_max = pdata->debounce_max;
946 if (ts->debounce_max < 2)
947 ts->debounce_max = 2;
948 ts->debounce_tol = pdata->debounce_tol;
949 ts->debounce_rep = pdata->debounce_rep;
950 ts->filter = ads7846_debounce;
951 ts->filter_data = ts;
953 ts->filter = ads7846_no_filter;
955 err = setup_pendown(spi, ts);
957 goto err_cleanup_filter;
959 if (pdata->penirq_recheck_delay_usecs)
960 ts->penirq_recheck_delay_usecs =
961 pdata->penirq_recheck_delay_usecs;
963 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
965 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
966 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
968 input_dev->name = ts->name;
969 input_dev->phys = ts->phys;
970 input_dev->dev.parent = &spi->dev;
972 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
973 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
974 input_set_abs_params(input_dev, ABS_X,
976 pdata->x_max ? : MAX_12BIT,
978 input_set_abs_params(input_dev, ABS_Y,
980 pdata->y_max ? : MAX_12BIT,
982 input_set_abs_params(input_dev, ABS_PRESSURE,
983 pdata->pressure_min, pdata->pressure_max, 0, 0);
985 vref = pdata->keep_vref_on;
987 /* set up the transfers to read touchscreen state; this assumes we
988 * use formula #2 for pressure, not #3.
995 /* y- still on; turn on only y+ (and ADC) */
996 packet->read_y = READ_Y(vref);
997 x->tx_buf = &packet->read_y;
999 spi_message_add_tail(x, m);
1002 x->rx_buf = &packet->tc.y;
1004 spi_message_add_tail(x, m);
1006 /* the first sample after switching drivers can be low quality;
1007 * optionally discard it, using a second one after the signals
1008 * have had enough time to stabilize.
1010 if (pdata->settle_delay_usecs) {
1011 x->delay_usecs = pdata->settle_delay_usecs;
1014 x->tx_buf = &packet->read_y;
1016 spi_message_add_tail(x, m);
1019 x->rx_buf = &packet->tc.y;
1021 spi_message_add_tail(x, m);
1024 m->complete = ads7846_rx_val;
1028 spi_message_init(m);
1030 /* turn y- off, x+ on, then leave in lowpower */
1032 packet->read_x = READ_X(vref);
1033 x->tx_buf = &packet->read_x;
1035 spi_message_add_tail(x, m);
1038 x->rx_buf = &packet->tc.x;
1040 spi_message_add_tail(x, m);
1042 /* ... maybe discard first sample ... */
1043 if (pdata->settle_delay_usecs) {
1044 x->delay_usecs = pdata->settle_delay_usecs;
1047 x->tx_buf = &packet->read_x;
1049 spi_message_add_tail(x, m);
1052 x->rx_buf = &packet->tc.x;
1054 spi_message_add_tail(x, m);
1057 m->complete = ads7846_rx_val;
1060 /* turn y+ off, x- on; we'll use formula #2 */
1061 if (ts->model == 7846) {
1063 spi_message_init(m);
1066 packet->read_z1 = READ_Z1(vref);
1067 x->tx_buf = &packet->read_z1;
1069 spi_message_add_tail(x, m);
1072 x->rx_buf = &packet->tc.z1;
1074 spi_message_add_tail(x, m);
1076 /* ... maybe discard first sample ... */
1077 if (pdata->settle_delay_usecs) {
1078 x->delay_usecs = pdata->settle_delay_usecs;
1081 x->tx_buf = &packet->read_z1;
1083 spi_message_add_tail(x, m);
1086 x->rx_buf = &packet->tc.z1;
1088 spi_message_add_tail(x, m);
1091 m->complete = ads7846_rx_val;
1095 spi_message_init(m);
1098 packet->read_z2 = READ_Z2(vref);
1099 x->tx_buf = &packet->read_z2;
1101 spi_message_add_tail(x, m);
1104 x->rx_buf = &packet->tc.z2;
1106 spi_message_add_tail(x, m);
1108 /* ... maybe discard first sample ... */
1109 if (pdata->settle_delay_usecs) {
1110 x->delay_usecs = pdata->settle_delay_usecs;
1113 x->tx_buf = &packet->read_z2;
1115 spi_message_add_tail(x, m);
1118 x->rx_buf = &packet->tc.z2;
1120 spi_message_add_tail(x, m);
1123 m->complete = ads7846_rx_val;
1129 spi_message_init(m);
1132 packet->pwrdown = PWRDOWN;
1133 x->tx_buf = &packet->pwrdown;
1135 spi_message_add_tail(x, m);
1138 x->rx_buf = &packet->dummy;
1141 spi_message_add_tail(x, m);
1143 m->complete = ads7846_rx;
1148 ts->reg = regulator_get(&spi->dev, "vcc");
1149 if (IS_ERR(ts->reg)) {
1150 dev_err(&spi->dev, "unable to get regulator: %ld\n",
1155 err = regulator_enable(ts->reg);
1157 dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
1158 goto err_put_regulator;
1161 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
1162 spi->dev.driver->name, ts)) {
1164 "trying pin change workaround on irq %d\n", spi->irq);
1165 err = request_irq(spi->irq, ads7846_irq,
1166 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1167 spi->dev.driver->name, ts);
1169 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1170 goto err_disable_regulator;
1174 err = ads784x_hwmon_register(spi, ts);
1178 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1180 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1181 * the touchscreen, in case it's not connected.
1183 (void) ads7846_read12_ser(&spi->dev,
1184 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1186 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1188 goto err_remove_hwmon;
1190 err = input_register_device(input_dev);
1192 goto err_remove_attr_group;
1196 err_remove_attr_group:
1197 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1199 ads784x_hwmon_unregister(spi, ts);
1201 free_irq(spi->irq, ts);
1202 err_disable_regulator:
1203 regulator_disable(ts->reg);
1205 regulator_put(ts->reg);
1207 if (ts->gpio_pendown != -1)
1208 gpio_free(ts->gpio_pendown);
1210 if (ts->filter_cleanup)
1211 ts->filter_cleanup(ts->filter_data);
1213 input_free_device(input_dev);
1219 static int __devexit ads7846_remove(struct spi_device *spi)
1221 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
1223 ads784x_hwmon_unregister(spi, ts);
1224 input_unregister_device(ts->input);
1226 ads7846_suspend(spi, PMSG_SUSPEND);
1228 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1230 free_irq(ts->spi->irq, ts);
1231 /* suspend left the IRQ disabled */
1232 enable_irq(ts->spi->irq);
1234 regulator_disable(ts->reg);
1235 regulator_put(ts->reg);
1237 if (ts->gpio_pendown != -1)
1238 gpio_free(ts->gpio_pendown);
1240 if (ts->filter_cleanup)
1241 ts->filter_cleanup(ts->filter_data);
1246 dev_dbg(&spi->dev, "unregistered touchscreen\n");
1250 static struct spi_driver ads7846_driver = {
1253 .bus = &spi_bus_type,
1254 .owner = THIS_MODULE,
1256 .probe = ads7846_probe,
1257 .remove = __devexit_p(ads7846_remove),
1258 .suspend = ads7846_suspend,
1259 .resume = ads7846_resume,
1262 static int __init ads7846_init(void)
1264 return spi_register_driver(&ads7846_driver);
1266 module_init(ads7846_init);
1268 static void __exit ads7846_exit(void)
1270 spi_unregister_driver(&ads7846_driver);
1272 module_exit(ads7846_exit);
1274 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1275 MODULE_LICENSE("GPL");
1276 MODULE_ALIAS("spi:ads7846");