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