2 * TSC2046 Touchscreen driver
4 * Author: Kevin Hilman, MontaVista Software, Inc. <source@mvista.com>
6 * Communication details from original TI driver
7 * Copyright (C) 2004-2005 Texas Instruments, Inc.
9 * Structure based heavily on TSC2301 driver
10 * Copyright (C) 2005-2006 Nokia Corporation
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
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>
26 #ifdef CONFIG_ARCH_OMAP
27 #include <asm/arch/gpio.h>
30 #include <linux/spi/tsc2046.h>
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
39 #define TSC2046_TS_SCAN_TIME 1
40 #define MAX_12BIT ((1 << 12) - 1)
43 struct input_dev *idev;
45 struct timer_list timer;
48 struct spi_transfer read_xfer[3];
49 struct spi_message read_msg;
50 struct spi_message enable_msg;
73 static const u16 tsc2046_ts_cmd_data[] = {
74 START_BYTE, X_CMDWD, Y_CMDWD, Z1_CMDWD, Z2_CMDWD,
77 static int device_suspended(struct device *dev)
79 struct tsc2046 *tsc = dev_get_drvdata(dev);
80 return dev->power.power_state.event != PM_EVENT_ON || tsc->ts->disabled;
83 static void update_pen_state(struct tsc2046 *tsc, int x, int y, int pressure)
85 struct tsc2046_ts *ts = tsc->ts;
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);
93 input_report_key(ts->idev, BTN_TOUCH, 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);
102 input_sync(ts->idev);
104 ts->pen_down = pressure ? 1 : 0;
107 dev_dbg(&tsc->spi->dev, "x %4d y %4d p %4d\n", x, y, pressure);
111 #define CONV_DATA(d1, d2) \
112 (((d1 & 0x7f) << 5) | ((d2 >> 11) & 0x1f))
115 * This procedure is called by the SPI framework after the coordinates
116 * have been read from TSC2046
118 static void tsc2046_ts_rx(void *arg)
120 struct tsc2046 *tsc = arg;
121 struct tsc2046_ts *ts = tsc->ts;
122 unsigned int x, y, z1, z2, pressure;
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);
130 pressure = ts->x_plate_ohm * x;
138 * If pressure value is above a preset limit (pen is barely
139 * touching the screen) we can't trust the coordinate values.
141 if (pressure < ts->pressure_limit && x < MAX_12BIT && y < MAX_12BIT) {
142 ts->pressure_limit = ts->max_pressure;
143 if (ts->ignore_last) {
145 update_pen_state(tsc, ts->x, ts->y, ts->p);
150 update_pen_state(tsc, x, y, pressure);
154 mod_timer(&ts->timer,
155 jiffies + msecs_to_jiffies(TSC2046_TS_SCAN_TIME));
158 static int is_pen_down(struct tsc2046_ts *ts)
164 * Timer is called every TSC2046_TS_SCAN_TIME when the pen is down
166 static void tsc2046_ts_timer(unsigned long arg)
168 struct tsc2046 *tsc = (void *) arg;
169 struct tsc2046_ts *ts = tsc->ts;
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) &&
183 update_pen_state(tsc, 0, 0, 0);
185 spin_unlock_irqrestore(&ts->lock, flags);
189 spin_unlock_irqrestore(&ts->lock, flags);
191 r = spi_async(tsc->spi, &ts->read_msg);
193 dev_err(&tsc->spi->dev, "ts: spi_async() failed");
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
203 static irqreturn_t tsc2046_ts_irq_handler(int irq, void *dev_id)
205 struct tsc2046 *tsc = dev_id;
206 struct tsc2046_ts *ts = tsc->ts;
209 spin_lock_irqsave(&ts->lock, flags);
210 if (ts->irq_enabled) {
212 disable_irq(ts->irq);
214 ts->pressure_limit = ts->touch_pressure;
216 mod_timer(&ts->timer,
217 jiffies + msecs_to_jiffies(TSC2046_TS_SCAN_TIME));
219 spin_unlock_irqrestore(&ts->lock, flags);
224 /* Must be called with ts->lock held */
225 static void tsc2046_ts_disable(struct tsc2046 *tsc)
227 struct tsc2046_ts *ts = tsc->ts;
235 disable_irq(ts->irq);
237 while (ts->pending) {
238 spin_unlock_irq(&ts->lock);
240 spin_lock_irq(&ts->lock);
245 static void tsc2046_ts_enable(struct tsc2046 *tsc)
247 struct tsc2046_ts *ts = tsc->ts;
258 static int tsc2046_suspend(struct spi_device *spi, pm_message_t mesg)
260 struct tsc2046 *tsc = dev_get_drvdata(&spi->dev);
261 struct tsc2046_ts *ts = tsc->ts;
263 spin_lock_irq(&ts->lock);
264 tsc2046_ts_disable(tsc);
265 spin_unlock_irq(&ts->lock);
270 static int tsc2046_resume(struct spi_device *spi)
272 struct tsc2046 *tsc = dev_get_drvdata(&spi->dev);
273 struct tsc2046_ts *ts = tsc->ts;
275 spin_lock_irq(&ts->lock);
276 tsc2046_ts_enable(tsc);
277 spin_unlock_irq(&ts->lock);
283 static void tsc2046_ts_setup_spi_xfer(struct tsc2046 *tsc)
285 struct tsc2046_ts *ts = tsc->ts;
286 struct spi_message *m = &ts->read_msg;
287 struct spi_transfer *x = &ts->read_xfer[1];
291 /* read and write data in one transaction */
292 x->tx_buf = &tsc2046_ts_cmd_data;
293 x->rx_buf = &ts->data;
295 spi_message_add_tail(x, m);
297 /* send another START_BYTE to (re)enable pen interrupts */
299 x->tx_buf = &tsc2046_ts_cmd_data[0];
301 spi_message_add_tail(x, m);
303 m->complete = tsc2046_ts_rx;
307 static ssize_t tsc2046_ts_pen_down_show(struct device *dev,
308 struct device_attribute *attr,
311 struct tsc2046 *tsc = dev_get_drvdata(dev);
313 return sprintf(buf, "%u\n", is_pen_down(tsc->ts));
316 static DEVICE_ATTR(pen_down, S_IRUGO, tsc2046_ts_pen_down_show, NULL);
318 static ssize_t tsc2046_ts_disable_show(struct device *dev,
319 struct device_attribute *attr, char *buf)
321 struct tsc2046 *tsc = dev_get_drvdata(dev);
322 struct tsc2046_ts *ts = tsc->ts;
324 return sprintf(buf, "%u\n", ts->disabled);
327 static ssize_t tsc2046_ts_disable_store(struct device *dev,
328 struct device_attribute *attr,
329 const char *buf, size_t count)
331 struct tsc2046 *tsc = dev_get_drvdata(dev);
332 struct tsc2046_ts *ts = tsc->ts;
336 i = simple_strtoul(buf, &endp, 10);
337 spin_lock_irq(&ts->lock);
340 tsc2046_ts_disable(tsc);
342 tsc2046_ts_enable(tsc);
344 spin_unlock_irq(&ts->lock);
349 static DEVICE_ATTR(disable_ts, 0664, tsc2046_ts_disable_show,
350 tsc2046_ts_disable_store);
352 int __devinit tsc2046_ts_init(struct tsc2046 *tsc,
353 struct tsc2046_platform_data *pdata)
355 struct tsc2046_ts *ts;
356 struct input_dev *idev;
359 if (pdata->dav_gpio < 0) {
360 dev_err(&tsc->spi->dev, "need DAV GPIO");
363 dav_gpio = pdata->dav_gpio;
365 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
370 ts->dav_gpio = dav_gpio;
371 #ifdef CONFIG_ARCH_OMAP
372 r = omap_request_gpio(dav_gpio);
374 dev_err(&tsc->spi->dev, "unable to get DAV GPIO");
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);
383 ts->irq = OMAP_GPIO_IRQ(dav_gpio);
385 setup_timer(&ts->timer, tsc2046_ts_timer, (unsigned long)tsc);
387 spin_lock_init(&ts->lock);
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;
394 idev = input_allocate_device();
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;
404 idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
405 idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
408 tsc2046_ts_setup_spi_xfer(tsc);
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);
416 r = request_irq(ts->irq, tsc2046_ts_irq_handler,
417 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
420 dev_err(&tsc->spi->dev, "unable to get DAV IRQ");
423 set_irq_wake(ts->irq, 1);
425 if (device_create_file(&tsc->spi->dev, &dev_attr_pen_down) < 0)
427 if (device_create_file(&tsc->spi->dev, &dev_attr_disable_ts) < 0)
430 r = input_register_device(idev);
432 dev_err(&tsc->spi->dev, "can't register touchscreen device\n");
436 /* kick off a transaction to enable pen interrupts */
437 spi_async(tsc->spi, &ts->read_msg);
441 device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
443 device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
445 free_irq(ts->irq, tsc);
447 input_free_device(idev);
449 #ifdef CONFIG_ARCH_OMAP
450 omap_free_gpio(dav_gpio);
456 EXPORT_SYMBOL(tsc2046_ts_init);
458 void __devexit tsc2046_ts_exit(struct tsc2046 *tsc)
460 struct tsc2046_ts *ts = tsc->ts;
463 spin_lock_irqsave(&ts->lock, flags);
464 tsc2046_ts_disable(tsc);
465 spin_unlock_irqrestore(&ts->lock, flags);
467 device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
468 device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
470 free_irq(ts->irq, tsc);
471 input_unregister_device(ts->idev);
473 #ifdef CONFIG_ARCH_OMAP
474 omap_free_gpio(ts->dav_gpio);
478 EXPORT_SYMBOL(tsc2046_ts_exit);
481 static int __devinit tsc2046_probe(struct spi_device *spi)
484 struct tsc2046_platform_data *pdata = spi->dev.platform_data;
487 dev_dbg(&spi->dev, "%s\n", __FUNCTION__);
490 dev_dbg(&spi->dev, "no platform data?\n");
494 tsc = kzalloc(sizeof(*tsc), GFP_KERNEL);
498 dev_set_drvdata(&spi->dev, tsc);
500 spi->dev.power.power_state = PMSG_ON;
502 spi->mode = SPI_MODE_0;
503 spi->bits_per_word = 16;
506 * The max speed might've been defined by the board-specific
509 if (!spi->max_speed_hz)
510 spi->max_speed_hz = TSC2046_HZ;
513 r = tsc2046_ts_init(tsc, pdata);
523 static int __devexit tsc2046_remove(struct spi_device *spi)
525 struct tsc2046 *tsc = dev_get_drvdata(&spi->dev);
527 dev_dbg(&tsc->spi->dev, "%s\n", __FUNCTION__);
529 tsc2046_ts_exit(tsc);
535 static struct spi_driver tsc2046_driver = {
538 .bus = &spi_bus_type,
539 .owner = THIS_MODULE,
541 .probe = tsc2046_probe,
542 .remove = __devexit_p(tsc2046_remove),
544 .suspend = tsc2046_suspend,
545 .resume = tsc2046_resume,
549 static int __init tsc2046_init(void)
551 printk("TSC2046 driver initializing\n");
553 return spi_register_driver(&tsc2046_driver);
555 module_init(tsc2046_init);
557 static void __exit tsc2046_exit(void)
559 spi_unregister_driver(&tsc2046_driver);
561 module_exit(tsc2046_exit);
563 MODULE_AUTHOR("Kevin Hilman <khilman@mvista.com>");
564 MODULE_LICENSE("GPL");