1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADS7846 based touchscreen and sensor driver
5 * Copyright (c) 2005 David Brownell
6 * Copyright (c) 2006 Nokia Corporation
7 * Various changes: Imre Deak <imre.deak@nokia.com>
11 * Copyright (C) 2004-2005 Richard Purdie
12 * - omap_ts.[hc], ads7846.h, ts_osk.c
13 * Copyright (C) 2002 MontaVista Software
14 * Copyright (C) 2004 Texas Instruments
15 * Copyright (C) 2005 Dirk Behme
17 #include <linux/types.h>
18 #include <linux/hwmon.h>
19 #include <linux/err.h>
20 #include <linux/sched.h>
21 #include <linux/delay.h>
22 #include <linux/input.h>
23 #include <linux/input/touchscreen.h>
24 #include <linux/interrupt.h>
25 #include <linux/slab.h>
28 #include <linux/of_gpio.h>
29 #include <linux/of_device.h>
30 #include <linux/gpio.h>
31 #include <linux/spi/spi.h>
32 #include <linux/spi/ads7846.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/module.h>
35 #include <asm/unaligned.h>
38 * This code has been heavily tested on a Nokia 770, and lightly
39 * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz).
40 * TSC2046 is just newer ads7846 silicon.
41 * Support for ads7843 tested on Atmel at91sam926x-EK.
42 * Support for ads7845 has only been stubbed in.
43 * Support for Analog Devices AD7873 and AD7843 tested.
45 * IRQ handling needs a workaround because of a shortcoming in handling
46 * edge triggered IRQs on some platforms like the OMAP1/2. These
47 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
48 * have to maintain our own SW IRQ disabled status. This should be
49 * removed as soon as the affected platform's IRQ handling is fixed.
51 * App note sbaa036 talks in more detail about accurate sampling...
52 * that ought to help in situations like LCDs inducing noise (which
53 * can also be helped by using synch signals) and more generally.
54 * This driver tries to utilize the measures described in the app
55 * note. The strength of filtering can be set in the board-* specific
59 #define TS_POLL_DELAY 1 /* ms delay before the first sample */
60 #define TS_POLL_PERIOD 5 /* ms delay between samples */
62 /* this driver doesn't aim at the peak continuous sample rate */
63 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
68 * This union is a temporary hack. The driver does an in-place
69 * endianness conversion. This will be cleaned up in the next
83 struct ads7846_buf z1
;
84 struct ads7846_buf z2
;
88 * We allocate this separately to avoid cache line sharing issues when
89 * driver is used with DMA-based SPI controllers (like atmel_spi) on
90 * systems where main memory is not DMA-coherent (most non-x86 boards).
92 struct ads7846_packet
{
94 struct ads7846_buf read_x_cmd
;
95 struct ads7846_buf read_y_cmd
;
96 struct ads7846_buf read_z1_cmd
;
97 struct ads7846_buf read_z2_cmd
;
98 struct ads7846_buf pwrdown_cmd
;
102 struct input_dev
*input
;
106 struct spi_device
*spi
;
107 struct regulator
*reg
;
109 #if IS_ENABLED(CONFIG_HWMON)
110 struct device
*hwmon
;
115 u16 vref_delay_usecs
;
122 struct ads7846_packet
*packet
;
124 struct spi_transfer xfer
[18];
125 struct spi_message msg
[5];
127 wait_queue_head_t wait
;
139 u16 penirq_recheck_delay_usecs
;
141 struct touchscreen_properties core_prop
;
144 bool stopped
; /* P: lock */
145 bool disabled
; /* P: lock */
146 bool suspended
; /* P: lock */
148 int (*filter
)(void *data
, int data_idx
, int *val
);
150 void (*filter_cleanup
)(void *data
);
151 int (*get_pendown_state
)(void);
154 void (*wait_for_sync
)(void);
157 /* leave chip selected when we're done, for quicker re-select? */
159 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
161 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
164 /*--------------------------------------------------------------------------*/
166 /* The ADS7846 has touchscreen and other sensors.
167 * Earlier ads784x chips are somewhat compatible.
169 #define ADS_START (1 << 7)
170 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
171 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
172 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
173 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
174 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
175 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
176 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
177 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
178 #define ADS_8_BIT (1 << 3)
179 #define ADS_12_BIT (0 << 3)
180 #define ADS_SER (1 << 2) /* non-differential */
181 #define ADS_DFR (0 << 2) /* differential */
182 #define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */
183 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
184 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
185 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
187 #define MAX_12BIT ((1<<12)-1)
189 /* leave ADC powered up (disables penirq) between differential samples */
190 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
191 | ADS_12_BIT | ADS_DFR | \
192 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
194 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
195 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
196 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
198 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
199 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
201 /* single-ended samples need to first power up reference voltage;
202 * we leave both ADC and VREF powered
204 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
205 | ADS_12_BIT | ADS_SER)
207 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
208 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
210 static int get_pendown_state(struct ads7846
*ts
)
212 if (ts
->get_pendown_state
)
213 return ts
->get_pendown_state();
215 return !gpio_get_value(ts
->gpio_pendown
);
218 static void ads7846_report_pen_up(struct ads7846
*ts
)
220 struct input_dev
*input
= ts
->input
;
222 input_report_key(input
, BTN_TOUCH
, 0);
223 input_report_abs(input
, ABS_PRESSURE
, 0);
227 dev_vdbg(&ts
->spi
->dev
, "UP\n");
230 /* Must be called with ts->lock held */
231 static void ads7846_stop(struct ads7846
*ts
)
233 if (!ts
->disabled
&& !ts
->suspended
) {
234 /* Signal IRQ thread to stop polling and disable the handler. */
238 disable_irq(ts
->spi
->irq
);
242 /* Must be called with ts->lock held */
243 static void ads7846_restart(struct ads7846
*ts
)
245 if (!ts
->disabled
&& !ts
->suspended
) {
246 /* Check if pen was released since last stop */
247 if (ts
->pendown
&& !get_pendown_state(ts
))
248 ads7846_report_pen_up(ts
);
250 /* Tell IRQ thread that it may poll the device. */
253 enable_irq(ts
->spi
->irq
);
257 /* Must be called with ts->lock held */
258 static void __ads7846_disable(struct ads7846
*ts
)
261 regulator_disable(ts
->reg
);
264 * We know the chip's in low power mode since we always
265 * leave it that way after every request
269 /* Must be called with ts->lock held */
270 static void __ads7846_enable(struct ads7846
*ts
)
274 error
= regulator_enable(ts
->reg
);
276 dev_err(&ts
->spi
->dev
, "Failed to enable supply: %d\n", error
);
281 static void ads7846_disable(struct ads7846
*ts
)
283 mutex_lock(&ts
->lock
);
288 __ads7846_disable(ts
);
293 mutex_unlock(&ts
->lock
);
296 static void ads7846_enable(struct ads7846
*ts
)
298 mutex_lock(&ts
->lock
);
302 ts
->disabled
= false;
305 __ads7846_enable(ts
);
308 mutex_unlock(&ts
->lock
);
311 /*--------------------------------------------------------------------------*/
314 * Non-touchscreen sensors only use single-ended conversions.
315 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
316 * ads7846 lets that pin be unconnected, to use internal vREF.
324 struct spi_message msg
;
325 struct spi_transfer xfer
[6];
327 * DMA (thus cache coherency maintenance) requires the
328 * transfer buffers to live in their own cache lines.
330 __be16 sample ____cacheline_aligned
;
333 struct ads7845_ser_req
{
335 struct spi_message msg
;
336 struct spi_transfer xfer
[2];
338 * DMA (thus cache coherency maintenance) requires the
339 * transfer buffers to live in their own cache lines.
341 u8 sample
[3] ____cacheline_aligned
;
344 static int ads7846_read12_ser(struct device
*dev
, unsigned command
)
346 struct spi_device
*spi
= to_spi_device(dev
);
347 struct ads7846
*ts
= dev_get_drvdata(dev
);
351 req
= kzalloc(sizeof *req
, GFP_KERNEL
);
355 spi_message_init(&req
->msg
);
357 /* maybe turn on internal vREF, and let it settle */
358 if (ts
->use_internal
) {
359 req
->ref_on
= REF_ON
;
360 req
->xfer
[0].tx_buf
= &req
->ref_on
;
361 req
->xfer
[0].len
= 1;
362 spi_message_add_tail(&req
->xfer
[0], &req
->msg
);
364 req
->xfer
[1].rx_buf
= &req
->scratch
;
365 req
->xfer
[1].len
= 2;
367 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
368 req
->xfer
[1].delay
.value
= ts
->vref_delay_usecs
;
369 req
->xfer
[1].delay
.unit
= SPI_DELAY_UNIT_USECS
;
370 spi_message_add_tail(&req
->xfer
[1], &req
->msg
);
372 /* Enable reference voltage */
373 command
|= ADS_PD10_REF_ON
;
376 /* Enable ADC in every case */
377 command
|= ADS_PD10_ADC_ON
;
380 req
->command
= (u8
) command
;
381 req
->xfer
[2].tx_buf
= &req
->command
;
382 req
->xfer
[2].len
= 1;
383 spi_message_add_tail(&req
->xfer
[2], &req
->msg
);
385 req
->xfer
[3].rx_buf
= &req
->sample
;
386 req
->xfer
[3].len
= 2;
387 spi_message_add_tail(&req
->xfer
[3], &req
->msg
);
389 /* REVISIT: take a few more samples, and compare ... */
391 /* converter in low power mode & enable PENIRQ */
392 req
->ref_off
= PWRDOWN
;
393 req
->xfer
[4].tx_buf
= &req
->ref_off
;
394 req
->xfer
[4].len
= 1;
395 spi_message_add_tail(&req
->xfer
[4], &req
->msg
);
397 req
->xfer
[5].rx_buf
= &req
->scratch
;
398 req
->xfer
[5].len
= 2;
399 CS_CHANGE(req
->xfer
[5]);
400 spi_message_add_tail(&req
->xfer
[5], &req
->msg
);
402 mutex_lock(&ts
->lock
);
404 status
= spi_sync(spi
, &req
->msg
);
406 mutex_unlock(&ts
->lock
);
409 /* on-wire is a must-ignore bit, a BE12 value, then padding */
410 status
= be16_to_cpu(req
->sample
);
411 status
= status
>> 3;
419 static int ads7845_read12_ser(struct device
*dev
, unsigned command
)
421 struct spi_device
*spi
= to_spi_device(dev
);
422 struct ads7846
*ts
= dev_get_drvdata(dev
);
423 struct ads7845_ser_req
*req
;
426 req
= kzalloc(sizeof *req
, GFP_KERNEL
);
430 spi_message_init(&req
->msg
);
432 req
->command
[0] = (u8
) command
;
433 req
->xfer
[0].tx_buf
= req
->command
;
434 req
->xfer
[0].rx_buf
= req
->sample
;
435 req
->xfer
[0].len
= 3;
436 spi_message_add_tail(&req
->xfer
[0], &req
->msg
);
438 mutex_lock(&ts
->lock
);
440 status
= spi_sync(spi
, &req
->msg
);
442 mutex_unlock(&ts
->lock
);
445 /* BE12 value, then padding */
446 status
= get_unaligned_be16(&req
->sample
[1]);
447 status
= status
>> 3;
455 #if IS_ENABLED(CONFIG_HWMON)
457 #define SHOW(name, var, adjust) static ssize_t \
458 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
460 struct ads7846 *ts = dev_get_drvdata(dev); \
461 ssize_t v = ads7846_read12_ser(&ts->spi->dev, \
462 READ_12BIT_SER(var)); \
465 return sprintf(buf, "%u\n", adjust(ts, v)); \
467 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
470 /* Sysfs conventions report temperatures in millidegrees Celsius.
471 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
472 * accuracy scheme without calibration data. For now we won't try either;
473 * userspace sees raw sensor values, and must scale/calibrate appropriately.
475 static inline unsigned null_adjust(struct ads7846
*ts
, ssize_t v
)
480 SHOW(temp0
, temp0
, null_adjust
) /* temp1_input */
481 SHOW(temp1
, temp1
, null_adjust
) /* temp2_input */
484 /* sysfs conventions report voltages in millivolts. We can convert voltages
485 * if we know vREF. userspace may need to scale vAUX to match the board's
486 * external resistors; we assume that vBATT only uses the internal ones.
488 static inline unsigned vaux_adjust(struct ads7846
*ts
, ssize_t v
)
492 /* external resistors may scale vAUX into 0..vREF */
493 retval
*= ts
->vref_mv
;
494 retval
= retval
>> 12;
499 static inline unsigned vbatt_adjust(struct ads7846
*ts
, ssize_t v
)
501 unsigned retval
= vaux_adjust(ts
, v
);
503 /* ads7846 has a resistor ladder to scale this signal down */
504 if (ts
->model
== 7846)
510 SHOW(in0_input
, vaux
, vaux_adjust
)
511 SHOW(in1_input
, vbatt
, vbatt_adjust
)
513 static umode_t
ads7846_is_visible(struct kobject
*kobj
, struct attribute
*attr
,
516 struct device
*dev
= kobj_to_dev(kobj
);
517 struct ads7846
*ts
= dev_get_drvdata(dev
);
519 if (ts
->model
== 7843 && index
< 2) /* in0, in1 */
521 if (ts
->model
== 7845 && index
!= 2) /* in0 */
527 static struct attribute
*ads7846_attributes
[] = {
528 &dev_attr_temp0
.attr
, /* 0 */
529 &dev_attr_temp1
.attr
, /* 1 */
530 &dev_attr_in0_input
.attr
, /* 2 */
531 &dev_attr_in1_input
.attr
, /* 3 */
535 static const struct attribute_group ads7846_attr_group
= {
536 .attrs
= ads7846_attributes
,
537 .is_visible
= ads7846_is_visible
,
539 __ATTRIBUTE_GROUPS(ads7846_attr
);
541 static int ads784x_hwmon_register(struct spi_device
*spi
, struct ads7846
*ts
)
543 /* hwmon sensors need a reference voltage */
547 dev_dbg(&spi
->dev
, "assuming 2.5V internal vREF\n");
549 ts
->use_internal
= true;
556 "external vREF for ADS%d not specified\n",
563 ts
->hwmon
= hwmon_device_register_with_groups(&spi
->dev
, spi
->modalias
,
564 ts
, ads7846_attr_groups
);
566 return PTR_ERR_OR_ZERO(ts
->hwmon
);
569 static void ads784x_hwmon_unregister(struct spi_device
*spi
,
573 hwmon_device_unregister(ts
->hwmon
);
577 static inline int ads784x_hwmon_register(struct spi_device
*spi
,
583 static inline void ads784x_hwmon_unregister(struct spi_device
*spi
,
589 static ssize_t
ads7846_pen_down_show(struct device
*dev
,
590 struct device_attribute
*attr
, char *buf
)
592 struct ads7846
*ts
= dev_get_drvdata(dev
);
594 return sprintf(buf
, "%u\n", ts
->pendown
);
597 static DEVICE_ATTR(pen_down
, S_IRUGO
, ads7846_pen_down_show
, NULL
);
599 static ssize_t
ads7846_disable_show(struct device
*dev
,
600 struct device_attribute
*attr
, char *buf
)
602 struct ads7846
*ts
= dev_get_drvdata(dev
);
604 return sprintf(buf
, "%u\n", ts
->disabled
);
607 static ssize_t
ads7846_disable_store(struct device
*dev
,
608 struct device_attribute
*attr
,
609 const char *buf
, size_t count
)
611 struct ads7846
*ts
= dev_get_drvdata(dev
);
615 err
= kstrtouint(buf
, 10, &i
);
627 static DEVICE_ATTR(disable
, 0664, ads7846_disable_show
, ads7846_disable_store
);
629 static struct attribute
*ads784x_attributes
[] = {
630 &dev_attr_pen_down
.attr
,
631 &dev_attr_disable
.attr
,
635 static const struct attribute_group ads784x_attr_group
= {
636 .attrs
= ads784x_attributes
,
639 /*--------------------------------------------------------------------------*/
641 static void null_wait_for_sync(void)
645 static int ads7846_debounce_filter(void *ads
, int data_idx
, int *val
)
647 struct ads7846
*ts
= ads
;
649 if (!ts
->read_cnt
|| (abs(ts
->last_read
- *val
) > ts
->debounce_tol
)) {
650 /* Start over collecting consistent readings. */
653 * Repeat it, if this was the first read or the read
654 * wasn't consistent enough.
656 if (ts
->read_cnt
< ts
->debounce_max
) {
657 ts
->last_read
= *val
;
659 return ADS7846_FILTER_REPEAT
;
662 * Maximum number of debouncing reached and still
663 * not enough number of consistent readings. Abort
664 * the whole sample, repeat it in the next sampling
668 return ADS7846_FILTER_IGNORE
;
671 if (++ts
->read_rep
> ts
->debounce_rep
) {
673 * Got a good reading for this coordinate,
674 * go for the next one.
678 return ADS7846_FILTER_OK
;
680 /* Read more values that are consistent. */
682 return ADS7846_FILTER_REPEAT
;
687 static int ads7846_no_filter(void *ads
, int data_idx
, int *val
)
689 return ADS7846_FILTER_OK
;
692 static int ads7846_get_value(struct ads7846
*ts
, struct spi_message
*m
)
695 struct spi_transfer
*t
=
696 list_entry(m
->transfers
.prev
, struct spi_transfer
, transfer_list
);
697 struct ads7846_buf
*buf
= t
->rx_buf
;
699 value
= be16_to_cpup(&buf
->data_be16
);
701 /* enforce ADC output is 12 bits width */
702 return (value
>> 3) & 0xfff;
705 static void ads7846_update_value(struct spi_message
*m
, int val
)
707 struct spi_transfer
*t
=
708 list_entry(m
->transfers
.prev
, struct spi_transfer
, transfer_list
);
709 struct ads7846_buf
*buf
= t
->rx_buf
;
714 static void ads7846_read_state(struct ads7846
*ts
)
716 struct ads7846_packet
*packet
= ts
->packet
;
717 struct spi_message
*m
;
723 while (msg_idx
< ts
->msg_count
) {
727 m
= &ts
->msg
[msg_idx
];
728 error
= spi_sync(ts
->spi
, m
);
730 dev_err(&ts
->spi
->dev
, "spi_sync --> %d\n", error
);
731 packet
->tc
.ignore
= true;
736 * Last message is power down request, no need to convert
737 * or filter the value.
739 if (msg_idx
< ts
->msg_count
- 1) {
741 val
= ads7846_get_value(ts
, m
);
743 action
= ts
->filter(ts
->filter_data
, msg_idx
, &val
);
745 case ADS7846_FILTER_REPEAT
:
748 case ADS7846_FILTER_IGNORE
:
749 packet
->tc
.ignore
= true;
750 msg_idx
= ts
->msg_count
- 1;
753 case ADS7846_FILTER_OK
:
754 ads7846_update_value(m
, val
);
755 packet
->tc
.ignore
= false;
768 static void ads7846_report_state(struct ads7846
*ts
)
770 struct ads7846_packet
*packet
= ts
->packet
;
775 * ads7846_get_value() does in-place conversion (including byte swap)
776 * from on-the-wire format as part of debouncing to get stable
779 x
= packet
->tc
.x
.data
;
780 y
= packet
->tc
.y
.data
;
781 if (ts
->model
== 7845) {
785 z1
= packet
->tc
.z1
.data
;
786 z2
= packet
->tc
.z2
.data
;
789 /* range filtering */
793 if (ts
->model
== 7843) {
794 Rt
= ts
->pressure_max
/ 2;
795 } else if (ts
->model
== 7845) {
796 if (get_pendown_state(ts
))
797 Rt
= ts
->pressure_max
/ 2;
800 dev_vdbg(&ts
->spi
->dev
, "x/y: %d/%d, PD %d\n", x
, y
, Rt
);
801 } else if (likely(x
&& z1
)) {
802 /* compute touch pressure resistance using equation #2 */
805 Rt
*= ts
->x_plate_ohms
;
806 Rt
= DIV_ROUND_CLOSEST(Rt
, 16);
809 Rt
= DIV_ROUND_CLOSEST(Rt
, 256);
815 * Sample found inconsistent by debouncing or pressure is beyond
816 * the maximum. Don't report it to user space, repeat at least
817 * once more the measurement
819 if (packet
->tc
.ignore
|| Rt
> ts
->pressure_max
) {
820 dev_vdbg(&ts
->spi
->dev
, "ignored %d pressure %d\n",
821 packet
->tc
.ignore
, Rt
);
826 * Maybe check the pendown state before reporting. This discards
827 * false readings when the pen is lifted.
829 if (ts
->penirq_recheck_delay_usecs
) {
830 udelay(ts
->penirq_recheck_delay_usecs
);
831 if (!get_pendown_state(ts
))
836 * NOTE: We can't rely on the pressure to determine the pen down
837 * state, even this controller has a pressure sensor. The pressure
838 * value can fluctuate for quite a while after lifting the pen and
839 * in some cases may not even settle at the expected value.
841 * The only safe way to check for the pen up condition is in the
842 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
845 struct input_dev
*input
= ts
->input
;
848 input_report_key(input
, BTN_TOUCH
, 1);
850 dev_vdbg(&ts
->spi
->dev
, "DOWN\n");
853 touchscreen_report_pos(input
, &ts
->core_prop
, x
, y
, false);
854 input_report_abs(input
, ABS_PRESSURE
, ts
->pressure_max
- Rt
);
857 dev_vdbg(&ts
->spi
->dev
, "%4d/%4d/%4d\n", x
, y
, Rt
);
861 static irqreturn_t
ads7846_hard_irq(int irq
, void *handle
)
863 struct ads7846
*ts
= handle
;
865 return get_pendown_state(ts
) ? IRQ_WAKE_THREAD
: IRQ_HANDLED
;
869 static irqreturn_t
ads7846_irq(int irq
, void *handle
)
871 struct ads7846
*ts
= handle
;
873 /* Start with a small delay before checking pendown state */
874 msleep(TS_POLL_DELAY
);
876 while (!ts
->stopped
&& get_pendown_state(ts
)) {
878 /* pen is down, continue with the measurement */
879 ads7846_read_state(ts
);
882 ads7846_report_state(ts
);
884 wait_event_timeout(ts
->wait
, ts
->stopped
,
885 msecs_to_jiffies(TS_POLL_PERIOD
));
888 if (ts
->pendown
&& !ts
->stopped
)
889 ads7846_report_pen_up(ts
);
894 static int __maybe_unused
ads7846_suspend(struct device
*dev
)
896 struct ads7846
*ts
= dev_get_drvdata(dev
);
898 mutex_lock(&ts
->lock
);
900 if (!ts
->suspended
) {
903 __ads7846_disable(ts
);
905 if (device_may_wakeup(&ts
->spi
->dev
))
906 enable_irq_wake(ts
->spi
->irq
);
908 ts
->suspended
= true;
911 mutex_unlock(&ts
->lock
);
916 static int __maybe_unused
ads7846_resume(struct device
*dev
)
918 struct ads7846
*ts
= dev_get_drvdata(dev
);
920 mutex_lock(&ts
->lock
);
924 ts
->suspended
= false;
926 if (device_may_wakeup(&ts
->spi
->dev
))
927 disable_irq_wake(ts
->spi
->irq
);
930 __ads7846_enable(ts
);
933 mutex_unlock(&ts
->lock
);
938 static SIMPLE_DEV_PM_OPS(ads7846_pm
, ads7846_suspend
, ads7846_resume
);
940 static int ads7846_setup_pendown(struct spi_device
*spi
,
942 const struct ads7846_platform_data
*pdata
)
947 * REVISIT when the irq can be triggered active-low, or if for some
948 * reason the touchscreen isn't hooked up, we don't need to access
952 if (pdata
->get_pendown_state
) {
953 ts
->get_pendown_state
= pdata
->get_pendown_state
;
954 } else if (gpio_is_valid(pdata
->gpio_pendown
)) {
956 err
= gpio_request_one(pdata
->gpio_pendown
, GPIOF_IN
,
960 "failed to request/setup pendown GPIO%d: %d\n",
961 pdata
->gpio_pendown
, err
);
965 ts
->gpio_pendown
= pdata
->gpio_pendown
;
967 if (pdata
->gpio_pendown_debounce
)
968 gpio_set_debounce(pdata
->gpio_pendown
,
969 pdata
->gpio_pendown_debounce
);
971 dev_err(&spi
->dev
, "no get_pendown_state nor gpio_pendown?\n");
979 * Set up the transfers to read touchscreen state; this assumes we
980 * use formula #2 for pressure, not #3.
982 static void ads7846_setup_spi_msg(struct ads7846
*ts
,
983 const struct ads7846_platform_data
*pdata
)
985 struct spi_message
*m
= &ts
->msg
[0];
986 struct spi_transfer
*x
= ts
->xfer
;
987 struct ads7846_packet
*packet
= ts
->packet
;
988 int vref
= pdata
->keep_vref_on
;
990 if (ts
->model
== 7873) {
992 * The AD7873 is almost identical to the ADS7846
993 * keep VREF off during differential/ratiometric
1001 spi_message_init(m
);
1004 packet
->read_y_cmd
.cmd
= READ_Y(vref
);
1005 x
->tx_buf
= &packet
->read_y_cmd
;
1006 x
->rx_buf
= &packet
->tc
.y
;
1008 spi_message_add_tail(x
, m
);
1011 * The first sample after switching drivers can be low quality;
1012 * optionally discard it, using a second one after the signals
1013 * have had enough time to stabilize.
1015 if (pdata
->settle_delay_usecs
) {
1016 x
->delay
.value
= pdata
->settle_delay_usecs
;
1017 x
->delay
.unit
= SPI_DELAY_UNIT_USECS
;
1020 x
->tx_buf
= &packet
->read_y_cmd
;
1021 x
->rx_buf
= &packet
->tc
.y
;
1023 spi_message_add_tail(x
, m
);
1028 spi_message_init(m
);
1031 /* turn y- off, x+ on, then leave in lowpower */
1033 packet
->read_x_cmd
.cmd
= READ_X(vref
);
1034 x
->tx_buf
= &packet
->read_x_cmd
;
1035 x
->rx_buf
= &packet
->tc
.x
;
1037 spi_message_add_tail(x
, m
);
1039 /* ... maybe discard first sample ... */
1040 if (pdata
->settle_delay_usecs
) {
1041 x
->delay
.value
= pdata
->settle_delay_usecs
;
1042 x
->delay
.unit
= SPI_DELAY_UNIT_USECS
;
1045 x
->tx_buf
= &packet
->read_x_cmd
;
1046 x
->rx_buf
= &packet
->tc
.x
;
1048 spi_message_add_tail(x
, m
);
1051 /* turn y+ off, x- on; we'll use formula #2 */
1052 if (ts
->model
== 7846) {
1055 spi_message_init(m
);
1059 packet
->read_z1_cmd
.cmd
= READ_Z1(vref
);
1060 x
->tx_buf
= &packet
->read_z1_cmd
;
1061 x
->rx_buf
= &packet
->tc
.z1
;
1063 spi_message_add_tail(x
, m
);
1065 /* ... maybe discard first sample ... */
1066 if (pdata
->settle_delay_usecs
) {
1067 x
->delay
.value
= pdata
->settle_delay_usecs
;
1068 x
->delay
.unit
= SPI_DELAY_UNIT_USECS
;
1071 x
->tx_buf
= &packet
->read_z1_cmd
;
1072 x
->rx_buf
= &packet
->tc
.z1
;
1074 spi_message_add_tail(x
, m
);
1079 spi_message_init(m
);
1083 packet
->read_z2_cmd
.cmd
= READ_Z2(vref
);
1084 x
->tx_buf
= &packet
->read_z2_cmd
;
1085 x
->rx_buf
= &packet
->tc
.z2
;
1087 spi_message_add_tail(x
, m
);
1089 /* ... maybe discard first sample ... */
1090 if (pdata
->settle_delay_usecs
) {
1091 x
->delay
.value
= pdata
->settle_delay_usecs
;
1092 x
->delay
.unit
= SPI_DELAY_UNIT_USECS
;
1095 x
->tx_buf
= &packet
->read_z2_cmd
;
1096 x
->rx_buf
= &packet
->tc
.z2
;
1098 spi_message_add_tail(x
, m
);
1105 spi_message_init(m
);
1109 packet
->pwrdown_cmd
.cmd
= PWRDOWN
;
1110 x
->tx_buf
= &packet
->pwrdown_cmd
;
1114 spi_message_add_tail(x
, m
);
1118 static const struct of_device_id ads7846_dt_ids
[] = {
1119 { .compatible
= "ti,tsc2046", .data
= (void *) 7846 },
1120 { .compatible
= "ti,ads7843", .data
= (void *) 7843 },
1121 { .compatible
= "ti,ads7845", .data
= (void *) 7845 },
1122 { .compatible
= "ti,ads7846", .data
= (void *) 7846 },
1123 { .compatible
= "ti,ads7873", .data
= (void *) 7873 },
1126 MODULE_DEVICE_TABLE(of
, ads7846_dt_ids
);
1128 static const struct ads7846_platform_data
*ads7846_probe_dt(struct device
*dev
)
1130 struct ads7846_platform_data
*pdata
;
1131 struct device_node
*node
= dev
->of_node
;
1132 const struct of_device_id
*match
;
1136 dev_err(dev
, "Device does not have associated DT data\n");
1137 return ERR_PTR(-EINVAL
);
1140 match
= of_match_device(ads7846_dt_ids
, dev
);
1142 dev_err(dev
, "Unknown device model\n");
1143 return ERR_PTR(-EINVAL
);
1146 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
1148 return ERR_PTR(-ENOMEM
);
1150 pdata
->model
= (unsigned long)match
->data
;
1152 of_property_read_u16(node
, "ti,vref-delay-usecs",
1153 &pdata
->vref_delay_usecs
);
1154 of_property_read_u16(node
, "ti,vref-mv", &pdata
->vref_mv
);
1155 pdata
->keep_vref_on
= of_property_read_bool(node
, "ti,keep-vref-on");
1157 pdata
->swap_xy
= of_property_read_bool(node
, "ti,swap-xy");
1159 of_property_read_u16(node
, "ti,settle-delay-usec",
1160 &pdata
->settle_delay_usecs
);
1161 of_property_read_u16(node
, "ti,penirq-recheck-delay-usecs",
1162 &pdata
->penirq_recheck_delay_usecs
);
1164 of_property_read_u16(node
, "ti,x-plate-ohms", &pdata
->x_plate_ohms
);
1165 of_property_read_u16(node
, "ti,y-plate-ohms", &pdata
->y_plate_ohms
);
1167 of_property_read_u16(node
, "ti,x-min", &pdata
->x_min
);
1168 of_property_read_u16(node
, "ti,y-min", &pdata
->y_min
);
1169 of_property_read_u16(node
, "ti,x-max", &pdata
->x_max
);
1170 of_property_read_u16(node
, "ti,y-max", &pdata
->y_max
);
1173 * touchscreen-max-pressure gets parsed during
1174 * touchscreen_parse_properties()
1176 of_property_read_u16(node
, "ti,pressure-min", &pdata
->pressure_min
);
1177 if (!of_property_read_u32(node
, "touchscreen-min-pressure", &value
))
1178 pdata
->pressure_min
= (u16
) value
;
1179 of_property_read_u16(node
, "ti,pressure-max", &pdata
->pressure_max
);
1181 of_property_read_u16(node
, "ti,debounce-max", &pdata
->debounce_max
);
1182 if (!of_property_read_u32(node
, "touchscreen-average-samples", &value
))
1183 pdata
->debounce_max
= (u16
) value
;
1184 of_property_read_u16(node
, "ti,debounce-tol", &pdata
->debounce_tol
);
1185 of_property_read_u16(node
, "ti,debounce-rep", &pdata
->debounce_rep
);
1187 of_property_read_u32(node
, "ti,pendown-gpio-debounce",
1188 &pdata
->gpio_pendown_debounce
);
1190 pdata
->wakeup
= of_property_read_bool(node
, "wakeup-source") ||
1191 of_property_read_bool(node
, "linux,wakeup");
1193 pdata
->gpio_pendown
= of_get_named_gpio(dev
->of_node
, "pendown-gpio", 0);
1198 static const struct ads7846_platform_data
*ads7846_probe_dt(struct device
*dev
)
1200 dev_err(dev
, "no platform data defined\n");
1201 return ERR_PTR(-EINVAL
);
1205 static int ads7846_probe(struct spi_device
*spi
)
1207 const struct ads7846_platform_data
*pdata
;
1209 struct ads7846_packet
*packet
;
1210 struct input_dev
*input_dev
;
1211 unsigned long irq_flags
;
1215 dev_dbg(&spi
->dev
, "no IRQ?\n");
1219 /* don't exceed max specified sample rate */
1220 if (spi
->max_speed_hz
> (125000 * SAMPLE_BITS
)) {
1221 dev_err(&spi
->dev
, "f(sample) %d KHz?\n",
1222 (spi
->max_speed_hz
/SAMPLE_BITS
)/1000);
1227 * We'd set TX word size 8 bits and RX word size to 13 bits ... except
1228 * that even if the hardware can do that, the SPI controller driver
1229 * may not. So we stick to very-portable 8 bit words, both RX and TX.
1231 spi
->bits_per_word
= 8;
1232 spi
->mode
&= ~SPI_MODE_X_MASK
;
1233 spi
->mode
|= SPI_MODE_0
;
1234 err
= spi_setup(spi
);
1238 ts
= kzalloc(sizeof(struct ads7846
), GFP_KERNEL
);
1239 packet
= kzalloc(sizeof(struct ads7846_packet
), GFP_KERNEL
);
1240 input_dev
= input_allocate_device();
1241 if (!ts
|| !packet
|| !input_dev
) {
1246 spi_set_drvdata(spi
, ts
);
1248 ts
->packet
= packet
;
1250 ts
->input
= input_dev
;
1252 mutex_init(&ts
->lock
);
1253 init_waitqueue_head(&ts
->wait
);
1255 pdata
= dev_get_platdata(&spi
->dev
);
1257 pdata
= ads7846_probe_dt(&spi
->dev
);
1258 if (IS_ERR(pdata
)) {
1259 err
= PTR_ERR(pdata
);
1264 ts
->model
= pdata
->model
? : 7846;
1265 ts
->vref_delay_usecs
= pdata
->vref_delay_usecs
? : 100;
1266 ts
->x_plate_ohms
= pdata
->x_plate_ohms
? : 400;
1267 ts
->vref_mv
= pdata
->vref_mv
;
1269 if (pdata
->filter
!= NULL
) {
1270 if (pdata
->filter_init
!= NULL
) {
1271 err
= pdata
->filter_init(pdata
, &ts
->filter_data
);
1275 ts
->filter
= pdata
->filter
;
1276 ts
->filter_cleanup
= pdata
->filter_cleanup
;
1277 } else if (pdata
->debounce_max
) {
1278 ts
->debounce_max
= pdata
->debounce_max
;
1279 if (ts
->debounce_max
< 2)
1280 ts
->debounce_max
= 2;
1281 ts
->debounce_tol
= pdata
->debounce_tol
;
1282 ts
->debounce_rep
= pdata
->debounce_rep
;
1283 ts
->filter
= ads7846_debounce_filter
;
1284 ts
->filter_data
= ts
;
1286 ts
->filter
= ads7846_no_filter
;
1289 err
= ads7846_setup_pendown(spi
, ts
, pdata
);
1291 goto err_cleanup_filter
;
1293 if (pdata
->penirq_recheck_delay_usecs
)
1294 ts
->penirq_recheck_delay_usecs
=
1295 pdata
->penirq_recheck_delay_usecs
;
1297 ts
->wait_for_sync
= pdata
->wait_for_sync
? : null_wait_for_sync
;
1299 snprintf(ts
->phys
, sizeof(ts
->phys
), "%s/input0", dev_name(&spi
->dev
));
1300 snprintf(ts
->name
, sizeof(ts
->name
), "ADS%d Touchscreen", ts
->model
);
1302 input_dev
->name
= ts
->name
;
1303 input_dev
->phys
= ts
->phys
;
1304 input_dev
->dev
.parent
= &spi
->dev
;
1306 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
1307 input_dev
->keybit
[BIT_WORD(BTN_TOUCH
)] = BIT_MASK(BTN_TOUCH
);
1308 input_set_abs_params(input_dev
, ABS_X
,
1310 pdata
->x_max
? : MAX_12BIT
,
1312 input_set_abs_params(input_dev
, ABS_Y
,
1314 pdata
->y_max
? : MAX_12BIT
,
1316 input_set_abs_params(input_dev
, ABS_PRESSURE
,
1317 pdata
->pressure_min
, pdata
->pressure_max
, 0, 0);
1320 * Parse common framework properties. Must be done here to ensure the
1321 * correct behaviour in case of using the legacy vendor bindings. The
1322 * general binding value overrides the vendor specific one.
1324 touchscreen_parse_properties(ts
->input
, false, &ts
->core_prop
);
1325 ts
->pressure_max
= input_abs_get_max(input_dev
, ABS_PRESSURE
) ? : ~0;
1328 * Check if legacy ti,swap-xy binding is used instead of
1329 * touchscreen-swapped-x-y
1331 if (!ts
->core_prop
.swap_x_y
&& pdata
->swap_xy
) {
1332 swap(input_dev
->absinfo
[ABS_X
], input_dev
->absinfo
[ABS_Y
]);
1333 ts
->core_prop
.swap_x_y
= true;
1336 ads7846_setup_spi_msg(ts
, pdata
);
1338 ts
->reg
= regulator_get(&spi
->dev
, "vcc");
1339 if (IS_ERR(ts
->reg
)) {
1340 err
= PTR_ERR(ts
->reg
);
1341 dev_err(&spi
->dev
, "unable to get regulator: %d\n", err
);
1345 err
= regulator_enable(ts
->reg
);
1347 dev_err(&spi
->dev
, "unable to enable regulator: %d\n", err
);
1348 goto err_put_regulator
;
1351 irq_flags
= pdata
->irq_flags
? : IRQF_TRIGGER_FALLING
;
1352 irq_flags
|= IRQF_ONESHOT
;
1354 err
= request_threaded_irq(spi
->irq
, ads7846_hard_irq
, ads7846_irq
,
1355 irq_flags
, spi
->dev
.driver
->name
, ts
);
1356 if (err
&& !pdata
->irq_flags
) {
1358 "trying pin change workaround on irq %d\n", spi
->irq
);
1359 irq_flags
|= IRQF_TRIGGER_RISING
;
1360 err
= request_threaded_irq(spi
->irq
,
1361 ads7846_hard_irq
, ads7846_irq
,
1362 irq_flags
, spi
->dev
.driver
->name
, ts
);
1366 dev_dbg(&spi
->dev
, "irq %d busy?\n", spi
->irq
);
1367 goto err_disable_regulator
;
1370 err
= ads784x_hwmon_register(spi
, ts
);
1374 dev_info(&spi
->dev
, "touchscreen, irq %d\n", spi
->irq
);
1377 * Take a first sample, leaving nPENIRQ active and vREF off; avoid
1378 * the touchscreen, in case it's not connected.
1380 if (ts
->model
== 7845)
1381 ads7845_read12_ser(&spi
->dev
, PWRDOWN
);
1383 (void) ads7846_read12_ser(&spi
->dev
, READ_12BIT_SER(vaux
));
1385 err
= sysfs_create_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1387 goto err_remove_hwmon
;
1389 err
= input_register_device(input_dev
);
1391 goto err_remove_attr_group
;
1393 device_init_wakeup(&spi
->dev
, pdata
->wakeup
);
1396 * If device does not carry platform data we must have allocated it
1397 * when parsing DT data.
1399 if (!dev_get_platdata(&spi
->dev
))
1400 devm_kfree(&spi
->dev
, (void *)pdata
);
1404 err_remove_attr_group
:
1405 sysfs_remove_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1407 ads784x_hwmon_unregister(spi
, ts
);
1409 free_irq(spi
->irq
, ts
);
1410 err_disable_regulator
:
1411 regulator_disable(ts
->reg
);
1413 regulator_put(ts
->reg
);
1415 if (!ts
->get_pendown_state
)
1416 gpio_free(ts
->gpio_pendown
);
1418 if (ts
->filter_cleanup
)
1419 ts
->filter_cleanup(ts
->filter_data
);
1421 input_free_device(input_dev
);
1427 static int ads7846_remove(struct spi_device
*spi
)
1429 struct ads7846
*ts
= spi_get_drvdata(spi
);
1431 sysfs_remove_group(&spi
->dev
.kobj
, &ads784x_attr_group
);
1433 ads7846_disable(ts
);
1434 free_irq(ts
->spi
->irq
, ts
);
1436 input_unregister_device(ts
->input
);
1438 ads784x_hwmon_unregister(spi
, ts
);
1440 regulator_put(ts
->reg
);
1442 if (!ts
->get_pendown_state
) {
1444 * If we are not using specialized pendown method we must
1445 * have been relying on gpio we set up ourselves.
1447 gpio_free(ts
->gpio_pendown
);
1450 if (ts
->filter_cleanup
)
1451 ts
->filter_cleanup(ts
->filter_data
);
1456 dev_dbg(&spi
->dev
, "unregistered touchscreen\n");
1461 static struct spi_driver ads7846_driver
= {
1465 .of_match_table
= of_match_ptr(ads7846_dt_ids
),
1467 .probe
= ads7846_probe
,
1468 .remove
= ads7846_remove
,
1471 module_spi_driver(ads7846_driver
);
1473 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1474 MODULE_LICENSE("GPL");
1475 MODULE_ALIAS("spi:ads7846");