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
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>
11 Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
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
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.
25 +config INPUT_AXP20X_PEK
26 + tristate "X-Powers AXP20X power button driver"
27 + depends on MFD_AXP20X
29 + Say Y here if you want to enable power key reporting via the
32 + To compile this driver as a module, choose M here. The module will
33 + be called axp20x-pek.
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
50 +++ b/drivers/input/misc/axp20x-pek.c
53 + * axp20x power button driver.
55 + * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
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.
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.
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>
79 +#define AXP20X_PEK_STARTUP_MASK (0xc0)
80 +#define AXP20X_PEK_SHUTDOWN_MASK (0x03)
83 + struct axp20x_dev *axp20x;
84 + struct input_dev *input;
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 },
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 },
108 +struct axp20x_pek_ext_attr {
109 + const struct axp20x_time *p_time;
113 +static struct axp20x_pek_ext_attr axp20x_pek_startup_ext_attr = {
114 + .p_time = startup_time,
115 + .mask = AXP20X_PEK_STARTUP_MASK,
118 +static struct axp20x_pek_ext_attr axp20x_pek_shutdown_ext_attr = {
119 + .p_time = shutdown_time,
120 + .mask = AXP20X_PEK_SHUTDOWN_MASK,
123 +static struct axp20x_pek_ext_attr *get_axp_ext_attr(struct device_attribute *attr)
125 + return container_of(attr, struct dev_ext_attribute, attr)->var;
128 +static ssize_t axp20x_show_ext_attr(struct device *dev, struct device_attribute *attr,
131 + struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
132 + struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
136 + ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
140 + val &= axp20x_ea->mask;
141 + val >>= ffs(axp20x_ea->mask) - 1;
143 + for (i = 0; i < 4; i++)
144 + if (val == axp20x_ea->p_time[i].idx)
145 + val = axp20x_ea->p_time[i].time;
147 + return sprintf(buf, "%u\n", val);
150 +static ssize_t axp20x_store_ext_attr(struct device *dev, struct device_attribute *attr,
151 + const char *buf, size_t count)
153 + struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
154 + struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
158 + unsigned int val, idx = 0;
159 + unsigned int best_err = UINT_MAX;
161 + val_str[sizeof(val_str) - 1] = '\0';
162 + strncpy(val_str, buf, sizeof(val_str) - 1);
163 + len = strlen(val_str);
165 + if (len && val_str[len - 1] == '\n')
166 + val_str[len - 1] = '\0';
168 + ret = kstrtouint(val_str, 10, &val);
172 + for (i = 3; i >= 0; i--) {
175 + err = abs(axp20x_ea->p_time[i].time - val);
176 + if (err < best_err) {
178 + idx = axp20x_ea->p_time[i].idx;
185 + idx <<= ffs(axp20x_ea->mask) - 1;
186 + ret = regmap_update_bits(axp20x_pek->axp20x->regmap,
188 + axp20x_ea->mask, idx);
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
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
204 +static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
206 + struct input_dev *idev = pwr;
207 + struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
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);
216 + return IRQ_HANDLED;
219 +static int axp20x_pek_probe(struct platform_device *pdev)
221 + struct axp20x_pek *axp20x_pek;
222 + struct axp20x_dev *axp20x;
223 + struct input_dev *idev;
226 + axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
231 + axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
232 + axp20x = axp20x_pek->axp20x;
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;
240 + axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
241 + axp20x_pek->irq_dbr);
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;
249 + axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
250 + axp20x_pek->irq_dbf);
252 + axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
253 + if (!axp20x_pek->input)
256 + idev = axp20x_pek->input;
258 + idev->name = "axp20x-pek";
259 + idev->phys = "m1kbd/input2";
260 + idev->dev.parent = &pdev->dev;
262 + input_set_capability(idev, EV_KEY, KEY_POWER);
264 + input_set_drvdata(idev, axp20x_pek);
266 + error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
268 + "axp20x-pek-dbr", idev);
270 + dev_err(axp20x->dev, "Failed to request dbr IRQ#%d: %d\n",
271 + axp20x_pek->irq_dbr, error);
276 + error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
278 + "axp20x-pek-dbf", idev);
280 + dev_err(axp20x->dev, "Failed to request dbf IRQ#%d: %d\n",
281 + axp20x_pek->irq_dbf, error);
285 + error = device_create_file(&pdev->dev, &axp20x_dev_attr_startup.attr);
289 + error = device_create_file(&pdev->dev, &axp20x_dev_attr_shutdown.attr);
291 + goto clear_startup_attr;
293 + error = input_register_device(idev);
295 + dev_err(axp20x->dev, "Can't register input device: %d\n", error);
299 + platform_set_drvdata(pdev, axp20x_pek);
304 + device_remove_file(&pdev->dev, &axp20x_dev_attr_shutdown.attr);
307 + device_remove_file(&pdev->dev, &axp20x_dev_attr_startup.attr);
312 +int axp20x_pek_remove(struct platform_device *pdev)
314 + device_remove_file(&pdev->dev, &axp20x_dev_attr_shutdown.attr);
315 + device_remove_file(&pdev->dev, &axp20x_dev_attr_startup.attr);
320 +static struct platform_driver axp20x_pek_driver = {
321 + .probe = axp20x_pek_probe,
322 + .remove = axp20x_pek_remove,
324 + .name = "axp20x-pek",
325 + .owner = THIS_MODULE,
328 +module_platform_driver(axp20x_pek_driver);
330 +MODULE_DESCRIPTION("axp20x Power Button");
331 +MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
332 +MODULE_LICENSE("GPL");