blob: 86086a42d5e3dd6024de2654feb0786b35fa0cea [file] [log] [blame]
Arnaud Patardf5f96b92009-11-23 09:47:12 -08001/*
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>
Arnaud Patardf5f96b92009-11-23 09:47:12 -080029#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>
Maurus Cuelenaere73e59b12010-05-04 13:12:41 +020040#include <plat/ts.h>
Arnaud Patardf5f96b92009-11-23 09:47:12 -080041
Arnaud Patardf5f96b92009-11-23 09:47:12 -080042#define TSC_SLEEP (S3C2410_ADCTSC_PULL_UP_DISABLE | S3C2410_ADCTSC_XY_PST(0))
43
44#define INT_DOWN (0)
45#define INT_UP (1 << 8)
46
47#define WAIT4INT (S3C2410_ADCTSC_YM_SEN | \
48 S3C2410_ADCTSC_YP_SEN | \
49 S3C2410_ADCTSC_XP_SEN | \
50 S3C2410_ADCTSC_XY_PST(3))
51
52#define AUTOPST (S3C2410_ADCTSC_YM_SEN | \
53 S3C2410_ADCTSC_YP_SEN | \
54 S3C2410_ADCTSC_XP_SEN | \
55 S3C2410_ADCTSC_AUTO_PST | \
56 S3C2410_ADCTSC_XY_PST(0))
57
58/* Per-touchscreen data. */
59
60/**
61 * struct s3c2410ts - driver touchscreen state.
62 * @client: The ADC client we registered with the core driver.
63 * @dev: The device we are bound to.
64 * @input: The input device we registered with the input subsystem.
65 * @clock: The clock for the adc.
66 * @io: Pointer to the IO base.
67 * @xp: The accumulated X position data.
68 * @yp: The accumulated Y position data.
69 * @irq_tc: The interrupt number for pen up/down interrupt
70 * @count: The number of samples collected.
71 * @shift: The log2 of the maximum count to read in one go.
72 */
73struct s3c2410ts {
74 struct s3c_adc_client *client;
75 struct device *dev;
76 struct input_dev *input;
77 struct clk *clock;
78 void __iomem *io;
79 unsigned long xp;
80 unsigned long yp;
81 int irq_tc;
82 int count;
83 int shift;
84};
85
86static struct s3c2410ts ts;
87
88/**
Arnaud Patardf5f96b92009-11-23 09:47:12 -080089 * get_down - return the down state of the pen
90 * @data0: The data read from ADCDAT0 register.
91 * @data1: The data read from ADCDAT1 register.
92 *
93 * Return non-zero if both readings show that the pen is down.
94 */
95static inline bool get_down(unsigned long data0, unsigned long data1)
96{
97 /* returns true if both data values show stylus down */
98 return (!(data0 & S3C2410_ADCDAT0_UPDOWN) &&
99 !(data1 & S3C2410_ADCDAT0_UPDOWN));
100}
101
102static void touch_timer_fire(unsigned long data)
103{
104 unsigned long data0;
105 unsigned long data1;
106 bool down;
107
108 data0 = readl(ts.io + S3C2410_ADCDAT0);
109 data1 = readl(ts.io + S3C2410_ADCDAT1);
110
111 down = get_down(data0, data1);
112
Arnaud Patardf5f96b92009-11-23 09:47:12 -0800113 if (down) {
Vasily Khoruzhick23c239b2010-02-20 01:06:20 -0800114 if (ts.count == (1 << ts.shift)) {
115 ts.xp >>= ts.shift;
116 ts.yp >>= ts.shift;
117
118 dev_dbg(ts.dev, "%s: X=%lu, Y=%lu, count=%d\n",
119 __func__, ts.xp, ts.yp, ts.count);
120
121 input_report_abs(ts.input, ABS_X, ts.xp);
122 input_report_abs(ts.input, ABS_Y, ts.yp);
123
124 input_report_key(ts.input, BTN_TOUCH, 1);
125 input_sync(ts.input);
126
127 ts.xp = 0;
128 ts.yp = 0;
129 ts.count = 0;
130 }
131
Arnaud Patardf5f96b92009-11-23 09:47:12 -0800132 s3c_adc_start(ts.client, 0, 1 << ts.shift);
133 } else {
Vasily Khoruzhick23c239b2010-02-20 01:06:20 -0800134 ts.xp = 0;
135 ts.yp = 0;
Arnaud Patardf5f96b92009-11-23 09:47:12 -0800136 ts.count = 0;
137
138 input_report_key(ts.input, BTN_TOUCH, 0);
139 input_sync(ts.input);
140
141 writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
142 }
143}
144
145static DEFINE_TIMER(touch_timer, touch_timer_fire, 0, 0);
146
147/**
148 * stylus_irq - touchscreen stylus event interrupt
149 * @irq: The interrupt number
150 * @dev_id: The device ID.
151 *
152 * Called when the IRQ_TC is fired for a pen up or down event.
153 */
154static irqreturn_t stylus_irq(int irq, void *dev_id)
155{
156 unsigned long data0;
157 unsigned long data1;
158 bool down;
159
160 data0 = readl(ts.io + S3C2410_ADCDAT0);
161 data1 = readl(ts.io + S3C2410_ADCDAT1);
162
163 down = get_down(data0, data1);
164
165 /* TODO we should never get an interrupt with down set while
166 * the timer is running, but maybe we ought to verify that the
167 * timer isn't running anyways. */
168
169 if (down)
170 s3c_adc_start(ts.client, 0, 1 << ts.shift);
171 else
172 dev_info(ts.dev, "%s: count=%d\n", __func__, ts.count);
173
174 return IRQ_HANDLED;
175}
176
177/**
178 * s3c24xx_ts_conversion - ADC conversion callback
179 * @client: The client that was registered with the ADC core.
180 * @data0: The reading from ADCDAT0.
181 * @data1: The reading from ADCDAT1.
182 * @left: The number of samples left.
183 *
184 * Called when a conversion has finished.
185 */
186static void s3c24xx_ts_conversion(struct s3c_adc_client *client,
187 unsigned data0, unsigned data1,
188 unsigned *left)
189{
190 dev_dbg(ts.dev, "%s: %d,%d\n", __func__, data0, data1);
191
192 ts.xp += data0;
193 ts.yp += data1;
194
195 ts.count++;
196
197 /* From tests, it seems that it is unlikely to get a pen-up
198 * event during the conversion process which means we can
199 * ignore any pen-up events with less than the requisite
200 * count done.
201 *
202 * In several thousand conversions, no pen-ups where detected
203 * before count completed.
204 */
205}
206
207/**
208 * s3c24xx_ts_select - ADC selection callback.
209 * @client: The client that was registered with the ADC core.
210 * @select: The reason for select.
211 *
212 * Called when the ADC core selects (or deslects) us as a client.
213 */
214static void s3c24xx_ts_select(struct s3c_adc_client *client, unsigned select)
215{
216 if (select) {
217 writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST,
218 ts.io + S3C2410_ADCTSC);
219 } else {
220 mod_timer(&touch_timer, jiffies+1);
221 writel(WAIT4INT | INT_UP, ts.io + S3C2410_ADCTSC);
222 }
223}
224
225/**
226 * s3c2410ts_probe - device core probe entry point
227 * @pdev: The device we are being bound to.
228 *
229 * Initialise, find and allocate any resources we need to run and then
230 * register with the ADC and input systems.
231 */
232static int __devinit s3c2410ts_probe(struct platform_device *pdev)
233{
234 struct s3c2410_ts_mach_info *info;
235 struct device *dev = &pdev->dev;
236 struct input_dev *input_dev;
237 struct resource *res;
238 int ret = -EINVAL;
239
240 /* Initialise input stuff */
241 memset(&ts, 0, sizeof(struct s3c2410ts));
242
243 ts.dev = dev;
244
245 info = pdev->dev.platform_data;
246 if (!info) {
247 dev_err(dev, "no platform data, cannot attach\n");
248 return -EINVAL;
249 }
250
251 dev_dbg(dev, "initialising touchscreen\n");
252
253 ts.clock = clk_get(dev, "adc");
254 if (IS_ERR(ts.clock)) {
255 dev_err(dev, "cannot get adc clock source\n");
256 return -ENOENT;
257 }
258
259 clk_enable(ts.clock);
260 dev_dbg(dev, "got and enabled clocks\n");
261
262 ts.irq_tc = ret = platform_get_irq(pdev, 0);
263 if (ret < 0) {
264 dev_err(dev, "no resource for interrupt\n");
265 goto err_clk;
266 }
267
268 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
269 if (!res) {
270 dev_err(dev, "no resource for registers\n");
271 ret = -ENOENT;
272 goto err_clk;
273 }
274
275 ts.io = ioremap(res->start, resource_size(res));
276 if (ts.io == NULL) {
277 dev_err(dev, "cannot map registers\n");
278 ret = -ENOMEM;
279 goto err_clk;
280 }
281
Naveen Krishna658ad392010-05-18 20:44:38 +0900282 /* inititalise the gpio */
283 if (info->cfg_gpio)
284 info->cfg_gpio(to_platform_device(ts.dev));
Arnaud Patardf5f96b92009-11-23 09:47:12 -0800285
286 ts.client = s3c_adc_register(pdev, s3c24xx_ts_select,
287 s3c24xx_ts_conversion, 1);
288 if (IS_ERR(ts.client)) {
289 dev_err(dev, "failed to register adc client\n");
290 ret = PTR_ERR(ts.client);
291 goto err_iomap;
292 }
293
294 /* Initialise registers */
295 if ((info->delay & 0xffff) > 0)
296 writel(info->delay & 0xffff, ts.io + S3C2410_ADCDLY);
297
298 writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
299
300 input_dev = input_allocate_device();
301 if (!input_dev) {
302 dev_err(dev, "Unable to allocate the input device !!\n");
303 ret = -ENOMEM;
304 goto err_iomap;
305 }
306
307 ts.input = input_dev;
308 ts.input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
309 ts.input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
310 input_set_abs_params(ts.input, ABS_X, 0, 0x3FF, 0, 0);
311 input_set_abs_params(ts.input, ABS_Y, 0, 0x3FF, 0, 0);
312
313 ts.input->name = "S3C24XX TouchScreen";
314 ts.input->id.bustype = BUS_HOST;
315 ts.input->id.vendor = 0xDEAD;
316 ts.input->id.product = 0xBEEF;
317 ts.input->id.version = 0x0102;
318
319 ts.shift = info->oversampling_shift;
320
321 ret = request_irq(ts.irq_tc, stylus_irq, IRQF_DISABLED,
322 "s3c2410_ts_pen", ts.input);
323 if (ret) {
324 dev_err(dev, "cannot get TC interrupt\n");
325 goto err_inputdev;
326 }
327
328 dev_info(dev, "driver attached, registering input device\n");
329
330 /* All went ok, so register to the input system */
331 ret = input_register_device(ts.input);
332 if (ret < 0) {
333 dev_err(dev, "failed to register input device\n");
334 ret = -EIO;
335 goto err_tcirq;
336 }
337
338 return 0;
339
340 err_tcirq:
341 free_irq(ts.irq_tc, ts.input);
342 err_inputdev:
343 input_unregister_device(ts.input);
344 err_iomap:
345 iounmap(ts.io);
346 err_clk:
347 del_timer_sync(&touch_timer);
348 clk_put(ts.clock);
349 return ret;
350}
351
352/**
353 * s3c2410ts_remove - device core removal entry point
354 * @pdev: The device we are being removed from.
355 *
356 * Free up our state ready to be removed.
357 */
358static int __devexit s3c2410ts_remove(struct platform_device *pdev)
359{
360 free_irq(ts.irq_tc, ts.input);
361 del_timer_sync(&touch_timer);
362
363 clk_disable(ts.clock);
364 clk_put(ts.clock);
365
366 input_unregister_device(ts.input);
367 iounmap(ts.io);
368
369 return 0;
370}
371
372#ifdef CONFIG_PM
373static int s3c2410ts_suspend(struct device *dev)
374{
375 writel(TSC_SLEEP, ts.io + S3C2410_ADCTSC);
376 disable_irq(ts.irq_tc);
377 clk_disable(ts.clock);
378
379 return 0;
380}
381
382static int s3c2410ts_resume(struct device *dev)
383{
384 struct platform_device *pdev = to_platform_device(dev);
385 struct s3c2410_ts_mach_info *info = pdev->dev.platform_data;
386
387 clk_enable(ts.clock);
Vasily Khoruzhick2f095862010-02-19 01:18:11 -0800388 enable_irq(ts.irq_tc);
Arnaud Patardf5f96b92009-11-23 09:47:12 -0800389
390 /* Initialise registers */
391 if ((info->delay & 0xffff) > 0)
392 writel(info->delay & 0xffff, ts.io + S3C2410_ADCDLY);
393
394 writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
395
396 return 0;
397}
398
399static struct dev_pm_ops s3c_ts_pmops = {
400 .suspend = s3c2410ts_suspend,
401 .resume = s3c2410ts_resume,
402};
403#endif
404
405static struct platform_device_id s3cts_driver_ids[] = {
406 { "s3c2410-ts", 0 },
407 { "s3c2440-ts", 1 },
408 { }
409};
410MODULE_DEVICE_TABLE(platform, s3cts_driver_ids);
411
412static struct platform_driver s3c_ts_driver = {
413 .driver = {
414 .name = "s3c24xx-ts",
415 .owner = THIS_MODULE,
416#ifdef CONFIG_PM
417 .pm = &s3c_ts_pmops,
418#endif
419 },
420 .id_table = s3cts_driver_ids,
421 .probe = s3c2410ts_probe,
422 .remove = __devexit_p(s3c2410ts_remove),
423};
424
425static int __init s3c2410ts_init(void)
426{
427 return platform_driver_register(&s3c_ts_driver);
428}
429
430static void __exit s3c2410ts_exit(void)
431{
432 platform_driver_unregister(&s3c_ts_driver);
433}
434
435module_init(s3c2410ts_init);
436module_exit(s3c2410ts_exit);
437
438MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, "
439 "Ben Dooks <ben@simtec.co.uk>, "
440 "Simtec Electronics <linux@simtec.co.uk>");
441MODULE_DESCRIPTION("S3C24XX Touchscreen driver");
442MODULE_LICENSE("GPL v2");