drm/i915: range-restricted eviction support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / touchscreen / tnetv107x-ts.c
blobcf1dba2e267c1510be41c901f02a058f94673bbb
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/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>
24 #include <linux/io.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 */
43 struct tsc_regs {
44 u32 rev;
45 u32 tscm;
46 u32 bwcm;
47 u32 swc;
48 u32 adcchnl;
49 u32 adcdata;
50 u32 chval[4];
53 /* TSC Mode Configuration Register (tscm) bits */
54 #define WMODE BIT(0)
55 #define TSKIND BIT(1)
56 #define ZMEASURE_EN BIT(2)
57 #define IDLE BIT(3)
58 #define TSC_EN BIT(4)
59 #define STOP BIT(5)
60 #define ONE_SHOT BIT(6)
61 #define SINGLE BIT(7)
62 #define AVG BIT(8)
63 #define AVGNUM(x) (((x) & 0x03) << 9)
64 #define PVSTC(x) (((x) & 0x07) << 11)
65 #define PON BIT(14)
66 #define PONBG BIT(15)
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))
80 struct sample {
81 int x, y, p;
84 struct tsc_data {
85 struct input_dev *input_dev;
86 struct resource *res;
87 struct tsc_regs __iomem *regs;
88 struct timer_list timer;
89 spinlock_t lock;
90 struct clk *clk;
91 struct device *dev;
92 int sample_count;
93 struct sample samples[TSC_SAMPLES];
94 int tsc_irq;
97 static int tsc_read_sample(struct tsc_data *ts, struct sample* sample)
99 int x, y, z1, z2, t, p = 0;
100 u32 val;
102 val = tsc_read(ts, chval[0]);
103 if (val & DATA_VALID)
104 x = val & 0xffff;
105 else
106 return -EINVAL;
108 y = tsc_read(ts, chval[1]) & 0xffff;
109 z1 = tsc_read(ts, chval[2]) & 0xffff;
110 z2 = tsc_read(ts, chval[3]) & 0xffff;
112 if (z1) {
113 t = ((600 * x) * (z2 - z1));
114 p = t / (u32) (z1 << 12);
115 if (p < 0)
116 p = 0;
119 sample->x = x;
120 sample->y = y;
121 sample->p = p;
123 return 0;
126 static void tsc_poll(unsigned long data)
128 struct tsc_data *ts = (struct tsc_data *)data;
129 unsigned long flags;
130 int i, val, x, y, p;
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
141 * report anyway.
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;
171 int index;
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)
178 goto out;
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);
192 out:
193 spin_unlock(&ts->lock);
194 return IRQ_HANDLED;
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);
201 u32 val;
203 clk_enable(ts->clk);
205 /* Go to idle mode, before any initialization */
206 while (time_after(timeout, jiffies)) {
207 if (tsc_read(ts, tscm) & IDLE)
208 break;
211 if (time_before(timeout, jiffies)) {
212 dev_warn(ts->dev, "timeout waiting for idle\n");
213 clk_disable(ts->clk);
214 return -EIO;
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 */
222 val &= ~(AFERST);
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);
231 return 0;
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;
247 struct tsc_data *ts;
248 int error = 0;
249 u32 rev = 0;
251 ts = kzalloc(sizeof(struct tsc_data), GFP_KERNEL);
252 if (!ts) {
253 dev_err(dev, "cannot allocate device info\n");
254 return -ENOMEM;
257 ts->dev = dev;
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");
265 error = -ENODEV;
266 goto error_res;
269 ts->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
270 if (!ts->res) {
271 dev_err(dev, "cannot determine register area\n");
272 error = -ENODEV;
273 goto error_res;
276 if (!request_mem_region(ts->res->start, resource_size(ts->res),
277 pdev->name)) {
278 dev_err(dev, "cannot claim register memory\n");
279 ts->res = NULL;
280 error = -EINVAL;
281 goto error_res;
284 ts->regs = ioremap(ts->res->start, resource_size(ts->res));
285 if (!ts->regs) {
286 dev_err(dev, "cannot map register memory\n");
287 error = -ENOMEM;
288 goto error_map;
291 ts->clk = clk_get(dev, NULL);
292 if (!ts->clk) {
293 dev_err(dev, "cannot claim device clock\n");
294 error = -EINVAL;
295 goto error_clk;
298 error = request_threaded_irq(ts->tsc_irq, NULL, tsc_irq, 0,
299 dev_name(dev), ts);
300 if (error < 0) {
301 dev_err(ts->dev, "Could not allocate ts irq\n");
302 goto error_irq;
305 ts->input_dev = input_allocate_device();
306 if (!ts->input_dev) {
307 dev_err(dev, "cannot allocate input device\n");
308 error = -ENOMEM;
309 goto error_input;
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;
319 clk_enable(ts->clk);
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);
334 if (error < 0) {
335 dev_err(dev, "failed input device registration\n");
336 goto error_reg;
339 return 0;
341 error_reg:
342 input_free_device(ts->input_dev);
343 error_input:
344 free_irq(ts->tsc_irq, ts);
345 error_irq:
346 clk_put(ts->clk);
347 error_clk:
348 iounmap(ts->regs);
349 error_map:
350 release_mem_region(ts->res->start, resource_size(ts->res));
351 error_res:
352 platform_set_drvdata(pdev, NULL);
353 kfree(ts);
355 return error;
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);
364 clk_put(ts->clk);
365 iounmap(ts->regs);
366 release_mem_region(ts->res->start, resource_size(ts->res));
367 platform_set_drvdata(pdev, NULL);
368 kfree(ts);
370 return 0;
373 static struct platform_driver tsc_driver = {
374 .probe = tsc_probe,
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");