hotplug_linein.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #define SYS_LOG_NO_NEWLINE
  10. #ifdef SYS_LOG_DOMAIN
  11. #undef SYS_LOG_DOMAIN
  12. #endif
  13. #define SYS_LOG_DOMAIN "hotpug_manager"
  14. #include <os_common_api.h>
  15. #include <msg_manager.h>
  16. #include <string.h>
  17. #include <hotplug_manager.h>
  18. struct linein_detect_state_t {
  19. uint8_t prev_state;
  20. uint8_t stable_state;
  21. uint8_t detect_cnt;
  22. };
  23. static struct linein_detect_state_t linein_detect_state;
  24. int _linein_get_state(void)
  25. {
  26. struct device *dev;
  27. int state;
  28. int ret;
  29. dev = device_get_binding("linein_detect");
  30. if (!dev) {
  31. SYS_LOG_ERR("dev not found\n");
  32. state = HOTPLUG_NONE;
  33. goto exit;
  34. }
  35. ret = hotplog_detect_state(dev, &state);
  36. if (ret < 0) {
  37. SYS_LOG_ERR("state error %d\n", ret);
  38. state = HOTPLUG_NONE;
  39. goto exit;
  40. }
  41. exit:
  42. return state;
  43. }
  44. int _linein_hotplug_detect(void)
  45. {
  46. int report_state = HOTPLUG_NONE;
  47. int state = HOTPLUG_NONE;
  48. state = _linein_get_state();
  49. if (state <= HOTPLUG_NONE) {
  50. goto exit;
  51. }
  52. if (state == linein_detect_state.prev_state) {
  53. linein_detect_state.detect_cnt++;
  54. if (linein_detect_state.detect_cnt >= 3) {
  55. linein_detect_state.detect_cnt = 0;
  56. if (state != linein_detect_state.stable_state) {
  57. linein_detect_state.stable_state = state;
  58. report_state = linein_detect_state.stable_state;
  59. }
  60. }
  61. } else {
  62. linein_detect_state.detect_cnt = 0;
  63. linein_detect_state.prev_state = state;
  64. }
  65. exit:
  66. return report_state;
  67. }
  68. static const struct hotplug_device_t linein_hotplug_device = {
  69. .type = HOTPLUG_LINEIN,
  70. .get_state = _linein_get_state,
  71. .hotplug_detect = _linein_hotplug_detect,
  72. };
  73. int hotplug_linein_init(void)
  74. {
  75. struct device *dev;
  76. int state;
  77. int ret;
  78. dev = device_get_binding("linein_detect");
  79. if (!dev) {
  80. SYS_LOG_ERR("dev not found\n");
  81. state = HOTPLUG_NONE;
  82. return -ENODEV;
  83. }
  84. ret = hotplog_detect_state(dev, &state);
  85. if (ret < 0) {
  86. SYS_LOG_ERR("state error %d\n", ret);
  87. state = HOTPLUG_NONE;
  88. return -ENODEV;
  89. }
  90. memset(&linein_detect_state, 0, sizeof(struct linein_detect_state_t));
  91. if (state == LINEIN_IN) {
  92. linein_detect_state.stable_state = HOTPLUG_IN;
  93. } else if (state == LINEIN_OUT) {
  94. linein_detect_state.stable_state = HOTPLUG_OUT;
  95. } else {
  96. linein_detect_state.stable_state = HOTPLUG_NONE;
  97. }
  98. return hotplug_device_register(&linein_hotplug_device);
  99. }