]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/touchscreen/tsc2301_ts.c
tsc2301 - add coordinate average filter
[linux-2.6-omap-h63xx.git] / drivers / input / touchscreen / tsc2301_ts.c
1 /*
2  * TSC2301 touchscreen driver
3  *
4  * Copyright (C) 2005-2008 Nokia Corporation
5  *
6  * Written by Jarkko Oikarinen, Imre Deak and Juha Yrjola
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/input.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/spi/spi.h>
30
31 #ifdef CONFIG_ARCH_OMAP
32 #include <asm/arch/gpio.h>
33 #endif
34
35 #include <linux/spi/tsc2301.h>
36
37 /**
38  * The touchscreen interface operates as follows:
39  *
40  * Initialize:
41  *    Request access to GPIO103 (DAV)
42  *    tsc2301_ts_irq_handler will trigger when DAV line goes down
43  *
44  *  1) Pen is pressed against touchscreeen
45  *  2) TSC2301 performs AD conversion
46  *  3) After the conversion is done TSC2301 drives DAV line down
47  *  4) GPIO IRQ is received and tsc2301_ts_irq_handler is called
48  *  5) tsc2301_ts_irq_handler queues up an spi transfer to fetch
49  *     the x, y, z1, z2 values
50  *  6) SPI framework calls tsc2301_ts_rx after the coordinates are read
51  *  7) When the penup_timer expires, there have not been DAV interrupts
52  *     during the last 20ms which means the pen has been lifted.
53  */
54
55
56 #define TSC2301_TOUCHSCREEN_PRODUCT_ID                  0x0052
57 #define TSC2301_TOUCHSCREEN_PRODUCT_VERSION             0x0001
58
59 #define TSC2301_TS_PENUP_TIME                           20
60
61 #define TSC2301_ADCREG_CONVERSION_CTRL_BY_TSC2301       0x8000
62 #define TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST          0x0000
63
64 #define TSC2301_ADCREG_FUNCTION_NONE                    0x0000
65 #define TSC2301_ADCREG_FUNCTION_XY                      0x0400
66 #define TSC2301_ADCREG_FUNCTION_XYZ                     0x0800
67 #define TSC2301_ADCREG_FUNCTION_X                       0x0C00
68 #define TSC2301_ADCREG_FUNCTION_Y                       0x1000
69 #define TSC2301_ADCREG_FUNCTION_Z                       0x1400
70 #define TSC2301_ADCREG_FUNCTION_DAT1                    0x1800
71 #define TSC2301_ADCREG_FUNCTION_DAT2                    0x1C00
72 #define TSC2301_ADCREG_FUNCTION_AUX1                    0x2000
73 #define TSC2301_ADCREG_FUNCTION_AUX2                    0x2400
74 #define TSC2301_ADCREG_FUNCTION_TEMP                    0x2800
75
76 #define TSC2301_ADCREG_RESOLUTION_8BIT                  0x0100
77 #define TSC2301_ADCREG_RESOLUTION_10BIT                 0x0200
78 #define TSC2301_ADCREG_RESOLUTION_12BIT                 0x0300
79
80 #define TSC2301_ADCREG_AVERAGING_NONE                   0x0000
81 #define TSC2301_ADCREG_AVERAGING_4AVG                   0x0040
82 #define TSC2301_ADCREG_AVERAGING_8AVG                   0x0080
83 #define TSC2301_ADCREG_AVERAGING_16AVG                  0x00C0
84
85 #define TSC2301_ADCREG_CLOCK_8MHZ                       0x0000
86 #define TSC2301_ADCREG_CLOCK_4MHZ                       0x0010
87 #define TSC2301_ADCREG_CLOCK_2MHZ                       0x0020
88 #define TSC2301_ADCREG_CLOCK_1MHZ                       0x0030
89
90 #define TSC2301_ADCREG_VOLTAGE_STAB_0US                 0x0000
91 #define TSC2301_ADCREG_VOLTAGE_STAB_100US               0x0002
92 #define TSC2301_ADCREG_VOLTAGE_STAB_500US               0x0004
93 #define TSC2301_ADCREG_VOLTAGE_STAB_1MS                 0x0006
94 #define TSC2301_ADCREG_VOLTAGE_STAB_5MS                 0x0008
95 #define TSC2301_ADCREG_VOLTAGE_STAB_10MS                0x000A
96 #define TSC2301_ADCREG_VOLTAGE_STAB_50MS                0x000C
97 #define TSC2301_ADCREG_VOLTAGE_STAB_100MS               0x000E
98
99 #define TSC2301_ADCREG_STOP_CONVERSION                  0x4000
100
101 #define MAX_12BIT                                       ((1 << 12) - 1)
102
103 #define TS_RECT_SIZE                                    8
104 #define TSF_MIN_Z1                                      100
105 #define TSF_MAX_Z2                                      4000
106
107 #define TSF_SAMPLES                                     4
108
109 struct ts_filter {
110         int                     sample_cnt;
111
112         int                     avg_x;
113         int                     avg_y;
114         int                     avg_z1;
115         int                     avg_z2;
116 };
117
118 struct tsc2301_ts {
119         struct input_dev        *idev;
120         char                    phys[32];
121         struct timer_list       penup_timer;
122         struct mutex            mutex;
123
124         struct spi_transfer     read_xfer[2];
125         struct spi_message      read_msg;
126         u16                     data[4];
127
128         struct ts_filter        filter;
129
130         int                     hw_avg_max;
131         u16                     x;
132         u16                     y;
133         u16                     p;
134
135         u16                     x_plate_ohm;
136         int                     stab_time;
137         int                     max_pressure;
138         int                     touch_pressure;
139
140         u8                      event_sent;
141         u8                      pen_down;
142         u8                      disabled;
143         u8                      disable_depth;
144
145         int                     hw_flags;
146
147         s16                     dav_gpio;
148         int                     irq;
149 };
150
151
152 static const u16 tsc2301_ts_read_data = 0x8000 | TSC2301_REG_X;
153
154 static int tsc2301_ts_check_config(struct tsc2301_ts *ts, int *hw_flags)
155 {
156         int flags;
157
158         flags = 0;
159         switch (ts->hw_avg_max) {
160         case 0:
161                 flags |= TSC2301_ADCREG_AVERAGING_NONE;
162                 break;
163         case 4:
164                 flags |= TSC2301_ADCREG_AVERAGING_4AVG;
165                 break;
166         case 8:
167                 flags |= TSC2301_ADCREG_AVERAGING_8AVG;
168                 break;
169         case 16:
170                 flags |= TSC2301_ADCREG_AVERAGING_16AVG;
171                 break;
172         default:
173                 return -EINVAL;
174         }
175
176         switch (ts->stab_time) {
177         case 0:
178                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_0US;
179                 break;
180         case 100:
181                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_100US;
182                 break;
183         case 500:
184                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_500US;
185                 break;
186         case 1000:
187                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_1MS;
188                 break;
189         case 5000:
190                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_5MS;
191                 break;
192         case 10000:
193                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_10MS;
194                 break;
195         case 50000:
196                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_50MS;
197                 break;
198         case 100000:
199                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_100MS;
200                 break;
201         default:
202                 return -EINVAL;
203         }
204
205         *hw_flags = flags;
206         return 0;
207 }
208
209 /*
210  * This odd three-time initialization is to work around a bug in TSC2301.
211  * See TSC2301 errata for details.
212  */
213 static int tsc2301_ts_configure(struct tsc2301 *tsc, int flags)
214 {
215         struct spi_transfer xfer[5];
216         struct spi_transfer *x;
217         struct spi_message m;
218         int i;
219         u16 val1, val2, val3;
220         u16 data[10];
221
222         val1 = TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST |
223                 TSC2301_ADCREG_STOP_CONVERSION |
224                 TSC2301_ADCREG_FUNCTION_NONE |
225                 TSC2301_ADCREG_RESOLUTION_12BIT |
226                 TSC2301_ADCREG_AVERAGING_NONE |
227                 TSC2301_ADCREG_CLOCK_2MHZ |
228                 TSC2301_ADCREG_VOLTAGE_STAB_100MS;
229
230         val2 = TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST |
231                 TSC2301_ADCREG_FUNCTION_XYZ |
232                 TSC2301_ADCREG_RESOLUTION_12BIT |
233                 TSC2301_ADCREG_AVERAGING_16AVG |
234                 TSC2301_ADCREG_CLOCK_1MHZ |
235                 TSC2301_ADCREG_VOLTAGE_STAB_100MS;
236
237         /* Averaging and voltage stabilization settings in flags */
238         val3 = TSC2301_ADCREG_CONVERSION_CTRL_BY_TSC2301 |
239                 TSC2301_ADCREG_FUNCTION_XYZ |
240                 TSC2301_ADCREG_RESOLUTION_12BIT |
241                 TSC2301_ADCREG_CLOCK_2MHZ |
242                 flags;
243
244         /* Now we prepare the command for transferring */
245         data[0] = TSC2301_REG_ADC;
246         data[1] = val1;
247         data[2] = TSC2301_REG_ADC;
248         data[3] = val2;
249         data[4] = TSC2301_REG_ADC;
250         data[5] = val3;
251         data[6] = TSC2301_REG_REF;
252         data[7] = 1 << 4 | 1 << 2 | 1; /* intref, 100uS settl, 2.5V ref */
253         data[8] = TSC2301_REG_CONFIG;
254         data[9] = 3 << 3 | 2 << 0; /* 340uS pre-chrg, 544us delay */
255
256         spi_message_init(&m);
257         m.spi = tsc->spi;
258
259         memset(xfer, 0, sizeof(xfer));
260         x = &xfer[0];
261
262         for (i = 0; i < 10; i += 2) {
263                 x->tx_buf = &data[i];
264                 x->len = 4;
265                 if (i != 8)
266                         x->cs_change = 1;
267                 spi_message_add_tail(x, &m);
268                 x++;
269         }
270         spi_sync(m.spi, &m);
271
272         return 0;
273 }
274
275 static void tsc2301_ts_start_scan(struct tsc2301 *tsc)
276 {
277         tsc2301_ts_configure(tsc, tsc->ts->hw_flags);
278 }
279
280 static void tsc2301_ts_stop_scan(struct tsc2301 *tsc)
281 {
282         tsc2301_write_reg(tsc, TSC2301_REG_ADC, TSC2301_ADCREG_STOP_CONVERSION);
283 }
284
285 static void update_pen_state(struct tsc2301_ts *ts, int x, int y, int pressure)
286 {
287         if (pressure) {
288                 input_report_abs(ts->idev, ABS_X, x);
289                 input_report_abs(ts->idev, ABS_Y, y);
290                 input_report_abs(ts->idev, ABS_PRESSURE, pressure);
291                 if (!ts->pen_down)
292                         input_report_key(ts->idev, BTN_TOUCH, 1);
293                 ts->pen_down = 1;
294         } else {
295                 input_report_abs(ts->idev, ABS_PRESSURE, 0);
296                 if (ts->pen_down)
297                         input_report_key(ts->idev, BTN_TOUCH, 0);
298                 ts->pen_down = 0;
299         }
300
301         input_sync(ts->idev);
302
303 #ifdef VERBOSE
304         dev_dbg(&tsc->spi->dev, "x %4d y %4d p %4d\n", x, y, pressure);
305 #endif
306 }
307
308 static int filter(struct tsc2301_ts *ts, int x, int y, int z1, int z2)
309 {
310         int inside_rect, pressure_limit, Rt;
311         struct ts_filter *tsf = &ts->filter;
312
313         /* validate pressure and position */
314         if (x > MAX_12BIT || y > MAX_12BIT)
315                 return 0;
316
317         /* skip coords if the pressure-components are out of range */
318         if (z1 < TSF_MIN_Z1 || z2 > TSF_MAX_Z2)
319                 return 0;
320
321         /* Use the x,y,z1,z2 directly on the first "pen down" event */
322         if (ts->event_sent) {
323                 tsf->avg_x  += x;
324                 tsf->avg_y  += y;
325                 tsf->avg_z1 += z1;
326                 tsf->avg_z2 += z2;
327
328                 if (++tsf->sample_cnt < TSF_SAMPLES)
329                         return 0;
330                 x = tsf->avg_x / TSF_SAMPLES;
331                 y = tsf->avg_y / TSF_SAMPLES;
332                 z1 = tsf->avg_z1 / TSF_SAMPLES;
333                 z2 = tsf->avg_z2 / TSF_SAMPLES;
334         }
335         tsf->sample_cnt = 0;
336         tsf->avg_x  = 0;
337         tsf->avg_y  = 0;
338         tsf->avg_z1 = 0;
339         tsf->avg_z2 = 0;
340
341         pressure_limit = ts->event_sent? ts->max_pressure: ts->touch_pressure;
342
343         /* z1 is always at least 100: */
344         Rt = x * (z2 - z1) / z1;
345         Rt = Rt * ts->x_plate_ohm / 4096;
346         if (Rt > pressure_limit)
347                 return 0;
348
349         /* discard the event if it still is within the previous rect - unless
350          * if the pressure is harder, but then use previous x,y position */
351         inside_rect = (
352             x > (int)ts->x - TS_RECT_SIZE && x < (int)ts->x + TS_RECT_SIZE &&
353             y > (int)ts->y - TS_RECT_SIZE && y < (int)ts->y + TS_RECT_SIZE);
354
355         if (!ts->event_sent || !inside_rect) {
356                 ts->x = x;
357                 ts->y = y;
358                 ts->p = Rt;
359                 return 1;
360         } else if (Rt < ts->p) {
361                 ts->p = Rt;
362                 return 1;
363         }
364         return 0;
365 }
366
367 /*
368  * This procedure is called by the SPI framework after the coordinates
369  * have been read from TSC2301
370  */
371 static void tsc2301_ts_rx(void *arg)
372 {
373         struct tsc2301 *tsc = arg;
374         struct tsc2301_ts *ts = tsc->ts;
375         int send_event;
376         int x, y, z1, z2;
377
378         x  = ts->data[0];
379         y  = ts->data[1];
380         z1 = ts->data[2];
381         z2 = ts->data[3];
382
383         send_event = filter(ts, x, y, z1, z2);
384         if (send_event) {
385                 update_pen_state(ts, ts->x, ts->y, ts->p);
386                 ts->event_sent = 1;
387         }
388
389         mod_timer(&ts->penup_timer,
390                   jiffies + msecs_to_jiffies(TSC2301_TS_PENUP_TIME));
391 }
392
393 /*
394  * Timer is called TSC2301_TS_PENUP_TIME after pen is up
395  */
396 static void tsc2301_ts_timer_handler(unsigned long data)
397 {
398         struct tsc2301 *tsc = (struct tsc2301 *)data;
399         struct tsc2301_ts *ts = tsc->ts;
400
401         if (ts->event_sent) {
402                 ts->event_sent = 0;
403                 update_pen_state(ts, 0, 0, 0);
404         }
405 }
406
407 /*
408  * This interrupt is called when pen is down and coordinates are
409  * available. That is indicated by a falling edge on DEV line.
410  */
411 static irqreturn_t tsc2301_ts_irq_handler(int irq, void *dev_id)
412 {
413         struct tsc2301 *tsc = dev_id;
414         struct tsc2301_ts *ts = tsc->ts;
415         int r;
416
417         r = spi_async(tsc->spi, &ts->read_msg);
418         if (r)
419                 dev_err(&tsc->spi->dev, "ts: spi_async() failed");
420
421         mod_timer(&ts->penup_timer,
422                   jiffies + msecs_to_jiffies(TSC2301_TS_PENUP_TIME));
423
424         return IRQ_HANDLED;
425 }
426
427 static void tsc2301_ts_disable(struct tsc2301 *tsc)
428 {
429         struct tsc2301_ts *ts = tsc->ts;
430
431         if (ts->disable_depth++ != 0)
432                 return;
433
434         disable_irq(ts->irq);
435
436         /* wait until penup timer expire normally */
437         do {
438                 msleep(1);
439         } while (ts->event_sent);
440
441         tsc2301_ts_stop_scan(tsc);
442         /* Workaround a bug where turning on / off touchscreen scanner
443          * can get the keypad scanner stuck.
444          */
445         tsc2301_kp_restart(tsc);
446 }
447
448 static void tsc2301_ts_enable(struct tsc2301 *tsc)
449 {
450         struct tsc2301_ts *ts = tsc->ts;
451
452         if (--ts->disable_depth != 0)
453                 return;
454
455         enable_irq(ts->irq);
456
457         tsc2301_ts_start_scan(tsc);
458         /* Same workaround as above. */
459         tsc2301_kp_restart(tsc);
460 }
461
462 #ifdef CONFIG_PM
463 int tsc2301_ts_suspend(struct tsc2301 *tsc)
464 {
465         struct tsc2301_ts *ts = tsc->ts;
466
467         mutex_lock(&ts->mutex);
468         tsc2301_ts_disable(tsc);
469         mutex_unlock(&ts->mutex);
470
471         return 0;
472 }
473
474 void tsc2301_ts_resume(struct tsc2301 *tsc)
475 {
476         struct tsc2301_ts *ts = tsc->ts;
477
478         mutex_lock(&ts->mutex);
479         tsc2301_ts_enable(tsc);
480         mutex_unlock(&ts->mutex);
481 }
482 #endif
483
484 static void tsc2301_ts_setup_spi_xfer(struct tsc2301 *tsc)
485 {
486         struct tsc2301_ts *ts = tsc->ts;
487         struct spi_message *m = &ts->read_msg;
488         struct spi_transfer *x = &ts->read_xfer[0];
489
490         spi_message_init(m);
491
492         x->tx_buf = &tsc2301_ts_read_data;
493         x->len = 2;
494         spi_message_add_tail(x, m);
495
496         x++;
497         x->rx_buf = &ts->data;
498         x->len = 8;
499         spi_message_add_tail(x, m);
500
501         m->complete = tsc2301_ts_rx;
502         m->context = tsc;
503 }
504
505 static ssize_t tsc2301_ts_pen_down_show(struct device *dev,
506                                         struct device_attribute *attr,
507                                         char *buf)
508 {
509         struct tsc2301 *tsc = dev_get_drvdata(dev);
510
511         return sprintf(buf, "%u\n", tsc->ts->pen_down);
512 }
513
514 static DEVICE_ATTR(pen_down, S_IRUGO, tsc2301_ts_pen_down_show, NULL);
515
516 static ssize_t tsc2301_ts_disable_show(struct device *dev,
517                                        struct device_attribute *attr, char *buf)
518 {
519         struct tsc2301          *tsc = dev_get_drvdata(dev);
520         struct tsc2301_ts       *ts = tsc->ts;
521
522         return sprintf(buf, "%u\n", ts->disabled);
523 }
524
525 static ssize_t tsc2301_ts_disable_store(struct device *dev,
526                                         struct device_attribute *attr,
527                                         const char *buf, size_t count)
528 {
529         struct tsc2301          *tsc = dev_get_drvdata(dev);
530         struct tsc2301_ts       *ts = tsc->ts;
531         char *endp;
532         int i;
533
534         i = simple_strtoul(buf, &endp, 10);
535         i = i ? 1 : 0;
536         mutex_lock(&ts->mutex);
537         if (i == ts->disabled) goto out;
538         ts->disabled = i;
539
540         if (i)
541                 tsc2301_ts_disable(tsc);
542         else
543                 tsc2301_ts_enable(tsc);
544 out:
545         mutex_unlock(&ts->mutex);
546         return count;
547 }
548
549 static DEVICE_ATTR(disable_ts, 0664, tsc2301_ts_disable_show,
550                    tsc2301_ts_disable_store);
551
552 int __devinit tsc2301_ts_init(struct tsc2301 *tsc,
553                               struct tsc2301_platform_data *pdata)
554 {
555         struct tsc2301_ts *ts;
556         struct input_dev *idev;
557         int dav_gpio, r;
558         int x_max, y_max;
559         int x_fudge, y_fudge, p_fudge;
560
561         if (pdata->dav_gpio < 0) {
562                 dev_err(&tsc->spi->dev, "need DAV GPIO");
563                 return -EINVAL;
564         }
565         dav_gpio = pdata->dav_gpio;
566
567         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
568         if (ts == NULL)
569                 return -ENOMEM;
570         tsc->ts = ts;
571
572         ts->dav_gpio = dav_gpio;
573 #ifdef CONFIG_ARCH_OMAP
574         r = omap_request_gpio(dav_gpio);
575         if (r < 0) {
576                 dev_err(&tsc->spi->dev, "unable to get DAV GPIO");
577                 goto err1;
578         }
579         omap_set_gpio_direction(dav_gpio, 1);
580         ts->irq = OMAP_GPIO_IRQ(dav_gpio);
581 #endif
582         init_timer(&ts->penup_timer);
583         setup_timer(&ts->penup_timer, tsc2301_ts_timer_handler,
584                         (unsigned long)tsc);
585
586         mutex_init(&ts->mutex);
587
588         ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
589         ts->hw_avg_max  = pdata->ts_hw_avg;
590         ts->max_pressure = pdata->ts_max_pressure ? : MAX_12BIT;
591         ts->touch_pressure = pdata->ts_touch_pressure ? : ts->max_pressure;
592         ts->stab_time   = pdata->ts_stab_time;
593
594         x_max           = pdata->ts_x_max ? : 4096;
595         y_max           = pdata->ts_y_max ? : 4096;
596         x_fudge         = pdata->ts_x_fudge ? : 4;
597         y_fudge         = pdata->ts_y_fudge ? : 8;
598         p_fudge         = pdata->ts_pressure_fudge ? : 2;
599
600         if ((r = tsc2301_ts_check_config(ts, &ts->hw_flags))) {
601                 dev_err(&tsc->spi->dev, "invalid configuration\n");
602                 goto err2;
603         }
604
605         idev = input_allocate_device();
606         if (idev == NULL) {
607                 r = -ENOMEM;
608                 goto err2;
609         }
610         idev->name = "TSC2301 touchscreen";
611         snprintf(ts->phys, sizeof(ts->phys),
612                  "%s/input-ts", tsc->spi->dev.bus_id);
613         idev->phys = ts->phys;
614
615         idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
616         idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
617         ts->idev = idev;
618
619         tsc2301_ts_setup_spi_xfer(tsc);
620
621         /* These parameters should perhaps be configurable? */
622         input_set_abs_params(idev, ABS_X, 0, x_max, x_fudge, 0);
623         input_set_abs_params(idev, ABS_Y, 0, y_max, y_fudge, 0);
624         input_set_abs_params(idev, ABS_PRESSURE, 0, ts->max_pressure,
625                              p_fudge, 0);
626
627         tsc2301_ts_start_scan(tsc);
628
629         r = request_irq(ts->irq, tsc2301_ts_irq_handler,
630                         IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
631                         "tsc2301-ts", tsc);
632         if (r < 0) {
633                 dev_err(&tsc->spi->dev, "unable to get DAV IRQ");
634                 goto err3;
635         }
636         set_irq_wake(ts->irq, 1);
637
638         if (device_create_file(&tsc->spi->dev, &dev_attr_pen_down) < 0)
639                 goto err4;
640         if (device_create_file(&tsc->spi->dev, &dev_attr_disable_ts) < 0)
641                 goto err5;
642
643         r = input_register_device(idev);
644         if (r < 0) {
645                 dev_err(&tsc->spi->dev, "can't register touchscreen device\n");
646                 goto err6;
647         }
648
649         return 0;
650 err6:
651         device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
652 err5:
653         device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
654 err4:
655         free_irq(ts->irq, tsc);
656 err3:
657         tsc2301_ts_stop_scan(tsc);
658         input_free_device(idev);
659 err2:
660 #ifdef CONFIG_ARCH_OMAP
661         omap_free_gpio(dav_gpio);
662 #endif
663 err1:
664         kfree(ts);
665         return r;
666 }
667
668 void __devexit tsc2301_ts_exit(struct tsc2301 *tsc)
669 {
670         struct tsc2301_ts *ts = tsc->ts;
671
672         tsc2301_ts_disable(tsc);
673
674         device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
675         device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
676
677         free_irq(ts->irq, tsc);
678         input_unregister_device(ts->idev);
679
680 #ifdef CONFIG_ARCH_OMAP
681         omap_free_gpio(ts->dav_gpio);
682 #endif
683         kfree(ts);
684 }
685 MODULE_AUTHOR("Jarkko Oikarinen <jarkko.oikarinen@nokia.com>");
686 MODULE_LICENSE("GPL");