]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/touchscreen/tsc2301_ts.c
148d8be02bb9b27ad7a41323fc08f3bc3bc5518a
[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 struct tsc2301_ts {
104         struct input_dev        *idev;
105         char                    phys[32];
106         struct timer_list       penup_timer;
107         struct mutex            mutex;
108
109         struct spi_transfer     read_xfer[2];
110         struct spi_message      read_msg;
111         u16                     data[4];
112
113         int                     hw_avg_max;
114         u16                     x;
115         u16                     y;
116         u16                     p;
117         int                     sample_cnt;
118
119         u16                     x_plate_ohm;
120         int                     stab_time;
121         int                     max_pressure;
122         int                     touch_pressure;
123
124         u8                      event_sent;
125         u8                      pen_down;
126         u8                      disabled;
127         u8                      disable_depth;
128
129         int                     hw_flags;
130
131         s16                     dav_gpio;
132         int                     irq;
133 };
134
135
136 static const u16 tsc2301_ts_read_data = 0x8000 | TSC2301_REG_X;
137
138 static int tsc2301_ts_check_config(struct tsc2301_ts *ts, int *hw_flags)
139 {
140         int flags;
141
142         flags = 0;
143         switch (ts->hw_avg_max) {
144         case 0:
145                 flags |= TSC2301_ADCREG_AVERAGING_NONE;
146                 break;
147         case 4:
148                 flags |= TSC2301_ADCREG_AVERAGING_4AVG;
149                 break;
150         case 8:
151                 flags |= TSC2301_ADCREG_AVERAGING_8AVG;
152                 break;
153         case 16:
154                 flags |= TSC2301_ADCREG_AVERAGING_16AVG;
155                 break;
156         default:
157                 return -EINVAL;
158         }
159
160         switch (ts->stab_time) {
161         case 0:
162                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_0US;
163                 break;
164         case 100:
165                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_100US;
166                 break;
167         case 500:
168                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_500US;
169                 break;
170         case 1000:
171                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_1MS;
172                 break;
173         case 5000:
174                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_5MS;
175                 break;
176         case 10000:
177                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_10MS;
178                 break;
179         case 50000:
180                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_50MS;
181                 break;
182         case 100000:
183                 flags |= TSC2301_ADCREG_VOLTAGE_STAB_100MS;
184                 break;
185         default:
186                 return -EINVAL;
187         }
188
189         *hw_flags = flags;
190         return 0;
191 }
192
193 /*
194  * This odd three-time initialization is to work around a bug in TSC2301.
195  * See TSC2301 errata for details.
196  */
197 static int tsc2301_ts_configure(struct tsc2301 *tsc, int flags)
198 {
199         struct spi_transfer xfer[5];
200         struct spi_transfer *x;
201         struct spi_message m;
202         int i;
203         u16 val1, val2, val3;
204         u16 data[10];
205
206         val1 = TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST |
207                 TSC2301_ADCREG_STOP_CONVERSION |
208                 TSC2301_ADCREG_FUNCTION_NONE |
209                 TSC2301_ADCREG_RESOLUTION_12BIT |
210                 TSC2301_ADCREG_AVERAGING_NONE |
211                 TSC2301_ADCREG_CLOCK_2MHZ |
212                 TSC2301_ADCREG_VOLTAGE_STAB_100MS;
213
214         val2 = TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST |
215                 TSC2301_ADCREG_FUNCTION_XYZ |
216                 TSC2301_ADCREG_RESOLUTION_12BIT |
217                 TSC2301_ADCREG_AVERAGING_16AVG |
218                 TSC2301_ADCREG_CLOCK_1MHZ |
219                 TSC2301_ADCREG_VOLTAGE_STAB_100MS;
220
221         /* Averaging and voltage stabilization settings in flags */
222         val3 = TSC2301_ADCREG_CONVERSION_CTRL_BY_TSC2301 |
223                 TSC2301_ADCREG_FUNCTION_XYZ |
224                 TSC2301_ADCREG_RESOLUTION_12BIT |
225                 TSC2301_ADCREG_CLOCK_2MHZ |
226                 flags;
227
228         /* Now we prepare the command for transferring */
229         data[0] = TSC2301_REG_ADC;
230         data[1] = val1;
231         data[2] = TSC2301_REG_ADC;
232         data[3] = val2;
233         data[4] = TSC2301_REG_ADC;
234         data[5] = val3;
235         data[6] = TSC2301_REG_REF;
236         data[7] = 1 << 4 | 1 << 2 | 1; /* intref, 100uS settl, 2.5V ref */
237         data[8] = TSC2301_REG_CONFIG;
238         data[9] = 3 << 3 | 2 << 0; /* 340uS pre-chrg, 544us delay */
239
240         spi_message_init(&m);
241         m.spi = tsc->spi;
242
243         memset(xfer, 0, sizeof(xfer));
244         x = &xfer[0];
245
246         for (i = 0; i < 10; i += 2) {
247                 x->tx_buf = &data[i];
248                 x->len = 4;
249                 if (i != 8)
250                         x->cs_change = 1;
251                 spi_message_add_tail(x, &m);
252                 x++;
253         }
254         spi_sync(m.spi, &m);
255
256         return 0;
257 }
258
259 static void tsc2301_ts_start_scan(struct tsc2301 *tsc)
260 {
261         tsc2301_ts_configure(tsc, tsc->ts->hw_flags);
262 }
263
264 static void tsc2301_ts_stop_scan(struct tsc2301 *tsc)
265 {
266         tsc2301_write_reg(tsc, TSC2301_REG_ADC, TSC2301_ADCREG_STOP_CONVERSION);
267 }
268
269 static void update_pen_state(struct tsc2301_ts *ts, int x, int y, int pressure)
270 {
271         if (pressure) {
272                 input_report_abs(ts->idev, ABS_X, x);
273                 input_report_abs(ts->idev, ABS_Y, y);
274                 input_report_abs(ts->idev, ABS_PRESSURE, pressure);
275                 if (!ts->pen_down)
276                         input_report_key(ts->idev, BTN_TOUCH, 1);
277                 ts->pen_down = 1;
278         } else {
279                 input_report_abs(ts->idev, ABS_PRESSURE, 0);
280                 if (ts->pen_down)
281                         input_report_key(ts->idev, BTN_TOUCH, 0);
282                 ts->pen_down = 0;
283         }
284
285         input_sync(ts->idev);
286
287 #ifdef VERBOSE
288         dev_dbg(&tsc->spi->dev, "x %4d y %4d p %4d\n", x, y, pressure);
289 #endif
290 }
291
292 static int filter(struct tsc2301_ts *ts, int x, int y, int z1, int z2)
293 {
294         int pressure, pressure_limit;
295
296         if (z1) {
297                 pressure = ts->x_plate_ohm * x;
298                 pressure /= 4096;
299                 pressure *= z2 - z1;
300                 pressure /= z1;
301         } else
302                 pressure = 0;
303
304         /* If pressure value is above a preset limit (pen is barely
305          * touching the screen) we can't trust the coordinate values.
306          */
307         pressure_limit = ts->event_sent? ts->max_pressure: ts->touch_pressure;
308
309         if (pressure < pressure_limit && x < MAX_12BIT && y < MAX_12BIT) {
310                 ts->x = x;
311                 ts->y = y;
312                 ts->p = pressure;
313                 return 1;
314         }
315         return 0;
316 }
317
318 /*
319  * This procedure is called by the SPI framework after the coordinates
320  * have been read from TSC2301
321  */
322 static void tsc2301_ts_rx(void *arg)
323 {
324         struct tsc2301 *tsc = arg;
325         struct tsc2301_ts *ts = tsc->ts;
326         int send_event;
327         int x, y, z1, z2;
328
329         x  = ts->data[0];
330         y  = ts->data[1];
331         z1 = ts->data[2];
332         z2 = ts->data[3];
333
334         send_event = filter(ts, x, y, z1, z2);
335         if (send_event) {
336                 update_pen_state(ts, ts->x, ts->y, ts->p);
337                 ts->event_sent = 1;
338         }
339
340         mod_timer(&ts->penup_timer,
341                   jiffies + msecs_to_jiffies(TSC2301_TS_PENUP_TIME));
342 }
343
344 /*
345  * Timer is called TSC2301_TS_PENUP_TIME after pen is up
346  */
347 static void tsc2301_ts_timer_handler(unsigned long data)
348 {
349         struct tsc2301 *tsc = (struct tsc2301 *)data;
350         struct tsc2301_ts *ts = tsc->ts;
351
352         if (ts->event_sent) {
353                 ts->event_sent = 0;
354                 update_pen_state(ts, 0, 0, 0);
355         }
356 }
357
358 /*
359  * This interrupt is called when pen is down and coordinates are
360  * available. That is indicated by a falling edge on DEV line.
361  */
362 static irqreturn_t tsc2301_ts_irq_handler(int irq, void *dev_id)
363 {
364         struct tsc2301 *tsc = dev_id;
365         struct tsc2301_ts *ts = tsc->ts;
366         int r;
367
368         r = spi_async(tsc->spi, &ts->read_msg);
369         if (r)
370                 dev_err(&tsc->spi->dev, "ts: spi_async() failed");
371
372         mod_timer(&ts->penup_timer,
373                   jiffies + msecs_to_jiffies(TSC2301_TS_PENUP_TIME));
374
375         return IRQ_HANDLED;
376 }
377
378 static void tsc2301_ts_disable(struct tsc2301 *tsc)
379 {
380         struct tsc2301_ts *ts = tsc->ts;
381
382         if (ts->disable_depth++ != 0)
383                 return;
384
385         disable_irq(ts->irq);
386
387         /* wait until penup timer expire normally */
388         do {
389                 msleep(1);
390         } while (ts->event_sent);
391
392         tsc2301_ts_stop_scan(tsc);
393         /* Workaround a bug where turning on / off touchscreen scanner
394          * can get the keypad scanner stuck.
395          */
396         tsc2301_kp_restart(tsc);
397 }
398
399 static void tsc2301_ts_enable(struct tsc2301 *tsc)
400 {
401         struct tsc2301_ts *ts = tsc->ts;
402
403         if (--ts->disable_depth != 0)
404                 return;
405
406         enable_irq(ts->irq);
407
408         tsc2301_ts_start_scan(tsc);
409         /* Same workaround as above. */
410         tsc2301_kp_restart(tsc);
411 }
412
413 #ifdef CONFIG_PM
414 int tsc2301_ts_suspend(struct tsc2301 *tsc)
415 {
416         struct tsc2301_ts *ts = tsc->ts;
417
418         mutex_lock(&ts->mutex);
419         tsc2301_ts_disable(tsc);
420         mutex_unlock(&ts->mutex);
421
422         return 0;
423 }
424
425 void tsc2301_ts_resume(struct tsc2301 *tsc)
426 {
427         struct tsc2301_ts *ts = tsc->ts;
428
429         mutex_lock(&ts->mutex);
430         tsc2301_ts_enable(tsc);
431         mutex_unlock(&ts->mutex);
432 }
433 #endif
434
435 static void tsc2301_ts_setup_spi_xfer(struct tsc2301 *tsc)
436 {
437         struct tsc2301_ts *ts = tsc->ts;
438         struct spi_message *m = &ts->read_msg;
439         struct spi_transfer *x = &ts->read_xfer[0];
440
441         spi_message_init(m);
442
443         x->tx_buf = &tsc2301_ts_read_data;
444         x->len = 2;
445         spi_message_add_tail(x, m);
446
447         x++;
448         x->rx_buf = &ts->data;
449         x->len = 8;
450         spi_message_add_tail(x, m);
451
452         m->complete = tsc2301_ts_rx;
453         m->context = tsc;
454 }
455
456 static ssize_t tsc2301_ts_pen_down_show(struct device *dev,
457                                         struct device_attribute *attr,
458                                         char *buf)
459 {
460         struct tsc2301 *tsc = dev_get_drvdata(dev);
461
462         return sprintf(buf, "%u\n", tsc->ts->pen_down);
463 }
464
465 static DEVICE_ATTR(pen_down, S_IRUGO, tsc2301_ts_pen_down_show, NULL);
466
467 static ssize_t tsc2301_ts_disable_show(struct device *dev,
468                                        struct device_attribute *attr, char *buf)
469 {
470         struct tsc2301          *tsc = dev_get_drvdata(dev);
471         struct tsc2301_ts       *ts = tsc->ts;
472
473         return sprintf(buf, "%u\n", ts->disabled);
474 }
475
476 static ssize_t tsc2301_ts_disable_store(struct device *dev,
477                                         struct device_attribute *attr,
478                                         const char *buf, size_t count)
479 {
480         struct tsc2301          *tsc = dev_get_drvdata(dev);
481         struct tsc2301_ts       *ts = tsc->ts;
482         char *endp;
483         int i;
484
485         i = simple_strtoul(buf, &endp, 10);
486         i = i ? 1 : 0;
487         mutex_lock(&ts->mutex);
488         if (i == ts->disabled) goto out;
489         ts->disabled = i;
490
491         if (i)
492                 tsc2301_ts_disable(tsc);
493         else
494                 tsc2301_ts_enable(tsc);
495 out:
496         mutex_unlock(&ts->mutex);
497         return count;
498 }
499
500 static DEVICE_ATTR(disable_ts, 0664, tsc2301_ts_disable_show,
501                    tsc2301_ts_disable_store);
502
503 int __devinit tsc2301_ts_init(struct tsc2301 *tsc,
504                               struct tsc2301_platform_data *pdata)
505 {
506         struct tsc2301_ts *ts;
507         struct input_dev *idev;
508         int dav_gpio, r;
509         int x_max, y_max;
510         int x_fudge, y_fudge, p_fudge;
511
512         if (pdata->dav_gpio < 0) {
513                 dev_err(&tsc->spi->dev, "need DAV GPIO");
514                 return -EINVAL;
515         }
516         dav_gpio = pdata->dav_gpio;
517
518         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
519         if (ts == NULL)
520                 return -ENOMEM;
521         tsc->ts = ts;
522
523         ts->dav_gpio = dav_gpio;
524 #ifdef CONFIG_ARCH_OMAP
525         r = omap_request_gpio(dav_gpio);
526         if (r < 0) {
527                 dev_err(&tsc->spi->dev, "unable to get DAV GPIO");
528                 goto err1;
529         }
530         omap_set_gpio_direction(dav_gpio, 1);
531         ts->irq = OMAP_GPIO_IRQ(dav_gpio);
532 #endif
533         init_timer(&ts->penup_timer);
534         setup_timer(&ts->penup_timer, tsc2301_ts_timer_handler,
535                         (unsigned long)tsc);
536
537         mutex_init(&ts->mutex);
538
539         ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
540         ts->hw_avg_max  = pdata->ts_hw_avg;
541         ts->max_pressure = pdata->ts_max_pressure ? : MAX_12BIT;
542         ts->touch_pressure = pdata->ts_touch_pressure ? : ts->max_pressure;
543         ts->stab_time   = pdata->ts_stab_time;
544
545         x_max           = pdata->ts_x_max ? : 4096;
546         y_max           = pdata->ts_y_max ? : 4096;
547         x_fudge         = pdata->ts_x_fudge ? : 4;
548         y_fudge         = pdata->ts_y_fudge ? : 8;
549         p_fudge         = pdata->ts_pressure_fudge ? : 2;
550
551         if ((r = tsc2301_ts_check_config(ts, &ts->hw_flags))) {
552                 dev_err(&tsc->spi->dev, "invalid configuration\n");
553                 goto err2;
554         }
555
556         idev = input_allocate_device();
557         if (idev == NULL) {
558                 r = -ENOMEM;
559                 goto err2;
560         }
561         idev->name = "TSC2301 touchscreen";
562         snprintf(ts->phys, sizeof(ts->phys),
563                  "%s/input-ts", tsc->spi->dev.bus_id);
564         idev->phys = ts->phys;
565
566         idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
567         idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
568         ts->idev = idev;
569
570         tsc2301_ts_setup_spi_xfer(tsc);
571
572         /* These parameters should perhaps be configurable? */
573         input_set_abs_params(idev, ABS_X, 0, x_max, x_fudge, 0);
574         input_set_abs_params(idev, ABS_Y, 0, y_max, y_fudge, 0);
575         input_set_abs_params(idev, ABS_PRESSURE, 0, ts->max_pressure,
576                              p_fudge, 0);
577
578         tsc2301_ts_start_scan(tsc);
579
580         r = request_irq(ts->irq, tsc2301_ts_irq_handler,
581                         IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
582                         "tsc2301-ts", tsc);
583         if (r < 0) {
584                 dev_err(&tsc->spi->dev, "unable to get DAV IRQ");
585                 goto err3;
586         }
587         set_irq_wake(ts->irq, 1);
588
589         if (device_create_file(&tsc->spi->dev, &dev_attr_pen_down) < 0)
590                 goto err4;
591         if (device_create_file(&tsc->spi->dev, &dev_attr_disable_ts) < 0)
592                 goto err5;
593
594         r = input_register_device(idev);
595         if (r < 0) {
596                 dev_err(&tsc->spi->dev, "can't register touchscreen device\n");
597                 goto err6;
598         }
599
600         return 0;
601 err6:
602         device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
603 err5:
604         device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
605 err4:
606         free_irq(ts->irq, tsc);
607 err3:
608         tsc2301_ts_stop_scan(tsc);
609         input_free_device(idev);
610 err2:
611 #ifdef CONFIG_ARCH_OMAP
612         omap_free_gpio(dav_gpio);
613 #endif
614 err1:
615         kfree(ts);
616         return r;
617 }
618
619 void __devexit tsc2301_ts_exit(struct tsc2301 *tsc)
620 {
621         struct tsc2301_ts *ts = tsc->ts;
622
623         tsc2301_ts_disable(tsc);
624
625         device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
626         device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
627
628         free_irq(ts->irq, tsc);
629         input_unregister_device(ts->idev);
630
631 #ifdef CONFIG_ARCH_OMAP
632         omap_free_gpio(ts->dav_gpio);
633 #endif
634         kfree(ts);
635 }
636 MODULE_AUTHOR("Jarkko Oikarinen <jarkko.oikarinen@nokia.com>");
637 MODULE_LICENSE("GPL");