xref: /linux/sound/soc/codecs/rt700.c (revision c058e7a8f8af355e4a441c89400a6e95a16320e5)
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 
rt700_index_write(struct regmap * regmap,unsigned int reg,unsigned int value)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("%s: Failed to set private value: %06x <= %04x ret=%d\n",
41 		       __func__, addr, value, ret);
42 
43 	return ret;
44 }
45 
rt700_index_read(struct regmap * regmap,unsigned int reg,unsigned int * value)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("%s: Failed to get private value: %06x => %04x ret=%d\n",
56 		       __func__, addr, *value, ret);
57 
58 	return ret;
59 }
60 
rt700_button_detect(struct rt700_priv * rt700)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 
rt700_headset_detect(struct rt700_priv * rt700)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 
rt700_jack_detect_handler(struct work_struct * work)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 
rt700_btn_check_handler(struct work_struct * work)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 
rt700_jack_init(struct rt700_priv * rt700)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 
rt700_set_jack_detect(struct snd_soc_component * component,struct snd_soc_jack * hs_jack,void * data)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 	/* we can only resume if the device was initialized at least once */
324 	if (!rt700->first_hw_init)
325 		return 0;
326 
327 	ret = pm_runtime_resume_and_get(component->dev);
328 	if (ret < 0) {
329 		if (ret != -EACCES) {
330 			dev_err(component->dev, "%s: failed to resume %d\n", __func__, ret);
331 			return ret;
332 		}
333 
334 		/* pm_runtime not enabled yet */
335 		dev_dbg(component->dev,	"%s: skipping jack init for now\n", __func__);
336 		return 0;
337 	}
338 
339 	rt700_jack_init(rt700);
340 
341 	pm_runtime_mark_last_busy(component->dev);
342 	pm_runtime_put_autosuspend(component->dev);
343 
344 	return 0;
345 }
346 
rt700_get_gain(struct rt700_priv * rt700,unsigned int addr_h,unsigned int addr_l,unsigned int val_h,unsigned int * r_val,unsigned int * l_val)347 static void rt700_get_gain(struct rt700_priv *rt700, unsigned int addr_h,
348 				unsigned int addr_l, unsigned int val_h,
349 				unsigned int *r_val, unsigned int *l_val)
350 {
351 	/* R Channel */
352 	*r_val = (val_h << 8);
353 	regmap_read(rt700->regmap, addr_l, r_val);
354 
355 	/* L Channel */
356 	val_h |= 0x20;
357 	*l_val = (val_h << 8);
358 	regmap_read(rt700->regmap, addr_h, l_val);
359 }
360 
361 /* For Verb-Set Amplifier Gain (Verb ID = 3h) */
rt700_set_amp_gain_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)362 static int rt700_set_amp_gain_put(struct snd_kcontrol *kcontrol,
363 		struct snd_ctl_elem_value *ucontrol)
364 {
365 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
366 	struct snd_soc_dapm_context *dapm =
367 		snd_soc_component_get_dapm(component);
368 	struct soc_mixer_control *mc =
369 		(struct soc_mixer_control *)kcontrol->private_value;
370 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
371 	unsigned int addr_h, addr_l, val_h, val_ll, val_lr;
372 	unsigned int read_ll, read_rl;
373 	int i;
374 
375 	/* Can't use update bit function, so read the original value first */
376 	addr_h = mc->reg;
377 	addr_l = mc->rreg;
378 	if (mc->shift == RT700_DIR_OUT_SFT) /* output */
379 		val_h = 0x80;
380 	else /* input */
381 		val_h = 0x0;
382 
383 	rt700_get_gain(rt700, addr_h, addr_l, val_h, &read_rl, &read_ll);
384 
385 	/* L Channel */
386 	if (mc->invert) {
387 		/* for mute */
388 		val_ll = (mc->max - ucontrol->value.integer.value[0]) << 7;
389 		/* keep gain */
390 		read_ll = read_ll & 0x7f;
391 		val_ll |= read_ll;
392 	} else {
393 		/* for gain */
394 		val_ll = ((ucontrol->value.integer.value[0]) & 0x7f);
395 		if (val_ll > mc->max)
396 			val_ll = mc->max;
397 		/* keep mute status */
398 		read_ll = read_ll & 0x80;
399 		val_ll |= read_ll;
400 	}
401 
402 	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
403 		regmap_write(rt700->regmap,
404 				RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
405 
406 	/* R Channel */
407 	if (mc->invert) {
408 		/* for mute */
409 		val_lr = (mc->max - ucontrol->value.integer.value[1]) << 7;
410 		/* keep gain */
411 		read_rl = read_rl & 0x7f;
412 		val_lr |= read_rl;
413 	} else {
414 		/* for gain */
415 		val_lr = ((ucontrol->value.integer.value[1]) & 0x7f);
416 		if (val_lr > mc->max)
417 			val_lr = mc->max;
418 		/* keep mute status */
419 		read_rl = read_rl & 0x80;
420 		val_lr |= read_rl;
421 	}
422 
423 	for (i = 0; i < 3; i++) { /* retry 3 times at most */
424 		if (val_ll == val_lr) {
425 			/* Set both L/R channels at the same time */
426 			val_h = (1 << mc->shift) | (3 << 4);
427 			regmap_write(rt700->regmap,
428 				addr_h, (val_h << 8 | val_ll));
429 			regmap_write(rt700->regmap,
430 				addr_l, (val_h << 8 | val_ll));
431 		} else {
432 			/* Lch*/
433 			val_h = (1 << mc->shift) | (1 << 5);
434 			regmap_write(rt700->regmap,
435 				addr_h, (val_h << 8 | val_ll));
436 
437 			/* Rch */
438 			val_h = (1 << mc->shift) | (1 << 4);
439 			regmap_write(rt700->regmap,
440 				addr_l, (val_h << 8 | val_lr));
441 		}
442 		/* check result */
443 		if (mc->shift == RT700_DIR_OUT_SFT) /* output */
444 			val_h = 0x80;
445 		else /* input */
446 			val_h = 0x0;
447 
448 		rt700_get_gain(rt700, addr_h, addr_l, val_h,
449 					&read_rl, &read_ll);
450 		if (read_rl == val_lr && read_ll == val_ll)
451 			break;
452 	}
453 
454 	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
455 		regmap_write(rt700->regmap,
456 				RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
457 	return 0;
458 }
459 
rt700_set_amp_gain_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)460 static int rt700_set_amp_gain_get(struct snd_kcontrol *kcontrol,
461 		struct snd_ctl_elem_value *ucontrol)
462 {
463 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
464 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
465 	struct soc_mixer_control *mc =
466 		(struct soc_mixer_control *)kcontrol->private_value;
467 	unsigned int addr_h, addr_l, val_h;
468 	unsigned int read_ll, read_rl;
469 
470 	addr_h = mc->reg;
471 	addr_l = mc->rreg;
472 	if (mc->shift == RT700_DIR_OUT_SFT) /* output */
473 		val_h = 0x80;
474 	else /* input */
475 		val_h = 0x0;
476 
477 	rt700_get_gain(rt700, addr_h, addr_l, val_h, &read_rl, &read_ll);
478 
479 	if (mc->invert) {
480 		/* for mute status */
481 		read_ll = !((read_ll & 0x80) >> RT700_MUTE_SFT);
482 		read_rl = !((read_rl & 0x80) >> RT700_MUTE_SFT);
483 	} else {
484 		/* for gain */
485 		read_ll = read_ll & 0x7f;
486 		read_rl = read_rl & 0x7f;
487 	}
488 	ucontrol->value.integer.value[0] = read_ll;
489 	ucontrol->value.integer.value[1] = read_rl;
490 
491 	return 0;
492 }
493 
494 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0);
495 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0);
496 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0);
497 
498 static const struct snd_kcontrol_new rt700_snd_controls[] = {
499 	SOC_DOUBLE_R_EXT_TLV("DAC Front Playback Volume",
500 		RT700_SET_GAIN_DAC1_H, RT700_SET_GAIN_DAC1_L,
501 		RT700_DIR_OUT_SFT, 0x57, 0,
502 		rt700_set_amp_gain_get, rt700_set_amp_gain_put, out_vol_tlv),
503 	SOC_DOUBLE_R_EXT("ADC 08 Capture Switch",
504 		RT700_SET_GAIN_ADC2_H, RT700_SET_GAIN_ADC2_L,
505 		RT700_DIR_IN_SFT, 1, 1,
506 		rt700_set_amp_gain_get, rt700_set_amp_gain_put),
507 	SOC_DOUBLE_R_EXT("ADC 09 Capture Switch",
508 		RT700_SET_GAIN_ADC1_H,	RT700_SET_GAIN_ADC1_L,
509 		RT700_DIR_IN_SFT, 1, 1,
510 		rt700_set_amp_gain_get, rt700_set_amp_gain_put),
511 	SOC_DOUBLE_R_EXT_TLV("ADC 08 Capture Volume",
512 		RT700_SET_GAIN_ADC2_H,	RT700_SET_GAIN_ADC2_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("ADC 09 Capture Volume",
516 		RT700_SET_GAIN_ADC1_H, RT700_SET_GAIN_ADC1_L,
517 		RT700_DIR_IN_SFT, 0x3f, 0,
518 		rt700_set_amp_gain_get, rt700_set_amp_gain_put, in_vol_tlv),
519 	SOC_DOUBLE_R_EXT_TLV("AMIC Volume",
520 		RT700_SET_GAIN_AMIC_H,	RT700_SET_GAIN_AMIC_L,
521 		RT700_DIR_IN_SFT, 3, 0,
522 		rt700_set_amp_gain_get, rt700_set_amp_gain_put, mic_vol_tlv),
523 };
524 
rt700_mux_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)525 static int rt700_mux_get(struct snd_kcontrol *kcontrol,
526 			struct snd_ctl_elem_value *ucontrol)
527 {
528 	struct snd_soc_component *component =
529 		snd_soc_dapm_kcontrol_component(kcontrol);
530 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
531 	unsigned int reg, val = 0, nid;
532 	int ret;
533 
534 	if (strstr(ucontrol->id.name, "HPO Mux"))
535 		nid = RT700_HP_OUT;
536 	else if (strstr(ucontrol->id.name, "ADC 22 Mux"))
537 		nid = RT700_MIXER_IN1;
538 	else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
539 		nid = RT700_MIXER_IN2;
540 	else
541 		return -EINVAL;
542 
543 	/* vid = 0xf01 */
544 	reg = RT700_VERB_SET_CONNECT_SEL | nid;
545 	ret = regmap_read(rt700->regmap, reg, &val);
546 	if (ret < 0)
547 		return ret;
548 
549 	ucontrol->value.enumerated.item[0] = val;
550 
551 	return 0;
552 }
553 
rt700_mux_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)554 static int rt700_mux_put(struct snd_kcontrol *kcontrol,
555 			struct snd_ctl_elem_value *ucontrol)
556 {
557 	struct snd_soc_component *component =
558 		snd_soc_dapm_kcontrol_component(kcontrol);
559 	struct snd_soc_dapm_context *dapm =
560 		snd_soc_dapm_kcontrol_dapm(kcontrol);
561 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
562 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
563 	unsigned int *item = ucontrol->value.enumerated.item;
564 	unsigned int val, val2 = 0, change, reg, nid;
565 	int ret;
566 
567 	if (item[0] >= e->items)
568 		return -EINVAL;
569 
570 	if (strstr(ucontrol->id.name, "HPO Mux"))
571 		nid = RT700_HP_OUT;
572 	else if (strstr(ucontrol->id.name, "ADC 22 Mux"))
573 		nid = RT700_MIXER_IN1;
574 	else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
575 		nid = RT700_MIXER_IN2;
576 	else
577 		return -EINVAL;
578 
579 	/* Verb ID = 0x701h */
580 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
581 
582 	reg = RT700_VERB_SET_CONNECT_SEL | nid;
583 	ret = regmap_read(rt700->regmap, reg, &val2);
584 	if (ret < 0)
585 		return ret;
586 
587 	if (val == val2)
588 		change = 0;
589 	else
590 		change = 1;
591 
592 	if (change) {
593 		reg = RT700_VERB_SET_CONNECT_SEL | nid;
594 		regmap_write(rt700->regmap, reg, val);
595 	}
596 
597 	snd_soc_dapm_mux_update_power(dapm, kcontrol,
598 						item[0], e, NULL);
599 
600 	return change;
601 }
602 
603 static const char * const adc_mux_text[] = {
604 	"MIC2",
605 	"LINE1",
606 	"LINE2",
607 	"DMIC",
608 };
609 
610 static SOC_ENUM_SINGLE_DECL(
611 	rt700_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text);
612 
613 static SOC_ENUM_SINGLE_DECL(
614 	rt700_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text);
615 
616 static const struct snd_kcontrol_new rt700_adc22_mux =
617 	SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt700_adc22_enum,
618 			rt700_mux_get, rt700_mux_put);
619 
620 static const struct snd_kcontrol_new rt700_adc23_mux =
621 	SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt700_adc23_enum,
622 			rt700_mux_get, rt700_mux_put);
623 
624 static const char * const out_mux_text[] = {
625 	"Front",
626 	"Surround",
627 };
628 
629 static SOC_ENUM_SINGLE_DECL(
630 	rt700_hp_enum, SND_SOC_NOPM, 0, out_mux_text);
631 
632 static const struct snd_kcontrol_new rt700_hp_mux =
633 	SOC_DAPM_ENUM_EXT("HP Mux", rt700_hp_enum,
634 			rt700_mux_get, rt700_mux_put);
635 
rt700_dac_front_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)636 static int rt700_dac_front_event(struct snd_soc_dapm_widget *w,
637 	struct snd_kcontrol *kcontrol, int event)
638 {
639 	struct snd_soc_component *component =
640 		snd_soc_dapm_to_component(w->dapm);
641 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
642 
643 	switch (event) {
644 	case SND_SOC_DAPM_POST_PMU:
645 		regmap_write(rt700->regmap,
646 			RT700_SET_STREAMID_DAC1, 0x10);
647 		break;
648 	case SND_SOC_DAPM_PRE_PMD:
649 		regmap_write(rt700->regmap,
650 			RT700_SET_STREAMID_DAC1, 0x00);
651 		break;
652 	}
653 	return 0;
654 }
655 
rt700_dac_surround_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)656 static int rt700_dac_surround_event(struct snd_soc_dapm_widget *w,
657 	struct snd_kcontrol *kcontrol, int event)
658 {
659 	struct snd_soc_component *component =
660 		snd_soc_dapm_to_component(w->dapm);
661 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
662 
663 	switch (event) {
664 	case SND_SOC_DAPM_POST_PMU:
665 		regmap_write(rt700->regmap,
666 			RT700_SET_STREAMID_DAC2, 0x10);
667 		break;
668 	case SND_SOC_DAPM_PRE_PMD:
669 		regmap_write(rt700->regmap,
670 			RT700_SET_STREAMID_DAC2, 0x00);
671 		break;
672 	}
673 	return 0;
674 }
675 
rt700_adc_09_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)676 static int rt700_adc_09_event(struct snd_soc_dapm_widget *w,
677 	struct snd_kcontrol *kcontrol, int event)
678 {
679 	struct snd_soc_component *component =
680 		snd_soc_dapm_to_component(w->dapm);
681 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
682 
683 	switch (event) {
684 	case SND_SOC_DAPM_POST_PMU:
685 		regmap_write(rt700->regmap,
686 			RT700_SET_STREAMID_ADC1, 0x10);
687 		break;
688 	case SND_SOC_DAPM_PRE_PMD:
689 		regmap_write(rt700->regmap,
690 			RT700_SET_STREAMID_ADC1, 0x00);
691 		break;
692 	}
693 	return 0;
694 }
695 
rt700_adc_08_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)696 static int rt700_adc_08_event(struct snd_soc_dapm_widget *w,
697 	struct snd_kcontrol *kcontrol, int event)
698 {
699 	struct snd_soc_component *component =
700 		snd_soc_dapm_to_component(w->dapm);
701 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
702 
703 	switch (event) {
704 	case SND_SOC_DAPM_POST_PMU:
705 		regmap_write(rt700->regmap,
706 			RT700_SET_STREAMID_ADC2, 0x10);
707 		break;
708 	case SND_SOC_DAPM_PRE_PMD:
709 		regmap_write(rt700->regmap,
710 			RT700_SET_STREAMID_ADC2, 0x00);
711 		break;
712 	}
713 	return 0;
714 }
715 
rt700_hpo_mux_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)716 static int rt700_hpo_mux_event(struct snd_soc_dapm_widget *w,
717 	struct snd_kcontrol *kcontrol, int event)
718 {
719 	struct snd_soc_component *component =
720 		snd_soc_dapm_to_component(w->dapm);
721 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
722 	unsigned int val_h = (1 << RT700_DIR_OUT_SFT) | (0x3 << 4);
723 	unsigned int val_l;
724 
725 	switch (event) {
726 	case SND_SOC_DAPM_POST_PMU:
727 		val_l = 0x00;
728 		regmap_write(rt700->regmap,
729 			RT700_SET_GAIN_HP_H, (val_h << 8 | val_l));
730 		break;
731 	case SND_SOC_DAPM_PRE_PMD:
732 		val_l = (1 << RT700_MUTE_SFT);
733 		regmap_write(rt700->regmap,
734 			RT700_SET_GAIN_HP_H, (val_h << 8 | val_l));
735 		usleep_range(50000, 55000);
736 		break;
737 	}
738 	return 0;
739 }
740 
rt700_spk_pga_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)741 static int rt700_spk_pga_event(struct snd_soc_dapm_widget *w,
742 	struct snd_kcontrol *kcontrol, int event)
743 {
744 	struct snd_soc_component *component =
745 		snd_soc_dapm_to_component(w->dapm);
746 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
747 	unsigned int val_h = (1 << RT700_DIR_OUT_SFT) | (0x3 << 4);
748 	unsigned int val_l;
749 
750 	switch (event) {
751 	case SND_SOC_DAPM_POST_PMU:
752 		val_l = 0x00;
753 		regmap_write(rt700->regmap,
754 			RT700_SET_GAIN_SPK_H, (val_h << 8 | val_l));
755 		break;
756 	case SND_SOC_DAPM_PRE_PMD:
757 		val_l = (1 << RT700_MUTE_SFT);
758 		regmap_write(rt700->regmap,
759 			RT700_SET_GAIN_SPK_H, (val_h << 8 | val_l));
760 		break;
761 	}
762 	return 0;
763 }
764 
765 static const struct snd_soc_dapm_widget rt700_dapm_widgets[] = {
766 	SND_SOC_DAPM_OUTPUT("HP"),
767 	SND_SOC_DAPM_OUTPUT("SPK"),
768 	SND_SOC_DAPM_INPUT("DMIC1"),
769 	SND_SOC_DAPM_INPUT("DMIC2"),
770 	SND_SOC_DAPM_INPUT("MIC2"),
771 	SND_SOC_DAPM_INPUT("LINE1"),
772 	SND_SOC_DAPM_INPUT("LINE2"),
773 	SND_SOC_DAPM_DAC_E("DAC Front", NULL, SND_SOC_NOPM, 0, 0,
774 		rt700_dac_front_event,
775 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
776 	SND_SOC_DAPM_DAC_E("DAC Surround", NULL, SND_SOC_NOPM, 0, 0,
777 		rt700_dac_surround_event,
778 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
779 	SND_SOC_DAPM_MUX_E("HPO Mux", SND_SOC_NOPM, 0, 0, &rt700_hp_mux,
780 		rt700_hpo_mux_event,
781 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
782 	SND_SOC_DAPM_PGA_E("SPK PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
783 		rt700_spk_pga_event,
784 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
785 	SND_SOC_DAPM_ADC_E("ADC 09", NULL, SND_SOC_NOPM, 0, 0,
786 		rt700_adc_09_event,
787 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
788 	SND_SOC_DAPM_ADC_E("ADC 08", NULL, SND_SOC_NOPM, 0, 0,
789 		rt700_adc_08_event,
790 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
791 	SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0,
792 		&rt700_adc22_mux),
793 	SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0,
794 		&rt700_adc23_mux),
795 	SND_SOC_DAPM_AIF_IN("DP1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0),
796 	SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0),
797 	SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0),
798 	SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0),
799 };
800 
801 static const struct snd_soc_dapm_route rt700_audio_map[] = {
802 	{"DAC Front", NULL, "DP1RX"},
803 	{"DAC Surround", NULL, "DP3RX"},
804 	{"DP2TX", NULL, "ADC 09"},
805 	{"DP4TX", NULL, "ADC 08"},
806 	{"ADC 09", NULL, "ADC 22 Mux"},
807 	{"ADC 08", NULL, "ADC 23 Mux"},
808 	{"ADC 22 Mux", "DMIC", "DMIC1"},
809 	{"ADC 22 Mux", "LINE1", "LINE1"},
810 	{"ADC 22 Mux", "LINE2", "LINE2"},
811 	{"ADC 22 Mux", "MIC2", "MIC2"},
812 	{"ADC 23 Mux", "DMIC", "DMIC2"},
813 	{"ADC 23 Mux", "LINE1", "LINE1"},
814 	{"ADC 23 Mux", "LINE2", "LINE2"},
815 	{"ADC 23 Mux", "MIC2", "MIC2"},
816 	{"HPO Mux", "Front", "DAC Front"},
817 	{"HPO Mux", "Surround", "DAC Surround"},
818 	{"HP", NULL, "HPO Mux"},
819 	{"SPK PGA", NULL, "DAC Front"},
820 	{"SPK", NULL, "SPK PGA"},
821 };
822 
rt700_probe(struct snd_soc_component * component)823 static int rt700_probe(struct snd_soc_component *component)
824 {
825 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
826 	int ret;
827 
828 	rt700->component = component;
829 
830 	if (!rt700->first_hw_init)
831 		return 0;
832 
833 	ret = pm_runtime_resume(component->dev);
834 	if (ret < 0 && ret != -EACCES)
835 		return ret;
836 
837 	return 0;
838 }
839 
rt700_set_bias_level(struct snd_soc_component * component,enum snd_soc_bias_level level)840 static int rt700_set_bias_level(struct snd_soc_component *component,
841 				enum snd_soc_bias_level level)
842 {
843 	struct snd_soc_dapm_context *dapm =
844 		snd_soc_component_get_dapm(component);
845 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
846 
847 	switch (level) {
848 	case SND_SOC_BIAS_PREPARE:
849 		if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
850 			regmap_write(rt700->regmap,
851 				RT700_SET_AUDIO_POWER_STATE,
852 				AC_PWRST_D0);
853 		}
854 		break;
855 
856 	case SND_SOC_BIAS_STANDBY:
857 		regmap_write(rt700->regmap,
858 			RT700_SET_AUDIO_POWER_STATE,
859 			AC_PWRST_D3);
860 		break;
861 
862 	default:
863 		break;
864 	}
865 	dapm->bias_level = level;
866 	return 0;
867 }
868 
869 static const struct snd_soc_component_driver soc_codec_dev_rt700 = {
870 	.probe = rt700_probe,
871 	.set_bias_level = rt700_set_bias_level,
872 	.controls = rt700_snd_controls,
873 	.num_controls = ARRAY_SIZE(rt700_snd_controls),
874 	.dapm_widgets = rt700_dapm_widgets,
875 	.num_dapm_widgets = ARRAY_SIZE(rt700_dapm_widgets),
876 	.dapm_routes = rt700_audio_map,
877 	.num_dapm_routes = ARRAY_SIZE(rt700_audio_map),
878 	.set_jack = rt700_set_jack_detect,
879 	.endianness = 1,
880 };
881 
rt700_set_sdw_stream(struct snd_soc_dai * dai,void * sdw_stream,int direction)882 static int rt700_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
883 				int direction)
884 {
885 	snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
886 
887 	return 0;
888 }
889 
rt700_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)890 static void rt700_shutdown(struct snd_pcm_substream *substream,
891 				struct snd_soc_dai *dai)
892 {
893 	snd_soc_dai_set_dma_data(dai, substream, NULL);
894 }
895 
rt700_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)896 static int rt700_pcm_hw_params(struct snd_pcm_substream *substream,
897 					struct snd_pcm_hw_params *params,
898 					struct snd_soc_dai *dai)
899 {
900 	struct snd_soc_component *component = dai->component;
901 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
902 	struct sdw_stream_config stream_config = {0};
903 	struct sdw_port_config port_config = {0};
904 	struct sdw_stream_runtime *sdw_stream;
905 	int retval;
906 	unsigned int val = 0;
907 
908 	dev_dbg(dai->dev, "%s %s", __func__, dai->name);
909 	sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
910 
911 	if (!sdw_stream)
912 		return -EINVAL;
913 
914 	if (!rt700->slave)
915 		return -EINVAL;
916 
917 	/* SoundWire specific configuration */
918 	snd_sdw_params_to_config(substream, params, &stream_config, &port_config);
919 
920 	/* This code assumes port 1 for playback and port 2 for capture */
921 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
922 		port_config.num = 1;
923 	else
924 		port_config.num = 2;
925 
926 	switch (dai->id) {
927 	case RT700_AIF1:
928 		break;
929 	case RT700_AIF2:
930 		port_config.num += 2;
931 		break;
932 	default:
933 		dev_err(component->dev, "%s: Invalid DAI id %d\n", __func__, dai->id);
934 		return -EINVAL;
935 	}
936 
937 	retval = sdw_stream_add_slave(rt700->slave, &stream_config,
938 					&port_config, 1, sdw_stream);
939 	if (retval) {
940 		dev_err(dai->dev, "%s: Unable to configure port\n", __func__);
941 		return retval;
942 	}
943 
944 	if (params_channels(params) <= 16) {
945 		/* bit 3:0 Number of Channel */
946 		val |= (params_channels(params) - 1);
947 	} else {
948 		dev_err(component->dev, "%s: Unsupported channels %d\n",
949 			__func__, params_channels(params));
950 		return -EINVAL;
951 	}
952 
953 	switch (params_width(params)) {
954 	/* bit 6:4 Bits per Sample */
955 	case 8:
956 		break;
957 	case 16:
958 		val |= (0x1 << 4);
959 		break;
960 	case 20:
961 		val |= (0x2 << 4);
962 		break;
963 	case 24:
964 		val |= (0x3 << 4);
965 		break;
966 	case 32:
967 		val |= (0x4 << 4);
968 		break;
969 	default:
970 		return -EINVAL;
971 	}
972 
973 	/* 48Khz */
974 	regmap_write(rt700->regmap, RT700_DAC_FORMAT_H, val);
975 	regmap_write(rt700->regmap, RT700_ADC_FORMAT_H, val);
976 
977 	return retval;
978 }
979 
rt700_pcm_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)980 static int rt700_pcm_hw_free(struct snd_pcm_substream *substream,
981 				struct snd_soc_dai *dai)
982 {
983 	struct snd_soc_component *component = dai->component;
984 	struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component);
985 	struct sdw_stream_runtime *sdw_stream =
986 		snd_soc_dai_get_dma_data(dai, substream);
987 
988 	if (!rt700->slave)
989 		return -EINVAL;
990 
991 	sdw_stream_remove_slave(rt700->slave, sdw_stream);
992 	return 0;
993 }
994 
995 #define RT700_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
996 #define RT700_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
997 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
998 
999 static const struct snd_soc_dai_ops rt700_ops = {
1000 	.hw_params	= rt700_pcm_hw_params,
1001 	.hw_free	= rt700_pcm_hw_free,
1002 	.set_stream	= rt700_set_sdw_stream,
1003 	.shutdown	= rt700_shutdown,
1004 };
1005 
1006 static struct snd_soc_dai_driver rt700_dai[] = {
1007 	{
1008 		.name = "rt700-aif1",
1009 		.id = RT700_AIF1,
1010 		.playback = {
1011 			.stream_name = "DP1 Playback",
1012 			.channels_min = 1,
1013 			.channels_max = 2,
1014 			.rates = RT700_STEREO_RATES,
1015 			.formats = RT700_FORMATS,
1016 		},
1017 		.capture = {
1018 			.stream_name = "DP2 Capture",
1019 			.channels_min = 1,
1020 			.channels_max = 2,
1021 			.rates = RT700_STEREO_RATES,
1022 			.formats = RT700_FORMATS,
1023 		},
1024 		.ops = &rt700_ops,
1025 	},
1026 	{
1027 		.name = "rt700-aif2",
1028 		.id = RT700_AIF2,
1029 		.playback = {
1030 			.stream_name = "DP3 Playback",
1031 			.channels_min = 1,
1032 			.channels_max = 2,
1033 			.rates = RT700_STEREO_RATES,
1034 			.formats = RT700_FORMATS,
1035 		},
1036 		.capture = {
1037 			.stream_name = "DP4 Capture",
1038 			.channels_min = 1,
1039 			.channels_max = 2,
1040 			.rates = RT700_STEREO_RATES,
1041 			.formats = RT700_FORMATS,
1042 		},
1043 		.ops = &rt700_ops,
1044 	},
1045 };
1046 
1047 /* Bus clock frequency */
1048 #define RT700_CLK_FREQ_9600000HZ 9600000
1049 #define RT700_CLK_FREQ_12000000HZ 12000000
1050 #define RT700_CLK_FREQ_6000000HZ 6000000
1051 #define RT700_CLK_FREQ_4800000HZ 4800000
1052 #define RT700_CLK_FREQ_2400000HZ 2400000
1053 #define RT700_CLK_FREQ_12288000HZ 12288000
1054 
rt700_clock_config(struct device * dev)1055 int rt700_clock_config(struct device *dev)
1056 {
1057 	struct rt700_priv *rt700 = dev_get_drvdata(dev);
1058 	unsigned int clk_freq, value;
1059 
1060 	clk_freq = (rt700->params.curr_dr_freq >> 1);
1061 
1062 	switch (clk_freq) {
1063 	case RT700_CLK_FREQ_12000000HZ:
1064 		value = 0x0;
1065 		break;
1066 	case RT700_CLK_FREQ_6000000HZ:
1067 		value = 0x1;
1068 		break;
1069 	case RT700_CLK_FREQ_9600000HZ:
1070 		value = 0x2;
1071 		break;
1072 	case RT700_CLK_FREQ_4800000HZ:
1073 		value = 0x3;
1074 		break;
1075 	case RT700_CLK_FREQ_2400000HZ:
1076 		value = 0x4;
1077 		break;
1078 	case RT700_CLK_FREQ_12288000HZ:
1079 		value = 0x5;
1080 		break;
1081 	default:
1082 		return -EINVAL;
1083 	}
1084 
1085 	regmap_write(rt700->regmap, 0xe0, value);
1086 	regmap_write(rt700->regmap, 0xf0, value);
1087 
1088 	dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
1089 
1090 	return 0;
1091 }
1092 
rt700_init(struct device * dev,struct regmap * sdw_regmap,struct regmap * regmap,struct sdw_slave * slave)1093 int rt700_init(struct device *dev, struct regmap *sdw_regmap,
1094 			struct regmap *regmap, struct sdw_slave *slave)
1095 
1096 {
1097 	struct rt700_priv *rt700;
1098 	int ret;
1099 
1100 	rt700 = devm_kzalloc(dev, sizeof(*rt700), GFP_KERNEL);
1101 	if (!rt700)
1102 		return -ENOMEM;
1103 
1104 	dev_set_drvdata(dev, rt700);
1105 	rt700->slave = slave;
1106 	rt700->sdw_regmap = sdw_regmap;
1107 	rt700->regmap = regmap;
1108 
1109 	regcache_cache_only(rt700->regmap, true);
1110 
1111 	mutex_init(&rt700->disable_irq_lock);
1112 
1113 	INIT_DELAYED_WORK(&rt700->jack_detect_work,
1114 			  rt700_jack_detect_handler);
1115 	INIT_DELAYED_WORK(&rt700->jack_btn_check_work,
1116 			  rt700_btn_check_handler);
1117 
1118 	/*
1119 	 * Mark hw_init to false
1120 	 * HW init will be performed when device reports present
1121 	 */
1122 	rt700->hw_init = false;
1123 	rt700->first_hw_init = false;
1124 
1125 	ret =  devm_snd_soc_register_component(dev,
1126 				&soc_codec_dev_rt700,
1127 				rt700_dai,
1128 				ARRAY_SIZE(rt700_dai));
1129 	if (ret < 0)
1130 		return ret;
1131 
1132 	/* set autosuspend parameters */
1133 	pm_runtime_set_autosuspend_delay(dev, 3000);
1134 	pm_runtime_use_autosuspend(dev);
1135 
1136 	/* make sure the device does not suspend immediately */
1137 	pm_runtime_mark_last_busy(dev);
1138 
1139 	pm_runtime_enable(dev);
1140 
1141 	/* important note: the device is NOT tagged as 'active' and will remain
1142 	 * 'suspended' until the hardware is enumerated/initialized. This is required
1143 	 * to make sure the ASoC framework use of pm_runtime_get_sync() does not silently
1144 	 * fail with -EACCESS because of race conditions between card creation and enumeration
1145 	 */
1146 	dev_dbg(&slave->dev, "%s\n", __func__);
1147 
1148 	return 0;
1149 }
1150 
rt700_io_init(struct device * dev,struct sdw_slave * slave)1151 int rt700_io_init(struct device *dev, struct sdw_slave *slave)
1152 {
1153 	struct rt700_priv *rt700 = dev_get_drvdata(dev);
1154 
1155 	rt700->disable_irq = false;
1156 
1157 	if (rt700->hw_init)
1158 		return 0;
1159 
1160 	regcache_cache_only(rt700->regmap, false);
1161 	if (rt700->first_hw_init)
1162 		regcache_cache_bypass(rt700->regmap, true);
1163 
1164 	/*
1165 	 * PM runtime is only enabled when a Slave reports as Attached
1166 	 */
1167 	if (!rt700->first_hw_init)
1168 		/* PM runtime status is marked as 'active' only when a Slave reports as Attached */
1169 		pm_runtime_set_active(&slave->dev);
1170 
1171 	pm_runtime_get_noresume(&slave->dev);
1172 
1173 	/* reset */
1174 	regmap_write(rt700->regmap, 0xff01, 0x0000);
1175 	regmap_write(rt700->regmap, 0x7520, 0x001a);
1176 	regmap_write(rt700->regmap, 0x7420, 0xc003);
1177 
1178 	/* power on */
1179 	regmap_write(rt700->regmap, RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
1180 	/* Set Pin Widget */
1181 	regmap_write(rt700->regmap, RT700_SET_PIN_HP, 0x40);
1182 	regmap_write(rt700->regmap, RT700_SET_PIN_SPK, 0x40);
1183 	regmap_write(rt700->regmap, RT700_SET_EAPD_SPK, RT700_EAPD_HIGH);
1184 	regmap_write(rt700->regmap, RT700_SET_PIN_DMIC1, 0x20);
1185 	regmap_write(rt700->regmap, RT700_SET_PIN_DMIC2, 0x20);
1186 	regmap_write(rt700->regmap, RT700_SET_PIN_MIC2, 0x20);
1187 
1188 	/* Set Configuration Default */
1189 	regmap_write(rt700->regmap, 0x4f12, 0x91);
1190 	regmap_write(rt700->regmap, 0x4e12, 0xd6);
1191 	regmap_write(rt700->regmap, 0x4d12, 0x11);
1192 	regmap_write(rt700->regmap, 0x4c12, 0x20);
1193 	regmap_write(rt700->regmap, 0x4f13, 0x91);
1194 	regmap_write(rt700->regmap, 0x4e13, 0xd6);
1195 	regmap_write(rt700->regmap, 0x4d13, 0x11);
1196 	regmap_write(rt700->regmap, 0x4c13, 0x21);
1197 
1198 	regmap_write(rt700->regmap, 0x4f19, 0x02);
1199 	regmap_write(rt700->regmap, 0x4e19, 0xa1);
1200 	regmap_write(rt700->regmap, 0x4d19, 0x90);
1201 	regmap_write(rt700->regmap, 0x4c19, 0x80);
1202 
1203 	/* Enable Line2 */
1204 	regmap_write(rt700->regmap,  0x371b, 0x40);
1205 	regmap_write(rt700->regmap,  0x731b, 0xb0);
1206 	regmap_write(rt700->regmap,  0x839b, 0x00);
1207 
1208 	/* Set index */
1209 	rt700_index_write(rt700->regmap, 0x4a, 0x201b);
1210 	rt700_index_write(rt700->regmap, 0x45, 0x5089);
1211 	rt700_index_write(rt700->regmap, 0x6b, 0x5064);
1212 	rt700_index_write(rt700->regmap, 0x48, 0xd249);
1213 
1214 	/* Finish Initial Settings, set power to D3 */
1215 	regmap_write(rt700->regmap, RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
1216 
1217 	/*
1218 	 * if set_jack callback occurred early than io_init,
1219 	 * we set up the jack detection function now
1220 	 */
1221 	if (rt700->hs_jack)
1222 		rt700_jack_init(rt700);
1223 
1224 	if (rt700->first_hw_init) {
1225 		regcache_cache_bypass(rt700->regmap, false);
1226 		regcache_mark_dirty(rt700->regmap);
1227 	} else
1228 		rt700->first_hw_init = true;
1229 
1230 	/* Mark Slave initialization complete */
1231 	rt700->hw_init = true;
1232 
1233 	pm_runtime_mark_last_busy(&slave->dev);
1234 	pm_runtime_put_autosuspend(&slave->dev);
1235 
1236 	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
1237 
1238 	return 0;
1239 }
1240 
1241 MODULE_DESCRIPTION("ASoC RT700 driver SDW");
1242 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1243 MODULE_LICENSE("GPL v2");
1244