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