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