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 */
132 struct timer_list penup_timer
;
133 struct work_struct penup_work
;
135 unsigned int esd_timeout
;
136 struct timer_list esd_timer
;
137 struct work_struct esd_work
;
139 unsigned int x_plate_ohm
;
142 unsigned int disable_depth
;
143 unsigned int pen_down
;
145 void (*set_reset
)(bool enable
);
148 static void tsc2005_cmd(struct tsc2005
*ts
, u8 cmd
)
151 struct spi_message msg
;
152 struct spi_transfer xfer
= { 0 };
154 tx
= TSC2005_CMD
| TSC2005_CMD_12BIT
| cmd
;
159 xfer
.bits_per_word
= 8;
161 spi_message_init(&msg
);
162 spi_message_add_tail(&xfer
, &msg
);
163 spi_sync(ts
->spi
, &msg
);
166 static void tsc2005_write(struct tsc2005
*ts
, u8 reg
, u16 value
)
169 struct spi_message msg
;
170 struct spi_transfer xfer
= { 0 };
172 tx
= (reg
| TSC2005_REG_PND0
) << 16;
178 xfer
.bits_per_word
= 24;
180 spi_message_init(&msg
);
181 spi_message_add_tail(&xfer
, &msg
);
182 spi_sync(ts
->spi
, &msg
);
185 static void tsc2005_setup_read(struct tsc2005_spi_rd
*rd
, u8 reg
, bool last
)
187 rd
->spi_tx
= (reg
| TSC2005_REG_READ
) << 16;
188 rd
->spi_xfer
.tx_buf
= &rd
->spi_tx
;
189 rd
->spi_xfer
.rx_buf
= &rd
->spi_rx
;
190 rd
->spi_xfer
.len
= 4;
191 rd
->spi_xfer
.bits_per_word
= 24;
192 rd
->spi_xfer
.cs_change
= !last
;
195 static void tsc2005_read(struct tsc2005
*ts
, u8 reg
, u16
*value
)
197 struct spi_message msg
;
198 struct tsc2005_spi_rd spi_rd
= { { 0 }, 0, 0 };
200 tsc2005_setup_read(&spi_rd
, reg
, 1);
202 spi_message_init(&msg
);
203 spi_message_add_tail(&spi_rd
.spi_xfer
, &msg
);
204 spi_sync(ts
->spi
, &msg
);
205 *value
= spi_rd
.spi_rx
;
208 static void tsc2005_update_pen_state(struct tsc2005
*ts
,
209 int x
, int y
, int pressure
)
212 input_report_abs(ts
->idev
, ABS_X
, x
);
213 input_report_abs(ts
->idev
, ABS_Y
, y
);
214 input_report_abs(ts
->idev
, ABS_PRESSURE
, pressure
);
216 input_report_key(ts
->idev
, BTN_TOUCH
, !!pressure
);
220 input_report_abs(ts
->idev
, ABS_PRESSURE
, 0);
222 input_report_key(ts
->idev
, BTN_TOUCH
, 0);
226 input_sync(ts
->idev
);
227 dev_dbg(&ts
->spi
->dev
, "point(%4d,%4d), pressure (%4d)\n", x
, y
,
231 static irqreturn_t
tsc2005_irq_handler(int irq
, void *dev_id
)
233 struct tsc2005
*ts
= dev_id
;
235 /* update the penup timer only if it's pending */
236 mod_timer_pending(&ts
->penup_timer
,
237 jiffies
+ msecs_to_jiffies(TSC2005_PENUP_TIME_MS
));
239 return IRQ_WAKE_THREAD
;
242 static irqreturn_t
tsc2005_irq_thread(int irq
, void *_ts
)
244 struct tsc2005
*ts
= _ts
;
245 unsigned int pressure
;
251 mutex_lock(&ts
->mutex
);
253 if (unlikely(ts
->disable_depth
))
256 /* read the coordinates */
257 spi_sync(ts
->spi
, &ts
->spi_read_msg
);
258 x
= ts
->spi_x
.spi_rx
;
259 y
= ts
->spi_y
.spi_rx
;
260 z1
= ts
->spi_z1
.spi_rx
;
261 z2
= ts
->spi_z2
.spi_rx
;
263 /* validate position */
264 if (unlikely(x
> MAX_12BIT
|| y
> MAX_12BIT
))
267 /* skip coords if the pressure components are out of range */
268 if (unlikely(z1
== 0 || z2
> MAX_12BIT
|| z1
>= z2
))
271 /* skip point if this is a pen down with the exact same values as
272 * the value before pen-up - that implies SPI fed us stale data
281 /* At this point we are happy we have a valid and useful reading.
282 * Remember it for later comparisons. We may now begin downsampling
289 /* compute touch pressure resistance using equation #1 */
290 pressure
= x
* (z2
- z1
) / z1
;
291 pressure
= pressure
* ts
->x_plate_ohm
/ 4096;
292 if (unlikely(pressure
> MAX_12BIT
))
295 tsc2005_update_pen_state(ts
, x
, y
, pressure
);
297 /* set the penup timer */
298 mod_timer(&ts
->penup_timer
,
299 jiffies
+ msecs_to_jiffies(TSC2005_PENUP_TIME_MS
));
301 if (!ts
->esd_timeout
)
304 /* update the watchdog timer */
305 mod_timer(&ts
->esd_timer
,
306 round_jiffies(jiffies
+ msecs_to_jiffies(ts
->esd_timeout
)));
309 mutex_unlock(&ts
->mutex
);
313 static void tsc2005_penup_timer(unsigned long data
)
315 struct tsc2005
*ts
= (struct tsc2005
*)data
;
317 schedule_work(&ts
->penup_work
);
320 static void tsc2005_penup_work(struct work_struct
*work
)
322 struct tsc2005
*ts
= container_of(work
, struct tsc2005
, penup_work
);
324 mutex_lock(&ts
->mutex
);
325 tsc2005_update_pen_state(ts
, 0, 0, 0);
326 mutex_unlock(&ts
->mutex
);
329 static void tsc2005_start_scan(struct tsc2005
*ts
)
331 tsc2005_write(ts
, TSC2005_REG_CFR0
, TSC2005_CFR0_INITVALUE
);
332 tsc2005_write(ts
, TSC2005_REG_CFR1
, TSC2005_CFR1_INITVALUE
);
333 tsc2005_write(ts
, TSC2005_REG_CFR2
, TSC2005_CFR2_INITVALUE
);
334 tsc2005_cmd(ts
, TSC2005_CMD_NORMAL
);
337 static void tsc2005_stop_scan(struct tsc2005
*ts
)
339 tsc2005_cmd(ts
, TSC2005_CMD_STOP
);
342 /* must be called with mutex held */
343 static void tsc2005_disable(struct tsc2005
*ts
)
345 if (ts
->disable_depth
++ != 0)
347 disable_irq(ts
->spi
->irq
);
349 del_timer_sync(&ts
->esd_timer
);
350 del_timer_sync(&ts
->penup_timer
);
351 tsc2005_stop_scan(ts
);
354 /* must be called with mutex held */
355 static void tsc2005_enable(struct tsc2005
*ts
)
357 if (--ts
->disable_depth
!= 0)
359 tsc2005_start_scan(ts
);
360 enable_irq(ts
->spi
->irq
);
361 if (!ts
->esd_timeout
)
363 mod_timer(&ts
->esd_timer
,
364 round_jiffies(jiffies
+ msecs_to_jiffies(ts
->esd_timeout
)));
367 static ssize_t
tsc2005_disable_show(struct device
*dev
,
368 struct device_attribute
*attr
, char *buf
)
370 struct spi_device
*spi
= to_spi_device(dev
);
371 struct tsc2005
*ts
= spi_get_drvdata(spi
);
373 return sprintf(buf
, "%u\n", ts
->disabled
);
376 static ssize_t
tsc2005_disable_store(struct device
*dev
,
377 struct device_attribute
*attr
,
378 const char *buf
, size_t count
)
380 struct spi_device
*spi
= to_spi_device(dev
);
381 struct tsc2005
*ts
= spi_get_drvdata(spi
);
385 if (strict_strtoul(buf
, 10, &res
) < 0)
389 mutex_lock(&ts
->mutex
);
390 if (i
== ts
->disabled
)
398 mutex_unlock(&ts
->mutex
);
401 static DEVICE_ATTR(disable
, 0664, tsc2005_disable_show
, tsc2005_disable_store
);
403 static ssize_t
tsc2005_selftest_show(struct device
*dev
,
404 struct device_attribute
*attr
,
407 struct spi_device
*spi
= to_spi_device(dev
);
408 struct tsc2005
*ts
= spi_get_drvdata(spi
);
414 if (!ts
->set_reset
) {
415 dev_warn(&ts
->spi
->dev
,
416 "unable to selftest: no reset function\n");
421 mutex_lock(&ts
->mutex
);
424 * Test TSC2005 communications via temp high register.
428 tsc2005_read(ts
, TSC2005_REG_TEMP_HIGH
, &temp_high_orig
);
429 temp_high_test
= (temp_high_orig
- 1) & MAX_12BIT
;
430 tsc2005_write(ts
, TSC2005_REG_TEMP_HIGH
, temp_high_test
);
431 tsc2005_read(ts
, TSC2005_REG_TEMP_HIGH
, &temp_high
);
432 if (temp_high
!= temp_high_test
) {
433 dev_warn(dev
, "selftest failed: %d != %d\n",
434 temp_high
, temp_high_test
);
440 usleep_range(100, 500); /* only 10us required */
444 /* test that the reset really happened */
445 tsc2005_read(ts
, TSC2005_REG_TEMP_HIGH
, &temp_high
);
446 if (temp_high
!= temp_high_orig
) {
447 dev_warn(dev
, "selftest failed after reset: %d != %d\n",
448 temp_high
, temp_high_orig
);
452 mutex_unlock(&ts
->mutex
);
455 return sprintf(buf
, "%u\n", result
);
457 static DEVICE_ATTR(selftest
, S_IRUGO
, tsc2005_selftest_show
, NULL
);
459 static void tsc2005_esd_timer(unsigned long data
)
461 struct tsc2005
*ts
= (struct tsc2005
*)data
;
463 schedule_work(&ts
->esd_work
);
466 static void tsc2005_esd_work(struct work_struct
*work
)
468 struct tsc2005
*ts
= container_of(work
, struct tsc2005
, esd_work
);
471 mutex_lock(&ts
->mutex
);
473 if (ts
->disable_depth
)
477 * If we cannot read our known value from configuration register 0 then
478 * reset the controller as if from power-up and start scanning again.
480 tsc2005_read(ts
, TSC2005_REG_CFR0
, &r
);
481 if ((r
^ TSC2005_CFR0_INITVALUE
) & TSC2005_CFR0_RW_MASK
) {
482 dev_info(&ts
->spi
->dev
, "TSC2005 not responding - resetting\n");
484 tsc2005_update_pen_state(ts
, 0, 0, 0);
485 usleep_range(100, 500); /* only 10us required */
487 tsc2005_start_scan(ts
);
490 /* re-arm the watchdog */
491 mod_timer(&ts
->esd_timer
,
492 round_jiffies(jiffies
+ msecs_to_jiffies(ts
->esd_timeout
)));
495 mutex_unlock(&ts
->mutex
);
498 static void __devinit
tsc2005_setup_spi_xfer(struct tsc2005
*ts
)
500 tsc2005_setup_read(&ts
->spi_x
, TSC2005_REG_X
, 0);
501 tsc2005_setup_read(&ts
->spi_y
, TSC2005_REG_Y
, 0);
502 tsc2005_setup_read(&ts
->spi_z1
, TSC2005_REG_Z1
, 0);
503 tsc2005_setup_read(&ts
->spi_z2
, TSC2005_REG_Z2
, 1);
505 spi_message_init(&ts
->spi_read_msg
);
506 spi_message_add_tail(&ts
->spi_x
.spi_xfer
, &ts
->spi_read_msg
);
507 spi_message_add_tail(&ts
->spi_y
.spi_xfer
, &ts
->spi_read_msg
);
508 spi_message_add_tail(&ts
->spi_z1
.spi_xfer
, &ts
->spi_read_msg
);
509 spi_message_add_tail(&ts
->spi_z2
.spi_xfer
, &ts
->spi_read_msg
);
512 static struct attribute
*tsc2005_attrs
[] = {
513 &dev_attr_disable
.attr
,
514 &dev_attr_selftest
.attr
,
518 static struct attribute_group tsc2005_attr_group
= {
519 .attrs
= tsc2005_attrs
,
522 static int __devinit
tsc2005_setup(struct tsc2005
*ts
,
523 struct tsc2005_platform_data
*pdata
)
533 mutex_init(&ts
->mutex
);
535 tsc2005_setup_spi_xfer(ts
);
537 init_timer(&ts
->penup_timer
);
538 setup_timer(&ts
->penup_timer
, tsc2005_penup_timer
, (unsigned long)ts
);
539 INIT_WORK(&ts
->penup_work
, tsc2005_penup_work
);
541 fudge_x
= pdata
->ts_x_fudge
? : 4;
542 fudge_y
= pdata
->ts_y_fudge
? : 8;
543 fudge_p
= pdata
->ts_pressure_fudge
? : 2;
544 x_max
= pdata
->ts_x_max
? : MAX_12BIT
;
545 y_max
= pdata
->ts_y_max
? : MAX_12BIT
;
546 p_max
= pdata
->ts_pressure_max
? : MAX_12BIT
;
547 ts
->x_plate_ohm
= pdata
->ts_x_plate_ohm
? : 280;
548 ts
->esd_timeout
= pdata
->esd_timeout_ms
;
549 ts
->set_reset
= pdata
->set_reset
;
551 ts
->idev
= input_allocate_device();
552 if (ts
->idev
== NULL
)
554 ts
->idev
->name
= "TSC2005 touchscreen";
555 snprintf(ts
->phys
, sizeof(ts
->phys
), "%s/input-ts",
556 dev_name(&ts
->spi
->dev
));
557 ts
->idev
->phys
= ts
->phys
;
558 ts
->idev
->evbit
[0] = BIT(EV_ABS
) | BIT(EV_KEY
);
559 ts
->idev
->absbit
[0] = BIT(ABS_X
) | BIT(ABS_Y
) | BIT(ABS_PRESSURE
);
560 ts
->idev
->keybit
[BIT_WORD(BTN_TOUCH
)] = BIT_MASK(BTN_TOUCH
);
562 input_set_abs_params(ts
->idev
, ABS_X
, 0, x_max
, fudge_x
, 0);
563 input_set_abs_params(ts
->idev
, ABS_Y
, 0, y_max
, fudge_y
, 0);
564 input_set_abs_params(ts
->idev
, ABS_PRESSURE
, 0, p_max
, fudge_p
, 0);
566 r
= request_threaded_irq(ts
->spi
->irq
, tsc2005_irq_handler
,
567 tsc2005_irq_thread
, IRQF_TRIGGER_RISING
,
570 dev_err(&ts
->spi
->dev
, "request_threaded_irq(): %d\n", r
);
573 set_irq_wake(ts
->spi
->irq
, 1);
575 r
= input_register_device(ts
->idev
);
577 dev_err(&ts
->spi
->dev
, "input_register_device(): %d\n", r
);
581 r
= sysfs_create_group(&ts
->spi
->dev
.kobj
, &tsc2005_attr_group
);
583 dev_warn(&ts
->spi
->dev
, "sysfs entry creation failed: %d\n", r
);
585 tsc2005_start_scan(ts
);
587 if (!ts
->esd_timeout
|| !ts
->set_reset
)
590 /* start the optional ESD watchdog */
591 setup_timer(&ts
->esd_timer
, tsc2005_esd_timer
, (unsigned long)ts
);
592 INIT_WORK(&ts
->esd_work
, tsc2005_esd_work
);
593 mod_timer(&ts
->esd_timer
,
594 round_jiffies(jiffies
+ msecs_to_jiffies(ts
->esd_timeout
)));
600 free_irq(ts
->spi
->irq
, ts
);
603 input_free_device(ts
->idev
);
607 static int __devinit
tsc2005_probe(struct spi_device
*spi
)
609 struct tsc2005_platform_data
*pdata
= spi
->dev
.platform_data
;
614 dev_dbg(&spi
->dev
, "no irq\n");
619 dev_dbg(&spi
->dev
, "no platform data\n");
623 ts
= kzalloc(sizeof(*ts
), GFP_KERNEL
);
627 spi_set_drvdata(spi
, ts
);
629 spi
->dev
.power
.power_state
= PMSG_ON
;
630 spi
->mode
= SPI_MODE_0
;
631 spi
->bits_per_word
= 8;
632 if (!spi
->max_speed_hz
)
633 spi
->max_speed_hz
= TSC2005_SPI_MAX_SPEED_HZ
;
636 r
= tsc2005_setup(ts
, pdata
);
642 static int __devexit
tsc2005_remove(struct spi_device
*spi
)
644 struct tsc2005
*ts
= spi_get_drvdata(spi
);
646 mutex_lock(&ts
->mutex
);
648 mutex_unlock(&ts
->mutex
);
651 del_timer_sync(&ts
->esd_timer
);
652 del_timer_sync(&ts
->penup_timer
);
654 flush_work(&ts
->esd_work
);
655 flush_work(&ts
->penup_work
);
657 sysfs_remove_group(&ts
->spi
->dev
.kobj
, &tsc2005_attr_group
);
658 free_irq(ts
->spi
->irq
, ts
);
659 input_unregister_device(ts
->idev
);
665 #ifdef CONFIG_PM_SLEEP
666 static int tsc2005_suspend(struct device
*dev
)
668 struct spi_device
*spi
= to_spi_device(dev
);
669 struct tsc2005
*ts
= spi_get_drvdata(spi
);
671 mutex_lock(&ts
->mutex
);
673 mutex_unlock(&ts
->mutex
);
678 static int tsc2005_resume(struct device
*dev
)
680 struct spi_device
*spi
= to_spi_device(dev
);
681 struct tsc2005
*ts
= spi_get_drvdata(spi
);
683 mutex_lock(&ts
->mutex
);
685 mutex_unlock(&ts
->mutex
);
691 static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops
, tsc2005_suspend
, tsc2005_resume
);
693 static struct spi_driver tsc2005_driver
= {
696 .owner
= THIS_MODULE
,
697 .pm
= &tsc2005_pm_ops
,
699 .probe
= tsc2005_probe
,
700 .remove
= __devexit_p(tsc2005_remove
),
703 static int __init
tsc2005_init(void)
705 printk(KERN_INFO
"TSC2005 driver initializing\n");
706 return spi_register_driver(&tsc2005_driver
);
708 module_init(tsc2005_init
);
710 static void __exit
tsc2005_exit(void)
712 spi_unregister_driver(&tsc2005_driver
);
714 module_exit(tsc2005_exit
);
716 MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
717 MODULE_LICENSE("GPL");
718 MODULE_ALIAS("platform:tsc2005");