blob: 2f5e3ab5ed638c9f7d0691f40528b766cf794134 [file] [log] [blame]
Fabio Estevama9faa462018-06-19 11:19:14 -07001// 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.
Robin Gongd3dc6e22015-05-27 00:26:00 +08005
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>
Anson Huangfb3c09b2019-04-03 15:15:44 -070018#include <linux/pm_wakeirq.h>
Robin Gongd3dc6e22015-05-27 00:26:00 +080019#include <linux/mfd/syscon.h>
20#include <linux/regmap.h>
21
Robin van der Gracht1a26c922019-11-25 12:40:47 -080022#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)
Robin Gongd3dc6e22015-05-27 00:26:00 +080029
Robin van der Gracht1a26c922019-11-25 12:40:47 -080030#define DEBOUNCE_TIME 30
31#define REPEAT_INTERVAL 60
Robin Gongd3dc6e22015-05-27 00:26:00 +080032
33struct pwrkey_drv_data {
34 struct regmap *snvs;
35 int irq;
36 int keycode;
37 int keystate; /* 1:pressed */
38 int wakeup;
39 struct timer_list check_timer;
40 struct input_dev *input;
Robin van der Gracht1a26c922019-11-25 12:40:47 -080041 u8 minor_rev;
Robin Gongd3dc6e22015-05-27 00:26:00 +080042};
43
Kees Cook4ea402782017-10-24 09:40:57 -070044static void imx_imx_snvs_check_for_events(struct timer_list *t)
Robin Gongd3dc6e22015-05-27 00:26:00 +080045{
Kees Cook4ea402782017-10-24 09:40:57 -070046 struct pwrkey_drv_data *pdata = from_timer(pdata, t, check_timer);
Robin Gongd3dc6e22015-05-27 00:26:00 +080047 struct input_dev *input = pdata->input;
48 u32 state;
49
50 regmap_read(pdata->snvs, SNVS_HPSR_REG, &state);
51 state = state & SNVS_HPSR_BTN ? 1 : 0;
52
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);
57 input_sync(input);
58 pm_relax(pdata->input->dev.parent);
59 }
60
61 /* repeat check if pressed long */
62 if (state) {
63 mod_timer(&pdata->check_timer,
64 jiffies + msecs_to_jiffies(REPEAT_INTERVAL));
65 }
66}
67
68static irqreturn_t imx_snvs_pwrkey_interrupt(int irq, void *dev_id)
69{
70 struct platform_device *pdev = dev_id;
71 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
Robin van der Gracht1a26c922019-11-25 12:40:47 -080072 struct input_dev *input = pdata->input;
Robin Gongd3dc6e22015-05-27 00:26:00 +080073 u32 lp_status;
74
Robin van der Gracht1a26c922019-11-25 12:40:47 -080075 pm_wakeup_event(input->dev.parent, 0);
Robin Gongd3dc6e22015-05-27 00:26:00 +080076
77 regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status);
Robin van der Gracht1a26c922019-11-25 12:40:47 -080078 if (lp_status & SNVS_LPSR_SPO) {
79 if (pdata->minor_rev == 0) {
80 /*
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.
84 */
85 input_report_key(input, pdata->keycode, 1);
86 input_sync(input);
87 input_report_key(input, pdata->keycode, 0);
88 input_sync(input);
89 pm_relax(input->dev.parent);
90 } else {
91 mod_timer(&pdata->check_timer,
92 jiffies + msecs_to_jiffies(DEBOUNCE_TIME));
93 }
94 }
Robin Gongd3dc6e22015-05-27 00:26:00 +080095
96 /* clear SPO status */
97 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
98
99 return IRQ_HANDLED;
100}
101
102static void imx_snvs_pwrkey_act(void *pdata)
103{
104 struct pwrkey_drv_data *pd = pdata;
105
106 del_timer_sync(&pd->check_timer);
107}
108
109static int imx_snvs_pwrkey_probe(struct platform_device *pdev)
110{
Dmitry Torokhov1e55c172019-12-04 17:56:24 -0800111 struct pwrkey_drv_data *pdata;
112 struct input_dev *input;
Robin Gongd3dc6e22015-05-27 00:26:00 +0800113 struct device_node *np;
114 int error;
Robin van der Gracht1a26c922019-11-25 12:40:47 -0800115 u32 vid;
Robin Gongd3dc6e22015-05-27 00:26:00 +0800116
117 /* Get SNVS register Page */
118 np = pdev->dev.of_node;
119 if (!np)
120 return -ENODEV;
121
122 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
123 if (!pdata)
124 return -ENOMEM;
125
Javier Martinez Canillas9ace41a2015-10-26 01:45:01 -0700126 pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap");
Vladimir Zapolskiy9979c1c2016-03-08 11:06:39 -0800127 if (IS_ERR(pdata->snvs)) {
Robin Gongd3dc6e22015-05-27 00:26:00 +0800128 dev_err(&pdev->dev, "Can't get snvs syscon\n");
Vladimir Zapolskiy9979c1c2016-03-08 11:06:39 -0800129 return PTR_ERR(pdata->snvs);
Robin Gongd3dc6e22015-05-27 00:26:00 +0800130 }
131
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");
135 }
136
Shawn Guodef56bb2015-07-15 10:36:37 +0800137 pdata->wakeup = of_property_read_bool(np, "wakeup-source");
Robin Gongd3dc6e22015-05-27 00:26:00 +0800138
139 pdata->irq = platform_get_irq(pdev, 0);
Stephen Boyd0bec8b72019-08-14 10:46:38 -0700140 if (pdata->irq < 0)
Robin Gongd3dc6e22015-05-27 00:26:00 +0800141 return -EINVAL;
Robin Gongd3dc6e22015-05-27 00:26:00 +0800142
Robin van der Gracht1a26c922019-11-25 12:40:47 -0800143 regmap_read(pdata->snvs, SNVS_HPVIDR1_REG, &vid);
144 pdata->minor_rev = vid & 0xff;
145
Robin Gongd3dc6e22015-05-27 00:26:00 +0800146 regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN);
147
148 /* clear the unexpected interrupt before driver ready */
149 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
150
Kees Cook4ea402782017-10-24 09:40:57 -0700151 timer_setup(&pdata->check_timer, imx_imx_snvs_check_for_events, 0);
Robin Gongd3dc6e22015-05-27 00:26:00 +0800152
153 input = devm_input_allocate_device(&pdev->dev);
154 if (!input) {
155 dev_err(&pdev->dev, "failed to allocate the input device\n");
156 return -ENOMEM;
157 }
158
159 input->name = pdev->name;
160 input->phys = "snvs-pwrkey/input0";
161 input->id.bustype = BUS_HOST;
162
163 input_set_capability(input, EV_KEY, pdata->keycode);
164
165 /* input customer action to cancel release timer */
166 error = devm_add_action(&pdev->dev, imx_snvs_pwrkey_act, pdata);
167 if (error) {
168 dev_err(&pdev->dev, "failed to register remove action\n");
169 return error;
170 }
171
Anson Huangbf2a7ca2019-04-03 15:14:44 -0700172 pdata->input = input;
173 platform_set_drvdata(pdev, pdata);
174
Robin Gongd3dc6e22015-05-27 00:26:00 +0800175 error = devm_request_irq(&pdev->dev, pdata->irq,
176 imx_snvs_pwrkey_interrupt,
177 0, pdev->name, pdev);
178
179 if (error) {
180 dev_err(&pdev->dev, "interrupt not available.\n");
181 return error;
182 }
183
184 error = input_register_device(input);
185 if (error < 0) {
186 dev_err(&pdev->dev, "failed to register input device\n");
Robin Gongd3dc6e22015-05-27 00:26:00 +0800187 return error;
188 }
189
Robin Gongd3dc6e22015-05-27 00:26:00 +0800190 device_init_wakeup(&pdev->dev, pdata->wakeup);
Anson Huangfb3c09b2019-04-03 15:15:44 -0700191 error = dev_pm_set_wake_irq(&pdev->dev, pdata->irq);
192 if (error)
193 dev_err(&pdev->dev, "irq wake enable failed.\n");
Robin Gongd3dc6e22015-05-27 00:26:00 +0800194
195 return 0;
196}
197
198static const struct of_device_id imx_snvs_pwrkey_ids[] = {
199 { .compatible = "fsl,sec-v4.0-pwrkey" },
200 { /* sentinel */ }
201};
202MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids);
203
Robin Gongd3dc6e22015-05-27 00:26:00 +0800204static struct platform_driver imx_snvs_pwrkey_driver = {
205 .driver = {
206 .name = "snvs_pwrkey",
Robin Gongd3dc6e22015-05-27 00:26:00 +0800207 .of_match_table = imx_snvs_pwrkey_ids,
208 },
209 .probe = imx_snvs_pwrkey_probe,
210};
211module_platform_driver(imx_snvs_pwrkey_driver);
212
213MODULE_AUTHOR("Freescale Semiconductor");
214MODULE_DESCRIPTION("i.MX snvs power key Driver");
215MODULE_LICENSE("GPL");