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