]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/input/touchscreen/ti_am335x_tsc.c
Input: edt-ft5x06 - add a missing condition
[karo-tx-linux.git] / drivers / input / touchscreen / ti_am335x_tsc.c
1 /*
2  * TI Touch Screen driver
3  *
4  * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
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
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/err.h>
20 #include <linux/module.h>
21 #include <linux/input.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/clk.h>
25 #include <linux/platform_device.h>
26 #include <linux/io.h>
27 #include <linux/delay.h>
28 #include <linux/of.h>
29 #include <linux/of_device.h>
30
31 #include <linux/mfd/ti_am335x_tscadc.h>
32
33 #define ADCFSM_STEPID           0x10
34 #define SEQ_SETTLE              275
35 #define MAX_12BIT               ((1 << 12) - 1)
36
37 static const int config_pins[] = {
38         STEPCONFIG_XPP,
39         STEPCONFIG_XNN,
40         STEPCONFIG_YPP,
41         STEPCONFIG_YNN,
42 };
43
44 struct titsc {
45         struct input_dev        *input;
46         struct ti_tscadc_dev    *mfd_tscadc;
47         unsigned int            irq;
48         unsigned int            wires;
49         unsigned int            x_plate_resistance;
50         bool                    pen_down;
51         int                     coordinate_readouts;
52         u32                     config_inp[4];
53         u32                     bit_xp, bit_xn, bit_yp, bit_yn;
54         u32                     inp_xp, inp_xn, inp_yp, inp_yn;
55         u32                     step_mask;
56 };
57
58 static unsigned int titsc_readl(struct titsc *ts, unsigned int reg)
59 {
60         return readl(ts->mfd_tscadc->tscadc_base + reg);
61 }
62
63 static void titsc_writel(struct titsc *tsc, unsigned int reg,
64                                         unsigned int val)
65 {
66         writel(val, tsc->mfd_tscadc->tscadc_base + reg);
67 }
68
69 static int titsc_config_wires(struct titsc *ts_dev)
70 {
71         u32 analog_line[4];
72         u32 wire_order[4];
73         int i, bit_cfg;
74
75         for (i = 0; i < 4; i++) {
76                 /*
77                  * Get the order in which TSC wires are attached
78                  * w.r.t. each of the analog input lines on the EVM.
79                  */
80                 analog_line[i] = (ts_dev->config_inp[i] & 0xF0) >> 4;
81                 wire_order[i] = ts_dev->config_inp[i] & 0x0F;
82                 if (WARN_ON(analog_line[i] > 7))
83                         return -EINVAL;
84                 if (WARN_ON(wire_order[i] > ARRAY_SIZE(config_pins)))
85                         return -EINVAL;
86         }
87
88         for (i = 0; i < 4; i++) {
89                 int an_line;
90                 int wi_order;
91
92                 an_line = analog_line[i];
93                 wi_order = wire_order[i];
94                 bit_cfg = config_pins[wi_order];
95                 if (bit_cfg == 0)
96                         return -EINVAL;
97                 switch (wi_order) {
98                 case 0:
99                         ts_dev->bit_xp = bit_cfg;
100                         ts_dev->inp_xp = an_line;
101                         break;
102
103                 case 1:
104                         ts_dev->bit_xn = bit_cfg;
105                         ts_dev->inp_xn = an_line;
106                         break;
107
108                 case 2:
109                         ts_dev->bit_yp = bit_cfg;
110                         ts_dev->inp_yp = an_line;
111                         break;
112                 case 3:
113                         ts_dev->bit_yn = bit_cfg;
114                         ts_dev->inp_yn = an_line;
115                         break;
116                 }
117         }
118         return 0;
119 }
120
121 static void titsc_step_config(struct titsc *ts_dev)
122 {
123         unsigned int    config;
124         int i;
125         int end_step;
126         u32 stepenable;
127
128         config = STEPCONFIG_MODE_HWSYNC |
129                         STEPCONFIG_AVG_16 | ts_dev->bit_xp;
130         switch (ts_dev->wires) {
131         case 4:
132                 config |= STEPCONFIG_INP(ts_dev->inp_yp) | ts_dev->bit_xn;
133                 break;
134         case 5:
135                 config |= ts_dev->bit_yn |
136                                 STEPCONFIG_INP_AN4 | ts_dev->bit_xn |
137                                 ts_dev->bit_yp;
138                 break;
139         case 8:
140                 config |= STEPCONFIG_INP(ts_dev->inp_yp) | ts_dev->bit_xn;
141                 break;
142         }
143
144         /* 1 … coordinate_readouts is for X */
145         end_step = ts_dev->coordinate_readouts;
146         for (i = 0; i < end_step; i++) {
147                 titsc_writel(ts_dev, REG_STEPCONFIG(i), config);
148                 titsc_writel(ts_dev, REG_STEPDELAY(i), STEPCONFIG_OPENDLY);
149         }
150
151         config = 0;
152         config = STEPCONFIG_MODE_HWSYNC |
153                         STEPCONFIG_AVG_16 | ts_dev->bit_yn |
154                         STEPCONFIG_INM_ADCREFM;
155         switch (ts_dev->wires) {
156         case 4:
157                 config |= ts_dev->bit_yp | STEPCONFIG_INP(ts_dev->inp_xp);
158                 break;
159         case 5:
160                 config |= ts_dev->bit_xp | STEPCONFIG_INP_AN4 |
161                                 ts_dev->bit_xn | ts_dev->bit_yp;
162                 break;
163         case 8:
164                 config |= ts_dev->bit_yp | STEPCONFIG_INP(ts_dev->inp_xp);
165                 break;
166         }
167
168         /* coordinate_readouts … coordinate_readouts * 2 is for Y */
169         end_step = ts_dev->coordinate_readouts * 2;
170         for (i = ts_dev->coordinate_readouts; i < end_step; i++) {
171                 titsc_writel(ts_dev, REG_STEPCONFIG(i), config);
172                 titsc_writel(ts_dev, REG_STEPDELAY(i), STEPCONFIG_OPENDLY);
173         }
174
175         /* Charge step configuration */
176         config = ts_dev->bit_xp | ts_dev->bit_yn |
177                         STEPCHARGE_RFP_XPUL | STEPCHARGE_RFM_XNUR |
178                         STEPCHARGE_INM_AN1 | STEPCHARGE_INP(ts_dev->inp_yp);
179
180         titsc_writel(ts_dev, REG_CHARGECONFIG, config);
181         titsc_writel(ts_dev, REG_CHARGEDELAY, CHARGEDLY_OPENDLY);
182
183         /* coordinate_readouts * 2 … coordinate_readouts * 2 + 2 is for Z */
184         config = STEPCONFIG_MODE_HWSYNC |
185                         STEPCONFIG_AVG_16 | ts_dev->bit_yp |
186                         ts_dev->bit_xn | STEPCONFIG_INM_ADCREFM |
187                         STEPCONFIG_INP(ts_dev->inp_xp);
188         titsc_writel(ts_dev, REG_STEPCONFIG(end_step), config);
189         titsc_writel(ts_dev, REG_STEPDELAY(end_step),
190                         STEPCONFIG_OPENDLY);
191
192         end_step++;
193         config |= STEPCONFIG_INP(ts_dev->inp_yn);
194         titsc_writel(ts_dev, REG_STEPCONFIG(end_step), config);
195         titsc_writel(ts_dev, REG_STEPDELAY(end_step),
196                         STEPCONFIG_OPENDLY);
197
198         /* The steps1 … end and bit 0 for TS_Charge */
199         stepenable = (1 << (end_step + 2)) - 1;
200         ts_dev->step_mask = stepenable;
201         am335x_tsc_se_set(ts_dev->mfd_tscadc, ts_dev->step_mask);
202 }
203
204 static void titsc_read_coordinates(struct titsc *ts_dev,
205                 u32 *x, u32 *y, u32 *z1, u32 *z2)
206 {
207         unsigned int fifocount = titsc_readl(ts_dev, REG_FIFO0CNT);
208         unsigned int prev_val_x = ~0, prev_val_y = ~0;
209         unsigned int prev_diff_x = ~0, prev_diff_y = ~0;
210         unsigned int read, diff;
211         unsigned int i, channel;
212         unsigned int creads = ts_dev->coordinate_readouts;
213
214         *z1 = *z2 = 0;
215         if (fifocount % (creads * 2 + 2))
216                 fifocount -= fifocount % (creads * 2 + 2);
217         /*
218          * Delta filter is used to remove large variations in sampled
219          * values from ADC. The filter tries to predict where the next
220          * coordinate could be. This is done by taking a previous
221          * coordinate and subtracting it form current one. Further the
222          * algorithm compares the difference with that of a present value,
223          * if true the value is reported to the sub system.
224          */
225         for (i = 0; i < fifocount; i++) {
226                 read = titsc_readl(ts_dev, REG_FIFO0);
227
228                 channel = (read & 0xf0000) >> 16;
229                 read &= 0xfff;
230                 if (channel < creads) {
231                         diff = abs(read - prev_val_x);
232                         if (diff < prev_diff_x) {
233                                 prev_diff_x = diff;
234                                 *x = read;
235                         }
236                         prev_val_x = read;
237
238                 } else if (channel < creads * 2) {
239                         diff = abs(read - prev_val_y);
240                         if (diff < prev_diff_y) {
241                                 prev_diff_y = diff;
242                                 *y = read;
243                         }
244                         prev_val_y = read;
245
246                 } else if (channel < creads * 2 + 1) {
247                         *z1 = read;
248
249                 } else if (channel < creads * 2 + 2) {
250                         *z2 = read;
251                 }
252         }
253 }
254
255 static irqreturn_t titsc_irq(int irq, void *dev)
256 {
257         struct titsc *ts_dev = dev;
258         struct input_dev *input_dev = ts_dev->input;
259         unsigned int status, irqclr = 0;
260         unsigned int x = 0, y = 0;
261         unsigned int z1, z2, z;
262         unsigned int fsm;
263
264         status = titsc_readl(ts_dev, REG_IRQSTATUS);
265         /*
266          * ADC and touchscreen share the IRQ line.
267          * FIFO1 interrupts are used by ADC. Handle FIFO0 IRQs here only
268          */
269         if (status & IRQENB_FIFO0THRES) {
270
271                 titsc_read_coordinates(ts_dev, &x, &y, &z1, &z2);
272
273                 if (ts_dev->pen_down && z1 != 0 && z2 != 0) {
274                         /*
275                          * Calculate pressure using formula
276                          * Resistance(touch) = x plate resistance *
277                          * x postion/4096 * ((z2 / z1) - 1)
278                          */
279                         z = z1 - z2;
280                         z *= x;
281                         z *= ts_dev->x_plate_resistance;
282                         z /= z2;
283                         z = (z + 2047) >> 12;
284
285                         if (z <= MAX_12BIT) {
286                                 input_report_abs(input_dev, ABS_X, x);
287                                 input_report_abs(input_dev, ABS_Y, y);
288                                 input_report_abs(input_dev, ABS_PRESSURE, z);
289                                 input_report_key(input_dev, BTN_TOUCH, 1);
290                                 input_sync(input_dev);
291                         }
292                 }
293                 irqclr |= IRQENB_FIFO0THRES;
294         }
295
296         /*
297          * Time for sequencer to settle, to read
298          * correct state of the sequencer.
299          */
300         udelay(SEQ_SETTLE);
301
302         status = titsc_readl(ts_dev, REG_RAWIRQSTATUS);
303         if (status & IRQENB_PENUP) {
304                 /* Pen up event */
305                 fsm = titsc_readl(ts_dev, REG_ADCFSM);
306                 if (fsm == ADCFSM_STEPID) {
307                         ts_dev->pen_down = false;
308                         input_report_key(input_dev, BTN_TOUCH, 0);
309                         input_report_abs(input_dev, ABS_PRESSURE, 0);
310                         input_sync(input_dev);
311                 } else {
312                         ts_dev->pen_down = true;
313                 }
314                 irqclr |= IRQENB_PENUP;
315         }
316
317         if (status & IRQENB_HW_PEN) {
318
319                 titsc_writel(ts_dev, REG_IRQWAKEUP, 0x00);
320                 titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
321         }
322
323         if (irqclr) {
324                 titsc_writel(ts_dev, REG_IRQSTATUS, irqclr);
325                 am335x_tsc_se_set(ts_dev->mfd_tscadc, ts_dev->step_mask);
326                 return IRQ_HANDLED;
327         }
328         return IRQ_NONE;
329 }
330
331 static int titsc_parse_dt(struct platform_device *pdev,
332                                         struct titsc *ts_dev)
333 {
334         struct device_node *node = pdev->dev.of_node;
335         int err;
336
337         if (!node)
338                 return -EINVAL;
339
340         err = of_property_read_u32(node, "ti,wires", &ts_dev->wires);
341         if (err < 0)
342                 return err;
343         switch (ts_dev->wires) {
344         case 4:
345         case 5:
346         case 8:
347                 break;
348         default:
349                 return -EINVAL;
350         }
351
352         err = of_property_read_u32(node, "ti,x-plate-resistance",
353                         &ts_dev->x_plate_resistance);
354         if (err < 0)
355                 return err;
356
357         err = of_property_read_u32(node, "ti,coordiante-readouts",
358                         &ts_dev->coordinate_readouts);
359         if (err < 0)
360                 return err;
361
362         return of_property_read_u32_array(node, "ti,wire-config",
363                         ts_dev->config_inp, ARRAY_SIZE(ts_dev->config_inp));
364 }
365
366 /*
367  * The functions for inserting/removing driver as a module.
368  */
369
370 static int titsc_probe(struct platform_device *pdev)
371 {
372         struct titsc *ts_dev;
373         struct input_dev *input_dev;
374         struct ti_tscadc_dev *tscadc_dev = ti_tscadc_dev_get(pdev);
375         int err;
376
377         /* Allocate memory for device */
378         ts_dev = kzalloc(sizeof(struct titsc), GFP_KERNEL);
379         input_dev = input_allocate_device();
380         if (!ts_dev || !input_dev) {
381                 dev_err(&pdev->dev, "failed to allocate memory.\n");
382                 err = -ENOMEM;
383                 goto err_free_mem;
384         }
385
386         tscadc_dev->tsc = ts_dev;
387         ts_dev->mfd_tscadc = tscadc_dev;
388         ts_dev->input = input_dev;
389         ts_dev->irq = tscadc_dev->irq;
390
391         err = titsc_parse_dt(pdev, ts_dev);
392         if (err) {
393                 dev_err(&pdev->dev, "Could not find valid DT data.\n");
394                 goto err_free_mem;
395         }
396
397         err = request_irq(ts_dev->irq, titsc_irq,
398                           IRQF_SHARED, pdev->dev.driver->name, ts_dev);
399         if (err) {
400                 dev_err(&pdev->dev, "failed to allocate irq.\n");
401                 goto err_free_mem;
402         }
403
404         titsc_writel(ts_dev, REG_IRQENABLE, IRQENB_FIFO0THRES);
405         err = titsc_config_wires(ts_dev);
406         if (err) {
407                 dev_err(&pdev->dev, "wrong i/p wire configuration\n");
408                 goto err_free_irq;
409         }
410         titsc_step_config(ts_dev);
411         titsc_writel(ts_dev, REG_FIFO0THR,
412                         ts_dev->coordinate_readouts * 2 + 2 - 1);
413
414         input_dev->name = "ti-tsc";
415         input_dev->dev.parent = &pdev->dev;
416
417         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
418         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
419
420         input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
421         input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
422         input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
423
424         /* register to the input system */
425         err = input_register_device(input_dev);
426         if (err)
427                 goto err_free_irq;
428
429         platform_set_drvdata(pdev, ts_dev);
430         return 0;
431
432 err_free_irq:
433         free_irq(ts_dev->irq, ts_dev);
434 err_free_mem:
435         input_free_device(input_dev);
436         kfree(ts_dev);
437         return err;
438 }
439
440 static int titsc_remove(struct platform_device *pdev)
441 {
442         struct titsc *ts_dev = platform_get_drvdata(pdev);
443         u32 steps;
444
445         free_irq(ts_dev->irq, ts_dev);
446
447         /* total steps followed by the enable mask */
448         steps = 2 * ts_dev->coordinate_readouts + 2;
449         steps = (1 << steps) - 1;
450         am335x_tsc_se_clr(ts_dev->mfd_tscadc, steps);
451
452         input_unregister_device(ts_dev->input);
453
454         kfree(ts_dev);
455         return 0;
456 }
457
458 #ifdef CONFIG_PM
459 static int titsc_suspend(struct device *dev)
460 {
461         struct titsc *ts_dev = dev_get_drvdata(dev);
462         struct ti_tscadc_dev *tscadc_dev;
463         unsigned int idle;
464
465         tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
466         if (device_may_wakeup(tscadc_dev->dev)) {
467                 idle = titsc_readl(ts_dev, REG_IRQENABLE);
468                 titsc_writel(ts_dev, REG_IRQENABLE,
469                                 (idle | IRQENB_HW_PEN));
470                 titsc_writel(ts_dev, REG_IRQWAKEUP, IRQWKUP_ENB);
471         }
472         return 0;
473 }
474
475 static int titsc_resume(struct device *dev)
476 {
477         struct titsc *ts_dev = dev_get_drvdata(dev);
478         struct ti_tscadc_dev *tscadc_dev;
479
480         tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
481         if (device_may_wakeup(tscadc_dev->dev)) {
482                 titsc_writel(ts_dev, REG_IRQWAKEUP,
483                                 0x00);
484                 titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
485         }
486         titsc_step_config(ts_dev);
487         titsc_writel(ts_dev, REG_FIFO0THR,
488                         ts_dev->coordinate_readouts * 2 + 2 - 1);
489         return 0;
490 }
491
492 static const struct dev_pm_ops titsc_pm_ops = {
493         .suspend = titsc_suspend,
494         .resume  = titsc_resume,
495 };
496 #define TITSC_PM_OPS (&titsc_pm_ops)
497 #else
498 #define TITSC_PM_OPS NULL
499 #endif
500
501 static const struct of_device_id ti_tsc_dt_ids[] = {
502         { .compatible = "ti,am3359-tsc", },
503         { }
504 };
505 MODULE_DEVICE_TABLE(of, ti_tsc_dt_ids);
506
507 static struct platform_driver ti_tsc_driver = {
508         .probe  = titsc_probe,
509         .remove = titsc_remove,
510         .driver = {
511                 .name   = "TI-am335x-tsc",
512                 .owner  = THIS_MODULE,
513                 .pm     = TITSC_PM_OPS,
514                 .of_match_table = ti_tsc_dt_ids,
515         },
516 };
517 module_platform_driver(ti_tsc_driver);
518
519 MODULE_DESCRIPTION("TI touchscreen controller driver");
520 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
521 MODULE_LICENSE("GPL");