1 // SPDX-License-Identifier: GPL-2.0-only
3 * rt5640.c -- RT5640/RT5639 ALSA SoC audio codec driver
5 * Copyright 2011 Realtek Semiconductor Corp.
6 * Author: Johnny Hsu <johnnyhsu@realtek.com>
7 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
15 #include <linux/gpio.h>
16 #include <linux/i2c.h>
17 #include <linux/regmap.h>
19 #include <linux/of_gpio.h>
20 #include <linux/platform_device.h>
21 #include <linux/spi/spi.h>
22 #include <linux/acpi.h>
23 #include <sound/core.h>
24 #include <sound/jack.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
29 #include <sound/initval.h>
30 #include <sound/tlv.h>
35 #define RT5640_DEVICE_ID 0x6231
37 #define RT5640_PR_RANGE_BASE (0xff + 1)
38 #define RT5640_PR_SPACING 0x100
40 #define RT5640_PR_BASE (RT5640_PR_RANGE_BASE + (0 * RT5640_PR_SPACING))
42 static const struct regmap_range_cfg rt5640_ranges
[] = {
43 { .name
= "PR", .range_min
= RT5640_PR_BASE
,
44 .range_max
= RT5640_PR_BASE
+ 0xb4,
45 .selector_reg
= RT5640_PRIV_INDEX
,
46 .selector_mask
= 0xff,
47 .selector_shift
= 0x0,
48 .window_start
= RT5640_PRIV_DATA
,
52 static const struct reg_sequence init_list
[] = {
53 {RT5640_PR_BASE
+ 0x3d, 0x3600},
54 {RT5640_PR_BASE
+ 0x12, 0x0aa8},
55 {RT5640_PR_BASE
+ 0x14, 0x0aaa},
56 {RT5640_PR_BASE
+ 0x20, 0x6110},
57 {RT5640_PR_BASE
+ 0x21, 0xe0e0},
58 {RT5640_PR_BASE
+ 0x23, 0x1804},
61 static const struct reg_default rt5640_reg
[] = {
167 static int rt5640_reset(struct snd_soc_component
*component
)
169 return snd_soc_component_write(component
, RT5640_RESET
, 0);
172 static bool rt5640_volatile_register(struct device
*dev
, unsigned int reg
)
176 for (i
= 0; i
< ARRAY_SIZE(rt5640_ranges
); i
++)
177 if ((reg
>= rt5640_ranges
[i
].window_start
&&
178 reg
<= rt5640_ranges
[i
].window_start
+
179 rt5640_ranges
[i
].window_len
) ||
180 (reg
>= rt5640_ranges
[i
].range_min
&&
181 reg
<= rt5640_ranges
[i
].range_max
))
187 case RT5640_EQ_CTRL1
:
188 case RT5640_DRC_AGC_1
:
189 case RT5640_ANC_CTRL1
:
190 case RT5640_IRQ_CTRL2
:
191 case RT5640_INT_IRQ_ST
:
192 case RT5640_DSP_CTRL2
:
193 case RT5640_DSP_CTRL3
:
194 case RT5640_PRIV_INDEX
:
195 case RT5640_PRIV_DATA
:
196 case RT5640_PGM_REG_ARR1
:
197 case RT5640_PGM_REG_ARR3
:
198 case RT5640_VENDOR_ID
:
199 case RT5640_VENDOR_ID1
:
200 case RT5640_VENDOR_ID2
:
207 static bool rt5640_readable_register(struct device
*dev
, unsigned int reg
)
211 for (i
= 0; i
< ARRAY_SIZE(rt5640_ranges
); i
++)
212 if ((reg
>= rt5640_ranges
[i
].window_start
&&
213 reg
<= rt5640_ranges
[i
].window_start
+
214 rt5640_ranges
[i
].window_len
) ||
215 (reg
>= rt5640_ranges
[i
].range_min
&&
216 reg
<= rt5640_ranges
[i
].range_max
))
224 case RT5640_MONO_OUT
:
227 case RT5640_INL_INR_VOL
:
228 case RT5640_DAC1_DIG_VOL
:
229 case RT5640_DAC2_DIG_VOL
:
230 case RT5640_DAC2_CTRL
:
231 case RT5640_ADC_DIG_VOL
:
232 case RT5640_ADC_DATA
:
233 case RT5640_ADC_BST_VOL
:
234 case RT5640_STO_ADC_MIXER
:
235 case RT5640_MONO_ADC_MIXER
:
236 case RT5640_AD_DA_MIXER
:
237 case RT5640_STO_DAC_MIXER
:
238 case RT5640_MONO_DAC_MIXER
:
239 case RT5640_DIG_MIXER
:
240 case RT5640_DSP_PATH1
:
241 case RT5640_DSP_PATH2
:
242 case RT5640_DIG_INF_DATA
:
243 case RT5640_REC_L1_MIXER
:
244 case RT5640_REC_L2_MIXER
:
245 case RT5640_REC_R1_MIXER
:
246 case RT5640_REC_R2_MIXER
:
247 case RT5640_HPO_MIXER
:
248 case RT5640_SPK_L_MIXER
:
249 case RT5640_SPK_R_MIXER
:
250 case RT5640_SPO_L_MIXER
:
251 case RT5640_SPO_R_MIXER
:
252 case RT5640_SPO_CLSD_RATIO
:
253 case RT5640_MONO_MIXER
:
254 case RT5640_OUT_L1_MIXER
:
255 case RT5640_OUT_L2_MIXER
:
256 case RT5640_OUT_L3_MIXER
:
257 case RT5640_OUT_R1_MIXER
:
258 case RT5640_OUT_R2_MIXER
:
259 case RT5640_OUT_R3_MIXER
:
260 case RT5640_LOUT_MIXER
:
261 case RT5640_PWR_DIG1
:
262 case RT5640_PWR_DIG2
:
263 case RT5640_PWR_ANLG1
:
264 case RT5640_PWR_ANLG2
:
265 case RT5640_PWR_MIXER
:
267 case RT5640_PRIV_INDEX
:
268 case RT5640_PRIV_DATA
:
269 case RT5640_I2S1_SDP
:
270 case RT5640_I2S2_SDP
:
271 case RT5640_ADDA_CLK1
:
272 case RT5640_ADDA_CLK2
:
275 case RT5640_PLL_CTRL1
:
276 case RT5640_PLL_CTRL2
:
283 case RT5640_CLS_D_OVCD
:
284 case RT5640_CLS_D_OUT
:
285 case RT5640_DEPOP_M1
:
286 case RT5640_DEPOP_M2
:
287 case RT5640_DEPOP_M3
:
288 case RT5640_CHARGE_PUMP
:
289 case RT5640_PV_DET_SPK_G
:
291 case RT5640_EQ_CTRL1
:
292 case RT5640_EQ_CTRL2
:
293 case RT5640_WIND_FILTER
:
294 case RT5640_DRC_AGC_1
:
295 case RT5640_DRC_AGC_2
:
296 case RT5640_DRC_AGC_3
:
298 case RT5640_ANC_CTRL1
:
299 case RT5640_ANC_CTRL2
:
300 case RT5640_ANC_CTRL3
:
303 case RT5640_IRQ_CTRL1
:
304 case RT5640_IRQ_CTRL2
:
305 case RT5640_INT_IRQ_ST
:
306 case RT5640_GPIO_CTRL1
:
307 case RT5640_GPIO_CTRL2
:
308 case RT5640_GPIO_CTRL3
:
309 case RT5640_DSP_CTRL1
:
310 case RT5640_DSP_CTRL2
:
311 case RT5640_DSP_CTRL3
:
312 case RT5640_DSP_CTRL4
:
313 case RT5640_PGM_REG_ARR1
:
314 case RT5640_PGM_REG_ARR2
:
315 case RT5640_PGM_REG_ARR3
:
316 case RT5640_PGM_REG_ARR4
:
317 case RT5640_PGM_REG_ARR5
:
318 case RT5640_SCB_FUNC
:
319 case RT5640_SCB_CTRL
:
320 case RT5640_BASE_BACK
:
321 case RT5640_MP3_PLUS1
:
322 case RT5640_MP3_PLUS2
:
325 case RT5640_HP_CALIB_AMP_DET
:
326 case RT5640_HP_CALIB2
:
332 case RT5640_VENDOR_ID
:
333 case RT5640_VENDOR_ID1
:
334 case RT5640_VENDOR_ID2
:
341 static const DECLARE_TLV_DB_SCALE(out_vol_tlv
, -4650, 150, 0);
342 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv
, -65625, 375, 0);
343 static const DECLARE_TLV_DB_SCALE(in_vol_tlv
, -3450, 150, 0);
344 static const DECLARE_TLV_DB_SCALE(adc_vol_tlv
, -17625, 375, 0);
345 static const DECLARE_TLV_DB_SCALE(adc_bst_tlv
, 0, 1200, 0);
347 /* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
348 static const DECLARE_TLV_DB_RANGE(bst_tlv
,
349 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
350 1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
351 2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
352 3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
353 6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
354 7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
355 8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0)
358 /* Interface data select */
359 static const char * const rt5640_data_select
[] = {
360 "Normal", "Swap", "left copy to right", "right copy to left"};
362 static SOC_ENUM_SINGLE_DECL(rt5640_if1_dac_enum
, RT5640_DIG_INF_DATA
,
363 RT5640_IF1_DAC_SEL_SFT
, rt5640_data_select
);
365 static SOC_ENUM_SINGLE_DECL(rt5640_if1_adc_enum
, RT5640_DIG_INF_DATA
,
366 RT5640_IF1_ADC_SEL_SFT
, rt5640_data_select
);
368 static SOC_ENUM_SINGLE_DECL(rt5640_if2_dac_enum
, RT5640_DIG_INF_DATA
,
369 RT5640_IF2_DAC_SEL_SFT
, rt5640_data_select
);
371 static SOC_ENUM_SINGLE_DECL(rt5640_if2_adc_enum
, RT5640_DIG_INF_DATA
,
372 RT5640_IF2_ADC_SEL_SFT
, rt5640_data_select
);
374 /* Class D speaker gain ratio */
375 static const char * const rt5640_clsd_spk_ratio
[] = {"1.66x", "1.83x", "1.94x",
376 "2x", "2.11x", "2.22x", "2.33x", "2.44x", "2.55x", "2.66x", "2.77x"};
378 static SOC_ENUM_SINGLE_DECL(rt5640_clsd_spk_ratio_enum
, RT5640_CLS_D_OUT
,
379 RT5640_CLSD_RATIO_SFT
, rt5640_clsd_spk_ratio
);
381 static const struct snd_kcontrol_new rt5640_snd_controls
[] = {
382 /* Speaker Output Volume */
383 SOC_DOUBLE("Speaker Channel Switch", RT5640_SPK_VOL
,
384 RT5640_VOL_L_SFT
, RT5640_VOL_R_SFT
, 1, 1),
385 SOC_DOUBLE_TLV("Speaker Playback Volume", RT5640_SPK_VOL
,
386 RT5640_L_VOL_SFT
, RT5640_R_VOL_SFT
, 39, 1, out_vol_tlv
),
387 /* Headphone Output Volume */
388 SOC_DOUBLE("HP Channel Switch", RT5640_HP_VOL
,
389 RT5640_VOL_L_SFT
, RT5640_VOL_R_SFT
, 1, 1),
390 SOC_DOUBLE_TLV("HP Playback Volume", RT5640_HP_VOL
,
391 RT5640_L_VOL_SFT
, RT5640_R_VOL_SFT
, 39, 1, out_vol_tlv
),
393 SOC_DOUBLE("OUT Playback Switch", RT5640_OUTPUT
,
394 RT5640_L_MUTE_SFT
, RT5640_R_MUTE_SFT
, 1, 1),
395 SOC_DOUBLE("OUT Channel Switch", RT5640_OUTPUT
,
396 RT5640_VOL_L_SFT
, RT5640_VOL_R_SFT
, 1, 1),
397 SOC_DOUBLE_TLV("OUT Playback Volume", RT5640_OUTPUT
,
398 RT5640_L_VOL_SFT
, RT5640_R_VOL_SFT
, 39, 1, out_vol_tlv
),
400 /* DAC Digital Volume */
401 SOC_DOUBLE("DAC2 Playback Switch", RT5640_DAC2_CTRL
,
402 RT5640_M_DAC_L2_VOL_SFT
, RT5640_M_DAC_R2_VOL_SFT
, 1, 1),
403 SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5640_DAC1_DIG_VOL
,
404 RT5640_L_VOL_SFT
, RT5640_R_VOL_SFT
,
405 175, 0, dac_vol_tlv
),
406 /* IN1/IN2/IN3 Control */
407 SOC_SINGLE_TLV("IN1 Boost", RT5640_IN1_IN2
,
408 RT5640_BST_SFT1
, 8, 0, bst_tlv
),
409 SOC_SINGLE_TLV("IN2 Boost", RT5640_IN3_IN4
,
410 RT5640_BST_SFT2
, 8, 0, bst_tlv
),
411 SOC_SINGLE_TLV("IN3 Boost", RT5640_IN1_IN2
,
412 RT5640_BST_SFT2
, 8, 0, bst_tlv
),
414 /* INL/INR Volume Control */
415 SOC_DOUBLE_TLV("IN Capture Volume", RT5640_INL_INR_VOL
,
416 RT5640_INL_VOL_SFT
, RT5640_INR_VOL_SFT
,
418 /* ADC Digital Volume Control */
419 SOC_DOUBLE("ADC Capture Switch", RT5640_ADC_DIG_VOL
,
420 RT5640_L_MUTE_SFT
, RT5640_R_MUTE_SFT
, 1, 1),
421 SOC_DOUBLE_TLV("ADC Capture Volume", RT5640_ADC_DIG_VOL
,
422 RT5640_L_VOL_SFT
, RT5640_R_VOL_SFT
,
423 127, 0, adc_vol_tlv
),
424 SOC_DOUBLE("Mono ADC Capture Switch", RT5640_DUMMY1
,
425 RT5640_M_MONO_ADC_L_SFT
, RT5640_M_MONO_ADC_R_SFT
, 1, 1),
426 SOC_DOUBLE_TLV("Mono ADC Capture Volume", RT5640_ADC_DATA
,
427 RT5640_L_VOL_SFT
, RT5640_R_VOL_SFT
,
428 127, 0, adc_vol_tlv
),
429 /* ADC Boost Volume Control */
430 SOC_DOUBLE_TLV("ADC Boost Gain", RT5640_ADC_BST_VOL
,
431 RT5640_ADC_L_BST_SFT
, RT5640_ADC_R_BST_SFT
,
433 /* Class D speaker gain ratio */
434 SOC_ENUM("Class D SPK Ratio Control", rt5640_clsd_spk_ratio_enum
),
436 SOC_ENUM("ADC IF1 Data Switch", rt5640_if1_adc_enum
),
437 SOC_ENUM("DAC IF1 Data Switch", rt5640_if1_dac_enum
),
438 SOC_ENUM("ADC IF2 Data Switch", rt5640_if2_adc_enum
),
439 SOC_ENUM("DAC IF2 Data Switch", rt5640_if2_dac_enum
),
442 static const struct snd_kcontrol_new rt5640_specific_snd_controls
[] = {
443 /* MONO Output Control */
444 SOC_SINGLE("Mono Playback Switch", RT5640_MONO_OUT
, RT5640_L_MUTE_SFT
,
447 SOC_DOUBLE_TLV("Mono DAC Playback Volume", RT5640_DAC2_DIG_VOL
,
448 RT5640_L_VOL_SFT
, RT5640_R_VOL_SFT
, 175, 0, dac_vol_tlv
),
452 * set_dmic_clk - Set parameter of dmic.
455 * @kcontrol: The kcontrol of this widget.
459 static int set_dmic_clk(struct snd_soc_dapm_widget
*w
,
460 struct snd_kcontrol
*kcontrol
, int event
)
462 struct snd_soc_component
*component
= snd_soc_dapm_to_component(w
->dapm
);
463 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
466 rate
= rt5640
->sysclk
/ rl6231_get_pre_div(rt5640
->regmap
,
467 RT5640_ADDA_CLK1
, RT5640_I2S_PD1_SFT
);
468 idx
= rl6231_calc_dmic_clk(rate
);
470 dev_err(component
->dev
, "Failed to set DMIC clock\n");
472 snd_soc_component_update_bits(component
, RT5640_DMIC
, RT5640_DMIC_CLK_MASK
,
473 idx
<< RT5640_DMIC_CLK_SFT
);
477 static int is_using_asrc(struct snd_soc_dapm_widget
*source
,
478 struct snd_soc_dapm_widget
*sink
)
480 struct snd_soc_component
*component
= snd_soc_dapm_to_component(source
->dapm
);
481 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
483 if (!rt5640
->asrc_en
)
490 static const struct snd_kcontrol_new rt5640_sto_adc_l_mix
[] = {
491 SOC_DAPM_SINGLE("ADC1 Switch", RT5640_STO_ADC_MIXER
,
492 RT5640_M_ADC_L1_SFT
, 1, 1),
493 SOC_DAPM_SINGLE("ADC2 Switch", RT5640_STO_ADC_MIXER
,
494 RT5640_M_ADC_L2_SFT
, 1, 1),
497 static const struct snd_kcontrol_new rt5640_sto_adc_r_mix
[] = {
498 SOC_DAPM_SINGLE("ADC1 Switch", RT5640_STO_ADC_MIXER
,
499 RT5640_M_ADC_R1_SFT
, 1, 1),
500 SOC_DAPM_SINGLE("ADC2 Switch", RT5640_STO_ADC_MIXER
,
501 RT5640_M_ADC_R2_SFT
, 1, 1),
504 static const struct snd_kcontrol_new rt5640_mono_adc_l_mix
[] = {
505 SOC_DAPM_SINGLE("ADC1 Switch", RT5640_MONO_ADC_MIXER
,
506 RT5640_M_MONO_ADC_L1_SFT
, 1, 1),
507 SOC_DAPM_SINGLE("ADC2 Switch", RT5640_MONO_ADC_MIXER
,
508 RT5640_M_MONO_ADC_L2_SFT
, 1, 1),
511 static const struct snd_kcontrol_new rt5640_mono_adc_r_mix
[] = {
512 SOC_DAPM_SINGLE("ADC1 Switch", RT5640_MONO_ADC_MIXER
,
513 RT5640_M_MONO_ADC_R1_SFT
, 1, 1),
514 SOC_DAPM_SINGLE("ADC2 Switch", RT5640_MONO_ADC_MIXER
,
515 RT5640_M_MONO_ADC_R2_SFT
, 1, 1),
518 static const struct snd_kcontrol_new rt5640_dac_l_mix
[] = {
519 SOC_DAPM_SINGLE("Stereo ADC Switch", RT5640_AD_DA_MIXER
,
520 RT5640_M_ADCMIX_L_SFT
, 1, 1),
521 SOC_DAPM_SINGLE("INF1 Switch", RT5640_AD_DA_MIXER
,
522 RT5640_M_IF1_DAC_L_SFT
, 1, 1),
525 static const struct snd_kcontrol_new rt5640_dac_r_mix
[] = {
526 SOC_DAPM_SINGLE("Stereo ADC Switch", RT5640_AD_DA_MIXER
,
527 RT5640_M_ADCMIX_R_SFT
, 1, 1),
528 SOC_DAPM_SINGLE("INF1 Switch", RT5640_AD_DA_MIXER
,
529 RT5640_M_IF1_DAC_R_SFT
, 1, 1),
532 static const struct snd_kcontrol_new rt5640_sto_dac_l_mix
[] = {
533 SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_STO_DAC_MIXER
,
534 RT5640_M_DAC_L1_SFT
, 1, 1),
535 SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_STO_DAC_MIXER
,
536 RT5640_M_DAC_L2_SFT
, 1, 1),
537 SOC_DAPM_SINGLE("ANC Switch", RT5640_STO_DAC_MIXER
,
538 RT5640_M_ANC_DAC_L_SFT
, 1, 1),
541 static const struct snd_kcontrol_new rt5640_sto_dac_r_mix
[] = {
542 SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_STO_DAC_MIXER
,
543 RT5640_M_DAC_R1_SFT
, 1, 1),
544 SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_STO_DAC_MIXER
,
545 RT5640_M_DAC_R2_SFT
, 1, 1),
546 SOC_DAPM_SINGLE("ANC Switch", RT5640_STO_DAC_MIXER
,
547 RT5640_M_ANC_DAC_R_SFT
, 1, 1),
550 static const struct snd_kcontrol_new rt5639_sto_dac_l_mix
[] = {
551 SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_STO_DAC_MIXER
,
552 RT5640_M_DAC_L1_SFT
, 1, 1),
553 SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_STO_DAC_MIXER
,
554 RT5640_M_DAC_L2_SFT
, 1, 1),
557 static const struct snd_kcontrol_new rt5639_sto_dac_r_mix
[] = {
558 SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_STO_DAC_MIXER
,
559 RT5640_M_DAC_R1_SFT
, 1, 1),
560 SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_STO_DAC_MIXER
,
561 RT5640_M_DAC_R2_SFT
, 1, 1),
564 static const struct snd_kcontrol_new rt5640_mono_dac_l_mix
[] = {
565 SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_MONO_DAC_MIXER
,
566 RT5640_M_DAC_L1_MONO_L_SFT
, 1, 1),
567 SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_DAC_MIXER
,
568 RT5640_M_DAC_L2_MONO_L_SFT
, 1, 1),
569 SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_DAC_MIXER
,
570 RT5640_M_DAC_R2_MONO_L_SFT
, 1, 1),
573 static const struct snd_kcontrol_new rt5640_mono_dac_r_mix
[] = {
574 SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_MONO_DAC_MIXER
,
575 RT5640_M_DAC_R1_MONO_R_SFT
, 1, 1),
576 SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_DAC_MIXER
,
577 RT5640_M_DAC_R2_MONO_R_SFT
, 1, 1),
578 SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_DAC_MIXER
,
579 RT5640_M_DAC_L2_MONO_R_SFT
, 1, 1),
582 static const struct snd_kcontrol_new rt5640_dig_l_mix
[] = {
583 SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_DIG_MIXER
,
584 RT5640_M_STO_L_DAC_L_SFT
, 1, 1),
585 SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_DIG_MIXER
,
586 RT5640_M_DAC_L2_DAC_L_SFT
, 1, 1),
589 static const struct snd_kcontrol_new rt5640_dig_r_mix
[] = {
590 SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_DIG_MIXER
,
591 RT5640_M_STO_R_DAC_R_SFT
, 1, 1),
592 SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_DIG_MIXER
,
593 RT5640_M_DAC_R2_DAC_R_SFT
, 1, 1),
596 /* Analog Input Mixer */
597 static const struct snd_kcontrol_new rt5640_rec_l_mix
[] = {
598 SOC_DAPM_SINGLE("HPOL Switch", RT5640_REC_L2_MIXER
,
599 RT5640_M_HP_L_RM_L_SFT
, 1, 1),
600 SOC_DAPM_SINGLE("INL Switch", RT5640_REC_L2_MIXER
,
601 RT5640_M_IN_L_RM_L_SFT
, 1, 1),
602 SOC_DAPM_SINGLE("BST3 Switch", RT5640_REC_L2_MIXER
,
603 RT5640_M_BST2_RM_L_SFT
, 1, 1),
604 SOC_DAPM_SINGLE("BST2 Switch", RT5640_REC_L2_MIXER
,
605 RT5640_M_BST4_RM_L_SFT
, 1, 1),
606 SOC_DAPM_SINGLE("BST1 Switch", RT5640_REC_L2_MIXER
,
607 RT5640_M_BST1_RM_L_SFT
, 1, 1),
608 SOC_DAPM_SINGLE("OUT MIXL Switch", RT5640_REC_L2_MIXER
,
609 RT5640_M_OM_L_RM_L_SFT
, 1, 1),
612 static const struct snd_kcontrol_new rt5640_rec_r_mix
[] = {
613 SOC_DAPM_SINGLE("HPOR Switch", RT5640_REC_R2_MIXER
,
614 RT5640_M_HP_R_RM_R_SFT
, 1, 1),
615 SOC_DAPM_SINGLE("INR Switch", RT5640_REC_R2_MIXER
,
616 RT5640_M_IN_R_RM_R_SFT
, 1, 1),
617 SOC_DAPM_SINGLE("BST3 Switch", RT5640_REC_R2_MIXER
,
618 RT5640_M_BST2_RM_R_SFT
, 1, 1),
619 SOC_DAPM_SINGLE("BST2 Switch", RT5640_REC_R2_MIXER
,
620 RT5640_M_BST4_RM_R_SFT
, 1, 1),
621 SOC_DAPM_SINGLE("BST1 Switch", RT5640_REC_R2_MIXER
,
622 RT5640_M_BST1_RM_R_SFT
, 1, 1),
623 SOC_DAPM_SINGLE("OUT MIXR Switch", RT5640_REC_R2_MIXER
,
624 RT5640_M_OM_R_RM_R_SFT
, 1, 1),
627 /* Analog Output Mixer */
628 static const struct snd_kcontrol_new rt5640_spk_l_mix
[] = {
629 SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_SPK_L_MIXER
,
630 RT5640_M_RM_L_SM_L_SFT
, 1, 1),
631 SOC_DAPM_SINGLE("INL Switch", RT5640_SPK_L_MIXER
,
632 RT5640_M_IN_L_SM_L_SFT
, 1, 1),
633 SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_SPK_L_MIXER
,
634 RT5640_M_DAC_L1_SM_L_SFT
, 1, 1),
635 SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_SPK_L_MIXER
,
636 RT5640_M_DAC_L2_SM_L_SFT
, 1, 1),
637 SOC_DAPM_SINGLE("OUT MIXL Switch", RT5640_SPK_L_MIXER
,
638 RT5640_M_OM_L_SM_L_SFT
, 1, 1),
641 static const struct snd_kcontrol_new rt5640_spk_r_mix
[] = {
642 SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_SPK_R_MIXER
,
643 RT5640_M_RM_R_SM_R_SFT
, 1, 1),
644 SOC_DAPM_SINGLE("INR Switch", RT5640_SPK_R_MIXER
,
645 RT5640_M_IN_R_SM_R_SFT
, 1, 1),
646 SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPK_R_MIXER
,
647 RT5640_M_DAC_R1_SM_R_SFT
, 1, 1),
648 SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_SPK_R_MIXER
,
649 RT5640_M_DAC_R2_SM_R_SFT
, 1, 1),
650 SOC_DAPM_SINGLE("OUT MIXR Switch", RT5640_SPK_R_MIXER
,
651 RT5640_M_OM_R_SM_R_SFT
, 1, 1),
654 static const struct snd_kcontrol_new rt5640_out_l_mix
[] = {
655 SOC_DAPM_SINGLE("SPK MIXL Switch", RT5640_OUT_L3_MIXER
,
656 RT5640_M_SM_L_OM_L_SFT
, 1, 1),
657 SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_L3_MIXER
,
658 RT5640_M_BST1_OM_L_SFT
, 1, 1),
659 SOC_DAPM_SINGLE("INL Switch", RT5640_OUT_L3_MIXER
,
660 RT5640_M_IN_L_OM_L_SFT
, 1, 1),
661 SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_OUT_L3_MIXER
,
662 RT5640_M_RM_L_OM_L_SFT
, 1, 1),
663 SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_OUT_L3_MIXER
,
664 RT5640_M_DAC_R2_OM_L_SFT
, 1, 1),
665 SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_OUT_L3_MIXER
,
666 RT5640_M_DAC_L2_OM_L_SFT
, 1, 1),
667 SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_OUT_L3_MIXER
,
668 RT5640_M_DAC_L1_OM_L_SFT
, 1, 1),
671 static const struct snd_kcontrol_new rt5640_out_r_mix
[] = {
672 SOC_DAPM_SINGLE("SPK MIXR Switch", RT5640_OUT_R3_MIXER
,
673 RT5640_M_SM_L_OM_R_SFT
, 1, 1),
674 SOC_DAPM_SINGLE("BST2 Switch", RT5640_OUT_R3_MIXER
,
675 RT5640_M_BST4_OM_R_SFT
, 1, 1),
676 SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_R3_MIXER
,
677 RT5640_M_BST1_OM_R_SFT
, 1, 1),
678 SOC_DAPM_SINGLE("INR Switch", RT5640_OUT_R3_MIXER
,
679 RT5640_M_IN_R_OM_R_SFT
, 1, 1),
680 SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_OUT_R3_MIXER
,
681 RT5640_M_RM_R_OM_R_SFT
, 1, 1),
682 SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_OUT_R3_MIXER
,
683 RT5640_M_DAC_L2_OM_R_SFT
, 1, 1),
684 SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_OUT_R3_MIXER
,
685 RT5640_M_DAC_R2_OM_R_SFT
, 1, 1),
686 SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_OUT_R3_MIXER
,
687 RT5640_M_DAC_R1_OM_R_SFT
, 1, 1),
690 static const struct snd_kcontrol_new rt5639_out_l_mix
[] = {
691 SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_L3_MIXER
,
692 RT5640_M_BST1_OM_L_SFT
, 1, 1),
693 SOC_DAPM_SINGLE("INL Switch", RT5640_OUT_L3_MIXER
,
694 RT5640_M_IN_L_OM_L_SFT
, 1, 1),
695 SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_OUT_L3_MIXER
,
696 RT5640_M_RM_L_OM_L_SFT
, 1, 1),
697 SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_OUT_L3_MIXER
,
698 RT5640_M_DAC_L1_OM_L_SFT
, 1, 1),
701 static const struct snd_kcontrol_new rt5639_out_r_mix
[] = {
702 SOC_DAPM_SINGLE("BST2 Switch", RT5640_OUT_R3_MIXER
,
703 RT5640_M_BST4_OM_R_SFT
, 1, 1),
704 SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_R3_MIXER
,
705 RT5640_M_BST1_OM_R_SFT
, 1, 1),
706 SOC_DAPM_SINGLE("INR Switch", RT5640_OUT_R3_MIXER
,
707 RT5640_M_IN_R_OM_R_SFT
, 1, 1),
708 SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_OUT_R3_MIXER
,
709 RT5640_M_RM_R_OM_R_SFT
, 1, 1),
710 SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_OUT_R3_MIXER
,
711 RT5640_M_DAC_R1_OM_R_SFT
, 1, 1),
714 static const struct snd_kcontrol_new rt5640_spo_l_mix
[] = {
715 SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPO_L_MIXER
,
716 RT5640_M_DAC_R1_SPM_L_SFT
, 1, 1),
717 SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_SPO_L_MIXER
,
718 RT5640_M_DAC_L1_SPM_L_SFT
, 1, 1),
719 SOC_DAPM_SINGLE("SPKVOL R Switch", RT5640_SPO_L_MIXER
,
720 RT5640_M_SV_R_SPM_L_SFT
, 1, 1),
721 SOC_DAPM_SINGLE("SPKVOL L Switch", RT5640_SPO_L_MIXER
,
722 RT5640_M_SV_L_SPM_L_SFT
, 1, 1),
723 SOC_DAPM_SINGLE("BST1 Switch", RT5640_SPO_L_MIXER
,
724 RT5640_M_BST1_SPM_L_SFT
, 1, 1),
727 static const struct snd_kcontrol_new rt5640_spo_r_mix
[] = {
728 SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPO_R_MIXER
,
729 RT5640_M_DAC_R1_SPM_R_SFT
, 1, 1),
730 SOC_DAPM_SINGLE("SPKVOL R Switch", RT5640_SPO_R_MIXER
,
731 RT5640_M_SV_R_SPM_R_SFT
, 1, 1),
732 SOC_DAPM_SINGLE("BST1 Switch", RT5640_SPO_R_MIXER
,
733 RT5640_M_BST1_SPM_R_SFT
, 1, 1),
736 static const struct snd_kcontrol_new rt5640_hpo_mix
[] = {
737 SOC_DAPM_SINGLE("HPO MIX DAC2 Switch", RT5640_HPO_MIXER
,
738 RT5640_M_DAC2_HM_SFT
, 1, 1),
739 SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5640_HPO_MIXER
,
740 RT5640_M_DAC1_HM_SFT
, 1, 1),
741 SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5640_HPO_MIXER
,
742 RT5640_M_HPVOL_HM_SFT
, 1, 1),
745 static const struct snd_kcontrol_new rt5639_hpo_mix
[] = {
746 SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5640_HPO_MIXER
,
747 RT5640_M_DAC1_HM_SFT
, 1, 1),
748 SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5640_HPO_MIXER
,
749 RT5640_M_HPVOL_HM_SFT
, 1, 1),
752 static const struct snd_kcontrol_new rt5640_lout_mix
[] = {
753 SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_LOUT_MIXER
,
754 RT5640_M_DAC_L1_LM_SFT
, 1, 1),
755 SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_LOUT_MIXER
,
756 RT5640_M_DAC_R1_LM_SFT
, 1, 1),
757 SOC_DAPM_SINGLE("OUTVOL L Switch", RT5640_LOUT_MIXER
,
758 RT5640_M_OV_L_LM_SFT
, 1, 1),
759 SOC_DAPM_SINGLE("OUTVOL R Switch", RT5640_LOUT_MIXER
,
760 RT5640_M_OV_R_LM_SFT
, 1, 1),
763 static const struct snd_kcontrol_new rt5640_mono_mix
[] = {
764 SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_MIXER
,
765 RT5640_M_DAC_R2_MM_SFT
, 1, 1),
766 SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_MIXER
,
767 RT5640_M_DAC_L2_MM_SFT
, 1, 1),
768 SOC_DAPM_SINGLE("OUTVOL R Switch", RT5640_MONO_MIXER
,
769 RT5640_M_OV_R_MM_SFT
, 1, 1),
770 SOC_DAPM_SINGLE("OUTVOL L Switch", RT5640_MONO_MIXER
,
771 RT5640_M_OV_L_MM_SFT
, 1, 1),
772 SOC_DAPM_SINGLE("BST1 Switch", RT5640_MONO_MIXER
,
773 RT5640_M_BST1_MM_SFT
, 1, 1),
776 static const struct snd_kcontrol_new spk_l_enable_control
=
777 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_SPK_VOL
,
778 RT5640_L_MUTE_SFT
, 1, 1);
780 static const struct snd_kcontrol_new spk_r_enable_control
=
781 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_SPK_VOL
,
782 RT5640_R_MUTE_SFT
, 1, 1);
784 static const struct snd_kcontrol_new hp_l_enable_control
=
785 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_HP_VOL
,
786 RT5640_L_MUTE_SFT
, 1, 1);
788 static const struct snd_kcontrol_new hp_r_enable_control
=
789 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_HP_VOL
,
790 RT5640_R_MUTE_SFT
, 1, 1);
792 /* Stereo ADC source */
793 static const char * const rt5640_stereo_adc1_src
[] = {
797 static SOC_ENUM_SINGLE_DECL(rt5640_stereo_adc1_enum
, RT5640_STO_ADC_MIXER
,
798 RT5640_ADC_1_SRC_SFT
, rt5640_stereo_adc1_src
);
800 static const struct snd_kcontrol_new rt5640_sto_adc_1_mux
=
801 SOC_DAPM_ENUM("Stereo ADC1 Mux", rt5640_stereo_adc1_enum
);
803 static const char * const rt5640_stereo_adc2_src
[] = {
804 "DMIC1", "DMIC2", "DIG MIX"
807 static SOC_ENUM_SINGLE_DECL(rt5640_stereo_adc2_enum
, RT5640_STO_ADC_MIXER
,
808 RT5640_ADC_2_SRC_SFT
, rt5640_stereo_adc2_src
);
810 static const struct snd_kcontrol_new rt5640_sto_adc_2_mux
=
811 SOC_DAPM_ENUM("Stereo ADC2 Mux", rt5640_stereo_adc2_enum
);
813 /* Mono ADC source */
814 static const char * const rt5640_mono_adc_l1_src
[] = {
815 "Mono DAC MIXL", "ADCL"
818 static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_l1_enum
, RT5640_MONO_ADC_MIXER
,
819 RT5640_MONO_ADC_L1_SRC_SFT
, rt5640_mono_adc_l1_src
);
821 static const struct snd_kcontrol_new rt5640_mono_adc_l1_mux
=
822 SOC_DAPM_ENUM("Mono ADC1 left source", rt5640_mono_adc_l1_enum
);
824 static const char * const rt5640_mono_adc_l2_src
[] = {
825 "DMIC L1", "DMIC L2", "Mono DAC MIXL"
828 static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_l2_enum
, RT5640_MONO_ADC_MIXER
,
829 RT5640_MONO_ADC_L2_SRC_SFT
, rt5640_mono_adc_l2_src
);
831 static const struct snd_kcontrol_new rt5640_mono_adc_l2_mux
=
832 SOC_DAPM_ENUM("Mono ADC2 left source", rt5640_mono_adc_l2_enum
);
834 static const char * const rt5640_mono_adc_r1_src
[] = {
835 "Mono DAC MIXR", "ADCR"
838 static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_r1_enum
, RT5640_MONO_ADC_MIXER
,
839 RT5640_MONO_ADC_R1_SRC_SFT
, rt5640_mono_adc_r1_src
);
841 static const struct snd_kcontrol_new rt5640_mono_adc_r1_mux
=
842 SOC_DAPM_ENUM("Mono ADC1 right source", rt5640_mono_adc_r1_enum
);
844 static const char * const rt5640_mono_adc_r2_src
[] = {
845 "DMIC R1", "DMIC R2", "Mono DAC MIXR"
848 static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_r2_enum
, RT5640_MONO_ADC_MIXER
,
849 RT5640_MONO_ADC_R2_SRC_SFT
, rt5640_mono_adc_r2_src
);
851 static const struct snd_kcontrol_new rt5640_mono_adc_r2_mux
=
852 SOC_DAPM_ENUM("Mono ADC2 right source", rt5640_mono_adc_r2_enum
);
854 /* DAC2 channel source */
855 static const char * const rt5640_dac_l2_src
[] = {
859 static int rt5640_dac_l2_values
[] = {
864 static SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dac_l2_enum
,
865 RT5640_DSP_PATH2
, RT5640_DAC_L2_SEL_SFT
,
866 0x3, rt5640_dac_l2_src
, rt5640_dac_l2_values
);
868 static const struct snd_kcontrol_new rt5640_dac_l2_mux
=
869 SOC_DAPM_ENUM("DAC2 left channel source", rt5640_dac_l2_enum
);
871 static const char * const rt5640_dac_r2_src
[] = {
875 static int rt5640_dac_r2_values
[] = {
879 static SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dac_r2_enum
,
880 RT5640_DSP_PATH2
, RT5640_DAC_R2_SEL_SFT
,
881 0x3, rt5640_dac_r2_src
, rt5640_dac_r2_values
);
883 static const struct snd_kcontrol_new rt5640_dac_r2_mux
=
884 SOC_DAPM_ENUM("DAC2 right channel source", rt5640_dac_r2_enum
);
886 /* digital interface and iis interface map */
887 static const char * const rt5640_dai_iis_map
[] = {
888 "1:1|2:2", "1:2|2:1", "1:1|2:1", "1:2|2:2"
891 static int rt5640_dai_iis_map_values
[] = {
898 static SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dai_iis_map_enum
,
899 RT5640_I2S1_SDP
, RT5640_I2S_IF_SFT
,
900 0x7, rt5640_dai_iis_map
,
901 rt5640_dai_iis_map_values
);
903 static const struct snd_kcontrol_new rt5640_dai_mux
=
904 SOC_DAPM_ENUM("DAI select", rt5640_dai_iis_map_enum
);
907 static const char * const rt5640_sdi_sel
[] = {
911 static SOC_ENUM_SINGLE_DECL(rt5640_sdi_sel_enum
, RT5640_I2S2_SDP
,
912 RT5640_I2S2_SDI_SFT
, rt5640_sdi_sel
);
914 static const struct snd_kcontrol_new rt5640_sdi_mux
=
915 SOC_DAPM_ENUM("SDI select", rt5640_sdi_sel_enum
);
917 static void hp_amp_power_on(struct snd_soc_component
*component
)
919 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
921 /* depop parameters */
922 regmap_update_bits(rt5640
->regmap
, RT5640_PR_BASE
+
923 RT5640_CHPUMP_INT_REG1
, 0x0700, 0x0200);
924 regmap_update_bits(rt5640
->regmap
, RT5640_DEPOP_M2
,
925 RT5640_DEPOP_MASK
, RT5640_DEPOP_MAN
);
926 regmap_update_bits(rt5640
->regmap
, RT5640_DEPOP_M1
,
927 RT5640_HP_CP_MASK
| RT5640_HP_SG_MASK
| RT5640_HP_CB_MASK
,
928 RT5640_HP_CP_PU
| RT5640_HP_SG_DIS
| RT5640_HP_CB_PU
);
929 regmap_write(rt5640
->regmap
, RT5640_PR_BASE
+ RT5640_HP_DCC_INT1
,
931 /* headphone amp power on */
932 regmap_update_bits(rt5640
->regmap
, RT5640_PWR_ANLG1
,
933 RT5640_PWR_FV1
| RT5640_PWR_FV2
, 0);
934 regmap_update_bits(rt5640
->regmap
, RT5640_PWR_ANLG1
,
937 usleep_range(10000, 15000);
938 regmap_update_bits(rt5640
->regmap
, RT5640_PWR_ANLG1
,
939 RT5640_PWR_FV1
| RT5640_PWR_FV2
,
940 RT5640_PWR_FV1
| RT5640_PWR_FV2
);
943 static void rt5640_pmu_depop(struct snd_soc_component
*component
)
945 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
947 regmap_update_bits(rt5640
->regmap
, RT5640_DEPOP_M2
,
948 RT5640_DEPOP_MASK
| RT5640_DIG_DP_MASK
,
949 RT5640_DEPOP_AUTO
| RT5640_DIG_DP_EN
);
950 regmap_update_bits(rt5640
->regmap
, RT5640_CHARGE_PUMP
,
951 RT5640_PM_HP_MASK
, RT5640_PM_HP_HV
);
953 regmap_update_bits(rt5640
->regmap
, RT5640_DEPOP_M3
,
954 RT5640_CP_FQ1_MASK
| RT5640_CP_FQ2_MASK
| RT5640_CP_FQ3_MASK
,
955 (RT5640_CP_FQ_192_KHZ
<< RT5640_CP_FQ1_SFT
) |
956 (RT5640_CP_FQ_12_KHZ
<< RT5640_CP_FQ2_SFT
) |
957 (RT5640_CP_FQ_192_KHZ
<< RT5640_CP_FQ3_SFT
));
959 regmap_write(rt5640
->regmap
, RT5640_PR_BASE
+
960 RT5640_MAMP_INT_REG2
, 0x1c00);
961 regmap_update_bits(rt5640
->regmap
, RT5640_DEPOP_M1
,
962 RT5640_HP_CP_MASK
| RT5640_HP_SG_MASK
,
963 RT5640_HP_CP_PD
| RT5640_HP_SG_EN
);
964 regmap_update_bits(rt5640
->regmap
, RT5640_PR_BASE
+
965 RT5640_CHPUMP_INT_REG1
, 0x0700, 0x0400);
968 static int rt5640_hp_event(struct snd_soc_dapm_widget
*w
,
969 struct snd_kcontrol
*kcontrol
, int event
)
971 struct snd_soc_component
*component
= snd_soc_dapm_to_component(w
->dapm
);
972 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
975 case SND_SOC_DAPM_POST_PMU
:
976 rt5640_pmu_depop(component
);
977 rt5640
->hp_mute
= false;
980 case SND_SOC_DAPM_PRE_PMD
:
981 rt5640
->hp_mute
= true;
992 static int rt5640_lout_event(struct snd_soc_dapm_widget
*w
,
993 struct snd_kcontrol
*kcontrol
, int event
)
995 struct snd_soc_component
*component
= snd_soc_dapm_to_component(w
->dapm
);
998 case SND_SOC_DAPM_POST_PMU
:
999 hp_amp_power_on(component
);
1000 snd_soc_component_update_bits(component
, RT5640_PWR_ANLG1
,
1001 RT5640_PWR_LM
, RT5640_PWR_LM
);
1002 snd_soc_component_update_bits(component
, RT5640_OUTPUT
,
1003 RT5640_L_MUTE
| RT5640_R_MUTE
, 0);
1006 case SND_SOC_DAPM_PRE_PMD
:
1007 snd_soc_component_update_bits(component
, RT5640_OUTPUT
,
1008 RT5640_L_MUTE
| RT5640_R_MUTE
,
1009 RT5640_L_MUTE
| RT5640_R_MUTE
);
1010 snd_soc_component_update_bits(component
, RT5640_PWR_ANLG1
,
1021 static int rt5640_hp_power_event(struct snd_soc_dapm_widget
*w
,
1022 struct snd_kcontrol
*kcontrol
, int event
)
1024 struct snd_soc_component
*component
= snd_soc_dapm_to_component(w
->dapm
);
1027 case SND_SOC_DAPM_POST_PMU
:
1028 hp_amp_power_on(component
);
1037 static int rt5640_hp_post_event(struct snd_soc_dapm_widget
*w
,
1038 struct snd_kcontrol
*kcontrol
, int event
)
1040 struct snd_soc_component
*component
= snd_soc_dapm_to_component(w
->dapm
);
1041 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
1044 case SND_SOC_DAPM_POST_PMU
:
1045 if (!rt5640
->hp_mute
)
1057 static const struct snd_soc_dapm_widget rt5640_dapm_widgets
[] = {
1059 SND_SOC_DAPM_SUPPLY_S("Stereo Filter ASRC", 1, RT5640_ASRC_1
,
1061 SND_SOC_DAPM_SUPPLY_S("I2S2 Filter ASRC", 1, RT5640_ASRC_1
,
1063 SND_SOC_DAPM_SUPPLY_S("I2S2 ASRC", 1, RT5640_ASRC_1
,
1065 SND_SOC_DAPM_SUPPLY_S("DMIC1 ASRC", 1, RT5640_ASRC_1
,
1067 SND_SOC_DAPM_SUPPLY_S("DMIC2 ASRC", 1, RT5640_ASRC_1
,
1073 SND_SOC_DAPM_SUPPLY("LDO2", RT5640_PWR_ANLG1
,
1074 RT5640_PWR_LDO2_BIT
, 0, NULL
, 0),
1075 SND_SOC_DAPM_SUPPLY("MICBIAS1", RT5640_PWR_ANLG2
,
1076 RT5640_PWR_MB1_BIT
, 0, NULL
, 0),
1078 SND_SOC_DAPM_INPUT("DMIC1"),
1079 SND_SOC_DAPM_INPUT("DMIC2"),
1080 SND_SOC_DAPM_INPUT("IN1P"),
1081 SND_SOC_DAPM_INPUT("IN1N"),
1082 SND_SOC_DAPM_INPUT("IN2P"),
1083 SND_SOC_DAPM_INPUT("IN2N"),
1084 SND_SOC_DAPM_INPUT("IN3P"),
1085 SND_SOC_DAPM_INPUT("IN3N"),
1086 SND_SOC_DAPM_PGA("DMIC L1", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1087 SND_SOC_DAPM_PGA("DMIC R1", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1088 SND_SOC_DAPM_PGA("DMIC L2", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1089 SND_SOC_DAPM_PGA("DMIC R2", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1091 SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM
, 0, 0,
1092 set_dmic_clk
, SND_SOC_DAPM_PRE_PMU
),
1093 SND_SOC_DAPM_SUPPLY("DMIC1 Power", RT5640_DMIC
, RT5640_DMIC_1_EN_SFT
, 0,
1095 SND_SOC_DAPM_SUPPLY("DMIC2 Power", RT5640_DMIC
, RT5640_DMIC_2_EN_SFT
, 0,
1098 SND_SOC_DAPM_PGA("BST1", RT5640_PWR_ANLG2
,
1099 RT5640_PWR_BST1_BIT
, 0, NULL
, 0),
1100 SND_SOC_DAPM_PGA("BST2", RT5640_PWR_ANLG2
,
1101 RT5640_PWR_BST4_BIT
, 0, NULL
, 0),
1102 SND_SOC_DAPM_PGA("BST3", RT5640_PWR_ANLG2
,
1103 RT5640_PWR_BST2_BIT
, 0, NULL
, 0),
1105 SND_SOC_DAPM_PGA("INL VOL", RT5640_PWR_VOL
,
1106 RT5640_PWR_IN_L_BIT
, 0, NULL
, 0),
1107 SND_SOC_DAPM_PGA("INR VOL", RT5640_PWR_VOL
,
1108 RT5640_PWR_IN_R_BIT
, 0, NULL
, 0),
1110 SND_SOC_DAPM_MIXER("RECMIXL", RT5640_PWR_MIXER
, RT5640_PWR_RM_L_BIT
, 0,
1111 rt5640_rec_l_mix
, ARRAY_SIZE(rt5640_rec_l_mix
)),
1112 SND_SOC_DAPM_MIXER("RECMIXR", RT5640_PWR_MIXER
, RT5640_PWR_RM_R_BIT
, 0,
1113 rt5640_rec_r_mix
, ARRAY_SIZE(rt5640_rec_r_mix
)),
1115 SND_SOC_DAPM_ADC("ADC L", NULL
, RT5640_PWR_DIG1
,
1116 RT5640_PWR_ADC_L_BIT
, 0),
1117 SND_SOC_DAPM_ADC("ADC R", NULL
, RT5640_PWR_DIG1
,
1118 RT5640_PWR_ADC_R_BIT
, 0),
1120 SND_SOC_DAPM_MUX("Stereo ADC L2 Mux", SND_SOC_NOPM
, 0, 0,
1121 &rt5640_sto_adc_2_mux
),
1122 SND_SOC_DAPM_MUX("Stereo ADC R2 Mux", SND_SOC_NOPM
, 0, 0,
1123 &rt5640_sto_adc_2_mux
),
1124 SND_SOC_DAPM_MUX("Stereo ADC L1 Mux", SND_SOC_NOPM
, 0, 0,
1125 &rt5640_sto_adc_1_mux
),
1126 SND_SOC_DAPM_MUX("Stereo ADC R1 Mux", SND_SOC_NOPM
, 0, 0,
1127 &rt5640_sto_adc_1_mux
),
1128 SND_SOC_DAPM_MUX("Mono ADC L2 Mux", SND_SOC_NOPM
, 0, 0,
1129 &rt5640_mono_adc_l2_mux
),
1130 SND_SOC_DAPM_MUX("Mono ADC L1 Mux", SND_SOC_NOPM
, 0, 0,
1131 &rt5640_mono_adc_l1_mux
),
1132 SND_SOC_DAPM_MUX("Mono ADC R1 Mux", SND_SOC_NOPM
, 0, 0,
1133 &rt5640_mono_adc_r1_mux
),
1134 SND_SOC_DAPM_MUX("Mono ADC R2 Mux", SND_SOC_NOPM
, 0, 0,
1135 &rt5640_mono_adc_r2_mux
),
1137 SND_SOC_DAPM_SUPPLY("Stereo Filter", RT5640_PWR_DIG2
,
1138 RT5640_PWR_ADC_SF_BIT
, 0, NULL
, 0),
1139 SND_SOC_DAPM_MIXER("Stereo ADC MIXL", SND_SOC_NOPM
, 0, 0,
1140 rt5640_sto_adc_l_mix
, ARRAY_SIZE(rt5640_sto_adc_l_mix
)),
1141 SND_SOC_DAPM_MIXER("Stereo ADC MIXR", SND_SOC_NOPM
, 0, 0,
1142 rt5640_sto_adc_r_mix
, ARRAY_SIZE(rt5640_sto_adc_r_mix
)),
1143 SND_SOC_DAPM_SUPPLY("Mono Left Filter", RT5640_PWR_DIG2
,
1144 RT5640_PWR_ADC_MF_L_BIT
, 0, NULL
, 0),
1145 SND_SOC_DAPM_MIXER("Mono ADC MIXL", SND_SOC_NOPM
, 0, 0,
1146 rt5640_mono_adc_l_mix
, ARRAY_SIZE(rt5640_mono_adc_l_mix
)),
1147 SND_SOC_DAPM_SUPPLY("Mono Right Filter", RT5640_PWR_DIG2
,
1148 RT5640_PWR_ADC_MF_R_BIT
, 0, NULL
, 0),
1149 SND_SOC_DAPM_MIXER("Mono ADC MIXR", SND_SOC_NOPM
, 0, 0,
1150 rt5640_mono_adc_r_mix
, ARRAY_SIZE(rt5640_mono_adc_r_mix
)),
1152 /* Digital Interface */
1153 SND_SOC_DAPM_SUPPLY("I2S1", RT5640_PWR_DIG1
,
1154 RT5640_PWR_I2S1_BIT
, 0, NULL
, 0),
1155 SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1156 SND_SOC_DAPM_PGA("IF1 DAC L", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1157 SND_SOC_DAPM_PGA("IF1 DAC R", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1158 SND_SOC_DAPM_PGA("IF1 ADC", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1159 SND_SOC_DAPM_PGA("IF1 ADC L", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1160 SND_SOC_DAPM_PGA("IF1 ADC R", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1161 SND_SOC_DAPM_SUPPLY("I2S2", RT5640_PWR_DIG1
,
1162 RT5640_PWR_I2S2_BIT
, 0, NULL
, 0),
1163 SND_SOC_DAPM_PGA("IF2 DAC", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1164 SND_SOC_DAPM_PGA("IF2 DAC L", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1165 SND_SOC_DAPM_PGA("IF2 DAC R", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1166 SND_SOC_DAPM_PGA("IF2 ADC", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1167 SND_SOC_DAPM_PGA("IF2 ADC L", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1168 SND_SOC_DAPM_PGA("IF2 ADC R", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1169 /* Digital Interface Select */
1170 SND_SOC_DAPM_MUX("DAI1 RX Mux", SND_SOC_NOPM
, 0, 0, &rt5640_dai_mux
),
1171 SND_SOC_DAPM_MUX("DAI1 TX Mux", SND_SOC_NOPM
, 0, 0, &rt5640_dai_mux
),
1172 SND_SOC_DAPM_MUX("DAI1 IF1 Mux", SND_SOC_NOPM
, 0, 0, &rt5640_dai_mux
),
1173 SND_SOC_DAPM_MUX("DAI1 IF2 Mux", SND_SOC_NOPM
, 0, 0, &rt5640_dai_mux
),
1174 SND_SOC_DAPM_MUX("SDI1 TX Mux", SND_SOC_NOPM
, 0, 0, &rt5640_sdi_mux
),
1175 SND_SOC_DAPM_MUX("DAI2 RX Mux", SND_SOC_NOPM
, 0, 0, &rt5640_dai_mux
),
1176 SND_SOC_DAPM_MUX("DAI2 TX Mux", SND_SOC_NOPM
, 0, 0, &rt5640_dai_mux
),
1177 SND_SOC_DAPM_MUX("DAI2 IF1 Mux", SND_SOC_NOPM
, 0, 0, &rt5640_dai_mux
),
1178 SND_SOC_DAPM_MUX("DAI2 IF2 Mux", SND_SOC_NOPM
, 0, 0, &rt5640_dai_mux
),
1179 SND_SOC_DAPM_MUX("SDI2 TX Mux", SND_SOC_NOPM
, 0, 0, &rt5640_sdi_mux
),
1180 /* Audio Interface */
1181 SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM
, 0, 0),
1182 SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM
, 0, 0),
1183 SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM
, 0, 0),
1184 SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, SND_SOC_NOPM
, 0, 0),
1187 /* DAC mixer before sound effect */
1188 SND_SOC_DAPM_MIXER("DAC MIXL", SND_SOC_NOPM
, 0, 0,
1189 rt5640_dac_l_mix
, ARRAY_SIZE(rt5640_dac_l_mix
)),
1190 SND_SOC_DAPM_MIXER("DAC MIXR", SND_SOC_NOPM
, 0, 0,
1191 rt5640_dac_r_mix
, ARRAY_SIZE(rt5640_dac_r_mix
)),
1194 SND_SOC_DAPM_MIXER("Mono DAC MIXL", SND_SOC_NOPM
, 0, 0,
1195 rt5640_mono_dac_l_mix
, ARRAY_SIZE(rt5640_mono_dac_l_mix
)),
1196 SND_SOC_DAPM_MIXER("Mono DAC MIXR", SND_SOC_NOPM
, 0, 0,
1197 rt5640_mono_dac_r_mix
, ARRAY_SIZE(rt5640_mono_dac_r_mix
)),
1198 SND_SOC_DAPM_MIXER("DIG MIXL", SND_SOC_NOPM
, 0, 0,
1199 rt5640_dig_l_mix
, ARRAY_SIZE(rt5640_dig_l_mix
)),
1200 SND_SOC_DAPM_MIXER("DIG MIXR", SND_SOC_NOPM
, 0, 0,
1201 rt5640_dig_r_mix
, ARRAY_SIZE(rt5640_dig_r_mix
)),
1203 SND_SOC_DAPM_DAC("DAC L1", NULL
, SND_SOC_NOPM
,
1205 SND_SOC_DAPM_DAC("DAC R1", NULL
, SND_SOC_NOPM
,
1207 SND_SOC_DAPM_SUPPLY("DAC L1 Power", RT5640_PWR_DIG1
,
1208 RT5640_PWR_DAC_L1_BIT
, 0, NULL
, 0),
1209 SND_SOC_DAPM_SUPPLY("DAC R1 Power", RT5640_PWR_DIG1
,
1210 RT5640_PWR_DAC_R1_BIT
, 0, NULL
, 0),
1211 SND_SOC_DAPM_SUPPLY("DAC L2 Power", RT5640_PWR_DIG1
,
1212 RT5640_PWR_DAC_L2_BIT
, 0, NULL
, 0),
1213 SND_SOC_DAPM_SUPPLY("DAC R2 Power", RT5640_PWR_DIG1
,
1214 RT5640_PWR_DAC_R2_BIT
, 0, NULL
, 0),
1216 SND_SOC_DAPM_MIXER("SPK MIXL", RT5640_PWR_MIXER
, RT5640_PWR_SM_L_BIT
,
1217 0, rt5640_spk_l_mix
, ARRAY_SIZE(rt5640_spk_l_mix
)),
1218 SND_SOC_DAPM_MIXER("SPK MIXR", RT5640_PWR_MIXER
, RT5640_PWR_SM_R_BIT
,
1219 0, rt5640_spk_r_mix
, ARRAY_SIZE(rt5640_spk_r_mix
)),
1221 SND_SOC_DAPM_PGA("SPKVOL L", RT5640_PWR_VOL
,
1222 RT5640_PWR_SV_L_BIT
, 0, NULL
, 0),
1223 SND_SOC_DAPM_PGA("SPKVOL R", RT5640_PWR_VOL
,
1224 RT5640_PWR_SV_R_BIT
, 0, NULL
, 0),
1225 SND_SOC_DAPM_PGA("OUTVOL L", RT5640_PWR_VOL
,
1226 RT5640_PWR_OV_L_BIT
, 0, NULL
, 0),
1227 SND_SOC_DAPM_PGA("OUTVOL R", RT5640_PWR_VOL
,
1228 RT5640_PWR_OV_R_BIT
, 0, NULL
, 0),
1229 SND_SOC_DAPM_PGA("HPOVOL L", RT5640_PWR_VOL
,
1230 RT5640_PWR_HV_L_BIT
, 0, NULL
, 0),
1231 SND_SOC_DAPM_PGA("HPOVOL R", RT5640_PWR_VOL
,
1232 RT5640_PWR_HV_R_BIT
, 0, NULL
, 0),
1233 /* SPO/HPO/LOUT/Mono Mixer */
1234 SND_SOC_DAPM_MIXER("SPOL MIX", SND_SOC_NOPM
, 0,
1235 0, rt5640_spo_l_mix
, ARRAY_SIZE(rt5640_spo_l_mix
)),
1236 SND_SOC_DAPM_MIXER("SPOR MIX", SND_SOC_NOPM
, 0,
1237 0, rt5640_spo_r_mix
, ARRAY_SIZE(rt5640_spo_r_mix
)),
1238 SND_SOC_DAPM_MIXER("LOUT MIX", SND_SOC_NOPM
, 0, 0,
1239 rt5640_lout_mix
, ARRAY_SIZE(rt5640_lout_mix
)),
1240 SND_SOC_DAPM_SUPPLY_S("Improve HP Amp Drv", 1, SND_SOC_NOPM
,
1241 0, 0, rt5640_hp_power_event
, SND_SOC_DAPM_POST_PMU
),
1242 SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM
, 0, 0,
1244 SND_SOC_DAPM_PRE_PMD
| SND_SOC_DAPM_POST_PMU
),
1245 SND_SOC_DAPM_PGA_S("LOUT amp", 1, SND_SOC_NOPM
, 0, 0,
1247 SND_SOC_DAPM_PRE_PMD
| SND_SOC_DAPM_POST_PMU
),
1248 SND_SOC_DAPM_SUPPLY("HP L Amp", RT5640_PWR_ANLG1
,
1249 RT5640_PWR_HP_L_BIT
, 0, NULL
, 0),
1250 SND_SOC_DAPM_SUPPLY("HP R Amp", RT5640_PWR_ANLG1
,
1251 RT5640_PWR_HP_R_BIT
, 0, NULL
, 0),
1252 SND_SOC_DAPM_SUPPLY("Improve SPK Amp Drv", RT5640_PWR_DIG1
,
1253 RT5640_PWR_CLS_D_BIT
, 0, NULL
, 0),
1256 SND_SOC_DAPM_SWITCH("Speaker L Playback", SND_SOC_NOPM
, 0, 0,
1257 &spk_l_enable_control
),
1258 SND_SOC_DAPM_SWITCH("Speaker R Playback", SND_SOC_NOPM
, 0, 0,
1259 &spk_r_enable_control
),
1260 SND_SOC_DAPM_SWITCH("HP L Playback", SND_SOC_NOPM
, 0, 0,
1261 &hp_l_enable_control
),
1262 SND_SOC_DAPM_SWITCH("HP R Playback", SND_SOC_NOPM
, 0, 0,
1263 &hp_r_enable_control
),
1264 SND_SOC_DAPM_POST("HP Post", rt5640_hp_post_event
),
1266 SND_SOC_DAPM_OUTPUT("SPOLP"),
1267 SND_SOC_DAPM_OUTPUT("SPOLN"),
1268 SND_SOC_DAPM_OUTPUT("SPORP"),
1269 SND_SOC_DAPM_OUTPUT("SPORN"),
1270 SND_SOC_DAPM_OUTPUT("HPOL"),
1271 SND_SOC_DAPM_OUTPUT("HPOR"),
1272 SND_SOC_DAPM_OUTPUT("LOUTL"),
1273 SND_SOC_DAPM_OUTPUT("LOUTR"),
1276 static const struct snd_soc_dapm_widget rt5640_specific_dapm_widgets
[] = {
1278 SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1280 SND_SOC_DAPM_PGA("ANC", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1282 /* DAC2 channel Mux */
1283 SND_SOC_DAPM_MUX("DAC L2 Mux", SND_SOC_NOPM
, 0, 0, &rt5640_dac_l2_mux
),
1284 SND_SOC_DAPM_MUX("DAC R2 Mux", SND_SOC_NOPM
, 0, 0, &rt5640_dac_r2_mux
),
1286 SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM
, 0, 0,
1287 rt5640_sto_dac_l_mix
, ARRAY_SIZE(rt5640_sto_dac_l_mix
)),
1288 SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM
, 0, 0,
1289 rt5640_sto_dac_r_mix
, ARRAY_SIZE(rt5640_sto_dac_r_mix
)),
1291 SND_SOC_DAPM_DAC("DAC R2", NULL
, SND_SOC_NOPM
, 0,
1293 SND_SOC_DAPM_DAC("DAC L2", NULL
, SND_SOC_NOPM
, 0,
1296 SND_SOC_DAPM_MIXER("OUT MIXL", RT5640_PWR_MIXER
, RT5640_PWR_OM_L_BIT
,
1297 0, rt5640_out_l_mix
, ARRAY_SIZE(rt5640_out_l_mix
)),
1298 SND_SOC_DAPM_MIXER("OUT MIXR", RT5640_PWR_MIXER
, RT5640_PWR_OM_R_BIT
,
1299 0, rt5640_out_r_mix
, ARRAY_SIZE(rt5640_out_r_mix
)),
1301 SND_SOC_DAPM_MIXER("HPO MIX L", SND_SOC_NOPM
, 0, 0,
1302 rt5640_hpo_mix
, ARRAY_SIZE(rt5640_hpo_mix
)),
1303 SND_SOC_DAPM_MIXER("HPO MIX R", SND_SOC_NOPM
, 0, 0,
1304 rt5640_hpo_mix
, ARRAY_SIZE(rt5640_hpo_mix
)),
1306 SND_SOC_DAPM_MIXER("Mono MIX", RT5640_PWR_ANLG1
, RT5640_PWR_MM_BIT
, 0,
1307 rt5640_mono_mix
, ARRAY_SIZE(rt5640_mono_mix
)),
1308 SND_SOC_DAPM_SUPPLY("Improve MONO Amp Drv", RT5640_PWR_ANLG1
,
1309 RT5640_PWR_MA_BIT
, 0, NULL
, 0),
1311 SND_SOC_DAPM_OUTPUT("MONOP"),
1312 SND_SOC_DAPM_OUTPUT("MONON"),
1315 static const struct snd_soc_dapm_widget rt5639_specific_dapm_widgets
[] = {
1316 SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM
, 0, 0,
1317 rt5639_sto_dac_l_mix
, ARRAY_SIZE(rt5639_sto_dac_l_mix
)),
1318 SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM
, 0, 0,
1319 rt5639_sto_dac_r_mix
, ARRAY_SIZE(rt5639_sto_dac_r_mix
)),
1321 SND_SOC_DAPM_MIXER("OUT MIXL", RT5640_PWR_MIXER
, RT5640_PWR_OM_L_BIT
,
1322 0, rt5639_out_l_mix
, ARRAY_SIZE(rt5639_out_l_mix
)),
1323 SND_SOC_DAPM_MIXER("OUT MIXR", RT5640_PWR_MIXER
, RT5640_PWR_OM_R_BIT
,
1324 0, rt5639_out_r_mix
, ARRAY_SIZE(rt5639_out_r_mix
)),
1326 SND_SOC_DAPM_MIXER("HPO MIX L", SND_SOC_NOPM
, 0, 0,
1327 rt5639_hpo_mix
, ARRAY_SIZE(rt5639_hpo_mix
)),
1328 SND_SOC_DAPM_MIXER("HPO MIX R", SND_SOC_NOPM
, 0, 0,
1329 rt5639_hpo_mix
, ARRAY_SIZE(rt5639_hpo_mix
)),
1332 static const struct snd_soc_dapm_route rt5640_dapm_routes
[] = {
1333 { "I2S1", NULL
, "Stereo Filter ASRC", is_using_asrc
},
1334 { "I2S2", NULL
, "I2S2 ASRC", is_using_asrc
},
1335 { "I2S2", NULL
, "I2S2 Filter ASRC", is_using_asrc
},
1336 { "DMIC1", NULL
, "DMIC1 ASRC", is_using_asrc
},
1337 { "DMIC2", NULL
, "DMIC2 ASRC", is_using_asrc
},
1339 {"IN1P", NULL
, "LDO2"},
1340 {"IN2P", NULL
, "LDO2"},
1341 {"IN3P", NULL
, "LDO2"},
1343 {"DMIC L1", NULL
, "DMIC1"},
1344 {"DMIC R1", NULL
, "DMIC1"},
1345 {"DMIC L2", NULL
, "DMIC2"},
1346 {"DMIC R2", NULL
, "DMIC2"},
1348 {"BST1", NULL
, "IN1P"},
1349 {"BST1", NULL
, "IN1N"},
1350 {"BST2", NULL
, "IN2P"},
1351 {"BST2", NULL
, "IN2N"},
1352 {"BST3", NULL
, "IN3P"},
1353 {"BST3", NULL
, "IN3N"},
1355 {"INL VOL", NULL
, "IN2P"},
1356 {"INR VOL", NULL
, "IN2N"},
1358 {"RECMIXL", "HPOL Switch", "HPOL"},
1359 {"RECMIXL", "INL Switch", "INL VOL"},
1360 {"RECMIXL", "BST3 Switch", "BST3"},
1361 {"RECMIXL", "BST2 Switch", "BST2"},
1362 {"RECMIXL", "BST1 Switch", "BST1"},
1363 {"RECMIXL", "OUT MIXL Switch", "OUT MIXL"},
1365 {"RECMIXR", "HPOR Switch", "HPOR"},
1366 {"RECMIXR", "INR Switch", "INR VOL"},
1367 {"RECMIXR", "BST3 Switch", "BST3"},
1368 {"RECMIXR", "BST2 Switch", "BST2"},
1369 {"RECMIXR", "BST1 Switch", "BST1"},
1370 {"RECMIXR", "OUT MIXR Switch", "OUT MIXR"},
1372 {"ADC L", NULL
, "RECMIXL"},
1373 {"ADC R", NULL
, "RECMIXR"},
1375 {"DMIC L1", NULL
, "DMIC CLK"},
1376 {"DMIC L1", NULL
, "DMIC1 Power"},
1377 {"DMIC R1", NULL
, "DMIC CLK"},
1378 {"DMIC R1", NULL
, "DMIC1 Power"},
1379 {"DMIC L2", NULL
, "DMIC CLK"},
1380 {"DMIC L2", NULL
, "DMIC2 Power"},
1381 {"DMIC R2", NULL
, "DMIC CLK"},
1382 {"DMIC R2", NULL
, "DMIC2 Power"},
1384 {"Stereo ADC L2 Mux", "DMIC1", "DMIC L1"},
1385 {"Stereo ADC L2 Mux", "DMIC2", "DMIC L2"},
1386 {"Stereo ADC L2 Mux", "DIG MIX", "DIG MIXL"},
1387 {"Stereo ADC L1 Mux", "ADC", "ADC L"},
1388 {"Stereo ADC L1 Mux", "DIG MIX", "DIG MIXL"},
1390 {"Stereo ADC R1 Mux", "ADC", "ADC R"},
1391 {"Stereo ADC R1 Mux", "DIG MIX", "DIG MIXR"},
1392 {"Stereo ADC R2 Mux", "DMIC1", "DMIC R1"},
1393 {"Stereo ADC R2 Mux", "DMIC2", "DMIC R2"},
1394 {"Stereo ADC R2 Mux", "DIG MIX", "DIG MIXR"},
1396 {"Mono ADC L2 Mux", "DMIC L1", "DMIC L1"},
1397 {"Mono ADC L2 Mux", "DMIC L2", "DMIC L2"},
1398 {"Mono ADC L2 Mux", "Mono DAC MIXL", "Mono DAC MIXL"},
1399 {"Mono ADC L1 Mux", "Mono DAC MIXL", "Mono DAC MIXL"},
1400 {"Mono ADC L1 Mux", "ADCL", "ADC L"},
1402 {"Mono ADC R1 Mux", "Mono DAC MIXR", "Mono DAC MIXR"},
1403 {"Mono ADC R1 Mux", "ADCR", "ADC R"},
1404 {"Mono ADC R2 Mux", "DMIC R1", "DMIC R1"},
1405 {"Mono ADC R2 Mux", "DMIC R2", "DMIC R2"},
1406 {"Mono ADC R2 Mux", "Mono DAC MIXR", "Mono DAC MIXR"},
1408 {"Stereo ADC MIXL", "ADC1 Switch", "Stereo ADC L1 Mux"},
1409 {"Stereo ADC MIXL", "ADC2 Switch", "Stereo ADC L2 Mux"},
1410 {"Stereo ADC MIXL", NULL
, "Stereo Filter"},
1412 {"Stereo ADC MIXR", "ADC1 Switch", "Stereo ADC R1 Mux"},
1413 {"Stereo ADC MIXR", "ADC2 Switch", "Stereo ADC R2 Mux"},
1414 {"Stereo ADC MIXR", NULL
, "Stereo Filter"},
1416 {"Mono ADC MIXL", "ADC1 Switch", "Mono ADC L1 Mux"},
1417 {"Mono ADC MIXL", "ADC2 Switch", "Mono ADC L2 Mux"},
1418 {"Mono ADC MIXL", NULL
, "Mono Left Filter"},
1420 {"Mono ADC MIXR", "ADC1 Switch", "Mono ADC R1 Mux"},
1421 {"Mono ADC MIXR", "ADC2 Switch", "Mono ADC R2 Mux"},
1422 {"Mono ADC MIXR", NULL
, "Mono Right Filter"},
1424 {"IF2 ADC L", NULL
, "Mono ADC MIXL"},
1425 {"IF2 ADC R", NULL
, "Mono ADC MIXR"},
1426 {"IF1 ADC L", NULL
, "Stereo ADC MIXL"},
1427 {"IF1 ADC R", NULL
, "Stereo ADC MIXR"},
1429 {"IF1 ADC", NULL
, "I2S1"},
1430 {"IF1 ADC", NULL
, "IF1 ADC L"},
1431 {"IF1 ADC", NULL
, "IF1 ADC R"},
1432 {"IF2 ADC", NULL
, "I2S2"},
1433 {"IF2 ADC", NULL
, "IF2 ADC L"},
1434 {"IF2 ADC", NULL
, "IF2 ADC R"},
1436 {"DAI1 TX Mux", "1:1|2:2", "IF1 ADC"},
1437 {"DAI1 TX Mux", "1:2|2:1", "IF2 ADC"},
1438 {"DAI1 IF1 Mux", "1:1|2:1", "IF1 ADC"},
1439 {"DAI1 IF2 Mux", "1:1|2:1", "IF2 ADC"},
1440 {"SDI1 TX Mux", "IF1", "DAI1 IF1 Mux"},
1441 {"SDI1 TX Mux", "IF2", "DAI1 IF2 Mux"},
1443 {"DAI2 TX Mux", "1:2|2:1", "IF1 ADC"},
1444 {"DAI2 TX Mux", "1:1|2:2", "IF2 ADC"},
1445 {"DAI2 IF1 Mux", "1:2|2:2", "IF1 ADC"},
1446 {"DAI2 IF2 Mux", "1:2|2:2", "IF2 ADC"},
1447 {"SDI2 TX Mux", "IF1", "DAI2 IF1 Mux"},
1448 {"SDI2 TX Mux", "IF2", "DAI2 IF2 Mux"},
1450 {"AIF1TX", NULL
, "DAI1 TX Mux"},
1451 {"AIF1TX", NULL
, "SDI1 TX Mux"},
1452 {"AIF2TX", NULL
, "DAI2 TX Mux"},
1453 {"AIF2TX", NULL
, "SDI2 TX Mux"},
1455 {"DAI1 RX Mux", "1:1|2:2", "AIF1RX"},
1456 {"DAI1 RX Mux", "1:1|2:1", "AIF1RX"},
1457 {"DAI1 RX Mux", "1:2|2:1", "AIF2RX"},
1458 {"DAI1 RX Mux", "1:2|2:2", "AIF2RX"},
1460 {"DAI2 RX Mux", "1:2|2:1", "AIF1RX"},
1461 {"DAI2 RX Mux", "1:1|2:1", "AIF1RX"},
1462 {"DAI2 RX Mux", "1:1|2:2", "AIF2RX"},
1463 {"DAI2 RX Mux", "1:2|2:2", "AIF2RX"},
1465 {"IF1 DAC", NULL
, "I2S1"},
1466 {"IF1 DAC", NULL
, "DAI1 RX Mux"},
1467 {"IF2 DAC", NULL
, "I2S2"},
1468 {"IF2 DAC", NULL
, "DAI2 RX Mux"},
1470 {"IF1 DAC L", NULL
, "IF1 DAC"},
1471 {"IF1 DAC R", NULL
, "IF1 DAC"},
1472 {"IF2 DAC L", NULL
, "IF2 DAC"},
1473 {"IF2 DAC R", NULL
, "IF2 DAC"},
1475 {"DAC MIXL", "Stereo ADC Switch", "Stereo ADC MIXL"},
1476 {"DAC MIXL", "INF1 Switch", "IF1 DAC L"},
1477 {"DAC MIXL", NULL
, "DAC L1 Power"},
1478 {"DAC MIXR", "Stereo ADC Switch", "Stereo ADC MIXR"},
1479 {"DAC MIXR", "INF1 Switch", "IF1 DAC R"},
1480 {"DAC MIXR", NULL
, "DAC R1 Power"},
1482 {"Stereo DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
1483 {"Stereo DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
1485 {"Mono DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
1486 {"Mono DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
1488 {"DIG MIXL", "DAC L1 Switch", "DAC MIXL"},
1489 {"DIG MIXR", "DAC R1 Switch", "DAC MIXR"},
1491 {"DAC L1", NULL
, "Stereo DAC MIXL"},
1492 {"DAC L1", NULL
, "DAC L1 Power"},
1493 {"DAC R1", NULL
, "Stereo DAC MIXR"},
1494 {"DAC R1", NULL
, "DAC R1 Power"},
1496 {"SPK MIXL", "REC MIXL Switch", "RECMIXL"},
1497 {"SPK MIXL", "INL Switch", "INL VOL"},
1498 {"SPK MIXL", "DAC L1 Switch", "DAC L1"},
1499 {"SPK MIXL", "OUT MIXL Switch", "OUT MIXL"},
1500 {"SPK MIXR", "REC MIXR Switch", "RECMIXR"},
1501 {"SPK MIXR", "INR Switch", "INR VOL"},
1502 {"SPK MIXR", "DAC R1 Switch", "DAC R1"},
1503 {"SPK MIXR", "OUT MIXR Switch", "OUT MIXR"},
1505 {"OUT MIXL", "BST1 Switch", "BST1"},
1506 {"OUT MIXL", "INL Switch", "INL VOL"},
1507 {"OUT MIXL", "REC MIXL Switch", "RECMIXL"},
1508 {"OUT MIXL", "DAC L1 Switch", "DAC L1"},
1510 {"OUT MIXR", "BST2 Switch", "BST2"},
1511 {"OUT MIXR", "BST1 Switch", "BST1"},
1512 {"OUT MIXR", "INR Switch", "INR VOL"},
1513 {"OUT MIXR", "REC MIXR Switch", "RECMIXR"},
1514 {"OUT MIXR", "DAC R1 Switch", "DAC R1"},
1516 {"SPKVOL L", NULL
, "SPK MIXL"},
1517 {"SPKVOL R", NULL
, "SPK MIXR"},
1518 {"HPOVOL L", NULL
, "OUT MIXL"},
1519 {"HPOVOL R", NULL
, "OUT MIXR"},
1520 {"OUTVOL L", NULL
, "OUT MIXL"},
1521 {"OUTVOL R", NULL
, "OUT MIXR"},
1523 {"SPOL MIX", "DAC R1 Switch", "DAC R1"},
1524 {"SPOL MIX", "DAC L1 Switch", "DAC L1"},
1525 {"SPOL MIX", "SPKVOL R Switch", "SPKVOL R"},
1526 {"SPOL MIX", "SPKVOL L Switch", "SPKVOL L"},
1527 {"SPOL MIX", "BST1 Switch", "BST1"},
1528 {"SPOR MIX", "DAC R1 Switch", "DAC R1"},
1529 {"SPOR MIX", "SPKVOL R Switch", "SPKVOL R"},
1530 {"SPOR MIX", "BST1 Switch", "BST1"},
1532 {"HPO MIX L", "HPO MIX DAC1 Switch", "DAC L1"},
1533 {"HPO MIX L", "HPO MIX HPVOL Switch", "HPOVOL L"},
1534 {"HPO MIX L", NULL
, "HP L Amp"},
1535 {"HPO MIX R", "HPO MIX DAC1 Switch", "DAC R1"},
1536 {"HPO MIX R", "HPO MIX HPVOL Switch", "HPOVOL R"},
1537 {"HPO MIX R", NULL
, "HP R Amp"},
1539 {"LOUT MIX", "DAC L1 Switch", "DAC L1"},
1540 {"LOUT MIX", "DAC R1 Switch", "DAC R1"},
1541 {"LOUT MIX", "OUTVOL L Switch", "OUTVOL L"},
1542 {"LOUT MIX", "OUTVOL R Switch", "OUTVOL R"},
1544 {"HP Amp", NULL
, "HPO MIX L"},
1545 {"HP Amp", NULL
, "HPO MIX R"},
1547 {"Speaker L Playback", "Switch", "SPOL MIX"},
1548 {"Speaker R Playback", "Switch", "SPOR MIX"},
1549 {"SPOLP", NULL
, "Speaker L Playback"},
1550 {"SPOLN", NULL
, "Speaker L Playback"},
1551 {"SPORP", NULL
, "Speaker R Playback"},
1552 {"SPORN", NULL
, "Speaker R Playback"},
1554 {"SPOLP", NULL
, "Improve SPK Amp Drv"},
1555 {"SPOLN", NULL
, "Improve SPK Amp Drv"},
1556 {"SPORP", NULL
, "Improve SPK Amp Drv"},
1557 {"SPORN", NULL
, "Improve SPK Amp Drv"},
1559 {"HPOL", NULL
, "Improve HP Amp Drv"},
1560 {"HPOR", NULL
, "Improve HP Amp Drv"},
1562 {"HP L Playback", "Switch", "HP Amp"},
1563 {"HP R Playback", "Switch", "HP Amp"},
1564 {"HPOL", NULL
, "HP L Playback"},
1565 {"HPOR", NULL
, "HP R Playback"},
1567 {"LOUT amp", NULL
, "LOUT MIX"},
1568 {"LOUTL", NULL
, "LOUT amp"},
1569 {"LOUTR", NULL
, "LOUT amp"},
1572 static const struct snd_soc_dapm_route rt5640_specific_dapm_routes
[] = {
1573 {"ANC", NULL
, "Stereo ADC MIXL"},
1574 {"ANC", NULL
, "Stereo ADC MIXR"},
1576 {"Audio DSP", NULL
, "DAC MIXL"},
1577 {"Audio DSP", NULL
, "DAC MIXR"},
1579 {"DAC L2 Mux", "IF2", "IF2 DAC L"},
1580 {"DAC L2 Mux", "Base L/R", "Audio DSP"},
1581 {"DAC L2 Mux", NULL
, "DAC L2 Power"},
1582 {"DAC R2 Mux", "IF2", "IF2 DAC R"},
1583 {"DAC R2 Mux", NULL
, "DAC R2 Power"},
1585 {"Stereo DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1586 {"Stereo DAC MIXL", "ANC Switch", "ANC"},
1587 {"Stereo DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1588 {"Stereo DAC MIXR", "ANC Switch", "ANC"},
1590 {"Mono DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1591 {"Mono DAC MIXL", "DAC R2 Switch", "DAC R2 Mux"},
1593 {"Mono DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1594 {"Mono DAC MIXR", "DAC L2 Switch", "DAC L2 Mux"},
1596 {"DIG MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1597 {"DIG MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1599 {"DAC L2", NULL
, "Mono DAC MIXL"},
1600 {"DAC L2", NULL
, "DAC L2 Power"},
1601 {"DAC R2", NULL
, "Mono DAC MIXR"},
1602 {"DAC R2", NULL
, "DAC R2 Power"},
1604 {"SPK MIXL", "DAC L2 Switch", "DAC L2"},
1605 {"SPK MIXR", "DAC R2 Switch", "DAC R2"},
1607 {"OUT MIXL", "SPK MIXL Switch", "SPK MIXL"},
1608 {"OUT MIXR", "SPK MIXR Switch", "SPK MIXR"},
1610 {"OUT MIXL", "DAC R2 Switch", "DAC R2"},
1611 {"OUT MIXL", "DAC L2 Switch", "DAC L2"},
1613 {"OUT MIXR", "DAC L2 Switch", "DAC L2"},
1614 {"OUT MIXR", "DAC R2 Switch", "DAC R2"},
1616 {"HPO MIX L", "HPO MIX DAC2 Switch", "DAC L2"},
1617 {"HPO MIX R", "HPO MIX DAC2 Switch", "DAC R2"},
1619 {"Mono MIX", "DAC R2 Switch", "DAC R2"},
1620 {"Mono MIX", "DAC L2 Switch", "DAC L2"},
1621 {"Mono MIX", "OUTVOL R Switch", "OUTVOL R"},
1622 {"Mono MIX", "OUTVOL L Switch", "OUTVOL L"},
1623 {"Mono MIX", "BST1 Switch", "BST1"},
1625 {"MONOP", NULL
, "Mono MIX"},
1626 {"MONON", NULL
, "Mono MIX"},
1627 {"MONOP", NULL
, "Improve MONO Amp Drv"},
1630 static const struct snd_soc_dapm_route rt5639_specific_dapm_routes
[] = {
1631 {"Stereo DAC MIXL", "DAC L2 Switch", "IF2 DAC L"},
1632 {"Stereo DAC MIXR", "DAC R2 Switch", "IF2 DAC R"},
1634 {"Mono DAC MIXL", "DAC L2 Switch", "IF2 DAC L"},
1635 {"Mono DAC MIXL", "DAC R2 Switch", "IF2 DAC R"},
1637 {"Mono DAC MIXR", "DAC R2 Switch", "IF2 DAC R"},
1638 {"Mono DAC MIXR", "DAC L2 Switch", "IF2 DAC L"},
1640 {"DIG MIXL", "DAC L2 Switch", "IF2 DAC L"},
1641 {"DIG MIXR", "DAC R2 Switch", "IF2 DAC R"},
1643 {"IF2 DAC L", NULL
, "DAC L2 Power"},
1644 {"IF2 DAC R", NULL
, "DAC R2 Power"},
1647 static int get_sdp_info(struct snd_soc_component
*component
, int dai_id
)
1651 if (component
== NULL
)
1654 val
= snd_soc_component_read32(component
, RT5640_I2S1_SDP
);
1655 val
= (val
& RT5640_I2S_IF_MASK
) >> RT5640_I2S_IF_SFT
;
1661 ret
|= RT5640_U_IF1
;
1664 ret
|= RT5640_U_IF1
;
1668 ret
|= RT5640_U_IF2
;
1677 ret
|= RT5640_U_IF1
;
1680 ret
|= RT5640_U_IF1
;
1684 ret
|= RT5640_U_IF2
;
1697 static int rt5640_hw_params(struct snd_pcm_substream
*substream
,
1698 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
1700 struct snd_soc_component
*component
= dai
->component
;
1701 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
1702 unsigned int val_len
= 0, val_clk
, mask_clk
;
1703 int dai_sel
, pre_div
, bclk_ms
, frame_size
;
1705 rt5640
->lrck
[dai
->id
] = params_rate(params
);
1706 pre_div
= rl6231_get_clk_info(rt5640
->sysclk
, rt5640
->lrck
[dai
->id
]);
1708 dev_err(component
->dev
, "Unsupported clock setting %d for DAI %d\n",
1709 rt5640
->lrck
[dai
->id
], dai
->id
);
1712 frame_size
= snd_soc_params_to_frame_size(params
);
1713 if (frame_size
< 0) {
1714 dev_err(component
->dev
, "Unsupported frame size: %d\n", frame_size
);
1717 if (frame_size
> 32)
1721 rt5640
->bclk
[dai
->id
] = rt5640
->lrck
[dai
->id
] * (32 << bclk_ms
);
1723 dev_dbg(dai
->dev
, "bclk is %dHz and lrck is %dHz\n",
1724 rt5640
->bclk
[dai
->id
], rt5640
->lrck
[dai
->id
]);
1725 dev_dbg(dai
->dev
, "bclk_ms is %d and pre_div is %d for iis %d\n",
1726 bclk_ms
, pre_div
, dai
->id
);
1728 switch (params_width(params
)) {
1732 val_len
|= RT5640_I2S_DL_20
;
1735 val_len
|= RT5640_I2S_DL_24
;
1738 val_len
|= RT5640_I2S_DL_8
;
1744 dai_sel
= get_sdp_info(component
, dai
->id
);
1746 dev_err(component
->dev
, "Failed to get sdp info: %d\n", dai_sel
);
1749 if (dai_sel
& RT5640_U_IF1
) {
1750 mask_clk
= RT5640_I2S_BCLK_MS1_MASK
| RT5640_I2S_PD1_MASK
;
1751 val_clk
= bclk_ms
<< RT5640_I2S_BCLK_MS1_SFT
|
1752 pre_div
<< RT5640_I2S_PD1_SFT
;
1753 snd_soc_component_update_bits(component
, RT5640_I2S1_SDP
,
1754 RT5640_I2S_DL_MASK
, val_len
);
1755 snd_soc_component_update_bits(component
, RT5640_ADDA_CLK1
, mask_clk
, val_clk
);
1757 if (dai_sel
& RT5640_U_IF2
) {
1758 mask_clk
= RT5640_I2S_BCLK_MS2_MASK
| RT5640_I2S_PD2_MASK
;
1759 val_clk
= bclk_ms
<< RT5640_I2S_BCLK_MS2_SFT
|
1760 pre_div
<< RT5640_I2S_PD2_SFT
;
1761 snd_soc_component_update_bits(component
, RT5640_I2S2_SDP
,
1762 RT5640_I2S_DL_MASK
, val_len
);
1763 snd_soc_component_update_bits(component
, RT5640_ADDA_CLK1
, mask_clk
, val_clk
);
1769 static int rt5640_set_dai_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
1771 struct snd_soc_component
*component
= dai
->component
;
1772 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
1773 unsigned int reg_val
= 0;
1776 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1777 case SND_SOC_DAIFMT_CBM_CFM
:
1778 rt5640
->master
[dai
->id
] = 1;
1780 case SND_SOC_DAIFMT_CBS_CFS
:
1781 reg_val
|= RT5640_I2S_MS_S
;
1782 rt5640
->master
[dai
->id
] = 0;
1788 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
1789 case SND_SOC_DAIFMT_NB_NF
:
1791 case SND_SOC_DAIFMT_IB_NF
:
1792 reg_val
|= RT5640_I2S_BP_INV
;
1798 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
1799 case SND_SOC_DAIFMT_I2S
:
1801 case SND_SOC_DAIFMT_LEFT_J
:
1802 reg_val
|= RT5640_I2S_DF_LEFT
;
1804 case SND_SOC_DAIFMT_DSP_A
:
1805 reg_val
|= RT5640_I2S_DF_PCM_A
;
1807 case SND_SOC_DAIFMT_DSP_B
:
1808 reg_val
|= RT5640_I2S_DF_PCM_B
;
1814 dai_sel
= get_sdp_info(component
, dai
->id
);
1816 dev_err(component
->dev
, "Failed to get sdp info: %d\n", dai_sel
);
1819 if (dai_sel
& RT5640_U_IF1
) {
1820 snd_soc_component_update_bits(component
, RT5640_I2S1_SDP
,
1821 RT5640_I2S_MS_MASK
| RT5640_I2S_BP_MASK
|
1822 RT5640_I2S_DF_MASK
, reg_val
);
1824 if (dai_sel
& RT5640_U_IF2
) {
1825 snd_soc_component_update_bits(component
, RT5640_I2S2_SDP
,
1826 RT5640_I2S_MS_MASK
| RT5640_I2S_BP_MASK
|
1827 RT5640_I2S_DF_MASK
, reg_val
);
1833 static int rt5640_set_dai_sysclk(struct snd_soc_dai
*dai
,
1834 int clk_id
, unsigned int freq
, int dir
)
1836 struct snd_soc_component
*component
= dai
->component
;
1837 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
1838 unsigned int reg_val
= 0;
1839 unsigned int pll_bit
= 0;
1841 if (freq
== rt5640
->sysclk
&& clk_id
== rt5640
->sysclk_src
)
1845 case RT5640_SCLK_S_MCLK
:
1846 reg_val
|= RT5640_SCLK_SRC_MCLK
;
1848 case RT5640_SCLK_S_PLL1
:
1849 reg_val
|= RT5640_SCLK_SRC_PLL1
;
1850 pll_bit
|= RT5640_PWR_PLL
;
1852 case RT5640_SCLK_S_RCCLK
:
1853 reg_val
|= RT5640_SCLK_SRC_RCCLK
;
1856 dev_err(component
->dev
, "Invalid clock id (%d)\n", clk_id
);
1859 snd_soc_component_update_bits(component
, RT5640_PWR_ANLG2
,
1860 RT5640_PWR_PLL
, pll_bit
);
1861 snd_soc_component_update_bits(component
, RT5640_GLB_CLK
,
1862 RT5640_SCLK_SRC_MASK
, reg_val
);
1863 rt5640
->sysclk
= freq
;
1864 rt5640
->sysclk_src
= clk_id
;
1866 dev_dbg(dai
->dev
, "Sysclk is %dHz and clock id is %d\n", freq
, clk_id
);
1870 static int rt5640_set_dai_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
1871 unsigned int freq_in
, unsigned int freq_out
)
1873 struct snd_soc_component
*component
= dai
->component
;
1874 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
1875 struct rl6231_pll_code pll_code
;
1878 if (source
== rt5640
->pll_src
&& freq_in
== rt5640
->pll_in
&&
1879 freq_out
== rt5640
->pll_out
)
1882 if (!freq_in
|| !freq_out
) {
1883 dev_dbg(component
->dev
, "PLL disabled\n");
1886 rt5640
->pll_out
= 0;
1887 snd_soc_component_update_bits(component
, RT5640_GLB_CLK
,
1888 RT5640_SCLK_SRC_MASK
, RT5640_SCLK_SRC_MCLK
);
1893 case RT5640_PLL1_S_MCLK
:
1894 snd_soc_component_update_bits(component
, RT5640_GLB_CLK
,
1895 RT5640_PLL1_SRC_MASK
, RT5640_PLL1_SRC_MCLK
);
1897 case RT5640_PLL1_S_BCLK1
:
1898 snd_soc_component_update_bits(component
, RT5640_GLB_CLK
,
1899 RT5640_PLL1_SRC_MASK
, RT5640_PLL1_SRC_BCLK1
);
1901 case RT5640_PLL1_S_BCLK2
:
1902 snd_soc_component_update_bits(component
, RT5640_GLB_CLK
,
1903 RT5640_PLL1_SRC_MASK
, RT5640_PLL1_SRC_BCLK2
);
1906 dev_err(component
->dev
, "Unknown PLL source %d\n", source
);
1910 ret
= rl6231_pll_calc(freq_in
, freq_out
, &pll_code
);
1912 dev_err(component
->dev
, "Unsupport input clock %d\n", freq_in
);
1916 dev_dbg(component
->dev
, "bypass=%d m=%d n=%d k=%d\n",
1917 pll_code
.m_bp
, (pll_code
.m_bp
? 0 : pll_code
.m_code
),
1918 pll_code
.n_code
, pll_code
.k_code
);
1920 snd_soc_component_write(component
, RT5640_PLL_CTRL1
,
1921 pll_code
.n_code
<< RT5640_PLL_N_SFT
| pll_code
.k_code
);
1922 snd_soc_component_write(component
, RT5640_PLL_CTRL2
,
1923 (pll_code
.m_bp
? 0 : pll_code
.m_code
) << RT5640_PLL_M_SFT
|
1924 pll_code
.m_bp
<< RT5640_PLL_M_BP_SFT
);
1926 rt5640
->pll_in
= freq_in
;
1927 rt5640
->pll_out
= freq_out
;
1928 rt5640
->pll_src
= source
;
1933 static int rt5640_set_bias_level(struct snd_soc_component
*component
,
1934 enum snd_soc_bias_level level
)
1936 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
1940 case SND_SOC_BIAS_ON
:
1943 case SND_SOC_BIAS_PREPARE
:
1945 * SND_SOC_BIAS_PREPARE is called while preparing for a
1946 * transition to ON or away from ON. If current bias_level
1947 * is SND_SOC_BIAS_ON, then it is preparing for a transition
1948 * away from ON. Disable the clock in that case, otherwise
1951 if (IS_ERR(rt5640
->mclk
))
1954 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_ON
) {
1955 clk_disable_unprepare(rt5640
->mclk
);
1957 ret
= clk_prepare_enable(rt5640
->mclk
);
1963 case SND_SOC_BIAS_STANDBY
:
1964 if (SND_SOC_BIAS_OFF
== snd_soc_component_get_bias_level(component
)) {
1965 snd_soc_component_update_bits(component
, RT5640_PWR_ANLG1
,
1966 RT5640_PWR_VREF1
| RT5640_PWR_MB
|
1967 RT5640_PWR_BG
| RT5640_PWR_VREF2
,
1968 RT5640_PWR_VREF1
| RT5640_PWR_MB
|
1969 RT5640_PWR_BG
| RT5640_PWR_VREF2
);
1970 usleep_range(10000, 15000);
1971 snd_soc_component_update_bits(component
, RT5640_PWR_ANLG1
,
1972 RT5640_PWR_FV1
| RT5640_PWR_FV2
,
1973 RT5640_PWR_FV1
| RT5640_PWR_FV2
);
1974 snd_soc_component_update_bits(component
, RT5640_DUMMY1
,
1976 snd_soc_component_update_bits(component
, RT5640_MICBIAS
,
1981 case SND_SOC_BIAS_OFF
:
1982 snd_soc_component_write(component
, RT5640_DEPOP_M1
, 0x0004);
1983 snd_soc_component_write(component
, RT5640_DEPOP_M2
, 0x1100);
1984 snd_soc_component_update_bits(component
, RT5640_DUMMY1
, 0x1, 0);
1985 snd_soc_component_write(component
, RT5640_PWR_DIG1
, 0x0000);
1986 snd_soc_component_write(component
, RT5640_PWR_DIG2
, 0x0000);
1987 snd_soc_component_write(component
, RT5640_PWR_VOL
, 0x0000);
1988 snd_soc_component_write(component
, RT5640_PWR_MIXER
, 0x0000);
1989 snd_soc_component_write(component
, RT5640_PWR_ANLG1
, 0x0000);
1990 snd_soc_component_write(component
, RT5640_PWR_ANLG2
, 0x0000);
2000 int rt5640_dmic_enable(struct snd_soc_component
*component
,
2001 bool dmic1_data_pin
, bool dmic2_data_pin
)
2003 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
2005 regmap_update_bits(rt5640
->regmap
, RT5640_GPIO_CTRL1
,
2006 RT5640_GP2_PIN_MASK
, RT5640_GP2_PIN_DMIC1_SCL
);
2008 if (dmic1_data_pin
) {
2009 regmap_update_bits(rt5640
->regmap
, RT5640_DMIC
,
2010 RT5640_DMIC_1_DP_MASK
, RT5640_DMIC_1_DP_GPIO3
);
2011 regmap_update_bits(rt5640
->regmap
, RT5640_GPIO_CTRL1
,
2012 RT5640_GP3_PIN_MASK
, RT5640_GP3_PIN_DMIC1_SDA
);
2015 if (dmic2_data_pin
) {
2016 regmap_update_bits(rt5640
->regmap
, RT5640_DMIC
,
2017 RT5640_DMIC_2_DP_MASK
, RT5640_DMIC_2_DP_GPIO4
);
2018 regmap_update_bits(rt5640
->regmap
, RT5640_GPIO_CTRL1
,
2019 RT5640_GP4_PIN_MASK
, RT5640_GP4_PIN_DMIC2_SDA
);
2024 EXPORT_SYMBOL_GPL(rt5640_dmic_enable
);
2026 int rt5640_sel_asrc_clk_src(struct snd_soc_component
*component
,
2027 unsigned int filter_mask
, unsigned int clk_src
)
2029 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
2030 unsigned int asrc2_mask
= 0;
2031 unsigned int asrc2_value
= 0;
2034 case RT5640_CLK_SEL_SYS
:
2035 case RT5640_CLK_SEL_ASRC
:
2045 if (filter_mask
& RT5640_DA_STEREO_FILTER
) {
2046 asrc2_mask
|= RT5640_STO_DAC_M_MASK
;
2047 asrc2_value
= (asrc2_value
& ~RT5640_STO_DAC_M_MASK
)
2048 | (clk_src
<< RT5640_STO_DAC_M_SFT
);
2051 if (filter_mask
& RT5640_DA_MONO_L_FILTER
) {
2052 asrc2_mask
|= RT5640_MDA_L_M_MASK
;
2053 asrc2_value
= (asrc2_value
& ~RT5640_MDA_L_M_MASK
)
2054 | (clk_src
<< RT5640_MDA_L_M_SFT
);
2057 if (filter_mask
& RT5640_DA_MONO_R_FILTER
) {
2058 asrc2_mask
|= RT5640_MDA_R_M_MASK
;
2059 asrc2_value
= (asrc2_value
& ~RT5640_MDA_R_M_MASK
)
2060 | (clk_src
<< RT5640_MDA_R_M_SFT
);
2063 if (filter_mask
& RT5640_AD_STEREO_FILTER
) {
2064 asrc2_mask
|= RT5640_ADC_M_MASK
;
2065 asrc2_value
= (asrc2_value
& ~RT5640_ADC_M_MASK
)
2066 | (clk_src
<< RT5640_ADC_M_SFT
);
2069 if (filter_mask
& RT5640_AD_MONO_L_FILTER
) {
2070 asrc2_mask
|= RT5640_MAD_L_M_MASK
;
2071 asrc2_value
= (asrc2_value
& ~RT5640_MAD_L_M_MASK
)
2072 | (clk_src
<< RT5640_MAD_L_M_SFT
);
2075 if (filter_mask
& RT5640_AD_MONO_R_FILTER
) {
2076 asrc2_mask
|= RT5640_MAD_R_M_MASK
;
2077 asrc2_value
= (asrc2_value
& ~RT5640_MAD_R_M_MASK
)
2078 | (clk_src
<< RT5640_MAD_R_M_SFT
);
2081 snd_soc_component_update_bits(component
, RT5640_ASRC_2
,
2082 asrc2_mask
, asrc2_value
);
2084 if (snd_soc_component_read32(component
, RT5640_ASRC_2
)) {
2085 rt5640
->asrc_en
= true;
2086 snd_soc_component_update_bits(component
, RT5640_JD_CTRL
, 0x3, 0x3);
2088 rt5640
->asrc_en
= false;
2089 snd_soc_component_update_bits(component
, RT5640_JD_CTRL
, 0x3, 0x0);
2094 EXPORT_SYMBOL_GPL(rt5640_sel_asrc_clk_src
);
2096 static void rt5640_enable_micbias1_for_ovcd(struct snd_soc_component
*component
)
2098 struct snd_soc_dapm_context
*dapm
= snd_soc_component_get_dapm(component
);
2100 snd_soc_dapm_mutex_lock(dapm
);
2101 snd_soc_dapm_force_enable_pin_unlocked(dapm
, "LDO2");
2102 snd_soc_dapm_force_enable_pin_unlocked(dapm
, "MICBIAS1");
2103 /* OVCD is unreliable when used with RCCLK as sysclk-source */
2104 snd_soc_dapm_force_enable_pin_unlocked(dapm
, "Platform Clock");
2105 snd_soc_dapm_sync_unlocked(dapm
);
2106 snd_soc_dapm_mutex_unlock(dapm
);
2109 static void rt5640_disable_micbias1_for_ovcd(struct snd_soc_component
*component
)
2111 struct snd_soc_dapm_context
*dapm
= snd_soc_component_get_dapm(component
);
2113 snd_soc_dapm_mutex_lock(dapm
);
2114 snd_soc_dapm_disable_pin_unlocked(dapm
, "Platform Clock");
2115 snd_soc_dapm_disable_pin_unlocked(dapm
, "MICBIAS1");
2116 snd_soc_dapm_disable_pin_unlocked(dapm
, "LDO2");
2117 snd_soc_dapm_sync_unlocked(dapm
);
2118 snd_soc_dapm_mutex_unlock(dapm
);
2121 static void rt5640_enable_micbias1_ovcd_irq(struct snd_soc_component
*component
)
2123 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
2125 snd_soc_component_update_bits(component
, RT5640_IRQ_CTRL2
,
2126 RT5640_IRQ_MB1_OC_MASK
, RT5640_IRQ_MB1_OC_NOR
);
2127 rt5640
->ovcd_irq_enabled
= true;
2130 static void rt5640_disable_micbias1_ovcd_irq(struct snd_soc_component
*component
)
2132 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
2134 snd_soc_component_update_bits(component
, RT5640_IRQ_CTRL2
,
2135 RT5640_IRQ_MB1_OC_MASK
, RT5640_IRQ_MB1_OC_BP
);
2136 rt5640
->ovcd_irq_enabled
= false;
2139 static void rt5640_clear_micbias1_ovcd(struct snd_soc_component
*component
)
2141 snd_soc_component_update_bits(component
, RT5640_IRQ_CTRL2
,
2142 RT5640_MB1_OC_STATUS
, 0);
2145 static bool rt5640_micbias1_ovcd(struct snd_soc_component
*component
)
2149 val
= snd_soc_component_read32(component
, RT5640_IRQ_CTRL2
);
2150 dev_dbg(component
->dev
, "irq ctrl2 %#04x\n", val
);
2152 return (val
& RT5640_MB1_OC_STATUS
);
2155 static bool rt5640_jack_inserted(struct snd_soc_component
*component
)
2157 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
2160 val
= snd_soc_component_read32(component
, RT5640_INT_IRQ_ST
);
2161 dev_dbg(component
->dev
, "irq status %#04x\n", val
);
2163 if (rt5640
->jd_inverted
)
2164 return !(val
& RT5640_JD_STATUS
);
2166 return (val
& RT5640_JD_STATUS
);
2169 /* Jack detect and button-press timings */
2170 #define JACK_SETTLE_TIME 100 /* milli seconds */
2171 #define JACK_DETECT_COUNT 5
2172 #define JACK_DETECT_MAXCOUNT 20 /* Aprox. 2 seconds worth of tries */
2173 #define JACK_UNPLUG_TIME 80 /* milli seconds */
2174 #define BP_POLL_TIME 10 /* milli seconds */
2175 #define BP_POLL_MAXCOUNT 200 /* assume something is wrong after this */
2176 #define BP_THRESHOLD 3
2178 static void rt5640_start_button_press_work(struct snd_soc_component
*component
)
2180 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
2182 rt5640
->poll_count
= 0;
2183 rt5640
->press_count
= 0;
2184 rt5640
->release_count
= 0;
2185 rt5640
->pressed
= false;
2186 rt5640
->press_reported
= false;
2187 rt5640_clear_micbias1_ovcd(component
);
2188 schedule_delayed_work(&rt5640
->bp_work
, msecs_to_jiffies(BP_POLL_TIME
));
2191 static void rt5640_button_press_work(struct work_struct
*work
)
2193 struct rt5640_priv
*rt5640
=
2194 container_of(work
, struct rt5640_priv
, bp_work
.work
);
2195 struct snd_soc_component
*component
= rt5640
->component
;
2197 /* Check the jack was not removed underneath us */
2198 if (!rt5640_jack_inserted(component
))
2201 if (rt5640_micbias1_ovcd(component
)) {
2202 rt5640
->release_count
= 0;
2203 rt5640
->press_count
++;
2204 /* Remember till after JACK_UNPLUG_TIME wait */
2205 if (rt5640
->press_count
>= BP_THRESHOLD
)
2206 rt5640
->pressed
= true;
2207 rt5640_clear_micbias1_ovcd(component
);
2209 rt5640
->press_count
= 0;
2210 rt5640
->release_count
++;
2214 * The pins get temporarily shorted on jack unplug, so we poll for
2215 * at least JACK_UNPLUG_TIME milli-seconds before reporting a press.
2217 rt5640
->poll_count
++;
2218 if (rt5640
->poll_count
< (JACK_UNPLUG_TIME
/ BP_POLL_TIME
)) {
2219 schedule_delayed_work(&rt5640
->bp_work
,
2220 msecs_to_jiffies(BP_POLL_TIME
));
2224 if (rt5640
->pressed
&& !rt5640
->press_reported
) {
2225 dev_dbg(component
->dev
, "headset button press\n");
2226 snd_soc_jack_report(rt5640
->jack
, SND_JACK_BTN_0
,
2228 rt5640
->press_reported
= true;
2231 if (rt5640
->release_count
>= BP_THRESHOLD
) {
2232 if (rt5640
->press_reported
) {
2233 dev_dbg(component
->dev
, "headset button release\n");
2234 snd_soc_jack_report(rt5640
->jack
, 0, SND_JACK_BTN_0
);
2236 /* Re-enable OVCD IRQ to detect next press */
2237 rt5640_enable_micbias1_ovcd_irq(component
);
2238 return; /* Stop polling */
2241 schedule_delayed_work(&rt5640
->bp_work
, msecs_to_jiffies(BP_POLL_TIME
));
2244 static int rt5640_detect_headset(struct snd_soc_component
*component
)
2246 int i
, headset_count
= 0, headphone_count
= 0;
2249 * We get the insertion event before the jack is fully inserted at which
2250 * point the second ring on a TRRS connector may short the 2nd ring and
2251 * sleeve contacts, also the overcurrent detection is not entirely
2252 * reliable. So we try several times with a wait in between until we
2253 * detect the same type JACK_DETECT_COUNT times in a row.
2255 for (i
= 0; i
< JACK_DETECT_MAXCOUNT
; i
++) {
2256 /* Clear any previous over-current status flag */
2257 rt5640_clear_micbias1_ovcd(component
);
2259 msleep(JACK_SETTLE_TIME
);
2261 /* Check the jack is still connected before checking ovcd */
2262 if (!rt5640_jack_inserted(component
))
2265 if (rt5640_micbias1_ovcd(component
)) {
2267 * Over current detected, there is a short between the
2268 * 2nd ring contact and the ground, so a TRS connector
2269 * without a mic contact and thus plain headphones.
2271 dev_dbg(component
->dev
, "jack mic-gnd shorted\n");
2274 if (headphone_count
== JACK_DETECT_COUNT
)
2275 return SND_JACK_HEADPHONE
;
2277 dev_dbg(component
->dev
, "jack mic-gnd open\n");
2278 headphone_count
= 0;
2280 if (headset_count
== JACK_DETECT_COUNT
)
2281 return SND_JACK_HEADSET
;
2285 dev_err(component
->dev
, "Error detecting headset vs headphones, bad contact?, assuming headphones\n");
2286 return SND_JACK_HEADPHONE
;
2289 static void rt5640_jack_work(struct work_struct
*work
)
2291 struct rt5640_priv
*rt5640
=
2292 container_of(work
, struct rt5640_priv
, jack_work
);
2293 struct snd_soc_component
*component
= rt5640
->component
;
2296 if (!rt5640_jack_inserted(component
)) {
2297 /* Jack removed, or spurious IRQ? */
2298 if (rt5640
->jack
->status
& SND_JACK_HEADPHONE
) {
2299 if (rt5640
->jack
->status
& SND_JACK_MICROPHONE
) {
2300 cancel_delayed_work_sync(&rt5640
->bp_work
);
2301 rt5640_disable_micbias1_ovcd_irq(component
);
2302 rt5640_disable_micbias1_for_ovcd(component
);
2304 snd_soc_jack_report(rt5640
->jack
, 0,
2305 SND_JACK_HEADSET
| SND_JACK_BTN_0
);
2306 dev_dbg(component
->dev
, "jack unplugged\n");
2308 } else if (!(rt5640
->jack
->status
& SND_JACK_HEADPHONE
)) {
2310 WARN_ON(rt5640
->ovcd_irq_enabled
);
2311 rt5640_enable_micbias1_for_ovcd(component
);
2312 status
= rt5640_detect_headset(component
);
2313 if (status
== SND_JACK_HEADSET
) {
2314 /* Enable ovcd IRQ for button press detect. */
2315 rt5640_enable_micbias1_ovcd_irq(component
);
2317 /* No more need for overcurrent detect. */
2318 rt5640_disable_micbias1_for_ovcd(component
);
2320 dev_dbg(component
->dev
, "detect status %#02x\n", status
);
2321 snd_soc_jack_report(rt5640
->jack
, status
, SND_JACK_HEADSET
);
2322 } else if (rt5640
->ovcd_irq_enabled
&& rt5640_micbias1_ovcd(component
)) {
2323 dev_dbg(component
->dev
, "OVCD IRQ\n");
2326 * The ovcd IRQ keeps firing while the button is pressed, so
2327 * we disable it and start polling the button until released.
2329 * The disable will make the IRQ pin 0 again and since we get
2330 * IRQs on both edges (so as to detect both jack plugin and
2331 * unplug) this means we will immediately get another IRQ.
2332 * The ovcd_irq_enabled check above makes the 2ND IRQ a NOP.
2334 rt5640_disable_micbias1_ovcd_irq(component
);
2335 rt5640_start_button_press_work(component
);
2338 * If the jack-detect IRQ flag goes high (unplug) after our
2339 * above rt5640_jack_inserted() check and before we have
2340 * disabled the OVCD IRQ, the IRQ pin will stay high and as
2341 * we react to edges, we miss the unplug event -> recheck.
2343 queue_work(system_long_wq
, &rt5640
->jack_work
);
2347 static irqreturn_t
rt5640_irq(int irq
, void *data
)
2349 struct rt5640_priv
*rt5640
= data
;
2352 queue_work(system_long_wq
, &rt5640
->jack_work
);
2357 static void rt5640_cancel_work(void *data
)
2359 struct rt5640_priv
*rt5640
= data
;
2361 cancel_work_sync(&rt5640
->jack_work
);
2362 cancel_delayed_work_sync(&rt5640
->bp_work
);
2365 static void rt5640_enable_jack_detect(struct snd_soc_component
*component
,
2366 struct snd_soc_jack
*jack
)
2368 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
2370 /* Select JD-source */
2371 snd_soc_component_update_bits(component
, RT5640_JD_CTRL
,
2372 RT5640_JD_MASK
, rt5640
->jd_src
);
2374 /* Selecting GPIO01 as an interrupt */
2375 snd_soc_component_update_bits(component
, RT5640_GPIO_CTRL1
,
2376 RT5640_GP1_PIN_MASK
, RT5640_GP1_PIN_IRQ
);
2378 /* Set GPIO1 output */
2379 snd_soc_component_update_bits(component
, RT5640_GPIO_CTRL3
,
2380 RT5640_GP1_PF_MASK
, RT5640_GP1_PF_OUT
);
2382 /* Enabling jd2 in general control 1 */
2383 snd_soc_component_write(component
, RT5640_DUMMY1
, 0x3f41);
2385 /* Enabling jd2 in general control 2 */
2386 snd_soc_component_write(component
, RT5640_DUMMY2
, 0x4001);
2388 snd_soc_component_write(component
, RT5640_PR_BASE
+ RT5640_BIAS_CUR4
,
2389 0xa800 | rt5640
->ovcd_sf
);
2391 snd_soc_component_update_bits(component
, RT5640_MICBIAS
,
2392 RT5640_MIC1_OVTH_MASK
| RT5640_MIC1_OVCD_MASK
,
2393 rt5640
->ovcd_th
| RT5640_MIC1_OVCD_EN
);
2396 * The over-current-detect is only reliable in detecting the absence
2397 * of over-current, when the mic-contact in the jack is short-circuited,
2398 * the hardware periodically retries if it can apply the bias-current
2399 * leading to the ovcd status flip-flopping 1-0-1 with it being 0 about
2400 * 10% of the time, as we poll the ovcd status bit we might hit that
2401 * 10%, so we enable sticky mode and when checking OVCD we clear the
2402 * status, msleep() a bit and then check to get a reliable reading.
2404 snd_soc_component_update_bits(component
, RT5640_IRQ_CTRL2
,
2405 RT5640_MB1_OC_STKY_MASK
, RT5640_MB1_OC_STKY_EN
);
2408 * All IRQs get or-ed together, so we need the jack IRQ to report 0
2409 * when a jack is inserted so that the OVCD IRQ then toggles the IRQ
2410 * pin 0/1 instead of it being stuck to 1. So we invert the JD polarity
2411 * on systems where the hardware does not already do this.
2413 if (rt5640
->jd_inverted
)
2414 snd_soc_component_write(component
, RT5640_IRQ_CTRL1
,
2417 snd_soc_component_write(component
, RT5640_IRQ_CTRL1
,
2418 RT5640_IRQ_JD_NOR
| RT5640_JD_P_INV
);
2420 rt5640
->jack
= jack
;
2421 if (rt5640
->jack
->status
& SND_JACK_MICROPHONE
) {
2422 rt5640_enable_micbias1_for_ovcd(component
);
2423 rt5640_enable_micbias1_ovcd_irq(component
);
2426 enable_irq(rt5640
->irq
);
2427 /* sync initial jack state */
2428 queue_work(system_long_wq
, &rt5640
->jack_work
);
2431 static void rt5640_disable_jack_detect(struct snd_soc_component
*component
)
2433 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
2436 * soc_remove_component() force-disables jack and thus rt5640->jack
2437 * could be NULL at the time of driver's module unloading.
2442 disable_irq(rt5640
->irq
);
2443 rt5640_cancel_work(rt5640
);
2445 if (rt5640
->jack
->status
& SND_JACK_MICROPHONE
) {
2446 rt5640_disable_micbias1_ovcd_irq(component
);
2447 rt5640_disable_micbias1_for_ovcd(component
);
2448 snd_soc_jack_report(rt5640
->jack
, 0, SND_JACK_BTN_0
);
2451 rt5640
->jack
= NULL
;
2454 static int rt5640_set_jack(struct snd_soc_component
*component
,
2455 struct snd_soc_jack
*jack
, void *data
)
2458 rt5640_enable_jack_detect(component
, jack
);
2460 rt5640_disable_jack_detect(component
);
2465 static int rt5640_probe(struct snd_soc_component
*component
)
2467 struct snd_soc_dapm_context
*dapm
= snd_soc_component_get_dapm(component
);
2468 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
2469 u32 dmic1_data_pin
= 0;
2470 u32 dmic2_data_pin
= 0;
2471 bool dmic_en
= false;
2474 /* Check if MCLK provided */
2475 rt5640
->mclk
= devm_clk_get(component
->dev
, "mclk");
2476 if (PTR_ERR(rt5640
->mclk
) == -EPROBE_DEFER
)
2477 return -EPROBE_DEFER
;
2479 rt5640
->component
= component
;
2481 snd_soc_component_force_bias_level(component
, SND_SOC_BIAS_OFF
);
2483 snd_soc_component_update_bits(component
, RT5640_DUMMY1
, 0x0301, 0x0301);
2484 snd_soc_component_update_bits(component
, RT5640_MICBIAS
, 0x0030, 0x0030);
2485 snd_soc_component_update_bits(component
, RT5640_DSP_PATH2
, 0xfc00, 0x0c00);
2487 switch (snd_soc_component_read32(component
, RT5640_RESET
) & RT5640_ID_MASK
) {
2488 case RT5640_ID_5640
:
2489 case RT5640_ID_5642
:
2490 snd_soc_add_component_controls(component
,
2491 rt5640_specific_snd_controls
,
2492 ARRAY_SIZE(rt5640_specific_snd_controls
));
2493 snd_soc_dapm_new_controls(dapm
,
2494 rt5640_specific_dapm_widgets
,
2495 ARRAY_SIZE(rt5640_specific_dapm_widgets
));
2496 snd_soc_dapm_add_routes(dapm
,
2497 rt5640_specific_dapm_routes
,
2498 ARRAY_SIZE(rt5640_specific_dapm_routes
));
2500 case RT5640_ID_5639
:
2501 snd_soc_dapm_new_controls(dapm
,
2502 rt5639_specific_dapm_widgets
,
2503 ARRAY_SIZE(rt5639_specific_dapm_widgets
));
2504 snd_soc_dapm_add_routes(dapm
,
2505 rt5639_specific_dapm_routes
,
2506 ARRAY_SIZE(rt5639_specific_dapm_routes
));
2509 dev_err(component
->dev
,
2510 "The driver is for RT5639 RT5640 or RT5642 only\n");
2515 * Note on some platforms the platform code may need to add device-props
2516 * rather then relying only on properties set by the firmware.
2517 * Therefor the property parsing MUST be done here, rather then from
2518 * rt5640_i2c_probe(), so that the platform-code can attach extra
2519 * properties before calling snd_soc_register_card().
2521 if (device_property_read_bool(component
->dev
, "realtek,in1-differential"))
2522 snd_soc_component_update_bits(component
, RT5640_IN1_IN2
,
2523 RT5640_IN_DF1
, RT5640_IN_DF1
);
2525 if (device_property_read_bool(component
->dev
, "realtek,in2-differential"))
2526 snd_soc_component_update_bits(component
, RT5640_IN3_IN4
,
2527 RT5640_IN_DF2
, RT5640_IN_DF2
);
2529 if (device_property_read_bool(component
->dev
, "realtek,in3-differential"))
2530 snd_soc_component_update_bits(component
, RT5640_IN1_IN2
,
2531 RT5640_IN_DF2
, RT5640_IN_DF2
);
2533 if (device_property_read_u32(component
->dev
, "realtek,dmic1-data-pin",
2534 &val
) == 0 && val
) {
2535 dmic1_data_pin
= val
- 1;
2539 if (device_property_read_u32(component
->dev
, "realtek,dmic2-data-pin",
2540 &val
) == 0 && val
) {
2541 dmic2_data_pin
= val
- 1;
2546 rt5640_dmic_enable(component
, dmic1_data_pin
, dmic2_data_pin
);
2548 if (device_property_read_u32(component
->dev
,
2549 "realtek,jack-detect-source", &val
) == 0) {
2550 if (val
<= RT5640_JD_SRC_GPIO4
)
2551 rt5640
->jd_src
= val
<< RT5640_JD_SFT
;
2553 dev_warn(component
->dev
, "Warning: Invalid jack-detect-source value: %d, leaving jack-detect disabled\n",
2557 if (!device_property_read_bool(component
->dev
, "realtek,jack-detect-not-inverted"))
2558 rt5640
->jd_inverted
= true;
2561 * Testing on various boards has shown that good defaults for the OVCD
2562 * threshold and scale-factor are 2000µA and 0.75. For an effective
2563 * limit of 1500µA, this seems to be more reliable then 1500µA and 1.0.
2565 rt5640
->ovcd_th
= RT5640_MIC1_OVTH_2000UA
;
2566 rt5640
->ovcd_sf
= RT5640_MIC_OVCD_SF_0P75
;
2568 if (device_property_read_u32(component
->dev
,
2569 "realtek,over-current-threshold-microamp", &val
) == 0) {
2572 rt5640
->ovcd_th
= RT5640_MIC1_OVTH_600UA
;
2575 rt5640
->ovcd_th
= RT5640_MIC1_OVTH_1500UA
;
2578 rt5640
->ovcd_th
= RT5640_MIC1_OVTH_2000UA
;
2581 dev_warn(component
->dev
, "Warning: Invalid over-current-threshold-microamp value: %d, defaulting to 2000uA\n",
2586 if (device_property_read_u32(component
->dev
,
2587 "realtek,over-current-scale-factor", &val
) == 0) {
2588 if (val
<= RT5640_OVCD_SF_1P5
)
2589 rt5640
->ovcd_sf
= val
<< RT5640_MIC_OVCD_SF_SFT
;
2591 dev_warn(component
->dev
, "Warning: Invalid over-current-scale-factor value: %d, defaulting to 0.75\n",
2598 static void rt5640_remove(struct snd_soc_component
*component
)
2600 rt5640_reset(component
);
2604 static int rt5640_suspend(struct snd_soc_component
*component
)
2606 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
2608 snd_soc_component_force_bias_level(component
, SND_SOC_BIAS_OFF
);
2609 rt5640_reset(component
);
2610 regcache_cache_only(rt5640
->regmap
, true);
2611 regcache_mark_dirty(rt5640
->regmap
);
2612 if (gpio_is_valid(rt5640
->ldo1_en
))
2613 gpio_set_value_cansleep(rt5640
->ldo1_en
, 0);
2618 static int rt5640_resume(struct snd_soc_component
*component
)
2620 struct rt5640_priv
*rt5640
= snd_soc_component_get_drvdata(component
);
2622 if (gpio_is_valid(rt5640
->ldo1_en
)) {
2623 gpio_set_value_cansleep(rt5640
->ldo1_en
, 1);
2627 regcache_cache_only(rt5640
->regmap
, false);
2628 regcache_sync(rt5640
->regmap
);
2633 #define rt5640_suspend NULL
2634 #define rt5640_resume NULL
2637 #define RT5640_STEREO_RATES SNDRV_PCM_RATE_8000_96000
2638 #define RT5640_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
2639 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
2641 static const struct snd_soc_dai_ops rt5640_aif_dai_ops
= {
2642 .hw_params
= rt5640_hw_params
,
2643 .set_fmt
= rt5640_set_dai_fmt
,
2644 .set_sysclk
= rt5640_set_dai_sysclk
,
2645 .set_pll
= rt5640_set_dai_pll
,
2648 static struct snd_soc_dai_driver rt5640_dai
[] = {
2650 .name
= "rt5640-aif1",
2653 .stream_name
= "AIF1 Playback",
2656 .rates
= RT5640_STEREO_RATES
,
2657 .formats
= RT5640_FORMATS
,
2660 .stream_name
= "AIF1 Capture",
2663 .rates
= RT5640_STEREO_RATES
,
2664 .formats
= RT5640_FORMATS
,
2666 .ops
= &rt5640_aif_dai_ops
,
2669 .name
= "rt5640-aif2",
2672 .stream_name
= "AIF2 Playback",
2675 .rates
= RT5640_STEREO_RATES
,
2676 .formats
= RT5640_FORMATS
,
2679 .stream_name
= "AIF2 Capture",
2682 .rates
= RT5640_STEREO_RATES
,
2683 .formats
= RT5640_FORMATS
,
2685 .ops
= &rt5640_aif_dai_ops
,
2689 static const struct snd_soc_component_driver soc_component_dev_rt5640
= {
2690 .probe
= rt5640_probe
,
2691 .remove
= rt5640_remove
,
2692 .suspend
= rt5640_suspend
,
2693 .resume
= rt5640_resume
,
2694 .set_bias_level
= rt5640_set_bias_level
,
2695 .set_jack
= rt5640_set_jack
,
2696 .controls
= rt5640_snd_controls
,
2697 .num_controls
= ARRAY_SIZE(rt5640_snd_controls
),
2698 .dapm_widgets
= rt5640_dapm_widgets
,
2699 .num_dapm_widgets
= ARRAY_SIZE(rt5640_dapm_widgets
),
2700 .dapm_routes
= rt5640_dapm_routes
,
2701 .num_dapm_routes
= ARRAY_SIZE(rt5640_dapm_routes
),
2702 .use_pmdown_time
= 1,
2704 .non_legacy_dai_naming
= 1,
2708 static const struct regmap_config rt5640_regmap
= {
2711 .use_single_read
= true,
2712 .use_single_write
= true,
2714 .max_register
= RT5640_VENDOR_ID2
+ 1 + (ARRAY_SIZE(rt5640_ranges
) *
2716 .volatile_reg
= rt5640_volatile_register
,
2717 .readable_reg
= rt5640_readable_register
,
2719 .cache_type
= REGCACHE_RBTREE
,
2720 .reg_defaults
= rt5640_reg
,
2721 .num_reg_defaults
= ARRAY_SIZE(rt5640_reg
),
2722 .ranges
= rt5640_ranges
,
2723 .num_ranges
= ARRAY_SIZE(rt5640_ranges
),
2726 static const struct i2c_device_id rt5640_i2c_id
[] = {
2732 MODULE_DEVICE_TABLE(i2c
, rt5640_i2c_id
);
2734 #if defined(CONFIG_OF)
2735 static const struct of_device_id rt5640_of_match
[] = {
2736 { .compatible
= "realtek,rt5639", },
2737 { .compatible
= "realtek,rt5640", },
2740 MODULE_DEVICE_TABLE(of
, rt5640_of_match
);
2744 static const struct acpi_device_id rt5640_acpi_match
[] = {
2752 MODULE_DEVICE_TABLE(acpi
, rt5640_acpi_match
);
2755 static int rt5640_parse_dt(struct rt5640_priv
*rt5640
, struct device_node
*np
)
2757 rt5640
->ldo1_en
= of_get_named_gpio(np
, "realtek,ldo1-en-gpios", 0);
2759 * LDO1_EN is optional (it may be statically tied on the board).
2760 * -ENOENT means that the property doesn't exist, i.e. there is no
2761 * GPIO, so is not an error. Any other error code means the property
2762 * exists, but could not be parsed.
2764 if (!gpio_is_valid(rt5640
->ldo1_en
) &&
2765 (rt5640
->ldo1_en
!= -ENOENT
))
2766 return rt5640
->ldo1_en
;
2771 static int rt5640_i2c_probe(struct i2c_client
*i2c
,
2772 const struct i2c_device_id
*id
)
2774 struct rt5640_priv
*rt5640
;
2778 rt5640
= devm_kzalloc(&i2c
->dev
,
2779 sizeof(struct rt5640_priv
),
2783 i2c_set_clientdata(i2c
, rt5640
);
2785 if (i2c
->dev
.of_node
) {
2786 ret
= rt5640_parse_dt(rt5640
, i2c
->dev
.of_node
);
2790 rt5640
->ldo1_en
= -EINVAL
;
2792 rt5640
->regmap
= devm_regmap_init_i2c(i2c
, &rt5640_regmap
);
2793 if (IS_ERR(rt5640
->regmap
)) {
2794 ret
= PTR_ERR(rt5640
->regmap
);
2795 dev_err(&i2c
->dev
, "Failed to allocate register map: %d\n",
2800 if (gpio_is_valid(rt5640
->ldo1_en
)) {
2801 ret
= devm_gpio_request_one(&i2c
->dev
, rt5640
->ldo1_en
,
2802 GPIOF_OUT_INIT_HIGH
,
2805 dev_err(&i2c
->dev
, "Failed to request LDO1_EN %d: %d\n",
2806 rt5640
->ldo1_en
, ret
);
2812 regmap_read(rt5640
->regmap
, RT5640_VENDOR_ID2
, &val
);
2813 if (val
!= RT5640_DEVICE_ID
) {
2815 "Device with ID register %#x is not rt5640/39\n", val
);
2819 regmap_write(rt5640
->regmap
, RT5640_RESET
, 0);
2821 ret
= regmap_register_patch(rt5640
->regmap
, init_list
,
2822 ARRAY_SIZE(init_list
));
2824 dev_warn(&i2c
->dev
, "Failed to apply regmap patch: %d\n", ret
);
2826 regmap_update_bits(rt5640
->regmap
, RT5640_DUMMY1
,
2827 RT5640_MCLK_DET
, RT5640_MCLK_DET
);
2829 rt5640
->hp_mute
= true;
2830 rt5640
->irq
= i2c
->irq
;
2831 INIT_DELAYED_WORK(&rt5640
->bp_work
, rt5640_button_press_work
);
2832 INIT_WORK(&rt5640
->jack_work
, rt5640_jack_work
);
2834 /* Make sure work is stopped on probe-error / remove */
2835 ret
= devm_add_action_or_reset(&i2c
->dev
, rt5640_cancel_work
, rt5640
);
2839 ret
= devm_request_irq(&i2c
->dev
, rt5640
->irq
, rt5640_irq
,
2840 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
2841 | IRQF_ONESHOT
, "rt5640", rt5640
);
2843 /* Gets re-enabled by rt5640_set_jack() */
2844 disable_irq(rt5640
->irq
);
2846 dev_warn(&i2c
->dev
, "Failed to reguest IRQ %d: %d\n",
2848 rt5640
->irq
= -ENXIO
;
2851 return devm_snd_soc_register_component(&i2c
->dev
,
2852 &soc_component_dev_rt5640
,
2853 rt5640_dai
, ARRAY_SIZE(rt5640_dai
));
2856 static struct i2c_driver rt5640_i2c_driver
= {
2859 .acpi_match_table
= ACPI_PTR(rt5640_acpi_match
),
2860 .of_match_table
= of_match_ptr(rt5640_of_match
),
2862 .probe
= rt5640_i2c_probe
,
2863 .id_table
= rt5640_i2c_id
,
2865 module_i2c_driver(rt5640_i2c_driver
);
2867 MODULE_DESCRIPTION("ASoC RT5640/RT5639 driver");
2868 MODULE_AUTHOR("Johnny Hsu <johnnyhsu@realtek.com>");
2869 MODULE_LICENSE("GPL v2");