ar6000: minimise possibility of race in ar6000_ioctl_siwscan
[kernel.git] / 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
66 #include <plat/regs-adc.h>
67
68 #include <linux/ts_filter.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 >> 4)            /* ~ 60 milliseconds */
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 *tsf[MAX_TS_FILTER_CHAIN];
114         int coords[2]; /* just X and Y for us */
115         int is_down;
116         int state;
117         struct kfifo *event_fifo;
118 };
119
120 static struct s3c2410ts ts;
121
122 static void __iomem *base_addr;
123
124 /*
125  * A few low level functions.
126  */
127
128 static inline void s3c2410_ts_connect(void)
129 {
130         s3c2410_gpio_cfgpin(S3C2410_GPG12, S3C2410_GPG12_XMON);
131         s3c2410_gpio_cfgpin(S3C2410_GPG13, S3C2410_GPG13_nXPON);
132         s3c2410_gpio_cfgpin(S3C2410_GPG14, S3C2410_GPG14_YMON);
133         s3c2410_gpio_cfgpin(S3C2410_GPG15, S3C2410_GPG15_nYPON);
134 }
135
136 static void s3c2410_ts_start_adc_conversion(void)
137 {
138         writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST,
139                base_addr + S3C2410_ADCTSC);
140         writel(readl(base_addr + S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START,
141                base_addr + S3C2410_ADCCON);
142 }
143
144 /*
145  * Just send the input events.
146  */
147
148 enum ts_input_event {IE_DOWN = 0, IE_UP};
149
150 static void ts_input_report(int event, int coords[])
151 {
152 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
153         static char *s[] = {"down", "up"};
154         struct timeval tv;
155
156         do_gettimeofday(&tv);
157 #endif
158
159         if (event == IE_DOWN) {
160                 input_report_abs(ts.dev, ABS_X, coords[0]);
161                 input_report_abs(ts.dev, ABS_Y, coords[1]);
162                 input_report_key(ts.dev, BTN_TOUCH, 1);
163                 input_report_abs(ts.dev, ABS_PRESSURE, 1);
164
165 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
166                 printk(DEBUG_LVL "T:%06d %6s (X:%03d, Y:%03d)\n",
167                        (int)tv.tv_usec, s[event], coords[0], coords[1]);
168 #endif
169         } else {
170                 input_report_key(ts.dev, BTN_TOUCH, 0);
171                 input_report_abs(ts.dev, ABS_PRESSURE, 0);
172
173 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
174                 printk(DEBUG_LVL "T:%06d %6s\n",
175                        (int)tv.tv_usec, s[event]);
176 #endif
177         }
178
179         input_sync(ts.dev);
180 }
181
182 /*
183  * Manage the state of the touchscreen.
184  */
185
186 static void event_send_timer_f(unsigned long data);
187
188 static struct timer_list event_send_timer =
189                 TIMER_INITIALIZER(event_send_timer_f, 0, 0);
190
191 static void event_send_timer_f(unsigned long data)
192 {
193         static unsigned long running;
194         static int noop_counter;
195         int event_type;
196
197         if (unlikely(test_and_set_bit(0, &running))) {
198                 mod_timer(&event_send_timer,
199                           jiffies + TS_RELEASE_TIMEOUT);
200                 return;
201         }
202
203         while (__kfifo_get(ts.event_fifo, (unsigned char *)&event_type,
204                            sizeof(int))) {
205                 int buf[2];
206
207                 switch (event_type) {
208                 case 'D':
209                         if (ts.state == TS_STATE_RELEASE_PENDING)
210                                 /* Ignore short UP event */
211                                 ts.state = TS_STATE_PRESSED;
212                         break;
213
214                 case 'U':
215                         ts.state = TS_STATE_RELEASE_PENDING;
216                         break;
217
218                 case 'P':
219                         if (ts.is_down) /* stylus_action needs a conversion */
220                                 s3c2410_ts_start_adc_conversion();
221
222                         if (unlikely(__kfifo_get(ts.event_fifo,
223                                                  (unsigned char *)buf,
224                                                  sizeof(int) * 2)
225                                      != sizeof(int) * 2))
226                                 goto ts_exit_error;
227
228                         ts_input_report(IE_DOWN, buf);
229                         ts.state = TS_STATE_PRESSED;
230                         break;
231
232                 default:
233                         goto ts_exit_error;
234                 }
235
236                 noop_counter = 0;
237         }
238
239         if (noop_counter++ >= 1) {
240                 noop_counter = 0;
241                 if (ts.state == TS_STATE_RELEASE_PENDING) {
242                         /* We delay the UP event for a
243                          * while to avoid jitter. If we get a DOWN
244                          * event we do not send it. */
245
246                         ts_input_report(IE_UP, NULL);
247                         ts.state = TS_STATE_STANDBY;
248
249                         if (ts.tsf[0])
250                                 (ts.tsf[0]->api->clear)(ts.tsf[0]);
251                 }
252         } else {
253                 mod_timer(&event_send_timer, jiffies + TS_RELEASE_TIMEOUT);
254         }
255
256         clear_bit(0, &running);
257
258         return;
259
260 ts_exit_error: /* should not happen unless we have a bug */
261         printk(KERN_ERR __FILE__ ": event_send_timer_f failed\n");
262 }
263
264 /*
265  * Manage interrupts.
266  */
267
268 static irqreturn_t stylus_updown(int irq, void *dev_id)
269 {
270         unsigned long data0;
271         unsigned long data1;
272         int event_type;
273
274         data0 = readl(base_addr+S3C2410_ADCDAT0);
275         data1 = readl(base_addr+S3C2410_ADCDAT1);
276
277         ts.is_down = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) &&
278                                             (!(data1 & S3C2410_ADCDAT0_UPDOWN));
279
280         event_type = ts.is_down ? 'D' : 'U';
281
282         if (unlikely(__kfifo_put(ts.event_fifo, (unsigned char *)&event_type,
283                      sizeof(int)) != sizeof(int))) /* should not happen */
284                 printk(KERN_ERR __FILE__": stylus_updown lost event!\n");
285
286         if (ts.is_down)
287                 s3c2410_ts_start_adc_conversion();
288         else
289                 writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
290
291         mod_timer(&event_send_timer, jiffies + 1);
292
293         return IRQ_HANDLED;
294 }
295
296 static irqreturn_t stylus_action(int irq, void *dev_id)
297 {
298         int buf[3];
299
300         /* grab the ADC results */
301         ts.coords[0] = readl(base_addr + S3C2410_ADCDAT0) &
302                                                     S3C2410_ADCDAT0_XPDATA_MASK;
303         ts.coords[1] = readl(base_addr + S3C2410_ADCDAT1) &
304                                                     S3C2410_ADCDAT1_YPDATA_MASK;
305
306         if (ts.tsf[0]) { /* filtering is enabled, don't use raw directly */
307                 switch ((ts.tsf[0]->api->process)(ts.tsf[0], &ts.coords[0])) {
308                 case 0: /*
309                          * no real sample came out of processing yet,
310                          * get another raw result to feed it
311                          */
312                         s3c2410_ts_start_adc_conversion();
313                         return IRQ_HANDLED;
314                 case 1: /* filters are ready to deliver a sample */
315                         (ts.tsf[0]->api->scale)(ts.tsf[0], &ts.coords[0]);
316                         break;
317                 case -1:
318                         /* error in filters, ignore the event */
319                         (ts.tsf[0]->api->clear)(ts.tsf[0]);
320                         writel(WAIT4INT(1), base_addr + S3C2410_ADCTSC);
321                         return IRQ_HANDLED;
322                 default:
323                         printk(KERN_ERR":stylus_action error\n");
324                 }
325         }
326
327         /* We use a buffer because want an atomic operation */
328         buf[0] = 'P';
329         buf[1] = ts.coords[0];
330         buf[2] = ts.coords[1];
331
332         if (unlikely(__kfifo_put(ts.event_fifo, (unsigned char *)buf,
333                      sizeof(int) * 3) != sizeof(int) * 3))
334                 /* should not happen */
335                         printk(KERN_ERR":stylus_action error\n");
336
337         writel(WAIT4INT(1), base_addr + S3C2410_ADCTSC);
338         mod_timer(&event_send_timer, jiffies + 1);
339
340         return IRQ_HANDLED;
341 }
342
343 static struct clk       *adc_clock;
344
345 /*
346  * The functions for inserting/removing us as a module.
347  */
348
349 static int __init s3c2410ts_probe(struct platform_device *pdev)
350 {
351         int rc;
352         struct s3c2410_ts_mach_info *info;
353         struct input_dev *input_dev;
354         int ret = 0;
355
356         dev_info(&pdev->dev, "Starting\n");
357
358         info = (struct s3c2410_ts_mach_info *)pdev->dev.platform_data;
359
360         if (!info)
361         {
362                 dev_err(&pdev->dev, "Hm... too bad: no platform data for ts\n");
363                 return -EINVAL;
364         }
365
366 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
367         printk(DEBUG_LVL "Entering s3c2410ts_init\n");
368 #endif
369
370         adc_clock = clk_get(NULL, "adc");
371         if (!adc_clock) {
372                 dev_err(&pdev->dev, "failed to get adc clock source\n");
373                 return -ENOENT;
374         }
375         clk_enable(adc_clock);
376
377 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
378         printk(DEBUG_LVL "got and enabled clock\n");
379 #endif
380
381         base_addr = ioremap(S3C2410_PA_ADC,0x20);
382         if (base_addr == NULL) {
383                 dev_err(&pdev->dev, "Failed to remap register block\n");
384                 ret = -ENOMEM;
385                 goto bail0;
386         }
387
388
389         /* If we acutally are a S3C2410: Configure GPIOs */
390         if (!strcmp(pdev->name, "s3c2410-ts"))
391                 s3c2410_ts_connect();
392
393         if ((info->presc & 0xff) > 0)
394                 writel(S3C2410_ADCCON_PRSCEN |
395                        S3C2410_ADCCON_PRSCVL(info->presc&0xFF),
396                                                     base_addr + S3C2410_ADCCON);
397         else
398                 writel(0, base_addr+S3C2410_ADCCON);
399
400         /* Initialise registers */
401         if ((info->delay & 0xffff) > 0)
402                 writel(info->delay & 0xffff,  base_addr + S3C2410_ADCDLY);
403
404         writel(WAIT4INT(0), base_addr + S3C2410_ADCTSC);
405
406         /* Initialise input stuff */
407         memset(&ts, 0, sizeof(struct s3c2410ts));
408         input_dev = input_allocate_device();
409
410         if (!input_dev) {
411                 dev_err(&pdev->dev, "Unable to allocate the input device\n");
412                 ret = -ENOMEM;
413                 goto bail1;
414         }
415
416         ts.dev = input_dev;
417         ts.dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) |
418                            BIT_MASK(EV_ABS);
419         ts.dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
420         input_set_abs_params(ts.dev, ABS_X, 0, 0x3FF, 0, 0);
421         input_set_abs_params(ts.dev, ABS_Y, 0, 0x3FF, 0, 0);
422         input_set_abs_params(ts.dev, ABS_PRESSURE, 0, 1, 0, 0);
423
424         ts.dev->name = s3c2410ts_name;
425         ts.dev->id.bustype = BUS_RS232;
426         ts.dev->id.vendor = 0xDEAD;
427         ts.dev->id.product = 0xBEEF;
428         ts.dev->id.version = S3C2410TSVERSION;
429         ts.state = TS_STATE_STANDBY;
430         ts.event_fifo = kfifo_alloc(TS_EVENT_FIFO_SIZE, GFP_KERNEL, NULL);
431         if (IS_ERR(ts.event_fifo)) {
432                 ret = -EIO;
433                 goto bail2;
434         }
435
436         /* create the filter chain set up for the 2 coordinates we produce */
437         ret = ts_filter_create_chain(
438                 (struct ts_filter_api **)&info->filter_sequence,
439                 (void *)&info->filter_config, ts.tsf, ARRAY_SIZE(ts.coords));
440         if (ret)
441                 dev_info(&pdev->dev, "%d filter(s) initialized\n", ret);
442         else /* this is OK, just means there won't be any filtering */
443                 dev_info(&pdev->dev, "Unfiltered output selected\n");
444
445         if (ts.tsf[0])
446                 (ts.tsf[0]->api->clear)(ts.tsf[0]);
447         else
448                 dev_info(&pdev->dev, "No filtering\n");
449
450         /* Get irqs */
451         if (request_irq(IRQ_ADC, stylus_action, IRQF_SAMPLE_RANDOM,
452                                                     "s3c2410_action", ts.dev)) {
453                 dev_err(&pdev->dev, "Could not allocate ts IRQ_ADC !\n");
454                 iounmap(base_addr);
455                 ret = -EIO;
456                 goto bail3;
457         }
458         if (request_irq(IRQ_TC, stylus_updown, IRQF_SAMPLE_RANDOM,
459                         "s3c2410_action", ts.dev)) {
460                 dev_err(&pdev->dev, "Could not allocate ts IRQ_TC !\n");
461                 free_irq(IRQ_ADC, ts.dev);
462                 iounmap(base_addr);
463                 ret = -EIO;
464                 goto bail4;
465         }
466
467         dev_info(&pdev->dev, "successfully loaded\n");
468
469         /* All went ok, so register to the input system */
470         rc = input_register_device(ts.dev);
471         if (rc) {
472                 ret = -EIO;
473                 goto bail5;
474         }
475
476         return 0;
477
478 bail5:
479         free_irq(IRQ_TC, ts.dev);
480         free_irq(IRQ_ADC, ts.dev);
481         clk_disable(adc_clock);
482         iounmap(base_addr);
483         disable_irq(IRQ_TC);
484 bail4:
485         disable_irq(IRQ_ADC);
486 bail3:
487         ts_filter_destroy_chain(ts.tsf);
488         kfifo_free(ts.event_fifo);
489 bail2:
490         input_unregister_device(ts.dev);
491 bail1:
492         iounmap(base_addr);
493 bail0:
494
495         return ret;
496 }
497
498 static int s3c2410ts_remove(struct platform_device *pdev)
499 {
500         disable_irq(IRQ_ADC);
501         disable_irq(IRQ_TC);
502         free_irq(IRQ_TC,ts.dev);
503         free_irq(IRQ_ADC,ts.dev);
504
505         if (adc_clock) {
506                 clk_disable(adc_clock);
507                 clk_put(adc_clock);
508                 adc_clock = NULL;
509         }
510
511         input_unregister_device(ts.dev);
512         iounmap(base_addr);
513
514         ts_filter_destroy_chain(ts.tsf);
515
516         kfifo_free(ts.event_fifo);
517
518         return 0;
519 }
520
521 #ifdef CONFIG_PM
522 static int s3c2410ts_suspend(struct platform_device *pdev, pm_message_t state)
523 {
524         writel(TSC_SLEEP, base_addr+S3C2410_ADCTSC);
525         writel(readl(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_STDBM,
526                base_addr+S3C2410_ADCCON);
527
528         disable_irq(IRQ_ADC);
529         disable_irq(IRQ_TC);
530
531         clk_disable(adc_clock);
532
533         return 0;
534 }
535
536 static int s3c2410ts_resume(struct platform_device *pdev)
537 {
538         struct s3c2410_ts_mach_info *info =
539                 ( struct s3c2410_ts_mach_info *)pdev->dev.platform_data;
540
541         clk_enable(adc_clock);
542         mdelay(1);
543
544         if (ts.tsf[0])
545                 (ts.tsf[0]->api->clear)(ts.tsf[0]);
546
547         enable_irq(IRQ_ADC);
548         enable_irq(IRQ_TC);
549
550         if ((info->presc&0xff) > 0)
551                 writel(S3C2410_ADCCON_PRSCEN |
552                        S3C2410_ADCCON_PRSCVL(info->presc&0xFF),
553                                                       base_addr+S3C2410_ADCCON);
554         else
555                 writel(0,base_addr+S3C2410_ADCCON);
556
557         /* Initialise registers */
558         if ((info->delay & 0xffff) > 0)
559                 writel(info->delay & 0xffff,  base_addr+S3C2410_ADCDLY);
560
561         writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
562
563         return 0;
564 }
565
566 #else
567 #define s3c2410ts_suspend NULL
568 #define s3c2410ts_resume  NULL
569 #endif
570
571 static struct platform_driver s3c2410ts_driver = {
572        .driver         = {
573                .name   = "s3c2410-ts",
574                .owner  = THIS_MODULE,
575        },
576        .probe          = s3c2410ts_probe,
577        .remove         = s3c2410ts_remove,
578        .suspend        = s3c2410ts_suspend,
579        .resume         = s3c2410ts_resume,
580
581 };
582
583 static struct platform_driver s3c2440ts_driver = {
584        .driver         = {
585                .name   = "s3c2440-ts",
586                .owner  = THIS_MODULE,
587        },
588        .probe          = s3c2410ts_probe,
589        .remove         = s3c2410ts_remove,
590        .suspend        = s3c2410ts_suspend,
591        .resume         = s3c2410ts_resume,
592
593 };
594
595 static int __init s3c2410ts_init(void)
596 {
597         int rc;
598
599         rc = platform_driver_register(&s3c2410ts_driver);
600         if (rc < 0)
601                 return rc;
602
603         rc = platform_driver_register(&s3c2440ts_driver);
604         if (rc < 0)
605                 platform_driver_unregister(&s3c2410ts_driver);
606
607         return rc;
608 }
609
610 static void __exit s3c2410ts_exit(void)
611 {
612         platform_driver_unregister(&s3c2440ts_driver);
613         platform_driver_unregister(&s3c2410ts_driver);
614 }
615
616 module_init(s3c2410ts_init);
617 module_exit(s3c2410ts_exit);
618