xref: /linux/drivers/input/misc/rt5120-pwrkey.c (revision 26ba30221c03364d6ed9910be8da4c1fd871b07b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2022 Richtek Technology Corp.
4  * Author: ChiYuan Huang <cy_huang@richtek.com>
5  */
6 
7 #include <linux/bits.h>
8 #include <linux/input.h>
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 
15 #define RT5120_REG_INTSTAT	0x1E
16 #define RT5120_PWRKEYSTAT_MASK	BIT(7)
17 
18 struct rt5120_priv {
19 	struct regmap *regmap;
20 	struct input_dev *input;
21 };
22 
23 static irqreturn_t rt5120_pwrkey_handler(int irq, void *devid)
24 {
25 	struct rt5120_priv *priv = devid;
26 	unsigned int stat;
27 	int error;
28 
29 	error = regmap_read(priv->regmap, RT5120_REG_INTSTAT, &stat);
30 	if (error)
31 		return IRQ_NONE;
32 
33 	input_report_key(priv->input, KEY_POWER,
34 			 !(stat & RT5120_PWRKEYSTAT_MASK));
35 	input_sync(priv->input);
36 
37 	return IRQ_HANDLED;
38 }
39 
40 static int rt5120_pwrkey_probe(struct platform_device *pdev)
41 {
42 	struct rt5120_priv *priv;
43 	struct device *dev = &pdev->dev;
44 	int press_irq, release_irq;
45 	int error;
46 
47 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
48 	if (!priv)
49 		return -ENOMEM;
50 
51 	priv->regmap = dev_get_regmap(dev->parent, NULL);
52 	if (!priv->regmap) {
53 		dev_err(dev, "Failed to init regmap\n");
54 		return -ENODEV;
55 	}
56 
57 	press_irq = platform_get_irq_byname(pdev, "pwrkey-press");
58 	if (press_irq < 0)
59 		return press_irq;
60 
61 	release_irq = platform_get_irq_byname(pdev, "pwrkey-release");
62 	if (release_irq < 0)
63 		return release_irq;
64 
65 	/* Make input device be device resource managed */
66 	priv->input = devm_input_allocate_device(dev);
67 	if (!priv->input)
68 		return -ENOMEM;
69 
70 	priv->input->name = "rt5120_pwrkey";
71 	priv->input->phys = "rt5120_pwrkey/input0";
72 	priv->input->id.bustype = BUS_I2C;
73 	input_set_capability(priv->input, EV_KEY, KEY_POWER);
74 
75 	error = input_register_device(priv->input);
76 	if (error) {
77 		dev_err(dev, "Failed to register input device: %d\n", error);
78 		return error;
79 	}
80 
81 	error = devm_request_threaded_irq(dev, press_irq,
82 					  NULL, rt5120_pwrkey_handler,
83 					  0, "pwrkey-press", priv);
84 	if (error) {
85 		dev_err(dev,
86 			"Failed to register pwrkey press irq: %d\n", error);
87 		return error;
88 	}
89 
90 	error = devm_request_threaded_irq(dev, release_irq,
91 					  NULL, rt5120_pwrkey_handler,
92 					  0, "pwrkey-release", priv);
93 	if (error) {
94 		dev_err(dev,
95 			"Failed to register pwrkey release irq: %d\n", error);
96 		return error;
97 	}
98 
99 	return 0;
100 }
101 
102 static const struct of_device_id r5120_pwrkey_match_table[] = {
103 	{ .compatible = "richtek,rt5120-pwrkey" },
104 	{}
105 };
106 MODULE_DEVICE_TABLE(of, r5120_pwrkey_match_table);
107 
108 static struct platform_driver rt5120_pwrkey_driver = {
109 	.driver = {
110 		.name = "rt5120-pwrkey",
111 		.of_match_table = r5120_pwrkey_match_table,
112 	},
113 	.probe = rt5120_pwrkey_probe,
114 };
115 module_platform_driver(rt5120_pwrkey_driver);
116 
117 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
118 MODULE_DESCRIPTION("Richtek RT5120 power key driver");
119 MODULE_LICENSE("GPL");
120