Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * ADS7846 based touchscreen and sensor driver
4 *
5 * Copyright (c) 2005 David Brownell
6 * Copyright (c) 2006 Nokia Corporation
7 * Various changes: Imre Deak <imre.deak@nokia.com>
8 *
9 * Using code from:
10 * - corgi_ts.c
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
16 */
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>
26#include <linux/pm.h>
27#include <linux/property.h>
28#include <linux/gpio/consumer.h>
29#include <linux/spi/spi.h>
30#include <linux/spi/ads7846.h>
31#include <linux/regulator/consumer.h>
32#include <linux/module.h>
33#include <linux/unaligned.h>
34
35/*
36 * This code has been heavily tested on a Nokia 770, and lightly
37 * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz).
38 * TSC2046 is just newer ads7846 silicon.
39 * Support for ads7843 tested on Atmel at91sam926x-EK.
40 * Support for ads7845 has only been stubbed in.
41 * Support for Analog Devices AD7873 and AD7843 tested.
42 *
43 * IRQ handling needs a workaround because of a shortcoming in handling
44 * edge triggered IRQs on some platforms like the OMAP1/2. These
45 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
46 * have to maintain our own SW IRQ disabled status. This should be
47 * removed as soon as the affected platform's IRQ handling is fixed.
48 *
49 * App note sbaa036 talks in more detail about accurate sampling...
50 * that ought to help in situations like LCDs inducing noise (which
51 * can also be helped by using synch signals) and more generally.
52 * This driver tries to utilize the measures described in the app
53 * note. The strength of filtering can be set in the board-* specific
54 * files.
55 */
56
57#define TS_POLL_DELAY 1 /* ms delay before the first sample */
58#define TS_POLL_PERIOD 5 /* ms delay between samples */
59
60/* this driver doesn't aim at the peak continuous sample rate */
61#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
62
63struct ads7846_buf {
64 u8 cmd;
65 __be16 data;
66} __packed;
67
68struct ads7846_buf_layout {
69 unsigned int offset;
70 unsigned int count;
71 unsigned int skip;
72};
73
74/*
75 * We allocate this separately to avoid cache line sharing issues when
76 * driver is used with DMA-based SPI controllers (like atmel_spi) on
77 * systems where main memory is not DMA-coherent (most non-x86 boards).
78 */
79struct ads7846_packet {
80 unsigned int count;
81 unsigned int count_skip;
82 unsigned int cmds;
83 unsigned int last_cmd_idx;
84 struct ads7846_buf_layout l[5];
85 struct ads7846_buf *rx;
86 struct ads7846_buf *tx;
87
88 struct ads7846_buf pwrdown_cmd;
89
90 bool ignore;
91 u16 x, y, z1, z2;
92};
93
94struct ads7846 {
95 struct input_dev *input;
96 char phys[32];
97 char name[32];
98
99 struct spi_device *spi;
100 struct regulator *reg;
101
102 u16 model;
103 u16 vref_mv;
104 u16 vref_delay_usecs;
105 u16 x_plate_ohms;
106 u16 pressure_max;
107
108 bool swap_xy;
109 bool use_internal;
110
111 struct ads7846_packet *packet;
112
113 struct spi_transfer xfer[18];
114 struct spi_message msg[5];
115 int msg_count;
116 wait_queue_head_t wait;
117
118 bool pendown;
119
120 int read_cnt;
121 int read_rep;
122 int last_read;
123
124 u16 debounce_max;
125 u16 debounce_tol;
126 u16 debounce_rep;
127
128 u16 penirq_recheck_delay_usecs;
129
130 struct touchscreen_properties core_prop;
131
132 struct mutex lock;
133 bool stopped; /* P: lock */
134 bool disabled; /* P: lock */
135 bool suspended; /* P: lock */
136
137 int (*filter)(void *data, int data_idx, int *val);
138 void *filter_data;
139 int (*get_pendown_state)(void);
140 struct gpio_desc *gpio_pendown;
141 struct gpio_desc *gpio_hsync;
142
143 void (*wait_for_sync)(void);
144};
145
146enum ads7846_filter {
147 ADS7846_FILTER_OK,
148 ADS7846_FILTER_REPEAT,
149 ADS7846_FILTER_IGNORE,
150};
151
152/* leave chip selected when we're done, for quicker re-select? */
153#if 0
154#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
155#else
156#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
157#endif
158
159/*--------------------------------------------------------------------------*/
160
161/* The ADS7846 has touchscreen and other sensors.
162 * Earlier ads784x chips are somewhat compatible.
163 */
164#define ADS_START (1 << 7)
165#define ADS_A2A1A0_d_y (1 << 4) /* differential */
166#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
167#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
168#define ADS_A2A1A0_d_x (5 << 4) /* differential */
169#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
170#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
171#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
172#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
173#define ADS_8_BIT (1 << 3)
174#define ADS_12_BIT (0 << 3)
175#define ADS_SER (1 << 2) /* non-differential */
176#define ADS_DFR (0 << 2) /* differential */
177#define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */
178#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
179#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
180#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
181
182#define MAX_12BIT ((1<<12)-1)
183
184/* leave ADC powered up (disables penirq) between differential samples */
185#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
186 | ADS_12_BIT | ADS_DFR | \
187 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
188
189#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
190#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
191#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
192#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
193#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
194
195/* single-ended samples need to first power up reference voltage;
196 * we leave both ADC and VREF powered
197 */
198#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
199 | ADS_12_BIT | ADS_SER)
200
201#define REF_ON (READ_12BIT_DFR(x, 1, 1))
202#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
203
204/* Order commands in the most optimal way to reduce Vref switching and
205 * settling time:
206 * Measure: X; Vref: X+, X-; IN: Y+
207 * Measure: Y; Vref: Y+, Y-; IN: X+
208 * Measure: Z1; Vref: Y+, X-; IN: X+
209 * Measure: Z2; Vref: Y+, X-; IN: Y-
210 */
211enum ads7846_cmds {
212 ADS7846_X,
213 ADS7846_Y,
214 ADS7846_Z1,
215 ADS7846_Z2,
216 ADS7846_PWDOWN,
217};
218
219static int get_pendown_state(struct ads7846 *ts)
220{
221 if (ts->get_pendown_state)
222 return ts->get_pendown_state();
223
224 return gpiod_get_value(ts->gpio_pendown);
225}
226
227static void ads7846_report_pen_up(struct ads7846 *ts)
228{
229 struct input_dev *input = ts->input;
230
231 input_report_key(input, BTN_TOUCH, 0);
232 input_report_abs(input, ABS_PRESSURE, 0);
233 input_sync(input);
234
235 ts->pendown = false;
236 dev_vdbg(&ts->spi->dev, "UP\n");
237}
238
239/* Must be called with ts->lock held */
240static void ads7846_stop(struct ads7846 *ts)
241{
242 if (!ts->disabled && !ts->suspended) {
243 /* Signal IRQ thread to stop polling and disable the handler. */
244 ts->stopped = true;
245 mb();
246 wake_up(&ts->wait);
247 disable_irq(ts->spi->irq);
248 }
249}
250
251/* Must be called with ts->lock held */
252static void ads7846_restart(struct ads7846 *ts)
253{
254 if (!ts->disabled && !ts->suspended) {
255 /* Check if pen was released since last stop */
256 if (ts->pendown && !get_pendown_state(ts))
257 ads7846_report_pen_up(ts);
258
259 /* Tell IRQ thread that it may poll the device. */
260 ts->stopped = false;
261 mb();
262 enable_irq(ts->spi->irq);
263 }
264}
265
266/* Must be called with ts->lock held */
267static void __ads7846_disable(struct ads7846 *ts)
268{
269 ads7846_stop(ts);
270 regulator_disable(ts->reg);
271
272 /*
273 * We know the chip's in low power mode since we always
274 * leave it that way after every request
275 */
276}
277
278/* Must be called with ts->lock held */
279static void __ads7846_enable(struct ads7846 *ts)
280{
281 int error;
282
283 error = regulator_enable(ts->reg);
284 if (error != 0)
285 dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error);
286
287 ads7846_restart(ts);
288}
289
290static void ads7846_disable(struct ads7846 *ts)
291{
292 mutex_lock(&ts->lock);
293
294 if (!ts->disabled) {
295
296 if (!ts->suspended)
297 __ads7846_disable(ts);
298
299 ts->disabled = true;
300 }
301
302 mutex_unlock(&ts->lock);
303}
304
305static void ads7846_enable(struct ads7846 *ts)
306{
307 mutex_lock(&ts->lock);
308
309 if (ts->disabled) {
310
311 ts->disabled = false;
312
313 if (!ts->suspended)
314 __ads7846_enable(ts);
315 }
316
317 mutex_unlock(&ts->lock);
318}
319
320/*--------------------------------------------------------------------------*/
321
322/*
323 * Non-touchscreen sensors only use single-ended conversions.
324 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
325 * ads7846 lets that pin be unconnected, to use internal vREF.
326 */
327
328struct ser_req {
329 u8 ref_on;
330 u8 command;
331 u8 ref_off;
332 u16 scratch;
333 struct spi_message msg;
334 struct spi_transfer xfer[8];
335 /*
336 * DMA (thus cache coherency maintenance) requires the
337 * transfer buffers to live in their own cache lines.
338 */
339 __be16 sample ____cacheline_aligned;
340};
341
342struct ads7845_ser_req {
343 u8 command[3];
344 struct spi_message msg;
345 struct spi_transfer xfer[2];
346 /*
347 * DMA (thus cache coherency maintenance) requires the
348 * transfer buffers to live in their own cache lines.
349 */
350 u8 sample[3] ____cacheline_aligned;
351};
352
353static int ads7846_read12_ser(struct device *dev, unsigned command)
354{
355 struct spi_device *spi = to_spi_device(dev);
356 struct ads7846 *ts = dev_get_drvdata(dev);
357 struct ser_req *req;
358 int status;
359
360 req = kzalloc(sizeof *req, GFP_KERNEL);
361 if (!req)
362 return -ENOMEM;
363
364 spi_message_init(&req->msg);
365
366 /* maybe turn on internal vREF, and let it settle */
367 if (ts->use_internal) {
368 req->ref_on = REF_ON;
369 req->xfer[0].tx_buf = &req->ref_on;
370 req->xfer[0].len = 1;
371 spi_message_add_tail(&req->xfer[0], &req->msg);
372
373 req->xfer[1].rx_buf = &req->scratch;
374 req->xfer[1].len = 2;
375
376 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
377 req->xfer[1].delay.value = ts->vref_delay_usecs;
378 req->xfer[1].delay.unit = SPI_DELAY_UNIT_USECS;
379 spi_message_add_tail(&req->xfer[1], &req->msg);
380
381 /* Enable reference voltage */
382 command |= ADS_PD10_REF_ON;
383 }
384
385 /* Enable ADC in every case */
386 command |= ADS_PD10_ADC_ON;
387
388 /* take sample */
389 req->command = (u8) command;
390 req->xfer[2].tx_buf = &req->command;
391 req->xfer[2].len = 1;
392 spi_message_add_tail(&req->xfer[2], &req->msg);
393
394 req->xfer[3].rx_buf = &req->sample;
395 req->xfer[3].len = 2;
396 spi_message_add_tail(&req->xfer[3], &req->msg);
397
398 /* REVISIT: take a few more samples, and compare ... */
399
400 /* converter in low power mode & enable PENIRQ */
401 req->ref_off = PWRDOWN;
402 req->xfer[4].tx_buf = &req->ref_off;
403 req->xfer[4].len = 1;
404 spi_message_add_tail(&req->xfer[4], &req->msg);
405
406 req->xfer[5].rx_buf = &req->scratch;
407 req->xfer[5].len = 2;
408 spi_message_add_tail(&req->xfer[5], &req->msg);
409
410 /* clear the command register */
411 req->scratch = 0;
412 req->xfer[6].tx_buf = &req->scratch;
413 req->xfer[6].len = 1;
414 spi_message_add_tail(&req->xfer[6], &req->msg);
415
416 req->xfer[7].rx_buf = &req->scratch;
417 req->xfer[7].len = 2;
418 CS_CHANGE(req->xfer[7]);
419 spi_message_add_tail(&req->xfer[7], &req->msg);
420
421 mutex_lock(&ts->lock);
422 ads7846_stop(ts);
423 status = spi_sync(spi, &req->msg);
424 ads7846_restart(ts);
425 mutex_unlock(&ts->lock);
426
427 if (status == 0) {
428 /* on-wire is a must-ignore bit, a BE12 value, then padding */
429 status = be16_to_cpu(req->sample);
430 status = status >> 3;
431 status &= 0x0fff;
432 }
433
434 kfree(req);
435 return status;
436}
437
438static int ads7845_read12_ser(struct device *dev, unsigned command)
439{
440 struct spi_device *spi = to_spi_device(dev);
441 struct ads7846 *ts = dev_get_drvdata(dev);
442 struct ads7845_ser_req *req;
443 int status;
444
445 req = kzalloc(sizeof *req, GFP_KERNEL);
446 if (!req)
447 return -ENOMEM;
448
449 spi_message_init(&req->msg);
450
451 req->command[0] = (u8) command;
452 req->xfer[0].tx_buf = req->command;
453 req->xfer[0].rx_buf = req->sample;
454 req->xfer[0].len = 3;
455 spi_message_add_tail(&req->xfer[0], &req->msg);
456
457 mutex_lock(&ts->lock);
458 ads7846_stop(ts);
459 status = spi_sync(spi, &req->msg);
460 ads7846_restart(ts);
461 mutex_unlock(&ts->lock);
462
463 if (status == 0) {
464 /* BE12 value, then padding */
465 status = get_unaligned_be16(&req->sample[1]);
466 status = status >> 3;
467 status &= 0x0fff;
468 }
469
470 kfree(req);
471 return status;
472}
473
474#if IS_ENABLED(CONFIG_HWMON)
475
476#define SHOW(name, var, adjust) static ssize_t \
477name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
478{ \
479 struct ads7846 *ts = dev_get_drvdata(dev); \
480 ssize_t v = ads7846_read12_ser(&ts->spi->dev, \
481 READ_12BIT_SER(var)); \
482 if (v < 0) \
483 return v; \
484 return sprintf(buf, "%u\n", adjust(ts, v)); \
485} \
486static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
487
488
489/* Sysfs conventions report temperatures in millidegrees Celsius.
490 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
491 * accuracy scheme without calibration data. For now we won't try either;
492 * userspace sees raw sensor values, and must scale/calibrate appropriately.
493 */
494static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
495{
496 return v;
497}
498
499SHOW(temp0, temp0, null_adjust) /* temp1_input */
500SHOW(temp1, temp1, null_adjust) /* temp2_input */
501
502
503/* sysfs conventions report voltages in millivolts. We can convert voltages
504 * if we know vREF. userspace may need to scale vAUX to match the board's
505 * external resistors; we assume that vBATT only uses the internal ones.
506 */
507static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
508{
509 unsigned retval = v;
510
511 /* external resistors may scale vAUX into 0..vREF */
512 retval *= ts->vref_mv;
513 retval = retval >> 12;
514
515 return retval;
516}
517
518static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
519{
520 unsigned retval = vaux_adjust(ts, v);
521
522 /* ads7846 has a resistor ladder to scale this signal down */
523 if (ts->model == 7846)
524 retval *= 4;
525
526 return retval;
527}
528
529SHOW(in0_input, vaux, vaux_adjust)
530SHOW(in1_input, vbatt, vbatt_adjust)
531
532static umode_t ads7846_is_visible(struct kobject *kobj, struct attribute *attr,
533 int index)
534{
535 struct device *dev = kobj_to_dev(kobj);
536 struct ads7846 *ts = dev_get_drvdata(dev);
537
538 if (ts->model == 7843 && index < 2) /* in0, in1 */
539 return 0;
540 if (ts->model == 7845 && index != 2) /* in0 */
541 return 0;
542
543 return attr->mode;
544}
545
546static struct attribute *ads7846_attributes[] = {
547 &dev_attr_temp0.attr, /* 0 */
548 &dev_attr_temp1.attr, /* 1 */
549 &dev_attr_in0_input.attr, /* 2 */
550 &dev_attr_in1_input.attr, /* 3 */
551 NULL,
552};
553
554static const struct attribute_group ads7846_attr_group = {
555 .attrs = ads7846_attributes,
556 .is_visible = ads7846_is_visible,
557};
558__ATTRIBUTE_GROUPS(ads7846_attr);
559
560static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
561{
562 struct device *hwmon;
563
564 /* hwmon sensors need a reference voltage */
565 switch (ts->model) {
566 case 7846:
567 if (!ts->vref_mv) {
568 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
569 ts->vref_mv = 2500;
570 ts->use_internal = true;
571 }
572 break;
573 case 7845:
574 case 7843:
575 if (!ts->vref_mv) {
576 dev_warn(&spi->dev,
577 "external vREF for ADS%d not specified\n",
578 ts->model);
579 return 0;
580 }
581 break;
582 }
583
584 hwmon = devm_hwmon_device_register_with_groups(&spi->dev,
585 spi->modalias, ts,
586 ads7846_attr_groups);
587
588 return PTR_ERR_OR_ZERO(hwmon);
589}
590
591#else
592static inline int ads784x_hwmon_register(struct spi_device *spi,
593 struct ads7846 *ts)
594{
595 return 0;
596}
597#endif
598
599static ssize_t ads7846_pen_down_show(struct device *dev,
600 struct device_attribute *attr, char *buf)
601{
602 struct ads7846 *ts = dev_get_drvdata(dev);
603
604 return sprintf(buf, "%u\n", ts->pendown);
605}
606
607static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
608
609static ssize_t ads7846_disable_show(struct device *dev,
610 struct device_attribute *attr, char *buf)
611{
612 struct ads7846 *ts = dev_get_drvdata(dev);
613
614 return sprintf(buf, "%u\n", ts->disabled);
615}
616
617static ssize_t ads7846_disable_store(struct device *dev,
618 struct device_attribute *attr,
619 const char *buf, size_t count)
620{
621 struct ads7846 *ts = dev_get_drvdata(dev);
622 unsigned int i;
623 int err;
624
625 err = kstrtouint(buf, 10, &i);
626 if (err)
627 return err;
628
629 if (i)
630 ads7846_disable(ts);
631 else
632 ads7846_enable(ts);
633
634 return count;
635}
636
637static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
638
639static struct attribute *ads784x_attrs[] = {
640 &dev_attr_pen_down.attr,
641 &dev_attr_disable.attr,
642 NULL,
643};
644ATTRIBUTE_GROUPS(ads784x);
645
646/*--------------------------------------------------------------------------*/
647
648static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
649{
650 struct ads7846 *ts = ads;
651
652 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
653 /* Start over collecting consistent readings. */
654 ts->read_rep = 0;
655 /*
656 * Repeat it, if this was the first read or the read
657 * wasn't consistent enough.
658 */
659 if (ts->read_cnt < ts->debounce_max) {
660 ts->last_read = *val;
661 ts->read_cnt++;
662 return ADS7846_FILTER_REPEAT;
663 } else {
664 /*
665 * Maximum number of debouncing reached and still
666 * not enough number of consistent readings. Abort
667 * the whole sample, repeat it in the next sampling
668 * period.
669 */
670 ts->read_cnt = 0;
671 return ADS7846_FILTER_IGNORE;
672 }
673 } else {
674 if (++ts->read_rep > ts->debounce_rep) {
675 /*
676 * Got a good reading for this coordinate,
677 * go for the next one.
678 */
679 ts->read_cnt = 0;
680 ts->read_rep = 0;
681 return ADS7846_FILTER_OK;
682 } else {
683 /* Read more values that are consistent. */
684 ts->read_cnt++;
685 return ADS7846_FILTER_REPEAT;
686 }
687 }
688}
689
690static int ads7846_no_filter(void *ads, int data_idx, int *val)
691{
692 return ADS7846_FILTER_OK;
693}
694
695static int ads7846_get_value(struct ads7846_buf *buf)
696{
697 int value;
698
699 value = be16_to_cpup(&buf->data);
700
701 /* enforce ADC output is 12 bits width */
702 return (value >> 3) & 0xfff;
703}
704
705static void ads7846_set_cmd_val(struct ads7846 *ts, enum ads7846_cmds cmd_idx,
706 u16 val)
707{
708 struct ads7846_packet *packet = ts->packet;
709
710 switch (cmd_idx) {
711 case ADS7846_Y:
712 packet->y = val;
713 break;
714 case ADS7846_X:
715 packet->x = val;
716 break;
717 case ADS7846_Z1:
718 packet->z1 = val;
719 break;
720 case ADS7846_Z2:
721 packet->z2 = val;
722 break;
723 default:
724 WARN_ON_ONCE(1);
725 }
726}
727
728static u8 ads7846_get_cmd(enum ads7846_cmds cmd_idx, int vref)
729{
730 switch (cmd_idx) {
731 case ADS7846_Y:
732 return READ_Y(vref);
733 case ADS7846_X:
734 return READ_X(vref);
735
736 /* 7846 specific commands */
737 case ADS7846_Z1:
738 return READ_Z1(vref);
739 case ADS7846_Z2:
740 return READ_Z2(vref);
741 case ADS7846_PWDOWN:
742 return PWRDOWN;
743 default:
744 WARN_ON_ONCE(1);
745 }
746
747 return 0;
748}
749
750static bool ads7846_cmd_need_settle(enum ads7846_cmds cmd_idx)
751{
752 switch (cmd_idx) {
753 case ADS7846_X:
754 case ADS7846_Y:
755 case ADS7846_Z1:
756 case ADS7846_Z2:
757 return true;
758 case ADS7846_PWDOWN:
759 return false;
760 default:
761 WARN_ON_ONCE(1);
762 }
763
764 return false;
765}
766
767static int ads7846_filter(struct ads7846 *ts)
768{
769 struct ads7846_packet *packet = ts->packet;
770 int action;
771 int val;
772 unsigned int cmd_idx, b;
773
774 packet->ignore = false;
775 for (cmd_idx = packet->last_cmd_idx; cmd_idx < packet->cmds - 1; cmd_idx++) {
776 struct ads7846_buf_layout *l = &packet->l[cmd_idx];
777
778 packet->last_cmd_idx = cmd_idx;
779
780 for (b = l->skip; b < l->count; b++) {
781 val = ads7846_get_value(&packet->rx[l->offset + b]);
782
783 action = ts->filter(ts->filter_data, cmd_idx, &val);
784 if (action == ADS7846_FILTER_REPEAT) {
785 if (b == l->count - 1)
786 return -EAGAIN;
787 } else if (action == ADS7846_FILTER_OK) {
788 ads7846_set_cmd_val(ts, cmd_idx, val);
789 break;
790 } else {
791 packet->ignore = true;
792 return 0;
793 }
794 }
795 }
796
797 return 0;
798}
799
800static void ads7846_wait_for_hsync(struct ads7846 *ts)
801{
802 if (ts->wait_for_sync) {
803 ts->wait_for_sync();
804 return;
805 }
806
807 if (!ts->gpio_hsync)
808 return;
809
810 /*
811 * Wait for HSYNC to assert the line should be flagged
812 * as active low so here we are waiting for it to assert
813 */
814 while (!gpiod_get_value(ts->gpio_hsync))
815 cpu_relax();
816
817 /* Then we wait for it do de-assert */
818 while (gpiod_get_value(ts->gpio_hsync))
819 cpu_relax();
820}
821
822static void ads7846_read_state(struct ads7846 *ts)
823{
824 struct ads7846_packet *packet = ts->packet;
825 struct spi_message *m;
826 int msg_idx = 0;
827 int error;
828
829 packet->last_cmd_idx = 0;
830
831 while (true) {
832 ads7846_wait_for_hsync(ts);
833
834 m = &ts->msg[msg_idx];
835 error = spi_sync(ts->spi, m);
836 if (error) {
837 dev_err_ratelimited(&ts->spi->dev, "spi_sync --> %d\n", error);
838 packet->ignore = true;
839 return;
840 }
841
842 error = ads7846_filter(ts);
843 if (error)
844 continue;
845
846 return;
847 }
848}
849
850static void ads7846_report_state(struct ads7846 *ts)
851{
852 struct ads7846_packet *packet = ts->packet;
853 unsigned int Rt;
854 u16 x, y, z1, z2;
855
856 x = packet->x;
857 y = packet->y;
858 if (ts->model == 7845) {
859 z1 = 0;
860 z2 = 0;
861 } else {
862 z1 = packet->z1;
863 z2 = packet->z2;
864 }
865
866 /* range filtering */
867 if (x == MAX_12BIT)
868 x = 0;
869
870 if (ts->model == 7843 || ts->model == 7845) {
871 Rt = ts->pressure_max / 2;
872 } else if (likely(x && z1)) {
873 /* compute touch pressure resistance using equation #2 */
874 Rt = z2;
875 Rt -= z1;
876 Rt *= ts->x_plate_ohms;
877 Rt = DIV_ROUND_CLOSEST(Rt, 16);
878 Rt *= x;
879 Rt /= z1;
880 Rt = DIV_ROUND_CLOSEST(Rt, 256);
881 } else {
882 Rt = 0;
883 }
884
885 /*
886 * Sample found inconsistent by debouncing or pressure is beyond
887 * the maximum. Don't report it to user space, repeat at least
888 * once more the measurement
889 */
890 if (packet->ignore || Rt > ts->pressure_max) {
891 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
892 packet->ignore, Rt);
893 return;
894 }
895
896 /*
897 * Maybe check the pendown state before reporting. This discards
898 * false readings when the pen is lifted.
899 */
900 if (ts->penirq_recheck_delay_usecs) {
901 udelay(ts->penirq_recheck_delay_usecs);
902 if (!get_pendown_state(ts))
903 Rt = 0;
904 }
905
906 /*
907 * NOTE: We can't rely on the pressure to determine the pen down
908 * state, even this controller has a pressure sensor. The pressure
909 * value can fluctuate for quite a while after lifting the pen and
910 * in some cases may not even settle at the expected value.
911 *
912 * The only safe way to check for the pen up condition is in the
913 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
914 */
915 if (Rt) {
916 struct input_dev *input = ts->input;
917
918 if (!ts->pendown) {
919 input_report_key(input, BTN_TOUCH, 1);
920 ts->pendown = true;
921 dev_vdbg(&ts->spi->dev, "DOWN\n");
922 }
923
924 touchscreen_report_pos(input, &ts->core_prop, x, y, false);
925 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
926
927 input_sync(input);
928 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
929 }
930}
931
932static irqreturn_t ads7846_hard_irq(int irq, void *handle)
933{
934 struct ads7846 *ts = handle;
935
936 return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
937}
938
939
940static irqreturn_t ads7846_irq(int irq, void *handle)
941{
942 struct ads7846 *ts = handle;
943
944 /* Start with a small delay before checking pendown state */
945 msleep(TS_POLL_DELAY);
946
947 while (!ts->stopped && get_pendown_state(ts)) {
948
949 /* pen is down, continue with the measurement */
950 ads7846_read_state(ts);
951
952 if (!ts->stopped)
953 ads7846_report_state(ts);
954
955 wait_event_timeout(ts->wait, ts->stopped,
956 msecs_to_jiffies(TS_POLL_PERIOD));
957 }
958
959 if (ts->pendown && !ts->stopped)
960 ads7846_report_pen_up(ts);
961
962 return IRQ_HANDLED;
963}
964
965static int ads7846_suspend(struct device *dev)
966{
967 struct ads7846 *ts = dev_get_drvdata(dev);
968
969 mutex_lock(&ts->lock);
970
971 if (!ts->suspended) {
972
973 if (!ts->disabled)
974 __ads7846_disable(ts);
975
976 if (device_may_wakeup(&ts->spi->dev))
977 enable_irq_wake(ts->spi->irq);
978
979 ts->suspended = true;
980 }
981
982 mutex_unlock(&ts->lock);
983
984 return 0;
985}
986
987static int ads7846_resume(struct device *dev)
988{
989 struct ads7846 *ts = dev_get_drvdata(dev);
990
991 mutex_lock(&ts->lock);
992
993 if (ts->suspended) {
994
995 ts->suspended = false;
996
997 if (device_may_wakeup(&ts->spi->dev))
998 disable_irq_wake(ts->spi->irq);
999
1000 if (!ts->disabled)
1001 __ads7846_enable(ts);
1002 }
1003
1004 mutex_unlock(&ts->lock);
1005
1006 return 0;
1007}
1008
1009static DEFINE_SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
1010
1011static int ads7846_setup_pendown(struct spi_device *spi,
1012 struct ads7846 *ts,
1013 const struct ads7846_platform_data *pdata)
1014{
1015 /*
1016 * REVISIT when the irq can be triggered active-low, or if for some
1017 * reason the touchscreen isn't hooked up, we don't need to access
1018 * the pendown state.
1019 */
1020
1021 if (pdata->get_pendown_state) {
1022 ts->get_pendown_state = pdata->get_pendown_state;
1023 } else {
1024 ts->gpio_pendown = gpiod_get(&spi->dev, "pendown", GPIOD_IN);
1025 if (IS_ERR(ts->gpio_pendown)) {
1026 dev_err(&spi->dev, "failed to request pendown GPIO\n");
1027 return PTR_ERR(ts->gpio_pendown);
1028 }
1029 if (pdata->gpio_pendown_debounce)
1030 gpiod_set_debounce(ts->gpio_pendown,
1031 pdata->gpio_pendown_debounce);
1032 }
1033
1034 return 0;
1035}
1036
1037/*
1038 * Set up the transfers to read touchscreen state; this assumes we
1039 * use formula #2 for pressure, not #3.
1040 */
1041static int ads7846_setup_spi_msg(struct ads7846 *ts,
1042 const struct ads7846_platform_data *pdata)
1043{
1044 struct spi_message *m = &ts->msg[0];
1045 struct spi_transfer *x = ts->xfer;
1046 struct ads7846_packet *packet = ts->packet;
1047 int vref = pdata->keep_vref_on;
1048 unsigned int count, offset = 0;
1049 unsigned int cmd_idx, b;
1050 unsigned long time;
1051 size_t size = 0;
1052
1053 /* time per bit */
1054 time = NSEC_PER_SEC / ts->spi->max_speed_hz;
1055
1056 count = pdata->settle_delay_usecs * NSEC_PER_USEC / time;
1057 packet->count_skip = DIV_ROUND_UP(count, 24);
1058
1059 if (ts->debounce_max && ts->debounce_rep)
1060 /* ads7846_debounce_filter() is making ts->debounce_rep + 2
1061 * reads. So we need to get all samples for normal case. */
1062 packet->count = ts->debounce_rep + 2;
1063 else
1064 packet->count = 1;
1065
1066 if (ts->model == 7846)
1067 packet->cmds = 5; /* x, y, z1, z2, pwdown */
1068 else
1069 packet->cmds = 3; /* x, y, pwdown */
1070
1071 for (cmd_idx = 0; cmd_idx < packet->cmds; cmd_idx++) {
1072 struct ads7846_buf_layout *l = &packet->l[cmd_idx];
1073 unsigned int max_count;
1074
1075 if (cmd_idx == packet->cmds - 1)
1076 cmd_idx = ADS7846_PWDOWN;
1077
1078 if (ads7846_cmd_need_settle(cmd_idx))
1079 max_count = packet->count + packet->count_skip;
1080 else
1081 max_count = packet->count;
1082
1083 l->offset = offset;
1084 offset += max_count;
1085 l->count = max_count;
1086 l->skip = packet->count_skip;
1087 size += sizeof(*packet->tx) * max_count;
1088 }
1089
1090 packet->tx = devm_kzalloc(&ts->spi->dev, size, GFP_KERNEL);
1091 if (!packet->tx)
1092 return -ENOMEM;
1093
1094 packet->rx = devm_kzalloc(&ts->spi->dev, size, GFP_KERNEL);
1095 if (!packet->rx)
1096 return -ENOMEM;
1097
1098 if (ts->model == 7873) {
1099 /*
1100 * The AD7873 is almost identical to the ADS7846
1101 * keep VREF off during differential/ratiometric
1102 * conversion modes.
1103 */
1104 ts->model = 7846;
1105 vref = 0;
1106 }
1107
1108 ts->msg_count = 1;
1109 spi_message_init(m);
1110 m->context = ts;
1111
1112 for (cmd_idx = 0; cmd_idx < packet->cmds; cmd_idx++) {
1113 struct ads7846_buf_layout *l = &packet->l[cmd_idx];
1114 u8 cmd;
1115
1116 if (cmd_idx == packet->cmds - 1)
1117 cmd_idx = ADS7846_PWDOWN;
1118
1119 cmd = ads7846_get_cmd(cmd_idx, vref);
1120
1121 for (b = 0; b < l->count; b++)
1122 packet->tx[l->offset + b].cmd = cmd;
1123 }
1124
1125 x->tx_buf = packet->tx;
1126 x->rx_buf = packet->rx;
1127 x->len = size;
1128 spi_message_add_tail(x, m);
1129
1130 return 0;
1131}
1132
1133static const struct of_device_id ads7846_dt_ids[] = {
1134 { .compatible = "ti,tsc2046", .data = (void *) 7846 },
1135 { .compatible = "ti,ads7843", .data = (void *) 7843 },
1136 { .compatible = "ti,ads7845", .data = (void *) 7845 },
1137 { .compatible = "ti,ads7846", .data = (void *) 7846 },
1138 { .compatible = "ti,ads7873", .data = (void *) 7873 },
1139 { }
1140};
1141MODULE_DEVICE_TABLE(of, ads7846_dt_ids);
1142
1143static const struct spi_device_id ads7846_spi_ids[] = {
1144 { "tsc2046", 7846 },
1145 { "ads7843", 7843 },
1146 { "ads7845", 7845 },
1147 { "ads7846", 7846 },
1148 { "ads7873", 7873 },
1149 { },
1150};
1151MODULE_DEVICE_TABLE(spi, ads7846_spi_ids);
1152
1153static const struct ads7846_platform_data *ads7846_get_props(struct device *dev)
1154{
1155 struct ads7846_platform_data *pdata;
1156 u32 value;
1157
1158 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1159 if (!pdata)
1160 return ERR_PTR(-ENOMEM);
1161
1162 pdata->model = (uintptr_t)device_get_match_data(dev);
1163
1164 device_property_read_u16(dev, "ti,vref-delay-usecs",
1165 &pdata->vref_delay_usecs);
1166 device_property_read_u16(dev, "ti,vref-mv", &pdata->vref_mv);
1167 pdata->keep_vref_on = device_property_read_bool(dev, "ti,keep-vref-on");
1168
1169 pdata->swap_xy = device_property_read_bool(dev, "ti,swap-xy");
1170
1171 device_property_read_u16(dev, "ti,settle-delay-usec",
1172 &pdata->settle_delay_usecs);
1173 device_property_read_u16(dev, "ti,penirq-recheck-delay-usecs",
1174 &pdata->penirq_recheck_delay_usecs);
1175
1176 device_property_read_u16(dev, "ti,x-plate-ohms", &pdata->x_plate_ohms);
1177 device_property_read_u16(dev, "ti,y-plate-ohms", &pdata->y_plate_ohms);
1178
1179 device_property_read_u16(dev, "ti,x-min", &pdata->x_min);
1180 device_property_read_u16(dev, "ti,y-min", &pdata->y_min);
1181 device_property_read_u16(dev, "ti,x-max", &pdata->x_max);
1182 device_property_read_u16(dev, "ti,y-max", &pdata->y_max);
1183
1184 /*
1185 * touchscreen-max-pressure gets parsed during
1186 * touchscreen_parse_properties()
1187 */
1188 device_property_read_u16(dev, "ti,pressure-min", &pdata->pressure_min);
1189 if (!device_property_read_u32(dev, "touchscreen-min-pressure", &value))
1190 pdata->pressure_min = (u16) value;
1191 device_property_read_u16(dev, "ti,pressure-max", &pdata->pressure_max);
1192
1193 device_property_read_u16(dev, "ti,debounce-max", &pdata->debounce_max);
1194 if (!device_property_read_u32(dev, "touchscreen-average-samples", &value))
1195 pdata->debounce_max = (u16) value;
1196 device_property_read_u16(dev, "ti,debounce-tol", &pdata->debounce_tol);
1197 device_property_read_u16(dev, "ti,debounce-rep", &pdata->debounce_rep);
1198
1199 device_property_read_u32(dev, "ti,pendown-gpio-debounce",
1200 &pdata->gpio_pendown_debounce);
1201
1202 pdata->wakeup = device_property_read_bool(dev, "wakeup-source") ||
1203 device_property_read_bool(dev, "linux,wakeup");
1204
1205 return pdata;
1206}
1207
1208static void ads7846_regulator_disable(void *regulator)
1209{
1210 regulator_disable(regulator);
1211}
1212
1213static int ads7846_probe(struct spi_device *spi)
1214{
1215 const struct ads7846_platform_data *pdata;
1216 struct ads7846 *ts;
1217 struct device *dev = &spi->dev;
1218 struct ads7846_packet *packet;
1219 struct input_dev *input_dev;
1220 unsigned long irq_flags;
1221 int err;
1222
1223 if (!spi->irq) {
1224 dev_dbg(dev, "no IRQ?\n");
1225 return -EINVAL;
1226 }
1227
1228 /* don't exceed max specified sample rate */
1229 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
1230 dev_err(dev, "f(sample) %d KHz?\n",
1231 (spi->max_speed_hz/SAMPLE_BITS)/1000);
1232 return -EINVAL;
1233 }
1234
1235 /*
1236 * We'd set TX word size 8 bits and RX word size to 13 bits ... except
1237 * that even if the hardware can do that, the SPI controller driver
1238 * may not. So we stick to very-portable 8 bit words, both RX and TX.
1239 */
1240 spi->bits_per_word = 8;
1241 spi->mode &= ~SPI_MODE_X_MASK;
1242 spi->mode |= SPI_MODE_0;
1243 err = spi_setup(spi);
1244 if (err < 0)
1245 return err;
1246
1247 ts = devm_kzalloc(dev, sizeof(struct ads7846), GFP_KERNEL);
1248 if (!ts)
1249 return -ENOMEM;
1250
1251 packet = devm_kzalloc(dev, sizeof(struct ads7846_packet), GFP_KERNEL);
1252 if (!packet)
1253 return -ENOMEM;
1254
1255 input_dev = devm_input_allocate_device(dev);
1256 if (!input_dev)
1257 return -ENOMEM;
1258
1259 spi_set_drvdata(spi, ts);
1260
1261 ts->packet = packet;
1262 ts->spi = spi;
1263 ts->input = input_dev;
1264
1265 mutex_init(&ts->lock);
1266 init_waitqueue_head(&ts->wait);
1267
1268 pdata = dev_get_platdata(dev);
1269 if (!pdata) {
1270 pdata = ads7846_get_props(dev);
1271 if (IS_ERR(pdata))
1272 return PTR_ERR(pdata);
1273 }
1274
1275 ts->model = pdata->model ? : 7846;
1276 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
1277 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
1278 ts->vref_mv = pdata->vref_mv;
1279
1280 if (pdata->debounce_max) {
1281 ts->debounce_max = pdata->debounce_max;
1282 if (ts->debounce_max < 2)
1283 ts->debounce_max = 2;
1284 ts->debounce_tol = pdata->debounce_tol;
1285 ts->debounce_rep = pdata->debounce_rep;
1286 ts->filter = ads7846_debounce_filter;
1287 ts->filter_data = ts;
1288 } else {
1289 ts->filter = ads7846_no_filter;
1290 }
1291
1292 err = ads7846_setup_pendown(spi, ts, pdata);
1293 if (err)
1294 return err;
1295
1296 if (pdata->penirq_recheck_delay_usecs)
1297 ts->penirq_recheck_delay_usecs =
1298 pdata->penirq_recheck_delay_usecs;
1299
1300 ts->wait_for_sync = pdata->wait_for_sync;
1301
1302 ts->gpio_hsync = devm_gpiod_get_optional(dev, "ti,hsync", GPIOD_IN);
1303 if (IS_ERR(ts->gpio_hsync))
1304 return PTR_ERR(ts->gpio_hsync);
1305
1306 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
1307 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
1308
1309 input_dev->name = ts->name;
1310 input_dev->phys = ts->phys;
1311
1312 input_dev->id.bustype = BUS_SPI;
1313 input_dev->id.product = pdata->model;
1314
1315 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
1316 input_set_abs_params(input_dev, ABS_X,
1317 pdata->x_min ? : 0,
1318 pdata->x_max ? : MAX_12BIT,
1319 0, 0);
1320 input_set_abs_params(input_dev, ABS_Y,
1321 pdata->y_min ? : 0,
1322 pdata->y_max ? : MAX_12BIT,
1323 0, 0);
1324 if (ts->model != 7845)
1325 input_set_abs_params(input_dev, ABS_PRESSURE,
1326 pdata->pressure_min, pdata->pressure_max, 0, 0);
1327
1328 /*
1329 * Parse common framework properties. Must be done here to ensure the
1330 * correct behaviour in case of using the legacy vendor bindings. The
1331 * general binding value overrides the vendor specific one.
1332 */
1333 touchscreen_parse_properties(ts->input, false, &ts->core_prop);
1334 ts->pressure_max = input_abs_get_max(input_dev, ABS_PRESSURE) ? : ~0;
1335
1336 /*
1337 * Check if legacy ti,swap-xy binding is used instead of
1338 * touchscreen-swapped-x-y
1339 */
1340 if (!ts->core_prop.swap_x_y && pdata->swap_xy) {
1341 swap(input_dev->absinfo[ABS_X], input_dev->absinfo[ABS_Y]);
1342 ts->core_prop.swap_x_y = true;
1343 }
1344
1345 ads7846_setup_spi_msg(ts, pdata);
1346
1347 ts->reg = devm_regulator_get(dev, "vcc");
1348 if (IS_ERR(ts->reg)) {
1349 err = PTR_ERR(ts->reg);
1350 dev_err(dev, "unable to get regulator: %d\n", err);
1351 return err;
1352 }
1353
1354 err = regulator_enable(ts->reg);
1355 if (err) {
1356 dev_err(dev, "unable to enable regulator: %d\n", err);
1357 return err;
1358 }
1359
1360 err = devm_add_action_or_reset(dev, ads7846_regulator_disable, ts->reg);
1361 if (err)
1362 return err;
1363
1364 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
1365 irq_flags |= IRQF_ONESHOT;
1366
1367 err = devm_request_threaded_irq(dev, spi->irq,
1368 ads7846_hard_irq, ads7846_irq,
1369 irq_flags, dev->driver->name, ts);
1370 if (err && err != -EPROBE_DEFER && !pdata->irq_flags) {
1371 dev_info(dev,
1372 "trying pin change workaround on irq %d\n", spi->irq);
1373 irq_flags |= IRQF_TRIGGER_RISING;
1374 err = devm_request_threaded_irq(dev, spi->irq,
1375 ads7846_hard_irq, ads7846_irq,
1376 irq_flags, dev->driver->name,
1377 ts);
1378 }
1379
1380 if (err) {
1381 dev_dbg(dev, "irq %d busy?\n", spi->irq);
1382 return err;
1383 }
1384
1385 err = ads784x_hwmon_register(spi, ts);
1386 if (err)
1387 return err;
1388
1389 dev_info(dev, "touchscreen, irq %d\n", spi->irq);
1390
1391 /*
1392 * Take a first sample, leaving nPENIRQ active and vREF off; avoid
1393 * the touchscreen, in case it's not connected.
1394 */
1395 if (ts->model == 7845)
1396 ads7845_read12_ser(dev, PWRDOWN);
1397 else
1398 (void) ads7846_read12_ser(dev, READ_12BIT_SER(vaux));
1399
1400 err = input_register_device(input_dev);
1401 if (err)
1402 return err;
1403
1404 device_init_wakeup(dev, pdata->wakeup);
1405
1406 /*
1407 * If device does not carry platform data we must have allocated it
1408 * when parsing DT data.
1409 */
1410 if (!dev_get_platdata(dev))
1411 devm_kfree(dev, (void *)pdata);
1412
1413 return 0;
1414}
1415
1416static void ads7846_remove(struct spi_device *spi)
1417{
1418 struct ads7846 *ts = spi_get_drvdata(spi);
1419
1420 ads7846_stop(ts);
1421}
1422
1423static struct spi_driver ads7846_driver = {
1424 .driver = {
1425 .name = "ads7846",
1426 .dev_groups = ads784x_groups,
1427 .pm = pm_sleep_ptr(&ads7846_pm),
1428 .of_match_table = ads7846_dt_ids,
1429 },
1430 .probe = ads7846_probe,
1431 .remove = ads7846_remove,
1432 .id_table = ads7846_spi_ids,
1433};
1434
1435module_spi_driver(ads7846_driver);
1436
1437MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1438MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * ADS7846 based touchscreen and sensor driver
4 *
5 * Copyright (c) 2005 David Brownell
6 * Copyright (c) 2006 Nokia Corporation
7 * Various changes: Imre Deak <imre.deak@nokia.com>
8 *
9 * Using code from:
10 * - corgi_ts.c
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
16 */
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>
26#include <linux/pm.h>
27#include <linux/of.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>
36
37/*
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.
44 *
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.
50 *
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
56 * files.
57 */
58
59#define TS_POLL_DELAY 1 /* ms delay before the first sample */
60#define TS_POLL_PERIOD 5 /* ms delay between samples */
61
62/* this driver doesn't aim at the peak continuous sample rate */
63#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
64
65struct ads7846_buf {
66 u8 cmd;
67 __be16 data;
68} __packed;
69
70struct ads7846_buf_layout {
71 unsigned int offset;
72 unsigned int count;
73 unsigned int skip;
74};
75
76/*
77 * We allocate this separately to avoid cache line sharing issues when
78 * driver is used with DMA-based SPI controllers (like atmel_spi) on
79 * systems where main memory is not DMA-coherent (most non-x86 boards).
80 */
81struct ads7846_packet {
82 unsigned int count;
83 unsigned int count_skip;
84 unsigned int cmds;
85 unsigned int last_cmd_idx;
86 struct ads7846_buf_layout l[5];
87 struct ads7846_buf *rx;
88 struct ads7846_buf *tx;
89
90 struct ads7846_buf pwrdown_cmd;
91
92 bool ignore;
93 u16 x, y, z1, z2;
94};
95
96struct ads7846 {
97 struct input_dev *input;
98 char phys[32];
99 char name[32];
100
101 struct spi_device *spi;
102 struct regulator *reg;
103
104 u16 model;
105 u16 vref_mv;
106 u16 vref_delay_usecs;
107 u16 x_plate_ohms;
108 u16 pressure_max;
109
110 bool swap_xy;
111 bool use_internal;
112
113 struct ads7846_packet *packet;
114
115 struct spi_transfer xfer[18];
116 struct spi_message msg[5];
117 int msg_count;
118 wait_queue_head_t wait;
119
120 bool pendown;
121
122 int read_cnt;
123 int read_rep;
124 int last_read;
125
126 u16 debounce_max;
127 u16 debounce_tol;
128 u16 debounce_rep;
129
130 u16 penirq_recheck_delay_usecs;
131
132 struct touchscreen_properties core_prop;
133
134 struct mutex lock;
135 bool stopped; /* P: lock */
136 bool disabled; /* P: lock */
137 bool suspended; /* P: lock */
138
139 int (*filter)(void *data, int data_idx, int *val);
140 void *filter_data;
141 int (*get_pendown_state)(void);
142 int gpio_pendown;
143
144 void (*wait_for_sync)(void);
145};
146
147enum ads7846_filter {
148 ADS7846_FILTER_OK,
149 ADS7846_FILTER_REPEAT,
150 ADS7846_FILTER_IGNORE,
151};
152
153/* leave chip selected when we're done, for quicker re-select? */
154#if 0
155#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
156#else
157#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
158#endif
159
160/*--------------------------------------------------------------------------*/
161
162/* The ADS7846 has touchscreen and other sensors.
163 * Earlier ads784x chips are somewhat compatible.
164 */
165#define ADS_START (1 << 7)
166#define ADS_A2A1A0_d_y (1 << 4) /* differential */
167#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
168#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
169#define ADS_A2A1A0_d_x (5 << 4) /* differential */
170#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
171#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
172#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
173#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
174#define ADS_8_BIT (1 << 3)
175#define ADS_12_BIT (0 << 3)
176#define ADS_SER (1 << 2) /* non-differential */
177#define ADS_DFR (0 << 2) /* differential */
178#define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */
179#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
180#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
181#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
182
183#define MAX_12BIT ((1<<12)-1)
184
185/* leave ADC powered up (disables penirq) between differential samples */
186#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
187 | ADS_12_BIT | ADS_DFR | \
188 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
189
190#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
191#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
192#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
193#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
194#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
195
196/* single-ended samples need to first power up reference voltage;
197 * we leave both ADC and VREF powered
198 */
199#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
200 | ADS_12_BIT | ADS_SER)
201
202#define REF_ON (READ_12BIT_DFR(x, 1, 1))
203#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
204
205/* Order commands in the most optimal way to reduce Vref switching and
206 * settling time:
207 * Measure: X; Vref: X+, X-; IN: Y+
208 * Measure: Y; Vref: Y+, Y-; IN: X+
209 * Measure: Z1; Vref: Y+, X-; IN: X+
210 * Measure: Z2; Vref: Y+, X-; IN: Y-
211 */
212enum ads7846_cmds {
213 ADS7846_X,
214 ADS7846_Y,
215 ADS7846_Z1,
216 ADS7846_Z2,
217 ADS7846_PWDOWN,
218};
219
220static int get_pendown_state(struct ads7846 *ts)
221{
222 if (ts->get_pendown_state)
223 return ts->get_pendown_state();
224
225 return !gpio_get_value(ts->gpio_pendown);
226}
227
228static void ads7846_report_pen_up(struct ads7846 *ts)
229{
230 struct input_dev *input = ts->input;
231
232 input_report_key(input, BTN_TOUCH, 0);
233 input_report_abs(input, ABS_PRESSURE, 0);
234 input_sync(input);
235
236 ts->pendown = false;
237 dev_vdbg(&ts->spi->dev, "UP\n");
238}
239
240/* Must be called with ts->lock held */
241static void ads7846_stop(struct ads7846 *ts)
242{
243 if (!ts->disabled && !ts->suspended) {
244 /* Signal IRQ thread to stop polling and disable the handler. */
245 ts->stopped = true;
246 mb();
247 wake_up(&ts->wait);
248 disable_irq(ts->spi->irq);
249 }
250}
251
252/* Must be called with ts->lock held */
253static void ads7846_restart(struct ads7846 *ts)
254{
255 if (!ts->disabled && !ts->suspended) {
256 /* Check if pen was released since last stop */
257 if (ts->pendown && !get_pendown_state(ts))
258 ads7846_report_pen_up(ts);
259
260 /* Tell IRQ thread that it may poll the device. */
261 ts->stopped = false;
262 mb();
263 enable_irq(ts->spi->irq);
264 }
265}
266
267/* Must be called with ts->lock held */
268static void __ads7846_disable(struct ads7846 *ts)
269{
270 ads7846_stop(ts);
271 regulator_disable(ts->reg);
272
273 /*
274 * We know the chip's in low power mode since we always
275 * leave it that way after every request
276 */
277}
278
279/* Must be called with ts->lock held */
280static void __ads7846_enable(struct ads7846 *ts)
281{
282 int error;
283
284 error = regulator_enable(ts->reg);
285 if (error != 0)
286 dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error);
287
288 ads7846_restart(ts);
289}
290
291static void ads7846_disable(struct ads7846 *ts)
292{
293 mutex_lock(&ts->lock);
294
295 if (!ts->disabled) {
296
297 if (!ts->suspended)
298 __ads7846_disable(ts);
299
300 ts->disabled = true;
301 }
302
303 mutex_unlock(&ts->lock);
304}
305
306static void ads7846_enable(struct ads7846 *ts)
307{
308 mutex_lock(&ts->lock);
309
310 if (ts->disabled) {
311
312 ts->disabled = false;
313
314 if (!ts->suspended)
315 __ads7846_enable(ts);
316 }
317
318 mutex_unlock(&ts->lock);
319}
320
321/*--------------------------------------------------------------------------*/
322
323/*
324 * Non-touchscreen sensors only use single-ended conversions.
325 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
326 * ads7846 lets that pin be unconnected, to use internal vREF.
327 */
328
329struct ser_req {
330 u8 ref_on;
331 u8 command;
332 u8 ref_off;
333 u16 scratch;
334 struct spi_message msg;
335 struct spi_transfer xfer[6];
336 /*
337 * DMA (thus cache coherency maintenance) requires the
338 * transfer buffers to live in their own cache lines.
339 */
340 __be16 sample ____cacheline_aligned;
341};
342
343struct ads7845_ser_req {
344 u8 command[3];
345 struct spi_message msg;
346 struct spi_transfer xfer[2];
347 /*
348 * DMA (thus cache coherency maintenance) requires the
349 * transfer buffers to live in their own cache lines.
350 */
351 u8 sample[3] ____cacheline_aligned;
352};
353
354static int ads7846_read12_ser(struct device *dev, unsigned command)
355{
356 struct spi_device *spi = to_spi_device(dev);
357 struct ads7846 *ts = dev_get_drvdata(dev);
358 struct ser_req *req;
359 int status;
360
361 req = kzalloc(sizeof *req, GFP_KERNEL);
362 if (!req)
363 return -ENOMEM;
364
365 spi_message_init(&req->msg);
366
367 /* maybe turn on internal vREF, and let it settle */
368 if (ts->use_internal) {
369 req->ref_on = REF_ON;
370 req->xfer[0].tx_buf = &req->ref_on;
371 req->xfer[0].len = 1;
372 spi_message_add_tail(&req->xfer[0], &req->msg);
373
374 req->xfer[1].rx_buf = &req->scratch;
375 req->xfer[1].len = 2;
376
377 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
378 req->xfer[1].delay.value = ts->vref_delay_usecs;
379 req->xfer[1].delay.unit = SPI_DELAY_UNIT_USECS;
380 spi_message_add_tail(&req->xfer[1], &req->msg);
381
382 /* Enable reference voltage */
383 command |= ADS_PD10_REF_ON;
384 }
385
386 /* Enable ADC in every case */
387 command |= ADS_PD10_ADC_ON;
388
389 /* take sample */
390 req->command = (u8) command;
391 req->xfer[2].tx_buf = &req->command;
392 req->xfer[2].len = 1;
393 spi_message_add_tail(&req->xfer[2], &req->msg);
394
395 req->xfer[3].rx_buf = &req->sample;
396 req->xfer[3].len = 2;
397 spi_message_add_tail(&req->xfer[3], &req->msg);
398
399 /* REVISIT: take a few more samples, and compare ... */
400
401 /* converter in low power mode & enable PENIRQ */
402 req->ref_off = PWRDOWN;
403 req->xfer[4].tx_buf = &req->ref_off;
404 req->xfer[4].len = 1;
405 spi_message_add_tail(&req->xfer[4], &req->msg);
406
407 req->xfer[5].rx_buf = &req->scratch;
408 req->xfer[5].len = 2;
409 CS_CHANGE(req->xfer[5]);
410 spi_message_add_tail(&req->xfer[5], &req->msg);
411
412 mutex_lock(&ts->lock);
413 ads7846_stop(ts);
414 status = spi_sync(spi, &req->msg);
415 ads7846_restart(ts);
416 mutex_unlock(&ts->lock);
417
418 if (status == 0) {
419 /* on-wire is a must-ignore bit, a BE12 value, then padding */
420 status = be16_to_cpu(req->sample);
421 status = status >> 3;
422 status &= 0x0fff;
423 }
424
425 kfree(req);
426 return status;
427}
428
429static int ads7845_read12_ser(struct device *dev, unsigned command)
430{
431 struct spi_device *spi = to_spi_device(dev);
432 struct ads7846 *ts = dev_get_drvdata(dev);
433 struct ads7845_ser_req *req;
434 int status;
435
436 req = kzalloc(sizeof *req, GFP_KERNEL);
437 if (!req)
438 return -ENOMEM;
439
440 spi_message_init(&req->msg);
441
442 req->command[0] = (u8) command;
443 req->xfer[0].tx_buf = req->command;
444 req->xfer[0].rx_buf = req->sample;
445 req->xfer[0].len = 3;
446 spi_message_add_tail(&req->xfer[0], &req->msg);
447
448 mutex_lock(&ts->lock);
449 ads7846_stop(ts);
450 status = spi_sync(spi, &req->msg);
451 ads7846_restart(ts);
452 mutex_unlock(&ts->lock);
453
454 if (status == 0) {
455 /* BE12 value, then padding */
456 status = get_unaligned_be16(&req->sample[1]);
457 status = status >> 3;
458 status &= 0x0fff;
459 }
460
461 kfree(req);
462 return status;
463}
464
465#if IS_ENABLED(CONFIG_HWMON)
466
467#define SHOW(name, var, adjust) static ssize_t \
468name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
469{ \
470 struct ads7846 *ts = dev_get_drvdata(dev); \
471 ssize_t v = ads7846_read12_ser(&ts->spi->dev, \
472 READ_12BIT_SER(var)); \
473 if (v < 0) \
474 return v; \
475 return sprintf(buf, "%u\n", adjust(ts, v)); \
476} \
477static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
478
479
480/* Sysfs conventions report temperatures in millidegrees Celsius.
481 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
482 * accuracy scheme without calibration data. For now we won't try either;
483 * userspace sees raw sensor values, and must scale/calibrate appropriately.
484 */
485static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
486{
487 return v;
488}
489
490SHOW(temp0, temp0, null_adjust) /* temp1_input */
491SHOW(temp1, temp1, null_adjust) /* temp2_input */
492
493
494/* sysfs conventions report voltages in millivolts. We can convert voltages
495 * if we know vREF. userspace may need to scale vAUX to match the board's
496 * external resistors; we assume that vBATT only uses the internal ones.
497 */
498static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
499{
500 unsigned retval = v;
501
502 /* external resistors may scale vAUX into 0..vREF */
503 retval *= ts->vref_mv;
504 retval = retval >> 12;
505
506 return retval;
507}
508
509static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
510{
511 unsigned retval = vaux_adjust(ts, v);
512
513 /* ads7846 has a resistor ladder to scale this signal down */
514 if (ts->model == 7846)
515 retval *= 4;
516
517 return retval;
518}
519
520SHOW(in0_input, vaux, vaux_adjust)
521SHOW(in1_input, vbatt, vbatt_adjust)
522
523static umode_t ads7846_is_visible(struct kobject *kobj, struct attribute *attr,
524 int index)
525{
526 struct device *dev = kobj_to_dev(kobj);
527 struct ads7846 *ts = dev_get_drvdata(dev);
528
529 if (ts->model == 7843 && index < 2) /* in0, in1 */
530 return 0;
531 if (ts->model == 7845 && index != 2) /* in0 */
532 return 0;
533
534 return attr->mode;
535}
536
537static struct attribute *ads7846_attributes[] = {
538 &dev_attr_temp0.attr, /* 0 */
539 &dev_attr_temp1.attr, /* 1 */
540 &dev_attr_in0_input.attr, /* 2 */
541 &dev_attr_in1_input.attr, /* 3 */
542 NULL,
543};
544
545static const struct attribute_group ads7846_attr_group = {
546 .attrs = ads7846_attributes,
547 .is_visible = ads7846_is_visible,
548};
549__ATTRIBUTE_GROUPS(ads7846_attr);
550
551static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
552{
553 struct device *hwmon;
554
555 /* hwmon sensors need a reference voltage */
556 switch (ts->model) {
557 case 7846:
558 if (!ts->vref_mv) {
559 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
560 ts->vref_mv = 2500;
561 ts->use_internal = true;
562 }
563 break;
564 case 7845:
565 case 7843:
566 if (!ts->vref_mv) {
567 dev_warn(&spi->dev,
568 "external vREF for ADS%d not specified\n",
569 ts->model);
570 return 0;
571 }
572 break;
573 }
574
575 hwmon = devm_hwmon_device_register_with_groups(&spi->dev,
576 spi->modalias, ts,
577 ads7846_attr_groups);
578
579 return PTR_ERR_OR_ZERO(hwmon);
580}
581
582#else
583static inline int ads784x_hwmon_register(struct spi_device *spi,
584 struct ads7846 *ts)
585{
586 return 0;
587}
588#endif
589
590static ssize_t ads7846_pen_down_show(struct device *dev,
591 struct device_attribute *attr, char *buf)
592{
593 struct ads7846 *ts = dev_get_drvdata(dev);
594
595 return sprintf(buf, "%u\n", ts->pendown);
596}
597
598static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
599
600static ssize_t ads7846_disable_show(struct device *dev,
601 struct device_attribute *attr, char *buf)
602{
603 struct ads7846 *ts = dev_get_drvdata(dev);
604
605 return sprintf(buf, "%u\n", ts->disabled);
606}
607
608static ssize_t ads7846_disable_store(struct device *dev,
609 struct device_attribute *attr,
610 const char *buf, size_t count)
611{
612 struct ads7846 *ts = dev_get_drvdata(dev);
613 unsigned int i;
614 int err;
615
616 err = kstrtouint(buf, 10, &i);
617 if (err)
618 return err;
619
620 if (i)
621 ads7846_disable(ts);
622 else
623 ads7846_enable(ts);
624
625 return count;
626}
627
628static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
629
630static struct attribute *ads784x_attributes[] = {
631 &dev_attr_pen_down.attr,
632 &dev_attr_disable.attr,
633 NULL,
634};
635
636static const struct attribute_group ads784x_attr_group = {
637 .attrs = ads784x_attributes,
638};
639
640/*--------------------------------------------------------------------------*/
641
642static void null_wait_for_sync(void)
643{
644}
645
646static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
647{
648 struct ads7846 *ts = ads;
649
650 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
651 /* Start over collecting consistent readings. */
652 ts->read_rep = 0;
653 /*
654 * Repeat it, if this was the first read or the read
655 * wasn't consistent enough.
656 */
657 if (ts->read_cnt < ts->debounce_max) {
658 ts->last_read = *val;
659 ts->read_cnt++;
660 return ADS7846_FILTER_REPEAT;
661 } else {
662 /*
663 * Maximum number of debouncing reached and still
664 * not enough number of consistent readings. Abort
665 * the whole sample, repeat it in the next sampling
666 * period.
667 */
668 ts->read_cnt = 0;
669 return ADS7846_FILTER_IGNORE;
670 }
671 } else {
672 if (++ts->read_rep > ts->debounce_rep) {
673 /*
674 * Got a good reading for this coordinate,
675 * go for the next one.
676 */
677 ts->read_cnt = 0;
678 ts->read_rep = 0;
679 return ADS7846_FILTER_OK;
680 } else {
681 /* Read more values that are consistent. */
682 ts->read_cnt++;
683 return ADS7846_FILTER_REPEAT;
684 }
685 }
686}
687
688static int ads7846_no_filter(void *ads, int data_idx, int *val)
689{
690 return ADS7846_FILTER_OK;
691}
692
693static int ads7846_get_value(struct ads7846_buf *buf)
694{
695 int value;
696
697 value = be16_to_cpup(&buf->data);
698
699 /* enforce ADC output is 12 bits width */
700 return (value >> 3) & 0xfff;
701}
702
703static void ads7846_set_cmd_val(struct ads7846 *ts, enum ads7846_cmds cmd_idx,
704 u16 val)
705{
706 struct ads7846_packet *packet = ts->packet;
707
708 switch (cmd_idx) {
709 case ADS7846_Y:
710 packet->y = val;
711 break;
712 case ADS7846_X:
713 packet->x = val;
714 break;
715 case ADS7846_Z1:
716 packet->z1 = val;
717 break;
718 case ADS7846_Z2:
719 packet->z2 = val;
720 break;
721 default:
722 WARN_ON_ONCE(1);
723 }
724}
725
726static u8 ads7846_get_cmd(enum ads7846_cmds cmd_idx, int vref)
727{
728 switch (cmd_idx) {
729 case ADS7846_Y:
730 return READ_Y(vref);
731 case ADS7846_X:
732 return READ_X(vref);
733
734 /* 7846 specific commands */
735 case ADS7846_Z1:
736 return READ_Z1(vref);
737 case ADS7846_Z2:
738 return READ_Z2(vref);
739 case ADS7846_PWDOWN:
740 return PWRDOWN;
741 default:
742 WARN_ON_ONCE(1);
743 }
744
745 return 0;
746}
747
748static bool ads7846_cmd_need_settle(enum ads7846_cmds cmd_idx)
749{
750 switch (cmd_idx) {
751 case ADS7846_X:
752 case ADS7846_Y:
753 case ADS7846_Z1:
754 case ADS7846_Z2:
755 return true;
756 case ADS7846_PWDOWN:
757 return false;
758 default:
759 WARN_ON_ONCE(1);
760 }
761
762 return false;
763}
764
765static int ads7846_filter(struct ads7846 *ts)
766{
767 struct ads7846_packet *packet = ts->packet;
768 int action;
769 int val;
770 unsigned int cmd_idx, b;
771
772 packet->ignore = false;
773 for (cmd_idx = packet->last_cmd_idx; cmd_idx < packet->cmds - 1; cmd_idx++) {
774 struct ads7846_buf_layout *l = &packet->l[cmd_idx];
775
776 packet->last_cmd_idx = cmd_idx;
777
778 for (b = l->skip; b < l->count; b++) {
779 val = ads7846_get_value(&packet->rx[l->offset + b]);
780
781 action = ts->filter(ts->filter_data, cmd_idx, &val);
782 if (action == ADS7846_FILTER_REPEAT) {
783 if (b == l->count - 1)
784 return -EAGAIN;
785 } else if (action == ADS7846_FILTER_OK) {
786 ads7846_set_cmd_val(ts, cmd_idx, val);
787 break;
788 } else {
789 packet->ignore = true;
790 return 0;
791 }
792 }
793 }
794
795 return 0;
796}
797
798static void ads7846_read_state(struct ads7846 *ts)
799{
800 struct ads7846_packet *packet = ts->packet;
801 struct spi_message *m;
802 int msg_idx = 0;
803 int error;
804
805 packet->last_cmd_idx = 0;
806
807 while (true) {
808 ts->wait_for_sync();
809
810 m = &ts->msg[msg_idx];
811 error = spi_sync(ts->spi, m);
812 if (error) {
813 dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
814 packet->ignore = true;
815 return;
816 }
817
818 error = ads7846_filter(ts);
819 if (error)
820 continue;
821
822 return;
823 }
824}
825
826static void ads7846_report_state(struct ads7846 *ts)
827{
828 struct ads7846_packet *packet = ts->packet;
829 unsigned int Rt;
830 u16 x, y, z1, z2;
831
832 x = packet->x;
833 y = packet->y;
834 if (ts->model == 7845) {
835 z1 = 0;
836 z2 = 0;
837 } else {
838 z1 = packet->z1;
839 z2 = packet->z2;
840 }
841
842 /* range filtering */
843 if (x == MAX_12BIT)
844 x = 0;
845
846 if (ts->model == 7843) {
847 Rt = ts->pressure_max / 2;
848 } else if (ts->model == 7845) {
849 if (get_pendown_state(ts))
850 Rt = ts->pressure_max / 2;
851 else
852 Rt = 0;
853 dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
854 } else if (likely(x && z1)) {
855 /* compute touch pressure resistance using equation #2 */
856 Rt = z2;
857 Rt -= z1;
858 Rt *= ts->x_plate_ohms;
859 Rt = DIV_ROUND_CLOSEST(Rt, 16);
860 Rt *= x;
861 Rt /= z1;
862 Rt = DIV_ROUND_CLOSEST(Rt, 256);
863 } else {
864 Rt = 0;
865 }
866
867 /*
868 * Sample found inconsistent by debouncing or pressure is beyond
869 * the maximum. Don't report it to user space, repeat at least
870 * once more the measurement
871 */
872 if (packet->ignore || Rt > ts->pressure_max) {
873 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
874 packet->ignore, Rt);
875 return;
876 }
877
878 /*
879 * Maybe check the pendown state before reporting. This discards
880 * false readings when the pen is lifted.
881 */
882 if (ts->penirq_recheck_delay_usecs) {
883 udelay(ts->penirq_recheck_delay_usecs);
884 if (!get_pendown_state(ts))
885 Rt = 0;
886 }
887
888 /*
889 * NOTE: We can't rely on the pressure to determine the pen down
890 * state, even this controller has a pressure sensor. The pressure
891 * value can fluctuate for quite a while after lifting the pen and
892 * in some cases may not even settle at the expected value.
893 *
894 * The only safe way to check for the pen up condition is in the
895 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
896 */
897 if (Rt) {
898 struct input_dev *input = ts->input;
899
900 if (!ts->pendown) {
901 input_report_key(input, BTN_TOUCH, 1);
902 ts->pendown = true;
903 dev_vdbg(&ts->spi->dev, "DOWN\n");
904 }
905
906 touchscreen_report_pos(input, &ts->core_prop, x, y, false);
907 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
908
909 input_sync(input);
910 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
911 }
912}
913
914static irqreturn_t ads7846_hard_irq(int irq, void *handle)
915{
916 struct ads7846 *ts = handle;
917
918 return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
919}
920
921
922static irqreturn_t ads7846_irq(int irq, void *handle)
923{
924 struct ads7846 *ts = handle;
925
926 /* Start with a small delay before checking pendown state */
927 msleep(TS_POLL_DELAY);
928
929 while (!ts->stopped && get_pendown_state(ts)) {
930
931 /* pen is down, continue with the measurement */
932 ads7846_read_state(ts);
933
934 if (!ts->stopped)
935 ads7846_report_state(ts);
936
937 wait_event_timeout(ts->wait, ts->stopped,
938 msecs_to_jiffies(TS_POLL_PERIOD));
939 }
940
941 if (ts->pendown && !ts->stopped)
942 ads7846_report_pen_up(ts);
943
944 return IRQ_HANDLED;
945}
946
947static int __maybe_unused ads7846_suspend(struct device *dev)
948{
949 struct ads7846 *ts = dev_get_drvdata(dev);
950
951 mutex_lock(&ts->lock);
952
953 if (!ts->suspended) {
954
955 if (!ts->disabled)
956 __ads7846_disable(ts);
957
958 if (device_may_wakeup(&ts->spi->dev))
959 enable_irq_wake(ts->spi->irq);
960
961 ts->suspended = true;
962 }
963
964 mutex_unlock(&ts->lock);
965
966 return 0;
967}
968
969static int __maybe_unused ads7846_resume(struct device *dev)
970{
971 struct ads7846 *ts = dev_get_drvdata(dev);
972
973 mutex_lock(&ts->lock);
974
975 if (ts->suspended) {
976
977 ts->suspended = false;
978
979 if (device_may_wakeup(&ts->spi->dev))
980 disable_irq_wake(ts->spi->irq);
981
982 if (!ts->disabled)
983 __ads7846_enable(ts);
984 }
985
986 mutex_unlock(&ts->lock);
987
988 return 0;
989}
990
991static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
992
993static int ads7846_setup_pendown(struct spi_device *spi,
994 struct ads7846 *ts,
995 const struct ads7846_platform_data *pdata)
996{
997 int err;
998
999 /*
1000 * REVISIT when the irq can be triggered active-low, or if for some
1001 * reason the touchscreen isn't hooked up, we don't need to access
1002 * the pendown state.
1003 */
1004
1005 if (pdata->get_pendown_state) {
1006 ts->get_pendown_state = pdata->get_pendown_state;
1007 } else if (gpio_is_valid(pdata->gpio_pendown)) {
1008
1009 err = devm_gpio_request_one(&spi->dev, pdata->gpio_pendown,
1010 GPIOF_IN, "ads7846_pendown");
1011 if (err) {
1012 dev_err(&spi->dev,
1013 "failed to request/setup pendown GPIO%d: %d\n",
1014 pdata->gpio_pendown, err);
1015 return err;
1016 }
1017
1018 ts->gpio_pendown = pdata->gpio_pendown;
1019
1020 if (pdata->gpio_pendown_debounce)
1021 gpio_set_debounce(pdata->gpio_pendown,
1022 pdata->gpio_pendown_debounce);
1023 } else {
1024 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
1025 return -EINVAL;
1026 }
1027
1028 return 0;
1029}
1030
1031/*
1032 * Set up the transfers to read touchscreen state; this assumes we
1033 * use formula #2 for pressure, not #3.
1034 */
1035static int ads7846_setup_spi_msg(struct ads7846 *ts,
1036 const struct ads7846_platform_data *pdata)
1037{
1038 struct spi_message *m = &ts->msg[0];
1039 struct spi_transfer *x = ts->xfer;
1040 struct ads7846_packet *packet = ts->packet;
1041 int vref = pdata->keep_vref_on;
1042 unsigned int count, offset = 0;
1043 unsigned int cmd_idx, b;
1044 unsigned long time;
1045 size_t size = 0;
1046
1047 /* time per bit */
1048 time = NSEC_PER_SEC / ts->spi->max_speed_hz;
1049
1050 count = pdata->settle_delay_usecs * NSEC_PER_USEC / time;
1051 packet->count_skip = DIV_ROUND_UP(count, 24);
1052
1053 if (ts->debounce_max && ts->debounce_rep)
1054 /* ads7846_debounce_filter() is making ts->debounce_rep + 2
1055 * reads. So we need to get all samples for normal case. */
1056 packet->count = ts->debounce_rep + 2;
1057 else
1058 packet->count = 1;
1059
1060 if (ts->model == 7846)
1061 packet->cmds = 5; /* x, y, z1, z2, pwdown */
1062 else
1063 packet->cmds = 3; /* x, y, pwdown */
1064
1065 for (cmd_idx = 0; cmd_idx < packet->cmds; cmd_idx++) {
1066 struct ads7846_buf_layout *l = &packet->l[cmd_idx];
1067 unsigned int max_count;
1068
1069 if (ads7846_cmd_need_settle(cmd_idx))
1070 max_count = packet->count + packet->count_skip;
1071 else
1072 max_count = packet->count;
1073
1074 l->offset = offset;
1075 offset += max_count;
1076 l->count = max_count;
1077 l->skip = packet->count_skip;
1078 size += sizeof(*packet->tx) * max_count;
1079 }
1080
1081 packet->tx = devm_kzalloc(&ts->spi->dev, size, GFP_KERNEL);
1082 if (!packet->tx)
1083 return -ENOMEM;
1084
1085 packet->rx = devm_kzalloc(&ts->spi->dev, size, GFP_KERNEL);
1086 if (!packet->rx)
1087 return -ENOMEM;
1088
1089 if (ts->model == 7873) {
1090 /*
1091 * The AD7873 is almost identical to the ADS7846
1092 * keep VREF off during differential/ratiometric
1093 * conversion modes.
1094 */
1095 ts->model = 7846;
1096 vref = 0;
1097 }
1098
1099 ts->msg_count = 1;
1100 spi_message_init(m);
1101 m->context = ts;
1102
1103 for (cmd_idx = 0; cmd_idx < packet->cmds; cmd_idx++) {
1104 struct ads7846_buf_layout *l = &packet->l[cmd_idx];
1105 u8 cmd = ads7846_get_cmd(cmd_idx, vref);
1106
1107 for (b = 0; b < l->count; b++)
1108 packet->tx[l->offset + b].cmd = cmd;
1109 }
1110
1111 x->tx_buf = packet->tx;
1112 x->rx_buf = packet->rx;
1113 x->len = size;
1114 spi_message_add_tail(x, m);
1115
1116 return 0;
1117}
1118
1119#ifdef CONFIG_OF
1120static const struct of_device_id ads7846_dt_ids[] = {
1121 { .compatible = "ti,tsc2046", .data = (void *) 7846 },
1122 { .compatible = "ti,ads7843", .data = (void *) 7843 },
1123 { .compatible = "ti,ads7845", .data = (void *) 7845 },
1124 { .compatible = "ti,ads7846", .data = (void *) 7846 },
1125 { .compatible = "ti,ads7873", .data = (void *) 7873 },
1126 { }
1127};
1128MODULE_DEVICE_TABLE(of, ads7846_dt_ids);
1129
1130static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1131{
1132 struct ads7846_platform_data *pdata;
1133 struct device_node *node = dev->of_node;
1134 const struct of_device_id *match;
1135 u32 value;
1136
1137 if (!node) {
1138 dev_err(dev, "Device does not have associated DT data\n");
1139 return ERR_PTR(-EINVAL);
1140 }
1141
1142 match = of_match_device(ads7846_dt_ids, dev);
1143 if (!match) {
1144 dev_err(dev, "Unknown device model\n");
1145 return ERR_PTR(-EINVAL);
1146 }
1147
1148 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1149 if (!pdata)
1150 return ERR_PTR(-ENOMEM);
1151
1152 pdata->model = (unsigned long)match->data;
1153
1154 of_property_read_u16(node, "ti,vref-delay-usecs",
1155 &pdata->vref_delay_usecs);
1156 of_property_read_u16(node, "ti,vref-mv", &pdata->vref_mv);
1157 pdata->keep_vref_on = of_property_read_bool(node, "ti,keep-vref-on");
1158
1159 pdata->swap_xy = of_property_read_bool(node, "ti,swap-xy");
1160
1161 of_property_read_u16(node, "ti,settle-delay-usec",
1162 &pdata->settle_delay_usecs);
1163 of_property_read_u16(node, "ti,penirq-recheck-delay-usecs",
1164 &pdata->penirq_recheck_delay_usecs);
1165
1166 of_property_read_u16(node, "ti,x-plate-ohms", &pdata->x_plate_ohms);
1167 of_property_read_u16(node, "ti,y-plate-ohms", &pdata->y_plate_ohms);
1168
1169 of_property_read_u16(node, "ti,x-min", &pdata->x_min);
1170 of_property_read_u16(node, "ti,y-min", &pdata->y_min);
1171 of_property_read_u16(node, "ti,x-max", &pdata->x_max);
1172 of_property_read_u16(node, "ti,y-max", &pdata->y_max);
1173
1174 /*
1175 * touchscreen-max-pressure gets parsed during
1176 * touchscreen_parse_properties()
1177 */
1178 of_property_read_u16(node, "ti,pressure-min", &pdata->pressure_min);
1179 if (!of_property_read_u32(node, "touchscreen-min-pressure", &value))
1180 pdata->pressure_min = (u16) value;
1181 of_property_read_u16(node, "ti,pressure-max", &pdata->pressure_max);
1182
1183 of_property_read_u16(node, "ti,debounce-max", &pdata->debounce_max);
1184 if (!of_property_read_u32(node, "touchscreen-average-samples", &value))
1185 pdata->debounce_max = (u16) value;
1186 of_property_read_u16(node, "ti,debounce-tol", &pdata->debounce_tol);
1187 of_property_read_u16(node, "ti,debounce-rep", &pdata->debounce_rep);
1188
1189 of_property_read_u32(node, "ti,pendown-gpio-debounce",
1190 &pdata->gpio_pendown_debounce);
1191
1192 pdata->wakeup = of_property_read_bool(node, "wakeup-source") ||
1193 of_property_read_bool(node, "linux,wakeup");
1194
1195 pdata->gpio_pendown = of_get_named_gpio(dev->of_node, "pendown-gpio", 0);
1196
1197 return pdata;
1198}
1199#else
1200static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1201{
1202 dev_err(dev, "no platform data defined\n");
1203 return ERR_PTR(-EINVAL);
1204}
1205#endif
1206
1207static void ads7846_regulator_disable(void *regulator)
1208{
1209 regulator_disable(regulator);
1210}
1211
1212static int ads7846_probe(struct spi_device *spi)
1213{
1214 const struct ads7846_platform_data *pdata;
1215 struct ads7846 *ts;
1216 struct device *dev = &spi->dev;
1217 struct ads7846_packet *packet;
1218 struct input_dev *input_dev;
1219 unsigned long irq_flags;
1220 int err;
1221
1222 if (!spi->irq) {
1223 dev_dbg(dev, "no IRQ?\n");
1224 return -EINVAL;
1225 }
1226
1227 /* don't exceed max specified sample rate */
1228 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
1229 dev_err(dev, "f(sample) %d KHz?\n",
1230 (spi->max_speed_hz/SAMPLE_BITS)/1000);
1231 return -EINVAL;
1232 }
1233
1234 /*
1235 * We'd set TX word size 8 bits and RX word size to 13 bits ... except
1236 * that even if the hardware can do that, the SPI controller driver
1237 * may not. So we stick to very-portable 8 bit words, both RX and TX.
1238 */
1239 spi->bits_per_word = 8;
1240 spi->mode &= ~SPI_MODE_X_MASK;
1241 spi->mode |= SPI_MODE_0;
1242 err = spi_setup(spi);
1243 if (err < 0)
1244 return err;
1245
1246 ts = devm_kzalloc(dev, sizeof(struct ads7846), GFP_KERNEL);
1247 if (!ts)
1248 return -ENOMEM;
1249
1250 packet = devm_kzalloc(dev, sizeof(struct ads7846_packet), GFP_KERNEL);
1251 if (!packet)
1252 return -ENOMEM;
1253
1254 input_dev = devm_input_allocate_device(dev);
1255 if (!input_dev)
1256 return -ENOMEM;
1257
1258 spi_set_drvdata(spi, ts);
1259
1260 ts->packet = packet;
1261 ts->spi = spi;
1262 ts->input = input_dev;
1263
1264 mutex_init(&ts->lock);
1265 init_waitqueue_head(&ts->wait);
1266
1267 pdata = dev_get_platdata(dev);
1268 if (!pdata) {
1269 pdata = ads7846_probe_dt(dev);
1270 if (IS_ERR(pdata))
1271 return PTR_ERR(pdata);
1272 }
1273
1274 ts->model = pdata->model ? : 7846;
1275 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
1276 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
1277 ts->vref_mv = pdata->vref_mv;
1278
1279 if (pdata->debounce_max) {
1280 ts->debounce_max = pdata->debounce_max;
1281 if (ts->debounce_max < 2)
1282 ts->debounce_max = 2;
1283 ts->debounce_tol = pdata->debounce_tol;
1284 ts->debounce_rep = pdata->debounce_rep;
1285 ts->filter = ads7846_debounce_filter;
1286 ts->filter_data = ts;
1287 } else {
1288 ts->filter = ads7846_no_filter;
1289 }
1290
1291 err = ads7846_setup_pendown(spi, ts, pdata);
1292 if (err)
1293 return err;
1294
1295 if (pdata->penirq_recheck_delay_usecs)
1296 ts->penirq_recheck_delay_usecs =
1297 pdata->penirq_recheck_delay_usecs;
1298
1299 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1300
1301 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
1302 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
1303
1304 input_dev->name = ts->name;
1305 input_dev->phys = ts->phys;
1306
1307 input_dev->id.bustype = BUS_SPI;
1308 input_dev->id.product = pdata->model;
1309
1310 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
1311 input_set_abs_params(input_dev, ABS_X,
1312 pdata->x_min ? : 0,
1313 pdata->x_max ? : MAX_12BIT,
1314 0, 0);
1315 input_set_abs_params(input_dev, ABS_Y,
1316 pdata->y_min ? : 0,
1317 pdata->y_max ? : MAX_12BIT,
1318 0, 0);
1319 input_set_abs_params(input_dev, ABS_PRESSURE,
1320 pdata->pressure_min, pdata->pressure_max, 0, 0);
1321
1322 /*
1323 * Parse common framework properties. Must be done here to ensure the
1324 * correct behaviour in case of using the legacy vendor bindings. The
1325 * general binding value overrides the vendor specific one.
1326 */
1327 touchscreen_parse_properties(ts->input, false, &ts->core_prop);
1328 ts->pressure_max = input_abs_get_max(input_dev, ABS_PRESSURE) ? : ~0;
1329
1330 /*
1331 * Check if legacy ti,swap-xy binding is used instead of
1332 * touchscreen-swapped-x-y
1333 */
1334 if (!ts->core_prop.swap_x_y && pdata->swap_xy) {
1335 swap(input_dev->absinfo[ABS_X], input_dev->absinfo[ABS_Y]);
1336 ts->core_prop.swap_x_y = true;
1337 }
1338
1339 ads7846_setup_spi_msg(ts, pdata);
1340
1341 ts->reg = devm_regulator_get(dev, "vcc");
1342 if (IS_ERR(ts->reg)) {
1343 err = PTR_ERR(ts->reg);
1344 dev_err(dev, "unable to get regulator: %d\n", err);
1345 return err;
1346 }
1347
1348 err = regulator_enable(ts->reg);
1349 if (err) {
1350 dev_err(dev, "unable to enable regulator: %d\n", err);
1351 return err;
1352 }
1353
1354 err = devm_add_action_or_reset(dev, ads7846_regulator_disable, ts->reg);
1355 if (err)
1356 return err;
1357
1358 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
1359 irq_flags |= IRQF_ONESHOT;
1360
1361 err = devm_request_threaded_irq(dev, spi->irq,
1362 ads7846_hard_irq, ads7846_irq,
1363 irq_flags, dev->driver->name, ts);
1364 if (err && err != -EPROBE_DEFER && !pdata->irq_flags) {
1365 dev_info(dev,
1366 "trying pin change workaround on irq %d\n", spi->irq);
1367 irq_flags |= IRQF_TRIGGER_RISING;
1368 err = devm_request_threaded_irq(dev, spi->irq,
1369 ads7846_hard_irq, ads7846_irq,
1370 irq_flags, dev->driver->name,
1371 ts);
1372 }
1373
1374 if (err) {
1375 dev_dbg(dev, "irq %d busy?\n", spi->irq);
1376 return err;
1377 }
1378
1379 err = ads784x_hwmon_register(spi, ts);
1380 if (err)
1381 return err;
1382
1383 dev_info(dev, "touchscreen, irq %d\n", spi->irq);
1384
1385 /*
1386 * Take a first sample, leaving nPENIRQ active and vREF off; avoid
1387 * the touchscreen, in case it's not connected.
1388 */
1389 if (ts->model == 7845)
1390 ads7845_read12_ser(dev, PWRDOWN);
1391 else
1392 (void) ads7846_read12_ser(dev, READ_12BIT_SER(vaux));
1393
1394 err = devm_device_add_group(dev, &ads784x_attr_group);
1395 if (err)
1396 return err;
1397
1398 err = input_register_device(input_dev);
1399 if (err)
1400 return err;
1401
1402 device_init_wakeup(dev, pdata->wakeup);
1403
1404 /*
1405 * If device does not carry platform data we must have allocated it
1406 * when parsing DT data.
1407 */
1408 if (!dev_get_platdata(dev))
1409 devm_kfree(dev, (void *)pdata);
1410
1411 return 0;
1412}
1413
1414static void ads7846_remove(struct spi_device *spi)
1415{
1416 struct ads7846 *ts = spi_get_drvdata(spi);
1417
1418 ads7846_stop(ts);
1419}
1420
1421static struct spi_driver ads7846_driver = {
1422 .driver = {
1423 .name = "ads7846",
1424 .pm = &ads7846_pm,
1425 .of_match_table = of_match_ptr(ads7846_dt_ids),
1426 },
1427 .probe = ads7846_probe,
1428 .remove = ads7846_remove,
1429};
1430
1431module_spi_driver(ads7846_driver);
1432
1433MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1434MODULE_LICENSE("GPL");
1435MODULE_ALIAS("spi:ads7846");