stm32mp_pmic.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (c) 2017-2024, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <common/debug.h>
  9. #include <drivers/delay_timer.h>
  10. #include <drivers/st/regulator.h>
  11. #include <drivers/st/stm32_i2c.h>
  12. #include <drivers/st/stm32mp_pmic.h>
  13. #include <drivers/st/stpmic1.h>
  14. #include <lib/mmio.h>
  15. #include <lib/utils_def.h>
  16. #include <libfdt.h>
  17. #include <platform_def.h>
  18. #define PMIC_NODE_NOT_FOUND 1
  19. #define NB_REG 14U
  20. static struct i2c_handle_s i2c_handle;
  21. static uint32_t pmic_i2c_addr;
  22. static int register_pmic(void);
  23. static int dt_get_pmic_node(void *fdt)
  24. {
  25. static int node = -FDT_ERR_BADOFFSET;
  26. if (node == -FDT_ERR_BADOFFSET) {
  27. node = fdt_node_offset_by_compatible(fdt, -1, "st,stpmic1");
  28. }
  29. return node;
  30. }
  31. int dt_pmic_status(void)
  32. {
  33. static int status = -FDT_ERR_BADVALUE;
  34. int node;
  35. void *fdt;
  36. if (status != -FDT_ERR_BADVALUE) {
  37. return status;
  38. }
  39. if (fdt_get_address(&fdt) == 0) {
  40. return -ENOENT;
  41. }
  42. node = dt_get_pmic_node(fdt);
  43. if (node <= 0) {
  44. status = -FDT_ERR_NOTFOUND;
  45. return status;
  46. }
  47. status = (int)fdt_get_status(node);
  48. return status;
  49. }
  50. static bool dt_pmic_is_secure(void)
  51. {
  52. int status = dt_pmic_status();
  53. return (status >= 0) &&
  54. (status == DT_SECURE) &&
  55. (i2c_handle.dt_status == DT_SECURE);
  56. }
  57. /*
  58. * Get PMIC and its I2C bus configuration from the device tree.
  59. * Return 0 on success, negative on error, 1 if no PMIC node is defined.
  60. */
  61. static int dt_pmic_i2c_config(struct dt_node_info *i2c_info,
  62. struct stm32_i2c_init_s *init)
  63. {
  64. static int i2c_node = -FDT_ERR_NOTFOUND;
  65. void *fdt;
  66. if (fdt_get_address(&fdt) == 0) {
  67. return -FDT_ERR_NOTFOUND;
  68. }
  69. if (i2c_node == -FDT_ERR_NOTFOUND) {
  70. int pmic_node;
  71. const fdt32_t *cuint;
  72. pmic_node = dt_get_pmic_node(fdt);
  73. if (pmic_node < 0) {
  74. return PMIC_NODE_NOT_FOUND;
  75. }
  76. cuint = fdt_getprop(fdt, pmic_node, "reg", NULL);
  77. if (cuint == NULL) {
  78. return -FDT_ERR_NOTFOUND;
  79. }
  80. pmic_i2c_addr = fdt32_to_cpu(*cuint) << 1;
  81. if (pmic_i2c_addr > UINT16_MAX) {
  82. return -FDT_ERR_BADVALUE;
  83. }
  84. i2c_node = fdt_parent_offset(fdt, pmic_node);
  85. if (i2c_node < 0) {
  86. return -FDT_ERR_NOTFOUND;
  87. }
  88. }
  89. dt_fill_device_info(i2c_info, i2c_node);
  90. if (i2c_info->base == 0U) {
  91. return -FDT_ERR_NOTFOUND;
  92. }
  93. return stm32_i2c_get_setup_from_fdt(fdt, i2c_node, init);
  94. }
  95. bool initialize_pmic_i2c(void)
  96. {
  97. int ret;
  98. struct dt_node_info i2c_info;
  99. struct i2c_handle_s *i2c = &i2c_handle;
  100. struct stm32_i2c_init_s i2c_init;
  101. ret = dt_pmic_i2c_config(&i2c_info, &i2c_init);
  102. if (ret < 0) {
  103. ERROR("I2C configuration failed %d\n", ret);
  104. panic();
  105. }
  106. if (ret != 0) {
  107. return false;
  108. }
  109. /* Initialize PMIC I2C */
  110. i2c->i2c_base_addr = i2c_info.base;
  111. i2c->dt_status = i2c_info.status;
  112. i2c->clock = i2c_info.clock;
  113. i2c->i2c_state = I2C_STATE_RESET;
  114. i2c_init.own_address1 = pmic_i2c_addr;
  115. i2c_init.addressing_mode = I2C_ADDRESSINGMODE_7BIT;
  116. i2c_init.dual_address_mode = I2C_DUALADDRESS_DISABLE;
  117. i2c_init.own_address2 = 0;
  118. i2c_init.own_address2_masks = I2C_OAR2_OA2NOMASK;
  119. i2c_init.general_call_mode = I2C_GENERALCALL_DISABLE;
  120. i2c_init.no_stretch_mode = I2C_NOSTRETCH_DISABLE;
  121. i2c_init.analog_filter = 1;
  122. i2c_init.digital_filter_coef = 0;
  123. ret = stm32_i2c_init(i2c, &i2c_init);
  124. if (ret != 0) {
  125. ERROR("Cannot initialize I2C %x (%d)\n",
  126. i2c->i2c_base_addr, ret);
  127. panic();
  128. }
  129. if (!stm32_i2c_is_device_ready(i2c, pmic_i2c_addr, 1,
  130. I2C_TIMEOUT_BUSY_MS)) {
  131. ERROR("I2C device not ready\n");
  132. panic();
  133. }
  134. stpmic1_bind_i2c(i2c, (uint16_t)pmic_i2c_addr);
  135. return true;
  136. }
  137. static void register_pmic_shared_peripherals(void)
  138. {
  139. uintptr_t i2c_base = i2c_handle.i2c_base_addr;
  140. if (dt_pmic_is_secure()) {
  141. stm32mp_register_secure_periph_iomem(i2c_base);
  142. } else {
  143. if (i2c_base != 0U) {
  144. stm32mp_register_non_secure_periph_iomem(i2c_base);
  145. }
  146. }
  147. }
  148. void initialize_pmic(void)
  149. {
  150. if (!initialize_pmic_i2c()) {
  151. VERBOSE("No PMIC\n");
  152. return;
  153. }
  154. register_pmic_shared_peripherals();
  155. if (register_pmic() < 0) {
  156. panic();
  157. }
  158. if (stpmic1_powerctrl_on() < 0) {
  159. panic();
  160. }
  161. }
  162. #if DEBUG
  163. void print_pmic_info_and_debug(void)
  164. {
  165. unsigned long pmic_version;
  166. if (stpmic1_get_version(&pmic_version) != 0) {
  167. ERROR("Failed to access PMIC\n");
  168. panic();
  169. }
  170. INFO("PMIC version = 0x%02lx\n", pmic_version);
  171. }
  172. #endif
  173. int pmic_voltages_init(void)
  174. {
  175. #if STM32MP13
  176. struct rdev *buck1, *buck4;
  177. int status;
  178. buck1 = regulator_get_by_name("buck1");
  179. if (buck1 == NULL) {
  180. return -ENOENT;
  181. }
  182. buck4 = regulator_get_by_name("buck4");
  183. if (buck4 == NULL) {
  184. return -ENOENT;
  185. }
  186. status = regulator_set_min_voltage(buck1);
  187. if (status != 0) {
  188. return status;
  189. }
  190. status = regulator_set_min_voltage(buck4);
  191. if (status != 0) {
  192. return status;
  193. }
  194. #endif
  195. return 0;
  196. }
  197. enum {
  198. STPMIC1_BUCK1 = 0,
  199. STPMIC1_BUCK2,
  200. STPMIC1_BUCK3,
  201. STPMIC1_BUCK4,
  202. STPMIC1_LDO1,
  203. STPMIC1_LDO2,
  204. STPMIC1_LDO3,
  205. STPMIC1_LDO4,
  206. STPMIC1_LDO5,
  207. STPMIC1_LDO6,
  208. STPMIC1_VREF_DDR,
  209. STPMIC1_BOOST,
  210. STPMIC1_VBUS_OTG,
  211. STPMIC1_SW_OUT,
  212. };
  213. static int pmic_set_state(const struct regul_description *desc, bool enable)
  214. {
  215. VERBOSE("%s: set state to %d\n", desc->node_name, enable);
  216. if (enable == STATE_ENABLE) {
  217. return stpmic1_regulator_enable(desc->node_name);
  218. } else {
  219. return stpmic1_regulator_disable(desc->node_name);
  220. }
  221. }
  222. static int pmic_get_state(const struct regul_description *desc)
  223. {
  224. VERBOSE("%s: get state\n", desc->node_name);
  225. return stpmic1_is_regulator_enabled(desc->node_name);
  226. }
  227. static int pmic_get_voltage(const struct regul_description *desc)
  228. {
  229. VERBOSE("%s: get volt\n", desc->node_name);
  230. return stpmic1_regulator_voltage_get(desc->node_name);
  231. }
  232. static int pmic_set_voltage(const struct regul_description *desc, uint16_t mv)
  233. {
  234. VERBOSE("%s: get volt\n", desc->node_name);
  235. return stpmic1_regulator_voltage_set(desc->node_name, mv);
  236. }
  237. static int pmic_list_voltages(const struct regul_description *desc,
  238. const uint16_t **levels, size_t *count)
  239. {
  240. VERBOSE("%s: list volt\n", desc->node_name);
  241. return stpmic1_regulator_levels_mv(desc->node_name, levels, count);
  242. }
  243. static int pmic_set_flag(const struct regul_description *desc, uint16_t flag)
  244. {
  245. VERBOSE("%s: set_flag 0x%x\n", desc->node_name, flag);
  246. switch (flag) {
  247. case REGUL_OCP:
  248. return stpmic1_regulator_icc_set(desc->node_name);
  249. case REGUL_ACTIVE_DISCHARGE:
  250. return stpmic1_active_discharge_mode_set(desc->node_name);
  251. case REGUL_PULL_DOWN:
  252. return stpmic1_regulator_pull_down_set(desc->node_name);
  253. case REGUL_MASK_RESET:
  254. return stpmic1_regulator_mask_reset_set(desc->node_name);
  255. case REGUL_SINK_SOURCE:
  256. return stpmic1_regulator_sink_mode_set(desc->node_name);
  257. case REGUL_ENABLE_BYPASS:
  258. return stpmic1_regulator_bypass_mode_set(desc->node_name);
  259. default:
  260. return -EINVAL;
  261. }
  262. }
  263. static const struct regul_ops pmic_ops = {
  264. .set_state = pmic_set_state,
  265. .get_state = pmic_get_state,
  266. .set_voltage = pmic_set_voltage,
  267. .get_voltage = pmic_get_voltage,
  268. .list_voltages = pmic_list_voltages,
  269. .set_flag = pmic_set_flag,
  270. };
  271. #define DEFINE_REGU(name) { \
  272. .node_name = (name), \
  273. .ops = &pmic_ops, \
  274. .driver_data = NULL, \
  275. .enable_ramp_delay = 1000, \
  276. }
  277. static const struct regul_description pmic_regs[NB_REG] = {
  278. [STPMIC1_BUCK1] = DEFINE_REGU("buck1"),
  279. [STPMIC1_BUCK2] = DEFINE_REGU("buck2"),
  280. [STPMIC1_BUCK3] = DEFINE_REGU("buck3"),
  281. [STPMIC1_BUCK4] = DEFINE_REGU("buck4"),
  282. [STPMIC1_LDO1] = DEFINE_REGU("ldo1"),
  283. [STPMIC1_LDO2] = DEFINE_REGU("ldo2"),
  284. [STPMIC1_LDO3] = DEFINE_REGU("ldo3"),
  285. [STPMIC1_LDO4] = DEFINE_REGU("ldo4"),
  286. [STPMIC1_LDO5] = DEFINE_REGU("ldo5"),
  287. [STPMIC1_LDO6] = DEFINE_REGU("ldo6"),
  288. [STPMIC1_VREF_DDR] = DEFINE_REGU("vref_ddr"),
  289. [STPMIC1_BOOST] = DEFINE_REGU("boost"),
  290. [STPMIC1_VBUS_OTG] = DEFINE_REGU("pwr_sw1"),
  291. [STPMIC1_SW_OUT] = DEFINE_REGU("pwr_sw2"),
  292. };
  293. static int register_pmic(void)
  294. {
  295. void *fdt;
  296. int pmic_node, regulators_node, subnode;
  297. VERBOSE("Register pmic\n");
  298. if (fdt_get_address(&fdt) == 0) {
  299. return -FDT_ERR_NOTFOUND;
  300. }
  301. pmic_node = dt_get_pmic_node(fdt);
  302. if (pmic_node < 0) {
  303. return pmic_node;
  304. }
  305. regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators");
  306. if (regulators_node < 0) {
  307. return -ENOENT;
  308. }
  309. fdt_for_each_subnode(subnode, fdt, regulators_node) {
  310. const char *reg_name = fdt_get_name(fdt, subnode, NULL);
  311. const struct regul_description *desc;
  312. unsigned int i;
  313. int ret;
  314. for (i = 0U; i < NB_REG; i++) {
  315. desc = &pmic_regs[i];
  316. if (strcmp(desc->node_name, reg_name) == 0) {
  317. break;
  318. }
  319. }
  320. assert(i < NB_REG);
  321. ret = regulator_register(desc, subnode);
  322. if (ret != 0) {
  323. WARN("%s:%d failed to register %s\n", __func__,
  324. __LINE__, reg_name);
  325. return ret;
  326. }
  327. }
  328. return 0;
  329. }