fsl_codec_common.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2017-2019 NXP
  3. * All rights reserved.
  4. *
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_codec_common.h"
  9. #include "fsl_codec_adapter.h"
  10. /*******************************************************************************
  11. * Definitions
  12. ******************************************************************************/
  13. /*! @brief codec play and record capability */
  14. #define GET_PLAY_CHANNEL_CAPABILITY(capability) (capability & 0xFFU)
  15. #define GET_PLAY_SOURCE_CAPABILITY(capability) (capability >> 8U)
  16. #define GET_RECORD_SOURCE_CAPABILITY(capability) (capability & 0x3FU)
  17. #define GET_RECORD_CHANNEL_CAPABILITY(capability) (capability >> 6U)
  18. /*******************************************************************************
  19. * Variables
  20. ******************************************************************************/
  21. /*******************************************************************************
  22. * Code
  23. ******************************************************************************/
  24. /*!
  25. * brief Codec initilization.
  26. *
  27. * param handle codec handle.
  28. * param config codec configuration.
  29. * return kStatus_Success is success, else initial failed.
  30. */
  31. status_t CODEC_Init(codec_handle_t *handle, codec_config_t *config)
  32. {
  33. assert((config != NULL) && (handle != NULL));
  34. /* Set the handle information */
  35. handle->codecConfig = config;
  36. return HAL_CODEC_Init(handle, config);
  37. }
  38. /*!
  39. * brief Codec de-initilization.
  40. *
  41. * param handle codec handle.
  42. * return kStatus_Success is success, else de-initial failed.
  43. */
  44. status_t CODEC_Deinit(codec_handle_t *handle)
  45. {
  46. assert((handle != NULL) && (handle->codecConfig != NULL));
  47. return HAL_CODEC_Deinit(handle);
  48. }
  49. /*!
  50. * brief set audio data format.
  51. *
  52. * param handle codec handle.
  53. * param mclk master clock frequency in HZ.
  54. * param sampleRate sample rate in HZ.
  55. * param bitWidth bit width.
  56. * return kStatus_Success is success, else configure failed.
  57. */
  58. status_t CODEC_SetFormat(codec_handle_t *handle, uint32_t mclk, uint32_t sampleRate, uint32_t bitWidth)
  59. {
  60. assert((handle != NULL) && (handle->codecConfig != NULL));
  61. return HAL_CODEC_SetFormat(handle, mclk, sampleRate, bitWidth);
  62. }
  63. /*!
  64. * brief codec module control.
  65. *
  66. * This function is used for codec module control, support switch digital interface cmd, can be expand to support codec
  67. * module specific feature
  68. *
  69. * param handle codec handle.
  70. * param cmd module control cmd, reference _codec_module_ctrl_cmd.
  71. * param data value to write, when cmd is kCODEC_ModuleRecordSourceChannel, the data should be a value combine
  72. * of channel and source, please reference macro CODEC_MODULE_RECORD_SOURCE_CHANNEL(source, LP, LN, RP, RN), reference
  73. * codec specific driver for detail configurations.
  74. * return kStatus_Success is success, else configure failed.
  75. */
  76. status_t CODEC_ModuleControl(codec_handle_t *handle, codec_module_ctrl_cmd_t cmd, uint32_t data)
  77. {
  78. assert((handle != NULL) && (handle->codecConfig != NULL));
  79. switch (cmd)
  80. {
  81. case kCODEC_ModuleSwitchI2SInInterface:
  82. if ((handle->codecCapability.codecModuleCapability & kCODEC_SupportModuleI2SInSwitchInterface) == 0U)
  83. {
  84. return kStatus_CODEC_NotSupport;
  85. }
  86. break;
  87. default:
  88. return kStatus_CODEC_NotSupport;
  89. }
  90. return HAL_CODEC_ModuleControl(handle, cmd, data);
  91. }
  92. /*!
  93. * brief set audio codec module volume.
  94. *
  95. * param handle codec handle.
  96. * param channel audio codec play channel, can be a value or combine value of _codec_play_channel.
  97. * param volume volume value, support 0 ~ 100, 0 is mute, 100 is the maximum volume value.
  98. * return kStatus_Success is success, else configure failed.
  99. */
  100. status_t CODEC_SetVolume(codec_handle_t *handle, uint32_t playChannel, uint32_t volume)
  101. {
  102. assert((handle != NULL) && (handle->codecConfig != NULL));
  103. assert(volume <= CODEC_VOLUME_MAX_VALUE);
  104. /* check capability of set volume */
  105. if ((GET_PLAY_CHANNEL_CAPABILITY(handle->codecCapability.codecPlayCapability) & playChannel) == 0U)
  106. {
  107. return kStatus_CODEC_NotSupport;
  108. }
  109. return HAL_CODEC_SetVolume(handle, playChannel, volume);
  110. }
  111. /*!
  112. * brief set audio codec module mute.
  113. *
  114. * param handle codec handle.
  115. * param channel audio codec play channel, can be a value or combine value of _codec_play_channel.
  116. * param mute true is mute, false is unmute.
  117. * return kStatus_Success is success, else configure failed.
  118. */
  119. status_t CODEC_SetMute(codec_handle_t *handle, uint32_t playChannel, bool mute)
  120. {
  121. assert((handle != NULL) && (handle->codecConfig != NULL));
  122. /* check capability of mute */
  123. if ((GET_PLAY_CHANNEL_CAPABILITY(handle->codecCapability.codecPlayCapability) & playChannel) == 0U)
  124. {
  125. return kStatus_CODEC_NotSupport;
  126. }
  127. return HAL_CODEC_SetMute(handle, playChannel, mute);
  128. }
  129. /*!
  130. * brief set audio codec module power.
  131. *
  132. * param handle codec handle.
  133. * param module audio codec module.
  134. * param powerOn true is power on, false is power down.
  135. * return kStatus_Success is success, else configure failed.
  136. */
  137. status_t CODEC_SetPower(codec_handle_t *handle, codec_module_t module, bool powerOn)
  138. {
  139. assert((handle != NULL) && (handle->codecConfig != NULL));
  140. /* check capability of power switch */
  141. if ((handle->codecCapability.codecModuleCapability & (1U << module)) == 0U)
  142. {
  143. return kStatus_CODEC_NotSupport;
  144. }
  145. return HAL_CODEC_SetPower(handle, module, powerOn);
  146. }
  147. /*!
  148. * brief codec set record source.
  149. *
  150. * param handle codec handle.
  151. * param source audio codec record source, can be a value or combine value of _codec_record_source.
  152. *
  153. * return kStatus_Success is success, else configure failed.
  154. */
  155. status_t CODEC_SetRecord(codec_handle_t *handle, uint32_t recordSource)
  156. {
  157. assert((handle != NULL) && (handle->codecConfig != NULL));
  158. /* check capability of record capability */
  159. if ((GET_RECORD_SOURCE_CAPABILITY(handle->codecCapability.codecRecordCapability) & recordSource) == 0U)
  160. {
  161. return kStatus_CODEC_NotSupport;
  162. }
  163. return HAL_CODEC_SetRecord(handle, recordSource);
  164. }
  165. /*!
  166. * brief codec set record channel.
  167. *
  168. * param handle codec handle.
  169. * param leftRecordChannel audio codec record channel, reference _codec_record_channel, can be a value or combine value
  170. of member in _codec_record_channel.
  171. * param rightRecordChannel audio codec record channel, reference _codec_record_channel, can be a value combine of
  172. member in _codec_record_channel.
  173. * return kStatus_Success is success, else configure failed.
  174. */
  175. status_t CODEC_SetRecordChannel(codec_handle_t *handle, uint32_t leftRecordChannel, uint32_t rightRecordChannel)
  176. {
  177. assert((handle != NULL) && (handle->codecConfig != NULL));
  178. /* check capability of record capability */
  179. if ((GET_RECORD_CHANNEL_CAPABILITY(handle->codecCapability.codecRecordCapability) & leftRecordChannel) == 0U)
  180. {
  181. return kStatus_CODEC_NotSupport;
  182. }
  183. if ((GET_RECORD_CHANNEL_CAPABILITY(handle->codecCapability.codecRecordCapability) & rightRecordChannel) == 0U)
  184. {
  185. return kStatus_CODEC_NotSupport;
  186. }
  187. return HAL_CODEC_SetRecordChannel(handle, leftRecordChannel, rightRecordChannel);
  188. }
  189. /*!
  190. * brief codec set play source.
  191. *
  192. * param handle codec handle.
  193. * param playSource audio codec play source, can be a value or combine value of _codec_play_source.
  194. *
  195. * return kStatus_Success is success, else configure failed.
  196. */
  197. status_t CODEC_SetPlay(codec_handle_t *handle, uint32_t playSource)
  198. {
  199. assert((handle != NULL) && (handle->codecConfig != NULL));
  200. /* check capability of record capability */
  201. if ((GET_PLAY_SOURCE_CAPABILITY(handle->codecCapability.codecPlayCapability) & playSource) == 0U)
  202. {
  203. return kStatus_CODEC_NotSupport;
  204. }
  205. return HAL_CODEC_SetPlay(handle, playSource);
  206. }