Input: tsc2005 - handle read errors from SPI layer
[pandora-kernel.git] / drivers / input / touchscreen / tsc2005.c
1 /*
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>
30 #include <linux/pm.h>
31 #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
106 struct tsc2005_spi_rd {
107         struct spi_transfer     spi_xfer;
108         u32                     spi_tx;
109         u32                     spi_rx;
110 };
111
112 struct 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         spinlock_t              lock;
133         struct timer_list       penup_timer;
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
144         bool                    pen_down;
145
146         void                    (*set_reset)(bool enable);
147 };
148
149 static int tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
150 {
151         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         };
157         struct spi_message msg;
158         int error;
159
160         spi_message_init(&msg);
161         spi_message_add_tail(&xfer, &msg);
162
163         error = spi_sync(ts->spi, &msg);
164         if (error) {
165                 dev_err(&ts->spi->dev, "%s: failed, command: %x, error: %d\n",
166                         __func__, cmd, error);
167                 return error;
168         }
169
170         return 0;
171 }
172
173 static int tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
174 {
175         u32 tx = ((reg | TSC2005_REG_PND0) << 16) | value;
176         struct spi_transfer xfer = {
177                 .tx_buf         = &tx,
178                 .len            = 4,
179                 .bits_per_word  = 24,
180         };
181         struct spi_message msg;
182         int error;
183
184         spi_message_init(&msg);
185         spi_message_add_tail(&xfer, &msg);
186
187         error = spi_sync(ts->spi, &msg);
188         if (error) {
189                 dev_err(&ts->spi->dev,
190                         "%s: failed, register: %x, value: %x, error: %d\n",
191                         __func__, reg, value, error);
192                 return error;
193         }
194
195         return 0;
196 }
197
198 static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last)
199 {
200         memset(rd, 0, sizeof(*rd));
201
202         rd->spi_tx                 = (reg | TSC2005_REG_READ) << 16;
203         rd->spi_xfer.tx_buf        = &rd->spi_tx;
204         rd->spi_xfer.rx_buf        = &rd->spi_rx;
205         rd->spi_xfer.len           = 4;
206         rd->spi_xfer.bits_per_word = 24;
207         rd->spi_xfer.cs_change     = !last;
208 }
209
210 static int tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value)
211 {
212         struct tsc2005_spi_rd spi_rd;
213         struct spi_message msg;
214         int error;
215
216         tsc2005_setup_read(&spi_rd, reg, true);
217
218         spi_message_init(&msg);
219         spi_message_add_tail(&spi_rd.spi_xfer, &msg);
220
221         error = spi_sync(ts->spi, &msg);
222         if (error)
223                 return error;
224
225         *value = spi_rd.spi_rx;
226         return 0;
227 }
228
229 static void tsc2005_update_pen_state(struct tsc2005 *ts,
230                                      int x, int y, int pressure)
231 {
232         if (pressure) {
233                 input_report_abs(ts->idev, ABS_X, x);
234                 input_report_abs(ts->idev, ABS_Y, y);
235                 input_report_abs(ts->idev, ABS_PRESSURE, pressure);
236                 if (!ts->pen_down) {
237                         input_report_key(ts->idev, BTN_TOUCH, !!pressure);
238                         ts->pen_down = true;
239                 }
240         } else {
241                 input_report_abs(ts->idev, ABS_PRESSURE, 0);
242                 if (ts->pen_down) {
243                         input_report_key(ts->idev, BTN_TOUCH, 0);
244                         ts->pen_down = false;
245                 }
246         }
247         input_sync(ts->idev);
248         dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
249                 pressure);
250 }
251
252 static irqreturn_t tsc2005_irq_thread(int irq, void *_ts)
253 {
254         struct tsc2005 *ts = _ts;
255         unsigned long flags;
256         unsigned int pressure;
257         u32 x, y;
258         u32 z1, z2;
259         int error;
260
261         mutex_lock(&ts->mutex);
262
263         if (unlikely(ts->disable_depth))
264                 goto out;
265
266         /* read the coordinates */
267         error = spi_sync(ts->spi, &ts->spi_read_msg);
268         if (unlikely(error))
269                 goto out;
270
271         x = ts->spi_x.spi_rx;
272         y = ts->spi_y.spi_rx;
273         z1 = ts->spi_z1.spi_rx;
274         z2 = ts->spi_z2.spi_rx;
275
276         /* validate position */
277         if (unlikely(x > MAX_12BIT || y > MAX_12BIT))
278                 goto out;
279
280         /* Skip reading if the pressure components are out of range */
281         if (unlikely(z1 == 0 || z2 > MAX_12BIT || z1 >= z2))
282                 goto out;
283
284        /*
285         * Skip point if this is a pen down with the exact same values as
286         * the value before pen-up - that implies SPI fed us stale data
287         */
288         if (!ts->pen_down &&
289             ts->in_x == x && ts->in_y == y &&
290             ts->in_z1 == z1 && ts->in_z2 == z2) {
291                 goto out;
292         }
293
294         /*
295          * At this point we are happy we have a valid and useful reading.
296          * Remember it for later comparisons. We may now begin downsampling.
297          */
298         ts->in_x = x;
299         ts->in_y = y;
300         ts->in_z1 = z1;
301         ts->in_z2 = z2;
302
303         /* Compute touch pressure resistance using equation #1 */
304         pressure = x * (z2 - z1) / z1;
305         pressure = pressure * ts->x_plate_ohm / 4096;
306         if (unlikely(pressure > MAX_12BIT))
307                 goto out;
308
309         spin_lock_irqsave(&ts->lock, flags);
310
311         tsc2005_update_pen_state(ts, x, y, pressure);
312
313         /* set the penup timer */
314         mod_timer(&ts->penup_timer,
315                   jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
316
317         if (ts->esd_timeout && ts->set_reset) {
318                 /* update the watchdog timer */
319                 mod_timer(&ts->esd_timer, round_jiffies(jiffies +
320                                         msecs_to_jiffies(ts->esd_timeout)));
321         }
322
323         spin_unlock_irqrestore(&ts->lock, flags);
324
325 out:
326         mutex_unlock(&ts->mutex);
327         return IRQ_HANDLED;
328 }
329
330 static void tsc2005_penup_timer(unsigned long data)
331 {
332         struct tsc2005 *ts = (struct tsc2005 *)data;
333         unsigned long flags;
334
335         spin_lock_irqsave(&ts->lock, flags);
336         tsc2005_update_pen_state(ts, 0, 0, 0);
337         spin_unlock_irqrestore(&ts->lock, flags);
338 }
339
340 static void tsc2005_start_scan(struct tsc2005 *ts)
341 {
342         tsc2005_write(ts, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE);
343         tsc2005_write(ts, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE);
344         tsc2005_write(ts, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE);
345         tsc2005_cmd(ts, TSC2005_CMD_NORMAL);
346 }
347
348 static void tsc2005_stop_scan(struct tsc2005 *ts)
349 {
350         tsc2005_cmd(ts, TSC2005_CMD_STOP);
351 }
352
353 /* must be called with mutex held */
354 static void tsc2005_disable(struct tsc2005 *ts)
355 {
356         if (ts->disable_depth++ != 0)
357                 return;
358         disable_irq(ts->spi->irq);
359         if (ts->esd_timeout)
360                 del_timer_sync(&ts->esd_timer);
361         del_timer_sync(&ts->penup_timer);
362         tsc2005_stop_scan(ts);
363 }
364
365 /* must be called with mutex held */
366 static void tsc2005_enable(struct tsc2005 *ts)
367 {
368         if (--ts->disable_depth != 0)
369                 return;
370         tsc2005_start_scan(ts);
371         enable_irq(ts->spi->irq);
372         if (!ts->esd_timeout)
373                 return;
374         mod_timer(&ts->esd_timer,
375                   round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
376 }
377
378 static ssize_t tsc2005_disable_show(struct device *dev,
379                                     struct device_attribute *attr, char *buf)
380 {
381         struct spi_device *spi = to_spi_device(dev);
382         struct tsc2005 *ts = spi_get_drvdata(spi);
383
384         return sprintf(buf, "%u\n", ts->disabled);
385 }
386
387 static ssize_t tsc2005_disable_store(struct device *dev,
388                                      struct device_attribute *attr,
389                                      const char *buf, size_t count)
390 {
391         struct spi_device *spi = to_spi_device(dev);
392         struct tsc2005 *ts = spi_get_drvdata(spi);
393         unsigned long res;
394         int i;
395
396         if (strict_strtoul(buf, 10, &res) < 0)
397                 return -EINVAL;
398         i = res ? 1 : 0;
399
400         mutex_lock(&ts->mutex);
401         if (i == ts->disabled)
402                 goto out;
403         ts->disabled = i;
404         if (i)
405                 tsc2005_disable(ts);
406         else
407                 tsc2005_enable(ts);
408 out:
409         mutex_unlock(&ts->mutex);
410         return count;
411 }
412 static DEVICE_ATTR(disable, 0664, tsc2005_disable_show, tsc2005_disable_store);
413
414 static ssize_t tsc2005_selftest_show(struct device *dev,
415                                      struct device_attribute *attr,
416                                      char *buf)
417 {
418         struct spi_device *spi = to_spi_device(dev);
419         struct tsc2005 *ts = spi_get_drvdata(spi);
420         u16 temp_high;
421         u16 temp_high_orig;
422         u16 temp_high_test;
423         bool success = true;
424         int error;
425
426         mutex_lock(&ts->mutex);
427
428         /*
429          * Test TSC2005 communications via temp high register.
430          */
431         tsc2005_disable(ts);
432
433         error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig);
434         if (error) {
435                 dev_warn(dev, "selftest failed: read error %d\n", error);
436                 success = false;
437                 goto out;
438         }
439
440         temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
441
442         error = tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test);
443         if (error) {
444                 dev_warn(dev, "selftest failed: write error %d\n", error);
445                 success = false;
446                 goto out;
447         }
448
449         error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
450         if (error) {
451                 dev_warn(dev, "selftest failed: read error %d after write\n",
452                          error);
453                 success = false;
454                 goto out;
455         }
456
457         if (temp_high != temp_high_test) {
458                 dev_warn(dev, "selftest failed: %d != %d\n",
459                          temp_high, temp_high_test);
460                 success = false;
461         }
462
463         /* hardware reset */
464         ts->set_reset(false);
465         usleep_range(100, 500); /* only 10us required */
466         ts->set_reset(true);
467
468         if (!success)
469                 goto out;
470
471         /* test that the reset really happened */
472         error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
473         if (error) {
474                 dev_warn(dev, "selftest failed: read error %d after reset\n",
475                          error);
476                 success = false;
477                 goto out;
478         }
479
480         if (temp_high != temp_high_orig) {
481                 dev_warn(dev, "selftest failed after reset: %d != %d\n",
482                          temp_high, temp_high_orig);
483                 success = false;
484         }
485
486 out:
487         tsc2005_enable(ts);
488         mutex_unlock(&ts->mutex);
489
490         return sprintf(buf, "%d\n", success);
491 }
492
493 static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL);
494
495 static struct attribute *tsc2005_attrs[] = {
496         &dev_attr_disable.attr,
497         &dev_attr_selftest.attr,
498         NULL
499 };
500
501 static mode_t tsc2005_attr_is_visible(struct kobject *kobj,
502                                       struct attribute *attr, int n)
503 {
504         struct device *dev = container_of(kobj, struct device, kobj);
505         struct spi_device *spi = to_spi_device(dev);
506         struct tsc2005 *ts = spi_get_drvdata(spi);
507         mode_t mode = attr->mode;
508
509         if (attr == &dev_attr_selftest.attr) {
510                 if (!ts->set_reset)
511                         mode = 0;
512         }
513
514         return mode;
515 }
516
517 static const struct attribute_group tsc2005_attr_group = {
518         .is_visible     = tsc2005_attr_is_visible,
519         .attrs          = tsc2005_attrs,
520 };
521
522 static void tsc2005_esd_timer(unsigned long data)
523 {
524         struct tsc2005 *ts = (struct tsc2005 *)data;
525
526         schedule_work(&ts->esd_work);
527 }
528
529 static void tsc2005_esd_work(struct work_struct *work)
530 {
531         struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work);
532         int error;
533         u16 r;
534
535         mutex_lock(&ts->mutex);
536
537         if (ts->disable_depth)
538                 goto out;
539
540         /*
541          * If we cannot read our known value from configuration register 0 then
542          * reset the controller as if from power-up and start scanning again.
543          */
544         error = tsc2005_read(ts, TSC2005_REG_CFR0, &r);
545         if (error ||
546             ((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK)) {
547                 dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");
548                 ts->set_reset(false);
549                 tsc2005_update_pen_state(ts, 0, 0, 0);
550                 usleep_range(100, 500); /* only 10us required */
551                 ts->set_reset(true);
552                 tsc2005_start_scan(ts);
553         }
554
555         /* re-arm the watchdog */
556         mod_timer(&ts->esd_timer,
557                   round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
558
559 out:
560         mutex_unlock(&ts->mutex);
561 }
562
563 static void __devinit tsc2005_setup_spi_xfer(struct tsc2005 *ts)
564 {
565         tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, false);
566         tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, false);
567         tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, false);
568         tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, true);
569
570         spi_message_init(&ts->spi_read_msg);
571         spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg);
572         spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg);
573         spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg);
574         spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg);
575 }
576
577 static int __devinit tsc2005_probe(struct spi_device *spi)
578 {
579         const struct tsc2005_platform_data *pdata = spi->dev.platform_data;
580         struct tsc2005 *ts;
581         struct input_dev *input_dev;
582         unsigned int max_x, max_y, max_p;
583         unsigned int fudge_x, fudge_y, fudge_p;
584         int error;
585
586         if (!pdata) {
587                 dev_dbg(&spi->dev, "no platform data\n");
588                 return -ENODEV;
589         }
590
591         fudge_x = pdata->ts_x_fudge        ? : 4;
592         fudge_y = pdata->ts_y_fudge        ? : 8;
593         fudge_p = pdata->ts_pressure_fudge ? : 2;
594         max_x   = pdata->ts_x_max          ? : MAX_12BIT;
595         max_y   = pdata->ts_y_max          ? : MAX_12BIT;
596         max_p   = pdata->ts_pressure_max   ? : MAX_12BIT;
597
598         if (spi->irq <= 0) {
599                 dev_dbg(&spi->dev, "no irq\n");
600                 return -ENODEV;
601         }
602
603         spi->mode = SPI_MODE_0;
604         spi->bits_per_word = 8;
605         if (!spi->max_speed_hz)
606                 spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ;
607
608         error = spi_setup(spi);
609         if (error)
610                 return error;
611
612         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
613         input_dev = input_allocate_device();
614         if (!ts || !input_dev) {
615                 error = -ENOMEM;
616                 goto err_free_mem;
617         }
618
619         ts->spi = spi;
620         ts->idev = input_dev;
621
622         ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
623         ts->esd_timeout = pdata->esd_timeout_ms;
624         ts->set_reset   = pdata->set_reset;
625
626         mutex_init(&ts->mutex);
627
628         spin_lock_init(&ts->lock);
629         setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);
630
631         setup_timer(&ts->esd_timer, tsc2005_esd_timer, (unsigned long)ts);
632         INIT_WORK(&ts->esd_work, tsc2005_esd_work);
633
634         tsc2005_setup_spi_xfer(ts);
635
636         snprintf(ts->phys, sizeof(ts->phys),
637                  "%s/input-ts", dev_name(&spi->dev));
638
639         input_dev->name = "TSC2005 touchscreen";
640         input_dev->phys = ts->phys;
641         input_dev->id.bustype = BUS_SPI;
642         input_dev->dev.parent = &spi->dev;
643         input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
644         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
645
646         input_set_abs_params(input_dev, ABS_X, 0, max_x, fudge_x, 0);
647         input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0);
648         input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0);
649
650         error = request_threaded_irq(spi->irq, NULL, tsc2005_irq_thread,
651                                      IRQF_TRIGGER_RISING, "tsc2005", ts);
652         if (error) {
653                 dev_err(&spi->dev, "Failed to request irq, err: %d\n", error);
654                 goto err_free_mem;
655         }
656
657         spi_set_drvdata(spi, ts);
658         error = sysfs_create_group(&spi->dev.kobj, &tsc2005_attr_group);
659         if (error) {
660                 dev_err(&spi->dev,
661                         "Failed to create sysfs attributes, err: %d\n", error);
662                 goto err_clear_drvdata;
663         }
664
665         error = input_register_device(ts->idev);
666         if (error) {
667                 dev_err(&spi->dev,
668                         "Failed to register input device, err: %d\n", error);
669                 goto err_remove_sysfs;
670         }
671
672         tsc2005_start_scan(ts);
673
674         if (ts->esd_timeout && ts->set_reset) {
675                 /* start the optional ESD watchdog */
676                 mod_timer(&ts->esd_timer, round_jiffies(jiffies +
677                                         msecs_to_jiffies(ts->esd_timeout)));
678         }
679
680         set_irq_wake(spi->irq, 1);
681         return 0;
682
683 err_remove_sysfs:
684         sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);
685 err_clear_drvdata:
686         spi_set_drvdata(spi, NULL);
687         free_irq(spi->irq, ts);
688 err_free_mem:
689         input_free_device(input_dev);
690         kfree(ts);
691         return error;
692 }
693
694 static int __devexit tsc2005_remove(struct spi_device *spi)
695 {
696         struct tsc2005 *ts = spi_get_drvdata(spi);
697
698         sysfs_remove_group(&ts->spi->dev.kobj, &tsc2005_attr_group);
699
700         mutex_lock(&ts->mutex);
701         tsc2005_disable(ts);
702         mutex_unlock(&ts->mutex);
703
704         if (ts->esd_timeout)
705                 del_timer_sync(&ts->esd_timer);
706         del_timer_sync(&ts->penup_timer);
707
708         flush_work(&ts->esd_work);
709
710         free_irq(ts->spi->irq, ts);
711         input_unregister_device(ts->idev);
712         kfree(ts);
713
714         spi_set_drvdata(spi, NULL);
715         return 0;
716 }
717
718 #ifdef CONFIG_PM_SLEEP
719 static int tsc2005_suspend(struct device *dev)
720 {
721         struct spi_device *spi = to_spi_device(dev);
722         struct tsc2005 *ts = spi_get_drvdata(spi);
723
724         mutex_lock(&ts->mutex);
725         tsc2005_disable(ts);
726         mutex_unlock(&ts->mutex);
727
728         return 0;
729 }
730
731 static int tsc2005_resume(struct device *dev)
732 {
733         struct spi_device *spi = to_spi_device(dev);
734         struct tsc2005 *ts = spi_get_drvdata(spi);
735
736         mutex_lock(&ts->mutex);
737         tsc2005_enable(ts);
738         mutex_unlock(&ts->mutex);
739
740         return 0;
741 }
742 #endif
743
744 static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume);
745
746 static struct spi_driver tsc2005_driver = {
747         .driver = {
748                 .name   = "tsc2005",
749                 .owner  = THIS_MODULE,
750                 .pm     = &tsc2005_pm_ops,
751         },
752         .probe  = tsc2005_probe,
753         .remove = __devexit_p(tsc2005_remove),
754 };
755
756 static int __init tsc2005_init(void)
757 {
758         return spi_register_driver(&tsc2005_driver);
759 }
760 module_init(tsc2005_init);
761
762 static void __exit tsc2005_exit(void)
763 {
764         spi_unregister_driver(&tsc2005_driver);
765 }
766 module_exit(tsc2005_exit);
767
768 MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
769 MODULE_DESCRIPTION("TSC2005 Touchscreen Driver");
770 MODULE_LICENSE("GPL");