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