blob: 03e527c16c23f54b2e6ed31d39e47b2b5ffb07d2 [file] [log] [blame]
David Brownellffa458c2006-01-08 13:34:21 -08001/*
2 * ADS7846 based touchscreen and sensor driver
3 *
4 * Copyright (c) 2005 David Brownell
Imre Deak7de90a82006-04-11 23:43:55 -04005 * Copyright (c) 2006 Nokia Corporation
6 * Various changes: Imre Deak <imre.deak@nokia.com>
David Brownellffa458c2006-01-08 13:34:21 -08007 *
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/device.h>
21#include <linux/init.h>
22#include <linux/delay.h>
23#include <linux/input.h>
24#include <linux/interrupt.h>
25#include <linux/slab.h>
26#include <linux/spi/spi.h>
27#include <linux/spi/ads7846.h>
Andrew Morton3ac8bf02006-03-26 01:37:33 -080028#include <asm/irq.h>
David Brownellffa458c2006-01-08 13:34:21 -080029
30#ifdef CONFIG_ARM
31#include <asm/mach-types.h>
32#ifdef CONFIG_ARCH_OMAP
33#include <asm/arch/gpio.h>
34#endif
David Brownellffa458c2006-01-08 13:34:21 -080035#endif
36
37
38/*
David Brownell90845332006-05-25 18:44:20 -070039 * This code has been heavily tested on a Nokia 770, and lightly
40 * tested on other ads7846 devices (OSK/Mistral, Lubbock).
David Brownellffa458c2006-01-08 13:34:21 -080041 * Support for ads7843 and ads7845 has only been stubbed in.
42 *
Imre Deak7de90a82006-04-11 23:43:55 -040043 * IRQ handling needs a workaround because of a shortcoming in handling
44 * edge triggered IRQs on some platforms like the OMAP1/2. These
45 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
46 * have to maintain our own SW IRQ disabled status. This should be
47 * removed as soon as the affected platform's IRQ handling is fixed.
48 *
David Brownellffa458c2006-01-08 13:34:21 -080049 * app note sbaa036 talks in more detail about accurate sampling...
50 * that ought to help in situations like LCDs inducing noise (which
51 * can also be helped by using synch signals) and more generally.
Imre Deak7de90a82006-04-11 23:43:55 -040052 * This driver tries to utilize the measures described in the app
53 * note. The strength of filtering can be set in the board-* specific
54 * files.
David Brownellffa458c2006-01-08 13:34:21 -080055 */
56
57#define TS_POLL_PERIOD msecs_to_jiffies(10)
58
David Brownelld93f70b2006-02-15 00:49:35 -050059/* this driver doesn't aim at the peak continuous sample rate */
60#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
61
David Brownellffa458c2006-01-08 13:34:21 -080062struct ts_event {
63 /* For portability, we can't read 12 bit values using SPI (which
64 * would make the controller deliver them as native byteorder u16
David Brownelld93f70b2006-02-15 00:49:35 -050065 * with msbs zeroed). Instead, we read them as two 8-bit values,
Imre Deakda970e62007-01-18 00:44:41 -050066 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
David Brownellffa458c2006-01-08 13:34:21 -080067 */
Imre Deakda970e62007-01-18 00:44:41 -050068 u16 x;
69 u16 y;
70 u16 z1, z2;
Imre Deakd5b415c2006-04-26 00:13:18 -040071 int ignore;
David Brownellffa458c2006-01-08 13:34:21 -080072};
73
74struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050075 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080076 char phys[32];
77
78 struct spi_device *spi;
Dmitry Torokhov8dd51652006-11-02 23:34:09 -050079 struct attribute_group *attr_group;
David Brownellffa458c2006-01-08 13:34:21 -080080 u16 model;
81 u16 vref_delay_usecs;
82 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -040083 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -080084
Imre Deak53a0ef892006-04-11 23:41:49 -040085 u8 read_x, read_y, read_z1, read_z2, pwrdown;
86 u16 dummy; /* for the pwrdown read */
David Brownellffa458c2006-01-08 13:34:21 -080087 struct ts_event tc;
88
Imre Deak53a0ef892006-04-11 23:41:49 -040089 struct spi_transfer xfer[10];
Imre Deak0b7018a2006-04-11 23:42:03 -040090 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -040091 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -040092 int msg_idx;
93 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -040094 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -040095 int last_read;
96
97 u16 debounce_max;
98 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -040099 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800100
101 spinlock_t lock;
102 struct timer_list timer; /* P: lock */
103 unsigned pendown:1; /* P: lock */
104 unsigned pending:1; /* P: lock */
105// FIXME remove "irq_disabled"
106 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400107 unsigned disabled:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400108
Imre Deakda970e62007-01-18 00:44:41 -0500109 int (*filter)(void *data, int data_idx, int *val);
110 void *filter_data;
111 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400112 int (*get_pendown_state)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800113};
114
115/* leave chip selected when we're done, for quicker re-select? */
116#if 0
117#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
118#else
119#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
120#endif
121
122/*--------------------------------------------------------------------------*/
123
124/* The ADS7846 has touchscreen and other sensors.
125 * Earlier ads784x chips are somewhat compatible.
126 */
127#define ADS_START (1 << 7)
128#define ADS_A2A1A0_d_y (1 << 4) /* differential */
129#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
130#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
131#define ADS_A2A1A0_d_x (5 << 4) /* differential */
132#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
133#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
134#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
135#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
136#define ADS_8_BIT (1 << 3)
137#define ADS_12_BIT (0 << 3)
138#define ADS_SER (1 << 2) /* non-differential */
139#define ADS_DFR (0 << 2) /* differential */
140#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
141#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
142#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
143#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
144
145#define MAX_12BIT ((1<<12)-1)
146
147/* leave ADC powered up (disables penirq) between differential samples */
148#define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
149 | ADS_12_BIT | ADS_DFR)
150
David Brownelld93f70b2006-02-15 00:49:35 -0500151#define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON)
152#define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
153#define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
Imre Deak53a0ef892006-04-11 23:41:49 -0400154
155#define READ_X (READ_12BIT_DFR(x) | ADS_PD10_ADC_ON)
156#define PWRDOWN (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800157
158/* single-ended samples need to first power up reference voltage;
159 * we leave both ADC and VREF powered
160 */
161#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
162 | ADS_12_BIT | ADS_SER)
163
David Brownelld93f70b2006-02-15 00:49:35 -0500164#define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
165#define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
David Brownellffa458c2006-01-08 13:34:21 -0800166
167/*--------------------------------------------------------------------------*/
168
169/*
170 * Non-touchscreen sensors only use single-ended conversions.
171 */
172
173struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500174 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800175 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500176 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800177 u16 scratch;
178 __be16 sample;
179 struct spi_message msg;
180 struct spi_transfer xfer[6];
181};
182
Imre Deak7de90a82006-04-11 23:43:55 -0400183static void ads7846_enable(struct ads7846 *ts);
184static void ads7846_disable(struct ads7846 *ts);
185
Imre Deakc9e617a2006-04-11 23:44:05 -0400186static int device_suspended(struct device *dev)
187{
188 struct ads7846 *ts = dev_get_drvdata(dev);
189 return dev->power.power_state.event != PM_EVENT_ON || ts->disabled;
190}
191
David Brownellffa458c2006-01-08 13:34:21 -0800192static int ads7846_read12_ser(struct device *dev, unsigned command)
193{
194 struct spi_device *spi = to_spi_device(dev);
195 struct ads7846 *ts = dev_get_drvdata(dev);
Christoph Lametere94b1762006-12-06 20:33:17 -0800196 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800197 int status;
198 int sample;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500199 int i;
David Brownellffa458c2006-01-08 13:34:21 -0800200
201 if (!req)
202 return -ENOMEM;
203
Imre Deak0b7018a2006-04-11 23:42:03 -0400204 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800205
David Brownellffa458c2006-01-08 13:34:21 -0800206 /* activate reference, so it has time to settle; */
David Brownelld93f70b2006-02-15 00:49:35 -0500207 req->ref_on = REF_ON;
208 req->xfer[0].tx_buf = &req->ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800209 req->xfer[0].len = 1;
210 req->xfer[1].rx_buf = &req->scratch;
211 req->xfer[1].len = 2;
212
213 /*
214 * for external VREF, 0 usec (and assume it's always on);
215 * for 1uF, use 800 usec;
216 * no cap, 100 usec.
217 */
218 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
219
220 /* take sample */
221 req->command = (u8) command;
222 req->xfer[2].tx_buf = &req->command;
223 req->xfer[2].len = 1;
224 req->xfer[3].rx_buf = &req->sample;
225 req->xfer[3].len = 2;
226
227 /* REVISIT: take a few more samples, and compare ... */
228
229 /* turn off reference */
David Brownelld93f70b2006-02-15 00:49:35 -0500230 req->ref_off = REF_OFF;
231 req->xfer[4].tx_buf = &req->ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800232 req->xfer[4].len = 1;
233 req->xfer[5].rx_buf = &req->scratch;
234 req->xfer[5].len = 2;
235
236 CS_CHANGE(req->xfer[5]);
237
238 /* group all the transfers together, so we can't interfere with
239 * reading touchscreen state; disable penirq while sampling
240 */
Vitaly Wool8275c642006-01-08 13:34:28 -0800241 for (i = 0; i < 6; i++)
242 spi_message_add_tail(&req->xfer[i], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800243
Imre Deakc9e617a2006-04-11 23:44:05 -0400244 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800245 disable_irq(spi->irq);
246 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400247 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800248 enable_irq(spi->irq);
249
250 if (req->msg.status)
251 status = req->msg.status;
David Brownellffa458c2006-01-08 13:34:21 -0800252
David Brownell90845332006-05-25 18:44:20 -0700253 /* on-wire is a must-ignore bit, a BE12 value, then padding */
254 sample = be16_to_cpu(req->sample);
255 sample = sample >> 3;
256 sample &= 0x0fff;
257
258 kfree(req);
David Brownellffa458c2006-01-08 13:34:21 -0800259 return status ? status : sample;
260}
261
262#define SHOW(name) static ssize_t \
263name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
264{ \
265 ssize_t v = ads7846_read12_ser(dev, \
266 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
267 if (v < 0) \
268 return v; \
269 return sprintf(buf, "%u\n", (unsigned) v); \
270} \
271static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
272
273SHOW(temp0)
274SHOW(temp1)
275SHOW(vaux)
276SHOW(vbatt)
277
Imre Deak438f2a72006-04-11 23:41:32 -0400278static int is_pen_down(struct device *dev)
279{
280 struct ads7846 *ts = dev_get_drvdata(dev);
281
282 return ts->pendown;
283}
284
285static ssize_t ads7846_pen_down_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
288 return sprintf(buf, "%u\n", is_pen_down(dev));
289}
290
291static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
292
Imre Deak7de90a82006-04-11 23:43:55 -0400293static ssize_t ads7846_disable_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
295{
296 struct ads7846 *ts = dev_get_drvdata(dev);
297
298 return sprintf(buf, "%u\n", ts->disabled);
299}
300
301static ssize_t ads7846_disable_store(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
304{
305 struct ads7846 *ts = dev_get_drvdata(dev);
306 char *endp;
307 int i;
308
309 i = simple_strtoul(buf, &endp, 10);
310 spin_lock_irq(&ts->lock);
311
312 if (i)
313 ads7846_disable(ts);
314 else
315 ads7846_enable(ts);
316
317 spin_unlock_irq(&ts->lock);
318
319 return count;
320}
321
322static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
323
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500324static struct attribute *ads7846_attributes[] = {
325 &dev_attr_temp0.attr,
326 &dev_attr_temp1.attr,
327 &dev_attr_vbatt.attr,
328 &dev_attr_vaux.attr,
329 &dev_attr_pen_down.attr,
330 &dev_attr_disable.attr,
331 NULL,
332};
333
334static struct attribute_group ads7846_attr_group = {
335 .attrs = ads7846_attributes,
336};
337
338/*
339 * ads7843/7845 don't have temperature sensors, and
340 * use the other sensors a bit differently too
341 */
342
343static struct attribute *ads7843_attributes[] = {
344 &dev_attr_vbatt.attr,
345 &dev_attr_vaux.attr,
346 &dev_attr_pen_down.attr,
347 &dev_attr_disable.attr,
348 NULL,
349};
350
351static struct attribute_group ads7843_attr_group = {
352 .attrs = ads7843_attributes,
353};
354
355static struct attribute *ads7845_attributes[] = {
356 &dev_attr_vaux.attr,
357 &dev_attr_pen_down.attr,
358 &dev_attr_disable.attr,
359 NULL,
360};
361
362static struct attribute_group ads7845_attr_group = {
363 .attrs = ads7845_attributes,
364};
365
David Brownellffa458c2006-01-08 13:34:21 -0800366/*--------------------------------------------------------------------------*/
367
368/*
369 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
370 * to retrieve touchscreen status.
371 *
372 * The SPI transfer completion callback does the real work. It reports
373 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
374 */
375
376static void ads7846_rx(void *ads)
377{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500378 struct ads7846 *ts = ads;
379 struct input_dev *input_dev = ts->input;
380 unsigned Rt;
381 unsigned sync = 0;
382 u16 x, y, z1, z2;
383 unsigned long flags;
David Brownellffa458c2006-01-08 13:34:21 -0800384
Imre Deakda970e62007-01-18 00:44:41 -0500385 /* ads7846_rx_val() did in-place conversion (including byteswap) from
386 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800387 */
Imre Deakda970e62007-01-18 00:44:41 -0500388 x = ts->tc.x;
389 y = ts->tc.y;
390 z1 = ts->tc.z1;
391 z2 = ts->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800392
393 /* range filtering */
394 if (x == MAX_12BIT)
395 x = 0;
396
Imre Deakc9e617a2006-04-11 23:44:05 -0400397 if (likely(x && z1 && !device_suspended(&ts->spi->dev))) {
David Brownellffa458c2006-01-08 13:34:21 -0800398 /* compute touch pressure resistance using equation #2 */
399 Rt = z2;
400 Rt -= z1;
401 Rt *= x;
402 Rt *= ts->x_plate_ohms;
403 Rt /= z1;
404 Rt = (Rt + 2047) >> 12;
405 } else
406 Rt = 0;
407
Imre Deakd5b415c2006-04-26 00:13:18 -0400408 /* Sample found inconsistent by debouncing or pressure is beyond
409 * the maximum. Don't report it to user space, repeat at least
410 * once more the measurement */
411 if (ts->tc.ignore || Rt > ts->pressure_max) {
412 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
413 return;
414 }
415
David Brownellffa458c2006-01-08 13:34:21 -0800416 /* NOTE: "pendown" is inferred from pressure; we don't rely on
417 * being able to check nPENIRQ status, or "friendly" trigger modes
418 * (both-edges is much better than just-falling or low-level).
419 *
420 * REVISIT: some boards may require reading nPENIRQ; it's
421 * needed on 7843. and 7845 reads pressure differently...
422 *
423 * REVISIT: the touchscreen might not be connected; this code
424 * won't notice that, even if nPENIRQ never fires ...
425 */
426 if (!ts->pendown && Rt != 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500427 input_report_key(input_dev, BTN_TOUCH, 1);
David Brownellffa458c2006-01-08 13:34:21 -0800428 sync = 1;
429 } else if (ts->pendown && Rt == 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500430 input_report_key(input_dev, BTN_TOUCH, 0);
David Brownellffa458c2006-01-08 13:34:21 -0800431 sync = 1;
432 }
433
434 if (Rt) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500435 input_report_abs(input_dev, ABS_X, x);
436 input_report_abs(input_dev, ABS_Y, y);
David Brownellffa458c2006-01-08 13:34:21 -0800437 sync = 1;
438 }
Imre Deakae82d5a2006-04-26 00:12:14 -0400439
440 if (sync) {
441 input_report_abs(input_dev, ABS_PRESSURE, Rt);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500442 input_sync(input_dev);
Imre Deakae82d5a2006-04-26 00:12:14 -0400443 }
David Brownellffa458c2006-01-08 13:34:21 -0800444
445#ifdef VERBOSE
446 if (Rt || ts->pendown)
447 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
448 x, y, Rt, Rt ? "" : " UP");
449#endif
450
David Brownellffa458c2006-01-08 13:34:21 -0800451 spin_lock_irqsave(&ts->lock, flags);
452
453 ts->pendown = (Rt != 0);
Imre Deakc9e617a2006-04-11 23:44:05 -0400454 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
David Brownellffa458c2006-01-08 13:34:21 -0800455
456 spin_unlock_irqrestore(&ts->lock, flags);
457}
458
Imre Deakda970e62007-01-18 00:44:41 -0500459static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400460{
461 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400462
Imre Deakda970e62007-01-18 00:44:41 -0500463 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
464 /* Start over collecting consistent readings. */
465 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400466 /* Repeat it, if this was the first read or the read
467 * wasn't consistent enough. */
468 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500469 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400470 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500471 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400472 } else {
473 /* Maximum number of debouncing reached and still
474 * not enough number of consistent readings. Abort
475 * the whole sample, repeat it in the next sampling
476 * period.
477 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400478 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500479 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400480 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400481 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400482 if (++ts->read_rep > ts->debounce_rep) {
483 /* Got a good reading for this coordinate,
484 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400485 ts->read_cnt = 0;
486 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500487 return ADS7846_FILTER_OK;
488 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400489 /* Read more values that are consistent. */
490 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500491 return ADS7846_FILTER_REPEAT;
492 }
493 }
494}
495
496static int ads7846_no_filter(void *ads, int data_idx, int *val)
497{
498 return ADS7846_FILTER_OK;
499}
500
501static void ads7846_rx_val(void *ads)
502{
503 struct ads7846 *ts = ads;
504 struct spi_message *m;
505 struct spi_transfer *t;
506 u16 *rx_val;
507 int val;
508 int action;
509 int status;
510
511 m = &ts->msg[ts->msg_idx];
512 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
513 rx_val = t->rx_buf;
514
515 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
516 * built from two 8 bit values written msb-first.
517 */
518 val = be16_to_cpu(*rx_val) >> 3;
519
520 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
521 switch (action) {
522 case ADS7846_FILTER_REPEAT:
523 break;
524 case ADS7846_FILTER_IGNORE:
525 ts->tc.ignore = 1;
526 /* Last message will contain ads7846_rx() as the
527 * completion function.
528 */
529 m = ts->last_msg;
530 break;
531 case ADS7846_FILTER_OK:
532 *rx_val = val;
533 ts->tc.ignore = 0;
534 m = &ts->msg[++ts->msg_idx];
535 break;
536 default:
537 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400538 }
539 status = spi_async(ts->spi, m);
540 if (status)
541 dev_err(&ts->spi->dev, "spi_async --> %d\n",
542 status);
543}
544
David Brownellffa458c2006-01-08 13:34:21 -0800545static void ads7846_timer(unsigned long handle)
546{
547 struct ads7846 *ts = (void *)handle;
548 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800549
Imre Deakc9e617a2006-04-11 23:44:05 -0400550 spin_lock_irq(&ts->lock);
551
552 if (unlikely(ts->msg_idx && !ts->pendown)) {
David Brownell90845332006-05-25 18:44:20 -0700553 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400554 if (!device_suspended(&ts->spi->dev)) {
555 ts->irq_disabled = 0;
556 enable_irq(ts->spi->irq);
557 }
558 ts->pending = 0;
559 ts->msg_idx = 0;
560 } else {
561 /* pen is still down, continue with the measurement */
562 ts->msg_idx = 0;
563 status = spi_async(ts->spi, &ts->msg[0]);
564 if (status)
565 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
566 }
567
568 spin_unlock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800569}
570
David Howells7d12e782006-10-05 14:55:46 +0100571static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800572{
Imre Deak0b7018a2006-04-11 23:42:03 -0400573 struct ads7846 *ts = handle;
574 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400575
576 spin_lock_irqsave(&ts->lock, flags);
Imre Deakc9e617a2006-04-11 23:44:05 -0400577 if (likely(ts->get_pendown_state())) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400578 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700579 /* The ARM do_simple_IRQ() dispatcher doesn't act
580 * like the other dispatchers: it will report IRQs
581 * even after they've been disabled. We work around
582 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400583 */
584 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400585 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400586 ts->pending = 1;
587 mod_timer(&ts->timer, jiffies);
588 }
589 }
590 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400591
592 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800593}
594
595/*--------------------------------------------------------------------------*/
596
Imre Deak7de90a82006-04-11 23:43:55 -0400597/* Must be called with ts->lock held */
598static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800599{
Imre Deak7de90a82006-04-11 23:43:55 -0400600 if (ts->disabled)
601 return;
David Brownellffa458c2006-01-08 13:34:21 -0800602
Imre Deakc9e617a2006-04-11 23:44:05 -0400603 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800604
Imre Deakc9e617a2006-04-11 23:44:05 -0400605 /* are we waiting for IRQ, or polling? */
606 if (!ts->pending) {
607 ts->irq_disabled = 1;
608 disable_irq(ts->spi->irq);
609 } else {
610 /* the timer will run at least once more, and
611 * leave everything in a clean state, IRQ disabled
612 */
613 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400614 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400615 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400616 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800617 }
618 }
619
620 /* we know the chip's in lowpower mode since we always
621 * leave it that way after every request
622 */
623
Imre Deak7de90a82006-04-11 23:43:55 -0400624}
625
626/* Must be called with ts->lock held */
627static void ads7846_enable(struct ads7846 *ts)
628{
629 if (!ts->disabled)
630 return;
631
632 ts->disabled = 0;
633 ts->irq_disabled = 0;
634 enable_irq(ts->spi->irq);
635}
636
637static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
638{
639 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
640
641 spin_lock_irq(&ts->lock);
642
643 spi->dev.power.power_state = message;
644 ads7846_disable(ts);
645
646 spin_unlock_irq(&ts->lock);
647
David Brownellffa458c2006-01-08 13:34:21 -0800648 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400649
David Brownellffa458c2006-01-08 13:34:21 -0800650}
651
David Brownell2e5a7bd2006-01-08 13:34:25 -0800652static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800653{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800654 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800655
Imre Deak7de90a82006-04-11 23:43:55 -0400656 spin_lock_irq(&ts->lock);
657
David Brownell2e5a7bd2006-01-08 13:34:25 -0800658 spi->dev.power.power_state = PMSG_ON;
Imre Deak7de90a82006-04-11 23:43:55 -0400659 ads7846_enable(ts);
660
661 spin_unlock_irq(&ts->lock);
662
David Brownellffa458c2006-01-08 13:34:21 -0800663 return 0;
664}
665
David Brownell2e5a7bd2006-01-08 13:34:25 -0800666static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800667{
David Brownellffa458c2006-01-08 13:34:21 -0800668 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500669 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800670 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400671 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800672 struct spi_transfer *x;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500673 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800674
675 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800676 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800677 return -ENODEV;
678 }
679
680 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800681 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800682 return -ENODEV;
683 }
684
685 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500686 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800687 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500688 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800689 return -EINVAL;
690 }
691
David Brownell90845332006-05-25 18:44:20 -0700692 /* REVISIT when the irq can be triggered active-low, or if for some
693 * reason the touchscreen isn't hooked up, we don't need to access
694 * the pendown state.
695 */
Imre Deakc9e617a2006-04-11 23:44:05 -0400696 if (pdata->get_pendown_state == NULL) {
697 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
698 return -EINVAL;
699 }
700
David Brownell90845332006-05-25 18:44:20 -0700701 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
702 * that even if the hardware can do that, the SPI controller driver
703 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800704 */
David Brownell90845332006-05-25 18:44:20 -0700705 spi->bits_per_word = 8;
David Brownellffa458c2006-01-08 13:34:21 -0800706
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500707 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
708 input_dev = input_allocate_device();
709 if (!ts || !input_dev) {
710 err = -ENOMEM;
711 goto err_free_mem;
712 }
David Brownellffa458c2006-01-08 13:34:21 -0800713
David Brownell2e5a7bd2006-01-08 13:34:25 -0800714 dev_set_drvdata(&spi->dev, ts);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500715 spi->dev.power.power_state = PMSG_ON;
David Brownellffa458c2006-01-08 13:34:21 -0800716
717 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500718 ts->input = input_dev;
David Brownellffa458c2006-01-08 13:34:21 -0800719
720 init_timer(&ts->timer);
721 ts->timer.data = (unsigned long) ts;
722 ts->timer.function = ads7846_timer;
723
Imre Deak7de90a82006-04-11 23:43:55 -0400724 spin_lock_init(&ts->lock);
725
David Brownellffa458c2006-01-08 13:34:21 -0800726 ts->model = pdata->model ? : 7846;
727 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
728 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400729 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500730
731 if (pdata->filter != NULL) {
732 if (pdata->filter_init != NULL) {
733 err = pdata->filter_init(pdata, &ts->filter_data);
734 if (err < 0)
735 goto err_free_mem;
736 }
737 ts->filter = pdata->filter;
738 ts->filter_cleanup = pdata->filter_cleanup;
739 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400740 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500741 if (ts->debounce_max < 2)
742 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400743 ts->debounce_tol = pdata->debounce_tol;
744 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500745 ts->filter = ads7846_debounce;
746 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400747 } else
Imre Deakda970e62007-01-18 00:44:41 -0500748 ts->filter = ads7846_no_filter;
Imre Deakc9e617a2006-04-11 23:44:05 -0400749 ts->get_pendown_state = pdata->get_pendown_state;
David Brownellffa458c2006-01-08 13:34:21 -0800750
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500751 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800752
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500753 input_dev->name = "ADS784x Touchscreen";
754 input_dev->phys = ts->phys;
755 input_dev->cdev.dev = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800756
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500757 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
758 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
759 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800760 pdata->x_min ? : 0,
761 pdata->x_max ? : MAX_12BIT,
762 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500763 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800764 pdata->y_min ? : 0,
765 pdata->y_max ? : MAX_12BIT,
766 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500767 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800768 pdata->pressure_min, pdata->pressure_max, 0, 0);
769
David Brownellffa458c2006-01-08 13:34:21 -0800770 /* set up the transfers to read touchscreen state; this assumes we
771 * use formula #2 for pressure, not #3.
772 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400773 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800774 x = ts->xfer;
775
Imre Deak0b7018a2006-04-11 23:42:03 -0400776 spi_message_init(m);
777
David Brownellffa458c2006-01-08 13:34:21 -0800778 /* y- still on; turn on only y+ (and ADC) */
David Brownelld93f70b2006-02-15 00:49:35 -0500779 ts->read_y = READ_Y;
780 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800781 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400782 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500783
David Brownellffa458c2006-01-08 13:34:21 -0800784 x++;
785 x->rx_buf = &ts->tc.y;
786 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400787 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800788
Imre Deakda970e62007-01-18 00:44:41 -0500789 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400790 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500791
Imre Deak0b7018a2006-04-11 23:42:03 -0400792 m++;
793 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800794
795 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500796 x++;
797 ts->read_x = READ_X;
798 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800799 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400800 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500801
David Brownellffa458c2006-01-08 13:34:21 -0800802 x++;
803 x->rx_buf = &ts->tc.x;
804 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400805 spi_message_add_tail(x, m);
806
Imre Deakda970e62007-01-18 00:44:41 -0500807 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400808 m->context = ts;
809
810 /* turn y+ off, x- on; we'll use formula #2 */
811 if (ts->model == 7846) {
812 m++;
813 spi_message_init(m);
814
815 x++;
816 ts->read_z1 = READ_Z1;
817 x->tx_buf = &ts->read_z1;
818 x->len = 1;
819 spi_message_add_tail(x, m);
820
821 x++;
822 x->rx_buf = &ts->tc.z1;
823 x->len = 2;
824 spi_message_add_tail(x, m);
825
Imre Deakda970e62007-01-18 00:44:41 -0500826 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400827 m->context = ts;
828
829 m++;
830 spi_message_init(m);
831
832 x++;
833 ts->read_z2 = READ_Z2;
834 x->tx_buf = &ts->read_z2;
835 x->len = 1;
836 spi_message_add_tail(x, m);
837
838 x++;
839 x->rx_buf = &ts->tc.z2;
840 x->len = 2;
841 spi_message_add_tail(x, m);
842
Imre Deakda970e62007-01-18 00:44:41 -0500843 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400844 m->context = ts;
845 }
Imre Deak53a0ef892006-04-11 23:41:49 -0400846
847 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -0400848 m++;
849 spi_message_init(m);
850
Imre Deak53a0ef892006-04-11 23:41:49 -0400851 x++;
852 ts->pwrdown = PWRDOWN;
853 x->tx_buf = &ts->pwrdown;
854 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400855 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -0400856
857 x++;
858 x->rx_buf = &ts->dummy;
859 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500860 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -0400861 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800862
Imre Deak0b7018a2006-04-11 23:42:03 -0400863 m->complete = ads7846_rx;
864 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -0800865
Imre Deakd5b415c2006-04-26 00:13:18 -0400866 ts->last_msg = m;
867
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700868 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -0700869 spi->dev.driver->name, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800870 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500871 err = -EBUSY;
Imre Deakda970e62007-01-18 00:44:41 -0500872 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -0800873 }
David Brownellffa458c2006-01-08 13:34:21 -0800874
David Brownell2e5a7bd2006-01-08 13:34:25 -0800875 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -0800876
877 /* take a first sample, leaving nPENIRQ active; avoid
878 * the touchscreen, in case it's not connected.
879 */
David Brownell2e5a7bd2006-01-08 13:34:25 -0800880 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -0800881 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
882
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500883 switch (ts->model) {
884 case 7846:
885 ts->attr_group = &ads7846_attr_group;
886 break;
887 case 7845:
888 ts->attr_group = &ads7845_attr_group;
889 break;
890 default:
891 ts->attr_group = &ads7843_attr_group;
892 break;
David Brownellffa458c2006-01-08 13:34:21 -0800893 }
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500894 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
895 if (err)
896 goto err_free_irq;
Imre Deak7de90a82006-04-11 23:43:55 -0400897
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500898 err = input_register_device(input_dev);
899 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500900 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500901
David Brownellffa458c2006-01-08 13:34:21 -0800902 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500903
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500904 err_remove_attr_group:
905 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
906 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500907 free_irq(spi->irq, ts);
Imre Deakda970e62007-01-18 00:44:41 -0500908 err_cleanup_filter:
909 if (ts->filter_cleanup)
910 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500911 err_free_mem:
912 input_free_device(input_dev);
913 kfree(ts);
914 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800915}
916
David Brownell2e5a7bd2006-01-08 13:34:25 -0800917static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800918{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800919 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800920
Imre Deak7de90a82006-04-11 23:43:55 -0400921 input_unregister_device(ts->input);
922
David Brownell2e5a7bd2006-01-08 13:34:25 -0800923 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -0800924
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500925 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
David Brownellffa458c2006-01-08 13:34:21 -0800926
Imre Deak7de90a82006-04-11 23:43:55 -0400927 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -0400928 /* suspend left the IRQ disabled */
929 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -0400930
Imre Deakda970e62007-01-18 00:44:41 -0500931 if (ts->filter_cleanup)
932 ts->filter_cleanup(ts->filter_data);
933
David Brownellffa458c2006-01-08 13:34:21 -0800934 kfree(ts);
935
David Brownell2e5a7bd2006-01-08 13:34:25 -0800936 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -0800937 return 0;
938}
939
David Brownell2e5a7bd2006-01-08 13:34:25 -0800940static struct spi_driver ads7846_driver = {
941 .driver = {
942 .name = "ads7846",
943 .bus = &spi_bus_type,
944 .owner = THIS_MODULE,
945 },
David Brownellffa458c2006-01-08 13:34:21 -0800946 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -0800947 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -0800948 .suspend = ads7846_suspend,
949 .resume = ads7846_resume,
950};
951
952static int __init ads7846_init(void)
953{
954 /* grr, board-specific init should stay out of drivers!! */
955
956#ifdef CONFIG_ARCH_OMAP
957 if (machine_is_omap_osk()) {
958 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
959 omap_request_gpio(4);
960 omap_set_gpio_direction(4, 1);
961 omap_request_gpio(6);
962 omap_set_gpio_direction(6, 1);
963 }
964 // also TI 1510 Innovator, bitbanging through FPGA
965 // also Nokia 770
966 // also Palm Tungsten T2
967#endif
968
969 // PXA:
970 // also Dell Axim X50
971 // also HP iPaq H191x/H192x/H415x/H435x
David Brownell2e5a7bd2006-01-08 13:34:25 -0800972 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
David Brownellffa458c2006-01-08 13:34:21 -0800973 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
974
David Brownell2e5a7bd2006-01-08 13:34:25 -0800975 // Atmel at91sam9261-EK uses ads7843
976
David Brownellffa458c2006-01-08 13:34:21 -0800977 // also various AMD Au1x00 devel boards
978
David Brownell2e5a7bd2006-01-08 13:34:25 -0800979 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800980}
981module_init(ads7846_init);
982
983static void __exit ads7846_exit(void)
984{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800985 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800986
987#ifdef CONFIG_ARCH_OMAP
988 if (machine_is_omap_osk()) {
989 omap_free_gpio(4);
990 omap_free_gpio(6);
991 }
992#endif
993
994}
995module_exit(ads7846_exit);
996
997MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
998MODULE_LICENSE("GPL");