2 * TI Touch Screen driver
4 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
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.
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>
27 #include <linux/delay.h>
29 #include <linux/of_device.h>
31 #include <linux/mfd/ti_am335x_tscadc.h>
33 #define ADCFSM_STEPID 0x10
34 #define SEQ_SETTLE 275
35 #define MAX_12BIT ((1 << 12) - 1)
37 static const int config_pins[] = {
45 struct input_dev *input;
46 struct ti_tscadc_dev *mfd_tscadc;
49 unsigned int x_plate_resistance;
51 int coordinate_readouts;
53 u32 bit_xp, bit_xn, bit_yp, bit_yn;
54 u32 inp_xp, inp_xn, inp_yp, inp_yn;
58 static unsigned int titsc_readl(struct titsc *ts, unsigned int reg)
60 return readl(ts->mfd_tscadc->tscadc_base + reg);
63 static void titsc_writel(struct titsc *tsc, unsigned int reg,
66 writel(val, tsc->mfd_tscadc->tscadc_base + reg);
69 static int titsc_config_wires(struct titsc *ts_dev)
75 for (i = 0; i < 4; i++) {
77 * Get the order in which TSC wires are attached
78 * w.r.t. each of the analog input lines on the EVM.
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))
84 if (WARN_ON(wire_order[i] > ARRAY_SIZE(config_pins)))
88 for (i = 0; i < 4; i++) {
92 an_line = analog_line[i];
93 wi_order = wire_order[i];
94 bit_cfg = config_pins[wi_order];
99 ts_dev->bit_xp = bit_cfg;
100 ts_dev->inp_xp = an_line;
104 ts_dev->bit_xn = bit_cfg;
105 ts_dev->inp_xn = an_line;
109 ts_dev->bit_yp = bit_cfg;
110 ts_dev->inp_yp = an_line;
113 ts_dev->bit_yn = bit_cfg;
114 ts_dev->inp_yn = an_line;
121 static void titsc_step_config(struct titsc *ts_dev)
128 config = STEPCONFIG_MODE_HWSYNC |
129 STEPCONFIG_AVG_16 | ts_dev->bit_xp;
130 switch (ts_dev->wires) {
132 config |= STEPCONFIG_INP(ts_dev->inp_yp) | ts_dev->bit_xn;
135 config |= ts_dev->bit_yn |
136 STEPCONFIG_INP_AN4 | ts_dev->bit_xn |
140 config |= STEPCONFIG_INP(ts_dev->inp_yp) | ts_dev->bit_xn;
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);
152 config = STEPCONFIG_MODE_HWSYNC |
153 STEPCONFIG_AVG_16 | ts_dev->bit_yn |
154 STEPCONFIG_INM_ADCREFM;
155 switch (ts_dev->wires) {
157 config |= ts_dev->bit_yp | STEPCONFIG_INP(ts_dev->inp_xp);
160 config |= ts_dev->bit_xp | STEPCONFIG_INP_AN4 |
161 ts_dev->bit_xn | ts_dev->bit_yp;
164 config |= ts_dev->bit_yp | STEPCONFIG_INP(ts_dev->inp_xp);
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);
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);
180 titsc_writel(ts_dev, REG_CHARGECONFIG, config);
181 titsc_writel(ts_dev, REG_CHARGEDELAY, CHARGEDLY_OPENDLY);
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),
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),
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);
204 static void titsc_read_coordinates(struct titsc *ts_dev,
205 u32 *x, u32 *y, u32 *z1, u32 *z2)
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;
215 if (fifocount % (creads * 2 + 2))
216 fifocount -= fifocount % (creads * 2 + 2);
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.
225 for (i = 0; i < fifocount; i++) {
226 read = titsc_readl(ts_dev, REG_FIFO0);
228 channel = (read & 0xf0000) >> 16;
230 if (channel < creads) {
231 diff = abs(read - prev_val_x);
232 if (diff < prev_diff_x) {
238 } else if (channel < creads * 2) {
239 diff = abs(read - prev_val_y);
240 if (diff < prev_diff_y) {
246 } else if (channel < creads * 2 + 1) {
249 } else if (channel < creads * 2 + 2) {
255 static irqreturn_t titsc_irq(int irq, void *dev)
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;
264 status = titsc_readl(ts_dev, REG_IRQSTATUS);
266 * ADC and touchscreen share the IRQ line.
267 * FIFO1 interrupts are used by ADC. Handle FIFO0 IRQs here only
269 if (status & IRQENB_FIFO0THRES) {
271 titsc_read_coordinates(ts_dev, &x, &y, &z1, &z2);
273 if (ts_dev->pen_down && z1 != 0 && z2 != 0) {
275 * Calculate pressure using formula
276 * Resistance(touch) = x plate resistance *
277 * x postion/4096 * ((z2 / z1) - 1)
281 z *= ts_dev->x_plate_resistance;
283 z = (z + 2047) >> 12;
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);
293 irqclr |= IRQENB_FIFO0THRES;
297 * Time for sequencer to settle, to read
298 * correct state of the sequencer.
302 status = titsc_readl(ts_dev, REG_RAWIRQSTATUS);
303 if (status & IRQENB_PENUP) {
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);
312 ts_dev->pen_down = true;
314 irqclr |= IRQENB_PENUP;
317 if (status & IRQENB_HW_PEN) {
319 titsc_writel(ts_dev, REG_IRQWAKEUP, 0x00);
320 titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
324 titsc_writel(ts_dev, REG_IRQSTATUS, irqclr);
325 am335x_tsc_se_set(ts_dev->mfd_tscadc, ts_dev->step_mask);
331 static int titsc_parse_dt(struct platform_device *pdev,
332 struct titsc *ts_dev)
334 struct device_node *node = pdev->dev.of_node;
340 err = of_property_read_u32(node, "ti,wires", &ts_dev->wires);
343 switch (ts_dev->wires) {
352 err = of_property_read_u32(node, "ti,x-plate-resistance",
353 &ts_dev->x_plate_resistance);
358 * Try with the new binding first. If it fails, try again with
359 * bogus, miss-spelled version.
361 err = of_property_read_u32(node, "ti,coordinate-readouts",
362 &ts_dev->coordinate_readouts);
364 err = of_property_read_u32(node, "ti,coordiante-readouts",
365 &ts_dev->coordinate_readouts);
369 return of_property_read_u32_array(node, "ti,wire-config",
370 ts_dev->config_inp, ARRAY_SIZE(ts_dev->config_inp));
374 * The functions for inserting/removing driver as a module.
377 static int titsc_probe(struct platform_device *pdev)
379 struct titsc *ts_dev;
380 struct input_dev *input_dev;
381 struct ti_tscadc_dev *tscadc_dev = ti_tscadc_dev_get(pdev);
384 /* Allocate memory for device */
385 ts_dev = kzalloc(sizeof(struct titsc), GFP_KERNEL);
386 input_dev = input_allocate_device();
387 if (!ts_dev || !input_dev) {
388 dev_err(&pdev->dev, "failed to allocate memory.\n");
393 tscadc_dev->tsc = ts_dev;
394 ts_dev->mfd_tscadc = tscadc_dev;
395 ts_dev->input = input_dev;
396 ts_dev->irq = tscadc_dev->irq;
398 err = titsc_parse_dt(pdev, ts_dev);
400 dev_err(&pdev->dev, "Could not find valid DT data.\n");
404 err = request_irq(ts_dev->irq, titsc_irq,
405 IRQF_SHARED, pdev->dev.driver->name, ts_dev);
407 dev_err(&pdev->dev, "failed to allocate irq.\n");
411 titsc_writel(ts_dev, REG_IRQENABLE, IRQENB_FIFO0THRES);
412 err = titsc_config_wires(ts_dev);
414 dev_err(&pdev->dev, "wrong i/p wire configuration\n");
417 titsc_step_config(ts_dev);
418 titsc_writel(ts_dev, REG_FIFO0THR,
419 ts_dev->coordinate_readouts * 2 + 2 - 1);
421 input_dev->name = "ti-tsc";
422 input_dev->dev.parent = &pdev->dev;
424 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
425 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
427 input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
428 input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
429 input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
431 /* register to the input system */
432 err = input_register_device(input_dev);
436 platform_set_drvdata(pdev, ts_dev);
440 free_irq(ts_dev->irq, ts_dev);
442 input_free_device(input_dev);
447 static int titsc_remove(struct platform_device *pdev)
449 struct titsc *ts_dev = platform_get_drvdata(pdev);
452 free_irq(ts_dev->irq, ts_dev);
454 /* total steps followed by the enable mask */
455 steps = 2 * ts_dev->coordinate_readouts + 2;
456 steps = (1 << steps) - 1;
457 am335x_tsc_se_clr(ts_dev->mfd_tscadc, steps);
459 input_unregister_device(ts_dev->input);
466 static int titsc_suspend(struct device *dev)
468 struct titsc *ts_dev = dev_get_drvdata(dev);
469 struct ti_tscadc_dev *tscadc_dev;
472 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
473 if (device_may_wakeup(tscadc_dev->dev)) {
474 idle = titsc_readl(ts_dev, REG_IRQENABLE);
475 titsc_writel(ts_dev, REG_IRQENABLE,
476 (idle | IRQENB_HW_PEN));
477 titsc_writel(ts_dev, REG_IRQWAKEUP, IRQWKUP_ENB);
482 static int titsc_resume(struct device *dev)
484 struct titsc *ts_dev = dev_get_drvdata(dev);
485 struct ti_tscadc_dev *tscadc_dev;
487 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
488 if (device_may_wakeup(tscadc_dev->dev)) {
489 titsc_writel(ts_dev, REG_IRQWAKEUP,
491 titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
493 titsc_step_config(ts_dev);
494 titsc_writel(ts_dev, REG_FIFO0THR,
495 ts_dev->coordinate_readouts * 2 + 2 - 1);
499 static const struct dev_pm_ops titsc_pm_ops = {
500 .suspend = titsc_suspend,
501 .resume = titsc_resume,
503 #define TITSC_PM_OPS (&titsc_pm_ops)
505 #define TITSC_PM_OPS NULL
508 static const struct of_device_id ti_tsc_dt_ids[] = {
509 { .compatible = "ti,am3359-tsc", },
512 MODULE_DEVICE_TABLE(of, ti_tsc_dt_ids);
514 static struct platform_driver ti_tsc_driver = {
515 .probe = titsc_probe,
516 .remove = titsc_remove,
518 .name = "TI-am335x-tsc",
519 .owner = THIS_MODULE,
521 .of_match_table = ti_tsc_dt_ids,
524 module_platform_driver(ti_tsc_driver);
526 MODULE_DESCRIPTION("TI touchscreen controller driver");
527 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
528 MODULE_LICENSE("GPL");