]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/touchscreen/tsc2046_ts.c
SPI: TSC2046 touchscreen support
[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         /* If pressure value is above a preset limit (pen is barely
138          * touching the screen) we can't trust the coordinate values.
139          */
140         if (pressure < ts->pressure_limit && x < MAX_12BIT && y < MAX_12BIT) {
141                 ts->pressure_limit = ts->max_pressure;
142                 if (ts->ignore_last) {
143                         if (ts->sample_cnt)
144                                 update_pen_state(tsc, ts->x, ts->y, ts->p);
145                         ts->x = x;
146                         ts->y = y;
147                         ts->p = pressure;
148                 } else
149                         update_pen_state(tsc, x, y, pressure);
150                 ts->sample_cnt++;
151         }
152
153         mod_timer(&ts->timer,
154                   jiffies + msecs_to_jiffies(TSC2046_TS_SCAN_TIME));
155 }
156
157 static int is_pen_down(struct tsc2046_ts *ts)
158 {
159         return ts->pen_down;
160 }
161
162 /*
163  * Timer is called every TSC2046_TS_SCAN_TIME when the pen is down
164  */
165 static void tsc2046_ts_timer(unsigned long arg)
166 {
167         struct tsc2046 *tsc = (void *) arg;
168         struct tsc2046_ts *ts = tsc->ts;
169         unsigned long flags;
170         int ndav;
171         int r;
172
173         spin_lock_irqsave(&ts->lock, flags);
174         ndav = omap_get_gpio_datain(ts->dav_gpio);
175         if (ndav || device_suspended(&tsc->spi->dev)) {
176                 /* Pen has been lifted */
177                 if (!device_suspended(&tsc->spi->dev) &&
178                     !ts->irq_enabled) {
179                         ts->irq_enabled = 1;
180                         enable_irq(ts->irq);
181                 }
182                 update_pen_state(tsc, 0, 0, 0);
183                 ts->pending = 0;
184                 spin_unlock_irqrestore(&ts->lock, flags);
185
186         } else {
187                 ts->pen_down = 1;
188                 spin_unlock_irqrestore(&ts->lock, flags);
189
190                 r = spi_async(tsc->spi, &ts->read_msg);
191                 if (r)
192                         dev_err(&tsc->spi->dev, "ts: spi_async() failed");
193         }
194 }
195
196 /*
197  * This interrupt is called when pen is down and first coordinates are
198  * available. That is indicated by a falling edge on DEV line.  IRQ is
199  * disabled here because while the pen is down the coordinates are
200  * read by a timer.
201  */
202 static irqreturn_t tsc2046_ts_irq_handler(int irq, void *dev_id)
203 {
204         struct tsc2046 *tsc = dev_id;
205         struct tsc2046_ts *ts = tsc->ts;
206         unsigned long flags;
207
208         spin_lock_irqsave(&ts->lock, flags);
209         if (ts->irq_enabled) {
210                 ts->irq_enabled = 0;
211                 disable_irq(ts->irq);
212                 ts->pending = 1;
213                 ts->pressure_limit = ts->touch_pressure;
214                 ts->sample_cnt = 0;
215                 mod_timer(&ts->timer,
216                           jiffies + msecs_to_jiffies(TSC2046_TS_SCAN_TIME));
217         }
218         spin_unlock_irqrestore(&ts->lock, flags);
219
220         return IRQ_HANDLED;
221 }
222
223 /* Must be called with ts->lock held */
224 static void tsc2046_ts_disable(struct tsc2046 *tsc)
225 {
226         struct tsc2046_ts *ts = tsc->ts;
227
228         if (ts->disabled)
229                 return;
230
231         ts->disabled = 1;
232         if (!ts->pending) {
233                 ts->irq_enabled = 0;
234                 disable_irq(ts->irq);
235         } else {
236                 while (ts->pending) {
237                         spin_unlock_irq(&ts->lock);
238                         msleep(1);
239                         spin_lock_irq(&ts->lock);
240                 }
241         }
242 }
243
244 static void tsc2046_ts_enable(struct tsc2046 *tsc)
245 {
246         struct tsc2046_ts *ts = tsc->ts;
247
248         if (!ts->disabled)
249                 return;
250
251         ts->disabled = 0;
252         ts->irq_enabled = 1;
253         enable_irq(ts->irq);
254 }
255
256 #ifdef CONFIG_PM
257 int tsc2046_ts_suspend(struct tsc2046 *tsc)
258 {
259         struct tsc2046_ts *ts = tsc->ts;
260
261         spin_lock_irq(&ts->lock);
262         tsc2046_ts_disable(tsc);
263         spin_unlock_irq(&ts->lock);
264
265         return 0;
266 }
267
268 void tsc2046_ts_resume(struct tsc2046 *tsc)
269 {
270         struct tsc2046_ts *ts = tsc->ts;
271
272         spin_lock_irq(&ts->lock);
273         tsc2046_ts_enable(tsc);
274         spin_unlock_irq(&ts->lock);
275 }
276 #endif
277
278 static void tsc2046_ts_setup_spi_xfer(struct tsc2046 *tsc)
279 {
280         struct tsc2046_ts *ts = tsc->ts;
281         struct spi_message *m = &ts->read_msg;
282         struct spi_transfer *x = &ts->read_xfer[1];
283
284         spi_message_init(m);
285
286         /* read and write data in one transaction */
287         x->tx_buf = &tsc2046_ts_cmd_data;
288         x->rx_buf = &ts->data;
289         x->len    = 10;
290         spi_message_add_tail(x, m);
291
292         /* send another START_BYTE to (re)enable pen interrupts */
293         x++;
294         x->tx_buf = &tsc2046_ts_cmd_data[0];
295         x->len = 2;
296         spi_message_add_tail(x, m);
297
298         m->complete = tsc2046_ts_rx;
299         m->context = tsc;
300 }
301
302 static ssize_t tsc2046_ts_pen_down_show(struct device *dev,
303                                         struct device_attribute *attr,
304                                         char *buf)
305 {
306         struct tsc2046 *tsc = dev_get_drvdata(dev);
307
308         return sprintf(buf, "%u\n", is_pen_down(tsc->ts));
309 }
310
311 static DEVICE_ATTR(pen_down, S_IRUGO, tsc2046_ts_pen_down_show, NULL);
312
313 static ssize_t tsc2046_ts_disable_show(struct device *dev,
314                                        struct device_attribute *attr, char *buf)
315 {
316         struct tsc2046          *tsc = dev_get_drvdata(dev);
317         struct tsc2046_ts       *ts = tsc->ts;
318
319         return sprintf(buf, "%u\n", ts->disabled);
320 }
321
322 static ssize_t tsc2046_ts_disable_store(struct device *dev,
323                                         struct device_attribute *attr,
324                                         const char *buf, size_t count)
325 {
326         struct tsc2046          *tsc = dev_get_drvdata(dev);
327         struct tsc2046_ts       *ts = tsc->ts;
328         char *endp;
329         int i;
330
331         i = simple_strtoul(buf, &endp, 10);
332         spin_lock_irq(&ts->lock);
333
334         if (i)
335                 tsc2046_ts_disable(tsc);
336         else
337                 tsc2046_ts_enable(tsc);
338
339         spin_unlock_irq(&ts->lock);
340
341         return count;
342 }
343
344 static DEVICE_ATTR(disable_ts, 0664, tsc2046_ts_disable_show,
345                    tsc2046_ts_disable_store);
346
347 int __devinit tsc2046_ts_init(struct tsc2046 *tsc,
348                               struct tsc2046_platform_data *pdata)
349 {
350         struct tsc2046_ts *ts;
351         struct input_dev *idev;
352         int dav_gpio, r;
353
354         if (pdata->dav_gpio < 0) {
355                 dev_err(&tsc->spi->dev, "need DAV GPIO");
356                 return -EINVAL;
357         }
358         dav_gpio = pdata->dav_gpio;
359
360         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
361         if (ts == NULL)
362                 return -ENOMEM;
363         tsc->ts = ts;
364
365         ts->dav_gpio = dav_gpio;
366 #ifdef CONFIG_ARCH_OMAP
367         r = omap_request_gpio(dav_gpio);
368         if (r < 0) {
369                 dev_err(&tsc->spi->dev, "unable to get DAV GPIO");
370                 goto err1;
371         }
372         omap_set_gpio_direction(dav_gpio, 1);
373         if (pdata->gpio_debounce) {
374                 omap_set_gpio_debounce(tsc->gpio, 1);
375                 omap_set_gpio_debounce_time(tsc->gpio, pdata->gpio_debounce);
376         }
377
378         ts->irq = OMAP_GPIO_IRQ(dav_gpio);
379 #endif
380         init_timer(&ts->timer);
381         ts->timer.data = (unsigned long) tsc;
382         ts->timer.function = tsc2046_ts_timer;
383
384         spin_lock_init(&ts->lock);
385
386         ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
387         ts->max_pressure= pdata->ts_max_pressure ? : MAX_12BIT;
388         ts->touch_pressure = pdata->ts_touch_pressure ? : ts->max_pressure;
389         ts->ignore_last = pdata->ts_ignore_last;
390
391         idev = input_allocate_device();
392         if (idev == NULL) {
393                 r = -ENOMEM;
394                 goto err2;
395         }
396         idev->name = "TSC2046 touchscreen";
397         snprintf(ts->phys, sizeof(ts->phys),
398                  "%s/input-ts", tsc->spi->dev.bus_id);
399         idev->phys = ts->phys;
400
401         idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
402         idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
403         ts->idev = idev;
404
405         tsc2046_ts_setup_spi_xfer(tsc);
406
407         /* These parameters should perhaps be configurable? */
408         input_set_abs_params(idev, ABS_X, 0, 4096, 0, 0);
409         input_set_abs_params(idev, ABS_Y, 0, 4096, 0, 0);
410         input_set_abs_params(idev, ABS_PRESSURE, 0, 1024, 0, 0);
411
412         ts->irq_enabled = 1;
413         r = request_irq(ts->irq, tsc2046_ts_irq_handler,
414                         SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
415                         "tsc2046-ts", tsc);
416         if (r < 0) {
417                 dev_err(&tsc->spi->dev, "unable to get DAV IRQ");
418                 goto err3;
419         }
420         set_irq_wake(ts->irq, 1);
421
422         if (device_create_file(&tsc->spi->dev, &dev_attr_pen_down) < 0)
423                 goto err4;
424         if (device_create_file(&tsc->spi->dev, &dev_attr_disable_ts) < 0)
425                 goto err5;
426
427         r = input_register_device(idev);
428         if (r < 0) {
429                 dev_err(&tsc->spi->dev, "can't register touchscreen device\n");
430                 goto err6;
431         }
432
433         /* kick off a transaction to enable pen interrupts */
434         spi_async(tsc->spi, &ts->read_msg);
435
436         return 0;
437 err6:
438         device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
439 err5:
440         device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
441 err4:
442         free_irq(ts->irq, tsc);
443 err3:
444         input_free_device(idev);
445 err2:
446 #ifdef CONFIG_ARCH_OMAP
447         omap_free_gpio(dav_gpio);
448 #endif
449  err1:
450         kfree(ts);
451         return r;
452 }
453 EXPORT_SYMBOL(tsc2046_ts_init);
454
455 void __devexit tsc2046_ts_exit(struct tsc2046 *tsc)
456 {
457         struct tsc2046_ts *ts = tsc->ts;
458         unsigned long flags;
459
460         spin_lock_irqsave(&ts->lock, flags);
461         tsc2046_ts_disable(tsc);
462         spin_unlock_irqrestore(&ts->lock, flags);
463
464         device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
465         device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
466
467         free_irq(ts->irq, tsc);
468         input_unregister_device(ts->idev);
469
470 #ifdef CONFIG_ARCH_OMAP
471         omap_free_gpio(ts->dav_gpio);
472 #endif
473         kfree(ts);
474 }
475 EXPORT_SYMBOL(tsc2046_ts_exit);
476
477
478 static int __devinit tsc2046_probe(struct spi_device *spi)
479 {
480         struct tsc2046                  *tsc;
481         struct tsc2046_platform_data    *pdata = spi->dev.platform_data;
482         int r = -ENODEV;
483
484         dev_dbg(&spi->dev, "%s\n", __FUNCTION__);
485
486         if (!pdata) {
487                 dev_dbg(&spi->dev, "no platform data?\n");
488                 return -ENODEV;
489         }
490
491         tsc = kzalloc(sizeof(*tsc), GFP_KERNEL);
492         if (tsc == NULL)
493                 return -ENOMEM;
494
495         dev_set_drvdata(&spi->dev, tsc);
496         tsc->spi = spi;
497         spi->dev.power.power_state = PMSG_ON;
498
499         spi->mode = SPI_MODE_1;
500         spi->bits_per_word = 16;
501
502         /* The max speed might've been defined by the board-specific
503          * struct */
504         if (!spi->max_speed_hz)
505                 spi->max_speed_hz = TSC2046_HZ;
506         spi_setup(spi);
507
508         r = tsc2046_ts_init(tsc, pdata);
509         if (r)
510                 goto err1;
511
512         return 0;
513  err1:
514         kfree(tsc);
515         return r;
516 }
517
518 static int __devexit tsc2046_remove(struct spi_device *spi)
519 {
520         struct tsc2046 *tsc = dev_get_drvdata(&spi->dev);
521
522         dev_dbg(&tsc->spi->dev, "%s\n", __FUNCTION__);
523
524         tsc2046_ts_exit(tsc);
525         kfree(tsc);
526
527         return 0;
528 }
529
530 static struct spi_driver tsc2046_driver = {
531         .driver = {
532                    .name = "tsc2046",
533                    .bus = &spi_bus_type,
534                    .owner = THIS_MODULE,
535         },
536         .probe = tsc2046_probe,
537         .remove = __devexit_p(tsc2046_remove),
538 };
539
540 static int __init tsc2046_init(void)
541 {
542         printk("TSC2046 driver initializing\n");
543
544         return spi_register_driver(&tsc2046_driver);
545 }
546 module_init(tsc2046_init);
547
548 static void __exit tsc2046_exit(void)
549 {
550         spi_unregister_driver(&tsc2046_driver);
551 }
552 module_exit(tsc2046_exit);
553
554 MODULE_AUTHOR("Kevin Hilman <khilman@mvista.com>");
555 MODULE_LICENSE("GPL");