Merge git://git.kernel.org/pub/scm/linux/kernel/git/joern/logfs
[pandora-kernel.git] / drivers / input / touchscreen / s3c2410_ts.c
1 /*
2  * Samsung S3C24XX touchscreen driver
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the term of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Copyright 2004 Arnaud Patard <arnaud.patard@rtp-net.org>
19  * Copyright 2008 Ben Dooks <ben-linux@fluff.org>
20  * Copyright 2009 Simtec Electronics <linux@simtec.co.uk>
21  *
22  * Additional work by Herbert Pƶtzl <herbert@13thfloor.at> and
23  * Harald Welte <laforge@openmoko.org>
24  */
25
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/gpio.h>
30 #include <linux/input.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/interrupt.h>
34 #include <linux/platform_device.h>
35 #include <linux/clk.h>
36 #include <linux/io.h>
37
38 #include <plat/adc.h>
39 #include <plat/regs-adc.h>
40
41 #include <mach/regs-gpio.h>
42 #include <mach/ts.h>
43
44 #define TSC_SLEEP  (S3C2410_ADCTSC_PULL_UP_DISABLE | S3C2410_ADCTSC_XY_PST(0))
45
46 #define INT_DOWN        (0)
47 #define INT_UP          (1 << 8)
48
49 #define WAIT4INT        (S3C2410_ADCTSC_YM_SEN | \
50                          S3C2410_ADCTSC_YP_SEN | \
51                          S3C2410_ADCTSC_XP_SEN | \
52                          S3C2410_ADCTSC_XY_PST(3))
53
54 #define AUTOPST         (S3C2410_ADCTSC_YM_SEN | \
55                          S3C2410_ADCTSC_YP_SEN | \
56                          S3C2410_ADCTSC_XP_SEN | \
57                          S3C2410_ADCTSC_AUTO_PST | \
58                          S3C2410_ADCTSC_XY_PST(0))
59
60 /* Per-touchscreen data. */
61
62 /**
63  * struct s3c2410ts - driver touchscreen state.
64  * @client: The ADC client we registered with the core driver.
65  * @dev: The device we are bound to.
66  * @input: The input device we registered with the input subsystem.
67  * @clock: The clock for the adc.
68  * @io: Pointer to the IO base.
69  * @xp: The accumulated X position data.
70  * @yp: The accumulated Y position data.
71  * @irq_tc: The interrupt number for pen up/down interrupt
72  * @count: The number of samples collected.
73  * @shift: The log2 of the maximum count to read in one go.
74  */
75 struct s3c2410ts {
76         struct s3c_adc_client *client;
77         struct device *dev;
78         struct input_dev *input;
79         struct clk *clock;
80         void __iomem *io;
81         unsigned long xp;
82         unsigned long yp;
83         int irq_tc;
84         int count;
85         int shift;
86 };
87
88 static struct s3c2410ts ts;
89
90 /**
91  * s3c2410_ts_connect - configure gpio for s3c2410 systems
92  *
93  * Configure the GPIO for the S3C2410 system, where we have external FETs
94  * connected to the device (later systems such as the S3C2440 integrate
95  * these into the device).
96 */
97 static inline void s3c2410_ts_connect(void)
98 {
99         s3c2410_gpio_cfgpin(S3C2410_GPG(12), S3C2410_GPG12_XMON);
100         s3c2410_gpio_cfgpin(S3C2410_GPG(13), S3C2410_GPG13_nXPON);
101         s3c2410_gpio_cfgpin(S3C2410_GPG(14), S3C2410_GPG14_YMON);
102         s3c2410_gpio_cfgpin(S3C2410_GPG(15), S3C2410_GPG15_nYPON);
103 }
104
105 /**
106  * get_down - return the down state of the pen
107  * @data0: The data read from ADCDAT0 register.
108  * @data1: The data read from ADCDAT1 register.
109  *
110  * Return non-zero if both readings show that the pen is down.
111  */
112 static inline bool get_down(unsigned long data0, unsigned long data1)
113 {
114         /* returns true if both data values show stylus down */
115         return (!(data0 & S3C2410_ADCDAT0_UPDOWN) &&
116                 !(data1 & S3C2410_ADCDAT0_UPDOWN));
117 }
118
119 static void touch_timer_fire(unsigned long data)
120 {
121         unsigned long data0;
122         unsigned long data1;
123         bool down;
124
125         data0 = readl(ts.io + S3C2410_ADCDAT0);
126         data1 = readl(ts.io + S3C2410_ADCDAT1);
127
128         down = get_down(data0, data1);
129
130         if (down) {
131                 if (ts.count == (1 << ts.shift)) {
132                         ts.xp >>= ts.shift;
133                         ts.yp >>= ts.shift;
134
135                         dev_dbg(ts.dev, "%s: X=%lu, Y=%lu, count=%d\n",
136                                 __func__, ts.xp, ts.yp, ts.count);
137
138                         input_report_abs(ts.input, ABS_X, ts.xp);
139                         input_report_abs(ts.input, ABS_Y, ts.yp);
140
141                         input_report_key(ts.input, BTN_TOUCH, 1);
142                         input_sync(ts.input);
143
144                         ts.xp = 0;
145                         ts.yp = 0;
146                         ts.count = 0;
147                 }
148
149                 s3c_adc_start(ts.client, 0, 1 << ts.shift);
150         } else {
151                 ts.xp = 0;
152                 ts.yp = 0;
153                 ts.count = 0;
154
155                 input_report_key(ts.input, BTN_TOUCH, 0);
156                 input_sync(ts.input);
157
158                 writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
159         }
160 }
161
162 static DEFINE_TIMER(touch_timer, touch_timer_fire, 0, 0);
163
164 /**
165  * stylus_irq - touchscreen stylus event interrupt
166  * @irq: The interrupt number
167  * @dev_id: The device ID.
168  *
169  * Called when the IRQ_TC is fired for a pen up or down event.
170  */
171 static irqreturn_t stylus_irq(int irq, void *dev_id)
172 {
173         unsigned long data0;
174         unsigned long data1;
175         bool down;
176
177         data0 = readl(ts.io + S3C2410_ADCDAT0);
178         data1 = readl(ts.io + S3C2410_ADCDAT1);
179
180         down = get_down(data0, data1);
181
182         /* TODO we should never get an interrupt with down set while
183          * the timer is running, but maybe we ought to verify that the
184          * timer isn't running anyways. */
185
186         if (down)
187                 s3c_adc_start(ts.client, 0, 1 << ts.shift);
188         else
189                 dev_info(ts.dev, "%s: count=%d\n", __func__, ts.count);
190
191         return IRQ_HANDLED;
192 }
193
194 /**
195  * s3c24xx_ts_conversion - ADC conversion callback
196  * @client: The client that was registered with the ADC core.
197  * @data0: The reading from ADCDAT0.
198  * @data1: The reading from ADCDAT1.
199  * @left: The number of samples left.
200  *
201  * Called when a conversion has finished.
202  */
203 static void s3c24xx_ts_conversion(struct s3c_adc_client *client,
204                                   unsigned data0, unsigned data1,
205                                   unsigned *left)
206 {
207         dev_dbg(ts.dev, "%s: %d,%d\n", __func__, data0, data1);
208
209         ts.xp += data0;
210         ts.yp += data1;
211
212         ts.count++;
213
214         /* From tests, it seems that it is unlikely to get a pen-up
215          * event during the conversion process which means we can
216          * ignore any pen-up events with less than the requisite
217          * count done.
218          *
219          * In several thousand conversions, no pen-ups where detected
220          * before count completed.
221          */
222 }
223
224 /**
225  * s3c24xx_ts_select - ADC selection callback.
226  * @client: The client that was registered with the ADC core.
227  * @select: The reason for select.
228  *
229  * Called when the ADC core selects (or deslects) us as a client.
230  */
231 static void s3c24xx_ts_select(struct s3c_adc_client *client, unsigned select)
232 {
233         if (select) {
234                 writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST,
235                        ts.io + S3C2410_ADCTSC);
236         } else {
237                 mod_timer(&touch_timer, jiffies+1);
238                 writel(WAIT4INT | INT_UP, ts.io + S3C2410_ADCTSC);
239         }
240 }
241
242 /**
243  * s3c2410ts_probe - device core probe entry point
244  * @pdev: The device we are being bound to.
245  *
246  * Initialise, find and allocate any resources we need to run and then
247  * register with the ADC and input systems.
248  */
249 static int __devinit s3c2410ts_probe(struct platform_device *pdev)
250 {
251         struct s3c2410_ts_mach_info *info;
252         struct device *dev = &pdev->dev;
253         struct input_dev *input_dev;
254         struct resource *res;
255         int ret = -EINVAL;
256
257         /* Initialise input stuff */
258         memset(&ts, 0, sizeof(struct s3c2410ts));
259
260         ts.dev = dev;
261
262         info = pdev->dev.platform_data;
263         if (!info) {
264                 dev_err(dev, "no platform data, cannot attach\n");
265                 return -EINVAL;
266         }
267
268         dev_dbg(dev, "initialising touchscreen\n");
269
270         ts.clock = clk_get(dev, "adc");
271         if (IS_ERR(ts.clock)) {
272                 dev_err(dev, "cannot get adc clock source\n");
273                 return -ENOENT;
274         }
275
276         clk_enable(ts.clock);
277         dev_dbg(dev, "got and enabled clocks\n");
278
279         ts.irq_tc = ret = platform_get_irq(pdev, 0);
280         if (ret < 0) {
281                 dev_err(dev, "no resource for interrupt\n");
282                 goto err_clk;
283         }
284
285         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
286         if (!res) {
287                 dev_err(dev, "no resource for registers\n");
288                 ret = -ENOENT;
289                 goto err_clk;
290         }
291
292         ts.io = ioremap(res->start, resource_size(res));
293         if (ts.io == NULL) {
294                 dev_err(dev, "cannot map registers\n");
295                 ret = -ENOMEM;
296                 goto err_clk;
297         }
298
299         /* Configure the touchscreen external FETs on the S3C2410 */
300         if (!platform_get_device_id(pdev)->driver_data)
301                 s3c2410_ts_connect();
302
303         ts.client = s3c_adc_register(pdev, s3c24xx_ts_select,
304                                      s3c24xx_ts_conversion, 1);
305         if (IS_ERR(ts.client)) {
306                 dev_err(dev, "failed to register adc client\n");
307                 ret = PTR_ERR(ts.client);
308                 goto err_iomap;
309         }
310
311         /* Initialise registers */
312         if ((info->delay & 0xffff) > 0)
313                 writel(info->delay & 0xffff, ts.io + S3C2410_ADCDLY);
314
315         writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
316
317         input_dev = input_allocate_device();
318         if (!input_dev) {
319                 dev_err(dev, "Unable to allocate the input device !!\n");
320                 ret = -ENOMEM;
321                 goto err_iomap;
322         }
323
324         ts.input = input_dev;
325         ts.input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
326         ts.input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
327         input_set_abs_params(ts.input, ABS_X, 0, 0x3FF, 0, 0);
328         input_set_abs_params(ts.input, ABS_Y, 0, 0x3FF, 0, 0);
329
330         ts.input->name = "S3C24XX TouchScreen";
331         ts.input->id.bustype = BUS_HOST;
332         ts.input->id.vendor = 0xDEAD;
333         ts.input->id.product = 0xBEEF;
334         ts.input->id.version = 0x0102;
335
336         ts.shift = info->oversampling_shift;
337
338         ret = request_irq(ts.irq_tc, stylus_irq, IRQF_DISABLED,
339                           "s3c2410_ts_pen", ts.input);
340         if (ret) {
341                 dev_err(dev, "cannot get TC interrupt\n");
342                 goto err_inputdev;
343         }
344
345         dev_info(dev, "driver attached, registering input device\n");
346
347         /* All went ok, so register to the input system */
348         ret = input_register_device(ts.input);
349         if (ret < 0) {
350                 dev_err(dev, "failed to register input device\n");
351                 ret = -EIO;
352                 goto err_tcirq;
353         }
354
355         return 0;
356
357  err_tcirq:
358         free_irq(ts.irq_tc, ts.input);
359  err_inputdev:
360         input_unregister_device(ts.input);
361  err_iomap:
362         iounmap(ts.io);
363  err_clk:
364         del_timer_sync(&touch_timer);
365         clk_put(ts.clock);
366         return ret;
367 }
368
369 /**
370  * s3c2410ts_remove - device core removal entry point
371  * @pdev: The device we are being removed from.
372  *
373  * Free up our state ready to be removed.
374  */
375 static int __devexit s3c2410ts_remove(struct platform_device *pdev)
376 {
377         free_irq(ts.irq_tc, ts.input);
378         del_timer_sync(&touch_timer);
379
380         clk_disable(ts.clock);
381         clk_put(ts.clock);
382
383         input_unregister_device(ts.input);
384         iounmap(ts.io);
385
386         return 0;
387 }
388
389 #ifdef CONFIG_PM
390 static int s3c2410ts_suspend(struct device *dev)
391 {
392         writel(TSC_SLEEP, ts.io + S3C2410_ADCTSC);
393         disable_irq(ts.irq_tc);
394         clk_disable(ts.clock);
395
396         return 0;
397 }
398
399 static int s3c2410ts_resume(struct device *dev)
400 {
401         struct platform_device *pdev = to_platform_device(dev);
402         struct s3c2410_ts_mach_info *info = pdev->dev.platform_data;
403
404         clk_enable(ts.clock);
405         enable_irq(ts.irq_tc);
406
407         /* Initialise registers */
408         if ((info->delay & 0xffff) > 0)
409                 writel(info->delay & 0xffff, ts.io + S3C2410_ADCDLY);
410
411         writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
412
413         return 0;
414 }
415
416 static struct dev_pm_ops s3c_ts_pmops = {
417         .suspend        = s3c2410ts_suspend,
418         .resume         = s3c2410ts_resume,
419 };
420 #endif
421
422 static struct platform_device_id s3cts_driver_ids[] = {
423         { "s3c2410-ts", 0 },
424         { "s3c2440-ts", 1 },
425         { }
426 };
427 MODULE_DEVICE_TABLE(platform, s3cts_driver_ids);
428
429 static struct platform_driver s3c_ts_driver = {
430         .driver         = {
431                 .name   = "s3c24xx-ts",
432                 .owner  = THIS_MODULE,
433 #ifdef CONFIG_PM
434                 .pm     = &s3c_ts_pmops,
435 #endif
436         },
437         .id_table       = s3cts_driver_ids,
438         .probe          = s3c2410ts_probe,
439         .remove         = __devexit_p(s3c2410ts_remove),
440 };
441
442 static int __init s3c2410ts_init(void)
443 {
444         return platform_driver_register(&s3c_ts_driver);
445 }
446
447 static void __exit s3c2410ts_exit(void)
448 {
449         platform_driver_unregister(&s3c_ts_driver);
450 }
451
452 module_init(s3c2410ts_init);
453 module_exit(s3c2410ts_exit);
454
455 MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, "
456               "Ben Dooks <ben@simtec.co.uk>, "
457               "Simtec Electronics <linux@simtec.co.uk>");
458 MODULE_DESCRIPTION("S3C24XX Touchscreen driver");
459 MODULE_LICENSE("GPL v2");