fsl_codec_common.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Copyright 2017- 2019 NXP
  3. * All rights reserved.
  4. *
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_CODEC_COMMON_H_
  9. #define _FSL_CODEC_COMMON_H_
  10. #include "fsl_common.h"
  11. /*******************************************************************************
  12. * Definitions
  13. ******************************************************************************/
  14. /*! @name Driver version */
  15. /*@{*/
  16. /*! @brief CLOCK driver version 2.1.0. */
  17. #define FSL_CODEC_DRIVER_VERSION (MAKE_VERSION(2, 1, 0))
  18. /*@}*/
  19. /*! @brief CODEC handle buffer size */
  20. #ifndef CODEC_HANDLE_SIZE
  21. #define CODEC_HANDLE_SIZE (128U)
  22. #endif
  23. /*! @brief codec maximum volume range */
  24. #define CODEC_VOLUME_MAX_VALUE (100U)
  25. /*! @brief CODEC status */
  26. enum _codec_status
  27. {
  28. kStatus_CODEC_NotSupport = MAKE_STATUS(kStatusGroup_CODEC, 0U), /*!< CODEC not support status */
  29. kStatus_CODEC_DeviceNotRegistered = MAKE_STATUS(kStatusGroup_CODEC, 1U), /*!< CODEC device register failed status */
  30. kStatus_CODEC_I2CBusInitialFailed =
  31. MAKE_STATUS(kStatusGroup_CODEC, 2U), /*!< CODEC i2c bus initialization failed status */
  32. kStatus_CODEC_I2CCommandTransferFailed =
  33. MAKE_STATUS(kStatusGroup_CODEC, 3U), /*!< CODEC i2c bus command transfer failed status */
  34. };
  35. /*! @brief AUDIO format definition. */
  36. typedef enum _codec_audio_protocol
  37. {
  38. kCODEC_BusI2S = 0U, /*!< I2S type */
  39. kCODEC_BusLeftJustified = 1U, /*!< Left justified mode */
  40. kCODEC_BusRightJustified = 2U, /*!< Right justified mode */
  41. kCODEC_BusPCMA = 3U, /*!< DSP/PCM A mode */
  42. kCODEC_BusPCMB = 4U, /*!< DSP/PCM B mode */
  43. kCODEC_BusTDM = 5U, /*!< TDM mode */
  44. } codec_audio_protocol_t;
  45. /*! @brief audio sample rate definition */
  46. enum _codec_audio_sample_rate
  47. {
  48. kCODEC_AudioSampleRate8KHz = 8000U, /*!< Sample rate 8000 Hz */
  49. kCODEC_AudioSampleRate11025Hz = 11025U, /*!< Sample rate 11025 Hz */
  50. kCODEC_AudioSampleRate12KHz = 12000U, /*!< Sample rate 12000 Hz */
  51. kCODEC_AudioSampleRate16KHz = 16000U, /*!< Sample rate 16000 Hz */
  52. kCODEC_AudioSampleRate22050Hz = 22050U, /*!< Sample rate 22050 Hz */
  53. kCODEC_AudioSampleRate24KHz = 24000U, /*!< Sample rate 24000 Hz */
  54. kCODEC_AudioSampleRate32KHz = 32000U, /*!< Sample rate 32000 Hz */
  55. kCODEC_AudioSampleRate44100Hz = 44100U, /*!< Sample rate 44100 Hz */
  56. kCODEC_AudioSampleRate48KHz = 48000U, /*!< Sample rate 48000 Hz */
  57. kCODEC_AudioSampleRate96KHz = 96000U, /*!< Sample rate 96000 Hz */
  58. kCODEC_AudioSampleRate192KHz = 192000U, /*!< Sample rate 192000 Hz */
  59. kCODEC_AudioSampleRate384KHz = 384000U, /*!< Sample rate 384000 Hz */
  60. };
  61. /*! @brief audio bit width */
  62. enum _codec_audio_bit_width
  63. {
  64. kCODEC_AudioBitWidth16bit = 16U, /*!< audio bit width 16 */
  65. kCODEC_AudioBitWidth20bit = 20U, /*!< audio bit width 20 */
  66. kCODEC_AudioBitWidth24bit = 24U, /*!< audio bit width 24 */
  67. kCODEC_AudioBitWidth32bit = 32U, /*!< audio bit width 32 */
  68. };
  69. /*! @brief audio codec module*/
  70. typedef enum _codec_module
  71. {
  72. kCODEC_ModuleADC = 0U, /*!< codec module ADC */
  73. kCODEC_ModuleDAC = 1U, /*!< codec module DAC */
  74. kCODEC_ModulePGA = 2U, /*!< codec module PGA */
  75. kCODEC_ModuleHeadphone = 3U, /*!< codec module headphone */
  76. kCODEC_ModuleSpeaker = 4U, /*!< codec module speaker */
  77. kCODEC_ModuleLinein = 5U, /*!< codec module linein */
  78. kCODEC_ModuleLineout = 6U, /*!< codec module lineout */
  79. kCODEC_ModuleVref = 7U, /*!< codec module VREF */
  80. kCODEC_ModuleMicbias = 8U, /*!< codec module MIC BIAS */
  81. kCODEC_ModuleMic = 9U, /*!< codec module MIC */
  82. kCODEC_ModuleI2SIn = 10U, /*!< codec module I2S in */
  83. kCODEC_ModuleI2SOut = 11U, /*!< codec module I2S out */
  84. kCODEC_ModuleMxier = 12U, /*!< codec module mixer */
  85. } codec_module_t;
  86. /*! @brief audio codec module control cmd */
  87. typedef enum _codec_module_ctrl_cmd
  88. {
  89. kCODEC_ModuleSwitchI2SInInterface = 0U, /*!< module digital interface siwtch. */
  90. } codec_module_ctrl_cmd_t;
  91. /*! @brief audio codec module digital interface */
  92. enum _codec_module_ctrl_i2s_in_interface
  93. {
  94. kCODEC_ModuleI2SInInterfacePCM = 0U, /*!< Pcm interface*/
  95. kCODEC_ModuleI2SInInterfaceDSD = 1U, /*!< DSD interface */
  96. };
  97. /*! @brief audio codec module record source value */
  98. enum _codec_record_source
  99. {
  100. kCODEC_RecordSourceDifferentialLine = 1U, /*!< record source from differential line */
  101. kCODEC_RecordSourceLineInput = 2U, /*!< record source from line input */
  102. kCODEC_RecordSourceDifferentialMic = 4U, /*!< record source from differential mic */
  103. kCODEC_RecordSourceDigitalMic = 8U, /*!< record source from digital microphone */
  104. kCODEC_RecordSourceSingleEndMic = 16U, /*!< record source from single microphone */
  105. };
  106. /*! @brief audio codec record channel */
  107. enum _codec_reocrd_channel
  108. {
  109. kCODEC_RecordChannelLeft1 = 1U, /*!< left record channel 1 */
  110. kCODEC_RecordChannelLeft2 = 2U, /*!< left record channel 2 */
  111. kCODEC_RecordChannelLeft3 = 4U, /*!< left record channel 3 */
  112. kCODEC_RecordChannelRight1 = 1U, /*!< right record channel 1 */
  113. kCODEC_RecordChannelRight2 = 2U, /*!< right record channel 2 */
  114. kCODEC_RecordChannelRight3 = 4U, /*!< right record channel 3 */
  115. kCODEC_RecordChannelDifferentialPositive1 = 1U, /*!< differential positive record channel 1 */
  116. kCODEC_RecordChannelDifferentialPositive2 = 2U, /*!< differential positive record channel 2 */
  117. kCODEC_RecordChannelDifferentialPositive3 = 4U, /*!< differential positive record channel 3 */
  118. kCODEC_RecordChannelDifferentialNegative1 = 8U, /*!< differential negative record channel 1 */
  119. kCODEC_RecordChannelDifferentialNegative2 = 16U, /*!< differential negative record channel 2 */
  120. kCODEC_RecordChannelDifferentialNegative3 = 32U, /*!< differential negative record channel 3 */
  121. };
  122. /*! @brief audio codec module play source value */
  123. enum _codec_play_source
  124. {
  125. kCODEC_PlaySourcePGA = 1U, /*!< play source PGA, bypass ADC */
  126. kCODEC_PlaySourceInput = 2U, /*!< play source Input3 */
  127. kCODEC_PlaySourceDAC = 4U, /*!< play source DAC */
  128. kCODEC_PlaySourceMixerIn = 1U, /*!< play source mixer in */
  129. kCODEC_PlaySourceMixerInLeft = 2U, /*!< play source mixer in left */
  130. kCODEC_PlaySourceMixerInRight = 4U, /*!< play source mixer in right */
  131. kCODEC_PlaySourceAux = 8U, /*!< play source mixer in AUx */
  132. };
  133. /*! @brief codec play channel */
  134. enum _codec_play_channel
  135. {
  136. kCODEC_PlayChannelHeadphoneLeft = 1U, /*!< play channel headphone left */
  137. kCODEC_PlayChannelHeadphoneRight = 2U, /*!< play channel headphone right */
  138. kCODEC_PlayChannelSpeakerLeft = 4U, /*!< play channel speaker left */
  139. kCODEC_PlayChannelSpeakerRight = 8U, /*!< play channel speaker right */
  140. kCODEC_PlayChannelLineOutLeft = 16U, /*!< play channel lineout left */
  141. kCODEC_PlayChannelLineOutRight = 32U, /*!< play channel lineout right */
  142. kCODEC_PlayChannelLeft0 = 1U, /*!< play channel left0 */
  143. kCODEC_PlayChannelRight0 = 2U, /*!< play channel right0 */
  144. kCODEC_PlayChannelLeft1 = 4U, /*!< play channel left1 */
  145. kCODEC_PlayChannelRight1 = 8U, /*!< play channel right1 */
  146. kCODEC_PlayChannelLeft2 = 16U, /*!< play channel left2 */
  147. kCODEC_PlayChannelRight2 = 32U, /*!< play channel right2 */
  148. kCODEC_PlayChannelLeft3 = 64U, /*!< play channel left3 */
  149. kCODEC_PlayChannelRight3 = 128U, /*!< play channel right3 */
  150. };
  151. /*! @brief audio codec capability */
  152. enum _codec_capability_flag
  153. {
  154. kCODEC_SupportModuleADC = 1U << 0U, /*!< codec capability of module ADC */
  155. kCODEC_SupportModuleDAC = 1U << 1U, /*!< codec capability of module DAC */
  156. kCODEC_SupportModulePGA = 1U << 2U, /*!< codec capability of module PGA */
  157. kCODEC_SupportModuleHeadphone = 1U << 3U, /*!< codec capability of module headphone */
  158. kCODEC_SupportModuleSpeaker = 1U << 4U, /*!< codec capability of module speaker */
  159. kCODEC_SupportModuleLinein = 1U << 5U, /*!< codec capability of module linein */
  160. kCODEC_SupportModuleLineout = 1U << 6U, /*!< codec capability of module lineout */
  161. kCODEC_SupportModuleVref = 1U << 7U, /*!< codec capability of module vref */
  162. kCODEC_SupportModuleMicbias = 1U << 8U, /*!< codec capability of module mic bias */
  163. kCODEC_SupportModuleMic = 1U << 9U, /*!< codec capability of module mic bias */
  164. kCODEC_SupportModuleI2SIn = 1U << 10U, /*!< codec capability of module I2S in */
  165. kCODEC_SupportModuleI2SOut = 1U << 11U, /*!< codec capability of module I2S out */
  166. kCODEC_SupportModuleMixer = 1U << 12U, /*!< codec capability of module mixer */
  167. kCODEC_SupportModuleI2SInSwitchInterface = 1U << 13U, /*!< codec capability of module I2S in switch interface */
  168. kCODEC_SupportPlayChannelLeft0 = 1U << 0U, /*!< codec capability of play channel left 0 */
  169. kCODEC_SupportPlayChannelRight0 = 1U << 1U, /*!< codec capability of play channel right 0 */
  170. kCODEC_SupportPlayChannelLeft1 = 1U << 2U, /*!< codec capability of play channel left 1 */
  171. kCODEC_SupportPlayChannelRight1 = 1U << 3U, /*!< codec capability of play channel right 1 */
  172. kCODEC_SupportPlayChannelLeft2 = 1U << 4U, /*!< codec capability of play channel left 2 */
  173. kCODEC_SupportPlayChannelRight2 = 1U << 5U, /*!< codec capability of play channel right 2 */
  174. kCODEC_SupportPlayChannelLeft3 = 1U << 6U, /*!< codec capability of play channel left 3 */
  175. kCODEC_SupportPlayChannelRight3 = 1U << 7U, /*!< codec capability of play channel right 3 */
  176. kCODEC_SupportPlaySourcePGA = 1U << 8U, /*!< codec capability of set playback source PGA */
  177. kCODEC_SupportPlaySourceInput = 1U << 9U, /*!< codec capability of set playback source INPUT */
  178. kCODEC_SupportPlaySourceDAC = 1U << 10U, /*!< codec capability of set playback source DAC */
  179. kCODEC_SupportPlaySourceMixerIn = 1U << 11U, /*!< codec capability of set play source Mixer in */
  180. kCODEC_SupportPlaySourceMixerInLeft = 1U << 12U, /*!< codec capability of set play source Mixer in left */
  181. kCODEC_SupportPlaySourceMixerInRight = 1U << 13U, /*!< codec capability of set play source Mixer in right */
  182. kCODEC_SupportPlaySourceAux = 1U << 14U, /*!< codec capability of set play source aux */
  183. kCODEC_SupportRecordSourceDifferentialLine = 1U << 0U, /*!< codec capability of record source differential line */
  184. kCODEC_SupportRecordSourceLineInput = 1U << 1U, /*!< codec capability of record source line input */
  185. kCODEC_SupportRecordSourceDifferentialMic = 1U << 2U, /*!< codec capability of record source differential mic */
  186. kCODEC_SupportRecordSourceDigitalMic = 1U << 3U, /*!< codec capability of record digital mic */
  187. kCODEC_SupportRecordSourceSingleEndMic = 1U << 4U, /*!< codec capability of single end mic */
  188. kCODEC_SupportRecordChannelLeft1 = 1U << 6U, /*!< left record channel 1 */
  189. kCODEC_SupportRecordChannelLeft2 = 1U << 7U, /*!< left record channel 2 */
  190. kCODEC_SupportRecordChannelLeft3 = 1U << 8U, /*!< left record channel 3 */
  191. kCODEC_SupportRecordChannelRight1 = 1U << 9U, /*!< right record channel 1 */
  192. kCODEC_SupportRecordChannelRight2 = 1U << 10U, /*!< right record channel 2 */
  193. kCODEC_SupportRecordChannelRight3 = 1U << 11U, /*!< right record channel 3 */
  194. };
  195. /*!@brief codec handle declaration */
  196. typedef struct codec_handle codec_handle_t;
  197. /*! @brief Initialize structure of the codec */
  198. typedef struct _codec_config
  199. {
  200. uint32_t codecDevType; /*!< codec type */
  201. void *codecDevConfig; /*!< Codec device specific configuration */
  202. } codec_config_t;
  203. /*! @brief codec capability */
  204. typedef struct _codec_capability
  205. {
  206. uint32_t codecModuleCapability; /*!< codec module capability */
  207. uint32_t codecPlayCapability; /*!< codec play capability */
  208. uint32_t codecRecordCapability; /*!< codec record capability */
  209. } codec_capability_t;
  210. /*! @brief Codec handle definition.
  211. * * Application should allocate a buffer with CODEC_HANDLE_SIZE for handle definition, such as
  212. * uint8_t codecHandleBuffer[CODEC_HANDLE_SIZE];
  213. * codec_handle_t *codecHandle = codecHandleBuffer;
  214. */
  215. struct codec_handle
  216. {
  217. codec_config_t *codecConfig; /*!< codec configuration function pointer */
  218. codec_capability_t codecCapability; /*!< codec capability */
  219. void *codecDevHandle; /*!< codec device handle */
  220. };
  221. /*******************************************************************************
  222. * API
  223. ******************************************************************************/
  224. #if defined(__cplusplus)
  225. extern "C" {
  226. #endif
  227. /*!
  228. * @brief Codec initilization.
  229. *
  230. * @param handle codec handle.
  231. * @param config codec configurations.
  232. * @return kStatus_Success is success, else de-initial failed.
  233. */
  234. status_t CODEC_Init(codec_handle_t *handle, codec_config_t *config);
  235. /*!
  236. * @brief Codec de-initilization.
  237. *
  238. * @param handle codec handle.
  239. * @return kStatus_Success is success, else de-initial failed.
  240. */
  241. status_t CODEC_Deinit(codec_handle_t *handle);
  242. /*!
  243. * @brief set audio data format.
  244. *
  245. * @param handle codec handle.
  246. * @param mclk master clock frequency in HZ.
  247. * @param sampleRate sample rate in HZ.
  248. * @param bitWidth bit width.
  249. * @return kStatus_Success is success, else configure failed.
  250. */
  251. status_t CODEC_SetFormat(codec_handle_t *handle, uint32_t mclk, uint32_t sampleRate, uint32_t bitWidth);
  252. /*!
  253. * @brief codec module control.
  254. *
  255. * This function is used for codec module control, support switch digital interface cmd, can be expand to support codec
  256. * module specific feature.
  257. *
  258. * @param handle codec handle.
  259. * @param cmd module control cmd, reference _codec_module_ctrl_cmd.
  260. * @param data value to write, when cmd is kCODEC_ModuleRecordSourceChannel, the data should be a value combine
  261. * of channel and source, please reference macro CODEC_MODULE_RECORD_SOURCE_CHANNEL(source, LP, LN, RP, RN), reference
  262. * codec specific driver for detail configurations.
  263. * @return kStatus_Success is success, else configure failed.
  264. */
  265. status_t CODEC_ModuleControl(codec_handle_t *handle, codec_module_ctrl_cmd_t cmd, uint32_t data);
  266. /*!
  267. * @brief set audio codec pl volume.
  268. *
  269. * @param handle codec handle.
  270. * @param channel audio codec play channel, can be a value or combine value of _codec_play_channel.
  271. * @param volume volume value, support 0 ~ 100, 0 is mute, 100 is the maximum volume value.
  272. * @return kStatus_Success is success, else configure failed.
  273. */
  274. status_t CODEC_SetVolume(codec_handle_t *handle, uint32_t channel, uint32_t volume);
  275. /*!
  276. * @brief set audio codec module mute.
  277. *
  278. * @param handle codec handle.
  279. * @param channel audio codec play channel, can be a value or combine value of _codec_play_channel.
  280. * @param mute true is mute, false is unmute.
  281. * @return kStatus_Success is success, else configure failed.
  282. */
  283. status_t CODEC_SetMute(codec_handle_t *handle, uint32_t channel, bool mute);
  284. /*!
  285. * @brief set audio codec power.
  286. *
  287. * @param handle codec handle.
  288. * @param module audio codec module.
  289. * @param powerOn true is power on, false is power down.
  290. * @return kStatus_Success is success, else configure failed.
  291. */
  292. status_t CODEC_SetPower(codec_handle_t *handle, codec_module_t module, bool powerOn);
  293. /*!
  294. * @brief codec set record source.
  295. *
  296. * @param handle codec handle.
  297. * @param source audio codec record source, can be a value or combine value of _codec_record_source.
  298. *
  299. * @return kStatus_Success is success, else configure failed.
  300. */
  301. status_t CODEC_SetRecord(codec_handle_t *handle, uint32_t recordRource);
  302. /*!
  303. * @brief codec set record channel.
  304. *
  305. * @param handle codec handle.
  306. * @param leftRecordChannel audio codec record channel, reference _codec_record_channel, can be a value combine of
  307. member in _codec_record_channel.
  308. * @param rightRecordChannel audio codec record channel, reference _codec_record_channel, can be a value combine of
  309. member in _codec_record_channel.
  310. * @return kStatus_Success is success, else configure failed.
  311. */
  312. status_t CODEC_SetRecordChannel(codec_handle_t *handle, uint32_t leftRecordChannel, uint32_t rightRecordChannel);
  313. /*!
  314. * @brief codec set play source.
  315. *
  316. * @param handle codec handle.
  317. * @param playSource audio codec play source, can be a value or combine value of _codec_play_source.
  318. *
  319. * @return kStatus_Success is success, else configure failed.
  320. */
  321. status_t CODEC_SetPlay(codec_handle_t *handle, uint32_t playSource);
  322. #if defined(__cplusplus)
  323. }
  324. #endif
  325. #endif /* _FSL_CODEC_COMMON_H_ */