kernel: update 3.14 to 3.14.18
[openwrt/openwrt.git] / target / linux / sunxi / patches-3.14 / 211-input-add-axp20x-power-enable-key-support.patch
1 From 656b9ff9781aa3ffeb4231f32dfc545c92d04a12 Mon Sep 17 00:00:00 2001
2 From: Carlo Caione <carlo@caione.org>
3 Date: Sat, 1 Mar 2014 17:45:49 +0100
4 Subject: [PATCH] input: misc: Add driver for AXP20x Power Enable Key
5
6 This patch add support for the Power Enable Key found on MFD AXP202 and
7 AXP209. Besides the basic support for the button, the driver adds two
8 entries in sysfs to configure the time delay for power on/off.
9
10 Signed-off-by: Carlo Caione <carlo@caione.org>
11 ---
12 arch/arm/configs/sunxi_defconfig | 2 +
13 drivers/input/misc/Kconfig | 11 ++
14 drivers/input/misc/Makefile | 1 +
15 drivers/input/misc/axp20x-pek.c | 265 +++++++++++++++++++++++++++++++++++++++
16 4 files changed, 279 insertions(+)
17 create mode 100644 drivers/input/misc/axp20x-pek.c
18
19 --- a/arch/arm/configs/sunxi_defconfig
20 +++ b/arch/arm/configs/sunxi_defconfig
21 @@ -40,6 +40,8 @@ CONFIG_SUN4I_EMAC=y
22 # CONFIG_NET_VENDOR_STMICRO is not set
23 # CONFIG_NET_VENDOR_WIZNET is not set
24 # CONFIG_WLAN is not set
25 +CONFIG_INPUT_MISC=y
26 +CONFIG_INPUT_AXP20X_PEK=y
27 CONFIG_SERIAL_8250=y
28 CONFIG_SERIAL_8250_CONSOLE=y
29 CONFIG_SERIAL_8250_NR_UARTS=8
30 --- a/drivers/input/misc/Kconfig
31 +++ b/drivers/input/misc/Kconfig
32 @@ -393,6 +393,17 @@ config INPUT_RETU_PWRBUTTON
33 To compile this driver as a module, choose M here. The module will
34 be called retu-pwrbutton.
35
36 +config INPUT_AXP20X_PEK
37 + tristate "X-Powers AXP20X power button driver"
38 + depends on MFD_AXP20X
39 + help
40 + Say Y here if you want to enable power key reporting via the
41 + AXP20X PMIC.
42 +
43 + To compile this driver as a module, choose M here. The module will
44 + be called axp20x-pek.
45 +
46 +
47 config INPUT_TWL4030_PWRBUTTON
48 tristate "TWL4030 Power button Driver"
49 depends on TWL4030_CORE
50 --- a/drivers/input/misc/Makefile
51 +++ b/drivers/input/misc/Makefile
52 @@ -50,6 +50,7 @@ obj-$(CONFIG_INPUT_POWERMATE) += powerm
53 obj-$(CONFIG_INPUT_PWM_BEEPER) += pwm-beeper.o
54 obj-$(CONFIG_INPUT_RB532_BUTTON) += rb532_button.o
55 obj-$(CONFIG_INPUT_RETU_PWRBUTTON) += retu-pwrbutton.o
56 +obj-$(CONFIG_INPUT_AXP20X_PEK) += axp20x-pek.o
57 obj-$(CONFIG_INPUT_GPIO_ROTARY_ENCODER) += rotary_encoder.o
58 obj-$(CONFIG_INPUT_SGI_BTNS) += sgi_btns.o
59 obj-$(CONFIG_INPUT_SIRFSOC_ONKEY) += sirfsoc-onkey.o
60 --- /dev/null
61 +++ b/drivers/input/misc/axp20x-pek.c
62 @@ -0,0 +1,265 @@
63 +/*
64 + * axp20x power button driver.
65 + *
66 + * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
67 + *
68 + * This file is subject to the terms and conditions of the GNU General
69 + * Public License. See the file "COPYING" in the main directory of this
70 + * archive for more details.
71 + *
72 + * This program is distributed in the hope that it will be useful,
73 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
74 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75 + * GNU General Public License for more details.
76 + */
77 +
78 +#include <linux/errno.h>
79 +#include <linux/irq.h>
80 +#include <linux/init.h>
81 +#include <linux/input.h>
82 +#include <linux/interrupt.h>
83 +#include <linux/kernel.h>
84 +#include <linux/mfd/axp20x.h>
85 +#include <linux/module.h>
86 +#include <linux/platform_device.h>
87 +#include <linux/regmap.h>
88 +#include <linux/slab.h>
89 +
90 +#define AXP20X_PEK_STARTUP_MASK (0xc0)
91 +#define AXP20X_PEK_SHUTDOWN_MASK (0x03)
92 +
93 +static const char const *startup_time[] = { "128mS", "3S" , "1S", "2S" };
94 +static const char const *shutdown_time[] = { "4S", "6S" , "8S", "10S" };
95 +
96 +struct axp20x_pek {
97 + struct axp20x_dev *axp20x;
98 + struct input_dev *input;
99 + int irq_dbr;
100 + int irq_dbf;
101 +};
102 +
103 +struct axp20x_pek_ext_attr {
104 + const char const **str;
105 + unsigned int mask;
106 +};
107 +
108 +static struct axp20x_pek_ext_attr axp20x_pek_startup_ext_attr = {
109 + .str = startup_time,
110 + .mask = AXP20X_PEK_STARTUP_MASK,
111 +};
112 +
113 +static struct axp20x_pek_ext_attr axp20x_pek_shutdown_ext_attr = {
114 + .str = shutdown_time,
115 + .mask = AXP20X_PEK_SHUTDOWN_MASK,
116 +};
117 +
118 +static struct axp20x_pek_ext_attr *get_axp_ext_attr(struct device_attribute *attr)
119 +{
120 + return container_of(attr, struct dev_ext_attribute, attr)->var;
121 +}
122 +
123 +ssize_t axp20x_show_ext_attr(struct device *dev, struct device_attribute *attr,
124 + char *buf)
125 +{
126 + struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
127 + struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
128 + unsigned int val;
129 + int ret, i;
130 + int cnt = 0;
131 +
132 + ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
133 + if (ret != 0)
134 + return ret;
135 +
136 + val &= axp20x_ea->mask;
137 + val >>= ffs(axp20x_ea->mask) - 1;
138 +
139 + for (i = 0; i < 4; i++) {
140 + if (val == i)
141 + cnt += sprintf(buf + cnt, "[%s] ", axp20x_ea->str[i]);
142 + else
143 + cnt += sprintf(buf + cnt, "%s ", axp20x_ea->str[i]);
144 + }
145 +
146 + cnt += sprintf(buf + cnt, "\n");
147 +
148 + return cnt;
149 +}
150 +
151 +ssize_t axp20x_store_ext_attr(struct device *dev, struct device_attribute *attr,
152 + const char *buf, size_t count)
153 +{
154 + struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
155 + struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
156 + char val_str[20];
157 + int ret, i;
158 + size_t len;
159 +
160 + val_str[sizeof(val_str) - 1] = '\0';
161 + strncpy(val_str, buf, sizeof(val_str) - 1);
162 + len = strlen(val_str);
163 +
164 + if (len && val_str[len - 1] == '\n')
165 + val_str[len - 1] = '\0';
166 +
167 + for (i = 0; i < 4; i++) {
168 + if (!strcmp(val_str, axp20x_ea->str[i])) {
169 + ret = regmap_update_bits(axp20x_pek->axp20x->regmap,
170 + AXP20X_PEK_KEY,
171 + axp20x_ea->mask, i);
172 + if (ret != 0)
173 + return -EINVAL;
174 + return count;
175 + }
176 + }
177 +
178 + return -EINVAL;
179 +}
180 +
181 +static struct dev_ext_attribute axp20x_dev_attr_startup = {
182 + .attr = __ATTR(startup, 0644, axp20x_show_ext_attr, axp20x_store_ext_attr),
183 + .var = &axp20x_pek_startup_ext_attr
184 +};
185 +
186 +static struct dev_ext_attribute axp20x_dev_attr_shutdown = {
187 + __ATTR(shutdown, 0644, axp20x_show_ext_attr, axp20x_store_ext_attr),
188 + &axp20x_pek_shutdown_ext_attr
189 +};
190 +
191 +static struct attribute *dev_attrs[] = {
192 + &axp20x_dev_attr_startup.attr.attr,
193 + &axp20x_dev_attr_shutdown.attr.attr,
194 + NULL,
195 +};
196 +
197 +static struct attribute_group dev_attr_group = {
198 + .attrs = dev_attrs,
199 +};
200 +
201 +static const struct attribute_group *dev_attr_groups[] = {
202 + &dev_attr_group,
203 + NULL,
204 +};
205 +
206 +static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
207 +{
208 + struct input_dev *idev = pwr;
209 + struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
210 +
211 + if (irq == axp20x_pek->irq_dbr)
212 + input_report_key(idev, KEY_POWER, true);
213 + else if (irq == axp20x_pek->irq_dbf)
214 + input_report_key(idev, KEY_POWER, false);
215 +
216 + input_sync(idev);
217 +
218 + return IRQ_HANDLED;
219 +}
220 +
221 +static int axp20x_pek_probe(struct platform_device *pdev)
222 +{
223 + struct axp20x_pek *axp20x_pek;
224 + struct axp20x_dev *axp20x;
225 + struct input_dev *idev;
226 + int error;
227 +
228 + axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
229 + GFP_KERNEL);
230 + if (!axp20x_pek)
231 + return -ENOMEM;
232 +
233 + axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
234 + axp20x = axp20x_pek->axp20x;
235 +
236 + axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR");
237 + if (axp20x_pek->irq_dbr < 0) {
238 + dev_err(&pdev->dev, "No IRQ for PEK_DBR, error=%d\n",
239 + axp20x_pek->irq_dbr);
240 + return axp20x_pek->irq_dbr;
241 + }
242 + axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
243 + axp20x_pek->irq_dbr);
244 +
245 + axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
246 + if (axp20x_pek->irq_dbf < 0) {
247 + dev_err(&pdev->dev, "No IRQ for PEK_DBF, error=%d\n",
248 + axp20x_pek->irq_dbf);
249 + return axp20x_pek->irq_dbf;
250 + }
251 + axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
252 + axp20x_pek->irq_dbf);
253 +
254 + axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
255 + if (!axp20x_pek->input)
256 + return -ENOMEM;
257 +
258 + idev = axp20x_pek->input;
259 +
260 + idev->name = "axp20x-pek";
261 + idev->phys = "m1kbd/input2";
262 + idev->dev.parent = &pdev->dev;
263 +
264 + input_set_capability(idev, EV_KEY, KEY_POWER);
265 +
266 + input_set_drvdata(idev, axp20x_pek);
267 +
268 + error = devm_request_threaded_irq(&pdev->dev, axp20x_pek->irq_dbr,
269 + NULL, axp20x_pek_irq, 0,
270 + "axp20x-pek-dbr", idev);
271 + if (error) {
272 + dev_err(axp20x->dev, "Failed to request dbr IRQ#%d: %d\n",
273 + axp20x_pek->irq_dbr, error);
274 +
275 + return error;
276 + }
277 +
278 + error = devm_request_threaded_irq(&pdev->dev, axp20x_pek->irq_dbf,
279 + NULL, axp20x_pek_irq, 0,
280 + "axp20x-pek-dbf", idev);
281 + if (error) {
282 + dev_err(axp20x->dev, "Failed to request dbf IRQ#%d: %d\n",
283 + axp20x_pek->irq_dbf, error);
284 + return error;
285 + }
286 +
287 + idev->dev.groups = dev_attr_groups;
288 + error = input_register_device(idev);
289 + if (error) {
290 + dev_err(axp20x->dev, "Can't register input device: %d\n", error);
291 + return error;
292 + }
293 +
294 + platform_set_drvdata(pdev, axp20x_pek);
295 +
296 + return 0;
297 +}
298 +
299 +static int axp20x_pek_remove(struct platform_device *pdev)
300 +{
301 + struct axp20x_pek *axp20x_pek = platform_get_drvdata(pdev);
302 +
303 + input_unregister_device(axp20x_pek->input);
304 +
305 + return 0;
306 +}
307 +
308 +static const struct of_device_id axp20x_pek_match[] = {
309 + { .compatible = "x-powers,axp20x-pek", },
310 + { /* sentinel */ },
311 +};
312 +MODULE_DEVICE_TABLE(of, axp20x_pek_match);
313 +
314 +static struct platform_driver axp20x_pek_driver = {
315 + .probe = axp20x_pek_probe,
316 + .remove = axp20x_pek_remove,
317 + .driver = {
318 + .name = "axp20x-pek",
319 + .owner = THIS_MODULE,
320 + .of_match_table = axp20x_pek_match,
321 + },
322 +};
323 +module_platform_driver(axp20x_pek_driver);
324 +
325 +MODULE_DESCRIPTION("axp20x Power Button");
326 +MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
327 +MODULE_LICENSE("GPL");