stm32mp_pmic2.c 11 KB

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