1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
4  * Copyright (c) 2014, Sony Mobile Communications Inc.
5  */
6 
7 #include <linux/delay.h>
8 #include <linux/errno.h>
9 #include <linux/input.h>
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/log2.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/reboot.h>
18 #include <linux/regmap.h>
19 
20 #define PON_REV2			0x01
21 
22 #define PON_RT_STS			0x10
23 #define  PON_KPDPWR_N_SET		BIT(0)
24 #define  PON_RESIN_N_SET		BIT(1)
25 
26 #define PON_PS_HOLD_RST_CTL		0x5a
27 #define PON_PS_HOLD_RST_CTL2		0x5b
28 #define  PON_PS_HOLD_ENABLE		BIT(7)
29 #define  PON_PS_HOLD_TYPE_MASK		0x0f
30 #define  PON_PS_HOLD_TYPE_SHUTDOWN	4
31 #define  PON_PS_HOLD_TYPE_HARD_RESET	7
32 
33 #define PON_PULL_CTL			0x70
34 #define  PON_KPDPWR_PULL_UP		BIT(1)
35 #define  PON_RESIN_PULL_UP		BIT(0)
36 
37 #define PON_DBC_CTL			0x71
38 #define  PON_DBC_DELAY_MASK		0x7
39 
40 struct pm8941_data {
41 	unsigned int pull_up_bit;
42 	unsigned int status_bit;
43 };
44 
45 struct pm8941_pwrkey {
46 	struct device *dev;
47 	int irq;
48 	u32 baseaddr;
49 	struct regmap *regmap;
50 	struct input_dev *input;
51 
52 	unsigned int revision;
53 	struct notifier_block reboot_notifier;
54 
55 	u32 code;
56 	const struct pm8941_data *data;
57 };
58 
59 static int pm8941_reboot_notify(struct notifier_block *nb,
60 				unsigned long code, void *unused)
61 {
62 	struct pm8941_pwrkey *pwrkey = container_of(nb, struct pm8941_pwrkey,
63 						    reboot_notifier);
64 	unsigned int enable_reg;
65 	unsigned int reset_type;
66 	int error;
67 
68 	/* PMICs with revision 0 have the enable bit in same register as ctrl */
69 	if (pwrkey->revision == 0)
70 		enable_reg = PON_PS_HOLD_RST_CTL;
71 	else
72 		enable_reg = PON_PS_HOLD_RST_CTL2;
73 
74 	error = regmap_update_bits(pwrkey->regmap,
75 				   pwrkey->baseaddr + enable_reg,
76 				   PON_PS_HOLD_ENABLE,
77 				   0);
78 	if (error)
79 		dev_err(pwrkey->dev,
80 			"unable to clear ps hold reset enable: %d\n",
81 			error);
82 
83 	/*
84 	 * Updates of PON_PS_HOLD_ENABLE requires 3 sleep cycles between
85 	 * writes.
86 	 */
87 	usleep_range(100, 1000);
88 
89 	switch (code) {
90 	case SYS_HALT:
91 	case SYS_POWER_OFF:
92 		reset_type = PON_PS_HOLD_TYPE_SHUTDOWN;
93 		break;
94 	case SYS_RESTART:
95 	default:
96 		reset_type = PON_PS_HOLD_TYPE_HARD_RESET;
97 		break;
98 	}
99 
100 	error = regmap_update_bits(pwrkey->regmap,
101 				   pwrkey->baseaddr + PON_PS_HOLD_RST_CTL,
102 				   PON_PS_HOLD_TYPE_MASK,
103 				   reset_type);
104 	if (error)
105 		dev_err(pwrkey->dev, "unable to set ps hold reset type: %d\n",
106 			error);
107 
108 	error = regmap_update_bits(pwrkey->regmap,
109 				   pwrkey->baseaddr + enable_reg,
110 				   PON_PS_HOLD_ENABLE,
111 				   PON_PS_HOLD_ENABLE);
112 	if (error)
113 		dev_err(pwrkey->dev, "unable to re-set enable: %d\n", error);
114 
115 	return NOTIFY_DONE;
116 }
117 
118 static irqreturn_t pm8941_pwrkey_irq(int irq, void *_data)
119 {
120 	struct pm8941_pwrkey *pwrkey = _data;
121 	unsigned int sts;
122 	int error;
123 
124 	error = regmap_read(pwrkey->regmap,
125 			    pwrkey->baseaddr + PON_RT_STS, &sts);
126 	if (error)
127 		return IRQ_HANDLED;
128 
129 	input_report_key(pwrkey->input, pwrkey->code,
130 			 sts & pwrkey->data->status_bit);
131 	input_sync(pwrkey->input);
132 
133 	return IRQ_HANDLED;
134 }
135 
136 static int __maybe_unused pm8941_pwrkey_suspend(struct device *dev)
137 {
138 	struct pm8941_pwrkey *pwrkey = dev_get_drvdata(dev);
139 
140 	if (device_may_wakeup(dev))
141 		enable_irq_wake(pwrkey->irq);
142 
143 	return 0;
144 }
145 
146 static int __maybe_unused pm8941_pwrkey_resume(struct device *dev)
147 {
148 	struct pm8941_pwrkey *pwrkey = dev_get_drvdata(dev);
149 
150 	if (device_may_wakeup(dev))
151 		disable_irq_wake(pwrkey->irq);
152 
153 	return 0;
154 }
155 
156 static SIMPLE_DEV_PM_OPS(pm8941_pwr_key_pm_ops,
157 			 pm8941_pwrkey_suspend, pm8941_pwrkey_resume);
158 
159 static int pm8941_pwrkey_probe(struct platform_device *pdev)
160 {
161 	struct pm8941_pwrkey *pwrkey;
162 	bool pull_up;
163 	struct device *parent;
164 	u32 req_delay;
165 	int error;
166 
167 	if (of_property_read_u32(pdev->dev.of_node, "debounce", &req_delay))
168 		req_delay = 15625;
169 
170 	if (req_delay > 2000000 || req_delay == 0) {
171 		dev_err(&pdev->dev, "invalid debounce time: %u\n", req_delay);
172 		return -EINVAL;
173 	}
174 
175 	pull_up = of_property_read_bool(pdev->dev.of_node, "bias-pull-up");
176 
177 	pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
178 	if (!pwrkey)
179 		return -ENOMEM;
180 
181 	pwrkey->dev = &pdev->dev;
182 	pwrkey->data = of_device_get_match_data(&pdev->dev);
183 
184 	parent = pdev->dev.parent;
185 	pwrkey->regmap = dev_get_regmap(parent, NULL);
186 	if (!pwrkey->regmap) {
187 		/*
188 		 * We failed to get regmap for parent. Let's see if we are
189 		 * a child of pon node and read regmap and reg from its
190 		 * parent.
191 		 */
192 		pwrkey->regmap = dev_get_regmap(parent->parent, NULL);
193 		if (!pwrkey->regmap) {
194 			dev_err(&pdev->dev, "failed to locate regmap\n");
195 			return -ENODEV;
196 		}
197 
198 		error = of_property_read_u32(parent->of_node,
199 					     "reg", &pwrkey->baseaddr);
200 	} else {
201 		error = of_property_read_u32(pdev->dev.of_node, "reg",
202 					     &pwrkey->baseaddr);
203 	}
204 	if (error)
205 		return error;
206 
207 	pwrkey->irq = platform_get_irq(pdev, 0);
208 	if (pwrkey->irq < 0) {
209 		dev_err(&pdev->dev, "failed to get irq\n");
210 		return pwrkey->irq;
211 	}
212 
213 	error = regmap_read(pwrkey->regmap, pwrkey->baseaddr + PON_REV2,
214 			    &pwrkey->revision);
215 	if (error) {
216 		dev_err(&pdev->dev, "failed to set debounce: %d\n", error);
217 		return error;
218 	}
219 
220 	error = of_property_read_u32(pdev->dev.of_node, "linux,code",
221 				     &pwrkey->code);
222 	if (error) {
223 		dev_dbg(&pdev->dev,
224 			"no linux,code assuming power (%d)\n", error);
225 		pwrkey->code = KEY_POWER;
226 	}
227 
228 	pwrkey->input = devm_input_allocate_device(&pdev->dev);
229 	if (!pwrkey->input) {
230 		dev_dbg(&pdev->dev, "unable to allocate input device\n");
231 		return -ENOMEM;
232 	}
233 
234 	input_set_capability(pwrkey->input, EV_KEY, pwrkey->code);
235 
236 	pwrkey->input->name = "pm8941_pwrkey";
237 	pwrkey->input->phys = "pm8941_pwrkey/input0";
238 
239 	req_delay = (req_delay << 6) / USEC_PER_SEC;
240 	req_delay = ilog2(req_delay);
241 
242 	error = regmap_update_bits(pwrkey->regmap,
243 				   pwrkey->baseaddr + PON_DBC_CTL,
244 				   PON_DBC_DELAY_MASK,
245 				   req_delay);
246 	if (error) {
247 		dev_err(&pdev->dev, "failed to set debounce: %d\n", error);
248 		return error;
249 	}
250 
251 	error = regmap_update_bits(pwrkey->regmap,
252 				   pwrkey->baseaddr + PON_PULL_CTL,
253 				   pwrkey->data->pull_up_bit,
254 				   pull_up ? pwrkey->data->pull_up_bit : 0);
255 	if (error) {
256 		dev_err(&pdev->dev, "failed to set pull: %d\n", error);
257 		return error;
258 	}
259 
260 	error = devm_request_threaded_irq(&pdev->dev, pwrkey->irq,
261 					  NULL, pm8941_pwrkey_irq,
262 					  IRQF_ONESHOT,
263 					  "pm8941_pwrkey", pwrkey);
264 	if (error) {
265 		dev_err(&pdev->dev, "failed requesting IRQ: %d\n", error);
266 		return error;
267 	}
268 
269 	error = input_register_device(pwrkey->input);
270 	if (error) {
271 		dev_err(&pdev->dev, "failed to register input device: %d\n",
272 			error);
273 		return error;
274 	}
275 
276 	pwrkey->reboot_notifier.notifier_call = pm8941_reboot_notify,
277 	error = register_reboot_notifier(&pwrkey->reboot_notifier);
278 	if (error) {
279 		dev_err(&pdev->dev, "failed to register reboot notifier: %d\n",
280 			error);
281 		return error;
282 	}
283 
284 	platform_set_drvdata(pdev, pwrkey);
285 	device_init_wakeup(&pdev->dev, 1);
286 
287 	return 0;
288 }
289 
290 static int pm8941_pwrkey_remove(struct platform_device *pdev)
291 {
292 	struct pm8941_pwrkey *pwrkey = platform_get_drvdata(pdev);
293 
294 	unregister_reboot_notifier(&pwrkey->reboot_notifier);
295 
296 	return 0;
297 }
298 
299 static const struct pm8941_data pwrkey_data = {
300 	.pull_up_bit = PON_KPDPWR_PULL_UP,
301 	.status_bit = PON_KPDPWR_N_SET,
302 };
303 
304 static const struct pm8941_data resin_data = {
305 	.pull_up_bit = PON_RESIN_PULL_UP,
306 	.status_bit = PON_RESIN_N_SET,
307 };
308 
309 static const struct of_device_id pm8941_pwr_key_id_table[] = {
310 	{ .compatible = "qcom,pm8941-pwrkey", .data = &pwrkey_data },
311 	{ .compatible = "qcom,pm8941-resin", .data = &resin_data },
312 	{ }
313 };
314 MODULE_DEVICE_TABLE(of, pm8941_pwr_key_id_table);
315 
316 static struct platform_driver pm8941_pwrkey_driver = {
317 	.probe = pm8941_pwrkey_probe,
318 	.remove = pm8941_pwrkey_remove,
319 	.driver = {
320 		.name = "pm8941-pwrkey",
321 		.pm = &pm8941_pwr_key_pm_ops,
322 		.of_match_table = of_match_ptr(pm8941_pwr_key_id_table),
323 	},
324 };
325 module_platform_driver(pm8941_pwrkey_driver);
326 
327 MODULE_DESCRIPTION("PM8941 Power Key driver");
328 MODULE_LICENSE("GPL v2");
329