xref: /linux/sound/soc/codecs/rt5640.c (revision e53524cdcc02d089e757b668da031ba06ff665c3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * rt5640.c  --  RT5640/RT5639 ALSA SoC audio codec driver
4  *
5  * Copyright 2011 Realtek Semiconductor Corp.
6  * Author: Johnny Hsu <johnnyhsu@realtek.com>
7  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
8  */
9 
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/pm.h>
15 #include <linux/gpio.h>
16 #include <linux/i2c.h>
17 #include <linux/regmap.h>
18 #include <linux/of.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>
31 
32 #include "rl6231.h"
33 #include "rt5640.h"
34 
35 #define RT5640_DEVICE_ID 0x6231
36 
37 #define RT5640_PR_RANGE_BASE (0xff + 1)
38 #define RT5640_PR_SPACING 0x100
39 
40 #define RT5640_PR_BASE (RT5640_PR_RANGE_BASE + (0 * RT5640_PR_SPACING))
41 
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,
49 	  .window_len = 0x1, },
50 };
51 
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 + 0x21,	0xe0e0},
57 	{RT5640_PR_BASE + 0x23,	0x1804},
58 };
59 
60 static const struct reg_default rt5640_reg[] = {
61 	{ 0x00, 0x000e },
62 	{ 0x01, 0xc8c8 },
63 	{ 0x02, 0xc8c8 },
64 	{ 0x03, 0xc8c8 },
65 	{ 0x04, 0x8000 },
66 	{ 0x0d, 0x0000 },
67 	{ 0x0e, 0x0000 },
68 	{ 0x0f, 0x0808 },
69 	{ 0x19, 0xafaf },
70 	{ 0x1a, 0xafaf },
71 	{ 0x1b, 0x0000 },
72 	{ 0x1c, 0x2f2f },
73 	{ 0x1d, 0x2f2f },
74 	{ 0x1e, 0x0000 },
75 	{ 0x27, 0x7060 },
76 	{ 0x28, 0x7070 },
77 	{ 0x29, 0x8080 },
78 	{ 0x2a, 0x5454 },
79 	{ 0x2b, 0x5454 },
80 	{ 0x2c, 0xaa00 },
81 	{ 0x2d, 0x0000 },
82 	{ 0x2e, 0xa000 },
83 	{ 0x2f, 0x0000 },
84 	{ 0x3b, 0x0000 },
85 	{ 0x3c, 0x007f },
86 	{ 0x3d, 0x0000 },
87 	{ 0x3e, 0x007f },
88 	{ 0x45, 0xe000 },
89 	{ 0x46, 0x003e },
90 	{ 0x47, 0x003e },
91 	{ 0x48, 0xf800 },
92 	{ 0x49, 0x3800 },
93 	{ 0x4a, 0x0004 },
94 	{ 0x4c, 0xfc00 },
95 	{ 0x4d, 0x0000 },
96 	{ 0x4f, 0x01ff },
97 	{ 0x50, 0x0000 },
98 	{ 0x51, 0x0000 },
99 	{ 0x52, 0x01ff },
100 	{ 0x53, 0xf000 },
101 	{ 0x61, 0x0000 },
102 	{ 0x62, 0x0000 },
103 	{ 0x63, 0x00c0 },
104 	{ 0x64, 0x0000 },
105 	{ 0x65, 0x0000 },
106 	{ 0x66, 0x0000 },
107 	{ 0x6a, 0x0000 },
108 	{ 0x6c, 0x0000 },
109 	{ 0x70, 0x8000 },
110 	{ 0x71, 0x8000 },
111 	{ 0x72, 0x8000 },
112 	{ 0x73, 0x1114 },
113 	{ 0x74, 0x0c00 },
114 	{ 0x75, 0x1d00 },
115 	{ 0x80, 0x0000 },
116 	{ 0x81, 0x0000 },
117 	{ 0x82, 0x0000 },
118 	{ 0x83, 0x0000 },
119 	{ 0x84, 0x0000 },
120 	{ 0x85, 0x0008 },
121 	{ 0x89, 0x0000 },
122 	{ 0x8a, 0x0000 },
123 	{ 0x8b, 0x0600 },
124 	{ 0x8c, 0x0228 },
125 	{ 0x8d, 0xa000 },
126 	{ 0x8e, 0x0004 },
127 	{ 0x8f, 0x1100 },
128 	{ 0x90, 0x0646 },
129 	{ 0x91, 0x0c00 },
130 	{ 0x92, 0x0000 },
131 	{ 0x93, 0x3000 },
132 	{ 0xb0, 0x2080 },
133 	{ 0xb1, 0x0000 },
134 	{ 0xb4, 0x2206 },
135 	{ 0xb5, 0x1f00 },
136 	{ 0xb6, 0x0000 },
137 	{ 0xb8, 0x034b },
138 	{ 0xb9, 0x0066 },
139 	{ 0xba, 0x000b },
140 	{ 0xbb, 0x0000 },
141 	{ 0xbc, 0x0000 },
142 	{ 0xbd, 0x0000 },
143 	{ 0xbe, 0x0000 },
144 	{ 0xbf, 0x0000 },
145 	{ 0xc0, 0x0400 },
146 	{ 0xc2, 0x0000 },
147 	{ 0xc4, 0x0000 },
148 	{ 0xc5, 0x0000 },
149 	{ 0xc6, 0x2000 },
150 	{ 0xc8, 0x0000 },
151 	{ 0xc9, 0x0000 },
152 	{ 0xca, 0x0000 },
153 	{ 0xcb, 0x0000 },
154 	{ 0xcc, 0x0000 },
155 	{ 0xcf, 0x0013 },
156 	{ 0xd0, 0x0680 },
157 	{ 0xd1, 0x1c17 },
158 	{ 0xd2, 0x8c00 },
159 	{ 0xd3, 0xaa20 },
160 	{ 0xd6, 0x0400 },
161 	{ 0xd9, 0x0809 },
162 	{ 0xfe, 0x10ec },
163 	{ 0xff, 0x6231 },
164 };
165 
166 static int rt5640_reset(struct snd_soc_component *component)
167 {
168 	return snd_soc_component_write(component, RT5640_RESET, 0);
169 }
170 
171 static bool rt5640_volatile_register(struct device *dev, unsigned int reg)
172 {
173 	int i;
174 
175 	for (i = 0; i < ARRAY_SIZE(rt5640_ranges); i++)
176 		if ((reg >= rt5640_ranges[i].window_start &&
177 		     reg <= rt5640_ranges[i].window_start +
178 		     rt5640_ranges[i].window_len) ||
179 		    (reg >= rt5640_ranges[i].range_min &&
180 		     reg <= rt5640_ranges[i].range_max))
181 			return true;
182 
183 	switch (reg) {
184 	case RT5640_RESET:
185 	case RT5640_ASRC_5:
186 	case RT5640_EQ_CTRL1:
187 	case RT5640_DRC_AGC_1:
188 	case RT5640_ANC_CTRL1:
189 	case RT5640_IRQ_CTRL2:
190 	case RT5640_INT_IRQ_ST:
191 	case RT5640_DSP_CTRL2:
192 	case RT5640_DSP_CTRL3:
193 	case RT5640_PRIV_INDEX:
194 	case RT5640_PRIV_DATA:
195 	case RT5640_PGM_REG_ARR1:
196 	case RT5640_PGM_REG_ARR3:
197 	case RT5640_DUMMY2:
198 	case RT5640_VENDOR_ID:
199 	case RT5640_VENDOR_ID1:
200 	case RT5640_VENDOR_ID2:
201 		return true;
202 	default:
203 		return false;
204 	}
205 }
206 
207 static bool rt5640_readable_register(struct device *dev, unsigned int reg)
208 {
209 	int i;
210 
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))
217 			return true;
218 
219 	switch (reg) {
220 	case RT5640_RESET:
221 	case RT5640_SPK_VOL:
222 	case RT5640_HP_VOL:
223 	case RT5640_OUTPUT:
224 	case RT5640_MONO_OUT:
225 	case RT5640_IN1_IN2:
226 	case RT5640_IN3_IN4:
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:
266 	case RT5640_PWR_VOL:
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:
273 	case RT5640_DMIC:
274 	case RT5640_GLB_CLK:
275 	case RT5640_PLL_CTRL1:
276 	case RT5640_PLL_CTRL2:
277 	case RT5640_ASRC_1:
278 	case RT5640_ASRC_2:
279 	case RT5640_ASRC_3:
280 	case RT5640_ASRC_4:
281 	case RT5640_ASRC_5:
282 	case RT5640_HP_OVCD:
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:
290 	case RT5640_MICBIAS:
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:
297 	case RT5640_SVOL_ZC:
298 	case RT5640_ANC_CTRL1:
299 	case RT5640_ANC_CTRL2:
300 	case RT5640_ANC_CTRL3:
301 	case RT5640_JD_CTRL:
302 	case RT5640_ANC_JD:
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:
323 	case RT5640_3D_HP:
324 	case RT5640_ADJ_HPF:
325 	case RT5640_HP_CALIB_AMP_DET:
326 	case RT5640_HP_CALIB2:
327 	case RT5640_SV_ZCD1:
328 	case RT5640_SV_ZCD2:
329 	case RT5640_DUMMY1:
330 	case RT5640_DUMMY2:
331 	case RT5640_DUMMY3:
332 	case RT5640_VENDOR_ID:
333 	case RT5640_VENDOR_ID1:
334 	case RT5640_VENDOR_ID2:
335 		return true;
336 	default:
337 		return false;
338 	}
339 }
340 
341 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
342 static const DECLARE_TLV_DB_MINMAX(dac_vol_tlv, -6562, 0);
343 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
344 static const DECLARE_TLV_DB_MINMAX(adc_vol_tlv, -1762, 3000);
345 static const DECLARE_TLV_DB_SCALE(adc_bst_tlv, 0, 1200, 0);
346 
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)
356 );
357 
358 /* Interface data select */
359 static const char * const rt5640_data_select[] = {
360 	"Normal", "Swap", "left copy to right", "right copy to left"};
361 
362 static SOC_ENUM_SINGLE_DECL(rt5640_if1_dac_enum, RT5640_DIG_INF_DATA,
363 			    RT5640_IF1_DAC_SEL_SFT, rt5640_data_select);
364 
365 static SOC_ENUM_SINGLE_DECL(rt5640_if1_adc_enum, RT5640_DIG_INF_DATA,
366 			    RT5640_IF1_ADC_SEL_SFT, rt5640_data_select);
367 
368 static SOC_ENUM_SINGLE_DECL(rt5640_if2_dac_enum, RT5640_DIG_INF_DATA,
369 			    RT5640_IF2_DAC_SEL_SFT, rt5640_data_select);
370 
371 static SOC_ENUM_SINGLE_DECL(rt5640_if2_adc_enum, RT5640_DIG_INF_DATA,
372 			    RT5640_IF2_ADC_SEL_SFT, rt5640_data_select);
373 
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"};
377 
378 static SOC_ENUM_SINGLE_DECL(rt5640_clsd_spk_ratio_enum, RT5640_CLS_D_OUT,
379 			    RT5640_CLSD_RATIO_SFT, rt5640_clsd_spk_ratio);
380 
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),
392 	/* OUTPUT Control */
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),
399 
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("DAC2 Playback Volume", RT5640_DAC2_DIG_VOL,
404 			RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
405 			175, 0, dac_vol_tlv),
406 	SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5640_DAC1_DIG_VOL,
407 			RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
408 			175, 0, dac_vol_tlv),
409 	/* IN1/IN2/IN3 Control */
410 	SOC_SINGLE_TLV("IN1 Boost", RT5640_IN1_IN2,
411 		RT5640_BST_SFT1, 8, 0, bst_tlv),
412 	SOC_SINGLE_TLV("IN2 Boost", RT5640_IN3_IN4,
413 		RT5640_BST_SFT2, 8, 0, bst_tlv),
414 	SOC_SINGLE_TLV("IN3 Boost", RT5640_IN1_IN2,
415 		RT5640_BST_SFT2, 8, 0, bst_tlv),
416 
417 	/* INL/INR Volume Control */
418 	SOC_DOUBLE_TLV("IN Capture Volume", RT5640_INL_INR_VOL,
419 			RT5640_INL_VOL_SFT, RT5640_INR_VOL_SFT,
420 			31, 1, in_vol_tlv),
421 	/* ADC Digital Volume Control */
422 	SOC_DOUBLE("ADC Capture Switch", RT5640_ADC_DIG_VOL,
423 		RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
424 	SOC_DOUBLE_TLV("ADC Capture Volume", RT5640_ADC_DIG_VOL,
425 			RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
426 			127, 0, adc_vol_tlv),
427 	SOC_DOUBLE("Mono ADC Capture Switch", RT5640_DUMMY1,
428 		RT5640_M_MONO_ADC_L_SFT, RT5640_M_MONO_ADC_R_SFT, 1, 1),
429 	SOC_DOUBLE_TLV("Mono ADC Capture Volume", RT5640_ADC_DATA,
430 			RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
431 			127, 0, adc_vol_tlv),
432 	/* ADC Boost Volume Control */
433 	SOC_DOUBLE_TLV("ADC Boost Gain", RT5640_ADC_BST_VOL,
434 			RT5640_ADC_L_BST_SFT, RT5640_ADC_R_BST_SFT,
435 			3, 0, adc_bst_tlv),
436 	/* Class D speaker gain ratio */
437 	SOC_ENUM("Class D SPK Ratio Control", rt5640_clsd_spk_ratio_enum),
438 
439 	SOC_ENUM("ADC IF1 Data Switch", rt5640_if1_adc_enum),
440 	SOC_ENUM("DAC IF1 Data Switch", rt5640_if1_dac_enum),
441 	SOC_ENUM("ADC IF2 Data Switch", rt5640_if2_adc_enum),
442 	SOC_ENUM("DAC IF2 Data Switch", rt5640_if2_dac_enum),
443 };
444 
445 static const struct snd_kcontrol_new rt5640_specific_snd_controls[] = {
446 	/* MONO Output Control */
447 	SOC_SINGLE("Mono Playback Switch", RT5640_MONO_OUT, RT5640_L_MUTE_SFT,
448 		1, 1),
449 };
450 
451 /**
452  * set_dmic_clk - Set parameter of dmic.
453  *
454  * @w: DAPM widget.
455  * @kcontrol: The kcontrol of this widget.
456  * @event: Event id.
457  *
458  */
459 static int set_dmic_clk(struct snd_soc_dapm_widget *w,
460 	struct snd_kcontrol *kcontrol, int event)
461 {
462 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
463 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
464 	int idx, rate;
465 
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);
469 	if (idx < 0)
470 		dev_err(component->dev, "Failed to set DMIC clock\n");
471 	else
472 		snd_soc_component_update_bits(component, RT5640_DMIC, RT5640_DMIC_CLK_MASK,
473 					idx << RT5640_DMIC_CLK_SFT);
474 	return idx;
475 }
476 
477 static int is_using_asrc(struct snd_soc_dapm_widget *source,
478 			 struct snd_soc_dapm_widget *sink)
479 {
480 	struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
481 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
482 
483 	if (!rt5640->asrc_en)
484 		return 0;
485 
486 	return 1;
487 }
488 
489 /* Digital Mixer */
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),
495 };
496 
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),
502 };
503 
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),
509 };
510 
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),
516 };
517 
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),
523 };
524 
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),
530 };
531 
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),
539 };
540 
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),
548 };
549 
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),
555 };
556 
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),
562 };
563 
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),
571 };
572 
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),
580 };
581 
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),
587 };
588 
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),
594 };
595 
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),
610 };
611 
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),
625 };
626 
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),
639 };
640 
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),
652 };
653 
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),
669 };
670 
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),
688 };
689 
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),
699 };
700 
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),
712 };
713 
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),
725 };
726 
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),
734 };
735 
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),
743 };
744 
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),
750 };
751 
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),
761 };
762 
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),
774 };
775 
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);
779 
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);
783 
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);
787 
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);
791 
792 /* Stereo ADC source */
793 static const char * const rt5640_stereo_adc1_src[] = {
794 	"DIG MIX", "ADC"
795 };
796 
797 static SOC_ENUM_SINGLE_DECL(rt5640_stereo_adc1_enum, RT5640_STO_ADC_MIXER,
798 			    RT5640_ADC_1_SRC_SFT, rt5640_stereo_adc1_src);
799 
800 static const struct snd_kcontrol_new rt5640_sto_adc_1_mux =
801 	SOC_DAPM_ENUM("Stereo ADC1 Mux", rt5640_stereo_adc1_enum);
802 
803 static const char * const rt5640_stereo_adc2_src[] = {
804 	"DMIC1", "DMIC2", "DIG MIX"
805 };
806 
807 static SOC_ENUM_SINGLE_DECL(rt5640_stereo_adc2_enum, RT5640_STO_ADC_MIXER,
808 			    RT5640_ADC_2_SRC_SFT, rt5640_stereo_adc2_src);
809 
810 static const struct snd_kcontrol_new rt5640_sto_adc_2_mux =
811 	SOC_DAPM_ENUM("Stereo ADC2 Mux", rt5640_stereo_adc2_enum);
812 
813 /* Mono ADC source */
814 static const char * const rt5640_mono_adc_l1_src[] = {
815 	"Mono DAC MIXL", "ADCL"
816 };
817 
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);
820 
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);
823 
824 static const char * const rt5640_mono_adc_l2_src[] = {
825 	"DMIC L1", "DMIC L2", "Mono DAC MIXL"
826 };
827 
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);
830 
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);
833 
834 static const char * const rt5640_mono_adc_r1_src[] = {
835 	"Mono DAC MIXR", "ADCR"
836 };
837 
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);
840 
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);
843 
844 static const char * const rt5640_mono_adc_r2_src[] = {
845 	"DMIC R1", "DMIC R2", "Mono DAC MIXR"
846 };
847 
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);
850 
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);
853 
854 /* DAC2 channel source */
855 static const char * const rt5640_dac_l2_src[] = {
856 	"IF2", "Base L/R"
857 };
858 
859 static int rt5640_dac_l2_values[] = {
860 	0,
861 	3,
862 };
863 
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);
867 
868 static const struct snd_kcontrol_new rt5640_dac_l2_mux =
869 	SOC_DAPM_ENUM("DAC2 left channel source", rt5640_dac_l2_enum);
870 
871 static const char * const rt5640_dac_r2_src[] = {
872 	"IF2",
873 };
874 
875 static int rt5640_dac_r2_values[] = {
876 	0,
877 };
878 
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);
882 
883 static const struct snd_kcontrol_new rt5640_dac_r2_mux =
884 	SOC_DAPM_ENUM("DAC2 right channel source", rt5640_dac_r2_enum);
885 
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"
889 };
890 
891 static int rt5640_dai_iis_map_values[] = {
892 	0,
893 	5,
894 	6,
895 	7,
896 };
897 
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);
902 
903 static const struct snd_kcontrol_new rt5640_dai_mux =
904 	SOC_DAPM_ENUM("DAI select", rt5640_dai_iis_map_enum);
905 
906 /* SDI select */
907 static const char * const rt5640_sdi_sel[] = {
908 	"IF1", "IF2"
909 };
910 
911 static SOC_ENUM_SINGLE_DECL(rt5640_sdi_sel_enum, RT5640_I2S2_SDP,
912 			    RT5640_I2S2_SDI_SFT, rt5640_sdi_sel);
913 
914 static const struct snd_kcontrol_new rt5640_sdi_mux =
915 	SOC_DAPM_ENUM("SDI select", rt5640_sdi_sel_enum);
916 
917 static void hp_amp_power_on(struct snd_soc_component *component)
918 {
919 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
920 
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,
930 			   0x9f00);
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,
935 		RT5640_PWR_HA,
936 		RT5640_PWR_HA);
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);
941 }
942 
943 static void rt5640_pmu_depop(struct snd_soc_component *component)
944 {
945 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
946 
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);
952 
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));
958 
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);
966 }
967 
968 static int rt5640_hp_event(struct snd_soc_dapm_widget *w,
969 			   struct snd_kcontrol *kcontrol, int event)
970 {
971 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
972 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
973 
974 	switch (event) {
975 	case SND_SOC_DAPM_POST_PMU:
976 		rt5640_pmu_depop(component);
977 		rt5640->hp_mute = false;
978 		break;
979 
980 	case SND_SOC_DAPM_PRE_PMD:
981 		rt5640->hp_mute = true;
982 		msleep(70);
983 		break;
984 
985 	default:
986 		return 0;
987 	}
988 
989 	return 0;
990 }
991 
992 static int rt5640_lout_event(struct snd_soc_dapm_widget *w,
993 	struct snd_kcontrol *kcontrol, int event)
994 {
995 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
996 
997 	switch (event) {
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);
1004 		break;
1005 
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,
1011 			RT5640_PWR_LM, 0);
1012 		break;
1013 
1014 	default:
1015 		return 0;
1016 	}
1017 
1018 	return 0;
1019 }
1020 
1021 static int rt5640_hp_power_event(struct snd_soc_dapm_widget *w,
1022 			   struct snd_kcontrol *kcontrol, int event)
1023 {
1024 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1025 
1026 	switch (event) {
1027 	case SND_SOC_DAPM_POST_PMU:
1028 		hp_amp_power_on(component);
1029 		break;
1030 	default:
1031 		return 0;
1032 	}
1033 
1034 	return 0;
1035 }
1036 
1037 static int rt5640_hp_post_event(struct snd_soc_dapm_widget *w,
1038 			   struct snd_kcontrol *kcontrol, int event)
1039 {
1040 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1041 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
1042 
1043 	switch (event) {
1044 	case SND_SOC_DAPM_POST_PMU:
1045 		if (!rt5640->hp_mute)
1046 			msleep(80);
1047 
1048 		break;
1049 
1050 	default:
1051 		return 0;
1052 	}
1053 
1054 	return 0;
1055 }
1056 
1057 static const struct snd_soc_dapm_widget rt5640_dapm_widgets[] = {
1058 	/* ASRC */
1059 	SND_SOC_DAPM_SUPPLY_S("Stereo Filter ASRC", 1, RT5640_ASRC_1,
1060 			 15, 0, NULL, 0),
1061 	SND_SOC_DAPM_SUPPLY_S("I2S2 Filter ASRC", 1, RT5640_ASRC_1,
1062 			 12, 0, NULL, 0),
1063 	SND_SOC_DAPM_SUPPLY_S("I2S2 ASRC", 1, RT5640_ASRC_1,
1064 			 11, 0, NULL, 0),
1065 	SND_SOC_DAPM_SUPPLY_S("DMIC1 ASRC", 1, RT5640_ASRC_1,
1066 			 9, 0, NULL, 0),
1067 	SND_SOC_DAPM_SUPPLY_S("DMIC2 ASRC", 1, RT5640_ASRC_1,
1068 			 8, 0, NULL, 0),
1069 
1070 
1071 	/* Input Side */
1072 	/* micbias */
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),
1077 	/* Input Lines */
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),
1090 
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,
1094 		NULL, 0),
1095 	SND_SOC_DAPM_SUPPLY("DMIC2 Power", RT5640_DMIC, RT5640_DMIC_2_EN_SFT, 0,
1096 		NULL, 0),
1097 	/* Boost */
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),
1104 	/* Input Volume */
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),
1109 	/* REC Mixer */
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)),
1114 	/* ADCs */
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),
1119 	/* ADC Mux */
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),
1136 	/* ADC Mixer */
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)),
1151 
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),
1185 
1186 	/* Output Side */
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)),
1192 
1193 	/* DAC Mixer */
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)),
1202 	/* DACs */
1203 	SND_SOC_DAPM_DAC("DAC L1", NULL, SND_SOC_NOPM,
1204 			0, 0),
1205 	SND_SOC_DAPM_DAC("DAC R1", NULL, SND_SOC_NOPM,
1206 			0, 0),
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),
1215 	/* SPK/OUT Mixer */
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)),
1220 	/* Ouput Volume */
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,
1243 		rt5640_hp_event,
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,
1246 		rt5640_lout_event,
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),
1254 
1255 	/* Output Switch */
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),
1265 	/* Output Lines */
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"),
1274 };
1275 
1276 static const struct snd_soc_dapm_widget rt5640_specific_dapm_widgets[] = {
1277 	/* Audio DSP */
1278 	SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM, 0, 0, NULL, 0),
1279 	/* ANC */
1280 	SND_SOC_DAPM_PGA("ANC", SND_SOC_NOPM, 0, 0, NULL, 0),
1281 
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),
1285 
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)),
1290 
1291 	SND_SOC_DAPM_DAC("DAC R2", NULL, SND_SOC_NOPM, 0,
1292 		0),
1293 	SND_SOC_DAPM_DAC("DAC L2", NULL, SND_SOC_NOPM, 0,
1294 		0),
1295 
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)),
1300 
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)),
1305 
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),
1310 
1311 	SND_SOC_DAPM_OUTPUT("MONOP"),
1312 	SND_SOC_DAPM_OUTPUT("MONON"),
1313 };
1314 
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)),
1320 
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)),
1325 
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)),
1330 };
1331 
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 },
1338 
1339 	{"IN1P", NULL, "LDO2"},
1340 	{"IN2P", NULL, "LDO2"},
1341 	{"IN3P", NULL, "LDO2"},
1342 
1343 	{"DMIC L1", NULL, "DMIC1"},
1344 	{"DMIC R1", NULL, "DMIC1"},
1345 	{"DMIC L2", NULL, "DMIC2"},
1346 	{"DMIC R2", NULL, "DMIC2"},
1347 
1348 	{"BST1", NULL, "IN1P"},
1349 	{"BST1", NULL, "IN1N"},
1350 	{"BST2", NULL, "IN2P"},
1351 	{"BST2", NULL, "IN2N"},
1352 	{"BST3", NULL, "IN3P"},
1353 	{"BST3", NULL, "IN3N"},
1354 
1355 	{"INL VOL", NULL, "IN2P"},
1356 	{"INR VOL", NULL, "IN2N"},
1357 
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"},
1364 
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"},
1371 
1372 	{"ADC L", NULL, "RECMIXL"},
1373 	{"ADC R", NULL, "RECMIXR"},
1374 
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"},
1383 
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"},
1389 
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"},
1395 
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"},
1401 
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"},
1407 
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"},
1411 
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"},
1415 
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"},
1419 
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"},
1423 
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"},
1428 
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"},
1435 
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"},
1442 
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"},
1449 
1450 	{"AIF1TX", NULL, "DAI1 TX Mux"},
1451 	{"AIF1TX", NULL, "SDI1 TX Mux"},
1452 	{"AIF2TX", NULL, "DAI2 TX Mux"},
1453 	{"AIF2TX", NULL, "SDI2 TX Mux"},
1454 
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"},
1459 
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"},
1464 
1465 	{"IF1 DAC", NULL, "I2S1"},
1466 	{"IF1 DAC", NULL, "DAI1 RX Mux"},
1467 	{"IF2 DAC", NULL, "I2S2"},
1468 	{"IF2 DAC", NULL, "DAI2 RX Mux"},
1469 
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"},
1474 
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"},
1481 
1482 	{"Stereo DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
1483 	{"Stereo DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
1484 
1485 	{"Mono DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
1486 	{"Mono DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
1487 
1488 	{"DIG MIXL", "DAC L1 Switch", "DAC MIXL"},
1489 	{"DIG MIXR", "DAC R1 Switch", "DAC MIXR"},
1490 
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"},
1495 
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"},
1504 
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"},
1509 
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"},
1515 
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"},
1522 
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"},
1531 
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"},
1538 
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"},
1543 
1544 	{"HP Amp", NULL, "HPO MIX L"},
1545 	{"HP Amp", NULL, "HPO MIX R"},
1546 
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"},
1553 
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"},
1558 
1559 	{"HPOL", NULL, "Improve HP Amp Drv"},
1560 	{"HPOR", NULL, "Improve HP Amp Drv"},
1561 
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"},
1566 
1567 	{"LOUT amp", NULL, "LOUT MIX"},
1568 	{"LOUTL", NULL, "LOUT amp"},
1569 	{"LOUTR", NULL, "LOUT amp"},
1570 };
1571 
1572 static const struct snd_soc_dapm_route rt5640_specific_dapm_routes[] = {
1573 	{"ANC", NULL, "Stereo ADC MIXL"},
1574 	{"ANC", NULL, "Stereo ADC MIXR"},
1575 
1576 	{"Audio DSP", NULL, "DAC MIXL"},
1577 	{"Audio DSP", NULL, "DAC MIXR"},
1578 
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"},
1584 
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"},
1589 
1590 	{"Mono DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1591 	{"Mono DAC MIXL", "DAC R2 Switch", "DAC R2 Mux"},
1592 
1593 	{"Mono DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1594 	{"Mono DAC MIXR", "DAC L2 Switch", "DAC L2 Mux"},
1595 
1596 	{"DIG MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1597 	{"DIG MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1598 
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"},
1603 
1604 	{"SPK MIXL", "DAC L2 Switch", "DAC L2"},
1605 	{"SPK MIXR", "DAC R2 Switch", "DAC R2"},
1606 
1607 	{"OUT MIXL", "SPK MIXL Switch", "SPK MIXL"},
1608 	{"OUT MIXR", "SPK MIXR Switch", "SPK MIXR"},
1609 
1610 	{"OUT MIXL", "DAC R2 Switch", "DAC R2"},
1611 	{"OUT MIXL", "DAC L2 Switch", "DAC L2"},
1612 
1613 	{"OUT MIXR", "DAC L2 Switch", "DAC L2"},
1614 	{"OUT MIXR", "DAC R2 Switch", "DAC R2"},
1615 
1616 	{"HPO MIX L", "HPO MIX DAC2 Switch", "DAC L2"},
1617 	{"HPO MIX R", "HPO MIX DAC2 Switch", "DAC R2"},
1618 
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"},
1624 
1625 	{"MONOP", NULL, "Mono MIX"},
1626 	{"MONON", NULL, "Mono MIX"},
1627 	{"MONOP", NULL, "Improve MONO Amp Drv"},
1628 };
1629 
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"},
1633 
1634 	{"Mono DAC MIXL", "DAC L2 Switch", "IF2 DAC L"},
1635 	{"Mono DAC MIXL", "DAC R2 Switch", "IF2 DAC R"},
1636 
1637 	{"Mono DAC MIXR", "DAC R2 Switch", "IF2 DAC R"},
1638 	{"Mono DAC MIXR", "DAC L2 Switch", "IF2 DAC L"},
1639 
1640 	{"DIG MIXL", "DAC L2 Switch", "IF2 DAC L"},
1641 	{"DIG MIXR", "DAC R2 Switch", "IF2 DAC R"},
1642 
1643 	{"IF2 DAC L", NULL, "DAC L2 Power"},
1644 	{"IF2 DAC R", NULL, "DAC R2 Power"},
1645 };
1646 
1647 static int get_sdp_info(struct snd_soc_component *component, int dai_id)
1648 {
1649 	int ret = 0, val;
1650 
1651 	if (component == NULL)
1652 		return -EINVAL;
1653 
1654 	val = snd_soc_component_read(component, RT5640_I2S1_SDP);
1655 	val = (val & RT5640_I2S_IF_MASK) >> RT5640_I2S_IF_SFT;
1656 	switch (dai_id) {
1657 	case RT5640_AIF1:
1658 		switch (val) {
1659 		case RT5640_IF_123:
1660 		case RT5640_IF_132:
1661 			ret |= RT5640_U_IF1;
1662 			break;
1663 		case RT5640_IF_113:
1664 			ret |= RT5640_U_IF1;
1665 			fallthrough;
1666 		case RT5640_IF_312:
1667 		case RT5640_IF_213:
1668 			ret |= RT5640_U_IF2;
1669 			break;
1670 		}
1671 		break;
1672 
1673 	case RT5640_AIF2:
1674 		switch (val) {
1675 		case RT5640_IF_231:
1676 		case RT5640_IF_213:
1677 			ret |= RT5640_U_IF1;
1678 			break;
1679 		case RT5640_IF_223:
1680 			ret |= RT5640_U_IF1;
1681 			fallthrough;
1682 		case RT5640_IF_123:
1683 		case RT5640_IF_321:
1684 			ret |= RT5640_U_IF2;
1685 			break;
1686 		}
1687 		break;
1688 
1689 	default:
1690 		ret = -EINVAL;
1691 		break;
1692 	}
1693 
1694 	return ret;
1695 }
1696 
1697 static int rt5640_hw_params(struct snd_pcm_substream *substream,
1698 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
1699 {
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;
1704 
1705 	rt5640->lrck[dai->id] = params_rate(params);
1706 	pre_div = rl6231_get_clk_info(rt5640->sysclk, rt5640->lrck[dai->id]);
1707 	if (pre_div < 0) {
1708 		dev_err(component->dev, "Unsupported clock setting %d for DAI %d\n",
1709 			rt5640->lrck[dai->id], dai->id);
1710 		return -EINVAL;
1711 	}
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);
1715 		return frame_size;
1716 	}
1717 	if (frame_size > 32)
1718 		bclk_ms = 1;
1719 	else
1720 		bclk_ms = 0;
1721 	rt5640->bclk[dai->id] = rt5640->lrck[dai->id] * (32 << bclk_ms);
1722 
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);
1727 
1728 	switch (params_width(params)) {
1729 	case 16:
1730 		break;
1731 	case 20:
1732 		val_len |= RT5640_I2S_DL_20;
1733 		break;
1734 	case 24:
1735 		val_len |= RT5640_I2S_DL_24;
1736 		break;
1737 	case 8:
1738 		val_len |= RT5640_I2S_DL_8;
1739 		break;
1740 	default:
1741 		return -EINVAL;
1742 	}
1743 
1744 	dai_sel = get_sdp_info(component, dai->id);
1745 	if (dai_sel < 0) {
1746 		dev_err(component->dev, "Failed to get sdp info: %d\n", dai_sel);
1747 		return -EINVAL;
1748 	}
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);
1756 	}
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);
1764 	}
1765 
1766 	return 0;
1767 }
1768 
1769 static int rt5640_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1770 {
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;
1774 	int dai_sel;
1775 
1776 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1777 	case SND_SOC_DAIFMT_CBM_CFM:
1778 		rt5640->master[dai->id] = 1;
1779 		break;
1780 	case SND_SOC_DAIFMT_CBS_CFS:
1781 		reg_val |= RT5640_I2S_MS_S;
1782 		rt5640->master[dai->id] = 0;
1783 		break;
1784 	default:
1785 		return -EINVAL;
1786 	}
1787 
1788 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1789 	case SND_SOC_DAIFMT_NB_NF:
1790 		break;
1791 	case SND_SOC_DAIFMT_IB_NF:
1792 		reg_val |= RT5640_I2S_BP_INV;
1793 		break;
1794 	default:
1795 		return -EINVAL;
1796 	}
1797 
1798 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1799 	case SND_SOC_DAIFMT_I2S:
1800 		break;
1801 	case SND_SOC_DAIFMT_LEFT_J:
1802 		reg_val |= RT5640_I2S_DF_LEFT;
1803 		break;
1804 	case SND_SOC_DAIFMT_DSP_A:
1805 		reg_val |= RT5640_I2S_DF_PCM_A;
1806 		break;
1807 	case SND_SOC_DAIFMT_DSP_B:
1808 		reg_val  |= RT5640_I2S_DF_PCM_B;
1809 		break;
1810 	default:
1811 		return -EINVAL;
1812 	}
1813 
1814 	dai_sel = get_sdp_info(component, dai->id);
1815 	if (dai_sel < 0) {
1816 		dev_err(component->dev, "Failed to get sdp info: %d\n", dai_sel);
1817 		return -EINVAL;
1818 	}
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);
1823 	}
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);
1828 	}
1829 
1830 	return 0;
1831 }
1832 
1833 static int rt5640_set_dai_sysclk(struct snd_soc_dai *dai,
1834 		int clk_id, unsigned int freq, int dir)
1835 {
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;
1840 	int ret;
1841 
1842 	switch (clk_id) {
1843 	case RT5640_SCLK_S_MCLK:
1844 		ret = clk_set_rate(rt5640->mclk, freq);
1845 		if (ret)
1846 			return ret;
1847 
1848 		reg_val |= RT5640_SCLK_SRC_MCLK;
1849 		break;
1850 	case RT5640_SCLK_S_PLL1:
1851 		reg_val |= RT5640_SCLK_SRC_PLL1;
1852 		pll_bit |= RT5640_PWR_PLL;
1853 		break;
1854 	case RT5640_SCLK_S_RCCLK:
1855 		reg_val |= RT5640_SCLK_SRC_RCCLK;
1856 		break;
1857 	default:
1858 		dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
1859 		return -EINVAL;
1860 	}
1861 	snd_soc_component_update_bits(component, RT5640_PWR_ANLG2,
1862 		RT5640_PWR_PLL, pll_bit);
1863 	snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1864 		RT5640_SCLK_SRC_MASK, reg_val);
1865 	rt5640->sysclk = freq;
1866 	rt5640->sysclk_src = clk_id;
1867 
1868 	dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
1869 	return 0;
1870 }
1871 
1872 static int rt5640_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
1873 			unsigned int freq_in, unsigned int freq_out)
1874 {
1875 	struct snd_soc_component *component = dai->component;
1876 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
1877 	struct rl6231_pll_code pll_code;
1878 	int ret;
1879 
1880 	if (source == rt5640->pll_src && freq_in == rt5640->pll_in &&
1881 	    freq_out == rt5640->pll_out)
1882 		return 0;
1883 
1884 	if (!freq_in || !freq_out) {
1885 		dev_dbg(component->dev, "PLL disabled\n");
1886 
1887 		rt5640->pll_in = 0;
1888 		rt5640->pll_out = 0;
1889 		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1890 			RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_MCLK);
1891 		return 0;
1892 	}
1893 
1894 	switch (source) {
1895 	case RT5640_PLL1_S_MCLK:
1896 		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1897 			RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_MCLK);
1898 		break;
1899 	case RT5640_PLL1_S_BCLK1:
1900 		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1901 			RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK1);
1902 		break;
1903 	case RT5640_PLL1_S_BCLK2:
1904 		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1905 			RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK2);
1906 		break;
1907 	default:
1908 		dev_err(component->dev, "Unknown PLL source %d\n", source);
1909 		return -EINVAL;
1910 	}
1911 
1912 	ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
1913 	if (ret < 0) {
1914 		dev_err(component->dev, "Unsupported input clock %d\n", freq_in);
1915 		return ret;
1916 	}
1917 
1918 	dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
1919 		pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
1920 		pll_code.n_code, pll_code.k_code);
1921 
1922 	snd_soc_component_write(component, RT5640_PLL_CTRL1,
1923 		(pll_code.n_code << RT5640_PLL_N_SFT) | pll_code.k_code);
1924 	snd_soc_component_write(component, RT5640_PLL_CTRL2,
1925 		((pll_code.m_bp ? 0 : pll_code.m_code) << RT5640_PLL_M_SFT) |
1926 		(pll_code.m_bp << RT5640_PLL_M_BP_SFT));
1927 
1928 	rt5640->pll_in = freq_in;
1929 	rt5640->pll_out = freq_out;
1930 	rt5640->pll_src = source;
1931 
1932 	return 0;
1933 }
1934 
1935 static int rt5640_set_bias_level(struct snd_soc_component *component,
1936 			enum snd_soc_bias_level level)
1937 {
1938 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
1939 	int ret;
1940 
1941 	switch (level) {
1942 	case SND_SOC_BIAS_ON:
1943 		break;
1944 
1945 	case SND_SOC_BIAS_PREPARE:
1946 		/*
1947 		 * SND_SOC_BIAS_PREPARE is called while preparing for a
1948 		 * transition to ON or away from ON. If current bias_level
1949 		 * is SND_SOC_BIAS_ON, then it is preparing for a transition
1950 		 * away from ON. Disable the clock in that case, otherwise
1951 		 * enable it.
1952 		 */
1953 		if (IS_ERR(rt5640->mclk))
1954 			break;
1955 
1956 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_ON) {
1957 			clk_disable_unprepare(rt5640->mclk);
1958 		} else {
1959 			ret = clk_prepare_enable(rt5640->mclk);
1960 			if (ret)
1961 				return ret;
1962 		}
1963 		break;
1964 
1965 	case SND_SOC_BIAS_STANDBY:
1966 		if (SND_SOC_BIAS_OFF == snd_soc_component_get_bias_level(component)) {
1967 			snd_soc_component_update_bits(component, RT5640_PWR_ANLG1,
1968 				RT5640_PWR_VREF1 | RT5640_PWR_MB |
1969 				RT5640_PWR_BG | RT5640_PWR_VREF2,
1970 				RT5640_PWR_VREF1 | RT5640_PWR_MB |
1971 				RT5640_PWR_BG | RT5640_PWR_VREF2);
1972 			usleep_range(10000, 15000);
1973 			snd_soc_component_update_bits(component, RT5640_PWR_ANLG1,
1974 				RT5640_PWR_FV1 | RT5640_PWR_FV2,
1975 				RT5640_PWR_FV1 | RT5640_PWR_FV2);
1976 			snd_soc_component_update_bits(component, RT5640_DUMMY1,
1977 						0x1, 0x1);
1978 			snd_soc_component_update_bits(component, RT5640_MICBIAS,
1979 						0x0030, 0x0030);
1980 		}
1981 		break;
1982 
1983 	case SND_SOC_BIAS_OFF:
1984 		snd_soc_component_write(component, RT5640_DEPOP_M1, 0x0004);
1985 		snd_soc_component_write(component, RT5640_DEPOP_M2, 0x1100);
1986 		snd_soc_component_update_bits(component, RT5640_DUMMY1, 0x1, 0);
1987 		snd_soc_component_write(component, RT5640_PWR_DIG1, 0x0000);
1988 		snd_soc_component_write(component, RT5640_PWR_DIG2, 0x0000);
1989 		snd_soc_component_write(component, RT5640_PWR_VOL, 0x0000);
1990 		snd_soc_component_write(component, RT5640_PWR_MIXER, 0x0000);
1991 		if (rt5640->jd_src == RT5640_JD_SRC_HDA_HEADER)
1992 			snd_soc_component_write(component, RT5640_PWR_ANLG1,
1993 				0x2818);
1994 		else
1995 			snd_soc_component_write(component, RT5640_PWR_ANLG1,
1996 				0x0000);
1997 		snd_soc_component_write(component, RT5640_PWR_ANLG2, 0x0000);
1998 		break;
1999 
2000 	default:
2001 		break;
2002 	}
2003 
2004 	return 0;
2005 }
2006 
2007 int rt5640_dmic_enable(struct snd_soc_component *component,
2008 		       bool dmic1_data_pin, bool dmic2_data_pin)
2009 {
2010 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2011 
2012 	regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
2013 		RT5640_GP2_PIN_MASK, RT5640_GP2_PIN_DMIC1_SCL);
2014 
2015 	if (dmic1_data_pin) {
2016 		regmap_update_bits(rt5640->regmap, RT5640_DMIC,
2017 			RT5640_DMIC_1_DP_MASK, RT5640_DMIC_1_DP_GPIO3);
2018 		regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
2019 			RT5640_GP3_PIN_MASK, RT5640_GP3_PIN_DMIC1_SDA);
2020 	}
2021 
2022 	if (dmic2_data_pin) {
2023 		regmap_update_bits(rt5640->regmap, RT5640_DMIC,
2024 			RT5640_DMIC_2_DP_MASK, RT5640_DMIC_2_DP_GPIO4);
2025 		regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
2026 			RT5640_GP4_PIN_MASK, RT5640_GP4_PIN_DMIC2_SDA);
2027 	}
2028 
2029 	return 0;
2030 }
2031 EXPORT_SYMBOL_GPL(rt5640_dmic_enable);
2032 
2033 int rt5640_sel_asrc_clk_src(struct snd_soc_component *component,
2034 		unsigned int filter_mask, unsigned int clk_src)
2035 {
2036 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2037 	unsigned int asrc2_mask = 0;
2038 	unsigned int asrc2_value = 0;
2039 
2040 	switch (clk_src) {
2041 	case RT5640_CLK_SEL_SYS:
2042 	case RT5640_CLK_SEL_ASRC:
2043 		break;
2044 
2045 	default:
2046 		return -EINVAL;
2047 	}
2048 
2049 	if (!filter_mask)
2050 		return -EINVAL;
2051 
2052 	if (filter_mask & RT5640_DA_STEREO_FILTER) {
2053 		asrc2_mask |= RT5640_STO_DAC_M_MASK;
2054 		asrc2_value = (asrc2_value & ~RT5640_STO_DAC_M_MASK)
2055 			| (clk_src << RT5640_STO_DAC_M_SFT);
2056 	}
2057 
2058 	if (filter_mask & RT5640_DA_MONO_L_FILTER) {
2059 		asrc2_mask |= RT5640_MDA_L_M_MASK;
2060 		asrc2_value = (asrc2_value & ~RT5640_MDA_L_M_MASK)
2061 			| (clk_src << RT5640_MDA_L_M_SFT);
2062 	}
2063 
2064 	if (filter_mask & RT5640_DA_MONO_R_FILTER) {
2065 		asrc2_mask |= RT5640_MDA_R_M_MASK;
2066 		asrc2_value = (asrc2_value & ~RT5640_MDA_R_M_MASK)
2067 			| (clk_src << RT5640_MDA_R_M_SFT);
2068 	}
2069 
2070 	if (filter_mask & RT5640_AD_STEREO_FILTER) {
2071 		asrc2_mask |= RT5640_ADC_M_MASK;
2072 		asrc2_value = (asrc2_value & ~RT5640_ADC_M_MASK)
2073 			| (clk_src << RT5640_ADC_M_SFT);
2074 	}
2075 
2076 	if (filter_mask & RT5640_AD_MONO_L_FILTER) {
2077 		asrc2_mask |= RT5640_MAD_L_M_MASK;
2078 		asrc2_value = (asrc2_value & ~RT5640_MAD_L_M_MASK)
2079 			| (clk_src << RT5640_MAD_L_M_SFT);
2080 	}
2081 
2082 	if (filter_mask & RT5640_AD_MONO_R_FILTER)  {
2083 		asrc2_mask |= RT5640_MAD_R_M_MASK;
2084 		asrc2_value = (asrc2_value & ~RT5640_MAD_R_M_MASK)
2085 			| (clk_src << RT5640_MAD_R_M_SFT);
2086 	}
2087 
2088 	snd_soc_component_update_bits(component, RT5640_ASRC_2,
2089 		asrc2_mask, asrc2_value);
2090 
2091 	if (snd_soc_component_read(component, RT5640_ASRC_2)) {
2092 		rt5640->asrc_en = true;
2093 		snd_soc_component_update_bits(component, RT5640_JD_CTRL, 0x3, 0x3);
2094 	} else {
2095 		rt5640->asrc_en = false;
2096 		snd_soc_component_update_bits(component, RT5640_JD_CTRL, 0x3, 0x0);
2097 	}
2098 
2099 	return 0;
2100 }
2101 EXPORT_SYMBOL_GPL(rt5640_sel_asrc_clk_src);
2102 
2103 void rt5640_enable_micbias1_for_ovcd(struct snd_soc_component *component)
2104 {
2105 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
2106 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2107 
2108 	snd_soc_dapm_mutex_lock(dapm);
2109 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "LDO2");
2110 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS1");
2111 	/* OVCD is unreliable when used with RCCLK as sysclk-source */
2112 	if (rt5640->use_platform_clock)
2113 		snd_soc_dapm_force_enable_pin_unlocked(dapm, "Platform Clock");
2114 	snd_soc_dapm_sync_unlocked(dapm);
2115 	snd_soc_dapm_mutex_unlock(dapm);
2116 }
2117 EXPORT_SYMBOL_GPL(rt5640_enable_micbias1_for_ovcd);
2118 
2119 void rt5640_disable_micbias1_for_ovcd(struct snd_soc_component *component)
2120 {
2121 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
2122 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2123 
2124 	snd_soc_dapm_mutex_lock(dapm);
2125 	if (rt5640->use_platform_clock)
2126 		snd_soc_dapm_disable_pin_unlocked(dapm, "Platform Clock");
2127 	snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS1");
2128 	snd_soc_dapm_disable_pin_unlocked(dapm, "LDO2");
2129 	snd_soc_dapm_sync_unlocked(dapm);
2130 	snd_soc_dapm_mutex_unlock(dapm);
2131 }
2132 EXPORT_SYMBOL_GPL(rt5640_disable_micbias1_for_ovcd);
2133 
2134 static void rt5640_enable_micbias1_ovcd_irq(struct snd_soc_component *component)
2135 {
2136 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2137 
2138 	snd_soc_component_update_bits(component, RT5640_IRQ_CTRL2,
2139 		RT5640_IRQ_MB1_OC_MASK, RT5640_IRQ_MB1_OC_NOR);
2140 	rt5640->ovcd_irq_enabled = true;
2141 }
2142 
2143 static void rt5640_disable_micbias1_ovcd_irq(struct snd_soc_component *component)
2144 {
2145 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2146 
2147 	snd_soc_component_update_bits(component, RT5640_IRQ_CTRL2,
2148 		RT5640_IRQ_MB1_OC_MASK, RT5640_IRQ_MB1_OC_BP);
2149 	rt5640->ovcd_irq_enabled = false;
2150 }
2151 
2152 static void rt5640_clear_micbias1_ovcd(struct snd_soc_component *component)
2153 {
2154 	snd_soc_component_update_bits(component, RT5640_IRQ_CTRL2,
2155 		RT5640_MB1_OC_STATUS, 0);
2156 }
2157 
2158 static bool rt5640_micbias1_ovcd(struct snd_soc_component *component)
2159 {
2160 	int val;
2161 
2162 	val = snd_soc_component_read(component, RT5640_IRQ_CTRL2);
2163 	dev_dbg(component->dev, "irq ctrl2 %#04x\n", val);
2164 
2165 	return (val & RT5640_MB1_OC_STATUS);
2166 }
2167 
2168 static bool rt5640_jack_inserted(struct snd_soc_component *component)
2169 {
2170 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2171 	int val;
2172 
2173 	if (rt5640->jd_gpio)
2174 		val = gpiod_get_value(rt5640->jd_gpio) ? RT5640_JD_STATUS : 0;
2175 	else
2176 		val = snd_soc_component_read(component, RT5640_INT_IRQ_ST);
2177 
2178 	dev_dbg(component->dev, "irq status %#04x\n", val);
2179 
2180 	if (rt5640->jd_inverted)
2181 		return !(val & RT5640_JD_STATUS);
2182 	else
2183 		return (val & RT5640_JD_STATUS);
2184 }
2185 
2186 /* Jack detect and button-press timings */
2187 #define JACK_SETTLE_TIME	100 /* milli seconds */
2188 #define JACK_DETECT_COUNT	5
2189 #define JACK_DETECT_MAXCOUNT	20  /* Aprox. 2 seconds worth of tries */
2190 #define JACK_UNPLUG_TIME	80  /* milli seconds */
2191 #define BP_POLL_TIME		10  /* milli seconds */
2192 #define BP_POLL_MAXCOUNT	200 /* assume something is wrong after this */
2193 #define BP_THRESHOLD		3
2194 
2195 static void rt5640_start_button_press_work(struct snd_soc_component *component)
2196 {
2197 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2198 
2199 	rt5640->poll_count = 0;
2200 	rt5640->press_count = 0;
2201 	rt5640->release_count = 0;
2202 	rt5640->pressed = false;
2203 	rt5640->press_reported = false;
2204 	rt5640_clear_micbias1_ovcd(component);
2205 	schedule_delayed_work(&rt5640->bp_work, msecs_to_jiffies(BP_POLL_TIME));
2206 }
2207 
2208 static void rt5640_button_press_work(struct work_struct *work)
2209 {
2210 	struct rt5640_priv *rt5640 =
2211 		container_of(work, struct rt5640_priv, bp_work.work);
2212 	struct snd_soc_component *component = rt5640->component;
2213 
2214 	/* Check the jack was not removed underneath us */
2215 	if (!rt5640_jack_inserted(component))
2216 		return;
2217 
2218 	if (rt5640_micbias1_ovcd(component)) {
2219 		rt5640->release_count = 0;
2220 		rt5640->press_count++;
2221 		/* Remember till after JACK_UNPLUG_TIME wait */
2222 		if (rt5640->press_count >= BP_THRESHOLD)
2223 			rt5640->pressed = true;
2224 		rt5640_clear_micbias1_ovcd(component);
2225 	} else {
2226 		rt5640->press_count = 0;
2227 		rt5640->release_count++;
2228 	}
2229 
2230 	/*
2231 	 * The pins get temporarily shorted on jack unplug, so we poll for
2232 	 * at least JACK_UNPLUG_TIME milli-seconds before reporting a press.
2233 	 */
2234 	rt5640->poll_count++;
2235 	if (rt5640->poll_count < (JACK_UNPLUG_TIME / BP_POLL_TIME)) {
2236 		schedule_delayed_work(&rt5640->bp_work,
2237 				      msecs_to_jiffies(BP_POLL_TIME));
2238 		return;
2239 	}
2240 
2241 	if (rt5640->pressed && !rt5640->press_reported) {
2242 		dev_dbg(component->dev, "headset button press\n");
2243 		snd_soc_jack_report(rt5640->jack, SND_JACK_BTN_0,
2244 				    SND_JACK_BTN_0);
2245 		rt5640->press_reported = true;
2246 	}
2247 
2248 	if (rt5640->release_count >= BP_THRESHOLD) {
2249 		if (rt5640->press_reported) {
2250 			dev_dbg(component->dev, "headset button release\n");
2251 			snd_soc_jack_report(rt5640->jack, 0, SND_JACK_BTN_0);
2252 		}
2253 		/* Re-enable OVCD IRQ to detect next press */
2254 		rt5640_enable_micbias1_ovcd_irq(component);
2255 		return; /* Stop polling */
2256 	}
2257 
2258 	schedule_delayed_work(&rt5640->bp_work, msecs_to_jiffies(BP_POLL_TIME));
2259 }
2260 
2261 int rt5640_detect_headset(struct snd_soc_component *component, struct gpio_desc *hp_det_gpio)
2262 {
2263 	int i, headset_count = 0, headphone_count = 0;
2264 
2265 	/*
2266 	 * We get the insertion event before the jack is fully inserted at which
2267 	 * point the second ring on a TRRS connector may short the 2nd ring and
2268 	 * sleeve contacts, also the overcurrent detection is not entirely
2269 	 * reliable. So we try several times with a wait in between until we
2270 	 * detect the same type JACK_DETECT_COUNT times in a row.
2271 	 */
2272 	for (i = 0; i < JACK_DETECT_MAXCOUNT; i++) {
2273 		/* Clear any previous over-current status flag */
2274 		rt5640_clear_micbias1_ovcd(component);
2275 
2276 		msleep(JACK_SETTLE_TIME);
2277 
2278 		/* Check the jack is still connected before checking ovcd */
2279 		if (hp_det_gpio) {
2280 			if (gpiod_get_value_cansleep(hp_det_gpio))
2281 				return 0;
2282 		} else {
2283 			if (!rt5640_jack_inserted(component))
2284 				return 0;
2285 		}
2286 
2287 		if (rt5640_micbias1_ovcd(component)) {
2288 			/*
2289 			 * Over current detected, there is a short between the
2290 			 * 2nd ring contact and the ground, so a TRS connector
2291 			 * without a mic contact and thus plain headphones.
2292 			 */
2293 			dev_dbg(component->dev, "jack mic-gnd shorted\n");
2294 			headset_count = 0;
2295 			headphone_count++;
2296 			if (headphone_count == JACK_DETECT_COUNT)
2297 				return SND_JACK_HEADPHONE;
2298 		} else {
2299 			dev_dbg(component->dev, "jack mic-gnd open\n");
2300 			headphone_count = 0;
2301 			headset_count++;
2302 			if (headset_count == JACK_DETECT_COUNT)
2303 				return SND_JACK_HEADSET;
2304 		}
2305 	}
2306 
2307 	dev_err(component->dev, "Error detecting headset vs headphones, bad contact?, assuming headphones\n");
2308 	return SND_JACK_HEADPHONE;
2309 }
2310 EXPORT_SYMBOL_GPL(rt5640_detect_headset);
2311 
2312 static void rt5640_jack_work(struct work_struct *work)
2313 {
2314 	struct rt5640_priv *rt5640 =
2315 		container_of(work, struct rt5640_priv, jack_work.work);
2316 	struct snd_soc_component *component = rt5640->component;
2317 	int status;
2318 
2319 	if (rt5640->jd_src == RT5640_JD_SRC_HDA_HEADER) {
2320 		int val, jack_type = 0, hda_mic_plugged, hda_hp_plugged;
2321 
2322 		/* mic jack */
2323 		val = snd_soc_component_read(component, RT5640_INT_IRQ_ST);
2324 		hda_mic_plugged = !(val & RT5640_JD_STATUS);
2325 		dev_dbg(component->dev, "mic jack status %d\n",
2326 			hda_mic_plugged);
2327 
2328 		snd_soc_component_update_bits(component, RT5640_IRQ_CTRL1,
2329 			RT5640_JD_P_MASK, !hda_mic_plugged << RT5640_JD_P_SFT);
2330 
2331 		if (hda_mic_plugged)
2332 			jack_type |= SND_JACK_MICROPHONE;
2333 
2334 		/* headphone jack */
2335 		val = snd_soc_component_read(component, RT5640_DUMMY2);
2336 		hda_hp_plugged = !(val & (0x1 << 11));
2337 		dev_dbg(component->dev, "headphone jack status %d\n",
2338 			hda_hp_plugged);
2339 
2340 		snd_soc_component_update_bits(component, RT5640_DUMMY2,
2341 			(0x1 << 10), !hda_hp_plugged << 10);
2342 
2343 		if (hda_hp_plugged)
2344 			jack_type |= SND_JACK_HEADPHONE;
2345 
2346 		snd_soc_jack_report(rt5640->jack, jack_type, SND_JACK_HEADSET);
2347 
2348 		return;
2349 	}
2350 
2351 	if (!rt5640_jack_inserted(component)) {
2352 		/* Jack removed, or spurious IRQ? */
2353 		if (rt5640->jack->status & SND_JACK_HEADPHONE) {
2354 			if (rt5640->jack->status & SND_JACK_MICROPHONE) {
2355 				cancel_delayed_work_sync(&rt5640->bp_work);
2356 				rt5640_disable_micbias1_ovcd_irq(component);
2357 				rt5640_disable_micbias1_for_ovcd(component);
2358 			}
2359 			snd_soc_jack_report(rt5640->jack, 0,
2360 					    SND_JACK_HEADSET | SND_JACK_BTN_0);
2361 			dev_dbg(component->dev, "jack unplugged\n");
2362 		}
2363 	} else if (!(rt5640->jack->status & SND_JACK_HEADPHONE)) {
2364 		/* Jack inserted */
2365 		WARN_ON(rt5640->ovcd_irq_enabled);
2366 		rt5640_enable_micbias1_for_ovcd(component);
2367 		status = rt5640_detect_headset(component, NULL);
2368 		if (status == SND_JACK_HEADSET) {
2369 			/* Enable ovcd IRQ for button press detect. */
2370 			rt5640_enable_micbias1_ovcd_irq(component);
2371 		} else {
2372 			/* No more need for overcurrent detect. */
2373 			rt5640_disable_micbias1_for_ovcd(component);
2374 		}
2375 		dev_dbg(component->dev, "detect status %#02x\n", status);
2376 		snd_soc_jack_report(rt5640->jack, status, SND_JACK_HEADSET);
2377 	} else if (rt5640->ovcd_irq_enabled && rt5640_micbias1_ovcd(component)) {
2378 		dev_dbg(component->dev, "OVCD IRQ\n");
2379 
2380 		/*
2381 		 * The ovcd IRQ keeps firing while the button is pressed, so
2382 		 * we disable it and start polling the button until released.
2383 		 *
2384 		 * The disable will make the IRQ pin 0 again and since we get
2385 		 * IRQs on both edges (so as to detect both jack plugin and
2386 		 * unplug) this means we will immediately get another IRQ.
2387 		 * The ovcd_irq_enabled check above makes the 2ND IRQ a NOP.
2388 		 */
2389 		rt5640_disable_micbias1_ovcd_irq(component);
2390 		rt5640_start_button_press_work(component);
2391 
2392 		/*
2393 		 * If the jack-detect IRQ flag goes high (unplug) after our
2394 		 * above rt5640_jack_inserted() check and before we have
2395 		 * disabled the OVCD IRQ, the IRQ pin will stay high and as
2396 		 * we react to edges, we miss the unplug event -> recheck.
2397 		 */
2398 		queue_delayed_work(system_long_wq, &rt5640->jack_work, 0);
2399 	}
2400 }
2401 
2402 static irqreturn_t rt5640_irq(int irq, void *data)
2403 {
2404 	struct rt5640_priv *rt5640 = data;
2405 	int delay = 0;
2406 
2407 	if (rt5640->jd_src == RT5640_JD_SRC_HDA_HEADER) {
2408 		cancel_delayed_work_sync(&rt5640->jack_work);
2409 		delay = 100;
2410 	}
2411 
2412 	if (rt5640->jack)
2413 		queue_delayed_work(system_long_wq, &rt5640->jack_work, delay);
2414 
2415 	return IRQ_HANDLED;
2416 }
2417 
2418 static irqreturn_t rt5640_jd_gpio_irq(int irq, void *data)
2419 {
2420 	struct rt5640_priv *rt5640 = data;
2421 
2422 	queue_delayed_work(system_long_wq, &rt5640->jack_work,
2423 			   msecs_to_jiffies(JACK_SETTLE_TIME));
2424 
2425 	return IRQ_HANDLED;
2426 }
2427 
2428 static void rt5640_cancel_work(void *data)
2429 {
2430 	struct rt5640_priv *rt5640 = data;
2431 
2432 	cancel_delayed_work_sync(&rt5640->jack_work);
2433 	cancel_delayed_work_sync(&rt5640->bp_work);
2434 }
2435 
2436 void rt5640_set_ovcd_params(struct snd_soc_component *component)
2437 {
2438 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2439 
2440 	snd_soc_component_write(component, RT5640_PR_BASE + RT5640_BIAS_CUR4,
2441 		0xa800 | rt5640->ovcd_sf);
2442 
2443 	snd_soc_component_update_bits(component, RT5640_MICBIAS,
2444 		RT5640_MIC1_OVTH_MASK | RT5640_MIC1_OVCD_MASK,
2445 		rt5640->ovcd_th | RT5640_MIC1_OVCD_EN);
2446 
2447 	/*
2448 	 * The over-current-detect is only reliable in detecting the absence
2449 	 * of over-current, when the mic-contact in the jack is short-circuited,
2450 	 * the hardware periodically retries if it can apply the bias-current
2451 	 * leading to the ovcd status flip-flopping 1-0-1 with it being 0 about
2452 	 * 10% of the time, as we poll the ovcd status bit we might hit that
2453 	 * 10%, so we enable sticky mode and when checking OVCD we clear the
2454 	 * status, msleep() a bit and then check to get a reliable reading.
2455 	 */
2456 	snd_soc_component_update_bits(component, RT5640_IRQ_CTRL2,
2457 		RT5640_MB1_OC_STKY_MASK, RT5640_MB1_OC_STKY_EN);
2458 }
2459 EXPORT_SYMBOL_GPL(rt5640_set_ovcd_params);
2460 
2461 static void rt5640_disable_jack_detect(struct snd_soc_component *component)
2462 {
2463 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2464 
2465 	/*
2466 	 * soc_remove_component() force-disables jack and thus rt5640->jack
2467 	 * could be NULL at the time of driver's module unloading.
2468 	 */
2469 	if (!rt5640->jack)
2470 		return;
2471 
2472 	if (rt5640->jd_gpio_irq_requested)
2473 		free_irq(rt5640->jd_gpio_irq, rt5640);
2474 
2475 	if (rt5640->irq_requested)
2476 		free_irq(rt5640->irq, rt5640);
2477 
2478 	rt5640_cancel_work(rt5640);
2479 
2480 	if (rt5640->jack->status & SND_JACK_MICROPHONE) {
2481 		rt5640_disable_micbias1_ovcd_irq(component);
2482 		rt5640_disable_micbias1_for_ovcd(component);
2483 		snd_soc_jack_report(rt5640->jack, 0, SND_JACK_BTN_0);
2484 	}
2485 
2486 	rt5640->jd_gpio_irq_requested = false;
2487 	rt5640->irq_requested = false;
2488 	rt5640->jd_gpio = NULL;
2489 	rt5640->jack = NULL;
2490 }
2491 
2492 static void rt5640_enable_jack_detect(struct snd_soc_component *component,
2493 				      struct snd_soc_jack *jack,
2494 				      struct rt5640_set_jack_data *jack_data)
2495 {
2496 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2497 	int ret;
2498 
2499 	/* Select JD-source */
2500 	snd_soc_component_update_bits(component, RT5640_JD_CTRL,
2501 		RT5640_JD_MASK, rt5640->jd_src << RT5640_JD_SFT);
2502 
2503 	/* Selecting GPIO01 as an interrupt */
2504 	snd_soc_component_update_bits(component, RT5640_GPIO_CTRL1,
2505 		RT5640_GP1_PIN_MASK, RT5640_GP1_PIN_IRQ);
2506 
2507 	/* Set GPIO1 output */
2508 	snd_soc_component_update_bits(component, RT5640_GPIO_CTRL3,
2509 		RT5640_GP1_PF_MASK, RT5640_GP1_PF_OUT);
2510 
2511 	snd_soc_component_write(component, RT5640_DUMMY1, 0x3f41);
2512 
2513 	rt5640_set_ovcd_params(component);
2514 
2515 	/*
2516 	 * All IRQs get or-ed together, so we need the jack IRQ to report 0
2517 	 * when a jack is inserted so that the OVCD IRQ then toggles the IRQ
2518 	 * pin 0/1 instead of it being stuck to 1. So we invert the JD polarity
2519 	 * on systems where the hardware does not already do this.
2520 	 */
2521 	if (rt5640->jd_inverted) {
2522 		if (rt5640->jd_src == RT5640_JD_SRC_JD1_IN4P)
2523 			snd_soc_component_write(component, RT5640_IRQ_CTRL1,
2524 				RT5640_IRQ_JD_NOR);
2525 		else if (rt5640->jd_src == RT5640_JD_SRC_JD2_IN4N)
2526 			snd_soc_component_update_bits(component, RT5640_DUMMY2,
2527 				RT5640_IRQ_JD2_MASK | RT5640_JD2_MASK,
2528 				RT5640_IRQ_JD2_NOR | RT5640_JD2_EN);
2529 	} else {
2530 		if (rt5640->jd_src == RT5640_JD_SRC_JD1_IN4P)
2531 			snd_soc_component_write(component, RT5640_IRQ_CTRL1,
2532 				RT5640_IRQ_JD_NOR | RT5640_JD_P_INV);
2533 		else if (rt5640->jd_src == RT5640_JD_SRC_JD2_IN4N)
2534 			snd_soc_component_update_bits(component, RT5640_DUMMY2,
2535 				RT5640_IRQ_JD2_MASK | RT5640_JD2_P_MASK |
2536 				RT5640_JD2_MASK,
2537 				RT5640_IRQ_JD2_NOR | RT5640_JD2_P_INV |
2538 				RT5640_JD2_EN);
2539 	}
2540 
2541 	rt5640->jack = jack;
2542 	if (rt5640->jack->status & SND_JACK_MICROPHONE) {
2543 		rt5640_enable_micbias1_for_ovcd(component);
2544 		rt5640_enable_micbias1_ovcd_irq(component);
2545 	}
2546 
2547 	if (jack_data && jack_data->codec_irq_override)
2548 		rt5640->irq = jack_data->codec_irq_override;
2549 
2550 	if (jack_data && jack_data->jd_gpio) {
2551 		rt5640->jd_gpio = jack_data->jd_gpio;
2552 		rt5640->jd_gpio_irq = gpiod_to_irq(rt5640->jd_gpio);
2553 
2554 		ret = request_irq(rt5640->jd_gpio_irq, rt5640_jd_gpio_irq,
2555 				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2556 				  "rt5640-jd-gpio", rt5640);
2557 		if (ret) {
2558 			dev_warn(component->dev, "Failed to request jd GPIO IRQ %d: %d\n",
2559 				 rt5640->jd_gpio_irq, ret);
2560 			rt5640_disable_jack_detect(component);
2561 			return;
2562 		}
2563 		rt5640->jd_gpio_irq_requested = true;
2564 	}
2565 
2566 	if (jack_data && jack_data->use_platform_clock)
2567 		rt5640->use_platform_clock = jack_data->use_platform_clock;
2568 
2569 	ret = devm_request_threaded_irq(component->dev, rt5640->irq,
2570 					NULL, rt5640_irq,
2571 					IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
2572 					"rt5640", rt5640);
2573 	if (ret) {
2574 		dev_warn(component->dev, "Failed to reguest IRQ %d: %d\n", rt5640->irq, ret);
2575 		rt5640_disable_jack_detect(component);
2576 		return;
2577 	}
2578 	rt5640->irq_requested = true;
2579 
2580 	/* sync initial jack state */
2581 	queue_delayed_work(system_long_wq, &rt5640->jack_work, 0);
2582 }
2583 
2584 static const struct snd_soc_dapm_route rt5640_hda_jack_dapm_routes[] = {
2585 	{"IN1P", NULL, "MICBIAS1"},
2586 	{"IN2P", NULL, "MICBIAS1"},
2587 	{"IN3P", NULL, "MICBIAS1"},
2588 };
2589 
2590 static void rt5640_enable_hda_jack_detect(
2591 	struct snd_soc_component *component, struct snd_soc_jack *jack)
2592 {
2593 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2594 	struct snd_soc_dapm_context *dapm =
2595 		snd_soc_component_get_dapm(component);
2596 	int ret;
2597 
2598 	/* Select JD1 for Mic */
2599 	snd_soc_component_update_bits(component, RT5640_JD_CTRL,
2600 		RT5640_JD_MASK, RT5640_JD_JD1_IN4P);
2601 	snd_soc_component_write(component, RT5640_IRQ_CTRL1, RT5640_IRQ_JD_NOR);
2602 
2603 	/* Select JD2 for Headphone */
2604 	snd_soc_component_update_bits(component, RT5640_DUMMY2, 0x1100, 0x1100);
2605 
2606 	/* Selecting GPIO01 as an interrupt */
2607 	snd_soc_component_update_bits(component, RT5640_GPIO_CTRL1,
2608 		RT5640_GP1_PIN_MASK, RT5640_GP1_PIN_IRQ);
2609 
2610 	/* Set GPIO1 output */
2611 	snd_soc_component_update_bits(component, RT5640_GPIO_CTRL3,
2612 		RT5640_GP1_PF_MASK, RT5640_GP1_PF_OUT);
2613 
2614 	snd_soc_component_update_bits(component, RT5640_DUMMY1, 0x400, 0x0);
2615 
2616 	snd_soc_component_update_bits(component, RT5640_PWR_ANLG1,
2617 		RT5640_PWR_VREF2 | RT5640_PWR_MB | RT5640_PWR_BG,
2618 		RT5640_PWR_VREF2 | RT5640_PWR_MB | RT5640_PWR_BG);
2619 	usleep_range(10000, 15000);
2620 	snd_soc_component_update_bits(component, RT5640_PWR_ANLG1,
2621 		RT5640_PWR_FV2, RT5640_PWR_FV2);
2622 
2623 	rt5640->jack = jack;
2624 
2625 	ret = devm_request_threaded_irq(component->dev, rt5640->irq,
2626 					NULL, rt5640_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT,
2627 					"rt5640", rt5640);
2628 	if (ret) {
2629 		dev_warn(component->dev, "Failed to reguest IRQ %d: %d\n", rt5640->irq, ret);
2630 		rt5640->irq = -ENXIO;
2631 		return;
2632 	}
2633 
2634 	/* sync initial jack state */
2635 	queue_delayed_work(system_long_wq, &rt5640->jack_work, 0);
2636 
2637 	snd_soc_dapm_add_routes(dapm, rt5640_hda_jack_dapm_routes,
2638 		ARRAY_SIZE(rt5640_hda_jack_dapm_routes));
2639 }
2640 
2641 static int rt5640_set_jack(struct snd_soc_component *component,
2642 			   struct snd_soc_jack *jack, void *data)
2643 {
2644 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2645 
2646 	if (jack) {
2647 		if (rt5640->jd_src == RT5640_JD_SRC_HDA_HEADER)
2648 			rt5640_enable_hda_jack_detect(component, jack);
2649 		else
2650 			rt5640_enable_jack_detect(component, jack, data);
2651 	} else {
2652 		rt5640_disable_jack_detect(component);
2653 	}
2654 
2655 	return 0;
2656 }
2657 
2658 static int rt5640_probe(struct snd_soc_component *component)
2659 {
2660 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
2661 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2662 	u32 dmic1_data_pin = 0;
2663 	u32 dmic2_data_pin = 0;
2664 	bool dmic_en = false;
2665 	u32 val;
2666 
2667 	/* Check if MCLK provided */
2668 	rt5640->mclk = devm_clk_get(component->dev, "mclk");
2669 	if (PTR_ERR(rt5640->mclk) == -EPROBE_DEFER)
2670 		return -EPROBE_DEFER;
2671 
2672 	rt5640->component = component;
2673 
2674 	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
2675 
2676 	snd_soc_component_update_bits(component, RT5640_DUMMY1, 0x0301, 0x0301);
2677 	snd_soc_component_update_bits(component, RT5640_MICBIAS, 0x0030, 0x0030);
2678 	snd_soc_component_update_bits(component, RT5640_DSP_PATH2, 0xfc00, 0x0c00);
2679 
2680 	switch (snd_soc_component_read(component, RT5640_RESET) & RT5640_ID_MASK) {
2681 	case RT5640_ID_5640:
2682 	case RT5640_ID_5642:
2683 		snd_soc_add_component_controls(component,
2684 			rt5640_specific_snd_controls,
2685 			ARRAY_SIZE(rt5640_specific_snd_controls));
2686 		snd_soc_dapm_new_controls(dapm,
2687 			rt5640_specific_dapm_widgets,
2688 			ARRAY_SIZE(rt5640_specific_dapm_widgets));
2689 		snd_soc_dapm_add_routes(dapm,
2690 			rt5640_specific_dapm_routes,
2691 			ARRAY_SIZE(rt5640_specific_dapm_routes));
2692 		break;
2693 	case RT5640_ID_5639:
2694 		snd_soc_dapm_new_controls(dapm,
2695 			rt5639_specific_dapm_widgets,
2696 			ARRAY_SIZE(rt5639_specific_dapm_widgets));
2697 		snd_soc_dapm_add_routes(dapm,
2698 			rt5639_specific_dapm_routes,
2699 			ARRAY_SIZE(rt5639_specific_dapm_routes));
2700 		break;
2701 	default:
2702 		dev_err(component->dev,
2703 			"The driver is for RT5639 RT5640 or RT5642 only\n");
2704 		return -ENODEV;
2705 	}
2706 
2707 	/*
2708 	 * Note on some platforms the platform code may need to add device-props
2709 	 * rather then relying only on properties set by the firmware.
2710 	 * Therefor the property parsing MUST be done here, rather then from
2711 	 * rt5640_i2c_probe(), so that the platform-code can attach extra
2712 	 * properties before calling snd_soc_register_card().
2713 	 */
2714 	if (device_property_read_bool(component->dev, "realtek,in1-differential"))
2715 		snd_soc_component_update_bits(component, RT5640_IN1_IN2,
2716 					      RT5640_IN_DF1, RT5640_IN_DF1);
2717 
2718 	if (device_property_read_bool(component->dev, "realtek,in2-differential"))
2719 		snd_soc_component_update_bits(component, RT5640_IN3_IN4,
2720 					      RT5640_IN_DF2, RT5640_IN_DF2);
2721 
2722 	if (device_property_read_bool(component->dev, "realtek,in3-differential"))
2723 		snd_soc_component_update_bits(component, RT5640_IN1_IN2,
2724 					      RT5640_IN_DF2, RT5640_IN_DF2);
2725 
2726 	if (device_property_read_bool(component->dev, "realtek,lout-differential"))
2727 		snd_soc_component_update_bits(component, RT5640_DUMMY1,
2728 					      RT5640_EN_LOUT_DF, RT5640_EN_LOUT_DF);
2729 
2730 	if (device_property_read_u32(component->dev, "realtek,dmic1-data-pin",
2731 				     &val) == 0 && val) {
2732 		dmic1_data_pin = val - 1;
2733 		dmic_en = true;
2734 	}
2735 
2736 	if (device_property_read_u32(component->dev, "realtek,dmic2-data-pin",
2737 				     &val) == 0 && val) {
2738 		dmic2_data_pin = val - 1;
2739 		dmic_en = true;
2740 	}
2741 
2742 	if (dmic_en)
2743 		rt5640_dmic_enable(component, dmic1_data_pin, dmic2_data_pin);
2744 
2745 	if (device_property_read_u32(component->dev,
2746 				     "realtek,jack-detect-source", &val) == 0) {
2747 		if (val <= RT5640_JD_SRC_HDA_HEADER)
2748 			rt5640->jd_src = val;
2749 		else
2750 			dev_warn(component->dev, "Warning: Invalid jack-detect-source value: %d, leaving jack-detect disabled\n",
2751 				 val);
2752 	}
2753 
2754 	if (!device_property_read_bool(component->dev, "realtek,jack-detect-not-inverted"))
2755 		rt5640->jd_inverted = true;
2756 
2757 	/*
2758 	 * Testing on various boards has shown that good defaults for the OVCD
2759 	 * threshold and scale-factor are 2000µA and 0.75. For an effective
2760 	 * limit of 1500µA, this seems to be more reliable then 1500µA and 1.0.
2761 	 */
2762 	rt5640->ovcd_th = RT5640_MIC1_OVTH_2000UA;
2763 	rt5640->ovcd_sf = RT5640_MIC_OVCD_SF_0P75;
2764 
2765 	if (device_property_read_u32(component->dev,
2766 			"realtek,over-current-threshold-microamp", &val) == 0) {
2767 		switch (val) {
2768 		case 600:
2769 			rt5640->ovcd_th = RT5640_MIC1_OVTH_600UA;
2770 			break;
2771 		case 1500:
2772 			rt5640->ovcd_th = RT5640_MIC1_OVTH_1500UA;
2773 			break;
2774 		case 2000:
2775 			rt5640->ovcd_th = RT5640_MIC1_OVTH_2000UA;
2776 			break;
2777 		default:
2778 			dev_warn(component->dev, "Warning: Invalid over-current-threshold-microamp value: %d, defaulting to 2000uA\n",
2779 				 val);
2780 		}
2781 	}
2782 
2783 	if (device_property_read_u32(component->dev,
2784 			"realtek,over-current-scale-factor", &val) == 0) {
2785 		if (val <= RT5640_OVCD_SF_1P5)
2786 			rt5640->ovcd_sf = val << RT5640_MIC_OVCD_SF_SFT;
2787 		else
2788 			dev_warn(component->dev, "Warning: Invalid over-current-scale-factor value: %d, defaulting to 0.75\n",
2789 				 val);
2790 	}
2791 
2792 	return 0;
2793 }
2794 
2795 static void rt5640_remove(struct snd_soc_component *component)
2796 {
2797 	rt5640_reset(component);
2798 }
2799 
2800 #ifdef CONFIG_PM
2801 static int rt5640_suspend(struct snd_soc_component *component)
2802 {
2803 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2804 
2805 	if (rt5640->irq) {
2806 		/* disable jack interrupts during system suspend */
2807 		disable_irq(rt5640->irq);
2808 	}
2809 
2810 	rt5640_cancel_work(rt5640);
2811 	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
2812 	rt5640_reset(component);
2813 	regcache_cache_only(rt5640->regmap, true);
2814 	regcache_mark_dirty(rt5640->regmap);
2815 	if (gpio_is_valid(rt5640->ldo1_en))
2816 		gpio_set_value_cansleep(rt5640->ldo1_en, 0);
2817 
2818 	return 0;
2819 }
2820 
2821 static int rt5640_resume(struct snd_soc_component *component)
2822 {
2823 	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2824 
2825 	if (gpio_is_valid(rt5640->ldo1_en)) {
2826 		gpio_set_value_cansleep(rt5640->ldo1_en, 1);
2827 		msleep(400);
2828 	}
2829 
2830 	regcache_cache_only(rt5640->regmap, false);
2831 	regcache_sync(rt5640->regmap);
2832 
2833 	if (rt5640->irq)
2834 		enable_irq(rt5640->irq);
2835 
2836 	if (rt5640->jack) {
2837 		if (rt5640->jd_src == RT5640_JD_SRC_HDA_HEADER) {
2838 			snd_soc_component_update_bits(component,
2839 				RT5640_DUMMY2, 0x1100, 0x1100);
2840 		} else {
2841 			if (rt5640->jd_inverted) {
2842 				if (rt5640->jd_src == RT5640_JD_SRC_JD2_IN4N)
2843 					snd_soc_component_update_bits(
2844 						component, RT5640_DUMMY2,
2845 						RT5640_IRQ_JD2_MASK |
2846 						RT5640_JD2_MASK,
2847 						RT5640_IRQ_JD2_NOR |
2848 						RT5640_JD2_EN);
2849 
2850 			} else {
2851 				if (rt5640->jd_src == RT5640_JD_SRC_JD2_IN4N)
2852 					snd_soc_component_update_bits(
2853 						component, RT5640_DUMMY2,
2854 						RT5640_IRQ_JD2_MASK |
2855 						RT5640_JD2_P_MASK |
2856 						RT5640_JD2_MASK,
2857 						RT5640_IRQ_JD2_NOR |
2858 						RT5640_JD2_P_INV |
2859 						RT5640_JD2_EN);
2860 			}
2861 		}
2862 
2863 		queue_delayed_work(system_long_wq, &rt5640->jack_work, 0);
2864 	}
2865 
2866 	return 0;
2867 }
2868 #else
2869 #define rt5640_suspend NULL
2870 #define rt5640_resume NULL
2871 #endif
2872 
2873 #define RT5640_STEREO_RATES SNDRV_PCM_RATE_8000_96000
2874 #define RT5640_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
2875 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
2876 
2877 static const struct snd_soc_dai_ops rt5640_aif_dai_ops = {
2878 	.hw_params = rt5640_hw_params,
2879 	.set_fmt = rt5640_set_dai_fmt,
2880 	.set_sysclk = rt5640_set_dai_sysclk,
2881 	.set_pll = rt5640_set_dai_pll,
2882 };
2883 
2884 static struct snd_soc_dai_driver rt5640_dai[] = {
2885 	{
2886 		.name = "rt5640-aif1",
2887 		.id = RT5640_AIF1,
2888 		.playback = {
2889 			.stream_name = "AIF1 Playback",
2890 			.channels_min = 1,
2891 			.channels_max = 2,
2892 			.rates = RT5640_STEREO_RATES,
2893 			.formats = RT5640_FORMATS,
2894 		},
2895 		.capture = {
2896 			.stream_name = "AIF1 Capture",
2897 			.channels_min = 1,
2898 			.channels_max = 2,
2899 			.rates = RT5640_STEREO_RATES,
2900 			.formats = RT5640_FORMATS,
2901 		},
2902 		.ops = &rt5640_aif_dai_ops,
2903 	},
2904 	{
2905 		.name = "rt5640-aif2",
2906 		.id = RT5640_AIF2,
2907 		.playback = {
2908 			.stream_name = "AIF2 Playback",
2909 			.channels_min = 1,
2910 			.channels_max = 2,
2911 			.rates = RT5640_STEREO_RATES,
2912 			.formats = RT5640_FORMATS,
2913 		},
2914 		.capture = {
2915 			.stream_name = "AIF2 Capture",
2916 			.channels_min = 1,
2917 			.channels_max = 2,
2918 			.rates = RT5640_STEREO_RATES,
2919 			.formats = RT5640_FORMATS,
2920 		},
2921 		.ops = &rt5640_aif_dai_ops,
2922 	},
2923 };
2924 
2925 static const struct snd_soc_component_driver soc_component_dev_rt5640 = {
2926 	.probe			= rt5640_probe,
2927 	.remove			= rt5640_remove,
2928 	.suspend		= rt5640_suspend,
2929 	.resume			= rt5640_resume,
2930 	.set_bias_level		= rt5640_set_bias_level,
2931 	.set_jack		= rt5640_set_jack,
2932 	.controls		= rt5640_snd_controls,
2933 	.num_controls		= ARRAY_SIZE(rt5640_snd_controls),
2934 	.dapm_widgets		= rt5640_dapm_widgets,
2935 	.num_dapm_widgets	= ARRAY_SIZE(rt5640_dapm_widgets),
2936 	.dapm_routes		= rt5640_dapm_routes,
2937 	.num_dapm_routes	= ARRAY_SIZE(rt5640_dapm_routes),
2938 	.use_pmdown_time	= 1,
2939 	.endianness		= 1,
2940 };
2941 
2942 static const struct regmap_config rt5640_regmap = {
2943 	.reg_bits = 8,
2944 	.val_bits = 16,
2945 	.use_single_read = true,
2946 	.use_single_write = true,
2947 
2948 	.max_register = RT5640_VENDOR_ID2 + 1 + (ARRAY_SIZE(rt5640_ranges) *
2949 					       RT5640_PR_SPACING),
2950 	.volatile_reg = rt5640_volatile_register,
2951 	.readable_reg = rt5640_readable_register,
2952 
2953 	.cache_type = REGCACHE_MAPLE,
2954 	.reg_defaults = rt5640_reg,
2955 	.num_reg_defaults = ARRAY_SIZE(rt5640_reg),
2956 	.ranges = rt5640_ranges,
2957 	.num_ranges = ARRAY_SIZE(rt5640_ranges),
2958 };
2959 
2960 static const struct i2c_device_id rt5640_i2c_id[] = {
2961 	{ "rt5640", 0 },
2962 	{ "rt5639", 0 },
2963 	{ "rt5642", 0 },
2964 	{ }
2965 };
2966 MODULE_DEVICE_TABLE(i2c, rt5640_i2c_id);
2967 
2968 #if defined(CONFIG_OF)
2969 static const struct of_device_id rt5640_of_match[] = {
2970 	{ .compatible = "realtek,rt5639", },
2971 	{ .compatible = "realtek,rt5640", },
2972 	{},
2973 };
2974 MODULE_DEVICE_TABLE(of, rt5640_of_match);
2975 #endif
2976 
2977 #ifdef CONFIG_ACPI
2978 static const struct acpi_device_id rt5640_acpi_match[] = {
2979 	{ "INT33CA", 0 },
2980 	{ "10EC3276", 0 },
2981 	{ "10EC5640", 0 },
2982 	{ "10EC5642", 0 },
2983 	{ "INTCCFFD", 0 },
2984 	{ },
2985 };
2986 MODULE_DEVICE_TABLE(acpi, rt5640_acpi_match);
2987 #endif
2988 
2989 static int rt5640_parse_dt(struct rt5640_priv *rt5640, struct device_node *np)
2990 {
2991 	rt5640->ldo1_en = of_get_named_gpio(np, "realtek,ldo1-en-gpios", 0);
2992 	/*
2993 	 * LDO1_EN is optional (it may be statically tied on the board).
2994 	 * -ENOENT means that the property doesn't exist, i.e. there is no
2995 	 * GPIO, so is not an error. Any other error code means the property
2996 	 * exists, but could not be parsed.
2997 	 */
2998 	if (!gpio_is_valid(rt5640->ldo1_en) &&
2999 			(rt5640->ldo1_en != -ENOENT))
3000 		return rt5640->ldo1_en;
3001 
3002 	return 0;
3003 }
3004 
3005 static int rt5640_i2c_probe(struct i2c_client *i2c)
3006 {
3007 	struct rt5640_priv *rt5640;
3008 	int ret;
3009 	unsigned int val;
3010 
3011 	rt5640 = devm_kzalloc(&i2c->dev,
3012 				sizeof(struct rt5640_priv),
3013 				GFP_KERNEL);
3014 	if (NULL == rt5640)
3015 		return -ENOMEM;
3016 	i2c_set_clientdata(i2c, rt5640);
3017 
3018 	if (i2c->dev.of_node) {
3019 		ret = rt5640_parse_dt(rt5640, i2c->dev.of_node);
3020 		if (ret)
3021 			return ret;
3022 	} else
3023 		rt5640->ldo1_en = -EINVAL;
3024 
3025 	rt5640->regmap = devm_regmap_init_i2c(i2c, &rt5640_regmap);
3026 	if (IS_ERR(rt5640->regmap)) {
3027 		ret = PTR_ERR(rt5640->regmap);
3028 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
3029 			ret);
3030 		return ret;
3031 	}
3032 
3033 	if (gpio_is_valid(rt5640->ldo1_en)) {
3034 		ret = devm_gpio_request_one(&i2c->dev, rt5640->ldo1_en,
3035 					    GPIOF_OUT_INIT_HIGH,
3036 					    "RT5640 LDO1_EN");
3037 		if (ret < 0) {
3038 			dev_err(&i2c->dev, "Failed to request LDO1_EN %d: %d\n",
3039 				rt5640->ldo1_en, ret);
3040 			return ret;
3041 		}
3042 		msleep(400);
3043 	}
3044 
3045 	regmap_read(rt5640->regmap, RT5640_VENDOR_ID2, &val);
3046 	if (val != RT5640_DEVICE_ID) {
3047 		dev_err(&i2c->dev,
3048 			"Device with ID register %#x is not rt5640/39\n", val);
3049 		return -ENODEV;
3050 	}
3051 
3052 	regmap_write(rt5640->regmap, RT5640_RESET, 0);
3053 
3054 	ret = regmap_register_patch(rt5640->regmap, init_list,
3055 				    ARRAY_SIZE(init_list));
3056 	if (ret != 0)
3057 		dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
3058 
3059 	regmap_update_bits(rt5640->regmap, RT5640_DUMMY1,
3060 				RT5640_MCLK_DET, RT5640_MCLK_DET);
3061 
3062 	rt5640->hp_mute = true;
3063 	rt5640->irq = i2c->irq;
3064 	INIT_DELAYED_WORK(&rt5640->bp_work, rt5640_button_press_work);
3065 	INIT_DELAYED_WORK(&rt5640->jack_work, rt5640_jack_work);
3066 
3067 	/* Make sure work is stopped on probe-error / remove */
3068 	ret = devm_add_action_or_reset(&i2c->dev, rt5640_cancel_work, rt5640);
3069 	if (ret)
3070 		return ret;
3071 
3072 	return devm_snd_soc_register_component(&i2c->dev,
3073 				      &soc_component_dev_rt5640,
3074 				      rt5640_dai, ARRAY_SIZE(rt5640_dai));
3075 }
3076 
3077 static struct i2c_driver rt5640_i2c_driver = {
3078 	.driver = {
3079 		.name = "rt5640",
3080 		.acpi_match_table = ACPI_PTR(rt5640_acpi_match),
3081 		.of_match_table = of_match_ptr(rt5640_of_match),
3082 	},
3083 	.probe = rt5640_i2c_probe,
3084 	.id_table = rt5640_i2c_id,
3085 };
3086 module_i2c_driver(rt5640_i2c_driver);
3087 
3088 MODULE_DESCRIPTION("ASoC RT5640/RT5639 driver");
3089 MODULE_AUTHOR("Johnny Hsu <johnnyhsu@realtek.com>");
3090 MODULE_LICENSE("GPL v2");
3091