]> git.baikalelectronics.ru Git - kernel.git/commitdiff
Input: rt5120 - add power key support
authorChiYuan Huang <cy_huang@richtek.com>
Fri, 12 Aug 2022 19:36:39 +0000 (12:36 -0700)
committerDmitry Torokhov <dmitry.torokhov@gmail.com>
Wed, 31 Aug 2022 22:48:49 +0000 (15:48 -0700)
Add RT5120 PMIC power key support.

Signed-off-by: ChiYuan Huang <cy_huang@richtek.com>
Link: https://lore.kernel.org/r/1660100142-32493-4-git-send-email-u0084500@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
drivers/input/misc/Kconfig
drivers/input/misc/Makefile
drivers/input/misc/rt5120-pwrkey.c [new file with mode: 0644]

index 968240288c61c89f0013490f0a4b751e71c74980..9f088900f863b7f1f37c738a6172828fae4cf423 100644 (file)
@@ -909,6 +909,15 @@ config INPUT_SC27XX_VIBRA
          To compile this driver as a module, choose M here. The module will
          be called sc27xx_vibra.
 
+config INPUT_RT5120_PWRKEY
+       tristate "RT5120 PMIC power key support"
+       depends on MFD_RT5120 || COMPILE_TEST
+       help
+         This enables support for RT5120 PMIC power key driver.
+
+         To compile this driver as a module, choose M here. the module will
+         be called rt5120-pwrkey.
+
 config INPUT_STPMIC1_ONKEY
        tristate "STPMIC1 PMIC Onkey support"
        depends on MFD_STPMIC1
index 9eea13e98d48056e0c58f1a8c491425f81988838..6abefc41037b532faa35144d3c5c58c393ec5b17 100644 (file)
@@ -70,6 +70,7 @@ obj-$(CONFIG_INPUT_RAVE_SP_PWRBUTTON) += rave-sp-pwrbutton.o
 obj-$(CONFIG_INPUT_RB532_BUTTON)       += rb532_button.o
 obj-$(CONFIG_INPUT_REGULATOR_HAPTIC)   += regulator-haptic.o
 obj-$(CONFIG_INPUT_RETU_PWRBUTTON)     += retu-pwrbutton.o
+obj-$(CONFIG_INPUT_RT5120_PWRKEY)      += rt5120-pwrkey.o
 obj-$(CONFIG_INPUT_AXP20X_PEK)         += axp20x-pek.o
 obj-$(CONFIG_INPUT_GPIO_ROTARY_ENCODER)        += rotary_encoder.o
 obj-$(CONFIG_INPUT_RK805_PWRKEY)       += rk805-pwrkey.o
diff --git a/drivers/input/misc/rt5120-pwrkey.c b/drivers/input/misc/rt5120-pwrkey.c
new file mode 100644 (file)
index 0000000..8a8c1ae
--- /dev/null
@@ -0,0 +1,120 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ * Author: ChiYuan Huang <cy_huang@richtek.com>
+ */
+
+#include <linux/bits.h>
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+
+#define RT5120_REG_INTSTAT     0x1E
+#define RT5120_PWRKEYSTAT_MASK BIT(7)
+
+struct rt5120_priv {
+       struct regmap *regmap;
+       struct input_dev *input;
+};
+
+static irqreturn_t rt5120_pwrkey_handler(int irq, void *devid)
+{
+       struct rt5120_priv *priv = devid;
+       unsigned int stat;
+       int error;
+
+       error = regmap_read(priv->regmap, RT5120_REG_INTSTAT, &stat);
+       if (error)
+               return IRQ_NONE;
+
+       input_report_key(priv->input, KEY_POWER,
+                        !(stat & RT5120_PWRKEYSTAT_MASK));
+       input_sync(priv->input);
+
+       return IRQ_HANDLED;
+}
+
+static int rt5120_pwrkey_probe(struct platform_device *pdev)
+{
+       struct rt5120_priv *priv;
+       struct device *dev = &pdev->dev;
+       int press_irq, release_irq;
+       int error;
+
+       priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+       if (!priv)
+               return -ENOMEM;
+
+       priv->regmap = dev_get_regmap(dev->parent, NULL);
+       if (!priv->regmap) {
+               dev_err(dev, "Failed to init regmap\n");
+               return -ENODEV;
+       }
+
+       press_irq = platform_get_irq_byname(pdev, "pwrkey-press");
+       if (press_irq < 0)
+               return press_irq;
+
+       release_irq = platform_get_irq_byname(pdev, "pwrkey-release");
+       if (release_irq < 0)
+               return release_irq;
+
+       /* Make input device be device resource managed */
+       priv->input = devm_input_allocate_device(dev);
+       if (!priv->input)
+               return -ENOMEM;
+
+       priv->input->name = "rt5120_pwrkey";
+       priv->input->phys = "rt5120_pwrkey/input0";
+       priv->input->id.bustype = BUS_I2C;
+       input_set_capability(priv->input, EV_KEY, KEY_POWER);
+
+       error = input_register_device(priv->input);
+       if (error) {
+               dev_err(dev, "Failed to register input device: %d\n", error);
+               return error;
+       }
+
+       error = devm_request_threaded_irq(dev, press_irq,
+                                         NULL, rt5120_pwrkey_handler,
+                                         0, "pwrkey-press", priv);
+       if (error) {
+               dev_err(dev,
+                       "Failed to register pwrkey press irq: %d\n", error);
+               return error;
+       }
+
+       error = devm_request_threaded_irq(dev, release_irq,
+                                         NULL, rt5120_pwrkey_handler,
+                                         0, "pwrkey-release", priv);
+       if (error) {
+               dev_err(dev,
+                       "Failed to register pwrkey release irq: %d\n", error);
+               return error;
+       }
+
+       return 0;
+}
+
+static const struct of_device_id r5120_pwrkey_match_table[] = {
+       { .compatible = "richtek,rt5120-pwrkey" },
+       {}
+};
+MODULE_DEVICE_TABLE(of, r5120_pwrkey_match_table);
+
+static struct platform_driver rt5120_pwrkey_driver = {
+       .driver = {
+               .name = "rt5120-pwrkey",
+               .of_match_table = r5120_pwrkey_match_table,
+       },
+       .probe = rt5120_pwrkey_probe,
+};
+module_platform_driver(rt5120_pwrkey_driver);
+
+MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
+MODULE_DESCRIPTION("Richtek RT5120 power key driver");
+MODULE_LICENSE("GPL");