2 * rt5631.c -- RT5631 ALSA Soc Audio driver
4 * Copyright 2011 Realtek Microelectronics
6 * Author: flove <flove@realtek.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
20 #include <linux/i2c.h>
21 #include <linux/platform_device.h>
22 #include <linux/spi/spi.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/soc-dapm.h>
28 #include <sound/initval.h>
29 #include <sound/tlv.h>
31 #include "rt5631_phone.h"
33 static struct snd_soc_codec *rt5631_codec;
35 struct rt5631_init_reg {
40 static struct rt5631_init_reg init_list[] = {
41 {RT5631_SPK_OUT_VOL , 0xc8c8},
42 {RT5631_HP_OUT_VOL , 0xc0c0},
43 {RT5631_MONO_AXO_1_2_VOL , 0xa080},
44 {RT5631_ADC_REC_MIXER , 0xb0b0},
45 {RT5631_MIC_CTRL_2 , 0x5588},
47 {RT5631_OUTMIXER_L_CTRL , 0xdfC0},
48 {RT5631_OUTMIXER_R_CTRL , 0xdfC0},
49 {RT5631_SPK_MIXER_CTRL , 0xd8d8},
50 {RT5631_SPK_MONO_OUT_CTRL , 0x6c00},
51 {RT5631_GEN_PUR_CTRL_REG , 0x4e00},
52 //{RT5631_SPK_MONO_HP_OUT_CTRL , 0x0000},
53 {RT5631_SPK_MONO_HP_OUT_CTRL , 0x000c},//HP from DAC,speaker out from SpeakerOut Mixer
54 {RT5631_INT_ST_IRQ_CTRL_2 , 0x0f18},
55 {RT5631_MIC_CTRL_1 , 0x8080},
56 {RT5631_INDEX_ADD , 0x0048},
57 {RT5631_INDEX_DATA , 0xf73c},
59 #define RT5631_INIT_REG_LEN ARRAY_SIZE(init_list)
61 static int rt5631_reg_init(struct snd_soc_codec *codec)
65 for (i = 0; i < RT5631_INIT_REG_LEN; i++)
66 snd_soc_write(codec, init_list[i].reg, init_list[i].val);
71 static int rt5631_index_sync(struct snd_soc_codec *codec)
75 for (i = 0; i < RT5631_INIT_REG_LEN; i++)
76 if (RT5631_INDEX_ADD == init_list[i].reg ||
77 RT5631_INDEX_DATA == init_list[i].reg)
78 snd_soc_write(codec, init_list[i].reg,
85 struct snd_soc_codec *codec;
87 int master[RT5631_AIFS];
88 int sysclk[RT5631_SCLKS];
95 static const u16 rt5631_reg[RT5631_VENDOR_ID2 + 1] = {
96 [RT5631_SPK_OUT_VOL] = 0x8888,
97 [RT5631_HP_OUT_VOL] = 0x8080,
98 [RT5631_MONO_AXO_1_2_VOL] = 0xa080,
99 [RT5631_AUX_IN_VOL] = 0x0808,
100 [RT5631_ADC_REC_MIXER] = 0xf0f0,
101 [RT5631_VDAC_DIG_VOL] = 0x0010,
102 [RT5631_OUTMIXER_L_CTRL] = 0xffc0,
103 [RT5631_OUTMIXER_R_CTRL] = 0xffc0,
104 [RT5631_AXO1MIXER_CTRL] = 0x88c0,
105 [RT5631_AXO2MIXER_CTRL] = 0x88c0,
106 [RT5631_DIG_MIC_CTRL] = 0x3000,
107 [RT5631_MONO_INPUT_VOL] = 0x8808,
108 [RT5631_SPK_MIXER_CTRL] = 0xf8f8,
109 [RT5631_SPK_MONO_OUT_CTRL] = 0xfc00,
110 [RT5631_SPK_MONO_HP_OUT_CTRL] = 0x4440,
111 [RT5631_SDP_CTRL] = 0x8000,
112 [RT5631_MONO_SDP_CTRL] = 0x8000,
113 [RT5631_STEREO_AD_DA_CLK_CTRL] = 0x2010,
114 [RT5631_GEN_PUR_CTRL_REG] = 0x0e00,
115 [RT5631_INT_ST_IRQ_CTRL_2] = 0x0710,
116 [RT5631_MISC_CTRL] = 0x2040,
117 [RT5631_DEPOP_FUN_CTRL_2] = 0x8000,
118 [RT5631_SOFT_VOL_CTRL] = 0x07e0,
119 [RT5631_ALC_CTRL_1] = 0x0206,
120 [RT5631_ALC_CTRL_3] = 0x2000,
121 [RT5631_PSEUDO_SPATL_CTRL] = 0x0553,
124 void rt5631_phone_set_spk(bool on)
126 struct snd_soc_codec *codec = rt5631_codec;
128 printk("%s: %d\n", __func__, on);
133 mutex_lock(&codec->mutex);
135 printk("snd_soc_dapm_enable_pin\n");
136 snd_soc_dapm_enable_pin(&codec->dapm, "Headphone Jack");
137 snd_soc_dapm_enable_pin(&codec->dapm, "Ext Spk");
140 printk("snd_soc_dapm_disable_pin\n");
141 snd_soc_dapm_disable_pin(&codec->dapm, "Headphone Jack");
142 snd_soc_dapm_disable_pin(&codec->dapm, "Ext Spk");
145 snd_soc_dapm_sync(&codec->dapm);
146 mutex_unlock(&codec->mutex);
151 * rt5631_index_write - Write private register.
152 * @codec: SoC audio codec device.
153 * @reg: Private register index.
154 * @value: Private register Data.
156 * Modify private register for advanced setting. It can be written through
157 * private index (0x6a) and data (0x6c) register.
159 * Returns 0 for success or negative error code.
161 static int rt5631_index_write(struct snd_soc_codec *codec,
162 unsigned int reg, unsigned int value)
166 ret = snd_soc_write(codec, RT5631_INDEX_ADD, reg);
168 dev_err(codec->dev, "Failed to set private addr: %d\n", ret);
171 ret = snd_soc_write(codec, RT5631_INDEX_DATA, value);
173 dev_err(codec->dev, "Failed to set private value: %d\n", ret);
183 * rt5631_index_read - Read private register.
184 * @codec: SoC audio codec device.
185 * @reg: Private register index.
187 * Read advanced setting from private register. It can be read through
188 * private index (0x6a) and data (0x6c) register.
190 * Returns private register value or negative error code.
192 static unsigned int rt5631_index_read(
193 struct snd_soc_codec *codec, unsigned int reg)
197 ret = snd_soc_write(codec, RT5631_INDEX_ADD, reg);
199 dev_err(codec->dev, "Failed to set private addr: %d\n", ret);
202 return snd_soc_read(codec, RT5631_INDEX_DATA);
206 * rt5631_index_update_bits - update private register bits
207 * @codec: audio codec
208 * @reg: Private register index.
209 * @mask: register mask
212 * Writes new register value.
214 * Returns 1 for change, 0 for no change, or negative error code.
216 static int rt5631_index_update_bits(struct snd_soc_codec *codec,
217 unsigned int reg, unsigned int mask, unsigned int value)
219 unsigned int old, new;
222 ret = rt5631_index_read(codec, reg);
224 dev_err(codec->dev, "Failed to read private reg: %d\n", ret);
229 new = (old & ~mask) | (value & mask);
232 ret = rt5631_index_write(codec, reg, new);
235 "Failed to write private reg: %d\n", ret);
245 static int rt5631_reset(struct snd_soc_codec *codec)
247 return snd_soc_write(codec, RT5631_RESET, 0);
250 static int rt5631_volatile_register(struct snd_soc_codec *codec,
255 case RT5631_INT_ST_IRQ_CTRL_2:
256 case RT5631_INDEX_ADD:
257 case RT5631_INDEX_DATA:
259 case RT5631_VENDOR_ID:
260 case RT5631_VENDOR_ID1:
261 case RT5631_VENDOR_ID2:
268 static int rt5631_readable_register(struct snd_soc_codec *codec,
273 case RT5631_SPK_OUT_VOL:
274 case RT5631_HP_OUT_VOL:
275 case RT5631_MONO_AXO_1_2_VOL:
276 case RT5631_AUX_IN_VOL:
277 case RT5631_STEREO_DAC_VOL_1:
278 case RT5631_MIC_CTRL_1:
279 case RT5631_STEREO_DAC_VOL_2:
280 case RT5631_ADC_CTRL_1:
281 case RT5631_ADC_REC_MIXER:
282 case RT5631_ADC_CTRL_2:
283 case RT5631_VDAC_DIG_VOL:
284 case RT5631_OUTMIXER_L_CTRL:
285 case RT5631_OUTMIXER_R_CTRL:
286 case RT5631_AXO1MIXER_CTRL:
287 case RT5631_AXO2MIXER_CTRL:
288 case RT5631_MIC_CTRL_2:
289 case RT5631_DIG_MIC_CTRL:
290 case RT5631_MONO_INPUT_VOL:
291 case RT5631_SPK_MIXER_CTRL:
292 case RT5631_SPK_MONO_OUT_CTRL:
293 case RT5631_SPK_MONO_HP_OUT_CTRL:
294 case RT5631_SDP_CTRL:
295 case RT5631_MONO_SDP_CTRL:
296 case RT5631_STEREO_AD_DA_CLK_CTRL:
297 case RT5631_PWR_MANAG_ADD1:
298 case RT5631_PWR_MANAG_ADD2:
299 case RT5631_PWR_MANAG_ADD3:
300 case RT5631_PWR_MANAG_ADD4:
301 case RT5631_GEN_PUR_CTRL_REG:
302 case RT5631_GLOBAL_CLK_CTRL:
303 case RT5631_PLL_CTRL:
304 case RT5631_INT_ST_IRQ_CTRL_1:
305 case RT5631_INT_ST_IRQ_CTRL_2:
306 case RT5631_GPIO_CTRL:
307 case RT5631_MISC_CTRL:
308 case RT5631_DEPOP_FUN_CTRL_1:
309 case RT5631_DEPOP_FUN_CTRL_2:
310 case RT5631_JACK_DET_CTRL:
311 case RT5631_SOFT_VOL_CTRL:
312 case RT5631_ALC_CTRL_1:
313 case RT5631_ALC_CTRL_2:
314 case RT5631_ALC_CTRL_3:
315 case RT5631_PSEUDO_SPATL_CTRL:
316 case RT5631_INDEX_ADD:
317 case RT5631_INDEX_DATA:
319 case RT5631_VENDOR_ID:
320 case RT5631_VENDOR_ID1:
321 case RT5631_VENDOR_ID2:
329 * rt5631_headset_detect - Detect headset.
330 * @codec: SoC audio codec device.
331 * @jack_insert: Jack insert or not.
333 * Detect whether is headset or not when jack inserted.
335 * Returns detect status.
337 int rt5631_headset_detect(struct snd_soc_codec *codec, int jack_insert)
342 snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD2,
343 RT5631_PWR_MICBIAS1_VOL, RT5631_PWR_MICBIAS1_VOL);
344 snd_soc_update_bits(codec, RT5631_MIC_CTRL_2,
345 RT5631_MICBIAS1_S_C_DET_MASK |
346 RT5631_MICBIAS1_SHORT_CURR_DET_MASK,
347 RT5631_MICBIAS1_S_C_DET_ENA |
348 RT5631_MICBIAS1_SHORT_CURR_DET_600UA);
350 if (rt5631_index_read(codec, 0x4a) & 0x2)
351 jack_type = RT5631_HEADPHO_DET;
353 jack_type = RT5631_HEADSET_DET;
355 snd_soc_update_bits(codec, RT5631_MIC_CTRL_2,
356 RT5631_MICBIAS1_S_C_DET_MASK,
357 RT5631_MICBIAS1_S_C_DET_DIS);
358 jack_type = RT5631_NO_JACK;
363 EXPORT_SYMBOL(rt5631_headset_detect);
365 int rt5631_headset_mic_detect(bool headset_status)
367 struct rt5631_priv *rt5631 = NULL;
369 printk("%s::%d headset_status=%d\n",__FUNCTION__,__LINE__,headset_status);
371 if(rt5631_codec == NULL)
373 rt5631 = snd_soc_codec_get_drvdata(rt5631_codec);
378 while(rt5631_codec->dapm.bias_level == SND_SOC_BIAS_OFF)
380 printk("----------rt5631 unnot standby-----------------\n");
383 snd_soc_update_bits(rt5631_codec, RT5631_PWR_MANAG_ADD3,
384 RT5631_PWR_VREF|RT5631_PWR_MAIN_BIAS ,
385 RT5631_PWR_VREF|RT5631_PWR_MAIN_BIAS);
387 snd_soc_update_bits(rt5631_codec, RT5631_PWR_MANAG_ADD2,
388 RT5631_PWR_MICBIAS2_VOL ,
389 RT5631_PWR_MICBIAS2_VOL);
393 {// headset is out,disable MIC2 Bias
394 printk("headset is out,disable Mic2 Bias\n");
395 snd_soc_update_bits(rt5631_codec, RT5631_PWR_MANAG_ADD2,
396 RT5631_PWR_MICBIAS2_VOL ,
401 EXPORT_SYMBOL(rt5631_headset_mic_detect);
403 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
404 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -95625, 375, 0);
405 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
406 /* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
407 static unsigned int mic_bst_tlv[] = {
408 TLV_DB_RANGE_HEAD(7),
409 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
410 1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
411 2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
412 3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
413 6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
414 7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
415 8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0),
418 static int rt5631_dmic_get(struct snd_kcontrol *kcontrol,
419 struct snd_ctl_elem_value *ucontrol)
421 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
422 struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
424 ucontrol->value.integer.value[0] = rt5631->dmic_used_flag;
429 static int rt5631_dmic_put(struct snd_kcontrol *kcontrol,
430 struct snd_ctl_elem_value *ucontrol)
432 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
433 struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
435 rt5631->dmic_used_flag = ucontrol->value.integer.value[0];
440 static const char *rt5631_input_mode[] = {
441 "Single ended", "Differential"};
443 static const SOC_ENUM_SINGLE_DECL(
444 rt5631_mic1_mode_enum, RT5631_MIC_CTRL_1,
445 RT5631_MIC1_DIFF_INPUT_SHIFT, rt5631_input_mode);
447 static const SOC_ENUM_SINGLE_DECL(
448 rt5631_mic2_mode_enum, RT5631_MIC_CTRL_1,
449 RT5631_MIC2_DIFF_INPUT_SHIFT, rt5631_input_mode);
451 /* MONO Input Type */
452 static const SOC_ENUM_SINGLE_DECL(
453 rt5631_monoin_mode_enum, RT5631_MONO_INPUT_VOL,
454 RT5631_MONO_DIFF_INPUT_SHIFT, rt5631_input_mode);
456 /* SPK Ratio Gain Control */
457 static const char *rt5631_spk_ratio[] = {"1.00x", "1.09x", "1.27x", "1.44x",
458 "1.56x", "1.68x", "1.99x", "2.34x"};
460 static const SOC_ENUM_SINGLE_DECL(
461 rt5631_spk_ratio_enum, RT5631_GEN_PUR_CTRL_REG,
462 RT5631_SPK_AMP_RATIO_CTRL_SHIFT, rt5631_spk_ratio);
464 /* ADC Data Select Control */
465 static const char *rt5631_adc_data_sel[] = {"Stereo", "Left ADC", "Right ADC"};
467 static const SOC_ENUM_SINGLE_DECL(
468 rt5631_adc_data_enum, RT5631_INT_ST_IRQ_CTRL_2,
469 RT5631_ADC_DATA_SEL_SHIFT, rt5631_adc_data_sel);
471 /* ADCR Function Select Control */
472 static const char *rt5631_adcr_fun_sel[] = {"Stereo ADC", "Voice ADC"};
474 static const SOC_ENUM_SINGLE_DECL(
475 rt5631_adcr_fun_enum, RT5631_GLOBAL_CLK_CTRL,
476 RT5631_ADCR_FUN_SFT, rt5631_adcr_fun_sel);
478 /* VDAC Clock Select Control */
479 static const char *rt5631_vdac_clk_sel[] = {"SYSCLK1", "SYSCLK2"};
481 static const SOC_ENUM_SINGLE_DECL(
482 rt5631_vdac_clk_enum, RT5631_GLOBAL_CLK_CTRL,
483 RT5631_VDAC_CLK_SOUR_SFT, rt5631_vdac_clk_sel);
485 static const struct snd_kcontrol_new rt5631_snd_controls[] = {
487 SOC_ENUM("MIC1 Mode Control", rt5631_mic1_mode_enum),
488 SOC_SINGLE_TLV("MIC1 Boost", RT5631_MIC_CTRL_2,
489 RT5631_MIC1_BOOST_SHIFT, 8, 0, mic_bst_tlv),
490 SOC_ENUM("MIC2 Mode Control", rt5631_mic2_mode_enum),
491 SOC_SINGLE_TLV("MIC2 Boost", RT5631_MIC_CTRL_2,
492 RT5631_MIC2_BOOST_SHIFT, 8, 0, mic_bst_tlv),
494 SOC_ENUM("MONOIN Mode Control", rt5631_monoin_mode_enum),
495 SOC_DOUBLE_TLV("MONOIN_RX Capture Volume", RT5631_MONO_INPUT_VOL,
496 RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT,
497 RT5631_VOL_MASK, 1, in_vol_tlv),
499 SOC_DOUBLE_TLV("AXI Capture Volume", RT5631_AUX_IN_VOL,
500 RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT,
501 RT5631_VOL_MASK, 1, in_vol_tlv),
503 SOC_DOUBLE_TLV("PCM Record Volume", RT5631_ADC_CTRL_2,
504 RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT,
505 RT5631_DAC_VOL_MASK, 1, dac_vol_tlv),
506 SOC_DOUBLE("PCM Record Switch", RT5631_ADC_CTRL_1,
507 RT5631_L_MUTE_SHIFT, RT5631_R_MUTE_SHIFT, 1, 1),
509 SOC_DOUBLE_TLV("PCM Playback Volume", RT5631_STEREO_DAC_VOL_2,
510 RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT,
511 RT5631_DAC_VOL_MASK, 1, dac_vol_tlv),
512 SOC_DOUBLE("PCM Playback Switch", RT5631_STEREO_DAC_VOL_1,
513 RT5631_L_MUTE_SHIFT, RT5631_R_MUTE_SHIFT, 1, 1),
515 SOC_SINGLE("AXO1 Playback Switch", RT5631_MONO_AXO_1_2_VOL,
516 RT5631_L_MUTE_SHIFT, 1, 1),
517 SOC_SINGLE("AXO2 Playback Switch", RT5631_MONO_AXO_1_2_VOL,
518 RT5631_R_VOL_SHIFT, 1, 1),
519 SOC_SINGLE("AXO Playback Switch", RT5631_MONO_AXO_1_2_VOL,
520 RT5631_L_MUTE_SHIFT, 1, 1),
522 SOC_DOUBLE("OUTVOL Channel Switch", RT5631_SPK_OUT_VOL,
523 RT5631_L_EN_SHIFT, RT5631_R_EN_SHIFT, 1, 0),
526 SOC_DOUBLE("Speaker Playback Switch", RT5631_SPK_OUT_VOL,
527 RT5631_L_MUTE_SHIFT, RT5631_R_MUTE_SHIFT, 1, 1),
528 SOC_DOUBLE_TLV("Speaker Playback Volume", RT5631_SPK_OUT_VOL,
529 RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT, 39, 1, out_vol_tlv),
531 SOC_SINGLE("MONO Playback Switch", RT5631_MONO_AXO_1_2_VOL,
532 RT5631_MUTE_MONO_SHIFT, 1, 1),
534 SOC_DOUBLE("HP Playback Switch", RT5631_HP_OUT_VOL,
535 RT5631_L_MUTE_SHIFT, RT5631_R_MUTE_SHIFT, 1, 1),
536 SOC_DOUBLE_TLV("HP Playback Volume", RT5631_HP_OUT_VOL,
537 RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT,
538 RT5631_VOL_MASK, 1, out_vol_tlv),
540 SOC_DOUBLE_TLV("Speaker Volume", RT5631_SPK_OUT_VOL,
541 RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT, 39, 1, out_vol_tlv),
542 SOC_DOUBLE_TLV("Earpiece Volume", RT5631_SPK_OUT_VOL,
543 RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT, 39, 1, out_vol_tlv),
544 SOC_DOUBLE_TLV("Headphone Volume", RT5631_HP_OUT_VOL,
545 RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT,
546 RT5631_VOL_MASK, 1, out_vol_tlv),
549 SOC_SINGLE_EXT("DMIC Switch", 0, 0, 1, 0,
550 rt5631_dmic_get, rt5631_dmic_put),
551 SOC_DOUBLE("DMIC Capture Switch", RT5631_DIG_MIC_CTRL,
552 RT5631_DMIC_L_CH_MUTE_SHIFT,
553 RT5631_DMIC_R_CH_MUTE_SHIFT, 1, 1),
554 /* SPK Ratio Gain Control */
555 SOC_ENUM("SPK Ratio Control", rt5631_spk_ratio_enum),
556 /* ADC Data Select Control */
557 SOC_ENUM("ADC Data Select", rt5631_adc_data_enum),
558 /* ADCR Function Select Control */
559 SOC_ENUM("ADCR Function Select", rt5631_adcr_fun_enum),
560 /* VDAC Function Select Control */
561 SOC_ENUM("Voice DAC Clock Select", rt5631_vdac_clk_enum),
564 static int check_sysclk1_source(struct snd_soc_dapm_widget *source,
565 struct snd_soc_dapm_widget *sink)
569 reg = snd_soc_read(source->codec, RT5631_GLOBAL_CLK_CTRL);
570 return (reg & RT5631_SYSCLK_SOUR_SEL_PLL);
573 static int check_adcr_pll1(struct snd_soc_dapm_widget *source,
574 struct snd_soc_dapm_widget *sink)
576 unsigned int reg = snd_soc_read(source->codec, RT5631_GLOBAL_CLK_CTRL);
578 if (reg & RT5631_ADCR_FUN_VADC) {
579 if (reg & RT5631_VDAC_CLK_SOUR_SCLK2)
582 return (reg & RT5631_SYSCLK_SOUR_SEL_PLL);
584 return (reg & RT5631_SYSCLK_SOUR_SEL_PLL);
587 static int check_adcr_stereo(struct snd_soc_dapm_widget *source,
588 struct snd_soc_dapm_widget *sink)
590 unsigned int reg = snd_soc_read(source->codec, RT5631_GLOBAL_CLK_CTRL);
591 return (RT5631_ADCR_FUN_ADC == (reg & RT5631_ADCR_FUN_MASK));
594 static int check_adcr_voice(struct snd_soc_dapm_widget *source,
595 struct snd_soc_dapm_widget *sink)
597 unsigned int reg = snd_soc_read(source->codec, RT5631_GLOBAL_CLK_CTRL);
598 return (RT5631_ADCR_FUN_VADC == (reg & RT5631_ADCR_FUN_MASK));
601 static int check_adcr_pll2(struct snd_soc_dapm_widget *source,
602 struct snd_soc_dapm_widget *sink)
604 unsigned int reg = snd_soc_read(source->codec, RT5631_GLOBAL_CLK_CTRL);
606 if (reg & RT5631_ADCR_FUN_VADC) {
607 if (reg & RT5631_VDAC_CLK_SOUR_SCLK2)
608 return (reg & RT5631_SYSCLK2_SOUR_SEL_PLL2);
615 static int check_vdac_pll1(struct snd_soc_dapm_widget *source,
616 struct snd_soc_dapm_widget *sink)
618 unsigned int reg = snd_soc_read(source->codec, RT5631_GLOBAL_CLK_CTRL);
620 if (reg & RT5631_VDAC_CLK_SOUR_SCLK2)
623 return (reg & RT5631_SYSCLK_SOUR_SEL_PLL);
626 static int check_vdac_pll2(struct snd_soc_dapm_widget *source,
627 struct snd_soc_dapm_widget *sink)
629 unsigned int reg = snd_soc_read(source->codec, RT5631_GLOBAL_CLK_CTRL);
631 if (reg & RT5631_VDAC_CLK_SOUR_SCLK2)
632 return (reg & RT5631_SYSCLK2_SOUR_SEL_PLL2);
637 static int check_dmic_used(struct snd_soc_dapm_widget *source,
638 struct snd_soc_dapm_widget *sink)
640 struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(source->codec);
641 return rt5631->dmic_used_flag;
644 static int check_dacl_to_outmixl(struct snd_soc_dapm_widget *source,
645 struct snd_soc_dapm_widget *sink)
649 reg = snd_soc_read(source->codec, RT5631_OUTMIXER_L_CTRL);
650 return !(reg & RT5631_M_DAC_L_TO_OUTMIXER_L);
653 static int check_dacr_to_outmixr(struct snd_soc_dapm_widget *source,
654 struct snd_soc_dapm_widget *sink)
658 reg = snd_soc_read(source->codec, RT5631_OUTMIXER_R_CTRL);
659 return !(reg & RT5631_M_DAC_R_TO_OUTMIXER_R);
662 static int check_dacl_to_spkmixl(struct snd_soc_dapm_widget *source,
663 struct snd_soc_dapm_widget *sink)
667 reg = snd_soc_read(source->codec, RT5631_SPK_MIXER_CTRL);
668 return !(reg & RT5631_M_DAC_L_TO_SPKMIXER_L);
671 static int check_dacr_to_spkmixr(struct snd_soc_dapm_widget *source,
672 struct snd_soc_dapm_widget *sink)
676 reg = snd_soc_read(source->codec, RT5631_SPK_MIXER_CTRL);
677 return !(reg & RT5631_M_DAC_R_TO_SPKMIXER_R);
680 static int check_vdac_to_outmix(struct snd_soc_dapm_widget *source,
681 struct snd_soc_dapm_widget *sink)
683 unsigned int reg, ret = 1;
685 reg = snd_soc_read(source->codec, RT5631_OUTMIXER_L_CTRL);
686 if (reg & RT5631_M_VDAC_TO_OUTMIXER_L) {
687 reg = snd_soc_read(source->codec, RT5631_OUTMIXER_R_CTRL);
688 if (reg & RT5631_M_VDAC_TO_OUTMIXER_R)
694 static int check_adcl_select(struct snd_soc_dapm_widget *source,
695 struct snd_soc_dapm_widget *sink)
697 unsigned int reg, ret = 0;
699 reg = snd_soc_read(source->codec, RT5631_ADC_REC_MIXER);
700 if (reg & RT5631_M_MIC2_TO_RECMIXER_R)
701 if (!(reg & RT5631_M_MIC1_TO_RECMIXER_L))
706 static int check_adcr_select(struct snd_soc_dapm_widget *source,
707 struct snd_soc_dapm_widget *sink)
709 unsigned int reg, ret = 0;
711 reg = snd_soc_read(source->codec, RT5631_ADC_REC_MIXER);
712 if (reg & RT5631_M_MIC1_TO_RECMIXER_L)
713 if (!(reg & RT5631_M_MIC2_TO_RECMIXER_R))
719 * onebit_depop_power_stage - auto depop in power stage.
720 * @enable: power on/off
722 * When power on/off headphone, the depop sequence is done by hardware.
724 static void onebit_depop_power_stage(struct snd_soc_codec *codec, int enable)
726 unsigned int soft_vol, hp_zc;
728 /* enable one-bit depop function */
729 snd_soc_update_bits(codec, RT5631_DEPOP_FUN_CTRL_2,
730 RT5631_EN_ONE_BIT_DEPOP, 0);
732 /* keep soft volume and zero crossing setting */
733 soft_vol = snd_soc_read(codec, RT5631_SOFT_VOL_CTRL);
734 snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, 0);
735 hp_zc = snd_soc_read(codec, RT5631_INT_ST_IRQ_CTRL_2);
736 snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc & 0xf7ff);
738 /* config one-bit depop parameter */
739 rt5631_index_write(codec, RT5631_TEST_MODE_CTRL, 0x84c0);
740 rt5631_index_write(codec, RT5631_SPK_INTL_CTRL, 0x309f);
741 rt5631_index_write(codec, RT5631_CP_INTL_REG2, 0x6530);
742 /* power on capless block */
743 snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_2,
744 RT5631_EN_CAP_FREE_DEPOP);
746 /* power off capless block */
747 snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_2, 0);
751 /* recover soft volume and zero crossing setting */
752 snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, soft_vol);
753 snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc);
757 * onebit_depop_mute_stage - auto depop in mute stage.
758 * @enable: mute/unmute
760 * When mute/unmute headphone, the depop sequence is done by hardware.
762 static void onebit_depop_mute_stage(struct snd_soc_codec *codec, int enable)
764 unsigned int soft_vol, hp_zc;
766 /* enable one-bit depop function */
767 snd_soc_update_bits(codec, RT5631_DEPOP_FUN_CTRL_2,
768 RT5631_EN_ONE_BIT_DEPOP, 0);
770 /* keep soft volume and zero crossing setting */
771 soft_vol = snd_soc_read(codec, RT5631_SOFT_VOL_CTRL);
772 snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, 0);
773 hp_zc = snd_soc_read(codec, RT5631_INT_ST_IRQ_CTRL_2);
774 snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc & 0xf7ff);
776 schedule_timeout_uninterruptible(msecs_to_jiffies(10));
777 /* config one-bit depop parameter */
778 rt5631_index_write(codec, RT5631_SPK_INTL_CTRL, 0x307f);
779 snd_soc_update_bits(codec, RT5631_HP_OUT_VOL,
780 RT5631_L_MUTE | RT5631_R_MUTE, 0);
783 snd_soc_update_bits(codec, RT5631_HP_OUT_VOL,
784 RT5631_L_MUTE | RT5631_R_MUTE,
785 RT5631_L_MUTE | RT5631_R_MUTE);
789 /* recover soft volume and zero crossing setting */
790 snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, soft_vol);
791 snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc);
795 * onebit_depop_power_stage - step by step depop sequence in power stage.
796 * @enable: power on/off
798 * When power on/off headphone, the depop sequence is done in step by step.
800 static void depop_seq_power_stage(struct snd_soc_codec *codec, int enable)
802 unsigned int soft_vol, hp_zc;
804 /* depop control by register */
805 snd_soc_update_bits(codec, RT5631_DEPOP_FUN_CTRL_2,
806 RT5631_EN_ONE_BIT_DEPOP, RT5631_EN_ONE_BIT_DEPOP);
808 /* keep soft volume and zero crossing setting */
809 soft_vol = snd_soc_read(codec, RT5631_SOFT_VOL_CTRL);
810 snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, 0);
811 hp_zc = snd_soc_read(codec, RT5631_INT_ST_IRQ_CTRL_2);
812 snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc & 0xf7ff);
814 /* config depop sequence parameter */
815 rt5631_index_write(codec, RT5631_SPK_INTL_CTRL, 0x303e);
817 /* power on headphone and charge pump */
818 snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
819 RT5631_PWR_CHARGE_PUMP | RT5631_PWR_HP_L_AMP |
821 RT5631_PWR_CHARGE_PUMP | RT5631_PWR_HP_L_AMP |
822 RT5631_PWR_HP_R_AMP);
824 /* power on soft generator and depop mode2 */
825 snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
826 RT5631_POW_ON_SOFT_GEN | RT5631_EN_DEPOP2_FOR_HP);
829 /* stop depop mode */
830 snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
831 RT5631_PWR_HP_DEPOP_DIS, RT5631_PWR_HP_DEPOP_DIS);
833 /* config depop sequence parameter */
834 rt5631_index_write(codec, RT5631_SPK_INTL_CTRL, 0x303F);
835 snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
836 RT5631_POW_ON_SOFT_GEN | RT5631_EN_MUTE_UNMUTE_DEPOP |
837 RT5631_PD_HPAMP_L_ST_UP | RT5631_PD_HPAMP_R_ST_UP);
839 snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
840 RT5631_POW_ON_SOFT_GEN | RT5631_PD_HPAMP_L_ST_UP |
841 RT5631_PD_HPAMP_R_ST_UP);
843 /* start depop mode */
844 snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
845 RT5631_PWR_HP_DEPOP_DIS, 0);
847 /* config depop sequence parameter */
848 snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
849 RT5631_POW_ON_SOFT_GEN | RT5631_EN_DEPOP2_FOR_HP |
850 RT5631_PD_HPAMP_L_ST_UP | RT5631_PD_HPAMP_R_ST_UP);
852 snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
853 RT5631_POW_ON_SOFT_GEN);
855 /* power down headphone and charge pump */
856 snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
857 RT5631_PWR_CHARGE_PUMP | RT5631_PWR_HP_L_AMP |
858 RT5631_PWR_HP_R_AMP, 0);
861 /* recover soft volume and zero crossing setting */
862 snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, soft_vol);
863 snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc);
867 * depop_seq_mute_stage - step by step depop sequence in mute stage.
868 * @enable: mute/unmute
870 * When mute/unmute headphone, the depop sequence is done in step by step.
872 static void depop_seq_mute_stage(struct snd_soc_codec *codec, int enable)
874 unsigned int soft_vol, hp_zc;
876 /* depop control by register */
877 snd_soc_update_bits(codec, RT5631_DEPOP_FUN_CTRL_2,
878 RT5631_EN_ONE_BIT_DEPOP, RT5631_EN_ONE_BIT_DEPOP);
880 /* keep soft volume and zero crossing setting */
881 soft_vol = snd_soc_read(codec, RT5631_SOFT_VOL_CTRL);
882 snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, 0);
883 hp_zc = snd_soc_read(codec, RT5631_INT_ST_IRQ_CTRL_2);
884 snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc & 0xf7ff);
886 schedule_timeout_uninterruptible(msecs_to_jiffies(10));
888 /* config depop sequence parameter */
889 rt5631_index_write(codec, RT5631_SPK_INTL_CTRL, 0x302f);
890 snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
891 RT5631_POW_ON_SOFT_GEN | RT5631_EN_MUTE_UNMUTE_DEPOP |
892 RT5631_EN_HP_R_M_UN_MUTE_DEPOP |
893 RT5631_EN_HP_L_M_UN_MUTE_DEPOP);
895 snd_soc_update_bits(codec, RT5631_HP_OUT_VOL,
896 RT5631_L_MUTE | RT5631_R_MUTE, 0);
899 /* config depop sequence parameter */
900 rt5631_index_write(codec, RT5631_SPK_INTL_CTRL, 0x302f);
901 snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
902 RT5631_POW_ON_SOFT_GEN | RT5631_EN_MUTE_UNMUTE_DEPOP |
903 RT5631_EN_HP_R_M_UN_MUTE_DEPOP |
904 RT5631_EN_HP_L_M_UN_MUTE_DEPOP);
906 snd_soc_update_bits(codec, RT5631_HP_OUT_VOL,
907 RT5631_L_MUTE | RT5631_R_MUTE,
908 RT5631_L_MUTE | RT5631_R_MUTE);
912 /* recover soft volume and zero crossing setting */
913 snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, soft_vol);
914 snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc);
917 static int hp_event(struct snd_soc_dapm_widget *w,
918 struct snd_kcontrol *kcontrol, int event)
920 struct snd_soc_codec *codec = w->codec;
921 struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
924 case SND_SOC_DAPM_PRE_PMD:
925 // printk("hp_event before widget power down\n");
926 if (rt5631->codec_version) {
927 onebit_depop_mute_stage(codec, 0);
928 onebit_depop_power_stage(codec, 0);
930 depop_seq_mute_stage(codec, 0);
931 depop_seq_power_stage(codec, 0);
935 case SND_SOC_DAPM_POST_PMU:
936 if (rt5631->codec_version) {
937 onebit_depop_power_stage(codec, 1);
938 onebit_depop_mute_stage(codec, 1);
940 depop_seq_power_stage(codec, 1);
941 depop_seq_mute_stage(codec, 1);
952 static int spk_event(struct snd_soc_dapm_widget *w,
953 struct snd_kcontrol *kcontrol, int event)
955 struct snd_soc_codec *codec = w->codec;
956 printk("Enter %s::%s---%d\n",__FILE__,__FUNCTION__,__LINE__);
959 case SND_SOC_DAPM_POST_PMU:
963 case SND_SOC_DAPM_POST_PMD:
972 static int auxo_event(struct snd_soc_dapm_widget *w,
973 struct snd_kcontrol *control, int event)
975 // struct snd_soc_codec *codec = w->codec;
976 // struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
977 printk("Enter %s::%s---%d\n",__FILE__,__FUNCTION__,__LINE__);
978 #ifdef CONFIG_PHONE_INCALL_IS_SUSPEND
981 case SND_SOC_DAPM_POST_PMU:
982 printk("rt5631v is incall status\n");
983 snd_soc_incall_status(1,1);
986 case SND_SOC_DAPM_PRE_PMD:
987 printk("rt5631v exit incall status\n");
988 snd_soc_incall_status(1,0);
999 static int set_dmic_params(struct snd_soc_dapm_widget *w,
1000 struct snd_kcontrol *kcontrol, int event)
1002 struct snd_soc_codec *codec = w->codec;
1003 struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
1005 switch (rt5631->rx_rate) {
1008 snd_soc_update_bits(codec, RT5631_DIG_MIC_CTRL,
1009 RT5631_DMIC_CLK_CTRL_MASK,
1010 RT5631_DMIC_CLK_CTRL_TO_32FS);
1015 snd_soc_update_bits(codec, RT5631_DIG_MIC_CTRL,
1016 RT5631_DMIC_CLK_CTRL_MASK,
1017 RT5631_DMIC_CLK_CTRL_TO_64FS);
1023 snd_soc_update_bits(codec, RT5631_DIG_MIC_CTRL,
1024 RT5631_DMIC_CLK_CTRL_MASK,
1025 RT5631_DMIC_CLK_CTRL_TO_128FS);
1035 static const struct snd_kcontrol_new rt5631_recmixl_mixer_controls[] = {
1036 SOC_DAPM_SINGLE("OUTMIXL Capture Switch", RT5631_ADC_REC_MIXER,
1037 RT5631_M_OUTMIXL_RECMIXL_BIT, 1, 1),
1038 SOC_DAPM_SINGLE("MIC1_BST1 Capture Switch", RT5631_ADC_REC_MIXER,
1039 RT5631_M_MIC1_RECMIXL_BIT, 1, 1),
1040 SOC_DAPM_SINGLE("AXILVOL Capture Switch", RT5631_ADC_REC_MIXER,
1041 RT5631_M_AXIL_RECMIXL_BIT, 1, 1),
1042 SOC_DAPM_SINGLE("MONOIN_RX Capture Switch", RT5631_ADC_REC_MIXER,
1043 RT5631_M_MONO_IN_RECMIXL_BIT, 1, 1),
1046 static const struct snd_kcontrol_new rt5631_recmixr_mixer_controls[] = {
1047 SOC_DAPM_SINGLE("MONOIN_RX Capture Switch", RT5631_ADC_REC_MIXER,
1048 RT5631_M_MONO_IN_RECMIXR_BIT, 1, 1),
1049 SOC_DAPM_SINGLE("AXIRVOL Capture Switch", RT5631_ADC_REC_MIXER,
1050 RT5631_M_AXIR_RECMIXR_BIT, 1, 1),
1051 SOC_DAPM_SINGLE("MIC2_BST2 Capture Switch", RT5631_ADC_REC_MIXER,
1052 RT5631_M_MIC2_RECMIXR_BIT, 1, 1),
1053 SOC_DAPM_SINGLE("OUTMIXR Capture Switch", RT5631_ADC_REC_MIXER,
1054 RT5631_M_OUTMIXR_RECMIXR_BIT, 1, 1),
1057 static const struct snd_kcontrol_new rt5631_spkmixl_mixer_controls[] = {
1058 SOC_DAPM_SINGLE("RECMIXL Playback Switch", RT5631_SPK_MIXER_CTRL,
1059 RT5631_M_RECMIXL_SPKMIXL_BIT, 1, 1),
1060 SOC_DAPM_SINGLE("MIC1_P Playback Switch", RT5631_SPK_MIXER_CTRL,
1061 RT5631_M_MIC1P_SPKMIXL_BIT, 1, 1),
1062 SOC_DAPM_SINGLE("DACL Playback Switch", RT5631_SPK_MIXER_CTRL,
1063 RT5631_M_DACL_SPKMIXL_BIT, 1, 1),
1064 SOC_DAPM_SINGLE("OUTMIXL Playback Switch", RT5631_SPK_MIXER_CTRL,
1065 RT5631_M_OUTMIXL_SPKMIXL_BIT, 1, 1),
1068 static const struct snd_kcontrol_new rt5631_spkmixr_mixer_controls[] = {
1069 SOC_DAPM_SINGLE("OUTMIXR Playback Switch", RT5631_SPK_MIXER_CTRL,
1070 RT5631_M_OUTMIXR_SPKMIXR_BIT, 1, 1),
1071 SOC_DAPM_SINGLE("DACR Playback Switch", RT5631_SPK_MIXER_CTRL,
1072 RT5631_M_DACR_SPKMIXR_BIT, 1, 1),
1073 SOC_DAPM_SINGLE("MIC2_P Playback Switch", RT5631_SPK_MIXER_CTRL,
1074 RT5631_M_MIC2P_SPKMIXR_BIT, 1, 1),
1075 SOC_DAPM_SINGLE("RECMIXR Playback Switch", RT5631_SPK_MIXER_CTRL,
1076 RT5631_M_RECMIXR_SPKMIXR_BIT, 1, 1),
1079 static const struct snd_kcontrol_new rt5631_outmixl_mixer_controls[] = {
1080 SOC_DAPM_SINGLE("RECMIXL Playback Switch", RT5631_OUTMIXER_L_CTRL,
1081 RT5631_M_RECMIXL_OUTMIXL_BIT, 1, 1),
1082 SOC_DAPM_SINGLE("RECMIXR Playback Switch", RT5631_OUTMIXER_L_CTRL,
1083 RT5631_M_RECMIXR_OUTMIXL_BIT, 1, 1),
1084 SOC_DAPM_SINGLE("DACL Playback Switch", RT5631_OUTMIXER_L_CTRL,
1085 RT5631_M_DACL_OUTMIXL_BIT, 1, 1),
1086 SOC_DAPM_SINGLE("MIC1_BST1 Playback Switch", RT5631_OUTMIXER_L_CTRL,
1087 RT5631_M_MIC1_OUTMIXL_BIT, 1, 1),
1088 SOC_DAPM_SINGLE("MIC2_BST2 Playback Switch", RT5631_OUTMIXER_L_CTRL,
1089 RT5631_M_MIC2_OUTMIXL_BIT, 1, 1),
1090 SOC_DAPM_SINGLE("MONOIN_RXP Playback Switch", RT5631_OUTMIXER_L_CTRL,
1091 RT5631_M_MONO_INP_OUTMIXL_BIT, 1, 1),
1092 SOC_DAPM_SINGLE("AXILVOL Playback Switch", RT5631_OUTMIXER_L_CTRL,
1093 RT5631_M_AXIL_OUTMIXL_BIT, 1, 1),
1094 SOC_DAPM_SINGLE("AXIRVOL Playback Switch", RT5631_OUTMIXER_L_CTRL,
1095 RT5631_M_AXIR_OUTMIXL_BIT, 1, 1),
1096 SOC_DAPM_SINGLE("VDAC Playback Switch", RT5631_OUTMIXER_L_CTRL,
1097 RT5631_M_VDAC_OUTMIXL_BIT, 1, 1),
1100 static const struct snd_kcontrol_new rt5631_outmixr_mixer_controls[] = {
1101 SOC_DAPM_SINGLE("VDAC Playback Switch", RT5631_OUTMIXER_R_CTRL,
1102 RT5631_M_VDAC_OUTMIXR_BIT, 1, 1),
1103 SOC_DAPM_SINGLE("AXIRVOL Playback Switch", RT5631_OUTMIXER_R_CTRL,
1104 RT5631_M_AXIR_OUTMIXR_BIT, 1, 1),
1105 SOC_DAPM_SINGLE("AXILVOL Playback Switch", RT5631_OUTMIXER_R_CTRL,
1106 RT5631_M_AXIL_OUTMIXR_BIT, 1, 1),
1107 SOC_DAPM_SINGLE("MONOIN_RXN Playback Switch", RT5631_OUTMIXER_R_CTRL,
1108 RT5631_M_MONO_INN_OUTMIXR_BIT, 1, 1),
1109 SOC_DAPM_SINGLE("MIC2_BST2 Playback Switch", RT5631_OUTMIXER_R_CTRL,
1110 RT5631_M_MIC2_OUTMIXR_BIT, 1, 1),
1111 SOC_DAPM_SINGLE("MIC1_BST1 Playback Switch", RT5631_OUTMIXER_R_CTRL,
1112 RT5631_M_MIC1_OUTMIXR_BIT, 1, 1),
1113 SOC_DAPM_SINGLE("DACR Playback Switch", RT5631_OUTMIXER_R_CTRL,
1114 RT5631_M_DACR_OUTMIXR_BIT, 1, 1),
1115 SOC_DAPM_SINGLE("RECMIXR Playback Switch", RT5631_OUTMIXER_R_CTRL,
1116 RT5631_M_RECMIXR_OUTMIXR_BIT, 1, 1),
1117 SOC_DAPM_SINGLE("RECMIXL Playback Switch", RT5631_OUTMIXER_R_CTRL,
1118 RT5631_M_RECMIXL_OUTMIXR_BIT, 1, 1),
1121 static const struct snd_kcontrol_new rt5631_AXO1MIX_mixer_controls[] = {
1122 SOC_DAPM_SINGLE("MIC1_BST1 Playback Switch", RT5631_AXO1MIXER_CTRL,
1123 RT5631_M_MIC1_AXO1MIX_BIT , 1, 1),
1124 SOC_DAPM_SINGLE("MIC2_BST2 Playback Switch", RT5631_AXO1MIXER_CTRL,
1125 RT5631_M_MIC2_AXO1MIX_BIT, 1, 1),
1126 SOC_DAPM_SINGLE("OUTVOLL Playback Switch", RT5631_AXO1MIXER_CTRL,
1127 RT5631_M_OUTMIXL_AXO1MIX_BIT , 1 , 1),
1128 SOC_DAPM_SINGLE("OUTVOLR Playback Switch", RT5631_AXO1MIXER_CTRL,
1129 RT5631_M_OUTMIXR_AXO1MIX_BIT, 1, 1),
1132 static const struct snd_kcontrol_new rt5631_AXO2MIX_mixer_controls[] = {
1133 SOC_DAPM_SINGLE("MIC1_BST1 Playback Switch", RT5631_AXO2MIXER_CTRL,
1134 RT5631_M_MIC1_AXO2MIX_BIT, 1, 1),
1135 SOC_DAPM_SINGLE("MIC2_BST2 Playback Switch", RT5631_AXO2MIXER_CTRL,
1136 RT5631_M_MIC2_AXO2MIX_BIT, 1, 1),
1137 SOC_DAPM_SINGLE("OUTVOLL Playback Switch", RT5631_AXO2MIXER_CTRL,
1138 RT5631_M_OUTMIXL_AXO2MIX_BIT, 1, 1),
1139 SOC_DAPM_SINGLE("OUTVOLR Playback Switch", RT5631_AXO2MIXER_CTRL,
1140 RT5631_M_OUTMIXR_AXO2MIX_BIT, 1 , 1),
1143 static const struct snd_kcontrol_new rt5631_spolmix_mixer_controls[] = {
1144 SOC_DAPM_SINGLE("SPKVOLL Playback Switch", RT5631_SPK_MONO_OUT_CTRL,
1145 RT5631_M_SPKVOLL_SPOLMIX_BIT, 1, 1),
1146 SOC_DAPM_SINGLE("SPKVOLR Playback Switch", RT5631_SPK_MONO_OUT_CTRL,
1147 RT5631_M_SPKVOLR_SPOLMIX_BIT, 1, 1),
1150 static const struct snd_kcontrol_new rt5631_spormix_mixer_controls[] = {
1151 SOC_DAPM_SINGLE("SPKVOLL Playback Switch", RT5631_SPK_MONO_OUT_CTRL,
1152 RT5631_M_SPKVOLL_SPORMIX_BIT, 1, 1),
1153 SOC_DAPM_SINGLE("SPKVOLR Playback Switch", RT5631_SPK_MONO_OUT_CTRL,
1154 RT5631_M_SPKVOLR_SPORMIX_BIT, 1, 1),
1157 static const struct snd_kcontrol_new rt5631_monomix_mixer_controls[] = {
1158 SOC_DAPM_SINGLE("OUTVOLL Playback Switch", RT5631_SPK_MONO_OUT_CTRL,
1159 RT5631_M_OUTVOLL_MONOMIX_BIT, 1, 1),
1160 SOC_DAPM_SINGLE("OUTVOLR Playback Switch", RT5631_SPK_MONO_OUT_CTRL,
1161 RT5631_M_OUTVOLR_MONOMIX_BIT, 1, 1),
1164 /* Left SPK Volume Input */
1165 static const char *rt5631_spkvoll_sel[] = {"Vmid", "SPKMIXL"};
1167 static const SOC_ENUM_SINGLE_DECL(
1168 rt5631_spkvoll_enum, RT5631_SPK_OUT_VOL,
1169 RT5631_L_EN_SHIFT, rt5631_spkvoll_sel);
1171 static const struct snd_kcontrol_new rt5631_spkvoll_mux_control =
1172 SOC_DAPM_ENUM("Left SPKVOL SRC", rt5631_spkvoll_enum);
1174 /* Left HP Volume Input */
1175 static const char *rt5631_hpvoll_sel[] = {"Vmid", "OUTMIXL"};
1177 static const SOC_ENUM_SINGLE_DECL(
1178 rt5631_hpvoll_enum, RT5631_HP_OUT_VOL,
1179 RT5631_L_EN_SHIFT, rt5631_hpvoll_sel);
1181 static const struct snd_kcontrol_new rt5631_hpvoll_mux_control =
1182 SOC_DAPM_ENUM("Left HPVOL SRC", rt5631_hpvoll_enum);
1184 /* Left Out Volume Input */
1185 static const char *rt5631_outvoll_sel[] = {"Vmid", "OUTMIXL"};
1187 static const SOC_ENUM_SINGLE_DECL(
1188 rt5631_outvoll_enum, RT5631_MONO_AXO_1_2_VOL,
1189 RT5631_L_EN_SHIFT, rt5631_outvoll_sel);
1191 static const struct snd_kcontrol_new rt5631_outvoll_mux_control =
1192 SOC_DAPM_ENUM("Left OUTVOL SRC", rt5631_outvoll_enum);
1194 /* Right Out Volume Input */
1195 static const char *rt5631_outvolr_sel[] = {"Vmid", "OUTMIXR"};
1197 static const SOC_ENUM_SINGLE_DECL(
1198 rt5631_outvolr_enum, RT5631_MONO_AXO_1_2_VOL,
1199 RT5631_R_EN_SHIFT, rt5631_outvolr_sel);
1201 static const struct snd_kcontrol_new rt5631_outvolr_mux_control =
1202 SOC_DAPM_ENUM("Right OUTVOL SRC", rt5631_outvolr_enum);
1204 /* Right HP Volume Input */
1205 static const char *rt5631_hpvolr_sel[] = {"Vmid", "OUTMIXR"};
1207 static const SOC_ENUM_SINGLE_DECL(
1208 rt5631_hpvolr_enum, RT5631_HP_OUT_VOL,
1209 RT5631_R_EN_SHIFT, rt5631_hpvolr_sel);
1211 static const struct snd_kcontrol_new rt5631_hpvolr_mux_control =
1212 SOC_DAPM_ENUM("Right HPVOL SRC", rt5631_hpvolr_enum);
1214 /* Right SPK Volume Input */
1215 static const char *rt5631_spkvolr_sel[] = {"Vmid", "SPKMIXR"};
1217 static const SOC_ENUM_SINGLE_DECL(
1218 rt5631_spkvolr_enum, RT5631_SPK_OUT_VOL,
1219 RT5631_R_EN_SHIFT, rt5631_spkvolr_sel);
1221 static const struct snd_kcontrol_new rt5631_spkvolr_mux_control =
1222 SOC_DAPM_ENUM("Right SPKVOL SRC", rt5631_spkvolr_enum);
1224 /* SPO Left Channel Input */
1225 static const char *rt5631_spol_src_sel[] = {
1226 "SPOLMIX", "MONOIN_RX", "VDAC", "DACL"};
1228 static const SOC_ENUM_SINGLE_DECL(
1229 rt5631_spol_src_enum, RT5631_SPK_MONO_HP_OUT_CTRL,
1230 RT5631_SPK_L_MUX_SEL_SHIFT, rt5631_spol_src_sel);
1232 static const struct snd_kcontrol_new rt5631_spol_mux_control =
1233 SOC_DAPM_ENUM("SPOL SRC", rt5631_spol_src_enum);
1235 /* SPO Right Channel Input */
1236 static const char *rt5631_spor_src_sel[] = {
1237 "SPORMIX", "MONOIN_RX", "VDAC", "DACR"};
1239 static const SOC_ENUM_SINGLE_DECL(
1240 rt5631_spor_src_enum, RT5631_SPK_MONO_HP_OUT_CTRL,
1241 RT5631_SPK_R_MUX_SEL_SHIFT, rt5631_spor_src_sel);
1243 static const struct snd_kcontrol_new rt5631_spor_mux_control =
1244 SOC_DAPM_ENUM("SPOR SRC", rt5631_spor_src_enum);
1247 static const char *rt5631_mono_src_sel[] = {"MONOMIX", "MONOIN_RX", "VDAC"};
1249 static const SOC_ENUM_SINGLE_DECL(
1250 rt5631_mono_src_enum, RT5631_SPK_MONO_HP_OUT_CTRL,
1251 RT5631_MONO_MUX_SEL_SHIFT, rt5631_mono_src_sel);
1253 static const struct snd_kcontrol_new rt5631_mono_mux_control =
1254 SOC_DAPM_ENUM("MONO SRC", rt5631_mono_src_enum);
1256 /* Left HPO Input */
1257 static const char *rt5631_hpl_src_sel[] = {"Left HPVOL", "Left DAC"};
1259 static const SOC_ENUM_SINGLE_DECL(
1260 rt5631_hpl_src_enum, RT5631_SPK_MONO_HP_OUT_CTRL,
1261 RT5631_HP_L_MUX_SEL_SHIFT, rt5631_hpl_src_sel);
1263 static const struct snd_kcontrol_new rt5631_hpl_mux_control =
1264 SOC_DAPM_ENUM("HPL SRC", rt5631_hpl_src_enum);
1266 /* Right HPO Input */
1267 static const char *rt5631_hpr_src_sel[] = {"Right HPVOL", "Right DAC"};
1269 static const SOC_ENUM_SINGLE_DECL(
1270 rt5631_hpr_src_enum, RT5631_SPK_MONO_HP_OUT_CTRL,
1271 RT5631_HP_R_MUX_SEL_SHIFT, rt5631_hpr_src_sel);
1273 static const struct snd_kcontrol_new rt5631_hpr_mux_control =
1274 SOC_DAPM_ENUM("HPR SRC", rt5631_hpr_src_enum);
1276 static const struct snd_soc_dapm_widget rt5631_dapm_widgets[] = {
1278 SND_SOC_DAPM_VMID("Vmid"),
1280 SND_SOC_DAPM_SUPPLY("PLL1", RT5631_PWR_MANAG_ADD2,
1281 RT5631_PWR_PLL1_BIT, 0, NULL, 0),
1282 SND_SOC_DAPM_SUPPLY("PLL2", RT5631_PWR_MANAG_ADD2,
1283 RT5631_PWR_PLL2_BIT, 0, NULL, 0),
1287 SND_SOC_DAPM_INPUT("MIC1"),
1288 SND_SOC_DAPM_INPUT("MIC2"),
1289 SND_SOC_DAPM_INPUT("AXIL"),
1290 SND_SOC_DAPM_INPUT("AXIR"),
1291 SND_SOC_DAPM_INPUT("MONOIN_RXN"),
1292 SND_SOC_DAPM_INPUT("MONOIN_RXP"),
1293 SND_SOC_DAPM_INPUT("DMIC"),
1296 SND_SOC_DAPM_MICBIAS("MIC Bias1", RT5631_PWR_MANAG_ADD2,
1297 RT5631_PWR_MICBIAS1_VOL_BIT, 0),
1298 SND_SOC_DAPM_MICBIAS("MIC Bias2", RT5631_PWR_MANAG_ADD2,
1299 RT5631_PWR_MICBIAS2_VOL_BIT, 0),
1302 SND_SOC_DAPM_PGA("MIC1 Boost", RT5631_PWR_MANAG_ADD2,
1303 RT5631_PWR_MIC1_BOOT_GAIN_BIT, 0, NULL, 0),
1304 SND_SOC_DAPM_PGA("MIC2 Boost", RT5631_PWR_MANAG_ADD2,
1305 RT5631_PWR_MIC2_BOOT_GAIN_BIT, 0, NULL, 0),
1306 SND_SOC_DAPM_PGA("MONOIN_RXP Boost", RT5631_PWR_MANAG_ADD4,
1307 RT5631_PWR_MONO_IN_P_VOL_BIT, 0, NULL, 0),
1308 SND_SOC_DAPM_PGA("MONOIN_RXN Boost", RT5631_PWR_MANAG_ADD4,
1309 RT5631_PWR_MONO_IN_N_VOL_BIT, 0, NULL, 0),
1310 SND_SOC_DAPM_PGA("AXIL Boost", RT5631_PWR_MANAG_ADD4,
1311 RT5631_PWR_AXIL_IN_VOL_BIT, 0, NULL, 0),
1312 SND_SOC_DAPM_PGA("AXIR Boost", RT5631_PWR_MANAG_ADD4,
1313 RT5631_PWR_AXIR_IN_VOL_BIT, 0, NULL, 0),
1316 SND_SOC_DAPM_MIXER("MONO_IN", SND_SOC_NOPM, 0, 0, NULL, 0),
1319 SND_SOC_DAPM_MIXER("RECMIXL Mixer", RT5631_PWR_MANAG_ADD2,
1320 RT5631_PWR_RECMIXER_L_BIT, 0,
1321 &rt5631_recmixl_mixer_controls[0],
1322 ARRAY_SIZE(rt5631_recmixl_mixer_controls)),
1323 SND_SOC_DAPM_MIXER("RECMIXR Mixer", RT5631_PWR_MANAG_ADD2,
1324 RT5631_PWR_RECMIXER_R_BIT, 0,
1325 &rt5631_recmixr_mixer_controls[0],
1326 ARRAY_SIZE(rt5631_recmixr_mixer_controls)),
1327 /* Because of record duplication for L/R channel,
1328 * L/R ADCs need power up at the same time */
1329 SND_SOC_DAPM_MIXER("ADC Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
1332 SND_SOC_DAPM_SUPPLY("DMIC Supply", RT5631_DIG_MIC_CTRL,
1333 RT5631_DMIC_ENA_SHIFT, 0,
1334 set_dmic_params, SND_SOC_DAPM_PRE_PMU),
1335 /* ADC Data Srouce */
1336 SND_SOC_DAPM_SUPPLY("Left ADC Select", RT5631_INT_ST_IRQ_CTRL_2,
1337 RT5631_ADC_DATA_SEL_MIC1_SHIFT, 0, NULL, 0),
1338 SND_SOC_DAPM_SUPPLY("Right ADC Select", RT5631_INT_ST_IRQ_CTRL_2,
1339 RT5631_ADC_DATA_SEL_MIC2_SHIFT, 0, NULL, 0),
1342 SND_SOC_DAPM_ADC("Left ADC", "HIFI Capture",
1343 RT5631_PWR_MANAG_ADD1, RT5631_PWR_ADC_L_CLK_BIT, 0),
1344 SND_SOC_DAPM_ADC("Right ADC", "Voice HIFI Capture",
1345 RT5631_PWR_MANAG_ADD1, RT5631_PWR_ADC_R_CLK_BIT, 0),
1347 /* DAC and ADC supply power */
1348 SND_SOC_DAPM_SUPPLY("I2S", RT5631_PWR_MANAG_ADD1,
1349 RT5631_PWR_MAIN_I2S_BIT, 0, NULL, 0),
1350 SND_SOC_DAPM_SUPPLY("VI2S", RT5631_PWR_MANAG_ADD1,
1351 RT5631_PWR_VOICE_I2S_BIT, 0, NULL, 0),
1352 SND_SOC_DAPM_SUPPLY("DAC REF", RT5631_PWR_MANAG_ADD1,
1353 RT5631_PWR_DAC_REF_BIT, 0, NULL, 0),
1357 SND_SOC_DAPM_DAC("Left DAC", "HIFI Playback",
1358 RT5631_PWR_MANAG_ADD1, RT5631_PWR_DAC_L_CLK_BIT, 0),
1359 SND_SOC_DAPM_DAC("Right DAC", "HIFI Playback",
1360 RT5631_PWR_MANAG_ADD1, RT5631_PWR_DAC_R_CLK_BIT, 0),
1361 SND_SOC_DAPM_DAC("Voice DAC", "Voice DAC Mono Playback",
1362 RT5631_PWR_MANAG_ADD1, RT5631_PWR_VDAC_CLK_BIT, 0),
1363 SND_SOC_DAPM_PGA("Voice DAC Boost", SND_SOC_NOPM, 0, 0, NULL, 0),
1364 /* DAC supply power */
1365 SND_SOC_DAPM_SUPPLY("Left DAC To Mixer", RT5631_PWR_MANAG_ADD1,
1366 RT5631_PWR_DAC_L_TO_MIXER_BIT, 0, NULL, 0),
1367 SND_SOC_DAPM_SUPPLY("Right DAC To Mixer", RT5631_PWR_MANAG_ADD1,
1368 RT5631_PWR_DAC_R_TO_MIXER_BIT, 0, NULL, 0),
1369 SND_SOC_DAPM_SUPPLY("Voice DAC To Mixer", RT5631_PWR_MANAG_ADD1,
1370 RT5631_PWR_VDAC_TO_MIXER_BIT, 0, NULL, 0),
1372 /* Left SPK Mixer */
1373 SND_SOC_DAPM_MIXER("SPKMIXL Mixer", RT5631_PWR_MANAG_ADD2,
1374 RT5631_PWR_SPKMIXER_L_BIT, 0,
1375 &rt5631_spkmixl_mixer_controls[0],
1376 ARRAY_SIZE(rt5631_spkmixl_mixer_controls)),
1377 /* Left Out Mixer */
1378 SND_SOC_DAPM_MIXER("OUTMIXL Mixer", RT5631_PWR_MANAG_ADD2,
1379 RT5631_PWR_OUTMIXER_L_BIT, 0,
1380 &rt5631_outmixl_mixer_controls[0],
1381 ARRAY_SIZE(rt5631_outmixl_mixer_controls)),
1382 /* Right Out Mixer */
1383 SND_SOC_DAPM_MIXER("OUTMIXR Mixer", RT5631_PWR_MANAG_ADD2,
1384 RT5631_PWR_OUTMIXER_R_BIT, 0,
1385 &rt5631_outmixr_mixer_controls[0],
1386 ARRAY_SIZE(rt5631_outmixr_mixer_controls)),
1387 /* Right SPK Mixer */
1388 SND_SOC_DAPM_MIXER("SPKMIXR Mixer", RT5631_PWR_MANAG_ADD2,
1389 RT5631_PWR_SPKMIXER_R_BIT, 0,
1390 &rt5631_spkmixr_mixer_controls[0],
1391 ARRAY_SIZE(rt5631_spkmixr_mixer_controls)),
1394 SND_SOC_DAPM_MUX("Left SPKVOL Mux", RT5631_PWR_MANAG_ADD4,
1395 RT5631_PWR_SPK_L_VOL_BIT, 0,
1396 &rt5631_spkvoll_mux_control),
1397 SND_SOC_DAPM_MUX("Left HPVOL Mux", RT5631_PWR_MANAG_ADD4,
1398 RT5631_PWR_HP_L_OUT_VOL_BIT, 0,
1399 &rt5631_hpvoll_mux_control),
1400 SND_SOC_DAPM_MUX("Left OUTVOL Mux", RT5631_PWR_MANAG_ADD4,
1401 RT5631_PWR_LOUT_VOL_BIT, 0,
1402 &rt5631_outvoll_mux_control),
1403 SND_SOC_DAPM_MUX("Right OUTVOL Mux", RT5631_PWR_MANAG_ADD4,
1404 RT5631_PWR_ROUT_VOL_BIT, 0,
1405 &rt5631_outvolr_mux_control),
1406 SND_SOC_DAPM_MUX("Right HPVOL Mux", RT5631_PWR_MANAG_ADD4,
1407 RT5631_PWR_HP_R_OUT_VOL_BIT, 0,
1408 &rt5631_hpvolr_mux_control),
1409 SND_SOC_DAPM_MUX("Right SPKVOL Mux", RT5631_PWR_MANAG_ADD4,
1410 RT5631_PWR_SPK_R_VOL_BIT, 0,
1411 &rt5631_spkvolr_mux_control),
1414 SND_SOC_DAPM_PGA_S("Left DAC_HP", 0, SND_SOC_NOPM, 0, 0, NULL, 0),
1415 SND_SOC_DAPM_PGA_S("Right DAC_HP", 0, SND_SOC_NOPM, 0, 0, NULL, 0),
1418 SND_SOC_DAPM_PGA_S("HP Depop", 1, SND_SOC_NOPM, 0, 0,
1419 hp_event, SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
1422 SND_SOC_DAPM_PGA_S("AUXO Depop", 1, SND_SOC_NOPM, 0, 0,
1423 auxo_event, SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
1427 SND_SOC_DAPM_PGA_S("SPOL PGA", 2,RT5631_SPK_OUT_VOL,
1428 RT5631_L_MUTE_SHIFT, 1,
1429 spk_event,SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
1430 SND_SOC_DAPM_PGA_S("SPOR PGA", 2,RT5631_SPK_OUT_VOL,
1431 RT5631_R_MUTE_SHIFT, 1,
1432 spk_event,SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
1435 SND_SOC_DAPM_MIXER("AXO1MIX Mixer", RT5631_PWR_MANAG_ADD3,
1436 RT5631_PWR_AXO1MIXER_BIT, 0,
1437 &rt5631_AXO1MIX_mixer_controls[0],
1438 ARRAY_SIZE(rt5631_AXO1MIX_mixer_controls)),
1440 SND_SOC_DAPM_MIXER("SPOLMIX Mixer", SND_SOC_NOPM, 0, 0,
1441 &rt5631_spolmix_mixer_controls[0],
1442 ARRAY_SIZE(rt5631_spolmix_mixer_controls)),
1444 SND_SOC_DAPM_MIXER("MONOMIX Mixer", RT5631_PWR_MANAG_ADD3,
1445 RT5631_PWR_MONOMIXER_BIT, 0,
1446 &rt5631_monomix_mixer_controls[0],
1447 ARRAY_SIZE(rt5631_monomix_mixer_controls)),
1449 SND_SOC_DAPM_MIXER("SPORMIX Mixer", SND_SOC_NOPM, 0, 0,
1450 &rt5631_spormix_mixer_controls[0],
1451 ARRAY_SIZE(rt5631_spormix_mixer_controls)),
1453 SND_SOC_DAPM_MIXER("AXO2MIX Mixer", RT5631_PWR_MANAG_ADD3,
1454 RT5631_PWR_AXO2MIXER_BIT, 0,
1455 &rt5631_AXO2MIX_mixer_controls[0],
1456 ARRAY_SIZE(rt5631_AXO2MIX_mixer_controls)),
1459 SND_SOC_DAPM_MUX("SPOL Mux", SND_SOC_NOPM, 0, 0,
1460 &rt5631_spol_mux_control),
1461 SND_SOC_DAPM_MUX("SPOR Mux", SND_SOC_NOPM, 0, 0,
1462 &rt5631_spor_mux_control),
1463 SND_SOC_DAPM_MUX("MONO Mux", SND_SOC_NOPM, 0, 0,
1464 &rt5631_mono_mux_control),
1465 SND_SOC_DAPM_MUX("HPL Mux", SND_SOC_NOPM, 0, 0,
1466 &rt5631_hpl_mux_control),
1467 SND_SOC_DAPM_MUX("HPR Mux", SND_SOC_NOPM, 0, 0,
1468 &rt5631_hpr_mux_control),
1471 SND_SOC_DAPM_SUPPLY("MONO Depop", RT5631_PWR_MANAG_ADD3,
1472 RT5631_PWR_MONO_DEPOP_DIS_BIT, 0, NULL, 0),
1473 SND_SOC_DAPM_SUPPLY("Class D", RT5631_PWR_MANAG_ADD1,
1474 RT5631_PWR_CLASS_D_BIT, 0, NULL, 0),
1477 SND_SOC_DAPM_OUTPUT("AUXO1"),
1478 SND_SOC_DAPM_OUTPUT("AUXO2"),
1479 SND_SOC_DAPM_OUTPUT("SPOL"),
1480 SND_SOC_DAPM_OUTPUT("SPOR"),
1481 SND_SOC_DAPM_OUTPUT("HPOL"),
1482 SND_SOC_DAPM_OUTPUT("HPOR"),
1483 SND_SOC_DAPM_OUTPUT("MONO"),
1486 static const struct snd_soc_dapm_route rt5631_dapm_routes[] = {
1487 {"MIC1 Boost", NULL, "MIC1"},
1488 {"MIC2 Boost", NULL, "MIC2"},
1489 {"MONOIN_RXP Boost", NULL, "MONOIN_RXP"},
1490 {"MONOIN_RXN Boost", NULL, "MONOIN_RXN"},
1491 {"AXIL Boost", NULL, "AXIL"},
1492 {"AXIR Boost", NULL, "AXIR"},
1494 {"MONO_IN", NULL, "MONOIN_RXP Boost"},
1495 {"MONO_IN", NULL, "MONOIN_RXN Boost"},
1497 {"RECMIXL Mixer", "OUTMIXL Capture Switch", "OUTMIXL Mixer"},
1498 {"RECMIXL Mixer", "MIC1_BST1 Capture Switch", "MIC1 Boost"},
1499 {"RECMIXL Mixer", "AXILVOL Capture Switch", "AXIL Boost"},
1500 {"RECMIXL Mixer", "MONOIN_RX Capture Switch", "MONO_IN"},
1502 {"RECMIXR Mixer", "OUTMIXR Capture Switch", "OUTMIXR Mixer"},
1503 {"RECMIXR Mixer", "MIC2_BST2 Capture Switch", "MIC2 Boost"},
1504 {"RECMIXR Mixer", "AXIRVOL Capture Switch", "AXIR Boost"},
1505 {"RECMIXR Mixer", "MONOIN_RX Capture Switch", "MONO_IN"},
1507 {"ADC Mixer", NULL, "RECMIXL Mixer"},
1508 {"ADC Mixer", NULL, "RECMIXR Mixer"},
1510 {"Left ADC", NULL, "ADC Mixer"},
1511 {"Left ADC", NULL, "Left ADC Select", check_adcl_select},
1512 {"Left ADC", NULL, "PLL1", check_sysclk1_source},
1513 {"Left ADC", NULL, "I2S", check_adcr_stereo},
1514 {"Left ADC", NULL, "VI2S", check_adcr_voice},
1515 {"Left ADC", NULL, "DAC REF"},
1517 {"Right ADC", NULL, "ADC Mixer"},
1518 {"Right ADC", NULL, "Right ADC Select", check_adcr_select},
1519 {"Right ADC", NULL, "PLL1", check_adcr_pll1},
1520 {"Right ADC", NULL, "PLL2", check_adcr_pll2},
1521 {"Right ADC", NULL, "I2S", check_adcr_stereo},
1522 {"Right ADC", NULL, "VI2S", check_adcr_voice},
1523 {"Right ADC", NULL, "DAC REF"},
1525 {"DMIC", NULL, "DMIC Supply", check_dmic_used},
1526 {"Left ADC", NULL, "DMIC"},
1527 {"Right ADC", NULL, "DMIC"},
1529 {"Left DAC", NULL, "PLL1", check_sysclk1_source},
1530 {"Left DAC", NULL, "I2S"},
1531 {"Left DAC", NULL, "DAC REF"},
1532 {"Left DAC", NULL, "Left DAC To Mixer"},
1533 {"Right DAC", NULL, "PLL1", check_sysclk1_source},
1534 {"Right DAC", NULL, "I2S"},
1535 {"Right DAC", NULL, "DAC REF"},
1536 {"Right DAC", NULL, "Right DAC To Mixer"},
1538 {"Voice DAC", NULL, "PLL1", check_vdac_pll1},
1539 {"Voice DAC", NULL, "PLL2", check_vdac_pll2},
1540 {"Voice DAC", NULL, "VI2S"},
1541 {"Voice DAC", NULL, "DAC REF"},
1542 {"Voice DAC", NULL, "Voice DAC To Mixer"},
1544 {"Voice DAC Boost", NULL, "Voice DAC"},
1546 {"SPKMIXL Mixer", "RECMIXL Playback Switch", "RECMIXL Mixer"},
1547 {"SPKMIXL Mixer", "MIC1_P Playback Switch", "MIC1"},
1548 {"SPKMIXL Mixer", "DACL Playback Switch", "Left DAC"},
1549 {"SPKMIXL Mixer", "OUTMIXL Playback Switch", "OUTMIXL Mixer"},
1551 {"SPKMIXR Mixer", "OUTMIXR Playback Switch", "OUTMIXR Mixer"},
1552 {"SPKMIXR Mixer", "DACR Playback Switch", "Right DAC"},
1553 {"SPKMIXR Mixer", "MIC2_P Playback Switch", "MIC2"},
1554 {"SPKMIXR Mixer", "RECMIXR Playback Switch", "RECMIXR Mixer"},
1556 {"OUTMIXL Mixer", "RECMIXL Playback Switch", "RECMIXL Mixer"},
1557 {"OUTMIXL Mixer", "RECMIXR Playback Switch", "RECMIXR Mixer"},
1558 {"OUTMIXL Mixer", "DACL Playback Switch", "Left DAC"},
1559 {"OUTMIXL Mixer", "MIC1_BST1 Playback Switch", "MIC1 Boost"},
1560 {"OUTMIXL Mixer", "MIC2_BST2 Playback Switch", "MIC2 Boost"},
1561 {"OUTMIXL Mixer", "MONOIN_RXP Playback Switch", "MONOIN_RXP Boost"},
1562 {"OUTMIXL Mixer", "AXILVOL Playback Switch", "AXIL Boost"},
1563 {"OUTMIXL Mixer", "AXIRVOL Playback Switch", "AXIR Boost"},
1564 {"OUTMIXL Mixer", "VDAC Playback Switch", "Voice DAC Boost"},
1566 {"OUTMIXR Mixer", "RECMIXL Playback Switch", "RECMIXL Mixer"},
1567 {"OUTMIXR Mixer", "RECMIXR Playback Switch", "RECMIXR Mixer"},
1568 {"OUTMIXR Mixer", "DACR Playback Switch", "Right DAC"},
1569 {"OUTMIXR Mixer", "MIC1_BST1 Playback Switch", "MIC1 Boost"},
1570 {"OUTMIXR Mixer", "MIC2_BST2 Playback Switch", "MIC2 Boost"},
1571 {"OUTMIXR Mixer", "MONOIN_RXN Playback Switch", "MONOIN_RXN Boost"},
1572 {"OUTMIXR Mixer", "AXILVOL Playback Switch", "AXIL Boost"},
1573 {"OUTMIXR Mixer", "AXIRVOL Playback Switch", "AXIR Boost"},
1574 {"OUTMIXR Mixer", "VDAC Playback Switch", "Voice DAC Boost"},
1576 {"Left SPKVOL Mux", "SPKMIXL", "SPKMIXL Mixer"},
1577 {"Left SPKVOL Mux", "Vmid", "Vmid"},
1578 {"Left HPVOL Mux", "OUTMIXL", "OUTMIXL Mixer"},
1579 {"Left HPVOL Mux", "Vmid", "Vmid"},
1580 {"Left OUTVOL Mux", "OUTMIXL", "OUTMIXL Mixer"},
1581 {"Left OUTVOL Mux", "Vmid", "Vmid"},
1582 {"Right OUTVOL Mux", "OUTMIXR", "OUTMIXR Mixer"},
1583 {"Right OUTVOL Mux", "Vmid", "Vmid"},
1584 {"Right HPVOL Mux", "OUTMIXR", "OUTMIXR Mixer"},
1585 {"Right HPVOL Mux", "Vmid", "Vmid"},
1586 {"Right SPKVOL Mux", "SPKMIXR", "SPKMIXR Mixer"},
1587 {"Right SPKVOL Mux", "Vmid", "Vmid"},
1589 {"AXO1MIX Mixer", "MIC1_BST1 Playback Switch", "MIC1 Boost"},
1590 {"AXO1MIX Mixer", "OUTVOLL Playback Switch", "Left OUTVOL Mux"},
1591 {"AXO1MIX Mixer", "OUTVOLR Playback Switch", "Right OUTVOL Mux"},
1592 {"AXO1MIX Mixer", "MIC2_BST2 Playback Switch", "MIC2 Boost"},
1594 {"AXO2MIX Mixer", "MIC1_BST1 Playback Switch", "MIC1 Boost"},
1595 {"AXO2MIX Mixer", "OUTVOLL Playback Switch", "Left OUTVOL Mux"},
1596 {"AXO2MIX Mixer", "OUTVOLR Playback Switch", "Right OUTVOL Mux"},
1597 {"AXO2MIX Mixer", "MIC2_BST2 Playback Switch", "MIC2 Boost"},
1599 {"SPOLMIX Mixer", "SPKVOLL Playback Switch", "Left SPKVOL Mux"},
1600 {"SPOLMIX Mixer", "SPKVOLR Playback Switch", "Right SPKVOL Mux"},
1602 {"SPORMIX Mixer", "SPKVOLL Playback Switch", "Left SPKVOL Mux"},
1603 {"SPORMIX Mixer", "SPKVOLR Playback Switch", "Right SPKVOL Mux"},
1605 {"MONOMIX Mixer", "OUTVOLL Playback Switch", "Left OUTVOL Mux"},
1606 {"MONOMIX Mixer", "OUTVOLR Playback Switch", "Right OUTVOL Mux"},
1608 {"SPOL Mux", "SPOLMIX", "SPOLMIX Mixer"},
1609 {"SPOL Mux", "MONOIN_RX", "MONO_IN"},
1610 {"SPOL Mux", "VDAC", "Voice DAC Boost"},
1611 {"SPOL Mux", "DACL", "Left DAC"},
1613 {"SPOR Mux", "SPORMIX", "SPORMIX Mixer"},
1614 {"SPOR Mux", "MONOIN_RX", "MONO_IN"},
1615 {"SPOR Mux", "VDAC", "Voice DAC Boost"},
1616 {"SPOR Mux", "DACR", "Right DAC"},
1618 {"MONO Mux", "MONOMIX", "MONOMIX Mixer"},
1619 {"MONO Mux", "MONOIN_RX", "MONO_IN"},
1620 {"MONO Mux", "VDAC", "Voice DAC Boost"},
1622 {"Right DAC_HP", NULL, "Right DAC"},
1623 {"Left DAC_HP", NULL, "Left DAC"},
1625 {"HPL Mux", "Left HPVOL", "Left HPVOL Mux"},
1626 {"HPL Mux", "Left DAC", "Left DAC_HP"},
1627 {"HPR Mux", "Right HPVOL", "Right HPVOL Mux"},
1628 {"HPR Mux", "Right DAC", "Right DAC_HP"},
1630 {"HP Depop", NULL, "HPL Mux"},
1631 {"HP Depop", NULL, "HPR Mux"},
1633 {"AUXO1", NULL, "AXO1MIX Mixer"},
1634 {"AUXO2", NULL, "AXO2MIX Mixer"},
1636 {"AUXO Depop", NULL, "AXO1MIX Mixer"},
1637 {"AUXO Depop", NULL, "AXO2MIX Mixer"},
1639 {"AUXO1", NULL, "AUXO Depop"},
1640 {"AUXO2", NULL, "AUXO Depop"},
1643 {"SPOL PGA", NULL, "Class D"},
1644 {"SPOL PGA", NULL, "SPOL Mux"},
1645 {"SPOR PGA", NULL, "Class D"},
1646 {"SPOR PGA", NULL, "SPOR Mux"},
1647 {"SPOL", NULL, "SPOL PGA"},
1648 {"SPOR", NULL, "SPOR PGA"},
1650 {"SPOL", NULL, "Class D"},
1651 {"SPOL", NULL, "SPOL Mux"},
1652 {"SPOR", NULL, "Class D"},
1653 {"SPOR", NULL, "SPOR Mux"},
1655 {"HPOL", NULL, "HP Depop"},
1656 {"HPOR", NULL, "HP Depop"},
1658 {"MONO", NULL, "MONO Depop"},
1659 {"MONO", NULL, "MONO Mux"},
1662 struct coeff_clk_div {
1676 static const struct pll_div codec_master_pll_div[] = {
1677 {2048000, 8192000, 0x0ea0},
1678 {3686400, 8192000, 0x4e27},
1679 {12000000, 8192000, 0x456b},
1680 {13000000, 8192000, 0x495f},
1681 {13100000, 8192000, 0x0320},
1682 {2048000, 11289600, 0xf637},
1683 {3686400, 11289600, 0x2f22},
1684 {12000000, 11289600, 0x3e2f},
1685 {13000000, 11289600, 0x4d5b},
1686 {13100000, 11289600, 0x363b},
1687 {2048000, 16384000, 0x1ea0},
1688 {3686400, 16384000, 0x9e27},
1689 {12000000, 16384000, 0x452b},
1690 {13000000, 16384000, 0x542f},
1691 {13100000, 16384000, 0x03a0},
1692 {2048000, 16934400, 0xe625},
1693 {3686400, 16934400, 0x9126},
1694 {12000000, 16934400, 0x4d2c},
1695 {13000000, 16934400, 0x742f},
1696 {13100000, 16934400, 0x3c27},
1697 {2048000, 22579200, 0x2aa0},
1698 {3686400, 22579200, 0x2f20},
1699 {12000000, 22579200, 0x7e2f},
1700 {13000000, 22579200, 0x742f},
1701 {13100000, 22579200, 0x3c27},
1702 {2048000, 24576000, 0x2ea0},
1703 {3686400, 24576000, 0xee27},
1704 {12000000, 24576000, 0x2915},
1705 {13000000, 24576000, 0x772e},
1706 {13100000, 24576000, 0x0d20},
1707 {26000000, 24576000, 0x2027},
1708 {26000000, 22579200, 0x392f},
1709 {24576000, 22579200, 0x0921},
1710 {24576000, 24576000, 0x02a0},
1713 static const struct pll_div codec_slave_pll_div[] = {
1714 {256000, 2048000, 0x46f0},
1715 {256000, 4096000, 0x3ea0},
1716 {352800, 5644800, 0x3ea0},
1717 {512000, 8192000, 0x3ea0},
1718 {1024000, 8192000, 0x46f0},
1719 {705600, 11289600, 0x3ea0},
1720 {1024000, 16384000, 0x3ea0},
1721 {1411200, 22579200, 0x3ea0},
1722 {1536000, 24576000, 0x3ea0},
1723 {2048000, 16384000, 0x1ea0},
1724 {2822400, 22579200, 0x1ea0},
1725 {2822400, 45158400, 0x5ec0},
1726 {5644800, 45158400, 0x46f0},
1727 {3072000, 24576000, 0x1ea0},
1728 {3072000, 49152000, 0x5ec0},
1729 {6144000, 49152000, 0x46f0},
1730 {705600, 11289600, 0x3ea0},
1731 {705600, 8467200, 0x3ab0},
1732 {24576000, 24576000, 0x02a0},
1733 {1411200, 11289600, 0x1690},
1734 {2822400, 11289600, 0x0a90},
1735 {1536000, 12288000, 0x1690},
1736 {3072000, 12288000, 0x0a90},
1739 static struct coeff_clk_div coeff_div[] = {
1740 /* sysclk is 256fs */
1741 {2048000, 8000 * 32, 8000, 0x1000},
1742 {2048000, 8000 * 64, 8000, 0x0000},
1743 {2822400, 11025 * 32, 11025, 0x1000},
1744 {2822400, 11025 * 64, 11025, 0x0000},
1745 {4096000, 16000 * 32, 16000, 0x1000},
1746 {4096000, 16000 * 64, 16000, 0x0000},
1747 {5644800, 22050 * 32, 22050, 0x1000},
1748 {5644800, 22050 * 64, 22050, 0x0000},
1749 {8192000, 32000 * 32, 32000, 0x1000},
1750 {8192000, 32000 * 64, 32000, 0x0000},
1751 {11289600, 44100 * 32, 44100, 0x1000},
1752 {11289600, 44100 * 64, 44100, 0x0000},
1753 {12288000, 48000 * 32, 48000, 0x1000},
1754 {12288000, 48000 * 64, 48000, 0x0000},
1755 {22579200, 88200 * 32, 88200, 0x1000},
1756 {22579200, 88200 * 64, 88200, 0x0000},
1757 {24576000, 96000 * 32, 96000, 0x1000},
1758 {24576000, 96000 * 64, 96000, 0x0000},
1759 /* sysclk is 512fs */
1760 {4096000, 8000 * 32, 8000, 0x3000},
1761 {4096000, 8000 * 64, 8000, 0x2000},
1762 {5644800, 11025 * 32, 11025, 0x3000},
1763 {5644800, 11025 * 64, 11025, 0x2000},
1764 {8192000, 16000 * 32, 16000, 0x3000},
1765 {8192000, 16000 * 64, 16000, 0x2000},
1766 {11289600, 22050 * 32, 22050, 0x3000},
1767 {11289600, 22050 * 64, 22050, 0x2000},
1768 {16384000, 32000 * 32, 32000, 0x3000},
1769 {16384000, 32000 * 64, 32000, 0x2000},
1770 {22579200, 44100 * 32, 44100, 0x3000},
1771 {22579200, 44100 * 64, 44100, 0x2000},
1772 {24576000, 48000 * 32, 48000, 0x3000},
1773 {24576000, 48000 * 64, 48000, 0x2000},
1774 {45158400, 88200 * 32, 88200, 0x3000},
1775 {45158400, 88200 * 64, 88200, 0x2000},
1776 {49152000, 96000 * 32, 96000, 0x3000},
1777 {49152000, 96000 * 64, 96000, 0x2000},
1778 /* sysclk is 24.576Mhz or 22.5792Mhz */
1779 {24576000, 8000 * 32, 8000, 0x7080},
1780 {24576000, 8000 * 64, 8000, 0x6080},
1781 {24576000, 16000 * 32, 16000, 0x5080},
1782 {24576000, 16000 * 64, 16000, 0x4080},
1783 {24576000, 24000 * 32, 24000, 0x5000},
1784 {24576000, 24000 * 64, 24000, 0x4000},
1785 {24576000, 32000 * 32, 32000, 0x3080},
1786 {24576000, 32000 * 64, 32000, 0x2080},
1787 {22579200, 11025 * 32, 11025, 0x7000},
1788 {22579200, 11025 * 64, 11025, 0x6000},
1789 {22579200, 22050 * 32, 22050, 0x5000},
1790 {22579200, 22050 * 64, 22050, 0x4000},
1793 struct coeff_clk_div coeff_div_voice[] = {
1794 /* sysclk is 256fs */
1795 {2048000, 8000 * 32, 8000, 0x0008},
1796 {2048000, 8000 * 64, 8000, 0x0000},
1797 {2822400, 11025 * 32, 11025, 0x0008},
1798 {2822400, 11025 * 64, 11025, 0x0000},
1799 {4096000, 16000 * 32, 16000, 0x0008},
1800 {4096000, 16000 * 64, 16000, 0x0000},
1801 /* sysclk is 512fs */
1802 {4096000, 8000 * 32, 8000, 0x0018},
1803 {4096000, 8000 * 64, 8000, 0x0010},
1804 {5644800, 11025 * 32, 11025, 0x0018},
1805 {5644800, 11025 * 64, 11025, 0x0010},
1806 {8192000, 16000 * 32, 16000, 0x0018},
1807 {8192000, 16000 * 64, 16000, 0x0010},
1808 /* sysclk is 24.576Mhz or 22.5792Mhz */
1809 {24576000, 8000 * 32, 8000, 0x003c},
1810 {24576000, 8000 * 64, 8000, 0x0034},
1811 {24576000, 16000 * 32, 16000, 0x002c},
1812 {24576000, 16000 * 64, 16000, 0x0024},
1813 {22579200, 11025 * 32, 11025, 0x0038},
1814 {22579200, 11025 * 64, 11025, 0x0030},
1817 static int get_coeff(int dai_id, int mclk, int rate, int timesofbclk)
1819 struct coeff_clk_div *tabp;
1822 if (dai_id == RT5631_AIF1) {
1824 tab_num = ARRAY_SIZE(coeff_div);
1825 } else if (dai_id == RT5631_AIF2) {
1826 tabp = coeff_div_voice;
1827 tab_num = ARRAY_SIZE(coeff_div_voice);
1831 for (i = 0; i < tab_num; i++) {
1832 if (tabp[i].mclk == mclk && tabp[i].rate == rate &&
1833 (tabp[i].bclk / tabp[i].rate) == timesofbclk)
1839 static int rt5631_hifi_pcm_params(struct snd_pcm_substream *substream,
1840 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
1842 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1843 struct snd_soc_codec *codec = rtd->codec;
1844 struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
1845 int timesofbclk = 32, coeff, sysclk=0;
1846 unsigned int iface = 0, val;
1848 dev_dbg(codec->dev, "enter %s\n", __func__);
1850 if (RT5631_AIF1 == dai->id) {
1851 snd_soc_update_bits(codec, RT5631_GLOBAL_CLK_CTRL,
1852 RT5631_ADCR_FUN_MASK, RT5631_ADCR_FUN_ADC);
1853 sysclk = rt5631->sysclk[RT5631_SCLK1];
1854 } else if (RT5631_AIF2 == dai->id) {
1855 snd_soc_update_bits(codec, RT5631_GLOBAL_CLK_CTRL,
1856 RT5631_ADCR_FUN_MASK, RT5631_ADCR_FUN_VADC);
1857 val = snd_soc_read(codec, RT5631_GLOBAL_CLK_CTRL);
1858 if (val & RT5631_VDAC_CLK_SOUR_SCLK2)
1859 sysclk = rt5631->sysclk[RT5631_SCLK2];
1861 sysclk = rt5631->sysclk[RT5631_SCLK1];
1864 rt5631->bclk_rate = snd_soc_params_to_bclk(params);
1865 if (rt5631->bclk_rate < 0) {
1866 dev_err(codec->dev, "Fail to get BCLK rate\n");
1867 return rt5631->bclk_rate;
1869 rt5631->rate = params_rate(params);
1870 if (SNDRV_PCM_STREAM_CAPTURE == substream->stream)
1871 rt5631->rx_rate = rt5631->rate;
1873 if (rt5631->master[dai->id])
1874 coeff = get_coeff(dai->id, sysclk, rt5631->rate,
1875 rt5631->bclk_rate / rt5631->rate);
1877 coeff = get_coeff(dai->id, sysclk, rt5631->rate, timesofbclk);
1879 dev_err(codec->dev, "Fail to get coeff\n");
1883 switch (params_format(params)) {
1884 case SNDRV_PCM_FORMAT_S16_LE:
1886 case SNDRV_PCM_FORMAT_S20_3LE:
1887 iface |= RT5631_SDP_I2S_DL_20;
1889 case SNDRV_PCM_FORMAT_S24_LE:
1890 iface |= RT5631_SDP_I2S_DL_24;
1892 case SNDRV_PCM_FORMAT_S8:
1893 iface |= RT5631_SDP_I2S_DL_8;
1899 if (RT5631_AIF1 == dai->id) {
1900 snd_soc_update_bits(codec, RT5631_SDP_CTRL,
1901 RT5631_SDP_I2S_DL_MASK, iface);
1902 snd_soc_update_bits(codec, RT5631_STEREO_AD_DA_CLK_CTRL,
1903 RT5631_I2S_PRE_DIV_MASK |
1904 RT5631_I2S_LRCK_SEL_N_BCLK_MASK |
1905 RT5631_ADDA_FILTER_CLK_SEL_MASK,
1906 coeff_div[coeff].reg_val);
1907 } else if (RT5631_AIF2 == dai->id) {
1908 snd_soc_update_bits(codec, RT5631_MONO_SDP_CTRL,
1909 RT5631_SDP_I2S_DL_MASK, iface);
1910 snd_soc_update_bits(codec, RT5631_STEREO_AD_DA_CLK_CTRL,
1911 RT5631_I2S_PRE_DIV2_MASK |
1912 RT5631_I2S_LRCK_SEL_N_BCLK2_MASK |
1913 RT5631_ADDA_FILTER_CLK2_SEL_MASK,
1914 coeff_div_voice[coeff].reg_val);
1920 static int rt5631_hifi_codec_set_dai_fmt(
1921 struct snd_soc_dai *dai, unsigned int fmt)
1923 struct snd_soc_codec *codec = dai->codec;
1924 struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
1925 unsigned int iface = 0;
1927 dev_dbg(codec->dev, "enter %s\n", __func__);
1929 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1930 case SND_SOC_DAIFMT_CBM_CFM:
1931 rt5631->master[dai->id] = 1;
1933 case SND_SOC_DAIFMT_CBS_CFS:
1934 iface |= RT5631_SDP_MODE_SEL_SLAVE;
1935 rt5631->master[dai->id] = 0;
1941 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1942 case SND_SOC_DAIFMT_I2S:
1944 case SND_SOC_DAIFMT_LEFT_J:
1945 iface |= RT5631_SDP_I2S_DF_LEFT;
1947 case SND_SOC_DAIFMT_DSP_A:
1948 iface |= RT5631_SDP_I2S_DF_PCM_A;
1950 case SND_SOC_DAIFMT_DSP_B:
1951 iface |= RT5631_SDP_I2S_DF_PCM_B;
1957 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1958 case SND_SOC_DAIFMT_NB_NF:
1960 case SND_SOC_DAIFMT_IB_NF:
1961 iface |= RT5631_SDP_I2S_BCLK_POL_CTRL;
1967 if (RT5631_AIF1 == dai->id)
1968 snd_soc_write(codec, RT5631_SDP_CTRL, iface);
1969 else if (RT5631_AIF2 == dai->id)
1970 snd_soc_write(codec, RT5631_MONO_SDP_CTRL, iface);
1975 static int rt5631_hifi_codec_set_dai_sysclk(struct snd_soc_dai *dai,
1976 int clk_id, unsigned int freq, int dir)
1978 struct snd_soc_codec *codec = dai->codec;
1979 struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
1981 dev_dbg(codec->dev, "syclk[%d]: %d\n", clk_id, freq);
1983 if (clk_id != RT5631_SCLK1 && clk_id != RT5631_SCLK2)
1986 if ((freq >= (256 * 8000)) && (freq <= (512 * 96000))) {
1987 rt5631->sysclk[clk_id] = freq;
1994 static int rt5631_codec_set_dai_pll(struct snd_soc_dai *dai, int pll_id,
1995 int source, unsigned int freq_in, unsigned int freq_out)
1997 struct snd_soc_codec *codec = dai->codec;
1998 struct pll_div *pll_tab;
1999 unsigned int pll_reg, clk_val, clk_mask;
2000 int i, tab_num, ret = -EINVAL;
2002 if (!freq_in || !freq_out) {
2003 dev_dbg(codec->dev, "PLL disabled\n");
2004 if (RT5631_PLL1 == pll_id)
2005 snd_soc_update_bits(codec, RT5631_GLOBAL_CLK_CTRL,
2006 RT5631_SYSCLK_SOUR_SEL_MASK,
2007 RT5631_SYSCLK_SOUR_SEL_MCLK);
2008 else if (RT5631_PLL2 == pll_id)
2009 snd_soc_update_bits(codec, RT5631_GLOBAL_CLK_CTRL,
2010 RT5631_SYSCLK2_SOUR_SEL_MASK,
2011 RT5631_SYSCLK2_SOUR_SEL_MCLK);
2017 pll_reg = RT5631_PLL_CTRL;
2018 clk_mask = RT5631_SYSCLK_SOUR_SEL_MASK |
2019 RT5631_PLLCLK_SOUR_SEL_MASK;
2020 clk_val = RT5631_SYSCLK_SOUR_SEL_PLL;
2024 pll_reg = RT5631_PLL2_CTRL;
2025 clk_mask = RT5631_SYSCLK2_SOUR_SEL_MASK |
2026 RT5631_PLLCLK2_SOUR_SEL_MASK;
2027 clk_val = RT5631_SYSCLK2_SOUR_SEL_PLL2;
2035 case RT5631_PLL_S_MCLK:
2036 pll_tab = (struct pll_div *)codec_master_pll_div;
2037 tab_num = ARRAY_SIZE(codec_master_pll_div);
2038 clk_val |= (pll_id ? RT5631_PLLCLK2_SOUR_SEL_MCLK :
2039 RT5631_PLLCLK_SOUR_SEL_MCLK);
2042 case RT5631_PLL_S_BCLK:
2043 pll_tab = (struct pll_div *)codec_slave_pll_div;
2044 tab_num = ARRAY_SIZE(codec_slave_pll_div);
2045 clk_val |= (pll_id ? RT5631_PLLCLK2_SOUR_SEL_BCLK :
2046 RT5631_PLLCLK_SOUR_SEL_BCLK);
2049 case RT5631_PLL_S_VBCLK:
2050 pll_tab = (struct pll_div *)codec_slave_pll_div;
2051 tab_num = ARRAY_SIZE(codec_slave_pll_div);
2052 clk_val |= (pll_id ? RT5631_PLLCLK2_SOUR_SEL_VBCLK :
2053 RT5631_PLLCLK_SOUR_SEL_VBCLK);
2060 for (i = 0; i < tab_num; i++)
2061 if (freq_in == pll_tab[i].pll_in &&
2062 freq_out == pll_tab[i].pll_out) {
2063 snd_soc_write(codec, pll_reg, pll_tab[i].reg_val);
2065 snd_soc_update_bits(codec, RT5631_GLOBAL_CLK_CTRL,
2074 static int rt5631_set_bias_level(struct snd_soc_codec *codec,
2075 enum snd_soc_bias_level level)
2078 case SND_SOC_BIAS_ON:
2080 snd_soc_update_bits(codec, RT5631_SPK_OUT_VOL,
2081 RT5631_L_MUTE | RT5631_R_MUTE,0);
2082 snd_soc_update_bits(codec, RT5631_HP_OUT_VOL,
2083 RT5631_L_MUTE | RT5631_R_MUTE,0);
2087 case SND_SOC_BIAS_PREPARE:
2089 snd_soc_update_bits(codec, RT5631_SPK_OUT_VOL,
2090 RT5631_L_MUTE | RT5631_R_MUTE,
2091 RT5631_L_MUTE | RT5631_R_MUTE);
2092 snd_soc_update_bits(codec, RT5631_HP_OUT_VOL,
2093 RT5631_L_MUTE | RT5631_R_MUTE,
2094 RT5631_L_MUTE | RT5631_R_MUTE);
2096 snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD2,
2097 RT5631_PWR_MICBIAS1_VOL | RT5631_PWR_MICBIAS2_VOL,
2098 RT5631_PWR_MICBIAS1_VOL | RT5631_PWR_MICBIAS2_VOL);
2101 case SND_SOC_BIAS_STANDBY:
2102 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
2103 snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
2104 RT5631_PWR_VREF | RT5631_PWR_MAIN_BIAS,
2105 RT5631_PWR_VREF | RT5631_PWR_MAIN_BIAS);
2107 snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
2108 RT5631_PWR_FAST_VREF_CTRL,
2109 RT5631_PWR_FAST_VREF_CTRL);
2111 snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD2,
2112 RT5631_PWR_MICBIAS1_VOL | RT5631_PWR_MICBIAS2_VOL,
2113 RT5631_PWR_MICBIAS1_VOL | RT5631_PWR_MICBIAS2_VOL);
2115 codec->cache_only = false;
2116 codec->cache_sync = 1;
2117 snd_soc_cache_sync(codec);
2118 rt5631_index_sync(codec);
2122 case SND_SOC_BIAS_OFF:
2123 snd_soc_write(codec, RT5631_PWR_MANAG_ADD1, 0x0000);
2124 snd_soc_write(codec, RT5631_PWR_MANAG_ADD2, 0x0000);
2125 snd_soc_write(codec, RT5631_PWR_MANAG_ADD3, 0x0000);
2126 snd_soc_write(codec, RT5631_PWR_MANAG_ADD4, 0x0000);
2132 codec->dapm.bias_level = level;
2138 * rt5631_index_show - Dump private registers.
2139 * @dev: codec device.
2140 * @attr: device attribute.
2141 * @buf: buffer for display.
2143 * To show non-zero values of all private registers.
2145 * Returns buffer length.
2147 static ssize_t rt5631_index_show(struct device *dev,
2148 struct device_attribute *attr, char *buf)
2150 struct i2c_client *client = to_i2c_client(dev);
2151 struct rt5631_priv *rt5631 = i2c_get_clientdata(client);
2152 struct snd_soc_codec *codec = rt5631->codec;
2156 cnt += sprintf(buf, "RT5631 index register\n");
2157 for (i = 0; i <= 0x23; i++) {
2158 if (cnt + 9 >= PAGE_SIZE - 1)
2160 val = rt5631_index_read(codec, i);
2163 cnt += snprintf(buf + cnt, 10, "%02x: %04x\n", i, val);
2166 if (cnt >= PAGE_SIZE)
2167 cnt = PAGE_SIZE - 1;
2171 static DEVICE_ATTR(index_reg, 0444, rt5631_index_show, NULL);
2173 static int rt5631_probe(struct snd_soc_codec *codec)
2175 struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
2179 ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
2181 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
2185 val = rt5631_index_read(codec, RT5631_ADDA_MIXER_INTL_REG3);
2187 rt5631->codec_version = 1;
2189 rt5631->codec_version = 0;
2191 rt5631_reset(codec);
2192 snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
2193 RT5631_PWR_VREF | RT5631_PWR_MAIN_BIAS,
2194 RT5631_PWR_VREF | RT5631_PWR_MAIN_BIAS);
2196 snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
2197 RT5631_PWR_FAST_VREF_CTRL, RT5631_PWR_FAST_VREF_CTRL);
2198 /* enable HP zero cross */
2199 snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, 0x0f18);
2200 /* power off ClassD auto Recovery */
2201 if (rt5631->codec_version)
2202 snd_soc_update_bits(codec, RT5631_INT_ST_IRQ_CTRL_2,
2205 snd_soc_update_bits(codec, RT5631_INT_ST_IRQ_CTRL_2,
2208 if (rt5631->dmic_used_flag) {
2209 snd_soc_update_bits(codec, RT5631_GPIO_CTRL,
2210 RT5631_GPIO_PIN_FUN_SEL_MASK |
2211 RT5631_GPIO_DMIC_FUN_SEL_MASK,
2212 RT5631_GPIO_PIN_FUN_SEL_GPIO_DIMC |
2213 RT5631_GPIO_DMIC_FUN_SEL_DIMC);
2214 snd_soc_update_bits(codec, RT5631_DIG_MIC_CTRL,
2215 RT5631_DMIC_L_CH_LATCH_MASK |
2216 RT5631_DMIC_R_CH_LATCH_MASK,
2217 RT5631_DMIC_L_CH_LATCH_FALLING |
2218 RT5631_DMIC_R_CH_LATCH_RISING);
2220 rt5631_reg_init(codec);
2222 codec->dapm.bias_level = SND_SOC_BIAS_STANDBY;
2223 rt5631->codec = codec;
2224 rt5631_codec = codec;
2225 snd_soc_add_codec_controls(codec, rt5631_snd_controls,
2226 ARRAY_SIZE(rt5631_snd_controls));
2227 snd_soc_dapm_new_controls(&codec->dapm, rt5631_dapm_widgets,
2228 ARRAY_SIZE(rt5631_dapm_widgets));
2229 snd_soc_dapm_add_routes(&codec->dapm, rt5631_dapm_routes,
2230 ARRAY_SIZE(rt5631_dapm_routes));
2232 ret = device_create_file(codec->dev, &dev_attr_index_reg);
2235 "Failed to create index_reg sysfs files: %d\n", ret);
2242 static int rt5631_remove(struct snd_soc_codec *codec)
2244 rt5631_set_bias_level(codec, SND_SOC_BIAS_OFF);
2249 static int rt5631_suspend(struct snd_soc_codec *codec)
2251 rt5631_set_bias_level(codec, SND_SOC_BIAS_OFF);
2255 static int rt5631_resume(struct snd_soc_codec *codec)
2257 rt5631_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2261 #define rt5631_suspend NULL
2262 #define rt5631_resume NULL
2265 #define RT5631_STEREO_RATES SNDRV_PCM_RATE_8000_96000
2266 #define RT5631_VOICE_RATES SNDRV_PCM_RATE_8000_96000//(SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_8000)
2267 #define RT5631_FORMAT (SNDRV_PCM_FMTBIT_S16_LE | \
2268 SNDRV_PCM_FMTBIT_S20_3LE | \
2269 SNDRV_PCM_FMTBIT_S24_LE | \
2270 SNDRV_PCM_FMTBIT_S8)
2272 static struct snd_soc_dai_ops rt5631_ops = {
2273 .hw_params = rt5631_hifi_pcm_params,
2274 .set_fmt = rt5631_hifi_codec_set_dai_fmt,
2275 .set_sysclk = rt5631_hifi_codec_set_dai_sysclk,
2276 .set_pll = rt5631_codec_set_dai_pll,
2279 static struct snd_soc_dai_driver rt5631_dai[] = {
2281 .name = "RT5631 HiFi",
2284 .stream_name = "HIFI Playback",
2287 .rates = RT5631_STEREO_RATES,
2288 .formats = RT5631_FORMAT,
2291 .stream_name = "HIFI Capture",
2294 .rates = RT5631_STEREO_RATES,
2295 .formats = RT5631_FORMAT,
2300 .name = "rt5631-voice",
2303 .stream_name = "Mono Playback",
2306 .rates = RT5631_VOICE_RATES,
2307 .formats = RT5631_FORMAT,
2310 .stream_name = "Voice HIFI Capture",
2313 .rates = RT5631_VOICE_RATES,
2314 .formats = RT5631_FORMAT,
2320 static struct snd_soc_codec_driver soc_codec_dev_rt5631 = {
2321 .probe = rt5631_probe,
2322 .remove = rt5631_remove,
2323 .suspend = rt5631_suspend,
2324 .resume = rt5631_resume,
2325 .set_bias_level = rt5631_set_bias_level,
2326 .reg_cache_size = RT5631_VENDOR_ID2 + 1,
2327 .reg_word_size = sizeof(u16),
2328 .reg_cache_default = rt5631_reg,
2329 .volatile_register = rt5631_volatile_register,
2330 .readable_register = rt5631_readable_register,
2331 .reg_cache_step = 1,
2332 /*.controls = rt5631_snd_controls,
2333 .num_controls = ARRAY_SIZE(rt5631_snd_controls),
2334 .dapm_widgets = rt5631_dapm_widgets,
2335 .num_dapm_widgets = ARRAY_SIZE(rt5631_dapm_widgets),
2336 .dapm_routes = rt5631_dapm_routes,
2337 .num_dapm_routes = ARRAY_SIZE(rt5631_dapm_routes),*/
2340 static const struct i2c_device_id rt5631_i2c_id[] = {
2344 MODULE_DEVICE_TABLE(i2c, rt5631_i2c_id);
2346 static int rt5631_i2c_probe(struct i2c_client *i2c,
2347 const struct i2c_device_id *id)
2349 struct rt5631_priv *rt5631;
2350 struct device_node *rt5631_np = i2c->dev.of_node;
2354 rt5631 = kzalloc(sizeof(struct rt5631_priv), GFP_KERNEL);
2357 if(!of_property_read_u32(rt5631_np, "rt5631-phone", &val)){
2360 printk("rt5631 use for phone\n");
2367 i2c_set_clientdata(i2c, rt5631);
2369 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt5631,
2370 rt5631_dai, ARRAY_SIZE(rt5631_dai));
2377 static int rt5631_i2c_remove(struct i2c_client *client)
2379 snd_soc_unregister_codec(&client->dev);
2380 kfree(i2c_get_clientdata(client));
2384 static void rt5631_i2c_shutdown(struct i2c_client *client)
2386 struct rt5631_priv *rt5631 = i2c_get_clientdata(client);
2387 struct snd_soc_codec *codec = rt5631->codec;
2390 rt5631_set_bias_level(codec, SND_SOC_BIAS_OFF);
2395 static struct i2c_driver rt5631_i2c_driver = {
2398 .owner = THIS_MODULE,
2400 .probe = rt5631_i2c_probe,
2401 .remove = rt5631_i2c_remove,
2402 .shutdown = rt5631_i2c_shutdown,
2403 .id_table = rt5631_i2c_id,
2406 static int __init rt5631_modinit(void)
2408 return i2c_add_driver(&rt5631_i2c_driver);
2410 module_init(rt5631_modinit);
2412 static void __exit rt5631_modexit(void)
2414 i2c_del_driver(&rt5631_i2c_driver);
2416 module_exit(rt5631_modexit);
2418 MODULE_DESCRIPTION("ASoC RT5631 driver");
2419 MODULE_AUTHOR("flove <flove@realtek.com>");
2420 MODULE_LICENSE("GPL");