xref: /linux/sound/soc/codecs/rt712-sdca.c (revision 7f4f3b14e8079ecde096bd734af10e30d40c27b7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // rt712-sdca.c -- rt712 SDCA ALSA SoC audio driver
4 //
5 // Copyright(c) 2023 Realtek Semiconductor Corp.
6 //
7 //
8 
9 #include <linux/bitops.h>
10 #include <sound/core.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <sound/initval.h>
14 #include <sound/jack.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/pm_runtime.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/sdca.h>
22 #include <linux/soundwire/sdw_registers.h>
23 #include <linux/slab.h>
24 #include <sound/soc-dapm.h>
25 #include <sound/tlv.h>
26 #include "rt712-sdca.h"
27 
28 static int rt712_sdca_index_write(struct rt712_sdca_priv *rt712,
29 		unsigned int nid, unsigned int reg, unsigned int value)
30 {
31 	int ret;
32 	struct regmap *regmap = rt712->mbq_regmap;
33 	unsigned int addr = (nid << 20) | reg;
34 
35 	ret = regmap_write(regmap, addr, value);
36 	if (ret < 0)
37 		dev_err(&rt712->slave->dev,
38 			"%s: Failed to set private value: %06x <= %04x ret=%d\n",
39 			__func__, addr, value, ret);
40 
41 	return ret;
42 }
43 
44 static int rt712_sdca_index_read(struct rt712_sdca_priv *rt712,
45 		unsigned int nid, unsigned int reg, unsigned int *value)
46 {
47 	int ret;
48 	struct regmap *regmap = rt712->mbq_regmap;
49 	unsigned int addr = (nid << 20) | reg;
50 
51 	ret = regmap_read(regmap, addr, value);
52 	if (ret < 0)
53 		dev_err(&rt712->slave->dev,
54 			"%s: Failed to get private value: %06x => %04x ret=%d\n",
55 			__func__, addr, *value, ret);
56 
57 	return ret;
58 }
59 
60 static int rt712_sdca_index_update_bits(struct rt712_sdca_priv *rt712,
61 	unsigned int nid, unsigned int reg, unsigned int mask, unsigned int val)
62 {
63 	unsigned int tmp;
64 	int ret;
65 
66 	ret = rt712_sdca_index_read(rt712, nid, reg, &tmp);
67 	if (ret < 0)
68 		return ret;
69 
70 	set_mask_bits(&tmp, mask, val);
71 	return rt712_sdca_index_write(rt712, nid, reg, tmp);
72 }
73 
74 static int rt712_sdca_calibration(struct rt712_sdca_priv *rt712)
75 {
76 	unsigned int val, loop_rc = 0, loop_dc = 0;
77 	struct device *dev;
78 	struct regmap *regmap = rt712->regmap;
79 	int chk_cnt = 100;
80 	int ret = 0;
81 
82 	mutex_lock(&rt712->calibrate_mutex);
83 	dev = regmap_get_device(regmap);
84 
85 	/* Set HP-JD source from JD1 */
86 	if (rt712->version_id == RT712_VA)
87 		rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_CC_DET1, 0x043a);
88 
89 	/* FSM switch to calibration manual mode */
90 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_FSM_CTL, 0x4100);
91 
92 	/* Calibration setting */
93 	rt712_sdca_index_write(rt712, RT712_VENDOR_CALI, RT712_DAC_DC_CALI_CTL1, 0x7883);
94 
95 	/* W1C Trigger DC calibration (HP & Class-D) */
96 	rt712_sdca_index_write(rt712, RT712_VENDOR_CALI, RT712_DAC_DC_CALI_CTL1, 0xf893);
97 
98 	/* wait for calibration process */
99 	rt712_sdca_index_read(rt712, RT712_VENDOR_CALI,
100 		RT712_DAC_DC_CALI_CTL1, &val);
101 
102 	for (loop_dc = 0; loop_dc < chk_cnt &&
103 		(val & RT712_DAC_DC_CALI_TRIGGER); loop_dc++) {
104 		usleep_range(10000, 11000);
105 		ret = rt712_sdca_index_read(rt712, RT712_VENDOR_CALI,
106 			RT712_DAC_DC_CALI_CTL1, &val);
107 		if (ret < 0)
108 			goto _cali_fail_;
109 	}
110 	if (loop_dc == chk_cnt)
111 		dev_err(dev, "%s, calibration time-out!\n", __func__);
112 
113 	if (loop_dc == chk_cnt || loop_rc == chk_cnt)
114 		ret = -ETIMEDOUT;
115 
116 _cali_fail_:
117 	/* Enable Rldet in FSM */
118 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_FSM_CTL, 0x4500);
119 
120 	/* Sensing Lch+Rch */
121 	rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_IMS_DIGITAL_CTL1, 0x040f);
122 
123 	/* Sine gen path control */
124 	rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_IMS_DIGITAL_CTL5, 0x0000);
125 
126 	/* Release HP-JD, EN_CBJ_TIE_GL/R open, en_osw gating auto done bit */
127 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_DIGITAL_MISC_CTRL4, 0x0010);
128 
129 	mutex_unlock(&rt712->calibrate_mutex);
130 	dev_dbg(dev, "%s calibration complete, ret=%d\n", __func__, ret);
131 	return ret;
132 }
133 
134 static unsigned int rt712_sdca_button_detect(struct rt712_sdca_priv *rt712)
135 {
136 	unsigned int btn_type = 0, offset, idx, val, owner;
137 	int ret;
138 	unsigned char buf[3];
139 
140 	/* get current UMP message owner */
141 	ret = regmap_read(rt712->regmap,
142 		SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01, RT712_SDCA_CTL_HIDTX_CURRENT_OWNER, 0),
143 		&owner);
144 	if (ret < 0)
145 		return 0;
146 
147 	/* if owner is device then there is no button event from device */
148 	if (owner == 1)
149 		return 0;
150 
151 	/* read UMP message offset */
152 	ret = regmap_read(rt712->regmap,
153 		SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01, RT712_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0),
154 		&offset);
155 	if (ret < 0)
156 		goto _end_btn_det_;
157 
158 	for (idx = 0; idx < sizeof(buf); idx++) {
159 		ret = regmap_read(rt712->regmap,
160 			RT712_BUF_ADDR_HID1 + offset + idx, &val);
161 		if (ret < 0)
162 			goto _end_btn_det_;
163 		buf[idx] = val & 0xff;
164 	}
165 
166 	if (buf[0] == 0x11) {
167 		switch (buf[1] & 0xf0) {
168 		case 0x10:
169 			btn_type |= SND_JACK_BTN_2;
170 			break;
171 		case 0x20:
172 			btn_type |= SND_JACK_BTN_3;
173 			break;
174 		case 0x40:
175 			btn_type |= SND_JACK_BTN_0;
176 			break;
177 		case 0x80:
178 			btn_type |= SND_JACK_BTN_1;
179 			break;
180 		}
181 		switch (buf[2]) {
182 		case 0x01:
183 		case 0x10:
184 			btn_type |= SND_JACK_BTN_2;
185 			break;
186 		case 0x02:
187 		case 0x20:
188 			btn_type |= SND_JACK_BTN_3;
189 			break;
190 		case 0x04:
191 		case 0x40:
192 			btn_type |= SND_JACK_BTN_0;
193 			break;
194 		case 0x08:
195 		case 0x80:
196 			btn_type |= SND_JACK_BTN_1;
197 			break;
198 		}
199 	}
200 
201 _end_btn_det_:
202 	/* Host is owner, so set back to device */
203 	if (owner == 0) {
204 		/* set owner to device */
205 		if (rt712->version_id == RT712_VA)
206 			regmap_write(rt712->regmap,
207 				SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01,
208 					RT712_SDCA_CTL_HIDTX_SET_OWNER_TO_DEVICE, 0), 0x01);
209 		else
210 			regmap_write(rt712->regmap,
211 				SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01,
212 					RT712_SDCA_CTL_HIDTX_CURRENT_OWNER, 0), 0x01);
213 	}
214 
215 	return btn_type;
216 }
217 
218 static int rt712_sdca_headset_detect(struct rt712_sdca_priv *rt712)
219 {
220 	unsigned int det_mode;
221 	int ret;
222 
223 	/* get detected_mode */
224 	ret = regmap_read(rt712->regmap,
225 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_GE49, RT712_SDCA_CTL_DETECTED_MODE, 0),
226 		&det_mode);
227 	if (ret < 0)
228 		goto io_error;
229 
230 	switch (det_mode) {
231 	case 0x00:
232 		rt712->jack_type = 0;
233 		break;
234 	case 0x03:
235 		rt712->jack_type = SND_JACK_HEADPHONE;
236 		break;
237 	case 0x05:
238 		rt712->jack_type = SND_JACK_HEADSET;
239 		break;
240 	}
241 
242 	/* write selected_mode */
243 	if (det_mode) {
244 		ret = regmap_write(rt712->regmap,
245 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_GE49, RT712_SDCA_CTL_SELECTED_MODE, 0),
246 			det_mode);
247 		if (ret < 0)
248 			goto io_error;
249 	}
250 
251 	dev_dbg(&rt712->slave->dev,
252 		"%s, detected_mode=0x%x\n", __func__, det_mode);
253 
254 	return 0;
255 
256 io_error:
257 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
258 	return ret;
259 }
260 
261 static void rt712_sdca_jack_detect_handler(struct work_struct *work)
262 {
263 	struct rt712_sdca_priv *rt712 =
264 		container_of(work, struct rt712_sdca_priv, jack_detect_work.work);
265 	int btn_type = 0, ret;
266 
267 	if (!rt712->hs_jack)
268 		return;
269 
270 	if (!rt712->component->card || !rt712->component->card->instantiated)
271 		return;
272 
273 	/* SDW_SCP_SDCA_INT_SDCA_0 is used for jack detection */
274 	if (rt712->scp_sdca_stat1 & SDW_SCP_SDCA_INT_SDCA_0) {
275 		ret = rt712_sdca_headset_detect(rt712);
276 		if (ret < 0)
277 			return;
278 	}
279 
280 	/* SDW_SCP_SDCA_INT_SDCA_8 is used for button detection */
281 	if (rt712->scp_sdca_stat2 & SDW_SCP_SDCA_INT_SDCA_8)
282 		btn_type = rt712_sdca_button_detect(rt712);
283 
284 	if (rt712->jack_type == 0)
285 		btn_type = 0;
286 
287 	dev_dbg(&rt712->slave->dev,
288 		"in %s, jack_type=0x%x\n", __func__, rt712->jack_type);
289 	dev_dbg(&rt712->slave->dev,
290 		"in %s, btn_type=0x%x\n", __func__, btn_type);
291 	dev_dbg(&rt712->slave->dev,
292 		"in %s, scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__,
293 		rt712->scp_sdca_stat1, rt712->scp_sdca_stat2);
294 
295 	snd_soc_jack_report(rt712->hs_jack, rt712->jack_type | btn_type,
296 			SND_JACK_HEADSET |
297 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
298 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
299 
300 	if (btn_type) {
301 		/* button released */
302 		snd_soc_jack_report(rt712->hs_jack, rt712->jack_type,
303 			SND_JACK_HEADSET |
304 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
305 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
306 
307 		mod_delayed_work(system_power_efficient_wq,
308 			&rt712->jack_btn_check_work, msecs_to_jiffies(200));
309 	}
310 }
311 
312 static void rt712_sdca_btn_check_handler(struct work_struct *work)
313 {
314 	struct rt712_sdca_priv *rt712 =
315 		container_of(work, struct rt712_sdca_priv, jack_btn_check_work.work);
316 	int btn_type = 0, ret, idx;
317 	unsigned int det_mode, offset, val;
318 	unsigned char buf[3];
319 
320 	ret = regmap_read(rt712->regmap,
321 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_GE49, RT712_SDCA_CTL_DETECTED_MODE, 0),
322 		&det_mode);
323 	if (ret < 0)
324 		goto io_error;
325 
326 	/* pin attached */
327 	if (det_mode) {
328 		/* read UMP message offset */
329 		ret = regmap_read(rt712->regmap,
330 			SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01, RT712_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0),
331 			&offset);
332 		if (ret < 0)
333 			goto io_error;
334 
335 		for (idx = 0; idx < sizeof(buf); idx++) {
336 			ret = regmap_read(rt712->regmap,
337 				RT712_BUF_ADDR_HID1 + offset + idx, &val);
338 			if (ret < 0)
339 				goto io_error;
340 			buf[idx] = val & 0xff;
341 		}
342 
343 		if (buf[0] == 0x11) {
344 			switch (buf[1] & 0xf0) {
345 			case 0x10:
346 				btn_type |= SND_JACK_BTN_2;
347 				break;
348 			case 0x20:
349 				btn_type |= SND_JACK_BTN_3;
350 				break;
351 			case 0x40:
352 				btn_type |= SND_JACK_BTN_0;
353 				break;
354 			case 0x80:
355 				btn_type |= SND_JACK_BTN_1;
356 				break;
357 			}
358 			switch (buf[2]) {
359 			case 0x01:
360 			case 0x10:
361 				btn_type |= SND_JACK_BTN_2;
362 				break;
363 			case 0x02:
364 			case 0x20:
365 				btn_type |= SND_JACK_BTN_3;
366 				break;
367 			case 0x04:
368 			case 0x40:
369 				btn_type |= SND_JACK_BTN_0;
370 				break;
371 			case 0x08:
372 			case 0x80:
373 				btn_type |= SND_JACK_BTN_1;
374 				break;
375 			}
376 		}
377 	} else {
378 		rt712->jack_type = 0;
379 	}
380 
381 	dev_dbg(&rt712->slave->dev, "%s, btn_type=0x%x\n",	__func__, btn_type);
382 	snd_soc_jack_report(rt712->hs_jack, rt712->jack_type | btn_type,
383 			SND_JACK_HEADSET |
384 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
385 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
386 
387 	if (btn_type) {
388 		/* button released */
389 		snd_soc_jack_report(rt712->hs_jack, rt712->jack_type,
390 			SND_JACK_HEADSET |
391 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
392 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
393 
394 		mod_delayed_work(system_power_efficient_wq,
395 			&rt712->jack_btn_check_work, msecs_to_jiffies(200));
396 	}
397 
398 	return;
399 
400 io_error:
401 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
402 }
403 
404 static void rt712_sdca_jack_init(struct rt712_sdca_priv *rt712)
405 {
406 	mutex_lock(&rt712->calibrate_mutex);
407 
408 	if (rt712->hs_jack) {
409 		/* Enable HID1 event & set button RTC mode */
410 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
411 			RT712_UMP_HID_CTL5, 0xfff0);
412 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
413 			RT712_UMP_HID_CTL0, 0x1100, 0x1100);
414 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
415 			RT712_UMP_HID_CTL7, 0xf000, 0x0000);
416 
417 		/* detected_mode_change_event_en & hid1_push_button_event_en */
418 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
419 			RT712_GE_RELATED_CTL1, 0x0c00, 0x0c00);
420 		/* ge_inbox_en */
421 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
422 			RT712_GE_RELATED_CTL2, 0x0020, 0x0000);
423 
424 		switch (rt712->jd_src) {
425 		case RT712_JD1:
426 			/* Set HP-JD source from JD1, VB uses JD1 in default */
427 			if (rt712->version_id == RT712_VA)
428 				rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_CC_DET1, 0x043a);
429 			break;
430 		default:
431 			dev_warn(rt712->component->dev, "Wrong JD source\n");
432 			break;
433 		}
434 
435 		/* set SCP_SDCA_IntMask1[0]=1 */
436 		sdw_write_no_pm(rt712->slave, SDW_SCP_SDCA_INTMASK1, SDW_SCP_SDCA_INTMASK_SDCA_0);
437 		/* set SCP_SDCA_IntMask2[0]=1 */
438 		sdw_write_no_pm(rt712->slave, SDW_SCP_SDCA_INTMASK2, SDW_SCP_SDCA_INTMASK_SDCA_8);
439 		dev_dbg(&rt712->slave->dev, "in %s enable\n", __func__);
440 
441 		/* trigger GE interrupt */
442 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
443 			RT712_GE_RELATED_CTL1, 0x0080, 0x0080);
444 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
445 			RT712_GE_RELATED_CTL1, 0x0080, 0x0000);
446 	} else {
447 		/* disable HID1 & detected_mode_change event */
448 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
449 			RT712_GE_RELATED_CTL1, 0x0c00, 0x0000);
450 
451 		dev_dbg(&rt712->slave->dev, "in %s disable\n", __func__);
452 	}
453 
454 	mutex_unlock(&rt712->calibrate_mutex);
455 }
456 
457 static int rt712_sdca_set_jack_detect(struct snd_soc_component *component,
458 	struct snd_soc_jack *hs_jack, void *data)
459 {
460 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
461 	int ret;
462 
463 	rt712->hs_jack = hs_jack;
464 
465 	if (!rt712->first_hw_init)
466 		return 0;
467 
468 	ret = pm_runtime_resume_and_get(component->dev);
469 	if (ret < 0) {
470 		if (ret != -EACCES) {
471 			dev_err(component->dev, "%s: failed to resume %d\n", __func__, ret);
472 			return ret;
473 		}
474 
475 		/* pm_runtime not enabled yet */
476 		dev_dbg(component->dev,	"%s: skipping jack init for now\n", __func__);
477 		return 0;
478 	}
479 
480 	rt712_sdca_jack_init(rt712);
481 
482 	pm_runtime_mark_last_busy(component->dev);
483 	pm_runtime_put_autosuspend(component->dev);
484 
485 	return 0;
486 }
487 
488 /* For SDCA control DAC/ADC Gain */
489 static int rt712_sdca_set_gain_put(struct snd_kcontrol *kcontrol,
490 		struct snd_ctl_elem_value *ucontrol)
491 {
492 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
493 	struct soc_mixer_control *mc =
494 		(struct soc_mixer_control *)kcontrol->private_value;
495 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
496 	unsigned int read_l, read_r, gain_l_val, gain_r_val;
497 	unsigned int adc_vol_flag = 0;
498 	unsigned int lvalue, rvalue;
499 	const unsigned int interval_offset = 0xc0;
500 	const unsigned int tendB = 0xa00;
501 
502 	if (strstr(ucontrol->id.name, "FU0F Capture Volume"))
503 		adc_vol_flag = 1;
504 
505 	regmap_read(rt712->mbq_regmap, mc->reg, &lvalue);
506 	regmap_read(rt712->mbq_regmap, mc->rreg, &rvalue);
507 
508 	/* L Channel */
509 	gain_l_val = ucontrol->value.integer.value[0];
510 	if (gain_l_val > mc->max)
511 		gain_l_val = mc->max;
512 
513 	if (mc->shift == 8) /* boost gain */
514 		gain_l_val = gain_l_val * tendB;
515 	else {
516 		/* ADC/DAC gain */
517 		if (adc_vol_flag)
518 			gain_l_val = 0x1e00 - ((mc->max - gain_l_val) * interval_offset);
519 		else
520 			gain_l_val = 0 - ((mc->max - gain_l_val) * interval_offset);
521 		gain_l_val &= 0xffff;
522 	}
523 
524 	/* R Channel */
525 	gain_r_val = ucontrol->value.integer.value[1];
526 	if (gain_r_val > mc->max)
527 		gain_r_val = mc->max;
528 
529 	if (mc->shift == 8) /* boost gain */
530 		gain_r_val = gain_r_val * tendB;
531 	else {
532 		/* ADC/DAC gain */
533 		if (adc_vol_flag)
534 			gain_r_val = 0x1e00 - ((mc->max - gain_r_val) * interval_offset);
535 		else
536 			gain_r_val = 0 - ((mc->max - gain_r_val) * interval_offset);
537 		gain_r_val &= 0xffff;
538 	}
539 
540 	if (lvalue == gain_l_val && rvalue == gain_r_val)
541 		return 0;
542 
543 	/* Lch*/
544 	regmap_write(rt712->mbq_regmap, mc->reg, gain_l_val);
545 	/* Rch */
546 	regmap_write(rt712->mbq_regmap, mc->rreg, gain_r_val);
547 
548 	regmap_read(rt712->mbq_regmap, mc->reg, &read_l);
549 	regmap_read(rt712->mbq_regmap, mc->rreg, &read_r);
550 	if (read_r == gain_r_val && read_l == gain_l_val)
551 		return 1;
552 
553 	return -EIO;
554 }
555 
556 static int rt712_sdca_set_gain_get(struct snd_kcontrol *kcontrol,
557 		struct snd_ctl_elem_value *ucontrol)
558 {
559 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
560 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
561 	struct soc_mixer_control *mc =
562 		(struct soc_mixer_control *)kcontrol->private_value;
563 	unsigned int read_l, read_r, ctl_l = 0, ctl_r = 0;
564 	unsigned int adc_vol_flag = 0;
565 	const unsigned int interval_offset = 0xc0;
566 	const unsigned int tendB = 0xa00;
567 
568 	if (strstr(ucontrol->id.name, "FU0F Capture Volume"))
569 		adc_vol_flag = 1;
570 
571 	regmap_read(rt712->mbq_regmap, mc->reg, &read_l);
572 	regmap_read(rt712->mbq_regmap, mc->rreg, &read_r);
573 
574 	if (mc->shift == 8) /* boost gain */
575 		ctl_l = read_l / tendB;
576 	else {
577 		if (adc_vol_flag)
578 			ctl_l = mc->max - (((0x1e00 - read_l) & 0xffff) / interval_offset);
579 		else
580 			ctl_l = mc->max - (((0 - read_l) & 0xffff) / interval_offset);
581 	}
582 
583 	if (read_l != read_r) {
584 		if (mc->shift == 8) /* boost gain */
585 			ctl_r = read_r / tendB;
586 		else { /* ADC/DAC gain */
587 			if (adc_vol_flag)
588 				ctl_r = mc->max - (((0x1e00 - read_r) & 0xffff) / interval_offset);
589 			else
590 				ctl_r = mc->max - (((0 - read_r) & 0xffff) / interval_offset);
591 		}
592 	} else
593 		ctl_r = ctl_l;
594 
595 	ucontrol->value.integer.value[0] = ctl_l;
596 	ucontrol->value.integer.value[1] = ctl_r;
597 
598 	return 0;
599 }
600 
601 static int rt712_sdca_set_fu0f_capture_ctl(struct rt712_sdca_priv *rt712)
602 {
603 	int err;
604 	unsigned int ch_01, ch_02;
605 
606 	ch_01 = (rt712->fu0f_dapm_mute || rt712->fu0f_mixer_l_mute) ? 0x01 : 0x00;
607 	ch_02 = (rt712->fu0f_dapm_mute || rt712->fu0f_mixer_r_mute) ? 0x01 : 0x00;
608 
609 	err = regmap_write(rt712->regmap,
610 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F,
611 			RT712_SDCA_CTL_FU_MUTE, CH_01), ch_01);
612 	if (err < 0)
613 		return err;
614 
615 	err = regmap_write(rt712->regmap,
616 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F,
617 			RT712_SDCA_CTL_FU_MUTE, CH_02), ch_02);
618 	if (err < 0)
619 		return err;
620 
621 	return 0;
622 }
623 
624 static int rt712_sdca_fu0f_capture_get(struct snd_kcontrol *kcontrol,
625 			struct snd_ctl_elem_value *ucontrol)
626 {
627 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
628 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
629 
630 	ucontrol->value.integer.value[0] = !rt712->fu0f_mixer_l_mute;
631 	ucontrol->value.integer.value[1] = !rt712->fu0f_mixer_r_mute;
632 	return 0;
633 }
634 
635 static int rt712_sdca_fu0f_capture_put(struct snd_kcontrol *kcontrol,
636 			struct snd_ctl_elem_value *ucontrol)
637 {
638 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
639 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
640 	int err;
641 
642 	if (rt712->fu0f_mixer_l_mute == !ucontrol->value.integer.value[0] &&
643 		rt712->fu0f_mixer_r_mute == !ucontrol->value.integer.value[1])
644 		return 0;
645 
646 	rt712->fu0f_mixer_l_mute = !ucontrol->value.integer.value[0];
647 	rt712->fu0f_mixer_r_mute = !ucontrol->value.integer.value[1];
648 	err = rt712_sdca_set_fu0f_capture_ctl(rt712);
649 	if (err < 0)
650 		return err;
651 
652 	return 1;
653 }
654 
655 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0);
656 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, -1725, 75, 0);
657 static const DECLARE_TLV_DB_SCALE(boost_vol_tlv, 0, 1000, 0);
658 
659 static const struct snd_kcontrol_new rt712_sdca_controls[] = {
660 	SOC_DOUBLE_R_EXT_TLV("FU05 Playback Volume",
661 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05, RT712_SDCA_CTL_FU_VOLUME, CH_01),
662 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05, RT712_SDCA_CTL_FU_VOLUME, CH_02),
663 		0, 0x57, 0,
664 		rt712_sdca_set_gain_get, rt712_sdca_set_gain_put, out_vol_tlv),
665 	SOC_DOUBLE_EXT("FU0F Capture Switch", SND_SOC_NOPM, 0, 1, 1, 0,
666 		rt712_sdca_fu0f_capture_get, rt712_sdca_fu0f_capture_put),
667 	SOC_DOUBLE_R_EXT_TLV("FU0F Capture Volume",
668 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F, RT712_SDCA_CTL_FU_VOLUME, CH_01),
669 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F, RT712_SDCA_CTL_FU_VOLUME, CH_02),
670 		0, 0x3f, 0,
671 		rt712_sdca_set_gain_get, rt712_sdca_set_gain_put, mic_vol_tlv),
672 	SOC_DOUBLE_R_EXT_TLV("FU44 Boost Volume",
673 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PLATFORM_FU44, RT712_SDCA_CTL_FU_CH_GAIN, CH_01),
674 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PLATFORM_FU44, RT712_SDCA_CTL_FU_CH_GAIN, CH_02),
675 		8, 3, 0,
676 		rt712_sdca_set_gain_get, rt712_sdca_set_gain_put, boost_vol_tlv),
677 };
678 
679 static const struct snd_kcontrol_new rt712_sdca_spk_controls[] = {
680 	SOC_DOUBLE_R_EXT_TLV("FU06 Playback Volume",
681 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_VOLUME, CH_01),
682 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_VOLUME, CH_02),
683 		0, 0x57, 0,
684 		rt712_sdca_set_gain_get, rt712_sdca_set_gain_put, out_vol_tlv),
685 };
686 
687 static int rt712_sdca_mux_get(struct snd_kcontrol *kcontrol,
688 			struct snd_ctl_elem_value *ucontrol)
689 {
690 	struct snd_soc_component *component =
691 		snd_soc_dapm_kcontrol_component(kcontrol);
692 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
693 	unsigned int val = 0, mask = 0x3300;
694 
695 	rt712_sdca_index_read(rt712, RT712_VENDOR_HDA_CTL, RT712_MIXER_CTL1, &val);
696 
697 	val = val & mask;
698 	switch (val) {
699 	case 0x3000:
700 		val = 1;
701 		break;
702 	case 0x0300:
703 		val = 0;
704 		break;
705 	}
706 
707 	ucontrol->value.enumerated.item[0] = val;
708 
709 	return 0;
710 }
711 
712 static int rt712_sdca_mux_put(struct snd_kcontrol *kcontrol,
713 			struct snd_ctl_elem_value *ucontrol)
714 {
715 	struct snd_soc_component *component =
716 		snd_soc_dapm_kcontrol_component(kcontrol);
717 	struct snd_soc_dapm_context *dapm =
718 		snd_soc_dapm_kcontrol_dapm(kcontrol);
719 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
720 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
721 	unsigned int *item = ucontrol->value.enumerated.item;
722 	unsigned int mask_sft;
723 	unsigned int val;
724 
725 	if (item[0] >= e->items)
726 		return -EINVAL;
727 
728 	if (ucontrol->value.enumerated.item[0] == 0)
729 		mask_sft = 12;
730 	else if (ucontrol->value.enumerated.item[0] == 1)
731 		mask_sft = 8;
732 	else
733 		return -EINVAL;
734 
735 	rt712_sdca_index_read(rt712, RT712_VENDOR_HDA_CTL, RT712_MIXER_CTL1, &val);
736 	val = (val >> mask_sft) & 0x3;
737 	if (!val)
738 		return 0;
739 
740 	rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
741 		RT712_MIXER_CTL1, 0x3fff);
742 	rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
743 		RT712_MIXER_CTL1, 0x3 << mask_sft, 0);
744 
745 	snd_soc_dapm_mux_update_power(dapm, kcontrol,
746 		item[0], e, NULL);
747 
748 	return 1;
749 }
750 
751 static const char * const adc_mux_text[] = {
752 	"MIC2",
753 	"LINE2",
754 };
755 
756 static SOC_ENUM_SINGLE_DECL(
757 	rt712_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text);
758 
759 static const struct snd_kcontrol_new rt712_sdca_adc23_mux =
760 	SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt712_adc23_enum,
761 			rt712_sdca_mux_get, rt712_sdca_mux_put);
762 
763 static int rt712_sdca_fu05_event(struct snd_soc_dapm_widget *w,
764 	struct snd_kcontrol *kcontrol, int event)
765 {
766 	struct snd_soc_component *component =
767 		snd_soc_dapm_to_component(w->dapm);
768 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
769 	unsigned char unmute = 0x0, mute = 0x1;
770 
771 	switch (event) {
772 	case SND_SOC_DAPM_POST_PMU:
773 		regmap_write(rt712->regmap,
774 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05,
775 				RT712_SDCA_CTL_FU_MUTE, CH_01),
776 				unmute);
777 		regmap_write(rt712->regmap,
778 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05,
779 				RT712_SDCA_CTL_FU_MUTE, CH_02),
780 				unmute);
781 		break;
782 	case SND_SOC_DAPM_PRE_PMD:
783 		regmap_write(rt712->regmap,
784 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05,
785 				RT712_SDCA_CTL_FU_MUTE, CH_01),
786 				mute);
787 		regmap_write(rt712->regmap,
788 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05,
789 				RT712_SDCA_CTL_FU_MUTE, CH_02),
790 				mute);
791 		break;
792 	}
793 	return 0;
794 }
795 
796 static int rt712_sdca_fu0f_event(struct snd_soc_dapm_widget *w,
797 	struct snd_kcontrol *kcontrol, int event)
798 {
799 	struct snd_soc_component *component =
800 		snd_soc_dapm_to_component(w->dapm);
801 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
802 
803 	switch (event) {
804 	case SND_SOC_DAPM_POST_PMU:
805 		rt712->fu0f_dapm_mute = false;
806 		rt712_sdca_set_fu0f_capture_ctl(rt712);
807 		break;
808 	case SND_SOC_DAPM_PRE_PMD:
809 		rt712->fu0f_dapm_mute = true;
810 		rt712_sdca_set_fu0f_capture_ctl(rt712);
811 		break;
812 	}
813 	return 0;
814 }
815 
816 static int rt712_sdca_pde40_event(struct snd_soc_dapm_widget *w,
817 	struct snd_kcontrol *kcontrol, int event)
818 {
819 	struct snd_soc_component *component =
820 		snd_soc_dapm_to_component(w->dapm);
821 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
822 	unsigned char ps0 = 0x0, ps3 = 0x3;
823 
824 	switch (event) {
825 	case SND_SOC_DAPM_POST_PMU:
826 		regmap_write(rt712->regmap,
827 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PDE40,
828 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
829 				ps0);
830 		break;
831 	case SND_SOC_DAPM_PRE_PMD:
832 		regmap_write(rt712->regmap,
833 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PDE40,
834 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
835 				ps3);
836 		break;
837 	}
838 	return 0;
839 }
840 
841 static int rt712_sdca_pde12_event(struct snd_soc_dapm_widget *w,
842 	struct snd_kcontrol *kcontrol, int event)
843 {
844 	struct snd_soc_component *component =
845 		snd_soc_dapm_to_component(w->dapm);
846 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
847 	unsigned char ps0 = 0x0, ps3 = 0x3;
848 
849 	switch (event) {
850 	case SND_SOC_DAPM_POST_PMU:
851 		regmap_write(rt712->regmap,
852 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PDE12,
853 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
854 				ps0);
855 		break;
856 	case SND_SOC_DAPM_PRE_PMD:
857 		regmap_write(rt712->regmap,
858 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PDE12,
859 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
860 				ps3);
861 		break;
862 	}
863 	return 0;
864 }
865 
866 static int rt712_sdca_pde23_event(struct snd_soc_dapm_widget *w,
867 	struct snd_kcontrol *kcontrol, int event)
868 {
869 	struct snd_soc_component *component =
870 		snd_soc_dapm_to_component(w->dapm);
871 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
872 	unsigned char ps0 = 0x0, ps3 = 0x3;
873 
874 	switch (event) {
875 	case SND_SOC_DAPM_POST_PMU:
876 		regmap_write(rt712->regmap,
877 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_PDE23,
878 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
879 				ps0);
880 		break;
881 	case SND_SOC_DAPM_PRE_PMD:
882 		regmap_write(rt712->regmap,
883 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_PDE23,
884 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
885 				ps3);
886 		break;
887 
888 	default:
889 		break;
890 	}
891 
892 	return 0;
893 }
894 
895 static const struct snd_kcontrol_new rt712_spk_l_dac =
896 	SOC_DAPM_SINGLE_AUTODISABLE("Switch",
897 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_MUTE, CH_01),
898 		0, 1, 1);
899 static const struct snd_kcontrol_new rt712_spk_r_dac =
900 	SOC_DAPM_SINGLE_AUTODISABLE("Switch",
901 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_MUTE, CH_02),
902 		0, 1, 1);
903 
904 static const struct snd_soc_dapm_widget rt712_sdca_dapm_widgets[] = {
905 	SND_SOC_DAPM_OUTPUT("HP"),
906 	SND_SOC_DAPM_INPUT("MIC2"),
907 	SND_SOC_DAPM_INPUT("LINE2"),
908 
909 	SND_SOC_DAPM_SUPPLY("PDE 40", SND_SOC_NOPM, 0, 0,
910 		rt712_sdca_pde40_event,
911 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
912 	SND_SOC_DAPM_SUPPLY("PDE 12", SND_SOC_NOPM, 0, 0,
913 		rt712_sdca_pde12_event,
914 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
915 
916 	SND_SOC_DAPM_DAC_E("FU 05", NULL, SND_SOC_NOPM, 0, 0,
917 		rt712_sdca_fu05_event,
918 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
919 	SND_SOC_DAPM_ADC_E("FU 0F", NULL, SND_SOC_NOPM, 0, 0,
920 		rt712_sdca_fu0f_event,
921 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
922 	SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0,
923 		&rt712_sdca_adc23_mux),
924 
925 	SND_SOC_DAPM_AIF_IN("DP1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0),
926 	SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0),
927 };
928 
929 static const struct snd_soc_dapm_route rt712_sdca_audio_map[] = {
930 	{ "FU 05", NULL, "DP1RX" },
931 	{ "DP4TX", NULL, "FU 0F" },
932 
933 	{ "FU 0F", NULL, "PDE 12" },
934 	{ "FU 0F", NULL, "ADC 23 Mux" },
935 	{ "ADC 23 Mux", "LINE2", "LINE2" },
936 	{ "ADC 23 Mux", "MIC2", "MIC2" },
937 
938 	{ "HP", NULL, "PDE 40" },
939 	{ "HP", NULL, "FU 05" },
940 };
941 
942 static const struct snd_soc_dapm_widget rt712_sdca_spk_dapm_widgets[] = {
943 	SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0),
944 
945 	/* Digital Interface */
946 	SND_SOC_DAPM_PGA("FU06", SND_SOC_NOPM, 0, 0, NULL, 0),
947 
948 	SND_SOC_DAPM_SUPPLY("PDE 23", SND_SOC_NOPM, 0, 0,
949 		rt712_sdca_pde23_event,
950 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
951 
952 	/* Output */
953 	SND_SOC_DAPM_SWITCH("OT23 L", SND_SOC_NOPM, 0, 0, &rt712_spk_l_dac),
954 	SND_SOC_DAPM_SWITCH("OT23 R", SND_SOC_NOPM, 0, 0, &rt712_spk_r_dac),
955 	SND_SOC_DAPM_OUTPUT("SPOL"),
956 	SND_SOC_DAPM_OUTPUT("SPOR"),
957 };
958 
959 static const struct snd_soc_dapm_route rt712_sdca_spk_dapm_routes[] = {
960 	{ "FU06", NULL, "DP3RX" },
961 	{ "FU06", NULL, "PDE 23" },
962 	{ "OT23 L", "Switch", "FU06" },
963 	{ "OT23 R", "Switch", "FU06" },
964 	{ "SPOL", NULL, "OT23 L" },
965 	{ "SPOR", NULL, "OT23 R" },
966 };
967 
968 static int rt712_sdca_parse_dt(struct rt712_sdca_priv *rt712, struct device *dev)
969 {
970 	device_property_read_u32(dev, "realtek,jd-src", &rt712->jd_src);
971 
972 	return 0;
973 }
974 
975 static int rt712_sdca_probe(struct snd_soc_component *component)
976 {
977 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
978 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
979 	int ret;
980 
981 	rt712_sdca_parse_dt(rt712, &rt712->slave->dev);
982 	rt712->component = component;
983 
984 	/* add SPK route */
985 	if (rt712->hw_id != RT712_DEV_ID_713) {
986 		snd_soc_add_component_controls(component,
987 			rt712_sdca_spk_controls, ARRAY_SIZE(rt712_sdca_spk_controls));
988 		snd_soc_dapm_new_controls(dapm,
989 			rt712_sdca_spk_dapm_widgets, ARRAY_SIZE(rt712_sdca_spk_dapm_widgets));
990 		snd_soc_dapm_add_routes(dapm,
991 			rt712_sdca_spk_dapm_routes, ARRAY_SIZE(rt712_sdca_spk_dapm_routes));
992 	}
993 
994 	if (!rt712->first_hw_init)
995 		return 0;
996 
997 	ret = pm_runtime_resume(component->dev);
998 	if (ret < 0 && ret != -EACCES)
999 		return ret;
1000 
1001 	return 0;
1002 }
1003 
1004 static int rt712_sdca_dmic_set_gain_get(struct snd_kcontrol *kcontrol,
1005 		struct snd_ctl_elem_value *ucontrol)
1006 {
1007 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1008 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1009 	struct rt712_dmic_kctrl_priv *p =
1010 		(struct rt712_dmic_kctrl_priv *)kcontrol->private_value;
1011 	unsigned int regvalue, ctl, i;
1012 	unsigned int adc_vol_flag = 0;
1013 	const unsigned int interval_offset = 0xc0;
1014 
1015 	if (strstr(ucontrol->id.name, "FU1E Capture Volume"))
1016 		adc_vol_flag = 1;
1017 
1018 	/* check all channels */
1019 	for (i = 0; i < p->count; i++) {
1020 		regmap_read(rt712->mbq_regmap, p->reg_base + i, &regvalue);
1021 
1022 		if (!adc_vol_flag) /* boost gain */
1023 			ctl = regvalue / 0x0a00;
1024 		else { /* ADC gain */
1025 			if (adc_vol_flag)
1026 				ctl = p->max - (((0x1e00 - regvalue) & 0xffff) / interval_offset);
1027 			else
1028 				ctl = p->max - (((0 - regvalue) & 0xffff) / interval_offset);
1029 		}
1030 
1031 		ucontrol->value.integer.value[i] = ctl;
1032 	}
1033 
1034 	return 0;
1035 }
1036 
1037 static int rt712_sdca_dmic_set_gain_put(struct snd_kcontrol *kcontrol,
1038 		struct snd_ctl_elem_value *ucontrol)
1039 {
1040 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1041 	struct rt712_dmic_kctrl_priv *p =
1042 		(struct rt712_dmic_kctrl_priv *)kcontrol->private_value;
1043 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1044 	unsigned int gain_val[4];
1045 	unsigned int i, adc_vol_flag = 0, changed = 0;
1046 	unsigned int regvalue[4];
1047 	const unsigned int interval_offset = 0xc0;
1048 	int err;
1049 
1050 	if (strstr(ucontrol->id.name, "FU1E Capture Volume"))
1051 		adc_vol_flag = 1;
1052 
1053 	/* check all channels */
1054 	for (i = 0; i < p->count; i++) {
1055 		regmap_read(rt712->mbq_regmap, p->reg_base + i, &regvalue[i]);
1056 
1057 		gain_val[i] = ucontrol->value.integer.value[i];
1058 		if (gain_val[i] > p->max)
1059 			gain_val[i] = p->max;
1060 
1061 		if (!adc_vol_flag) /* boost gain */
1062 			gain_val[i] = gain_val[i] * 0x0a00;
1063 		else { /* ADC gain */
1064 			gain_val[i] = 0x1e00 - ((p->max - gain_val[i]) * interval_offset);
1065 			gain_val[i] &= 0xffff;
1066 		}
1067 
1068 		if (regvalue[i] != gain_val[i])
1069 			changed = 1;
1070 	}
1071 
1072 	if (!changed)
1073 		return 0;
1074 
1075 	for (i = 0; i < p->count; i++) {
1076 		err = regmap_write(rt712->mbq_regmap, p->reg_base + i, gain_val[i]);
1077 		if (err < 0)
1078 			dev_err(&rt712->slave->dev, "0x%08x can't be set\n", p->reg_base + i);
1079 	}
1080 
1081 	return changed;
1082 }
1083 
1084 static int rt712_sdca_set_fu1e_capture_ctl(struct rt712_sdca_priv *rt712)
1085 {
1086 	int err, i;
1087 	unsigned int ch_mute;
1088 
1089 	for (i = 0; i < ARRAY_SIZE(rt712->fu1e_mixer_mute); i++) {
1090 		ch_mute = (rt712->fu1e_dapm_mute || rt712->fu1e_mixer_mute[i]) ? 0x01 : 0x00;
1091 		err = regmap_write(rt712->regmap,
1092 				SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT712_SDCA_ENT_USER_FU1E,
1093 				RT712_SDCA_CTL_FU_MUTE, CH_01) + i, ch_mute);
1094 		if (err < 0)
1095 			return err;
1096 	}
1097 
1098 	return 0;
1099 }
1100 
1101 static int rt712_sdca_dmic_fu1e_capture_get(struct snd_kcontrol *kcontrol,
1102 			struct snd_ctl_elem_value *ucontrol)
1103 {
1104 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1105 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1106 	struct rt712_dmic_kctrl_priv *p =
1107 		(struct rt712_dmic_kctrl_priv *)kcontrol->private_value;
1108 	unsigned int i;
1109 
1110 	for (i = 0; i < p->count; i++)
1111 		ucontrol->value.integer.value[i] = !rt712->fu1e_mixer_mute[i];
1112 
1113 	return 0;
1114 }
1115 
1116 static int rt712_sdca_dmic_fu1e_capture_put(struct snd_kcontrol *kcontrol,
1117 			struct snd_ctl_elem_value *ucontrol)
1118 {
1119 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1120 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1121 	struct rt712_dmic_kctrl_priv *p =
1122 		(struct rt712_dmic_kctrl_priv *)kcontrol->private_value;
1123 	int err, changed = 0, i;
1124 
1125 	for (i = 0; i < p->count; i++) {
1126 		if (rt712->fu1e_mixer_mute[i] != !ucontrol->value.integer.value[i])
1127 			changed = 1;
1128 		rt712->fu1e_mixer_mute[i] = !ucontrol->value.integer.value[i];
1129 	}
1130 
1131 	err = rt712_sdca_set_fu1e_capture_ctl(rt712);
1132 	if (err < 0)
1133 		return err;
1134 
1135 	return changed;
1136 }
1137 
1138 static int rt712_sdca_fu_info(struct snd_kcontrol *kcontrol,
1139 	struct snd_ctl_elem_info *uinfo)
1140 {
1141 	struct rt712_dmic_kctrl_priv *p =
1142 		(struct rt712_dmic_kctrl_priv *)kcontrol->private_value;
1143 
1144 	if (p->max == 1)
1145 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1146 	else
1147 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1148 	uinfo->count = p->count;
1149 	uinfo->value.integer.min = 0;
1150 	uinfo->value.integer.max = p->max;
1151 	return 0;
1152 }
1153 
1154 #define RT712_SDCA_PR_VALUE(xreg_base, xcount, xmax, xinvert) \
1155 	((unsigned long)&(struct rt712_dmic_kctrl_priv) \
1156 		{.reg_base = xreg_base, .count = xcount, .max = xmax, \
1157 		.invert = xinvert})
1158 
1159 #define RT712_SDCA_FU_CTRL(xname, reg_base, xmax, xinvert, xcount) \
1160 {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
1161 	.info = rt712_sdca_fu_info, \
1162 	.get = rt712_sdca_dmic_fu1e_capture_get, \
1163 	.put = rt712_sdca_dmic_fu1e_capture_put, \
1164 	.private_value = RT712_SDCA_PR_VALUE(reg_base, xcount, xmax, xinvert)}
1165 
1166 #define RT712_SDCA_EXT_TLV(xname, reg_base, xhandler_get,\
1167 	 xhandler_put, xcount, xmax, tlv_array) \
1168 {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
1169 	.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
1170 		 SNDRV_CTL_ELEM_ACCESS_READWRITE, \
1171 	.tlv.p = (tlv_array), \
1172 	.info = rt712_sdca_fu_info, \
1173 	.get = xhandler_get, .put = xhandler_put, \
1174 	.private_value = RT712_SDCA_PR_VALUE(reg_base, xcount, xmax, 0) }
1175 
1176 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0);
1177 static const DECLARE_TLV_DB_SCALE(dmic_vol_tlv, 0, 1000, 0);
1178 
1179 static const struct snd_kcontrol_new rt712_sdca_dmic_snd_controls[] = {
1180 	RT712_SDCA_FU_CTRL("FU1E Capture Switch",
1181 		SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT712_SDCA_ENT_USER_FU1E, RT712_SDCA_CTL_FU_MUTE, CH_01),
1182 		1, 1, 4),
1183 	RT712_SDCA_EXT_TLV("FU1E Capture Volume",
1184 		SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT712_SDCA_ENT_USER_FU1E, RT712_SDCA_CTL_FU_VOLUME, CH_01),
1185 		rt712_sdca_dmic_set_gain_get, rt712_sdca_dmic_set_gain_put, 4, 0x3f, in_vol_tlv),
1186 	RT712_SDCA_EXT_TLV("FU15 Boost Volume",
1187 		SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT712_SDCA_ENT_PLATFORM_FU15, RT712_SDCA_CTL_FU_CH_GAIN, CH_01),
1188 		rt712_sdca_dmic_set_gain_get, rt712_sdca_dmic_set_gain_put, 4, 3, dmic_vol_tlv),
1189 };
1190 
1191 static int rt712_sdca_dmic_mux_get(struct snd_kcontrol *kcontrol,
1192 			struct snd_ctl_elem_value *ucontrol)
1193 {
1194 	struct snd_soc_component *component =
1195 		snd_soc_dapm_kcontrol_component(kcontrol);
1196 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1197 	unsigned int val = 0, mask_sft;
1198 
1199 	if (strstr(ucontrol->id.name, "ADC 0A Mux"))
1200 		mask_sft = 0;
1201 	else if (strstr(ucontrol->id.name, "ADC 0B Mux"))
1202 		mask_sft = 4;
1203 	else
1204 		return -EINVAL;
1205 
1206 	rt712_sdca_index_read(rt712, RT712_VENDOR_HDA_CTL,
1207 		RT712_HDA_LEGACY_MUX_CTL0, &val);
1208 
1209 	ucontrol->value.enumerated.item[0] = (((val >> mask_sft) & 0xf) == 0x4) ? 0 : 1;
1210 
1211 	return 0;
1212 }
1213 
1214 static int rt712_sdca_dmic_mux_put(struct snd_kcontrol *kcontrol,
1215 			struct snd_ctl_elem_value *ucontrol)
1216 {
1217 	struct snd_soc_component *component =
1218 		snd_soc_dapm_kcontrol_component(kcontrol);
1219 	struct snd_soc_dapm_context *dapm =
1220 		snd_soc_dapm_kcontrol_dapm(kcontrol);
1221 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1222 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1223 	unsigned int *item = ucontrol->value.enumerated.item;
1224 	unsigned int val, val2 = 0, change, mask_sft;
1225 
1226 	if (item[0] >= e->items)
1227 		return -EINVAL;
1228 
1229 	if (strstr(ucontrol->id.name, "ADC 0A Mux"))
1230 		mask_sft = 0;
1231 	else if (strstr(ucontrol->id.name, "ADC 0B Mux"))
1232 		mask_sft = 4;
1233 	else
1234 		return -EINVAL;
1235 
1236 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
1237 
1238 	rt712_sdca_index_read(rt712, RT712_VENDOR_HDA_CTL,
1239 		RT712_HDA_LEGACY_MUX_CTL0, &val2);
1240 	val2 = ((0xf << mask_sft) & val2) >> mask_sft;
1241 
1242 	if (val == 0)
1243 		val = 0x4;
1244 	else if (val >= 1)
1245 		val = 0xe;
1246 
1247 	if (val == val2)
1248 		change = 0;
1249 	else
1250 		change = 1;
1251 
1252 	if (change)
1253 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
1254 			RT712_HDA_LEGACY_MUX_CTL0, 0xf << mask_sft,
1255 			val << mask_sft);
1256 
1257 	snd_soc_dapm_mux_update_power(dapm, kcontrol, item[0], e, NULL);
1258 
1259 	return change;
1260 }
1261 
1262 static const char * const adc_dmic_mux_text[] = {
1263 	"DMIC1",
1264 	"DMIC2",
1265 };
1266 
1267 static SOC_ENUM_SINGLE_DECL(
1268 	rt712_adc0a_enum, SND_SOC_NOPM, 0, adc_dmic_mux_text);
1269 
1270 static SOC_ENUM_SINGLE_DECL(
1271 	rt712_adc0b_enum, SND_SOC_NOPM, 0, adc_dmic_mux_text);
1272 
1273 static const struct snd_kcontrol_new rt712_sdca_dmic_adc0a_mux =
1274 	SOC_DAPM_ENUM_EXT("ADC 0A Mux", rt712_adc0a_enum,
1275 			rt712_sdca_dmic_mux_get, rt712_sdca_dmic_mux_put);
1276 
1277 static const struct snd_kcontrol_new rt712_sdca_dmic_adc0b_mux =
1278 	SOC_DAPM_ENUM_EXT("ADC 0B Mux", rt712_adc0b_enum,
1279 			rt712_sdca_dmic_mux_get, rt712_sdca_dmic_mux_put);
1280 
1281 static int rt712_sdca_dmic_fu1e_event(struct snd_soc_dapm_widget *w,
1282 	struct snd_kcontrol *kcontrol, int event)
1283 {
1284 	struct snd_soc_component *component =
1285 		snd_soc_dapm_to_component(w->dapm);
1286 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1287 
1288 	switch (event) {
1289 	case SND_SOC_DAPM_POST_PMU:
1290 		rt712->fu1e_dapm_mute = false;
1291 		rt712_sdca_set_fu1e_capture_ctl(rt712);
1292 		break;
1293 	case SND_SOC_DAPM_PRE_PMD:
1294 		rt712->fu1e_dapm_mute = true;
1295 		rt712_sdca_set_fu1e_capture_ctl(rt712);
1296 		break;
1297 	}
1298 	return 0;
1299 }
1300 
1301 static int rt712_sdca_dmic_pde11_event(struct snd_soc_dapm_widget *w,
1302 	struct snd_kcontrol *kcontrol, int event)
1303 {
1304 	struct snd_soc_component *component =
1305 		snd_soc_dapm_to_component(w->dapm);
1306 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1307 	unsigned char ps0 = 0x0, ps3 = 0x3;
1308 
1309 	switch (event) {
1310 	case SND_SOC_DAPM_POST_PMU:
1311 		regmap_write(rt712->regmap,
1312 			SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT712_SDCA_ENT_PDE11,
1313 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
1314 				ps0);
1315 		break;
1316 	case SND_SOC_DAPM_PRE_PMD:
1317 		regmap_write(rt712->regmap,
1318 			SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT712_SDCA_ENT_PDE11,
1319 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
1320 				ps3);
1321 		break;
1322 	}
1323 	return 0;
1324 }
1325 
1326 static const struct snd_soc_dapm_widget rt712_sdca_dmic_dapm_widgets[] = {
1327 	SND_SOC_DAPM_INPUT("DMIC1"),
1328 	SND_SOC_DAPM_INPUT("DMIC2"),
1329 
1330 	SND_SOC_DAPM_SUPPLY("PDE 11", SND_SOC_NOPM, 0, 0,
1331 		rt712_sdca_dmic_pde11_event,
1332 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1333 
1334 	SND_SOC_DAPM_ADC_E("FU 1E", NULL, SND_SOC_NOPM, 0, 0,
1335 		rt712_sdca_dmic_fu1e_event,
1336 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1337 	SND_SOC_DAPM_MUX("ADC 0A Mux", SND_SOC_NOPM, 0, 0,
1338 		&rt712_sdca_dmic_adc0a_mux),
1339 	SND_SOC_DAPM_MUX("ADC 0B Mux", SND_SOC_NOPM, 0, 0,
1340 		&rt712_sdca_dmic_adc0b_mux),
1341 
1342 	SND_SOC_DAPM_AIF_OUT("DP8TX", "DP8 Capture", 0, SND_SOC_NOPM, 0, 0),
1343 };
1344 
1345 static const struct snd_soc_dapm_route rt712_sdca_dmic_audio_map[] = {
1346 	{"DP8TX", NULL, "FU 1E"},
1347 
1348 	{"FU 1E", NULL, "PDE 11"},
1349 	{"FU 1E", NULL, "ADC 0A Mux"},
1350 	{"FU 1E", NULL, "ADC 0B Mux"},
1351 	{"ADC 0A Mux", "DMIC1", "DMIC1"},
1352 	{"ADC 0A Mux", "DMIC2", "DMIC2"},
1353 	{"ADC 0B Mux", "DMIC1", "DMIC1"},
1354 	{"ADC 0B Mux", "DMIC2", "DMIC2"},
1355 };
1356 
1357 static int rt712_sdca_dmic_probe(struct snd_soc_component *component)
1358 {
1359 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1360 	int ret;
1361 
1362 	rt712->dmic_component = component;
1363 
1364 	if (!rt712->first_hw_init)
1365 		return 0;
1366 
1367 	ret = pm_runtime_resume(component->dev);
1368 	if (ret < 0 && ret != -EACCES)
1369 		return ret;
1370 
1371 	return 0;
1372 }
1373 
1374 static const struct snd_soc_component_driver soc_sdca_dev_rt712 = {
1375 	.probe = rt712_sdca_probe,
1376 	.controls = rt712_sdca_controls,
1377 	.num_controls = ARRAY_SIZE(rt712_sdca_controls),
1378 	.dapm_widgets = rt712_sdca_dapm_widgets,
1379 	.num_dapm_widgets = ARRAY_SIZE(rt712_sdca_dapm_widgets),
1380 	.dapm_routes = rt712_sdca_audio_map,
1381 	.num_dapm_routes = ARRAY_SIZE(rt712_sdca_audio_map),
1382 	.set_jack = rt712_sdca_set_jack_detect,
1383 	.endianness = 1,
1384 };
1385 
1386 static const struct snd_soc_component_driver soc_sdca_dev_rt712_dmic = {
1387 	.probe = rt712_sdca_dmic_probe,
1388 	.controls = rt712_sdca_dmic_snd_controls,
1389 	.num_controls = ARRAY_SIZE(rt712_sdca_dmic_snd_controls),
1390 	.dapm_widgets = rt712_sdca_dmic_dapm_widgets,
1391 	.num_dapm_widgets = ARRAY_SIZE(rt712_sdca_dmic_dapm_widgets),
1392 	.dapm_routes = rt712_sdca_dmic_audio_map,
1393 	.num_dapm_routes = ARRAY_SIZE(rt712_sdca_dmic_audio_map),
1394 	.endianness = 1,
1395 #ifdef CONFIG_DEBUG_FS
1396 	.debugfs_prefix = "dmic",
1397 #endif
1398 };
1399 
1400 static int rt712_sdca_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
1401 				int direction)
1402 {
1403 	snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
1404 
1405 	return 0;
1406 }
1407 
1408 static void rt712_sdca_shutdown(struct snd_pcm_substream *substream,
1409 				struct snd_soc_dai *dai)
1410 {
1411 	snd_soc_dai_set_dma_data(dai, substream, NULL);
1412 }
1413 
1414 static int rt712_sdca_pcm_hw_params(struct snd_pcm_substream *substream,
1415 				struct snd_pcm_hw_params *params,
1416 				struct snd_soc_dai *dai)
1417 {
1418 	struct snd_soc_component *component = dai->component;
1419 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1420 	struct sdw_stream_config stream_config;
1421 	struct sdw_port_config port_config;
1422 	enum sdw_data_direction direction;
1423 	struct sdw_stream_runtime *sdw_stream;
1424 	int retval, port, num_channels;
1425 	unsigned int sampling_rate;
1426 
1427 	dev_dbg(dai->dev, "%s %s id %d", __func__, dai->name, dai->id);
1428 	sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
1429 
1430 	if (!sdw_stream)
1431 		return -EINVAL;
1432 
1433 	if (!rt712->slave)
1434 		return -EINVAL;
1435 
1436 	/* VA doesn't support AIF3 */
1437 	if (dai->id == RT712_AIF3 && rt712->version_id == RT712_VA)
1438 		return -EINVAL;
1439 
1440 	/* SoundWire specific configuration */
1441 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1442 		direction = SDW_DATA_DIR_RX;
1443 		if (dai->id == RT712_AIF1)
1444 			port = 1;
1445 		else if (dai->id == RT712_AIF2)
1446 			port = 3;
1447 		else
1448 			return -EINVAL;
1449 	} else {
1450 		direction = SDW_DATA_DIR_TX;
1451 		if (dai->id == RT712_AIF1)
1452 			port = 4;
1453 		else if (dai->id == RT712_AIF3)
1454 			port = 8;
1455 		else
1456 			return -EINVAL;
1457 	}
1458 
1459 	stream_config.frame_rate = params_rate(params);
1460 	stream_config.ch_count = params_channels(params);
1461 	stream_config.bps = snd_pcm_format_width(params_format(params));
1462 	stream_config.direction = direction;
1463 
1464 	num_channels = params_channels(params);
1465 	port_config.ch_mask = GENMASK(num_channels - 1, 0);
1466 	port_config.num = port;
1467 
1468 	retval = sdw_stream_add_slave(rt712->slave, &stream_config,
1469 					&port_config, 1, sdw_stream);
1470 	if (retval) {
1471 		dev_err(dai->dev, "%s: Unable to configure port\n", __func__);
1472 		return retval;
1473 	}
1474 
1475 	if (params_channels(params) > 16) {
1476 		dev_err(component->dev, "%s: Unsupported channels %d\n",
1477 			__func__, params_channels(params));
1478 		return -EINVAL;
1479 	}
1480 
1481 	/* sampling rate configuration */
1482 	switch (params_rate(params)) {
1483 	case 44100:
1484 		sampling_rate = RT712_SDCA_RATE_44100HZ;
1485 		break;
1486 	case 48000:
1487 		sampling_rate = RT712_SDCA_RATE_48000HZ;
1488 		break;
1489 	case 96000:
1490 		sampling_rate = RT712_SDCA_RATE_96000HZ;
1491 		break;
1492 	case 192000:
1493 		sampling_rate = RT712_SDCA_RATE_192000HZ;
1494 		break;
1495 	default:
1496 		dev_err(component->dev, "%s: Rate %d is not supported\n",
1497 			__func__, params_rate(params));
1498 		return -EINVAL;
1499 	}
1500 
1501 	/* set sampling frequency */
1502 	switch (dai->id) {
1503 	case RT712_AIF1:
1504 		regmap_write(rt712->regmap,
1505 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_CS01, RT712_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1506 			sampling_rate);
1507 		regmap_write(rt712->regmap,
1508 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_CS11, RT712_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1509 			sampling_rate);
1510 		break;
1511 	case RT712_AIF2:
1512 		regmap_write(rt712->regmap,
1513 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_CS31, RT712_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1514 			sampling_rate);
1515 		break;
1516 	case RT712_AIF3:
1517 		regmap_write(rt712->regmap,
1518 			SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT712_SDCA_ENT_CS1F, RT712_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1519 			sampling_rate);
1520 		regmap_write(rt712->regmap,
1521 			SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT712_SDCA_ENT_CS1C, RT712_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1522 			sampling_rate);
1523 		break;
1524 	default:
1525 		dev_err(component->dev, "%s: Wrong DAI id\n", __func__);
1526 		return -EINVAL;
1527 	}
1528 
1529 	return 0;
1530 }
1531 
1532 static int rt712_sdca_pcm_hw_free(struct snd_pcm_substream *substream,
1533 				struct snd_soc_dai *dai)
1534 {
1535 	struct snd_soc_component *component = dai->component;
1536 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1537 	struct sdw_stream_runtime *sdw_stream =
1538 		snd_soc_dai_get_dma_data(dai, substream);
1539 
1540 	if (!rt712->slave)
1541 		return -EINVAL;
1542 
1543 	sdw_stream_remove_slave(rt712->slave, sdw_stream);
1544 	return 0;
1545 }
1546 
1547 #define RT712_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
1548 			SNDRV_PCM_RATE_192000)
1549 #define RT712_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1550 			SNDRV_PCM_FMTBIT_S24_LE)
1551 
1552 static const struct snd_soc_dai_ops rt712_sdca_ops = {
1553 	.hw_params	= rt712_sdca_pcm_hw_params,
1554 	.hw_free	= rt712_sdca_pcm_hw_free,
1555 	.set_stream	= rt712_sdca_set_sdw_stream,
1556 	.shutdown	= rt712_sdca_shutdown,
1557 };
1558 
1559 static struct snd_soc_dai_driver rt712_sdca_dai[] = {
1560 	{
1561 		.name = "rt712-sdca-aif1",
1562 		.id = RT712_AIF1,
1563 		.playback = {
1564 			.stream_name = "DP1 Playback",
1565 			.channels_min = 1,
1566 			.channels_max = 2,
1567 			.rates = RT712_STEREO_RATES,
1568 			.formats = RT712_FORMATS,
1569 		},
1570 		.capture = {
1571 			.stream_name = "DP4 Capture",
1572 			.channels_min = 1,
1573 			.channels_max = 2,
1574 			.rates = RT712_STEREO_RATES,
1575 			.formats = RT712_FORMATS,
1576 		},
1577 		.ops = &rt712_sdca_ops,
1578 	},
1579 	{
1580 		.name = "rt712-sdca-aif2",
1581 		.id = RT712_AIF2,
1582 		.playback = {
1583 			.stream_name = "DP3 Playback",
1584 			.channels_min = 1,
1585 			.channels_max = 2,
1586 			.rates = RT712_STEREO_RATES,
1587 			.formats = RT712_FORMATS,
1588 		},
1589 		.ops = &rt712_sdca_ops,
1590 	}
1591 };
1592 
1593 static struct snd_soc_dai_driver rt712_sdca_dmic_dai[] = {
1594 	{
1595 		.name = "rt712-sdca-aif3",
1596 		.id = RT712_AIF3,
1597 		.capture = {
1598 			.stream_name = "DP8 Capture",
1599 			.channels_min = 1,
1600 			.channels_max = 4,
1601 			.rates = RT712_STEREO_RATES,
1602 			.formats = RT712_FORMATS,
1603 		},
1604 		.ops = &rt712_sdca_ops,
1605 	}
1606 };
1607 
1608 int rt712_sdca_init(struct device *dev, struct regmap *regmap,
1609 			struct regmap *mbq_regmap, struct sdw_slave *slave)
1610 {
1611 	struct rt712_sdca_priv *rt712;
1612 	int ret;
1613 
1614 	rt712 = devm_kzalloc(dev, sizeof(*rt712), GFP_KERNEL);
1615 	if (!rt712)
1616 		return -ENOMEM;
1617 
1618 	dev_set_drvdata(dev, rt712);
1619 	rt712->slave = slave;
1620 	rt712->regmap = regmap;
1621 	rt712->mbq_regmap = mbq_regmap;
1622 
1623 	regcache_cache_only(rt712->regmap, true);
1624 	regcache_cache_only(rt712->mbq_regmap, true);
1625 
1626 	mutex_init(&rt712->calibrate_mutex);
1627 	mutex_init(&rt712->disable_irq_lock);
1628 
1629 	INIT_DELAYED_WORK(&rt712->jack_detect_work, rt712_sdca_jack_detect_handler);
1630 	INIT_DELAYED_WORK(&rt712->jack_btn_check_work, rt712_sdca_btn_check_handler);
1631 
1632 	/*
1633 	 * Mark hw_init to false
1634 	 * HW init will be performed when device reports present
1635 	 */
1636 	rt712->hw_init = false;
1637 	rt712->first_hw_init = false;
1638 	rt712->fu0f_dapm_mute = true;
1639 	rt712->fu0f_mixer_l_mute = rt712->fu0f_mixer_r_mute = true;
1640 	rt712->fu1e_dapm_mute = true;
1641 	rt712->fu1e_mixer_mute[0] = rt712->fu1e_mixer_mute[1] =
1642 		rt712->fu1e_mixer_mute[2] = rt712->fu1e_mixer_mute[3] = true;
1643 
1644 	/* JD source uses JD1 in default */
1645 	rt712->jd_src = RT712_JD1;
1646 
1647 	if (slave->id.part_id != RT712_PART_ID_713)
1648 		ret =  devm_snd_soc_register_component(dev,
1649 				&soc_sdca_dev_rt712, rt712_sdca_dai, ARRAY_SIZE(rt712_sdca_dai));
1650 	else
1651 		ret =  devm_snd_soc_register_component(dev,
1652 				&soc_sdca_dev_rt712, rt712_sdca_dai, 1);
1653 	if (ret < 0)
1654 		return ret;
1655 
1656 	/* only add the dmic component if a SMART_MIC function is exposed in ACPI */
1657 	if (sdca_device_quirk_match(slave, SDCA_QUIRKS_RT712_VB)) {
1658 		ret =  devm_snd_soc_register_component(dev,
1659 						       &soc_sdca_dev_rt712_dmic,
1660 						       rt712_sdca_dmic_dai,
1661 						       ARRAY_SIZE(rt712_sdca_dmic_dai));
1662 		if (ret < 0)
1663 			return ret;
1664 		rt712->dmic_function_found = true;
1665 	}
1666 
1667 	/* set autosuspend parameters */
1668 	pm_runtime_set_autosuspend_delay(dev, 3000);
1669 	pm_runtime_use_autosuspend(dev);
1670 
1671 	/* make sure the device does not suspend immediately */
1672 	pm_runtime_mark_last_busy(dev);
1673 
1674 	pm_runtime_enable(dev);
1675 
1676 	/* important note: the device is NOT tagged as 'active' and will remain
1677 	 * 'suspended' until the hardware is enumerated/initialized. This is required
1678 	 * to make sure the ASoC framework use of pm_runtime_get_sync() does not silently
1679 	 * fail with -EACCESS because of race conditions between card creation and enumeration
1680 	 */
1681 
1682 	dev_dbg(dev, "%s\n", __func__);
1683 
1684 	return 0;
1685 }
1686 
1687 static void rt712_sdca_va_io_init(struct rt712_sdca_priv *rt712)
1688 {
1689 	int ret = 0;
1690 	unsigned int hibernation_flag;
1691 	struct device *dev = &rt712->slave->dev;
1692 
1693 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_ANALOG_BIAS_CTL3, 0xaa81);
1694 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_LDO2_3_CTL1, 0xa1e0);
1695 	rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_HP_DETECT_RLDET_CTL1, 0x0000);
1696 	rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_HP_DETECT_RLDET_CTL2, 0x0000);
1697 	rt712_sdca_index_write(rt712, RT712_VENDOR_ANALOG_CTL, RT712_MISC_POWER_CTL7, 0x0000);
1698 	regmap_write(rt712->regmap, RT712_RC_CAL, 0x23);
1699 
1700 	/* calibration */
1701 	rt712_sdca_index_read(rt712, RT712_VENDOR_REG, RT712_SW_CONFIG1, &hibernation_flag);
1702 	if (!hibernation_flag) {
1703 		ret = rt712_sdca_calibration(rt712);
1704 		if (ret < 0)
1705 			dev_err(dev, "%s, calibration failed!\n", __func__);
1706 	}
1707 
1708 	rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
1709 		RT712_MIXER_CTL1, 0x3000, 0x0000);
1710 	rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
1711 		RT712_ADC0A_08_PDE_FLOAT_CTL, 0x1112);
1712 	rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
1713 		RT712_MIC2_LINE2_PDE_FLOAT_CTL, 0x3412);
1714 	rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
1715 		RT712_DAC03_HP_PDE_FLOAT_CTL, 0x4040);
1716 
1717 	rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
1718 		RT712_HDA_LEGACY_GPIO_WAKE_EN_CTL, 0x0001, 0x0000);
1719 	regmap_write(rt712->regmap, 0x2f50, 0x00);
1720 	regmap_write(rt712->regmap, 0x2f54, 0x00);
1721 	regmap_write(rt712->regmap,
1722 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_IT09, RT712_SDCA_CTL_VENDOR_DEF, 0), 0x01);
1723 
1724 	/* add SPK settings */
1725 	if (rt712->hw_id != RT712_DEV_ID_713) {
1726 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_AMP_PDE_FLOAT_CTL, 0x2323);
1727 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_EAPD_CTL, 0x0002);
1728 		regmap_write(rt712->regmap,
1729 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_OT23, RT712_SDCA_CTL_VENDOR_DEF, 0), 0x04);
1730 	}
1731 }
1732 
1733 static void rt712_sdca_vb_io_init(struct rt712_sdca_priv *rt712)
1734 {
1735 	int ret = 0;
1736 	unsigned int jack_func_status, mic_func_status, amp_func_status;
1737 	struct device *dev = &rt712->slave->dev;
1738 
1739 	regmap_read(rt712->regmap,
1740 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT0, RT712_SDCA_CTL_FUNC_STATUS, 0), &jack_func_status);
1741 	regmap_read(rt712->regmap,
1742 		SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT712_SDCA_ENT0, RT712_SDCA_CTL_FUNC_STATUS, 0), &mic_func_status);
1743 	regmap_read(rt712->regmap,
1744 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT0, RT712_SDCA_CTL_FUNC_STATUS, 0), &amp_func_status);
1745 	dev_dbg(dev, "%s jack/mic/amp func_status=0x%x, 0x%x, 0x%x\n",
1746 		__func__, jack_func_status, mic_func_status, amp_func_status);
1747 
1748 	/* DMIC */
1749 	if ((mic_func_status & FUNCTION_NEEDS_INITIALIZATION) || (!rt712->first_hw_init)) {
1750 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_DMIC2_FU_IT_FLOAT_CTL, 0x1526);
1751 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_DMIC2_FU_CH12_FLOAT_CTL, 0x0304);
1752 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_ADC0A_CS_ADC0B_FU_FLOAT_CTL, 0x1f1e);
1753 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_ADC0B_FU_CH12_FLOAT_CTL, 0x0304);
1754 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_HDA_LEGACY_CONFIG_CTL0, 0x8010);
1755 		regmap_write(rt712->regmap,
1756 			SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT712_SDCA_ENT_IT11, RT712_SDCA_CTL_VENDOR_DEF, 0), 0x01);
1757 		rt712_sdca_index_write(rt712, RT712_ULTRA_SOUND_DET, RT712_ULTRA_SOUND_DETECTOR6, 0x3200);
1758 		regmap_write(rt712->regmap, RT712_RC_CAL, 0x23);
1759 
1760 		/* clear flag */
1761 		regmap_write(rt712->regmap,
1762 			SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT712_SDCA_ENT0, RT712_SDCA_CTL_FUNC_STATUS, 0),
1763 				FUNCTION_NEEDS_INITIALIZATION);
1764 	}
1765 
1766 	/* Jack */
1767 	if ((jack_func_status & FUNCTION_NEEDS_INITIALIZATION) || (!rt712->first_hw_init)) {
1768 		rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_SEL_VEE2_HP_CTL1, 0x042a);
1769 		rt712_sdca_index_write(rt712, RT712_CHARGE_PUMP, RT712_HP_DET_CTL3, 0x1fff);
1770 		rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_IO_CTL, 0xec67);
1771 		rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_ANALOG_BIAS_CTL3, 0xaa81);
1772 		rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_LDO2_3_CTL1, 0xa1e0);
1773 		rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_HP_DETECT_RLDET_CTL1, 0x0000);
1774 		rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_HP_DETECT_RLDET_CTL2, 0x0000);
1775 		regmap_write(rt712->regmap, RT712_RC_CAL, 0x23);
1776 		rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_JD_CTL1, 0x2802);
1777 		rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_CLASSD_AMP_CTL6, 0xf215);
1778 
1779 		/* calibration */
1780 		ret = rt712_sdca_calibration(rt712);
1781 		if (ret < 0)
1782 			dev_err(dev, "%s, calibration failed!\n", __func__);
1783 
1784 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL, RT712_MIXER_CTL1, 0x3000, 0x0000);
1785 		regmap_write(rt712->regmap,
1786 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_IT09, RT712_SDCA_CTL_VENDOR_DEF, 0), 0x01);
1787 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_MISC_CTL_FOR_UAJ, 0x0003);
1788 
1789 		/* clear flag */
1790 		regmap_write(rt712->regmap,
1791 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT0, RT712_SDCA_CTL_FUNC_STATUS, 0),
1792 				FUNCTION_NEEDS_INITIALIZATION);
1793 	}
1794 
1795 	/* SPK */
1796 	if ((amp_func_status & FUNCTION_NEEDS_INITIALIZATION) || (!rt712->first_hw_init)) {
1797 		if (rt712->hw_id != RT712_DEV_ID_713) {
1798 			rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_IO_CTL, 0xec63);
1799 			rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_CLASSD_AMP_CTL1, 0xfff5);
1800 			rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_EAPD_CTL, 0x0002);
1801 			regmap_write(rt712->regmap,
1802 				SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_OT23, RT712_SDCA_CTL_VENDOR_DEF, 0), 0x04);
1803 		}
1804 		/* clear flag */
1805 		regmap_write(rt712->regmap,
1806 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT0, RT712_SDCA_CTL_FUNC_STATUS, 0),
1807 				FUNCTION_NEEDS_INITIALIZATION);
1808 	}
1809 }
1810 
1811 int rt712_sdca_io_init(struct device *dev, struct sdw_slave *slave)
1812 {
1813 	struct rt712_sdca_priv *rt712 = dev_get_drvdata(dev);
1814 	unsigned int val;
1815 	struct sdw_slave_prop *prop = &slave->prop;
1816 
1817 	rt712->disable_irq = false;
1818 
1819 	if (rt712->hw_init)
1820 		return 0;
1821 
1822 	regcache_cache_only(rt712->regmap, false);
1823 	regcache_cache_only(rt712->mbq_regmap, false);
1824 	if (rt712->first_hw_init) {
1825 		regcache_cache_bypass(rt712->regmap, true);
1826 		regcache_cache_bypass(rt712->mbq_regmap, true);
1827 	} else {
1828 		/*
1829 		 *  PM runtime status is marked as 'active' only when a Slave reports as Attached
1830 		 */
1831 
1832 		/* update count of parent 'active' children */
1833 		pm_runtime_set_active(&slave->dev);
1834 	}
1835 
1836 	pm_runtime_get_noresume(&slave->dev);
1837 
1838 	rt712_sdca_index_read(rt712, RT712_VENDOR_REG, RT712_JD_PRODUCT_NUM, &val);
1839 	rt712->hw_id = (val & 0xf000) >> 12;
1840 	rt712->version_id = (val & 0x0f00) >> 8;
1841 	dev_dbg(&slave->dev, "%s hw_id=0x%x, version_id=0x%x\n", __func__, rt712->hw_id, rt712->version_id);
1842 
1843 	if (rt712->version_id == RT712_VA) {
1844 		if (rt712->dmic_function_found) {
1845 			dev_err(&slave->dev, "%s RT712 VA detected but SMART_MIC function exposed in ACPI\n",
1846 				__func__);
1847 			goto suspend;
1848 		}
1849 
1850 		rt712_sdca_va_io_init(rt712);
1851 	} else {
1852 		if (!rt712->dmic_function_found) {
1853 			dev_err(&slave->dev, "%s RT712 VB detected but no SMART_MIC function exposed in ACPI\n",
1854 				__func__);
1855 			goto suspend;
1856 		}
1857 
1858 		/* multilanes and DMIC are supported by rt712vb */
1859 		prop->lane_control_support = true;
1860 		rt712_sdca_vb_io_init(rt712);
1861 	}
1862 
1863 	/*
1864 	 * if set_jack callback occurred early than io_init,
1865 	 * we set up the jack detection function now
1866 	 */
1867 	if (rt712->hs_jack)
1868 		rt712_sdca_jack_init(rt712);
1869 
1870 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_SW_CONFIG1, 0x0001);
1871 
1872 	if (rt712->first_hw_init) {
1873 		regcache_cache_bypass(rt712->regmap, false);
1874 		regcache_mark_dirty(rt712->regmap);
1875 		regcache_cache_bypass(rt712->mbq_regmap, false);
1876 		regcache_mark_dirty(rt712->mbq_regmap);
1877 	} else
1878 		rt712->first_hw_init = true;
1879 
1880 	/* Mark Slave initialization complete */
1881 	rt712->hw_init = true;
1882 
1883 	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
1884 
1885 suspend:
1886 	pm_runtime_mark_last_busy(&slave->dev);
1887 	pm_runtime_put_autosuspend(&slave->dev);
1888 
1889 	return 0;
1890 }
1891 
1892 MODULE_DESCRIPTION("ASoC RT712 SDCA SDW driver");
1893 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1894 MODULE_LICENSE("GPL");
1895