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