Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / platform / x86 / intel_mrfld_pwrbtn.c
blobd58fea51747e940251494096ef525b5e68b1e82e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Power-button driver for Basin Cove PMIC
5 * Copyright (c) 2019, Intel Corporation.
6 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
7 */
9 #include <linux/input.h>
10 #include <linux/interrupt.h>
11 #include <linux/device.h>
12 #include <linux/mfd/intel_soc_pmic.h>
13 #include <linux/mfd/intel_soc_pmic_mrfld.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_wakeirq.h>
17 #include <linux/slab.h>
19 #define BCOVE_PBSTATUS 0x27
20 #define BCOVE_PBSTATUS_PBLVL BIT(4) /* 1 - release, 0 - press */
22 static irqreturn_t mrfld_pwrbtn_interrupt(int irq, void *dev_id)
24 struct input_dev *input = dev_id;
25 struct device *dev = input->dev.parent;
26 struct regmap *regmap = dev_get_drvdata(dev);
27 unsigned int state;
28 int ret;
30 ret = regmap_read(regmap, BCOVE_PBSTATUS, &state);
31 if (ret)
32 return IRQ_NONE;
34 dev_dbg(dev, "PBSTATUS=0x%x\n", state);
35 input_report_key(input, KEY_POWER, !(state & BCOVE_PBSTATUS_PBLVL));
36 input_sync(input);
38 regmap_update_bits(regmap, BCOVE_MIRQLVL1, BCOVE_LVL1_PWRBTN, 0);
39 return IRQ_HANDLED;
42 static int mrfld_pwrbtn_probe(struct platform_device *pdev)
44 struct device *dev = &pdev->dev;
45 struct intel_soc_pmic *pmic = dev_get_drvdata(dev->parent);
46 struct input_dev *input;
47 int irq, ret;
49 irq = platform_get_irq(pdev, 0);
50 if (irq < 0)
51 return irq;
53 input = devm_input_allocate_device(dev);
54 if (!input)
55 return -ENOMEM;
56 input->name = pdev->name;
57 input->phys = "power-button/input0";
58 input->id.bustype = BUS_HOST;
59 input->dev.parent = dev;
60 input_set_capability(input, EV_KEY, KEY_POWER);
61 ret = input_register_device(input);
62 if (ret)
63 return ret;
65 dev_set_drvdata(dev, pmic->regmap);
67 ret = devm_request_threaded_irq(dev, irq, NULL, mrfld_pwrbtn_interrupt,
68 IRQF_ONESHOT | IRQF_SHARED, pdev->name,
69 input);
70 if (ret)
71 return ret;
73 regmap_update_bits(pmic->regmap, BCOVE_MIRQLVL1, BCOVE_LVL1_PWRBTN, 0);
74 regmap_update_bits(pmic->regmap, BCOVE_MPBIRQ, BCOVE_PBIRQ_PBTN, 0);
76 device_init_wakeup(dev, true);
77 dev_pm_set_wake_irq(dev, irq);
78 return 0;
81 static int mrfld_pwrbtn_remove(struct platform_device *pdev)
83 struct device *dev = &pdev->dev;
85 dev_pm_clear_wake_irq(dev);
86 device_init_wakeup(dev, false);
87 return 0;
90 static const struct platform_device_id mrfld_pwrbtn_id_table[] = {
91 { .name = "mrfld_bcove_pwrbtn" },
94 MODULE_DEVICE_TABLE(platform, mrfld_pwrbtn_id_table);
96 static struct platform_driver mrfld_pwrbtn_driver = {
97 .driver = {
98 .name = "mrfld_bcove_pwrbtn",
100 .probe = mrfld_pwrbtn_probe,
101 .remove = mrfld_pwrbtn_remove,
102 .id_table = mrfld_pwrbtn_id_table,
104 module_platform_driver(mrfld_pwrbtn_driver);
106 MODULE_DESCRIPTION("Power-button driver for Basin Cove PMIC");
107 MODULE_LICENSE("GPL v2");