regulator.c 14 KB

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