dd8c6a9ffc762a2bf8e071ced39cfa467b6920a9
[deliverable/linux.git] / drivers / input / touchscreen / ads7846.c
1 /*
2 * ADS7846 based touchscreen and sensor driver
3 *
4 * Copyright (c) 2005 David Brownell
5 *
6 * Using code from:
7 * - corgi_ts.c
8 * Copyright (C) 2004-2005 Richard Purdie
9 * - omap_ts.[hc], ads7846.h, ts_osk.c
10 * Copyright (C) 2002 MontaVista Software
11 * Copyright (C) 2004 Texas Instruments
12 * Copyright (C) 2005 Dirk Behme
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18 #include <linux/device.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/input.h>
22 #include <linux/interrupt.h>
23 #include <linux/slab.h>
24 #include <linux/spi/spi.h>
25 #include <linux/spi/ads7846.h>
26
27 #ifdef CONFIG_ARM
28 #include <asm/mach-types.h>
29 #ifdef CONFIG_ARCH_OMAP
30 #include <asm/arch/gpio.h>
31 #endif
32
33 #else
34 #define set_irq_type(irq,type) do{}while(0)
35 #endif
36
37
38 /*
39 * This code has been lightly tested on an ads7846.
40 * Support for ads7843 and ads7845 has only been stubbed in.
41 *
42 * Not yet done: investigate the values reported. Are x/y/pressure
43 * event values sane enough for X11? How accurate are the temperature
44 * and voltage readings? (System-specific calibration should support
45 * accuracy of 0.3 degrees C; otherwise it's 2.0 degrees.)
46 *
47 * app note sbaa036 talks in more detail about accurate sampling...
48 * that ought to help in situations like LCDs inducing noise (which
49 * can also be helped by using synch signals) and more generally.
50 */
51
52 #define TS_POLL_PERIOD msecs_to_jiffies(10)
53
54 struct ts_event {
55 /* For portability, we can't read 12 bit values using SPI (which
56 * would make the controller deliver them as native byteorder u16
57 * with msbs zeroed). Instead, we read them as two 8-byte values,
58 * which need byteswapping then range adjustment.
59 */
60 __be16 x;
61 __be16 y;
62 __be16 z1, z2;
63 };
64
65 struct ads7846 {
66 struct input_dev input;
67 char phys[32];
68
69 struct spi_device *spi;
70 u16 model;
71 u16 vref_delay_usecs;
72 u16 x_plate_ohms;
73
74 struct ts_event tc;
75
76 struct spi_transfer xfer[8];
77 struct spi_message msg;
78
79 spinlock_t lock;
80 struct timer_list timer; /* P: lock */
81 unsigned pendown:1; /* P: lock */
82 unsigned pending:1; /* P: lock */
83 // FIXME remove "irq_disabled"
84 unsigned irq_disabled:1; /* P: lock */
85 };
86
87 /* leave chip selected when we're done, for quicker re-select? */
88 #if 0
89 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
90 #else
91 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
92 #endif
93
94 /*--------------------------------------------------------------------------*/
95
96 /* The ADS7846 has touchscreen and other sensors.
97 * Earlier ads784x chips are somewhat compatible.
98 */
99 #define ADS_START (1 << 7)
100 #define ADS_A2A1A0_d_y (1 << 4) /* differential */
101 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
102 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
103 #define ADS_A2A1A0_d_x (5 << 4) /* differential */
104 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
105 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
106 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
107 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
108 #define ADS_8_BIT (1 << 3)
109 #define ADS_12_BIT (0 << 3)
110 #define ADS_SER (1 << 2) /* non-differential */
111 #define ADS_DFR (0 << 2) /* differential */
112 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
113 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
114 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
115 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
116
117 #define MAX_12BIT ((1<<12)-1)
118
119 /* leave ADC powered up (disables penirq) between differential samples */
120 #define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
121 | ADS_12_BIT | ADS_DFR)
122
123 static const u8 read_y = READ_12BIT_DFR(y) | ADS_PD10_ADC_ON;
124 static const u8 read_z1 = READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON;
125 static const u8 read_z2 = READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON;
126 static const u8 read_x = READ_12BIT_DFR(x) | ADS_PD10_PDOWN; /* LAST */
127
128 /* single-ended samples need to first power up reference voltage;
129 * we leave both ADC and VREF powered
130 */
131 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
132 | ADS_12_BIT | ADS_SER)
133
134 static const u8 ref_on = READ_12BIT_DFR(x) | ADS_PD10_ALL_ON;
135 static const u8 ref_off = READ_12BIT_DFR(y) | ADS_PD10_PDOWN;
136
137 /*--------------------------------------------------------------------------*/
138
139 /*
140 * Non-touchscreen sensors only use single-ended conversions.
141 */
142
143 struct ser_req {
144 u8 command;
145 u16 scratch;
146 __be16 sample;
147 struct spi_message msg;
148 struct spi_transfer xfer[6];
149 };
150
151 static int ads7846_read12_ser(struct device *dev, unsigned command)
152 {
153 struct spi_device *spi = to_spi_device(dev);
154 struct ads7846 *ts = dev_get_drvdata(dev);
155 struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL);
156 int status;
157 int sample;
158 int i;
159
160 if (!req)
161 return -ENOMEM;
162
163 INIT_LIST_HEAD(&req->msg.transfers);
164
165 /* activate reference, so it has time to settle; */
166 req->xfer[0].tx_buf = &ref_on;
167 req->xfer[0].len = 1;
168 req->xfer[1].rx_buf = &req->scratch;
169 req->xfer[1].len = 2;
170
171 /*
172 * for external VREF, 0 usec (and assume it's always on);
173 * for 1uF, use 800 usec;
174 * no cap, 100 usec.
175 */
176 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
177
178 /* take sample */
179 req->command = (u8) command;
180 req->xfer[2].tx_buf = &req->command;
181 req->xfer[2].len = 1;
182 req->xfer[3].rx_buf = &req->sample;
183 req->xfer[3].len = 2;
184
185 /* REVISIT: take a few more samples, and compare ... */
186
187 /* turn off reference */
188 req->xfer[4].tx_buf = &ref_off;
189 req->xfer[4].len = 1;
190 req->xfer[5].rx_buf = &req->scratch;
191 req->xfer[5].len = 2;
192
193 CS_CHANGE(req->xfer[5]);
194
195 /* group all the transfers together, so we can't interfere with
196 * reading touchscreen state; disable penirq while sampling
197 */
198 for (i = 0; i < 6; i++)
199 spi_message_add_tail(&req->xfer[i], &req->msg);
200
201 disable_irq(spi->irq);
202 status = spi_sync(spi, &req->msg);
203 enable_irq(spi->irq);
204
205 if (req->msg.status)
206 status = req->msg.status;
207 sample = be16_to_cpu(req->sample);
208 sample = sample >> 4;
209 kfree(req);
210
211 return status ? status : sample;
212 }
213
214 #define SHOW(name) static ssize_t \
215 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
216 { \
217 ssize_t v = ads7846_read12_ser(dev, \
218 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
219 if (v < 0) \
220 return v; \
221 return sprintf(buf, "%u\n", (unsigned) v); \
222 } \
223 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
224
225 SHOW(temp0)
226 SHOW(temp1)
227 SHOW(vaux)
228 SHOW(vbatt)
229
230 /*--------------------------------------------------------------------------*/
231
232 /*
233 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
234 * to retrieve touchscreen status.
235 *
236 * The SPI transfer completion callback does the real work. It reports
237 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
238 */
239
240 static void ads7846_rx(void *ads)
241 {
242 struct ads7846 *ts = ads;
243 unsigned Rt;
244 unsigned sync = 0;
245 u16 x, y, z1, z2;
246 unsigned long flags;
247
248 /* adjust: 12 bit samples (left aligned), built from
249 * two 8 bit values writen msb-first.
250 */
251 x = be16_to_cpu(ts->tc.x) >> 4;
252 y = be16_to_cpu(ts->tc.y) >> 4;
253 z1 = be16_to_cpu(ts->tc.z1) >> 4;
254 z2 = be16_to_cpu(ts->tc.z2) >> 4;
255
256 /* range filtering */
257 if (x == MAX_12BIT)
258 x = 0;
259
260 if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
261 /* compute touch pressure resistance using equation #2 */
262 Rt = z2;
263 Rt -= z1;
264 Rt *= x;
265 Rt *= ts->x_plate_ohms;
266 Rt /= z1;
267 Rt = (Rt + 2047) >> 12;
268 } else
269 Rt = 0;
270
271 /* NOTE: "pendown" is inferred from pressure; we don't rely on
272 * being able to check nPENIRQ status, or "friendly" trigger modes
273 * (both-edges is much better than just-falling or low-level).
274 *
275 * REVISIT: some boards may require reading nPENIRQ; it's
276 * needed on 7843. and 7845 reads pressure differently...
277 *
278 * REVISIT: the touchscreen might not be connected; this code
279 * won't notice that, even if nPENIRQ never fires ...
280 */
281 if (!ts->pendown && Rt != 0) {
282 input_report_key(&ts->input, BTN_TOUCH, 1);
283 sync = 1;
284 } else if (ts->pendown && Rt == 0) {
285 input_report_key(&ts->input, BTN_TOUCH, 0);
286 sync = 1;
287 }
288
289 if (Rt) {
290 input_report_abs(&ts->input, ABS_X, x);
291 input_report_abs(&ts->input, ABS_Y, y);
292 input_report_abs(&ts->input, ABS_PRESSURE, Rt);
293 sync = 1;
294 }
295 if (sync)
296 input_sync(&ts->input);
297
298 #ifdef VERBOSE
299 if (Rt || ts->pendown)
300 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
301 x, y, Rt, Rt ? "" : " UP");
302 #endif
303
304 /* don't retrigger while we're suspended */
305 spin_lock_irqsave(&ts->lock, flags);
306
307 ts->pendown = (Rt != 0);
308 ts->pending = 0;
309
310 if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
311 if (ts->pendown)
312 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
313 else if (ts->irq_disabled) {
314 ts->irq_disabled = 0;
315 enable_irq(ts->spi->irq);
316 }
317 }
318
319 spin_unlock_irqrestore(&ts->lock, flags);
320 }
321
322 static void ads7846_timer(unsigned long handle)
323 {
324 struct ads7846 *ts = (void *)handle;
325 int status = 0;
326 unsigned long flags;
327
328 spin_lock_irqsave(&ts->lock, flags);
329 if (!ts->pending) {
330 ts->pending = 1;
331 if (!ts->irq_disabled) {
332 ts->irq_disabled = 1;
333 disable_irq(ts->spi->irq);
334 }
335 status = spi_async(ts->spi, &ts->msg);
336 if (status)
337 dev_err(&ts->spi->dev, "spi_async --> %d\n",
338 status);
339 }
340 spin_unlock_irqrestore(&ts->lock, flags);
341 }
342
343 static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
344 {
345 ads7846_timer((unsigned long) handle);
346 return IRQ_HANDLED;
347 }
348
349 /*--------------------------------------------------------------------------*/
350
351 static int
352 ads7846_suspend(struct spi_device *spi, pm_message_t message)
353 {
354 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
355 unsigned long flags;
356
357 spin_lock_irqsave(&ts->lock, flags);
358
359 spi->dev.power.power_state = message;
360
361 /* are we waiting for IRQ, or polling? */
362 if (!ts->pendown) {
363 if (!ts->irq_disabled) {
364 ts->irq_disabled = 1;
365 disable_irq(ts->spi->irq);
366 }
367 } else {
368 /* polling; force a final SPI completion;
369 * that will clean things up neatly
370 */
371 if (!ts->pending)
372 mod_timer(&ts->timer, jiffies);
373
374 while (ts->pendown || ts->pending) {
375 spin_unlock_irqrestore(&ts->lock, flags);
376 udelay(10);
377 spin_lock_irqsave(&ts->lock, flags);
378 }
379 }
380
381 /* we know the chip's in lowpower mode since we always
382 * leave it that way after every request
383 */
384
385 spin_unlock_irqrestore(&ts->lock, flags);
386 return 0;
387 }
388
389 static int ads7846_resume(struct spi_device *spi)
390 {
391 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
392
393 ts->irq_disabled = 0;
394 enable_irq(ts->spi->irq);
395 spi->dev.power.power_state = PMSG_ON;
396 return 0;
397 }
398
399 static int __devinit ads7846_probe(struct spi_device *spi)
400 {
401 struct ads7846 *ts;
402 struct ads7846_platform_data *pdata = spi->dev.platform_data;
403 struct spi_transfer *x;
404 int i;
405
406 if (!spi->irq) {
407 dev_dbg(&spi->dev, "no IRQ?\n");
408 return -ENODEV;
409 }
410
411 if (!pdata) {
412 dev_dbg(&spi->dev, "no platform data?\n");
413 return -ENODEV;
414 }
415
416 /* don't exceed max specified sample rate */
417 if (spi->max_speed_hz > (125000 * 16)) {
418 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
419 (spi->max_speed_hz/16)/1000);
420 return -EINVAL;
421 }
422
423 /* We'd set the wordsize to 12 bits ... except that some controllers
424 * will then treat the 8 bit command words as 12 bits (and drop the
425 * four MSBs of the 12 bit result). Result: inputs must be shifted
426 * to discard the four garbage LSBs.
427 */
428
429 if (!(ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL)))
430 return -ENOMEM;
431
432 dev_set_drvdata(&spi->dev, ts);
433
434 ts->spi = spi;
435 spi->dev.power.power_state = PMSG_ON;
436
437 init_timer(&ts->timer);
438 ts->timer.data = (unsigned long) ts;
439 ts->timer.function = ads7846_timer;
440
441 ts->model = pdata->model ? : 7846;
442 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
443 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
444
445 init_input_dev(&ts->input);
446
447 ts->input.dev = &spi->dev;
448 ts->input.name = "ADS784x Touchscreen";
449 snprintf(ts->phys, sizeof ts->phys, "%s/input0", spi->dev.bus_id);
450 ts->input.phys = ts->phys;
451
452 ts->input.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
453 ts->input.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
454 input_set_abs_params(&ts->input, ABS_X,
455 pdata->x_min ? : 0,
456 pdata->x_max ? : MAX_12BIT,
457 0, 0);
458 input_set_abs_params(&ts->input, ABS_Y,
459 pdata->y_min ? : 0,
460 pdata->y_max ? : MAX_12BIT,
461 0, 0);
462 input_set_abs_params(&ts->input, ABS_PRESSURE,
463 pdata->pressure_min, pdata->pressure_max, 0, 0);
464
465 input_register_device(&ts->input);
466
467 /* set up the transfers to read touchscreen state; this assumes we
468 * use formula #2 for pressure, not #3.
469 */
470 x = ts->xfer;
471
472 /* y- still on; turn on only y+ (and ADC) */
473 x->tx_buf = &read_y;
474 x->len = 1;
475 x++;
476 x->rx_buf = &ts->tc.y;
477 x->len = 2;
478 x++;
479
480 /* turn y+ off, x- on; we'll use formula #2 */
481 if (ts->model == 7846) {
482 x->tx_buf = &read_z1;
483 x->len = 1;
484 x++;
485 x->rx_buf = &ts->tc.z1;
486 x->len = 2;
487 x++;
488
489 x->tx_buf = &read_z2;
490 x->len = 1;
491 x++;
492 x->rx_buf = &ts->tc.z2;
493 x->len = 2;
494 x++;
495 }
496
497 /* turn y- off, x+ on, then leave in lowpower */
498 x->tx_buf = &read_x;
499 x->len = 1;
500 x++;
501 x->rx_buf = &ts->tc.x;
502 x->len = 2;
503 x++;
504
505 CS_CHANGE(x[-1]);
506
507 for (i = 0; i < x - ts->xfer; i++)
508 spi_message_add_tail(&ts->xfer[i], &ts->msg);
509 ts->msg.complete = ads7846_rx;
510 ts->msg.context = ts;
511
512 if (request_irq(spi->irq, ads7846_irq, SA_SAMPLE_RANDOM,
513 spi->dev.bus_id, ts)) {
514 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
515 input_unregister_device(&ts->input);
516 kfree(ts);
517 return -EBUSY;
518 }
519 set_irq_type(spi->irq, IRQT_FALLING);
520
521 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
522
523 /* take a first sample, leaving nPENIRQ active; avoid
524 * the touchscreen, in case it's not connected.
525 */
526 (void) ads7846_read12_ser(&spi->dev,
527 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
528
529 /* ads7843/7845 don't have temperature sensors, and
530 * use the other sensors a bit differently too
531 */
532 if (ts->model == 7846) {
533 device_create_file(&spi->dev, &dev_attr_temp0);
534 device_create_file(&spi->dev, &dev_attr_temp1);
535 }
536 if (ts->model != 7845)
537 device_create_file(&spi->dev, &dev_attr_vbatt);
538 device_create_file(&spi->dev, &dev_attr_vaux);
539
540 return 0;
541 }
542
543 static int __devexit ads7846_remove(struct spi_device *spi)
544 {
545 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
546
547 ads7846_suspend(spi, PMSG_SUSPEND);
548 free_irq(ts->spi->irq, ts);
549 if (ts->irq_disabled)
550 enable_irq(ts->spi->irq);
551
552 if (ts->model == 7846) {
553 device_remove_file(&spi->dev, &dev_attr_temp0);
554 device_remove_file(&spi->dev, &dev_attr_temp1);
555 }
556 if (ts->model != 7845)
557 device_remove_file(&spi->dev, &dev_attr_vbatt);
558 device_remove_file(&spi->dev, &dev_attr_vaux);
559
560 input_unregister_device(&ts->input);
561 kfree(ts);
562
563 dev_dbg(&spi->dev, "unregistered touchscreen\n");
564 return 0;
565 }
566
567 static struct spi_driver ads7846_driver = {
568 .driver = {
569 .name = "ads7846",
570 .bus = &spi_bus_type,
571 .owner = THIS_MODULE,
572 },
573 .probe = ads7846_probe,
574 .remove = __devexit_p(ads7846_remove),
575 .suspend = ads7846_suspend,
576 .resume = ads7846_resume,
577 };
578
579 static int __init ads7846_init(void)
580 {
581 /* grr, board-specific init should stay out of drivers!! */
582
583 #ifdef CONFIG_ARCH_OMAP
584 if (machine_is_omap_osk()) {
585 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
586 omap_request_gpio(4);
587 omap_set_gpio_direction(4, 1);
588 omap_request_gpio(6);
589 omap_set_gpio_direction(6, 1);
590 }
591 // also TI 1510 Innovator, bitbanging through FPGA
592 // also Nokia 770
593 // also Palm Tungsten T2
594 #endif
595
596 // PXA:
597 // also Dell Axim X50
598 // also HP iPaq H191x/H192x/H415x/H435x
599 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
600 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
601
602 // Atmel at91sam9261-EK uses ads7843
603
604 // also various AMD Au1x00 devel boards
605
606 return spi_register_driver(&ads7846_driver);
607 }
608 module_init(ads7846_init);
609
610 static void __exit ads7846_exit(void)
611 {
612 spi_unregister_driver(&ads7846_driver);
613
614 #ifdef CONFIG_ARCH_OMAP
615 if (machine_is_omap_osk()) {
616 omap_free_gpio(4);
617 omap_free_gpio(6);
618 }
619 #endif
620
621 }
622 module_exit(ads7846_exit);
623
624 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
625 MODULE_LICENSE("GPL");
This page took 0.043685 seconds and 4 git commands to generate.