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 	/*
68 	 * This union is a temporary hack. The driver does an in-place
69 	 * endianness conversion. This will be cleaned up in the next
70 	 * patch.
71 	 */
72 	union {
73 		__be16 data_be16;
74 		u16 data;
75 	};
76 } __packed;
77 
78 
79 struct ts_event {
80 	bool ignore;
81 	struct ads7846_buf x;
82 	struct ads7846_buf y;
83 	struct ads7846_buf z1;
84 	struct ads7846_buf z2;
85 };
86 
87 /*
88  * We allocate this separately to avoid cache line sharing issues when
89  * driver is used with DMA-based SPI controllers (like atmel_spi) on
90  * systems where main memory is not DMA-coherent (most non-x86 boards).
91  */
92 struct ads7846_packet {
93 	struct ts_event tc;
94 	struct ads7846_buf read_x_cmd;
95 	struct ads7846_buf read_y_cmd;
96 	struct ads7846_buf read_z1_cmd;
97 	struct ads7846_buf read_z2_cmd;
98 	struct ads7846_buf pwrdown_cmd;
99 };
100 
101 struct ads7846 {
102 	struct input_dev	*input;
103 	char			phys[32];
104 	char			name[32];
105 
106 	struct spi_device	*spi;
107 	struct regulator	*reg;
108 
109 #if IS_ENABLED(CONFIG_HWMON)
110 	struct device		*hwmon;
111 #endif
112 
113 	u16			model;
114 	u16			vref_mv;
115 	u16			vref_delay_usecs;
116 	u16			x_plate_ohms;
117 	u16			pressure_max;
118 
119 	bool			swap_xy;
120 	bool			use_internal;
121 
122 	struct ads7846_packet	*packet;
123 
124 	struct spi_transfer	xfer[18];
125 	struct spi_message	msg[5];
126 	int			msg_count;
127 	wait_queue_head_t	wait;
128 
129 	bool			pendown;
130 
131 	int			read_cnt;
132 	int			read_rep;
133 	int			last_read;
134 
135 	u16			debounce_max;
136 	u16			debounce_tol;
137 	u16			debounce_rep;
138 
139 	u16			penirq_recheck_delay_usecs;
140 
141 	struct touchscreen_properties core_prop;
142 
143 	struct mutex		lock;
144 	bool			stopped;	/* P: lock */
145 	bool			disabled;	/* P: lock */
146 	bool			suspended;	/* P: lock */
147 
148 	int			(*filter)(void *data, int data_idx, int *val);
149 	void			*filter_data;
150 	void			(*filter_cleanup)(void *data);
151 	int			(*get_pendown_state)(void);
152 	int			gpio_pendown;
153 
154 	void			(*wait_for_sync)(void);
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 
198 #define	READ_X(vref)	(READ_12BIT_DFR(x,  1, vref))
199 #define	PWRDOWN		(READ_12BIT_DFR(y,  0, 0))	/* LAST */
200 
201 /* single-ended samples need to first power up reference voltage;
202  * we leave both ADC and VREF powered
203  */
204 #define	READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
205 	| ADS_12_BIT | ADS_SER)
206 
207 #define	REF_ON	(READ_12BIT_DFR(x, 1, 1))
208 #define	REF_OFF	(READ_12BIT_DFR(y, 0, 0))
209 
210 static int get_pendown_state(struct ads7846 *ts)
211 {
212 	if (ts->get_pendown_state)
213 		return ts->get_pendown_state();
214 
215 	return !gpio_get_value(ts->gpio_pendown);
216 }
217 
218 static void ads7846_report_pen_up(struct ads7846 *ts)
219 {
220 	struct input_dev *input = ts->input;
221 
222 	input_report_key(input, BTN_TOUCH, 0);
223 	input_report_abs(input, ABS_PRESSURE, 0);
224 	input_sync(input);
225 
226 	ts->pendown = false;
227 	dev_vdbg(&ts->spi->dev, "UP\n");
228 }
229 
230 /* Must be called with ts->lock held */
231 static void ads7846_stop(struct ads7846 *ts)
232 {
233 	if (!ts->disabled && !ts->suspended) {
234 		/* Signal IRQ thread to stop polling and disable the handler. */
235 		ts->stopped = true;
236 		mb();
237 		wake_up(&ts->wait);
238 		disable_irq(ts->spi->irq);
239 	}
240 }
241 
242 /* Must be called with ts->lock held */
243 static void ads7846_restart(struct ads7846 *ts)
244 {
245 	if (!ts->disabled && !ts->suspended) {
246 		/* Check if pen was released since last stop */
247 		if (ts->pendown && !get_pendown_state(ts))
248 			ads7846_report_pen_up(ts);
249 
250 		/* Tell IRQ thread that it may poll the device. */
251 		ts->stopped = false;
252 		mb();
253 		enable_irq(ts->spi->irq);
254 	}
255 }
256 
257 /* Must be called with ts->lock held */
258 static void __ads7846_disable(struct ads7846 *ts)
259 {
260 	ads7846_stop(ts);
261 	regulator_disable(ts->reg);
262 
263 	/*
264 	 * We know the chip's in low power mode since we always
265 	 * leave it that way after every request
266 	 */
267 }
268 
269 /* Must be called with ts->lock held */
270 static void __ads7846_enable(struct ads7846 *ts)
271 {
272 	int error;
273 
274 	error = regulator_enable(ts->reg);
275 	if (error != 0)
276 		dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error);
277 
278 	ads7846_restart(ts);
279 }
280 
281 static void ads7846_disable(struct ads7846 *ts)
282 {
283 	mutex_lock(&ts->lock);
284 
285 	if (!ts->disabled) {
286 
287 		if  (!ts->suspended)
288 			__ads7846_disable(ts);
289 
290 		ts->disabled = true;
291 	}
292 
293 	mutex_unlock(&ts->lock);
294 }
295 
296 static void ads7846_enable(struct ads7846 *ts)
297 {
298 	mutex_lock(&ts->lock);
299 
300 	if (ts->disabled) {
301 
302 		ts->disabled = false;
303 
304 		if (!ts->suspended)
305 			__ads7846_enable(ts);
306 	}
307 
308 	mutex_unlock(&ts->lock);
309 }
310 
311 /*--------------------------------------------------------------------------*/
312 
313 /*
314  * Non-touchscreen sensors only use single-ended conversions.
315  * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
316  * ads7846 lets that pin be unconnected, to use internal vREF.
317  */
318 
319 struct ser_req {
320 	u8			ref_on;
321 	u8			command;
322 	u8			ref_off;
323 	u16			scratch;
324 	struct spi_message	msg;
325 	struct spi_transfer	xfer[6];
326 	/*
327 	 * DMA (thus cache coherency maintenance) requires the
328 	 * transfer buffers to live in their own cache lines.
329 	 */
330 	__be16 sample ____cacheline_aligned;
331 };
332 
333 struct ads7845_ser_req {
334 	u8			command[3];
335 	struct spi_message	msg;
336 	struct spi_transfer	xfer[2];
337 	/*
338 	 * DMA (thus cache coherency maintenance) requires the
339 	 * transfer buffers to live in their own cache lines.
340 	 */
341 	u8 sample[3] ____cacheline_aligned;
342 };
343 
344 static int ads7846_read12_ser(struct device *dev, unsigned command)
345 {
346 	struct spi_device *spi = to_spi_device(dev);
347 	struct ads7846 *ts = dev_get_drvdata(dev);
348 	struct ser_req *req;
349 	int status;
350 
351 	req = kzalloc(sizeof *req, GFP_KERNEL);
352 	if (!req)
353 		return -ENOMEM;
354 
355 	spi_message_init(&req->msg);
356 
357 	/* maybe turn on internal vREF, and let it settle */
358 	if (ts->use_internal) {
359 		req->ref_on = REF_ON;
360 		req->xfer[0].tx_buf = &req->ref_on;
361 		req->xfer[0].len = 1;
362 		spi_message_add_tail(&req->xfer[0], &req->msg);
363 
364 		req->xfer[1].rx_buf = &req->scratch;
365 		req->xfer[1].len = 2;
366 
367 		/* for 1uF, settle for 800 usec; no cap, 100 usec.  */
368 		req->xfer[1].delay.value = ts->vref_delay_usecs;
369 		req->xfer[1].delay.unit = SPI_DELAY_UNIT_USECS;
370 		spi_message_add_tail(&req->xfer[1], &req->msg);
371 
372 		/* Enable reference voltage */
373 		command |= ADS_PD10_REF_ON;
374 	}
375 
376 	/* Enable ADC in every case */
377 	command |= ADS_PD10_ADC_ON;
378 
379 	/* take sample */
380 	req->command = (u8) command;
381 	req->xfer[2].tx_buf = &req->command;
382 	req->xfer[2].len = 1;
383 	spi_message_add_tail(&req->xfer[2], &req->msg);
384 
385 	req->xfer[3].rx_buf = &req->sample;
386 	req->xfer[3].len = 2;
387 	spi_message_add_tail(&req->xfer[3], &req->msg);
388 
389 	/* REVISIT:  take a few more samples, and compare ... */
390 
391 	/* converter in low power mode & enable PENIRQ */
392 	req->ref_off = PWRDOWN;
393 	req->xfer[4].tx_buf = &req->ref_off;
394 	req->xfer[4].len = 1;
395 	spi_message_add_tail(&req->xfer[4], &req->msg);
396 
397 	req->xfer[5].rx_buf = &req->scratch;
398 	req->xfer[5].len = 2;
399 	CS_CHANGE(req->xfer[5]);
400 	spi_message_add_tail(&req->xfer[5], &req->msg);
401 
402 	mutex_lock(&ts->lock);
403 	ads7846_stop(ts);
404 	status = spi_sync(spi, &req->msg);
405 	ads7846_restart(ts);
406 	mutex_unlock(&ts->lock);
407 
408 	if (status == 0) {
409 		/* on-wire is a must-ignore bit, a BE12 value, then padding */
410 		status = be16_to_cpu(req->sample);
411 		status = status >> 3;
412 		status &= 0x0fff;
413 	}
414 
415 	kfree(req);
416 	return status;
417 }
418 
419 static int ads7845_read12_ser(struct device *dev, unsigned command)
420 {
421 	struct spi_device *spi = to_spi_device(dev);
422 	struct ads7846 *ts = dev_get_drvdata(dev);
423 	struct ads7845_ser_req *req;
424 	int status;
425 
426 	req = kzalloc(sizeof *req, GFP_KERNEL);
427 	if (!req)
428 		return -ENOMEM;
429 
430 	spi_message_init(&req->msg);
431 
432 	req->command[0] = (u8) command;
433 	req->xfer[0].tx_buf = req->command;
434 	req->xfer[0].rx_buf = req->sample;
435 	req->xfer[0].len = 3;
436 	spi_message_add_tail(&req->xfer[0], &req->msg);
437 
438 	mutex_lock(&ts->lock);
439 	ads7846_stop(ts);
440 	status = spi_sync(spi, &req->msg);
441 	ads7846_restart(ts);
442 	mutex_unlock(&ts->lock);
443 
444 	if (status == 0) {
445 		/* BE12 value, then padding */
446 		status = get_unaligned_be16(&req->sample[1]);
447 		status = status >> 3;
448 		status &= 0x0fff;
449 	}
450 
451 	kfree(req);
452 	return status;
453 }
454 
455 #if IS_ENABLED(CONFIG_HWMON)
456 
457 #define SHOW(name, var, adjust) static ssize_t \
458 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
459 { \
460 	struct ads7846 *ts = dev_get_drvdata(dev); \
461 	ssize_t v = ads7846_read12_ser(&ts->spi->dev, \
462 			READ_12BIT_SER(var)); \
463 	if (v < 0) \
464 		return v; \
465 	return sprintf(buf, "%u\n", adjust(ts, v)); \
466 } \
467 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
468 
469 
470 /* Sysfs conventions report temperatures in millidegrees Celsius.
471  * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
472  * accuracy scheme without calibration data.  For now we won't try either;
473  * userspace sees raw sensor values, and must scale/calibrate appropriately.
474  */
475 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
476 {
477 	return v;
478 }
479 
480 SHOW(temp0, temp0, null_adjust)		/* temp1_input */
481 SHOW(temp1, temp1, null_adjust)		/* temp2_input */
482 
483 
484 /* sysfs conventions report voltages in millivolts.  We can convert voltages
485  * if we know vREF.  userspace may need to scale vAUX to match the board's
486  * external resistors; we assume that vBATT only uses the internal ones.
487  */
488 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
489 {
490 	unsigned retval = v;
491 
492 	/* external resistors may scale vAUX into 0..vREF */
493 	retval *= ts->vref_mv;
494 	retval = retval >> 12;
495 
496 	return retval;
497 }
498 
499 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
500 {
501 	unsigned retval = vaux_adjust(ts, v);
502 
503 	/* ads7846 has a resistor ladder to scale this signal down */
504 	if (ts->model == 7846)
505 		retval *= 4;
506 
507 	return retval;
508 }
509 
510 SHOW(in0_input, vaux, vaux_adjust)
511 SHOW(in1_input, vbatt, vbatt_adjust)
512 
513 static umode_t ads7846_is_visible(struct kobject *kobj, struct attribute *attr,
514 				  int index)
515 {
516 	struct device *dev = kobj_to_dev(kobj);
517 	struct ads7846 *ts = dev_get_drvdata(dev);
518 
519 	if (ts->model == 7843 && index < 2)	/* in0, in1 */
520 		return 0;
521 	if (ts->model == 7845 && index != 2)	/* in0 */
522 		return 0;
523 
524 	return attr->mode;
525 }
526 
527 static struct attribute *ads7846_attributes[] = {
528 	&dev_attr_temp0.attr,		/* 0 */
529 	&dev_attr_temp1.attr,		/* 1 */
530 	&dev_attr_in0_input.attr,	/* 2 */
531 	&dev_attr_in1_input.attr,	/* 3 */
532 	NULL,
533 };
534 
535 static const struct attribute_group ads7846_attr_group = {
536 	.attrs = ads7846_attributes,
537 	.is_visible = ads7846_is_visible,
538 };
539 __ATTRIBUTE_GROUPS(ads7846_attr);
540 
541 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
542 {
543 	/* hwmon sensors need a reference voltage */
544 	switch (ts->model) {
545 	case 7846:
546 		if (!ts->vref_mv) {
547 			dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
548 			ts->vref_mv = 2500;
549 			ts->use_internal = true;
550 		}
551 		break;
552 	case 7845:
553 	case 7843:
554 		if (!ts->vref_mv) {
555 			dev_warn(&spi->dev,
556 				"external vREF for ADS%d not specified\n",
557 				ts->model);
558 			return 0;
559 		}
560 		break;
561 	}
562 
563 	ts->hwmon = hwmon_device_register_with_groups(&spi->dev, spi->modalias,
564 						      ts, ads7846_attr_groups);
565 
566 	return PTR_ERR_OR_ZERO(ts->hwmon);
567 }
568 
569 static void ads784x_hwmon_unregister(struct spi_device *spi,
570 				     struct ads7846 *ts)
571 {
572 	if (ts->hwmon)
573 		hwmon_device_unregister(ts->hwmon);
574 }
575 
576 #else
577 static inline int ads784x_hwmon_register(struct spi_device *spi,
578 					 struct ads7846 *ts)
579 {
580 	return 0;
581 }
582 
583 static inline void ads784x_hwmon_unregister(struct spi_device *spi,
584 					    struct ads7846 *ts)
585 {
586 }
587 #endif
588 
589 static ssize_t ads7846_pen_down_show(struct device *dev,
590 				     struct device_attribute *attr, char *buf)
591 {
592 	struct ads7846 *ts = dev_get_drvdata(dev);
593 
594 	return sprintf(buf, "%u\n", ts->pendown);
595 }
596 
597 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
598 
599 static ssize_t ads7846_disable_show(struct device *dev,
600 				     struct device_attribute *attr, char *buf)
601 {
602 	struct ads7846 *ts = dev_get_drvdata(dev);
603 
604 	return sprintf(buf, "%u\n", ts->disabled);
605 }
606 
607 static ssize_t ads7846_disable_store(struct device *dev,
608 				     struct device_attribute *attr,
609 				     const char *buf, size_t count)
610 {
611 	struct ads7846 *ts = dev_get_drvdata(dev);
612 	unsigned int i;
613 	int err;
614 
615 	err = kstrtouint(buf, 10, &i);
616 	if (err)
617 		return err;
618 
619 	if (i)
620 		ads7846_disable(ts);
621 	else
622 		ads7846_enable(ts);
623 
624 	return count;
625 }
626 
627 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
628 
629 static struct attribute *ads784x_attributes[] = {
630 	&dev_attr_pen_down.attr,
631 	&dev_attr_disable.attr,
632 	NULL,
633 };
634 
635 static const struct attribute_group ads784x_attr_group = {
636 	.attrs = ads784x_attributes,
637 };
638 
639 /*--------------------------------------------------------------------------*/
640 
641 static void null_wait_for_sync(void)
642 {
643 }
644 
645 static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
646 {
647 	struct ads7846 *ts = ads;
648 
649 	if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
650 		/* Start over collecting consistent readings. */
651 		ts->read_rep = 0;
652 		/*
653 		 * Repeat it, if this was the first read or the read
654 		 * wasn't consistent enough.
655 		 */
656 		if (ts->read_cnt < ts->debounce_max) {
657 			ts->last_read = *val;
658 			ts->read_cnt++;
659 			return ADS7846_FILTER_REPEAT;
660 		} else {
661 			/*
662 			 * Maximum number of debouncing reached and still
663 			 * not enough number of consistent readings. Abort
664 			 * the whole sample, repeat it in the next sampling
665 			 * period.
666 			 */
667 			ts->read_cnt = 0;
668 			return ADS7846_FILTER_IGNORE;
669 		}
670 	} else {
671 		if (++ts->read_rep > ts->debounce_rep) {
672 			/*
673 			 * Got a good reading for this coordinate,
674 			 * go for the next one.
675 			 */
676 			ts->read_cnt = 0;
677 			ts->read_rep = 0;
678 			return ADS7846_FILTER_OK;
679 		} else {
680 			/* Read more values that are consistent. */
681 			ts->read_cnt++;
682 			return ADS7846_FILTER_REPEAT;
683 		}
684 	}
685 }
686 
687 static int ads7846_no_filter(void *ads, int data_idx, int *val)
688 {
689 	return ADS7846_FILTER_OK;
690 }
691 
692 static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)
693 {
694 	int value;
695 	struct spi_transfer *t =
696 		list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
697 	struct ads7846_buf *buf = t->rx_buf;
698 
699 	value = be16_to_cpup(&buf->data_be16);
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 	struct ads7846_buf *buf = t->rx_buf;
710 
711 	buf->data = val;
712 }
713 
714 static void ads7846_read_state(struct ads7846 *ts)
715 {
716 	struct ads7846_packet *packet = ts->packet;
717 	struct spi_message *m;
718 	int msg_idx = 0;
719 	int val;
720 	int action;
721 	int error;
722 
723 	while (msg_idx < ts->msg_count) {
724 
725 		ts->wait_for_sync();
726 
727 		m = &ts->msg[msg_idx];
728 		error = spi_sync(ts->spi, m);
729 		if (error) {
730 			dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
731 			packet->tc.ignore = true;
732 			return;
733 		}
734 
735 		/*
736 		 * Last message is power down request, no need to convert
737 		 * or filter the value.
738 		 */
739 		if (msg_idx < ts->msg_count - 1) {
740 
741 			val = ads7846_get_value(ts, m);
742 
743 			action = ts->filter(ts->filter_data, msg_idx, &val);
744 			switch (action) {
745 			case ADS7846_FILTER_REPEAT:
746 				continue;
747 
748 			case ADS7846_FILTER_IGNORE:
749 				packet->tc.ignore = true;
750 				msg_idx = ts->msg_count - 1;
751 				continue;
752 
753 			case ADS7846_FILTER_OK:
754 				ads7846_update_value(m, val);
755 				packet->tc.ignore = false;
756 				msg_idx++;
757 				break;
758 
759 			default:
760 				BUG();
761 			}
762 		} else {
763 			msg_idx++;
764 		}
765 	}
766 }
767 
768 static void ads7846_report_state(struct ads7846 *ts)
769 {
770 	struct ads7846_packet *packet = ts->packet;
771 	unsigned int Rt;
772 	u16 x, y, z1, z2;
773 
774 	/*
775 	 * ads7846_get_value() does in-place conversion (including byte swap)
776 	 * from on-the-wire format as part of debouncing to get stable
777 	 * readings.
778 	 */
779 	x = packet->tc.x.data;
780 	y = packet->tc.y.data;
781 	if (ts->model == 7845) {
782 		z1 = 0;
783 		z2 = 0;
784 	} else {
785 		z1 = packet->tc.z1.data;
786 		z2 = packet->tc.z2.data;
787 	}
788 
789 	/* range filtering */
790 	if (x == MAX_12BIT)
791 		x = 0;
792 
793 	if (ts->model == 7843) {
794 		Rt = ts->pressure_max / 2;
795 	} else if (ts->model == 7845) {
796 		if (get_pendown_state(ts))
797 			Rt = ts->pressure_max / 2;
798 		else
799 			Rt = 0;
800 		dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
801 	} else if (likely(x && z1)) {
802 		/* compute touch pressure resistance using equation #2 */
803 		Rt = z2;
804 		Rt -= z1;
805 		Rt *= ts->x_plate_ohms;
806 		Rt = DIV_ROUND_CLOSEST(Rt, 16);
807 		Rt *= x;
808 		Rt /= z1;
809 		Rt = DIV_ROUND_CLOSEST(Rt, 256);
810 	} else {
811 		Rt = 0;
812 	}
813 
814 	/*
815 	 * Sample found inconsistent by debouncing or pressure is beyond
816 	 * the maximum. Don't report it to user space, repeat at least
817 	 * once more the measurement
818 	 */
819 	if (packet->tc.ignore || Rt > ts->pressure_max) {
820 		dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
821 			 packet->tc.ignore, Rt);
822 		return;
823 	}
824 
825 	/*
826 	 * Maybe check the pendown state before reporting. This discards
827 	 * false readings when the pen is lifted.
828 	 */
829 	if (ts->penirq_recheck_delay_usecs) {
830 		udelay(ts->penirq_recheck_delay_usecs);
831 		if (!get_pendown_state(ts))
832 			Rt = 0;
833 	}
834 
835 	/*
836 	 * NOTE: We can't rely on the pressure to determine the pen down
837 	 * state, even this controller has a pressure sensor. The pressure
838 	 * value can fluctuate for quite a while after lifting the pen and
839 	 * in some cases may not even settle at the expected value.
840 	 *
841 	 * The only safe way to check for the pen up condition is in the
842 	 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
843 	 */
844 	if (Rt) {
845 		struct input_dev *input = ts->input;
846 
847 		if (!ts->pendown) {
848 			input_report_key(input, BTN_TOUCH, 1);
849 			ts->pendown = true;
850 			dev_vdbg(&ts->spi->dev, "DOWN\n");
851 		}
852 
853 		touchscreen_report_pos(input, &ts->core_prop, x, y, false);
854 		input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
855 
856 		input_sync(input);
857 		dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
858 	}
859 }
860 
861 static irqreturn_t ads7846_hard_irq(int irq, void *handle)
862 {
863 	struct ads7846 *ts = handle;
864 
865 	return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
866 }
867 
868 
869 static irqreturn_t ads7846_irq(int irq, void *handle)
870 {
871 	struct ads7846 *ts = handle;
872 
873 	/* Start with a small delay before checking pendown state */
874 	msleep(TS_POLL_DELAY);
875 
876 	while (!ts->stopped && get_pendown_state(ts)) {
877 
878 		/* pen is down, continue with the measurement */
879 		ads7846_read_state(ts);
880 
881 		if (!ts->stopped)
882 			ads7846_report_state(ts);
883 
884 		wait_event_timeout(ts->wait, ts->stopped,
885 				   msecs_to_jiffies(TS_POLL_PERIOD));
886 	}
887 
888 	if (ts->pendown && !ts->stopped)
889 		ads7846_report_pen_up(ts);
890 
891 	return IRQ_HANDLED;
892 }
893 
894 static int __maybe_unused ads7846_suspend(struct device *dev)
895 {
896 	struct ads7846 *ts = dev_get_drvdata(dev);
897 
898 	mutex_lock(&ts->lock);
899 
900 	if (!ts->suspended) {
901 
902 		if (!ts->disabled)
903 			__ads7846_disable(ts);
904 
905 		if (device_may_wakeup(&ts->spi->dev))
906 			enable_irq_wake(ts->spi->irq);
907 
908 		ts->suspended = true;
909 	}
910 
911 	mutex_unlock(&ts->lock);
912 
913 	return 0;
914 }
915 
916 static int __maybe_unused ads7846_resume(struct device *dev)
917 {
918 	struct ads7846 *ts = dev_get_drvdata(dev);
919 
920 	mutex_lock(&ts->lock);
921 
922 	if (ts->suspended) {
923 
924 		ts->suspended = false;
925 
926 		if (device_may_wakeup(&ts->spi->dev))
927 			disable_irq_wake(ts->spi->irq);
928 
929 		if (!ts->disabled)
930 			__ads7846_enable(ts);
931 	}
932 
933 	mutex_unlock(&ts->lock);
934 
935 	return 0;
936 }
937 
938 static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
939 
940 static int ads7846_setup_pendown(struct spi_device *spi,
941 				 struct ads7846 *ts,
942 				 const struct ads7846_platform_data *pdata)
943 {
944 	int err;
945 
946 	/*
947 	 * REVISIT when the irq can be triggered active-low, or if for some
948 	 * reason the touchscreen isn't hooked up, we don't need to access
949 	 * the pendown state.
950 	 */
951 
952 	if (pdata->get_pendown_state) {
953 		ts->get_pendown_state = pdata->get_pendown_state;
954 	} else if (gpio_is_valid(pdata->gpio_pendown)) {
955 
956 		err = gpio_request_one(pdata->gpio_pendown, GPIOF_IN,
957 				       "ads7846_pendown");
958 		if (err) {
959 			dev_err(&spi->dev,
960 				"failed to request/setup pendown GPIO%d: %d\n",
961 				pdata->gpio_pendown, err);
962 			return err;
963 		}
964 
965 		ts->gpio_pendown = pdata->gpio_pendown;
966 
967 		if (pdata->gpio_pendown_debounce)
968 			gpio_set_debounce(pdata->gpio_pendown,
969 					  pdata->gpio_pendown_debounce);
970 	} else {
971 		dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
972 		return -EINVAL;
973 	}
974 
975 	return 0;
976 }
977 
978 /*
979  * Set up the transfers to read touchscreen state; this assumes we
980  * use formula #2 for pressure, not #3.
981  */
982 static void ads7846_setup_spi_msg(struct ads7846 *ts,
983 				  const struct ads7846_platform_data *pdata)
984 {
985 	struct spi_message *m = &ts->msg[0];
986 	struct spi_transfer *x = ts->xfer;
987 	struct ads7846_packet *packet = ts->packet;
988 	int vref = pdata->keep_vref_on;
989 
990 	if (ts->model == 7873) {
991 		/*
992 		 * The AD7873 is almost identical to the ADS7846
993 		 * keep VREF off during differential/ratiometric
994 		 * conversion modes.
995 		 */
996 		ts->model = 7846;
997 		vref = 0;
998 	}
999 
1000 	ts->msg_count = 1;
1001 	spi_message_init(m);
1002 	m->context = ts;
1003 
1004 	packet->read_y_cmd.cmd = READ_Y(vref);
1005 	x->tx_buf = &packet->read_y_cmd;
1006 	x->rx_buf = &packet->tc.y;
1007 	x->len = 3;
1008 	spi_message_add_tail(x, m);
1009 
1010 	/*
1011 	 * The first sample after switching drivers can be low quality;
1012 	 * optionally discard it, using a second one after the signals
1013 	 * have had enough time to stabilize.
1014 	 */
1015 	if (pdata->settle_delay_usecs) {
1016 		x->delay.value = pdata->settle_delay_usecs;
1017 		x->delay.unit = SPI_DELAY_UNIT_USECS;
1018 		x++;
1019 
1020 		x->tx_buf = &packet->read_y_cmd;
1021 		x->rx_buf = &packet->tc.y;
1022 		x->len = 3;
1023 		spi_message_add_tail(x, m);
1024 	}
1025 
1026 	ts->msg_count++;
1027 	m++;
1028 	spi_message_init(m);
1029 	m->context = ts;
1030 
1031 	/* turn y- off, x+ on, then leave in lowpower */
1032 	x++;
1033 	packet->read_x_cmd.cmd = READ_X(vref);
1034 	x->tx_buf = &packet->read_x_cmd;
1035 	x->rx_buf = &packet->tc.x;
1036 	x->len = 3;
1037 	spi_message_add_tail(x, m);
1038 
1039 	/* ... maybe discard first sample ... */
1040 	if (pdata->settle_delay_usecs) {
1041 		x->delay.value = pdata->settle_delay_usecs;
1042 		x->delay.unit = SPI_DELAY_UNIT_USECS;
1043 
1044 		x++;
1045 		x->tx_buf = &packet->read_x_cmd;
1046 		x->rx_buf = &packet->tc.x;
1047 		x->len = 3;
1048 		spi_message_add_tail(x, m);
1049 	}
1050 
1051 	/* turn y+ off, x- on; we'll use formula #2 */
1052 	if (ts->model == 7846) {
1053 		ts->msg_count++;
1054 		m++;
1055 		spi_message_init(m);
1056 		m->context = ts;
1057 
1058 		x++;
1059 		packet->read_z1_cmd.cmd = READ_Z1(vref);
1060 		x->tx_buf = &packet->read_z1_cmd;
1061 		x->rx_buf = &packet->tc.z1;
1062 		x->len = 3;
1063 		spi_message_add_tail(x, m);
1064 
1065 		/* ... maybe discard first sample ... */
1066 		if (pdata->settle_delay_usecs) {
1067 			x->delay.value = pdata->settle_delay_usecs;
1068 			x->delay.unit = SPI_DELAY_UNIT_USECS;
1069 
1070 			x++;
1071 			x->tx_buf = &packet->read_z1_cmd;
1072 			x->rx_buf = &packet->tc.z1;
1073 			x->len = 3;
1074 			spi_message_add_tail(x, m);
1075 		}
1076 
1077 		ts->msg_count++;
1078 		m++;
1079 		spi_message_init(m);
1080 		m->context = ts;
1081 
1082 		x++;
1083 		packet->read_z2_cmd.cmd = READ_Z2(vref);
1084 		x->tx_buf = &packet->read_z2_cmd;
1085 		x->rx_buf = &packet->tc.z2;
1086 		x->len = 3;
1087 		spi_message_add_tail(x, m);
1088 
1089 		/* ... maybe discard first sample ... */
1090 		if (pdata->settle_delay_usecs) {
1091 			x->delay.value = pdata->settle_delay_usecs;
1092 			x->delay.unit = SPI_DELAY_UNIT_USECS;
1093 
1094 			x++;
1095 			x->tx_buf = &packet->read_z2_cmd;
1096 			x->rx_buf = &packet->tc.z2;
1097 			x->len = 3;
1098 			spi_message_add_tail(x, m);
1099 		}
1100 	}
1101 
1102 	/* power down */
1103 	ts->msg_count++;
1104 	m++;
1105 	spi_message_init(m);
1106 	m->context = ts;
1107 
1108 	x++;
1109 	packet->pwrdown_cmd.cmd = PWRDOWN;
1110 	x->tx_buf = &packet->pwrdown_cmd;
1111 	x->len = 3;
1112 
1113 	CS_CHANGE(*x);
1114 	spi_message_add_tail(x, m);
1115 }
1116 
1117 #ifdef CONFIG_OF
1118 static const struct of_device_id ads7846_dt_ids[] = {
1119 	{ .compatible = "ti,tsc2046",	.data = (void *) 7846 },
1120 	{ .compatible = "ti,ads7843",	.data = (void *) 7843 },
1121 	{ .compatible = "ti,ads7845",	.data = (void *) 7845 },
1122 	{ .compatible = "ti,ads7846",	.data = (void *) 7846 },
1123 	{ .compatible = "ti,ads7873",	.data = (void *) 7873 },
1124 	{ }
1125 };
1126 MODULE_DEVICE_TABLE(of, ads7846_dt_ids);
1127 
1128 static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1129 {
1130 	struct ads7846_platform_data *pdata;
1131 	struct device_node *node = dev->of_node;
1132 	const struct of_device_id *match;
1133 	u32 value;
1134 
1135 	if (!node) {
1136 		dev_err(dev, "Device does not have associated DT data\n");
1137 		return ERR_PTR(-EINVAL);
1138 	}
1139 
1140 	match = of_match_device(ads7846_dt_ids, dev);
1141 	if (!match) {
1142 		dev_err(dev, "Unknown device model\n");
1143 		return ERR_PTR(-EINVAL);
1144 	}
1145 
1146 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1147 	if (!pdata)
1148 		return ERR_PTR(-ENOMEM);
1149 
1150 	pdata->model = (unsigned long)match->data;
1151 
1152 	of_property_read_u16(node, "ti,vref-delay-usecs",
1153 			     &pdata->vref_delay_usecs);
1154 	of_property_read_u16(node, "ti,vref-mv", &pdata->vref_mv);
1155 	pdata->keep_vref_on = of_property_read_bool(node, "ti,keep-vref-on");
1156 
1157 	pdata->swap_xy = of_property_read_bool(node, "ti,swap-xy");
1158 
1159 	of_property_read_u16(node, "ti,settle-delay-usec",
1160 			     &pdata->settle_delay_usecs);
1161 	of_property_read_u16(node, "ti,penirq-recheck-delay-usecs",
1162 			     &pdata->penirq_recheck_delay_usecs);
1163 
1164 	of_property_read_u16(node, "ti,x-plate-ohms", &pdata->x_plate_ohms);
1165 	of_property_read_u16(node, "ti,y-plate-ohms", &pdata->y_plate_ohms);
1166 
1167 	of_property_read_u16(node, "ti,x-min", &pdata->x_min);
1168 	of_property_read_u16(node, "ti,y-min", &pdata->y_min);
1169 	of_property_read_u16(node, "ti,x-max", &pdata->x_max);
1170 	of_property_read_u16(node, "ti,y-max", &pdata->y_max);
1171 
1172 	/*
1173 	 * touchscreen-max-pressure gets parsed during
1174 	 * touchscreen_parse_properties()
1175 	 */
1176 	of_property_read_u16(node, "ti,pressure-min", &pdata->pressure_min);
1177 	if (!of_property_read_u32(node, "touchscreen-min-pressure", &value))
1178 		pdata->pressure_min = (u16) value;
1179 	of_property_read_u16(node, "ti,pressure-max", &pdata->pressure_max);
1180 
1181 	of_property_read_u16(node, "ti,debounce-max", &pdata->debounce_max);
1182 	if (!of_property_read_u32(node, "touchscreen-average-samples", &value))
1183 		pdata->debounce_max = (u16) value;
1184 	of_property_read_u16(node, "ti,debounce-tol", &pdata->debounce_tol);
1185 	of_property_read_u16(node, "ti,debounce-rep", &pdata->debounce_rep);
1186 
1187 	of_property_read_u32(node, "ti,pendown-gpio-debounce",
1188 			     &pdata->gpio_pendown_debounce);
1189 
1190 	pdata->wakeup = of_property_read_bool(node, "wakeup-source") ||
1191 			of_property_read_bool(node, "linux,wakeup");
1192 
1193 	pdata->gpio_pendown = of_get_named_gpio(dev->of_node, "pendown-gpio", 0);
1194 
1195 	return pdata;
1196 }
1197 #else
1198 static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1199 {
1200 	dev_err(dev, "no platform data defined\n");
1201 	return ERR_PTR(-EINVAL);
1202 }
1203 #endif
1204 
1205 static int ads7846_probe(struct spi_device *spi)
1206 {
1207 	const struct ads7846_platform_data *pdata;
1208 	struct ads7846 *ts;
1209 	struct ads7846_packet *packet;
1210 	struct input_dev *input_dev;
1211 	unsigned long irq_flags;
1212 	int err;
1213 
1214 	if (!spi->irq) {
1215 		dev_dbg(&spi->dev, "no IRQ?\n");
1216 		return -EINVAL;
1217 	}
1218 
1219 	/* don't exceed max specified sample rate */
1220 	if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
1221 		dev_err(&spi->dev, "f(sample) %d KHz?\n",
1222 				(spi->max_speed_hz/SAMPLE_BITS)/1000);
1223 		return -EINVAL;
1224 	}
1225 
1226 	/*
1227 	 * We'd set TX word size 8 bits and RX word size to 13 bits ... except
1228 	 * that even if the hardware can do that, the SPI controller driver
1229 	 * may not.  So we stick to very-portable 8 bit words, both RX and TX.
1230 	 */
1231 	spi->bits_per_word = 8;
1232 	spi->mode &= ~SPI_MODE_X_MASK;
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