sunxi: driver refresh for 3.13
[openwrt.git] / target / linux / sunxi / patches-3.13 / 280-input-add-sun4i-ts-driver.patch
1 From 328d21d9c56ddca56ed80b1595066b1621f6926b Mon Sep 17 00:00:00 2001
2 From: Hans de Goede <hdegoede@redhat.com>
3 Date: Mon, 23 Dec 2013 16:21:02 +0100
4 Subject: [PATCH] input: Add new sun4i-ts driver for Allwinner sunxi SoC's rtp
5  controller
6
7 Note the sun4i-ts controller is capable of detecting a second touch, but when
8 a second touch is present then the accuracy becomes so bad the reported touch
9 location is not useable.
10
11 The original android driver contains some complicated heuristics using the
12 aprox. distance between the 2 touches to see if the user is making a pinch
13 open / close movement, and then reports emulated multi-touch events around
14 the last touch coordinate (as the dual-touch coordinates are worthless).
15
16 These kinds of heuristics are just asking for trouble (and don't belong
17 in the kernel). So this driver offers straight forward, reliable single
18 touch functionality only.
19
20 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
21 ---
22  .../bindings/input/touchscreen/sun4i.txt           |  15 ++
23  drivers/input/touchscreen/Kconfig                  |  10 +
24  drivers/input/touchscreen/Makefile                 |   1 +
25  drivers/input/touchscreen/sun4i-ts.c               | 262 +++++++++++++++++++++
26  4 files changed, 288 insertions(+)
27  create mode 100644 Documentation/devicetree/bindings/input/touchscreen/sun4i.txt
28  create mode 100644 drivers/input/touchscreen/sun4i-ts.c
29
30 diff --git a/Documentation/devicetree/bindings/input/touchscreen/sun4i.txt b/Documentation/devicetree/bindings/input/touchscreen/sun4i.txt
31 new file mode 100644
32 index 0000000..e45927e
33 --- /dev/null
34 +++ b/Documentation/devicetree/bindings/input/touchscreen/sun4i.txt
35 @@ -0,0 +1,15 @@
36 +sun4i resistive touchscreen controller
37 +--------------------------------------
38 +
39 +Required properties:
40 + - compatible: "allwinner,sun4i-ts"
41 + - reg: mmio address range of the chip
42 + - interrupts: interrupt to which the chip is connected
43 +
44 +Example:
45 +
46 +       rtp: rtp@01c25000 {
47 +               compatible = "allwinner,sun4i-ts";
48 +               reg = <0x01c25000 0x100>;
49 +               interrupts = <29>;
50 +       };
51 diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
52 index 07e9e82..f4c5ca3 100644
53 --- a/drivers/input/touchscreen/Kconfig
54 +++ b/drivers/input/touchscreen/Kconfig
55 @@ -906,6 +906,16 @@ config TOUCHSCREEN_STMPE
56           To compile this driver as a module, choose M here: the
57           module will be called stmpe-ts.
58  
59 +config TOUCHSCREEN_SUN4I
60 +       tristate "Allwinner sun4i resistive touchscreen controller support"
61 +       depends on ARCH_SUNXI
62 +       help
63 +         This selects support for the resistive touchscreen controller
64 +         found on Allwinner sunxi SoCs.
65 +
66 +         To compile this driver as a module, choose M here: the
67 +         module will be called sun4i-ts.
68 +
69  config TOUCHSCREEN_SUR40
70         tristate "Samsung SUR40 (Surface 2.0/PixelSense) touchscreen"
71         depends on USB
72 diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
73 index 62801f2..c8f7375 100644
74 --- a/drivers/input/touchscreen/Makefile
75 +++ b/drivers/input/touchscreen/Makefile
76 @@ -54,6 +54,7 @@ obj-$(CONFIG_TOUCHSCREEN_PIXCIR)      += pixcir_i2c_ts.o
77  obj-$(CONFIG_TOUCHSCREEN_S3C2410)      += s3c2410_ts.o
78  obj-$(CONFIG_TOUCHSCREEN_ST1232)       += st1232.o
79  obj-$(CONFIG_TOUCHSCREEN_STMPE)                += stmpe-ts.o
80 +obj-$(CONFIG_TOUCHSCREEN_SUN4I)                += sun4i-ts.o
81  obj-$(CONFIG_TOUCHSCREEN_SUR40)                += sur40.o
82  obj-$(CONFIG_TOUCHSCREEN_TI_AM335X_TSC)        += ti_am335x_tsc.o
83  obj-$(CONFIG_TOUCHSCREEN_TNETV107X)    += tnetv107x-ts.o
84 diff --git a/drivers/input/touchscreen/sun4i-ts.c b/drivers/input/touchscreen/sun4i-ts.c
85 new file mode 100644
86 index 0000000..5945219
87 --- /dev/null
88 +++ b/drivers/input/touchscreen/sun4i-ts.c
89 @@ -0,0 +1,262 @@
90 +/*
91 + * Allwinner sunxi resistive touchscreen controller driver
92 + *
93 + * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
94 + *
95 + * This program is free software; you can redistribute it and/or modify
96 + * it under the terms of the GNU General Public License as published by
97 + * the Free Software Foundation; either version 2 of the License, or
98 + * (at your option) any later version.
99 + *
100 + * This program is distributed in the hope that it will be useful,
101 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
102 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
103 + * GNU General Public License for more details.
104 + */
105 +
106 +/*
107 + * The sun4i-ts controller is capable of detecting a second touch, but when a
108 + * second touch is present then the accuracy becomes so bad the reported touch
109 + * location is not useable.
110 + *
111 + * The original android driver contains some complicated heuristics using the
112 + * aprox. distance between the 2 touches to see if the user is making a pinch
113 + * open / close movement, and then reports emulated multi-touch events around
114 + * the last touch coordinate (as the dual-touch coordinates are worthless).
115 + *
116 + * These kinds of heuristics are just asking for trouble (and don't belong
117 + * in the kernel). So this driver offers straight forward, reliable single
118 + * touch functionality only.
119 + */
120 +
121 +#include <linux/err.h>
122 +#include <linux/init.h>
123 +#include <linux/input.h>
124 +#include <linux/interrupt.h>
125 +#include <linux/io.h>
126 +#include <linux/module.h>
127 +#include <linux/of_platform.h>
128 +#include <linux/platform_device.h>
129 +#include <linux/slab.h>
130 +
131 +#define TP_CTRL0               0x00
132 +#define TP_CTRL1               0x04
133 +#define TP_CTRL2               0x08
134 +#define TP_CTRL3               0x0c
135 +#define TP_INT_FIFOC           0x10
136 +#define TP_INT_FIFOS           0x14
137 +#define TP_TPR                 0x18
138 +#define TP_CDAT                        0x1c
139 +#define TEMP_DATA              0x20
140 +#define TP_DATA                        0x24
141 +
142 +/* TP_CTRL0 bits */
143 +#define ADC_FIRST_DLY(x)       ((x) << 24) /* 8 bits */
144 +#define ADC_FIRST_DLY_MODE(x)  ((x) << 23)
145 +#define ADC_CLK_SEL(x)         ((x) << 22)
146 +#define ADC_CLK_DIV(x)         ((x) << 20) /* 3 bits */
147 +#define FS_DIV(x)              ((x) << 16) /* 4 bits */
148 +#define T_ACQ(x)               ((x) << 0) /* 16 bits */
149 +
150 +/* TP_CTRL1 bits */
151 +#define STYLUS_UP_DEBOUN(x)    ((x) << 12) /* 8 bits */
152 +#define STYLUS_UP_DEBOUN_EN(x) ((x) << 9)
153 +#define TOUCH_PAN_CALI_EN(x)   ((x) << 6)
154 +#define TP_DUAL_EN(x)          ((x) << 5)
155 +#define TP_MODE_EN(x)          ((x) << 4)
156 +#define TP_ADC_SELECT(x)       ((x) << 3)
157 +#define ADC_CHAN_SELECT(x)     ((x) << 0)  /* 3 bits */
158 +
159 +/* TP_CTRL2 bits */
160 +#define TP_SENSITIVE_ADJUST(x) ((x) << 28) /* 4 bits */
161 +#define TP_MODE_SELECT(x)      ((x) << 26) /* 2 bits */
162 +#define PRE_MEA_EN(x)          ((x) << 24)
163 +#define PRE_MEA_THRE_CNT(x)    ((x) << 0) /* 24 bits */
164 +
165 +/* TP_CTRL3 bits */
166 +#define FILTER_EN(x)           ((x) << 2)
167 +#define FILTER_TYPE(x)         ((x) << 0)  /* 2 bits */
168 +
169 +/* TP_INT_FIFOC irq and fifo mask / control bits */
170 +#define TEMP_IRQ_EN(x)         ((x) << 18)
171 +#define OVERRUN_IRQ_EN(x)      ((x) << 17)
172 +#define DATA_IRQ_EN(x)         ((x) << 16)
173 +#define TP_DATA_XY_CHANGE(x)   ((x) << 13)
174 +#define FIFO_TRIG(x)           ((x) << 8)  /* 5 bits */
175 +#define DATA_DRQ_EN(x)         ((x) << 7)
176 +#define FIFO_FLUSH(x)          ((x) << 4)
177 +#define TP_UP_IRQ_EN(x)                ((x) << 1)
178 +#define TP_DOWN_IRQ_EN(x)      ((x) << 0)
179 +
180 +/* TP_INT_FIFOS irq and fifo status bits */
181 +#define TEMP_DATA_PENDING      BIT(18)
182 +#define FIFO_OVERRUN_PENDING   BIT(17)
183 +#define FIFO_DATA_PENDING      BIT(16)
184 +#define TP_IDLE_FLG            BIT(2)
185 +#define TP_UP_PENDING          BIT(1)
186 +#define TP_DOWN_PENDING                BIT(0)
187 +
188 +/* TP_TPR bits */
189 +#define TEMP_ENABLE(x)         ((x) << 16)
190 +#define TEMP_PERIOD(x)         ((x) << 0)  /* t = x * 256 * 16 / clkin */
191 +
192 +struct sun4i_ts_data {
193 +       struct device *dev;
194 +       struct input_dev *input;
195 +       void __iomem *base;
196 +       unsigned int irq;
197 +       bool ignore_fifo_data;
198 +};
199 +
200 +static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
201 +{
202 +       struct sun4i_ts_data *ts = dev_id;
203 +       u32 reg_val, x, y;
204 +
205 +       reg_val  = readl(ts->base + TP_INT_FIFOS);
206 +
207 +       if (reg_val & FIFO_DATA_PENDING) {
208 +               x = readl(ts->base + TP_DATA);
209 +               y = readl(ts->base + TP_DATA);
210 +               /* The 1st location reported after an up event is unreliable */
211 +               if (!ts->ignore_fifo_data) {
212 +                       input_report_abs(ts->input, ABS_X, x);
213 +                       input_report_abs(ts->input, ABS_Y, y);
214 +                       /*
215 +                        * The hardware has a separate down status bit, but
216 +                        * that gets set before we get the first location,
217 +                        * resulting in reporting a click on the old location.
218 +                        */
219 +                       input_report_key(ts->input, BTN_TOUCH, 1);
220 +                       input_sync(ts->input);
221 +               } else {
222 +                       ts->ignore_fifo_data = false;
223 +               }
224 +       }
225 +
226 +       if (reg_val & TP_UP_PENDING) {
227 +               ts->ignore_fifo_data = true;
228 +               input_report_key(ts->input, BTN_TOUCH, 0);
229 +               input_sync(ts->input);
230 +       }
231 +
232 +       writel(reg_val, ts->base + TP_INT_FIFOS);
233 +
234 +       return IRQ_HANDLED;
235 +}
236 +
237 +static int sun4i_ts_open(struct input_dev *dev)
238 +{
239 +       struct sun4i_ts_data *ts = input_get_drvdata(dev);
240 +
241 +       /* Flush, set trig level to 1, enable data and up irqs */
242 +       writel(DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) | TP_UP_IRQ_EN(1),
243 +              ts->base + TP_INT_FIFOC);
244 +
245 +       return 0;
246 +}
247 +
248 +static void sun4i_ts_close(struct input_dev *dev)
249 +{
250 +       struct sun4i_ts_data *ts = input_get_drvdata(dev);
251 +
252 +       /* Deactivate all IRQs */
253 +       writel(0, ts->base + TP_INT_FIFOC);
254 +}
255 +
256 +static int sun4i_ts_probe(struct platform_device *pdev)
257 +{
258 +       struct sun4i_ts_data *ts;
259 +       struct device *dev = &pdev->dev;
260 +       int ret;
261 +
262 +       ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
263 +       if (!ts)
264 +               return -ENOMEM;
265 +
266 +       ts->dev = dev;
267 +       ts->ignore_fifo_data = true;
268 +
269 +       ts->input = devm_input_allocate_device(dev);
270 +       if (!ts->input)
271 +               return -ENOMEM;
272 +
273 +       ts->input->name = pdev->name;
274 +       ts->input->phys = "sun4i_ts/input0";
275 +       ts->input->open = sun4i_ts_open;
276 +       ts->input->close = sun4i_ts_close;
277 +       ts->input->id.bustype = BUS_HOST;
278 +       ts->input->id.vendor = 0x0001;
279 +       ts->input->id.product = 0x0001;
280 +       ts->input->id.version = 0x0100;
281 +       ts->input->evbit[0] =  BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
282 +       set_bit(BTN_TOUCH, ts->input->keybit);
283 +       input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
284 +       input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
285 +       input_set_drvdata(ts->input, ts);
286 +
287 +       ts->base = devm_ioremap_resource(dev,
288 +                             platform_get_resource(pdev, IORESOURCE_MEM, 0));
289 +       if (IS_ERR(ts->base))
290 +               return PTR_ERR(ts->base);
291 +
292 +       ts->irq = platform_get_irq(pdev, 0);
293 +       ret = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
294 +       if (ret)
295 +               return ret;
296 +
297 +       /*
298 +        * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
299 +        * t_acq = clkin / (16 * 64)
300 +        */
301 +       writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
302 +              ts->base + TP_CTRL0);
303 +
304 +       /*
305 +        * sensitive_adjust = 15 : max, which is not all that sensitive,
306 +        * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
307 +        */
308 +       writel(TP_SENSITIVE_ADJUST(15) | TP_MODE_SELECT(0),
309 +              ts->base + TP_CTRL2);
310 +
311 +       /* Enable median filter, type 1 : 5/3 */
312 +       writel(FILTER_EN(1) | FILTER_TYPE(1), ts->base + TP_CTRL3);
313 +
314 +       /* Enable temperature measurement, period 1953 (2 seconds) */
315 +       writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
316 +
317 +       /*
318 +        * Set stylus up debounce to aprox 10 ms, enable debounce, and
319 +        * finally enable tp mode.
320 +        */
321 +       writel(STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1) | TP_MODE_EN(1),
322 +              ts->base + TP_CTRL1);
323 +
324 +       ret = input_register_device(ts->input);
325 +       if (ret)
326 +               return ret;
327 +
328 +       platform_set_drvdata(pdev, ts);
329 +       return 0;
330 +}
331 +
332 +static const struct of_device_id sun4i_ts_of_match[] = {
333 +       { .compatible = "allwinner,sun4i-ts", },
334 +       { /* sentinel */ }
335 +};
336 +MODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
337 +
338 +static struct platform_driver sun4i_ts_driver = {
339 +       .driver = {
340 +               .owner  = THIS_MODULE,
341 +               .name   = "sun4i-ts",
342 +               .of_match_table = of_match_ptr(sun4i_ts_of_match),
343 +       },
344 +       .probe  = sun4i_ts_probe,
345 +};
346 +
347 +module_platform_driver(sun4i_ts_driver);
348 +
349 +MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
350 +MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
351 +MODULE_LICENSE("GPL");
352 -- 
353 1.8.5.5
354