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
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.
10 Signed-off-by: Carlo Caione <carlo@caione.org>
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
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
28 +CONFIG_INPUT_AXP20X_PEK=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.
40 +config INPUT_AXP20X_PEK
41 + tristate "X-Powers AXP20X power button driver"
42 + depends on MFD_AXP20X
44 + Say Y here if you want to enable power key reporting via the
47 + To compile this driver as a module, choose M here. The module will
48 + be called axp20x-pek.
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
68 index 0000000..799275d
70 +++ b/drivers/input/misc/axp20x-pek.c
73 + * axp20x power button driver.
75 + * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
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.
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.
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>
99 +#define AXP20X_PEK_STARTUP_MASK (0xc0)
100 +#define AXP20X_PEK_SHUTDOWN_MASK (0x03)
102 +static const char const *startup_time[] = { "128mS", "3S" , "1S", "2S" };
103 +static const char const *shutdown_time[] = { "4S", "6S" , "8S", "10S" };
106 + struct axp20x_dev *axp20x;
107 + struct input_dev *input;
112 +struct axp20x_pek_ext_attr {
113 + const char const **str;
117 +static struct axp20x_pek_ext_attr axp20x_pek_startup_ext_attr = {
118 + .str = startup_time,
119 + .mask = AXP20X_PEK_STARTUP_MASK,
122 +static struct axp20x_pek_ext_attr axp20x_pek_shutdown_ext_attr = {
123 + .str = shutdown_time,
124 + .mask = AXP20X_PEK_SHUTDOWN_MASK,
127 +static struct axp20x_pek_ext_attr *get_axp_ext_attr(struct device_attribute *attr)
129 + return container_of(attr, struct dev_ext_attribute, attr)->var;
132 +ssize_t axp20x_show_ext_attr(struct device *dev, struct device_attribute *attr,
135 + struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
136 + struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
141 + ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
145 + val &= axp20x_ea->mask;
146 + val >>= ffs(axp20x_ea->mask) - 1;
148 + for (i = 0; i < 4; i++) {
150 + cnt += sprintf(buf + cnt, "[%s] ", axp20x_ea->str[i]);
152 + cnt += sprintf(buf + cnt, "%s ", axp20x_ea->str[i]);
155 + cnt += sprintf(buf + cnt, "\n");
160 +ssize_t axp20x_store_ext_attr(struct device *dev, struct device_attribute *attr,
161 + const char *buf, size_t count)
163 + struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
164 + struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
169 + val_str[sizeof(val_str) - 1] = '\0';
170 + strncpy(val_str, buf, sizeof(val_str) - 1);
171 + len = strlen(val_str);
173 + if (len && val_str[len - 1] == '\n')
174 + val_str[len - 1] = '\0';
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,
180 + axp20x_ea->mask, i);
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
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
200 +static struct attribute *dev_attrs[] = {
201 + &axp20x_dev_attr_startup.attr.attr,
202 + &axp20x_dev_attr_shutdown.attr.attr,
206 +static struct attribute_group dev_attr_group = {
207 + .attrs = dev_attrs,
210 +static const struct attribute_group *dev_attr_groups[] = {
215 +static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
217 + struct input_dev *idev = pwr;
218 + struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
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);
227 + return IRQ_HANDLED;
230 +static int axp20x_pek_probe(struct platform_device *pdev)
232 + struct axp20x_pek *axp20x_pek;
233 + struct axp20x_dev *axp20x;
234 + struct input_dev *idev;
237 + axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
242 + axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
243 + axp20x = axp20x_pek->axp20x;
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;
251 + axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
252 + axp20x_pek->irq_dbr);
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;
260 + axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
261 + axp20x_pek->irq_dbf);
263 + axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
264 + if (!axp20x_pek->input)
267 + idev = axp20x_pek->input;
269 + idev->name = "axp20x-pek";
270 + idev->phys = "m1kbd/input2";
271 + idev->dev.parent = &pdev->dev;
273 + input_set_capability(idev, EV_KEY, KEY_POWER);
275 + input_set_drvdata(idev, axp20x_pek);
277 + error = devm_request_threaded_irq(&pdev->dev, axp20x_pek->irq_dbr,
278 + NULL, axp20x_pek_irq, 0,
279 + "axp20x-pek-dbr", idev);
281 + dev_err(axp20x->dev, "Failed to request dbr IRQ#%d: %d\n",
282 + axp20x_pek->irq_dbr, error);
287 + error = devm_request_threaded_irq(&pdev->dev, axp20x_pek->irq_dbf,
288 + NULL, axp20x_pek_irq, 0,
289 + "axp20x-pek-dbf", idev);
291 + dev_err(axp20x->dev, "Failed to request dbf IRQ#%d: %d\n",
292 + axp20x_pek->irq_dbf, error);
296 + idev->dev.groups = dev_attr_groups;
297 + error = input_register_device(idev);
299 + dev_err(axp20x->dev, "Can't register input device: %d\n", error);
303 + platform_set_drvdata(pdev, axp20x_pek);
308 +static int axp20x_pek_remove(struct platform_device *pdev)
310 + struct axp20x_pek *axp20x_pek = platform_get_drvdata(pdev);
312 + input_unregister_device(axp20x_pek->input);
317 +static const struct of_device_id axp20x_pek_match[] = {
318 + { .compatible = "x-powers,axp20x-pek", },
319 + { /* sentinel */ },
321 +MODULE_DEVICE_TABLE(of, axp20x_pek_match);
323 +static struct platform_driver axp20x_pek_driver = {
324 + .probe = axp20x_pek_probe,
325 + .remove = axp20x_pek_remove,
327 + .name = "axp20x-pek",
328 + .owner = THIS_MODULE,
329 + .of_match_table = axp20x_pek_match,
332 +module_platform_driver(axp20x_pek_driver);
334 +MODULE_DESCRIPTION("axp20x Power Button");
335 +MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
336 +MODULE_LICENSE("GPL");