xref: /optee_os/core/drivers/stpmic1.c (revision 68cfb83d49a5374478d3a941c47e4b37152457e4)
1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright (c) 2016-2020, STMicroelectronics - All Rights Reserved
4  */
5 
6 #include <assert.h>
7 #include <drivers/stpmic1.h>
8 #include <kernel/panic.h>
9 #include <platform_config.h>
10 #include <stdint.h>
11 #include <string.h>
12 #include <trace.h>
13 
14 struct regul_struct {
15 	const char *dt_node_name;
16 	const uint16_t *voltage_table;
17 	uint8_t voltage_table_size;
18 	uint8_t control_reg;
19 	uint8_t low_power_reg;
20 	uint8_t enable_pos;
21 	uint8_t pull_down_reg;
22 	uint8_t pull_down_pos;
23 	uint8_t mask_reset_reg;
24 	uint8_t mask_reset_pos;
25 };
26 
27 static struct i2c_handle_s *pmic_i2c_handle;
28 static uint16_t pmic_i2c_addr;
29 
30 /* Voltage tables in mV */
31 static const uint16_t buck1_voltage_table[] = {
32 	725,
33 	725,
34 	725,
35 	725,
36 	725,
37 	725,
38 	750,
39 	775,
40 	800,
41 	825,
42 	850,
43 	875,
44 	900,
45 	925,
46 	950,
47 	975,
48 	1000,
49 	1025,
50 	1050,
51 	1075,
52 	1100,
53 	1125,
54 	1150,
55 	1175,
56 	1200,
57 	1225,
58 	1250,
59 	1275,
60 	1300,
61 	1325,
62 	1350,
63 	1375,
64 	1400,
65 	1425,
66 	1450,
67 	1475,
68 	1500,
69 	1500,
70 	1500,
71 	1500,
72 	1500,
73 	1500,
74 	1500,
75 	1500,
76 	1500,
77 	1500,
78 	1500,
79 	1500,
80 	1500,
81 	1500,
82 	1500,
83 	1500,
84 	1500,
85 	1500,
86 	1500,
87 	1500,
88 	1500,
89 	1500,
90 	1500,
91 	1500,
92 	1500,
93 	1500,
94 	1500,
95 	1500,
96 };
97 
98 static const uint16_t buck2_voltage_table[] = {
99 	1000,
100 	1000,
101 	1000,
102 	1000,
103 	1000,
104 	1000,
105 	1000,
106 	1000,
107 	1000,
108 	1000,
109 	1000,
110 	1000,
111 	1000,
112 	1000,
113 	1000,
114 	1000,
115 	1000,
116 	1000,
117 	1050,
118 	1050,
119 	1100,
120 	1100,
121 	1150,
122 	1150,
123 	1200,
124 	1200,
125 	1250,
126 	1250,
127 	1300,
128 	1300,
129 	1350,
130 	1350,
131 	1400,
132 	1400,
133 	1450,
134 	1450,
135 	1500,
136 };
137 
138 static const uint16_t buck3_voltage_table[] = {
139 	1000,
140 	1000,
141 	1000,
142 	1000,
143 	1000,
144 	1000,
145 	1000,
146 	1000,
147 	1000,
148 	1000,
149 	1000,
150 	1000,
151 	1000,
152 	1000,
153 	1000,
154 	1000,
155 	1000,
156 	1000,
157 	1000,
158 	1000,
159 	1100,
160 	1100,
161 	1100,
162 	1100,
163 	1200,
164 	1200,
165 	1200,
166 	1200,
167 	1300,
168 	1300,
169 	1300,
170 	1300,
171 	1400,
172 	1400,
173 	1400,
174 	1400,
175 	1500,
176 	1600,
177 	1700,
178 	1800,
179 	1900,
180 	2000,
181 	2100,
182 	2200,
183 	2300,
184 	2400,
185 	2500,
186 	2600,
187 	2700,
188 	2800,
189 	2900,
190 	3000,
191 	3100,
192 	3200,
193 	3300,
194 	3400,
195 };
196 
197 static const uint16_t buck4_voltage_table[] = {
198 	600,
199 	625,
200 	650,
201 	675,
202 	700,
203 	725,
204 	750,
205 	775,
206 	800,
207 	825,
208 	850,
209 	875,
210 	900,
211 	925,
212 	950,
213 	975,
214 	1000,
215 	1025,
216 	1050,
217 	1075,
218 	1100,
219 	1125,
220 	1150,
221 	1175,
222 	1200,
223 	1225,
224 	1250,
225 	1275,
226 	1300,
227 	1300,
228 	1350,
229 	1350,
230 	1400,
231 	1400,
232 	1450,
233 	1450,
234 	1500,
235 	1600,
236 	1700,
237 	1800,
238 	1900,
239 	2000,
240 	2100,
241 	2200,
242 	2300,
243 	2400,
244 	2500,
245 	2600,
246 	2700,
247 	2800,
248 	2900,
249 	3000,
250 	3100,
251 	3200,
252 	3300,
253 	3400,
254 	3500,
255 	3600,
256 	3700,
257 	3800,
258 	3900,
259 };
260 
261 static const uint16_t ldo1_voltage_table[] = {
262 	1700,
263 	1700,
264 	1700,
265 	1700,
266 	1700,
267 	1700,
268 	1700,
269 	1700,
270 	1700,
271 	1800,
272 	1900,
273 	2000,
274 	2100,
275 	2200,
276 	2300,
277 	2400,
278 	2500,
279 	2600,
280 	2700,
281 	2800,
282 	2900,
283 	3000,
284 	3100,
285 	3200,
286 	3300,
287 };
288 
289 static const uint16_t ldo2_voltage_table[] = {
290 	1700,
291 	1700,
292 	1700,
293 	1700,
294 	1700,
295 	1700,
296 	1700,
297 	1700,
298 	1700,
299 	1800,
300 	1900,
301 	2000,
302 	2100,
303 	2200,
304 	2300,
305 	2400,
306 	2500,
307 	2600,
308 	2700,
309 	2800,
310 	2900,
311 	3000,
312 	3100,
313 	3200,
314 	3300,
315 };
316 
317 static const uint16_t ldo3_voltage_table[] = {
318 	1700,
319 	1700,
320 	1700,
321 	1700,
322 	1700,
323 	1700,
324 	1700,
325 	1700,
326 	1700,
327 	1800,
328 	1900,
329 	2000,
330 	2100,
331 	2200,
332 	2300,
333 	2400,
334 	2500,
335 	2600,
336 	2700,
337 	2800,
338 	2900,
339 	3000,
340 	3100,
341 	3200,
342 	3300,
343 	3300,
344 	3300,
345 	3300,
346 	3300,
347 	3300,
348 	3300,
349 	500,	/* VOUT2/2 (Sink/source mode) */
350 	0xFFFF, /* VREFDDR */
351 };
352 
353 static const uint16_t ldo5_voltage_table[] = {
354 	1700,
355 	1700,
356 	1700,
357 	1700,
358 	1700,
359 	1700,
360 	1700,
361 	1700,
362 	1700,
363 	1800,
364 	1900,
365 	2000,
366 	2100,
367 	2200,
368 	2300,
369 	2400,
370 	2500,
371 	2600,
372 	2700,
373 	2800,
374 	2900,
375 	3000,
376 	3100,
377 	3200,
378 	3300,
379 	3400,
380 	3500,
381 	3600,
382 	3700,
383 	3800,
384 	3900,
385 };
386 
387 static const uint16_t ldo6_voltage_table[] = {
388 	900,
389 	1000,
390 	1100,
391 	1200,
392 	1300,
393 	1400,
394 	1500,
395 	1600,
396 	1700,
397 	1800,
398 	1900,
399 	2000,
400 	2100,
401 	2200,
402 	2300,
403 	2400,
404 	2500,
405 	2600,
406 	2700,
407 	2800,
408 	2900,
409 	3000,
410 	3100,
411 	3200,
412 	3300,
413 };
414 
415 static const uint16_t ldo4_voltage_table[] = {
416 	3300,
417 };
418 
419 static const uint16_t vref_ddr_voltage_table[] = {
420 	3300,
421 };
422 
423 /* Table of Regulators in PMIC SoC */
424 static const struct regul_struct regulators_table[] = {
425 	{
426 		.dt_node_name	= "buck1",
427 		.voltage_table	= buck1_voltage_table,
428 		.voltage_table_size = ARRAY_SIZE(buck1_voltage_table),
429 		.control_reg	= BUCK1_CONTROL_REG,
430 		.low_power_reg	= BUCK1_PWRCTRL_REG,
431 		.enable_pos	= LDO_BUCK_ENABLE_POS,
432 		.pull_down_reg	= BUCK_PULL_DOWN_REG,
433 		.pull_down_pos	= BUCK1_PULL_DOWN_SHIFT,
434 		.mask_reset_reg = MASK_RESET_BUCK_REG,
435 		.mask_reset_pos = BUCK1_MASK_RESET_SHIFT,
436 	},
437 	{
438 		.dt_node_name	= "buck2",
439 		.voltage_table	= buck2_voltage_table,
440 		.voltage_table_size = ARRAY_SIZE(buck2_voltage_table),
441 		.control_reg	= BUCK2_CONTROL_REG,
442 		.low_power_reg	= BUCK2_PWRCTRL_REG,
443 		.enable_pos	= LDO_BUCK_ENABLE_POS,
444 		.pull_down_reg	= BUCK_PULL_DOWN_REG,
445 		.pull_down_pos	= BUCK2_PULL_DOWN_SHIFT,
446 		.mask_reset_reg = MASK_RESET_BUCK_REG,
447 		.mask_reset_pos = BUCK2_MASK_RESET_SHIFT,
448 	},
449 	{
450 		.dt_node_name	= "buck3",
451 		.voltage_table	= buck3_voltage_table,
452 		.voltage_table_size = ARRAY_SIZE(buck3_voltage_table),
453 		.control_reg	= BUCK3_CONTROL_REG,
454 		.low_power_reg	= BUCK3_PWRCTRL_REG,
455 		.enable_pos	= LDO_BUCK_ENABLE_POS,
456 		.pull_down_reg	= BUCK_PULL_DOWN_REG,
457 		.pull_down_pos	= BUCK3_PULL_DOWN_SHIFT,
458 		.mask_reset_reg = MASK_RESET_BUCK_REG,
459 		.mask_reset_pos = BUCK3_MASK_RESET_SHIFT,
460 	},
461 	{
462 		.dt_node_name	= "buck4",
463 		.voltage_table	= buck4_voltage_table,
464 		.voltage_table_size = ARRAY_SIZE(buck4_voltage_table),
465 		.control_reg	= BUCK4_CONTROL_REG,
466 		.low_power_reg	= BUCK4_PWRCTRL_REG,
467 		.enable_pos	= LDO_BUCK_ENABLE_POS,
468 		.pull_down_reg	= BUCK_PULL_DOWN_REG,
469 		.pull_down_pos	= BUCK4_PULL_DOWN_SHIFT,
470 		.mask_reset_reg = MASK_RESET_BUCK_REG,
471 		.mask_reset_pos = BUCK4_MASK_RESET_SHIFT,
472 	},
473 	{
474 		.dt_node_name	= "ldo1",
475 		.voltage_table	= ldo1_voltage_table,
476 		.voltage_table_size = ARRAY_SIZE(ldo1_voltage_table),
477 		.control_reg	= LDO1_CONTROL_REG,
478 		.low_power_reg	= LDO1_PWRCTRL_REG,
479 		.enable_pos	= LDO_BUCK_ENABLE_POS,
480 		.mask_reset_reg = MASK_RESET_LDO_REG,
481 		.mask_reset_pos = LDO1_MASK_RESET_SHIFT,
482 	},
483 	{
484 		.dt_node_name	= "ldo2",
485 		.voltage_table	= ldo2_voltage_table,
486 		.voltage_table_size = ARRAY_SIZE(ldo2_voltage_table),
487 		.control_reg	= LDO2_CONTROL_REG,
488 		.low_power_reg	= LDO2_PWRCTRL_REG,
489 		.enable_pos	= LDO_BUCK_ENABLE_POS,
490 		.mask_reset_reg = MASK_RESET_LDO_REG,
491 		.mask_reset_pos = LDO2_MASK_RESET_SHIFT,
492 	},
493 	{
494 		.dt_node_name	= "ldo3",
495 		.voltage_table	= ldo3_voltage_table,
496 		.voltage_table_size = ARRAY_SIZE(ldo3_voltage_table),
497 		.control_reg	= LDO3_CONTROL_REG,
498 		.low_power_reg	= LDO3_PWRCTRL_REG,
499 		.enable_pos	= LDO_BUCK_ENABLE_POS,
500 		.mask_reset_reg = MASK_RESET_LDO_REG,
501 		.mask_reset_pos = LDO3_MASK_RESET_SHIFT,
502 	},
503 	{
504 		.dt_node_name	= "ldo4",
505 		.voltage_table	= ldo4_voltage_table,
506 		.voltage_table_size = ARRAY_SIZE(ldo4_voltage_table),
507 		.control_reg	= LDO4_CONTROL_REG,
508 		.low_power_reg	= LDO4_PWRCTRL_REG,
509 		.enable_pos	= LDO_BUCK_ENABLE_POS,
510 		.mask_reset_reg = MASK_RESET_LDO_REG,
511 		.mask_reset_pos = LDO4_MASK_RESET_SHIFT,
512 	},
513 	{
514 		.dt_node_name	= "ldo5",
515 		.voltage_table	= ldo5_voltage_table,
516 		.voltage_table_size = ARRAY_SIZE(ldo5_voltage_table),
517 		.control_reg	= LDO5_CONTROL_REG,
518 		.low_power_reg	= LDO5_PWRCTRL_REG,
519 		.enable_pos	= LDO_BUCK_ENABLE_POS,
520 		.mask_reset_reg = MASK_RESET_LDO_REG,
521 		.mask_reset_pos = LDO5_MASK_RESET_SHIFT,
522 	},
523 	{
524 		.dt_node_name	= "ldo6",
525 		.voltage_table	= ldo6_voltage_table,
526 		.voltage_table_size = ARRAY_SIZE(ldo6_voltage_table),
527 		.control_reg	= LDO6_CONTROL_REG,
528 		.low_power_reg	= LDO6_PWRCTRL_REG,
529 		.enable_pos	= LDO_BUCK_ENABLE_POS,
530 		.mask_reset_reg = MASK_RESET_LDO_REG,
531 		.mask_reset_pos = LDO6_MASK_RESET_SHIFT,
532 	},
533 	{
534 		.dt_node_name	= "vref_ddr",
535 		.voltage_table	= vref_ddr_voltage_table,
536 		.voltage_table_size = ARRAY_SIZE(vref_ddr_voltage_table),
537 		.control_reg	= VREF_DDR_CONTROL_REG,
538 		.low_power_reg	= VREF_DDR_PWRCTRL_REG,
539 		.enable_pos	= LDO_BUCK_ENABLE_POS,
540 		.mask_reset_reg = MASK_RESET_LDO_REG,
541 		.mask_reset_pos = VREF_DDR_MASK_RESET_SHIFT,
542 	},
543 	{
544 		.dt_node_name = "boost",
545 	},
546 	{
547 		.dt_node_name = "pwr_sw1",
548 	},
549 	{
550 		.dt_node_name = "pwr_sw2",
551 	},
552 };
553 
554 static const struct regul_struct *get_regulator_data(const char *name)
555 {
556 	unsigned int i = 0;
557 
558 	for (i = 0; i < ARRAY_SIZE(regulators_table); i++)
559 		if (strcmp(name, regulators_table[i].dt_node_name) == 0)
560 			return &regulators_table[i];
561 
562 	/* Regulator not found */
563 	panic(name);
564 }
565 
566 static uint8_t voltage_to_index(const char *name, uint16_t millivolts)
567 {
568 	const struct regul_struct *regul = get_regulator_data(name);
569 	unsigned int i = 0;
570 
571 	assert(regul->voltage_table);
572 	for (i = 0; i < regul->voltage_table_size; i++)
573 		if (regul->voltage_table[i] == millivolts)
574 			return i;
575 
576 	/* Voltage not found */
577 	panic(name);
578 }
579 
580 int stpmic1_powerctrl_on(void)
581 {
582 	return stpmic1_register_update(MAIN_CONTROL_REG, PWRCTRL_PIN_VALID,
583 				       PWRCTRL_PIN_VALID);
584 }
585 
586 int stpmic1_switch_off(void)
587 {
588 	return stpmic1_register_update(MAIN_CONTROL_REG, 1,
589 				       SOFTWARE_SWITCH_OFF_ENABLED);
590 }
591 
592 int stpmic1_regulator_enable(const char *name)
593 {
594 	const struct regul_struct *regul = get_regulator_data(name);
595 
596 	return stpmic1_register_update(regul->control_reg,
597 				       BIT(regul->enable_pos),
598 				       BIT(regul->enable_pos));
599 }
600 
601 int stpmic1_regulator_disable(const char *name)
602 {
603 	const struct regul_struct *regul = get_regulator_data(name);
604 
605 	return stpmic1_register_update(regul->control_reg, 0,
606 				       BIT(regul->enable_pos));
607 }
608 
609 bool stpmic1_is_regulator_enabled(const char *name)
610 {
611 	const struct regul_struct *regul = get_regulator_data(name);
612 	uint8_t val = 0;
613 
614 	if (stpmic1_register_read(regul->control_reg, &val))
615 		panic();
616 
617 	return val & BIT(regul->enable_pos);
618 }
619 
620 int stpmic1_regulator_voltage_set(const char *name, uint16_t millivolts)
621 {
622 	uint8_t voltage_index = voltage_to_index(name, millivolts);
623 	const struct regul_struct *regul = get_regulator_data(name);
624 	uint8_t mask = 0;
625 
626 	/* Voltage can be set for buck<N> or ldo<N> (except ldo4) regulators */
627 	if (!strcmp(name, "buck"))
628 		mask = BUCK_VOLTAGE_MASK;
629 	else if (!strcmp(name, "ldo") && strcmp(name, "ldo4"))
630 		mask = LDO_VOLTAGE_MASK;
631 	else
632 		return 0;
633 
634 	return stpmic1_register_update(regul->control_reg,
635 				       voltage_index << LDO_BUCK_VOLTAGE_SHIFT,
636 				       mask);
637 }
638 
639 int stpmic1_regulator_mask_reset_set(const char *name)
640 {
641 	const struct regul_struct *regul = get_regulator_data(name);
642 
643 	return stpmic1_register_update(regul->mask_reset_reg,
644 				       BIT(regul->mask_reset_pos),
645 				       LDO_BUCK_RESET_MASK <<
646 				       regul->mask_reset_pos);
647 }
648 
649 int stpmic1_bo_enable_unpg(struct stpmic1_bo_cfg *cfg)
650 {
651 	return stpmic1_register_update(cfg->ctrl_reg,
652 				       BIT(cfg->enable_pos),
653 				       BIT(cfg->enable_pos));
654 }
655 
656 /* Returns 1 if no configuration are expected applied at runtime, 0 otherwise */
657 int stpmic1_bo_voltage_cfg(const char *name, uint16_t millivolts,
658 			   struct stpmic1_bo_cfg *cfg)
659 {
660 	uint8_t voltage_index = voltage_to_index(name, millivolts);
661 	const struct regul_struct *regul = get_regulator_data(name);
662 	uint8_t mask = 0;
663 
664 	/* Voltage can be set for buck<N> or ldo<N> (except ldo4) regulators */
665 	if (!strcmp(name, "buck"))
666 		mask = BUCK_VOLTAGE_MASK;
667 	else if (!strcmp(name, "ldo") && strcmp(name, "ldo4"))
668 		mask = LDO_VOLTAGE_MASK;
669 	else
670 		return 1;
671 
672 	cfg->ctrl_reg = regul->control_reg;
673 	cfg->value = voltage_index << LDO_BUCK_VOLTAGE_SHIFT;
674 	cfg->mask = mask;
675 
676 	return 0;
677 }
678 
679 int stpmic1_bo_voltage_unpg(struct stpmic1_bo_cfg *cfg)
680 {
681 	return stpmic1_register_update(cfg->ctrl_reg, cfg->value, cfg->mask);
682 }
683 
684 int stpmic1_bo_pull_down_cfg(const char *name, struct stpmic1_bo_cfg *cfg)
685 {
686 	const struct regul_struct *regul = get_regulator_data(name);
687 
688 	cfg->pd_reg = regul->pull_down_reg;
689 	cfg->pd_value = BIT(regul->pull_down_pos);
690 	cfg->pd_mask = LDO_BUCK_PULL_DOWN_MASK << regul->pull_down_pos;
691 
692 	return 0;
693 }
694 
695 int stpmic1_bo_pull_down_unpg(struct stpmic1_bo_cfg *cfg)
696 {
697 	return stpmic1_register_update(cfg->pd_reg, cfg->pd_value,
698 				       cfg->pd_mask);
699 }
700 
701 int stpmic1_bo_mask_reset_cfg(const char *name, struct stpmic1_bo_cfg *cfg)
702 {
703 	const struct regul_struct *regul = get_regulator_data(name);
704 
705 	cfg->mrst_reg = regul->mask_reset_reg;
706 	cfg->mrst_value = BIT(regul->mask_reset_pos);
707 	cfg->mrst_mask = LDO_BUCK_RESET_MASK << regul->mask_reset_pos;
708 
709 	return 0;
710 }
711 
712 int stpmic1_bo_mask_reset_unpg(struct stpmic1_bo_cfg *cfg)
713 {
714 	return stpmic1_register_update(cfg->mrst_reg, cfg->mrst_value,
715 				       cfg->mrst_mask);
716 }
717 
718 int stpmic1_regulator_voltage_get(const char *name)
719 {
720 	const struct regul_struct *regul = get_regulator_data(name);
721 	uint8_t value = 0;
722 	uint8_t mask = 0;
723 
724 	/* Voltage can be set for buck<N> or ldo<N> (except ldo4) regulators */
725 	if (!strcmp(name, "buck"))
726 		mask = BUCK_VOLTAGE_MASK;
727 	else if (!strcmp(name, "ldo") && strcmp(name, "ldo4"))
728 		mask = LDO_VOLTAGE_MASK;
729 	else
730 		return 0;
731 
732 	if (stpmic1_register_read(regul->control_reg, &value))
733 		return -1;
734 
735 	value = (value & mask) >> LDO_BUCK_VOLTAGE_SHIFT;
736 
737 	if (value > regul->voltage_table_size)
738 		return -1;
739 
740 	return regul->voltage_table[value];
741 }
742 
743 int stpmic1_lp_copy_reg(const char *name)
744 {
745 	const struct regul_struct *regul = get_regulator_data(name);
746 	uint8_t val = 0;
747 	int status = 0;
748 
749 	status = stpmic1_register_read(regul->control_reg, &val);
750 	if (status)
751 		return status;
752 
753 	return stpmic1_register_write(regul->low_power_reg, val);
754 }
755 
756 int stpmic1_lp_cfg(const char *name, struct stpmic1_lp_cfg *cfg)
757 {
758 	const struct regul_struct *regul = get_regulator_data(name);
759 
760 	cfg->ctrl_reg = regul->control_reg;
761 	cfg->lp_reg = regul->low_power_reg;
762 
763 	return 0;
764 }
765 
766 int stpmic1_lp_load_unpg(struct stpmic1_lp_cfg *cfg)
767 {
768 	uint8_t val = 0;
769 	int status = 0;
770 
771 	status = stpmic1_register_read(cfg->ctrl_reg, &val);
772 	if (!status)
773 		status = stpmic1_register_write(cfg->lp_reg, val);
774 
775 	return status;
776 }
777 
778 int stpmic1_lp_reg_on_off(const char *name, uint8_t enable)
779 {
780 	const struct regul_struct *regul = get_regulator_data(name);
781 
782 	return stpmic1_register_update(regul->low_power_reg, enable,
783 				       LDO_BUCK_ENABLE_MASK);
784 }
785 
786 int stpmic1_lp_on_off_unpg(struct stpmic1_lp_cfg *cfg, int enable)
787 {
788 	assert(enable == 0 || enable == 1);
789 	return stpmic1_register_update(cfg->lp_reg, enable,
790 				       LDO_BUCK_ENABLE_MASK);
791 }
792 
793 int stpmic1_lp_set_mode(const char *name, uint8_t hplp)
794 {
795 	const struct regul_struct *regul = get_regulator_data(name);
796 
797 	return stpmic1_register_update(regul->low_power_reg,
798 				       hplp << LDO_BUCK_HPLP_SHIFT,
799 				       LDO_BUCK_HPLP_ENABLE_MASK);
800 }
801 
802 int stpmic1_lp_mode_unpg(struct stpmic1_lp_cfg *cfg, unsigned int mode)
803 {
804 	assert(mode == 0 || mode == 1);
805 	return stpmic1_register_update(cfg->lp_reg,
806 				       mode << LDO_BUCK_HPLP_SHIFT,
807 				       LDO_BUCK_HPLP_ENABLE_MASK);
808 }
809 
810 int stpmic1_lp_set_voltage(const char *name, uint16_t millivolts)
811 {
812 	uint8_t voltage_index = voltage_to_index(name, millivolts);
813 	const struct regul_struct *regul = get_regulator_data(name);
814 	uint8_t mask = 0;
815 
816 	/* Voltage can be set for buck<N> or ldo<N> (except ldo4) regulators */
817 	if (!strcmp(name, "buck"))
818 		mask = BUCK_VOLTAGE_MASK;
819 	else if (!strcmp(name, "ldo") && strcmp(name, "ldo4"))
820 		mask = LDO_VOLTAGE_MASK;
821 	else
822 		return 0;
823 
824 	return stpmic1_register_update(regul->low_power_reg, voltage_index << 2,
825 				       mask);
826 }
827 
828 /* Returns 1 if no configuration are expected applied at runtime, 0 otherwise */
829 int stpmic1_lp_voltage_cfg(const char *name, uint16_t millivolts,
830 			   struct stpmic1_lp_cfg *cfg)
831 
832 {
833 	uint8_t voltage_index = voltage_to_index(name, millivolts);
834 	uint8_t mask = 0;
835 
836 	/* Voltage can be set for buck<N> or ldo<N> (except ldo4) regulators */
837 	if (!strcmp(name, "buck"))
838 		mask = BUCK_VOLTAGE_MASK;
839 	else if (!strcmp(name, "ldo") && strcmp(name, "ldo4"))
840 		mask = LDO_VOLTAGE_MASK;
841 	else
842 		return 1;
843 
844 	assert(cfg->lp_reg == get_regulator_data(name)->low_power_reg);
845 	cfg->value = voltage_index << 2;
846 	cfg->mask = mask;
847 
848 	return 0;
849 }
850 
851 int stpmic1_lp_voltage_unpg(struct stpmic1_lp_cfg *cfg)
852 {
853 	return stpmic1_register_update(cfg->lp_reg, cfg->value,	cfg->mask);
854 }
855 
856 int stpmic1_register_read(uint8_t register_id,  uint8_t *value)
857 {
858 	struct i2c_handle_s *i2c = pmic_i2c_handle;
859 
860 	return stm32_i2c_read_write_membyte(i2c, pmic_i2c_addr,
861 					    register_id, value,
862 					    false /* !write */);
863 }
864 
865 int stpmic1_register_write(uint8_t register_id, uint8_t value)
866 {
867 	struct i2c_handle_s *i2c = pmic_i2c_handle;
868 	uint8_t val = value;
869 
870 	return stm32_i2c_read_write_membyte(i2c, pmic_i2c_addr,
871 					    register_id, &val,
872 					    true /* write */);
873 }
874 
875 int stpmic1_register_update(uint8_t register_id, uint8_t value, uint8_t mask)
876 {
877 	int status = 0;
878 	uint8_t val = 0;
879 
880 	status = stpmic1_register_read(register_id, &val);
881 	if (status)
882 		return status;
883 
884 	val = (val & ~mask) | (value & mask);
885 
886 	return stpmic1_register_write(register_id, val);
887 }
888 
889 void stpmic1_bind_i2c(struct i2c_handle_s *i2c_handle, uint16_t i2c_addr)
890 {
891 	pmic_i2c_handle = i2c_handle;
892 	pmic_i2c_addr = i2c_addr;
893 }
894 
895 void stpmic1_dump_regulators(void)
896 {
897 	size_t i = 0;
898 	char __maybe_unused const *name = NULL;
899 
900 	for (i = 0; i < ARRAY_SIZE(regulators_table); i++) {
901 		if (!regulators_table[i].control_reg)
902 			continue;
903 
904 		name = regulators_table[i].dt_node_name;
905 		DMSG("PMIC regul %s: %sable, %dmV",
906 		     name, stpmic1_is_regulator_enabled(name) ? "en" : "dis",
907 		     stpmic1_regulator_voltage_get(name));
908 	}
909 }
910 
911 int stpmic1_get_version(unsigned long *version)
912 {
913 	uint8_t read_val = 0;
914 
915 	if (stpmic1_register_read(VERSION_STATUS_REG, &read_val))
916 		return -1;
917 
918 	*version = read_val;
919 	return 0;
920 }
921