2 * axp20x power button driver.
4 * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
6 * This file is subject to the terms and conditions of the GNU General
7 * Public License. See the file "COPYING" in the main directory of this
8 * archive for more details.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/errno.h>
17 #include <linux/irq.h>
18 #include <linux/init.h>
19 #include <linux/input.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/mfd/axp20x.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/regmap.h>
26 #include <linux/slab.h>
28 #define AXP20X_PEK_STARTUP_MASK (0xc0)
29 #define AXP20X_PEK_SHUTDOWN_MASK (0x03)
32 struct axp20x_dev
*axp20x
;
33 struct input_dev
*input
;
43 static const struct axp20x_time startup_time
[] = {
44 { .time
= 128, .idx
= 0 },
45 { .time
= 1000, .idx
= 2 },
46 { .time
= 3000, .idx
= 1 },
47 { .time
= 2000, .idx
= 3 },
50 static const struct axp20x_time shutdown_time
[] = {
51 { .time
= 4000, .idx
= 0 },
52 { .time
= 6000, .idx
= 1 },
53 { .time
= 8000, .idx
= 2 },
54 { .time
= 10000, .idx
= 3 },
57 struct axp20x_pek_ext_attr
{
58 const struct axp20x_time
*p_time
;
62 static struct axp20x_pek_ext_attr axp20x_pek_startup_ext_attr
= {
63 .p_time
= startup_time
,
64 .mask
= AXP20X_PEK_STARTUP_MASK
,
67 static struct axp20x_pek_ext_attr axp20x_pek_shutdown_ext_attr
= {
68 .p_time
= shutdown_time
,
69 .mask
= AXP20X_PEK_SHUTDOWN_MASK
,
72 static struct axp20x_pek_ext_attr
*get_axp_ext_attr(struct device_attribute
*attr
)
74 return container_of(attr
, struct dev_ext_attribute
, attr
)->var
;
77 static ssize_t
axp20x_show_ext_attr(struct device
*dev
,
78 struct device_attribute
*attr
, char *buf
)
80 struct axp20x_pek
*axp20x_pek
= dev_get_drvdata(dev
);
81 struct axp20x_pek_ext_attr
*axp20x_ea
= get_axp_ext_attr(attr
);
85 ret
= regmap_read(axp20x_pek
->axp20x
->regmap
, AXP20X_PEK_KEY
, &val
);
89 val
&= axp20x_ea
->mask
;
90 val
>>= ffs(axp20x_ea
->mask
) - 1;
92 for (i
= 0; i
< 4; i
++)
93 if (val
== axp20x_ea
->p_time
[i
].idx
)
94 val
= axp20x_ea
->p_time
[i
].time
;
96 return sprintf(buf
, "%u\n", val
);
99 static ssize_t
axp20x_store_ext_attr(struct device
*dev
,
100 struct device_attribute
*attr
,
101 const char *buf
, size_t count
)
103 struct axp20x_pek
*axp20x_pek
= dev_get_drvdata(dev
);
104 struct axp20x_pek_ext_attr
*axp20x_ea
= get_axp_ext_attr(attr
);
108 unsigned int val
, idx
= 0;
109 unsigned int best_err
= UINT_MAX
;
111 val_str
[sizeof(val_str
) - 1] = '\0';
112 strncpy(val_str
, buf
, sizeof(val_str
) - 1);
113 len
= strlen(val_str
);
115 if (len
&& val_str
[len
- 1] == '\n')
116 val_str
[len
- 1] = '\0';
118 ret
= kstrtouint(val_str
, 10, &val
);
122 for (i
= 3; i
>= 0; i
--) {
125 err
= abs(axp20x_ea
->p_time
[i
].time
- val
);
126 if (err
< best_err
) {
128 idx
= axp20x_ea
->p_time
[i
].idx
;
135 idx
<<= ffs(axp20x_ea
->mask
) - 1;
136 ret
= regmap_update_bits(axp20x_pek
->axp20x
->regmap
,
138 axp20x_ea
->mask
, idx
);
145 static struct dev_ext_attribute axp20x_dev_attr_startup
= {
146 .attr
= __ATTR(startup
, 0644, axp20x_show_ext_attr
, axp20x_store_ext_attr
),
147 .var
= &axp20x_pek_startup_ext_attr
,
150 static struct dev_ext_attribute axp20x_dev_attr_shutdown
= {
151 .attr
= __ATTR(shutdown
, 0644, axp20x_show_ext_attr
, axp20x_store_ext_attr
),
152 .var
= &axp20x_pek_shutdown_ext_attr
,
155 static struct attribute
*axp20x_attributes
[] = {
156 &axp20x_dev_attr_startup
.attr
.attr
,
157 &axp20x_dev_attr_shutdown
.attr
.attr
,
161 static const struct attribute_group axp20x_attribute_group
= {
162 .attrs
= axp20x_attributes
,
165 static irqreturn_t
axp20x_pek_irq(int irq
, void *pwr
)
167 struct input_dev
*idev
= pwr
;
168 struct axp20x_pek
*axp20x_pek
= input_get_drvdata(idev
);
171 * The power-button is connected to ground so a falling edge (dbf)
172 * means it is pressed.
174 if (irq
== axp20x_pek
->irq_dbf
)
175 input_report_key(idev
, KEY_POWER
, true);
176 else if (irq
== axp20x_pek
->irq_dbr
)
177 input_report_key(idev
, KEY_POWER
, false);
184 static void axp20x_remove_sysfs_group(void *_data
)
186 struct device
*dev
= _data
;
188 sysfs_remove_group(&dev
->kobj
, &axp20x_attribute_group
);
191 static int axp20x_pek_probe(struct platform_device
*pdev
)
193 struct axp20x_pek
*axp20x_pek
;
194 struct axp20x_dev
*axp20x
;
195 struct input_dev
*idev
;
198 axp20x_pek
= devm_kzalloc(&pdev
->dev
, sizeof(struct axp20x_pek
),
203 axp20x_pek
->axp20x
= dev_get_drvdata(pdev
->dev
.parent
);
204 axp20x
= axp20x_pek
->axp20x
;
206 axp20x_pek
->irq_dbr
= platform_get_irq_byname(pdev
, "PEK_DBR");
207 if (axp20x_pek
->irq_dbr
< 0) {
208 dev_err(&pdev
->dev
, "No IRQ for PEK_DBR, error=%d\n",
209 axp20x_pek
->irq_dbr
);
210 return axp20x_pek
->irq_dbr
;
212 axp20x_pek
->irq_dbr
= regmap_irq_get_virq(axp20x
->regmap_irqc
,
213 axp20x_pek
->irq_dbr
);
215 axp20x_pek
->irq_dbf
= platform_get_irq_byname(pdev
, "PEK_DBF");
216 if (axp20x_pek
->irq_dbf
< 0) {
217 dev_err(&pdev
->dev
, "No IRQ for PEK_DBF, error=%d\n",
218 axp20x_pek
->irq_dbf
);
219 return axp20x_pek
->irq_dbf
;
221 axp20x_pek
->irq_dbf
= regmap_irq_get_virq(axp20x
->regmap_irqc
,
222 axp20x_pek
->irq_dbf
);
224 axp20x_pek
->input
= devm_input_allocate_device(&pdev
->dev
);
225 if (!axp20x_pek
->input
)
228 idev
= axp20x_pek
->input
;
230 idev
->name
= "axp20x-pek";
231 idev
->phys
= "m1kbd/input2";
232 idev
->dev
.parent
= &pdev
->dev
;
234 input_set_capability(idev
, EV_KEY
, KEY_POWER
);
236 input_set_drvdata(idev
, axp20x_pek
);
238 error
= devm_request_any_context_irq(&pdev
->dev
, axp20x_pek
->irq_dbr
,
240 "axp20x-pek-dbr", idev
);
242 dev_err(axp20x
->dev
, "Failed to request dbr IRQ#%d: %d\n",
243 axp20x_pek
->irq_dbr
, error
);
247 error
= devm_request_any_context_irq(&pdev
->dev
, axp20x_pek
->irq_dbf
,
249 "axp20x-pek-dbf", idev
);
251 dev_err(axp20x
->dev
, "Failed to request dbf IRQ#%d: %d\n",
252 axp20x_pek
->irq_dbf
, error
);
256 error
= sysfs_create_group(&pdev
->dev
.kobj
, &axp20x_attribute_group
);
258 dev_err(axp20x
->dev
, "Failed to create sysfs attributes: %d\n",
263 error
= devm_add_action(&pdev
->dev
,
264 axp20x_remove_sysfs_group
, &pdev
->dev
);
266 axp20x_remove_sysfs_group(&pdev
->dev
);
267 dev_err(&pdev
->dev
, "Failed to add sysfs cleanup action: %d\n",
272 error
= input_register_device(idev
);
274 dev_err(axp20x
->dev
, "Can't register input device: %d\n",
279 platform_set_drvdata(pdev
, axp20x_pek
);
284 static struct platform_driver axp20x_pek_driver
= {
285 .probe
= axp20x_pek_probe
,
287 .name
= "axp20x-pek",
290 module_platform_driver(axp20x_pek_driver
);
292 MODULE_DESCRIPTION("axp20x Power Button");
293 MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
294 MODULE_LICENSE("GPL");
295 MODULE_ALIAS("platform:axp20x-pek");