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