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