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