1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Power-button driver for Basin Cove PMIC
4  *
5  * Copyright (c) 2019, Intel Corporation.
6  * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
7  */
8 
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>
18 
19 #define BCOVE_PBSTATUS		0x27
20 #define BCOVE_PBSTATUS_PBLVL	BIT(4)	/* 1 - release, 0 - press */
21 
22 static irqreturn_t mrfld_pwrbtn_interrupt(int irq, void *dev_id)
23 {
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;
29 
30 	ret = regmap_read(regmap, BCOVE_PBSTATUS, &state);
31 	if (ret)
32 		return IRQ_NONE;
33 
34 	dev_dbg(dev, "PBSTATUS=0x%x\n", state);
35 	input_report_key(input, KEY_POWER, !(state & BCOVE_PBSTATUS_PBLVL));
36 	input_sync(input);
37 
38 	regmap_update_bits(regmap, BCOVE_MIRQLVL1, BCOVE_LVL1_PWRBTN, 0);
39 	return IRQ_HANDLED;
40 }
41 
42 static int mrfld_pwrbtn_probe(struct platform_device *pdev)
43 {
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;
48 
49 	irq = platform_get_irq(pdev, 0);
50 	if (irq < 0)
51 		return irq;
52 
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;
64 
65 	dev_set_drvdata(dev, pmic->regmap);
66 
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;
72 
73 	regmap_update_bits(pmic->regmap, BCOVE_MIRQLVL1, BCOVE_LVL1_PWRBTN, 0);
74 	regmap_update_bits(pmic->regmap, BCOVE_MPBIRQ, BCOVE_PBIRQ_PBTN, 0);
75 
76 	device_init_wakeup(dev, true);
77 	dev_pm_set_wake_irq(dev, irq);
78 	return 0;
79 }
80 
81 static int mrfld_pwrbtn_remove(struct platform_device *pdev)
82 {
83 	struct device *dev = &pdev->dev;
84 
85 	dev_pm_clear_wake_irq(dev);
86 	device_init_wakeup(dev, false);
87 	return 0;
88 }
89 
90 static const struct platform_device_id mrfld_pwrbtn_id_table[] = {
91 	{ .name = "mrfld_bcove_pwrbtn" },
92 	{}
93 };
94 MODULE_DEVICE_TABLE(platform, mrfld_pwrbtn_id_table);
95 
96 static struct platform_driver mrfld_pwrbtn_driver = {
97 	.driver = {
98 		.name	= "mrfld_bcove_pwrbtn",
99 	},
100 	.probe		= mrfld_pwrbtn_probe,
101 	.remove		= mrfld_pwrbtn_remove,
102 	.id_table	= mrfld_pwrbtn_id_table,
103 };
104 module_platform_driver(mrfld_pwrbtn_driver);
105 
106 MODULE_DESCRIPTION("Power-button driver for Basin Cove PMIC");
107 MODULE_LICENSE("GPL v2");
108