[s3c24xx] bump to 2.6.30-rc6
[openwrt.git] / target / linux / s3c24xx / files-2.6.30 / drivers / input / touchscreen / s3c2410_ts.c
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.
6 *
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.
11 *
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
15 *
16 * Copyright (c) 2004 Arnaud Patard <arnaud.patard@rtp-net.org>
17 * iPAQ H1940 touchscreen support
18 *
19 * ChangeLog
20 *
21 * 2004-09-05: Herbert Pƶtzl <herbert@13thfloor.at>
22 * - added clock (de-)allocation code
23 *
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
32 *
33 * 2005-03-23: Arnaud Patard <arnaud.patard@rtp-net.org>
34 * - Make use of some undocumented features of the touchscreen
35 * controller
36 *
37 * 2007-05-23: Harald Welte <laforge@openmoko.org>
38 * - Add proper support for S32440
39 *
40 * 2008-06-23: Andy Green <andy@openmoko.com>
41 * - removed averaging system
42 * - added generic Touchscreen filter stuff
43 *
44 * 2008-11-27: Nelson Castillo <arhuaco@freaks-unidos.net>
45 * - improve interrupt handling
46 */
47
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>
62
63 #include <mach/regs-gpio.h>
64 #include <mach/ts.h>
65 #include <mach/hardware.h>
66 #include <plat/regs-adc.h>
67
68 #include <linux/touchscreen/ts_filter_chain.h>
69
70 /* For ts.dev.id.version */
71 #define S3C2410TSVERSION 0x0101
72
73 #define TSC_SLEEP (S3C2410_ADCTSC_PULL_UP_DISABLE | S3C2410_ADCTSC_XY_PST(0))
74
75 #define WAIT4INT(x) (((x)<<8) | \
76 S3C2410_ADCTSC_YM_SEN | \
77 S3C2410_ADCTSC_YP_SEN | \
78 S3C2410_ADCTSC_XP_SEN | \
79 S3C2410_ADCTSC_XY_PST(3))
80
81 #define AUTOPST (S3C2410_ADCTSC_YM_SEN | \
82 S3C2410_ADCTSC_YP_SEN | \
83 S3C2410_ADCTSC_XP_SEN | \
84 S3C2410_ADCTSC_AUTO_PST | \
85 S3C2410_ADCTSC_XY_PST(0))
86
87 #define DEBUG_LVL KERN_DEBUG
88
89 MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
90 MODULE_DESCRIPTION("s3c2410 touchscreen driver");
91 MODULE_LICENSE("GPL");
92
93 /*
94 * Definitions & global arrays.
95 */
96
97 static char *s3c2410ts_name = "s3c2410 TouchScreen";
98
99 #define TS_RELEASE_TIMEOUT (HZ >> 7 ? HZ >> 7 : 1) /* 8ms (5ms if HZ is 200) */
100 #define TS_EVENT_FIFO_SIZE (2 << 6) /* must be a power of 2 */
101
102 #define TS_STATE_STANDBY 0 /* initial state */
103 #define TS_STATE_PRESSED 1
104 #define TS_STATE_RELEASE_PENDING 2
105 #define TS_STATE_RELEASE 3
106
107 /*
108 * Per-touchscreen data.
109 */
110
111 struct s3c2410ts {
112 struct input_dev *dev;
113 struct ts_filter_chain *chain;
114 int is_down;
115 int state;
116 struct kfifo *event_fifo;
117 };
118
119 static struct s3c2410ts ts;
120
121 static void __iomem *base_addr;
122
123 /*
124 * A few low level functions.
125 */
126
127 static inline void s3c2410_ts_connect(void)
128 {
129 s3c2410_gpio_cfgpin(S3C2410_GPG12, S3C2410_GPG12_XMON);
130 s3c2410_gpio_cfgpin(S3C2410_GPG13, S3C2410_GPG13_nXPON);
131 s3c2410_gpio_cfgpin(S3C2410_GPG14, S3C2410_GPG14_YMON);
132 s3c2410_gpio_cfgpin(S3C2410_GPG15, S3C2410_GPG15_nYPON);
133 }
134
135 static void s3c2410_ts_start_adc_conversion(void)
136 {
137 writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST,
138 base_addr + S3C2410_ADCTSC);
139 writel(readl(base_addr + S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START,
140 base_addr + S3C2410_ADCCON);
141 }
142
143 /*
144 * Just send the input events.
145 */
146
147 enum ts_input_event {IE_DOWN = 0, IE_UP};
148
149 static void ts_input_report(int event, int coords[])
150 {
151 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
152 static char *s[] = {"down", "up"};
153 struct timeval tv;
154
155 do_gettimeofday(&tv);
156 #endif
157
158 if (event == IE_DOWN) {
159 input_report_abs(ts.dev, ABS_X, coords[0]);
160 input_report_abs(ts.dev, ABS_Y, coords[1]);
161 input_report_key(ts.dev, BTN_TOUCH, 1);
162 input_report_abs(ts.dev, ABS_PRESSURE, 1);
163
164 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
165 printk(DEBUG_LVL "T:%06d %6s (X:%03d, Y:%03d)\n",
166 (int)tv.tv_usec, s[event], coords[0], coords[1]);
167 #endif
168 } else {
169 input_report_key(ts.dev, BTN_TOUCH, 0);
170 input_report_abs(ts.dev, ABS_PRESSURE, 0);
171
172 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
173 printk(DEBUG_LVL "T:%06d %6s\n",
174 (int)tv.tv_usec, s[event]);
175 #endif
176 }
177
178 input_sync(ts.dev);
179 }
180
181 /*
182 * Manage the state of the touchscreen.
183 */
184
185 static void event_send_timer_f(unsigned long data);
186
187 static struct timer_list event_send_timer =
188 TIMER_INITIALIZER(event_send_timer_f, 0, 0);
189
190 static void event_send_timer_f(unsigned long data)
191 {
192 static int noop_counter;
193 int event_type;
194
195 while (__kfifo_get(ts.event_fifo, (unsigned char *)&event_type,
196 sizeof(int))) {
197 int buf[2];
198
199 switch (event_type) {
200 case 'D':
201 if (ts.state == TS_STATE_RELEASE_PENDING)
202 /* Ignore short UP event */
203 ts.state = TS_STATE_PRESSED;
204 break;
205
206 case 'U':
207 ts.state = TS_STATE_RELEASE_PENDING;
208 break;
209
210 case 'P':
211 if (ts.is_down) /* stylus_action needs a conversion */
212 s3c2410_ts_start_adc_conversion();
213
214 if (unlikely(__kfifo_get(ts.event_fifo,
215 (unsigned char *)buf,
216 sizeof(int) * 2)
217 != sizeof(int) * 2))
218 goto ts_exit_error;
219
220 ts_input_report(IE_DOWN, buf);
221 ts.state = TS_STATE_PRESSED;
222 break;
223
224 default:
225 goto ts_exit_error;
226 }
227
228 noop_counter = 0;
229 }
230
231 if (noop_counter++ >= 1) {
232 noop_counter = 0;
233 if (ts.state == TS_STATE_RELEASE_PENDING) {
234 /*
235 * We delay the UP event for a while to avoid jitter.
236 * If we get a DOWN event we do not send it.
237 */
238 ts_input_report(IE_UP, NULL);
239 ts.state = TS_STATE_STANDBY;
240
241 ts_filter_chain_clear(ts.chain);
242 }
243 } else {
244 mod_timer(&event_send_timer, jiffies + TS_RELEASE_TIMEOUT);
245 }
246
247 return;
248
249 ts_exit_error: /* should not happen unless we have a bug */
250 printk(KERN_ERR __FILE__ ": event_send_timer_f failed\n");
251 }
252
253 /*
254 * Manage interrupts.
255 */
256
257 static irqreturn_t stylus_updown(int irq, void *dev_id)
258 {
259 unsigned long data0;
260 unsigned long data1;
261 int event_type;
262
263 data0 = readl(base_addr+S3C2410_ADCDAT0);
264 data1 = readl(base_addr+S3C2410_ADCDAT1);
265
266 ts.is_down = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) &&
267 (!(data1 & S3C2410_ADCDAT0_UPDOWN));
268
269 event_type = ts.is_down ? 'D' : 'U';
270
271 if (unlikely(__kfifo_put(ts.event_fifo, (unsigned char *)&event_type,
272 sizeof(int)) != sizeof(int))) /* should not happen */
273 printk(KERN_ERR __FILE__": stylus_updown lost event!\n");
274
275 if (ts.is_down)
276 s3c2410_ts_start_adc_conversion();
277 else
278 writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
279
280 mod_timer(&event_send_timer, jiffies + 1);
281
282 return IRQ_HANDLED;
283 }
284
285 static irqreturn_t stylus_action(int irq, void *dev_id)
286 {
287 int buf[3];
288
289 /* Grab the ADC results. */
290 buf[1] = readl(base_addr + S3C2410_ADCDAT0) &
291 S3C2410_ADCDAT0_XPDATA_MASK;
292 buf[2] = readl(base_addr + S3C2410_ADCDAT1) &
293 S3C2410_ADCDAT1_YPDATA_MASK;
294
295 switch (ts_filter_chain_feed(ts.chain, &buf[1])) {
296 case 0:
297 /* The filter wants more points. */
298 s3c2410_ts_start_adc_conversion();
299 return IRQ_HANDLED;
300 case 1:
301 /* We have a point from the filters or no filtering enabled. */
302 buf[0] = 'P';
303 break;
304 default:
305 printk(KERN_ERR __FILE__
306 ":%d Invalid ts_filter_chain_feed return value.\n",
307 __LINE__);
308 case -1:
309 /* Error. Ignore the event. */
310 ts_filter_chain_clear(ts.chain);
311 writel(WAIT4INT(1), base_addr + S3C2410_ADCTSC);
312 return IRQ_HANDLED;
313 };
314
315 if (unlikely(__kfifo_put(ts.event_fifo, (unsigned char *)buf,
316 sizeof(int) * 3) != sizeof(int) * 3))
317 printk(KERN_ERR __FILE__":stylus_action bug.\n");
318
319 writel(WAIT4INT(1), base_addr + S3C2410_ADCTSC);
320 mod_timer(&event_send_timer, jiffies + 1);
321
322 return IRQ_HANDLED;
323 }
324
325 static struct clk *adc_clock;
326
327 /*
328 * The functions for inserting/removing us as a module.
329 */
330
331 static int __init s3c2410ts_probe(struct platform_device *pdev)
332 {
333 int rc;
334 struct s3c2410_ts_mach_info *info;
335 struct input_dev *input_dev;
336 int ret = 0;
337
338 dev_info(&pdev->dev, "Starting\n");
339
340 info = (struct s3c2410_ts_mach_info *)pdev->dev.platform_data;
341
342 if (!info)
343 {
344 dev_err(&pdev->dev, "Hm... too bad: no platform data for ts\n");
345 return -EINVAL;
346 }
347
348 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
349 printk(DEBUG_LVL "Entering s3c2410ts_init\n");
350 #endif
351
352 adc_clock = clk_get(NULL, "adc");
353 if (!adc_clock) {
354 dev_err(&pdev->dev, "failed to get adc clock source\n");
355 return -ENOENT;
356 }
357 clk_enable(adc_clock);
358
359 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
360 printk(DEBUG_LVL "got and enabled clock\n");
361 #endif
362
363 base_addr = ioremap(S3C2410_PA_ADC,0x20);
364 if (base_addr == NULL) {
365 dev_err(&pdev->dev, "Failed to remap register block\n");
366 ret = -ENOMEM;
367 goto bail0;
368 }
369
370
371 /* If we acutally are a S3C2410: Configure GPIOs */
372 if (!strcmp(pdev->name, "s3c2410-ts"))
373 s3c2410_ts_connect();
374
375 if ((info->presc & 0xff) > 0)
376 writel(S3C2410_ADCCON_PRSCEN |
377 S3C2410_ADCCON_PRSCVL(info->presc&0xFF),
378 base_addr + S3C2410_ADCCON);
379 else
380 writel(0, base_addr+S3C2410_ADCCON);
381
382 /* Initialise registers */
383 if ((info->delay & 0xffff) > 0)
384 writel(info->delay & 0xffff, base_addr + S3C2410_ADCDLY);
385
386 writel(WAIT4INT(0), base_addr + S3C2410_ADCTSC);
387
388 /* Initialise input stuff */
389 memset(&ts, 0, sizeof(struct s3c2410ts));
390 input_dev = input_allocate_device();
391
392 if (!input_dev) {
393 dev_err(&pdev->dev, "Unable to allocate the input device\n");
394 ret = -ENOMEM;
395 goto bail1;
396 }
397
398 ts.dev = input_dev;
399 ts.dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) |
400 BIT_MASK(EV_ABS);
401 ts.dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
402 input_set_abs_params(ts.dev, ABS_X, 0, 0x3FF, 0, 0);
403 input_set_abs_params(ts.dev, ABS_Y, 0, 0x3FF, 0, 0);
404 input_set_abs_params(ts.dev, ABS_PRESSURE, 0, 1, 0, 0);
405
406 ts.dev->name = s3c2410ts_name;
407 ts.dev->id.bustype = BUS_RS232;
408 ts.dev->id.vendor = 0xDEAD;
409 ts.dev->id.product = 0xBEEF;
410 ts.dev->id.version = S3C2410TSVERSION;
411 ts.state = TS_STATE_STANDBY;
412 ts.event_fifo = kfifo_alloc(TS_EVENT_FIFO_SIZE, GFP_KERNEL, NULL);
413 if (IS_ERR(ts.event_fifo)) {
414 ret = -EIO;
415 goto bail2;
416 }
417
418 /* create the filter chain set up for the 2 coordinates we produce */
419 ts.chain = ts_filter_chain_create(pdev, info->filter_config, 2);
420
421 if (IS_ERR(ts.chain))
422 goto bail2;
423
424 ts_filter_chain_clear(ts.chain);
425
426 /* Get irqs */
427 if (request_irq(IRQ_ADC, stylus_action, IRQF_SAMPLE_RANDOM,
428 "s3c2410_action", ts.dev)) {
429 dev_err(&pdev->dev, "Could not allocate ts IRQ_ADC !\n");
430 iounmap(base_addr);
431 ret = -EIO;
432 goto bail3;
433 }
434 if (request_irq(IRQ_TC, stylus_updown, IRQF_SAMPLE_RANDOM,
435 "s3c2410_action", ts.dev)) {
436 dev_err(&pdev->dev, "Could not allocate ts IRQ_TC !\n");
437 free_irq(IRQ_ADC, ts.dev);
438 iounmap(base_addr);
439 ret = -EIO;
440 goto bail4;
441 }
442
443 dev_info(&pdev->dev, "Successfully loaded\n");
444
445 /* All went ok, so register to the input system */
446 rc = input_register_device(ts.dev);
447 if (rc) {
448 ret = -EIO;
449 goto bail5;
450 }
451
452 return 0;
453
454 bail5:
455 free_irq(IRQ_TC, ts.dev);
456 free_irq(IRQ_ADC, ts.dev);
457 clk_disable(adc_clock);
458 iounmap(base_addr);
459 disable_irq(IRQ_TC);
460 bail4:
461 disable_irq(IRQ_ADC);
462 bail3:
463 ts_filter_chain_destroy(ts.chain);
464 kfifo_free(ts.event_fifo);
465 bail2:
466 input_unregister_device(ts.dev);
467 bail1:
468 iounmap(base_addr);
469 bail0:
470
471 return ret;
472 }
473
474 static int s3c2410ts_remove(struct platform_device *pdev)
475 {
476 disable_irq(IRQ_ADC);
477 disable_irq(IRQ_TC);
478 free_irq(IRQ_TC,ts.dev);
479 free_irq(IRQ_ADC,ts.dev);
480
481 if (adc_clock) {
482 clk_disable(adc_clock);
483 clk_put(adc_clock);
484 adc_clock = NULL;
485 }
486
487 input_unregister_device(ts.dev);
488 iounmap(base_addr);
489
490 ts_filter_chain_destroy(ts.chain);
491
492 kfifo_free(ts.event_fifo);
493
494 return 0;
495 }
496
497 #ifdef CONFIG_PM
498 static int s3c2410ts_suspend(struct platform_device *pdev, pm_message_t state)
499 {
500 writel(TSC_SLEEP, base_addr+S3C2410_ADCTSC);
501 writel(readl(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_STDBM,
502 base_addr+S3C2410_ADCCON);
503
504 disable_irq(IRQ_ADC);
505 disable_irq(IRQ_TC);
506
507 clk_disable(adc_clock);
508
509 return 0;
510 }
511
512 static int s3c2410ts_resume(struct platform_device *pdev)
513 {
514 struct s3c2410_ts_mach_info *info =
515 ( struct s3c2410_ts_mach_info *)pdev->dev.platform_data;
516
517 clk_enable(adc_clock);
518 mdelay(1);
519
520 ts_filter_chain_clear(ts.chain);
521
522 enable_irq(IRQ_ADC);
523 enable_irq(IRQ_TC);
524
525 if ((info->presc&0xff) > 0)
526 writel(S3C2410_ADCCON_PRSCEN |
527 S3C2410_ADCCON_PRSCVL(info->presc&0xFF),
528 base_addr+S3C2410_ADCCON);
529 else
530 writel(0,base_addr+S3C2410_ADCCON);
531
532 /* Initialise registers */
533 if ((info->delay & 0xffff) > 0)
534 writel(info->delay & 0xffff, base_addr+S3C2410_ADCDLY);
535
536 writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
537
538 return 0;
539 }
540
541 #else
542 #define s3c2410ts_suspend NULL
543 #define s3c2410ts_resume NULL
544 #endif
545
546 static struct platform_driver s3c2410ts_driver = {
547 .driver = {
548 .name = "s3c2410-ts",
549 .owner = THIS_MODULE,
550 },
551 .probe = s3c2410ts_probe,
552 .remove = s3c2410ts_remove,
553 .suspend = s3c2410ts_suspend,
554 .resume = s3c2410ts_resume,
555
556 };
557
558 static struct platform_driver s3c2440ts_driver = {
559 .driver = {
560 .name = "s3c2440-ts",
561 .owner = THIS_MODULE,
562 },
563 .probe = s3c2410ts_probe,
564 .remove = s3c2410ts_remove,
565 .suspend = s3c2410ts_suspend,
566 .resume = s3c2410ts_resume,
567
568 };
569
570 static int __init s3c2410ts_init(void)
571 {
572 int rc;
573
574 rc = platform_driver_register(&s3c2410ts_driver);
575 if (rc < 0)
576 return rc;
577
578 rc = platform_driver_register(&s3c2440ts_driver);
579 if (rc < 0)
580 platform_driver_unregister(&s3c2410ts_driver);
581
582 return rc;
583 }
584
585 static void __exit s3c2410ts_exit(void)
586 {
587 platform_driver_unregister(&s3c2440ts_driver);
588 platform_driver_unregister(&s3c2410ts_driver);
589 }
590
591 module_init(s3c2410ts_init);
592 module_exit(s3c2410ts_exit);
593
This page took 0.068755 seconds and 5 git commands to generate.