Input: ads7846 - detect pen up from GPIO state
[linux-2.6/openmoko-kernel.git] / drivers / input / touchscreen / ads7846.c
blobb18c63a3f2abd8aeebb5999f35cbcb59d65e6f27
1 /*
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>
8 * Using code from:
9 * - corgi_ts.c
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/device.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/input.h>
24 #include <linux/interrupt.h>
25 #include <linux/slab.h>
26 #include <linux/spi/spi.h>
27 #include <linux/spi/ads7846.h>
28 #include <asm/irq.h>
30 #ifdef CONFIG_ARM
31 #include <asm/mach-types.h>
32 #ifdef CONFIG_ARCH_OMAP
33 #include <asm/arch/gpio.h>
34 #endif
35 #endif
39 * This code has been heavily tested on a Nokia 770, and lightly
40 * tested on other ads7846 devices (OSK/Mistral, Lubbock).
41 * Support for ads7843 and ads7845 has only been stubbed in.
43 * IRQ handling needs a workaround because of a shortcoming in handling
44 * edge triggered IRQs on some platforms like the OMAP1/2. These
45 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
46 * have to maintain our own SW IRQ disabled status. This should be
47 * removed as soon as the affected platform's IRQ handling is fixed.
49 * app note sbaa036 talks in more detail about accurate sampling...
50 * that ought to help in situations like LCDs inducing noise (which
51 * can also be helped by using synch signals) and more generally.
52 * This driver tries to utilize the measures described in the app
53 * note. The strength of filtering can be set in the board-* specific
54 * files.
57 #define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
58 #define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
60 /* this driver doesn't aim at the peak continuous sample rate */
61 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
63 struct ts_event {
64 /* For portability, we can't read 12 bit values using SPI (which
65 * would make the controller deliver them as native byteorder u16
66 * with msbs zeroed). Instead, we read them as two 8-bit values,
67 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
69 u16 x;
70 u16 y;
71 u16 z1, z2;
72 int ignore;
75 struct ads7846 {
76 struct input_dev *input;
77 char phys[32];
79 struct spi_device *spi;
80 struct attribute_group *attr_group;
81 u16 model;
82 u16 vref_delay_usecs;
83 u16 x_plate_ohms;
84 u16 pressure_max;
86 u8 read_x, read_y, read_z1, read_z2, pwrdown;
87 u16 dummy; /* for the pwrdown read */
88 struct ts_event tc;
90 struct spi_transfer xfer[10];
91 struct spi_message msg[5];
92 struct spi_message *last_msg;
93 int msg_idx;
94 int read_cnt;
95 int read_rep;
96 int last_read;
98 u16 debounce_max;
99 u16 debounce_tol;
100 u16 debounce_rep;
102 spinlock_t lock;
103 struct hrtimer timer;
104 unsigned pendown:1; /* P: lock */
105 unsigned pending:1; /* P: lock */
106 // FIXME remove "irq_disabled"
107 unsigned irq_disabled:1; /* P: lock */
108 unsigned disabled:1;
110 int (*filter)(void *data, int data_idx, int *val);
111 void *filter_data;
112 void (*filter_cleanup)(void *data);
113 int (*get_pendown_state)(void);
116 /* leave chip selected when we're done, for quicker re-select? */
117 #if 0
118 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
119 #else
120 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
121 #endif
123 /*--------------------------------------------------------------------------*/
125 /* The ADS7846 has touchscreen and other sensors.
126 * Earlier ads784x chips are somewhat compatible.
128 #define ADS_START (1 << 7)
129 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
130 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
131 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
132 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
133 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
134 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
135 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
136 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
137 #define ADS_8_BIT (1 << 3)
138 #define ADS_12_BIT (0 << 3)
139 #define ADS_SER (1 << 2) /* non-differential */
140 #define ADS_DFR (0 << 2) /* differential */
141 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
142 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
143 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
144 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
146 #define MAX_12BIT ((1<<12)-1)
148 /* leave ADC powered up (disables penirq) between differential samples */
149 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
150 | ADS_12_BIT | ADS_DFR | \
151 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
153 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
154 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
155 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
157 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
158 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
160 /* single-ended samples need to first power up reference voltage;
161 * we leave both ADC and VREF powered
163 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
164 | ADS_12_BIT | ADS_SER)
166 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
167 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
169 /*--------------------------------------------------------------------------*/
172 * Non-touchscreen sensors only use single-ended conversions.
175 struct ser_req {
176 u8 ref_on;
177 u8 command;
178 u8 ref_off;
179 u16 scratch;
180 __be16 sample;
181 struct spi_message msg;
182 struct spi_transfer xfer[6];
185 static void ads7846_enable(struct ads7846 *ts);
186 static void ads7846_disable(struct ads7846 *ts);
188 static int device_suspended(struct device *dev)
190 struct ads7846 *ts = dev_get_drvdata(dev);
191 return dev->power.power_state.event != PM_EVENT_ON || ts->disabled;
194 static int ads7846_read12_ser(struct device *dev, unsigned command)
196 struct spi_device *spi = to_spi_device(dev);
197 struct ads7846 *ts = dev_get_drvdata(dev);
198 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
199 int status;
200 int sample;
201 int i;
203 if (!req)
204 return -ENOMEM;
206 spi_message_init(&req->msg);
208 /* activate reference, so it has time to settle; */
209 req->ref_on = REF_ON;
210 req->xfer[0].tx_buf = &req->ref_on;
211 req->xfer[0].len = 1;
212 req->xfer[1].rx_buf = &req->scratch;
213 req->xfer[1].len = 2;
216 * for external VREF, 0 usec (and assume it's always on);
217 * for 1uF, use 800 usec;
218 * no cap, 100 usec.
220 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
222 /* take sample */
223 req->command = (u8) command;
224 req->xfer[2].tx_buf = &req->command;
225 req->xfer[2].len = 1;
226 req->xfer[3].rx_buf = &req->sample;
227 req->xfer[3].len = 2;
229 /* REVISIT: take a few more samples, and compare ... */
231 /* turn off reference */
232 req->ref_off = REF_OFF;
233 req->xfer[4].tx_buf = &req->ref_off;
234 req->xfer[4].len = 1;
235 req->xfer[5].rx_buf = &req->scratch;
236 req->xfer[5].len = 2;
238 CS_CHANGE(req->xfer[5]);
240 /* group all the transfers together, so we can't interfere with
241 * reading touchscreen state; disable penirq while sampling
243 for (i = 0; i < 6; i++)
244 spi_message_add_tail(&req->xfer[i], &req->msg);
246 ts->irq_disabled = 1;
247 disable_irq(spi->irq);
248 status = spi_sync(spi, &req->msg);
249 ts->irq_disabled = 0;
250 enable_irq(spi->irq);
252 if (req->msg.status)
253 status = req->msg.status;
255 /* on-wire is a must-ignore bit, a BE12 value, then padding */
256 sample = be16_to_cpu(req->sample);
257 sample = sample >> 3;
258 sample &= 0x0fff;
260 kfree(req);
261 return status ? status : sample;
264 #define SHOW(name) static ssize_t \
265 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
267 ssize_t v = ads7846_read12_ser(dev, \
268 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
269 if (v < 0) \
270 return v; \
271 return sprintf(buf, "%u\n", (unsigned) v); \
273 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
275 SHOW(temp0)
276 SHOW(temp1)
277 SHOW(vaux)
278 SHOW(vbatt)
280 static int is_pen_down(struct device *dev)
282 struct ads7846 *ts = dev_get_drvdata(dev);
284 return ts->pendown;
287 static ssize_t ads7846_pen_down_show(struct device *dev,
288 struct device_attribute *attr, char *buf)
290 return sprintf(buf, "%u\n", is_pen_down(dev));
293 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
295 static ssize_t ads7846_disable_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
298 struct ads7846 *ts = dev_get_drvdata(dev);
300 return sprintf(buf, "%u\n", ts->disabled);
303 static ssize_t ads7846_disable_store(struct device *dev,
304 struct device_attribute *attr,
305 const char *buf, size_t count)
307 struct ads7846 *ts = dev_get_drvdata(dev);
308 char *endp;
309 int i;
311 i = simple_strtoul(buf, &endp, 10);
312 spin_lock_irq(&ts->lock);
314 if (i)
315 ads7846_disable(ts);
316 else
317 ads7846_enable(ts);
319 spin_unlock_irq(&ts->lock);
321 return count;
324 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
326 static struct attribute *ads7846_attributes[] = {
327 &dev_attr_temp0.attr,
328 &dev_attr_temp1.attr,
329 &dev_attr_vbatt.attr,
330 &dev_attr_vaux.attr,
331 &dev_attr_pen_down.attr,
332 &dev_attr_disable.attr,
333 NULL,
336 static struct attribute_group ads7846_attr_group = {
337 .attrs = ads7846_attributes,
341 * ads7843/7845 don't have temperature sensors, and
342 * use the other sensors a bit differently too
345 static struct attribute *ads7843_attributes[] = {
346 &dev_attr_vbatt.attr,
347 &dev_attr_vaux.attr,
348 &dev_attr_pen_down.attr,
349 &dev_attr_disable.attr,
350 NULL,
353 static struct attribute_group ads7843_attr_group = {
354 .attrs = ads7843_attributes,
357 static struct attribute *ads7845_attributes[] = {
358 &dev_attr_vaux.attr,
359 &dev_attr_pen_down.attr,
360 &dev_attr_disable.attr,
361 NULL,
364 static struct attribute_group ads7845_attr_group = {
365 .attrs = ads7845_attributes,
368 /*--------------------------------------------------------------------------*/
371 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
372 * to retrieve touchscreen status.
374 * The SPI transfer completion callback does the real work. It reports
375 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
378 static void ads7846_rx(void *ads)
380 struct ads7846 *ts = ads;
381 unsigned Rt;
382 u16 x, y, z1, z2;
384 /* ads7846_rx_val() did in-place conversion (including byteswap) from
385 * on-the-wire format as part of debouncing to get stable readings.
387 x = ts->tc.x;
388 y = ts->tc.y;
389 z1 = ts->tc.z1;
390 z2 = ts->tc.z2;
392 /* range filtering */
393 if (x == MAX_12BIT)
394 x = 0;
396 if (likely(x && z1)) {
397 /* compute touch pressure resistance using equation #2 */
398 Rt = z2;
399 Rt -= z1;
400 Rt *= x;
401 Rt *= ts->x_plate_ohms;
402 Rt /= z1;
403 Rt = (Rt + 2047) >> 12;
404 } else
405 Rt = 0;
407 /* Sample found inconsistent by debouncing or pressure is beyond
408 * the maximum. Don't report it to user space, repeat at least
409 * once more the measurement
411 if (ts->tc.ignore || Rt > ts->pressure_max) {
412 #ifdef VERBOSE
413 pr_debug("%s: ignored %d pressure %d\n",
414 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
415 #endif
416 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
417 HRTIMER_REL);
418 return;
421 /* NOTE: We can't rely on the pressure to determine the pen down
422 * state, even this controller has a pressure sensor. The pressure
423 * value can fluctuate for quite a while after lifting the pen and
424 * in some cases may not even settle at the expected value.
426 * The only safe way to check for the pen up condition is in the
427 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
429 if (Rt) {
430 struct input_dev *input = ts->input;
432 if (!ts->pendown) {
433 input_report_key(input, BTN_TOUCH, 1);
434 ts->pendown = 1;
435 #ifdef VERBOSE
436 dev_dbg(&ts->spi->dev, "DOWN\n");
437 #endif
439 input_report_abs(input, ABS_X, x);
440 input_report_abs(input, ABS_Y, y);
441 input_report_abs(input, ABS_PRESSURE, Rt);
443 input_sync(input);
444 #ifdef VERBOSE
445 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
446 #endif
449 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD), HRTIMER_REL);
452 static int ads7846_debounce(void *ads, int data_idx, int *val)
454 struct ads7846 *ts = ads;
456 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
457 /* Start over collecting consistent readings. */
458 ts->read_rep = 0;
459 /* Repeat it, if this was the first read or the read
460 * wasn't consistent enough. */
461 if (ts->read_cnt < ts->debounce_max) {
462 ts->last_read = *val;
463 ts->read_cnt++;
464 return ADS7846_FILTER_REPEAT;
465 } else {
466 /* Maximum number of debouncing reached and still
467 * not enough number of consistent readings. Abort
468 * the whole sample, repeat it in the next sampling
469 * period.
471 ts->read_cnt = 0;
472 return ADS7846_FILTER_IGNORE;
474 } else {
475 if (++ts->read_rep > ts->debounce_rep) {
476 /* Got a good reading for this coordinate,
477 * go for the next one. */
478 ts->read_cnt = 0;
479 ts->read_rep = 0;
480 return ADS7846_FILTER_OK;
481 } else {
482 /* Read more values that are consistent. */
483 ts->read_cnt++;
484 return ADS7846_FILTER_REPEAT;
489 static int ads7846_no_filter(void *ads, int data_idx, int *val)
491 return ADS7846_FILTER_OK;
494 static void ads7846_rx_val(void *ads)
496 struct ads7846 *ts = ads;
497 struct spi_message *m;
498 struct spi_transfer *t;
499 u16 *rx_val;
500 int val;
501 int action;
502 int status;
504 m = &ts->msg[ts->msg_idx];
505 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
506 rx_val = t->rx_buf;
508 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
509 * built from two 8 bit values written msb-first.
511 val = be16_to_cpu(*rx_val) >> 3;
513 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
514 switch (action) {
515 case ADS7846_FILTER_REPEAT:
516 break;
517 case ADS7846_FILTER_IGNORE:
518 ts->tc.ignore = 1;
519 /* Last message will contain ads7846_rx() as the
520 * completion function.
522 m = ts->last_msg;
523 break;
524 case ADS7846_FILTER_OK:
525 *rx_val = val;
526 ts->tc.ignore = 0;
527 m = &ts->msg[++ts->msg_idx];
528 break;
529 default:
530 BUG();
532 status = spi_async(ts->spi, m);
533 if (status)
534 dev_err(&ts->spi->dev, "spi_async --> %d\n",
535 status);
538 static int ads7846_timer(struct hrtimer *handle)
540 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
541 int status = 0;
543 spin_lock_irq(&ts->lock);
545 if (unlikely(!ts->get_pendown_state() ||
546 device_suspended(&ts->spi->dev))) {
547 if (ts->pendown) {
548 struct input_dev *input = ts->input;
550 input_report_key(input, BTN_TOUCH, 0);
551 input_report_abs(input, ABS_PRESSURE, 0);
552 input_sync(input);
554 ts->pendown = 0;
555 #ifdef VERBOSE
556 dev_dbg(&ts->spi->dev, "UP\n");
557 #endif
560 /* measurement cycle ended */
561 if (!device_suspended(&ts->spi->dev)) {
562 ts->irq_disabled = 0;
563 enable_irq(ts->spi->irq);
565 ts->pending = 0;
566 } else {
567 /* pen is still down, continue with the measurement */
568 ts->msg_idx = 0;
569 status = spi_async(ts->spi, &ts->msg[0]);
570 if (status)
571 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
574 spin_unlock_irq(&ts->lock);
575 return HRTIMER_NORESTART;
578 static irqreturn_t ads7846_irq(int irq, void *handle)
580 struct ads7846 *ts = handle;
581 unsigned long flags;
583 spin_lock_irqsave(&ts->lock, flags);
584 if (likely(ts->get_pendown_state())) {
585 if (!ts->irq_disabled) {
586 /* The ARM do_simple_IRQ() dispatcher doesn't act
587 * like the other dispatchers: it will report IRQs
588 * even after they've been disabled. We work around
589 * that here. (The "generic irq" framework may help...)
591 ts->irq_disabled = 1;
592 disable_irq(ts->spi->irq);
593 ts->pending = 1;
594 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
595 HRTIMER_REL);
598 spin_unlock_irqrestore(&ts->lock, flags);
600 return IRQ_HANDLED;
603 /*--------------------------------------------------------------------------*/
605 /* Must be called with ts->lock held */
606 static void ads7846_disable(struct ads7846 *ts)
608 if (ts->disabled)
609 return;
611 ts->disabled = 1;
613 /* are we waiting for IRQ, or polling? */
614 if (!ts->pending) {
615 ts->irq_disabled = 1;
616 disable_irq(ts->spi->irq);
617 } else {
618 /* the timer will run at least once more, and
619 * leave everything in a clean state, IRQ disabled
621 while (ts->pending) {
622 spin_unlock_irq(&ts->lock);
623 msleep(1);
624 spin_lock_irq(&ts->lock);
628 /* we know the chip's in lowpower mode since we always
629 * leave it that way after every request
634 /* Must be called with ts->lock held */
635 static void ads7846_enable(struct ads7846 *ts)
637 if (!ts->disabled)
638 return;
640 ts->disabled = 0;
641 ts->irq_disabled = 0;
642 enable_irq(ts->spi->irq);
645 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
647 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
649 spin_lock_irq(&ts->lock);
651 spi->dev.power.power_state = message;
652 ads7846_disable(ts);
654 spin_unlock_irq(&ts->lock);
656 return 0;
660 static int ads7846_resume(struct spi_device *spi)
662 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
664 spin_lock_irq(&ts->lock);
666 spi->dev.power.power_state = PMSG_ON;
667 ads7846_enable(ts);
669 spin_unlock_irq(&ts->lock);
671 return 0;
674 static int __devinit ads7846_probe(struct spi_device *spi)
676 struct ads7846 *ts;
677 struct input_dev *input_dev;
678 struct ads7846_platform_data *pdata = spi->dev.platform_data;
679 struct spi_message *m;
680 struct spi_transfer *x;
681 int vref;
682 int err;
684 if (!spi->irq) {
685 dev_dbg(&spi->dev, "no IRQ?\n");
686 return -ENODEV;
689 if (!pdata) {
690 dev_dbg(&spi->dev, "no platform data?\n");
691 return -ENODEV;
694 /* don't exceed max specified sample rate */
695 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
696 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
697 (spi->max_speed_hz/SAMPLE_BITS)/1000);
698 return -EINVAL;
701 /* REVISIT when the irq can be triggered active-low, or if for some
702 * reason the touchscreen isn't hooked up, we don't need to access
703 * the pendown state.
705 if (pdata->get_pendown_state == NULL) {
706 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
707 return -EINVAL;
710 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
711 * that even if the hardware can do that, the SPI controller driver
712 * may not. So we stick to very-portable 8 bit words, both RX and TX.
714 spi->bits_per_word = 8;
715 spi->mode = SPI_MODE_1;
716 err = spi_setup(spi);
717 if (err < 0)
718 return err;
720 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
721 input_dev = input_allocate_device();
722 if (!ts || !input_dev) {
723 err = -ENOMEM;
724 goto err_free_mem;
727 dev_set_drvdata(&spi->dev, ts);
728 spi->dev.power.power_state = PMSG_ON;
730 ts->spi = spi;
731 ts->input = input_dev;
733 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_REL);
734 ts->timer.function = ads7846_timer;
736 spin_lock_init(&ts->lock);
738 ts->model = pdata->model ? : 7846;
739 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
740 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
741 ts->pressure_max = pdata->pressure_max ? : ~0;
743 if (pdata->filter != NULL) {
744 if (pdata->filter_init != NULL) {
745 err = pdata->filter_init(pdata, &ts->filter_data);
746 if (err < 0)
747 goto err_free_mem;
749 ts->filter = pdata->filter;
750 ts->filter_cleanup = pdata->filter_cleanup;
751 } else if (pdata->debounce_max) {
752 ts->debounce_max = pdata->debounce_max;
753 if (ts->debounce_max < 2)
754 ts->debounce_max = 2;
755 ts->debounce_tol = pdata->debounce_tol;
756 ts->debounce_rep = pdata->debounce_rep;
757 ts->filter = ads7846_debounce;
758 ts->filter_data = ts;
759 } else
760 ts->filter = ads7846_no_filter;
761 ts->get_pendown_state = pdata->get_pendown_state;
763 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
765 input_dev->name = "ADS784x Touchscreen";
766 input_dev->phys = ts->phys;
767 input_dev->cdev.dev = &spi->dev;
769 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
770 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
771 input_set_abs_params(input_dev, ABS_X,
772 pdata->x_min ? : 0,
773 pdata->x_max ? : MAX_12BIT,
774 0, 0);
775 input_set_abs_params(input_dev, ABS_Y,
776 pdata->y_min ? : 0,
777 pdata->y_max ? : MAX_12BIT,
778 0, 0);
779 input_set_abs_params(input_dev, ABS_PRESSURE,
780 pdata->pressure_min, pdata->pressure_max, 0, 0);
782 vref = pdata->keep_vref_on;
784 /* set up the transfers to read touchscreen state; this assumes we
785 * use formula #2 for pressure, not #3.
787 m = &ts->msg[0];
788 x = ts->xfer;
790 spi_message_init(m);
792 /* y- still on; turn on only y+ (and ADC) */
793 ts->read_y = READ_Y(vref);
794 x->tx_buf = &ts->read_y;
795 x->len = 1;
796 spi_message_add_tail(x, m);
798 x++;
799 x->rx_buf = &ts->tc.y;
800 x->len = 2;
801 spi_message_add_tail(x, m);
803 m->complete = ads7846_rx_val;
804 m->context = ts;
806 m++;
807 spi_message_init(m);
809 /* turn y- off, x+ on, then leave in lowpower */
810 x++;
811 ts->read_x = READ_X(vref);
812 x->tx_buf = &ts->read_x;
813 x->len = 1;
814 spi_message_add_tail(x, m);
816 x++;
817 x->rx_buf = &ts->tc.x;
818 x->len = 2;
819 spi_message_add_tail(x, m);
821 m->complete = ads7846_rx_val;
822 m->context = ts;
824 /* turn y+ off, x- on; we'll use formula #2 */
825 if (ts->model == 7846) {
826 m++;
827 spi_message_init(m);
829 x++;
830 ts->read_z1 = READ_Z1(vref);
831 x->tx_buf = &ts->read_z1;
832 x->len = 1;
833 spi_message_add_tail(x, m);
835 x++;
836 x->rx_buf = &ts->tc.z1;
837 x->len = 2;
838 spi_message_add_tail(x, m);
840 m->complete = ads7846_rx_val;
841 m->context = ts;
843 m++;
844 spi_message_init(m);
846 x++;
847 ts->read_z2 = READ_Z2(vref);
848 x->tx_buf = &ts->read_z2;
849 x->len = 1;
850 spi_message_add_tail(x, m);
852 x++;
853 x->rx_buf = &ts->tc.z2;
854 x->len = 2;
855 spi_message_add_tail(x, m);
857 m->complete = ads7846_rx_val;
858 m->context = ts;
861 /* power down */
862 m++;
863 spi_message_init(m);
865 x++;
866 ts->pwrdown = PWRDOWN;
867 x->tx_buf = &ts->pwrdown;
868 x->len = 1;
869 spi_message_add_tail(x, m);
871 x++;
872 x->rx_buf = &ts->dummy;
873 x->len = 2;
874 CS_CHANGE(*x);
875 spi_message_add_tail(x, m);
877 m->complete = ads7846_rx;
878 m->context = ts;
880 ts->last_msg = m;
882 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
883 spi->dev.driver->name, ts)) {
884 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
885 err = -EBUSY;
886 goto err_cleanup_filter;
889 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
891 /* take a first sample, leaving nPENIRQ active; avoid
892 * the touchscreen, in case it's not connected.
894 (void) ads7846_read12_ser(&spi->dev,
895 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
897 switch (ts->model) {
898 case 7846:
899 ts->attr_group = &ads7846_attr_group;
900 break;
901 case 7845:
902 ts->attr_group = &ads7845_attr_group;
903 break;
904 default:
905 ts->attr_group = &ads7843_attr_group;
906 break;
908 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
909 if (err)
910 goto err_free_irq;
912 err = input_register_device(input_dev);
913 if (err)
914 goto err_remove_attr_group;
916 return 0;
918 err_remove_attr_group:
919 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
920 err_free_irq:
921 free_irq(spi->irq, ts);
922 err_cleanup_filter:
923 if (ts->filter_cleanup)
924 ts->filter_cleanup(ts->filter_data);
925 err_free_mem:
926 input_free_device(input_dev);
927 kfree(ts);
928 return err;
931 static int __devexit ads7846_remove(struct spi_device *spi)
933 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
935 input_unregister_device(ts->input);
937 ads7846_suspend(spi, PMSG_SUSPEND);
939 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
941 free_irq(ts->spi->irq, ts);
942 /* suspend left the IRQ disabled */
943 enable_irq(ts->spi->irq);
945 if (ts->filter_cleanup)
946 ts->filter_cleanup(ts->filter_data);
948 kfree(ts);
950 dev_dbg(&spi->dev, "unregistered touchscreen\n");
951 return 0;
954 static struct spi_driver ads7846_driver = {
955 .driver = {
956 .name = "ads7846",
957 .bus = &spi_bus_type,
958 .owner = THIS_MODULE,
960 .probe = ads7846_probe,
961 .remove = __devexit_p(ads7846_remove),
962 .suspend = ads7846_suspend,
963 .resume = ads7846_resume,
966 static int __init ads7846_init(void)
968 /* grr, board-specific init should stay out of drivers!! */
970 #ifdef CONFIG_ARCH_OMAP
971 if (machine_is_omap_osk()) {
972 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
973 omap_request_gpio(4);
974 omap_set_gpio_direction(4, 1);
975 omap_request_gpio(6);
976 omap_set_gpio_direction(6, 1);
978 // also TI 1510 Innovator, bitbanging through FPGA
979 // also Nokia 770
980 // also Palm Tungsten T2
981 #endif
983 // PXA:
984 // also Dell Axim X50
985 // also HP iPaq H191x/H192x/H415x/H435x
986 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
987 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
989 // Atmel at91sam9261-EK uses ads7843
991 // also various AMD Au1x00 devel boards
993 return spi_register_driver(&ads7846_driver);
995 module_init(ads7846_init);
997 static void __exit ads7846_exit(void)
999 spi_unregister_driver(&ads7846_driver);
1001 #ifdef CONFIG_ARCH_OMAP
1002 if (machine_is_omap_osk()) {
1003 omap_free_gpio(4);
1004 omap_free_gpio(6);
1006 #endif
1009 module_exit(ads7846_exit);
1011 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1012 MODULE_LICENSE("GPL");