audio_acts_utils.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2020 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Common utils for in/out audio drivers
  9. */
  10. #ifndef __AUDIO_ACTS_UTILS_H__
  11. #define __AUDIO_ACTS_UTILS_H__
  12. #include <drivers/audio/audio_common.h>
  13. #include <shell/shell_uart.h>
  14. /* config mic input pin as a Universal GUIO, default 0 is disable*/
  15. #define CONFIG_MIC_PIN_GPIO 0
  16. /***************************************************************************************************
  17. * CMU_DACCLK
  18. */
  19. #define CMU_DACCLK_DACFIFO0CLKEN BIT(24)
  20. #define CMU_DACCLK_DACCICFIRCLOCK BIT(21)
  21. #define CMU_DACCLK_DACSDMCLOCK BIT(20)
  22. #define CMU_DACCLK_DACFIR2XCLKDIV_SHIFT (16)
  23. #define CMU_DACCLK_DACFIR2XCLKDIV_MASK (0x3 << CMU_DACCLK_DACFIR2XCLKDIV_SHIFT)
  24. #define CMU_DACCLK_DACFIR2XCLKDIV(x) ((x) << CMU_DACCLK_DACFIR2XCLKDIV_SHIFT)
  25. #define CMU_DACCLK_DACFIRCLKDIV (14)
  26. #define CMU_DACCLK_DACHUMDIV BIT(13)
  27. #define CMU_DACCLK_DACCICCLCKDIV (12)
  28. #define CMU_DACCLK_DACCLKSRC (8)
  29. #define CMU_DACCLK_DACCLKDIV_SHIFT (0)
  30. #define CMU_DACCLK_DACCLKDIV_MASK (0x3 << CMU_DACCLK_DACCLKDIV_SHIFT)
  31. #define CMU_DACCLK_DACCLKDIV(x) ((x) << CMU_DACCLK_DACCLKDIV_SHIFT)
  32. /***************************************************************************************************
  33. * CMU_ADCCLK
  34. */
  35. #define CMU_ADCCLK_ADCDEBUGEN BIT(31)
  36. #define CMU_ADCCLK_ADCSWITCHRC4M BIT(30)
  37. #define CMU_ADCCLK_ADCFIFOCLKEN BIT(24)
  38. #define CMU_ADCCLK_ADCFIREN BIT(23)
  39. #define CMU_ADCCLK_ADCCICEN BIT(22)
  40. #define CMU_ADCCLK_ADCANAEN BIT(21)
  41. #define CMU_ADCCLK_ADCDMICEN BIT(20)
  42. #define CMU_ADCCLK_ADCFIRCLKRVS BIT(19)
  43. #define CMU_ADCCLK_ADCCICCLKRVS BIT(18)
  44. #define CMU_ADCCLK_ADCANACLKRVS BIT(17)
  45. #define CMU_ADCCLK_ADCDMICCLKRVS BIT(16)
  46. #define CMU_ADCCLK_ADCFIRCLKDIV BIT(15)
  47. #define CMU_ADCCLK_ADCOVFSCLKDIV_SHIFT (12)
  48. #define CMU_ADCCLK_ADCOVFSCLKDIV_MASK (0x3 << CMU_ADCCLK_ADCOVFSCLKDIV_SHIFT)
  49. #define CMU_ADCCLK_ADCOVFSCLKDIV(x) ((x) << CMU_ADCCLK_ADCOVFSCLKDIV_SHIFT)
  50. #define CMU_ADCCLK_ADCCLKSRC (8)
  51. #define CMU_ADCCLK_ADCCLKPREDIV (4)
  52. #define CMU_ADCCLK_ADCCLKDIV_SHIFT (0)
  53. #define CMU_ADCCLK_ADCCLKDIV_MASK (0x7 << CMU_ADCCLK_ADCCLKDIV_SHIFT)
  54. #define CMU_ADCCLK_ADCCLKDIV(x) ((x) << CMU_ADCCLK_ADCCLKDIV_SHIFT)
  55. /***************************************************************************************************
  56. * CMU_I2STXCLK
  57. */
  58. #define CMU_I2SG0LRCLOKEN BIT(31)
  59. #define CMU_I2SG0BCLKOEN BIT(30)
  60. #define CMU_I2SG0MCLKOEN BIT(29)
  61. #define CMU_I2STX0CLKSRC BIT(28)
  62. #define CMU_I2STXCLK_I2SG0LRCLKPROC_SHIFT (23)
  63. #define CMU_I2STXCLK_I2SG0LRCLKPROC_MASK (0x7 << CMU_I2STXCLK_I2SG0LRCLKPROC_SHIFT)
  64. #define CMU_I2STXCLK_I2SG0LRCLKPROC(x) ((x) << CMU_I2STXCLK_I2SG0LRCLKPROC_SHIFT)
  65. #define CMU_I2STXCLK_I2SG0LRCLKDIV_SHIFT (20)
  66. #define CMU_I2STXCLK_I2SG0LRCLKDIV_MASK (0x7 << CMU_I2STXCLK_I2SG0LRCLKDIV_SHIFT)
  67. #define CMU_I2STXCLK_I2SG0LRCLKDIV(x) ((x) << CMU_I2STXCLK_I2SG0LRCLKDIV_SHIFT)
  68. #define CMU_I2STXCLK_I2SG0BCLKDIV_SHIFT (18)
  69. #define CMU_I2STXCLK_I2SG0BCLKDIV_MASK (0x3 << CMU_I2STXCLK_I2SG0BCLKDIV_SHIFT)
  70. #define CMU_I2STXCLK_I2SG0BCLKDIV(x) ((x) << CMU_I2STXCLK_I2SG0BCLKDIV_SHIFT)
  71. #define CMU_I2STXCLK_I2SG0BLRCLKSRC BIT(16)
  72. #define CMU_I2STXCLK_I2SG0MCLKEXTREV BIT(14)
  73. #define CMU_I2STXCLK_I2SG0MCLKSRC_SHIFT (12)
  74. #define CMU_I2STXCLK_I2SG0MCLKSRC_MASK (0x3 << CMU_I2STXCLK_I2SG0MCLKSRC_SHIFT)
  75. #define CMU_I2STXCLK_I2SG0MCLKSRC(x) ((x) << CMU_I2STXCLK_I2SG0MCLKSRC_SHIFT)
  76. #define CMU_I2STX0_FIFOCLKEN BIT(11)
  77. #define CMU_I2STXCLK_I2SG0CLKDIV_SHIFT (0)
  78. #define CMU_I2STXCLK_I2SG0CLKDIV_MASK (0xF << CMU_I2STXCLK_I2SG0CLKDIV_SHIFT)
  79. #define CMU_I2STXCLK_I2SG0CLKDIV(x) ((x) << CMU_I2STXCLK_I2SG0CLKDIV_SHIFT)
  80. /***************************************************************************************************
  81. * CMU_I2SRXCLK
  82. */
  83. #define CMU_I2SG1LRCLOKEN BIT(31)
  84. #define CMU_I2SG1BCLKOEN BIT(30)
  85. #define CMU_I2SG1MCLKOEN BIT(29)
  86. #define CMU_I2SRXCLK_I2SRX0CLKSRC BIT(28)
  87. #define CMU_I2SRXCLK_I2SG1LRCLKPROC_SHIFT (23)
  88. #define CMU_I2SRXCLK_I2SG1LRCLKPROC_MASK (0x7 << CMU_I2SRXCLK_I2SG1LRCLKPROC_SHIFT)
  89. #define CMU_I2SRXCLK_I2SG1LRCLKPROC(x) ((x) << CMU_I2SRXCLK_I2SG1LRCLKPROC_SHIFT)
  90. #define CMU_I2SRXCLK_I2SG1LRCLKDIV_SHIFT (20)
  91. #define CMU_I2SRXCLK_I2SG1LRCLKDIV_MASK (0x3 << CMU_I2SRXCLK_I2SG1LRCLKDIV_SHIFT)
  92. #define CMU_I2SRXCLK_I2SG1LRCLKDIV(x) ((x) << CMU_I2SRXCLK_I2SG1LRCLKDIV_SHIFT)
  93. #define CMU_I2SRXCLK_I2SG1BCLKDIV_SHIFT (18)
  94. #define CMU_I2SRXCLK_I2SG1BCLKDIV_MASK (0x3 << CMU_I2SRXCLK_I2SG1BCLKDIV_SHIFT)
  95. #define CMU_I2SRXCLK_I2SG1BCLKDIV(x) ((x) << CMU_I2SRXCLK_I2SG1BCLKDIV_SHIFT)
  96. #define CMU_I2SRXCLK_I2SG1BLRCLKSRC BIT(16)
  97. #define CMU_I2SRXCLK_I2SG1MCLKEXTREV BIT(14)
  98. #define CMU_I2SRXCLK_I2SG1MCLKSRC_SHIFT (12)
  99. #define CMU_I2SRXCLK_I2SG1MCLKSRC_MASK (0x3 << CMU_I2SRXCLK_I2SG1MCLKSRC_SHIFT)
  100. #define CMU_I2SRXCLK_I2SG1MCLKSRC(x) ((x) << CMU_I2SRXCLK_I2SG1MCLKSRC_SHIFT)
  101. #define CMU_I2SRX0_FIFOCLKEN BIT(11)
  102. #define CMU_I2SRXCLK_I2SG1CLKDIV_SHIFT (0)
  103. #define CMU_I2SRXCLK_I2SG1CLKDIV_MASK (0xF << CMU_I2SRXCLK_I2SG1CLKDIV_SHIFT)
  104. #define CMU_I2SRXCLK_I2SG1CLKDIV(x) ((x) << CMU_I2SRXCLK_I2SG1CLKDIV_SHIFT)
  105. /*
  106. * enum a_pll_series_e
  107. * @brief The series of audio pll
  108. */
  109. typedef enum {
  110. AUDIOPLL_44KSR = 0, /* 44.1K sample rate seires */
  111. AUDIOPLL_48KSR /* 48K sample rate series */
  112. } a_pll_series_e;
  113. /*
  114. * enum a_pll_type_e
  115. * @brief The audio pll type selection
  116. */
  117. typedef enum {
  118. AUDIOPLL_TYPE_0 = 0, /* AUDIO_PLL0 */
  119. AUDIOPLL_TYPE_1, /* AUDIO_PLL1 */
  120. } a_pll_type_e;
  121. /*
  122. * enum a_mclk_type_e
  123. * @brief The rate of MCLK in the multiple of sample rate
  124. * @note DAC MCLK is always 256FS, and the I2S MCLK depends on BCLK (MCLK = 4BCLK)
  125. */
  126. typedef enum {
  127. MCLK_128FS = 128,
  128. MCLK_192FS = 192,
  129. MCLK_256FS = 256,
  130. MCLK_384FS = 384,
  131. MCLK_512FS = 512,
  132. MCLK_768FS = 768,
  133. MCLK_1536FS = 1536,
  134. } a_mclk_type_e;
  135. #define AUDIO_DUMP_MEM(data, len) \
  136. { \
  137. uint32_t i; \
  138. for (i = 0; i < len; i += 4) { \
  139. printk("record 0x%08x ", *(uint32_t *)&data[i]); \
  140. if (!((i + 4) % 16)) { \
  141. printk("\n"); \
  142. } \
  143. } \
  144. printk("\n"); \
  145. }
  146. uint32_t audio_sr_khz_to_hz(audio_sr_sel_e sr_khz);
  147. audio_sr_sel_e audio_sr_hz_to_Khz(uint32_t sr_hz);
  148. int audio_get_pll_sample_rate_dac(uint8_t div, uint8_t fir_div,
  149. uint8_t fir2x_div, uint8_t cic_div, a_pll_type_e index);
  150. int audio_get_pll_setting_dac(audio_sr_sel_e sr_khz,uint8_t *div,
  151. uint8_t *fir_div,uint8_t *fir2x_div, uint8_t *cic_div,uint8_t *series);
  152. int audio_get_pll_setting(audio_sr_sel_e sr_khz, a_mclk_type_e mclk,
  153. uint8_t *pre_div, uint8_t *clk_div, uint8_t *series);
  154. int audio_get_pll_setting_i2s(uint16_t sr_khz, a_mclk_type_e mclk,
  155. uint8_t *div, uint8_t *series);
  156. int audio_pll_check_config(a_pll_series_e series, uint8_t *index);
  157. int audio_get_pll_sample_rate(a_mclk_type_e mclk, uint8_t pre_div, uint8_t clk_div, a_pll_type_e index);
  158. int audio_get_pll_sample_rate_i2s(a_mclk_type_e mclk, uint8_t clk_div, a_pll_type_e index);
  159. int audio_pll_get_aps(a_pll_type_e index);
  160. int audio_pll_set_aps(a_pll_type_e index, audio_aps_level_e level);
  161. void audio_pll_set(a_pll_type_e index, a_pll_series_e series);
  162. void audio_pll_unset(a_pll_type_e index);
  163. void adc_reset_control(bool enable);
  164. #endif /* __AUDIO_ACTS_UTILS_H__ */