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>
36 #include <asm/unaligned.h>
39 * This code has been heavily tested on a Nokia 770, and lightly
40 * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz).
41 * TSC2046 is just newer ads7846 silicon.
42 * Support for ads7843 tested on Atmel at91sam926x-EK.
43 * Support for ads7845 has only been stubbed in.
44 * Support for Analog Devices AD7873 and AD7843 tested.
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 /* ms delay before the first sample */
61 #define TS_POLL_PERIOD 5 /* ms 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 */)
68 * For portability, we can't read 12 bit values using SPI (which
69 * would make the controller deliver them as native byte order u16
70 * with msbs zeroed). Instead, we read them as two 8-bit values,
71 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
82 * We allocate this separately to avoid cache line sharing issues when
83 * driver is used with DMA-based SPI controllers (like atmel_spi) on
84 * systems where main memory is not DMA-coherent (most non-x86 boards).
86 struct ads7846_packet {
87 u8 read_x, read_y, read_z1, read_z2, pwrdown;
88 u16 dummy; /* for the pwrdown read */
90 /* for ads7845 with mpc5121 psc spi we use 3-byte buffers */
91 u8 read_x_cmd[3], read_y_cmd[3], pwrdown_cmd[3];
95 struct input_dev *input;
99 struct spi_device *spi;
100 struct regulator *reg;
102 #if IS_ENABLED(CONFIG_HWMON)
103 struct device *hwmon;
108 u16 vref_delay_usecs;
115 struct ads7846_packet *packet;
117 struct spi_transfer xfer[18];
118 struct spi_message msg[5];
120 wait_queue_head_t wait;
132 u16 penirq_recheck_delay_usecs;
134 struct touchscreen_properties core_prop;
137 bool stopped; /* P: lock */
138 bool disabled; /* P: lock */
139 bool suspended; /* P: lock */
141 int (*filter)(void *data, int data_idx, int *val);
143 void (*filter_cleanup)(void *data);
144 int (*get_pendown_state)(void);
147 void (*wait_for_sync)(void);
150 /* leave chip selected when we're done, for quicker re-select? */
152 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
154 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
157 /*--------------------------------------------------------------------------*/
159 /* The ADS7846 has touchscreen and other sensors.
160 * Earlier ads784x chips are somewhat compatible.
162 #define ADS_START (1 << 7)
163 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
164 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
165 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
166 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
167 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
168 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
169 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
170 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
171 #define ADS_8_BIT (1 << 3)
172 #define ADS_12_BIT (0 << 3)
173 #define ADS_SER (1 << 2) /* non-differential */
174 #define ADS_DFR (0 << 2) /* differential */
175 #define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */
176 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
177 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
178 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
180 #define MAX_12BIT ((1<<12)-1)
182 /* leave ADC powered up (disables penirq) between differential samples */
183 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
184 | ADS_12_BIT | ADS_DFR | \
185 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
187 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
188 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
189 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
191 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
192 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
194 /* single-ended samples need to first power up reference voltage;
195 * we leave both ADC and VREF powered
197 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
198 | ADS_12_BIT | ADS_SER)
200 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
201 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
203 static int get_pendown_state(struct ads7846 *ts)
205 if (ts->get_pendown_state)
206 return ts->get_pendown_state();
208 return !gpio_get_value(ts->gpio_pendown);
211 static void ads7846_report_pen_up(struct ads7846 *ts)
213 struct input_dev *input = ts->input;
215 input_report_key(input, BTN_TOUCH, 0);
216 input_report_abs(input, ABS_PRESSURE, 0);
220 dev_vdbg(&ts->spi->dev, "UP\n");
223 /* Must be called with ts->lock held */
224 static void ads7846_stop(struct ads7846 *ts)
226 if (!ts->disabled && !ts->suspended) {
227 /* Signal IRQ thread to stop polling and disable the handler. */
231 disable_irq(ts->spi->irq);
235 /* Must be called with ts->lock held */
236 static void ads7846_restart(struct ads7846 *ts)
238 if (!ts->disabled && !ts->suspended) {
239 /* Check if pen was released since last stop */
240 if (ts->pendown && !get_pendown_state(ts))
241 ads7846_report_pen_up(ts);
243 /* Tell IRQ thread that it may poll the device. */
246 enable_irq(ts->spi->irq);
250 /* Must be called with ts->lock held */
251 static void __ads7846_disable(struct ads7846 *ts)
254 regulator_disable(ts->reg);
257 * We know the chip's in low power mode since we always
258 * leave it that way after every request
262 /* Must be called with ts->lock held */
263 static void __ads7846_enable(struct ads7846 *ts)
267 error = regulator_enable(ts->reg);
269 dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error);
274 static void ads7846_disable(struct ads7846 *ts)
276 mutex_lock(&ts->lock);
281 __ads7846_disable(ts);
286 mutex_unlock(&ts->lock);
289 static void ads7846_enable(struct ads7846 *ts)
291 mutex_lock(&ts->lock);
295 ts->disabled = false;
298 __ads7846_enable(ts);
301 mutex_unlock(&ts->lock);
304 /*--------------------------------------------------------------------------*/
307 * Non-touchscreen sensors only use single-ended conversions.
308 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
309 * ads7846 lets that pin be unconnected, to use internal vREF.
317 struct spi_message msg;
318 struct spi_transfer xfer[6];
320 * DMA (thus cache coherency maintenance) requires the
321 * transfer buffers to live in their own cache lines.
323 __be16 sample ____cacheline_aligned;
326 struct ads7845_ser_req {
328 struct spi_message msg;
329 struct spi_transfer xfer[2];
331 * DMA (thus cache coherency maintenance) requires the
332 * transfer buffers to live in their own cache lines.
334 u8 sample[3] ____cacheline_aligned;
337 static int ads7846_read12_ser(struct device *dev, unsigned command)
339 struct spi_device *spi = to_spi_device(dev);
340 struct ads7846 *ts = dev_get_drvdata(dev);
344 req = kzalloc(sizeof *req, GFP_KERNEL);
348 spi_message_init(&req->msg);
350 /* maybe turn on internal vREF, and let it settle */
351 if (ts->use_internal) {
352 req->ref_on = REF_ON;
353 req->xfer[0].tx_buf = &req->ref_on;
354 req->xfer[0].len = 1;
355 spi_message_add_tail(&req->xfer[0], &req->msg);
357 req->xfer[1].rx_buf = &req->scratch;
358 req->xfer[1].len = 2;
360 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
361 req->xfer[1].delay.value = ts->vref_delay_usecs;
362 req->xfer[1].delay.unit = SPI_DELAY_UNIT_USECS;
363 spi_message_add_tail(&req->xfer[1], &req->msg);
365 /* Enable reference voltage */
366 command |= ADS_PD10_REF_ON;
369 /* Enable ADC in every case */
370 command |= ADS_PD10_ADC_ON;
373 req->command = (u8) command;
374 req->xfer[2].tx_buf = &req->command;
375 req->xfer[2].len = 1;
376 spi_message_add_tail(&req->xfer[2], &req->msg);
378 req->xfer[3].rx_buf = &req->sample;
379 req->xfer[3].len = 2;
380 spi_message_add_tail(&req->xfer[3], &req->msg);
382 /* REVISIT: take a few more samples, and compare ... */
384 /* converter in low power mode & enable PENIRQ */
385 req->ref_off = PWRDOWN;
386 req->xfer[4].tx_buf = &req->ref_off;
387 req->xfer[4].len = 1;
388 spi_message_add_tail(&req->xfer[4], &req->msg);
390 req->xfer[5].rx_buf = &req->scratch;
391 req->xfer[5].len = 2;
392 CS_CHANGE(req->xfer[5]);
393 spi_message_add_tail(&req->xfer[5], &req->msg);
395 mutex_lock(&ts->lock);
397 status = spi_sync(spi, &req->msg);
399 mutex_unlock(&ts->lock);
402 /* on-wire is a must-ignore bit, a BE12 value, then padding */
403 status = be16_to_cpu(req->sample);
404 status = status >> 3;
412 static int ads7845_read12_ser(struct device *dev, unsigned command)
414 struct spi_device *spi = to_spi_device(dev);
415 struct ads7846 *ts = dev_get_drvdata(dev);
416 struct ads7845_ser_req *req;
419 req = kzalloc(sizeof *req, GFP_KERNEL);
423 spi_message_init(&req->msg);
425 req->command[0] = (u8) command;
426 req->xfer[0].tx_buf = req->command;
427 req->xfer[0].rx_buf = req->sample;
428 req->xfer[0].len = 3;
429 spi_message_add_tail(&req->xfer[0], &req->msg);
431 mutex_lock(&ts->lock);
433 status = spi_sync(spi, &req->msg);
435 mutex_unlock(&ts->lock);
438 /* BE12 value, then padding */
439 status = get_unaligned_be16(&req->sample[1]);
440 status = status >> 3;
448 #if IS_ENABLED(CONFIG_HWMON)
450 #define SHOW(name, var, adjust) static ssize_t \
451 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
453 struct ads7846 *ts = dev_get_drvdata(dev); \
454 ssize_t v = ads7846_read12_ser(&ts->spi->dev, \
455 READ_12BIT_SER(var)); \
458 return sprintf(buf, "%u\n", adjust(ts, v)); \
460 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
463 /* Sysfs conventions report temperatures in millidegrees Celsius.
464 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
465 * accuracy scheme without calibration data. For now we won't try either;
466 * userspace sees raw sensor values, and must scale/calibrate appropriately.
468 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
473 SHOW(temp0, temp0, null_adjust) /* temp1_input */
474 SHOW(temp1, temp1, null_adjust) /* temp2_input */
477 /* sysfs conventions report voltages in millivolts. We can convert voltages
478 * if we know vREF. userspace may need to scale vAUX to match the board's
479 * external resistors; we assume that vBATT only uses the internal ones.
481 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
485 /* external resistors may scale vAUX into 0..vREF */
486 retval *= ts->vref_mv;
487 retval = retval >> 12;
492 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
494 unsigned retval = vaux_adjust(ts, v);
496 /* ads7846 has a resistor ladder to scale this signal down */
497 if (ts->model == 7846)
503 SHOW(in0_input, vaux, vaux_adjust)
504 SHOW(in1_input, vbatt, vbatt_adjust)
506 static umode_t ads7846_is_visible(struct kobject *kobj, struct attribute *attr,
509 struct device *dev = container_of(kobj, struct device, kobj);
510 struct ads7846 *ts = dev_get_drvdata(dev);
512 if (ts->model == 7843 && index < 2) /* in0, in1 */
514 if (ts->model == 7845 && index != 2) /* in0 */
520 static struct attribute *ads7846_attributes[] = {
521 &dev_attr_temp0.attr, /* 0 */
522 &dev_attr_temp1.attr, /* 1 */
523 &dev_attr_in0_input.attr, /* 2 */
524 &dev_attr_in1_input.attr, /* 3 */
528 static const struct attribute_group ads7846_attr_group = {
529 .attrs = ads7846_attributes,
530 .is_visible = ads7846_is_visible,
532 __ATTRIBUTE_GROUPS(ads7846_attr);
534 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
536 /* hwmon sensors need a reference voltage */
540 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
542 ts->use_internal = true;
549 "external vREF for ADS%d not specified\n",
556 ts->hwmon = hwmon_device_register_with_groups(&spi->dev, spi->modalias,
557 ts, ads7846_attr_groups);
559 return PTR_ERR_OR_ZERO(ts->hwmon);
562 static void ads784x_hwmon_unregister(struct spi_device *spi,
566 hwmon_device_unregister(ts->hwmon);
570 static inline int ads784x_hwmon_register(struct spi_device *spi,
576 static inline void ads784x_hwmon_unregister(struct spi_device *spi,
582 static ssize_t ads7846_pen_down_show(struct device *dev,
583 struct device_attribute *attr, char *buf)
585 struct ads7846 *ts = dev_get_drvdata(dev);
587 return sprintf(buf, "%u\n", ts->pendown);
590 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
592 static ssize_t ads7846_disable_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
595 struct ads7846 *ts = dev_get_drvdata(dev);
597 return sprintf(buf, "%u\n", ts->disabled);
600 static ssize_t ads7846_disable_store(struct device *dev,
601 struct device_attribute *attr,
602 const char *buf, size_t count)
604 struct ads7846 *ts = dev_get_drvdata(dev);
608 err = kstrtouint(buf, 10, &i);
620 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
622 static struct attribute *ads784x_attributes[] = {
623 &dev_attr_pen_down.attr,
624 &dev_attr_disable.attr,
628 static const struct attribute_group ads784x_attr_group = {
629 .attrs = ads784x_attributes,
632 /*--------------------------------------------------------------------------*/
634 static void null_wait_for_sync(void)
638 static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
640 struct ads7846 *ts = ads;
642 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
643 /* Start over collecting consistent readings. */
646 * Repeat it, if this was the first read or the read
647 * wasn't consistent enough.
649 if (ts->read_cnt < ts->debounce_max) {
650 ts->last_read = *val;
652 return ADS7846_FILTER_REPEAT;
655 * Maximum number of debouncing reached and still
656 * not enough number of consistent readings. Abort
657 * the whole sample, repeat it in the next sampling
661 return ADS7846_FILTER_IGNORE;
664 if (++ts->read_rep > ts->debounce_rep) {
666 * Got a good reading for this coordinate,
667 * go for the next one.
671 return ADS7846_FILTER_OK;
673 /* Read more values that are consistent. */
675 return ADS7846_FILTER_REPEAT;
680 static int ads7846_no_filter(void *ads, int data_idx, int *val)
682 return ADS7846_FILTER_OK;
685 static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)
688 struct spi_transfer *t =
689 list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
691 if (ts->model == 7845) {
692 value = be16_to_cpup((__be16 *)&(((char *)t->rx_buf)[1]));
695 * adjust: on-wire is a must-ignore bit, a BE12 value, then
696 * padding; built from two 8 bit values written msb-first.
698 value = be16_to_cpup((__be16 *)t->rx_buf);
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);
710 *(u16 *)t->rx_buf = val;
713 static void ads7846_read_state(struct ads7846 *ts)
715 struct ads7846_packet *packet = ts->packet;
716 struct spi_message *m;
722 while (msg_idx < ts->msg_count) {
726 m = &ts->msg[msg_idx];
727 error = spi_sync(ts->spi, m);
729 dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
730 packet->tc.ignore = true;
735 * Last message is power down request, no need to convert
736 * or filter the value.
738 if (msg_idx < ts->msg_count - 1) {
740 val = ads7846_get_value(ts, m);
742 action = ts->filter(ts->filter_data, msg_idx, &val);
744 case ADS7846_FILTER_REPEAT:
747 case ADS7846_FILTER_IGNORE:
748 packet->tc.ignore = true;
749 msg_idx = ts->msg_count - 1;
752 case ADS7846_FILTER_OK:
753 ads7846_update_value(m, val);
754 packet->tc.ignore = false;
767 static void ads7846_report_state(struct ads7846 *ts)
769 struct ads7846_packet *packet = ts->packet;
774 * ads7846_get_value() does in-place conversion (including byte swap)
775 * from on-the-wire format as part of debouncing to get stable
778 if (ts->model == 7845) {
779 x = *(u16 *)packet->tc.x_buf;
780 y = *(u16 *)packet->tc.y_buf;
790 /* range filtering */
794 if (ts->model == 7843) {
795 Rt = ts->pressure_max / 2;
796 } else if (ts->model == 7845) {
797 if (get_pendown_state(ts))
798 Rt = ts->pressure_max / 2;
801 dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
802 } else if (likely(x && z1)) {
803 /* compute touch pressure resistance using equation #2 */
806 Rt *= ts->x_plate_ohms;
807 Rt = DIV_ROUND_CLOSEST(Rt, 16);
810 Rt = DIV_ROUND_CLOSEST(Rt, 256);
816 * Sample found inconsistent by debouncing or pressure is beyond
817 * the maximum. Don't report it to user space, repeat at least
818 * once more the measurement
820 if (packet->tc.ignore || Rt > ts->pressure_max) {
821 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
822 packet->tc.ignore, Rt);
827 * Maybe check the pendown state before reporting. This discards
828 * false readings when the pen is lifted.
830 if (ts->penirq_recheck_delay_usecs) {
831 udelay(ts->penirq_recheck_delay_usecs);
832 if (!get_pendown_state(ts))
837 * NOTE: We can't rely on the pressure to determine the pen down
838 * state, even this controller has a pressure sensor. The pressure
839 * value can fluctuate for quite a while after lifting the pen and
840 * in some cases may not even settle at the expected value.
842 * The only safe way to check for the pen up condition is in the
843 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
846 struct input_dev *input = ts->input;
849 input_report_key(input, BTN_TOUCH, 1);
851 dev_vdbg(&ts->spi->dev, "DOWN\n");
854 touchscreen_report_pos(input, &ts->core_prop, x, y, false);
855 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
858 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
862 static irqreturn_t ads7846_hard_irq(int irq, void *handle)
864 struct ads7846 *ts = handle;
866 return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
870 static irqreturn_t ads7846_irq(int irq, void *handle)
872 struct ads7846 *ts = handle;
874 /* Start with a small delay before checking pendown state */
875 msleep(TS_POLL_DELAY);
877 while (!ts->stopped && get_pendown_state(ts)) {
879 /* pen is down, continue with the measurement */
880 ads7846_read_state(ts);
883 ads7846_report_state(ts);
885 wait_event_timeout(ts->wait, ts->stopped,
886 msecs_to_jiffies(TS_POLL_PERIOD));
889 if (ts->pendown && !ts->stopped)
890 ads7846_report_pen_up(ts);
895 static int __maybe_unused ads7846_suspend(struct device *dev)
897 struct ads7846 *ts = dev_get_drvdata(dev);
899 mutex_lock(&ts->lock);
901 if (!ts->suspended) {
904 __ads7846_disable(ts);
906 if (device_may_wakeup(&ts->spi->dev))
907 enable_irq_wake(ts->spi->irq);
909 ts->suspended = true;
912 mutex_unlock(&ts->lock);
917 static int __maybe_unused ads7846_resume(struct device *dev)
919 struct ads7846 *ts = dev_get_drvdata(dev);
921 mutex_lock(&ts->lock);
925 ts->suspended = false;
927 if (device_may_wakeup(&ts->spi->dev))
928 disable_irq_wake(ts->spi->irq);
931 __ads7846_enable(ts);
934 mutex_unlock(&ts->lock);
939 static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
941 static int ads7846_setup_pendown(struct spi_device *spi,
943 const struct ads7846_platform_data *pdata)
948 * REVISIT when the irq can be triggered active-low, or if for some
949 * reason the touchscreen isn't hooked up, we don't need to access
953 if (pdata->get_pendown_state) {
954 ts->get_pendown_state = pdata->get_pendown_state;
955 } else if (gpio_is_valid(pdata->gpio_pendown)) {
957 err = gpio_request_one(pdata->gpio_pendown, GPIOF_IN,
961 "failed to request/setup pendown GPIO%d: %d\n",
962 pdata->gpio_pendown, err);
966 ts->gpio_pendown = pdata->gpio_pendown;
968 if (pdata->gpio_pendown_debounce)
969 gpio_set_debounce(pdata->gpio_pendown,
970 pdata->gpio_pendown_debounce);
972 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
980 * Set up the transfers to read touchscreen state; this assumes we
981 * use formula #2 for pressure, not #3.
983 static void ads7846_setup_spi_msg(struct ads7846 *ts,
984 const struct ads7846_platform_data *pdata)
986 struct spi_message *m = &ts->msg[0];
987 struct spi_transfer *x = ts->xfer;
988 struct ads7846_packet *packet = ts->packet;
989 int vref = pdata->keep_vref_on;
991 if (ts->model == 7873) {
993 * The AD7873 is almost identical to the ADS7846
994 * keep VREF off during differential/ratiometric
1002 spi_message_init(m);
1005 if (ts->model == 7845) {
1006 packet->read_y_cmd[0] = READ_Y(vref);
1007 packet->read_y_cmd[1] = 0;
1008 packet->read_y_cmd[2] = 0;
1009 x->tx_buf = &packet->read_y_cmd[0];
1010 x->rx_buf = &packet->tc.y_buf[0];
1012 spi_message_add_tail(x, m);
1014 /* y- still on; turn on only y+ (and ADC) */
1015 packet->read_y = READ_Y(vref);
1016 x->tx_buf = &packet->read_y;
1018 spi_message_add_tail(x, m);
1021 x->rx_buf = &packet->tc.y;
1023 spi_message_add_tail(x, m);
1027 * The first sample after switching drivers can be low quality;
1028 * optionally discard it, using a second one after the signals
1029 * have had enough time to stabilize.
1031 if (pdata->settle_delay_usecs) {
1032 x->delay.value = pdata->settle_delay_usecs;
1033 x->delay.unit = SPI_DELAY_UNIT_USECS;
1036 x->tx_buf = &packet->read_y;
1038 spi_message_add_tail(x, m);
1041 x->rx_buf = &packet->tc.y;
1043 spi_message_add_tail(x, m);
1048 spi_message_init(m);
1051 if (ts->model == 7845) {
1053 packet->read_x_cmd[0] = READ_X(vref);
1054 packet->read_x_cmd[1] = 0;
1055 packet->read_x_cmd[2] = 0;
1056 x->tx_buf = &packet->read_x_cmd[0];
1057 x->rx_buf = &packet->tc.x_buf[0];
1059 spi_message_add_tail(x, m);
1061 /* turn y- off, x+ on, then leave in lowpower */
1063 packet->read_x = READ_X(vref);
1064 x->tx_buf = &packet->read_x;
1066 spi_message_add_tail(x, m);
1069 x->rx_buf = &packet->tc.x;
1071 spi_message_add_tail(x, m);
1074 /* ... maybe discard first sample ... */
1075 if (pdata->settle_delay_usecs) {
1076 x->delay.value = pdata->settle_delay_usecs;
1077 x->delay.unit = SPI_DELAY_UNIT_USECS;
1080 x->tx_buf = &packet->read_x;
1082 spi_message_add_tail(x, m);
1085 x->rx_buf = &packet->tc.x;
1087 spi_message_add_tail(x, m);
1090 /* turn y+ off, x- on; we'll use formula #2 */
1091 if (ts->model == 7846) {
1094 spi_message_init(m);
1098 packet->read_z1 = READ_Z1(vref);
1099 x->tx_buf = &packet->read_z1;
1101 spi_message_add_tail(x, m);
1104 x->rx_buf = &packet->tc.z1;
1106 spi_message_add_tail(x, m);
1108 /* ... maybe discard first sample ... */
1109 if (pdata->settle_delay_usecs) {
1110 x->delay.value = pdata->settle_delay_usecs;
1111 x->delay.unit = SPI_DELAY_UNIT_USECS;
1114 x->tx_buf = &packet->read_z1;
1116 spi_message_add_tail(x, m);
1119 x->rx_buf = &packet->tc.z1;
1121 spi_message_add_tail(x, m);
1126 spi_message_init(m);
1130 packet->read_z2 = READ_Z2(vref);
1131 x->tx_buf = &packet->read_z2;
1133 spi_message_add_tail(x, m);
1136 x->rx_buf = &packet->tc.z2;
1138 spi_message_add_tail(x, m);
1140 /* ... maybe discard first sample ... */
1141 if (pdata->settle_delay_usecs) {
1142 x->delay.value = pdata->settle_delay_usecs;
1143 x->delay.unit = SPI_DELAY_UNIT_USECS;
1146 x->tx_buf = &packet->read_z2;
1148 spi_message_add_tail(x, m);
1151 x->rx_buf = &packet->tc.z2;
1153 spi_message_add_tail(x, m);
1160 spi_message_init(m);
1163 if (ts->model == 7845) {
1165 packet->pwrdown_cmd[0] = PWRDOWN;
1166 packet->pwrdown_cmd[1] = 0;
1167 packet->pwrdown_cmd[2] = 0;
1168 x->tx_buf = &packet->pwrdown_cmd[0];
1172 packet->pwrdown = PWRDOWN;
1173 x->tx_buf = &packet->pwrdown;
1175 spi_message_add_tail(x, m);
1178 x->rx_buf = &packet->dummy;
1183 spi_message_add_tail(x, m);
1187 static const struct of_device_id ads7846_dt_ids[] = {
1188 { .compatible = "ti,tsc2046", .data = (void *) 7846 },
1189 { .compatible = "ti,ads7843", .data = (void *) 7843 },
1190 { .compatible = "ti,ads7845", .data = (void *) 7845 },
1191 { .compatible = "ti,ads7846", .data = (void *) 7846 },
1192 { .compatible = "ti,ads7873", .data = (void *) 7873 },
1195 MODULE_DEVICE_TABLE(of, ads7846_dt_ids);
1197 static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1199 struct ads7846_platform_data *pdata;
1200 struct device_node *node = dev->of_node;
1201 const struct of_device_id *match;
1205 dev_err(dev, "Device does not have associated DT data\n");
1206 return ERR_PTR(-EINVAL);
1209 match = of_match_device(ads7846_dt_ids, dev);
1211 dev_err(dev, "Unknown device model\n");
1212 return ERR_PTR(-EINVAL);
1215 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1217 return ERR_PTR(-ENOMEM);
1219 pdata->model = (unsigned long)match->data;
1221 of_property_read_u16(node, "ti,vref-delay-usecs",
1222 &pdata->vref_delay_usecs);
1223 of_property_read_u16(node, "ti,vref-mv", &pdata->vref_mv);
1224 pdata->keep_vref_on = of_property_read_bool(node, "ti,keep-vref-on");
1226 pdata->swap_xy = of_property_read_bool(node, "ti,swap-xy");
1228 of_property_read_u16(node, "ti,settle-delay-usec",
1229 &pdata->settle_delay_usecs);
1230 of_property_read_u16(node, "ti,penirq-recheck-delay-usecs",
1231 &pdata->penirq_recheck_delay_usecs);
1233 of_property_read_u16(node, "ti,x-plate-ohms", &pdata->x_plate_ohms);
1234 of_property_read_u16(node, "ti,y-plate-ohms", &pdata->y_plate_ohms);
1236 of_property_read_u16(node, "ti,x-min", &pdata->x_min);
1237 of_property_read_u16(node, "ti,y-min", &pdata->y_min);
1238 of_property_read_u16(node, "ti,x-max", &pdata->x_max);
1239 of_property_read_u16(node, "ti,y-max", &pdata->y_max);
1242 * touchscreen-max-pressure gets parsed during
1243 * touchscreen_parse_properties()
1245 of_property_read_u16(node, "ti,pressure-min", &pdata->pressure_min);
1246 if (!of_property_read_u32(node, "touchscreen-min-pressure", &value))
1247 pdata->pressure_min = (u16) value;
1248 of_property_read_u16(node, "ti,pressure-max", &pdata->pressure_max);
1250 of_property_read_u16(node, "ti,debounce-max", &pdata->debounce_max);
1251 if (!of_property_read_u32(node, "touchscreen-average-samples", &value))
1252 pdata->debounce_max = (u16) value;
1253 of_property_read_u16(node, "ti,debounce-tol", &pdata->debounce_tol);
1254 of_property_read_u16(node, "ti,debounce-rep", &pdata->debounce_rep);
1256 of_property_read_u32(node, "ti,pendown-gpio-debounce",
1257 &pdata->gpio_pendown_debounce);
1259 pdata->wakeup = of_property_read_bool(node, "wakeup-source") ||
1260 of_property_read_bool(node, "linux,wakeup");
1262 pdata->gpio_pendown = of_get_named_gpio(dev->of_node, "pendown-gpio", 0);
1267 static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1269 dev_err(dev, "no platform data defined\n");
1270 return ERR_PTR(-EINVAL);
1274 static int ads7846_probe(struct spi_device *spi)
1276 const struct ads7846_platform_data *pdata;
1278 struct ads7846_packet *packet;
1279 struct input_dev *input_dev;
1280 unsigned long irq_flags;
1284 dev_dbg(&spi->dev, "no IRQ?\n");
1288 /* don't exceed max specified sample rate */
1289 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
1290 dev_err(&spi->dev, "f(sample) %d KHz?\n",
1291 (spi->max_speed_hz/SAMPLE_BITS)/1000);
1296 * We'd set TX word size 8 bits and RX word size to 13 bits ... except
1297 * that even if the hardware can do that, the SPI controller driver
1298 * may not. So we stick to very-portable 8 bit words, both RX and TX.
1300 spi->bits_per_word = 8;
1301 spi->mode = SPI_MODE_0;
1302 err = spi_setup(spi);
1306 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
1307 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
1308 input_dev = input_allocate_device();
1309 if (!ts || !packet || !input_dev) {
1314 spi_set_drvdata(spi, ts);
1316 ts->packet = packet;
1318 ts->input = input_dev;
1320 mutex_init(&ts->lock);
1321 init_waitqueue_head(&ts->wait);
1323 pdata = dev_get_platdata(&spi->dev);
1325 pdata = ads7846_probe_dt(&spi->dev);
1326 if (IS_ERR(pdata)) {
1327 err = PTR_ERR(pdata);
1332 ts->model = pdata->model ? : 7846;
1333 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
1334 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
1335 ts->vref_mv = pdata->vref_mv;
1337 if (pdata->filter != NULL) {
1338 if (pdata->filter_init != NULL) {
1339 err = pdata->filter_init(pdata, &ts->filter_data);
1343 ts->filter = pdata->filter;
1344 ts->filter_cleanup = pdata->filter_cleanup;
1345 } else if (pdata->debounce_max) {
1346 ts->debounce_max = pdata->debounce_max;
1347 if (ts->debounce_max < 2)
1348 ts->debounce_max = 2;
1349 ts->debounce_tol = pdata->debounce_tol;
1350 ts->debounce_rep = pdata->debounce_rep;
1351 ts->filter = ads7846_debounce_filter;
1352 ts->filter_data = ts;
1354 ts->filter = ads7846_no_filter;
1357 err = ads7846_setup_pendown(spi, ts, pdata);
1359 goto err_cleanup_filter;
1361 if (pdata->penirq_recheck_delay_usecs)
1362 ts->penirq_recheck_delay_usecs =
1363 pdata->penirq_recheck_delay_usecs;
1365 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1367 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
1368 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
1370 input_dev->name = ts->name;
1371 input_dev->phys = ts->phys;
1372 input_dev->dev.parent = &spi->dev;
1374 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1375 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1376 input_set_abs_params(input_dev, ABS_X,
1378 pdata->x_max ? : MAX_12BIT,
1380 input_set_abs_params(input_dev, ABS_Y,
1382 pdata->y_max ? : MAX_12BIT,
1384 input_set_abs_params(input_dev, ABS_PRESSURE,
1385 pdata->pressure_min, pdata->pressure_max, 0, 0);
1388 * Parse common framework properties. Must be done here to ensure the
1389 * correct behaviour in case of using the legacy vendor bindings. The
1390 * general binding value overrides the vendor specific one.
1392 touchscreen_parse_properties(ts->input, false, &ts->core_prop);
1393 ts->pressure_max = input_abs_get_max(input_dev, ABS_PRESSURE) ? : ~0;
1396 * Check if legacy ti,swap-xy binding is used instead of
1397 * touchscreen-swapped-x-y
1399 if (!ts->core_prop.swap_x_y && pdata->swap_xy) {
1400 swap(input_dev->absinfo[ABS_X], input_dev->absinfo[ABS_Y]);
1401 ts->core_prop.swap_x_y = true;
1404 ads7846_setup_spi_msg(ts, pdata);
1406 ts->reg = regulator_get(&spi->dev, "vcc");
1407 if (IS_ERR(ts->reg)) {
1408 err = PTR_ERR(ts->reg);
1409 dev_err(&spi->dev, "unable to get regulator: %d\n", err);
1413 err = regulator_enable(ts->reg);
1415 dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
1416 goto err_put_regulator;
1419 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
1420 irq_flags |= IRQF_ONESHOT;
1422 err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,
1423 irq_flags, spi->dev.driver->name, ts);
1424 if (err && !pdata->irq_flags) {
1426 "trying pin change workaround on irq %d\n", spi->irq);
1427 irq_flags |= IRQF_TRIGGER_RISING;
1428 err = request_threaded_irq(spi->irq,
1429 ads7846_hard_irq, ads7846_irq,
1430 irq_flags, spi->dev.driver->name, ts);
1434 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1435 goto err_disable_regulator;
1438 err = ads784x_hwmon_register(spi, ts);
1442 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1445 * Take a first sample, leaving nPENIRQ active and vREF off; avoid
1446 * the touchscreen, in case it's not connected.
1448 if (ts->model == 7845)
1449 ads7845_read12_ser(&spi->dev, PWRDOWN);
1451 (void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux));
1453 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1455 goto err_remove_hwmon;
1457 err = input_register_device(input_dev);
1459 goto err_remove_attr_group;
1461 device_init_wakeup(&spi->dev, pdata->wakeup);
1464 * If device does not carry platform data we must have allocated it
1465 * when parsing DT data.
1467 if (!dev_get_platdata(&spi->dev))
1468 devm_kfree(&spi->dev, (void *)pdata);
1472 err_remove_attr_group:
1473 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1475 ads784x_hwmon_unregister(spi, ts);
1477 free_irq(spi->irq, ts);
1478 err_disable_regulator:
1479 regulator_disable(ts->reg);
1481 regulator_put(ts->reg);
1483 if (!ts->get_pendown_state)
1484 gpio_free(ts->gpio_pendown);
1486 if (ts->filter_cleanup)
1487 ts->filter_cleanup(ts->filter_data);
1489 input_free_device(input_dev);
1495 static int ads7846_remove(struct spi_device *spi)
1497 struct ads7846 *ts = spi_get_drvdata(spi);
1499 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1501 ads7846_disable(ts);
1502 free_irq(ts->spi->irq, ts);
1504 input_unregister_device(ts->input);
1506 ads784x_hwmon_unregister(spi, ts);
1508 regulator_put(ts->reg);
1510 if (!ts->get_pendown_state) {
1512 * If we are not using specialized pendown method we must
1513 * have been relying on gpio we set up ourselves.
1515 gpio_free(ts->gpio_pendown);
1518 if (ts->filter_cleanup)
1519 ts->filter_cleanup(ts->filter_data);
1524 dev_dbg(&spi->dev, "unregistered touchscreen\n");
1529 static struct spi_driver ads7846_driver = {
1533 .of_match_table = of_match_ptr(ads7846_dt_ids),
1535 .probe = ads7846_probe,
1536 .remove = ads7846_remove,
1539 module_spi_driver(ads7846_driver);
1541 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1542 MODULE_LICENSE("GPL");
1543 MODULE_ALIAS("spi:ads7846");