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