1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Power-button driver for Dollar Cove TI PMIC
4  * Copyright (C) 2014 Intel Corp
5  * Copyright (c) 2017 Takashi Iwai <tiwai@suse.de>
6  */
7 
8 #include <linux/input.h>
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/mfd/intel_soc_pmic.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_wakeirq.h>
15 #include <linux/slab.h>
16 
17 #define CHTDC_TI_SIRQ_REG	0x3
18 #define SIRQ_PWRBTN_REL		BIT(0)
19 
20 static irqreturn_t chtdc_ti_pwrbtn_interrupt(int irq, void *dev_id)
21 {
22 	struct input_dev *input = dev_id;
23 	struct device *dev = input->dev.parent;
24 	struct regmap *regmap = dev_get_drvdata(dev);
25 	int state;
26 
27 	if (!regmap_read(regmap, CHTDC_TI_SIRQ_REG, &state)) {
28 		dev_dbg(dev, "SIRQ_REG=0x%x\n", state);
29 		input_report_key(input, KEY_POWER, !(state & SIRQ_PWRBTN_REL));
30 		input_sync(input);
31 	}
32 
33 	return IRQ_HANDLED;
34 }
35 
36 static int chtdc_ti_pwrbtn_probe(struct platform_device *pdev)
37 {
38 	struct device *dev = &pdev->dev;
39 	struct intel_soc_pmic *pmic = dev_get_drvdata(dev->parent);
40 	struct input_dev *input;
41 	int irq, err;
42 
43 	irq = platform_get_irq(pdev, 0);
44 	if (irq < 0)
45 		return irq;
46 	input = devm_input_allocate_device(dev);
47 	if (!input)
48 		return -ENOMEM;
49 	input->name = pdev->name;
50 	input->phys = "power-button/input0";
51 	input->id.bustype = BUS_HOST;
52 	input_set_capability(input, EV_KEY, KEY_POWER);
53 	err = input_register_device(input);
54 	if (err)
55 		return err;
56 
57 	dev_set_drvdata(dev, pmic->regmap);
58 
59 	err = devm_request_threaded_irq(dev, irq, NULL,
60 					chtdc_ti_pwrbtn_interrupt,
61 					IRQF_ONESHOT, KBUILD_MODNAME, input);
62 	if (err)
63 		return err;
64 
65 	device_init_wakeup(dev, true);
66 	dev_pm_set_wake_irq(dev, irq);
67 	return 0;
68 }
69 
70 static int chtdc_ti_pwrbtn_remove(struct platform_device *pdev)
71 {
72 	dev_pm_clear_wake_irq(&pdev->dev);
73 	device_init_wakeup(&pdev->dev, false);
74 	return 0;
75 }
76 
77 static const struct platform_device_id chtdc_ti_pwrbtn_id_table[] = {
78 	{ .name = "chtdc_ti_pwrbtn" },
79 	{},
80 };
81 MODULE_DEVICE_TABLE(platform, chtdc_ti_pwrbtn_id_table);
82 
83 static struct platform_driver chtdc_ti_pwrbtn_driver = {
84 	.driver = {
85 		.name	= KBUILD_MODNAME,
86 	},
87 	.probe		= chtdc_ti_pwrbtn_probe,
88 	.remove		= chtdc_ti_pwrbtn_remove,
89 	.id_table	= chtdc_ti_pwrbtn_id_table,
90 };
91 module_platform_driver(chtdc_ti_pwrbtn_driver);
92 
93 MODULE_DESCRIPTION("Power-button driver for Dollar Cove TI PMIC");
94 MODULE_LICENSE("GPL v2");
95