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/acpi.h>
17 #include <linux/errno.h>
18 #include <linux/irq.h>
19 #include <linux/init.h>
20 #include <linux/input.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/mfd/axp20x.h>
24 #include <linux/module.h>
25 #include <linux/platform_data/x86/soc.h>
26 #include <linux/platform_device.h>
27 #include <linux/regmap.h>
28 #include <linux/slab.h>
30 #define AXP20X_PEK_STARTUP_MASK (0xc0)
31 #define AXP20X_PEK_SHUTDOWN_MASK (0x03)
34 const struct axp20x_time *startup_time;
35 unsigned int startup_mask;
36 const struct axp20x_time *shutdown_time;
37 unsigned int shutdown_mask;
41 struct axp20x_dev *axp20x;
42 struct input_dev *input;
43 struct axp20x_info *info;
53 static const struct axp20x_time startup_time[] = {
54 { .time = 128, .idx = 0 },
55 { .time = 1000, .idx = 2 },
56 { .time = 3000, .idx = 1 },
57 { .time = 2000, .idx = 3 },
60 static const struct axp20x_time axp221_startup_time[] = {
61 { .time = 128, .idx = 0 },
62 { .time = 1000, .idx = 1 },
63 { .time = 2000, .idx = 2 },
64 { .time = 3000, .idx = 3 },
67 static const struct axp20x_time shutdown_time[] = {
68 { .time = 4000, .idx = 0 },
69 { .time = 6000, .idx = 1 },
70 { .time = 8000, .idx = 2 },
71 { .time = 10000, .idx = 3 },
74 static const struct axp20x_info axp20x_info = {
75 .startup_time = startup_time,
76 .startup_mask = AXP20X_PEK_STARTUP_MASK,
77 .shutdown_time = shutdown_time,
78 .shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK,
81 static const struct axp20x_info axp221_info = {
82 .startup_time = axp221_startup_time,
83 .startup_mask = AXP20X_PEK_STARTUP_MASK,
84 .shutdown_time = shutdown_time,
85 .shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK,
88 static ssize_t axp20x_show_attr(struct device *dev,
89 const struct axp20x_time *time,
90 unsigned int mask, char *buf)
92 struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
96 ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
101 val >>= ffs(mask) - 1;
103 for (i = 0; i < 4; i++)
104 if (val == time[i].idx)
107 return sprintf(buf, "%u\n", val);
110 static ssize_t axp20x_show_attr_startup(struct device *dev,
111 struct device_attribute *attr,
114 struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
116 return axp20x_show_attr(dev, axp20x_pek->info->startup_time,
117 axp20x_pek->info->startup_mask, buf);
120 static ssize_t axp20x_show_attr_shutdown(struct device *dev,
121 struct device_attribute *attr,
124 struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
126 return axp20x_show_attr(dev, axp20x_pek->info->shutdown_time,
127 axp20x_pek->info->shutdown_mask, buf);
130 static ssize_t axp20x_store_attr(struct device *dev,
131 const struct axp20x_time *time,
132 unsigned int mask, const char *buf,
135 struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
137 unsigned int val, idx = 0;
138 unsigned int best_err = UINT_MAX;
140 ret = kstrtouint(buf, 10, &val);
144 for (i = 3; i >= 0; i--) {
147 err = abs(time[i].time - val);
148 if (err < best_err) {
157 idx <<= ffs(mask) - 1;
158 ret = regmap_update_bits(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY,
166 static ssize_t axp20x_store_attr_startup(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf, size_t count)
170 struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
172 return axp20x_store_attr(dev, axp20x_pek->info->startup_time,
173 axp20x_pek->info->startup_mask, buf, count);
176 static ssize_t axp20x_store_attr_shutdown(struct device *dev,
177 struct device_attribute *attr,
178 const char *buf, size_t count)
180 struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
182 return axp20x_store_attr(dev, axp20x_pek->info->shutdown_time,
183 axp20x_pek->info->shutdown_mask, buf, count);
186 static DEVICE_ATTR(startup, 0644, axp20x_show_attr_startup,
187 axp20x_store_attr_startup);
188 static DEVICE_ATTR(shutdown, 0644, axp20x_show_attr_shutdown,
189 axp20x_store_attr_shutdown);
191 static struct attribute *axp20x_attrs[] = {
192 &dev_attr_startup.attr,
193 &dev_attr_shutdown.attr,
196 ATTRIBUTE_GROUPS(axp20x);
198 static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
200 struct input_dev *idev = pwr;
201 struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
204 * The power-button is connected to ground so a falling edge (dbf)
205 * means it is pressed.
207 if (irq == axp20x_pek->irq_dbf)
208 input_report_key(idev, KEY_POWER, true);
209 else if (irq == axp20x_pek->irq_dbr)
210 input_report_key(idev, KEY_POWER, false);
217 static int axp20x_pek_probe_input_device(struct axp20x_pek *axp20x_pek,
218 struct platform_device *pdev)
220 struct axp20x_dev *axp20x = axp20x_pek->axp20x;
221 struct input_dev *idev;
224 axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR");
225 if (axp20x_pek->irq_dbr < 0)
226 return axp20x_pek->irq_dbr;
227 axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
228 axp20x_pek->irq_dbr);
230 axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
231 if (axp20x_pek->irq_dbf < 0)
232 return axp20x_pek->irq_dbf;
233 axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
234 axp20x_pek->irq_dbf);
236 axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
237 if (!axp20x_pek->input)
240 idev = axp20x_pek->input;
242 idev->name = "axp20x-pek";
243 idev->phys = "m1kbd/input2";
244 idev->dev.parent = &pdev->dev;
246 input_set_capability(idev, EV_KEY, KEY_POWER);
248 input_set_drvdata(idev, axp20x_pek);
250 error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
252 "axp20x-pek-dbr", idev);
254 dev_err(&pdev->dev, "Failed to request dbr IRQ#%d: %d\n",
255 axp20x_pek->irq_dbr, error);
259 error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
261 "axp20x-pek-dbf", idev);
263 dev_err(&pdev->dev, "Failed to request dbf IRQ#%d: %d\n",
264 axp20x_pek->irq_dbf, error);
268 error = input_register_device(idev);
270 dev_err(&pdev->dev, "Can't register input device: %d\n",
275 device_init_wakeup(&pdev->dev, true);
280 static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek)
282 if (IS_ENABLED(CONFIG_INPUT_SOC_BUTTON_ARRAY) &&
283 axp20x_pek->axp20x->variant == AXP288_ID) {
285 * On Cherry Trail platforms (hrv == 3), do not register the
286 * input device if there is an "INTCFD9" or "ACPI0011" gpio
287 * button ACPI device, as that handles the power button too,
288 * and otherwise we end up reporting all presses twice.
290 if (soc_intel_is_cht() &&
291 (acpi_dev_present("INTCFD9", NULL, -1) ||
292 acpi_dev_present("ACPI0011", NULL, -1)))
299 static int axp20x_pek_probe(struct platform_device *pdev)
301 struct axp20x_pek *axp20x_pek;
302 const struct platform_device_id *match = platform_get_device_id(pdev);
306 dev_err(&pdev->dev, "Failed to get platform_device_id\n");
310 axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
315 axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
317 if (axp20x_pek_should_register_input(axp20x_pek)) {
318 error = axp20x_pek_probe_input_device(axp20x_pek, pdev);
323 axp20x_pek->info = (struct axp20x_info *)match->driver_data;
325 platform_set_drvdata(pdev, axp20x_pek);
330 static int axp20x_pek_suspend(struct device *dev)
332 struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
335 * As nested threaded IRQs are not automatically disabled during
336 * suspend, we must explicitly disable non-wakeup IRQs.
338 if (device_may_wakeup(dev)) {
339 enable_irq_wake(axp20x_pek->irq_dbf);
340 enable_irq_wake(axp20x_pek->irq_dbr);
342 disable_irq(axp20x_pek->irq_dbf);
343 disable_irq(axp20x_pek->irq_dbr);
349 static int axp20x_pek_resume(struct device *dev)
351 struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
353 if (device_may_wakeup(dev)) {
354 disable_irq_wake(axp20x_pek->irq_dbf);
355 disable_irq_wake(axp20x_pek->irq_dbr);
357 enable_irq(axp20x_pek->irq_dbf);
358 enable_irq(axp20x_pek->irq_dbr);
364 static int __maybe_unused axp20x_pek_resume_noirq(struct device *dev)
366 struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
368 if (axp20x_pek->axp20x->variant != AXP288_ID)
372 * Clear interrupts from button presses during suspend, to avoid
373 * a wakeup power-button press getting reported to userspace.
375 regmap_write(axp20x_pek->axp20x->regmap,
376 AXP20X_IRQ1_STATE + AXP288_IRQ_POKN / 8,
377 BIT(AXP288_IRQ_POKN % 8));
382 static const struct dev_pm_ops axp20x_pek_pm_ops = {
383 SYSTEM_SLEEP_PM_OPS(axp20x_pek_suspend, axp20x_pek_resume)
384 .resume_noirq = pm_sleep_ptr(axp20x_pek_resume_noirq),
387 static const struct platform_device_id axp_pek_id_match[] = {
389 .name = "axp20x-pek",
390 .driver_data = (kernel_ulong_t)&axp20x_info,
393 .name = "axp221-pek",
394 .driver_data = (kernel_ulong_t)&axp221_info,
398 MODULE_DEVICE_TABLE(platform, axp_pek_id_match);
400 static struct platform_driver axp20x_pek_driver = {
401 .probe = axp20x_pek_probe,
402 .id_table = axp_pek_id_match,
404 .name = "axp20x-pek",
405 .pm = pm_sleep_ptr(&axp20x_pek_pm_ops),
406 .dev_groups = axp20x_groups,
409 module_platform_driver(axp20x_pek_driver);
411 MODULE_DESCRIPTION("axp20x Power Button");
412 MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
413 MODULE_LICENSE("GPL");