blob: bc7e2f974b7ee1c0ee36ca338e4016f3972ad769 [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
132 struct timer_list penup_timer;
133 struct work_struct penup_work;
134
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;
143 unsigned int pen_down;
144
145 void (*set_reset)(bool enable);
146};
147
148static void tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
149{
150 u8 tx;
151 struct spi_message msg;
152 struct spi_transfer xfer = { 0 };
153
154 tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd;
155
156 xfer.tx_buf = &tx;
157 xfer.rx_buf = NULL;
158 xfer.len = 1;
159 xfer.bits_per_word = 8;
160
161 spi_message_init(&msg);
162 spi_message_add_tail(&xfer, &msg);
163 spi_sync(ts->spi, &msg);
164}
165
166static void tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
167{
168 u32 tx;
169 struct spi_message msg;
170 struct spi_transfer xfer = { 0 };
171
172 tx = (reg | TSC2005_REG_PND0) << 16;
173 tx |= value;
174
175 xfer.tx_buf = &tx;
176 xfer.rx_buf = NULL;
177 xfer.len = 4;
178 xfer.bits_per_word = 24;
179
180 spi_message_init(&msg);
181 spi_message_add_tail(&xfer, &msg);
182 spi_sync(ts->spi, &msg);
183}
184
185static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last)
186{
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;
193}
194
195static void tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value)
196{
197 struct spi_message msg;
198 struct tsc2005_spi_rd spi_rd = { { 0 }, 0, 0 };
199
200 tsc2005_setup_read(&spi_rd, reg, 1);
201
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;
206}
207
208static void tsc2005_update_pen_state(struct tsc2005 *ts,
209 int x, int y, int pressure)
210{
211 if (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);
215 if (!ts->pen_down) {
216 input_report_key(ts->idev, BTN_TOUCH, !!pressure);
217 ts->pen_down = 1;
218 }
219 } else {
220 input_report_abs(ts->idev, ABS_PRESSURE, 0);
221 if (ts->pen_down) {
222 input_report_key(ts->idev, BTN_TOUCH, 0);
223 ts->pen_down = 0;
224 }
225 }
226 input_sync(ts->idev);
227 dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
228 pressure);
229}
230
231static irqreturn_t tsc2005_irq_handler(int irq, void *dev_id)
232{
233 struct tsc2005 *ts = dev_id;
234
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));
238
239 return IRQ_WAKE_THREAD;
240}
241
242static irqreturn_t tsc2005_irq_thread(int irq, void *_ts)
243{
244 struct tsc2005 *ts = _ts;
245 unsigned int pressure;
246 u32 x;
247 u32 y;
248 u32 z1;
249 u32 z2;
250
251 mutex_lock(&ts->mutex);
252
253 if (unlikely(ts->disable_depth))
254 goto out;
255
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;
262
263 /* validate position */
264 if (unlikely(x > MAX_12BIT || y > MAX_12BIT))
265 goto out;
266
267 /* skip coords if the pressure components are out of range */
268 if (unlikely(z1 == 0 || z2 > MAX_12BIT || z1 >= z2))
269 goto out;
270
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
273 */
274 if (!ts->pen_down &&
275 ts->in_x == x &&
276 ts->in_y == y &&
277 ts->in_z1 == z1 &&
278 ts->in_z2 == z2)
279 goto out;
280
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
283 */
284 ts->in_x = x;
285 ts->in_y = y;
286 ts->in_z1 = z1;
287 ts->in_z2 = z2;
288
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))
293 goto out;
294
295 tsc2005_update_pen_state(ts, x, y, pressure);
296
297 /* set the penup timer */
298 mod_timer(&ts->penup_timer,
299 jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
300
301 if (!ts->esd_timeout)
302 goto out;
303
304 /* update the watchdog timer */
305 mod_timer(&ts->esd_timer,
306 round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
307
308out:
309 mutex_unlock(&ts->mutex);
310 return IRQ_HANDLED;
311}
312
313static void tsc2005_penup_timer(unsigned long data)
314{
315 struct tsc2005 *ts = (struct tsc2005 *)data;
316
317 schedule_work(&ts->penup_work);
318}
319
320static void tsc2005_penup_work(struct work_struct *work)
321{
322 struct tsc2005 *ts = container_of(work, struct tsc2005, penup_work);
323
324 mutex_lock(&ts->mutex);
325 tsc2005_update_pen_state(ts, 0, 0, 0);
326 mutex_unlock(&ts->mutex);
327}
328
329static void tsc2005_start_scan(struct tsc2005 *ts)
330{
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);
335}
336
337static void tsc2005_stop_scan(struct tsc2005 *ts)
338{
339 tsc2005_cmd(ts, TSC2005_CMD_STOP);
340}
341
342/* must be called with mutex held */
343static void tsc2005_disable(struct tsc2005 *ts)
344{
345 if (ts->disable_depth++ != 0)
346 return;
347 disable_irq(ts->spi->irq);
348 if (ts->esd_timeout)
349 del_timer_sync(&ts->esd_timer);
350 del_timer_sync(&ts->penup_timer);
351 tsc2005_stop_scan(ts);
352}
353
354/* must be called with mutex held */
355static void tsc2005_enable(struct tsc2005 *ts)
356{
357 if (--ts->disable_depth != 0)
358 return;
359 tsc2005_start_scan(ts);
360 enable_irq(ts->spi->irq);
361 if (!ts->esd_timeout)
362 return;
363 mod_timer(&ts->esd_timer,
364 round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
365}
366
367static ssize_t tsc2005_disable_show(struct device *dev,
368 struct device_attribute *attr, char *buf)
369{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700370 struct spi_device *spi = to_spi_device(dev);
371 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700372
373 return sprintf(buf, "%u\n", ts->disabled);
374}
375
376static ssize_t tsc2005_disable_store(struct device *dev,
377 struct device_attribute *attr,
378 const char *buf, size_t count)
379{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700380 struct spi_device *spi = to_spi_device(dev);
381 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700382 unsigned long res;
383 int i;
384
385 if (strict_strtoul(buf, 10, &res) < 0)
386 return -EINVAL;
387 i = res ? 1 : 0;
388
389 mutex_lock(&ts->mutex);
390 if (i == ts->disabled)
391 goto out;
392 ts->disabled = i;
393 if (i)
394 tsc2005_disable(ts);
395 else
396 tsc2005_enable(ts);
397out:
398 mutex_unlock(&ts->mutex);
399 return count;
400}
401static DEVICE_ATTR(disable, 0664, tsc2005_disable_show, tsc2005_disable_store);
402
403static ssize_t tsc2005_selftest_show(struct device *dev,
404 struct device_attribute *attr,
405 char *buf)
406{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700407 struct spi_device *spi = to_spi_device(dev);
408 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700409 u16 temp_high;
410 u16 temp_high_orig;
411 u16 temp_high_test;
412 unsigned int result;
413
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700414 mutex_lock(&ts->mutex);
415
416 /*
417 * Test TSC2005 communications via temp high register.
418 */
419 tsc2005_disable(ts);
420 result = 1;
421 tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig);
422 temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
423 tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test);
424 tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
425 if (temp_high != temp_high_test) {
426 dev_warn(dev, "selftest failed: %d != %d\n",
427 temp_high, temp_high_test);
428 result = 0;
429 }
430
431 /* hardware reset */
432 ts->set_reset(0);
433 usleep_range(100, 500); /* only 10us required */
434 ts->set_reset(1);
435 tsc2005_enable(ts);
436
437 /* test that the reset really happened */
438 tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
439 if (temp_high != temp_high_orig) {
440 dev_warn(dev, "selftest failed after reset: %d != %d\n",
441 temp_high, temp_high_orig);
442 result = 0;
443 }
444
445 mutex_unlock(&ts->mutex);
446
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700447 return sprintf(buf, "%u\n", result);
448}
Dmitry Torokhov8dbcc432011-03-16 22:10:37 -0700449
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700450static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL);
451
Dmitry Torokhov8dbcc432011-03-16 22:10:37 -0700452static struct attribute *tsc2005_attrs[] = {
453 &dev_attr_disable.attr,
454 &dev_attr_selftest.attr,
455 NULL
456};
457
458static mode_t tsc2005_attr_is_visible(struct kobject *kobj,
459 struct attribute *attr, int n)
460{
461 struct device *dev = container_of(kobj, struct device, kobj);
462 struct spi_device *spi = to_spi_device(dev);
463 struct tsc2005 *ts = spi_get_drvdata(spi);
464 mode_t mode = attr->mode;
465
466 if (attr == &dev_attr_selftest.attr) {
467 if (!ts->set_reset)
468 mode = 0;
469 }
470
471 return mode;
472}
473
474static const struct attribute_group tsc2005_attr_group = {
475 .is_visible = tsc2005_attr_is_visible,
476 .attrs = tsc2005_attrs,
477};
478
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700479static void tsc2005_esd_timer(unsigned long data)
480{
481 struct tsc2005 *ts = (struct tsc2005 *)data;
482
483 schedule_work(&ts->esd_work);
484}
485
486static void tsc2005_esd_work(struct work_struct *work)
487{
488 struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work);
489 u16 r;
490
491 mutex_lock(&ts->mutex);
492
493 if (ts->disable_depth)
494 goto out;
495
496 /*
497 * If we cannot read our known value from configuration register 0 then
498 * reset the controller as if from power-up and start scanning again.
499 */
500 tsc2005_read(ts, TSC2005_REG_CFR0, &r);
501 if ((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK) {
502 dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");
503 ts->set_reset(0);
504 tsc2005_update_pen_state(ts, 0, 0, 0);
505 usleep_range(100, 500); /* only 10us required */
506 ts->set_reset(1);
507 tsc2005_start_scan(ts);
508 }
509
510 /* re-arm the watchdog */
511 mod_timer(&ts->esd_timer,
512 round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
513
514out:
515 mutex_unlock(&ts->mutex);
516}
517
518static void __devinit tsc2005_setup_spi_xfer(struct tsc2005 *ts)
519{
520 tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, 0);
521 tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, 0);
522 tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, 0);
523 tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, 1);
524
525 spi_message_init(&ts->spi_read_msg);
526 spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg);
527 spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg);
528 spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg);
529 spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg);
530}
531
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700532static int __devinit tsc2005_probe(struct spi_device *spi)
533{
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700534 const struct tsc2005_platform_data *pdata = spi->dev.platform_data;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700535 struct tsc2005 *ts;
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700536 struct input_dev *input_dev;
537 unsigned int max_x, max_y, max_p;
538 unsigned int fudge_x, fudge_y, fudge_p;
539 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700540
541 if (!pdata) {
542 dev_dbg(&spi->dev, "no platform data\n");
543 return -ENODEV;
544 }
545
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700546 fudge_x = pdata->ts_x_fudge ? : 4;
547 fudge_y = pdata->ts_y_fudge ? : 8;
548 fudge_p = pdata->ts_pressure_fudge ? : 2;
549 max_x = pdata->ts_x_max ? : MAX_12BIT;
550 max_y = pdata->ts_y_max ? : MAX_12BIT;
551 max_p = pdata->ts_pressure_max ? : MAX_12BIT;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700552
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700553 if (spi->irq <= 0) {
554 dev_dbg(&spi->dev, "no irq\n");
555 return -ENODEV;
556 }
557
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700558 spi->mode = SPI_MODE_0;
559 spi->bits_per_word = 8;
560 if (!spi->max_speed_hz)
561 spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700562
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700563 error = spi_setup(spi);
564 if (error)
565 return error;
566
567 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
568 input_dev = input_allocate_device();
569 if (!ts || !input_dev) {
570 error = -ENOMEM;
571 goto err_free_mem;
Dmitry Torokhov2721a892011-03-16 22:09:09 -0700572 }
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700573
574 ts->spi = spi;
575 ts->idev = input_dev;
576
577 ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
578 ts->esd_timeout = pdata->esd_timeout_ms;
579 ts->set_reset = pdata->set_reset;
580
581 mutex_init(&ts->mutex);
582
583 setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);
584 INIT_WORK(&ts->penup_work, tsc2005_penup_work);
585
586 setup_timer(&ts->esd_timer, tsc2005_esd_timer, (unsigned long)ts);
587 INIT_WORK(&ts->esd_work, tsc2005_esd_work);
588
589 tsc2005_setup_spi_xfer(ts);
590
591 snprintf(ts->phys, sizeof(ts->phys),
592 "%s/input-ts", dev_name(&spi->dev));
593
594 input_dev->name = "TSC2005 touchscreen";
595 input_dev->phys = ts->phys;
596 input_dev->id.bustype = BUS_SPI;
597 input_dev->dev.parent = &spi->dev;
598 input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
599 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
600
601 input_set_abs_params(input_dev, ABS_X, 0, max_x, fudge_x, 0);
602 input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0);
603 input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0);
604
605 error = request_threaded_irq(spi->irq,
606 tsc2005_irq_handler, tsc2005_irq_thread,
607 IRQF_TRIGGER_RISING, "tsc2005", ts);
608 if (error) {
609 dev_err(&spi->dev, "Failed to request irq, err: %d\n", error);
610 goto err_free_mem;
611 }
612
613 spi_set_drvdata(spi, ts);
614 error = sysfs_create_group(&spi->dev.kobj, &tsc2005_attr_group);
615 if (error) {
616 dev_err(&spi->dev,
617 "Failed to create sysfs attributes, err: %d\n", error);
618 goto err_clear_drvdata;
619 }
620
621 error = input_register_device(ts->idev);
622 if (error) {
623 dev_err(&spi->dev,
624 "Failed to register input device, err: %d\n", error);
625 goto err_remove_sysfs;
626 }
627
628 tsc2005_start_scan(ts);
629
630 if (ts->esd_timeout && ts->set_reset) {
631 /* start the optional ESD watchdog */
632 mod_timer(&ts->esd_timer, round_jiffies(jiffies +
633 msecs_to_jiffies(ts->esd_timeout)));
634 }
635
636 set_irq_wake(spi->irq, 1);
637 return 0;
638
639err_remove_sysfs:
640 sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);
641err_clear_drvdata:
642 spi_set_drvdata(spi, NULL);
643 free_irq(spi->irq, ts);
644err_free_mem:
645 input_free_device(input_dev);
646 kfree(ts);
647 return error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700648}
649
650static int __devexit tsc2005_remove(struct spi_device *spi)
651{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700652 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700653
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700654 sysfs_remove_group(&ts->spi->dev.kobj, &tsc2005_attr_group);
655
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700656 mutex_lock(&ts->mutex);
657 tsc2005_disable(ts);
658 mutex_unlock(&ts->mutex);
659
660 if (ts->esd_timeout)
661 del_timer_sync(&ts->esd_timer);
662 del_timer_sync(&ts->penup_timer);
663
664 flush_work(&ts->esd_work);
665 flush_work(&ts->penup_work);
666
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700667 free_irq(ts->spi->irq, ts);
668 input_unregister_device(ts->idev);
669 kfree(ts);
670
Dmitry Torokhov2721a892011-03-16 22:09:09 -0700671 spi_set_drvdata(spi, NULL);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700672 return 0;
673}
674
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700675#ifdef CONFIG_PM_SLEEP
676static int tsc2005_suspend(struct device *dev)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700677{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700678 struct spi_device *spi = to_spi_device(dev);
679 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700680
681 mutex_lock(&ts->mutex);
682 tsc2005_disable(ts);
683 mutex_unlock(&ts->mutex);
684
685 return 0;
686}
687
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700688static int tsc2005_resume(struct device *dev)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700689{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700690 struct spi_device *spi = to_spi_device(dev);
691 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700692
693 mutex_lock(&ts->mutex);
694 tsc2005_enable(ts);
695 mutex_unlock(&ts->mutex);
696
697 return 0;
698}
699#endif
700
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700701static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume);
702
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700703static struct spi_driver tsc2005_driver = {
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700704 .driver = {
705 .name = "tsc2005",
706 .owner = THIS_MODULE,
707 .pm = &tsc2005_pm_ops,
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700708 },
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700709 .probe = tsc2005_probe,
710 .remove = __devexit_p(tsc2005_remove),
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700711};
712
713static int __init tsc2005_init(void)
714{
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700715 return spi_register_driver(&tsc2005_driver);
716}
717module_init(tsc2005_init);
718
719static void __exit tsc2005_exit(void)
720{
721 spi_unregister_driver(&tsc2005_driver);
722}
723module_exit(tsc2005_exit);
724
725MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
Dmitry Torokhovb88aa492011-03-16 22:09:03 -0700726MODULE_DESCRIPTION("TSC2005 Touchscreen Driver");
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700727MODULE_LICENSE("GPL");