• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
5 
6 #include <linux/clk.h>
7 #include <linux/device.h>
8 #include <linux/err.h>
9 #include <linux/init.h>
10 #include <linux/input.h>
11 #include <linux/interrupt.h>
12 #include <linux/io.h>
13 #include <linux/jiffies.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_wakeirq.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/regmap.h>
22 
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)
29 
30 #define DEBOUNCE_TIME 30
31 #define REPEAT_INTERVAL 60
32 
33 struct 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;
41 };
42 
imx_imx_snvs_check_for_events(struct timer_list * t)43 static void imx_imx_snvs_check_for_events(struct timer_list *t)
44 {
45 	struct pwrkey_drv_data *pdata = from_timer(pdata, t, check_timer);
46 	struct input_dev *input = pdata->input;
47 	u32 state;
48 
49 	regmap_read(pdata->snvs, SNVS_HPSR_REG, &state);
50 	state = state & SNVS_HPSR_BTN ? 1 : 0;
51 
52 	/* only report new event if status changed */
53 	if (state ^ pdata->keystate) {
54 		pdata->keystate = state;
55 		input_event(input, EV_KEY, pdata->keycode, state);
56 		input_sync(input);
57 		pm_relax(pdata->input->dev.parent);
58 	}
59 
60 	/* repeat check if pressed long */
61 	if (state) {
62 		mod_timer(&pdata->check_timer,
63 			  jiffies + msecs_to_jiffies(REPEAT_INTERVAL));
64 	}
65 }
66 
imx_snvs_pwrkey_interrupt(int irq,void * dev_id)67 static irqreturn_t imx_snvs_pwrkey_interrupt(int irq, void *dev_id)
68 {
69 	struct platform_device *pdev = dev_id;
70 	struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
71 	u32 lp_status;
72 
73 	pm_wakeup_event(pdata->input->dev.parent, 0);
74 
75 	regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status);
76 	if (lp_status & SNVS_LPSR_SPO)
77 		mod_timer(&pdata->check_timer, jiffies + msecs_to_jiffies(DEBOUNCE_TIME));
78 
79 	/* clear SPO status */
80 	regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
81 
82 	return IRQ_HANDLED;
83 }
84 
imx_snvs_pwrkey_disable_clk(void * data)85 static void imx_snvs_pwrkey_disable_clk(void *data)
86 {
87 	clk_disable_unprepare(data);
88 }
89 
imx_snvs_pwrkey_act(void * pdata)90 static void imx_snvs_pwrkey_act(void *pdata)
91 {
92 	struct pwrkey_drv_data *pd = pdata;
93 
94 	del_timer_sync(&pd->check_timer);
95 }
96 
imx_snvs_pwrkey_probe(struct platform_device * pdev)97 static int imx_snvs_pwrkey_probe(struct platform_device *pdev)
98 {
99 	struct pwrkey_drv_data *pdata = NULL;
100 	struct input_dev *input = NULL;
101 	struct device_node *np;
102 	struct clk *clk;
103 	int error;
104 
105 	/* Get SNVS register Page */
106 	np = pdev->dev.of_node;
107 	if (!np)
108 		return -ENODEV;
109 
110 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
111 	if (!pdata)
112 		return -ENOMEM;
113 
114 	pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap");
115 	if (IS_ERR(pdata->snvs)) {
116 		dev_err(&pdev->dev, "Can't get snvs syscon\n");
117 		return PTR_ERR(pdata->snvs);
118 	}
119 
120 	if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) {
121 		pdata->keycode = KEY_POWER;
122 		dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n");
123 	}
124 
125 	clk = devm_clk_get_optional(&pdev->dev, NULL);
126 	if (IS_ERR(clk)) {
127 		dev_err(&pdev->dev, "Failed to get snvs clock (%pe)\n", clk);
128 		return PTR_ERR(clk);
129 	}
130 
131 	error = clk_prepare_enable(clk);
132 	if (error) {
133 		dev_err(&pdev->dev, "Failed to enable snvs clock (%pe)\n",
134 			ERR_PTR(error));
135 		return error;
136 	}
137 
138 	error = devm_add_action_or_reset(&pdev->dev,
139 					 imx_snvs_pwrkey_disable_clk, clk);
140 	if (error) {
141 		dev_err(&pdev->dev,
142 			"Failed to register clock cleanup handler (%pe)\n",
143 			ERR_PTR(error));
144 		return error;
145 	}
146 
147 	pdata->wakeup = of_property_read_bool(np, "wakeup-source");
148 
149 	pdata->irq = platform_get_irq(pdev, 0);
150 	if (pdata->irq < 0)
151 		return -EINVAL;
152 
153 	regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN);
154 
155 	/* clear the unexpected interrupt before driver ready */
156 	regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
157 
158 	timer_setup(&pdata->check_timer, imx_imx_snvs_check_for_events, 0);
159 
160 	input = devm_input_allocate_device(&pdev->dev);
161 	if (!input) {
162 		dev_err(&pdev->dev, "failed to allocate the input device\n");
163 		return -ENOMEM;
164 	}
165 
166 	input->name = pdev->name;
167 	input->phys = "snvs-pwrkey/input0";
168 	input->id.bustype = BUS_HOST;
169 
170 	input_set_capability(input, EV_KEY, pdata->keycode);
171 
172 	/* input customer action to cancel release timer */
173 	error = devm_add_action(&pdev->dev, imx_snvs_pwrkey_act, pdata);
174 	if (error) {
175 		dev_err(&pdev->dev, "failed to register remove action\n");
176 		return error;
177 	}
178 
179 	pdata->input = input;
180 	platform_set_drvdata(pdev, pdata);
181 
182 	error = devm_request_irq(&pdev->dev, pdata->irq,
183 			       imx_snvs_pwrkey_interrupt,
184 			       0, pdev->name, pdev);
185 
186 	if (error) {
187 		dev_err(&pdev->dev, "interrupt not available.\n");
188 		return error;
189 	}
190 
191 	error = input_register_device(input);
192 	if (error < 0) {
193 		dev_err(&pdev->dev, "failed to register input device\n");
194 		return error;
195 	}
196 
197 	device_init_wakeup(&pdev->dev, pdata->wakeup);
198 	error = dev_pm_set_wake_irq(&pdev->dev, pdata->irq);
199 	if (error)
200 		dev_err(&pdev->dev, "irq wake enable failed.\n");
201 
202 	return 0;
203 }
204 
205 static const struct of_device_id imx_snvs_pwrkey_ids[] = {
206 	{ .compatible = "fsl,sec-v4.0-pwrkey" },
207 	{ /* sentinel */ }
208 };
209 MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids);
210 
211 static struct platform_driver imx_snvs_pwrkey_driver = {
212 	.driver = {
213 		.name = "snvs_pwrkey",
214 		.of_match_table = imx_snvs_pwrkey_ids,
215 	},
216 	.probe = imx_snvs_pwrkey_probe,
217 };
218 module_platform_driver(imx_snvs_pwrkey_driver);
219 
220 MODULE_AUTHOR("Freescale Semiconductor");
221 MODULE_DESCRIPTION("i.MX snvs power key Driver");
222 MODULE_LICENSE("GPL");
223