xref: /openbmc/linux/sound/soc/codecs/rt700.c (revision 017cdefe)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // rt700.c -- rt700 ALSA SoC audio driver
4 //
5 // Copyright(c) 2019 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/pm.h>
16 #include <linux/soundwire/sdw.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/sdw.h>
23 #include <sound/soc.h>
24 #include <sound/soc-dapm.h>
25 #include <sound/initval.h>
26 #include <sound/tlv.h>
27 #include <sound/hda_verbs.h>
28 #include <sound/jack.h>
29 
30 #include "rt700.h"
31 
32 static int rt700_index_write(struct regmap *regmap,
33 		unsigned int reg, unsigned int value)
34 {
35 	int ret;
36 	unsigned int addr = (RT700_PRIV_INDEX_W_H << 8) | reg;
37 
38 	ret = regmap_write(regmap, addr, value);
39 	if (ret < 0)
40 		pr_err("Failed to set private value: %06x <= %04x ret=%d\n",
41 			addr, value, ret);
42 
43 	return ret;
44 }
45 
46 static int rt700_index_read(struct regmap *regmap,
47 		unsigned int reg, unsigned int *value)
48 {
49 	int ret;
50 	unsigned int addr = (RT700_PRIV_INDEX_W_H << 8) | reg;
51 
52 	*value = 0;
53 	ret = regmap_read(regmap, addr, value);
54 	if (ret < 0)
55 		pr_err("Failed to get private value: %06x => %04x ret=%d\n",
56 			addr, *value, ret);
57 
58 	return ret;
59 }
60 
61 static unsigned int rt700_button_detect(struct rt700_priv *rt700)
62 {
63 	unsigned int btn_type = 0, val80, val81;
64 	int ret;
65 
66 	ret = rt700_index_read(rt700->regmap, RT700_IRQ_FLAG_TABLE1, &val80);
67 	if (ret < 0)
68 		goto read_error;
69 	ret = rt700_index_read(rt700->regmap, RT700_IRQ_FLAG_TABLE2, &val81);
70 	if (ret < 0)
71 		goto read_error;
72 
73 	val80 &= 0x0381;
74 	val81 &= 0xff00;
75 
76 	switch (val80) {
77 	case 0x0200:
78 	case 0x0100:
79 	case 0x0080:
80 		btn_type |= SND_JACK_BTN_0;
81 		break;
82 	case 0x0001:
83 		btn_type |= SND_JACK_BTN_3;
84 		break;
85 	}
86 	switch (val81) {
87 	case 0x8000:
88 	case 0x4000:
89 	case 0x2000:
90 		btn_type |= SND_JACK_BTN_1;
91 		break;
92 	case 0x1000:
93 	case 0x0800:
94 	case 0x0400:
95 		btn_type |= SND_JACK_BTN_2;
96 		break;
97 	case 0x0200:
98 	case 0x0100:
99 		btn_type |= SND_JACK_BTN_3;
100 		break;
101 	}
102 read_error:
103 	return btn_type;
104 }
105 
106 static int rt700_headset_detect(struct rt700_priv *rt700)
107 {
108 	unsigned int buf, loop = 0;
109 	int ret;
110 	unsigned int jack_status = 0, reg;
111 
112 	ret = rt700_index_read(rt700->regmap,
113 					RT700_COMBO_JACK_AUTO_CTL2, &buf);
114 	if (ret < 0)
115 		goto io_error;
116 
117 	while (loop < 500 &&
118 		(buf & RT700_COMBOJACK_AUTO_DET_STATUS) == 0) {
119 		loop++;
120 
121 		usleep_range(9000, 10000);
122 		ret = rt700_index_read(rt700->regmap,
123 					RT700_COMBO_JACK_AUTO_CTL2, &buf);
124 		if (ret < 0)
125 			goto io_error;
126 
127 		reg = RT700_VERB_GET_PIN_SENSE | RT700_HP_OUT;
128 		ret = regmap_read(rt700->regmap, reg, &jack_status);
129 		if ((jack_status & (1 << 31)) == 0)
130 			goto remove_error;
131 	}
132 
133 	if (loop >= 500)
134 		goto to_error;
135 
136 	if (buf & RT700_COMBOJACK_AUTO_DET_TRS)
137 		rt700->jack_type = SND_JACK_HEADPHONE;
138 	else if ((buf & RT700_COMBOJACK_AUTO_DET_CTIA) ||
139 		(buf & RT700_COMBOJACK_AUTO_DET_OMTP))
140 		rt700->jack_type = SND_JACK_HEADSET;
141 
142 	return 0;
143 
144 to_error:
145 	ret = -ETIMEDOUT;
146 	pr_err_ratelimited("Time-out error in %s\n", __func__);
147 	return ret;
148 io_error:
149 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
150 	return ret;
151 remove_error:
152 	pr_err_ratelimited("Jack removal in %s\n", __func__);
153 	return -ENODEV;
154 }
155 
156 static void rt700_jack_detect_handler(struct work_struct *work)
157 {
158 	struct rt700_priv *rt700 =
159 		container_of(work, struct rt700_priv, jack_detect_work.work);
160 	int btn_type = 0, ret;
161 	unsigned int jack_status = 0, reg;
162 
163 	if (!rt700->hs_jack)
164 		return;
165 
166 	if (!snd_soc_card_is_instantiated(rt700->component->card))
167 		return;
168 
169 	reg = RT700_VERB_GET_PIN_SENSE | RT700_HP_OUT;
170 	ret = regmap_read(rt700->regmap, reg, &jack_status);
171 	if (ret < 0)
172 		goto io_error;
173 
174 	/* pin attached */
175 	if (jack_status & (1 << 31)) {
176 		/* jack in */
177 		if (rt700->jack_type == 0) {
178 			ret = rt700_headset_detect(rt700);
179 			if (ret < 0)
180 				return;
181 			if (rt700->jack_type == SND_JACK_HEADSET)
182 				btn_type = rt700_button_detect(rt700);
183 		} else if (rt700->jack_type == SND_JACK_HEADSET) {
184 			/* jack is already in, report button event */
185 			btn_type = rt700_button_detect(rt700);
186 		}
187 	} else {
188 		/* jack out */
189 		rt700->jack_type = 0;
190 	}
191 
192 	dev_dbg(&rt700->slave->dev,
193 		"in %s, jack_type=0x%x\n", __func__, rt700->jack_type);
194 	dev_dbg(&rt700->slave->dev,
195 		"in %s, btn_type=0x%x\n", __func__, btn_type);
196 
197 	snd_soc_jack_report(rt700->hs_jack, rt700->jack_type | btn_type,
198 			SND_JACK_HEADSET |
199 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
200 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
201 
202 	if (btn_type) {
203 		/* button released */
204 		snd_soc_jack_report(rt700->hs_jack, rt700->jack_type,
205 			SND_JACK_HEADSET |
206 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
207 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
208 
209 		mod_delayed_work(system_power_efficient_wq,
210 			&rt700->jack_btn_check_work, msecs_to_jiffies(200));
211 	}
212 
213 	return;
214 
215 io_error:
216 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
217 }
218 
219 static void rt700_btn_check_handler(struct work_struct *work)
220 {
221 	struct rt700_priv *rt700 = container_of(work, struct rt700_priv,
222 		jack_btn_check_work.work);
223 	int btn_type = 0, ret;
224 	unsigned int jack_status = 0, reg;
225 
226 	reg = RT700_VERB_GET_PIN_SENSE | RT700_HP_OUT;
227 	ret = regmap_read(rt700->regmap, reg, &jack_status);
228 	if (ret < 0)
229 		goto io_error;
230 
231 	/* pin attached */
232 	if (jack_status & (1 << 31)) {
233 		if (rt700->jack_type == SND_JACK_HEADSET) {
234 			/* jack is already in, report button event */
235 			btn_type = rt700_button_detect(rt700);
236 		}
237 	} else {
238 		rt700->jack_type = 0;
239 	}
240 
241 	/* cbj comparator */
242 	ret = rt700_index_read(rt700->regmap, RT700_COMBO_JACK_AUTO_CTL2, &reg);
243 	if (ret < 0)
244 		goto io_error;
245 
246 	if ((reg & 0xf0) == 0xf0)
247 		btn_type = 0;
248 
249 	dev_dbg(&rt700->slave->dev,
250 		"%s, btn_type=0x%x\n",	__func__, btn_type);
251 	snd_soc_jack_report(rt700->hs_jack, rt700->jack_type | btn_type,
252 			SND_JACK_HEADSET |
253 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
254 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
255 
256 	if (btn_type) {
257 		/* button released */
258 		snd_soc_jack_report(rt700->hs_jack, rt700->jack_type,
259 			SND_JACK_HEADSET |
260 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
261 			SND_JACK_BTN_2 | SND_JACK_BTN_3);
262 
263 		mod_delayed_work(system_power_efficient_wq,
264 			&rt700->jack_btn_check_work, msecs_to_jiffies(200));
265 	}
266 
267 	return;
268 
269 io_error:
270 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
271 }
272 
273 static void rt700_jack_init(struct rt700_priv *rt700)
274 {
275 	struct snd_soc_dapm_context *dapm =
276 		snd_soc_component_get_dapm(rt700->component);
277 
278 	/* power on */
279 	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
280 		regmap_write(rt700->regmap,
281 			RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
282 
283 	if (rt700->hs_jack) {
284 		/* Enable Jack Detection */
285 		regmap_write(rt700->regmap,
286 			RT700_SET_MIC2_UNSOLICITED_ENABLE, 0x82);
287 		regmap_write(rt700->regmap,
288 			RT700_SET_HP_UNSOLICITED_ENABLE, 0x81);
289 		regmap_write(rt700->regmap,
290 			RT700_SET_INLINE_UNSOLICITED_ENABLE, 0x83);
291 		rt700_index_write(rt700->regmap, 0x10, 0x2420);
292 		rt700_index_write(rt700->regmap, 0x19, 0x2e11);
293 
294 		dev_dbg(&rt700->slave->dev, "in %s enable\n", __func__);
295 
296 		mod_delayed_work(system_power_efficient_wq,
297 			&rt700->jack_detect_work, msecs_to_jiffies(250));
298 	} else {
299 		regmap_write(rt700->regmap,
300 			RT700_SET_MIC2_UNSOLICITED_ENABLE, 0x00);
301 		regmap_write(rt700->regmap,
302 			RT700_SET_HP_UNSOLICITED_ENABLE, 0x00);
303 		regmap_write(rt700->regmap,
304 			RT700_SET_INLINE_UNSOLICITED_ENABLE, 0x00);
305 
306 		dev_dbg(&rt700->slave->dev, "in %s disable\n", __func__);
307 	}
308 
309 	/* power off */
310 	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
311 		regmap_write(rt700->regmap,
312 			RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
313 }
314 
315 static int rt700_set_jack_detect(struct snd_soc_component *component,
316 	struct snd_soc_jack *hs_jack, void *data)
317 {
318 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
319 	int ret;
320 
321 	rt700->hs_jack = hs_jack;
322 
323 	ret = pm_runtime_resume_and_get(component->dev);
324 	if (ret < 0) {
325 		if (ret != -EACCES) {
326 			dev_err(component->dev, "%s: failed to resume %d\n", __func__, ret);
327 			return ret;
328 		}
329 
330 		/* pm_runtime not enabled yet */
331 		dev_dbg(component->dev,	"%s: skipping jack init for now\n", __func__);
332 		return 0;
333 	}
334 
335 	rt700_jack_init(rt700);
336 
337 	pm_runtime_mark_last_busy(component->dev);
338 	pm_runtime_put_autosuspend(component->dev);
339 
340 	return 0;
341 }
342 
343 static void rt700_get_gain(struct rt700_priv *rt700, unsigned int addr_h,
344 				unsigned int addr_l, unsigned int val_h,
345 				unsigned int *r_val, unsigned int *l_val)
346 {
347 	/* R Channel */
348 	*r_val = (val_h << 8);
349 	regmap_read(rt700->regmap, addr_l, r_val);
350 
351 	/* L Channel */
352 	val_h |= 0x20;
353 	*l_val = (val_h << 8);
354 	regmap_read(rt700->regmap, addr_h, l_val);
355 }
356 
357 /* For Verb-Set Amplifier Gain (Verb ID = 3h) */
358 static int rt700_set_amp_gain_put(struct snd_kcontrol *kcontrol,
359 		struct snd_ctl_elem_value *ucontrol)
360 {
361 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
362 	struct snd_soc_dapm_context *dapm =
363 		snd_soc_component_get_dapm(component);
364 	struct soc_mixer_control *mc =
365 		(struct soc_mixer_control *)kcontrol->private_value;
366 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
367 	unsigned int addr_h, addr_l, val_h, val_ll, val_lr;
368 	unsigned int read_ll, read_rl;
369 	int i;
370 
371 	/* Can't use update bit function, so read the original value first */
372 	addr_h = mc->reg;
373 	addr_l = mc->rreg;
374 	if (mc->shift == RT700_DIR_OUT_SFT) /* output */
375 		val_h = 0x80;
376 	else /* input */
377 		val_h = 0x0;
378 
379 	rt700_get_gain(rt700, addr_h, addr_l, val_h, &read_rl, &read_ll);
380 
381 	/* L Channel */
382 	if (mc->invert) {
383 		/* for mute */
384 		val_ll = (mc->max - ucontrol->value.integer.value[0]) << 7;
385 		/* keep gain */
386 		read_ll = read_ll & 0x7f;
387 		val_ll |= read_ll;
388 	} else {
389 		/* for gain */
390 		val_ll = ((ucontrol->value.integer.value[0]) & 0x7f);
391 		if (val_ll > mc->max)
392 			val_ll = mc->max;
393 		/* keep mute status */
394 		read_ll = read_ll & 0x80;
395 		val_ll |= read_ll;
396 	}
397 
398 	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
399 		regmap_write(rt700->regmap,
400 				RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
401 
402 	/* R Channel */
403 	if (mc->invert) {
404 		/* for mute */
405 		val_lr = (mc->max - ucontrol->value.integer.value[1]) << 7;
406 		/* keep gain */
407 		read_rl = read_rl & 0x7f;
408 		val_lr |= read_rl;
409 	} else {
410 		/* for gain */
411 		val_lr = ((ucontrol->value.integer.value[1]) & 0x7f);
412 		if (val_lr > mc->max)
413 			val_lr = mc->max;
414 		/* keep mute status */
415 		read_rl = read_rl & 0x80;
416 		val_lr |= read_rl;
417 	}
418 
419 	for (i = 0; i < 3; i++) { /* retry 3 times at most */
420 		if (val_ll == val_lr) {
421 			/* Set both L/R channels at the same time */
422 			val_h = (1 << mc->shift) | (3 << 4);
423 			regmap_write(rt700->regmap,
424 				addr_h, (val_h << 8 | val_ll));
425 			regmap_write(rt700->regmap,
426 				addr_l, (val_h << 8 | val_ll));
427 		} else {
428 			/* Lch*/
429 			val_h = (1 << mc->shift) | (1 << 5);
430 			regmap_write(rt700->regmap,
431 				addr_h, (val_h << 8 | val_ll));
432 
433 			/* Rch */
434 			val_h = (1 << mc->shift) | (1 << 4);
435 			regmap_write(rt700->regmap,
436 				addr_l, (val_h << 8 | val_lr));
437 		}
438 		/* check result */
439 		if (mc->shift == RT700_DIR_OUT_SFT) /* output */
440 			val_h = 0x80;
441 		else /* input */
442 			val_h = 0x0;
443 
444 		rt700_get_gain(rt700, addr_h, addr_l, val_h,
445 					&read_rl, &read_ll);
446 		if (read_rl == val_lr && read_ll == val_ll)
447 			break;
448 	}
449 
450 	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
451 		regmap_write(rt700->regmap,
452 				RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
453 	return 0;
454 }
455 
456 static int rt700_set_amp_gain_get(struct snd_kcontrol *kcontrol,
457 		struct snd_ctl_elem_value *ucontrol)
458 {
459 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
460 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
461 	struct soc_mixer_control *mc =
462 		(struct soc_mixer_control *)kcontrol->private_value;
463 	unsigned int addr_h, addr_l, val_h;
464 	unsigned int read_ll, read_rl;
465 
466 	addr_h = mc->reg;
467 	addr_l = mc->rreg;
468 	if (mc->shift == RT700_DIR_OUT_SFT) /* output */
469 		val_h = 0x80;
470 	else /* input */
471 		val_h = 0x0;
472 
473 	rt700_get_gain(rt700, addr_h, addr_l, val_h, &read_rl, &read_ll);
474 
475 	if (mc->invert) {
476 		/* for mute status */
477 		read_ll = !((read_ll & 0x80) >> RT700_MUTE_SFT);
478 		read_rl = !((read_rl & 0x80) >> RT700_MUTE_SFT);
479 	} else {
480 		/* for gain */
481 		read_ll = read_ll & 0x7f;
482 		read_rl = read_rl & 0x7f;
483 	}
484 	ucontrol->value.integer.value[0] = read_ll;
485 	ucontrol->value.integer.value[1] = read_rl;
486 
487 	return 0;
488 }
489 
490 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0);
491 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0);
492 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0);
493 
494 static const struct snd_kcontrol_new rt700_snd_controls[] = {
495 	SOC_DOUBLE_R_EXT_TLV("DAC Front Playback Volume",
496 		RT700_SET_GAIN_DAC1_H, RT700_SET_GAIN_DAC1_L,
497 		RT700_DIR_OUT_SFT, 0x57, 0,
498 		rt700_set_amp_gain_get, rt700_set_amp_gain_put, out_vol_tlv),
499 	SOC_DOUBLE_R_EXT("ADC 08 Capture Switch",
500 		RT700_SET_GAIN_ADC2_H, RT700_SET_GAIN_ADC2_L,
501 		RT700_DIR_IN_SFT, 1, 1,
502 		rt700_set_amp_gain_get, rt700_set_amp_gain_put),
503 	SOC_DOUBLE_R_EXT("ADC 09 Capture Switch",
504 		RT700_SET_GAIN_ADC1_H,	RT700_SET_GAIN_ADC1_L,
505 		RT700_DIR_IN_SFT, 1, 1,
506 		rt700_set_amp_gain_get, rt700_set_amp_gain_put),
507 	SOC_DOUBLE_R_EXT_TLV("ADC 08 Capture Volume",
508 		RT700_SET_GAIN_ADC2_H,	RT700_SET_GAIN_ADC2_L,
509 		RT700_DIR_IN_SFT, 0x3f, 0,
510 		rt700_set_amp_gain_get, rt700_set_amp_gain_put, in_vol_tlv),
511 	SOC_DOUBLE_R_EXT_TLV("ADC 09 Capture Volume",
512 		RT700_SET_GAIN_ADC1_H, RT700_SET_GAIN_ADC1_L,
513 		RT700_DIR_IN_SFT, 0x3f, 0,
514 		rt700_set_amp_gain_get, rt700_set_amp_gain_put, in_vol_tlv),
515 	SOC_DOUBLE_R_EXT_TLV("AMIC Volume",
516 		RT700_SET_GAIN_AMIC_H,	RT700_SET_GAIN_AMIC_L,
517 		RT700_DIR_IN_SFT, 3, 0,
518 		rt700_set_amp_gain_get, rt700_set_amp_gain_put, mic_vol_tlv),
519 };
520 
521 static int rt700_mux_get(struct snd_kcontrol *kcontrol,
522 			struct snd_ctl_elem_value *ucontrol)
523 {
524 	struct snd_soc_component *component =
525 		snd_soc_dapm_kcontrol_component(kcontrol);
526 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
527 	unsigned int reg, val = 0, nid;
528 	int ret;
529 
530 	if (strstr(ucontrol->id.name, "HPO Mux"))
531 		nid = RT700_HP_OUT;
532 	else if (strstr(ucontrol->id.name, "ADC 22 Mux"))
533 		nid = RT700_MIXER_IN1;
534 	else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
535 		nid = RT700_MIXER_IN2;
536 	else
537 		return -EINVAL;
538 
539 	/* vid = 0xf01 */
540 	reg = RT700_VERB_SET_CONNECT_SEL | nid;
541 	ret = regmap_read(rt700->regmap, reg, &val);
542 	if (ret < 0)
543 		return ret;
544 
545 	ucontrol->value.enumerated.item[0] = val;
546 
547 	return 0;
548 }
549 
550 static int rt700_mux_put(struct snd_kcontrol *kcontrol,
551 			struct snd_ctl_elem_value *ucontrol)
552 {
553 	struct snd_soc_component *component =
554 		snd_soc_dapm_kcontrol_component(kcontrol);
555 	struct snd_soc_dapm_context *dapm =
556 		snd_soc_dapm_kcontrol_dapm(kcontrol);
557 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
558 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
559 	unsigned int *item = ucontrol->value.enumerated.item;
560 	unsigned int val, val2 = 0, change, reg, nid;
561 	int ret;
562 
563 	if (item[0] >= e->items)
564 		return -EINVAL;
565 
566 	if (strstr(ucontrol->id.name, "HPO Mux"))
567 		nid = RT700_HP_OUT;
568 	else if (strstr(ucontrol->id.name, "ADC 22 Mux"))
569 		nid = RT700_MIXER_IN1;
570 	else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
571 		nid = RT700_MIXER_IN2;
572 	else
573 		return -EINVAL;
574 
575 	/* Verb ID = 0x701h */
576 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
577 
578 	reg = RT700_VERB_SET_CONNECT_SEL | nid;
579 	ret = regmap_read(rt700->regmap, reg, &val2);
580 	if (ret < 0)
581 		return ret;
582 
583 	if (val == val2)
584 		change = 0;
585 	else
586 		change = 1;
587 
588 	if (change) {
589 		reg = RT700_VERB_SET_CONNECT_SEL | nid;
590 		regmap_write(rt700->regmap, reg, val);
591 	}
592 
593 	snd_soc_dapm_mux_update_power(dapm, kcontrol,
594 						item[0], e, NULL);
595 
596 	return change;
597 }
598 
599 static const char * const adc_mux_text[] = {
600 	"MIC2",
601 	"LINE1",
602 	"LINE2",
603 	"DMIC",
604 };
605 
606 static SOC_ENUM_SINGLE_DECL(
607 	rt700_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text);
608 
609 static SOC_ENUM_SINGLE_DECL(
610 	rt700_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text);
611 
612 static const struct snd_kcontrol_new rt700_adc22_mux =
613 	SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt700_adc22_enum,
614 			rt700_mux_get, rt700_mux_put);
615 
616 static const struct snd_kcontrol_new rt700_adc23_mux =
617 	SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt700_adc23_enum,
618 			rt700_mux_get, rt700_mux_put);
619 
620 static const char * const out_mux_text[] = {
621 	"Front",
622 	"Surround",
623 };
624 
625 static SOC_ENUM_SINGLE_DECL(
626 	rt700_hp_enum, SND_SOC_NOPM, 0, out_mux_text);
627 
628 static const struct snd_kcontrol_new rt700_hp_mux =
629 	SOC_DAPM_ENUM_EXT("HP Mux", rt700_hp_enum,
630 			rt700_mux_get, rt700_mux_put);
631 
632 static int rt700_dac_front_event(struct snd_soc_dapm_widget *w,
633 	struct snd_kcontrol *kcontrol, int event)
634 {
635 	struct snd_soc_component *component =
636 		snd_soc_dapm_to_component(w->dapm);
637 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
638 
639 	switch (event) {
640 	case SND_SOC_DAPM_POST_PMU:
641 		regmap_write(rt700->regmap,
642 			RT700_SET_STREAMID_DAC1, 0x10);
643 		break;
644 	case SND_SOC_DAPM_PRE_PMD:
645 		regmap_write(rt700->regmap,
646 			RT700_SET_STREAMID_DAC1, 0x00);
647 		break;
648 	}
649 	return 0;
650 }
651 
652 static int rt700_dac_surround_event(struct snd_soc_dapm_widget *w,
653 	struct snd_kcontrol *kcontrol, int event)
654 {
655 	struct snd_soc_component *component =
656 		snd_soc_dapm_to_component(w->dapm);
657 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
658 
659 	switch (event) {
660 	case SND_SOC_DAPM_POST_PMU:
661 		regmap_write(rt700->regmap,
662 			RT700_SET_STREAMID_DAC2, 0x10);
663 		break;
664 	case SND_SOC_DAPM_PRE_PMD:
665 		regmap_write(rt700->regmap,
666 			RT700_SET_STREAMID_DAC2, 0x00);
667 		break;
668 	}
669 	return 0;
670 }
671 
672 static int rt700_adc_09_event(struct snd_soc_dapm_widget *w,
673 	struct snd_kcontrol *kcontrol, int event)
674 {
675 	struct snd_soc_component *component =
676 		snd_soc_dapm_to_component(w->dapm);
677 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
678 
679 	switch (event) {
680 	case SND_SOC_DAPM_POST_PMU:
681 		regmap_write(rt700->regmap,
682 			RT700_SET_STREAMID_ADC1, 0x10);
683 		break;
684 	case SND_SOC_DAPM_PRE_PMD:
685 		regmap_write(rt700->regmap,
686 			RT700_SET_STREAMID_ADC1, 0x00);
687 		break;
688 	}
689 	return 0;
690 }
691 
692 static int rt700_adc_08_event(struct snd_soc_dapm_widget *w,
693 	struct snd_kcontrol *kcontrol, int event)
694 {
695 	struct snd_soc_component *component =
696 		snd_soc_dapm_to_component(w->dapm);
697 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
698 
699 	switch (event) {
700 	case SND_SOC_DAPM_POST_PMU:
701 		regmap_write(rt700->regmap,
702 			RT700_SET_STREAMID_ADC2, 0x10);
703 		break;
704 	case SND_SOC_DAPM_PRE_PMD:
705 		regmap_write(rt700->regmap,
706 			RT700_SET_STREAMID_ADC2, 0x00);
707 		break;
708 	}
709 	return 0;
710 }
711 
712 static int rt700_hpo_mux_event(struct snd_soc_dapm_widget *w,
713 	struct snd_kcontrol *kcontrol, int event)
714 {
715 	struct snd_soc_component *component =
716 		snd_soc_dapm_to_component(w->dapm);
717 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
718 	unsigned int val_h = (1 << RT700_DIR_OUT_SFT) | (0x3 << 4);
719 	unsigned int val_l;
720 
721 	switch (event) {
722 	case SND_SOC_DAPM_POST_PMU:
723 		val_l = 0x00;
724 		regmap_write(rt700->regmap,
725 			RT700_SET_GAIN_HP_H, (val_h << 8 | val_l));
726 		break;
727 	case SND_SOC_DAPM_PRE_PMD:
728 		val_l = (1 << RT700_MUTE_SFT);
729 		regmap_write(rt700->regmap,
730 			RT700_SET_GAIN_HP_H, (val_h << 8 | val_l));
731 		usleep_range(50000, 55000);
732 		break;
733 	}
734 	return 0;
735 }
736 
737 static int rt700_spk_pga_event(struct snd_soc_dapm_widget *w,
738 	struct snd_kcontrol *kcontrol, int event)
739 {
740 	struct snd_soc_component *component =
741 		snd_soc_dapm_to_component(w->dapm);
742 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
743 	unsigned int val_h = (1 << RT700_DIR_OUT_SFT) | (0x3 << 4);
744 	unsigned int val_l;
745 
746 	switch (event) {
747 	case SND_SOC_DAPM_POST_PMU:
748 		val_l = 0x00;
749 		regmap_write(rt700->regmap,
750 			RT700_SET_GAIN_SPK_H, (val_h << 8 | val_l));
751 		break;
752 	case SND_SOC_DAPM_PRE_PMD:
753 		val_l = (1 << RT700_MUTE_SFT);
754 		regmap_write(rt700->regmap,
755 			RT700_SET_GAIN_SPK_H, (val_h << 8 | val_l));
756 		break;
757 	}
758 	return 0;
759 }
760 
761 static const struct snd_soc_dapm_widget rt700_dapm_widgets[] = {
762 	SND_SOC_DAPM_OUTPUT("HP"),
763 	SND_SOC_DAPM_OUTPUT("SPK"),
764 	SND_SOC_DAPM_INPUT("DMIC1"),
765 	SND_SOC_DAPM_INPUT("DMIC2"),
766 	SND_SOC_DAPM_INPUT("MIC2"),
767 	SND_SOC_DAPM_INPUT("LINE1"),
768 	SND_SOC_DAPM_INPUT("LINE2"),
769 	SND_SOC_DAPM_DAC_E("DAC Front", NULL, SND_SOC_NOPM, 0, 0,
770 		rt700_dac_front_event,
771 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
772 	SND_SOC_DAPM_DAC_E("DAC Surround", NULL, SND_SOC_NOPM, 0, 0,
773 		rt700_dac_surround_event,
774 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
775 	SND_SOC_DAPM_MUX_E("HPO Mux", SND_SOC_NOPM, 0, 0, &rt700_hp_mux,
776 		rt700_hpo_mux_event,
777 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
778 	SND_SOC_DAPM_PGA_E("SPK PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
779 		rt700_spk_pga_event,
780 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
781 	SND_SOC_DAPM_ADC_E("ADC 09", NULL, SND_SOC_NOPM, 0, 0,
782 		rt700_adc_09_event,
783 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
784 	SND_SOC_DAPM_ADC_E("ADC 08", NULL, SND_SOC_NOPM, 0, 0,
785 		rt700_adc_08_event,
786 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
787 	SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0,
788 		&rt700_adc22_mux),
789 	SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0,
790 		&rt700_adc23_mux),
791 	SND_SOC_DAPM_AIF_IN("DP1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0),
792 	SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0),
793 	SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0),
794 	SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0),
795 };
796 
797 static const struct snd_soc_dapm_route rt700_audio_map[] = {
798 	{"DAC Front", NULL, "DP1RX"},
799 	{"DAC Surround", NULL, "DP3RX"},
800 	{"DP2TX", NULL, "ADC 09"},
801 	{"DP4TX", NULL, "ADC 08"},
802 	{"ADC 09", NULL, "ADC 22 Mux"},
803 	{"ADC 08", NULL, "ADC 23 Mux"},
804 	{"ADC 22 Mux", "DMIC", "DMIC1"},
805 	{"ADC 22 Mux", "LINE1", "LINE1"},
806 	{"ADC 22 Mux", "LINE2", "LINE2"},
807 	{"ADC 22 Mux", "MIC2", "MIC2"},
808 	{"ADC 23 Mux", "DMIC", "DMIC2"},
809 	{"ADC 23 Mux", "LINE1", "LINE1"},
810 	{"ADC 23 Mux", "LINE2", "LINE2"},
811 	{"ADC 23 Mux", "MIC2", "MIC2"},
812 	{"HPO Mux", "Front", "DAC Front"},
813 	{"HPO Mux", "Surround", "DAC Surround"},
814 	{"HP", NULL, "HPO Mux"},
815 	{"SPK PGA", NULL, "DAC Front"},
816 	{"SPK", NULL, "SPK PGA"},
817 };
818 
819 static int rt700_probe(struct snd_soc_component *component)
820 {
821 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
822 	int ret;
823 
824 	rt700->component = component;
825 
826 	ret = pm_runtime_resume(component->dev);
827 	if (ret < 0 && ret != -EACCES)
828 		return ret;
829 
830 	return 0;
831 }
832 
833 static int rt700_set_bias_level(struct snd_soc_component *component,
834 				enum snd_soc_bias_level level)
835 {
836 	struct snd_soc_dapm_context *dapm =
837 		snd_soc_component_get_dapm(component);
838 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
839 
840 	switch (level) {
841 	case SND_SOC_BIAS_PREPARE:
842 		if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
843 			regmap_write(rt700->regmap,
844 				RT700_SET_AUDIO_POWER_STATE,
845 				AC_PWRST_D0);
846 		}
847 		break;
848 
849 	case SND_SOC_BIAS_STANDBY:
850 		regmap_write(rt700->regmap,
851 			RT700_SET_AUDIO_POWER_STATE,
852 			AC_PWRST_D3);
853 		break;
854 
855 	default:
856 		break;
857 	}
858 	dapm->bias_level = level;
859 	return 0;
860 }
861 
862 static const struct snd_soc_component_driver soc_codec_dev_rt700 = {
863 	.probe = rt700_probe,
864 	.set_bias_level = rt700_set_bias_level,
865 	.controls = rt700_snd_controls,
866 	.num_controls = ARRAY_SIZE(rt700_snd_controls),
867 	.dapm_widgets = rt700_dapm_widgets,
868 	.num_dapm_widgets = ARRAY_SIZE(rt700_dapm_widgets),
869 	.dapm_routes = rt700_audio_map,
870 	.num_dapm_routes = ARRAY_SIZE(rt700_audio_map),
871 	.set_jack = rt700_set_jack_detect,
872 	.endianness = 1,
873 };
874 
875 static int rt700_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
876 				int direction)
877 {
878 	struct sdw_stream_data *stream;
879 
880 	if (!sdw_stream)
881 		return 0;
882 
883 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
884 	if (!stream)
885 		return -ENOMEM;
886 
887 	stream->sdw_stream = sdw_stream;
888 
889 	/* Use tx_mask or rx_mask to configure stream tag and set dma_data */
890 	snd_soc_dai_dma_data_set(dai, direction, stream);
891 
892 	return 0;
893 }
894 
895 static void rt700_shutdown(struct snd_pcm_substream *substream,
896 				struct snd_soc_dai *dai)
897 {
898 	struct sdw_stream_data *stream;
899 
900 	stream = snd_soc_dai_get_dma_data(dai, substream);
901 	snd_soc_dai_set_dma_data(dai, substream, NULL);
902 	kfree(stream);
903 }
904 
905 static int rt700_pcm_hw_params(struct snd_pcm_substream *substream,
906 					struct snd_pcm_hw_params *params,
907 					struct snd_soc_dai *dai)
908 {
909 	struct snd_soc_component *component = dai->component;
910 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
911 	struct sdw_stream_config stream_config = {0};
912 	struct sdw_port_config port_config = {0};
913 	struct sdw_stream_data *stream;
914 	int retval;
915 	unsigned int val = 0;
916 
917 	dev_dbg(dai->dev, "%s %s", __func__, dai->name);
918 	stream = snd_soc_dai_get_dma_data(dai, substream);
919 
920 	if (!stream)
921 		return -EINVAL;
922 
923 	if (!rt700->slave)
924 		return -EINVAL;
925 
926 	/* SoundWire specific configuration */
927 	snd_sdw_params_to_config(substream, params, &stream_config, &port_config);
928 
929 	/* This code assumes port 1 for playback and port 2 for capture */
930 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
931 		port_config.num = 1;
932 	else
933 		port_config.num = 2;
934 
935 	switch (dai->id) {
936 	case RT700_AIF1:
937 		break;
938 	case RT700_AIF2:
939 		port_config.num += 2;
940 		break;
941 	default:
942 		dev_err(component->dev, "Invalid DAI id %d\n", dai->id);
943 		return -EINVAL;
944 	}
945 
946 	retval = sdw_stream_add_slave(rt700->slave, &stream_config,
947 					&port_config, 1, stream->sdw_stream);
948 	if (retval) {
949 		dev_err(dai->dev, "Unable to configure port\n");
950 		return retval;
951 	}
952 
953 	if (params_channels(params) <= 16) {
954 		/* bit 3:0 Number of Channel */
955 		val |= (params_channels(params) - 1);
956 	} else {
957 		dev_err(component->dev, "Unsupported channels %d\n",
958 			params_channels(params));
959 		return -EINVAL;
960 	}
961 
962 	switch (params_width(params)) {
963 	/* bit 6:4 Bits per Sample */
964 	case 8:
965 		break;
966 	case 16:
967 		val |= (0x1 << 4);
968 		break;
969 	case 20:
970 		val |= (0x2 << 4);
971 		break;
972 	case 24:
973 		val |= (0x3 << 4);
974 		break;
975 	case 32:
976 		val |= (0x4 << 4);
977 		break;
978 	default:
979 		return -EINVAL;
980 	}
981 
982 	/* 48Khz */
983 	regmap_write(rt700->regmap, RT700_DAC_FORMAT_H, val);
984 	regmap_write(rt700->regmap, RT700_ADC_FORMAT_H, val);
985 
986 	return retval;
987 }
988 
989 static int rt700_pcm_hw_free(struct snd_pcm_substream *substream,
990 				struct snd_soc_dai *dai)
991 {
992 	struct snd_soc_component *component = dai->component;
993 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
994 	struct sdw_stream_data *stream =
995 		snd_soc_dai_get_dma_data(dai, substream);
996 
997 	if (!rt700->slave)
998 		return -EINVAL;
999 
1000 	sdw_stream_remove_slave(rt700->slave, stream->sdw_stream);
1001 	return 0;
1002 }
1003 
1004 #define RT700_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
1005 #define RT700_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1006 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1007 
1008 static const struct snd_soc_dai_ops rt700_ops = {
1009 	.hw_params	= rt700_pcm_hw_params,
1010 	.hw_free	= rt700_pcm_hw_free,
1011 	.set_stream	= rt700_set_sdw_stream,
1012 	.shutdown	= rt700_shutdown,
1013 };
1014 
1015 static struct snd_soc_dai_driver rt700_dai[] = {
1016 	{
1017 		.name = "rt700-aif1",
1018 		.id = RT700_AIF1,
1019 		.playback = {
1020 			.stream_name = "DP1 Playback",
1021 			.channels_min = 1,
1022 			.channels_max = 2,
1023 			.rates = RT700_STEREO_RATES,
1024 			.formats = RT700_FORMATS,
1025 		},
1026 		.capture = {
1027 			.stream_name = "DP2 Capture",
1028 			.channels_min = 1,
1029 			.channels_max = 2,
1030 			.rates = RT700_STEREO_RATES,
1031 			.formats = RT700_FORMATS,
1032 		},
1033 		.ops = &rt700_ops,
1034 	},
1035 	{
1036 		.name = "rt700-aif2",
1037 		.id = RT700_AIF2,
1038 		.playback = {
1039 			.stream_name = "DP3 Playback",
1040 			.channels_min = 1,
1041 			.channels_max = 2,
1042 			.rates = RT700_STEREO_RATES,
1043 			.formats = RT700_FORMATS,
1044 		},
1045 		.capture = {
1046 			.stream_name = "DP4 Capture",
1047 			.channels_min = 1,
1048 			.channels_max = 2,
1049 			.rates = RT700_STEREO_RATES,
1050 			.formats = RT700_FORMATS,
1051 		},
1052 		.ops = &rt700_ops,
1053 	},
1054 };
1055 
1056 /* Bus clock frequency */
1057 #define RT700_CLK_FREQ_9600000HZ 9600000
1058 #define RT700_CLK_FREQ_12000000HZ 12000000
1059 #define RT700_CLK_FREQ_6000000HZ 6000000
1060 #define RT700_CLK_FREQ_4800000HZ 4800000
1061 #define RT700_CLK_FREQ_2400000HZ 2400000
1062 #define RT700_CLK_FREQ_12288000HZ 12288000
1063 
1064 int rt700_clock_config(struct device *dev)
1065 {
1066 	struct rt700_priv *rt700 = dev_get_drvdata(dev);
1067 	unsigned int clk_freq, value;
1068 
1069 	clk_freq = (rt700->params.curr_dr_freq >> 1);
1070 
1071 	switch (clk_freq) {
1072 	case RT700_CLK_FREQ_12000000HZ:
1073 		value = 0x0;
1074 		break;
1075 	case RT700_CLK_FREQ_6000000HZ:
1076 		value = 0x1;
1077 		break;
1078 	case RT700_CLK_FREQ_9600000HZ:
1079 		value = 0x2;
1080 		break;
1081 	case RT700_CLK_FREQ_4800000HZ:
1082 		value = 0x3;
1083 		break;
1084 	case RT700_CLK_FREQ_2400000HZ:
1085 		value = 0x4;
1086 		break;
1087 	case RT700_CLK_FREQ_12288000HZ:
1088 		value = 0x5;
1089 		break;
1090 	default:
1091 		return -EINVAL;
1092 	}
1093 
1094 	regmap_write(rt700->regmap, 0xe0, value);
1095 	regmap_write(rt700->regmap, 0xf0, value);
1096 
1097 	dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
1098 
1099 	return 0;
1100 }
1101 
1102 int rt700_init(struct device *dev, struct regmap *sdw_regmap,
1103 			struct regmap *regmap, struct sdw_slave *slave)
1104 
1105 {
1106 	struct rt700_priv *rt700;
1107 	int ret;
1108 
1109 	rt700 = devm_kzalloc(dev, sizeof(*rt700), GFP_KERNEL);
1110 	if (!rt700)
1111 		return -ENOMEM;
1112 
1113 	dev_set_drvdata(dev, rt700);
1114 	rt700->slave = slave;
1115 	rt700->sdw_regmap = sdw_regmap;
1116 	rt700->regmap = regmap;
1117 
1118 	mutex_init(&rt700->disable_irq_lock);
1119 
1120 	INIT_DELAYED_WORK(&rt700->jack_detect_work,
1121 			  rt700_jack_detect_handler);
1122 	INIT_DELAYED_WORK(&rt700->jack_btn_check_work,
1123 			  rt700_btn_check_handler);
1124 
1125 	/*
1126 	 * Mark hw_init to false
1127 	 * HW init will be performed when device reports present
1128 	 */
1129 	rt700->hw_init = false;
1130 	rt700->first_hw_init = false;
1131 
1132 	ret =  devm_snd_soc_register_component(dev,
1133 				&soc_codec_dev_rt700,
1134 				rt700_dai,
1135 				ARRAY_SIZE(rt700_dai));
1136 
1137 	dev_dbg(&slave->dev, "%s\n", __func__);
1138 
1139 	return ret;
1140 }
1141 
1142 int rt700_io_init(struct device *dev, struct sdw_slave *slave)
1143 {
1144 	struct rt700_priv *rt700 = dev_get_drvdata(dev);
1145 
1146 	rt700->disable_irq = false;
1147 
1148 	if (rt700->hw_init)
1149 		return 0;
1150 
1151 	if (rt700->first_hw_init) {
1152 		regcache_cache_only(rt700->regmap, false);
1153 		regcache_cache_bypass(rt700->regmap, true);
1154 	}
1155 
1156 	/*
1157 	 * PM runtime is only enabled when a Slave reports as Attached
1158 	 */
1159 	if (!rt700->first_hw_init) {
1160 		/* set autosuspend parameters */
1161 		pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
1162 		pm_runtime_use_autosuspend(&slave->dev);
1163 
1164 		/* update count of parent 'active' children */
1165 		pm_runtime_set_active(&slave->dev);
1166 
1167 		/* make sure the device does not suspend immediately */
1168 		pm_runtime_mark_last_busy(&slave->dev);
1169 
1170 		pm_runtime_enable(&slave->dev);
1171 	}
1172 
1173 	pm_runtime_get_noresume(&slave->dev);
1174 
1175 	/* reset */
1176 	regmap_write(rt700->regmap, 0xff01, 0x0000);
1177 	regmap_write(rt700->regmap, 0x7520, 0x001a);
1178 	regmap_write(rt700->regmap, 0x7420, 0xc003);
1179 
1180 	/* power on */
1181 	regmap_write(rt700->regmap, RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
1182 	/* Set Pin Widget */
1183 	regmap_write(rt700->regmap, RT700_SET_PIN_HP, 0x40);
1184 	regmap_write(rt700->regmap, RT700_SET_PIN_SPK, 0x40);
1185 	regmap_write(rt700->regmap, RT700_SET_EAPD_SPK, RT700_EAPD_HIGH);
1186 	regmap_write(rt700->regmap, RT700_SET_PIN_DMIC1, 0x20);
1187 	regmap_write(rt700->regmap, RT700_SET_PIN_DMIC2, 0x20);
1188 	regmap_write(rt700->regmap, RT700_SET_PIN_MIC2, 0x20);
1189 
1190 	/* Set Configuration Default */
1191 	regmap_write(rt700->regmap, 0x4f12, 0x91);
1192 	regmap_write(rt700->regmap, 0x4e12, 0xd6);
1193 	regmap_write(rt700->regmap, 0x4d12, 0x11);
1194 	regmap_write(rt700->regmap, 0x4c12, 0x20);
1195 	regmap_write(rt700->regmap, 0x4f13, 0x91);
1196 	regmap_write(rt700->regmap, 0x4e13, 0xd6);
1197 	regmap_write(rt700->regmap, 0x4d13, 0x11);
1198 	regmap_write(rt700->regmap, 0x4c13, 0x21);
1199 
1200 	regmap_write(rt700->regmap, 0x4f19, 0x02);
1201 	regmap_write(rt700->regmap, 0x4e19, 0xa1);
1202 	regmap_write(rt700->regmap, 0x4d19, 0x90);
1203 	regmap_write(rt700->regmap, 0x4c19, 0x80);
1204 
1205 	/* Enable Line2 */
1206 	regmap_write(rt700->regmap,  0x371b, 0x40);
1207 	regmap_write(rt700->regmap,  0x731b, 0xb0);
1208 	regmap_write(rt700->regmap,  0x839b, 0x00);
1209 
1210 	/* Set index */
1211 	rt700_index_write(rt700->regmap, 0x4a, 0x201b);
1212 	rt700_index_write(rt700->regmap, 0x45, 0x5089);
1213 	rt700_index_write(rt700->regmap, 0x6b, 0x5064);
1214 	rt700_index_write(rt700->regmap, 0x48, 0xd249);
1215 
1216 	/* Finish Initial Settings, set power to D3 */
1217 	regmap_write(rt700->regmap, RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
1218 
1219 	/*
1220 	 * if set_jack callback occurred early than io_init,
1221 	 * we set up the jack detection function now
1222 	 */
1223 	if (rt700->hs_jack)
1224 		rt700_jack_init(rt700);
1225 
1226 	if (rt700->first_hw_init) {
1227 		regcache_cache_bypass(rt700->regmap, false);
1228 		regcache_mark_dirty(rt700->regmap);
1229 	} else
1230 		rt700->first_hw_init = true;
1231 
1232 	/* Mark Slave initialization complete */
1233 	rt700->hw_init = true;
1234 
1235 	pm_runtime_mark_last_busy(&slave->dev);
1236 	pm_runtime_put_autosuspend(&slave->dev);
1237 
1238 	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
1239 
1240 	return 0;
1241 }
1242 
1243 MODULE_DESCRIPTION("ASoC RT700 driver SDW");
1244 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1245 MODULE_LICENSE("GPL v2");
1246