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