fsl_dmic.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_dmic.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.dmic"
  38. #endif
  39. /*******************************************************************************
  40. * Variables
  41. ******************************************************************************/
  42. /* Array of DMIC peripheral base address. */
  43. static DMIC_Type *const s_dmicBases[FSL_FEATURE_SOC_DMIC_COUNT] = DMIC_BASE_PTRS;
  44. /* Array of DMIC clock name. */
  45. static const clock_ip_name_t s_dmicClock[FSL_FEATURE_SOC_DMIC_COUNT] = DMIC_CLOCKS;
  46. /* Array of DMIC IRQ number. */
  47. static const IRQn_Type s_dmicIRQ[FSL_FEATURE_SOC_DMIC_COUNT] = DMIC_IRQS;
  48. /*! @brief Callback function array for DMIC(s). */
  49. static dmic_callback_t s_dmicCallback[FSL_FEATURE_SOC_DMIC_COUNT];
  50. /* Array of HWVAD IRQ number. */
  51. static const IRQn_Type s_dmicHwvadIRQ[FSL_FEATURE_SOC_DMIC_COUNT] = DMIC_HWVAD_IRQS;
  52. /*! @brief Callback function array for HWVAD(s). */
  53. static dmic_hwvad_callback_t s_dmicHwvadCallback[FSL_FEATURE_SOC_DMIC_COUNT];
  54. /*******************************************************************************
  55. * Prototypes
  56. ******************************************************************************/
  57. /*!
  58. * @brief Get the DMIC instance from peripheral base address.
  59. *
  60. * @param base DMIC peripheral base address.
  61. * @return DMIC instance.
  62. */
  63. uint32_t DMIC_GetInstance(DMIC_Type *base)
  64. {
  65. uint32_t instance;
  66. /* Find the instance index from base address mappings. */
  67. for (instance = 0; instance < ARRAY_SIZE(s_dmicBases); instance++)
  68. {
  69. if (s_dmicBases[instance] == base)
  70. {
  71. break;
  72. }
  73. }
  74. assert(instance < ARRAY_SIZE(s_dmicBases));
  75. return instance;
  76. }
  77. void DMIC_Init(DMIC_Type *base)
  78. {
  79. assert(base);
  80. /* Enable the clock to the register interface */
  81. CLOCK_EnableClock(s_dmicClock[DMIC_GetInstance(base)]);
  82. /* Reset the peripheral */
  83. RESET_PeripheralReset(kDMIC_RST_SHIFT_RSTn);
  84. /* Disable DMA request*/
  85. base->CHANNEL[0].FIFO_CTRL &= ~DMIC_CHANNEL_FIFO_CTRL_DMAEN(1);
  86. base->CHANNEL[1].FIFO_CTRL &= ~DMIC_CHANNEL_FIFO_CTRL_DMAEN(1);
  87. /* Disable DMIC interrupt. */
  88. base->CHANNEL[0].FIFO_CTRL &= ~DMIC_CHANNEL_FIFO_CTRL_INTEN(1);
  89. base->CHANNEL[1].FIFO_CTRL &= ~DMIC_CHANNEL_FIFO_CTRL_INTEN(1);
  90. }
  91. void DMIC_DeInit(DMIC_Type *base)
  92. {
  93. assert(base);
  94. /* Disable the clock to the register interface */
  95. CLOCK_DisableClock(s_dmicClock[DMIC_GetInstance(base)]);
  96. }
  97. void DMIC_ConfigIO(DMIC_Type *base, dmic_io_t config)
  98. {
  99. base->IOCFG = config;
  100. }
  101. void DMIC_SetOperationMode(DMIC_Type *base, operation_mode_t mode)
  102. {
  103. if (mode == kDMIC_OperationModeInterrupt)
  104. {
  105. /* Enable DMIC interrupt. */
  106. base->CHANNEL[0].FIFO_CTRL |= DMIC_CHANNEL_FIFO_CTRL_INTEN(1);
  107. base->CHANNEL[1].FIFO_CTRL |= DMIC_CHANNEL_FIFO_CTRL_INTEN(1);
  108. }
  109. if (mode == kDMIC_OperationModeDma)
  110. {
  111. /* enable DMA request*/
  112. base->CHANNEL[0].FIFO_CTRL |= DMIC_CHANNEL_FIFO_CTRL_DMAEN(1);
  113. base->CHANNEL[1].FIFO_CTRL |= DMIC_CHANNEL_FIFO_CTRL_DMAEN(1);
  114. }
  115. }
  116. void DMIC_ConfigChannel(DMIC_Type *base,
  117. dmic_channel_t channel,
  118. stereo_side_t side,
  119. dmic_channel_config_t *channel_config)
  120. {
  121. base->CHANNEL[channel].DIVHFCLK = channel_config->divhfclk;
  122. base->CHANNEL[channel].OSR = channel_config->osr;
  123. base->CHANNEL[channel].GAINSHIFT = channel_config->gainshft;
  124. base->CHANNEL[channel].PREAC2FSCOEF = channel_config->preac2coef;
  125. base->CHANNEL[channel].PREAC4FSCOEF = channel_config->preac4coef;
  126. base->CHANNEL[channel].PHY_CTRL =
  127. DMIC_CHANNEL_PHY_CTRL_PHY_FALL(side) | DMIC_CHANNEL_PHY_CTRL_PHY_HALF(channel_config->sample_rate);
  128. base->CHANNEL[channel].DC_CTRL = DMIC_CHANNEL_DC_CTRL_DCPOLE(channel_config->dc_cut_level) |
  129. DMIC_CHANNEL_DC_CTRL_DCGAIN(channel_config->post_dc_gain_reduce) |
  130. DMIC_CHANNEL_DC_CTRL_SATURATEAT16BIT(channel_config->saturate16bit);
  131. }
  132. void DMIC_CfgChannelDc(DMIC_Type *base,
  133. dmic_channel_t channel,
  134. dc_removal_t dc_cut_level,
  135. uint32_t post_dc_gain_reduce,
  136. bool saturate16bit)
  137. {
  138. base->CHANNEL[channel].DC_CTRL = DMIC_CHANNEL_DC_CTRL_DCPOLE(dc_cut_level) |
  139. DMIC_CHANNEL_DC_CTRL_DCGAIN(post_dc_gain_reduce) |
  140. DMIC_CHANNEL_DC_CTRL_SATURATEAT16BIT(saturate16bit);
  141. }
  142. void DMIC_Use2fs(DMIC_Type *base, bool use2fs)
  143. {
  144. base->USE2FS = (use2fs) ? 0x1 : 0x0;
  145. }
  146. void DMIC_EnableChannnel(DMIC_Type *base, uint32_t channelmask)
  147. {
  148. base->CHANEN = channelmask;
  149. }
  150. void DMIC_FifoChannel(DMIC_Type *base, uint32_t channel, uint32_t trig_level, uint32_t enable, uint32_t resetn)
  151. {
  152. base->CHANNEL[channel].FIFO_CTRL |=
  153. (base->CHANNEL[channel].FIFO_CTRL & (DMIC_CHANNEL_FIFO_CTRL_INTEN_MASK | DMIC_CHANNEL_FIFO_CTRL_DMAEN_MASK)) |
  154. DMIC_CHANNEL_FIFO_CTRL_TRIGLVL(trig_level) | DMIC_CHANNEL_FIFO_CTRL_ENABLE(enable) |
  155. DMIC_CHANNEL_FIFO_CTRL_RESETN(resetn);
  156. }
  157. void DMIC_EnableIntCallback(DMIC_Type *base, dmic_callback_t cb)
  158. {
  159. uint32_t instance;
  160. instance = DMIC_GetInstance(base);
  161. NVIC_ClearPendingIRQ(s_dmicIRQ[instance]);
  162. /* Save callback pointer */
  163. s_dmicCallback[instance] = cb;
  164. EnableIRQ(s_dmicIRQ[instance]);
  165. }
  166. void DMIC_DisableIntCallback(DMIC_Type *base, dmic_callback_t cb)
  167. {
  168. uint32_t instance;
  169. instance = DMIC_GetInstance(base);
  170. DisableIRQ(s_dmicIRQ[instance]);
  171. s_dmicCallback[instance] = NULL;
  172. NVIC_ClearPendingIRQ(s_dmicIRQ[instance]);
  173. }
  174. void DMIC_HwvadEnableIntCallback(DMIC_Type *base, dmic_hwvad_callback_t vadcb)
  175. {
  176. uint32_t instance;
  177. instance = DMIC_GetInstance(base);
  178. NVIC_ClearPendingIRQ(s_dmicHwvadIRQ[instance]);
  179. /* Save callback pointer */
  180. s_dmicHwvadCallback[instance] = vadcb;
  181. EnableIRQ(s_dmicHwvadIRQ[instance]);
  182. }
  183. void DMIC_HwvadDisableIntCallback(DMIC_Type *base, dmic_hwvad_callback_t vadcb)
  184. {
  185. uint32_t instance;
  186. instance = DMIC_GetInstance(base);
  187. DisableIRQ(s_dmicHwvadIRQ[instance]);
  188. s_dmicHwvadCallback[instance] = NULL;
  189. NVIC_ClearPendingIRQ(s_dmicHwvadIRQ[instance]);
  190. }
  191. /* IRQ handler functions overloading weak symbols in the startup */
  192. #if defined(DMIC0)
  193. /*DMIC0 IRQ handler */
  194. void DMIC0_DriverIRQHandler(void)
  195. {
  196. if (s_dmicCallback[0] != NULL)
  197. {
  198. s_dmicCallback[0]();
  199. }
  200. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  201. exception return operation might vector to incorrect interrupt */
  202. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  203. __DSB();
  204. #endif
  205. }
  206. /*DMIC0 HWVAD IRQ handler */
  207. void HWVAD0_DriverIRQHandler(void)
  208. {
  209. if (s_dmicHwvadCallback[0] != NULL)
  210. {
  211. s_dmicHwvadCallback[0]();
  212. }
  213. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  214. exception return operation might vector to incorrect interrupt */
  215. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  216. __DSB();
  217. #endif
  218. }
  219. #endif