regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-09-23 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtservice.h>
  12. #define DBG_TAG "rtdm.regulator"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. #include <drivers/ofw.h>
  16. #include <drivers/platform.h>
  17. #include <drivers/regulator.h>
  18. struct rt_regulator
  19. {
  20. struct rt_regulator_node *reg_np;
  21. };
  22. static RT_DEFINE_SPINLOCK(_regulator_lock);
  23. static rt_err_t regulator_enable(struct rt_regulator_node *reg_np);
  24. static rt_err_t regulator_disable(struct rt_regulator_node *reg_np);
  25. rt_err_t rt_regulator_register(struct rt_regulator_node *reg_np)
  26. {
  27. const struct rt_regulator_param *param;
  28. if (!reg_np || !reg_np->dev || !reg_np->param || !reg_np->ops)
  29. {
  30. return -RT_EINVAL;
  31. }
  32. rt_list_init(&reg_np->list);
  33. rt_list_init(&reg_np->children_nodes);
  34. rt_list_init(&reg_np->notifier_nodes);
  35. rt_ref_init(&reg_np->ref);
  36. rt_atomic_store(&reg_np->enabled_count, 0);
  37. param = reg_np->param;
  38. reg_np->parent = RT_NULL;
  39. #ifdef RT_USING_OFW
  40. if (reg_np->dev->ofw_node)
  41. {
  42. rt_ofw_data(reg_np->dev->ofw_node) = reg_np;
  43. }
  44. #endif /* RT_USING_OFW */
  45. if (param->boot_on || param->always_on)
  46. {
  47. regulator_enable(reg_np);
  48. }
  49. return RT_EOK;
  50. }
  51. rt_err_t rt_regulator_unregister(struct rt_regulator_node *reg_np)
  52. {
  53. rt_err_t err = RT_EOK;
  54. if (!reg_np)
  55. {
  56. return -RT_EINVAL;
  57. }
  58. rt_hw_spin_lock(&_regulator_lock.lock);
  59. if (rt_atomic_load(&reg_np->enabled_count) != 0)
  60. {
  61. err = -RT_EBUSY;
  62. LOG_E("%s was enabled by consumer", reg_np->supply_name);
  63. goto _unlock;
  64. }
  65. if (!(reg_np->param->boot_on || reg_np->param->always_on))
  66. {
  67. regulator_disable(reg_np);
  68. }
  69. if (!rt_list_isempty(&reg_np->children_nodes) || rt_ref_read(&reg_np->ref) > 1)
  70. {
  71. err = -RT_EBUSY;
  72. goto _unlock;
  73. }
  74. reg_np->parent = RT_NULL;
  75. rt_list_remove(&reg_np->list);
  76. _unlock:
  77. rt_hw_spin_unlock(&_regulator_lock.lock);
  78. return err;
  79. }
  80. rt_err_t rt_regulator_notifier_register(struct rt_regulator *reg,
  81. struct rt_regulator_notifier *notifier)
  82. {
  83. struct rt_regulator_node *reg_np;
  84. if (!reg || !notifier)
  85. {
  86. return -RT_EINVAL;
  87. }
  88. rt_hw_spin_lock(&_regulator_lock.lock);
  89. reg_np = reg->reg_np;
  90. notifier->regulator = reg;
  91. rt_list_init(&notifier->list);
  92. rt_list_insert_after(&reg_np->notifier_nodes, &notifier->list);
  93. rt_hw_spin_unlock(&_regulator_lock.lock);
  94. return RT_EOK;
  95. }
  96. rt_err_t rt_regulator_notifier_unregister(struct rt_regulator *reg,
  97. struct rt_regulator_notifier *notifier)
  98. {
  99. if (!reg || !notifier)
  100. {
  101. return -RT_EINVAL;
  102. }
  103. rt_hw_spin_lock(&_regulator_lock.lock);
  104. rt_list_remove(&notifier->list);
  105. rt_hw_spin_unlock(&_regulator_lock.lock);
  106. return RT_EOK;
  107. }
  108. static rt_err_t regulator_notifier_call_chain(struct rt_regulator_node *reg_np,
  109. rt_ubase_t msg, void *data)
  110. {
  111. rt_err_t err = RT_EOK;
  112. struct rt_regulator_notifier *notifier;
  113. rt_list_t *head = &reg_np->notifier_nodes;
  114. if (rt_list_isempty(head))
  115. {
  116. return err;
  117. }
  118. rt_list_for_each_entry(notifier, head, list)
  119. {
  120. err = notifier->callback(notifier, msg, data);
  121. if (err == -RT_EIO)
  122. {
  123. break;
  124. }
  125. }
  126. return err;
  127. }
  128. static rt_uint32_t regulator_get_enable_time(struct rt_regulator_node *reg_np)
  129. {
  130. if (reg_np->param->enable_delay)
  131. {
  132. return reg_np->param->enable_delay;
  133. }
  134. if (reg_np->ops->enable_time)
  135. {
  136. return reg_np->ops->enable_time(reg_np);
  137. }
  138. return 0;
  139. }
  140. static void regulator_delay(rt_uint32_t delay)
  141. {
  142. rt_uint32_t ms = delay / 1000;
  143. rt_uint32_t us = delay % 1000;
  144. if (ms > 0)
  145. {
  146. /*
  147. * For small enough values, handle super-millisecond
  148. * delays in the usleep_range() call below.
  149. */
  150. if (ms < 20)
  151. {
  152. us += ms * 1000;
  153. }
  154. else if (rt_thread_self())
  155. {
  156. rt_thread_mdelay(ms);
  157. }
  158. else
  159. {
  160. rt_hw_us_delay(ms * 1000);
  161. }
  162. }
  163. /*
  164. * Give the scheduler some room to coalesce with any other
  165. * wakeup sources. For delays shorter than 10 us, don't even
  166. * bother setting up high-resolution timers and just busy-loop.
  167. */
  168. if (us >= 10)
  169. {
  170. rt_hw_us_delay((us + 100) >> 1);
  171. }
  172. else
  173. {
  174. rt_hw_us_delay(us);
  175. }
  176. }
  177. static rt_err_t regulator_enable(struct rt_regulator_node *reg_np)
  178. {
  179. rt_err_t err = RT_EOK;
  180. rt_uint32_t enable_delay = regulator_get_enable_time(reg_np);
  181. if (reg_np->ops->enable)
  182. {
  183. err = reg_np->ops->enable(reg_np);
  184. if (!err)
  185. {
  186. if (enable_delay)
  187. {
  188. regulator_delay(enable_delay);
  189. }
  190. rt_atomic_add(&reg_np->enabled_count, 1);
  191. err = regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_ENABLE, RT_NULL);
  192. }
  193. }
  194. if (!err && reg_np->parent)
  195. {
  196. err = regulator_enable(reg_np->parent);
  197. }
  198. return err;
  199. }
  200. rt_err_t rt_regulator_enable(struct rt_regulator *reg)
  201. {
  202. rt_err_t err;
  203. if (!reg)
  204. {
  205. return -RT_EINVAL;
  206. }
  207. if (rt_regulator_is_enabled(reg))
  208. {
  209. return RT_EOK;
  210. }
  211. rt_hw_spin_lock(&_regulator_lock.lock);
  212. err = regulator_enable(reg->reg_np);
  213. rt_hw_spin_unlock(&_regulator_lock.lock);
  214. return err;
  215. }
  216. static rt_err_t regulator_disable(struct rt_regulator_node *reg_np)
  217. {
  218. rt_err_t err = RT_EOK;
  219. if (reg_np->ops->disable)
  220. {
  221. err = reg_np->ops->disable(reg_np);
  222. if (!err)
  223. {
  224. if (reg_np->param->off_on_delay)
  225. {
  226. regulator_delay(reg_np->param->off_on_delay);
  227. }
  228. err = regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_DISABLE, RT_NULL);
  229. }
  230. }
  231. if (!err && reg_np->parent)
  232. {
  233. err = regulator_disable(reg_np->parent);
  234. }
  235. return err;
  236. }
  237. rt_err_t rt_regulator_disable(struct rt_regulator *reg)
  238. {
  239. rt_err_t err;
  240. if (!reg)
  241. {
  242. return -RT_EINVAL;
  243. }
  244. if (!rt_regulator_is_enabled(reg))
  245. {
  246. return RT_EOK;
  247. }
  248. if (rt_atomic_load(&reg->reg_np->enabled_count) != 0)
  249. {
  250. rt_atomic_sub(&reg->reg_np->enabled_count, 1);
  251. return RT_EOK;
  252. }
  253. rt_hw_spin_lock(&_regulator_lock.lock);
  254. err = regulator_disable(reg->reg_np);
  255. rt_hw_spin_unlock(&_regulator_lock.lock);
  256. return err;
  257. }
  258. rt_bool_t rt_regulator_is_enabled(struct rt_regulator *reg)
  259. {
  260. if (!reg)
  261. {
  262. return -RT_EINVAL;
  263. }
  264. if (reg->reg_np->ops->is_enabled)
  265. {
  266. return reg->reg_np->ops->is_enabled(reg->reg_np);
  267. }
  268. return rt_atomic_load(&reg->reg_np->enabled_count) > 0;
  269. }
  270. static rt_err_t regulator_set_voltage(struct rt_regulator_node *reg_np, int min_uvolt, int max_uvolt)
  271. {
  272. rt_err_t err = RT_EOK;
  273. if (reg_np->ops->set_voltage)
  274. {
  275. union rt_regulator_notifier_args args;
  276. RT_ASSERT(reg_np->ops->get_voltage != RT_NULL);
  277. args.old_uvolt = reg_np->ops->get_voltage(reg_np);
  278. args.min_uvolt = min_uvolt;
  279. args.max_uvolt = max_uvolt;
  280. err = regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_VOLTAGE_CHANGE, &args);
  281. if (!err)
  282. {
  283. err = reg_np->ops->set_voltage(reg_np, min_uvolt, max_uvolt);
  284. }
  285. if (err)
  286. {
  287. regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_VOLTAGE_CHANGE_ERR,
  288. (void *)(rt_base_t)args.old_uvolt);
  289. }
  290. }
  291. if (!err && reg_np->parent)
  292. {
  293. err = regulator_set_voltage(reg_np->parent, min_uvolt, max_uvolt);
  294. }
  295. return err;
  296. }
  297. rt_bool_t rt_regulator_is_supported_voltage(struct rt_regulator *reg, int min_uvolt, int max_uvolt)
  298. {
  299. const struct rt_regulator_param *param;
  300. RT_ASSERT(reg != RT_NULL);
  301. param = reg->reg_np->param;
  302. if (!param)
  303. {
  304. return RT_FALSE;
  305. }
  306. return param->min_uvolt <= min_uvolt && param->max_uvolt >= max_uvolt;
  307. }
  308. rt_err_t rt_regulator_set_voltage(struct rt_regulator *reg, int min_uvolt, int max_uvolt)
  309. {
  310. rt_err_t err;
  311. if (!reg)
  312. {
  313. return -RT_EINVAL;
  314. }
  315. rt_hw_spin_lock(&_regulator_lock.lock);
  316. err = regulator_set_voltage(reg->reg_np, min_uvolt, max_uvolt);
  317. rt_hw_spin_unlock(&_regulator_lock.lock);
  318. return err;
  319. }
  320. int rt_regulator_get_voltage(struct rt_regulator *reg)
  321. {
  322. int uvolt = RT_REGULATOR_UVOLT_INVALID;
  323. struct rt_regulator_node *reg_np;
  324. if (!reg)
  325. {
  326. return -RT_EINVAL;
  327. }
  328. rt_hw_spin_lock(&_regulator_lock.lock);
  329. reg_np = reg->reg_np;
  330. if (reg_np->ops->get_voltage)
  331. {
  332. uvolt = reg_np->ops->get_voltage(reg->reg_np);
  333. }
  334. else
  335. {
  336. uvolt = -RT_ENOSYS;
  337. }
  338. rt_hw_spin_unlock(&_regulator_lock.lock);
  339. return uvolt;
  340. }
  341. rt_err_t rt_regulator_set_mode(struct rt_regulator *reg, rt_uint32_t mode)
  342. {
  343. rt_err_t err;
  344. struct rt_regulator_node *reg_np;
  345. if (!reg)
  346. {
  347. return -RT_EINVAL;
  348. }
  349. rt_hw_spin_lock(&_regulator_lock.lock);
  350. reg_np = reg->reg_np;
  351. if (reg_np->ops->set_mode)
  352. {
  353. err = reg_np->ops->set_mode(reg_np, mode);
  354. }
  355. else
  356. {
  357. err = -RT_ENOSYS;
  358. }
  359. rt_hw_spin_unlock(&_regulator_lock.lock);
  360. return err;
  361. }
  362. rt_int32_t rt_regulator_get_mode(struct rt_regulator *reg)
  363. {
  364. rt_int32_t mode;
  365. struct rt_regulator_node *reg_np;
  366. if (!reg)
  367. {
  368. return -RT_EINVAL;
  369. }
  370. rt_hw_spin_lock(&_regulator_lock.lock);
  371. reg_np = reg->reg_np;
  372. if (reg_np->ops->get_mode)
  373. {
  374. mode = reg_np->ops->get_mode(reg_np);
  375. }
  376. else
  377. {
  378. mode = -RT_ENOSYS;
  379. }
  380. rt_hw_spin_unlock(&_regulator_lock.lock);
  381. return mode;
  382. }
  383. static void regulator_check_parent(struct rt_regulator_node *reg_np)
  384. {
  385. if (reg_np->parent)
  386. {
  387. return;
  388. }
  389. else
  390. {
  391. #ifdef RT_USING_OFW
  392. rt_phandle parent_phandle = 0;
  393. struct rt_ofw_node *np = reg_np->dev->ofw_node;
  394. while (np)
  395. {
  396. if (rt_ofw_prop_read_u32(np, "vin-supply", &parent_phandle))
  397. {
  398. break;
  399. }
  400. if (!(np = rt_ofw_find_node_by_phandle(parent_phandle)))
  401. {
  402. break;
  403. }
  404. if (!(reg_np->parent = rt_ofw_data(np)))
  405. {
  406. LOG_W("%s parent ofw node = %s not init",
  407. reg_np->supply_name, rt_ofw_node_full_name(np));
  408. rt_ofw_node_put(np);
  409. break;
  410. }
  411. rt_list_insert_after(&reg_np->parent->children_nodes, &reg_np->list);
  412. rt_ofw_node_put(np);
  413. }
  414. #endif
  415. }
  416. }
  417. struct rt_regulator *rt_regulator_get(struct rt_device *dev, const char *id)
  418. {
  419. struct rt_regulator *reg = RT_NULL;
  420. struct rt_regulator_node *reg_np = RT_NULL;
  421. if (!dev || !id)
  422. {
  423. reg = rt_err_ptr(-RT_EINVAL);
  424. goto _end;
  425. }
  426. #ifdef RT_USING_OFW
  427. if (dev->ofw_node)
  428. {
  429. rt_phandle supply_phandle;
  430. struct rt_ofw_node *np = dev->ofw_node;
  431. char supply_name[64];
  432. rt_snprintf(supply_name, sizeof(supply_name), "%s-supply", id);
  433. if (rt_ofw_prop_read_u32(np, supply_name, &supply_phandle))
  434. {
  435. goto _end;
  436. }
  437. if (!(np = rt_ofw_find_node_by_phandle(supply_phandle)))
  438. {
  439. reg = rt_err_ptr(-RT_EIO);
  440. goto _end;
  441. }
  442. if (!rt_ofw_data(np))
  443. {
  444. rt_platform_ofw_request(np);
  445. }
  446. reg_np = rt_ofw_data(np);
  447. rt_ofw_node_put(np);
  448. }
  449. #endif
  450. if (!reg_np)
  451. {
  452. reg = rt_err_ptr(-RT_ENOSYS);
  453. goto _end;
  454. }
  455. rt_hw_spin_lock(&_regulator_lock.lock);
  456. regulator_check_parent(reg_np);
  457. rt_hw_spin_unlock(&_regulator_lock.lock);
  458. reg = rt_calloc(1, sizeof(*reg));
  459. if (!reg)
  460. {
  461. reg = rt_err_ptr(-RT_ENOMEM);
  462. goto _end;
  463. }
  464. reg->reg_np = reg_np;
  465. rt_ref_get(&reg_np->ref);
  466. _end:
  467. return reg;
  468. }
  469. static void regulator_release(struct rt_ref *r)
  470. {
  471. struct rt_regulator_node *reg_np = rt_container_of(r, struct rt_regulator_node, ref);
  472. rt_regulator_unregister(reg_np);
  473. }
  474. void rt_regulator_put(struct rt_regulator *reg)
  475. {
  476. if (!reg)
  477. {
  478. return;
  479. }
  480. rt_ref_put(&reg->reg_np->ref, &regulator_release);
  481. rt_free(reg);
  482. }