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