anc_hal.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <devicetree.h>
  4. #include <string.h>
  5. #include <mem_manager.h>
  6. #include "anc_inner.h"
  7. #include "drivers/anc.h"
  8. #include "os_common_api.h"
  9. #include "drivers/audio/audio_in.h"
  10. #include "drivers/audio/audio_out.h"
  11. #include "soc_regs.h"
  12. #include "anc_hal.h"
  13. #define ANC_DSP_IMAGE "anc_dsp.dsp"
  14. #define ANC_CFG_FF_FILE "anccfgff.bin"
  15. #define ANC_CFG_FB_FILE "anccfgfb.bin"
  16. #define ANC_CFG_TT_FILE "anccfgtt.bin"
  17. int anc_dsp_open(anc_mode_e mode)
  18. {
  19. int ret = 0, i;
  20. char *cfg[2] = {NULL, NULL};
  21. uint32_t *anc_cfg = NULL;
  22. struct device *anc_dev = NULL;
  23. struct device *ain_dev = NULL;
  24. struct device *aout_dev = NULL;
  25. struct anc_imageinfo image;
  26. adc_anc_ctl_t adc_anc_ctl = {0};
  27. dac_anc_ctl_t dac_anc_ctl = {0};
  28. anc_dev = device_get_binding(CONFIG_ANC_NAME);
  29. ain_dev = device_get_binding(CONFIG_AUDIO_IN_ACTS_DEV_NAME);
  30. aout_dev = device_get_binding(CONFIG_AUDIO_OUT_ACTS_DEV_NAME);
  31. if(!aout_dev || !ain_dev || !anc_dev)
  32. {
  33. SYS_LOG_ERR("get device failed\n");
  34. return -1;
  35. }
  36. if(anc_get_status(anc_dev) != ANC_STATUS_POWEROFF)
  37. {
  38. SYS_LOG_ERR("anc already open\n");
  39. return -1;
  40. }
  41. memset(&image, 0, sizeof(image));
  42. if(sd_fmap(ANC_DSP_IMAGE, (void **)&image.ptr, (int *)&image.size))
  43. {
  44. SYS_LOG_ERR("cannot find anc image \"%s\"", ANC_DSP_IMAGE);
  45. return -1;
  46. }
  47. image.name = ANC_DSP_IMAGE;
  48. ret = anc_request_image(anc_dev, &image);
  49. if(ret)
  50. {
  51. SYS_LOG_ERR("anc request image err\n");
  52. return -1;
  53. }
  54. adc_anc_ctl.is_open_anc = 1;
  55. audio_in_control(ain_dev, NULL, AIN_CMD_ANC_CONTROL, &adc_anc_ctl);
  56. dac_anc_ctl.is_open_anc = 1;
  57. audio_out_control(aout_dev, NULL, AOUT_CMD_ANC_CONTROL, &dac_anc_ctl);
  58. ret = anc_poweron(anc_dev);
  59. if(ret)
  60. {
  61. SYS_LOG_ERR("anc power on err\n");
  62. goto err;
  63. }
  64. if(mode == ANC_MODE_ANC){
  65. cfg[0] = ANC_CFG_FF_FILE;
  66. cfg[1] = ANC_CFG_FB_FILE;
  67. SYS_LOG_INF("ANC MODE");
  68. }
  69. else{
  70. SYS_LOG_INF("TRANSPARENT MODE");
  71. cfg[0] = ANC_CFG_TT_FILE;
  72. }
  73. for(i=0; i<2; i++){
  74. if(cfg[i]){
  75. if(sd_fmap(cfg[i], (void **)&anc_cfg, NULL))
  76. {
  77. SYS_LOG_ERR("cannot find anc cfg file \"%s\"", cfg[i]);
  78. return -1;
  79. }
  80. if(anc_send_command(anc_dev, ANC_COMMAND_ANCTDATA, anc_cfg+1, *anc_cfg))
  81. {
  82. SYS_LOG_ERR("config anc err\n");
  83. goto err;
  84. }
  85. }
  86. }
  87. return 0;
  88. err:
  89. anc_release_image(anc_dev);
  90. return -1;
  91. }
  92. int anc_dsp_close(void)
  93. {
  94. struct device *anc_dev = NULL;
  95. struct device *ain_dev = NULL;
  96. struct device *aout_dev = NULL;
  97. adc_anc_ctl_t adc_anc_ctl = {0};
  98. dac_anc_ctl_t dac_anc_ctl = {0};
  99. anc_dev = device_get_binding(CONFIG_ANC_NAME);
  100. ain_dev = device_get_binding(CONFIG_AUDIO_IN_ACTS_DEV_NAME);
  101. aout_dev = device_get_binding(CONFIG_AUDIO_OUT_ACTS_DEV_NAME);
  102. if(!aout_dev || !ain_dev || !anc_dev)
  103. {
  104. SYS_LOG_ERR("get device failed\n");
  105. return -1;
  106. }
  107. if(anc_get_status(anc_dev) == ANC_STATUS_POWEROFF)
  108. {
  109. SYS_LOG_ERR("anc already close");
  110. return 0;
  111. }
  112. anc_send_command(anc_dev, ANC_COMMAND_POWEROFF, NULL, 0);
  113. /*wait dsp close*/
  114. os_sleep(5);
  115. anc_poweroff(anc_dev);
  116. anc_release_image(anc_dev);
  117. adc_anc_ctl.is_open_anc = 0;
  118. audio_in_control(ain_dev, NULL, AIN_CMD_ANC_CONTROL, &adc_anc_ctl);
  119. dac_anc_ctl.is_open_anc = 0;
  120. audio_out_control(aout_dev, NULL, AOUT_CMD_ANC_CONTROL, &dac_anc_ctl);
  121. return 0;
  122. }
  123. int anc_dsp_send_anct_data(void *data, int size)
  124. {
  125. struct device *anc_dev = NULL;
  126. anc_dev = device_get_binding(CONFIG_ANC_NAME);
  127. if(anc_dev == NULL)
  128. {
  129. SYS_LOG_ERR("get anc device failed\n");
  130. return -1;
  131. }
  132. if(anc_get_status(anc_dev) != ANC_STATUS_POWERON)
  133. {
  134. SYS_LOG_ERR("anc not open");
  135. return 0;
  136. }
  137. return anc_send_command(anc_dev, ANC_COMMAND_ANCTDATA, data, size);
  138. }
  139. int anc_dsp_dump_data(int start, uint32_t ringbuf_addr)
  140. {
  141. struct device *anc_dev = NULL;
  142. anc_dev = device_get_binding(CONFIG_ANC_NAME);
  143. if(anc_dev == NULL)
  144. {
  145. SYS_LOG_ERR("get anc device failed\n");
  146. return -1;
  147. }
  148. if(anc_get_status(anc_dev) != ANC_STATUS_POWERON)
  149. {
  150. SYS_LOG_ERR("anc not open");
  151. return 0;
  152. }
  153. if(start){
  154. return anc_send_command(anc_dev, ANC_COMMAND_DUMPSTART, &ringbuf_addr, 4);
  155. }
  156. else{
  157. return anc_send_command(anc_dev, ANC_COMMAND_DUMPSTOP, NULL, 0);
  158. }
  159. }
  160. int anc_dsp_samplerate_notify(void)
  161. {
  162. return 0;
  163. }