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