S3C: Backported the s3c2410 touchscreen from openmoko
[linux-2.6/mini2440.git] / drivers / input / touchscreen / s3c2410_ts.c
blob0347c43c24b105dc0977c2f50552242755f2659e
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * Copyright (c) 2004 Arnaud Patard <arnaud.patard@rtp-net.org>
17 * iPAQ H1940 touchscreen support
19 * ChangeLog
21 * 2004-09-05: Herbert Pƶtzl <herbert@13thfloor.at>
22 * - added clock (de-)allocation code
24 * 2005-03-06: Arnaud Patard <arnaud.patard@rtp-net.org>
25 * - h1940_ -> s3c2410 (this driver is now also used on the n30
26 * machines :P)
27 * - Debug messages are now enabled with the config option
28 * TOUCHSCREEN_S3C2410_DEBUG
29 * - Changed the way the value are read
30 * - Input subsystem should now work
31 * - Use ioremap and readl/writel
33 * 2005-03-23: Arnaud Patard <arnaud.patard@rtp-net.org>
34 * - Make use of some undocumented features of the touchscreen
35 * controller
37 * 2007-05-23: Harald Welte <laforge@openmoko.org>
38 * - Add proper support for S32440
40 * 2008-06-23: Andy Green <andy@openmoko.com>
41 * - removed averaging system
42 * - added generic Touchscreen filter stuff
44 * 2008-11-27: Nelson Castillo <arhuaco@freaks-unidos.net>
45 * - improve interrupt handling
48 #include <linux/errno.h>
49 #include <linux/kernel.h>
50 #include <linux/module.h>
51 #include <linux/slab.h>
52 #include <linux/input.h>
53 #include <linux/init.h>
54 #include <linux/serio.h>
55 #include <linux/timer.h>
56 #include <linux/kfifo.h>
57 #include <linux/delay.h>
58 #include <linux/platform_device.h>
59 #include <linux/clk.h>
60 #include <asm/io.h>
61 #include <asm/irq.h>
63 #include <linux/gpio.h>
64 #include <mach/regs-gpio.h>
65 #include <mach/ts.h>
67 #include <plat/regs-adc.h>
69 #include "ts_filter.h"
71 /* For ts.dev.id.version */
72 #define S3C2410TSVERSION 0x0101
74 #define TSC_SLEEP (S3C2410_ADCTSC_PULL_UP_DISABLE | S3C2410_ADCTSC_XY_PST(0))
76 #define WAIT4INT(x) (((x)<<8) | \
77 S3C2410_ADCTSC_YM_SEN | \
78 S3C2410_ADCTSC_YP_SEN | \
79 S3C2410_ADCTSC_XP_SEN | \
80 S3C2410_ADCTSC_XY_PST(3))
82 #define AUTOPST (S3C2410_ADCTSC_YM_SEN | \
83 S3C2410_ADCTSC_YP_SEN | \
84 S3C2410_ADCTSC_XP_SEN | \
85 S3C2410_ADCTSC_AUTO_PST | \
86 S3C2410_ADCTSC_XY_PST(0))
88 #define DEBUG_LVL KERN_DEBUG
90 MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
91 MODULE_DESCRIPTION("s3c2410 touchscreen driver");
92 MODULE_LICENSE("GPL");
95 * Definitions & global arrays.
98 static char *s3c2410ts_name = "s3c2410 TouchScreen";
100 #define TS_RELEASE_TIMEOUT (HZ >> 7 ? HZ >> 7 : 1) /* 8ms (5ms if HZ is 200) */
101 #define TS_EVENT_FIFO_SIZE (2 << 6) /* must be a power of 2 */
103 #define TS_STATE_STANDBY 0 /* initial state */
104 #define TS_STATE_PRESSED 1
105 #define TS_STATE_RELEASE_PENDING 2
106 #define TS_STATE_RELEASE 3
109 * Per-touchscreen data.
112 struct s3c2410ts {
113 struct input_dev *dev;
114 struct ts_filter *tsf[MAX_TS_FILTER_CHAIN];
115 int coords[2]; /* just X and Y for us */
116 int is_down;
117 int state;
118 struct kfifo *event_fifo;
121 static struct s3c2410ts ts;
123 static void __iomem *base_addr;
126 * A few low level functions.
129 static inline void s3c2410_ts_connect(void)
131 s3c2410_gpio_cfgpin(S3C2410_GPG(12), S3C2410_GPG12_XMON);
132 s3c2410_gpio_cfgpin(S3C2410_GPG(13), S3C2410_GPG13_nXPON);
133 s3c2410_gpio_cfgpin(S3C2410_GPG(14), S3C2410_GPG14_YMON);
134 s3c2410_gpio_cfgpin(S3C2410_GPG(15), S3C2410_GPG15_nYPON);
137 static void s3c2410_ts_start_adc_conversion(void)
139 writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST,
140 base_addr + S3C2410_ADCTSC);
141 writel(readl(base_addr + S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START,
142 base_addr + S3C2410_ADCCON);
146 * Just send the input events.
149 enum ts_input_event {IE_DOWN = 0, IE_UP};
151 static void ts_input_report(int event, int coords[])
153 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
154 static char *s[] = {"down", "up"};
155 struct timeval tv;
157 do_gettimeofday(&tv);
158 #endif
160 if (event == IE_DOWN) {
161 input_report_abs(ts.dev, ABS_X, coords[0]);
162 input_report_abs(ts.dev, ABS_Y, coords[1]);
163 input_report_key(ts.dev, BTN_TOUCH, 1);
164 input_report_abs(ts.dev, ABS_PRESSURE, 1);
166 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
167 printk(DEBUG_LVL "T:%06d %6s (X:%03d, Y:%03d)\n",
168 (int)tv.tv_usec, s[event], coords[0], coords[1]);
169 #endif
170 } else {
171 input_report_key(ts.dev, BTN_TOUCH, 0);
172 input_report_abs(ts.dev, ABS_PRESSURE, 0);
174 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
175 printk(DEBUG_LVL "T:%06d %6s\n",
176 (int)tv.tv_usec, s[event]);
177 #endif
180 input_sync(ts.dev);
184 * Manage the state of the touchscreen.
187 static void event_send_timer_f(unsigned long data);
189 static struct timer_list event_send_timer =
190 TIMER_INITIALIZER(event_send_timer_f, 0, 0);
192 static void event_send_timer_f(unsigned long data)
194 static int noop_counter;
195 int event_type;
197 while (__kfifo_get(ts.event_fifo, (unsigned char *)&event_type,
198 sizeof(int))) {
199 int buf[2];
201 switch (event_type) {
202 case 'D':
203 if (ts.state == TS_STATE_RELEASE_PENDING)
204 /* Ignore short UP event */
205 ts.state = TS_STATE_PRESSED;
206 break;
208 case 'U':
209 ts.state = TS_STATE_RELEASE_PENDING;
210 break;
212 case 'P':
213 if (ts.is_down) /* stylus_action needs a conversion */
214 s3c2410_ts_start_adc_conversion();
216 if (unlikely(__kfifo_get(ts.event_fifo,
217 (unsigned char *)buf,
218 sizeof(int) * 2)
219 != sizeof(int) * 2))
220 goto ts_exit_error;
222 ts_input_report(IE_DOWN, buf);
223 ts.state = TS_STATE_PRESSED;
224 break;
226 default:
227 goto ts_exit_error;
230 noop_counter = 0;
233 if (noop_counter++ >= 1) {
234 noop_counter = 0;
235 if (ts.state == TS_STATE_RELEASE_PENDING) {
236 /* We delay the UP event for a
237 * while to avoid jitter. If we get a DOWN
238 * event we do not send it. */
240 ts_input_report(IE_UP, NULL);
241 ts.state = TS_STATE_STANDBY;
243 if (ts.tsf[0])
244 (ts.tsf[0]->api->clear)(ts.tsf[0]);
246 } else {
247 mod_timer(&event_send_timer, jiffies + TS_RELEASE_TIMEOUT);
250 return;
252 ts_exit_error: /* should not happen unless we have a bug */
253 printk(KERN_ERR __FILE__ ": event_send_timer_f failed\n");
257 * Manage interrupts.
260 static irqreturn_t stylus_updown(int irq, void *dev_id)
262 unsigned long data0;
263 unsigned long data1;
264 int event_type;
266 data0 = readl(base_addr+S3C2410_ADCDAT0);
267 data1 = readl(base_addr+S3C2410_ADCDAT1);
269 ts.is_down = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) &&
270 (!(data1 & S3C2410_ADCDAT0_UPDOWN));
272 event_type = ts.is_down ? 'D' : 'U';
274 if (unlikely(__kfifo_put(ts.event_fifo, (unsigned char *)&event_type,
275 sizeof(int)) != sizeof(int))) /* should not happen */
276 printk(KERN_ERR __FILE__": stylus_updown lost event!\n");
278 if (ts.is_down)
279 s3c2410_ts_start_adc_conversion();
280 else
281 writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
283 mod_timer(&event_send_timer, jiffies + 1);
285 return IRQ_HANDLED;
288 static irqreturn_t stylus_action(int irq, void *dev_id)
290 int buf[3];
292 /* grab the ADC results */
293 ts.coords[0] = readl(base_addr + S3C2410_ADCDAT0) &
294 S3C2410_ADCDAT0_XPDATA_MASK;
295 ts.coords[1] = readl(base_addr + S3C2410_ADCDAT1) &
296 S3C2410_ADCDAT1_YPDATA_MASK;
298 if (ts.tsf[0]) { /* filtering is enabled, don't use raw directly */
299 switch ((ts.tsf[0]->api->process)(ts.tsf[0], &ts.coords[0])) {
300 case 0: /*
301 * no real sample came out of processing yet,
302 * get another raw result to feed it
304 s3c2410_ts_start_adc_conversion();
305 return IRQ_HANDLED;
306 case 1: /* filters are ready to deliver a sample */
307 (ts.tsf[0]->api->scale)(ts.tsf[0], &ts.coords[0]);
308 break;
309 case -1:
310 /* error in filters, ignore the event */
311 (ts.tsf[0]->api->clear)(ts.tsf[0]);
312 writel(WAIT4INT(1), base_addr + S3C2410_ADCTSC);
313 return IRQ_HANDLED;
314 default:
315 printk(KERN_ERR":stylus_action error\n");
319 /* We use a buffer because want an atomic operation */
320 buf[0] = 'P';
321 buf[1] = ts.coords[0];
322 buf[2] = ts.coords[1];
324 if (unlikely(__kfifo_put(ts.event_fifo, (unsigned char *)buf,
325 sizeof(int) * 3) != sizeof(int) * 3))
326 /* should not happen */
327 printk(KERN_ERR":stylus_action error\n");
329 writel(WAIT4INT(1), base_addr + S3C2410_ADCTSC);
330 mod_timer(&event_send_timer, jiffies + 1);
332 return IRQ_HANDLED;
335 static struct clk *adc_clock;
338 * The functions for inserting/removing us as a module.
341 static int __init s3c2410ts_probe(struct platform_device *pdev)
343 int rc;
344 struct s3c2410_ts_mach_info *info;
345 struct input_dev *input_dev;
346 int ret = 0;
348 dev_info(&pdev->dev, "Starting\n");
350 info = (struct s3c2410_ts_mach_info *)pdev->dev.platform_data;
352 if (!info)
354 dev_err(&pdev->dev, "Hm... too bad: no platform data for ts\n");
355 return -EINVAL;
358 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
359 printk(DEBUG_LVL "Entering s3c2410ts_init\n");
360 #endif
362 adc_clock = clk_get(NULL, "adc");
363 if (!adc_clock) {
364 dev_err(&pdev->dev, "failed to get adc clock source\n");
365 return -ENOENT;
367 clk_enable(adc_clock);
369 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
370 printk(DEBUG_LVL "got and enabled clock\n");
371 #endif
373 base_addr = ioremap(S3C2410_PA_ADC,0x20);
374 if (base_addr == NULL) {
375 dev_err(&pdev->dev, "Failed to remap register block\n");
376 ret = -ENOMEM;
377 goto bail0;
381 /* If we acutally are a S3C2410: Configure GPIOs */
382 if (!strcmp(pdev->name, "s3c2410-ts"))
383 s3c2410_ts_connect();
385 if ((info->presc & 0xff) > 0)
386 writel(S3C2410_ADCCON_PRSCEN |
387 S3C2410_ADCCON_PRSCVL(info->presc&0xFF),
388 base_addr + S3C2410_ADCCON);
389 else
390 writel(0, base_addr+S3C2410_ADCCON);
392 /* Initialise registers */
393 if ((info->delay & 0xffff) > 0)
394 writel(info->delay & 0xffff, base_addr + S3C2410_ADCDLY);
396 writel(WAIT4INT(0), base_addr + S3C2410_ADCTSC);
398 /* Initialise input stuff */
399 memset(&ts, 0, sizeof(struct s3c2410ts));
400 input_dev = input_allocate_device();
402 if (!input_dev) {
403 dev_err(&pdev->dev, "Unable to allocate the input device\n");
404 ret = -ENOMEM;
405 goto bail1;
408 ts.dev = input_dev;
409 ts.dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) |
410 BIT_MASK(EV_ABS);
411 ts.dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
412 input_set_abs_params(ts.dev, ABS_X, 0, 0x3FF, 0, 0);
413 input_set_abs_params(ts.dev, ABS_Y, 0, 0x3FF, 0, 0);
414 input_set_abs_params(ts.dev, ABS_PRESSURE, 0, 1, 0, 0);
416 ts.dev->name = s3c2410ts_name;
417 ts.dev->id.bustype = BUS_RS232;
418 ts.dev->id.vendor = 0xDEAD;
419 ts.dev->id.product = 0xBEEF;
420 ts.dev->id.version = S3C2410TSVERSION;
421 ts.state = TS_STATE_STANDBY;
422 ts.event_fifo = kfifo_alloc(TS_EVENT_FIFO_SIZE, GFP_KERNEL, NULL);
423 if (IS_ERR(ts.event_fifo)) {
424 ret = -EIO;
425 goto bail2;
428 /* create the filter chain set up for the 2 coordinates we produce */
429 ret = ts_filter_create_chain(
430 pdev, (struct ts_filter_api **)&info->filter_sequence,
431 (void *)&info->filter_config, ts.tsf, ARRAY_SIZE(ts.coords));
432 if (ret)
433 dev_info(&pdev->dev, "%d filter(s) initialized\n", ret);
434 else /* this is OK, just means there won't be any filtering */
435 dev_info(&pdev->dev, "Unfiltered output selected\n");
437 if (ts.tsf[0])
438 (ts.tsf[0]->api->clear)(ts.tsf[0]);
439 else
440 dev_info(&pdev->dev, "No filtering\n");
442 /* Get irqs */
443 if (request_irq(IRQ_ADC, stylus_action, IRQF_SAMPLE_RANDOM,
444 "s3c2410_action", ts.dev)) {
445 dev_err(&pdev->dev, "Could not allocate ts IRQ_ADC !\n");
446 iounmap(base_addr);
447 ret = -EIO;
448 goto bail3;
450 if (request_irq(IRQ_TC, stylus_updown, IRQF_SAMPLE_RANDOM,
451 "s3c2410_action", ts.dev)) {
452 dev_err(&pdev->dev, "Could not allocate ts IRQ_TC !\n");
453 free_irq(IRQ_ADC, ts.dev);
454 iounmap(base_addr);
455 ret = -EIO;
456 goto bail4;
459 dev_info(&pdev->dev, "successfully loaded\n");
461 /* All went ok, so register to the input system */
462 rc = input_register_device(ts.dev);
463 if (rc) {
464 ret = -EIO;
465 goto bail5;
468 return 0;
470 bail5:
471 free_irq(IRQ_TC, ts.dev);
472 free_irq(IRQ_ADC, ts.dev);
473 clk_disable(adc_clock);
474 iounmap(base_addr);
475 disable_irq(IRQ_TC);
476 bail4:
477 disable_irq(IRQ_ADC);
478 bail3:
479 ts_filter_destroy_chain(pdev, ts.tsf);
480 kfifo_free(ts.event_fifo);
481 bail2:
482 input_unregister_device(ts.dev);
483 bail1:
484 iounmap(base_addr);
485 bail0:
487 return ret;
490 static int s3c2410ts_remove(struct platform_device *pdev)
492 disable_irq(IRQ_ADC);
493 disable_irq(IRQ_TC);
494 free_irq(IRQ_TC,ts.dev);
495 free_irq(IRQ_ADC,ts.dev);
497 if (adc_clock) {
498 clk_disable(adc_clock);
499 clk_put(adc_clock);
500 adc_clock = NULL;
503 input_unregister_device(ts.dev);
504 iounmap(base_addr);
506 ts_filter_destroy_chain(pdev, ts.tsf);
508 kfifo_free(ts.event_fifo);
510 return 0;
513 #ifdef CONFIG_PM
514 static int s3c2410ts_suspend(struct platform_device *pdev, pm_message_t state)
516 writel(TSC_SLEEP, base_addr+S3C2410_ADCTSC);
517 writel(readl(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_STDBM,
518 base_addr+S3C2410_ADCCON);
520 disable_irq(IRQ_ADC);
521 disable_irq(IRQ_TC);
523 clk_disable(adc_clock);
525 return 0;
528 static int s3c2410ts_resume(struct platform_device *pdev)
530 struct s3c2410_ts_mach_info *info =
531 ( struct s3c2410_ts_mach_info *)pdev->dev.platform_data;
533 clk_enable(adc_clock);
534 mdelay(1);
536 if (ts.tsf[0])
537 (ts.tsf[0]->api->clear)(ts.tsf[0]);
539 enable_irq(IRQ_ADC);
540 enable_irq(IRQ_TC);
542 if ((info->presc&0xff) > 0)
543 writel(S3C2410_ADCCON_PRSCEN |
544 S3C2410_ADCCON_PRSCVL(info->presc&0xFF),
545 base_addr+S3C2410_ADCCON);
546 else
547 writel(0,base_addr+S3C2410_ADCCON);
549 /* Initialise registers */
550 if ((info->delay & 0xffff) > 0)
551 writel(info->delay & 0xffff, base_addr+S3C2410_ADCDLY);
553 writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
555 return 0;
558 #else
559 #define s3c2410ts_suspend NULL
560 #define s3c2410ts_resume NULL
561 #endif
563 static struct platform_driver s3c2410ts_driver = {
564 .driver = {
565 .name = "s3c2410-ts",
566 .owner = THIS_MODULE,
568 .probe = s3c2410ts_probe,
569 .remove = s3c2410ts_remove,
570 .suspend = s3c2410ts_suspend,
571 .resume = s3c2410ts_resume,
575 static struct platform_driver s3c2440ts_driver = {
576 .driver = {
577 .name = "s3c2440-ts",
578 .owner = THIS_MODULE,
580 .probe = s3c2410ts_probe,
581 .remove = s3c2410ts_remove,
582 .suspend = s3c2410ts_suspend,
583 .resume = s3c2410ts_resume,
587 static int __init s3c2410ts_init(void)
589 int rc;
591 rc = platform_driver_register(&s3c2410ts_driver);
592 if (rc < 0)
593 return rc;
595 rc = platform_driver_register(&s3c2440ts_driver);
596 if (rc < 0)
597 platform_driver_unregister(&s3c2410ts_driver);
599 return rc;
602 static void __exit s3c2410ts_exit(void)
604 platform_driver_unregister(&s3c2440ts_driver);
605 platform_driver_unregister(&s3c2410ts_driver);
608 module_init(s3c2410ts_init);
609 module_exit(s3c2410ts_exit);