GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / input / touchscreen / ads7846.c
blob29c5c681abe9466db007429f08b7854e04cbf287
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/gpio.h>
28 #include <linux/spi/spi.h>
29 #include <linux/spi/ads7846.h>
30 #include <linux/regulator/consumer.h>
31 #include <asm/irq.h>
34 #define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
35 #define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
37 /* this driver doesn't aim at the peak continuous sample rate */
38 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
40 struct ts_event {
41 /* For portability, we can't read 12 bit values using SPI (which
42 * would make the controller deliver them as native byteorder u16
43 * with msbs zeroed). Instead, we read them as two 8-bit values,
44 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
46 u16 x;
47 u16 y;
48 u16 z1, z2;
49 int ignore;
50 u8 x_buf[3];
51 u8 y_buf[3];
55 * We allocate this separately to avoid cache line sharing issues when
56 * driver is used with DMA-based SPI controllers (like atmel_spi) on
57 * systems where main memory is not DMA-coherent (most non-x86 boards).
59 struct ads7846_packet {
60 u8 read_x, read_y, read_z1, read_z2, pwrdown;
61 u16 dummy; /* for the pwrdown read */
62 struct ts_event tc;
63 /* for ads7845 with mpc5121 psc spi we use 3-byte buffers */
64 u8 read_x_cmd[3], read_y_cmd[3], pwrdown_cmd[3];
67 struct ads7846 {
68 struct input_dev *input;
69 char phys[32];
70 char name[32];
72 struct spi_device *spi;
73 struct regulator *reg;
75 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
76 struct attribute_group *attr_group;
77 struct device *hwmon;
78 #endif
80 u16 model;
81 u16 vref_mv;
82 u16 vref_delay_usecs;
83 u16 x_plate_ohms;
84 u16 pressure_max;
86 bool swap_xy;
88 struct ads7846_packet *packet;
90 struct spi_transfer xfer[18];
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 u16 penirq_recheck_delay_usecs;
104 spinlock_t lock;
105 struct hrtimer timer;
106 unsigned pendown:1; /* P: lock */
107 unsigned pending:1; /* P: lock */
108 unsigned irq_disabled:1; /* P: lock */
109 unsigned disabled:1;
110 unsigned is_suspended:1;
112 int (*filter)(void *data, int data_idx, int *val);
113 void *filter_data;
114 void (*filter_cleanup)(void *data);
115 int (*get_pendown_state)(void);
116 int gpio_pendown;
118 void (*wait_for_sync)(void);
121 /* leave chip selected when we're done, for quicker re-select? */
122 #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.
178 struct ser_req {
179 u8 ref_on;
180 u8 command;
181 u8 ref_off;
182 u16 scratch;
183 __be16 sample;
184 struct spi_message msg;
185 struct spi_transfer xfer[6];
188 struct ads7845_ser_req {
189 u8 command[3];
190 u8 pwrdown[3];
191 u8 sample[3];
192 struct spi_message msg;
193 struct spi_transfer xfer[2];
196 static void ads7846_enable(struct ads7846 *ts);
197 static void ads7846_disable(struct ads7846 *ts);
199 static int device_suspended(struct device *dev)
201 struct ads7846 *ts = dev_get_drvdata(dev);
202 return ts->is_suspended || ts->disabled;
205 static int ads7846_read12_ser(struct device *dev, unsigned command)
207 struct spi_device *spi = to_spi_device(dev);
208 struct ads7846 *ts = dev_get_drvdata(dev);
209 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
210 int status;
211 int use_internal;
213 if (!req)
214 return -ENOMEM;
216 spi_message_init(&req->msg);
218 use_internal = (ts->model == 7846);
220 /* maybe turn on internal vREF, and let it settle */
221 if (use_internal) {
222 req->ref_on = REF_ON;
223 req->xfer[0].tx_buf = &req->ref_on;
224 req->xfer[0].len = 1;
225 spi_message_add_tail(&req->xfer[0], &req->msg);
227 req->xfer[1].rx_buf = &req->scratch;
228 req->xfer[1].len = 2;
230 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
231 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
232 spi_message_add_tail(&req->xfer[1], &req->msg);
235 /* take sample */
236 req->command = (u8) command;
237 req->xfer[2].tx_buf = &req->command;
238 req->xfer[2].len = 1;
239 spi_message_add_tail(&req->xfer[2], &req->msg);
241 req->xfer[3].rx_buf = &req->sample;
242 req->xfer[3].len = 2;
243 spi_message_add_tail(&req->xfer[3], &req->msg);
245 /* REVISIT: take a few more samples, and compare ... */
247 /* converter in low power mode & enable PENIRQ */
248 req->ref_off = PWRDOWN;
249 req->xfer[4].tx_buf = &req->ref_off;
250 req->xfer[4].len = 1;
251 spi_message_add_tail(&req->xfer[4], &req->msg);
253 req->xfer[5].rx_buf = &req->scratch;
254 req->xfer[5].len = 2;
255 CS_CHANGE(req->xfer[5]);
256 spi_message_add_tail(&req->xfer[5], &req->msg);
258 ts->irq_disabled = 1;
259 disable_irq(spi->irq);
260 status = spi_sync(spi, &req->msg);
261 ts->irq_disabled = 0;
262 enable_irq(spi->irq);
264 if (status == 0) {
265 /* on-wire is a must-ignore bit, a BE12 value, then padding */
266 status = be16_to_cpu(req->sample);
267 status = status >> 3;
268 status &= 0x0fff;
271 kfree(req);
272 return status;
275 static int ads7845_read12_ser(struct device *dev, unsigned command)
277 struct spi_device *spi = to_spi_device(dev);
278 struct ads7846 *ts = dev_get_drvdata(dev);
279 struct ads7845_ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
280 int status;
282 if (!req)
283 return -ENOMEM;
285 spi_message_init(&req->msg);
287 req->command[0] = (u8) command;
288 req->xfer[0].tx_buf = req->command;
289 req->xfer[0].rx_buf = req->sample;
290 req->xfer[0].len = 3;
291 spi_message_add_tail(&req->xfer[0], &req->msg);
293 ts->irq_disabled = 1;
294 disable_irq(spi->irq);
295 status = spi_sync(spi, &req->msg);
296 ts->irq_disabled = 0;
297 enable_irq(spi->irq);
299 if (status == 0) {
300 /* BE12 value, then padding */
301 status = be16_to_cpu(*((u16 *)&req->sample[1]));
302 status = status >> 3;
303 status &= 0x0fff;
306 kfree(req);
307 return status;
310 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
312 #define SHOW(name, var, adjust) static ssize_t \
313 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
315 struct ads7846 *ts = dev_get_drvdata(dev); \
316 ssize_t v = ads7846_read12_ser(dev, \
317 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
318 if (v < 0) \
319 return v; \
320 return sprintf(buf, "%u\n", adjust(ts, v)); \
322 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
325 /* Sysfs conventions report temperatures in millidegrees Celsius.
326 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
327 * accuracy scheme without calibration data. For now we won't try either;
328 * userspace sees raw sensor values, and must scale/calibrate appropriately.
330 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
332 return v;
335 SHOW(temp0, temp0, null_adjust) /* temp1_input */
336 SHOW(temp1, temp1, null_adjust) /* temp2_input */
339 /* sysfs conventions report voltages in millivolts. We can convert voltages
340 * if we know vREF. userspace may need to scale vAUX to match the board's
341 * external resistors; we assume that vBATT only uses the internal ones.
343 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
345 unsigned retval = v;
347 /* external resistors may scale vAUX into 0..vREF */
348 retval *= ts->vref_mv;
349 retval = retval >> 12;
350 return retval;
353 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
355 unsigned retval = vaux_adjust(ts, v);
357 /* ads7846 has a resistor ladder to scale this signal down */
358 if (ts->model == 7846)
359 retval *= 4;
360 return retval;
363 SHOW(in0_input, vaux, vaux_adjust)
364 SHOW(in1_input, vbatt, vbatt_adjust)
367 static struct attribute *ads7846_attributes[] = {
368 &dev_attr_temp0.attr,
369 &dev_attr_temp1.attr,
370 &dev_attr_in0_input.attr,
371 &dev_attr_in1_input.attr,
372 NULL,
375 static struct attribute_group ads7846_attr_group = {
376 .attrs = ads7846_attributes,
379 static struct attribute *ads7843_attributes[] = {
380 &dev_attr_in0_input.attr,
381 &dev_attr_in1_input.attr,
382 NULL,
385 static struct attribute_group ads7843_attr_group = {
386 .attrs = ads7843_attributes,
389 static struct attribute *ads7845_attributes[] = {
390 &dev_attr_in0_input.attr,
391 NULL,
394 static struct attribute_group ads7845_attr_group = {
395 .attrs = ads7845_attributes,
398 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
400 struct device *hwmon;
401 int err;
403 /* hwmon sensors need a reference voltage */
404 switch (ts->model) {
405 case 7846:
406 if (!ts->vref_mv) {
407 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
408 ts->vref_mv = 2500;
410 break;
411 case 7845:
412 case 7843:
413 if (!ts->vref_mv) {
414 dev_warn(&spi->dev,
415 "external vREF for ADS%d not specified\n",
416 ts->model);
417 return 0;
419 break;
422 /* different chips have different sensor groups */
423 switch (ts->model) {
424 case 7846:
425 ts->attr_group = &ads7846_attr_group;
426 break;
427 case 7845:
428 ts->attr_group = &ads7845_attr_group;
429 break;
430 case 7843:
431 ts->attr_group = &ads7843_attr_group;
432 break;
433 default:
434 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
435 return 0;
438 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
439 if (err)
440 return err;
442 hwmon = hwmon_device_register(&spi->dev);
443 if (IS_ERR(hwmon)) {
444 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
445 return PTR_ERR(hwmon);
448 ts->hwmon = hwmon;
449 return 0;
452 static void ads784x_hwmon_unregister(struct spi_device *spi,
453 struct ads7846 *ts)
455 if (ts->hwmon) {
456 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
457 hwmon_device_unregister(ts->hwmon);
461 #else
462 static inline int ads784x_hwmon_register(struct spi_device *spi,
463 struct ads7846 *ts)
465 return 0;
468 static inline void ads784x_hwmon_unregister(struct spi_device *spi,
469 struct ads7846 *ts)
472 #endif
474 static int is_pen_down(struct device *dev)
476 struct ads7846 *ts = dev_get_drvdata(dev);
478 return ts->pendown;
481 static ssize_t ads7846_pen_down_show(struct device *dev,
482 struct device_attribute *attr, char *buf)
484 return sprintf(buf, "%u\n", is_pen_down(dev));
487 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
489 static ssize_t ads7846_disable_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
492 struct ads7846 *ts = dev_get_drvdata(dev);
494 return sprintf(buf, "%u\n", ts->disabled);
497 static ssize_t ads7846_disable_store(struct device *dev,
498 struct device_attribute *attr,
499 const char *buf, size_t count)
501 struct ads7846 *ts = dev_get_drvdata(dev);
502 unsigned long i;
504 if (strict_strtoul(buf, 10, &i))
505 return -EINVAL;
507 spin_lock_irq(&ts->lock);
509 if (i)
510 ads7846_disable(ts);
511 else
512 ads7846_enable(ts);
514 spin_unlock_irq(&ts->lock);
516 return count;
519 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
521 static struct attribute *ads784x_attributes[] = {
522 &dev_attr_pen_down.attr,
523 &dev_attr_disable.attr,
524 NULL,
527 static struct attribute_group ads784x_attr_group = {
528 .attrs = ads784x_attributes,
531 /*--------------------------------------------------------------------------*/
533 static int get_pendown_state(struct ads7846 *ts)
535 if (ts->get_pendown_state)
536 return ts->get_pendown_state();
538 return !gpio_get_value(ts->gpio_pendown);
541 static void null_wait_for_sync(void)
546 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
547 * to retrieve touchscreen status.
549 * The SPI transfer completion callback does the real work. It reports
550 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
553 static void ads7846_rx(void *ads)
555 struct ads7846 *ts = ads;
556 struct ads7846_packet *packet = ts->packet;
557 unsigned Rt;
558 u16 x, y, z1, z2;
560 /* ads7846_rx_val() did in-place conversion (including byteswap) from
561 * on-the-wire format as part of debouncing to get stable readings.
563 if (ts->model == 7845) {
564 x = *(u16 *)packet->tc.x_buf;
565 y = *(u16 *)packet->tc.y_buf;
566 z1 = 0;
567 z2 = 0;
568 } else {
569 x = packet->tc.x;
570 y = packet->tc.y;
571 z1 = packet->tc.z1;
572 z2 = packet->tc.z2;
575 /* range filtering */
576 if (x == MAX_12BIT)
577 x = 0;
579 if (ts->model == 7843) {
580 Rt = ts->pressure_max / 2;
581 } else if (ts->model == 7845) {
582 if (get_pendown_state(ts))
583 Rt = ts->pressure_max / 2;
584 else
585 Rt = 0;
586 dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
587 } else if (likely(x && z1)) {
588 /* compute touch pressure resistance using equation #2 */
589 Rt = z2;
590 Rt -= z1;
591 Rt *= x;
592 Rt *= ts->x_plate_ohms;
593 Rt /= z1;
594 Rt = (Rt + 2047) >> 12;
595 } else {
596 Rt = 0;
599 /* Sample found inconsistent by debouncing or pressure is beyond
600 * the maximum. Don't report it to user space, repeat at least
601 * once more the measurement
603 if (packet->tc.ignore || Rt > ts->pressure_max) {
604 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
605 packet->tc.ignore, Rt);
606 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
607 HRTIMER_MODE_REL);
608 return;
611 /* Maybe check the pendown state before reporting. This discards
612 * false readings when the pen is lifted.
614 if (ts->penirq_recheck_delay_usecs) {
615 udelay(ts->penirq_recheck_delay_usecs);
616 if (!get_pendown_state(ts))
617 Rt = 0;
620 /* NOTE: We can't rely on the pressure to determine the pen down
621 * state, even this controller has a pressure sensor. The pressure
622 * value can fluctuate for quite a while after lifting the pen and
623 * in some cases may not even settle at the expected value.
625 * The only safe way to check for the pen up condition is in the
626 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
628 if (Rt) {
629 struct input_dev *input = ts->input;
631 if (!ts->pendown) {
632 input_report_key(input, BTN_TOUCH, 1);
633 ts->pendown = 1;
634 dev_vdbg(&ts->spi->dev, "DOWN\n");
637 if (ts->swap_xy)
638 swap(x, y);
640 input_report_abs(input, ABS_X, x);
641 input_report_abs(input, ABS_Y, y);
642 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
644 input_sync(input);
645 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
648 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
649 HRTIMER_MODE_REL);
652 static int ads7846_debounce(void *ads, int data_idx, int *val)
654 struct ads7846 *ts = ads;
656 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
657 /* Start over collecting consistent readings. */
658 ts->read_rep = 0;
659 /* Repeat it, if this was the first read or the read
660 * wasn't consistent enough. */
661 if (ts->read_cnt < ts->debounce_max) {
662 ts->last_read = *val;
663 ts->read_cnt++;
664 return ADS7846_FILTER_REPEAT;
665 } else {
666 /* Maximum number of debouncing reached and still
667 * not enough number of consistent readings. Abort
668 * the whole sample, repeat it in the next sampling
669 * period.
671 ts->read_cnt = 0;
672 return ADS7846_FILTER_IGNORE;
674 } else {
675 if (++ts->read_rep > ts->debounce_rep) {
676 /* Got a good reading for this coordinate,
677 * go for the next one. */
678 ts->read_cnt = 0;
679 ts->read_rep = 0;
680 return ADS7846_FILTER_OK;
681 } else {
682 /* Read more values that are consistent. */
683 ts->read_cnt++;
684 return ADS7846_FILTER_REPEAT;
689 static int ads7846_no_filter(void *ads, int data_idx, int *val)
691 return ADS7846_FILTER_OK;
694 static void ads7846_rx_val(void *ads)
696 struct ads7846 *ts = ads;
697 struct ads7846_packet *packet = ts->packet;
698 struct spi_message *m;
699 struct spi_transfer *t;
700 int val;
701 int action;
702 int status;
704 m = &ts->msg[ts->msg_idx];
705 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
707 if (ts->model == 7845) {
708 val = be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3;
709 } else {
710 /* adjust: on-wire is a must-ignore bit, a BE12 value, then
711 * padding; built from two 8 bit values written msb-first.
713 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
716 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
717 switch (action) {
718 case ADS7846_FILTER_REPEAT:
719 break;
720 case ADS7846_FILTER_IGNORE:
721 packet->tc.ignore = 1;
722 /* Last message will contain ads7846_rx() as the
723 * completion function.
725 m = ts->last_msg;
726 break;
727 case ADS7846_FILTER_OK:
728 *(u16 *)t->rx_buf = val;
729 packet->tc.ignore = 0;
730 m = &ts->msg[++ts->msg_idx];
731 break;
732 default:
733 BUG();
735 ts->wait_for_sync();
736 status = spi_async(ts->spi, m);
737 if (status)
738 dev_err(&ts->spi->dev, "spi_async --> %d\n",
739 status);
742 static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
744 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
745 int status = 0;
747 spin_lock(&ts->lock);
749 if (unlikely(!get_pendown_state(ts) ||
750 device_suspended(&ts->spi->dev))) {
751 if (ts->pendown) {
752 struct input_dev *input = ts->input;
754 input_report_key(input, BTN_TOUCH, 0);
755 input_report_abs(input, ABS_PRESSURE, 0);
756 input_sync(input);
758 ts->pendown = 0;
759 dev_vdbg(&ts->spi->dev, "UP\n");
762 /* measurement cycle ended */
763 if (!device_suspended(&ts->spi->dev)) {
764 ts->irq_disabled = 0;
765 enable_irq(ts->spi->irq);
767 ts->pending = 0;
768 } else {
769 /* pen is still down, continue with the measurement */
770 ts->msg_idx = 0;
771 ts->wait_for_sync();
772 status = spi_async(ts->spi, &ts->msg[0]);
773 if (status)
774 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
777 spin_unlock(&ts->lock);
778 return HRTIMER_NORESTART;
781 static irqreturn_t ads7846_irq(int irq, void *handle)
783 struct ads7846 *ts = handle;
784 unsigned long flags;
786 spin_lock_irqsave(&ts->lock, flags);
787 if (likely(get_pendown_state(ts))) {
788 if (!ts->irq_disabled) {
789 ts->irq_disabled = 1;
790 disable_irq_nosync(ts->spi->irq);
791 ts->pending = 1;
792 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
793 HRTIMER_MODE_REL);
796 spin_unlock_irqrestore(&ts->lock, flags);
798 return IRQ_HANDLED;
801 /*--------------------------------------------------------------------------*/
803 /* Must be called with ts->lock held */
804 static void ads7846_disable(struct ads7846 *ts)
806 if (ts->disabled)
807 return;
809 ts->disabled = 1;
811 /* are we waiting for IRQ, or polling? */
812 if (!ts->pending) {
813 ts->irq_disabled = 1;
814 disable_irq(ts->spi->irq);
815 } else {
816 /* the timer will run at least once more, and
817 * leave everything in a clean state, IRQ disabled
819 while (ts->pending) {
820 spin_unlock_irq(&ts->lock);
821 msleep(1);
822 spin_lock_irq(&ts->lock);
826 regulator_disable(ts->reg);
828 /* we know the chip's in lowpower mode since we always
829 * leave it that way after every request
833 /* Must be called with ts->lock held */
834 static void ads7846_enable(struct ads7846 *ts)
836 if (!ts->disabled)
837 return;
839 regulator_enable(ts->reg);
841 ts->disabled = 0;
842 ts->irq_disabled = 0;
843 enable_irq(ts->spi->irq);
846 static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
848 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
850 spin_lock_irq(&ts->lock);
852 ts->is_suspended = 1;
853 ads7846_disable(ts);
855 spin_unlock_irq(&ts->lock);
857 if (device_may_wakeup(&ts->spi->dev))
858 enable_irq_wake(ts->spi->irq);
860 return 0;
864 static int ads7846_resume(struct spi_device *spi)
866 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
868 if (device_may_wakeup(&ts->spi->dev))
869 disable_irq_wake(ts->spi->irq);
871 spin_lock_irq(&ts->lock);
873 ts->is_suspended = 0;
874 ads7846_enable(ts);
876 spin_unlock_irq(&ts->lock);
878 return 0;
881 static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts)
883 struct ads7846_platform_data *pdata = spi->dev.platform_data;
884 int err;
886 /* REVISIT when the irq can be triggered active-low, or if for some
887 * reason the touchscreen isn't hooked up, we don't need to access
888 * the pendown state.
890 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
891 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
892 return -EINVAL;
895 if (pdata->get_pendown_state) {
896 ts->get_pendown_state = pdata->get_pendown_state;
897 return 0;
900 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
901 if (err) {
902 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
903 pdata->gpio_pendown);
904 return err;
907 ts->gpio_pendown = pdata->gpio_pendown;
908 return 0;
911 static int __devinit ads7846_probe(struct spi_device *spi)
913 struct ads7846 *ts;
914 struct ads7846_packet *packet;
915 struct input_dev *input_dev;
916 const struct ads7846_platform_data *pdata = spi->dev.platform_data;
917 struct spi_message *m;
918 struct spi_transfer *x;
919 unsigned long irq_flags;
920 int vref;
921 int err;
923 if (!spi->irq) {
924 dev_dbg(&spi->dev, "no IRQ?\n");
925 return -ENODEV;
928 if (!pdata) {
929 dev_dbg(&spi->dev, "no platform data?\n");
930 return -ENODEV;
933 /* don't exceed max specified sample rate */
934 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
935 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
936 (spi->max_speed_hz/SAMPLE_BITS)/1000);
937 return -EINVAL;
940 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
941 * that even if the hardware can do that, the SPI controller driver
942 * may not. So we stick to very-portable 8 bit words, both RX and TX.
944 spi->bits_per_word = 8;
945 spi->mode = SPI_MODE_0;
946 err = spi_setup(spi);
947 if (err < 0)
948 return err;
950 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
951 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
952 input_dev = input_allocate_device();
953 if (!ts || !packet || !input_dev) {
954 err = -ENOMEM;
955 goto err_free_mem;
958 dev_set_drvdata(&spi->dev, ts);
960 ts->packet = packet;
961 ts->spi = spi;
962 ts->input = input_dev;
963 ts->vref_mv = pdata->vref_mv;
964 ts->swap_xy = pdata->swap_xy;
966 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
967 ts->timer.function = ads7846_timer;
969 spin_lock_init(&ts->lock);
971 ts->model = pdata->model ? : 7846;
972 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
973 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
974 ts->pressure_max = pdata->pressure_max ? : ~0;
976 if (pdata->filter != NULL) {
977 if (pdata->filter_init != NULL) {
978 err = pdata->filter_init(pdata, &ts->filter_data);
979 if (err < 0)
980 goto err_free_mem;
982 ts->filter = pdata->filter;
983 ts->filter_cleanup = pdata->filter_cleanup;
984 } else if (pdata->debounce_max) {
985 ts->debounce_max = pdata->debounce_max;
986 if (ts->debounce_max < 2)
987 ts->debounce_max = 2;
988 ts->debounce_tol = pdata->debounce_tol;
989 ts->debounce_rep = pdata->debounce_rep;
990 ts->filter = ads7846_debounce;
991 ts->filter_data = ts;
992 } else
993 ts->filter = ads7846_no_filter;
995 err = setup_pendown(spi, ts);
996 if (err)
997 goto err_cleanup_filter;
999 if (pdata->penirq_recheck_delay_usecs)
1000 ts->penirq_recheck_delay_usecs =
1001 pdata->penirq_recheck_delay_usecs;
1003 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1005 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
1006 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
1008 input_dev->name = ts->name;
1009 input_dev->phys = ts->phys;
1010 input_dev->dev.parent = &spi->dev;
1012 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1013 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1014 input_set_abs_params(input_dev, ABS_X,
1015 pdata->x_min ? : 0,
1016 pdata->x_max ? : MAX_12BIT,
1017 0, 0);
1018 input_set_abs_params(input_dev, ABS_Y,
1019 pdata->y_min ? : 0,
1020 pdata->y_max ? : MAX_12BIT,
1021 0, 0);
1022 input_set_abs_params(input_dev, ABS_PRESSURE,
1023 pdata->pressure_min, pdata->pressure_max, 0, 0);
1025 vref = pdata->keep_vref_on;
1027 if (ts->model == 7873) {
1028 /* The AD7873 is almost identical to the ADS7846
1029 * keep VREF off during differential/ratiometric
1030 * conversion modes
1032 ts->model = 7846;
1033 vref = 0;
1036 /* set up the transfers to read touchscreen state; this assumes we
1037 * use formula #2 for pressure, not #3.
1039 m = &ts->msg[0];
1040 x = ts->xfer;
1042 spi_message_init(m);
1044 if (ts->model == 7845) {
1045 packet->read_y_cmd[0] = READ_Y(vref);
1046 packet->read_y_cmd[1] = 0;
1047 packet->read_y_cmd[2] = 0;
1048 x->tx_buf = &packet->read_y_cmd[0];
1049 x->rx_buf = &packet->tc.y_buf[0];
1050 x->len = 3;
1051 spi_message_add_tail(x, m);
1052 } else {
1053 /* y- still on; turn on only y+ (and ADC) */
1054 packet->read_y = READ_Y(vref);
1055 x->tx_buf = &packet->read_y;
1056 x->len = 1;
1057 spi_message_add_tail(x, m);
1059 x++;
1060 x->rx_buf = &packet->tc.y;
1061 x->len = 2;
1062 spi_message_add_tail(x, m);
1065 /* the first sample after switching drivers can be low quality;
1066 * optionally discard it, using a second one after the signals
1067 * have had enough time to stabilize.
1069 if (pdata->settle_delay_usecs) {
1070 x->delay_usecs = pdata->settle_delay_usecs;
1072 x++;
1073 x->tx_buf = &packet->read_y;
1074 x->len = 1;
1075 spi_message_add_tail(x, m);
1077 x++;
1078 x->rx_buf = &packet->tc.y;
1079 x->len = 2;
1080 spi_message_add_tail(x, m);
1083 m->complete = ads7846_rx_val;
1084 m->context = ts;
1086 m++;
1087 spi_message_init(m);
1089 if (ts->model == 7845) {
1090 x++;
1091 packet->read_x_cmd[0] = READ_X(vref);
1092 packet->read_x_cmd[1] = 0;
1093 packet->read_x_cmd[2] = 0;
1094 x->tx_buf = &packet->read_x_cmd[0];
1095 x->rx_buf = &packet->tc.x_buf[0];
1096 x->len = 3;
1097 spi_message_add_tail(x, m);
1098 } else {
1099 /* turn y- off, x+ on, then leave in lowpower */
1100 x++;
1101 packet->read_x = READ_X(vref);
1102 x->tx_buf = &packet->read_x;
1103 x->len = 1;
1104 spi_message_add_tail(x, m);
1106 x++;
1107 x->rx_buf = &packet->tc.x;
1108 x->len = 2;
1109 spi_message_add_tail(x, m);
1112 /* ... maybe discard first sample ... */
1113 if (pdata->settle_delay_usecs) {
1114 x->delay_usecs = pdata->settle_delay_usecs;
1116 x++;
1117 x->tx_buf = &packet->read_x;
1118 x->len = 1;
1119 spi_message_add_tail(x, m);
1121 x++;
1122 x->rx_buf = &packet->tc.x;
1123 x->len = 2;
1124 spi_message_add_tail(x, m);
1127 m->complete = ads7846_rx_val;
1128 m->context = ts;
1130 /* turn y+ off, x- on; we'll use formula #2 */
1131 if (ts->model == 7846) {
1132 m++;
1133 spi_message_init(m);
1135 x++;
1136 packet->read_z1 = READ_Z1(vref);
1137 x->tx_buf = &packet->read_z1;
1138 x->len = 1;
1139 spi_message_add_tail(x, m);
1141 x++;
1142 x->rx_buf = &packet->tc.z1;
1143 x->len = 2;
1144 spi_message_add_tail(x, m);
1146 /* ... maybe discard first sample ... */
1147 if (pdata->settle_delay_usecs) {
1148 x->delay_usecs = pdata->settle_delay_usecs;
1150 x++;
1151 x->tx_buf = &packet->read_z1;
1152 x->len = 1;
1153 spi_message_add_tail(x, m);
1155 x++;
1156 x->rx_buf = &packet->tc.z1;
1157 x->len = 2;
1158 spi_message_add_tail(x, m);
1161 m->complete = ads7846_rx_val;
1162 m->context = ts;
1164 m++;
1165 spi_message_init(m);
1167 x++;
1168 packet->read_z2 = READ_Z2(vref);
1169 x->tx_buf = &packet->read_z2;
1170 x->len = 1;
1171 spi_message_add_tail(x, m);
1173 x++;
1174 x->rx_buf = &packet->tc.z2;
1175 x->len = 2;
1176 spi_message_add_tail(x, m);
1178 /* ... maybe discard first sample ... */
1179 if (pdata->settle_delay_usecs) {
1180 x->delay_usecs = pdata->settle_delay_usecs;
1182 x++;
1183 x->tx_buf = &packet->read_z2;
1184 x->len = 1;
1185 spi_message_add_tail(x, m);
1187 x++;
1188 x->rx_buf = &packet->tc.z2;
1189 x->len = 2;
1190 spi_message_add_tail(x, m);
1193 m->complete = ads7846_rx_val;
1194 m->context = ts;
1197 /* power down */
1198 m++;
1199 spi_message_init(m);
1201 if (ts->model == 7845) {
1202 x++;
1203 packet->pwrdown_cmd[0] = PWRDOWN;
1204 packet->pwrdown_cmd[1] = 0;
1205 packet->pwrdown_cmd[2] = 0;
1206 x->tx_buf = &packet->pwrdown_cmd[0];
1207 x->len = 3;
1208 } else {
1209 x++;
1210 packet->pwrdown = PWRDOWN;
1211 x->tx_buf = &packet->pwrdown;
1212 x->len = 1;
1213 spi_message_add_tail(x, m);
1215 x++;
1216 x->rx_buf = &packet->dummy;
1217 x->len = 2;
1220 CS_CHANGE(*x);
1221 spi_message_add_tail(x, m);
1223 m->complete = ads7846_rx;
1224 m->context = ts;
1226 ts->last_msg = m;
1228 ts->reg = regulator_get(&spi->dev, "vcc");
1229 if (IS_ERR(ts->reg)) {
1230 err = PTR_ERR(ts->reg);
1231 dev_err(&spi->dev, "unable to get regulator: %d\n", err);
1232 goto err_free_gpio;
1235 err = regulator_enable(ts->reg);
1236 if (err) {
1237 dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
1238 goto err_put_regulator;
1241 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
1243 err = request_irq(spi->irq, ads7846_irq, irq_flags,
1244 spi->dev.driver->name, ts);
1246 if (err && !pdata->irq_flags) {
1247 dev_info(&spi->dev,
1248 "trying pin change workaround on irq %d\n", spi->irq);
1249 err = request_irq(spi->irq, ads7846_irq,
1250 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1251 spi->dev.driver->name, ts);
1254 if (err) {
1255 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1256 goto err_disable_regulator;
1259 err = ads784x_hwmon_register(spi, ts);
1260 if (err)
1261 goto err_free_irq;
1263 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1265 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1266 * the touchscreen, in case it's not connected.
1268 if (ts->model == 7845)
1269 ads7845_read12_ser(&spi->dev, PWRDOWN);
1270 else
1271 (void) ads7846_read12_ser(&spi->dev,
1272 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1274 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1275 if (err)
1276 goto err_remove_hwmon;
1278 err = input_register_device(input_dev);
1279 if (err)
1280 goto err_remove_attr_group;
1282 device_init_wakeup(&spi->dev, pdata->wakeup);
1284 return 0;
1286 err_remove_attr_group:
1287 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1288 err_remove_hwmon:
1289 ads784x_hwmon_unregister(spi, ts);
1290 err_free_irq:
1291 free_irq(spi->irq, ts);
1292 err_disable_regulator:
1293 regulator_disable(ts->reg);
1294 err_put_regulator:
1295 regulator_put(ts->reg);
1296 err_free_gpio:
1297 if (ts->gpio_pendown != -1)
1298 gpio_free(ts->gpio_pendown);
1299 err_cleanup_filter:
1300 if (ts->filter_cleanup)
1301 ts->filter_cleanup(ts->filter_data);
1302 err_free_mem:
1303 input_free_device(input_dev);
1304 kfree(packet);
1305 kfree(ts);
1306 return err;
1309 static int __devexit ads7846_remove(struct spi_device *spi)
1311 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
1313 device_init_wakeup(&spi->dev, false);
1315 ads784x_hwmon_unregister(spi, ts);
1316 input_unregister_device(ts->input);
1318 ads7846_suspend(spi, PMSG_SUSPEND);
1320 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1322 free_irq(ts->spi->irq, ts);
1323 /* suspend left the IRQ disabled */
1324 enable_irq(ts->spi->irq);
1326 regulator_disable(ts->reg);
1327 regulator_put(ts->reg);
1329 if (ts->gpio_pendown != -1)
1330 gpio_free(ts->gpio_pendown);
1332 if (ts->filter_cleanup)
1333 ts->filter_cleanup(ts->filter_data);
1335 kfree(ts->packet);
1336 kfree(ts);
1338 dev_dbg(&spi->dev, "unregistered touchscreen\n");
1339 return 0;
1342 static struct spi_driver ads7846_driver = {
1343 .driver = {
1344 .name = "ads7846",
1345 .bus = &spi_bus_type,
1346 .owner = THIS_MODULE,
1348 .probe = ads7846_probe,
1349 .remove = __devexit_p(ads7846_remove),
1350 .suspend = ads7846_suspend,
1351 .resume = ads7846_resume,
1354 static int __init ads7846_init(void)
1356 return spi_register_driver(&ads7846_driver);
1358 module_init(ads7846_init);
1360 static void __exit ads7846_exit(void)
1362 spi_unregister_driver(&ads7846_driver);
1364 module_exit(ads7846_exit);
1366 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1367 MODULE_LICENSE("GPL");
1368 MODULE_ALIAS("spi:ads7846");