xref: /openbmc/linux/sound/soc/codecs/rt711.c (revision 0a94608f)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // rt711.c -- rt711 ALSA SoC audio driver
4 //
5 // Copyright(c) 2019 Realtek Semiconductor Corp.
6 //
7 //
8 
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/pm.h>
16 #include <linux/soundwire/sdw.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/soc-dapm.h>
24 #include <sound/initval.h>
25 #include <sound/tlv.h>
26 #include <sound/hda_verbs.h>
27 #include <sound/jack.h>
28 
29 #include "rt711.h"
30 
31 static int rt711_index_write(struct regmap *regmap,
32 		unsigned int nid, unsigned int reg, unsigned int value)
33 {
34 	int ret;
35 	unsigned int addr = ((RT711_PRIV_INDEX_W_H | nid) << 8) | reg;
36 
37 	ret = regmap_write(regmap, addr, value);
38 	if (ret < 0)
39 		pr_err("Failed to set private value: %06x <= %04x ret=%d\n",
40 			addr, value, ret);
41 
42 	return ret;
43 }
44 
45 static int rt711_index_read(struct regmap *regmap,
46 		unsigned int nid, unsigned int reg, unsigned int *value)
47 {
48 	int ret;
49 	unsigned int addr = ((RT711_PRIV_INDEX_W_H | nid) << 8) | reg;
50 
51 	*value = 0;
52 	ret = regmap_read(regmap, addr, value);
53 	if (ret < 0)
54 		pr_err("Failed to get private value: %06x => %04x ret=%d\n",
55 			addr, *value, ret);
56 
57 	return ret;
58 }
59 
60 static int rt711_index_update_bits(struct regmap *regmap, unsigned int nid,
61 			unsigned int reg, unsigned int mask, unsigned int val)
62 {
63 	unsigned int tmp, orig;
64 	int ret;
65 
66 	ret = rt711_index_read(regmap, nid, reg, &orig);
67 	if (ret < 0)
68 		return ret;
69 
70 	tmp = orig & ~mask;
71 	tmp |= val & mask;
72 
73 	return rt711_index_write(regmap, nid, reg, tmp);
74 }
75 
76 static void rt711_reset(struct regmap *regmap)
77 {
78 	regmap_write(regmap, RT711_FUNC_RESET, 0);
79 	rt711_index_update_bits(regmap, RT711_VENDOR_REG,
80 		RT711_PARA_VERB_CTL, RT711_HIDDEN_REG_SW_RESET,
81 		RT711_HIDDEN_REG_SW_RESET);
82 }
83 
84 static int rt711_calibration(struct rt711_priv *rt711)
85 {
86 	unsigned int val, loop = 0;
87 	struct device *dev;
88 	struct regmap *regmap = rt711->regmap;
89 	int ret = 0;
90 
91 	mutex_lock(&rt711->calibrate_mutex);
92 	regmap_write(rt711->regmap,
93 		RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
94 
95 	dev = regmap_get_device(regmap);
96 
97 	/* Calibration manual mode */
98 	rt711_index_update_bits(regmap, RT711_VENDOR_REG, RT711_FSM_CTL,
99 		0xf, 0x0);
100 
101 	/* trigger */
102 	rt711_index_update_bits(regmap, RT711_VENDOR_CALI,
103 		RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_CALI_TRIGGER,
104 		RT711_DAC_DC_CALI_TRIGGER);
105 
106 	/* wait for calibration process */
107 	rt711_index_read(regmap, RT711_VENDOR_CALI,
108 		RT711_DAC_DC_CALI_CTL1, &val);
109 
110 	while (val & RT711_DAC_DC_CALI_TRIGGER) {
111 		if (loop >= 500) {
112 			pr_err("%s, calibration time-out!\n",
113 							__func__);
114 			ret = -ETIMEDOUT;
115 			break;
116 		}
117 		loop++;
118 
119 		usleep_range(10000, 11000);
120 		rt711_index_read(regmap, RT711_VENDOR_CALI,
121 			RT711_DAC_DC_CALI_CTL1, &val);
122 	}
123 
124 	/* depop mode */
125 	rt711_index_update_bits(regmap, RT711_VENDOR_REG,
126 		RT711_FSM_CTL, 0xf, RT711_DEPOP_CTL);
127 
128 	regmap_write(rt711->regmap,
129 		RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
130 	mutex_unlock(&rt711->calibrate_mutex);
131 
132 	dev_dbg(dev, "%s calibration complete, ret=%d\n", __func__, ret);
133 	return ret;
134 }
135 
136 static unsigned int rt711_button_detect(struct rt711_priv *rt711)
137 {
138 	unsigned int btn_type = 0, val80, val81;
139 	int ret;
140 
141 	ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
142 				RT711_IRQ_FLAG_TABLE1, &val80);
143 	if (ret < 0)
144 		goto read_error;
145 	ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
146 					RT711_IRQ_FLAG_TABLE2, &val81);
147 	if (ret < 0)
148 		goto read_error;
149 
150 	val80 &= 0x0381;
151 	val81 &= 0xff00;
152 
153 	switch (val80) {
154 	case 0x0200:
155 	case 0x0100:
156 	case 0x0080:
157 		btn_type |= SND_JACK_BTN_0;
158 		break;
159 	case 0x0001:
160 		btn_type |= SND_JACK_BTN_3;
161 		break;
162 	}
163 	switch (val81) {
164 	case 0x8000:
165 	case 0x4000:
166 	case 0x2000:
167 		btn_type |= SND_JACK_BTN_1;
168 		break;
169 	case 0x1000:
170 	case 0x0800:
171 	case 0x0400:
172 		btn_type |= SND_JACK_BTN_2;
173 		break;
174 	case 0x0200:
175 	case 0x0100:
176 		btn_type |= SND_JACK_BTN_3;
177 		break;
178 	}
179 read_error:
180 	return btn_type;
181 }
182 
183 static int rt711_headset_detect(struct rt711_priv *rt711)
184 {
185 	unsigned int buf, loop = 0;
186 	int ret;
187 	unsigned int jack_status = 0, reg;
188 
189 	ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
190 				RT711_COMBO_JACK_AUTO_CTL2, &buf);
191 	if (ret < 0)
192 		goto io_error;
193 
194 	while (loop < 500 &&
195 		(buf & RT711_COMBOJACK_AUTO_DET_STATUS) == 0) {
196 		loop++;
197 
198 		usleep_range(9000, 10000);
199 		ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
200 					RT711_COMBO_JACK_AUTO_CTL2, &buf);
201 		if (ret < 0)
202 			goto io_error;
203 
204 		reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT;
205 		ret = regmap_read(rt711->regmap, reg, &jack_status);
206 		if (ret < 0)
207 			goto io_error;
208 		if ((jack_status & (1 << 31)) == 0)
209 			goto remove_error;
210 	}
211 
212 	if (loop >= 500)
213 		goto to_error;
214 
215 	if (buf & RT711_COMBOJACK_AUTO_DET_TRS)
216 		rt711->jack_type = SND_JACK_HEADPHONE;
217 	else if ((buf & RT711_COMBOJACK_AUTO_DET_CTIA) ||
218 		(buf & RT711_COMBOJACK_AUTO_DET_OMTP))
219 		rt711->jack_type = SND_JACK_HEADSET;
220 
221 	return 0;
222 
223 to_error:
224 	ret = -ETIMEDOUT;
225 	pr_err_ratelimited("Time-out error in %s\n", __func__);
226 	return ret;
227 io_error:
228 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
229 	return ret;
230 remove_error:
231 	pr_err_ratelimited("Jack removal in %s\n", __func__);
232 	return -ENODEV;
233 }
234 
235 static void rt711_jack_detect_handler(struct work_struct *work)
236 {
237 	struct rt711_priv *rt711 =
238 		container_of(work, struct rt711_priv, jack_detect_work.work);
239 	int btn_type = 0, ret;
240 	unsigned int jack_status = 0, reg;
241 
242 	if (!rt711->hs_jack)
243 		return;
244 
245 	if (!rt711->component->card->instantiated)
246 		return;
247 
248 	if (pm_runtime_status_suspended(rt711->slave->dev.parent)) {
249 		dev_dbg(&rt711->slave->dev,
250 			"%s: parent device is pm_runtime_status_suspended, skipping jack detection\n",
251 			__func__);
252 		return;
253 	}
254 
255 	reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT;
256 	ret = regmap_read(rt711->regmap, reg, &jack_status);
257 	if (ret < 0)
258 		goto io_error;
259 
260 	/* pin attached */
261 	if (jack_status & (1 << 31)) {
262 		/* jack in */
263 		if (rt711->jack_type == 0) {
264 			ret = rt711_headset_detect(rt711);
265 			if (ret < 0)
266 				return;
267 			if (rt711->jack_type == SND_JACK_HEADSET)
268 				btn_type = rt711_button_detect(rt711);
269 		} else if (rt711->jack_type == SND_JACK_HEADSET) {
270 			/* jack is already in, report button event */
271 			btn_type = rt711_button_detect(rt711);
272 		}
273 	} else {
274 		/* jack out */
275 		rt711->jack_type = 0;
276 	}
277 
278 	dev_dbg(&rt711->slave->dev,
279 		"in %s, jack_type=0x%x\n", __func__, rt711->jack_type);
280 	dev_dbg(&rt711->slave->dev,
281 		"in %s, btn_type=0x%x\n", __func__, btn_type);
282 
283 	snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type,
284 			SND_JACK_HEADSET |
285 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
286 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
287 
288 	if (btn_type) {
289 		/* button released */
290 		snd_soc_jack_report(rt711->hs_jack, rt711->jack_type,
291 			SND_JACK_HEADSET |
292 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
293 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
294 
295 		mod_delayed_work(system_power_efficient_wq,
296 			&rt711->jack_btn_check_work, msecs_to_jiffies(200));
297 	}
298 
299 	return;
300 
301 io_error:
302 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
303 }
304 
305 static void rt711_btn_check_handler(struct work_struct *work)
306 {
307 	struct rt711_priv *rt711 = container_of(work, struct rt711_priv,
308 		jack_btn_check_work.work);
309 	int btn_type = 0, ret;
310 	unsigned int jack_status = 0, reg;
311 
312 	reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT;
313 	ret = regmap_read(rt711->regmap, reg, &jack_status);
314 	if (ret < 0)
315 		goto io_error;
316 
317 	/* pin attached */
318 	if (jack_status & (1 << 31)) {
319 		if (rt711->jack_type == SND_JACK_HEADSET) {
320 			/* jack is already in, report button event */
321 			btn_type = rt711_button_detect(rt711);
322 		}
323 	} else {
324 		rt711->jack_type = 0;
325 	}
326 
327 	/* cbj comparator */
328 	ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
329 		RT711_COMBO_JACK_AUTO_CTL2, &reg);
330 	if (ret < 0)
331 		goto io_error;
332 
333 	if ((reg & 0xf0) == 0xf0)
334 		btn_type = 0;
335 
336 	dev_dbg(&rt711->slave->dev,
337 		"%s, btn_type=0x%x\n",	__func__, btn_type);
338 	snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type,
339 			SND_JACK_HEADSET |
340 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
341 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
342 
343 	if (btn_type) {
344 		/* button released */
345 		snd_soc_jack_report(rt711->hs_jack, rt711->jack_type,
346 			SND_JACK_HEADSET |
347 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
348 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
349 
350 		mod_delayed_work(system_power_efficient_wq,
351 			&rt711->jack_btn_check_work, msecs_to_jiffies(200));
352 	}
353 
354 	return;
355 
356 io_error:
357 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
358 }
359 
360 static void rt711_jack_init(struct rt711_priv *rt711)
361 {
362 	struct snd_soc_dapm_context *dapm =
363 		snd_soc_component_get_dapm(rt711->component);
364 
365 	mutex_lock(&rt711->calibrate_mutex);
366 	/* power on */
367 	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
368 		regmap_write(rt711->regmap,
369 			RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
370 
371 	if (rt711->hs_jack) {
372 		/* unsolicited response & IRQ control */
373 		regmap_write(rt711->regmap,
374 			RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x82);
375 		regmap_write(rt711->regmap,
376 			RT711_SET_HP_UNSOLICITED_ENABLE, 0x81);
377 		regmap_write(rt711->regmap,
378 			RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x83);
379 		rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
380 			0x10, 0x2420);
381 		rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
382 			0x19, 0x2e11);
383 
384 		switch (rt711->jd_src) {
385 		case RT711_JD1:
386 			/* default settings was already for JD1 */
387 			break;
388 		case RT711_JD2:
389 			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
390 				RT711_JD_CTL2, RT711_JD2_2PORT_200K_DECODE_HP |
391 				RT711_HP_JD_SEL_JD2,
392 				RT711_JD2_2PORT_200K_DECODE_HP |
393 				RT711_HP_JD_SEL_JD2);
394 			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
395 				RT711_CC_DET1,
396 				RT711_HP_JD_FINAL_RESULT_CTL_JD12,
397 				RT711_HP_JD_FINAL_RESULT_CTL_JD12);
398 			break;
399 		case RT711_JD2_100K:
400 			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
401 				RT711_JD_CTL2, RT711_JD2_2PORT_100K_DECODE | RT711_JD2_1PORT_TYPE_DECODE |
402 				RT711_HP_JD_SEL_JD2 | RT711_JD1_2PORT_TYPE_100K_DECODE,
403 				RT711_JD2_2PORT_100K_DECODE_HP | RT711_JD2_1PORT_JD_HP |
404 				RT711_HP_JD_SEL_JD2 | RT711_JD1_2PORT_JD_RESERVED);
405 			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
406 				RT711_CC_DET1,
407 				RT711_HP_JD_FINAL_RESULT_CTL_JD12,
408 				RT711_HP_JD_FINAL_RESULT_CTL_JD12);
409 			break;
410 		case RT711_JD2_1P8V_1PORT:
411 			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
412 				RT711_JD_CTL1, RT711_JD2_DIGITAL_JD_MODE_SEL,
413 				RT711_JD2_1_JD_MODE);
414 			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
415 				RT711_JD_CTL2, RT711_JD2_1PORT_TYPE_DECODE |
416 				RT711_HP_JD_SEL_JD2,
417 				RT711_JD2_1PORT_JD_HP |
418 				RT711_HP_JD_SEL_JD2);
419 			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
420 				RT711_JD_CTL4, RT711_JD2_PAD_PULL_UP_MASK |
421 				RT711_JD2_MODE_SEL_MASK,
422 				RT711_JD2_PAD_PULL_UP |
423 				RT711_JD2_MODE2_1P8V_1PORT);
424 			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
425 				RT711_CC_DET1,
426 				RT711_HP_JD_FINAL_RESULT_CTL_JD12,
427 				RT711_HP_JD_FINAL_RESULT_CTL_JD12);
428 			break;
429 		default:
430 			dev_warn(rt711->component->dev, "Wrong JD source\n");
431 			break;
432 		}
433 
434 		dev_dbg(&rt711->slave->dev, "in %s enable\n", __func__);
435 
436 		mod_delayed_work(system_power_efficient_wq,
437 			&rt711->jack_detect_work, msecs_to_jiffies(250));
438 	} else {
439 		regmap_write(rt711->regmap,
440 			RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x00);
441 		regmap_write(rt711->regmap,
442 			RT711_SET_HP_UNSOLICITED_ENABLE, 0x00);
443 		regmap_write(rt711->regmap,
444 			RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x00);
445 
446 		dev_dbg(&rt711->slave->dev, "in %s disable\n", __func__);
447 	}
448 
449 	/* power off */
450 	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
451 		regmap_write(rt711->regmap,
452 			RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
453 	mutex_unlock(&rt711->calibrate_mutex);
454 }
455 
456 static int rt711_set_jack_detect(struct snd_soc_component *component,
457 	struct snd_soc_jack *hs_jack, void *data)
458 {
459 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
460 
461 	rt711->hs_jack = hs_jack;
462 
463 	if (!rt711->hw_init) {
464 		dev_dbg(&rt711->slave->dev,
465 			"%s hw_init not ready yet\n", __func__);
466 		return 0;
467 	}
468 
469 	rt711_jack_init(rt711);
470 
471 	return 0;
472 }
473 
474 static void rt711_get_gain(struct rt711_priv *rt711, unsigned int addr_h,
475 				unsigned int addr_l, unsigned int val_h,
476 				unsigned int *r_val, unsigned int *l_val)
477 {
478 	/* R Channel */
479 	*r_val = (val_h << 8);
480 	regmap_read(rt711->regmap, addr_l, r_val);
481 
482 	/* L Channel */
483 	val_h |= 0x20;
484 	*l_val = (val_h << 8);
485 	regmap_read(rt711->regmap, addr_h, l_val);
486 }
487 
488 /* For Verb-Set Amplifier Gain (Verb ID = 3h) */
489 static int rt711_set_amp_gain_put(struct snd_kcontrol *kcontrol,
490 		struct snd_ctl_elem_value *ucontrol)
491 {
492 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
493 	struct snd_soc_dapm_context *dapm =
494 		snd_soc_component_get_dapm(component);
495 	struct soc_mixer_control *mc =
496 		(struct soc_mixer_control *)kcontrol->private_value;
497 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
498 	unsigned int addr_h, addr_l, val_h, val_ll, val_lr;
499 	unsigned int read_ll, read_rl;
500 	int i;
501 
502 	mutex_lock(&rt711->calibrate_mutex);
503 
504 	/* Can't use update bit function, so read the original value first */
505 	addr_h = mc->reg;
506 	addr_l = mc->rreg;
507 	if (mc->shift == RT711_DIR_OUT_SFT) /* output */
508 		val_h = 0x80;
509 	else /* input */
510 		val_h = 0x0;
511 
512 	rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll);
513 
514 	/* L Channel */
515 	if (mc->invert) {
516 		/* for mute/unmute */
517 		val_ll = (mc->max - ucontrol->value.integer.value[0])
518 					<< RT711_MUTE_SFT;
519 		/* keep gain */
520 		read_ll = read_ll & 0x7f;
521 		val_ll |= read_ll;
522 	} else {
523 		/* for gain */
524 		val_ll = ((ucontrol->value.integer.value[0]) & 0x7f);
525 		if (val_ll > mc->max)
526 			val_ll = mc->max;
527 		/* keep mute status */
528 		read_ll = read_ll & (1 << RT711_MUTE_SFT);
529 		val_ll |= read_ll;
530 	}
531 
532 	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
533 		regmap_write(rt711->regmap,
534 				RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
535 
536 	/* R Channel */
537 	if (mc->invert) {
538 		/* for mute/unmute */
539 		val_lr = (mc->max - ucontrol->value.integer.value[1])
540 					<< RT711_MUTE_SFT;
541 		/* keep gain */
542 		read_rl = read_rl & 0x7f;
543 		val_lr |= read_rl;
544 	} else {
545 		/* for gain */
546 		val_lr = ((ucontrol->value.integer.value[1]) & 0x7f);
547 		if (val_lr > mc->max)
548 			val_lr = mc->max;
549 		/* keep mute status */
550 		read_rl = read_rl & (1 << RT711_MUTE_SFT);
551 		val_lr |= read_rl;
552 	}
553 
554 	for (i = 0; i < 3; i++) { /* retry 3 times at most */
555 
556 		if (val_ll == val_lr) {
557 			/* Set both L/R channels at the same time */
558 			val_h = (1 << mc->shift) | (3 << 4);
559 			regmap_write(rt711->regmap,
560 				addr_h, (val_h << 8 | val_ll));
561 			regmap_write(rt711->regmap,
562 				addr_l, (val_h << 8 | val_ll));
563 		} else {
564 			/* Lch*/
565 			val_h = (1 << mc->shift) | (1 << 5);
566 			regmap_write(rt711->regmap,
567 				addr_h, (val_h << 8 | val_ll));
568 
569 			/* Rch */
570 			val_h = (1 << mc->shift) | (1 << 4);
571 			regmap_write(rt711->regmap,
572 				addr_l, (val_h << 8 | val_lr));
573 		}
574 		/* check result */
575 		if (mc->shift == RT711_DIR_OUT_SFT) /* output */
576 			val_h = 0x80;
577 		else /* input */
578 			val_h = 0x0;
579 
580 		rt711_get_gain(rt711, addr_h, addr_l, val_h,
581 					&read_rl, &read_ll);
582 		if (read_rl == val_lr && read_ll == val_ll)
583 			break;
584 	}
585 
586 	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
587 		regmap_write(rt711->regmap,
588 				RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
589 
590 	mutex_unlock(&rt711->calibrate_mutex);
591 	return 0;
592 }
593 
594 static int rt711_set_amp_gain_get(struct snd_kcontrol *kcontrol,
595 		struct snd_ctl_elem_value *ucontrol)
596 {
597 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
598 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
599 	struct soc_mixer_control *mc =
600 		(struct soc_mixer_control *)kcontrol->private_value;
601 	unsigned int addr_h, addr_l, val_h;
602 	unsigned int read_ll, read_rl;
603 
604 	/* switch to get command */
605 	addr_h = mc->reg;
606 	addr_l = mc->rreg;
607 	if (mc->shift == RT711_DIR_OUT_SFT) /* output */
608 		val_h = 0x80;
609 	else /* input */
610 		val_h = 0x0;
611 
612 	rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll);
613 
614 	if (mc->invert) {
615 		/* mute/unmute for switch controls */
616 		read_ll = !((read_ll & 0x80) >> RT711_MUTE_SFT);
617 		read_rl = !((read_rl & 0x80) >> RT711_MUTE_SFT);
618 	} else {
619 		/* for gain volume controls */
620 		read_ll = read_ll & 0x7f;
621 		read_rl = read_rl & 0x7f;
622 	}
623 	ucontrol->value.integer.value[0] = read_ll;
624 	ucontrol->value.integer.value[1] = read_rl;
625 
626 	return 0;
627 }
628 
629 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0);
630 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0);
631 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0);
632 
633 static const struct snd_kcontrol_new rt711_snd_controls[] = {
634 	SOC_DOUBLE_R_EXT_TLV("DAC Surr Playback Volume",
635 		RT711_SET_GAIN_DAC2_H, RT711_SET_GAIN_DAC2_L,
636 		RT711_DIR_OUT_SFT, 0x57, 0,
637 		rt711_set_amp_gain_get, rt711_set_amp_gain_put, out_vol_tlv),
638 	SOC_DOUBLE_R_EXT("ADC 08 Capture Switch",
639 		RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L,
640 		RT711_DIR_IN_SFT, 1, 1,
641 		rt711_set_amp_gain_get, rt711_set_amp_gain_put),
642 	SOC_DOUBLE_R_EXT("ADC 09 Capture Switch",
643 		RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L,
644 		RT711_DIR_IN_SFT, 1, 1,
645 		rt711_set_amp_gain_get, rt711_set_amp_gain_put),
646 	SOC_DOUBLE_R_EXT_TLV("ADC 08 Capture Volume",
647 		RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L,
648 		RT711_DIR_IN_SFT, 0x3f, 0,
649 		rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv),
650 	SOC_DOUBLE_R_EXT_TLV("ADC 09 Capture Volume",
651 		RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L,
652 		RT711_DIR_IN_SFT, 0x3f, 0,
653 		rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv),
654 	SOC_DOUBLE_R_EXT_TLV("AMIC Volume",
655 		RT711_SET_GAIN_AMIC_H, RT711_SET_GAIN_AMIC_L,
656 		RT711_DIR_IN_SFT, 3, 0,
657 		rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv),
658 	SOC_DOUBLE_R_EXT_TLV("DMIC1 Volume",
659 		RT711_SET_GAIN_DMIC1_H, RT711_SET_GAIN_DMIC1_L,
660 		RT711_DIR_IN_SFT, 3, 0,
661 		rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv),
662 	SOC_DOUBLE_R_EXT_TLV("DMIC2 Volume",
663 		RT711_SET_GAIN_DMIC2_H, RT711_SET_GAIN_DMIC2_L,
664 		RT711_DIR_IN_SFT, 3, 0,
665 		rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv),
666 };
667 
668 static int rt711_mux_get(struct snd_kcontrol *kcontrol,
669 			struct snd_ctl_elem_value *ucontrol)
670 {
671 	struct snd_soc_component *component =
672 		snd_soc_dapm_kcontrol_component(kcontrol);
673 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
674 	unsigned int reg, val = 0, nid;
675 	int ret;
676 
677 	if (strstr(ucontrol->id.name, "ADC 22 Mux"))
678 		nid = RT711_MIXER_IN1;
679 	else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
680 		nid = RT711_MIXER_IN2;
681 	else
682 		return -EINVAL;
683 
684 	/* vid = 0xf01 */
685 	reg = RT711_VERB_SET_CONNECT_SEL | nid;
686 	ret = regmap_read(rt711->regmap, reg, &val);
687 	if (ret < 0) {
688 		dev_err(component->dev, "%s: sdw read failed: %d\n",
689 			__func__, ret);
690 		return ret;
691 	}
692 
693 	ucontrol->value.enumerated.item[0] = val;
694 
695 	return 0;
696 }
697 
698 static int rt711_mux_put(struct snd_kcontrol *kcontrol,
699 			struct snd_ctl_elem_value *ucontrol)
700 {
701 	struct snd_soc_component *component =
702 		snd_soc_dapm_kcontrol_component(kcontrol);
703 	struct snd_soc_dapm_context *dapm =
704 		snd_soc_dapm_kcontrol_dapm(kcontrol);
705 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
706 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
707 	unsigned int *item = ucontrol->value.enumerated.item;
708 	unsigned int val, val2 = 0, change, reg, nid;
709 	int ret;
710 
711 	if (item[0] >= e->items)
712 		return -EINVAL;
713 
714 	if (strstr(ucontrol->id.name, "ADC 22 Mux"))
715 		nid = RT711_MIXER_IN1;
716 	else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
717 		nid = RT711_MIXER_IN2;
718 	else
719 		return -EINVAL;
720 
721 	/* Verb ID = 0x701h */
722 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
723 
724 	reg = RT711_VERB_SET_CONNECT_SEL | nid;
725 	ret = regmap_read(rt711->regmap, reg, &val2);
726 	if (ret < 0) {
727 		dev_err(component->dev, "%s: sdw read failed: %d\n",
728 			__func__, ret);
729 		return ret;
730 	}
731 
732 	if (val == val2)
733 		change = 0;
734 	else
735 		change = 1;
736 
737 	if (change) {
738 		reg = RT711_VERB_SET_CONNECT_SEL | nid;
739 		regmap_write(rt711->regmap, reg, val);
740 	}
741 
742 	snd_soc_dapm_mux_update_power(dapm, kcontrol,
743 						item[0], e, NULL);
744 
745 	return change;
746 }
747 
748 static const char * const adc_mux_text[] = {
749 	"MIC2",
750 	"LINE1",
751 	"LINE2",
752 	"DMIC",
753 };
754 
755 static SOC_ENUM_SINGLE_DECL(
756 	rt711_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text);
757 
758 static SOC_ENUM_SINGLE_DECL(
759 	rt711_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text);
760 
761 static const struct snd_kcontrol_new rt711_adc22_mux =
762 	SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum,
763 			rt711_mux_get, rt711_mux_put);
764 
765 static const struct snd_kcontrol_new rt711_adc23_mux =
766 	SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum,
767 			rt711_mux_get, rt711_mux_put);
768 
769 static int rt711_dac_surround_event(struct snd_soc_dapm_widget *w,
770 	struct snd_kcontrol *kcontrol, int event)
771 {
772 	struct snd_soc_component *component =
773 		snd_soc_dapm_to_component(w->dapm);
774 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
775 	unsigned int val_h = (1 << RT711_DIR_OUT_SFT) | (0x3 << 4);
776 	unsigned int val_l;
777 
778 	switch (event) {
779 	case SND_SOC_DAPM_POST_PMU:
780 		regmap_write(rt711->regmap,
781 			RT711_SET_STREAMID_DAC2, 0x10);
782 
783 		val_l = 0x00;
784 		regmap_write(rt711->regmap,
785 			RT711_SET_GAIN_HP_H, (val_h << 8 | val_l));
786 		break;
787 	case SND_SOC_DAPM_PRE_PMD:
788 		val_l = (1 << RT711_MUTE_SFT);
789 		regmap_write(rt711->regmap,
790 			RT711_SET_GAIN_HP_H, (val_h << 8 | val_l));
791 		usleep_range(50000, 55000);
792 
793 		regmap_write(rt711->regmap,
794 			RT711_SET_STREAMID_DAC2, 0x00);
795 		break;
796 	}
797 	return 0;
798 }
799 
800 static int rt711_adc_09_event(struct snd_soc_dapm_widget *w,
801 	struct snd_kcontrol *kcontrol, int event)
802 {
803 	struct snd_soc_component *component =
804 		snd_soc_dapm_to_component(w->dapm);
805 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
806 
807 	switch (event) {
808 	case SND_SOC_DAPM_POST_PMU:
809 		regmap_write(rt711->regmap,
810 			RT711_SET_STREAMID_ADC1, 0x10);
811 		break;
812 	case SND_SOC_DAPM_PRE_PMD:
813 		regmap_write(rt711->regmap,
814 			RT711_SET_STREAMID_ADC1, 0x00);
815 		break;
816 	}
817 	return 0;
818 }
819 
820 static int rt711_adc_08_event(struct snd_soc_dapm_widget *w,
821 	struct snd_kcontrol *kcontrol, int event)
822 {
823 	struct snd_soc_component *component =
824 		snd_soc_dapm_to_component(w->dapm);
825 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
826 
827 	switch (event) {
828 	case SND_SOC_DAPM_POST_PMU:
829 		regmap_write(rt711->regmap,
830 			RT711_SET_STREAMID_ADC2, 0x10);
831 		break;
832 	case SND_SOC_DAPM_PRE_PMD:
833 		regmap_write(rt711->regmap,
834 			RT711_SET_STREAMID_ADC2, 0x00);
835 		break;
836 	}
837 	return 0;
838 }
839 
840 static const struct snd_soc_dapm_widget rt711_dapm_widgets[] = {
841 	SND_SOC_DAPM_OUTPUT("HP"),
842 	SND_SOC_DAPM_INPUT("MIC2"),
843 	SND_SOC_DAPM_INPUT("DMIC1"),
844 	SND_SOC_DAPM_INPUT("DMIC2"),
845 	SND_SOC_DAPM_INPUT("LINE1"),
846 	SND_SOC_DAPM_INPUT("LINE2"),
847 
848 	SND_SOC_DAPM_DAC_E("DAC Surround", NULL, SND_SOC_NOPM, 0, 0,
849 		rt711_dac_surround_event,
850 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
851 	SND_SOC_DAPM_ADC_E("ADC 09", NULL, SND_SOC_NOPM, 0, 0,
852 		rt711_adc_09_event,
853 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
854 	SND_SOC_DAPM_ADC_E("ADC 08", NULL, SND_SOC_NOPM, 0, 0,
855 		rt711_adc_08_event,
856 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
857 	SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0,
858 		&rt711_adc22_mux),
859 	SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0,
860 		&rt711_adc23_mux),
861 
862 	SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0),
863 	SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0),
864 	SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0),
865 };
866 
867 static const struct snd_soc_dapm_route rt711_audio_map[] = {
868 	{"DAC Surround", NULL, "DP3RX"},
869 	{"DP2TX", NULL, "ADC 09"},
870 	{"DP4TX", NULL, "ADC 08"},
871 
872 	{"ADC 09", NULL, "ADC 22 Mux"},
873 	{"ADC 08", NULL, "ADC 23 Mux"},
874 	{"ADC 22 Mux", "DMIC", "DMIC1"},
875 	{"ADC 22 Mux", "LINE1", "LINE1"},
876 	{"ADC 22 Mux", "LINE2", "LINE2"},
877 	{"ADC 22 Mux", "MIC2", "MIC2"},
878 	{"ADC 23 Mux", "DMIC", "DMIC2"},
879 	{"ADC 23 Mux", "LINE1", "LINE1"},
880 	{"ADC 23 Mux", "LINE2", "LINE2"},
881 	{"ADC 23 Mux", "MIC2", "MIC2"},
882 
883 	{"HP", NULL, "DAC Surround"},
884 };
885 
886 static int rt711_set_bias_level(struct snd_soc_component *component,
887 				enum snd_soc_bias_level level)
888 {
889 	struct snd_soc_dapm_context *dapm =
890 		snd_soc_component_get_dapm(component);
891 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
892 
893 	switch (level) {
894 	case SND_SOC_BIAS_PREPARE:
895 		if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
896 			regmap_write(rt711->regmap,
897 				RT711_SET_AUDIO_POWER_STATE,
898 				AC_PWRST_D0);
899 		}
900 		break;
901 
902 	case SND_SOC_BIAS_STANDBY:
903 		mutex_lock(&rt711->calibrate_mutex);
904 		regmap_write(rt711->regmap,
905 			RT711_SET_AUDIO_POWER_STATE,
906 			AC_PWRST_D3);
907 		mutex_unlock(&rt711->calibrate_mutex);
908 		break;
909 
910 	default:
911 		break;
912 	}
913 
914 	return 0;
915 }
916 
917 static int rt711_parse_dt(struct rt711_priv *rt711, struct device *dev)
918 {
919 	device_property_read_u32(dev, "realtek,jd-src",
920 		&rt711->jd_src);
921 
922 	return 0;
923 }
924 
925 static int rt711_probe(struct snd_soc_component *component)
926 {
927 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
928 
929 	rt711_parse_dt(rt711, &rt711->slave->dev);
930 	rt711->component = component;
931 
932 	return 0;
933 }
934 
935 static void rt711_remove(struct snd_soc_component *component)
936 {
937 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
938 
939 	regcache_cache_only(rt711->regmap, true);
940 }
941 
942 static const struct snd_soc_component_driver soc_codec_dev_rt711 = {
943 	.probe = rt711_probe,
944 	.set_bias_level = rt711_set_bias_level,
945 	.controls = rt711_snd_controls,
946 	.num_controls = ARRAY_SIZE(rt711_snd_controls),
947 	.dapm_widgets = rt711_dapm_widgets,
948 	.num_dapm_widgets = ARRAY_SIZE(rt711_dapm_widgets),
949 	.dapm_routes = rt711_audio_map,
950 	.num_dapm_routes = ARRAY_SIZE(rt711_audio_map),
951 	.set_jack = rt711_set_jack_detect,
952 	.remove = rt711_remove,
953 };
954 
955 static int rt711_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
956 				int direction)
957 {
958 	struct sdw_stream_data *stream;
959 
960 	if (!sdw_stream)
961 		return 0;
962 
963 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
964 	if (!stream)
965 		return -ENOMEM;
966 
967 	stream->sdw_stream = sdw_stream;
968 
969 	/* Use tx_mask or rx_mask to configure stream tag and set dma_data */
970 	if (direction == SNDRV_PCM_STREAM_PLAYBACK)
971 		dai->playback_dma_data = stream;
972 	else
973 		dai->capture_dma_data = stream;
974 
975 	return 0;
976 }
977 
978 static void rt711_shutdown(struct snd_pcm_substream *substream,
979 				struct snd_soc_dai *dai)
980 {
981 	struct sdw_stream_data *stream;
982 
983 	stream = snd_soc_dai_get_dma_data(dai, substream);
984 	snd_soc_dai_set_dma_data(dai, substream, NULL);
985 	kfree(stream);
986 }
987 
988 static int rt711_pcm_hw_params(struct snd_pcm_substream *substream,
989 				struct snd_pcm_hw_params *params,
990 				struct snd_soc_dai *dai)
991 {
992 	struct snd_soc_component *component = dai->component;
993 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
994 	struct sdw_stream_config stream_config;
995 	struct sdw_port_config port_config;
996 	enum sdw_data_direction direction;
997 	struct sdw_stream_data *stream;
998 	int retval, port, num_channels;
999 	unsigned int val = 0;
1000 
1001 	dev_dbg(dai->dev, "%s %s", __func__, dai->name);
1002 	stream = snd_soc_dai_get_dma_data(dai, substream);
1003 
1004 	if (!stream)
1005 		return -EINVAL;
1006 
1007 	if (!rt711->slave)
1008 		return -EINVAL;
1009 
1010 	/* SoundWire specific configuration */
1011 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1012 		direction = SDW_DATA_DIR_RX;
1013 		port = 3;
1014 	} else {
1015 		direction = SDW_DATA_DIR_TX;
1016 		if (dai->id == RT711_AIF1)
1017 			port = 4;
1018 		else if (dai->id == RT711_AIF2)
1019 			port = 2;
1020 		else
1021 			return -EINVAL;
1022 	}
1023 
1024 	stream_config.frame_rate = params_rate(params);
1025 	stream_config.ch_count = params_channels(params);
1026 	stream_config.bps = snd_pcm_format_width(params_format(params));
1027 	stream_config.direction = direction;
1028 
1029 	num_channels = params_channels(params);
1030 	port_config.ch_mask = (1 << (num_channels)) - 1;
1031 	port_config.num = port;
1032 
1033 	retval = sdw_stream_add_slave(rt711->slave, &stream_config,
1034 					&port_config, 1, stream->sdw_stream);
1035 	if (retval) {
1036 		dev_err(dai->dev, "Unable to configure port\n");
1037 		return retval;
1038 	}
1039 
1040 	if (params_channels(params) <= 16) {
1041 		/* bit 3:0 Number of Channel */
1042 		val |= (params_channels(params) - 1);
1043 	} else {
1044 		dev_err(component->dev, "Unsupported channels %d\n",
1045 			params_channels(params));
1046 		return -EINVAL;
1047 	}
1048 
1049 	switch (params_width(params)) {
1050 	/* bit 6:4 Bits per Sample */
1051 	case 8:
1052 		break;
1053 	case 16:
1054 		val |= (0x1 << 4);
1055 		break;
1056 	case 20:
1057 		val |= (0x2 << 4);
1058 		break;
1059 	case 24:
1060 		val |= (0x3 << 4);
1061 		break;
1062 	case 32:
1063 		val |= (0x4 << 4);
1064 		break;
1065 	default:
1066 		return -EINVAL;
1067 	}
1068 
1069 	/* 48Khz */
1070 	regmap_write(rt711->regmap, RT711_DAC_FORMAT_H, val);
1071 	regmap_write(rt711->regmap, RT711_ADC1_FORMAT_H, val);
1072 	regmap_write(rt711->regmap, RT711_ADC2_FORMAT_H, val);
1073 
1074 	return retval;
1075 }
1076 
1077 static int rt711_pcm_hw_free(struct snd_pcm_substream *substream,
1078 				struct snd_soc_dai *dai)
1079 {
1080 	struct snd_soc_component *component = dai->component;
1081 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
1082 	struct sdw_stream_data *stream =
1083 		snd_soc_dai_get_dma_data(dai, substream);
1084 
1085 	if (!rt711->slave)
1086 		return -EINVAL;
1087 
1088 	sdw_stream_remove_slave(rt711->slave, stream->sdw_stream);
1089 	return 0;
1090 }
1091 
1092 #define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
1093 #define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1094 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1095 
1096 static const struct snd_soc_dai_ops rt711_ops = {
1097 	.hw_params	= rt711_pcm_hw_params,
1098 	.hw_free	= rt711_pcm_hw_free,
1099 	.set_stream	= rt711_set_sdw_stream,
1100 	.shutdown	= rt711_shutdown,
1101 };
1102 
1103 static struct snd_soc_dai_driver rt711_dai[] = {
1104 	{
1105 		.name = "rt711-aif1",
1106 		.id = RT711_AIF1,
1107 		.playback = {
1108 			.stream_name = "DP3 Playback",
1109 			.channels_min = 1,
1110 			.channels_max = 2,
1111 			.rates = RT711_STEREO_RATES,
1112 			.formats = RT711_FORMATS,
1113 		},
1114 		.capture = {
1115 			.stream_name = "DP4 Capture",
1116 			.channels_min = 1,
1117 			.channels_max = 2,
1118 			.rates = RT711_STEREO_RATES,
1119 			.formats = RT711_FORMATS,
1120 		},
1121 		.ops = &rt711_ops,
1122 	},
1123 	{
1124 		.name = "rt711-aif2",
1125 		.id = RT711_AIF2,
1126 		.capture = {
1127 			.stream_name = "DP2 Capture",
1128 			.channels_min = 1,
1129 			.channels_max = 2,
1130 			.rates = RT711_STEREO_RATES,
1131 			.formats = RT711_FORMATS,
1132 		},
1133 		.ops = &rt711_ops,
1134 	}
1135 };
1136 
1137 /* Bus clock frequency */
1138 #define RT711_CLK_FREQ_9600000HZ 9600000
1139 #define RT711_CLK_FREQ_12000000HZ 12000000
1140 #define RT711_CLK_FREQ_6000000HZ 6000000
1141 #define RT711_CLK_FREQ_4800000HZ 4800000
1142 #define RT711_CLK_FREQ_2400000HZ 2400000
1143 #define RT711_CLK_FREQ_12288000HZ 12288000
1144 
1145 int rt711_clock_config(struct device *dev)
1146 {
1147 	struct rt711_priv *rt711 = dev_get_drvdata(dev);
1148 	unsigned int clk_freq, value;
1149 
1150 	clk_freq = (rt711->params.curr_dr_freq >> 1);
1151 
1152 	switch (clk_freq) {
1153 	case RT711_CLK_FREQ_12000000HZ:
1154 		value = 0x0;
1155 		break;
1156 	case RT711_CLK_FREQ_6000000HZ:
1157 		value = 0x1;
1158 		break;
1159 	case RT711_CLK_FREQ_9600000HZ:
1160 		value = 0x2;
1161 		break;
1162 	case RT711_CLK_FREQ_4800000HZ:
1163 		value = 0x3;
1164 		break;
1165 	case RT711_CLK_FREQ_2400000HZ:
1166 		value = 0x4;
1167 		break;
1168 	case RT711_CLK_FREQ_12288000HZ:
1169 		value = 0x5;
1170 		break;
1171 	default:
1172 		return -EINVAL;
1173 	}
1174 
1175 	regmap_write(rt711->regmap, 0xe0, value);
1176 	regmap_write(rt711->regmap, 0xf0, value);
1177 
1178 	dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
1179 
1180 	return 0;
1181 }
1182 
1183 static void rt711_calibration_work(struct work_struct *work)
1184 {
1185 	struct rt711_priv *rt711 =
1186 		container_of(work, struct rt711_priv, calibration_work);
1187 
1188 	rt711_calibration(rt711);
1189 }
1190 
1191 int rt711_init(struct device *dev, struct regmap *sdw_regmap,
1192 			struct regmap *regmap, struct sdw_slave *slave)
1193 {
1194 	struct rt711_priv *rt711;
1195 	int ret;
1196 
1197 	rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL);
1198 	if (!rt711)
1199 		return -ENOMEM;
1200 
1201 	dev_set_drvdata(dev, rt711);
1202 	rt711->slave = slave;
1203 	rt711->sdw_regmap = sdw_regmap;
1204 	rt711->regmap = regmap;
1205 
1206 	mutex_init(&rt711->disable_irq_lock);
1207 
1208 	/*
1209 	 * Mark hw_init to false
1210 	 * HW init will be performed when device reports present
1211 	 */
1212 	rt711->hw_init = false;
1213 	rt711->first_hw_init = false;
1214 
1215 	/* JD source uses JD2 in default */
1216 	rt711->jd_src = RT711_JD2;
1217 
1218 	ret =  devm_snd_soc_register_component(dev,
1219 				&soc_codec_dev_rt711,
1220 				rt711_dai,
1221 				ARRAY_SIZE(rt711_dai));
1222 
1223 	dev_dbg(&slave->dev, "%s\n", __func__);
1224 
1225 	return ret;
1226 }
1227 
1228 int rt711_io_init(struct device *dev, struct sdw_slave *slave)
1229 {
1230 	struct rt711_priv *rt711 = dev_get_drvdata(dev);
1231 
1232 	rt711->disable_irq = false;
1233 
1234 	if (rt711->hw_init)
1235 		return 0;
1236 
1237 	if (rt711->first_hw_init) {
1238 		regcache_cache_only(rt711->regmap, false);
1239 		regcache_cache_bypass(rt711->regmap, true);
1240 	}
1241 
1242 	/*
1243 	 * PM runtime is only enabled when a Slave reports as Attached
1244 	 */
1245 	if (!rt711->first_hw_init) {
1246 		/* set autosuspend parameters */
1247 		pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
1248 		pm_runtime_use_autosuspend(&slave->dev);
1249 
1250 		/* update count of parent 'active' children */
1251 		pm_runtime_set_active(&slave->dev);
1252 
1253 		/* make sure the device does not suspend immediately */
1254 		pm_runtime_mark_last_busy(&slave->dev);
1255 
1256 		pm_runtime_enable(&slave->dev);
1257 	}
1258 
1259 	pm_runtime_get_noresume(&slave->dev);
1260 
1261 	rt711_reset(rt711->regmap);
1262 
1263 	/* power on */
1264 	regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
1265 
1266 	/* Set Pin Widget */
1267 	regmap_write(rt711->regmap, RT711_SET_PIN_MIC2, 0x25);
1268 	regmap_write(rt711->regmap, RT711_SET_PIN_HP, 0xc0);
1269 	regmap_write(rt711->regmap, RT711_SET_PIN_DMIC1, 0x20);
1270 	regmap_write(rt711->regmap, RT711_SET_PIN_DMIC2, 0x20);
1271 	regmap_write(rt711->regmap, RT711_SET_PIN_LINE1, 0x20);
1272 	regmap_write(rt711->regmap, RT711_SET_PIN_LINE2, 0x20);
1273 
1274 	/* Mute HP/ADC1/ADC2 */
1275 	regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0xa080);
1276 	regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0x9080);
1277 	regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x6080);
1278 	regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x5080);
1279 	regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x6080);
1280 	regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x5080);
1281 
1282 	/* Set Configuration Default */
1283 	regmap_write(rt711->regmap, 0x4f12, 0x91);
1284 	regmap_write(rt711->regmap, 0x4e12, 0xd6);
1285 	regmap_write(rt711->regmap, 0x4d12, 0x11);
1286 	regmap_write(rt711->regmap, 0x4c12, 0x20);
1287 	regmap_write(rt711->regmap, 0x4f13, 0x91);
1288 	regmap_write(rt711->regmap, 0x4e13, 0xd6);
1289 	regmap_write(rt711->regmap, 0x4d13, 0x11);
1290 	regmap_write(rt711->regmap, 0x4c13, 0x21);
1291 	regmap_write(rt711->regmap, 0x4c21, 0xf0);
1292 	regmap_write(rt711->regmap, 0x4d21, 0x11);
1293 	regmap_write(rt711->regmap, 0x4e21, 0x11);
1294 	regmap_write(rt711->regmap, 0x4f21, 0x01);
1295 
1296 	/* Data port arrangement */
1297 	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1298 		RT711_TX_RX_MUX_CTL, 0x0154);
1299 
1300 	/* Set index */
1301 	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1302 		RT711_DIGITAL_MISC_CTRL4, 0x201b);
1303 	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1304 		RT711_COMBO_JACK_AUTO_CTL1, 0x5089);
1305 	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1306 		RT711_VREFOUT_CTL, 0x5064);
1307 	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1308 		RT711_INLINE_CMD_CTL, 0xd249);
1309 
1310 	/* Finish Initial Settings, set power to D3 */
1311 	regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
1312 
1313 	if (rt711->first_hw_init)
1314 		rt711_calibration(rt711);
1315 	else {
1316 		INIT_DELAYED_WORK(&rt711->jack_detect_work,
1317 			rt711_jack_detect_handler);
1318 		INIT_DELAYED_WORK(&rt711->jack_btn_check_work,
1319 			rt711_btn_check_handler);
1320 		mutex_init(&rt711->calibrate_mutex);
1321 		INIT_WORK(&rt711->calibration_work, rt711_calibration_work);
1322 		schedule_work(&rt711->calibration_work);
1323 	}
1324 
1325 	/*
1326 	 * if set_jack callback occurred early than io_init,
1327 	 * we set up the jack detection function now
1328 	 */
1329 	if (rt711->hs_jack)
1330 		rt711_jack_init(rt711);
1331 
1332 	if (rt711->first_hw_init) {
1333 		regcache_cache_bypass(rt711->regmap, false);
1334 		regcache_mark_dirty(rt711->regmap);
1335 	} else
1336 		rt711->first_hw_init = true;
1337 
1338 	/* Mark Slave initialization complete */
1339 	rt711->hw_init = true;
1340 
1341 	pm_runtime_mark_last_busy(&slave->dev);
1342 	pm_runtime_put_autosuspend(&slave->dev);
1343 
1344 	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
1345 	return 0;
1346 }
1347 
1348 MODULE_DESCRIPTION("ASoC RT711 SDW driver");
1349 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1350 MODULE_LICENSE("GPL");
1351