fsl_wm8904.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2019 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_wm8904.h"
  9. #if WM8904_DEBUG_REGISTER
  10. #include "fsl_debug_console.h"
  11. #endif
  12. /*******************************************************************************
  13. * Definitions
  14. ******************************************************************************/
  15. /*! @brief wm8904 volume mapping */
  16. #define WM8904_MAP_DAC_ADC_VOLUME(volume) (volume * (255 / 100U))
  17. #define WM8904_MAP_PGA_VOLUME(volume) (volume > 0x1FU ? 0x1FU : volume)
  18. #define WM8904_MAP_HEADPHONE_LINEOUT_VOLUME(volume) (volume > 0x3FU ? 0x3FU : volume)
  19. #define WM8904_SWAP_UINT16_BYTE_SEQUENCE(x) (__REV16(x))
  20. /*******************************************************************************
  21. * Prototypes
  22. ******************************************************************************/
  23. /*!
  24. * @brief WM8904 update format.
  25. *
  26. * @param handle WM8904 handle structure.
  27. * @param format format configurations.
  28. * @return kStatus_Success, else failed.
  29. */
  30. static status_t WM8904_UpdateFormat(wm8904_handle_t *handle, wm8904_audio_format_t *format);
  31. /*!
  32. * @brief WM8904 wait on write sequencer.
  33. *
  34. * @param handle WM8904 handle structure.
  35. * @return kStatus_Success, else failed.
  36. */
  37. static status_t WM8904_WaitOnWriteSequencer(wm8904_handle_t *handle);
  38. /*******************************************************************************
  39. * Variables
  40. ******************************************************************************/
  41. #if WM8904_DEBUG_REGISTER
  42. /*! @brief register definition */
  43. static const uint8_t allRegisters[] = {
  44. 0x00, 0x04, 0x05, 0x06, 0x07, 0x0A, 0x0C, 0x0E, 0x0F, 0x12, 0x14, 0x15, 0x16, 0x18, 0x19, 0x1A, 0x1B,
  45. 0x1E, 0x1F, 0x20, 0x21, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x39,
  46. 0x3A, 0x3B, 0x3C, 0x3D, 0x43, 0x44, 0x45, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x5A, 0x5E, 0x62,
  47. 0x68, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7E, 0x7F,
  48. 0x80, 0x81, 0x82, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93,
  49. 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0xC6, 0xF7, 0xF8};
  50. #endif
  51. /*******************************************************************************
  52. * Code
  53. ******************************************************************************/
  54. static status_t WM8904_UpdateFormat(wm8904_handle_t *handle, wm8904_audio_format_t *format)
  55. {
  56. status_t result;
  57. /* Disable SYSCLK */
  58. result = WM8904_WriteRegister(handle, WM8904_CLK_RATES_2, 0x00);
  59. if (result != kStatus_WM8904_Success)
  60. {
  61. return result;
  62. }
  63. /* Set Clock ratio and sample rate */
  64. result = WM8904_WriteRegister(handle, WM8904_CLK_RATES_1, (format->fsRatio << 10) | format->sampleRate);
  65. if (result != kStatus_WM8904_Success)
  66. {
  67. return result;
  68. }
  69. /* Set bit resolution */
  70. result = WM8904_ModifyRegister(handle, WM8904_AUDIO_IF_1, 0x000C, format->bitWidth);
  71. if (result != kStatus_WM8904_Success)
  72. {
  73. return result;
  74. }
  75. /* Enable SYSCLK */
  76. result = WM8904_WriteRegister(handle, WM8904_CLK_RATES_2, 0x1007);
  77. if (result != kStatus_WM8904_Success)
  78. {
  79. return result;
  80. }
  81. return kStatus_WM8904_Success;
  82. }
  83. static status_t WM8904_WaitOnWriteSequencer(wm8904_handle_t *handle)
  84. {
  85. status_t result;
  86. uint16_t value;
  87. do
  88. {
  89. result = WM8904_ReadRegister(handle, WM8904_WRT_SEQUENCER_4, &value);
  90. } while ((result == kStatus_WM8904_Success) && (value & 1));
  91. return result;
  92. }
  93. /*!
  94. * brief WM8904 write register.
  95. *
  96. * param handle WM8904 handle structure.
  97. * param reg register address.
  98. * param value value to write.
  99. * return kStatus_Success, else failed.
  100. */
  101. status_t WM8904_WriteRegister(wm8904_handle_t *handle, uint8_t reg, uint16_t value)
  102. {
  103. assert(handle->config != NULL);
  104. assert(handle->config->slaveAddress != 0U);
  105. uint16_t writeValue = WM8904_SWAP_UINT16_BYTE_SEQUENCE(value);
  106. return CODEC_I2C_Send(&(handle->i2cHandle), handle->config->slaveAddress, reg, 1U, (uint8_t *)&writeValue, 2U);
  107. }
  108. /*!
  109. * brief WM8904 write register.
  110. *
  111. * param handle WM8904 handle structure.
  112. * param reg register address.
  113. * param value value to read.
  114. * return kStatus_Success, else failed.
  115. */
  116. status_t WM8904_ReadRegister(wm8904_handle_t *handle, uint8_t reg, uint16_t *value)
  117. {
  118. assert(handle->config != NULL);
  119. assert(handle->config->slaveAddress != 0U);
  120. uint8_t retval = 0;
  121. uint16_t readValue = 0U;
  122. retval = CODEC_I2C_Receive(&(handle->i2cHandle), handle->config->slaveAddress, reg, 1U, (uint8_t *)&readValue, 2U);
  123. *value = WM8904_SWAP_UINT16_BYTE_SEQUENCE(readValue);
  124. return retval;
  125. }
  126. /*!
  127. * brief WM8904 modify register.
  128. *
  129. * param handle WM8904 handle structure.
  130. * param reg register address.
  131. * oaram mask register bits mask.
  132. * param value value to write.
  133. * return kStatus_Success, else failed.
  134. */
  135. status_t WM8904_ModifyRegister(wm8904_handle_t *handle, uint8_t reg, uint16_t mask, uint16_t value)
  136. {
  137. status_t result;
  138. uint16_t regValue;
  139. result = WM8904_ReadRegister(handle, reg, &regValue);
  140. if (result != kStatus_WM8904_Success)
  141. {
  142. return result;
  143. }
  144. regValue &= (uint16_t)~mask;
  145. regValue |= value;
  146. return WM8904_WriteRegister(handle, reg, regValue);
  147. }
  148. /*!
  149. * brief Initializes WM8904.
  150. *
  151. * param handle WM8904 handle structure.
  152. * param codec_config WM8904 configuration structure.
  153. */
  154. status_t WM8904_Init(wm8904_handle_t *handle, wm8904_config_t *wm8904Config)
  155. {
  156. assert(handle != NULL);
  157. assert(wm8904Config != NULL);
  158. status_t result;
  159. wm8904_config_t *config = wm8904Config;
  160. handle->config = config;
  161. /* i2c bus initialization */
  162. result = CODEC_I2C_Init(&(handle->i2cHandle), wm8904Config->i2cConfig.codecI2CInstance, WM8904_I2C_BITRATE,
  163. wm8904Config->i2cConfig.codecI2CSourceClock);
  164. if (result != kStatus_HAL_I2cSuccess)
  165. {
  166. return kStatus_WM8904_Fail;
  167. }
  168. /* reset */
  169. result = WM8904_WriteRegister(handle, WM8904_RESET, 0x0000);
  170. if (result != kStatus_WM8904_Success)
  171. {
  172. return result;
  173. }
  174. /* MCLK_INV=0, SYSCLK_SRC=0, TOCLK_RATE=0, OPCLK_ENA=1,
  175. * CLK_SYS_ENA=1, CLK_DSP_ENA=1, TOCLK_ENA=1 */
  176. result = WM8904_WriteRegister(handle, WM8904_CLK_RATES_2, 0x000F);
  177. if (result != kStatus_WM8904_Success)
  178. {
  179. return result;
  180. }
  181. /* WSEQ_ENA=1, WSEQ_WRITE_INDEX=0_0000 */
  182. result = WM8904_WriteRegister(handle, WM8904_WRT_SEQUENCER_0, 0x0100);
  183. if (result != kStatus_WM8904_Success)
  184. {
  185. return result;
  186. }
  187. /* WSEQ_ABORT=0, WSEQ_START=1, WSEQ_START_INDEX=00_0000 */
  188. result = WM8904_WriteRegister(handle, WM8904_WRT_SEQUENCER_3, 0x0100);
  189. if (result != kStatus_WM8904_Success)
  190. {
  191. return result;
  192. }
  193. result = WM8904_WaitOnWriteSequencer(handle);
  194. if (result != kStatus_WM8904_Success)
  195. {
  196. return result;
  197. }
  198. /* TOCLK_RATE_DIV16=0, TOCLK_RATE_x4=1, SR_MODE=0, MCLK_DIV=1
  199. * (Required for MMCs: SGY, KRT see erratum CE000546) */
  200. result = WM8904_WriteRegister(handle, WM8904_CLK_RATES_0, 0xA45F);
  201. if (result != kStatus_WM8904_Success)
  202. {
  203. return result;
  204. }
  205. /* INL_ENA=1, INR ENA=1 */
  206. result = WM8904_WriteRegister(handle, WM8904_POWER_MGMT_0, 0x0003);
  207. if (result != kStatus_WM8904_Success)
  208. {
  209. return result;
  210. }
  211. /* HPL_PGA_ENA=1, HPR_PGA_ENA=1 */
  212. result = WM8904_WriteRegister(handle, WM8904_POWER_MGMT_2, 0x0003);
  213. if (result != kStatus_WM8904_Success)
  214. {
  215. return result;
  216. }
  217. /* DACL_ENA=1, DACR_ENA=1, ADCL_ENA=1, ADCR_ENA=1 */
  218. result = WM8904_WriteRegister(handle, WM8904_POWER_MGMT_6, 0x000F);
  219. if (result != kStatus_WM8904_Success)
  220. {
  221. return result;
  222. }
  223. /* ADC_OSR128=1 */
  224. result = WM8904_WriteRegister(handle, WM8904_ANALOG_ADC_0, 0x0001);
  225. if (result != kStatus_WM8904_Success)
  226. {
  227. return result;
  228. }
  229. /* DACL_DATINV=0, DACR_DATINV=0, DAC_BOOST=00, LOOPBACK=0, AIFADCL_SRC=0,
  230. * AIFADCR_SRC=1, AIFDACL_SRC=0, AIFDACR_SRC=1, ADC_COMP=0, ADC_COMPMODE=0,
  231. * DAC_COMP=0, DAC_COMPMODE=0 */
  232. result = WM8904_WriteRegister(handle, WM8904_AUDIO_IF_0, 0x0050);
  233. if (result != kStatus_WM8904_Success)
  234. {
  235. return result;
  236. }
  237. /* DAC_MONO=0, DAC_SB_FILT-0, DAC_MUTERATE=0, DAC_UNMUTE RAMP=0,
  238. * DAC_OSR128=1, DAC_MUTE=0, DEEMPH=0 (none) */
  239. result = WM8904_WriteRegister(handle, WM8904_DAC_DIG_1, 0x0040);
  240. if (result != kStatus_WM8904_Success)
  241. {
  242. return result;
  243. }
  244. /* LINMUTE=0, LIN_VOL=0_0101 */
  245. result = WM8904_WriteRegister(handle, WM8904_ANALOG_LEFT_IN_0, 0x0005);
  246. if (result != kStatus_WM8904_Success)
  247. {
  248. return result;
  249. }
  250. /* RINMUTE=0, RIN VOL=0_0101 LINEOUTL RMV SHORT-1, LINEOUTL ENA_OUTP=1,
  251. * LINEOUTL_ENA_DLY=1, LINEOUTL_ENA=1, LINEOUTR_RMV_SHORT-1,
  252. * LINEOUTR_ENA_OUTP=1 */
  253. result = WM8904_WriteRegister(handle, WM8904_ANALOG_RIGHT_IN_0, 0x0005);
  254. if (result != kStatus_WM8904_Success)
  255. {
  256. return result;
  257. }
  258. /* HPOUTL_MUTE=0, HPOUT_VU=0, HPOUTLZC=0, HPOUTL_VOL=11_1001 */
  259. result = WM8904_WriteRegister(handle, WM8904_ANALOG_OUT1_LEFT, 0x0039);
  260. if (result != kStatus_WM8904_Success)
  261. {
  262. return result;
  263. }
  264. /* HPOUTR_MUTE=0, HPOUT_VU=0, HPOUTRZC=0, HPOUTR_VOL=11_1001 */
  265. result = WM8904_WriteRegister(handle, WM8904_ANALOG_OUT1_RIGHT, 0x0039);
  266. if (result != kStatus_WM8904_Success)
  267. {
  268. return result;
  269. }
  270. /* Enable DC servos for headphone out */
  271. result = WM8904_WriteRegister(handle, WM8904_DC_SERVO_0, 0x0003);
  272. if (result != kStatus_WM8904_Success)
  273. {
  274. return result;
  275. }
  276. /* HPL_RMV_SHORT=1, HPL_ENA_OUTP=1, HPL_ENA_DLY=1, HPL_ENA=1,
  277. * HPR_RMV_SHORT=1, HPR_ENA_OUTP=1, HPR_ENA_DLY=1, HPR_ENA=1 */
  278. result = WM8904_WriteRegister(handle, WM8904_ANALOG_HP_0, 0x00FF);
  279. if (result != kStatus_WM8904_Success)
  280. {
  281. return result;
  282. }
  283. /* CP_DYN_PWR=1 */
  284. result = WM8904_WriteRegister(handle, WM8904_CLS_W_0, 0x0001);
  285. if (result != kStatus_WM8904_Success)
  286. {
  287. return result;
  288. }
  289. /* CP_ENA=1 */
  290. result = WM8904_WriteRegister(handle, WM8904_CHRG_PUMP_0, 0x0001);
  291. if (result != kStatus_WM8904_Success)
  292. {
  293. return result;
  294. }
  295. /* set wm8904 as slave */
  296. result = WM8904_SetMasterSlave(handle, config->master);
  297. if (result != kStatus_WM8904_Success)
  298. {
  299. return result;
  300. }
  301. /* set audio format */
  302. result = WM8904_SetProtocol(handle, config->protocol);
  303. if (result != kStatus_WM8904_Success)
  304. {
  305. return result;
  306. }
  307. result = WM8904_CheckAudioFormat(handle, &config->format, config->mclk_HZ);
  308. if (result != kStatus_WM8904_Success)
  309. {
  310. return result;
  311. }
  312. /* set record source and channel */
  313. result = WM8904_SetRecord(handle, config->recordSource);
  314. if (result != kStatus_WM8904_Success)
  315. {
  316. return result;
  317. }
  318. result = WM8904_SetRecordChannel(handle, config->recordChannelLeft, config->recordChannelRight);
  319. if (result != kStatus_WM8904_Success)
  320. {
  321. return result;
  322. }
  323. /* set play source */
  324. result = WM8904_SetPlay(handle, config->playSource);
  325. if (result != kStatus_WM8904_Success)
  326. {
  327. return result;
  328. }
  329. return result;
  330. }
  331. /*!
  332. * brief Deinitializes the WM8904 codec.
  333. *
  334. * This function resets WM8904.
  335. *
  336. * param handle WM8904 handle structure.
  337. *
  338. * return kStatus_WM8904_Success if successful, different code otherwise.
  339. */
  340. status_t WM8904_Deinit(wm8904_handle_t *handle)
  341. {
  342. /* reset */
  343. if (WM8904_WriteRegister(handle, WM8904_RESET, 0x0000) == kStatus_WM8904_Success)
  344. {
  345. return CODEC_I2C_Deinit(&(handle->i2cHandle));
  346. }
  347. return kStatus_WM8904_Fail;
  348. }
  349. /*!
  350. * brief Fills the configuration structure with default values.
  351. *
  352. * The default values are:
  353. *
  354. * master = false;
  355. * protocol = kWM8904_ProtocolI2S;
  356. * format.fsRatio = kWM8904_FsRatio64X;
  357. * format.sampleRate = kWM8904_SampleRate48kHz;
  358. * format.bitWidth = kWM8904_BitWidth16;
  359. *
  360. * param handle WM8904 handle structure to be filled with default values.
  361. */
  362. void WM8904_GetDefaultConfig(wm8904_config_t *config)
  363. {
  364. memset(config, 0, sizeof(wm8904_config_t));
  365. config->master = false;
  366. config->protocol = kWM8904_ProtocolI2S;
  367. config->format.sampleRate = kWM8904_SampleRate48kHz;
  368. config->format.bitWidth = kWM8904_BitWidth16;
  369. }
  370. /*!
  371. * brief Sets WM8904 as master or slave.
  372. *
  373. * param handle WM8904 handle structure.
  374. * param master true for master, false for slave.
  375. *
  376. * return kStatus_WM8904_Success if successful, different code otherwise.
  377. */
  378. status_t WM8904_SetMasterSlave(wm8904_handle_t *handle, bool master)
  379. {
  380. if (master)
  381. {
  382. /* only slave currently supported */
  383. return kStatus_WM8904_Fail;
  384. }
  385. return kStatus_WM8904_Success;
  386. }
  387. /*!
  388. * brief Sets the audio data transfer protocol.
  389. *
  390. * param handle WM8904 handle structure.
  391. * param protocol Audio transfer protocol.
  392. *
  393. * return kStatus_WM8904_Success if successful, different code otherwise.
  394. */
  395. status_t WM8904_SetProtocol(wm8904_handle_t *handle, wm8904_protocol_t protocol)
  396. {
  397. return WM8904_ModifyRegister(handle, WM8904_AUDIO_IF_1, (0x0003 | (1U << 4U)), (uint16_t)protocol);
  398. }
  399. /*!
  400. * brief Select LRC polarity.
  401. *
  402. * param handle WM8904 handle structure.
  403. * param polarity LRC clock polarity.
  404. *
  405. * return kStatus_WM8904_Success if successful, different code otherwise.
  406. */
  407. status_t WM8904_SelectLRCPolarity(wm8904_handle_t *handle, uint32_t polarity)
  408. {
  409. return WM8904_ModifyRegister(handle, WM8904_AUDIO_IF_1, 0x0010, polarity);
  410. }
  411. /*!
  412. * brief Enable WM8904 DAC time slot.
  413. *
  414. * param handle WM8904 handle structure.
  415. * param timeslot timeslot number.
  416. *
  417. * return kStatus_WM8904_Success if successful, different code otherwise.
  418. */
  419. status_t WM8904_EnableDACTDMMode(wm8904_handle_t *handle, wm8904_timeslot_t timeSlot)
  420. {
  421. return WM8904_ModifyRegister(handle, WM8904_AUDIO_IF_1, 3U << 12U, 1U << 13U | timeSlot << 12U);
  422. }
  423. /*!
  424. * brief Enable WM8904 ADC time slot.
  425. *
  426. * param handle WM8904 handle structure.
  427. * param timeslot timeslot number.
  428. *
  429. * return kStatus_WM8904_Success if successful, different code otherwise.
  430. */
  431. status_t WM8904_EnableADCTDMMode(wm8904_handle_t *handle, wm8904_timeslot_t timeSlot)
  432. {
  433. return WM8904_ModifyRegister(handle, WM8904_AUDIO_IF_1, 3U << 10U, 1U << 11U | timeSlot << 10U);
  434. }
  435. /*!
  436. * brief check and update the audio data format.
  437. * This api is used check the fsRatio setting based on the mclk and sample rate, if fsRatio setting
  438. * is not correct, it will correct it according to mclk and sample rate.
  439. * param handle WM8904 handle structure.
  440. * param format audio data format
  441. * param mclkFreq mclk frequency
  442. *
  443. * return kStatus_WM8904_Success if successful, different code otherwise.
  444. */
  445. status_t WM8904_CheckAudioFormat(wm8904_handle_t *handle, wm8904_audio_format_t *format, uint32_t mclkFreq)
  446. {
  447. assert(handle && format);
  448. status_t result;
  449. uint16_t mclkDiv = 0U;
  450. uint32_t sampleRate = 0U;
  451. uint32_t fsRatio = 0U;
  452. result = WM8904_ReadRegister(handle, WM8904_CLK_RATES_0, &mclkDiv);
  453. if (kStatus_WM8904_Success != result)
  454. {
  455. return result;
  456. }
  457. switch (format->sampleRate)
  458. {
  459. case kWM8904_SampleRate8kHz:
  460. sampleRate = 8000;
  461. break;
  462. case kWM8904_SampleRate12kHz:
  463. sampleRate = 12000;
  464. break;
  465. case kWM8904_SampleRate16kHz:
  466. sampleRate = 16000;
  467. break;
  468. case kWM8904_SampleRate24kHz:
  469. sampleRate = 24000;
  470. break;
  471. case kWM8904_SampleRate32kHz:
  472. sampleRate = 32000;
  473. break;
  474. case kWM8904_SampleRate48kHz:
  475. sampleRate = 48000;
  476. break;
  477. default:
  478. break;
  479. }
  480. fsRatio = (mclkFreq >> (mclkDiv & 0x1U)) / sampleRate;
  481. switch (fsRatio)
  482. {
  483. case 64:
  484. format->fsRatio = kWM8904_FsRatio64X;
  485. break;
  486. case 128:
  487. format->fsRatio = kWM8904_FsRatio128X;
  488. break;
  489. case 192:
  490. format->fsRatio = kWM8904_FsRatio192X;
  491. break;
  492. case 256:
  493. format->fsRatio = kWM8904_FsRatio256X;
  494. break;
  495. case 384:
  496. format->fsRatio = kWM8904_FsRatio384X;
  497. break;
  498. case 512:
  499. format->fsRatio = kWM8904_FsRatio512X;
  500. break;
  501. case 768:
  502. format->fsRatio = kWM8904_FsRatio768X;
  503. break;
  504. case 1024:
  505. format->fsRatio = kWM8904_FsRatio1024X;
  506. break;
  507. case 1408:
  508. format->fsRatio = kWM8904_FsRatio1408X;
  509. break;
  510. case 1536:
  511. format->fsRatio = kWM8904_FsRatio1536X;
  512. break;
  513. default:
  514. break;
  515. }
  516. return WM8904_UpdateFormat(handle, format);
  517. }
  518. /*!
  519. * brief Sets the audio data format.
  520. *
  521. * param handle WM8904 handle structure.
  522. * param sysclk System clock frequency for codec, user should pay attention to this parater, sysclk is caculate as
  523. * SYSCLK = MCLK / MCLKDIV, MCLKDIV is bit0 of WM8904_CLK_RATES_0.
  524. * param sampleRate Sample rate frequency in Hz.
  525. * param bitWidth Audio data bit width.
  526. *
  527. * return kStatus_WM8904_Success if successful, different code otherwise.
  528. */
  529. status_t WM8904_SetAudioFormat(wm8904_handle_t *handle, uint32_t sysclk, uint32_t sampleRate, uint32_t bitWidth)
  530. {
  531. status_t result;
  532. wm8904_audio_format_t format;
  533. uint32_t ratio = 0;
  534. switch (sampleRate)
  535. {
  536. case 8000:
  537. format.sampleRate = kWM8904_SampleRate8kHz;
  538. break;
  539. case 11025:
  540. case 12000:
  541. format.sampleRate = kWM8904_SampleRate12kHz;
  542. break;
  543. case 16000:
  544. format.sampleRate = kWM8904_SampleRate16kHz;
  545. break;
  546. case 22050:
  547. case 24000:
  548. format.sampleRate = kWM8904_SampleRate24kHz;
  549. break;
  550. case 32000:
  551. format.sampleRate = kWM8904_SampleRate32kHz;
  552. break;
  553. case 44100:
  554. case 48000:
  555. format.sampleRate = kWM8904_SampleRate48kHz;
  556. break;
  557. default:
  558. return kStatus_WM8904_Fail;
  559. }
  560. switch (bitWidth)
  561. {
  562. case 16:
  563. format.bitWidth = kWM8904_BitWidth16;
  564. break;
  565. case 20:
  566. format.bitWidth = kWM8904_BitWidth20;
  567. break;
  568. case 24:
  569. format.bitWidth = kWM8904_BitWidth24;
  570. break;
  571. case 32:
  572. format.bitWidth = kWM8904_BitWidth32;
  573. break;
  574. default:
  575. break;
  576. }
  577. ratio = sysclk / sampleRate;
  578. switch (ratio)
  579. {
  580. case 64:
  581. format.fsRatio = kWM8904_FsRatio64X;
  582. break;
  583. case 128:
  584. format.fsRatio = kWM8904_FsRatio128X;
  585. break;
  586. case 192:
  587. format.fsRatio = kWM8904_FsRatio192X;
  588. break;
  589. case 256:
  590. format.fsRatio = kWM8904_FsRatio256X;
  591. break;
  592. case 384:
  593. format.fsRatio = kWM8904_FsRatio384X;
  594. break;
  595. case 512:
  596. format.fsRatio = kWM8904_FsRatio512X;
  597. break;
  598. case 768:
  599. format.fsRatio = kWM8904_FsRatio768X;
  600. break;
  601. case 1024:
  602. format.fsRatio = kWM8904_FsRatio1024X;
  603. break;
  604. case 1408:
  605. format.fsRatio = kWM8904_FsRatio1408X;
  606. break;
  607. case 1536:
  608. format.fsRatio = kWM8904_FsRatio1536X;
  609. break;
  610. default:
  611. return kStatus_WM8904_Fail;
  612. }
  613. result = WM8904_UpdateFormat(handle, &format);
  614. return result;
  615. }
  616. /*!
  617. * brief Sets the headphone output volume.
  618. *
  619. * The parameter should be from 0 to 63.
  620. * The resulting volume will be (parameter - 57 dB).
  621. * 0 for -57 dB, 57 for 0 dB, 63 for +6 dB etc.
  622. *
  623. * param handle WM8904 handle structure.
  624. * param volumeLeft Volume of the left channel.
  625. * param volumeRight Volume of the right channel.
  626. *
  627. * return kStatus_WM8904_Success if successful, different code otherwise.
  628. */
  629. status_t WM8904_SetVolume(wm8904_handle_t *handle, uint16_t volumeLeft, uint16_t volumeRight)
  630. {
  631. status_t result;
  632. result = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT1_LEFT, 0x3F, volumeLeft);
  633. if (result != kStatus_WM8904_Success)
  634. {
  635. return result;
  636. }
  637. result = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT1_RIGHT, 0xBF, volumeRight | 0x0080);
  638. if (result != kStatus_WM8904_Success)
  639. {
  640. return result;
  641. }
  642. return kStatus_WM8904_Success;
  643. }
  644. /*!
  645. * brief Sets the headphone output mute.
  646. *
  647. * param handle WM8904 handle structure.
  648. * param muteLeft true to mute left channel, false to unmute.
  649. * param muteRight true to mute right channel, false to unmute.
  650. *
  651. * return kStatus_WM8904_Success if successful, different code otherwise.
  652. */
  653. status_t WM8904_SetMute(wm8904_handle_t *handle, bool muteLeft, bool muteRight)
  654. {
  655. status_t result;
  656. uint16_t left = muteLeft ? 0x0100 : 0x0000;
  657. uint16_t right = muteRight ? 0x0100 : 0x0000;
  658. result = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT1_LEFT, 0x0100, left);
  659. if (result != kStatus_WM8904_Success)
  660. {
  661. return result;
  662. }
  663. result = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT1_RIGHT, 0x0180, right | 0x0080);
  664. if (result != kStatus_WM8904_Success)
  665. {
  666. return result;
  667. }
  668. return kStatus_WM8904_Success;
  669. }
  670. #if WM8904_DEBUG_REGISTER
  671. /*!
  672. * brief Reads content of all WM8904 registers and prints it to debug console.
  673. *
  674. * param handle WM8904 handle structure.
  675. *
  676. * return kStatus_WM8904_Success if successful, different code otherwise.
  677. */
  678. status_t WM8904_PrintRegisters(wm8904_handle_t *handle)
  679. {
  680. status_t result;
  681. uint16_t value;
  682. uint32_t i;
  683. for (i = 0; i < sizeof(allRegisters); i++)
  684. {
  685. result = WM8904_ReadRegister(handle, allRegisters[i], &value);
  686. if (result != kStatus_WM8904_Success)
  687. {
  688. PRINTF("\r\n");
  689. return result;
  690. }
  691. PRINTF("%s", ((i % 8) == 0) ? "\r\n" : "\t");
  692. PRINTF("%02X:%04X", allRegisters[i], value);
  693. }
  694. PRINTF("\r\n");
  695. return result;
  696. }
  697. #endif
  698. /*!
  699. * brief Sets the channel output volume.
  700. *
  701. * The parameter should be from 0 to 100.
  702. * The resulting volume will be.
  703. * 0 for mute, 100 for maximum volume value.
  704. *
  705. * param handle codec handle structure.
  706. * param channel codec channel.
  707. * param volume volume value.
  708. *
  709. * return kStatus_WM8904_Success if successful, different code otherwise.
  710. */
  711. status_t WM8904_SetChannelVolume(wm8904_handle_t *handle, uint32_t channel, uint32_t volume)
  712. {
  713. status_t ret = kStatus_Fail;
  714. volume = WM8904_MAP_HEADPHONE_LINEOUT_VOLUME(volume);
  715. /* headphone left channel */
  716. if (channel & kWM8904_HeadphoneLeft)
  717. {
  718. ret = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT1_LEFT, volume == 0U ? 0x100U : 0x3FU,
  719. volume == 0U ? 0x100U : (volume));
  720. }
  721. /* headphone right channel */
  722. if (channel & kWM8904_HeadphoneRight)
  723. {
  724. ret = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT1_RIGHT, volume == 0U ? 0x100U : 0xBFU,
  725. volume == 0U ? 0x100U : (volume | 0x80U));
  726. }
  727. /* line out left channel */
  728. if (channel & kWM8904_LineoutLeft)
  729. {
  730. ret = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT2_LEFT, volume == 0U ? 0x100U : 0x3FU,
  731. volume == 0U ? 0x100U : (volume));
  732. }
  733. /* line out right channel */
  734. if (channel & kWM8904_LineoutRight)
  735. {
  736. ret = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT2_RIGHT, volume == 0U ? 0x100U : 0xBFU,
  737. volume == 0U ? 0x100U : (volume | 0x80U));
  738. }
  739. return ret;
  740. }
  741. /*!
  742. * brief Sets the channel mute.
  743. *
  744. * param handle codec handle structure.
  745. * param channel codec module name.
  746. * param isMute true is mute, false unmute.
  747. *
  748. * return kStatus_WM8904_Success if successful, different code otherwise.
  749. */
  750. status_t WM8904_SetChannelMute(wm8904_handle_t *handle, uint32_t channel, bool isMute)
  751. {
  752. status_t ret = kStatus_Fail;
  753. uint16_t regValue = 0U, regMask = 0U;
  754. regValue = isMute ? 0x180U : 0x80U;
  755. regMask = 0x100U;
  756. /* headphone left channel */
  757. if (channel & kWM8904_HeadphoneLeft)
  758. {
  759. ret = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT1_LEFT, regMask, regValue);
  760. }
  761. /* headphone right channel */
  762. if (channel & kWM8904_HeadphoneRight)
  763. {
  764. ret = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT1_RIGHT, regMask, regValue);
  765. }
  766. /* line out left channel */
  767. if (channel & kWM8904_LineoutLeft)
  768. {
  769. ret = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT2_LEFT, regMask, regValue);
  770. }
  771. /* line out right channel */
  772. if (channel & kWM8904_LineoutRight)
  773. {
  774. ret = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT2_RIGHT, regMask, regValue);
  775. }
  776. return ret;
  777. }
  778. /*!
  779. * brief SET the module output power.
  780. *
  781. * param handle WM8904 handle structure.
  782. * param module wm8904 module.
  783. * param isEnabled, true is power on, false is power down.
  784. *
  785. * return kStatus_WM8904_Success if successful, different code otherwise..
  786. */
  787. status_t WM8904_SetModulePower(wm8904_handle_t *handle, wm8904_module_t module, bool isEnabled)
  788. {
  789. uint8_t regAddr = 0, regBitMask = 0U, regValue = 0U;
  790. switch (module)
  791. {
  792. case kWM8904_ModuleADC:
  793. regAddr = WM8904_POWER_MGMT_6;
  794. regBitMask = 3U;
  795. regValue = isEnabled ? 3U : 0U;
  796. break;
  797. case kWM8904_ModuleDAC:
  798. regAddr = WM8904_POWER_MGMT_6;
  799. regBitMask = 0xCU;
  800. regValue = isEnabled ? 0xCU : 0U;
  801. break;
  802. case kWM8904_ModulePGA:
  803. regAddr = WM8904_POWER_MGMT_0;
  804. regBitMask = 3U;
  805. regValue = isEnabled ? 3U : 0U;
  806. break;
  807. case kWM8904_ModuleHeadphone:
  808. regAddr = WM8904_POWER_MGMT_2;
  809. regBitMask = 3U;
  810. regValue = isEnabled ? 3U : 0U;
  811. break;
  812. case kWM8904_ModuleLineout:
  813. regAddr = WM8904_POWER_MGMT_3;
  814. regBitMask = 3U;
  815. regValue = isEnabled ? 3U : 0U;
  816. break;
  817. default:
  818. return kStatus_InvalidArgument;
  819. }
  820. return WM8904_ModifyRegister(handle, regAddr, regBitMask, regValue);
  821. }
  822. /*!
  823. * brief SET the WM8904 record source.
  824. *
  825. * param handle WM8904 handle structure.
  826. * param recordSource record source , can be a value of kWM8904_ModuleRecordSourceDifferentialLine,
  827. * kWM8904_ModuleRecordSourceDifferentialMic, kWM8904_ModuleRecordSourceSingleEndMic,
  828. * kWM8904_ModuleRecordSourceDigitalMic.
  829. *
  830. * return kStatus_WM8904_Success if successful, different code otherwise.
  831. */
  832. status_t WM8904_SetRecord(wm8904_handle_t *handle, uint32_t recordSource)
  833. {
  834. uint8_t regLeftAddr = WM8904_ANALOG_LEFT_IN_1, regRightAddr = WM8904_ANALOG_RIGHT_IN_1;
  835. uint16_t regLeftValue = 0U, regRightValue = 0U, regBitMask;
  836. status_t ret = kStatus_Success;
  837. switch (recordSource)
  838. {
  839. case kWM8904_RecordSourceDifferentialLine:
  840. regLeftValue = 1U;
  841. regRightValue = 1U;
  842. regBitMask = 0x3FU;
  843. break;
  844. case kWM8904_RecordSourceDifferentialMic:
  845. regLeftValue = 2U;
  846. regRightValue = 2U;
  847. regBitMask = 0x3FU;
  848. break;
  849. case kWM8904_RecordSourceLineInput:
  850. regLeftValue = 0U;
  851. regRightValue = 0U;
  852. regBitMask = 0x3FU;
  853. break;
  854. case kWM8904_RecordSourceDigitalMic:
  855. regLeftValue = (1U << 12U);
  856. regLeftAddr = WM8904_DAC_DIG_0;
  857. regRightAddr = 0U;
  858. regBitMask = 1U << 12U;
  859. break;
  860. default:
  861. return kStatus_InvalidArgument;
  862. }
  863. ret = WM8904_ModifyRegister(handle, regLeftAddr, regBitMask, regLeftValue);
  864. if ((ret == kStatus_Success) && (regRightAddr))
  865. {
  866. return WM8904_ModifyRegister(handle, regRightAddr, regBitMask, regRightValue);
  867. }
  868. return kStatus_Success;
  869. }
  870. /*!
  871. * brief SET the WM8904 record source.
  872. *
  873. * param handle WM8904 handle structure.
  874. * param leftRecordChannel channel number of left record channel when using differential source, channel number of
  875. * single end left channel when using single end source, channel number of digital mic when using digital mic source.
  876. * param rightRecordChannel channel number of right record channel when using differential source, channel number
  877. * of single end right channel when using single end source.
  878. *
  879. * return kStatus_WM8904_Success if successful, different code otherwise..
  880. */
  881. status_t WM8904_SetRecordChannel(wm8904_handle_t *handle, uint32_t leftRecordChannel, uint32_t rightRecordChannel)
  882. {
  883. uint8_t regLeftAddr = WM8904_ANALOG_LEFT_IN_1, regRightAddr = WM8904_ANALOG_RIGHT_IN_1;
  884. uint16_t regLeftValue = 0U, regRightValue = 0U, regBitMask;
  885. status_t ret = kStatus_Success;
  886. uint8_t leftPositiveChannel = 0U, leftNegativeChannel = 0U, rightPositiveChannel = 0U, rightNegativeChannel = 0U;
  887. if (leftRecordChannel & kWM8904_RecordChannelDifferentialPositive1)
  888. {
  889. leftPositiveChannel = 0U;
  890. }
  891. else if (leftRecordChannel & kWM8904_RecordChannelDifferentialPositive2)
  892. {
  893. leftPositiveChannel = 1U;
  894. }
  895. else
  896. {
  897. leftPositiveChannel = 2U;
  898. }
  899. if (leftRecordChannel & kWM8904_RecordChannelDifferentialNegative1)
  900. {
  901. leftNegativeChannel = 0U;
  902. }
  903. else if (leftRecordChannel & kWM8904_RecordChannelDifferentialNegative2)
  904. {
  905. leftNegativeChannel = 1U;
  906. }
  907. else if (leftRecordChannel & kWM8904_RecordChannelDifferentialNegative3)
  908. {
  909. leftNegativeChannel = 2U;
  910. }
  911. else
  912. {
  913. leftNegativeChannel = leftPositiveChannel;
  914. }
  915. if (rightRecordChannel & kWM8904_RecordChannelDifferentialPositive1)
  916. {
  917. rightPositiveChannel = 0U;
  918. }
  919. else if (rightRecordChannel & kWM8904_RecordChannelDifferentialPositive2)
  920. {
  921. rightPositiveChannel = 1U;
  922. }
  923. else
  924. {
  925. rightPositiveChannel = 2U;
  926. }
  927. if (rightRecordChannel & kWM8904_RecordChannelDifferentialNegative1)
  928. {
  929. rightNegativeChannel = 0U;
  930. }
  931. else if (rightRecordChannel & kWM8904_RecordChannelDifferentialNegative2)
  932. {
  933. rightNegativeChannel = 1U;
  934. }
  935. else if (rightRecordChannel & kWM8904_RecordChannelDifferentialNegative3)
  936. {
  937. rightNegativeChannel = 2U;
  938. }
  939. else
  940. {
  941. rightNegativeChannel = rightPositiveChannel;
  942. }
  943. regLeftValue = ((leftNegativeChannel & 3U) << 4U) | ((leftPositiveChannel & 3U) << 2U);
  944. regRightValue = ((rightNegativeChannel & 3U) << 4U) | ((rightPositiveChannel & 3U) << 2U);
  945. regBitMask = 0x3CU;
  946. ret = WM8904_ModifyRegister(handle, regLeftAddr, regBitMask, regLeftValue);
  947. if ((ret == kStatus_Success) && (regRightAddr))
  948. {
  949. return WM8904_ModifyRegister(handle, regRightAddr, regBitMask, regRightValue);
  950. }
  951. return kStatus_Success;
  952. }
  953. /*!
  954. * brief SET the WM8904 play source.
  955. *
  956. * param handle WM8904 handle structure.
  957. * param playSource play source , can be a value of kWM8904_PlaySourcePGA/kWM8904_PlaySourceDAC.
  958. *
  959. * return kStatus_WM8904_Success if successful, different code otherwise..
  960. */
  961. status_t WM8904_SetPlay(wm8904_handle_t *handle, uint32_t playSource)
  962. {
  963. uint16_t regValue = 0U, regBitMask = 0xFU;
  964. /* source form PGA*/
  965. if (playSource == kWM8904_PlaySourcePGA)
  966. {
  967. regValue |= (3U << 2U) | 3U;
  968. }
  969. /* source from DAC*/
  970. if (playSource == kWM8904_PlaySourceDAC)
  971. {
  972. regValue &= ~((3U << 2U) | 3U);
  973. }
  974. return WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT12_ZC, regBitMask, regValue);
  975. }