1 // SPDX-License-Identifier: GPL-2.0+
3 // Driver for the IMX SNVS ON/OFF Power Key
4 // Copyright (C) 2015 Freescale Semiconductor, Inc. All Rights Reserved.
6 #include <linux/device.h>
8 #include <linux/init.h>
9 #include <linux/input.h>
10 #include <linux/interrupt.h>
12 #include <linux/jiffies.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_wakeirq.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/regmap.h>
22 #define SNVS_HPVIDR1_REG 0xF8
23 #define SNVS_LPSR_REG 0x4C /* LP Status Register */
24 #define SNVS_LPCR_REG 0x38 /* LP Control Register */
25 #define SNVS_HPSR_REG 0x14
26 #define SNVS_HPSR_BTN BIT(6)
27 #define SNVS_LPSR_SPO BIT(18)
28 #define SNVS_LPCR_DEP_EN BIT(5)
30 #define DEBOUNCE_TIME 30
31 #define REPEAT_INTERVAL 60
33 struct pwrkey_drv_data {
37 int keystate; /* 1:pressed */
39 struct timer_list check_timer;
40 struct input_dev *input;
44 static void imx_imx_snvs_check_for_events(struct timer_list *t)
46 struct pwrkey_drv_data *pdata = from_timer(pdata, t, check_timer);
47 struct input_dev *input = pdata->input;
50 regmap_read(pdata->snvs, SNVS_HPSR_REG, &state);
51 state = state & SNVS_HPSR_BTN ? 1 : 0;
53 /* only report new event if status changed */
54 if (state ^ pdata->keystate) {
55 pdata->keystate = state;
56 input_event(input, EV_KEY, pdata->keycode, state);
58 pm_relax(pdata->input->dev.parent);
61 /* repeat check if pressed long */
63 mod_timer(&pdata->check_timer,
64 jiffies + msecs_to_jiffies(REPEAT_INTERVAL));
68 static irqreturn_t imx_snvs_pwrkey_interrupt(int irq, void *dev_id)
70 struct platform_device *pdev = dev_id;
71 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
72 struct input_dev *input = pdata->input;
75 pm_wakeup_event(input->dev.parent, 0);
77 regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status);
78 if (lp_status & SNVS_LPSR_SPO) {
79 if (pdata->minor_rev == 0) {
81 * The first generation i.MX6 SoCs only sends an
82 * interrupt on button release. To mimic power-key
83 * usage, we'll prepend a press event.
85 input_report_key(input, pdata->keycode, 1);
87 input_report_key(input, pdata->keycode, 0);
89 pm_relax(input->dev.parent);
91 mod_timer(&pdata->check_timer,
92 jiffies + msecs_to_jiffies(DEBOUNCE_TIME));
96 /* clear SPO status */
97 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
102 static void imx_snvs_pwrkey_act(void *pdata)
104 struct pwrkey_drv_data *pd = pdata;
106 del_timer_sync(&pd->check_timer);
109 static int imx_snvs_pwrkey_probe(struct platform_device *pdev)
111 struct pwrkey_drv_data *pdata;
112 struct input_dev *input;
113 struct device_node *np;
117 /* Get SNVS register Page */
118 np = pdev->dev.of_node;
122 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
126 pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap");
127 if (IS_ERR(pdata->snvs)) {
128 dev_err(&pdev->dev, "Can't get snvs syscon\n");
129 return PTR_ERR(pdata->snvs);
132 if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) {
133 pdata->keycode = KEY_POWER;
134 dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n");
137 pdata->wakeup = of_property_read_bool(np, "wakeup-source");
139 pdata->irq = platform_get_irq(pdev, 0);
143 regmap_read(pdata->snvs, SNVS_HPVIDR1_REG, &vid);
144 pdata->minor_rev = vid & 0xff;
146 regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN);
148 /* clear the unexpected interrupt before driver ready */
149 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
151 timer_setup(&pdata->check_timer, imx_imx_snvs_check_for_events, 0);
153 input = devm_input_allocate_device(&pdev->dev);
155 dev_err(&pdev->dev, "failed to allocate the input device\n");
159 input->name = pdev->name;
160 input->phys = "snvs-pwrkey/input0";
161 input->id.bustype = BUS_HOST;
163 input_set_capability(input, EV_KEY, pdata->keycode);
165 /* input customer action to cancel release timer */
166 error = devm_add_action(&pdev->dev, imx_snvs_pwrkey_act, pdata);
168 dev_err(&pdev->dev, "failed to register remove action\n");
172 pdata->input = input;
173 platform_set_drvdata(pdev, pdata);
175 error = devm_request_irq(&pdev->dev, pdata->irq,
176 imx_snvs_pwrkey_interrupt,
177 0, pdev->name, pdev);
180 dev_err(&pdev->dev, "interrupt not available.\n");
184 error = input_register_device(input);
186 dev_err(&pdev->dev, "failed to register input device\n");
190 device_init_wakeup(&pdev->dev, pdata->wakeup);
191 error = dev_pm_set_wake_irq(&pdev->dev, pdata->irq);
193 dev_err(&pdev->dev, "irq wake enable failed.\n");
198 static const struct of_device_id imx_snvs_pwrkey_ids[] = {
199 { .compatible = "fsl,sec-v4.0-pwrkey" },
202 MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids);
204 static struct platform_driver imx_snvs_pwrkey_driver = {
206 .name = "snvs_pwrkey",
207 .of_match_table = imx_snvs_pwrkey_ids,
209 .probe = imx_snvs_pwrkey_probe,
211 module_platform_driver(imx_snvs_pwrkey_driver);
213 MODULE_AUTHOR("Freescale Semiconductor");
214 MODULE_DESCRIPTION("i.MX snvs power key Driver");
215 MODULE_LICENSE("GPL");