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