xref: /openbmc/linux/sound/soc/codecs/rt712-sdca.c (revision d47a97bd)
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 <linux/soundwire/sdw_registers.h>
22 #include <linux/slab.h>
23 #include <sound/soc-dapm.h>
24 #include <sound/tlv.h>
25 #include "rt712-sdca.h"
26 
27 static int rt712_sdca_index_write(struct rt712_sdca_priv *rt712,
28 		unsigned int nid, unsigned int reg, unsigned int value)
29 {
30 	int ret;
31 	struct regmap *regmap = rt712->mbq_regmap;
32 	unsigned int addr = (nid << 20) | reg;
33 
34 	ret = regmap_write(regmap, addr, value);
35 	if (ret < 0)
36 		dev_err(&rt712->slave->dev,
37 			"Failed to set private value: %06x <= %04x ret=%d\n",
38 			addr, value, ret);
39 
40 	return ret;
41 }
42 
43 static int rt712_sdca_index_read(struct rt712_sdca_priv *rt712,
44 		unsigned int nid, unsigned int reg, unsigned int *value)
45 {
46 	int ret;
47 	struct regmap *regmap = rt712->mbq_regmap;
48 	unsigned int addr = (nid << 20) | reg;
49 
50 	ret = regmap_read(regmap, addr, value);
51 	if (ret < 0)
52 		dev_err(&rt712->slave->dev,
53 			"Failed to get private value: %06x => %04x ret=%d\n",
54 			addr, *value, ret);
55 
56 	return ret;
57 }
58 
59 static int rt712_sdca_index_update_bits(struct rt712_sdca_priv *rt712,
60 	unsigned int nid, unsigned int reg, unsigned int mask, unsigned int val)
61 {
62 	unsigned int tmp;
63 	int ret;
64 
65 	ret = rt712_sdca_index_read(rt712, nid, reg, &tmp);
66 	if (ret < 0)
67 		return ret;
68 
69 	set_mask_bits(&tmp, mask, val);
70 	return rt712_sdca_index_write(rt712, nid, reg, tmp);
71 }
72 
73 static int rt712_sdca_calibration(struct rt712_sdca_priv *rt712)
74 {
75 	unsigned int val, loop_rc = 0, loop_dc = 0;
76 	struct device *dev;
77 	struct regmap *regmap = rt712->regmap;
78 	int chk_cnt = 100;
79 	int ret = 0;
80 
81 	mutex_lock(&rt712->calibrate_mutex);
82 	dev = regmap_get_device(regmap);
83 
84 	/* Set HP-JD source from JD1 */
85 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_CC_DET1, 0x043a);
86 
87 	/* FSM switch to calibration manual mode */
88 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_FSM_CTL, 0x4100);
89 
90 	/* Calibration setting */
91 	rt712_sdca_index_write(rt712, RT712_VENDOR_CALI, RT712_DAC_DC_CALI_CTL1, 0x7883);
92 
93 	/* W1C Trigger DC calibration (HP & Class-D) */
94 	rt712_sdca_index_write(rt712, RT712_VENDOR_CALI, RT712_DAC_DC_CALI_CTL1, 0xf893);
95 
96 	/* wait for calibration process */
97 	rt712_sdca_index_read(rt712, RT712_VENDOR_CALI,
98 		RT712_DAC_DC_CALI_CTL1, &val);
99 
100 	for (loop_dc = 0; loop_dc < chk_cnt &&
101 		(val & RT712_DAC_DC_CALI_TRIGGER); loop_dc++) {
102 		usleep_range(10000, 11000);
103 		ret = rt712_sdca_index_read(rt712, RT712_VENDOR_CALI,
104 			RT712_DAC_DC_CALI_CTL1, &val);
105 		if (ret < 0)
106 			goto _cali_fail_;
107 	}
108 	if (loop_dc == chk_cnt)
109 		dev_err(dev, "%s, calibration time-out!\n", __func__);
110 
111 	if (loop_dc == chk_cnt || loop_rc == chk_cnt)
112 		ret = -ETIMEDOUT;
113 
114 _cali_fail_:
115 	/* Enable Rldet in FSM */
116 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_FSM_CTL, 0x4500);
117 
118 	/* Sensing Lch+Rch */
119 	rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_IMS_DIGITAL_CTL1, 0x040f);
120 
121 	/* Sine gen path control */
122 	rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_IMS_DIGITAL_CTL5, 0x0000);
123 
124 	/* Release HP-JD, EN_CBJ_TIE_GL/R open, en_osw gating auto done bit */
125 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_DIGITAL_MISC_CTRL4, 0x0010);
126 
127 	mutex_unlock(&rt712->calibrate_mutex);
128 	dev_dbg(dev, "%s calibration complete, ret=%d\n", __func__, ret);
129 	return ret;
130 }
131 
132 static unsigned int rt712_sdca_button_detect(struct rt712_sdca_priv *rt712)
133 {
134 	unsigned int btn_type = 0, offset, idx, val, owner;
135 	int ret;
136 	unsigned char buf[3];
137 
138 	/* get current UMP message owner */
139 	ret = regmap_read(rt712->regmap,
140 		SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01, RT712_SDCA_CTL_HIDTX_CURRENT_OWNER, 0),
141 		&owner);
142 	if (ret < 0)
143 		return 0;
144 
145 	/* if owner is device then there is no button event from device */
146 	if (owner == 1)
147 		return 0;
148 
149 	/* read UMP message offset */
150 	ret = regmap_read(rt712->regmap,
151 		SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01, RT712_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0),
152 		&offset);
153 	if (ret < 0)
154 		goto _end_btn_det_;
155 
156 	for (idx = 0; idx < sizeof(buf); idx++) {
157 		ret = regmap_read(rt712->regmap,
158 			RT712_BUF_ADDR_HID1 + offset + idx, &val);
159 		if (ret < 0)
160 			goto _end_btn_det_;
161 		buf[idx] = val & 0xff;
162 	}
163 
164 	if (buf[0] == 0x11) {
165 		switch (buf[1] & 0xf0) {
166 		case 0x10:
167 			btn_type |= SND_JACK_BTN_2;
168 			break;
169 		case 0x20:
170 			btn_type |= SND_JACK_BTN_3;
171 			break;
172 		case 0x40:
173 			btn_type |= SND_JACK_BTN_0;
174 			break;
175 		case 0x80:
176 			btn_type |= SND_JACK_BTN_1;
177 			break;
178 		}
179 		switch (buf[2]) {
180 		case 0x01:
181 		case 0x10:
182 			btn_type |= SND_JACK_BTN_2;
183 			break;
184 		case 0x02:
185 		case 0x20:
186 			btn_type |= SND_JACK_BTN_3;
187 			break;
188 		case 0x04:
189 		case 0x40:
190 			btn_type |= SND_JACK_BTN_0;
191 			break;
192 		case 0x08:
193 		case 0x80:
194 			btn_type |= SND_JACK_BTN_1;
195 			break;
196 		}
197 	}
198 
199 _end_btn_det_:
200 	/* Host is owner, so set back to device */
201 	if (owner == 0)
202 		/* set owner to device */
203 		regmap_write(rt712->regmap,
204 			SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01,
205 				RT712_SDCA_CTL_HIDTX_SET_OWNER_TO_DEVICE, 0), 0x01);
206 
207 	return btn_type;
208 }
209 
210 static int rt712_sdca_headset_detect(struct rt712_sdca_priv *rt712)
211 {
212 	unsigned int det_mode;
213 	int ret;
214 
215 	/* get detected_mode */
216 	ret = regmap_read(rt712->regmap,
217 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_GE49, RT712_SDCA_CTL_DETECTED_MODE, 0),
218 		&det_mode);
219 	if (ret < 0)
220 		goto io_error;
221 
222 	switch (det_mode) {
223 	case 0x00:
224 		rt712->jack_type = 0;
225 		break;
226 	case 0x03:
227 		rt712->jack_type = SND_JACK_HEADPHONE;
228 		break;
229 	case 0x05:
230 		rt712->jack_type = SND_JACK_HEADSET;
231 		break;
232 	}
233 
234 	/* write selected_mode */
235 	if (det_mode) {
236 		ret = regmap_write(rt712->regmap,
237 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_GE49, RT712_SDCA_CTL_SELECTED_MODE, 0),
238 			det_mode);
239 		if (ret < 0)
240 			goto io_error;
241 	}
242 
243 	dev_dbg(&rt712->slave->dev,
244 		"%s, detected_mode=0x%x\n", __func__, det_mode);
245 
246 	return 0;
247 
248 io_error:
249 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
250 	return ret;
251 }
252 
253 static void rt712_sdca_jack_detect_handler(struct work_struct *work)
254 {
255 	struct rt712_sdca_priv *rt712 =
256 		container_of(work, struct rt712_sdca_priv, jack_detect_work.work);
257 	int btn_type = 0, ret;
258 
259 	if (!rt712->hs_jack)
260 		return;
261 
262 	if (!rt712->component->card || !rt712->component->card->instantiated)
263 		return;
264 
265 	/* SDW_SCP_SDCA_INT_SDCA_0 is used for jack detection */
266 	if (rt712->scp_sdca_stat1 & SDW_SCP_SDCA_INT_SDCA_0) {
267 		ret = rt712_sdca_headset_detect(rt712);
268 		if (ret < 0)
269 			return;
270 	}
271 
272 	/* SDW_SCP_SDCA_INT_SDCA_8 is used for button detection */
273 	if (rt712->scp_sdca_stat2 & SDW_SCP_SDCA_INT_SDCA_8)
274 		btn_type = rt712_sdca_button_detect(rt712);
275 
276 	if (rt712->jack_type == 0)
277 		btn_type = 0;
278 
279 	dev_dbg(&rt712->slave->dev,
280 		"in %s, jack_type=0x%x\n", __func__, rt712->jack_type);
281 	dev_dbg(&rt712->slave->dev,
282 		"in %s, btn_type=0x%x\n", __func__, btn_type);
283 	dev_dbg(&rt712->slave->dev,
284 		"in %s, scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__,
285 		rt712->scp_sdca_stat1, rt712->scp_sdca_stat2);
286 
287 	snd_soc_jack_report(rt712->hs_jack, rt712->jack_type | btn_type,
288 			SND_JACK_HEADSET |
289 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
290 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
291 
292 	if (btn_type) {
293 		/* button released */
294 		snd_soc_jack_report(rt712->hs_jack, rt712->jack_type,
295 			SND_JACK_HEADSET |
296 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
297 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
298 
299 		mod_delayed_work(system_power_efficient_wq,
300 			&rt712->jack_btn_check_work, msecs_to_jiffies(200));
301 	}
302 }
303 
304 static void rt712_sdca_btn_check_handler(struct work_struct *work)
305 {
306 	struct rt712_sdca_priv *rt712 =
307 		container_of(work, struct rt712_sdca_priv, jack_btn_check_work.work);
308 	int btn_type = 0, ret, idx;
309 	unsigned int det_mode, offset, val;
310 	unsigned char buf[3];
311 
312 	ret = regmap_read(rt712->regmap,
313 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_GE49, RT712_SDCA_CTL_DETECTED_MODE, 0),
314 		&det_mode);
315 	if (ret < 0)
316 		goto io_error;
317 
318 	/* pin attached */
319 	if (det_mode) {
320 		/* read UMP message offset */
321 		ret = regmap_read(rt712->regmap,
322 			SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01, RT712_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0),
323 			&offset);
324 		if (ret < 0)
325 			goto io_error;
326 
327 		for (idx = 0; idx < sizeof(buf); idx++) {
328 			ret = regmap_read(rt712->regmap,
329 				RT712_BUF_ADDR_HID1 + offset + idx, &val);
330 			if (ret < 0)
331 				goto io_error;
332 			buf[idx] = val & 0xff;
333 		}
334 
335 		if (buf[0] == 0x11) {
336 			switch (buf[1] & 0xf0) {
337 			case 0x10:
338 				btn_type |= SND_JACK_BTN_2;
339 				break;
340 			case 0x20:
341 				btn_type |= SND_JACK_BTN_3;
342 				break;
343 			case 0x40:
344 				btn_type |= SND_JACK_BTN_0;
345 				break;
346 			case 0x80:
347 				btn_type |= SND_JACK_BTN_1;
348 				break;
349 			}
350 			switch (buf[2]) {
351 			case 0x01:
352 			case 0x10:
353 				btn_type |= SND_JACK_BTN_2;
354 				break;
355 			case 0x02:
356 			case 0x20:
357 				btn_type |= SND_JACK_BTN_3;
358 				break;
359 			case 0x04:
360 			case 0x40:
361 				btn_type |= SND_JACK_BTN_0;
362 				break;
363 			case 0x08:
364 			case 0x80:
365 				btn_type |= SND_JACK_BTN_1;
366 				break;
367 			}
368 		}
369 	} else {
370 		rt712->jack_type = 0;
371 	}
372 
373 	dev_dbg(&rt712->slave->dev, "%s, btn_type=0x%x\n",	__func__, btn_type);
374 	snd_soc_jack_report(rt712->hs_jack, rt712->jack_type | btn_type,
375 			SND_JACK_HEADSET |
376 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
377 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
378 
379 	if (btn_type) {
380 		/* button released */
381 		snd_soc_jack_report(rt712->hs_jack, rt712->jack_type,
382 			SND_JACK_HEADSET |
383 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
384 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
385 
386 		mod_delayed_work(system_power_efficient_wq,
387 			&rt712->jack_btn_check_work, msecs_to_jiffies(200));
388 	}
389 
390 	return;
391 
392 io_error:
393 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
394 }
395 
396 static void rt712_sdca_jack_init(struct rt712_sdca_priv *rt712)
397 {
398 	mutex_lock(&rt712->calibrate_mutex);
399 
400 	if (rt712->hs_jack) {
401 		/* Enable HID1 event & set button RTC mode */
402 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
403 			RT712_UMP_HID_CTL5, 0xfff0);
404 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
405 			RT712_UMP_HID_CTL0, 0x1100, 0x1100);
406 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
407 			RT712_UMP_HID_CTL7, 0xf000, 0x0000);
408 
409 		/* detected_mode_change_event_en & hid1_push_button_event_en */
410 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
411 			RT712_GE_RELATED_CTL1, 0x0c00, 0x0c00);
412 		/* ge_inbox_en */
413 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
414 			RT712_GE_RELATED_CTL2, 0x0020, 0x0000);
415 
416 		switch (rt712->jd_src) {
417 		case RT712_JD1:
418 			/* Set HP-JD source from JD1 */
419 			rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_CC_DET1, 0x043a);
420 			break;
421 		default:
422 			dev_warn(rt712->component->dev, "Wrong JD source\n");
423 			break;
424 		}
425 
426 		/* set SCP_SDCA_IntMask1[0]=1 */
427 		sdw_write_no_pm(rt712->slave, SDW_SCP_SDCA_INTMASK1, SDW_SCP_SDCA_INTMASK_SDCA_0);
428 		/* set SCP_SDCA_IntMask2[0]=1 */
429 		sdw_write_no_pm(rt712->slave, SDW_SCP_SDCA_INTMASK2, SDW_SCP_SDCA_INTMASK_SDCA_8);
430 		dev_dbg(&rt712->slave->dev, "in %s enable\n", __func__);
431 
432 		/* trigger GE interrupt */
433 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
434 			RT712_GE_RELATED_CTL1, 0x0080, 0x0080);
435 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
436 			RT712_GE_RELATED_CTL1, 0x0080, 0x0000);
437 	} else {
438 		/* disable HID1 & detected_mode_change event */
439 		rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
440 			RT712_GE_RELATED_CTL1, 0x0c00, 0x0000);
441 
442 		dev_dbg(&rt712->slave->dev, "in %s disable\n", __func__);
443 	}
444 
445 	mutex_unlock(&rt712->calibrate_mutex);
446 }
447 
448 static int rt712_sdca_set_jack_detect(struct snd_soc_component *component,
449 	struct snd_soc_jack *hs_jack, void *data)
450 {
451 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
452 	int ret;
453 
454 	rt712->hs_jack = hs_jack;
455 
456 	ret = pm_runtime_resume_and_get(component->dev);
457 	if (ret < 0) {
458 		if (ret != -EACCES) {
459 			dev_err(component->dev, "%s: failed to resume %d\n", __func__, ret);
460 			return ret;
461 		}
462 
463 		/* pm_runtime not enabled yet */
464 		dev_dbg(component->dev,	"%s: skipping jack init for now\n", __func__);
465 		return 0;
466 	}
467 
468 	rt712_sdca_jack_init(rt712);
469 
470 	pm_runtime_mark_last_busy(component->dev);
471 	pm_runtime_put_autosuspend(component->dev);
472 
473 	return 0;
474 }
475 
476 /* For SDCA control DAC/ADC Gain */
477 static int rt712_sdca_set_gain_put(struct snd_kcontrol *kcontrol,
478 		struct snd_ctl_elem_value *ucontrol)
479 {
480 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
481 	struct soc_mixer_control *mc =
482 		(struct soc_mixer_control *)kcontrol->private_value;
483 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
484 	unsigned int read_l, read_r, gain_l_val, gain_r_val;
485 	unsigned int adc_vol_flag = 0;
486 	unsigned int lvalue, rvalue;
487 	const unsigned int interval_offset = 0xc0;
488 	const unsigned int tendB = 0xa00;
489 
490 	if (strstr(ucontrol->id.name, "FU0F Capture Volume"))
491 		adc_vol_flag = 1;
492 
493 	regmap_read(rt712->mbq_regmap, mc->reg, &lvalue);
494 	regmap_read(rt712->mbq_regmap, mc->rreg, &rvalue);
495 
496 	/* L Channel */
497 	gain_l_val = ucontrol->value.integer.value[0];
498 	if (gain_l_val > mc->max)
499 		gain_l_val = mc->max;
500 
501 	if (mc->shift == 8) /* boost gain */
502 		gain_l_val = gain_l_val * tendB;
503 	else {
504 		/* ADC/DAC gain */
505 		if (adc_vol_flag)
506 			gain_l_val = 0x1e00 - ((mc->max - gain_l_val) * interval_offset);
507 		else
508 			gain_l_val = 0 - ((mc->max - gain_l_val) * interval_offset);
509 		gain_l_val &= 0xffff;
510 	}
511 
512 	/* R Channel */
513 	gain_r_val = ucontrol->value.integer.value[1];
514 	if (gain_r_val > mc->max)
515 		gain_r_val = mc->max;
516 
517 	if (mc->shift == 8) /* boost gain */
518 		gain_r_val = gain_r_val * tendB;
519 	else {
520 		/* ADC/DAC gain */
521 		if (adc_vol_flag)
522 			gain_r_val = 0x1e00 - ((mc->max - gain_r_val) * interval_offset);
523 		else
524 			gain_r_val = 0 - ((mc->max - gain_r_val) * interval_offset);
525 		gain_r_val &= 0xffff;
526 	}
527 
528 	if (lvalue == gain_l_val && rvalue == gain_r_val)
529 		return 0;
530 
531 	/* Lch*/
532 	regmap_write(rt712->mbq_regmap, mc->reg, gain_l_val);
533 	/* Rch */
534 	regmap_write(rt712->mbq_regmap, mc->rreg, gain_r_val);
535 
536 	regmap_read(rt712->mbq_regmap, mc->reg, &read_l);
537 	regmap_read(rt712->mbq_regmap, mc->rreg, &read_r);
538 	if (read_r == gain_r_val && read_l == gain_l_val)
539 		return 1;
540 
541 	return -EIO;
542 }
543 
544 static int rt712_sdca_set_gain_get(struct snd_kcontrol *kcontrol,
545 		struct snd_ctl_elem_value *ucontrol)
546 {
547 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
548 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
549 	struct soc_mixer_control *mc =
550 		(struct soc_mixer_control *)kcontrol->private_value;
551 	unsigned int read_l, read_r, ctl_l = 0, ctl_r = 0;
552 	unsigned int adc_vol_flag = 0;
553 	const unsigned int interval_offset = 0xc0;
554 	const unsigned int tendB = 0xa00;
555 
556 	if (strstr(ucontrol->id.name, "FU0F Capture Volume"))
557 		adc_vol_flag = 1;
558 
559 	regmap_read(rt712->mbq_regmap, mc->reg, &read_l);
560 	regmap_read(rt712->mbq_regmap, mc->rreg, &read_r);
561 
562 	if (mc->shift == 8) /* boost gain */
563 		ctl_l = read_l / tendB;
564 	else {
565 		if (adc_vol_flag)
566 			ctl_l = mc->max - (((0x1e00 - read_l) & 0xffff) / interval_offset);
567 		else
568 			ctl_l = mc->max - (((0 - read_l) & 0xffff) / interval_offset);
569 	}
570 
571 	if (read_l != read_r) {
572 		if (mc->shift == 8) /* boost gain */
573 			ctl_r = read_r / tendB;
574 		else { /* ADC/DAC gain */
575 			if (adc_vol_flag)
576 				ctl_r = mc->max - (((0x1e00 - read_r) & 0xffff) / interval_offset);
577 			else
578 				ctl_r = mc->max - (((0 - read_r) & 0xffff) / interval_offset);
579 		}
580 	} else
581 		ctl_r = ctl_l;
582 
583 	ucontrol->value.integer.value[0] = ctl_l;
584 	ucontrol->value.integer.value[1] = ctl_r;
585 
586 	return 0;
587 }
588 
589 static int rt712_sdca_set_fu0f_capture_ctl(struct rt712_sdca_priv *rt712)
590 {
591 	int err;
592 	unsigned int ch_l, ch_r;
593 
594 	ch_l = (rt712->fu0f_dapm_mute || rt712->fu0f_mixer_l_mute) ? 0x01 : 0x00;
595 	ch_r = (rt712->fu0f_dapm_mute || rt712->fu0f_mixer_r_mute) ? 0x01 : 0x00;
596 
597 	err = regmap_write(rt712->regmap,
598 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F,
599 			RT712_SDCA_CTL_FU_MUTE, CH_L), ch_l);
600 	if (err < 0)
601 		return err;
602 
603 	err = regmap_write(rt712->regmap,
604 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F,
605 			RT712_SDCA_CTL_FU_MUTE, CH_R), ch_r);
606 	if (err < 0)
607 		return err;
608 
609 	return 0;
610 }
611 
612 static int rt712_sdca_fu0f_capture_get(struct snd_kcontrol *kcontrol,
613 			struct snd_ctl_elem_value *ucontrol)
614 {
615 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
616 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
617 
618 	ucontrol->value.integer.value[0] = !rt712->fu0f_mixer_l_mute;
619 	ucontrol->value.integer.value[1] = !rt712->fu0f_mixer_r_mute;
620 	return 0;
621 }
622 
623 static int rt712_sdca_fu0f_capture_put(struct snd_kcontrol *kcontrol,
624 			struct snd_ctl_elem_value *ucontrol)
625 {
626 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
627 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
628 	int err;
629 
630 	if (rt712->fu0f_mixer_l_mute == !ucontrol->value.integer.value[0] &&
631 		rt712->fu0f_mixer_r_mute == !ucontrol->value.integer.value[1])
632 		return 0;
633 
634 	rt712->fu0f_mixer_l_mute = !ucontrol->value.integer.value[0];
635 	rt712->fu0f_mixer_r_mute = !ucontrol->value.integer.value[1];
636 	err = rt712_sdca_set_fu0f_capture_ctl(rt712);
637 	if (err < 0)
638 		return err;
639 
640 	return 1;
641 }
642 
643 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0);
644 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, -1725, 75, 0);
645 static const DECLARE_TLV_DB_SCALE(boost_vol_tlv, 0, 1000, 0);
646 
647 static const struct snd_kcontrol_new rt712_sdca_controls[] = {
648 	SOC_DOUBLE_R_EXT_TLV("FU05 Playback Volume",
649 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05, RT712_SDCA_CTL_FU_VOLUME, CH_L),
650 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05, RT712_SDCA_CTL_FU_VOLUME, CH_R),
651 		0, 0x57, 0,
652 		rt712_sdca_set_gain_get, rt712_sdca_set_gain_put, out_vol_tlv),
653 	SOC_DOUBLE_EXT("FU0F Capture Switch", SND_SOC_NOPM, 0, 1, 1, 0,
654 		rt712_sdca_fu0f_capture_get, rt712_sdca_fu0f_capture_put),
655 	SOC_DOUBLE_R_EXT_TLV("FU0F Capture Volume",
656 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F, RT712_SDCA_CTL_FU_VOLUME, CH_L),
657 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F, RT712_SDCA_CTL_FU_VOLUME, CH_R),
658 		0, 0x3f, 0,
659 		rt712_sdca_set_gain_get, rt712_sdca_set_gain_put, mic_vol_tlv),
660 	SOC_DOUBLE_R_EXT_TLV("FU44 Boost Volume",
661 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PLATFORM_FU44, RT712_SDCA_CTL_FU_CH_GAIN, CH_L),
662 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PLATFORM_FU44, RT712_SDCA_CTL_FU_CH_GAIN, CH_R),
663 		8, 3, 0,
664 		rt712_sdca_set_gain_get, rt712_sdca_set_gain_put, boost_vol_tlv),
665 };
666 
667 static const struct snd_kcontrol_new rt712_sdca_spk_controls[] = {
668 	SOC_DOUBLE_R_EXT_TLV("FU06 Playback Volume",
669 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_VOLUME, CH_L),
670 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_VOLUME, CH_R),
671 		0, 0x57, 0,
672 		rt712_sdca_set_gain_get, rt712_sdca_set_gain_put, out_vol_tlv),
673 };
674 
675 static int rt712_sdca_mux_get(struct snd_kcontrol *kcontrol,
676 			struct snd_ctl_elem_value *ucontrol)
677 {
678 	struct snd_soc_component *component =
679 		snd_soc_dapm_kcontrol_component(kcontrol);
680 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
681 	unsigned int val = 0, mask = 0x3300;
682 
683 	rt712_sdca_index_read(rt712, RT712_VENDOR_HDA_CTL, RT712_MIXER_CTL1, &val);
684 
685 	val = val & mask;
686 	switch (val) {
687 	case 0x3000:
688 		val = 1;
689 		break;
690 	case 0x0300:
691 		val = 0;
692 		break;
693 	}
694 
695 	ucontrol->value.enumerated.item[0] = val;
696 
697 	return 0;
698 }
699 
700 static int rt712_sdca_mux_put(struct snd_kcontrol *kcontrol,
701 			struct snd_ctl_elem_value *ucontrol)
702 {
703 	struct snd_soc_component *component =
704 		snd_soc_dapm_kcontrol_component(kcontrol);
705 	struct snd_soc_dapm_context *dapm =
706 		snd_soc_dapm_kcontrol_dapm(kcontrol);
707 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
708 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
709 	unsigned int *item = ucontrol->value.enumerated.item;
710 	unsigned int mask_sft;
711 	unsigned int val;
712 
713 	if (item[0] >= e->items)
714 		return -EINVAL;
715 
716 	if (ucontrol->value.enumerated.item[0] == 0)
717 		mask_sft = 12;
718 	else if (ucontrol->value.enumerated.item[0] == 1)
719 		mask_sft = 8;
720 	else
721 		return -EINVAL;
722 
723 	rt712_sdca_index_read(rt712, RT712_VENDOR_HDA_CTL, RT712_MIXER_CTL1, &val);
724 	val = (val >> mask_sft) & 0x3;
725 	if (!val)
726 		return 0;
727 
728 	rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
729 		RT712_MIXER_CTL1, 0x3fff);
730 	rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
731 		RT712_MIXER_CTL1, 0x3 << mask_sft, 0);
732 
733 	snd_soc_dapm_mux_update_power(dapm, kcontrol,
734 		item[0], e, NULL);
735 
736 	return 1;
737 }
738 
739 static const char * const adc_mux_text[] = {
740 	"MIC2",
741 	"LINE2",
742 };
743 
744 static SOC_ENUM_SINGLE_DECL(
745 	rt712_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text);
746 
747 static const struct snd_kcontrol_new rt712_sdca_adc23_mux =
748 	SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt712_adc23_enum,
749 			rt712_sdca_mux_get, rt712_sdca_mux_put);
750 
751 static int rt712_sdca_fu05_event(struct snd_soc_dapm_widget *w,
752 	struct snd_kcontrol *kcontrol, int event)
753 {
754 	struct snd_soc_component *component =
755 		snd_soc_dapm_to_component(w->dapm);
756 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
757 	unsigned char unmute = 0x0, mute = 0x1;
758 
759 	switch (event) {
760 	case SND_SOC_DAPM_POST_PMU:
761 		regmap_write(rt712->regmap,
762 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05,
763 				RT712_SDCA_CTL_FU_MUTE, CH_L),
764 				unmute);
765 		regmap_write(rt712->regmap,
766 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05,
767 				RT712_SDCA_CTL_FU_MUTE, CH_R),
768 				unmute);
769 		break;
770 	case SND_SOC_DAPM_PRE_PMD:
771 		regmap_write(rt712->regmap,
772 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05,
773 				RT712_SDCA_CTL_FU_MUTE, CH_L),
774 				mute);
775 		regmap_write(rt712->regmap,
776 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05,
777 				RT712_SDCA_CTL_FU_MUTE, CH_R),
778 				mute);
779 		break;
780 	}
781 	return 0;
782 }
783 
784 static int rt712_sdca_fu0f_event(struct snd_soc_dapm_widget *w,
785 	struct snd_kcontrol *kcontrol, int event)
786 {
787 	struct snd_soc_component *component =
788 		snd_soc_dapm_to_component(w->dapm);
789 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
790 
791 	switch (event) {
792 	case SND_SOC_DAPM_POST_PMU:
793 		rt712->fu0f_dapm_mute = false;
794 		rt712_sdca_set_fu0f_capture_ctl(rt712);
795 		break;
796 	case SND_SOC_DAPM_PRE_PMD:
797 		rt712->fu0f_dapm_mute = true;
798 		rt712_sdca_set_fu0f_capture_ctl(rt712);
799 		break;
800 	}
801 	return 0;
802 }
803 
804 static int rt712_sdca_pde40_event(struct snd_soc_dapm_widget *w,
805 	struct snd_kcontrol *kcontrol, int event)
806 {
807 	struct snd_soc_component *component =
808 		snd_soc_dapm_to_component(w->dapm);
809 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
810 	unsigned char ps0 = 0x0, ps3 = 0x3;
811 
812 	switch (event) {
813 	case SND_SOC_DAPM_POST_PMU:
814 		regmap_write(rt712->regmap,
815 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PDE40,
816 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
817 				ps0);
818 		break;
819 	case SND_SOC_DAPM_PRE_PMD:
820 		regmap_write(rt712->regmap,
821 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PDE40,
822 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
823 				ps3);
824 		break;
825 	}
826 	return 0;
827 }
828 
829 static int rt712_sdca_pde12_event(struct snd_soc_dapm_widget *w,
830 	struct snd_kcontrol *kcontrol, int event)
831 {
832 	struct snd_soc_component *component =
833 		snd_soc_dapm_to_component(w->dapm);
834 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
835 	unsigned char ps0 = 0x0, ps3 = 0x3;
836 
837 	switch (event) {
838 	case SND_SOC_DAPM_POST_PMU:
839 		regmap_write(rt712->regmap,
840 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PDE12,
841 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
842 				ps0);
843 		break;
844 	case SND_SOC_DAPM_PRE_PMD:
845 		regmap_write(rt712->regmap,
846 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PDE12,
847 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
848 				ps3);
849 		break;
850 	}
851 	return 0;
852 }
853 
854 static int rt712_sdca_classd_event(struct snd_soc_dapm_widget *w,
855 	struct snd_kcontrol *kcontrol, int event)
856 {
857 	struct snd_soc_component *component =
858 		snd_soc_dapm_to_component(w->dapm);
859 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
860 	unsigned char ps0 = 0x0, ps3 = 0x3;
861 
862 	switch (event) {
863 	case SND_SOC_DAPM_POST_PMU:
864 		regmap_write(rt712->regmap,
865 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_PDE23,
866 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
867 				ps0);
868 		break;
869 	case SND_SOC_DAPM_PRE_PMD:
870 		regmap_write(rt712->regmap,
871 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_PDE23,
872 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
873 				ps3);
874 		break;
875 
876 	default:
877 		break;
878 	}
879 
880 	return 0;
881 }
882 
883 static const struct snd_kcontrol_new rt712_spk_sto_dac =
884 	SOC_DAPM_DOUBLE_R("Switch",
885 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_MUTE, CH_L),
886 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_MUTE, CH_R),
887 		0, 1, 1);
888 
889 static const struct snd_soc_dapm_widget rt712_sdca_dapm_widgets[] = {
890 	SND_SOC_DAPM_OUTPUT("HP"),
891 	SND_SOC_DAPM_INPUT("MIC2"),
892 	SND_SOC_DAPM_INPUT("LINE2"),
893 
894 	SND_SOC_DAPM_SUPPLY("PDE 40", SND_SOC_NOPM, 0, 0,
895 		rt712_sdca_pde40_event,
896 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
897 	SND_SOC_DAPM_SUPPLY("PDE 12", SND_SOC_NOPM, 0, 0,
898 		rt712_sdca_pde12_event,
899 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
900 
901 	SND_SOC_DAPM_DAC_E("FU 05", NULL, SND_SOC_NOPM, 0, 0,
902 		rt712_sdca_fu05_event,
903 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
904 	SND_SOC_DAPM_ADC_E("FU 0F", NULL, SND_SOC_NOPM, 0, 0,
905 		rt712_sdca_fu0f_event,
906 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
907 	SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0,
908 		&rt712_sdca_adc23_mux),
909 
910 	SND_SOC_DAPM_AIF_IN("DP1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0),
911 	SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0),
912 };
913 
914 static const struct snd_soc_dapm_route rt712_sdca_audio_map[] = {
915 	{ "FU 05", NULL, "DP1RX" },
916 	{ "DP4TX", NULL, "FU 0F" },
917 
918 	{ "FU 0F", NULL, "PDE 12" },
919 	{ "FU 0F", NULL, "ADC 23 Mux" },
920 	{ "ADC 23 Mux", "LINE2", "LINE2" },
921 	{ "ADC 23 Mux", "MIC2", "MIC2" },
922 
923 	{ "HP", NULL, "PDE 40" },
924 	{ "HP", NULL, "FU 05" },
925 };
926 
927 static const struct snd_soc_dapm_widget rt712_sdca_spk_dapm_widgets[] = {
928 	SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0),
929 
930 	/* Digital Interface */
931 	SND_SOC_DAPM_SWITCH("FU06", SND_SOC_NOPM, 0, 0, &rt712_spk_sto_dac),
932 
933 	/* Output */
934 	SND_SOC_DAPM_PGA_E("CLASS D", SND_SOC_NOPM, 0, 0, NULL, 0,
935 		rt712_sdca_classd_event, SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
936 	SND_SOC_DAPM_OUTPUT("SPOL"),
937 	SND_SOC_DAPM_OUTPUT("SPOR"),
938 };
939 
940 static const struct snd_soc_dapm_route rt712_sdca_spk_dapm_routes[] = {
941 	{ "FU06", "Switch", "DP3RX" },
942 	{ "CLASS D", NULL, "FU06" },
943 	{ "SPOL", NULL, "CLASS D" },
944 	{ "SPOR", NULL, "CLASS D" },
945 };
946 
947 static int rt712_sdca_parse_dt(struct rt712_sdca_priv *rt712, struct device *dev)
948 {
949 	device_property_read_u32(dev, "realtek,jd-src", &rt712->jd_src);
950 
951 	return 0;
952 }
953 
954 static int rt712_sdca_probe(struct snd_soc_component *component)
955 {
956 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
957 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
958 	int ret;
959 
960 	rt712_sdca_parse_dt(rt712, &rt712->slave->dev);
961 	rt712->component = component;
962 
963 	ret = pm_runtime_resume(component->dev);
964 	if (ret < 0 && ret != -EACCES)
965 		return ret;
966 
967 	/* add SPK route */
968 	if (rt712->hw_id != RT712_DEV_ID_713) {
969 		snd_soc_add_component_controls(component,
970 			rt712_sdca_spk_controls, ARRAY_SIZE(rt712_sdca_spk_controls));
971 		snd_soc_dapm_new_controls(dapm,
972 			rt712_sdca_spk_dapm_widgets, ARRAY_SIZE(rt712_sdca_spk_dapm_widgets));
973 		snd_soc_dapm_add_routes(dapm,
974 			rt712_sdca_spk_dapm_routes, ARRAY_SIZE(rt712_sdca_spk_dapm_routes));
975 	}
976 
977 	return 0;
978 }
979 
980 static const struct snd_soc_component_driver soc_sdca_dev_rt712 = {
981 	.probe = rt712_sdca_probe,
982 	.controls = rt712_sdca_controls,
983 	.num_controls = ARRAY_SIZE(rt712_sdca_controls),
984 	.dapm_widgets = rt712_sdca_dapm_widgets,
985 	.num_dapm_widgets = ARRAY_SIZE(rt712_sdca_dapm_widgets),
986 	.dapm_routes = rt712_sdca_audio_map,
987 	.num_dapm_routes = ARRAY_SIZE(rt712_sdca_audio_map),
988 	.set_jack = rt712_sdca_set_jack_detect,
989 	.endianness = 1,
990 };
991 
992 static int rt712_sdca_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
993 				int direction)
994 {
995 	struct sdw_stream_data *stream;
996 
997 	if (!sdw_stream)
998 		return 0;
999 
1000 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
1001 	if (!stream)
1002 		return -ENOMEM;
1003 
1004 	stream->sdw_stream = sdw_stream;
1005 
1006 	/* Use tx_mask or rx_mask to configure stream tag and set dma_data */
1007 	snd_soc_dai_dma_data_set(dai, direction, stream);
1008 
1009 	return 0;
1010 }
1011 
1012 static void rt712_sdca_shutdown(struct snd_pcm_substream *substream,
1013 				struct snd_soc_dai *dai)
1014 {
1015 	struct sdw_stream_data *stream;
1016 
1017 	stream = snd_soc_dai_get_dma_data(dai, substream);
1018 	snd_soc_dai_set_dma_data(dai, substream, NULL);
1019 	kfree(stream);
1020 }
1021 
1022 static int rt712_sdca_pcm_hw_params(struct snd_pcm_substream *substream,
1023 				struct snd_pcm_hw_params *params,
1024 				struct snd_soc_dai *dai)
1025 {
1026 	struct snd_soc_component *component = dai->component;
1027 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1028 	struct sdw_stream_config stream_config;
1029 	struct sdw_port_config port_config;
1030 	enum sdw_data_direction direction;
1031 	struct sdw_stream_data *stream;
1032 	int retval, port, num_channels;
1033 	unsigned int sampling_rate;
1034 
1035 	dev_dbg(dai->dev, "%s %s", __func__, dai->name);
1036 	stream = snd_soc_dai_get_dma_data(dai, substream);
1037 
1038 	if (!stream)
1039 		return -EINVAL;
1040 
1041 	if (!rt712->slave)
1042 		return -EINVAL;
1043 
1044 	/* SoundWire specific configuration */
1045 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1046 		direction = SDW_DATA_DIR_RX;
1047 		if (dai->id == RT712_AIF1)
1048 			port = 1;
1049 		else if (dai->id == RT712_AIF2)
1050 			port = 3;
1051 		else
1052 			return -EINVAL;
1053 	} else {
1054 		direction = SDW_DATA_DIR_TX;
1055 		if (dai->id == RT712_AIF1)
1056 			port = 4;
1057 		else
1058 			return -EINVAL;
1059 	}
1060 
1061 	stream_config.frame_rate = params_rate(params);
1062 	stream_config.ch_count = params_channels(params);
1063 	stream_config.bps = snd_pcm_format_width(params_format(params));
1064 	stream_config.direction = direction;
1065 
1066 	num_channels = params_channels(params);
1067 	port_config.ch_mask = GENMASK(num_channels - 1, 0);
1068 	port_config.num = port;
1069 
1070 	retval = sdw_stream_add_slave(rt712->slave, &stream_config,
1071 					&port_config, 1, stream->sdw_stream);
1072 	if (retval) {
1073 		dev_err(dai->dev, "Unable to configure port\n");
1074 		return retval;
1075 	}
1076 
1077 	if (params_channels(params) > 16) {
1078 		dev_err(component->dev, "Unsupported channels %d\n",
1079 			params_channels(params));
1080 		return -EINVAL;
1081 	}
1082 
1083 	/* sampling rate configuration */
1084 	switch (params_rate(params)) {
1085 	case 44100:
1086 		sampling_rate = RT712_SDCA_RATE_44100HZ;
1087 		break;
1088 	case 48000:
1089 		sampling_rate = RT712_SDCA_RATE_48000HZ;
1090 		break;
1091 	case 96000:
1092 		sampling_rate = RT712_SDCA_RATE_96000HZ;
1093 		break;
1094 	case 192000:
1095 		sampling_rate = RT712_SDCA_RATE_192000HZ;
1096 		break;
1097 	default:
1098 		dev_err(component->dev, "Rate %d is not supported\n",
1099 			params_rate(params));
1100 		return -EINVAL;
1101 	}
1102 
1103 	/* set sampling frequency */
1104 	switch (dai->id) {
1105 	case RT712_AIF1:
1106 		regmap_write(rt712->regmap,
1107 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_CS01, RT712_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1108 			sampling_rate);
1109 		regmap_write(rt712->regmap,
1110 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_CS11, RT712_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1111 			sampling_rate);
1112 		break;
1113 	case RT712_AIF2:
1114 		regmap_write(rt712->regmap,
1115 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_CS31, RT712_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1116 			sampling_rate);
1117 		break;
1118 	default:
1119 		dev_err(component->dev, "Wrong DAI id\n");
1120 		return -EINVAL;
1121 	}
1122 
1123 	return 0;
1124 }
1125 
1126 static int rt712_sdca_pcm_hw_free(struct snd_pcm_substream *substream,
1127 				struct snd_soc_dai *dai)
1128 {
1129 	struct snd_soc_component *component = dai->component;
1130 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1131 	struct sdw_stream_data *stream =
1132 		snd_soc_dai_get_dma_data(dai, substream);
1133 
1134 	if (!rt712->slave)
1135 		return -EINVAL;
1136 
1137 	sdw_stream_remove_slave(rt712->slave, stream->sdw_stream);
1138 	return 0;
1139 }
1140 
1141 #define RT712_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
1142 			SNDRV_PCM_RATE_192000)
1143 #define RT712_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1144 			SNDRV_PCM_FMTBIT_S24_LE)
1145 
1146 static const struct snd_soc_dai_ops rt712_sdca_ops = {
1147 	.hw_params	= rt712_sdca_pcm_hw_params,
1148 	.hw_free	= rt712_sdca_pcm_hw_free,
1149 	.set_stream	= rt712_sdca_set_sdw_stream,
1150 	.shutdown	= rt712_sdca_shutdown,
1151 };
1152 
1153 static struct snd_soc_dai_driver rt712_sdca_dai[] = {
1154 	{
1155 		.name = "rt712-sdca-aif1",
1156 		.id = RT712_AIF1,
1157 		.playback = {
1158 			.stream_name = "DP1 Playback",
1159 			.channels_min = 1,
1160 			.channels_max = 2,
1161 			.rates = RT712_STEREO_RATES,
1162 			.formats = RT712_FORMATS,
1163 		},
1164 		.capture = {
1165 			.stream_name = "DP4 Capture",
1166 			.channels_min = 1,
1167 			.channels_max = 2,
1168 			.rates = RT712_STEREO_RATES,
1169 			.formats = RT712_FORMATS,
1170 		},
1171 		.ops = &rt712_sdca_ops,
1172 	},
1173 	{
1174 		.name = "rt712-sdca-aif2",
1175 		.id = RT712_AIF2,
1176 		.playback = {
1177 			.stream_name = "DP3 Playback",
1178 			.channels_min = 1,
1179 			.channels_max = 2,
1180 			.rates = RT712_STEREO_RATES,
1181 			.formats = RT712_FORMATS,
1182 		},
1183 		.ops = &rt712_sdca_ops,
1184 	}
1185 };
1186 
1187 int rt712_sdca_init(struct device *dev, struct regmap *regmap,
1188 			struct regmap *mbq_regmap, struct sdw_slave *slave)
1189 {
1190 	struct rt712_sdca_priv *rt712;
1191 	int ret;
1192 
1193 	rt712 = devm_kzalloc(dev, sizeof(*rt712), GFP_KERNEL);
1194 	if (!rt712)
1195 		return -ENOMEM;
1196 
1197 	dev_set_drvdata(dev, rt712);
1198 	rt712->slave = slave;
1199 	rt712->regmap = regmap;
1200 	rt712->mbq_regmap = mbq_regmap;
1201 
1202 	mutex_init(&rt712->calibrate_mutex);
1203 	mutex_init(&rt712->disable_irq_lock);
1204 
1205 	INIT_DELAYED_WORK(&rt712->jack_detect_work, rt712_sdca_jack_detect_handler);
1206 	INIT_DELAYED_WORK(&rt712->jack_btn_check_work, rt712_sdca_btn_check_handler);
1207 
1208 	/*
1209 	 * Mark hw_init to false
1210 	 * HW init will be performed when device reports present
1211 	 */
1212 	rt712->hw_init = false;
1213 	rt712->first_hw_init = false;
1214 	rt712->fu0f_dapm_mute = true;
1215 	rt712->fu0f_mixer_l_mute = rt712->fu0f_mixer_r_mute = true;
1216 
1217 	/* JD source uses JD1 in default */
1218 	rt712->jd_src = RT712_JD1;
1219 
1220 	if (slave->id.part_id != RT712_PART_ID_713)
1221 		ret =  devm_snd_soc_register_component(dev,
1222 				&soc_sdca_dev_rt712, rt712_sdca_dai, ARRAY_SIZE(rt712_sdca_dai));
1223 	else
1224 		ret =  devm_snd_soc_register_component(dev,
1225 				&soc_sdca_dev_rt712, rt712_sdca_dai, 1);
1226 
1227 	dev_dbg(&slave->dev, "%s\n", __func__);
1228 
1229 	return ret;
1230 }
1231 
1232 int rt712_sdca_io_init(struct device *dev, struct sdw_slave *slave)
1233 {
1234 	struct rt712_sdca_priv *rt712 = dev_get_drvdata(dev);
1235 	int ret = 0;
1236 	unsigned int val, hibernation_flag;
1237 
1238 	rt712->disable_irq = false;
1239 
1240 	if (rt712->hw_init)
1241 		return 0;
1242 
1243 	if (rt712->first_hw_init) {
1244 		regcache_cache_only(rt712->regmap, false);
1245 		regcache_cache_bypass(rt712->regmap, true);
1246 		regcache_cache_only(rt712->mbq_regmap, false);
1247 		regcache_cache_bypass(rt712->mbq_regmap, true);
1248 	} else {
1249 		/*
1250 		 * PM runtime is only enabled when a Slave reports as Attached
1251 		 */
1252 
1253 		/* set autosuspend parameters */
1254 		pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
1255 		pm_runtime_use_autosuspend(&slave->dev);
1256 
1257 		/* update count of parent 'active' children */
1258 		pm_runtime_set_active(&slave->dev);
1259 
1260 		/* make sure the device does not suspend immediately */
1261 		pm_runtime_mark_last_busy(&slave->dev);
1262 
1263 		pm_runtime_enable(&slave->dev);
1264 	}
1265 
1266 	pm_runtime_get_noresume(&slave->dev);
1267 
1268 	rt712_sdca_index_read(rt712, RT712_VENDOR_REG, RT712_JD_PRODUCT_NUM, &val);
1269 	rt712->hw_id = (val & 0xf000) >> 12;
1270 
1271 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_ANALOG_BIAS_CTL3, 0xaa81);
1272 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_LDO2_3_CTL1, 0xa1e0);
1273 	rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_HP_DETECT_RLDET_CTL1, 0x0000);
1274 	rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_HP_DETECT_RLDET_CTL2, 0x0000);
1275 	rt712_sdca_index_write(rt712, RT712_VENDOR_ANALOG_CTL, RT712_MISC_POWER_CTL7, 0x0000);
1276 	regmap_write(rt712->regmap, RT712_RC_CAL, 0x23);
1277 
1278 	/* calibration */
1279 	rt712_sdca_index_read(rt712, RT712_VENDOR_REG, RT712_SW_CONFIG1, &hibernation_flag);
1280 	if (!hibernation_flag) {
1281 		ret = rt712_sdca_calibration(rt712);
1282 		if (ret < 0)
1283 			dev_err(dev, "%s, calibration failed!\n", __func__);
1284 	}
1285 
1286 	rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
1287 		RT712_MIXER_CTL1, 0x3000, 0x0000);
1288 	rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
1289 		RT712_ADC0A_08_PDE_FLOAT_CTL, 0x1112);
1290 	rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
1291 		RT712_MIC2_LINE2_PDE_FLOAT_CTL, 0x3412);
1292 	rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
1293 		RT712_DAC03_HP_PDE_FLOAT_CTL, 0x4040);
1294 
1295 	rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
1296 		RT712_HDA_LEGACY_GPIO_WAKE_EN_CTL, 0x0001, 0x0000);
1297 	regmap_write(rt712->regmap, 0x2f50, 0x00);
1298 	regmap_write(rt712->regmap, 0x2f54, 0x00);
1299 	regmap_write(rt712->regmap,
1300 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_IT09, RT712_SDCA_CTL_VENDOR_DEF, 0), 0x01);
1301 
1302 	/* add SPK settings */
1303 	if (rt712->hw_id != RT712_DEV_ID_713) {
1304 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_AMP_PDE_FLOAT_CTL, 0x2323);
1305 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_EAPD_CTL, 0x0002);
1306 		regmap_write(rt712->regmap,
1307 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_OT23, RT712_SDCA_CTL_VENDOR_DEF, 0), 0x04);
1308 	}
1309 
1310 	/*
1311 	 * if set_jack callback occurred early than io_init,
1312 	 * we set up the jack detection function now
1313 	 */
1314 	if (rt712->hs_jack)
1315 		rt712_sdca_jack_init(rt712);
1316 
1317 	if (!hibernation_flag)
1318 		rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_SW_CONFIG1, 0x0001);
1319 
1320 	if (rt712->first_hw_init) {
1321 		regcache_cache_bypass(rt712->regmap, false);
1322 		regcache_mark_dirty(rt712->regmap);
1323 		regcache_cache_bypass(rt712->mbq_regmap, false);
1324 		regcache_mark_dirty(rt712->mbq_regmap);
1325 	} else
1326 		rt712->first_hw_init = true;
1327 
1328 	/* Mark Slave initialization complete */
1329 	rt712->hw_init = true;
1330 
1331 	pm_runtime_mark_last_busy(&slave->dev);
1332 	pm_runtime_put_autosuspend(&slave->dev);
1333 
1334 	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
1335 	return 0;
1336 }
1337 
1338 MODULE_DESCRIPTION("ASoC RT712 SDCA SDW driver");
1339 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1340 MODULE_LICENSE("GPL");
1341