supply.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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-02-25 GuEe-GUI the first version
  9. * 2026-03-27 Evlers add CLI snapshot/name helpers and OFW guards
  10. */
  11. #include <rtdevice.h>
  12. #define DBG_TAG "rtdm.power_supply"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. #ifndef INT_MAX
  16. #define INT_MAX (RT_UINT32_MAX >> 1)
  17. #endif
  18. #ifndef INT_MIN
  19. #define INT_MIN (-INT_MAX - 1)
  20. #endif
  21. static RT_DEFINE_SPINLOCK(nodes_lock);
  22. static rt_list_t power_supply_nodes = RT_LIST_OBJECT_INIT(power_supply_nodes);
  23. static rt_list_t power_supply_notifier_nodes = RT_LIST_OBJECT_INIT(power_supply_notifier_nodes);
  24. const char *rt_power_supply_name(struct rt_power_supply *psy)
  25. {
  26. struct rt_device *dev = psy ? psy->dev : RT_NULL;
  27. #ifdef RT_USING_DM
  28. return rt_dm_dev_get_name(dev);
  29. #else
  30. return dev ? dev->parent.name : "<no-dev>";
  31. #endif
  32. }
  33. static rt_bool_t power_supply_have_property(struct rt_power_supply *psy,
  34. enum rt_power_supply_property prop);
  35. #ifdef RT_USING_THERMAL
  36. static rt_err_t power_supply_thermal_zone_get_temp(struct rt_thermal_zone_device *zdev,
  37. int *out_temp)
  38. {
  39. rt_err_t err;
  40. union rt_power_supply_property_val val;
  41. struct rt_power_supply *psy = zdev->priv;
  42. if ((err = rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_TEMP, &val)))
  43. {
  44. return err;
  45. }
  46. *out_temp = val.intval;
  47. return RT_EOK;
  48. }
  49. const static struct rt_thermal_zone_ops power_supply_thermal_zone_ops =
  50. {
  51. .get_temp = power_supply_thermal_zone_get_temp,
  52. };
  53. rt_err_t power_supply_thermal_register(struct rt_power_supply *psy)
  54. {
  55. if (psy->thermal_dev)
  56. {
  57. return RT_EOK;
  58. }
  59. if (power_supply_have_property(psy, RT_POWER_SUPPLY_PROP_TEMP))
  60. {
  61. rt_err_t err;
  62. if (!(psy->thermal_dev = rt_calloc(1, sizeof(*psy->thermal_dev))))
  63. {
  64. return -RT_ENOMEM;
  65. }
  66. #ifdef RT_USING_DM
  67. rt_dm_dev_set_name(&psy->thermal_dev->parent, rt_dm_dev_get_name(psy->dev));
  68. #else
  69. psy->thermal_dev->parent.parent.name = rt_power_supply_name(psy);
  70. #endif
  71. psy->thermal_dev->zone_id = 0;
  72. psy->thermal_dev->ops = &power_supply_thermal_zone_ops;
  73. #ifdef RT_USING_OFW
  74. psy->thermal_dev->parent.ofw_node = psy->dev->ofw_node;
  75. #endif
  76. psy->thermal_dev->priv = psy;
  77. if ((err = rt_thermal_zone_device_register(psy->thermal_dev)))
  78. {
  79. rt_free(psy->thermal_dev);
  80. psy->thermal_dev = RT_NULL;
  81. return err;
  82. }
  83. }
  84. return RT_EOK;
  85. }
  86. rt_err_t power_supply_thermal_unregister(struct rt_power_supply *psy)
  87. {
  88. rt_err_t err = RT_EOK;
  89. if (psy->thermal_dev)
  90. {
  91. if (!(err = rt_thermal_zone_device_unregister(psy->thermal_dev)))
  92. {
  93. rt_free(psy->thermal_dev);
  94. psy->thermal_dev = RT_NULL;
  95. }
  96. }
  97. return err;
  98. }
  99. #else
  100. rt_err_t power_supply_thermal_register(struct rt_power_supply *psy)
  101. {
  102. return RT_EOK;
  103. }
  104. rt_err_t power_supply_thermal_unregister(struct rt_power_supply *psy)
  105. {
  106. return RT_EOK;
  107. }
  108. #endif /* RT_USING_THERMAL */
  109. #ifdef RT_USING_LED
  110. static void power_supply_update_battery_led(struct rt_power_supply *psy)
  111. {
  112. union rt_power_supply_property_val status;
  113. if (rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_STATUS, &status))
  114. {
  115. return;
  116. }
  117. switch (status.intval)
  118. {
  119. case RT_POWER_SUPPLY_STATUS_FULL:
  120. rt_led_set_state(psy->led_dev, RT_LED_S_ON);
  121. rt_led_set_brightness(psy->led_dev, 255);
  122. break;
  123. case RT_POWER_SUPPLY_STATUS_CHARGING:
  124. rt_led_set_state(psy->led_dev, RT_LED_S_ON);
  125. rt_led_set_brightness(psy->led_dev, 255 >> 1);
  126. break;
  127. default:
  128. rt_led_set_state(psy->led_dev, RT_LED_S_OFF);
  129. break;
  130. }
  131. }
  132. static void power_supply_update_online_led(struct rt_power_supply *psy)
  133. {
  134. union rt_power_supply_property_val online;
  135. if (rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_ONLINE, &online))
  136. {
  137. return;
  138. }
  139. if (online.intval)
  140. {
  141. rt_led_set_state(psy->led_dev, RT_LED_S_ON);
  142. }
  143. else
  144. {
  145. rt_led_set_state(psy->led_dev, RT_LED_S_OFF);
  146. }
  147. }
  148. static void power_supply_update_led(struct rt_power_supply *psy)
  149. {
  150. if (!psy->led_dev)
  151. {
  152. return;
  153. }
  154. if (psy->type == RT_POWER_SUPPLY_TYPE_BATTERY)
  155. {
  156. power_supply_update_battery_led(psy);
  157. }
  158. else
  159. {
  160. power_supply_update_online_led(psy);
  161. }
  162. }
  163. #else
  164. static void power_supply_update_led(struct rt_power_supply *psy)
  165. {
  166. }
  167. #endif /* RT_USING_LED */
  168. static void power_supply_changed_work(struct rt_work *work, void *work_data)
  169. {
  170. struct rt_power_supply *psy = work_data;
  171. struct rt_power_supply_notifier *notifier, *next_notifier;
  172. power_supply_update_led(psy);
  173. rt_spin_lock(&nodes_lock);
  174. rt_list_for_each_entry_safe(notifier, next_notifier, &power_supply_notifier_nodes, list)
  175. {
  176. rt_spin_unlock(&nodes_lock);
  177. notifier->callback(notifier, psy);
  178. rt_spin_lock(&nodes_lock);
  179. }
  180. rt_spin_unlock(&nodes_lock);
  181. }
  182. rt_err_t rt_power_supply_register(struct rt_power_supply *psy)
  183. {
  184. rt_err_t err;
  185. if (!psy || !psy->dev)
  186. {
  187. return -RT_EINVAL;
  188. }
  189. if (!psy->battery_info && (!psy->properties_nr || !psy->properties || !psy->ops))
  190. {
  191. return -RT_EINVAL;
  192. }
  193. if ((err = power_supply_thermal_register(psy)))
  194. {
  195. return err;
  196. }
  197. rt_ref_init(&psy->ref);
  198. rt_list_init(&psy->list);
  199. rt_work_init(&psy->changed_work, power_supply_changed_work, psy);
  200. rt_spin_lock(&nodes_lock);
  201. rt_list_insert_before(&power_supply_nodes, &psy->list);
  202. rt_spin_unlock(&nodes_lock);
  203. #ifdef RT_USING_OFW
  204. if (psy->dev->ofw_node)
  205. {
  206. #ifdef RT_USING_DM
  207. rt_dm_dev_bind_fwdata(psy->dev, RT_NULL, psy);
  208. #endif
  209. }
  210. #endif
  211. return RT_EOK;
  212. }
  213. rt_err_t rt_power_supply_unregister(struct rt_power_supply *psy)
  214. {
  215. rt_err_t err;
  216. if (!psy)
  217. {
  218. return -RT_EINVAL;
  219. }
  220. rt_spin_lock(&nodes_lock);
  221. if (rt_ref_read(&psy->ref) > 1)
  222. {
  223. err = -RT_EBUSY;
  224. goto _unlock;
  225. }
  226. rt_list_remove(&psy->list);
  227. #ifdef RT_USING_OFW
  228. if (psy->dev->ofw_node)
  229. {
  230. #ifdef RT_USING_DM
  231. rt_dm_dev_unbind_fwdata(psy->dev, RT_NULL);
  232. #endif
  233. }
  234. #endif
  235. _unlock:
  236. rt_spin_unlock(&nodes_lock);
  237. if (!err)
  238. {
  239. rt_work_cancel(&psy->changed_work);
  240. err = power_supply_thermal_unregister(psy);
  241. }
  242. return err;
  243. }
  244. rt_err_t rt_power_supply_notifier_register(struct rt_power_supply_notifier *notifier)
  245. {
  246. if (!notifier || !notifier->callback)
  247. {
  248. return -RT_EINVAL;
  249. }
  250. rt_list_init(&notifier->list);
  251. rt_spin_lock(&nodes_lock);
  252. rt_list_insert_before(&power_supply_notifier_nodes, &notifier->list);
  253. rt_spin_unlock(&nodes_lock);
  254. return RT_EOK;
  255. }
  256. rt_err_t rt_power_supply_notifier_unregister(struct rt_power_supply_notifier *notifier)
  257. {
  258. if (!notifier)
  259. {
  260. return -RT_EINVAL;
  261. }
  262. rt_spin_lock(&nodes_lock);
  263. rt_list_remove(&notifier->list);
  264. rt_spin_unlock(&nodes_lock);
  265. return RT_EOK;
  266. }
  267. static rt_bool_t power_supply_have_property(struct rt_power_supply *psy,
  268. enum rt_power_supply_property prop)
  269. {
  270. if (!psy->ops->get_property)
  271. {
  272. return RT_FALSE;
  273. }
  274. for (int i = 0; i < psy->properties_nr; ++i)
  275. {
  276. if (psy->properties[i] == prop)
  277. {
  278. return RT_TRUE;
  279. }
  280. }
  281. return RT_FALSE;
  282. }
  283. static rt_bool_t power_supply_battery_info_have_property(
  284. struct rt_power_supply_battery_info *info, enum rt_power_supply_property prop)
  285. {
  286. if (!info)
  287. {
  288. return RT_FALSE;
  289. }
  290. switch (prop)
  291. {
  292. case RT_POWER_SUPPLY_PROP_TECHNOLOGY:
  293. return info->technology != RT_POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  294. case RT_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  295. return info->energy_full_design_uwh >= 0;
  296. case RT_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  297. return info->charge_full_design_uah >= 0;
  298. case RT_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  299. return info->voltage_min_design_uv >= 0;
  300. case RT_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  301. return info->voltage_max_design_uv >= 0;
  302. case RT_POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
  303. return info->precharge_current_ua >= 0;
  304. case RT_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
  305. return info->charge_term_current_ua >= 0;
  306. case RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  307. return info->constant_charge_current_max_ua >= 0;
  308. case RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  309. return info->constant_charge_voltage_max_uv >= 0;
  310. case RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:
  311. return info->temp_ambient_alert_min > INT_MIN;
  312. case RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:
  313. return info->temp_ambient_alert_max < INT_MAX;
  314. case RT_POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
  315. return info->temp_alert_min > INT_MIN;
  316. case RT_POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
  317. return info->temp_alert_max < INT_MAX;
  318. case RT_POWER_SUPPLY_PROP_TEMP_MIN:
  319. return info->temp_min > INT_MIN;
  320. case RT_POWER_SUPPLY_PROP_TEMP_MAX:
  321. return info->temp_max < INT_MAX;
  322. default:
  323. return RT_FALSE;
  324. }
  325. }
  326. static rt_err_t power_supply_battery_info_get_property(
  327. struct rt_power_supply_battery_info *info, enum rt_power_supply_property prop,
  328. union rt_power_supply_property_val *val)
  329. {
  330. switch (prop)
  331. {
  332. case RT_POWER_SUPPLY_PROP_TECHNOLOGY:
  333. val->intval = info->technology;
  334. break;
  335. case RT_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  336. val->intval = info->energy_full_design_uwh;
  337. break;
  338. case RT_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  339. val->intval = info->charge_full_design_uah;
  340. break;
  341. case RT_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  342. val->intval = info->voltage_min_design_uv;
  343. break;
  344. case RT_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  345. val->intval = info->voltage_max_design_uv;
  346. break;
  347. case RT_POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
  348. val->intval = info->precharge_current_ua;
  349. break;
  350. case RT_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
  351. val->intval = info->charge_term_current_ua;
  352. break;
  353. case RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  354. val->intval = info->constant_charge_current_max_ua;
  355. break;
  356. case RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  357. val->intval = info->constant_charge_voltage_max_uv;
  358. break;
  359. case RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:
  360. val->intval = info->temp_ambient_alert_min;
  361. break;
  362. case RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:
  363. val->intval = info->temp_ambient_alert_max;
  364. break;
  365. case RT_POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
  366. val->intval = info->temp_alert_min;
  367. break;
  368. case RT_POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
  369. val->intval = info->temp_alert_max;
  370. break;
  371. case RT_POWER_SUPPLY_PROP_TEMP_MIN:
  372. val->intval = info->temp_min;
  373. break;
  374. case RT_POWER_SUPPLY_PROP_TEMP_MAX:
  375. val->intval = info->temp_max;
  376. break;
  377. default:
  378. return -RT_EINVAL;
  379. }
  380. return RT_EOK;
  381. }
  382. rt_err_t rt_power_supply_get_property(struct rt_power_supply *psy,
  383. enum rt_power_supply_property prop,
  384. union rt_power_supply_property_val *val)
  385. {
  386. if (!psy || !val)
  387. {
  388. return -RT_EINVAL;
  389. }
  390. if (power_supply_have_property(psy, prop))
  391. {
  392. return psy->ops->get_property(psy, prop, val);
  393. }
  394. else if (power_supply_battery_info_have_property(psy->battery_info, prop))
  395. {
  396. return power_supply_battery_info_get_property(psy->battery_info, prop, val);
  397. }
  398. return -RT_ENOSYS;
  399. }
  400. rt_err_t rt_power_supply_set_property(struct rt_power_supply *psy,
  401. enum rt_power_supply_property prop,
  402. const union rt_power_supply_property_val *val)
  403. {
  404. if (!psy || !val)
  405. {
  406. return -RT_EINVAL;
  407. }
  408. if (!psy->ops->set_property)
  409. {
  410. return -RT_ENOSYS;
  411. }
  412. return psy->ops->set_property(psy, prop, val);
  413. }
  414. void rt_power_supply_changed(struct rt_power_supply *psy)
  415. {
  416. RT_ASSERT(psy != RT_NULL);
  417. rt_work_submit(&psy->changed_work, 0);
  418. }
  419. struct rt_power_supply *rt_power_supply_get(struct rt_device *dev, const char *id)
  420. {
  421. struct rt_power_supply *psy = RT_NULL;
  422. if (!dev || !id)
  423. {
  424. return rt_err_ptr(-RT_EINVAL);
  425. }
  426. #ifdef RT_USING_OFW
  427. if (dev->ofw_node)
  428. {
  429. struct rt_ofw_node *psy_np = rt_ofw_parse_phandle(dev->ofw_node, id, 0);
  430. if (!psy_np)
  431. {
  432. return psy;
  433. }
  434. psy = rt_ofw_data(psy_np);
  435. }
  436. #endif /* RT_USING_OFW */
  437. if (!psy)
  438. {
  439. struct rt_power_supply *psy_target, *psy_next;
  440. rt_spin_lock(&nodes_lock);
  441. rt_list_for_each_entry_safe(psy_target, psy_next, &power_supply_nodes, list)
  442. {
  443. if (!rt_strcmp(psy_target->dev->parent.name, id))
  444. {
  445. psy = psy_target;
  446. break;
  447. }
  448. }
  449. rt_spin_unlock(&nodes_lock);
  450. }
  451. return psy;
  452. }
  453. static void power_supply_release(struct rt_ref *r)
  454. {
  455. struct rt_power_supply *psy = rt_container_of(r, struct rt_power_supply, ref);
  456. rt_power_supply_unregister(psy);
  457. }
  458. void rt_power_supply_put(struct rt_power_supply *psy)
  459. {
  460. if (!psy)
  461. {
  462. return;
  463. }
  464. rt_ref_put(&psy->ref, power_supply_release);
  465. }
  466. struct rt_power_supply **rt_power_supply_snapshot(rt_size_t *count)
  467. {
  468. struct rt_power_supply *psy, *psy_next;
  469. struct rt_power_supply **nodes;
  470. rt_size_t total = 0;
  471. rt_size_t idx = 0;
  472. if (!count)
  473. {
  474. return RT_NULL;
  475. }
  476. *count = 0;
  477. rt_spin_lock(&nodes_lock);
  478. rt_list_for_each_entry_safe(psy, psy_next, &power_supply_nodes, list)
  479. {
  480. total++;
  481. }
  482. rt_spin_unlock(&nodes_lock);
  483. if (!total)
  484. {
  485. return RT_NULL;
  486. }
  487. nodes = rt_calloc(total, sizeof(*nodes));
  488. if (!nodes)
  489. {
  490. return RT_NULL;
  491. }
  492. rt_spin_lock(&nodes_lock);
  493. rt_list_for_each_entry_safe(psy, psy_next, &power_supply_nodes, list)
  494. {
  495. nodes[idx] = psy;
  496. rt_ref_get(&psy->ref);
  497. idx++;
  498. }
  499. rt_spin_unlock(&nodes_lock);
  500. *count = total;
  501. return nodes;
  502. }
  503. void rt_power_supply_snapshot_free(struct rt_power_supply **nodes, rt_size_t count)
  504. {
  505. if (!nodes)
  506. {
  507. return;
  508. }
  509. while (count--)
  510. {
  511. rt_ref_put(&nodes[count]->ref, power_supply_release);
  512. }
  513. rt_free(nodes);
  514. }