2 * Texas Instruments TNETV107X Touchscreen Driver
4 * Copyright (C) 2010 Texas Instruments
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/err.h>
18 #include <linux/errno.h>
19 #include <linux/input.h>
20 #include <linux/platform_device.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
24 #include <linux/ctype.h>
26 #include <linux/clk.h>
28 #include <mach/tnetv107x.h>
30 #define TSC_PENUP_POLL (HZ / 5)
31 #define IDLE_TIMEOUT 100 /* msec */
34 * The first and last samples of a touch interval are usually garbage and need
35 * to be filtered out with these devices. The following definitions control
36 * the number of samples skipped.
38 #define TSC_HEAD_SKIP 1
39 #define TSC_TAIL_SKIP 1
40 #define TSC_SKIP (TSC_HEAD_SKIP + TSC_TAIL_SKIP + 1)
41 #define TSC_SAMPLES (TSC_SKIP + 1)
43 /* Register Offsets */
54 /* TSC Mode Configuration Register (tscm) bits */
57 #define ZMEASURE_EN BIT(2)
61 #define ONE_SHOT BIT(6)
64 #define AVGNUM(x) (((x) & 0x03) << 9)
65 #define PVSTC(x) (((x) & 0x07) << 11)
68 #define AFERST BIT(16)
70 /* ADC DATA Capture Register bits */
71 #define DATA_VALID BIT(16)
73 /* Register Access Macros */
74 #define tsc_read(ts, reg) __raw_readl(&(ts)->regs->reg)
75 #define tsc_write(ts, reg, val) __raw_writel(val, &(ts)->regs->reg);
76 #define tsc_set_bits(ts, reg, val) \
77 tsc_write(ts, reg, tsc_read(ts, reg) | (val))
78 #define tsc_clr_bits(ts, reg, val) \
79 tsc_write(ts, reg, tsc_read(ts, reg) & ~(val))
86 struct input_dev *input_dev;
88 struct tsc_regs __iomem *regs;
89 struct timer_list timer;
94 struct sample samples[TSC_SAMPLES];
98 static int tsc_read_sample(struct tsc_data *ts, struct sample* sample)
100 int x, y, z1, z2, t, p = 0;
103 val = tsc_read(ts, chval[0]);
104 if (val & DATA_VALID)
109 y = tsc_read(ts, chval[1]) & 0xffff;
110 z1 = tsc_read(ts, chval[2]) & 0xffff;
111 z2 = tsc_read(ts, chval[3]) & 0xffff;
114 t = ((600 * x) * (z2 - z1));
115 p = t / (u32) (z1 << 12);
127 static void tsc_poll(unsigned long data)
129 struct tsc_data *ts = (struct tsc_data *)data;
133 spin_lock_irqsave(&ts->lock, flags);
135 if (ts->sample_count >= TSC_SKIP) {
136 input_report_abs(ts->input_dev, ABS_PRESSURE, 0);
137 input_report_key(ts->input_dev, BTN_TOUCH, 0);
138 input_sync(ts->input_dev);
139 } else if (ts->sample_count > 0) {
141 * A touch event lasted less than our skip count. Salvage and
144 for (i = 0, val = 0; i < ts->sample_count; i++)
145 val += ts->samples[i].x;
146 x = val / ts->sample_count;
148 for (i = 0, val = 0; i < ts->sample_count; i++)
149 val += ts->samples[i].y;
150 y = val / ts->sample_count;
152 for (i = 0, val = 0; i < ts->sample_count; i++)
153 val += ts->samples[i].p;
154 p = val / ts->sample_count;
156 input_report_abs(ts->input_dev, ABS_X, x);
157 input_report_abs(ts->input_dev, ABS_Y, y);
158 input_report_abs(ts->input_dev, ABS_PRESSURE, p);
159 input_report_key(ts->input_dev, BTN_TOUCH, 1);
160 input_sync(ts->input_dev);
163 ts->sample_count = 0;
165 spin_unlock_irqrestore(&ts->lock, flags);
168 static irqreturn_t tsc_irq(int irq, void *dev_id)
170 struct tsc_data *ts = (struct tsc_data *)dev_id;
171 struct sample *sample;
174 spin_lock(&ts->lock);
176 index = ts->sample_count % TSC_SAMPLES;
177 sample = &ts->samples[index];
178 if (tsc_read_sample(ts, sample) < 0)
181 if (++ts->sample_count >= TSC_SKIP) {
182 index = (ts->sample_count - TSC_TAIL_SKIP - 1) % TSC_SAMPLES;
183 sample = &ts->samples[index];
185 input_report_abs(ts->input_dev, ABS_X, sample->x);
186 input_report_abs(ts->input_dev, ABS_Y, sample->y);
187 input_report_abs(ts->input_dev, ABS_PRESSURE, sample->p);
188 if (ts->sample_count == TSC_SKIP)
189 input_report_key(ts->input_dev, BTN_TOUCH, 1);
190 input_sync(ts->input_dev);
192 mod_timer(&ts->timer, jiffies + TSC_PENUP_POLL);
194 spin_unlock(&ts->lock);
198 static int tsc_start(struct input_dev *dev)
200 struct tsc_data *ts = input_get_drvdata(dev);
201 unsigned long timeout = jiffies + msecs_to_jiffies(IDLE_TIMEOUT);
206 /* Go to idle mode, before any initialization */
207 while (time_after(timeout, jiffies)) {
208 if (tsc_read(ts, tscm) & IDLE)
212 if (time_before(timeout, jiffies)) {
213 dev_warn(ts->dev, "timeout waiting for idle\n");
214 clk_disable(ts->clk);
218 /* Configure TSC Control register*/
219 val = (PONBG | PON | PVSTC(4) | ONE_SHOT | ZMEASURE_EN);
220 tsc_write(ts, tscm, val);
222 /* Bring TSC out of reset: Clear AFE reset bit */
224 tsc_write(ts, tscm, val);
226 /* Configure all pins for hardware control*/
227 tsc_write(ts, bwcm, 0);
229 /* Finally enable the TSC */
230 tsc_set_bits(ts, tscm, TSC_EN);
235 static void tsc_stop(struct input_dev *dev)
237 struct tsc_data *ts = input_get_drvdata(dev);
239 tsc_clr_bits(ts, tscm, TSC_EN);
240 synchronize_irq(ts->tsc_irq);
241 del_timer_sync(&ts->timer);
242 clk_disable(ts->clk);
245 static int __devinit tsc_probe(struct platform_device *pdev)
247 struct device *dev = &pdev->dev;
252 ts = kzalloc(sizeof(struct tsc_data), GFP_KERNEL);
254 dev_err(dev, "cannot allocate device info\n");
259 spin_lock_init(&ts->lock);
260 setup_timer(&ts->timer, tsc_poll, (unsigned long)ts);
261 platform_set_drvdata(pdev, ts);
263 ts->tsc_irq = platform_get_irq(pdev, 0);
264 if (ts->tsc_irq < 0) {
265 dev_err(dev, "cannot determine device interrupt\n");
270 ts->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
272 dev_err(dev, "cannot determine register area\n");
277 if (!request_mem_region(ts->res->start, resource_size(ts->res),
279 dev_err(dev, "cannot claim register memory\n");
285 ts->regs = ioremap(ts->res->start, resource_size(ts->res));
287 dev_err(dev, "cannot map register memory\n");
292 ts->clk = clk_get(dev, NULL);
293 if (IS_ERR(ts->clk)) {
294 dev_err(dev, "cannot claim device clock\n");
295 error = PTR_ERR(ts->clk);
299 error = request_threaded_irq(ts->tsc_irq, NULL, tsc_irq, 0,
302 dev_err(ts->dev, "Could not allocate ts irq\n");
306 ts->input_dev = input_allocate_device();
307 if (!ts->input_dev) {
308 dev_err(dev, "cannot allocate input device\n");
312 input_set_drvdata(ts->input_dev, ts);
314 ts->input_dev->name = pdev->name;
315 ts->input_dev->id.bustype = BUS_HOST;
316 ts->input_dev->dev.parent = &pdev->dev;
317 ts->input_dev->open = tsc_start;
318 ts->input_dev->close = tsc_stop;
321 rev = tsc_read(ts, rev);
322 ts->input_dev->id.product = ((rev >> 8) & 0x07);
323 ts->input_dev->id.version = ((rev >> 16) & 0xfff);
324 clk_disable(ts->clk);
326 __set_bit(EV_KEY, ts->input_dev->evbit);
327 __set_bit(EV_ABS, ts->input_dev->evbit);
328 __set_bit(BTN_TOUCH, ts->input_dev->keybit);
330 input_set_abs_params(ts->input_dev, ABS_X, 0, 0xffff, 5, 0);
331 input_set_abs_params(ts->input_dev, ABS_Y, 0, 0xffff, 5, 0);
332 input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 4095, 128, 0);
334 error = input_register_device(ts->input_dev);
336 dev_err(dev, "failed input device registration\n");
343 input_free_device(ts->input_dev);
345 free_irq(ts->tsc_irq, ts);
351 release_mem_region(ts->res->start, resource_size(ts->res));
353 platform_set_drvdata(pdev, NULL);
359 static int __devexit tsc_remove(struct platform_device *pdev)
361 struct tsc_data *ts = platform_get_drvdata(pdev);
363 input_unregister_device(ts->input_dev);
364 free_irq(ts->tsc_irq, ts);
367 release_mem_region(ts->res->start, resource_size(ts->res));
368 platform_set_drvdata(pdev, NULL);
374 static struct platform_driver tsc_driver = {
376 .remove = __devexit_p(tsc_remove),
377 .driver.name = "tnetv107x-ts",
378 .driver.owner = THIS_MODULE,
381 static int __init tsc_init(void)
383 return platform_driver_register(&tsc_driver);
386 static void __exit tsc_exit(void)
388 platform_driver_unregister(&tsc_driver);
391 module_init(tsc_init);
392 module_exit(tsc_exit);
394 MODULE_AUTHOR("Cyril Chemparathy");
395 MODULE_DESCRIPTION("TNETV107X Touchscreen Driver");
396 MODULE_ALIAS("platform: tnetv107x-ts");
397 MODULE_LICENSE("GPL");