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