xref: /openbmc/linux/sound/soc/codecs/rt711.c (revision 2a954832)
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 	snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
968 
969 	return 0;
970 }
971 
972 static void rt711_shutdown(struct snd_pcm_substream *substream,
973 				struct snd_soc_dai *dai)
974 {
975 	snd_soc_dai_set_dma_data(dai, substream, NULL);
976 }
977 
978 static int rt711_pcm_hw_params(struct snd_pcm_substream *substream,
979 				struct snd_pcm_hw_params *params,
980 				struct snd_soc_dai *dai)
981 {
982 	struct snd_soc_component *component = dai->component;
983 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
984 	struct sdw_stream_config stream_config = {0};
985 	struct sdw_port_config port_config = {0};
986 	struct sdw_stream_runtime *sdw_stream;
987 	int retval;
988 	unsigned int val = 0;
989 
990 	dev_dbg(dai->dev, "%s %s", __func__, dai->name);
991 	sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
992 
993 	if (!sdw_stream)
994 		return -EINVAL;
995 
996 	if (!rt711->slave)
997 		return -EINVAL;
998 
999 	/* SoundWire specific configuration */
1000 	snd_sdw_params_to_config(substream, params, &stream_config, &port_config);
1001 
1002 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1003 		port_config.num = 3;
1004 	} else {
1005 		if (dai->id == RT711_AIF1)
1006 			port_config.num = 4;
1007 		else if (dai->id == RT711_AIF2)
1008 			port_config.num = 2;
1009 		else
1010 			return -EINVAL;
1011 	}
1012 
1013 	retval = sdw_stream_add_slave(rt711->slave, &stream_config,
1014 					&port_config, 1, sdw_stream);
1015 	if (retval) {
1016 		dev_err(dai->dev, "Unable to configure port\n");
1017 		return retval;
1018 	}
1019 
1020 	if (params_channels(params) <= 16) {
1021 		/* bit 3:0 Number of Channel */
1022 		val |= (params_channels(params) - 1);
1023 	} else {
1024 		dev_err(component->dev, "Unsupported channels %d\n",
1025 			params_channels(params));
1026 		return -EINVAL;
1027 	}
1028 
1029 	switch (params_width(params)) {
1030 	/* bit 6:4 Bits per Sample */
1031 	case 8:
1032 		break;
1033 	case 16:
1034 		val |= (0x1 << 4);
1035 		break;
1036 	case 20:
1037 		val |= (0x2 << 4);
1038 		break;
1039 	case 24:
1040 		val |= (0x3 << 4);
1041 		break;
1042 	case 32:
1043 		val |= (0x4 << 4);
1044 		break;
1045 	default:
1046 		return -EINVAL;
1047 	}
1048 
1049 	/* 48Khz */
1050 	regmap_write(rt711->regmap, RT711_DAC_FORMAT_H, val);
1051 	regmap_write(rt711->regmap, RT711_ADC1_FORMAT_H, val);
1052 	regmap_write(rt711->regmap, RT711_ADC2_FORMAT_H, val);
1053 
1054 	return retval;
1055 }
1056 
1057 static int rt711_pcm_hw_free(struct snd_pcm_substream *substream,
1058 				struct snd_soc_dai *dai)
1059 {
1060 	struct snd_soc_component *component = dai->component;
1061 	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
1062 	struct sdw_stream_runtime *sdw_stream =
1063 		snd_soc_dai_get_dma_data(dai, substream);
1064 
1065 	if (!rt711->slave)
1066 		return -EINVAL;
1067 
1068 	sdw_stream_remove_slave(rt711->slave, sdw_stream);
1069 	return 0;
1070 }
1071 
1072 #define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
1073 #define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1074 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1075 
1076 static const struct snd_soc_dai_ops rt711_ops = {
1077 	.hw_params	= rt711_pcm_hw_params,
1078 	.hw_free	= rt711_pcm_hw_free,
1079 	.set_stream	= rt711_set_sdw_stream,
1080 	.shutdown	= rt711_shutdown,
1081 };
1082 
1083 static struct snd_soc_dai_driver rt711_dai[] = {
1084 	{
1085 		.name = "rt711-aif1",
1086 		.id = RT711_AIF1,
1087 		.playback = {
1088 			.stream_name = "DP3 Playback",
1089 			.channels_min = 1,
1090 			.channels_max = 2,
1091 			.rates = RT711_STEREO_RATES,
1092 			.formats = RT711_FORMATS,
1093 		},
1094 		.capture = {
1095 			.stream_name = "DP4 Capture",
1096 			.channels_min = 1,
1097 			.channels_max = 2,
1098 			.rates = RT711_STEREO_RATES,
1099 			.formats = RT711_FORMATS,
1100 		},
1101 		.ops = &rt711_ops,
1102 	},
1103 	{
1104 		.name = "rt711-aif2",
1105 		.id = RT711_AIF2,
1106 		.capture = {
1107 			.stream_name = "DP2 Capture",
1108 			.channels_min = 1,
1109 			.channels_max = 2,
1110 			.rates = RT711_STEREO_RATES,
1111 			.formats = RT711_FORMATS,
1112 		},
1113 		.ops = &rt711_ops,
1114 	}
1115 };
1116 
1117 /* Bus clock frequency */
1118 #define RT711_CLK_FREQ_9600000HZ 9600000
1119 #define RT711_CLK_FREQ_12000000HZ 12000000
1120 #define RT711_CLK_FREQ_6000000HZ 6000000
1121 #define RT711_CLK_FREQ_4800000HZ 4800000
1122 #define RT711_CLK_FREQ_2400000HZ 2400000
1123 #define RT711_CLK_FREQ_12288000HZ 12288000
1124 
1125 int rt711_clock_config(struct device *dev)
1126 {
1127 	struct rt711_priv *rt711 = dev_get_drvdata(dev);
1128 	unsigned int clk_freq, value;
1129 
1130 	clk_freq = (rt711->params.curr_dr_freq >> 1);
1131 
1132 	switch (clk_freq) {
1133 	case RT711_CLK_FREQ_12000000HZ:
1134 		value = 0x0;
1135 		break;
1136 	case RT711_CLK_FREQ_6000000HZ:
1137 		value = 0x1;
1138 		break;
1139 	case RT711_CLK_FREQ_9600000HZ:
1140 		value = 0x2;
1141 		break;
1142 	case RT711_CLK_FREQ_4800000HZ:
1143 		value = 0x3;
1144 		break;
1145 	case RT711_CLK_FREQ_2400000HZ:
1146 		value = 0x4;
1147 		break;
1148 	case RT711_CLK_FREQ_12288000HZ:
1149 		value = 0x5;
1150 		break;
1151 	default:
1152 		return -EINVAL;
1153 	}
1154 
1155 	regmap_write(rt711->regmap, 0xe0, value);
1156 	regmap_write(rt711->regmap, 0xf0, value);
1157 
1158 	dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
1159 
1160 	return 0;
1161 }
1162 
1163 static void rt711_calibration_work(struct work_struct *work)
1164 {
1165 	struct rt711_priv *rt711 =
1166 		container_of(work, struct rt711_priv, calibration_work);
1167 
1168 	rt711_calibration(rt711);
1169 }
1170 
1171 int rt711_init(struct device *dev, struct regmap *sdw_regmap,
1172 			struct regmap *regmap, struct sdw_slave *slave)
1173 {
1174 	struct rt711_priv *rt711;
1175 	int ret;
1176 
1177 	rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL);
1178 	if (!rt711)
1179 		return -ENOMEM;
1180 
1181 	dev_set_drvdata(dev, rt711);
1182 	rt711->slave = slave;
1183 	rt711->sdw_regmap = sdw_regmap;
1184 	rt711->regmap = regmap;
1185 
1186 	mutex_init(&rt711->calibrate_mutex);
1187 	mutex_init(&rt711->disable_irq_lock);
1188 
1189 	INIT_DELAYED_WORK(&rt711->jack_detect_work, rt711_jack_detect_handler);
1190 	INIT_DELAYED_WORK(&rt711->jack_btn_check_work, rt711_btn_check_handler);
1191 	INIT_WORK(&rt711->calibration_work, rt711_calibration_work);
1192 
1193 	/*
1194 	 * Mark hw_init to false
1195 	 * HW init will be performed when device reports present
1196 	 */
1197 	rt711->hw_init = false;
1198 	rt711->first_hw_init = false;
1199 
1200 	/* JD source uses JD2 in default */
1201 	rt711->jd_src = RT711_JD2;
1202 
1203 	ret =  devm_snd_soc_register_component(dev,
1204 				&soc_codec_dev_rt711,
1205 				rt711_dai,
1206 				ARRAY_SIZE(rt711_dai));
1207 
1208 	dev_dbg(&slave->dev, "%s\n", __func__);
1209 
1210 	return ret;
1211 }
1212 
1213 int rt711_io_init(struct device *dev, struct sdw_slave *slave)
1214 {
1215 	struct rt711_priv *rt711 = dev_get_drvdata(dev);
1216 
1217 	rt711->disable_irq = false;
1218 
1219 	if (rt711->hw_init)
1220 		return 0;
1221 
1222 	if (rt711->first_hw_init) {
1223 		regcache_cache_only(rt711->regmap, false);
1224 		regcache_cache_bypass(rt711->regmap, true);
1225 	}
1226 
1227 	/*
1228 	 * PM runtime is only enabled when a Slave reports as Attached
1229 	 */
1230 	if (!rt711->first_hw_init) {
1231 		/* set autosuspend parameters */
1232 		pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
1233 		pm_runtime_use_autosuspend(&slave->dev);
1234 
1235 		/* update count of parent 'active' children */
1236 		pm_runtime_set_active(&slave->dev);
1237 
1238 		/* make sure the device does not suspend immediately */
1239 		pm_runtime_mark_last_busy(&slave->dev);
1240 
1241 		pm_runtime_enable(&slave->dev);
1242 	}
1243 
1244 	pm_runtime_get_noresume(&slave->dev);
1245 
1246 	rt711_reset(rt711->regmap);
1247 
1248 	/* power on */
1249 	regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
1250 
1251 	/* Set Pin Widget */
1252 	regmap_write(rt711->regmap, RT711_SET_PIN_MIC2, 0x25);
1253 	regmap_write(rt711->regmap, RT711_SET_PIN_HP, 0xc0);
1254 	regmap_write(rt711->regmap, RT711_SET_PIN_DMIC1, 0x20);
1255 	regmap_write(rt711->regmap, RT711_SET_PIN_DMIC2, 0x20);
1256 	regmap_write(rt711->regmap, RT711_SET_PIN_LINE1, 0x20);
1257 	regmap_write(rt711->regmap, RT711_SET_PIN_LINE2, 0x20);
1258 
1259 	/* Mute HP/ADC1/ADC2 */
1260 	regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0xa080);
1261 	regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0x9080);
1262 	regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x6080);
1263 	regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x5080);
1264 	regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x6080);
1265 	regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x5080);
1266 
1267 	/* Set Configuration Default */
1268 	regmap_write(rt711->regmap, 0x4f12, 0x91);
1269 	regmap_write(rt711->regmap, 0x4e12, 0xd6);
1270 	regmap_write(rt711->regmap, 0x4d12, 0x11);
1271 	regmap_write(rt711->regmap, 0x4c12, 0x20);
1272 	regmap_write(rt711->regmap, 0x4f13, 0x91);
1273 	regmap_write(rt711->regmap, 0x4e13, 0xd6);
1274 	regmap_write(rt711->regmap, 0x4d13, 0x11);
1275 	regmap_write(rt711->regmap, 0x4c13, 0x21);
1276 	regmap_write(rt711->regmap, 0x4c21, 0xf0);
1277 	regmap_write(rt711->regmap, 0x4d21, 0x11);
1278 	regmap_write(rt711->regmap, 0x4e21, 0x11);
1279 	regmap_write(rt711->regmap, 0x4f21, 0x01);
1280 
1281 	/* Data port arrangement */
1282 	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1283 		RT711_TX_RX_MUX_CTL, 0x0154);
1284 
1285 	/* Set index */
1286 	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1287 		RT711_DIGITAL_MISC_CTRL4, 0x201b);
1288 	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1289 		RT711_COMBO_JACK_AUTO_CTL1, 0x5089);
1290 	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1291 		RT711_VREFOUT_CTL, 0x5064);
1292 	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1293 		RT711_INLINE_CMD_CTL, 0xd249);
1294 
1295 	/* Finish Initial Settings, set power to D3 */
1296 	regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
1297 
1298 	if (rt711->first_hw_init)
1299 		rt711_calibration(rt711);
1300 	else
1301 		schedule_work(&rt711->calibration_work);
1302 
1303 	/*
1304 	 * if set_jack callback occurred early than io_init,
1305 	 * we set up the jack detection function now
1306 	 */
1307 	if (rt711->hs_jack)
1308 		rt711_jack_init(rt711);
1309 
1310 	if (rt711->first_hw_init) {
1311 		regcache_cache_bypass(rt711->regmap, false);
1312 		regcache_mark_dirty(rt711->regmap);
1313 	} else
1314 		rt711->first_hw_init = true;
1315 
1316 	/* Mark Slave initialization complete */
1317 	rt711->hw_init = true;
1318 
1319 	pm_runtime_mark_last_busy(&slave->dev);
1320 	pm_runtime_put_autosuspend(&slave->dev);
1321 
1322 	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
1323 	return 0;
1324 }
1325 
1326 MODULE_DESCRIPTION("ASoC RT711 SDW driver");
1327 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1328 MODULE_LICENSE("GPL");
1329