blob: fec3b9b22309fe5e8d74ebe204f707805c54be56 [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;
David Brownellffa458c2006-01-08 13:34:21 -0800105};
106
107/* leave chip selected when we're done, for quicker re-select? */
108#if 0
109#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
110#else
111#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
112#endif
113
114/*--------------------------------------------------------------------------*/
115
116/* The ADS7846 has touchscreen and other sensors.
117 * Earlier ads784x chips are somewhat compatible.
118 */
119#define ADS_START (1 << 7)
120#define ADS_A2A1A0_d_y (1 << 4) /* differential */
121#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
122#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
123#define ADS_A2A1A0_d_x (5 << 4) /* differential */
124#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
125#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
126#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
127#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
128#define ADS_8_BIT (1 << 3)
129#define ADS_12_BIT (0 << 3)
130#define ADS_SER (1 << 2) /* non-differential */
131#define ADS_DFR (0 << 2) /* differential */
132#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
133#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
134#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
135#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
136
137#define MAX_12BIT ((1<<12)-1)
138
139/* leave ADC powered up (disables penirq) between differential samples */
140#define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
141 | ADS_12_BIT | ADS_DFR)
142
David Brownelld93f70b2006-02-15 00:49:35 -0500143#define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON)
144#define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
145#define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
Imre Deak53a0ef892006-04-11 23:41:49 -0400146
147#define READ_X (READ_12BIT_DFR(x) | ADS_PD10_ADC_ON)
148#define PWRDOWN (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800149
150/* single-ended samples need to first power up reference voltage;
151 * we leave both ADC and VREF powered
152 */
153#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
154 | ADS_12_BIT | ADS_SER)
155
David Brownelld93f70b2006-02-15 00:49:35 -0500156#define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
157#define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
David Brownellffa458c2006-01-08 13:34:21 -0800158
159/*--------------------------------------------------------------------------*/
160
161/*
162 * Non-touchscreen sensors only use single-ended conversions.
163 */
164
165struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500166 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800167 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500168 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800169 u16 scratch;
170 __be16 sample;
171 struct spi_message msg;
172 struct spi_transfer xfer[6];
173};
174
Imre Deak7de90a82006-04-11 23:43:55 -0400175static void ads7846_enable(struct ads7846 *ts);
176static void ads7846_disable(struct ads7846 *ts);
177
David Brownellffa458c2006-01-08 13:34:21 -0800178static int ads7846_read12_ser(struct device *dev, unsigned command)
179{
180 struct spi_device *spi = to_spi_device(dev);
181 struct ads7846 *ts = dev_get_drvdata(dev);
182 struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL);
183 int status;
184 int sample;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500185 int i;
David Brownellffa458c2006-01-08 13:34:21 -0800186
187 if (!req)
188 return -ENOMEM;
189
Imre Deak0b7018a2006-04-11 23:42:03 -0400190 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800191
David Brownellffa458c2006-01-08 13:34:21 -0800192 /* activate reference, so it has time to settle; */
David Brownelld93f70b2006-02-15 00:49:35 -0500193 req->ref_on = REF_ON;
194 req->xfer[0].tx_buf = &req->ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800195 req->xfer[0].len = 1;
196 req->xfer[1].rx_buf = &req->scratch;
197 req->xfer[1].len = 2;
198
199 /*
200 * for external VREF, 0 usec (and assume it's always on);
201 * for 1uF, use 800 usec;
202 * no cap, 100 usec.
203 */
204 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
205
206 /* take sample */
207 req->command = (u8) command;
208 req->xfer[2].tx_buf = &req->command;
209 req->xfer[2].len = 1;
210 req->xfer[3].rx_buf = &req->sample;
211 req->xfer[3].len = 2;
212
213 /* REVISIT: take a few more samples, and compare ... */
214
215 /* turn off reference */
David Brownelld93f70b2006-02-15 00:49:35 -0500216 req->ref_off = REF_OFF;
217 req->xfer[4].tx_buf = &req->ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800218 req->xfer[4].len = 1;
219 req->xfer[5].rx_buf = &req->scratch;
220 req->xfer[5].len = 2;
221
222 CS_CHANGE(req->xfer[5]);
223
224 /* group all the transfers together, so we can't interfere with
225 * reading touchscreen state; disable penirq while sampling
226 */
Vitaly Wool8275c642006-01-08 13:34:28 -0800227 for (i = 0; i < 6; i++)
228 spi_message_add_tail(&req->xfer[i], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800229
230 disable_irq(spi->irq);
231 status = spi_sync(spi, &req->msg);
232 enable_irq(spi->irq);
233
234 if (req->msg.status)
235 status = req->msg.status;
236 sample = be16_to_cpu(req->sample);
237 sample = sample >> 4;
238 kfree(req);
239
240 return status ? status : sample;
241}
242
243#define SHOW(name) static ssize_t \
244name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
245{ \
246 ssize_t v = ads7846_read12_ser(dev, \
247 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
248 if (v < 0) \
249 return v; \
250 return sprintf(buf, "%u\n", (unsigned) v); \
251} \
252static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
253
254SHOW(temp0)
255SHOW(temp1)
256SHOW(vaux)
257SHOW(vbatt)
258
Imre Deak438f2a72006-04-11 23:41:32 -0400259static int is_pen_down(struct device *dev)
260{
261 struct ads7846 *ts = dev_get_drvdata(dev);
262
263 return ts->pendown;
264}
265
266static ssize_t ads7846_pen_down_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
268{
269 return sprintf(buf, "%u\n", is_pen_down(dev));
270}
271
272static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
273
Imre Deak7de90a82006-04-11 23:43:55 -0400274static ssize_t ads7846_disable_show(struct device *dev,
275 struct device_attribute *attr, char *buf)
276{
277 struct ads7846 *ts = dev_get_drvdata(dev);
278
279 return sprintf(buf, "%u\n", ts->disabled);
280}
281
282static ssize_t ads7846_disable_store(struct device *dev,
283 struct device_attribute *attr,
284 const char *buf, size_t count)
285{
286 struct ads7846 *ts = dev_get_drvdata(dev);
287 char *endp;
288 int i;
289
290 i = simple_strtoul(buf, &endp, 10);
291 spin_lock_irq(&ts->lock);
292
293 if (i)
294 ads7846_disable(ts);
295 else
296 ads7846_enable(ts);
297
298 spin_unlock_irq(&ts->lock);
299
300 return count;
301}
302
303static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
304
David Brownellffa458c2006-01-08 13:34:21 -0800305/*--------------------------------------------------------------------------*/
306
307/*
308 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
309 * to retrieve touchscreen status.
310 *
311 * The SPI transfer completion callback does the real work. It reports
312 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
313 */
314
315static void ads7846_rx(void *ads)
316{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500317 struct ads7846 *ts = ads;
318 struct input_dev *input_dev = ts->input;
319 unsigned Rt;
320 unsigned sync = 0;
321 u16 x, y, z1, z2;
322 unsigned long flags;
David Brownellffa458c2006-01-08 13:34:21 -0800323
324 /* adjust: 12 bit samples (left aligned), built from
325 * two 8 bit values writen msb-first.
326 */
327 x = be16_to_cpu(ts->tc.x) >> 4;
328 y = be16_to_cpu(ts->tc.y) >> 4;
329 z1 = be16_to_cpu(ts->tc.z1) >> 4;
330 z2 = be16_to_cpu(ts->tc.z2) >> 4;
331
332 /* range filtering */
333 if (x == MAX_12BIT)
334 x = 0;
335
336 if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
337 /* compute touch pressure resistance using equation #2 */
338 Rt = z2;
339 Rt -= z1;
340 Rt *= x;
341 Rt *= ts->x_plate_ohms;
342 Rt /= z1;
343 Rt = (Rt + 2047) >> 12;
344 } else
345 Rt = 0;
346
347 /* NOTE: "pendown" is inferred from pressure; we don't rely on
348 * being able to check nPENIRQ status, or "friendly" trigger modes
349 * (both-edges is much better than just-falling or low-level).
350 *
351 * REVISIT: some boards may require reading nPENIRQ; it's
352 * needed on 7843. and 7845 reads pressure differently...
353 *
354 * REVISIT: the touchscreen might not be connected; this code
355 * won't notice that, even if nPENIRQ never fires ...
356 */
357 if (!ts->pendown && Rt != 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500358 input_report_key(input_dev, BTN_TOUCH, 1);
David Brownellffa458c2006-01-08 13:34:21 -0800359 sync = 1;
360 } else if (ts->pendown && Rt == 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500361 input_report_key(input_dev, BTN_TOUCH, 0);
David Brownellffa458c2006-01-08 13:34:21 -0800362 sync = 1;
363 }
364
365 if (Rt) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500366 input_report_abs(input_dev, ABS_X, x);
367 input_report_abs(input_dev, ABS_Y, y);
368 input_report_abs(input_dev, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800369 sync = 1;
370 }
371 if (sync)
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500372 input_sync(input_dev);
David Brownellffa458c2006-01-08 13:34:21 -0800373
374#ifdef VERBOSE
375 if (Rt || ts->pendown)
376 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
377 x, y, Rt, Rt ? "" : " UP");
378#endif
379
380 /* don't retrigger while we're suspended */
381 spin_lock_irqsave(&ts->lock, flags);
382
383 ts->pendown = (Rt != 0);
384 ts->pending = 0;
385
386 if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
387 if (ts->pendown)
388 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
389 else if (ts->irq_disabled) {
390 ts->irq_disabled = 0;
391 enable_irq(ts->spi->irq);
392 }
393 }
394
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 Deak0b7018a2006-04-11 23:42:03 -0400434 ts->msg_idx = 0;
435 status = spi_async(ts->spi, &ts->msg[0]);
436 if (status)
437 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
David Brownellffa458c2006-01-08 13:34:21 -0800438}
439
440static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
441{
Imre Deak0b7018a2006-04-11 23:42:03 -0400442 struct ads7846 *ts = handle;
443 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400444
445 spin_lock_irqsave(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400446 if (likely(!ts->irq_disabled && !ts->disabled)) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400447 if (!ts->irq_disabled) {
448 /* REVISIT irq logic for many ARM chips has cloned a
449 * bug wherein disabling an irq in its handler won't
450 * work;(it's disabled lazily, and too late to work.
451 * until all their irq logic is fixed, we must shadow
452 * that state here.
453 */
454 ts->irq_disabled = 1;
455
456 disable_irq(ts->spi->irq);
457 }
458 if (!ts->pending) {
459 ts->pending = 1;
460 mod_timer(&ts->timer, jiffies);
461 }
462 }
463 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400464
465 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800466}
467
468/*--------------------------------------------------------------------------*/
469
Imre Deak7de90a82006-04-11 23:43:55 -0400470/* Must be called with ts->lock held */
471static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800472{
Imre Deak7de90a82006-04-11 23:43:55 -0400473 if (ts->disabled)
474 return;
David Brownellffa458c2006-01-08 13:34:21 -0800475
476 /* are we waiting for IRQ, or polling? */
477 if (!ts->pendown) {
478 if (!ts->irq_disabled) {
479 ts->irq_disabled = 1;
480 disable_irq(ts->spi->irq);
481 }
482 } else {
483 /* polling; force a final SPI completion;
484 * that will clean things up neatly
485 */
486 if (!ts->pending)
487 mod_timer(&ts->timer, jiffies);
488
489 while (ts->pendown || ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400490 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400491 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400492 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800493 }
494 }
495
496 /* we know the chip's in lowpower mode since we always
497 * leave it that way after every request
498 */
499
Imre Deak7de90a82006-04-11 23:43:55 -0400500 ts->disabled = 1;
501}
502
503/* Must be called with ts->lock held */
504static void ads7846_enable(struct ads7846 *ts)
505{
506 if (!ts->disabled)
507 return;
508
509 ts->disabled = 0;
510 ts->irq_disabled = 0;
511 enable_irq(ts->spi->irq);
512}
513
514static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
515{
516 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
517
518 spin_lock_irq(&ts->lock);
519
520 spi->dev.power.power_state = message;
521 ads7846_disable(ts);
522
523 spin_unlock_irq(&ts->lock);
524
David Brownellffa458c2006-01-08 13:34:21 -0800525 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400526
David Brownellffa458c2006-01-08 13:34:21 -0800527}
528
David Brownell2e5a7bd2006-01-08 13:34:25 -0800529static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800530{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800531 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800532
Imre Deak7de90a82006-04-11 23:43:55 -0400533 spin_lock_irq(&ts->lock);
534
David Brownell2e5a7bd2006-01-08 13:34:25 -0800535 spi->dev.power.power_state = PMSG_ON;
Imre Deak7de90a82006-04-11 23:43:55 -0400536 ads7846_enable(ts);
537
538 spin_unlock_irq(&ts->lock);
539
David Brownellffa458c2006-01-08 13:34:21 -0800540 return 0;
541}
542
David Brownell2e5a7bd2006-01-08 13:34:25 -0800543static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800544{
David Brownellffa458c2006-01-08 13:34:21 -0800545 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500546 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800547 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400548 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800549 struct spi_transfer *x;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500550 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800551
552 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800553 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800554 return -ENODEV;
555 }
556
557 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800558 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800559 return -ENODEV;
560 }
561
562 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500563 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800564 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500565 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800566 return -EINVAL;
567 }
568
569 /* We'd set the wordsize to 12 bits ... except that some controllers
570 * will then treat the 8 bit command words as 12 bits (and drop the
571 * four MSBs of the 12 bit result). Result: inputs must be shifted
572 * to discard the four garbage LSBs.
573 */
574
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500575 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
576 input_dev = input_allocate_device();
577 if (!ts || !input_dev) {
578 err = -ENOMEM;
579 goto err_free_mem;
580 }
David Brownellffa458c2006-01-08 13:34:21 -0800581
David Brownell2e5a7bd2006-01-08 13:34:25 -0800582 dev_set_drvdata(&spi->dev, ts);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500583 spi->dev.power.power_state = PMSG_ON;
David Brownellffa458c2006-01-08 13:34:21 -0800584
585 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500586 ts->input = input_dev;
David Brownellffa458c2006-01-08 13:34:21 -0800587
588 init_timer(&ts->timer);
589 ts->timer.data = (unsigned long) ts;
590 ts->timer.function = ads7846_timer;
591
Imre Deak7de90a82006-04-11 23:43:55 -0400592 spin_lock_init(&ts->lock);
593
David Brownellffa458c2006-01-08 13:34:21 -0800594 ts->model = pdata->model ? : 7846;
595 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
596 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deak0b7018a2006-04-11 23:42:03 -0400597 ts->debounce_max = pdata->debounce_max ? : 1;
598 ts->debounce_tol = pdata->debounce_tol ? : 10;
David Brownellffa458c2006-01-08 13:34:21 -0800599
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500600 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800601
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500602 input_dev->name = "ADS784x Touchscreen";
603 input_dev->phys = ts->phys;
604 input_dev->cdev.dev = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800605
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500606 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
607 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
608 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800609 pdata->x_min ? : 0,
610 pdata->x_max ? : MAX_12BIT,
611 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500612 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800613 pdata->y_min ? : 0,
614 pdata->y_max ? : MAX_12BIT,
615 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500616 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800617 pdata->pressure_min, pdata->pressure_max, 0, 0);
618
David Brownellffa458c2006-01-08 13:34:21 -0800619 /* set up the transfers to read touchscreen state; this assumes we
620 * use formula #2 for pressure, not #3.
621 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400622 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800623 x = ts->xfer;
624
Imre Deak0b7018a2006-04-11 23:42:03 -0400625 spi_message_init(m);
626
David Brownellffa458c2006-01-08 13:34:21 -0800627 /* y- still on; turn on only y+ (and ADC) */
David Brownelld93f70b2006-02-15 00:49:35 -0500628 ts->read_y = READ_Y;
629 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800630 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400631 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500632
David Brownellffa458c2006-01-08 13:34:21 -0800633 x++;
634 x->rx_buf = &ts->tc.y;
635 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400636 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800637
Imre Deak0b7018a2006-04-11 23:42:03 -0400638 m->complete = ads7846_debounce;
639 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500640
Imre Deak0b7018a2006-04-11 23:42:03 -0400641 m++;
642 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800643
644 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500645 x++;
646 ts->read_x = READ_X;
647 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800648 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400649 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500650
David Brownellffa458c2006-01-08 13:34:21 -0800651 x++;
652 x->rx_buf = &ts->tc.x;
653 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400654 spi_message_add_tail(x, m);
655
656 m->complete = ads7846_debounce;
657 m->context = ts;
658
659 /* turn y+ off, x- on; we'll use formula #2 */
660 if (ts->model == 7846) {
661 m++;
662 spi_message_init(m);
663
664 x++;
665 ts->read_z1 = READ_Z1;
666 x->tx_buf = &ts->read_z1;
667 x->len = 1;
668 spi_message_add_tail(x, m);
669
670 x++;
671 x->rx_buf = &ts->tc.z1;
672 x->len = 2;
673 spi_message_add_tail(x, m);
674
675 m->complete = ads7846_debounce;
676 m->context = ts;
677
678 m++;
679 spi_message_init(m);
680
681 x++;
682 ts->read_z2 = READ_Z2;
683 x->tx_buf = &ts->read_z2;
684 x->len = 1;
685 spi_message_add_tail(x, m);
686
687 x++;
688 x->rx_buf = &ts->tc.z2;
689 x->len = 2;
690 spi_message_add_tail(x, m);
691
692 m->complete = ads7846_debounce;
693 m->context = ts;
694 }
Imre Deak53a0ef892006-04-11 23:41:49 -0400695
696 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -0400697 m++;
698 spi_message_init(m);
699
Imre Deak53a0ef892006-04-11 23:41:49 -0400700 x++;
701 ts->pwrdown = PWRDOWN;
702 x->tx_buf = &ts->pwrdown;
703 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400704 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -0400705
706 x++;
707 x->rx_buf = &ts->dummy;
708 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500709 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -0400710 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800711
Imre Deak0b7018a2006-04-11 23:42:03 -0400712 m->complete = ads7846_rx;
713 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -0800714
Russell Kingf43aaba2006-01-19 12:26:57 +0000715 if (request_irq(spi->irq, ads7846_irq,
716 SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
717 spi->dev.bus_id, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800718 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500719 err = -EBUSY;
720 goto err_free_mem;
David Brownellffa458c2006-01-08 13:34:21 -0800721 }
David Brownellffa458c2006-01-08 13:34:21 -0800722
David Brownell2e5a7bd2006-01-08 13:34:25 -0800723 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -0800724
725 /* take a first sample, leaving nPENIRQ active; avoid
726 * the touchscreen, in case it's not connected.
727 */
David Brownell2e5a7bd2006-01-08 13:34:25 -0800728 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -0800729 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
730
731 /* ads7843/7845 don't have temperature sensors, and
732 * use the other sensors a bit differently too
733 */
734 if (ts->model == 7846) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800735 device_create_file(&spi->dev, &dev_attr_temp0);
736 device_create_file(&spi->dev, &dev_attr_temp1);
David Brownellffa458c2006-01-08 13:34:21 -0800737 }
738 if (ts->model != 7845)
David Brownell2e5a7bd2006-01-08 13:34:25 -0800739 device_create_file(&spi->dev, &dev_attr_vbatt);
740 device_create_file(&spi->dev, &dev_attr_vaux);
David Brownellffa458c2006-01-08 13:34:21 -0800741
Imre Deak438f2a72006-04-11 23:41:32 -0400742 device_create_file(&spi->dev, &dev_attr_pen_down);
743
Imre Deak7de90a82006-04-11 23:43:55 -0400744 device_create_file(&spi->dev, &dev_attr_disable);
745
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500746 err = input_register_device(input_dev);
747 if (err)
Imre Deak7de90a82006-04-11 23:43:55 -0400748 goto err_remove_attr;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500749
David Brownellffa458c2006-01-08 13:34:21 -0800750 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500751
Imre Deak7de90a82006-04-11 23:43:55 -0400752 err_remove_attr:
753 device_remove_file(&spi->dev, &dev_attr_disable);
754 device_remove_file(&spi->dev, &dev_attr_pen_down);
755 if (ts->model == 7846) {
756 device_remove_file(&spi->dev, &dev_attr_temp1);
757 device_remove_file(&spi->dev, &dev_attr_temp0);
758 }
759 if (ts->model != 7845)
760 device_remove_file(&spi->dev, &dev_attr_vbatt);
761 device_remove_file(&spi->dev, &dev_attr_vaux);
762
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500763 free_irq(spi->irq, ts);
764 err_free_mem:
765 input_free_device(input_dev);
766 kfree(ts);
767 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800768}
769
David Brownell2e5a7bd2006-01-08 13:34:25 -0800770static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800771{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800772 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800773
Imre Deak7de90a82006-04-11 23:43:55 -0400774 input_unregister_device(ts->input);
775
David Brownell2e5a7bd2006-01-08 13:34:25 -0800776 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -0800777
Imre Deak7de90a82006-04-11 23:43:55 -0400778 device_remove_file(&spi->dev, &dev_attr_disable);
Imre Deak438f2a72006-04-11 23:41:32 -0400779 device_remove_file(&spi->dev, &dev_attr_pen_down);
David Brownellffa458c2006-01-08 13:34:21 -0800780 if (ts->model == 7846) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800781 device_remove_file(&spi->dev, &dev_attr_temp1);
Imre Deak7de90a82006-04-11 23:43:55 -0400782 device_remove_file(&spi->dev, &dev_attr_temp0);
David Brownellffa458c2006-01-08 13:34:21 -0800783 }
784 if (ts->model != 7845)
David Brownell2e5a7bd2006-01-08 13:34:25 -0800785 device_remove_file(&spi->dev, &dev_attr_vbatt);
786 device_remove_file(&spi->dev, &dev_attr_vaux);
David Brownellffa458c2006-01-08 13:34:21 -0800787
Imre Deak7de90a82006-04-11 23:43:55 -0400788 free_irq(ts->spi->irq, ts);
789 if (ts->irq_disabled)
790 enable_irq(ts->spi->irq);
791
David Brownellffa458c2006-01-08 13:34:21 -0800792 kfree(ts);
793
David Brownell2e5a7bd2006-01-08 13:34:25 -0800794 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -0800795 return 0;
796}
797
David Brownell2e5a7bd2006-01-08 13:34:25 -0800798static struct spi_driver ads7846_driver = {
799 .driver = {
800 .name = "ads7846",
801 .bus = &spi_bus_type,
802 .owner = THIS_MODULE,
803 },
David Brownellffa458c2006-01-08 13:34:21 -0800804 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -0800805 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -0800806 .suspend = ads7846_suspend,
807 .resume = ads7846_resume,
808};
809
810static int __init ads7846_init(void)
811{
812 /* grr, board-specific init should stay out of drivers!! */
813
814#ifdef CONFIG_ARCH_OMAP
815 if (machine_is_omap_osk()) {
816 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
817 omap_request_gpio(4);
818 omap_set_gpio_direction(4, 1);
819 omap_request_gpio(6);
820 omap_set_gpio_direction(6, 1);
821 }
822 // also TI 1510 Innovator, bitbanging through FPGA
823 // also Nokia 770
824 // also Palm Tungsten T2
825#endif
826
827 // PXA:
828 // also Dell Axim X50
829 // also HP iPaq H191x/H192x/H415x/H435x
David Brownell2e5a7bd2006-01-08 13:34:25 -0800830 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
David Brownellffa458c2006-01-08 13:34:21 -0800831 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
832
David Brownell2e5a7bd2006-01-08 13:34:25 -0800833 // Atmel at91sam9261-EK uses ads7843
834
David Brownellffa458c2006-01-08 13:34:21 -0800835 // also various AMD Au1x00 devel boards
836
David Brownell2e5a7bd2006-01-08 13:34:25 -0800837 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800838}
839module_init(ads7846_init);
840
841static void __exit ads7846_exit(void)
842{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800843 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800844
845#ifdef CONFIG_ARCH_OMAP
846 if (machine_is_omap_osk()) {
847 omap_free_gpio(4);
848 omap_free_gpio(6);
849 }
850#endif
851
852}
853module_exit(ads7846_exit);
854
855MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
856MODULE_LICENSE("GPL");