xref: /openbmc/linux/sound/soc/codecs/rt711-sdca.c (revision 71de0a05)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // rt711-sdca.c -- rt711 SDCA ALSA SoC audio driver
4 //
5 // Copyright(c) 2021 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/soundwire/sdw_registers.h>
16 #include <linux/slab.h>
17 #include <linux/bitops.h>
18 #include <sound/core.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/sdw.h>
22 #include <sound/soc-dapm.h>
23 #include <sound/initval.h>
24 #include <sound/tlv.h>
25 #include <sound/jack.h>
26 
27 #include "rt711-sdca.h"
28 
29 static int rt711_sdca_index_write(struct rt711_sdca_priv *rt711,
30 		unsigned int nid, unsigned int reg, unsigned int value)
31 {
32 	int ret;
33 	struct regmap *regmap = rt711->mbq_regmap;
34 	unsigned int addr = (nid << 20) | reg;
35 
36 	ret = regmap_write(regmap, addr, value);
37 	if (ret < 0)
38 		dev_err(&rt711->slave->dev,
39 			"Failed to set private value: %06x <= %04x ret=%d\n",
40 			addr, value, ret);
41 
42 	return ret;
43 }
44 
45 static int rt711_sdca_index_read(struct rt711_sdca_priv *rt711,
46 		unsigned int nid, unsigned int reg, unsigned int *value)
47 {
48 	int ret;
49 	struct regmap *regmap = rt711->mbq_regmap;
50 	unsigned int addr = (nid << 20) | reg;
51 
52 	ret = regmap_read(regmap, addr, value);
53 	if (ret < 0)
54 		dev_err(&rt711->slave->dev,
55 			"Failed to get private value: %06x => %04x ret=%d\n",
56 			addr, *value, ret);
57 
58 	return ret;
59 }
60 
61 static int rt711_sdca_index_update_bits(struct rt711_sdca_priv *rt711,
62 	unsigned int nid, unsigned int reg, unsigned int mask, unsigned int val)
63 {
64 	unsigned int tmp;
65 	int ret;
66 
67 	ret = rt711_sdca_index_read(rt711, nid, reg, &tmp);
68 	if (ret < 0)
69 		return ret;
70 
71 	set_mask_bits(&tmp, mask, val);
72 	return rt711_sdca_index_write(rt711, nid, reg, tmp);
73 }
74 
75 static void rt711_sdca_reset(struct rt711_sdca_priv *rt711)
76 {
77 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
78 		RT711_PARA_VERB_CTL, RT711_HIDDEN_REG_SW_RESET,
79 		RT711_HIDDEN_REG_SW_RESET);
80 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL,
81 		RT711_HDA_LEGACY_RESET_CTL, 0x1, 0x1);
82 }
83 
84 static int rt711_sdca_calibration(struct rt711_sdca_priv *rt711)
85 {
86 	unsigned int val, loop_rc = 0, loop_dc = 0;
87 	struct device *dev;
88 	struct regmap *regmap = rt711->regmap;
89 	int chk_cnt = 100;
90 	int ret = 0;
91 
92 	mutex_lock(&rt711->calibrate_mutex);
93 	dev = regmap_get_device(regmap);
94 
95 	regmap_read(rt711->regmap, RT711_RC_CAL_STATUS, &val);
96 	/* RC calibration */
97 	if (!(val & 0x40))
98 		rt711_sdca_index_update_bits(rt711, RT711_VENDOR_ANALOG_CTL,
99 			RT711_MISC_POWER_CTL0, 0x0010, 0x0010);
100 
101 	for (loop_rc = 0; loop_rc < chk_cnt && !(val & 0x40); loop_rc++) {
102 		usleep_range(10000, 11000);
103 		ret = regmap_read(rt711->regmap, RT711_RC_CAL_STATUS, &val);
104 		if (ret < 0)
105 			goto _cali_fail_;
106 	}
107 	if (loop_rc == chk_cnt)
108 		dev_err(dev, "%s, RC calibration time-out!\n", __func__);
109 
110 	/* HP calibration by manual mode setting */
111 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
112 		RT711_FSM_CTL, 0x2000, 0x2000);
113 
114 	/* Calibration manual mode */
115 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
116 		RT711_FSM_CTL, 0xf, RT711_CALI_CTL);
117 
118 	/* reset HP calibration */
119 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_CALI,
120 		RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_FORCE_CALI_RST, 0x00);
121 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_CALI,
122 		RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_FORCE_CALI_RST,
123 		RT711_DAC_DC_FORCE_CALI_RST);
124 
125 	/* cal_clk_en_reg */
126 	if (rt711->hw_ver == RT711_VER_VD0)
127 		rt711_sdca_index_update_bits(rt711, RT711_VENDOR_CALI,
128 			RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_CALI_CLK_EN,
129 			RT711_DAC_DC_CALI_CLK_EN);
130 
131 	/* trigger */
132 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_CALI,
133 		RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_CALI_TRIGGER,
134 		RT711_DAC_DC_CALI_TRIGGER);
135 
136 	/* wait for calibration process */
137 	rt711_sdca_index_read(rt711, RT711_VENDOR_CALI,
138 		RT711_DAC_DC_CALI_CTL1, &val);
139 
140 	for (loop_dc = 0; loop_dc < chk_cnt &&
141 		(val & RT711_DAC_DC_CALI_TRIGGER); loop_dc++) {
142 		usleep_range(10000, 11000);
143 		ret = rt711_sdca_index_read(rt711, RT711_VENDOR_CALI,
144 			RT711_DAC_DC_CALI_CTL1, &val);
145 		if (ret < 0)
146 			goto _cali_fail_;
147 	}
148 	if (loop_dc == chk_cnt)
149 		dev_err(dev, "%s, calibration time-out!\n", __func__);
150 
151 	if (loop_dc == chk_cnt || loop_rc == chk_cnt)
152 		ret = -ETIMEDOUT;
153 
154 _cali_fail_:
155 	/* enable impedance sense */
156 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
157 		RT711_FSM_CTL, RT711_FSM_IMP_EN, RT711_FSM_IMP_EN);
158 
159 	/* release HP-JD and trigger FSM */
160 	rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
161 		RT711_DIGITAL_MISC_CTRL4, 0x201b);
162 
163 	mutex_unlock(&rt711->calibrate_mutex);
164 	dev_dbg(dev, "%s calibration complete, ret=%d\n", __func__, ret);
165 	return ret;
166 }
167 
168 static unsigned int rt711_sdca_button_detect(struct rt711_sdca_priv *rt711)
169 {
170 	unsigned int btn_type = 0, offset, idx, val, owner;
171 	int ret;
172 	unsigned char buf[3];
173 
174 	/* get current UMP message owner */
175 	ret = regmap_read(rt711->regmap,
176 		SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_CURRENT_OWNER, 0),
177 		&owner);
178 	if (ret < 0)
179 		return 0;
180 
181 	/* if owner is device then there is no button event from device */
182 	if (owner == 1)
183 		return 0;
184 
185 	/* read UMP message offset */
186 	ret = regmap_read(rt711->regmap,
187 		SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0),
188 		&offset);
189 	if (ret < 0)
190 		goto _end_btn_det_;
191 
192 	for (idx = 0; idx < sizeof(buf); idx++) {
193 		ret = regmap_read(rt711->regmap,
194 			RT711_BUF_ADDR_HID1 + offset + idx, &val);
195 		if (ret < 0)
196 			goto _end_btn_det_;
197 		buf[idx] = val & 0xff;
198 	}
199 
200 	if (buf[0] == 0x11) {
201 		switch (buf[1] & 0xf0) {
202 		case 0x10:
203 			btn_type |= SND_JACK_BTN_2;
204 			break;
205 		case 0x20:
206 			btn_type |= SND_JACK_BTN_3;
207 			break;
208 		case 0x40:
209 			btn_type |= SND_JACK_BTN_0;
210 			break;
211 		case 0x80:
212 			btn_type |= SND_JACK_BTN_1;
213 			break;
214 		}
215 		switch (buf[2]) {
216 		case 0x01:
217 		case 0x10:
218 			btn_type |= SND_JACK_BTN_2;
219 			break;
220 		case 0x02:
221 		case 0x20:
222 			btn_type |= SND_JACK_BTN_3;
223 			break;
224 		case 0x04:
225 		case 0x40:
226 			btn_type |= SND_JACK_BTN_0;
227 			break;
228 		case 0x08:
229 		case 0x80:
230 			btn_type |= SND_JACK_BTN_1;
231 			break;
232 		}
233 	}
234 
235 _end_btn_det_:
236 	/* Host is owner, so set back to device */
237 	if (owner == 0)
238 		/* set owner to device */
239 		regmap_write(rt711->regmap,
240 			SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01,
241 				RT711_SDCA_CTL_HIDTX_SET_OWNER_TO_DEVICE, 0), 0x01);
242 
243 	return btn_type;
244 }
245 
246 static int rt711_sdca_headset_detect(struct rt711_sdca_priv *rt711)
247 {
248 	unsigned int det_mode;
249 	int ret;
250 
251 	/* get detected_mode */
252 	ret = regmap_read(rt711->regmap,
253 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0),
254 		&det_mode);
255 	if (ret < 0)
256 		goto io_error;
257 
258 	switch (det_mode) {
259 	case 0x00:
260 		rt711->jack_type = 0;
261 		break;
262 	case 0x03:
263 		rt711->jack_type = SND_JACK_HEADPHONE;
264 		break;
265 	case 0x05:
266 		rt711->jack_type = SND_JACK_HEADSET;
267 		break;
268 	}
269 
270 	/* write selected_mode */
271 	if (det_mode) {
272 		ret = regmap_write(rt711->regmap,
273 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_SELECTED_MODE, 0),
274 			det_mode);
275 		if (ret < 0)
276 			goto io_error;
277 	}
278 
279 	dev_dbg(&rt711->slave->dev,
280 		"%s, detected_mode=0x%x\n", __func__, det_mode);
281 
282 	return 0;
283 
284 io_error:
285 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
286 	return ret;
287 }
288 
289 static void rt711_sdca_jack_detect_handler(struct work_struct *work)
290 {
291 	struct rt711_sdca_priv *rt711 =
292 		container_of(work, struct rt711_sdca_priv, jack_detect_work.work);
293 	int btn_type = 0, ret;
294 
295 	if (!rt711->hs_jack)
296 		return;
297 
298 	if (!snd_soc_card_is_instantiated(rt711->component->card))
299 		return;
300 
301 	/* SDW_SCP_SDCA_INT_SDCA_0 is used for jack detection */
302 	if (rt711->scp_sdca_stat1 & SDW_SCP_SDCA_INT_SDCA_0) {
303 		ret = rt711_sdca_headset_detect(rt711);
304 		if (ret < 0)
305 			return;
306 	}
307 
308 	/* SDW_SCP_SDCA_INT_SDCA_8 is used for button detection */
309 	if (rt711->scp_sdca_stat2 & SDW_SCP_SDCA_INT_SDCA_8)
310 		btn_type = rt711_sdca_button_detect(rt711);
311 
312 	if (rt711->jack_type == 0)
313 		btn_type = 0;
314 
315 	dev_dbg(&rt711->slave->dev,
316 		"in %s, jack_type=0x%x\n", __func__, rt711->jack_type);
317 	dev_dbg(&rt711->slave->dev,
318 		"in %s, btn_type=0x%x\n", __func__, btn_type);
319 	dev_dbg(&rt711->slave->dev,
320 		"in %s, scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__,
321 		rt711->scp_sdca_stat1, rt711->scp_sdca_stat2);
322 
323 	snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type,
324 			SND_JACK_HEADSET |
325 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
326 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
327 
328 	if (btn_type) {
329 		/* button released */
330 		snd_soc_jack_report(rt711->hs_jack, rt711->jack_type,
331 			SND_JACK_HEADSET |
332 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
333 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
334 
335 		mod_delayed_work(system_power_efficient_wq,
336 			&rt711->jack_btn_check_work, msecs_to_jiffies(200));
337 	}
338 }
339 
340 static void rt711_sdca_btn_check_handler(struct work_struct *work)
341 {
342 	struct rt711_sdca_priv *rt711 =
343 		container_of(work, struct rt711_sdca_priv, jack_btn_check_work.work);
344 	int btn_type = 0, ret, idx;
345 	unsigned int det_mode, offset, val;
346 	unsigned char buf[3];
347 
348 	ret = regmap_read(rt711->regmap,
349 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0),
350 		&det_mode);
351 	if (ret < 0)
352 		goto io_error;
353 
354 	/* pin attached */
355 	if (det_mode) {
356 		/* read UMP message offset */
357 		ret = regmap_read(rt711->regmap,
358 			SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0),
359 			&offset);
360 		if (ret < 0)
361 			goto io_error;
362 
363 		for (idx = 0; idx < sizeof(buf); idx++) {
364 			ret = regmap_read(rt711->regmap,
365 				RT711_BUF_ADDR_HID1 + offset + idx, &val);
366 			if (ret < 0)
367 				goto io_error;
368 			buf[idx] = val & 0xff;
369 		}
370 
371 		if (buf[0] == 0x11) {
372 			switch (buf[1] & 0xf0) {
373 			case 0x10:
374 				btn_type |= SND_JACK_BTN_2;
375 				break;
376 			case 0x20:
377 				btn_type |= SND_JACK_BTN_3;
378 				break;
379 			case 0x40:
380 				btn_type |= SND_JACK_BTN_0;
381 				break;
382 			case 0x80:
383 				btn_type |= SND_JACK_BTN_1;
384 				break;
385 			}
386 			switch (buf[2]) {
387 			case 0x01:
388 			case 0x10:
389 				btn_type |= SND_JACK_BTN_2;
390 				break;
391 			case 0x02:
392 			case 0x20:
393 				btn_type |= SND_JACK_BTN_3;
394 				break;
395 			case 0x04:
396 			case 0x40:
397 				btn_type |= SND_JACK_BTN_0;
398 				break;
399 			case 0x08:
400 			case 0x80:
401 				btn_type |= SND_JACK_BTN_1;
402 				break;
403 			}
404 		}
405 	} else
406 		rt711->jack_type = 0;
407 
408 	dev_dbg(&rt711->slave->dev, "%s, btn_type=0x%x\n",	__func__, btn_type);
409 	snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type,
410 			SND_JACK_HEADSET |
411 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
412 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
413 
414 	if (btn_type) {
415 		/* button released */
416 		snd_soc_jack_report(rt711->hs_jack, rt711->jack_type,
417 			SND_JACK_HEADSET |
418 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
419 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
420 
421 		mod_delayed_work(system_power_efficient_wq,
422 			&rt711->jack_btn_check_work, msecs_to_jiffies(200));
423 	}
424 
425 	return;
426 
427 io_error:
428 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
429 }
430 
431 static void rt711_sdca_jack_init(struct rt711_sdca_priv *rt711)
432 {
433 	mutex_lock(&rt711->calibrate_mutex);
434 
435 	if (rt711->hs_jack) {
436 		/* Enable HID1 event & set button RTC mode */
437 		rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL,
438 			RT711_PUSH_BTN_INT_CTL6, 0x80f0, 0x8000);
439 		rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL,
440 			RT711_PUSH_BTN_INT_CTL2, 0x11dd, 0x11dd);
441 		rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL,
442 			RT711_PUSH_BTN_INT_CTL7, 0xffff);
443 		rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL,
444 			RT711_PUSH_BTN_INT_CTL9, 0xf000, 0x0000);
445 
446 		/* GE_mode_change_event_en & Hid1_push_button_event_en */
447 		rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL,
448 			RT711_GE_MODE_RELATED_CTL, 0x0c00, 0x0c00);
449 
450 		switch (rt711->jd_src) {
451 		case RT711_JD1:
452 			/* default settings was already for JD1 */
453 			break;
454 		case RT711_JD2:
455 			rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
456 				RT711_JD_CTL1, RT711_JD2_DIGITAL_MODE_SEL,
457 				RT711_JD2_DIGITAL_MODE_SEL);
458 			rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
459 				RT711_JD_CTL2, RT711_JD2_2PORT_200K_DECODE_HP | RT711_HP_JD_SEL_JD2,
460 				RT711_JD2_2PORT_200K_DECODE_HP | RT711_HP_JD_SEL_JD2);
461 			rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
462 				RT711_CC_DET1,
463 				RT711_HP_JD_FINAL_RESULT_CTL_JD12,
464 				RT711_HP_JD_FINAL_RESULT_CTL_JD12);
465 			break;
466 		case RT711_JD2_100K:
467 			rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
468 				RT711_COMBO_JACK_AUTO_CTL3, 0xa47e);
469 			rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
470 				RT711_JD_CTL1, RT711_JD2_DIGITAL_MODE_SEL,
471 				RT711_JD2_DIGITAL_MODE_SEL);
472 			rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
473 				RT711_JD_CTL2, RT711_JD2_2PORT_200K_DECODE_HP |
474 				RT711_JD2_2PORT_100K_DECODE_MASK | RT711_HP_JD_SEL_JD2,
475 				RT711_JD2_2PORT_100K_DECODE_HP | RT711_HP_JD_SEL_JD2);
476 			rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
477 				RT711_CC_DET1,
478 				RT711_HP_JD_FINAL_RESULT_CTL_JD12 | RT711_POW_CC1_AGPI,
479 				RT711_HP_JD_FINAL_RESULT_CTL_JD12 | RT711_POW_CC1_AGPI_OFF);
480 			break;
481 		default:
482 			dev_warn(rt711->component->dev, "Wrong JD source\n");
483 			break;
484 		}
485 
486 		/* set SCP_SDCA_IntMask1[0]=1 */
487 		sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK1, SDW_SCP_SDCA_INTMASK_SDCA_0);
488 		/* set SCP_SDCA_IntMask2[0]=1 */
489 		sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK2, SDW_SCP_SDCA_INTMASK_SDCA_8);
490 		dev_dbg(&rt711->slave->dev, "in %s enable\n", __func__);
491 	} else {
492 		/* disable HID 1/2 event */
493 		rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL,
494 			RT711_GE_MODE_RELATED_CTL, 0x0c00, 0x0000);
495 
496 		dev_dbg(&rt711->slave->dev, "in %s disable\n", __func__);
497 	}
498 
499 	mutex_unlock(&rt711->calibrate_mutex);
500 }
501 
502 static int rt711_sdca_set_jack_detect(struct snd_soc_component *component,
503 	struct snd_soc_jack *hs_jack, void *data)
504 {
505 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
506 	int ret;
507 
508 	rt711->hs_jack = hs_jack;
509 
510 	ret = pm_runtime_resume_and_get(component->dev);
511 	if (ret < 0) {
512 		if (ret != -EACCES) {
513 			dev_err(component->dev, "%s: failed to resume %d\n", __func__, ret);
514 			return ret;
515 		}
516 
517 		/* pm_runtime not enabled yet */
518 		dev_dbg(component->dev,	"%s: skipping jack init for now\n", __func__);
519 		return 0;
520 	}
521 
522 	rt711_sdca_jack_init(rt711);
523 
524 	pm_runtime_mark_last_busy(component->dev);
525 	pm_runtime_put_autosuspend(component->dev);
526 
527 	return 0;
528 }
529 
530 /* For SDCA control DAC/ADC Gain */
531 static int rt711_sdca_set_gain_put(struct snd_kcontrol *kcontrol,
532 		struct snd_ctl_elem_value *ucontrol)
533 {
534 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
535 	struct soc_mixer_control *mc =
536 		(struct soc_mixer_control *)kcontrol->private_value;
537 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
538 	unsigned int read_l, read_r, gain_l_val, gain_r_val;
539 	unsigned int i, adc_vol_flag = 0, changed = 0;
540 	unsigned int lvalue, rvalue;
541 
542 	if (strstr(ucontrol->id.name, "FU1E Capture Volume") ||
543 		strstr(ucontrol->id.name, "FU0F Capture Volume"))
544 		adc_vol_flag = 1;
545 
546 	regmap_read(rt711->mbq_regmap, mc->reg, &lvalue);
547 	regmap_read(rt711->mbq_regmap, mc->rreg, &rvalue);
548 
549 	/* control value to 2's complement value */
550 	/* L Channel */
551 	gain_l_val = ucontrol->value.integer.value[0];
552 	if (gain_l_val > mc->max)
553 		gain_l_val = mc->max;
554 	read_l = gain_l_val;
555 
556 	if (mc->shift == 8) /* boost gain */
557 		gain_l_val = (gain_l_val * 10) << mc->shift;
558 	else { /* ADC/DAC gain */
559 		if (adc_vol_flag && gain_l_val > mc->shift)
560 			gain_l_val = (gain_l_val - mc->shift) * 75;
561 		else
562 			gain_l_val = (mc->shift - gain_l_val) * 75;
563 		gain_l_val <<= 8;
564 		gain_l_val /= 100;
565 		if (!(adc_vol_flag && read_l > mc->shift)) {
566 			gain_l_val = ~gain_l_val;
567 			gain_l_val += 1;
568 		}
569 		gain_l_val &= 0xffff;
570 	}
571 
572 	/* R Channel */
573 	gain_r_val = ucontrol->value.integer.value[1];
574 	if (gain_r_val > mc->max)
575 		gain_r_val = mc->max;
576 	read_r = gain_r_val;
577 
578 	if (mc->shift == 8) /* boost gain */
579 		gain_r_val = (gain_r_val * 10) << mc->shift;
580 	else { /* ADC/DAC gain */
581 		if (adc_vol_flag && gain_r_val > mc->shift)
582 			gain_r_val = (gain_r_val - mc->shift) * 75;
583 		else
584 			gain_r_val = (mc->shift - gain_r_val) * 75;
585 		gain_r_val <<= 8;
586 		gain_r_val /= 100;
587 		if (!(adc_vol_flag && read_r > mc->shift)) {
588 			gain_r_val = ~gain_r_val;
589 			gain_r_val += 1;
590 		}
591 		gain_r_val &= 0xffff;
592 	}
593 
594 	if (lvalue != gain_l_val || rvalue != gain_r_val)
595 		changed = 1;
596 	else
597 		return 0;
598 
599 	for (i = 0; i < 3; i++) { /* retry 3 times at most */
600 		/* Lch*/
601 		regmap_write(rt711->mbq_regmap, mc->reg, gain_l_val);
602 
603 		/* Rch */
604 		regmap_write(rt711->mbq_regmap, mc->rreg, gain_r_val);
605 
606 		regmap_read(rt711->mbq_regmap, mc->reg, &read_l);
607 		regmap_read(rt711->mbq_regmap, mc->rreg, &read_r);
608 		if (read_r == gain_r_val && read_l == gain_l_val)
609 			break;
610 	}
611 
612 	return i == 3 ? -EIO : changed;
613 }
614 
615 static int rt711_sdca_set_gain_get(struct snd_kcontrol *kcontrol,
616 		struct snd_ctl_elem_value *ucontrol)
617 {
618 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
619 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
620 	struct soc_mixer_control *mc =
621 		(struct soc_mixer_control *)kcontrol->private_value;
622 	unsigned int read_l, read_r, ctl_l = 0, ctl_r = 0;
623 	unsigned int adc_vol_flag = 0, neg_flag = 0;
624 
625 	if (strstr(ucontrol->id.name, "FU1E Capture Volume") ||
626 		strstr(ucontrol->id.name, "FU0F Capture Volume"))
627 		adc_vol_flag = 1;
628 
629 	regmap_read(rt711->mbq_regmap, mc->reg, &read_l);
630 	regmap_read(rt711->mbq_regmap, mc->rreg, &read_r);
631 
632 	/* 2's complement value to control value */
633 	if (mc->shift == 8) /* boost gain */
634 		ctl_l = (read_l >> mc->shift) / 10;
635 	else { /* ADC/DAC gain */
636 		ctl_l = read_l;
637 		if (read_l & BIT(15)) {
638 			ctl_l = 0xffff & ~(read_l - 1);
639 			neg_flag = 1;
640 		}
641 		ctl_l *= 100;
642 		ctl_l >>= 8;
643 		if (adc_vol_flag) {
644 			if (neg_flag)
645 				ctl_l = mc->shift - (ctl_l / 75);
646 			else
647 				ctl_l = mc->shift + (ctl_l / 75);
648 		} else
649 			ctl_l = mc->max - (ctl_l / 75);
650 	}
651 
652 	neg_flag = 0;
653 	if (read_l != read_r) {
654 		if (mc->shift == 8) /* boost gain */
655 			ctl_r = (read_r >> mc->shift) / 10;
656 		else { /* ADC/DAC gain */
657 			ctl_r = read_r;
658 			if (read_r & BIT(15)) {
659 				ctl_r = 0xffff & ~(read_r - 1);
660 				neg_flag = 1;
661 			}
662 			ctl_r *= 100;
663 			ctl_r >>= 8;
664 			if (adc_vol_flag) {
665 				if (neg_flag)
666 					ctl_r = mc->shift - (ctl_r / 75);
667 				else
668 					ctl_r = mc->shift + (ctl_r / 75);
669 			} else
670 				ctl_r = mc->max - (ctl_r / 75);
671 		}
672 	} else
673 		ctl_r = ctl_l;
674 
675 	ucontrol->value.integer.value[0] = ctl_l;
676 	ucontrol->value.integer.value[1] = ctl_r;
677 
678 	return 0;
679 }
680 
681 static int rt711_sdca_set_fu0f_capture_ctl(struct rt711_sdca_priv *rt711)
682 {
683 	int err;
684 	unsigned int ch_l, ch_r;
685 
686 	ch_l = (rt711->fu0f_dapm_mute || rt711->fu0f_mixer_l_mute) ? 0x01 : 0x00;
687 	ch_r = (rt711->fu0f_dapm_mute || rt711->fu0f_mixer_r_mute) ? 0x01 : 0x00;
688 
689 	err = regmap_write(rt711->regmap,
690 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F,
691 			RT711_SDCA_CTL_FU_MUTE, CH_L), ch_l);
692 	if (err < 0)
693 		return err;
694 
695 	err = regmap_write(rt711->regmap,
696 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F,
697 			RT711_SDCA_CTL_FU_MUTE, CH_R), ch_r);
698 	if (err < 0)
699 		return err;
700 
701 	return 0;
702 }
703 
704 static int rt711_sdca_set_fu1e_capture_ctl(struct rt711_sdca_priv *rt711)
705 {
706 	int err;
707 	unsigned int ch_l, ch_r;
708 
709 	ch_l = (rt711->fu1e_dapm_mute || rt711->fu1e_mixer_l_mute) ? 0x01 : 0x00;
710 	ch_r = (rt711->fu1e_dapm_mute || rt711->fu1e_mixer_r_mute) ? 0x01 : 0x00;
711 
712 	err = regmap_write(rt711->regmap,
713 			SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E,
714 			RT711_SDCA_CTL_FU_MUTE, CH_L), ch_l);
715 	if (err < 0)
716 		return err;
717 
718 	err = regmap_write(rt711->regmap,
719 			SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E,
720 			RT711_SDCA_CTL_FU_MUTE, CH_R), ch_r);
721 	if (err < 0)
722 		return err;
723 
724 	return 0;
725 }
726 
727 static int rt711_sdca_fu1e_capture_get(struct snd_kcontrol *kcontrol,
728 			struct snd_ctl_elem_value *ucontrol)
729 {
730 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
731 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
732 
733 	ucontrol->value.integer.value[0] = !rt711->fu1e_mixer_l_mute;
734 	ucontrol->value.integer.value[1] = !rt711->fu1e_mixer_r_mute;
735 	return 0;
736 }
737 
738 static int rt711_sdca_fu1e_capture_put(struct snd_kcontrol *kcontrol,
739 			struct snd_ctl_elem_value *ucontrol)
740 {
741 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
742 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
743 	int err, changed = 0;
744 
745 	if (rt711->fu1e_mixer_l_mute != !ucontrol->value.integer.value[0] ||
746 		rt711->fu1e_mixer_r_mute != !ucontrol->value.integer.value[1])
747 		changed = 1;
748 
749 	rt711->fu1e_mixer_l_mute = !ucontrol->value.integer.value[0];
750 	rt711->fu1e_mixer_r_mute = !ucontrol->value.integer.value[1];
751 	err = rt711_sdca_set_fu1e_capture_ctl(rt711);
752 	if (err < 0)
753 		return err;
754 
755 	return changed;
756 }
757 
758 static int rt711_sdca_fu0f_capture_get(struct snd_kcontrol *kcontrol,
759 			struct snd_ctl_elem_value *ucontrol)
760 {
761 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
762 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
763 
764 	ucontrol->value.integer.value[0] = !rt711->fu0f_mixer_l_mute;
765 	ucontrol->value.integer.value[1] = !rt711->fu0f_mixer_r_mute;
766 	return 0;
767 }
768 
769 static int rt711_sdca_fu0f_capture_put(struct snd_kcontrol *kcontrol,
770 			struct snd_ctl_elem_value *ucontrol)
771 {
772 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
773 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
774 	int err, changed = 0;
775 
776 	if (rt711->fu0f_mixer_l_mute != !ucontrol->value.integer.value[0] ||
777 		rt711->fu0f_mixer_r_mute != !ucontrol->value.integer.value[1])
778 		changed = 1;
779 
780 	rt711->fu0f_mixer_l_mute = !ucontrol->value.integer.value[0];
781 	rt711->fu0f_mixer_r_mute = !ucontrol->value.integer.value[1];
782 	err = rt711_sdca_set_fu0f_capture_ctl(rt711);
783 	if (err < 0)
784 		return err;
785 
786 	return changed;
787 }
788 
789 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0);
790 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0);
791 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0);
792 
793 static const struct snd_kcontrol_new rt711_sdca_snd_controls[] = {
794 	SOC_DOUBLE_R_EXT_TLV("FU05 Playback Volume",
795 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_VOLUME, CH_L),
796 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_VOLUME, CH_R),
797 		0x57, 0x57, 0,
798 		rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, out_vol_tlv),
799 	SOC_DOUBLE_EXT("FU1E Capture Switch", SND_SOC_NOPM, 0, 1, 1, 0,
800 		rt711_sdca_fu1e_capture_get, rt711_sdca_fu1e_capture_put),
801 	SOC_DOUBLE_EXT("FU0F Capture Switch", SND_SOC_NOPM, 0, 1, 1, 0,
802 		rt711_sdca_fu0f_capture_get, rt711_sdca_fu0f_capture_put),
803 	SOC_DOUBLE_R_EXT_TLV("FU1E Capture Volume",
804 		SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_VOLUME, CH_L),
805 		SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_VOLUME, CH_R),
806 		0x17, 0x3f, 0,
807 		rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, in_vol_tlv),
808 	SOC_DOUBLE_R_EXT_TLV("FU0F Capture Volume",
809 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_VOLUME, CH_L),
810 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_VOLUME, CH_R),
811 		0x17, 0x3f, 0,
812 		rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, in_vol_tlv),
813 	SOC_DOUBLE_R_EXT_TLV("FU44 Gain Volume",
814 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PLATFORM_FU44, RT711_SDCA_CTL_FU_CH_GAIN, CH_L),
815 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PLATFORM_FU44, RT711_SDCA_CTL_FU_CH_GAIN, CH_R),
816 		8, 3, 0,
817 		rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, mic_vol_tlv),
818 	SOC_DOUBLE_R_EXT_TLV("FU15 Gain Volume",
819 		SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PLATFORM_FU15, RT711_SDCA_CTL_FU_CH_GAIN, CH_L),
820 		SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PLATFORM_FU15, RT711_SDCA_CTL_FU_CH_GAIN, CH_R),
821 		8, 3, 0,
822 		rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, mic_vol_tlv),
823 };
824 
825 static int rt711_sdca_mux_get(struct snd_kcontrol *kcontrol,
826 			struct snd_ctl_elem_value *ucontrol)
827 {
828 	struct snd_soc_component *component =
829 		snd_soc_dapm_kcontrol_component(kcontrol);
830 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
831 	unsigned int val = 0, mask_sft;
832 
833 	if (strstr(ucontrol->id.name, "ADC 22 Mux"))
834 		mask_sft = 10;
835 	else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
836 		mask_sft = 13;
837 	else
838 		return -EINVAL;
839 
840 	rt711_sdca_index_read(rt711, RT711_VENDOR_HDA_CTL,
841 		RT711_HDA_LEGACY_MUX_CTL1, &val);
842 
843 	ucontrol->value.enumerated.item[0] = (val >> mask_sft) & 0x7;
844 
845 	return 0;
846 }
847 
848 static int rt711_sdca_mux_put(struct snd_kcontrol *kcontrol,
849 			struct snd_ctl_elem_value *ucontrol)
850 {
851 	struct snd_soc_component *component =
852 		snd_soc_dapm_kcontrol_component(kcontrol);
853 	struct snd_soc_dapm_context *dapm =
854 		snd_soc_dapm_kcontrol_dapm(kcontrol);
855 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
856 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
857 	unsigned int *item = ucontrol->value.enumerated.item;
858 	unsigned int val, val2 = 0, change, mask_sft;
859 
860 	if (item[0] >= e->items)
861 		return -EINVAL;
862 
863 	if (strstr(ucontrol->id.name, "ADC 22 Mux"))
864 		mask_sft = 10;
865 	else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
866 		mask_sft = 13;
867 	else
868 		return -EINVAL;
869 
870 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
871 
872 	rt711_sdca_index_read(rt711, RT711_VENDOR_HDA_CTL,
873 		RT711_HDA_LEGACY_MUX_CTL1, &val2);
874 	val2 = (val2 >> mask_sft) & 0x7;
875 
876 	if (val == val2)
877 		change = 0;
878 	else
879 		change = 1;
880 
881 	if (change)
882 		rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL,
883 			RT711_HDA_LEGACY_MUX_CTL1, 0x7 << mask_sft,
884 			val << mask_sft);
885 
886 	snd_soc_dapm_mux_update_power(dapm, kcontrol,
887 		item[0], e, NULL);
888 
889 	return change;
890 }
891 
892 static const char * const adc_mux_text[] = {
893 	"MIC2",
894 	"LINE1",
895 	"LINE2",
896 	"DMIC",
897 };
898 
899 static SOC_ENUM_SINGLE_DECL(
900 	rt711_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text);
901 
902 static SOC_ENUM_SINGLE_DECL(
903 	rt711_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text);
904 
905 static const struct snd_kcontrol_new rt711_sdca_adc22_mux =
906 	SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum,
907 			rt711_sdca_mux_get, rt711_sdca_mux_put);
908 
909 static const struct snd_kcontrol_new rt711_sdca_adc23_mux =
910 	SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum,
911 			rt711_sdca_mux_get, rt711_sdca_mux_put);
912 
913 static int rt711_sdca_fu05_event(struct snd_soc_dapm_widget *w,
914 	struct snd_kcontrol *kcontrol, int event)
915 {
916 	struct snd_soc_component *component =
917 		snd_soc_dapm_to_component(w->dapm);
918 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
919 	unsigned char unmute = 0x0, mute = 0x1;
920 
921 	switch (event) {
922 	case SND_SOC_DAPM_POST_PMU:
923 		regmap_write(rt711->regmap,
924 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05,
925 				RT711_SDCA_CTL_FU_MUTE, CH_L),
926 				unmute);
927 		regmap_write(rt711->regmap,
928 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05,
929 				RT711_SDCA_CTL_FU_MUTE, CH_R),
930 				unmute);
931 		break;
932 	case SND_SOC_DAPM_PRE_PMD:
933 		regmap_write(rt711->regmap,
934 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05,
935 				RT711_SDCA_CTL_FU_MUTE, CH_L),
936 				mute);
937 		regmap_write(rt711->regmap,
938 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05,
939 				RT711_SDCA_CTL_FU_MUTE, CH_R),
940 				mute);
941 		break;
942 	}
943 	return 0;
944 }
945 
946 static int rt711_sdca_fu0f_event(struct snd_soc_dapm_widget *w,
947 	struct snd_kcontrol *kcontrol, int event)
948 {
949 	struct snd_soc_component *component =
950 		snd_soc_dapm_to_component(w->dapm);
951 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
952 
953 	switch (event) {
954 	case SND_SOC_DAPM_POST_PMU:
955 		rt711->fu0f_dapm_mute = false;
956 		rt711_sdca_set_fu0f_capture_ctl(rt711);
957 		break;
958 	case SND_SOC_DAPM_PRE_PMD:
959 		rt711->fu0f_dapm_mute = true;
960 		rt711_sdca_set_fu0f_capture_ctl(rt711);
961 		break;
962 	}
963 	return 0;
964 }
965 
966 static int rt711_sdca_fu1e_event(struct snd_soc_dapm_widget *w,
967 	struct snd_kcontrol *kcontrol, int event)
968 {
969 	struct snd_soc_component *component =
970 		snd_soc_dapm_to_component(w->dapm);
971 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
972 
973 	switch (event) {
974 	case SND_SOC_DAPM_POST_PMU:
975 		rt711->fu1e_dapm_mute = false;
976 		rt711_sdca_set_fu1e_capture_ctl(rt711);
977 		break;
978 	case SND_SOC_DAPM_PRE_PMD:
979 		rt711->fu1e_dapm_mute = true;
980 		rt711_sdca_set_fu1e_capture_ctl(rt711);
981 		break;
982 	}
983 	return 0;
984 }
985 
986 static int rt711_sdca_pde28_event(struct snd_soc_dapm_widget *w,
987 	struct snd_kcontrol *kcontrol, int event)
988 {
989 	struct snd_soc_component *component =
990 		snd_soc_dapm_to_component(w->dapm);
991 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
992 	unsigned char ps0 = 0x0, ps3 = 0x3;
993 
994 	switch (event) {
995 	case SND_SOC_DAPM_POST_PMU:
996 		regmap_write(rt711->regmap,
997 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE28,
998 				RT711_SDCA_CTL_REQ_POWER_STATE, 0),
999 				ps0);
1000 		break;
1001 	case SND_SOC_DAPM_PRE_PMD:
1002 		regmap_write(rt711->regmap,
1003 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE28,
1004 				RT711_SDCA_CTL_REQ_POWER_STATE, 0),
1005 				ps3);
1006 		break;
1007 	}
1008 	return 0;
1009 }
1010 
1011 static int rt711_sdca_pde29_event(struct snd_soc_dapm_widget *w,
1012 	struct snd_kcontrol *kcontrol, int event)
1013 {
1014 	struct snd_soc_component *component =
1015 		snd_soc_dapm_to_component(w->dapm);
1016 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
1017 	unsigned char ps0 = 0x0, ps3 = 0x3;
1018 
1019 	switch (event) {
1020 	case SND_SOC_DAPM_POST_PMU:
1021 		regmap_write(rt711->regmap,
1022 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE29,
1023 				RT711_SDCA_CTL_REQ_POWER_STATE, 0),
1024 				ps0);
1025 		break;
1026 	case SND_SOC_DAPM_PRE_PMD:
1027 		regmap_write(rt711->regmap,
1028 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE29,
1029 				RT711_SDCA_CTL_REQ_POWER_STATE, 0),
1030 				ps3);
1031 		break;
1032 	}
1033 	return 0;
1034 }
1035 
1036 static int rt711_sdca_pde2a_event(struct snd_soc_dapm_widget *w,
1037 	struct snd_kcontrol *kcontrol, int event)
1038 {
1039 	struct snd_soc_component *component =
1040 		snd_soc_dapm_to_component(w->dapm);
1041 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
1042 	unsigned char ps0 = 0x0, ps3 = 0x3;
1043 
1044 	switch (event) {
1045 	case SND_SOC_DAPM_POST_PMU:
1046 		regmap_write(rt711->regmap,
1047 			SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PDE2A,
1048 				RT711_SDCA_CTL_REQ_POWER_STATE, 0),
1049 				ps0);
1050 		break;
1051 	case SND_SOC_DAPM_PRE_PMD:
1052 		regmap_write(rt711->regmap,
1053 			SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PDE2A,
1054 				RT711_SDCA_CTL_REQ_POWER_STATE, 0),
1055 				ps3);
1056 		break;
1057 	}
1058 	return 0;
1059 }
1060 
1061 static int rt711_sdca_line1_power_event(struct snd_soc_dapm_widget *w,
1062 			struct snd_kcontrol *kcontrol, int event)
1063 {
1064 	struct snd_soc_component *component =
1065 		snd_soc_dapm_to_component(w->dapm);
1066 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
1067 	static unsigned int sel_mode = 0xffff;
1068 
1069 	switch (event) {
1070 	case SND_SOC_DAPM_POST_PMU:
1071 		regmap_read(rt711->regmap,
1072 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49,
1073 				RT711_SDCA_CTL_SELECTED_MODE, 0),
1074 				&sel_mode);
1075 		regmap_write(rt711->regmap,
1076 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE1,
1077 				RT711_SDCA_CTL_VENDOR_DEF, 0),
1078 				0x1);
1079 		regmap_write(rt711->regmap,
1080 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49,
1081 				RT711_SDCA_CTL_SELECTED_MODE, 0),
1082 				0x7);
1083 		break;
1084 	case SND_SOC_DAPM_PRE_PMD:
1085 		regmap_write(rt711->regmap,
1086 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE1,
1087 				RT711_SDCA_CTL_VENDOR_DEF, 0),
1088 				0x0);
1089 		if (sel_mode != 0xffff)
1090 			regmap_write(rt711->regmap,
1091 				SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49,
1092 				RT711_SDCA_CTL_SELECTED_MODE, 0),
1093 				sel_mode);
1094 		break;
1095 	}
1096 
1097 	return 0;
1098 }
1099 
1100 static int rt711_sdca_line2_power_event(struct snd_soc_dapm_widget *w,
1101 			struct snd_kcontrol *kcontrol, int event)
1102 {
1103 	struct snd_soc_component *component =
1104 		snd_soc_dapm_to_component(w->dapm);
1105 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
1106 	unsigned char ps0 = 0x0, ps3 = 0x3;
1107 
1108 	switch (event) {
1109 	case SND_SOC_DAPM_POST_PMU:
1110 		regmap_write(rt711->regmap,
1111 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDELINE2,
1112 				RT711_SDCA_CTL_REQ_POWER_STATE, 0),
1113 				ps0);
1114 		regmap_write(rt711->regmap,
1115 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE2,
1116 				RT711_SDCA_CTL_VENDOR_DEF, 0),
1117 				0x1);
1118 		break;
1119 	case SND_SOC_DAPM_PRE_PMD:
1120 		regmap_write(rt711->regmap,
1121 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE2,
1122 				RT711_SDCA_CTL_VENDOR_DEF, 0),
1123 				0x0);
1124 		regmap_write(rt711->regmap,
1125 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDELINE2,
1126 				RT711_SDCA_CTL_REQ_POWER_STATE, 0),
1127 				ps3);
1128 		break;
1129 	}
1130 
1131 	return 0;
1132 }
1133 
1134 static const struct snd_soc_dapm_widget rt711_sdca_dapm_widgets[] = {
1135 	SND_SOC_DAPM_OUTPUT("HP"),
1136 	SND_SOC_DAPM_INPUT("MIC2"),
1137 	SND_SOC_DAPM_INPUT("DMIC1"),
1138 	SND_SOC_DAPM_INPUT("DMIC2"),
1139 	SND_SOC_DAPM_INPUT("LINE1"),
1140 	SND_SOC_DAPM_INPUT("LINE2"),
1141 
1142 	SND_SOC_DAPM_PGA_E("LINE1 Power", SND_SOC_NOPM,
1143 		0, 0, NULL, 0, rt711_sdca_line1_power_event,
1144 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1145 	SND_SOC_DAPM_PGA_E("LINE2 Power", SND_SOC_NOPM,
1146 		0, 0, NULL, 0, rt711_sdca_line2_power_event,
1147 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1148 
1149 	SND_SOC_DAPM_SUPPLY("PDE 28", SND_SOC_NOPM, 0, 0,
1150 		rt711_sdca_pde28_event,
1151 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1152 	SND_SOC_DAPM_SUPPLY("PDE 29", SND_SOC_NOPM, 0, 0,
1153 		rt711_sdca_pde29_event,
1154 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1155 	SND_SOC_DAPM_SUPPLY("PDE 2A", SND_SOC_NOPM, 0, 0,
1156 		rt711_sdca_pde2a_event,
1157 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1158 
1159 	SND_SOC_DAPM_DAC_E("FU 05", NULL, SND_SOC_NOPM, 0, 0,
1160 		rt711_sdca_fu05_event,
1161 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1162 	SND_SOC_DAPM_ADC_E("FU 0F", NULL, SND_SOC_NOPM, 0, 0,
1163 		rt711_sdca_fu0f_event,
1164 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1165 	SND_SOC_DAPM_ADC_E("FU 1E", NULL, SND_SOC_NOPM, 0, 0,
1166 		rt711_sdca_fu1e_event,
1167 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1168 	SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0,
1169 		&rt711_sdca_adc22_mux),
1170 	SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0,
1171 		&rt711_sdca_adc23_mux),
1172 
1173 	SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0),
1174 	SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0),
1175 	SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0),
1176 };
1177 
1178 static const struct snd_soc_dapm_route rt711_sdca_audio_map[] = {
1179 	{"FU 05", NULL, "DP3RX"},
1180 	{"DP2TX", NULL, "FU 0F"},
1181 	{"DP4TX", NULL, "FU 1E"},
1182 
1183 	{"LINE1 Power", NULL, "LINE1"},
1184 	{"LINE2 Power", NULL, "LINE2"},
1185 	{"HP", NULL, "PDE 28"},
1186 	{"FU 0F", NULL, "PDE 29"},
1187 	{"FU 1E", NULL, "PDE 2A"},
1188 
1189 	{"FU 0F", NULL, "ADC 22 Mux"},
1190 	{"FU 1E", NULL, "ADC 23 Mux"},
1191 	{"ADC 22 Mux", "DMIC", "DMIC1"},
1192 	{"ADC 22 Mux", "LINE1", "LINE1 Power"},
1193 	{"ADC 22 Mux", "LINE2", "LINE2 Power"},
1194 	{"ADC 22 Mux", "MIC2", "MIC2"},
1195 	{"ADC 23 Mux", "DMIC", "DMIC2"},
1196 	{"ADC 23 Mux", "LINE1", "LINE1 Power"},
1197 	{"ADC 23 Mux", "LINE2", "LINE2 Power"},
1198 	{"ADC 23 Mux", "MIC2", "MIC2"},
1199 
1200 	{"HP", NULL, "FU 05"},
1201 };
1202 
1203 static int rt711_sdca_parse_dt(struct rt711_sdca_priv *rt711, struct device *dev)
1204 {
1205 	device_property_read_u32(dev, "realtek,jd-src", &rt711->jd_src);
1206 
1207 	return 0;
1208 }
1209 
1210 static int rt711_sdca_probe(struct snd_soc_component *component)
1211 {
1212 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
1213 	int ret;
1214 
1215 	rt711_sdca_parse_dt(rt711, &rt711->slave->dev);
1216 	rt711->component = component;
1217 
1218 	ret = pm_runtime_resume(component->dev);
1219 	if (ret < 0 && ret != -EACCES)
1220 		return ret;
1221 
1222 	return 0;
1223 }
1224 
1225 static const struct snd_soc_component_driver soc_sdca_dev_rt711 = {
1226 	.probe = rt711_sdca_probe,
1227 	.controls = rt711_sdca_snd_controls,
1228 	.num_controls = ARRAY_SIZE(rt711_sdca_snd_controls),
1229 	.dapm_widgets = rt711_sdca_dapm_widgets,
1230 	.num_dapm_widgets = ARRAY_SIZE(rt711_sdca_dapm_widgets),
1231 	.dapm_routes = rt711_sdca_audio_map,
1232 	.num_dapm_routes = ARRAY_SIZE(rt711_sdca_audio_map),
1233 	.set_jack = rt711_sdca_set_jack_detect,
1234 	.endianness = 1,
1235 };
1236 
1237 static int rt711_sdca_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
1238 				int direction)
1239 {
1240 	struct sdw_stream_data *stream;
1241 
1242 	if (!sdw_stream)
1243 		return 0;
1244 
1245 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
1246 	if (!stream)
1247 		return -ENOMEM;
1248 
1249 	stream->sdw_stream = sdw_stream;
1250 
1251 	/* Use tx_mask or rx_mask to configure stream tag and set dma_data */
1252 	snd_soc_dai_dma_data_set(dai, direction, stream);
1253 
1254 	return 0;
1255 }
1256 
1257 static void rt711_sdca_shutdown(struct snd_pcm_substream *substream,
1258 				struct snd_soc_dai *dai)
1259 {
1260 	struct sdw_stream_data *stream;
1261 
1262 	stream = snd_soc_dai_get_dma_data(dai, substream);
1263 	snd_soc_dai_set_dma_data(dai, substream, NULL);
1264 	kfree(stream);
1265 }
1266 
1267 static int rt711_sdca_pcm_hw_params(struct snd_pcm_substream *substream,
1268 				struct snd_pcm_hw_params *params,
1269 				struct snd_soc_dai *dai)
1270 {
1271 	struct snd_soc_component *component = dai->component;
1272 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
1273 	struct sdw_stream_config stream_config = {0};
1274 	struct sdw_port_config port_config = {0};
1275 	struct sdw_stream_data *stream;
1276 	int retval;
1277 	unsigned int sampling_rate;
1278 
1279 	dev_dbg(dai->dev, "%s %s", __func__, dai->name);
1280 	stream = snd_soc_dai_get_dma_data(dai, substream);
1281 
1282 	if (!stream)
1283 		return -EINVAL;
1284 
1285 	if (!rt711->slave)
1286 		return -EINVAL;
1287 
1288 	/* SoundWire specific configuration */
1289 	snd_sdw_params_to_config(substream, params, &stream_config, &port_config);
1290 
1291 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1292 		port_config.num = 3;
1293 	} else {
1294 		if (dai->id == RT711_AIF1)
1295 			port_config.num = 2;
1296 		else if (dai->id == RT711_AIF2)
1297 			port_config.num = 4;
1298 		else
1299 			return -EINVAL;
1300 	}
1301 
1302 	retval = sdw_stream_add_slave(rt711->slave, &stream_config,
1303 					&port_config, 1, stream->sdw_stream);
1304 	if (retval) {
1305 		dev_err(dai->dev, "Unable to configure port\n");
1306 		return retval;
1307 	}
1308 
1309 	if (params_channels(params) > 16) {
1310 		dev_err(component->dev, "Unsupported channels %d\n",
1311 			params_channels(params));
1312 		return -EINVAL;
1313 	}
1314 
1315 	/* sampling rate configuration */
1316 	switch (params_rate(params)) {
1317 	case 44100:
1318 		sampling_rate = RT711_SDCA_RATE_44100HZ;
1319 		break;
1320 	case 48000:
1321 		sampling_rate = RT711_SDCA_RATE_48000HZ;
1322 		break;
1323 	case 96000:
1324 		sampling_rate = RT711_SDCA_RATE_96000HZ;
1325 		break;
1326 	case 192000:
1327 		sampling_rate = RT711_SDCA_RATE_192000HZ;
1328 		break;
1329 	default:
1330 		dev_err(component->dev, "Rate %d is not supported\n",
1331 			params_rate(params));
1332 		return -EINVAL;
1333 	}
1334 
1335 	/* set sampling frequency */
1336 	regmap_write(rt711->regmap,
1337 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_CS01, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1338 		sampling_rate);
1339 	regmap_write(rt711->regmap,
1340 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_CS11, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1341 		sampling_rate);
1342 	regmap_write(rt711->regmap,
1343 		SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_CS1F, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1344 		sampling_rate);
1345 
1346 	return 0;
1347 }
1348 
1349 static int rt711_sdca_pcm_hw_free(struct snd_pcm_substream *substream,
1350 				struct snd_soc_dai *dai)
1351 {
1352 	struct snd_soc_component *component = dai->component;
1353 	struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
1354 	struct sdw_stream_data *stream =
1355 		snd_soc_dai_get_dma_data(dai, substream);
1356 
1357 	if (!rt711->slave)
1358 		return -EINVAL;
1359 
1360 	sdw_stream_remove_slave(rt711->slave, stream->sdw_stream);
1361 	return 0;
1362 }
1363 
1364 #define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
1365 			SNDRV_PCM_RATE_192000)
1366 #define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1367 			SNDRV_PCM_FMTBIT_S24_LE)
1368 
1369 static const struct snd_soc_dai_ops rt711_sdca_ops = {
1370 	.hw_params	= rt711_sdca_pcm_hw_params,
1371 	.hw_free	= rt711_sdca_pcm_hw_free,
1372 	.set_stream	= rt711_sdca_set_sdw_stream,
1373 	.shutdown	= rt711_sdca_shutdown,
1374 };
1375 
1376 static struct snd_soc_dai_driver rt711_sdca_dai[] = {
1377 	{
1378 		.name = "rt711-sdca-aif1",
1379 		.id = RT711_AIF1,
1380 		.playback = {
1381 			.stream_name = "DP3 Playback",
1382 			.channels_min = 1,
1383 			.channels_max = 2,
1384 			.rates = RT711_STEREO_RATES,
1385 			.formats = RT711_FORMATS,
1386 		},
1387 		.capture = {
1388 			.stream_name = "DP2 Capture",
1389 			.channels_min = 1,
1390 			.channels_max = 2,
1391 			.rates = RT711_STEREO_RATES,
1392 			.formats = RT711_FORMATS,
1393 		},
1394 		.ops = &rt711_sdca_ops,
1395 	},
1396 	{
1397 		.name = "rt711-sdca-aif2",
1398 		.id = RT711_AIF2,
1399 		.capture = {
1400 			.stream_name = "DP4 Capture",
1401 			.channels_min = 1,
1402 			.channels_max = 2,
1403 			.rates = RT711_STEREO_RATES,
1404 			.formats = RT711_FORMATS,
1405 		},
1406 		.ops = &rt711_sdca_ops,
1407 	}
1408 };
1409 
1410 int rt711_sdca_init(struct device *dev, struct regmap *regmap,
1411 			struct regmap *mbq_regmap, struct sdw_slave *slave)
1412 {
1413 	struct rt711_sdca_priv *rt711;
1414 	int ret;
1415 
1416 	rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL);
1417 	if (!rt711)
1418 		return -ENOMEM;
1419 
1420 	dev_set_drvdata(dev, rt711);
1421 	rt711->slave = slave;
1422 	rt711->regmap = regmap;
1423 	rt711->mbq_regmap = mbq_regmap;
1424 
1425 	mutex_init(&rt711->calibrate_mutex);
1426 	mutex_init(&rt711->disable_irq_lock);
1427 
1428 	INIT_DELAYED_WORK(&rt711->jack_detect_work, rt711_sdca_jack_detect_handler);
1429 	INIT_DELAYED_WORK(&rt711->jack_btn_check_work, rt711_sdca_btn_check_handler);
1430 
1431 	/*
1432 	 * Mark hw_init to false
1433 	 * HW init will be performed when device reports present
1434 	 */
1435 	rt711->hw_init = false;
1436 	rt711->first_hw_init = false;
1437 	rt711->fu0f_dapm_mute = true;
1438 	rt711->fu1e_dapm_mute = true;
1439 	rt711->fu0f_mixer_l_mute = rt711->fu0f_mixer_r_mute = true;
1440 	rt711->fu1e_mixer_l_mute = rt711->fu1e_mixer_r_mute = true;
1441 
1442 	/* JD source uses JD2 in default */
1443 	rt711->jd_src = RT711_JD2;
1444 
1445 	ret =  devm_snd_soc_register_component(dev,
1446 			&soc_sdca_dev_rt711,
1447 			rt711_sdca_dai,
1448 			ARRAY_SIZE(rt711_sdca_dai));
1449 
1450 	dev_dbg(&slave->dev, "%s\n", __func__);
1451 
1452 	return ret;
1453 }
1454 
1455 static void rt711_sdca_vd0_io_init(struct rt711_sdca_priv *rt711)
1456 {
1457 	rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
1458 		RT711_GPIO_TEST_MODE_CTL2, 0x0e00);
1459 	rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL,
1460 		RT711_HDA_LEGACY_GPIO_CTL, 0x0008);
1461 
1462 	regmap_write(rt711->regmap, 0x2f5a, 0x01);
1463 
1464 	rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
1465 		RT711_ADC27_VOL_SET, 0x8728);
1466 
1467 	rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
1468 		RT711_COMBO_JACK_AUTO_CTL3, 0xa472);
1469 
1470 	regmap_write(rt711->regmap, 0x2f50, 0x02);
1471 
1472 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_ANALOG_CTL,
1473 		RT711_MISC_POWER_CTL4, 0x6000, 0x6000);
1474 
1475 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
1476 		RT711_COMBO_JACK_AUTO_CTL3, 0x000c, 0x000c);
1477 
1478 	rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL,
1479 		RT711_HDA_LEGACY_CONFIG_CTL, 0x0000);
1480 
1481 	rt711_sdca_index_write(rt711, RT711_VENDOR_VAD,
1482 		RT711_VAD_SRAM_CTL1, 0x0050);
1483 }
1484 
1485 static void rt711_sdca_vd1_io_init(struct rt711_sdca_priv *rt711)
1486 {
1487 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL,
1488 		RT711_HDA_LEGACY_UNSOLICITED_CTL, 0x0300, 0x0000);
1489 
1490 	rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
1491 		RT711_COMBO_JACK_AUTO_CTL3, 0xa43e);
1492 
1493 	regmap_write(rt711->regmap, 0x2f5a, 0x05);
1494 
1495 	rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
1496 		RT711_JD_CTRL6, 0x0500);
1497 
1498 	rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
1499 		RT711_DMIC_CTL1, 0x6173);
1500 
1501 	rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL,
1502 		RT711_HDA_LEGACY_CONFIG_CTL, 0x0000);
1503 
1504 	rt711_sdca_index_write(rt711, RT711_VENDOR_VAD,
1505 		RT711_VAD_SRAM_CTL1, 0x0050);
1506 }
1507 
1508 int rt711_sdca_io_init(struct device *dev, struct sdw_slave *slave)
1509 {
1510 	struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev);
1511 	int ret = 0;
1512 	unsigned int val;
1513 
1514 	rt711->disable_irq = false;
1515 
1516 	if (rt711->hw_init)
1517 		return 0;
1518 
1519 	if (rt711->first_hw_init) {
1520 		regcache_cache_only(rt711->regmap, false);
1521 		regcache_cache_bypass(rt711->regmap, true);
1522 		regcache_cache_only(rt711->mbq_regmap, false);
1523 		regcache_cache_bypass(rt711->mbq_regmap, true);
1524 	} else {
1525 		/*
1526 		 * PM runtime is only enabled when a Slave reports as Attached
1527 		 */
1528 
1529 		/* set autosuspend parameters */
1530 		pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
1531 		pm_runtime_use_autosuspend(&slave->dev);
1532 
1533 		/* update count of parent 'active' children */
1534 		pm_runtime_set_active(&slave->dev);
1535 
1536 		/* make sure the device does not suspend immediately */
1537 		pm_runtime_mark_last_busy(&slave->dev);
1538 
1539 		pm_runtime_enable(&slave->dev);
1540 	}
1541 
1542 	pm_runtime_get_noresume(&slave->dev);
1543 
1544 	rt711_sdca_reset(rt711);
1545 
1546 	rt711_sdca_index_read(rt711, RT711_VENDOR_REG, RT711_JD_PRODUCT_NUM, &val);
1547 	rt711->hw_ver = val & 0xf;
1548 
1549 	if (rt711->hw_ver == RT711_VER_VD0)
1550 		rt711_sdca_vd0_io_init(rt711);
1551 	else
1552 		rt711_sdca_vd1_io_init(rt711);
1553 
1554 	/* DP4 mux select from 08_filter_Out_pri */
1555 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
1556 		RT711_FILTER_SRC_SEL, 0x1800, 0x0800);
1557 
1558 	/* ge_exclusive_inbox_en disable */
1559 	rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL,
1560 		RT711_PUSH_BTN_INT_CTL0, 0x20, 0x00);
1561 
1562 	/* calibration */
1563 	ret = rt711_sdca_calibration(rt711);
1564 	if (ret < 0)
1565 		dev_err(dev, "%s, calibration failed!\n", __func__);
1566 
1567 	/* HP output enable */
1568 	regmap_write(rt711->regmap,
1569 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_OT1, RT711_SDCA_CTL_VENDOR_DEF, 0), 0x4);
1570 
1571 	/*
1572 	 * if set_jack callback occurred early than io_init,
1573 	 * we set up the jack detection function now
1574 	 */
1575 	if (rt711->hs_jack)
1576 		rt711_sdca_jack_init(rt711);
1577 
1578 	if (rt711->first_hw_init) {
1579 		regcache_cache_bypass(rt711->regmap, false);
1580 		regcache_mark_dirty(rt711->regmap);
1581 		regcache_cache_bypass(rt711->mbq_regmap, false);
1582 		regcache_mark_dirty(rt711->mbq_regmap);
1583 	} else
1584 		rt711->first_hw_init = true;
1585 
1586 	/* Mark Slave initialization complete */
1587 	rt711->hw_init = true;
1588 
1589 	pm_runtime_mark_last_busy(&slave->dev);
1590 	pm_runtime_put_autosuspend(&slave->dev);
1591 
1592 	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
1593 	return 0;
1594 }
1595 
1596 MODULE_DESCRIPTION("ASoC RT711 SDCA SDW driver");
1597 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1598 MODULE_LICENSE("GPL");
1599