blob: 7a76533901870bcc7c99e3824597d5abdb783471 [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;
136 struct timer_list esd_timer;
137 struct work_struct esd_work;
138
139 unsigned int x_plate_ohm;
140
141 bool disabled;
142 unsigned int disable_depth;
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700143
144 bool pen_down;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700145
146 void (*set_reset)(bool enable);
147};
148
149static void tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
150{
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700151 u8 tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd;
152 struct spi_transfer xfer = {
153 .tx_buf = &tx,
154 .len = 1,
155 .bits_per_word = 8,
156 };
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700157 struct spi_message msg;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700158
159 spi_message_init(&msg);
160 spi_message_add_tail(&xfer, &msg);
161 spi_sync(ts->spi, &msg);
162}
163
164static void tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
165{
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700166 u32 tx = ((reg | TSC2005_REG_PND0) << 16) | value;
167 struct spi_transfer xfer = {
168 .tx_buf = &tx,
169 .len = 4,
170 .bits_per_word = 24,
171 };
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700172 struct spi_message msg;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700173
174 spi_message_init(&msg);
175 spi_message_add_tail(&xfer, &msg);
176 spi_sync(ts->spi, &msg);
177}
178
179static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last)
180{
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700181 memset(rd, 0, sizeof(*rd));
182
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700183 rd->spi_tx = (reg | TSC2005_REG_READ) << 16;
184 rd->spi_xfer.tx_buf = &rd->spi_tx;
185 rd->spi_xfer.rx_buf = &rd->spi_rx;
186 rd->spi_xfer.len = 4;
187 rd->spi_xfer.bits_per_word = 24;
188 rd->spi_xfer.cs_change = !last;
189}
190
191static void tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value)
192{
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700193 struct tsc2005_spi_rd spi_rd;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700194 struct spi_message msg;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700195
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700196 tsc2005_setup_read(&spi_rd, reg, true);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700197
198 spi_message_init(&msg);
199 spi_message_add_tail(&spi_rd.spi_xfer, &msg);
200 spi_sync(ts->spi, &msg);
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700201
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700202 *value = spi_rd.spi_rx;
203}
204
205static void tsc2005_update_pen_state(struct tsc2005 *ts,
206 int x, int y, int pressure)
207{
208 if (pressure) {
209 input_report_abs(ts->idev, ABS_X, x);
210 input_report_abs(ts->idev, ABS_Y, y);
211 input_report_abs(ts->idev, ABS_PRESSURE, pressure);
212 if (!ts->pen_down) {
213 input_report_key(ts->idev, BTN_TOUCH, !!pressure);
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700214 ts->pen_down = true;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700215 }
216 } else {
217 input_report_abs(ts->idev, ABS_PRESSURE, 0);
218 if (ts->pen_down) {
219 input_report_key(ts->idev, BTN_TOUCH, 0);
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700220 ts->pen_down = false;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700221 }
222 }
223 input_sync(ts->idev);
224 dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
225 pressure);
226}
227
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700228static irqreturn_t tsc2005_irq_thread(int irq, void *_ts)
229{
230 struct tsc2005 *ts = _ts;
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700231 unsigned long flags;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700232 unsigned int pressure;
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700233 u32 x, y;
234 u32 z1, z2;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700235
236 mutex_lock(&ts->mutex);
237
238 if (unlikely(ts->disable_depth))
239 goto out;
240
241 /* read the coordinates */
242 spi_sync(ts->spi, &ts->spi_read_msg);
243 x = ts->spi_x.spi_rx;
244 y = ts->spi_y.spi_rx;
245 z1 = ts->spi_z1.spi_rx;
246 z2 = ts->spi_z2.spi_rx;
247
248 /* validate position */
249 if (unlikely(x > MAX_12BIT || y > MAX_12BIT))
250 goto out;
251
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700252 /* Skip reading if the pressure components are out of range */
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700253 if (unlikely(z1 == 0 || z2 > MAX_12BIT || z1 >= z2))
254 goto out;
255
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700256 /*
257 * Skip point if this is a pen down with the exact same values as
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700258 * the value before pen-up - that implies SPI fed us stale data
259 */
260 if (!ts->pen_down &&
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700261 ts->in_x == x && ts->in_y == y &&
262 ts->in_z1 == z1 && ts->in_z2 == z2) {
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700263 goto out;
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700264 }
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700265
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700266 /*
267 * At this point we are happy we have a valid and useful reading.
268 * Remember it for later comparisons. We may now begin downsampling.
269 */
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700270 ts->in_x = x;
271 ts->in_y = y;
272 ts->in_z1 = z1;
273 ts->in_z2 = z2;
274
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700275 /* Compute touch pressure resistance using equation #1 */
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700276 pressure = x * (z2 - z1) / z1;
277 pressure = pressure * ts->x_plate_ohm / 4096;
278 if (unlikely(pressure > MAX_12BIT))
279 goto out;
280
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700281 spin_lock_irqsave(&ts->lock, flags);
282
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700283 tsc2005_update_pen_state(ts, x, y, pressure);
284
285 /* set the penup timer */
286 mod_timer(&ts->penup_timer,
287 jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
288
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700289 if (ts->esd_timeout && ts->set_reset) {
290 /* update the watchdog timer */
291 mod_timer(&ts->esd_timer, round_jiffies(jiffies +
292 msecs_to_jiffies(ts->esd_timeout)));
293 }
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700294
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700295 spin_unlock_irqrestore(&ts->lock, flags);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700296
297out:
298 mutex_unlock(&ts->mutex);
299 return IRQ_HANDLED;
300}
301
302static void tsc2005_penup_timer(unsigned long data)
303{
304 struct tsc2005 *ts = (struct tsc2005 *)data;
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700305 unsigned long flags;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700306
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700307 spin_lock_irqsave(&ts->lock, flags);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700308 tsc2005_update_pen_state(ts, 0, 0, 0);
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700309 spin_unlock_irqrestore(&ts->lock, flags);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700310}
311
312static void tsc2005_start_scan(struct tsc2005 *ts)
313{
314 tsc2005_write(ts, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE);
315 tsc2005_write(ts, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE);
316 tsc2005_write(ts, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE);
317 tsc2005_cmd(ts, TSC2005_CMD_NORMAL);
318}
319
320static void tsc2005_stop_scan(struct tsc2005 *ts)
321{
322 tsc2005_cmd(ts, TSC2005_CMD_STOP);
323}
324
325/* must be called with mutex held */
326static void tsc2005_disable(struct tsc2005 *ts)
327{
328 if (ts->disable_depth++ != 0)
329 return;
330 disable_irq(ts->spi->irq);
331 if (ts->esd_timeout)
332 del_timer_sync(&ts->esd_timer);
333 del_timer_sync(&ts->penup_timer);
334 tsc2005_stop_scan(ts);
335}
336
337/* must be called with mutex held */
338static void tsc2005_enable(struct tsc2005 *ts)
339{
340 if (--ts->disable_depth != 0)
341 return;
342 tsc2005_start_scan(ts);
343 enable_irq(ts->spi->irq);
344 if (!ts->esd_timeout)
345 return;
346 mod_timer(&ts->esd_timer,
347 round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
348}
349
350static ssize_t tsc2005_disable_show(struct device *dev,
351 struct device_attribute *attr, char *buf)
352{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700353 struct spi_device *spi = to_spi_device(dev);
354 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700355
356 return sprintf(buf, "%u\n", ts->disabled);
357}
358
359static ssize_t tsc2005_disable_store(struct device *dev,
360 struct device_attribute *attr,
361 const char *buf, size_t count)
362{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700363 struct spi_device *spi = to_spi_device(dev);
364 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700365 unsigned long res;
366 int i;
367
368 if (strict_strtoul(buf, 10, &res) < 0)
369 return -EINVAL;
370 i = res ? 1 : 0;
371
372 mutex_lock(&ts->mutex);
373 if (i == ts->disabled)
374 goto out;
375 ts->disabled = i;
376 if (i)
377 tsc2005_disable(ts);
378 else
379 tsc2005_enable(ts);
380out:
381 mutex_unlock(&ts->mutex);
382 return count;
383}
384static DEVICE_ATTR(disable, 0664, tsc2005_disable_show, tsc2005_disable_store);
385
386static ssize_t tsc2005_selftest_show(struct device *dev,
387 struct device_attribute *attr,
388 char *buf)
389{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700390 struct spi_device *spi = to_spi_device(dev);
391 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700392 u16 temp_high;
393 u16 temp_high_orig;
394 u16 temp_high_test;
395 unsigned int result;
396
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700397 mutex_lock(&ts->mutex);
398
399 /*
400 * Test TSC2005 communications via temp high register.
401 */
402 tsc2005_disable(ts);
403 result = 1;
404 tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig);
405 temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
406 tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test);
407 tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
408 if (temp_high != temp_high_test) {
409 dev_warn(dev, "selftest failed: %d != %d\n",
410 temp_high, temp_high_test);
411 result = 0;
412 }
413
414 /* hardware reset */
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700415 ts->set_reset(false);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700416 usleep_range(100, 500); /* only 10us required */
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700417 ts->set_reset(true);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700418 tsc2005_enable(ts);
419
420 /* test that the reset really happened */
421 tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
422 if (temp_high != temp_high_orig) {
423 dev_warn(dev, "selftest failed after reset: %d != %d\n",
424 temp_high, temp_high_orig);
425 result = 0;
426 }
427
428 mutex_unlock(&ts->mutex);
429
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700430 return sprintf(buf, "%u\n", result);
431}
Dmitry Torokhov8dbcc432011-03-16 22:10:37 -0700432
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700433static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL);
434
Dmitry Torokhov8dbcc432011-03-16 22:10:37 -0700435static struct attribute *tsc2005_attrs[] = {
436 &dev_attr_disable.attr,
437 &dev_attr_selftest.attr,
438 NULL
439};
440
441static mode_t tsc2005_attr_is_visible(struct kobject *kobj,
442 struct attribute *attr, int n)
443{
444 struct device *dev = container_of(kobj, struct device, kobj);
445 struct spi_device *spi = to_spi_device(dev);
446 struct tsc2005 *ts = spi_get_drvdata(spi);
447 mode_t mode = attr->mode;
448
449 if (attr == &dev_attr_selftest.attr) {
450 if (!ts->set_reset)
451 mode = 0;
452 }
453
454 return mode;
455}
456
457static const struct attribute_group tsc2005_attr_group = {
458 .is_visible = tsc2005_attr_is_visible,
459 .attrs = tsc2005_attrs,
460};
461
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700462static void tsc2005_esd_timer(unsigned long data)
463{
464 struct tsc2005 *ts = (struct tsc2005 *)data;
465
466 schedule_work(&ts->esd_work);
467}
468
469static void tsc2005_esd_work(struct work_struct *work)
470{
471 struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work);
472 u16 r;
473
474 mutex_lock(&ts->mutex);
475
476 if (ts->disable_depth)
477 goto out;
478
479 /*
480 * If we cannot read our known value from configuration register 0 then
481 * reset the controller as if from power-up and start scanning again.
482 */
483 tsc2005_read(ts, TSC2005_REG_CFR0, &r);
484 if ((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK) {
485 dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700486 ts->set_reset(false);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700487 tsc2005_update_pen_state(ts, 0, 0, 0);
488 usleep_range(100, 500); /* only 10us required */
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700489 ts->set_reset(true);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700490 tsc2005_start_scan(ts);
491 }
492
493 /* re-arm the watchdog */
494 mod_timer(&ts->esd_timer,
495 round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
496
497out:
498 mutex_unlock(&ts->mutex);
499}
500
501static void __devinit tsc2005_setup_spi_xfer(struct tsc2005 *ts)
502{
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700503 tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, false);
504 tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, false);
505 tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, false);
506 tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, true);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700507
508 spi_message_init(&ts->spi_read_msg);
509 spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg);
510 spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg);
511 spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg);
512 spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg);
513}
514
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700515static int __devinit tsc2005_probe(struct spi_device *spi)
516{
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700517 const struct tsc2005_platform_data *pdata = spi->dev.platform_data;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700518 struct tsc2005 *ts;
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700519 struct input_dev *input_dev;
520 unsigned int max_x, max_y, max_p;
521 unsigned int fudge_x, fudge_y, fudge_p;
522 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700523
524 if (!pdata) {
525 dev_dbg(&spi->dev, "no platform data\n");
526 return -ENODEV;
527 }
528
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700529 fudge_x = pdata->ts_x_fudge ? : 4;
530 fudge_y = pdata->ts_y_fudge ? : 8;
531 fudge_p = pdata->ts_pressure_fudge ? : 2;
532 max_x = pdata->ts_x_max ? : MAX_12BIT;
533 max_y = pdata->ts_y_max ? : MAX_12BIT;
534 max_p = pdata->ts_pressure_max ? : MAX_12BIT;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700535
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700536 if (spi->irq <= 0) {
537 dev_dbg(&spi->dev, "no irq\n");
538 return -ENODEV;
539 }
540
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700541 spi->mode = SPI_MODE_0;
542 spi->bits_per_word = 8;
543 if (!spi->max_speed_hz)
544 spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700545
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700546 error = spi_setup(spi);
547 if (error)
548 return error;
549
550 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
551 input_dev = input_allocate_device();
552 if (!ts || !input_dev) {
553 error = -ENOMEM;
554 goto err_free_mem;
Dmitry Torokhov2721a892011-03-16 22:09:09 -0700555 }
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700556
557 ts->spi = spi;
558 ts->idev = input_dev;
559
560 ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
561 ts->esd_timeout = pdata->esd_timeout_ms;
562 ts->set_reset = pdata->set_reset;
563
564 mutex_init(&ts->mutex);
565
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700566 spin_lock_init(&ts->lock);
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700567 setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700568
569 setup_timer(&ts->esd_timer, tsc2005_esd_timer, (unsigned long)ts);
570 INIT_WORK(&ts->esd_work, tsc2005_esd_work);
571
572 tsc2005_setup_spi_xfer(ts);
573
574 snprintf(ts->phys, sizeof(ts->phys),
575 "%s/input-ts", dev_name(&spi->dev));
576
577 input_dev->name = "TSC2005 touchscreen";
578 input_dev->phys = ts->phys;
579 input_dev->id.bustype = BUS_SPI;
580 input_dev->dev.parent = &spi->dev;
581 input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
582 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
583
584 input_set_abs_params(input_dev, ABS_X, 0, max_x, fudge_x, 0);
585 input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0);
586 input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0);
587
Dmitry Torokhovdacb6502011-03-16 22:11:14 -0700588 error = request_threaded_irq(spi->irq, NULL, tsc2005_irq_thread,
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700589 IRQF_TRIGGER_RISING, "tsc2005", ts);
590 if (error) {
591 dev_err(&spi->dev, "Failed to request irq, err: %d\n", error);
592 goto err_free_mem;
593 }
594
595 spi_set_drvdata(spi, ts);
596 error = sysfs_create_group(&spi->dev.kobj, &tsc2005_attr_group);
597 if (error) {
598 dev_err(&spi->dev,
599 "Failed to create sysfs attributes, err: %d\n", error);
600 goto err_clear_drvdata;
601 }
602
603 error = input_register_device(ts->idev);
604 if (error) {
605 dev_err(&spi->dev,
606 "Failed to register input device, err: %d\n", error);
607 goto err_remove_sysfs;
608 }
609
610 tsc2005_start_scan(ts);
611
612 if (ts->esd_timeout && ts->set_reset) {
613 /* start the optional ESD watchdog */
614 mod_timer(&ts->esd_timer, round_jiffies(jiffies +
615 msecs_to_jiffies(ts->esd_timeout)));
616 }
617
618 set_irq_wake(spi->irq, 1);
619 return 0;
620
621err_remove_sysfs:
622 sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);
623err_clear_drvdata:
624 spi_set_drvdata(spi, NULL);
625 free_irq(spi->irq, ts);
626err_free_mem:
627 input_free_device(input_dev);
628 kfree(ts);
629 return error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700630}
631
632static int __devexit tsc2005_remove(struct spi_device *spi)
633{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700634 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700635
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700636 sysfs_remove_group(&ts->spi->dev.kobj, &tsc2005_attr_group);
637
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700638 mutex_lock(&ts->mutex);
639 tsc2005_disable(ts);
640 mutex_unlock(&ts->mutex);
641
642 if (ts->esd_timeout)
643 del_timer_sync(&ts->esd_timer);
644 del_timer_sync(&ts->penup_timer);
645
646 flush_work(&ts->esd_work);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700647
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700648 free_irq(ts->spi->irq, ts);
649 input_unregister_device(ts->idev);
650 kfree(ts);
651
Dmitry Torokhov2721a892011-03-16 22:09:09 -0700652 spi_set_drvdata(spi, NULL);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700653 return 0;
654}
655
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700656#ifdef CONFIG_PM_SLEEP
657static int tsc2005_suspend(struct device *dev)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700658{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700659 struct spi_device *spi = to_spi_device(dev);
660 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700661
662 mutex_lock(&ts->mutex);
663 tsc2005_disable(ts);
664 mutex_unlock(&ts->mutex);
665
666 return 0;
667}
668
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700669static int tsc2005_resume(struct device *dev)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700670{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700671 struct spi_device *spi = to_spi_device(dev);
672 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700673
674 mutex_lock(&ts->mutex);
675 tsc2005_enable(ts);
676 mutex_unlock(&ts->mutex);
677
678 return 0;
679}
680#endif
681
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700682static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume);
683
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700684static struct spi_driver tsc2005_driver = {
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700685 .driver = {
686 .name = "tsc2005",
687 .owner = THIS_MODULE,
688 .pm = &tsc2005_pm_ops,
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700689 },
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700690 .probe = tsc2005_probe,
691 .remove = __devexit_p(tsc2005_remove),
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700692};
693
694static int __init tsc2005_init(void)
695{
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700696 return spi_register_driver(&tsc2005_driver);
697}
698module_init(tsc2005_init);
699
700static void __exit tsc2005_exit(void)
701{
702 spi_unregister_driver(&tsc2005_driver);
703}
704module_exit(tsc2005_exit);
705
706MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
Dmitry Torokhovb88aa492011-03-16 22:09:03 -0700707MODULE_DESCRIPTION("TSC2005 Touchscreen Driver");
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700708MODULE_LICENSE("GPL");