blob: d983cc51ad32e67f2edac5c1f147bd46d3c31683 [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
Imre Deak1936d592007-01-18 00:45:31 -050057#define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
58#define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
David Brownellffa458c2006-01-08 13:34:21 -080059
David Brownelld93f70b2006-02-15 00:49:35 -050060/* this driver doesn't aim at the peak continuous sample rate */
61#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
62
David Brownellffa458c2006-01-08 13:34:21 -080063struct ts_event {
64 /* For portability, we can't read 12 bit values using SPI (which
65 * would make the controller deliver them as native byteorder u16
David Brownelld93f70b2006-02-15 00:49:35 -050066 * with msbs zeroed). Instead, we read them as two 8-bit values,
Imre Deakda970e62007-01-18 00:44:41 -050067 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
David Brownellffa458c2006-01-08 13:34:21 -080068 */
Imre Deakda970e62007-01-18 00:44:41 -050069 u16 x;
70 u16 y;
71 u16 z1, z2;
Imre Deakd5b415c2006-04-26 00:13:18 -040072 int ignore;
David Brownellffa458c2006-01-08 13:34:21 -080073};
74
75struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050076 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080077 char phys[32];
78
79 struct spi_device *spi;
Dmitry Torokhov8dd51652006-11-02 23:34:09 -050080 struct attribute_group *attr_group;
David Brownellffa458c2006-01-08 13:34:21 -080081 u16 model;
82 u16 vref_delay_usecs;
83 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -040084 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -080085
Imre Deak53a0ef892006-04-11 23:41:49 -040086 u8 read_x, read_y, read_z1, read_z2, pwrdown;
87 u16 dummy; /* for the pwrdown read */
David Brownellffa458c2006-01-08 13:34:21 -080088 struct ts_event tc;
89
Imre Deak53a0ef892006-04-11 23:41:49 -040090 struct spi_transfer xfer[10];
Imre Deak0b7018a2006-04-11 23:42:03 -040091 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -040092 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -040093 int msg_idx;
94 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -040095 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -040096 int last_read;
97
98 u16 debounce_max;
99 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400100 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800101
102 spinlock_t lock;
Imre Deak1936d592007-01-18 00:45:31 -0500103 struct hrtimer timer;
David Brownellffa458c2006-01-08 13:34:21 -0800104 unsigned pendown:1; /* P: lock */
105 unsigned pending:1; /* P: lock */
106// FIXME remove "irq_disabled"
107 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400108 unsigned disabled:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400109
Imre Deakda970e62007-01-18 00:44:41 -0500110 int (*filter)(void *data, int data_idx, int *val);
111 void *filter_data;
112 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400113 int (*get_pendown_state)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800114};
115
116/* leave chip selected when we're done, for quicker re-select? */
117#if 0
118#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
119#else
120#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
121#endif
122
123/*--------------------------------------------------------------------------*/
124
125/* The ADS7846 has touchscreen and other sensors.
126 * Earlier ads784x chips are somewhat compatible.
127 */
128#define ADS_START (1 << 7)
129#define ADS_A2A1A0_d_y (1 << 4) /* differential */
130#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
131#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
132#define ADS_A2A1A0_d_x (5 << 4) /* differential */
133#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
134#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
135#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
136#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
137#define ADS_8_BIT (1 << 3)
138#define ADS_12_BIT (0 << 3)
139#define ADS_SER (1 << 2) /* non-differential */
140#define ADS_DFR (0 << 2) /* differential */
141#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
142#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
143#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
144#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
145
146#define MAX_12BIT ((1<<12)-1)
147
148/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500149#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
150 | ADS_12_BIT | ADS_DFR | \
151 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800152
Imre Deakde2defd2007-01-18 00:45:21 -0500153#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
154#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
155#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400156
Imre Deakde2defd2007-01-18 00:45:21 -0500157#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
158#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800159
160/* single-ended samples need to first power up reference voltage;
161 * we leave both ADC and VREF powered
162 */
163#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
164 | ADS_12_BIT | ADS_SER)
165
Imre Deakde2defd2007-01-18 00:45:21 -0500166#define REF_ON (READ_12BIT_DFR(x, 1, 1))
167#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800168
169/*--------------------------------------------------------------------------*/
170
171/*
172 * Non-touchscreen sensors only use single-ended conversions.
173 */
174
175struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500176 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800177 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500178 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800179 u16 scratch;
180 __be16 sample;
181 struct spi_message msg;
182 struct spi_transfer xfer[6];
183};
184
Imre Deak7de90a82006-04-11 23:43:55 -0400185static void ads7846_enable(struct ads7846 *ts);
186static void ads7846_disable(struct ads7846 *ts);
187
Imre Deakc9e617a2006-04-11 23:44:05 -0400188static int device_suspended(struct device *dev)
189{
190 struct ads7846 *ts = dev_get_drvdata(dev);
191 return dev->power.power_state.event != PM_EVENT_ON || ts->disabled;
192}
193
David Brownellffa458c2006-01-08 13:34:21 -0800194static int ads7846_read12_ser(struct device *dev, unsigned command)
195{
196 struct spi_device *spi = to_spi_device(dev);
197 struct ads7846 *ts = dev_get_drvdata(dev);
Christoph Lametere94b1762006-12-06 20:33:17 -0800198 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800199 int status;
200 int sample;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500201 int i;
David Brownellffa458c2006-01-08 13:34:21 -0800202
203 if (!req)
204 return -ENOMEM;
205
Imre Deak0b7018a2006-04-11 23:42:03 -0400206 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800207
David Brownellffa458c2006-01-08 13:34:21 -0800208 /* activate reference, so it has time to settle; */
David Brownelld93f70b2006-02-15 00:49:35 -0500209 req->ref_on = REF_ON;
210 req->xfer[0].tx_buf = &req->ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800211 req->xfer[0].len = 1;
212 req->xfer[1].rx_buf = &req->scratch;
213 req->xfer[1].len = 2;
214
215 /*
216 * for external VREF, 0 usec (and assume it's always on);
217 * for 1uF, use 800 usec;
218 * no cap, 100 usec.
219 */
220 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
221
222 /* take sample */
223 req->command = (u8) command;
224 req->xfer[2].tx_buf = &req->command;
225 req->xfer[2].len = 1;
226 req->xfer[3].rx_buf = &req->sample;
227 req->xfer[3].len = 2;
228
229 /* REVISIT: take a few more samples, and compare ... */
230
231 /* turn off reference */
David Brownelld93f70b2006-02-15 00:49:35 -0500232 req->ref_off = REF_OFF;
233 req->xfer[4].tx_buf = &req->ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800234 req->xfer[4].len = 1;
235 req->xfer[5].rx_buf = &req->scratch;
236 req->xfer[5].len = 2;
237
238 CS_CHANGE(req->xfer[5]);
239
240 /* group all the transfers together, so we can't interfere with
241 * reading touchscreen state; disable penirq while sampling
242 */
Vitaly Wool8275c642006-01-08 13:34:28 -0800243 for (i = 0; i < 6; i++)
244 spi_message_add_tail(&req->xfer[i], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800245
Imre Deakc9e617a2006-04-11 23:44:05 -0400246 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800247 disable_irq(spi->irq);
248 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400249 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800250 enable_irq(spi->irq);
251
252 if (req->msg.status)
253 status = req->msg.status;
David Brownellffa458c2006-01-08 13:34:21 -0800254
David Brownell90845332006-05-25 18:44:20 -0700255 /* on-wire is a must-ignore bit, a BE12 value, then padding */
256 sample = be16_to_cpu(req->sample);
257 sample = sample >> 3;
258 sample &= 0x0fff;
259
260 kfree(req);
David Brownellffa458c2006-01-08 13:34:21 -0800261 return status ? status : sample;
262}
263
264#define SHOW(name) static ssize_t \
265name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
266{ \
267 ssize_t v = ads7846_read12_ser(dev, \
268 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
269 if (v < 0) \
270 return v; \
271 return sprintf(buf, "%u\n", (unsigned) v); \
272} \
273static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
274
275SHOW(temp0)
276SHOW(temp1)
277SHOW(vaux)
278SHOW(vbatt)
279
Imre Deak438f2a72006-04-11 23:41:32 -0400280static int is_pen_down(struct device *dev)
281{
282 struct ads7846 *ts = dev_get_drvdata(dev);
283
284 return ts->pendown;
285}
286
287static ssize_t ads7846_pen_down_show(struct device *dev,
288 struct device_attribute *attr, char *buf)
289{
290 return sprintf(buf, "%u\n", is_pen_down(dev));
291}
292
293static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
294
Imre Deak7de90a82006-04-11 23:43:55 -0400295static ssize_t ads7846_disable_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
297{
298 struct ads7846 *ts = dev_get_drvdata(dev);
299
300 return sprintf(buf, "%u\n", ts->disabled);
301}
302
303static ssize_t ads7846_disable_store(struct device *dev,
304 struct device_attribute *attr,
305 const char *buf, size_t count)
306{
307 struct ads7846 *ts = dev_get_drvdata(dev);
308 char *endp;
309 int i;
310
311 i = simple_strtoul(buf, &endp, 10);
312 spin_lock_irq(&ts->lock);
313
314 if (i)
315 ads7846_disable(ts);
316 else
317 ads7846_enable(ts);
318
319 spin_unlock_irq(&ts->lock);
320
321 return count;
322}
323
324static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
325
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500326static struct attribute *ads7846_attributes[] = {
327 &dev_attr_temp0.attr,
328 &dev_attr_temp1.attr,
329 &dev_attr_vbatt.attr,
330 &dev_attr_vaux.attr,
331 &dev_attr_pen_down.attr,
332 &dev_attr_disable.attr,
333 NULL,
334};
335
336static struct attribute_group ads7846_attr_group = {
337 .attrs = ads7846_attributes,
338};
339
340/*
341 * ads7843/7845 don't have temperature sensors, and
342 * use the other sensors a bit differently too
343 */
344
345static struct attribute *ads7843_attributes[] = {
346 &dev_attr_vbatt.attr,
347 &dev_attr_vaux.attr,
348 &dev_attr_pen_down.attr,
349 &dev_attr_disable.attr,
350 NULL,
351};
352
353static struct attribute_group ads7843_attr_group = {
354 .attrs = ads7843_attributes,
355};
356
357static struct attribute *ads7845_attributes[] = {
358 &dev_attr_vaux.attr,
359 &dev_attr_pen_down.attr,
360 &dev_attr_disable.attr,
361 NULL,
362};
363
364static struct attribute_group ads7845_attr_group = {
365 .attrs = ads7845_attributes,
366};
367
David Brownellffa458c2006-01-08 13:34:21 -0800368/*--------------------------------------------------------------------------*/
369
370/*
371 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
372 * to retrieve touchscreen status.
373 *
374 * The SPI transfer completion callback does the real work. It reports
375 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
376 */
377
378static void ads7846_rx(void *ads)
379{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500380 struct ads7846 *ts = ads;
381 struct input_dev *input_dev = ts->input;
382 unsigned Rt;
383 unsigned sync = 0;
384 u16 x, y, z1, z2;
385 unsigned long flags;
David Brownellffa458c2006-01-08 13:34:21 -0800386
Imre Deakda970e62007-01-18 00:44:41 -0500387 /* ads7846_rx_val() did in-place conversion (including byteswap) from
388 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800389 */
Imre Deakda970e62007-01-18 00:44:41 -0500390 x = ts->tc.x;
391 y = ts->tc.y;
392 z1 = ts->tc.z1;
393 z2 = ts->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800394
395 /* range filtering */
396 if (x == MAX_12BIT)
397 x = 0;
398
Imre Deakc9e617a2006-04-11 23:44:05 -0400399 if (likely(x && z1 && !device_suspended(&ts->spi->dev))) {
David Brownellffa458c2006-01-08 13:34:21 -0800400 /* compute touch pressure resistance using equation #2 */
401 Rt = z2;
402 Rt -= z1;
403 Rt *= x;
404 Rt *= ts->x_plate_ohms;
405 Rt /= z1;
406 Rt = (Rt + 2047) >> 12;
407 } else
408 Rt = 0;
409
Imre Deakd5b415c2006-04-26 00:13:18 -0400410 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500411 * the maximum. Don't report it to user space, repeat at least
412 * once more the measurement
413 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400414 if (ts->tc.ignore || Rt > ts->pressure_max) {
Imre Deak1936d592007-01-18 00:45:31 -0500415 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
416 HRTIMER_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400417 return;
418 }
419
David Brownellffa458c2006-01-08 13:34:21 -0800420 /* NOTE: "pendown" is inferred from pressure; we don't rely on
421 * being able to check nPENIRQ status, or "friendly" trigger modes
422 * (both-edges is much better than just-falling or low-level).
423 *
424 * REVISIT: some boards may require reading nPENIRQ; it's
425 * needed on 7843. and 7845 reads pressure differently...
426 *
427 * REVISIT: the touchscreen might not be connected; this code
428 * won't notice that, even if nPENIRQ never fires ...
429 */
430 if (!ts->pendown && Rt != 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500431 input_report_key(input_dev, BTN_TOUCH, 1);
David Brownellffa458c2006-01-08 13:34:21 -0800432 sync = 1;
433 } else if (ts->pendown && Rt == 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500434 input_report_key(input_dev, BTN_TOUCH, 0);
David Brownellffa458c2006-01-08 13:34:21 -0800435 sync = 1;
436 }
437
438 if (Rt) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500439 input_report_abs(input_dev, ABS_X, x);
440 input_report_abs(input_dev, ABS_Y, y);
David Brownellffa458c2006-01-08 13:34:21 -0800441 sync = 1;
442 }
Imre Deakae82d5a2006-04-26 00:12:14 -0400443
444 if (sync) {
445 input_report_abs(input_dev, ABS_PRESSURE, Rt);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500446 input_sync(input_dev);
Imre Deakae82d5a2006-04-26 00:12:14 -0400447 }
David Brownellffa458c2006-01-08 13:34:21 -0800448
449#ifdef VERBOSE
450 if (Rt || ts->pendown)
451 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
452 x, y, Rt, Rt ? "" : " UP");
453#endif
454
David Brownellffa458c2006-01-08 13:34:21 -0800455 spin_lock_irqsave(&ts->lock, flags);
456
457 ts->pendown = (Rt != 0);
Imre Deak1936d592007-01-18 00:45:31 -0500458 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD), HRTIMER_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800459
460 spin_unlock_irqrestore(&ts->lock, flags);
461}
462
Imre Deakda970e62007-01-18 00:44:41 -0500463static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400464{
465 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400466
Imre Deakda970e62007-01-18 00:44:41 -0500467 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
468 /* Start over collecting consistent readings. */
469 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400470 /* Repeat it, if this was the first read or the read
471 * wasn't consistent enough. */
472 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500473 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400474 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500475 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400476 } else {
477 /* Maximum number of debouncing reached and still
478 * not enough number of consistent readings. Abort
479 * the whole sample, repeat it in the next sampling
480 * period.
481 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400482 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500483 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400484 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400485 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400486 if (++ts->read_rep > ts->debounce_rep) {
487 /* Got a good reading for this coordinate,
488 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400489 ts->read_cnt = 0;
490 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500491 return ADS7846_FILTER_OK;
492 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400493 /* Read more values that are consistent. */
494 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500495 return ADS7846_FILTER_REPEAT;
496 }
497 }
498}
499
500static int ads7846_no_filter(void *ads, int data_idx, int *val)
501{
502 return ADS7846_FILTER_OK;
503}
504
505static void ads7846_rx_val(void *ads)
506{
507 struct ads7846 *ts = ads;
508 struct spi_message *m;
509 struct spi_transfer *t;
510 u16 *rx_val;
511 int val;
512 int action;
513 int status;
514
515 m = &ts->msg[ts->msg_idx];
516 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
517 rx_val = t->rx_buf;
518
519 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
520 * built from two 8 bit values written msb-first.
521 */
522 val = be16_to_cpu(*rx_val) >> 3;
523
524 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
525 switch (action) {
526 case ADS7846_FILTER_REPEAT:
527 break;
528 case ADS7846_FILTER_IGNORE:
529 ts->tc.ignore = 1;
530 /* Last message will contain ads7846_rx() as the
531 * completion function.
532 */
533 m = ts->last_msg;
534 break;
535 case ADS7846_FILTER_OK:
536 *rx_val = val;
537 ts->tc.ignore = 0;
538 m = &ts->msg[++ts->msg_idx];
539 break;
540 default:
541 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400542 }
543 status = spi_async(ts->spi, m);
544 if (status)
545 dev_err(&ts->spi->dev, "spi_async --> %d\n",
546 status);
547}
548
Imre Deak1936d592007-01-18 00:45:31 -0500549static int ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800550{
Imre Deak1936d592007-01-18 00:45:31 -0500551 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800552 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800553
Imre Deakc9e617a2006-04-11 23:44:05 -0400554 spin_lock_irq(&ts->lock);
555
556 if (unlikely(ts->msg_idx && !ts->pendown)) {
David Brownell90845332006-05-25 18:44:20 -0700557 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400558 if (!device_suspended(&ts->spi->dev)) {
559 ts->irq_disabled = 0;
560 enable_irq(ts->spi->irq);
561 }
562 ts->pending = 0;
563 ts->msg_idx = 0;
564 } else {
565 /* pen is still down, continue with the measurement */
566 ts->msg_idx = 0;
567 status = spi_async(ts->spi, &ts->msg[0]);
568 if (status)
569 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
570 }
571
572 spin_unlock_irq(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500573 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800574}
575
David Howells7d12e782006-10-05 14:55:46 +0100576static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800577{
Imre Deak0b7018a2006-04-11 23:42:03 -0400578 struct ads7846 *ts = handle;
579 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400580
581 spin_lock_irqsave(&ts->lock, flags);
Imre Deakc9e617a2006-04-11 23:44:05 -0400582 if (likely(ts->get_pendown_state())) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400583 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700584 /* The ARM do_simple_IRQ() dispatcher doesn't act
585 * like the other dispatchers: it will report IRQs
586 * even after they've been disabled. We work around
587 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400588 */
589 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400590 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400591 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500592 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
593 HRTIMER_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400594 }
595 }
596 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400597
598 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800599}
600
601/*--------------------------------------------------------------------------*/
602
Imre Deak7de90a82006-04-11 23:43:55 -0400603/* Must be called with ts->lock held */
604static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800605{
Imre Deak7de90a82006-04-11 23:43:55 -0400606 if (ts->disabled)
607 return;
David Brownellffa458c2006-01-08 13:34:21 -0800608
Imre Deakc9e617a2006-04-11 23:44:05 -0400609 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800610
Imre Deakc9e617a2006-04-11 23:44:05 -0400611 /* are we waiting for IRQ, or polling? */
612 if (!ts->pending) {
613 ts->irq_disabled = 1;
614 disable_irq(ts->spi->irq);
615 } else {
616 /* the timer will run at least once more, and
617 * leave everything in a clean state, IRQ disabled
618 */
619 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400620 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400621 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400622 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800623 }
624 }
625
626 /* we know the chip's in lowpower mode since we always
627 * leave it that way after every request
628 */
629
Imre Deak7de90a82006-04-11 23:43:55 -0400630}
631
632/* Must be called with ts->lock held */
633static void ads7846_enable(struct ads7846 *ts)
634{
635 if (!ts->disabled)
636 return;
637
638 ts->disabled = 0;
639 ts->irq_disabled = 0;
640 enable_irq(ts->spi->irq);
641}
642
643static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
644{
645 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
646
647 spin_lock_irq(&ts->lock);
648
649 spi->dev.power.power_state = message;
650 ads7846_disable(ts);
651
652 spin_unlock_irq(&ts->lock);
653
David Brownellffa458c2006-01-08 13:34:21 -0800654 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400655
David Brownellffa458c2006-01-08 13:34:21 -0800656}
657
David Brownell2e5a7bd2006-01-08 13:34:25 -0800658static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800659{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800660 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800661
Imre Deak7de90a82006-04-11 23:43:55 -0400662 spin_lock_irq(&ts->lock);
663
David Brownell2e5a7bd2006-01-08 13:34:25 -0800664 spi->dev.power.power_state = PMSG_ON;
Imre Deak7de90a82006-04-11 23:43:55 -0400665 ads7846_enable(ts);
666
667 spin_unlock_irq(&ts->lock);
668
David Brownellffa458c2006-01-08 13:34:21 -0800669 return 0;
670}
671
David Brownell2e5a7bd2006-01-08 13:34:25 -0800672static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800673{
David Brownellffa458c2006-01-08 13:34:21 -0800674 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500675 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800676 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400677 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800678 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500679 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500680 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800681
682 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800683 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800684 return -ENODEV;
685 }
686
687 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800688 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800689 return -ENODEV;
690 }
691
692 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500693 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800694 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500695 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800696 return -EINVAL;
697 }
698
David Brownell90845332006-05-25 18:44:20 -0700699 /* REVISIT when the irq can be triggered active-low, or if for some
700 * reason the touchscreen isn't hooked up, we don't need to access
701 * the pendown state.
702 */
Imre Deakc9e617a2006-04-11 23:44:05 -0400703 if (pdata->get_pendown_state == NULL) {
704 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
705 return -EINVAL;
706 }
707
David Brownell90845332006-05-25 18:44:20 -0700708 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
709 * that even if the hardware can do that, the SPI controller driver
710 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800711 */
David Brownell90845332006-05-25 18:44:20 -0700712 spi->bits_per_word = 8;
David Brownellffa458c2006-01-08 13:34:21 -0800713
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500714 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
715 input_dev = input_allocate_device();
716 if (!ts || !input_dev) {
717 err = -ENOMEM;
718 goto err_free_mem;
719 }
David Brownellffa458c2006-01-08 13:34:21 -0800720
David Brownell2e5a7bd2006-01-08 13:34:25 -0800721 dev_set_drvdata(&spi->dev, ts);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500722 spi->dev.power.power_state = PMSG_ON;
David Brownellffa458c2006-01-08 13:34:21 -0800723
724 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500725 ts->input = input_dev;
David Brownellffa458c2006-01-08 13:34:21 -0800726
Imre Deak1936d592007-01-18 00:45:31 -0500727 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800728 ts->timer.function = ads7846_timer;
729
Imre Deak7de90a82006-04-11 23:43:55 -0400730 spin_lock_init(&ts->lock);
731
David Brownellffa458c2006-01-08 13:34:21 -0800732 ts->model = pdata->model ? : 7846;
733 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
734 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400735 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500736
737 if (pdata->filter != NULL) {
738 if (pdata->filter_init != NULL) {
739 err = pdata->filter_init(pdata, &ts->filter_data);
740 if (err < 0)
741 goto err_free_mem;
742 }
743 ts->filter = pdata->filter;
744 ts->filter_cleanup = pdata->filter_cleanup;
745 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400746 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500747 if (ts->debounce_max < 2)
748 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400749 ts->debounce_tol = pdata->debounce_tol;
750 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500751 ts->filter = ads7846_debounce;
752 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400753 } else
Imre Deakda970e62007-01-18 00:44:41 -0500754 ts->filter = ads7846_no_filter;
Imre Deakc9e617a2006-04-11 23:44:05 -0400755 ts->get_pendown_state = pdata->get_pendown_state;
David Brownellffa458c2006-01-08 13:34:21 -0800756
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500757 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800758
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500759 input_dev->name = "ADS784x Touchscreen";
760 input_dev->phys = ts->phys;
761 input_dev->cdev.dev = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800762
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500763 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
764 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
765 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800766 pdata->x_min ? : 0,
767 pdata->x_max ? : MAX_12BIT,
768 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500769 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800770 pdata->y_min ? : 0,
771 pdata->y_max ? : MAX_12BIT,
772 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500773 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800774 pdata->pressure_min, pdata->pressure_max, 0, 0);
775
Imre Deakde2defd2007-01-18 00:45:21 -0500776 vref = pdata->keep_vref_on;
777
David Brownellffa458c2006-01-08 13:34:21 -0800778 /* set up the transfers to read touchscreen state; this assumes we
779 * use formula #2 for pressure, not #3.
780 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400781 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800782 x = ts->xfer;
783
Imre Deak0b7018a2006-04-11 23:42:03 -0400784 spi_message_init(m);
785
David Brownellffa458c2006-01-08 13:34:21 -0800786 /* y- still on; turn on only y+ (and ADC) */
Imre Deakde2defd2007-01-18 00:45:21 -0500787 ts->read_y = READ_Y(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500788 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800789 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400790 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500791
David Brownellffa458c2006-01-08 13:34:21 -0800792 x++;
793 x->rx_buf = &ts->tc.y;
794 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400795 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800796
Imre Deakda970e62007-01-18 00:44:41 -0500797 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400798 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500799
Imre Deak0b7018a2006-04-11 23:42:03 -0400800 m++;
801 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800802
803 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500804 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500805 ts->read_x = READ_X(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500806 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800807 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400808 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500809
David Brownellffa458c2006-01-08 13:34:21 -0800810 x++;
811 x->rx_buf = &ts->tc.x;
812 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400813 spi_message_add_tail(x, m);
814
Imre Deakda970e62007-01-18 00:44:41 -0500815 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400816 m->context = ts;
817
818 /* turn y+ off, x- on; we'll use formula #2 */
819 if (ts->model == 7846) {
820 m++;
821 spi_message_init(m);
822
823 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500824 ts->read_z1 = READ_Z1(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -0400825 x->tx_buf = &ts->read_z1;
826 x->len = 1;
827 spi_message_add_tail(x, m);
828
829 x++;
830 x->rx_buf = &ts->tc.z1;
831 x->len = 2;
832 spi_message_add_tail(x, m);
833
Imre Deakda970e62007-01-18 00:44:41 -0500834 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400835 m->context = ts;
836
837 m++;
838 spi_message_init(m);
839
840 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500841 ts->read_z2 = READ_Z2(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -0400842 x->tx_buf = &ts->read_z2;
843 x->len = 1;
844 spi_message_add_tail(x, m);
845
846 x++;
847 x->rx_buf = &ts->tc.z2;
848 x->len = 2;
849 spi_message_add_tail(x, m);
850
Imre Deakda970e62007-01-18 00:44:41 -0500851 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400852 m->context = ts;
853 }
Imre Deak53a0ef892006-04-11 23:41:49 -0400854
855 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -0400856 m++;
857 spi_message_init(m);
858
Imre Deak53a0ef892006-04-11 23:41:49 -0400859 x++;
860 ts->pwrdown = PWRDOWN;
861 x->tx_buf = &ts->pwrdown;
862 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400863 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -0400864
865 x++;
866 x->rx_buf = &ts->dummy;
867 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500868 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -0400869 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800870
Imre Deak0b7018a2006-04-11 23:42:03 -0400871 m->complete = ads7846_rx;
872 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -0800873
Imre Deakd5b415c2006-04-26 00:13:18 -0400874 ts->last_msg = m;
875
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700876 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -0700877 spi->dev.driver->name, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800878 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500879 err = -EBUSY;
Imre Deakda970e62007-01-18 00:44:41 -0500880 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -0800881 }
David Brownellffa458c2006-01-08 13:34:21 -0800882
David Brownell2e5a7bd2006-01-08 13:34:25 -0800883 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -0800884
885 /* take a first sample, leaving nPENIRQ active; avoid
886 * the touchscreen, in case it's not connected.
887 */
David Brownell2e5a7bd2006-01-08 13:34:25 -0800888 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -0800889 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
890
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500891 switch (ts->model) {
892 case 7846:
893 ts->attr_group = &ads7846_attr_group;
894 break;
895 case 7845:
896 ts->attr_group = &ads7845_attr_group;
897 break;
898 default:
899 ts->attr_group = &ads7843_attr_group;
900 break;
David Brownellffa458c2006-01-08 13:34:21 -0800901 }
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500902 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
903 if (err)
904 goto err_free_irq;
Imre Deak7de90a82006-04-11 23:43:55 -0400905
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500906 err = input_register_device(input_dev);
907 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500908 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500909
David Brownellffa458c2006-01-08 13:34:21 -0800910 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500911
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500912 err_remove_attr_group:
913 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
914 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500915 free_irq(spi->irq, ts);
Imre Deakda970e62007-01-18 00:44:41 -0500916 err_cleanup_filter:
917 if (ts->filter_cleanup)
918 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500919 err_free_mem:
920 input_free_device(input_dev);
921 kfree(ts);
922 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800923}
924
David Brownell2e5a7bd2006-01-08 13:34:25 -0800925static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800926{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800927 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800928
Imre Deak7de90a82006-04-11 23:43:55 -0400929 input_unregister_device(ts->input);
930
David Brownell2e5a7bd2006-01-08 13:34:25 -0800931 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -0800932
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500933 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
David Brownellffa458c2006-01-08 13:34:21 -0800934
Imre Deak7de90a82006-04-11 23:43:55 -0400935 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -0400936 /* suspend left the IRQ disabled */
937 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -0400938
Imre Deakda970e62007-01-18 00:44:41 -0500939 if (ts->filter_cleanup)
940 ts->filter_cleanup(ts->filter_data);
941
David Brownellffa458c2006-01-08 13:34:21 -0800942 kfree(ts);
943
David Brownell2e5a7bd2006-01-08 13:34:25 -0800944 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -0800945 return 0;
946}
947
David Brownell2e5a7bd2006-01-08 13:34:25 -0800948static struct spi_driver ads7846_driver = {
949 .driver = {
950 .name = "ads7846",
951 .bus = &spi_bus_type,
952 .owner = THIS_MODULE,
953 },
David Brownellffa458c2006-01-08 13:34:21 -0800954 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -0800955 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -0800956 .suspend = ads7846_suspend,
957 .resume = ads7846_resume,
958};
959
960static int __init ads7846_init(void)
961{
962 /* grr, board-specific init should stay out of drivers!! */
963
964#ifdef CONFIG_ARCH_OMAP
965 if (machine_is_omap_osk()) {
966 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
967 omap_request_gpio(4);
968 omap_set_gpio_direction(4, 1);
969 omap_request_gpio(6);
970 omap_set_gpio_direction(6, 1);
971 }
972 // also TI 1510 Innovator, bitbanging through FPGA
973 // also Nokia 770
974 // also Palm Tungsten T2
975#endif
976
977 // PXA:
978 // also Dell Axim X50
979 // also HP iPaq H191x/H192x/H415x/H435x
David Brownell2e5a7bd2006-01-08 13:34:25 -0800980 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
David Brownellffa458c2006-01-08 13:34:21 -0800981 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
982
David Brownell2e5a7bd2006-01-08 13:34:25 -0800983 // Atmel at91sam9261-EK uses ads7843
984
David Brownellffa458c2006-01-08 13:34:21 -0800985 // also various AMD Au1x00 devel boards
986
David Brownell2e5a7bd2006-01-08 13:34:25 -0800987 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800988}
989module_init(ads7846_init);
990
991static void __exit ads7846_exit(void)
992{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800993 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800994
995#ifdef CONFIG_ARCH_OMAP
996 if (machine_is_omap_osk()) {
997 omap_free_gpio(4);
998 omap_free_gpio(6);
999 }
1000#endif
1001
1002}
1003module_exit(ads7846_exit);
1004
1005MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1006MODULE_LICENSE("GPL");