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