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 #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 */)
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.
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 */
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];
68 struct input_dev
*input
;
72 struct spi_device
*spi
;
73 struct regulator
*reg
;
75 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
76 struct attribute_group
*attr_group
;
88 struct ads7846_packet
*packet
;
90 struct spi_transfer xfer
[18];
91 struct spi_message msg
[5];
92 struct spi_message
*last_msg
;
102 u16 penirq_recheck_delay_usecs
;
105 struct hrtimer timer
;
106 unsigned pendown
:1; /* P: lock */
107 unsigned pending
:1; /* P: lock */
108 unsigned irq_disabled
:1; /* P: lock */
110 unsigned is_suspended
:1;
112 int (*filter
)(void *data
, int data_idx
, int *val
);
114 void (*filter_cleanup
)(void *data
);
115 int (*get_pendown_state
)(void);
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.
184 struct spi_message msg
;
185 struct spi_transfer xfer
[6];
188 struct ads7845_ser_req
{
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
);
216 spi_message_init(&req
->msg
);
218 use_internal
= (ts
->model
== 7846);
220 /* maybe turn on internal vREF, and let it settle */
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
);
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
);
265 /* on-wire is a must-ignore bit, a BE12 value, then padding */
266 status
= be16_to_cpu(req
->sample
);
267 status
= status
>> 3;
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
);
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
);
300 /* BE12 value, then padding */
301 status
= be16_to_cpu(*((u16
*)&req
->sample
[1]));
302 status
= status
>> 3;
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); \
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
)
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
)
347 /* external resistors may scale vAUX into 0..vREF */
348 retval
*= ts
->vref_mv
;
349 retval
= retval
>> 12;
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)
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
,
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
,
385 static struct attribute_group ads7843_attr_group
= {
386 .attrs
= ads7843_attributes
,
389 static struct attribute
*ads7845_attributes
[] = {
390 &dev_attr_in0_input
.attr
,
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
;
403 /* hwmon sensors need a reference voltage */
407 dev_dbg(&spi
->dev
, "assuming 2.5V internal vREF\n");
415 "external vREF for ADS%d not specified\n",
422 /* different chips have different sensor groups */
425 ts
->attr_group
= &ads7846_attr_group
;
428 ts
->attr_group
= &ads7845_attr_group
;
431 ts
->attr_group
= &ads7843_attr_group
;
434 dev_dbg(&spi
->dev
, "ADS%d not recognized\n", ts
->model
);
438 err
= sysfs_create_group(&spi
->dev
.kobj
, ts
->attr_group
);
442 hwmon
= hwmon_device_register(&spi
->dev
);
444 sysfs_remove_group(&spi
->dev
.kobj
, ts
->attr_group
);
445 return PTR_ERR(hwmon
);
452 static void ads784x_hwmon_unregister(struct spi_device
*spi
,
456 sysfs_remove_group(&spi
->dev
.kobj
, ts
->attr_group
);
457 hwmon_device_unregister(ts
->hwmon
);
462 static inline int ads784x_hwmon_register(struct spi_device
*spi
,
468 static inline void ads784x_hwmon_unregister(struct spi_device
*spi
,
474 static int is_pen_down(struct device
*dev
)
476 struct ads7846
*ts
= dev_get_drvdata(dev
);
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
);
504 if (strict_strtoul(buf
, 10, &i
))
507 spin_lock_irq(&ts
->lock
);
514 spin_unlock_irq(&ts
->lock
);
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
,
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
;
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
;
575 /* range filtering */
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;
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 */
592 Rt
*= ts
->x_plate_ohms
;
594 Rt
= (Rt
+ 2047) >> 12;
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
),
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
))
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).
629 struct input_dev
*input
= ts
->input
;
632 input_report_key(input
, BTN_TOUCH
, 1);
634 dev_vdbg(&ts
->spi
->dev
, "DOWN\n");
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
);
645 dev_vdbg(&ts
->spi
->dev
, "%4d/%4d/%4d\n", x
, y
, Rt
);
648 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_PERIOD
),
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. */
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
;
664 return ADS7846_FILTER_REPEAT
;
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
672 return ADS7846_FILTER_IGNORE
;
675 if (++ts
->read_rep
> ts
->debounce_rep
) {
676 /* Got a good reading for this coordinate,
677 * go for the next one. */
680 return ADS7846_FILTER_OK
;
682 /* Read more values that are consistent. */
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
;
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;
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
);
718 case ADS7846_FILTER_REPEAT
:
720 case ADS7846_FILTER_IGNORE
:
721 packet
->tc
.ignore
= 1;
722 /* Last message will contain ads7846_rx() as the
723 * completion function.
727 case ADS7846_FILTER_OK
:
728 *(u16
*)t
->rx_buf
= val
;
729 packet
->tc
.ignore
= 0;
730 m
= &ts
->msg
[++ts
->msg_idx
];
736 status
= spi_async(ts
->spi
, m
);
738 dev_err(&ts
->spi
->dev
, "spi_async --> %d\n",
742 static enum hrtimer_restart
ads7846_timer(struct hrtimer
*handle
)
744 struct ads7846
*ts
= container_of(handle
, struct ads7846
, timer
);
747 spin_lock(&ts
->lock
);
749 if (unlikely(!get_pendown_state(ts
) ||
750 device_suspended(&ts
->spi
->dev
))) {
752 struct input_dev
*input
= ts
->input
;
754 input_report_key(input
, BTN_TOUCH
, 0);
755 input_report_abs(input
, ABS_PRESSURE
, 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
);
769 /* pen is still down, continue with the measurement */
772 status
= spi_async(ts
->spi
, &ts
->msg
[0]);
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
;
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
);
792 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_DELAY
),
796 spin_unlock_irqrestore(&ts
->lock
, flags
);
801 /*--------------------------------------------------------------------------*/
803 /* Must be called with ts->lock held */
804 static void ads7846_disable(struct ads7846
*ts
)
811 /* are we waiting for IRQ, or polling? */
813 ts
->irq_disabled
= 1;
814 disable_irq(ts
->spi
->irq
);
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
);
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
)
839 regulator_enable(ts
->reg
);
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;
855 spin_unlock_irq(&ts
->lock
);
857 if (device_may_wakeup(&ts
->spi
->dev
))
858 enable_irq_wake(ts
->spi
->irq
);
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;
876 spin_unlock_irq(&ts
->lock
);
881 static int __devinit
setup_pendown(struct spi_device
*spi
, struct ads7846
*ts
)
883 struct ads7846_platform_data
*pdata
= spi
->dev
.platform_data
;
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
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");
895 if (pdata
->get_pendown_state
) {
896 ts
->get_pendown_state
= pdata
->get_pendown_state
;
900 err
= gpio_request(pdata
->gpio_pendown
, "ads7846_pendown");
902 dev_err(&spi
->dev
, "failed to request pendown GPIO%d\n",
903 pdata
->gpio_pendown
);
907 ts
->gpio_pendown
= pdata
->gpio_pendown
;
911 static int __devinit
ads7846_probe(struct spi_device
*spi
)
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
;
924 dev_dbg(&spi
->dev
, "no IRQ?\n");
929 dev_dbg(&spi
->dev
, "no platform data?\n");
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);
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
);
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
) {
958 dev_set_drvdata(&spi
->dev
, ts
);
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
);
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
;
993 ts
->filter
= ads7846_no_filter
;
995 err
= setup_pendown(spi
, ts
);
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
,
1016 pdata
->x_max
? : MAX_12BIT
,
1018 input_set_abs_params(input_dev
, ABS_Y
,
1020 pdata
->y_max
? : MAX_12BIT
,
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
1036 /* set up the transfers to read touchscreen state; this assumes we
1037 * use formula #2 for pressure, not #3.
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];
1051 spi_message_add_tail(x
, m
);
1053 /* y- still on; turn on only y+ (and ADC) */
1054 packet
->read_y
= READ_Y(vref
);
1055 x
->tx_buf
= &packet
->read_y
;
1057 spi_message_add_tail(x
, m
);
1060 x
->rx_buf
= &packet
->tc
.y
;
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
;
1073 x
->tx_buf
= &packet
->read_y
;
1075 spi_message_add_tail(x
, m
);
1078 x
->rx_buf
= &packet
->tc
.y
;
1080 spi_message_add_tail(x
, m
);
1083 m
->complete
= ads7846_rx_val
;
1087 spi_message_init(m
);
1089 if (ts
->model
== 7845) {
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];
1097 spi_message_add_tail(x
, m
);
1099 /* turn y- off, x+ on, then leave in lowpower */
1101 packet
->read_x
= READ_X(vref
);
1102 x
->tx_buf
= &packet
->read_x
;
1104 spi_message_add_tail(x
, m
);
1107 x
->rx_buf
= &packet
->tc
.x
;
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
;
1117 x
->tx_buf
= &packet
->read_x
;
1119 spi_message_add_tail(x
, m
);
1122 x
->rx_buf
= &packet
->tc
.x
;
1124 spi_message_add_tail(x
, m
);
1127 m
->complete
= ads7846_rx_val
;
1130 /* turn y+ off, x- on; we'll use formula #2 */
1131 if (ts
->model
== 7846) {
1133 spi_message_init(m
);
1136 packet
->read_z1
= READ_Z1(vref
);
1137 x
->tx_buf
= &packet
->read_z1
;
1139 spi_message_add_tail(x
, m
);
1142 x
->rx_buf
= &packet
->tc
.z1
;
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
;
1151 x
->tx_buf
= &packet
->read_z1
;
1153 spi_message_add_tail(x
, m
);
1156 x
->rx_buf
= &packet
->tc
.z1
;
1158 spi_message_add_tail(x
, m
);
1161 m
->complete
= ads7846_rx_val
;
1165 spi_message_init(m
);
1168 packet
->read_z2
= READ_Z2(vref
);
1169 x
->tx_buf
= &packet
->read_z2
;
1171 spi_message_add_tail(x
, m
);
1174 x
->rx_buf
= &packet
->tc
.z2
;
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_z2
;
1185 spi_message_add_tail(x
, m
);
1188 x
->rx_buf
= &packet
->tc
.z2
;
1190 spi_message_add_tail(x
, m
);
1193 m
->complete
= ads7846_rx_val
;
1199 spi_message_init(m
);
1201 if (ts
->model
== 7845) {
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];
1210 packet
->pwrdown
= PWRDOWN
;
1211 x
->tx_buf
= &packet
->pwrdown
;
1213 spi_message_add_tail(x
, m
);
1216 x
->rx_buf
= &packet
->dummy
;
1221 spi_message_add_tail(x
, m
);
1223 m
->complete
= ads7846_rx
;
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
);
1235 err
= regulator_enable(ts
->reg
);
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
) {
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
);
1255 dev_dbg(&spi
->dev
, "irq %d busy?\n", spi
->irq
);
1256 goto err_disable_regulator
;
1259 err
= ads784x_hwmon_register(spi
, ts
);
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
);
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
);
1276 goto err_remove_hwmon
;
1278 err
= input_register_device(input_dev
);
1280 goto err_remove_attr_group
;
1282 device_init_wakeup(&spi
->dev
, pdata
->wakeup
);
1286 err_remove_attr_group
:
1287 sysfs_remove_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1289 ads784x_hwmon_unregister(spi
, ts
);
1291 free_irq(spi
->irq
, ts
);
1292 err_disable_regulator
:
1293 regulator_disable(ts
->reg
);
1295 regulator_put(ts
->reg
);
1297 if (ts
->gpio_pendown
!= -1)
1298 gpio_free(ts
->gpio_pendown
);
1300 if (ts
->filter_cleanup
)
1301 ts
->filter_cleanup(ts
->filter_data
);
1303 input_free_device(input_dev
);
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
);
1338 dev_dbg(&spi
->dev
, "unregistered touchscreen\n");
1342 static struct spi_driver ads7846_driver
= {
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");