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 --- 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
26 +CONFIG_INPUT_AXP20X_PEK=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.
36 +config INPUT_AXP20X_PEK
37 + tristate "X-Powers AXP20X power button driver"
38 + depends on MFD_AXP20X
40 + Say Y here if you want to enable power key reporting via the
43 + To compile this driver as a module, choose M here. The module will
44 + be called axp20x-pek.
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
61 +++ b/drivers/input/misc/axp20x-pek.c
64 + * axp20x power button driver.
66 + * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
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.
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.
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>
90 +#define AXP20X_PEK_STARTUP_MASK (0xc0)
91 +#define AXP20X_PEK_SHUTDOWN_MASK (0x03)
93 +static const char const *startup_time[] = { "128mS", "3S" , "1S", "2S" };
94 +static const char const *shutdown_time[] = { "4S", "6S" , "8S", "10S" };
97 + struct axp20x_dev *axp20x;
98 + struct input_dev *input;
103 +struct axp20x_pek_ext_attr {
104 + const char const **str;
108 +static struct axp20x_pek_ext_attr axp20x_pek_startup_ext_attr = {
109 + .str = startup_time,
110 + .mask = AXP20X_PEK_STARTUP_MASK,
113 +static struct axp20x_pek_ext_attr axp20x_pek_shutdown_ext_attr = {
114 + .str = shutdown_time,
115 + .mask = AXP20X_PEK_SHUTDOWN_MASK,
118 +static struct axp20x_pek_ext_attr *get_axp_ext_attr(struct device_attribute *attr)
120 + return container_of(attr, struct dev_ext_attribute, attr)->var;
123 +ssize_t axp20x_show_ext_attr(struct device *dev, struct device_attribute *attr,
126 + struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
127 + struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
132 + ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
136 + val &= axp20x_ea->mask;
137 + val >>= ffs(axp20x_ea->mask) - 1;
139 + for (i = 0; i < 4; i++) {
141 + cnt += sprintf(buf + cnt, "[%s] ", axp20x_ea->str[i]);
143 + cnt += sprintf(buf + cnt, "%s ", axp20x_ea->str[i]);
146 + cnt += sprintf(buf + cnt, "\n");
151 +ssize_t axp20x_store_ext_attr(struct device *dev, struct device_attribute *attr,
152 + const char *buf, size_t count)
154 + struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
155 + struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
160 + val_str[sizeof(val_str) - 1] = '\0';
161 + strncpy(val_str, buf, sizeof(val_str) - 1);
162 + len = strlen(val_str);
164 + if (len && val_str[len - 1] == '\n')
165 + val_str[len - 1] = '\0';
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,
171 + axp20x_ea->mask, i);
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
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
191 +static struct attribute *dev_attrs[] = {
192 + &axp20x_dev_attr_startup.attr.attr,
193 + &axp20x_dev_attr_shutdown.attr.attr,
197 +static struct attribute_group dev_attr_group = {
198 + .attrs = dev_attrs,
201 +static const struct attribute_group *dev_attr_groups[] = {
206 +static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
208 + struct input_dev *idev = pwr;
209 + struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
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);
218 + return IRQ_HANDLED;
221 +static int axp20x_pek_probe(struct platform_device *pdev)
223 + struct axp20x_pek *axp20x_pek;
224 + struct axp20x_dev *axp20x;
225 + struct input_dev *idev;
228 + axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
233 + axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
234 + axp20x = axp20x_pek->axp20x;
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;
242 + axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
243 + axp20x_pek->irq_dbr);
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;
251 + axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
252 + axp20x_pek->irq_dbf);
254 + axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
255 + if (!axp20x_pek->input)
258 + idev = axp20x_pek->input;
260 + idev->name = "axp20x-pek";
261 + idev->phys = "m1kbd/input2";
262 + idev->dev.parent = &pdev->dev;
264 + input_set_capability(idev, EV_KEY, KEY_POWER);
266 + input_set_drvdata(idev, axp20x_pek);
268 + error = devm_request_threaded_irq(&pdev->dev, axp20x_pek->irq_dbr,
269 + NULL, axp20x_pek_irq, 0,
270 + "axp20x-pek-dbr", idev);
272 + dev_err(axp20x->dev, "Failed to request dbr IRQ#%d: %d\n",
273 + axp20x_pek->irq_dbr, error);
278 + error = devm_request_threaded_irq(&pdev->dev, axp20x_pek->irq_dbf,
279 + NULL, axp20x_pek_irq, 0,
280 + "axp20x-pek-dbf", idev);
282 + dev_err(axp20x->dev, "Failed to request dbf IRQ#%d: %d\n",
283 + axp20x_pek->irq_dbf, error);
287 + idev->dev.groups = dev_attr_groups;
288 + error = input_register_device(idev);
290 + dev_err(axp20x->dev, "Can't register input device: %d\n", error);
294 + platform_set_drvdata(pdev, axp20x_pek);
299 +static int axp20x_pek_remove(struct platform_device *pdev)
301 + struct axp20x_pek *axp20x_pek = platform_get_drvdata(pdev);
303 + input_unregister_device(axp20x_pek->input);
308 +static const struct of_device_id axp20x_pek_match[] = {
309 + { .compatible = "x-powers,axp20x-pek", },
310 + { /* sentinel */ },
312 +MODULE_DEVICE_TABLE(of, axp20x_pek_match);
314 +static struct platform_driver axp20x_pek_driver = {
315 + .probe = axp20x_pek_probe,
316 + .remove = axp20x_pek_remove,
318 + .name = "axp20x-pek",
319 + .owner = THIS_MODULE,
320 + .of_match_table = axp20x_pek_match,
323 +module_platform_driver(axp20x_pek_driver);
325 +MODULE_DESCRIPTION("axp20x Power Button");
326 +MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
327 +MODULE_LICENSE("GPL");