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>
32 #include <asm/mach-types.h>
33 #ifdef CONFIG_ARCH_OMAP
34 #include <asm/arch/gpio.h>
40 * This code has been heavily tested on a Nokia 770, and lightly
41 * tested on other ads7846 devices (OSK/Mistral, Lubbock).
42 * Support for ads7843 tested on Atmel at91sam926x-EK.
43 * Support for ads7845 has only been stubbed in.
45 * IRQ handling needs a workaround because of a shortcoming in handling
46 * edge triggered IRQs on some platforms like the OMAP1/2. These
47 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
48 * have to maintain our own SW IRQ disabled status. This should be
49 * removed as soon as the affected platform's IRQ handling is fixed.
51 * app note sbaa036 talks in more detail about accurate sampling...
52 * that ought to help in situations like LCDs inducing noise (which
53 * can also be helped by using synch signals) and more generally.
54 * This driver tries to utilize the measures described in the app
55 * note. The strength of filtering can be set in the board-* specific
59 #define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
60 #define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
62 /* this driver doesn't aim at the peak continuous sample rate */
63 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
66 /* For portability, we can't read 12 bit values using SPI (which
67 * would make the controller deliver them as native byteorder u16
68 * with msbs zeroed). Instead, we read them as two 8-bit values,
69 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
78 struct input_dev *input;
81 struct spi_device *spi;
83 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
84 struct attribute_group *attr_group;
85 struct class_device *hwmon;
93 u8 read_x, read_y, read_z1, read_z2, pwrdown;
94 u16 dummy; /* for the pwrdown read */
97 struct spi_transfer xfer[10];
98 struct spi_message msg[5];
99 struct spi_message *last_msg;
110 struct hrtimer timer;
111 unsigned pendown:1; /* P: lock */
112 unsigned pending:1; /* P: lock */
113 // FIXME remove "irq_disabled"
114 unsigned irq_disabled:1; /* P: lock */
117 int (*filter)(void *data, int data_idx, int *val);
119 void (*filter_cleanup)(void *data);
120 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.
183 static unsigned vREF_mV;
184 module_param(vREF_mV, uint, 0);
185 MODULE_PARM_DESC(vREF_mV, "external vREF voltage, in milliVolts");
193 struct spi_message msg;
194 struct spi_transfer xfer[6];
197 static void ads7846_enable(struct ads7846 *ts);
198 static void ads7846_disable(struct ads7846 *ts);
200 static int device_suspended(struct device *dev)
202 struct ads7846 *ts = dev_get_drvdata(dev);
203 return dev->power.power_state.event != PM_EVENT_ON || ts->disabled;
206 static int ads7846_read12_ser(struct device *dev, unsigned command)
208 struct spi_device *spi = to_spi_device(dev);
209 struct ads7846 *ts = dev_get_drvdata(dev);
210 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
218 spi_message_init(&req->msg);
220 /* FIXME boards with ads7846 might use external vref instead ... */
221 use_internal = (ts->model == 7846);
223 /* maybe turn on internal vREF, and let it settle */
225 req->ref_on = REF_ON;
226 req->xfer[0].tx_buf = &req->ref_on;
227 req->xfer[0].len = 1;
228 spi_message_add_tail(&req->xfer[0], &req->msg);
230 req->xfer[1].rx_buf = &req->scratch;
231 req->xfer[1].len = 2;
233 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
234 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
235 spi_message_add_tail(&req->xfer[1], &req->msg);
239 req->command = (u8) command;
240 req->xfer[2].tx_buf = &req->command;
241 req->xfer[2].len = 1;
242 spi_message_add_tail(&req->xfer[2], &req->msg);
244 req->xfer[3].rx_buf = &req->sample;
245 req->xfer[3].len = 2;
246 spi_message_add_tail(&req->xfer[3], &req->msg);
248 /* REVISIT: take a few more samples, and compare ... */
250 /* converter in low power mode & enable PENIRQ */
251 req->ref_off = PWRDOWN;
252 req->xfer[4].tx_buf = &req->ref_off;
253 req->xfer[4].len = 1;
254 spi_message_add_tail(&req->xfer[4], &req->msg);
256 req->xfer[5].rx_buf = &req->scratch;
257 req->xfer[5].len = 2;
258 CS_CHANGE(req->xfer[5]);
259 spi_message_add_tail(&req->xfer[5], &req->msg);
261 ts->irq_disabled = 1;
262 disable_irq(spi->irq);
263 status = spi_sync(spi, &req->msg);
264 ts->irq_disabled = 0;
265 enable_irq(spi->irq);
268 status = req->msg.status;
270 /* on-wire is a must-ignore bit, a BE12 value, then padding */
271 sample = be16_to_cpu(req->sample);
272 sample = sample >> 3;
276 return status ? status : sample;
279 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
281 #define SHOW(name, var, adjust) static ssize_t \
282 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
284 struct ads7846 *ts = dev_get_drvdata(dev); \
285 ssize_t v = ads7846_read12_ser(dev, \
286 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
289 return sprintf(buf, "%u\n", adjust(ts, v)); \
291 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
294 /* Sysfs conventions report temperatures in millidegrees Celcius.
295 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
296 * accuracy scheme without calibration data. For now we won't try either;
297 * userspace sees raw sensor values, and must scale/calibrate appropriately.
299 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
304 SHOW(temp0, temp0, null_adjust) /* temp1_input */
305 SHOW(temp1, temp1, null_adjust) /* temp2_input */
308 /* sysfs conventions report voltages in millivolts. We can convert voltages
309 * if we know vREF. userspace may need to scale vAUX to match the board's
310 * external resistors; we assume that vBATT only uses the internal ones.
312 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
316 /* external resistors may scale vAUX into 0..vREF */
318 retval = retval >> 12;
322 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
324 unsigned retval = vaux_adjust(ts, v);
326 /* ads7846 has a resistor ladder to scale this signal down */
327 if (ts->model == 7846)
332 SHOW(in0_input, vaux, vaux_adjust)
333 SHOW(in1_input, vbatt, vbatt_adjust)
336 static struct attribute *ads7846_attributes[] = {
337 &dev_attr_temp0.attr,
338 &dev_attr_temp1.attr,
339 &dev_attr_in0_input.attr,
340 &dev_attr_in1_input.attr,
344 static struct attribute_group ads7846_attr_group = {
345 .attrs = ads7846_attributes,
348 static struct attribute *ads7843_attributes[] = {
349 &dev_attr_in0_input.attr,
350 &dev_attr_in1_input.attr,
354 static struct attribute_group ads7843_attr_group = {
355 .attrs = ads7843_attributes,
358 static struct attribute *ads7845_attributes[] = {
359 &dev_attr_in0_input.attr,
363 static struct attribute_group ads7845_attr_group = {
364 .attrs = ads7845_attributes,
367 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
369 struct class_device *hwmon;
372 /* hwmon sensors need a reference voltage */
376 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
384 "external vREF for ADS%d not specified\n",
391 /* different chips have different sensor groups */
394 ts->attr_group = &ads7846_attr_group;
397 ts->attr_group = &ads7845_attr_group;
400 ts->attr_group = &ads7843_attr_group;
403 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
407 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
411 hwmon = hwmon_device_register(&spi->dev);
413 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
414 return PTR_ERR(hwmon);
421 static void ads784x_hwmon_unregister(struct spi_device *spi,
425 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
426 hwmon_device_unregister(ts->hwmon);
431 static inline int ads784x_hwmon_register(struct spi_device *spi,
437 static inline void ads784x_hwmon_unregister(struct spi_device *spi,
443 static int is_pen_down(struct device *dev)
445 struct ads7846 *ts = dev_get_drvdata(dev);
450 static ssize_t ads7846_pen_down_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
453 return sprintf(buf, "%u\n", is_pen_down(dev));
456 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
458 static ssize_t ads7846_disable_show(struct device *dev,
459 struct device_attribute *attr, char *buf)
461 struct ads7846 *ts = dev_get_drvdata(dev);
463 return sprintf(buf, "%u\n", ts->disabled);
466 static ssize_t ads7846_disable_store(struct device *dev,
467 struct device_attribute *attr,
468 const char *buf, size_t count)
470 struct ads7846 *ts = dev_get_drvdata(dev);
474 i = simple_strtoul(buf, &endp, 10);
475 spin_lock_irq(&ts->lock);
482 spin_unlock_irq(&ts->lock);
487 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
489 static struct attribute *ads784x_attributes[] = {
490 &dev_attr_pen_down.attr,
491 &dev_attr_disable.attr,
495 static struct attribute_group ads784x_attr_group = {
496 .attrs = ads784x_attributes,
499 /*--------------------------------------------------------------------------*/
502 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
503 * to retrieve touchscreen status.
505 * The SPI transfer completion callback does the real work. It reports
506 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
509 static void ads7846_rx(void *ads)
511 struct ads7846 *ts = ads;
515 /* ads7846_rx_val() did in-place conversion (including byteswap) from
516 * on-the-wire format as part of debouncing to get stable readings.
523 /* range filtering */
527 if (likely(x && z1)) {
528 /* compute touch pressure resistance using equation #2 */
532 Rt *= ts->x_plate_ohms;
534 Rt = (Rt + 2047) >> 12;
538 if (ts->model == 7843)
539 Rt = ts->pressure_max / 2;
541 /* Sample found inconsistent by debouncing or pressure is beyond
542 * the maximum. Don't report it to user space, repeat at least
543 * once more the measurement
545 if (ts->tc.ignore || Rt > ts->pressure_max) {
547 pr_debug("%s: ignored %d pressure %d\n",
548 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
550 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
555 /* NOTE: We can't rely on the pressure to determine the pen down
556 * state, even this controller has a pressure sensor. The pressure
557 * value can fluctuate for quite a while after lifting the pen and
558 * in some cases may not even settle at the expected value.
560 * The only safe way to check for the pen up condition is in the
561 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
564 struct input_dev *input = ts->input;
567 input_report_key(input, BTN_TOUCH, 1);
570 dev_dbg(&ts->spi->dev, "DOWN\n");
573 input_report_abs(input, ABS_X, x);
574 input_report_abs(input, ABS_Y, y);
575 input_report_abs(input, ABS_PRESSURE, Rt);
579 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
583 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
587 static int ads7846_debounce(void *ads, int data_idx, int *val)
589 struct ads7846 *ts = ads;
591 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
592 /* Start over collecting consistent readings. */
594 /* Repeat it, if this was the first read or the read
595 * wasn't consistent enough. */
596 if (ts->read_cnt < ts->debounce_max) {
597 ts->last_read = *val;
599 return ADS7846_FILTER_REPEAT;
601 /* Maximum number of debouncing reached and still
602 * not enough number of consistent readings. Abort
603 * the whole sample, repeat it in the next sampling
607 return ADS7846_FILTER_IGNORE;
610 if (++ts->read_rep > ts->debounce_rep) {
611 /* Got a good reading for this coordinate,
612 * go for the next one. */
615 return ADS7846_FILTER_OK;
617 /* Read more values that are consistent. */
619 return ADS7846_FILTER_REPEAT;
624 static int ads7846_no_filter(void *ads, int data_idx, int *val)
626 return ADS7846_FILTER_OK;
629 static void ads7846_rx_val(void *ads)
631 struct ads7846 *ts = ads;
632 struct spi_message *m;
633 struct spi_transfer *t;
639 m = &ts->msg[ts->msg_idx];
640 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
643 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
644 * built from two 8 bit values written msb-first.
646 val = be16_to_cpu(*rx_val) >> 3;
648 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
650 case ADS7846_FILTER_REPEAT:
652 case ADS7846_FILTER_IGNORE:
654 /* Last message will contain ads7846_rx() as the
655 * completion function.
659 case ADS7846_FILTER_OK:
662 m = &ts->msg[++ts->msg_idx];
667 status = spi_async(ts->spi, m);
669 dev_err(&ts->spi->dev, "spi_async --> %d\n",
673 static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
675 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
678 spin_lock_irq(&ts->lock);
680 if (unlikely(!ts->get_pendown_state() ||
681 device_suspended(&ts->spi->dev))) {
683 struct input_dev *input = ts->input;
685 input_report_key(input, BTN_TOUCH, 0);
686 input_report_abs(input, ABS_PRESSURE, 0);
691 dev_dbg(&ts->spi->dev, "UP\n");
695 /* measurement cycle ended */
696 if (!device_suspended(&ts->spi->dev)) {
697 ts->irq_disabled = 0;
698 enable_irq(ts->spi->irq);
702 /* pen is still down, continue with the measurement */
704 status = spi_async(ts->spi, &ts->msg[0]);
706 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
709 spin_unlock_irq(&ts->lock);
710 return HRTIMER_NORESTART;
713 static irqreturn_t ads7846_irq(int irq, void *handle)
715 struct ads7846 *ts = handle;
718 spin_lock_irqsave(&ts->lock, flags);
719 if (likely(ts->get_pendown_state())) {
720 if (!ts->irq_disabled) {
721 /* The ARM do_simple_IRQ() dispatcher doesn't act
722 * like the other dispatchers: it will report IRQs
723 * even after they've been disabled. We work around
724 * that here. (The "generic irq" framework may help...)
726 ts->irq_disabled = 1;
727 disable_irq(ts->spi->irq);
729 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
733 spin_unlock_irqrestore(&ts->lock, flags);
738 /*--------------------------------------------------------------------------*/
740 /* Must be called with ts->lock held */
741 static void ads7846_disable(struct ads7846 *ts)
748 /* are we waiting for IRQ, or polling? */
750 ts->irq_disabled = 1;
751 disable_irq(ts->spi->irq);
753 /* the timer will run at least once more, and
754 * leave everything in a clean state, IRQ disabled
756 while (ts->pending) {
757 spin_unlock_irq(&ts->lock);
759 spin_lock_irq(&ts->lock);
763 /* we know the chip's in lowpower mode since we always
764 * leave it that way after every request
769 /* Must be called with ts->lock held */
770 static void ads7846_enable(struct ads7846 *ts)
776 ts->irq_disabled = 0;
777 enable_irq(ts->spi->irq);
780 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
782 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
784 spin_lock_irq(&ts->lock);
786 spi->dev.power.power_state = message;
789 spin_unlock_irq(&ts->lock);
795 static int ads7846_resume(struct spi_device *spi)
797 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
799 spin_lock_irq(&ts->lock);
801 spi->dev.power.power_state = PMSG_ON;
804 spin_unlock_irq(&ts->lock);
809 static int __devinit ads7846_probe(struct spi_device *spi)
812 struct input_dev *input_dev;
813 struct ads7846_platform_data *pdata = spi->dev.platform_data;
814 struct spi_message *m;
815 struct spi_transfer *x;
820 dev_dbg(&spi->dev, "no IRQ?\n");
825 dev_dbg(&spi->dev, "no platform data?\n");
829 /* don't exceed max specified sample rate */
830 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
831 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
832 (spi->max_speed_hz/SAMPLE_BITS)/1000);
836 /* REVISIT when the irq can be triggered active-low, or if for some
837 * reason the touchscreen isn't hooked up, we don't need to access
840 if (pdata->get_pendown_state == NULL) {
841 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
845 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
846 * that even if the hardware can do that, the SPI controller driver
847 * may not. So we stick to very-portable 8 bit words, both RX and TX.
849 spi->bits_per_word = 8;
850 spi->mode = SPI_MODE_1;
851 err = spi_setup(spi);
855 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
856 input_dev = input_allocate_device();
857 if (!ts || !input_dev) {
862 dev_set_drvdata(&spi->dev, ts);
863 spi->dev.power.power_state = PMSG_ON;
866 ts->input = input_dev;
868 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
869 ts->timer.function = ads7846_timer;
871 spin_lock_init(&ts->lock);
873 ts->model = pdata->model ? : 7846;
874 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
875 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
876 ts->pressure_max = pdata->pressure_max ? : ~0;
878 if (pdata->filter != NULL) {
879 if (pdata->filter_init != NULL) {
880 err = pdata->filter_init(pdata, &ts->filter_data);
884 ts->filter = pdata->filter;
885 ts->filter_cleanup = pdata->filter_cleanup;
886 } else if (pdata->debounce_max) {
887 ts->debounce_max = pdata->debounce_max;
888 if (ts->debounce_max < 2)
889 ts->debounce_max = 2;
890 ts->debounce_tol = pdata->debounce_tol;
891 ts->debounce_rep = pdata->debounce_rep;
892 ts->filter = ads7846_debounce;
893 ts->filter_data = ts;
895 ts->filter = ads7846_no_filter;
896 ts->get_pendown_state = pdata->get_pendown_state;
898 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
900 input_dev->name = "ADS784x Touchscreen";
901 input_dev->phys = ts->phys;
902 input_dev->dev.parent = &spi->dev;
904 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
905 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
906 input_set_abs_params(input_dev, ABS_X,
908 pdata->x_max ? : MAX_12BIT,
910 input_set_abs_params(input_dev, ABS_Y,
912 pdata->y_max ? : MAX_12BIT,
914 input_set_abs_params(input_dev, ABS_PRESSURE,
915 pdata->pressure_min, pdata->pressure_max, 0, 0);
917 vref = pdata->keep_vref_on;
919 /* set up the transfers to read touchscreen state; this assumes we
920 * use formula #2 for pressure, not #3.
927 /* y- still on; turn on only y+ (and ADC) */
928 ts->read_y = READ_Y(vref);
929 x->tx_buf = &ts->read_y;
931 spi_message_add_tail(x, m);
934 x->rx_buf = &ts->tc.y;
936 spi_message_add_tail(x, m);
938 m->complete = ads7846_rx_val;
944 /* turn y- off, x+ on, then leave in lowpower */
946 ts->read_x = READ_X(vref);
947 x->tx_buf = &ts->read_x;
949 spi_message_add_tail(x, m);
952 x->rx_buf = &ts->tc.x;
954 spi_message_add_tail(x, m);
956 m->complete = ads7846_rx_val;
959 /* turn y+ off, x- on; we'll use formula #2 */
960 if (ts->model == 7846) {
965 ts->read_z1 = READ_Z1(vref);
966 x->tx_buf = &ts->read_z1;
968 spi_message_add_tail(x, m);
971 x->rx_buf = &ts->tc.z1;
973 spi_message_add_tail(x, m);
975 m->complete = ads7846_rx_val;
982 ts->read_z2 = READ_Z2(vref);
983 x->tx_buf = &ts->read_z2;
985 spi_message_add_tail(x, m);
988 x->rx_buf = &ts->tc.z2;
990 spi_message_add_tail(x, m);
992 m->complete = ads7846_rx_val;
1001 ts->pwrdown = PWRDOWN;
1002 x->tx_buf = &ts->pwrdown;
1004 spi_message_add_tail(x, m);
1007 x->rx_buf = &ts->dummy;
1010 spi_message_add_tail(x, m);
1012 m->complete = ads7846_rx;
1017 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
1018 spi->dev.driver->name, ts)) {
1019 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1021 goto err_cleanup_filter;
1024 err = ads784x_hwmon_register(spi, ts);
1028 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1030 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1031 * the touchscreen, in case it's not connected.
1033 (void) ads7846_read12_ser(&spi->dev,
1034 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1036 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1038 goto err_remove_hwmon;
1040 err = input_register_device(input_dev);
1042 goto err_remove_attr_group;
1046 err_remove_attr_group:
1047 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1049 ads784x_hwmon_unregister(spi, ts);
1051 free_irq(spi->irq, ts);
1053 if (ts->filter_cleanup)
1054 ts->filter_cleanup(ts->filter_data);
1056 input_free_device(input_dev);
1061 static int __devexit ads7846_remove(struct spi_device *spi)
1063 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
1065 ads784x_hwmon_unregister(spi, ts);
1066 input_unregister_device(ts->input);
1068 ads7846_suspend(spi, PMSG_SUSPEND);
1070 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1072 free_irq(ts->spi->irq, ts);
1073 /* suspend left the IRQ disabled */
1074 enable_irq(ts->spi->irq);
1076 if (ts->filter_cleanup)
1077 ts->filter_cleanup(ts->filter_data);
1081 dev_dbg(&spi->dev, "unregistered touchscreen\n");
1085 static struct spi_driver ads7846_driver = {
1088 .bus = &spi_bus_type,
1089 .owner = THIS_MODULE,
1091 .probe = ads7846_probe,
1092 .remove = __devexit_p(ads7846_remove),
1093 .suspend = ads7846_suspend,
1094 .resume = ads7846_resume,
1097 static int __init ads7846_init(void)
1099 /* grr, board-specific init should stay out of drivers!! */
1101 #ifdef CONFIG_ARCH_OMAP
1102 if (machine_is_omap_osk()) {
1103 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
1104 omap_request_gpio(4);
1105 omap_set_gpio_direction(4, 1);
1106 omap_request_gpio(6);
1107 omap_set_gpio_direction(6, 1);
1109 // also TI 1510 Innovator, bitbanging through FPGA
1111 // also Palm Tungsten T2
1115 // also Dell Axim X50
1116 // also HP iPaq H191x/H192x/H415x/H435x
1117 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
1118 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
1120 // Atmel at91sam9261-EK uses ads7843
1122 // also various AMD Au1x00 devel boards
1124 return spi_register_driver(&ads7846_driver);
1126 module_init(ads7846_init);
1128 static void __exit ads7846_exit(void)
1130 spi_unregister_driver(&ads7846_driver);
1132 #ifdef CONFIG_ARCH_OMAP
1133 if (machine_is_omap_osk()) {
1140 module_exit(ads7846_exit);
1142 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1143 MODULE_LICENSE("GPL");