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
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.
10 There are 2 channels, currently this driver only supports chan0 since there
11 are no boards known to use chan1.
13 This has been tested on an olimex a10s-olinuxino-micro, a13-olinuxino, and
16 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
19 -Change devicetree bindings to use a per key subnode, like gpio-keys does
21 .../devicetree/bindings/input/sun4i-lradc-keys.txt | 62 +++++
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
31 +++ b/Documentation/devicetree/bindings/input/sun4i-lradc-keys.txt
33 +Allwinner sun4i low res adc attached tablet keys
34 +------------------------------------------------
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
42 +Each key is represented as a sub-node of "allwinner,sun4i-a10-lradc-keys":
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.
52 +#include <dt-bindings/input/input.h>
54 + lradc: lradc@01c22800 {
55 + compatible = "allwinner,sun4i-a10-lradc-keys";
56 + reg = <0x01c22800 0x100>;
58 + vref-supply = <®_vcc3v0>;
61 + label = "Volume Up";
62 + linux,code = <KEY_VOLUMEUP>;
68 + label = "Volume Down";
69 + linux,code = <KEY_VOLUMEDOWN>;
76 + linux,code = <KEY_MENU>;
83 + linux,code = <KEY_ENTER>;
90 + linux,code = <KEY_HOMEPAGE>;
97 @@ -9015,6 +9015,13 @@ F: arch/m68k/sun3*/
98 F: arch/m68k/include/asm/sun3*
99 F: drivers/net/ethernet/i825xx/sun3*
101 +SUN4I LOW RES ADC ATTACHED TABLET KEYS DRIVER
102 +M: Hans de Goede <hdegoede@redhat.com>
103 +L: linux-input@vger.kernel.org
105 +F: Documentation/devicetree/bindings/input/sun4i-lradc-keys.txt
106 +F: drivers/input/keyboard/sun4i-lradc-keys.c
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
117 +config KEYBOARD_SUN4I_LRADC
118 + tristate "Allwinner sun4i low res adc attached tablet keys support"
119 + depends on ARCH_SUNXI
121 + This selects support for the Allwinner low res adc attached tablet
122 + keys found on Allwinner sunxi SoCs.
124 + To compile this driver as a module, choose M here: the
125 + module will be called sun4i-lradc-keys.
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
141 +++ b/drivers/input/keyboard/sun4i-lradc-keys.c
144 + * Allwinner sun4i low res adc attached tablet keys driver
146 + * Copyright (C) 2014 Hans de Goede <hdegoede@redhat.com>
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.
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.
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.
164 + * There are 2 channels, currently this driver only supports channel 0 since
165 + * there are no boards known to use channel 1.
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>
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
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)
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)
208 +struct sun4i_lradc_keymap {
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;
224 +static irqreturn_t sun4i_lradc_irq(int irq, void *dev_id)
226 + struct sun4i_lradc_data *lradc = dev_id;
227 + u32 i, ints, val, voltage, diff, keycode = 0, closest = 0xffffffff;
229 + ints = readl(lradc->base + LRADC_INTS);
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.
235 + if ((ints & CHAN0_KEYDOWN_IRQ) && lradc->chan0_keycode == 0) {
236 + val = readl(lradc->base + LRADC_DATA0) & 0x3f;
237 + voltage = val * lradc->vref / 63;
239 + for (i = 0; i < lradc->chan0_map_count; i++) {
240 + diff = abs(lradc->chan0_map[i].voltage - voltage);
241 + if (diff < closest) {
243 + keycode = lradc->chan0_map[i].keycode;
247 + lradc->chan0_keycode = keycode;
248 + input_report_key(lradc->input, lradc->chan0_keycode, 1);
251 + if (ints & CHAN0_KEYUP_IRQ) {
252 + input_report_key(lradc->input, lradc->chan0_keycode, 0);
253 + lradc->chan0_keycode = 0;
256 + input_sync(lradc->input);
258 + writel(ints, lradc->base + LRADC_INTS);
260 + return IRQ_HANDLED;
263 +static int sun4i_lradc_open(struct input_dev *dev)
265 + struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
268 + ret = regulator_enable(lradc->vref_supply);
272 + /* lradc Vref internally is divided by 2/3 */
273 + lradc->vref = regulator_get_voltage(lradc->vref_supply) * 2 / 3;
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
279 + writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
280 + SAMPLE_RATE(0) | ENABLE(1), lradc->base + LRADC_CTRL);
282 + writel(CHAN0_KEYUP_IRQ | CHAN0_KEYDOWN_IRQ, lradc->base + LRADC_INTC);
287 +static void sun4i_lradc_close(struct input_dev *dev)
289 + struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
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);
296 + regulator_disable(lradc->vref_supply);
299 +static int sun4i_lradc_probe(struct platform_device *pdev)
301 + struct sun4i_lradc_data *lradc;
302 + struct device *dev = &pdev->dev;
303 + struct device_node *pp, *np = dev->of_node;
307 + lradc = devm_kzalloc(dev, sizeof(struct sun4i_lradc_data), GFP_KERNEL);
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)
318 + for_each_child_of_node(np, pp) {
319 + struct sun4i_lradc_keymap *map = &lradc->chan0_map[i];
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);
327 + ret = of_property_read_u32(pp, "voltage", &map->voltage);
329 + dev_err(dev, "%s: Inval voltage prop\n", pp->name);
333 + ret = of_property_read_u32(pp, "linux,code", &map->keycode);
335 + dev_err(dev, "%s: Inval linux,code prop\n", pp->name);
342 + lradc->vref_supply = devm_regulator_get(dev, "vref");
343 + if (IS_ERR(lradc->vref_supply))
344 + return PTR_ERR(lradc->vref_supply);
347 + lradc->input = devm_input_allocate_device(dev);
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);
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);
369 + ret = devm_request_irq(dev, platform_get_irq(pdev, 0), sun4i_lradc_irq,
370 + 0, "sun4i-a10-lradc-keys", lradc);
374 + ret = input_register_device(lradc->input);
378 + platform_set_drvdata(pdev, lradc);
382 +static const struct of_device_id sun4i_lradc_of_match[] = {
383 + { .compatible = "allwinner,sun4i-a10-lradc-keys", },
386 +MODULE_DEVICE_TABLE(of, sun4i_lradc_of_match);
388 +static struct platform_driver sun4i_lradc_driver = {
390 + .name = "sun4i-a10-lradc-keys",
391 + .of_match_table = of_match_ptr(sun4i_lradc_of_match),
393 + .probe = sun4i_lradc_probe,
396 +module_platform_driver(sun4i_lradc_driver);
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");