]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/touchscreen/tsc2046_ts.c
80aea9fa59168f442a6850de83d8b9659edbc073
[linux-2.6-omap-h63xx.git] / drivers / input / touchscreen / tsc2046_ts.c
1 /*
2  * TSC2046 Touchscreen driver
3  *
4  * Author: Kevin Hilman, MontaVista Software, Inc. <source@mvista.com>
5  *
6  * Communication details from original TI driver
7  * Copyright (C) 2004-2005 Texas Instruments, Inc.
8  *
9  * Structure based heavily on TSC2301 driver
10  * Copyright (C) 2005-2006 Nokia Corporation
11  *
12  * 2007 (c) MontaVista Software, Inc. This file is licensed under
13  * the terms of the GNU General Public License version 2. This program
14  * is licensed "as is" without any warranty of any kind, whether express
15  * or implied.
16  *
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/input.h>
22 #include <linux/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/spi/spi.h>
25
26 #ifdef CONFIG_ARCH_OMAP
27 #include <asm/arch/gpio.h>
28 #endif
29
30 #include <linux/spi/tsc2046.h>
31
32 /* TSC2046 commands */
33 #define START_BYTE              0x8000
34 #define X_CMDWD                 0xd300
35 #define Y_CMDWD                 0x9300
36 #define Z1_CMDWD                0xb300
37 #define Z2_CMDWD                0xc300
38
39 #define TSC2046_TS_SCAN_TIME    1
40 #define MAX_12BIT               ((1 << 12) - 1)
41
42 struct tsc2046_ts {
43         struct input_dev        *idev;
44         char                    phys[32];
45         struct timer_list       timer;
46         spinlock_t              lock;
47
48         struct spi_transfer     read_xfer[3];
49         struct spi_message      read_msg;
50         struct spi_message      enable_msg;
51         u16                     data[5];
52
53         u16                     x;
54         u16                     y;
55         u16                     p;
56         int                     sample_cnt;
57
58         int                     ignore_last : 1;
59         u16                     x_plate_ohm;
60         int                     max_pressure;
61         int                     touch_pressure;
62         int                     pressure_limit;
63
64         u16                     irq_enabled:1;
65         u16                     pen_down:1;
66         u16                     disabled:1;
67         u16                     pending:1;
68
69         s16                     dav_gpio;
70         int                     irq;
71 };
72
73 static const u16 tsc2046_ts_cmd_data[] = {
74         START_BYTE, X_CMDWD, Y_CMDWD, Z1_CMDWD, Z2_CMDWD,
75 };
76
77 static int device_suspended(struct device *dev)
78 {
79         struct tsc2046 *tsc = dev_get_drvdata(dev);
80         return dev->power.power_state.event != PM_EVENT_ON || tsc->ts->disabled;
81 }
82
83 static void update_pen_state(struct tsc2046 *tsc, int x, int y, int pressure)
84 {
85         struct tsc2046_ts *ts = tsc->ts;
86         int sync = 0;
87
88         if (pressure) {
89                 input_report_abs(ts->idev, ABS_X, x);
90                 input_report_abs(ts->idev, ABS_Y, y);
91                 input_report_abs(ts->idev, ABS_PRESSURE, pressure);
92                 if (!ts->pen_down)
93                         input_report_key(ts->idev, BTN_TOUCH, 1);
94                 sync = 1;
95         } else if (ts->pen_down) {
96                 input_report_abs(ts->idev, ABS_PRESSURE, 0);
97                 input_report_key(ts->idev, BTN_TOUCH, 0);
98                 sync = 1;
99         }
100
101         if (sync)
102                 input_sync(ts->idev);
103
104         ts->pen_down = pressure ? 1 : 0;
105
106 #ifdef VERBOSE
107         dev_dbg(&tsc->spi->dev, "x %4d y %4d p %4d\n", x, y, pressure);
108 #endif
109 }
110
111 #define CONV_DATA(d1, d2) \
112   (((d1 & 0x7f) << 5) | ((d2 >> 11) & 0x1f))
113
114 /*
115  * This procedure is called by the SPI framework after the coordinates
116  * have been read from TSC2046
117  */
118 static void tsc2046_ts_rx(void *arg)
119 {
120         struct tsc2046 *tsc = arg;
121         struct tsc2046_ts *ts = tsc->ts;
122         unsigned int x, y, z1, z2, pressure;
123
124         x  = CONV_DATA(ts->data[2], ts->data[3]);
125         y  = CONV_DATA(ts->data[1], ts->data[2]);
126         z1 = CONV_DATA(ts->data[3], ts->data[4]);
127         z2 = CONV_DATA(ts->data[4], 0);
128
129         if (z1) {
130                 pressure = ts->x_plate_ohm * x;
131                 pressure /= 4096;
132                 pressure *= z2 - z1;
133                 pressure /= z1;
134         } else
135                 pressure = 0;
136
137         /*
138          * If pressure value is above a preset limit (pen is barely
139          * touching the screen) we can't trust the coordinate values.
140          */
141         if (pressure < ts->pressure_limit && x < MAX_12BIT && y < MAX_12BIT) {
142                 ts->pressure_limit = ts->max_pressure;
143                 if (ts->ignore_last) {
144                         if (ts->sample_cnt)
145                                 update_pen_state(tsc, ts->x, ts->y, ts->p);
146                         ts->x = x;
147                         ts->y = y;
148                         ts->p = pressure;
149                 } else
150                         update_pen_state(tsc, x, y, pressure);
151                 ts->sample_cnt++;
152         }
153
154         mod_timer(&ts->timer,
155                   jiffies + msecs_to_jiffies(TSC2046_TS_SCAN_TIME));
156 }
157
158 static int is_pen_down(struct tsc2046_ts *ts)
159 {
160         return ts->pen_down;
161 }
162
163 /*
164  * Timer is called every TSC2046_TS_SCAN_TIME when the pen is down
165  */
166 static void tsc2046_ts_timer(unsigned long arg)
167 {
168         struct tsc2046 *tsc = (void *) arg;
169         struct tsc2046_ts *ts = tsc->ts;
170         unsigned long flags;
171         int ndav;
172         int r;
173
174         spin_lock_irqsave(&ts->lock, flags);
175         ndav = omap_get_gpio_datain(ts->dav_gpio);
176         if (ndav || device_suspended(&tsc->spi->dev)) {
177                 /* Pen has been lifted */
178                 if (!device_suspended(&tsc->spi->dev) &&
179                     !ts->irq_enabled) {
180                         ts->irq_enabled = 1;
181                         enable_irq(ts->irq);
182                 }
183                 update_pen_state(tsc, 0, 0, 0);
184                 ts->pending = 0;
185                 spin_unlock_irqrestore(&ts->lock, flags);
186
187         } else {
188                 ts->pen_down = 1;
189                 spin_unlock_irqrestore(&ts->lock, flags);
190
191                 r = spi_async(tsc->spi, &ts->read_msg);
192                 if (r)
193                         dev_err(&tsc->spi->dev, "ts: spi_async() failed");
194         }
195 }
196
197 /*
198  * This interrupt is called when pen is down and first coordinates are
199  * available. That is indicated by a falling edge on DEV line.  IRQ is
200  * disabled here because while the pen is down the coordinates are
201  * read by a timer.
202  */
203 static irqreturn_t tsc2046_ts_irq_handler(int irq, void *dev_id)
204 {
205         struct tsc2046 *tsc = dev_id;
206         struct tsc2046_ts *ts = tsc->ts;
207         unsigned long flags;
208
209         spin_lock_irqsave(&ts->lock, flags);
210         if (ts->irq_enabled) {
211                 ts->irq_enabled = 0;
212                 disable_irq(ts->irq);
213                 ts->pending = 1;
214                 ts->pressure_limit = ts->touch_pressure;
215                 ts->sample_cnt = 0;
216                 mod_timer(&ts->timer,
217                           jiffies + msecs_to_jiffies(TSC2046_TS_SCAN_TIME));
218         }
219         spin_unlock_irqrestore(&ts->lock, flags);
220
221         return IRQ_HANDLED;
222 }
223
224 /* Must be called with ts->lock held */
225 static void tsc2046_ts_disable(struct tsc2046 *tsc)
226 {
227         struct tsc2046_ts *ts = tsc->ts;
228
229         if (ts->disabled)
230                 return;
231
232         ts->disabled = 1;
233         if (!ts->pending) {
234                 ts->irq_enabled = 0;
235                 disable_irq(ts->irq);
236         } else {
237                 while (ts->pending) {
238                         spin_unlock_irq(&ts->lock);
239                         msleep(1);
240                         spin_lock_irq(&ts->lock);
241                 }
242         }
243 }
244
245 static void tsc2046_ts_enable(struct tsc2046 *tsc)
246 {
247         struct tsc2046_ts *ts = tsc->ts;
248
249         if (!ts->disabled)
250                 return;
251
252         ts->disabled = 0;
253         ts->irq_enabled = 1;
254         enable_irq(ts->irq);
255 }
256
257 #ifdef CONFIG_PM
258 static int tsc2046_suspend(struct spi_device *spi, pm_message_t mesg)
259 {
260         struct tsc2046 *tsc = dev_get_drvdata(&spi->dev);
261         struct tsc2046_ts *ts = tsc->ts;
262
263         spin_lock_irq(&ts->lock);
264         tsc2046_ts_disable(tsc);
265         spin_unlock_irq(&ts->lock);
266
267         return 0;
268 }
269
270 static int tsc2046_resume(struct spi_device *spi)
271 {
272         struct tsc2046 *tsc = dev_get_drvdata(&spi->dev);
273         struct tsc2046_ts *ts = tsc->ts;
274
275         spin_lock_irq(&ts->lock);
276         tsc2046_ts_enable(tsc);
277         spin_unlock_irq(&ts->lock);
278
279         return 0;
280 }
281 #endif
282
283 static void tsc2046_ts_setup_spi_xfer(struct tsc2046 *tsc)
284 {
285         struct tsc2046_ts *ts = tsc->ts;
286         struct spi_message *m = &ts->read_msg;
287         struct spi_transfer *x = &ts->read_xfer[1];
288
289         spi_message_init(m);
290
291         /* read and write data in one transaction */
292         x->tx_buf = &tsc2046_ts_cmd_data;
293         x->rx_buf = &ts->data;
294         x->len    = 10;
295         spi_message_add_tail(x, m);
296
297         /* send another START_BYTE to (re)enable pen interrupts */
298         x++;
299         x->tx_buf = &tsc2046_ts_cmd_data[0];
300         x->len = 2;
301         spi_message_add_tail(x, m);
302
303         m->complete = tsc2046_ts_rx;
304         m->context = tsc;
305 }
306
307 static ssize_t tsc2046_ts_pen_down_show(struct device *dev,
308                                         struct device_attribute *attr,
309                                         char *buf)
310 {
311         struct tsc2046 *tsc = dev_get_drvdata(dev);
312
313         return sprintf(buf, "%u\n", is_pen_down(tsc->ts));
314 }
315
316 static DEVICE_ATTR(pen_down, S_IRUGO, tsc2046_ts_pen_down_show, NULL);
317
318 static ssize_t tsc2046_ts_disable_show(struct device *dev,
319                                        struct device_attribute *attr, char *buf)
320 {
321         struct tsc2046          *tsc = dev_get_drvdata(dev);
322         struct tsc2046_ts       *ts = tsc->ts;
323
324         return sprintf(buf, "%u\n", ts->disabled);
325 }
326
327 static ssize_t tsc2046_ts_disable_store(struct device *dev,
328                                         struct device_attribute *attr,
329                                         const char *buf, size_t count)
330 {
331         struct tsc2046          *tsc = dev_get_drvdata(dev);
332         struct tsc2046_ts       *ts = tsc->ts;
333         char *endp;
334         int i;
335
336         i = simple_strtoul(buf, &endp, 10);
337         spin_lock_irq(&ts->lock);
338
339         if (i)
340                 tsc2046_ts_disable(tsc);
341         else
342                 tsc2046_ts_enable(tsc);
343
344         spin_unlock_irq(&ts->lock);
345
346         return count;
347 }
348
349 static DEVICE_ATTR(disable_ts, 0664, tsc2046_ts_disable_show,
350                    tsc2046_ts_disable_store);
351
352 int __devinit tsc2046_ts_init(struct tsc2046 *tsc,
353                               struct tsc2046_platform_data *pdata)
354 {
355         struct tsc2046_ts *ts;
356         struct input_dev *idev;
357         int dav_gpio, r;
358
359         if (pdata->dav_gpio < 0) {
360                 dev_err(&tsc->spi->dev, "need DAV GPIO");
361                 return -EINVAL;
362         }
363         dav_gpio = pdata->dav_gpio;
364
365         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
366         if (ts == NULL)
367                 return -ENOMEM;
368         tsc->ts = ts;
369
370         ts->dav_gpio = dav_gpio;
371 #ifdef CONFIG_ARCH_OMAP
372         r = omap_request_gpio(dav_gpio);
373         if (r < 0) {
374                 dev_err(&tsc->spi->dev, "unable to get DAV GPIO");
375                 goto err1;
376         }
377         omap_set_gpio_direction(dav_gpio, 1);
378         if (pdata->gpio_debounce) {
379                 omap_set_gpio_debounce(tsc->gpio, 1);
380                 omap_set_gpio_debounce_time(tsc->gpio, pdata->gpio_debounce);
381         }
382
383         ts->irq = OMAP_GPIO_IRQ(dav_gpio);
384 #endif
385         setup_timer(&ts->timer, tsc2046_ts_timer, (unsigned long)tsc);
386
387         spin_lock_init(&ts->lock);
388
389         ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
390         ts->max_pressure = pdata->ts_max_pressure ? : MAX_12BIT;
391         ts->touch_pressure = pdata->ts_touch_pressure ? : ts->max_pressure;
392         ts->ignore_last = pdata->ts_ignore_last;
393
394         idev = input_allocate_device();
395         if (idev == NULL) {
396                 r = -ENOMEM;
397                 goto err2;
398         }
399         idev->name = "TSC2046 touchscreen";
400         snprintf(ts->phys, sizeof(ts->phys),
401                  "%s/input-ts", tsc->spi->dev.bus_id);
402         idev->phys = ts->phys;
403
404         idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
405         idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
406         ts->idev = idev;
407
408         tsc2046_ts_setup_spi_xfer(tsc);
409
410         /* These parameters should perhaps be configurable? */
411         input_set_abs_params(idev, ABS_X, 0, 4096, 0, 0);
412         input_set_abs_params(idev, ABS_Y, 0, 4096, 0, 0);
413         input_set_abs_params(idev, ABS_PRESSURE, 0, 1024, 0, 0);
414
415         ts->irq_enabled = 1;
416         r = request_irq(ts->irq, tsc2046_ts_irq_handler,
417                         IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
418                         "tsc2046-ts", tsc);
419         if (r < 0) {
420                 dev_err(&tsc->spi->dev, "unable to get DAV IRQ");
421                 goto err3;
422         }
423         set_irq_wake(ts->irq, 1);
424
425         if (device_create_file(&tsc->spi->dev, &dev_attr_pen_down) < 0)
426                 goto err4;
427         if (device_create_file(&tsc->spi->dev, &dev_attr_disable_ts) < 0)
428                 goto err5;
429
430         r = input_register_device(idev);
431         if (r < 0) {
432                 dev_err(&tsc->spi->dev, "can't register touchscreen device\n");
433                 goto err6;
434         }
435
436         /* kick off a transaction to enable pen interrupts */
437         spi_async(tsc->spi, &ts->read_msg);
438
439         return 0;
440 err6:
441         device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
442 err5:
443         device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
444 err4:
445         free_irq(ts->irq, tsc);
446 err3:
447         input_free_device(idev);
448 err2:
449 #ifdef CONFIG_ARCH_OMAP
450         omap_free_gpio(dav_gpio);
451 #endif
452 err1:
453         kfree(ts);
454         return r;
455 }
456 EXPORT_SYMBOL(tsc2046_ts_init);
457
458 void __devexit tsc2046_ts_exit(struct tsc2046 *tsc)
459 {
460         struct tsc2046_ts *ts = tsc->ts;
461         unsigned long flags;
462
463         spin_lock_irqsave(&ts->lock, flags);
464         tsc2046_ts_disable(tsc);
465         spin_unlock_irqrestore(&ts->lock, flags);
466
467         device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
468         device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
469
470         free_irq(ts->irq, tsc);
471         input_unregister_device(ts->idev);
472
473 #ifdef CONFIG_ARCH_OMAP
474         omap_free_gpio(ts->dav_gpio);
475 #endif
476         kfree(ts);
477 }
478 EXPORT_SYMBOL(tsc2046_ts_exit);
479
480
481 static int __devinit tsc2046_probe(struct spi_device *spi)
482 {
483         struct tsc2046                  *tsc;
484         struct tsc2046_platform_data    *pdata = spi->dev.platform_data;
485         int r = -ENODEV;
486
487         dev_dbg(&spi->dev, "%s\n", __FUNCTION__);
488
489         if (!pdata) {
490                 dev_dbg(&spi->dev, "no platform data?\n");
491                 return -ENODEV;
492         }
493
494         tsc = kzalloc(sizeof(*tsc), GFP_KERNEL);
495         if (tsc == NULL)
496                 return -ENOMEM;
497
498         dev_set_drvdata(&spi->dev, tsc);
499         tsc->spi = spi;
500         spi->dev.power.power_state = PMSG_ON;
501
502         spi->mode = SPI_MODE_0;
503         spi->bits_per_word = 16;
504
505         /*
506          * The max speed might've been defined by the board-specific
507          * struct
508          */
509         if (!spi->max_speed_hz)
510                 spi->max_speed_hz = TSC2046_HZ;
511         spi_setup(spi);
512
513         r = tsc2046_ts_init(tsc, pdata);
514         if (r)
515                 goto err1;
516
517         return 0;
518 err1:
519         kfree(tsc);
520         return r;
521 }
522
523 static int __devexit tsc2046_remove(struct spi_device *spi)
524 {
525         struct tsc2046 *tsc = dev_get_drvdata(&spi->dev);
526
527         dev_dbg(&tsc->spi->dev, "%s\n", __FUNCTION__);
528
529         tsc2046_ts_exit(tsc);
530         kfree(tsc);
531
532         return 0;
533 }
534
535 static struct spi_driver tsc2046_driver = {
536         .driver = {
537                    .name = "tsc2046",
538                    .bus = &spi_bus_type,
539                    .owner = THIS_MODULE,
540         },
541         .probe = tsc2046_probe,
542         .remove = __devexit_p(tsc2046_remove),
543 #ifdef CONFIG_PM
544         .suspend = tsc2046_suspend,
545         .resume = tsc2046_resume,
546 #endif
547 };
548
549 static int __init tsc2046_init(void)
550 {
551         printk("TSC2046 driver initializing\n");
552
553         return spi_register_driver(&tsc2046_driver);
554 }
555 module_init(tsc2046_init);
556
557 static void __exit tsc2046_exit(void)
558 {
559         spi_unregister_driver(&tsc2046_driver);
560 }
561 module_exit(tsc2046_exit);
562
563 MODULE_AUTHOR("Kevin Hilman <khilman@mvista.com>");
564 MODULE_LICENSE("GPL");