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