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>
31 #include <linux/spi/spi.h>
32 #include <linux/spi/tsc2005.h>
35 * The touchscreen interface operates as follows:
37 * 1) Pen is pressed against the touchscreen.
38 * 2) TSC2005 performs AD conversion.
39 * 3) After the conversion is done TSC2005 drives DAV line down.
40 * 4) GPIO IRQ is received and tsc2005_irq_thread() is scheduled.
41 * 5) tsc2005_irq_thread() queues up an spi transfer to fetch the x, y, z1, z2
43 * 6) tsc2005_irq_thread() reports coordinates to input layer and sets up
44 * tsc2005_penup_timer() to be called after TSC2005_PENUP_TIME_MS (40ms).
45 * 7) When the penup timer expires, there have not been touch or DAV interrupts
46 * during the last 40ms which means the pen has been lifted.
48 * ESD recovery via a hardware reset is done if the TSC2005 doesn't respond
49 * after a configurable period (in ms) of activity. If esd_timeout is 0, the
50 * watchdog is disabled.
54 #define TSC2005_CMD 0x80
55 #define TSC2005_CMD_NORMAL 0x00
56 #define TSC2005_CMD_STOP 0x01
57 #define TSC2005_CMD_12BIT 0x04
60 #define TSC2005_REG_READ 0x0001
61 #define TSC2005_REG_PND0 0x0002
62 #define TSC2005_REG_X 0x0000
63 #define TSC2005_REG_Y 0x0008
64 #define TSC2005_REG_Z1 0x0010
65 #define TSC2005_REG_Z2 0x0018
66 #define TSC2005_REG_TEMP_HIGH 0x0050
67 #define TSC2005_REG_CFR0 0x0060
68 #define TSC2005_REG_CFR1 0x0068
69 #define TSC2005_REG_CFR2 0x0070
71 /* configuration register 0 */
72 #define TSC2005_CFR0_PRECHARGE_276US 0x0040
73 #define TSC2005_CFR0_STABTIME_1MS 0x0300
74 #define TSC2005_CFR0_CLOCK_1MHZ 0x1000
75 #define TSC2005_CFR0_RESOLUTION12 0x2000
76 #define TSC2005_CFR0_PENMODE 0x8000
77 #define TSC2005_CFR0_INITVALUE (TSC2005_CFR0_STABTIME_1MS | \
78 TSC2005_CFR0_CLOCK_1MHZ | \
79 TSC2005_CFR0_RESOLUTION12 | \
80 TSC2005_CFR0_PRECHARGE_276US | \
83 /* bits common to both read and write of configuration register 0 */
84 #define TSC2005_CFR0_RW_MASK 0x3fff
86 /* configuration register 1 */
87 #define TSC2005_CFR1_BATCHDELAY_4MS 0x0003
88 #define TSC2005_CFR1_INITVALUE TSC2005_CFR1_BATCHDELAY_4MS
90 /* configuration register 2 */
91 #define TSC2005_CFR2_MAVE_Z 0x0004
92 #define TSC2005_CFR2_MAVE_Y 0x0008
93 #define TSC2005_CFR2_MAVE_X 0x0010
94 #define TSC2005_CFR2_AVG_7 0x0800
95 #define TSC2005_CFR2_MEDIUM_15 0x3000
96 #define TSC2005_CFR2_INITVALUE (TSC2005_CFR2_MAVE_X | \
97 TSC2005_CFR2_MAVE_Y | \
98 TSC2005_CFR2_MAVE_Z | \
99 TSC2005_CFR2_MEDIUM_15 | \
102 #define MAX_12BIT 0xfff
103 #define TSC2005_SPI_MAX_SPEED_HZ 10000000
104 #define TSC2005_PENUP_TIME_MS 40
106 struct tsc2005_spi_rd
{
107 struct spi_transfer spi_xfer
;
113 struct spi_device
*spi
;
115 struct spi_message spi_read_msg
;
116 struct tsc2005_spi_rd spi_x
;
117 struct tsc2005_spi_rd spi_y
;
118 struct tsc2005_spi_rd spi_z1
;
119 struct tsc2005_spi_rd spi_z2
;
121 struct input_dev
*idev
;
126 /* raw copy of previous x,y,z */
133 struct timer_list penup_timer
;
135 unsigned int esd_timeout
;
136 struct delayed_work esd_work
;
137 unsigned long last_valid_interrupt
;
139 unsigned int x_plate_ohm
;
146 void (*set_reset
)(bool enable
);
149 static int tsc2005_cmd(struct tsc2005
*ts
, u8 cmd
)
151 u8 tx
= TSC2005_CMD
| TSC2005_CMD_12BIT
| cmd
;
152 struct spi_transfer xfer
= {
157 struct spi_message msg
;
160 spi_message_init(&msg
);
161 spi_message_add_tail(&xfer
, &msg
);
163 error
= spi_sync(ts
->spi
, &msg
);
165 dev_err(&ts
->spi
->dev
, "%s: failed, command: %x, error: %d\n",
166 __func__
, cmd
, error
);
173 static int tsc2005_write(struct tsc2005
*ts
, u8 reg
, u16 value
)
175 u32 tx
= ((reg
| TSC2005_REG_PND0
) << 16) | value
;
176 struct spi_transfer xfer
= {
181 struct spi_message msg
;
184 spi_message_init(&msg
);
185 spi_message_add_tail(&xfer
, &msg
);
187 error
= spi_sync(ts
->spi
, &msg
);
189 dev_err(&ts
->spi
->dev
,
190 "%s: failed, register: %x, value: %x, error: %d\n",
191 __func__
, reg
, value
, error
);
198 static void tsc2005_setup_read(struct tsc2005_spi_rd
*rd
, u8 reg
, bool last
)
200 memset(rd
, 0, sizeof(*rd
));
202 rd
->spi_tx
= (reg
| TSC2005_REG_READ
) << 16;
203 rd
->spi_xfer
.tx_buf
= &rd
->spi_tx
;
204 rd
->spi_xfer
.rx_buf
= &rd
->spi_rx
;
205 rd
->spi_xfer
.len
= 4;
206 rd
->spi_xfer
.bits_per_word
= 24;
207 rd
->spi_xfer
.cs_change
= !last
;
210 static int tsc2005_read(struct tsc2005
*ts
, u8 reg
, u16
*value
)
212 struct tsc2005_spi_rd spi_rd
;
213 struct spi_message msg
;
216 tsc2005_setup_read(&spi_rd
, reg
, true);
218 spi_message_init(&msg
);
219 spi_message_add_tail(&spi_rd
.spi_xfer
, &msg
);
221 error
= spi_sync(ts
->spi
, &msg
);
225 *value
= spi_rd
.spi_rx
;
229 static void tsc2005_update_pen_state(struct tsc2005
*ts
,
230 int x
, int y
, int pressure
)
233 input_report_abs(ts
->idev
, ABS_X
, x
);
234 input_report_abs(ts
->idev
, ABS_Y
, y
);
235 input_report_abs(ts
->idev
, ABS_PRESSURE
, pressure
);
237 input_report_key(ts
->idev
, BTN_TOUCH
, !!pressure
);
241 input_report_abs(ts
->idev
, ABS_PRESSURE
, 0);
243 input_report_key(ts
->idev
, BTN_TOUCH
, 0);
244 ts
->pen_down
= false;
247 input_sync(ts
->idev
);
248 dev_dbg(&ts
->spi
->dev
, "point(%4d,%4d), pressure (%4d)\n", x
, y
,
252 static irqreturn_t
tsc2005_irq_thread(int irq
, void *_ts
)
254 struct tsc2005
*ts
= _ts
;
256 unsigned int pressure
;
261 /* read the coordinates */
262 error
= spi_sync(ts
->spi
, &ts
->spi_read_msg
);
266 x
= ts
->spi_x
.spi_rx
;
267 y
= ts
->spi_y
.spi_rx
;
268 z1
= ts
->spi_z1
.spi_rx
;
269 z2
= ts
->spi_z2
.spi_rx
;
271 /* validate position */
272 if (unlikely(x
> MAX_12BIT
|| y
> MAX_12BIT
))
275 /* Skip reading if the pressure components are out of range */
276 if (unlikely(z1
== 0 || z2
> MAX_12BIT
|| z1
>= z2
))
280 * Skip point if this is a pen down with the exact same values as
281 * the value before pen-up - that implies SPI fed us stale data
284 ts
->in_x
== x
&& ts
->in_y
== y
&&
285 ts
->in_z1
== z1
&& ts
->in_z2
== z2
) {
290 * At this point we are happy we have a valid and useful reading.
291 * Remember it for later comparisons. We may now begin downsampling.
298 /* Compute touch pressure resistance using equation #1 */
299 pressure
= x
* (z2
- z1
) / z1
;
300 pressure
= pressure
* ts
->x_plate_ohm
/ 4096;
301 if (unlikely(pressure
> MAX_12BIT
))
304 spin_lock_irqsave(&ts
->lock
, flags
);
306 tsc2005_update_pen_state(ts
, x
, y
, pressure
);
307 mod_timer(&ts
->penup_timer
,
308 jiffies
+ msecs_to_jiffies(TSC2005_PENUP_TIME_MS
));
310 spin_unlock_irqrestore(&ts
->lock
, flags
);
312 ts
->last_valid_interrupt
= jiffies
;
317 static void tsc2005_penup_timer(unsigned long data
)
319 struct tsc2005
*ts
= (struct tsc2005
*)data
;
322 spin_lock_irqsave(&ts
->lock
, flags
);
323 tsc2005_update_pen_state(ts
, 0, 0, 0);
324 spin_unlock_irqrestore(&ts
->lock
, flags
);
327 static void tsc2005_start_scan(struct tsc2005
*ts
)
329 tsc2005_write(ts
, TSC2005_REG_CFR0
, TSC2005_CFR0_INITVALUE
);
330 tsc2005_write(ts
, TSC2005_REG_CFR1
, TSC2005_CFR1_INITVALUE
);
331 tsc2005_write(ts
, TSC2005_REG_CFR2
, TSC2005_CFR2_INITVALUE
);
332 tsc2005_cmd(ts
, TSC2005_CMD_NORMAL
);
335 static void tsc2005_stop_scan(struct tsc2005
*ts
)
337 tsc2005_cmd(ts
, TSC2005_CMD_STOP
);
340 /* must be called with ts->mutex held */
341 static void __tsc2005_disable(struct tsc2005
*ts
)
343 tsc2005_stop_scan(ts
);
345 disable_irq(ts
->spi
->irq
);
346 del_timer_sync(&ts
->penup_timer
);
348 cancel_delayed_work_sync(&ts
->esd_work
);
350 enable_irq(ts
->spi
->irq
);
353 /* must be called with ts->mutex held */
354 static void __tsc2005_enable(struct tsc2005
*ts
)
356 tsc2005_start_scan(ts
);
358 if (ts
->esd_timeout
&& ts
->set_reset
) {
359 ts
->last_valid_interrupt
= jiffies
;
360 schedule_delayed_work(&ts
->esd_work
,
361 round_jiffies(jiffies
+
362 msecs_to_jiffies(ts
->esd_timeout
)));
367 static ssize_t
tsc2005_selftest_show(struct device
*dev
,
368 struct device_attribute
*attr
,
371 struct spi_device
*spi
= to_spi_device(dev
);
372 struct tsc2005
*ts
= spi_get_drvdata(spi
);
379 mutex_lock(&ts
->mutex
);
382 * Test TSC2005 communications via temp high register.
384 __tsc2005_disable(ts
);
386 error
= tsc2005_read(ts
, TSC2005_REG_TEMP_HIGH
, &temp_high_orig
);
388 dev_warn(dev
, "selftest failed: read error %d\n", error
);
393 temp_high_test
= (temp_high_orig
- 1) & MAX_12BIT
;
395 error
= tsc2005_write(ts
, TSC2005_REG_TEMP_HIGH
, temp_high_test
);
397 dev_warn(dev
, "selftest failed: write error %d\n", error
);
402 error
= tsc2005_read(ts
, TSC2005_REG_TEMP_HIGH
, &temp_high
);
404 dev_warn(dev
, "selftest failed: read error %d after write\n",
410 if (temp_high
!= temp_high_test
) {
411 dev_warn(dev
, "selftest failed: %d != %d\n",
412 temp_high
, temp_high_test
);
417 ts
->set_reset(false);
418 usleep_range(100, 500); /* only 10us required */
424 /* test that the reset really happened */
425 error
= tsc2005_read(ts
, TSC2005_REG_TEMP_HIGH
, &temp_high
);
427 dev_warn(dev
, "selftest failed: read error %d after reset\n",
433 if (temp_high
!= temp_high_orig
) {
434 dev_warn(dev
, "selftest failed after reset: %d != %d\n",
435 temp_high
, temp_high_orig
);
440 __tsc2005_enable(ts
);
441 mutex_unlock(&ts
->mutex
);
443 return sprintf(buf
, "%d\n", success
);
446 static DEVICE_ATTR(selftest
, S_IRUGO
, tsc2005_selftest_show
, NULL
);
448 static struct attribute
*tsc2005_attrs
[] = {
449 &dev_attr_selftest
.attr
,
453 static mode_t
tsc2005_attr_is_visible(struct kobject
*kobj
,
454 struct attribute
*attr
, int n
)
456 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
457 struct spi_device
*spi
= to_spi_device(dev
);
458 struct tsc2005
*ts
= spi_get_drvdata(spi
);
459 mode_t mode
= attr
->mode
;
461 if (attr
== &dev_attr_selftest
.attr
) {
469 static const struct attribute_group tsc2005_attr_group
= {
470 .is_visible
= tsc2005_attr_is_visible
,
471 .attrs
= tsc2005_attrs
,
474 static void tsc2005_esd_work(struct work_struct
*work
)
476 struct tsc2005
*ts
= container_of(work
, struct tsc2005
, esd_work
.work
);
480 mutex_lock(&ts
->mutex
);
482 if (time_is_after_jiffies(ts
->last_valid_interrupt
+
483 msecs_to_jiffies(ts
->esd_timeout
)))
486 /* We should be able to read register without disabling interrupts. */
487 error
= tsc2005_read(ts
, TSC2005_REG_CFR0
, &r
);
489 !((r
^ TSC2005_CFR0_INITVALUE
) & TSC2005_CFR0_RW_MASK
)) {
494 * If we could not read our known value from configuration register 0
495 * then we should reset the controller as if from power-up and start
498 dev_info(&ts
->spi
->dev
, "TSC2005 not responding - resetting\n");
500 disable_irq(ts
->spi
->irq
);
501 del_timer_sync(&ts
->penup_timer
);
503 tsc2005_update_pen_state(ts
, 0, 0, 0);
505 ts
->set_reset(false);
506 usleep_range(100, 500); /* only 10us required */
509 enable_irq(ts
->spi
->irq
);
510 tsc2005_start_scan(ts
);
513 /* re-arm the watchdog */
514 schedule_delayed_work(&ts
->esd_work
,
515 round_jiffies(jiffies
+
516 msecs_to_jiffies(ts
->esd_timeout
)));
517 mutex_unlock(&ts
->mutex
);
520 static int tsc2005_open(struct input_dev
*input
)
522 struct tsc2005
*ts
= input_get_drvdata(input
);
524 mutex_lock(&ts
->mutex
);
527 __tsc2005_enable(ts
);
531 mutex_unlock(&ts
->mutex
);
536 static void tsc2005_close(struct input_dev
*input
)
538 struct tsc2005
*ts
= input_get_drvdata(input
);
540 mutex_lock(&ts
->mutex
);
543 __tsc2005_disable(ts
);
547 mutex_unlock(&ts
->mutex
);
550 static void __devinit
tsc2005_setup_spi_xfer(struct tsc2005
*ts
)
552 tsc2005_setup_read(&ts
->spi_x
, TSC2005_REG_X
, false);
553 tsc2005_setup_read(&ts
->spi_y
, TSC2005_REG_Y
, false);
554 tsc2005_setup_read(&ts
->spi_z1
, TSC2005_REG_Z1
, false);
555 tsc2005_setup_read(&ts
->spi_z2
, TSC2005_REG_Z2
, true);
557 spi_message_init(&ts
->spi_read_msg
);
558 spi_message_add_tail(&ts
->spi_x
.spi_xfer
, &ts
->spi_read_msg
);
559 spi_message_add_tail(&ts
->spi_y
.spi_xfer
, &ts
->spi_read_msg
);
560 spi_message_add_tail(&ts
->spi_z1
.spi_xfer
, &ts
->spi_read_msg
);
561 spi_message_add_tail(&ts
->spi_z2
.spi_xfer
, &ts
->spi_read_msg
);
564 static int __devinit
tsc2005_probe(struct spi_device
*spi
)
566 const struct tsc2005_platform_data
*pdata
= spi
->dev
.platform_data
;
568 struct input_dev
*input_dev
;
569 unsigned int max_x
, max_y
, max_p
;
570 unsigned int fudge_x
, fudge_y
, fudge_p
;
574 dev_dbg(&spi
->dev
, "no platform data\n");
578 fudge_x
= pdata
->ts_x_fudge
? : 4;
579 fudge_y
= pdata
->ts_y_fudge
? : 8;
580 fudge_p
= pdata
->ts_pressure_fudge
? : 2;
581 max_x
= pdata
->ts_x_max
? : MAX_12BIT
;
582 max_y
= pdata
->ts_y_max
? : MAX_12BIT
;
583 max_p
= pdata
->ts_pressure_max
? : MAX_12BIT
;
586 dev_dbg(&spi
->dev
, "no irq\n");
590 spi
->mode
= SPI_MODE_0
;
591 spi
->bits_per_word
= 8;
592 if (!spi
->max_speed_hz
)
593 spi
->max_speed_hz
= TSC2005_SPI_MAX_SPEED_HZ
;
595 error
= spi_setup(spi
);
599 ts
= kzalloc(sizeof(*ts
), GFP_KERNEL
);
600 input_dev
= input_allocate_device();
601 if (!ts
|| !input_dev
) {
607 ts
->idev
= input_dev
;
609 ts
->x_plate_ohm
= pdata
->ts_x_plate_ohm
? : 280;
610 ts
->esd_timeout
= pdata
->esd_timeout_ms
;
611 ts
->set_reset
= pdata
->set_reset
;
613 mutex_init(&ts
->mutex
);
615 spin_lock_init(&ts
->lock
);
616 setup_timer(&ts
->penup_timer
, tsc2005_penup_timer
, (unsigned long)ts
);
618 INIT_DELAYED_WORK(&ts
->esd_work
, tsc2005_esd_work
);
620 tsc2005_setup_spi_xfer(ts
);
622 snprintf(ts
->phys
, sizeof(ts
->phys
),
623 "%s/input-ts", dev_name(&spi
->dev
));
625 input_dev
->name
= "TSC2005 touchscreen";
626 input_dev
->phys
= ts
->phys
;
627 input_dev
->id
.bustype
= BUS_SPI
;
628 input_dev
->dev
.parent
= &spi
->dev
;
629 input_dev
->evbit
[0] = BIT(EV_ABS
) | BIT(EV_KEY
);
630 input_dev
->keybit
[BIT_WORD(BTN_TOUCH
)] = BIT_MASK(BTN_TOUCH
);
632 input_set_abs_params(input_dev
, ABS_X
, 0, max_x
, fudge_x
, 0);
633 input_set_abs_params(input_dev
, ABS_Y
, 0, max_y
, fudge_y
, 0);
634 input_set_abs_params(input_dev
, ABS_PRESSURE
, 0, max_p
, fudge_p
, 0);
636 input_dev
->open
= tsc2005_open
;
637 input_dev
->close
= tsc2005_close
;
639 input_set_drvdata(input_dev
, ts
);
641 /* Ensure the touchscreen is off */
642 tsc2005_stop_scan(ts
);
644 error
= request_threaded_irq(spi
->irq
, NULL
, tsc2005_irq_thread
,
645 IRQF_TRIGGER_RISING
, "tsc2005", ts
);
647 dev_err(&spi
->dev
, "Failed to request irq, err: %d\n", error
);
651 spi_set_drvdata(spi
, ts
);
652 error
= sysfs_create_group(&spi
->dev
.kobj
, &tsc2005_attr_group
);
655 "Failed to create sysfs attributes, err: %d\n", error
);
656 goto err_clear_drvdata
;
659 error
= input_register_device(ts
->idev
);
662 "Failed to register input device, err: %d\n", error
);
663 goto err_remove_sysfs
;
666 set_irq_wake(spi
->irq
, 1);
670 sysfs_remove_group(&spi
->dev
.kobj
, &tsc2005_attr_group
);
672 spi_set_drvdata(spi
, NULL
);
673 free_irq(spi
->irq
, ts
);
675 input_free_device(input_dev
);
680 static int __devexit
tsc2005_remove(struct spi_device
*spi
)
682 struct tsc2005
*ts
= spi_get_drvdata(spi
);
684 sysfs_remove_group(&ts
->spi
->dev
.kobj
, &tsc2005_attr_group
);
686 free_irq(ts
->spi
->irq
, ts
);
687 input_unregister_device(ts
->idev
);
690 spi_set_drvdata(spi
, NULL
);
694 #ifdef CONFIG_PM_SLEEP
695 static int tsc2005_suspend(struct device
*dev
)
697 struct spi_device
*spi
= to_spi_device(dev
);
698 struct tsc2005
*ts
= spi_get_drvdata(spi
);
700 mutex_lock(&ts
->mutex
);
702 if (!ts
->suspended
&& ts
->opened
)
703 __tsc2005_disable(ts
);
705 ts
->suspended
= true;
707 mutex_unlock(&ts
->mutex
);
712 static int tsc2005_resume(struct device
*dev
)
714 struct spi_device
*spi
= to_spi_device(dev
);
715 struct tsc2005
*ts
= spi_get_drvdata(spi
);
717 mutex_lock(&ts
->mutex
);
719 if (ts
->suspended
&& ts
->opened
)
720 __tsc2005_enable(ts
);
722 ts
->suspended
= false;
724 mutex_unlock(&ts
->mutex
);
730 static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops
, tsc2005_suspend
, tsc2005_resume
);
732 static struct spi_driver tsc2005_driver
= {
735 .owner
= THIS_MODULE
,
736 .pm
= &tsc2005_pm_ops
,
738 .probe
= tsc2005_probe
,
739 .remove
= __devexit_p(tsc2005_remove
),
742 static int __init
tsc2005_init(void)
744 return spi_register_driver(&tsc2005_driver
);
746 module_init(tsc2005_init
);
748 static void __exit
tsc2005_exit(void)
750 spi_unregister_driver(&tsc2005_driver
);
752 module_exit(tsc2005_exit
);
754 MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
755 MODULE_DESCRIPTION("TSC2005 Touchscreen Driver");
756 MODULE_LICENSE("GPL");