fsl_wm8960.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_WM8960_H_
  31. #define _FSL_WM8960_H_
  32. #include "fsl_common.h"
  33. #if defined(FSL_FEATURE_SOC_LPI2C_COUNT) && (FSL_FEATURE_SOC_LPI2C_COUNT)
  34. #include "fsl_lpi2c.h"
  35. #else
  36. #include "fsl_i2c.h"
  37. #endif
  38. /*!
  39. * @addtogroup wm8960
  40. * @{
  41. */
  42. /*******************************************************************************
  43. * Definitions
  44. ******************************************************************************/
  45. /*! @brief Define the register address of WM8960. */
  46. #define WM8960_LINVOL 0x0
  47. #define WM8960_RINVOL 0x1
  48. #define WM8960_LOUT1 0x2
  49. #define WM8960_ROUT1 0x3
  50. #define WM8960_CLOCK1 0x4
  51. #define WM8960_DACCTL1 0x5
  52. #define WM8960_DACCTL2 0x6
  53. #define WM8960_IFACE1 0x7
  54. #define WM8960_CLOCK2 0x8
  55. #define WM8960_IFACE2 0x9
  56. #define WM8960_LDAC 0xa
  57. #define WM8960_RDAC 0xb
  58. #define WM8960_RESET 0xf
  59. #define WM8960_3D 0x10
  60. #define WM8960_ALC1 0x11
  61. #define WM8960_ALC2 0x12
  62. #define WM8960_ALC3 0x13
  63. #define WM8960_NOISEG 0x14
  64. #define WM8960_LADC 0x15
  65. #define WM8960_RADC 0x16
  66. #define WM8960_ADDCTL1 0x17
  67. #define WM8960_ADDCTL2 0x18
  68. #define WM8960_POWER1 0x19
  69. #define WM8960_POWER2 0x1a
  70. #define WM8960_ADDCTL3 0x1b
  71. #define WM8960_APOP1 0x1c
  72. #define WM8960_APOP2 0x1d
  73. #define WM8960_LINPATH 0x20
  74. #define WM8960_RINPATH 0x21
  75. #define WM8960_LOUTMIX 0x22
  76. #define WM8960_ROUTMIX 0x25
  77. #define WM8960_MONOMIX1 0x26
  78. #define WM8960_MONOMIX2 0x27
  79. #define WM8960_LOUT2 0x28
  80. #define WM8960_ROUT2 0x29
  81. #define WM8960_MONO 0x2a
  82. #define WM8960_INBMIX1 0x2b
  83. #define WM8960_INBMIX2 0x2c
  84. #define WM8960_BYPASS1 0x2d
  85. #define WM8960_BYPASS2 0x2e
  86. #define WM8960_POWER3 0x2f
  87. #define WM8960_ADDCTL4 0x30
  88. #define WM8960_CLASSD1 0x31
  89. #define WM8960_CLASSD3 0x33
  90. #define WM8960_PLL1 0x34
  91. #define WM8960_PLL2 0x35
  92. #define WM8960_PLL3 0x36
  93. #define WM8960_PLL4 0x37
  94. /*! @brief Cache register number */
  95. #define WM8960_CACHEREGNUM 56
  96. /*! @brief WM8960_IFACE1 FORMAT bits */
  97. #define WM8960_IFACE1_FORMAT_MASK 0x03
  98. #define WM8960_IFACE1_FORMAT_SHIFT 0x00
  99. #define WM8960_IFACE1_FORMAT_RJ 0x00
  100. #define WM8960_IFACE1_FORMAT_LJ 0x01
  101. #define WM8960_IFACE1_FORMAT_I2S 0x02
  102. #define WM8960_IFACE1_FORMAT_DSP 0x03
  103. #define WM8960_IFACE1_FORMAT(x) ((x << WM8960_IFACE1_FORMAT_SHIFT) & WM8960_IFACE1_FORMAT_MASK)
  104. /*! @brief WM8960_IFACE1 WL bits */
  105. #define WM8960_IFACE1_WL_MASK 0x0C
  106. #define WM8960_IFACE1_WL_SHIFT 0x02
  107. #define WM8960_IFACE1_WL_16BITS 0x00
  108. #define WM8960_IFACE1_WL_20BITS 0x01
  109. #define WM8960_IFACE1_WL_24BITS 0x02
  110. #define WM8960_IFACE1_WL_32BITS 0x03
  111. #define WM8960_IFACE1_WL(x) ((x << WM8960_IFACE1_WL_SHIFT) & WM8960_IFACE1_WL_MASK)
  112. /*! @brief WM8960_IFACE1 LRP bit */
  113. #define WM8960_IFACE1_LRP_MASK 0x10
  114. #define WM8960_IFACE1_LRP_SHIFT 0x04
  115. #define WM8960_IFACE1_LRCLK_NORMAL_POL 0x00
  116. #define WM8960_IFACE1_LRCLK_INVERT_POL 0x01
  117. #define WM8960_IFACE1_DSP_MODEA 0x00
  118. #define WM8960_IFACE1_DSP_MODEB 0x01
  119. #define WM8960_IFACE1_LRP(x) ((x << WM8960_IFACE1_LRP_SHIFT) & WM8960_IFACE1_LRP_MASK)
  120. /*! @brief WM8960_IFACE1 DLRSWAP bit */
  121. #define WM8960_IFACE1_DLRSWAP_MASK 0x20
  122. #define WM8960_IFACE1_DLRSWAP_SHIFT 0x05
  123. #define WM8960_IFACE1_DACCH_NORMAL 0x00
  124. #define WM8960_IFACE1_DACCH_SWAP 0x01
  125. #define WM8960_IFACE1_DLRSWAP(x) ((x << WM8960_IFACE1_DLRSWAP_SHIFT) & WM8960_IFACE1_DLRSWAP_MASK)
  126. /*! @brief WM8960_IFACE1 MS bit */
  127. #define WM8960_IFACE1_MS_MASK 0x40
  128. #define WM8960_IFACE1_MS_SHIFT 0x06
  129. #define WM8960_IFACE1_SLAVE 0x00
  130. #define WM8960_IFACE1_MASTER 0x01
  131. #define WM8960_IFACE1_MS(x) ((x << WM8960_IFACE1_MS_SHIFT) & WM8960_IFACE1_MS_MASK)
  132. /*! @brief WM8960_IFACE1 BCLKINV bit */
  133. #define WM8960_IFACE1_BCLKINV_MASK 0x80
  134. #define WM8960_IFACE1_BCLKINV_SHIFT 0x07
  135. #define WM8960_IFACE1_BCLK_NONINVERT 0x00
  136. #define WM8960_IFACE1_BCLK_INVERT 0x01
  137. #define WM8960_IFACE1_BCLKINV(x) ((x << WM8960_IFACE1_BCLKINV_SHIFT) & WM8960_IFACE1_BCLKINV_MASK)
  138. /*! @brief WM8960_IFACE1 ALRSWAP bit */
  139. #define WM8960_IFACE1_ALRSWAP_MASK 0x100
  140. #define WM8960_IFACE1_ALRSWAP_SHIFT 0x08
  141. #define WM8960_IFACE1_ADCCH_NORMAL 0x00
  142. #define WM8960_IFACE1_ADCCH_SWAP 0x01
  143. #define WM8960_IFACE1_ALRSWAP(x) ((x << WM8960_IFACE1_ALRSWAP_SHIFT) & WM8960_IFACE1_ALRSWAP_MASK)
  144. /*! @brief WM8960_POWER1 */
  145. #define WM8960_POWER1_VREF_MASK 0x40
  146. #define WM8960_POWER1_VREF_SHIFT 0x06
  147. #define WM8960_POWER1_AINL_MASK 0x20
  148. #define WM8960_POWER1_AINL_SHIFT 0x05
  149. #define WM8960_POWER1_AINR_MASK 0x10
  150. #define WM8960_POWER1_AINR_SHIFT 0x04
  151. #define WM8960_POWER1_ADCL_MASK 0x08
  152. #define WM8960_POWER1_ADCL_SHIFT 0x03
  153. #define WM8960_POWER1_ADCR_MASK 0x04
  154. #define WM8960_POWER1_ADCR_SHIFT 0x02
  155. /*! @brief WM8960_POWER2 */
  156. #define WM8960_POWER2_DACL_MASK 0x100
  157. #define WM8960_POWER2_DACL_SHIFT 0x08
  158. #define WM8960_POWER2_DACR_MASK 0x80
  159. #define WM8960_POWER2_DACR_SHIFT 0x07
  160. #define WM8960_POWER2_LOUT1_MASK 0x40
  161. #define WM8960_POWER2_LOUT1_SHIFT 0x06
  162. #define WM8960_POWER2_ROUT1_MASK 0x20
  163. #define WM8960_POWER2_ROUT1_SHIFT 0x05
  164. #define WM8960_POWER2_SPKL_MASK 0x10
  165. #define WM8960_POWER2_SPKL_SHIFT 0x04
  166. #define WM8960_POWER2_SPKR_MASK 0x08
  167. #define WM8960_POWER2_SPKR_SHIFT 0x03
  168. /*! @brief WM8960 I2C address. */
  169. #define WM8960_I2C_ADDR 0x1A
  170. /*! @brief Modules in WM8960 board. */
  171. typedef enum _WM8960_module
  172. {
  173. kWM8960_ModuleADC = 0x0, /*!< ADC module in WM8960 */
  174. kWM8960_ModuleDAC = 0x1, /*!< DAC module in WM8960 */
  175. kWM8960_ModuleVREF = 0x2, /*!< VREF module */
  176. kWM8960_ModuleHP = 0x03, /*!< Headphone module */
  177. kWM8960_ModuleLineIn = 0x6, /*!< Line-in module */
  178. kWM8960_ModuleLineOut = 0x7, /*!< Line out module */
  179. kWM8960_ModuleSpeaker = 0x8 /*!< Speaker module */
  180. } wm8960_module_t;
  181. /*!
  182. * @brief WM8960 data route.
  183. * Only provide some typical data route, not all route listed.
  184. * Note: Users cannot combine any routes, once a new route is set, the previous one would be replaced.
  185. */
  186. typedef enum _wm8960_route
  187. {
  188. kWM8960_RouteBypass = 0x0, /*!< LINEIN->Headphone. */
  189. kWM8960_RoutePlayback = 0x1, /*!< I2SIN->DAC->Headphone. */
  190. kWM8960_RoutePlaybackandRecord = 0x2, /*!< I2SIN->DAC->Headphone, LINEIN->ADC->I2SOUT. */
  191. kWM8960_RoutePlaybackwithDAP = 0x3, /*!< I2SIN->DAP->DAC->Headphone. */
  192. kWM8960_RoutePlaybackwithDAPandRecord = 0x4, /*!< I2SIN->DAP->DAC->HP, LINEIN->ADC->I2SOUT. */
  193. kWM8960_RouteRecord = 0x5 /*!< LINEIN->ADC->I2SOUT. */
  194. } wm8960_route_t;
  195. /*!
  196. * @brief The audio data transfer protocol choice.
  197. * WM8960 only supports I2S format and PCM format.
  198. */
  199. typedef enum _wm8960_protocol
  200. {
  201. kWM8960_BusI2S = 0x0, /*!< I2S type */
  202. kWM8960_BusLeftJustified = 0x1, /*!< Left justified mode */
  203. kWM8960_BusRightJustified = 0x2, /*!< Right justified mode */
  204. kWM8960_BusPCMA = 0x3, /*!< PCM A mode */
  205. kWM8960_BusPCMB = 0x4 /*!< PCM B mode */
  206. } wm8960_protocol_t;
  207. /*! @brief WM8960 configure definition. */
  208. typedef struct wm8960_handle
  209. {
  210. #if defined(FSL_FEATURE_SOC_LPI2C_COUNT) && (FSL_FEATURE_SOC_LPI2C_COUNT)
  211. LPI2C_Type *base;
  212. lpi2c_master_transfer_t xfer;
  213. lpi2c_master_handle_t *i2cHandle;
  214. #else
  215. /* I2C relevant definition. */
  216. I2C_Type *base; /*!< I2C instance. */
  217. i2c_master_transfer_t xfer; /*!< I2C device setting */
  218. i2c_master_handle_t *i2cHandle; /*!< I2C internal state space. */
  219. #endif
  220. } wm8960_handle_t;
  221. /*! @brief Initialize structure of WM8960 */
  222. typedef struct wm8960_config
  223. {
  224. wm8960_route_t route; /*!< Audio data route.*/
  225. wm8960_protocol_t bus; /*!< Audio transfer protocol */
  226. bool master_slave; /*!< Master or slave. */
  227. } wm8960_config_t;
  228. /*******************************************************************************
  229. * API
  230. ******************************************************************************/
  231. #if defined(__cplusplus)
  232. extern "C" {
  233. #endif
  234. /*!
  235. * @brief WM8960 initialize function.
  236. *
  237. * The second parameter is NULL to WM8960 in this version. If users want
  238. * to change the settings, they have to use wm8960_write_reg() or wm8960_modify_reg()
  239. * to set the register value of WM8960.
  240. * Note: If the codec_config is NULL, it would initialize WM8960 using default settings.
  241. * The default setting:
  242. * codec_config->route = kWM8960_RoutePlaybackandRecord
  243. * codec_config->bus = kWM8960_BusI2S
  244. * codec_config->master = slave
  245. *
  246. * @param handle WM8960 handle structure.
  247. * @param codec_config WM8960 configuration structure.
  248. */
  249. void WM8960_Init(wm8960_handle_t *handle, wm8960_config_t *config);
  250. /*!
  251. * @brief Deinit the WM8960 codec.
  252. *
  253. * This function close all modules in WM8960 to save power.
  254. *
  255. * @param handle WM8960 handle structure pointer.
  256. */
  257. void WM8960_Deinit(wm8960_handle_t *handle);
  258. /*!
  259. * @brief Set audio data route in WM8960.
  260. *
  261. * This function would set the data route according to route. The route cannot be combined,
  262. * as all route would enable different modules.
  263. * Note: If a new route is set, the previous route would not work.
  264. *
  265. * @param handle WM8960 handle structure.
  266. * @param route Audio data route in WM8960.
  267. */
  268. status_t WM8960_SetDataRoute(wm8960_handle_t *handle, wm8960_route_t route);
  269. /*!
  270. * @brief Set the audio transfer protocol.
  271. *
  272. * WM8960 only supports I2S, left justified, right justified, PCM A, PCM B format.
  273. *
  274. * @param handle WM8960 handle structure.
  275. * @param bus Audio data transfer protocol.
  276. */
  277. status_t WM8960_SetProtocol(wm8960_handle_t *handle, wm8960_protocol_t protocol);
  278. /*!
  279. * @brief Set WM8960 as master or slave.
  280. *
  281. * @param handle WM8960 handle structure.
  282. * @param master 1 represent master, 0 represent slave.
  283. */
  284. void WM8960_SetMasterSlave(wm8960_handle_t *handle, bool master);
  285. /*!
  286. * @brief Set the volume of different modules in WM8960.
  287. *
  288. * This function would set the volume of WM8960 modules. Uses need to appoint the module.
  289. * The function assume that left channel and right channel has the same volume.
  290. *
  291. * @param handle WM8960 handle structure.
  292. * @param module Module to set volume, it can be ADC, DAC, Headphone and so on.
  293. * @param volume Volume value need to be set.
  294. */
  295. status_t WM8960_SetVolume(wm8960_handle_t *handle, wm8960_module_t module, uint32_t volume);
  296. /*!
  297. * @brief Get the volume of different modules in WM8960.
  298. *
  299. * This function gets the volume of WM8960 modules. Uses need to appoint the module.
  300. * The function assume that left channel and right channel has the same volume.
  301. *
  302. * @param handle WM8960 handle structure.
  303. * @param module Module to set volume, it can be ADC, DAC, Headphone and so on.
  304. * @return Volume value of the module.
  305. */
  306. uint32_t WM8960_GetVolume(wm8960_handle_t *handle, wm8960_module_t module);
  307. /*!
  308. * @brief Mute modules in WM8960.
  309. *
  310. * @param handle WM8960 handle structure.
  311. * @param module Modules need to be mute.
  312. * @param isEnabled Mute or unmute, 1 represent mute.
  313. */
  314. status_t WM8960_SetMute(wm8960_handle_t *handle, wm8960_module_t module, bool isEnabled);
  315. /*!
  316. * @brief Enable/disable expected devices.
  317. *
  318. * @param handle WM8960 handle structure.
  319. * @param module Module expected to enable.
  320. * @param isEnabled Enable or disable moudles.
  321. */
  322. status_t WM8960_SetModule(wm8960_handle_t *handle, wm8960_module_t module, bool isEnabled);
  323. /*!
  324. * @brief Configure the data format of audio data.
  325. *
  326. * This function would configure the registers about the sample rate, bit depths.
  327. *
  328. * @param handle WM8960 handle structure pointer.
  329. * @param mclk Master clock frequency of I2S.
  330. * @param sample_rate Sample rate of audio file running in WM8960. WM8960 now
  331. * supports 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k, 44.1k, 48k and 96k sample rate.
  332. * @param bits Bit depth of audio file (WM8960 only supports 16bit, 20bit, 24bit
  333. * and 32 bit in HW).
  334. */
  335. status_t WM8960_ConfigDataFormat(wm8960_handle_t *handle, uint32_t mclk, uint32_t sample_rate, uint8_t bits);
  336. /*!
  337. * @brief Enable/disable jack detect feature.
  338. *
  339. * @param handle WM8960 handle structure.
  340. * @param isEnabled Enable or disable moudles.
  341. */
  342. status_t WM8960_SetJackDetect(wm8960_handle_t *handle, bool isEnabled);
  343. /*!
  344. * @brief Write register to WM8960 using I2C.
  345. *
  346. * @param handle WM8960 handle structure.
  347. * @param reg The register address in WM8960.
  348. * @param val Value needs to write into the register.
  349. */
  350. status_t WM8960_WriteReg(wm8960_handle_t *handle, uint8_t reg, uint16_t val);
  351. /*!
  352. * @brief Read register from WM8960 using I2C.
  353. * @param handle WM8960 handle structure.
  354. * @param reg The register address in WM8960.
  355. * @param val Value written to.
  356. */
  357. status_t WM8960_ReadReg(uint8_t reg, uint16_t *val);
  358. /*!
  359. * @brief Modify some bits in the register using I2C.
  360. * @param handle WM8960 handle structure.
  361. * @param reg The register address in WM8960.
  362. * @param mask The mask code for the bits want to write. The bit you want to write should be 0.
  363. * @param val Value needs to write into the register.
  364. */
  365. status_t WM8960_ModifyReg(wm8960_handle_t *handle, uint8_t reg, uint16_t mask, uint16_t val);
  366. #if defined(__cplusplus)
  367. }
  368. #endif
  369. /*! @} */
  370. #endif /* _FSL_WM8960_H_ */
  371. /*******************************************************************************
  372. * API
  373. ******************************************************************************/