2 * TSC2005 touchscreen driver
4 * Copyright (C) 2006-2010 Nokia Corporation
6 * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com>
7 * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/input.h>
28 #include <linux/interrupt.h>
29 #include <linux/delay.h>
30 #include <linux/spi/spi.h>
31 #include <linux/spi/tsc2005.h>
34 * The touchscreen interface operates as follows:
36 * 1) Pen is pressed against the touchscreen.
37 * 2) TSC2005 performs AD conversion.
38 * 3) After the conversion is done TSC2005 drives DAV line down.
39 * 4) GPIO IRQ is received and tsc2005_irq_thread() is scheduled.
40 * 5) tsc2005_irq_thread() queues up an spi transfer to fetch the x, y, z1, z2
42 * 6) tsc2005_irq_thread() reports coordinates to input layer and sets up
43 * tsc2005_penup_timer() to be called after TSC2005_PENUP_TIME_MS (40ms).
44 * 7) When the penup timer expires, there have not been touch or DAV interrupts
45 * during the last 40ms which means the pen has been lifted.
47 * ESD recovery via a hardware reset is done if the TSC2005 doesn't respond
48 * after a configurable period (in ms) of activity. If esd_timeout is 0, the
49 * watchdog is disabled.
53 #define TSC2005_CMD 0x80
54 #define TSC2005_CMD_NORMAL 0x00
55 #define TSC2005_CMD_STOP 0x01
56 #define TSC2005_CMD_12BIT 0x04
59 #define TSC2005_REG_READ 0x0001
60 #define TSC2005_REG_PND0 0x0002
61 #define TSC2005_REG_X 0x0000
62 #define TSC2005_REG_Y 0x0008
63 #define TSC2005_REG_Z1 0x0010
64 #define TSC2005_REG_Z2 0x0018
65 #define TSC2005_REG_TEMP_HIGH 0x0050
66 #define TSC2005_REG_CFR0 0x0060
67 #define TSC2005_REG_CFR1 0x0068
68 #define TSC2005_REG_CFR2 0x0070
70 /* configuration register 0 */
71 #define TSC2005_CFR0_PRECHARGE_276US 0x0040
72 #define TSC2005_CFR0_STABTIME_1MS 0x0300
73 #define TSC2005_CFR0_CLOCK_1MHZ 0x1000
74 #define TSC2005_CFR0_RESOLUTION12 0x2000
75 #define TSC2005_CFR0_PENMODE 0x8000
76 #define TSC2005_CFR0_INITVALUE (TSC2005_CFR0_STABTIME_1MS | \
77 TSC2005_CFR0_CLOCK_1MHZ | \
78 TSC2005_CFR0_RESOLUTION12 | \
79 TSC2005_CFR0_PRECHARGE_276US | \
82 /* bits common to both read and write of configuration register 0 */
83 #define TSC2005_CFR0_RW_MASK 0x3fff
85 /* configuration register 1 */
86 #define TSC2005_CFR1_BATCHDELAY_4MS 0x0003
87 #define TSC2005_CFR1_INITVALUE TSC2005_CFR1_BATCHDELAY_4MS
89 /* configuration register 2 */
90 #define TSC2005_CFR2_MAVE_Z 0x0004
91 #define TSC2005_CFR2_MAVE_Y 0x0008
92 #define TSC2005_CFR2_MAVE_X 0x0010
93 #define TSC2005_CFR2_AVG_7 0x0800
94 #define TSC2005_CFR2_MEDIUM_15 0x3000
95 #define TSC2005_CFR2_INITVALUE (TSC2005_CFR2_MAVE_X | \
96 TSC2005_CFR2_MAVE_Y | \
97 TSC2005_CFR2_MAVE_Z | \
98 TSC2005_CFR2_MEDIUM_15 | \
101 #define MAX_12BIT 0xfff
102 #define TSC2005_SPI_MAX_SPEED_HZ 10000000
103 #define TSC2005_PENUP_TIME_MS 40
105 struct tsc2005_spi_rd {
106 struct spi_transfer spi_xfer;
112 struct spi_device *spi;
114 struct spi_message spi_read_msg;
115 struct tsc2005_spi_rd spi_x;
116 struct tsc2005_spi_rd spi_y;
117 struct tsc2005_spi_rd spi_z1;
118 struct tsc2005_spi_rd spi_z2;
120 struct input_dev *idev;
125 /* raw copy of previous x,y,z */
131 struct timer_list penup_timer;
132 struct work_struct penup_work;
134 unsigned int esd_timeout;
135 struct timer_list esd_timer;
136 struct work_struct esd_work;
138 unsigned int x_plate_ohm;
141 unsigned int disable_depth;
142 unsigned int pen_down;
144 void (*set_reset)(bool enable);
147 static void tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
150 struct spi_message msg;
151 struct spi_transfer xfer = { 0 };
153 tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd;
158 xfer.bits_per_word = 8;
160 spi_message_init(&msg);
161 spi_message_add_tail(&xfer, &msg);
162 spi_sync(ts->spi, &msg);
165 static void tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
168 struct spi_message msg;
169 struct spi_transfer xfer = { 0 };
171 tx = (reg | TSC2005_REG_PND0) << 16;
177 xfer.bits_per_word = 24;
179 spi_message_init(&msg);
180 spi_message_add_tail(&xfer, &msg);
181 spi_sync(ts->spi, &msg);
184 static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last)
186 rd->spi_tx = (reg | TSC2005_REG_READ) << 16;
187 rd->spi_xfer.tx_buf = &rd->spi_tx;
188 rd->spi_xfer.rx_buf = &rd->spi_rx;
189 rd->spi_xfer.len = 4;
190 rd->spi_xfer.bits_per_word = 24;
191 rd->spi_xfer.cs_change = !last;
194 static void tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value)
196 struct spi_message msg;
197 struct tsc2005_spi_rd spi_rd = { { 0 }, 0, 0 };
199 tsc2005_setup_read(&spi_rd, reg, 1);
201 spi_message_init(&msg);
202 spi_message_add_tail(&spi_rd.spi_xfer, &msg);
203 spi_sync(ts->spi, &msg);
204 *value = spi_rd.spi_rx;
207 static void tsc2005_update_pen_state(struct tsc2005 *ts,
208 int x, int y, int pressure)
211 input_report_abs(ts->idev, ABS_X, x);
212 input_report_abs(ts->idev, ABS_Y, y);
213 input_report_abs(ts->idev, ABS_PRESSURE, pressure);
215 input_report_key(ts->idev, BTN_TOUCH, !!pressure);
219 input_report_abs(ts->idev, ABS_PRESSURE, 0);
221 input_report_key(ts->idev, BTN_TOUCH, 0);
225 input_sync(ts->idev);
226 dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
230 static irqreturn_t tsc2005_irq_handler(int irq, void *dev_id)
232 struct tsc2005 *ts = dev_id;
234 /* update the penup timer only if it's pending */
235 mod_timer_pending(&ts->penup_timer,
236 jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
238 return IRQ_WAKE_THREAD;
241 static irqreturn_t tsc2005_irq_thread(int irq, void *_ts)
243 struct tsc2005 *ts = _ts;
244 unsigned int pressure;
250 mutex_lock(&ts->mutex);
252 if (unlikely(ts->disable_depth))
255 /* read the coordinates */
256 spi_sync(ts->spi, &ts->spi_read_msg);
257 x = ts->spi_x.spi_rx;
258 y = ts->spi_y.spi_rx;
259 z1 = ts->spi_z1.spi_rx;
260 z2 = ts->spi_z2.spi_rx;
262 /* validate position */
263 if (unlikely(x > MAX_12BIT || y > MAX_12BIT))
266 /* skip coords if the pressure components are out of range */
267 if (unlikely(z1 == 0 || z2 > MAX_12BIT || z1 >= z2))
270 /* skip point if this is a pen down with the exact same values as
271 * the value before pen-up - that implies SPI fed us stale data
280 /* At this point we are happy we have a valid and useful reading.
281 * Remember it for later comparisons. We may now begin downsampling
288 /* compute touch pressure resistance using equation #1 */
289 pressure = x * (z2 - z1) / z1;
290 pressure = pressure * ts->x_plate_ohm / 4096;
291 if (unlikely(pressure > MAX_12BIT))
294 tsc2005_update_pen_state(ts, x, y, pressure);
296 /* set the penup timer */
297 mod_timer(&ts->penup_timer,
298 jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
300 if (!ts->esd_timeout)
303 /* update the watchdog timer */
304 mod_timer(&ts->esd_timer,
305 round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
308 mutex_unlock(&ts->mutex);
312 static void tsc2005_penup_timer(unsigned long data)
314 struct tsc2005 *ts = (struct tsc2005 *)data;
316 schedule_work(&ts->penup_work);
319 static void tsc2005_penup_work(struct work_struct *work)
321 struct tsc2005 *ts = container_of(work, struct tsc2005, penup_work);
323 mutex_lock(&ts->mutex);
324 tsc2005_update_pen_state(ts, 0, 0, 0);
325 mutex_unlock(&ts->mutex);
328 static void tsc2005_start_scan(struct tsc2005 *ts)
330 tsc2005_write(ts, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE);
331 tsc2005_write(ts, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE);
332 tsc2005_write(ts, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE);
333 tsc2005_cmd(ts, TSC2005_CMD_NORMAL);
336 static void tsc2005_stop_scan(struct tsc2005 *ts)
338 tsc2005_cmd(ts, TSC2005_CMD_STOP);
341 /* must be called with mutex held */
342 static void tsc2005_disable(struct tsc2005 *ts)
344 if (ts->disable_depth++ != 0)
346 disable_irq(ts->spi->irq);
348 del_timer_sync(&ts->esd_timer);
349 del_timer_sync(&ts->penup_timer);
350 tsc2005_stop_scan(ts);
353 /* must be called with mutex held */
354 static void tsc2005_enable(struct tsc2005 *ts)
356 if (--ts->disable_depth != 0)
358 tsc2005_start_scan(ts);
359 enable_irq(ts->spi->irq);
360 if (!ts->esd_timeout)
362 mod_timer(&ts->esd_timer,
363 round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
366 static ssize_t tsc2005_disable_show(struct device *dev,
367 struct device_attribute *attr, char *buf)
369 struct tsc2005 *ts = dev_get_drvdata(dev);
371 return sprintf(buf, "%u\n", ts->disabled);
374 static ssize_t tsc2005_disable_store(struct device *dev,
375 struct device_attribute *attr,
376 const char *buf, size_t count)
378 struct tsc2005 *ts = dev_get_drvdata(dev);
382 if (strict_strtoul(buf, 10, &res) < 0)
386 mutex_lock(&ts->mutex);
387 if (i == ts->disabled)
395 mutex_unlock(&ts->mutex);
398 static DEVICE_ATTR(disable, 0664, tsc2005_disable_show, tsc2005_disable_store);
400 static ssize_t tsc2005_selftest_show(struct device *dev,
401 struct device_attribute *attr,
404 struct tsc2005 *ts = dev_get_drvdata(dev);
410 if (!ts->set_reset) {
411 dev_warn(&ts->spi->dev,
412 "unable to selftest: no reset function\n");
417 mutex_lock(&ts->mutex);
420 * Test TSC2005 communications via temp high register.
424 tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig);
425 temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
426 tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test);
427 tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
428 if (temp_high != temp_high_test) {
429 dev_warn(dev, "selftest failed: %d != %d\n",
430 temp_high, temp_high_test);
436 usleep_range(100, 500); /* only 10us required */
440 /* test that the reset really happened */
441 tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
442 if (temp_high != temp_high_orig) {
443 dev_warn(dev, "selftest failed after reset: %d != %d\n",
444 temp_high, temp_high_orig);
448 mutex_unlock(&ts->mutex);
451 return sprintf(buf, "%u\n", result);
453 static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL);
455 static void tsc2005_esd_timer(unsigned long data)
457 struct tsc2005 *ts = (struct tsc2005 *)data;
459 schedule_work(&ts->esd_work);
462 static void tsc2005_esd_work(struct work_struct *work)
464 struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work);
467 mutex_lock(&ts->mutex);
469 if (ts->disable_depth)
473 * If we cannot read our known value from configuration register 0 then
474 * reset the controller as if from power-up and start scanning again.
476 tsc2005_read(ts, TSC2005_REG_CFR0, &r);
477 if ((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK) {
478 dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");
480 tsc2005_update_pen_state(ts, 0, 0, 0);
481 usleep_range(100, 500); /* only 10us required */
483 tsc2005_start_scan(ts);
486 /* re-arm the watchdog */
487 mod_timer(&ts->esd_timer,
488 round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
491 mutex_unlock(&ts->mutex);
494 static void __devinit tsc2005_setup_spi_xfer(struct tsc2005 *ts)
496 tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, 0);
497 tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, 0);
498 tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, 0);
499 tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, 1);
501 spi_message_init(&ts->spi_read_msg);
502 spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg);
503 spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg);
504 spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg);
505 spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg);
508 static struct attribute *tsc2005_attrs[] = {
509 &dev_attr_disable.attr,
510 &dev_attr_selftest.attr,
514 static struct attribute_group tsc2005_attr_group = {
515 .attrs = tsc2005_attrs,
518 static int __devinit tsc2005_setup(struct tsc2005 *ts,
519 struct tsc2005_platform_data *pdata)
529 mutex_init(&ts->mutex);
531 tsc2005_setup_spi_xfer(ts);
533 init_timer(&ts->penup_timer);
534 setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);
535 INIT_WORK(&ts->penup_work, tsc2005_penup_work);
537 fudge_x = pdata->ts_x_fudge ? : 4;
538 fudge_y = pdata->ts_y_fudge ? : 8;
539 fudge_p = pdata->ts_pressure_fudge ? : 2;
540 x_max = pdata->ts_x_max ? : MAX_12BIT;
541 y_max = pdata->ts_y_max ? : MAX_12BIT;
542 p_max = pdata->ts_pressure_max ? : MAX_12BIT;
543 ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
544 ts->esd_timeout = pdata->esd_timeout_ms;
545 ts->set_reset = pdata->set_reset;
547 ts->idev = input_allocate_device();
548 if (ts->idev == NULL)
550 ts->idev->name = "TSC2005 touchscreen";
551 snprintf(ts->phys, sizeof(ts->phys), "%s/input-ts",
552 dev_name(&ts->spi->dev));
553 ts->idev->phys = ts->phys;
554 ts->idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
555 ts->idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
556 ts->idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
558 input_set_abs_params(ts->idev, ABS_X, 0, x_max, fudge_x, 0);
559 input_set_abs_params(ts->idev, ABS_Y, 0, y_max, fudge_y, 0);
560 input_set_abs_params(ts->idev, ABS_PRESSURE, 0, p_max, fudge_p, 0);
562 r = request_threaded_irq(ts->spi->irq, tsc2005_irq_handler,
563 tsc2005_irq_thread, IRQF_TRIGGER_RISING,
566 dev_err(&ts->spi->dev, "request_threaded_irq(): %d\n", r);
569 set_irq_wake(ts->spi->irq, 1);
571 r = input_register_device(ts->idev);
573 dev_err(&ts->spi->dev, "input_register_device(): %d\n", r);
577 r = sysfs_create_group(&ts->spi->dev.kobj, &tsc2005_attr_group);
579 dev_warn(&ts->spi->dev, "sysfs entry creation failed: %d\n", r);
581 tsc2005_start_scan(ts);
583 if (!ts->esd_timeout || !ts->set_reset)
586 /* start the optional ESD watchdog */
587 setup_timer(&ts->esd_timer, tsc2005_esd_timer, (unsigned long)ts);
588 INIT_WORK(&ts->esd_work, tsc2005_esd_work);
589 mod_timer(&ts->esd_timer,
590 round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
596 free_irq(ts->spi->irq, ts);
599 input_free_device(ts->idev);
603 static int __devinit tsc2005_probe(struct spi_device *spi)
605 struct tsc2005_platform_data *pdata = spi->dev.platform_data;
610 dev_dbg(&spi->dev, "no irq\n");
615 dev_dbg(&spi->dev, "no platform data\n");
619 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
623 dev_set_drvdata(&spi->dev, ts);
625 spi->dev.power.power_state = PMSG_ON;
626 spi->mode = SPI_MODE_0;
627 spi->bits_per_word = 8;
628 if (!spi->max_speed_hz)
629 spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ;
632 r = tsc2005_setup(ts, pdata);
638 static int __devexit tsc2005_remove(struct spi_device *spi)
640 struct tsc2005 *ts = dev_get_drvdata(&spi->dev);
642 mutex_lock(&ts->mutex);
644 mutex_unlock(&ts->mutex);
647 del_timer_sync(&ts->esd_timer);
648 del_timer_sync(&ts->penup_timer);
650 flush_work(&ts->esd_work);
651 flush_work(&ts->penup_work);
653 sysfs_remove_group(&ts->spi->dev.kobj, &tsc2005_attr_group);
654 free_irq(ts->spi->irq, ts);
655 input_unregister_device(ts->idev);
662 static int tsc2005_suspend(struct spi_device *spi, pm_message_t mesg)
664 struct tsc2005 *ts = dev_get_drvdata(&spi->dev);
666 mutex_lock(&ts->mutex);
668 mutex_unlock(&ts->mutex);
673 static int tsc2005_resume(struct spi_device *spi)
675 struct tsc2005 *ts = dev_get_drvdata(&spi->dev);
677 mutex_lock(&ts->mutex);
679 mutex_unlock(&ts->mutex);
685 static struct spi_driver tsc2005_driver = {
688 .owner = THIS_MODULE,
691 .suspend = tsc2005_suspend,
692 .resume = tsc2005_resume,
694 .probe = tsc2005_probe,
695 .remove = __devexit_p(tsc2005_remove),
698 static int __init tsc2005_init(void)
700 printk(KERN_INFO "TSC2005 driver initializing\n");
701 return spi_register_driver(&tsc2005_driver);
703 module_init(tsc2005_init);
705 static void __exit tsc2005_exit(void)
707 spi_unregister_driver(&tsc2005_driver);
709 module_exit(tsc2005_exit);
711 MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
712 MODULE_LICENSE("GPL");
713 MODULE_ALIAS("platform:tsc2005");