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.
74 * We allocate this separately to avoid cache line sharing issues when
75 * driver is used with DMA-based SPI controllers (like atmel_spi) on
76 * systems where main memory is not DMA-coherent (most non-x86 boards).
78 struct ads7846_packet
{
79 u8 read_x
, read_y
, read_z1
, read_z2
, pwrdown
;
80 u16 dummy
; /* for the pwrdown read */
85 struct input_dev
*input
;
89 struct spi_device
*spi
;
90 struct regulator
*reg
;
92 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
93 struct attribute_group
*attr_group
;
105 struct ads7846_packet
*packet
;
107 struct spi_transfer xfer
[18];
108 struct spi_message msg
[5];
109 struct spi_message
*last_msg
;
119 u16 penirq_recheck_delay_usecs
;
122 struct hrtimer timer
;
123 unsigned pendown
:1; /* P: lock */
124 unsigned pending
:1; /* P: lock */
125 // FIXME remove "irq_disabled"
126 unsigned irq_disabled
:1; /* P: lock */
128 unsigned is_suspended
:1;
130 int (*filter
)(void *data
, int data_idx
, int *val
);
132 void (*filter_cleanup
)(void *data
);
133 int (*get_pendown_state
)(void);
136 void (*wait_for_sync
)(void);
139 /* leave chip selected when we're done, for quicker re-select? */
141 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
143 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
146 /*--------------------------------------------------------------------------*/
148 /* The ADS7846 has touchscreen and other sensors.
149 * Earlier ads784x chips are somewhat compatible.
151 #define ADS_START (1 << 7)
152 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
153 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
154 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
155 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
156 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
157 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
158 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
159 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
160 #define ADS_8_BIT (1 << 3)
161 #define ADS_12_BIT (0 << 3)
162 #define ADS_SER (1 << 2) /* non-differential */
163 #define ADS_DFR (0 << 2) /* differential */
164 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
165 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
166 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
167 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
169 #define MAX_12BIT ((1<<12)-1)
171 /* leave ADC powered up (disables penirq) between differential samples */
172 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
173 | ADS_12_BIT | ADS_DFR | \
174 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
176 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
177 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
178 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
180 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
181 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
183 /* single-ended samples need to first power up reference voltage;
184 * we leave both ADC and VREF powered
186 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
187 | ADS_12_BIT | ADS_SER)
189 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
190 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
192 /*--------------------------------------------------------------------------*/
195 * Non-touchscreen sensors only use single-ended conversions.
196 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
197 * ads7846 lets that pin be unconnected, to use internal vREF.
206 struct spi_message msg
;
207 struct spi_transfer xfer
[6];
210 static void ads7846_enable(struct ads7846
*ts
);
211 static void ads7846_disable(struct ads7846
*ts
);
213 static int device_suspended(struct device
*dev
)
215 struct ads7846
*ts
= dev_get_drvdata(dev
);
216 return ts
->is_suspended
|| ts
->disabled
;
219 static int ads7846_read12_ser(struct device
*dev
, unsigned command
)
221 struct spi_device
*spi
= to_spi_device(dev
);
222 struct ads7846
*ts
= dev_get_drvdata(dev
);
223 struct ser_req
*req
= kzalloc(sizeof *req
, GFP_KERNEL
);
230 spi_message_init(&req
->msg
);
232 /* FIXME boards with ads7846 might use external vref instead ... */
233 use_internal
= (ts
->model
== 7846);
235 /* maybe turn on internal vREF, and let it settle */
237 req
->ref_on
= REF_ON
;
238 req
->xfer
[0].tx_buf
= &req
->ref_on
;
239 req
->xfer
[0].len
= 1;
240 spi_message_add_tail(&req
->xfer
[0], &req
->msg
);
242 req
->xfer
[1].rx_buf
= &req
->scratch
;
243 req
->xfer
[1].len
= 2;
245 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
246 req
->xfer
[1].delay_usecs
= ts
->vref_delay_usecs
;
247 spi_message_add_tail(&req
->xfer
[1], &req
->msg
);
251 req
->command
= (u8
) command
;
252 req
->xfer
[2].tx_buf
= &req
->command
;
253 req
->xfer
[2].len
= 1;
254 spi_message_add_tail(&req
->xfer
[2], &req
->msg
);
256 req
->xfer
[3].rx_buf
= &req
->sample
;
257 req
->xfer
[3].len
= 2;
258 spi_message_add_tail(&req
->xfer
[3], &req
->msg
);
260 /* REVISIT: take a few more samples, and compare ... */
262 /* converter in low power mode & enable PENIRQ */
263 req
->ref_off
= PWRDOWN
;
264 req
->xfer
[4].tx_buf
= &req
->ref_off
;
265 req
->xfer
[4].len
= 1;
266 spi_message_add_tail(&req
->xfer
[4], &req
->msg
);
268 req
->xfer
[5].rx_buf
= &req
->scratch
;
269 req
->xfer
[5].len
= 2;
270 CS_CHANGE(req
->xfer
[5]);
271 spi_message_add_tail(&req
->xfer
[5], &req
->msg
);
273 ts
->irq_disabled
= 1;
274 disable_irq(spi
->irq
);
275 status
= spi_sync(spi
, &req
->msg
);
276 ts
->irq_disabled
= 0;
277 enable_irq(spi
->irq
);
280 /* on-wire is a must-ignore bit, a BE12 value, then padding */
281 status
= be16_to_cpu(req
->sample
);
282 status
= status
>> 3;
290 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
292 #define SHOW(name, var, adjust) static ssize_t \
293 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
295 struct ads7846 *ts = dev_get_drvdata(dev); \
296 ssize_t v = ads7846_read12_ser(dev, \
297 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
300 return sprintf(buf, "%u\n", adjust(ts, v)); \
302 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
305 /* Sysfs conventions report temperatures in millidegrees Celsius.
306 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
307 * accuracy scheme without calibration data. For now we won't try either;
308 * userspace sees raw sensor values, and must scale/calibrate appropriately.
310 static inline unsigned null_adjust(struct ads7846
*ts
, ssize_t v
)
315 SHOW(temp0
, temp0
, null_adjust
) /* temp1_input */
316 SHOW(temp1
, temp1
, null_adjust
) /* temp2_input */
319 /* sysfs conventions report voltages in millivolts. We can convert voltages
320 * if we know vREF. userspace may need to scale vAUX to match the board's
321 * external resistors; we assume that vBATT only uses the internal ones.
323 static inline unsigned vaux_adjust(struct ads7846
*ts
, ssize_t v
)
327 /* external resistors may scale vAUX into 0..vREF */
328 retval
*= ts
->vref_mv
;
329 retval
= retval
>> 12;
333 static inline unsigned vbatt_adjust(struct ads7846
*ts
, ssize_t v
)
335 unsigned retval
= vaux_adjust(ts
, v
);
337 /* ads7846 has a resistor ladder to scale this signal down */
338 if (ts
->model
== 7846)
343 SHOW(in0_input
, vaux
, vaux_adjust
)
344 SHOW(in1_input
, vbatt
, vbatt_adjust
)
347 static struct attribute
*ads7846_attributes
[] = {
348 &dev_attr_temp0
.attr
,
349 &dev_attr_temp1
.attr
,
350 &dev_attr_in0_input
.attr
,
351 &dev_attr_in1_input
.attr
,
355 static struct attribute_group ads7846_attr_group
= {
356 .attrs
= ads7846_attributes
,
359 static struct attribute
*ads7843_attributes
[] = {
360 &dev_attr_in0_input
.attr
,
361 &dev_attr_in1_input
.attr
,
365 static struct attribute_group ads7843_attr_group
= {
366 .attrs
= ads7843_attributes
,
369 static struct attribute
*ads7845_attributes
[] = {
370 &dev_attr_in0_input
.attr
,
374 static struct attribute_group ads7845_attr_group
= {
375 .attrs
= ads7845_attributes
,
378 static int ads784x_hwmon_register(struct spi_device
*spi
, struct ads7846
*ts
)
380 struct device
*hwmon
;
383 /* hwmon sensors need a reference voltage */
387 dev_dbg(&spi
->dev
, "assuming 2.5V internal vREF\n");
395 "external vREF for ADS%d not specified\n",
402 /* different chips have different sensor groups */
405 ts
->attr_group
= &ads7846_attr_group
;
408 ts
->attr_group
= &ads7845_attr_group
;
411 ts
->attr_group
= &ads7843_attr_group
;
414 dev_dbg(&spi
->dev
, "ADS%d not recognized\n", ts
->model
);
418 err
= sysfs_create_group(&spi
->dev
.kobj
, ts
->attr_group
);
422 hwmon
= hwmon_device_register(&spi
->dev
);
424 sysfs_remove_group(&spi
->dev
.kobj
, ts
->attr_group
);
425 return PTR_ERR(hwmon
);
432 static void ads784x_hwmon_unregister(struct spi_device
*spi
,
436 sysfs_remove_group(&spi
->dev
.kobj
, ts
->attr_group
);
437 hwmon_device_unregister(ts
->hwmon
);
442 static inline int ads784x_hwmon_register(struct spi_device
*spi
,
448 static inline void ads784x_hwmon_unregister(struct spi_device
*spi
,
454 static int is_pen_down(struct device
*dev
)
456 struct ads7846
*ts
= dev_get_drvdata(dev
);
461 static ssize_t
ads7846_pen_down_show(struct device
*dev
,
462 struct device_attribute
*attr
, char *buf
)
464 return sprintf(buf
, "%u\n", is_pen_down(dev
));
467 static DEVICE_ATTR(pen_down
, S_IRUGO
, ads7846_pen_down_show
, NULL
);
469 static ssize_t
ads7846_disable_show(struct device
*dev
,
470 struct device_attribute
*attr
, char *buf
)
472 struct ads7846
*ts
= dev_get_drvdata(dev
);
474 return sprintf(buf
, "%u\n", ts
->disabled
);
477 static ssize_t
ads7846_disable_store(struct device
*dev
,
478 struct device_attribute
*attr
,
479 const char *buf
, size_t count
)
481 struct ads7846
*ts
= dev_get_drvdata(dev
);
484 if (strict_strtoul(buf
, 10, &i
))
487 spin_lock_irq(&ts
->lock
);
494 spin_unlock_irq(&ts
->lock
);
499 static DEVICE_ATTR(disable
, 0664, ads7846_disable_show
, ads7846_disable_store
);
501 static struct attribute
*ads784x_attributes
[] = {
502 &dev_attr_pen_down
.attr
,
503 &dev_attr_disable
.attr
,
507 static struct attribute_group ads784x_attr_group
= {
508 .attrs
= ads784x_attributes
,
511 /*--------------------------------------------------------------------------*/
513 static int get_pendown_state(struct ads7846
*ts
)
515 if (ts
->get_pendown_state
)
516 return ts
->get_pendown_state();
518 return !gpio_get_value(ts
->gpio_pendown
);
521 static void null_wait_for_sync(void)
526 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
527 * to retrieve touchscreen status.
529 * The SPI transfer completion callback does the real work. It reports
530 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
533 static void ads7846_rx(void *ads
)
535 struct ads7846
*ts
= ads
;
536 struct ads7846_packet
*packet
= ts
->packet
;
540 /* ads7846_rx_val() did in-place conversion (including byteswap) from
541 * on-the-wire format as part of debouncing to get stable readings.
548 /* range filtering */
552 if (ts
->model
== 7843) {
553 Rt
= ts
->pressure_max
/ 2;
554 } else if (likely(x
&& z1
)) {
555 /* compute touch pressure resistance using equation #2 */
559 Rt
*= ts
->x_plate_ohms
;
561 Rt
= (Rt
+ 2047) >> 12;
566 /* Sample found inconsistent by debouncing or pressure is beyond
567 * the maximum. Don't report it to user space, repeat at least
568 * once more the measurement
570 if (packet
->tc
.ignore
|| Rt
> ts
->pressure_max
) {
571 dev_vdbg(&ts
->spi
->dev
, "ignored %d pressure %d\n",
572 packet
->tc
.ignore
, Rt
);
573 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_PERIOD
),
578 /* Maybe check the pendown state before reporting. This discards
579 * false readings when the pen is lifted.
581 if (ts
->penirq_recheck_delay_usecs
) {
582 udelay(ts
->penirq_recheck_delay_usecs
);
583 if (!get_pendown_state(ts
))
587 /* NOTE: We can't rely on the pressure to determine the pen down
588 * state, even this controller has a pressure sensor. The pressure
589 * value can fluctuate for quite a while after lifting the pen and
590 * in some cases may not even settle at the expected value.
592 * The only safe way to check for the pen up condition is in the
593 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
596 struct input_dev
*input
= ts
->input
;
599 input_report_key(input
, BTN_TOUCH
, 1);
601 dev_vdbg(&ts
->spi
->dev
, "DOWN\n");
607 input_report_abs(input
, ABS_X
, x
);
608 input_report_abs(input
, ABS_Y
, y
);
609 input_report_abs(input
, ABS_PRESSURE
, ts
->pressure_max
- Rt
);
612 dev_vdbg(&ts
->spi
->dev
, "%4d/%4d/%4d\n", x
, y
, Rt
);
615 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_PERIOD
),
619 static int ads7846_debounce(void *ads
, int data_idx
, int *val
)
621 struct ads7846
*ts
= ads
;
623 if (!ts
->read_cnt
|| (abs(ts
->last_read
- *val
) > ts
->debounce_tol
)) {
624 /* Start over collecting consistent readings. */
626 /* Repeat it, if this was the first read or the read
627 * wasn't consistent enough. */
628 if (ts
->read_cnt
< ts
->debounce_max
) {
629 ts
->last_read
= *val
;
631 return ADS7846_FILTER_REPEAT
;
633 /* Maximum number of debouncing reached and still
634 * not enough number of consistent readings. Abort
635 * the whole sample, repeat it in the next sampling
639 return ADS7846_FILTER_IGNORE
;
642 if (++ts
->read_rep
> ts
->debounce_rep
) {
643 /* Got a good reading for this coordinate,
644 * go for the next one. */
647 return ADS7846_FILTER_OK
;
649 /* Read more values that are consistent. */
651 return ADS7846_FILTER_REPEAT
;
656 static int ads7846_no_filter(void *ads
, int data_idx
, int *val
)
658 return ADS7846_FILTER_OK
;
661 static void ads7846_rx_val(void *ads
)
663 struct ads7846
*ts
= ads
;
664 struct ads7846_packet
*packet
= ts
->packet
;
665 struct spi_message
*m
;
666 struct spi_transfer
*t
;
671 m
= &ts
->msg
[ts
->msg_idx
];
672 t
= list_entry(m
->transfers
.prev
, struct spi_transfer
, transfer_list
);
674 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
675 * built from two 8 bit values written msb-first.
677 val
= be16_to_cpup((__be16
*)t
->rx_buf
) >> 3;
679 action
= ts
->filter(ts
->filter_data
, ts
->msg_idx
, &val
);
681 case ADS7846_FILTER_REPEAT
:
683 case ADS7846_FILTER_IGNORE
:
684 packet
->tc
.ignore
= 1;
685 /* Last message will contain ads7846_rx() as the
686 * completion function.
690 case ADS7846_FILTER_OK
:
691 *(u16
*)t
->rx_buf
= val
;
692 packet
->tc
.ignore
= 0;
693 m
= &ts
->msg
[++ts
->msg_idx
];
699 status
= spi_async(ts
->spi
, m
);
701 dev_err(&ts
->spi
->dev
, "spi_async --> %d\n",
705 static enum hrtimer_restart
ads7846_timer(struct hrtimer
*handle
)
707 struct ads7846
*ts
= container_of(handle
, struct ads7846
, timer
);
710 spin_lock(&ts
->lock
);
712 if (unlikely(!get_pendown_state(ts
) ||
713 device_suspended(&ts
->spi
->dev
))) {
715 struct input_dev
*input
= ts
->input
;
717 input_report_key(input
, BTN_TOUCH
, 0);
718 input_report_abs(input
, ABS_PRESSURE
, 0);
722 dev_vdbg(&ts
->spi
->dev
, "UP\n");
725 /* measurement cycle ended */
726 if (!device_suspended(&ts
->spi
->dev
)) {
727 ts
->irq_disabled
= 0;
728 enable_irq(ts
->spi
->irq
);
732 /* pen is still down, continue with the measurement */
735 status
= spi_async(ts
->spi
, &ts
->msg
[0]);
737 dev_err(&ts
->spi
->dev
, "spi_async --> %d\n", status
);
740 spin_unlock(&ts
->lock
);
741 return HRTIMER_NORESTART
;
744 static irqreturn_t
ads7846_irq(int irq
, void *handle
)
746 struct ads7846
*ts
= handle
;
749 spin_lock_irqsave(&ts
->lock
, flags
);
750 if (likely(get_pendown_state(ts
))) {
751 if (!ts
->irq_disabled
) {
752 /* The ARM do_simple_IRQ() dispatcher doesn't act
753 * like the other dispatchers: it will report IRQs
754 * even after they've been disabled. We work around
755 * that here. (The "generic irq" framework may help...)
757 ts
->irq_disabled
= 1;
758 disable_irq_nosync(ts
->spi
->irq
);
760 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_DELAY
),
764 spin_unlock_irqrestore(&ts
->lock
, flags
);
769 /*--------------------------------------------------------------------------*/
771 /* Must be called with ts->lock held */
772 static void ads7846_disable(struct ads7846
*ts
)
779 /* are we waiting for IRQ, or polling? */
781 ts
->irq_disabled
= 1;
782 disable_irq(ts
->spi
->irq
);
784 /* the timer will run at least once more, and
785 * leave everything in a clean state, IRQ disabled
787 while (ts
->pending
) {
788 spin_unlock_irq(&ts
->lock
);
790 spin_lock_irq(&ts
->lock
);
794 regulator_disable(ts
->reg
);
796 /* we know the chip's in lowpower mode since we always
797 * leave it that way after every request
801 /* Must be called with ts->lock held */
802 static void ads7846_enable(struct ads7846
*ts
)
807 regulator_enable(ts
->reg
);
810 ts
->irq_disabled
= 0;
811 enable_irq(ts
->spi
->irq
);
814 static int ads7846_suspend(struct spi_device
*spi
, pm_message_t message
)
816 struct ads7846
*ts
= dev_get_drvdata(&spi
->dev
);
818 spin_lock_irq(&ts
->lock
);
820 ts
->is_suspended
= 1;
823 spin_unlock_irq(&ts
->lock
);
825 if (device_may_wakeup(&ts
->spi
->dev
))
826 enable_irq_wake(ts
->spi
->irq
);
832 static int ads7846_resume(struct spi_device
*spi
)
834 struct ads7846
*ts
= dev_get_drvdata(&spi
->dev
);
836 if (device_may_wakeup(&ts
->spi
->dev
))
837 disable_irq_wake(ts
->spi
->irq
);
839 spin_lock_irq(&ts
->lock
);
841 ts
->is_suspended
= 0;
844 spin_unlock_irq(&ts
->lock
);
849 static int __devinit
setup_pendown(struct spi_device
*spi
, struct ads7846
*ts
)
851 struct ads7846_platform_data
*pdata
= spi
->dev
.platform_data
;
854 /* REVISIT when the irq can be triggered active-low, or if for some
855 * reason the touchscreen isn't hooked up, we don't need to access
858 if (!pdata
->get_pendown_state
&& !gpio_is_valid(pdata
->gpio_pendown
)) {
859 dev_err(&spi
->dev
, "no get_pendown_state nor gpio_pendown?\n");
863 if (pdata
->get_pendown_state
) {
864 ts
->get_pendown_state
= pdata
->get_pendown_state
;
868 err
= gpio_request(pdata
->gpio_pendown
, "ads7846_pendown");
870 dev_err(&spi
->dev
, "failed to request pendown GPIO%d\n",
871 pdata
->gpio_pendown
);
875 ts
->gpio_pendown
= pdata
->gpio_pendown
;
879 static int __devinit
ads7846_probe(struct spi_device
*spi
)
882 struct ads7846_packet
*packet
;
883 struct input_dev
*input_dev
;
884 struct ads7846_platform_data
*pdata
= spi
->dev
.platform_data
;
885 struct spi_message
*m
;
886 struct spi_transfer
*x
;
891 dev_dbg(&spi
->dev
, "no IRQ?\n");
896 dev_dbg(&spi
->dev
, "no platform data?\n");
900 /* don't exceed max specified sample rate */
901 if (spi
->max_speed_hz
> (125000 * SAMPLE_BITS
)) {
902 dev_dbg(&spi
->dev
, "f(sample) %d KHz?\n",
903 (spi
->max_speed_hz
/SAMPLE_BITS
)/1000);
907 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
908 * that even if the hardware can do that, the SPI controller driver
909 * may not. So we stick to very-portable 8 bit words, both RX and TX.
911 spi
->bits_per_word
= 8;
912 spi
->mode
= SPI_MODE_0
;
913 err
= spi_setup(spi
);
917 ts
= kzalloc(sizeof(struct ads7846
), GFP_KERNEL
);
918 packet
= kzalloc(sizeof(struct ads7846_packet
), GFP_KERNEL
);
919 input_dev
= input_allocate_device();
920 if (!ts
|| !packet
|| !input_dev
) {
925 dev_set_drvdata(&spi
->dev
, ts
);
929 ts
->input
= input_dev
;
930 ts
->vref_mv
= pdata
->vref_mv
;
931 ts
->swap_xy
= pdata
->swap_xy
;
933 hrtimer_init(&ts
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
934 ts
->timer
.function
= ads7846_timer
;
936 spin_lock_init(&ts
->lock
);
938 ts
->model
= pdata
->model
? : 7846;
939 ts
->vref_delay_usecs
= pdata
->vref_delay_usecs
? : 100;
940 ts
->x_plate_ohms
= pdata
->x_plate_ohms
? : 400;
941 ts
->pressure_max
= pdata
->pressure_max
? : ~0;
943 if (pdata
->filter
!= NULL
) {
944 if (pdata
->filter_init
!= NULL
) {
945 err
= pdata
->filter_init(pdata
, &ts
->filter_data
);
949 ts
->filter
= pdata
->filter
;
950 ts
->filter_cleanup
= pdata
->filter_cleanup
;
951 } else if (pdata
->debounce_max
) {
952 ts
->debounce_max
= pdata
->debounce_max
;
953 if (ts
->debounce_max
< 2)
954 ts
->debounce_max
= 2;
955 ts
->debounce_tol
= pdata
->debounce_tol
;
956 ts
->debounce_rep
= pdata
->debounce_rep
;
957 ts
->filter
= ads7846_debounce
;
958 ts
->filter_data
= ts
;
960 ts
->filter
= ads7846_no_filter
;
962 err
= setup_pendown(spi
, ts
);
964 goto err_cleanup_filter
;
966 if (pdata
->penirq_recheck_delay_usecs
)
967 ts
->penirq_recheck_delay_usecs
=
968 pdata
->penirq_recheck_delay_usecs
;
970 ts
->wait_for_sync
= pdata
->wait_for_sync
? : null_wait_for_sync
;
972 snprintf(ts
->phys
, sizeof(ts
->phys
), "%s/input0", dev_name(&spi
->dev
));
973 snprintf(ts
->name
, sizeof(ts
->name
), "ADS%d Touchscreen", ts
->model
);
975 input_dev
->name
= ts
->name
;
976 input_dev
->phys
= ts
->phys
;
977 input_dev
->dev
.parent
= &spi
->dev
;
979 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
980 input_dev
->keybit
[BIT_WORD(BTN_TOUCH
)] = BIT_MASK(BTN_TOUCH
);
981 input_set_abs_params(input_dev
, ABS_X
,
983 pdata
->x_max
? : MAX_12BIT
,
985 input_set_abs_params(input_dev
, ABS_Y
,
987 pdata
->y_max
? : MAX_12BIT
,
989 input_set_abs_params(input_dev
, ABS_PRESSURE
,
990 pdata
->pressure_min
, pdata
->pressure_max
, 0, 0);
992 vref
= pdata
->keep_vref_on
;
994 if (ts
->model
== 7873) {
995 /* The AD7873 is almost identical to the ADS7846
996 * keep VREF off during differential/ratiometric
1003 /* set up the transfers to read touchscreen state; this assumes we
1004 * use formula #2 for pressure, not #3.
1009 spi_message_init(m
);
1011 /* y- still on; turn on only y+ (and ADC) */
1012 packet
->read_y
= READ_Y(vref
);
1013 x
->tx_buf
= &packet
->read_y
;
1015 spi_message_add_tail(x
, m
);
1018 x
->rx_buf
= &packet
->tc
.y
;
1020 spi_message_add_tail(x
, m
);
1022 /* the first sample after switching drivers can be low quality;
1023 * optionally discard it, using a second one after the signals
1024 * have had enough time to stabilize.
1026 if (pdata
->settle_delay_usecs
) {
1027 x
->delay_usecs
= pdata
->settle_delay_usecs
;
1030 x
->tx_buf
= &packet
->read_y
;
1032 spi_message_add_tail(x
, m
);
1035 x
->rx_buf
= &packet
->tc
.y
;
1037 spi_message_add_tail(x
, m
);
1040 m
->complete
= ads7846_rx_val
;
1044 spi_message_init(m
);
1046 /* turn y- off, x+ on, then leave in lowpower */
1048 packet
->read_x
= READ_X(vref
);
1049 x
->tx_buf
= &packet
->read_x
;
1051 spi_message_add_tail(x
, m
);
1054 x
->rx_buf
= &packet
->tc
.x
;
1056 spi_message_add_tail(x
, m
);
1058 /* ... maybe discard first sample ... */
1059 if (pdata
->settle_delay_usecs
) {
1060 x
->delay_usecs
= pdata
->settle_delay_usecs
;
1063 x
->tx_buf
= &packet
->read_x
;
1065 spi_message_add_tail(x
, m
);
1068 x
->rx_buf
= &packet
->tc
.x
;
1070 spi_message_add_tail(x
, m
);
1073 m
->complete
= ads7846_rx_val
;
1076 /* turn y+ off, x- on; we'll use formula #2 */
1077 if (ts
->model
== 7846) {
1079 spi_message_init(m
);
1082 packet
->read_z1
= READ_Z1(vref
);
1083 x
->tx_buf
= &packet
->read_z1
;
1085 spi_message_add_tail(x
, m
);
1088 x
->rx_buf
= &packet
->tc
.z1
;
1090 spi_message_add_tail(x
, m
);
1092 /* ... maybe discard first sample ... */
1093 if (pdata
->settle_delay_usecs
) {
1094 x
->delay_usecs
= pdata
->settle_delay_usecs
;
1097 x
->tx_buf
= &packet
->read_z1
;
1099 spi_message_add_tail(x
, m
);
1102 x
->rx_buf
= &packet
->tc
.z1
;
1104 spi_message_add_tail(x
, m
);
1107 m
->complete
= ads7846_rx_val
;
1111 spi_message_init(m
);
1114 packet
->read_z2
= READ_Z2(vref
);
1115 x
->tx_buf
= &packet
->read_z2
;
1117 spi_message_add_tail(x
, m
);
1120 x
->rx_buf
= &packet
->tc
.z2
;
1122 spi_message_add_tail(x
, m
);
1124 /* ... maybe discard first sample ... */
1125 if (pdata
->settle_delay_usecs
) {
1126 x
->delay_usecs
= pdata
->settle_delay_usecs
;
1129 x
->tx_buf
= &packet
->read_z2
;
1131 spi_message_add_tail(x
, m
);
1134 x
->rx_buf
= &packet
->tc
.z2
;
1136 spi_message_add_tail(x
, m
);
1139 m
->complete
= ads7846_rx_val
;
1145 spi_message_init(m
);
1148 packet
->pwrdown
= PWRDOWN
;
1149 x
->tx_buf
= &packet
->pwrdown
;
1151 spi_message_add_tail(x
, m
);
1154 x
->rx_buf
= &packet
->dummy
;
1157 spi_message_add_tail(x
, m
);
1159 m
->complete
= ads7846_rx
;
1164 ts
->reg
= regulator_get(&spi
->dev
, "vcc");
1165 if (IS_ERR(ts
->reg
)) {
1166 dev_err(&spi
->dev
, "unable to get regulator: %ld\n",
1171 err
= regulator_enable(ts
->reg
);
1173 dev_err(&spi
->dev
, "unable to enable regulator: %d\n", err
);
1174 goto err_put_regulator
;
1177 if (request_irq(spi
->irq
, ads7846_irq
, IRQF_TRIGGER_FALLING
,
1178 spi
->dev
.driver
->name
, ts
)) {
1180 "trying pin change workaround on irq %d\n", spi
->irq
);
1181 err
= request_irq(spi
->irq
, ads7846_irq
,
1182 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
1183 spi
->dev
.driver
->name
, ts
);
1185 dev_dbg(&spi
->dev
, "irq %d busy?\n", spi
->irq
);
1186 goto err_disable_regulator
;
1190 err
= ads784x_hwmon_register(spi
, ts
);
1194 dev_info(&spi
->dev
, "touchscreen, irq %d\n", spi
->irq
);
1196 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1197 * the touchscreen, in case it's not connected.
1199 (void) ads7846_read12_ser(&spi
->dev
,
1200 READ_12BIT_SER(vaux
) | ADS_PD10_ALL_ON
);
1202 err
= sysfs_create_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1204 goto err_remove_hwmon
;
1206 err
= input_register_device(input_dev
);
1208 goto err_remove_attr_group
;
1210 device_init_wakeup(&spi
->dev
, pdata
->wakeup
);
1214 err_remove_attr_group
:
1215 sysfs_remove_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1217 ads784x_hwmon_unregister(spi
, ts
);
1219 free_irq(spi
->irq
, ts
);
1220 err_disable_regulator
:
1221 regulator_disable(ts
->reg
);
1223 regulator_put(ts
->reg
);
1225 if (ts
->gpio_pendown
!= -1)
1226 gpio_free(ts
->gpio_pendown
);
1228 if (ts
->filter_cleanup
)
1229 ts
->filter_cleanup(ts
->filter_data
);
1231 input_free_device(input_dev
);
1237 static int __devexit
ads7846_remove(struct spi_device
*spi
)
1239 struct ads7846
*ts
= dev_get_drvdata(&spi
->dev
);
1241 device_init_wakeup(&spi
->dev
, false);
1243 ads784x_hwmon_unregister(spi
, ts
);
1244 input_unregister_device(ts
->input
);
1246 ads7846_suspend(spi
, PMSG_SUSPEND
);
1248 sysfs_remove_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1250 free_irq(ts
->spi
->irq
, ts
);
1251 /* suspend left the IRQ disabled */
1252 enable_irq(ts
->spi
->irq
);
1254 regulator_disable(ts
->reg
);
1255 regulator_put(ts
->reg
);
1257 if (ts
->gpio_pendown
!= -1)
1258 gpio_free(ts
->gpio_pendown
);
1260 if (ts
->filter_cleanup
)
1261 ts
->filter_cleanup(ts
->filter_data
);
1266 dev_dbg(&spi
->dev
, "unregistered touchscreen\n");
1270 static struct spi_driver ads7846_driver
= {
1273 .bus
= &spi_bus_type
,
1274 .owner
= THIS_MODULE
,
1276 .probe
= ads7846_probe
,
1277 .remove
= __devexit_p(ads7846_remove
),
1278 .suspend
= ads7846_suspend
,
1279 .resume
= ads7846_resume
,
1282 static int __init
ads7846_init(void)
1284 return spi_register_driver(&ads7846_driver
);
1286 module_init(ads7846_init
);
1288 static void __exit
ads7846_exit(void)
1290 spi_unregister_driver(&ads7846_driver
);
1292 module_exit(ads7846_exit
);
1294 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1295 MODULE_LICENSE("GPL");
1296 MODULE_ALIAS("spi:ads7846");