2 * TSC2301 touchscreen driver
4 * Copyright (C) 2005-2006 Nokia Corporation
6 * Written by Jarkko Oikarinen, Imre Deak and Juha Yrjola
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/input.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/spi/spi.h>
31 #ifdef CONFIG_ARCH_OMAP
32 #include <asm/arch/gpio.h>
35 #include <linux/spi/tsc2301.h>
38 * The touchscreen interface operates as follows:
41 * Request access to GPIO103 (DAV)
42 * tsc2301_dav_irq_handler will trigger when DAV line goes down
44 * 1) Pen is pressed against touchscreeen
45 * 2) TSC2301 performs AD conversion
46 * 3) After the conversion is done TSC2301 drives DAV line down
47 * 4) GPIO IRQ is received and tsc2301_dav_irq_handler is called
48 * 5) tsc2301_dav_irq_handler sets up tsc2301_ts_timer in TSC2301_TS_SCAN_TIME
49 * 6) tsc2301_ts_timer disables the irq and requests spi driver
50 * to read X, Y, Z1 and Z2
51 * 7) SPI framework calls tsc2301_ts_rx after the coordinates are read
52 * 8) tsc2301_ts_rx reports coordinates to input layer and
53 * sets up tsc2301_ts_timer to be called after TSC2301_TS_SCAN_TIME
54 * 9) if tsc2301_tx_timer notices that the pen has been lifted, the lift event
55 * is sent, and irq is again enabled.
59 #define TSC2301_TOUCHSCREEN_PRODUCT_ID 0x0052
60 #define TSC2301_TOUCHSCREEN_PRODUCT_VERSION 0x0001
62 #define TSC2301_TS_SCAN_TIME 1
64 #define TSC2301_ADCREG_CONVERSION_CTRL_BY_TSC2301 0x8000
65 #define TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST 0x0000
67 #define TSC2301_ADCREG_FUNCTION_NONE 0x0000
68 #define TSC2301_ADCREG_FUNCTION_XY 0x0400
69 #define TSC2301_ADCREG_FUNCTION_XYZ 0x0800
70 #define TSC2301_ADCREG_FUNCTION_X 0x0C00
71 #define TSC2301_ADCREG_FUNCTION_Y 0x1000
72 #define TSC2301_ADCREG_FUNCTION_Z 0x1400
73 #define TSC2301_ADCREG_FUNCTION_DAT1 0x1800
74 #define TSC2301_ADCREG_FUNCTION_DAT2 0x1C00
75 #define TSC2301_ADCREG_FUNCTION_AUX1 0x2000
76 #define TSC2301_ADCREG_FUNCTION_AUX2 0x2400
77 #define TSC2301_ADCREG_FUNCTION_TEMP 0x2800
79 #define TSC2301_ADCREG_RESOLUTION_8BIT 0x0100
80 #define TSC2301_ADCREG_RESOLUTION_10BIT 0x0200
81 #define TSC2301_ADCREG_RESOLUTION_12BIT 0x0300
83 #define TSC2301_ADCREG_AVERAGING_NONE 0x0000
84 #define TSC2301_ADCREG_AVERAGING_4AVG 0x0040
85 #define TSC2301_ADCREG_AVERAGING_8AVG 0x0080
86 #define TSC2301_ADCREG_AVERAGING_16AVG 0x00C0
88 #define TSC2301_ADCREG_CLOCK_8MHZ 0x0000
89 #define TSC2301_ADCREG_CLOCK_4MHZ 0x0010
90 #define TSC2301_ADCREG_CLOCK_2MHZ 0x0020
91 #define TSC2301_ADCREG_CLOCK_1MHZ 0x0030
93 #define TSC2301_ADCREG_VOLTAGE_STAB_0US 0x0000
94 #define TSC2301_ADCREG_VOLTAGE_STAB_100US 0x0002
95 #define TSC2301_ADCREG_VOLTAGE_STAB_500US 0x0004
96 #define TSC2301_ADCREG_VOLTAGE_STAB_1MS 0x0006
97 #define TSC2301_ADCREG_VOLTAGE_STAB_5MS 0x0008
98 #define TSC2301_ADCREG_VOLTAGE_STAB_10MS 0x000A
99 #define TSC2301_ADCREG_VOLTAGE_STAB_50MS 0x000C
100 #define TSC2301_ADCREG_VOLTAGE_STAB_100MS 0x000E
102 #define TSC2301_ADCREG_STOP_CONVERSION 0x4000
104 #define MAX_12BIT ((1 << 12) - 1)
107 struct input_dev *idev;
109 struct timer_list timer;
112 struct spi_transfer read_xfer[2];
113 struct spi_message read_msg;
141 static const u16 tsc2301_ts_read_data = 0x8000 | TSC2301_REG_X;
143 static int tsc2301_ts_check_config(struct tsc2301_ts *ts, int *hw_flags)
148 switch (ts->hw_avg_max) {
150 flags |= TSC2301_ADCREG_AVERAGING_NONE;
153 flags |= TSC2301_ADCREG_AVERAGING_4AVG;
156 flags |= TSC2301_ADCREG_AVERAGING_8AVG;
159 flags |= TSC2301_ADCREG_AVERAGING_16AVG;
165 switch (ts->stab_time) {
167 flags |= TSC2301_ADCREG_VOLTAGE_STAB_0US;
170 flags |= TSC2301_ADCREG_VOLTAGE_STAB_100US;
173 flags |= TSC2301_ADCREG_VOLTAGE_STAB_500US;
176 flags |= TSC2301_ADCREG_VOLTAGE_STAB_1MS;
179 flags |= TSC2301_ADCREG_VOLTAGE_STAB_5MS;
182 flags |= TSC2301_ADCREG_VOLTAGE_STAB_10MS;
185 flags |= TSC2301_ADCREG_VOLTAGE_STAB_50MS;
188 flags |= TSC2301_ADCREG_VOLTAGE_STAB_100MS;
199 * This odd three-time initialization is to work around a bug in TSC2301.
200 * See TSC2301 errata for details.
202 static int tsc2301_ts_configure(struct tsc2301 *tsc, int flags)
204 struct spi_transfer xfer[3];
205 struct spi_transfer *x;
206 struct spi_message m;
207 int reg = TSC2301_REG_ADC;
208 u16 val1, val2, val3;
211 val1 = TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST |
212 TSC2301_ADCREG_STOP_CONVERSION |
213 TSC2301_ADCREG_FUNCTION_NONE |
214 TSC2301_ADCREG_RESOLUTION_12BIT |
215 TSC2301_ADCREG_AVERAGING_NONE |
216 TSC2301_ADCREG_CLOCK_2MHZ |
217 TSC2301_ADCREG_VOLTAGE_STAB_100MS;
219 val2 = TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST |
220 TSC2301_ADCREG_FUNCTION_XYZ |
221 TSC2301_ADCREG_RESOLUTION_12BIT |
222 TSC2301_ADCREG_AVERAGING_16AVG |
223 TSC2301_ADCREG_CLOCK_1MHZ |
224 TSC2301_ADCREG_VOLTAGE_STAB_100MS;
226 /* Averaging and voltage stabilization settings in flags */
227 val3 = TSC2301_ADCREG_CONVERSION_CTRL_BY_TSC2301 |
228 TSC2301_ADCREG_FUNCTION_XYZ |
229 TSC2301_ADCREG_RESOLUTION_12BIT |
230 TSC2301_ADCREG_CLOCK_1MHZ |
233 /* Now we prepare the command for transferring */
241 spi_message_init(&m);
244 memset(xfer, 0, sizeof(xfer));
247 x->tx_buf = &data[0];
250 spi_message_add_tail(x, &m);
253 x->tx_buf = &data[2];
256 spi_message_add_tail(x, &m);
259 x->tx_buf = &data[4];
261 spi_message_add_tail(x, &m);
268 static void tsc2301_ts_start_scan(struct tsc2301 *tsc)
270 tsc2301_ts_configure(tsc, tsc->ts->hw_flags);
273 static void tsc2301_ts_stop_scan(struct tsc2301 *tsc)
275 tsc2301_ts_configure(tsc, TSC2301_ADCREG_STOP_CONVERSION);
278 static int device_suspended(struct device *dev)
280 struct tsc2301 *tsc = dev_get_drvdata(dev);
281 return dev->power.power_state.event != PM_EVENT_ON || tsc->ts->disabled;
284 static void update_pen_state(struct tsc2301_ts *ts, int x, int y, int pressure)
289 input_report_abs(ts->idev, ABS_X, x);
290 input_report_abs(ts->idev, ABS_Y, y);
291 input_report_abs(ts->idev, ABS_PRESSURE, pressure);
293 input_report_key(ts->idev, BTN_TOUCH, 1);
295 } else if (ts->pen_down) {
296 input_report_abs(ts->idev, ABS_PRESSURE, 0);
297 input_report_key(ts->idev, BTN_TOUCH, 0);
302 input_sync(ts->idev);
304 ts->pen_down = pressure ? 1 : 0;
306 dev_dbg(&tsc->spi->dev, "x %4d y %4d p %4d\n", x, y, pressure);
311 * This procedure is called by the SPI framework after the coordinates
312 * have been read from TSC2301
314 static void tsc2301_ts_rx(void *arg)
316 struct tsc2301 *tsc = arg;
317 struct tsc2301_ts *ts = tsc->ts;
318 unsigned int x, y, z1, z2, pressure;
326 pressure = ts->x_plate_ohm * x;
333 /* If pressure value is above a preset limit (pen is barely
334 * touching the screen) we can't trust the coordinate values.
336 if (pressure < ts->pressure_limit && x < MAX_12BIT && y < MAX_12BIT) {
337 ts->pressure_limit = ts->max_pressure;
338 if (ts->ignore_last) {
340 update_pen_state(ts, ts->x, ts->y, ts->p);
345 update_pen_state(ts, x, y, pressure);
349 mod_timer(&ts->timer,
350 jiffies + msecs_to_jiffies(TSC2301_TS_SCAN_TIME));
353 static int is_pen_down(struct tsc2301_ts *ts)
359 * Timer is called every TSC2301_TS_SCAN_TIME when the pen is down
361 static void tsc2301_ts_timer(unsigned long arg)
363 struct tsc2301 *tsc = (void *) arg;
364 struct tsc2301_ts *ts = tsc->ts;
369 spin_lock_irqsave(&ts->lock, flags);
370 ndav = omap_get_gpio_datain(ts->dav_gpio);
371 if (ndav || device_suspended(&tsc->spi->dev)) {
372 /* Pen has been lifted */
373 if (!device_suspended(&tsc->spi->dev)) {
377 update_pen_state(ts, 0, 0, 0);
379 spin_unlock_irqrestore(&ts->lock, flags);
383 spin_unlock_irqrestore(&ts->lock, flags);
385 r = spi_async(tsc->spi, &ts->read_msg);
387 dev_err(&tsc->spi->dev, "ts: spi_async() failed");
392 * This interrupt is called when pen is down and first coordinates are
393 * available. That is indicated by a falling edge on DEV line. IRQ is
394 * disabled here because while the pen is down the coordinates are
397 static irqreturn_t tsc2301_ts_irq_handler(int irq, void *dev_id)
399 struct tsc2301 *tsc = dev_id;
400 struct tsc2301_ts *ts = tsc->ts;
403 spin_lock_irqsave(&ts->lock, flags);
404 if (ts->irq_enabled) {
406 disable_irq(ts->irq);
408 ts->pressure_limit = ts->touch_pressure;
410 mod_timer(&ts->timer,
411 jiffies + msecs_to_jiffies(TSC2301_TS_SCAN_TIME));
413 spin_unlock_irqrestore(&ts->lock, flags);
418 /* Must be called with ts->lock held */
419 static void tsc2301_ts_disable(struct tsc2301 *tsc)
421 struct tsc2301_ts *ts = tsc->ts;
429 disable_irq(ts->irq);
431 while (ts->pending) {
432 spin_unlock_irq(&ts->lock);
434 spin_lock_irq(&ts->lock);
438 spin_unlock_irq(&ts->lock);
439 tsc2301_ts_stop_scan(tsc);
440 /* Workaround a bug where turning on / off touchscreen scanner
441 * can get the keypad scanner stuck.
443 tsc2301_kp_restart(tsc);
444 spin_lock_irq(&ts->lock);
447 static void tsc2301_ts_enable(struct tsc2301 *tsc)
449 struct tsc2301_ts *ts = tsc->ts;
458 spin_unlock_irq(&ts->lock);
459 tsc2301_ts_start_scan(tsc);
460 /* Same workaround as above. */
461 tsc2301_kp_restart(tsc);
462 spin_lock_irq(&ts->lock);
466 int tsc2301_ts_suspend(struct tsc2301 *tsc)
468 struct tsc2301_ts *ts = tsc->ts;
470 spin_lock_irq(&ts->lock);
471 tsc2301_ts_disable(tsc);
472 spin_unlock_irq(&ts->lock);
477 void tsc2301_ts_resume(struct tsc2301 *tsc)
479 struct tsc2301_ts *ts = tsc->ts;
481 spin_lock_irq(&ts->lock);
482 tsc2301_ts_enable(tsc);
483 spin_unlock_irq(&ts->lock);
487 static void tsc2301_ts_setup_spi_xfer(struct tsc2301 *tsc)
489 struct tsc2301_ts *ts = tsc->ts;
490 struct spi_message *m = &ts->read_msg;
491 struct spi_transfer *x = &ts->read_xfer[0];
495 x->tx_buf = &tsc2301_ts_read_data;
497 spi_message_add_tail(x, m);
500 x->rx_buf = &ts->data;
502 spi_message_add_tail(x, m);
504 m->complete = tsc2301_ts_rx;
508 static ssize_t tsc2301_ts_pen_down_show(struct device *dev,
509 struct device_attribute *attr,
512 struct tsc2301 *tsc = dev_get_drvdata(dev);
514 return sprintf(buf, "%u\n", is_pen_down(tsc->ts));
517 static DEVICE_ATTR(pen_down, S_IRUGO, tsc2301_ts_pen_down_show, NULL);
519 static ssize_t tsc2301_ts_disable_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
522 struct tsc2301 *tsc = dev_get_drvdata(dev);
523 struct tsc2301_ts *ts = tsc->ts;
525 return sprintf(buf, "%u\n", ts->disabled);
528 static ssize_t tsc2301_ts_disable_store(struct device *dev,
529 struct device_attribute *attr,
530 const char *buf, size_t count)
532 struct tsc2301 *tsc = dev_get_drvdata(dev);
533 struct tsc2301_ts *ts = tsc->ts;
537 i = simple_strtoul(buf, &endp, 10);
538 spin_lock_irq(&ts->lock);
541 tsc2301_ts_disable(tsc);
543 tsc2301_ts_enable(tsc);
545 spin_unlock_irq(&ts->lock);
550 static DEVICE_ATTR(disable_ts, 0664, tsc2301_ts_disable_show,
551 tsc2301_ts_disable_store);
553 int __devinit tsc2301_ts_init(struct tsc2301 *tsc,
554 struct tsc2301_platform_data *pdata)
556 struct tsc2301_ts *ts;
557 struct input_dev *idev;
560 if (pdata->dav_gpio < 0) {
561 dev_err(&tsc->spi->dev, "need DAV GPIO");
564 dav_gpio = pdata->dav_gpio;
566 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
571 ts->dav_gpio = dav_gpio;
572 #ifdef CONFIG_ARCH_OMAP
573 r = omap_request_gpio(dav_gpio);
575 dev_err(&tsc->spi->dev, "unable to get DAV GPIO");
578 omap_set_gpio_direction(dav_gpio, 1);
579 ts->irq = OMAP_GPIO_IRQ(dav_gpio);
581 init_timer(&ts->timer);
582 ts->timer.data = (unsigned long) tsc;
583 ts->timer.function = tsc2301_ts_timer;
585 spin_lock_init(&ts->lock);
587 ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
588 ts->hw_avg_max = pdata->ts_hw_avg;
589 ts->max_pressure= pdata->ts_max_pressure ? : MAX_12BIT;
590 ts->touch_pressure = pdata->ts_touch_pressure ? : ts->max_pressure;
591 ts->ignore_last = pdata->ts_ignore_last;
592 ts->stab_time = pdata->ts_stab_time;
594 if ((r = tsc2301_ts_check_config(ts, &ts->hw_flags))) {
595 dev_err(&tsc->spi->dev, "invalid configuration\n");
599 idev = input_allocate_device();
604 idev->name = "TSC2301 touchscreen";
605 snprintf(ts->phys, sizeof(ts->phys),
606 "%s/input-ts", tsc->spi->dev.bus_id);
607 idev->phys = ts->phys;
609 idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
610 idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
613 tsc2301_ts_setup_spi_xfer(tsc);
615 /* These parameters should perhaps be configurable? */
616 input_set_abs_params(idev, ABS_X, 0, 4096, 0, 0);
617 input_set_abs_params(idev, ABS_Y, 0, 4096, 0, 0);
618 input_set_abs_params(idev, ABS_PRESSURE, 0, 1024, 0, 0);
620 tsc2301_ts_start_scan(tsc);
623 r = request_irq(ts->irq, tsc2301_ts_irq_handler,
624 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
627 dev_err(&tsc->spi->dev, "unable to get DAV IRQ");
630 set_irq_wake(ts->irq, 1);
632 if (device_create_file(&tsc->spi->dev, &dev_attr_pen_down) < 0)
634 if (device_create_file(&tsc->spi->dev, &dev_attr_disable_ts) < 0)
637 r = input_register_device(idev);
639 dev_err(&tsc->spi->dev, "can't register touchscreen device\n");
645 device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
647 device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
649 free_irq(ts->irq, tsc);
651 tsc2301_ts_stop_scan(tsc);
652 input_free_device(idev);
654 #ifdef CONFIG_ARCH_OMAP
655 omap_free_gpio(dav_gpio);
662 void __devexit tsc2301_ts_exit(struct tsc2301 *tsc)
664 struct tsc2301_ts *ts = tsc->ts;
667 spin_lock_irqsave(&ts->lock, flags);
668 tsc2301_ts_disable(tsc);
669 spin_unlock_irqrestore(&ts->lock, flags);
671 device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
672 device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
674 free_irq(ts->irq, tsc);
675 input_unregister_device(ts->idev);
677 #ifdef CONFIG_ARCH_OMAP
678 omap_free_gpio(ts->dav_gpio);
682 MODULE_AUTHOR("Jarkko Oikarinen <jarkko.oikarinen@nokia.com>");
683 MODULE_LICENSE("GPL");