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/irq.h>
36 #include <asm/unaligned.h>
37 
38 /*
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.
45  *
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.
51  *
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
57  * files.
58  */
59 
60 #define TS_POLL_DELAY	1	/* ms delay before the first sample */
61 #define TS_POLL_PERIOD	5	/* ms delay between samples */
62 
63 /* this driver doesn't aim at the peak continuous sample rate */
64 #define	SAMPLE_BITS	(8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
65 
66 struct ts_event {
67 	/*
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.
72 	 */
73 	u16	x;
74 	u16	y;
75 	u16	z1, z2;
76 	bool	ignore;
77 	u8	x_buf[3];
78 	u8	y_buf[3];
79 };
80 
81 /*
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).
85  */
86 struct ads7846_packet {
87 	u8			read_x, read_y, read_z1, read_z2, pwrdown;
88 	u16			dummy;		/* for the pwrdown read */
89 	struct ts_event		tc;
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];
92 };
93 
94 struct 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 #if IS_ENABLED(CONFIG_HWMON)
103 	struct device		*hwmon;
104 #endif
105 
106 	u16			model;
107 	u16			vref_mv;
108 	u16			vref_delay_usecs;
109 	u16			x_plate_ohms;
110 	u16			pressure_max;
111 
112 	bool			swap_xy;
113 	bool			use_internal;
114 
115 	struct ads7846_packet	*packet;
116 
117 	struct spi_transfer	xfer[18];
118 	struct spi_message	msg[5];
119 	int			msg_count;
120 	wait_queue_head_t	wait;
121 
122 	bool			pendown;
123 
124 	int			read_cnt;
125 	int			read_rep;
126 	int			last_read;
127 
128 	u16			debounce_max;
129 	u16			debounce_tol;
130 	u16			debounce_rep;
131 
132 	u16			penirq_recheck_delay_usecs;
133 
134 	struct touchscreen_properties core_prop;
135 
136 	struct mutex		lock;
137 	bool			stopped;	/* P: lock */
138 	bool			disabled;	/* P: lock */
139 	bool			suspended;	/* P: lock */
140 
141 	int			(*filter)(void *data, int data_idx, int *val);
142 	void			*filter_data;
143 	void			(*filter_cleanup)(void *data);
144 	int			(*get_pendown_state)(void);
145 	int			gpio_pendown;
146 
147 	void			(*wait_for_sync)(void);
148 };
149 
150 /* leave chip selected when we're done, for quicker re-select? */
151 #if	0
152 #define	CS_CHANGE(xfer)	((xfer).cs_change = 1)
153 #else
154 #define	CS_CHANGE(xfer)	((xfer).cs_change = 0)
155 #endif
156 
157 /*--------------------------------------------------------------------------*/
158 
159 /* The ADS7846 has touchscreen and other sensors.
160  * Earlier ads784x chips are somewhat compatible.
161  */
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 */
179 
180 #define	MAX_12BIT	((1<<12)-1)
181 
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))
186 
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))
190 
191 #define	READ_X(vref)	(READ_12BIT_DFR(x,  1, vref))
192 #define	PWRDOWN		(READ_12BIT_DFR(y,  0, 0))	/* LAST */
193 
194 /* single-ended samples need to first power up reference voltage;
195  * we leave both ADC and VREF powered
196  */
197 #define	READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
198 	| ADS_12_BIT | ADS_SER)
199 
200 #define	REF_ON	(READ_12BIT_DFR(x, 1, 1))
201 #define	REF_OFF	(READ_12BIT_DFR(y, 0, 0))
202 
203 static int get_pendown_state(struct ads7846 *ts)
204 {
205 	if (ts->get_pendown_state)
206 		return ts->get_pendown_state();
207 
208 	return !gpio_get_value(ts->gpio_pendown);
209 }
210 
211 static void ads7846_report_pen_up(struct ads7846 *ts)
212 {
213 	struct input_dev *input = ts->input;
214 
215 	input_report_key(input, BTN_TOUCH, 0);
216 	input_report_abs(input, ABS_PRESSURE, 0);
217 	input_sync(input);
218 
219 	ts->pendown = false;
220 	dev_vdbg(&ts->spi->dev, "UP\n");
221 }
222 
223 /* Must be called with ts->lock held */
224 static void ads7846_stop(struct ads7846 *ts)
225 {
226 	if (!ts->disabled && !ts->suspended) {
227 		/* Signal IRQ thread to stop polling and disable the handler. */
228 		ts->stopped = true;
229 		mb();
230 		wake_up(&ts->wait);
231 		disable_irq(ts->spi->irq);
232 	}
233 }
234 
235 /* Must be called with ts->lock held */
236 static void ads7846_restart(struct ads7846 *ts)
237 {
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);
242 
243 		/* Tell IRQ thread that it may poll the device. */
244 		ts->stopped = false;
245 		mb();
246 		enable_irq(ts->spi->irq);
247 	}
248 }
249 
250 /* Must be called with ts->lock held */
251 static void __ads7846_disable(struct ads7846 *ts)
252 {
253 	ads7846_stop(ts);
254 	regulator_disable(ts->reg);
255 
256 	/*
257 	 * We know the chip's in low power mode since we always
258 	 * leave it that way after every request
259 	 */
260 }
261 
262 /* Must be called with ts->lock held */
263 static void __ads7846_enable(struct ads7846 *ts)
264 {
265 	int error;
266 
267 	error = regulator_enable(ts->reg);
268 	if (error != 0)
269 		dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error);
270 
271 	ads7846_restart(ts);
272 }
273 
274 static void ads7846_disable(struct ads7846 *ts)
275 {
276 	mutex_lock(&ts->lock);
277 
278 	if (!ts->disabled) {
279 
280 		if  (!ts->suspended)
281 			__ads7846_disable(ts);
282 
283 		ts->disabled = true;
284 	}
285 
286 	mutex_unlock(&ts->lock);
287 }
288 
289 static void ads7846_enable(struct ads7846 *ts)
290 {
291 	mutex_lock(&ts->lock);
292 
293 	if (ts->disabled) {
294 
295 		ts->disabled = false;
296 
297 		if (!ts->suspended)
298 			__ads7846_enable(ts);
299 	}
300 
301 	mutex_unlock(&ts->lock);
302 }
303 
304 /*--------------------------------------------------------------------------*/
305 
306 /*
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.
310  */
311 
312 struct ser_req {
313 	u8			ref_on;
314 	u8			command;
315 	u8			ref_off;
316 	u16			scratch;
317 	struct spi_message	msg;
318 	struct spi_transfer	xfer[6];
319 	/*
320 	 * DMA (thus cache coherency maintenance) requires the
321 	 * transfer buffers to live in their own cache lines.
322 	 */
323 	__be16 sample ____cacheline_aligned;
324 };
325 
326 struct ads7845_ser_req {
327 	u8			command[3];
328 	struct spi_message	msg;
329 	struct spi_transfer	xfer[2];
330 	/*
331 	 * DMA (thus cache coherency maintenance) requires the
332 	 * transfer buffers to live in their own cache lines.
333 	 */
334 	u8 sample[3] ____cacheline_aligned;
335 };
336 
337 static int ads7846_read12_ser(struct device *dev, unsigned command)
338 {
339 	struct spi_device *spi = to_spi_device(dev);
340 	struct ads7846 *ts = dev_get_drvdata(dev);
341 	struct ser_req *req;
342 	int status;
343 
344 	req = kzalloc(sizeof *req, GFP_KERNEL);
345 	if (!req)
346 		return -ENOMEM;
347 
348 	spi_message_init(&req->msg);
349 
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);
356 
357 		req->xfer[1].rx_buf = &req->scratch;
358 		req->xfer[1].len = 2;
359 
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);
364 
365 		/* Enable reference voltage */
366 		command |= ADS_PD10_REF_ON;
367 	}
368 
369 	/* Enable ADC in every case */
370 	command |= ADS_PD10_ADC_ON;
371 
372 	/* take sample */
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);
377 
378 	req->xfer[3].rx_buf = &req->sample;
379 	req->xfer[3].len = 2;
380 	spi_message_add_tail(&req->xfer[3], &req->msg);
381 
382 	/* REVISIT:  take a few more samples, and compare ... */
383 
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);
389 
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);
394 
395 	mutex_lock(&ts->lock);
396 	ads7846_stop(ts);
397 	status = spi_sync(spi, &req->msg);
398 	ads7846_restart(ts);
399 	mutex_unlock(&ts->lock);
400 
401 	if (status == 0) {
402 		/* on-wire is a must-ignore bit, a BE12 value, then padding */
403 		status = be16_to_cpu(req->sample);
404 		status = status >> 3;
405 		status &= 0x0fff;
406 	}
407 
408 	kfree(req);
409 	return status;
410 }
411 
412 static int ads7845_read12_ser(struct device *dev, unsigned command)
413 {
414 	struct spi_device *spi = to_spi_device(dev);
415 	struct ads7846 *ts = dev_get_drvdata(dev);
416 	struct ads7845_ser_req *req;
417 	int status;
418 
419 	req = kzalloc(sizeof *req, GFP_KERNEL);
420 	if (!req)
421 		return -ENOMEM;
422 
423 	spi_message_init(&req->msg);
424 
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);
430 
431 	mutex_lock(&ts->lock);
432 	ads7846_stop(ts);
433 	status = spi_sync(spi, &req->msg);
434 	ads7846_restart(ts);
435 	mutex_unlock(&ts->lock);
436 
437 	if (status == 0) {
438 		/* BE12 value, then padding */
439 		status = get_unaligned_be16(&req->sample[1]);
440 		status = status >> 3;
441 		status &= 0x0fff;
442 	}
443 
444 	kfree(req);
445 	return status;
446 }
447 
448 #if IS_ENABLED(CONFIG_HWMON)
449 
450 #define SHOW(name, var, adjust) static ssize_t \
451 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
452 { \
453 	struct ads7846 *ts = dev_get_drvdata(dev); \
454 	ssize_t v = ads7846_read12_ser(&ts->spi->dev, \
455 			READ_12BIT_SER(var)); \
456 	if (v < 0) \
457 		return v; \
458 	return sprintf(buf, "%u\n", adjust(ts, v)); \
459 } \
460 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
461 
462 
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.
467  */
468 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
469 {
470 	return v;
471 }
472 
473 SHOW(temp0, temp0, null_adjust)		/* temp1_input */
474 SHOW(temp1, temp1, null_adjust)		/* temp2_input */
475 
476 
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.
480  */
481 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
482 {
483 	unsigned retval = v;
484 
485 	/* external resistors may scale vAUX into 0..vREF */
486 	retval *= ts->vref_mv;
487 	retval = retval >> 12;
488 
489 	return retval;
490 }
491 
492 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
493 {
494 	unsigned retval = vaux_adjust(ts, v);
495 
496 	/* ads7846 has a resistor ladder to scale this signal down */
497 	if (ts->model == 7846)
498 		retval *= 4;
499 
500 	return retval;
501 }
502 
503 SHOW(in0_input, vaux, vaux_adjust)
504 SHOW(in1_input, vbatt, vbatt_adjust)
505 
506 static umode_t ads7846_is_visible(struct kobject *kobj, struct attribute *attr,
507 				  int index)
508 {
509 	struct device *dev = container_of(kobj, struct device, kobj);
510 	struct ads7846 *ts = dev_get_drvdata(dev);
511 
512 	if (ts->model == 7843 && index < 2)	/* in0, in1 */
513 		return 0;
514 	if (ts->model == 7845 && index != 2)	/* in0 */
515 		return 0;
516 
517 	return attr->mode;
518 }
519 
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 */
525 	NULL,
526 };
527 
528 static const struct attribute_group ads7846_attr_group = {
529 	.attrs = ads7846_attributes,
530 	.is_visible = ads7846_is_visible,
531 };
532 __ATTRIBUTE_GROUPS(ads7846_attr);
533 
534 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
535 {
536 	/* hwmon sensors need a reference voltage */
537 	switch (ts->model) {
538 	case 7846:
539 		if (!ts->vref_mv) {
540 			dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
541 			ts->vref_mv = 2500;
542 			ts->use_internal = true;
543 		}
544 		break;
545 	case 7845:
546 	case 7843:
547 		if (!ts->vref_mv) {
548 			dev_warn(&spi->dev,
549 				"external vREF for ADS%d not specified\n",
550 				ts->model);
551 			return 0;
552 		}
553 		break;
554 	}
555 
556 	ts->hwmon = hwmon_device_register_with_groups(&spi->dev, spi->modalias,
557 						      ts, ads7846_attr_groups);
558 
559 	return PTR_ERR_OR_ZERO(ts->hwmon);
560 }
561 
562 static void ads784x_hwmon_unregister(struct spi_device *spi,
563 				     struct ads7846 *ts)
564 {
565 	if (ts->hwmon)
566 		hwmon_device_unregister(ts->hwmon);
567 }
568 
569 #else
570 static inline int ads784x_hwmon_register(struct spi_device *spi,
571 					 struct ads7846 *ts)
572 {
573 	return 0;
574 }
575 
576 static inline void ads784x_hwmon_unregister(struct spi_device *spi,
577 					    struct ads7846 *ts)
578 {
579 }
580 #endif
581 
582 static ssize_t ads7846_pen_down_show(struct device *dev,
583 				     struct device_attribute *attr, char *buf)
584 {
585 	struct ads7846 *ts = dev_get_drvdata(dev);
586 
587 	return sprintf(buf, "%u\n", ts->pendown);
588 }
589 
590 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
591 
592 static ssize_t ads7846_disable_show(struct device *dev,
593 				     struct device_attribute *attr, char *buf)
594 {
595 	struct ads7846 *ts = dev_get_drvdata(dev);
596 
597 	return sprintf(buf, "%u\n", ts->disabled);
598 }
599 
600 static ssize_t ads7846_disable_store(struct device *dev,
601 				     struct device_attribute *attr,
602 				     const char *buf, size_t count)
603 {
604 	struct ads7846 *ts = dev_get_drvdata(dev);
605 	unsigned int i;
606 	int err;
607 
608 	err = kstrtouint(buf, 10, &i);
609 	if (err)
610 		return err;
611 
612 	if (i)
613 		ads7846_disable(ts);
614 	else
615 		ads7846_enable(ts);
616 
617 	return count;
618 }
619 
620 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
621 
622 static struct attribute *ads784x_attributes[] = {
623 	&dev_attr_pen_down.attr,
624 	&dev_attr_disable.attr,
625 	NULL,
626 };
627 
628 static const struct attribute_group ads784x_attr_group = {
629 	.attrs = ads784x_attributes,
630 };
631 
632 /*--------------------------------------------------------------------------*/
633 
634 static void null_wait_for_sync(void)
635 {
636 }
637 
638 static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
639 {
640 	struct ads7846 *ts = ads;
641 
642 	if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
643 		/* Start over collecting consistent readings. */
644 		ts->read_rep = 0;
645 		/*
646 		 * Repeat it, if this was the first read or the read
647 		 * wasn't consistent enough.
648 		 */
649 		if (ts->read_cnt < ts->debounce_max) {
650 			ts->last_read = *val;
651 			ts->read_cnt++;
652 			return ADS7846_FILTER_REPEAT;
653 		} else {
654 			/*
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
658 			 * period.
659 			 */
660 			ts->read_cnt = 0;
661 			return ADS7846_FILTER_IGNORE;
662 		}
663 	} else {
664 		if (++ts->read_rep > ts->debounce_rep) {
665 			/*
666 			 * Got a good reading for this coordinate,
667 			 * go for the next one.
668 			 */
669 			ts->read_cnt = 0;
670 			ts->read_rep = 0;
671 			return ADS7846_FILTER_OK;
672 		} else {
673 			/* Read more values that are consistent. */
674 			ts->read_cnt++;
675 			return ADS7846_FILTER_REPEAT;
676 		}
677 	}
678 }
679 
680 static int ads7846_no_filter(void *ads, int data_idx, int *val)
681 {
682 	return ADS7846_FILTER_OK;
683 }
684 
685 static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)
686 {
687 	int value;
688 	struct spi_transfer *t =
689 		list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
690 
691 	if (ts->model == 7845) {
692 		value = be16_to_cpup((__be16 *)&(((char *)t->rx_buf)[1]));
693 	} else {
694 		/*
695 		 * adjust:  on-wire is a must-ignore bit, a BE12 value, then
696 		 * padding; built from two 8 bit values written msb-first.
697 		 */
698 		value = be16_to_cpup((__be16 *)t->rx_buf);
699 	}
700 
701 	/* enforce ADC output is 12 bits width */
702 	return (value >> 3) & 0xfff;
703 }
704 
705 static void ads7846_update_value(struct spi_message *m, int val)
706 {
707 	struct spi_transfer *t =
708 		list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
709 
710 	*(u16 *)t->rx_buf = val;
711 }
712 
713 static void ads7846_read_state(struct ads7846 *ts)
714 {
715 	struct ads7846_packet *packet = ts->packet;
716 	struct spi_message *m;
717 	int msg_idx = 0;
718 	int val;
719 	int action;
720 	int error;
721 
722 	while (msg_idx < ts->msg_count) {
723 
724 		ts->wait_for_sync();
725 
726 		m = &ts->msg[msg_idx];
727 		error = spi_sync(ts->spi, m);
728 		if (error) {
729 			dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
730 			packet->tc.ignore = true;
731 			return;
732 		}
733 
734 		/*
735 		 * Last message is power down request, no need to convert
736 		 * or filter the value.
737 		 */
738 		if (msg_idx < ts->msg_count - 1) {
739 
740 			val = ads7846_get_value(ts, m);
741 
742 			action = ts->filter(ts->filter_data, msg_idx, &val);
743 			switch (action) {
744 			case ADS7846_FILTER_REPEAT:
745 				continue;
746 
747 			case ADS7846_FILTER_IGNORE:
748 				packet->tc.ignore = true;
749 				msg_idx = ts->msg_count - 1;
750 				continue;
751 
752 			case ADS7846_FILTER_OK:
753 				ads7846_update_value(m, val);
754 				packet->tc.ignore = false;
755 				msg_idx++;
756 				break;
757 
758 			default:
759 				BUG();
760 			}
761 		} else {
762 			msg_idx++;
763 		}
764 	}
765 }
766 
767 static void ads7846_report_state(struct ads7846 *ts)
768 {
769 	struct ads7846_packet *packet = ts->packet;
770 	unsigned int Rt;
771 	u16 x, y, z1, z2;
772 
773 	/*
774 	 * ads7846_get_value() does in-place conversion (including byte swap)
775 	 * from on-the-wire format as part of debouncing to get stable
776 	 * readings.
777 	 */
778 	if (ts->model == 7845) {
779 		x = *(u16 *)packet->tc.x_buf;
780 		y = *(u16 *)packet->tc.y_buf;
781 		z1 = 0;
782 		z2 = 0;
783 	} else {
784 		x = packet->tc.x;
785 		y = packet->tc.y;
786 		z1 = packet->tc.z1;
787 		z2 = packet->tc.z2;
788 	}
789 
790 	/* range filtering */
791 	if (x == MAX_12BIT)
792 		x = 0;
793 
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;
799 		else
800 			Rt = 0;
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 */
804 		Rt = z2;
805 		Rt -= z1;
806 		Rt *= ts->x_plate_ohms;
807 		Rt = DIV_ROUND_CLOSEST(Rt, 16);
808 		Rt *= x;
809 		Rt /= z1;
810 		Rt = DIV_ROUND_CLOSEST(Rt, 256);
811 	} else {
812 		Rt = 0;
813 	}
814 
815 	/*
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
819 	 */
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);
823 		return;
824 	}
825 
826 	/*
827 	 * Maybe check the pendown state before reporting. This discards
828 	 * false readings when the pen is lifted.
829 	 */
830 	if (ts->penirq_recheck_delay_usecs) {
831 		udelay(ts->penirq_recheck_delay_usecs);
832 		if (!get_pendown_state(ts))
833 			Rt = 0;
834 	}
835 
836 	/*
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.
841 	 *
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).
844 	 */
845 	if (Rt) {
846 		struct input_dev *input = ts->input;
847 
848 		if (!ts->pendown) {
849 			input_report_key(input, BTN_TOUCH, 1);
850 			ts->pendown = true;
851 			dev_vdbg(&ts->spi->dev, "DOWN\n");
852 		}
853 
854 		touchscreen_report_pos(input, &ts->core_prop, x, y, false);
855 		input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
856 
857 		input_sync(input);
858 		dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
859 	}
860 }
861 
862 static irqreturn_t ads7846_hard_irq(int irq, void *handle)
863 {
864 	struct ads7846 *ts = handle;
865 
866 	return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
867 }
868 
869 
870 static irqreturn_t ads7846_irq(int irq, void *handle)
871 {
872 	struct ads7846 *ts = handle;
873 
874 	/* Start with a small delay before checking pendown state */
875 	msleep(TS_POLL_DELAY);
876 
877 	while (!ts->stopped && get_pendown_state(ts)) {
878 
879 		/* pen is down, continue with the measurement */
880 		ads7846_read_state(ts);
881 
882 		if (!ts->stopped)
883 			ads7846_report_state(ts);
884 
885 		wait_event_timeout(ts->wait, ts->stopped,
886 				   msecs_to_jiffies(TS_POLL_PERIOD));
887 	}
888 
889 	if (ts->pendown && !ts->stopped)
890 		ads7846_report_pen_up(ts);
891 
892 	return IRQ_HANDLED;
893 }
894 
895 static int __maybe_unused ads7846_suspend(struct device *dev)
896 {
897 	struct ads7846 *ts = dev_get_drvdata(dev);
898 
899 	mutex_lock(&ts->lock);
900 
901 	if (!ts->suspended) {
902 
903 		if (!ts->disabled)
904 			__ads7846_disable(ts);
905 
906 		if (device_may_wakeup(&ts->spi->dev))
907 			enable_irq_wake(ts->spi->irq);
908 
909 		ts->suspended = true;
910 	}
911 
912 	mutex_unlock(&ts->lock);
913 
914 	return 0;
915 }
916 
917 static int __maybe_unused ads7846_resume(struct device *dev)
918 {
919 	struct ads7846 *ts = dev_get_drvdata(dev);
920 
921 	mutex_lock(&ts->lock);
922 
923 	if (ts->suspended) {
924 
925 		ts->suspended = false;
926 
927 		if (device_may_wakeup(&ts->spi->dev))
928 			disable_irq_wake(ts->spi->irq);
929 
930 		if (!ts->disabled)
931 			__ads7846_enable(ts);
932 	}
933 
934 	mutex_unlock(&ts->lock);
935 
936 	return 0;
937 }
938 
939 static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
940 
941 static int ads7846_setup_pendown(struct spi_device *spi,
942 				 struct ads7846 *ts,
943 				 const struct ads7846_platform_data *pdata)
944 {
945 	int err;
946 
947 	/*
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
950 	 * the pendown state.
951 	 */
952 
953 	if (pdata->get_pendown_state) {
954 		ts->get_pendown_state = pdata->get_pendown_state;
955 	} else if (gpio_is_valid(pdata->gpio_pendown)) {
956 
957 		err = gpio_request_one(pdata->gpio_pendown, GPIOF_IN,
958 				       "ads7846_pendown");
959 		if (err) {
960 			dev_err(&spi->dev,
961 				"failed to request/setup pendown GPIO%d: %d\n",
962 				pdata->gpio_pendown, err);
963 			return err;
964 		}
965 
966 		ts->gpio_pendown = pdata->gpio_pendown;
967 
968 		if (pdata->gpio_pendown_debounce)
969 			gpio_set_debounce(pdata->gpio_pendown,
970 					  pdata->gpio_pendown_debounce);
971 	} else {
972 		dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
973 		return -EINVAL;
974 	}
975 
976 	return 0;
977 }
978 
979 /*
980  * Set up the transfers to read touchscreen state; this assumes we
981  * use formula #2 for pressure, not #3.
982  */
983 static void ads7846_setup_spi_msg(struct ads7846 *ts,
984 				  const struct ads7846_platform_data *pdata)
985 {
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;
990 
991 	if (ts->model == 7873) {
992 		/*
993 		 * The AD7873 is almost identical to the ADS7846
994 		 * keep VREF off during differential/ratiometric
995 		 * conversion modes.
996 		 */
997 		ts->model = 7846;
998 		vref = 0;
999 	}
1000 
1001 	ts->msg_count = 1;
1002 	spi_message_init(m);
1003 	m->context = ts;
1004 
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];
1011 		x->len = 3;
1012 		spi_message_add_tail(x, m);
1013 	} else {
1014 		/* y- still on; turn on only y+ (and ADC) */
1015 		packet->read_y = READ_Y(vref);
1016 		x->tx_buf = &packet->read_y;
1017 		x->len = 1;
1018 		spi_message_add_tail(x, m);
1019 
1020 		x++;
1021 		x->rx_buf = &packet->tc.y;
1022 		x->len = 2;
1023 		spi_message_add_tail(x, m);
1024 	}
1025 
1026 	/*
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.
1030 	 */
1031 	if (pdata->settle_delay_usecs) {
1032 		x->delay.value = pdata->settle_delay_usecs;
1033 		x->delay.unit = SPI_DELAY_UNIT_USECS;
1034 
1035 		x++;
1036 		x->tx_buf = &packet->read_y;
1037 		x->len = 1;
1038 		spi_message_add_tail(x, m);
1039 
1040 		x++;
1041 		x->rx_buf = &packet->tc.y;
1042 		x->len = 2;
1043 		spi_message_add_tail(x, m);
1044 	}
1045 
1046 	ts->msg_count++;
1047 	m++;
1048 	spi_message_init(m);
1049 	m->context = ts;
1050 
1051 	if (ts->model == 7845) {
1052 		x++;
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];
1058 		x->len = 3;
1059 		spi_message_add_tail(x, m);
1060 	} else {
1061 		/* turn y- off, x+ on, then leave in lowpower */
1062 		x++;
1063 		packet->read_x = READ_X(vref);
1064 		x->tx_buf = &packet->read_x;
1065 		x->len = 1;
1066 		spi_message_add_tail(x, m);
1067 
1068 		x++;
1069 		x->rx_buf = &packet->tc.x;
1070 		x->len = 2;
1071 		spi_message_add_tail(x, m);
1072 	}
1073 
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;
1078 
1079 		x++;
1080 		x->tx_buf = &packet->read_x;
1081 		x->len = 1;
1082 		spi_message_add_tail(x, m);
1083 
1084 		x++;
1085 		x->rx_buf = &packet->tc.x;
1086 		x->len = 2;
1087 		spi_message_add_tail(x, m);
1088 	}
1089 
1090 	/* turn y+ off, x- on; we'll use formula #2 */
1091 	if (ts->model == 7846) {
1092 		ts->msg_count++;
1093 		m++;
1094 		spi_message_init(m);
1095 		m->context = ts;
1096 
1097 		x++;
1098 		packet->read_z1 = READ_Z1(vref);
1099 		x->tx_buf = &packet->read_z1;
1100 		x->len = 1;
1101 		spi_message_add_tail(x, m);
1102 
1103 		x++;
1104 		x->rx_buf = &packet->tc.z1;
1105 		x->len = 2;
1106 		spi_message_add_tail(x, m);
1107 
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;
1112 
1113 			x++;
1114 			x->tx_buf = &packet->read_z1;
1115 			x->len = 1;
1116 			spi_message_add_tail(x, m);
1117 
1118 			x++;
1119 			x->rx_buf = &packet->tc.z1;
1120 			x->len = 2;
1121 			spi_message_add_tail(x, m);
1122 		}
1123 
1124 		ts->msg_count++;
1125 		m++;
1126 		spi_message_init(m);
1127 		m->context = ts;
1128 
1129 		x++;
1130 		packet->read_z2 = READ_Z2(vref);
1131 		x->tx_buf = &packet->read_z2;
1132 		x->len = 1;
1133 		spi_message_add_tail(x, m);
1134 
1135 		x++;
1136 		x->rx_buf = &packet->tc.z2;
1137 		x->len = 2;
1138 		spi_message_add_tail(x, m);
1139 
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;
1144 
1145 			x++;
1146 			x->tx_buf = &packet->read_z2;
1147 			x->len = 1;
1148 			spi_message_add_tail(x, m);
1149 
1150 			x++;
1151 			x->rx_buf = &packet->tc.z2;
1152 			x->len = 2;
1153 			spi_message_add_tail(x, m);
1154 		}
1155 	}
1156 
1157 	/* power down */
1158 	ts->msg_count++;
1159 	m++;
1160 	spi_message_init(m);
1161 	m->context = ts;
1162 
1163 	if (ts->model == 7845) {
1164 		x++;
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];
1169 		x->len = 3;
1170 	} else {
1171 		x++;
1172 		packet->pwrdown = PWRDOWN;
1173 		x->tx_buf = &packet->pwrdown;
1174 		x->len = 1;
1175 		spi_message_add_tail(x, m);
1176 
1177 		x++;
1178 		x->rx_buf = &packet->dummy;
1179 		x->len = 2;
1180 	}
1181 
1182 	CS_CHANGE(*x);
1183 	spi_message_add_tail(x, m);
1184 }
1185 
1186 #ifdef CONFIG_OF
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 },
1193 	{ }
1194 };
1195 MODULE_DEVICE_TABLE(of, ads7846_dt_ids);
1196 
1197 static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1198 {
1199 	struct ads7846_platform_data *pdata;
1200 	struct device_node *node = dev->of_node;
1201 	const struct of_device_id *match;
1202 	u32 value;
1203 
1204 	if (!node) {
1205 		dev_err(dev, "Device does not have associated DT data\n");
1206 		return ERR_PTR(-EINVAL);
1207 	}
1208 
1209 	match = of_match_device(ads7846_dt_ids, dev);
1210 	if (!match) {
1211 		dev_err(dev, "Unknown device model\n");
1212 		return ERR_PTR(-EINVAL);
1213 	}
1214 
1215 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1216 	if (!pdata)
1217 		return ERR_PTR(-ENOMEM);
1218 
1219 	pdata->model = (unsigned long)match->data;
1220 
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");
1225 
1226 	pdata->swap_xy = of_property_read_bool(node, "ti,swap-xy");
1227 
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);
1232 
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);
1235 
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);
1240 
1241 	/*
1242 	 * touchscreen-max-pressure gets parsed during
1243 	 * touchscreen_parse_properties()
1244 	 */
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);
1249 
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);
1255 
1256 	of_property_read_u32(node, "ti,pendown-gpio-debounce",
1257 			     &pdata->gpio_pendown_debounce);
1258 
1259 	pdata->wakeup = of_property_read_bool(node, "wakeup-source") ||
1260 			of_property_read_bool(node, "linux,wakeup");
1261 
1262 	pdata->gpio_pendown = of_get_named_gpio(dev->of_node, "pendown-gpio", 0);
1263 
1264 	return pdata;
1265 }
1266 #else
1267 static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1268 {
1269 	dev_err(dev, "no platform data defined\n");
1270 	return ERR_PTR(-EINVAL);
1271 }
1272 #endif
1273 
1274 static int ads7846_probe(struct spi_device *spi)
1275 {
1276 	const struct ads7846_platform_data *pdata;
1277 	struct ads7846 *ts;
1278 	struct ads7846_packet *packet;
1279 	struct input_dev *input_dev;
1280 	unsigned long irq_flags;
1281 	int err;
1282 
1283 	if (!spi->irq) {
1284 		dev_dbg(&spi->dev, "no IRQ?\n");
1285 		return -EINVAL;
1286 	}
1287 
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);
1292 		return -EINVAL;
1293 	}
1294 
1295 	/*
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.
1299 	 */
1300 	spi->bits_per_word = 8;
1301 	spi->mode = SPI_MODE_0;
1302 	err = spi_setup(spi);
1303 	if (err < 0)
1304 		return err;
1305 
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) {
1310 		err = -ENOMEM;
1311 		goto err_free_mem;
1312 	}
1313 
1314 	spi_set_drvdata(spi, ts);
1315 
1316 	ts->packet = packet;
1317 	ts->spi = spi;
1318 	ts->input = input_dev;
1319 
1320 	mutex_init(&ts->lock);
1321 	init_waitqueue_head(&ts->wait);
1322 
1323 	pdata = dev_get_platdata(&spi->dev);
1324 	if (!pdata) {
1325 		pdata = ads7846_probe_dt(&spi->dev);
1326 		if (IS_ERR(pdata)) {
1327 			err = PTR_ERR(pdata);
1328 			goto err_free_mem;
1329 		}
1330 	}
1331 
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;
1336 
1337 	if (pdata->filter != NULL) {
1338 		if (pdata->filter_init != NULL) {
1339 			err = pdata->filter_init(pdata, &ts->filter_data);
1340 			if (err < 0)
1341 				goto err_free_mem;
1342 		}
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;
1353 	} else {
1354 		ts->filter = ads7846_no_filter;
1355 	}
1356 
1357 	err = ads7846_setup_pendown(spi, ts, pdata);
1358 	if (err)
1359 		goto err_cleanup_filter;
1360 
1361 	if (pdata->penirq_recheck_delay_usecs)
1362 		ts->penirq_recheck_delay_usecs =
1363 				pdata->penirq_recheck_delay_usecs;
1364 
1365 	ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1366 
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);
1369 
1370 	input_dev->name = ts->name;
1371 	input_dev->phys = ts->phys;
1372 	input_dev->dev.parent = &spi->dev;
1373 
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,
1377 			pdata->x_min ? : 0,
1378 			pdata->x_max ? : MAX_12BIT,
1379 			0, 0);
1380 	input_set_abs_params(input_dev, ABS_Y,
1381 			pdata->y_min ? : 0,
1382 			pdata->y_max ? : MAX_12BIT,
1383 			0, 0);
1384 	input_set_abs_params(input_dev, ABS_PRESSURE,
1385 			pdata->pressure_min, pdata->pressure_max, 0, 0);
1386 
1387 	/*
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.
1391 	 */
1392 	touchscreen_parse_properties(ts->input, false, &ts->core_prop);
1393 	ts->pressure_max = input_abs_get_max(input_dev, ABS_PRESSURE) ? : ~0;
1394 
1395 	/*
1396 	 * Check if legacy ti,swap-xy binding is used instead of
1397 	 * touchscreen-swapped-x-y
1398 	 */
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;
1402 	}
1403 
1404 	ads7846_setup_spi_msg(ts, pdata);
1405 
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);
1410 		goto err_free_gpio;
1411 	}
1412 
1413 	err = regulator_enable(ts->reg);
1414 	if (err) {
1415 		dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
1416 		goto err_put_regulator;
1417 	}
1418 
1419 	irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
1420 	irq_flags |= IRQF_ONESHOT;
1421 
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) {
1425 		dev_info(&spi->dev,
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);
1431 	}
1432 
1433 	if (err) {
1434 		dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1435 		goto err_disable_regulator;
1436 	}
1437 
1438 	err = ads784x_hwmon_register(spi, ts);
1439 	if (err)
1440 		goto err_free_irq;
1441 
1442 	dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1443 
1444 	/*
1445 	 * Take a first sample, leaving nPENIRQ active and vREF off; avoid
1446 	 * the touchscreen, in case it's not connected.
1447 	 */
1448 	if (ts->model == 7845)
1449 		ads7845_read12_ser(&spi->dev, PWRDOWN);
1450 	else
1451 		(void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux));
1452 
1453 	err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1454 	if (err)
1455 		goto err_remove_hwmon;
1456 
1457 	err = input_register_device(input_dev);
1458 	if (err)
1459 		goto err_remove_attr_group;
1460 
1461 	device_init_wakeup(&spi->dev, pdata->wakeup);
1462 
1463 	/*
1464 	 * If device does not carry platform data we must have allocated it
1465 	 * when parsing DT data.
1466 	 */
1467 	if (!dev_get_platdata(&spi->dev))
1468 		devm_kfree(&spi->dev, (void *)pdata);
1469 
1470 	return 0;
1471 
1472  err_remove_attr_group:
1473 	sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1474  err_remove_hwmon:
1475 	ads784x_hwmon_unregister(spi, ts);
1476  err_free_irq:
1477 	free_irq(spi->irq, ts);
1478  err_disable_regulator:
1479 	regulator_disable(ts->reg);
1480  err_put_regulator:
1481 	regulator_put(ts->reg);
1482  err_free_gpio:
1483 	if (!ts->get_pendown_state)
1484 		gpio_free(ts->gpio_pendown);
1485  err_cleanup_filter:
1486 	if (ts->filter_cleanup)
1487 		ts->filter_cleanup(ts->filter_data);
1488  err_free_mem:
1489 	input_free_device(input_dev);
1490 	kfree(packet);
1491 	kfree(ts);
1492 	return err;
1493 }
1494 
1495 static int ads7846_remove(struct spi_device *spi)
1496 {
1497 	struct ads7846 *ts = spi_get_drvdata(spi);
1498 
1499 	sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1500 
1501 	ads7846_disable(ts);
1502 	free_irq(ts->spi->irq, ts);
1503 
1504 	input_unregister_device(ts->input);
1505 
1506 	ads784x_hwmon_unregister(spi, ts);
1507 
1508 	regulator_put(ts->reg);
1509 
1510 	if (!ts->get_pendown_state) {
1511 		/*
1512 		 * If we are not using specialized pendown method we must
1513 		 * have been relying on gpio we set up ourselves.
1514 		 */
1515 		gpio_free(ts->gpio_pendown);
1516 	}
1517 
1518 	if (ts->filter_cleanup)
1519 		ts->filter_cleanup(ts->filter_data);
1520 
1521 	kfree(ts->packet);
1522 	kfree(ts);
1523 
1524 	dev_dbg(&spi->dev, "unregistered touchscreen\n");
1525 
1526 	return 0;
1527 }
1528 
1529 static struct spi_driver ads7846_driver = {
1530 	.driver = {
1531 		.name	= "ads7846",
1532 		.pm	= &ads7846_pm,
1533 		.of_match_table = of_match_ptr(ads7846_dt_ids),
1534 	},
1535 	.probe		= ads7846_probe,
1536 	.remove		= ads7846_remove,
1537 };
1538 
1539 module_spi_driver(ads7846_driver);
1540 
1541 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1542 MODULE_LICENSE("GPL");
1543 MODULE_ALIAS("spi:ads7846");
1544