hpm_wm8978.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2024 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef _HPM_W8978_H_
  8. #define _HPM_W8978_H_
  9. #include "hpm_i2c_drv.h"
  10. #include "hpm_wm8978_regs.h"
  11. typedef enum wm8978_audio_interface {
  12. wm8978_right_justified = 0, /* Right Justified */
  13. wm8978_left_justified, /* Left Justified */
  14. wm8978_philips_i2s, /* I2S format */
  15. wm8978_pcm_mode, /* DSP/PCM mode */
  16. } wm8978_audio_interface_t;
  17. typedef enum wm8978_word_length {
  18. wm8978_16bits_length = 0, /* 16bits */
  19. wm8978_20bits_length, /* 20bits */
  20. wm8978_24bits_length, /* 24bits */
  21. wm8978_32bits_length, /* 32bits */
  22. } wm8978_word_length_t;
  23. typedef enum wm8978_out_channel {
  24. wm8978_out1_channel = 0, /* R/LOUT1 channel. */
  25. wm8978_out2_channel = 1, /* R/LOUT2 channel. */
  26. } wm8978_out_channel_t;
  27. typedef enum input_channel_flag {
  28. input_off = 0x00, /* no input */
  29. mic_left_on = 0x01, /* LIN,LIP pin,MIC left input */
  30. mic_right_on = 0x02, /* RIN,RIP pin,MIC right input */
  31. line_on = 0x04, /* L2,R2 pin input */
  32. aux_on = 0x08, /* AUXL,AUXR pins input */
  33. dac_on = 0x10, /* dac for i2s */
  34. adc_on = 0x20 /* input fed into the WM8978 internal ADC */
  35. } input_channel_flags_t;
  36. /* WM8978 音频输出通道控制选项, 可以选择多路 */
  37. typedef enum output_channel_flag {
  38. output_off = 0x00, /* no output */
  39. earphone_left_on = 0x01, /* LOUT1 earphone */
  40. earphone_right_on = 0x02, /* ROUT1 earphone */
  41. spk_on = 0x04, /* LOUT2 and ROUT2 Inverting output mono*/
  42. out_3_4_on = 0x08, /* OUT3 and OUT4 output mono audio*/
  43. } output_channel_flag_t;
  44. typedef struct {
  45. I2C_Type *ptr; /* I2C bus */
  46. uint8_t device_address; /* code device address */
  47. } wm8978_context_t;
  48. #if defined(__cplusplus)
  49. extern "C" {
  50. #endif
  51. /**
  52. * @brief WM8979 initialize function.
  53. *
  54. * @param [in] control WM8979 control structure.
  55. * @retval hpm_stat_t status_success if init without any error
  56. */
  57. hpm_stat_t wm8979_init(wm8978_context_t *control);
  58. /**
  59. * @brief WM8979 set out volume function.
  60. *
  61. * @param [in] control WM8979 control structure.
  62. * @param [in] channel out channel
  63. * @param [in] volume volume value
  64. * @retval hpm_stat_t status_success if set without any error
  65. */
  66. hpm_stat_t wm8978_set_out_volume(wm8978_context_t *control, wm8978_out_channel_t channel, uint8_t volume);
  67. /**
  68. * @brief WM8979 read out volume function.
  69. *
  70. * @param [in] control WM8979 control structure.
  71. * @param [in] channel out channel
  72. * @param [out] volume volume points value
  73. * @retval hpm_stat_t status_success if set without any error
  74. */
  75. hpm_stat_t wm8978_get_out_volume(wm8978_context_t *control, wm8978_out_channel_t channel, uint8_t *volume);
  76. /**
  77. * @brief WM8979 set out mute.
  78. *
  79. * @param [in] control WM8979 control structure.
  80. * @param [in] channel out channel
  81. * @param [in] mute if mute is set to true
  82. * @retval hpm_stat_t status_success if set without any error
  83. */
  84. hpm_stat_t wm8978_set_out_mute(wm8978_context_t *control, wm8978_out_channel_t channel, bool mute);
  85. /**
  86. * @brief WM8979 set gain of mic.
  87. *
  88. * @param [in] control WM8979 control structure.
  89. * @param [in] gain gain value, range: 0 ~ 63
  90. * @retval hpm_stat_t status_success if set without any error
  91. */
  92. hpm_stat_t wm8978_set_mic_gain(wm8978_context_t *control, uint8_t gain);
  93. /**
  94. * @brief WM8979 set the gain of the Line input channel
  95. *
  96. * @param [in] control WM8979 control structure.
  97. * @param [in] gain volume value, range: 0 ~ 7
  98. * @retval hpm_stat_t status_success if set without any error
  99. */
  100. hpm_stat_t wm8978_set_line_gain(wm8978_context_t *control, uint8_t gain);
  101. /**
  102. * @brief WM8979 enter power down mode
  103. *
  104. * @param [in] control WM8979 control structure.
  105. * @retval hpm_stat_t status_success if set without any error
  106. */
  107. hpm_stat_t wm8978_power_down(wm8978_context_t *control);
  108. /**
  109. * @brief Control the GPIO1 pin of WM8978 to output high or low
  110. *
  111. * @param [in] control WM8979 control structure.
  112. * @param [in] value output high or low. if true, it's high
  113. * @retval hpm_stat_t status_success if set without any error
  114. */
  115. hpm_stat_t wm8978_ctrl_gpio1(wm8978_context_t *control, bool value);
  116. /**
  117. * @brief Configuring the audio interface of WM8978
  118. *
  119. * @param [in] control WM8979 control structure.
  120. * @param [in] standard wm8978_audio_interface_t structure
  121. * @param [in] word_len wm8978_word_length_t structure
  122. * @retval hpm_stat_t status_success if set without any error
  123. */
  124. hpm_stat_t wm8978_cfg_audio_interface(wm8978_context_t *control,
  125. wm8978_audio_interface_t standard,
  126. wm8978_word_length_t word_len);
  127. /**
  128. * @brief Configure wm8978 audio channel
  129. *
  130. * @param [in] control WM8979 control structure.
  131. * @param [in] in_flags input_channel_flags_t structure
  132. * @param [in] out_flags output_channel_flag_t structure
  133. * @retval hpm_stat_t status_success if set without any error
  134. */
  135. hpm_stat_t wm8978_cfg_audio_channel(wm8978_context_t *control,
  136. input_channel_flags_t in_flags,
  137. output_channel_flag_t out_flags);
  138. /**
  139. * @brief setting the Notch Filter for WM8978
  140. *
  141. * @note used to suppress positive feedback of microphone sound waves to avoid howling
  142. * @param [in] control WM8979 control structure.
  143. * @param [in] nfa0 Notch Filter0 value
  144. * @param [in] nfa1 Notch Filter1 value
  145. * @retval hpm_stat_t status_success if set without any error
  146. */
  147. hpm_stat_t wm8978_notch_filter(wm8978_context_t *control, uint16_t nfa0, uint16_t nfa1);
  148. /**
  149. * @brief Write register to WM8978 using I2C.
  150. *
  151. * @param [in] control WM8978 control structure.
  152. * @retval hpm_stat_t status_success if reset without any error
  153. */
  154. hpm_stat_t wm8978_reset(wm8978_context_t *control);
  155. /**
  156. * @brief Write register to WM8978 using I2C.
  157. *
  158. * @param [in] control WM8978 control structure.
  159. * @param [in] reg The register address in WM8978.
  160. * @param [in] val Value needs to write into the register.
  161. * @retval hpm_stat_t status_success if write reg without any error
  162. */
  163. hpm_stat_t wm8978_write_reg(wm8978_context_t *control, uint8_t reg, uint16_t val);
  164. /**
  165. * @brief Read register from WM8978 using I2C.
  166. * @param [in] reg The register address in WM8978.
  167. * @param [in] reg The register address in WM8978.
  168. * @param [out] val Value point read to.
  169. * @retval hpm_stat_t status_success if read reg without any error
  170. */
  171. hpm_stat_t wm8978_read_reg(wm8978_context_t *control, uint8_t reg, uint16_t *val);
  172. /**
  173. * @brief Modify some bits in the register using I2C.
  174. * @param [in] control WM8978 control structure.
  175. * @param [in] reg The register address in WM8978.
  176. * @param [in] mask The mask code for the bits want to write. The bit you want to write should be 0.
  177. * @param [in] val Value needs to write into the register.
  178. * @retval hpm_stat_t status_success if modify reg without any error
  179. */
  180. hpm_stat_t wm8978_modify_reg(wm8978_context_t *control, uint8_t reg, uint16_t mask, uint16_t val);
  181. #ifdef __cplusplus
  182. }
  183. #endif
  184. /**
  185. * @}
  186. *
  187. */
  188. #endif /* HPM_WM8978_H */