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