staging: usbip: bugfix for isochronous packets and optimization
[zen-stable.git] / drivers / input / touchscreen / tnetv107x-ts.c
blob22a3411e93c56bf7f8577334ab19d7a4e2003111
1 /*
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>
25 #include <linux/io.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 */
44 struct tsc_regs {
45 u32 rev;
46 u32 tscm;
47 u32 bwcm;
48 u32 swc;
49 u32 adcchnl;
50 u32 adcdata;
51 u32 chval[4];
54 /* TSC Mode Configuration Register (tscm) bits */
55 #define WMODE BIT(0)
56 #define TSKIND BIT(1)
57 #define ZMEASURE_EN BIT(2)
58 #define IDLE BIT(3)
59 #define TSC_EN BIT(4)
60 #define STOP BIT(5)
61 #define ONE_SHOT BIT(6)
62 #define SINGLE BIT(7)
63 #define AVG BIT(8)
64 #define AVGNUM(x) (((x) & 0x03) << 9)
65 #define PVSTC(x) (((x) & 0x07) << 11)
66 #define PON BIT(14)
67 #define PONBG BIT(15)
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))
81 struct sample {
82 int x, y, p;
85 struct tsc_data {
86 struct input_dev *input_dev;
87 struct resource *res;
88 struct tsc_regs __iomem *regs;
89 struct timer_list timer;
90 spinlock_t lock;
91 struct clk *clk;
92 struct device *dev;
93 int sample_count;
94 struct sample samples[TSC_SAMPLES];
95 int tsc_irq;
98 static int tsc_read_sample(struct tsc_data *ts, struct sample* sample)
100 int x, y, z1, z2, t, p = 0;
101 u32 val;
103 val = tsc_read(ts, chval[0]);
104 if (val & DATA_VALID)
105 x = val & 0xffff;
106 else
107 return -EINVAL;
109 y = tsc_read(ts, chval[1]) & 0xffff;
110 z1 = tsc_read(ts, chval[2]) & 0xffff;
111 z2 = tsc_read(ts, chval[3]) & 0xffff;
113 if (z1) {
114 t = ((600 * x) * (z2 - z1));
115 p = t / (u32) (z1 << 12);
116 if (p < 0)
117 p = 0;
120 sample->x = x;
121 sample->y = y;
122 sample->p = p;
124 return 0;
127 static void tsc_poll(unsigned long data)
129 struct tsc_data *ts = (struct tsc_data *)data;
130 unsigned long flags;
131 int i, val, x, y, p;
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
142 * report anyway.
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;
172 int index;
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)
179 goto out;
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);
193 out:
194 spin_unlock(&ts->lock);
195 return IRQ_HANDLED;
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);
202 u32 val;
204 clk_enable(ts->clk);
206 /* Go to idle mode, before any initialization */
207 while (time_after(timeout, jiffies)) {
208 if (tsc_read(ts, tscm) & IDLE)
209 break;
212 if (time_before(timeout, jiffies)) {
213 dev_warn(ts->dev, "timeout waiting for idle\n");
214 clk_disable(ts->clk);
215 return -EIO;
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 */
223 val &= ~(AFERST);
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);
232 return 0;
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;
248 struct tsc_data *ts;
249 int error = 0;
250 u32 rev = 0;
252 ts = kzalloc(sizeof(struct tsc_data), GFP_KERNEL);
253 if (!ts) {
254 dev_err(dev, "cannot allocate device info\n");
255 return -ENOMEM;
258 ts->dev = dev;
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");
266 error = -ENODEV;
267 goto error_res;
270 ts->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
271 if (!ts->res) {
272 dev_err(dev, "cannot determine register area\n");
273 error = -ENODEV;
274 goto error_res;
277 if (!request_mem_region(ts->res->start, resource_size(ts->res),
278 pdev->name)) {
279 dev_err(dev, "cannot claim register memory\n");
280 ts->res = NULL;
281 error = -EINVAL;
282 goto error_res;
285 ts->regs = ioremap(ts->res->start, resource_size(ts->res));
286 if (!ts->regs) {
287 dev_err(dev, "cannot map register memory\n");
288 error = -ENOMEM;
289 goto error_map;
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);
296 goto error_clk;
299 error = request_threaded_irq(ts->tsc_irq, NULL, tsc_irq, 0,
300 dev_name(dev), ts);
301 if (error < 0) {
302 dev_err(ts->dev, "Could not allocate ts irq\n");
303 goto error_irq;
306 ts->input_dev = input_allocate_device();
307 if (!ts->input_dev) {
308 dev_err(dev, "cannot allocate input device\n");
309 error = -ENOMEM;
310 goto error_input;
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;
320 clk_enable(ts->clk);
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);
335 if (error < 0) {
336 dev_err(dev, "failed input device registration\n");
337 goto error_reg;
340 return 0;
342 error_reg:
343 input_free_device(ts->input_dev);
344 error_input:
345 free_irq(ts->tsc_irq, ts);
346 error_irq:
347 clk_put(ts->clk);
348 error_clk:
349 iounmap(ts->regs);
350 error_map:
351 release_mem_region(ts->res->start, resource_size(ts->res));
352 error_res:
353 platform_set_drvdata(pdev, NULL);
354 kfree(ts);
356 return error;
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);
365 clk_put(ts->clk);
366 iounmap(ts->regs);
367 release_mem_region(ts->res->start, resource_size(ts->res));
368 platform_set_drvdata(pdev, NULL);
369 kfree(ts);
371 return 0;
374 static struct platform_driver tsc_driver = {
375 .probe = tsc_probe,
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");