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