]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/touchscreen/tsc2005.c
use gpio_direction_input (OMAP tree only)
[linux-2.6-omap-h63xx.git] / drivers / input / touchscreen / tsc2005.c
1 /*
2  * TSC2005 touchscreen driver
3  *
4  * Copyright (C) 2006-2008 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/spi/spi.h>
31
32 #ifdef CONFIG_ARCH_OMAP
33 #include <mach/gpio.h>
34 #endif
35
36 #include <linux/spi/tsc2005.h>
37
38 /**
39  * The touchscreen interface operates as follows:
40  *
41  * Initialize:
42  *    Request access to GPIO103 (DAV)
43  *    tsc2005_dav_irq_handler will trigger when DAV line goes down
44  *
45  *  1) Pen is pressed against touchscreeen
46  *  2) TSC2005 performs AD conversion
47  *  3) After the conversion is done TSC2005 drives DAV line down
48  *  4) GPIO IRQ is received and tsc2005_dav_irq_handler is called
49  *  5) tsc2005_ts_irq_handler queues up an spi transfer to fetch
50  *     the x, y, z1, z2 values
51  *  6) tsc2005_ts_rx() reports coordinates to input layer and
52  *     sets up tsc2005_ts_timer() to be called after TSC2005_TS_SCAN_TIME
53  *  7)  When the penup_timer expires, there have not been DAV interrupts
54  *     during the last 20ms which means the pen has been lifted.
55  */
56
57 #define TSC2005_VDD_LOWER_27
58
59 #ifdef TSC2005_VDD_LOWER_27
60 #define TSC2005_HZ     (10000000)
61 #else
62 #define TSC2005_HZ     (25000000)
63 #endif
64
65 #define TSC2005_CMD     (0x80)
66 #define TSC2005_REG     (0x00)
67
68 #define TSC2005_CMD_STOP        (1)
69 #define TSC2005_CMD_10BIT       (0 << 2)
70 #define TSC2005_CMD_12BIT       (1 << 2)
71
72 #define TSC2005_CMD_SCAN_XYZZ   (0 << 3)
73 #define TSC2005_CMD_SCAN_XY     (1 << 3)
74 #define TSC2005_CMD_SCAN_X      (2 << 3)
75 #define TSC2005_CMD_SCAN_Y      (3 << 3)
76 #define TSC2005_CMD_SCAN_ZZ     (4 << 3)
77 #define TSC2005_CMD_AUX_SINGLE  (5 << 3)
78 #define TSC2005_CMD_TEMP1       (6 << 3)
79 #define TSC2005_CMD_TEMP2       (7 << 3)
80 #define TSC2005_CMD_AUX_CONT    (8 << 3)
81 #define TSC2005_CMD_TEST_X_CONN (9 << 3)
82 #define TSC2005_CMD_TEST_Y_CONN (10 << 3)
83 /* command 11 reserved */
84 #define TSC2005_CMD_TEST_SHORT  (12 << 3)
85 #define TSC2005_CMD_DRIVE_XX    (13 << 3)
86 #define TSC2005_CMD_DRIVE_YY    (14 << 3)
87 #define TSC2005_CMD_DRIVE_YX    (15 << 3)
88
89 #define TSC2005_REG_X           (0 << 3)
90 #define TSC2005_REG_Y           (1 << 3)
91 #define TSC2005_REG_Z1          (2 << 3)
92 #define TSC2005_REG_Z2          (3 << 3)
93 #define TSC2005_REG_AUX         (4 << 3)
94 #define TSC2005_REG_TEMP1       (5 << 3)
95 #define TSC2005_REG_TEMP2       (6 << 3)
96 #define TSC2005_REG_STATUS      (7 << 3)
97 #define TSC2005_REG_AUX_HIGH    (8 << 3)
98 #define TSC2005_REG_AUX_LOW     (9 << 3)
99 #define TSC2005_REG_TEMP_HIGH   (10 << 3)
100 #define TSC2005_REG_TEMP_LOW    (11 << 3)
101 #define TSC2005_REG_CFR0        (12 << 3)
102 #define TSC2005_REG_CFR1        (13 << 3)
103 #define TSC2005_REG_CFR2        (14 << 3)
104 #define TSC2005_REG_FUNCTION    (15 << 3)
105
106 #define TSC2005_REG_PND0        (1 << 1)
107 #define TSC2005_REG_READ        (0x01)
108 #define TSC2005_REG_WRITE       (0x00)
109
110
111 #define TSC2005_CFR0_LONGSAMPLING       (1)
112 #define TSC2005_CFR0_DETECTINWAIT       (1 << 1)
113 #define TSC2005_CFR0_SENSETIME_32US     (0)
114 #define TSC2005_CFR0_SENSETIME_96US     (1 << 2)
115 #define TSC2005_CFR0_SENSETIME_544US    (1 << 3)
116 #define TSC2005_CFR0_SENSETIME_2080US   (1 << 4)
117 #define TSC2005_CFR0_SENSETIME_2656US   (0x001C)
118 #define TSC2005_CFR0_PRECHARGE_20US     (0x0000)
119 #define TSC2005_CFR0_PRECHARGE_84US     (0x0020)
120 #define TSC2005_CFR0_PRECHARGE_276US    (0x0040)
121 #define TSC2005_CFR0_PRECHARGE_1044US   (0x0080)
122 #define TSC2005_CFR0_PRECHARGE_1364US   (0x00E0)
123 #define TSC2005_CFR0_STABTIME_0US       (0x0000)
124 #define TSC2005_CFR0_STABTIME_100US     (0x0100)
125 #define TSC2005_CFR0_STABTIME_500US     (0x0200)
126 #define TSC2005_CFR0_STABTIME_1MS       (0x0300)
127 #define TSC2005_CFR0_STABTIME_5MS       (0x0400)
128 #define TSC2005_CFR0_STABTIME_100MS     (0x0700)
129 #define TSC2005_CFR0_CLOCK_4MHZ         (0x0000)
130 #define TSC2005_CFR0_CLOCK_2MHZ         (0x0800)
131 #define TSC2005_CFR0_CLOCK_1MHZ         (0x1000)
132 #define TSC2005_CFR0_RESOLUTION12       (0x2000)
133 #define TSC2005_CFR0_STATUS             (0x4000)
134 #define TSC2005_CFR0_PENMODE            (0x8000)
135
136 #define TSC2005_CFR0_INITVALUE  (TSC2005_CFR0_STABTIME_1MS  |   \
137                                  TSC2005_CFR0_CLOCK_1MHZ    |   \
138                                  TSC2005_CFR0_RESOLUTION12  |   \
139                                  TSC2005_CFR0_PRECHARGE_276US | \
140                                  TSC2005_CFR0_PENMODE)
141
142 #define TSC2005_CFR1_BATCHDELAY_0MS     (0x0000)
143 #define TSC2005_CFR1_BATCHDELAY_1MS     (0x0001)
144 #define TSC2005_CFR1_BATCHDELAY_2MS     (0x0002)
145 #define TSC2005_CFR1_BATCHDELAY_4MS     (0x0003)
146 #define TSC2005_CFR1_BATCHDELAY_10MS    (0x0004)
147 #define TSC2005_CFR1_BATCHDELAY_20MS    (0x0005)
148 #define TSC2005_CFR1_BATCHDELAY_40MS    (0x0006)
149 #define TSC2005_CFR1_BATCHDELAY_100MS   (0x0007)
150
151 #define TSC2005_CFR1_INITVALUE  (TSC2005_CFR1_BATCHDELAY_2MS)
152
153 #define TSC2005_CFR2_MAVE_TEMP  (0x0001)
154 #define TSC2005_CFR2_MAVE_AUX   (0x0002)
155 #define TSC2005_CFR2_MAVE_Z     (0x0004)
156 #define TSC2005_CFR2_MAVE_Y     (0x0008)
157 #define TSC2005_CFR2_MAVE_X     (0x0010)
158 #define TSC2005_CFR2_AVG_1      (0x0000)
159 #define TSC2005_CFR2_AVG_3      (0x0400)
160 #define TSC2005_CFR2_AVG_7      (0x0800)
161 #define TSC2005_CFR2_MEDIUM_1   (0x0000)
162 #define TSC2005_CFR2_MEDIUM_3   (0x1000)
163 #define TSC2005_CFR2_MEDIUM_7   (0x2000)
164 #define TSC2005_CFR2_MEDIUM_15  (0x3000)
165
166 #define TSC2005_CFR2_IRQ_DAV    (0x4000)
167 #define TSC2005_CFR2_IRQ_PEN    (0x8000)
168 #define TSC2005_CFR2_IRQ_PENDAV (0x0000)
169
170 #define TSC2005_CFR2_INITVALUE  (TSC2005_CFR2_IRQ_DAV   |       \
171                                  TSC2005_CFR2_MAVE_X    |       \
172                                  TSC2005_CFR2_MAVE_Y    |       \
173                                  TSC2005_CFR2_MAVE_Z    |       \
174                                  TSC2005_CFR2_MEDIUM_15 |       \
175                                  TSC2005_CFR2_AVG_7)
176
177 #define MAX_12BIT                                       ((1 << 12) - 1)
178 #define TS_SAMPLES                                      4
179 #define TS_RECT_SIZE                                    8
180 #define TSC2005_TS_PENUP_TIME                           20
181
182 static const u32 tsc2005_read_reg[] = {
183         (TSC2005_REG | TSC2005_REG_X | TSC2005_REG_READ) << 16,
184         (TSC2005_REG | TSC2005_REG_Y | TSC2005_REG_READ) << 16,
185         (TSC2005_REG | TSC2005_REG_Z1 | TSC2005_REG_READ) << 16,
186         (TSC2005_REG | TSC2005_REG_Z2 | TSC2005_REG_READ) << 16,
187 };
188 #define NUM_READ_REGS   (sizeof(tsc2005_read_reg)/sizeof(tsc2005_read_reg[0]))
189
190 struct tsc2005 {
191         struct spi_device       *spi;
192
193         struct input_dev        *idev;
194         char                    phys[32];
195         struct timer_list       penup_timer;
196         spinlock_t              lock;
197         struct mutex            mutex;
198
199         struct spi_message      read_msg;
200         struct spi_transfer     read_xfer[NUM_READ_REGS];
201         u32                     data[NUM_READ_REGS];
202
203         /* previous x,y,z */
204         int                     x;
205         int                     y;
206         int                     p;
207         /* average accumulators for each component */
208         int                     sample_cnt;
209         int                     avg_x;
210         int                     avg_y;
211         int                     avg_z1;
212         int                     avg_z2;
213         /* configuration */
214         int                     x_plate_ohm;
215         int                     hw_avg_max;
216         int                     stab_time;
217         int                     p_max;
218         int                     touch_pressure;
219         int                     irq;
220         s16                     dav_gpio;
221         /* status */
222         u8                      sample_sent;
223         u8                      pen_down;
224         u8                      disabled;
225         u8                      disable_depth;
226         u8                      spi_active;
227 };
228
229 static void tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
230 {
231         u16 data = TSC2005_CMD | TSC2005_CMD_12BIT | cmd;
232         struct spi_message msg;
233         struct spi_transfer xfer = { 0 };
234
235         xfer.tx_buf = &data;
236         xfer.rx_buf = NULL;
237         xfer.len = 1;
238         xfer.bits_per_word = 8;
239
240         spi_message_init(&msg);
241         spi_message_add_tail(&xfer, &msg);
242         spi_sync(ts->spi, &msg);
243 }
244
245 static void tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
246 {
247         u32 tx;
248         struct spi_message msg;
249         struct spi_transfer xfer = { 0 };
250
251         tx = (TSC2005_REG | reg | TSC2005_REG_PND0 |
252                TSC2005_REG_WRITE) << 16;
253         tx |= value;
254
255         xfer.tx_buf = &tx;
256         xfer.rx_buf = NULL;
257         xfer.len = 4;
258         xfer.bits_per_word = 24;
259
260         spi_message_init(&msg);
261         spi_message_add_tail(&xfer, &msg);
262         spi_sync(ts->spi, &msg);
263 }
264
265 static void tsc2005_ts_update_pen_state(struct tsc2005 *ts,
266                                         int x, int y, int pressure)
267 {
268         if (pressure) {
269                 input_report_abs(ts->idev, ABS_X, x);
270                 input_report_abs(ts->idev, ABS_Y, y);
271                 input_report_abs(ts->idev, ABS_PRESSURE, pressure);
272                 if (!ts->pen_down) {
273                         input_report_key(ts->idev, BTN_TOUCH, 1);
274                         ts->pen_down = 1;
275                 }
276         } else {
277                 input_report_abs(ts->idev, ABS_PRESSURE, 0);
278                 if (ts->pen_down) {
279                         input_report_key(ts->idev, BTN_TOUCH, 0);
280                         ts->pen_down = 0;
281                 }
282         }
283
284         input_sync(ts->idev);
285 }
286
287 /*
288  * This function is called by the SPI framework after the coordinates
289  * have been read from TSC2005
290  */
291 static void tsc2005_ts_rx(void *arg)
292 {
293         struct tsc2005 *ts = arg;
294         unsigned long flags;
295         int inside_rect, pressure_limit;
296         int x, y, z1, z2, pressure;
297
298         spin_lock_irqsave(&ts->lock, flags);
299
300         x = ts->data[0];
301         y = ts->data[1];
302         z1 = ts->data[2];
303         z2 = ts->data[3];
304
305         /* validate pressure and position */
306         if (x > MAX_12BIT || y > MAX_12BIT)
307                 goto out;
308
309         /* skip coords if the pressure-components are out of range */
310         if (z1 < 100 || z2 > 4000)
311                 goto out;
312
313         /* don't run average on the "pen down" event */
314         if (ts->sample_sent) {
315                 ts->avg_x += x;
316                 ts->avg_y += y;
317                 ts->avg_z1 += z1;
318                 ts->avg_z2 += z2;
319
320                 if (++ts->sample_cnt < TS_SAMPLES)
321                         goto out;
322
323                 x = ts->avg_x / TS_SAMPLES;
324                 y = ts->avg_y / TS_SAMPLES;
325                 z1 = ts->avg_z1 / TS_SAMPLES;
326                 z2 = ts->avg_z2 / TS_SAMPLES;
327         }
328
329         ts->sample_cnt = 0;
330         ts->avg_x = 0;
331         ts->avg_y = 0;
332         ts->avg_z1 = 0;
333         ts->avg_z2 = 0;
334
335         if (z1) {
336                 pressure = x * (z2 - z1) / z1;
337                 pressure = pressure * ts->x_plate_ohm / 4096;
338         } else
339                 goto out;
340
341         pressure_limit = ts->sample_sent? ts->p_max: ts->touch_pressure;
342         if (pressure > pressure_limit)
343                 goto out;
344
345         /* discard the event if it still is within the previous rect - unless
346          * if the pressure is harder, but then use previous x,y position */
347         inside_rect = (ts->sample_sent &&
348                 x > (int)ts->x - TS_RECT_SIZE &&
349                 x < (int)ts->x + TS_RECT_SIZE &&
350                 y > (int)ts->y - TS_RECT_SIZE &&
351                 y < (int)ts->y + TS_RECT_SIZE);
352         if (inside_rect)
353                 x = ts->x, y = ts->y;
354
355         if (!inside_rect || pressure < ts->p) {
356                 tsc2005_ts_update_pen_state(ts, x, y, pressure);
357                 ts->sample_sent = 1;
358                 ts->x = x;
359                 ts->y = y;
360                 ts->p = pressure;
361         }
362 out:
363         ts->spi_active = 0;
364         spin_unlock_irqrestore(&ts->lock, flags);
365
366         /* kick pen up timer - to make sure it expires again(!) */
367         if (ts->sample_sent)
368                 mod_timer(&ts->penup_timer,
369                           jiffies + msecs_to_jiffies(TSC2005_TS_PENUP_TIME));
370 }
371
372 static void tsc2005_ts_penup_timer_handler(unsigned long data)
373 {
374         struct tsc2005 *ts = (struct tsc2005 *)data;
375
376         if (ts->sample_sent) {
377                 tsc2005_ts_update_pen_state(ts, 0, 0, 0);
378                 ts->sample_sent = 0;
379         }
380 }
381
382 /*
383  * This interrupt is called when pen is down and coordinates are
384  * available. That is indicated by a falling edge on DAV line.
385  */
386 static irqreturn_t tsc2005_ts_irq_handler(int irq, void *dev_id)
387 {
388         struct tsc2005 *ts = dev_id;
389         int r;
390
391         if (ts->spi_active)
392                 return IRQ_HANDLED;
393
394         ts->spi_active = 1;
395         r = spi_async(ts->spi, &ts->read_msg);
396         if (r)
397                 dev_err(&ts->spi->dev, "ts: spi_async() failed");
398
399         /* kick pen up timer */
400         mod_timer(&ts->penup_timer,
401                   jiffies + msecs_to_jiffies(TSC2005_TS_PENUP_TIME));
402
403         return IRQ_HANDLED;
404 }
405
406 static void tsc2005_ts_setup_spi_xfer(struct tsc2005 *ts)
407 {
408         struct spi_message *m = &ts->read_msg;
409         struct spi_transfer *x = &ts->read_xfer[0];
410         int i;
411
412         spi_message_init(m);
413
414         for (i = 0; i < NUM_READ_REGS; i++, x++) {
415                 x->tx_buf = &tsc2005_read_reg[i];
416                 x->rx_buf = &ts->data[i];
417                 x->len = 4;
418                 x->bits_per_word = 24;
419                 x->cs_change = i < (NUM_READ_REGS - 1);
420                 spi_message_add_tail(x, m);
421         }
422
423         m->complete = tsc2005_ts_rx;
424         m->context = ts;
425 }
426
427 static ssize_t tsc2005_ts_pen_down_show(struct device *dev,
428                                         struct device_attribute *attr,
429                                         char *buf)
430 {
431         struct tsc2005 *tsc = dev_get_drvdata(dev);
432
433         return sprintf(buf, "%u\n", tsc->pen_down);
434 }
435
436 static DEVICE_ATTR(pen_down, S_IRUGO, tsc2005_ts_pen_down_show, NULL);
437
438 static int tsc2005_configure(struct tsc2005 *tsc, int flags)
439 {
440         tsc2005_write(tsc, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE);
441         tsc2005_write(tsc, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE);
442         tsc2005_write(tsc, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE);
443         tsc2005_cmd(tsc, flags);
444
445         return 0;
446 }
447
448 static void tsc2005_start_scan(struct tsc2005 *tsc)
449 {
450         tsc2005_configure(tsc, TSC2005_CMD_SCAN_XYZZ);
451 }
452
453 static void tsc2005_stop_scan(struct tsc2005 *tsc)
454 {
455         tsc2005_cmd(tsc, TSC2005_CMD_STOP);
456 }
457
458 /* Must be called with mutex held */
459 static void tsc2005_disable(struct tsc2005 *ts)
460 {
461         if (ts->disable_depth++ != 0)
462                 return;
463
464         disable_irq(ts->irq);
465
466         /* wait until penup timer expire normally */
467         do {
468                 msleep(4);
469         } while (ts->sample_sent);
470
471         tsc2005_stop_scan(ts);
472 }
473
474 static void tsc2005_enable(struct tsc2005 *ts)
475 {
476         if (--ts->disable_depth != 0)
477                 return;
478
479         enable_irq(ts->irq);
480
481         tsc2005_start_scan(ts);
482 }
483
484 static ssize_t tsc2005_disable_show(struct device *dev,
485                                     struct device_attribute *attr, char *buf)
486 {
487         struct tsc2005 *ts = dev_get_drvdata(dev);
488
489         return sprintf(buf, "%u\n", ts->disabled);
490 }
491
492 static ssize_t tsc2005_disable_store(struct device *dev,
493                                      struct device_attribute *attr,
494                                      const char *buf, size_t count)
495 {
496         struct tsc2005          *tsc = dev_get_drvdata(dev);
497         unsigned long res;
498         int i;
499
500         i = strict_strtoul(buf, 10, &res);
501         i = i ? 1 : 0;
502
503         mutex_lock(&tsc->mutex);
504         if (i == tsc->disabled)
505                 goto out;
506         tsc->disabled = i;
507
508         if (i)
509                 tsc2005_disable(tsc);
510         else
511                 tsc2005_enable(tsc);
512 out:
513         mutex_unlock(&tsc->mutex);
514         return count;
515 }
516
517 static DEVICE_ATTR(disable_ts, 0664, tsc2005_disable_show,
518                    tsc2005_disable_store);
519
520
521 static int __devinit tsc2005_ts_init(struct tsc2005 *ts,
522                                      struct tsc2005_platform_data *pdata)
523 {
524         struct input_dev *idev;
525         int dav_gpio, r;
526         int x_max, y_max;
527         int x_fudge, y_fudge, p_fudge;
528
529         if (pdata->dav_gpio < 0) {
530                 dev_err(&ts->spi->dev, "need DAV GPIO");
531                 return -EINVAL;
532         }
533         dav_gpio = pdata->dav_gpio;
534         ts->dav_gpio = dav_gpio;
535         dev_dbg(&ts->spi->dev, "TSC2005: DAV GPIO = %d\n", dav_gpio);
536
537 #ifdef CONFIG_ARCH_OMAP
538         r = omap_request_gpio(dav_gpio);
539         if (r < 0) {
540                 dev_err(&ts->spi->dev, "unable to get DAV GPIO");
541                 goto err1;
542         }
543         gpio_direction_input(dav_gpio);
544         ts->irq = OMAP_GPIO_IRQ(dav_gpio);
545         dev_dbg(&ts->spi->dev, "TSC2005: DAV IRQ = %d\n", ts->irq);
546 #endif
547         init_timer(&ts->penup_timer);
548         setup_timer(&ts->penup_timer, tsc2005_ts_penup_timer_handler,
549                         (unsigned long)ts);
550
551         spin_lock_init(&ts->lock);
552         mutex_init(&ts->mutex);
553
554         ts->x_plate_ohm         = pdata->ts_x_plate_ohm ? : 280;
555         ts->hw_avg_max          = pdata->ts_hw_avg;
556         ts->stab_time           = pdata->ts_stab_time;
557         x_max                   = pdata->ts_x_max ? : 4096;
558         x_fudge                 = pdata->ts_x_fudge ? : 4;
559         y_max                   = pdata->ts_y_max ? : 4096;
560         y_fudge                 = pdata->ts_y_fudge ? : 8;
561         ts->p_max               = pdata->ts_pressure_max ? : MAX_12BIT;
562         ts->touch_pressure      = pdata->ts_touch_pressure ? : ts->p_max;
563         p_fudge                 = pdata->ts_pressure_fudge ? : 2;
564
565         idev = input_allocate_device();
566         if (idev == NULL) {
567                 r = -ENOMEM;
568                 goto err2;
569         }
570
571         idev->name = "TSC2005 touchscreen";
572         snprintf(ts->phys, sizeof(ts->phys), "%s/input-ts",
573                  ts->spi->dev.bus_id);
574         idev->phys = ts->phys;
575
576         idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
577         idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
578         ts->idev = idev;
579
580         tsc2005_ts_setup_spi_xfer(ts);
581
582         input_set_abs_params(idev, ABS_X, 0, x_max, x_fudge, 0);
583         input_set_abs_params(idev, ABS_Y, 0, y_max, y_fudge, 0);
584         input_set_abs_params(idev, ABS_PRESSURE, 0, ts->p_max, p_fudge, 0);
585
586         tsc2005_start_scan(ts);
587
588         r = request_irq(ts->irq, tsc2005_ts_irq_handler,
589                         IRQF_TRIGGER_FALLING | IRQF_DISABLED |
590                         IRQF_SAMPLE_RANDOM, "tsc2005", ts);
591         if (r < 0) {
592                 dev_err(&ts->spi->dev, "unable to get DAV IRQ");
593                 goto err3;
594         }
595
596         set_irq_wake(ts->irq, 1);
597
598         r = input_register_device(idev);
599         if (r < 0) {
600                 dev_err(&ts->spi->dev, "can't register touchscreen device\n");
601                 goto err4;
602         }
603
604         /* We can tolerate these failing */
605         if (device_create_file(&ts->spi->dev, &dev_attr_pen_down));
606         if (device_create_file(&ts->spi->dev, &dev_attr_disable_ts));
607
608         return 0;
609 err4:
610         free_irq(ts->irq, ts);
611 err3:
612         tsc2005_stop_scan(ts);
613         input_free_device(idev);
614 err2:
615 #ifdef CONFIG_ARCH_OMAP
616         omap_free_gpio(dav_gpio);
617 #endif
618 err1:
619         return r;
620 }
621
622 static int __devinit tsc2005_probe(struct spi_device *spi)
623 {
624         struct tsc2005                  *tsc;
625         struct tsc2005_platform_data    *pdata = spi->dev.platform_data;
626         int r;
627
628         if (!pdata) {
629                 dev_dbg(&spi->dev, "no platform data?\n");
630                 return -ENODEV;
631         }
632
633         tsc = kzalloc(sizeof(*tsc), GFP_KERNEL);
634         if (tsc == NULL)
635                 return -ENOMEM;
636
637         dev_set_drvdata(&spi->dev, tsc);
638         tsc->spi = spi;
639         spi->dev.power.power_state = PMSG_ON;
640
641         spi->mode = SPI_MODE_0;
642         spi->bits_per_word = 8;
643         /* The max speed might've been defined by the board-specific
644          * struct */
645         if (!spi->max_speed_hz)
646                 spi->max_speed_hz = TSC2005_HZ;
647
648         spi_setup(spi);
649
650         r = tsc2005_ts_init(tsc, pdata);
651         if (r)
652                 goto err1;
653
654         return 0;
655
656 err1:
657         kfree(tsc);
658         return r;
659 }
660
661 static int __devexit tsc2005_remove(struct spi_device *spi)
662 {
663         struct tsc2005 *ts = dev_get_drvdata(&spi->dev);
664
665         mutex_lock(&ts->mutex);
666         tsc2005_disable(ts);
667         mutex_unlock(&ts->mutex);
668
669         device_remove_file(&ts->spi->dev, &dev_attr_disable_ts);
670         device_remove_file(&ts->spi->dev, &dev_attr_pen_down);
671
672         free_irq(ts->irq, ts);
673         input_unregister_device(ts->idev);
674
675 #ifdef CONFIG_ARCH_OMAP
676         omap_free_gpio(ts->dav_gpio);
677 #endif
678         kfree(ts);
679
680         return 0;
681 }
682
683 #ifdef CONFIG_PM
684 static int tsc2005_suspend(struct spi_device *spi, pm_message_t mesg)
685 {
686         struct tsc2005 *ts = dev_get_drvdata(&spi->dev);
687
688         mutex_lock(&ts->mutex);
689         tsc2005_disable(ts);
690         mutex_unlock(&ts->mutex);
691
692         return 0;
693 }
694
695 static int tsc2005_resume(struct spi_device *spi)
696 {
697         struct tsc2005 *ts = dev_get_drvdata(&spi->dev);
698
699         mutex_lock(&ts->mutex);
700         tsc2005_enable(ts);
701         mutex_unlock(&ts->mutex);
702
703         return 0;
704 }
705 #endif
706
707 static struct spi_driver tsc2005_driver = {
708         .driver = {
709                 .name = "tsc2005",
710                 .owner = THIS_MODULE,
711         },
712 #ifdef CONFIG_PM
713         .suspend = tsc2005_suspend,
714         .resume = tsc2005_resume,
715 #endif
716         .probe = tsc2005_probe,
717         .remove = __devexit_p(tsc2005_remove),
718 };
719
720 static int __init tsc2005_init(void)
721 {
722         printk(KERN_INFO "TSC2005 driver initializing\n");
723
724         return spi_register_driver(&tsc2005_driver);
725 }
726 module_init(tsc2005_init);
727
728 static void __exit tsc2005_exit(void)
729 {
730         spi_unregister_driver(&tsc2005_driver);
731 }
732 module_exit(tsc2005_exit);
733
734 MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
735 MODULE_LICENSE("GPL");
736 MODULE_ALIAS("platform:tsc2005");