blob: b18c63a3f2abd8aeebb5999f35cbcb59d65e6f27 [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;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500381 unsigned Rt;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500382 u16 x, y, z1, z2;
David Brownellffa458c2006-01-08 13:34:21 -0800383
Imre Deakda970e62007-01-18 00:44:41 -0500384 /* ads7846_rx_val() did in-place conversion (including byteswap) from
385 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800386 */
Imre Deakda970e62007-01-18 00:44:41 -0500387 x = ts->tc.x;
388 y = ts->tc.y;
389 z1 = ts->tc.z1;
390 z2 = ts->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800391
392 /* range filtering */
393 if (x == MAX_12BIT)
394 x = 0;
395
Imre Deak15e35892007-01-18 00:45:43 -0500396 if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800397 /* compute touch pressure resistance using equation #2 */
398 Rt = z2;
399 Rt -= z1;
400 Rt *= x;
401 Rt *= ts->x_plate_ohms;
402 Rt /= z1;
403 Rt = (Rt + 2047) >> 12;
404 } else
405 Rt = 0;
406
Imre Deakd5b415c2006-04-26 00:13:18 -0400407 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500408 * the maximum. Don't report it to user space, repeat at least
409 * once more the measurement
410 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400411 if (ts->tc.ignore || Rt > ts->pressure_max) {
Imre Deak15e35892007-01-18 00:45:43 -0500412#ifdef VERBOSE
413 pr_debug("%s: ignored %d pressure %d\n",
414 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
415#endif
Imre Deak1936d592007-01-18 00:45:31 -0500416 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
417 HRTIMER_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400418 return;
419 }
420
Imre Deak15e35892007-01-18 00:45:43 -0500421 /* NOTE: We can't rely on the pressure to determine the pen down
422 * state, even this controller has a pressure sensor. The pressure
423 * value can fluctuate for quite a while after lifting the pen and
424 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800425 *
Imre Deak15e35892007-01-18 00:45:43 -0500426 * The only safe way to check for the pen up condition is in the
427 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800428 */
David Brownellffa458c2006-01-08 13:34:21 -0800429 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500430 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400431
Imre Deak15e35892007-01-18 00:45:43 -0500432 if (!ts->pendown) {
433 input_report_key(input, BTN_TOUCH, 1);
434 ts->pendown = 1;
435#ifdef VERBOSE
436 dev_dbg(&ts->spi->dev, "DOWN\n");
David Brownellffa458c2006-01-08 13:34:21 -0800437#endif
Imre Deak15e35892007-01-18 00:45:43 -0500438 }
439 input_report_abs(input, ABS_X, x);
440 input_report_abs(input, ABS_Y, y);
441 input_report_abs(input, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800442
Imre Deak15e35892007-01-18 00:45:43 -0500443 input_sync(input);
444#ifdef VERBOSE
445 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
446#endif
447 }
David Brownellffa458c2006-01-08 13:34:21 -0800448
Imre Deak1936d592007-01-18 00:45:31 -0500449 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD), HRTIMER_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800450}
451
Imre Deakda970e62007-01-18 00:44:41 -0500452static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400453{
454 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400455
Imre Deakda970e62007-01-18 00:44:41 -0500456 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
457 /* Start over collecting consistent readings. */
458 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400459 /* Repeat it, if this was the first read or the read
460 * wasn't consistent enough. */
461 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500462 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400463 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500464 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400465 } else {
466 /* Maximum number of debouncing reached and still
467 * not enough number of consistent readings. Abort
468 * the whole sample, repeat it in the next sampling
469 * period.
470 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400471 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500472 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400473 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400474 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400475 if (++ts->read_rep > ts->debounce_rep) {
476 /* Got a good reading for this coordinate,
477 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400478 ts->read_cnt = 0;
479 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500480 return ADS7846_FILTER_OK;
481 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400482 /* Read more values that are consistent. */
483 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500484 return ADS7846_FILTER_REPEAT;
485 }
486 }
487}
488
489static int ads7846_no_filter(void *ads, int data_idx, int *val)
490{
491 return ADS7846_FILTER_OK;
492}
493
494static void ads7846_rx_val(void *ads)
495{
496 struct ads7846 *ts = ads;
497 struct spi_message *m;
498 struct spi_transfer *t;
499 u16 *rx_val;
500 int val;
501 int action;
502 int status;
503
504 m = &ts->msg[ts->msg_idx];
505 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
506 rx_val = t->rx_buf;
507
508 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
509 * built from two 8 bit values written msb-first.
510 */
511 val = be16_to_cpu(*rx_val) >> 3;
512
513 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
514 switch (action) {
515 case ADS7846_FILTER_REPEAT:
516 break;
517 case ADS7846_FILTER_IGNORE:
518 ts->tc.ignore = 1;
519 /* Last message will contain ads7846_rx() as the
520 * completion function.
521 */
522 m = ts->last_msg;
523 break;
524 case ADS7846_FILTER_OK:
525 *rx_val = val;
526 ts->tc.ignore = 0;
527 m = &ts->msg[++ts->msg_idx];
528 break;
529 default:
530 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400531 }
532 status = spi_async(ts->spi, m);
533 if (status)
534 dev_err(&ts->spi->dev, "spi_async --> %d\n",
535 status);
536}
537
Imre Deak1936d592007-01-18 00:45:31 -0500538static int ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800539{
Imre Deak1936d592007-01-18 00:45:31 -0500540 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800541 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800542
Imre Deakc9e617a2006-04-11 23:44:05 -0400543 spin_lock_irq(&ts->lock);
544
Imre Deak15e35892007-01-18 00:45:43 -0500545 if (unlikely(!ts->get_pendown_state() ||
546 device_suspended(&ts->spi->dev))) {
547 if (ts->pendown) {
548 struct input_dev *input = ts->input;
549
550 input_report_key(input, BTN_TOUCH, 0);
551 input_report_abs(input, ABS_PRESSURE, 0);
552 input_sync(input);
553
554 ts->pendown = 0;
555#ifdef VERBOSE
556 dev_dbg(&ts->spi->dev, "UP\n");
557#endif
558 }
559
David Brownell90845332006-05-25 18:44:20 -0700560 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400561 if (!device_suspended(&ts->spi->dev)) {
562 ts->irq_disabled = 0;
563 enable_irq(ts->spi->irq);
564 }
565 ts->pending = 0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400566 } else {
567 /* pen is still down, continue with the measurement */
568 ts->msg_idx = 0;
569 status = spi_async(ts->spi, &ts->msg[0]);
570 if (status)
571 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
572 }
573
574 spin_unlock_irq(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500575 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800576}
577
David Howells7d12e782006-10-05 14:55:46 +0100578static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800579{
Imre Deak0b7018a2006-04-11 23:42:03 -0400580 struct ads7846 *ts = handle;
581 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400582
583 spin_lock_irqsave(&ts->lock, flags);
Imre Deakc9e617a2006-04-11 23:44:05 -0400584 if (likely(ts->get_pendown_state())) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400585 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700586 /* The ARM do_simple_IRQ() dispatcher doesn't act
587 * like the other dispatchers: it will report IRQs
588 * even after they've been disabled. We work around
589 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400590 */
591 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400592 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400593 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500594 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
595 HRTIMER_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400596 }
597 }
598 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400599
600 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800601}
602
603/*--------------------------------------------------------------------------*/
604
Imre Deak7de90a82006-04-11 23:43:55 -0400605/* Must be called with ts->lock held */
606static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800607{
Imre Deak7de90a82006-04-11 23:43:55 -0400608 if (ts->disabled)
609 return;
David Brownellffa458c2006-01-08 13:34:21 -0800610
Imre Deakc9e617a2006-04-11 23:44:05 -0400611 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800612
Imre Deakc9e617a2006-04-11 23:44:05 -0400613 /* are we waiting for IRQ, or polling? */
614 if (!ts->pending) {
615 ts->irq_disabled = 1;
616 disable_irq(ts->spi->irq);
617 } else {
618 /* the timer will run at least once more, and
619 * leave everything in a clean state, IRQ disabled
620 */
621 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400622 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400623 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400624 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800625 }
626 }
627
628 /* we know the chip's in lowpower mode since we always
629 * leave it that way after every request
630 */
631
Imre Deak7de90a82006-04-11 23:43:55 -0400632}
633
634/* Must be called with ts->lock held */
635static void ads7846_enable(struct ads7846 *ts)
636{
637 if (!ts->disabled)
638 return;
639
640 ts->disabled = 0;
641 ts->irq_disabled = 0;
642 enable_irq(ts->spi->irq);
643}
644
645static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
646{
647 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
648
649 spin_lock_irq(&ts->lock);
650
651 spi->dev.power.power_state = message;
652 ads7846_disable(ts);
653
654 spin_unlock_irq(&ts->lock);
655
David Brownellffa458c2006-01-08 13:34:21 -0800656 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400657
David Brownellffa458c2006-01-08 13:34:21 -0800658}
659
David Brownell2e5a7bd2006-01-08 13:34:25 -0800660static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800661{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800662 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800663
Imre Deak7de90a82006-04-11 23:43:55 -0400664 spin_lock_irq(&ts->lock);
665
David Brownell2e5a7bd2006-01-08 13:34:25 -0800666 spi->dev.power.power_state = PMSG_ON;
Imre Deak7de90a82006-04-11 23:43:55 -0400667 ads7846_enable(ts);
668
669 spin_unlock_irq(&ts->lock);
670
David Brownellffa458c2006-01-08 13:34:21 -0800671 return 0;
672}
673
David Brownell2e5a7bd2006-01-08 13:34:25 -0800674static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800675{
David Brownellffa458c2006-01-08 13:34:21 -0800676 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500677 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800678 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400679 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800680 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500681 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500682 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800683
684 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800685 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800686 return -ENODEV;
687 }
688
689 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800690 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800691 return -ENODEV;
692 }
693
694 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500695 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800696 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500697 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800698 return -EINVAL;
699 }
700
David Brownell90845332006-05-25 18:44:20 -0700701 /* REVISIT when the irq can be triggered active-low, or if for some
702 * reason the touchscreen isn't hooked up, we don't need to access
703 * the pendown state.
704 */
Imre Deakc9e617a2006-04-11 23:44:05 -0400705 if (pdata->get_pendown_state == NULL) {
706 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
707 return -EINVAL;
708 }
709
David Brownell90845332006-05-25 18:44:20 -0700710 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
711 * that even if the hardware can do that, the SPI controller driver
712 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800713 */
David Brownell90845332006-05-25 18:44:20 -0700714 spi->bits_per_word = 8;
Imre Deak7937e862007-01-18 00:45:38 -0500715 spi->mode = SPI_MODE_1;
716 err = spi_setup(spi);
717 if (err < 0)
718 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800719
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500720 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
721 input_dev = input_allocate_device();
722 if (!ts || !input_dev) {
723 err = -ENOMEM;
724 goto err_free_mem;
725 }
David Brownellffa458c2006-01-08 13:34:21 -0800726
David Brownell2e5a7bd2006-01-08 13:34:25 -0800727 dev_set_drvdata(&spi->dev, ts);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500728 spi->dev.power.power_state = PMSG_ON;
David Brownellffa458c2006-01-08 13:34:21 -0800729
730 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500731 ts->input = input_dev;
David Brownellffa458c2006-01-08 13:34:21 -0800732
Imre Deak1936d592007-01-18 00:45:31 -0500733 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800734 ts->timer.function = ads7846_timer;
735
Imre Deak7de90a82006-04-11 23:43:55 -0400736 spin_lock_init(&ts->lock);
737
David Brownellffa458c2006-01-08 13:34:21 -0800738 ts->model = pdata->model ? : 7846;
739 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
740 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400741 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500742
743 if (pdata->filter != NULL) {
744 if (pdata->filter_init != NULL) {
745 err = pdata->filter_init(pdata, &ts->filter_data);
746 if (err < 0)
747 goto err_free_mem;
748 }
749 ts->filter = pdata->filter;
750 ts->filter_cleanup = pdata->filter_cleanup;
751 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400752 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500753 if (ts->debounce_max < 2)
754 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400755 ts->debounce_tol = pdata->debounce_tol;
756 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500757 ts->filter = ads7846_debounce;
758 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400759 } else
Imre Deakda970e62007-01-18 00:44:41 -0500760 ts->filter = ads7846_no_filter;
Imre Deakc9e617a2006-04-11 23:44:05 -0400761 ts->get_pendown_state = pdata->get_pendown_state;
David Brownellffa458c2006-01-08 13:34:21 -0800762
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500763 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800764
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500765 input_dev->name = "ADS784x Touchscreen";
766 input_dev->phys = ts->phys;
767 input_dev->cdev.dev = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800768
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500769 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
770 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
771 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800772 pdata->x_min ? : 0,
773 pdata->x_max ? : MAX_12BIT,
774 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500775 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800776 pdata->y_min ? : 0,
777 pdata->y_max ? : MAX_12BIT,
778 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500779 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800780 pdata->pressure_min, pdata->pressure_max, 0, 0);
781
Imre Deakde2defd2007-01-18 00:45:21 -0500782 vref = pdata->keep_vref_on;
783
David Brownellffa458c2006-01-08 13:34:21 -0800784 /* set up the transfers to read touchscreen state; this assumes we
785 * use formula #2 for pressure, not #3.
786 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400787 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800788 x = ts->xfer;
789
Imre Deak0b7018a2006-04-11 23:42:03 -0400790 spi_message_init(m);
791
David Brownellffa458c2006-01-08 13:34:21 -0800792 /* y- still on; turn on only y+ (and ADC) */
Imre Deakde2defd2007-01-18 00:45:21 -0500793 ts->read_y = READ_Y(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500794 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800795 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400796 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500797
David Brownellffa458c2006-01-08 13:34:21 -0800798 x++;
799 x->rx_buf = &ts->tc.y;
800 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400801 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800802
Imre Deakda970e62007-01-18 00:44:41 -0500803 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400804 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500805
Imre Deak0b7018a2006-04-11 23:42:03 -0400806 m++;
807 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800808
809 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500810 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500811 ts->read_x = READ_X(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500812 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800813 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400814 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500815
David Brownellffa458c2006-01-08 13:34:21 -0800816 x++;
817 x->rx_buf = &ts->tc.x;
818 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400819 spi_message_add_tail(x, m);
820
Imre Deakda970e62007-01-18 00:44:41 -0500821 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400822 m->context = ts;
823
824 /* turn y+ off, x- on; we'll use formula #2 */
825 if (ts->model == 7846) {
826 m++;
827 spi_message_init(m);
828
829 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500830 ts->read_z1 = READ_Z1(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -0400831 x->tx_buf = &ts->read_z1;
832 x->len = 1;
833 spi_message_add_tail(x, m);
834
835 x++;
836 x->rx_buf = &ts->tc.z1;
837 x->len = 2;
838 spi_message_add_tail(x, m);
839
Imre Deakda970e62007-01-18 00:44:41 -0500840 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400841 m->context = ts;
842
843 m++;
844 spi_message_init(m);
845
846 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500847 ts->read_z2 = READ_Z2(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -0400848 x->tx_buf = &ts->read_z2;
849 x->len = 1;
850 spi_message_add_tail(x, m);
851
852 x++;
853 x->rx_buf = &ts->tc.z2;
854 x->len = 2;
855 spi_message_add_tail(x, m);
856
Imre Deakda970e62007-01-18 00:44:41 -0500857 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400858 m->context = ts;
859 }
Imre Deak53a0ef892006-04-11 23:41:49 -0400860
861 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -0400862 m++;
863 spi_message_init(m);
864
Imre Deak53a0ef892006-04-11 23:41:49 -0400865 x++;
866 ts->pwrdown = PWRDOWN;
867 x->tx_buf = &ts->pwrdown;
868 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400869 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -0400870
871 x++;
872 x->rx_buf = &ts->dummy;
873 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500874 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -0400875 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800876
Imre Deak0b7018a2006-04-11 23:42:03 -0400877 m->complete = ads7846_rx;
878 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -0800879
Imre Deakd5b415c2006-04-26 00:13:18 -0400880 ts->last_msg = m;
881
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700882 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -0700883 spi->dev.driver->name, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800884 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500885 err = -EBUSY;
Imre Deakda970e62007-01-18 00:44:41 -0500886 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -0800887 }
David Brownellffa458c2006-01-08 13:34:21 -0800888
David Brownell2e5a7bd2006-01-08 13:34:25 -0800889 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -0800890
891 /* take a first sample, leaving nPENIRQ active; avoid
892 * the touchscreen, in case it's not connected.
893 */
David Brownell2e5a7bd2006-01-08 13:34:25 -0800894 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -0800895 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
896
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500897 switch (ts->model) {
898 case 7846:
899 ts->attr_group = &ads7846_attr_group;
900 break;
901 case 7845:
902 ts->attr_group = &ads7845_attr_group;
903 break;
904 default:
905 ts->attr_group = &ads7843_attr_group;
906 break;
David Brownellffa458c2006-01-08 13:34:21 -0800907 }
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500908 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
909 if (err)
910 goto err_free_irq;
Imre Deak7de90a82006-04-11 23:43:55 -0400911
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500912 err = input_register_device(input_dev);
913 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500914 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500915
David Brownellffa458c2006-01-08 13:34:21 -0800916 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500917
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500918 err_remove_attr_group:
919 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
920 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500921 free_irq(spi->irq, ts);
Imre Deakda970e62007-01-18 00:44:41 -0500922 err_cleanup_filter:
923 if (ts->filter_cleanup)
924 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500925 err_free_mem:
926 input_free_device(input_dev);
927 kfree(ts);
928 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800929}
930
David Brownell2e5a7bd2006-01-08 13:34:25 -0800931static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800932{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800933 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800934
Imre Deak7de90a82006-04-11 23:43:55 -0400935 input_unregister_device(ts->input);
936
David Brownell2e5a7bd2006-01-08 13:34:25 -0800937 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -0800938
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500939 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
David Brownellffa458c2006-01-08 13:34:21 -0800940
Imre Deak7de90a82006-04-11 23:43:55 -0400941 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -0400942 /* suspend left the IRQ disabled */
943 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -0400944
Imre Deakda970e62007-01-18 00:44:41 -0500945 if (ts->filter_cleanup)
946 ts->filter_cleanup(ts->filter_data);
947
David Brownellffa458c2006-01-08 13:34:21 -0800948 kfree(ts);
949
David Brownell2e5a7bd2006-01-08 13:34:25 -0800950 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -0800951 return 0;
952}
953
David Brownell2e5a7bd2006-01-08 13:34:25 -0800954static struct spi_driver ads7846_driver = {
955 .driver = {
956 .name = "ads7846",
957 .bus = &spi_bus_type,
958 .owner = THIS_MODULE,
959 },
David Brownellffa458c2006-01-08 13:34:21 -0800960 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -0800961 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -0800962 .suspend = ads7846_suspend,
963 .resume = ads7846_resume,
964};
965
966static int __init ads7846_init(void)
967{
968 /* grr, board-specific init should stay out of drivers!! */
969
970#ifdef CONFIG_ARCH_OMAP
971 if (machine_is_omap_osk()) {
972 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
973 omap_request_gpio(4);
974 omap_set_gpio_direction(4, 1);
975 omap_request_gpio(6);
976 omap_set_gpio_direction(6, 1);
977 }
978 // also TI 1510 Innovator, bitbanging through FPGA
979 // also Nokia 770
980 // also Palm Tungsten T2
981#endif
982
983 // PXA:
984 // also Dell Axim X50
985 // also HP iPaq H191x/H192x/H415x/H435x
David Brownell2e5a7bd2006-01-08 13:34:25 -0800986 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
David Brownellffa458c2006-01-08 13:34:21 -0800987 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
988
David Brownell2e5a7bd2006-01-08 13:34:25 -0800989 // Atmel at91sam9261-EK uses ads7843
990
David Brownellffa458c2006-01-08 13:34:21 -0800991 // also various AMD Au1x00 devel boards
992
David Brownell2e5a7bd2006-01-08 13:34:25 -0800993 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800994}
995module_init(ads7846_init);
996
997static void __exit ads7846_exit(void)
998{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800999 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001000
1001#ifdef CONFIG_ARCH_OMAP
1002 if (machine_is_omap_osk()) {
1003 omap_free_gpio(4);
1004 omap_free_gpio(6);
1005 }
1006#endif
1007
1008}
1009module_exit(ads7846_exit);
1010
1011MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1012MODULE_LICENSE("GPL");