blob: 1494175ac6fe43d0666e3cfed2426cfb0e622ed5 [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/*
Imre Deak7de90a82006-04-11 23:43:55 -040039 * This code has been tested on an ads7846 / N770 device.
David Brownellffa458c2006-01-08 13:34:21 -080040 * Support for ads7843 and ads7845 has only been stubbed in.
41 *
Imre Deak7de90a82006-04-11 23:43:55 -040042 * Not yet done: How accurate are the temperature and voltage
43 * readings? (System-specific calibration should support
David Brownellffa458c2006-01-08 13:34:21 -080044 * accuracy of 0.3 degrees C; otherwise it's 2.0 degrees.)
45 *
Imre Deak7de90a82006-04-11 23:43:55 -040046 * IRQ handling needs a workaround because of a shortcoming in handling
47 * edge triggered IRQs on some platforms like the OMAP1/2. These
48 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
49 * have to maintain our own SW IRQ disabled status. This should be
50 * removed as soon as the affected platform's IRQ handling is fixed.
51 *
David Brownellffa458c2006-01-08 13:34:21 -080052 * app note sbaa036 talks in more detail about accurate sampling...
53 * that ought to help in situations like LCDs inducing noise (which
54 * can also be helped by using synch signals) and more generally.
Imre Deak7de90a82006-04-11 23:43:55 -040055 * This driver tries to utilize the measures described in the app
56 * note. The strength of filtering can be set in the board-* specific
57 * files.
David Brownellffa458c2006-01-08 13:34:21 -080058 */
59
60#define TS_POLL_PERIOD msecs_to_jiffies(10)
61
David Brownelld93f70b2006-02-15 00:49:35 -050062/* this driver doesn't aim at the peak continuous sample rate */
63#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
64
David Brownellffa458c2006-01-08 13:34:21 -080065struct ts_event {
66 /* For portability, we can't read 12 bit values using SPI (which
67 * would make the controller deliver them as native byteorder u16
David Brownelld93f70b2006-02-15 00:49:35 -050068 * with msbs zeroed). Instead, we read them as two 8-bit values,
David Brownellffa458c2006-01-08 13:34:21 -080069 * which need byteswapping then range adjustment.
70 */
71 __be16 x;
72 __be16 y;
73 __be16 z1, z2;
Imre Deakd5b415c2006-04-26 00:13:18 -040074 int ignore;
David Brownellffa458c2006-01-08 13:34:21 -080075};
76
77struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050078 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080079 char phys[32];
80
81 struct spi_device *spi;
82 u16 model;
83 u16 vref_delay_usecs;
84 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -040085 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -080086
Imre Deak53a0ef892006-04-11 23:41:49 -040087 u8 read_x, read_y, read_z1, read_z2, pwrdown;
88 u16 dummy; /* for the pwrdown read */
David Brownellffa458c2006-01-08 13:34:21 -080089 struct ts_event tc;
90
Imre Deak53a0ef892006-04-11 23:41:49 -040091 struct spi_transfer xfer[10];
Imre Deak0b7018a2006-04-11 23:42:03 -040092 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -040093 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -040094 int msg_idx;
95 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -040096 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -040097 int last_read;
98
99 u16 debounce_max;
100 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400101 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800102
103 spinlock_t lock;
104 struct timer_list timer; /* P: lock */
105 unsigned pendown:1; /* P: lock */
106 unsigned pending:1; /* P: lock */
107// FIXME remove "irq_disabled"
108 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400109 unsigned disabled:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400110
111 int (*get_pendown_state)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800112};
113
114/* leave chip selected when we're done, for quicker re-select? */
115#if 0
116#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
117#else
118#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
119#endif
120
121/*--------------------------------------------------------------------------*/
122
123/* The ADS7846 has touchscreen and other sensors.
124 * Earlier ads784x chips are somewhat compatible.
125 */
126#define ADS_START (1 << 7)
127#define ADS_A2A1A0_d_y (1 << 4) /* differential */
128#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
129#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
130#define ADS_A2A1A0_d_x (5 << 4) /* differential */
131#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
132#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
133#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
134#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
135#define ADS_8_BIT (1 << 3)
136#define ADS_12_BIT (0 << 3)
137#define ADS_SER (1 << 2) /* non-differential */
138#define ADS_DFR (0 << 2) /* differential */
139#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
140#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
141#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
142#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
143
144#define MAX_12BIT ((1<<12)-1)
145
146/* leave ADC powered up (disables penirq) between differential samples */
147#define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
148 | ADS_12_BIT | ADS_DFR)
149
David Brownelld93f70b2006-02-15 00:49:35 -0500150#define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON)
151#define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
152#define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
Imre Deak53a0ef892006-04-11 23:41:49 -0400153
154#define READ_X (READ_12BIT_DFR(x) | ADS_PD10_ADC_ON)
155#define PWRDOWN (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800156
157/* single-ended samples need to first power up reference voltage;
158 * we leave both ADC and VREF powered
159 */
160#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
161 | ADS_12_BIT | ADS_SER)
162
David Brownelld93f70b2006-02-15 00:49:35 -0500163#define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
164#define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
David Brownellffa458c2006-01-08 13:34:21 -0800165
166/*--------------------------------------------------------------------------*/
167
168/*
169 * Non-touchscreen sensors only use single-ended conversions.
170 */
171
172struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500173 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800174 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500175 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800176 u16 scratch;
177 __be16 sample;
178 struct spi_message msg;
179 struct spi_transfer xfer[6];
180};
181
Imre Deak7de90a82006-04-11 23:43:55 -0400182static void ads7846_enable(struct ads7846 *ts);
183static void ads7846_disable(struct ads7846 *ts);
184
Imre Deakc9e617a2006-04-11 23:44:05 -0400185static int device_suspended(struct device *dev)
186{
187 struct ads7846 *ts = dev_get_drvdata(dev);
188 return dev->power.power_state.event != PM_EVENT_ON || ts->disabled;
189}
190
David Brownellffa458c2006-01-08 13:34:21 -0800191static int ads7846_read12_ser(struct device *dev, unsigned command)
192{
193 struct spi_device *spi = to_spi_device(dev);
194 struct ads7846 *ts = dev_get_drvdata(dev);
195 struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL);
196 int status;
197 int sample;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500198 int i;
David Brownellffa458c2006-01-08 13:34:21 -0800199
200 if (!req)
201 return -ENOMEM;
202
Imre Deak0b7018a2006-04-11 23:42:03 -0400203 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800204
David Brownellffa458c2006-01-08 13:34:21 -0800205 /* activate reference, so it has time to settle; */
David Brownelld93f70b2006-02-15 00:49:35 -0500206 req->ref_on = REF_ON;
207 req->xfer[0].tx_buf = &req->ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800208 req->xfer[0].len = 1;
209 req->xfer[1].rx_buf = &req->scratch;
210 req->xfer[1].len = 2;
211
212 /*
213 * for external VREF, 0 usec (and assume it's always on);
214 * for 1uF, use 800 usec;
215 * no cap, 100 usec.
216 */
217 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
218
219 /* take sample */
220 req->command = (u8) command;
221 req->xfer[2].tx_buf = &req->command;
222 req->xfer[2].len = 1;
223 req->xfer[3].rx_buf = &req->sample;
224 req->xfer[3].len = 2;
225
226 /* REVISIT: take a few more samples, and compare ... */
227
228 /* turn off reference */
David Brownelld93f70b2006-02-15 00:49:35 -0500229 req->ref_off = REF_OFF;
230 req->xfer[4].tx_buf = &req->ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800231 req->xfer[4].len = 1;
232 req->xfer[5].rx_buf = &req->scratch;
233 req->xfer[5].len = 2;
234
235 CS_CHANGE(req->xfer[5]);
236
237 /* group all the transfers together, so we can't interfere with
238 * reading touchscreen state; disable penirq while sampling
239 */
Vitaly Wool8275c642006-01-08 13:34:28 -0800240 for (i = 0; i < 6; i++)
241 spi_message_add_tail(&req->xfer[i], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800242
Imre Deakc9e617a2006-04-11 23:44:05 -0400243 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800244 disable_irq(spi->irq);
245 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400246 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800247 enable_irq(spi->irq);
248
249 if (req->msg.status)
250 status = req->msg.status;
251 sample = be16_to_cpu(req->sample);
252 sample = sample >> 4;
253 kfree(req);
254
255 return status ? status : sample;
256}
257
258#define SHOW(name) static ssize_t \
259name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
260{ \
261 ssize_t v = ads7846_read12_ser(dev, \
262 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
263 if (v < 0) \
264 return v; \
265 return sprintf(buf, "%u\n", (unsigned) v); \
266} \
267static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
268
269SHOW(temp0)
270SHOW(temp1)
271SHOW(vaux)
272SHOW(vbatt)
273
Imre Deak438f2a72006-04-11 23:41:32 -0400274static int is_pen_down(struct device *dev)
275{
276 struct ads7846 *ts = dev_get_drvdata(dev);
277
278 return ts->pendown;
279}
280
281static ssize_t ads7846_pen_down_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
283{
284 return sprintf(buf, "%u\n", is_pen_down(dev));
285}
286
287static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
288
Imre Deak7de90a82006-04-11 23:43:55 -0400289static ssize_t ads7846_disable_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
291{
292 struct ads7846 *ts = dev_get_drvdata(dev);
293
294 return sprintf(buf, "%u\n", ts->disabled);
295}
296
297static ssize_t ads7846_disable_store(struct device *dev,
298 struct device_attribute *attr,
299 const char *buf, size_t count)
300{
301 struct ads7846 *ts = dev_get_drvdata(dev);
302 char *endp;
303 int i;
304
305 i = simple_strtoul(buf, &endp, 10);
306 spin_lock_irq(&ts->lock);
307
308 if (i)
309 ads7846_disable(ts);
310 else
311 ads7846_enable(ts);
312
313 spin_unlock_irq(&ts->lock);
314
315 return count;
316}
317
318static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
319
David Brownellffa458c2006-01-08 13:34:21 -0800320/*--------------------------------------------------------------------------*/
321
322/*
323 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
324 * to retrieve touchscreen status.
325 *
326 * The SPI transfer completion callback does the real work. It reports
327 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
328 */
329
330static void ads7846_rx(void *ads)
331{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500332 struct ads7846 *ts = ads;
333 struct input_dev *input_dev = ts->input;
334 unsigned Rt;
335 unsigned sync = 0;
336 u16 x, y, z1, z2;
337 unsigned long flags;
David Brownellffa458c2006-01-08 13:34:21 -0800338
339 /* adjust: 12 bit samples (left aligned), built from
340 * two 8 bit values writen msb-first.
341 */
342 x = be16_to_cpu(ts->tc.x) >> 4;
343 y = be16_to_cpu(ts->tc.y) >> 4;
344 z1 = be16_to_cpu(ts->tc.z1) >> 4;
345 z2 = be16_to_cpu(ts->tc.z2) >> 4;
346
347 /* range filtering */
348 if (x == MAX_12BIT)
349 x = 0;
350
Imre Deakc9e617a2006-04-11 23:44:05 -0400351 if (likely(x && z1 && !device_suspended(&ts->spi->dev))) {
David Brownellffa458c2006-01-08 13:34:21 -0800352 /* compute touch pressure resistance using equation #2 */
353 Rt = z2;
354 Rt -= z1;
355 Rt *= x;
356 Rt *= ts->x_plate_ohms;
357 Rt /= z1;
358 Rt = (Rt + 2047) >> 12;
359 } else
360 Rt = 0;
361
Imre Deakd5b415c2006-04-26 00:13:18 -0400362 /* Sample found inconsistent by debouncing or pressure is beyond
363 * the maximum. Don't report it to user space, repeat at least
364 * once more the measurement */
365 if (ts->tc.ignore || Rt > ts->pressure_max) {
366 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
367 return;
368 }
369
David Brownellffa458c2006-01-08 13:34:21 -0800370 /* NOTE: "pendown" is inferred from pressure; we don't rely on
371 * being able to check nPENIRQ status, or "friendly" trigger modes
372 * (both-edges is much better than just-falling or low-level).
373 *
374 * REVISIT: some boards may require reading nPENIRQ; it's
375 * needed on 7843. and 7845 reads pressure differently...
376 *
377 * REVISIT: the touchscreen might not be connected; this code
378 * won't notice that, even if nPENIRQ never fires ...
379 */
380 if (!ts->pendown && Rt != 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500381 input_report_key(input_dev, BTN_TOUCH, 1);
David Brownellffa458c2006-01-08 13:34:21 -0800382 sync = 1;
383 } else if (ts->pendown && Rt == 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500384 input_report_key(input_dev, BTN_TOUCH, 0);
David Brownellffa458c2006-01-08 13:34:21 -0800385 sync = 1;
386 }
387
388 if (Rt) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500389 input_report_abs(input_dev, ABS_X, x);
390 input_report_abs(input_dev, ABS_Y, y);
David Brownellffa458c2006-01-08 13:34:21 -0800391 sync = 1;
392 }
Imre Deakae82d5a2006-04-26 00:12:14 -0400393
394 if (sync) {
395 input_report_abs(input_dev, ABS_PRESSURE, Rt);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500396 input_sync(input_dev);
Imre Deakae82d5a2006-04-26 00:12:14 -0400397 }
David Brownellffa458c2006-01-08 13:34:21 -0800398
399#ifdef VERBOSE
400 if (Rt || ts->pendown)
401 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
402 x, y, Rt, Rt ? "" : " UP");
403#endif
404
David Brownellffa458c2006-01-08 13:34:21 -0800405 spin_lock_irqsave(&ts->lock, flags);
406
407 ts->pendown = (Rt != 0);
Imre Deakc9e617a2006-04-11 23:44:05 -0400408 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
David Brownellffa458c2006-01-08 13:34:21 -0800409
410 spin_unlock_irqrestore(&ts->lock, flags);
411}
412
Imre Deak0b7018a2006-04-11 23:42:03 -0400413static void ads7846_debounce(void *ads)
414{
415 struct ads7846 *ts = ads;
416 struct spi_message *m;
417 struct spi_transfer *t;
Imre Deakd5b415c2006-04-26 00:13:18 -0400418 int val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400419 int status;
420
421 m = &ts->msg[ts->msg_idx];
422 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
423 val = (*(u16 *)t->rx_buf) >> 3;
Imre Deakd5b415c2006-04-26 00:13:18 -0400424 if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol)) {
425 /* Repeat it, if this was the first read or the read
426 * wasn't consistent enough. */
427 if (ts->read_cnt < ts->debounce_max) {
428 ts->last_read = val;
429 ts->read_cnt++;
430 } else {
431 /* Maximum number of debouncing reached and still
432 * not enough number of consistent readings. Abort
433 * the whole sample, repeat it in the next sampling
434 * period.
435 */
436 ts->tc.ignore = 1;
437 ts->read_cnt = 0;
438 /* Last message will contain ads7846_rx() as the
439 * completion function.
440 */
441 m = ts->last_msg;
442 }
443 /* Start over collecting consistent readings. */
444 ts->read_rep = 0;
Imre Deak0b7018a2006-04-11 23:42:03 -0400445 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400446 if (++ts->read_rep > ts->debounce_rep) {
447 /* Got a good reading for this coordinate,
448 * go for the next one. */
449 ts->tc.ignore = 0;
450 ts->msg_idx++;
451 ts->read_cnt = 0;
452 ts->read_rep = 0;
453 m++;
454 } else
455 /* Read more values that are consistent. */
456 ts->read_cnt++;
Imre Deak0b7018a2006-04-11 23:42:03 -0400457 }
458 status = spi_async(ts->spi, m);
459 if (status)
460 dev_err(&ts->spi->dev, "spi_async --> %d\n",
461 status);
462}
463
David Brownellffa458c2006-01-08 13:34:21 -0800464static void ads7846_timer(unsigned long handle)
465{
466 struct ads7846 *ts = (void *)handle;
467 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800468
Imre Deakc9e617a2006-04-11 23:44:05 -0400469 spin_lock_irq(&ts->lock);
470
471 if (unlikely(ts->msg_idx && !ts->pendown)) {
472 /* measurment cycle ended */
473 if (!device_suspended(&ts->spi->dev)) {
474 ts->irq_disabled = 0;
475 enable_irq(ts->spi->irq);
476 }
477 ts->pending = 0;
478 ts->msg_idx = 0;
479 } else {
480 /* pen is still down, continue with the measurement */
481 ts->msg_idx = 0;
482 status = spi_async(ts->spi, &ts->msg[0]);
483 if (status)
484 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
485 }
486
487 spin_unlock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800488}
489
490static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
491{
Imre Deak0b7018a2006-04-11 23:42:03 -0400492 struct ads7846 *ts = handle;
493 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400494
495 spin_lock_irqsave(&ts->lock, flags);
Imre Deakc9e617a2006-04-11 23:44:05 -0400496 if (likely(ts->get_pendown_state())) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400497 if (!ts->irq_disabled) {
498 /* REVISIT irq logic for many ARM chips has cloned a
499 * bug wherein disabling an irq in its handler won't
500 * work;(it's disabled lazily, and too late to work.
501 * until all their irq logic is fixed, we must shadow
502 * that state here.
503 */
504 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400505 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400506 ts->pending = 1;
507 mod_timer(&ts->timer, jiffies);
508 }
509 }
510 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400511
512 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800513}
514
515/*--------------------------------------------------------------------------*/
516
Imre Deak7de90a82006-04-11 23:43:55 -0400517/* Must be called with ts->lock held */
518static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800519{
Imre Deak7de90a82006-04-11 23:43:55 -0400520 if (ts->disabled)
521 return;
David Brownellffa458c2006-01-08 13:34:21 -0800522
Imre Deakc9e617a2006-04-11 23:44:05 -0400523 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800524
Imre Deakc9e617a2006-04-11 23:44:05 -0400525 /* are we waiting for IRQ, or polling? */
526 if (!ts->pending) {
527 ts->irq_disabled = 1;
528 disable_irq(ts->spi->irq);
529 } else {
530 /* the timer will run at least once more, and
531 * leave everything in a clean state, IRQ disabled
532 */
533 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400534 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400535 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400536 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800537 }
538 }
539
540 /* we know the chip's in lowpower mode since we always
541 * leave it that way after every request
542 */
543
Imre Deak7de90a82006-04-11 23:43:55 -0400544}
545
546/* Must be called with ts->lock held */
547static void ads7846_enable(struct ads7846 *ts)
548{
549 if (!ts->disabled)
550 return;
551
552 ts->disabled = 0;
553 ts->irq_disabled = 0;
554 enable_irq(ts->spi->irq);
555}
556
557static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
558{
559 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
560
561 spin_lock_irq(&ts->lock);
562
563 spi->dev.power.power_state = message;
564 ads7846_disable(ts);
565
566 spin_unlock_irq(&ts->lock);
567
David Brownellffa458c2006-01-08 13:34:21 -0800568 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400569
David Brownellffa458c2006-01-08 13:34:21 -0800570}
571
David Brownell2e5a7bd2006-01-08 13:34:25 -0800572static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800573{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800574 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800575
Imre Deak7de90a82006-04-11 23:43:55 -0400576 spin_lock_irq(&ts->lock);
577
David Brownell2e5a7bd2006-01-08 13:34:25 -0800578 spi->dev.power.power_state = PMSG_ON;
Imre Deak7de90a82006-04-11 23:43:55 -0400579 ads7846_enable(ts);
580
581 spin_unlock_irq(&ts->lock);
582
David Brownellffa458c2006-01-08 13:34:21 -0800583 return 0;
584}
585
David Brownell2e5a7bd2006-01-08 13:34:25 -0800586static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800587{
David Brownellffa458c2006-01-08 13:34:21 -0800588 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500589 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800590 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400591 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800592 struct spi_transfer *x;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500593 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800594
595 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800596 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800597 return -ENODEV;
598 }
599
600 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800601 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800602 return -ENODEV;
603 }
604
605 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500606 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800607 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500608 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800609 return -EINVAL;
610 }
611
Imre Deakc9e617a2006-04-11 23:44:05 -0400612 if (pdata->get_pendown_state == NULL) {
613 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
614 return -EINVAL;
615 }
616
David Brownellffa458c2006-01-08 13:34:21 -0800617 /* We'd set the wordsize to 12 bits ... except that some controllers
618 * will then treat the 8 bit command words as 12 bits (and drop the
619 * four MSBs of the 12 bit result). Result: inputs must be shifted
620 * to discard the four garbage LSBs.
621 */
622
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500623 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
624 input_dev = input_allocate_device();
625 if (!ts || !input_dev) {
626 err = -ENOMEM;
627 goto err_free_mem;
628 }
David Brownellffa458c2006-01-08 13:34:21 -0800629
David Brownell2e5a7bd2006-01-08 13:34:25 -0800630 dev_set_drvdata(&spi->dev, ts);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500631 spi->dev.power.power_state = PMSG_ON;
David Brownellffa458c2006-01-08 13:34:21 -0800632
633 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500634 ts->input = input_dev;
David Brownellffa458c2006-01-08 13:34:21 -0800635
636 init_timer(&ts->timer);
637 ts->timer.data = (unsigned long) ts;
638 ts->timer.function = ads7846_timer;
639
Imre Deak7de90a82006-04-11 23:43:55 -0400640 spin_lock_init(&ts->lock);
641
David Brownellffa458c2006-01-08 13:34:21 -0800642 ts->model = pdata->model ? : 7846;
643 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
644 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400645 ts->pressure_max = pdata->pressure_max ? : ~0;
646 if (pdata->debounce_max) {
647 ts->debounce_max = pdata->debounce_max;
648 ts->debounce_tol = pdata->debounce_tol;
649 ts->debounce_rep = pdata->debounce_rep;
650 if (ts->debounce_rep > ts->debounce_max + 1)
651 ts->debounce_rep = ts->debounce_max - 1;
652 } else
653 ts->debounce_tol = ~0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400654 ts->get_pendown_state = pdata->get_pendown_state;
David Brownellffa458c2006-01-08 13:34:21 -0800655
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500656 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800657
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500658 input_dev->name = "ADS784x Touchscreen";
659 input_dev->phys = ts->phys;
660 input_dev->cdev.dev = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800661
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500662 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
663 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
664 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800665 pdata->x_min ? : 0,
666 pdata->x_max ? : MAX_12BIT,
667 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500668 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800669 pdata->y_min ? : 0,
670 pdata->y_max ? : MAX_12BIT,
671 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500672 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800673 pdata->pressure_min, pdata->pressure_max, 0, 0);
674
David Brownellffa458c2006-01-08 13:34:21 -0800675 /* set up the transfers to read touchscreen state; this assumes we
676 * use formula #2 for pressure, not #3.
677 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400678 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800679 x = ts->xfer;
680
Imre Deak0b7018a2006-04-11 23:42:03 -0400681 spi_message_init(m);
682
David Brownellffa458c2006-01-08 13:34:21 -0800683 /* y- still on; turn on only y+ (and ADC) */
David Brownelld93f70b2006-02-15 00:49:35 -0500684 ts->read_y = READ_Y;
685 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800686 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400687 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500688
David Brownellffa458c2006-01-08 13:34:21 -0800689 x++;
690 x->rx_buf = &ts->tc.y;
691 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400692 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800693
Imre Deak0b7018a2006-04-11 23:42:03 -0400694 m->complete = ads7846_debounce;
695 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500696
Imre Deak0b7018a2006-04-11 23:42:03 -0400697 m++;
698 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800699
700 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500701 x++;
702 ts->read_x = READ_X;
703 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800704 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400705 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500706
David Brownellffa458c2006-01-08 13:34:21 -0800707 x++;
708 x->rx_buf = &ts->tc.x;
709 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400710 spi_message_add_tail(x, m);
711
712 m->complete = ads7846_debounce;
713 m->context = ts;
714
715 /* turn y+ off, x- on; we'll use formula #2 */
716 if (ts->model == 7846) {
717 m++;
718 spi_message_init(m);
719
720 x++;
721 ts->read_z1 = READ_Z1;
722 x->tx_buf = &ts->read_z1;
723 x->len = 1;
724 spi_message_add_tail(x, m);
725
726 x++;
727 x->rx_buf = &ts->tc.z1;
728 x->len = 2;
729 spi_message_add_tail(x, m);
730
731 m->complete = ads7846_debounce;
732 m->context = ts;
733
734 m++;
735 spi_message_init(m);
736
737 x++;
738 ts->read_z2 = READ_Z2;
739 x->tx_buf = &ts->read_z2;
740 x->len = 1;
741 spi_message_add_tail(x, m);
742
743 x++;
744 x->rx_buf = &ts->tc.z2;
745 x->len = 2;
746 spi_message_add_tail(x, m);
747
748 m->complete = ads7846_debounce;
749 m->context = ts;
750 }
Imre Deak53a0ef892006-04-11 23:41:49 -0400751
752 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -0400753 m++;
754 spi_message_init(m);
755
Imre Deak53a0ef892006-04-11 23:41:49 -0400756 x++;
757 ts->pwrdown = PWRDOWN;
758 x->tx_buf = &ts->pwrdown;
759 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400760 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -0400761
762 x++;
763 x->rx_buf = &ts->dummy;
764 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500765 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -0400766 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800767
Imre Deak0b7018a2006-04-11 23:42:03 -0400768 m->complete = ads7846_rx;
769 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -0800770
Imre Deakd5b415c2006-04-26 00:13:18 -0400771 ts->last_msg = m;
772
Russell Kingf43aaba2006-01-19 12:26:57 +0000773 if (request_irq(spi->irq, ads7846_irq,
774 SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
775 spi->dev.bus_id, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800776 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500777 err = -EBUSY;
778 goto err_free_mem;
David Brownellffa458c2006-01-08 13:34:21 -0800779 }
David Brownellffa458c2006-01-08 13:34:21 -0800780
David Brownell2e5a7bd2006-01-08 13:34:25 -0800781 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -0800782
783 /* take a first sample, leaving nPENIRQ active; avoid
784 * the touchscreen, in case it's not connected.
785 */
David Brownell2e5a7bd2006-01-08 13:34:25 -0800786 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -0800787 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
788
789 /* ads7843/7845 don't have temperature sensors, and
790 * use the other sensors a bit differently too
791 */
792 if (ts->model == 7846) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800793 device_create_file(&spi->dev, &dev_attr_temp0);
794 device_create_file(&spi->dev, &dev_attr_temp1);
David Brownellffa458c2006-01-08 13:34:21 -0800795 }
796 if (ts->model != 7845)
David Brownell2e5a7bd2006-01-08 13:34:25 -0800797 device_create_file(&spi->dev, &dev_attr_vbatt);
798 device_create_file(&spi->dev, &dev_attr_vaux);
David Brownellffa458c2006-01-08 13:34:21 -0800799
Imre Deak438f2a72006-04-11 23:41:32 -0400800 device_create_file(&spi->dev, &dev_attr_pen_down);
801
Imre Deak7de90a82006-04-11 23:43:55 -0400802 device_create_file(&spi->dev, &dev_attr_disable);
803
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500804 err = input_register_device(input_dev);
805 if (err)
Imre Deak7de90a82006-04-11 23:43:55 -0400806 goto err_remove_attr;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500807
David Brownellffa458c2006-01-08 13:34:21 -0800808 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500809
Imre Deak7de90a82006-04-11 23:43:55 -0400810 err_remove_attr:
811 device_remove_file(&spi->dev, &dev_attr_disable);
812 device_remove_file(&spi->dev, &dev_attr_pen_down);
813 if (ts->model == 7846) {
814 device_remove_file(&spi->dev, &dev_attr_temp1);
815 device_remove_file(&spi->dev, &dev_attr_temp0);
816 }
817 if (ts->model != 7845)
818 device_remove_file(&spi->dev, &dev_attr_vbatt);
819 device_remove_file(&spi->dev, &dev_attr_vaux);
820
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500821 free_irq(spi->irq, ts);
822 err_free_mem:
823 input_free_device(input_dev);
824 kfree(ts);
825 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800826}
827
David Brownell2e5a7bd2006-01-08 13:34:25 -0800828static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800829{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800830 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800831
Imre Deak7de90a82006-04-11 23:43:55 -0400832 input_unregister_device(ts->input);
833
David Brownell2e5a7bd2006-01-08 13:34:25 -0800834 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -0800835
Imre Deak7de90a82006-04-11 23:43:55 -0400836 device_remove_file(&spi->dev, &dev_attr_disable);
Imre Deak438f2a72006-04-11 23:41:32 -0400837 device_remove_file(&spi->dev, &dev_attr_pen_down);
David Brownellffa458c2006-01-08 13:34:21 -0800838 if (ts->model == 7846) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800839 device_remove_file(&spi->dev, &dev_attr_temp1);
Imre Deak7de90a82006-04-11 23:43:55 -0400840 device_remove_file(&spi->dev, &dev_attr_temp0);
David Brownellffa458c2006-01-08 13:34:21 -0800841 }
842 if (ts->model != 7845)
David Brownell2e5a7bd2006-01-08 13:34:25 -0800843 device_remove_file(&spi->dev, &dev_attr_vbatt);
844 device_remove_file(&spi->dev, &dev_attr_vaux);
David Brownellffa458c2006-01-08 13:34:21 -0800845
Imre Deak7de90a82006-04-11 23:43:55 -0400846 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -0400847 /* suspend left the IRQ disabled */
848 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -0400849
David Brownellffa458c2006-01-08 13:34:21 -0800850 kfree(ts);
851
David Brownell2e5a7bd2006-01-08 13:34:25 -0800852 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -0800853 return 0;
854}
855
David Brownell2e5a7bd2006-01-08 13:34:25 -0800856static struct spi_driver ads7846_driver = {
857 .driver = {
858 .name = "ads7846",
859 .bus = &spi_bus_type,
860 .owner = THIS_MODULE,
861 },
David Brownellffa458c2006-01-08 13:34:21 -0800862 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -0800863 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -0800864 .suspend = ads7846_suspend,
865 .resume = ads7846_resume,
866};
867
868static int __init ads7846_init(void)
869{
870 /* grr, board-specific init should stay out of drivers!! */
871
872#ifdef CONFIG_ARCH_OMAP
873 if (machine_is_omap_osk()) {
874 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
875 omap_request_gpio(4);
876 omap_set_gpio_direction(4, 1);
877 omap_request_gpio(6);
878 omap_set_gpio_direction(6, 1);
879 }
880 // also TI 1510 Innovator, bitbanging through FPGA
881 // also Nokia 770
882 // also Palm Tungsten T2
883#endif
884
885 // PXA:
886 // also Dell Axim X50
887 // also HP iPaq H191x/H192x/H415x/H435x
David Brownell2e5a7bd2006-01-08 13:34:25 -0800888 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
David Brownellffa458c2006-01-08 13:34:21 -0800889 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
890
David Brownell2e5a7bd2006-01-08 13:34:25 -0800891 // Atmel at91sam9261-EK uses ads7843
892
David Brownellffa458c2006-01-08 13:34:21 -0800893 // also various AMD Au1x00 devel boards
894
David Brownell2e5a7bd2006-01-08 13:34:25 -0800895 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800896}
897module_init(ads7846_init);
898
899static void __exit ads7846_exit(void)
900{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800901 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800902
903#ifdef CONFIG_ARCH_OMAP
904 if (machine_is_omap_osk()) {
905 omap_free_gpio(4);
906 omap_free_gpio(6);
907 }
908#endif
909
910}
911module_exit(ads7846_exit);
912
913MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
914MODULE_LICENSE("GPL");