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