| 1 |
/* |
| 2 |
* Copyright (c) 2017-2024, 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 |
|
| 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 |
|
| 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 |
|
| 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 |
*/ |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
| 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 |
|
| 219 |
int 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 |
|
| 249 |
enum { |
| 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 |
|
| 266 |
static int pmic_set_state(const struct regul_description *desc, bool enable) |
| 267 |
{ |
| 268 |
VERBOSE("%s: set state to %d\n", desc->node_name, enable); |
| 269 |
|
| 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 |
|
| 277 |
static 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 |
|
| 284 |
static 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 |
|
| 291 |
static 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 |
|
| 298 |
static 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 |
|
| 306 |
static 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 |
|
| 334 |
static const struct regul_ops pmic_ops = { |
| 335 |
.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) { \ |
| 344 |
.node_name = (name), \ |
| 345 |
.ops = &pmic_ops, \ |
| 346 |
.driver_data = NULL, \ |
| 347 |
.enable_ramp_delay = 1000, \ |
| 348 |
} |
| 349 |
|
| 350 |
static const struct regul_description pmic_regs[NB_REG] = { |
| 351 |
[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 |
|
| 367 |
static 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 |
|
| 394 |
for (i = 0U; i < NB_REG; i++) { |
| 395 |
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 |
} |