Pascal Paillet | 3263aea | 2022-12-16 14:59:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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_pmic2.h> |
| 15 | #include <drivers/st/stpmic2.h> |
| 16 | #include <lib/mmio.h> |
| 17 | #include <lib/spinlock.h> |
| 18 | #include <lib/utils_def.h> |
| 19 | #include <libfdt.h> |
| 20 | |
| 21 | #include <platform_def.h> |
| 22 | |
| 23 | #define PMIC_NODE_NOT_FOUND 1 |
| 24 | |
| 25 | struct regul_handle_s { |
| 26 | const uint32_t id; |
| 27 | uint16_t bypass_mv; |
| 28 | }; |
| 29 | |
| 30 | static struct pmic_handle_s pmic2_handle; |
| 31 | static struct i2c_handle_s i2c_handle; |
| 32 | |
| 33 | /* This driver is monoinstance */ |
| 34 | static struct pmic_handle_s *pmic2; |
| 35 | |
| 36 | static int dt_get_pmic_node(void *fdt) |
| 37 | { |
| 38 | static int node = -FDT_ERR_BADOFFSET; |
| 39 | |
| 40 | if (node == -FDT_ERR_BADOFFSET) { |
| 41 | node = fdt_node_offset_by_compatible(fdt, -1, "st,stpmic2"); |
| 42 | } |
| 43 | |
| 44 | return node; |
| 45 | } |
| 46 | |
| 47 | int dt_pmic_status(void) |
| 48 | { |
| 49 | static int status = -FDT_ERR_BADVALUE; |
| 50 | int node; |
| 51 | void *fdt; |
| 52 | |
| 53 | if (status != -FDT_ERR_BADVALUE) { |
| 54 | return status; |
| 55 | } |
| 56 | |
| 57 | if (fdt_get_address(&fdt) == 0) { |
| 58 | return -ENOENT; |
| 59 | } |
| 60 | |
| 61 | node = dt_get_pmic_node(fdt); |
| 62 | if (node <= 0) { |
| 63 | status = -FDT_ERR_NOTFOUND; |
| 64 | |
| 65 | return status; |
| 66 | } |
| 67 | |
| 68 | status = DT_SECURE; |
| 69 | |
| 70 | return status; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Get PMIC and its I2C bus configuration from the device tree. |
| 75 | * Return 0 on success, negative on error, 1 if no PMIC node is defined. |
| 76 | */ |
| 77 | static int dt_pmic2_i2c_config(struct dt_node_info *i2c_info, |
| 78 | struct stm32_i2c_init_s *init, |
| 79 | uint32_t *i2c_addr) |
| 80 | { |
| 81 | static int i2c_node = -FDT_ERR_NOTFOUND; |
| 82 | void *fdt; |
| 83 | |
| 84 | if (fdt_get_address(&fdt) == 0) { |
| 85 | return -FDT_ERR_NOTFOUND; |
| 86 | } |
| 87 | |
| 88 | if (i2c_node == -FDT_ERR_NOTFOUND) { |
| 89 | int pmic_node; |
| 90 | const fdt32_t *cuint; |
| 91 | |
| 92 | pmic_node = dt_get_pmic_node(fdt); |
| 93 | if (pmic_node < 0) { |
| 94 | return PMIC_NODE_NOT_FOUND; |
| 95 | } |
| 96 | |
| 97 | cuint = fdt_getprop(fdt, pmic_node, "reg", NULL); |
| 98 | if (cuint == NULL) { |
| 99 | return -FDT_ERR_NOTFOUND; |
| 100 | } |
| 101 | |
| 102 | *i2c_addr = fdt32_to_cpu(*cuint) << 1; |
| 103 | if (*i2c_addr > UINT16_MAX) { |
| 104 | return -FDT_ERR_BADVALUE; |
| 105 | } |
| 106 | |
| 107 | i2c_node = fdt_parent_offset(fdt, pmic_node); |
| 108 | if (i2c_node < 0) { |
| 109 | return -FDT_ERR_NOTFOUND; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | dt_fill_device_info(i2c_info, i2c_node); |
| 114 | if (i2c_info->base == 0U) { |
| 115 | return -FDT_ERR_NOTFOUND; |
| 116 | } |
| 117 | |
| 118 | i2c_info->status = DT_SECURE; |
| 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 | uint32_t i2c_addr = 0U; |
| 129 | struct stm32_i2c_init_s i2c_init; |
| 130 | |
| 131 | ret = dt_pmic2_i2c_config(&i2c_info, &i2c_init, &i2c_addr); |
| 132 | if (ret < 0) { |
| 133 | ERROR("I2C configuration failed %d\n", ret); |
| 134 | panic(); |
| 135 | } |
| 136 | |
| 137 | if (ret != 0) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | /* Initialize PMIC I2C */ |
| 142 | i2c->i2c_base_addr = i2c_info.base; |
| 143 | i2c->dt_status = i2c_info.status; |
| 144 | i2c->clock = i2c_info.clock; |
| 145 | i2c->i2c_state = I2C_STATE_RESET; |
| 146 | i2c_init.own_address1 = i2c_addr; |
| 147 | i2c_init.addressing_mode = I2C_ADDRESSINGMODE_7BIT; |
| 148 | i2c_init.dual_address_mode = I2C_DUALADDRESS_DISABLE; |
| 149 | i2c_init.own_address2 = 0; |
| 150 | i2c_init.own_address2_masks = I2C_OAR2_OA2NOMASK; |
| 151 | i2c_init.general_call_mode = I2C_GENERALCALL_DISABLE; |
| 152 | i2c_init.no_stretch_mode = I2C_NOSTRETCH_DISABLE; |
| 153 | i2c_init.analog_filter = 1; |
| 154 | i2c_init.digital_filter_coef = 0; |
| 155 | |
| 156 | ret = stm32_i2c_init(i2c, &i2c_init); |
| 157 | if (ret != 0) { |
| 158 | ERROR("Cannot initialize I2C %x (%d)\n", |
| 159 | i2c->i2c_base_addr, ret); |
| 160 | panic(); |
| 161 | } |
| 162 | |
| 163 | if (!stm32_i2c_is_device_ready(i2c, i2c_addr, 1, |
| 164 | I2C_TIMEOUT_BUSY_MS)) { |
| 165 | ERROR("I2C device not ready\n"); |
| 166 | panic(); |
| 167 | } |
| 168 | |
| 169 | pmic2 = &pmic2_handle; |
| 170 | pmic2->i2c_handle = &i2c_handle; |
| 171 | pmic2->i2c_addr = i2c_addr; |
| 172 | |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | static int pmic2_set_state(const struct regul_description *desc, bool enable) |
| 177 | { |
| 178 | struct regul_handle_s *regul = (struct regul_handle_s *)desc->driver_data; |
| 179 | |
| 180 | VERBOSE("%s: set state to %d\n", desc->node_name, enable); |
| 181 | |
| 182 | return stpmic2_regulator_set_state(pmic2, regul->id, enable); |
| 183 | } |
| 184 | |
| 185 | static int pmic2_get_state(const struct regul_description *desc) |
| 186 | { |
| 187 | struct regul_handle_s *regul = (struct regul_handle_s *)desc->driver_data; |
| 188 | bool enabled; |
| 189 | |
| 190 | VERBOSE("%s: get state\n", desc->node_name); |
| 191 | |
| 192 | if (stpmic2_regulator_get_state(pmic2, regul->id, &enabled) < 0) { |
| 193 | panic(); |
| 194 | } |
| 195 | |
| 196 | return enabled; |
| 197 | } |
| 198 | |
| 199 | static int pmic2_get_voltage(const struct regul_description *desc) |
| 200 | { |
| 201 | struct regul_handle_s *regul = (struct regul_handle_s *)desc->driver_data; |
| 202 | uint16_t mv; |
| 203 | |
| 204 | VERBOSE("%s: get volt\n", desc->node_name); |
| 205 | |
| 206 | if (regul->bypass_mv != 0U) { |
| 207 | int ret; |
| 208 | |
| 209 | /* If the regul is in bypass mode, return bypass value */ |
| 210 | ret = stpmic2_regulator_get_prop(pmic2, regul->id, STPMIC2_BYPASS); |
| 211 | if (ret < 0) { |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | if (ret == 1) { |
| 216 | return regul->bypass_mv; |
| 217 | } |
| 218 | }; |
| 219 | |
| 220 | if (stpmic2_regulator_get_voltage(pmic2, regul->id, &mv) < 0) { |
| 221 | panic(); |
| 222 | } |
| 223 | |
| 224 | return mv; |
| 225 | } |
| 226 | |
| 227 | static int pmic2_set_voltage(const struct regul_description *desc, uint16_t mv) |
| 228 | { |
| 229 | struct regul_handle_s *regul = (struct regul_handle_s *)desc->driver_data; |
| 230 | |
| 231 | VERBOSE("%s: set volt\n", desc->node_name); |
| 232 | |
| 233 | if (regul->bypass_mv != 0U) { |
| 234 | int ret; |
| 235 | |
| 236 | /* If the regul is in bypass mode, authorize bypass mV */ |
| 237 | ret = stpmic2_regulator_get_prop(pmic2, regul->id, STPMIC2_BYPASS); |
| 238 | if (ret < 0) { |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | if ((ret == 1) && (mv != regul->bypass_mv)) { |
| 243 | return -EPERM; |
| 244 | } |
| 245 | }; |
| 246 | |
| 247 | return stpmic2_regulator_set_voltage(pmic2, regul->id, mv); |
| 248 | } |
| 249 | |
| 250 | static int pmic2_list_voltages(const struct regul_description *desc, |
| 251 | const uint16_t **levels, size_t *count) |
| 252 | { |
| 253 | struct regul_handle_s *regul = (struct regul_handle_s *)desc->driver_data; |
| 254 | |
| 255 | VERBOSE("%s: list volt\n", desc->node_name); |
| 256 | |
| 257 | if (regul->bypass_mv != 0U) { |
| 258 | int ret; |
| 259 | |
| 260 | ret = stpmic2_regulator_get_prop(pmic2, regul->id, STPMIC2_BYPASS); |
| 261 | if (ret < 0) { |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | /* bypass is enabled, return a list with only bypass mV */ |
| 266 | if (ret == 1) { |
| 267 | if (count != NULL) { |
| 268 | *count = 1U; |
| 269 | } |
| 270 | if (levels != NULL) { |
| 271 | *levels = ®ul->bypass_mv; |
| 272 | } |
| 273 | return 0; |
| 274 | } |
| 275 | }; |
| 276 | |
| 277 | return stpmic2_regulator_levels_mv(pmic2, regul->id, levels, count); |
| 278 | } |
| 279 | |
| 280 | static int pmic2_set_flag(const struct regul_description *desc, uint16_t flag) |
| 281 | { |
| 282 | struct regul_handle_s *regul = (struct regul_handle_s *)desc->driver_data; |
| 283 | uint32_t id = regul->id; |
| 284 | int ret = -EPERM; |
| 285 | |
| 286 | VERBOSE("%s: set_flag 0x%x\n", desc->node_name, flag); |
| 287 | |
| 288 | switch (flag) { |
| 289 | case REGUL_PULL_DOWN: |
| 290 | ret = stpmic2_regulator_set_prop(pmic2, id, STPMIC2_PULL_DOWN, 1U); |
| 291 | break; |
| 292 | case REGUL_OCP: |
| 293 | ret = stpmic2_regulator_set_prop(pmic2, id, STPMIC2_OCP, 1U); |
| 294 | break; |
| 295 | case REGUL_SINK_SOURCE: |
| 296 | ret = stpmic2_regulator_set_prop(pmic2, id, STPMIC2_SINK_SOURCE, 1U); |
| 297 | break; |
| 298 | case REGUL_ENABLE_BYPASS: |
| 299 | ret = stpmic2_regulator_set_prop(pmic2, id, STPMIC2_BYPASS, 1U); |
| 300 | break; |
| 301 | case REGUL_MASK_RESET: |
| 302 | ret = stpmic2_regulator_set_prop(pmic2, id, STPMIC2_MASK_RESET, 1U); |
| 303 | break; |
| 304 | default: |
| 305 | ERROR("Invalid flag %u", flag); |
| 306 | panic(); |
| 307 | } |
| 308 | |
| 309 | if (ret != 0) { |
| 310 | return -EPERM; |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | int stpmic2_set_prop(const struct regul_description *desc, uint16_t prop, uint32_t value) |
| 317 | { |
| 318 | struct regul_handle_s *regul = (struct regul_handle_s *)desc->driver_data; |
| 319 | int ret; |
| 320 | |
| 321 | VERBOSE("%s: set_prop 0x%x val=%u\n", desc->node_name, prop, value); |
| 322 | |
| 323 | ret = stpmic2_regulator_set_prop(pmic2, regul->id, prop, value); |
| 324 | if (ret != 0) |
| 325 | return -EPERM; |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static struct regul_ops pmic2_ops = { |
| 331 | .set_state = pmic2_set_state, |
| 332 | .get_state = pmic2_get_state, |
| 333 | .set_voltage = pmic2_set_voltage, |
| 334 | .get_voltage = pmic2_get_voltage, |
| 335 | .list_voltages = pmic2_list_voltages, |
| 336 | .set_flag = pmic2_set_flag, |
| 337 | }; |
| 338 | |
| 339 | #define DEFINE_PMIC_REGUL_HANDLE(rid) \ |
| 340 | [(rid)] = { \ |
| 341 | .id = (rid), \ |
| 342 | } |
| 343 | |
| 344 | static struct regul_handle_s pmic2_regul_handles[STPMIC2_NB_REG] = { |
| 345 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_BUCK1), |
| 346 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_BUCK2), |
| 347 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_BUCK3), |
| 348 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_BUCK4), |
| 349 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_BUCK5), |
| 350 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_BUCK6), |
| 351 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_BUCK7), |
| 352 | |
| 353 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_LDO1), |
| 354 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_LDO2), |
| 355 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_LDO3), |
| 356 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_LDO4), |
| 357 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_LDO5), |
| 358 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_LDO6), |
| 359 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_LDO7), |
| 360 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_LDO8), |
| 361 | |
| 362 | DEFINE_PMIC_REGUL_HANDLE(STPMIC2_REFDDR), |
| 363 | }; |
| 364 | |
| 365 | #define DEFINE_REGUL(rid, name) \ |
| 366 | [rid] = { \ |
| 367 | .node_name = name, \ |
| 368 | .ops = &pmic2_ops, \ |
| 369 | .driver_data = &pmic2_regul_handles[rid], \ |
| 370 | } |
| 371 | |
| 372 | static const struct regul_description pmic2_descs[STPMIC2_NB_REG] = { |
| 373 | DEFINE_REGUL(STPMIC2_BUCK1, "buck1"), |
| 374 | DEFINE_REGUL(STPMIC2_BUCK2, "buck2"), |
| 375 | DEFINE_REGUL(STPMIC2_BUCK3, "buck3"), |
| 376 | DEFINE_REGUL(STPMIC2_BUCK4, "buck4"), |
| 377 | DEFINE_REGUL(STPMIC2_BUCK5, "buck5"), |
| 378 | DEFINE_REGUL(STPMIC2_BUCK6, "buck6"), |
| 379 | DEFINE_REGUL(STPMIC2_BUCK7, "buck7"), |
| 380 | |
| 381 | DEFINE_REGUL(STPMIC2_LDO1, "ldo1"), |
| 382 | DEFINE_REGUL(STPMIC2_LDO2, "ldo2"), |
| 383 | DEFINE_REGUL(STPMIC2_LDO3, "ldo3"), |
| 384 | DEFINE_REGUL(STPMIC2_LDO4, "ldo4"), |
| 385 | DEFINE_REGUL(STPMIC2_LDO5, "ldo5"), |
| 386 | DEFINE_REGUL(STPMIC2_LDO6, "ldo6"), |
| 387 | DEFINE_REGUL(STPMIC2_LDO7, "ldo7"), |
| 388 | DEFINE_REGUL(STPMIC2_LDO8, "ldo8"), |
| 389 | |
| 390 | DEFINE_REGUL(STPMIC2_REFDDR, "refddr"), |
| 391 | }; |
| 392 | |
| 393 | static int register_pmic2(void) |
| 394 | { |
| 395 | void *fdt; |
| 396 | int pmic_node, regulators_node, subnode; |
| 397 | |
| 398 | VERBOSE("Register pmic2\n"); |
| 399 | |
| 400 | if (fdt_get_address(&fdt) == 0) { |
| 401 | return -FDT_ERR_NOTFOUND; |
| 402 | } |
| 403 | |
| 404 | pmic_node = dt_get_pmic_node(fdt); |
| 405 | if (pmic_node < 0) { |
| 406 | return pmic_node; |
| 407 | } |
| 408 | |
| 409 | regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators"); |
| 410 | if (regulators_node < 0) { |
| 411 | return -ENOENT; |
| 412 | } |
| 413 | |
| 414 | fdt_for_each_subnode(subnode, fdt, regulators_node) { |
| 415 | const char *reg_name = fdt_get_name(fdt, subnode, NULL); |
| 416 | const struct regul_description *desc; |
| 417 | unsigned int i; |
| 418 | int ret; |
| 419 | const fdt32_t *cuint; |
| 420 | |
| 421 | for (i = 0; i < STPMIC2_NB_REG; i++) { |
| 422 | desc = &pmic2_descs[i]; |
| 423 | if (strcmp(desc->node_name, reg_name) == 0) { |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | assert(i < STPMIC2_NB_REG); |
| 428 | |
| 429 | ret = regulator_register(desc, subnode); |
| 430 | if (ret != 0) { |
| 431 | WARN("%s:%d failed to register %s\n", __func__, |
| 432 | __LINE__, reg_name); |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | cuint = fdt_getprop(fdt, subnode, "st,regulator-bypass-microvolt", NULL); |
| 437 | if (cuint != NULL) { |
| 438 | struct regul_handle_s *regul = (struct regul_handle_s *)desc->driver_data; |
| 439 | |
| 440 | regul->bypass_mv = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U); |
| 441 | VERBOSE("%s: bypass voltage=%umV\n", desc->node_name, |
| 442 | regul->bypass_mv); |
| 443 | } |
| 444 | |
| 445 | if (fdt_getprop(fdt, subnode, "st,mask-reset", NULL) != NULL) { |
| 446 | VERBOSE("%s: set mask-reset\n", desc->node_name); |
| 447 | ret = pmic2_set_flag(desc, REGUL_MASK_RESET); |
| 448 | if (ret != 0) { |
| 449 | ERROR("set mask-reset failed\n"); |
| 450 | return ret; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | if (fdt_getprop(fdt, subnode, "st,regulator-sink-source", NULL) != NULL) { |
| 455 | VERBOSE("%s: set regulator-sink-source\n", desc->node_name); |
| 456 | ret = pmic2_set_flag(desc, REGUL_SINK_SOURCE); |
| 457 | if (ret != 0) { |
| 458 | ERROR("set regulator-sink-source failed\n"); |
| 459 | return ret; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | void initialize_pmic(void) |
| 468 | { |
| 469 | int ret; |
| 470 | uint8_t val; |
| 471 | |
| 472 | ret = initialize_pmic_i2c(); |
| 473 | if (!ret) { |
| 474 | VERBOSE("No PMIC2\n"); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | if (stpmic2_get_version(pmic2, &val) != 0) { |
| 479 | ERROR("Failed to access PMIC\n"); |
| 480 | panic(); |
| 481 | } |
| 482 | INFO("PMIC2 version = 0x%02x\n", val); |
| 483 | |
| 484 | if (stpmic2_get_product_id(pmic2, &val) != 0) { |
| 485 | ERROR("Failed to access PMIC\n"); |
| 486 | panic(); |
| 487 | } |
| 488 | INFO("PMIC2 product ID = 0x%02x\n", val); |
| 489 | |
| 490 | ret = register_pmic2(); |
| 491 | if (ret < 0) { |
| 492 | ERROR("Register pmic2 failed\n"); |
| 493 | panic(); |
| 494 | } |
| 495 | |
| 496 | #if EVENT_LOG_LEVEL == LOG_LEVEL_VERBOSE |
| 497 | stpmic2_dump_regulators(pmic2); |
| 498 | #endif |
| 499 | } |