Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / input / touchscreen / tnetv107x-ts.c
1 /*
2  * Texas Instruments TNETV107X Touchscreen Driver
3  *
4  * Copyright (C) 2010 Texas Instruments
5  *
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.
9  *
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.
14  */
15
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>
26
27 #include <mach/tnetv107x.h>
28
29 #define TSC_PENUP_POLL          (HZ / 5)
30 #define IDLE_TIMEOUT            100 /* msec */
31
32 /*
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.
36  */
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)
41
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];
51 };
52
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)
68
69 /* ADC DATA Capture Register bits */
70 #define DATA_VALID      BIT(16)
71
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))
79
80 struct sample {
81         int x, y, p;
82 };
83
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;
95 };
96
97 static int tsc_read_sample(struct tsc_data *ts, struct sample* sample)
98 {
99         int     x, y, z1, z2, t, p = 0;
100         u32     val;
101
102         val = tsc_read(ts, chval[0]);
103         if (val & DATA_VALID)
104                 x = val & 0xffff;
105         else
106                 return -EINVAL;
107
108         y  = tsc_read(ts, chval[1]) & 0xffff;
109         z1 = tsc_read(ts, chval[2]) & 0xffff;
110         z2 = tsc_read(ts, chval[3]) & 0xffff;
111
112         if (z1) {
113                 t = ((600 * x) * (z2 - z1));
114                 p = t / (u32) (z1 << 12);
115                 if (p < 0)
116                         p = 0;
117         }
118
119         sample->x  = x;
120         sample->y  = y;
121         sample->p  = p;
122
123         return 0;
124 }
125
126 static void tsc_poll(unsigned long data)
127 {
128         struct tsc_data *ts = (struct tsc_data *)data;
129         unsigned long flags;
130         int i, val, x, y, p;
131
132         spin_lock_irqsave(&ts->lock, flags);
133
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) {
139                 /*
140                  * A touch event lasted less than our skip count.  Salvage and
141                  * report anyway.
142                  */
143                 for (i = 0, val = 0; i < ts->sample_count; i++)
144                         val += ts->samples[i].x;
145                 x = val / ts->sample_count;
146
147                 for (i = 0, val = 0; i < ts->sample_count; i++)
148                         val += ts->samples[i].y;
149                 y = val / ts->sample_count;
150
151                 for (i = 0, val = 0; i < ts->sample_count; i++)
152                         val += ts->samples[i].p;
153                 p = val / ts->sample_count;
154
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);
160         }
161
162         ts->sample_count = 0;
163
164         spin_unlock_irqrestore(&ts->lock, flags);
165 }
166
167 static irqreturn_t tsc_irq(int irq, void *dev_id)
168 {
169         struct tsc_data *ts = (struct tsc_data *)dev_id;
170         struct sample *sample;
171         int index;
172
173         spin_lock(&ts->lock);
174
175         index = ts->sample_count % TSC_SAMPLES;
176         sample = &ts->samples[index];
177         if (tsc_read_sample(ts, sample) < 0)
178                 goto out;
179
180         if (++ts->sample_count >= TSC_SKIP) {
181                 index = (ts->sample_count - TSC_TAIL_SKIP - 1) % TSC_SAMPLES;
182                 sample = &ts->samples[index];
183
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);
190         }
191         mod_timer(&ts->timer, jiffies + TSC_PENUP_POLL);
192 out:
193         spin_unlock(&ts->lock);
194         return IRQ_HANDLED;
195 }
196
197 static int tsc_start(struct input_dev *dev)
198 {
199         struct tsc_data *ts = input_get_drvdata(dev);
200         unsigned long timeout = jiffies + msecs_to_jiffies(IDLE_TIMEOUT);
201         u32 val;
202
203         clk_enable(ts->clk);
204
205         /* Go to idle mode, before any initialization */
206         while (time_after(timeout, jiffies)) {
207                 if (tsc_read(ts, tscm) & IDLE)
208                         break;
209         }
210
211         if (time_before(timeout, jiffies)) {
212                 dev_warn(ts->dev, "timeout waiting for idle\n");
213                 clk_disable(ts->clk);
214                 return -EIO;
215         }
216
217         /* Configure TSC Control register*/
218         val = (PONBG | PON | PVSTC(4) | ONE_SHOT | ZMEASURE_EN);
219         tsc_write(ts, tscm, val);
220
221         /* Bring TSC out of reset: Clear AFE reset bit */
222         val &= ~(AFERST);
223         tsc_write(ts, tscm, val);
224
225         /* Configure all pins for hardware control*/
226         tsc_write(ts, bwcm, 0);
227
228         /* Finally enable the TSC */
229         tsc_set_bits(ts, tscm, TSC_EN);
230
231         return 0;
232 }
233
234 static void tsc_stop(struct input_dev *dev)
235 {
236         struct tsc_data *ts = input_get_drvdata(dev);
237
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);
242 }
243
244 static int __devinit tsc_probe(struct platform_device *pdev)
245 {
246         struct device *dev = &pdev->dev;
247         struct tsc_data *ts;
248         int error = 0;
249         u32 rev = 0;
250
251         ts = kzalloc(sizeof(struct tsc_data), GFP_KERNEL);
252         if (!ts) {
253                 dev_err(dev, "cannot allocate device info\n");
254                 return -ENOMEM;
255         }
256
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);
261
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;
267         }
268
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;
274         }
275
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;
282         }
283
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;
289         }
290
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;
296         }
297
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;
303         }
304
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;
310         }
311         input_set_drvdata(ts->input_dev, ts);
312
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;
318
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);
324
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);
328
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);
332
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;
337         }
338
339         return 0;
340
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);
354
355         return error;
356 }
357
358 static int __devexit tsc_remove(struct platform_device *pdev)
359 {
360         struct tsc_data *ts = platform_get_drvdata(pdev);
361
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);
369
370         return 0;
371 }
372
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,
378 };
379
380 static int __init tsc_init(void)
381 {
382         return platform_driver_register(&tsc_driver);
383 }
384
385 static void __exit tsc_exit(void)
386 {
387         platform_driver_unregister(&tsc_driver);
388 }
389
390 module_init(tsc_init);
391 module_exit(tsc_exit);
392
393 MODULE_AUTHOR("Cyril Chemparathy");
394 MODULE_DESCRIPTION("TNETV107X Touchscreen Driver");
395 MODULE_ALIAS("platform: tnetv107x-ts");
396 MODULE_LICENSE("GPL");