blob: c25a2427cae400934ef38d9a6c6f6d58a752de05 [file] [log] [blame]
Yann Gautierbb836ee2018-07-16 17:55:07 +02001/*
Benjamin Gaignard53ee7d32020-02-24 13:57:40 +01002 * Copyright (c) 2017-2021, STMicroelectronics - All Rights Reserved
Yann Gautierbb836ee2018-07-16 17:55:07 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Yann Gautiere6f10322021-09-27 14:31:40 +02007#include <assert.h>
Yann Gautierbb836ee2018-07-16 17:55:07 +02008#include <errno.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <common/debug.h>
11#include <drivers/delay_timer.h>
Yann Gautiere6f10322021-09-27 14:31:40 +020012#include <drivers/st/regulator.h>
Yann Gautierf3928f62019-02-14 11:15:03 +010013#include <drivers/st/stm32_i2c.h>
Yann Gautiera45433b2019-01-16 18:31:00 +010014#include <drivers/st/stm32mp_pmic.h>
Yann Gautiera45433b2019-01-16 18:31:00 +010015#include <drivers/st/stpmic1.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <lib/mmio.h>
17#include <lib/utils_def.h>
Nicolas Le Bayonf5188ee2019-09-19 11:24:50 +020018#include <libfdt.h>
19
20#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000021
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010022#define PMIC_NODE_NOT_FOUND 1
Yann Gautiera45433b2019-01-16 18:31:00 +010023#define STPMIC1_LDO12356_OUTPUT_MASK (uint8_t)(GENMASK(6, 2))
24#define STPMIC1_LDO12356_OUTPUT_SHIFT 2
25#define STPMIC1_LDO3_MODE (uint8_t)(BIT(7))
26#define STPMIC1_LDO3_DDR_SEL 31U
27#define STPMIC1_LDO3_1800000 (9U << STPMIC1_LDO12356_OUTPUT_SHIFT)
Yann Gautierbb836ee2018-07-16 17:55:07 +020028
Yann Gautiera45433b2019-01-16 18:31:00 +010029#define STPMIC1_BUCK_OUTPUT_SHIFT 2
30#define STPMIC1_BUCK3_1V8 (39U << STPMIC1_BUCK_OUTPUT_SHIFT)
Yann Gautierbb836ee2018-07-16 17:55:07 +020031
Yann Gautiera45433b2019-01-16 18:31:00 +010032#define STPMIC1_DEFAULT_START_UP_DELAY_MS 1
Yann Gautierbb836ee2018-07-16 17:55:07 +020033
34static struct i2c_handle_s i2c_handle;
35static uint32_t pmic_i2c_addr;
36
Yann Gautiere6f10322021-09-27 14:31:40 +020037static int register_pmic(void);
38
Yann Gautierbb836ee2018-07-16 17:55:07 +020039static int dt_get_pmic_node(void *fdt)
40{
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010041 static int node = -FDT_ERR_BADOFFSET;
42
43 if (node == -FDT_ERR_BADOFFSET) {
44 node = fdt_node_offset_by_compatible(fdt, -1, "st,stpmic1");
45 }
46
47 return node;
Yann Gautierbb836ee2018-07-16 17:55:07 +020048}
49
Yann Gautierf3928f62019-02-14 11:15:03 +010050int dt_pmic_status(void)
Yann Gautierbb836ee2018-07-16 17:55:07 +020051{
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010052 static int status = -FDT_ERR_BADVALUE;
Yann Gautierbb836ee2018-07-16 17:55:07 +020053 int node;
54 void *fdt;
55
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010056 if (status != -FDT_ERR_BADVALUE) {
57 return status;
58 }
59
Yann Gautierbb836ee2018-07-16 17:55:07 +020060 if (fdt_get_address(&fdt) == 0) {
Yann Gautierf3928f62019-02-14 11:15:03 +010061 return -ENOENT;
Yann Gautierbb836ee2018-07-16 17:55:07 +020062 }
63
64 node = dt_get_pmic_node(fdt);
Yann Gautierf3928f62019-02-14 11:15:03 +010065 if (node <= 0) {
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010066 status = -FDT_ERR_NOTFOUND;
67
68 return status;
Yann Gautierbb836ee2018-07-16 17:55:07 +020069 }
70
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010071 status = (int)fdt_get_status(node);
72
73 return status;
Yann Gautierbb836ee2018-07-16 17:55:07 +020074}
75
Etienne Carriere67b106a2019-12-02 10:10:08 +010076static bool dt_pmic_is_secure(void)
77{
78 int status = dt_pmic_status();
79
80 return (status >= 0) &&
81 (status == DT_SECURE) &&
82 (i2c_handle.dt_status == DT_SECURE);
83}
84
Yann Gautierf3928f62019-02-14 11:15:03 +010085/*
86 * Get PMIC and its I2C bus configuration from the device tree.
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010087 * Return 0 on success, negative on error, 1 if no PMIC node is defined.
Yann Gautierf3928f62019-02-14 11:15:03 +010088 */
89static int dt_pmic_i2c_config(struct dt_node_info *i2c_info,
90 struct stm32_i2c_init_s *init)
Yann Gautierbb836ee2018-07-16 17:55:07 +020091{
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010092 static int i2c_node = -FDT_ERR_NOTFOUND;
Yann Gautierbb836ee2018-07-16 17:55:07 +020093 void *fdt;
Yann Gautierbb836ee2018-07-16 17:55:07 +020094
95 if (fdt_get_address(&fdt) == 0) {
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010096 return -FDT_ERR_NOTFOUND;
Yann Gautierbb836ee2018-07-16 17:55:07 +020097 }
98
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010099 if (i2c_node == -FDT_ERR_NOTFOUND) {
100 int pmic_node;
101 const fdt32_t *cuint;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200102
Nicolas Le Bayon342865a2019-11-15 15:56:06 +0100103 pmic_node = dt_get_pmic_node(fdt);
104 if (pmic_node < 0) {
105 return PMIC_NODE_NOT_FOUND;
106 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200107
Nicolas Le Bayon342865a2019-11-15 15:56:06 +0100108 cuint = fdt_getprop(fdt, pmic_node, "reg", NULL);
109 if (cuint == NULL) {
110 return -FDT_ERR_NOTFOUND;
111 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200112
Nicolas Le Bayon342865a2019-11-15 15:56:06 +0100113 pmic_i2c_addr = fdt32_to_cpu(*cuint) << 1;
114 if (pmic_i2c_addr > UINT16_MAX) {
115 return -FDT_ERR_BADVALUE;
116 }
117
118 i2c_node = fdt_parent_offset(fdt, pmic_node);
119 if (i2c_node < 0) {
120 return -FDT_ERR_NOTFOUND;
121 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200122 }
123
124 dt_fill_device_info(i2c_info, i2c_node);
125 if (i2c_info->base == 0U) {
126 return -FDT_ERR_NOTFOUND;
127 }
128
Yann Gautierf3928f62019-02-14 11:15:03 +0100129 return stm32_i2c_get_setup_from_fdt(fdt, i2c_node, init);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200130}
131
Yann Gautierf3928f62019-02-14 11:15:03 +0100132bool initialize_pmic_i2c(void)
Yann Gautierbb836ee2018-07-16 17:55:07 +0200133{
134 int ret;
135 struct dt_node_info i2c_info;
Yann Gautierf3928f62019-02-14 11:15:03 +0100136 struct i2c_handle_s *i2c = &i2c_handle;
137 struct stm32_i2c_init_s i2c_init;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200138
Yann Gautierf3928f62019-02-14 11:15:03 +0100139 ret = dt_pmic_i2c_config(&i2c_info, &i2c_init);
140 if (ret < 0) {
141 ERROR("I2C configuration failed %d\n", ret);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200142 panic();
143 }
144
Yann Gautierf3928f62019-02-14 11:15:03 +0100145 if (ret != 0) {
146 return false;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200147 }
148
149 /* Initialize PMIC I2C */
Yann Gautierf3928f62019-02-14 11:15:03 +0100150 i2c->i2c_base_addr = i2c_info.base;
151 i2c->dt_status = i2c_info.status;
152 i2c->clock = i2c_info.clock;
Benjamin Gaignard53ee7d32020-02-24 13:57:40 +0100153 i2c->i2c_state = I2C_STATE_RESET;
Yann Gautierf3928f62019-02-14 11:15:03 +0100154 i2c_init.own_address1 = pmic_i2c_addr;
155 i2c_init.addressing_mode = I2C_ADDRESSINGMODE_7BIT;
156 i2c_init.dual_address_mode = I2C_DUALADDRESS_DISABLE;
157 i2c_init.own_address2 = 0;
158 i2c_init.own_address2_masks = I2C_OAR2_OA2NOMASK;
159 i2c_init.general_call_mode = I2C_GENERALCALL_DISABLE;
160 i2c_init.no_stretch_mode = I2C_NOSTRETCH_DISABLE;
161 i2c_init.analog_filter = 1;
162 i2c_init.digital_filter_coef = 0;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200163
Yann Gautierf3928f62019-02-14 11:15:03 +0100164 ret = stm32_i2c_init(i2c, &i2c_init);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200165 if (ret != 0) {
166 ERROR("Cannot initialize I2C %x (%d)\n",
Yann Gautierf3928f62019-02-14 11:15:03 +0100167 i2c->i2c_base_addr, ret);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200168 panic();
169 }
170
Yann Gautierf3928f62019-02-14 11:15:03 +0100171 if (!stm32_i2c_is_device_ready(i2c, pmic_i2c_addr, 1,
172 I2C_TIMEOUT_BUSY_MS)) {
173 ERROR("I2C device not ready\n");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200174 panic();
175 }
176
Yann Gautierf3928f62019-02-14 11:15:03 +0100177 stpmic1_bind_i2c(i2c, (uint16_t)pmic_i2c_addr);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200178
Yann Gautierf3928f62019-02-14 11:15:03 +0100179 return true;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200180}
181
Etienne Carriere67b106a2019-12-02 10:10:08 +0100182static void register_pmic_shared_peripherals(void)
183{
184 uintptr_t i2c_base = i2c_handle.i2c_base_addr;
185
186 if (dt_pmic_is_secure()) {
187 stm32mp_register_secure_periph_iomem(i2c_base);
188 } else {
189 if (i2c_base != 0U) {
190 stm32mp_register_non_secure_periph_iomem(i2c_base);
191 }
192 }
193}
194
Yann Gautierbb836ee2018-07-16 17:55:07 +0200195void initialize_pmic(void)
196{
Yann Gautierf3928f62019-02-14 11:15:03 +0100197 if (!initialize_pmic_i2c()) {
198 VERBOSE("No PMIC\n");
199 return;
200 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200201
Etienne Carriere67b106a2019-12-02 10:10:08 +0100202 register_pmic_shared_peripherals();
203
Yann Gautiere6f10322021-09-27 14:31:40 +0200204 if (register_pmic() < 0) {
Nicolas Le Bayon0b10b652019-11-18 13:13:36 +0100205 panic();
Yann Gautiere6f10322021-09-27 14:31:40 +0200206 }
207
208 if (stpmic1_powerctrl_on() < 0) {
209 panic();
210 }
211
Nicolas Le Bayon0b10b652019-11-18 13:13:36 +0100212}
213
214#if DEBUG
215void print_pmic_info_and_debug(void)
216{
217 unsigned long pmic_version;
218
Yann Gautierf3928f62019-02-14 11:15:03 +0100219 if (stpmic1_get_version(&pmic_version) != 0) {
220 ERROR("Failed to access PMIC\n");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200221 panic();
222 }
223
Yann Gautierf3928f62019-02-14 11:15:03 +0100224 INFO("PMIC version = 0x%02lx\n", pmic_version);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200225}
Nicolas Le Bayon0b10b652019-11-18 13:13:36 +0100226#endif
Yann Gautierbb836ee2018-07-16 17:55:07 +0200227
228int pmic_ddr_power_init(enum ddr_type ddr_type)
229{
230 bool buck3_at_1v8 = false;
231 uint8_t read_val;
232 int status;
233
234 switch (ddr_type) {
235 case STM32MP_DDR3:
236 /* Set LDO3 to sync mode */
Yann Gautiera45433b2019-01-16 18:31:00 +0100237 status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200238 if (status != 0) {
239 return status;
240 }
241
Yann Gautiera45433b2019-01-16 18:31:00 +0100242 read_val &= ~STPMIC1_LDO3_MODE;
243 read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK;
244 read_val |= STPMIC1_LDO3_DDR_SEL <<
245 STPMIC1_LDO12356_OUTPUT_SHIFT;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200246
Yann Gautiera45433b2019-01-16 18:31:00 +0100247 status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200248 if (status != 0) {
249 return status;
250 }
251
Yann Gautiera45433b2019-01-16 18:31:00 +0100252 status = stpmic1_regulator_voltage_set("buck2", 1350);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200253 if (status != 0) {
254 return status;
255 }
256
Yann Gautiera45433b2019-01-16 18:31:00 +0100257 status = stpmic1_regulator_enable("buck2");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200258 if (status != 0) {
259 return status;
260 }
261
Yann Gautiera45433b2019-01-16 18:31:00 +0100262 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200263
Yann Gautiera45433b2019-01-16 18:31:00 +0100264 status = stpmic1_regulator_enable("vref_ddr");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200265 if (status != 0) {
266 return status;
267 }
268
Yann Gautiera45433b2019-01-16 18:31:00 +0100269 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200270
Yann Gautiera45433b2019-01-16 18:31:00 +0100271 status = stpmic1_regulator_enable("ldo3");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200272 if (status != 0) {
273 return status;
274 }
275
Yann Gautiera45433b2019-01-16 18:31:00 +0100276 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200277 break;
278
279 case STM32MP_LPDDR2:
Yann Gautier917a00c2019-04-16 16:20:58 +0200280 case STM32MP_LPDDR3:
Yann Gautierbb836ee2018-07-16 17:55:07 +0200281 /*
282 * Set LDO3 to 1.8V
283 * Set LDO3 to bypass mode if BUCK3 = 1.8V
284 * Set LDO3 to normal mode if BUCK3 != 1.8V
285 */
Yann Gautiera45433b2019-01-16 18:31:00 +0100286 status = stpmic1_register_read(BUCK3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200287 if (status != 0) {
288 return status;
289 }
290
Yann Gautiera45433b2019-01-16 18:31:00 +0100291 if ((read_val & STPMIC1_BUCK3_1V8) == STPMIC1_BUCK3_1V8) {
Yann Gautierbb836ee2018-07-16 17:55:07 +0200292 buck3_at_1v8 = true;
293 }
294
Yann Gautiera45433b2019-01-16 18:31:00 +0100295 status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200296 if (status != 0) {
297 return status;
298 }
299
Yann Gautiera45433b2019-01-16 18:31:00 +0100300 read_val &= ~STPMIC1_LDO3_MODE;
301 read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK;
302 read_val |= STPMIC1_LDO3_1800000;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200303 if (buck3_at_1v8) {
Yann Gautiera45433b2019-01-16 18:31:00 +0100304 read_val |= STPMIC1_LDO3_MODE;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200305 }
306
Yann Gautiera45433b2019-01-16 18:31:00 +0100307 status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200308 if (status != 0) {
309 return status;
310 }
311
Yann Gautiera45433b2019-01-16 18:31:00 +0100312 status = stpmic1_regulator_voltage_set("buck2", 1200);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200313 if (status != 0) {
314 return status;
315 }
316
Yann Gautiera45433b2019-01-16 18:31:00 +0100317 status = stpmic1_regulator_enable("ldo3");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200318 if (status != 0) {
319 return status;
320 }
321
Yann Gautiera45433b2019-01-16 18:31:00 +0100322 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200323
Yann Gautiera45433b2019-01-16 18:31:00 +0100324 status = stpmic1_regulator_enable("buck2");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200325 if (status != 0) {
326 return status;
327 }
328
Yann Gautiera45433b2019-01-16 18:31:00 +0100329 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200330
Yann Gautiera45433b2019-01-16 18:31:00 +0100331 status = stpmic1_regulator_enable("vref_ddr");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200332 if (status != 0) {
333 return status;
334 }
335
Yann Gautiera45433b2019-01-16 18:31:00 +0100336 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200337 break;
338
339 default:
340 break;
341 };
342
343 return 0;
344}
Yann Gautiere6f10322021-09-27 14:31:40 +0200345
346enum {
347 STPMIC1_BUCK1 = 0,
348 STPMIC1_BUCK2,
349 STPMIC1_BUCK3,
350 STPMIC1_BUCK4,
351 STPMIC1_LDO1,
352 STPMIC1_LDO2,
353 STPMIC1_LDO3,
354 STPMIC1_LDO4,
355 STPMIC1_LDO5,
356 STPMIC1_LDO6,
357 STPMIC1_VREF_DDR,
358 STPMIC1_BOOST,
359 STPMIC1_VBUS_OTG,
360 STPMIC1_SW_OUT,
361};
362
363static int pmic_set_state(const struct regul_description *desc, bool enable)
364{
365 VERBOSE("%s: set state to %u\n", desc->node_name, enable);
366
367 if (enable == STATE_ENABLE) {
368 return stpmic1_regulator_enable(desc->node_name);
369 } else {
370 return stpmic1_regulator_disable(desc->node_name);
371 }
372}
373
374static int pmic_get_state(const struct regul_description *desc)
375{
376 VERBOSE("%s: get state\n", desc->node_name);
377
378 return stpmic1_is_regulator_enabled(desc->node_name);
379}
380
381static int pmic_get_voltage(const struct regul_description *desc)
382{
383 VERBOSE("%s: get volt\n", desc->node_name);
384
385 return stpmic1_regulator_voltage_get(desc->node_name);
386}
387
388static int pmic_set_voltage(const struct regul_description *desc, uint16_t mv)
389{
390 VERBOSE("%s: get volt\n", desc->node_name);
391
392 return stpmic1_regulator_voltage_set(desc->node_name, mv);
393}
394
395static int pmic_list_voltages(const struct regul_description *desc,
396 const uint16_t **levels, size_t *count)
397{
398 VERBOSE("%s: list volt\n", desc->node_name);
399
400 return stpmic1_regulator_levels_mv(desc->node_name, levels, count);
401}
402
403static int pmic_set_flag(const struct regul_description *desc, uint16_t flag)
404{
405 VERBOSE("%s: set_flag 0x%x\n", desc->node_name, flag);
406
407 switch (flag) {
408 case REGUL_OCP:
409 return stpmic1_regulator_icc_set(desc->node_name);
410
411 case REGUL_ACTIVE_DISCHARGE:
412 return stpmic1_active_discharge_mode_set(desc->node_name);
413
414 case REGUL_PULL_DOWN:
415 return stpmic1_regulator_pull_down_set(desc->node_name);
416
417 case REGUL_MASK_RESET:
418 return stpmic1_regulator_mask_reset_set(desc->node_name);
419
420 case REGUL_SINK_SOURCE:
421 return stpmic1_regulator_sink_mode_set(desc->node_name);
422
423 case REGUL_ENABLE_BYPASS:
424 return stpmic1_regulator_bypass_mode_set(desc->node_name);
425
426 default:
427 return -EINVAL;
428 }
429}
430
431struct regul_ops pmic_ops = {
432 .set_state = pmic_set_state,
433 .get_state = pmic_get_state,
434 .set_voltage = pmic_set_voltage,
435 .get_voltage = pmic_get_voltage,
436 .list_voltages = pmic_list_voltages,
437 .set_flag = pmic_set_flag,
438};
439
440#define DEFINE_REGU(name) { \
441 .node_name = name, \
442 .ops = &pmic_ops, \
443 .driver_data = NULL, \
444 .enable_ramp_delay = 1000, \
445}
446
447static const struct regul_description pmic_regs[] = {
448 [STPMIC1_BUCK1] = DEFINE_REGU("buck1"),
449 [STPMIC1_BUCK2] = DEFINE_REGU("buck2"),
450 [STPMIC1_BUCK3] = DEFINE_REGU("buck3"),
451 [STPMIC1_BUCK4] = DEFINE_REGU("buck4"),
452 [STPMIC1_LDO1] = DEFINE_REGU("ldo1"),
453 [STPMIC1_LDO2] = DEFINE_REGU("ldo2"),
454 [STPMIC1_LDO3] = DEFINE_REGU("ldo3"),
455 [STPMIC1_LDO4] = DEFINE_REGU("ldo4"),
456 [STPMIC1_LDO5] = DEFINE_REGU("ldo5"),
457 [STPMIC1_LDO6] = DEFINE_REGU("ldo6"),
458 [STPMIC1_VREF_DDR] = DEFINE_REGU("vref_ddr"),
459 [STPMIC1_BOOST] = DEFINE_REGU("boost"),
460 [STPMIC1_VBUS_OTG] = DEFINE_REGU("pwr_sw1"),
461 [STPMIC1_SW_OUT] = DEFINE_REGU("pwr_sw2"),
462};
463
464#define NB_REG ARRAY_SIZE(pmic_regs)
465
466static int register_pmic(void)
467{
468 void *fdt;
469 int pmic_node, regulators_node, subnode;
470
471 VERBOSE("Register pmic\n");
472
473 if (fdt_get_address(&fdt) == 0) {
474 return -FDT_ERR_NOTFOUND;
475 }
476
477 pmic_node = dt_get_pmic_node(fdt);
478 if (pmic_node < 0) {
479 return pmic_node;
480 }
481
482 regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators");
483 if (regulators_node < 0) {
484 return -ENOENT;
485 }
486
487 fdt_for_each_subnode(subnode, fdt, regulators_node) {
488 const char *reg_name = fdt_get_name(fdt, subnode, NULL);
489 const struct regul_description *desc;
490 unsigned int i;
491 int ret;
492
493 for (i = 0; i < NB_REG; i++) {
494 desc = &pmic_regs[i];
495 if (strcmp(desc->node_name, reg_name) == 0) {
496 break;
497 }
498 }
499 assert(i < NB_REG);
500
501 ret = regulator_register(desc, subnode);
502 if (ret != 0) {
503 WARN("%s:%d failed to register %s\n", __func__,
504 __LINE__, reg_name);
505 return ret;
506 }
507 }
508
509 return 0;
510}