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 ads7846_buf {
67 	u8 cmd;
68 	/*
69 	 * This union is a temporary hack. The driver does an in-place
70 	 * endianness conversion. This will be cleaned up in the next
71 	 * patch.
72 	 */
73 	union {
74 		__be16 data_be16;
75 		u16 data;
76 	};
77 } __packed;
78 
79 
80 struct ts_event {
81 	bool ignore;
82 	struct ads7846_buf x;
83 	struct ads7846_buf y;
84 	struct ads7846_buf z1;
85 	struct ads7846_buf z2;
86 };
87 
88 /*
89  * We allocate this separately to avoid cache line sharing issues when
90  * driver is used with DMA-based SPI controllers (like atmel_spi) on
91  * systems where main memory is not DMA-coherent (most non-x86 boards).
92  */
93 struct ads7846_packet {
94 	struct ts_event tc;
95 	struct ads7846_buf read_x_cmd;
96 	struct ads7846_buf read_y_cmd;
97 	struct ads7846_buf read_z1_cmd;
98 	struct ads7846_buf read_z2_cmd;
99 	struct ads7846_buf pwrdown_cmd;
100 };
101 
102 struct ads7846 {
103 	struct input_dev	*input;
104 	char			phys[32];
105 	char			name[32];
106 
107 	struct spi_device	*spi;
108 	struct regulator	*reg;
109 
110 #if IS_ENABLED(CONFIG_HWMON)
111 	struct device		*hwmon;
112 #endif
113 
114 	u16			model;
115 	u16			vref_mv;
116 	u16			vref_delay_usecs;
117 	u16			x_plate_ohms;
118 	u16			pressure_max;
119 
120 	bool			swap_xy;
121 	bool			use_internal;
122 
123 	struct ads7846_packet	*packet;
124 
125 	struct spi_transfer	xfer[18];
126 	struct spi_message	msg[5];
127 	int			msg_count;
128 	wait_queue_head_t	wait;
129 
130 	bool			pendown;
131 
132 	int			read_cnt;
133 	int			read_rep;
134 	int			last_read;
135 
136 	u16			debounce_max;
137 	u16			debounce_tol;
138 	u16			debounce_rep;
139 
140 	u16			penirq_recheck_delay_usecs;
141 
142 	struct touchscreen_properties core_prop;
143 
144 	struct mutex		lock;
145 	bool			stopped;	/* P: lock */
146 	bool			disabled;	/* P: lock */
147 	bool			suspended;	/* P: lock */
148 
149 	int			(*filter)(void *data, int data_idx, int *val);
150 	void			*filter_data;
151 	void			(*filter_cleanup)(void *data);
152 	int			(*get_pendown_state)(void);
153 	int			gpio_pendown;
154 
155 	void			(*wait_for_sync)(void);
156 };
157 
158 /* leave chip selected when we're done, for quicker re-select? */
159 #if	0
160 #define	CS_CHANGE(xfer)	((xfer).cs_change = 1)
161 #else
162 #define	CS_CHANGE(xfer)	((xfer).cs_change = 0)
163 #endif
164 
165 /*--------------------------------------------------------------------------*/
166 
167 /* The ADS7846 has touchscreen and other sensors.
168  * Earlier ads784x chips are somewhat compatible.
169  */
170 #define	ADS_START		(1 << 7)
171 #define	ADS_A2A1A0_d_y		(1 << 4)	/* differential */
172 #define	ADS_A2A1A0_d_z1		(3 << 4)	/* differential */
173 #define	ADS_A2A1A0_d_z2		(4 << 4)	/* differential */
174 #define	ADS_A2A1A0_d_x		(5 << 4)	/* differential */
175 #define	ADS_A2A1A0_temp0	(0 << 4)	/* non-differential */
176 #define	ADS_A2A1A0_vbatt	(2 << 4)	/* non-differential */
177 #define	ADS_A2A1A0_vaux		(6 << 4)	/* non-differential */
178 #define	ADS_A2A1A0_temp1	(7 << 4)	/* non-differential */
179 #define	ADS_8_BIT		(1 << 3)
180 #define	ADS_12_BIT		(0 << 3)
181 #define	ADS_SER			(1 << 2)	/* non-differential */
182 #define	ADS_DFR			(0 << 2)	/* differential */
183 #define	ADS_PD10_PDOWN		(0 << 0)	/* low power mode + penirq */
184 #define	ADS_PD10_ADC_ON		(1 << 0)	/* ADC on */
185 #define	ADS_PD10_REF_ON		(2 << 0)	/* vREF on + penirq */
186 #define	ADS_PD10_ALL_ON		(3 << 0)	/* ADC + vREF on */
187 
188 #define	MAX_12BIT	((1<<12)-1)
189 
190 /* leave ADC powered up (disables penirq) between differential samples */
191 #define	READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
192 	| ADS_12_BIT | ADS_DFR | \
193 	(adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
194 
195 #define	READ_Y(vref)	(READ_12BIT_DFR(y,  1, vref))
196 #define	READ_Z1(vref)	(READ_12BIT_DFR(z1, 1, vref))
197 #define	READ_Z2(vref)	(READ_12BIT_DFR(z2, 1, vref))
198 
199 #define	READ_X(vref)	(READ_12BIT_DFR(x,  1, vref))
200 #define	PWRDOWN		(READ_12BIT_DFR(y,  0, 0))	/* LAST */
201 
202 /* single-ended samples need to first power up reference voltage;
203  * we leave both ADC and VREF powered
204  */
205 #define	READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
206 	| ADS_12_BIT | ADS_SER)
207 
208 #define	REF_ON	(READ_12BIT_DFR(x, 1, 1))
209 #define	REF_OFF	(READ_12BIT_DFR(y, 0, 0))
210 
211 static int get_pendown_state(struct ads7846 *ts)
212 {
213 	if (ts->get_pendown_state)
214 		return ts->get_pendown_state();
215 
216 	return !gpio_get_value(ts->gpio_pendown);
217 }
218 
219 static void ads7846_report_pen_up(struct ads7846 *ts)
220 {
221 	struct input_dev *input = ts->input;
222 
223 	input_report_key(input, BTN_TOUCH, 0);
224 	input_report_abs(input, ABS_PRESSURE, 0);
225 	input_sync(input);
226 
227 	ts->pendown = false;
228 	dev_vdbg(&ts->spi->dev, "UP\n");
229 }
230 
231 /* Must be called with ts->lock held */
232 static void ads7846_stop(struct ads7846 *ts)
233 {
234 	if (!ts->disabled && !ts->suspended) {
235 		/* Signal IRQ thread to stop polling and disable the handler. */
236 		ts->stopped = true;
237 		mb();
238 		wake_up(&ts->wait);
239 		disable_irq(ts->spi->irq);
240 	}
241 }
242 
243 /* Must be called with ts->lock held */
244 static void ads7846_restart(struct ads7846 *ts)
245 {
246 	if (!ts->disabled && !ts->suspended) {
247 		/* Check if pen was released since last stop */
248 		if (ts->pendown && !get_pendown_state(ts))
249 			ads7846_report_pen_up(ts);
250 
251 		/* Tell IRQ thread that it may poll the device. */
252 		ts->stopped = false;
253 		mb();
254 		enable_irq(ts->spi->irq);
255 	}
256 }
257 
258 /* Must be called with ts->lock held */
259 static void __ads7846_disable(struct ads7846 *ts)
260 {
261 	ads7846_stop(ts);
262 	regulator_disable(ts->reg);
263 
264 	/*
265 	 * We know the chip's in low power mode since we always
266 	 * leave it that way after every request
267 	 */
268 }
269 
270 /* Must be called with ts->lock held */
271 static void __ads7846_enable(struct ads7846 *ts)
272 {
273 	int error;
274 
275 	error = regulator_enable(ts->reg);
276 	if (error != 0)
277 		dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error);
278 
279 	ads7846_restart(ts);
280 }
281 
282 static void ads7846_disable(struct ads7846 *ts)
283 {
284 	mutex_lock(&ts->lock);
285 
286 	if (!ts->disabled) {
287 
288 		if  (!ts->suspended)
289 			__ads7846_disable(ts);
290 
291 		ts->disabled = true;
292 	}
293 
294 	mutex_unlock(&ts->lock);
295 }
296 
297 static void ads7846_enable(struct ads7846 *ts)
298 {
299 	mutex_lock(&ts->lock);
300 
301 	if (ts->disabled) {
302 
303 		ts->disabled = false;
304 
305 		if (!ts->suspended)
306 			__ads7846_enable(ts);
307 	}
308 
309 	mutex_unlock(&ts->lock);
310 }
311 
312 /*--------------------------------------------------------------------------*/
313 
314 /*
315  * Non-touchscreen sensors only use single-ended conversions.
316  * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
317  * ads7846 lets that pin be unconnected, to use internal vREF.
318  */
319 
320 struct ser_req {
321 	u8			ref_on;
322 	u8			command;
323 	u8			ref_off;
324 	u16			scratch;
325 	struct spi_message	msg;
326 	struct spi_transfer	xfer[6];
327 	/*
328 	 * DMA (thus cache coherency maintenance) requires the
329 	 * transfer buffers to live in their own cache lines.
330 	 */
331 	__be16 sample ____cacheline_aligned;
332 };
333 
334 struct ads7845_ser_req {
335 	u8			command[3];
336 	struct spi_message	msg;
337 	struct spi_transfer	xfer[2];
338 	/*
339 	 * DMA (thus cache coherency maintenance) requires the
340 	 * transfer buffers to live in their own cache lines.
341 	 */
342 	u8 sample[3] ____cacheline_aligned;
343 };
344 
345 static int ads7846_read12_ser(struct device *dev, unsigned command)
346 {
347 	struct spi_device *spi = to_spi_device(dev);
348 	struct ads7846 *ts = dev_get_drvdata(dev);
349 	struct ser_req *req;
350 	int status;
351 
352 	req = kzalloc(sizeof *req, GFP_KERNEL);
353 	if (!req)
354 		return -ENOMEM;
355 
356 	spi_message_init(&req->msg);
357 
358 	/* maybe turn on internal vREF, and let it settle */
359 	if (ts->use_internal) {
360 		req->ref_on = REF_ON;
361 		req->xfer[0].tx_buf = &req->ref_on;
362 		req->xfer[0].len = 1;
363 		spi_message_add_tail(&req->xfer[0], &req->msg);
364 
365 		req->xfer[1].rx_buf = &req->scratch;
366 		req->xfer[1].len = 2;
367 
368 		/* for 1uF, settle for 800 usec; no cap, 100 usec.  */
369 		req->xfer[1].delay.value = ts->vref_delay_usecs;
370 		req->xfer[1].delay.unit = SPI_DELAY_UNIT_USECS;
371 		spi_message_add_tail(&req->xfer[1], &req->msg);
372 
373 		/* Enable reference voltage */
374 		command |= ADS_PD10_REF_ON;
375 	}
376 
377 	/* Enable ADC in every case */
378 	command |= ADS_PD10_ADC_ON;
379 
380 	/* take sample */
381 	req->command = (u8) command;
382 	req->xfer[2].tx_buf = &req->command;
383 	req->xfer[2].len = 1;
384 	spi_message_add_tail(&req->xfer[2], &req->msg);
385 
386 	req->xfer[3].rx_buf = &req->sample;
387 	req->xfer[3].len = 2;
388 	spi_message_add_tail(&req->xfer[3], &req->msg);
389 
390 	/* REVISIT:  take a few more samples, and compare ... */
391 
392 	/* converter in low power mode & enable PENIRQ */
393 	req->ref_off = PWRDOWN;
394 	req->xfer[4].tx_buf = &req->ref_off;
395 	req->xfer[4].len = 1;
396 	spi_message_add_tail(&req->xfer[4], &req->msg);
397 
398 	req->xfer[5].rx_buf = &req->scratch;
399 	req->xfer[5].len = 2;
400 	CS_CHANGE(req->xfer[5]);
401 	spi_message_add_tail(&req->xfer[5], &req->msg);
402 
403 	mutex_lock(&ts->lock);
404 	ads7846_stop(ts);
405 	status = spi_sync(spi, &req->msg);
406 	ads7846_restart(ts);
407 	mutex_unlock(&ts->lock);
408 
409 	if (status == 0) {
410 		/* on-wire is a must-ignore bit, a BE12 value, then padding */
411 		status = be16_to_cpu(req->sample);
412 		status = status >> 3;
413 		status &= 0x0fff;
414 	}
415 
416 	kfree(req);
417 	return status;
418 }
419 
420 static int ads7845_read12_ser(struct device *dev, unsigned command)
421 {
422 	struct spi_device *spi = to_spi_device(dev);
423 	struct ads7846 *ts = dev_get_drvdata(dev);
424 	struct ads7845_ser_req *req;
425 	int status;
426 
427 	req = kzalloc(sizeof *req, GFP_KERNEL);
428 	if (!req)
429 		return -ENOMEM;
430 
431 	spi_message_init(&req->msg);
432 
433 	req->command[0] = (u8) command;
434 	req->xfer[0].tx_buf = req->command;
435 	req->xfer[0].rx_buf = req->sample;
436 	req->xfer[0].len = 3;
437 	spi_message_add_tail(&req->xfer[0], &req->msg);
438 
439 	mutex_lock(&ts->lock);
440 	ads7846_stop(ts);
441 	status = spi_sync(spi, &req->msg);
442 	ads7846_restart(ts);
443 	mutex_unlock(&ts->lock);
444 
445 	if (status == 0) {
446 		/* BE12 value, then padding */
447 		status = get_unaligned_be16(&req->sample[1]);
448 		status = status >> 3;
449 		status &= 0x0fff;
450 	}
451 
452 	kfree(req);
453 	return status;
454 }
455 
456 #if IS_ENABLED(CONFIG_HWMON)
457 
458 #define SHOW(name, var, adjust) static ssize_t \
459 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
460 { \
461 	struct ads7846 *ts = dev_get_drvdata(dev); \
462 	ssize_t v = ads7846_read12_ser(&ts->spi->dev, \
463 			READ_12BIT_SER(var)); \
464 	if (v < 0) \
465 		return v; \
466 	return sprintf(buf, "%u\n", adjust(ts, v)); \
467 } \
468 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
469 
470 
471 /* Sysfs conventions report temperatures in millidegrees Celsius.
472  * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
473  * accuracy scheme without calibration data.  For now we won't try either;
474  * userspace sees raw sensor values, and must scale/calibrate appropriately.
475  */
476 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
477 {
478 	return v;
479 }
480 
481 SHOW(temp0, temp0, null_adjust)		/* temp1_input */
482 SHOW(temp1, temp1, null_adjust)		/* temp2_input */
483 
484 
485 /* sysfs conventions report voltages in millivolts.  We can convert voltages
486  * if we know vREF.  userspace may need to scale vAUX to match the board's
487  * external resistors; we assume that vBATT only uses the internal ones.
488  */
489 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
490 {
491 	unsigned retval = v;
492 
493 	/* external resistors may scale vAUX into 0..vREF */
494 	retval *= ts->vref_mv;
495 	retval = retval >> 12;
496 
497 	return retval;
498 }
499 
500 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
501 {
502 	unsigned retval = vaux_adjust(ts, v);
503 
504 	/* ads7846 has a resistor ladder to scale this signal down */
505 	if (ts->model == 7846)
506 		retval *= 4;
507 
508 	return retval;
509 }
510 
511 SHOW(in0_input, vaux, vaux_adjust)
512 SHOW(in1_input, vbatt, vbatt_adjust)
513 
514 static umode_t ads7846_is_visible(struct kobject *kobj, struct attribute *attr,
515 				  int index)
516 {
517 	struct device *dev = kobj_to_dev(kobj);
518 	struct ads7846 *ts = dev_get_drvdata(dev);
519 
520 	if (ts->model == 7843 && index < 2)	/* in0, in1 */
521 		return 0;
522 	if (ts->model == 7845 && index != 2)	/* in0 */
523 		return 0;
524 
525 	return attr->mode;
526 }
527 
528 static struct attribute *ads7846_attributes[] = {
529 	&dev_attr_temp0.attr,		/* 0 */
530 	&dev_attr_temp1.attr,		/* 1 */
531 	&dev_attr_in0_input.attr,	/* 2 */
532 	&dev_attr_in1_input.attr,	/* 3 */
533 	NULL,
534 };
535 
536 static const struct attribute_group ads7846_attr_group = {
537 	.attrs = ads7846_attributes,
538 	.is_visible = ads7846_is_visible,
539 };
540 __ATTRIBUTE_GROUPS(ads7846_attr);
541 
542 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
543 {
544 	/* hwmon sensors need a reference voltage */
545 	switch (ts->model) {
546 	case 7846:
547 		if (!ts->vref_mv) {
548 			dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
549 			ts->vref_mv = 2500;
550 			ts->use_internal = true;
551 		}
552 		break;
553 	case 7845:
554 	case 7843:
555 		if (!ts->vref_mv) {
556 			dev_warn(&spi->dev,
557 				"external vREF for ADS%d not specified\n",
558 				ts->model);
559 			return 0;
560 		}
561 		break;
562 	}
563 
564 	ts->hwmon = hwmon_device_register_with_groups(&spi->dev, spi->modalias,
565 						      ts, ads7846_attr_groups);
566 
567 	return PTR_ERR_OR_ZERO(ts->hwmon);
568 }
569 
570 static void ads784x_hwmon_unregister(struct spi_device *spi,
571 				     struct ads7846 *ts)
572 {
573 	if (ts->hwmon)
574 		hwmon_device_unregister(ts->hwmon);
575 }
576 
577 #else
578 static inline int ads784x_hwmon_register(struct spi_device *spi,
579 					 struct ads7846 *ts)
580 {
581 	return 0;
582 }
583 
584 static inline void ads784x_hwmon_unregister(struct spi_device *spi,
585 					    struct ads7846 *ts)
586 {
587 }
588 #endif
589 
590 static 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 
598 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
599 
600 static 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 
608 static 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 
628 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
629 
630 static struct attribute *ads784x_attributes[] = {
631 	&dev_attr_pen_down.attr,
632 	&dev_attr_disable.attr,
633 	NULL,
634 };
635 
636 static const struct attribute_group ads784x_attr_group = {
637 	.attrs = ads784x_attributes,
638 };
639 
640 /*--------------------------------------------------------------------------*/
641 
642 static void null_wait_for_sync(void)
643 {
644 }
645 
646 static 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 
688 static int ads7846_no_filter(void *ads, int data_idx, int *val)
689 {
690 	return ADS7846_FILTER_OK;
691 }
692 
693 static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)
694 {
695 	int value;
696 	struct spi_transfer *t =
697 		list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
698 	struct ads7846_buf *buf = t->rx_buf;
699 
700 	value = be16_to_cpup(&buf->data_be16);
701 
702 	/* enforce ADC output is 12 bits width */
703 	return (value >> 3) & 0xfff;
704 }
705 
706 static void ads7846_update_value(struct spi_message *m, int val)
707 {
708 	struct spi_transfer *t =
709 		list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
710 	struct ads7846_buf *buf = t->rx_buf;
711 
712 	buf->data = val;
713 }
714 
715 static void ads7846_read_state(struct ads7846 *ts)
716 {
717 	struct ads7846_packet *packet = ts->packet;
718 	struct spi_message *m;
719 	int msg_idx = 0;
720 	int val;
721 	int action;
722 	int error;
723 
724 	while (msg_idx < ts->msg_count) {
725 
726 		ts->wait_for_sync();
727 
728 		m = &ts->msg[msg_idx];
729 		error = spi_sync(ts->spi, m);
730 		if (error) {
731 			dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
732 			packet->tc.ignore = true;
733 			return;
734 		}
735 
736 		/*
737 		 * Last message is power down request, no need to convert
738 		 * or filter the value.
739 		 */
740 		if (msg_idx < ts->msg_count - 1) {
741 
742 			val = ads7846_get_value(ts, m);
743 
744 			action = ts->filter(ts->filter_data, msg_idx, &val);
745 			switch (action) {
746 			case ADS7846_FILTER_REPEAT:
747 				continue;
748 
749 			case ADS7846_FILTER_IGNORE:
750 				packet->tc.ignore = true;
751 				msg_idx = ts->msg_count - 1;
752 				continue;
753 
754 			case ADS7846_FILTER_OK:
755 				ads7846_update_value(m, val);
756 				packet->tc.ignore = false;
757 				msg_idx++;
758 				break;
759 
760 			default:
761 				BUG();
762 			}
763 		} else {
764 			msg_idx++;
765 		}
766 	}
767 }
768 
769 static void ads7846_report_state(struct ads7846 *ts)
770 {
771 	struct ads7846_packet *packet = ts->packet;
772 	unsigned int Rt;
773 	u16 x, y, z1, z2;
774 
775 	/*
776 	 * ads7846_get_value() does in-place conversion (including byte swap)
777 	 * from on-the-wire format as part of debouncing to get stable
778 	 * readings.
779 	 */
780 	x = packet->tc.x.data;
781 	y = packet->tc.y.data;
782 	if (ts->model == 7845) {
783 		z1 = 0;
784 		z2 = 0;
785 	} else {
786 		z1 = packet->tc.z1.data;
787 		z2 = packet->tc.z2.data;
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 	packet->read_y_cmd.cmd = READ_Y(vref);
1006 	x->tx_buf = &packet->read_y_cmd;
1007 	x->rx_buf = &packet->tc.y;
1008 	x->len = 3;
1009 	spi_message_add_tail(x, m);
1010 
1011 	/*
1012 	 * The first sample after switching drivers can be low quality;
1013 	 * optionally discard it, using a second one after the signals
1014 	 * have had enough time to stabilize.
1015 	 */
1016 	if (pdata->settle_delay_usecs) {
1017 		x->delay.value = pdata->settle_delay_usecs;
1018 		x->delay.unit = SPI_DELAY_UNIT_USECS;
1019 		x++;
1020 
1021 		x->tx_buf = &packet->read_y_cmd;
1022 		x->rx_buf = &packet->tc.y;
1023 		x->len = 3;
1024 		spi_message_add_tail(x, m);
1025 	}
1026 
1027 	ts->msg_count++;
1028 	m++;
1029 	spi_message_init(m);
1030 	m->context = ts;
1031 
1032 	/* turn y- off, x+ on, then leave in lowpower */
1033 	x++;
1034 	packet->read_x_cmd.cmd = READ_X(vref);
1035 	x->tx_buf = &packet->read_x_cmd;
1036 	x->rx_buf = &packet->tc.x;
1037 	x->len = 3;
1038 	spi_message_add_tail(x, m);
1039 
1040 	/* ... maybe discard first sample ... */
1041 	if (pdata->settle_delay_usecs) {
1042 		x->delay.value = pdata->settle_delay_usecs;
1043 		x->delay.unit = SPI_DELAY_UNIT_USECS;
1044 
1045 		x++;
1046 		x->tx_buf = &packet->read_x_cmd;
1047 		x->rx_buf = &packet->tc.x;
1048 		x->len = 3;
1049 		spi_message_add_tail(x, m);
1050 	}
1051 
1052 	/* turn y+ off, x- on; we'll use formula #2 */
1053 	if (ts->model == 7846) {
1054 		ts->msg_count++;
1055 		m++;
1056 		spi_message_init(m);
1057 		m->context = ts;
1058 
1059 		x++;
1060 		packet->read_z1_cmd.cmd = READ_Z1(vref);
1061 		x->tx_buf = &packet->read_z1_cmd;
1062 		x->rx_buf = &packet->tc.z1;
1063 		x->len = 3;
1064 		spi_message_add_tail(x, m);
1065 
1066 		/* ... maybe discard first sample ... */
1067 		if (pdata->settle_delay_usecs) {
1068 			x->delay.value = pdata->settle_delay_usecs;
1069 			x->delay.unit = SPI_DELAY_UNIT_USECS;
1070 
1071 			x++;
1072 			x->tx_buf = &packet->read_z1_cmd;
1073 			x->rx_buf = &packet->tc.z1;
1074 			x->len = 3;
1075 			spi_message_add_tail(x, m);
1076 		}
1077 
1078 		ts->msg_count++;
1079 		m++;
1080 		spi_message_init(m);
1081 		m->context = ts;
1082 
1083 		x++;
1084 		packet->read_z2_cmd.cmd = READ_Z2(vref);
1085 		x->tx_buf = &packet->read_z2_cmd;
1086 		x->rx_buf = &packet->tc.z2;
1087 		x->len = 3;
1088 		spi_message_add_tail(x, m);
1089 
1090 		/* ... maybe discard first sample ... */
1091 		if (pdata->settle_delay_usecs) {
1092 			x->delay.value = pdata->settle_delay_usecs;
1093 			x->delay.unit = SPI_DELAY_UNIT_USECS;
1094 
1095 			x++;
1096 			x->tx_buf = &packet->read_z2_cmd;
1097 			x->rx_buf = &packet->tc.z2;
1098 			x->len = 3;
1099 			spi_message_add_tail(x, m);
1100 		}
1101 	}
1102 
1103 	/* power down */
1104 	ts->msg_count++;
1105 	m++;
1106 	spi_message_init(m);
1107 	m->context = ts;
1108 
1109 	x++;
1110 	packet->pwrdown_cmd.cmd = PWRDOWN;
1111 	x->tx_buf = &packet->pwrdown_cmd;
1112 	x->len = 3;
1113 
1114 	CS_CHANGE(*x);
1115 	spi_message_add_tail(x, m);
1116 }
1117 
1118 #ifdef CONFIG_OF
1119 static const struct of_device_id ads7846_dt_ids[] = {
1120 	{ .compatible = "ti,tsc2046",	.data = (void *) 7846 },
1121 	{ .compatible = "ti,ads7843",	.data = (void *) 7843 },
1122 	{ .compatible = "ti,ads7845",	.data = (void *) 7845 },
1123 	{ .compatible = "ti,ads7846",	.data = (void *) 7846 },
1124 	{ .compatible = "ti,ads7873",	.data = (void *) 7873 },
1125 	{ }
1126 };
1127 MODULE_DEVICE_TABLE(of, ads7846_dt_ids);
1128 
1129 static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1130 {
1131 	struct ads7846_platform_data *pdata;
1132 	struct device_node *node = dev->of_node;
1133 	const struct of_device_id *match;
1134 	u32 value;
1135 
1136 	if (!node) {
1137 		dev_err(dev, "Device does not have associated DT data\n");
1138 		return ERR_PTR(-EINVAL);
1139 	}
1140 
1141 	match = of_match_device(ads7846_dt_ids, dev);
1142 	if (!match) {
1143 		dev_err(dev, "Unknown device model\n");
1144 		return ERR_PTR(-EINVAL);
1145 	}
1146 
1147 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1148 	if (!pdata)
1149 		return ERR_PTR(-ENOMEM);
1150 
1151 	pdata->model = (unsigned long)match->data;
1152 
1153 	of_property_read_u16(node, "ti,vref-delay-usecs",
1154 			     &pdata->vref_delay_usecs);
1155 	of_property_read_u16(node, "ti,vref-mv", &pdata->vref_mv);
1156 	pdata->keep_vref_on = of_property_read_bool(node, "ti,keep-vref-on");
1157 
1158 	pdata->swap_xy = of_property_read_bool(node, "ti,swap-xy");
1159 
1160 	of_property_read_u16(node, "ti,settle-delay-usec",
1161 			     &pdata->settle_delay_usecs);
1162 	of_property_read_u16(node, "ti,penirq-recheck-delay-usecs",
1163 			     &pdata->penirq_recheck_delay_usecs);
1164 
1165 	of_property_read_u16(node, "ti,x-plate-ohms", &pdata->x_plate_ohms);
1166 	of_property_read_u16(node, "ti,y-plate-ohms", &pdata->y_plate_ohms);
1167 
1168 	of_property_read_u16(node, "ti,x-min", &pdata->x_min);
1169 	of_property_read_u16(node, "ti,y-min", &pdata->y_min);
1170 	of_property_read_u16(node, "ti,x-max", &pdata->x_max);
1171 	of_property_read_u16(node, "ti,y-max", &pdata->y_max);
1172 
1173 	/*
1174 	 * touchscreen-max-pressure gets parsed during
1175 	 * touchscreen_parse_properties()
1176 	 */
1177 	of_property_read_u16(node, "ti,pressure-min", &pdata->pressure_min);
1178 	if (!of_property_read_u32(node, "touchscreen-min-pressure", &value))
1179 		pdata->pressure_min = (u16) value;
1180 	of_property_read_u16(node, "ti,pressure-max", &pdata->pressure_max);
1181 
1182 	of_property_read_u16(node, "ti,debounce-max", &pdata->debounce_max);
1183 	if (!of_property_read_u32(node, "touchscreen-average-samples", &value))
1184 		pdata->debounce_max = (u16) value;
1185 	of_property_read_u16(node, "ti,debounce-tol", &pdata->debounce_tol);
1186 	of_property_read_u16(node, "ti,debounce-rep", &pdata->debounce_rep);
1187 
1188 	of_property_read_u32(node, "ti,pendown-gpio-debounce",
1189 			     &pdata->gpio_pendown_debounce);
1190 
1191 	pdata->wakeup = of_property_read_bool(node, "wakeup-source") ||
1192 			of_property_read_bool(node, "linux,wakeup");
1193 
1194 	pdata->gpio_pendown = of_get_named_gpio(dev->of_node, "pendown-gpio", 0);
1195 
1196 	return pdata;
1197 }
1198 #else
1199 static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1200 {
1201 	dev_err(dev, "no platform data defined\n");
1202 	return ERR_PTR(-EINVAL);
1203 }
1204 #endif
1205 
1206 static int ads7846_probe(struct spi_device *spi)
1207 {
1208 	const struct ads7846_platform_data *pdata;
1209 	struct ads7846 *ts;
1210 	struct ads7846_packet *packet;
1211 	struct input_dev *input_dev;
1212 	unsigned long irq_flags;
1213 	int err;
1214 
1215 	if (!spi->irq) {
1216 		dev_dbg(&spi->dev, "no IRQ?\n");
1217 		return -EINVAL;
1218 	}
1219 
1220 	/* don't exceed max specified sample rate */
1221 	if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
1222 		dev_err(&spi->dev, "f(sample) %d KHz?\n",
1223 				(spi->max_speed_hz/SAMPLE_BITS)/1000);
1224 		return -EINVAL;
1225 	}
1226 
1227 	/*
1228 	 * We'd set TX word size 8 bits and RX word size to 13 bits ... except
1229 	 * that even if the hardware can do that, the SPI controller driver
1230 	 * may not.  So we stick to very-portable 8 bit words, both RX and TX.
1231 	 */
1232 	spi->bits_per_word = 8;
1233 	spi->mode = SPI_MODE_0;
1234 	err = spi_setup(spi);
1235 	if (err < 0)
1236 		return err;
1237 
1238 	ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
1239 	packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
1240 	input_dev = input_allocate_device();
1241 	if (!ts || !packet || !input_dev) {
1242 		err = -ENOMEM;
1243 		goto err_free_mem;
1244 	}
1245 
1246 	spi_set_drvdata(spi, ts);
1247 
1248 	ts->packet = packet;
1249 	ts->spi = spi;
1250 	ts->input = input_dev;
1251 
1252 	mutex_init(&ts->lock);
1253 	init_waitqueue_head(&ts->wait);
1254 
1255 	pdata = dev_get_platdata(&spi->dev);
1256 	if (!pdata) {
1257 		pdata = ads7846_probe_dt(&spi->dev);
1258 		if (IS_ERR(pdata)) {
1259 			err = PTR_ERR(pdata);
1260 			goto err_free_mem;
1261 		}
1262 	}
1263 
1264 	ts->model = pdata->model ? : 7846;
1265 	ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
1266 	ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
1267 	ts->vref_mv = pdata->vref_mv;
1268 
1269 	if (pdata->filter != NULL) {
1270 		if (pdata->filter_init != NULL) {
1271 			err = pdata->filter_init(pdata, &ts->filter_data);
1272 			if (err < 0)
1273 				goto err_free_mem;
1274 		}
1275 		ts->filter = pdata->filter;
1276 		ts->filter_cleanup = pdata->filter_cleanup;
1277 	} else if (pdata->debounce_max) {
1278 		ts->debounce_max = pdata->debounce_max;
1279 		if (ts->debounce_max < 2)
1280 			ts->debounce_max = 2;
1281 		ts->debounce_tol = pdata->debounce_tol;
1282 		ts->debounce_rep = pdata->debounce_rep;
1283 		ts->filter = ads7846_debounce_filter;
1284 		ts->filter_data = ts;
1285 	} else {
1286 		ts->filter = ads7846_no_filter;
1287 	}
1288 
1289 	err = ads7846_setup_pendown(spi, ts, pdata);
1290 	if (err)
1291 		goto err_cleanup_filter;
1292 
1293 	if (pdata->penirq_recheck_delay_usecs)
1294 		ts->penirq_recheck_delay_usecs =
1295 				pdata->penirq_recheck_delay_usecs;
1296 
1297 	ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1298 
1299 	snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
1300 	snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
1301 
1302 	input_dev->name = ts->name;
1303 	input_dev->phys = ts->phys;
1304 	input_dev->dev.parent = &spi->dev;
1305 
1306 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1307 	input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1308 	input_set_abs_params(input_dev, ABS_X,
1309 			pdata->x_min ? : 0,
1310 			pdata->x_max ? : MAX_12BIT,
1311 			0, 0);
1312 	input_set_abs_params(input_dev, ABS_Y,
1313 			pdata->y_min ? : 0,
1314 			pdata->y_max ? : MAX_12BIT,
1315 			0, 0);
1316 	input_set_abs_params(input_dev, ABS_PRESSURE,
1317 			pdata->pressure_min, pdata->pressure_max, 0, 0);
1318 
1319 	/*
1320 	 * Parse common framework properties. Must be done here to ensure the
1321 	 * correct behaviour in case of using the legacy vendor bindings. The
1322 	 * general binding value overrides the vendor specific one.
1323 	 */
1324 	touchscreen_parse_properties(ts->input, false, &ts->core_prop);
1325 	ts->pressure_max = input_abs_get_max(input_dev, ABS_PRESSURE) ? : ~0;
1326 
1327 	/*
1328 	 * Check if legacy ti,swap-xy binding is used instead of
1329 	 * touchscreen-swapped-x-y
1330 	 */
1331 	if (!ts->core_prop.swap_x_y && pdata->swap_xy) {
1332 		swap(input_dev->absinfo[ABS_X], input_dev->absinfo[ABS_Y]);
1333 		ts->core_prop.swap_x_y = true;
1334 	}
1335 
1336 	ads7846_setup_spi_msg(ts, pdata);
1337 
1338 	ts->reg = regulator_get(&spi->dev, "vcc");
1339 	if (IS_ERR(ts->reg)) {
1340 		err = PTR_ERR(ts->reg);
1341 		dev_err(&spi->dev, "unable to get regulator: %d\n", err);
1342 		goto err_free_gpio;
1343 	}
1344 
1345 	err = regulator_enable(ts->reg);
1346 	if (err) {
1347 		dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
1348 		goto err_put_regulator;
1349 	}
1350 
1351 	irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
1352 	irq_flags |= IRQF_ONESHOT;
1353 
1354 	err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,
1355 				   irq_flags, spi->dev.driver->name, ts);
1356 	if (err && !pdata->irq_flags) {
1357 		dev_info(&spi->dev,
1358 			"trying pin change workaround on irq %d\n", spi->irq);
1359 		irq_flags |= IRQF_TRIGGER_RISING;
1360 		err = request_threaded_irq(spi->irq,
1361 				  ads7846_hard_irq, ads7846_irq,
1362 				  irq_flags, spi->dev.driver->name, ts);
1363 	}
1364 
1365 	if (err) {
1366 		dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1367 		goto err_disable_regulator;
1368 	}
1369 
1370 	err = ads784x_hwmon_register(spi, ts);
1371 	if (err)
1372 		goto err_free_irq;
1373 
1374 	dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1375 
1376 	/*
1377 	 * Take a first sample, leaving nPENIRQ active and vREF off; avoid
1378 	 * the touchscreen, in case it's not connected.
1379 	 */
1380 	if (ts->model == 7845)
1381 		ads7845_read12_ser(&spi->dev, PWRDOWN);
1382 	else
1383 		(void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux));
1384 
1385 	err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1386 	if (err)
1387 		goto err_remove_hwmon;
1388 
1389 	err = input_register_device(input_dev);
1390 	if (err)
1391 		goto err_remove_attr_group;
1392 
1393 	device_init_wakeup(&spi->dev, pdata->wakeup);
1394 
1395 	/*
1396 	 * If device does not carry platform data we must have allocated it
1397 	 * when parsing DT data.
1398 	 */
1399 	if (!dev_get_platdata(&spi->dev))
1400 		devm_kfree(&spi->dev, (void *)pdata);
1401 
1402 	return 0;
1403 
1404  err_remove_attr_group:
1405 	sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1406  err_remove_hwmon:
1407 	ads784x_hwmon_unregister(spi, ts);
1408  err_free_irq:
1409 	free_irq(spi->irq, ts);
1410  err_disable_regulator:
1411 	regulator_disable(ts->reg);
1412  err_put_regulator:
1413 	regulator_put(ts->reg);
1414  err_free_gpio:
1415 	if (!ts->get_pendown_state)
1416 		gpio_free(ts->gpio_pendown);
1417  err_cleanup_filter:
1418 	if (ts->filter_cleanup)
1419 		ts->filter_cleanup(ts->filter_data);
1420  err_free_mem:
1421 	input_free_device(input_dev);
1422 	kfree(packet);
1423 	kfree(ts);
1424 	return err;
1425 }
1426 
1427 static int ads7846_remove(struct spi_device *spi)
1428 {
1429 	struct ads7846 *ts = spi_get_drvdata(spi);
1430 
1431 	sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1432 
1433 	ads7846_disable(ts);
1434 	free_irq(ts->spi->irq, ts);
1435 
1436 	input_unregister_device(ts->input);
1437 
1438 	ads784x_hwmon_unregister(spi, ts);
1439 
1440 	regulator_put(ts->reg);
1441 
1442 	if (!ts->get_pendown_state) {
1443 		/*
1444 		 * If we are not using specialized pendown method we must
1445 		 * have been relying on gpio we set up ourselves.
1446 		 */
1447 		gpio_free(ts->gpio_pendown);
1448 	}
1449 
1450 	if (ts->filter_cleanup)
1451 		ts->filter_cleanup(ts->filter_data);
1452 
1453 	kfree(ts->packet);
1454 	kfree(ts);
1455 
1456 	dev_dbg(&spi->dev, "unregistered touchscreen\n");
1457 
1458 	return 0;
1459 }
1460 
1461 static struct spi_driver ads7846_driver = {
1462 	.driver = {
1463 		.name	= "ads7846",
1464 		.pm	= &ads7846_pm,
1465 		.of_match_table = of_match_ptr(ads7846_dt_ids),
1466 	},
1467 	.probe		= ads7846_probe,
1468 	.remove		= ads7846_remove,
1469 };
1470 
1471 module_spi_driver(ads7846_driver);
1472 
1473 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1474 MODULE_LICENSE("GPL");
1475 MODULE_ALIAS("spi:ads7846");
1476