regulator_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Copyright (c) 2021-2022, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <limits.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <common/debug.h>
  12. #include <drivers/delay_timer.h>
  13. #include <drivers/st/regulator.h>
  14. #include <libfdt.h>
  15. #define MAX_PROPERTY_LEN 64
  16. CASSERT(PLAT_NB_RDEVS >= 1U, plat_nb_rdevs_must_be_higher);
  17. static struct rdev rdev_array[PLAT_NB_RDEVS];
  18. #define for_each_rdev(rdev) \
  19. for ((rdev) = rdev_array; (rdev) <= &rdev_array[PLAT_NB_RDEVS - 1U]; (rdev)++)
  20. #define for_each_registered_rdev(rdev) \
  21. for ((rdev) = rdev_array; \
  22. ((rdev) <= &rdev_array[PLAT_NB_RDEVS - 1U]) && ((rdev)->desc != NULL); (rdev)++)
  23. static void lock_driver(const struct rdev *rdev)
  24. {
  25. if (rdev->desc->ops->lock != NULL) {
  26. rdev->desc->ops->lock(rdev->desc);
  27. }
  28. }
  29. static void unlock_driver(const struct rdev *rdev)
  30. {
  31. if (rdev->desc->ops->unlock != NULL) {
  32. rdev->desc->ops->unlock(rdev->desc);
  33. }
  34. }
  35. static struct rdev *regulator_get_by_phandle(int32_t phandle)
  36. {
  37. struct rdev *rdev;
  38. for_each_registered_rdev(rdev) {
  39. if (rdev->phandle == phandle) {
  40. return rdev;
  41. }
  42. }
  43. WARN("%s: phandle %d not found\n", __func__, phandle);
  44. return NULL;
  45. }
  46. /*
  47. * Get a regulator from its node name
  48. *
  49. * @fdt - pointer to device tree memory
  50. * @node_name - name of the node "ldo1"
  51. * Return pointer to rdev if succeed, NULL else.
  52. */
  53. struct rdev *regulator_get_by_name(const char *node_name)
  54. {
  55. struct rdev *rdev;
  56. assert(node_name != NULL);
  57. VERBOSE("get %s\n", node_name);
  58. for_each_registered_rdev(rdev) {
  59. if (strcmp(rdev->desc->node_name, node_name) == 0) {
  60. return rdev;
  61. }
  62. }
  63. WARN("%s: %s not found\n", __func__, node_name);
  64. return NULL;
  65. }
  66. static int32_t get_supply_phandle(const void *fdt, int node, const char *name)
  67. {
  68. const fdt32_t *cuint;
  69. int len __unused;
  70. int supply_phandle = -FDT_ERR_NOTFOUND;
  71. char prop_name[MAX_PROPERTY_LEN];
  72. len = snprintf(prop_name, MAX_PROPERTY_LEN - 1, "%s-supply", name);
  73. assert((len >= 0) && (len < (MAX_PROPERTY_LEN - 1)));
  74. cuint = fdt_getprop(fdt, node, prop_name, NULL);
  75. if (cuint != NULL) {
  76. supply_phandle = fdt32_to_cpu(*cuint);
  77. VERBOSE("%s: supplied by %d\n", name, supply_phandle);
  78. }
  79. return supply_phandle;
  80. }
  81. /*
  82. * Get a regulator from a supply name
  83. *
  84. * @fdt - pointer to device tree memory
  85. * @node - offset of the node that contains the supply description
  86. * @name - name of the supply "vdd" for "vdd-supply'
  87. * Return pointer to rdev if succeed, NULL else.
  88. */
  89. struct rdev *regulator_get_by_supply_name(const void *fdt, int node, const char *name)
  90. {
  91. const int p = get_supply_phandle(fdt, node, name);
  92. if (p < 0) {
  93. return NULL;
  94. }
  95. return regulator_get_by_phandle(p);
  96. }
  97. static int __regulator_set_state(struct rdev *rdev, bool state)
  98. {
  99. if (rdev->desc->ops->set_state == NULL) {
  100. return -ENODEV;
  101. }
  102. return rdev->desc->ops->set_state(rdev->desc, state);
  103. }
  104. /*
  105. * Enable regulator
  106. *
  107. * @rdev - pointer to rdev struct
  108. * Return 0 if succeed, non 0 else.
  109. */
  110. int regulator_enable(struct rdev *rdev)
  111. {
  112. int ret;
  113. assert(rdev != NULL);
  114. ret = __regulator_set_state(rdev, STATE_ENABLE);
  115. udelay(rdev->enable_ramp_delay);
  116. return ret;
  117. }
  118. /*
  119. * Disable regulator
  120. *
  121. * @rdev - pointer to rdev struct
  122. * Return 0 if succeed, non 0 else.
  123. */
  124. int regulator_disable(struct rdev *rdev)
  125. {
  126. int ret;
  127. assert(rdev != NULL);
  128. if ((rdev->flags & REGUL_ALWAYS_ON) != 0U) {
  129. return 0;
  130. }
  131. ret = __regulator_set_state(rdev, STATE_DISABLE);
  132. udelay(rdev->enable_ramp_delay);
  133. return ret;
  134. }
  135. /*
  136. * Regulator enabled query
  137. *
  138. * @rdev - pointer to rdev struct
  139. * Return 0 if disabled, 1 if enabled, <0 else.
  140. */
  141. int regulator_is_enabled(const struct rdev *rdev)
  142. {
  143. int ret;
  144. assert(rdev != NULL);
  145. VERBOSE("%s: is en\n", rdev->desc->node_name);
  146. if (rdev->desc->ops->get_state == NULL) {
  147. return -ENODEV;
  148. }
  149. lock_driver(rdev);
  150. ret = rdev->desc->ops->get_state(rdev->desc);
  151. if (ret < 0) {
  152. ERROR("regul %s get state failed: err:%d\n",
  153. rdev->desc->node_name, ret);
  154. }
  155. unlock_driver(rdev);
  156. return ret;
  157. }
  158. /*
  159. * Set regulator voltage
  160. *
  161. * @rdev - pointer to rdev struct
  162. * @mvolt - Target voltage level in millivolt
  163. * Return 0 if succeed, non 0 else.
  164. */
  165. int regulator_set_voltage(struct rdev *rdev, uint16_t mvolt)
  166. {
  167. int ret;
  168. assert(rdev != NULL);
  169. VERBOSE("%s: set mvolt\n", rdev->desc->node_name);
  170. if ((mvolt < rdev->min_mv) || (mvolt > rdev->max_mv)) {
  171. return -EPERM;
  172. }
  173. if (regulator_get_voltage(rdev) == mvolt) {
  174. return 0U;
  175. }
  176. if (rdev->desc->ops->set_voltage == NULL) {
  177. return -ENODEV;
  178. }
  179. lock_driver(rdev);
  180. ret = rdev->desc->ops->set_voltage(rdev->desc, mvolt);
  181. if (ret < 0) {
  182. ERROR("regul %s set volt failed: err:%d\n",
  183. rdev->desc->node_name, ret);
  184. }
  185. unlock_driver(rdev);
  186. return ret;
  187. }
  188. /*
  189. * Set regulator min voltage
  190. *
  191. * @rdev - pointer to rdev struct
  192. * Return 0 if succeed, non 0 else.
  193. */
  194. int regulator_set_min_voltage(struct rdev *rdev)
  195. {
  196. return regulator_set_voltage(rdev, rdev->min_mv);
  197. }
  198. /*
  199. * Get regulator voltage
  200. *
  201. * @rdev - pointer to rdev struct
  202. * Return milli volts if succeed, <0 else.
  203. */
  204. int regulator_get_voltage(const struct rdev *rdev)
  205. {
  206. int ret;
  207. assert(rdev != NULL);
  208. VERBOSE("%s: get volt\n", rdev->desc->node_name);
  209. if (rdev->desc->ops->get_voltage == NULL) {
  210. return rdev->min_mv;
  211. }
  212. lock_driver(rdev);
  213. ret = rdev->desc->ops->get_voltage(rdev->desc);
  214. if (ret < 0) {
  215. ERROR("regul %s get voltage failed: err:%d\n",
  216. rdev->desc->node_name, ret);
  217. }
  218. unlock_driver(rdev);
  219. return ret;
  220. }
  221. /*
  222. * List regulator voltages
  223. *
  224. * @rdev - pointer to rdev struct
  225. * @levels - out: array of supported millitvolt levels from min to max value
  226. * @count - out: number of possible millivolt values
  227. * Return 0 if succeed, non 0 else.
  228. */
  229. int regulator_list_voltages(const struct rdev *rdev, const uint16_t **levels, size_t *count)
  230. {
  231. int ret;
  232. size_t n;
  233. assert(rdev != NULL);
  234. assert(levels != NULL);
  235. assert(count != NULL);
  236. VERBOSE("%s: list volt\n", rdev->desc->node_name);
  237. if (rdev->desc->ops->list_voltages == NULL) {
  238. return -ENODEV;
  239. }
  240. lock_driver(rdev);
  241. ret = rdev->desc->ops->list_voltages(rdev->desc, levels, count);
  242. unlock_driver(rdev);
  243. if (ret < 0) {
  244. ERROR("regul %s list_voltages failed: err: %d\n",
  245. rdev->desc->node_name, ret);
  246. return ret;
  247. }
  248. /*
  249. * Reduce the possible values depending on min and max from device-tree
  250. */
  251. n = *count;
  252. while ((n > 1U) && ((*levels)[n - 1U] > rdev->max_mv)) {
  253. n--;
  254. }
  255. /* Verify that max val is a valid value */
  256. if (rdev->max_mv != (*levels)[n - 1]) {
  257. ERROR("regul %s: max value %u is invalid\n",
  258. rdev->desc->node_name, rdev->max_mv);
  259. return -EINVAL;
  260. }
  261. while ((n > 1U) && ((*levels[0U]) < rdev->min_mv)) {
  262. (*levels)++;
  263. n--;
  264. }
  265. /* Verify that min is not too high */
  266. if (n == 0U) {
  267. ERROR("regul %s set min voltage is too high\n",
  268. rdev->desc->node_name);
  269. return -EINVAL;
  270. }
  271. /* Verify that min val is a valid vlue */
  272. if (rdev->min_mv != (*levels)[0U]) {
  273. ERROR("regul %s: min value %u is invalid\n",
  274. rdev->desc->node_name, rdev->min_mv);
  275. return -EINVAL;
  276. }
  277. *count = n;
  278. VERBOSE("rdev->min_mv=%u rdev->max_mv=%u\n", rdev->min_mv, rdev->max_mv);
  279. return 0;
  280. }
  281. /*
  282. * Get regulator voltages range
  283. *
  284. * @rdev - pointer to rdev struct
  285. * @min_mv - out: min possible millivolt value
  286. * @max_mv - out: max possible millivolt value
  287. * Return 0 if succeed, non 0 else.
  288. */
  289. void regulator_get_range(const struct rdev *rdev, uint16_t *min_mv, uint16_t *max_mv)
  290. {
  291. assert(rdev != NULL);
  292. if (min_mv != NULL) {
  293. *min_mv = rdev->min_mv;
  294. }
  295. if (max_mv != NULL) {
  296. *max_mv = rdev->max_mv;
  297. }
  298. }
  299. /*
  300. * Set regulator flag
  301. *
  302. * @rdev - pointer to rdev struct
  303. * @flag - flag value to set (eg: REGUL_OCP)
  304. * Return 0 if succeed, non 0 else.
  305. */
  306. int regulator_set_flag(struct rdev *rdev, uint16_t flag)
  307. {
  308. int ret;
  309. /* check that only one bit is set on flag */
  310. if (__builtin_popcount(flag) != 1) {
  311. return -EINVAL;
  312. }
  313. /* REGUL_ALWAYS_ON and REGUL_BOOT_ON are internal properties of the core */
  314. if ((flag == REGUL_ALWAYS_ON) || (flag == REGUL_BOOT_ON)) {
  315. rdev->flags |= flag;
  316. return 0;
  317. }
  318. if (rdev->desc->ops->set_flag == NULL) {
  319. ERROR("%s can not set any flag\n", rdev->desc->node_name);
  320. return -ENODEV;
  321. }
  322. lock_driver(rdev);
  323. ret = rdev->desc->ops->set_flag(rdev->desc, flag);
  324. unlock_driver(rdev);
  325. if (ret != 0) {
  326. ERROR("%s: could not set flag %d ret=%d\n",
  327. rdev->desc->node_name, flag, ret);
  328. return ret;
  329. }
  330. rdev->flags |= flag;
  331. return 0;
  332. }
  333. static int parse_properties(const void *fdt, struct rdev *rdev, int node)
  334. {
  335. const fdt32_t *cuint;
  336. int ret;
  337. if (fdt_getprop(fdt, node, "regulator-always-on", NULL) != NULL) {
  338. VERBOSE("%s: set regulator-always-on\n", rdev->desc->node_name);
  339. ret = regulator_set_flag(rdev, REGUL_ALWAYS_ON);
  340. if (ret != 0) {
  341. return ret;
  342. }
  343. }
  344. cuint = fdt_getprop(fdt, node, "regulator-enable-ramp-delay", NULL);
  345. if (cuint != NULL) {
  346. rdev->enable_ramp_delay = fdt32_to_cpu(*cuint);
  347. VERBOSE("%s: enable_ramp_delay=%u\n", rdev->desc->node_name,
  348. rdev->enable_ramp_delay);
  349. }
  350. return 0;
  351. }
  352. /*
  353. * Parse the device-tree for a regulator
  354. *
  355. * Read min/max voltage from dt and check its validity
  356. * Read the properties, and call the driver to set flags
  357. * Read power supply phandle
  358. * Read and store low power mode states
  359. *
  360. * @rdev - pointer to rdev struct
  361. * @node - device-tree node offset of the regulator
  362. * Return 0 if disabled, 1 if enabled, <0 else.
  363. */
  364. static int parse_dt(struct rdev *rdev, int node)
  365. {
  366. void *fdt;
  367. const fdt32_t *cuint;
  368. const uint16_t *levels;
  369. size_t size;
  370. int ret;
  371. VERBOSE("%s: parse dt\n", rdev->desc->node_name);
  372. if (fdt_get_address(&fdt) == 0) {
  373. return -ENOENT;
  374. }
  375. rdev->phandle = fdt_get_phandle(fdt, node);
  376. cuint = fdt_getprop(fdt, node, "regulator-min-microvolt", NULL);
  377. if (cuint != NULL) {
  378. uint16_t min_mv;
  379. min_mv = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U);
  380. VERBOSE("%s: min_mv=%d\n", rdev->desc->node_name, (int)min_mv);
  381. if (min_mv <= rdev->max_mv) {
  382. rdev->min_mv = min_mv;
  383. } else {
  384. ERROR("%s: min_mv=%d is too high\n",
  385. rdev->desc->node_name, (int)min_mv);
  386. return -EINVAL;
  387. }
  388. }
  389. cuint = fdt_getprop(fdt, node, "regulator-max-microvolt", NULL);
  390. if (cuint != NULL) {
  391. uint16_t max_mv;
  392. max_mv = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U);
  393. VERBOSE("%s: max_mv=%d\n", rdev->desc->node_name, (int)max_mv);
  394. if (max_mv >= rdev->min_mv) {
  395. rdev->max_mv = max_mv;
  396. } else {
  397. ERROR("%s: max_mv=%d is too low\n",
  398. rdev->desc->node_name, (int)max_mv);
  399. return -EINVAL;
  400. }
  401. }
  402. /* validate that min and max values can be used */
  403. ret = regulator_list_voltages(rdev, &levels, &size);
  404. if ((ret != 0) && (ret != -ENODEV)) {
  405. return ret;
  406. }
  407. ret = parse_properties(fdt, rdev, node);
  408. if (ret != 0) {
  409. return ret;
  410. }
  411. return 0;
  412. }
  413. /*
  414. * Register a regulator driver in regulator framework.
  415. * Initialize voltage range from driver description
  416. *
  417. * @desc - pointer to the regulator description
  418. * @node - device-tree node offset of the regulator
  419. * Return 0 if succeed, non 0 else.
  420. */
  421. int regulator_register(const struct regul_description *desc, int node)
  422. {
  423. struct rdev *rdev;
  424. assert(desc != NULL);
  425. VERBOSE("register %s\n", desc->node_name);
  426. for_each_rdev(rdev) {
  427. if (rdev->desc == NULL) {
  428. break;
  429. }
  430. }
  431. if (rdev > &rdev_array[PLAT_NB_RDEVS - 1U]) {
  432. WARN("Not enough place for regulators, PLAT_NB_RDEVS should be increased.\n");
  433. return -ENOMEM;
  434. }
  435. rdev->desc = desc;
  436. rdev->enable_ramp_delay = rdev->desc->enable_ramp_delay;
  437. if (rdev->desc->ops->list_voltages != NULL) {
  438. int ret;
  439. const uint16_t *levels;
  440. size_t count;
  441. lock_driver(rdev);
  442. ret = rdev->desc->ops->list_voltages(rdev->desc, &levels, &count);
  443. unlock_driver(rdev);
  444. if (ret < 0) {
  445. ERROR("regul %s set state failed: err:%d\n",
  446. rdev->desc->node_name, ret);
  447. return ret;
  448. }
  449. rdev->min_mv = levels[0];
  450. rdev->max_mv = levels[count - 1U];
  451. } else {
  452. rdev->max_mv = UINT16_MAX;
  453. }
  454. return parse_dt(rdev, node);
  455. }