1 /*
2 * Copyright (c) 2017-2022, STMicroelectronics - All Rights Reserved
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <assert.h>
8 #include <errno.h>
9
10 #include <common/debug.h>
11 #include <drivers/delay_timer.h>
12 #include <drivers/st/regulator.h>
13 #include <drivers/st/stm32_i2c.h>
14 #include <drivers/st/stm32mp_pmic.h>
15 #include <drivers/st/stpmic1.h>
16 #include <lib/mmio.h>
17 #include <lib/utils_def.h>
18 #include <libfdt.h>
19
20 #include <platform_def.h>
21
22 #define PMIC_NODE_NOT_FOUND 1
23 #define NB_REG 14U
24
25 static struct i2c_handle_s i2c_handle;
26 static uint32_t pmic_i2c_addr;
27
28 static int register_pmic(void);
29
dt_get_pmic_node(void * fdt)30 static int dt_get_pmic_node(void *fdt)
31 {
32 static int node = -FDT_ERR_BADOFFSET;
33
34 if (node == -FDT_ERR_BADOFFSET) {
35 node = fdt_node_offset_by_compatible(fdt, -1, "st,stpmic1");
36 }
37
38 return node;
39 }
40
dt_pmic_status(void)41 int dt_pmic_status(void)
42 {
43 static int status = -FDT_ERR_BADVALUE;
44 int node;
45 void *fdt;
46
47 if (status != -FDT_ERR_BADVALUE) {
48 return status;
49 }
50
51 if (fdt_get_address(&fdt) == 0) {
52 return -ENOENT;
53 }
54
55 node = dt_get_pmic_node(fdt);
56 if (node <= 0) {
57 status = -FDT_ERR_NOTFOUND;
58
59 return status;
60 }
61
62 status = (int)fdt_get_status(node);
63
64 return status;
65 }
66
dt_pmic_is_secure(void)67 static bool dt_pmic_is_secure(void)
68 {
69 int status = dt_pmic_status();
70
71 return (status >= 0) &&
72 (status == DT_SECURE) &&
73 (i2c_handle.dt_status == DT_SECURE);
74 }
75
76 /*
77 * Get PMIC and its I2C bus configuration from the device tree.
78 * Return 0 on success, negative on error, 1 if no PMIC node is defined.
79 */
dt_pmic_i2c_config(struct dt_node_info * i2c_info,struct stm32_i2c_init_s * init)80 static int dt_pmic_i2c_config(struct dt_node_info *i2c_info,
81 struct stm32_i2c_init_s *init)
82 {
83 static int i2c_node = -FDT_ERR_NOTFOUND;
84 void *fdt;
85
86 if (fdt_get_address(&fdt) == 0) {
87 return -FDT_ERR_NOTFOUND;
88 }
89
90 if (i2c_node == -FDT_ERR_NOTFOUND) {
91 int pmic_node;
92 const fdt32_t *cuint;
93
94 pmic_node = dt_get_pmic_node(fdt);
95 if (pmic_node < 0) {
96 return PMIC_NODE_NOT_FOUND;
97 }
98
99 cuint = fdt_getprop(fdt, pmic_node, "reg", NULL);
100 if (cuint == NULL) {
101 return -FDT_ERR_NOTFOUND;
102 }
103
104 pmic_i2c_addr = fdt32_to_cpu(*cuint) << 1;
105 if (pmic_i2c_addr > UINT16_MAX) {
106 return -FDT_ERR_BADVALUE;
107 }
108
109 i2c_node = fdt_parent_offset(fdt, pmic_node);
110 if (i2c_node < 0) {
111 return -FDT_ERR_NOTFOUND;
112 }
113 }
114
115 dt_fill_device_info(i2c_info, i2c_node);
116 if (i2c_info->base == 0U) {
117 return -FDT_ERR_NOTFOUND;
118 }
119
120 return stm32_i2c_get_setup_from_fdt(fdt, i2c_node, init);
121 }
122
initialize_pmic_i2c(void)123 bool initialize_pmic_i2c(void)
124 {
125 int ret;
126 struct dt_node_info i2c_info;
127 struct i2c_handle_s *i2c = &i2c_handle;
128 struct stm32_i2c_init_s i2c_init;
129
130 ret = dt_pmic_i2c_config(&i2c_info, &i2c_init);
131 if (ret < 0) {
132 ERROR("I2C configuration failed %d\n", ret);
133 panic();
134 }
135
136 if (ret != 0) {
137 return false;
138 }
139
140 /* Initialize PMIC I2C */
141 i2c->i2c_base_addr = i2c_info.base;
142 i2c->dt_status = i2c_info.status;
143 i2c->clock = i2c_info.clock;
144 i2c->i2c_state = I2C_STATE_RESET;
145 i2c_init.own_address1 = pmic_i2c_addr;
146 i2c_init.addressing_mode = I2C_ADDRESSINGMODE_7BIT;
147 i2c_init.dual_address_mode = I2C_DUALADDRESS_DISABLE;
148 i2c_init.own_address2 = 0;
149 i2c_init.own_address2_masks = I2C_OAR2_OA2NOMASK;
150 i2c_init.general_call_mode = I2C_GENERALCALL_DISABLE;
151 i2c_init.no_stretch_mode = I2C_NOSTRETCH_DISABLE;
152 i2c_init.analog_filter = 1;
153 i2c_init.digital_filter_coef = 0;
154
155 ret = stm32_i2c_init(i2c, &i2c_init);
156 if (ret != 0) {
157 ERROR("Cannot initialize I2C %x (%d)\n",
158 i2c->i2c_base_addr, ret);
159 panic();
160 }
161
162 if (!stm32_i2c_is_device_ready(i2c, pmic_i2c_addr, 1,
163 I2C_TIMEOUT_BUSY_MS)) {
164 ERROR("I2C device not ready\n");
165 panic();
166 }
167
168 stpmic1_bind_i2c(i2c, (uint16_t)pmic_i2c_addr);
169
170 return true;
171 }
172
register_pmic_shared_peripherals(void)173 static void register_pmic_shared_peripherals(void)
174 {
175 uintptr_t i2c_base = i2c_handle.i2c_base_addr;
176
177 if (dt_pmic_is_secure()) {
178 stm32mp_register_secure_periph_iomem(i2c_base);
179 } else {
180 if (i2c_base != 0U) {
181 stm32mp_register_non_secure_periph_iomem(i2c_base);
182 }
183 }
184 }
185
initialize_pmic(void)186 void initialize_pmic(void)
187 {
188 if (!initialize_pmic_i2c()) {
189 VERBOSE("No PMIC\n");
190 return;
191 }
192
193 register_pmic_shared_peripherals();
194
195 if (register_pmic() < 0) {
196 panic();
197 }
198
199 if (stpmic1_powerctrl_on() < 0) {
200 panic();
201 }
202
203 }
204
205 #if DEBUG
print_pmic_info_and_debug(void)206 void print_pmic_info_and_debug(void)
207 {
208 unsigned long pmic_version;
209
210 if (stpmic1_get_version(&pmic_version) != 0) {
211 ERROR("Failed to access PMIC\n");
212 panic();
213 }
214
215 INFO("PMIC version = 0x%02lx\n", pmic_version);
216 }
217 #endif
218
pmic_ddr_power_init(enum ddr_type ddr_type)219 int pmic_ddr_power_init(enum ddr_type ddr_type)
220 {
221 int status;
222 uint16_t buck3_min_mv;
223 struct rdev *buck2, *buck3, *vref;
224 struct rdev *ldo3 __unused;
225
226 buck2 = regulator_get_by_name("buck2");
227 if (buck2 == NULL) {
228 return -ENOENT;
229 }
230
231 #if STM32MP15
232 ldo3 = regulator_get_by_name("ldo3");
233 if (ldo3 == NULL) {
234 return -ENOENT;
235 }
236 #endif
237
238 vref = regulator_get_by_name("vref_ddr");
239 if (vref == NULL) {
240 return -ENOENT;
241 }
242
243 switch (ddr_type) {
244 case STM32MP_DDR3:
245 #if STM32MP15
246 status = regulator_set_flag(ldo3, REGUL_SINK_SOURCE);
247 if (status != 0) {
248 return status;
249 }
250 #endif
251
252 status = regulator_set_min_voltage(buck2);
253 if (status != 0) {
254 return status;
255 }
256
257 status = regulator_enable(buck2);
258 if (status != 0) {
259 return status;
260 }
261
262 status = regulator_enable(vref);
263 if (status != 0) {
264 return status;
265 }
266
267 #if STM32MP15
268 status = regulator_enable(ldo3);
269 if (status != 0) {
270 return status;
271 }
272 #endif
273 break;
274
275 case STM32MP_LPDDR2:
276 case STM32MP_LPDDR3:
277 /*
278 * Set LDO3 to 1.8V
279 * Set LDO3 to bypass mode if BUCK3 = 1.8V
280 * Set LDO3 to normal mode if BUCK3 != 1.8V
281 */
282 buck3 = regulator_get_by_name("buck3");
283 if (buck3 == NULL) {
284 return -ENOENT;
285 }
286
287 regulator_get_range(buck3, &buck3_min_mv, NULL);
288
289 #if STM32MP15
290 if (buck3_min_mv != 1800) {
291 status = regulator_set_min_voltage(ldo3);
292 if (status != 0) {
293 return status;
294 }
295 } else {
296 status = regulator_set_flag(ldo3, REGUL_ENABLE_BYPASS);
297 if (status != 0) {
298 return status;
299 }
300 }
301 #endif
302
303 status = regulator_set_min_voltage(buck2);
304 if (status != 0) {
305 return status;
306 }
307
308 #if STM32MP15
309 status = regulator_enable(ldo3);
310 if (status != 0) {
311 return status;
312 }
313 #endif
314
315 status = regulator_enable(buck2);
316 if (status != 0) {
317 return status;
318 }
319
320 status = regulator_enable(vref);
321 if (status != 0) {
322 return status;
323 }
324 break;
325
326 default:
327 break;
328 };
329
330 return 0;
331 }
332
pmic_voltages_init(void)333 int pmic_voltages_init(void)
334 {
335 #if STM32MP13
336 struct rdev *buck1, *buck4;
337 int status;
338
339 buck1 = regulator_get_by_name("buck1");
340 if (buck1 == NULL) {
341 return -ENOENT;
342 }
343
344 buck4 = regulator_get_by_name("buck4");
345 if (buck4 == NULL) {
346 return -ENOENT;
347 }
348
349 status = regulator_set_min_voltage(buck1);
350 if (status != 0) {
351 return status;
352 }
353
354 status = regulator_set_min_voltage(buck4);
355 if (status != 0) {
356 return status;
357 }
358 #endif
359
360 return 0;
361 }
362
363 enum {
364 STPMIC1_BUCK1 = 0,
365 STPMIC1_BUCK2,
366 STPMIC1_BUCK3,
367 STPMIC1_BUCK4,
368 STPMIC1_LDO1,
369 STPMIC1_LDO2,
370 STPMIC1_LDO3,
371 STPMIC1_LDO4,
372 STPMIC1_LDO5,
373 STPMIC1_LDO6,
374 STPMIC1_VREF_DDR,
375 STPMIC1_BOOST,
376 STPMIC1_VBUS_OTG,
377 STPMIC1_SW_OUT,
378 };
379
pmic_set_state(const struct regul_description * desc,bool enable)380 static int pmic_set_state(const struct regul_description *desc, bool enable)
381 {
382 VERBOSE("%s: set state to %d\n", desc->node_name, enable);
383
384 if (enable == STATE_ENABLE) {
385 return stpmic1_regulator_enable(desc->node_name);
386 } else {
387 return stpmic1_regulator_disable(desc->node_name);
388 }
389 }
390
pmic_get_state(const struct regul_description * desc)391 static int pmic_get_state(const struct regul_description *desc)
392 {
393 VERBOSE("%s: get state\n", desc->node_name);
394
395 return stpmic1_is_regulator_enabled(desc->node_name);
396 }
397
pmic_get_voltage(const struct regul_description * desc)398 static int pmic_get_voltage(const struct regul_description *desc)
399 {
400 VERBOSE("%s: get volt\n", desc->node_name);
401
402 return stpmic1_regulator_voltage_get(desc->node_name);
403 }
404
pmic_set_voltage(const struct regul_description * desc,uint16_t mv)405 static int pmic_set_voltage(const struct regul_description *desc, uint16_t mv)
406 {
407 VERBOSE("%s: get volt\n", desc->node_name);
408
409 return stpmic1_regulator_voltage_set(desc->node_name, mv);
410 }
411
pmic_list_voltages(const struct regul_description * desc,const uint16_t ** levels,size_t * count)412 static int pmic_list_voltages(const struct regul_description *desc,
413 const uint16_t **levels, size_t *count)
414 {
415 VERBOSE("%s: list volt\n", desc->node_name);
416
417 return stpmic1_regulator_levels_mv(desc->node_name, levels, count);
418 }
419
pmic_set_flag(const struct regul_description * desc,uint16_t flag)420 static int pmic_set_flag(const struct regul_description *desc, uint16_t flag)
421 {
422 VERBOSE("%s: set_flag 0x%x\n", desc->node_name, flag);
423
424 switch (flag) {
425 case REGUL_OCP:
426 return stpmic1_regulator_icc_set(desc->node_name);
427
428 case REGUL_ACTIVE_DISCHARGE:
429 return stpmic1_active_discharge_mode_set(desc->node_name);
430
431 case REGUL_PULL_DOWN:
432 return stpmic1_regulator_pull_down_set(desc->node_name);
433
434 case REGUL_MASK_RESET:
435 return stpmic1_regulator_mask_reset_set(desc->node_name);
436
437 case REGUL_SINK_SOURCE:
438 return stpmic1_regulator_sink_mode_set(desc->node_name);
439
440 case REGUL_ENABLE_BYPASS:
441 return stpmic1_regulator_bypass_mode_set(desc->node_name);
442
443 default:
444 return -EINVAL;
445 }
446 }
447
448 static const struct regul_ops pmic_ops = {
449 .set_state = pmic_set_state,
450 .get_state = pmic_get_state,
451 .set_voltage = pmic_set_voltage,
452 .get_voltage = pmic_get_voltage,
453 .list_voltages = pmic_list_voltages,
454 .set_flag = pmic_set_flag,
455 };
456
457 #define DEFINE_REGU(name) { \
458 .node_name = (name), \
459 .ops = &pmic_ops, \
460 .driver_data = NULL, \
461 .enable_ramp_delay = 1000, \
462 }
463
464 static const struct regul_description pmic_regs[NB_REG] = {
465 [STPMIC1_BUCK1] = DEFINE_REGU("buck1"),
466 [STPMIC1_BUCK2] = DEFINE_REGU("buck2"),
467 [STPMIC1_BUCK3] = DEFINE_REGU("buck3"),
468 [STPMIC1_BUCK4] = DEFINE_REGU("buck4"),
469 [STPMIC1_LDO1] = DEFINE_REGU("ldo1"),
470 [STPMIC1_LDO2] = DEFINE_REGU("ldo2"),
471 [STPMIC1_LDO3] = DEFINE_REGU("ldo3"),
472 [STPMIC1_LDO4] = DEFINE_REGU("ldo4"),
473 [STPMIC1_LDO5] = DEFINE_REGU("ldo5"),
474 [STPMIC1_LDO6] = DEFINE_REGU("ldo6"),
475 [STPMIC1_VREF_DDR] = DEFINE_REGU("vref_ddr"),
476 [STPMIC1_BOOST] = DEFINE_REGU("boost"),
477 [STPMIC1_VBUS_OTG] = DEFINE_REGU("pwr_sw1"),
478 [STPMIC1_SW_OUT] = DEFINE_REGU("pwr_sw2"),
479 };
480
register_pmic(void)481 static int register_pmic(void)
482 {
483 void *fdt;
484 int pmic_node, regulators_node, subnode;
485
486 VERBOSE("Register pmic\n");
487
488 if (fdt_get_address(&fdt) == 0) {
489 return -FDT_ERR_NOTFOUND;
490 }
491
492 pmic_node = dt_get_pmic_node(fdt);
493 if (pmic_node < 0) {
494 return pmic_node;
495 }
496
497 regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators");
498 if (regulators_node < 0) {
499 return -ENOENT;
500 }
501
502 fdt_for_each_subnode(subnode, fdt, regulators_node) {
503 const char *reg_name = fdt_get_name(fdt, subnode, NULL);
504 const struct regul_description *desc;
505 unsigned int i;
506 int ret;
507
508 for (i = 0U; i < NB_REG; i++) {
509 desc = &pmic_regs[i];
510 if (strcmp(desc->node_name, reg_name) == 0) {
511 break;
512 }
513 }
514 assert(i < NB_REG);
515
516 ret = regulator_register(desc, subnode);
517 if (ret != 0) {
518 WARN("%s:%d failed to register %s\n", __func__,
519 __LINE__, reg_name);
520 return ret;
521 }
522 }
523
524 return 0;
525 }
526