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/errno.h>
18 #include <linux/input.h>
19 #include <linux/platform_device.h>
20 #include <linux/interrupt.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/ctype.h>
25 #include <linux/clk.h>
27 #include <mach/tnetv107x.h>
29 #define TSC_PENUP_POLL (HZ / 5)
30 #define IDLE_TIMEOUT 100 /* msec */
33 * The first and last samples of a touch interval are usually garbage and need
34 * to be filtered out with these devices. The following definitions control
35 * the number of samples skipped.
37 #define TSC_HEAD_SKIP 1
38 #define TSC_TAIL_SKIP 1
39 #define TSC_SKIP (TSC_HEAD_SKIP + TSC_TAIL_SKIP + 1)
40 #define TSC_SAMPLES (TSC_SKIP + 1)
42 /* Register Offsets */
53 /* TSC Mode Configuration Register (tscm) bits */
56 #define ZMEASURE_EN BIT(2)
60 #define ONE_SHOT BIT(6)
63 #define AVGNUM(x) (((x) & 0x03) << 9)
64 #define PVSTC(x) (((x) & 0x07) << 11)
67 #define AFERST BIT(16)
69 /* ADC DATA Capture Register bits */
70 #define DATA_VALID BIT(16)
72 /* Register Access Macros */
73 #define tsc_read(ts, reg) __raw_readl(&(ts)->regs->reg)
74 #define tsc_write(ts, reg, val) __raw_writel(val, &(ts)->regs->reg);
75 #define tsc_set_bits(ts, reg, val) \
76 tsc_write(ts, reg, tsc_read(ts, reg) | (val))
77 #define tsc_clr_bits(ts, reg, val) \
78 tsc_write(ts, reg, tsc_read(ts, reg) & ~(val))
85 struct input_dev *input_dev;
87 struct tsc_regs __iomem *regs;
88 struct timer_list timer;
93 struct sample samples[TSC_SAMPLES];
97 static int tsc_read_sample(struct tsc_data *ts, struct sample* sample)
99 int x, y, z1, z2, t, p = 0;
102 val = tsc_read(ts, chval[0]);
103 if (val & DATA_VALID)
108 y = tsc_read(ts, chval[1]) & 0xffff;
109 z1 = tsc_read(ts, chval[2]) & 0xffff;
110 z2 = tsc_read(ts, chval[3]) & 0xffff;
113 t = ((600 * x) * (z2 - z1));
114 p = t / (u32) (z1 << 12);
126 static void tsc_poll(unsigned long data)
128 struct tsc_data *ts = (struct tsc_data *)data;
132 spin_lock_irqsave(&ts->lock, flags);
134 if (ts->sample_count >= TSC_SKIP) {
135 input_report_abs(ts->input_dev, ABS_PRESSURE, 0);
136 input_report_key(ts->input_dev, BTN_TOUCH, 0);
137 input_sync(ts->input_dev);
138 } else if (ts->sample_count > 0) {
140 * A touch event lasted less than our skip count. Salvage and
143 for (i = 0, val = 0; i < ts->sample_count; i++)
144 val += ts->samples[i].x;
145 x = val / ts->sample_count;
147 for (i = 0, val = 0; i < ts->sample_count; i++)
148 val += ts->samples[i].y;
149 y = val / ts->sample_count;
151 for (i = 0, val = 0; i < ts->sample_count; i++)
152 val += ts->samples[i].p;
153 p = val / ts->sample_count;
155 input_report_abs(ts->input_dev, ABS_X, x);
156 input_report_abs(ts->input_dev, ABS_Y, y);
157 input_report_abs(ts->input_dev, ABS_PRESSURE, p);
158 input_report_key(ts->input_dev, BTN_TOUCH, 1);
159 input_sync(ts->input_dev);
162 ts->sample_count = 0;
164 spin_unlock_irqrestore(&ts->lock, flags);
167 static irqreturn_t tsc_irq(int irq, void *dev_id)
169 struct tsc_data *ts = (struct tsc_data *)dev_id;
170 struct sample *sample;
173 spin_lock(&ts->lock);
175 index = ts->sample_count % TSC_SAMPLES;
176 sample = &ts->samples[index];
177 if (tsc_read_sample(ts, sample) < 0)
180 if (++ts->sample_count >= TSC_SKIP) {
181 index = (ts->sample_count - TSC_TAIL_SKIP - 1) % TSC_SAMPLES;
182 sample = &ts->samples[index];
184 input_report_abs(ts->input_dev, ABS_X, sample->x);
185 input_report_abs(ts->input_dev, ABS_Y, sample->y);
186 input_report_abs(ts->input_dev, ABS_PRESSURE, sample->p);
187 if (ts->sample_count == TSC_SKIP)
188 input_report_key(ts->input_dev, BTN_TOUCH, 1);
189 input_sync(ts->input_dev);
191 mod_timer(&ts->timer, jiffies + TSC_PENUP_POLL);
193 spin_unlock(&ts->lock);
197 static int tsc_start(struct input_dev *dev)
199 struct tsc_data *ts = input_get_drvdata(dev);
200 unsigned long timeout = jiffies + msecs_to_jiffies(IDLE_TIMEOUT);
205 /* Go to idle mode, before any initialization */
206 while (time_after(timeout, jiffies)) {
207 if (tsc_read(ts, tscm) & IDLE)
211 if (time_before(timeout, jiffies)) {
212 dev_warn(ts->dev, "timeout waiting for idle\n");
213 clk_disable(ts->clk);
217 /* Configure TSC Control register*/
218 val = (PONBG | PON | PVSTC(4) | ONE_SHOT | ZMEASURE_EN);
219 tsc_write(ts, tscm, val);
221 /* Bring TSC out of reset: Clear AFE reset bit */
223 tsc_write(ts, tscm, val);
225 /* Configure all pins for hardware control*/
226 tsc_write(ts, bwcm, 0);
228 /* Finally enable the TSC */
229 tsc_set_bits(ts, tscm, TSC_EN);
234 static void tsc_stop(struct input_dev *dev)
236 struct tsc_data *ts = input_get_drvdata(dev);
238 tsc_clr_bits(ts, tscm, TSC_EN);
239 synchronize_irq(ts->tsc_irq);
240 del_timer_sync(&ts->timer);
241 clk_disable(ts->clk);
244 static int __devinit tsc_probe(struct platform_device *pdev)
246 struct device *dev = &pdev->dev;
251 ts = kzalloc(sizeof(struct tsc_data), GFP_KERNEL);
253 dev_err(dev, "cannot allocate device info\n");
258 spin_lock_init(&ts->lock);
259 setup_timer(&ts->timer, tsc_poll, (unsigned long)ts);
260 platform_set_drvdata(pdev, ts);
262 ts->tsc_irq = platform_get_irq(pdev, 0);
263 if (ts->tsc_irq < 0) {
264 dev_err(dev, "cannot determine device interrupt\n");
269 ts->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
271 dev_err(dev, "cannot determine register area\n");
276 if (!request_mem_region(ts->res->start, resource_size(ts->res),
278 dev_err(dev, "cannot claim register memory\n");
284 ts->regs = ioremap(ts->res->start, resource_size(ts->res));
286 dev_err(dev, "cannot map register memory\n");
291 ts->clk = clk_get(dev, NULL);
293 dev_err(dev, "cannot claim device clock\n");
298 error = request_threaded_irq(ts->tsc_irq, NULL, tsc_irq, 0,
301 dev_err(ts->dev, "Could not allocate ts irq\n");
305 ts->input_dev = input_allocate_device();
306 if (!ts->input_dev) {
307 dev_err(dev, "cannot allocate input device\n");
311 input_set_drvdata(ts->input_dev, ts);
313 ts->input_dev->name = pdev->name;
314 ts->input_dev->id.bustype = BUS_HOST;
315 ts->input_dev->dev.parent = &pdev->dev;
316 ts->input_dev->open = tsc_start;
317 ts->input_dev->close = tsc_stop;
320 rev = tsc_read(ts, rev);
321 ts->input_dev->id.product = ((rev >> 8) & 0x07);
322 ts->input_dev->id.version = ((rev >> 16) & 0xfff);
323 clk_disable(ts->clk);
325 __set_bit(EV_KEY, ts->input_dev->evbit);
326 __set_bit(EV_ABS, ts->input_dev->evbit);
327 __set_bit(BTN_TOUCH, ts->input_dev->keybit);
329 input_set_abs_params(ts->input_dev, ABS_X, 0, 0xffff, 5, 0);
330 input_set_abs_params(ts->input_dev, ABS_Y, 0, 0xffff, 5, 0);
331 input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 4095, 128, 0);
333 error = input_register_device(ts->input_dev);
335 dev_err(dev, "failed input device registration\n");
342 input_free_device(ts->input_dev);
344 free_irq(ts->tsc_irq, ts);
350 release_mem_region(ts->res->start, resource_size(ts->res));
352 platform_set_drvdata(pdev, NULL);
358 static int __devexit tsc_remove(struct platform_device *pdev)
360 struct tsc_data *ts = platform_get_drvdata(pdev);
362 input_unregister_device(ts->input_dev);
363 free_irq(ts->tsc_irq, ts);
366 release_mem_region(ts->res->start, resource_size(ts->res));
367 platform_set_drvdata(pdev, NULL);
373 static struct platform_driver tsc_driver = {
375 .remove = __devexit_p(tsc_remove),
376 .driver.name = "tnetv107x-ts",
377 .driver.owner = THIS_MODULE,
380 static int __init tsc_init(void)
382 return platform_driver_register(&tsc_driver);
385 static void __exit tsc_exit(void)
387 platform_driver_unregister(&tsc_driver);
390 module_init(tsc_init);
391 module_exit(tsc_exit);
393 MODULE_AUTHOR("Cyril Chemparathy");
394 MODULE_DESCRIPTION("TNETV107X Touchscreen Driver");
395 MODULE_ALIAS("platform: tnetv107x-ts");
396 MODULE_LICENSE("GPL");