kernel: update 3.14 to 3.14.18
[openwrt/svn-archive/archive.git] / target / linux / sunxi / patches-3.14 / 172-input-add-lradc-keys-driver.patch
1 From 3f8fd9b9e2daefb7be4c46369f86af1c7bb2f1ca 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. The devicetree properties are already
12 prefixed with chan0 as preparation for chan1 support in the future.
13
14 This has been tested on an olimex a10s-olinuxino-micro, a13-olinuxino, and
15 a20-olinuxino-micro.
16
17 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
18 ---
19 .../devicetree/bindings/input/sun4i-lradc-keys.txt | 22 ++
20 drivers/input/keyboard/Kconfig | 10 +
21 drivers/input/keyboard/Makefile | 1 +
22 drivers/input/keyboard/sun4i-lradc-keys.c | 243 +++++++++++++++++++++
23 4 files changed, 276 insertions(+)
24 create mode 100644 Documentation/devicetree/bindings/input/sun4i-lradc-keys.txt
25 create mode 100644 drivers/input/keyboard/sun4i-lradc-keys.c
26
27 --- /dev/null
28 +++ b/Documentation/devicetree/bindings/input/sun4i-lradc-keys.txt
29 @@ -0,0 +1,22 @@
30 +Allwinner sun4i low res adc attached tablet keys
31 +------------------------------------------------
32 +
33 +Required properties:
34 + - compatible: "allwinner,sun4i-lradc-keys"
35 + - reg: mmio address range of the chip
36 + - interrupts: interrupt to which the chip is connected
37 + - allwinner,chan0-step: step in mV between keys must be 150 or 200
38 + - linux,chan0-keycodes: array of dt-bindings/input/input.h KEY_ codes
39 +
40 +Example:
41 +
42 +#include <dt-bindings/input/input.h>
43 +
44 + lradc: lradc@01c22800 {
45 + compatible = "allwinner,sun4i-lradc-keys";
46 + reg = <0x01c22800 0x100>;
47 + interrupts = <31>;
48 + allwinner,chan0-step = <200>;
49 + linux,chan0-keycodes = <KEY_VOLUMEUP KEY_VOLUMEDOWN
50 + KEY_MENU KEY_ENTER KEY_HOME>;
51 + };
52 --- a/drivers/input/keyboard/Kconfig
53 +++ b/drivers/input/keyboard/Kconfig
54 @@ -544,6 +544,16 @@ config KEYBOARD_STMPE
55 To compile this driver as a module, choose M here: the module will be
56 called stmpe-keypad.
57
58 +config KEYBOARD_SUN4I_LRADC
59 + tristate "Allwinner sun4i low res adc attached tablet keys support"
60 + depends on ARCH_SUNXI
61 + help
62 + This selects support for the Allwinner low res adc attached tablet
63 + keys found on Allwinner sunxi SoCs.
64 +
65 + To compile this driver as a module, choose M here: the
66 + module will be called sun4i-lradc-keys.
67 +
68 config KEYBOARD_DAVINCI
69 tristate "TI DaVinci Key Scan"
70 depends on ARCH_DAVINCI_DM365
71 --- a/drivers/input/keyboard/Makefile
72 +++ b/drivers/input/keyboard/Makefile
73 @@ -50,6 +50,7 @@ obj-$(CONFIG_KEYBOARD_SH_KEYSC) += sh_k
74 obj-$(CONFIG_KEYBOARD_SPEAR) += spear-keyboard.o
75 obj-$(CONFIG_KEYBOARD_STMPE) += stmpe-keypad.o
76 obj-$(CONFIG_KEYBOARD_STOWAWAY) += stowaway.o
77 +obj-$(CONFIG_KEYBOARD_SUN4I_LRADC) += sun4i-lradc-keys.o
78 obj-$(CONFIG_KEYBOARD_SUNKBD) += sunkbd.o
79 obj-$(CONFIG_KEYBOARD_TC3589X) += tc3589x-keypad.o
80 obj-$(CONFIG_KEYBOARD_TEGRA) += tegra-kbc.o
81 --- /dev/null
82 +++ b/drivers/input/keyboard/sun4i-lradc-keys.c
83 @@ -0,0 +1,243 @@
84 +/*
85 + * Allwinner sun4i low res adc attached tablet keys driver
86 + *
87 + * Copyright (C) 2014 Hans de Goede <hdegoede@redhat.com>
88 + *
89 + * This program is free software; you can redistribute it and/or modify
90 + * it under the terms of the GNU General Public License as published by
91 + * the Free Software Foundation; either version 2 of the License, or
92 + * (at your option) any later version.
93 + *
94 + * This program is distributed in the hope that it will be useful,
95 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
96 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
97 + * GNU General Public License for more details.
98 + */
99 +
100 +/*
101 + * Allwinnner sunxi SoCs have a lradc which is specifically designed to have
102 + * various (tablet) keys (ie home, back, search, etc). attached to it using
103 + * a resistor network. This driver is for the keys on such boards.
104 + *
105 + * There are 2 channels, currently this driver only supports chan0 since there
106 + * are no boards known to use chan1. The devicetree properties are already
107 + * prefixed with chan0 as preparation for chan1 support in the future.
108 + */
109 +
110 +#include <linux/err.h>
111 +#include <linux/init.h>
112 +#include <linux/input.h>
113 +#include <linux/interrupt.h>
114 +#include <linux/io.h>
115 +#include <linux/module.h>
116 +#include <linux/of_platform.h>
117 +#include <linux/platform_device.h>
118 +#include <linux/slab.h>
119 +
120 +#define LRADC_CTRL 0x00
121 +#define LRADC_INTC 0x04
122 +#define LRADC_INTS 0x08
123 +#define LRADC_DATA0 0x0c
124 +#define LRADC_DATA1 0x10
125 +
126 +/* LRADC_CTRL bits */
127 +#define FIRST_CONVERT_DLY(x) ((x) << 24) /* 8 bits */
128 +#define CHAN_SELECT(x) ((x) << 22) /* 2 bits */
129 +#define CONTINUE_TIME_SEL(x) ((x) << 16) /* 4 bits */
130 +#define KEY_MODE_SEL(x) ((x) << 12) /* 2 bits */
131 +#define LEVELA_B_CNT(x) ((x) << 8) /* 4 bits */
132 +#define HOLD_EN(x) ((x) << 6)
133 +#define LEVELB_VOL(x) ((x) << 4) /* 2 bits */
134 +#define SAMPLE_RATE(x) ((x) << 2) /* 2 bits */
135 +#define ENABLE(x) ((x) << 0)
136 +
137 +/* LRADC_INTC and LRADC_INTS bits */
138 +#define CHAN1_KEYUP_IRQ BIT(12)
139 +#define CHAN1_ALRDY_HOLD_IRQ BIT(11)
140 +#define CHAN1_HOLD_IRQ BIT(10)
141 +#define CHAN1_KEYDOWN_IRQ BIT(9)
142 +#define CHAN1_DATA_IRQ BIT(8)
143 +#define CHAN0_KEYUP_IRQ BIT(4)
144 +#define CHAN0_ALRDY_HOLD_IRQ BIT(3)
145 +#define CHAN0_HOLD_IRQ BIT(2)
146 +#define CHAN0_KEYDOWN_IRQ BIT(1)
147 +#define CHAN0_DATA_IRQ BIT(0)
148 +
149 +#define MAX_KEYS 13
150 +
151 +/* Lookup table to map the adc val to a keycode index for 150 mv step size */
152 +static const u8 adc_val_to_key_index_step150[64] = {
153 + 0, 0, 0,
154 + 1, 1, 1, 1, 1,
155 + 2, 2, 2, 2, 2,
156 + 3, 3, 3, 3,
157 + 4, 4, 4, 4, 4,
158 + 5, 5, 5, 5, 5,
159 + 6, 6, 6, 6, 6,
160 + 7, 7, 7, 7,
161 + 8, 8, 8, 8, 8,
162 + 9, 9, 9, 9, 9,
163 + 10, 10, 10, 10,
164 + 11, 11, 11, 11,
165 + 12, 12, 12, 12, 12, 12, 12, 12, 12, 12
166 +};
167 +
168 +/* Lookup table to map the adc val to a keycode index for 200 mv step size */
169 +static const u8 adc_val_to_key_index_step200[64] = {
170 + 0, 0, 0, 0, 0, 0, 0, 0,
171 + 1, 1, 1, 1, 1, 1, 1,
172 + 2, 2, 2, 2, 2, 2, 2,
173 + 3, 3, 3, 3, 3, 3,
174 + 4, 4, 4, 4, 4, 4,
175 + 5, 5, 5, 5, 5, 5,
176 + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
177 + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
178 +};
179 +
180 +struct sun4i_lradc_data {
181 + struct device *dev;
182 + struct input_dev *input;
183 + void __iomem *base;
184 + u32 chan0_step;
185 + u32 chan0_keycode;
186 + u32 chan0_keycodes[MAX_KEYS];
187 +};
188 +
189 +static irqreturn_t sun4i_lradc_irq(int irq, void *dev_id)
190 +{
191 + struct sun4i_lradc_data *lradc = dev_id;
192 + u32 ints, val;
193 +
194 + ints = readl(lradc->base + LRADC_INTS);
195 +
196 + /*
197 + * lradc supports only one keypress at a time, release does not give
198 + * any info as to which key was released, so we cache the keycode.
199 + */
200 + if ((ints & CHAN0_KEYDOWN_IRQ) && lradc->chan0_keycode == 0) {
201 + val = readl(lradc->base + LRADC_DATA0);
202 + if (lradc->chan0_step == 150)
203 + val = adc_val_to_key_index_step150[val];
204 + else
205 + val = adc_val_to_key_index_step200[val];
206 +
207 + lradc->chan0_keycode = lradc->chan0_keycodes[val];
208 + input_report_key(lradc->input, lradc->chan0_keycode, 1);
209 + }
210 +
211 + if (ints & CHAN0_KEYUP_IRQ) {
212 + input_report_key(lradc->input, lradc->chan0_keycode, 0);
213 + lradc->chan0_keycode = 0;
214 + }
215 +
216 + input_sync(lradc->input);
217 +
218 + writel(ints, lradc->base + LRADC_INTS);
219 +
220 + return IRQ_HANDLED;
221 +}
222 +
223 +static int sun4i_lradc_open(struct input_dev *dev)
224 +{
225 + struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
226 +
227 + /*
228 + * Set sample time to 16 ms / 62.5 Hz. Wait 2 * 16 ms for key to
229 + * stabilize on press, wait (1 + 1) * 16 ms for key release
230 + */
231 + writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
232 + SAMPLE_RATE(2) | ENABLE(1), lradc->base + LRADC_CTRL);
233 +
234 + writel(CHAN0_KEYUP_IRQ | CHAN0_KEYDOWN_IRQ, lradc->base + LRADC_INTC);
235 +
236 + return 0;
237 +}
238 +
239 +static void sun4i_lradc_close(struct input_dev *dev)
240 +{
241 + struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
242 +
243 + /* Disable lradc, leave other settings unchanged */
244 + writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
245 + SAMPLE_RATE(2), lradc->base + LRADC_CTRL);
246 + writel(0, lradc->base + LRADC_INTC);
247 +}
248 +
249 +static int sun4i_lradc_probe(struct platform_device *pdev)
250 +{
251 + struct sun4i_lradc_data *lradc;
252 + struct device *dev = &pdev->dev;
253 + struct device_node *np = dev->of_node;
254 + int i, ret;
255 +
256 + lradc = devm_kzalloc(dev, sizeof(struct sun4i_lradc_data), GFP_KERNEL);
257 + if (!lradc)
258 + return -ENOMEM;
259 +
260 + ret = of_property_read_u32(np, "allwinner,chan0-step",
261 + &lradc->chan0_step);
262 + if (ret || (lradc->chan0_step != 150 && lradc->chan0_step != 200)) {
263 + dev_err(dev, "Invalid allwinner,chan0-step dt-property\n");
264 + return -EINVAL;
265 + }
266 +
267 + for (i = 0; i < MAX_KEYS; i++)
268 + of_property_read_u32_index(np, "linux,chan0-keycodes",
269 + i, &lradc->chan0_keycodes[i]);
270 +
271 + lradc->dev = dev;
272 + lradc->input = devm_input_allocate_device(dev);
273 + if (!lradc->input)
274 + return -ENOMEM;
275 +
276 + lradc->input->name = pdev->name;
277 + lradc->input->phys = "sun4i_lradc/input0";
278 + lradc->input->open = sun4i_lradc_open;
279 + lradc->input->close = sun4i_lradc_close;
280 + lradc->input->id.bustype = BUS_HOST;
281 + lradc->input->id.vendor = 0x0001;
282 + lradc->input->id.product = 0x0001;
283 + lradc->input->id.version = 0x0100;
284 + lradc->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY);
285 + for (i = 0; i < MAX_KEYS; i++)
286 + set_bit(lradc->chan0_keycodes[i], lradc->input->keybit);
287 + input_set_drvdata(lradc->input, lradc);
288 +
289 + lradc->base = devm_ioremap_resource(dev,
290 + platform_get_resource(pdev, IORESOURCE_MEM, 0));
291 + if (IS_ERR(lradc->base))
292 + return PTR_ERR(lradc->base);
293 +
294 + ret = devm_request_irq(dev, platform_get_irq(pdev, 0), sun4i_lradc_irq,
295 + 0, "sun4i-lradc-keys", lradc);
296 + if (ret)
297 + return ret;
298 +
299 + ret = input_register_device(lradc->input);
300 + if (ret)
301 + return ret;
302 +
303 + platform_set_drvdata(pdev, lradc);
304 + return 0;
305 +}
306 +
307 +static const struct of_device_id sun4i_lradc_of_match[] = {
308 + { .compatible = "allwinner,sun4i-lradc-keys", },
309 + { /* sentinel */ }
310 +};
311 +MODULE_DEVICE_TABLE(of, sun4i_lradc_of_match);
312 +
313 +static struct platform_driver sun4i_lradc_driver = {
314 + .driver = {
315 + .owner = THIS_MODULE,
316 + .name = "sun4i-lradc-keys",
317 + .of_match_table = of_match_ptr(sun4i_lradc_of_match),
318 + },
319 + .probe = sun4i_lradc_probe,
320 +};
321 +
322 +module_platform_driver(sun4i_lradc_driver);
323 +
324 +MODULE_DESCRIPTION("Allwinner sun4i low res adc attached tablet keys driver");
325 +MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
326 +MODULE_LICENSE("GPL");