power_manager.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file power manager interface
  8. */
  9. #if defined(CONFIG_SYS_LOG)
  10. #define SYS_LOG_NO_NEWLINE
  11. #ifdef SYS_LOG_DOMAIN
  12. #undef SYS_LOG_DOMAIN
  13. #endif
  14. #define SYS_LOG_DOMAIN "power"
  15. #endif
  16. #include <os_common_api.h>
  17. #include <mem_manager.h>
  18. #include <msg_manager.h>
  19. #include <sys_event.h>
  20. #include <sys_monitor.h>
  21. #include <property_manager.h>
  22. #ifndef CONFIG_SIMULATOR
  23. #include <drivers/power_supply.h>
  24. #endif
  25. #ifdef CONFIG_BLUETOOTH
  26. #include <bt_manager.h>
  27. #endif
  28. #ifdef CONFIG_SYS_WAKELOCK
  29. #include <sys_wakelock.h>
  30. #endif
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #define DEFAULT_BOOTPOWER_LEVEL 3100000
  34. #define DEFAULT_NOPOWER_LEVEL 3400000
  35. #define DEFAULT_LOWPOWER_LEVEL 3600000
  36. #define DEFAULT_REPORT_PERIODS (60*1000)
  37. struct power_manager_info {
  38. struct device *dev;
  39. bat_charge_callback_t cb;
  40. bat_charge_event_t event;
  41. bat_charge_event_para_t *para;
  42. int nopower_level;
  43. int lowpower_level;
  44. int current_vol;
  45. int current_cap;
  46. int slave_vol;
  47. int slave_cap;
  48. int battary_changed;
  49. uint32_t charge_full_flag;
  50. uint32_t report_timestamp;
  51. uint32_t wakelock_flag;
  52. };
  53. struct power_manager_info *power_manager = NULL;
  54. void power_supply_report(bat_charge_event_t event, bat_charge_event_para_t *para)
  55. {
  56. struct app_msg msg = {0};
  57. if (!power_manager) {
  58. return;
  59. }
  60. SYS_LOG_INF("event %d\n", event);
  61. switch (event) {
  62. case BAT_CHG_EVENT_DC5V_IN:
  63. break;
  64. case BAT_CHG_EVENT_DC5V_OUT:
  65. break;
  66. case BAT_CHG_EVENT_CHARGE_START:
  67. break;
  68. case BAT_CHG_EVENT_CHARGE_FULL:
  69. power_manager->charge_full_flag = 1;
  70. break;
  71. case BAT_CHG_EVENT_VOLTAGE_CHANGE:
  72. SYS_LOG_INF("voltage change %u uV\n", para->voltage_val);
  73. power_manager->current_vol = para->voltage_val;
  74. break;
  75. case BAT_CHG_EVENT_CAP_CHANGE:
  76. SYS_LOG_INF("cap change %u\n", para->cap);
  77. power_manager->current_cap = para->cap;
  78. power_manager->battary_changed = 1;
  79. break;
  80. case BAT_CHG_EVENT_EXIT_MINI_CHARGE:
  81. break;
  82. default:
  83. break;
  84. }
  85. msg.type = MSG_BAT_CHARGE_EVENT;
  86. msg.cmd = event;
  87. send_async_msg("main", &msg);
  88. }
  89. static int get_system_bat_info(int property)
  90. {
  91. union power_supply_propval val;
  92. int ret;
  93. if (!power_manager || !power_manager->dev) {
  94. SYS_LOG_ERR("dev not found\n");
  95. return -ENODEV;
  96. }
  97. ret = power_supply_get_property(power_manager->dev, property, &val);
  98. if (ret < 0) {
  99. SYS_LOG_ERR("get property err %d\n", ret);
  100. return -ENODEV;
  101. }
  102. return val.intval;
  103. }
  104. static int set_system_bat_info(int property, union power_supply_propval *val)
  105. {
  106. if (!power_manager || !power_manager->dev) {
  107. SYS_LOG_ERR("dev not found\n");
  108. return -ENODEV;
  109. }
  110. power_supply_set_property(power_manager->dev, property, val);
  111. return 0;
  112. }
  113. int power_manager_get_battery_capacity(void)
  114. {
  115. #ifdef CONFIG_TWS
  116. int report_cap = get_system_bat_info(POWER_SUPPLY_PROP_CAPACITY);
  117. if (bt_manager_tws_get_dev_role() == BTSRV_TWS_MASTER) {
  118. if (report_cap < power_manager->slave_cap)
  119. report_cap = power_manager->slave_cap;
  120. }
  121. return report_cap;
  122. #else
  123. return get_system_bat_info(POWER_SUPPLY_PROP_CAPACITY);
  124. #endif
  125. }
  126. int power_manager_get_battery_capacity_local(void)
  127. {
  128. return get_system_bat_info(POWER_SUPPLY_PROP_CAPACITY);
  129. }
  130. int power_manager_get_charge_status(void)
  131. {
  132. return get_system_bat_info(POWER_SUPPLY_PROP_STATUS);
  133. }
  134. int power_manager_get_battery_vol(void)
  135. {
  136. return get_system_bat_info(POWER_SUPPLY_PROP_VOLTAGE_NOW);
  137. }
  138. int power_manager_get_dc5v_status(void)
  139. {
  140. return get_system_bat_info(POWER_SUPPLY_PROP_DC5V);
  141. }
  142. int power_manager_get_dc5v_status_chargebox(void)
  143. {
  144. return get_system_bat_info(POWER_SUPPLY_PROP_DC5V_STATUS);
  145. }
  146. int power_manager_get_dc5v_voltage(void)
  147. {
  148. return get_system_bat_info(POWER_SUPPLY_PROP_DC5V_VOLTAGE);
  149. }
  150. int power_manger_set_dc5v_pulldown(void)
  151. {
  152. return set_system_bat_info(POWER_SUPPLY_SET_DC5V_PULLDOWM, NULL);
  153. }
  154. int power_manger_set_init_charge_consume_ma(int consume)
  155. {
  156. union power_supply_propval val;
  157. val.intval = consume;
  158. SYS_LOG_INF("setting init charge consume ma %d\n", consume);
  159. return set_system_bat_info(POWER_SUPPLY_SET_CONSUME_MA, &val);
  160. }
  161. int power_manger_wake_charger_box(void)
  162. {
  163. int ret;
  164. union power_supply_propval val;
  165. ret = set_system_bat_info(POWER_SUPPLY_WAKE_CHARGER_BOX, &val);
  166. if (ret < 0)
  167. {
  168. return ret;
  169. }
  170. return val.intval;
  171. }
  172. int power_manager_set_slave_battery_state(int capacity, int vol)
  173. {
  174. power_manager->slave_vol = vol;
  175. power_manager->slave_cap = capacity;
  176. power_manager->battary_changed = 1;
  177. SYS_LOG_INF("vol %dmv cap %d\n", vol, capacity);
  178. return 0;
  179. }
  180. bool power_manager_check_is_no_power(void)
  181. {
  182. if (power_manager_get_dc5v_status())
  183. return false;
  184. if (power_manager_get_battery_vol() <= power_manager->nopower_level) {
  185. SYS_LOG_INF("%d %d too low", power_manager_get_battery_vol(), power_manager_get_battery_capacity_local());
  186. return true;
  187. }
  188. return false;
  189. }
  190. bool power_manager_check_bat_is_lowpower(void)
  191. {
  192. int state = get_system_bat_info(POWER_SUPPLY_PROP_STATUS);
  193. if(state == POWER_SUPPLY_STATUS_BAT_LOWPOWER)
  194. return true;
  195. return false;
  196. }
  197. bool power_manager_check_bat_is_full(void)
  198. {
  199. int state = get_system_bat_info(POWER_SUPPLY_PROP_STATUS);
  200. if(state == POWER_SUPPLY_STATUS_FULL)
  201. return true;
  202. return false;
  203. }
  204. bool power_manager_check_bat_exit(void)
  205. {
  206. int state = get_system_bat_info(POWER_SUPPLY_PROP_STATUS);
  207. if(state == POWER_SUPPLY_STATUS_BAT_NOTEXIST)
  208. return false;
  209. return true;
  210. }
  211. int power_manager_set_before_poweroff(void)
  212. {
  213. if (!power_manager || !power_manager->dev) {
  214. SYS_LOG_ERR("dev not found\n");
  215. return -ENODEV;
  216. }
  217. power_supply_set_property(power_manager->dev, POWER_SUPPLY_SET_BEFORE_ENTER_S4, NULL);
  218. return 0;
  219. }
  220. int power_manager_sync_slave_battery_state(void)
  221. {
  222. uint32_t send_value;
  223. #ifdef CONFIG_BT_TWS_US281B
  224. send_value = power_manager->current_cap / 10;
  225. #else
  226. send_value = (power_manager->current_cap << 24) | power_manager->current_vol;
  227. #endif
  228. #ifdef CONFIG_TWS
  229. bt_manager_tws_send_event(TWS_BATTERY_EVENT, send_value);
  230. #endif
  231. return 0;
  232. }
  233. static int _power_manager_work_handle(void)
  234. {
  235. if (!power_manager)
  236. return -ESRCH;
  237. if (power_manager->battary_changed) {
  238. power_manager->battary_changed = 0;
  239. #ifdef CONFIG_BT_HFP_HF
  240. #ifdef CONFIG_TWS
  241. if (bt_manager_tws_get_dev_role() == BTSRV_TWS_SLAVE) {
  242. power_manager_sync_slave_battery_state();
  243. } else if (bt_manager_tws_get_dev_role() == BTSRV_TWS_MASTER) {
  244. int report_cap = power_manager->current_cap;
  245. if (report_cap < power_manager->slave_cap)
  246. report_cap = power_manager->slave_cap;
  247. bt_manager_hfp_battery_report(BT_BATTERY_REPORT_VAL, report_cap);
  248. } else {
  249. bt_manager_hfp_battery_report(BT_BATTERY_REPORT_VAL, power_manager->current_cap);
  250. }
  251. #else
  252. bt_manager_hfp_battery_report(BT_BATTERY_REPORT_VAL, power_manager->current_cap);
  253. #endif
  254. #endif
  255. }
  256. if (power_manager_get_dc5v_status()) {
  257. if(power_manager->wakelock_flag == 0) {
  258. #ifdef CONFIG_SYS_WAKELOCK
  259. sys_wake_lock_ext(PARTIAL_WAKE_LOCK,POWER_WAKE_LOCK_USER);
  260. #endif
  261. power_manager->wakelock_flag = 1;
  262. SYS_LOG_INF("dc5v in, wake lock!");
  263. }
  264. return 0;
  265. } else {
  266. if(power_manager->wakelock_flag != 0) {
  267. #ifdef CONFIG_SYS_WAKELOCK
  268. sys_wake_unlock_ext(PARTIAL_WAKE_LOCK,POWER_WAKE_LOCK_USER);
  269. #endif
  270. power_manager->wakelock_flag = 0;
  271. SYS_LOG_INF("dc5v out, wake unlock!");
  272. }
  273. }
  274. if (power_manager->charge_full_flag) {
  275. power_manager->charge_full_flag = 0;
  276. sys_event_notify(SYS_EVENT_CHARGE_FULL);
  277. }
  278. if ((power_manager->current_vol <= power_manager->lowpower_level)
  279. && ((os_uptime_get_32() - power_manager->report_timestamp) >= DEFAULT_REPORT_PERIODS
  280. || !power_manager->report_timestamp)) {
  281. SYS_LOG_INF("%d %d low", power_manager->current_vol, power_manager->lowpower_level);
  282. #ifdef CONFIG_TWS
  283. if (bt_manager_tws_get_dev_role() != BTSRV_TWS_SLAVE) {
  284. sys_event_notify(SYS_EVENT_BATTERY_LOW);
  285. }
  286. #else
  287. sys_event_notify(SYS_EVENT_BATTERY_LOW);
  288. #endif
  289. power_manager->report_timestamp = os_uptime_get_32();
  290. }
  291. if (power_manager->current_vol <= power_manager->nopower_level) {
  292. SYS_LOG_INF("%d %d too low", power_manager->current_vol, power_manager->nopower_level);
  293. sys_event_notify(SYS_EVENT_BATTERY_TOO_LOW);
  294. return 0;
  295. }
  296. return 0;
  297. }
  298. int power_manager_save_last_voltage(bool is_clear)
  299. {
  300. char temp[6];
  301. uint16_t bat_vol_saved = power_manager_get_battery_vol()/1000;
  302. if(is_clear) {
  303. bat_vol_saved = 0;
  304. }
  305. snprintf(temp, sizeof(temp), "%d", bat_vol_saved);
  306. SYS_LOG_INF("last vol_s: %s", temp);
  307. #ifdef CONFIG_PROPERTY
  308. property_set(CFG_BAT_LAST_VOLTAGE, temp, strlen(temp) + 1);
  309. property_flush(CFG_BAT_LAST_VOLTAGE);
  310. return 0;
  311. #else
  312. return 0;
  313. #endif
  314. }
  315. static int power_supply_set_init_vol(void)
  316. {
  317. //uint32_t bat_vol_saved_flag = 0;
  318. if (!power_manager || !power_manager->dev) {
  319. SYS_LOG_ERR("dev not found\n");
  320. return -ENODEV;
  321. }
  322. SYS_LOG_INF("%s: %d", __func__, __LINE__);
  323. #if 0
  324. soc_pstore_get(SOC_PSTORE_TAG_FLAG_CAP, &bat_vol_saved_flag);
  325. if(bat_vol_saved_flag != 0) {
  326. uint32_t bat_vol_saved = 0;
  327. union power_supply_propval val;
  328. soc_pstore_get(SOC_PSTORE_TAG_CAPACITY, &bat_vol_saved);
  329. val.intval = bat_vol_saved;
  330. power_supply_set_property(power_manager->dev, POWER_SUPPLY_SET_INIT_VOL, &val);
  331. SYS_LOG_INF("bat init vol from pstore: %dmv\n", bat_vol_saved);
  332. soc_pstore_set(SOC_PSTORE_TAG_FLAG_CAP, 0);
  333. }
  334. #else
  335. #ifdef CONFIG_PROPERTY
  336. {
  337. char temp[8];
  338. uint16_t bat_vol_saved = 0;
  339. union power_supply_propval val;
  340. memset(temp, 0, sizeof(temp));
  341. int ret = property_get(CFG_BAT_LAST_VOLTAGE, temp, sizeof(temp));
  342. if (ret >= 0) {
  343. bat_vol_saved = atoi(temp);
  344. SYS_LOG_INF("%s, bat_vol: %dmv\n", __func__, bat_vol_saved);
  345. }
  346. if ((bat_vol_saved > 1000) && (bat_vol_saved < 5000)) {
  347. val.intval = bat_vol_saved;
  348. power_supply_set_property(power_manager->dev, POWER_SUPPLY_SET_INIT_VOL, &val);
  349. SYS_LOG_INF("bat init vol from nvram: %dmv\n", bat_vol_saved);
  350. power_manager_save_last_voltage(1);
  351. }
  352. }
  353. #endif
  354. #endif
  355. return 0;
  356. }
  357. static struct power_manager_info global_power_manager;
  358. int power_manager_init(void)
  359. {
  360. power_manager = &global_power_manager;
  361. memset(power_manager, 0, sizeof(struct power_manager_info));
  362. power_manager->dev = (struct device *)device_get_binding(CONFIG_ACTS_BATTERY_DEV_NAME);
  363. if (!power_manager->dev) {
  364. SYS_LOG_ERR("dev not found\n");
  365. return -ENODEV;
  366. }
  367. #if 0
  368. if ((power_manager_get_battery_vol() <= DEFAULT_BOOTPOWER_LEVEL)
  369. && (!power_manager_get_dc5v_status())) {
  370. SYS_LOG_INF("no power ,shundown: %d\n", power_manager_get_battery_vol());
  371. sys_pm_poweroff();
  372. return 0;
  373. }
  374. #endif
  375. power_supply_register_notify(power_manager->dev, power_supply_report);
  376. power_supply_set_init_vol();
  377. power_supply_enable(power_manager->dev);
  378. #ifdef CONFIG_PROPERTY
  379. power_manager->lowpower_level = property_get_int(CFG_LOW_POWER_WARNING_LEVEL, DEFAULT_LOWPOWER_LEVEL);
  380. power_manager->nopower_level = property_get_int(CFG_SHUTDOWN_POWER_LEVEL, DEFAULT_NOPOWER_LEVEL);
  381. #else
  382. power_manager->lowpower_level = DEFAULT_LOWPOWER_LEVEL;
  383. power_manager->nopower_level = DEFAULT_NOPOWER_LEVEL;
  384. #endif
  385. power_manager->current_vol = power_manager_get_battery_vol();
  386. power_manager->current_cap = power_manager_get_battery_capacity_local();
  387. power_manager->slave_vol = 4200000;
  388. power_manager->slave_cap = 100;
  389. power_manager->report_timestamp = 0;
  390. sys_monitor_add_work(_power_manager_work_handle);
  391. return 0;
  392. }