hotplug_charger.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file hotplug 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 "hotpug_manager"
  15. #endif
  16. #include <os_common_api.h>
  17. #include <msg_manager.h>
  18. #include <string.h>
  19. #include <hotplug_manager.h>
  20. #include <soc_pmu.h>
  21. struct charger_detect_state_t {
  22. uint8_t prev_state;
  23. uint8_t stable_state;
  24. uint8_t detect_cnt;
  25. };
  26. static struct charger_detect_state_t charger_detect_state;
  27. int _charger_get_state(void)
  28. {
  29. int state;
  30. if (soc_pmu_get_dc5v_status()) {
  31. state = HOTPLUG_IN;
  32. } else {
  33. state = HOTPLUG_OUT;
  34. }
  35. return state;
  36. }
  37. int _charger_hotplug_detect(void)
  38. {
  39. int report_state = HOTPLUG_NONE;
  40. int state = HOTPLUG_NONE;
  41. state = _charger_get_state();
  42. if (state <= HOTPLUG_NONE) {
  43. goto exit;
  44. }
  45. if (state == charger_detect_state.prev_state) {
  46. charger_detect_state.detect_cnt++;
  47. if (charger_detect_state.detect_cnt >= 1) {
  48. charger_detect_state.detect_cnt = 0;
  49. if (state != charger_detect_state.stable_state) {
  50. charger_detect_state.stable_state = state;
  51. report_state = charger_detect_state.stable_state;
  52. }
  53. }
  54. } else {
  55. charger_detect_state.detect_cnt = 0;
  56. charger_detect_state.prev_state = state;
  57. }
  58. exit:
  59. return report_state;
  60. }
  61. static const struct hotplug_device_t charger_hotplug_device = {
  62. .type = HOTPLUG_CHARGER,
  63. .get_state = _charger_get_state,
  64. .hotplug_detect = _charger_hotplug_detect,
  65. };
  66. int hotplug_charger_init(void)
  67. {
  68. memset(&charger_detect_state, 0, sizeof(struct charger_detect_state_t));
  69. if (soc_pmu_get_dc5v_status()) {
  70. charger_detect_state.stable_state = HOTPLUG_IN;
  71. } else {
  72. charger_detect_state.stable_state = HOTPLUG_NONE;
  73. }
  74. return hotplug_device_register(&charger_hotplug_device);
  75. }