sunxi: add initial 3.18 support
[openwrt/svn-archive/archive.git] / target / linux / sunxi / patches-3.18 / 110-input-add-sun4i-lradc.patch
1 From 2c1fab89e83245a520871e807e233e66cbdb7c57 Mon Sep 17 00:00:00 2001
2 From: Hans de Goede <hdegoede@redhat.com>
3 Date: Wed, 1 Jan 2014 19:44:49 +0100
4 Subject: [PATCH] input: Add new sun4i-lradc-keys driver
5
6 Allwinnner sunxi SoCs have a low resolution adc (called lradc) which is
7 specifically designed to have various (tablet) keys (ie home, back, search,
8 etc). attached to it using a resistor network. This adds a driver for this.
9
10 There are 2 channels, currently this driver only supports chan0 since there
11 are no boards known to use chan1.
12
13 This has been tested on an olimex a10s-olinuxino-micro, a13-olinuxino, and
14 a20-olinuxino-micro.
15
16 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
17 --
18 Changes in v2:
19 -Change devicetree bindings to use a per key subnode, like gpio-keys does
20 ---
21 .../devicetree/bindings/input/sun4i-lradc-keys.txt | 62 +++++
22 MAINTAINERS | 7 +
23 drivers/input/keyboard/Kconfig | 10 +
24 drivers/input/keyboard/Makefile | 1 +
25 drivers/input/keyboard/sun4i-lradc-keys.c | 258 +++++++++++++++++++++
26 5 files changed, 338 insertions(+)
27 create mode 100644 Documentation/devicetree/bindings/input/sun4i-lradc-keys.txt
28 create mode 100644 drivers/input/keyboard/sun4i-lradc-keys.c
29
30 diff --git a/Documentation/devicetree/bindings/input/sun4i-lradc-keys.txt b/Documentation/devicetree/bindings/input/sun4i-lradc-keys.txt
31 new file mode 100644
32 index 0000000..b9c32f6
33 --- /dev/null
34 +++ b/Documentation/devicetree/bindings/input/sun4i-lradc-keys.txt
35 @@ -0,0 +1,62 @@
36 +Allwinner sun4i low res adc attached tablet keys
37 +------------------------------------------------
38 +
39 +Required properties:
40 + - compatible: "allwinner,sun4i-a10-lradc-keys"
41 + - reg: mmio address range of the chip
42 + - interrupts: interrupt to which the chip is connected
43 + - vref-supply: powersupply for the lradc reference voltage
44 +
45 +Each key is represented as a sub-node of "allwinner,sun4i-a10-lradc-keys":
46 +
47 +Required subnode-properties:
48 + - label: Descriptive name of the key.
49 + - linux,code: Keycode to emit.
50 + - channel: Channel this key is attached to, mut be 0 or 1.
51 + - voltage: Voltage in µV at lradc input when this key is pressed.
52 +
53 +Example:
54 +
55 +#include <dt-bindings/input/input.h>
56 +
57 + lradc: lradc@01c22800 {
58 + compatible = "allwinner,sun4i-a10-lradc-keys";
59 + reg = <0x01c22800 0x100>;
60 + interrupts = <31>;
61 + vref-supply = <&reg_vcc3v0>;
62 +
63 + button@191 {
64 + label = "Volume Up";
65 + linux,code = <KEY_VOLUMEUP>;
66 + channel = <0>;
67 + voltage = <191274>;
68 + };
69 +
70 + button@392 {
71 + label = "Volume Down";
72 + linux,code = <KEY_VOLUMEDOWN>;
73 + channel = <0>;
74 + voltage = <392644>;
75 + };
76 +
77 + button@601 {
78 + label = "Menu";
79 + linux,code = <KEY_MENU>;
80 + channel = <0>;
81 + voltage = <601151>;
82 + };
83 +
84 + button@795 {
85 + label = "Enter";
86 + linux,code = <KEY_ENTER>;
87 + channel = <0>;
88 + voltage = <795090>;
89 + };
90 +
91 + button@987 {
92 + label = "Home";
93 + linux,code = <KEY_HOMEPAGE>;
94 + channel = <0>;
95 + voltage = <987387>;
96 + };
97 + };
98 diff --git a/MAINTAINERS b/MAINTAINERS
99 index a20df9b..73d1aef 100644
100 --- a/MAINTAINERS
101 +++ b/MAINTAINERS
102 @@ -8932,6 +8932,13 @@ F: arch/m68k/sun3*/
103 F: arch/m68k/include/asm/sun3*
104 F: drivers/net/ethernet/i825xx/sun3*
105
106 +SUN4I LOW RES ADC ATTACHED TABLET KEYS DRIVER
107 +M: Hans de Goede <hdegoede@redhat.com>
108 +L: linux-input@vger.kernel.org
109 +S: Maintained
110 +F: Documentation/devicetree/bindings/input/sun4i-lradc-keys.txt
111 +F: drivers/input/keyboard/sun4i-lradc-keys.c
112 +
113 SUNDANCE NETWORK DRIVER
114 M: Denis Kirjanov <kda@linux-powerpc.org>
115 L: netdev@vger.kernel.org
116 diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
117 index a3958c6..2d11b44 100644
118 --- a/drivers/input/keyboard/Kconfig
119 +++ b/drivers/input/keyboard/Kconfig
120 @@ -567,6 +567,16 @@ config KEYBOARD_STMPE
121 To compile this driver as a module, choose M here: the module will be
122 called stmpe-keypad.
123
124 +config KEYBOARD_SUN4I_LRADC
125 + tristate "Allwinner sun4i low res adc attached tablet keys support"
126 + depends on ARCH_SUNXI
127 + help
128 + This selects support for the Allwinner low res adc attached tablet
129 + keys found on Allwinner sunxi SoCs.
130 +
131 + To compile this driver as a module, choose M here: the
132 + module will be called sun4i-lradc-keys.
133 +
134 config KEYBOARD_DAVINCI
135 tristate "TI DaVinci Key Scan"
136 depends on ARCH_DAVINCI_DM365
137 diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
138 index 0a33456..a35269a 100644
139 --- a/drivers/input/keyboard/Makefile
140 +++ b/drivers/input/keyboard/Makefile
141 @@ -53,6 +53,7 @@ obj-$(CONFIG_KEYBOARD_SPEAR) += spear-keyboard.o
142 obj-$(CONFIG_KEYBOARD_STMPE) += stmpe-keypad.o
143 obj-$(CONFIG_KEYBOARD_STOWAWAY) += stowaway.o
144 obj-$(CONFIG_KEYBOARD_ST_KEYSCAN) += st-keyscan.o
145 +obj-$(CONFIG_KEYBOARD_SUN4I_LRADC) += sun4i-lradc-keys.o
146 obj-$(CONFIG_KEYBOARD_SUNKBD) += sunkbd.o
147 obj-$(CONFIG_KEYBOARD_TC3589X) += tc3589x-keypad.o
148 obj-$(CONFIG_KEYBOARD_TEGRA) += tegra-kbc.o
149 diff --git a/drivers/input/keyboard/sun4i-lradc-keys.c b/drivers/input/keyboard/sun4i-lradc-keys.c
150 new file mode 100644
151 index 0000000..f11f002
152 --- /dev/null
153 +++ b/drivers/input/keyboard/sun4i-lradc-keys.c
154 @@ -0,0 +1,258 @@
155 +/*
156 + * Allwinner sun4i low res adc attached tablet keys driver
157 + *
158 + * Copyright (C) 2014 Hans de Goede <hdegoede@redhat.com>
159 + *
160 + * This program is free software; you can redistribute it and/or modify
161 + * it under the terms of the GNU General Public License as published by
162 + * the Free Software Foundation; either version 2 of the License, or
163 + * (at your option) any later version.
164 + *
165 + * This program is distributed in the hope that it will be useful,
166 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
167 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
168 + * GNU General Public License for more details.
169 + */
170 +
171 +/*
172 + * Allwinnner sunxi SoCs have a lradc which is specifically designed to have
173 + * various (tablet) keys (ie home, back, search, etc). attached to it using
174 + * a resistor network. This driver is for the keys on such boards.
175 + *
176 + * There are 2 channels, currently this driver only supports channel 0 since
177 + * there are no boards known to use channel 1.
178 + */
179 +
180 +#include <linux/err.h>
181 +#include <linux/init.h>
182 +#include <linux/input.h>
183 +#include <linux/interrupt.h>
184 +#include <linux/io.h>
185 +#include <linux/module.h>
186 +#include <linux/of_platform.h>
187 +#include <linux/platform_device.h>
188 +#include <linux/regulator/consumer.h>
189 +#include <linux/slab.h>
190 +
191 +#define LRADC_CTRL 0x00
192 +#define LRADC_INTC 0x04
193 +#define LRADC_INTS 0x08
194 +#define LRADC_DATA0 0x0c
195 +#define LRADC_DATA1 0x10
196 +
197 +/* LRADC_CTRL bits */
198 +#define FIRST_CONVERT_DLY(x) ((x) << 24) /* 8 bits */
199 +#define CHAN_SELECT(x) ((x) << 22) /* 2 bits */
200 +#define CONTINUE_TIME_SEL(x) ((x) << 16) /* 4 bits */
201 +#define KEY_MODE_SEL(x) ((x) << 12) /* 2 bits */
202 +#define LEVELA_B_CNT(x) ((x) << 8) /* 4 bits */
203 +#define HOLD_EN(x) ((x) << 6)
204 +#define LEVELB_VOL(x) ((x) << 4) /* 2 bits */
205 +#define SAMPLE_RATE(x) ((x) << 2) /* 2 bits */
206 +#define ENABLE(x) ((x) << 0)
207 +
208 +/* LRADC_INTC and LRADC_INTS bits */
209 +#define CHAN1_KEYUP_IRQ BIT(12)
210 +#define CHAN1_ALRDY_HOLD_IRQ BIT(11)
211 +#define CHAN1_HOLD_IRQ BIT(10)
212 +#define CHAN1_KEYDOWN_IRQ BIT(9)
213 +#define CHAN1_DATA_IRQ BIT(8)
214 +#define CHAN0_KEYUP_IRQ BIT(4)
215 +#define CHAN0_ALRDY_HOLD_IRQ BIT(3)
216 +#define CHAN0_HOLD_IRQ BIT(2)
217 +#define CHAN0_KEYDOWN_IRQ BIT(1)
218 +#define CHAN0_DATA_IRQ BIT(0)
219 +
220 +struct sun4i_lradc_keymap {
221 + u32 voltage;
222 + u32 keycode;
223 +};
224 +
225 +struct sun4i_lradc_data {
226 + struct device *dev;
227 + struct input_dev *input;
228 + void __iomem *base;
229 + struct regulator *vref_supply;
230 + struct sun4i_lradc_keymap *chan0_map;
231 + u32 chan0_map_count;
232 + u32 chan0_keycode;
233 + u32 vref;
234 +};
235 +
236 +static irqreturn_t sun4i_lradc_irq(int irq, void *dev_id)
237 +{
238 + struct sun4i_lradc_data *lradc = dev_id;
239 + u32 i, ints, val, voltage, diff, keycode = 0, closest = 0xffffffff;
240 +
241 + ints = readl(lradc->base + LRADC_INTS);
242 +
243 + /*
244 + * lradc supports only one keypress at a time, release does not give
245 + * any info as to which key was released, so we cache the keycode.
246 + */
247 + if ((ints & CHAN0_KEYDOWN_IRQ) && lradc->chan0_keycode == 0) {
248 + val = readl(lradc->base + LRADC_DATA0) & 0x3f;
249 + voltage = val * lradc->vref / 63;
250 +
251 + for (i = 0; i < lradc->chan0_map_count; i++) {
252 + diff = abs(lradc->chan0_map[i].voltage - voltage);
253 + if (diff < closest) {
254 + closest = diff;
255 + keycode = lradc->chan0_map[i].keycode;
256 + }
257 + }
258 +
259 + lradc->chan0_keycode = keycode;
260 + input_report_key(lradc->input, lradc->chan0_keycode, 1);
261 + }
262 +
263 + if (ints & CHAN0_KEYUP_IRQ) {
264 + input_report_key(lradc->input, lradc->chan0_keycode, 0);
265 + lradc->chan0_keycode = 0;
266 + }
267 +
268 + input_sync(lradc->input);
269 +
270 + writel(ints, lradc->base + LRADC_INTS);
271 +
272 + return IRQ_HANDLED;
273 +}
274 +
275 +static int sun4i_lradc_open(struct input_dev *dev)
276 +{
277 + struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
278 + int ret;
279 +
280 + ret = regulator_enable(lradc->vref_supply);
281 + if (ret)
282 + return ret;
283 +
284 + /* lradc Vref internally is divided by 2/3 */
285 + lradc->vref = regulator_get_voltage(lradc->vref_supply) * 2 / 3;
286 +
287 + /*
288 + * Set sample time to 4 ms / 250 Hz. Wait 2 * 4 ms for key to
289 + * stabilize on press, wait (1 + 1) * 4 ms for key release
290 + */
291 + writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
292 + SAMPLE_RATE(0) | ENABLE(1), lradc->base + LRADC_CTRL);
293 +
294 + writel(CHAN0_KEYUP_IRQ | CHAN0_KEYDOWN_IRQ, lradc->base + LRADC_INTC);
295 +
296 + return 0;
297 +}
298 +
299 +static void sun4i_lradc_close(struct input_dev *dev)
300 +{
301 + struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
302 +
303 + /* Disable lradc, leave other settings unchanged */
304 + writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
305 + SAMPLE_RATE(2), lradc->base + LRADC_CTRL);
306 + writel(0, lradc->base + LRADC_INTC);
307 +
308 + regulator_disable(lradc->vref_supply);
309 +}
310 +
311 +static int sun4i_lradc_probe(struct platform_device *pdev)
312 +{
313 + struct sun4i_lradc_data *lradc;
314 + struct device *dev = &pdev->dev;
315 + struct device_node *pp, *np = dev->of_node;
316 + u32 channel;
317 + int i, ret;
318 +
319 + lradc = devm_kzalloc(dev, sizeof(struct sun4i_lradc_data), GFP_KERNEL);
320 + if (!lradc)
321 + return -ENOMEM;
322 +
323 + lradc->chan0_map_count = of_get_child_count(np);
324 + lradc->chan0_map = devm_kmalloc(dev, lradc->chan0_map_count *
325 + sizeof(struct sun4i_lradc_keymap), GFP_KERNEL);
326 + if (!lradc->chan0_map)
327 + return -ENOMEM;
328 +
329 + i = 0;
330 + for_each_child_of_node(np, pp) {
331 + struct sun4i_lradc_keymap *map = &lradc->chan0_map[i];
332 +
333 + ret = of_property_read_u32(pp, "channel", &channel);
334 + if (ret || channel != 0) {
335 + dev_err(dev, "%s: Inval channel prop\n", pp->name);
336 + return -EINVAL;
337 + }
338 +
339 + ret = of_property_read_u32(pp, "voltage", &map->voltage);
340 + if (ret) {
341 + dev_err(dev, "%s: Inval voltage prop\n", pp->name);
342 + return -EINVAL;
343 + }
344 +
345 + ret = of_property_read_u32(pp, "linux,code", &map->keycode);
346 + if (ret) {
347 + dev_err(dev, "%s: Inval linux,code prop\n", pp->name);
348 + return -EINVAL;
349 + }
350 +
351 + i++;
352 + }
353 +
354 + lradc->vref_supply = devm_regulator_get(dev, "vref");
355 + if (IS_ERR(lradc->vref_supply))
356 + return PTR_ERR(lradc->vref_supply);
357 +
358 + lradc->dev = dev;
359 + lradc->input = devm_input_allocate_device(dev);
360 + if (!lradc->input)
361 + return -ENOMEM;
362 +
363 + lradc->input->name = pdev->name;
364 + lradc->input->phys = "sun4i_lradc/input0";
365 + lradc->input->open = sun4i_lradc_open;
366 + lradc->input->close = sun4i_lradc_close;
367 + lradc->input->id.bustype = BUS_HOST;
368 + lradc->input->id.vendor = 0x0001;
369 + lradc->input->id.product = 0x0001;
370 + lradc->input->id.version = 0x0100;
371 + lradc->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY);
372 + for (i = 0; i < lradc->chan0_map_count; i++)
373 + set_bit(lradc->chan0_map[i].keycode, lradc->input->keybit);
374 + input_set_drvdata(lradc->input, lradc);
375 +
376 + lradc->base = devm_ioremap_resource(dev,
377 + platform_get_resource(pdev, IORESOURCE_MEM, 0));
378 + if (IS_ERR(lradc->base))
379 + return PTR_ERR(lradc->base);
380 +
381 + ret = devm_request_irq(dev, platform_get_irq(pdev, 0), sun4i_lradc_irq,
382 + 0, "sun4i-a10-lradc-keys", lradc);
383 + if (ret)
384 + return ret;
385 +
386 + ret = input_register_device(lradc->input);
387 + if (ret)
388 + return ret;
389 +
390 + platform_set_drvdata(pdev, lradc);
391 + return 0;
392 +}
393 +
394 +static const struct of_device_id sun4i_lradc_of_match[] = {
395 + { .compatible = "allwinner,sun4i-a10-lradc-keys", },
396 + { /* sentinel */ }
397 +};
398 +MODULE_DEVICE_TABLE(of, sun4i_lradc_of_match);
399 +
400 +static struct platform_driver sun4i_lradc_driver = {
401 + .driver = {
402 + .name = "sun4i-a10-lradc-keys",
403 + .of_match_table = of_match_ptr(sun4i_lradc_of_match),
404 + },
405 + .probe = sun4i_lradc_probe,
406 +};
407 +
408 +module_platform_driver(sun4i_lradc_driver);
409 +
410 +MODULE_DESCRIPTION("Allwinner sun4i low res adc attached tablet keys driver");
411 +MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
412 +MODULE_LICENSE("GPL");