1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
3  */
4 
5 #include <linux/module.h>
6 #include <linux/kernel.h>
7 #include <linux/errno.h>
8 #include <linux/slab.h>
9 #include <linux/input.h>
10 #include <linux/interrupt.h>
11 #include <linux/platform_device.h>
12 #include <linux/regmap.h>
13 #include <linux/log2.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 
17 #define PON_CNTL_1 0x1C
18 #define PON_CNTL_PULL_UP BIT(7)
19 #define PON_CNTL_TRIG_DELAY_MASK (0x7)
20 #define PON_CNTL_1_PULL_UP_EN			0xe0
21 #define PON_CNTL_1_USB_PWR_EN			0x10
22 #define PON_CNTL_1_WD_EN_RESET			0x08
23 
24 #define PM8058_SLEEP_CTRL			0x02b
25 #define PM8921_SLEEP_CTRL			0x10a
26 
27 #define SLEEP_CTRL_SMPL_EN_RESET		0x04
28 
29 /* Regulator master enable addresses */
30 #define REG_PM8058_VREG_EN_MSM			0x018
31 #define REG_PM8058_VREG_EN_GRP_5_4		0x1c8
32 
33 /* Regulator control registers for shutdown/reset */
34 #define PM8058_S0_CTRL				0x004
35 #define PM8058_S1_CTRL				0x005
36 #define PM8058_S3_CTRL				0x111
37 #define PM8058_L21_CTRL				0x120
38 #define PM8058_L22_CTRL				0x121
39 
40 #define PM8058_REGULATOR_ENABLE_MASK		0x80
41 #define PM8058_REGULATOR_ENABLE			0x80
42 #define PM8058_REGULATOR_DISABLE		0x00
43 #define PM8058_REGULATOR_PULL_DOWN_MASK		0x40
44 #define PM8058_REGULATOR_PULL_DOWN_EN		0x40
45 
46 /* Buck CTRL register */
47 #define PM8058_SMPS_LEGACY_VREF_SEL		0x20
48 #define PM8058_SMPS_LEGACY_VPROG_MASK		0x1f
49 #define PM8058_SMPS_ADVANCED_BAND_MASK		0xC0
50 #define PM8058_SMPS_ADVANCED_BAND_SHIFT		6
51 #define PM8058_SMPS_ADVANCED_VPROG_MASK		0x3f
52 
53 /* Buck TEST2 registers for shutdown/reset */
54 #define PM8058_S0_TEST2				0x084
55 #define PM8058_S1_TEST2				0x085
56 #define PM8058_S3_TEST2				0x11a
57 
58 #define PM8058_REGULATOR_BANK_WRITE		0x80
59 #define PM8058_REGULATOR_BANK_MASK		0x70
60 #define PM8058_REGULATOR_BANK_SHIFT		4
61 #define PM8058_REGULATOR_BANK_SEL(n)	((n) << PM8058_REGULATOR_BANK_SHIFT)
62 
63 /* Buck TEST2 register bank 1 */
64 #define PM8058_SMPS_LEGACY_VLOW_SEL		0x01
65 
66 /* Buck TEST2 register bank 7 */
67 #define PM8058_SMPS_ADVANCED_MODE_MASK		0x02
68 #define PM8058_SMPS_ADVANCED_MODE		0x02
69 #define PM8058_SMPS_LEGACY_MODE			0x00
70 
71 /**
72  * struct pmic8xxx_pwrkey - pmic8xxx pwrkey information
73  * @key_press_irq: key press irq number
74  * @regmap: device regmap
75  * @shutdown_fn: shutdown configuration function
76  */
77 struct pmic8xxx_pwrkey {
78 	int key_press_irq;
79 	struct regmap *regmap;
80 	int (*shutdown_fn)(struct pmic8xxx_pwrkey *, bool);
81 };
82 
83 static irqreturn_t pwrkey_press_irq(int irq, void *_pwr)
84 {
85 	struct input_dev *pwr = _pwr;
86 
87 	input_report_key(pwr, KEY_POWER, 1);
88 	input_sync(pwr);
89 
90 	return IRQ_HANDLED;
91 }
92 
93 static irqreturn_t pwrkey_release_irq(int irq, void *_pwr)
94 {
95 	struct input_dev *pwr = _pwr;
96 
97 	input_report_key(pwr, KEY_POWER, 0);
98 	input_sync(pwr);
99 
100 	return IRQ_HANDLED;
101 }
102 
103 static int __maybe_unused pmic8xxx_pwrkey_suspend(struct device *dev)
104 {
105 	struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
106 
107 	if (device_may_wakeup(dev))
108 		enable_irq_wake(pwrkey->key_press_irq);
109 
110 	return 0;
111 }
112 
113 static int __maybe_unused pmic8xxx_pwrkey_resume(struct device *dev)
114 {
115 	struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
116 
117 	if (device_may_wakeup(dev))
118 		disable_irq_wake(pwrkey->key_press_irq);
119 
120 	return 0;
121 }
122 
123 static SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops,
124 		pmic8xxx_pwrkey_suspend, pmic8xxx_pwrkey_resume);
125 
126 static void pmic8xxx_pwrkey_shutdown(struct platform_device *pdev)
127 {
128 	struct pmic8xxx_pwrkey *pwrkey = platform_get_drvdata(pdev);
129 	int error;
130 	u8 mask, val;
131 	bool reset = system_state == SYSTEM_RESTART;
132 
133 	if (pwrkey->shutdown_fn) {
134 		error = pwrkey->shutdown_fn(pwrkey, reset);
135 		if (error)
136 			return;
137 	}
138 
139 	/*
140 	 * Select action to perform (reset or shutdown) when PS_HOLD goes low.
141 	 * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that
142 	 * USB charging is enabled.
143 	 */
144 	mask = PON_CNTL_1_PULL_UP_EN | PON_CNTL_1_USB_PWR_EN;
145 	mask |= PON_CNTL_1_WD_EN_RESET;
146 	val = mask;
147 	if (!reset)
148 		val &= ~PON_CNTL_1_WD_EN_RESET;
149 
150 	regmap_update_bits(pwrkey->regmap, PON_CNTL_1, mask, val);
151 }
152 
153 /*
154  * Set an SMPS regulator to be disabled in its CTRL register, but enabled
155  * in the master enable register.  Also set it's pull down enable bit.
156  * Take care to make sure that the output voltage doesn't change if switching
157  * from advanced mode to legacy mode.
158  */
159 static int pm8058_disable_smps_locally_set_pull_down(struct regmap *regmap,
160 	u16 ctrl_addr, u16 test2_addr, u16 master_enable_addr,
161 	u8 master_enable_bit)
162 {
163 	int error;
164 	u8 vref_sel, vlow_sel, band, vprog, bank;
165 	unsigned int reg;
166 
167 	bank = PM8058_REGULATOR_BANK_SEL(7);
168 	error = regmap_write(regmap, test2_addr, bank);
169 	if (error)
170 		return error;
171 
172 	error = regmap_read(regmap, test2_addr, &reg);
173 	if (error)
174 		return error;
175 
176 	reg &= PM8058_SMPS_ADVANCED_MODE_MASK;
177 	/* Check if in advanced mode. */
178 	if (reg == PM8058_SMPS_ADVANCED_MODE) {
179 		/* Determine current output voltage. */
180 		error = regmap_read(regmap, ctrl_addr, &reg);
181 		if (error)
182 			return error;
183 
184 		band = reg & PM8058_SMPS_ADVANCED_BAND_MASK;
185 		band >>= PM8058_SMPS_ADVANCED_BAND_SHIFT;
186 		switch (band) {
187 		case 3:
188 			vref_sel = 0;
189 			vlow_sel = 0;
190 			break;
191 		case 2:
192 			vref_sel = PM8058_SMPS_LEGACY_VREF_SEL;
193 			vlow_sel = 0;
194 			break;
195 		case 1:
196 			vref_sel = PM8058_SMPS_LEGACY_VREF_SEL;
197 			vlow_sel = PM8058_SMPS_LEGACY_VLOW_SEL;
198 			break;
199 		default:
200 			pr_err("%s: regulator already disabled\n", __func__);
201 			return -EPERM;
202 		}
203 		vprog = reg & PM8058_SMPS_ADVANCED_VPROG_MASK;
204 		/* Round up if fine step is in use. */
205 		vprog = (vprog + 1) >> 1;
206 		if (vprog > PM8058_SMPS_LEGACY_VPROG_MASK)
207 			vprog = PM8058_SMPS_LEGACY_VPROG_MASK;
208 
209 		/* Set VLOW_SEL bit. */
210 		bank = PM8058_REGULATOR_BANK_SEL(1);
211 		error = regmap_write(regmap, test2_addr, bank);
212 		if (error)
213 			return error;
214 
215 		error = regmap_update_bits(regmap, test2_addr,
216 			PM8058_REGULATOR_BANK_WRITE | PM8058_REGULATOR_BANK_MASK
217 				| PM8058_SMPS_LEGACY_VLOW_SEL,
218 			PM8058_REGULATOR_BANK_WRITE |
219 			PM8058_REGULATOR_BANK_SEL(1) | vlow_sel);
220 		if (error)
221 			return error;
222 
223 		/* Switch to legacy mode */
224 		bank = PM8058_REGULATOR_BANK_SEL(7);
225 		error = regmap_write(regmap, test2_addr, bank);
226 		if (error)
227 			return error;
228 
229 		error = regmap_update_bits(regmap, test2_addr,
230 				PM8058_REGULATOR_BANK_WRITE |
231 				PM8058_REGULATOR_BANK_MASK |
232 				PM8058_SMPS_ADVANCED_MODE_MASK,
233 				PM8058_REGULATOR_BANK_WRITE |
234 				PM8058_REGULATOR_BANK_SEL(7) |
235 				PM8058_SMPS_LEGACY_MODE);
236 		if (error)
237 			return error;
238 
239 		/* Enable locally, enable pull down, keep voltage the same. */
240 		error = regmap_update_bits(regmap, ctrl_addr,
241 			PM8058_REGULATOR_ENABLE_MASK |
242 			PM8058_REGULATOR_PULL_DOWN_MASK |
243 			PM8058_SMPS_LEGACY_VREF_SEL |
244 			PM8058_SMPS_LEGACY_VPROG_MASK,
245 			PM8058_REGULATOR_ENABLE | PM8058_REGULATOR_PULL_DOWN_EN
246 				| vref_sel | vprog);
247 		if (error)
248 			return error;
249 	}
250 
251 	/* Enable in master control register. */
252 	error = regmap_update_bits(regmap, master_enable_addr,
253 			master_enable_bit, master_enable_bit);
254 	if (error)
255 		return error;
256 
257 	/* Disable locally and enable pull down. */
258 	return regmap_update_bits(regmap, ctrl_addr,
259 		PM8058_REGULATOR_ENABLE_MASK | PM8058_REGULATOR_PULL_DOWN_MASK,
260 		PM8058_REGULATOR_DISABLE | PM8058_REGULATOR_PULL_DOWN_EN);
261 }
262 
263 static int pm8058_disable_ldo_locally_set_pull_down(struct regmap *regmap,
264 		u16 ctrl_addr, u16 master_enable_addr, u8 master_enable_bit)
265 {
266 	int error;
267 
268 	/* Enable LDO in master control register. */
269 	error = regmap_update_bits(regmap, master_enable_addr,
270 			master_enable_bit, master_enable_bit);
271 	if (error)
272 		return error;
273 
274 	/* Disable LDO in CTRL register and set pull down */
275 	return regmap_update_bits(regmap, ctrl_addr,
276 		PM8058_REGULATOR_ENABLE_MASK | PM8058_REGULATOR_PULL_DOWN_MASK,
277 		PM8058_REGULATOR_DISABLE | PM8058_REGULATOR_PULL_DOWN_EN);
278 }
279 
280 static int pm8058_pwrkey_shutdown(struct pmic8xxx_pwrkey *pwrkey, bool reset)
281 {
282 	int error;
283 	struct regmap *regmap = pwrkey->regmap;
284 	u8 mask, val;
285 
286 	/* When shutting down, enable active pulldowns on important rails. */
287 	if (!reset) {
288 		/* Disable SMPS's 0,1,3 locally and set pulldown enable bits. */
289 		pm8058_disable_smps_locally_set_pull_down(regmap,
290 			PM8058_S0_CTRL, PM8058_S0_TEST2,
291 			REG_PM8058_VREG_EN_MSM, BIT(7));
292 		pm8058_disable_smps_locally_set_pull_down(regmap,
293 			PM8058_S1_CTRL, PM8058_S1_TEST2,
294 			REG_PM8058_VREG_EN_MSM, BIT(6));
295 		pm8058_disable_smps_locally_set_pull_down(regmap,
296 			PM8058_S3_CTRL, PM8058_S3_TEST2,
297 			REG_PM8058_VREG_EN_GRP_5_4, BIT(7) | BIT(4));
298 		/* Disable LDO 21 locally and set pulldown enable bit. */
299 		pm8058_disable_ldo_locally_set_pull_down(regmap,
300 			PM8058_L21_CTRL, REG_PM8058_VREG_EN_GRP_5_4,
301 			BIT(1));
302 	}
303 
304 	/*
305 	 * Fix-up: Set regulator LDO22 to 1.225 V in high power mode. Leave its
306 	 * pull-down state intact. This ensures a safe shutdown.
307 	 */
308 	error = regmap_update_bits(regmap, PM8058_L22_CTRL, 0xbf, 0x93);
309 	if (error)
310 		return error;
311 
312 	/* Enable SMPL if resetting is desired */
313 	mask = SLEEP_CTRL_SMPL_EN_RESET;
314 	val = 0;
315 	if (reset)
316 		val = mask;
317 	return regmap_update_bits(regmap, PM8058_SLEEP_CTRL, mask, val);
318 }
319 
320 static int pm8921_pwrkey_shutdown(struct pmic8xxx_pwrkey *pwrkey, bool reset)
321 {
322 	struct regmap *regmap = pwrkey->regmap;
323 	u8 mask = SLEEP_CTRL_SMPL_EN_RESET;
324 	u8 val = 0;
325 
326 	/* Enable SMPL if resetting is desired */
327 	if (reset)
328 		val = mask;
329 	return regmap_update_bits(regmap, PM8921_SLEEP_CTRL, mask, val);
330 }
331 
332 static int pmic8xxx_pwrkey_probe(struct platform_device *pdev)
333 {
334 	struct input_dev *pwr;
335 	int key_release_irq = platform_get_irq(pdev, 0);
336 	int key_press_irq = platform_get_irq(pdev, 1);
337 	int err;
338 	unsigned int delay;
339 	unsigned int pon_cntl;
340 	struct regmap *regmap;
341 	struct pmic8xxx_pwrkey *pwrkey;
342 	u32 kpd_delay;
343 	bool pull_up;
344 
345 	if (of_property_read_u32(pdev->dev.of_node, "debounce", &kpd_delay))
346 		kpd_delay = 15625;
347 
348 	/* Valid range of pwr key trigger delay is 1/64 sec to 2 seconds. */
349 	if (kpd_delay > USEC_PER_SEC * 2 || kpd_delay < USEC_PER_SEC / 64) {
350 		dev_err(&pdev->dev, "invalid power key trigger delay\n");
351 		return -EINVAL;
352 	}
353 
354 	pull_up = of_property_read_bool(pdev->dev.of_node, "pull-up");
355 
356 	regmap = dev_get_regmap(pdev->dev.parent, NULL);
357 	if (!regmap) {
358 		dev_err(&pdev->dev, "failed to locate regmap for the device\n");
359 		return -ENODEV;
360 	}
361 
362 	pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
363 	if (!pwrkey)
364 		return -ENOMEM;
365 
366 	pwrkey->shutdown_fn = of_device_get_match_data(&pdev->dev);
367 	pwrkey->regmap = regmap;
368 	pwrkey->key_press_irq = key_press_irq;
369 
370 	pwr = devm_input_allocate_device(&pdev->dev);
371 	if (!pwr) {
372 		dev_dbg(&pdev->dev, "Can't allocate power button\n");
373 		return -ENOMEM;
374 	}
375 
376 	input_set_capability(pwr, EV_KEY, KEY_POWER);
377 
378 	pwr->name = "pmic8xxx_pwrkey";
379 	pwr->phys = "pmic8xxx_pwrkey/input0";
380 
381 	delay = (kpd_delay << 6) / USEC_PER_SEC;
382 	delay = ilog2(delay);
383 
384 	err = regmap_read(regmap, PON_CNTL_1, &pon_cntl);
385 	if (err < 0) {
386 		dev_err(&pdev->dev, "failed reading PON_CNTL_1 err=%d\n", err);
387 		return err;
388 	}
389 
390 	pon_cntl &= ~PON_CNTL_TRIG_DELAY_MASK;
391 	pon_cntl |= (delay & PON_CNTL_TRIG_DELAY_MASK);
392 	if (pull_up)
393 		pon_cntl |= PON_CNTL_PULL_UP;
394 	else
395 		pon_cntl &= ~PON_CNTL_PULL_UP;
396 
397 	err = regmap_write(regmap, PON_CNTL_1, pon_cntl);
398 	if (err < 0) {
399 		dev_err(&pdev->dev, "failed writing PON_CNTL_1 err=%d\n", err);
400 		return err;
401 	}
402 
403 	err = devm_request_irq(&pdev->dev, key_press_irq, pwrkey_press_irq,
404 			       IRQF_TRIGGER_RISING,
405 			       "pmic8xxx_pwrkey_press", pwr);
406 	if (err) {
407 		dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
408 			key_press_irq, err);
409 		return err;
410 	}
411 
412 	err = devm_request_irq(&pdev->dev, key_release_irq, pwrkey_release_irq,
413 			       IRQF_TRIGGER_RISING,
414 			       "pmic8xxx_pwrkey_release", pwr);
415 	if (err) {
416 		dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
417 			key_release_irq, err);
418 		return err;
419 	}
420 
421 	err = input_register_device(pwr);
422 	if (err) {
423 		dev_err(&pdev->dev, "Can't register power key: %d\n", err);
424 		return err;
425 	}
426 
427 	platform_set_drvdata(pdev, pwrkey);
428 	device_init_wakeup(&pdev->dev, 1);
429 
430 	return 0;
431 }
432 
433 static const struct of_device_id pm8xxx_pwr_key_id_table[] = {
434 	{ .compatible = "qcom,pm8058-pwrkey", .data = &pm8058_pwrkey_shutdown },
435 	{ .compatible = "qcom,pm8921-pwrkey", .data = &pm8921_pwrkey_shutdown },
436 	{ }
437 };
438 MODULE_DEVICE_TABLE(of, pm8xxx_pwr_key_id_table);
439 
440 static struct platform_driver pmic8xxx_pwrkey_driver = {
441 	.probe		= pmic8xxx_pwrkey_probe,
442 	.shutdown	= pmic8xxx_pwrkey_shutdown,
443 	.driver		= {
444 		.name	= "pm8xxx-pwrkey",
445 		.pm	= &pm8xxx_pwr_key_pm_ops,
446 		.of_match_table = pm8xxx_pwr_key_id_table,
447 	},
448 };
449 module_platform_driver(pmic8xxx_pwrkey_driver);
450 
451 MODULE_ALIAS("platform:pmic8xxx_pwrkey");
452 MODULE_DESCRIPTION("PMIC8XXX Power Key driver");
453 MODULE_LICENSE("GPL v2");
454 MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");
455