xref: /openbmc/linux/sound/soc/codecs/rt5670.c (revision ff6defa6)
1 /*
2  * rt5670.c  --  RT5670 ALSA SoC audio codec driver
3  *
4  * Copyright 2014 Realtek Semiconductor Corp.
5  * Author: Bard Liao <bardliao@realtek.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/pm.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/i2c.h>
19 #include <linux/platform_device.h>
20 #include <linux/acpi.h>
21 #include <linux/spi/spi.h>
22 #include <linux/dmi.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/jack.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
29 #include <sound/initval.h>
30 #include <sound/tlv.h>
31 #include <sound/rt5670.h>
32 
33 #include "rl6231.h"
34 #include "rt5670.h"
35 #include "rt5670-dsp.h"
36 
37 #define RT5670_DEVICE_ID 0x6271
38 
39 #define RT5670_PR_RANGE_BASE (0xff + 1)
40 #define RT5670_PR_SPACING 0x100
41 
42 #define RT5670_PR_BASE (RT5670_PR_RANGE_BASE + (0 * RT5670_PR_SPACING))
43 
44 static const struct regmap_range_cfg rt5670_ranges[] = {
45 	{ .name = "PR", .range_min = RT5670_PR_BASE,
46 	  .range_max = RT5670_PR_BASE + 0xf8,
47 	  .selector_reg = RT5670_PRIV_INDEX,
48 	  .selector_mask = 0xff,
49 	  .selector_shift = 0x0,
50 	  .window_start = RT5670_PRIV_DATA,
51 	  .window_len = 0x1, },
52 };
53 
54 static struct reg_default init_list[] = {
55 	{ RT5670_PR_BASE + 0x14, 0x9a8a },
56 	{ RT5670_PR_BASE + 0x38, 0x3ba1 },
57 	{ RT5670_PR_BASE + 0x3d, 0x3640 },
58 };
59 #define RT5670_INIT_REG_LEN ARRAY_SIZE(init_list)
60 
61 static const struct reg_default rt5670_reg[] = {
62 	{ 0x00, 0x0000 },
63 	{ 0x02, 0x8888 },
64 	{ 0x03, 0x8888 },
65 	{ 0x0a, 0x0001 },
66 	{ 0x0b, 0x0827 },
67 	{ 0x0c, 0x0000 },
68 	{ 0x0d, 0x0008 },
69 	{ 0x0e, 0x0000 },
70 	{ 0x0f, 0x0808 },
71 	{ 0x19, 0xafaf },
72 	{ 0x1a, 0xafaf },
73 	{ 0x1b, 0x0011 },
74 	{ 0x1c, 0x2f2f },
75 	{ 0x1d, 0x2f2f },
76 	{ 0x1e, 0x0000 },
77 	{ 0x1f, 0x2f2f },
78 	{ 0x20, 0x0000 },
79 	{ 0x26, 0x7860 },
80 	{ 0x27, 0x7860 },
81 	{ 0x28, 0x7871 },
82 	{ 0x29, 0x8080 },
83 	{ 0x2a, 0x5656 },
84 	{ 0x2b, 0x5454 },
85 	{ 0x2c, 0xaaa0 },
86 	{ 0x2d, 0x0000 },
87 	{ 0x2e, 0x2f2f },
88 	{ 0x2f, 0x1002 },
89 	{ 0x30, 0x0000 },
90 	{ 0x31, 0x5f00 },
91 	{ 0x32, 0x0000 },
92 	{ 0x33, 0x0000 },
93 	{ 0x34, 0x0000 },
94 	{ 0x35, 0x0000 },
95 	{ 0x36, 0x0000 },
96 	{ 0x37, 0x0000 },
97 	{ 0x38, 0x0000 },
98 	{ 0x3b, 0x0000 },
99 	{ 0x3c, 0x007f },
100 	{ 0x3d, 0x0000 },
101 	{ 0x3e, 0x007f },
102 	{ 0x45, 0xe00f },
103 	{ 0x4c, 0x5380 },
104 	{ 0x4f, 0x0073 },
105 	{ 0x52, 0x00d3 },
106 	{ 0x53, 0xf000 },
107 	{ 0x61, 0x0000 },
108 	{ 0x62, 0x0001 },
109 	{ 0x63, 0x00c3 },
110 	{ 0x64, 0x0000 },
111 	{ 0x65, 0x0001 },
112 	{ 0x66, 0x0000 },
113 	{ 0x6f, 0x8000 },
114 	{ 0x70, 0x8000 },
115 	{ 0x71, 0x8000 },
116 	{ 0x72, 0x8000 },
117 	{ 0x73, 0x7770 },
118 	{ 0x74, 0x0e00 },
119 	{ 0x75, 0x1505 },
120 	{ 0x76, 0x0015 },
121 	{ 0x77, 0x0c00 },
122 	{ 0x78, 0x4000 },
123 	{ 0x79, 0x0123 },
124 	{ 0x7f, 0x1100 },
125 	{ 0x80, 0x0000 },
126 	{ 0x81, 0x0000 },
127 	{ 0x82, 0x0000 },
128 	{ 0x83, 0x0000 },
129 	{ 0x84, 0x0000 },
130 	{ 0x85, 0x0000 },
131 	{ 0x86, 0x0004 },
132 	{ 0x87, 0x0000 },
133 	{ 0x88, 0x0000 },
134 	{ 0x89, 0x0000 },
135 	{ 0x8a, 0x0000 },
136 	{ 0x8b, 0x0000 },
137 	{ 0x8c, 0x0003 },
138 	{ 0x8d, 0x0000 },
139 	{ 0x8e, 0x0004 },
140 	{ 0x8f, 0x1100 },
141 	{ 0x90, 0x0646 },
142 	{ 0x91, 0x0c06 },
143 	{ 0x93, 0x0000 },
144 	{ 0x94, 0x1270 },
145 	{ 0x95, 0x1000 },
146 	{ 0x97, 0x0000 },
147 	{ 0x98, 0x0000 },
148 	{ 0x99, 0x0000 },
149 	{ 0x9a, 0x2184 },
150 	{ 0x9b, 0x010a },
151 	{ 0x9c, 0x0aea },
152 	{ 0x9d, 0x000c },
153 	{ 0x9e, 0x0400 },
154 	{ 0xae, 0x7000 },
155 	{ 0xaf, 0x0000 },
156 	{ 0xb0, 0x7000 },
157 	{ 0xb1, 0x0000 },
158 	{ 0xb2, 0x0000 },
159 	{ 0xb3, 0x001f },
160 	{ 0xb4, 0x220c },
161 	{ 0xb5, 0x1f00 },
162 	{ 0xb6, 0x0000 },
163 	{ 0xb7, 0x0000 },
164 	{ 0xbb, 0x0000 },
165 	{ 0xbc, 0x0000 },
166 	{ 0xbd, 0x0000 },
167 	{ 0xbe, 0x0000 },
168 	{ 0xbf, 0x0000 },
169 	{ 0xc0, 0x0000 },
170 	{ 0xc1, 0x0000 },
171 	{ 0xc2, 0x0000 },
172 	{ 0xcd, 0x0000 },
173 	{ 0xce, 0x0000 },
174 	{ 0xcf, 0x1813 },
175 	{ 0xd0, 0x0690 },
176 	{ 0xd1, 0x1c17 },
177 	{ 0xd3, 0xa220 },
178 	{ 0xd4, 0x0000 },
179 	{ 0xd6, 0x0400 },
180 	{ 0xd9, 0x0809 },
181 	{ 0xda, 0x0000 },
182 	{ 0xdb, 0x0001 },
183 	{ 0xdc, 0x0049 },
184 	{ 0xdd, 0x0024 },
185 	{ 0xe6, 0x8000 },
186 	{ 0xe7, 0x0000 },
187 	{ 0xec, 0xa200 },
188 	{ 0xed, 0x0000 },
189 	{ 0xee, 0xa200 },
190 	{ 0xef, 0x0000 },
191 	{ 0xf8, 0x0000 },
192 	{ 0xf9, 0x0000 },
193 	{ 0xfa, 0x8010 },
194 	{ 0xfb, 0x0033 },
195 	{ 0xfc, 0x0100 },
196 };
197 
198 static bool rt5670_volatile_register(struct device *dev, unsigned int reg)
199 {
200 	int i;
201 
202 	for (i = 0; i < ARRAY_SIZE(rt5670_ranges); i++) {
203 		if ((reg >= rt5670_ranges[i].window_start &&
204 		     reg <= rt5670_ranges[i].window_start +
205 		     rt5670_ranges[i].window_len) ||
206 		    (reg >= rt5670_ranges[i].range_min &&
207 		     reg <= rt5670_ranges[i].range_max)) {
208 			return true;
209 		}
210 	}
211 
212 	switch (reg) {
213 	case RT5670_RESET:
214 	case RT5670_PDM_DATA_CTRL1:
215 	case RT5670_PDM1_DATA_CTRL4:
216 	case RT5670_PDM2_DATA_CTRL4:
217 	case RT5670_PRIV_DATA:
218 	case RT5670_ASRC_5:
219 	case RT5670_CJ_CTRL1:
220 	case RT5670_CJ_CTRL2:
221 	case RT5670_CJ_CTRL3:
222 	case RT5670_A_JD_CTRL1:
223 	case RT5670_A_JD_CTRL2:
224 	case RT5670_VAD_CTRL5:
225 	case RT5670_ADC_EQ_CTRL1:
226 	case RT5670_EQ_CTRL1:
227 	case RT5670_ALC_CTRL_1:
228 	case RT5670_IRQ_CTRL1:
229 	case RT5670_IRQ_CTRL2:
230 	case RT5670_INT_IRQ_ST:
231 	case RT5670_IL_CMD:
232 	case RT5670_DSP_CTRL1:
233 	case RT5670_DSP_CTRL2:
234 	case RT5670_DSP_CTRL3:
235 	case RT5670_DSP_CTRL4:
236 	case RT5670_DSP_CTRL5:
237 	case RT5670_VENDOR_ID:
238 	case RT5670_VENDOR_ID1:
239 	case RT5670_VENDOR_ID2:
240 		return true;
241 	default:
242 		return false;
243 	}
244 }
245 
246 static bool rt5670_readable_register(struct device *dev, unsigned int reg)
247 {
248 	int i;
249 
250 	for (i = 0; i < ARRAY_SIZE(rt5670_ranges); i++) {
251 		if ((reg >= rt5670_ranges[i].window_start &&
252 		     reg <= rt5670_ranges[i].window_start +
253 		     rt5670_ranges[i].window_len) ||
254 		    (reg >= rt5670_ranges[i].range_min &&
255 		     reg <= rt5670_ranges[i].range_max)) {
256 			return true;
257 		}
258 	}
259 
260 	switch (reg) {
261 	case RT5670_RESET:
262 	case RT5670_HP_VOL:
263 	case RT5670_LOUT1:
264 	case RT5670_CJ_CTRL1:
265 	case RT5670_CJ_CTRL2:
266 	case RT5670_CJ_CTRL3:
267 	case RT5670_IN2:
268 	case RT5670_INL1_INR1_VOL:
269 	case RT5670_DAC1_DIG_VOL:
270 	case RT5670_DAC2_DIG_VOL:
271 	case RT5670_DAC_CTRL:
272 	case RT5670_STO1_ADC_DIG_VOL:
273 	case RT5670_MONO_ADC_DIG_VOL:
274 	case RT5670_STO2_ADC_DIG_VOL:
275 	case RT5670_ADC_BST_VOL1:
276 	case RT5670_ADC_BST_VOL2:
277 	case RT5670_STO2_ADC_MIXER:
278 	case RT5670_STO1_ADC_MIXER:
279 	case RT5670_MONO_ADC_MIXER:
280 	case RT5670_AD_DA_MIXER:
281 	case RT5670_STO_DAC_MIXER:
282 	case RT5670_DD_MIXER:
283 	case RT5670_DIG_MIXER:
284 	case RT5670_DSP_PATH1:
285 	case RT5670_DSP_PATH2:
286 	case RT5670_DIG_INF1_DATA:
287 	case RT5670_DIG_INF2_DATA:
288 	case RT5670_PDM_OUT_CTRL:
289 	case RT5670_PDM_DATA_CTRL1:
290 	case RT5670_PDM1_DATA_CTRL2:
291 	case RT5670_PDM1_DATA_CTRL3:
292 	case RT5670_PDM1_DATA_CTRL4:
293 	case RT5670_PDM2_DATA_CTRL2:
294 	case RT5670_PDM2_DATA_CTRL3:
295 	case RT5670_PDM2_DATA_CTRL4:
296 	case RT5670_REC_L1_MIXER:
297 	case RT5670_REC_L2_MIXER:
298 	case RT5670_REC_R1_MIXER:
299 	case RT5670_REC_R2_MIXER:
300 	case RT5670_HPO_MIXER:
301 	case RT5670_MONO_MIXER:
302 	case RT5670_OUT_L1_MIXER:
303 	case RT5670_OUT_R1_MIXER:
304 	case RT5670_LOUT_MIXER:
305 	case RT5670_PWR_DIG1:
306 	case RT5670_PWR_DIG2:
307 	case RT5670_PWR_ANLG1:
308 	case RT5670_PWR_ANLG2:
309 	case RT5670_PWR_MIXER:
310 	case RT5670_PWR_VOL:
311 	case RT5670_PRIV_INDEX:
312 	case RT5670_PRIV_DATA:
313 	case RT5670_I2S4_SDP:
314 	case RT5670_I2S1_SDP:
315 	case RT5670_I2S2_SDP:
316 	case RT5670_I2S3_SDP:
317 	case RT5670_ADDA_CLK1:
318 	case RT5670_ADDA_CLK2:
319 	case RT5670_DMIC_CTRL1:
320 	case RT5670_DMIC_CTRL2:
321 	case RT5670_TDM_CTRL_1:
322 	case RT5670_TDM_CTRL_2:
323 	case RT5670_TDM_CTRL_3:
324 	case RT5670_DSP_CLK:
325 	case RT5670_GLB_CLK:
326 	case RT5670_PLL_CTRL1:
327 	case RT5670_PLL_CTRL2:
328 	case RT5670_ASRC_1:
329 	case RT5670_ASRC_2:
330 	case RT5670_ASRC_3:
331 	case RT5670_ASRC_4:
332 	case RT5670_ASRC_5:
333 	case RT5670_ASRC_7:
334 	case RT5670_ASRC_8:
335 	case RT5670_ASRC_9:
336 	case RT5670_ASRC_10:
337 	case RT5670_ASRC_11:
338 	case RT5670_ASRC_12:
339 	case RT5670_ASRC_13:
340 	case RT5670_ASRC_14:
341 	case RT5670_DEPOP_M1:
342 	case RT5670_DEPOP_M2:
343 	case RT5670_DEPOP_M3:
344 	case RT5670_CHARGE_PUMP:
345 	case RT5670_MICBIAS:
346 	case RT5670_A_JD_CTRL1:
347 	case RT5670_A_JD_CTRL2:
348 	case RT5670_VAD_CTRL1:
349 	case RT5670_VAD_CTRL2:
350 	case RT5670_VAD_CTRL3:
351 	case RT5670_VAD_CTRL4:
352 	case RT5670_VAD_CTRL5:
353 	case RT5670_ADC_EQ_CTRL1:
354 	case RT5670_ADC_EQ_CTRL2:
355 	case RT5670_EQ_CTRL1:
356 	case RT5670_EQ_CTRL2:
357 	case RT5670_ALC_DRC_CTRL1:
358 	case RT5670_ALC_DRC_CTRL2:
359 	case RT5670_ALC_CTRL_1:
360 	case RT5670_ALC_CTRL_2:
361 	case RT5670_ALC_CTRL_3:
362 	case RT5670_JD_CTRL:
363 	case RT5670_IRQ_CTRL1:
364 	case RT5670_IRQ_CTRL2:
365 	case RT5670_INT_IRQ_ST:
366 	case RT5670_GPIO_CTRL1:
367 	case RT5670_GPIO_CTRL2:
368 	case RT5670_GPIO_CTRL3:
369 	case RT5670_SCRABBLE_FUN:
370 	case RT5670_SCRABBLE_CTRL:
371 	case RT5670_BASE_BACK:
372 	case RT5670_MP3_PLUS1:
373 	case RT5670_MP3_PLUS2:
374 	case RT5670_ADJ_HPF1:
375 	case RT5670_ADJ_HPF2:
376 	case RT5670_HP_CALIB_AMP_DET:
377 	case RT5670_SV_ZCD1:
378 	case RT5670_SV_ZCD2:
379 	case RT5670_IL_CMD:
380 	case RT5670_IL_CMD2:
381 	case RT5670_IL_CMD3:
382 	case RT5670_DRC_HL_CTRL1:
383 	case RT5670_DRC_HL_CTRL2:
384 	case RT5670_ADC_MONO_HP_CTRL1:
385 	case RT5670_ADC_MONO_HP_CTRL2:
386 	case RT5670_ADC_STO2_HP_CTRL1:
387 	case RT5670_ADC_STO2_HP_CTRL2:
388 	case RT5670_JD_CTRL3:
389 	case RT5670_JD_CTRL4:
390 	case RT5670_DIG_MISC:
391 	case RT5670_DSP_CTRL1:
392 	case RT5670_DSP_CTRL2:
393 	case RT5670_DSP_CTRL3:
394 	case RT5670_DSP_CTRL4:
395 	case RT5670_DSP_CTRL5:
396 	case RT5670_GEN_CTRL2:
397 	case RT5670_GEN_CTRL3:
398 	case RT5670_VENDOR_ID:
399 	case RT5670_VENDOR_ID1:
400 	case RT5670_VENDOR_ID2:
401 		return true;
402 	default:
403 		return false;
404 	}
405 }
406 
407 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
408 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -65625, 375, 0);
409 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
410 static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -17625, 375, 0);
411 static const DECLARE_TLV_DB_SCALE(adc_bst_tlv, 0, 1200, 0);
412 
413 /* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
414 static unsigned int bst_tlv[] = {
415 	TLV_DB_RANGE_HEAD(7),
416 	0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
417 	1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
418 	2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
419 	3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
420 	6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
421 	7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
422 	8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0),
423 };
424 
425 /* Interface data select */
426 static const char * const rt5670_data_select[] = {
427 	"Normal", "Swap", "left copy to right", "right copy to left"
428 };
429 
430 static SOC_ENUM_SINGLE_DECL(rt5670_if2_dac_enum, RT5670_DIG_INF1_DATA,
431 				RT5670_IF2_DAC_SEL_SFT, rt5670_data_select);
432 
433 static SOC_ENUM_SINGLE_DECL(rt5670_if2_adc_enum, RT5670_DIG_INF1_DATA,
434 				RT5670_IF2_ADC_SEL_SFT, rt5670_data_select);
435 
436 static const struct snd_kcontrol_new rt5670_snd_controls[] = {
437 	/* Headphone Output Volume */
438 	SOC_DOUBLE("HP Playback Switch", RT5670_HP_VOL,
439 		RT5670_L_MUTE_SFT, RT5670_R_MUTE_SFT, 1, 1),
440 	SOC_DOUBLE_TLV("HP Playback Volume", RT5670_HP_VOL,
441 		RT5670_L_VOL_SFT, RT5670_R_VOL_SFT,
442 		39, 0, out_vol_tlv),
443 	/* OUTPUT Control */
444 	SOC_DOUBLE("OUT Channel Switch", RT5670_LOUT1,
445 		RT5670_VOL_L_SFT, RT5670_VOL_R_SFT, 1, 1),
446 	SOC_DOUBLE_TLV("OUT Playback Volume", RT5670_LOUT1,
447 		RT5670_L_VOL_SFT, RT5670_R_VOL_SFT, 39, 1, out_vol_tlv),
448 	/* DAC Digital Volume */
449 	SOC_DOUBLE("DAC2 Playback Switch", RT5670_DAC_CTRL,
450 		RT5670_M_DAC_L2_VOL_SFT, RT5670_M_DAC_R2_VOL_SFT, 1, 1),
451 	SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5670_DAC1_DIG_VOL,
452 			RT5670_L_VOL_SFT, RT5670_R_VOL_SFT,
453 			175, 0, dac_vol_tlv),
454 	SOC_DOUBLE_TLV("Mono DAC Playback Volume", RT5670_DAC2_DIG_VOL,
455 			RT5670_L_VOL_SFT, RT5670_R_VOL_SFT,
456 			175, 0, dac_vol_tlv),
457 	/* IN1/IN2 Control */
458 	SOC_SINGLE_TLV("IN1 Boost Volume", RT5670_CJ_CTRL1,
459 		RT5670_BST_SFT1, 8, 0, bst_tlv),
460 	SOC_SINGLE_TLV("IN2 Boost Volume", RT5670_IN2,
461 		RT5670_BST_SFT1, 8, 0, bst_tlv),
462 	/* INL/INR Volume Control */
463 	SOC_DOUBLE_TLV("IN Capture Volume", RT5670_INL1_INR1_VOL,
464 			RT5670_INL_VOL_SFT, RT5670_INR_VOL_SFT,
465 			31, 1, in_vol_tlv),
466 	/* ADC Digital Volume Control */
467 	SOC_DOUBLE("ADC Capture Switch", RT5670_STO1_ADC_DIG_VOL,
468 		RT5670_L_MUTE_SFT, RT5670_R_MUTE_SFT, 1, 1),
469 	SOC_DOUBLE_TLV("ADC Capture Volume", RT5670_STO1_ADC_DIG_VOL,
470 			RT5670_L_VOL_SFT, RT5670_R_VOL_SFT,
471 			127, 0, adc_vol_tlv),
472 
473 	SOC_DOUBLE_TLV("Mono ADC Capture Volume", RT5670_MONO_ADC_DIG_VOL,
474 			RT5670_L_VOL_SFT, RT5670_R_VOL_SFT,
475 			127, 0, adc_vol_tlv),
476 
477 	/* ADC Boost Volume Control */
478 	SOC_DOUBLE_TLV("STO1 ADC Boost Gain Volume", RT5670_ADC_BST_VOL1,
479 			RT5670_STO1_ADC_L_BST_SFT, RT5670_STO1_ADC_R_BST_SFT,
480 			3, 0, adc_bst_tlv),
481 
482 	SOC_DOUBLE_TLV("STO2 ADC Boost Gain Volume", RT5670_ADC_BST_VOL1,
483 			RT5670_STO2_ADC_L_BST_SFT, RT5670_STO2_ADC_R_BST_SFT,
484 			3, 0, adc_bst_tlv),
485 
486 	SOC_ENUM("ADC IF2 Data Switch", rt5670_if2_adc_enum),
487 	SOC_ENUM("DAC IF2 Data Switch", rt5670_if2_dac_enum),
488 };
489 
490 /**
491  * set_dmic_clk - Set parameter of dmic.
492  *
493  * @w: DAPM widget.
494  * @kcontrol: The kcontrol of this widget.
495  * @event: Event id.
496  *
497  * Choose dmic clock between 1MHz and 3MHz.
498  * It is better for clock to approximate 3MHz.
499  */
500 static int set_dmic_clk(struct snd_soc_dapm_widget *w,
501 	struct snd_kcontrol *kcontrol, int event)
502 {
503 	struct snd_soc_codec *codec = w->codec;
504 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
505 	int idx = -EINVAL;
506 
507 	idx = rl6231_calc_dmic_clk(rt5670->sysclk);
508 
509 	if (idx < 0)
510 		dev_err(codec->dev, "Failed to set DMIC clock\n");
511 	else
512 		snd_soc_update_bits(codec, RT5670_DMIC_CTRL1,
513 			RT5670_DMIC_CLK_MASK, idx << RT5670_DMIC_CLK_SFT);
514 	return idx;
515 }
516 
517 static int is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
518 			 struct snd_soc_dapm_widget *sink)
519 {
520 	unsigned int val;
521 
522 	val = snd_soc_read(source->codec, RT5670_GLB_CLK);
523 	val &= RT5670_SCLK_SRC_MASK;
524 	if (val == RT5670_SCLK_SRC_PLL1)
525 		return 1;
526 	else
527 		return 0;
528 }
529 
530 static int is_using_asrc(struct snd_soc_dapm_widget *source,
531 			 struct snd_soc_dapm_widget *sink)
532 {
533 	unsigned int reg, shift, val;
534 
535 	switch (source->shift) {
536 	case 0:
537 		reg = RT5670_ASRC_3;
538 		shift = 0;
539 		break;
540 	case 1:
541 		reg = RT5670_ASRC_3;
542 		shift = 4;
543 		break;
544 	case 2:
545 		reg = RT5670_ASRC_5;
546 		shift = 12;
547 		break;
548 	case 3:
549 		reg = RT5670_ASRC_2;
550 		shift = 0;
551 		break;
552 	case 8:
553 		reg = RT5670_ASRC_2;
554 		shift = 4;
555 		break;
556 	case 9:
557 		reg = RT5670_ASRC_2;
558 		shift = 8;
559 		break;
560 	case 10:
561 		reg = RT5670_ASRC_2;
562 		shift = 12;
563 		break;
564 	default:
565 		return 0;
566 	}
567 
568 	val = (snd_soc_read(source->codec, reg) >> shift) & 0xf;
569 	switch (val) {
570 	case 1:
571 	case 2:
572 	case 3:
573 	case 4:
574 		return 1;
575 	default:
576 		return 0;
577 	}
578 
579 }
580 
581 static int can_use_asrc(struct snd_soc_dapm_widget *source,
582 			 struct snd_soc_dapm_widget *sink)
583 {
584 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
585 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
586 
587 	if (rt5670->sysclk > rt5670->lrck[RT5670_AIF1] * 384)
588 		return 1;
589 
590 	return 0;
591 }
592 
593 /* Digital Mixer */
594 static const struct snd_kcontrol_new rt5670_sto1_adc_l_mix[] = {
595 	SOC_DAPM_SINGLE("ADC1 Switch", RT5670_STO1_ADC_MIXER,
596 			RT5670_M_ADC_L1_SFT, 1, 1),
597 	SOC_DAPM_SINGLE("ADC2 Switch", RT5670_STO1_ADC_MIXER,
598 			RT5670_M_ADC_L2_SFT, 1, 1),
599 };
600 
601 static const struct snd_kcontrol_new rt5670_sto1_adc_r_mix[] = {
602 	SOC_DAPM_SINGLE("ADC1 Switch", RT5670_STO1_ADC_MIXER,
603 			RT5670_M_ADC_R1_SFT, 1, 1),
604 	SOC_DAPM_SINGLE("ADC2 Switch", RT5670_STO1_ADC_MIXER,
605 			RT5670_M_ADC_R2_SFT, 1, 1),
606 };
607 
608 static const struct snd_kcontrol_new rt5670_sto2_adc_l_mix[] = {
609 	SOC_DAPM_SINGLE("ADC1 Switch", RT5670_STO2_ADC_MIXER,
610 			RT5670_M_ADC_L1_SFT, 1, 1),
611 	SOC_DAPM_SINGLE("ADC2 Switch", RT5670_STO2_ADC_MIXER,
612 			RT5670_M_ADC_L2_SFT, 1, 1),
613 };
614 
615 static const struct snd_kcontrol_new rt5670_sto2_adc_r_mix[] = {
616 	SOC_DAPM_SINGLE("ADC1 Switch", RT5670_STO2_ADC_MIXER,
617 			RT5670_M_ADC_R1_SFT, 1, 1),
618 	SOC_DAPM_SINGLE("ADC2 Switch", RT5670_STO2_ADC_MIXER,
619 			RT5670_M_ADC_R2_SFT, 1, 1),
620 };
621 
622 static const struct snd_kcontrol_new rt5670_mono_adc_l_mix[] = {
623 	SOC_DAPM_SINGLE("ADC1 Switch", RT5670_MONO_ADC_MIXER,
624 			RT5670_M_MONO_ADC_L1_SFT, 1, 1),
625 	SOC_DAPM_SINGLE("ADC2 Switch", RT5670_MONO_ADC_MIXER,
626 			RT5670_M_MONO_ADC_L2_SFT, 1, 1),
627 };
628 
629 static const struct snd_kcontrol_new rt5670_mono_adc_r_mix[] = {
630 	SOC_DAPM_SINGLE("ADC1 Switch", RT5670_MONO_ADC_MIXER,
631 			RT5670_M_MONO_ADC_R1_SFT, 1, 1),
632 	SOC_DAPM_SINGLE("ADC2 Switch", RT5670_MONO_ADC_MIXER,
633 			RT5670_M_MONO_ADC_R2_SFT, 1, 1),
634 };
635 
636 static const struct snd_kcontrol_new rt5670_dac_l_mix[] = {
637 	SOC_DAPM_SINGLE("Stereo ADC Switch", RT5670_AD_DA_MIXER,
638 			RT5670_M_ADCMIX_L_SFT, 1, 1),
639 	SOC_DAPM_SINGLE("DAC1 Switch", RT5670_AD_DA_MIXER,
640 			RT5670_M_DAC1_L_SFT, 1, 1),
641 };
642 
643 static const struct snd_kcontrol_new rt5670_dac_r_mix[] = {
644 	SOC_DAPM_SINGLE("Stereo ADC Switch", RT5670_AD_DA_MIXER,
645 			RT5670_M_ADCMIX_R_SFT, 1, 1),
646 	SOC_DAPM_SINGLE("DAC1 Switch", RT5670_AD_DA_MIXER,
647 			RT5670_M_DAC1_R_SFT, 1, 1),
648 };
649 
650 static const struct snd_kcontrol_new rt5670_sto_dac_l_mix[] = {
651 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5670_STO_DAC_MIXER,
652 			RT5670_M_DAC_L1_SFT, 1, 1),
653 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5670_STO_DAC_MIXER,
654 			RT5670_M_DAC_L2_SFT, 1, 1),
655 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5670_STO_DAC_MIXER,
656 			RT5670_M_DAC_R1_STO_L_SFT, 1, 1),
657 };
658 
659 static const struct snd_kcontrol_new rt5670_sto_dac_r_mix[] = {
660 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5670_STO_DAC_MIXER,
661 			RT5670_M_DAC_R1_SFT, 1, 1),
662 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5670_STO_DAC_MIXER,
663 			RT5670_M_DAC_R2_SFT, 1, 1),
664 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5670_STO_DAC_MIXER,
665 			RT5670_M_DAC_L1_STO_R_SFT, 1, 1),
666 };
667 
668 static const struct snd_kcontrol_new rt5670_mono_dac_l_mix[] = {
669 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5670_DD_MIXER,
670 			RT5670_M_DAC_L1_MONO_L_SFT, 1, 1),
671 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5670_DD_MIXER,
672 			RT5670_M_DAC_L2_MONO_L_SFT, 1, 1),
673 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5670_DD_MIXER,
674 			RT5670_M_DAC_R2_MONO_L_SFT, 1, 1),
675 };
676 
677 static const struct snd_kcontrol_new rt5670_mono_dac_r_mix[] = {
678 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5670_DD_MIXER,
679 			RT5670_M_DAC_R1_MONO_R_SFT, 1, 1),
680 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5670_DD_MIXER,
681 			RT5670_M_DAC_R2_MONO_R_SFT, 1, 1),
682 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5670_DD_MIXER,
683 			RT5670_M_DAC_L2_MONO_R_SFT, 1, 1),
684 };
685 
686 static const struct snd_kcontrol_new rt5670_dig_l_mix[] = {
687 	SOC_DAPM_SINGLE("Sto DAC Mix L Switch", RT5670_DIG_MIXER,
688 			RT5670_M_STO_L_DAC_L_SFT, 1, 1),
689 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5670_DIG_MIXER,
690 			RT5670_M_DAC_L2_DAC_L_SFT, 1, 1),
691 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5670_DIG_MIXER,
692 			RT5670_M_DAC_R2_DAC_L_SFT, 1, 1),
693 };
694 
695 static const struct snd_kcontrol_new rt5670_dig_r_mix[] = {
696 	SOC_DAPM_SINGLE("Sto DAC Mix R Switch", RT5670_DIG_MIXER,
697 			RT5670_M_STO_R_DAC_R_SFT, 1, 1),
698 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5670_DIG_MIXER,
699 			RT5670_M_DAC_R2_DAC_R_SFT, 1, 1),
700 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5670_DIG_MIXER,
701 			RT5670_M_DAC_L2_DAC_R_SFT, 1, 1),
702 };
703 
704 /* Analog Input Mixer */
705 static const struct snd_kcontrol_new rt5670_rec_l_mix[] = {
706 	SOC_DAPM_SINGLE("INL Switch", RT5670_REC_L2_MIXER,
707 			RT5670_M_IN_L_RM_L_SFT, 1, 1),
708 	SOC_DAPM_SINGLE("BST2 Switch", RT5670_REC_L2_MIXER,
709 			RT5670_M_BST2_RM_L_SFT, 1, 1),
710 	SOC_DAPM_SINGLE("BST1 Switch", RT5670_REC_L2_MIXER,
711 			RT5670_M_BST1_RM_L_SFT, 1, 1),
712 };
713 
714 static const struct snd_kcontrol_new rt5670_rec_r_mix[] = {
715 	SOC_DAPM_SINGLE("INR Switch", RT5670_REC_R2_MIXER,
716 			RT5670_M_IN_R_RM_R_SFT, 1, 1),
717 	SOC_DAPM_SINGLE("BST2 Switch", RT5670_REC_R2_MIXER,
718 			RT5670_M_BST2_RM_R_SFT, 1, 1),
719 	SOC_DAPM_SINGLE("BST1 Switch", RT5670_REC_R2_MIXER,
720 			RT5670_M_BST1_RM_R_SFT, 1, 1),
721 };
722 
723 static const struct snd_kcontrol_new rt5670_out_l_mix[] = {
724 	SOC_DAPM_SINGLE("BST1 Switch", RT5670_OUT_L1_MIXER,
725 			RT5670_M_BST1_OM_L_SFT, 1, 1),
726 	SOC_DAPM_SINGLE("INL Switch", RT5670_OUT_L1_MIXER,
727 			RT5670_M_IN_L_OM_L_SFT, 1, 1),
728 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5670_OUT_L1_MIXER,
729 			RT5670_M_DAC_L2_OM_L_SFT, 1, 1),
730 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5670_OUT_L1_MIXER,
731 			RT5670_M_DAC_L1_OM_L_SFT, 1, 1),
732 };
733 
734 static const struct snd_kcontrol_new rt5670_out_r_mix[] = {
735 	SOC_DAPM_SINGLE("BST2 Switch", RT5670_OUT_R1_MIXER,
736 			RT5670_M_BST2_OM_R_SFT, 1, 1),
737 	SOC_DAPM_SINGLE("INR Switch", RT5670_OUT_R1_MIXER,
738 			RT5670_M_IN_R_OM_R_SFT, 1, 1),
739 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5670_OUT_R1_MIXER,
740 			RT5670_M_DAC_R2_OM_R_SFT, 1, 1),
741 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5670_OUT_R1_MIXER,
742 			RT5670_M_DAC_R1_OM_R_SFT, 1, 1),
743 };
744 
745 static const struct snd_kcontrol_new rt5670_hpo_mix[] = {
746 	SOC_DAPM_SINGLE("DAC1 Switch", RT5670_HPO_MIXER,
747 			RT5670_M_DAC1_HM_SFT, 1, 1),
748 	SOC_DAPM_SINGLE("HPVOL Switch", RT5670_HPO_MIXER,
749 			RT5670_M_HPVOL_HM_SFT, 1, 1),
750 };
751 
752 static const struct snd_kcontrol_new rt5670_hpvoll_mix[] = {
753 	SOC_DAPM_SINGLE("DAC1 Switch", RT5670_HPO_MIXER,
754 			RT5670_M_DACL1_HML_SFT, 1, 1),
755 	SOC_DAPM_SINGLE("INL Switch", RT5670_HPO_MIXER,
756 			RT5670_M_INL1_HML_SFT, 1, 1),
757 };
758 
759 static const struct snd_kcontrol_new rt5670_hpvolr_mix[] = {
760 	SOC_DAPM_SINGLE("DAC1 Switch", RT5670_HPO_MIXER,
761 			RT5670_M_DACR1_HMR_SFT, 1, 1),
762 	SOC_DAPM_SINGLE("INR Switch", RT5670_HPO_MIXER,
763 			RT5670_M_INR1_HMR_SFT, 1, 1),
764 };
765 
766 static const struct snd_kcontrol_new rt5670_lout_mix[] = {
767 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5670_LOUT_MIXER,
768 			RT5670_M_DAC_L1_LM_SFT, 1, 1),
769 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5670_LOUT_MIXER,
770 			RT5670_M_DAC_R1_LM_SFT, 1, 1),
771 	SOC_DAPM_SINGLE("OUTMIX L Switch", RT5670_LOUT_MIXER,
772 			RT5670_M_OV_L_LM_SFT, 1, 1),
773 	SOC_DAPM_SINGLE("OUTMIX R Switch", RT5670_LOUT_MIXER,
774 			RT5670_M_OV_R_LM_SFT, 1, 1),
775 };
776 
777 static const struct snd_kcontrol_new rt5670_hpl_mix[] = {
778 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5670_HPO_MIXER,
779 			RT5670_M_DACL1_HML_SFT, 1, 1),
780 	SOC_DAPM_SINGLE("INL1 Switch", RT5670_HPO_MIXER,
781 			RT5670_M_INL1_HML_SFT, 1, 1),
782 };
783 
784 static const struct snd_kcontrol_new rt5670_hpr_mix[] = {
785 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5670_HPO_MIXER,
786 			RT5670_M_DACR1_HMR_SFT, 1, 1),
787 	SOC_DAPM_SINGLE("INR1 Switch", RT5670_HPO_MIXER,
788 			RT5670_M_INR1_HMR_SFT, 1, 1),
789 };
790 
791 static const struct snd_kcontrol_new lout_l_enable_control =
792 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5670_LOUT1,
793 		RT5670_L_MUTE_SFT, 1, 1);
794 
795 static const struct snd_kcontrol_new lout_r_enable_control =
796 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5670_LOUT1,
797 		RT5670_R_MUTE_SFT, 1, 1);
798 
799 /* DAC1 L/R source */ /* MX-29 [9:8] [11:10] */
800 static const char * const rt5670_dac1_src[] = {
801 	"IF1 DAC", "IF2 DAC"
802 };
803 
804 static SOC_ENUM_SINGLE_DECL(rt5670_dac1l_enum, RT5670_AD_DA_MIXER,
805 	RT5670_DAC1_L_SEL_SFT, rt5670_dac1_src);
806 
807 static const struct snd_kcontrol_new rt5670_dac1l_mux =
808 	SOC_DAPM_ENUM("DAC1 L source", rt5670_dac1l_enum);
809 
810 static SOC_ENUM_SINGLE_DECL(rt5670_dac1r_enum, RT5670_AD_DA_MIXER,
811 	RT5670_DAC1_R_SEL_SFT, rt5670_dac1_src);
812 
813 static const struct snd_kcontrol_new rt5670_dac1r_mux =
814 	SOC_DAPM_ENUM("DAC1 R source", rt5670_dac1r_enum);
815 
816 /*DAC2 L/R source*/ /* MX-1B [6:4] [2:0] */
817 /* TODO Use SOC_VALUE_ENUM_SINGLE_DECL */
818 static const char * const rt5670_dac12_src[] = {
819 	"IF1 DAC", "IF2 DAC", "IF3 DAC", "TxDC DAC",
820 	"Bass", "VAD_ADC", "IF4 DAC"
821 };
822 
823 static SOC_ENUM_SINGLE_DECL(rt5670_dac2l_enum, RT5670_DAC_CTRL,
824 	RT5670_DAC2_L_SEL_SFT, rt5670_dac12_src);
825 
826 static const struct snd_kcontrol_new rt5670_dac_l2_mux =
827 	SOC_DAPM_ENUM("DAC2 L source", rt5670_dac2l_enum);
828 
829 static const char * const rt5670_dacr2_src[] = {
830 	"IF1 DAC", "IF2 DAC", "IF3 DAC", "TxDC DAC", "TxDP ADC", "IF4 DAC"
831 };
832 
833 static SOC_ENUM_SINGLE_DECL(rt5670_dac2r_enum, RT5670_DAC_CTRL,
834 	RT5670_DAC2_R_SEL_SFT, rt5670_dacr2_src);
835 
836 static const struct snd_kcontrol_new rt5670_dac_r2_mux =
837 	SOC_DAPM_ENUM("DAC2 R source", rt5670_dac2r_enum);
838 
839 /*RxDP source*/ /* MX-2D [15:13] */
840 static const char * const rt5670_rxdp_src[] = {
841 	"IF2 DAC", "IF1 DAC", "STO1 ADC Mixer", "STO2 ADC Mixer",
842 	"Mono ADC Mixer L", "Mono ADC Mixer R", "DAC1"
843 };
844 
845 static SOC_ENUM_SINGLE_DECL(rt5670_rxdp_enum, RT5670_DSP_PATH1,
846 	RT5670_RXDP_SEL_SFT, rt5670_rxdp_src);
847 
848 static const struct snd_kcontrol_new rt5670_rxdp_mux =
849 	SOC_DAPM_ENUM("DAC2 L source", rt5670_rxdp_enum);
850 
851 /* MX-2D [1] [0] */
852 static const char * const rt5670_dsp_bypass_src[] = {
853 	"DSP", "Bypass"
854 };
855 
856 static SOC_ENUM_SINGLE_DECL(rt5670_dsp_ul_enum, RT5670_DSP_PATH1,
857 	RT5670_DSP_UL_SFT, rt5670_dsp_bypass_src);
858 
859 static const struct snd_kcontrol_new rt5670_dsp_ul_mux =
860 	SOC_DAPM_ENUM("DSP UL source", rt5670_dsp_ul_enum);
861 
862 static SOC_ENUM_SINGLE_DECL(rt5670_dsp_dl_enum, RT5670_DSP_PATH1,
863 	RT5670_DSP_DL_SFT, rt5670_dsp_bypass_src);
864 
865 static const struct snd_kcontrol_new rt5670_dsp_dl_mux =
866 	SOC_DAPM_ENUM("DSP DL source", rt5670_dsp_dl_enum);
867 
868 /* Stereo2 ADC source */
869 /* MX-26 [15] */
870 static const char * const rt5670_stereo2_adc_lr_src[] = {
871 	"L", "LR"
872 };
873 
874 static SOC_ENUM_SINGLE_DECL(rt5670_stereo2_adc_lr_enum, RT5670_STO2_ADC_MIXER,
875 	RT5670_STO2_ADC_SRC_SFT, rt5670_stereo2_adc_lr_src);
876 
877 static const struct snd_kcontrol_new rt5670_sto2_adc_lr_mux =
878 	SOC_DAPM_ENUM("Stereo2 ADC LR source", rt5670_stereo2_adc_lr_enum);
879 
880 /* Stereo1 ADC source */
881 /* MX-27 MX-26 [12] */
882 static const char * const rt5670_stereo_adc1_src[] = {
883 	"DAC MIX", "ADC"
884 };
885 
886 static SOC_ENUM_SINGLE_DECL(rt5670_stereo1_adc1_enum, RT5670_STO1_ADC_MIXER,
887 	RT5670_ADC_1_SRC_SFT, rt5670_stereo_adc1_src);
888 
889 static const struct snd_kcontrol_new rt5670_sto_adc_l1_mux =
890 	SOC_DAPM_ENUM("Stereo1 ADC L1 source", rt5670_stereo1_adc1_enum);
891 
892 static const struct snd_kcontrol_new rt5670_sto_adc_r1_mux =
893 	SOC_DAPM_ENUM("Stereo1 ADC R1 source", rt5670_stereo1_adc1_enum);
894 
895 static SOC_ENUM_SINGLE_DECL(rt5670_stereo2_adc1_enum, RT5670_STO2_ADC_MIXER,
896 	RT5670_ADC_1_SRC_SFT, rt5670_stereo_adc1_src);
897 
898 static const struct snd_kcontrol_new rt5670_sto2_adc_l1_mux =
899 	SOC_DAPM_ENUM("Stereo2 ADC L1 source", rt5670_stereo2_adc1_enum);
900 
901 static const struct snd_kcontrol_new rt5670_sto2_adc_r1_mux =
902 	SOC_DAPM_ENUM("Stereo2 ADC R1 source", rt5670_stereo2_adc1_enum);
903 
904 /* MX-27 MX-26 [11] */
905 static const char * const rt5670_stereo_adc2_src[] = {
906 	"DAC MIX", "DMIC"
907 };
908 
909 static SOC_ENUM_SINGLE_DECL(rt5670_stereo1_adc2_enum, RT5670_STO1_ADC_MIXER,
910 	RT5670_ADC_2_SRC_SFT, rt5670_stereo_adc2_src);
911 
912 static const struct snd_kcontrol_new rt5670_sto_adc_l2_mux =
913 	SOC_DAPM_ENUM("Stereo1 ADC L2 source", rt5670_stereo1_adc2_enum);
914 
915 static const struct snd_kcontrol_new rt5670_sto_adc_r2_mux =
916 	SOC_DAPM_ENUM("Stereo1 ADC R2 source", rt5670_stereo1_adc2_enum);
917 
918 static SOC_ENUM_SINGLE_DECL(rt5670_stereo2_adc2_enum, RT5670_STO2_ADC_MIXER,
919 	RT5670_ADC_2_SRC_SFT, rt5670_stereo_adc2_src);
920 
921 static const struct snd_kcontrol_new rt5670_sto2_adc_l2_mux =
922 	SOC_DAPM_ENUM("Stereo2 ADC L2 source", rt5670_stereo2_adc2_enum);
923 
924 static const struct snd_kcontrol_new rt5670_sto2_adc_r2_mux =
925 	SOC_DAPM_ENUM("Stereo2 ADC R2 source", rt5670_stereo2_adc2_enum);
926 
927 /* MX-27 MX26 [10] */
928 static const char * const rt5670_stereo_adc_src[] = {
929 	"ADC1L ADC2R", "ADC3"
930 };
931 
932 static SOC_ENUM_SINGLE_DECL(rt5670_stereo1_adc_enum, RT5670_STO1_ADC_MIXER,
933 	RT5670_ADC_SRC_SFT, rt5670_stereo_adc_src);
934 
935 static const struct snd_kcontrol_new rt5670_sto_adc_mux =
936 	SOC_DAPM_ENUM("Stereo1 ADC source", rt5670_stereo1_adc_enum);
937 
938 static SOC_ENUM_SINGLE_DECL(rt5670_stereo2_adc_enum, RT5670_STO2_ADC_MIXER,
939 	RT5670_ADC_SRC_SFT, rt5670_stereo_adc_src);
940 
941 static const struct snd_kcontrol_new rt5670_sto2_adc_mux =
942 	SOC_DAPM_ENUM("Stereo2 ADC source", rt5670_stereo2_adc_enum);
943 
944 /* MX-27 MX-26 [9:8] */
945 static const char * const rt5670_stereo_dmic_src[] = {
946 	"DMIC1", "DMIC2", "DMIC3"
947 };
948 
949 static SOC_ENUM_SINGLE_DECL(rt5670_stereo1_dmic_enum, RT5670_STO1_ADC_MIXER,
950 	RT5670_DMIC_SRC_SFT, rt5670_stereo_dmic_src);
951 
952 static const struct snd_kcontrol_new rt5670_sto1_dmic_mux =
953 	SOC_DAPM_ENUM("Stereo1 DMIC source", rt5670_stereo1_dmic_enum);
954 
955 static SOC_ENUM_SINGLE_DECL(rt5670_stereo2_dmic_enum, RT5670_STO2_ADC_MIXER,
956 	RT5670_DMIC_SRC_SFT, rt5670_stereo_dmic_src);
957 
958 static const struct snd_kcontrol_new rt5670_sto2_dmic_mux =
959 	SOC_DAPM_ENUM("Stereo2 DMIC source", rt5670_stereo2_dmic_enum);
960 
961 /* MX-27 [0] */
962 static const char * const rt5670_stereo_dmic3_src[] = {
963 	"DMIC3", "PDM ADC"
964 };
965 
966 static SOC_ENUM_SINGLE_DECL(rt5670_stereo_dmic3_enum, RT5670_STO1_ADC_MIXER,
967 	RT5670_DMIC3_SRC_SFT, rt5670_stereo_dmic3_src);
968 
969 static const struct snd_kcontrol_new rt5670_sto_dmic3_mux =
970 	SOC_DAPM_ENUM("Stereo DMIC3 source", rt5670_stereo_dmic3_enum);
971 
972 /* Mono ADC source */
973 /* MX-28 [12] */
974 static const char * const rt5670_mono_adc_l1_src[] = {
975 	"Mono DAC MIXL", "ADC1"
976 };
977 
978 static SOC_ENUM_SINGLE_DECL(rt5670_mono_adc_l1_enum, RT5670_MONO_ADC_MIXER,
979 	RT5670_MONO_ADC_L1_SRC_SFT, rt5670_mono_adc_l1_src);
980 
981 static const struct snd_kcontrol_new rt5670_mono_adc_l1_mux =
982 	SOC_DAPM_ENUM("Mono ADC1 left source", rt5670_mono_adc_l1_enum);
983 /* MX-28 [11] */
984 static const char * const rt5670_mono_adc_l2_src[] = {
985 	"Mono DAC MIXL", "DMIC"
986 };
987 
988 static SOC_ENUM_SINGLE_DECL(rt5670_mono_adc_l2_enum, RT5670_MONO_ADC_MIXER,
989 	RT5670_MONO_ADC_L2_SRC_SFT, rt5670_mono_adc_l2_src);
990 
991 static const struct snd_kcontrol_new rt5670_mono_adc_l2_mux =
992 	SOC_DAPM_ENUM("Mono ADC2 left source", rt5670_mono_adc_l2_enum);
993 
994 /* MX-28 [9:8] */
995 static const char * const rt5670_mono_dmic_src[] = {
996 	"DMIC1", "DMIC2", "DMIC3"
997 };
998 
999 static SOC_ENUM_SINGLE_DECL(rt5670_mono_dmic_l_enum, RT5670_MONO_ADC_MIXER,
1000 	RT5670_MONO_DMIC_L_SRC_SFT, rt5670_mono_dmic_src);
1001 
1002 static const struct snd_kcontrol_new rt5670_mono_dmic_l_mux =
1003 	SOC_DAPM_ENUM("Mono DMIC left source", rt5670_mono_dmic_l_enum);
1004 /* MX-28 [1:0] */
1005 static SOC_ENUM_SINGLE_DECL(rt5670_mono_dmic_r_enum, RT5670_MONO_ADC_MIXER,
1006 	RT5670_MONO_DMIC_R_SRC_SFT, rt5670_mono_dmic_src);
1007 
1008 static const struct snd_kcontrol_new rt5670_mono_dmic_r_mux =
1009 	SOC_DAPM_ENUM("Mono DMIC Right source", rt5670_mono_dmic_r_enum);
1010 /* MX-28 [4] */
1011 static const char * const rt5670_mono_adc_r1_src[] = {
1012 	"Mono DAC MIXR", "ADC2"
1013 };
1014 
1015 static SOC_ENUM_SINGLE_DECL(rt5670_mono_adc_r1_enum, RT5670_MONO_ADC_MIXER,
1016 	RT5670_MONO_ADC_R1_SRC_SFT, rt5670_mono_adc_r1_src);
1017 
1018 static const struct snd_kcontrol_new rt5670_mono_adc_r1_mux =
1019 	SOC_DAPM_ENUM("Mono ADC1 right source", rt5670_mono_adc_r1_enum);
1020 /* MX-28 [3] */
1021 static const char * const rt5670_mono_adc_r2_src[] = {
1022 	"Mono DAC MIXR", "DMIC"
1023 };
1024 
1025 static SOC_ENUM_SINGLE_DECL(rt5670_mono_adc_r2_enum, RT5670_MONO_ADC_MIXER,
1026 	RT5670_MONO_ADC_R2_SRC_SFT, rt5670_mono_adc_r2_src);
1027 
1028 static const struct snd_kcontrol_new rt5670_mono_adc_r2_mux =
1029 	SOC_DAPM_ENUM("Mono ADC2 right source", rt5670_mono_adc_r2_enum);
1030 
1031 /* MX-2D [3:2] */
1032 static const char * const rt5670_txdp_slot_src[] = {
1033 	"Slot 0-1", "Slot 2-3", "Slot 4-5", "Slot 6-7"
1034 };
1035 
1036 static SOC_ENUM_SINGLE_DECL(rt5670_txdp_slot_enum, RT5670_DSP_PATH1,
1037 	RT5670_TXDP_SLOT_SEL_SFT, rt5670_txdp_slot_src);
1038 
1039 static const struct snd_kcontrol_new rt5670_txdp_slot_mux =
1040 	SOC_DAPM_ENUM("TxDP Slot source", rt5670_txdp_slot_enum);
1041 
1042 /* MX-2F [15] */
1043 static const char * const rt5670_if1_adc2_in_src[] = {
1044 	"IF_ADC2", "VAD_ADC"
1045 };
1046 
1047 static SOC_ENUM_SINGLE_DECL(rt5670_if1_adc2_in_enum, RT5670_DIG_INF1_DATA,
1048 	RT5670_IF1_ADC2_IN_SFT, rt5670_if1_adc2_in_src);
1049 
1050 static const struct snd_kcontrol_new rt5670_if1_adc2_in_mux =
1051 	SOC_DAPM_ENUM("IF1 ADC2 IN source", rt5670_if1_adc2_in_enum);
1052 
1053 /* MX-2F [14:12] */
1054 static const char * const rt5670_if2_adc_in_src[] = {
1055 	"IF_ADC1", "IF_ADC2", "IF_ADC3", "TxDC_DAC", "TxDP_ADC", "VAD_ADC"
1056 };
1057 
1058 static SOC_ENUM_SINGLE_DECL(rt5670_if2_adc_in_enum, RT5670_DIG_INF1_DATA,
1059 	RT5670_IF2_ADC_IN_SFT, rt5670_if2_adc_in_src);
1060 
1061 static const struct snd_kcontrol_new rt5670_if2_adc_in_mux =
1062 	SOC_DAPM_ENUM("IF2 ADC IN source", rt5670_if2_adc_in_enum);
1063 
1064 /* MX-30 [5:4] */
1065 static const char * const rt5670_if4_adc_in_src[] = {
1066 	"IF_ADC1", "IF_ADC2", "IF_ADC3"
1067 };
1068 
1069 static SOC_ENUM_SINGLE_DECL(rt5670_if4_adc_in_enum, RT5670_DIG_INF2_DATA,
1070 	RT5670_IF4_ADC_IN_SFT, rt5670_if4_adc_in_src);
1071 
1072 static const struct snd_kcontrol_new rt5670_if4_adc_in_mux =
1073 	SOC_DAPM_ENUM("IF4 ADC IN source", rt5670_if4_adc_in_enum);
1074 
1075 /* MX-31 [15] [13] [11] [9] */
1076 static const char * const rt5670_pdm_src[] = {
1077 	"Mono DAC", "Stereo DAC"
1078 };
1079 
1080 static SOC_ENUM_SINGLE_DECL(rt5670_pdm1_l_enum, RT5670_PDM_OUT_CTRL,
1081 	RT5670_PDM1_L_SFT, rt5670_pdm_src);
1082 
1083 static const struct snd_kcontrol_new rt5670_pdm1_l_mux =
1084 	SOC_DAPM_ENUM("PDM1 L source", rt5670_pdm1_l_enum);
1085 
1086 static SOC_ENUM_SINGLE_DECL(rt5670_pdm1_r_enum, RT5670_PDM_OUT_CTRL,
1087 	RT5670_PDM1_R_SFT, rt5670_pdm_src);
1088 
1089 static const struct snd_kcontrol_new rt5670_pdm1_r_mux =
1090 	SOC_DAPM_ENUM("PDM1 R source", rt5670_pdm1_r_enum);
1091 
1092 static SOC_ENUM_SINGLE_DECL(rt5670_pdm2_l_enum, RT5670_PDM_OUT_CTRL,
1093 	RT5670_PDM2_L_SFT, rt5670_pdm_src);
1094 
1095 static const struct snd_kcontrol_new rt5670_pdm2_l_mux =
1096 	SOC_DAPM_ENUM("PDM2 L source", rt5670_pdm2_l_enum);
1097 
1098 static SOC_ENUM_SINGLE_DECL(rt5670_pdm2_r_enum, RT5670_PDM_OUT_CTRL,
1099 	RT5670_PDM2_R_SFT, rt5670_pdm_src);
1100 
1101 static const struct snd_kcontrol_new rt5670_pdm2_r_mux =
1102 	SOC_DAPM_ENUM("PDM2 R source", rt5670_pdm2_r_enum);
1103 
1104 /* MX-FA [12] */
1105 static const char * const rt5670_if1_adc1_in1_src[] = {
1106 	"IF_ADC1", "IF1_ADC3"
1107 };
1108 
1109 static SOC_ENUM_SINGLE_DECL(rt5670_if1_adc1_in1_enum, RT5670_DIG_MISC,
1110 	RT5670_IF1_ADC1_IN1_SFT, rt5670_if1_adc1_in1_src);
1111 
1112 static const struct snd_kcontrol_new rt5670_if1_adc1_in1_mux =
1113 	SOC_DAPM_ENUM("IF1 ADC1 IN1 source", rt5670_if1_adc1_in1_enum);
1114 
1115 /* MX-FA [11] */
1116 static const char * const rt5670_if1_adc1_in2_src[] = {
1117 	"IF1_ADC1_IN1", "IF1_ADC4"
1118 };
1119 
1120 static SOC_ENUM_SINGLE_DECL(rt5670_if1_adc1_in2_enum, RT5670_DIG_MISC,
1121 	RT5670_IF1_ADC1_IN2_SFT, rt5670_if1_adc1_in2_src);
1122 
1123 static const struct snd_kcontrol_new rt5670_if1_adc1_in2_mux =
1124 	SOC_DAPM_ENUM("IF1 ADC1 IN2 source", rt5670_if1_adc1_in2_enum);
1125 
1126 /* MX-FA [10] */
1127 static const char * const rt5670_if1_adc2_in1_src[] = {
1128 	"IF1_ADC2_IN", "IF1_ADC4"
1129 };
1130 
1131 static SOC_ENUM_SINGLE_DECL(rt5670_if1_adc2_in1_enum, RT5670_DIG_MISC,
1132 	RT5670_IF1_ADC2_IN1_SFT, rt5670_if1_adc2_in1_src);
1133 
1134 static const struct snd_kcontrol_new rt5670_if1_adc2_in1_mux =
1135 	SOC_DAPM_ENUM("IF1 ADC2 IN1 source", rt5670_if1_adc2_in1_enum);
1136 
1137 /* MX-9D [9:8] */
1138 static const char * const rt5670_vad_adc_src[] = {
1139 	"Sto1 ADC L", "Mono ADC L", "Mono ADC R", "Sto2 ADC L"
1140 };
1141 
1142 static SOC_ENUM_SINGLE_DECL(rt5670_vad_adc_enum, RT5670_VAD_CTRL4,
1143 	RT5670_VAD_SEL_SFT, rt5670_vad_adc_src);
1144 
1145 static const struct snd_kcontrol_new rt5670_vad_adc_mux =
1146 	SOC_DAPM_ENUM("VAD ADC source", rt5670_vad_adc_enum);
1147 
1148 static int rt5670_hp_power_event(struct snd_soc_dapm_widget *w,
1149 			   struct snd_kcontrol *kcontrol, int event)
1150 {
1151 	struct snd_soc_codec *codec = w->codec;
1152 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
1153 
1154 	switch (event) {
1155 	case SND_SOC_DAPM_POST_PMU:
1156 		regmap_update_bits(rt5670->regmap, RT5670_CHARGE_PUMP,
1157 			RT5670_PM_HP_MASK, RT5670_PM_HP_HV);
1158 		regmap_update_bits(rt5670->regmap, RT5670_GEN_CTRL2,
1159 			0x0400, 0x0400);
1160 		/* headphone amp power on */
1161 		regmap_update_bits(rt5670->regmap, RT5670_PWR_ANLG1,
1162 			RT5670_PWR_HA |	RT5670_PWR_FV1 |
1163 			RT5670_PWR_FV2,	RT5670_PWR_HA |
1164 			RT5670_PWR_FV1 | RT5670_PWR_FV2);
1165 		/* depop parameters */
1166 		regmap_write(rt5670->regmap, RT5670_DEPOP_M2, 0x3100);
1167 		regmap_write(rt5670->regmap, RT5670_DEPOP_M1, 0x8009);
1168 		regmap_write(rt5670->regmap, RT5670_PR_BASE +
1169 			RT5670_HP_DCC_INT1, 0x9f00);
1170 		mdelay(20);
1171 		regmap_write(rt5670->regmap, RT5670_DEPOP_M1, 0x8019);
1172 		break;
1173 	case SND_SOC_DAPM_PRE_PMD:
1174 		regmap_write(rt5670->regmap, RT5670_DEPOP_M1, 0x0004);
1175 		msleep(30);
1176 		break;
1177 	default:
1178 		return 0;
1179 	}
1180 
1181 	return 0;
1182 }
1183 
1184 static int rt5670_hp_event(struct snd_soc_dapm_widget *w,
1185 	struct snd_kcontrol *kcontrol, int event)
1186 {
1187 	struct snd_soc_codec *codec = w->codec;
1188 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
1189 
1190 	switch (event) {
1191 	case SND_SOC_DAPM_POST_PMU:
1192 		/* headphone unmute sequence */
1193 		regmap_write(rt5670->regmap, RT5670_PR_BASE +
1194 				RT5670_MAMP_INT_REG2, 0xb400);
1195 		regmap_write(rt5670->regmap, RT5670_DEPOP_M3, 0x0772);
1196 		regmap_write(rt5670->regmap, RT5670_DEPOP_M1, 0x805d);
1197 		regmap_write(rt5670->regmap, RT5670_DEPOP_M1, 0x831d);
1198 		regmap_update_bits(rt5670->regmap, RT5670_GEN_CTRL2,
1199 				0x0300, 0x0300);
1200 		regmap_update_bits(rt5670->regmap, RT5670_HP_VOL,
1201 			RT5670_L_MUTE | RT5670_R_MUTE, 0);
1202 		msleep(80);
1203 		regmap_write(rt5670->regmap, RT5670_DEPOP_M1, 0x8019);
1204 		break;
1205 
1206 	case SND_SOC_DAPM_PRE_PMD:
1207 		/* headphone mute sequence */
1208 		regmap_write(rt5670->regmap, RT5670_PR_BASE +
1209 				RT5670_MAMP_INT_REG2, 0xb400);
1210 		regmap_write(rt5670->regmap, RT5670_DEPOP_M3, 0x0772);
1211 		regmap_write(rt5670->regmap, RT5670_DEPOP_M1, 0x803d);
1212 		mdelay(10);
1213 		regmap_write(rt5670->regmap, RT5670_DEPOP_M1, 0x831d);
1214 		mdelay(10);
1215 		regmap_update_bits(rt5670->regmap, RT5670_HP_VOL,
1216 				   RT5670_L_MUTE | RT5670_R_MUTE,
1217 				   RT5670_L_MUTE | RT5670_R_MUTE);
1218 		msleep(20);
1219 		regmap_update_bits(rt5670->regmap,
1220 				   RT5670_GEN_CTRL2, 0x0300, 0x0);
1221 		regmap_write(rt5670->regmap, RT5670_DEPOP_M1, 0x8019);
1222 		regmap_write(rt5670->regmap, RT5670_DEPOP_M3, 0x0707);
1223 		regmap_write(rt5670->regmap, RT5670_PR_BASE +
1224 				RT5670_MAMP_INT_REG2, 0xfc00);
1225 		break;
1226 
1227 	default:
1228 		return 0;
1229 	}
1230 
1231 	return 0;
1232 }
1233 
1234 static int rt5670_bst1_event(struct snd_soc_dapm_widget *w,
1235 	struct snd_kcontrol *kcontrol, int event)
1236 {
1237 	struct snd_soc_codec *codec = w->codec;
1238 
1239 	switch (event) {
1240 	case SND_SOC_DAPM_POST_PMU:
1241 		snd_soc_update_bits(codec, RT5670_PWR_ANLG2,
1242 				    RT5670_PWR_BST1_P, RT5670_PWR_BST1_P);
1243 		break;
1244 
1245 	case SND_SOC_DAPM_PRE_PMD:
1246 		snd_soc_update_bits(codec, RT5670_PWR_ANLG2,
1247 				    RT5670_PWR_BST1_P, 0);
1248 		break;
1249 
1250 	default:
1251 		return 0;
1252 	}
1253 
1254 	return 0;
1255 }
1256 
1257 static int rt5670_bst2_event(struct snd_soc_dapm_widget *w,
1258 	struct snd_kcontrol *kcontrol, int event)
1259 {
1260 	struct snd_soc_codec *codec = w->codec;
1261 
1262 	switch (event) {
1263 	case SND_SOC_DAPM_POST_PMU:
1264 		snd_soc_update_bits(codec, RT5670_PWR_ANLG2,
1265 				    RT5670_PWR_BST2_P, RT5670_PWR_BST2_P);
1266 		break;
1267 
1268 	case SND_SOC_DAPM_PRE_PMD:
1269 		snd_soc_update_bits(codec, RT5670_PWR_ANLG2,
1270 				    RT5670_PWR_BST2_P, 0);
1271 		break;
1272 
1273 	default:
1274 		return 0;
1275 	}
1276 
1277 	return 0;
1278 }
1279 
1280 static const struct snd_soc_dapm_widget rt5670_dapm_widgets[] = {
1281 	SND_SOC_DAPM_SUPPLY("PLL1", RT5670_PWR_ANLG2,
1282 			    RT5670_PWR_PLL_BIT, 0, NULL, 0),
1283 	SND_SOC_DAPM_SUPPLY("I2S DSP", RT5670_PWR_DIG2,
1284 			    RT5670_PWR_I2S_DSP_BIT, 0, NULL, 0),
1285 	SND_SOC_DAPM_SUPPLY("Mic Det Power", RT5670_PWR_VOL,
1286 			    RT5670_PWR_MIC_DET_BIT, 0, NULL, 0),
1287 
1288 	/* ASRC */
1289 	SND_SOC_DAPM_SUPPLY_S("I2S1 ASRC", 1, RT5670_ASRC_1,
1290 			      11, 0, NULL, 0),
1291 	SND_SOC_DAPM_SUPPLY_S("I2S2 ASRC", 1, RT5670_ASRC_1,
1292 			      12, 0, NULL, 0),
1293 	SND_SOC_DAPM_SUPPLY_S("DAC STO ASRC", 1, RT5670_ASRC_1,
1294 			      10, 0, NULL, 0),
1295 	SND_SOC_DAPM_SUPPLY_S("DAC MONO L ASRC", 1, RT5670_ASRC_1,
1296 			      9, 0, NULL, 0),
1297 	SND_SOC_DAPM_SUPPLY_S("DAC MONO R ASRC", 1, RT5670_ASRC_1,
1298 			      8, 0, NULL, 0),
1299 	SND_SOC_DAPM_SUPPLY_S("DMIC STO1 ASRC", 1, RT5670_ASRC_1,
1300 			      7, 0, NULL, 0),
1301 	SND_SOC_DAPM_SUPPLY_S("DMIC STO2 ASRC", 1, RT5670_ASRC_1,
1302 			      6, 0, NULL, 0),
1303 	SND_SOC_DAPM_SUPPLY_S("DMIC MONO L ASRC", 1, RT5670_ASRC_1,
1304 			      5, 0, NULL, 0),
1305 	SND_SOC_DAPM_SUPPLY_S("DMIC MONO R ASRC", 1, RT5670_ASRC_1,
1306 			      4, 0, NULL, 0),
1307 	SND_SOC_DAPM_SUPPLY_S("ADC STO1 ASRC", 1, RT5670_ASRC_1,
1308 			      3, 0, NULL, 0),
1309 	SND_SOC_DAPM_SUPPLY_S("ADC STO2 ASRC", 1, RT5670_ASRC_1,
1310 			      2, 0, NULL, 0),
1311 	SND_SOC_DAPM_SUPPLY_S("ADC MONO L ASRC", 1, RT5670_ASRC_1,
1312 			      1, 0, NULL, 0),
1313 	SND_SOC_DAPM_SUPPLY_S("ADC MONO R ASRC", 1, RT5670_ASRC_1,
1314 			      0, 0, NULL, 0),
1315 
1316 	/* Input Side */
1317 	/* micbias */
1318 	SND_SOC_DAPM_SUPPLY("MICBIAS1", RT5670_PWR_ANLG2,
1319 			     RT5670_PWR_MB1_BIT, 0, NULL, 0),
1320 
1321 	/* Input Lines */
1322 	SND_SOC_DAPM_INPUT("DMIC L1"),
1323 	SND_SOC_DAPM_INPUT("DMIC R1"),
1324 	SND_SOC_DAPM_INPUT("DMIC L2"),
1325 	SND_SOC_DAPM_INPUT("DMIC R2"),
1326 	SND_SOC_DAPM_INPUT("DMIC L3"),
1327 	SND_SOC_DAPM_INPUT("DMIC R3"),
1328 
1329 	SND_SOC_DAPM_INPUT("IN1P"),
1330 	SND_SOC_DAPM_INPUT("IN1N"),
1331 	SND_SOC_DAPM_INPUT("IN2P"),
1332 	SND_SOC_DAPM_INPUT("IN2N"),
1333 
1334 	SND_SOC_DAPM_PGA("DMIC1", SND_SOC_NOPM, 0, 0, NULL, 0),
1335 	SND_SOC_DAPM_PGA("DMIC2", SND_SOC_NOPM, 0, 0, NULL, 0),
1336 	SND_SOC_DAPM_PGA("DMIC3", SND_SOC_NOPM, 0, 0, NULL, 0),
1337 
1338 	SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM, 0, 0,
1339 			    set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
1340 	SND_SOC_DAPM_SUPPLY("DMIC1 Power", RT5670_DMIC_CTRL1,
1341 			    RT5670_DMIC_1_EN_SFT, 0, NULL, 0),
1342 	SND_SOC_DAPM_SUPPLY("DMIC2 Power", RT5670_DMIC_CTRL1,
1343 			    RT5670_DMIC_2_EN_SFT, 0, NULL, 0),
1344 	SND_SOC_DAPM_SUPPLY("DMIC3 Power", RT5670_DMIC_CTRL1,
1345 			    RT5670_DMIC_3_EN_SFT, 0, NULL, 0),
1346 	/* Boost */
1347 	SND_SOC_DAPM_PGA_E("BST1", RT5670_PWR_ANLG2, RT5670_PWR_BST1_BIT,
1348 			   0, NULL, 0, rt5670_bst1_event,
1349 			   SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
1350 	SND_SOC_DAPM_PGA_E("BST2", RT5670_PWR_ANLG2, RT5670_PWR_BST2_BIT,
1351 			   0, NULL, 0, rt5670_bst2_event,
1352 			   SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
1353 	/* Input Volume */
1354 	SND_SOC_DAPM_PGA("INL VOL", RT5670_PWR_VOL,
1355 			 RT5670_PWR_IN_L_BIT, 0, NULL, 0),
1356 	SND_SOC_DAPM_PGA("INR VOL", RT5670_PWR_VOL,
1357 			 RT5670_PWR_IN_R_BIT, 0, NULL, 0),
1358 
1359 	/* REC Mixer */
1360 	SND_SOC_DAPM_MIXER("RECMIXL", RT5670_PWR_MIXER, RT5670_PWR_RM_L_BIT, 0,
1361 			   rt5670_rec_l_mix, ARRAY_SIZE(rt5670_rec_l_mix)),
1362 	SND_SOC_DAPM_MIXER("RECMIXR", RT5670_PWR_MIXER, RT5670_PWR_RM_R_BIT, 0,
1363 			   rt5670_rec_r_mix, ARRAY_SIZE(rt5670_rec_r_mix)),
1364 	/* ADCs */
1365 	SND_SOC_DAPM_ADC("ADC 1", NULL, SND_SOC_NOPM, 0, 0),
1366 	SND_SOC_DAPM_ADC("ADC 2", NULL, SND_SOC_NOPM, 0, 0),
1367 
1368 	SND_SOC_DAPM_PGA("ADC 1_2", SND_SOC_NOPM, 0, 0, NULL, 0),
1369 
1370 	SND_SOC_DAPM_SUPPLY("ADC 1 power", RT5670_PWR_DIG1,
1371 			    RT5670_PWR_ADC_L_BIT, 0, NULL, 0),
1372 	SND_SOC_DAPM_SUPPLY("ADC 2 power", RT5670_PWR_DIG1,
1373 			    RT5670_PWR_ADC_R_BIT, 0, NULL, 0),
1374 	SND_SOC_DAPM_SUPPLY("ADC clock", RT5670_PR_BASE +
1375 			    RT5670_CHOP_DAC_ADC, 12, 0, NULL, 0),
1376 	/* ADC Mux */
1377 	SND_SOC_DAPM_MUX("Stereo1 DMIC Mux", SND_SOC_NOPM, 0, 0,
1378 			 &rt5670_sto1_dmic_mux),
1379 	SND_SOC_DAPM_MUX("Stereo1 ADC L2 Mux", SND_SOC_NOPM, 0, 0,
1380 			 &rt5670_sto_adc_l2_mux),
1381 	SND_SOC_DAPM_MUX("Stereo1 ADC R2 Mux", SND_SOC_NOPM, 0, 0,
1382 			 &rt5670_sto_adc_r2_mux),
1383 	SND_SOC_DAPM_MUX("Stereo1 ADC L1 Mux", SND_SOC_NOPM, 0, 0,
1384 			 &rt5670_sto_adc_l1_mux),
1385 	SND_SOC_DAPM_MUX("Stereo1 ADC R1 Mux", SND_SOC_NOPM, 0, 0,
1386 			 &rt5670_sto_adc_r1_mux),
1387 	SND_SOC_DAPM_MUX("Stereo2 DMIC Mux", SND_SOC_NOPM, 0, 0,
1388 			 &rt5670_sto2_dmic_mux),
1389 	SND_SOC_DAPM_MUX("Stereo2 ADC L2 Mux", SND_SOC_NOPM, 0, 0,
1390 			 &rt5670_sto2_adc_l2_mux),
1391 	SND_SOC_DAPM_MUX("Stereo2 ADC R2 Mux", SND_SOC_NOPM, 0, 0,
1392 			 &rt5670_sto2_adc_r2_mux),
1393 	SND_SOC_DAPM_MUX("Stereo2 ADC L1 Mux", SND_SOC_NOPM, 0, 0,
1394 			 &rt5670_sto2_adc_l1_mux),
1395 	SND_SOC_DAPM_MUX("Stereo2 ADC R1 Mux", SND_SOC_NOPM, 0, 0,
1396 			 &rt5670_sto2_adc_r1_mux),
1397 	SND_SOC_DAPM_MUX("Stereo2 ADC LR Mux", SND_SOC_NOPM, 0, 0,
1398 			 &rt5670_sto2_adc_lr_mux),
1399 	SND_SOC_DAPM_MUX("Mono DMIC L Mux", SND_SOC_NOPM, 0, 0,
1400 			 &rt5670_mono_dmic_l_mux),
1401 	SND_SOC_DAPM_MUX("Mono DMIC R Mux", SND_SOC_NOPM, 0, 0,
1402 			 &rt5670_mono_dmic_r_mux),
1403 	SND_SOC_DAPM_MUX("Mono ADC L2 Mux", SND_SOC_NOPM, 0, 0,
1404 			 &rt5670_mono_adc_l2_mux),
1405 	SND_SOC_DAPM_MUX("Mono ADC L1 Mux", SND_SOC_NOPM, 0, 0,
1406 			 &rt5670_mono_adc_l1_mux),
1407 	SND_SOC_DAPM_MUX("Mono ADC R1 Mux", SND_SOC_NOPM, 0, 0,
1408 			 &rt5670_mono_adc_r1_mux),
1409 	SND_SOC_DAPM_MUX("Mono ADC R2 Mux", SND_SOC_NOPM, 0, 0,
1410 			 &rt5670_mono_adc_r2_mux),
1411 	/* ADC Mixer */
1412 	SND_SOC_DAPM_SUPPLY("ADC Stereo1 Filter", RT5670_PWR_DIG2,
1413 			    RT5670_PWR_ADC_S1F_BIT, 0, NULL, 0),
1414 	SND_SOC_DAPM_SUPPLY("ADC Stereo2 Filter", RT5670_PWR_DIG2,
1415 			    RT5670_PWR_ADC_S2F_BIT, 0, NULL, 0),
1416 	SND_SOC_DAPM_MIXER("Sto1 ADC MIXL", RT5670_STO1_ADC_DIG_VOL,
1417 			   RT5670_L_MUTE_SFT, 1, rt5670_sto1_adc_l_mix,
1418 			   ARRAY_SIZE(rt5670_sto1_adc_l_mix)),
1419 	SND_SOC_DAPM_MIXER("Sto1 ADC MIXR", RT5670_STO1_ADC_DIG_VOL,
1420 			   RT5670_R_MUTE_SFT, 1, rt5670_sto1_adc_r_mix,
1421 			   ARRAY_SIZE(rt5670_sto1_adc_r_mix)),
1422 	SND_SOC_DAPM_MIXER("Sto2 ADC MIXL", SND_SOC_NOPM, 0, 0,
1423 			   rt5670_sto2_adc_l_mix,
1424 			   ARRAY_SIZE(rt5670_sto2_adc_l_mix)),
1425 	SND_SOC_DAPM_MIXER("Sto2 ADC MIXR", SND_SOC_NOPM, 0, 0,
1426 			   rt5670_sto2_adc_r_mix,
1427 			   ARRAY_SIZE(rt5670_sto2_adc_r_mix)),
1428 	SND_SOC_DAPM_SUPPLY("ADC Mono Left Filter", RT5670_PWR_DIG2,
1429 			    RT5670_PWR_ADC_MF_L_BIT, 0, NULL, 0),
1430 	SND_SOC_DAPM_MIXER("Mono ADC MIXL", RT5670_MONO_ADC_DIG_VOL,
1431 			   RT5670_L_MUTE_SFT, 1, rt5670_mono_adc_l_mix,
1432 			   ARRAY_SIZE(rt5670_mono_adc_l_mix)),
1433 	SND_SOC_DAPM_SUPPLY("ADC Mono Right Filter", RT5670_PWR_DIG2,
1434 			    RT5670_PWR_ADC_MF_R_BIT, 0, NULL, 0),
1435 	SND_SOC_DAPM_MIXER("Mono ADC MIXR", RT5670_MONO_ADC_DIG_VOL,
1436 			   RT5670_R_MUTE_SFT, 1, rt5670_mono_adc_r_mix,
1437 			   ARRAY_SIZE(rt5670_mono_adc_r_mix)),
1438 
1439 	/* ADC PGA */
1440 	SND_SOC_DAPM_PGA("Stereo1 ADC MIXL", SND_SOC_NOPM, 0, 0, NULL, 0),
1441 	SND_SOC_DAPM_PGA("Stereo1 ADC MIXR", SND_SOC_NOPM, 0, 0, NULL, 0),
1442 	SND_SOC_DAPM_PGA("Stereo2 ADC MIXL", SND_SOC_NOPM, 0, 0, NULL, 0),
1443 	SND_SOC_DAPM_PGA("Stereo2 ADC MIXR", SND_SOC_NOPM, 0, 0, NULL, 0),
1444 	SND_SOC_DAPM_PGA("Sto2 ADC LR MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
1445 	SND_SOC_DAPM_PGA("Stereo1 ADC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
1446 	SND_SOC_DAPM_PGA("Stereo2 ADC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
1447 	SND_SOC_DAPM_PGA("Mono ADC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
1448 	SND_SOC_DAPM_PGA("VAD_ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
1449 	SND_SOC_DAPM_PGA("IF_ADC1", SND_SOC_NOPM, 0, 0, NULL, 0),
1450 	SND_SOC_DAPM_PGA("IF_ADC2", SND_SOC_NOPM, 0, 0, NULL, 0),
1451 	SND_SOC_DAPM_PGA("IF_ADC3", SND_SOC_NOPM, 0, 0, NULL, 0),
1452 	SND_SOC_DAPM_PGA("IF1_ADC1", SND_SOC_NOPM, 0, 0, NULL, 0),
1453 	SND_SOC_DAPM_PGA("IF1_ADC2", SND_SOC_NOPM, 0, 0, NULL, 0),
1454 	SND_SOC_DAPM_PGA("IF1_ADC3", SND_SOC_NOPM, 0, 0, NULL, 0),
1455 	SND_SOC_DAPM_PGA("IF1_ADC4", SND_SOC_NOPM, 0, 0, NULL, 0),
1456 
1457 	/* DSP */
1458 	SND_SOC_DAPM_PGA("TxDP_ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
1459 	SND_SOC_DAPM_PGA("TxDP_ADC_L", SND_SOC_NOPM, 0, 0, NULL, 0),
1460 	SND_SOC_DAPM_PGA("TxDP_ADC_R", SND_SOC_NOPM, 0, 0, NULL, 0),
1461 	SND_SOC_DAPM_PGA("TxDC_DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
1462 
1463 	SND_SOC_DAPM_MUX("TDM Data Mux", SND_SOC_NOPM, 0, 0,
1464 			 &rt5670_txdp_slot_mux),
1465 
1466 	SND_SOC_DAPM_MUX("DSP UL Mux", SND_SOC_NOPM, 0, 0,
1467 			 &rt5670_dsp_ul_mux),
1468 	SND_SOC_DAPM_MUX("DSP DL Mux", SND_SOC_NOPM, 0, 0,
1469 			 &rt5670_dsp_dl_mux),
1470 
1471 	SND_SOC_DAPM_MUX("RxDP Mux", SND_SOC_NOPM, 0, 0,
1472 			 &rt5670_rxdp_mux),
1473 
1474 	/* IF2 Mux */
1475 	SND_SOC_DAPM_MUX("IF2 ADC Mux", SND_SOC_NOPM, 0, 0,
1476 			 &rt5670_if2_adc_in_mux),
1477 
1478 	/* Digital Interface */
1479 	SND_SOC_DAPM_SUPPLY("I2S1", RT5670_PWR_DIG1,
1480 			    RT5670_PWR_I2S1_BIT, 0, NULL, 0),
1481 	SND_SOC_DAPM_PGA("IF1 DAC1", SND_SOC_NOPM, 0, 0, NULL, 0),
1482 	SND_SOC_DAPM_PGA("IF1 DAC2", SND_SOC_NOPM, 0, 0, NULL, 0),
1483 	SND_SOC_DAPM_PGA("IF1 DAC1 L", SND_SOC_NOPM, 0, 0, NULL, 0),
1484 	SND_SOC_DAPM_PGA("IF1 DAC1 R", SND_SOC_NOPM, 0, 0, NULL, 0),
1485 	SND_SOC_DAPM_PGA("IF1 DAC2 L", SND_SOC_NOPM, 0, 0, NULL, 0),
1486 	SND_SOC_DAPM_PGA("IF1 DAC2 R", SND_SOC_NOPM, 0, 0, NULL, 0),
1487 	SND_SOC_DAPM_PGA("IF1 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
1488 	SND_SOC_DAPM_PGA("IF1 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1489 	SND_SOC_DAPM_PGA("IF1 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1490 	SND_SOC_DAPM_SUPPLY("I2S2", RT5670_PWR_DIG1,
1491 			    RT5670_PWR_I2S2_BIT, 0, NULL, 0),
1492 	SND_SOC_DAPM_PGA("IF2 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
1493 	SND_SOC_DAPM_PGA("IF2 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1494 	SND_SOC_DAPM_PGA("IF2 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1495 	SND_SOC_DAPM_PGA("IF2 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
1496 	SND_SOC_DAPM_PGA("IF2 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1497 	SND_SOC_DAPM_PGA("IF2 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1498 
1499 	/* Digital Interface Select */
1500 	SND_SOC_DAPM_MUX("IF1 ADC1 IN1 Mux", SND_SOC_NOPM, 0, 0,
1501 			 &rt5670_if1_adc1_in1_mux),
1502 	SND_SOC_DAPM_MUX("IF1 ADC1 IN2 Mux", SND_SOC_NOPM, 0, 0,
1503 			 &rt5670_if1_adc1_in2_mux),
1504 	SND_SOC_DAPM_MUX("IF1 ADC2 IN Mux", SND_SOC_NOPM, 0, 0,
1505 			 &rt5670_if1_adc2_in_mux),
1506 	SND_SOC_DAPM_MUX("IF1 ADC2 IN1 Mux", SND_SOC_NOPM, 0, 0,
1507 			 &rt5670_if1_adc2_in1_mux),
1508 	SND_SOC_DAPM_MUX("VAD ADC Mux", SND_SOC_NOPM, 0, 0,
1509 			 &rt5670_vad_adc_mux),
1510 
1511 	/* Audio Interface */
1512 	SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1513 	SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
1514 	SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
1515 	SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0,
1516 			     RT5670_GPIO_CTRL1, RT5670_I2S2_PIN_SFT, 1),
1517 
1518 	/* Audio DSP */
1519 	SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM, 0, 0, NULL, 0),
1520 
1521 	/* Output Side */
1522 	/* DAC mixer before sound effect  */
1523 	SND_SOC_DAPM_MIXER("DAC1 MIXL", SND_SOC_NOPM, 0, 0,
1524 			   rt5670_dac_l_mix, ARRAY_SIZE(rt5670_dac_l_mix)),
1525 	SND_SOC_DAPM_MIXER("DAC1 MIXR", SND_SOC_NOPM, 0, 0,
1526 			   rt5670_dac_r_mix, ARRAY_SIZE(rt5670_dac_r_mix)),
1527 	SND_SOC_DAPM_PGA("DAC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
1528 
1529 	/* DAC2 channel Mux */
1530 	SND_SOC_DAPM_MUX("DAC L2 Mux", SND_SOC_NOPM, 0, 0,
1531 			 &rt5670_dac_l2_mux),
1532 	SND_SOC_DAPM_MUX("DAC R2 Mux", SND_SOC_NOPM, 0, 0,
1533 			 &rt5670_dac_r2_mux),
1534 	SND_SOC_DAPM_PGA("DAC L2 Volume", RT5670_PWR_DIG1,
1535 			 RT5670_PWR_DAC_L2_BIT, 0, NULL, 0),
1536 	SND_SOC_DAPM_PGA("DAC R2 Volume", RT5670_PWR_DIG1,
1537 			 RT5670_PWR_DAC_R2_BIT, 0, NULL, 0),
1538 
1539 	SND_SOC_DAPM_MUX("DAC1 L Mux", SND_SOC_NOPM, 0, 0, &rt5670_dac1l_mux),
1540 	SND_SOC_DAPM_MUX("DAC1 R Mux", SND_SOC_NOPM, 0, 0, &rt5670_dac1r_mux),
1541 
1542 	/* DAC Mixer */
1543 	SND_SOC_DAPM_SUPPLY("DAC Stereo1 Filter", RT5670_PWR_DIG2,
1544 			    RT5670_PWR_DAC_S1F_BIT, 0, NULL, 0),
1545 	SND_SOC_DAPM_SUPPLY("DAC Mono Left Filter", RT5670_PWR_DIG2,
1546 			    RT5670_PWR_DAC_MF_L_BIT, 0, NULL, 0),
1547 	SND_SOC_DAPM_SUPPLY("DAC Mono Right Filter", RT5670_PWR_DIG2,
1548 			    RT5670_PWR_DAC_MF_R_BIT, 0, NULL, 0),
1549 	SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
1550 			   rt5670_sto_dac_l_mix,
1551 			   ARRAY_SIZE(rt5670_sto_dac_l_mix)),
1552 	SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
1553 			   rt5670_sto_dac_r_mix,
1554 			   ARRAY_SIZE(rt5670_sto_dac_r_mix)),
1555 	SND_SOC_DAPM_MIXER("Mono DAC MIXL", SND_SOC_NOPM, 0, 0,
1556 			   rt5670_mono_dac_l_mix,
1557 			   ARRAY_SIZE(rt5670_mono_dac_l_mix)),
1558 	SND_SOC_DAPM_MIXER("Mono DAC MIXR", SND_SOC_NOPM, 0, 0,
1559 			   rt5670_mono_dac_r_mix,
1560 			   ARRAY_SIZE(rt5670_mono_dac_r_mix)),
1561 	SND_SOC_DAPM_MIXER("DAC MIXL", SND_SOC_NOPM, 0, 0,
1562 			   rt5670_dig_l_mix,
1563 			   ARRAY_SIZE(rt5670_dig_l_mix)),
1564 	SND_SOC_DAPM_MIXER("DAC MIXR", SND_SOC_NOPM, 0, 0,
1565 			   rt5670_dig_r_mix,
1566 			   ARRAY_SIZE(rt5670_dig_r_mix)),
1567 
1568 	/* DACs */
1569 	SND_SOC_DAPM_SUPPLY("DAC L1 Power", RT5670_PWR_DIG1,
1570 			    RT5670_PWR_DAC_L1_BIT, 0, NULL, 0),
1571 	SND_SOC_DAPM_SUPPLY("DAC R1 Power", RT5670_PWR_DIG1,
1572 			    RT5670_PWR_DAC_R1_BIT, 0, NULL, 0),
1573 	SND_SOC_DAPM_DAC("DAC L1", NULL, SND_SOC_NOPM, 0, 0),
1574 	SND_SOC_DAPM_DAC("DAC R1", NULL, SND_SOC_NOPM, 0, 0),
1575 	SND_SOC_DAPM_DAC("DAC L2", NULL, RT5670_PWR_DIG1,
1576 			 RT5670_PWR_DAC_L2_BIT, 0),
1577 
1578 	SND_SOC_DAPM_DAC("DAC R2", NULL, RT5670_PWR_DIG1,
1579 			 RT5670_PWR_DAC_R2_BIT, 0),
1580 	/* OUT Mixer */
1581 
1582 	SND_SOC_DAPM_MIXER("OUT MIXL", RT5670_PWR_MIXER, RT5670_PWR_OM_L_BIT,
1583 			   0, rt5670_out_l_mix, ARRAY_SIZE(rt5670_out_l_mix)),
1584 	SND_SOC_DAPM_MIXER("OUT MIXR", RT5670_PWR_MIXER, RT5670_PWR_OM_R_BIT,
1585 			   0, rt5670_out_r_mix, ARRAY_SIZE(rt5670_out_r_mix)),
1586 	/* Ouput Volume */
1587 	SND_SOC_DAPM_MIXER("HPOVOL MIXL", RT5670_PWR_VOL,
1588 			   RT5670_PWR_HV_L_BIT, 0,
1589 			   rt5670_hpvoll_mix, ARRAY_SIZE(rt5670_hpvoll_mix)),
1590 	SND_SOC_DAPM_MIXER("HPOVOL MIXR", RT5670_PWR_VOL,
1591 			   RT5670_PWR_HV_R_BIT, 0,
1592 			   rt5670_hpvolr_mix, ARRAY_SIZE(rt5670_hpvolr_mix)),
1593 	SND_SOC_DAPM_PGA("DAC 1", SND_SOC_NOPM, 0, 0, NULL, 0),
1594 	SND_SOC_DAPM_PGA("DAC 2", SND_SOC_NOPM,	0, 0, NULL, 0),
1595 	SND_SOC_DAPM_PGA("HPOVOL", SND_SOC_NOPM, 0, 0, NULL, 0),
1596 
1597 	/* HPO/LOUT/Mono Mixer */
1598 	SND_SOC_DAPM_MIXER("HPO MIX", SND_SOC_NOPM, 0, 0,
1599 			   rt5670_hpo_mix, ARRAY_SIZE(rt5670_hpo_mix)),
1600 	SND_SOC_DAPM_MIXER("LOUT MIX", RT5670_PWR_ANLG1, RT5670_PWR_LM_BIT,
1601 			   0, rt5670_lout_mix, ARRAY_SIZE(rt5670_lout_mix)),
1602 	SND_SOC_DAPM_SUPPLY_S("Improve HP Amp Drv", 1, SND_SOC_NOPM, 0, 0,
1603 			      rt5670_hp_power_event, SND_SOC_DAPM_POST_PMU |
1604 			      SND_SOC_DAPM_PRE_PMD),
1605 	SND_SOC_DAPM_SUPPLY("HP L Amp", RT5670_PWR_ANLG1,
1606 			    RT5670_PWR_HP_L_BIT, 0, NULL, 0),
1607 	SND_SOC_DAPM_SUPPLY("HP R Amp", RT5670_PWR_ANLG1,
1608 			    RT5670_PWR_HP_R_BIT, 0, NULL, 0),
1609 	SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM, 0, 0,
1610 			   rt5670_hp_event, SND_SOC_DAPM_PRE_PMD |
1611 			   SND_SOC_DAPM_POST_PMU),
1612 	SND_SOC_DAPM_SWITCH("LOUT L Playback", SND_SOC_NOPM, 0, 0,
1613 			    &lout_l_enable_control),
1614 	SND_SOC_DAPM_SWITCH("LOUT R Playback", SND_SOC_NOPM, 0, 0,
1615 			    &lout_r_enable_control),
1616 	SND_SOC_DAPM_PGA("LOUT Amp", SND_SOC_NOPM, 0, 0, NULL, 0),
1617 
1618 	/* PDM */
1619 	SND_SOC_DAPM_SUPPLY("PDM1 Power", RT5670_PWR_DIG2,
1620 		RT5670_PWR_PDM1_BIT, 0, NULL, 0),
1621 
1622 	SND_SOC_DAPM_MUX("PDM1 L Mux", RT5670_PDM_OUT_CTRL,
1623 			 RT5670_M_PDM1_L_SFT, 1, &rt5670_pdm1_l_mux),
1624 	SND_SOC_DAPM_MUX("PDM1 R Mux", RT5670_PDM_OUT_CTRL,
1625 			 RT5670_M_PDM1_R_SFT, 1, &rt5670_pdm1_r_mux),
1626 
1627 	/* Output Lines */
1628 	SND_SOC_DAPM_OUTPUT("HPOL"),
1629 	SND_SOC_DAPM_OUTPUT("HPOR"),
1630 	SND_SOC_DAPM_OUTPUT("LOUTL"),
1631 	SND_SOC_DAPM_OUTPUT("LOUTR"),
1632 };
1633 
1634 static const struct snd_soc_dapm_widget rt5670_specific_dapm_widgets[] = {
1635 	SND_SOC_DAPM_SUPPLY("PDM2 Power", RT5670_PWR_DIG2,
1636 		RT5670_PWR_PDM2_BIT, 0, NULL, 0),
1637 	SND_SOC_DAPM_MUX("PDM2 L Mux", RT5670_PDM_OUT_CTRL,
1638 			 RT5670_M_PDM2_L_SFT, 1, &rt5670_pdm2_l_mux),
1639 	SND_SOC_DAPM_MUX("PDM2 R Mux", RT5670_PDM_OUT_CTRL,
1640 			 RT5670_M_PDM2_R_SFT, 1, &rt5670_pdm2_r_mux),
1641 	SND_SOC_DAPM_OUTPUT("PDM1L"),
1642 	SND_SOC_DAPM_OUTPUT("PDM1R"),
1643 	SND_SOC_DAPM_OUTPUT("PDM2L"),
1644 	SND_SOC_DAPM_OUTPUT("PDM2R"),
1645 };
1646 
1647 static const struct snd_soc_dapm_widget rt5672_specific_dapm_widgets[] = {
1648 	SND_SOC_DAPM_PGA("SPO Amp", SND_SOC_NOPM, 0, 0, NULL, 0),
1649 	SND_SOC_DAPM_OUTPUT("SPOLP"),
1650 	SND_SOC_DAPM_OUTPUT("SPOLN"),
1651 	SND_SOC_DAPM_OUTPUT("SPORP"),
1652 	SND_SOC_DAPM_OUTPUT("SPORN"),
1653 };
1654 
1655 static const struct snd_soc_dapm_route rt5670_dapm_routes[] = {
1656 	{ "ADC Stereo1 Filter", NULL, "ADC STO1 ASRC", is_using_asrc },
1657 	{ "ADC Stereo2 Filter", NULL, "ADC STO2 ASRC", is_using_asrc },
1658 	{ "ADC Mono Left Filter", NULL, "ADC MONO L ASRC", is_using_asrc },
1659 	{ "ADC Mono Right Filter", NULL, "ADC MONO R ASRC", is_using_asrc },
1660 	{ "DAC Mono Left Filter", NULL, "DAC MONO L ASRC", is_using_asrc },
1661 	{ "DAC Mono Right Filter", NULL, "DAC MONO R ASRC", is_using_asrc },
1662 	{ "DAC Stereo1 Filter", NULL, "DAC STO ASRC", is_using_asrc },
1663 	{ "Stereo1 DMIC Mux", NULL, "DMIC STO1 ASRC", can_use_asrc },
1664 	{ "Stereo2 DMIC Mux", NULL, "DMIC STO2 ASRC", can_use_asrc },
1665 	{ "Mono DMIC L Mux", NULL, "DMIC MONO L ASRC", can_use_asrc },
1666 	{ "Mono DMIC R Mux", NULL, "DMIC MONO R ASRC", can_use_asrc },
1667 
1668 	{ "I2S1", NULL, "I2S1 ASRC", can_use_asrc},
1669 	{ "I2S2", NULL, "I2S2 ASRC", can_use_asrc},
1670 
1671 	{ "DMIC1", NULL, "DMIC L1" },
1672 	{ "DMIC1", NULL, "DMIC R1" },
1673 	{ "DMIC2", NULL, "DMIC L2" },
1674 	{ "DMIC2", NULL, "DMIC R2" },
1675 	{ "DMIC3", NULL, "DMIC L3" },
1676 	{ "DMIC3", NULL, "DMIC R3" },
1677 
1678 	{ "BST1", NULL, "IN1P" },
1679 	{ "BST1", NULL, "IN1N" },
1680 	{ "BST1", NULL, "Mic Det Power" },
1681 	{ "BST2", NULL, "IN2P" },
1682 	{ "BST2", NULL, "IN2N" },
1683 
1684 	{ "INL VOL", NULL, "IN2P" },
1685 	{ "INR VOL", NULL, "IN2N" },
1686 
1687 	{ "RECMIXL", "INL Switch", "INL VOL" },
1688 	{ "RECMIXL", "BST2 Switch", "BST2" },
1689 	{ "RECMIXL", "BST1 Switch", "BST1" },
1690 
1691 	{ "RECMIXR", "INR Switch", "INR VOL" },
1692 	{ "RECMIXR", "BST2 Switch", "BST2" },
1693 	{ "RECMIXR", "BST1 Switch", "BST1" },
1694 
1695 	{ "ADC 1", NULL, "RECMIXL" },
1696 	{ "ADC 1", NULL, "ADC 1 power" },
1697 	{ "ADC 1", NULL, "ADC clock" },
1698 	{ "ADC 2", NULL, "RECMIXR" },
1699 	{ "ADC 2", NULL, "ADC 2 power" },
1700 	{ "ADC 2", NULL, "ADC clock" },
1701 
1702 	{ "DMIC L1", NULL, "DMIC CLK" },
1703 	{ "DMIC L1", NULL, "DMIC1 Power" },
1704 	{ "DMIC R1", NULL, "DMIC CLK" },
1705 	{ "DMIC R1", NULL, "DMIC1 Power" },
1706 	{ "DMIC L2", NULL, "DMIC CLK" },
1707 	{ "DMIC L2", NULL, "DMIC2 Power" },
1708 	{ "DMIC R2", NULL, "DMIC CLK" },
1709 	{ "DMIC R2", NULL, "DMIC2 Power" },
1710 	{ "DMIC L3", NULL, "DMIC CLK" },
1711 	{ "DMIC L3", NULL, "DMIC3 Power" },
1712 	{ "DMIC R3", NULL, "DMIC CLK" },
1713 	{ "DMIC R3", NULL, "DMIC3 Power" },
1714 
1715 	{ "Stereo1 DMIC Mux", "DMIC1", "DMIC1" },
1716 	{ "Stereo1 DMIC Mux", "DMIC2", "DMIC2" },
1717 	{ "Stereo1 DMIC Mux", "DMIC3", "DMIC3" },
1718 
1719 	{ "Stereo2 DMIC Mux", "DMIC1", "DMIC1" },
1720 	{ "Stereo2 DMIC Mux", "DMIC2", "DMIC2" },
1721 	{ "Stereo2 DMIC Mux", "DMIC3", "DMIC3" },
1722 
1723 	{ "Mono DMIC L Mux", "DMIC1", "DMIC L1" },
1724 	{ "Mono DMIC L Mux", "DMIC2", "DMIC L2" },
1725 	{ "Mono DMIC L Mux", "DMIC3", "DMIC L3" },
1726 
1727 	{ "Mono DMIC R Mux", "DMIC1", "DMIC R1" },
1728 	{ "Mono DMIC R Mux", "DMIC2", "DMIC R2" },
1729 	{ "Mono DMIC R Mux", "DMIC3", "DMIC R3" },
1730 
1731 	{ "ADC 1_2", NULL, "ADC 1" },
1732 	{ "ADC 1_2", NULL, "ADC 2" },
1733 
1734 	{ "Stereo1 ADC L2 Mux", "DMIC", "Stereo1 DMIC Mux" },
1735 	{ "Stereo1 ADC L2 Mux", "DAC MIX", "DAC MIXL" },
1736 	{ "Stereo1 ADC L1 Mux", "ADC", "ADC 1_2" },
1737 	{ "Stereo1 ADC L1 Mux", "DAC MIX", "DAC MIXL" },
1738 
1739 	{ "Stereo1 ADC R1 Mux", "ADC", "ADC 1_2" },
1740 	{ "Stereo1 ADC R1 Mux", "DAC MIX", "DAC MIXR" },
1741 	{ "Stereo1 ADC R2 Mux", "DMIC", "Stereo1 DMIC Mux" },
1742 	{ "Stereo1 ADC R2 Mux", "DAC MIX", "DAC MIXR" },
1743 
1744 	{ "Mono ADC L2 Mux", "DMIC", "Mono DMIC L Mux" },
1745 	{ "Mono ADC L2 Mux", "Mono DAC MIXL", "Mono DAC MIXL" },
1746 	{ "Mono ADC L1 Mux", "Mono DAC MIXL", "Mono DAC MIXL" },
1747 	{ "Mono ADC L1 Mux", "ADC1",  "ADC 1" },
1748 
1749 	{ "Mono ADC R1 Mux", "Mono DAC MIXR", "Mono DAC MIXR" },
1750 	{ "Mono ADC R1 Mux", "ADC2", "ADC 2" },
1751 	{ "Mono ADC R2 Mux", "DMIC", "Mono DMIC R Mux" },
1752 	{ "Mono ADC R2 Mux", "Mono DAC MIXR", "Mono DAC MIXR" },
1753 
1754 	{ "Sto1 ADC MIXL", "ADC1 Switch", "Stereo1 ADC L1 Mux" },
1755 	{ "Sto1 ADC MIXL", "ADC2 Switch", "Stereo1 ADC L2 Mux" },
1756 	{ "Sto1 ADC MIXR", "ADC1 Switch", "Stereo1 ADC R1 Mux" },
1757 	{ "Sto1 ADC MIXR", "ADC2 Switch", "Stereo1 ADC R2 Mux" },
1758 
1759 	{ "Stereo1 ADC MIXL", NULL, "Sto1 ADC MIXL" },
1760 	{ "Stereo1 ADC MIXL", NULL, "ADC Stereo1 Filter" },
1761 	{ "ADC Stereo1 Filter", NULL, "PLL1", is_sys_clk_from_pll },
1762 
1763 	{ "Stereo1 ADC MIXR", NULL, "Sto1 ADC MIXR" },
1764 	{ "Stereo1 ADC MIXR", NULL, "ADC Stereo1 Filter" },
1765 	{ "ADC Stereo1 Filter", NULL, "PLL1", is_sys_clk_from_pll },
1766 
1767 	{ "Mono ADC MIXL", "ADC1 Switch", "Mono ADC L1 Mux" },
1768 	{ "Mono ADC MIXL", "ADC2 Switch", "Mono ADC L2 Mux" },
1769 	{ "Mono ADC MIXL", NULL, "ADC Mono Left Filter" },
1770 	{ "ADC Mono Left Filter", NULL, "PLL1", is_sys_clk_from_pll },
1771 
1772 	{ "Mono ADC MIXR", "ADC1 Switch", "Mono ADC R1 Mux" },
1773 	{ "Mono ADC MIXR", "ADC2 Switch", "Mono ADC R2 Mux" },
1774 	{ "Mono ADC MIXR", NULL, "ADC Mono Right Filter" },
1775 	{ "ADC Mono Right Filter", NULL, "PLL1", is_sys_clk_from_pll },
1776 
1777 	{ "Stereo2 ADC L2 Mux", "DMIC", "Stereo2 DMIC Mux" },
1778 	{ "Stereo2 ADC L2 Mux", "DAC MIX", "DAC MIXL" },
1779 	{ "Stereo2 ADC L1 Mux", "ADC", "ADC 1_2" },
1780 	{ "Stereo2 ADC L1 Mux", "DAC MIX", "DAC MIXL" },
1781 
1782 	{ "Stereo2 ADC R1 Mux", "ADC", "ADC 1_2" },
1783 	{ "Stereo2 ADC R1 Mux", "DAC MIX", "DAC MIXR" },
1784 	{ "Stereo2 ADC R2 Mux", "DMIC", "Stereo2 DMIC Mux" },
1785 	{ "Stereo2 ADC R2 Mux", "DAC MIX", "DAC MIXR" },
1786 
1787 	{ "Sto2 ADC MIXL", "ADC1 Switch", "Stereo2 ADC L1 Mux" },
1788 	{ "Sto2 ADC MIXL", "ADC2 Switch", "Stereo2 ADC L2 Mux" },
1789 	{ "Sto2 ADC MIXR", "ADC1 Switch", "Stereo2 ADC R1 Mux" },
1790 	{ "Sto2 ADC MIXR", "ADC2 Switch", "Stereo2 ADC R2 Mux" },
1791 
1792 	{ "Sto2 ADC LR MIX", NULL, "Sto2 ADC MIXL" },
1793 	{ "Sto2 ADC LR MIX", NULL, "Sto2 ADC MIXR" },
1794 
1795 	{ "Stereo2 ADC LR Mux", "L", "Sto2 ADC MIXL" },
1796 	{ "Stereo2 ADC LR Mux", "LR", "Sto2 ADC LR MIX" },
1797 
1798 	{ "Stereo2 ADC MIXL", NULL, "Stereo2 ADC LR Mux" },
1799 	{ "Stereo2 ADC MIXL", NULL, "ADC Stereo2 Filter" },
1800 	{ "ADC Stereo2 Filter", NULL, "PLL1", is_sys_clk_from_pll },
1801 
1802 	{ "Stereo2 ADC MIXR", NULL, "Sto2 ADC MIXR" },
1803 	{ "Stereo2 ADC MIXR", NULL, "ADC Stereo2 Filter" },
1804 	{ "ADC Stereo2 Filter", NULL, "PLL1", is_sys_clk_from_pll },
1805 
1806 	{ "VAD ADC Mux", "Sto1 ADC L", "Stereo1 ADC MIXL" },
1807 	{ "VAD ADC Mux", "Mono ADC L", "Mono ADC MIXL" },
1808 	{ "VAD ADC Mux", "Mono ADC R", "Mono ADC MIXR" },
1809 	{ "VAD ADC Mux", "Sto2 ADC L", "Sto2 ADC MIXL" },
1810 
1811 	{ "VAD_ADC", NULL, "VAD ADC Mux" },
1812 
1813 	{ "IF_ADC1", NULL, "Stereo1 ADC MIXL" },
1814 	{ "IF_ADC1", NULL, "Stereo1 ADC MIXR" },
1815 	{ "IF_ADC2", NULL, "Mono ADC MIXL" },
1816 	{ "IF_ADC2", NULL, "Mono ADC MIXR" },
1817 	{ "IF_ADC3", NULL, "Stereo2 ADC MIXL" },
1818 	{ "IF_ADC3", NULL, "Stereo2 ADC MIXR" },
1819 
1820 	{ "IF1 ADC1 IN1 Mux", "IF_ADC1", "IF_ADC1" },
1821 	{ "IF1 ADC1 IN1 Mux", "IF1_ADC3", "IF1_ADC3" },
1822 
1823 	{ "IF1 ADC1 IN2 Mux", "IF1_ADC1_IN1", "IF1 ADC1 IN1 Mux" },
1824 	{ "IF1 ADC1 IN2 Mux", "IF1_ADC4", "IF1_ADC4" },
1825 
1826 	{ "IF1 ADC2 IN Mux", "IF_ADC2", "IF_ADC2" },
1827 	{ "IF1 ADC2 IN Mux", "VAD_ADC", "VAD_ADC" },
1828 
1829 	{ "IF1 ADC2 IN1 Mux", "IF1_ADC2_IN", "IF1 ADC2 IN Mux" },
1830 	{ "IF1 ADC2 IN1 Mux", "IF1_ADC4", "IF1_ADC4" },
1831 
1832 	{ "IF1_ADC1" , NULL, "IF1 ADC1 IN2 Mux" },
1833 	{ "IF1_ADC2" , NULL, "IF1 ADC2 IN1 Mux" },
1834 
1835 	{ "Stereo1 ADC MIX", NULL, "Stereo1 ADC MIXL" },
1836 	{ "Stereo1 ADC MIX", NULL, "Stereo1 ADC MIXR" },
1837 	{ "Stereo2 ADC MIX", NULL, "Sto2 ADC MIXL" },
1838 	{ "Stereo2 ADC MIX", NULL, "Sto2 ADC MIXR" },
1839 	{ "Mono ADC MIX", NULL, "Mono ADC MIXL" },
1840 	{ "Mono ADC MIX", NULL, "Mono ADC MIXR" },
1841 
1842 	{ "RxDP Mux", "IF2 DAC", "IF2 DAC" },
1843 	{ "RxDP Mux", "IF1 DAC", "IF1 DAC2" },
1844 	{ "RxDP Mux", "STO1 ADC Mixer", "Stereo1 ADC MIX" },
1845 	{ "RxDP Mux", "STO2 ADC Mixer", "Stereo2 ADC MIX" },
1846 	{ "RxDP Mux", "Mono ADC Mixer L", "Mono ADC MIXL" },
1847 	{ "RxDP Mux", "Mono ADC Mixer R", "Mono ADC MIXR" },
1848 	{ "RxDP Mux", "DAC1", "DAC MIX" },
1849 
1850 	{ "TDM Data Mux", "Slot 0-1", "Stereo1 ADC MIX" },
1851 	{ "TDM Data Mux", "Slot 2-3", "Mono ADC MIX" },
1852 	{ "TDM Data Mux", "Slot 4-5", "Stereo2 ADC MIX" },
1853 	{ "TDM Data Mux", "Slot 6-7", "IF2 DAC" },
1854 
1855 	{ "DSP UL Mux", "Bypass", "TDM Data Mux" },
1856 	{ "DSP UL Mux", NULL, "I2S DSP" },
1857 	{ "DSP DL Mux", "Bypass", "RxDP Mux" },
1858 	{ "DSP DL Mux", NULL, "I2S DSP" },
1859 
1860 	{ "TxDP_ADC_L", NULL, "DSP UL Mux" },
1861 	{ "TxDP_ADC_R", NULL, "DSP UL Mux" },
1862 	{ "TxDC_DAC", NULL, "DSP DL Mux" },
1863 
1864 	{ "TxDP_ADC", NULL, "TxDP_ADC_L" },
1865 	{ "TxDP_ADC", NULL, "TxDP_ADC_R" },
1866 
1867 	{ "IF1 ADC", NULL, "I2S1" },
1868 	{ "IF1 ADC", NULL, "IF1_ADC1" },
1869 	{ "IF1 ADC", NULL, "IF1_ADC2" },
1870 	{ "IF1 ADC", NULL, "IF_ADC3" },
1871 	{ "IF1 ADC", NULL, "TxDP_ADC" },
1872 
1873 	{ "IF2 ADC Mux", "IF_ADC1", "IF_ADC1" },
1874 	{ "IF2 ADC Mux", "IF_ADC2", "IF_ADC2" },
1875 	{ "IF2 ADC Mux", "IF_ADC3", "IF_ADC3" },
1876 	{ "IF2 ADC Mux", "TxDC_DAC", "TxDC_DAC" },
1877 	{ "IF2 ADC Mux", "TxDP_ADC", "TxDP_ADC" },
1878 	{ "IF2 ADC Mux", "VAD_ADC", "VAD_ADC" },
1879 
1880 	{ "IF2 ADC L", NULL, "IF2 ADC Mux" },
1881 	{ "IF2 ADC R", NULL, "IF2 ADC Mux" },
1882 
1883 	{ "IF2 ADC", NULL, "I2S2" },
1884 	{ "IF2 ADC", NULL, "IF2 ADC L" },
1885 	{ "IF2 ADC", NULL, "IF2 ADC R" },
1886 
1887 	{ "AIF1TX", NULL, "IF1 ADC" },
1888 	{ "AIF2TX", NULL, "IF2 ADC" },
1889 
1890 	{ "IF1 DAC1", NULL, "AIF1RX" },
1891 	{ "IF1 DAC2", NULL, "AIF1RX" },
1892 	{ "IF2 DAC", NULL, "AIF2RX" },
1893 
1894 	{ "IF1 DAC1", NULL, "I2S1" },
1895 	{ "IF1 DAC2", NULL, "I2S1" },
1896 	{ "IF2 DAC", NULL, "I2S2" },
1897 
1898 	{ "IF1 DAC2 L", NULL, "IF1 DAC2" },
1899 	{ "IF1 DAC2 R", NULL, "IF1 DAC2" },
1900 	{ "IF1 DAC1 L", NULL, "IF1 DAC1" },
1901 	{ "IF1 DAC1 R", NULL, "IF1 DAC1" },
1902 	{ "IF2 DAC L", NULL, "IF2 DAC" },
1903 	{ "IF2 DAC R", NULL, "IF2 DAC" },
1904 
1905 	{ "DAC1 L Mux", "IF1 DAC", "IF1 DAC1 L" },
1906 	{ "DAC1 L Mux", "IF2 DAC", "IF2 DAC L" },
1907 
1908 	{ "DAC1 R Mux", "IF1 DAC", "IF1 DAC1 R" },
1909 	{ "DAC1 R Mux", "IF2 DAC", "IF2 DAC R" },
1910 
1911 	{ "DAC1 MIXL", "Stereo ADC Switch", "Stereo1 ADC MIXL" },
1912 	{ "DAC1 MIXL", "DAC1 Switch", "DAC1 L Mux" },
1913 	{ "DAC1 MIXL", NULL, "DAC Stereo1 Filter" },
1914 	{ "DAC1 MIXR", "Stereo ADC Switch", "Stereo1 ADC MIXR" },
1915 	{ "DAC1 MIXR", "DAC1 Switch", "DAC1 R Mux" },
1916 	{ "DAC1 MIXR", NULL, "DAC Stereo1 Filter" },
1917 
1918 	{ "DAC Stereo1 Filter", NULL, "PLL1", is_sys_clk_from_pll },
1919 	{ "DAC Mono Left Filter", NULL, "PLL1", is_sys_clk_from_pll },
1920 	{ "DAC Mono Right Filter", NULL, "PLL1", is_sys_clk_from_pll },
1921 
1922 	{ "DAC MIX", NULL, "DAC1 MIXL" },
1923 	{ "DAC MIX", NULL, "DAC1 MIXR" },
1924 
1925 	{ "Audio DSP", NULL, "DAC1 MIXL" },
1926 	{ "Audio DSP", NULL, "DAC1 MIXR" },
1927 
1928 	{ "DAC L2 Mux", "IF1 DAC", "IF1 DAC2 L" },
1929 	{ "DAC L2 Mux", "IF2 DAC", "IF2 DAC L" },
1930 	{ "DAC L2 Mux", "TxDC DAC", "TxDC_DAC" },
1931 	{ "DAC L2 Mux", "VAD_ADC", "VAD_ADC" },
1932 	{ "DAC L2 Volume", NULL, "DAC L2 Mux" },
1933 	{ "DAC L2 Volume", NULL, "DAC Mono Left Filter" },
1934 
1935 	{ "DAC R2 Mux", "IF1 DAC", "IF1 DAC2 R" },
1936 	{ "DAC R2 Mux", "IF2 DAC", "IF2 DAC R" },
1937 	{ "DAC R2 Mux", "TxDC DAC", "TxDC_DAC" },
1938 	{ "DAC R2 Mux", "TxDP ADC", "TxDP_ADC" },
1939 	{ "DAC R2 Volume", NULL, "DAC R2 Mux" },
1940 	{ "DAC R2 Volume", NULL, "DAC Mono Right Filter" },
1941 
1942 	{ "Stereo DAC MIXL", "DAC L1 Switch", "DAC1 MIXL" },
1943 	{ "Stereo DAC MIXL", "DAC R1 Switch", "DAC1 MIXR" },
1944 	{ "Stereo DAC MIXL", "DAC L2 Switch", "DAC L2 Volume" },
1945 	{ "Stereo DAC MIXL", NULL, "DAC Stereo1 Filter" },
1946 	{ "Stereo DAC MIXL", NULL, "DAC L1 Power" },
1947 	{ "Stereo DAC MIXR", "DAC R1 Switch", "DAC1 MIXR" },
1948 	{ "Stereo DAC MIXR", "DAC L1 Switch", "DAC1 MIXL" },
1949 	{ "Stereo DAC MIXR", "DAC R2 Switch", "DAC R2 Volume" },
1950 	{ "Stereo DAC MIXR", NULL, "DAC Stereo1 Filter" },
1951 	{ "Stereo DAC MIXR", NULL, "DAC R1 Power" },
1952 
1953 	{ "Mono DAC MIXL", "DAC L1 Switch", "DAC1 MIXL" },
1954 	{ "Mono DAC MIXL", "DAC L2 Switch", "DAC L2 Volume" },
1955 	{ "Mono DAC MIXL", "DAC R2 Switch", "DAC R2 Volume" },
1956 	{ "Mono DAC MIXL", NULL, "DAC Mono Left Filter" },
1957 	{ "Mono DAC MIXR", "DAC R1 Switch", "DAC1 MIXR" },
1958 	{ "Mono DAC MIXR", "DAC R2 Switch", "DAC R2 Volume" },
1959 	{ "Mono DAC MIXR", "DAC L2 Switch", "DAC L2 Volume" },
1960 	{ "Mono DAC MIXR", NULL, "DAC Mono Right Filter" },
1961 
1962 	{ "DAC MIXL", "Sto DAC Mix L Switch", "Stereo DAC MIXL" },
1963 	{ "DAC MIXL", "DAC L2 Switch", "DAC L2 Volume" },
1964 	{ "DAC MIXL", "DAC R2 Switch", "DAC R2 Volume" },
1965 	{ "DAC MIXR", "Sto DAC Mix R Switch", "Stereo DAC MIXR" },
1966 	{ "DAC MIXR", "DAC R2 Switch", "DAC R2 Volume" },
1967 	{ "DAC MIXR", "DAC L2 Switch", "DAC L2 Volume" },
1968 
1969 	{ "DAC L1", NULL, "DAC L1 Power" },
1970 	{ "DAC L1", NULL, "Stereo DAC MIXL" },
1971 	{ "DAC R1", NULL, "DAC R1 Power" },
1972 	{ "DAC R1", NULL, "Stereo DAC MIXR" },
1973 	{ "DAC L2", NULL, "Mono DAC MIXL" },
1974 	{ "DAC R2", NULL, "Mono DAC MIXR" },
1975 
1976 	{ "OUT MIXL", "BST1 Switch", "BST1" },
1977 	{ "OUT MIXL", "INL Switch", "INL VOL" },
1978 	{ "OUT MIXL", "DAC L2 Switch", "DAC L2" },
1979 	{ "OUT MIXL", "DAC L1 Switch", "DAC L1" },
1980 
1981 	{ "OUT MIXR", "BST2 Switch", "BST2" },
1982 	{ "OUT MIXR", "INR Switch", "INR VOL" },
1983 	{ "OUT MIXR", "DAC R2 Switch", "DAC R2" },
1984 	{ "OUT MIXR", "DAC R1 Switch", "DAC R1" },
1985 
1986 	{ "HPOVOL MIXL", "DAC1 Switch", "DAC L1" },
1987 	{ "HPOVOL MIXL", "INL Switch", "INL VOL" },
1988 	{ "HPOVOL MIXR", "DAC1 Switch", "DAC R1" },
1989 	{ "HPOVOL MIXR", "INR Switch", "INR VOL" },
1990 
1991 	{ "DAC 2", NULL, "DAC L2" },
1992 	{ "DAC 2", NULL, "DAC R2" },
1993 	{ "DAC 1", NULL, "DAC L1" },
1994 	{ "DAC 1", NULL, "DAC R1" },
1995 	{ "HPOVOL", NULL, "HPOVOL MIXL" },
1996 	{ "HPOVOL", NULL, "HPOVOL MIXR" },
1997 	{ "HPO MIX", "DAC1 Switch", "DAC 1" },
1998 	{ "HPO MIX", "HPVOL Switch", "HPOVOL" },
1999 
2000 	{ "LOUT MIX", "DAC L1 Switch", "DAC L1" },
2001 	{ "LOUT MIX", "DAC R1 Switch", "DAC R1" },
2002 	{ "LOUT MIX", "OUTMIX L Switch", "OUT MIXL" },
2003 	{ "LOUT MIX", "OUTMIX R Switch", "OUT MIXR" },
2004 
2005 	{ "PDM1 L Mux", "Stereo DAC", "Stereo DAC MIXL" },
2006 	{ "PDM1 L Mux", "Mono DAC", "Mono DAC MIXL" },
2007 	{ "PDM1 L Mux", NULL, "PDM1 Power" },
2008 	{ "PDM1 R Mux", "Stereo DAC", "Stereo DAC MIXR" },
2009 	{ "PDM1 R Mux", "Mono DAC", "Mono DAC MIXR" },
2010 	{ "PDM1 R Mux", NULL, "PDM1 Power" },
2011 
2012 	{ "HP Amp", NULL, "HPO MIX" },
2013 	{ "HP Amp", NULL, "Mic Det Power" },
2014 	{ "HPOL", NULL, "HP Amp" },
2015 	{ "HPOL", NULL, "HP L Amp" },
2016 	{ "HPOL", NULL, "Improve HP Amp Drv" },
2017 	{ "HPOR", NULL, "HP Amp" },
2018 	{ "HPOR", NULL, "HP R Amp" },
2019 	{ "HPOR", NULL, "Improve HP Amp Drv" },
2020 
2021 	{ "LOUT Amp", NULL, "LOUT MIX" },
2022 	{ "LOUT L Playback", "Switch", "LOUT Amp" },
2023 	{ "LOUT R Playback", "Switch", "LOUT Amp" },
2024 	{ "LOUTL", NULL, "LOUT L Playback" },
2025 	{ "LOUTR", NULL, "LOUT R Playback" },
2026 	{ "LOUTL", NULL, "Improve HP Amp Drv" },
2027 	{ "LOUTR", NULL, "Improve HP Amp Drv" },
2028 };
2029 
2030 static const struct snd_soc_dapm_route rt5670_specific_dapm_routes[] = {
2031 	{ "PDM2 L Mux", "Stereo DAC", "Stereo DAC MIXL" },
2032 	{ "PDM2 L Mux", "Mono DAC", "Mono DAC MIXL" },
2033 	{ "PDM2 L Mux", NULL, "PDM2 Power" },
2034 	{ "PDM2 R Mux", "Stereo DAC", "Stereo DAC MIXR" },
2035 	{ "PDM2 R Mux", "Mono DAC", "Mono DAC MIXR" },
2036 	{ "PDM2 R Mux", NULL, "PDM2 Power" },
2037 	{ "PDM1L", NULL, "PDM1 L Mux" },
2038 	{ "PDM1R", NULL, "PDM1 R Mux" },
2039 	{ "PDM2L", NULL, "PDM2 L Mux" },
2040 	{ "PDM2R", NULL, "PDM2 R Mux" },
2041 };
2042 
2043 static const struct snd_soc_dapm_route rt5672_specific_dapm_routes[] = {
2044 	{ "SPO Amp", NULL, "PDM1 L Mux" },
2045 	{ "SPO Amp", NULL, "PDM1 R Mux" },
2046 	{ "SPOLP", NULL, "SPO Amp" },
2047 	{ "SPOLN", NULL, "SPO Amp" },
2048 	{ "SPORP", NULL, "SPO Amp" },
2049 	{ "SPORN", NULL, "SPO Amp" },
2050 };
2051 
2052 static int rt5670_hw_params(struct snd_pcm_substream *substream,
2053 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
2054 {
2055 	struct snd_soc_codec *codec = dai->codec;
2056 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
2057 	unsigned int val_len = 0, val_clk, mask_clk;
2058 	int pre_div, bclk_ms, frame_size;
2059 
2060 	rt5670->lrck[dai->id] = params_rate(params);
2061 	pre_div = rl6231_get_clk_info(rt5670->sysclk, rt5670->lrck[dai->id]);
2062 	if (pre_div < 0) {
2063 		dev_err(codec->dev, "Unsupported clock setting %d for DAI %d\n",
2064 			rt5670->lrck[dai->id], dai->id);
2065 		return -EINVAL;
2066 	}
2067 	frame_size = snd_soc_params_to_frame_size(params);
2068 	if (frame_size < 0) {
2069 		dev_err(codec->dev, "Unsupported frame size: %d\n", frame_size);
2070 		return -EINVAL;
2071 	}
2072 	bclk_ms = frame_size > 32;
2073 	rt5670->bclk[dai->id] = rt5670->lrck[dai->id] * (32 << bclk_ms);
2074 
2075 	dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
2076 		rt5670->bclk[dai->id], rt5670->lrck[dai->id]);
2077 	dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
2078 				bclk_ms, pre_div, dai->id);
2079 
2080 	switch (params_width(params)) {
2081 	case 16:
2082 		break;
2083 	case 20:
2084 		val_len |= RT5670_I2S_DL_20;
2085 		break;
2086 	case 24:
2087 		val_len |= RT5670_I2S_DL_24;
2088 		break;
2089 	case 8:
2090 		val_len |= RT5670_I2S_DL_8;
2091 		break;
2092 	default:
2093 		return -EINVAL;
2094 	}
2095 
2096 	switch (dai->id) {
2097 	case RT5670_AIF1:
2098 		mask_clk = RT5670_I2S_BCLK_MS1_MASK | RT5670_I2S_PD1_MASK;
2099 		val_clk = bclk_ms << RT5670_I2S_BCLK_MS1_SFT |
2100 			pre_div << RT5670_I2S_PD1_SFT;
2101 		snd_soc_update_bits(codec, RT5670_I2S1_SDP,
2102 			RT5670_I2S_DL_MASK, val_len);
2103 		snd_soc_update_bits(codec, RT5670_ADDA_CLK1, mask_clk, val_clk);
2104 		break;
2105 	case RT5670_AIF2:
2106 		mask_clk = RT5670_I2S_BCLK_MS2_MASK | RT5670_I2S_PD2_MASK;
2107 		val_clk = bclk_ms << RT5670_I2S_BCLK_MS2_SFT |
2108 			pre_div << RT5670_I2S_PD2_SFT;
2109 		snd_soc_update_bits(codec, RT5670_I2S2_SDP,
2110 			RT5670_I2S_DL_MASK, val_len);
2111 		snd_soc_update_bits(codec, RT5670_ADDA_CLK1, mask_clk, val_clk);
2112 		break;
2113 	default:
2114 		dev_err(codec->dev, "Invalid dai->id: %d\n", dai->id);
2115 		return -EINVAL;
2116 	}
2117 
2118 	return 0;
2119 }
2120 
2121 static int rt5670_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2122 {
2123 	struct snd_soc_codec *codec = dai->codec;
2124 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
2125 	unsigned int reg_val = 0;
2126 
2127 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2128 	case SND_SOC_DAIFMT_CBM_CFM:
2129 		rt5670->master[dai->id] = 1;
2130 		break;
2131 	case SND_SOC_DAIFMT_CBS_CFS:
2132 		reg_val |= RT5670_I2S_MS_S;
2133 		rt5670->master[dai->id] = 0;
2134 		break;
2135 	default:
2136 		return -EINVAL;
2137 	}
2138 
2139 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2140 	case SND_SOC_DAIFMT_NB_NF:
2141 		break;
2142 	case SND_SOC_DAIFMT_IB_NF:
2143 		reg_val |= RT5670_I2S_BP_INV;
2144 		break;
2145 	default:
2146 		return -EINVAL;
2147 	}
2148 
2149 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2150 	case SND_SOC_DAIFMT_I2S:
2151 		break;
2152 	case SND_SOC_DAIFMT_LEFT_J:
2153 		reg_val |= RT5670_I2S_DF_LEFT;
2154 		break;
2155 	case SND_SOC_DAIFMT_DSP_A:
2156 		reg_val |= RT5670_I2S_DF_PCM_A;
2157 		break;
2158 	case SND_SOC_DAIFMT_DSP_B:
2159 		reg_val |= RT5670_I2S_DF_PCM_B;
2160 		break;
2161 	default:
2162 		return -EINVAL;
2163 	}
2164 
2165 	switch (dai->id) {
2166 	case RT5670_AIF1:
2167 		snd_soc_update_bits(codec, RT5670_I2S1_SDP,
2168 			RT5670_I2S_MS_MASK | RT5670_I2S_BP_MASK |
2169 			RT5670_I2S_DF_MASK, reg_val);
2170 		break;
2171 	case RT5670_AIF2:
2172 		snd_soc_update_bits(codec, RT5670_I2S2_SDP,
2173 			RT5670_I2S_MS_MASK | RT5670_I2S_BP_MASK |
2174 			RT5670_I2S_DF_MASK, reg_val);
2175 		break;
2176 	default:
2177 		dev_err(codec->dev, "Invalid dai->id: %d\n", dai->id);
2178 		return -EINVAL;
2179 	}
2180 	return 0;
2181 }
2182 
2183 static int rt5670_set_dai_sysclk(struct snd_soc_dai *dai,
2184 		int clk_id, unsigned int freq, int dir)
2185 {
2186 	struct snd_soc_codec *codec = dai->codec;
2187 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
2188 	unsigned int reg_val = 0;
2189 
2190 	if (freq == rt5670->sysclk && clk_id == rt5670->sysclk_src)
2191 		return 0;
2192 
2193 	if (rt5670->pdata.jd_mode) {
2194 		if (clk_id == RT5670_SCLK_S_PLL1)
2195 			snd_soc_dapm_force_enable_pin(&codec->dapm, "PLL1");
2196 		else
2197 			snd_soc_dapm_disable_pin(&codec->dapm, "PLL1");
2198 		snd_soc_dapm_sync(&codec->dapm);
2199 	}
2200 	switch (clk_id) {
2201 	case RT5670_SCLK_S_MCLK:
2202 		reg_val |= RT5670_SCLK_SRC_MCLK;
2203 		break;
2204 	case RT5670_SCLK_S_PLL1:
2205 		reg_val |= RT5670_SCLK_SRC_PLL1;
2206 		break;
2207 	case RT5670_SCLK_S_RCCLK:
2208 		reg_val |= RT5670_SCLK_SRC_RCCLK;
2209 		break;
2210 	default:
2211 		dev_err(codec->dev, "Invalid clock id (%d)\n", clk_id);
2212 		return -EINVAL;
2213 	}
2214 	snd_soc_update_bits(codec, RT5670_GLB_CLK,
2215 		RT5670_SCLK_SRC_MASK, reg_val);
2216 	rt5670->sysclk = freq;
2217 	rt5670->sysclk_src = clk_id;
2218 
2219 	dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
2220 
2221 	return 0;
2222 }
2223 
2224 static int rt5670_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
2225 			unsigned int freq_in, unsigned int freq_out)
2226 {
2227 	struct snd_soc_codec *codec = dai->codec;
2228 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
2229 	struct rl6231_pll_code pll_code;
2230 	int ret;
2231 
2232 	if (source == rt5670->pll_src && freq_in == rt5670->pll_in &&
2233 	    freq_out == rt5670->pll_out)
2234 		return 0;
2235 
2236 	if (!freq_in || !freq_out) {
2237 		dev_dbg(codec->dev, "PLL disabled\n");
2238 
2239 		rt5670->pll_in = 0;
2240 		rt5670->pll_out = 0;
2241 		snd_soc_update_bits(codec, RT5670_GLB_CLK,
2242 			RT5670_SCLK_SRC_MASK, RT5670_SCLK_SRC_MCLK);
2243 		return 0;
2244 	}
2245 
2246 	switch (source) {
2247 	case RT5670_PLL1_S_MCLK:
2248 		snd_soc_update_bits(codec, RT5670_GLB_CLK,
2249 			RT5670_PLL1_SRC_MASK, RT5670_PLL1_SRC_MCLK);
2250 		break;
2251 	case RT5670_PLL1_S_BCLK1:
2252 	case RT5670_PLL1_S_BCLK2:
2253 	case RT5670_PLL1_S_BCLK3:
2254 	case RT5670_PLL1_S_BCLK4:
2255 		switch (dai->id) {
2256 		case RT5670_AIF1:
2257 			snd_soc_update_bits(codec, RT5670_GLB_CLK,
2258 				RT5670_PLL1_SRC_MASK, RT5670_PLL1_SRC_BCLK1);
2259 			break;
2260 		case RT5670_AIF2:
2261 			snd_soc_update_bits(codec, RT5670_GLB_CLK,
2262 				RT5670_PLL1_SRC_MASK, RT5670_PLL1_SRC_BCLK2);
2263 			break;
2264 		default:
2265 			dev_err(codec->dev, "Invalid dai->id: %d\n", dai->id);
2266 			return -EINVAL;
2267 		}
2268 		break;
2269 	default:
2270 		dev_err(codec->dev, "Unknown PLL source %d\n", source);
2271 		return -EINVAL;
2272 	}
2273 
2274 	ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
2275 	if (ret < 0) {
2276 		dev_err(codec->dev, "Unsupport input clock %d\n", freq_in);
2277 		return ret;
2278 	}
2279 
2280 	dev_dbg(codec->dev, "bypass=%d m=%d n=%d k=%d\n",
2281 		pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
2282 		pll_code.n_code, pll_code.k_code);
2283 
2284 	snd_soc_write(codec, RT5670_PLL_CTRL1,
2285 		pll_code.n_code << RT5670_PLL_N_SFT | pll_code.k_code);
2286 	snd_soc_write(codec, RT5670_PLL_CTRL2,
2287 		(pll_code.m_bp ? 0 : pll_code.m_code) << RT5670_PLL_M_SFT |
2288 		pll_code.m_bp << RT5670_PLL_M_BP_SFT);
2289 
2290 	rt5670->pll_in = freq_in;
2291 	rt5670->pll_out = freq_out;
2292 	rt5670->pll_src = source;
2293 
2294 	return 0;
2295 }
2296 
2297 static int rt5670_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
2298 			unsigned int rx_mask, int slots, int slot_width)
2299 {
2300 	struct snd_soc_codec *codec = dai->codec;
2301 	unsigned int val = 0;
2302 
2303 	if (rx_mask || tx_mask)
2304 		val |= (1 << 14);
2305 
2306 	switch (slots) {
2307 	case 4:
2308 		val |= (1 << 12);
2309 		break;
2310 	case 6:
2311 		val |= (2 << 12);
2312 		break;
2313 	case 8:
2314 		val |= (3 << 12);
2315 		break;
2316 	case 2:
2317 		break;
2318 	default:
2319 		return -EINVAL;
2320 	}
2321 
2322 	switch (slot_width) {
2323 	case 20:
2324 		val |= (1 << 10);
2325 		break;
2326 	case 24:
2327 		val |= (2 << 10);
2328 		break;
2329 	case 32:
2330 		val |= (3 << 10);
2331 		break;
2332 	case 16:
2333 		break;
2334 	default:
2335 		return -EINVAL;
2336 	}
2337 
2338 	snd_soc_update_bits(codec, RT5670_TDM_CTRL_1, 0x7c00, val);
2339 
2340 	return 0;
2341 }
2342 
2343 static int rt5670_set_bias_level(struct snd_soc_codec *codec,
2344 			enum snd_soc_bias_level level)
2345 {
2346 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
2347 
2348 	switch (level) {
2349 	case SND_SOC_BIAS_PREPARE:
2350 		if (SND_SOC_BIAS_STANDBY == codec->dapm.bias_level) {
2351 			snd_soc_update_bits(codec, RT5670_PWR_ANLG1,
2352 				RT5670_PWR_VREF1 | RT5670_PWR_MB |
2353 				RT5670_PWR_BG | RT5670_PWR_VREF2,
2354 				RT5670_PWR_VREF1 | RT5670_PWR_MB |
2355 				RT5670_PWR_BG | RT5670_PWR_VREF2);
2356 			mdelay(10);
2357 			snd_soc_update_bits(codec, RT5670_PWR_ANLG1,
2358 				RT5670_PWR_FV1 | RT5670_PWR_FV2,
2359 				RT5670_PWR_FV1 | RT5670_PWR_FV2);
2360 			snd_soc_update_bits(codec, RT5670_CHARGE_PUMP,
2361 				RT5670_OSW_L_MASK | RT5670_OSW_R_MASK,
2362 				RT5670_OSW_L_DIS | RT5670_OSW_R_DIS);
2363 			snd_soc_update_bits(codec, RT5670_DIG_MISC, 0x1, 0x1);
2364 			snd_soc_update_bits(codec, RT5670_PWR_ANLG1,
2365 				RT5670_LDO_SEL_MASK, 0x3);
2366 		}
2367 		break;
2368 	case SND_SOC_BIAS_STANDBY:
2369 		snd_soc_update_bits(codec, RT5670_PWR_ANLG1,
2370 				RT5670_PWR_VREF1 | RT5670_PWR_VREF2 |
2371 				RT5670_PWR_FV1 | RT5670_PWR_FV2, 0);
2372 		snd_soc_update_bits(codec, RT5670_PWR_ANLG1,
2373 				RT5670_LDO_SEL_MASK, 0x1);
2374 		break;
2375 	case SND_SOC_BIAS_OFF:
2376 		if (rt5670->pdata.jd_mode)
2377 			snd_soc_update_bits(codec, RT5670_PWR_ANLG1,
2378 				RT5670_PWR_VREF1 | RT5670_PWR_MB |
2379 				RT5670_PWR_BG | RT5670_PWR_VREF2 |
2380 				RT5670_PWR_FV1 | RT5670_PWR_FV2,
2381 				RT5670_PWR_MB | RT5670_PWR_BG);
2382 		else
2383 			snd_soc_update_bits(codec, RT5670_PWR_ANLG1,
2384 				RT5670_PWR_VREF1 | RT5670_PWR_MB |
2385 				RT5670_PWR_BG | RT5670_PWR_VREF2 |
2386 				RT5670_PWR_FV1 | RT5670_PWR_FV2, 0);
2387 
2388 		snd_soc_update_bits(codec, RT5670_DIG_MISC, 0x1, 0x0);
2389 		break;
2390 
2391 	default:
2392 		break;
2393 	}
2394 	codec->dapm.bias_level = level;
2395 
2396 	return 0;
2397 }
2398 
2399 static int rt5670_probe(struct snd_soc_codec *codec)
2400 {
2401 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
2402 
2403 	switch (snd_soc_read(codec, RT5670_RESET) & RT5670_ID_MASK) {
2404 	case RT5670_ID_5670:
2405 	case RT5670_ID_5671:
2406 		snd_soc_dapm_new_controls(&codec->dapm,
2407 			rt5670_specific_dapm_widgets,
2408 			ARRAY_SIZE(rt5670_specific_dapm_widgets));
2409 		snd_soc_dapm_add_routes(&codec->dapm,
2410 			rt5670_specific_dapm_routes,
2411 			ARRAY_SIZE(rt5670_specific_dapm_routes));
2412 		break;
2413 	case RT5670_ID_5672:
2414 		snd_soc_dapm_new_controls(&codec->dapm,
2415 			rt5672_specific_dapm_widgets,
2416 			ARRAY_SIZE(rt5672_specific_dapm_widgets));
2417 		snd_soc_dapm_add_routes(&codec->dapm,
2418 			rt5672_specific_dapm_routes,
2419 			ARRAY_SIZE(rt5672_specific_dapm_routes));
2420 		break;
2421 	default:
2422 		dev_err(codec->dev,
2423 			"The driver is for RT5670 RT5671 or RT5672 only\n");
2424 		return -ENODEV;
2425 	}
2426 	rt5670->codec = codec;
2427 
2428 	return 0;
2429 }
2430 
2431 static int rt5670_remove(struct snd_soc_codec *codec)
2432 {
2433 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
2434 
2435 	regmap_write(rt5670->regmap, RT5670_RESET, 0);
2436 	return 0;
2437 }
2438 
2439 #ifdef CONFIG_PM
2440 static int rt5670_suspend(struct snd_soc_codec *codec)
2441 {
2442 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
2443 
2444 	regcache_cache_only(rt5670->regmap, true);
2445 	regcache_mark_dirty(rt5670->regmap);
2446 	return 0;
2447 }
2448 
2449 static int rt5670_resume(struct snd_soc_codec *codec)
2450 {
2451 	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
2452 
2453 	regcache_cache_only(rt5670->regmap, false);
2454 	regcache_sync(rt5670->regmap);
2455 
2456 	return 0;
2457 }
2458 #else
2459 #define rt5670_suspend NULL
2460 #define rt5670_resume NULL
2461 #endif
2462 
2463 #define RT5670_STEREO_RATES SNDRV_PCM_RATE_8000_96000
2464 #define RT5670_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
2465 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
2466 
2467 static struct snd_soc_dai_ops rt5670_aif_dai_ops = {
2468 	.hw_params = rt5670_hw_params,
2469 	.set_fmt = rt5670_set_dai_fmt,
2470 	.set_sysclk = rt5670_set_dai_sysclk,
2471 	.set_tdm_slot = rt5670_set_tdm_slot,
2472 	.set_pll = rt5670_set_dai_pll,
2473 };
2474 
2475 static struct snd_soc_dai_driver rt5670_dai[] = {
2476 	{
2477 		.name = "rt5670-aif1",
2478 		.id = RT5670_AIF1,
2479 		.playback = {
2480 			.stream_name = "AIF1 Playback",
2481 			.channels_min = 1,
2482 			.channels_max = 2,
2483 			.rates = RT5670_STEREO_RATES,
2484 			.formats = RT5670_FORMATS,
2485 		},
2486 		.capture = {
2487 			.stream_name = "AIF1 Capture",
2488 			.channels_min = 1,
2489 			.channels_max = 2,
2490 			.rates = RT5670_STEREO_RATES,
2491 			.formats = RT5670_FORMATS,
2492 		},
2493 		.ops = &rt5670_aif_dai_ops,
2494 	},
2495 	{
2496 		.name = "rt5670-aif2",
2497 		.id = RT5670_AIF2,
2498 		.playback = {
2499 			.stream_name = "AIF2 Playback",
2500 			.channels_min = 1,
2501 			.channels_max = 2,
2502 			.rates = RT5670_STEREO_RATES,
2503 			.formats = RT5670_FORMATS,
2504 		},
2505 		.capture = {
2506 			.stream_name = "AIF2 Capture",
2507 			.channels_min = 1,
2508 			.channels_max = 2,
2509 			.rates = RT5670_STEREO_RATES,
2510 			.formats = RT5670_FORMATS,
2511 		},
2512 		.ops = &rt5670_aif_dai_ops,
2513 	},
2514 };
2515 
2516 static struct snd_soc_codec_driver soc_codec_dev_rt5670 = {
2517 	.probe = rt5670_probe,
2518 	.remove = rt5670_remove,
2519 	.suspend = rt5670_suspend,
2520 	.resume = rt5670_resume,
2521 	.set_bias_level = rt5670_set_bias_level,
2522 	.idle_bias_off = true,
2523 	.controls = rt5670_snd_controls,
2524 	.num_controls = ARRAY_SIZE(rt5670_snd_controls),
2525 	.dapm_widgets = rt5670_dapm_widgets,
2526 	.num_dapm_widgets = ARRAY_SIZE(rt5670_dapm_widgets),
2527 	.dapm_routes = rt5670_dapm_routes,
2528 	.num_dapm_routes = ARRAY_SIZE(rt5670_dapm_routes),
2529 };
2530 
2531 static const struct regmap_config rt5670_regmap = {
2532 	.reg_bits = 8,
2533 	.val_bits = 16,
2534 	.max_register = RT5670_VENDOR_ID2 + 1 + (ARRAY_SIZE(rt5670_ranges) *
2535 					       RT5670_PR_SPACING),
2536 	.volatile_reg = rt5670_volatile_register,
2537 	.readable_reg = rt5670_readable_register,
2538 	.cache_type = REGCACHE_RBTREE,
2539 	.reg_defaults = rt5670_reg,
2540 	.num_reg_defaults = ARRAY_SIZE(rt5670_reg),
2541 	.ranges = rt5670_ranges,
2542 	.num_ranges = ARRAY_SIZE(rt5670_ranges),
2543 };
2544 
2545 static const struct i2c_device_id rt5670_i2c_id[] = {
2546 	{ "rt5670", 0 },
2547 	{ "rt5671", 0 },
2548 	{ "rt5672", 0 },
2549 	{ }
2550 };
2551 MODULE_DEVICE_TABLE(i2c, rt5670_i2c_id);
2552 
2553 #ifdef CONFIG_ACPI
2554 static struct acpi_device_id rt5670_acpi_match[] = {
2555 	{ "10EC5670", 0},
2556 	{ },
2557 };
2558 MODULE_DEVICE_TABLE(acpi, rt5670_acpi_match);
2559 #endif
2560 
2561 static const struct dmi_system_id dmi_platform_intel_braswell[] = {
2562 	{
2563 		.ident = "Intel Braswell",
2564 		.matches = {
2565 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
2566 			DMI_MATCH(DMI_BOARD_NAME, "Braswell CRB"),
2567 		},
2568 	},
2569 	{}
2570 };
2571 
2572 static int rt5670_i2c_probe(struct i2c_client *i2c,
2573 		    const struct i2c_device_id *id)
2574 {
2575 	struct rt5670_platform_data *pdata = dev_get_platdata(&i2c->dev);
2576 	struct rt5670_priv *rt5670;
2577 	int ret;
2578 	unsigned int val;
2579 
2580 	rt5670 = devm_kzalloc(&i2c->dev,
2581 				sizeof(struct rt5670_priv),
2582 				GFP_KERNEL);
2583 	if (NULL == rt5670)
2584 		return -ENOMEM;
2585 
2586 	i2c_set_clientdata(i2c, rt5670);
2587 
2588 	if (pdata)
2589 		rt5670->pdata = *pdata;
2590 
2591 	if (dmi_check_system(dmi_platform_intel_braswell)) {
2592 		rt5670->pdata.dmic_en = true;
2593 		rt5670->pdata.dmic1_data_pin = RT5670_DMIC_DATA_IN2P;
2594 		rt5670->pdata.jd_mode = 1;
2595 	}
2596 
2597 	rt5670->regmap = devm_regmap_init_i2c(i2c, &rt5670_regmap);
2598 	if (IS_ERR(rt5670->regmap)) {
2599 		ret = PTR_ERR(rt5670->regmap);
2600 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
2601 			ret);
2602 		return ret;
2603 	}
2604 
2605 	regmap_read(rt5670->regmap, RT5670_VENDOR_ID2, &val);
2606 	if (val != RT5670_DEVICE_ID) {
2607 		dev_err(&i2c->dev,
2608 			"Device with ID register %x is not rt5670/72\n", val);
2609 		return -ENODEV;
2610 	}
2611 
2612 	regmap_write(rt5670->regmap, RT5670_RESET, 0);
2613 	regmap_update_bits(rt5670->regmap, RT5670_PWR_ANLG1,
2614 		RT5670_PWR_HP_L | RT5670_PWR_HP_R |
2615 		RT5670_PWR_VREF2, RT5670_PWR_VREF2);
2616 	msleep(100);
2617 
2618 	regmap_write(rt5670->regmap, RT5670_RESET, 0);
2619 
2620 	ret = regmap_register_patch(rt5670->regmap, init_list,
2621 				    ARRAY_SIZE(init_list));
2622 	if (ret != 0)
2623 		dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
2624 
2625 	if (rt5670->pdata.in2_diff)
2626 		regmap_update_bits(rt5670->regmap, RT5670_IN2,
2627 					RT5670_IN_DF2, RT5670_IN_DF2);
2628 
2629 	if (i2c->irq) {
2630 		regmap_update_bits(rt5670->regmap, RT5670_GPIO_CTRL1,
2631 				   RT5670_GP1_PIN_MASK, RT5670_GP1_PIN_IRQ);
2632 		regmap_update_bits(rt5670->regmap, RT5670_GPIO_CTRL2,
2633 				   RT5670_GP1_PF_MASK, RT5670_GP1_PF_OUT);
2634 
2635 	}
2636 
2637 	if (rt5670->pdata.jd_mode) {
2638 		regmap_update_bits(rt5670->regmap, RT5670_GLB_CLK,
2639 				   RT5670_SCLK_SRC_MASK, RT5670_SCLK_SRC_RCCLK);
2640 		rt5670->sysclk = 0;
2641 		rt5670->sysclk_src = RT5670_SCLK_S_RCCLK;
2642 		regmap_update_bits(rt5670->regmap, RT5670_PWR_ANLG1,
2643 				   RT5670_PWR_MB, RT5670_PWR_MB);
2644 		regmap_update_bits(rt5670->regmap, RT5670_PWR_ANLG2,
2645 				   RT5670_PWR_JD1, RT5670_PWR_JD1);
2646 		regmap_update_bits(rt5670->regmap, RT5670_IRQ_CTRL1,
2647 				   RT5670_JD1_1_EN_MASK, RT5670_JD1_1_EN);
2648 		regmap_update_bits(rt5670->regmap, RT5670_JD_CTRL3,
2649 				   RT5670_JD_TRI_CBJ_SEL_MASK |
2650 				   RT5670_JD_TRI_HPO_SEL_MASK,
2651 				   RT5670_JD_CBJ_JD1_1 | RT5670_JD_HPO_JD1_1);
2652 		switch (rt5670->pdata.jd_mode) {
2653 		case 1:
2654 			regmap_update_bits(rt5670->regmap, RT5670_A_JD_CTRL1,
2655 					   RT5670_JD1_MODE_MASK,
2656 					   RT5670_JD1_MODE_0);
2657 			break;
2658 		case 2:
2659 			regmap_update_bits(rt5670->regmap, RT5670_A_JD_CTRL1,
2660 					   RT5670_JD1_MODE_MASK,
2661 					   RT5670_JD1_MODE_1);
2662 			break;
2663 		case 3:
2664 			regmap_update_bits(rt5670->regmap, RT5670_A_JD_CTRL1,
2665 					   RT5670_JD1_MODE_MASK,
2666 					   RT5670_JD1_MODE_2);
2667 			break;
2668 		default:
2669 			break;
2670 		}
2671 	}
2672 
2673 	if (rt5670->pdata.dmic_en) {
2674 		regmap_update_bits(rt5670->regmap, RT5670_GPIO_CTRL1,
2675 				   RT5670_GP2_PIN_MASK,
2676 				   RT5670_GP2_PIN_DMIC1_SCL);
2677 
2678 		switch (rt5670->pdata.dmic1_data_pin) {
2679 		case RT5670_DMIC_DATA_IN2P:
2680 			regmap_update_bits(rt5670->regmap, RT5670_DMIC_CTRL1,
2681 					   RT5670_DMIC_1_DP_MASK,
2682 					   RT5670_DMIC_1_DP_IN2P);
2683 			break;
2684 
2685 		case RT5670_DMIC_DATA_GPIO6:
2686 			regmap_update_bits(rt5670->regmap, RT5670_DMIC_CTRL1,
2687 					   RT5670_DMIC_1_DP_MASK,
2688 					   RT5670_DMIC_1_DP_GPIO6);
2689 			regmap_update_bits(rt5670->regmap, RT5670_GPIO_CTRL1,
2690 					   RT5670_GP6_PIN_MASK,
2691 					   RT5670_GP6_PIN_DMIC1_SDA);
2692 			break;
2693 
2694 		case RT5670_DMIC_DATA_GPIO7:
2695 			regmap_update_bits(rt5670->regmap, RT5670_DMIC_CTRL1,
2696 					   RT5670_DMIC_1_DP_MASK,
2697 					   RT5670_DMIC_1_DP_GPIO7);
2698 			regmap_update_bits(rt5670->regmap, RT5670_GPIO_CTRL1,
2699 					   RT5670_GP7_PIN_MASK,
2700 					   RT5670_GP7_PIN_DMIC1_SDA);
2701 			break;
2702 
2703 		default:
2704 			break;
2705 		}
2706 
2707 		switch (rt5670->pdata.dmic2_data_pin) {
2708 		case RT5670_DMIC_DATA_IN3N:
2709 			regmap_update_bits(rt5670->regmap, RT5670_DMIC_CTRL1,
2710 					   RT5670_DMIC_2_DP_MASK,
2711 					   RT5670_DMIC_2_DP_IN3N);
2712 			break;
2713 
2714 		case RT5670_DMIC_DATA_GPIO8:
2715 			regmap_update_bits(rt5670->regmap, RT5670_DMIC_CTRL1,
2716 					   RT5670_DMIC_2_DP_MASK,
2717 					   RT5670_DMIC_2_DP_GPIO8);
2718 			regmap_update_bits(rt5670->regmap, RT5670_GPIO_CTRL1,
2719 					   RT5670_GP8_PIN_MASK,
2720 					   RT5670_GP8_PIN_DMIC2_SDA);
2721 			break;
2722 
2723 		default:
2724 			break;
2725 		}
2726 
2727 		switch (rt5670->pdata.dmic3_data_pin) {
2728 		case RT5670_DMIC_DATA_GPIO5:
2729 			regmap_update_bits(rt5670->regmap, RT5670_DMIC_CTRL2,
2730 					   RT5670_DMIC_3_DP_MASK,
2731 					   RT5670_DMIC_3_DP_GPIO5);
2732 			regmap_update_bits(rt5670->regmap, RT5670_GPIO_CTRL1,
2733 					   RT5670_GP5_PIN_MASK,
2734 					   RT5670_GP5_PIN_DMIC3_SDA);
2735 			break;
2736 
2737 		case RT5670_DMIC_DATA_GPIO9:
2738 		case RT5670_DMIC_DATA_GPIO10:
2739 			dev_err(&i2c->dev,
2740 				"Always use GPIO5 as DMIC3 data pin\n");
2741 			break;
2742 
2743 		default:
2744 			break;
2745 		}
2746 
2747 	}
2748 
2749 	pm_runtime_enable(&i2c->dev);
2750 	pm_request_idle(&i2c->dev);
2751 
2752 	ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt5670,
2753 			rt5670_dai, ARRAY_SIZE(rt5670_dai));
2754 	if (ret < 0)
2755 		goto err;
2756 
2757 	pm_runtime_put(&i2c->dev);
2758 
2759 	return 0;
2760 err:
2761 	pm_runtime_disable(&i2c->dev);
2762 
2763 	return ret;
2764 }
2765 
2766 static int rt5670_i2c_remove(struct i2c_client *i2c)
2767 {
2768 	pm_runtime_disable(&i2c->dev);
2769 	snd_soc_unregister_codec(&i2c->dev);
2770 
2771 	return 0;
2772 }
2773 
2774 static struct i2c_driver rt5670_i2c_driver = {
2775 	.driver = {
2776 		.name = "rt5670",
2777 		.owner = THIS_MODULE,
2778 		.acpi_match_table = ACPI_PTR(rt5670_acpi_match),
2779 	},
2780 	.probe = rt5670_i2c_probe,
2781 	.remove   = rt5670_i2c_remove,
2782 	.id_table = rt5670_i2c_id,
2783 };
2784 
2785 module_i2c_driver(rt5670_i2c_driver);
2786 
2787 MODULE_DESCRIPTION("ASoC RT5670 driver");
2788 MODULE_AUTHOR("Bard Liao <bardliao@realtek.com>");
2789 MODULE_LICENSE("GPL v2");
2790