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