fsl_wm8904.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. * Change Logs:
  35. * Date Author Notes
  36. * 2019-11-09 LiWeiHao Porting to RT-Thread
  37. */
  38. #include "fsl_common.h"
  39. #include "fsl_debug_console.h"
  40. #include "fsl_wm8904.h"
  41. #include "fsl_i2c.h"
  42. /*******************************************************************************
  43. * Definitions
  44. ******************************************************************************/
  45. #define WM8904_RESET (0x00)
  46. #define WM8904_ANALOG_ADC_0 (0x0A)
  47. #define WM8904_POWER_MGMT_0 (0x0C)
  48. #define WM8904_POWER_MGMT_2 (0x0E)
  49. #define WM8904_POWER_MGMT_6 (0x12)
  50. #define WM8904_CLK_RATES_0 (0x14)
  51. #define WM8904_CLK_RATES_1 (0x15)
  52. #define WM8904_CLK_RATES_2 (0x16)
  53. #define WM8904_AUDIO_IF_0 (0x18)
  54. #define WM8904_AUDIO_IF_1 (0x19)
  55. #define WM8904_AUDIO_IF_2 (0x1A)
  56. #define WM8904_AUDIO_IF_3 (0x1B)
  57. #define WM8904_DAC_DIG_1 (0x21)
  58. #define WM8904_ANALOG_LEFT_IN_0 (0x2C)
  59. #define WM8904_ANALOG_RIGHT_IN_0 (0x2D)
  60. #define WM8904_ANALOG_LEFT_IN_1 (0x2E)
  61. #define WM8904_ANALOG_RIGHT_IN_1 (0x2F)
  62. #define WM8904_ANALOG_OUT1_LEFT (0x39)
  63. #define WM8904_ANALOG_OUT1_RIGHT (0x3A)
  64. #define WM8904_ANALOG_OUT12_ZC (0x3D)
  65. #define WM8904_DC_SERVO_0 (0x43)
  66. #define WM8904_ANALOG_HP_0 (0x5A)
  67. #define WM8904_CHRG_PUMP_0 (0x62)
  68. #define WM8904_CLS_W_0 (0x68)
  69. #define WM8904_WRT_SEQUENCER_0 (0x6C)
  70. #define WM8904_WRT_SEQUENCER_3 (0x6F)
  71. #define WM8904_WRT_SEQUENCER_4 (0x70)
  72. /*******************************************************************************
  73. * Prototypes
  74. ******************************************************************************/
  75. static status_t WM8904_WaitOnWriteSequencer(wm8904_handle_t *handle);
  76. static status_t WM8904_WriteRegister(wm8904_handle_t *handle, uint8_t reg, uint16_t value);
  77. static status_t WM8904_ReadRegister(wm8904_handle_t *handle, uint8_t reg, uint16_t *value);
  78. static status_t WM8904_ModifyRegister(wm8904_handle_t *handle, uint8_t reg, uint16_t mask, uint16_t value);
  79. /*******************************************************************************
  80. * Variables
  81. ******************************************************************************/
  82. static const uint8_t allRegisters[] =
  83. {
  84. 0x00, 0x04, 0x05, 0x06, 0x07, 0x0A, 0x0C, 0x0E, 0x0F, 0x12, 0x14, 0x15, 0x16, 0x18, 0x19, 0x1A, 0x1B,
  85. 0x1E, 0x1F, 0x20, 0x21, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x39,
  86. 0x3A, 0x3B, 0x3C, 0x3D, 0x43, 0x44, 0x45, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x5A, 0x5E, 0x62,
  87. 0x68, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7E, 0x7F,
  88. 0x80, 0x81, 0x82, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93,
  89. 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0xC6, 0xF7, 0xF8
  90. };
  91. /*******************************************************************************
  92. * Code
  93. ******************************************************************************/
  94. status_t WM8904_Init(wm8904_handle_t *handle, wm8904_config_t *config)
  95. {
  96. status_t result;
  97. /* reset */
  98. result = WM8904_WriteRegister(handle, WM8904_RESET, 0x0000);
  99. if (result != kStatus_WM8904_Success)
  100. {
  101. return result;
  102. }
  103. /* MCLK_INV=0, SYSCLK_SRC=0, TOCLK_RATE=0, OPCLK_ENA=1,
  104. * CLK_SYS_ENA=1, CLK_DSP_ENA=1, TOCLK_ENA=1 */
  105. result = WM8904_WriteRegister(handle, WM8904_CLK_RATES_2, 0x000F);
  106. if (result != kStatus_WM8904_Success)
  107. {
  108. return result;
  109. }
  110. /* WSEQ_ENA=1, WSEQ_WRITE_INDEX=0_0000 */
  111. result = WM8904_WriteRegister(handle, WM8904_WRT_SEQUENCER_0, 0x0100);
  112. if (result != kStatus_WM8904_Success)
  113. {
  114. return result;
  115. }
  116. /* WSEQ_ABORT=0, WSEQ_START=1, WSEQ_START_INDEX=00_0000 */
  117. result = WM8904_WriteRegister(handle, WM8904_WRT_SEQUENCER_3, 0x0100);
  118. if (result != kStatus_WM8904_Success)
  119. {
  120. return result;
  121. }
  122. result = WM8904_WaitOnWriteSequencer(handle);
  123. if (result != kStatus_WM8904_Success)
  124. {
  125. return result;
  126. }
  127. /* TOCLK_RATE_DIV16=0, TOCLK_RATE_x4=1, SR_MODE=0, MCLK_DIV=1
  128. * (Required for MMCs: SGY, KRT see erratum CE000546) */
  129. result = WM8904_WriteRegister(handle, WM8904_CLK_RATES_0, 0xA45F);
  130. if (result != kStatus_WM8904_Success)
  131. {
  132. return result;
  133. }
  134. /* INL_ENA=1, INR ENA=1 */
  135. result = WM8904_WriteRegister(handle, WM8904_POWER_MGMT_0, 0x0003);
  136. if (result != kStatus_WM8904_Success)
  137. {
  138. return result;
  139. }
  140. /* HPL_PGA_ENA=1, HPR_PGA_ENA=1 */
  141. result = WM8904_WriteRegister(handle, WM8904_POWER_MGMT_2, 0x0003);
  142. if (result != kStatus_WM8904_Success)
  143. {
  144. return result;
  145. }
  146. /* DACL_ENA=1, DACR_ENA=1, ADCL_ENA=1, ADCR_ENA=1 */
  147. result = WM8904_WriteRegister(handle, WM8904_POWER_MGMT_6, 0x000F);
  148. if (result != kStatus_WM8904_Success)
  149. {
  150. return result;
  151. }
  152. /* ADC_OSR128=1 */
  153. result = WM8904_WriteRegister(handle, WM8904_ANALOG_ADC_0, 0x0001);
  154. if (result != kStatus_WM8904_Success)
  155. {
  156. return result;
  157. }
  158. /* CLK_SYS_RAT=0101 (512/fs) SAMPLE_RATE=101 (44.1kHz /48kHz) */
  159. result = WM8904_WriteRegister(handle, WM8904_CLK_RATES_1, 0x1405);
  160. if (result != kStatus_WM8904_Success)
  161. {
  162. return result;
  163. }
  164. /* DACL_DATINV=0, DACR_DATINV=0, DAC_BOOST=00, LOOPBACK=0, AIFADCL_SRC=0,
  165. * AIFADCR_SRC=1, AIFDACL_SRC=0, AIFDACR_SRC=1, ADC_COMP=0, ADC_COMPMODE=0,
  166. * DAC_COMP=0, DAC_COMPMODE=0 */
  167. result = WM8904_WriteRegister(handle, WM8904_AUDIO_IF_0, 0x0050);
  168. if (result != kStatus_WM8904_Success)
  169. {
  170. return result;
  171. }
  172. /* BCLK_DIR=0 (input), AIF_WL=00 (16-bits) */
  173. result = WM8904_WriteRegister(handle, WM8904_AUDIO_IF_1, 0x0002);
  174. if (result != kStatus_WM8904_Success)
  175. {
  176. return result;
  177. }
  178. /* OPCLK_DIV=0 (sysclk), BCLK_DIV=0c (sysclk/16) */
  179. result = WM8904_WriteRegister(handle, WM8904_AUDIO_IF_2, 0x000c);
  180. if (result != kStatus_WM8904_Success)
  181. {
  182. return result;
  183. }
  184. /* LRCLK_DIR=0 (input), LRCLK_RATE=0010_0000_0000 (BCLK / 32) */
  185. result = WM8904_WriteRegister(handle, WM8904_AUDIO_IF_3, 0x0020);
  186. if (result != kStatus_WM8904_Success)
  187. {
  188. return result;
  189. }
  190. /* DAC_MONO=0, DAC_SB_FILT-0, DAC_MUTERATE=0, DAC_UNMUTE RAMP=0,
  191. * DAC_OSR128=1, DAC_MUTE=0, DEEMPH=0 (none) */
  192. result = WM8904_WriteRegister(handle, WM8904_DAC_DIG_1, 0x0040);
  193. if (result != kStatus_WM8904_Success)
  194. {
  195. return result;
  196. }
  197. /* INL_CM_ENA=0, L_IP_SEL_N=10, L_IP_SEL_P=01, L_MODE=00 */
  198. result = WM8904_WriteRegister(handle, WM8904_ANALOG_LEFT_IN_1, 0x0014);
  199. if (result != kStatus_WM8904_Success)
  200. {
  201. return result;
  202. }
  203. /* INR CM_ENA=0, R_IP_SEL_N=10, R_IP_SEL_P=01, R_MODE=00 */
  204. result = WM8904_WriteRegister(handle, WM8904_ANALOG_RIGHT_IN_1, 0x0014);
  205. if (result != kStatus_WM8904_Success)
  206. {
  207. return result;
  208. }
  209. /* LINMUTE=0, LIN_VOL=0_0101 */
  210. result = WM8904_WriteRegister(handle, WM8904_ANALOG_LEFT_IN_0, 0x0005);
  211. if (result != kStatus_WM8904_Success)
  212. {
  213. return result;
  214. }
  215. /* RINMUTE=0, RIN VOL=0_0101 LINEOUTL RMV SHORT-1, LINEOUTL ENA_OUTP=1,
  216. * LINEOUTL_ENA_DLY=1, LINEOUTL_ENA=1, LINEOUTR_RMV_SHORT-1,
  217. * LINEOUTR_ENA_OUTP=1 */
  218. result = WM8904_WriteRegister(handle, WM8904_ANALOG_RIGHT_IN_0, 0x0005);
  219. if (result != kStatus_WM8904_Success)
  220. {
  221. return result;
  222. }
  223. /* HPL_BYP_ENA=0, HPR_BYP_ENA=0, LINEOUTL_BYP ENA=0, LINEOUTR_BYP ENA=0 */
  224. result = WM8904_WriteRegister(handle, WM8904_ANALOG_OUT12_ZC, 0x0000);
  225. if (result != kStatus_WM8904_Success)
  226. {
  227. return result;
  228. }
  229. /* HPOUTL_MUTE=0, HPOUT_VU=0, HPOUTLZC=0, HPOUTL_VOL=11_1001 */
  230. result = WM8904_WriteRegister(handle, WM8904_ANALOG_OUT1_LEFT, 0x0039);
  231. if (result != kStatus_WM8904_Success)
  232. {
  233. return result;
  234. }
  235. /* HPOUTR_MUTE=0, HPOUT_VU=0, HPOUTRZC=0, HPOUTR_VOL=11_1001 */
  236. result = WM8904_WriteRegister(handle, WM8904_ANALOG_OUT1_RIGHT, 0x0039);
  237. if (result != kStatus_WM8904_Success)
  238. {
  239. return result;
  240. }
  241. /* Enable DC servos for headphone out */
  242. result = WM8904_WriteRegister(handle, WM8904_DC_SERVO_0, 0x0003);
  243. if (result != kStatus_WM8904_Success)
  244. {
  245. return result;
  246. }
  247. /* HPL_RMV_SHORT=1, HPL_ENA_OUTP=1, HPL_ENA_DLY=1, HPL_ENA=1,
  248. * HPR_RMV_SHORT=1, HPR_ENA_OUTP=1, HPR_ENA_DLY=1, HPR_ENA=1 */
  249. result = WM8904_WriteRegister(handle, WM8904_ANALOG_HP_0, 0x00FF);
  250. if (result != kStatus_WM8904_Success)
  251. {
  252. return result;
  253. }
  254. /* CP_DYN_PWR=1 */
  255. result = WM8904_WriteRegister(handle, WM8904_CLS_W_0, 0x0001);
  256. if (result != kStatus_WM8904_Success)
  257. {
  258. return result;
  259. }
  260. /* CP_ENA=1 */
  261. result = WM8904_WriteRegister(handle, WM8904_CHRG_PUMP_0, 0x0001);
  262. if (result != kStatus_WM8904_Success)
  263. {
  264. return result;
  265. }
  266. result = WM8904_SetMasterSlave(handle, config->master);
  267. if (result != kStatus_WM8904_Success)
  268. {
  269. return result;
  270. }
  271. result = WM8904_SetProtocol(handle, config->protocol);
  272. if (result != kStatus_WM8904_Success)
  273. {
  274. return result;
  275. }
  276. result = WM8904_SetAudioFormat(handle, &(config->format));
  277. if (result != kStatus_WM8904_Success)
  278. {
  279. return result;
  280. }
  281. return kStatus_WM8904_Success;
  282. }
  283. status_t WM8904_Deinit(wm8904_handle_t *handle)
  284. {
  285. /* reset */
  286. return WM8904_WriteRegister(handle, WM8904_RESET, 0x0000);
  287. }
  288. void WM8904_GetDefaultConfig(wm8904_config_t *config)
  289. {
  290. memset(config, 0, sizeof(wm8904_config_t));
  291. config->master = false;
  292. config->protocol = kWM8904_ProtocolI2S;
  293. config->format.fsRatio = kWM8904_FsRatio64X;
  294. config->format.sampleRate = kWM8904_SampleRate48kHz;
  295. config->format.bitWidth = kWM8904_BitWidth16;
  296. }
  297. status_t WM8904_SetMasterSlave(wm8904_handle_t *handle, bool master)
  298. {
  299. if (master)
  300. {
  301. /* only slave currently supported */
  302. return kStatus_WM8904_Fail;
  303. }
  304. return kStatus_WM8904_Success;
  305. }
  306. status_t WM8904_SetProtocol(wm8904_handle_t *handle, wm8904_protocol_t protocol)
  307. {
  308. return WM8904_ModifyRegister(handle, WM8904_AUDIO_IF_1, 0x0003, (uint16_t)protocol);
  309. }
  310. status_t WM8904_SetAudioFormat(wm8904_handle_t *handle, wm8904_audio_format_t *format)
  311. {
  312. status_t result;
  313. /* Disable SYSCLK */
  314. result = WM8904_WriteRegister(handle, WM8904_CLK_RATES_2, 0x00);
  315. if (result != kStatus_WM8904_Success)
  316. {
  317. return result;
  318. }
  319. /* Set Clock ratio and sample rate */
  320. result = WM8904_WriteRegister(handle, WM8904_CLK_RATES_1, ((uint32_t)format->fsRatio << 10) | format->sampleRate);
  321. if (result != kStatus_WM8904_Success)
  322. {
  323. return result;
  324. }
  325. /* Set bit resolution */
  326. result = WM8904_ModifyRegister(handle, WM8904_AUDIO_IF_1, 0x000C, (uint16_t)format->bitWidth);
  327. if (result != kStatus_WM8904_Success)
  328. {
  329. return result;
  330. }
  331. /* Enable SYSCLK */
  332. result = WM8904_WriteRegister(handle, WM8904_CLK_RATES_2, 0x1007);
  333. if (result != kStatus_WM8904_Success)
  334. {
  335. return result;
  336. }
  337. return kStatus_WM8904_Success;
  338. }
  339. status_t WM8904_SetVolume(wm8904_handle_t *handle, uint16_t volumeLeft, uint16_t volumeRight)
  340. {
  341. status_t result;
  342. result = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT1_LEFT, 0x3F, volumeLeft);
  343. if (result != kStatus_WM8904_Success)
  344. {
  345. return result;
  346. }
  347. result = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT1_RIGHT, 0xBF, volumeRight | 0x0080);
  348. if (result != kStatus_WM8904_Success)
  349. {
  350. return result;
  351. }
  352. return kStatus_WM8904_Success;
  353. }
  354. status_t WM8904_SetMute(wm8904_handle_t *handle, bool muteLeft, bool muteRight)
  355. {
  356. status_t result;
  357. uint16_t left = muteLeft ? 0x0100 : 0x0000;
  358. uint16_t right = muteRight ? 0x0100 : 0x0000;
  359. result = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT1_LEFT, 0x0100, left);
  360. if (result != kStatus_WM8904_Success)
  361. {
  362. return result;
  363. }
  364. result = WM8904_ModifyRegister(handle, WM8904_ANALOG_OUT1_RIGHT, 0x0180, right | 0x0080);
  365. if (result != kStatus_WM8904_Success)
  366. {
  367. return result;
  368. }
  369. return kStatus_WM8904_Success;
  370. }
  371. status_t WM8904_PrintRegisters(wm8904_handle_t *handle)
  372. {
  373. status_t result;
  374. uint16_t value;
  375. uint32_t i;
  376. for (i = 0; i < sizeof(allRegisters); i++)
  377. {
  378. result = WM8904_ReadRegister(handle, allRegisters[i], &value);
  379. if (result != kStatus_WM8904_Success)
  380. {
  381. PRINTF("\r\n");
  382. return result;
  383. }
  384. PRINTF("%s", ((i % 8) == 0) ? "\r\n" : "\t");
  385. PRINTF("%02X:%04X", allRegisters[i], value);
  386. }
  387. PRINTF("\r\n");
  388. return result;
  389. }
  390. static status_t WM8904_WaitOnWriteSequencer(wm8904_handle_t *handle)
  391. {
  392. status_t result;
  393. uint16_t value;
  394. do
  395. {
  396. result = WM8904_ReadRegister(handle, WM8904_WRT_SEQUENCER_4, &value);
  397. }
  398. while ((result == kStatus_WM8904_Success) && (value & 1));
  399. return result;
  400. }
  401. static status_t WM8904_WriteRegister(wm8904_handle_t *handle, uint8_t reg, uint16_t value)
  402. {
  403. rt_size_t result;
  404. struct rt_i2c_msg msgs;
  405. uint8_t buffer[3];
  406. buffer[0] = reg;
  407. buffer[1] = (value >> 8U) & 0xFFU;
  408. buffer[2] = value & 0xFFU;
  409. msgs.addr = WM8904_I2C_ADDRESS;
  410. msgs.flags = RT_I2C_WR;
  411. msgs.buf = buffer;
  412. msgs.len = sizeof(buffer);
  413. result = rt_i2c_transfer(handle->i2c, &msgs, 1);
  414. if (result == 1)
  415. {
  416. return kStatus_WM8904_Success;
  417. }
  418. else
  419. {
  420. return kStatus_WM8904_Fail;
  421. }
  422. }
  423. static status_t WM8904_ReadRegister(wm8904_handle_t *handle, uint8_t reg, uint16_t *value)
  424. {
  425. rt_size_t result;
  426. struct rt_i2c_msg msgs[2];
  427. uint8_t buffer[2] = {0};
  428. uint8_t write_buffer;
  429. *value = 0x0000U;
  430. write_buffer = reg;
  431. msgs[0].addr = WM8904_I2C_ADDRESS;
  432. msgs[0].flags = RT_I2C_WR;
  433. msgs[0].buf = &write_buffer;
  434. msgs[0].len = 1;
  435. msgs[1].addr = WM8904_I2C_ADDRESS;
  436. msgs[1].flags = RT_I2C_RD;
  437. msgs[1].buf = buffer;
  438. msgs[1].len = 2;
  439. result = rt_i2c_transfer(handle->i2c, msgs, 2);
  440. if (result != 2)
  441. {
  442. return kStatus_WM8904_Fail;
  443. }
  444. *value = (uint16_t)((((uint32_t)buffer[0]) << 8U) | ((uint32_t)buffer[1]));
  445. return kStatus_WM8904_Success;
  446. }
  447. static status_t WM8904_ModifyRegister(wm8904_handle_t *handle, uint8_t reg, uint16_t mask, uint16_t value)
  448. {
  449. status_t result;
  450. uint16_t regValue;
  451. result = WM8904_ReadRegister(handle, reg, &regValue);
  452. if (result != kStatus_WM8904_Success)
  453. {
  454. return result;
  455. }
  456. regValue &= (uint16_t)~mask;
  457. regValue |= value;
  458. return WM8904_WriteRegister(handle, reg, regValue);
  459. }