Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / input / keyboard / snvs_pwrkey.c
blob5342d8d45f811b399bb740ec4a0158d50c502c44
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/pm_wakeirq.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/regmap.h>
22 #define SNVS_LPSR_REG 0x4C /* LP Status Register */
23 #define SNVS_LPCR_REG 0x38 /* LP Control Register */
24 #define SNVS_HPSR_REG 0x14
25 #define SNVS_HPSR_BTN BIT(6)
26 #define SNVS_LPSR_SPO BIT(18)
27 #define SNVS_LPCR_DEP_EN BIT(5)
29 #define DEBOUNCE_TIME 30
30 #define REPEAT_INTERVAL 60
32 struct pwrkey_drv_data {
33 struct regmap *snvs;
34 int irq;
35 int keycode;
36 int keystate; /* 1:pressed */
37 int wakeup;
38 struct timer_list check_timer;
39 struct input_dev *input;
42 static void imx_imx_snvs_check_for_events(struct timer_list *t)
44 struct pwrkey_drv_data *pdata = from_timer(pdata, t, check_timer);
45 struct input_dev *input = pdata->input;
46 u32 state;
48 regmap_read(pdata->snvs, SNVS_HPSR_REG, &state);
49 state = state & SNVS_HPSR_BTN ? 1 : 0;
51 /* only report new event if status changed */
52 if (state ^ pdata->keystate) {
53 pdata->keystate = state;
54 input_event(input, EV_KEY, pdata->keycode, state);
55 input_sync(input);
56 pm_relax(pdata->input->dev.parent);
59 /* repeat check if pressed long */
60 if (state) {
61 mod_timer(&pdata->check_timer,
62 jiffies + msecs_to_jiffies(REPEAT_INTERVAL));
66 static irqreturn_t imx_snvs_pwrkey_interrupt(int irq, void *dev_id)
68 struct platform_device *pdev = dev_id;
69 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
70 u32 lp_status;
72 pm_wakeup_event(pdata->input->dev.parent, 0);
74 regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status);
75 if (lp_status & SNVS_LPSR_SPO)
76 mod_timer(&pdata->check_timer, jiffies + msecs_to_jiffies(DEBOUNCE_TIME));
78 /* clear SPO status */
79 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
81 return IRQ_HANDLED;
84 static void imx_snvs_pwrkey_act(void *pdata)
86 struct pwrkey_drv_data *pd = pdata;
88 del_timer_sync(&pd->check_timer);
91 static int imx_snvs_pwrkey_probe(struct platform_device *pdev)
93 struct pwrkey_drv_data *pdata = NULL;
94 struct input_dev *input = NULL;
95 struct device_node *np;
96 int error;
98 /* Get SNVS register Page */
99 np = pdev->dev.of_node;
100 if (!np)
101 return -ENODEV;
103 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
104 if (!pdata)
105 return -ENOMEM;
107 pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap");
108 if (IS_ERR(pdata->snvs)) {
109 dev_err(&pdev->dev, "Can't get snvs syscon\n");
110 return PTR_ERR(pdata->snvs);
113 if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) {
114 pdata->keycode = KEY_POWER;
115 dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n");
118 pdata->wakeup = of_property_read_bool(np, "wakeup-source");
120 pdata->irq = platform_get_irq(pdev, 0);
121 if (pdata->irq < 0) {
122 dev_err(&pdev->dev, "no irq defined in platform data\n");
123 return -EINVAL;
126 regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN);
128 /* clear the unexpected interrupt before driver ready */
129 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
131 timer_setup(&pdata->check_timer, imx_imx_snvs_check_for_events, 0);
133 input = devm_input_allocate_device(&pdev->dev);
134 if (!input) {
135 dev_err(&pdev->dev, "failed to allocate the input device\n");
136 return -ENOMEM;
139 input->name = pdev->name;
140 input->phys = "snvs-pwrkey/input0";
141 input->id.bustype = BUS_HOST;
143 input_set_capability(input, EV_KEY, pdata->keycode);
145 /* input customer action to cancel release timer */
146 error = devm_add_action(&pdev->dev, imx_snvs_pwrkey_act, pdata);
147 if (error) {
148 dev_err(&pdev->dev, "failed to register remove action\n");
149 return error;
152 pdata->input = input;
153 platform_set_drvdata(pdev, pdata);
155 error = devm_request_irq(&pdev->dev, pdata->irq,
156 imx_snvs_pwrkey_interrupt,
157 0, pdev->name, pdev);
159 if (error) {
160 dev_err(&pdev->dev, "interrupt not available.\n");
161 return error;
164 error = input_register_device(input);
165 if (error < 0) {
166 dev_err(&pdev->dev, "failed to register input device\n");
167 return error;
170 device_init_wakeup(&pdev->dev, pdata->wakeup);
171 error = dev_pm_set_wake_irq(&pdev->dev, pdata->irq);
172 if (error)
173 dev_err(&pdev->dev, "irq wake enable failed.\n");
175 return 0;
178 static const struct of_device_id imx_snvs_pwrkey_ids[] = {
179 { .compatible = "fsl,sec-v4.0-pwrkey" },
180 { /* sentinel */ }
182 MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids);
184 static struct platform_driver imx_snvs_pwrkey_driver = {
185 .driver = {
186 .name = "snvs_pwrkey",
187 .of_match_table = imx_snvs_pwrkey_ids,
189 .probe = imx_snvs_pwrkey_probe,
191 module_platform_driver(imx_snvs_pwrkey_driver);
193 MODULE_AUTHOR("Freescale Semiconductor");
194 MODULE_DESCRIPTION("i.MX snvs power key Driver");
195 MODULE_LICENSE("GPL");