Linux 5.1.15
[linux/fpc-iii.git] / drivers / input / keyboard / snvs_pwrkey.c
blob4c67cf30a5d9ab14bff5f5c53d289ba347d241f4
1 // SPDX-License-Identifier: GPL-2.0+
2 //
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>
7 #include <linux/err.h>
8 #include <linux/init.h>
9 #include <linux/input.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/jiffies.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_address.h>
17 #include <linux/platform_device.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/regmap.h>
21 #define SNVS_LPSR_REG 0x4C /* LP Status Register */
22 #define SNVS_LPCR_REG 0x38 /* LP Control Register */
23 #define SNVS_HPSR_REG 0x14
24 #define SNVS_HPSR_BTN BIT(6)
25 #define SNVS_LPSR_SPO BIT(18)
26 #define SNVS_LPCR_DEP_EN BIT(5)
28 #define DEBOUNCE_TIME 30
29 #define REPEAT_INTERVAL 60
31 struct pwrkey_drv_data {
32 struct regmap *snvs;
33 int irq;
34 int keycode;
35 int keystate; /* 1:pressed */
36 int wakeup;
37 struct timer_list check_timer;
38 struct input_dev *input;
41 static void imx_imx_snvs_check_for_events(struct timer_list *t)
43 struct pwrkey_drv_data *pdata = from_timer(pdata, t, check_timer);
44 struct input_dev *input = pdata->input;
45 u32 state;
47 regmap_read(pdata->snvs, SNVS_HPSR_REG, &state);
48 state = state & SNVS_HPSR_BTN ? 1 : 0;
50 /* only report new event if status changed */
51 if (state ^ pdata->keystate) {
52 pdata->keystate = state;
53 input_event(input, EV_KEY, pdata->keycode, state);
54 input_sync(input);
55 pm_relax(pdata->input->dev.parent);
58 /* repeat check if pressed long */
59 if (state) {
60 mod_timer(&pdata->check_timer,
61 jiffies + msecs_to_jiffies(REPEAT_INTERVAL));
65 static irqreturn_t imx_snvs_pwrkey_interrupt(int irq, void *dev_id)
67 struct platform_device *pdev = dev_id;
68 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
69 u32 lp_status;
71 pm_wakeup_event(pdata->input->dev.parent, 0);
73 regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status);
74 if (lp_status & SNVS_LPSR_SPO)
75 mod_timer(&pdata->check_timer, jiffies + msecs_to_jiffies(DEBOUNCE_TIME));
77 /* clear SPO status */
78 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
80 return IRQ_HANDLED;
83 static void imx_snvs_pwrkey_act(void *pdata)
85 struct pwrkey_drv_data *pd = pdata;
87 del_timer_sync(&pd->check_timer);
90 static int imx_snvs_pwrkey_probe(struct platform_device *pdev)
92 struct pwrkey_drv_data *pdata = NULL;
93 struct input_dev *input = NULL;
94 struct device_node *np;
95 int error;
97 /* Get SNVS register Page */
98 np = pdev->dev.of_node;
99 if (!np)
100 return -ENODEV;
102 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
103 if (!pdata)
104 return -ENOMEM;
106 pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap");
107 if (IS_ERR(pdata->snvs)) {
108 dev_err(&pdev->dev, "Can't get snvs syscon\n");
109 return PTR_ERR(pdata->snvs);
112 if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) {
113 pdata->keycode = KEY_POWER;
114 dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n");
117 pdata->wakeup = of_property_read_bool(np, "wakeup-source");
119 pdata->irq = platform_get_irq(pdev, 0);
120 if (pdata->irq < 0) {
121 dev_err(&pdev->dev, "no irq defined in platform data\n");
122 return -EINVAL;
125 regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN);
127 /* clear the unexpected interrupt before driver ready */
128 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
130 timer_setup(&pdata->check_timer, imx_imx_snvs_check_for_events, 0);
132 input = devm_input_allocate_device(&pdev->dev);
133 if (!input) {
134 dev_err(&pdev->dev, "failed to allocate the input device\n");
135 return -ENOMEM;
138 input->name = pdev->name;
139 input->phys = "snvs-pwrkey/input0";
140 input->id.bustype = BUS_HOST;
142 input_set_capability(input, EV_KEY, pdata->keycode);
144 /* input customer action to cancel release timer */
145 error = devm_add_action(&pdev->dev, imx_snvs_pwrkey_act, pdata);
146 if (error) {
147 dev_err(&pdev->dev, "failed to register remove action\n");
148 return error;
151 pdata->input = input;
152 platform_set_drvdata(pdev, pdata);
154 error = devm_request_irq(&pdev->dev, pdata->irq,
155 imx_snvs_pwrkey_interrupt,
156 0, pdev->name, pdev);
158 if (error) {
159 dev_err(&pdev->dev, "interrupt not available.\n");
160 return error;
163 error = input_register_device(input);
164 if (error < 0) {
165 dev_err(&pdev->dev, "failed to register input device\n");
166 return error;
169 device_init_wakeup(&pdev->dev, pdata->wakeup);
171 return 0;
174 static int __maybe_unused imx_snvs_pwrkey_suspend(struct device *dev)
176 struct platform_device *pdev = to_platform_device(dev);
177 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
179 if (device_may_wakeup(&pdev->dev))
180 enable_irq_wake(pdata->irq);
182 return 0;
185 static int __maybe_unused imx_snvs_pwrkey_resume(struct device *dev)
187 struct platform_device *pdev = to_platform_device(dev);
188 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
190 if (device_may_wakeup(&pdev->dev))
191 disable_irq_wake(pdata->irq);
193 return 0;
196 static const struct of_device_id imx_snvs_pwrkey_ids[] = {
197 { .compatible = "fsl,sec-v4.0-pwrkey" },
198 { /* sentinel */ }
200 MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids);
202 static SIMPLE_DEV_PM_OPS(imx_snvs_pwrkey_pm_ops, imx_snvs_pwrkey_suspend,
203 imx_snvs_pwrkey_resume);
205 static struct platform_driver imx_snvs_pwrkey_driver = {
206 .driver = {
207 .name = "snvs_pwrkey",
208 .pm = &imx_snvs_pwrkey_pm_ops,
209 .of_match_table = imx_snvs_pwrkey_ids,
211 .probe = imx_snvs_pwrkey_probe,
213 module_platform_driver(imx_snvs_pwrkey_driver);
215 MODULE_AUTHOR("Freescale Semiconductor");
216 MODULE_DESCRIPTION("i.MX snvs power key Driver");
217 MODULE_LICENSE("GPL");