hpm_wm8960.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2021 NXP
  4. * Copyright (c) 2022 HPMicro
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. *
  8. */
  9. #ifndef _HPM_SGTL5000_H_
  10. #define _HPM_SGTL5000_H_
  11. #include "hpm_i2c_drv.h"
  12. #include "hpm_common.h"
  13. #include "hpm_wm8960_regs.h"
  14. #define WM8960_I2C_ADDR 0x1A
  15. typedef enum wm8960_module {
  16. wm8960_module_adc = 0, /* ADC module in WM8960 */
  17. wm8960_module_dac = 1, /* DAC module in WM8960 */
  18. wm8960_module_vref = 2, /* VREF module */
  19. wm8960_module_headphone = 3, /* Headphone */
  20. wm8960_module_micbais = 4, /* Mic bias */
  21. wm8960_module_ana_in = 6, /* Analog in PGA */
  22. wm8960_module_lineout = 7, /* Line out module */
  23. wm8960_module_speaker = 8, /* Speaker module */
  24. wm8960_module_output_mixer = 9, /* Output mixer */
  25. } wm8960_module_t;
  26. /* wm8960 play source for output mixer */
  27. typedef enum wm8960_play_source {
  28. wm8960_play_source_input_mixer = 1, /* Input Boost Mixer to Output Mixer */
  29. wm8960_play_source_input3 = 2, /* L/RINPUT3 to Output Mixer */
  30. wm8960_play_source_dac = 4, /* DAC to Output Mixer */
  31. } wm8960_play_source_t;
  32. /* WM8960 data route */
  33. typedef enum wm8960_route {
  34. wm8960_route_bypass = 0, /* ANA_IN->Headphone. */
  35. wm8960_route_playback = 1, /* I2SIN->DAC->Headphone. */
  36. wm8960_route_playback_and_record = 2, /* I2SIN->DAC->Headphone, ANA_IN->ADC->I2SOUT. */
  37. wm8960_route_record = 5 /* ANA_IN->ADC->I2SOUT. */
  38. } wm8960_route_t;
  39. /* The audio data transfer protocol choice */
  40. typedef enum wm8960_protocol {
  41. wm8960_bus_i2s = 2, /* I2S type */
  42. wm8960_bus_left_justified = 1, /* Left justified mode */
  43. wm8960_bus_right_justified = 0, /* Right justified mode */
  44. wm8960_bus_pcma = 3, /* PCM A mode */
  45. wm8960_bus_pcmb = 3 | (1 << 4) /* PCM B mode */
  46. } wm8960_protocol_t;
  47. /* wm8960 input source */
  48. typedef enum wm8960_input {
  49. wm8960_input_closed = 0, /* Input device is closed */
  50. wm8960_input_single_ended_mic = 1, /* Input as single ended mic, only use L/RINPUT1 */
  51. wm8960_input_differential_mic_input2 = 2, /* Input as differential mic, use L/RINPUT1 and L/RINPUT2 */
  52. wm8960_input_differential_mic_input3 = 3, /* Input as differential mic, use L/RINPUT1 and L/RINPUT3*/
  53. wm8960_input_line_input2 = 4, /* Input as line input, only use L/RINPUT2 */
  54. wm8960_input_line_input3 = 5 /* Input as line input, only use L/RINPUT3 */
  55. } wm8960_input_t;
  56. /* wm8960 audio format */
  57. typedef struct wm8960_audio_format {
  58. uint32_t mclk_hz; /* master clock frequency */
  59. uint32_t sample_rate; /* sample rate */
  60. uint32_t bit_width; /* bit width */
  61. } wm8960_audio_format_t;
  62. /* configure structure of WM8960 */
  63. typedef struct wm8960_config {
  64. wm8960_route_t route; /* Audio data route.*/
  65. wm8960_protocol_t bus; /* Audio transfer protocol */
  66. bool enable_speaker; /* True means enable class D speaker as output, false means no */
  67. wm8960_input_t left_input; /* Left input source for WM8960 */
  68. wm8960_input_t right_input; /* Right input source for wm8960 */
  69. wm8960_play_source_t play_source; /* play source */
  70. wm8960_audio_format_t format; /* Audio format */
  71. } wm8960_config_t;
  72. typedef struct {
  73. I2C_Type *ptr;; /* I2C bus */
  74. uint8_t slave_address; /* code device address */
  75. } wm8960_control_t;
  76. #if defined(__cplusplus)
  77. extern "C" {
  78. #endif
  79. /**
  80. * @brief WM8960 initialize function.
  81. *
  82. * @param control WM8960 control structure.
  83. * @param config WM8960 configuration structure.
  84. */
  85. hpm_stat_t wm8960_init(wm8960_control_t *control, wm8960_config_t *config);
  86. /**
  87. * @brief Deinit the WM8960 codec.
  88. *
  89. * This function close all modules in WM8960 to save power.
  90. *
  91. * @param control WM8960 control structure pointer.
  92. */
  93. hpm_stat_t wm8960_deinit(wm8960_control_t *control);
  94. /**
  95. * @brief Set audio data route in WM8960.
  96. *
  97. * This function would set the data route according to route.
  98. *
  99. * @param control WM8960 control structure.
  100. * @param config Audio configure structure in WM8960.
  101. */
  102. hpm_stat_t wm8960_set_data_route(wm8960_control_t *control, wm8960_config_t *config);
  103. /**
  104. * @brief Set left audio input source in WM8960.
  105. *
  106. * @param control WM8960 control structure.
  107. * @param input Audio input source.
  108. */
  109. hpm_stat_t wm8960_set_left_input(wm8960_control_t *control, wm8960_input_t input);
  110. /**
  111. * @brief Set right audio input source in WM8960.
  112. *
  113. * @param control WM8960 control structure.
  114. * @param input Audio input source.
  115. */
  116. hpm_stat_t wm8960_set_right_input(wm8960_control_t *control, wm8960_input_t input);
  117. /**
  118. * @brief Set the audio transfer protocol.
  119. *
  120. * @param control WM8960 control structure.
  121. * @param protocol Audio data transfer protocol.
  122. */
  123. hpm_stat_t wm8960_set_protocol(wm8960_control_t *control, wm8960_protocol_t protocol);
  124. /**
  125. * @brief Set the volume of different modules in WM8960.
  126. *
  127. * This function would set the volume of WM8960 modules. Uses need to appoint the module.
  128. * The function assume that left channel and right channel has the same volume.
  129. *
  130. * Module:wm8960_module_adc, volume range value: 0 is mute, 1-255 is -97db to 30db
  131. * Module:wm8960_module_dac, volume range value: 0 is mute, 1-255 is -127db to 0db
  132. * Module:wm8960_module_headphone, volume range value: 0 - 2F is mute, 0x30 - 0x7F is -73db to 6db
  133. * Module:wm8960_module_ana_in, volume range value: 0 - 0x3F is -17.25db to 30db
  134. * Module:wm8960_module_speaker, volume range value: 0 - 2F is mute, 0x30 - 0x7F is -73db to 6db
  135. *
  136. *
  137. * @param control WM8960 control structure.
  138. * @param module Module to set volume, it can be ADC, DAC, Headphone and so on.
  139. * @param volume Volume value need to be set.
  140. */
  141. hpm_stat_t wm8960_set_volume(wm8960_control_t *control, wm8960_module_t module, uint32_t volume);
  142. /**
  143. * @brief Enable/disable expected module.
  144. *
  145. * @param control WM8960 control structure.
  146. * @param module Module expected to enable.
  147. * @param enable Enable or disable moudles.
  148. */
  149. hpm_stat_t wm8960_set_module(wm8960_control_t *control, wm8960_module_t module, bool enable);
  150. /**
  151. * @brief SET the WM8960 play source.
  152. *
  153. * @param control WM8960 control structure.
  154. * @param play_source play source
  155. *
  156. * @return kStatus_WM8904_Success if successful, different code otherwise..
  157. */
  158. hpm_stat_t wm8960_config_input_to_output_mixer(wm8960_control_t *control, uint32_t play_source);
  159. /**
  160. * @brief Configure the data format of audio data.
  161. *
  162. * This function would configure the registers about the sample rate, bit depths.
  163. *
  164. * @param control WM8960 control structure pointer.
  165. * @param sysclk system clock of the codec which can be generated by MCLK or PLL output.
  166. * @param sample_rate Sample rate of audio file running in WM8960. WM8960 now
  167. * supports 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k, 44.1k, 48k and 96k sample rate.
  168. * @param bits Bit depth of audio file (WM8960 only supports 16bit, 20bit, 24bit
  169. * and 32 bit in HW).
  170. */
  171. hpm_stat_t wm8960_set_data_format(wm8960_control_t *control, uint32_t sysclk, uint32_t sample_rate, uint32_t bits);
  172. /**
  173. * @brief Write register to WM8960 using I2C.
  174. *
  175. * @param control WM8960 control structure.
  176. * @param reg The register address in WM8960.
  177. * @param val Value needs to write into the register.
  178. */
  179. hpm_stat_t wm8960_write_reg(wm8960_control_t *control, uint8_t reg, uint16_t val);
  180. /**
  181. * @brief Read register from WM8960 using I2C.
  182. * @param reg The register address in WM8960.
  183. * @param val Value written to.
  184. */
  185. hpm_stat_t wm8960_read_reg(uint8_t reg, uint16_t *val);
  186. /**
  187. * @brief Modify some bits in the register using I2C.
  188. * @param control WM8960 control structure.
  189. * @param reg The register address in WM8960.
  190. * @param mask The mask code for the bits want to write. The bit you want to write should be 0.
  191. * @param val Value needs to write into the register.
  192. */
  193. hpm_stat_t wm8960_modify_reg(wm8960_control_t *control, uint8_t reg, uint16_t mask, uint16_t val);
  194. #endif /* _HPM_SGTL5000_H_ */