blob: e7cabf12c8dc77a46e525fd4984870e289436fee [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;
74};
75
76struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050077 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080078 char phys[32];
79
80 struct spi_device *spi;
81 u16 model;
82 u16 vref_delay_usecs;
83 u16 x_plate_ohms;
84
Imre Deak53a0ef892006-04-11 23:41:49 -040085 u8 read_x, read_y, read_z1, read_z2, pwrdown;
86 u16 dummy; /* for the pwrdown read */
David Brownellffa458c2006-01-08 13:34:21 -080087 struct ts_event tc;
88
Imre Deak53a0ef892006-04-11 23:41:49 -040089 struct spi_transfer xfer[10];
Imre Deak0b7018a2006-04-11 23:42:03 -040090 struct spi_message msg[5];
91 int msg_idx;
92 int read_cnt;
93 int last_read;
94
95 u16 debounce_max;
96 u16 debounce_tol;
David Brownellffa458c2006-01-08 13:34:21 -080097
98 spinlock_t lock;
99 struct timer_list timer; /* P: lock */
100 unsigned pendown:1; /* P: lock */
101 unsigned pending:1; /* P: lock */
102// FIXME remove "irq_disabled"
103 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400104 unsigned disabled:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400105
106 int (*get_pendown_state)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800107};
108
109/* leave chip selected when we're done, for quicker re-select? */
110#if 0
111#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
112#else
113#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
114#endif
115
116/*--------------------------------------------------------------------------*/
117
118/* The ADS7846 has touchscreen and other sensors.
119 * Earlier ads784x chips are somewhat compatible.
120 */
121#define ADS_START (1 << 7)
122#define ADS_A2A1A0_d_y (1 << 4) /* differential */
123#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
124#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
125#define ADS_A2A1A0_d_x (5 << 4) /* differential */
126#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
127#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
128#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
129#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
130#define ADS_8_BIT (1 << 3)
131#define ADS_12_BIT (0 << 3)
132#define ADS_SER (1 << 2) /* non-differential */
133#define ADS_DFR (0 << 2) /* differential */
134#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
135#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
136#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
137#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
138
139#define MAX_12BIT ((1<<12)-1)
140
141/* leave ADC powered up (disables penirq) between differential samples */
142#define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
143 | ADS_12_BIT | ADS_DFR)
144
David Brownelld93f70b2006-02-15 00:49:35 -0500145#define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON)
146#define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
147#define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
Imre Deak53a0ef892006-04-11 23:41:49 -0400148
149#define READ_X (READ_12BIT_DFR(x) | ADS_PD10_ADC_ON)
150#define PWRDOWN (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800151
152/* single-ended samples need to first power up reference voltage;
153 * we leave both ADC and VREF powered
154 */
155#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
156 | ADS_12_BIT | ADS_SER)
157
David Brownelld93f70b2006-02-15 00:49:35 -0500158#define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
159#define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
David Brownellffa458c2006-01-08 13:34:21 -0800160
161/*--------------------------------------------------------------------------*/
162
163/*
164 * Non-touchscreen sensors only use single-ended conversions.
165 */
166
167struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500168 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800169 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500170 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800171 u16 scratch;
172 __be16 sample;
173 struct spi_message msg;
174 struct spi_transfer xfer[6];
175};
176
Imre Deak7de90a82006-04-11 23:43:55 -0400177static void ads7846_enable(struct ads7846 *ts);
178static void ads7846_disable(struct ads7846 *ts);
179
Imre Deakc9e617a2006-04-11 23:44:05 -0400180static int device_suspended(struct device *dev)
181{
182 struct ads7846 *ts = dev_get_drvdata(dev);
183 return dev->power.power_state.event != PM_EVENT_ON || ts->disabled;
184}
185
David Brownellffa458c2006-01-08 13:34:21 -0800186static int ads7846_read12_ser(struct device *dev, unsigned command)
187{
188 struct spi_device *spi = to_spi_device(dev);
189 struct ads7846 *ts = dev_get_drvdata(dev);
190 struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL);
191 int status;
192 int sample;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500193 int i;
David Brownellffa458c2006-01-08 13:34:21 -0800194
195 if (!req)
196 return -ENOMEM;
197
Imre Deak0b7018a2006-04-11 23:42:03 -0400198 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800199
David Brownellffa458c2006-01-08 13:34:21 -0800200 /* activate reference, so it has time to settle; */
David Brownelld93f70b2006-02-15 00:49:35 -0500201 req->ref_on = REF_ON;
202 req->xfer[0].tx_buf = &req->ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800203 req->xfer[0].len = 1;
204 req->xfer[1].rx_buf = &req->scratch;
205 req->xfer[1].len = 2;
206
207 /*
208 * for external VREF, 0 usec (and assume it's always on);
209 * for 1uF, use 800 usec;
210 * no cap, 100 usec.
211 */
212 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
213
214 /* take sample */
215 req->command = (u8) command;
216 req->xfer[2].tx_buf = &req->command;
217 req->xfer[2].len = 1;
218 req->xfer[3].rx_buf = &req->sample;
219 req->xfer[3].len = 2;
220
221 /* REVISIT: take a few more samples, and compare ... */
222
223 /* turn off reference */
David Brownelld93f70b2006-02-15 00:49:35 -0500224 req->ref_off = REF_OFF;
225 req->xfer[4].tx_buf = &req->ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800226 req->xfer[4].len = 1;
227 req->xfer[5].rx_buf = &req->scratch;
228 req->xfer[5].len = 2;
229
230 CS_CHANGE(req->xfer[5]);
231
232 /* group all the transfers together, so we can't interfere with
233 * reading touchscreen state; disable penirq while sampling
234 */
Vitaly Wool8275c642006-01-08 13:34:28 -0800235 for (i = 0; i < 6; i++)
236 spi_message_add_tail(&req->xfer[i], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800237
Imre Deakc9e617a2006-04-11 23:44:05 -0400238 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800239 disable_irq(spi->irq);
240 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400241 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800242 enable_irq(spi->irq);
243
244 if (req->msg.status)
245 status = req->msg.status;
246 sample = be16_to_cpu(req->sample);
247 sample = sample >> 4;
248 kfree(req);
249
250 return status ? status : sample;
251}
252
253#define SHOW(name) static ssize_t \
254name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
255{ \
256 ssize_t v = ads7846_read12_ser(dev, \
257 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
258 if (v < 0) \
259 return v; \
260 return sprintf(buf, "%u\n", (unsigned) v); \
261} \
262static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
263
264SHOW(temp0)
265SHOW(temp1)
266SHOW(vaux)
267SHOW(vbatt)
268
Imre Deak438f2a72006-04-11 23:41:32 -0400269static int is_pen_down(struct device *dev)
270{
271 struct ads7846 *ts = dev_get_drvdata(dev);
272
273 return ts->pendown;
274}
275
276static ssize_t ads7846_pen_down_show(struct device *dev,
277 struct device_attribute *attr, char *buf)
278{
279 return sprintf(buf, "%u\n", is_pen_down(dev));
280}
281
282static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
283
Imre Deak7de90a82006-04-11 23:43:55 -0400284static ssize_t ads7846_disable_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
286{
287 struct ads7846 *ts = dev_get_drvdata(dev);
288
289 return sprintf(buf, "%u\n", ts->disabled);
290}
291
292static ssize_t ads7846_disable_store(struct device *dev,
293 struct device_attribute *attr,
294 const char *buf, size_t count)
295{
296 struct ads7846 *ts = dev_get_drvdata(dev);
297 char *endp;
298 int i;
299
300 i = simple_strtoul(buf, &endp, 10);
301 spin_lock_irq(&ts->lock);
302
303 if (i)
304 ads7846_disable(ts);
305 else
306 ads7846_enable(ts);
307
308 spin_unlock_irq(&ts->lock);
309
310 return count;
311}
312
313static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
314
David Brownellffa458c2006-01-08 13:34:21 -0800315/*--------------------------------------------------------------------------*/
316
317/*
318 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
319 * to retrieve touchscreen status.
320 *
321 * The SPI transfer completion callback does the real work. It reports
322 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
323 */
324
325static void ads7846_rx(void *ads)
326{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500327 struct ads7846 *ts = ads;
328 struct input_dev *input_dev = ts->input;
329 unsigned Rt;
330 unsigned sync = 0;
331 u16 x, y, z1, z2;
332 unsigned long flags;
David Brownellffa458c2006-01-08 13:34:21 -0800333
334 /* adjust: 12 bit samples (left aligned), built from
335 * two 8 bit values writen msb-first.
336 */
337 x = be16_to_cpu(ts->tc.x) >> 4;
338 y = be16_to_cpu(ts->tc.y) >> 4;
339 z1 = be16_to_cpu(ts->tc.z1) >> 4;
340 z2 = be16_to_cpu(ts->tc.z2) >> 4;
341
342 /* range filtering */
343 if (x == MAX_12BIT)
344 x = 0;
345
Imre Deakc9e617a2006-04-11 23:44:05 -0400346 if (likely(x && z1 && !device_suspended(&ts->spi->dev))) {
David Brownellffa458c2006-01-08 13:34:21 -0800347 /* compute touch pressure resistance using equation #2 */
348 Rt = z2;
349 Rt -= z1;
350 Rt *= x;
351 Rt *= ts->x_plate_ohms;
352 Rt /= z1;
353 Rt = (Rt + 2047) >> 12;
354 } else
355 Rt = 0;
356
357 /* NOTE: "pendown" is inferred from pressure; we don't rely on
358 * being able to check nPENIRQ status, or "friendly" trigger modes
359 * (both-edges is much better than just-falling or low-level).
360 *
361 * REVISIT: some boards may require reading nPENIRQ; it's
362 * needed on 7843. and 7845 reads pressure differently...
363 *
364 * REVISIT: the touchscreen might not be connected; this code
365 * won't notice that, even if nPENIRQ never fires ...
366 */
367 if (!ts->pendown && Rt != 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500368 input_report_key(input_dev, BTN_TOUCH, 1);
David Brownellffa458c2006-01-08 13:34:21 -0800369 sync = 1;
370 } else if (ts->pendown && Rt == 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500371 input_report_key(input_dev, BTN_TOUCH, 0);
David Brownellffa458c2006-01-08 13:34:21 -0800372 sync = 1;
373 }
374
375 if (Rt) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500376 input_report_abs(input_dev, ABS_X, x);
377 input_report_abs(input_dev, ABS_Y, y);
378 input_report_abs(input_dev, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800379 sync = 1;
380 }
381 if (sync)
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500382 input_sync(input_dev);
David Brownellffa458c2006-01-08 13:34:21 -0800383
384#ifdef VERBOSE
385 if (Rt || ts->pendown)
386 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
387 x, y, Rt, Rt ? "" : " UP");
388#endif
389
David Brownellffa458c2006-01-08 13:34:21 -0800390 spin_lock_irqsave(&ts->lock, flags);
391
392 ts->pendown = (Rt != 0);
Imre Deakc9e617a2006-04-11 23:44:05 -0400393 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
David Brownellffa458c2006-01-08 13:34:21 -0800394
395 spin_unlock_irqrestore(&ts->lock, flags);
396}
397
Imre Deak0b7018a2006-04-11 23:42:03 -0400398static void ads7846_debounce(void *ads)
399{
400 struct ads7846 *ts = ads;
401 struct spi_message *m;
402 struct spi_transfer *t;
403 u16 val;
404 int status;
405
406 m = &ts->msg[ts->msg_idx];
407 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
408 val = (*(u16 *)t->rx_buf) >> 3;
409
410 if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol
411 && ts->read_cnt < ts->debounce_max)) {
412 /* Repeat it, if this was the first read or the read wasn't
413 * consistent enough
414 */
415 ts->read_cnt++;
416 ts->last_read = val;
417 } else {
418 /* Go for the next read */
419 ts->msg_idx++;
420 ts->read_cnt = 0;
421 m++;
422 }
423 status = spi_async(ts->spi, m);
424 if (status)
425 dev_err(&ts->spi->dev, "spi_async --> %d\n",
426 status);
427}
428
David Brownellffa458c2006-01-08 13:34:21 -0800429static void ads7846_timer(unsigned long handle)
430{
431 struct ads7846 *ts = (void *)handle;
432 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800433
Imre Deakc9e617a2006-04-11 23:44:05 -0400434 spin_lock_irq(&ts->lock);
435
436 if (unlikely(ts->msg_idx && !ts->pendown)) {
437 /* measurment cycle ended */
438 if (!device_suspended(&ts->spi->dev)) {
439 ts->irq_disabled = 0;
440 enable_irq(ts->spi->irq);
441 }
442 ts->pending = 0;
443 ts->msg_idx = 0;
444 } else {
445 /* pen is still down, continue with the measurement */
446 ts->msg_idx = 0;
447 status = spi_async(ts->spi, &ts->msg[0]);
448 if (status)
449 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
450 }
451
452 spin_unlock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800453}
454
455static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
456{
Imre Deak0b7018a2006-04-11 23:42:03 -0400457 struct ads7846 *ts = handle;
458 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400459
460 spin_lock_irqsave(&ts->lock, flags);
Imre Deakc9e617a2006-04-11 23:44:05 -0400461 if (likely(ts->get_pendown_state())) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400462 if (!ts->irq_disabled) {
463 /* REVISIT irq logic for many ARM chips has cloned a
464 * bug wherein disabling an irq in its handler won't
465 * work;(it's disabled lazily, and too late to work.
466 * until all their irq logic is fixed, we must shadow
467 * that state here.
468 */
469 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400470 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400471 ts->pending = 1;
472 mod_timer(&ts->timer, jiffies);
473 }
474 }
475 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400476
477 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800478}
479
480/*--------------------------------------------------------------------------*/
481
Imre Deak7de90a82006-04-11 23:43:55 -0400482/* Must be called with ts->lock held */
483static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800484{
Imre Deak7de90a82006-04-11 23:43:55 -0400485 if (ts->disabled)
486 return;
David Brownellffa458c2006-01-08 13:34:21 -0800487
Imre Deakc9e617a2006-04-11 23:44:05 -0400488 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800489
Imre Deakc9e617a2006-04-11 23:44:05 -0400490 /* are we waiting for IRQ, or polling? */
491 if (!ts->pending) {
492 ts->irq_disabled = 1;
493 disable_irq(ts->spi->irq);
494 } else {
495 /* the timer will run at least once more, and
496 * leave everything in a clean state, IRQ disabled
497 */
498 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400499 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400500 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400501 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800502 }
503 }
504
505 /* we know the chip's in lowpower mode since we always
506 * leave it that way after every request
507 */
508
Imre Deak7de90a82006-04-11 23:43:55 -0400509}
510
511/* Must be called with ts->lock held */
512static void ads7846_enable(struct ads7846 *ts)
513{
514 if (!ts->disabled)
515 return;
516
517 ts->disabled = 0;
518 ts->irq_disabled = 0;
519 enable_irq(ts->spi->irq);
520}
521
522static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
523{
524 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
525
526 spin_lock_irq(&ts->lock);
527
528 spi->dev.power.power_state = message;
529 ads7846_disable(ts);
530
531 spin_unlock_irq(&ts->lock);
532
David Brownellffa458c2006-01-08 13:34:21 -0800533 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400534
David Brownellffa458c2006-01-08 13:34:21 -0800535}
536
David Brownell2e5a7bd2006-01-08 13:34:25 -0800537static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800538{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800539 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800540
Imre Deak7de90a82006-04-11 23:43:55 -0400541 spin_lock_irq(&ts->lock);
542
David Brownell2e5a7bd2006-01-08 13:34:25 -0800543 spi->dev.power.power_state = PMSG_ON;
Imre Deak7de90a82006-04-11 23:43:55 -0400544 ads7846_enable(ts);
545
546 spin_unlock_irq(&ts->lock);
547
David Brownellffa458c2006-01-08 13:34:21 -0800548 return 0;
549}
550
David Brownell2e5a7bd2006-01-08 13:34:25 -0800551static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800552{
David Brownellffa458c2006-01-08 13:34:21 -0800553 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500554 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800555 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400556 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800557 struct spi_transfer *x;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500558 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800559
560 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800561 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800562 return -ENODEV;
563 }
564
565 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800566 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800567 return -ENODEV;
568 }
569
570 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500571 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800572 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500573 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800574 return -EINVAL;
575 }
576
Imre Deakc9e617a2006-04-11 23:44:05 -0400577 if (pdata->get_pendown_state == NULL) {
578 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
579 return -EINVAL;
580 }
581
David Brownellffa458c2006-01-08 13:34:21 -0800582 /* We'd set the wordsize to 12 bits ... except that some controllers
583 * will then treat the 8 bit command words as 12 bits (and drop the
584 * four MSBs of the 12 bit result). Result: inputs must be shifted
585 * to discard the four garbage LSBs.
586 */
587
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500588 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
589 input_dev = input_allocate_device();
590 if (!ts || !input_dev) {
591 err = -ENOMEM;
592 goto err_free_mem;
593 }
David Brownellffa458c2006-01-08 13:34:21 -0800594
David Brownell2e5a7bd2006-01-08 13:34:25 -0800595 dev_set_drvdata(&spi->dev, ts);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500596 spi->dev.power.power_state = PMSG_ON;
David Brownellffa458c2006-01-08 13:34:21 -0800597
598 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500599 ts->input = input_dev;
David Brownellffa458c2006-01-08 13:34:21 -0800600
601 init_timer(&ts->timer);
602 ts->timer.data = (unsigned long) ts;
603 ts->timer.function = ads7846_timer;
604
Imre Deak7de90a82006-04-11 23:43:55 -0400605 spin_lock_init(&ts->lock);
606
David Brownellffa458c2006-01-08 13:34:21 -0800607 ts->model = pdata->model ? : 7846;
608 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
609 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deak0b7018a2006-04-11 23:42:03 -0400610 ts->debounce_max = pdata->debounce_max ? : 1;
611 ts->debounce_tol = pdata->debounce_tol ? : 10;
Imre Deakc9e617a2006-04-11 23:44:05 -0400612 ts->get_pendown_state = pdata->get_pendown_state;
David Brownellffa458c2006-01-08 13:34:21 -0800613
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500614 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800615
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500616 input_dev->name = "ADS784x Touchscreen";
617 input_dev->phys = ts->phys;
618 input_dev->cdev.dev = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800619
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500620 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
621 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
622 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800623 pdata->x_min ? : 0,
624 pdata->x_max ? : MAX_12BIT,
625 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500626 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800627 pdata->y_min ? : 0,
628 pdata->y_max ? : MAX_12BIT,
629 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500630 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800631 pdata->pressure_min, pdata->pressure_max, 0, 0);
632
David Brownellffa458c2006-01-08 13:34:21 -0800633 /* set up the transfers to read touchscreen state; this assumes we
634 * use formula #2 for pressure, not #3.
635 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400636 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800637 x = ts->xfer;
638
Imre Deak0b7018a2006-04-11 23:42:03 -0400639 spi_message_init(m);
640
David Brownellffa458c2006-01-08 13:34:21 -0800641 /* y- still on; turn on only y+ (and ADC) */
David Brownelld93f70b2006-02-15 00:49:35 -0500642 ts->read_y = READ_Y;
643 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800644 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400645 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500646
David Brownellffa458c2006-01-08 13:34:21 -0800647 x++;
648 x->rx_buf = &ts->tc.y;
649 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400650 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800651
Imre Deak0b7018a2006-04-11 23:42:03 -0400652 m->complete = ads7846_debounce;
653 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500654
Imre Deak0b7018a2006-04-11 23:42:03 -0400655 m++;
656 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800657
658 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500659 x++;
660 ts->read_x = READ_X;
661 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800662 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400663 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500664
David Brownellffa458c2006-01-08 13:34:21 -0800665 x++;
666 x->rx_buf = &ts->tc.x;
667 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400668 spi_message_add_tail(x, m);
669
670 m->complete = ads7846_debounce;
671 m->context = ts;
672
673 /* turn y+ off, x- on; we'll use formula #2 */
674 if (ts->model == 7846) {
675 m++;
676 spi_message_init(m);
677
678 x++;
679 ts->read_z1 = READ_Z1;
680 x->tx_buf = &ts->read_z1;
681 x->len = 1;
682 spi_message_add_tail(x, m);
683
684 x++;
685 x->rx_buf = &ts->tc.z1;
686 x->len = 2;
687 spi_message_add_tail(x, m);
688
689 m->complete = ads7846_debounce;
690 m->context = ts;
691
692 m++;
693 spi_message_init(m);
694
695 x++;
696 ts->read_z2 = READ_Z2;
697 x->tx_buf = &ts->read_z2;
698 x->len = 1;
699 spi_message_add_tail(x, m);
700
701 x++;
702 x->rx_buf = &ts->tc.z2;
703 x->len = 2;
704 spi_message_add_tail(x, m);
705
706 m->complete = ads7846_debounce;
707 m->context = ts;
708 }
Imre Deak53a0ef892006-04-11 23:41:49 -0400709
710 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -0400711 m++;
712 spi_message_init(m);
713
Imre Deak53a0ef892006-04-11 23:41:49 -0400714 x++;
715 ts->pwrdown = PWRDOWN;
716 x->tx_buf = &ts->pwrdown;
717 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400718 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -0400719
720 x++;
721 x->rx_buf = &ts->dummy;
722 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500723 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -0400724 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800725
Imre Deak0b7018a2006-04-11 23:42:03 -0400726 m->complete = ads7846_rx;
727 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -0800728
Russell Kingf43aaba2006-01-19 12:26:57 +0000729 if (request_irq(spi->irq, ads7846_irq,
730 SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
731 spi->dev.bus_id, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800732 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500733 err = -EBUSY;
734 goto err_free_mem;
David Brownellffa458c2006-01-08 13:34:21 -0800735 }
David Brownellffa458c2006-01-08 13:34:21 -0800736
David Brownell2e5a7bd2006-01-08 13:34:25 -0800737 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -0800738
739 /* take a first sample, leaving nPENIRQ active; avoid
740 * the touchscreen, in case it's not connected.
741 */
David Brownell2e5a7bd2006-01-08 13:34:25 -0800742 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -0800743 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
744
745 /* ads7843/7845 don't have temperature sensors, and
746 * use the other sensors a bit differently too
747 */
748 if (ts->model == 7846) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800749 device_create_file(&spi->dev, &dev_attr_temp0);
750 device_create_file(&spi->dev, &dev_attr_temp1);
David Brownellffa458c2006-01-08 13:34:21 -0800751 }
752 if (ts->model != 7845)
David Brownell2e5a7bd2006-01-08 13:34:25 -0800753 device_create_file(&spi->dev, &dev_attr_vbatt);
754 device_create_file(&spi->dev, &dev_attr_vaux);
David Brownellffa458c2006-01-08 13:34:21 -0800755
Imre Deak438f2a72006-04-11 23:41:32 -0400756 device_create_file(&spi->dev, &dev_attr_pen_down);
757
Imre Deak7de90a82006-04-11 23:43:55 -0400758 device_create_file(&spi->dev, &dev_attr_disable);
759
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500760 err = input_register_device(input_dev);
761 if (err)
Imre Deak7de90a82006-04-11 23:43:55 -0400762 goto err_remove_attr;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500763
David Brownellffa458c2006-01-08 13:34:21 -0800764 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500765
Imre Deak7de90a82006-04-11 23:43:55 -0400766 err_remove_attr:
767 device_remove_file(&spi->dev, &dev_attr_disable);
768 device_remove_file(&spi->dev, &dev_attr_pen_down);
769 if (ts->model == 7846) {
770 device_remove_file(&spi->dev, &dev_attr_temp1);
771 device_remove_file(&spi->dev, &dev_attr_temp0);
772 }
773 if (ts->model != 7845)
774 device_remove_file(&spi->dev, &dev_attr_vbatt);
775 device_remove_file(&spi->dev, &dev_attr_vaux);
776
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500777 free_irq(spi->irq, ts);
778 err_free_mem:
779 input_free_device(input_dev);
780 kfree(ts);
781 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800782}
783
David Brownell2e5a7bd2006-01-08 13:34:25 -0800784static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800785{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800786 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800787
Imre Deak7de90a82006-04-11 23:43:55 -0400788 input_unregister_device(ts->input);
789
David Brownell2e5a7bd2006-01-08 13:34:25 -0800790 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -0800791
Imre Deak7de90a82006-04-11 23:43:55 -0400792 device_remove_file(&spi->dev, &dev_attr_disable);
Imre Deak438f2a72006-04-11 23:41:32 -0400793 device_remove_file(&spi->dev, &dev_attr_pen_down);
David Brownellffa458c2006-01-08 13:34:21 -0800794 if (ts->model == 7846) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800795 device_remove_file(&spi->dev, &dev_attr_temp1);
Imre Deak7de90a82006-04-11 23:43:55 -0400796 device_remove_file(&spi->dev, &dev_attr_temp0);
David Brownellffa458c2006-01-08 13:34:21 -0800797 }
798 if (ts->model != 7845)
David Brownell2e5a7bd2006-01-08 13:34:25 -0800799 device_remove_file(&spi->dev, &dev_attr_vbatt);
800 device_remove_file(&spi->dev, &dev_attr_vaux);
David Brownellffa458c2006-01-08 13:34:21 -0800801
Imre Deak7de90a82006-04-11 23:43:55 -0400802 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -0400803 /* suspend left the IRQ disabled */
804 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -0400805
David Brownellffa458c2006-01-08 13:34:21 -0800806 kfree(ts);
807
David Brownell2e5a7bd2006-01-08 13:34:25 -0800808 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -0800809 return 0;
810}
811
David Brownell2e5a7bd2006-01-08 13:34:25 -0800812static struct spi_driver ads7846_driver = {
813 .driver = {
814 .name = "ads7846",
815 .bus = &spi_bus_type,
816 .owner = THIS_MODULE,
817 },
David Brownellffa458c2006-01-08 13:34:21 -0800818 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -0800819 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -0800820 .suspend = ads7846_suspend,
821 .resume = ads7846_resume,
822};
823
824static int __init ads7846_init(void)
825{
826 /* grr, board-specific init should stay out of drivers!! */
827
828#ifdef CONFIG_ARCH_OMAP
829 if (machine_is_omap_osk()) {
830 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
831 omap_request_gpio(4);
832 omap_set_gpio_direction(4, 1);
833 omap_request_gpio(6);
834 omap_set_gpio_direction(6, 1);
835 }
836 // also TI 1510 Innovator, bitbanging through FPGA
837 // also Nokia 770
838 // also Palm Tungsten T2
839#endif
840
841 // PXA:
842 // also Dell Axim X50
843 // also HP iPaq H191x/H192x/H415x/H435x
David Brownell2e5a7bd2006-01-08 13:34:25 -0800844 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
David Brownellffa458c2006-01-08 13:34:21 -0800845 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
846
David Brownell2e5a7bd2006-01-08 13:34:25 -0800847 // Atmel at91sam9261-EK uses ads7843
848
David Brownellffa458c2006-01-08 13:34:21 -0800849 // also various AMD Au1x00 devel boards
850
David Brownell2e5a7bd2006-01-08 13:34:25 -0800851 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800852}
853module_init(ads7846_init);
854
855static void __exit ads7846_exit(void)
856{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800857 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800858
859#ifdef CONFIG_ARCH_OMAP
860 if (machine_is_omap_osk()) {
861 omap_free_gpio(4);
862 omap_free_gpio(6);
863 }
864#endif
865
866}
867module_exit(ads7846_exit);
868
869MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
870MODULE_LICENSE("GPL");