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