xref: /openbmc/linux/sound/soc/codecs/rt5660.c (revision 1a59d1b8)
1 /*
2  * rt5660.c  --  RT5660 ALSA SoC audio codec driver
3  *
4  * Copyright 2016 Realtek Semiconductor Corp.
5  * Author: Oder Chiou <oder_chiou@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/gpio.h>
18 #include <linux/i2c.h>
19 #include <linux/regmap.h>
20 #include <linux/of.h>
21 #include <linux/of_gpio.h>
22 #include <linux/platform_device.h>
23 #include <linux/spi/spi.h>
24 #include <linux/acpi.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/initval.h>
31 #include <sound/tlv.h>
32 
33 #include "rl6231.h"
34 #include "rt5660.h"
35 
36 #define RT5660_DEVICE_ID 0x6338
37 
38 #define RT5660_PR_RANGE_BASE (0xff + 1)
39 #define RT5660_PR_SPACING 0x100
40 
41 #define RT5660_PR_BASE (RT5660_PR_RANGE_BASE + (0 * RT5660_PR_SPACING))
42 
43 static const struct regmap_range_cfg rt5660_ranges[] = {
44 	{ .name = "PR", .range_min = RT5660_PR_BASE,
45 	  .range_max = RT5660_PR_BASE + 0xf3,
46 	  .selector_reg = RT5660_PRIV_INDEX,
47 	  .selector_mask = 0xff,
48 	  .selector_shift = 0x0,
49 	  .window_start = RT5660_PRIV_DATA,
50 	  .window_len = 0x1, },
51 };
52 
53 static const struct reg_sequence rt5660_patch[] = {
54 	{ RT5660_ALC_PGA_CTRL2,		0x44c3 },
55 	{ RT5660_PR_BASE + 0x3d,	0x2600 },
56 };
57 
58 static const struct reg_default rt5660_reg[] = {
59 	{ 0x00, 0x0000 },
60 	{ 0x01, 0xc800 },
61 	{ 0x02, 0xc8c8 },
62 	{ 0x0d, 0x1010 },
63 	{ 0x0e, 0x1010 },
64 	{ 0x19, 0xafaf },
65 	{ 0x1c, 0x2f2f },
66 	{ 0x1e, 0x0000 },
67 	{ 0x27, 0x6060 },
68 	{ 0x29, 0x8080 },
69 	{ 0x2a, 0x4242 },
70 	{ 0x2f, 0x0000 },
71 	{ 0x3b, 0x0000 },
72 	{ 0x3c, 0x007f },
73 	{ 0x3d, 0x0000 },
74 	{ 0x3e, 0x007f },
75 	{ 0x45, 0xe000 },
76 	{ 0x46, 0x003e },
77 	{ 0x48, 0xf800 },
78 	{ 0x4a, 0x0004 },
79 	{ 0x4d, 0x0000 },
80 	{ 0x4e, 0x0000 },
81 	{ 0x4f, 0x01ff },
82 	{ 0x50, 0x0000 },
83 	{ 0x51, 0x0000 },
84 	{ 0x52, 0x01ff },
85 	{ 0x61, 0x0000 },
86 	{ 0x62, 0x0000 },
87 	{ 0x63, 0x00c0 },
88 	{ 0x64, 0x0000 },
89 	{ 0x65, 0x0000 },
90 	{ 0x66, 0x0000 },
91 	{ 0x70, 0x8000 },
92 	{ 0x73, 0x7000 },
93 	{ 0x74, 0x3c00 },
94 	{ 0x75, 0x2800 },
95 	{ 0x80, 0x0000 },
96 	{ 0x81, 0x0000 },
97 	{ 0x82, 0x0000 },
98 	{ 0x8c, 0x0228 },
99 	{ 0x8d, 0xa000 },
100 	{ 0x8e, 0x0000 },
101 	{ 0x92, 0x0000 },
102 	{ 0x93, 0x3000 },
103 	{ 0xa1, 0x0059 },
104 	{ 0xa2, 0x0001 },
105 	{ 0xa3, 0x5c80 },
106 	{ 0xa4, 0x0146 },
107 	{ 0xa5, 0x1f1f },
108 	{ 0xa6, 0x78c6 },
109 	{ 0xa7, 0xe5ec },
110 	{ 0xa8, 0xba61 },
111 	{ 0xa9, 0x3c78 },
112 	{ 0xaa, 0x8ae2 },
113 	{ 0xab, 0xe5ec },
114 	{ 0xac, 0xc600 },
115 	{ 0xad, 0xba61 },
116 	{ 0xae, 0x17ed },
117 	{ 0xb0, 0x2080 },
118 	{ 0xb1, 0x0000 },
119 	{ 0xb3, 0x001f },
120 	{ 0xb4, 0x020c },
121 	{ 0xb5, 0x1f00 },
122 	{ 0xb6, 0x0000 },
123 	{ 0xb7, 0x4000 },
124 	{ 0xbb, 0x0000 },
125 	{ 0xbd, 0x0000 },
126 	{ 0xbe, 0x0000 },
127 	{ 0xbf, 0x0100 },
128 	{ 0xc0, 0x0000 },
129 	{ 0xc2, 0x0000 },
130 	{ 0xd3, 0xa220 },
131 	{ 0xd9, 0x0809 },
132 	{ 0xda, 0x0000 },
133 	{ 0xe0, 0x8000 },
134 	{ 0xe1, 0x0200 },
135 	{ 0xe2, 0x8000 },
136 	{ 0xe3, 0x0200 },
137 	{ 0xe4, 0x0f20 },
138 	{ 0xe5, 0x001f },
139 	{ 0xe6, 0x020c },
140 	{ 0xe7, 0x1f00 },
141 	{ 0xe8, 0x0000 },
142 	{ 0xe9, 0x4000 },
143 	{ 0xea, 0x00a6 },
144 	{ 0xeb, 0x04c3 },
145 	{ 0xec, 0x27c8 },
146 	{ 0xed, 0x7418 },
147 	{ 0xee, 0xbf50 },
148 	{ 0xef, 0x0045 },
149 	{ 0xf0, 0x0007 },
150 	{ 0xfa, 0x0000 },
151 	{ 0xfd, 0x0000 },
152 	{ 0xfe, 0x10ec },
153 	{ 0xff, 0x6338 },
154 };
155 
156 static bool rt5660_volatile_register(struct device *dev, unsigned int reg)
157 {
158 	int i;
159 
160 	for (i = 0; i < ARRAY_SIZE(rt5660_ranges); i++)
161 		if ((reg >= rt5660_ranges[i].window_start &&
162 		     reg <= rt5660_ranges[i].window_start +
163 		     rt5660_ranges[i].window_len) ||
164 		    (reg >= rt5660_ranges[i].range_min &&
165 		     reg <= rt5660_ranges[i].range_max))
166 			return true;
167 
168 	switch (reg) {
169 	case RT5660_RESET:
170 	case RT5660_PRIV_DATA:
171 	case RT5660_EQ_CTRL1:
172 	case RT5660_IRQ_CTRL2:
173 	case RT5660_INT_IRQ_ST:
174 	case RT5660_VENDOR_ID:
175 	case RT5660_VENDOR_ID1:
176 	case RT5660_VENDOR_ID2:
177 		return true;
178 	default:
179 		return false;
180 	}
181 }
182 
183 static bool rt5660_readable_register(struct device *dev, unsigned int reg)
184 {
185 	int i;
186 
187 	for (i = 0; i < ARRAY_SIZE(rt5660_ranges); i++)
188 		if ((reg >= rt5660_ranges[i].window_start &&
189 		     reg <= rt5660_ranges[i].window_start +
190 		     rt5660_ranges[i].window_len) ||
191 		    (reg >= rt5660_ranges[i].range_min &&
192 		     reg <= rt5660_ranges[i].range_max))
193 			return true;
194 
195 	switch (reg) {
196 	case RT5660_RESET:
197 	case RT5660_SPK_VOL:
198 	case RT5660_LOUT_VOL:
199 	case RT5660_IN1_IN2:
200 	case RT5660_IN3_IN4:
201 	case RT5660_DAC1_DIG_VOL:
202 	case RT5660_STO1_ADC_DIG_VOL:
203 	case RT5660_ADC_BST_VOL1:
204 	case RT5660_STO1_ADC_MIXER:
205 	case RT5660_AD_DA_MIXER:
206 	case RT5660_STO_DAC_MIXER:
207 	case RT5660_DIG_INF1_DATA:
208 	case RT5660_REC_L1_MIXER:
209 	case RT5660_REC_L2_MIXER:
210 	case RT5660_REC_R1_MIXER:
211 	case RT5660_REC_R2_MIXER:
212 	case RT5660_LOUT_MIXER:
213 	case RT5660_SPK_MIXER:
214 	case RT5660_SPO_MIXER:
215 	case RT5660_SPO_CLSD_RATIO:
216 	case RT5660_OUT_L_GAIN1:
217 	case RT5660_OUT_L_GAIN2:
218 	case RT5660_OUT_L1_MIXER:
219 	case RT5660_OUT_R_GAIN1:
220 	case RT5660_OUT_R_GAIN2:
221 	case RT5660_OUT_R1_MIXER:
222 	case RT5660_PWR_DIG1:
223 	case RT5660_PWR_DIG2:
224 	case RT5660_PWR_ANLG1:
225 	case RT5660_PWR_ANLG2:
226 	case RT5660_PWR_MIXER:
227 	case RT5660_PWR_VOL:
228 	case RT5660_PRIV_INDEX:
229 	case RT5660_PRIV_DATA:
230 	case RT5660_I2S1_SDP:
231 	case RT5660_ADDA_CLK1:
232 	case RT5660_ADDA_CLK2:
233 	case RT5660_DMIC_CTRL1:
234 	case RT5660_GLB_CLK:
235 	case RT5660_PLL_CTRL1:
236 	case RT5660_PLL_CTRL2:
237 	case RT5660_CLSD_AMP_OC_CTRL:
238 	case RT5660_CLSD_AMP_CTRL:
239 	case RT5660_LOUT_AMP_CTRL:
240 	case RT5660_SPK_AMP_SPKVDD:
241 	case RT5660_MICBIAS:
242 	case RT5660_CLSD_OUT_CTRL1:
243 	case RT5660_CLSD_OUT_CTRL2:
244 	case RT5660_DIPOLE_MIC_CTRL1:
245 	case RT5660_DIPOLE_MIC_CTRL2:
246 	case RT5660_DIPOLE_MIC_CTRL3:
247 	case RT5660_DIPOLE_MIC_CTRL4:
248 	case RT5660_DIPOLE_MIC_CTRL5:
249 	case RT5660_DIPOLE_MIC_CTRL6:
250 	case RT5660_DIPOLE_MIC_CTRL7:
251 	case RT5660_DIPOLE_MIC_CTRL8:
252 	case RT5660_DIPOLE_MIC_CTRL9:
253 	case RT5660_DIPOLE_MIC_CTRL10:
254 	case RT5660_DIPOLE_MIC_CTRL11:
255 	case RT5660_DIPOLE_MIC_CTRL12:
256 	case RT5660_EQ_CTRL1:
257 	case RT5660_EQ_CTRL2:
258 	case RT5660_DRC_AGC_CTRL1:
259 	case RT5660_DRC_AGC_CTRL2:
260 	case RT5660_DRC_AGC_CTRL3:
261 	case RT5660_DRC_AGC_CTRL4:
262 	case RT5660_DRC_AGC_CTRL5:
263 	case RT5660_JD_CTRL:
264 	case RT5660_IRQ_CTRL1:
265 	case RT5660_IRQ_CTRL2:
266 	case RT5660_INT_IRQ_ST:
267 	case RT5660_GPIO_CTRL1:
268 	case RT5660_GPIO_CTRL2:
269 	case RT5660_WIND_FILTER_CTRL1:
270 	case RT5660_SV_ZCD1:
271 	case RT5660_SV_ZCD2:
272 	case RT5660_DRC1_LM_CTRL1:
273 	case RT5660_DRC1_LM_CTRL2:
274 	case RT5660_DRC2_LM_CTRL1:
275 	case RT5660_DRC2_LM_CTRL2:
276 	case RT5660_MULTI_DRC_CTRL:
277 	case RT5660_DRC2_CTRL1:
278 	case RT5660_DRC2_CTRL2:
279 	case RT5660_DRC2_CTRL3:
280 	case RT5660_DRC2_CTRL4:
281 	case RT5660_DRC2_CTRL5:
282 	case RT5660_ALC_PGA_CTRL1:
283 	case RT5660_ALC_PGA_CTRL2:
284 	case RT5660_ALC_PGA_CTRL3:
285 	case RT5660_ALC_PGA_CTRL4:
286 	case RT5660_ALC_PGA_CTRL5:
287 	case RT5660_ALC_PGA_CTRL6:
288 	case RT5660_ALC_PGA_CTRL7:
289 	case RT5660_GEN_CTRL1:
290 	case RT5660_GEN_CTRL2:
291 	case RT5660_GEN_CTRL3:
292 	case RT5660_VENDOR_ID:
293 	case RT5660_VENDOR_ID1:
294 	case RT5660_VENDOR_ID2:
295 		return true;
296 	default:
297 		return false;
298 	}
299 }
300 
301 static const DECLARE_TLV_DB_SCALE(rt5660_out_vol_tlv, -4650, 150, 0);
302 static const DECLARE_TLV_DB_SCALE(rt5660_dac_vol_tlv, -6525, 75, 0);
303 static const DECLARE_TLV_DB_SCALE(rt5660_adc_vol_tlv, -1725, 75, 0);
304 static const DECLARE_TLV_DB_SCALE(rt5660_adc_bst_tlv, 0, 1200, 0);
305 static const DECLARE_TLV_DB_SCALE(rt5660_bst_tlv, -1200, 75, 0);
306 
307 static const struct snd_kcontrol_new rt5660_snd_controls[] = {
308 	/* Speaker Output Volume */
309 	SOC_SINGLE("Speaker Playback Switch", RT5660_SPK_VOL, RT5660_L_MUTE_SFT,
310 		1, 1),
311 	SOC_SINGLE_TLV("Speaker Playback Volume", RT5660_SPK_VOL,
312 		RT5660_L_VOL_SFT, 39, 1, rt5660_out_vol_tlv),
313 
314 	/* OUTPUT Control */
315 	SOC_DOUBLE("OUT Playback Switch", RT5660_LOUT_VOL, RT5660_L_MUTE_SFT,
316 		RT5660_R_MUTE_SFT, 1, 1),
317 	SOC_DOUBLE_TLV("OUT Playback Volume", RT5660_LOUT_VOL, RT5660_L_VOL_SFT,
318 		RT5660_R_VOL_SFT, 39, 1, rt5660_out_vol_tlv),
319 
320 	/* DAC Digital Volume */
321 	SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5660_DAC1_DIG_VOL,
322 		RT5660_DAC_L1_VOL_SFT, RT5660_DAC_R1_VOL_SFT, 87, 0,
323 		rt5660_dac_vol_tlv),
324 
325 	/* IN1/IN2/IN3 Control */
326 	SOC_SINGLE_TLV("IN1 Boost Volume", RT5660_IN1_IN2, RT5660_BST_SFT1, 69,
327 		0, rt5660_bst_tlv),
328 	SOC_SINGLE_TLV("IN2 Boost Volume", RT5660_IN1_IN2, RT5660_BST_SFT2, 69,
329 		0, rt5660_bst_tlv),
330 	SOC_SINGLE_TLV("IN3 Boost Volume", RT5660_IN3_IN4, RT5660_BST_SFT3, 69,
331 		0, rt5660_bst_tlv),
332 
333 	/* ADC Digital Volume Control */
334 	SOC_DOUBLE("ADC Capture Switch", RT5660_STO1_ADC_DIG_VOL,
335 		RT5660_L_MUTE_SFT, RT5660_R_MUTE_SFT, 1, 1),
336 	SOC_DOUBLE_TLV("ADC Capture Volume", RT5660_STO1_ADC_DIG_VOL,
337 		RT5660_ADC_L_VOL_SFT, RT5660_ADC_R_VOL_SFT, 63, 0,
338 		rt5660_adc_vol_tlv),
339 
340 	/* ADC Boost Volume Control */
341 	SOC_DOUBLE_TLV("STO1 ADC Boost Gain Volume", RT5660_ADC_BST_VOL1,
342 		RT5660_STO1_ADC_L_BST_SFT, RT5660_STO1_ADC_R_BST_SFT, 3, 0,
343 		rt5660_adc_bst_tlv),
344 };
345 
346 /**
347  * rt5660_set_dmic_clk - Set parameter of dmic.
348  *
349  * @w: DAPM widget.
350  * @kcontrol: The kcontrol of this widget.
351  * @event: Event id.
352  *
353  */
354 static int rt5660_set_dmic_clk(struct snd_soc_dapm_widget *w,
355 	struct snd_kcontrol *kcontrol, int event)
356 {
357 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
358 	struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
359 	int idx, rate;
360 
361 	rate = rt5660->sysclk / rl6231_get_pre_div(rt5660->regmap,
362 		RT5660_ADDA_CLK1, RT5660_I2S_PD1_SFT);
363 	idx = rl6231_calc_dmic_clk(rate);
364 	if (idx < 0)
365 		dev_err(component->dev, "Failed to set DMIC clock\n");
366 	else
367 		snd_soc_component_update_bits(component, RT5660_DMIC_CTRL1,
368 			RT5660_DMIC_CLK_MASK, idx << RT5660_DMIC_CLK_SFT);
369 
370 	return idx;
371 }
372 
373 static int rt5660_is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
374 			 struct snd_soc_dapm_widget *sink)
375 {
376 	struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
377 	unsigned int val;
378 
379 	val = snd_soc_component_read32(component, RT5660_GLB_CLK);
380 	val &= RT5660_SCLK_SRC_MASK;
381 	if (val == RT5660_SCLK_SRC_PLL1)
382 		return 1;
383 	else
384 		return 0;
385 }
386 
387 /* Digital Mixer */
388 static const struct snd_kcontrol_new rt5660_sto1_adc_l_mix[] = {
389 	SOC_DAPM_SINGLE("ADC1 Switch", RT5660_STO1_ADC_MIXER,
390 			RT5660_M_ADC_L1_SFT, 1, 1),
391 	SOC_DAPM_SINGLE("ADC2 Switch", RT5660_STO1_ADC_MIXER,
392 			RT5660_M_ADC_L2_SFT, 1, 1),
393 };
394 
395 static const struct snd_kcontrol_new rt5660_sto1_adc_r_mix[] = {
396 	SOC_DAPM_SINGLE("ADC1 Switch", RT5660_STO1_ADC_MIXER,
397 			RT5660_M_ADC_R1_SFT, 1, 1),
398 	SOC_DAPM_SINGLE("ADC2 Switch", RT5660_STO1_ADC_MIXER,
399 			RT5660_M_ADC_R2_SFT, 1, 1),
400 };
401 
402 static const struct snd_kcontrol_new rt5660_dac_l_mix[] = {
403 	SOC_DAPM_SINGLE("Stereo ADC Switch", RT5660_AD_DA_MIXER,
404 			RT5660_M_ADCMIX_L_SFT, 1, 1),
405 	SOC_DAPM_SINGLE("DAC1 Switch", RT5660_AD_DA_MIXER,
406 			RT5660_M_DAC1_L_SFT, 1, 1),
407 };
408 
409 static const struct snd_kcontrol_new rt5660_dac_r_mix[] = {
410 	SOC_DAPM_SINGLE("Stereo ADC Switch", RT5660_AD_DA_MIXER,
411 			RT5660_M_ADCMIX_R_SFT, 1, 1),
412 	SOC_DAPM_SINGLE("DAC1 Switch", RT5660_AD_DA_MIXER,
413 			RT5660_M_DAC1_R_SFT, 1, 1),
414 };
415 
416 static const struct snd_kcontrol_new rt5660_sto_dac_l_mix[] = {
417 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5660_STO_DAC_MIXER,
418 			RT5660_M_DAC_L1_SFT, 1, 1),
419 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5660_STO_DAC_MIXER,
420 			RT5660_M_DAC_R1_STO_L_SFT, 1, 1),
421 };
422 
423 static const struct snd_kcontrol_new rt5660_sto_dac_r_mix[] = {
424 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5660_STO_DAC_MIXER,
425 			RT5660_M_DAC_R1_SFT, 1, 1),
426 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5660_STO_DAC_MIXER,
427 			RT5660_M_DAC_L1_STO_R_SFT, 1, 1),
428 };
429 
430 /* Analog Input Mixer */
431 static const struct snd_kcontrol_new rt5660_rec_l_mix[] = {
432 	SOC_DAPM_SINGLE("BST3 Switch", RT5660_REC_L2_MIXER,
433 			RT5660_M_BST3_RM_L_SFT, 1, 1),
434 	SOC_DAPM_SINGLE("BST2 Switch", RT5660_REC_L2_MIXER,
435 			RT5660_M_BST2_RM_L_SFT, 1, 1),
436 	SOC_DAPM_SINGLE("BST1 Switch", RT5660_REC_L2_MIXER,
437 			RT5660_M_BST1_RM_L_SFT, 1, 1),
438 	SOC_DAPM_SINGLE("OUT MIXL Switch", RT5660_REC_L2_MIXER,
439 			RT5660_M_OM_L_RM_L_SFT, 1, 1),
440 };
441 
442 static const struct snd_kcontrol_new rt5660_rec_r_mix[] = {
443 	SOC_DAPM_SINGLE("BST3 Switch", RT5660_REC_R2_MIXER,
444 			RT5660_M_BST3_RM_R_SFT, 1, 1),
445 	SOC_DAPM_SINGLE("BST2 Switch", RT5660_REC_R2_MIXER,
446 			RT5660_M_BST2_RM_R_SFT, 1, 1),
447 	SOC_DAPM_SINGLE("BST1 Switch", RT5660_REC_R2_MIXER,
448 			RT5660_M_BST1_RM_R_SFT, 1, 1),
449 	SOC_DAPM_SINGLE("OUT MIXR Switch", RT5660_REC_R2_MIXER,
450 			RT5660_M_OM_R_RM_R_SFT, 1, 1),
451 };
452 
453 static const struct snd_kcontrol_new rt5660_spk_mix[] = {
454 	SOC_DAPM_SINGLE("BST3 Switch", RT5660_SPK_MIXER,
455 			RT5660_M_BST3_SM_SFT, 1, 1),
456 	SOC_DAPM_SINGLE("BST1 Switch", RT5660_SPK_MIXER,
457 			RT5660_M_BST1_SM_SFT, 1, 1),
458 	SOC_DAPM_SINGLE("DACL Switch", RT5660_SPK_MIXER,
459 			RT5660_M_DACL_SM_SFT, 1, 1),
460 	SOC_DAPM_SINGLE("DACR Switch", RT5660_SPK_MIXER,
461 			RT5660_M_DACR_SM_SFT, 1, 1),
462 	SOC_DAPM_SINGLE("OUTMIXL Switch", RT5660_SPK_MIXER,
463 			RT5660_M_OM_L_SM_SFT, 1, 1),
464 };
465 
466 static const struct snd_kcontrol_new rt5660_out_l_mix[] = {
467 	SOC_DAPM_SINGLE("BST3 Switch", RT5660_OUT_L1_MIXER,
468 			RT5660_M_BST3_OM_L_SFT, 1, 1),
469 	SOC_DAPM_SINGLE("BST2 Switch", RT5660_OUT_L1_MIXER,
470 			RT5660_M_BST2_OM_L_SFT, 1, 1),
471 	SOC_DAPM_SINGLE("BST1 Switch", RT5660_OUT_L1_MIXER,
472 			RT5660_M_BST1_OM_L_SFT, 1, 1),
473 	SOC_DAPM_SINGLE("RECMIXL Switch", RT5660_OUT_L1_MIXER,
474 			RT5660_M_RM_L_OM_L_SFT, 1, 1),
475 	SOC_DAPM_SINGLE("DACR Switch", RT5660_OUT_L1_MIXER,
476 			RT5660_M_DAC_R_OM_L_SFT, 1, 1),
477 	SOC_DAPM_SINGLE("DACL Switch", RT5660_OUT_L1_MIXER,
478 			RT5660_M_DAC_L_OM_L_SFT, 1, 1),
479 };
480 
481 static const struct snd_kcontrol_new rt5660_out_r_mix[] = {
482 	SOC_DAPM_SINGLE("BST2 Switch", RT5660_OUT_R1_MIXER,
483 			RT5660_M_BST2_OM_R_SFT, 1, 1),
484 	SOC_DAPM_SINGLE("BST1 Switch", RT5660_OUT_R1_MIXER,
485 			RT5660_M_BST1_OM_R_SFT, 1, 1),
486 	SOC_DAPM_SINGLE("RECMIXR Switch", RT5660_OUT_R1_MIXER,
487 			RT5660_M_RM_R_OM_R_SFT, 1, 1),
488 	SOC_DAPM_SINGLE("DACR Switch", RT5660_OUT_R1_MIXER,
489 			RT5660_M_DAC_R_OM_R_SFT, 1, 1),
490 	SOC_DAPM_SINGLE("DACL Switch", RT5660_OUT_R1_MIXER,
491 			RT5660_M_DAC_L_OM_R_SFT, 1, 1),
492 };
493 
494 static const struct snd_kcontrol_new rt5660_spo_mix[] = {
495 	SOC_DAPM_SINGLE("DACR Switch", RT5660_SPO_MIXER,
496 			RT5660_M_DAC_R_SPM_SFT, 1, 1),
497 	SOC_DAPM_SINGLE("DACL Switch", RT5660_SPO_MIXER,
498 			RT5660_M_DAC_L_SPM_SFT, 1, 1),
499 	SOC_DAPM_SINGLE("SPKVOL Switch", RT5660_SPO_MIXER,
500 			RT5660_M_SV_SPM_SFT, 1, 1),
501 	SOC_DAPM_SINGLE("BST1 Switch", RT5660_SPO_MIXER,
502 			RT5660_M_BST1_SPM_SFT, 1, 1),
503 };
504 
505 static const struct snd_kcontrol_new rt5660_lout_mix[] = {
506 	SOC_DAPM_SINGLE("DAC Switch", RT5660_LOUT_MIXER,
507 			RT5660_M_DAC1_LM_SFT, 1, 1),
508 	SOC_DAPM_SINGLE("OUTMIX Switch", RT5660_LOUT_MIXER,
509 			RT5660_M_LOVOL_LM_SFT, 1, 1),
510 };
511 
512 static const struct snd_kcontrol_new spk_vol_control =
513 	SOC_DAPM_SINGLE("Switch", RT5660_SPK_VOL,
514 		RT5660_VOL_L_SFT, 1, 1);
515 
516 static const struct snd_kcontrol_new lout_l_vol_control =
517 	SOC_DAPM_SINGLE("Switch", RT5660_LOUT_VOL,
518 		RT5660_VOL_L_SFT, 1, 1);
519 
520 static const struct snd_kcontrol_new lout_r_vol_control =
521 	SOC_DAPM_SINGLE("Switch", RT5660_LOUT_VOL,
522 		RT5660_VOL_R_SFT, 1, 1);
523 
524 /* Interface data select */
525 static const char * const rt5660_data_select[] = {
526 	"L/R", "R/L", "L/L", "R/R"
527 };
528 
529 static SOC_ENUM_SINGLE_DECL(rt5660_if1_dac_enum,
530 	RT5660_DIG_INF1_DATA, RT5660_IF1_DAC_IN_SFT, rt5660_data_select);
531 
532 static SOC_ENUM_SINGLE_DECL(rt5660_if1_adc_enum,
533 	RT5660_DIG_INF1_DATA, RT5660_IF1_ADC_IN_SFT, rt5660_data_select);
534 
535 static const struct snd_kcontrol_new rt5660_if1_dac_swap_mux =
536 	SOC_DAPM_ENUM("IF1 DAC Swap Source", rt5660_if1_dac_enum);
537 
538 static const struct snd_kcontrol_new rt5660_if1_adc_swap_mux =
539 	SOC_DAPM_ENUM("IF1 ADC Swap Source", rt5660_if1_adc_enum);
540 
541 static int rt5660_lout_event(struct snd_soc_dapm_widget *w,
542 	struct snd_kcontrol *kcontrol, int event)
543 {
544 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
545 
546 	switch (event) {
547 	case SND_SOC_DAPM_POST_PMU:
548 		snd_soc_component_update_bits(component, RT5660_LOUT_AMP_CTRL,
549 			RT5660_LOUT_CO_MASK | RT5660_LOUT_CB_MASK,
550 			RT5660_LOUT_CO_EN | RT5660_LOUT_CB_PU);
551 		break;
552 
553 	case SND_SOC_DAPM_PRE_PMD:
554 		snd_soc_component_update_bits(component, RT5660_LOUT_AMP_CTRL,
555 			RT5660_LOUT_CO_MASK | RT5660_LOUT_CB_MASK,
556 			RT5660_LOUT_CO_DIS | RT5660_LOUT_CB_PD);
557 		break;
558 
559 	default:
560 		return 0;
561 	}
562 
563 	return 0;
564 }
565 
566 static const struct snd_soc_dapm_widget rt5660_dapm_widgets[] = {
567 	SND_SOC_DAPM_SUPPLY("LDO2", RT5660_PWR_ANLG1,
568 		RT5660_PWR_LDO2_BIT, 0, NULL, 0),
569 	SND_SOC_DAPM_SUPPLY("PLL1", RT5660_PWR_ANLG2,
570 		RT5660_PWR_PLL_BIT, 0, NULL, 0),
571 
572 	/* MICBIAS */
573 	SND_SOC_DAPM_SUPPLY("MICBIAS1", RT5660_PWR_ANLG2,
574 			RT5660_PWR_MB1_BIT, 0, NULL, 0),
575 	SND_SOC_DAPM_SUPPLY("MICBIAS2", RT5660_PWR_ANLG2,
576 			RT5660_PWR_MB2_BIT, 0, NULL, 0),
577 
578 	/* Input Side */
579 	/* Input Lines */
580 	SND_SOC_DAPM_INPUT("DMIC L1"),
581 	SND_SOC_DAPM_INPUT("DMIC R1"),
582 
583 	SND_SOC_DAPM_INPUT("IN1P"),
584 	SND_SOC_DAPM_INPUT("IN1N"),
585 	SND_SOC_DAPM_INPUT("IN2P"),
586 	SND_SOC_DAPM_INPUT("IN3P"),
587 	SND_SOC_DAPM_INPUT("IN3N"),
588 
589 	SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM, 0, 0,
590 		rt5660_set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
591 	SND_SOC_DAPM_SUPPLY("DMIC Power", RT5660_DMIC_CTRL1,
592 		RT5660_DMIC_1_EN_SFT, 0, NULL, 0),
593 
594 	/* Boost */
595 	SND_SOC_DAPM_PGA("BST1", RT5660_PWR_ANLG2, RT5660_PWR_BST1_BIT, 0,
596 		NULL, 0),
597 	SND_SOC_DAPM_PGA("BST2", RT5660_PWR_ANLG2, RT5660_PWR_BST2_BIT, 0,
598 		NULL, 0),
599 	SND_SOC_DAPM_PGA("BST3", RT5660_PWR_ANLG2, RT5660_PWR_BST3_BIT, 0,
600 		NULL, 0),
601 
602 	/* REC Mixer */
603 	SND_SOC_DAPM_MIXER("RECMIXL", RT5660_PWR_MIXER, RT5660_PWR_RM_L_BIT,
604 			0, rt5660_rec_l_mix, ARRAY_SIZE(rt5660_rec_l_mix)),
605 	SND_SOC_DAPM_MIXER("RECMIXR", RT5660_PWR_MIXER, RT5660_PWR_RM_R_BIT,
606 			0, rt5660_rec_r_mix, ARRAY_SIZE(rt5660_rec_r_mix)),
607 
608 	/* ADCs */
609 	SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
610 	SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
611 
612 	SND_SOC_DAPM_SUPPLY("ADC L power", RT5660_PWR_DIG1,
613 			RT5660_PWR_ADC_L_BIT, 0, NULL, 0),
614 	SND_SOC_DAPM_SUPPLY("ADC R power", RT5660_PWR_DIG1,
615 			RT5660_PWR_ADC_R_BIT, 0, NULL, 0),
616 	SND_SOC_DAPM_SUPPLY("ADC clock", RT5660_PR_BASE + RT5660_CHOP_DAC_ADC,
617 			12, 0, NULL, 0),
618 
619 	/* ADC Mixer */
620 	SND_SOC_DAPM_SUPPLY("adc stereo1 filter", RT5660_PWR_DIG2,
621 		RT5660_PWR_ADC_S1F_BIT, 0, NULL, 0),
622 	SND_SOC_DAPM_MIXER("Sto1 ADC MIXL", SND_SOC_NOPM, 0, 0,
623 		rt5660_sto1_adc_l_mix, ARRAY_SIZE(rt5660_sto1_adc_l_mix)),
624 	SND_SOC_DAPM_MIXER("Sto1 ADC MIXR", SND_SOC_NOPM, 0, 0,
625 		rt5660_sto1_adc_r_mix, ARRAY_SIZE(rt5660_sto1_adc_r_mix)),
626 
627 	/* ADC */
628 	SND_SOC_DAPM_ADC("Stereo1 ADC MIXL", NULL, RT5660_STO1_ADC_DIG_VOL,
629 		RT5660_L_MUTE_SFT, 1),
630 	SND_SOC_DAPM_ADC("Stereo1 ADC MIXR", NULL, RT5660_STO1_ADC_DIG_VOL,
631 		RT5660_R_MUTE_SFT, 1),
632 
633 	/* Digital Interface */
634 	SND_SOC_DAPM_SUPPLY("I2S1", RT5660_PWR_DIG1, RT5660_PWR_I2S1_BIT, 0,
635 		NULL, 0),
636 	SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
637 	SND_SOC_DAPM_PGA("IF1 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
638 	SND_SOC_DAPM_PGA("IF1 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
639 	SND_SOC_DAPM_MUX("IF1 DAC Swap Mux", SND_SOC_NOPM, 0, 0,
640 			&rt5660_if1_dac_swap_mux),
641 	SND_SOC_DAPM_PGA("IF1 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
642 	SND_SOC_DAPM_MUX("IF1 ADC Swap Mux", SND_SOC_NOPM, 0, 0,
643 			&rt5660_if1_adc_swap_mux),
644 
645 	/* Audio Interface */
646 	SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
647 	SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
648 
649 	/* Output Side */
650 	/* DAC mixer before sound effect  */
651 	SND_SOC_DAPM_MIXER("DAC1 MIXL", SND_SOC_NOPM, 0, 0, rt5660_dac_l_mix,
652 		ARRAY_SIZE(rt5660_dac_l_mix)),
653 	SND_SOC_DAPM_MIXER("DAC1 MIXR", SND_SOC_NOPM, 0, 0, rt5660_dac_r_mix,
654 		ARRAY_SIZE(rt5660_dac_r_mix)),
655 
656 	/* DAC Mixer */
657 	SND_SOC_DAPM_SUPPLY("dac stereo1 filter", RT5660_PWR_DIG2,
658 		RT5660_PWR_DAC_S1F_BIT, 0, NULL, 0),
659 	SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
660 		rt5660_sto_dac_l_mix, ARRAY_SIZE(rt5660_sto_dac_l_mix)),
661 	SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
662 		rt5660_sto_dac_r_mix, ARRAY_SIZE(rt5660_sto_dac_r_mix)),
663 
664 	/* DACs */
665 	SND_SOC_DAPM_DAC("DAC L1", NULL, RT5660_PWR_DIG1,
666 			RT5660_PWR_DAC_L1_BIT, 0),
667 	SND_SOC_DAPM_DAC("DAC R1", NULL, RT5660_PWR_DIG1,
668 			RT5660_PWR_DAC_R1_BIT, 0),
669 
670 	/* OUT Mixer */
671 	SND_SOC_DAPM_MIXER("SPK MIX", RT5660_PWR_MIXER, RT5660_PWR_SM_BIT,
672 		0, rt5660_spk_mix, ARRAY_SIZE(rt5660_spk_mix)),
673 	SND_SOC_DAPM_MIXER("OUT MIXL", RT5660_PWR_MIXER, RT5660_PWR_OM_L_BIT,
674 		0, rt5660_out_l_mix, ARRAY_SIZE(rt5660_out_l_mix)),
675 	SND_SOC_DAPM_MIXER("OUT MIXR", RT5660_PWR_MIXER, RT5660_PWR_OM_R_BIT,
676 		0, rt5660_out_r_mix, ARRAY_SIZE(rt5660_out_r_mix)),
677 
678 	/* Output Volume */
679 	SND_SOC_DAPM_SWITCH("SPKVOL", RT5660_PWR_VOL,
680 		RT5660_PWR_SV_BIT, 0, &spk_vol_control),
681 	SND_SOC_DAPM_PGA("DAC 1", SND_SOC_NOPM,
682 		0, 0, NULL, 0),
683 	SND_SOC_DAPM_PGA("LOUTVOL", SND_SOC_NOPM,
684 		0, 0, NULL, 0),
685 	SND_SOC_DAPM_SWITCH("LOUTVOL L", SND_SOC_NOPM,
686 		RT5660_PWR_LV_L_BIT, 0, &lout_l_vol_control),
687 	SND_SOC_DAPM_SWITCH("LOUTVOL R", SND_SOC_NOPM,
688 		RT5660_PWR_LV_R_BIT, 0, &lout_r_vol_control),
689 
690 	/* HPO/LOUT/Mono Mixer */
691 	SND_SOC_DAPM_MIXER("SPO MIX", SND_SOC_NOPM, 0,
692 		0, rt5660_spo_mix, ARRAY_SIZE(rt5660_spo_mix)),
693 	SND_SOC_DAPM_MIXER("LOUT MIX", SND_SOC_NOPM, 0, 0,
694 		rt5660_lout_mix, ARRAY_SIZE(rt5660_lout_mix)),
695 	SND_SOC_DAPM_SUPPLY("VREF HP", RT5660_GEN_CTRL1,
696 		RT5660_PWR_VREF_HP_SFT, 0, NULL, 0),
697 	SND_SOC_DAPM_PGA_S("LOUT amp", 1, RT5660_PWR_ANLG1,
698 		RT5660_PWR_HA_BIT, 0, rt5660_lout_event,
699 		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
700 	SND_SOC_DAPM_PGA_S("SPK amp", 1, RT5660_PWR_DIG1,
701 		RT5660_PWR_CLS_D_BIT, 0, NULL, 0),
702 
703 	/* Output Lines */
704 	SND_SOC_DAPM_OUTPUT("LOUTL"),
705 	SND_SOC_DAPM_OUTPUT("LOUTR"),
706 	SND_SOC_DAPM_OUTPUT("SPO"),
707 };
708 
709 static const struct snd_soc_dapm_route rt5660_dapm_routes[] = {
710 	{ "MICBIAS1", NULL, "LDO2" },
711 	{ "MICBIAS2", NULL, "LDO2" },
712 
713 	{ "BST1", NULL, "IN1P" },
714 	{ "BST1", NULL, "IN1N" },
715 	{ "BST2", NULL, "IN2P" },
716 	{ "BST3", NULL, "IN3P" },
717 	{ "BST3", NULL, "IN3N" },
718 
719 	{ "RECMIXL", "BST3 Switch", "BST3" },
720 	{ "RECMIXL", "BST2 Switch", "BST2" },
721 	{ "RECMIXL", "BST1 Switch", "BST1" },
722 	{ "RECMIXL", "OUT MIXL Switch", "OUT MIXL" },
723 
724 	{ "RECMIXR", "BST3 Switch", "BST3" },
725 	{ "RECMIXR", "BST2 Switch", "BST2" },
726 	{ "RECMIXR", "BST1 Switch", "BST1" },
727 	{ "RECMIXR", "OUT MIXR Switch", "OUT MIXR" },
728 
729 	{ "ADC L", NULL, "RECMIXL" },
730 	{ "ADC L", NULL, "ADC L power" },
731 	{ "ADC L", NULL, "ADC clock" },
732 	{ "ADC R", NULL, "RECMIXR" },
733 	{ "ADC R", NULL, "ADC R power" },
734 	{ "ADC R", NULL, "ADC clock" },
735 
736 	{"DMIC L1", NULL, "DMIC CLK"},
737 	{"DMIC L1", NULL, "DMIC Power"},
738 	{"DMIC R1", NULL, "DMIC CLK"},
739 	{"DMIC R1", NULL, "DMIC Power"},
740 
741 	{ "Sto1 ADC MIXL", "ADC1 Switch", "ADC L" },
742 	{ "Sto1 ADC MIXL", "ADC2 Switch", "DMIC L1" },
743 	{ "Sto1 ADC MIXR", "ADC1 Switch", "ADC R" },
744 	{ "Sto1 ADC MIXR", "ADC2 Switch", "DMIC R1" },
745 
746 	{ "Stereo1 ADC MIXL", NULL, "Sto1 ADC MIXL" },
747 	{ "Stereo1 ADC MIXL", NULL, "adc stereo1 filter" },
748 	{ "adc stereo1 filter", NULL, "PLL1", rt5660_is_sys_clk_from_pll },
749 
750 	{ "Stereo1 ADC MIXR", NULL, "Sto1 ADC MIXR" },
751 	{ "Stereo1 ADC MIXR", NULL, "adc stereo1 filter" },
752 	{ "adc stereo1 filter", NULL, "PLL1", rt5660_is_sys_clk_from_pll },
753 
754 	{ "IF1 ADC", NULL, "Stereo1 ADC MIXL" },
755 	{ "IF1 ADC", NULL, "Stereo1 ADC MIXR" },
756 	{ "IF1 ADC", NULL, "I2S1" },
757 
758 	{ "IF1 ADC Swap Mux", "L/R", "IF1 ADC" },
759 	{ "IF1 ADC Swap Mux", "R/L", "IF1 ADC" },
760 	{ "IF1 ADC Swap Mux", "L/L", "IF1 ADC" },
761 	{ "IF1 ADC Swap Mux", "R/R", "IF1 ADC" },
762 	{ "AIF1TX", NULL, "IF1 ADC Swap Mux" },
763 
764 	{ "IF1 DAC", NULL, "AIF1RX" },
765 	{ "IF1 DAC", NULL, "I2S1" },
766 
767 	{ "IF1 DAC Swap Mux", "L/R", "IF1 DAC" },
768 	{ "IF1 DAC Swap Mux", "R/L", "IF1 DAC" },
769 	{ "IF1 DAC Swap Mux", "L/L", "IF1 DAC" },
770 	{ "IF1 DAC Swap Mux", "R/R", "IF1 DAC" },
771 
772 	{ "IF1 DAC L", NULL, "IF1 DAC Swap Mux" },
773 	{ "IF1 DAC R", NULL, "IF1 DAC Swap Mux" },
774 
775 	{ "DAC1 MIXL", "Stereo ADC Switch", "Stereo1 ADC MIXL" },
776 	{ "DAC1 MIXL", "DAC1 Switch", "IF1 DAC L" },
777 	{ "DAC1 MIXR", "Stereo ADC Switch", "Stereo1 ADC MIXR" },
778 	{ "DAC1 MIXR", "DAC1 Switch", "IF1 DAC R" },
779 
780 	{ "Stereo DAC MIXL", "DAC L1 Switch", "DAC1 MIXL" },
781 	{ "Stereo DAC MIXL", "DAC R1 Switch", "DAC1 MIXR" },
782 	{ "Stereo DAC MIXL", NULL, "dac stereo1 filter" },
783 	{ "dac stereo1 filter", NULL, "PLL1", rt5660_is_sys_clk_from_pll },
784 	{ "Stereo DAC MIXR", "DAC R1 Switch", "DAC1 MIXR" },
785 	{ "Stereo DAC MIXR", "DAC L1 Switch", "DAC1 MIXL" },
786 	{ "Stereo DAC MIXR", NULL, "dac stereo1 filter" },
787 	{ "dac stereo1 filter", NULL, "PLL1", rt5660_is_sys_clk_from_pll },
788 
789 	{ "DAC L1", NULL, "Stereo DAC MIXL" },
790 	{ "DAC R1", NULL, "Stereo DAC MIXR" },
791 
792 	{ "SPK MIX", "BST3 Switch", "BST3" },
793 	{ "SPK MIX", "BST1 Switch", "BST1" },
794 	{ "SPK MIX", "DACL Switch", "DAC L1" },
795 	{ "SPK MIX", "DACR Switch", "DAC R1" },
796 	{ "SPK MIX", "OUTMIXL Switch", "OUT MIXL" },
797 
798 	{ "OUT MIXL", "BST3 Switch", "BST3" },
799 	{ "OUT MIXL", "BST2 Switch", "BST2" },
800 	{ "OUT MIXL", "BST1 Switch", "BST1" },
801 	{ "OUT MIXL", "RECMIXL Switch", "RECMIXL" },
802 	{ "OUT MIXL", "DACR Switch", "DAC R1" },
803 	{ "OUT MIXL", "DACL Switch", "DAC L1" },
804 
805 	{ "OUT MIXR", "BST2 Switch", "BST2" },
806 	{ "OUT MIXR", "BST1 Switch", "BST1" },
807 	{ "OUT MIXR", "RECMIXR Switch", "RECMIXR" },
808 	{ "OUT MIXR", "DACR Switch", "DAC R1" },
809 	{ "OUT MIXR", "DACL Switch", "DAC L1" },
810 
811 	{ "SPO MIX", "DACR Switch", "DAC R1" },
812 	{ "SPO MIX", "DACL Switch", "DAC L1" },
813 	{ "SPO MIX", "SPKVOL Switch", "SPKVOL" },
814 	{ "SPO MIX", "BST1 Switch", "BST1" },
815 
816 	{ "SPKVOL", "Switch", "SPK MIX" },
817 	{ "LOUTVOL L", "Switch", "OUT MIXL" },
818 	{ "LOUTVOL R", "Switch", "OUT MIXR" },
819 
820 	{ "LOUTVOL", NULL, "LOUTVOL L" },
821 	{ "LOUTVOL", NULL, "LOUTVOL R" },
822 
823 	{ "DAC 1", NULL, "DAC L1" },
824 	{ "DAC 1", NULL, "DAC R1" },
825 
826 	{ "LOUT MIX", "DAC Switch", "DAC 1" },
827 	{ "LOUT MIX", "OUTMIX Switch", "LOUTVOL" },
828 
829 	{ "LOUT amp", NULL, "LOUT MIX" },
830 	{ "LOUT amp", NULL, "VREF HP" },
831 	{ "LOUTL", NULL, "LOUT amp" },
832 	{ "LOUTR", NULL, "LOUT amp" },
833 
834 	{ "SPK amp", NULL, "SPO MIX" },
835 	{ "SPO", NULL, "SPK amp" },
836 };
837 
838 static int rt5660_hw_params(struct snd_pcm_substream *substream,
839 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
840 {
841 	struct snd_soc_component *component = dai->component;
842 	struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
843 	unsigned int val_len = 0, val_clk, mask_clk;
844 	int pre_div, bclk_ms, frame_size;
845 
846 	rt5660->lrck[dai->id] = params_rate(params);
847 	pre_div = rl6231_get_clk_info(rt5660->sysclk, rt5660->lrck[dai->id]);
848 	if (pre_div < 0) {
849 		dev_err(component->dev, "Unsupported clock setting %d for DAI %d\n",
850 			rt5660->lrck[dai->id], dai->id);
851 		return -EINVAL;
852 	}
853 
854 	frame_size = snd_soc_params_to_frame_size(params);
855 	if (frame_size < 0) {
856 		dev_err(component->dev, "Unsupported frame size: %d\n", frame_size);
857 		return frame_size;
858 	}
859 
860 	if (frame_size > 32)
861 		bclk_ms = 1;
862 	else
863 		bclk_ms = 0;
864 
865 	rt5660->bclk[dai->id] = rt5660->lrck[dai->id] * (32 << bclk_ms);
866 
867 	dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
868 		rt5660->bclk[dai->id], rt5660->lrck[dai->id]);
869 	dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
870 				bclk_ms, pre_div, dai->id);
871 
872 	switch (params_width(params)) {
873 	case 16:
874 		break;
875 	case 20:
876 		val_len |= RT5660_I2S_DL_20;
877 		break;
878 	case 24:
879 		val_len |= RT5660_I2S_DL_24;
880 		break;
881 	case 8:
882 		val_len |= RT5660_I2S_DL_8;
883 		break;
884 	default:
885 		return -EINVAL;
886 	}
887 
888 	switch (dai->id) {
889 	case RT5660_AIF1:
890 		mask_clk = RT5660_I2S_BCLK_MS1_MASK | RT5660_I2S_PD1_MASK;
891 		val_clk = bclk_ms << RT5660_I2S_BCLK_MS1_SFT |
892 			pre_div << RT5660_I2S_PD1_SFT;
893 		snd_soc_component_update_bits(component, RT5660_I2S1_SDP, RT5660_I2S_DL_MASK,
894 			val_len);
895 		snd_soc_component_update_bits(component, RT5660_ADDA_CLK1, mask_clk, val_clk);
896 		break;
897 
898 	default:
899 		dev_err(component->dev, "Invalid dai->id: %d\n", dai->id);
900 		return -EINVAL;
901 	}
902 
903 	return 0;
904 }
905 
906 static int rt5660_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
907 {
908 	struct snd_soc_component *component = dai->component;
909 	struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
910 	unsigned int reg_val = 0;
911 
912 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
913 	case SND_SOC_DAIFMT_CBM_CFM:
914 		rt5660->master[dai->id] = 1;
915 		break;
916 
917 	case SND_SOC_DAIFMT_CBS_CFS:
918 		reg_val |= RT5660_I2S_MS_S;
919 		rt5660->master[dai->id] = 0;
920 		break;
921 
922 	default:
923 		return -EINVAL;
924 	}
925 
926 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
927 	case SND_SOC_DAIFMT_NB_NF:
928 		break;
929 
930 	case SND_SOC_DAIFMT_IB_NF:
931 		reg_val |= RT5660_I2S_BP_INV;
932 		break;
933 
934 	default:
935 		return -EINVAL;
936 	}
937 
938 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
939 	case SND_SOC_DAIFMT_I2S:
940 		break;
941 
942 	case SND_SOC_DAIFMT_LEFT_J:
943 		reg_val |= RT5660_I2S_DF_LEFT;
944 		break;
945 
946 	case SND_SOC_DAIFMT_DSP_A:
947 		reg_val |= RT5660_I2S_DF_PCM_A;
948 		break;
949 
950 	case SND_SOC_DAIFMT_DSP_B:
951 		reg_val  |= RT5660_I2S_DF_PCM_B;
952 		break;
953 
954 	default:
955 		return -EINVAL;
956 	}
957 
958 	switch (dai->id) {
959 	case RT5660_AIF1:
960 		snd_soc_component_update_bits(component, RT5660_I2S1_SDP,
961 			RT5660_I2S_MS_MASK | RT5660_I2S_BP_MASK |
962 			RT5660_I2S_DF_MASK, reg_val);
963 		break;
964 
965 	default:
966 		dev_err(component->dev, "Invalid dai->id: %d\n", dai->id);
967 		return -EINVAL;
968 	}
969 
970 	return 0;
971 }
972 
973 static int rt5660_set_dai_sysclk(struct snd_soc_dai *dai,
974 		int clk_id, unsigned int freq, int dir)
975 {
976 	struct snd_soc_component *component = dai->component;
977 	struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
978 	unsigned int reg_val = 0;
979 
980 	if (freq == rt5660->sysclk && clk_id == rt5660->sysclk_src)
981 		return 0;
982 
983 	switch (clk_id) {
984 	case RT5660_SCLK_S_MCLK:
985 		reg_val |= RT5660_SCLK_SRC_MCLK;
986 		break;
987 
988 	case RT5660_SCLK_S_PLL1:
989 		reg_val |= RT5660_SCLK_SRC_PLL1;
990 		break;
991 
992 	case RT5660_SCLK_S_RCCLK:
993 		reg_val |= RT5660_SCLK_SRC_RCCLK;
994 		break;
995 
996 	default:
997 		dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
998 		return -EINVAL;
999 	}
1000 
1001 	snd_soc_component_update_bits(component, RT5660_GLB_CLK, RT5660_SCLK_SRC_MASK,
1002 		reg_val);
1003 
1004 	rt5660->sysclk = freq;
1005 	rt5660->sysclk_src = clk_id;
1006 
1007 	dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
1008 
1009 	return 0;
1010 }
1011 
1012 static int rt5660_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
1013 			unsigned int freq_in, unsigned int freq_out)
1014 {
1015 	struct snd_soc_component *component = dai->component;
1016 	struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
1017 	struct rl6231_pll_code pll_code;
1018 	int ret;
1019 
1020 	if (source == rt5660->pll_src && freq_in == rt5660->pll_in &&
1021 		freq_out == rt5660->pll_out)
1022 		return 0;
1023 
1024 	if (!freq_in || !freq_out) {
1025 		dev_dbg(component->dev, "PLL disabled\n");
1026 
1027 		rt5660->pll_in = 0;
1028 		rt5660->pll_out = 0;
1029 		snd_soc_component_update_bits(component, RT5660_GLB_CLK,
1030 			RT5660_SCLK_SRC_MASK, RT5660_SCLK_SRC_MCLK);
1031 		return 0;
1032 	}
1033 
1034 	switch (source) {
1035 	case RT5660_PLL1_S_MCLK:
1036 		snd_soc_component_update_bits(component, RT5660_GLB_CLK,
1037 			RT5660_PLL1_SRC_MASK, RT5660_PLL1_SRC_MCLK);
1038 		break;
1039 
1040 	case RT5660_PLL1_S_BCLK:
1041 		snd_soc_component_update_bits(component, RT5660_GLB_CLK,
1042 			RT5660_PLL1_SRC_MASK, RT5660_PLL1_SRC_BCLK1);
1043 		break;
1044 
1045 	default:
1046 		dev_err(component->dev, "Unknown PLL source %d\n", source);
1047 		return -EINVAL;
1048 	}
1049 
1050 	ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
1051 	if (ret < 0) {
1052 		dev_err(component->dev, "Unsupport input clock %d\n", freq_in);
1053 		return ret;
1054 	}
1055 
1056 	dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
1057 		pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
1058 		pll_code.n_code, pll_code.k_code);
1059 
1060 	snd_soc_component_write(component, RT5660_PLL_CTRL1,
1061 		pll_code.n_code << RT5660_PLL_N_SFT | pll_code.k_code);
1062 	snd_soc_component_write(component, RT5660_PLL_CTRL2,
1063 		(pll_code.m_bp ? 0 : pll_code.m_code) << RT5660_PLL_M_SFT |
1064 		pll_code.m_bp << RT5660_PLL_M_BP_SFT);
1065 
1066 	rt5660->pll_in = freq_in;
1067 	rt5660->pll_out = freq_out;
1068 	rt5660->pll_src = source;
1069 
1070 	return 0;
1071 }
1072 
1073 static int rt5660_set_bias_level(struct snd_soc_component *component,
1074 			enum snd_soc_bias_level level)
1075 {
1076 	struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
1077 	int ret;
1078 
1079 	switch (level) {
1080 	case SND_SOC_BIAS_ON:
1081 		break;
1082 
1083 	case SND_SOC_BIAS_PREPARE:
1084 		snd_soc_component_update_bits(component, RT5660_GEN_CTRL1,
1085 			RT5660_DIG_GATE_CTRL, RT5660_DIG_GATE_CTRL);
1086 
1087 		if (IS_ERR(rt5660->mclk))
1088 			break;
1089 
1090 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_ON) {
1091 			clk_disable_unprepare(rt5660->mclk);
1092 		} else {
1093 			ret = clk_prepare_enable(rt5660->mclk);
1094 			if (ret)
1095 				return ret;
1096 		}
1097 		break;
1098 
1099 	case SND_SOC_BIAS_STANDBY:
1100 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
1101 			snd_soc_component_update_bits(component, RT5660_PWR_ANLG1,
1102 				RT5660_PWR_VREF1 | RT5660_PWR_MB |
1103 				RT5660_PWR_BG | RT5660_PWR_VREF2,
1104 				RT5660_PWR_VREF1 | RT5660_PWR_MB |
1105 				RT5660_PWR_BG | RT5660_PWR_VREF2);
1106 			usleep_range(10000, 15000);
1107 			snd_soc_component_update_bits(component, RT5660_PWR_ANLG1,
1108 				RT5660_PWR_FV1 | RT5660_PWR_FV2,
1109 				RT5660_PWR_FV1 | RT5660_PWR_FV2);
1110 		}
1111 		break;
1112 
1113 	case SND_SOC_BIAS_OFF:
1114 		snd_soc_component_update_bits(component, RT5660_GEN_CTRL1,
1115 			RT5660_DIG_GATE_CTRL, 0);
1116 		break;
1117 
1118 	default:
1119 		break;
1120 	}
1121 
1122 	return 0;
1123 }
1124 
1125 static int rt5660_probe(struct snd_soc_component *component)
1126 {
1127 	struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
1128 
1129 	rt5660->component = component;
1130 
1131 	return 0;
1132 }
1133 
1134 static void rt5660_remove(struct snd_soc_component *component)
1135 {
1136 	snd_soc_component_write(component, RT5660_RESET, 0);
1137 }
1138 
1139 #ifdef CONFIG_PM
1140 static int rt5660_suspend(struct snd_soc_component *component)
1141 {
1142 	struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
1143 
1144 	regcache_cache_only(rt5660->regmap, true);
1145 	regcache_mark_dirty(rt5660->regmap);
1146 
1147 	return 0;
1148 }
1149 
1150 static int rt5660_resume(struct snd_soc_component *component)
1151 {
1152 	struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
1153 
1154 	if (rt5660->pdata.poweroff_codec_in_suspend)
1155 		msleep(350);
1156 
1157 	regcache_cache_only(rt5660->regmap, false);
1158 	regcache_sync(rt5660->regmap);
1159 
1160 	return 0;
1161 }
1162 #else
1163 #define rt5660_suspend NULL
1164 #define rt5660_resume NULL
1165 #endif
1166 
1167 #define RT5660_STEREO_RATES SNDRV_PCM_RATE_8000_192000
1168 #define RT5660_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1169 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1170 
1171 static const struct snd_soc_dai_ops rt5660_aif_dai_ops = {
1172 	.hw_params = rt5660_hw_params,
1173 	.set_fmt = rt5660_set_dai_fmt,
1174 	.set_sysclk = rt5660_set_dai_sysclk,
1175 	.set_pll = rt5660_set_dai_pll,
1176 };
1177 
1178 static struct snd_soc_dai_driver rt5660_dai[] = {
1179 	{
1180 		.name = "rt5660-aif1",
1181 		.id = RT5660_AIF1,
1182 		.playback = {
1183 			.stream_name = "AIF1 Playback",
1184 			.channels_min = 1,
1185 			.channels_max = 2,
1186 			.rates = RT5660_STEREO_RATES,
1187 			.formats = RT5660_FORMATS,
1188 		},
1189 		.capture = {
1190 			.stream_name = "AIF1 Capture",
1191 			.channels_min = 1,
1192 			.channels_max = 2,
1193 			.rates = RT5660_STEREO_RATES,
1194 			.formats = RT5660_FORMATS,
1195 		},
1196 		.ops = &rt5660_aif_dai_ops,
1197 	},
1198 };
1199 
1200 static const struct snd_soc_component_driver soc_component_dev_rt5660 = {
1201 	.probe			= rt5660_probe,
1202 	.remove			= rt5660_remove,
1203 	.suspend		= rt5660_suspend,
1204 	.resume			= rt5660_resume,
1205 	.set_bias_level		= rt5660_set_bias_level,
1206 	.controls		= rt5660_snd_controls,
1207 	.num_controls		= ARRAY_SIZE(rt5660_snd_controls),
1208 	.dapm_widgets		= rt5660_dapm_widgets,
1209 	.num_dapm_widgets	= ARRAY_SIZE(rt5660_dapm_widgets),
1210 	.dapm_routes		= rt5660_dapm_routes,
1211 	.num_dapm_routes	= ARRAY_SIZE(rt5660_dapm_routes),
1212 	.use_pmdown_time	= 1,
1213 	.endianness		= 1,
1214 	.non_legacy_dai_naming	= 1,
1215 };
1216 
1217 static const struct regmap_config rt5660_regmap = {
1218 	.reg_bits = 8,
1219 	.val_bits = 16,
1220 	.use_single_read = true,
1221 	.use_single_write = true,
1222 
1223 	.max_register = RT5660_VENDOR_ID2 + 1 + (ARRAY_SIZE(rt5660_ranges) *
1224 					       RT5660_PR_SPACING),
1225 	.volatile_reg = rt5660_volatile_register,
1226 	.readable_reg = rt5660_readable_register,
1227 
1228 	.cache_type = REGCACHE_RBTREE,
1229 	.reg_defaults = rt5660_reg,
1230 	.num_reg_defaults = ARRAY_SIZE(rt5660_reg),
1231 	.ranges = rt5660_ranges,
1232 	.num_ranges = ARRAY_SIZE(rt5660_ranges),
1233 };
1234 
1235 static const struct i2c_device_id rt5660_i2c_id[] = {
1236 	{ "rt5660", 0 },
1237 	{ }
1238 };
1239 MODULE_DEVICE_TABLE(i2c, rt5660_i2c_id);
1240 
1241 static const struct of_device_id rt5660_of_match[] = {
1242 	{ .compatible = "realtek,rt5660", },
1243 	{},
1244 };
1245 MODULE_DEVICE_TABLE(of, rt5660_of_match);
1246 
1247 static const struct acpi_device_id rt5660_acpi_match[] = {
1248 	{ "10EC5660", 0 },
1249 	{ "10EC3277", 0 },
1250 	{ },
1251 };
1252 MODULE_DEVICE_TABLE(acpi, rt5660_acpi_match);
1253 
1254 static int rt5660_parse_dt(struct rt5660_priv *rt5660, struct device *dev)
1255 {
1256 	rt5660->pdata.in1_diff = device_property_read_bool(dev,
1257 					"realtek,in1-differential");
1258 	rt5660->pdata.in3_diff = device_property_read_bool(dev,
1259 					"realtek,in3-differential");
1260 	rt5660->pdata.poweroff_codec_in_suspend = device_property_read_bool(dev,
1261 					"realtek,poweroff-in-suspend");
1262 	device_property_read_u32(dev, "realtek,dmic1-data-pin",
1263 		&rt5660->pdata.dmic1_data_pin);
1264 
1265 	return 0;
1266 }
1267 
1268 static int rt5660_i2c_probe(struct i2c_client *i2c,
1269 		    const struct i2c_device_id *id)
1270 {
1271 	struct rt5660_platform_data *pdata = dev_get_platdata(&i2c->dev);
1272 	struct rt5660_priv *rt5660;
1273 	int ret;
1274 	unsigned int val;
1275 
1276 	rt5660 = devm_kzalloc(&i2c->dev, sizeof(struct rt5660_priv),
1277 		GFP_KERNEL);
1278 
1279 	if (rt5660 == NULL)
1280 		return -ENOMEM;
1281 
1282 	/* Check if MCLK provided */
1283 	rt5660->mclk = devm_clk_get(&i2c->dev, "mclk");
1284 	if (PTR_ERR(rt5660->mclk) == -EPROBE_DEFER)
1285 		return -EPROBE_DEFER;
1286 
1287 	i2c_set_clientdata(i2c, rt5660);
1288 
1289 	if (pdata)
1290 		rt5660->pdata = *pdata;
1291 	else if (i2c->dev.of_node)
1292 		rt5660_parse_dt(rt5660, &i2c->dev);
1293 
1294 	rt5660->regmap = devm_regmap_init_i2c(i2c, &rt5660_regmap);
1295 	if (IS_ERR(rt5660->regmap)) {
1296 		ret = PTR_ERR(rt5660->regmap);
1297 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1298 			ret);
1299 		return ret;
1300 	}
1301 
1302 	regmap_read(rt5660->regmap, RT5660_VENDOR_ID2, &val);
1303 	if (val != RT5660_DEVICE_ID) {
1304 		dev_err(&i2c->dev,
1305 			"Device with ID register %#x is not rt5660\n", val);
1306 		return -ENODEV;
1307 	}
1308 
1309 	regmap_write(rt5660->regmap, RT5660_RESET, 0);
1310 
1311 	ret = regmap_register_patch(rt5660->regmap, rt5660_patch,
1312 				    ARRAY_SIZE(rt5660_patch));
1313 	if (ret != 0)
1314 		dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
1315 
1316 	regmap_update_bits(rt5660->regmap, RT5660_GEN_CTRL1,
1317 		RT5660_AUTO_DIS_AMP | RT5660_MCLK_DET | RT5660_POW_CLKDET,
1318 		RT5660_AUTO_DIS_AMP | RT5660_MCLK_DET | RT5660_POW_CLKDET);
1319 
1320 	if (rt5660->pdata.dmic1_data_pin) {
1321 		regmap_update_bits(rt5660->regmap, RT5660_GPIO_CTRL1,
1322 			RT5660_GP1_PIN_MASK, RT5660_GP1_PIN_DMIC1_SCL);
1323 
1324 		if (rt5660->pdata.dmic1_data_pin == RT5660_DMIC1_DATA_GPIO2)
1325 			regmap_update_bits(rt5660->regmap, RT5660_DMIC_CTRL1,
1326 				RT5660_SEL_DMIC_DATA_MASK,
1327 				RT5660_SEL_DMIC_DATA_GPIO2);
1328 		else if (rt5660->pdata.dmic1_data_pin == RT5660_DMIC1_DATA_IN1P)
1329 			regmap_update_bits(rt5660->regmap, RT5660_DMIC_CTRL1,
1330 				RT5660_SEL_DMIC_DATA_MASK,
1331 				RT5660_SEL_DMIC_DATA_IN1P);
1332 	}
1333 
1334 	return devm_snd_soc_register_component(&i2c->dev,
1335 				      &soc_component_dev_rt5660,
1336 				      rt5660_dai, ARRAY_SIZE(rt5660_dai));
1337 }
1338 
1339 static struct i2c_driver rt5660_i2c_driver = {
1340 	.driver = {
1341 		.name = "rt5660",
1342 		.acpi_match_table = ACPI_PTR(rt5660_acpi_match),
1343 		.of_match_table = of_match_ptr(rt5660_of_match),
1344 	},
1345 	.probe = rt5660_i2c_probe,
1346 	.id_table = rt5660_i2c_id,
1347 };
1348 module_i2c_driver(rt5660_i2c_driver);
1349 
1350 MODULE_DESCRIPTION("ASoC RT5660 driver");
1351 MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
1352 MODULE_LICENSE("GPL v2");
1353