xref: /openbmc/linux/sound/soc/codecs/rt700.c (revision 7559e757)
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 	snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
879 
880 	return 0;
881 }
882 
883 static void rt700_shutdown(struct snd_pcm_substream *substream,
884 				struct snd_soc_dai *dai)
885 {
886 	snd_soc_dai_set_dma_data(dai, substream, NULL);
887 }
888 
889 static int rt700_pcm_hw_params(struct snd_pcm_substream *substream,
890 					struct snd_pcm_hw_params *params,
891 					struct snd_soc_dai *dai)
892 {
893 	struct snd_soc_component *component = dai->component;
894 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
895 	struct sdw_stream_config stream_config = {0};
896 	struct sdw_port_config port_config = {0};
897 	struct sdw_stream_runtime *sdw_stream;
898 	int retval;
899 	unsigned int val = 0;
900 
901 	dev_dbg(dai->dev, "%s %s", __func__, dai->name);
902 	sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
903 
904 	if (!sdw_stream)
905 		return -EINVAL;
906 
907 	if (!rt700->slave)
908 		return -EINVAL;
909 
910 	/* SoundWire specific configuration */
911 	snd_sdw_params_to_config(substream, params, &stream_config, &port_config);
912 
913 	/* This code assumes port 1 for playback and port 2 for capture */
914 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
915 		port_config.num = 1;
916 	else
917 		port_config.num = 2;
918 
919 	switch (dai->id) {
920 	case RT700_AIF1:
921 		break;
922 	case RT700_AIF2:
923 		port_config.num += 2;
924 		break;
925 	default:
926 		dev_err(component->dev, "Invalid DAI id %d\n", dai->id);
927 		return -EINVAL;
928 	}
929 
930 	retval = sdw_stream_add_slave(rt700->slave, &stream_config,
931 					&port_config, 1, sdw_stream);
932 	if (retval) {
933 		dev_err(dai->dev, "Unable to configure port\n");
934 		return retval;
935 	}
936 
937 	if (params_channels(params) <= 16) {
938 		/* bit 3:0 Number of Channel */
939 		val |= (params_channels(params) - 1);
940 	} else {
941 		dev_err(component->dev, "Unsupported channels %d\n",
942 			params_channels(params));
943 		return -EINVAL;
944 	}
945 
946 	switch (params_width(params)) {
947 	/* bit 6:4 Bits per Sample */
948 	case 8:
949 		break;
950 	case 16:
951 		val |= (0x1 << 4);
952 		break;
953 	case 20:
954 		val |= (0x2 << 4);
955 		break;
956 	case 24:
957 		val |= (0x3 << 4);
958 		break;
959 	case 32:
960 		val |= (0x4 << 4);
961 		break;
962 	default:
963 		return -EINVAL;
964 	}
965 
966 	/* 48Khz */
967 	regmap_write(rt700->regmap, RT700_DAC_FORMAT_H, val);
968 	regmap_write(rt700->regmap, RT700_ADC_FORMAT_H, val);
969 
970 	return retval;
971 }
972 
973 static int rt700_pcm_hw_free(struct snd_pcm_substream *substream,
974 				struct snd_soc_dai *dai)
975 {
976 	struct snd_soc_component *component = dai->component;
977 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
978 	struct sdw_stream_runtime *sdw_stream =
979 		snd_soc_dai_get_dma_data(dai, substream);
980 
981 	if (!rt700->slave)
982 		return -EINVAL;
983 
984 	sdw_stream_remove_slave(rt700->slave, sdw_stream);
985 	return 0;
986 }
987 
988 #define RT700_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
989 #define RT700_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
990 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
991 
992 static const struct snd_soc_dai_ops rt700_ops = {
993 	.hw_params	= rt700_pcm_hw_params,
994 	.hw_free	= rt700_pcm_hw_free,
995 	.set_stream	= rt700_set_sdw_stream,
996 	.shutdown	= rt700_shutdown,
997 };
998 
999 static struct snd_soc_dai_driver rt700_dai[] = {
1000 	{
1001 		.name = "rt700-aif1",
1002 		.id = RT700_AIF1,
1003 		.playback = {
1004 			.stream_name = "DP1 Playback",
1005 			.channels_min = 1,
1006 			.channels_max = 2,
1007 			.rates = RT700_STEREO_RATES,
1008 			.formats = RT700_FORMATS,
1009 		},
1010 		.capture = {
1011 			.stream_name = "DP2 Capture",
1012 			.channels_min = 1,
1013 			.channels_max = 2,
1014 			.rates = RT700_STEREO_RATES,
1015 			.formats = RT700_FORMATS,
1016 		},
1017 		.ops = &rt700_ops,
1018 	},
1019 	{
1020 		.name = "rt700-aif2",
1021 		.id = RT700_AIF2,
1022 		.playback = {
1023 			.stream_name = "DP3 Playback",
1024 			.channels_min = 1,
1025 			.channels_max = 2,
1026 			.rates = RT700_STEREO_RATES,
1027 			.formats = RT700_FORMATS,
1028 		},
1029 		.capture = {
1030 			.stream_name = "DP4 Capture",
1031 			.channels_min = 1,
1032 			.channels_max = 2,
1033 			.rates = RT700_STEREO_RATES,
1034 			.formats = RT700_FORMATS,
1035 		},
1036 		.ops = &rt700_ops,
1037 	},
1038 };
1039 
1040 /* Bus clock frequency */
1041 #define RT700_CLK_FREQ_9600000HZ 9600000
1042 #define RT700_CLK_FREQ_12000000HZ 12000000
1043 #define RT700_CLK_FREQ_6000000HZ 6000000
1044 #define RT700_CLK_FREQ_4800000HZ 4800000
1045 #define RT700_CLK_FREQ_2400000HZ 2400000
1046 #define RT700_CLK_FREQ_12288000HZ 12288000
1047 
1048 int rt700_clock_config(struct device *dev)
1049 {
1050 	struct rt700_priv *rt700 = dev_get_drvdata(dev);
1051 	unsigned int clk_freq, value;
1052 
1053 	clk_freq = (rt700->params.curr_dr_freq >> 1);
1054 
1055 	switch (clk_freq) {
1056 	case RT700_CLK_FREQ_12000000HZ:
1057 		value = 0x0;
1058 		break;
1059 	case RT700_CLK_FREQ_6000000HZ:
1060 		value = 0x1;
1061 		break;
1062 	case RT700_CLK_FREQ_9600000HZ:
1063 		value = 0x2;
1064 		break;
1065 	case RT700_CLK_FREQ_4800000HZ:
1066 		value = 0x3;
1067 		break;
1068 	case RT700_CLK_FREQ_2400000HZ:
1069 		value = 0x4;
1070 		break;
1071 	case RT700_CLK_FREQ_12288000HZ:
1072 		value = 0x5;
1073 		break;
1074 	default:
1075 		return -EINVAL;
1076 	}
1077 
1078 	regmap_write(rt700->regmap, 0xe0, value);
1079 	regmap_write(rt700->regmap, 0xf0, value);
1080 
1081 	dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
1082 
1083 	return 0;
1084 }
1085 
1086 int rt700_init(struct device *dev, struct regmap *sdw_regmap,
1087 			struct regmap *regmap, struct sdw_slave *slave)
1088 
1089 {
1090 	struct rt700_priv *rt700;
1091 	int ret;
1092 
1093 	rt700 = devm_kzalloc(dev, sizeof(*rt700), GFP_KERNEL);
1094 	if (!rt700)
1095 		return -ENOMEM;
1096 
1097 	dev_set_drvdata(dev, rt700);
1098 	rt700->slave = slave;
1099 	rt700->sdw_regmap = sdw_regmap;
1100 	rt700->regmap = regmap;
1101 
1102 	mutex_init(&rt700->disable_irq_lock);
1103 
1104 	INIT_DELAYED_WORK(&rt700->jack_detect_work,
1105 			  rt700_jack_detect_handler);
1106 	INIT_DELAYED_WORK(&rt700->jack_btn_check_work,
1107 			  rt700_btn_check_handler);
1108 
1109 	/*
1110 	 * Mark hw_init to false
1111 	 * HW init will be performed when device reports present
1112 	 */
1113 	rt700->hw_init = false;
1114 	rt700->first_hw_init = false;
1115 
1116 	ret =  devm_snd_soc_register_component(dev,
1117 				&soc_codec_dev_rt700,
1118 				rt700_dai,
1119 				ARRAY_SIZE(rt700_dai));
1120 
1121 	dev_dbg(&slave->dev, "%s\n", __func__);
1122 
1123 	return ret;
1124 }
1125 
1126 int rt700_io_init(struct device *dev, struct sdw_slave *slave)
1127 {
1128 	struct rt700_priv *rt700 = dev_get_drvdata(dev);
1129 
1130 	rt700->disable_irq = false;
1131 
1132 	if (rt700->hw_init)
1133 		return 0;
1134 
1135 	if (rt700->first_hw_init) {
1136 		regcache_cache_only(rt700->regmap, false);
1137 		regcache_cache_bypass(rt700->regmap, true);
1138 	}
1139 
1140 	/*
1141 	 * PM runtime is only enabled when a Slave reports as Attached
1142 	 */
1143 	if (!rt700->first_hw_init) {
1144 		/* set autosuspend parameters */
1145 		pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
1146 		pm_runtime_use_autosuspend(&slave->dev);
1147 
1148 		/* update count of parent 'active' children */
1149 		pm_runtime_set_active(&slave->dev);
1150 
1151 		/* make sure the device does not suspend immediately */
1152 		pm_runtime_mark_last_busy(&slave->dev);
1153 
1154 		pm_runtime_enable(&slave->dev);
1155 	}
1156 
1157 	pm_runtime_get_noresume(&slave->dev);
1158 
1159 	/* reset */
1160 	regmap_write(rt700->regmap, 0xff01, 0x0000);
1161 	regmap_write(rt700->regmap, 0x7520, 0x001a);
1162 	regmap_write(rt700->regmap, 0x7420, 0xc003);
1163 
1164 	/* power on */
1165 	regmap_write(rt700->regmap, RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
1166 	/* Set Pin Widget */
1167 	regmap_write(rt700->regmap, RT700_SET_PIN_HP, 0x40);
1168 	regmap_write(rt700->regmap, RT700_SET_PIN_SPK, 0x40);
1169 	regmap_write(rt700->regmap, RT700_SET_EAPD_SPK, RT700_EAPD_HIGH);
1170 	regmap_write(rt700->regmap, RT700_SET_PIN_DMIC1, 0x20);
1171 	regmap_write(rt700->regmap, RT700_SET_PIN_DMIC2, 0x20);
1172 	regmap_write(rt700->regmap, RT700_SET_PIN_MIC2, 0x20);
1173 
1174 	/* Set Configuration Default */
1175 	regmap_write(rt700->regmap, 0x4f12, 0x91);
1176 	regmap_write(rt700->regmap, 0x4e12, 0xd6);
1177 	regmap_write(rt700->regmap, 0x4d12, 0x11);
1178 	regmap_write(rt700->regmap, 0x4c12, 0x20);
1179 	regmap_write(rt700->regmap, 0x4f13, 0x91);
1180 	regmap_write(rt700->regmap, 0x4e13, 0xd6);
1181 	regmap_write(rt700->regmap, 0x4d13, 0x11);
1182 	regmap_write(rt700->regmap, 0x4c13, 0x21);
1183 
1184 	regmap_write(rt700->regmap, 0x4f19, 0x02);
1185 	regmap_write(rt700->regmap, 0x4e19, 0xa1);
1186 	regmap_write(rt700->regmap, 0x4d19, 0x90);
1187 	regmap_write(rt700->regmap, 0x4c19, 0x80);
1188 
1189 	/* Enable Line2 */
1190 	regmap_write(rt700->regmap,  0x371b, 0x40);
1191 	regmap_write(rt700->regmap,  0x731b, 0xb0);
1192 	regmap_write(rt700->regmap,  0x839b, 0x00);
1193 
1194 	/* Set index */
1195 	rt700_index_write(rt700->regmap, 0x4a, 0x201b);
1196 	rt700_index_write(rt700->regmap, 0x45, 0x5089);
1197 	rt700_index_write(rt700->regmap, 0x6b, 0x5064);
1198 	rt700_index_write(rt700->regmap, 0x48, 0xd249);
1199 
1200 	/* Finish Initial Settings, set power to D3 */
1201 	regmap_write(rt700->regmap, RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
1202 
1203 	/*
1204 	 * if set_jack callback occurred early than io_init,
1205 	 * we set up the jack detection function now
1206 	 */
1207 	if (rt700->hs_jack)
1208 		rt700_jack_init(rt700);
1209 
1210 	if (rt700->first_hw_init) {
1211 		regcache_cache_bypass(rt700->regmap, false);
1212 		regcache_mark_dirty(rt700->regmap);
1213 	} else
1214 		rt700->first_hw_init = true;
1215 
1216 	/* Mark Slave initialization complete */
1217 	rt700->hw_init = true;
1218 
1219 	pm_runtime_mark_last_busy(&slave->dev);
1220 	pm_runtime_put_autosuspend(&slave->dev);
1221 
1222 	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
1223 
1224 	return 0;
1225 }
1226 
1227 MODULE_DESCRIPTION("ASoC RT700 driver SDW");
1228 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1229 MODULE_LICENSE("GPL v2");
1230