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;
137 /* If pressure value is above a preset limit (pen is barely
138 * touching the screen) we can't trust the coordinate values.
140 if (pressure < ts->pressure_limit && x < MAX_12BIT && y < MAX_12BIT) {
141 ts->pressure_limit = ts->max_pressure;
142 if (ts->ignore_last) {
144 update_pen_state(tsc, ts->x, ts->y, ts->p);
149 update_pen_state(tsc, x, y, pressure);
153 mod_timer(&ts->timer,
154 jiffies + msecs_to_jiffies(TSC2046_TS_SCAN_TIME));
157 static int is_pen_down(struct tsc2046_ts *ts)
163 * Timer is called every TSC2046_TS_SCAN_TIME when the pen is down
165 static void tsc2046_ts_timer(unsigned long arg)
167 struct tsc2046 *tsc = (void *) arg;
168 struct tsc2046_ts *ts = tsc->ts;
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) &&
182 update_pen_state(tsc, 0, 0, 0);
184 spin_unlock_irqrestore(&ts->lock, flags);
188 spin_unlock_irqrestore(&ts->lock, flags);
190 r = spi_async(tsc->spi, &ts->read_msg);
192 dev_err(&tsc->spi->dev, "ts: spi_async() failed");
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
202 static irqreturn_t tsc2046_ts_irq_handler(int irq, void *dev_id)
204 struct tsc2046 *tsc = dev_id;
205 struct tsc2046_ts *ts = tsc->ts;
208 spin_lock_irqsave(&ts->lock, flags);
209 if (ts->irq_enabled) {
211 disable_irq(ts->irq);
213 ts->pressure_limit = ts->touch_pressure;
215 mod_timer(&ts->timer,
216 jiffies + msecs_to_jiffies(TSC2046_TS_SCAN_TIME));
218 spin_unlock_irqrestore(&ts->lock, flags);
223 /* Must be called with ts->lock held */
224 static void tsc2046_ts_disable(struct tsc2046 *tsc)
226 struct tsc2046_ts *ts = tsc->ts;
234 disable_irq(ts->irq);
236 while (ts->pending) {
237 spin_unlock_irq(&ts->lock);
239 spin_lock_irq(&ts->lock);
244 static void tsc2046_ts_enable(struct tsc2046 *tsc)
246 struct tsc2046_ts *ts = tsc->ts;
257 int tsc2046_ts_suspend(struct tsc2046 *tsc)
259 struct tsc2046_ts *ts = tsc->ts;
261 spin_lock_irq(&ts->lock);
262 tsc2046_ts_disable(tsc);
263 spin_unlock_irq(&ts->lock);
268 void tsc2046_ts_resume(struct tsc2046 *tsc)
270 struct tsc2046_ts *ts = tsc->ts;
272 spin_lock_irq(&ts->lock);
273 tsc2046_ts_enable(tsc);
274 spin_unlock_irq(&ts->lock);
278 static void tsc2046_ts_setup_spi_xfer(struct tsc2046 *tsc)
280 struct tsc2046_ts *ts = tsc->ts;
281 struct spi_message *m = &ts->read_msg;
282 struct spi_transfer *x = &ts->read_xfer[1];
286 /* read and write data in one transaction */
287 x->tx_buf = &tsc2046_ts_cmd_data;
288 x->rx_buf = &ts->data;
290 spi_message_add_tail(x, m);
292 /* send another START_BYTE to (re)enable pen interrupts */
294 x->tx_buf = &tsc2046_ts_cmd_data[0];
296 spi_message_add_tail(x, m);
298 m->complete = tsc2046_ts_rx;
302 static ssize_t tsc2046_ts_pen_down_show(struct device *dev,
303 struct device_attribute *attr,
306 struct tsc2046 *tsc = dev_get_drvdata(dev);
308 return sprintf(buf, "%u\n", is_pen_down(tsc->ts));
311 static DEVICE_ATTR(pen_down, S_IRUGO, tsc2046_ts_pen_down_show, NULL);
313 static ssize_t tsc2046_ts_disable_show(struct device *dev,
314 struct device_attribute *attr, char *buf)
316 struct tsc2046 *tsc = dev_get_drvdata(dev);
317 struct tsc2046_ts *ts = tsc->ts;
319 return sprintf(buf, "%u\n", ts->disabled);
322 static ssize_t tsc2046_ts_disable_store(struct device *dev,
323 struct device_attribute *attr,
324 const char *buf, size_t count)
326 struct tsc2046 *tsc = dev_get_drvdata(dev);
327 struct tsc2046_ts *ts = tsc->ts;
331 i = simple_strtoul(buf, &endp, 10);
332 spin_lock_irq(&ts->lock);
335 tsc2046_ts_disable(tsc);
337 tsc2046_ts_enable(tsc);
339 spin_unlock_irq(&ts->lock);
344 static DEVICE_ATTR(disable_ts, 0664, tsc2046_ts_disable_show,
345 tsc2046_ts_disable_store);
347 int __devinit tsc2046_ts_init(struct tsc2046 *tsc,
348 struct tsc2046_platform_data *pdata)
350 struct tsc2046_ts *ts;
351 struct input_dev *idev;
354 if (pdata->dav_gpio < 0) {
355 dev_err(&tsc->spi->dev, "need DAV GPIO");
358 dav_gpio = pdata->dav_gpio;
360 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
365 ts->dav_gpio = dav_gpio;
366 #ifdef CONFIG_ARCH_OMAP
367 r = omap_request_gpio(dav_gpio);
369 dev_err(&tsc->spi->dev, "unable to get DAV GPIO");
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);
378 ts->irq = OMAP_GPIO_IRQ(dav_gpio);
380 init_timer(&ts->timer);
381 ts->timer.data = (unsigned long) tsc;
382 ts->timer.function = tsc2046_ts_timer;
384 spin_lock_init(&ts->lock);
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;
391 idev = input_allocate_device();
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;
401 idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
402 idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
405 tsc2046_ts_setup_spi_xfer(tsc);
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);
413 r = request_irq(ts->irq, tsc2046_ts_irq_handler,
414 SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
417 dev_err(&tsc->spi->dev, "unable to get DAV IRQ");
420 set_irq_wake(ts->irq, 1);
422 if (device_create_file(&tsc->spi->dev, &dev_attr_pen_down) < 0)
424 if (device_create_file(&tsc->spi->dev, &dev_attr_disable_ts) < 0)
427 r = input_register_device(idev);
429 dev_err(&tsc->spi->dev, "can't register touchscreen device\n");
433 /* kick off a transaction to enable pen interrupts */
434 spi_async(tsc->spi, &ts->read_msg);
438 device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
440 device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
442 free_irq(ts->irq, tsc);
444 input_free_device(idev);
446 #ifdef CONFIG_ARCH_OMAP
447 omap_free_gpio(dav_gpio);
453 EXPORT_SYMBOL(tsc2046_ts_init);
455 void __devexit tsc2046_ts_exit(struct tsc2046 *tsc)
457 struct tsc2046_ts *ts = tsc->ts;
460 spin_lock_irqsave(&ts->lock, flags);
461 tsc2046_ts_disable(tsc);
462 spin_unlock_irqrestore(&ts->lock, flags);
464 device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
465 device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
467 free_irq(ts->irq, tsc);
468 input_unregister_device(ts->idev);
470 #ifdef CONFIG_ARCH_OMAP
471 omap_free_gpio(ts->dav_gpio);
475 EXPORT_SYMBOL(tsc2046_ts_exit);
478 static int __devinit tsc2046_probe(struct spi_device *spi)
481 struct tsc2046_platform_data *pdata = spi->dev.platform_data;
484 dev_dbg(&spi->dev, "%s\n", __FUNCTION__);
487 dev_dbg(&spi->dev, "no platform data?\n");
491 tsc = kzalloc(sizeof(*tsc), GFP_KERNEL);
495 dev_set_drvdata(&spi->dev, tsc);
497 spi->dev.power.power_state = PMSG_ON;
499 spi->mode = SPI_MODE_1;
500 spi->bits_per_word = 16;
502 /* The max speed might've been defined by the board-specific
504 if (!spi->max_speed_hz)
505 spi->max_speed_hz = TSC2046_HZ;
508 r = tsc2046_ts_init(tsc, pdata);
518 static int __devexit tsc2046_remove(struct spi_device *spi)
520 struct tsc2046 *tsc = dev_get_drvdata(&spi->dev);
522 dev_dbg(&tsc->spi->dev, "%s\n", __FUNCTION__);
524 tsc2046_ts_exit(tsc);
530 static struct spi_driver tsc2046_driver = {
533 .bus = &spi_bus_type,
534 .owner = THIS_MODULE,
536 .probe = tsc2046_probe,
537 .remove = __devexit_p(tsc2046_remove),
540 static int __init tsc2046_init(void)
542 printk("TSC2046 driver initializing\n");
544 return spi_register_driver(&tsc2046_driver);
546 module_init(tsc2046_init);
548 static void __exit tsc2046_exit(void)
550 spi_unregister_driver(&tsc2046_driver);
552 module_exit(tsc2046_exit);
554 MODULE_AUTHOR("Kevin Hilman <khilman@mvista.com>");
555 MODULE_LICENSE("GPL");