blob: 3e1c9c297f33051783b85f983c244775a4e44264 [file] [log] [blame]
Lauri Leukkunen37bd4462011-03-16 22:07:36 -07001/*
2 * TSC2005 touchscreen driver
3 *
4 * Copyright (C) 2006-2010 Nokia Corporation
5 *
6 * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com>
7 * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com>
8 *
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.
13 *
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.
18 *
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
22 *
23 */
24
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>
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -070030#include <linux/pm.h>
Lauri Leukkunen37bd4462011-03-16 22:07:36 -070031#include <linux/spi/spi.h>
32#include <linux/spi/tsc2005.h>
33
34/*
35 * The touchscreen interface operates as follows:
36 *
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
42 * values.
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.
47 *
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.
51 */
52
53/* control byte 1 */
54#define TSC2005_CMD 0x80
55#define TSC2005_CMD_NORMAL 0x00
56#define TSC2005_CMD_STOP 0x01
57#define TSC2005_CMD_12BIT 0x04
58
59/* control byte 0 */
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
70
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 | \
81 TSC2005_CFR0_PENMODE)
82
83/* bits common to both read and write of configuration register 0 */
84#define TSC2005_CFR0_RW_MASK 0x3fff
85
86/* configuration register 1 */
87#define TSC2005_CFR1_BATCHDELAY_4MS 0x0003
88#define TSC2005_CFR1_INITVALUE TSC2005_CFR1_BATCHDELAY_4MS
89
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 | \
100 TSC2005_CFR2_AVG_7)
101
102#define MAX_12BIT 0xfff
103#define TSC2005_SPI_MAX_SPEED_HZ 10000000
104#define TSC2005_PENUP_TIME_MS 40
105
106struct tsc2005_spi_rd {
107 struct spi_transfer spi_xfer;
108 u32 spi_tx;
109 u32 spi_rx;
110};
111
112struct tsc2005 {
113 struct spi_device *spi;
114
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;
120
121 struct input_dev *idev;
122 char phys[32];
123
124 struct mutex mutex;
125
126 /* raw copy of previous x,y,z */
127 int in_x;
128 int in_y;
129 int in_z1;
130 int in_z2;
131
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700132 spinlock_t lock;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700133 struct timer_list penup_timer;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700134
135 unsigned int esd_timeout;
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700136 struct delayed_work esd_work;
137 unsigned long last_valid_interrupt;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700138
139 unsigned int x_plate_ohm;
140
141 bool disabled;
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700142 bool opened;
143 bool suspended;
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700144
145 bool pen_down;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700146
147 void (*set_reset)(bool enable);
148};
149
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700150static int tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700151{
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700152 u8 tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd;
153 struct spi_transfer xfer = {
154 .tx_buf = &tx,
155 .len = 1,
156 .bits_per_word = 8,
157 };
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700158 struct spi_message msg;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700159 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700160
161 spi_message_init(&msg);
162 spi_message_add_tail(&xfer, &msg);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700163
164 error = spi_sync(ts->spi, &msg);
165 if (error) {
166 dev_err(&ts->spi->dev, "%s: failed, command: %x, error: %d\n",
167 __func__, cmd, error);
168 return error;
169 }
170
171 return 0;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700172}
173
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700174static int tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700175{
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700176 u32 tx = ((reg | TSC2005_REG_PND0) << 16) | value;
177 struct spi_transfer xfer = {
178 .tx_buf = &tx,
179 .len = 4,
180 .bits_per_word = 24,
181 };
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700182 struct spi_message msg;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700183 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700184
185 spi_message_init(&msg);
186 spi_message_add_tail(&xfer, &msg);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700187
188 error = spi_sync(ts->spi, &msg);
189 if (error) {
190 dev_err(&ts->spi->dev,
191 "%s: failed, register: %x, value: %x, error: %d\n",
192 __func__, reg, value, error);
193 return error;
194 }
195
196 return 0;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700197}
198
199static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last)
200{
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700201 memset(rd, 0, sizeof(*rd));
202
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700203 rd->spi_tx = (reg | TSC2005_REG_READ) << 16;
204 rd->spi_xfer.tx_buf = &rd->spi_tx;
205 rd->spi_xfer.rx_buf = &rd->spi_rx;
206 rd->spi_xfer.len = 4;
207 rd->spi_xfer.bits_per_word = 24;
208 rd->spi_xfer.cs_change = !last;
209}
210
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700211static int tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700212{
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700213 struct tsc2005_spi_rd spi_rd;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700214 struct spi_message msg;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700215 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700216
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700217 tsc2005_setup_read(&spi_rd, reg, true);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700218
219 spi_message_init(&msg);
220 spi_message_add_tail(&spi_rd.spi_xfer, &msg);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700221
222 error = spi_sync(ts->spi, &msg);
223 if (error)
224 return error;
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700225
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700226 *value = spi_rd.spi_rx;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700227 return 0;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700228}
229
230static void tsc2005_update_pen_state(struct tsc2005 *ts,
231 int x, int y, int pressure)
232{
233 if (pressure) {
234 input_report_abs(ts->idev, ABS_X, x);
235 input_report_abs(ts->idev, ABS_Y, y);
236 input_report_abs(ts->idev, ABS_PRESSURE, pressure);
237 if (!ts->pen_down) {
238 input_report_key(ts->idev, BTN_TOUCH, !!pressure);
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700239 ts->pen_down = true;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700240 }
241 } else {
242 input_report_abs(ts->idev, ABS_PRESSURE, 0);
243 if (ts->pen_down) {
244 input_report_key(ts->idev, BTN_TOUCH, 0);
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700245 ts->pen_down = false;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700246 }
247 }
248 input_sync(ts->idev);
249 dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
250 pressure);
251}
252
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700253static irqreturn_t tsc2005_irq_thread(int irq, void *_ts)
254{
255 struct tsc2005 *ts = _ts;
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700256 unsigned long flags;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700257 unsigned int pressure;
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700258 u32 x, y;
259 u32 z1, z2;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700260 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700261
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700262 /* read the coordinates */
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700263 error = spi_sync(ts->spi, &ts->spi_read_msg);
264 if (unlikely(error))
265 goto out;
266
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700267 x = ts->spi_x.spi_rx;
268 y = ts->spi_y.spi_rx;
269 z1 = ts->spi_z1.spi_rx;
270 z2 = ts->spi_z2.spi_rx;
271
272 /* validate position */
273 if (unlikely(x > MAX_12BIT || y > MAX_12BIT))
274 goto out;
275
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700276 /* Skip reading if the pressure components are out of range */
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700277 if (unlikely(z1 == 0 || z2 > MAX_12BIT || z1 >= z2))
278 goto out;
279
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700280 /*
281 * Skip point if this is a pen down with the exact same values as
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700282 * the value before pen-up - that implies SPI fed us stale data
283 */
284 if (!ts->pen_down &&
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700285 ts->in_x == x && ts->in_y == y &&
286 ts->in_z1 == z1 && ts->in_z2 == z2) {
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700287 goto out;
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700288 }
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700289
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700290 /*
291 * At this point we are happy we have a valid and useful reading.
292 * Remember it for later comparisons. We may now begin downsampling.
293 */
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700294 ts->in_x = x;
295 ts->in_y = y;
296 ts->in_z1 = z1;
297 ts->in_z2 = z2;
298
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700299 /* Compute touch pressure resistance using equation #1 */
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700300 pressure = x * (z2 - z1) / z1;
301 pressure = pressure * ts->x_plate_ohm / 4096;
302 if (unlikely(pressure > MAX_12BIT))
303 goto out;
304
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700305 spin_lock_irqsave(&ts->lock, flags);
306
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700307 tsc2005_update_pen_state(ts, x, y, pressure);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700308 mod_timer(&ts->penup_timer,
309 jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
310
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700311 spin_unlock_irqrestore(&ts->lock, flags);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700312
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700313 ts->last_valid_interrupt = jiffies;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700314out:
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700315 return IRQ_HANDLED;
316}
317
318static void tsc2005_penup_timer(unsigned long data)
319{
320 struct tsc2005 *ts = (struct tsc2005 *)data;
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700321 unsigned long flags;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700322
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700323 spin_lock_irqsave(&ts->lock, flags);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700324 tsc2005_update_pen_state(ts, 0, 0, 0);
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700325 spin_unlock_irqrestore(&ts->lock, flags);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700326}
327
328static void tsc2005_start_scan(struct tsc2005 *ts)
329{
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);
334}
335
336static void tsc2005_stop_scan(struct tsc2005 *ts)
337{
338 tsc2005_cmd(ts, TSC2005_CMD_STOP);
339}
340
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700341/* must be called with ts->mutex held */
342static void __tsc2005_disable(struct tsc2005 *ts)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700343{
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700344 tsc2005_stop_scan(ts);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700345
346 disable_irq(ts->spi->irq);
347 del_timer_sync(&ts->penup_timer);
348
349 cancel_delayed_work_sync(&ts->esd_work);
350
351 enable_irq(ts->spi->irq);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700352}
353
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700354/* must be called with ts->mutex held */
355static void __tsc2005_enable(struct tsc2005 *ts)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700356{
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700357 tsc2005_start_scan(ts);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700358
359 if (ts->esd_timeout && ts->set_reset) {
360 ts->last_valid_interrupt = jiffies;
361 schedule_delayed_work(&ts->esd_work,
362 round_jiffies(jiffies +
363 msecs_to_jiffies(ts->esd_timeout)));
364 }
365
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700366}
367
368static ssize_t tsc2005_disable_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
370{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700371 struct spi_device *spi = to_spi_device(dev);
372 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700373
374 return sprintf(buf, "%u\n", ts->disabled);
375}
376
377static ssize_t tsc2005_disable_store(struct device *dev,
378 struct device_attribute *attr,
379 const char *buf, size_t count)
380{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700381 struct spi_device *spi = to_spi_device(dev);
382 struct tsc2005 *ts = spi_get_drvdata(spi);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700383 unsigned long val;
384 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700385
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700386 error = strict_strtoul(buf, 10, &val);
387 if (error)
388 return error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700389
390 mutex_lock(&ts->mutex);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700391
392 if (!ts->suspended && ts->opened) {
393 if (val) {
394 if (!ts->disabled)
395 __tsc2005_disable(ts);
396 } else {
397 if (ts->disabled)
398 __tsc2005_enable(ts);
399 }
400 }
401
402 ts->disabled = !!val;
403
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700404 mutex_unlock(&ts->mutex);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700405
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700406 return count;
407}
408static DEVICE_ATTR(disable, 0664, tsc2005_disable_show, tsc2005_disable_store);
409
410static ssize_t tsc2005_selftest_show(struct device *dev,
411 struct device_attribute *attr,
412 char *buf)
413{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700414 struct spi_device *spi = to_spi_device(dev);
415 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700416 u16 temp_high;
417 u16 temp_high_orig;
418 u16 temp_high_test;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700419 bool success = true;
420 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700421
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700422 mutex_lock(&ts->mutex);
423
424 /*
425 * Test TSC2005 communications via temp high register.
426 */
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700427 __tsc2005_disable(ts);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700428
429 error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig);
430 if (error) {
431 dev_warn(dev, "selftest failed: read error %d\n", error);
432 success = false;
433 goto out;
434 }
435
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700436 temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700437
438 error = tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test);
439 if (error) {
440 dev_warn(dev, "selftest failed: write error %d\n", error);
441 success = false;
442 goto out;
443 }
444
445 error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
446 if (error) {
447 dev_warn(dev, "selftest failed: read error %d after write\n",
448 error);
449 success = false;
450 goto out;
451 }
452
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700453 if (temp_high != temp_high_test) {
454 dev_warn(dev, "selftest failed: %d != %d\n",
455 temp_high, temp_high_test);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700456 success = false;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700457 }
458
459 /* hardware reset */
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700460 ts->set_reset(false);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700461 usleep_range(100, 500); /* only 10us required */
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700462 ts->set_reset(true);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700463
464 if (!success)
465 goto out;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700466
467 /* test that the reset really happened */
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700468 error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
469 if (error) {
470 dev_warn(dev, "selftest failed: read error %d after reset\n",
471 error);
472 success = false;
473 goto out;
474 }
475
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700476 if (temp_high != temp_high_orig) {
477 dev_warn(dev, "selftest failed after reset: %d != %d\n",
478 temp_high, temp_high_orig);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700479 success = false;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700480 }
481
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700482out:
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700483 __tsc2005_enable(ts);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700484 mutex_unlock(&ts->mutex);
485
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700486 return sprintf(buf, "%d\n", success);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700487}
Dmitry Torokhov8dbcc432011-03-16 22:10:37 -0700488
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700489static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL);
490
Dmitry Torokhov8dbcc432011-03-16 22:10:37 -0700491static struct attribute *tsc2005_attrs[] = {
492 &dev_attr_disable.attr,
493 &dev_attr_selftest.attr,
494 NULL
495};
496
497static mode_t tsc2005_attr_is_visible(struct kobject *kobj,
498 struct attribute *attr, int n)
499{
500 struct device *dev = container_of(kobj, struct device, kobj);
501 struct spi_device *spi = to_spi_device(dev);
502 struct tsc2005 *ts = spi_get_drvdata(spi);
503 mode_t mode = attr->mode;
504
505 if (attr == &dev_attr_selftest.attr) {
506 if (!ts->set_reset)
507 mode = 0;
508 }
509
510 return mode;
511}
512
513static const struct attribute_group tsc2005_attr_group = {
514 .is_visible = tsc2005_attr_is_visible,
515 .attrs = tsc2005_attrs,
516};
517
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700518static void tsc2005_esd_work(struct work_struct *work)
519{
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700520 struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work.work);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700521 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700522 u16 r;
523
524 mutex_lock(&ts->mutex);
525
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700526 if (time_is_after_jiffies(ts->last_valid_interrupt +
527 msecs_to_jiffies(ts->esd_timeout)))
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700528 goto out;
529
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700530 /* We should be able to read register without disabling interrupts. */
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700531 error = tsc2005_read(ts, TSC2005_REG_CFR0, &r);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700532 if (!error &&
533 !((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK)) {
534 goto out;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700535 }
536
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700537 /*
538 * If we could not read our known value from configuration register 0
539 * then we should reset the controller as if from power-up and start
540 * scanning again.
541 */
542 dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");
543
544 disable_irq(ts->spi->irq);
545 del_timer_sync(&ts->penup_timer);
546
547 tsc2005_update_pen_state(ts, 0, 0, 0);
548
549 ts->set_reset(false);
550 usleep_range(100, 500); /* only 10us required */
551 ts->set_reset(true);
552
553 enable_irq(ts->spi->irq);
554 tsc2005_start_scan(ts);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700555
556out:
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700557 /* re-arm the watchdog */
558 schedule_delayed_work(&ts->esd_work,
559 round_jiffies(jiffies +
560 msecs_to_jiffies(ts->esd_timeout)));
561 mutex_unlock(&ts->mutex);
562}
563
564static int tsc2005_open(struct input_dev *input)
565{
566 struct tsc2005 *ts = input_get_drvdata(input);
567
568 mutex_lock(&ts->mutex);
569
570 if (!ts->suspended && !ts->disabled)
571 __tsc2005_enable(ts);
572
573 ts->opened = true;
574
575 mutex_unlock(&ts->mutex);
576
577 return 0;
578}
579
580static void tsc2005_close(struct input_dev *input)
581{
582 struct tsc2005 *ts = input_get_drvdata(input);
583
584 mutex_lock(&ts->mutex);
585
586 if (!ts->suspended && !ts->disabled)
587 __tsc2005_disable(ts);
588
589 ts->opened = false;
590
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700591 mutex_unlock(&ts->mutex);
592}
593
594static void __devinit tsc2005_setup_spi_xfer(struct tsc2005 *ts)
595{
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700596 tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, false);
597 tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, false);
598 tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, false);
599 tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, true);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700600
601 spi_message_init(&ts->spi_read_msg);
602 spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg);
603 spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg);
604 spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg);
605 spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg);
606}
607
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700608static int __devinit tsc2005_probe(struct spi_device *spi)
609{
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700610 const struct tsc2005_platform_data *pdata = spi->dev.platform_data;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700611 struct tsc2005 *ts;
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700612 struct input_dev *input_dev;
613 unsigned int max_x, max_y, max_p;
614 unsigned int fudge_x, fudge_y, fudge_p;
615 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700616
617 if (!pdata) {
618 dev_dbg(&spi->dev, "no platform data\n");
619 return -ENODEV;
620 }
621
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700622 fudge_x = pdata->ts_x_fudge ? : 4;
623 fudge_y = pdata->ts_y_fudge ? : 8;
624 fudge_p = pdata->ts_pressure_fudge ? : 2;
625 max_x = pdata->ts_x_max ? : MAX_12BIT;
626 max_y = pdata->ts_y_max ? : MAX_12BIT;
627 max_p = pdata->ts_pressure_max ? : MAX_12BIT;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700628
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700629 if (spi->irq <= 0) {
630 dev_dbg(&spi->dev, "no irq\n");
631 return -ENODEV;
632 }
633
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700634 spi->mode = SPI_MODE_0;
635 spi->bits_per_word = 8;
636 if (!spi->max_speed_hz)
637 spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700638
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700639 error = spi_setup(spi);
640 if (error)
641 return error;
642
643 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
644 input_dev = input_allocate_device();
645 if (!ts || !input_dev) {
646 error = -ENOMEM;
647 goto err_free_mem;
Dmitry Torokhov2721a892011-03-16 22:09:09 -0700648 }
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700649
650 ts->spi = spi;
651 ts->idev = input_dev;
652
653 ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
654 ts->esd_timeout = pdata->esd_timeout_ms;
655 ts->set_reset = pdata->set_reset;
656
657 mutex_init(&ts->mutex);
658
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700659 spin_lock_init(&ts->lock);
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700660 setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700661
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700662 INIT_DELAYED_WORK(&ts->esd_work, tsc2005_esd_work);
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700663
664 tsc2005_setup_spi_xfer(ts);
665
666 snprintf(ts->phys, sizeof(ts->phys),
667 "%s/input-ts", dev_name(&spi->dev));
668
669 input_dev->name = "TSC2005 touchscreen";
670 input_dev->phys = ts->phys;
671 input_dev->id.bustype = BUS_SPI;
672 input_dev->dev.parent = &spi->dev;
673 input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
674 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
675
676 input_set_abs_params(input_dev, ABS_X, 0, max_x, fudge_x, 0);
677 input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0);
678 input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0);
679
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700680 input_dev->open = tsc2005_open;
681 input_dev->close = tsc2005_close;
682
683 input_set_drvdata(input_dev, ts);
684
685 /* Ensure the touchscreen is off */
686 tsc2005_stop_scan(ts);
687
Dmitry Torokhovdacb6502011-03-16 22:11:14 -0700688 error = request_threaded_irq(spi->irq, NULL, tsc2005_irq_thread,
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700689 IRQF_TRIGGER_RISING, "tsc2005", ts);
690 if (error) {
691 dev_err(&spi->dev, "Failed to request irq, err: %d\n", error);
692 goto err_free_mem;
693 }
694
695 spi_set_drvdata(spi, ts);
696 error = sysfs_create_group(&spi->dev.kobj, &tsc2005_attr_group);
697 if (error) {
698 dev_err(&spi->dev,
699 "Failed to create sysfs attributes, err: %d\n", error);
700 goto err_clear_drvdata;
701 }
702
703 error = input_register_device(ts->idev);
704 if (error) {
705 dev_err(&spi->dev,
706 "Failed to register input device, err: %d\n", error);
707 goto err_remove_sysfs;
708 }
709
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700710 set_irq_wake(spi->irq, 1);
711 return 0;
712
713err_remove_sysfs:
714 sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);
715err_clear_drvdata:
716 spi_set_drvdata(spi, NULL);
717 free_irq(spi->irq, ts);
718err_free_mem:
719 input_free_device(input_dev);
720 kfree(ts);
721 return error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700722}
723
724static int __devexit tsc2005_remove(struct spi_device *spi)
725{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700726 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700727
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700728 sysfs_remove_group(&ts->spi->dev.kobj, &tsc2005_attr_group);
729
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700730 free_irq(ts->spi->irq, ts);
731 input_unregister_device(ts->idev);
732 kfree(ts);
733
Dmitry Torokhov2721a892011-03-16 22:09:09 -0700734 spi_set_drvdata(spi, NULL);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700735 return 0;
736}
737
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700738#ifdef CONFIG_PM_SLEEP
739static int tsc2005_suspend(struct device *dev)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700740{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700741 struct spi_device *spi = to_spi_device(dev);
742 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700743
744 mutex_lock(&ts->mutex);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700745
746 if (!ts->suspended && !ts->disabled && ts->opened)
747 __tsc2005_disable(ts);
748
749 ts->suspended = true;
750
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700751 mutex_unlock(&ts->mutex);
752
753 return 0;
754}
755
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700756static int tsc2005_resume(struct device *dev)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700757{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700758 struct spi_device *spi = to_spi_device(dev);
759 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700760
761 mutex_lock(&ts->mutex);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700762
763 if (ts->suspended && !ts->disabled && ts->opened)
764 __tsc2005_enable(ts);
765
766 ts->suspended = false;
767
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700768 mutex_unlock(&ts->mutex);
769
770 return 0;
771}
772#endif
773
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700774static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume);
775
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700776static struct spi_driver tsc2005_driver = {
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700777 .driver = {
778 .name = "tsc2005",
779 .owner = THIS_MODULE,
780 .pm = &tsc2005_pm_ops,
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700781 },
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700782 .probe = tsc2005_probe,
783 .remove = __devexit_p(tsc2005_remove),
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700784};
785
786static int __init tsc2005_init(void)
787{
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700788 return spi_register_driver(&tsc2005_driver);
789}
790module_init(tsc2005_init);
791
792static void __exit tsc2005_exit(void)
793{
794 spi_unregister_driver(&tsc2005_driver);
795}
796module_exit(tsc2005_exit);
797
798MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
Dmitry Torokhovb88aa492011-03-16 22:09:03 -0700799MODULE_DESCRIPTION("TSC2005 Touchscreen Driver");
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700800MODULE_LICENSE("GPL");