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