regulator.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. * 2026-03-27 Evlers add current support and break away from reliance on DM,
  10. * and solve the problem of enabling counting.
  11. */
  12. #include <rtthread.h>
  13. #include <rtservice.h>
  14. #include <rtdevice.h>
  15. #define DBG_TAG "rtdm.regulator"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. struct rt_regulator
  19. {
  20. struct rt_regulator_node *reg_np;
  21. };
  22. struct rt_regulator_record
  23. {
  24. rt_list_t list;
  25. struct rt_regulator_node *reg_np;
  26. };
  27. static RT_DEFINE_SPINLOCK(_regulator_lock);
  28. static rt_list_t _regulator_records = RT_LIST_OBJECT_INIT(_regulator_records);
  29. static rt_err_t regulator_enable(struct rt_regulator_node *reg_np);
  30. static rt_err_t regulator_disable(struct rt_regulator_node *reg_np);
  31. static struct rt_regulator_record *regulator_find_record_by_name(const char *name)
  32. {
  33. struct rt_regulator_record *record = RT_NULL;
  34. rt_list_for_each_entry(record, &_regulator_records, list)
  35. {
  36. if (!rt_strcmp(record->reg_np->supply_name, name))
  37. {
  38. return record;
  39. }
  40. }
  41. return RT_NULL;
  42. }
  43. static struct rt_regulator_record *regulator_find_record_by_node(struct rt_regulator_node *reg_np)
  44. {
  45. struct rt_regulator_record *record = RT_NULL;
  46. rt_list_for_each_entry(record, &_regulator_records, list)
  47. {
  48. if (record->reg_np == reg_np)
  49. {
  50. return record;
  51. }
  52. }
  53. return RT_NULL;
  54. }
  55. rt_err_t rt_regulator_register(struct rt_regulator_node *reg_np)
  56. {
  57. rt_err_t err;
  58. struct rt_regulator_record *record;
  59. const struct rt_regulator_param *param;
  60. if (!reg_np || !reg_np->dev || !reg_np->param || !reg_np->ops)
  61. {
  62. return -RT_EINVAL;
  63. }
  64. rt_list_init(&reg_np->list);
  65. rt_list_init(&reg_np->children_nodes);
  66. rt_list_init(&reg_np->notifier_nodes);
  67. rt_ref_init(&reg_np->ref);
  68. rt_atomic_store(&reg_np->enabled_count, 0);
  69. param = reg_np->param;
  70. reg_np->parent = RT_NULL;
  71. if ((param->ramp_delay || param->ramp_disable) &&
  72. reg_np->ops->set_ramp_delay)
  73. {
  74. if ((err = reg_np->ops->set_ramp_delay(reg_np, param->ramp_delay)))
  75. {
  76. LOG_E("Set ramp error = %s\n", rt_strerror(err));
  77. return err;
  78. }
  79. }
  80. rt_hw_spin_lock(&_regulator_lock.lock);
  81. if (regulator_find_record_by_name(reg_np->supply_name))
  82. {
  83. rt_hw_spin_unlock(&_regulator_lock.lock);
  84. return -RT_EBUSY;
  85. }
  86. record = rt_calloc(1, sizeof(*record));
  87. if (!record)
  88. {
  89. rt_hw_spin_unlock(&_regulator_lock.lock);
  90. return -RT_ENOMEM;
  91. }
  92. record->reg_np = reg_np;
  93. rt_list_init(&record->list);
  94. rt_list_insert_before(&_regulator_records, &record->list);
  95. rt_hw_spin_unlock(&_regulator_lock.lock);
  96. #ifdef RT_USING_OFW
  97. if (reg_np->dev->ofw_node)
  98. {
  99. rt_ofw_data(reg_np->dev->ofw_node) = reg_np;
  100. }
  101. #endif /* RT_USING_OFW */
  102. if (param->boot_on || param->always_on)
  103. {
  104. regulator_enable(reg_np);
  105. }
  106. return RT_EOK;
  107. }
  108. rt_err_t rt_regulator_unregister(struct rt_regulator_node *reg_np)
  109. {
  110. rt_err_t err = RT_EOK;
  111. struct rt_regulator_record *record;
  112. if (!reg_np)
  113. {
  114. return -RT_EINVAL;
  115. }
  116. rt_hw_spin_lock(&_regulator_lock.lock);
  117. if (rt_atomic_load(&reg_np->enabled_count) != 0)
  118. {
  119. err = -RT_EBUSY;
  120. LOG_E("%s was enabled by consumer", reg_np->supply_name);
  121. goto _unlock;
  122. }
  123. if (!(reg_np->param->boot_on || reg_np->param->always_on))
  124. {
  125. regulator_disable(reg_np);
  126. }
  127. if (!rt_list_isempty(&reg_np->children_nodes) || rt_ref_read(&reg_np->ref) > 1)
  128. {
  129. err = -RT_EBUSY;
  130. goto _unlock;
  131. }
  132. reg_np->parent = RT_NULL;
  133. rt_list_remove(&reg_np->list);
  134. record = regulator_find_record_by_node(reg_np);
  135. if (record)
  136. {
  137. rt_list_remove(&record->list);
  138. }
  139. _unlock:
  140. rt_hw_spin_unlock(&_regulator_lock.lock);
  141. if (!err && record)
  142. {
  143. rt_free(record);
  144. }
  145. return err;
  146. }
  147. rt_err_t rt_regulator_notifier_register(struct rt_regulator *reg,
  148. struct rt_regulator_notifier *notifier)
  149. {
  150. struct rt_regulator_node *reg_np;
  151. if (!reg || !notifier)
  152. {
  153. return -RT_EINVAL;
  154. }
  155. rt_hw_spin_lock(&_regulator_lock.lock);
  156. reg_np = reg->reg_np;
  157. notifier->regulator = reg;
  158. rt_list_init(&notifier->list);
  159. rt_list_insert_after(&reg_np->notifier_nodes, &notifier->list);
  160. rt_hw_spin_unlock(&_regulator_lock.lock);
  161. return RT_EOK;
  162. }
  163. rt_err_t rt_regulator_notifier_unregister(struct rt_regulator *reg,
  164. struct rt_regulator_notifier *notifier)
  165. {
  166. if (!reg || !notifier)
  167. {
  168. return -RT_EINVAL;
  169. }
  170. rt_hw_spin_lock(&_regulator_lock.lock);
  171. rt_list_remove(&notifier->list);
  172. rt_hw_spin_unlock(&_regulator_lock.lock);
  173. return RT_EOK;
  174. }
  175. static rt_err_t regulator_notifier_call_chain(struct rt_regulator_node *reg_np,
  176. rt_ubase_t msg, void *data)
  177. {
  178. rt_err_t err = RT_EOK;
  179. struct rt_regulator_notifier *notifier;
  180. rt_list_t *head = &reg_np->notifier_nodes;
  181. if (rt_list_isempty(head))
  182. {
  183. return err;
  184. }
  185. rt_list_for_each_entry(notifier, head, list)
  186. {
  187. err = notifier->callback(notifier, msg, data);
  188. if (err == -RT_EIO)
  189. {
  190. break;
  191. }
  192. }
  193. return err;
  194. }
  195. static rt_uint32_t regulator_get_enable_time(struct rt_regulator_node *reg_np)
  196. {
  197. if (reg_np->param->enable_delay)
  198. {
  199. return reg_np->param->enable_delay;
  200. }
  201. if (reg_np->ops->enable_time)
  202. {
  203. return reg_np->ops->enable_time(reg_np);
  204. }
  205. return 0;
  206. }
  207. static rt_uint32_t regulator_set_voltage_time(struct rt_regulator_node *reg_np,
  208. int old_uvolt, int new_uvolt)
  209. {
  210. unsigned int ramp_delay = 0;
  211. if (reg_np->param->ramp_delay)
  212. {
  213. ramp_delay = reg_np->param->ramp_delay;
  214. }
  215. else if (reg_np->param->ramp_delay)
  216. {
  217. ramp_delay = reg_np->param->ramp_delay;
  218. }
  219. else if (reg_np->param->settling_time)
  220. {
  221. return reg_np->param->settling_time;
  222. }
  223. else if (reg_np->param->settling_time_up && new_uvolt > old_uvolt)
  224. {
  225. return reg_np->param->settling_time_up;
  226. }
  227. else if (reg_np->param->settling_time_down && new_uvolt < old_uvolt)
  228. {
  229. return reg_np->param->settling_time_down;
  230. }
  231. if (ramp_delay == 0)
  232. {
  233. return 0;
  234. }
  235. return RT_DIV_ROUND_UP(rt_abs(new_uvolt - old_uvolt), ramp_delay);
  236. }
  237. static void regulator_delay(rt_uint32_t delay)
  238. {
  239. rt_uint32_t ms = delay / 1000;
  240. rt_uint32_t us = delay % 1000;
  241. if (ms > 0)
  242. {
  243. /*
  244. * For small enough values, handle super-millisecond
  245. * delays in the usleep_range() call below.
  246. */
  247. if (ms < 20)
  248. {
  249. us += ms * 1000;
  250. }
  251. else if (rt_thread_self())
  252. {
  253. rt_thread_mdelay(ms);
  254. }
  255. else
  256. {
  257. rt_hw_us_delay(ms * 1000);
  258. }
  259. }
  260. /*
  261. * Give the scheduler some room to coalesce with any other
  262. * wakeup sources. For delays shorter than 10 us, don't even
  263. * bother setting up high-resolution timers and just busy-loop.
  264. */
  265. if (us >= 10)
  266. {
  267. rt_hw_us_delay((us + 100) >> 1);
  268. }
  269. else
  270. {
  271. rt_hw_us_delay(us);
  272. }
  273. }
  274. static rt_err_t regulator_enable(struct rt_regulator_node *reg_np)
  275. {
  276. rt_err_t err = RT_EOK;
  277. if (reg_np->ops->enable)
  278. {
  279. err = reg_np->ops->enable(reg_np);
  280. if (!err)
  281. {
  282. rt_uint32_t enable_delay = regulator_get_enable_time(reg_np);
  283. if (enable_delay)
  284. {
  285. regulator_delay(enable_delay);
  286. }
  287. err = regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_ENABLE, RT_NULL);
  288. }
  289. }
  290. if (!err && reg_np->parent)
  291. {
  292. err = regulator_enable(reg_np->parent);
  293. }
  294. return err;
  295. }
  296. rt_err_t rt_regulator_enable(struct rt_regulator *reg)
  297. {
  298. rt_err_t err;
  299. int enabled_cnt;
  300. if (!reg)
  301. {
  302. return -RT_EINVAL;
  303. }
  304. if (rt_regulator_is_enabled(reg))
  305. {
  306. rt_atomic_add(&reg->reg_np->enabled_count, 1);
  307. return RT_EOK;
  308. }
  309. rt_hw_spin_lock(&_regulator_lock.lock);
  310. enabled_cnt = rt_atomic_load(&reg->reg_np->enabled_count);
  311. if (enabled_cnt > 0)
  312. {
  313. rt_atomic_add(&reg->reg_np->enabled_count, 1);
  314. rt_hw_spin_unlock(&_regulator_lock.lock);
  315. return RT_EOK;
  316. }
  317. err = regulator_enable(reg->reg_np);
  318. rt_hw_spin_unlock(&_regulator_lock.lock);
  319. return err;
  320. }
  321. static rt_err_t regulator_disable(struct rt_regulator_node *reg_np)
  322. {
  323. rt_err_t err = RT_EOK;
  324. if (reg_np->ops->disable)
  325. {
  326. err = reg_np->ops->disable(reg_np);
  327. if (!err)
  328. {
  329. if (reg_np->param->off_on_delay)
  330. {
  331. regulator_delay(reg_np->param->off_on_delay);
  332. }
  333. err = regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_DISABLE, RT_NULL);
  334. }
  335. }
  336. if (!err && reg_np->parent)
  337. {
  338. err = regulator_disable(reg_np->parent);
  339. }
  340. return err;
  341. }
  342. rt_err_t rt_regulator_disable(struct rt_regulator *reg)
  343. {
  344. rt_err_t err = RT_EOK;
  345. int enabled_cnt;
  346. if (!reg)
  347. {
  348. return -RT_EINVAL;
  349. }
  350. if (!rt_regulator_is_enabled(reg))
  351. {
  352. return RT_EOK;
  353. }
  354. rt_hw_spin_lock(&_regulator_lock.lock);
  355. enabled_cnt = rt_atomic_load(&reg->reg_np->enabled_count);
  356. rt_atomic_sub(&reg->reg_np->enabled_count, 1);
  357. if (enabled_cnt == 1)
  358. {
  359. err = regulator_disable(reg->reg_np);
  360. }
  361. rt_hw_spin_unlock(&_regulator_lock.lock);
  362. return err;
  363. }
  364. rt_bool_t rt_regulator_is_enabled(struct rt_regulator *reg)
  365. {
  366. if (!reg)
  367. {
  368. return RT_FALSE;
  369. }
  370. if (reg->reg_np->ops->is_enabled)
  371. {
  372. return reg->reg_np->ops->is_enabled(reg->reg_np);
  373. }
  374. return rt_atomic_load(&reg->reg_np->enabled_count) > 0;
  375. }
  376. static rt_err_t regulator_set_voltage(struct rt_regulator_node *reg_np, int min_uvolt, int max_uvolt)
  377. {
  378. rt_err_t err = RT_EOK;
  379. if (reg_np->ops->set_voltage)
  380. {
  381. union rt_regulator_notifier_args args;
  382. RT_ASSERT(reg_np->ops->get_voltage != RT_NULL);
  383. args.old_uvolt = reg_np->ops->get_voltage(reg_np);
  384. args.min_uvolt = min_uvolt;
  385. args.max_uvolt = max_uvolt;
  386. err = regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_VOLTAGE_CHANGE, &args);
  387. if (!err)
  388. {
  389. err = reg_np->ops->set_voltage(reg_np, min_uvolt, max_uvolt);
  390. }
  391. if (!err)
  392. {
  393. rt_uint32_t delay = regulator_set_voltage_time(reg_np,
  394. args.old_uvolt, reg_np->ops->get_voltage(reg_np));
  395. if (delay)
  396. {
  397. regulator_delay(delay);
  398. }
  399. }
  400. else
  401. {
  402. regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_VOLTAGE_CHANGE_ERR,
  403. (void *)(rt_base_t)args.old_uvolt);
  404. }
  405. }
  406. if (!err && reg_np->parent)
  407. {
  408. err = regulator_set_voltage(reg_np->parent, min_uvolt, max_uvolt);
  409. }
  410. return err;
  411. }
  412. static rt_err_t regulator_set_current(struct rt_regulator_node *reg_np, int min_uamp, int max_uamp)
  413. {
  414. rt_err_t err = RT_EOK;
  415. if (reg_np->ops->set_current)
  416. {
  417. union rt_regulator_notifier_args args;
  418. RT_ASSERT(reg_np->ops->get_current != RT_NULL);
  419. args.old_uamp = reg_np->ops->get_current(reg_np);
  420. args.min_uamp = min_uamp;
  421. args.max_uamp = max_uamp;
  422. err = regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_CURRENT_CHANGE, &args);
  423. if (!err)
  424. {
  425. err = reg_np->ops->set_current(reg_np, min_uamp, max_uamp);
  426. }
  427. if (err)
  428. {
  429. regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_CURRENT_CHANGE_ERR,
  430. (void *)(rt_base_t)args.old_uamp);
  431. }
  432. }
  433. else
  434. {
  435. err = -RT_ENOSYS;
  436. }
  437. return err;
  438. }
  439. rt_bool_t rt_regulator_is_supported_voltage(struct rt_regulator *reg, int min_uvolt, int max_uvolt)
  440. {
  441. const struct rt_regulator_param *param;
  442. RT_ASSERT(reg != RT_NULL);
  443. param = reg->reg_np->param;
  444. if (!param)
  445. {
  446. return RT_FALSE;
  447. }
  448. return param->min_uvolt <= min_uvolt && param->max_uvolt >= max_uvolt;
  449. }
  450. rt_err_t rt_regulator_set_voltage(struct rt_regulator *reg, int min_uvolt, int max_uvolt)
  451. {
  452. rt_err_t err;
  453. if (!reg)
  454. {
  455. return -RT_EINVAL;
  456. }
  457. rt_hw_spin_lock(&_regulator_lock.lock);
  458. err = regulator_set_voltage(reg->reg_np, min_uvolt, max_uvolt);
  459. rt_hw_spin_unlock(&_regulator_lock.lock);
  460. return err;
  461. }
  462. int rt_regulator_get_voltage(struct rt_regulator *reg)
  463. {
  464. int uvolt = RT_REGULATOR_UVOLT_INVALID;
  465. struct rt_regulator_node *reg_np;
  466. if (!reg)
  467. {
  468. return -RT_EINVAL;
  469. }
  470. rt_hw_spin_lock(&_regulator_lock.lock);
  471. reg_np = reg->reg_np;
  472. if (reg_np->ops->get_voltage)
  473. {
  474. uvolt = reg_np->ops->get_voltage(reg->reg_np);
  475. }
  476. else
  477. {
  478. uvolt = -RT_ENOSYS;
  479. }
  480. rt_hw_spin_unlock(&_regulator_lock.lock);
  481. return uvolt;
  482. }
  483. rt_bool_t rt_regulator_is_supported_current(struct rt_regulator *reg, int min_uamp, int max_uamp)
  484. {
  485. const struct rt_regulator_param *param;
  486. if (!reg)
  487. {
  488. return RT_FALSE;
  489. }
  490. param = reg->reg_np->param;
  491. if (!param || param->max_uamp <= 0)
  492. {
  493. return RT_FALSE;
  494. }
  495. return param->min_uamp <= min_uamp && param->max_uamp >= max_uamp;
  496. }
  497. rt_err_t rt_regulator_set_current(struct rt_regulator *reg, int min_uamp, int max_uamp)
  498. {
  499. rt_err_t err;
  500. if (!reg)
  501. {
  502. return -RT_EINVAL;
  503. }
  504. rt_hw_spin_lock(&_regulator_lock.lock);
  505. err = regulator_set_current(reg->reg_np, min_uamp, max_uamp);
  506. rt_hw_spin_unlock(&_regulator_lock.lock);
  507. return err;
  508. }
  509. int rt_regulator_get_current(struct rt_regulator *reg)
  510. {
  511. int uamp = RT_REGULATOR_UAMP_INVALID;
  512. struct rt_regulator_node *reg_np;
  513. if (!reg)
  514. {
  515. return -RT_EINVAL;
  516. }
  517. rt_hw_spin_lock(&_regulator_lock.lock);
  518. reg_np = reg->reg_np;
  519. if (reg_np->ops->get_current)
  520. {
  521. uamp = reg_np->ops->get_current(reg_np);
  522. }
  523. else
  524. {
  525. uamp = -RT_ENOSYS;
  526. }
  527. rt_hw_spin_unlock(&_regulator_lock.lock);
  528. return uamp;
  529. }
  530. rt_err_t rt_regulator_set_mode(struct rt_regulator *reg, rt_uint32_t mode)
  531. {
  532. rt_err_t err;
  533. struct rt_regulator_node *reg_np;
  534. if (!reg)
  535. {
  536. return -RT_EINVAL;
  537. }
  538. rt_hw_spin_lock(&_regulator_lock.lock);
  539. reg_np = reg->reg_np;
  540. if (reg_np->ops->set_mode)
  541. {
  542. err = reg_np->ops->set_mode(reg_np, mode);
  543. }
  544. else
  545. {
  546. err = -RT_ENOSYS;
  547. }
  548. rt_hw_spin_unlock(&_regulator_lock.lock);
  549. return err;
  550. }
  551. rt_int32_t rt_regulator_get_mode(struct rt_regulator *reg)
  552. {
  553. rt_int32_t mode;
  554. struct rt_regulator_node *reg_np;
  555. if (!reg)
  556. {
  557. return -RT_EINVAL;
  558. }
  559. rt_hw_spin_lock(&_regulator_lock.lock);
  560. reg_np = reg->reg_np;
  561. if (reg_np->ops->get_mode)
  562. {
  563. mode = reg_np->ops->get_mode(reg_np);
  564. }
  565. else
  566. {
  567. mode = -RT_ENOSYS;
  568. }
  569. rt_hw_spin_unlock(&_regulator_lock.lock);
  570. return mode;
  571. }
  572. static void regulator_check_parent(struct rt_regulator_node *reg_np)
  573. {
  574. if (reg_np->parent)
  575. {
  576. return;
  577. }
  578. else
  579. {
  580. #ifdef RT_USING_OFW
  581. rt_phandle parent_phandle = 0;
  582. struct rt_ofw_node *np = reg_np->dev->ofw_node;
  583. while (np)
  584. {
  585. if (rt_ofw_prop_read_u32(np, "vin-supply", &parent_phandle))
  586. {
  587. break;
  588. }
  589. if (!(np = rt_ofw_find_node_by_phandle(parent_phandle)))
  590. {
  591. break;
  592. }
  593. if (!(reg_np->parent = rt_ofw_data(np)))
  594. {
  595. LOG_W("%s parent ofw node = %s not init",
  596. reg_np->supply_name, rt_ofw_node_full_name(np));
  597. rt_ofw_node_put(np);
  598. break;
  599. }
  600. rt_list_insert_after(&reg_np->parent->children_nodes, &reg_np->list);
  601. rt_ofw_node_put(np);
  602. }
  603. #endif /* RT_USING_OFW */
  604. }
  605. }
  606. struct rt_regulator *rt_regulator_get(struct rt_device *dev, const char *id)
  607. {
  608. struct rt_regulator *reg = RT_NULL;
  609. struct rt_regulator_node *reg_np = RT_NULL;
  610. if (!id)
  611. {
  612. reg = rt_err_ptr(-RT_EINVAL);
  613. goto _end;
  614. }
  615. #ifdef RT_USING_OFW
  616. if (dev && dev->ofw_node)
  617. {
  618. rt_phandle supply_phandle;
  619. struct rt_ofw_node *np = dev->ofw_node;
  620. char supply_name[64];
  621. rt_snprintf(supply_name, sizeof(supply_name), "%s-supply", id);
  622. if (rt_ofw_prop_read_u32(np, supply_name, &supply_phandle))
  623. {
  624. goto _end;
  625. }
  626. if (!(np = rt_ofw_find_node_by_phandle(supply_phandle)))
  627. {
  628. reg = rt_err_ptr(-RT_EIO);
  629. goto _end;
  630. }
  631. if (!rt_ofw_data(np))
  632. {
  633. rt_platform_ofw_request(np);
  634. }
  635. reg_np = rt_ofw_data(np);
  636. rt_ofw_node_put(np);
  637. }
  638. #endif /* RT_USING_OFW */
  639. if (!reg_np)
  640. {
  641. struct rt_regulator_record *record;
  642. rt_hw_spin_lock(&_regulator_lock.lock);
  643. record = regulator_find_record_by_name(id);
  644. if (record)
  645. {
  646. reg_np = record->reg_np;
  647. }
  648. rt_hw_spin_unlock(&_regulator_lock.lock);
  649. }
  650. if (!reg_np)
  651. {
  652. goto _end;
  653. }
  654. rt_hw_spin_lock(&_regulator_lock.lock);
  655. regulator_check_parent(reg_np);
  656. rt_hw_spin_unlock(&_regulator_lock.lock);
  657. reg = rt_calloc(1, sizeof(*reg));
  658. if (!reg)
  659. {
  660. reg = rt_err_ptr(-RT_ENOMEM);
  661. goto _end;
  662. }
  663. reg->reg_np = reg_np;
  664. rt_ref_get(&reg_np->ref);
  665. _end:
  666. return reg;
  667. }
  668. static void regulator_release(struct rt_ref *r)
  669. {
  670. struct rt_regulator_node *reg_np = rt_container_of(r, struct rt_regulator_node, ref);
  671. rt_regulator_unregister(reg_np);
  672. }
  673. void rt_regulator_put(struct rt_regulator *reg)
  674. {
  675. if (!reg)
  676. {
  677. return;
  678. }
  679. rt_ref_put(&reg->reg_np->ref, &regulator_release);
  680. rt_free(reg);
  681. }
  682. struct rt_regulator_node **rt_regulator_nodes_snapshot(rt_size_t *count)
  683. {
  684. struct rt_regulator_record *record;
  685. struct rt_regulator_node **nodes;
  686. rt_size_t total = 0;
  687. rt_size_t idx = 0;
  688. if (!count)
  689. {
  690. return RT_NULL;
  691. }
  692. *count = 0;
  693. rt_hw_spin_lock(&_regulator_lock.lock);
  694. rt_list_for_each_entry(record, &_regulator_records, list)
  695. {
  696. total++;
  697. }
  698. rt_hw_spin_unlock(&_regulator_lock.lock);
  699. if (!total)
  700. {
  701. return RT_NULL;
  702. }
  703. nodes = rt_calloc(total, sizeof(*nodes));
  704. if (!nodes)
  705. {
  706. return RT_NULL;
  707. }
  708. rt_hw_spin_lock(&_regulator_lock.lock);
  709. rt_list_for_each_entry(record, &_regulator_records, list)
  710. {
  711. nodes[idx] = record->reg_np;
  712. rt_ref_get(&record->reg_np->ref);
  713. idx++;
  714. }
  715. rt_hw_spin_unlock(&_regulator_lock.lock);
  716. *count = total;
  717. return nodes;
  718. }
  719. void rt_regulator_nodes_snapshot_free(struct rt_regulator_node **nodes, rt_size_t count)
  720. {
  721. if (!nodes)
  722. {
  723. return;
  724. }
  725. while (count--)
  726. {
  727. rt_ref_put(&nodes[count]->ref, &regulator_release);
  728. }
  729. rt_free(nodes);
  730. }