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 and ads7845 has only been stubbed in.
44 * IRQ handling needs a workaround because of a shortcoming in handling
45 * edge triggered IRQs on some platforms like the OMAP1/2. These
46 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
47 * have to maintain our own SW IRQ disabled status. This should be
48 * removed as soon as the affected platform's IRQ handling is fixed.
50 * app note sbaa036 talks in more detail about accurate sampling...
51 * that ought to help in situations like LCDs inducing noise (which
52 * can also be helped by using synch signals) and more generally.
53 * This driver tries to utilize the measures described in the app
54 * note. The strength of filtering can be set in the board-* specific
58 #define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
59 #define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
61 /* this driver doesn't aim at the peak continuous sample rate */
62 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
65 /* For portability, we can't read 12 bit values using SPI (which
66 * would make the controller deliver them as native byteorder u16
67 * with msbs zeroed). Instead, we read them as two 8-bit values,
68 * which need byteswapping then range adjustment.
77 struct input_dev *input;
80 struct spi_device *spi;
81 struct class_device *hwmon;
87 u8 read_x, read_y, read_z1, read_z2, pwrdown;
88 u16 dummy; /* for the pwrdown read */
91 struct spi_transfer xfer[10];
92 struct spi_message msg[5];
93 struct spi_message *last_msg;
104 struct hrtimer timer;
105 unsigned pendown:1; /* P: lock */
106 unsigned pending:1; /* P: lock */
107 // FIXME remove "irq_disabled"
108 unsigned irq_disabled:1; /* P: lock */
111 int (*filter)(void *data, int data_idx, int *val);
113 void (*filter_cleanup)(void *data);
114 int (*get_pendown_state)(void);
117 /* leave chip selected when we're done, for quicker re-select? */
119 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
121 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
124 /*--------------------------------------------------------------------------*/
126 /* The ADS7846 has touchscreen and other sensors.
127 * Earlier ads784x chips are somewhat compatible.
129 #define ADS_START (1 << 7)
130 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
131 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
132 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
133 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
134 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
135 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
136 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
137 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
138 #define ADS_8_BIT (1 << 3)
139 #define ADS_12_BIT (0 << 3)
140 #define ADS_SER (1 << 2) /* non-differential */
141 #define ADS_DFR (0 << 2) /* differential */
142 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
143 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
144 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
145 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
147 #define MAX_12BIT ((1<<12)-1)
149 /* leave ADC powered up (disables penirq) between differential samples */
150 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
151 | ADS_12_BIT | ADS_DFR | \
152 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
154 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
155 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
156 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
158 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
159 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
161 /* single-ended samples need to first power up reference voltage;
162 * we leave both ADC and VREF powered
164 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
165 | ADS_12_BIT | ADS_SER)
167 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
168 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
170 /*--------------------------------------------------------------------------*/
173 * Non-touchscreen sensors only use single-ended conversions.
174 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
175 * ads7846 lets that pin be unconnected, to use internal vREF.
177 * FIXME make external vREF_mV be a module option, and use that as needed...
179 static const unsigned vREF_mV = 2500;
187 struct spi_message msg;
188 struct spi_transfer xfer[6];
191 static void ads7846_enable(struct ads7846 *ts);
192 static void ads7846_disable(struct ads7846 *ts);
194 static int device_suspended(struct device *dev)
196 struct ads7846 *ts = dev_get_drvdata(dev);
197 return dev->power.power_state.event != PM_EVENT_ON || ts->disabled;
200 static int ads7846_read12_ser(struct device *dev, unsigned command)
202 struct spi_device *spi = to_spi_device(dev);
203 struct ads7846 *ts = dev_get_drvdata(dev);
204 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
212 spi_message_init(&req->msg);
214 /* FIXME boards with ads7846 might use external vref instead ... */
215 use_internal = (ts->model == 7846);
217 /* maybe turn on internal vREF, and let it settle */
219 req->ref_on = REF_ON;
220 req->xfer[0].tx_buf = &req->ref_on;
221 req->xfer[0].len = 1;
222 spi_message_add_tail(&req->xfer[0], &req->msg);
224 req->xfer[1].rx_buf = &req->scratch;
225 req->xfer[1].len = 2;
227 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
228 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
229 spi_message_add_tail(&req->xfer[1], &req->msg);
233 req->command = (u8) command;
234 req->xfer[2].tx_buf = &req->command;
235 req->xfer[2].len = 1;
236 spi_message_add_tail(&req->xfer[2], &req->msg);
238 req->xfer[3].rx_buf = &req->sample;
239 req->xfer[3].len = 2;
240 spi_message_add_tail(&req->xfer[3], &req->msg);
242 /* REVISIT: take a few more samples, and compare ... */
244 /* maybe off internal vREF */
246 req->ref_off = REF_OFF;
247 req->xfer[4].tx_buf = &req->ref_off;
248 req->xfer[4].len = 1;
249 spi_message_add_tail(&req->xfer[4], &req->msg);
251 req->xfer[5].rx_buf = &req->scratch;
252 req->xfer[5].len = 2;
253 CS_CHANGE(req->xfer[5]);
254 spi_message_add_tail(&req->xfer[5], &req->msg);
257 ts->irq_disabled = 1;
258 disable_irq(spi->irq);
259 status = spi_sync(spi, &req->msg);
260 ts->irq_disabled = 0;
261 enable_irq(spi->irq);
264 status = req->msg.status;
266 /* on-wire is a must-ignore bit, a BE12 value, then padding */
267 sample = be16_to_cpu(req->sample);
268 sample = sample >> 3;
272 return status ? status : sample;
275 #define SHOW(name,var,adjust) static ssize_t \
276 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
278 struct ads7846 *ts = dev_get_drvdata(dev); \
279 ssize_t v = ads7846_read12_ser(dev, \
280 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
283 return sprintf(buf, "%u\n", adjust(ts, v)); \
285 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
288 /* Sysfs conventions report temperatures in millidegrees Celcius.
289 * We could use the low-accuracy two-sample scheme, but can't do the high
290 * accuracy scheme without calibration data. For now we won't try either;
291 * userspace sees raw sensor values, and must scale appropriately.
293 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
298 SHOW(temp0, temp0, null_adjust) // temp1_input
299 SHOW(temp1, temp1, null_adjust) // temp2_input
302 /* sysfs conventions report voltages in millivolts. We can convert voltages
303 * if we know vREF. userspace may need to scale vAUX to match the board's
304 * external resistors; we assume that vBATT only uses the internal ones.
306 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
310 /* external resistors may scale vAUX into 0..vREF */
312 retval = retval >> 12;
316 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
318 unsigned retval = vaux_adjust(ts, v);
320 /* ads7846 has a resistor ladder to scale this signal down */
321 if (ts->model == 7846)
326 SHOW(in0_input, vaux, vaux_adjust)
327 SHOW(in1_input, vbatt, vbatt_adjust)
330 static int is_pen_down(struct device *dev)
332 struct ads7846 *ts = dev_get_drvdata(dev);
337 static ssize_t ads7846_pen_down_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
340 return sprintf(buf, "%u\n", is_pen_down(dev));
343 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
345 static ssize_t ads7846_disable_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
348 struct ads7846 *ts = dev_get_drvdata(dev);
350 return sprintf(buf, "%u\n", ts->disabled);
353 static ssize_t ads7846_disable_store(struct device *dev,
354 struct device_attribute *attr,
355 const char *buf, size_t count)
357 struct ads7846 *ts = dev_get_drvdata(dev);
361 i = simple_strtoul(buf, &endp, 10);
362 spin_lock_irq(&ts->lock);
369 spin_unlock_irq(&ts->lock);
374 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
376 /*--------------------------------------------------------------------------*/
378 static void ads7846_report_pen_state(struct ads7846 *ts, int down)
380 struct input_dev *input_dev = ts->input;
382 input_report_key(input_dev, BTN_TOUCH, down);
384 input_report_abs(input_dev, ABS_PRESSURE, 0);
386 pr_debug("%s: %s\n", ts->spi->dev.bus_id, down ? "DOWN" : "UP");
390 static void ads7846_report_pen_position(struct ads7846 *ts, int x, int y,
393 struct input_dev *input_dev = ts->input;
395 input_report_abs(input_dev, ABS_X, x);
396 input_report_abs(input_dev, ABS_Y, y);
397 input_report_abs(input_dev, ABS_PRESSURE, pressure);
400 pr_debug("%s: %d/%d/%d\n", ts->spi->dev.bus_id, x, y, pressure);
404 static void ads7846_sync_events(struct ads7846 *ts)
406 struct input_dev *input_dev = ts->input;
408 input_sync(input_dev);
412 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
413 * to retrieve touchscreen status.
415 * The SPI transfer completion callback does the real work. It reports
416 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
419 static void ads7846_rx(void *ads)
421 struct ads7846 *ts = ads;
425 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
426 * built from two 8 bit values written msb-first.
433 /* range filtering */
437 if (likely(x && z1)) {
438 /* compute touch pressure resistance using equation #2 */
442 Rt *= ts->x_plate_ohms;
444 Rt = (Rt + 2047) >> 12;
448 /* Sample found inconsistent by debouncing or pressure is beyond
449 * the maximum. Don't report it to user space, repeat at least
450 * once more the measurement */
451 if (ts->tc.ignore || Rt > ts->pressure_max) {
453 pr_debug("%s: ignored %d pressure %d\n",
454 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
456 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
461 /* NOTE: We can't rely on the pressure to determine the pen down
462 * state. The pressure value can fluctuate for quite a while
463 * after lifting the pen and in some cases may not even settle at
464 * the expected value. The only safe way to check for the pen up
465 * condition is in the timer by reading the pen IRQ state.
469 ads7846_report_pen_state(ts, 1);
472 ads7846_report_pen_position(ts, x, y, Rt);
473 ads7846_sync_events(ts);
476 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD), HRTIMER_REL);
479 static int ads7846_debounce(void *ads, int data_idx, int *val)
481 struct ads7846 *ts = ads;
483 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
484 /* Start over collecting consistent readings. */
486 /* Repeat it, if this was the first read or the read
487 * wasn't consistent enough. */
488 if (ts->read_cnt < ts->debounce_max) {
489 ts->last_read = *val;
491 return ADS7846_FILTER_REPEAT;
493 /* Maximum number of debouncing reached and still
494 * not enough number of consistent readings. Abort
495 * the whole sample, repeat it in the next sampling
499 return ADS7846_FILTER_IGNORE;
502 if (++ts->read_rep > ts->debounce_rep) {
503 /* Got a good reading for this coordinate,
504 * go for the next one. */
507 return ADS7846_FILTER_OK;
509 /* Read more values that are consistent. */
511 return ADS7846_FILTER_REPEAT;
516 static int ads7846_no_filter(void *ads, int data_idx, int *val)
518 return ADS7846_FILTER_OK;
521 static void ads7846_rx_val(void *ads)
523 struct ads7846 *ts = ads;
524 struct spi_message *m;
525 struct spi_transfer *t;
531 m = &ts->msg[ts->msg_idx];
532 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
533 rx_val = (u16 *)t->rx_buf;
534 val = be16_to_cpu(*rx_val) >> 3;
536 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
538 case ADS7846_FILTER_REPEAT:
540 case ADS7846_FILTER_IGNORE:
542 /* Last message will contain ads7846_rx() as the
543 * completion function.
547 case ADS7846_FILTER_OK:
550 m = &ts->msg[++ts->msg_idx];
555 status = spi_async(ts->spi, m);
557 dev_err(&ts->spi->dev, "spi_async --> %d\n",
561 static int ads7846_timer(struct hrtimer *handle)
563 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
566 spin_lock_irq(&ts->lock);
568 if (unlikely(!ts->get_pendown_state() ||
569 device_suspended(&ts->spi->dev))) {
571 ads7846_report_pen_state(ts, 0);
572 ads7846_sync_events(ts);
576 /* measurment cycle ended */
577 if (!device_suspended(&ts->spi->dev)) {
578 ts->irq_disabled = 0;
579 enable_irq(ts->spi->irq);
583 /* pen is still down, continue with the measurement */
585 status = spi_async(ts->spi, &ts->msg[0]);
587 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
590 spin_unlock_irq(&ts->lock);
591 return HRTIMER_NORESTART;
594 static irqreturn_t ads7846_irq(int irq, void *handle)
596 struct ads7846 *ts = handle;
599 spin_lock_irqsave(&ts->lock, flags);
600 if (likely(ts->get_pendown_state())) {
601 if (!ts->irq_disabled) {
602 /* REVISIT irq logic for many ARM chips has cloned a
603 * bug wherein disabling an irq in its handler won't
604 * work;(it's disabled lazily, and too late to work.
605 * until all their irq logic is fixed, we must shadow
608 ts->irq_disabled = 1;
609 disable_irq(ts->spi->irq);
611 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
615 spin_unlock_irqrestore(&ts->lock, flags);
620 /*--------------------------------------------------------------------------*/
622 /* Must be called with ts->lock held */
623 static void ads7846_disable(struct ads7846 *ts)
630 /* are we waiting for IRQ, or polling? */
632 ts->irq_disabled = 1;
633 disable_irq(ts->spi->irq);
635 /* the timer will run at least once more, and
636 * leave everything in a clean state, IRQ disabled
638 while (ts->pending) {
639 spin_unlock_irq(&ts->lock);
641 spin_lock_irq(&ts->lock);
645 /* we know the chip's in lowpower mode since we always
646 * leave it that way after every request
651 /* Must be called with ts->lock held */
652 static void ads7846_enable(struct ads7846 *ts)
658 ts->irq_disabled = 0;
659 enable_irq(ts->spi->irq);
662 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
664 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
666 spin_lock_irq(&ts->lock);
668 spi->dev.power.power_state = message;
671 spin_unlock_irq(&ts->lock);
677 static int ads7846_resume(struct spi_device *spi)
679 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
681 spin_lock_irq(&ts->lock);
683 spi->dev.power.power_state = PMSG_ON;
686 spin_unlock_irq(&ts->lock);
691 static int __devinit ads7846_probe(struct spi_device *spi)
694 struct input_dev *input_dev;
695 struct class_device *hwmon = ERR_PTR(-ENOMEM);
696 struct ads7846_platform_data *pdata = spi->dev.platform_data;
697 struct spi_message *m;
698 struct spi_transfer *x;
703 dev_dbg(&spi->dev, "no IRQ?\n");
708 dev_dbg(&spi->dev, "no platform data?\n");
712 /* don't exceed max specified sample rate */
713 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
714 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
715 (spi->max_speed_hz/SAMPLE_BITS)/1000);
719 if (pdata->get_pendown_state == NULL) {
720 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
724 /* We'd set the wordsize to 12 bits ... except that some controllers
725 * will then treat the 8 bit command words as 12 bits (and drop the
726 * four MSBs of the 12 bit result). Result: inputs must be shifted
727 * to discard the four garbage LSBs. (Also, not all controllers can
728 * support 12 bit words.)
731 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
732 input_dev = input_allocate_device();
733 hwmon = hwmon_device_register(&spi->dev);
734 if (!ts || !input_dev || IS_ERR(hwmon)) {
739 dev_set_drvdata(&spi->dev, ts);
740 spi->dev.power.power_state = PMSG_ON;
741 spi->mode = SPI_MODE_1;
742 err = spi_setup(spi);
747 ts->input = input_dev;
750 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_REL);
751 ts->timer.function = ads7846_timer;
753 spin_lock_init(&ts->lock);
755 ts->model = pdata->model ? : 7846;
756 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
757 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
758 ts->pressure_max = pdata->pressure_max ? : ~0;
760 if (pdata->filter != NULL) {
761 if (pdata->filter_init != NULL) {
762 err = pdata->filter_init(pdata, &ts->filter_data);
766 ts->filter = pdata->filter;
767 ts->filter_cleanup = pdata->filter_cleanup;
768 } else if (pdata->debounce_max) {
769 ts->debounce_max = pdata->debounce_max;
770 if (ts->debounce_max < 2)
771 ts->debounce_max = 2;
772 ts->debounce_tol = pdata->debounce_tol;
773 ts->debounce_rep = pdata->debounce_rep;
774 ts->filter = ads7846_debounce;
775 ts->filter_data = ts;
777 ts->filter = ads7846_no_filter;
778 ts->get_pendown_state = pdata->get_pendown_state;
780 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
782 input_dev->name = "ADS784x Touchscreen";
783 input_dev->phys = ts->phys;
784 input_dev->cdev.dev = &spi->dev;
786 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
787 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
788 input_set_abs_params(input_dev, ABS_X,
790 pdata->x_max ? : MAX_12BIT,
792 input_set_abs_params(input_dev, ABS_Y,
794 pdata->y_max ? : MAX_12BIT,
796 input_set_abs_params(input_dev, ABS_PRESSURE,
797 pdata->pressure_min, pdata->pressure_max, 0, 0);
799 vref = pdata->keep_vref_on;
801 /* set up the transfers to read touchscreen state; this assumes we
802 * use formula #2 for pressure, not #3.
809 /* y- still on; turn on only y+ (and ADC) */
810 ts->read_y = READ_Y(vref);
811 x->tx_buf = &ts->read_y;
813 spi_message_add_tail(x, m);
816 x->rx_buf = &ts->tc.y;
818 spi_message_add_tail(x, m);
820 m->complete = ads7846_rx_val;
826 /* turn y- off, x+ on, then leave in lowpower */
828 ts->read_x = READ_X(vref);
829 x->tx_buf = &ts->read_x;
831 spi_message_add_tail(x, m);
834 x->rx_buf = &ts->tc.x;
836 spi_message_add_tail(x, m);
838 m->complete = ads7846_rx_val;
841 /* turn y+ off, x- on; we'll use formula #2 */
842 if (ts->model == 7846) {
847 ts->read_z1 = READ_Z1(vref);
848 x->tx_buf = &ts->read_z1;
850 spi_message_add_tail(x, m);
853 x->rx_buf = &ts->tc.z1;
855 spi_message_add_tail(x, m);
857 m->complete = ads7846_rx_val;
864 ts->read_z2 = READ_Z2(vref);
865 x->tx_buf = &ts->read_z2;
867 spi_message_add_tail(x, m);
870 x->rx_buf = &ts->tc.z2;
872 spi_message_add_tail(x, m);
874 m->complete = ads7846_rx_val;
883 ts->pwrdown = PWRDOWN;
884 x->tx_buf = &ts->pwrdown;
886 spi_message_add_tail(x, m);
889 x->rx_buf = &ts->dummy;
892 spi_message_add_tail(x, m);
894 m->complete = ads7846_rx;
899 if (request_irq(spi->irq, ads7846_irq,
900 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
901 spi->dev.bus_id, ts)) {
902 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
904 goto err_cleanup_filter;
907 dev_info(&spi->dev, "touchscreen + hwmon, irq %d\n", spi->irq);
909 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
910 * the touchscreen, in case it's not connected.
912 (void) ads7846_read12_ser(&spi->dev,
913 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
915 /* ads7843/7845 don't have temperature sensors, and
916 * use the other ADC lines a bit differently too
918 if (ts->model == 7846) {
919 err = device_create_file(&spi->dev, &dev_attr_temp0);
921 goto err_remove_attr7;
922 err = device_create_file(&spi->dev, &dev_attr_temp1);
924 goto err_remove_attr6;
926 /* in1 == vBAT (7846), or a non-scaled ADC input */
927 if (ts->model != 7845) {
928 err = device_create_file(&spi->dev, &dev_attr_in1_input);
930 goto err_remove_attr5;
932 /* in0 == a non-scaled ADC input */
933 err = device_create_file(&spi->dev, &dev_attr_in0_input);
935 goto err_remove_attr4;
937 /* non-hwmon device attributes */
938 err = device_create_file(&spi->dev, &dev_attr_pen_down);
940 goto err_remove_attr3;
941 err = device_create_file(&spi->dev, &dev_attr_disable);
943 goto err_remove_attr2;
945 err = input_register_device(input_dev);
947 goto err_remove_attr1;
952 device_remove_file(&spi->dev, &dev_attr_disable);
954 device_remove_file(&spi->dev, &dev_attr_pen_down);
956 device_remove_file(&spi->dev, &dev_attr_in0_input);
958 if (ts->model != 7845)
959 device_remove_file(&spi->dev, &dev_attr_in1_input);
961 if (ts->model == 7846) {
962 device_remove_file(&spi->dev, &dev_attr_temp1);
964 device_remove_file(&spi->dev, &dev_attr_temp0);
967 free_irq(spi->irq, ts);
969 if (ts->filter_cleanup)
970 ts->filter_cleanup(ts->filter_data);
973 hwmon_device_unregister(hwmon);
974 input_free_device(input_dev);
979 static int __devexit ads7846_remove(struct spi_device *spi)
981 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
983 hwmon_device_unregister(ts->hwmon);
984 input_unregister_device(ts->input);
986 ads7846_suspend(spi, PMSG_SUSPEND);
988 device_remove_file(&spi->dev, &dev_attr_disable);
989 device_remove_file(&spi->dev, &dev_attr_pen_down);
990 if (ts->model == 7846) {
991 device_remove_file(&spi->dev, &dev_attr_temp1);
992 device_remove_file(&spi->dev, &dev_attr_temp0);
994 if (ts->model != 7845)
995 device_remove_file(&spi->dev, &dev_attr_in1_input);
996 device_remove_file(&spi->dev, &dev_attr_in0_input);
998 free_irq(ts->spi->irq, ts);
999 /* suspend left the IRQ disabled */
1000 enable_irq(ts->spi->irq);
1002 if (ts->filter_cleanup != NULL)
1003 ts->filter_cleanup(ts->filter_data);
1007 dev_dbg(&spi->dev, "unregistered touchscreen\n");
1011 static struct spi_driver ads7846_driver = {
1014 .bus = &spi_bus_type,
1015 .owner = THIS_MODULE,
1017 .probe = ads7846_probe,
1018 .remove = __devexit_p(ads7846_remove),
1019 .suspend = ads7846_suspend,
1020 .resume = ads7846_resume,
1023 static int __init ads7846_init(void)
1025 /* grr, board-specific init should stay out of drivers!! */
1027 #ifdef CONFIG_ARCH_OMAP
1028 if (machine_is_omap_osk()) {
1029 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
1030 omap_request_gpio(4);
1031 omap_set_gpio_direction(4, 1);
1032 omap_request_gpio(6);
1033 omap_set_gpio_direction(6, 1);
1035 // also TI 1510 Innovator, bitbanging through FPGA
1037 // also Palm Tungsten T2
1041 // also Dell Axim X50
1042 // also HP iPaq H191x/H192x/H415x/H435x
1043 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
1044 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
1046 // Atmel at91sam9261-EK uses ads7843
1048 // also various AMD Au1x00 devel boards
1050 return spi_register_driver(&ads7846_driver);
1052 device_initcall(ads7846_init);
1054 static void __exit ads7846_exit(void)
1056 spi_unregister_driver(&ads7846_driver);
1058 #ifdef CONFIG_ARCH_OMAP
1059 if (machine_is_omap_osk()) {
1066 module_exit(ads7846_exit);
1068 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1069 MODULE_LICENSE("GPL");