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/module.h>
17 #include <linux/kernel.h>
18 #include <linux/err.h>
19 #include <linux/errno.h>
20 #include <linux/input.h>
21 #include <linux/platform_device.h>
22 #include <linux/interrupt.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25 #include <linux/ctype.h>
27 #include <linux/clk.h>
29 #include <mach/tnetv107x.h>
31 #define TSC_PENUP_POLL (HZ / 5)
32 #define IDLE_TIMEOUT 100 /* msec */
35 * The first and last samples of a touch interval are usually garbage and need
36 * to be filtered out with these devices. The following definitions control
37 * the number of samples skipped.
39 #define TSC_HEAD_SKIP 1
40 #define TSC_TAIL_SKIP 1
41 #define TSC_SKIP (TSC_HEAD_SKIP + TSC_TAIL_SKIP + 1)
42 #define TSC_SAMPLES (TSC_SKIP + 1)
44 /* Register Offsets */
55 /* TSC Mode Configuration Register (tscm) bits */
58 #define ZMEASURE_EN BIT(2)
62 #define ONE_SHOT BIT(6)
65 #define AVGNUM(x) (((x) & 0x03) << 9)
66 #define PVSTC(x) (((x) & 0x07) << 11)
69 #define AFERST BIT(16)
71 /* ADC DATA Capture Register bits */
72 #define DATA_VALID BIT(16)
74 /* Register Access Macros */
75 #define tsc_read(ts, reg) __raw_readl(&(ts)->regs->reg)
76 #define tsc_write(ts, reg, val) __raw_writel(val, &(ts)->regs->reg);
77 #define tsc_set_bits(ts, reg, val) \
78 tsc_write(ts, reg, tsc_read(ts, reg) | (val))
79 #define tsc_clr_bits(ts, reg, val) \
80 tsc_write(ts, reg, tsc_read(ts, reg) & ~(val))
87 struct input_dev *input_dev;
89 struct tsc_regs __iomem *regs;
90 struct timer_list timer;
95 struct sample samples[TSC_SAMPLES];
99 static int tsc_read_sample(struct tsc_data *ts, struct sample* sample)
101 int x, y, z1, z2, t, p = 0;
104 val = tsc_read(ts, chval[0]);
105 if (val & DATA_VALID)
110 y = tsc_read(ts, chval[1]) & 0xffff;
111 z1 = tsc_read(ts, chval[2]) & 0xffff;
112 z2 = tsc_read(ts, chval[3]) & 0xffff;
115 t = ((600 * x) * (z2 - z1));
116 p = t / (u32) (z1 << 12);
128 static void tsc_poll(unsigned long data)
130 struct tsc_data *ts = (struct tsc_data *)data;
134 spin_lock_irqsave(&ts->lock, flags);
136 if (ts->sample_count >= TSC_SKIP) {
137 input_report_abs(ts->input_dev, ABS_PRESSURE, 0);
138 input_report_key(ts->input_dev, BTN_TOUCH, 0);
139 input_sync(ts->input_dev);
140 } else if (ts->sample_count > 0) {
142 * A touch event lasted less than our skip count. Salvage and
145 for (i = 0, val = 0; i < ts->sample_count; i++)
146 val += ts->samples[i].x;
147 x = val / ts->sample_count;
149 for (i = 0, val = 0; i < ts->sample_count; i++)
150 val += ts->samples[i].y;
151 y = val / ts->sample_count;
153 for (i = 0, val = 0; i < ts->sample_count; i++)
154 val += ts->samples[i].p;
155 p = val / ts->sample_count;
157 input_report_abs(ts->input_dev, ABS_X, x);
158 input_report_abs(ts->input_dev, ABS_Y, y);
159 input_report_abs(ts->input_dev, ABS_PRESSURE, p);
160 input_report_key(ts->input_dev, BTN_TOUCH, 1);
161 input_sync(ts->input_dev);
164 ts->sample_count = 0;
166 spin_unlock_irqrestore(&ts->lock, flags);
169 static irqreturn_t tsc_irq(int irq, void *dev_id)
171 struct tsc_data *ts = (struct tsc_data *)dev_id;
172 struct sample *sample;
175 spin_lock(&ts->lock);
177 index = ts->sample_count % TSC_SAMPLES;
178 sample = &ts->samples[index];
179 if (tsc_read_sample(ts, sample) < 0)
182 if (++ts->sample_count >= TSC_SKIP) {
183 index = (ts->sample_count - TSC_TAIL_SKIP - 1) % TSC_SAMPLES;
184 sample = &ts->samples[index];
186 input_report_abs(ts->input_dev, ABS_X, sample->x);
187 input_report_abs(ts->input_dev, ABS_Y, sample->y);
188 input_report_abs(ts->input_dev, ABS_PRESSURE, sample->p);
189 if (ts->sample_count == TSC_SKIP)
190 input_report_key(ts->input_dev, BTN_TOUCH, 1);
191 input_sync(ts->input_dev);
193 mod_timer(&ts->timer, jiffies + TSC_PENUP_POLL);
195 spin_unlock(&ts->lock);
199 static int tsc_start(struct input_dev *dev)
201 struct tsc_data *ts = input_get_drvdata(dev);
202 unsigned long timeout = jiffies + msecs_to_jiffies(IDLE_TIMEOUT);
207 /* Go to idle mode, before any initialization */
208 while (time_after(timeout, jiffies)) {
209 if (tsc_read(ts, tscm) & IDLE)
213 if (time_before(timeout, jiffies)) {
214 dev_warn(ts->dev, "timeout waiting for idle\n");
215 clk_disable(ts->clk);
219 /* Configure TSC Control register*/
220 val = (PONBG | PON | PVSTC(4) | ONE_SHOT | ZMEASURE_EN);
221 tsc_write(ts, tscm, val);
223 /* Bring TSC out of reset: Clear AFE reset bit */
225 tsc_write(ts, tscm, val);
227 /* Configure all pins for hardware control*/
228 tsc_write(ts, bwcm, 0);
230 /* Finally enable the TSC */
231 tsc_set_bits(ts, tscm, TSC_EN);
236 static void tsc_stop(struct input_dev *dev)
238 struct tsc_data *ts = input_get_drvdata(dev);
240 tsc_clr_bits(ts, tscm, TSC_EN);
241 synchronize_irq(ts->tsc_irq);
242 del_timer_sync(&ts->timer);
243 clk_disable(ts->clk);
246 static int tsc_probe(struct platform_device *pdev)
248 struct device *dev = &pdev->dev;
253 ts = kzalloc(sizeof(struct tsc_data), GFP_KERNEL);
255 dev_err(dev, "cannot allocate device info\n");
260 spin_lock_init(&ts->lock);
261 setup_timer(&ts->timer, tsc_poll, (unsigned long)ts);
262 platform_set_drvdata(pdev, ts);
264 ts->tsc_irq = platform_get_irq(pdev, 0);
265 if (ts->tsc_irq < 0) {
266 dev_err(dev, "cannot determine device interrupt\n");
271 ts->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
273 dev_err(dev, "cannot determine register area\n");
278 if (!request_mem_region(ts->res->start, resource_size(ts->res),
280 dev_err(dev, "cannot claim register memory\n");
286 ts->regs = ioremap(ts->res->start, resource_size(ts->res));
288 dev_err(dev, "cannot map register memory\n");
293 ts->clk = clk_get(dev, NULL);
294 if (IS_ERR(ts->clk)) {
295 dev_err(dev, "cannot claim device clock\n");
296 error = PTR_ERR(ts->clk);
300 error = request_threaded_irq(ts->tsc_irq, NULL, tsc_irq, IRQF_ONESHOT,
303 dev_err(ts->dev, "Could not allocate ts irq\n");
307 ts->input_dev = input_allocate_device();
308 if (!ts->input_dev) {
309 dev_err(dev, "cannot allocate input device\n");
313 input_set_drvdata(ts->input_dev, ts);
315 ts->input_dev->name = pdev->name;
316 ts->input_dev->id.bustype = BUS_HOST;
317 ts->input_dev->dev.parent = &pdev->dev;
318 ts->input_dev->open = tsc_start;
319 ts->input_dev->close = tsc_stop;
322 rev = tsc_read(ts, rev);
323 ts->input_dev->id.product = ((rev >> 8) & 0x07);
324 ts->input_dev->id.version = ((rev >> 16) & 0xfff);
325 clk_disable(ts->clk);
327 __set_bit(EV_KEY, ts->input_dev->evbit);
328 __set_bit(EV_ABS, ts->input_dev->evbit);
329 __set_bit(BTN_TOUCH, ts->input_dev->keybit);
331 input_set_abs_params(ts->input_dev, ABS_X, 0, 0xffff, 5, 0);
332 input_set_abs_params(ts->input_dev, ABS_Y, 0, 0xffff, 5, 0);
333 input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 4095, 128, 0);
335 error = input_register_device(ts->input_dev);
337 dev_err(dev, "failed input device registration\n");
344 input_free_device(ts->input_dev);
346 free_irq(ts->tsc_irq, ts);
352 release_mem_region(ts->res->start, resource_size(ts->res));
359 static int 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));
373 static struct platform_driver tsc_driver = {
375 .remove = tsc_remove,
376 .driver.name = "tnetv107x-ts",
377 .driver.owner = THIS_MODULE,
379 module_platform_driver(tsc_driver);
381 MODULE_AUTHOR("Cyril Chemparathy");
382 MODULE_DESCRIPTION("TNETV107X Touchscreen Driver");
383 MODULE_ALIAS("platform:tnetv107x-ts");
384 MODULE_LICENSE("GPL");