1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2022 Richtek Technology Corp.
4 * Author: ChiYuan Huang <cy_huang@richtek.com>
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/mod_devicetable.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
16 #define RT5120_REG_INTSTAT 0x1E
17 #define RT5120_PWRKEYSTAT_MASK BIT(7)
20 struct regmap *regmap;
21 struct input_dev *input;
24 static irqreturn_t rt5120_pwrkey_handler(int irq, void *devid)
26 struct rt5120_priv *priv = devid;
30 error = regmap_read(priv->regmap, RT5120_REG_INTSTAT, &stat);
34 input_report_key(priv->input, KEY_POWER,
35 !(stat & RT5120_PWRKEYSTAT_MASK));
36 input_sync(priv->input);
41 static int rt5120_pwrkey_probe(struct platform_device *pdev)
43 struct rt5120_priv *priv;
44 struct device *dev = &pdev->dev;
45 int press_irq, release_irq;
48 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
52 priv->regmap = dev_get_regmap(dev->parent, NULL);
54 dev_err(dev, "Failed to init regmap\n");
58 press_irq = platform_get_irq_byname(pdev, "pwrkey-press");
62 release_irq = platform_get_irq_byname(pdev, "pwrkey-release");
66 /* Make input device be device resource managed */
67 priv->input = devm_input_allocate_device(dev);
71 priv->input->name = "rt5120_pwrkey";
72 priv->input->phys = "rt5120_pwrkey/input0";
73 priv->input->id.bustype = BUS_I2C;
74 input_set_capability(priv->input, EV_KEY, KEY_POWER);
76 error = input_register_device(priv->input);
78 dev_err(dev, "Failed to register input device: %d\n", error);
82 error = devm_request_threaded_irq(dev, press_irq,
83 NULL, rt5120_pwrkey_handler,
84 0, "pwrkey-press", priv);
87 "Failed to register pwrkey press irq: %d\n", error);
91 error = devm_request_threaded_irq(dev, release_irq,
92 NULL, rt5120_pwrkey_handler,
93 0, "pwrkey-release", priv);
96 "Failed to register pwrkey release irq: %d\n", error);
103 static const struct of_device_id r5120_pwrkey_match_table[] = {
104 { .compatible = "richtek,rt5120-pwrkey" },
107 MODULE_DEVICE_TABLE(of, r5120_pwrkey_match_table);
109 static struct platform_driver rt5120_pwrkey_driver = {
111 .name = "rt5120-pwrkey",
112 .of_match_table = r5120_pwrkey_match_table,
114 .probe = rt5120_pwrkey_probe,
116 module_platform_driver(rt5120_pwrkey_driver);
118 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
119 MODULE_DESCRIPTION("Richtek RT5120 power key driver");
120 MODULE_LICENSE("GPL");