Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / input / touchscreen / ads7846.c
blob1bd5b5fae84e1f93808fef6c2ce4e52497caf6a6
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/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>
29 #include <asm/irq.h>
31 #ifdef CONFIG_ARM
32 #include <asm/mach-types.h>
33 #ifdef CONFIG_ARCH_OMAP
34 #include <asm/arch/gpio.h>
35 #endif
36 #endif
40 * This code has been heavily tested on a Nokia 770, and lightly
41 * tested on other ads7846 devices (OSK/Mistral, Lubbock).
42 * TSC2046 is just newer ads7846 silicon.
43 * Support for ads7843 tested on Atmel at91sam926x-EK.
44 * Support for ads7845 has only been stubbed in.
46 * IRQ handling needs a workaround because of a shortcoming in handling
47 * edge triggered IRQs on some platforms like the OMAP1/2. These
48 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
49 * have to maintain our own SW IRQ disabled status. This should be
50 * removed as soon as the affected platform's IRQ handling is fixed.
52 * app note sbaa036 talks in more detail about accurate sampling...
53 * that ought to help in situations like LCDs inducing noise (which
54 * can also be helped by using synch signals) and more generally.
55 * This driver tries to utilize the measures described in the app
56 * note. The strength of filtering can be set in the board-* specific
57 * files.
60 #define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
61 #define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
63 /* this driver doesn't aim at the peak continuous sample rate */
64 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
66 struct ts_event {
67 /* For portability, we can't read 12 bit values using SPI (which
68 * would make the controller deliver them as native byteorder u16
69 * with msbs zeroed). Instead, we read them as two 8-bit values,
70 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
72 u16 x;
73 u16 y;
74 u16 z1, z2;
75 int ignore;
78 struct ads7846 {
79 struct input_dev *input;
80 char phys[32];
82 struct spi_device *spi;
84 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
85 struct attribute_group *attr_group;
86 struct device *hwmon;
87 #endif
89 u16 model;
90 u16 vref_delay_usecs;
91 u16 x_plate_ohms;
92 u16 pressure_max;
94 u8 read_x, read_y, read_z1, read_z2, pwrdown;
95 u16 dummy; /* for the pwrdown read */
96 struct ts_event tc;
98 struct spi_transfer xfer[18];
99 struct spi_message msg[5];
100 struct spi_message *last_msg;
101 int msg_idx;
102 int read_cnt;
103 int read_rep;
104 int last_read;
106 u16 debounce_max;
107 u16 debounce_tol;
108 u16 debounce_rep;
110 u16 penirq_recheck_delay_usecs;
112 spinlock_t lock;
113 struct hrtimer timer;
114 unsigned pendown:1; /* P: lock */
115 unsigned pending:1; /* P: lock */
116 // FIXME remove "irq_disabled"
117 unsigned irq_disabled:1; /* P: lock */
118 unsigned disabled:1;
119 unsigned is_suspended:1;
121 int (*filter)(void *data, int data_idx, int *val);
122 void *filter_data;
123 void (*filter_cleanup)(void *data);
124 int (*get_pendown_state)(void);
127 /* leave chip selected when we're done, for quicker re-select? */
128 #if 0
129 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
130 #else
131 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
132 #endif
134 /*--------------------------------------------------------------------------*/
136 /* The ADS7846 has touchscreen and other sensors.
137 * Earlier ads784x chips are somewhat compatible.
139 #define ADS_START (1 << 7)
140 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
141 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
142 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
143 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
144 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
145 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
146 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
147 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
148 #define ADS_8_BIT (1 << 3)
149 #define ADS_12_BIT (0 << 3)
150 #define ADS_SER (1 << 2) /* non-differential */
151 #define ADS_DFR (0 << 2) /* differential */
152 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
153 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
154 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
155 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
157 #define MAX_12BIT ((1<<12)-1)
159 /* leave ADC powered up (disables penirq) between differential samples */
160 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
161 | ADS_12_BIT | ADS_DFR | \
162 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
164 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
165 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
166 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
168 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
169 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
171 /* single-ended samples need to first power up reference voltage;
172 * we leave both ADC and VREF powered
174 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
175 | ADS_12_BIT | ADS_SER)
177 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
178 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
180 /*--------------------------------------------------------------------------*/
183 * Non-touchscreen sensors only use single-ended conversions.
184 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
185 * ads7846 lets that pin be unconnected, to use internal vREF.
187 static unsigned vREF_mV;
188 module_param(vREF_mV, uint, 0);
189 MODULE_PARM_DESC(vREF_mV, "external vREF voltage, in milliVolts");
191 struct ser_req {
192 u8 ref_on;
193 u8 command;
194 u8 ref_off;
195 u16 scratch;
196 __be16 sample;
197 struct spi_message msg;
198 struct spi_transfer xfer[6];
201 static void ads7846_enable(struct ads7846 *ts);
202 static void ads7846_disable(struct ads7846 *ts);
204 static int device_suspended(struct device *dev)
206 struct ads7846 *ts = dev_get_drvdata(dev);
207 return ts->is_suspended || ts->disabled;
210 static int ads7846_read12_ser(struct device *dev, unsigned command)
212 struct spi_device *spi = to_spi_device(dev);
213 struct ads7846 *ts = dev_get_drvdata(dev);
214 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
215 int status;
216 <<<<<<< HEAD:drivers/input/touchscreen/ads7846.c
217 int sample;
218 =======
219 int uninitialized_var(sample);
220 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/input/touchscreen/ads7846.c
221 int use_internal;
223 if (!req)
224 return -ENOMEM;
226 spi_message_init(&req->msg);
228 /* FIXME boards with ads7846 might use external vref instead ... */
229 use_internal = (ts->model == 7846);
231 /* maybe turn on internal vREF, and let it settle */
232 if (use_internal) {
233 req->ref_on = REF_ON;
234 req->xfer[0].tx_buf = &req->ref_on;
235 req->xfer[0].len = 1;
236 spi_message_add_tail(&req->xfer[0], &req->msg);
238 req->xfer[1].rx_buf = &req->scratch;
239 req->xfer[1].len = 2;
241 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
242 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
243 spi_message_add_tail(&req->xfer[1], &req->msg);
246 /* take sample */
247 req->command = (u8) command;
248 req->xfer[2].tx_buf = &req->command;
249 req->xfer[2].len = 1;
250 spi_message_add_tail(&req->xfer[2], &req->msg);
252 req->xfer[3].rx_buf = &req->sample;
253 req->xfer[3].len = 2;
254 spi_message_add_tail(&req->xfer[3], &req->msg);
256 /* REVISIT: take a few more samples, and compare ... */
258 /* converter in low power mode & enable PENIRQ */
259 req->ref_off = PWRDOWN;
260 req->xfer[4].tx_buf = &req->ref_off;
261 req->xfer[4].len = 1;
262 spi_message_add_tail(&req->xfer[4], &req->msg);
264 req->xfer[5].rx_buf = &req->scratch;
265 req->xfer[5].len = 2;
266 CS_CHANGE(req->xfer[5]);
267 spi_message_add_tail(&req->xfer[5], &req->msg);
269 ts->irq_disabled = 1;
270 disable_irq(spi->irq);
271 status = spi_sync(spi, &req->msg);
272 ts->irq_disabled = 0;
273 enable_irq(spi->irq);
275 if (status == 0) {
276 /* on-wire is a must-ignore bit, a BE12 value, then padding */
277 sample = be16_to_cpu(req->sample);
278 sample = sample >> 3;
279 sample &= 0x0fff;
282 kfree(req);
283 return status ? status : sample;
286 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
288 #define SHOW(name, var, adjust) static ssize_t \
289 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
291 struct ads7846 *ts = dev_get_drvdata(dev); \
292 ssize_t v = ads7846_read12_ser(dev, \
293 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
294 if (v < 0) \
295 return v; \
296 return sprintf(buf, "%u\n", adjust(ts, v)); \
298 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
301 /* Sysfs conventions report temperatures in millidegrees Celcius.
302 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
303 * accuracy scheme without calibration data. For now we won't try either;
304 * userspace sees raw sensor values, and must scale/calibrate appropriately.
306 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
308 return v;
311 SHOW(temp0, temp0, null_adjust) /* temp1_input */
312 SHOW(temp1, temp1, null_adjust) /* temp2_input */
315 /* sysfs conventions report voltages in millivolts. We can convert voltages
316 * if we know vREF. userspace may need to scale vAUX to match the board's
317 * external resistors; we assume that vBATT only uses the internal ones.
319 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
321 unsigned retval = v;
323 /* external resistors may scale vAUX into 0..vREF */
324 retval *= vREF_mV;
325 retval = retval >> 12;
326 return retval;
329 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
331 unsigned retval = vaux_adjust(ts, v);
333 /* ads7846 has a resistor ladder to scale this signal down */
334 if (ts->model == 7846)
335 retval *= 4;
336 return retval;
339 SHOW(in0_input, vaux, vaux_adjust)
340 SHOW(in1_input, vbatt, vbatt_adjust)
343 static struct attribute *ads7846_attributes[] = {
344 &dev_attr_temp0.attr,
345 &dev_attr_temp1.attr,
346 &dev_attr_in0_input.attr,
347 &dev_attr_in1_input.attr,
348 NULL,
351 static struct attribute_group ads7846_attr_group = {
352 .attrs = ads7846_attributes,
355 static struct attribute *ads7843_attributes[] = {
356 &dev_attr_in0_input.attr,
357 &dev_attr_in1_input.attr,
358 NULL,
361 static struct attribute_group ads7843_attr_group = {
362 .attrs = ads7843_attributes,
365 static struct attribute *ads7845_attributes[] = {
366 &dev_attr_in0_input.attr,
367 NULL,
370 static struct attribute_group ads7845_attr_group = {
371 .attrs = ads7845_attributes,
374 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
376 struct device *hwmon;
377 int err;
379 /* hwmon sensors need a reference voltage */
380 switch (ts->model) {
381 case 7846:
382 if (!vREF_mV) {
383 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
384 vREF_mV = 2500;
386 break;
387 case 7845:
388 case 7843:
389 if (!vREF_mV) {
390 dev_warn(&spi->dev,
391 "external vREF for ADS%d not specified\n",
392 ts->model);
393 return 0;
395 break;
398 /* different chips have different sensor groups */
399 switch (ts->model) {
400 case 7846:
401 ts->attr_group = &ads7846_attr_group;
402 break;
403 case 7845:
404 ts->attr_group = &ads7845_attr_group;
405 break;
406 case 7843:
407 ts->attr_group = &ads7843_attr_group;
408 break;
409 default:
410 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
411 return 0;
414 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
415 if (err)
416 return err;
418 hwmon = hwmon_device_register(&spi->dev);
419 if (IS_ERR(hwmon)) {
420 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
421 return PTR_ERR(hwmon);
424 ts->hwmon = hwmon;
425 return 0;
428 static void ads784x_hwmon_unregister(struct spi_device *spi,
429 struct ads7846 *ts)
431 if (ts->hwmon) {
432 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
433 hwmon_device_unregister(ts->hwmon);
437 #else
438 static inline int ads784x_hwmon_register(struct spi_device *spi,
439 struct ads7846 *ts)
441 return 0;
444 static inline void ads784x_hwmon_unregister(struct spi_device *spi,
445 struct ads7846 *ts)
448 #endif
450 static int is_pen_down(struct device *dev)
452 struct ads7846 *ts = dev_get_drvdata(dev);
454 return ts->pendown;
457 static ssize_t ads7846_pen_down_show(struct device *dev,
458 struct device_attribute *attr, char *buf)
460 return sprintf(buf, "%u\n", is_pen_down(dev));
463 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
465 static ssize_t ads7846_disable_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct ads7846 *ts = dev_get_drvdata(dev);
470 return sprintf(buf, "%u\n", ts->disabled);
473 static ssize_t ads7846_disable_store(struct device *dev,
474 struct device_attribute *attr,
475 const char *buf, size_t count)
477 struct ads7846 *ts = dev_get_drvdata(dev);
478 char *endp;
479 int i;
481 i = simple_strtoul(buf, &endp, 10);
482 spin_lock_irq(&ts->lock);
484 if (i)
485 ads7846_disable(ts);
486 else
487 ads7846_enable(ts);
489 spin_unlock_irq(&ts->lock);
491 return count;
494 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
496 static struct attribute *ads784x_attributes[] = {
497 &dev_attr_pen_down.attr,
498 &dev_attr_disable.attr,
499 NULL,
502 static struct attribute_group ads784x_attr_group = {
503 .attrs = ads784x_attributes,
506 /*--------------------------------------------------------------------------*/
509 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
510 * to retrieve touchscreen status.
512 * The SPI transfer completion callback does the real work. It reports
513 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
516 static void ads7846_rx(void *ads)
518 struct ads7846 *ts = ads;
519 unsigned Rt;
520 u16 x, y, z1, z2;
522 /* ads7846_rx_val() did in-place conversion (including byteswap) from
523 * on-the-wire format as part of debouncing to get stable readings.
525 x = ts->tc.x;
526 y = ts->tc.y;
527 z1 = ts->tc.z1;
528 z2 = ts->tc.z2;
530 /* range filtering */
531 if (x == MAX_12BIT)
532 x = 0;
534 if (likely(x && z1)) {
535 /* compute touch pressure resistance using equation #2 */
536 Rt = z2;
537 Rt -= z1;
538 Rt *= x;
539 Rt *= ts->x_plate_ohms;
540 Rt /= z1;
541 Rt = (Rt + 2047) >> 12;
542 } else
543 Rt = 0;
545 if (ts->model == 7843)
546 Rt = ts->pressure_max / 2;
548 /* Sample found inconsistent by debouncing or pressure is beyond
549 * the maximum. Don't report it to user space, repeat at least
550 * once more the measurement
552 if (ts->tc.ignore || Rt > ts->pressure_max) {
553 #ifdef VERBOSE
554 pr_debug("%s: ignored %d pressure %d\n",
555 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
556 #endif
557 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
558 HRTIMER_MODE_REL);
559 return;
562 /* Maybe check the pendown state before reporting. This discards
563 * false readings when the pen is lifted.
565 if (ts->penirq_recheck_delay_usecs) {
566 udelay(ts->penirq_recheck_delay_usecs);
567 if (!ts->get_pendown_state())
568 Rt = 0;
571 /* NOTE: We can't rely on the pressure to determine the pen down
572 * state, even this controller has a pressure sensor. The pressure
573 * value can fluctuate for quite a while after lifting the pen and
574 * in some cases may not even settle at the expected value.
576 * The only safe way to check for the pen up condition is in the
577 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
579 if (Rt) {
580 struct input_dev *input = ts->input;
582 if (!ts->pendown) {
583 input_report_key(input, BTN_TOUCH, 1);
584 ts->pendown = 1;
585 #ifdef VERBOSE
586 dev_dbg(&ts->spi->dev, "DOWN\n");
587 #endif
589 input_report_abs(input, ABS_X, x);
590 input_report_abs(input, ABS_Y, y);
591 input_report_abs(input, ABS_PRESSURE, Rt);
593 input_sync(input);
594 #ifdef VERBOSE
595 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
596 #endif
599 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
600 HRTIMER_MODE_REL);
603 static int ads7846_debounce(void *ads, int data_idx, int *val)
605 struct ads7846 *ts = ads;
607 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
608 /* Start over collecting consistent readings. */
609 ts->read_rep = 0;
610 /* Repeat it, if this was the first read or the read
611 * wasn't consistent enough. */
612 if (ts->read_cnt < ts->debounce_max) {
613 ts->last_read = *val;
614 ts->read_cnt++;
615 return ADS7846_FILTER_REPEAT;
616 } else {
617 /* Maximum number of debouncing reached and still
618 * not enough number of consistent readings. Abort
619 * the whole sample, repeat it in the next sampling
620 * period.
622 ts->read_cnt = 0;
623 return ADS7846_FILTER_IGNORE;
625 } else {
626 if (++ts->read_rep > ts->debounce_rep) {
627 /* Got a good reading for this coordinate,
628 * go for the next one. */
629 ts->read_cnt = 0;
630 ts->read_rep = 0;
631 return ADS7846_FILTER_OK;
632 } else {
633 /* Read more values that are consistent. */
634 ts->read_cnt++;
635 return ADS7846_FILTER_REPEAT;
640 static int ads7846_no_filter(void *ads, int data_idx, int *val)
642 return ADS7846_FILTER_OK;
645 static void ads7846_rx_val(void *ads)
647 struct ads7846 *ts = ads;
648 struct spi_message *m;
649 struct spi_transfer *t;
650 u16 *rx_val;
651 int val;
652 int action;
653 int status;
655 m = &ts->msg[ts->msg_idx];
656 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
657 rx_val = t->rx_buf;
659 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
660 * built from two 8 bit values written msb-first.
662 val = be16_to_cpu(*rx_val) >> 3;
664 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
665 switch (action) {
666 case ADS7846_FILTER_REPEAT:
667 break;
668 case ADS7846_FILTER_IGNORE:
669 ts->tc.ignore = 1;
670 /* Last message will contain ads7846_rx() as the
671 * completion function.
673 m = ts->last_msg;
674 break;
675 case ADS7846_FILTER_OK:
676 *rx_val = val;
677 ts->tc.ignore = 0;
678 m = &ts->msg[++ts->msg_idx];
679 break;
680 default:
681 BUG();
683 status = spi_async(ts->spi, m);
684 if (status)
685 dev_err(&ts->spi->dev, "spi_async --> %d\n",
686 status);
689 static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
691 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
692 int status = 0;
694 spin_lock_irq(&ts->lock);
696 if (unlikely(!ts->get_pendown_state() ||
697 device_suspended(&ts->spi->dev))) {
698 if (ts->pendown) {
699 struct input_dev *input = ts->input;
701 input_report_key(input, BTN_TOUCH, 0);
702 input_report_abs(input, ABS_PRESSURE, 0);
703 input_sync(input);
705 ts->pendown = 0;
706 #ifdef VERBOSE
707 dev_dbg(&ts->spi->dev, "UP\n");
708 #endif
711 /* measurement cycle ended */
712 if (!device_suspended(&ts->spi->dev)) {
713 ts->irq_disabled = 0;
714 enable_irq(ts->spi->irq);
716 ts->pending = 0;
717 } else {
718 /* pen is still down, continue with the measurement */
719 ts->msg_idx = 0;
720 status = spi_async(ts->spi, &ts->msg[0]);
721 if (status)
722 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
725 spin_unlock_irq(&ts->lock);
726 return HRTIMER_NORESTART;
729 static irqreturn_t ads7846_irq(int irq, void *handle)
731 struct ads7846 *ts = handle;
732 unsigned long flags;
734 spin_lock_irqsave(&ts->lock, flags);
735 if (likely(ts->get_pendown_state())) {
736 if (!ts->irq_disabled) {
737 /* The ARM do_simple_IRQ() dispatcher doesn't act
738 * like the other dispatchers: it will report IRQs
739 * even after they've been disabled. We work around
740 * that here. (The "generic irq" framework may help...)
742 ts->irq_disabled = 1;
743 disable_irq(ts->spi->irq);
744 ts->pending = 1;
745 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
746 HRTIMER_MODE_REL);
749 spin_unlock_irqrestore(&ts->lock, flags);
751 return IRQ_HANDLED;
754 /*--------------------------------------------------------------------------*/
756 /* Must be called with ts->lock held */
757 static void ads7846_disable(struct ads7846 *ts)
759 if (ts->disabled)
760 return;
762 ts->disabled = 1;
764 /* are we waiting for IRQ, or polling? */
765 if (!ts->pending) {
766 ts->irq_disabled = 1;
767 disable_irq(ts->spi->irq);
768 } else {
769 /* the timer will run at least once more, and
770 * leave everything in a clean state, IRQ disabled
772 while (ts->pending) {
773 spin_unlock_irq(&ts->lock);
774 msleep(1);
775 spin_lock_irq(&ts->lock);
779 /* we know the chip's in lowpower mode since we always
780 * leave it that way after every request
785 /* Must be called with ts->lock held */
786 static void ads7846_enable(struct ads7846 *ts)
788 if (!ts->disabled)
789 return;
791 ts->disabled = 0;
792 ts->irq_disabled = 0;
793 enable_irq(ts->spi->irq);
796 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
798 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
800 spin_lock_irq(&ts->lock);
802 ts->is_suspended = 1;
803 ads7846_disable(ts);
805 spin_unlock_irq(&ts->lock);
807 return 0;
811 static int ads7846_resume(struct spi_device *spi)
813 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
815 spin_lock_irq(&ts->lock);
817 ts->is_suspended = 0;
818 ads7846_enable(ts);
820 spin_unlock_irq(&ts->lock);
822 return 0;
825 static int __devinit ads7846_probe(struct spi_device *spi)
827 struct ads7846 *ts;
828 struct input_dev *input_dev;
829 struct ads7846_platform_data *pdata = spi->dev.platform_data;
830 struct spi_message *m;
831 struct spi_transfer *x;
832 int vref;
833 int err;
835 if (!spi->irq) {
836 dev_dbg(&spi->dev, "no IRQ?\n");
837 return -ENODEV;
840 if (!pdata) {
841 dev_dbg(&spi->dev, "no platform data?\n");
842 return -ENODEV;
845 /* don't exceed max specified sample rate */
846 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
847 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
848 (spi->max_speed_hz/SAMPLE_BITS)/1000);
849 return -EINVAL;
852 /* REVISIT when the irq can be triggered active-low, or if for some
853 * reason the touchscreen isn't hooked up, we don't need to access
854 * the pendown state.
856 if (pdata->get_pendown_state == NULL) {
857 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
858 return -EINVAL;
861 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
862 * that even if the hardware can do that, the SPI controller driver
863 * may not. So we stick to very-portable 8 bit words, both RX and TX.
865 spi->bits_per_word = 8;
866 spi->mode = SPI_MODE_0;
867 err = spi_setup(spi);
868 if (err < 0)
869 return err;
871 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
872 input_dev = input_allocate_device();
873 if (!ts || !input_dev) {
874 err = -ENOMEM;
875 goto err_free_mem;
878 dev_set_drvdata(&spi->dev, ts);
880 ts->spi = spi;
881 ts->input = input_dev;
883 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
884 ts->timer.function = ads7846_timer;
886 spin_lock_init(&ts->lock);
888 ts->model = pdata->model ? : 7846;
889 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
890 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
891 ts->pressure_max = pdata->pressure_max ? : ~0;
893 if (pdata->filter != NULL) {
894 if (pdata->filter_init != NULL) {
895 err = pdata->filter_init(pdata, &ts->filter_data);
896 if (err < 0)
897 goto err_free_mem;
899 ts->filter = pdata->filter;
900 ts->filter_cleanup = pdata->filter_cleanup;
901 } else if (pdata->debounce_max) {
902 ts->debounce_max = pdata->debounce_max;
903 if (ts->debounce_max < 2)
904 ts->debounce_max = 2;
905 ts->debounce_tol = pdata->debounce_tol;
906 ts->debounce_rep = pdata->debounce_rep;
907 ts->filter = ads7846_debounce;
908 ts->filter_data = ts;
909 } else
910 ts->filter = ads7846_no_filter;
911 ts->get_pendown_state = pdata->get_pendown_state;
913 if (pdata->penirq_recheck_delay_usecs)
914 ts->penirq_recheck_delay_usecs =
915 pdata->penirq_recheck_delay_usecs;
917 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
919 input_dev->name = "ADS784x Touchscreen";
920 input_dev->phys = ts->phys;
921 input_dev->dev.parent = &spi->dev;
923 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
924 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
925 input_set_abs_params(input_dev, ABS_X,
926 pdata->x_min ? : 0,
927 pdata->x_max ? : MAX_12BIT,
928 0, 0);
929 input_set_abs_params(input_dev, ABS_Y,
930 pdata->y_min ? : 0,
931 pdata->y_max ? : MAX_12BIT,
932 0, 0);
933 input_set_abs_params(input_dev, ABS_PRESSURE,
934 pdata->pressure_min, pdata->pressure_max, 0, 0);
936 vref = pdata->keep_vref_on;
938 /* set up the transfers to read touchscreen state; this assumes we
939 * use formula #2 for pressure, not #3.
941 m = &ts->msg[0];
942 x = ts->xfer;
944 spi_message_init(m);
946 /* y- still on; turn on only y+ (and ADC) */
947 ts->read_y = READ_Y(vref);
948 x->tx_buf = &ts->read_y;
949 x->len = 1;
950 spi_message_add_tail(x, m);
952 x++;
953 x->rx_buf = &ts->tc.y;
954 x->len = 2;
955 spi_message_add_tail(x, m);
957 /* the first sample after switching drivers can be low quality;
958 * optionally discard it, using a second one after the signals
959 * have had enough time to stabilize.
961 if (pdata->settle_delay_usecs) {
962 x->delay_usecs = pdata->settle_delay_usecs;
964 x++;
965 x->tx_buf = &ts->read_y;
966 x->len = 1;
967 spi_message_add_tail(x, m);
969 x++;
970 x->rx_buf = &ts->tc.y;
971 x->len = 2;
972 spi_message_add_tail(x, m);
975 m->complete = ads7846_rx_val;
976 m->context = ts;
978 m++;
979 spi_message_init(m);
981 /* turn y- off, x+ on, then leave in lowpower */
982 x++;
983 ts->read_x = READ_X(vref);
984 x->tx_buf = &ts->read_x;
985 x->len = 1;
986 spi_message_add_tail(x, m);
988 x++;
989 x->rx_buf = &ts->tc.x;
990 x->len = 2;
991 spi_message_add_tail(x, m);
993 /* ... maybe discard first sample ... */
994 if (pdata->settle_delay_usecs) {
995 x->delay_usecs = pdata->settle_delay_usecs;
997 x++;
998 x->tx_buf = &ts->read_x;
999 x->len = 1;
1000 spi_message_add_tail(x, m);
1002 x++;
1003 x->rx_buf = &ts->tc.x;
1004 x->len = 2;
1005 spi_message_add_tail(x, m);
1008 m->complete = ads7846_rx_val;
1009 m->context = ts;
1011 /* turn y+ off, x- on; we'll use formula #2 */
1012 if (ts->model == 7846) {
1013 m++;
1014 spi_message_init(m);
1016 x++;
1017 ts->read_z1 = READ_Z1(vref);
1018 x->tx_buf = &ts->read_z1;
1019 x->len = 1;
1020 spi_message_add_tail(x, m);
1022 x++;
1023 x->rx_buf = &ts->tc.z1;
1024 x->len = 2;
1025 spi_message_add_tail(x, m);
1027 /* ... maybe discard first sample ... */
1028 if (pdata->settle_delay_usecs) {
1029 x->delay_usecs = pdata->settle_delay_usecs;
1031 x++;
1032 x->tx_buf = &ts->read_z1;
1033 x->len = 1;
1034 spi_message_add_tail(x, m);
1036 x++;
1037 x->rx_buf = &ts->tc.z1;
1038 x->len = 2;
1039 spi_message_add_tail(x, m);
1042 m->complete = ads7846_rx_val;
1043 m->context = ts;
1045 m++;
1046 spi_message_init(m);
1048 x++;
1049 ts->read_z2 = READ_Z2(vref);
1050 x->tx_buf = &ts->read_z2;
1051 x->len = 1;
1052 spi_message_add_tail(x, m);
1054 x++;
1055 x->rx_buf = &ts->tc.z2;
1056 x->len = 2;
1057 spi_message_add_tail(x, m);
1059 /* ... maybe discard first sample ... */
1060 if (pdata->settle_delay_usecs) {
1061 x->delay_usecs = pdata->settle_delay_usecs;
1063 x++;
1064 x->tx_buf = &ts->read_z2;
1065 x->len = 1;
1066 spi_message_add_tail(x, m);
1068 x++;
1069 x->rx_buf = &ts->tc.z2;
1070 x->len = 2;
1071 spi_message_add_tail(x, m);
1074 m->complete = ads7846_rx_val;
1075 m->context = ts;
1078 /* power down */
1079 m++;
1080 spi_message_init(m);
1082 x++;
1083 ts->pwrdown = PWRDOWN;
1084 x->tx_buf = &ts->pwrdown;
1085 x->len = 1;
1086 spi_message_add_tail(x, m);
1088 x++;
1089 x->rx_buf = &ts->dummy;
1090 x->len = 2;
1091 CS_CHANGE(*x);
1092 spi_message_add_tail(x, m);
1094 m->complete = ads7846_rx;
1095 m->context = ts;
1097 ts->last_msg = m;
1099 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
1100 spi->dev.driver->name, ts)) {
1101 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1102 err = -EBUSY;
1103 goto err_cleanup_filter;
1106 err = ads784x_hwmon_register(spi, ts);
1107 if (err)
1108 goto err_free_irq;
1110 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1112 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1113 * the touchscreen, in case it's not connected.
1115 (void) ads7846_read12_ser(&spi->dev,
1116 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1118 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1119 if (err)
1120 goto err_remove_hwmon;
1122 err = input_register_device(input_dev);
1123 if (err)
1124 goto err_remove_attr_group;
1126 return 0;
1128 err_remove_attr_group:
1129 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1130 err_remove_hwmon:
1131 ads784x_hwmon_unregister(spi, ts);
1132 err_free_irq:
1133 free_irq(spi->irq, ts);
1134 err_cleanup_filter:
1135 if (ts->filter_cleanup)
1136 ts->filter_cleanup(ts->filter_data);
1137 err_free_mem:
1138 input_free_device(input_dev);
1139 kfree(ts);
1140 return err;
1143 static int __devexit ads7846_remove(struct spi_device *spi)
1145 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
1147 ads784x_hwmon_unregister(spi, ts);
1148 input_unregister_device(ts->input);
1150 ads7846_suspend(spi, PMSG_SUSPEND);
1152 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1154 free_irq(ts->spi->irq, ts);
1155 /* suspend left the IRQ disabled */
1156 enable_irq(ts->spi->irq);
1158 if (ts->filter_cleanup)
1159 ts->filter_cleanup(ts->filter_data);
1161 kfree(ts);
1163 dev_dbg(&spi->dev, "unregistered touchscreen\n");
1164 return 0;
1167 static struct spi_driver ads7846_driver = {
1168 .driver = {
1169 .name = "ads7846",
1170 .bus = &spi_bus_type,
1171 .owner = THIS_MODULE,
1173 .probe = ads7846_probe,
1174 .remove = __devexit_p(ads7846_remove),
1175 .suspend = ads7846_suspend,
1176 .resume = ads7846_resume,
1179 static int __init ads7846_init(void)
1181 /* grr, board-specific init should stay out of drivers!! */
1183 #ifdef CONFIG_ARCH_OMAP
1184 if (machine_is_omap_osk()) {
1185 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
1186 omap_request_gpio(4);
1187 omap_set_gpio_direction(4, 1);
1188 omap_request_gpio(6);
1189 omap_set_gpio_direction(6, 1);
1191 // also TI 1510 Innovator, bitbanging through FPGA
1192 // also Nokia 770
1193 // also Palm Tungsten T2
1194 #endif
1196 // PXA:
1197 // also Dell Axim X50
1198 // also HP iPaq H191x/H192x/H415x/H435x
1199 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
1200 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
1202 // Atmel at91sam9261-EK uses ads7843
1204 // also various AMD Au1x00 devel boards
1206 return spi_register_driver(&ads7846_driver);
1208 module_init(ads7846_init);
1210 static void __exit ads7846_exit(void)
1212 spi_unregister_driver(&ads7846_driver);
1214 #ifdef CONFIG_ARCH_OMAP
1215 if (machine_is_omap_osk()) {
1216 omap_free_gpio(4);
1217 omap_free_gpio(6);
1219 #endif
1222 module_exit(ads7846_exit);
1224 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1225 MODULE_LICENSE("GPL");