xref: /openbmc/linux/sound/soc/codecs/rt5640.c (revision 52fb57e7)
1 /*
2  * rt5640.c  --  RT5640/RT5639 ALSA SoC audio codec driver
3  *
4  * Copyright 2011 Realtek Semiconductor Corp.
5  * Author: Johnny Hsu <johnnyhsu@realtek.com>
6  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/pm.h>
18 #include <linux/gpio.h>
19 #include <linux/i2c.h>
20 #include <linux/regmap.h>
21 #include <linux/of.h>
22 #include <linux/of_gpio.h>
23 #include <linux/platform_device.h>
24 #include <linux/spi/spi.h>
25 #include <linux/acpi.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/soc-dapm.h>
31 #include <sound/initval.h>
32 #include <sound/tlv.h>
33 
34 #include "rl6231.h"
35 #include "rt5640.h"
36 
37 #define RT5640_DEVICE_ID 0x6231
38 
39 #define RT5640_PR_RANGE_BASE (0xff + 1)
40 #define RT5640_PR_SPACING 0x100
41 
42 #define RT5640_PR_BASE (RT5640_PR_RANGE_BASE + (0 * RT5640_PR_SPACING))
43 
44 static const struct regmap_range_cfg rt5640_ranges[] = {
45 	{ .name = "PR", .range_min = RT5640_PR_BASE,
46 	  .range_max = RT5640_PR_BASE + 0xb4,
47 	  .selector_reg = RT5640_PRIV_INDEX,
48 	  .selector_mask = 0xff,
49 	  .selector_shift = 0x0,
50 	  .window_start = RT5640_PRIV_DATA,
51 	  .window_len = 0x1, },
52 };
53 
54 static struct reg_default init_list[] = {
55 	{RT5640_PR_BASE + 0x3d,	0x3600},
56 	{RT5640_PR_BASE + 0x12,	0x0aa8},
57 	{RT5640_PR_BASE + 0x14,	0x0aaa},
58 	{RT5640_PR_BASE + 0x20,	0x6110},
59 	{RT5640_PR_BASE + 0x21,	0xe0e0},
60 	{RT5640_PR_BASE + 0x23,	0x1804},
61 };
62 #define RT5640_INIT_REG_LEN ARRAY_SIZE(init_list)
63 
64 static const struct reg_default rt5640_reg[] = {
65 	{ 0x00, 0x000e },
66 	{ 0x01, 0xc8c8 },
67 	{ 0x02, 0xc8c8 },
68 	{ 0x03, 0xc8c8 },
69 	{ 0x04, 0x8000 },
70 	{ 0x0d, 0x0000 },
71 	{ 0x0e, 0x0000 },
72 	{ 0x0f, 0x0808 },
73 	{ 0x19, 0xafaf },
74 	{ 0x1a, 0xafaf },
75 	{ 0x1b, 0x0000 },
76 	{ 0x1c, 0x2f2f },
77 	{ 0x1d, 0x2f2f },
78 	{ 0x1e, 0x0000 },
79 	{ 0x27, 0x7060 },
80 	{ 0x28, 0x7070 },
81 	{ 0x29, 0x8080 },
82 	{ 0x2a, 0x5454 },
83 	{ 0x2b, 0x5454 },
84 	{ 0x2c, 0xaa00 },
85 	{ 0x2d, 0x0000 },
86 	{ 0x2e, 0xa000 },
87 	{ 0x2f, 0x0000 },
88 	{ 0x3b, 0x0000 },
89 	{ 0x3c, 0x007f },
90 	{ 0x3d, 0x0000 },
91 	{ 0x3e, 0x007f },
92 	{ 0x45, 0xe000 },
93 	{ 0x46, 0x003e },
94 	{ 0x47, 0x003e },
95 	{ 0x48, 0xf800 },
96 	{ 0x49, 0x3800 },
97 	{ 0x4a, 0x0004 },
98 	{ 0x4c, 0xfc00 },
99 	{ 0x4d, 0x0000 },
100 	{ 0x4f, 0x01ff },
101 	{ 0x50, 0x0000 },
102 	{ 0x51, 0x0000 },
103 	{ 0x52, 0x01ff },
104 	{ 0x53, 0xf000 },
105 	{ 0x61, 0x0000 },
106 	{ 0x62, 0x0000 },
107 	{ 0x63, 0x00c0 },
108 	{ 0x64, 0x0000 },
109 	{ 0x65, 0x0000 },
110 	{ 0x66, 0x0000 },
111 	{ 0x6a, 0x0000 },
112 	{ 0x6c, 0x0000 },
113 	{ 0x70, 0x8000 },
114 	{ 0x71, 0x8000 },
115 	{ 0x72, 0x8000 },
116 	{ 0x73, 0x1114 },
117 	{ 0x74, 0x0c00 },
118 	{ 0x75, 0x1d00 },
119 	{ 0x80, 0x0000 },
120 	{ 0x81, 0x0000 },
121 	{ 0x82, 0x0000 },
122 	{ 0x83, 0x0000 },
123 	{ 0x84, 0x0000 },
124 	{ 0x85, 0x0008 },
125 	{ 0x89, 0x0000 },
126 	{ 0x8a, 0x0000 },
127 	{ 0x8b, 0x0600 },
128 	{ 0x8c, 0x0228 },
129 	{ 0x8d, 0xa000 },
130 	{ 0x8e, 0x0004 },
131 	{ 0x8f, 0x1100 },
132 	{ 0x90, 0x0646 },
133 	{ 0x91, 0x0c00 },
134 	{ 0x92, 0x0000 },
135 	{ 0x93, 0x3000 },
136 	{ 0xb0, 0x2080 },
137 	{ 0xb1, 0x0000 },
138 	{ 0xb4, 0x2206 },
139 	{ 0xb5, 0x1f00 },
140 	{ 0xb6, 0x0000 },
141 	{ 0xb8, 0x034b },
142 	{ 0xb9, 0x0066 },
143 	{ 0xba, 0x000b },
144 	{ 0xbb, 0x0000 },
145 	{ 0xbc, 0x0000 },
146 	{ 0xbd, 0x0000 },
147 	{ 0xbe, 0x0000 },
148 	{ 0xbf, 0x0000 },
149 	{ 0xc0, 0x0400 },
150 	{ 0xc2, 0x0000 },
151 	{ 0xc4, 0x0000 },
152 	{ 0xc5, 0x0000 },
153 	{ 0xc6, 0x2000 },
154 	{ 0xc8, 0x0000 },
155 	{ 0xc9, 0x0000 },
156 	{ 0xca, 0x0000 },
157 	{ 0xcb, 0x0000 },
158 	{ 0xcc, 0x0000 },
159 	{ 0xcf, 0x0013 },
160 	{ 0xd0, 0x0680 },
161 	{ 0xd1, 0x1c17 },
162 	{ 0xd2, 0x8c00 },
163 	{ 0xd3, 0xaa20 },
164 	{ 0xd6, 0x0400 },
165 	{ 0xd9, 0x0809 },
166 	{ 0xfe, 0x10ec },
167 	{ 0xff, 0x6231 },
168 };
169 
170 static int rt5640_reset(struct snd_soc_codec *codec)
171 {
172 	return snd_soc_write(codec, RT5640_RESET, 0);
173 }
174 
175 static bool rt5640_volatile_register(struct device *dev, unsigned int reg)
176 {
177 	int i;
178 
179 	for (i = 0; i < ARRAY_SIZE(rt5640_ranges); i++)
180 		if ((reg >= rt5640_ranges[i].window_start &&
181 		     reg <= rt5640_ranges[i].window_start +
182 		     rt5640_ranges[i].window_len) ||
183 		    (reg >= rt5640_ranges[i].range_min &&
184 		     reg <= rt5640_ranges[i].range_max))
185 			return true;
186 
187 	switch (reg) {
188 	case RT5640_RESET:
189 	case RT5640_ASRC_5:
190 	case RT5640_EQ_CTRL1:
191 	case RT5640_DRC_AGC_1:
192 	case RT5640_ANC_CTRL1:
193 	case RT5640_IRQ_CTRL2:
194 	case RT5640_INT_IRQ_ST:
195 	case RT5640_DSP_CTRL2:
196 	case RT5640_DSP_CTRL3:
197 	case RT5640_PRIV_INDEX:
198 	case RT5640_PRIV_DATA:
199 	case RT5640_PGM_REG_ARR1:
200 	case RT5640_PGM_REG_ARR3:
201 	case RT5640_VENDOR_ID:
202 	case RT5640_VENDOR_ID1:
203 	case RT5640_VENDOR_ID2:
204 		return true;
205 	default:
206 		return false;
207 	}
208 }
209 
210 static bool rt5640_readable_register(struct device *dev, unsigned int reg)
211 {
212 	int i;
213 
214 	for (i = 0; i < ARRAY_SIZE(rt5640_ranges); i++)
215 		if ((reg >= rt5640_ranges[i].window_start &&
216 		     reg <= rt5640_ranges[i].window_start +
217 		     rt5640_ranges[i].window_len) ||
218 		    (reg >= rt5640_ranges[i].range_min &&
219 		     reg <= rt5640_ranges[i].range_max))
220 			return true;
221 
222 	switch (reg) {
223 	case RT5640_RESET:
224 	case RT5640_SPK_VOL:
225 	case RT5640_HP_VOL:
226 	case RT5640_OUTPUT:
227 	case RT5640_MONO_OUT:
228 	case RT5640_IN1_IN2:
229 	case RT5640_IN3_IN4:
230 	case RT5640_INL_INR_VOL:
231 	case RT5640_DAC1_DIG_VOL:
232 	case RT5640_DAC2_DIG_VOL:
233 	case RT5640_DAC2_CTRL:
234 	case RT5640_ADC_DIG_VOL:
235 	case RT5640_ADC_DATA:
236 	case RT5640_ADC_BST_VOL:
237 	case RT5640_STO_ADC_MIXER:
238 	case RT5640_MONO_ADC_MIXER:
239 	case RT5640_AD_DA_MIXER:
240 	case RT5640_STO_DAC_MIXER:
241 	case RT5640_MONO_DAC_MIXER:
242 	case RT5640_DIG_MIXER:
243 	case RT5640_DSP_PATH1:
244 	case RT5640_DSP_PATH2:
245 	case RT5640_DIG_INF_DATA:
246 	case RT5640_REC_L1_MIXER:
247 	case RT5640_REC_L2_MIXER:
248 	case RT5640_REC_R1_MIXER:
249 	case RT5640_REC_R2_MIXER:
250 	case RT5640_HPO_MIXER:
251 	case RT5640_SPK_L_MIXER:
252 	case RT5640_SPK_R_MIXER:
253 	case RT5640_SPO_L_MIXER:
254 	case RT5640_SPO_R_MIXER:
255 	case RT5640_SPO_CLSD_RATIO:
256 	case RT5640_MONO_MIXER:
257 	case RT5640_OUT_L1_MIXER:
258 	case RT5640_OUT_L2_MIXER:
259 	case RT5640_OUT_L3_MIXER:
260 	case RT5640_OUT_R1_MIXER:
261 	case RT5640_OUT_R2_MIXER:
262 	case RT5640_OUT_R3_MIXER:
263 	case RT5640_LOUT_MIXER:
264 	case RT5640_PWR_DIG1:
265 	case RT5640_PWR_DIG2:
266 	case RT5640_PWR_ANLG1:
267 	case RT5640_PWR_ANLG2:
268 	case RT5640_PWR_MIXER:
269 	case RT5640_PWR_VOL:
270 	case RT5640_PRIV_INDEX:
271 	case RT5640_PRIV_DATA:
272 	case RT5640_I2S1_SDP:
273 	case RT5640_I2S2_SDP:
274 	case RT5640_ADDA_CLK1:
275 	case RT5640_ADDA_CLK2:
276 	case RT5640_DMIC:
277 	case RT5640_GLB_CLK:
278 	case RT5640_PLL_CTRL1:
279 	case RT5640_PLL_CTRL2:
280 	case RT5640_ASRC_1:
281 	case RT5640_ASRC_2:
282 	case RT5640_ASRC_3:
283 	case RT5640_ASRC_4:
284 	case RT5640_ASRC_5:
285 	case RT5640_HP_OVCD:
286 	case RT5640_CLS_D_OVCD:
287 	case RT5640_CLS_D_OUT:
288 	case RT5640_DEPOP_M1:
289 	case RT5640_DEPOP_M2:
290 	case RT5640_DEPOP_M3:
291 	case RT5640_CHARGE_PUMP:
292 	case RT5640_PV_DET_SPK_G:
293 	case RT5640_MICBIAS:
294 	case RT5640_EQ_CTRL1:
295 	case RT5640_EQ_CTRL2:
296 	case RT5640_WIND_FILTER:
297 	case RT5640_DRC_AGC_1:
298 	case RT5640_DRC_AGC_2:
299 	case RT5640_DRC_AGC_3:
300 	case RT5640_SVOL_ZC:
301 	case RT5640_ANC_CTRL1:
302 	case RT5640_ANC_CTRL2:
303 	case RT5640_ANC_CTRL3:
304 	case RT5640_JD_CTRL:
305 	case RT5640_ANC_JD:
306 	case RT5640_IRQ_CTRL1:
307 	case RT5640_IRQ_CTRL2:
308 	case RT5640_INT_IRQ_ST:
309 	case RT5640_GPIO_CTRL1:
310 	case RT5640_GPIO_CTRL2:
311 	case RT5640_GPIO_CTRL3:
312 	case RT5640_DSP_CTRL1:
313 	case RT5640_DSP_CTRL2:
314 	case RT5640_DSP_CTRL3:
315 	case RT5640_DSP_CTRL4:
316 	case RT5640_PGM_REG_ARR1:
317 	case RT5640_PGM_REG_ARR2:
318 	case RT5640_PGM_REG_ARR3:
319 	case RT5640_PGM_REG_ARR4:
320 	case RT5640_PGM_REG_ARR5:
321 	case RT5640_SCB_FUNC:
322 	case RT5640_SCB_CTRL:
323 	case RT5640_BASE_BACK:
324 	case RT5640_MP3_PLUS1:
325 	case RT5640_MP3_PLUS2:
326 	case RT5640_3D_HP:
327 	case RT5640_ADJ_HPF:
328 	case RT5640_HP_CALIB_AMP_DET:
329 	case RT5640_HP_CALIB2:
330 	case RT5640_SV_ZCD1:
331 	case RT5640_SV_ZCD2:
332 	case RT5640_DUMMY1:
333 	case RT5640_DUMMY2:
334 	case RT5640_DUMMY3:
335 	case RT5640_VENDOR_ID:
336 	case RT5640_VENDOR_ID1:
337 	case RT5640_VENDOR_ID2:
338 		return true;
339 	default:
340 		return false;
341 	}
342 }
343 
344 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
345 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -65625, 375, 0);
346 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
347 static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -17625, 375, 0);
348 static const DECLARE_TLV_DB_SCALE(adc_bst_tlv, 0, 1200, 0);
349 
350 /* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
351 static unsigned int bst_tlv[] = {
352 	TLV_DB_RANGE_HEAD(7),
353 	0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
354 	1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
355 	2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
356 	3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
357 	6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
358 	7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
359 	8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0),
360 };
361 
362 /* Interface data select */
363 static const char * const rt5640_data_select[] = {
364 	"Normal", "left copy to right", "right copy to left", "Swap"};
365 
366 static SOC_ENUM_SINGLE_DECL(rt5640_if1_dac_enum, RT5640_DIG_INF_DATA,
367 			    RT5640_IF1_DAC_SEL_SFT, rt5640_data_select);
368 
369 static SOC_ENUM_SINGLE_DECL(rt5640_if1_adc_enum, RT5640_DIG_INF_DATA,
370 			    RT5640_IF1_ADC_SEL_SFT, rt5640_data_select);
371 
372 static SOC_ENUM_SINGLE_DECL(rt5640_if2_dac_enum, RT5640_DIG_INF_DATA,
373 			    RT5640_IF2_DAC_SEL_SFT, rt5640_data_select);
374 
375 static SOC_ENUM_SINGLE_DECL(rt5640_if2_adc_enum, RT5640_DIG_INF_DATA,
376 			    RT5640_IF2_ADC_SEL_SFT, rt5640_data_select);
377 
378 /* Class D speaker gain ratio */
379 static const char * const rt5640_clsd_spk_ratio[] = {"1.66x", "1.83x", "1.94x",
380 	"2x", "2.11x", "2.22x", "2.33x", "2.44x", "2.55x", "2.66x", "2.77x"};
381 
382 static SOC_ENUM_SINGLE_DECL(rt5640_clsd_spk_ratio_enum, RT5640_CLS_D_OUT,
383 			    RT5640_CLSD_RATIO_SFT, rt5640_clsd_spk_ratio);
384 
385 static const struct snd_kcontrol_new rt5640_snd_controls[] = {
386 	/* Speaker Output Volume */
387 	SOC_DOUBLE("Speaker Channel Switch", RT5640_SPK_VOL,
388 		RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
389 	SOC_DOUBLE_TLV("Speaker Playback Volume", RT5640_SPK_VOL,
390 		RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
391 	/* Headphone Output Volume */
392 	SOC_DOUBLE("HP Channel Switch", RT5640_HP_VOL,
393 		RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
394 	SOC_DOUBLE_TLV("HP Playback Volume", RT5640_HP_VOL,
395 		RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
396 	/* OUTPUT Control */
397 	SOC_DOUBLE("OUT Playback Switch", RT5640_OUTPUT,
398 		RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
399 	SOC_DOUBLE("OUT Channel Switch", RT5640_OUTPUT,
400 		RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
401 	SOC_DOUBLE_TLV("OUT Playback Volume", RT5640_OUTPUT,
402 		RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
403 
404 	/* DAC Digital Volume */
405 	SOC_DOUBLE("DAC2 Playback Switch", RT5640_DAC2_CTRL,
406 		RT5640_M_DAC_L2_VOL_SFT, RT5640_M_DAC_R2_VOL_SFT, 1, 1),
407 	SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5640_DAC1_DIG_VOL,
408 			RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
409 			175, 0, dac_vol_tlv),
410 	/* IN1/IN2 Control */
411 	SOC_SINGLE_TLV("IN1 Boost", RT5640_IN1_IN2,
412 		RT5640_BST_SFT1, 8, 0, bst_tlv),
413 	SOC_SINGLE_TLV("IN2 Boost", RT5640_IN3_IN4,
414 		RT5640_BST_SFT2, 8, 0, bst_tlv),
415 	/* INL/INR Volume Control */
416 	SOC_DOUBLE_TLV("IN Capture Volume", RT5640_INL_INR_VOL,
417 			RT5640_INL_VOL_SFT, RT5640_INR_VOL_SFT,
418 			31, 1, in_vol_tlv),
419 	/* ADC Digital Volume Control */
420 	SOC_DOUBLE("ADC Capture Switch", RT5640_ADC_DIG_VOL,
421 		RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
422 	SOC_DOUBLE_TLV("ADC Capture Volume", RT5640_ADC_DIG_VOL,
423 			RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
424 			127, 0, adc_vol_tlv),
425 	SOC_DOUBLE_TLV("Mono ADC Capture Volume", RT5640_ADC_DATA,
426 			RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
427 			127, 0, adc_vol_tlv),
428 	/* ADC Boost Volume Control */
429 	SOC_DOUBLE_TLV("ADC Boost Gain", RT5640_ADC_BST_VOL,
430 			RT5640_ADC_L_BST_SFT, RT5640_ADC_R_BST_SFT,
431 			3, 0, adc_bst_tlv),
432 	/* Class D speaker gain ratio */
433 	SOC_ENUM("Class D SPK Ratio Control", rt5640_clsd_spk_ratio_enum),
434 
435 	SOC_ENUM("ADC IF1 Data Switch", rt5640_if1_adc_enum),
436 	SOC_ENUM("DAC IF1 Data Switch", rt5640_if1_dac_enum),
437 	SOC_ENUM("ADC IF2 Data Switch", rt5640_if2_adc_enum),
438 	SOC_ENUM("DAC IF2 Data Switch", rt5640_if2_dac_enum),
439 };
440 
441 static const struct snd_kcontrol_new rt5640_specific_snd_controls[] = {
442 	/* MONO Output Control */
443 	SOC_SINGLE("Mono Playback Switch", RT5640_MONO_OUT, RT5640_L_MUTE_SFT,
444 		1, 1),
445 
446 	SOC_DOUBLE_TLV("Mono DAC Playback Volume", RT5640_DAC2_DIG_VOL,
447 		RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 175, 0, dac_vol_tlv),
448 };
449 
450 /**
451  * set_dmic_clk - Set parameter of dmic.
452  *
453  * @w: DAPM widget.
454  * @kcontrol: The kcontrol of this widget.
455  * @event: Event id.
456  *
457  */
458 static int set_dmic_clk(struct snd_soc_dapm_widget *w,
459 	struct snd_kcontrol *kcontrol, int event)
460 {
461 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
462 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
463 	int idx = -EINVAL;
464 
465 	idx = rl6231_calc_dmic_clk(rt5640->sysclk);
466 
467 	if (idx < 0)
468 		dev_err(codec->dev, "Failed to set DMIC clock\n");
469 	else
470 		snd_soc_update_bits(codec, RT5640_DMIC, RT5640_DMIC_CLK_MASK,
471 					idx << RT5640_DMIC_CLK_SFT);
472 	return idx;
473 }
474 
475 static int is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
476 			 struct snd_soc_dapm_widget *sink)
477 {
478 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
479 	unsigned int val;
480 
481 	val = snd_soc_read(codec, RT5640_GLB_CLK);
482 	val &= RT5640_SCLK_SRC_MASK;
483 	if (val == RT5640_SCLK_SRC_PLL1)
484 		return 1;
485 	else
486 		return 0;
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("BST2 Switch", RT5640_REC_L2_MIXER,
603 			RT5640_M_BST4_RM_L_SFT, 1, 1),
604 	SOC_DAPM_SINGLE("BST1 Switch", RT5640_REC_L2_MIXER,
605 			RT5640_M_BST1_RM_L_SFT, 1, 1),
606 	SOC_DAPM_SINGLE("OUT MIXL Switch", RT5640_REC_L2_MIXER,
607 			RT5640_M_OM_L_RM_L_SFT, 1, 1),
608 };
609 
610 static const struct snd_kcontrol_new rt5640_rec_r_mix[] = {
611 	SOC_DAPM_SINGLE("HPOR Switch", RT5640_REC_R2_MIXER,
612 			RT5640_M_HP_R_RM_R_SFT, 1, 1),
613 	SOC_DAPM_SINGLE("INR Switch", RT5640_REC_R2_MIXER,
614 			RT5640_M_IN_R_RM_R_SFT, 1, 1),
615 	SOC_DAPM_SINGLE("BST2 Switch", RT5640_REC_R2_MIXER,
616 			RT5640_M_BST4_RM_R_SFT, 1, 1),
617 	SOC_DAPM_SINGLE("BST1 Switch", RT5640_REC_R2_MIXER,
618 			RT5640_M_BST1_RM_R_SFT, 1, 1),
619 	SOC_DAPM_SINGLE("OUT MIXR Switch", RT5640_REC_R2_MIXER,
620 			RT5640_M_OM_R_RM_R_SFT, 1, 1),
621 };
622 
623 /* Analog Output Mixer */
624 static const struct snd_kcontrol_new rt5640_spk_l_mix[] = {
625 	SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_SPK_L_MIXER,
626 			RT5640_M_RM_L_SM_L_SFT, 1, 1),
627 	SOC_DAPM_SINGLE("INL Switch", RT5640_SPK_L_MIXER,
628 			RT5640_M_IN_L_SM_L_SFT, 1, 1),
629 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_SPK_L_MIXER,
630 			RT5640_M_DAC_L1_SM_L_SFT, 1, 1),
631 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_SPK_L_MIXER,
632 			RT5640_M_DAC_L2_SM_L_SFT, 1, 1),
633 	SOC_DAPM_SINGLE("OUT MIXL Switch", RT5640_SPK_L_MIXER,
634 			RT5640_M_OM_L_SM_L_SFT, 1, 1),
635 };
636 
637 static const struct snd_kcontrol_new rt5640_spk_r_mix[] = {
638 	SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_SPK_R_MIXER,
639 			RT5640_M_RM_R_SM_R_SFT, 1, 1),
640 	SOC_DAPM_SINGLE("INR Switch", RT5640_SPK_R_MIXER,
641 			RT5640_M_IN_R_SM_R_SFT, 1, 1),
642 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPK_R_MIXER,
643 			RT5640_M_DAC_R1_SM_R_SFT, 1, 1),
644 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_SPK_R_MIXER,
645 			RT5640_M_DAC_R2_SM_R_SFT, 1, 1),
646 	SOC_DAPM_SINGLE("OUT MIXR Switch", RT5640_SPK_R_MIXER,
647 			RT5640_M_OM_R_SM_R_SFT, 1, 1),
648 };
649 
650 static const struct snd_kcontrol_new rt5640_out_l_mix[] = {
651 	SOC_DAPM_SINGLE("SPK MIXL Switch", RT5640_OUT_L3_MIXER,
652 			RT5640_M_SM_L_OM_L_SFT, 1, 1),
653 	SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_L3_MIXER,
654 			RT5640_M_BST1_OM_L_SFT, 1, 1),
655 	SOC_DAPM_SINGLE("INL Switch", RT5640_OUT_L3_MIXER,
656 			RT5640_M_IN_L_OM_L_SFT, 1, 1),
657 	SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_OUT_L3_MIXER,
658 			RT5640_M_RM_L_OM_L_SFT, 1, 1),
659 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_OUT_L3_MIXER,
660 			RT5640_M_DAC_R2_OM_L_SFT, 1, 1),
661 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_OUT_L3_MIXER,
662 			RT5640_M_DAC_L2_OM_L_SFT, 1, 1),
663 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_OUT_L3_MIXER,
664 			RT5640_M_DAC_L1_OM_L_SFT, 1, 1),
665 };
666 
667 static const struct snd_kcontrol_new rt5640_out_r_mix[] = {
668 	SOC_DAPM_SINGLE("SPK MIXR Switch", RT5640_OUT_R3_MIXER,
669 			RT5640_M_SM_L_OM_R_SFT, 1, 1),
670 	SOC_DAPM_SINGLE("BST2 Switch", RT5640_OUT_R3_MIXER,
671 			RT5640_M_BST4_OM_R_SFT, 1, 1),
672 	SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_R3_MIXER,
673 			RT5640_M_BST1_OM_R_SFT, 1, 1),
674 	SOC_DAPM_SINGLE("INR Switch", RT5640_OUT_R3_MIXER,
675 			RT5640_M_IN_R_OM_R_SFT, 1, 1),
676 	SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_OUT_R3_MIXER,
677 			RT5640_M_RM_R_OM_R_SFT, 1, 1),
678 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_OUT_R3_MIXER,
679 			RT5640_M_DAC_L2_OM_R_SFT, 1, 1),
680 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_OUT_R3_MIXER,
681 			RT5640_M_DAC_R2_OM_R_SFT, 1, 1),
682 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_OUT_R3_MIXER,
683 			RT5640_M_DAC_R1_OM_R_SFT, 1, 1),
684 };
685 
686 static const struct snd_kcontrol_new rt5639_out_l_mix[] = {
687 	SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_L3_MIXER,
688 			RT5640_M_BST1_OM_L_SFT, 1, 1),
689 	SOC_DAPM_SINGLE("INL Switch", RT5640_OUT_L3_MIXER,
690 			RT5640_M_IN_L_OM_L_SFT, 1, 1),
691 	SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_OUT_L3_MIXER,
692 			RT5640_M_RM_L_OM_L_SFT, 1, 1),
693 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_OUT_L3_MIXER,
694 			RT5640_M_DAC_L1_OM_L_SFT, 1, 1),
695 };
696 
697 static const struct snd_kcontrol_new rt5639_out_r_mix[] = {
698 	SOC_DAPM_SINGLE("BST2 Switch", RT5640_OUT_R3_MIXER,
699 			RT5640_M_BST4_OM_R_SFT, 1, 1),
700 	SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_R3_MIXER,
701 			RT5640_M_BST1_OM_R_SFT, 1, 1),
702 	SOC_DAPM_SINGLE("INR Switch", RT5640_OUT_R3_MIXER,
703 			RT5640_M_IN_R_OM_R_SFT, 1, 1),
704 	SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_OUT_R3_MIXER,
705 			RT5640_M_RM_R_OM_R_SFT, 1, 1),
706 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_OUT_R3_MIXER,
707 			RT5640_M_DAC_R1_OM_R_SFT, 1, 1),
708 };
709 
710 static const struct snd_kcontrol_new rt5640_spo_l_mix[] = {
711 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPO_L_MIXER,
712 			RT5640_M_DAC_R1_SPM_L_SFT, 1, 1),
713 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_SPO_L_MIXER,
714 			RT5640_M_DAC_L1_SPM_L_SFT, 1, 1),
715 	SOC_DAPM_SINGLE("SPKVOL R Switch", RT5640_SPO_L_MIXER,
716 			RT5640_M_SV_R_SPM_L_SFT, 1, 1),
717 	SOC_DAPM_SINGLE("SPKVOL L Switch", RT5640_SPO_L_MIXER,
718 			RT5640_M_SV_L_SPM_L_SFT, 1, 1),
719 	SOC_DAPM_SINGLE("BST1 Switch", RT5640_SPO_L_MIXER,
720 			RT5640_M_BST1_SPM_L_SFT, 1, 1),
721 };
722 
723 static const struct snd_kcontrol_new rt5640_spo_r_mix[] = {
724 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPO_R_MIXER,
725 			RT5640_M_DAC_R1_SPM_R_SFT, 1, 1),
726 	SOC_DAPM_SINGLE("SPKVOL R Switch", RT5640_SPO_R_MIXER,
727 			RT5640_M_SV_R_SPM_R_SFT, 1, 1),
728 	SOC_DAPM_SINGLE("BST1 Switch", RT5640_SPO_R_MIXER,
729 			RT5640_M_BST1_SPM_R_SFT, 1, 1),
730 };
731 
732 static const struct snd_kcontrol_new rt5640_hpo_mix[] = {
733 	SOC_DAPM_SINGLE("HPO MIX DAC2 Switch", RT5640_HPO_MIXER,
734 			RT5640_M_DAC2_HM_SFT, 1, 1),
735 	SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5640_HPO_MIXER,
736 			RT5640_M_DAC1_HM_SFT, 1, 1),
737 	SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5640_HPO_MIXER,
738 			RT5640_M_HPVOL_HM_SFT, 1, 1),
739 };
740 
741 static const struct snd_kcontrol_new rt5639_hpo_mix[] = {
742 	SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5640_HPO_MIXER,
743 			RT5640_M_DAC1_HM_SFT, 1, 1),
744 	SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5640_HPO_MIXER,
745 			RT5640_M_HPVOL_HM_SFT, 1, 1),
746 };
747 
748 static const struct snd_kcontrol_new rt5640_lout_mix[] = {
749 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_LOUT_MIXER,
750 			RT5640_M_DAC_L1_LM_SFT, 1, 1),
751 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_LOUT_MIXER,
752 			RT5640_M_DAC_R1_LM_SFT, 1, 1),
753 	SOC_DAPM_SINGLE("OUTVOL L Switch", RT5640_LOUT_MIXER,
754 			RT5640_M_OV_L_LM_SFT, 1, 1),
755 	SOC_DAPM_SINGLE("OUTVOL R Switch", RT5640_LOUT_MIXER,
756 			RT5640_M_OV_R_LM_SFT, 1, 1),
757 };
758 
759 static const struct snd_kcontrol_new rt5640_mono_mix[] = {
760 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_MIXER,
761 			RT5640_M_DAC_R2_MM_SFT, 1, 1),
762 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_MIXER,
763 			RT5640_M_DAC_L2_MM_SFT, 1, 1),
764 	SOC_DAPM_SINGLE("OUTVOL R Switch", RT5640_MONO_MIXER,
765 			RT5640_M_OV_R_MM_SFT, 1, 1),
766 	SOC_DAPM_SINGLE("OUTVOL L Switch", RT5640_MONO_MIXER,
767 			RT5640_M_OV_L_MM_SFT, 1, 1),
768 	SOC_DAPM_SINGLE("BST1 Switch", RT5640_MONO_MIXER,
769 			RT5640_M_BST1_MM_SFT, 1, 1),
770 };
771 
772 static const struct snd_kcontrol_new spk_l_enable_control =
773 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_SPK_VOL,
774 		RT5640_L_MUTE_SFT, 1, 1);
775 
776 static const struct snd_kcontrol_new spk_r_enable_control =
777 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_SPK_VOL,
778 		RT5640_R_MUTE_SFT, 1, 1);
779 
780 static const struct snd_kcontrol_new hp_l_enable_control =
781 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_HP_VOL,
782 		RT5640_L_MUTE_SFT, 1, 1);
783 
784 static const struct snd_kcontrol_new hp_r_enable_control =
785 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_HP_VOL,
786 		RT5640_R_MUTE_SFT, 1, 1);
787 
788 /* Stereo ADC source */
789 static const char * const rt5640_stereo_adc1_src[] = {
790 	"DIG MIX", "ADC"
791 };
792 
793 static SOC_ENUM_SINGLE_DECL(rt5640_stereo_adc1_enum, RT5640_STO_ADC_MIXER,
794 			    RT5640_ADC_1_SRC_SFT, rt5640_stereo_adc1_src);
795 
796 static const struct snd_kcontrol_new rt5640_sto_adc_1_mux =
797 	SOC_DAPM_ENUM("Stereo ADC1 Mux", rt5640_stereo_adc1_enum);
798 
799 static const char * const rt5640_stereo_adc2_src[] = {
800 	"DMIC1", "DMIC2", "DIG MIX"
801 };
802 
803 static SOC_ENUM_SINGLE_DECL(rt5640_stereo_adc2_enum, RT5640_STO_ADC_MIXER,
804 			    RT5640_ADC_2_SRC_SFT, rt5640_stereo_adc2_src);
805 
806 static const struct snd_kcontrol_new rt5640_sto_adc_2_mux =
807 	SOC_DAPM_ENUM("Stereo ADC2 Mux", rt5640_stereo_adc2_enum);
808 
809 /* Mono ADC source */
810 static const char * const rt5640_mono_adc_l1_src[] = {
811 	"Mono DAC MIXL", "ADCL"
812 };
813 
814 static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_l1_enum, RT5640_MONO_ADC_MIXER,
815 			    RT5640_MONO_ADC_L1_SRC_SFT, rt5640_mono_adc_l1_src);
816 
817 static const struct snd_kcontrol_new rt5640_mono_adc_l1_mux =
818 	SOC_DAPM_ENUM("Mono ADC1 left source", rt5640_mono_adc_l1_enum);
819 
820 static const char * const rt5640_mono_adc_l2_src[] = {
821 	"DMIC L1", "DMIC L2", "Mono DAC MIXL"
822 };
823 
824 static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_l2_enum, RT5640_MONO_ADC_MIXER,
825 			    RT5640_MONO_ADC_L2_SRC_SFT, rt5640_mono_adc_l2_src);
826 
827 static const struct snd_kcontrol_new rt5640_mono_adc_l2_mux =
828 	SOC_DAPM_ENUM("Mono ADC2 left source", rt5640_mono_adc_l2_enum);
829 
830 static const char * const rt5640_mono_adc_r1_src[] = {
831 	"Mono DAC MIXR", "ADCR"
832 };
833 
834 static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_r1_enum, RT5640_MONO_ADC_MIXER,
835 			    RT5640_MONO_ADC_R1_SRC_SFT, rt5640_mono_adc_r1_src);
836 
837 static const struct snd_kcontrol_new rt5640_mono_adc_r1_mux =
838 	SOC_DAPM_ENUM("Mono ADC1 right source", rt5640_mono_adc_r1_enum);
839 
840 static const char * const rt5640_mono_adc_r2_src[] = {
841 	"DMIC R1", "DMIC R2", "Mono DAC MIXR"
842 };
843 
844 static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_r2_enum, RT5640_MONO_ADC_MIXER,
845 			    RT5640_MONO_ADC_R2_SRC_SFT, rt5640_mono_adc_r2_src);
846 
847 static const struct snd_kcontrol_new rt5640_mono_adc_r2_mux =
848 	SOC_DAPM_ENUM("Mono ADC2 right source", rt5640_mono_adc_r2_enum);
849 
850 /* DAC2 channel source */
851 static const char * const rt5640_dac_l2_src[] = {
852 	"IF2", "Base L/R"
853 };
854 
855 static int rt5640_dac_l2_values[] = {
856 	0,
857 	3,
858 };
859 
860 static SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dac_l2_enum,
861 				  RT5640_DSP_PATH2, RT5640_DAC_L2_SEL_SFT,
862 				  0x3, rt5640_dac_l2_src, rt5640_dac_l2_values);
863 
864 static const struct snd_kcontrol_new rt5640_dac_l2_mux =
865 	SOC_DAPM_ENUM("DAC2 left channel source", rt5640_dac_l2_enum);
866 
867 static const char * const rt5640_dac_r2_src[] = {
868 	"IF2",
869 };
870 
871 static int rt5640_dac_r2_values[] = {
872 	0,
873 };
874 
875 static SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dac_r2_enum,
876 				  RT5640_DSP_PATH2, RT5640_DAC_R2_SEL_SFT,
877 				  0x3, rt5640_dac_r2_src, rt5640_dac_r2_values);
878 
879 static const struct snd_kcontrol_new rt5640_dac_r2_mux =
880 	SOC_DAPM_ENUM("DAC2 right channel source", rt5640_dac_r2_enum);
881 
882 /* digital interface and iis interface map */
883 static const char * const rt5640_dai_iis_map[] = {
884 	"1:1|2:2", "1:2|2:1", "1:1|2:1", "1:2|2:2"
885 };
886 
887 static int rt5640_dai_iis_map_values[] = {
888 	0,
889 	5,
890 	6,
891 	7,
892 };
893 
894 static SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dai_iis_map_enum,
895 				  RT5640_I2S1_SDP, RT5640_I2S_IF_SFT,
896 				  0x7, rt5640_dai_iis_map,
897 				  rt5640_dai_iis_map_values);
898 
899 static const struct snd_kcontrol_new rt5640_dai_mux =
900 	SOC_DAPM_ENUM("DAI select", rt5640_dai_iis_map_enum);
901 
902 /* SDI select */
903 static const char * const rt5640_sdi_sel[] = {
904 	"IF1", "IF2"
905 };
906 
907 static SOC_ENUM_SINGLE_DECL(rt5640_sdi_sel_enum, RT5640_I2S2_SDP,
908 			    RT5640_I2S2_SDI_SFT, rt5640_sdi_sel);
909 
910 static const struct snd_kcontrol_new rt5640_sdi_mux =
911 	SOC_DAPM_ENUM("SDI select", rt5640_sdi_sel_enum);
912 
913 static void hp_amp_power_on(struct snd_soc_codec *codec)
914 {
915 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
916 
917 	/* depop parameters */
918 	regmap_update_bits(rt5640->regmap, RT5640_PR_BASE +
919 		RT5640_CHPUMP_INT_REG1, 0x0700, 0x0200);
920 	regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M2,
921 		RT5640_DEPOP_MASK, RT5640_DEPOP_MAN);
922 	regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M1,
923 		RT5640_HP_CP_MASK | RT5640_HP_SG_MASK | RT5640_HP_CB_MASK,
924 		RT5640_HP_CP_PU | RT5640_HP_SG_DIS | RT5640_HP_CB_PU);
925 	regmap_write(rt5640->regmap, RT5640_PR_BASE + RT5640_HP_DCC_INT1,
926 			   0x9f00);
927 	/* headphone amp power on */
928 	regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1,
929 		RT5640_PWR_FV1 | RT5640_PWR_FV2, 0);
930 	regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1,
931 		RT5640_PWR_HA,
932 		RT5640_PWR_HA);
933 	usleep_range(10000, 15000);
934 	regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1,
935 		RT5640_PWR_FV1 | RT5640_PWR_FV2 ,
936 		RT5640_PWR_FV1 | RT5640_PWR_FV2);
937 }
938 
939 static void rt5640_pmu_depop(struct snd_soc_codec *codec)
940 {
941 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
942 
943 	regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M2,
944 		RT5640_DEPOP_MASK | RT5640_DIG_DP_MASK,
945 		RT5640_DEPOP_AUTO | RT5640_DIG_DP_EN);
946 	regmap_update_bits(rt5640->regmap, RT5640_CHARGE_PUMP,
947 		RT5640_PM_HP_MASK, RT5640_PM_HP_HV);
948 
949 	regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M3,
950 		RT5640_CP_FQ1_MASK | RT5640_CP_FQ2_MASK | RT5640_CP_FQ3_MASK,
951 		(RT5640_CP_FQ_192_KHZ << RT5640_CP_FQ1_SFT) |
952 		(RT5640_CP_FQ_12_KHZ << RT5640_CP_FQ2_SFT) |
953 		(RT5640_CP_FQ_192_KHZ << RT5640_CP_FQ3_SFT));
954 
955 	regmap_write(rt5640->regmap, RT5640_PR_BASE +
956 		RT5640_MAMP_INT_REG2, 0x1c00);
957 	regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M1,
958 		RT5640_HP_CP_MASK | RT5640_HP_SG_MASK,
959 		RT5640_HP_CP_PD | RT5640_HP_SG_EN);
960 	regmap_update_bits(rt5640->regmap, RT5640_PR_BASE +
961 		RT5640_CHPUMP_INT_REG1, 0x0700, 0x0400);
962 }
963 
964 static int rt5640_hp_event(struct snd_soc_dapm_widget *w,
965 			   struct snd_kcontrol *kcontrol, int event)
966 {
967 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
968 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
969 
970 	switch (event) {
971 	case SND_SOC_DAPM_POST_PMU:
972 		rt5640_pmu_depop(codec);
973 		rt5640->hp_mute = 0;
974 		break;
975 
976 	case SND_SOC_DAPM_PRE_PMD:
977 		rt5640->hp_mute = 1;
978 		usleep_range(70000, 75000);
979 		break;
980 
981 	default:
982 		return 0;
983 	}
984 
985 	return 0;
986 }
987 
988 static int rt5640_hp_power_event(struct snd_soc_dapm_widget *w,
989 			   struct snd_kcontrol *kcontrol, int event)
990 {
991 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
992 
993 	switch (event) {
994 	case SND_SOC_DAPM_POST_PMU:
995 		hp_amp_power_on(codec);
996 		break;
997 	default:
998 		return 0;
999 	}
1000 
1001 	return 0;
1002 }
1003 
1004 static int rt5640_hp_post_event(struct snd_soc_dapm_widget *w,
1005 			   struct snd_kcontrol *kcontrol, int event)
1006 {
1007 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
1008 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
1009 
1010 	switch (event) {
1011 	case SND_SOC_DAPM_POST_PMU:
1012 		if (!rt5640->hp_mute)
1013 			usleep_range(80000, 85000);
1014 
1015 		break;
1016 
1017 	default:
1018 		return 0;
1019 	}
1020 
1021 	return 0;
1022 }
1023 
1024 static const struct snd_soc_dapm_widget rt5640_dapm_widgets[] = {
1025 	SND_SOC_DAPM_SUPPLY("PLL1", RT5640_PWR_ANLG2,
1026 			RT5640_PWR_PLL_BIT, 0, NULL, 0),
1027 	/* Input Side */
1028 	/* micbias */
1029 	SND_SOC_DAPM_SUPPLY("LDO2", RT5640_PWR_ANLG1,
1030 			RT5640_PWR_LDO2_BIT, 0, NULL, 0),
1031 	SND_SOC_DAPM_SUPPLY("MICBIAS1", RT5640_PWR_ANLG2,
1032 			RT5640_PWR_MB1_BIT, 0, NULL, 0),
1033 	/* Input Lines */
1034 	SND_SOC_DAPM_INPUT("DMIC1"),
1035 	SND_SOC_DAPM_INPUT("DMIC2"),
1036 	SND_SOC_DAPM_INPUT("IN1P"),
1037 	SND_SOC_DAPM_INPUT("IN1N"),
1038 	SND_SOC_DAPM_INPUT("IN2P"),
1039 	SND_SOC_DAPM_INPUT("IN2N"),
1040 	SND_SOC_DAPM_PGA("DMIC L1", SND_SOC_NOPM, 0, 0, NULL, 0),
1041 	SND_SOC_DAPM_PGA("DMIC R1", SND_SOC_NOPM, 0, 0, NULL, 0),
1042 	SND_SOC_DAPM_PGA("DMIC L2", SND_SOC_NOPM, 0, 0, NULL, 0),
1043 	SND_SOC_DAPM_PGA("DMIC R2", SND_SOC_NOPM, 0, 0, NULL, 0),
1044 
1045 	SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM, 0, 0,
1046 		set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
1047 	SND_SOC_DAPM_SUPPLY("DMIC1 Power", RT5640_DMIC, RT5640_DMIC_1_EN_SFT, 0,
1048 		NULL, 0),
1049 	SND_SOC_DAPM_SUPPLY("DMIC2 Power", RT5640_DMIC, RT5640_DMIC_2_EN_SFT, 0,
1050 		NULL, 0),
1051 	/* Boost */
1052 	SND_SOC_DAPM_PGA("BST1", RT5640_PWR_ANLG2,
1053 		RT5640_PWR_BST1_BIT, 0, NULL, 0),
1054 	SND_SOC_DAPM_PGA("BST2", RT5640_PWR_ANLG2,
1055 		RT5640_PWR_BST4_BIT, 0, NULL, 0),
1056 	/* Input Volume */
1057 	SND_SOC_DAPM_PGA("INL VOL", RT5640_PWR_VOL,
1058 		RT5640_PWR_IN_L_BIT, 0, NULL, 0),
1059 	SND_SOC_DAPM_PGA("INR VOL", RT5640_PWR_VOL,
1060 		RT5640_PWR_IN_R_BIT, 0, NULL, 0),
1061 	/* REC Mixer */
1062 	SND_SOC_DAPM_MIXER("RECMIXL", RT5640_PWR_MIXER, RT5640_PWR_RM_L_BIT, 0,
1063 			rt5640_rec_l_mix, ARRAY_SIZE(rt5640_rec_l_mix)),
1064 	SND_SOC_DAPM_MIXER("RECMIXR", RT5640_PWR_MIXER, RT5640_PWR_RM_R_BIT, 0,
1065 			rt5640_rec_r_mix, ARRAY_SIZE(rt5640_rec_r_mix)),
1066 	/* ADCs */
1067 	SND_SOC_DAPM_ADC("ADC L", NULL, RT5640_PWR_DIG1,
1068 			RT5640_PWR_ADC_L_BIT, 0),
1069 	SND_SOC_DAPM_ADC("ADC R", NULL, RT5640_PWR_DIG1,
1070 			RT5640_PWR_ADC_R_BIT, 0),
1071 	/* ADC Mux */
1072 	SND_SOC_DAPM_MUX("Stereo ADC L2 Mux", SND_SOC_NOPM, 0, 0,
1073 				&rt5640_sto_adc_2_mux),
1074 	SND_SOC_DAPM_MUX("Stereo ADC R2 Mux", SND_SOC_NOPM, 0, 0,
1075 				&rt5640_sto_adc_2_mux),
1076 	SND_SOC_DAPM_MUX("Stereo ADC L1 Mux", SND_SOC_NOPM, 0, 0,
1077 				&rt5640_sto_adc_1_mux),
1078 	SND_SOC_DAPM_MUX("Stereo ADC R1 Mux", SND_SOC_NOPM, 0, 0,
1079 				&rt5640_sto_adc_1_mux),
1080 	SND_SOC_DAPM_MUX("Mono ADC L2 Mux", SND_SOC_NOPM, 0, 0,
1081 				&rt5640_mono_adc_l2_mux),
1082 	SND_SOC_DAPM_MUX("Mono ADC L1 Mux", SND_SOC_NOPM, 0, 0,
1083 				&rt5640_mono_adc_l1_mux),
1084 	SND_SOC_DAPM_MUX("Mono ADC R1 Mux", SND_SOC_NOPM, 0, 0,
1085 				&rt5640_mono_adc_r1_mux),
1086 	SND_SOC_DAPM_MUX("Mono ADC R2 Mux", SND_SOC_NOPM, 0, 0,
1087 				&rt5640_mono_adc_r2_mux),
1088 	/* ADC Mixer */
1089 	SND_SOC_DAPM_SUPPLY("Stereo Filter", RT5640_PWR_DIG2,
1090 		RT5640_PWR_ADC_SF_BIT, 0, NULL, 0),
1091 	SND_SOC_DAPM_MIXER("Stereo ADC MIXL", SND_SOC_NOPM, 0, 0,
1092 		rt5640_sto_adc_l_mix, ARRAY_SIZE(rt5640_sto_adc_l_mix)),
1093 	SND_SOC_DAPM_MIXER("Stereo ADC MIXR", SND_SOC_NOPM, 0, 0,
1094 		rt5640_sto_adc_r_mix, ARRAY_SIZE(rt5640_sto_adc_r_mix)),
1095 	SND_SOC_DAPM_SUPPLY("Mono Left Filter", RT5640_PWR_DIG2,
1096 		RT5640_PWR_ADC_MF_L_BIT, 0, NULL, 0),
1097 	SND_SOC_DAPM_MIXER("Mono ADC MIXL", SND_SOC_NOPM, 0, 0,
1098 		rt5640_mono_adc_l_mix, ARRAY_SIZE(rt5640_mono_adc_l_mix)),
1099 	SND_SOC_DAPM_SUPPLY("Mono Right Filter", RT5640_PWR_DIG2,
1100 		RT5640_PWR_ADC_MF_R_BIT, 0, NULL, 0),
1101 	SND_SOC_DAPM_MIXER("Mono ADC MIXR", SND_SOC_NOPM, 0, 0,
1102 		rt5640_mono_adc_r_mix, ARRAY_SIZE(rt5640_mono_adc_r_mix)),
1103 
1104 	/* Digital Interface */
1105 	SND_SOC_DAPM_SUPPLY("I2S1", RT5640_PWR_DIG1,
1106 		RT5640_PWR_I2S1_BIT, 0, NULL, 0),
1107 	SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
1108 	SND_SOC_DAPM_PGA("IF1 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1109 	SND_SOC_DAPM_PGA("IF1 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1110 	SND_SOC_DAPM_PGA("IF1 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
1111 	SND_SOC_DAPM_PGA("IF1 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1112 	SND_SOC_DAPM_PGA("IF1 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1113 	SND_SOC_DAPM_SUPPLY("I2S2", RT5640_PWR_DIG1,
1114 		RT5640_PWR_I2S2_BIT, 0, NULL, 0),
1115 	SND_SOC_DAPM_PGA("IF2 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
1116 	SND_SOC_DAPM_PGA("IF2 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1117 	SND_SOC_DAPM_PGA("IF2 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1118 	SND_SOC_DAPM_PGA("IF2 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
1119 	SND_SOC_DAPM_PGA("IF2 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1120 	SND_SOC_DAPM_PGA("IF2 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1121 	/* Digital Interface Select */
1122 	SND_SOC_DAPM_MUX("DAI1 RX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1123 	SND_SOC_DAPM_MUX("DAI1 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1124 	SND_SOC_DAPM_MUX("DAI1 IF1 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1125 	SND_SOC_DAPM_MUX("DAI1 IF2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1126 	SND_SOC_DAPM_MUX("SDI1 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_sdi_mux),
1127 	SND_SOC_DAPM_MUX("DAI2 RX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1128 	SND_SOC_DAPM_MUX("DAI2 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1129 	SND_SOC_DAPM_MUX("DAI2 IF1 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1130 	SND_SOC_DAPM_MUX("DAI2 IF2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1131 	SND_SOC_DAPM_MUX("SDI2 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_sdi_mux),
1132 	/* Audio Interface */
1133 	SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1134 	SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
1135 	SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
1136 	SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
1137 
1138 	/* Output Side */
1139 	/* DAC mixer before sound effect  */
1140 	SND_SOC_DAPM_MIXER("DAC MIXL", SND_SOC_NOPM, 0, 0,
1141 		rt5640_dac_l_mix, ARRAY_SIZE(rt5640_dac_l_mix)),
1142 	SND_SOC_DAPM_MIXER("DAC MIXR", SND_SOC_NOPM, 0, 0,
1143 		rt5640_dac_r_mix, ARRAY_SIZE(rt5640_dac_r_mix)),
1144 
1145 	/* DAC Mixer */
1146 	SND_SOC_DAPM_MIXER("Mono DAC MIXL", SND_SOC_NOPM, 0, 0,
1147 		rt5640_mono_dac_l_mix, ARRAY_SIZE(rt5640_mono_dac_l_mix)),
1148 	SND_SOC_DAPM_MIXER("Mono DAC MIXR", SND_SOC_NOPM, 0, 0,
1149 		rt5640_mono_dac_r_mix, ARRAY_SIZE(rt5640_mono_dac_r_mix)),
1150 	SND_SOC_DAPM_MIXER("DIG MIXL", SND_SOC_NOPM, 0, 0,
1151 		rt5640_dig_l_mix, ARRAY_SIZE(rt5640_dig_l_mix)),
1152 	SND_SOC_DAPM_MIXER("DIG MIXR", SND_SOC_NOPM, 0, 0,
1153 		rt5640_dig_r_mix, ARRAY_SIZE(rt5640_dig_r_mix)),
1154 	/* DACs */
1155 	SND_SOC_DAPM_DAC("DAC L1", NULL, RT5640_PWR_DIG1,
1156 			RT5640_PWR_DAC_L1_BIT, 0),
1157 	SND_SOC_DAPM_DAC("DAC R1", NULL, RT5640_PWR_DIG1,
1158 			RT5640_PWR_DAC_R1_BIT, 0),
1159 
1160 	/* SPK/OUT Mixer */
1161 	SND_SOC_DAPM_MIXER("SPK MIXL", RT5640_PWR_MIXER, RT5640_PWR_SM_L_BIT,
1162 		0, rt5640_spk_l_mix, ARRAY_SIZE(rt5640_spk_l_mix)),
1163 	SND_SOC_DAPM_MIXER("SPK MIXR", RT5640_PWR_MIXER, RT5640_PWR_SM_R_BIT,
1164 		0, rt5640_spk_r_mix, ARRAY_SIZE(rt5640_spk_r_mix)),
1165 	/* Ouput Volume */
1166 	SND_SOC_DAPM_PGA("SPKVOL L", RT5640_PWR_VOL,
1167 		RT5640_PWR_SV_L_BIT, 0, NULL, 0),
1168 	SND_SOC_DAPM_PGA("SPKVOL R", RT5640_PWR_VOL,
1169 		RT5640_PWR_SV_R_BIT, 0, NULL, 0),
1170 	SND_SOC_DAPM_PGA("OUTVOL L", RT5640_PWR_VOL,
1171 		RT5640_PWR_OV_L_BIT, 0, NULL, 0),
1172 	SND_SOC_DAPM_PGA("OUTVOL R", RT5640_PWR_VOL,
1173 		RT5640_PWR_OV_R_BIT, 0, NULL, 0),
1174 	SND_SOC_DAPM_PGA("HPOVOL L", RT5640_PWR_VOL,
1175 		RT5640_PWR_HV_L_BIT, 0, NULL, 0),
1176 	SND_SOC_DAPM_PGA("HPOVOL R", RT5640_PWR_VOL,
1177 		RT5640_PWR_HV_R_BIT, 0, NULL, 0),
1178 	/* SPO/HPO/LOUT/Mono Mixer */
1179 	SND_SOC_DAPM_MIXER("SPOL MIX", SND_SOC_NOPM, 0,
1180 		0, rt5640_spo_l_mix, ARRAY_SIZE(rt5640_spo_l_mix)),
1181 	SND_SOC_DAPM_MIXER("SPOR MIX", SND_SOC_NOPM, 0,
1182 		0, rt5640_spo_r_mix, ARRAY_SIZE(rt5640_spo_r_mix)),
1183 	SND_SOC_DAPM_MIXER("LOUT MIX", RT5640_PWR_ANLG1, RT5640_PWR_LM_BIT, 0,
1184 		rt5640_lout_mix, ARRAY_SIZE(rt5640_lout_mix)),
1185 	SND_SOC_DAPM_SUPPLY_S("Improve HP Amp Drv", 1, SND_SOC_NOPM,
1186 		0, 0, rt5640_hp_power_event, SND_SOC_DAPM_POST_PMU),
1187 	SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM, 0, 0,
1188 		rt5640_hp_event,
1189 		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
1190 	SND_SOC_DAPM_SUPPLY("HP L Amp", RT5640_PWR_ANLG1,
1191 		RT5640_PWR_HP_L_BIT, 0, NULL, 0),
1192 	SND_SOC_DAPM_SUPPLY("HP R Amp", RT5640_PWR_ANLG1,
1193 		RT5640_PWR_HP_R_BIT, 0, NULL, 0),
1194 	SND_SOC_DAPM_SUPPLY("Improve SPK Amp Drv", RT5640_PWR_DIG1,
1195 		RT5640_PWR_CLS_D_BIT, 0, NULL, 0),
1196 
1197 	/* Output Switch */
1198 	SND_SOC_DAPM_SWITCH("Speaker L Playback", SND_SOC_NOPM, 0, 0,
1199 			&spk_l_enable_control),
1200 	SND_SOC_DAPM_SWITCH("Speaker R Playback", SND_SOC_NOPM, 0, 0,
1201 			&spk_r_enable_control),
1202 	SND_SOC_DAPM_SWITCH("HP L Playback", SND_SOC_NOPM, 0, 0,
1203 			&hp_l_enable_control),
1204 	SND_SOC_DAPM_SWITCH("HP R Playback", SND_SOC_NOPM, 0, 0,
1205 			&hp_r_enable_control),
1206 	SND_SOC_DAPM_POST("HP Post", rt5640_hp_post_event),
1207 	/* Output Lines */
1208 	SND_SOC_DAPM_OUTPUT("SPOLP"),
1209 	SND_SOC_DAPM_OUTPUT("SPOLN"),
1210 	SND_SOC_DAPM_OUTPUT("SPORP"),
1211 	SND_SOC_DAPM_OUTPUT("SPORN"),
1212 	SND_SOC_DAPM_OUTPUT("HPOL"),
1213 	SND_SOC_DAPM_OUTPUT("HPOR"),
1214 	SND_SOC_DAPM_OUTPUT("LOUTL"),
1215 	SND_SOC_DAPM_OUTPUT("LOUTR"),
1216 };
1217 
1218 static const struct snd_soc_dapm_widget rt5640_specific_dapm_widgets[] = {
1219 	/* Audio DSP */
1220 	SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM, 0, 0, NULL, 0),
1221 	/* ANC */
1222 	SND_SOC_DAPM_PGA("ANC", SND_SOC_NOPM, 0, 0, NULL, 0),
1223 
1224 	/* DAC2 channel Mux */
1225 	SND_SOC_DAPM_MUX("DAC L2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dac_l2_mux),
1226 	SND_SOC_DAPM_MUX("DAC R2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dac_r2_mux),
1227 
1228 	SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
1229 		rt5640_sto_dac_l_mix, ARRAY_SIZE(rt5640_sto_dac_l_mix)),
1230 	SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
1231 		rt5640_sto_dac_r_mix, ARRAY_SIZE(rt5640_sto_dac_r_mix)),
1232 
1233 	SND_SOC_DAPM_DAC("DAC R2", NULL, RT5640_PWR_DIG1, RT5640_PWR_DAC_R2_BIT,
1234 		0),
1235 	SND_SOC_DAPM_DAC("DAC L2", NULL, RT5640_PWR_DIG1, RT5640_PWR_DAC_L2_BIT,
1236 		0),
1237 
1238 	SND_SOC_DAPM_MIXER("OUT MIXL", RT5640_PWR_MIXER, RT5640_PWR_OM_L_BIT,
1239 		0, rt5640_out_l_mix, ARRAY_SIZE(rt5640_out_l_mix)),
1240 	SND_SOC_DAPM_MIXER("OUT MIXR", RT5640_PWR_MIXER, RT5640_PWR_OM_R_BIT,
1241 		0, rt5640_out_r_mix, ARRAY_SIZE(rt5640_out_r_mix)),
1242 
1243 	SND_SOC_DAPM_MIXER("HPO MIX L", SND_SOC_NOPM, 0, 0,
1244 		rt5640_hpo_mix, ARRAY_SIZE(rt5640_hpo_mix)),
1245 	SND_SOC_DAPM_MIXER("HPO MIX R", SND_SOC_NOPM, 0, 0,
1246 		rt5640_hpo_mix, ARRAY_SIZE(rt5640_hpo_mix)),
1247 
1248 	SND_SOC_DAPM_MIXER("Mono MIX", RT5640_PWR_ANLG1, RT5640_PWR_MM_BIT, 0,
1249 		rt5640_mono_mix, ARRAY_SIZE(rt5640_mono_mix)),
1250 	SND_SOC_DAPM_SUPPLY("Improve MONO Amp Drv", RT5640_PWR_ANLG1,
1251 		RT5640_PWR_MA_BIT, 0, NULL, 0),
1252 
1253 	SND_SOC_DAPM_OUTPUT("MONOP"),
1254 	SND_SOC_DAPM_OUTPUT("MONON"),
1255 };
1256 
1257 static const struct snd_soc_dapm_widget rt5639_specific_dapm_widgets[] = {
1258 	SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
1259 		rt5639_sto_dac_l_mix, ARRAY_SIZE(rt5639_sto_dac_l_mix)),
1260 	SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
1261 		rt5639_sto_dac_r_mix, ARRAY_SIZE(rt5639_sto_dac_r_mix)),
1262 
1263 	SND_SOC_DAPM_SUPPLY("DAC L2 Filter", RT5640_PWR_DIG1,
1264 		RT5640_PWR_DAC_L2_BIT, 0, NULL, 0),
1265 	SND_SOC_DAPM_SUPPLY("DAC R2 Filter", RT5640_PWR_DIG1,
1266 		RT5640_PWR_DAC_R2_BIT, 0, NULL, 0),
1267 
1268 	SND_SOC_DAPM_MIXER("OUT MIXL", RT5640_PWR_MIXER, RT5640_PWR_OM_L_BIT,
1269 		0, rt5639_out_l_mix, ARRAY_SIZE(rt5639_out_l_mix)),
1270 	SND_SOC_DAPM_MIXER("OUT MIXR", RT5640_PWR_MIXER, RT5640_PWR_OM_R_BIT,
1271 		0, rt5639_out_r_mix, ARRAY_SIZE(rt5639_out_r_mix)),
1272 
1273 	SND_SOC_DAPM_MIXER("HPO MIX L", SND_SOC_NOPM, 0, 0,
1274 		rt5639_hpo_mix, ARRAY_SIZE(rt5639_hpo_mix)),
1275 	SND_SOC_DAPM_MIXER("HPO MIX R", SND_SOC_NOPM, 0, 0,
1276 		rt5639_hpo_mix, ARRAY_SIZE(rt5639_hpo_mix)),
1277 };
1278 
1279 static const struct snd_soc_dapm_route rt5640_dapm_routes[] = {
1280 	{"IN1P", NULL, "LDO2"},
1281 	{"IN2P", NULL, "LDO2"},
1282 
1283 	{"DMIC L1", NULL, "DMIC1"},
1284 	{"DMIC R1", NULL, "DMIC1"},
1285 	{"DMIC L2", NULL, "DMIC2"},
1286 	{"DMIC R2", NULL, "DMIC2"},
1287 
1288 	{"BST1", NULL, "IN1P"},
1289 	{"BST1", NULL, "IN1N"},
1290 	{"BST2", NULL, "IN2P"},
1291 	{"BST2", NULL, "IN2N"},
1292 
1293 	{"INL VOL", NULL, "IN2P"},
1294 	{"INR VOL", NULL, "IN2N"},
1295 
1296 	{"RECMIXL", "HPOL Switch", "HPOL"},
1297 	{"RECMIXL", "INL Switch", "INL VOL"},
1298 	{"RECMIXL", "BST2 Switch", "BST2"},
1299 	{"RECMIXL", "BST1 Switch", "BST1"},
1300 	{"RECMIXL", "OUT MIXL Switch", "OUT MIXL"},
1301 
1302 	{"RECMIXR", "HPOR Switch", "HPOR"},
1303 	{"RECMIXR", "INR Switch", "INR VOL"},
1304 	{"RECMIXR", "BST2 Switch", "BST2"},
1305 	{"RECMIXR", "BST1 Switch", "BST1"},
1306 	{"RECMIXR", "OUT MIXR Switch", "OUT MIXR"},
1307 
1308 	{"ADC L", NULL, "RECMIXL"},
1309 	{"ADC R", NULL, "RECMIXR"},
1310 
1311 	{"DMIC L1", NULL, "DMIC CLK"},
1312 	{"DMIC L1", NULL, "DMIC1 Power"},
1313 	{"DMIC R1", NULL, "DMIC CLK"},
1314 	{"DMIC R1", NULL, "DMIC1 Power"},
1315 	{"DMIC L2", NULL, "DMIC CLK"},
1316 	{"DMIC L2", NULL, "DMIC2 Power"},
1317 	{"DMIC R2", NULL, "DMIC CLK"},
1318 	{"DMIC R2", NULL, "DMIC2 Power"},
1319 
1320 	{"Stereo ADC L2 Mux", "DMIC1", "DMIC L1"},
1321 	{"Stereo ADC L2 Mux", "DMIC2", "DMIC L2"},
1322 	{"Stereo ADC L2 Mux", "DIG MIX", "DIG MIXL"},
1323 	{"Stereo ADC L1 Mux", "ADC", "ADC L"},
1324 	{"Stereo ADC L1 Mux", "DIG MIX", "DIG MIXL"},
1325 
1326 	{"Stereo ADC R1 Mux", "ADC", "ADC R"},
1327 	{"Stereo ADC R1 Mux", "DIG MIX", "DIG MIXR"},
1328 	{"Stereo ADC R2 Mux", "DMIC1", "DMIC R1"},
1329 	{"Stereo ADC R2 Mux", "DMIC2", "DMIC R2"},
1330 	{"Stereo ADC R2 Mux", "DIG MIX", "DIG MIXR"},
1331 
1332 	{"Mono ADC L2 Mux", "DMIC L1", "DMIC L1"},
1333 	{"Mono ADC L2 Mux", "DMIC L2", "DMIC L2"},
1334 	{"Mono ADC L2 Mux", "Mono DAC MIXL", "Mono DAC MIXL"},
1335 	{"Mono ADC L1 Mux", "Mono DAC MIXL", "Mono DAC MIXL"},
1336 	{"Mono ADC L1 Mux", "ADCL", "ADC L"},
1337 
1338 	{"Mono ADC R1 Mux", "Mono DAC MIXR", "Mono DAC MIXR"},
1339 	{"Mono ADC R1 Mux", "ADCR", "ADC R"},
1340 	{"Mono ADC R2 Mux", "DMIC R1", "DMIC R1"},
1341 	{"Mono ADC R2 Mux", "DMIC R2", "DMIC R2"},
1342 	{"Mono ADC R2 Mux", "Mono DAC MIXR", "Mono DAC MIXR"},
1343 
1344 	{"Stereo ADC MIXL", "ADC1 Switch", "Stereo ADC L1 Mux"},
1345 	{"Stereo ADC MIXL", "ADC2 Switch", "Stereo ADC L2 Mux"},
1346 	{"Stereo ADC MIXL", NULL, "Stereo Filter"},
1347 	{"Stereo Filter", NULL, "PLL1", is_sys_clk_from_pll},
1348 
1349 	{"Stereo ADC MIXR", "ADC1 Switch", "Stereo ADC R1 Mux"},
1350 	{"Stereo ADC MIXR", "ADC2 Switch", "Stereo ADC R2 Mux"},
1351 	{"Stereo ADC MIXR", NULL, "Stereo Filter"},
1352 	{"Stereo Filter", NULL, "PLL1", is_sys_clk_from_pll},
1353 
1354 	{"Mono ADC MIXL", "ADC1 Switch", "Mono ADC L1 Mux"},
1355 	{"Mono ADC MIXL", "ADC2 Switch", "Mono ADC L2 Mux"},
1356 	{"Mono ADC MIXL", NULL, "Mono Left Filter"},
1357 	{"Mono Left Filter", NULL, "PLL1", is_sys_clk_from_pll},
1358 
1359 	{"Mono ADC MIXR", "ADC1 Switch", "Mono ADC R1 Mux"},
1360 	{"Mono ADC MIXR", "ADC2 Switch", "Mono ADC R2 Mux"},
1361 	{"Mono ADC MIXR", NULL, "Mono Right Filter"},
1362 	{"Mono Right Filter", NULL, "PLL1", is_sys_clk_from_pll},
1363 
1364 	{"IF2 ADC L", NULL, "Mono ADC MIXL"},
1365 	{"IF2 ADC R", NULL, "Mono ADC MIXR"},
1366 	{"IF1 ADC L", NULL, "Stereo ADC MIXL"},
1367 	{"IF1 ADC R", NULL, "Stereo ADC MIXR"},
1368 
1369 	{"IF1 ADC", NULL, "I2S1"},
1370 	{"IF1 ADC", NULL, "IF1 ADC L"},
1371 	{"IF1 ADC", NULL, "IF1 ADC R"},
1372 	{"IF2 ADC", NULL, "I2S2"},
1373 	{"IF2 ADC", NULL, "IF2 ADC L"},
1374 	{"IF2 ADC", NULL, "IF2 ADC R"},
1375 
1376 	{"DAI1 TX Mux", "1:1|2:2", "IF1 ADC"},
1377 	{"DAI1 TX Mux", "1:2|2:1", "IF2 ADC"},
1378 	{"DAI1 IF1 Mux", "1:1|2:1", "IF1 ADC"},
1379 	{"DAI1 IF2 Mux", "1:1|2:1", "IF2 ADC"},
1380 	{"SDI1 TX Mux", "IF1", "DAI1 IF1 Mux"},
1381 	{"SDI1 TX Mux", "IF2", "DAI1 IF2 Mux"},
1382 
1383 	{"DAI2 TX Mux", "1:2|2:1", "IF1 ADC"},
1384 	{"DAI2 TX Mux", "1:1|2:2", "IF2 ADC"},
1385 	{"DAI2 IF1 Mux", "1:2|2:2", "IF1 ADC"},
1386 	{"DAI2 IF2 Mux", "1:2|2:2", "IF2 ADC"},
1387 	{"SDI2 TX Mux", "IF1", "DAI2 IF1 Mux"},
1388 	{"SDI2 TX Mux", "IF2", "DAI2 IF2 Mux"},
1389 
1390 	{"AIF1TX", NULL, "DAI1 TX Mux"},
1391 	{"AIF1TX", NULL, "SDI1 TX Mux"},
1392 	{"AIF2TX", NULL, "DAI2 TX Mux"},
1393 	{"AIF2TX", NULL, "SDI2 TX Mux"},
1394 
1395 	{"DAI1 RX Mux", "1:1|2:2", "AIF1RX"},
1396 	{"DAI1 RX Mux", "1:1|2:1", "AIF1RX"},
1397 	{"DAI1 RX Mux", "1:2|2:1", "AIF2RX"},
1398 	{"DAI1 RX Mux", "1:2|2:2", "AIF2RX"},
1399 
1400 	{"DAI2 RX Mux", "1:2|2:1", "AIF1RX"},
1401 	{"DAI2 RX Mux", "1:1|2:1", "AIF1RX"},
1402 	{"DAI2 RX Mux", "1:1|2:2", "AIF2RX"},
1403 	{"DAI2 RX Mux", "1:2|2:2", "AIF2RX"},
1404 
1405 	{"IF1 DAC", NULL, "I2S1"},
1406 	{"IF1 DAC", NULL, "DAI1 RX Mux"},
1407 	{"IF2 DAC", NULL, "I2S2"},
1408 	{"IF2 DAC", NULL, "DAI2 RX Mux"},
1409 
1410 	{"IF1 DAC L", NULL, "IF1 DAC"},
1411 	{"IF1 DAC R", NULL, "IF1 DAC"},
1412 	{"IF2 DAC L", NULL, "IF2 DAC"},
1413 	{"IF2 DAC R", NULL, "IF2 DAC"},
1414 
1415 	{"DAC MIXL", "Stereo ADC Switch", "Stereo ADC MIXL"},
1416 	{"DAC MIXL", "INF1 Switch", "IF1 DAC L"},
1417 	{"DAC MIXR", "Stereo ADC Switch", "Stereo ADC MIXR"},
1418 	{"DAC MIXR", "INF1 Switch", "IF1 DAC R"},
1419 
1420 	{"Stereo DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
1421 	{"Stereo DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
1422 
1423 	{"Mono DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
1424 	{"Mono DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
1425 
1426 	{"DIG MIXL", "DAC L1 Switch", "DAC MIXL"},
1427 	{"DIG MIXR", "DAC R1 Switch", "DAC MIXR"},
1428 
1429 	{"DAC L1", NULL, "Stereo DAC MIXL"},
1430 	{"DAC L1", NULL, "PLL1", is_sys_clk_from_pll},
1431 	{"DAC R1", NULL, "Stereo DAC MIXR"},
1432 	{"DAC R1", NULL, "PLL1", is_sys_clk_from_pll},
1433 
1434 	{"SPK MIXL", "REC MIXL Switch", "RECMIXL"},
1435 	{"SPK MIXL", "INL Switch", "INL VOL"},
1436 	{"SPK MIXL", "DAC L1 Switch", "DAC L1"},
1437 	{"SPK MIXL", "OUT MIXL Switch", "OUT MIXL"},
1438 	{"SPK MIXR", "REC MIXR Switch", "RECMIXR"},
1439 	{"SPK MIXR", "INR Switch", "INR VOL"},
1440 	{"SPK MIXR", "DAC R1 Switch", "DAC R1"},
1441 	{"SPK MIXR", "OUT MIXR Switch", "OUT MIXR"},
1442 
1443 	{"OUT MIXL", "BST1 Switch", "BST1"},
1444 	{"OUT MIXL", "INL Switch", "INL VOL"},
1445 	{"OUT MIXL", "REC MIXL Switch", "RECMIXL"},
1446 	{"OUT MIXL", "DAC L1 Switch", "DAC L1"},
1447 
1448 	{"OUT MIXR", "BST2 Switch", "BST2"},
1449 	{"OUT MIXR", "BST1 Switch", "BST1"},
1450 	{"OUT MIXR", "INR Switch", "INR VOL"},
1451 	{"OUT MIXR", "REC MIXR Switch", "RECMIXR"},
1452 	{"OUT MIXR", "DAC R1 Switch", "DAC R1"},
1453 
1454 	{"SPKVOL L", NULL, "SPK MIXL"},
1455 	{"SPKVOL R", NULL, "SPK MIXR"},
1456 	{"HPOVOL L", NULL, "OUT MIXL"},
1457 	{"HPOVOL R", NULL, "OUT MIXR"},
1458 	{"OUTVOL L", NULL, "OUT MIXL"},
1459 	{"OUTVOL R", NULL, "OUT MIXR"},
1460 
1461 	{"SPOL MIX", "DAC R1 Switch", "DAC R1"},
1462 	{"SPOL MIX", "DAC L1 Switch", "DAC L1"},
1463 	{"SPOL MIX", "SPKVOL R Switch", "SPKVOL R"},
1464 	{"SPOL MIX", "SPKVOL L Switch", "SPKVOL L"},
1465 	{"SPOL MIX", "BST1 Switch", "BST1"},
1466 	{"SPOR MIX", "DAC R1 Switch", "DAC R1"},
1467 	{"SPOR MIX", "SPKVOL R Switch", "SPKVOL R"},
1468 	{"SPOR MIX", "BST1 Switch", "BST1"},
1469 
1470 	{"HPO MIX L", "HPO MIX DAC1 Switch", "DAC L1"},
1471 	{"HPO MIX L", "HPO MIX HPVOL Switch", "HPOVOL L"},
1472 	{"HPO MIX L", NULL, "HP L Amp"},
1473 	{"HPO MIX R", "HPO MIX DAC1 Switch", "DAC R1"},
1474 	{"HPO MIX R", "HPO MIX HPVOL Switch", "HPOVOL R"},
1475 	{"HPO MIX R", NULL, "HP R Amp"},
1476 
1477 	{"LOUT MIX", "DAC L1 Switch", "DAC L1"},
1478 	{"LOUT MIX", "DAC R1 Switch", "DAC R1"},
1479 	{"LOUT MIX", "OUTVOL L Switch", "OUTVOL L"},
1480 	{"LOUT MIX", "OUTVOL R Switch", "OUTVOL R"},
1481 
1482 	{"HP Amp", NULL, "HPO MIX L"},
1483 	{"HP Amp", NULL, "HPO MIX R"},
1484 
1485 	{"Speaker L Playback", "Switch", "SPOL MIX"},
1486 	{"Speaker R Playback", "Switch", "SPOR MIX"},
1487 	{"SPOLP", NULL, "Speaker L Playback"},
1488 	{"SPOLN", NULL, "Speaker L Playback"},
1489 	{"SPORP", NULL, "Speaker R Playback"},
1490 	{"SPORN", NULL, "Speaker R Playback"},
1491 
1492 	{"SPOLP", NULL, "Improve SPK Amp Drv"},
1493 	{"SPOLN", NULL, "Improve SPK Amp Drv"},
1494 	{"SPORP", NULL, "Improve SPK Amp Drv"},
1495 	{"SPORN", NULL, "Improve SPK Amp Drv"},
1496 
1497 	{"HPOL", NULL, "Improve HP Amp Drv"},
1498 	{"HPOR", NULL, "Improve HP Amp Drv"},
1499 
1500 	{"HP L Playback", "Switch", "HP Amp"},
1501 	{"HP R Playback", "Switch", "HP Amp"},
1502 	{"HPOL", NULL, "HP L Playback"},
1503 	{"HPOR", NULL, "HP R Playback"},
1504 	{"LOUTL", NULL, "LOUT MIX"},
1505 	{"LOUTR", NULL, "LOUT MIX"},
1506 };
1507 
1508 static const struct snd_soc_dapm_route rt5640_specific_dapm_routes[] = {
1509 	{"ANC", NULL, "Stereo ADC MIXL"},
1510 	{"ANC", NULL, "Stereo ADC MIXR"},
1511 
1512 	{"Audio DSP", NULL, "DAC MIXL"},
1513 	{"Audio DSP", NULL, "DAC MIXR"},
1514 
1515 	{"DAC L2 Mux", "IF2", "IF2 DAC L"},
1516 	{"DAC L2 Mux", "Base L/R", "Audio DSP"},
1517 
1518 	{"DAC R2 Mux", "IF2", "IF2 DAC R"},
1519 
1520 	{"Stereo DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1521 	{"Stereo DAC MIXL", "ANC Switch", "ANC"},
1522 	{"Stereo DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1523 	{"Stereo DAC MIXR", "ANC Switch", "ANC"},
1524 
1525 	{"Mono DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1526 	{"Mono DAC MIXL", "DAC R2 Switch", "DAC R2 Mux"},
1527 
1528 	{"Mono DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1529 	{"Mono DAC MIXR", "DAC L2 Switch", "DAC L2 Mux"},
1530 
1531 	{"DIG MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1532 	{"DIG MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1533 
1534 	{"DAC L2", NULL, "Mono DAC MIXL"},
1535 	{"DAC L2", NULL, "PLL1", is_sys_clk_from_pll},
1536 	{"DAC R2", NULL, "Mono DAC MIXR"},
1537 	{"DAC R2", NULL, "PLL1", is_sys_clk_from_pll},
1538 
1539 	{"SPK MIXL", "DAC L2 Switch", "DAC L2"},
1540 	{"SPK MIXR", "DAC R2 Switch", "DAC R2"},
1541 
1542 	{"OUT MIXL", "SPK MIXL Switch", "SPK MIXL"},
1543 	{"OUT MIXR", "SPK MIXR Switch", "SPK MIXR"},
1544 
1545 	{"OUT MIXL", "DAC R2 Switch", "DAC R2"},
1546 	{"OUT MIXL", "DAC L2 Switch", "DAC L2"},
1547 
1548 	{"OUT MIXR", "DAC L2 Switch", "DAC L2"},
1549 	{"OUT MIXR", "DAC R2 Switch", "DAC R2"},
1550 
1551 	{"HPO MIX L", "HPO MIX DAC2 Switch", "DAC L2"},
1552 	{"HPO MIX R", "HPO MIX DAC2 Switch", "DAC R2"},
1553 
1554 	{"Mono MIX", "DAC R2 Switch", "DAC R2"},
1555 	{"Mono MIX", "DAC L2 Switch", "DAC L2"},
1556 	{"Mono MIX", "OUTVOL R Switch", "OUTVOL R"},
1557 	{"Mono MIX", "OUTVOL L Switch", "OUTVOL L"},
1558 	{"Mono MIX", "BST1 Switch", "BST1"},
1559 
1560 	{"MONOP", NULL, "Mono MIX"},
1561 	{"MONON", NULL, "Mono MIX"},
1562 	{"MONOP", NULL, "Improve MONO Amp Drv"},
1563 };
1564 
1565 static const struct snd_soc_dapm_route rt5639_specific_dapm_routes[] = {
1566 	{"Stereo DAC MIXL", "DAC L2 Switch", "IF2 DAC L"},
1567 	{"Stereo DAC MIXR", "DAC R2 Switch", "IF2 DAC R"},
1568 
1569 	{"Mono DAC MIXL", "DAC L2 Switch", "IF2 DAC L"},
1570 	{"Mono DAC MIXL", "DAC R2 Switch", "IF2 DAC R"},
1571 
1572 	{"Mono DAC MIXR", "DAC R2 Switch", "IF2 DAC R"},
1573 	{"Mono DAC MIXR", "DAC L2 Switch", "IF2 DAC L"},
1574 
1575 	{"DIG MIXL", "DAC L2 Switch", "IF2 DAC L"},
1576 	{"DIG MIXR", "DAC R2 Switch", "IF2 DAC R"},
1577 
1578 	{"IF2 DAC L", NULL, "DAC L2 Filter"},
1579 	{"IF2 DAC R", NULL, "DAC R2 Filter"},
1580 };
1581 
1582 static int get_sdp_info(struct snd_soc_codec *codec, int dai_id)
1583 {
1584 	int ret = 0, val;
1585 
1586 	if (codec == NULL)
1587 		return -EINVAL;
1588 
1589 	val = snd_soc_read(codec, RT5640_I2S1_SDP);
1590 	val = (val & RT5640_I2S_IF_MASK) >> RT5640_I2S_IF_SFT;
1591 	switch (dai_id) {
1592 	case RT5640_AIF1:
1593 		switch (val) {
1594 		case RT5640_IF_123:
1595 		case RT5640_IF_132:
1596 			ret |= RT5640_U_IF1;
1597 			break;
1598 		case RT5640_IF_113:
1599 			ret |= RT5640_U_IF1;
1600 		case RT5640_IF_312:
1601 		case RT5640_IF_213:
1602 			ret |= RT5640_U_IF2;
1603 			break;
1604 		}
1605 		break;
1606 
1607 	case RT5640_AIF2:
1608 		switch (val) {
1609 		case RT5640_IF_231:
1610 		case RT5640_IF_213:
1611 			ret |= RT5640_U_IF1;
1612 			break;
1613 		case RT5640_IF_223:
1614 			ret |= RT5640_U_IF1;
1615 		case RT5640_IF_123:
1616 		case RT5640_IF_321:
1617 			ret |= RT5640_U_IF2;
1618 			break;
1619 		}
1620 		break;
1621 
1622 	default:
1623 		ret = -EINVAL;
1624 		break;
1625 	}
1626 
1627 	return ret;
1628 }
1629 
1630 static int rt5640_hw_params(struct snd_pcm_substream *substream,
1631 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
1632 {
1633 	struct snd_soc_codec *codec = dai->codec;
1634 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
1635 	unsigned int val_len = 0, val_clk, mask_clk;
1636 	int dai_sel, pre_div, bclk_ms, frame_size;
1637 
1638 	rt5640->lrck[dai->id] = params_rate(params);
1639 	pre_div = rl6231_get_clk_info(rt5640->sysclk, rt5640->lrck[dai->id]);
1640 	if (pre_div < 0) {
1641 		dev_err(codec->dev, "Unsupported clock setting %d for DAI %d\n",
1642 			rt5640->lrck[dai->id], dai->id);
1643 		return -EINVAL;
1644 	}
1645 	frame_size = snd_soc_params_to_frame_size(params);
1646 	if (frame_size < 0) {
1647 		dev_err(codec->dev, "Unsupported frame size: %d\n", frame_size);
1648 		return frame_size;
1649 	}
1650 	if (frame_size > 32)
1651 		bclk_ms = 1;
1652 	else
1653 		bclk_ms = 0;
1654 	rt5640->bclk[dai->id] = rt5640->lrck[dai->id] * (32 << bclk_ms);
1655 
1656 	dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
1657 		rt5640->bclk[dai->id], rt5640->lrck[dai->id]);
1658 	dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
1659 				bclk_ms, pre_div, dai->id);
1660 
1661 	switch (params_width(params)) {
1662 	case 16:
1663 		break;
1664 	case 20:
1665 		val_len |= RT5640_I2S_DL_20;
1666 		break;
1667 	case 24:
1668 		val_len |= RT5640_I2S_DL_24;
1669 		break;
1670 	case 8:
1671 		val_len |= RT5640_I2S_DL_8;
1672 		break;
1673 	default:
1674 		return -EINVAL;
1675 	}
1676 
1677 	dai_sel = get_sdp_info(codec, dai->id);
1678 	if (dai_sel < 0) {
1679 		dev_err(codec->dev, "Failed to get sdp info: %d\n", dai_sel);
1680 		return -EINVAL;
1681 	}
1682 	if (dai_sel & RT5640_U_IF1) {
1683 		mask_clk = RT5640_I2S_BCLK_MS1_MASK | RT5640_I2S_PD1_MASK;
1684 		val_clk = bclk_ms << RT5640_I2S_BCLK_MS1_SFT |
1685 			pre_div << RT5640_I2S_PD1_SFT;
1686 		snd_soc_update_bits(codec, RT5640_I2S1_SDP,
1687 			RT5640_I2S_DL_MASK, val_len);
1688 		snd_soc_update_bits(codec, RT5640_ADDA_CLK1, mask_clk, val_clk);
1689 	}
1690 	if (dai_sel & RT5640_U_IF2) {
1691 		mask_clk = RT5640_I2S_BCLK_MS2_MASK | RT5640_I2S_PD2_MASK;
1692 		val_clk = bclk_ms << RT5640_I2S_BCLK_MS2_SFT |
1693 			pre_div << RT5640_I2S_PD2_SFT;
1694 		snd_soc_update_bits(codec, RT5640_I2S2_SDP,
1695 			RT5640_I2S_DL_MASK, val_len);
1696 		snd_soc_update_bits(codec, RT5640_ADDA_CLK1, mask_clk, val_clk);
1697 	}
1698 
1699 	return 0;
1700 }
1701 
1702 static int rt5640_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1703 {
1704 	struct snd_soc_codec *codec = dai->codec;
1705 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
1706 	unsigned int reg_val = 0;
1707 	int dai_sel;
1708 
1709 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1710 	case SND_SOC_DAIFMT_CBM_CFM:
1711 		rt5640->master[dai->id] = 1;
1712 		break;
1713 	case SND_SOC_DAIFMT_CBS_CFS:
1714 		reg_val |= RT5640_I2S_MS_S;
1715 		rt5640->master[dai->id] = 0;
1716 		break;
1717 	default:
1718 		return -EINVAL;
1719 	}
1720 
1721 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1722 	case SND_SOC_DAIFMT_NB_NF:
1723 		break;
1724 	case SND_SOC_DAIFMT_IB_NF:
1725 		reg_val |= RT5640_I2S_BP_INV;
1726 		break;
1727 	default:
1728 		return -EINVAL;
1729 	}
1730 
1731 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1732 	case SND_SOC_DAIFMT_I2S:
1733 		break;
1734 	case SND_SOC_DAIFMT_LEFT_J:
1735 		reg_val |= RT5640_I2S_DF_LEFT;
1736 		break;
1737 	case SND_SOC_DAIFMT_DSP_A:
1738 		reg_val |= RT5640_I2S_DF_PCM_A;
1739 		break;
1740 	case SND_SOC_DAIFMT_DSP_B:
1741 		reg_val  |= RT5640_I2S_DF_PCM_B;
1742 		break;
1743 	default:
1744 		return -EINVAL;
1745 	}
1746 
1747 	dai_sel = get_sdp_info(codec, dai->id);
1748 	if (dai_sel < 0) {
1749 		dev_err(codec->dev, "Failed to get sdp info: %d\n", dai_sel);
1750 		return -EINVAL;
1751 	}
1752 	if (dai_sel & RT5640_U_IF1) {
1753 		snd_soc_update_bits(codec, RT5640_I2S1_SDP,
1754 			RT5640_I2S_MS_MASK | RT5640_I2S_BP_MASK |
1755 			RT5640_I2S_DF_MASK, reg_val);
1756 	}
1757 	if (dai_sel & RT5640_U_IF2) {
1758 		snd_soc_update_bits(codec, RT5640_I2S2_SDP,
1759 			RT5640_I2S_MS_MASK | RT5640_I2S_BP_MASK |
1760 			RT5640_I2S_DF_MASK, reg_val);
1761 	}
1762 
1763 	return 0;
1764 }
1765 
1766 static int rt5640_set_dai_sysclk(struct snd_soc_dai *dai,
1767 		int clk_id, unsigned int freq, int dir)
1768 {
1769 	struct snd_soc_codec *codec = dai->codec;
1770 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
1771 	unsigned int reg_val = 0;
1772 
1773 	if (freq == rt5640->sysclk && clk_id == rt5640->sysclk_src)
1774 		return 0;
1775 
1776 	switch (clk_id) {
1777 	case RT5640_SCLK_S_MCLK:
1778 		reg_val |= RT5640_SCLK_SRC_MCLK;
1779 		break;
1780 	case RT5640_SCLK_S_PLL1:
1781 		reg_val |= RT5640_SCLK_SRC_PLL1;
1782 		break;
1783 	default:
1784 		dev_err(codec->dev, "Invalid clock id (%d)\n", clk_id);
1785 		return -EINVAL;
1786 	}
1787 	snd_soc_update_bits(codec, RT5640_GLB_CLK,
1788 		RT5640_SCLK_SRC_MASK, reg_val);
1789 	rt5640->sysclk = freq;
1790 	rt5640->sysclk_src = clk_id;
1791 
1792 	dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
1793 	return 0;
1794 }
1795 
1796 static int rt5640_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
1797 			unsigned int freq_in, unsigned int freq_out)
1798 {
1799 	struct snd_soc_codec *codec = dai->codec;
1800 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
1801 	struct rl6231_pll_code pll_code;
1802 	int ret, dai_sel;
1803 
1804 	if (source == rt5640->pll_src && freq_in == rt5640->pll_in &&
1805 	    freq_out == rt5640->pll_out)
1806 		return 0;
1807 
1808 	if (!freq_in || !freq_out) {
1809 		dev_dbg(codec->dev, "PLL disabled\n");
1810 
1811 		rt5640->pll_in = 0;
1812 		rt5640->pll_out = 0;
1813 		snd_soc_update_bits(codec, RT5640_GLB_CLK,
1814 			RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_MCLK);
1815 		return 0;
1816 	}
1817 
1818 	switch (source) {
1819 	case RT5640_PLL1_S_MCLK:
1820 		snd_soc_update_bits(codec, RT5640_GLB_CLK,
1821 			RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_MCLK);
1822 		break;
1823 	case RT5640_PLL1_S_BCLK1:
1824 	case RT5640_PLL1_S_BCLK2:
1825 		dai_sel = get_sdp_info(codec, dai->id);
1826 		if (dai_sel < 0) {
1827 			dev_err(codec->dev,
1828 				"Failed to get sdp info: %d\n", dai_sel);
1829 			return -EINVAL;
1830 		}
1831 		if (dai_sel & RT5640_U_IF1) {
1832 			snd_soc_update_bits(codec, RT5640_GLB_CLK,
1833 				RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK1);
1834 		}
1835 		if (dai_sel & RT5640_U_IF2) {
1836 			snd_soc_update_bits(codec, RT5640_GLB_CLK,
1837 				RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK2);
1838 		}
1839 		break;
1840 	default:
1841 		dev_err(codec->dev, "Unknown PLL source %d\n", source);
1842 		return -EINVAL;
1843 	}
1844 
1845 	ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
1846 	if (ret < 0) {
1847 		dev_err(codec->dev, "Unsupport input clock %d\n", freq_in);
1848 		return ret;
1849 	}
1850 
1851 	dev_dbg(codec->dev, "bypass=%d m=%d n=%d k=%d\n",
1852 		pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
1853 		pll_code.n_code, pll_code.k_code);
1854 
1855 	snd_soc_write(codec, RT5640_PLL_CTRL1,
1856 		pll_code.n_code << RT5640_PLL_N_SFT | pll_code.k_code);
1857 	snd_soc_write(codec, RT5640_PLL_CTRL2,
1858 		(pll_code.m_bp ? 0 : pll_code.m_code) << RT5640_PLL_M_SFT |
1859 		pll_code.m_bp << RT5640_PLL_M_BP_SFT);
1860 
1861 	rt5640->pll_in = freq_in;
1862 	rt5640->pll_out = freq_out;
1863 	rt5640->pll_src = source;
1864 
1865 	return 0;
1866 }
1867 
1868 static int rt5640_set_bias_level(struct snd_soc_codec *codec,
1869 			enum snd_soc_bias_level level)
1870 {
1871 	switch (level) {
1872 	case SND_SOC_BIAS_STANDBY:
1873 		if (SND_SOC_BIAS_OFF == codec->dapm.bias_level) {
1874 			snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
1875 				RT5640_PWR_VREF1 | RT5640_PWR_MB |
1876 				RT5640_PWR_BG | RT5640_PWR_VREF2,
1877 				RT5640_PWR_VREF1 | RT5640_PWR_MB |
1878 				RT5640_PWR_BG | RT5640_PWR_VREF2);
1879 			usleep_range(10000, 15000);
1880 			snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
1881 				RT5640_PWR_FV1 | RT5640_PWR_FV2,
1882 				RT5640_PWR_FV1 | RT5640_PWR_FV2);
1883 			snd_soc_update_bits(codec, RT5640_DUMMY1,
1884 						0x0301, 0x0301);
1885 			snd_soc_update_bits(codec, RT5640_MICBIAS,
1886 						0x0030, 0x0030);
1887 		}
1888 		break;
1889 
1890 	case SND_SOC_BIAS_OFF:
1891 		snd_soc_write(codec, RT5640_DEPOP_M1, 0x0004);
1892 		snd_soc_write(codec, RT5640_DEPOP_M2, 0x1100);
1893 		snd_soc_update_bits(codec, RT5640_DUMMY1, 0x1, 0);
1894 		snd_soc_write(codec, RT5640_PWR_DIG1, 0x0000);
1895 		snd_soc_write(codec, RT5640_PWR_DIG2, 0x0000);
1896 		snd_soc_write(codec, RT5640_PWR_VOL, 0x0000);
1897 		snd_soc_write(codec, RT5640_PWR_MIXER, 0x0000);
1898 		snd_soc_write(codec, RT5640_PWR_ANLG1, 0x0000);
1899 		snd_soc_write(codec, RT5640_PWR_ANLG2, 0x0000);
1900 		break;
1901 
1902 	default:
1903 		break;
1904 	}
1905 	codec->dapm.bias_level = level;
1906 
1907 	return 0;
1908 }
1909 
1910 int rt5640_dmic_enable(struct snd_soc_codec *codec,
1911 		       bool dmic1_data_pin, bool dmic2_data_pin)
1912 {
1913 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
1914 
1915 	regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
1916 		RT5640_GP2_PIN_MASK, RT5640_GP2_PIN_DMIC1_SCL);
1917 
1918 	if (dmic1_data_pin) {
1919 		regmap_update_bits(rt5640->regmap, RT5640_DMIC,
1920 			RT5640_DMIC_1_DP_MASK, RT5640_DMIC_1_DP_GPIO3);
1921 		regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
1922 			RT5640_GP3_PIN_MASK, RT5640_GP3_PIN_DMIC1_SDA);
1923 	}
1924 
1925 	if (dmic2_data_pin) {
1926 		regmap_update_bits(rt5640->regmap, RT5640_DMIC,
1927 			RT5640_DMIC_2_DP_MASK, RT5640_DMIC_2_DP_GPIO4);
1928 		regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
1929 			RT5640_GP4_PIN_MASK, RT5640_GP4_PIN_DMIC2_SDA);
1930 	}
1931 
1932 	return 0;
1933 }
1934 EXPORT_SYMBOL_GPL(rt5640_dmic_enable);
1935 
1936 static int rt5640_probe(struct snd_soc_codec *codec)
1937 {
1938 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
1939 
1940 	rt5640->codec = codec;
1941 
1942 	rt5640_set_bias_level(codec, SND_SOC_BIAS_OFF);
1943 
1944 	snd_soc_update_bits(codec, RT5640_DUMMY1, 0x0301, 0x0301);
1945 	snd_soc_update_bits(codec, RT5640_MICBIAS, 0x0030, 0x0030);
1946 	snd_soc_update_bits(codec, RT5640_DSP_PATH2, 0xfc00, 0x0c00);
1947 
1948 	switch (snd_soc_read(codec, RT5640_RESET) & RT5640_ID_MASK) {
1949 	case RT5640_ID_5640:
1950 	case RT5640_ID_5642:
1951 		snd_soc_add_codec_controls(codec,
1952 			rt5640_specific_snd_controls,
1953 			ARRAY_SIZE(rt5640_specific_snd_controls));
1954 		snd_soc_dapm_new_controls(&codec->dapm,
1955 			rt5640_specific_dapm_widgets,
1956 			ARRAY_SIZE(rt5640_specific_dapm_widgets));
1957 		snd_soc_dapm_add_routes(&codec->dapm,
1958 			rt5640_specific_dapm_routes,
1959 			ARRAY_SIZE(rt5640_specific_dapm_routes));
1960 		break;
1961 	case RT5640_ID_5639:
1962 		snd_soc_dapm_new_controls(&codec->dapm,
1963 			rt5639_specific_dapm_widgets,
1964 			ARRAY_SIZE(rt5639_specific_dapm_widgets));
1965 		snd_soc_dapm_add_routes(&codec->dapm,
1966 			rt5639_specific_dapm_routes,
1967 			ARRAY_SIZE(rt5639_specific_dapm_routes));
1968 		break;
1969 	default:
1970 		dev_err(codec->dev,
1971 			"The driver is for RT5639 RT5640 or RT5642 only\n");
1972 		return -ENODEV;
1973 	}
1974 
1975 	if (rt5640->pdata.dmic_en)
1976 		rt5640_dmic_enable(codec, rt5640->pdata.dmic1_data_pin,
1977 					  rt5640->pdata.dmic2_data_pin);
1978 
1979 	return 0;
1980 }
1981 
1982 static int rt5640_remove(struct snd_soc_codec *codec)
1983 {
1984 	rt5640_reset(codec);
1985 
1986 	return 0;
1987 }
1988 
1989 #ifdef CONFIG_PM
1990 static int rt5640_suspend(struct snd_soc_codec *codec)
1991 {
1992 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
1993 
1994 	rt5640_set_bias_level(codec, SND_SOC_BIAS_OFF);
1995 	rt5640_reset(codec);
1996 	regcache_cache_only(rt5640->regmap, true);
1997 	regcache_mark_dirty(rt5640->regmap);
1998 	if (gpio_is_valid(rt5640->pdata.ldo1_en))
1999 		gpio_set_value_cansleep(rt5640->pdata.ldo1_en, 0);
2000 
2001 	return 0;
2002 }
2003 
2004 static int rt5640_resume(struct snd_soc_codec *codec)
2005 {
2006 	struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
2007 
2008 	if (gpio_is_valid(rt5640->pdata.ldo1_en)) {
2009 		gpio_set_value_cansleep(rt5640->pdata.ldo1_en, 1);
2010 		msleep(400);
2011 	}
2012 
2013 	regcache_cache_only(rt5640->regmap, false);
2014 	regcache_sync(rt5640->regmap);
2015 
2016 	return 0;
2017 }
2018 #else
2019 #define rt5640_suspend NULL
2020 #define rt5640_resume NULL
2021 #endif
2022 
2023 #define RT5640_STEREO_RATES SNDRV_PCM_RATE_8000_96000
2024 #define RT5640_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
2025 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
2026 
2027 static const struct snd_soc_dai_ops rt5640_aif_dai_ops = {
2028 	.hw_params = rt5640_hw_params,
2029 	.set_fmt = rt5640_set_dai_fmt,
2030 	.set_sysclk = rt5640_set_dai_sysclk,
2031 	.set_pll = rt5640_set_dai_pll,
2032 };
2033 
2034 static struct snd_soc_dai_driver rt5640_dai[] = {
2035 	{
2036 		.name = "rt5640-aif1",
2037 		.id = RT5640_AIF1,
2038 		.playback = {
2039 			.stream_name = "AIF1 Playback",
2040 			.channels_min = 1,
2041 			.channels_max = 2,
2042 			.rates = RT5640_STEREO_RATES,
2043 			.formats = RT5640_FORMATS,
2044 		},
2045 		.capture = {
2046 			.stream_name = "AIF1 Capture",
2047 			.channels_min = 1,
2048 			.channels_max = 2,
2049 			.rates = RT5640_STEREO_RATES,
2050 			.formats = RT5640_FORMATS,
2051 		},
2052 		.ops = &rt5640_aif_dai_ops,
2053 	},
2054 	{
2055 		.name = "rt5640-aif2",
2056 		.id = RT5640_AIF2,
2057 		.playback = {
2058 			.stream_name = "AIF2 Playback",
2059 			.channels_min = 1,
2060 			.channels_max = 2,
2061 			.rates = RT5640_STEREO_RATES,
2062 			.formats = RT5640_FORMATS,
2063 		},
2064 		.capture = {
2065 			.stream_name = "AIF2 Capture",
2066 			.channels_min = 1,
2067 			.channels_max = 2,
2068 			.rates = RT5640_STEREO_RATES,
2069 			.formats = RT5640_FORMATS,
2070 		},
2071 		.ops = &rt5640_aif_dai_ops,
2072 	},
2073 };
2074 
2075 static struct snd_soc_codec_driver soc_codec_dev_rt5640 = {
2076 	.probe = rt5640_probe,
2077 	.remove = rt5640_remove,
2078 	.suspend = rt5640_suspend,
2079 	.resume = rt5640_resume,
2080 	.set_bias_level = rt5640_set_bias_level,
2081 	.idle_bias_off = true,
2082 	.controls = rt5640_snd_controls,
2083 	.num_controls = ARRAY_SIZE(rt5640_snd_controls),
2084 	.dapm_widgets = rt5640_dapm_widgets,
2085 	.num_dapm_widgets = ARRAY_SIZE(rt5640_dapm_widgets),
2086 	.dapm_routes = rt5640_dapm_routes,
2087 	.num_dapm_routes = ARRAY_SIZE(rt5640_dapm_routes),
2088 };
2089 
2090 static const struct regmap_config rt5640_regmap = {
2091 	.reg_bits = 8,
2092 	.val_bits = 16,
2093 	.use_single_rw = true,
2094 
2095 	.max_register = RT5640_VENDOR_ID2 + 1 + (ARRAY_SIZE(rt5640_ranges) *
2096 					       RT5640_PR_SPACING),
2097 	.volatile_reg = rt5640_volatile_register,
2098 	.readable_reg = rt5640_readable_register,
2099 
2100 	.cache_type = REGCACHE_RBTREE,
2101 	.reg_defaults = rt5640_reg,
2102 	.num_reg_defaults = ARRAY_SIZE(rt5640_reg),
2103 	.ranges = rt5640_ranges,
2104 	.num_ranges = ARRAY_SIZE(rt5640_ranges),
2105 };
2106 
2107 static const struct i2c_device_id rt5640_i2c_id[] = {
2108 	{ "rt5640", 0 },
2109 	{ "rt5639", 0 },
2110 	{ "rt5642", 0 },
2111 	{ }
2112 };
2113 MODULE_DEVICE_TABLE(i2c, rt5640_i2c_id);
2114 
2115 #if defined(CONFIG_OF)
2116 static const struct of_device_id rt5640_of_match[] = {
2117 	{ .compatible = "realtek,rt5639", },
2118 	{ .compatible = "realtek,rt5640", },
2119 	{},
2120 };
2121 MODULE_DEVICE_TABLE(of, rt5640_of_match);
2122 #endif
2123 
2124 #ifdef CONFIG_ACPI
2125 static struct acpi_device_id rt5640_acpi_match[] = {
2126 	{ "INT33CA", 0 },
2127 	{ "10EC5640", 0 },
2128 	{ "10EC5642", 0 },
2129 	{ },
2130 };
2131 MODULE_DEVICE_TABLE(acpi, rt5640_acpi_match);
2132 #endif
2133 
2134 static int rt5640_parse_dt(struct rt5640_priv *rt5640, struct device_node *np)
2135 {
2136 	rt5640->pdata.in1_diff = of_property_read_bool(np,
2137 					"realtek,in1-differential");
2138 	rt5640->pdata.in2_diff = of_property_read_bool(np,
2139 					"realtek,in2-differential");
2140 
2141 	rt5640->pdata.ldo1_en = of_get_named_gpio(np,
2142 					"realtek,ldo1-en-gpios", 0);
2143 	/*
2144 	 * LDO1_EN is optional (it may be statically tied on the board).
2145 	 * -ENOENT means that the property doesn't exist, i.e. there is no
2146 	 * GPIO, so is not an error. Any other error code means the property
2147 	 * exists, but could not be parsed.
2148 	 */
2149 	if (!gpio_is_valid(rt5640->pdata.ldo1_en) &&
2150 			(rt5640->pdata.ldo1_en != -ENOENT))
2151 		return rt5640->pdata.ldo1_en;
2152 
2153 	return 0;
2154 }
2155 
2156 static int rt5640_i2c_probe(struct i2c_client *i2c,
2157 		    const struct i2c_device_id *id)
2158 {
2159 	struct rt5640_platform_data *pdata = dev_get_platdata(&i2c->dev);
2160 	struct rt5640_priv *rt5640;
2161 	int ret;
2162 	unsigned int val;
2163 
2164 	rt5640 = devm_kzalloc(&i2c->dev,
2165 				sizeof(struct rt5640_priv),
2166 				GFP_KERNEL);
2167 	if (NULL == rt5640)
2168 		return -ENOMEM;
2169 	i2c_set_clientdata(i2c, rt5640);
2170 
2171 	if (pdata) {
2172 		rt5640->pdata = *pdata;
2173 		/*
2174 		 * Translate zero'd out (default) pdata value to an invalid
2175 		 * GPIO ID. This makes the pdata and DT paths consistent in
2176 		 * terms of the value left in this field when no GPIO is
2177 		 * specified, but means we can't actually use GPIO 0.
2178 		 */
2179 		if (!rt5640->pdata.ldo1_en)
2180 			rt5640->pdata.ldo1_en = -EINVAL;
2181 	} else if (i2c->dev.of_node) {
2182 		ret = rt5640_parse_dt(rt5640, i2c->dev.of_node);
2183 		if (ret)
2184 			return ret;
2185 	} else
2186 		rt5640->pdata.ldo1_en = -EINVAL;
2187 
2188 	rt5640->regmap = devm_regmap_init_i2c(i2c, &rt5640_regmap);
2189 	if (IS_ERR(rt5640->regmap)) {
2190 		ret = PTR_ERR(rt5640->regmap);
2191 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
2192 			ret);
2193 		return ret;
2194 	}
2195 
2196 	if (gpio_is_valid(rt5640->pdata.ldo1_en)) {
2197 		ret = devm_gpio_request_one(&i2c->dev, rt5640->pdata.ldo1_en,
2198 					    GPIOF_OUT_INIT_HIGH,
2199 					    "RT5640 LDO1_EN");
2200 		if (ret < 0) {
2201 			dev_err(&i2c->dev, "Failed to request LDO1_EN %d: %d\n",
2202 				rt5640->pdata.ldo1_en, ret);
2203 			return ret;
2204 		}
2205 		msleep(400);
2206 	}
2207 
2208 	regmap_read(rt5640->regmap, RT5640_VENDOR_ID2, &val);
2209 	if (val != RT5640_DEVICE_ID) {
2210 		dev_err(&i2c->dev,
2211 			"Device with ID register %x is not rt5640/39\n", val);
2212 		return -ENODEV;
2213 	}
2214 
2215 	regmap_write(rt5640->regmap, RT5640_RESET, 0);
2216 
2217 	ret = regmap_register_patch(rt5640->regmap, init_list,
2218 				    ARRAY_SIZE(init_list));
2219 	if (ret != 0)
2220 		dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
2221 
2222 	if (rt5640->pdata.in1_diff)
2223 		regmap_update_bits(rt5640->regmap, RT5640_IN1_IN2,
2224 					RT5640_IN_DF1, RT5640_IN_DF1);
2225 
2226 	if (rt5640->pdata.in2_diff)
2227 		regmap_update_bits(rt5640->regmap, RT5640_IN3_IN4,
2228 					RT5640_IN_DF2, RT5640_IN_DF2);
2229 
2230 	rt5640->hp_mute = 1;
2231 
2232 	return snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt5640,
2233 				      rt5640_dai, ARRAY_SIZE(rt5640_dai));
2234 }
2235 
2236 static int rt5640_i2c_remove(struct i2c_client *i2c)
2237 {
2238 	snd_soc_unregister_codec(&i2c->dev);
2239 
2240 	return 0;
2241 }
2242 
2243 static struct i2c_driver rt5640_i2c_driver = {
2244 	.driver = {
2245 		.name = "rt5640",
2246 		.owner = THIS_MODULE,
2247 		.acpi_match_table = ACPI_PTR(rt5640_acpi_match),
2248 		.of_match_table = of_match_ptr(rt5640_of_match),
2249 	},
2250 	.probe = rt5640_i2c_probe,
2251 	.remove   = rt5640_i2c_remove,
2252 	.id_table = rt5640_i2c_id,
2253 };
2254 module_i2c_driver(rt5640_i2c_driver);
2255 
2256 MODULE_DESCRIPTION("ASoC RT5640/RT5639 driver");
2257 MODULE_AUTHOR("Johnny Hsu <johnnyhsu@realtek.com>");
2258 MODULE_LICENSE("GPL v2");
2259