blob: 58f97b3e4364b0409413e4f088449043384d78ec [file] [log] [blame]
Yann Gautierbb836ee2018-07-16 17:55:07 +02001/*
Patrick Delaunay507173f2023-02-16 14:29:25 +01002 * Copyright (c) 2017-2024, 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 Gautierf64eae02022-11-24 18:17:02 +010023#define NB_REG 14U
Yann Gautierbb836ee2018-07-16 17:55:07 +020024
25static struct i2c_handle_s i2c_handle;
26static uint32_t pmic_i2c_addr;
27
Yann Gautiere6f10322021-09-27 14:31:40 +020028static int register_pmic(void);
29
Yann Gautierbb836ee2018-07-16 17:55:07 +020030static int dt_get_pmic_node(void *fdt)
31{
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010032 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;
Yann Gautierbb836ee2018-07-16 17:55:07 +020039}
40
Yann Gautierf3928f62019-02-14 11:15:03 +010041int dt_pmic_status(void)
Yann Gautierbb836ee2018-07-16 17:55:07 +020042{
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010043 static int status = -FDT_ERR_BADVALUE;
Yann Gautierbb836ee2018-07-16 17:55:07 +020044 int node;
45 void *fdt;
46
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010047 if (status != -FDT_ERR_BADVALUE) {
48 return status;
49 }
50
Yann Gautierbb836ee2018-07-16 17:55:07 +020051 if (fdt_get_address(&fdt) == 0) {
Yann Gautierf3928f62019-02-14 11:15:03 +010052 return -ENOENT;
Yann Gautierbb836ee2018-07-16 17:55:07 +020053 }
54
55 node = dt_get_pmic_node(fdt);
Yann Gautierf3928f62019-02-14 11:15:03 +010056 if (node <= 0) {
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010057 status = -FDT_ERR_NOTFOUND;
58
59 return status;
Yann Gautierbb836ee2018-07-16 17:55:07 +020060 }
61
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010062 status = (int)fdt_get_status(node);
63
64 return status;
Yann Gautierbb836ee2018-07-16 17:55:07 +020065}
66
Etienne Carriere67b106a2019-12-02 10:10:08 +010067static 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
Yann Gautierf3928f62019-02-14 11:15:03 +010076/*
77 * Get PMIC and its I2C bus configuration from the device tree.
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010078 * Return 0 on success, negative on error, 1 if no PMIC node is defined.
Yann Gautierf3928f62019-02-14 11:15:03 +010079 */
80static int dt_pmic_i2c_config(struct dt_node_info *i2c_info,
81 struct stm32_i2c_init_s *init)
Yann Gautierbb836ee2018-07-16 17:55:07 +020082{
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010083 static int i2c_node = -FDT_ERR_NOTFOUND;
Yann Gautierbb836ee2018-07-16 17:55:07 +020084 void *fdt;
Yann Gautierbb836ee2018-07-16 17:55:07 +020085
86 if (fdt_get_address(&fdt) == 0) {
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010087 return -FDT_ERR_NOTFOUND;
Yann Gautierbb836ee2018-07-16 17:55:07 +020088 }
89
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010090 if (i2c_node == -FDT_ERR_NOTFOUND) {
91 int pmic_node;
92 const fdt32_t *cuint;
Yann Gautierbb836ee2018-07-16 17:55:07 +020093
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010094 pmic_node = dt_get_pmic_node(fdt);
95 if (pmic_node < 0) {
96 return PMIC_NODE_NOT_FOUND;
97 }
Yann Gautierbb836ee2018-07-16 17:55:07 +020098
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010099 cuint = fdt_getprop(fdt, pmic_node, "reg", NULL);
100 if (cuint == NULL) {
101 return -FDT_ERR_NOTFOUND;
102 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200103
Nicolas Le Bayon342865a2019-11-15 15:56:06 +0100104 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 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200113 }
114
115 dt_fill_device_info(i2c_info, i2c_node);
116 if (i2c_info->base == 0U) {
117 return -FDT_ERR_NOTFOUND;
118 }
119
Yann Gautierf3928f62019-02-14 11:15:03 +0100120 return stm32_i2c_get_setup_from_fdt(fdt, i2c_node, init);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200121}
122
Yann Gautierf3928f62019-02-14 11:15:03 +0100123bool initialize_pmic_i2c(void)
Yann Gautierbb836ee2018-07-16 17:55:07 +0200124{
125 int ret;
126 struct dt_node_info i2c_info;
Yann Gautierf3928f62019-02-14 11:15:03 +0100127 struct i2c_handle_s *i2c = &i2c_handle;
128 struct stm32_i2c_init_s i2c_init;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200129
Yann Gautierf3928f62019-02-14 11:15:03 +0100130 ret = dt_pmic_i2c_config(&i2c_info, &i2c_init);
131 if (ret < 0) {
132 ERROR("I2C configuration failed %d\n", ret);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200133 panic();
134 }
135
Yann Gautierf3928f62019-02-14 11:15:03 +0100136 if (ret != 0) {
137 return false;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200138 }
139
140 /* Initialize PMIC I2C */
Yann Gautierf3928f62019-02-14 11:15:03 +0100141 i2c->i2c_base_addr = i2c_info.base;
142 i2c->dt_status = i2c_info.status;
143 i2c->clock = i2c_info.clock;
Benjamin Gaignard53ee7d32020-02-24 13:57:40 +0100144 i2c->i2c_state = I2C_STATE_RESET;
Yann Gautierf3928f62019-02-14 11:15:03 +0100145 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;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200154
Yann Gautierf3928f62019-02-14 11:15:03 +0100155 ret = stm32_i2c_init(i2c, &i2c_init);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200156 if (ret != 0) {
157 ERROR("Cannot initialize I2C %x (%d)\n",
Yann Gautierf3928f62019-02-14 11:15:03 +0100158 i2c->i2c_base_addr, ret);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200159 panic();
160 }
161
Yann Gautierf3928f62019-02-14 11:15:03 +0100162 if (!stm32_i2c_is_device_ready(i2c, pmic_i2c_addr, 1,
163 I2C_TIMEOUT_BUSY_MS)) {
164 ERROR("I2C device not ready\n");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200165 panic();
166 }
167
Yann Gautierf3928f62019-02-14 11:15:03 +0100168 stpmic1_bind_i2c(i2c, (uint16_t)pmic_i2c_addr);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200169
Yann Gautierf3928f62019-02-14 11:15:03 +0100170 return true;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200171}
172
Etienne Carriere67b106a2019-12-02 10:10:08 +0100173static 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
Yann Gautierbb836ee2018-07-16 17:55:07 +0200186void initialize_pmic(void)
187{
Yann Gautierf3928f62019-02-14 11:15:03 +0100188 if (!initialize_pmic_i2c()) {
189 VERBOSE("No PMIC\n");
190 return;
191 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200192
Etienne Carriere67b106a2019-12-02 10:10:08 +0100193 register_pmic_shared_peripherals();
194
Yann Gautiere6f10322021-09-27 14:31:40 +0200195 if (register_pmic() < 0) {
Nicolas Le Bayon0b10b652019-11-18 13:13:36 +0100196 panic();
Yann Gautiere6f10322021-09-27 14:31:40 +0200197 }
198
199 if (stpmic1_powerctrl_on() < 0) {
200 panic();
201 }
202
Nicolas Le Bayon0b10b652019-11-18 13:13:36 +0100203}
204
205#if DEBUG
206void print_pmic_info_and_debug(void)
207{
208 unsigned long pmic_version;
209
Yann Gautierf3928f62019-02-14 11:15:03 +0100210 if (stpmic1_get_version(&pmic_version) != 0) {
211 ERROR("Failed to access PMIC\n");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200212 panic();
213 }
214
Yann Gautierf3928f62019-02-14 11:15:03 +0100215 INFO("PMIC version = 0x%02lx\n", pmic_version);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200216}
Nicolas Le Bayon0b10b652019-11-18 13:13:36 +0100217#endif
Yann Gautierbb836ee2018-07-16 17:55:07 +0200218
Yann Gautiere05e8cf2022-01-18 15:49:42 +0100219int pmic_voltages_init(void)
220{
221#if STM32MP13
222 struct rdev *buck1, *buck4;
223 int status;
224
225 buck1 = regulator_get_by_name("buck1");
226 if (buck1 == NULL) {
227 return -ENOENT;
228 }
229
230 buck4 = regulator_get_by_name("buck4");
231 if (buck4 == NULL) {
232 return -ENOENT;
233 }
234
235 status = regulator_set_min_voltage(buck1);
236 if (status != 0) {
237 return status;
238 }
239
240 status = regulator_set_min_voltage(buck4);
241 if (status != 0) {
242 return status;
243 }
244#endif
245
246 return 0;
247}
248
Yann Gautiere6f10322021-09-27 14:31:40 +0200249enum {
250 STPMIC1_BUCK1 = 0,
251 STPMIC1_BUCK2,
252 STPMIC1_BUCK3,
253 STPMIC1_BUCK4,
254 STPMIC1_LDO1,
255 STPMIC1_LDO2,
256 STPMIC1_LDO3,
257 STPMIC1_LDO4,
258 STPMIC1_LDO5,
259 STPMIC1_LDO6,
260 STPMIC1_VREF_DDR,
261 STPMIC1_BOOST,
262 STPMIC1_VBUS_OTG,
263 STPMIC1_SW_OUT,
264};
265
266static int pmic_set_state(const struct regul_description *desc, bool enable)
267{
Yann Gautierf0a74762022-01-06 09:35:35 +0100268 VERBOSE("%s: set state to %d\n", desc->node_name, enable);
Yann Gautiere6f10322021-09-27 14:31:40 +0200269
270 if (enable == STATE_ENABLE) {
271 return stpmic1_regulator_enable(desc->node_name);
272 } else {
273 return stpmic1_regulator_disable(desc->node_name);
274 }
275}
276
277static int pmic_get_state(const struct regul_description *desc)
278{
279 VERBOSE("%s: get state\n", desc->node_name);
280
281 return stpmic1_is_regulator_enabled(desc->node_name);
282}
283
284static int pmic_get_voltage(const struct regul_description *desc)
285{
286 VERBOSE("%s: get volt\n", desc->node_name);
287
288 return stpmic1_regulator_voltage_get(desc->node_name);
289}
290
291static int pmic_set_voltage(const struct regul_description *desc, uint16_t mv)
292{
293 VERBOSE("%s: get volt\n", desc->node_name);
294
295 return stpmic1_regulator_voltage_set(desc->node_name, mv);
296}
297
298static int pmic_list_voltages(const struct regul_description *desc,
299 const uint16_t **levels, size_t *count)
300{
301 VERBOSE("%s: list volt\n", desc->node_name);
302
303 return stpmic1_regulator_levels_mv(desc->node_name, levels, count);
304}
305
306static int pmic_set_flag(const struct regul_description *desc, uint16_t flag)
307{
308 VERBOSE("%s: set_flag 0x%x\n", desc->node_name, flag);
309
310 switch (flag) {
311 case REGUL_OCP:
312 return stpmic1_regulator_icc_set(desc->node_name);
313
314 case REGUL_ACTIVE_DISCHARGE:
315 return stpmic1_active_discharge_mode_set(desc->node_name);
316
317 case REGUL_PULL_DOWN:
318 return stpmic1_regulator_pull_down_set(desc->node_name);
319
320 case REGUL_MASK_RESET:
321 return stpmic1_regulator_mask_reset_set(desc->node_name);
322
323 case REGUL_SINK_SOURCE:
324 return stpmic1_regulator_sink_mode_set(desc->node_name);
325
326 case REGUL_ENABLE_BYPASS:
327 return stpmic1_regulator_bypass_mode_set(desc->node_name);
328
329 default:
330 return -EINVAL;
331 }
332}
333
Yann Gautierb62e1172022-02-09 17:35:45 +0100334static const struct regul_ops pmic_ops = {
Yann Gautiere6f10322021-09-27 14:31:40 +0200335 .set_state = pmic_set_state,
336 .get_state = pmic_get_state,
337 .set_voltage = pmic_set_voltage,
338 .get_voltage = pmic_get_voltage,
339 .list_voltages = pmic_list_voltages,
340 .set_flag = pmic_set_flag,
341};
342
343#define DEFINE_REGU(name) { \
Yann Gautier4737ed62022-11-24 18:14:26 +0100344 .node_name = (name), \
Yann Gautiere6f10322021-09-27 14:31:40 +0200345 .ops = &pmic_ops, \
346 .driver_data = NULL, \
347 .enable_ramp_delay = 1000, \
348}
349
Yann Gautierf64eae02022-11-24 18:17:02 +0100350static const struct regul_description pmic_regs[NB_REG] = {
Yann Gautiere6f10322021-09-27 14:31:40 +0200351 [STPMIC1_BUCK1] = DEFINE_REGU("buck1"),
352 [STPMIC1_BUCK2] = DEFINE_REGU("buck2"),
353 [STPMIC1_BUCK3] = DEFINE_REGU("buck3"),
354 [STPMIC1_BUCK4] = DEFINE_REGU("buck4"),
355 [STPMIC1_LDO1] = DEFINE_REGU("ldo1"),
356 [STPMIC1_LDO2] = DEFINE_REGU("ldo2"),
357 [STPMIC1_LDO3] = DEFINE_REGU("ldo3"),
358 [STPMIC1_LDO4] = DEFINE_REGU("ldo4"),
359 [STPMIC1_LDO5] = DEFINE_REGU("ldo5"),
360 [STPMIC1_LDO6] = DEFINE_REGU("ldo6"),
361 [STPMIC1_VREF_DDR] = DEFINE_REGU("vref_ddr"),
362 [STPMIC1_BOOST] = DEFINE_REGU("boost"),
363 [STPMIC1_VBUS_OTG] = DEFINE_REGU("pwr_sw1"),
364 [STPMIC1_SW_OUT] = DEFINE_REGU("pwr_sw2"),
365};
366
Yann Gautiere6f10322021-09-27 14:31:40 +0200367static int register_pmic(void)
368{
369 void *fdt;
370 int pmic_node, regulators_node, subnode;
371
372 VERBOSE("Register pmic\n");
373
374 if (fdt_get_address(&fdt) == 0) {
375 return -FDT_ERR_NOTFOUND;
376 }
377
378 pmic_node = dt_get_pmic_node(fdt);
379 if (pmic_node < 0) {
380 return pmic_node;
381 }
382
383 regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators");
384 if (regulators_node < 0) {
385 return -ENOENT;
386 }
387
388 fdt_for_each_subnode(subnode, fdt, regulators_node) {
389 const char *reg_name = fdt_get_name(fdt, subnode, NULL);
390 const struct regul_description *desc;
391 unsigned int i;
392 int ret;
393
Yann Gautierf64eae02022-11-24 18:17:02 +0100394 for (i = 0U; i < NB_REG; i++) {
Yann Gautiere6f10322021-09-27 14:31:40 +0200395 desc = &pmic_regs[i];
396 if (strcmp(desc->node_name, reg_name) == 0) {
397 break;
398 }
399 }
400 assert(i < NB_REG);
401
402 ret = regulator_register(desc, subnode);
403 if (ret != 0) {
404 WARN("%s:%d failed to register %s\n", __func__,
405 __LINE__, reg_name);
406 return ret;
407 }
408 }
409
410 return 0;
411}