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/spi/spi.h>
28 #include <linux/spi/ads7846.h>
32 #include <asm/mach-types.h>
33 #ifdef CONFIG_ARCH_OMAP
34 #include <asm/arch/gpio.h>
40 * This code has been heavily tested on a Nokia 770, and lightly
41 * tested on other ads7846 devices (OSK/Mistral, Lubbock).
42 * TSC2046 is just newer ads7846 silicon.
43 * Support for ads7843 tested on Atmel at91sam926x-EK.
44 * Support for ads7845 has only been stubbed in.
46 * IRQ handling needs a workaround because of a shortcoming in handling
47 * edge triggered IRQs on some platforms like the OMAP1/2. These
48 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
49 * have to maintain our own SW IRQ disabled status. This should be
50 * removed as soon as the affected platform's IRQ handling is fixed.
52 * app note sbaa036 talks in more detail about accurate sampling...
53 * that ought to help in situations like LCDs inducing noise (which
54 * can also be helped by using synch signals) and more generally.
55 * This driver tries to utilize the measures described in the app
56 * note. The strength of filtering can be set in the board-* specific
60 #define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
61 #define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
63 /* this driver doesn't aim at the peak continuous sample rate */
64 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
67 /* For portability, we can't read 12 bit values using SPI (which
68 * would make the controller deliver them as native byteorder u16
69 * with msbs zeroed). Instead, we read them as two 8-bit values,
70 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
79 struct input_dev
*input
;
82 struct spi_device
*spi
;
84 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
85 struct attribute_group
*attr_group
;
94 u8 read_x
, read_y
, read_z1
, read_z2
, pwrdown
;
95 u16 dummy
; /* for the pwrdown read */
98 struct spi_transfer xfer
[18];
99 struct spi_message msg
[5];
100 struct spi_message
*last_msg
;
110 u16 penirq_recheck_delay_usecs
;
113 struct hrtimer timer
;
114 unsigned pendown
:1; /* P: lock */
115 unsigned pending
:1; /* P: lock */
116 // FIXME remove "irq_disabled"
117 unsigned irq_disabled
:1; /* P: lock */
120 int (*filter
)(void *data
, int data_idx
, int *val
);
122 void (*filter_cleanup
)(void *data
);
123 int (*get_pendown_state
)(void);
126 /* leave chip selected when we're done, for quicker re-select? */
128 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
130 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
133 /*--------------------------------------------------------------------------*/
135 /* The ADS7846 has touchscreen and other sensors.
136 * Earlier ads784x chips are somewhat compatible.
138 #define ADS_START (1 << 7)
139 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
140 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
141 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
142 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
143 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
144 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
145 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
146 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
147 #define ADS_8_BIT (1 << 3)
148 #define ADS_12_BIT (0 << 3)
149 #define ADS_SER (1 << 2) /* non-differential */
150 #define ADS_DFR (0 << 2) /* differential */
151 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
152 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
153 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
154 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
156 #define MAX_12BIT ((1<<12)-1)
158 /* leave ADC powered up (disables penirq) between differential samples */
159 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
160 | ADS_12_BIT | ADS_DFR | \
161 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
163 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
164 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
165 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
167 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
168 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
170 /* single-ended samples need to first power up reference voltage;
171 * we leave both ADC and VREF powered
173 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
174 | ADS_12_BIT | ADS_SER)
176 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
177 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
179 /*--------------------------------------------------------------------------*/
182 * Non-touchscreen sensors only use single-ended conversions.
183 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
184 * ads7846 lets that pin be unconnected, to use internal vREF.
186 static unsigned vREF_mV
;
187 module_param(vREF_mV
, uint
, 0);
188 MODULE_PARM_DESC(vREF_mV
, "external vREF voltage, in milliVolts");
196 struct spi_message msg
;
197 struct spi_transfer xfer
[6];
200 static void ads7846_enable(struct ads7846
*ts
);
201 static void ads7846_disable(struct ads7846
*ts
);
203 static int device_suspended(struct device
*dev
)
205 struct ads7846
*ts
= dev_get_drvdata(dev
);
206 return dev
->power
.power_state
.event
!= PM_EVENT_ON
|| ts
->disabled
;
209 static int ads7846_read12_ser(struct device
*dev
, unsigned command
)
211 struct spi_device
*spi
= to_spi_device(dev
);
212 struct ads7846
*ts
= dev_get_drvdata(dev
);
213 struct ser_req
*req
= kzalloc(sizeof *req
, GFP_KERNEL
);
221 spi_message_init(&req
->msg
);
223 /* FIXME boards with ads7846 might use external vref instead ... */
224 use_internal
= (ts
->model
== 7846);
226 /* maybe turn on internal vREF, and let it settle */
228 req
->ref_on
= REF_ON
;
229 req
->xfer
[0].tx_buf
= &req
->ref_on
;
230 req
->xfer
[0].len
= 1;
231 spi_message_add_tail(&req
->xfer
[0], &req
->msg
);
233 req
->xfer
[1].rx_buf
= &req
->scratch
;
234 req
->xfer
[1].len
= 2;
236 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
237 req
->xfer
[1].delay_usecs
= ts
->vref_delay_usecs
;
238 spi_message_add_tail(&req
->xfer
[1], &req
->msg
);
242 req
->command
= (u8
) command
;
243 req
->xfer
[2].tx_buf
= &req
->command
;
244 req
->xfer
[2].len
= 1;
245 spi_message_add_tail(&req
->xfer
[2], &req
->msg
);
247 req
->xfer
[3].rx_buf
= &req
->sample
;
248 req
->xfer
[3].len
= 2;
249 spi_message_add_tail(&req
->xfer
[3], &req
->msg
);
251 /* REVISIT: take a few more samples, and compare ... */
253 /* converter in low power mode & enable PENIRQ */
254 req
->ref_off
= PWRDOWN
;
255 req
->xfer
[4].tx_buf
= &req
->ref_off
;
256 req
->xfer
[4].len
= 1;
257 spi_message_add_tail(&req
->xfer
[4], &req
->msg
);
259 req
->xfer
[5].rx_buf
= &req
->scratch
;
260 req
->xfer
[5].len
= 2;
261 CS_CHANGE(req
->xfer
[5]);
262 spi_message_add_tail(&req
->xfer
[5], &req
->msg
);
264 ts
->irq_disabled
= 1;
265 disable_irq(spi
->irq
);
266 status
= spi_sync(spi
, &req
->msg
);
267 ts
->irq_disabled
= 0;
268 enable_irq(spi
->irq
);
271 status
= req
->msg
.status
;
273 /* on-wire is a must-ignore bit, a BE12 value, then padding */
274 sample
= be16_to_cpu(req
->sample
);
275 sample
= sample
>> 3;
279 return status
? status
: sample
;
282 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
284 #define SHOW(name, var, adjust) static ssize_t \
285 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
287 struct ads7846 *ts = dev_get_drvdata(dev); \
288 ssize_t v = ads7846_read12_ser(dev, \
289 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
292 return sprintf(buf, "%u\n", adjust(ts, v)); \
294 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
297 /* Sysfs conventions report temperatures in millidegrees Celcius.
298 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
299 * accuracy scheme without calibration data. For now we won't try either;
300 * userspace sees raw sensor values, and must scale/calibrate appropriately.
302 static inline unsigned null_adjust(struct ads7846
*ts
, ssize_t v
)
307 SHOW(temp0
, temp0
, null_adjust
) /* temp1_input */
308 SHOW(temp1
, temp1
, null_adjust
) /* temp2_input */
311 /* sysfs conventions report voltages in millivolts. We can convert voltages
312 * if we know vREF. userspace may need to scale vAUX to match the board's
313 * external resistors; we assume that vBATT only uses the internal ones.
315 static inline unsigned vaux_adjust(struct ads7846
*ts
, ssize_t v
)
319 /* external resistors may scale vAUX into 0..vREF */
321 retval
= retval
>> 12;
325 static inline unsigned vbatt_adjust(struct ads7846
*ts
, ssize_t v
)
327 unsigned retval
= vaux_adjust(ts
, v
);
329 /* ads7846 has a resistor ladder to scale this signal down */
330 if (ts
->model
== 7846)
335 SHOW(in0_input
, vaux
, vaux_adjust
)
336 SHOW(in1_input
, vbatt
, vbatt_adjust
)
339 static struct attribute
*ads7846_attributes
[] = {
340 &dev_attr_temp0
.attr
,
341 &dev_attr_temp1
.attr
,
342 &dev_attr_in0_input
.attr
,
343 &dev_attr_in1_input
.attr
,
347 static struct attribute_group ads7846_attr_group
= {
348 .attrs
= ads7846_attributes
,
351 static struct attribute
*ads7843_attributes
[] = {
352 &dev_attr_in0_input
.attr
,
353 &dev_attr_in1_input
.attr
,
357 static struct attribute_group ads7843_attr_group
= {
358 .attrs
= ads7843_attributes
,
361 static struct attribute
*ads7845_attributes
[] = {
362 &dev_attr_in0_input
.attr
,
366 static struct attribute_group ads7845_attr_group
= {
367 .attrs
= ads7845_attributes
,
370 static int ads784x_hwmon_register(struct spi_device
*spi
, struct ads7846
*ts
)
372 struct device
*hwmon
;
375 /* hwmon sensors need a reference voltage */
379 dev_dbg(&spi
->dev
, "assuming 2.5V internal vREF\n");
387 "external vREF for ADS%d not specified\n",
394 /* different chips have different sensor groups */
397 ts
->attr_group
= &ads7846_attr_group
;
400 ts
->attr_group
= &ads7845_attr_group
;
403 ts
->attr_group
= &ads7843_attr_group
;
406 dev_dbg(&spi
->dev
, "ADS%d not recognized\n", ts
->model
);
410 err
= sysfs_create_group(&spi
->dev
.kobj
, ts
->attr_group
);
414 hwmon
= hwmon_device_register(&spi
->dev
);
416 sysfs_remove_group(&spi
->dev
.kobj
, ts
->attr_group
);
417 return PTR_ERR(hwmon
);
424 static void ads784x_hwmon_unregister(struct spi_device
*spi
,
428 sysfs_remove_group(&spi
->dev
.kobj
, ts
->attr_group
);
429 hwmon_device_unregister(ts
->hwmon
);
434 static inline int ads784x_hwmon_register(struct spi_device
*spi
,
440 static inline void ads784x_hwmon_unregister(struct spi_device
*spi
,
446 static int is_pen_down(struct device
*dev
)
448 struct ads7846
*ts
= dev_get_drvdata(dev
);
453 static ssize_t
ads7846_pen_down_show(struct device
*dev
,
454 struct device_attribute
*attr
, char *buf
)
456 return sprintf(buf
, "%u\n", is_pen_down(dev
));
459 static DEVICE_ATTR(pen_down
, S_IRUGO
, ads7846_pen_down_show
, NULL
);
461 static ssize_t
ads7846_disable_show(struct device
*dev
,
462 struct device_attribute
*attr
, char *buf
)
464 struct ads7846
*ts
= dev_get_drvdata(dev
);
466 return sprintf(buf
, "%u\n", ts
->disabled
);
469 static ssize_t
ads7846_disable_store(struct device
*dev
,
470 struct device_attribute
*attr
,
471 const char *buf
, size_t count
)
473 struct ads7846
*ts
= dev_get_drvdata(dev
);
477 i
= simple_strtoul(buf
, &endp
, 10);
478 spin_lock_irq(&ts
->lock
);
485 spin_unlock_irq(&ts
->lock
);
490 static DEVICE_ATTR(disable
, 0664, ads7846_disable_show
, ads7846_disable_store
);
492 static struct attribute
*ads784x_attributes
[] = {
493 &dev_attr_pen_down
.attr
,
494 &dev_attr_disable
.attr
,
498 static struct attribute_group ads784x_attr_group
= {
499 .attrs
= ads784x_attributes
,
502 /*--------------------------------------------------------------------------*/
505 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
506 * to retrieve touchscreen status.
508 * The SPI transfer completion callback does the real work. It reports
509 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
512 static void ads7846_rx(void *ads
)
514 struct ads7846
*ts
= ads
;
518 /* ads7846_rx_val() did in-place conversion (including byteswap) from
519 * on-the-wire format as part of debouncing to get stable readings.
526 /* range filtering */
530 if (likely(x
&& z1
)) {
531 /* compute touch pressure resistance using equation #2 */
535 Rt
*= ts
->x_plate_ohms
;
537 Rt
= (Rt
+ 2047) >> 12;
541 if (ts
->model
== 7843)
542 Rt
= ts
->pressure_max
/ 2;
544 /* Sample found inconsistent by debouncing or pressure is beyond
545 * the maximum. Don't report it to user space, repeat at least
546 * once more the measurement
548 if (ts
->tc
.ignore
|| Rt
> ts
->pressure_max
) {
550 pr_debug("%s: ignored %d pressure %d\n",
551 ts
->spi
->dev
.bus_id
, ts
->tc
.ignore
, Rt
);
553 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_PERIOD
),
558 /* Maybe check the pendown state before reporting. This discards
559 * false readings when the pen is lifted.
561 if (ts
->penirq_recheck_delay_usecs
) {
562 udelay(ts
->penirq_recheck_delay_usecs
);
563 if (!ts
->get_pendown_state())
567 /* NOTE: We can't rely on the pressure to determine the pen down
568 * state, even this controller has a pressure sensor. The pressure
569 * value can fluctuate for quite a while after lifting the pen and
570 * in some cases may not even settle at the expected value.
572 * The only safe way to check for the pen up condition is in the
573 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
576 struct input_dev
*input
= ts
->input
;
579 input_report_key(input
, BTN_TOUCH
, 1);
582 dev_dbg(&ts
->spi
->dev
, "DOWN\n");
585 input_report_abs(input
, ABS_X
, x
);
586 input_report_abs(input
, ABS_Y
, y
);
587 input_report_abs(input
, ABS_PRESSURE
, Rt
);
591 dev_dbg(&ts
->spi
->dev
, "%4d/%4d/%4d\n", x
, y
, Rt
);
595 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_PERIOD
),
599 static int ads7846_debounce(void *ads
, int data_idx
, int *val
)
601 struct ads7846
*ts
= ads
;
603 if (!ts
->read_cnt
|| (abs(ts
->last_read
- *val
) > ts
->debounce_tol
)) {
604 /* Start over collecting consistent readings. */
606 /* Repeat it, if this was the first read or the read
607 * wasn't consistent enough. */
608 if (ts
->read_cnt
< ts
->debounce_max
) {
609 ts
->last_read
= *val
;
611 return ADS7846_FILTER_REPEAT
;
613 /* Maximum number of debouncing reached and still
614 * not enough number of consistent readings. Abort
615 * the whole sample, repeat it in the next sampling
619 return ADS7846_FILTER_IGNORE
;
622 if (++ts
->read_rep
> ts
->debounce_rep
) {
623 /* Got a good reading for this coordinate,
624 * go for the next one. */
627 return ADS7846_FILTER_OK
;
629 /* Read more values that are consistent. */
631 return ADS7846_FILTER_REPEAT
;
636 static int ads7846_no_filter(void *ads
, int data_idx
, int *val
)
638 return ADS7846_FILTER_OK
;
641 static void ads7846_rx_val(void *ads
)
643 struct ads7846
*ts
= ads
;
644 struct spi_message
*m
;
645 struct spi_transfer
*t
;
651 m
= &ts
->msg
[ts
->msg_idx
];
652 t
= list_entry(m
->transfers
.prev
, struct spi_transfer
, transfer_list
);
655 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
656 * built from two 8 bit values written msb-first.
658 val
= be16_to_cpu(*rx_val
) >> 3;
660 action
= ts
->filter(ts
->filter_data
, ts
->msg_idx
, &val
);
662 case ADS7846_FILTER_REPEAT
:
664 case ADS7846_FILTER_IGNORE
:
666 /* Last message will contain ads7846_rx() as the
667 * completion function.
671 case ADS7846_FILTER_OK
:
674 m
= &ts
->msg
[++ts
->msg_idx
];
679 status
= spi_async(ts
->spi
, m
);
681 dev_err(&ts
->spi
->dev
, "spi_async --> %d\n",
685 static enum hrtimer_restart
ads7846_timer(struct hrtimer
*handle
)
687 struct ads7846
*ts
= container_of(handle
, struct ads7846
, timer
);
690 spin_lock_irq(&ts
->lock
);
692 if (unlikely(!ts
->get_pendown_state() ||
693 device_suspended(&ts
->spi
->dev
))) {
695 struct input_dev
*input
= ts
->input
;
697 input_report_key(input
, BTN_TOUCH
, 0);
698 input_report_abs(input
, ABS_PRESSURE
, 0);
703 dev_dbg(&ts
->spi
->dev
, "UP\n");
707 /* measurement cycle ended */
708 if (!device_suspended(&ts
->spi
->dev
)) {
709 ts
->irq_disabled
= 0;
710 enable_irq(ts
->spi
->irq
);
714 /* pen is still down, continue with the measurement */
716 status
= spi_async(ts
->spi
, &ts
->msg
[0]);
718 dev_err(&ts
->spi
->dev
, "spi_async --> %d\n", status
);
721 spin_unlock_irq(&ts
->lock
);
722 return HRTIMER_NORESTART
;
725 static irqreturn_t
ads7846_irq(int irq
, void *handle
)
727 struct ads7846
*ts
= handle
;
730 spin_lock_irqsave(&ts
->lock
, flags
);
731 if (likely(ts
->get_pendown_state())) {
732 if (!ts
->irq_disabled
) {
733 /* The ARM do_simple_IRQ() dispatcher doesn't act
734 * like the other dispatchers: it will report IRQs
735 * even after they've been disabled. We work around
736 * that here. (The "generic irq" framework may help...)
738 ts
->irq_disabled
= 1;
739 disable_irq(ts
->spi
->irq
);
741 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_DELAY
),
745 spin_unlock_irqrestore(&ts
->lock
, flags
);
750 /*--------------------------------------------------------------------------*/
752 /* Must be called with ts->lock held */
753 static void ads7846_disable(struct ads7846
*ts
)
760 /* are we waiting for IRQ, or polling? */
762 ts
->irq_disabled
= 1;
763 disable_irq(ts
->spi
->irq
);
765 /* the timer will run at least once more, and
766 * leave everything in a clean state, IRQ disabled
768 while (ts
->pending
) {
769 spin_unlock_irq(&ts
->lock
);
771 spin_lock_irq(&ts
->lock
);
775 /* we know the chip's in lowpower mode since we always
776 * leave it that way after every request
781 /* Must be called with ts->lock held */
782 static void ads7846_enable(struct ads7846
*ts
)
788 ts
->irq_disabled
= 0;
789 enable_irq(ts
->spi
->irq
);
792 static int ads7846_suspend(struct spi_device
*spi
, pm_message_t message
)
794 struct ads7846
*ts
= dev_get_drvdata(&spi
->dev
);
796 spin_lock_irq(&ts
->lock
);
798 spi
->dev
.power
.power_state
= message
;
801 spin_unlock_irq(&ts
->lock
);
807 static int ads7846_resume(struct spi_device
*spi
)
809 struct ads7846
*ts
= dev_get_drvdata(&spi
->dev
);
811 spin_lock_irq(&ts
->lock
);
813 spi
->dev
.power
.power_state
= PMSG_ON
;
816 spin_unlock_irq(&ts
->lock
);
821 static int __devinit
ads7846_probe(struct spi_device
*spi
)
824 struct input_dev
*input_dev
;
825 struct ads7846_platform_data
*pdata
= spi
->dev
.platform_data
;
826 struct spi_message
*m
;
827 struct spi_transfer
*x
;
832 dev_dbg(&spi
->dev
, "no IRQ?\n");
837 dev_dbg(&spi
->dev
, "no platform data?\n");
841 /* don't exceed max specified sample rate */
842 if (spi
->max_speed_hz
> (125000 * SAMPLE_BITS
)) {
843 dev_dbg(&spi
->dev
, "f(sample) %d KHz?\n",
844 (spi
->max_speed_hz
/SAMPLE_BITS
)/1000);
848 /* REVISIT when the irq can be triggered active-low, or if for some
849 * reason the touchscreen isn't hooked up, we don't need to access
852 if (pdata
->get_pendown_state
== NULL
) {
853 dev_dbg(&spi
->dev
, "no get_pendown_state function?\n");
857 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
858 * that even if the hardware can do that, the SPI controller driver
859 * may not. So we stick to very-portable 8 bit words, both RX and TX.
861 spi
->bits_per_word
= 8;
862 spi
->mode
= SPI_MODE_0
;
863 err
= spi_setup(spi
);
867 ts
= kzalloc(sizeof(struct ads7846
), GFP_KERNEL
);
868 input_dev
= input_allocate_device();
869 if (!ts
|| !input_dev
) {
874 dev_set_drvdata(&spi
->dev
, ts
);
875 spi
->dev
.power
.power_state
= PMSG_ON
;
878 ts
->input
= input_dev
;
880 hrtimer_init(&ts
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
881 ts
->timer
.function
= ads7846_timer
;
883 spin_lock_init(&ts
->lock
);
885 ts
->model
= pdata
->model
? : 7846;
886 ts
->vref_delay_usecs
= pdata
->vref_delay_usecs
? : 100;
887 ts
->x_plate_ohms
= pdata
->x_plate_ohms
? : 400;
888 ts
->pressure_max
= pdata
->pressure_max
? : ~0;
890 if (pdata
->filter
!= NULL
) {
891 if (pdata
->filter_init
!= NULL
) {
892 err
= pdata
->filter_init(pdata
, &ts
->filter_data
);
896 ts
->filter
= pdata
->filter
;
897 ts
->filter_cleanup
= pdata
->filter_cleanup
;
898 } else if (pdata
->debounce_max
) {
899 ts
->debounce_max
= pdata
->debounce_max
;
900 if (ts
->debounce_max
< 2)
901 ts
->debounce_max
= 2;
902 ts
->debounce_tol
= pdata
->debounce_tol
;
903 ts
->debounce_rep
= pdata
->debounce_rep
;
904 ts
->filter
= ads7846_debounce
;
905 ts
->filter_data
= ts
;
907 ts
->filter
= ads7846_no_filter
;
908 ts
->get_pendown_state
= pdata
->get_pendown_state
;
910 if (pdata
->penirq_recheck_delay_usecs
)
911 ts
->penirq_recheck_delay_usecs
=
912 pdata
->penirq_recheck_delay_usecs
;
914 snprintf(ts
->phys
, sizeof(ts
->phys
), "%s/input0", spi
->dev
.bus_id
);
916 input_dev
->name
= "ADS784x Touchscreen";
917 input_dev
->phys
= ts
->phys
;
918 input_dev
->dev
.parent
= &spi
->dev
;
920 input_dev
->evbit
[0] = BIT(EV_KEY
) | BIT(EV_ABS
);
921 input_dev
->keybit
[LONG(BTN_TOUCH
)] = BIT(BTN_TOUCH
);
922 input_set_abs_params(input_dev
, ABS_X
,
924 pdata
->x_max
? : MAX_12BIT
,
926 input_set_abs_params(input_dev
, ABS_Y
,
928 pdata
->y_max
? : MAX_12BIT
,
930 input_set_abs_params(input_dev
, ABS_PRESSURE
,
931 pdata
->pressure_min
, pdata
->pressure_max
, 0, 0);
933 vref
= pdata
->keep_vref_on
;
935 /* set up the transfers to read touchscreen state; this assumes we
936 * use formula #2 for pressure, not #3.
943 /* y- still on; turn on only y+ (and ADC) */
944 ts
->read_y
= READ_Y(vref
);
945 x
->tx_buf
= &ts
->read_y
;
947 spi_message_add_tail(x
, m
);
950 x
->rx_buf
= &ts
->tc
.y
;
952 spi_message_add_tail(x
, m
);
954 /* the first sample after switching drivers can be low quality;
955 * optionally discard it, using a second one after the signals
956 * have had enough time to stabilize.
958 if (pdata
->settle_delay_usecs
) {
959 x
->delay_usecs
= pdata
->settle_delay_usecs
;
962 x
->tx_buf
= &ts
->read_y
;
964 spi_message_add_tail(x
, m
);
967 x
->rx_buf
= &ts
->tc
.y
;
969 spi_message_add_tail(x
, m
);
972 m
->complete
= ads7846_rx_val
;
978 /* turn y- off, x+ on, then leave in lowpower */
980 ts
->read_x
= READ_X(vref
);
981 x
->tx_buf
= &ts
->read_x
;
983 spi_message_add_tail(x
, m
);
986 x
->rx_buf
= &ts
->tc
.x
;
988 spi_message_add_tail(x
, m
);
990 /* ... maybe discard first sample ... */
991 if (pdata
->settle_delay_usecs
) {
992 x
->delay_usecs
= pdata
->settle_delay_usecs
;
995 x
->tx_buf
= &ts
->read_x
;
997 spi_message_add_tail(x
, m
);
1000 x
->rx_buf
= &ts
->tc
.x
;
1002 spi_message_add_tail(x
, m
);
1005 m
->complete
= ads7846_rx_val
;
1008 /* turn y+ off, x- on; we'll use formula #2 */
1009 if (ts
->model
== 7846) {
1011 spi_message_init(m
);
1014 ts
->read_z1
= READ_Z1(vref
);
1015 x
->tx_buf
= &ts
->read_z1
;
1017 spi_message_add_tail(x
, m
);
1020 x
->rx_buf
= &ts
->tc
.z1
;
1022 spi_message_add_tail(x
, m
);
1024 /* ... maybe discard first sample ... */
1025 if (pdata
->settle_delay_usecs
) {
1026 x
->delay_usecs
= pdata
->settle_delay_usecs
;
1029 x
->tx_buf
= &ts
->read_z1
;
1031 spi_message_add_tail(x
, m
);
1034 x
->rx_buf
= &ts
->tc
.z1
;
1036 spi_message_add_tail(x
, m
);
1039 m
->complete
= ads7846_rx_val
;
1043 spi_message_init(m
);
1046 ts
->read_z2
= READ_Z2(vref
);
1047 x
->tx_buf
= &ts
->read_z2
;
1049 spi_message_add_tail(x
, m
);
1052 x
->rx_buf
= &ts
->tc
.z2
;
1054 spi_message_add_tail(x
, m
);
1056 /* ... maybe discard first sample ... */
1057 if (pdata
->settle_delay_usecs
) {
1058 x
->delay_usecs
= pdata
->settle_delay_usecs
;
1061 x
->tx_buf
= &ts
->read_z2
;
1063 spi_message_add_tail(x
, m
);
1066 x
->rx_buf
= &ts
->tc
.z2
;
1068 spi_message_add_tail(x
, m
);
1071 m
->complete
= ads7846_rx_val
;
1077 spi_message_init(m
);
1080 ts
->pwrdown
= PWRDOWN
;
1081 x
->tx_buf
= &ts
->pwrdown
;
1083 spi_message_add_tail(x
, m
);
1086 x
->rx_buf
= &ts
->dummy
;
1089 spi_message_add_tail(x
, m
);
1091 m
->complete
= ads7846_rx
;
1096 if (request_irq(spi
->irq
, ads7846_irq
, IRQF_TRIGGER_FALLING
,
1097 spi
->dev
.driver
->name
, ts
)) {
1098 dev_dbg(&spi
->dev
, "irq %d busy?\n", spi
->irq
);
1100 goto err_cleanup_filter
;
1103 err
= ads784x_hwmon_register(spi
, ts
);
1107 dev_info(&spi
->dev
, "touchscreen, irq %d\n", spi
->irq
);
1109 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1110 * the touchscreen, in case it's not connected.
1112 (void) ads7846_read12_ser(&spi
->dev
,
1113 READ_12BIT_SER(vaux
) | ADS_PD10_ALL_ON
);
1115 err
= sysfs_create_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1117 goto err_remove_hwmon
;
1119 err
= input_register_device(input_dev
);
1121 goto err_remove_attr_group
;
1125 err_remove_attr_group
:
1126 sysfs_remove_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1128 ads784x_hwmon_unregister(spi
, ts
);
1130 free_irq(spi
->irq
, ts
);
1132 if (ts
->filter_cleanup
)
1133 ts
->filter_cleanup(ts
->filter_data
);
1135 input_free_device(input_dev
);
1140 static int __devexit
ads7846_remove(struct spi_device
*spi
)
1142 struct ads7846
*ts
= dev_get_drvdata(&spi
->dev
);
1144 ads784x_hwmon_unregister(spi
, ts
);
1145 input_unregister_device(ts
->input
);
1147 ads7846_suspend(spi
, PMSG_SUSPEND
);
1149 sysfs_remove_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1151 free_irq(ts
->spi
->irq
, ts
);
1152 /* suspend left the IRQ disabled */
1153 enable_irq(ts
->spi
->irq
);
1155 if (ts
->filter_cleanup
)
1156 ts
->filter_cleanup(ts
->filter_data
);
1160 dev_dbg(&spi
->dev
, "unregistered touchscreen\n");
1164 static struct spi_driver ads7846_driver
= {
1167 .bus
= &spi_bus_type
,
1168 .owner
= THIS_MODULE
,
1170 .probe
= ads7846_probe
,
1171 .remove
= __devexit_p(ads7846_remove
),
1172 .suspend
= ads7846_suspend
,
1173 .resume
= ads7846_resume
,
1176 static int __init
ads7846_init(void)
1178 /* grr, board-specific init should stay out of drivers!! */
1180 #ifdef CONFIG_ARCH_OMAP
1181 if (machine_is_omap_osk()) {
1182 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
1183 omap_request_gpio(4);
1184 omap_set_gpio_direction(4, 1);
1185 omap_request_gpio(6);
1186 omap_set_gpio_direction(6, 1);
1188 // also TI 1510 Innovator, bitbanging through FPGA
1190 // also Palm Tungsten T2
1194 // also Dell Axim X50
1195 // also HP iPaq H191x/H192x/H415x/H435x
1196 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
1197 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
1199 // Atmel at91sam9261-EK uses ads7843
1201 // also various AMD Au1x00 devel boards
1203 return spi_register_driver(&ads7846_driver
);
1205 module_init(ads7846_init
);
1207 static void __exit
ads7846_exit(void)
1209 spi_unregister_driver(&ads7846_driver
);
1211 #ifdef CONFIG_ARCH_OMAP
1212 if (machine_is_omap_osk()) {
1219 module_exit(ads7846_exit
);
1221 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1222 MODULE_LICENSE("GPL");