xref: /linux/sound/soc/mediatek/mt8188/mt8188-mt6359.c (revision 9e56ff53b4115875667760445b028357848b4748)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mt8188-mt6359.c  --  MT8188-MT6359 ALSA SoC machine driver
4  *
5  * Copyright (c) 2022 MediaTek Inc.
6  * Author: Trevor Wu <trevor.wu@mediatek.com>
7  */
8 
9 #include <linux/bitfield.h>
10 #include <linux/input.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/pm_runtime.h>
14 #include <sound/jack.h>
15 #include <sound/pcm_params.h>
16 #include <sound/soc.h>
17 #include "mt8188-afe-common.h"
18 #include "../../codecs/nau8825.h"
19 #include "../../codecs/mt6359.h"
20 #include "../../codecs/rt5682.h"
21 #include "../common/mtk-afe-platform-driver.h"
22 #include "../common/mtk-soundcard-driver.h"
23 #include "../common/mtk-dsp-sof-common.h"
24 #include "../common/mtk-soc-card.h"
25 
26 #define CKSYS_AUD_TOP_CFG	0x032c
27  #define RG_TEST_ON		BIT(0)
28  #define RG_TEST_TYPE		BIT(2)
29 #define CKSYS_AUD_TOP_MON	0x0330
30  #define TEST_MISO_COUNT_1	GENMASK(3, 0)
31  #define TEST_MISO_COUNT_2	GENMASK(7, 4)
32  #define TEST_MISO_DONE_1	BIT(28)
33  #define TEST_MISO_DONE_2	BIT(29)
34 
35 #define NAU8825_HS_PRESENT	BIT(0)
36 #define RT5682S_HS_PRESENT	BIT(1)
37 #define ES8326_HS_PRESENT	BIT(2)
38 #define MAX98390_TWO_AMP	BIT(3)
39 /*
40  * Maxim MAX98390
41  */
42 #define MAX98390_CODEC_DAI     "max98390-aif1"
43 #define MAX98390_DEV0_NAME     "max98390.0-0038" /* rear right */
44 #define MAX98390_DEV1_NAME     "max98390.0-0039" /* rear left */
45 #define MAX98390_DEV2_NAME     "max98390.0-003a" /* front right */
46 #define MAX98390_DEV3_NAME     "max98390.0-003b" /* front left */
47 
48 /*
49  * Nau88l25
50  */
51 #define NAU8825_CODEC_DAI  "nau8825-hifi"
52 
53 /*
54  * ES8326
55  */
56 #define ES8326_CODEC_DAI  "ES8326 HiFi"
57 
58 #define SOF_DMA_DL2 "SOF_DMA_DL2"
59 #define SOF_DMA_DL3 "SOF_DMA_DL3"
60 #define SOF_DMA_UL4 "SOF_DMA_UL4"
61 #define SOF_DMA_UL5 "SOF_DMA_UL5"
62 
63 #define RT5682S_CODEC_DAI     "rt5682s-aif1"
64 
65 /* FE */
66 SND_SOC_DAILINK_DEFS(playback2,
67 		     DAILINK_COMP_ARRAY(COMP_CPU("DL2")),
68 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
69 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
70 
71 SND_SOC_DAILINK_DEFS(playback3,
72 		     DAILINK_COMP_ARRAY(COMP_CPU("DL3")),
73 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
74 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
75 
76 SND_SOC_DAILINK_DEFS(playback6,
77 		     DAILINK_COMP_ARRAY(COMP_CPU("DL6")),
78 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
79 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
80 
81 SND_SOC_DAILINK_DEFS(playback7,
82 		     DAILINK_COMP_ARRAY(COMP_CPU("DL7")),
83 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
84 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
85 
86 SND_SOC_DAILINK_DEFS(playback8,
87 		     DAILINK_COMP_ARRAY(COMP_CPU("DL8")),
88 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
89 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
90 
91 SND_SOC_DAILINK_DEFS(playback10,
92 		     DAILINK_COMP_ARRAY(COMP_CPU("DL10")),
93 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
94 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
95 
96 SND_SOC_DAILINK_DEFS(playback11,
97 		     DAILINK_COMP_ARRAY(COMP_CPU("DL11")),
98 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
99 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
100 
101 SND_SOC_DAILINK_DEFS(capture1,
102 		     DAILINK_COMP_ARRAY(COMP_CPU("UL1")),
103 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
104 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
105 
106 SND_SOC_DAILINK_DEFS(capture2,
107 		     DAILINK_COMP_ARRAY(COMP_CPU("UL2")),
108 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
109 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
110 
111 SND_SOC_DAILINK_DEFS(capture3,
112 		     DAILINK_COMP_ARRAY(COMP_CPU("UL3")),
113 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
114 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
115 
116 SND_SOC_DAILINK_DEFS(capture4,
117 		     DAILINK_COMP_ARRAY(COMP_CPU("UL4")),
118 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
119 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
120 
121 SND_SOC_DAILINK_DEFS(capture5,
122 		     DAILINK_COMP_ARRAY(COMP_CPU("UL5")),
123 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
124 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
125 
126 SND_SOC_DAILINK_DEFS(capture6,
127 		     DAILINK_COMP_ARRAY(COMP_CPU("UL6")),
128 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
129 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
130 
131 SND_SOC_DAILINK_DEFS(capture8,
132 		     DAILINK_COMP_ARRAY(COMP_CPU("UL8")),
133 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
134 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
135 
136 SND_SOC_DAILINK_DEFS(capture9,
137 		     DAILINK_COMP_ARRAY(COMP_CPU("UL9")),
138 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
139 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
140 
141 SND_SOC_DAILINK_DEFS(capture10,
142 		     DAILINK_COMP_ARRAY(COMP_CPU("UL10")),
143 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
144 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
145 
146 /* BE */
147 SND_SOC_DAILINK_DEFS(dl_src,
148 		     DAILINK_COMP_ARRAY(COMP_CPU("DL_SRC")),
149 		     DAILINK_COMP_ARRAY(COMP_CODEC("mt6359-sound",
150 						   "mt6359-snd-codec-aif1")),
151 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
152 
153 SND_SOC_DAILINK_DEFS(dptx,
154 		     DAILINK_COMP_ARRAY(COMP_CPU("DPTX")),
155 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
156 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
157 
158 SND_SOC_DAILINK_DEFS(etdm1_in,
159 		     DAILINK_COMP_ARRAY(COMP_CPU("ETDM1_IN")),
160 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
161 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
162 
163 SND_SOC_DAILINK_DEFS(etdm2_in,
164 		     DAILINK_COMP_ARRAY(COMP_CPU("ETDM2_IN")),
165 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
166 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
167 
168 SND_SOC_DAILINK_DEFS(etdm1_out,
169 		     DAILINK_COMP_ARRAY(COMP_CPU("ETDM1_OUT")),
170 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
171 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
172 
173 SND_SOC_DAILINK_DEFS(etdm2_out,
174 		     DAILINK_COMP_ARRAY(COMP_CPU("ETDM2_OUT")),
175 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
176 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
177 
178 SND_SOC_DAILINK_DEFS(etdm3_out,
179 		     DAILINK_COMP_ARRAY(COMP_CPU("ETDM3_OUT")),
180 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
181 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
182 
183 SND_SOC_DAILINK_DEFS(pcm1,
184 		     DAILINK_COMP_ARRAY(COMP_CPU("PCM1")),
185 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
186 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
187 
188 SND_SOC_DAILINK_DEFS(ul_src,
189 		     DAILINK_COMP_ARRAY(COMP_CPU("UL_SRC")),
190 		     DAILINK_COMP_ARRAY(COMP_CODEC("mt6359-sound",
191 						   "mt6359-snd-codec-aif1"),
192 					COMP_CODEC("dmic-codec",
193 						   "dmic-hifi")),
194 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
195 
196 SND_SOC_DAILINK_DEFS(AFE_SOF_DL2,
197 		     DAILINK_COMP_ARRAY(COMP_CPU("SOF_DL2")),
198 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
199 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
200 
201 SND_SOC_DAILINK_DEFS(AFE_SOF_DL3,
202 		     DAILINK_COMP_ARRAY(COMP_CPU("SOF_DL3")),
203 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
204 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
205 
206 SND_SOC_DAILINK_DEFS(AFE_SOF_UL4,
207 		     DAILINK_COMP_ARRAY(COMP_CPU("SOF_UL4")),
208 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
209 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
210 
211 SND_SOC_DAILINK_DEFS(AFE_SOF_UL5,
212 		     DAILINK_COMP_ARRAY(COMP_CPU("SOF_UL5")),
213 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
214 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
215 
216 static const struct sof_conn_stream g_sof_conn_streams[] = {
217 	{
218 		.sof_link = "AFE_SOF_DL2",
219 		.sof_dma = SOF_DMA_DL2,
220 		.stream_dir = SNDRV_PCM_STREAM_PLAYBACK
221 	},
222 	{
223 		.sof_link = "AFE_SOF_DL3",
224 		.sof_dma = SOF_DMA_DL3,
225 		.stream_dir = SNDRV_PCM_STREAM_PLAYBACK
226 	},
227 	{
228 		.sof_link = "AFE_SOF_UL4",
229 		.sof_dma = SOF_DMA_UL4,
230 		.stream_dir = SNDRV_PCM_STREAM_CAPTURE
231 	},
232 	{
233 		.sof_link = "AFE_SOF_UL5",
234 		.sof_dma = SOF_DMA_UL5,
235 		.stream_dir = SNDRV_PCM_STREAM_CAPTURE
236 	},
237 };
238 
239 struct mt8188_mt6359_priv {
240 	struct snd_soc_jack dp_jack;
241 	struct snd_soc_jack hdmi_jack;
242 	struct snd_soc_jack headset_jack;
243 	void *private_data;
244 };
245 
246 static struct snd_soc_jack_pin mt8188_hdmi_jack_pins[] = {
247 	{
248 		.pin = "HDMI",
249 		.mask = SND_JACK_LINEOUT,
250 	},
251 };
252 
253 static struct snd_soc_jack_pin mt8188_dp_jack_pins[] = {
254 	{
255 		.pin = "DP",
256 		.mask = SND_JACK_LINEOUT,
257 	},
258 };
259 
260 static struct snd_soc_jack_pin nau8825_jack_pins[] = {
261 	{
262 		.pin    = "Headphone Jack",
263 		.mask   = SND_JACK_HEADPHONE,
264 	},
265 	{
266 		.pin    = "Headset Mic",
267 		.mask   = SND_JACK_MICROPHONE,
268 	},
269 };
270 
271 struct mt8188_card_data {
272 	const char *name;
273 	unsigned long quirk;
274 };
275 
276 static const struct snd_kcontrol_new mt8188_dumb_spk_controls[] = {
277 	SOC_DAPM_PIN_SWITCH("Ext Spk"),
278 };
279 
280 static const struct snd_soc_dapm_widget mt8188_dumb_spk_widgets[] = {
281 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
282 };
283 
284 static const struct snd_kcontrol_new mt8188_dual_spk_controls[] = {
285 	SOC_DAPM_PIN_SWITCH("Left Spk"),
286 	SOC_DAPM_PIN_SWITCH("Right Spk"),
287 };
288 
289 static const struct snd_soc_dapm_widget mt8188_dual_spk_widgets[] = {
290 	SND_SOC_DAPM_SPK("Left Spk", NULL),
291 	SND_SOC_DAPM_SPK("Right Spk", NULL),
292 };
293 
294 static const struct snd_kcontrol_new mt8188_rear_spk_controls[] = {
295 	SOC_DAPM_PIN_SWITCH("Rear Left Spk"),
296 	SOC_DAPM_PIN_SWITCH("Rear Right Spk"),
297 };
298 
299 static const struct snd_soc_dapm_widget mt8188_rear_spk_widgets[] = {
300 	SND_SOC_DAPM_SPK("Rear Left Spk", NULL),
301 	SND_SOC_DAPM_SPK("Rear Right Spk", NULL),
302 };
303 
304 static const struct snd_soc_dapm_widget mt8188_mt6359_widgets[] = {
305 	SND_SOC_DAPM_HP("Headphone", NULL),
306 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
307 	SND_SOC_DAPM_SINK("HDMI"),
308 	SND_SOC_DAPM_SINK("DP"),
309 	SND_SOC_DAPM_MIXER(SOF_DMA_DL2, SND_SOC_NOPM, 0, 0, NULL, 0),
310 	SND_SOC_DAPM_MIXER(SOF_DMA_DL3, SND_SOC_NOPM, 0, 0, NULL, 0),
311 	SND_SOC_DAPM_MIXER(SOF_DMA_UL4, SND_SOC_NOPM, 0, 0, NULL, 0),
312 	SND_SOC_DAPM_MIXER(SOF_DMA_UL5, SND_SOC_NOPM, 0, 0, NULL, 0),
313 
314 	/* dynamic pinctrl */
315 	SND_SOC_DAPM_PINCTRL("ETDM_SPK_PIN", "aud_etdm_spk_on", "aud_etdm_spk_off"),
316 	SND_SOC_DAPM_PINCTRL("ETDM_HP_PIN", "aud_etdm_hp_on", "aud_etdm_hp_off"),
317 	SND_SOC_DAPM_PINCTRL("MTKAIF_PIN", "aud_mtkaif_on", "aud_mtkaif_off"),
318 };
319 
320 static const struct snd_kcontrol_new mt8188_mt6359_controls[] = {
321 	SOC_DAPM_PIN_SWITCH("Headphone"),
322 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
323 };
324 
325 static const struct snd_soc_dapm_widget mt8188_nau8825_widgets[] = {
326 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
327 };
328 
329 static const struct snd_kcontrol_new mt8188_nau8825_controls[] = {
330 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
331 };
332 
333 static const struct snd_soc_dapm_route mt8188_mt6359_routes[] = {
334 	/* SOF Uplink */
335 	{SOF_DMA_UL4, NULL, "O034"},
336 	{SOF_DMA_UL4, NULL, "O035"},
337 	{SOF_DMA_UL5, NULL, "O036"},
338 	{SOF_DMA_UL5, NULL, "O037"},
339 	/* SOF Downlink */
340 	{"I070", NULL, SOF_DMA_DL2},
341 	{"I071", NULL, SOF_DMA_DL2},
342 	{"I020", NULL, SOF_DMA_DL3},
343 	{"I021", NULL, SOF_DMA_DL3},
344 };
345 
346 static int mt8188_mt6359_mtkaif_calibration(struct snd_soc_pcm_runtime *rtd)
347 {
348 	struct snd_soc_component *cmpnt_afe =
349 		snd_soc_rtdcom_lookup(rtd, AFE_PCM_NAME);
350 	struct snd_soc_component *cmpnt_codec =
351 		snd_soc_rtd_to_codec(rtd, 0)->component;
352 	struct snd_soc_dapm_widget *pin_w = NULL, *w;
353 	struct mtk_base_afe *afe;
354 	struct mt8188_afe_private *afe_priv;
355 	struct mtkaif_param *param;
356 	int chosen_phase_1, chosen_phase_2;
357 	int prev_cycle_1, prev_cycle_2;
358 	u8 test_done_1, test_done_2;
359 	int cycle_1, cycle_2;
360 	int mtkaif_chosen_phase[MT8188_MTKAIF_MISO_NUM];
361 	int mtkaif_phase_cycle[MT8188_MTKAIF_MISO_NUM];
362 	int mtkaif_calibration_num_phase;
363 	bool mtkaif_calibration_ok;
364 	u32 monitor = 0;
365 	int counter;
366 	int phase;
367 	int i;
368 
369 	if (!cmpnt_afe)
370 		return -EINVAL;
371 
372 	afe = snd_soc_component_get_drvdata(cmpnt_afe);
373 	afe_priv = afe->platform_priv;
374 	param = &afe_priv->mtkaif_params;
375 
376 	dev_dbg(afe->dev, "%s(), start\n", __func__);
377 
378 	param->mtkaif_calibration_ok = false;
379 	for (i = 0; i < MT8188_MTKAIF_MISO_NUM; i++) {
380 		param->mtkaif_chosen_phase[i] = -1;
381 		param->mtkaif_phase_cycle[i] = 0;
382 		mtkaif_chosen_phase[i] = -1;
383 		mtkaif_phase_cycle[i] = 0;
384 	}
385 
386 	if (IS_ERR(afe_priv->topckgen)) {
387 		dev_info(afe->dev, "%s() Cannot find topckgen controller\n",
388 			 __func__);
389 		return 0;
390 	}
391 
392 	for_each_card_widgets(rtd->card, w) {
393 		if (!strcmp(w->name, "MTKAIF_PIN")) {
394 			pin_w = w;
395 			break;
396 		}
397 	}
398 
399 	if (pin_w)
400 		dapm_pinctrl_event(pin_w, NULL, SND_SOC_DAPM_PRE_PMU);
401 	else
402 		dev_dbg(afe->dev, "%s(), no pinmux widget, please check if default on\n", __func__);
403 
404 	pm_runtime_get_sync(afe->dev);
405 	mt6359_mtkaif_calibration_enable(cmpnt_codec);
406 
407 	/* set test type to synchronizer pulse */
408 	regmap_write(afe_priv->topckgen, CKSYS_AUD_TOP_CFG, RG_TEST_TYPE);
409 	mtkaif_calibration_num_phase = 42;	/* mt6359: 0 ~ 42 */
410 	mtkaif_calibration_ok = true;
411 
412 	for (phase = 0;
413 	     phase <= mtkaif_calibration_num_phase && mtkaif_calibration_ok;
414 	     phase++) {
415 		mt6359_set_mtkaif_calibration_phase(cmpnt_codec,
416 						    phase, phase, phase);
417 
418 		regmap_set_bits(afe_priv->topckgen, CKSYS_AUD_TOP_CFG, RG_TEST_ON);
419 
420 		test_done_1 = 0;
421 		test_done_2 = 0;
422 
423 		cycle_1 = -1;
424 		cycle_2 = -1;
425 
426 		counter = 0;
427 		while (!(test_done_1 & test_done_2)) {
428 			regmap_read(afe_priv->topckgen,
429 				    CKSYS_AUD_TOP_MON, &monitor);
430 			test_done_1 = FIELD_GET(TEST_MISO_DONE_1, monitor);
431 			test_done_2 = FIELD_GET(TEST_MISO_DONE_2, monitor);
432 
433 			if (test_done_1 == 1)
434 				cycle_1 = FIELD_GET(TEST_MISO_COUNT_1, monitor);
435 
436 			if (test_done_2 == 1)
437 				cycle_2 = FIELD_GET(TEST_MISO_COUNT_2, monitor);
438 
439 			/* handle if never test done */
440 			if (++counter > 10000) {
441 				dev_err(afe->dev, "%s(), test fail, cycle_1 %d, cycle_2 %d, monitor 0x%x\n",
442 					__func__, cycle_1, cycle_2, monitor);
443 				mtkaif_calibration_ok = false;
444 				break;
445 			}
446 		}
447 
448 		if (phase == 0) {
449 			prev_cycle_1 = cycle_1;
450 			prev_cycle_2 = cycle_2;
451 		}
452 
453 		if (cycle_1 != prev_cycle_1 &&
454 		    mtkaif_chosen_phase[MT8188_MTKAIF_MISO_0] < 0) {
455 			mtkaif_chosen_phase[MT8188_MTKAIF_MISO_0] = phase - 1;
456 			mtkaif_phase_cycle[MT8188_MTKAIF_MISO_0] = prev_cycle_1;
457 		}
458 
459 		if (cycle_2 != prev_cycle_2 &&
460 		    mtkaif_chosen_phase[MT8188_MTKAIF_MISO_1] < 0) {
461 			mtkaif_chosen_phase[MT8188_MTKAIF_MISO_1] = phase - 1;
462 			mtkaif_phase_cycle[MT8188_MTKAIF_MISO_1] = prev_cycle_2;
463 		}
464 
465 		regmap_clear_bits(afe_priv->topckgen, CKSYS_AUD_TOP_CFG, RG_TEST_ON);
466 
467 		if (mtkaif_chosen_phase[MT8188_MTKAIF_MISO_0] >= 0 &&
468 		    mtkaif_chosen_phase[MT8188_MTKAIF_MISO_1] >= 0)
469 			break;
470 	}
471 
472 	if (mtkaif_chosen_phase[MT8188_MTKAIF_MISO_0] < 0) {
473 		mtkaif_calibration_ok = false;
474 		chosen_phase_1 = 0;
475 	} else {
476 		chosen_phase_1 = mtkaif_chosen_phase[MT8188_MTKAIF_MISO_0];
477 	}
478 
479 	if (mtkaif_chosen_phase[MT8188_MTKAIF_MISO_1] < 0) {
480 		mtkaif_calibration_ok = false;
481 		chosen_phase_2 = 0;
482 	} else {
483 		chosen_phase_2 = mtkaif_chosen_phase[MT8188_MTKAIF_MISO_1];
484 	}
485 
486 	mt6359_set_mtkaif_calibration_phase(cmpnt_codec,
487 					    chosen_phase_1,
488 					    chosen_phase_2,
489 					    0);
490 
491 	mt6359_mtkaif_calibration_disable(cmpnt_codec);
492 	pm_runtime_put(afe->dev);
493 
494 	param->mtkaif_calibration_ok = mtkaif_calibration_ok;
495 	param->mtkaif_chosen_phase[MT8188_MTKAIF_MISO_0] = chosen_phase_1;
496 	param->mtkaif_chosen_phase[MT8188_MTKAIF_MISO_1] = chosen_phase_2;
497 
498 	for (i = 0; i < MT8188_MTKAIF_MISO_NUM; i++)
499 		param->mtkaif_phase_cycle[i] = mtkaif_phase_cycle[i];
500 
501 	if (pin_w)
502 		dapm_pinctrl_event(pin_w, NULL, SND_SOC_DAPM_POST_PMD);
503 
504 	dev_dbg(afe->dev, "%s(), end, calibration ok %d\n",
505 		__func__, param->mtkaif_calibration_ok);
506 
507 	return 0;
508 }
509 
510 static int mt8188_mt6359_init(struct snd_soc_pcm_runtime *rtd)
511 {
512 	struct snd_soc_component *cmpnt_codec =
513 		snd_soc_rtd_to_codec(rtd, 0)->component;
514 
515 	/* set mtkaif protocol */
516 	mt6359_set_mtkaif_protocol(cmpnt_codec,
517 				   MT6359_MTKAIF_PROTOCOL_2_CLK_P2);
518 
519 	/* mtkaif calibration */
520 	mt8188_mt6359_mtkaif_calibration(rtd);
521 
522 	return 0;
523 }
524 
525 enum {
526 	DAI_LINK_DL2_FE,
527 	DAI_LINK_DL3_FE,
528 	DAI_LINK_DL6_FE,
529 	DAI_LINK_DL7_FE,
530 	DAI_LINK_DL8_FE,
531 	DAI_LINK_DL10_FE,
532 	DAI_LINK_DL11_FE,
533 	DAI_LINK_UL1_FE,
534 	DAI_LINK_UL2_FE,
535 	DAI_LINK_UL3_FE,
536 	DAI_LINK_UL4_FE,
537 	DAI_LINK_UL5_FE,
538 	DAI_LINK_UL6_FE,
539 	DAI_LINK_UL8_FE,
540 	DAI_LINK_UL9_FE,
541 	DAI_LINK_UL10_FE,
542 	DAI_LINK_DL_SRC_BE,
543 	DAI_LINK_DPTX_BE,
544 	DAI_LINK_ETDM1_IN_BE,
545 	DAI_LINK_ETDM2_IN_BE,
546 	DAI_LINK_ETDM1_OUT_BE,
547 	DAI_LINK_ETDM2_OUT_BE,
548 	DAI_LINK_ETDM3_OUT_BE,
549 	DAI_LINK_PCM1_BE,
550 	DAI_LINK_UL_SRC_BE,
551 	DAI_LINK_REGULAR_LAST = DAI_LINK_UL_SRC_BE,
552 	DAI_LINK_SOF_START,
553 	DAI_LINK_SOF_DL2_BE = DAI_LINK_SOF_START,
554 	DAI_LINK_SOF_DL3_BE,
555 	DAI_LINK_SOF_UL4_BE,
556 	DAI_LINK_SOF_UL5_BE,
557 	DAI_LINK_SOF_END = DAI_LINK_SOF_UL5_BE,
558 };
559 
560 #define	DAI_LINK_REGULAR_NUM	(DAI_LINK_REGULAR_LAST + 1)
561 
562 static int mt8188_dptx_hw_params(struct snd_pcm_substream *substream,
563 				 struct snd_pcm_hw_params *params)
564 {
565 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
566 	unsigned int rate = params_rate(params);
567 	unsigned int mclk_fs_ratio = 256;
568 	unsigned int mclk_fs = rate * mclk_fs_ratio;
569 	struct snd_soc_dai *dai = snd_soc_rtd_to_cpu(rtd, 0);
570 
571 	return snd_soc_dai_set_sysclk(dai, 0, mclk_fs, SND_SOC_CLOCK_OUT);
572 }
573 
574 static const struct snd_soc_ops mt8188_dptx_ops = {
575 	.hw_params = mt8188_dptx_hw_params,
576 };
577 
578 static int mt8188_dptx_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
579 				       struct snd_pcm_hw_params *params)
580 {
581 	/* fix BE i2s format to 32bit, clean param mask first */
582 	snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
583 			     0, (__force unsigned int)SNDRV_PCM_FORMAT_LAST);
584 
585 	params_set_format(params, SNDRV_PCM_FORMAT_S32_LE);
586 
587 	return 0;
588 }
589 
590 static int mt8188_hdmi_codec_init(struct snd_soc_pcm_runtime *rtd)
591 {
592 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(rtd->card);
593 	struct mt8188_mt6359_priv *priv = soc_card_data->mach_priv;
594 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
595 	int ret = 0;
596 
597 	ret = snd_soc_card_jack_new_pins(rtd->card, "HDMI Jack",
598 					 SND_JACK_LINEOUT, &priv->hdmi_jack,
599 					 mt8188_hdmi_jack_pins,
600 					 ARRAY_SIZE(mt8188_hdmi_jack_pins));
601 	if (ret) {
602 		dev_err(rtd->dev, "%s, new jack failed: %d\n", __func__, ret);
603 		return ret;
604 	}
605 
606 	ret = snd_soc_component_set_jack(component, &priv->hdmi_jack, NULL);
607 	if (ret) {
608 		dev_err(rtd->dev, "%s, set jack failed on %s (ret=%d)\n",
609 			__func__, component->name, ret);
610 		return ret;
611 	}
612 
613 	return 0;
614 }
615 
616 static int mt8188_dptx_codec_init(struct snd_soc_pcm_runtime *rtd)
617 {
618 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(rtd->card);
619 	struct mt8188_mt6359_priv *priv = soc_card_data->mach_priv;
620 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
621 	int ret = 0;
622 
623 	ret = snd_soc_card_jack_new_pins(rtd->card, "DP Jack", SND_JACK_LINEOUT,
624 					 &priv->dp_jack, mt8188_dp_jack_pins,
625 					 ARRAY_SIZE(mt8188_dp_jack_pins));
626 	if (ret) {
627 		dev_err(rtd->dev, "%s, new jack failed: %d\n", __func__, ret);
628 		return ret;
629 	}
630 
631 	ret = snd_soc_component_set_jack(component, &priv->dp_jack, NULL);
632 	if (ret) {
633 		dev_err(rtd->dev, "%s, set jack failed on %s (ret=%d)\n",
634 			__func__, component->name, ret);
635 		return ret;
636 	}
637 
638 	return 0;
639 }
640 
641 static int mt8188_dumb_amp_init(struct snd_soc_pcm_runtime *rtd)
642 {
643 	struct snd_soc_card *card = rtd->card;
644 	int ret = 0;
645 
646 	ret = snd_soc_dapm_new_controls(&card->dapm, mt8188_dumb_spk_widgets,
647 					ARRAY_SIZE(mt8188_dumb_spk_widgets));
648 	if (ret) {
649 		dev_err(rtd->dev, "unable to add Dumb Speaker dapm, ret %d\n", ret);
650 		return ret;
651 	}
652 
653 	ret = snd_soc_add_card_controls(card, mt8188_dumb_spk_controls,
654 					ARRAY_SIZE(mt8188_dumb_spk_controls));
655 	if (ret) {
656 		dev_err(rtd->dev, "unable to add Dumb card controls, ret %d\n", ret);
657 		return ret;
658 	}
659 
660 	return 0;
661 }
662 
663 static int mt8188_max98390_hw_params(struct snd_pcm_substream *substream,
664 				     struct snd_pcm_hw_params *params)
665 {
666 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
667 	unsigned int bit_width = params_width(params);
668 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
669 	struct snd_soc_dai *codec_dai;
670 	int i;
671 
672 	snd_soc_dai_set_tdm_slot(cpu_dai, 0xf, 0xf, 4, bit_width);
673 
674 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
675 		if (!strcmp(codec_dai->component->name, MAX98390_DEV0_NAME))
676 			snd_soc_dai_set_tdm_slot(codec_dai, 0x8, 0x3, 4, bit_width);
677 
678 		if (!strcmp(codec_dai->component->name, MAX98390_DEV1_NAME))
679 			snd_soc_dai_set_tdm_slot(codec_dai, 0x4, 0x3, 4, bit_width);
680 
681 		if (!strcmp(codec_dai->component->name, MAX98390_DEV2_NAME))
682 			snd_soc_dai_set_tdm_slot(codec_dai, 0x2, 0x3, 4, bit_width);
683 
684 		if (!strcmp(codec_dai->component->name, MAX98390_DEV3_NAME))
685 			snd_soc_dai_set_tdm_slot(codec_dai, 0x1, 0x3, 4, bit_width);
686 	}
687 	return 0;
688 }
689 
690 static const struct snd_soc_ops mt8188_max98390_ops = {
691 	.hw_params = mt8188_max98390_hw_params,
692 };
693 
694 static int mt8188_max98390_codec_init(struct snd_soc_pcm_runtime *rtd)
695 {
696 	struct snd_soc_card *card = rtd->card;
697 	int ret;
698 
699 	/* add regular speakers dapm route */
700 	ret = snd_soc_dapm_new_controls(&card->dapm, mt8188_dual_spk_widgets,
701 					ARRAY_SIZE(mt8188_dual_spk_widgets));
702 	if (ret) {
703 		dev_err(rtd->dev, "unable to add Left/Right Speaker widget, ret %d\n", ret);
704 		return ret;
705 	}
706 
707 	ret = snd_soc_add_card_controls(card, mt8188_dual_spk_controls,
708 					ARRAY_SIZE(mt8188_dual_spk_controls));
709 	if (ret) {
710 		dev_err(rtd->dev, "unable to add Left/Right card controls, ret %d\n", ret);
711 		return ret;
712 	}
713 
714 	if (rtd->dai_link->num_codecs <= 2)
715 		return 0;
716 
717 	/* add widgets/controls/dapm for rear speakers */
718 	ret = snd_soc_dapm_new_controls(&card->dapm, mt8188_rear_spk_widgets,
719 					ARRAY_SIZE(mt8188_rear_spk_widgets));
720 	if (ret) {
721 		dev_err(rtd->dev, "unable to add Rear Speaker widget, ret %d\n", ret);
722 		/* Don't need to add routes if widget addition failed */
723 		return ret;
724 	}
725 
726 	ret = snd_soc_add_card_controls(card, mt8188_rear_spk_controls,
727 					ARRAY_SIZE(mt8188_rear_spk_controls));
728 	if (ret) {
729 		dev_err(rtd->dev, "unable to add Rear card controls, ret %d\n", ret);
730 		return ret;
731 	}
732 
733 	return 0;
734 }
735 
736 static int mt8188_headset_codec_init(struct snd_soc_pcm_runtime *rtd)
737 {
738 	struct snd_soc_card *card = rtd->card;
739 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(card);
740 	struct mt8188_mt6359_priv *priv = soc_card_data->mach_priv;
741 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
742 	struct snd_soc_jack *jack = &priv->headset_jack;
743 	int ret;
744 
745 	ret = snd_soc_dapm_new_controls(&card->dapm, mt8188_nau8825_widgets,
746 					ARRAY_SIZE(mt8188_nau8825_widgets));
747 	if (ret) {
748 		dev_err(rtd->dev, "unable to add nau8825 card widget, ret %d\n", ret);
749 		return ret;
750 	}
751 
752 	ret = snd_soc_add_card_controls(card, mt8188_nau8825_controls,
753 					ARRAY_SIZE(mt8188_nau8825_controls));
754 	if (ret) {
755 		dev_err(rtd->dev, "unable to add nau8825 card controls, ret %d\n", ret);
756 		return ret;
757 	}
758 
759 	ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
760 					 SND_JACK_HEADSET | SND_JACK_BTN_0 |
761 					 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
762 					 SND_JACK_BTN_3,
763 					 jack,
764 					 nau8825_jack_pins,
765 					 ARRAY_SIZE(nau8825_jack_pins));
766 	if (ret) {
767 		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
768 		return ret;
769 	}
770 
771 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
772 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
773 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
774 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
775 	ret = snd_soc_component_set_jack(component, jack, NULL);
776 
777 	if (ret) {
778 		dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
779 		return ret;
780 	}
781 
782 	return 0;
783 };
784 
785 static void mt8188_headset_codec_exit(struct snd_soc_pcm_runtime *rtd)
786 {
787 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
788 
789 	snd_soc_component_set_jack(component, NULL, NULL);
790 }
791 
792 
793 static int mt8188_nau8825_hw_params(struct snd_pcm_substream *substream,
794 				    struct snd_pcm_hw_params *params)
795 {
796 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
797 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
798 	unsigned int rate = params_rate(params);
799 	unsigned int bit_width = params_width(params);
800 	int clk_freq, ret;
801 
802 	clk_freq = rate * 2 * bit_width;
803 
804 	/* Configure clock for codec */
805 	ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_BLK, 0,
806 				     SND_SOC_CLOCK_IN);
807 	if (ret < 0) {
808 		dev_err(codec_dai->dev, "can't set BCLK clock %d\n", ret);
809 		return ret;
810 	}
811 
812 	/* Configure pll for codec */
813 	ret = snd_soc_dai_set_pll(codec_dai, 0, 0, clk_freq,
814 				  params_rate(params) * 256);
815 	if (ret < 0) {
816 		dev_err(codec_dai->dev, "can't set BCLK: %d\n", ret);
817 		return ret;
818 	}
819 
820 	return 0;
821 }
822 
823 static const struct snd_soc_ops mt8188_nau8825_ops = {
824 	.hw_params = mt8188_nau8825_hw_params,
825 };
826 
827 static int mt8188_rt5682s_i2s_hw_params(struct snd_pcm_substream *substream,
828 					struct snd_pcm_hw_params *params)
829 {
830 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
831 	struct snd_soc_card *card = rtd->card;
832 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
833 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
834 	unsigned int rate = params_rate(params);
835 	int bitwidth;
836 	int ret;
837 
838 	bitwidth = snd_pcm_format_width(params_format(params));
839 	if (bitwidth < 0) {
840 		dev_err(card->dev, "invalid bit width: %d\n", bitwidth);
841 		return bitwidth;
842 	}
843 
844 	ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x00, 0x0, 0x2, bitwidth);
845 	if (ret) {
846 		dev_err(card->dev, "failed to set tdm slot\n");
847 		return ret;
848 	}
849 
850 	ret = snd_soc_dai_set_pll(codec_dai, RT5682_PLL1, RT5682_PLL1_S_BCLK1,
851 				  rate * 32, rate * 512);
852 	if (ret) {
853 		dev_err(card->dev, "failed to set pll\n");
854 		return ret;
855 	}
856 
857 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1,
858 				     rate * 512, SND_SOC_CLOCK_IN);
859 	if (ret) {
860 		dev_err(card->dev, "failed to set sysclk\n");
861 		return ret;
862 	}
863 
864 	return snd_soc_dai_set_sysclk(cpu_dai, 0, rate * 128,
865 				      SND_SOC_CLOCK_OUT);
866 }
867 
868 static const struct snd_soc_ops mt8188_rt5682s_i2s_ops = {
869 	.hw_params = mt8188_rt5682s_i2s_hw_params,
870 };
871 
872 static int mt8188_sof_be_hw_params(struct snd_pcm_substream *substream,
873 				   struct snd_pcm_hw_params *params)
874 {
875 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
876 	struct snd_soc_component *cmpnt_afe = NULL;
877 	struct snd_soc_pcm_runtime *runtime;
878 
879 	/* find afe component */
880 	for_each_card_rtds(rtd->card, runtime) {
881 		cmpnt_afe = snd_soc_rtdcom_lookup(runtime, AFE_PCM_NAME);
882 		if (cmpnt_afe)
883 			break;
884 	}
885 
886 	if (cmpnt_afe && !pm_runtime_active(cmpnt_afe->dev)) {
887 		dev_err(rtd->dev, "afe pm runtime is not active!!\n");
888 		return -EINVAL;
889 	}
890 
891 	return 0;
892 }
893 
894 static const struct snd_soc_ops mt8188_sof_be_ops = {
895 	.hw_params = mt8188_sof_be_hw_params,
896 };
897 
898 static int mt8188_es8326_hw_params(struct snd_pcm_substream *substream,
899 				 struct snd_pcm_hw_params *params)
900 {
901 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
902 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
903 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
904 	unsigned int rate = params_rate(params);
905 	int ret;
906 
907 	/* Configure MCLK for codec */
908 	ret = snd_soc_dai_set_sysclk(codec_dai, 0, rate * 256, SND_SOC_CLOCK_IN);
909 	if (ret < 0) {
910 		dev_err(codec_dai->dev, "can't set MCLK %d\n", ret);
911 		return ret;
912 	}
913 
914 	/* Configure MCLK for cpu */
915 	return snd_soc_dai_set_sysclk(cpu_dai, 0, rate * 256, SND_SOC_CLOCK_OUT);
916 }
917 
918 static const struct snd_soc_ops mt8188_es8326_ops = {
919 	.hw_params = mt8188_es8326_hw_params,
920 };
921 
922 static struct snd_soc_dai_link mt8188_mt6359_dai_links[] = {
923 	/* FE */
924 	[DAI_LINK_DL2_FE] = {
925 		.name = "DL2_FE",
926 		.stream_name = "DL2 Playback",
927 		.trigger = {
928 			SND_SOC_DPCM_TRIGGER_POST,
929 			SND_SOC_DPCM_TRIGGER_POST,
930 		},
931 		.dynamic = 1,
932 		.dpcm_playback = 1,
933 		.dpcm_merged_chan = 1,
934 		.dpcm_merged_rate = 1,
935 		.dpcm_merged_format = 1,
936 		SND_SOC_DAILINK_REG(playback2),
937 	},
938 	[DAI_LINK_DL3_FE] = {
939 		.name = "DL3_FE",
940 		.stream_name = "DL3 Playback",
941 		.trigger = {
942 			SND_SOC_DPCM_TRIGGER_POST,
943 			SND_SOC_DPCM_TRIGGER_POST,
944 		},
945 		.dynamic = 1,
946 		.dpcm_playback = 1,
947 		.dpcm_merged_chan = 1,
948 		.dpcm_merged_rate = 1,
949 		.dpcm_merged_format = 1,
950 		SND_SOC_DAILINK_REG(playback3),
951 	},
952 	[DAI_LINK_DL6_FE] = {
953 		.name = "DL6_FE",
954 		.stream_name = "DL6 Playback",
955 		.trigger = {
956 			SND_SOC_DPCM_TRIGGER_POST,
957 			SND_SOC_DPCM_TRIGGER_POST,
958 		},
959 		.dynamic = 1,
960 		.dpcm_playback = 1,
961 		.dpcm_merged_chan = 1,
962 		.dpcm_merged_rate = 1,
963 		.dpcm_merged_format = 1,
964 		SND_SOC_DAILINK_REG(playback6),
965 	},
966 	[DAI_LINK_DL7_FE] = {
967 		.name = "DL7_FE",
968 		.stream_name = "DL7 Playback",
969 		.trigger = {
970 			SND_SOC_DPCM_TRIGGER_PRE,
971 			SND_SOC_DPCM_TRIGGER_PRE,
972 		},
973 		.dynamic = 1,
974 		.dpcm_playback = 1,
975 		SND_SOC_DAILINK_REG(playback7),
976 	},
977 	[DAI_LINK_DL8_FE] = {
978 		.name = "DL8_FE",
979 		.stream_name = "DL8 Playback",
980 		.trigger = {
981 			SND_SOC_DPCM_TRIGGER_POST,
982 			SND_SOC_DPCM_TRIGGER_POST,
983 		},
984 		.dynamic = 1,
985 		.dpcm_playback = 1,
986 		SND_SOC_DAILINK_REG(playback8),
987 	},
988 	[DAI_LINK_DL10_FE] = {
989 		.name = "DL10_FE",
990 		.stream_name = "DL10 Playback",
991 		.trigger = {
992 			SND_SOC_DPCM_TRIGGER_POST,
993 			SND_SOC_DPCM_TRIGGER_POST,
994 		},
995 		.dynamic = 1,
996 		.dpcm_playback = 1,
997 		SND_SOC_DAILINK_REG(playback10),
998 	},
999 	[DAI_LINK_DL11_FE] = {
1000 		.name = "DL11_FE",
1001 		.stream_name = "DL11 Playback",
1002 		.trigger = {
1003 			SND_SOC_DPCM_TRIGGER_POST,
1004 			SND_SOC_DPCM_TRIGGER_POST,
1005 		},
1006 		.dynamic = 1,
1007 		.dpcm_playback = 1,
1008 		SND_SOC_DAILINK_REG(playback11),
1009 	},
1010 	[DAI_LINK_UL1_FE] = {
1011 		.name = "UL1_FE",
1012 		.stream_name = "UL1 Capture",
1013 		.trigger = {
1014 			SND_SOC_DPCM_TRIGGER_PRE,
1015 			SND_SOC_DPCM_TRIGGER_PRE,
1016 		},
1017 		.dynamic = 1,
1018 		.dpcm_capture = 1,
1019 		SND_SOC_DAILINK_REG(capture1),
1020 	},
1021 	[DAI_LINK_UL2_FE] = {
1022 		.name = "UL2_FE",
1023 		.stream_name = "UL2 Capture",
1024 		.trigger = {
1025 			SND_SOC_DPCM_TRIGGER_POST,
1026 			SND_SOC_DPCM_TRIGGER_POST,
1027 		},
1028 		.dynamic = 1,
1029 		.dpcm_capture = 1,
1030 		SND_SOC_DAILINK_REG(capture2),
1031 	},
1032 	[DAI_LINK_UL3_FE] = {
1033 		.name = "UL3_FE",
1034 		.stream_name = "UL3 Capture",
1035 		.trigger = {
1036 			SND_SOC_DPCM_TRIGGER_POST,
1037 			SND_SOC_DPCM_TRIGGER_POST,
1038 		},
1039 		.dynamic = 1,
1040 		.dpcm_capture = 1,
1041 		SND_SOC_DAILINK_REG(capture3),
1042 	},
1043 	[DAI_LINK_UL4_FE] = {
1044 		.name = "UL4_FE",
1045 		.stream_name = "UL4 Capture",
1046 		.trigger = {
1047 			SND_SOC_DPCM_TRIGGER_POST,
1048 			SND_SOC_DPCM_TRIGGER_POST,
1049 		},
1050 		.dynamic = 1,
1051 		.dpcm_capture = 1,
1052 		.dpcm_merged_chan = 1,
1053 		.dpcm_merged_rate = 1,
1054 		.dpcm_merged_format = 1,
1055 		SND_SOC_DAILINK_REG(capture4),
1056 	},
1057 	[DAI_LINK_UL5_FE] = {
1058 		.name = "UL5_FE",
1059 		.stream_name = "UL5 Capture",
1060 		.trigger = {
1061 			SND_SOC_DPCM_TRIGGER_POST,
1062 			SND_SOC_DPCM_TRIGGER_POST,
1063 		},
1064 		.dynamic = 1,
1065 		.dpcm_capture = 1,
1066 		.dpcm_merged_chan = 1,
1067 		.dpcm_merged_rate = 1,
1068 		.dpcm_merged_format = 1,
1069 		SND_SOC_DAILINK_REG(capture5),
1070 	},
1071 	[DAI_LINK_UL6_FE] = {
1072 		.name = "UL6_FE",
1073 		.stream_name = "UL6 Capture",
1074 		.trigger = {
1075 			SND_SOC_DPCM_TRIGGER_PRE,
1076 			SND_SOC_DPCM_TRIGGER_PRE,
1077 		},
1078 		.dynamic = 1,
1079 		.dpcm_capture = 1,
1080 		SND_SOC_DAILINK_REG(capture6),
1081 	},
1082 	[DAI_LINK_UL8_FE] = {
1083 		.name = "UL8_FE",
1084 		.stream_name = "UL8 Capture",
1085 		.trigger = {
1086 			SND_SOC_DPCM_TRIGGER_POST,
1087 			SND_SOC_DPCM_TRIGGER_POST,
1088 		},
1089 		.dynamic = 1,
1090 		.dpcm_capture = 1,
1091 		SND_SOC_DAILINK_REG(capture8),
1092 	},
1093 	[DAI_LINK_UL9_FE] = {
1094 		.name = "UL9_FE",
1095 		.stream_name = "UL9 Capture",
1096 		.trigger = {
1097 			SND_SOC_DPCM_TRIGGER_POST,
1098 			SND_SOC_DPCM_TRIGGER_POST,
1099 		},
1100 		.dynamic = 1,
1101 		.dpcm_capture = 1,
1102 		SND_SOC_DAILINK_REG(capture9),
1103 	},
1104 	[DAI_LINK_UL10_FE] = {
1105 		.name = "UL10_FE",
1106 		.stream_name = "UL10 Capture",
1107 		.trigger = {
1108 			SND_SOC_DPCM_TRIGGER_POST,
1109 			SND_SOC_DPCM_TRIGGER_POST,
1110 		},
1111 		.dynamic = 1,
1112 		.dpcm_capture = 1,
1113 		SND_SOC_DAILINK_REG(capture10),
1114 	},
1115 	/* BE */
1116 	[DAI_LINK_DL_SRC_BE] = {
1117 		.name = "DL_SRC_BE",
1118 		.no_pcm = 1,
1119 		.dpcm_playback = 1,
1120 		SND_SOC_DAILINK_REG(dl_src),
1121 	},
1122 	[DAI_LINK_DPTX_BE] = {
1123 		.name = "DPTX_BE",
1124 		.ops = &mt8188_dptx_ops,
1125 		.be_hw_params_fixup = mt8188_dptx_hw_params_fixup,
1126 		.no_pcm = 1,
1127 		.dpcm_playback = 1,
1128 		SND_SOC_DAILINK_REG(dptx),
1129 	},
1130 	[DAI_LINK_ETDM1_IN_BE] = {
1131 		.name = "ETDM1_IN_BE",
1132 		.no_pcm = 1,
1133 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1134 			SND_SOC_DAIFMT_NB_NF |
1135 			SND_SOC_DAIFMT_CBP_CFP,
1136 		.dpcm_capture = 1,
1137 		.ignore_suspend = 1,
1138 		SND_SOC_DAILINK_REG(etdm1_in),
1139 	},
1140 	[DAI_LINK_ETDM2_IN_BE] = {
1141 		.name = "ETDM2_IN_BE",
1142 		.no_pcm = 1,
1143 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1144 			SND_SOC_DAIFMT_NB_NF |
1145 			SND_SOC_DAIFMT_CBP_CFP,
1146 		.dpcm_capture = 1,
1147 		SND_SOC_DAILINK_REG(etdm2_in),
1148 	},
1149 	[DAI_LINK_ETDM1_OUT_BE] = {
1150 		.name = "ETDM1_OUT_BE",
1151 		.no_pcm = 1,
1152 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1153 			SND_SOC_DAIFMT_NB_NF |
1154 			SND_SOC_DAIFMT_CBC_CFC,
1155 		.dpcm_playback = 1,
1156 		SND_SOC_DAILINK_REG(etdm1_out),
1157 	},
1158 	[DAI_LINK_ETDM2_OUT_BE] = {
1159 		.name = "ETDM2_OUT_BE",
1160 		.no_pcm = 1,
1161 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1162 			SND_SOC_DAIFMT_NB_NF |
1163 			SND_SOC_DAIFMT_CBC_CFC,
1164 		.dpcm_playback = 1,
1165 		SND_SOC_DAILINK_REG(etdm2_out),
1166 	},
1167 	[DAI_LINK_ETDM3_OUT_BE] = {
1168 		.name = "ETDM3_OUT_BE",
1169 		.no_pcm = 1,
1170 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1171 			SND_SOC_DAIFMT_NB_NF |
1172 			SND_SOC_DAIFMT_CBC_CFC,
1173 		.dpcm_playback = 1,
1174 		SND_SOC_DAILINK_REG(etdm3_out),
1175 	},
1176 	[DAI_LINK_PCM1_BE] = {
1177 		.name = "PCM1_BE",
1178 		.no_pcm = 1,
1179 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1180 			SND_SOC_DAIFMT_NB_NF |
1181 			SND_SOC_DAIFMT_CBC_CFC,
1182 		.dpcm_playback = 1,
1183 		.dpcm_capture = 1,
1184 		SND_SOC_DAILINK_REG(pcm1),
1185 	},
1186 	[DAI_LINK_UL_SRC_BE] = {
1187 		.name = "UL_SRC_BE",
1188 		.no_pcm = 1,
1189 		.dpcm_capture = 1,
1190 		SND_SOC_DAILINK_REG(ul_src),
1191 	},
1192 
1193 	/* SOF BE */
1194 	[DAI_LINK_SOF_DL2_BE] = {
1195 		.name = "AFE_SOF_DL2",
1196 		.no_pcm = 1,
1197 		.dpcm_playback = 1,
1198 		.ops = &mt8188_sof_be_ops,
1199 		SND_SOC_DAILINK_REG(AFE_SOF_DL2),
1200 	},
1201 	[DAI_LINK_SOF_DL3_BE] = {
1202 		.name = "AFE_SOF_DL3",
1203 		.no_pcm = 1,
1204 		.dpcm_playback = 1,
1205 		.ops = &mt8188_sof_be_ops,
1206 		SND_SOC_DAILINK_REG(AFE_SOF_DL3),
1207 	},
1208 	[DAI_LINK_SOF_UL4_BE] = {
1209 		.name = "AFE_SOF_UL4",
1210 		.no_pcm = 1,
1211 		.dpcm_capture = 1,
1212 		.ops = &mt8188_sof_be_ops,
1213 		SND_SOC_DAILINK_REG(AFE_SOF_UL4),
1214 	},
1215 	[DAI_LINK_SOF_UL5_BE] = {
1216 		.name = "AFE_SOF_UL5",
1217 		.no_pcm = 1,
1218 		.dpcm_capture = 1,
1219 		.ops = &mt8188_sof_be_ops,
1220 		SND_SOC_DAILINK_REG(AFE_SOF_UL5),
1221 	},
1222 };
1223 
1224 static void mt8188_fixup_controls(struct snd_soc_card *card)
1225 {
1226 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(card);
1227 	struct mt8188_mt6359_priv *priv = soc_card_data->mach_priv;
1228 	struct mt8188_card_data *card_data = (struct mt8188_card_data *)priv->private_data;
1229 	struct snd_kcontrol *kctl;
1230 
1231 	if (card_data->quirk & (NAU8825_HS_PRESENT | RT5682S_HS_PRESENT | ES8326_HS_PRESENT)) {
1232 		struct snd_soc_dapm_widget *w, *next_w;
1233 
1234 		for_each_card_widgets_safe(card, w, next_w) {
1235 			if (strcmp(w->name, "Headphone"))
1236 				continue;
1237 
1238 			snd_soc_dapm_free_widget(w);
1239 		}
1240 
1241 		kctl = snd_ctl_find_id_mixer(card->snd_card, "Headphone Switch");
1242 		if (kctl)
1243 			snd_ctl_remove(card->snd_card, kctl);
1244 		else
1245 			dev_warn(card->dev, "Cannot find ctl : Headphone Switch\n");
1246 	}
1247 }
1248 
1249 static struct snd_soc_card mt8188_mt6359_soc_card = {
1250 	.owner = THIS_MODULE,
1251 	.dai_link = mt8188_mt6359_dai_links,
1252 	.num_links = ARRAY_SIZE(mt8188_mt6359_dai_links),
1253 	.dapm_widgets = mt8188_mt6359_widgets,
1254 	.num_dapm_widgets = ARRAY_SIZE(mt8188_mt6359_widgets),
1255 	.dapm_routes = mt8188_mt6359_routes,
1256 	.num_dapm_routes = ARRAY_SIZE(mt8188_mt6359_routes),
1257 	.controls = mt8188_mt6359_controls,
1258 	.num_controls = ARRAY_SIZE(mt8188_mt6359_controls),
1259 	.fixup_controls = mt8188_fixup_controls,
1260 };
1261 
1262 static int mt8188_mt6359_dev_probe(struct platform_device *pdev)
1263 {
1264 	struct snd_soc_card *card = &mt8188_mt6359_soc_card;
1265 	struct device_node *platform_node;
1266 	struct device_node *adsp_node;
1267 	struct mtk_soc_card_data *soc_card_data;
1268 	struct mt8188_mt6359_priv *priv;
1269 	struct mt8188_card_data *card_data;
1270 	struct snd_soc_dai_link *dai_link;
1271 	bool init_mt6359 = false;
1272 	bool init_es8326 = false;
1273 	bool init_nau8825 = false;
1274 	bool init_rt5682s = false;
1275 	bool init_max98390 = false;
1276 	bool init_dumb = false;
1277 	int ret, i;
1278 
1279 	card_data = (struct mt8188_card_data *)of_device_get_match_data(&pdev->dev);
1280 	card->dev = &pdev->dev;
1281 
1282 	ret = snd_soc_of_parse_card_name(card, "model");
1283 	if (ret)
1284 		return dev_err_probe(&pdev->dev, ret, "%s new card name parsing error\n",
1285 				     __func__);
1286 
1287 	if (!card->name)
1288 		card->name = card_data->name;
1289 
1290 	if (of_property_read_bool(pdev->dev.of_node, "audio-routing")) {
1291 		ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
1292 		if (ret)
1293 			return ret;
1294 	}
1295 
1296 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1297 	if (!priv)
1298 		return -ENOMEM;
1299 
1300 	soc_card_data = devm_kzalloc(&pdev->dev, sizeof(*card_data), GFP_KERNEL);
1301 	if (!soc_card_data)
1302 		return -ENOMEM;
1303 
1304 	soc_card_data->mach_priv = priv;
1305 
1306 	adsp_node = of_parse_phandle(pdev->dev.of_node, "mediatek,adsp", 0);
1307 	if (adsp_node) {
1308 		struct mtk_sof_priv *sof_priv;
1309 
1310 		sof_priv = devm_kzalloc(&pdev->dev, sizeof(*sof_priv), GFP_KERNEL);
1311 		if (!sof_priv) {
1312 			ret = -ENOMEM;
1313 			goto err_adsp_node;
1314 		}
1315 		sof_priv->conn_streams = g_sof_conn_streams;
1316 		sof_priv->num_streams = ARRAY_SIZE(g_sof_conn_streams);
1317 		soc_card_data->sof_priv = sof_priv;
1318 		card->probe = mtk_sof_card_probe;
1319 		card->late_probe = mtk_sof_card_late_probe;
1320 		if (!card->topology_shortname_created) {
1321 			snprintf(card->topology_shortname, 32, "sof-%s", card->name);
1322 			card->topology_shortname_created = true;
1323 		}
1324 		card->name = card->topology_shortname;
1325 	}
1326 
1327 	if (of_property_read_bool(pdev->dev.of_node, "mediatek,dai-link")) {
1328 		ret = mtk_sof_dailink_parse_of(card, pdev->dev.of_node,
1329 					       "mediatek,dai-link",
1330 					       mt8188_mt6359_dai_links,
1331 					       ARRAY_SIZE(mt8188_mt6359_dai_links));
1332 		if (ret) {
1333 			dev_err_probe(&pdev->dev, ret, "Parse dai-link fail\n");
1334 			goto err_adsp_node;
1335 		}
1336 	} else {
1337 		if (!adsp_node)
1338 			card->num_links = DAI_LINK_REGULAR_NUM;
1339 	}
1340 
1341 	platform_node = of_parse_phandle(pdev->dev.of_node,
1342 					 "mediatek,platform", 0);
1343 	if (!platform_node) {
1344 		ret = dev_err_probe(&pdev->dev, -EINVAL,
1345 				    "Property 'platform' missing or invalid\n");
1346 		goto err_adsp_node;
1347 
1348 	}
1349 
1350 	ret = parse_dai_link_info(card);
1351 	if (ret)
1352 		goto err;
1353 
1354 	for_each_card_prelinks(card, i, dai_link) {
1355 		if (!dai_link->platforms->name) {
1356 			if (!strncmp(dai_link->name, "AFE_SOF", strlen("AFE_SOF")) && adsp_node)
1357 				dai_link->platforms->of_node = adsp_node;
1358 			else
1359 				dai_link->platforms->of_node = platform_node;
1360 		}
1361 
1362 		if (strcmp(dai_link->name, "DPTX_BE") == 0) {
1363 			if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
1364 				dai_link->init = mt8188_dptx_codec_init;
1365 		} else if (strcmp(dai_link->name, "ETDM3_OUT_BE") == 0) {
1366 			if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
1367 				dai_link->init = mt8188_hdmi_codec_init;
1368 		} else if (strcmp(dai_link->name, "DL_SRC_BE") == 0 ||
1369 			   strcmp(dai_link->name, "UL_SRC_BE") == 0) {
1370 			if (!init_mt6359) {
1371 				dai_link->init = mt8188_mt6359_init;
1372 				init_mt6359 = true;
1373 			}
1374 		} else if (strcmp(dai_link->name, "ETDM1_OUT_BE") == 0 ||
1375 			   strcmp(dai_link->name, "ETDM2_OUT_BE") == 0 ||
1376 			   strcmp(dai_link->name, "ETDM1_IN_BE") == 0 ||
1377 			   strcmp(dai_link->name, "ETDM2_IN_BE") == 0) {
1378 			if (!strcmp(dai_link->codecs->dai_name, MAX98390_CODEC_DAI)) {
1379 				/*
1380 				 * The TDM protocol settings with fixed 4 slots are defined in
1381 				 * mt8188_max98390_ops. Two amps is I2S mode,
1382 				 * SOC and codec don't require TDM settings.
1383 				 */
1384 				if (!(card_data->quirk & MAX98390_TWO_AMP)) {
1385 					dai_link->ops = &mt8188_max98390_ops;
1386 				}
1387 				if (!init_max98390) {
1388 					dai_link->init = mt8188_max98390_codec_init;
1389 					init_max98390 = true;
1390 				}
1391 			} else if (!strcmp(dai_link->codecs->dai_name, NAU8825_CODEC_DAI)) {
1392 				dai_link->ops = &mt8188_nau8825_ops;
1393 				if (!init_nau8825) {
1394 					dai_link->init = mt8188_headset_codec_init;
1395 					dai_link->exit = mt8188_headset_codec_exit;
1396 					init_nau8825 = true;
1397 				}
1398 			} else if (!strcmp(dai_link->codecs->dai_name, RT5682S_CODEC_DAI)) {
1399 				dai_link->ops = &mt8188_rt5682s_i2s_ops;
1400 				if (!init_rt5682s) {
1401 					dai_link->init = mt8188_headset_codec_init;
1402 					dai_link->exit = mt8188_headset_codec_exit;
1403 					init_rt5682s = true;
1404 				}
1405 			} else if (!strcmp(dai_link->codecs->dai_name, ES8326_CODEC_DAI)) {
1406 				dai_link->ops = &mt8188_es8326_ops;
1407 				if (!init_es8326) {
1408 					dai_link->init = mt8188_headset_codec_init;
1409 					dai_link->exit = mt8188_headset_codec_exit;
1410 					init_es8326 = true;
1411 				}
1412 			} else {
1413 				if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai")) {
1414 					if (!init_dumb) {
1415 						dai_link->init = mt8188_dumb_amp_init;
1416 						init_dumb = true;
1417 					}
1418 				}
1419 			}
1420 		}
1421 	}
1422 
1423 	priv->private_data = card_data;
1424 	snd_soc_card_set_drvdata(card, soc_card_data);
1425 
1426 	ret = devm_snd_soc_register_card(&pdev->dev, card);
1427 	if (ret)
1428 		dev_err_probe(&pdev->dev, ret, "%s snd_soc_register_card fail\n",
1429 			      __func__);
1430 err:
1431 	of_node_put(platform_node);
1432 	clean_card_reference(card);
1433 
1434 err_adsp_node:
1435 	of_node_put(adsp_node);
1436 
1437 	return ret;
1438 }
1439 
1440 static struct mt8188_card_data mt8188_evb_card = {
1441 	.name = "mt8188_mt6359",
1442 };
1443 
1444 static struct mt8188_card_data mt8188_nau8825_card = {
1445 	.name = "mt8188_nau8825",
1446 	.quirk = NAU8825_HS_PRESENT,
1447 };
1448 
1449 static struct mt8188_card_data mt8188_rt5682s_card = {
1450 	.name = "mt8188_rt5682s",
1451 	.quirk = RT5682S_HS_PRESENT | MAX98390_TWO_AMP,
1452 };
1453 
1454 static struct mt8188_card_data mt8188_es8326_card = {
1455 	.name = "mt8188_es8326",
1456 	.quirk = ES8326_HS_PRESENT | MAX98390_TWO_AMP,
1457 };
1458 
1459 static const struct of_device_id mt8188_mt6359_dt_match[] = {
1460 	{ .compatible = "mediatek,mt8188-mt6359-evb", .data = &mt8188_evb_card, },
1461 	{ .compatible = "mediatek,mt8188-nau8825", .data = &mt8188_nau8825_card, },
1462 	{ .compatible = "mediatek,mt8188-rt5682s", .data = &mt8188_rt5682s_card, },
1463 	{ .compatible = "mediatek,mt8188-es8326", .data = &mt8188_es8326_card, },
1464 	{ /* sentinel */ },
1465 };
1466 MODULE_DEVICE_TABLE(of, mt8188_mt6359_dt_match);
1467 
1468 static struct platform_driver mt8188_mt6359_driver = {
1469 	.driver = {
1470 		.name = "mt8188_mt6359",
1471 		.of_match_table = mt8188_mt6359_dt_match,
1472 		.pm = &snd_soc_pm_ops,
1473 	},
1474 	.probe = mt8188_mt6359_dev_probe,
1475 };
1476 
1477 module_platform_driver(mt8188_mt6359_driver);
1478 
1479 /* Module information */
1480 MODULE_DESCRIPTION("MT8188-MT6359 ALSA SoC machine driver");
1481 MODULE_AUTHOR("Trevor Wu <trevor.wu@mediatek.com>");
1482 MODULE_LICENSE("GPL");
1483 MODULE_ALIAS("mt8188 mt6359 soc card");
1484