2 * ADS7846 based touchscreen and sensor driver
4 * Copyright (c) 2005 David Brownell
5 * Copyright (c) 2006 Nokia Corporation
6 * Various changes: Imre Deak <imre.deak@nokia.com>
10 * Copyright (C) 2004-2005 Richard Purdie
11 * - omap_ts.[hc], ads7846.h, ts_osk.c
12 * Copyright (C) 2002 MontaVista Software
13 * Copyright (C) 2004 Texas Instruments
14 * Copyright (C) 2005 Dirk Behme
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
20 #include <linux/hwmon.h>
21 #include <linux/init.h>
22 #include <linux/err.h>
23 #include <linux/delay.h>
24 #include <linux/input.h>
25 #include <linux/interrupt.h>
26 #include <linux/slab.h>
27 #include <linux/gpio.h>
28 #include <linux/spi/spi.h>
29 #include <linux/spi/ads7846.h>
30 #include <linux/regulator/consumer.h>
34 * This code has been heavily tested on a Nokia 770, and lightly
35 * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz).
36 * TSC2046 is just newer ads7846 silicon.
37 * Support for ads7843 tested on Atmel at91sam926x-EK.
38 * Support for ads7845 has only been stubbed in.
39 * Support for Analog Devices AD7873 and AD7843 tested.
41 * IRQ handling needs a workaround because of a shortcoming in handling
42 * edge triggered IRQs on some platforms like the OMAP1/2. These
43 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
44 * have to maintain our own SW IRQ disabled status. This should be
45 * removed as soon as the affected platform's IRQ handling is fixed.
47 * App note sbaa036 talks in more detail about accurate sampling...
48 * that ought to help in situations like LCDs inducing noise (which
49 * can also be helped by using synch signals) and more generally.
50 * This driver tries to utilize the measures described in the app
51 * note. The strength of filtering can be set in the board-* specific
55 #define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
56 #define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
58 /* this driver doesn't aim at the peak continuous sample rate */
59 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
62 /* For portability, we can't read 12 bit values using SPI (which
63 * would make the controller deliver them as native byteorder u16
64 * with msbs zeroed). Instead, we read them as two 8-bit values,
65 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
76 * We allocate this separately to avoid cache line sharing issues when
77 * driver is used with DMA-based SPI controllers (like atmel_spi) on
78 * systems where main memory is not DMA-coherent (most non-x86 boards).
80 struct ads7846_packet
{
81 u8 read_x
, read_y
, read_z1
, read_z2
, pwrdown
;
82 u16 dummy
; /* for the pwrdown read */
84 /* for ads7845 with mpc5121 psc spi we use 3-byte buffers */
85 u8 read_x_cmd
[3], read_y_cmd
[3], pwrdown_cmd
[3];
89 struct input_dev
*input
;
93 struct spi_device
*spi
;
94 struct regulator
*reg
;
96 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
97 struct attribute_group
*attr_group
;
103 u16 vref_delay_usecs
;
109 struct ads7846_packet
*packet
;
111 struct spi_transfer xfer
[18];
112 struct spi_message msg
[5];
113 struct spi_message
*last_msg
;
123 u16 penirq_recheck_delay_usecs
;
126 struct hrtimer timer
;
127 unsigned pendown
:1; /* P: lock */
128 unsigned pending
:1; /* P: lock */
129 // FIXME remove "irq_disabled"
130 unsigned irq_disabled
:1; /* P: lock */
132 unsigned is_suspended
:1;
134 int (*filter
)(void *data
, int data_idx
, int *val
);
136 void (*filter_cleanup
)(void *data
);
137 int (*get_pendown_state
)(void);
140 void (*wait_for_sync
)(void);
143 /* leave chip selected when we're done, for quicker re-select? */
145 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
147 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
150 /*--------------------------------------------------------------------------*/
152 /* The ADS7846 has touchscreen and other sensors.
153 * Earlier ads784x chips are somewhat compatible.
155 #define ADS_START (1 << 7)
156 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
157 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
158 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
159 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
160 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
161 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
162 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
163 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
164 #define ADS_8_BIT (1 << 3)
165 #define ADS_12_BIT (0 << 3)
166 #define ADS_SER (1 << 2) /* non-differential */
167 #define ADS_DFR (0 << 2) /* differential */
168 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
169 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
170 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
171 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
173 #define MAX_12BIT ((1<<12)-1)
175 /* leave ADC powered up (disables penirq) between differential samples */
176 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
177 | ADS_12_BIT | ADS_DFR | \
178 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
180 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
181 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
182 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
184 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
185 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
187 /* single-ended samples need to first power up reference voltage;
188 * we leave both ADC and VREF powered
190 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
191 | ADS_12_BIT | ADS_SER)
193 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
194 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
196 /*--------------------------------------------------------------------------*/
199 * Non-touchscreen sensors only use single-ended conversions.
200 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
201 * ads7846 lets that pin be unconnected, to use internal vREF.
210 struct spi_message msg
;
211 struct spi_transfer xfer
[6];
214 struct ads7845_ser_req
{
218 struct spi_message msg
;
219 struct spi_transfer xfer
[2];
222 static void ads7846_enable(struct ads7846
*ts
);
223 static void ads7846_disable(struct ads7846
*ts
);
225 static int device_suspended(struct device
*dev
)
227 struct ads7846
*ts
= dev_get_drvdata(dev
);
228 return ts
->is_suspended
|| ts
->disabled
;
231 static int ads7846_read12_ser(struct device
*dev
, unsigned command
)
233 struct spi_device
*spi
= to_spi_device(dev
);
234 struct ads7846
*ts
= dev_get_drvdata(dev
);
235 struct ser_req
*req
= kzalloc(sizeof *req
, GFP_KERNEL
);
242 spi_message_init(&req
->msg
);
244 /* FIXME boards with ads7846 might use external vref instead ... */
245 use_internal
= (ts
->model
== 7846);
247 /* maybe turn on internal vREF, and let it settle */
249 req
->ref_on
= REF_ON
;
250 req
->xfer
[0].tx_buf
= &req
->ref_on
;
251 req
->xfer
[0].len
= 1;
252 spi_message_add_tail(&req
->xfer
[0], &req
->msg
);
254 req
->xfer
[1].rx_buf
= &req
->scratch
;
255 req
->xfer
[1].len
= 2;
257 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
258 req
->xfer
[1].delay_usecs
= ts
->vref_delay_usecs
;
259 spi_message_add_tail(&req
->xfer
[1], &req
->msg
);
263 req
->command
= (u8
) command
;
264 req
->xfer
[2].tx_buf
= &req
->command
;
265 req
->xfer
[2].len
= 1;
266 spi_message_add_tail(&req
->xfer
[2], &req
->msg
);
268 req
->xfer
[3].rx_buf
= &req
->sample
;
269 req
->xfer
[3].len
= 2;
270 spi_message_add_tail(&req
->xfer
[3], &req
->msg
);
272 /* REVISIT: take a few more samples, and compare ... */
274 /* converter in low power mode & enable PENIRQ */
275 req
->ref_off
= PWRDOWN
;
276 req
->xfer
[4].tx_buf
= &req
->ref_off
;
277 req
->xfer
[4].len
= 1;
278 spi_message_add_tail(&req
->xfer
[4], &req
->msg
);
280 req
->xfer
[5].rx_buf
= &req
->scratch
;
281 req
->xfer
[5].len
= 2;
282 CS_CHANGE(req
->xfer
[5]);
283 spi_message_add_tail(&req
->xfer
[5], &req
->msg
);
285 ts
->irq_disabled
= 1;
286 disable_irq(spi
->irq
);
287 status
= spi_sync(spi
, &req
->msg
);
288 ts
->irq_disabled
= 0;
289 enable_irq(spi
->irq
);
292 /* on-wire is a must-ignore bit, a BE12 value, then padding */
293 status
= be16_to_cpu(req
->sample
);
294 status
= status
>> 3;
302 static int ads7845_read12_ser(struct device
*dev
, unsigned command
)
304 struct spi_device
*spi
= to_spi_device(dev
);
305 struct ads7846
*ts
= dev_get_drvdata(dev
);
306 struct ads7845_ser_req
*req
= kzalloc(sizeof *req
, GFP_KERNEL
);
312 spi_message_init(&req
->msg
);
314 req
->command
[0] = (u8
) command
;
315 req
->xfer
[0].tx_buf
= req
->command
;
316 req
->xfer
[0].rx_buf
= req
->sample
;
317 req
->xfer
[0].len
= 3;
318 spi_message_add_tail(&req
->xfer
[0], &req
->msg
);
320 ts
->irq_disabled
= 1;
321 disable_irq(spi
->irq
);
322 status
= spi_sync(spi
, &req
->msg
);
323 ts
->irq_disabled
= 0;
324 enable_irq(spi
->irq
);
327 /* BE12 value, then padding */
328 status
= be16_to_cpu(*((u16
*)&req
->sample
[1]));
329 status
= status
>> 3;
337 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
339 #define SHOW(name, var, adjust) static ssize_t \
340 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
342 struct ads7846 *ts = dev_get_drvdata(dev); \
343 ssize_t v = ads7846_read12_ser(dev, \
344 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
347 return sprintf(buf, "%u\n", adjust(ts, v)); \
349 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
352 /* Sysfs conventions report temperatures in millidegrees Celsius.
353 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
354 * accuracy scheme without calibration data. For now we won't try either;
355 * userspace sees raw sensor values, and must scale/calibrate appropriately.
357 static inline unsigned null_adjust(struct ads7846
*ts
, ssize_t v
)
362 SHOW(temp0
, temp0
, null_adjust
) /* temp1_input */
363 SHOW(temp1
, temp1
, null_adjust
) /* temp2_input */
366 /* sysfs conventions report voltages in millivolts. We can convert voltages
367 * if we know vREF. userspace may need to scale vAUX to match the board's
368 * external resistors; we assume that vBATT only uses the internal ones.
370 static inline unsigned vaux_adjust(struct ads7846
*ts
, ssize_t v
)
374 /* external resistors may scale vAUX into 0..vREF */
375 retval
*= ts
->vref_mv
;
376 retval
= retval
>> 12;
380 static inline unsigned vbatt_adjust(struct ads7846
*ts
, ssize_t v
)
382 unsigned retval
= vaux_adjust(ts
, v
);
384 /* ads7846 has a resistor ladder to scale this signal down */
385 if (ts
->model
== 7846)
390 SHOW(in0_input
, vaux
, vaux_adjust
)
391 SHOW(in1_input
, vbatt
, vbatt_adjust
)
394 static struct attribute
*ads7846_attributes
[] = {
395 &dev_attr_temp0
.attr
,
396 &dev_attr_temp1
.attr
,
397 &dev_attr_in0_input
.attr
,
398 &dev_attr_in1_input
.attr
,
402 static struct attribute_group ads7846_attr_group
= {
403 .attrs
= ads7846_attributes
,
406 static struct attribute
*ads7843_attributes
[] = {
407 &dev_attr_in0_input
.attr
,
408 &dev_attr_in1_input
.attr
,
412 static struct attribute_group ads7843_attr_group
= {
413 .attrs
= ads7843_attributes
,
416 static struct attribute
*ads7845_attributes
[] = {
417 &dev_attr_in0_input
.attr
,
421 static struct attribute_group ads7845_attr_group
= {
422 .attrs
= ads7845_attributes
,
425 static int ads784x_hwmon_register(struct spi_device
*spi
, struct ads7846
*ts
)
427 struct device
*hwmon
;
430 /* hwmon sensors need a reference voltage */
434 dev_dbg(&spi
->dev
, "assuming 2.5V internal vREF\n");
442 "external vREF for ADS%d not specified\n",
449 /* different chips have different sensor groups */
452 ts
->attr_group
= &ads7846_attr_group
;
455 ts
->attr_group
= &ads7845_attr_group
;
458 ts
->attr_group
= &ads7843_attr_group
;
461 dev_dbg(&spi
->dev
, "ADS%d not recognized\n", ts
->model
);
465 err
= sysfs_create_group(&spi
->dev
.kobj
, ts
->attr_group
);
469 hwmon
= hwmon_device_register(&spi
->dev
);
471 sysfs_remove_group(&spi
->dev
.kobj
, ts
->attr_group
);
472 return PTR_ERR(hwmon
);
479 static void ads784x_hwmon_unregister(struct spi_device
*spi
,
483 sysfs_remove_group(&spi
->dev
.kobj
, ts
->attr_group
);
484 hwmon_device_unregister(ts
->hwmon
);
489 static inline int ads784x_hwmon_register(struct spi_device
*spi
,
495 static inline void ads784x_hwmon_unregister(struct spi_device
*spi
,
501 static int is_pen_down(struct device
*dev
)
503 struct ads7846
*ts
= dev_get_drvdata(dev
);
508 static ssize_t
ads7846_pen_down_show(struct device
*dev
,
509 struct device_attribute
*attr
, char *buf
)
511 return sprintf(buf
, "%u\n", is_pen_down(dev
));
514 static DEVICE_ATTR(pen_down
, S_IRUGO
, ads7846_pen_down_show
, NULL
);
516 static ssize_t
ads7846_disable_show(struct device
*dev
,
517 struct device_attribute
*attr
, char *buf
)
519 struct ads7846
*ts
= dev_get_drvdata(dev
);
521 return sprintf(buf
, "%u\n", ts
->disabled
);
524 static ssize_t
ads7846_disable_store(struct device
*dev
,
525 struct device_attribute
*attr
,
526 const char *buf
, size_t count
)
528 struct ads7846
*ts
= dev_get_drvdata(dev
);
531 if (strict_strtoul(buf
, 10, &i
))
534 spin_lock_irq(&ts
->lock
);
541 spin_unlock_irq(&ts
->lock
);
546 static DEVICE_ATTR(disable
, 0664, ads7846_disable_show
, ads7846_disable_store
);
548 static struct attribute
*ads784x_attributes
[] = {
549 &dev_attr_pen_down
.attr
,
550 &dev_attr_disable
.attr
,
554 static struct attribute_group ads784x_attr_group
= {
555 .attrs
= ads784x_attributes
,
558 /*--------------------------------------------------------------------------*/
560 static int get_pendown_state(struct ads7846
*ts
)
562 if (ts
->get_pendown_state
)
563 return ts
->get_pendown_state();
565 return !gpio_get_value(ts
->gpio_pendown
);
568 static void null_wait_for_sync(void)
573 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
574 * to retrieve touchscreen status.
576 * The SPI transfer completion callback does the real work. It reports
577 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
580 static void ads7846_rx(void *ads
)
582 struct ads7846
*ts
= ads
;
583 struct ads7846_packet
*packet
= ts
->packet
;
587 /* ads7846_rx_val() did in-place conversion (including byteswap) from
588 * on-the-wire format as part of debouncing to get stable readings.
590 if (ts
->model
== 7845) {
591 x
= *(u16
*)packet
->tc
.x_buf
;
592 y
= *(u16
*)packet
->tc
.y_buf
;
602 /* range filtering */
606 if (ts
->model
== 7843) {
607 Rt
= ts
->pressure_max
/ 2;
608 } else if (ts
->model
== 7845) {
609 if (get_pendown_state(ts
))
610 Rt
= ts
->pressure_max
/ 2;
613 dev_vdbg(&ts
->spi
->dev
, "x/y: %d/%d, PD %d\n", x
, y
, Rt
);
614 } else if (likely(x
&& z1
)) {
615 /* compute touch pressure resistance using equation #2 */
619 Rt
*= ts
->x_plate_ohms
;
621 Rt
= (Rt
+ 2047) >> 12;
626 /* Sample found inconsistent by debouncing or pressure is beyond
627 * the maximum. Don't report it to user space, repeat at least
628 * once more the measurement
630 if (packet
->tc
.ignore
|| Rt
> ts
->pressure_max
) {
631 dev_vdbg(&ts
->spi
->dev
, "ignored %d pressure %d\n",
632 packet
->tc
.ignore
, Rt
);
633 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_PERIOD
),
638 /* Maybe check the pendown state before reporting. This discards
639 * false readings when the pen is lifted.
641 if (ts
->penirq_recheck_delay_usecs
) {
642 udelay(ts
->penirq_recheck_delay_usecs
);
643 if (!get_pendown_state(ts
))
647 /* NOTE: We can't rely on the pressure to determine the pen down
648 * state, even this controller has a pressure sensor. The pressure
649 * value can fluctuate for quite a while after lifting the pen and
650 * in some cases may not even settle at the expected value.
652 * The only safe way to check for the pen up condition is in the
653 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
656 struct input_dev
*input
= ts
->input
;
659 input_report_key(input
, BTN_TOUCH
, 1);
661 dev_vdbg(&ts
->spi
->dev
, "DOWN\n");
667 input_report_abs(input
, ABS_X
, x
);
668 input_report_abs(input
, ABS_Y
, y
);
669 input_report_abs(input
, ABS_PRESSURE
, ts
->pressure_max
- Rt
);
672 dev_vdbg(&ts
->spi
->dev
, "%4d/%4d/%4d\n", x
, y
, Rt
);
675 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_PERIOD
),
679 static int ads7846_debounce(void *ads
, int data_idx
, int *val
)
681 struct ads7846
*ts
= ads
;
683 if (!ts
->read_cnt
|| (abs(ts
->last_read
- *val
) > ts
->debounce_tol
)) {
684 /* Start over collecting consistent readings. */
686 /* Repeat it, if this was the first read or the read
687 * wasn't consistent enough. */
688 if (ts
->read_cnt
< ts
->debounce_max
) {
689 ts
->last_read
= *val
;
691 return ADS7846_FILTER_REPEAT
;
693 /* Maximum number of debouncing reached and still
694 * not enough number of consistent readings. Abort
695 * the whole sample, repeat it in the next sampling
699 return ADS7846_FILTER_IGNORE
;
702 if (++ts
->read_rep
> ts
->debounce_rep
) {
703 /* Got a good reading for this coordinate,
704 * go for the next one. */
707 return ADS7846_FILTER_OK
;
709 /* Read more values that are consistent. */
711 return ADS7846_FILTER_REPEAT
;
716 static int ads7846_no_filter(void *ads
, int data_idx
, int *val
)
718 return ADS7846_FILTER_OK
;
721 static void ads7846_rx_val(void *ads
)
723 struct ads7846
*ts
= ads
;
724 struct ads7846_packet
*packet
= ts
->packet
;
725 struct spi_message
*m
;
726 struct spi_transfer
*t
;
731 m
= &ts
->msg
[ts
->msg_idx
];
732 t
= list_entry(m
->transfers
.prev
, struct spi_transfer
, transfer_list
);
734 if (ts
->model
== 7845) {
735 val
= be16_to_cpup((__be16
*)&(((char*)t
->rx_buf
)[1])) >> 3;
737 /* adjust: on-wire is a must-ignore bit, a BE12 value, then
738 * padding; built from two 8 bit values written msb-first.
740 val
= be16_to_cpup((__be16
*)t
->rx_buf
) >> 3;
743 action
= ts
->filter(ts
->filter_data
, ts
->msg_idx
, &val
);
745 case ADS7846_FILTER_REPEAT
:
747 case ADS7846_FILTER_IGNORE
:
748 packet
->tc
.ignore
= 1;
749 /* Last message will contain ads7846_rx() as the
750 * completion function.
754 case ADS7846_FILTER_OK
:
755 *(u16
*)t
->rx_buf
= val
;
756 packet
->tc
.ignore
= 0;
757 m
= &ts
->msg
[++ts
->msg_idx
];
763 status
= spi_async(ts
->spi
, m
);
765 dev_err(&ts
->spi
->dev
, "spi_async --> %d\n",
769 static enum hrtimer_restart
ads7846_timer(struct hrtimer
*handle
)
771 struct ads7846
*ts
= container_of(handle
, struct ads7846
, timer
);
774 spin_lock(&ts
->lock
);
776 if (unlikely(!get_pendown_state(ts
) ||
777 device_suspended(&ts
->spi
->dev
))) {
779 struct input_dev
*input
= ts
->input
;
781 input_report_key(input
, BTN_TOUCH
, 0);
782 input_report_abs(input
, ABS_PRESSURE
, 0);
786 dev_vdbg(&ts
->spi
->dev
, "UP\n");
789 /* measurement cycle ended */
790 if (!device_suspended(&ts
->spi
->dev
)) {
791 ts
->irq_disabled
= 0;
792 enable_irq(ts
->spi
->irq
);
796 /* pen is still down, continue with the measurement */
799 status
= spi_async(ts
->spi
, &ts
->msg
[0]);
801 dev_err(&ts
->spi
->dev
, "spi_async --> %d\n", status
);
804 spin_unlock(&ts
->lock
);
805 return HRTIMER_NORESTART
;
808 static irqreturn_t
ads7846_irq(int irq
, void *handle
)
810 struct ads7846
*ts
= handle
;
813 spin_lock_irqsave(&ts
->lock
, flags
);
814 if (likely(get_pendown_state(ts
))) {
815 if (!ts
->irq_disabled
) {
816 /* The ARM do_simple_IRQ() dispatcher doesn't act
817 * like the other dispatchers: it will report IRQs
818 * even after they've been disabled. We work around
819 * that here. (The "generic irq" framework may help...)
821 ts
->irq_disabled
= 1;
822 disable_irq_nosync(ts
->spi
->irq
);
824 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_DELAY
),
828 spin_unlock_irqrestore(&ts
->lock
, flags
);
833 /*--------------------------------------------------------------------------*/
835 /* Must be called with ts->lock held */
836 static void ads7846_disable(struct ads7846
*ts
)
843 /* are we waiting for IRQ, or polling? */
845 ts
->irq_disabled
= 1;
846 disable_irq(ts
->spi
->irq
);
848 /* the timer will run at least once more, and
849 * leave everything in a clean state, IRQ disabled
851 while (ts
->pending
) {
852 spin_unlock_irq(&ts
->lock
);
854 spin_lock_irq(&ts
->lock
);
858 regulator_disable(ts
->reg
);
860 /* we know the chip's in lowpower mode since we always
861 * leave it that way after every request
865 /* Must be called with ts->lock held */
866 static void ads7846_enable(struct ads7846
*ts
)
871 regulator_enable(ts
->reg
);
874 ts
->irq_disabled
= 0;
875 enable_irq(ts
->spi
->irq
);
878 static int ads7846_suspend(struct spi_device
*spi
, pm_message_t message
)
880 struct ads7846
*ts
= dev_get_drvdata(&spi
->dev
);
882 spin_lock_irq(&ts
->lock
);
884 ts
->is_suspended
= 1;
887 spin_unlock_irq(&ts
->lock
);
889 if (device_may_wakeup(&ts
->spi
->dev
))
890 enable_irq_wake(ts
->spi
->irq
);
896 static int ads7846_resume(struct spi_device
*spi
)
898 struct ads7846
*ts
= dev_get_drvdata(&spi
->dev
);
900 if (device_may_wakeup(&ts
->spi
->dev
))
901 disable_irq_wake(ts
->spi
->irq
);
903 spin_lock_irq(&ts
->lock
);
905 ts
->is_suspended
= 0;
908 spin_unlock_irq(&ts
->lock
);
913 static int __devinit
setup_pendown(struct spi_device
*spi
, struct ads7846
*ts
)
915 struct ads7846_platform_data
*pdata
= spi
->dev
.platform_data
;
918 /* REVISIT when the irq can be triggered active-low, or if for some
919 * reason the touchscreen isn't hooked up, we don't need to access
922 if (!pdata
->get_pendown_state
&& !gpio_is_valid(pdata
->gpio_pendown
)) {
923 dev_err(&spi
->dev
, "no get_pendown_state nor gpio_pendown?\n");
927 if (pdata
->get_pendown_state
) {
928 ts
->get_pendown_state
= pdata
->get_pendown_state
;
932 err
= gpio_request(pdata
->gpio_pendown
, "ads7846_pendown");
934 dev_err(&spi
->dev
, "failed to request pendown GPIO%d\n",
935 pdata
->gpio_pendown
);
939 ts
->gpio_pendown
= pdata
->gpio_pendown
;
943 static int __devinit
ads7846_probe(struct spi_device
*spi
)
946 struct ads7846_packet
*packet
;
947 struct input_dev
*input_dev
;
948 const struct ads7846_platform_data
*pdata
= spi
->dev
.platform_data
;
949 struct spi_message
*m
;
950 struct spi_transfer
*x
;
951 unsigned long irq_flags
;
956 dev_dbg(&spi
->dev
, "no IRQ?\n");
961 dev_dbg(&spi
->dev
, "no platform data?\n");
965 /* don't exceed max specified sample rate */
966 if (spi
->max_speed_hz
> (125000 * SAMPLE_BITS
)) {
967 dev_dbg(&spi
->dev
, "f(sample) %d KHz?\n",
968 (spi
->max_speed_hz
/SAMPLE_BITS
)/1000);
972 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
973 * that even if the hardware can do that, the SPI controller driver
974 * may not. So we stick to very-portable 8 bit words, both RX and TX.
976 spi
->bits_per_word
= 8;
977 spi
->mode
= SPI_MODE_0
;
978 err
= spi_setup(spi
);
982 ts
= kzalloc(sizeof(struct ads7846
), GFP_KERNEL
);
983 packet
= kzalloc(sizeof(struct ads7846_packet
), GFP_KERNEL
);
984 input_dev
= input_allocate_device();
985 if (!ts
|| !packet
|| !input_dev
) {
990 dev_set_drvdata(&spi
->dev
, ts
);
994 ts
->input
= input_dev
;
995 ts
->vref_mv
= pdata
->vref_mv
;
996 ts
->swap_xy
= pdata
->swap_xy
;
998 hrtimer_init(&ts
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
999 ts
->timer
.function
= ads7846_timer
;
1001 spin_lock_init(&ts
->lock
);
1003 ts
->model
= pdata
->model
? : 7846;
1004 ts
->vref_delay_usecs
= pdata
->vref_delay_usecs
? : 100;
1005 ts
->x_plate_ohms
= pdata
->x_plate_ohms
? : 400;
1006 ts
->pressure_max
= pdata
->pressure_max
? : ~0;
1008 if (pdata
->filter
!= NULL
) {
1009 if (pdata
->filter_init
!= NULL
) {
1010 err
= pdata
->filter_init(pdata
, &ts
->filter_data
);
1014 ts
->filter
= pdata
->filter
;
1015 ts
->filter_cleanup
= pdata
->filter_cleanup
;
1016 } else if (pdata
->debounce_max
) {
1017 ts
->debounce_max
= pdata
->debounce_max
;
1018 if (ts
->debounce_max
< 2)
1019 ts
->debounce_max
= 2;
1020 ts
->debounce_tol
= pdata
->debounce_tol
;
1021 ts
->debounce_rep
= pdata
->debounce_rep
;
1022 ts
->filter
= ads7846_debounce
;
1023 ts
->filter_data
= ts
;
1025 ts
->filter
= ads7846_no_filter
;
1027 err
= setup_pendown(spi
, ts
);
1029 goto err_cleanup_filter
;
1031 if (pdata
->penirq_recheck_delay_usecs
)
1032 ts
->penirq_recheck_delay_usecs
=
1033 pdata
->penirq_recheck_delay_usecs
;
1035 ts
->wait_for_sync
= pdata
->wait_for_sync
? : null_wait_for_sync
;
1037 snprintf(ts
->phys
, sizeof(ts
->phys
), "%s/input0", dev_name(&spi
->dev
));
1038 snprintf(ts
->name
, sizeof(ts
->name
), "ADS%d Touchscreen", ts
->model
);
1040 input_dev
->name
= ts
->name
;
1041 input_dev
->phys
= ts
->phys
;
1042 input_dev
->dev
.parent
= &spi
->dev
;
1044 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
1045 input_dev
->keybit
[BIT_WORD(BTN_TOUCH
)] = BIT_MASK(BTN_TOUCH
);
1046 input_set_abs_params(input_dev
, ABS_X
,
1048 pdata
->x_max
? : MAX_12BIT
,
1050 input_set_abs_params(input_dev
, ABS_Y
,
1052 pdata
->y_max
? : MAX_12BIT
,
1054 input_set_abs_params(input_dev
, ABS_PRESSURE
,
1055 pdata
->pressure_min
, pdata
->pressure_max
, 0, 0);
1057 vref
= pdata
->keep_vref_on
;
1059 if (ts
->model
== 7873) {
1060 /* The AD7873 is almost identical to the ADS7846
1061 * keep VREF off during differential/ratiometric
1068 /* set up the transfers to read touchscreen state; this assumes we
1069 * use formula #2 for pressure, not #3.
1074 spi_message_init(m
);
1076 if (ts
->model
== 7845) {
1077 packet
->read_y_cmd
[0] = READ_Y(vref
);
1078 packet
->read_y_cmd
[1] = 0;
1079 packet
->read_y_cmd
[2] = 0;
1080 x
->tx_buf
= &packet
->read_y_cmd
[0];
1081 x
->rx_buf
= &packet
->tc
.y_buf
[0];
1083 spi_message_add_tail(x
, m
);
1085 /* y- still on; turn on only y+ (and ADC) */
1086 packet
->read_y
= READ_Y(vref
);
1087 x
->tx_buf
= &packet
->read_y
;
1089 spi_message_add_tail(x
, m
);
1092 x
->rx_buf
= &packet
->tc
.y
;
1094 spi_message_add_tail(x
, m
);
1097 /* the first sample after switching drivers can be low quality;
1098 * optionally discard it, using a second one after the signals
1099 * have had enough time to stabilize.
1101 if (pdata
->settle_delay_usecs
) {
1102 x
->delay_usecs
= pdata
->settle_delay_usecs
;
1105 x
->tx_buf
= &packet
->read_y
;
1107 spi_message_add_tail(x
, m
);
1110 x
->rx_buf
= &packet
->tc
.y
;
1112 spi_message_add_tail(x
, m
);
1115 m
->complete
= ads7846_rx_val
;
1119 spi_message_init(m
);
1121 if (ts
->model
== 7845) {
1123 packet
->read_x_cmd
[0] = READ_X(vref
);
1124 packet
->read_x_cmd
[1] = 0;
1125 packet
->read_x_cmd
[2] = 0;
1126 x
->tx_buf
= &packet
->read_x_cmd
[0];
1127 x
->rx_buf
= &packet
->tc
.x_buf
[0];
1129 spi_message_add_tail(x
, m
);
1131 /* turn y- off, x+ on, then leave in lowpower */
1133 packet
->read_x
= READ_X(vref
);
1134 x
->tx_buf
= &packet
->read_x
;
1136 spi_message_add_tail(x
, m
);
1139 x
->rx_buf
= &packet
->tc
.x
;
1141 spi_message_add_tail(x
, m
);
1144 /* ... maybe discard first sample ... */
1145 if (pdata
->settle_delay_usecs
) {
1146 x
->delay_usecs
= pdata
->settle_delay_usecs
;
1149 x
->tx_buf
= &packet
->read_x
;
1151 spi_message_add_tail(x
, m
);
1154 x
->rx_buf
= &packet
->tc
.x
;
1156 spi_message_add_tail(x
, m
);
1159 m
->complete
= ads7846_rx_val
;
1162 /* turn y+ off, x- on; we'll use formula #2 */
1163 if (ts
->model
== 7846) {
1165 spi_message_init(m
);
1168 packet
->read_z1
= READ_Z1(vref
);
1169 x
->tx_buf
= &packet
->read_z1
;
1171 spi_message_add_tail(x
, m
);
1174 x
->rx_buf
= &packet
->tc
.z1
;
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
;
1183 x
->tx_buf
= &packet
->read_z1
;
1185 spi_message_add_tail(x
, m
);
1188 x
->rx_buf
= &packet
->tc
.z1
;
1190 spi_message_add_tail(x
, m
);
1193 m
->complete
= ads7846_rx_val
;
1197 spi_message_init(m
);
1200 packet
->read_z2
= READ_Z2(vref
);
1201 x
->tx_buf
= &packet
->read_z2
;
1203 spi_message_add_tail(x
, m
);
1206 x
->rx_buf
= &packet
->tc
.z2
;
1208 spi_message_add_tail(x
, m
);
1210 /* ... maybe discard first sample ... */
1211 if (pdata
->settle_delay_usecs
) {
1212 x
->delay_usecs
= pdata
->settle_delay_usecs
;
1215 x
->tx_buf
= &packet
->read_z2
;
1217 spi_message_add_tail(x
, m
);
1220 x
->rx_buf
= &packet
->tc
.z2
;
1222 spi_message_add_tail(x
, m
);
1225 m
->complete
= ads7846_rx_val
;
1231 spi_message_init(m
);
1233 if (ts
->model
== 7845) {
1235 packet
->pwrdown_cmd
[0] = PWRDOWN
;
1236 packet
->pwrdown_cmd
[1] = 0;
1237 packet
->pwrdown_cmd
[2] = 0;
1238 x
->tx_buf
= &packet
->pwrdown_cmd
[0];
1242 packet
->pwrdown
= PWRDOWN
;
1243 x
->tx_buf
= &packet
->pwrdown
;
1245 spi_message_add_tail(x
, m
);
1248 x
->rx_buf
= &packet
->dummy
;
1253 spi_message_add_tail(x
, m
);
1255 m
->complete
= ads7846_rx
;
1260 ts
->reg
= regulator_get(&spi
->dev
, "vcc");
1261 if (IS_ERR(ts
->reg
)) {
1262 err
= PTR_ERR(ts
->reg
);
1263 dev_err(&spi
->dev
, "unable to get regulator: %d\n", err
);
1267 err
= regulator_enable(ts
->reg
);
1269 dev_err(&spi
->dev
, "unable to enable regulator: %d\n", err
);
1270 goto err_put_regulator
;
1273 irq_flags
= pdata
->irq_flags
? : IRQF_TRIGGER_FALLING
;
1275 err
= request_irq(spi
->irq
, ads7846_irq
, irq_flags
,
1276 spi
->dev
.driver
->name
, ts
);
1278 if (err
&& !pdata
->irq_flags
) {
1280 "trying pin change workaround on irq %d\n", spi
->irq
);
1281 err
= request_irq(spi
->irq
, ads7846_irq
,
1282 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
1283 spi
->dev
.driver
->name
, ts
);
1287 dev_dbg(&spi
->dev
, "irq %d busy?\n", spi
->irq
);
1288 goto err_disable_regulator
;
1291 err
= ads784x_hwmon_register(spi
, ts
);
1295 dev_info(&spi
->dev
, "touchscreen, irq %d\n", spi
->irq
);
1297 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1298 * the touchscreen, in case it's not connected.
1300 if (ts
->model
== 7845)
1301 ads7845_read12_ser(&spi
->dev
, PWRDOWN
);
1303 (void) ads7846_read12_ser(&spi
->dev
,
1304 READ_12BIT_SER(vaux
) | ADS_PD10_ALL_ON
);
1306 err
= sysfs_create_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1308 goto err_remove_hwmon
;
1310 err
= input_register_device(input_dev
);
1312 goto err_remove_attr_group
;
1314 device_init_wakeup(&spi
->dev
, pdata
->wakeup
);
1318 err_remove_attr_group
:
1319 sysfs_remove_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1321 ads784x_hwmon_unregister(spi
, ts
);
1323 free_irq(spi
->irq
, ts
);
1324 err_disable_regulator
:
1325 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 input_free_device(input_dev
);
1341 static int __devexit
ads7846_remove(struct spi_device
*spi
)
1343 struct ads7846
*ts
= dev_get_drvdata(&spi
->dev
);
1345 device_init_wakeup(&spi
->dev
, false);
1347 ads784x_hwmon_unregister(spi
, ts
);
1348 input_unregister_device(ts
->input
);
1350 ads7846_suspend(spi
, PMSG_SUSPEND
);
1352 sysfs_remove_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1354 free_irq(ts
->spi
->irq
, ts
);
1355 /* suspend left the IRQ disabled */
1356 enable_irq(ts
->spi
->irq
);
1358 regulator_disable(ts
->reg
);
1359 regulator_put(ts
->reg
);
1361 if (ts
->gpio_pendown
!= -1)
1362 gpio_free(ts
->gpio_pendown
);
1364 if (ts
->filter_cleanup
)
1365 ts
->filter_cleanup(ts
->filter_data
);
1370 dev_dbg(&spi
->dev
, "unregistered touchscreen\n");
1374 static struct spi_driver ads7846_driver
= {
1377 .bus
= &spi_bus_type
,
1378 .owner
= THIS_MODULE
,
1380 .probe
= ads7846_probe
,
1381 .remove
= __devexit_p(ads7846_remove
),
1382 .suspend
= ads7846_suspend
,
1383 .resume
= ads7846_resume
,
1386 static int __init
ads7846_init(void)
1388 return spi_register_driver(&ads7846_driver
);
1390 module_init(ads7846_init
);
1392 static void __exit
ads7846_exit(void)
1394 spi_unregister_driver(&ads7846_driver
);
1396 module_exit(ads7846_exit
);
1398 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1399 MODULE_LICENSE("GPL");
1400 MODULE_ALIAS("spi:ads7846");