25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/input.h>
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
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 | \
84 #define TSC2005_CFR0_RW_MASK 0x3fff
87 #define TSC2005_CFR1_BATCHDELAY_4MS 0x0003
88 #define TSC2005_CFR1_INITVALUE TSC2005_CFR1_BATCHDELAY_4MS
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
160 spi_message_init(&
msg);
161 spi_message_add_tail(&xfer, &
msg);
165 dev_err(&ts->
spi->dev,
"%s: failed, command: %x, error: %d\n",
166 __func__, cmd, error);
184 spi_message_init(&
msg);
185 spi_message_add_tail(&xfer, &
msg);
190 "%s: failed, register: %x, value: %x, error: %d\n",
191 __func__, reg, value, error);
200 memset(rd, 0,
sizeof(*rd));
210 static int tsc2005_read(
struct tsc2005 *ts,
u8 reg,
u16 *value)
216 tsc2005_setup_read(&spi_rd, reg,
true);
218 spi_message_init(&
msg);
219 spi_message_add_tail(&spi_rd.spi_xfer, &
msg);
225 *value = spi_rd.spi_rx;
229 static void tsc2005_update_pen_state(
struct tsc2005 *ts,
230 int x,
int y,
int pressure)
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)
256 unsigned int pressure;
266 x = ts->
spi_x.spi_rx;
267 y = ts->
spi_y.spi_rx;
299 pressure = x * (z2 - z1) / z1;
306 tsc2005_update_pen_state(ts, x, y, pressure);
310 spin_unlock_irqrestore(&ts->
lock, flags);
317 static void tsc2005_penup_timer(
unsigned long data)
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)
335 static void tsc2005_stop_scan(
struct tsc2005 *ts)
341 static void __tsc2005_disable(
struct tsc2005 *ts)
343 tsc2005_stop_scan(ts);
354 static void __tsc2005_enable(
struct tsc2005 *ts)
356 tsc2005_start_scan(ts);
372 struct tsc2005 *ts = spi_get_drvdata(spi);
384 __tsc2005_disable(ts);
388 dev_warn(dev,
"selftest failed: read error %d\n", error);
393 temp_high_test = (temp_high_orig - 1) &
MAX_12BIT;
397 dev_warn(dev,
"selftest failed: write error %d\n", error);
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);
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);
443 return sprintf(buf,
"%d\n", success);
448 static struct attribute *tsc2005_attrs[] = {
449 &dev_attr_selftest.attr,
458 struct tsc2005 *ts = spi_get_drvdata(spi);
461 if (attr == &dev_attr_selftest.attr) {
470 .is_visible = tsc2005_attr_is_visible,
471 .attrs = tsc2005_attrs,
505 dev_info(&ts->
spi->dev,
"TSC2005 not responding - resetting\n");
510 tsc2005_update_pen_state(ts, 0, 0, 0);
517 tsc2005_start_scan(ts);
528 static int tsc2005_open(
struct input_dev *
input)
530 struct tsc2005 *ts = input_get_drvdata(input);
535 __tsc2005_enable(ts);
544 static void tsc2005_close(
struct input_dev *input)
546 struct tsc2005 *ts = input_get_drvdata(input);
551 __tsc2005_disable(ts);
576 struct input_dev *input_dev;
577 unsigned int max_x, max_y, max_p;
578 unsigned int fudge_x, fudge_y, fudge_p;
608 input_dev = input_allocate_device();
609 if (!ts || !input_dev) {
615 ts->
idev = input_dev;
628 tsc2005_setup_spi_xfer(ts);
631 "%s/input-ts", dev_name(&spi->
dev));
633 input_dev->name =
"TSC2005 touchscreen";
634 input_dev->phys = ts->
phys;
635 input_dev->id.bustype =
BUS_SPI;
636 input_dev->dev.parent = &spi->
dev;
640 input_set_abs_params(input_dev,
ABS_X, 0, max_x, fudge_x, 0);
641 input_set_abs_params(input_dev,
ABS_Y, 0, max_y, fudge_y, 0);
642 input_set_abs_params(input_dev,
ABS_PRESSURE, 0, max_p, fudge_p, 0);
644 input_dev->open = tsc2005_open;
645 input_dev->close = tsc2005_close;
647 input_set_drvdata(input_dev, ts);
650 tsc2005_stop_scan(ts);
656 dev_err(&spi->
dev,
"Failed to request irq, err: %d\n", error);
660 spi_set_drvdata(spi, ts);
664 "Failed to create sysfs attributes, err: %d\n", error);
665 goto err_clear_drvdata;
668 error = input_register_device(ts->
idev);
671 "Failed to register input device, err: %d\n", error);
672 goto err_remove_sysfs;
681 spi_set_drvdata(spi,
NULL);
684 input_free_device(input_dev);
691 struct tsc2005 *ts = spi_get_drvdata(spi);
696 input_unregister_device(ts->
idev);
699 spi_set_drvdata(spi,
NULL);
703 #ifdef CONFIG_PM_SLEEP
704 static int tsc2005_suspend(
struct device *dev)
707 struct tsc2005 *ts = spi_get_drvdata(spi);
712 __tsc2005_disable(ts);
721 static int tsc2005_resume(
struct device *dev)
724 struct tsc2005 *ts = spi_get_drvdata(spi);
729 __tsc2005_enable(ts);
745 .pm = &tsc2005_pm_ops,
747 .probe = tsc2005_probe,