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
;
86 struct class_device
*hwmon
;
94 u8 read_x
, read_y
, read_z1
, read_z2
, pwrdown
;
95 u16 dummy
; /* for the pwrdown read */
98 struct spi_transfer xfer
[10];
99 struct spi_message msg
[5];
100 struct spi_message
*last_msg
;
111 struct hrtimer timer
;
112 unsigned pendown
:1; /* P: lock */
113 unsigned pending
:1; /* P: lock */
114 // FIXME remove "irq_disabled"
115 unsigned irq_disabled
:1; /* P: lock */
118 int (*filter
)(void *data
, int data_idx
, int *val
);
120 void (*filter_cleanup
)(void *data
);
121 int (*get_pendown_state
)(void);
124 /* leave chip selected when we're done, for quicker re-select? */
126 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
128 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
131 /*--------------------------------------------------------------------------*/
133 /* The ADS7846 has touchscreen and other sensors.
134 * Earlier ads784x chips are somewhat compatible.
136 #define ADS_START (1 << 7)
137 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
138 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
139 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
140 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
141 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
142 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
143 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
144 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
145 #define ADS_8_BIT (1 << 3)
146 #define ADS_12_BIT (0 << 3)
147 #define ADS_SER (1 << 2) /* non-differential */
148 #define ADS_DFR (0 << 2) /* differential */
149 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
150 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
151 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
152 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
154 #define MAX_12BIT ((1<<12)-1)
156 /* leave ADC powered up (disables penirq) between differential samples */
157 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
158 | ADS_12_BIT | ADS_DFR | \
159 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
161 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
162 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
163 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
165 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
166 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
168 /* single-ended samples need to first power up reference voltage;
169 * we leave both ADC and VREF powered
171 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
172 | ADS_12_BIT | ADS_SER)
174 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
175 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
177 /*--------------------------------------------------------------------------*/
180 * Non-touchscreen sensors only use single-ended conversions.
181 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
182 * ads7846 lets that pin be unconnected, to use internal vREF.
184 static unsigned vREF_mV
;
185 module_param(vREF_mV
, uint
, 0);
186 MODULE_PARM_DESC(vREF_mV
, "external vREF voltage, in milliVolts");
194 struct spi_message msg
;
195 struct spi_transfer xfer
[6];
198 static void ads7846_enable(struct ads7846
*ts
);
199 static void ads7846_disable(struct ads7846
*ts
);
201 static int device_suspended(struct device
*dev
)
203 struct ads7846
*ts
= dev_get_drvdata(dev
);
204 return dev
->power
.power_state
.event
!= PM_EVENT_ON
|| ts
->disabled
;
207 static int ads7846_read12_ser(struct device
*dev
, unsigned command
)
209 struct spi_device
*spi
= to_spi_device(dev
);
210 struct ads7846
*ts
= dev_get_drvdata(dev
);
211 struct ser_req
*req
= kzalloc(sizeof *req
, GFP_KERNEL
);
219 spi_message_init(&req
->msg
);
221 /* FIXME boards with ads7846 might use external vref instead ... */
222 use_internal
= (ts
->model
== 7846);
224 /* maybe turn on internal vREF, and let it settle */
226 req
->ref_on
= REF_ON
;
227 req
->xfer
[0].tx_buf
= &req
->ref_on
;
228 req
->xfer
[0].len
= 1;
229 spi_message_add_tail(&req
->xfer
[0], &req
->msg
);
231 req
->xfer
[1].rx_buf
= &req
->scratch
;
232 req
->xfer
[1].len
= 2;
234 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
235 req
->xfer
[1].delay_usecs
= ts
->vref_delay_usecs
;
236 spi_message_add_tail(&req
->xfer
[1], &req
->msg
);
240 req
->command
= (u8
) command
;
241 req
->xfer
[2].tx_buf
= &req
->command
;
242 req
->xfer
[2].len
= 1;
243 spi_message_add_tail(&req
->xfer
[2], &req
->msg
);
245 req
->xfer
[3].rx_buf
= &req
->sample
;
246 req
->xfer
[3].len
= 2;
247 spi_message_add_tail(&req
->xfer
[3], &req
->msg
);
249 /* REVISIT: take a few more samples, and compare ... */
251 /* converter in low power mode & enable PENIRQ */
252 req
->ref_off
= PWRDOWN
;
253 req
->xfer
[4].tx_buf
= &req
->ref_off
;
254 req
->xfer
[4].len
= 1;
255 spi_message_add_tail(&req
->xfer
[4], &req
->msg
);
257 req
->xfer
[5].rx_buf
= &req
->scratch
;
258 req
->xfer
[5].len
= 2;
259 CS_CHANGE(req
->xfer
[5]);
260 spi_message_add_tail(&req
->xfer
[5], &req
->msg
);
262 ts
->irq_disabled
= 1;
263 disable_irq(spi
->irq
);
264 status
= spi_sync(spi
, &req
->msg
);
265 ts
->irq_disabled
= 0;
266 enable_irq(spi
->irq
);
269 status
= req
->msg
.status
;
271 /* on-wire is a must-ignore bit, a BE12 value, then padding */
272 sample
= be16_to_cpu(req
->sample
);
273 sample
= sample
>> 3;
277 return status
? status
: sample
;
280 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
282 #define SHOW(name, var, adjust) static ssize_t \
283 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
285 struct ads7846 *ts = dev_get_drvdata(dev); \
286 ssize_t v = ads7846_read12_ser(dev, \
287 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
290 return sprintf(buf, "%u\n", adjust(ts, v)); \
292 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
295 /* Sysfs conventions report temperatures in millidegrees Celcius.
296 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
297 * accuracy scheme without calibration data. For now we won't try either;
298 * userspace sees raw sensor values, and must scale/calibrate appropriately.
300 static inline unsigned null_adjust(struct ads7846
*ts
, ssize_t v
)
305 SHOW(temp0
, temp0
, null_adjust
) /* temp1_input */
306 SHOW(temp1
, temp1
, null_adjust
) /* temp2_input */
309 /* sysfs conventions report voltages in millivolts. We can convert voltages
310 * if we know vREF. userspace may need to scale vAUX to match the board's
311 * external resistors; we assume that vBATT only uses the internal ones.
313 static inline unsigned vaux_adjust(struct ads7846
*ts
, ssize_t v
)
317 /* external resistors may scale vAUX into 0..vREF */
319 retval
= retval
>> 12;
323 static inline unsigned vbatt_adjust(struct ads7846
*ts
, ssize_t v
)
325 unsigned retval
= vaux_adjust(ts
, v
);
327 /* ads7846 has a resistor ladder to scale this signal down */
328 if (ts
->model
== 7846)
333 SHOW(in0_input
, vaux
, vaux_adjust
)
334 SHOW(in1_input
, vbatt
, vbatt_adjust
)
337 static struct attribute
*ads7846_attributes
[] = {
338 &dev_attr_temp0
.attr
,
339 &dev_attr_temp1
.attr
,
340 &dev_attr_in0_input
.attr
,
341 &dev_attr_in1_input
.attr
,
345 static struct attribute_group ads7846_attr_group
= {
346 .attrs
= ads7846_attributes
,
349 static struct attribute
*ads7843_attributes
[] = {
350 &dev_attr_in0_input
.attr
,
351 &dev_attr_in1_input
.attr
,
355 static struct attribute_group ads7843_attr_group
= {
356 .attrs
= ads7843_attributes
,
359 static struct attribute
*ads7845_attributes
[] = {
360 &dev_attr_in0_input
.attr
,
364 static struct attribute_group ads7845_attr_group
= {
365 .attrs
= ads7845_attributes
,
368 static int ads784x_hwmon_register(struct spi_device
*spi
, struct ads7846
*ts
)
370 struct class_device
*hwmon
;
373 /* hwmon sensors need a reference voltage */
377 dev_dbg(&spi
->dev
, "assuming 2.5V internal vREF\n");
385 "external vREF for ADS%d not specified\n",
392 /* different chips have different sensor groups */
395 ts
->attr_group
= &ads7846_attr_group
;
398 ts
->attr_group
= &ads7845_attr_group
;
401 ts
->attr_group
= &ads7843_attr_group
;
404 dev_dbg(&spi
->dev
, "ADS%d not recognized\n", ts
->model
);
408 err
= sysfs_create_group(&spi
->dev
.kobj
, ts
->attr_group
);
412 hwmon
= hwmon_device_register(&spi
->dev
);
414 sysfs_remove_group(&spi
->dev
.kobj
, ts
->attr_group
);
415 return PTR_ERR(hwmon
);
422 static void ads784x_hwmon_unregister(struct spi_device
*spi
,
426 sysfs_remove_group(&spi
->dev
.kobj
, ts
->attr_group
);
427 hwmon_device_unregister(ts
->hwmon
);
432 static inline int ads784x_hwmon_register(struct spi_device
*spi
,
438 static inline void ads784x_hwmon_unregister(struct spi_device
*spi
,
444 static int is_pen_down(struct device
*dev
)
446 struct ads7846
*ts
= dev_get_drvdata(dev
);
451 static ssize_t
ads7846_pen_down_show(struct device
*dev
,
452 struct device_attribute
*attr
, char *buf
)
454 return sprintf(buf
, "%u\n", is_pen_down(dev
));
457 static DEVICE_ATTR(pen_down
, S_IRUGO
, ads7846_pen_down_show
, NULL
);
459 static ssize_t
ads7846_disable_show(struct device
*dev
,
460 struct device_attribute
*attr
, char *buf
)
462 struct ads7846
*ts
= dev_get_drvdata(dev
);
464 return sprintf(buf
, "%u\n", ts
->disabled
);
467 static ssize_t
ads7846_disable_store(struct device
*dev
,
468 struct device_attribute
*attr
,
469 const char *buf
, size_t count
)
471 struct ads7846
*ts
= dev_get_drvdata(dev
);
475 i
= simple_strtoul(buf
, &endp
, 10);
476 spin_lock_irq(&ts
->lock
);
483 spin_unlock_irq(&ts
->lock
);
488 static DEVICE_ATTR(disable
, 0664, ads7846_disable_show
, ads7846_disable_store
);
490 static struct attribute
*ads784x_attributes
[] = {
491 &dev_attr_pen_down
.attr
,
492 &dev_attr_disable
.attr
,
496 static struct attribute_group ads784x_attr_group
= {
497 .attrs
= ads784x_attributes
,
500 /*--------------------------------------------------------------------------*/
503 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
504 * to retrieve touchscreen status.
506 * The SPI transfer completion callback does the real work. It reports
507 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
510 static void ads7846_rx(void *ads
)
512 struct ads7846
*ts
= ads
;
516 /* ads7846_rx_val() did in-place conversion (including byteswap) from
517 * on-the-wire format as part of debouncing to get stable readings.
524 /* range filtering */
528 if (likely(x
&& z1
)) {
529 /* compute touch pressure resistance using equation #2 */
533 Rt
*= ts
->x_plate_ohms
;
535 Rt
= (Rt
+ 2047) >> 12;
539 if (ts
->model
== 7843)
540 Rt
= ts
->pressure_max
/ 2;
542 /* Sample found inconsistent by debouncing or pressure is beyond
543 * the maximum. Don't report it to user space, repeat at least
544 * once more the measurement
546 if (ts
->tc
.ignore
|| Rt
> ts
->pressure_max
) {
548 pr_debug("%s: ignored %d pressure %d\n",
549 ts
->spi
->dev
.bus_id
, ts
->tc
.ignore
, Rt
);
551 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_PERIOD
),
556 /* NOTE: We can't rely on the pressure to determine the pen down
557 * state, even this controller has a pressure sensor. The pressure
558 * value can fluctuate for quite a while after lifting the pen and
559 * in some cases may not even settle at the expected value.
561 * The only safe way to check for the pen up condition is in the
562 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
565 struct input_dev
*input
= ts
->input
;
568 input_report_key(input
, BTN_TOUCH
, 1);
571 dev_dbg(&ts
->spi
->dev
, "DOWN\n");
574 input_report_abs(input
, ABS_X
, x
);
575 input_report_abs(input
, ABS_Y
, y
);
576 input_report_abs(input
, ABS_PRESSURE
, Rt
);
580 dev_dbg(&ts
->spi
->dev
, "%4d/%4d/%4d\n", x
, y
, Rt
);
584 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_PERIOD
),
588 static int ads7846_debounce(void *ads
, int data_idx
, int *val
)
590 struct ads7846
*ts
= ads
;
592 if (!ts
->read_cnt
|| (abs(ts
->last_read
- *val
) > ts
->debounce_tol
)) {
593 /* Start over collecting consistent readings. */
595 /* Repeat it, if this was the first read or the read
596 * wasn't consistent enough. */
597 if (ts
->read_cnt
< ts
->debounce_max
) {
598 ts
->last_read
= *val
;
600 return ADS7846_FILTER_REPEAT
;
602 /* Maximum number of debouncing reached and still
603 * not enough number of consistent readings. Abort
604 * the whole sample, repeat it in the next sampling
608 return ADS7846_FILTER_IGNORE
;
611 if (++ts
->read_rep
> ts
->debounce_rep
) {
612 /* Got a good reading for this coordinate,
613 * go for the next one. */
616 return ADS7846_FILTER_OK
;
618 /* Read more values that are consistent. */
620 return ADS7846_FILTER_REPEAT
;
625 static int ads7846_no_filter(void *ads
, int data_idx
, int *val
)
627 return ADS7846_FILTER_OK
;
630 static void ads7846_rx_val(void *ads
)
632 struct ads7846
*ts
= ads
;
633 struct spi_message
*m
;
634 struct spi_transfer
*t
;
640 m
= &ts
->msg
[ts
->msg_idx
];
641 t
= list_entry(m
->transfers
.prev
, struct spi_transfer
, transfer_list
);
644 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
645 * built from two 8 bit values written msb-first.
647 val
= be16_to_cpu(*rx_val
) >> 3;
649 action
= ts
->filter(ts
->filter_data
, ts
->msg_idx
, &val
);
651 case ADS7846_FILTER_REPEAT
:
653 case ADS7846_FILTER_IGNORE
:
655 /* Last message will contain ads7846_rx() as the
656 * completion function.
660 case ADS7846_FILTER_OK
:
663 m
= &ts
->msg
[++ts
->msg_idx
];
668 status
= spi_async(ts
->spi
, m
);
670 dev_err(&ts
->spi
->dev
, "spi_async --> %d\n",
674 static enum hrtimer_restart
ads7846_timer(struct hrtimer
*handle
)
676 struct ads7846
*ts
= container_of(handle
, struct ads7846
, timer
);
679 spin_lock_irq(&ts
->lock
);
681 if (unlikely(!ts
->get_pendown_state() ||
682 device_suspended(&ts
->spi
->dev
))) {
684 struct input_dev
*input
= ts
->input
;
686 input_report_key(input
, BTN_TOUCH
, 0);
687 input_report_abs(input
, ABS_PRESSURE
, 0);
692 dev_dbg(&ts
->spi
->dev
, "UP\n");
696 /* measurement cycle ended */
697 if (!device_suspended(&ts
->spi
->dev
)) {
698 ts
->irq_disabled
= 0;
699 enable_irq(ts
->spi
->irq
);
703 /* pen is still down, continue with the measurement */
705 status
= spi_async(ts
->spi
, &ts
->msg
[0]);
707 dev_err(&ts
->spi
->dev
, "spi_async --> %d\n", status
);
710 spin_unlock_irq(&ts
->lock
);
711 return HRTIMER_NORESTART
;
714 static irqreturn_t
ads7846_irq(int irq
, void *handle
)
716 struct ads7846
*ts
= handle
;
719 spin_lock_irqsave(&ts
->lock
, flags
);
720 if (likely(ts
->get_pendown_state())) {
721 if (!ts
->irq_disabled
) {
722 /* The ARM do_simple_IRQ() dispatcher doesn't act
723 * like the other dispatchers: it will report IRQs
724 * even after they've been disabled. We work around
725 * that here. (The "generic irq" framework may help...)
727 ts
->irq_disabled
= 1;
728 disable_irq(ts
->spi
->irq
);
730 hrtimer_start(&ts
->timer
, ktime_set(0, TS_POLL_DELAY
),
734 spin_unlock_irqrestore(&ts
->lock
, flags
);
739 /*--------------------------------------------------------------------------*/
741 /* Must be called with ts->lock held */
742 static void ads7846_disable(struct ads7846
*ts
)
749 /* are we waiting for IRQ, or polling? */
751 ts
->irq_disabled
= 1;
752 disable_irq(ts
->spi
->irq
);
754 /* the timer will run at least once more, and
755 * leave everything in a clean state, IRQ disabled
757 while (ts
->pending
) {
758 spin_unlock_irq(&ts
->lock
);
760 spin_lock_irq(&ts
->lock
);
764 /* we know the chip's in lowpower mode since we always
765 * leave it that way after every request
770 /* Must be called with ts->lock held */
771 static void ads7846_enable(struct ads7846
*ts
)
777 ts
->irq_disabled
= 0;
778 enable_irq(ts
->spi
->irq
);
781 static int ads7846_suspend(struct spi_device
*spi
, pm_message_t message
)
783 struct ads7846
*ts
= dev_get_drvdata(&spi
->dev
);
785 spin_lock_irq(&ts
->lock
);
787 spi
->dev
.power
.power_state
= message
;
790 spin_unlock_irq(&ts
->lock
);
796 static int ads7846_resume(struct spi_device
*spi
)
798 struct ads7846
*ts
= dev_get_drvdata(&spi
->dev
);
800 spin_lock_irq(&ts
->lock
);
802 spi
->dev
.power
.power_state
= PMSG_ON
;
805 spin_unlock_irq(&ts
->lock
);
810 static int __devinit
ads7846_probe(struct spi_device
*spi
)
813 struct input_dev
*input_dev
;
814 struct ads7846_platform_data
*pdata
= spi
->dev
.platform_data
;
815 struct spi_message
*m
;
816 struct spi_transfer
*x
;
821 dev_dbg(&spi
->dev
, "no IRQ?\n");
826 dev_dbg(&spi
->dev
, "no platform data?\n");
830 /* don't exceed max specified sample rate */
831 if (spi
->max_speed_hz
> (125000 * SAMPLE_BITS
)) {
832 dev_dbg(&spi
->dev
, "f(sample) %d KHz?\n",
833 (spi
->max_speed_hz
/SAMPLE_BITS
)/1000);
837 /* REVISIT when the irq can be triggered active-low, or if for some
838 * reason the touchscreen isn't hooked up, we don't need to access
841 if (pdata
->get_pendown_state
== NULL
) {
842 dev_dbg(&spi
->dev
, "no get_pendown_state function?\n");
846 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
847 * that even if the hardware can do that, the SPI controller driver
848 * may not. So we stick to very-portable 8 bit words, both RX and TX.
850 spi
->bits_per_word
= 8;
851 spi
->mode
= SPI_MODE_0
;
852 err
= spi_setup(spi
);
856 ts
= kzalloc(sizeof(struct ads7846
), GFP_KERNEL
);
857 input_dev
= input_allocate_device();
858 if (!ts
|| !input_dev
) {
863 dev_set_drvdata(&spi
->dev
, ts
);
864 spi
->dev
.power
.power_state
= PMSG_ON
;
867 ts
->input
= input_dev
;
869 hrtimer_init(&ts
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
870 ts
->timer
.function
= ads7846_timer
;
872 spin_lock_init(&ts
->lock
);
874 ts
->model
= pdata
->model
? : 7846;
875 ts
->vref_delay_usecs
= pdata
->vref_delay_usecs
? : 100;
876 ts
->x_plate_ohms
= pdata
->x_plate_ohms
? : 400;
877 ts
->pressure_max
= pdata
->pressure_max
? : ~0;
879 if (pdata
->filter
!= NULL
) {
880 if (pdata
->filter_init
!= NULL
) {
881 err
= pdata
->filter_init(pdata
, &ts
->filter_data
);
885 ts
->filter
= pdata
->filter
;
886 ts
->filter_cleanup
= pdata
->filter_cleanup
;
887 } else if (pdata
->debounce_max
) {
888 ts
->debounce_max
= pdata
->debounce_max
;
889 if (ts
->debounce_max
< 2)
890 ts
->debounce_max
= 2;
891 ts
->debounce_tol
= pdata
->debounce_tol
;
892 ts
->debounce_rep
= pdata
->debounce_rep
;
893 ts
->filter
= ads7846_debounce
;
894 ts
->filter_data
= ts
;
896 ts
->filter
= ads7846_no_filter
;
897 ts
->get_pendown_state
= pdata
->get_pendown_state
;
899 snprintf(ts
->phys
, sizeof(ts
->phys
), "%s/input0", spi
->dev
.bus_id
);
901 input_dev
->name
= "ADS784x Touchscreen";
902 input_dev
->phys
= ts
->phys
;
903 input_dev
->dev
.parent
= &spi
->dev
;
905 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
906 input_dev
->keybit
[BIT_WORD(BTN_TOUCH
)] = BIT_MASK(BTN_TOUCH
);
907 input_set_abs_params(input_dev
, ABS_X
,
909 pdata
->x_max
? : MAX_12BIT
,
911 input_set_abs_params(input_dev
, ABS_Y
,
913 pdata
->y_max
? : MAX_12BIT
,
915 input_set_abs_params(input_dev
, ABS_PRESSURE
,
916 pdata
->pressure_min
, pdata
->pressure_max
, 0, 0);
918 vref
= pdata
->keep_vref_on
;
920 /* set up the transfers to read touchscreen state; this assumes we
921 * use formula #2 for pressure, not #3.
928 /* y- still on; turn on only y+ (and ADC) */
929 ts
->read_y
= READ_Y(vref
);
930 x
->tx_buf
= &ts
->read_y
;
932 spi_message_add_tail(x
, m
);
935 x
->rx_buf
= &ts
->tc
.y
;
937 spi_message_add_tail(x
, m
);
939 m
->complete
= ads7846_rx_val
;
945 /* turn y- off, x+ on, then leave in lowpower */
947 ts
->read_x
= READ_X(vref
);
948 x
->tx_buf
= &ts
->read_x
;
950 spi_message_add_tail(x
, m
);
953 x
->rx_buf
= &ts
->tc
.x
;
955 spi_message_add_tail(x
, m
);
957 m
->complete
= ads7846_rx_val
;
960 /* turn y+ off, x- on; we'll use formula #2 */
961 if (ts
->model
== 7846) {
966 ts
->read_z1
= READ_Z1(vref
);
967 x
->tx_buf
= &ts
->read_z1
;
969 spi_message_add_tail(x
, m
);
972 x
->rx_buf
= &ts
->tc
.z1
;
974 spi_message_add_tail(x
, m
);
976 m
->complete
= ads7846_rx_val
;
983 ts
->read_z2
= READ_Z2(vref
);
984 x
->tx_buf
= &ts
->read_z2
;
986 spi_message_add_tail(x
, m
);
989 x
->rx_buf
= &ts
->tc
.z2
;
991 spi_message_add_tail(x
, m
);
993 m
->complete
= ads7846_rx_val
;
1002 ts
->pwrdown
= PWRDOWN
;
1003 x
->tx_buf
= &ts
->pwrdown
;
1005 spi_message_add_tail(x
, m
);
1008 x
->rx_buf
= &ts
->dummy
;
1011 spi_message_add_tail(x
, m
);
1013 m
->complete
= ads7846_rx
;
1018 if (request_irq(spi
->irq
, ads7846_irq
, IRQF_TRIGGER_FALLING
,
1019 spi
->dev
.driver
->name
, ts
)) {
1020 dev_dbg(&spi
->dev
, "irq %d busy?\n", spi
->irq
);
1022 goto err_cleanup_filter
;
1025 err
= ads784x_hwmon_register(spi
, ts
);
1029 dev_info(&spi
->dev
, "touchscreen, irq %d\n", spi
->irq
);
1031 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1032 * the touchscreen, in case it's not connected.
1034 (void) ads7846_read12_ser(&spi
->dev
,
1035 READ_12BIT_SER(vaux
) | ADS_PD10_ALL_ON
);
1037 err
= sysfs_create_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1039 goto err_remove_hwmon
;
1041 err
= input_register_device(input_dev
);
1043 goto err_remove_attr_group
;
1047 err_remove_attr_group
:
1048 sysfs_remove_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1050 ads784x_hwmon_unregister(spi
, ts
);
1052 free_irq(spi
->irq
, ts
);
1054 if (ts
->filter_cleanup
)
1055 ts
->filter_cleanup(ts
->filter_data
);
1057 input_free_device(input_dev
);
1062 static int __devexit
ads7846_remove(struct spi_device
*spi
)
1064 struct ads7846
*ts
= dev_get_drvdata(&spi
->dev
);
1066 ads784x_hwmon_unregister(spi
, ts
);
1067 input_unregister_device(ts
->input
);
1069 ads7846_suspend(spi
, PMSG_SUSPEND
);
1071 sysfs_remove_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1073 free_irq(ts
->spi
->irq
, ts
);
1074 /* suspend left the IRQ disabled */
1075 enable_irq(ts
->spi
->irq
);
1077 if (ts
->filter_cleanup
)
1078 ts
->filter_cleanup(ts
->filter_data
);
1082 dev_dbg(&spi
->dev
, "unregistered touchscreen\n");
1086 static struct spi_driver ads7846_driver
= {
1089 .bus
= &spi_bus_type
,
1090 .owner
= THIS_MODULE
,
1092 .probe
= ads7846_probe
,
1093 .remove
= __devexit_p(ads7846_remove
),
1094 .suspend
= ads7846_suspend
,
1095 .resume
= ads7846_resume
,
1098 static int __init
ads7846_init(void)
1100 /* grr, board-specific init should stay out of drivers!! */
1102 #ifdef CONFIG_ARCH_OMAP
1103 if (machine_is_omap_osk()) {
1104 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
1105 omap_request_gpio(4);
1106 omap_set_gpio_direction(4, 1);
1107 omap_request_gpio(6);
1108 omap_set_gpio_direction(6, 1);
1110 // also TI 1510 Innovator, bitbanging through FPGA
1112 // also Palm Tungsten T2
1116 // also Dell Axim X50
1117 // also HP iPaq H191x/H192x/H415x/H435x
1118 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
1119 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
1121 // Atmel at91sam9261-EK uses ads7843
1123 // also various AMD Au1x00 devel boards
1125 return spi_register_driver(&ads7846_driver
);
1127 module_init(ads7846_init
);
1129 static void __exit
ads7846_exit(void)
1131 spi_unregister_driver(&ads7846_driver
);
1133 #ifdef CONFIG_ARCH_OMAP
1134 if (machine_is_omap_osk()) {
1141 module_exit(ads7846_exit
);
1143 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1144 MODULE_LICENSE("GPL");