Input: ads7846 - select correct SPI mode
[deliverable/linux.git] / drivers / input / touchscreen / ads7846.c
CommitLineData
ffa458c1
DB
1/*
2 * ADS7846 based touchscreen and sensor driver
3 *
4 * Copyright (c) 2005 David Brownell
7de90a8c
ID
5 * Copyright (c) 2006 Nokia Corporation
6 * Various changes: Imre Deak <imre.deak@nokia.com>
ffa458c1
DB
7 *
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>
3ac8bf07 28#include <asm/irq.h>
ffa458c1
DB
29
30#ifdef CONFIG_ARM
31#include <asm/mach-types.h>
32#ifdef CONFIG_ARCH_OMAP
33#include <asm/arch/gpio.h>
34#endif
ffa458c1
DB
35#endif
36
37
38/*
9084533e
DB
39 * This code has been heavily tested on a Nokia 770, and lightly
40 * tested on other ads7846 devices (OSK/Mistral, Lubbock).
ffa458c1
DB
41 * Support for ads7843 and ads7845 has only been stubbed in.
42 *
7de90a8c
ID
43 * IRQ handling needs a workaround because of a shortcoming in handling
44 * edge triggered IRQs on some platforms like the OMAP1/2. These
45 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
46 * have to maintain our own SW IRQ disabled status. This should be
47 * removed as soon as the affected platform's IRQ handling is fixed.
48 *
ffa458c1
DB
49 * app note sbaa036 talks in more detail about accurate sampling...
50 * that ought to help in situations like LCDs inducing noise (which
51 * can also be helped by using synch signals) and more generally.
7de90a8c
ID
52 * This driver tries to utilize the measures described in the app
53 * note. The strength of filtering can be set in the board-* specific
54 * files.
ffa458c1
DB
55 */
56
1936d590
ID
57#define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
58#define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
ffa458c1 59
d93f70b2
DB
60/* this driver doesn't aim at the peak continuous sample rate */
61#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
62
ffa458c1
DB
63struct ts_event {
64 /* For portability, we can't read 12 bit values using SPI (which
65 * would make the controller deliver them as native byteorder u16
d93f70b2 66 * with msbs zeroed). Instead, we read them as two 8-bit values,
da970e69 67 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
ffa458c1 68 */
da970e69
ID
69 u16 x;
70 u16 y;
71 u16 z1, z2;
d5b415c9 72 int ignore;
ffa458c1
DB
73};
74
75struct ads7846 {
a90f7e98 76 struct input_dev *input;
ffa458c1
DB
77 char phys[32];
78
79 struct spi_device *spi;
8dd51650 80 struct attribute_group *attr_group;
ffa458c1
DB
81 u16 model;
82 u16 vref_delay_usecs;
83 u16 x_plate_ohms;
d5b415c9 84 u16 pressure_max;
ffa458c1 85
53a0ef89
ID
86 u8 read_x, read_y, read_z1, read_z2, pwrdown;
87 u16 dummy; /* for the pwrdown read */
ffa458c1
DB
88 struct ts_event tc;
89
53a0ef89 90 struct spi_transfer xfer[10];
0b7018aa 91 struct spi_message msg[5];
d5b415c9 92 struct spi_message *last_msg;
0b7018aa
ID
93 int msg_idx;
94 int read_cnt;
d5b415c9 95 int read_rep;
0b7018aa
ID
96 int last_read;
97
98 u16 debounce_max;
99 u16 debounce_tol;
d5b415c9 100 u16 debounce_rep;
ffa458c1
DB
101
102 spinlock_t lock;
1936d590 103 struct hrtimer timer;
ffa458c1
DB
104 unsigned pendown:1; /* P: lock */
105 unsigned pending:1; /* P: lock */
106// FIXME remove "irq_disabled"
107 unsigned irq_disabled:1; /* P: lock */
7de90a8c 108 unsigned disabled:1;
c9e617a5 109
da970e69
ID
110 int (*filter)(void *data, int data_idx, int *val);
111 void *filter_data;
112 void (*filter_cleanup)(void *data);
c9e617a5 113 int (*get_pendown_state)(void);
ffa458c1
DB
114};
115
116/* leave chip selected when we're done, for quicker re-select? */
117#if 0
118#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
119#else
120#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
121#endif
122
123/*--------------------------------------------------------------------------*/
124
125/* The ADS7846 has touchscreen and other sensors.
126 * Earlier ads784x chips are somewhat compatible.
127 */
128#define ADS_START (1 << 7)
129#define ADS_A2A1A0_d_y (1 << 4) /* differential */
130#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
131#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
132#define ADS_A2A1A0_d_x (5 << 4) /* differential */
133#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
134#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
135#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
136#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
137#define ADS_8_BIT (1 << 3)
138#define ADS_12_BIT (0 << 3)
139#define ADS_SER (1 << 2) /* non-differential */
140#define ADS_DFR (0 << 2) /* differential */
141#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
142#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
143#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
144#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
145
146#define MAX_12BIT ((1<<12)-1)
147
148/* leave ADC powered up (disables penirq) between differential samples */
de2defd9
ID
149#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
150 | ADS_12_BIT | ADS_DFR | \
151 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
ffa458c1 152
de2defd9
ID
153#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
154#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
155#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
53a0ef89 156
de2defd9
ID
157#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
158#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
ffa458c1
DB
159
160/* single-ended samples need to first power up reference voltage;
161 * we leave both ADC and VREF powered
162 */
163#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
164 | ADS_12_BIT | ADS_SER)
165
de2defd9
ID
166#define REF_ON (READ_12BIT_DFR(x, 1, 1))
167#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
ffa458c1
DB
168
169/*--------------------------------------------------------------------------*/
170
171/*
172 * Non-touchscreen sensors only use single-ended conversions.
173 */
174
175struct ser_req {
d93f70b2 176 u8 ref_on;
ffa458c1 177 u8 command;
d93f70b2 178 u8 ref_off;
ffa458c1
DB
179 u16 scratch;
180 __be16 sample;
181 struct spi_message msg;
182 struct spi_transfer xfer[6];
183};
184
7de90a8c
ID
185static void ads7846_enable(struct ads7846 *ts);
186static void ads7846_disable(struct ads7846 *ts);
187
c9e617a5
ID
188static int device_suspended(struct device *dev)
189{
190 struct ads7846 *ts = dev_get_drvdata(dev);
191 return dev->power.power_state.event != PM_EVENT_ON || ts->disabled;
192}
193
ffa458c1
DB
194static int ads7846_read12_ser(struct device *dev, unsigned command)
195{
196 struct spi_device *spi = to_spi_device(dev);
197 struct ads7846 *ts = dev_get_drvdata(dev);
e94b1766 198 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
ffa458c1
DB
199 int status;
200 int sample;
a90f7e98 201 int i;
ffa458c1
DB
202
203 if (!req)
204 return -ENOMEM;
205
0b7018aa 206 spi_message_init(&req->msg);
8275c642 207
ffa458c1 208 /* activate reference, so it has time to settle; */
d93f70b2
DB
209 req->ref_on = REF_ON;
210 req->xfer[0].tx_buf = &req->ref_on;
ffa458c1
DB
211 req->xfer[0].len = 1;
212 req->xfer[1].rx_buf = &req->scratch;
213 req->xfer[1].len = 2;
214
215 /*
216 * for external VREF, 0 usec (and assume it's always on);
217 * for 1uF, use 800 usec;
218 * no cap, 100 usec.
219 */
220 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
221
222 /* take sample */
223 req->command = (u8) command;
224 req->xfer[2].tx_buf = &req->command;
225 req->xfer[2].len = 1;
226 req->xfer[3].rx_buf = &req->sample;
227 req->xfer[3].len = 2;
228
229 /* REVISIT: take a few more samples, and compare ... */
230
231 /* turn off reference */
d93f70b2
DB
232 req->ref_off = REF_OFF;
233 req->xfer[4].tx_buf = &req->ref_off;
ffa458c1
DB
234 req->xfer[4].len = 1;
235 req->xfer[5].rx_buf = &req->scratch;
236 req->xfer[5].len = 2;
237
238 CS_CHANGE(req->xfer[5]);
239
240 /* group all the transfers together, so we can't interfere with
241 * reading touchscreen state; disable penirq while sampling
242 */
8275c642
VW
243 for (i = 0; i < 6; i++)
244 spi_message_add_tail(&req->xfer[i], &req->msg);
ffa458c1 245
c9e617a5 246 ts->irq_disabled = 1;
ffa458c1
DB
247 disable_irq(spi->irq);
248 status = spi_sync(spi, &req->msg);
c9e617a5 249 ts->irq_disabled = 0;
ffa458c1
DB
250 enable_irq(spi->irq);
251
252 if (req->msg.status)
253 status = req->msg.status;
9084533e
DB
254
255 /* on-wire is a must-ignore bit, a BE12 value, then padding */
ffa458c1 256 sample = be16_to_cpu(req->sample);
9084533e
DB
257 sample = sample >> 3;
258 sample &= 0x0fff;
ffa458c1 259
9084533e 260 kfree(req);
ffa458c1
DB
261 return status ? status : sample;
262}
263
264#define SHOW(name) static ssize_t \
265name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
266{ \
267 ssize_t v = ads7846_read12_ser(dev, \
268 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
269 if (v < 0) \
270 return v; \
271 return sprintf(buf, "%u\n", (unsigned) v); \
272} \
273static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
274
275SHOW(temp0)
276SHOW(temp1)
277SHOW(vaux)
278SHOW(vbatt)
279
438f2a74
ID
280static int is_pen_down(struct device *dev)
281{
282 struct ads7846 *ts = dev_get_drvdata(dev);
283
284 return ts->pendown;
285}
286
287static ssize_t ads7846_pen_down_show(struct device *dev,
288 struct device_attribute *attr, char *buf)
289{
290 return sprintf(buf, "%u\n", is_pen_down(dev));
291}
292
293static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
294
7de90a8c
ID
295static ssize_t ads7846_disable_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
297{
298 struct ads7846 *ts = dev_get_drvdata(dev);
299
300 return sprintf(buf, "%u\n", ts->disabled);
301}
302
303static ssize_t ads7846_disable_store(struct device *dev,
304 struct device_attribute *attr,
305 const char *buf, size_t count)
306{
307 struct ads7846 *ts = dev_get_drvdata(dev);
308 char *endp;
309 int i;
310
311 i = simple_strtoul(buf, &endp, 10);
312 spin_lock_irq(&ts->lock);
313
314 if (i)
315 ads7846_disable(ts);
316 else
317 ads7846_enable(ts);
318
319 spin_unlock_irq(&ts->lock);
320
321 return count;
322}
323
324static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
325
8dd51650
DT
326static struct attribute *ads7846_attributes[] = {
327 &dev_attr_temp0.attr,
328 &dev_attr_temp1.attr,
329 &dev_attr_vbatt.attr,
330 &dev_attr_vaux.attr,
331 &dev_attr_pen_down.attr,
332 &dev_attr_disable.attr,
333 NULL,
334};
335
336static struct attribute_group ads7846_attr_group = {
337 .attrs = ads7846_attributes,
338};
339
340/*
341 * ads7843/7845 don't have temperature sensors, and
342 * use the other sensors a bit differently too
343 */
344
345static struct attribute *ads7843_attributes[] = {
346 &dev_attr_vbatt.attr,
347 &dev_attr_vaux.attr,
348 &dev_attr_pen_down.attr,
349 &dev_attr_disable.attr,
350 NULL,
351};
352
353static struct attribute_group ads7843_attr_group = {
354 .attrs = ads7843_attributes,
355};
356
357static struct attribute *ads7845_attributes[] = {
358 &dev_attr_vaux.attr,
359 &dev_attr_pen_down.attr,
360 &dev_attr_disable.attr,
361 NULL,
362};
363
364static struct attribute_group ads7845_attr_group = {
365 .attrs = ads7845_attributes,
366};
367
ffa458c1
DB
368/*--------------------------------------------------------------------------*/
369
370/*
371 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
372 * to retrieve touchscreen status.
373 *
374 * The SPI transfer completion callback does the real work. It reports
375 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
376 */
377
378static void ads7846_rx(void *ads)
379{
a90f7e98
DT
380 struct ads7846 *ts = ads;
381 struct input_dev *input_dev = ts->input;
382 unsigned Rt;
383 unsigned sync = 0;
384 u16 x, y, z1, z2;
385 unsigned long flags;
ffa458c1 386
da970e69
ID
387 /* ads7846_rx_val() did in-place conversion (including byteswap) from
388 * on-the-wire format as part of debouncing to get stable readings.
ffa458c1 389 */
da970e69
ID
390 x = ts->tc.x;
391 y = ts->tc.y;
392 z1 = ts->tc.z1;
393 z2 = ts->tc.z2;
ffa458c1
DB
394
395 /* range filtering */
396 if (x == MAX_12BIT)
397 x = 0;
398
c9e617a5 399 if (likely(x && z1 && !device_suspended(&ts->spi->dev))) {
ffa458c1
DB
400 /* compute touch pressure resistance using equation #2 */
401 Rt = z2;
402 Rt -= z1;
403 Rt *= x;
404 Rt *= ts->x_plate_ohms;
405 Rt /= z1;
406 Rt = (Rt + 2047) >> 12;
407 } else
408 Rt = 0;
409
d5b415c9 410 /* Sample found inconsistent by debouncing or pressure is beyond
1936d590
ID
411 * the maximum. Don't report it to user space, repeat at least
412 * once more the measurement
413 */
d5b415c9 414 if (ts->tc.ignore || Rt > ts->pressure_max) {
1936d590
ID
415 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
416 HRTIMER_REL);
d5b415c9
ID
417 return;
418 }
419
ffa458c1
DB
420 /* NOTE: "pendown" is inferred from pressure; we don't rely on
421 * being able to check nPENIRQ status, or "friendly" trigger modes
422 * (both-edges is much better than just-falling or low-level).
423 *
424 * REVISIT: some boards may require reading nPENIRQ; it's
425 * needed on 7843. and 7845 reads pressure differently...
426 *
427 * REVISIT: the touchscreen might not be connected; this code
428 * won't notice that, even if nPENIRQ never fires ...
429 */
430 if (!ts->pendown && Rt != 0) {
a90f7e98 431 input_report_key(input_dev, BTN_TOUCH, 1);
ffa458c1
DB
432 sync = 1;
433 } else if (ts->pendown && Rt == 0) {
a90f7e98 434 input_report_key(input_dev, BTN_TOUCH, 0);
ffa458c1
DB
435 sync = 1;
436 }
437
438 if (Rt) {
a90f7e98
DT
439 input_report_abs(input_dev, ABS_X, x);
440 input_report_abs(input_dev, ABS_Y, y);
ffa458c1
DB
441 sync = 1;
442 }
ae82d5ab
ID
443
444 if (sync) {
445 input_report_abs(input_dev, ABS_PRESSURE, Rt);
a90f7e98 446 input_sync(input_dev);
ae82d5ab 447 }
ffa458c1
DB
448
449#ifdef VERBOSE
450 if (Rt || ts->pendown)
451 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
452 x, y, Rt, Rt ? "" : " UP");
453#endif
454
ffa458c1
DB
455 spin_lock_irqsave(&ts->lock, flags);
456
457 ts->pendown = (Rt != 0);
1936d590 458 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD), HRTIMER_REL);
ffa458c1
DB
459
460 spin_unlock_irqrestore(&ts->lock, flags);
461}
462
da970e69 463static int ads7846_debounce(void *ads, int data_idx, int *val)
0b7018aa
ID
464{
465 struct ads7846 *ts = ads;
0b7018aa 466
da970e69
ID
467 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
468 /* Start over collecting consistent readings. */
469 ts->read_rep = 0;
d5b415c9
ID
470 /* Repeat it, if this was the first read or the read
471 * wasn't consistent enough. */
472 if (ts->read_cnt < ts->debounce_max) {
da970e69 473 ts->last_read = *val;
d5b415c9 474 ts->read_cnt++;
da970e69 475 return ADS7846_FILTER_REPEAT;
d5b415c9
ID
476 } else {
477 /* Maximum number of debouncing reached and still
478 * not enough number of consistent readings. Abort
479 * the whole sample, repeat it in the next sampling
480 * period.
481 */
d5b415c9 482 ts->read_cnt = 0;
da970e69 483 return ADS7846_FILTER_IGNORE;
d5b415c9 484 }
0b7018aa 485 } else {
d5b415c9
ID
486 if (++ts->read_rep > ts->debounce_rep) {
487 /* Got a good reading for this coordinate,
488 * go for the next one. */
d5b415c9
ID
489 ts->read_cnt = 0;
490 ts->read_rep = 0;
da970e69
ID
491 return ADS7846_FILTER_OK;
492 } else {
d5b415c9
ID
493 /* Read more values that are consistent. */
494 ts->read_cnt++;
da970e69
ID
495 return ADS7846_FILTER_REPEAT;
496 }
497 }
498}
499
500static int ads7846_no_filter(void *ads, int data_idx, int *val)
501{
502 return ADS7846_FILTER_OK;
503}
504
505static void ads7846_rx_val(void *ads)
506{
507 struct ads7846 *ts = ads;
508 struct spi_message *m;
509 struct spi_transfer *t;
510 u16 *rx_val;
511 int val;
512 int action;
513 int status;
514
515 m = &ts->msg[ts->msg_idx];
516 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
517 rx_val = t->rx_buf;
518
519 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
520 * built from two 8 bit values written msb-first.
521 */
522 val = be16_to_cpu(*rx_val) >> 3;
523
524 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
525 switch (action) {
526 case ADS7846_FILTER_REPEAT:
527 break;
528 case ADS7846_FILTER_IGNORE:
529 ts->tc.ignore = 1;
530 /* Last message will contain ads7846_rx() as the
531 * completion function.
532 */
533 m = ts->last_msg;
534 break;
535 case ADS7846_FILTER_OK:
536 *rx_val = val;
537 ts->tc.ignore = 0;
538 m = &ts->msg[++ts->msg_idx];
539 break;
540 default:
541 BUG();
0b7018aa
ID
542 }
543 status = spi_async(ts->spi, m);
544 if (status)
545 dev_err(&ts->spi->dev, "spi_async --> %d\n",
546 status);
547}
548
1936d590 549static int ads7846_timer(struct hrtimer *handle)
ffa458c1 550{
1936d590 551 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
ffa458c1 552 int status = 0;
0b7018aa 553
c9e617a5
ID
554 spin_lock_irq(&ts->lock);
555
556 if (unlikely(ts->msg_idx && !ts->pendown)) {
9084533e 557 /* measurement cycle ended */
c9e617a5
ID
558 if (!device_suspended(&ts->spi->dev)) {
559 ts->irq_disabled = 0;
560 enable_irq(ts->spi->irq);
561 }
562 ts->pending = 0;
563 ts->msg_idx = 0;
564 } else {
565 /* pen is still down, continue with the measurement */
566 ts->msg_idx = 0;
567 status = spi_async(ts->spi, &ts->msg[0]);
568 if (status)
569 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
570 }
571
572 spin_unlock_irq(&ts->lock);
1936d590 573 return HRTIMER_NORESTART;
0b7018aa
ID
574}
575
7d12e780 576static irqreturn_t ads7846_irq(int irq, void *handle)
0b7018aa
ID
577{
578 struct ads7846 *ts = handle;
579 unsigned long flags;
ffa458c1
DB
580
581 spin_lock_irqsave(&ts->lock, flags);
c9e617a5 582 if (likely(ts->get_pendown_state())) {
ffa458c1 583 if (!ts->irq_disabled) {
9084533e
DB
584 /* The ARM do_simple_IRQ() dispatcher doesn't act
585 * like the other dispatchers: it will report IRQs
586 * even after they've been disabled. We work around
587 * that here. (The "generic irq" framework may help...)
0b7018aa 588 */
ffa458c1
DB
589 ts->irq_disabled = 1;
590 disable_irq(ts->spi->irq);
0b7018aa 591 ts->pending = 1;
1936d590
ID
592 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
593 HRTIMER_REL);
0b7018aa 594 }
ffa458c1
DB
595 }
596 spin_unlock_irqrestore(&ts->lock, flags);
7de90a8c
ID
597
598 return IRQ_HANDLED;
ffa458c1
DB
599}
600
601/*--------------------------------------------------------------------------*/
602
7de90a8c
ID
603/* Must be called with ts->lock held */
604static void ads7846_disable(struct ads7846 *ts)
ffa458c1 605{
7de90a8c
ID
606 if (ts->disabled)
607 return;
ffa458c1 608
c9e617a5
ID
609 ts->disabled = 1;
610
ffa458c1 611 /* are we waiting for IRQ, or polling? */
c9e617a5
ID
612 if (!ts->pending) {
613 ts->irq_disabled = 1;
614 disable_irq(ts->spi->irq);
ffa458c1 615 } else {
c9e617a5
ID
616 /* the timer will run at least once more, and
617 * leave everything in a clean state, IRQ disabled
ffa458c1 618 */
c9e617a5 619 while (ts->pending) {
7de90a8c 620 spin_unlock_irq(&ts->lock);
c4febb94 621 msleep(1);
7de90a8c 622 spin_lock_irq(&ts->lock);
ffa458c1
DB
623 }
624 }
625
626 /* we know the chip's in lowpower mode since we always
627 * leave it that way after every request
628 */
629
7de90a8c
ID
630}
631
632/* Must be called with ts->lock held */
633static void ads7846_enable(struct ads7846 *ts)
634{
635 if (!ts->disabled)
636 return;
637
638 ts->disabled = 0;
639 ts->irq_disabled = 0;
640 enable_irq(ts->spi->irq);
641}
642
643static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
644{
645 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
646
647 spin_lock_irq(&ts->lock);
648
649 spi->dev.power.power_state = message;
650 ads7846_disable(ts);
651
652 spin_unlock_irq(&ts->lock);
653
ffa458c1 654 return 0;
7de90a8c 655
ffa458c1
DB
656}
657
2e5a7bd9 658static int ads7846_resume(struct spi_device *spi)
ffa458c1 659{
2e5a7bd9 660 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
ffa458c1 661
7de90a8c
ID
662 spin_lock_irq(&ts->lock);
663
2e5a7bd9 664 spi->dev.power.power_state = PMSG_ON;
7de90a8c
ID
665 ads7846_enable(ts);
666
667 spin_unlock_irq(&ts->lock);
668
ffa458c1
DB
669 return 0;
670}
671
2e5a7bd9 672static int __devinit ads7846_probe(struct spi_device *spi)
ffa458c1 673{
ffa458c1 674 struct ads7846 *ts;
a90f7e98 675 struct input_dev *input_dev;
2e5a7bd9 676 struct ads7846_platform_data *pdata = spi->dev.platform_data;
0b7018aa 677 struct spi_message *m;
ffa458c1 678 struct spi_transfer *x;
de2defd9 679 int vref;
a90f7e98 680 int err;
ffa458c1
DB
681
682 if (!spi->irq) {
2e5a7bd9 683 dev_dbg(&spi->dev, "no IRQ?\n");
ffa458c1
DB
684 return -ENODEV;
685 }
686
687 if (!pdata) {
2e5a7bd9 688 dev_dbg(&spi->dev, "no platform data?\n");
ffa458c1
DB
689 return -ENODEV;
690 }
691
692 /* don't exceed max specified sample rate */
d93f70b2 693 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
2e5a7bd9 694 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
d93f70b2 695 (spi->max_speed_hz/SAMPLE_BITS)/1000);
ffa458c1
DB
696 return -EINVAL;
697 }
698
9084533e
DB
699 /* REVISIT when the irq can be triggered active-low, or if for some
700 * reason the touchscreen isn't hooked up, we don't need to access
701 * the pendown state.
702 */
c9e617a5
ID
703 if (pdata->get_pendown_state == NULL) {
704 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
705 return -EINVAL;
706 }
707
9084533e
DB
708 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
709 * that even if the hardware can do that, the SPI controller driver
710 * may not. So we stick to very-portable 8 bit words, both RX and TX.
ffa458c1 711 */
9084533e 712 spi->bits_per_word = 8;
7937e86a
ID
713 spi->mode = SPI_MODE_1;
714 err = spi_setup(spi);
715 if (err < 0)
716 return err;
ffa458c1 717
a90f7e98
DT
718 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
719 input_dev = input_allocate_device();
720 if (!ts || !input_dev) {
721 err = -ENOMEM;
722 goto err_free_mem;
723 }
ffa458c1 724
2e5a7bd9 725 dev_set_drvdata(&spi->dev, ts);
a90f7e98 726 spi->dev.power.power_state = PMSG_ON;
ffa458c1
DB
727
728 ts->spi = spi;
a90f7e98 729 ts->input = input_dev;
ffa458c1 730
1936d590 731 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_REL);
ffa458c1
DB
732 ts->timer.function = ads7846_timer;
733
7de90a8c
ID
734 spin_lock_init(&ts->lock);
735
ffa458c1
DB
736 ts->model = pdata->model ? : 7846;
737 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
738 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
d5b415c9 739 ts->pressure_max = pdata->pressure_max ? : ~0;
da970e69
ID
740
741 if (pdata->filter != NULL) {
742 if (pdata->filter_init != NULL) {
743 err = pdata->filter_init(pdata, &ts->filter_data);
744 if (err < 0)
745 goto err_free_mem;
746 }
747 ts->filter = pdata->filter;
748 ts->filter_cleanup = pdata->filter_cleanup;
749 } else if (pdata->debounce_max) {
d5b415c9 750 ts->debounce_max = pdata->debounce_max;
da970e69
ID
751 if (ts->debounce_max < 2)
752 ts->debounce_max = 2;
d5b415c9
ID
753 ts->debounce_tol = pdata->debounce_tol;
754 ts->debounce_rep = pdata->debounce_rep;
da970e69
ID
755 ts->filter = ads7846_debounce;
756 ts->filter_data = ts;
d5b415c9 757 } else
da970e69 758 ts->filter = ads7846_no_filter;
c9e617a5 759 ts->get_pendown_state = pdata->get_pendown_state;
ffa458c1 760
a90f7e98 761 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
ffa458c1 762
a90f7e98
DT
763 input_dev->name = "ADS784x Touchscreen";
764 input_dev->phys = ts->phys;
765 input_dev->cdev.dev = &spi->dev;
ffa458c1 766
a90f7e98
DT
767 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
768 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
769 input_set_abs_params(input_dev, ABS_X,
ffa458c1
DB
770 pdata->x_min ? : 0,
771 pdata->x_max ? : MAX_12BIT,
772 0, 0);
a90f7e98 773 input_set_abs_params(input_dev, ABS_Y,
ffa458c1
DB
774 pdata->y_min ? : 0,
775 pdata->y_max ? : MAX_12BIT,
776 0, 0);
a90f7e98 777 input_set_abs_params(input_dev, ABS_PRESSURE,
ffa458c1
DB
778 pdata->pressure_min, pdata->pressure_max, 0, 0);
779
de2defd9
ID
780 vref = pdata->keep_vref_on;
781
ffa458c1
DB
782 /* set up the transfers to read touchscreen state; this assumes we
783 * use formula #2 for pressure, not #3.
784 */
0b7018aa 785 m = &ts->msg[0];
ffa458c1
DB
786 x = ts->xfer;
787
0b7018aa
ID
788 spi_message_init(m);
789
ffa458c1 790 /* y- still on; turn on only y+ (and ADC) */
de2defd9 791 ts->read_y = READ_Y(vref);
d93f70b2 792 x->tx_buf = &ts->read_y;
ffa458c1 793 x->len = 1;
0b7018aa 794 spi_message_add_tail(x, m);
d93f70b2 795
ffa458c1
DB
796 x++;
797 x->rx_buf = &ts->tc.y;
798 x->len = 2;
0b7018aa
ID
799 spi_message_add_tail(x, m);
800
da970e69 801 m->complete = ads7846_rx_val;
0b7018aa
ID
802 m->context = ts;
803
804 m++;
805 spi_message_init(m);
806
807 /* turn y- off, x+ on, then leave in lowpower */
808 x++;
de2defd9 809 ts->read_x = READ_X(vref);
0b7018aa
ID
810 x->tx_buf = &ts->read_x;
811 x->len = 1;
812 spi_message_add_tail(x, m);
813
814 x++;
815 x->rx_buf = &ts->tc.x;
816 x->len = 2;
817 spi_message_add_tail(x, m);
818
da970e69 819 m->complete = ads7846_rx_val;
0b7018aa 820 m->context = ts;
ffa458c1
DB
821
822 /* turn y+ off, x- on; we'll use formula #2 */
823 if (ts->model == 7846) {
0b7018aa
ID
824 m++;
825 spi_message_init(m);
826
d93f70b2 827 x++;
de2defd9 828 ts->read_z1 = READ_Z1(vref);
d93f70b2 829 x->tx_buf = &ts->read_z1;
ffa458c1 830 x->len = 1;
0b7018aa 831 spi_message_add_tail(x, m);
d93f70b2 832
ffa458c1
DB
833 x++;
834 x->rx_buf = &ts->tc.z1;
835 x->len = 2;
0b7018aa
ID
836 spi_message_add_tail(x, m);
837
da970e69 838 m->complete = ads7846_rx_val;
0b7018aa
ID
839 m->context = ts;
840
841 m++;
842 spi_message_init(m);
ffa458c1 843
d93f70b2 844 x++;
de2defd9 845 ts->read_z2 = READ_Z2(vref);
d93f70b2 846 x->tx_buf = &ts->read_z2;
ffa458c1 847 x->len = 1;
0b7018aa 848 spi_message_add_tail(x, m);
d93f70b2 849
ffa458c1
DB
850 x++;
851 x->rx_buf = &ts->tc.z2;
852 x->len = 2;
0b7018aa 853 spi_message_add_tail(x, m);
ffa458c1 854
da970e69 855 m->complete = ads7846_rx_val;
0b7018aa
ID
856 m->context = ts;
857 }
53a0ef89
ID
858
859 /* power down */
0b7018aa
ID
860 m++;
861 spi_message_init(m);
862
53a0ef89
ID
863 x++;
864 ts->pwrdown = PWRDOWN;
865 x->tx_buf = &ts->pwrdown;
866 x->len = 1;
0b7018aa 867 spi_message_add_tail(x, m);
53a0ef89
ID
868
869 x++;
870 x->rx_buf = &ts->dummy;
871 x->len = 2;
d93f70b2 872 CS_CHANGE(*x);
0b7018aa 873 spi_message_add_tail(x, m);
ffa458c1 874
0b7018aa
ID
875 m->complete = ads7846_rx;
876 m->context = ts;
ffa458c1 877
d5b415c9
ID
878 ts->last_msg = m;
879
dace1453 880 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
9084533e 881 spi->dev.driver->name, ts)) {
2e5a7bd9 882 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
a90f7e98 883 err = -EBUSY;
da970e69 884 goto err_cleanup_filter;
ffa458c1 885 }
ffa458c1 886
2e5a7bd9 887 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
ffa458c1
DB
888
889 /* take a first sample, leaving nPENIRQ active; avoid
890 * the touchscreen, in case it's not connected.
891 */
2e5a7bd9 892 (void) ads7846_read12_ser(&spi->dev,
ffa458c1
DB
893 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
894
8dd51650
DT
895 switch (ts->model) {
896 case 7846:
897 ts->attr_group = &ads7846_attr_group;
898 break;
899 case 7845:
900 ts->attr_group = &ads7845_attr_group;
901 break;
902 default:
903 ts->attr_group = &ads7843_attr_group;
904 break;
ffa458c1 905 }
8dd51650
DT
906 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
907 if (err)
908 goto err_free_irq;
7de90a8c 909
a90f7e98
DT
910 err = input_register_device(input_dev);
911 if (err)
8dd51650 912 goto err_remove_attr_group;
a90f7e98 913
ffa458c1 914 return 0;
a90f7e98 915
8dd51650
DT
916 err_remove_attr_group:
917 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
918 err_free_irq:
a90f7e98 919 free_irq(spi->irq, ts);
da970e69
ID
920 err_cleanup_filter:
921 if (ts->filter_cleanup)
922 ts->filter_cleanup(ts->filter_data);
a90f7e98
DT
923 err_free_mem:
924 input_free_device(input_dev);
925 kfree(ts);
926 return err;
ffa458c1
DB
927}
928
2e5a7bd9 929static int __devexit ads7846_remove(struct spi_device *spi)
ffa458c1 930{
2e5a7bd9 931 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
ffa458c1 932
7de90a8c
ID
933 input_unregister_device(ts->input);
934
2e5a7bd9 935 ads7846_suspend(spi, PMSG_SUSPEND);
ffa458c1 936
8dd51650 937 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
ffa458c1 938
7de90a8c 939 free_irq(ts->spi->irq, ts);
c9e617a5
ID
940 /* suspend left the IRQ disabled */
941 enable_irq(ts->spi->irq);
7de90a8c 942
da970e69
ID
943 if (ts->filter_cleanup)
944 ts->filter_cleanup(ts->filter_data);
945
ffa458c1
DB
946 kfree(ts);
947
2e5a7bd9 948 dev_dbg(&spi->dev, "unregistered touchscreen\n");
ffa458c1
DB
949 return 0;
950}
951
2e5a7bd9
DB
952static struct spi_driver ads7846_driver = {
953 .driver = {
954 .name = "ads7846",
955 .bus = &spi_bus_type,
956 .owner = THIS_MODULE,
957 },
ffa458c1 958 .probe = ads7846_probe,
2e5a7bd9 959 .remove = __devexit_p(ads7846_remove),
ffa458c1
DB
960 .suspend = ads7846_suspend,
961 .resume = ads7846_resume,
962};
963
964static int __init ads7846_init(void)
965{
966 /* grr, board-specific init should stay out of drivers!! */
967
968#ifdef CONFIG_ARCH_OMAP
969 if (machine_is_omap_osk()) {
970 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
971 omap_request_gpio(4);
972 omap_set_gpio_direction(4, 1);
973 omap_request_gpio(6);
974 omap_set_gpio_direction(6, 1);
975 }
976 // also TI 1510 Innovator, bitbanging through FPGA
977 // also Nokia 770
978 // also Palm Tungsten T2
979#endif
980
981 // PXA:
982 // also Dell Axim X50
983 // also HP iPaq H191x/H192x/H415x/H435x
2e5a7bd9 984 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
ffa458c1
DB
985 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
986
2e5a7bd9
DB
987 // Atmel at91sam9261-EK uses ads7843
988
ffa458c1
DB
989 // also various AMD Au1x00 devel boards
990
2e5a7bd9 991 return spi_register_driver(&ads7846_driver);
ffa458c1
DB
992}
993module_init(ads7846_init);
994
995static void __exit ads7846_exit(void)
996{
2e5a7bd9 997 spi_unregister_driver(&ads7846_driver);
ffa458c1
DB
998
999#ifdef CONFIG_ARCH_OMAP
1000 if (machine_is_omap_osk()) {
1001 omap_free_gpio(4);
1002 omap_free_gpio(6);
1003 }
1004#endif
1005
1006}
1007module_exit(ads7846_exit);
1008
1009MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1010MODULE_LICENSE("GPL");
This page took 0.169895 seconds and 5 git commands to generate.