xref: /linux/sound/soc/mediatek/mt8188/mt8188-mt6359.c (revision 9a95c5bfbf02a0a7f5983280fe284a0ff0836c34)
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 enum mt8188_jacks {
240 	MT8188_JACK_HEADSET,
241 	MT8188_JACK_DP,
242 	MT8188_JACK_HDMI,
243 	MT8188_JACK_MAX,
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 static const struct snd_kcontrol_new mt8188_dumb_spk_controls[] = {
272 	SOC_DAPM_PIN_SWITCH("Ext Spk"),
273 };
274 
275 static const struct snd_soc_dapm_widget mt8188_dumb_spk_widgets[] = {
276 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
277 };
278 
279 static const struct snd_kcontrol_new mt8188_dual_spk_controls[] = {
280 	SOC_DAPM_PIN_SWITCH("Left Spk"),
281 	SOC_DAPM_PIN_SWITCH("Right Spk"),
282 };
283 
284 static const struct snd_soc_dapm_widget mt8188_dual_spk_widgets[] = {
285 	SND_SOC_DAPM_SPK("Left Spk", NULL),
286 	SND_SOC_DAPM_SPK("Right Spk", NULL),
287 };
288 
289 static const struct snd_kcontrol_new mt8188_rear_spk_controls[] = {
290 	SOC_DAPM_PIN_SWITCH("Rear Left Spk"),
291 	SOC_DAPM_PIN_SWITCH("Rear Right Spk"),
292 };
293 
294 static const struct snd_soc_dapm_widget mt8188_rear_spk_widgets[] = {
295 	SND_SOC_DAPM_SPK("Rear Left Spk", NULL),
296 	SND_SOC_DAPM_SPK("Rear Right Spk", NULL),
297 };
298 
299 static const struct snd_soc_dapm_widget mt8188_mt6359_widgets[] = {
300 	SND_SOC_DAPM_HP("Headphone", NULL),
301 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
302 	SND_SOC_DAPM_SINK("HDMI"),
303 	SND_SOC_DAPM_SINK("DP"),
304 	SND_SOC_DAPM_MIXER(SOF_DMA_DL2, SND_SOC_NOPM, 0, 0, NULL, 0),
305 	SND_SOC_DAPM_MIXER(SOF_DMA_DL3, SND_SOC_NOPM, 0, 0, NULL, 0),
306 	SND_SOC_DAPM_MIXER(SOF_DMA_UL4, SND_SOC_NOPM, 0, 0, NULL, 0),
307 	SND_SOC_DAPM_MIXER(SOF_DMA_UL5, SND_SOC_NOPM, 0, 0, NULL, 0),
308 
309 	/* dynamic pinctrl */
310 	SND_SOC_DAPM_PINCTRL("ETDM_SPK_PIN", "aud_etdm_spk_on", "aud_etdm_spk_off"),
311 	SND_SOC_DAPM_PINCTRL("ETDM_HP_PIN", "aud_etdm_hp_on", "aud_etdm_hp_off"),
312 	SND_SOC_DAPM_PINCTRL("MTKAIF_PIN", "aud_mtkaif_on", "aud_mtkaif_off"),
313 };
314 
315 static const struct snd_kcontrol_new mt8188_mt6359_controls[] = {
316 	SOC_DAPM_PIN_SWITCH("Headphone"),
317 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
318 };
319 
320 static const struct snd_soc_dapm_widget mt8188_nau8825_widgets[] = {
321 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
322 };
323 
324 static const struct snd_kcontrol_new mt8188_nau8825_controls[] = {
325 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
326 };
327 
328 static const struct snd_soc_dapm_route mt8188_mt6359_routes[] = {
329 	/* SOF Uplink */
330 	{SOF_DMA_UL4, NULL, "O034"},
331 	{SOF_DMA_UL4, NULL, "O035"},
332 	{SOF_DMA_UL5, NULL, "O036"},
333 	{SOF_DMA_UL5, NULL, "O037"},
334 	/* SOF Downlink */
335 	{"I070", NULL, SOF_DMA_DL2},
336 	{"I071", NULL, SOF_DMA_DL2},
337 	{"I020", NULL, SOF_DMA_DL3},
338 	{"I021", NULL, SOF_DMA_DL3},
339 };
340 
341 static int mt8188_mt6359_mtkaif_calibration(struct snd_soc_pcm_runtime *rtd)
342 {
343 	struct snd_soc_component *cmpnt_afe =
344 		snd_soc_rtdcom_lookup(rtd, AFE_PCM_NAME);
345 	struct snd_soc_component *cmpnt_codec =
346 		snd_soc_rtd_to_codec(rtd, 0)->component;
347 	struct snd_soc_dapm_widget *pin_w = NULL, *w;
348 	struct mtk_base_afe *afe;
349 	struct mt8188_afe_private *afe_priv;
350 	struct mtkaif_param *param;
351 	int chosen_phase_1, chosen_phase_2;
352 	int prev_cycle_1, prev_cycle_2;
353 	u8 test_done_1, test_done_2;
354 	int cycle_1, cycle_2;
355 	int mtkaif_chosen_phase[MT8188_MTKAIF_MISO_NUM];
356 	int mtkaif_phase_cycle[MT8188_MTKAIF_MISO_NUM];
357 	int mtkaif_calibration_num_phase;
358 	bool mtkaif_calibration_ok;
359 	u32 monitor = 0;
360 	int counter;
361 	int phase;
362 	int i;
363 
364 	if (!cmpnt_afe)
365 		return -EINVAL;
366 
367 	afe = snd_soc_component_get_drvdata(cmpnt_afe);
368 	afe_priv = afe->platform_priv;
369 	param = &afe_priv->mtkaif_params;
370 
371 	dev_dbg(afe->dev, "%s(), start\n", __func__);
372 
373 	param->mtkaif_calibration_ok = false;
374 	for (i = 0; i < MT8188_MTKAIF_MISO_NUM; i++) {
375 		param->mtkaif_chosen_phase[i] = -1;
376 		param->mtkaif_phase_cycle[i] = 0;
377 		mtkaif_chosen_phase[i] = -1;
378 		mtkaif_phase_cycle[i] = 0;
379 	}
380 
381 	if (IS_ERR(afe_priv->topckgen)) {
382 		dev_info(afe->dev, "%s() Cannot find topckgen controller\n",
383 			 __func__);
384 		return 0;
385 	}
386 
387 	for_each_card_widgets(rtd->card, w) {
388 		if (!strcmp(w->name, "MTKAIF_PIN")) {
389 			pin_w = w;
390 			break;
391 		}
392 	}
393 
394 	if (pin_w)
395 		dapm_pinctrl_event(pin_w, NULL, SND_SOC_DAPM_PRE_PMU);
396 	else
397 		dev_dbg(afe->dev, "%s(), no pinmux widget, please check if default on\n", __func__);
398 
399 	pm_runtime_get_sync(afe->dev);
400 	mt6359_mtkaif_calibration_enable(cmpnt_codec);
401 
402 	/* set test type to synchronizer pulse */
403 	regmap_write(afe_priv->topckgen, CKSYS_AUD_TOP_CFG, RG_TEST_TYPE);
404 	mtkaif_calibration_num_phase = 42;	/* mt6359: 0 ~ 42 */
405 	mtkaif_calibration_ok = true;
406 
407 	for (phase = 0;
408 	     phase <= mtkaif_calibration_num_phase && mtkaif_calibration_ok;
409 	     phase++) {
410 		mt6359_set_mtkaif_calibration_phase(cmpnt_codec,
411 						    phase, phase, phase);
412 
413 		regmap_set_bits(afe_priv->topckgen, CKSYS_AUD_TOP_CFG, RG_TEST_ON);
414 
415 		test_done_1 = 0;
416 		test_done_2 = 0;
417 
418 		cycle_1 = -1;
419 		cycle_2 = -1;
420 
421 		counter = 0;
422 		while (!(test_done_1 & test_done_2)) {
423 			regmap_read(afe_priv->topckgen,
424 				    CKSYS_AUD_TOP_MON, &monitor);
425 			test_done_1 = FIELD_GET(TEST_MISO_DONE_1, monitor);
426 			test_done_2 = FIELD_GET(TEST_MISO_DONE_2, monitor);
427 
428 			if (test_done_1 == 1)
429 				cycle_1 = FIELD_GET(TEST_MISO_COUNT_1, monitor);
430 
431 			if (test_done_2 == 1)
432 				cycle_2 = FIELD_GET(TEST_MISO_COUNT_2, monitor);
433 
434 			/* handle if never test done */
435 			if (++counter > 10000) {
436 				dev_err(afe->dev, "%s(), test fail, cycle_1 %d, cycle_2 %d, monitor 0x%x\n",
437 					__func__, cycle_1, cycle_2, monitor);
438 				mtkaif_calibration_ok = false;
439 				break;
440 			}
441 		}
442 
443 		if (phase == 0) {
444 			prev_cycle_1 = cycle_1;
445 			prev_cycle_2 = cycle_2;
446 		}
447 
448 		if (cycle_1 != prev_cycle_1 &&
449 		    mtkaif_chosen_phase[MT8188_MTKAIF_MISO_0] < 0) {
450 			mtkaif_chosen_phase[MT8188_MTKAIF_MISO_0] = phase - 1;
451 			mtkaif_phase_cycle[MT8188_MTKAIF_MISO_0] = prev_cycle_1;
452 		}
453 
454 		if (cycle_2 != prev_cycle_2 &&
455 		    mtkaif_chosen_phase[MT8188_MTKAIF_MISO_1] < 0) {
456 			mtkaif_chosen_phase[MT8188_MTKAIF_MISO_1] = phase - 1;
457 			mtkaif_phase_cycle[MT8188_MTKAIF_MISO_1] = prev_cycle_2;
458 		}
459 
460 		regmap_clear_bits(afe_priv->topckgen, CKSYS_AUD_TOP_CFG, RG_TEST_ON);
461 
462 		if (mtkaif_chosen_phase[MT8188_MTKAIF_MISO_0] >= 0 &&
463 		    mtkaif_chosen_phase[MT8188_MTKAIF_MISO_1] >= 0)
464 			break;
465 	}
466 
467 	if (mtkaif_chosen_phase[MT8188_MTKAIF_MISO_0] < 0) {
468 		mtkaif_calibration_ok = false;
469 		chosen_phase_1 = 0;
470 	} else {
471 		chosen_phase_1 = mtkaif_chosen_phase[MT8188_MTKAIF_MISO_0];
472 	}
473 
474 	if (mtkaif_chosen_phase[MT8188_MTKAIF_MISO_1] < 0) {
475 		mtkaif_calibration_ok = false;
476 		chosen_phase_2 = 0;
477 	} else {
478 		chosen_phase_2 = mtkaif_chosen_phase[MT8188_MTKAIF_MISO_1];
479 	}
480 
481 	mt6359_set_mtkaif_calibration_phase(cmpnt_codec,
482 					    chosen_phase_1,
483 					    chosen_phase_2,
484 					    0);
485 
486 	mt6359_mtkaif_calibration_disable(cmpnt_codec);
487 	pm_runtime_put(afe->dev);
488 
489 	param->mtkaif_calibration_ok = mtkaif_calibration_ok;
490 	param->mtkaif_chosen_phase[MT8188_MTKAIF_MISO_0] = chosen_phase_1;
491 	param->mtkaif_chosen_phase[MT8188_MTKAIF_MISO_1] = chosen_phase_2;
492 
493 	for (i = 0; i < MT8188_MTKAIF_MISO_NUM; i++)
494 		param->mtkaif_phase_cycle[i] = mtkaif_phase_cycle[i];
495 
496 	if (pin_w)
497 		dapm_pinctrl_event(pin_w, NULL, SND_SOC_DAPM_POST_PMD);
498 
499 	dev_dbg(afe->dev, "%s(), end, calibration ok %d\n",
500 		__func__, param->mtkaif_calibration_ok);
501 
502 	return 0;
503 }
504 
505 static int mt8188_mt6359_init(struct snd_soc_pcm_runtime *rtd)
506 {
507 	struct snd_soc_component *cmpnt_codec =
508 		snd_soc_rtd_to_codec(rtd, 0)->component;
509 
510 	/* set mtkaif protocol */
511 	mt6359_set_mtkaif_protocol(cmpnt_codec,
512 				   MT6359_MTKAIF_PROTOCOL_2_CLK_P2);
513 
514 	/* mtkaif calibration */
515 	mt8188_mt6359_mtkaif_calibration(rtd);
516 
517 	return 0;
518 }
519 
520 enum {
521 	DAI_LINK_DL2_FE,
522 	DAI_LINK_DL3_FE,
523 	DAI_LINK_DL6_FE,
524 	DAI_LINK_DL7_FE,
525 	DAI_LINK_DL8_FE,
526 	DAI_LINK_DL10_FE,
527 	DAI_LINK_DL11_FE,
528 	DAI_LINK_UL1_FE,
529 	DAI_LINK_UL2_FE,
530 	DAI_LINK_UL3_FE,
531 	DAI_LINK_UL4_FE,
532 	DAI_LINK_UL5_FE,
533 	DAI_LINK_UL6_FE,
534 	DAI_LINK_UL8_FE,
535 	DAI_LINK_UL9_FE,
536 	DAI_LINK_UL10_FE,
537 	DAI_LINK_DL_SRC_BE,
538 	DAI_LINK_DPTX_BE,
539 	DAI_LINK_ETDM1_IN_BE,
540 	DAI_LINK_ETDM2_IN_BE,
541 	DAI_LINK_ETDM1_OUT_BE,
542 	DAI_LINK_ETDM2_OUT_BE,
543 	DAI_LINK_ETDM3_OUT_BE,
544 	DAI_LINK_PCM1_BE,
545 	DAI_LINK_UL_SRC_BE,
546 	DAI_LINK_REGULAR_LAST = DAI_LINK_UL_SRC_BE,
547 	DAI_LINK_SOF_START,
548 	DAI_LINK_SOF_DL2_BE = DAI_LINK_SOF_START,
549 	DAI_LINK_SOF_DL3_BE,
550 	DAI_LINK_SOF_UL4_BE,
551 	DAI_LINK_SOF_UL5_BE,
552 	DAI_LINK_SOF_END = DAI_LINK_SOF_UL5_BE,
553 };
554 
555 #define	DAI_LINK_REGULAR_NUM	(DAI_LINK_REGULAR_LAST + 1)
556 
557 static int mt8188_dptx_hw_params(struct snd_pcm_substream *substream,
558 				 struct snd_pcm_hw_params *params)
559 {
560 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
561 	unsigned int rate = params_rate(params);
562 	unsigned int mclk_fs_ratio = 256;
563 	unsigned int mclk_fs = rate * mclk_fs_ratio;
564 	struct snd_soc_dai *dai = snd_soc_rtd_to_cpu(rtd, 0);
565 
566 	return snd_soc_dai_set_sysclk(dai, 0, mclk_fs, SND_SOC_CLOCK_OUT);
567 }
568 
569 static const struct snd_soc_ops mt8188_dptx_ops = {
570 	.hw_params = mt8188_dptx_hw_params,
571 };
572 
573 static int mt8188_dptx_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
574 				       struct snd_pcm_hw_params *params)
575 {
576 	/* fix BE i2s format to 32bit, clean param mask first */
577 	snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
578 			     0, (__force unsigned int)SNDRV_PCM_FORMAT_LAST);
579 
580 	params_set_format(params, SNDRV_PCM_FORMAT_S32_LE);
581 
582 	return 0;
583 }
584 
585 static int mt8188_hdmi_codec_init(struct snd_soc_pcm_runtime *rtd)
586 {
587 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(rtd->card);
588 	struct snd_soc_jack *jack = &soc_card_data->card_data->jacks[MT8188_JACK_HDMI];
589 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
590 	int ret = 0;
591 
592 	ret = snd_soc_card_jack_new_pins(rtd->card, "HDMI Jack",
593 					 SND_JACK_LINEOUT, jack,
594 					 mt8188_hdmi_jack_pins,
595 					 ARRAY_SIZE(mt8188_hdmi_jack_pins));
596 	if (ret) {
597 		dev_err(rtd->dev, "%s, new jack failed: %d\n", __func__, ret);
598 		return ret;
599 	}
600 
601 	ret = snd_soc_component_set_jack(component, jack, NULL);
602 	if (ret) {
603 		dev_err(rtd->dev, "%s, set jack failed on %s (ret=%d)\n",
604 			__func__, component->name, ret);
605 		return ret;
606 	}
607 
608 	return 0;
609 }
610 
611 static int mt8188_dptx_codec_init(struct snd_soc_pcm_runtime *rtd)
612 {
613 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(rtd->card);
614 	struct snd_soc_jack *jack = &soc_card_data->card_data->jacks[MT8188_JACK_DP];
615 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
616 	int ret = 0;
617 
618 	ret = snd_soc_card_jack_new_pins(rtd->card, "DP Jack", SND_JACK_LINEOUT,
619 					 jack, mt8188_dp_jack_pins,
620 					 ARRAY_SIZE(mt8188_dp_jack_pins));
621 	if (ret) {
622 		dev_err(rtd->dev, "%s, new jack failed: %d\n", __func__, ret);
623 		return ret;
624 	}
625 
626 	ret = snd_soc_component_set_jack(component, jack, NULL);
627 	if (ret) {
628 		dev_err(rtd->dev, "%s, set jack failed on %s (ret=%d)\n",
629 			__func__, component->name, ret);
630 		return ret;
631 	}
632 
633 	return 0;
634 }
635 
636 static int mt8188_dumb_amp_init(struct snd_soc_pcm_runtime *rtd)
637 {
638 	struct snd_soc_card *card = rtd->card;
639 	int ret = 0;
640 
641 	ret = snd_soc_dapm_new_controls(&card->dapm, mt8188_dumb_spk_widgets,
642 					ARRAY_SIZE(mt8188_dumb_spk_widgets));
643 	if (ret) {
644 		dev_err(rtd->dev, "unable to add Dumb Speaker dapm, ret %d\n", ret);
645 		return ret;
646 	}
647 
648 	ret = snd_soc_add_card_controls(card, mt8188_dumb_spk_controls,
649 					ARRAY_SIZE(mt8188_dumb_spk_controls));
650 	if (ret) {
651 		dev_err(rtd->dev, "unable to add Dumb card controls, ret %d\n", ret);
652 		return ret;
653 	}
654 
655 	return 0;
656 }
657 
658 static int mt8188_max98390_hw_params(struct snd_pcm_substream *substream,
659 				     struct snd_pcm_hw_params *params)
660 {
661 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
662 	unsigned int bit_width = params_width(params);
663 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
664 	struct snd_soc_dai *codec_dai;
665 	int i;
666 
667 	snd_soc_dai_set_tdm_slot(cpu_dai, 0xf, 0xf, 4, bit_width);
668 
669 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
670 		if (!strcmp(codec_dai->component->name, MAX98390_DEV0_NAME))
671 			snd_soc_dai_set_tdm_slot(codec_dai, 0x8, 0x3, 4, bit_width);
672 
673 		if (!strcmp(codec_dai->component->name, MAX98390_DEV1_NAME))
674 			snd_soc_dai_set_tdm_slot(codec_dai, 0x4, 0x3, 4, bit_width);
675 
676 		if (!strcmp(codec_dai->component->name, MAX98390_DEV2_NAME))
677 			snd_soc_dai_set_tdm_slot(codec_dai, 0x2, 0x3, 4, bit_width);
678 
679 		if (!strcmp(codec_dai->component->name, MAX98390_DEV3_NAME))
680 			snd_soc_dai_set_tdm_slot(codec_dai, 0x1, 0x3, 4, bit_width);
681 	}
682 	return 0;
683 }
684 
685 static const struct snd_soc_ops mt8188_max98390_ops = {
686 	.hw_params = mt8188_max98390_hw_params,
687 };
688 
689 static int mt8188_max98390_codec_init(struct snd_soc_pcm_runtime *rtd)
690 {
691 	struct snd_soc_card *card = rtd->card;
692 	int ret;
693 
694 	/* add regular speakers dapm route */
695 	ret = snd_soc_dapm_new_controls(&card->dapm, mt8188_dual_spk_widgets,
696 					ARRAY_SIZE(mt8188_dual_spk_widgets));
697 	if (ret) {
698 		dev_err(rtd->dev, "unable to add Left/Right Speaker widget, ret %d\n", ret);
699 		return ret;
700 	}
701 
702 	ret = snd_soc_add_card_controls(card, mt8188_dual_spk_controls,
703 					ARRAY_SIZE(mt8188_dual_spk_controls));
704 	if (ret) {
705 		dev_err(rtd->dev, "unable to add Left/Right card controls, ret %d\n", ret);
706 		return ret;
707 	}
708 
709 	if (rtd->dai_link->num_codecs <= 2)
710 		return 0;
711 
712 	/* add widgets/controls/dapm for rear speakers */
713 	ret = snd_soc_dapm_new_controls(&card->dapm, mt8188_rear_spk_widgets,
714 					ARRAY_SIZE(mt8188_rear_spk_widgets));
715 	if (ret) {
716 		dev_err(rtd->dev, "unable to add Rear Speaker widget, ret %d\n", ret);
717 		/* Don't need to add routes if widget addition failed */
718 		return ret;
719 	}
720 
721 	ret = snd_soc_add_card_controls(card, mt8188_rear_spk_controls,
722 					ARRAY_SIZE(mt8188_rear_spk_controls));
723 	if (ret) {
724 		dev_err(rtd->dev, "unable to add Rear card controls, ret %d\n", ret);
725 		return ret;
726 	}
727 
728 	return 0;
729 }
730 
731 static int mt8188_headset_codec_init(struct snd_soc_pcm_runtime *rtd)
732 {
733 	struct snd_soc_card *card = rtd->card;
734 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(rtd->card);
735 	struct snd_soc_jack *jack = &soc_card_data->card_data->jacks[MT8188_JACK_HEADSET];
736 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
737 	int ret;
738 
739 	ret = snd_soc_dapm_new_controls(&card->dapm, mt8188_nau8825_widgets,
740 					ARRAY_SIZE(mt8188_nau8825_widgets));
741 	if (ret) {
742 		dev_err(rtd->dev, "unable to add nau8825 card widget, ret %d\n", ret);
743 		return ret;
744 	}
745 
746 	ret = snd_soc_add_card_controls(card, mt8188_nau8825_controls,
747 					ARRAY_SIZE(mt8188_nau8825_controls));
748 	if (ret) {
749 		dev_err(rtd->dev, "unable to add nau8825 card controls, ret %d\n", ret);
750 		return ret;
751 	}
752 
753 	ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
754 					 SND_JACK_HEADSET | SND_JACK_BTN_0 |
755 					 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
756 					 SND_JACK_BTN_3,
757 					 jack,
758 					 nau8825_jack_pins,
759 					 ARRAY_SIZE(nau8825_jack_pins));
760 	if (ret) {
761 		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
762 		return ret;
763 	}
764 
765 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
766 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
767 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
768 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
769 	ret = snd_soc_component_set_jack(component, jack, NULL);
770 
771 	if (ret) {
772 		dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
773 		return ret;
774 	}
775 
776 	return 0;
777 };
778 
779 static void mt8188_headset_codec_exit(struct snd_soc_pcm_runtime *rtd)
780 {
781 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
782 
783 	snd_soc_component_set_jack(component, NULL, NULL);
784 }
785 
786 
787 static int mt8188_nau8825_hw_params(struct snd_pcm_substream *substream,
788 				    struct snd_pcm_hw_params *params)
789 {
790 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
791 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
792 	unsigned int rate = params_rate(params);
793 	unsigned int bit_width = params_width(params);
794 	int clk_freq, ret;
795 
796 	clk_freq = rate * 2 * bit_width;
797 
798 	/* Configure clock for codec */
799 	ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_BLK, 0,
800 				     SND_SOC_CLOCK_IN);
801 	if (ret < 0) {
802 		dev_err(codec_dai->dev, "can't set BCLK clock %d\n", ret);
803 		return ret;
804 	}
805 
806 	/* Configure pll for codec */
807 	ret = snd_soc_dai_set_pll(codec_dai, 0, 0, clk_freq,
808 				  params_rate(params) * 256);
809 	if (ret < 0) {
810 		dev_err(codec_dai->dev, "can't set BCLK: %d\n", ret);
811 		return ret;
812 	}
813 
814 	return 0;
815 }
816 
817 static const struct snd_soc_ops mt8188_nau8825_ops = {
818 	.hw_params = mt8188_nau8825_hw_params,
819 };
820 
821 static int mt8188_rt5682s_i2s_hw_params(struct snd_pcm_substream *substream,
822 					struct snd_pcm_hw_params *params)
823 {
824 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
825 	struct snd_soc_card *card = rtd->card;
826 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
827 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
828 	unsigned int rate = params_rate(params);
829 	int bitwidth;
830 	int ret;
831 
832 	bitwidth = snd_pcm_format_width(params_format(params));
833 	if (bitwidth < 0) {
834 		dev_err(card->dev, "invalid bit width: %d\n", bitwidth);
835 		return bitwidth;
836 	}
837 
838 	ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x00, 0x0, 0x2, bitwidth);
839 	if (ret) {
840 		dev_err(card->dev, "failed to set tdm slot\n");
841 		return ret;
842 	}
843 
844 	ret = snd_soc_dai_set_pll(codec_dai, RT5682_PLL1, RT5682_PLL1_S_BCLK1,
845 				  rate * 32, rate * 512);
846 	if (ret) {
847 		dev_err(card->dev, "failed to set pll\n");
848 		return ret;
849 	}
850 
851 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1,
852 				     rate * 512, SND_SOC_CLOCK_IN);
853 	if (ret) {
854 		dev_err(card->dev, "failed to set sysclk\n");
855 		return ret;
856 	}
857 
858 	return snd_soc_dai_set_sysclk(cpu_dai, 0, rate * 128,
859 				      SND_SOC_CLOCK_OUT);
860 }
861 
862 static const struct snd_soc_ops mt8188_rt5682s_i2s_ops = {
863 	.hw_params = mt8188_rt5682s_i2s_hw_params,
864 };
865 
866 static int mt8188_sof_be_hw_params(struct snd_pcm_substream *substream,
867 				   struct snd_pcm_hw_params *params)
868 {
869 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
870 	struct snd_soc_component *cmpnt_afe = NULL;
871 	struct snd_soc_pcm_runtime *runtime;
872 
873 	/* find afe component */
874 	for_each_card_rtds(rtd->card, runtime) {
875 		cmpnt_afe = snd_soc_rtdcom_lookup(runtime, AFE_PCM_NAME);
876 		if (cmpnt_afe)
877 			break;
878 	}
879 
880 	if (cmpnt_afe && !pm_runtime_active(cmpnt_afe->dev)) {
881 		dev_err(rtd->dev, "afe pm runtime is not active!!\n");
882 		return -EINVAL;
883 	}
884 
885 	return 0;
886 }
887 
888 static const struct snd_soc_ops mt8188_sof_be_ops = {
889 	.hw_params = mt8188_sof_be_hw_params,
890 };
891 
892 static int mt8188_es8326_hw_params(struct snd_pcm_substream *substream,
893 				 struct snd_pcm_hw_params *params)
894 {
895 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
896 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
897 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
898 	unsigned int rate = params_rate(params);
899 	int ret;
900 
901 	/* Configure MCLK for codec */
902 	ret = snd_soc_dai_set_sysclk(codec_dai, 0, rate * 256, SND_SOC_CLOCK_IN);
903 	if (ret < 0) {
904 		dev_err(codec_dai->dev, "can't set MCLK %d\n", ret);
905 		return ret;
906 	}
907 
908 	/* Configure MCLK for cpu */
909 	return snd_soc_dai_set_sysclk(cpu_dai, 0, rate * 256, SND_SOC_CLOCK_OUT);
910 }
911 
912 static const struct snd_soc_ops mt8188_es8326_ops = {
913 	.hw_params = mt8188_es8326_hw_params,
914 };
915 
916 static struct snd_soc_dai_link mt8188_mt6359_dai_links[] = {
917 	/* FE */
918 	[DAI_LINK_DL2_FE] = {
919 		.name = "DL2_FE",
920 		.stream_name = "DL2 Playback",
921 		.trigger = {
922 			SND_SOC_DPCM_TRIGGER_POST,
923 			SND_SOC_DPCM_TRIGGER_POST,
924 		},
925 		.dynamic = 1,
926 		.dpcm_playback = 1,
927 		.dpcm_merged_chan = 1,
928 		.dpcm_merged_rate = 1,
929 		.dpcm_merged_format = 1,
930 		SND_SOC_DAILINK_REG(playback2),
931 	},
932 	[DAI_LINK_DL3_FE] = {
933 		.name = "DL3_FE",
934 		.stream_name = "DL3 Playback",
935 		.trigger = {
936 			SND_SOC_DPCM_TRIGGER_POST,
937 			SND_SOC_DPCM_TRIGGER_POST,
938 		},
939 		.dynamic = 1,
940 		.dpcm_playback = 1,
941 		.dpcm_merged_chan = 1,
942 		.dpcm_merged_rate = 1,
943 		.dpcm_merged_format = 1,
944 		SND_SOC_DAILINK_REG(playback3),
945 	},
946 	[DAI_LINK_DL6_FE] = {
947 		.name = "DL6_FE",
948 		.stream_name = "DL6 Playback",
949 		.trigger = {
950 			SND_SOC_DPCM_TRIGGER_POST,
951 			SND_SOC_DPCM_TRIGGER_POST,
952 		},
953 		.dynamic = 1,
954 		.dpcm_playback = 1,
955 		.dpcm_merged_chan = 1,
956 		.dpcm_merged_rate = 1,
957 		.dpcm_merged_format = 1,
958 		SND_SOC_DAILINK_REG(playback6),
959 	},
960 	[DAI_LINK_DL7_FE] = {
961 		.name = "DL7_FE",
962 		.stream_name = "DL7 Playback",
963 		.trigger = {
964 			SND_SOC_DPCM_TRIGGER_PRE,
965 			SND_SOC_DPCM_TRIGGER_PRE,
966 		},
967 		.dynamic = 1,
968 		.dpcm_playback = 1,
969 		SND_SOC_DAILINK_REG(playback7),
970 	},
971 	[DAI_LINK_DL8_FE] = {
972 		.name = "DL8_FE",
973 		.stream_name = "DL8 Playback",
974 		.trigger = {
975 			SND_SOC_DPCM_TRIGGER_POST,
976 			SND_SOC_DPCM_TRIGGER_POST,
977 		},
978 		.dynamic = 1,
979 		.dpcm_playback = 1,
980 		SND_SOC_DAILINK_REG(playback8),
981 	},
982 	[DAI_LINK_DL10_FE] = {
983 		.name = "DL10_FE",
984 		.stream_name = "DL10 Playback",
985 		.trigger = {
986 			SND_SOC_DPCM_TRIGGER_POST,
987 			SND_SOC_DPCM_TRIGGER_POST,
988 		},
989 		.dynamic = 1,
990 		.dpcm_playback = 1,
991 		SND_SOC_DAILINK_REG(playback10),
992 	},
993 	[DAI_LINK_DL11_FE] = {
994 		.name = "DL11_FE",
995 		.stream_name = "DL11 Playback",
996 		.trigger = {
997 			SND_SOC_DPCM_TRIGGER_POST,
998 			SND_SOC_DPCM_TRIGGER_POST,
999 		},
1000 		.dynamic = 1,
1001 		.dpcm_playback = 1,
1002 		SND_SOC_DAILINK_REG(playback11),
1003 	},
1004 	[DAI_LINK_UL1_FE] = {
1005 		.name = "UL1_FE",
1006 		.stream_name = "UL1 Capture",
1007 		.trigger = {
1008 			SND_SOC_DPCM_TRIGGER_PRE,
1009 			SND_SOC_DPCM_TRIGGER_PRE,
1010 		},
1011 		.dynamic = 1,
1012 		.dpcm_capture = 1,
1013 		SND_SOC_DAILINK_REG(capture1),
1014 	},
1015 	[DAI_LINK_UL2_FE] = {
1016 		.name = "UL2_FE",
1017 		.stream_name = "UL2 Capture",
1018 		.trigger = {
1019 			SND_SOC_DPCM_TRIGGER_POST,
1020 			SND_SOC_DPCM_TRIGGER_POST,
1021 		},
1022 		.dynamic = 1,
1023 		.dpcm_capture = 1,
1024 		SND_SOC_DAILINK_REG(capture2),
1025 	},
1026 	[DAI_LINK_UL3_FE] = {
1027 		.name = "UL3_FE",
1028 		.stream_name = "UL3 Capture",
1029 		.trigger = {
1030 			SND_SOC_DPCM_TRIGGER_POST,
1031 			SND_SOC_DPCM_TRIGGER_POST,
1032 		},
1033 		.dynamic = 1,
1034 		.dpcm_capture = 1,
1035 		SND_SOC_DAILINK_REG(capture3),
1036 	},
1037 	[DAI_LINK_UL4_FE] = {
1038 		.name = "UL4_FE",
1039 		.stream_name = "UL4 Capture",
1040 		.trigger = {
1041 			SND_SOC_DPCM_TRIGGER_POST,
1042 			SND_SOC_DPCM_TRIGGER_POST,
1043 		},
1044 		.dynamic = 1,
1045 		.dpcm_capture = 1,
1046 		.dpcm_merged_chan = 1,
1047 		.dpcm_merged_rate = 1,
1048 		.dpcm_merged_format = 1,
1049 		SND_SOC_DAILINK_REG(capture4),
1050 	},
1051 	[DAI_LINK_UL5_FE] = {
1052 		.name = "UL5_FE",
1053 		.stream_name = "UL5 Capture",
1054 		.trigger = {
1055 			SND_SOC_DPCM_TRIGGER_POST,
1056 			SND_SOC_DPCM_TRIGGER_POST,
1057 		},
1058 		.dynamic = 1,
1059 		.dpcm_capture = 1,
1060 		.dpcm_merged_chan = 1,
1061 		.dpcm_merged_rate = 1,
1062 		.dpcm_merged_format = 1,
1063 		SND_SOC_DAILINK_REG(capture5),
1064 	},
1065 	[DAI_LINK_UL6_FE] = {
1066 		.name = "UL6_FE",
1067 		.stream_name = "UL6 Capture",
1068 		.trigger = {
1069 			SND_SOC_DPCM_TRIGGER_PRE,
1070 			SND_SOC_DPCM_TRIGGER_PRE,
1071 		},
1072 		.dynamic = 1,
1073 		.dpcm_capture = 1,
1074 		SND_SOC_DAILINK_REG(capture6),
1075 	},
1076 	[DAI_LINK_UL8_FE] = {
1077 		.name = "UL8_FE",
1078 		.stream_name = "UL8 Capture",
1079 		.trigger = {
1080 			SND_SOC_DPCM_TRIGGER_POST,
1081 			SND_SOC_DPCM_TRIGGER_POST,
1082 		},
1083 		.dynamic = 1,
1084 		.dpcm_capture = 1,
1085 		SND_SOC_DAILINK_REG(capture8),
1086 	},
1087 	[DAI_LINK_UL9_FE] = {
1088 		.name = "UL9_FE",
1089 		.stream_name = "UL9 Capture",
1090 		.trigger = {
1091 			SND_SOC_DPCM_TRIGGER_POST,
1092 			SND_SOC_DPCM_TRIGGER_POST,
1093 		},
1094 		.dynamic = 1,
1095 		.dpcm_capture = 1,
1096 		SND_SOC_DAILINK_REG(capture9),
1097 	},
1098 	[DAI_LINK_UL10_FE] = {
1099 		.name = "UL10_FE",
1100 		.stream_name = "UL10 Capture",
1101 		.trigger = {
1102 			SND_SOC_DPCM_TRIGGER_POST,
1103 			SND_SOC_DPCM_TRIGGER_POST,
1104 		},
1105 		.dynamic = 1,
1106 		.dpcm_capture = 1,
1107 		SND_SOC_DAILINK_REG(capture10),
1108 	},
1109 	/* BE */
1110 	[DAI_LINK_DL_SRC_BE] = {
1111 		.name = "DL_SRC_BE",
1112 		.no_pcm = 1,
1113 		.dpcm_playback = 1,
1114 		SND_SOC_DAILINK_REG(dl_src),
1115 	},
1116 	[DAI_LINK_DPTX_BE] = {
1117 		.name = "DPTX_BE",
1118 		.ops = &mt8188_dptx_ops,
1119 		.be_hw_params_fixup = mt8188_dptx_hw_params_fixup,
1120 		.no_pcm = 1,
1121 		.dpcm_playback = 1,
1122 		SND_SOC_DAILINK_REG(dptx),
1123 	},
1124 	[DAI_LINK_ETDM1_IN_BE] = {
1125 		.name = "ETDM1_IN_BE",
1126 		.no_pcm = 1,
1127 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1128 			SND_SOC_DAIFMT_NB_NF |
1129 			SND_SOC_DAIFMT_CBP_CFP,
1130 		.dpcm_capture = 1,
1131 		.ignore_suspend = 1,
1132 		SND_SOC_DAILINK_REG(etdm1_in),
1133 	},
1134 	[DAI_LINK_ETDM2_IN_BE] = {
1135 		.name = "ETDM2_IN_BE",
1136 		.no_pcm = 1,
1137 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1138 			SND_SOC_DAIFMT_NB_NF |
1139 			SND_SOC_DAIFMT_CBP_CFP,
1140 		.dpcm_capture = 1,
1141 		SND_SOC_DAILINK_REG(etdm2_in),
1142 	},
1143 	[DAI_LINK_ETDM1_OUT_BE] = {
1144 		.name = "ETDM1_OUT_BE",
1145 		.no_pcm = 1,
1146 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1147 			SND_SOC_DAIFMT_NB_NF |
1148 			SND_SOC_DAIFMT_CBC_CFC,
1149 		.dpcm_playback = 1,
1150 		SND_SOC_DAILINK_REG(etdm1_out),
1151 	},
1152 	[DAI_LINK_ETDM2_OUT_BE] = {
1153 		.name = "ETDM2_OUT_BE",
1154 		.no_pcm = 1,
1155 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1156 			SND_SOC_DAIFMT_NB_NF |
1157 			SND_SOC_DAIFMT_CBC_CFC,
1158 		.dpcm_playback = 1,
1159 		SND_SOC_DAILINK_REG(etdm2_out),
1160 	},
1161 	[DAI_LINK_ETDM3_OUT_BE] = {
1162 		.name = "ETDM3_OUT_BE",
1163 		.no_pcm = 1,
1164 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1165 			SND_SOC_DAIFMT_NB_NF |
1166 			SND_SOC_DAIFMT_CBC_CFC,
1167 		.dpcm_playback = 1,
1168 		SND_SOC_DAILINK_REG(etdm3_out),
1169 	},
1170 	[DAI_LINK_PCM1_BE] = {
1171 		.name = "PCM1_BE",
1172 		.no_pcm = 1,
1173 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1174 			SND_SOC_DAIFMT_NB_NF |
1175 			SND_SOC_DAIFMT_CBC_CFC,
1176 		.dpcm_playback = 1,
1177 		.dpcm_capture = 1,
1178 		SND_SOC_DAILINK_REG(pcm1),
1179 	},
1180 	[DAI_LINK_UL_SRC_BE] = {
1181 		.name = "UL_SRC_BE",
1182 		.no_pcm = 1,
1183 		.dpcm_capture = 1,
1184 		SND_SOC_DAILINK_REG(ul_src),
1185 	},
1186 
1187 	/* SOF BE */
1188 	[DAI_LINK_SOF_DL2_BE] = {
1189 		.name = "AFE_SOF_DL2",
1190 		.no_pcm = 1,
1191 		.dpcm_playback = 1,
1192 		.ops = &mt8188_sof_be_ops,
1193 		SND_SOC_DAILINK_REG(AFE_SOF_DL2),
1194 	},
1195 	[DAI_LINK_SOF_DL3_BE] = {
1196 		.name = "AFE_SOF_DL3",
1197 		.no_pcm = 1,
1198 		.dpcm_playback = 1,
1199 		.ops = &mt8188_sof_be_ops,
1200 		SND_SOC_DAILINK_REG(AFE_SOF_DL3),
1201 	},
1202 	[DAI_LINK_SOF_UL4_BE] = {
1203 		.name = "AFE_SOF_UL4",
1204 		.no_pcm = 1,
1205 		.dpcm_capture = 1,
1206 		.ops = &mt8188_sof_be_ops,
1207 		SND_SOC_DAILINK_REG(AFE_SOF_UL4),
1208 	},
1209 	[DAI_LINK_SOF_UL5_BE] = {
1210 		.name = "AFE_SOF_UL5",
1211 		.no_pcm = 1,
1212 		.dpcm_capture = 1,
1213 		.ops = &mt8188_sof_be_ops,
1214 		SND_SOC_DAILINK_REG(AFE_SOF_UL5),
1215 	},
1216 };
1217 
1218 static void mt8188_fixup_controls(struct snd_soc_card *card)
1219 {
1220 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(card);
1221 	struct mtk_platform_card_data *card_data = soc_card_data->card_data;
1222 	struct snd_kcontrol *kctl;
1223 
1224 	if (card_data->flags & (NAU8825_HS_PRESENT | RT5682S_HS_PRESENT | ES8326_HS_PRESENT)) {
1225 		struct snd_soc_dapm_widget *w, *next_w;
1226 
1227 		for_each_card_widgets_safe(card, w, next_w) {
1228 			if (strcmp(w->name, "Headphone"))
1229 				continue;
1230 
1231 			snd_soc_dapm_free_widget(w);
1232 		}
1233 
1234 		kctl = snd_ctl_find_id_mixer(card->snd_card, "Headphone Switch");
1235 		if (kctl)
1236 			snd_ctl_remove(card->snd_card, kctl);
1237 		else
1238 			dev_warn(card->dev, "Cannot find ctl : Headphone Switch\n");
1239 	}
1240 }
1241 
1242 static struct snd_soc_card mt8188_mt6359_soc_card = {
1243 	.owner = THIS_MODULE,
1244 	.dai_link = mt8188_mt6359_dai_links,
1245 	.num_links = ARRAY_SIZE(mt8188_mt6359_dai_links),
1246 	.dapm_widgets = mt8188_mt6359_widgets,
1247 	.num_dapm_widgets = ARRAY_SIZE(mt8188_mt6359_widgets),
1248 	.dapm_routes = mt8188_mt6359_routes,
1249 	.num_dapm_routes = ARRAY_SIZE(mt8188_mt6359_routes),
1250 	.controls = mt8188_mt6359_controls,
1251 	.num_controls = ARRAY_SIZE(mt8188_mt6359_controls),
1252 	.fixup_controls = mt8188_fixup_controls,
1253 };
1254 
1255 static int mt8188_mt6359_soc_card_probe(struct mtk_soc_card_data *soc_card_data, bool legacy)
1256 {
1257 	struct mtk_platform_card_data *card_data = soc_card_data->card_data;
1258 	struct snd_soc_card *card = soc_card_data->card_data->card;
1259 	struct snd_soc_dai_link *dai_link;
1260 	bool init_mt6359 = false;
1261 	bool init_es8326 = false;
1262 	bool init_nau8825 = false;
1263 	bool init_rt5682s = false;
1264 	bool init_max98390 = false;
1265 	bool init_dumb = false;
1266 	int i;
1267 
1268 	if (legacy)
1269 		return -EINVAL;
1270 
1271 	for_each_card_prelinks(card, i, dai_link) {
1272 		if (strcmp(dai_link->name, "DPTX_BE") == 0) {
1273 			if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
1274 				dai_link->init = mt8188_dptx_codec_init;
1275 		} else if (strcmp(dai_link->name, "ETDM3_OUT_BE") == 0) {
1276 			if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
1277 				dai_link->init = mt8188_hdmi_codec_init;
1278 		} else if (strcmp(dai_link->name, "DL_SRC_BE") == 0 ||
1279 			   strcmp(dai_link->name, "UL_SRC_BE") == 0) {
1280 			if (!init_mt6359) {
1281 				dai_link->init = mt8188_mt6359_init;
1282 				init_mt6359 = true;
1283 			}
1284 		} else if (strcmp(dai_link->name, "ETDM1_OUT_BE") == 0 ||
1285 			   strcmp(dai_link->name, "ETDM2_OUT_BE") == 0 ||
1286 			   strcmp(dai_link->name, "ETDM1_IN_BE") == 0 ||
1287 			   strcmp(dai_link->name, "ETDM2_IN_BE") == 0) {
1288 			if (!strcmp(dai_link->codecs->dai_name, MAX98390_CODEC_DAI)) {
1289 				/*
1290 				 * The TDM protocol settings with fixed 4 slots are defined in
1291 				 * mt8188_max98390_ops. Two amps is I2S mode,
1292 				 * SOC and codec don't require TDM settings.
1293 				 */
1294 				if (!(card_data->flags & MAX98390_TWO_AMP)) {
1295 					dai_link->ops = &mt8188_max98390_ops;
1296 				}
1297 				if (!init_max98390) {
1298 					dai_link->init = mt8188_max98390_codec_init;
1299 					init_max98390 = true;
1300 				}
1301 			} else if (!strcmp(dai_link->codecs->dai_name, NAU8825_CODEC_DAI)) {
1302 				dai_link->ops = &mt8188_nau8825_ops;
1303 				if (!init_nau8825) {
1304 					dai_link->init = mt8188_headset_codec_init;
1305 					dai_link->exit = mt8188_headset_codec_exit;
1306 					init_nau8825 = true;
1307 				}
1308 			} else if (!strcmp(dai_link->codecs->dai_name, RT5682S_CODEC_DAI)) {
1309 				dai_link->ops = &mt8188_rt5682s_i2s_ops;
1310 				if (!init_rt5682s) {
1311 					dai_link->init = mt8188_headset_codec_init;
1312 					dai_link->exit = mt8188_headset_codec_exit;
1313 					init_rt5682s = true;
1314 				}
1315 			} else if (!strcmp(dai_link->codecs->dai_name, ES8326_CODEC_DAI)) {
1316 				dai_link->ops = &mt8188_es8326_ops;
1317 				if (!init_es8326) {
1318 					dai_link->init = mt8188_headset_codec_init;
1319 					dai_link->exit = mt8188_headset_codec_exit;
1320 					init_es8326 = true;
1321 				}
1322 			} else {
1323 				if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai")) {
1324 					if (!init_dumb) {
1325 						dai_link->init = mt8188_dumb_amp_init;
1326 						init_dumb = true;
1327 					}
1328 				}
1329 			}
1330 		}
1331 	}
1332 
1333 	return 0;
1334 }
1335 
1336 static const struct mtk_sof_priv mt8188_sof_priv = {
1337 	.conn_streams = g_sof_conn_streams,
1338 	.num_streams = ARRAY_SIZE(g_sof_conn_streams),
1339 };
1340 
1341 static const struct mtk_soundcard_pdata mt8188_evb_card = {
1342 	.card_name = "mt8188_mt6359",
1343 	.card_data = &(struct mtk_platform_card_data) {
1344 		.card = &mt8188_mt6359_soc_card,
1345 		.num_jacks = MT8188_JACK_MAX,
1346 	},
1347 	.sof_priv = &mt8188_sof_priv,
1348 	.soc_probe = mt8188_mt6359_soc_card_probe,
1349 };
1350 
1351 static const struct mtk_soundcard_pdata mt8188_nau8825_card = {
1352 	.card_name = "mt8188_nau8825",
1353 	.card_data = &(struct mtk_platform_card_data) {
1354 		.card = &mt8188_mt6359_soc_card,
1355 		.num_jacks = MT8188_JACK_MAX,
1356 		.flags = NAU8825_HS_PRESENT
1357 	},
1358 	.sof_priv = &mt8188_sof_priv,
1359 	.soc_probe = mt8188_mt6359_soc_card_probe,
1360 };
1361 
1362 static const struct mtk_soundcard_pdata mt8188_rt5682s_card = {
1363 	.card_name = "mt8188_rt5682s",
1364 	.card_data = &(struct mtk_platform_card_data) {
1365 		.card = &mt8188_mt6359_soc_card,
1366 		.num_jacks = MT8188_JACK_MAX,
1367 		.flags = RT5682S_HS_PRESENT | MAX98390_TWO_AMP
1368 	},
1369 	.sof_priv = &mt8188_sof_priv,
1370 	.soc_probe = mt8188_mt6359_soc_card_probe,
1371 };
1372 
1373 static const struct mtk_soundcard_pdata mt8188_es8326_card = {
1374 	.card_name = "mt8188_es8326",
1375 	.card_data = &(struct mtk_platform_card_data) {
1376 		.card = &mt8188_mt6359_soc_card,
1377 		.num_jacks = MT8188_JACK_MAX,
1378 		.flags = ES8326_HS_PRESENT | MAX98390_TWO_AMP
1379 	},
1380 	.sof_priv = &mt8188_sof_priv,
1381 	.soc_probe = mt8188_mt6359_soc_card_probe,
1382 };
1383 
1384 static const struct of_device_id mt8188_mt6359_dt_match[] = {
1385 	{ .compatible = "mediatek,mt8188-mt6359-evb", .data = &mt8188_evb_card, },
1386 	{ .compatible = "mediatek,mt8188-nau8825", .data = &mt8188_nau8825_card, },
1387 	{ .compatible = "mediatek,mt8188-rt5682s", .data = &mt8188_rt5682s_card, },
1388 	{ .compatible = "mediatek,mt8188-es8326", .data = &mt8188_es8326_card, },
1389 	{ /* sentinel */ },
1390 };
1391 MODULE_DEVICE_TABLE(of, mt8188_mt6359_dt_match);
1392 
1393 static struct platform_driver mt8188_mt6359_driver = {
1394 	.driver = {
1395 		.name = "mt8188_mt6359",
1396 		.of_match_table = mt8188_mt6359_dt_match,
1397 		.pm = &snd_soc_pm_ops,
1398 	},
1399 	.probe = mtk_soundcard_common_probe,
1400 };
1401 
1402 module_platform_driver(mt8188_mt6359_driver);
1403 
1404 /* Module information */
1405 MODULE_DESCRIPTION("MT8188-MT6359 ALSA SoC machine driver");
1406 MODULE_AUTHOR("Trevor Wu <trevor.wu@mediatek.com>");
1407 MODULE_LICENSE("GPL");
1408 MODULE_ALIAS("mt8188 mt6359 soc card");
1409