xref: /openbmc/linux/sound/soc/codecs/rt712-sdca.c (revision d68a32eb)
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 
rt712_sdca_index_write(struct rt712_sdca_priv * rt712,unsigned int nid,unsigned int reg,unsigned int value)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 
rt712_sdca_index_read(struct rt712_sdca_priv * rt712,unsigned int nid,unsigned int reg,unsigned int * value)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 
rt712_sdca_index_update_bits(struct rt712_sdca_priv * rt712,unsigned int nid,unsigned int reg,unsigned int mask,unsigned int val)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 
rt712_sdca_calibration(struct rt712_sdca_priv * rt712)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 
rt712_sdca_button_detect(struct rt712_sdca_priv * rt712)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 
rt712_sdca_headset_detect(struct rt712_sdca_priv * rt712)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 
rt712_sdca_jack_detect_handler(struct work_struct * work)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 
rt712_sdca_btn_check_handler(struct work_struct * work)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 
rt712_sdca_jack_init(struct rt712_sdca_priv * rt712)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 
rt712_sdca_set_jack_detect(struct snd_soc_component * component,struct snd_soc_jack * hs_jack,void * data)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 	if (!rt712->first_hw_init)
457 		return 0;
458 
459 	ret = pm_runtime_resume_and_get(component->dev);
460 	if (ret < 0) {
461 		if (ret != -EACCES) {
462 			dev_err(component->dev, "%s: failed to resume %d\n", __func__, ret);
463 			return ret;
464 		}
465 
466 		/* pm_runtime not enabled yet */
467 		dev_dbg(component->dev,	"%s: skipping jack init for now\n", __func__);
468 		return 0;
469 	}
470 
471 	rt712_sdca_jack_init(rt712);
472 
473 	pm_runtime_mark_last_busy(component->dev);
474 	pm_runtime_put_autosuspend(component->dev);
475 
476 	return 0;
477 }
478 
479 /* For SDCA control DAC/ADC Gain */
rt712_sdca_set_gain_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)480 static int rt712_sdca_set_gain_put(struct snd_kcontrol *kcontrol,
481 		struct snd_ctl_elem_value *ucontrol)
482 {
483 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
484 	struct soc_mixer_control *mc =
485 		(struct soc_mixer_control *)kcontrol->private_value;
486 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
487 	unsigned int read_l, read_r, gain_l_val, gain_r_val;
488 	unsigned int adc_vol_flag = 0;
489 	unsigned int lvalue, rvalue;
490 	const unsigned int interval_offset = 0xc0;
491 	const unsigned int tendB = 0xa00;
492 
493 	if (strstr(ucontrol->id.name, "FU0F Capture Volume"))
494 		adc_vol_flag = 1;
495 
496 	regmap_read(rt712->mbq_regmap, mc->reg, &lvalue);
497 	regmap_read(rt712->mbq_regmap, mc->rreg, &rvalue);
498 
499 	/* L Channel */
500 	gain_l_val = ucontrol->value.integer.value[0];
501 	if (gain_l_val > mc->max)
502 		gain_l_val = mc->max;
503 
504 	if (mc->shift == 8) /* boost gain */
505 		gain_l_val = gain_l_val * tendB;
506 	else {
507 		/* ADC/DAC gain */
508 		if (adc_vol_flag)
509 			gain_l_val = 0x1e00 - ((mc->max - gain_l_val) * interval_offset);
510 		else
511 			gain_l_val = 0 - ((mc->max - gain_l_val) * interval_offset);
512 		gain_l_val &= 0xffff;
513 	}
514 
515 	/* R Channel */
516 	gain_r_val = ucontrol->value.integer.value[1];
517 	if (gain_r_val > mc->max)
518 		gain_r_val = mc->max;
519 
520 	if (mc->shift == 8) /* boost gain */
521 		gain_r_val = gain_r_val * tendB;
522 	else {
523 		/* ADC/DAC gain */
524 		if (adc_vol_flag)
525 			gain_r_val = 0x1e00 - ((mc->max - gain_r_val) * interval_offset);
526 		else
527 			gain_r_val = 0 - ((mc->max - gain_r_val) * interval_offset);
528 		gain_r_val &= 0xffff;
529 	}
530 
531 	if (lvalue == gain_l_val && rvalue == gain_r_val)
532 		return 0;
533 
534 	/* Lch*/
535 	regmap_write(rt712->mbq_regmap, mc->reg, gain_l_val);
536 	/* Rch */
537 	regmap_write(rt712->mbq_regmap, mc->rreg, gain_r_val);
538 
539 	regmap_read(rt712->mbq_regmap, mc->reg, &read_l);
540 	regmap_read(rt712->mbq_regmap, mc->rreg, &read_r);
541 	if (read_r == gain_r_val && read_l == gain_l_val)
542 		return 1;
543 
544 	return -EIO;
545 }
546 
rt712_sdca_set_gain_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)547 static int rt712_sdca_set_gain_get(struct snd_kcontrol *kcontrol,
548 		struct snd_ctl_elem_value *ucontrol)
549 {
550 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
551 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
552 	struct soc_mixer_control *mc =
553 		(struct soc_mixer_control *)kcontrol->private_value;
554 	unsigned int read_l, read_r, ctl_l = 0, ctl_r = 0;
555 	unsigned int adc_vol_flag = 0;
556 	const unsigned int interval_offset = 0xc0;
557 	const unsigned int tendB = 0xa00;
558 
559 	if (strstr(ucontrol->id.name, "FU0F Capture Volume"))
560 		adc_vol_flag = 1;
561 
562 	regmap_read(rt712->mbq_regmap, mc->reg, &read_l);
563 	regmap_read(rt712->mbq_regmap, mc->rreg, &read_r);
564 
565 	if (mc->shift == 8) /* boost gain */
566 		ctl_l = read_l / tendB;
567 	else {
568 		if (adc_vol_flag)
569 			ctl_l = mc->max - (((0x1e00 - read_l) & 0xffff) / interval_offset);
570 		else
571 			ctl_l = mc->max - (((0 - read_l) & 0xffff) / interval_offset);
572 	}
573 
574 	if (read_l != read_r) {
575 		if (mc->shift == 8) /* boost gain */
576 			ctl_r = read_r / tendB;
577 		else { /* ADC/DAC gain */
578 			if (adc_vol_flag)
579 				ctl_r = mc->max - (((0x1e00 - read_r) & 0xffff) / interval_offset);
580 			else
581 				ctl_r = mc->max - (((0 - read_r) & 0xffff) / interval_offset);
582 		}
583 	} else
584 		ctl_r = ctl_l;
585 
586 	ucontrol->value.integer.value[0] = ctl_l;
587 	ucontrol->value.integer.value[1] = ctl_r;
588 
589 	return 0;
590 }
591 
rt712_sdca_set_fu0f_capture_ctl(struct rt712_sdca_priv * rt712)592 static int rt712_sdca_set_fu0f_capture_ctl(struct rt712_sdca_priv *rt712)
593 {
594 	int err;
595 	unsigned int ch_l, ch_r;
596 
597 	ch_l = (rt712->fu0f_dapm_mute || rt712->fu0f_mixer_l_mute) ? 0x01 : 0x00;
598 	ch_r = (rt712->fu0f_dapm_mute || rt712->fu0f_mixer_r_mute) ? 0x01 : 0x00;
599 
600 	err = regmap_write(rt712->regmap,
601 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F,
602 			RT712_SDCA_CTL_FU_MUTE, CH_L), ch_l);
603 	if (err < 0)
604 		return err;
605 
606 	err = regmap_write(rt712->regmap,
607 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F,
608 			RT712_SDCA_CTL_FU_MUTE, CH_R), ch_r);
609 	if (err < 0)
610 		return err;
611 
612 	return 0;
613 }
614 
rt712_sdca_fu0f_capture_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)615 static int rt712_sdca_fu0f_capture_get(struct snd_kcontrol *kcontrol,
616 			struct snd_ctl_elem_value *ucontrol)
617 {
618 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
619 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
620 
621 	ucontrol->value.integer.value[0] = !rt712->fu0f_mixer_l_mute;
622 	ucontrol->value.integer.value[1] = !rt712->fu0f_mixer_r_mute;
623 	return 0;
624 }
625 
rt712_sdca_fu0f_capture_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)626 static int rt712_sdca_fu0f_capture_put(struct snd_kcontrol *kcontrol,
627 			struct snd_ctl_elem_value *ucontrol)
628 {
629 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
630 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
631 	int err;
632 
633 	if (rt712->fu0f_mixer_l_mute == !ucontrol->value.integer.value[0] &&
634 		rt712->fu0f_mixer_r_mute == !ucontrol->value.integer.value[1])
635 		return 0;
636 
637 	rt712->fu0f_mixer_l_mute = !ucontrol->value.integer.value[0];
638 	rt712->fu0f_mixer_r_mute = !ucontrol->value.integer.value[1];
639 	err = rt712_sdca_set_fu0f_capture_ctl(rt712);
640 	if (err < 0)
641 		return err;
642 
643 	return 1;
644 }
645 
646 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0);
647 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, -1725, 75, 0);
648 static const DECLARE_TLV_DB_SCALE(boost_vol_tlv, 0, 1000, 0);
649 
650 static const struct snd_kcontrol_new rt712_sdca_controls[] = {
651 	SOC_DOUBLE_R_EXT_TLV("FU05 Playback Volume",
652 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05, RT712_SDCA_CTL_FU_VOLUME, CH_L),
653 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05, RT712_SDCA_CTL_FU_VOLUME, CH_R),
654 		0, 0x57, 0,
655 		rt712_sdca_set_gain_get, rt712_sdca_set_gain_put, out_vol_tlv),
656 	SOC_DOUBLE_EXT("FU0F Capture Switch", SND_SOC_NOPM, 0, 1, 1, 0,
657 		rt712_sdca_fu0f_capture_get, rt712_sdca_fu0f_capture_put),
658 	SOC_DOUBLE_R_EXT_TLV("FU0F Capture Volume",
659 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F, RT712_SDCA_CTL_FU_VOLUME, CH_L),
660 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F, RT712_SDCA_CTL_FU_VOLUME, CH_R),
661 		0, 0x3f, 0,
662 		rt712_sdca_set_gain_get, rt712_sdca_set_gain_put, mic_vol_tlv),
663 	SOC_DOUBLE_R_EXT_TLV("FU44 Boost Volume",
664 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PLATFORM_FU44, RT712_SDCA_CTL_FU_CH_GAIN, CH_L),
665 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PLATFORM_FU44, RT712_SDCA_CTL_FU_CH_GAIN, CH_R),
666 		8, 3, 0,
667 		rt712_sdca_set_gain_get, rt712_sdca_set_gain_put, boost_vol_tlv),
668 };
669 
670 static const struct snd_kcontrol_new rt712_sdca_spk_controls[] = {
671 	SOC_DOUBLE_R_EXT_TLV("FU06 Playback Volume",
672 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_VOLUME, CH_L),
673 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_VOLUME, CH_R),
674 		0, 0x57, 0,
675 		rt712_sdca_set_gain_get, rt712_sdca_set_gain_put, out_vol_tlv),
676 };
677 
rt712_sdca_mux_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)678 static int rt712_sdca_mux_get(struct snd_kcontrol *kcontrol,
679 			struct snd_ctl_elem_value *ucontrol)
680 {
681 	struct snd_soc_component *component =
682 		snd_soc_dapm_kcontrol_component(kcontrol);
683 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
684 	unsigned int val = 0, mask = 0x3300;
685 
686 	rt712_sdca_index_read(rt712, RT712_VENDOR_HDA_CTL, RT712_MIXER_CTL1, &val);
687 
688 	val = val & mask;
689 	switch (val) {
690 	case 0x3000:
691 		val = 1;
692 		break;
693 	case 0x0300:
694 		val = 0;
695 		break;
696 	}
697 
698 	ucontrol->value.enumerated.item[0] = val;
699 
700 	return 0;
701 }
702 
rt712_sdca_mux_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)703 static int rt712_sdca_mux_put(struct snd_kcontrol *kcontrol,
704 			struct snd_ctl_elem_value *ucontrol)
705 {
706 	struct snd_soc_component *component =
707 		snd_soc_dapm_kcontrol_component(kcontrol);
708 	struct snd_soc_dapm_context *dapm =
709 		snd_soc_dapm_kcontrol_dapm(kcontrol);
710 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
711 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
712 	unsigned int *item = ucontrol->value.enumerated.item;
713 	unsigned int mask_sft;
714 	unsigned int val;
715 
716 	if (item[0] >= e->items)
717 		return -EINVAL;
718 
719 	if (ucontrol->value.enumerated.item[0] == 0)
720 		mask_sft = 12;
721 	else if (ucontrol->value.enumerated.item[0] == 1)
722 		mask_sft = 8;
723 	else
724 		return -EINVAL;
725 
726 	rt712_sdca_index_read(rt712, RT712_VENDOR_HDA_CTL, RT712_MIXER_CTL1, &val);
727 	val = (val >> mask_sft) & 0x3;
728 	if (!val)
729 		return 0;
730 
731 	rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
732 		RT712_MIXER_CTL1, 0x3fff);
733 	rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
734 		RT712_MIXER_CTL1, 0x3 << mask_sft, 0);
735 
736 	snd_soc_dapm_mux_update_power(dapm, kcontrol,
737 		item[0], e, NULL);
738 
739 	return 1;
740 }
741 
742 static const char * const adc_mux_text[] = {
743 	"MIC2",
744 	"LINE2",
745 };
746 
747 static SOC_ENUM_SINGLE_DECL(
748 	rt712_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text);
749 
750 static const struct snd_kcontrol_new rt712_sdca_adc23_mux =
751 	SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt712_adc23_enum,
752 			rt712_sdca_mux_get, rt712_sdca_mux_put);
753 
rt712_sdca_fu05_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)754 static int rt712_sdca_fu05_event(struct snd_soc_dapm_widget *w,
755 	struct snd_kcontrol *kcontrol, int event)
756 {
757 	struct snd_soc_component *component =
758 		snd_soc_dapm_to_component(w->dapm);
759 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
760 	unsigned char unmute = 0x0, mute = 0x1;
761 
762 	switch (event) {
763 	case SND_SOC_DAPM_POST_PMU:
764 		regmap_write(rt712->regmap,
765 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05,
766 				RT712_SDCA_CTL_FU_MUTE, CH_L),
767 				unmute);
768 		regmap_write(rt712->regmap,
769 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05,
770 				RT712_SDCA_CTL_FU_MUTE, CH_R),
771 				unmute);
772 		break;
773 	case SND_SOC_DAPM_PRE_PMD:
774 		regmap_write(rt712->regmap,
775 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05,
776 				RT712_SDCA_CTL_FU_MUTE, CH_L),
777 				mute);
778 		regmap_write(rt712->regmap,
779 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05,
780 				RT712_SDCA_CTL_FU_MUTE, CH_R),
781 				mute);
782 		break;
783 	}
784 	return 0;
785 }
786 
rt712_sdca_fu0f_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)787 static int rt712_sdca_fu0f_event(struct snd_soc_dapm_widget *w,
788 	struct snd_kcontrol *kcontrol, int event)
789 {
790 	struct snd_soc_component *component =
791 		snd_soc_dapm_to_component(w->dapm);
792 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
793 
794 	switch (event) {
795 	case SND_SOC_DAPM_POST_PMU:
796 		rt712->fu0f_dapm_mute = false;
797 		rt712_sdca_set_fu0f_capture_ctl(rt712);
798 		break;
799 	case SND_SOC_DAPM_PRE_PMD:
800 		rt712->fu0f_dapm_mute = true;
801 		rt712_sdca_set_fu0f_capture_ctl(rt712);
802 		break;
803 	}
804 	return 0;
805 }
806 
rt712_sdca_pde40_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)807 static int rt712_sdca_pde40_event(struct snd_soc_dapm_widget *w,
808 	struct snd_kcontrol *kcontrol, int event)
809 {
810 	struct snd_soc_component *component =
811 		snd_soc_dapm_to_component(w->dapm);
812 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
813 	unsigned char ps0 = 0x0, ps3 = 0x3;
814 
815 	switch (event) {
816 	case SND_SOC_DAPM_POST_PMU:
817 		regmap_write(rt712->regmap,
818 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PDE40,
819 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
820 				ps0);
821 		break;
822 	case SND_SOC_DAPM_PRE_PMD:
823 		regmap_write(rt712->regmap,
824 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PDE40,
825 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
826 				ps3);
827 		break;
828 	}
829 	return 0;
830 }
831 
rt712_sdca_pde12_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)832 static int rt712_sdca_pde12_event(struct snd_soc_dapm_widget *w,
833 	struct snd_kcontrol *kcontrol, int event)
834 {
835 	struct snd_soc_component *component =
836 		snd_soc_dapm_to_component(w->dapm);
837 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
838 	unsigned char ps0 = 0x0, ps3 = 0x3;
839 
840 	switch (event) {
841 	case SND_SOC_DAPM_POST_PMU:
842 		regmap_write(rt712->regmap,
843 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PDE12,
844 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
845 				ps0);
846 		break;
847 	case SND_SOC_DAPM_PRE_PMD:
848 		regmap_write(rt712->regmap,
849 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_PDE12,
850 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
851 				ps3);
852 		break;
853 	}
854 	return 0;
855 }
856 
rt712_sdca_classd_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)857 static int rt712_sdca_classd_event(struct snd_soc_dapm_widget *w,
858 	struct snd_kcontrol *kcontrol, int event)
859 {
860 	struct snd_soc_component *component =
861 		snd_soc_dapm_to_component(w->dapm);
862 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
863 	unsigned char ps0 = 0x0, ps3 = 0x3;
864 
865 	switch (event) {
866 	case SND_SOC_DAPM_POST_PMU:
867 		regmap_write(rt712->regmap,
868 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_PDE23,
869 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
870 				ps0);
871 		break;
872 	case SND_SOC_DAPM_PRE_PMD:
873 		regmap_write(rt712->regmap,
874 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_PDE23,
875 				RT712_SDCA_CTL_REQ_POWER_STATE, 0),
876 				ps3);
877 		break;
878 
879 	default:
880 		break;
881 	}
882 
883 	return 0;
884 }
885 
886 static const struct snd_kcontrol_new rt712_spk_sto_dac =
887 	SOC_DAPM_DOUBLE_R("Switch",
888 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_MUTE, CH_L),
889 		SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_MUTE, CH_R),
890 		0, 1, 1);
891 
892 static const struct snd_soc_dapm_widget rt712_sdca_dapm_widgets[] = {
893 	SND_SOC_DAPM_OUTPUT("HP"),
894 	SND_SOC_DAPM_INPUT("MIC2"),
895 	SND_SOC_DAPM_INPUT("LINE2"),
896 
897 	SND_SOC_DAPM_SUPPLY("PDE 40", SND_SOC_NOPM, 0, 0,
898 		rt712_sdca_pde40_event,
899 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
900 	SND_SOC_DAPM_SUPPLY("PDE 12", SND_SOC_NOPM, 0, 0,
901 		rt712_sdca_pde12_event,
902 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
903 
904 	SND_SOC_DAPM_DAC_E("FU 05", NULL, SND_SOC_NOPM, 0, 0,
905 		rt712_sdca_fu05_event,
906 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
907 	SND_SOC_DAPM_ADC_E("FU 0F", NULL, SND_SOC_NOPM, 0, 0,
908 		rt712_sdca_fu0f_event,
909 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
910 	SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0,
911 		&rt712_sdca_adc23_mux),
912 
913 	SND_SOC_DAPM_AIF_IN("DP1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0),
914 	SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0),
915 };
916 
917 static const struct snd_soc_dapm_route rt712_sdca_audio_map[] = {
918 	{ "FU 05", NULL, "DP1RX" },
919 	{ "DP4TX", NULL, "FU 0F" },
920 
921 	{ "FU 0F", NULL, "PDE 12" },
922 	{ "FU 0F", NULL, "ADC 23 Mux" },
923 	{ "ADC 23 Mux", "LINE2", "LINE2" },
924 	{ "ADC 23 Mux", "MIC2", "MIC2" },
925 
926 	{ "HP", NULL, "PDE 40" },
927 	{ "HP", NULL, "FU 05" },
928 };
929 
930 static const struct snd_soc_dapm_widget rt712_sdca_spk_dapm_widgets[] = {
931 	SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0),
932 
933 	/* Digital Interface */
934 	SND_SOC_DAPM_SWITCH("FU06", SND_SOC_NOPM, 0, 0, &rt712_spk_sto_dac),
935 
936 	/* Output */
937 	SND_SOC_DAPM_PGA_E("CLASS D", SND_SOC_NOPM, 0, 0, NULL, 0,
938 		rt712_sdca_classd_event, SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
939 	SND_SOC_DAPM_OUTPUT("SPOL"),
940 	SND_SOC_DAPM_OUTPUT("SPOR"),
941 };
942 
943 static const struct snd_soc_dapm_route rt712_sdca_spk_dapm_routes[] = {
944 	{ "FU06", "Switch", "DP3RX" },
945 	{ "CLASS D", NULL, "FU06" },
946 	{ "SPOL", NULL, "CLASS D" },
947 	{ "SPOR", NULL, "CLASS D" },
948 };
949 
rt712_sdca_parse_dt(struct rt712_sdca_priv * rt712,struct device * dev)950 static int rt712_sdca_parse_dt(struct rt712_sdca_priv *rt712, struct device *dev)
951 {
952 	device_property_read_u32(dev, "realtek,jd-src", &rt712->jd_src);
953 
954 	return 0;
955 }
956 
rt712_sdca_probe(struct snd_soc_component * component)957 static int rt712_sdca_probe(struct snd_soc_component *component)
958 {
959 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
960 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
961 	int ret;
962 
963 	rt712_sdca_parse_dt(rt712, &rt712->slave->dev);
964 	rt712->component = component;
965 
966 	/* add SPK route */
967 	if (rt712->hw_id != RT712_DEV_ID_713) {
968 		snd_soc_add_component_controls(component,
969 			rt712_sdca_spk_controls, ARRAY_SIZE(rt712_sdca_spk_controls));
970 		snd_soc_dapm_new_controls(dapm,
971 			rt712_sdca_spk_dapm_widgets, ARRAY_SIZE(rt712_sdca_spk_dapm_widgets));
972 		snd_soc_dapm_add_routes(dapm,
973 			rt712_sdca_spk_dapm_routes, ARRAY_SIZE(rt712_sdca_spk_dapm_routes));
974 	}
975 
976 	if (!rt712->first_hw_init)
977 		return 0;
978 
979 	ret = pm_runtime_resume(component->dev);
980 	if (ret < 0 && ret != -EACCES)
981 		return ret;
982 
983 	return 0;
984 }
985 
986 static const struct snd_soc_component_driver soc_sdca_dev_rt712 = {
987 	.probe = rt712_sdca_probe,
988 	.controls = rt712_sdca_controls,
989 	.num_controls = ARRAY_SIZE(rt712_sdca_controls),
990 	.dapm_widgets = rt712_sdca_dapm_widgets,
991 	.num_dapm_widgets = ARRAY_SIZE(rt712_sdca_dapm_widgets),
992 	.dapm_routes = rt712_sdca_audio_map,
993 	.num_dapm_routes = ARRAY_SIZE(rt712_sdca_audio_map),
994 	.set_jack = rt712_sdca_set_jack_detect,
995 	.endianness = 1,
996 };
997 
rt712_sdca_set_sdw_stream(struct snd_soc_dai * dai,void * sdw_stream,int direction)998 static int rt712_sdca_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
999 				int direction)
1000 {
1001 	snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
1002 
1003 	return 0;
1004 }
1005 
rt712_sdca_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1006 static void rt712_sdca_shutdown(struct snd_pcm_substream *substream,
1007 				struct snd_soc_dai *dai)
1008 {
1009 	snd_soc_dai_set_dma_data(dai, substream, NULL);
1010 }
1011 
rt712_sdca_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)1012 static int rt712_sdca_pcm_hw_params(struct snd_pcm_substream *substream,
1013 				struct snd_pcm_hw_params *params,
1014 				struct snd_soc_dai *dai)
1015 {
1016 	struct snd_soc_component *component = dai->component;
1017 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1018 	struct sdw_stream_config stream_config;
1019 	struct sdw_port_config port_config;
1020 	enum sdw_data_direction direction;
1021 	struct sdw_stream_runtime *sdw_stream;
1022 	int retval, port, num_channels;
1023 	unsigned int sampling_rate;
1024 
1025 	dev_dbg(dai->dev, "%s %s", __func__, dai->name);
1026 	sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
1027 
1028 	if (!sdw_stream)
1029 		return -EINVAL;
1030 
1031 	if (!rt712->slave)
1032 		return -EINVAL;
1033 
1034 	/* SoundWire specific configuration */
1035 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1036 		direction = SDW_DATA_DIR_RX;
1037 		if (dai->id == RT712_AIF1)
1038 			port = 1;
1039 		else if (dai->id == RT712_AIF2)
1040 			port = 3;
1041 		else
1042 			return -EINVAL;
1043 	} else {
1044 		direction = SDW_DATA_DIR_TX;
1045 		if (dai->id == RT712_AIF1)
1046 			port = 4;
1047 		else
1048 			return -EINVAL;
1049 	}
1050 
1051 	stream_config.frame_rate = params_rate(params);
1052 	stream_config.ch_count = params_channels(params);
1053 	stream_config.bps = snd_pcm_format_width(params_format(params));
1054 	stream_config.direction = direction;
1055 
1056 	num_channels = params_channels(params);
1057 	port_config.ch_mask = GENMASK(num_channels - 1, 0);
1058 	port_config.num = port;
1059 
1060 	retval = sdw_stream_add_slave(rt712->slave, &stream_config,
1061 					&port_config, 1, sdw_stream);
1062 	if (retval) {
1063 		dev_err(dai->dev, "Unable to configure port\n");
1064 		return retval;
1065 	}
1066 
1067 	if (params_channels(params) > 16) {
1068 		dev_err(component->dev, "Unsupported channels %d\n",
1069 			params_channels(params));
1070 		return -EINVAL;
1071 	}
1072 
1073 	/* sampling rate configuration */
1074 	switch (params_rate(params)) {
1075 	case 44100:
1076 		sampling_rate = RT712_SDCA_RATE_44100HZ;
1077 		break;
1078 	case 48000:
1079 		sampling_rate = RT712_SDCA_RATE_48000HZ;
1080 		break;
1081 	case 96000:
1082 		sampling_rate = RT712_SDCA_RATE_96000HZ;
1083 		break;
1084 	case 192000:
1085 		sampling_rate = RT712_SDCA_RATE_192000HZ;
1086 		break;
1087 	default:
1088 		dev_err(component->dev, "Rate %d is not supported\n",
1089 			params_rate(params));
1090 		return -EINVAL;
1091 	}
1092 
1093 	/* set sampling frequency */
1094 	switch (dai->id) {
1095 	case RT712_AIF1:
1096 		regmap_write(rt712->regmap,
1097 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_CS01, RT712_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1098 			sampling_rate);
1099 		regmap_write(rt712->regmap,
1100 			SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_CS11, RT712_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1101 			sampling_rate);
1102 		break;
1103 	case RT712_AIF2:
1104 		regmap_write(rt712->regmap,
1105 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_CS31, RT712_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1106 			sampling_rate);
1107 		break;
1108 	default:
1109 		dev_err(component->dev, "Wrong DAI id\n");
1110 		return -EINVAL;
1111 	}
1112 
1113 	return 0;
1114 }
1115 
rt712_sdca_pcm_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1116 static int rt712_sdca_pcm_hw_free(struct snd_pcm_substream *substream,
1117 				struct snd_soc_dai *dai)
1118 {
1119 	struct snd_soc_component *component = dai->component;
1120 	struct rt712_sdca_priv *rt712 = snd_soc_component_get_drvdata(component);
1121 	struct sdw_stream_runtime *sdw_stream =
1122 		snd_soc_dai_get_dma_data(dai, substream);
1123 
1124 	if (!rt712->slave)
1125 		return -EINVAL;
1126 
1127 	sdw_stream_remove_slave(rt712->slave, sdw_stream);
1128 	return 0;
1129 }
1130 
1131 #define RT712_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
1132 			SNDRV_PCM_RATE_192000)
1133 #define RT712_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1134 			SNDRV_PCM_FMTBIT_S24_LE)
1135 
1136 static const struct snd_soc_dai_ops rt712_sdca_ops = {
1137 	.hw_params	= rt712_sdca_pcm_hw_params,
1138 	.hw_free	= rt712_sdca_pcm_hw_free,
1139 	.set_stream	= rt712_sdca_set_sdw_stream,
1140 	.shutdown	= rt712_sdca_shutdown,
1141 };
1142 
1143 static struct snd_soc_dai_driver rt712_sdca_dai[] = {
1144 	{
1145 		.name = "rt712-sdca-aif1",
1146 		.id = RT712_AIF1,
1147 		.playback = {
1148 			.stream_name = "DP1 Playback",
1149 			.channels_min = 1,
1150 			.channels_max = 2,
1151 			.rates = RT712_STEREO_RATES,
1152 			.formats = RT712_FORMATS,
1153 		},
1154 		.capture = {
1155 			.stream_name = "DP4 Capture",
1156 			.channels_min = 1,
1157 			.channels_max = 2,
1158 			.rates = RT712_STEREO_RATES,
1159 			.formats = RT712_FORMATS,
1160 		},
1161 		.ops = &rt712_sdca_ops,
1162 	},
1163 	{
1164 		.name = "rt712-sdca-aif2",
1165 		.id = RT712_AIF2,
1166 		.playback = {
1167 			.stream_name = "DP3 Playback",
1168 			.channels_min = 1,
1169 			.channels_max = 2,
1170 			.rates = RT712_STEREO_RATES,
1171 			.formats = RT712_FORMATS,
1172 		},
1173 		.ops = &rt712_sdca_ops,
1174 	}
1175 };
1176 
rt712_sdca_init(struct device * dev,struct regmap * regmap,struct regmap * mbq_regmap,struct sdw_slave * slave)1177 int rt712_sdca_init(struct device *dev, struct regmap *regmap,
1178 			struct regmap *mbq_regmap, struct sdw_slave *slave)
1179 {
1180 	struct rt712_sdca_priv *rt712;
1181 	int ret;
1182 
1183 	rt712 = devm_kzalloc(dev, sizeof(*rt712), GFP_KERNEL);
1184 	if (!rt712)
1185 		return -ENOMEM;
1186 
1187 	dev_set_drvdata(dev, rt712);
1188 	rt712->slave = slave;
1189 	rt712->regmap = regmap;
1190 	rt712->mbq_regmap = mbq_regmap;
1191 
1192 	regcache_cache_only(rt712->regmap, true);
1193 	regcache_cache_only(rt712->mbq_regmap, true);
1194 
1195 	mutex_init(&rt712->calibrate_mutex);
1196 	mutex_init(&rt712->disable_irq_lock);
1197 
1198 	INIT_DELAYED_WORK(&rt712->jack_detect_work, rt712_sdca_jack_detect_handler);
1199 	INIT_DELAYED_WORK(&rt712->jack_btn_check_work, rt712_sdca_btn_check_handler);
1200 
1201 	/*
1202 	 * Mark hw_init to false
1203 	 * HW init will be performed when device reports present
1204 	 */
1205 	rt712->hw_init = false;
1206 	rt712->first_hw_init = false;
1207 	rt712->fu0f_dapm_mute = true;
1208 	rt712->fu0f_mixer_l_mute = rt712->fu0f_mixer_r_mute = true;
1209 
1210 	/* JD source uses JD1 in default */
1211 	rt712->jd_src = RT712_JD1;
1212 
1213 	if (slave->id.part_id != RT712_PART_ID_713)
1214 		ret =  devm_snd_soc_register_component(dev,
1215 				&soc_sdca_dev_rt712, rt712_sdca_dai, ARRAY_SIZE(rt712_sdca_dai));
1216 	else
1217 		ret =  devm_snd_soc_register_component(dev,
1218 				&soc_sdca_dev_rt712, rt712_sdca_dai, 1);
1219 	if (ret < 0)
1220 		return ret;
1221 
1222 	/* set autosuspend parameters */
1223 	pm_runtime_set_autosuspend_delay(dev, 3000);
1224 	pm_runtime_use_autosuspend(dev);
1225 
1226 	/* make sure the device does not suspend immediately */
1227 	pm_runtime_mark_last_busy(dev);
1228 
1229 	pm_runtime_enable(dev);
1230 
1231 	/* important note: the device is NOT tagged as 'active' and will remain
1232 	 * 'suspended' until the hardware is enumerated/initialized. This is required
1233 	 * to make sure the ASoC framework use of pm_runtime_get_sync() does not silently
1234 	 * fail with -EACCESS because of race conditions between card creation and enumeration
1235 	 */
1236 
1237 	dev_dbg(dev, "%s\n", __func__);
1238 
1239 	return 0;
1240 }
1241 
rt712_sdca_io_init(struct device * dev,struct sdw_slave * slave)1242 int rt712_sdca_io_init(struct device *dev, struct sdw_slave *slave)
1243 {
1244 	struct rt712_sdca_priv *rt712 = dev_get_drvdata(dev);
1245 	int ret = 0;
1246 	unsigned int val, hibernation_flag;
1247 
1248 	rt712->disable_irq = false;
1249 
1250 	if (rt712->hw_init)
1251 		return 0;
1252 
1253 	regcache_cache_only(rt712->regmap, false);
1254 	regcache_cache_only(rt712->mbq_regmap, false);
1255 	if (rt712->first_hw_init) {
1256 		regcache_cache_bypass(rt712->regmap, true);
1257 		regcache_cache_bypass(rt712->mbq_regmap, true);
1258 	} else {
1259 		/*
1260 		 *  PM runtime status is marked as 'active' only when a Slave reports as Attached
1261 		 */
1262 
1263 		/* update count of parent 'active' children */
1264 		pm_runtime_set_active(&slave->dev);
1265 	}
1266 
1267 	pm_runtime_get_noresume(&slave->dev);
1268 
1269 	rt712_sdca_index_read(rt712, RT712_VENDOR_REG, RT712_JD_PRODUCT_NUM, &val);
1270 	rt712->hw_id = (val & 0xf000) >> 12;
1271 
1272 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_ANALOG_BIAS_CTL3, 0xaa81);
1273 	rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_LDO2_3_CTL1, 0xa1e0);
1274 	rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_HP_DETECT_RLDET_CTL1, 0x0000);
1275 	rt712_sdca_index_write(rt712, RT712_VENDOR_IMS_DRE, RT712_HP_DETECT_RLDET_CTL2, 0x0000);
1276 	rt712_sdca_index_write(rt712, RT712_VENDOR_ANALOG_CTL, RT712_MISC_POWER_CTL7, 0x0000);
1277 	regmap_write(rt712->regmap, RT712_RC_CAL, 0x23);
1278 
1279 	/* calibration */
1280 	rt712_sdca_index_read(rt712, RT712_VENDOR_REG, RT712_SW_CONFIG1, &hibernation_flag);
1281 	if (!hibernation_flag) {
1282 		ret = rt712_sdca_calibration(rt712);
1283 		if (ret < 0)
1284 			dev_err(dev, "%s, calibration failed!\n", __func__);
1285 	}
1286 
1287 	rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
1288 		RT712_MIXER_CTL1, 0x3000, 0x0000);
1289 	rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
1290 		RT712_ADC0A_08_PDE_FLOAT_CTL, 0x1112);
1291 	rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
1292 		RT712_MIC2_LINE2_PDE_FLOAT_CTL, 0x3412);
1293 	rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL,
1294 		RT712_DAC03_HP_PDE_FLOAT_CTL, 0x4040);
1295 
1296 	rt712_sdca_index_update_bits(rt712, RT712_VENDOR_HDA_CTL,
1297 		RT712_HDA_LEGACY_GPIO_WAKE_EN_CTL, 0x0001, 0x0000);
1298 	regmap_write(rt712->regmap, 0x2f50, 0x00);
1299 	regmap_write(rt712->regmap, 0x2f54, 0x00);
1300 	regmap_write(rt712->regmap,
1301 		SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_IT09, RT712_SDCA_CTL_VENDOR_DEF, 0), 0x01);
1302 
1303 	/* add SPK settings */
1304 	if (rt712->hw_id != RT712_DEV_ID_713) {
1305 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_AMP_PDE_FLOAT_CTL, 0x2323);
1306 		rt712_sdca_index_write(rt712, RT712_VENDOR_HDA_CTL, RT712_EAPD_CTL, 0x0002);
1307 		regmap_write(rt712->regmap,
1308 			SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_OT23, RT712_SDCA_CTL_VENDOR_DEF, 0), 0x04);
1309 	}
1310 
1311 	/*
1312 	 * if set_jack callback occurred early than io_init,
1313 	 * we set up the jack detection function now
1314 	 */
1315 	if (rt712->hs_jack)
1316 		rt712_sdca_jack_init(rt712);
1317 
1318 	if (!hibernation_flag)
1319 		rt712_sdca_index_write(rt712, RT712_VENDOR_REG, RT712_SW_CONFIG1, 0x0001);
1320 
1321 	if (rt712->first_hw_init) {
1322 		regcache_cache_bypass(rt712->regmap, false);
1323 		regcache_mark_dirty(rt712->regmap);
1324 		regcache_cache_bypass(rt712->mbq_regmap, false);
1325 		regcache_mark_dirty(rt712->mbq_regmap);
1326 	} else
1327 		rt712->first_hw_init = true;
1328 
1329 	/* Mark Slave initialization complete */
1330 	rt712->hw_init = true;
1331 
1332 	pm_runtime_mark_last_busy(&slave->dev);
1333 	pm_runtime_put_autosuspend(&slave->dev);
1334 
1335 	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
1336 	return 0;
1337 }
1338 
1339 MODULE_DESCRIPTION("ASoC RT712 SDCA SDW driver");
1340 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1341 MODULE_LICENSE("GPL");
1342