xref: /linux/sound/soc/mediatek/mt8188/mt8188-mt6359.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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 	struct mtk_platform_card_data *card_data = soc_card_data->card_data;
738 	int ret;
739 
740 	ret = snd_soc_dapm_new_controls(&card->dapm, mt8188_nau8825_widgets,
741 					ARRAY_SIZE(mt8188_nau8825_widgets));
742 	if (ret) {
743 		dev_err(rtd->dev, "unable to add nau8825 card widget, ret %d\n", ret);
744 		return ret;
745 	}
746 
747 	ret = snd_soc_add_card_controls(card, mt8188_nau8825_controls,
748 					ARRAY_SIZE(mt8188_nau8825_controls));
749 	if (ret) {
750 		dev_err(rtd->dev, "unable to add nau8825 card controls, ret %d\n", ret);
751 		return ret;
752 	}
753 
754 	ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
755 					 SND_JACK_HEADSET | SND_JACK_BTN_0 |
756 					 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
757 					 SND_JACK_BTN_3,
758 					 jack,
759 					 nau8825_jack_pins,
760 					 ARRAY_SIZE(nau8825_jack_pins));
761 	if (ret) {
762 		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
763 		return ret;
764 	}
765 
766 	if (card_data->flags & ES8326_HS_PRESENT) {
767 		snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
768 		snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
769 		snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
770 		snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
771 	} else {
772 		snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
773 		snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
774 		snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
775 		snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
776 	}
777 
778 	ret = snd_soc_component_set_jack(component, jack, NULL);
779 
780 	if (ret) {
781 		dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
782 		return ret;
783 	}
784 
785 	return 0;
786 };
787 
788 static void mt8188_headset_codec_exit(struct snd_soc_pcm_runtime *rtd)
789 {
790 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
791 
792 	snd_soc_component_set_jack(component, NULL, NULL);
793 }
794 
795 
796 static int mt8188_nau8825_hw_params(struct snd_pcm_substream *substream,
797 				    struct snd_pcm_hw_params *params)
798 {
799 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
800 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
801 	unsigned int rate = params_rate(params);
802 	unsigned int bit_width = params_width(params);
803 	int clk_freq, ret;
804 
805 	clk_freq = rate * 2 * bit_width;
806 
807 	/* Configure clock for codec */
808 	ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_BLK, 0,
809 				     SND_SOC_CLOCK_IN);
810 	if (ret < 0) {
811 		dev_err(codec_dai->dev, "can't set BCLK clock %d\n", ret);
812 		return ret;
813 	}
814 
815 	/* Configure pll for codec */
816 	ret = snd_soc_dai_set_pll(codec_dai, 0, 0, clk_freq,
817 				  params_rate(params) * 256);
818 	if (ret < 0) {
819 		dev_err(codec_dai->dev, "can't set BCLK: %d\n", ret);
820 		return ret;
821 	}
822 
823 	return 0;
824 }
825 
826 static const struct snd_soc_ops mt8188_nau8825_ops = {
827 	.hw_params = mt8188_nau8825_hw_params,
828 };
829 
830 static int mt8188_rt5682s_i2s_hw_params(struct snd_pcm_substream *substream,
831 					struct snd_pcm_hw_params *params)
832 {
833 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
834 	struct snd_soc_card *card = rtd->card;
835 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
836 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
837 	unsigned int rate = params_rate(params);
838 	int bitwidth;
839 	int ret;
840 
841 	bitwidth = snd_pcm_format_width(params_format(params));
842 	if (bitwidth < 0) {
843 		dev_err(card->dev, "invalid bit width: %d\n", bitwidth);
844 		return bitwidth;
845 	}
846 
847 	ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x00, 0x0, 0x2, bitwidth);
848 	if (ret) {
849 		dev_err(card->dev, "failed to set tdm slot\n");
850 		return ret;
851 	}
852 
853 	ret = snd_soc_dai_set_pll(codec_dai, RT5682_PLL1, RT5682_PLL1_S_BCLK1,
854 				  rate * 32, rate * 512);
855 	if (ret) {
856 		dev_err(card->dev, "failed to set pll\n");
857 		return ret;
858 	}
859 
860 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1,
861 				     rate * 512, SND_SOC_CLOCK_IN);
862 	if (ret) {
863 		dev_err(card->dev, "failed to set sysclk\n");
864 		return ret;
865 	}
866 
867 	return snd_soc_dai_set_sysclk(cpu_dai, 0, rate * 128,
868 				      SND_SOC_CLOCK_OUT);
869 }
870 
871 static const struct snd_soc_ops mt8188_rt5682s_i2s_ops = {
872 	.hw_params = mt8188_rt5682s_i2s_hw_params,
873 };
874 
875 static int mt8188_sof_be_hw_params(struct snd_pcm_substream *substream,
876 				   struct snd_pcm_hw_params *params)
877 {
878 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
879 	struct snd_soc_component *cmpnt_afe = NULL;
880 	struct snd_soc_pcm_runtime *runtime;
881 
882 	/* find afe component */
883 	for_each_card_rtds(rtd->card, runtime) {
884 		cmpnt_afe = snd_soc_rtdcom_lookup(runtime, AFE_PCM_NAME);
885 		if (cmpnt_afe)
886 			break;
887 	}
888 
889 	if (cmpnt_afe && !pm_runtime_active(cmpnt_afe->dev)) {
890 		dev_err(rtd->dev, "afe pm runtime is not active!!\n");
891 		return -EINVAL;
892 	}
893 
894 	return 0;
895 }
896 
897 static const struct snd_soc_ops mt8188_sof_be_ops = {
898 	.hw_params = mt8188_sof_be_hw_params,
899 };
900 
901 static int mt8188_es8326_hw_params(struct snd_pcm_substream *substream,
902 				 struct snd_pcm_hw_params *params)
903 {
904 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
905 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
906 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
907 	unsigned int rate = params_rate(params);
908 	int ret;
909 
910 	/* Configure MCLK for codec */
911 	ret = snd_soc_dai_set_sysclk(codec_dai, 0, rate * 256, SND_SOC_CLOCK_IN);
912 	if (ret < 0) {
913 		dev_err(codec_dai->dev, "can't set MCLK %d\n", ret);
914 		return ret;
915 	}
916 
917 	/* Configure MCLK for cpu */
918 	return snd_soc_dai_set_sysclk(cpu_dai, 0, rate * 256, SND_SOC_CLOCK_OUT);
919 }
920 
921 static const struct snd_soc_ops mt8188_es8326_ops = {
922 	.hw_params = mt8188_es8326_hw_params,
923 };
924 
925 static struct snd_soc_dai_link mt8188_mt6359_dai_links[] = {
926 	/* FE */
927 	[DAI_LINK_DL2_FE] = {
928 		.name = "DL2_FE",
929 		.stream_name = "DL2 Playback",
930 		.trigger = {
931 			SND_SOC_DPCM_TRIGGER_POST,
932 			SND_SOC_DPCM_TRIGGER_POST,
933 		},
934 		.dynamic = 1,
935 		.dpcm_playback = 1,
936 		.dpcm_merged_chan = 1,
937 		.dpcm_merged_rate = 1,
938 		.dpcm_merged_format = 1,
939 		SND_SOC_DAILINK_REG(playback2),
940 	},
941 	[DAI_LINK_DL3_FE] = {
942 		.name = "DL3_FE",
943 		.stream_name = "DL3 Playback",
944 		.trigger = {
945 			SND_SOC_DPCM_TRIGGER_POST,
946 			SND_SOC_DPCM_TRIGGER_POST,
947 		},
948 		.dynamic = 1,
949 		.dpcm_playback = 1,
950 		.dpcm_merged_chan = 1,
951 		.dpcm_merged_rate = 1,
952 		.dpcm_merged_format = 1,
953 		SND_SOC_DAILINK_REG(playback3),
954 	},
955 	[DAI_LINK_DL6_FE] = {
956 		.name = "DL6_FE",
957 		.stream_name = "DL6 Playback",
958 		.trigger = {
959 			SND_SOC_DPCM_TRIGGER_POST,
960 			SND_SOC_DPCM_TRIGGER_POST,
961 		},
962 		.dynamic = 1,
963 		.dpcm_playback = 1,
964 		.dpcm_merged_chan = 1,
965 		.dpcm_merged_rate = 1,
966 		.dpcm_merged_format = 1,
967 		SND_SOC_DAILINK_REG(playback6),
968 	},
969 	[DAI_LINK_DL7_FE] = {
970 		.name = "DL7_FE",
971 		.stream_name = "DL7 Playback",
972 		.trigger = {
973 			SND_SOC_DPCM_TRIGGER_PRE,
974 			SND_SOC_DPCM_TRIGGER_PRE,
975 		},
976 		.dynamic = 1,
977 		.dpcm_playback = 1,
978 		SND_SOC_DAILINK_REG(playback7),
979 	},
980 	[DAI_LINK_DL8_FE] = {
981 		.name = "DL8_FE",
982 		.stream_name = "DL8 Playback",
983 		.trigger = {
984 			SND_SOC_DPCM_TRIGGER_POST,
985 			SND_SOC_DPCM_TRIGGER_POST,
986 		},
987 		.dynamic = 1,
988 		.dpcm_playback = 1,
989 		SND_SOC_DAILINK_REG(playback8),
990 	},
991 	[DAI_LINK_DL10_FE] = {
992 		.name = "DL10_FE",
993 		.stream_name = "DL10 Playback",
994 		.trigger = {
995 			SND_SOC_DPCM_TRIGGER_POST,
996 			SND_SOC_DPCM_TRIGGER_POST,
997 		},
998 		.dynamic = 1,
999 		.dpcm_playback = 1,
1000 		SND_SOC_DAILINK_REG(playback10),
1001 	},
1002 	[DAI_LINK_DL11_FE] = {
1003 		.name = "DL11_FE",
1004 		.stream_name = "DL11 Playback",
1005 		.trigger = {
1006 			SND_SOC_DPCM_TRIGGER_POST,
1007 			SND_SOC_DPCM_TRIGGER_POST,
1008 		},
1009 		.dynamic = 1,
1010 		.dpcm_playback = 1,
1011 		SND_SOC_DAILINK_REG(playback11),
1012 	},
1013 	[DAI_LINK_UL1_FE] = {
1014 		.name = "UL1_FE",
1015 		.stream_name = "UL1 Capture",
1016 		.trigger = {
1017 			SND_SOC_DPCM_TRIGGER_PRE,
1018 			SND_SOC_DPCM_TRIGGER_PRE,
1019 		},
1020 		.dynamic = 1,
1021 		.dpcm_capture = 1,
1022 		SND_SOC_DAILINK_REG(capture1),
1023 	},
1024 	[DAI_LINK_UL2_FE] = {
1025 		.name = "UL2_FE",
1026 		.stream_name = "UL2 Capture",
1027 		.trigger = {
1028 			SND_SOC_DPCM_TRIGGER_POST,
1029 			SND_SOC_DPCM_TRIGGER_POST,
1030 		},
1031 		.dynamic = 1,
1032 		.dpcm_capture = 1,
1033 		SND_SOC_DAILINK_REG(capture2),
1034 	},
1035 	[DAI_LINK_UL3_FE] = {
1036 		.name = "UL3_FE",
1037 		.stream_name = "UL3 Capture",
1038 		.trigger = {
1039 			SND_SOC_DPCM_TRIGGER_POST,
1040 			SND_SOC_DPCM_TRIGGER_POST,
1041 		},
1042 		.dynamic = 1,
1043 		.dpcm_capture = 1,
1044 		SND_SOC_DAILINK_REG(capture3),
1045 	},
1046 	[DAI_LINK_UL4_FE] = {
1047 		.name = "UL4_FE",
1048 		.stream_name = "UL4 Capture",
1049 		.trigger = {
1050 			SND_SOC_DPCM_TRIGGER_POST,
1051 			SND_SOC_DPCM_TRIGGER_POST,
1052 		},
1053 		.dynamic = 1,
1054 		.dpcm_capture = 1,
1055 		.dpcm_merged_chan = 1,
1056 		.dpcm_merged_rate = 1,
1057 		.dpcm_merged_format = 1,
1058 		SND_SOC_DAILINK_REG(capture4),
1059 	},
1060 	[DAI_LINK_UL5_FE] = {
1061 		.name = "UL5_FE",
1062 		.stream_name = "UL5 Capture",
1063 		.trigger = {
1064 			SND_SOC_DPCM_TRIGGER_POST,
1065 			SND_SOC_DPCM_TRIGGER_POST,
1066 		},
1067 		.dynamic = 1,
1068 		.dpcm_capture = 1,
1069 		.dpcm_merged_chan = 1,
1070 		.dpcm_merged_rate = 1,
1071 		.dpcm_merged_format = 1,
1072 		SND_SOC_DAILINK_REG(capture5),
1073 	},
1074 	[DAI_LINK_UL6_FE] = {
1075 		.name = "UL6_FE",
1076 		.stream_name = "UL6 Capture",
1077 		.trigger = {
1078 			SND_SOC_DPCM_TRIGGER_PRE,
1079 			SND_SOC_DPCM_TRIGGER_PRE,
1080 		},
1081 		.dynamic = 1,
1082 		.dpcm_capture = 1,
1083 		SND_SOC_DAILINK_REG(capture6),
1084 	},
1085 	[DAI_LINK_UL8_FE] = {
1086 		.name = "UL8_FE",
1087 		.stream_name = "UL8 Capture",
1088 		.trigger = {
1089 			SND_SOC_DPCM_TRIGGER_POST,
1090 			SND_SOC_DPCM_TRIGGER_POST,
1091 		},
1092 		.dynamic = 1,
1093 		.dpcm_capture = 1,
1094 		SND_SOC_DAILINK_REG(capture8),
1095 	},
1096 	[DAI_LINK_UL9_FE] = {
1097 		.name = "UL9_FE",
1098 		.stream_name = "UL9 Capture",
1099 		.trigger = {
1100 			SND_SOC_DPCM_TRIGGER_POST,
1101 			SND_SOC_DPCM_TRIGGER_POST,
1102 		},
1103 		.dynamic = 1,
1104 		.dpcm_capture = 1,
1105 		SND_SOC_DAILINK_REG(capture9),
1106 	},
1107 	[DAI_LINK_UL10_FE] = {
1108 		.name = "UL10_FE",
1109 		.stream_name = "UL10 Capture",
1110 		.trigger = {
1111 			SND_SOC_DPCM_TRIGGER_POST,
1112 			SND_SOC_DPCM_TRIGGER_POST,
1113 		},
1114 		.dynamic = 1,
1115 		.dpcm_capture = 1,
1116 		SND_SOC_DAILINK_REG(capture10),
1117 	},
1118 	/* BE */
1119 	[DAI_LINK_DL_SRC_BE] = {
1120 		.name = "DL_SRC_BE",
1121 		.no_pcm = 1,
1122 		.dpcm_playback = 1,
1123 		SND_SOC_DAILINK_REG(dl_src),
1124 	},
1125 	[DAI_LINK_DPTX_BE] = {
1126 		.name = "DPTX_BE",
1127 		.ops = &mt8188_dptx_ops,
1128 		.be_hw_params_fixup = mt8188_dptx_hw_params_fixup,
1129 		.no_pcm = 1,
1130 		.dpcm_playback = 1,
1131 		SND_SOC_DAILINK_REG(dptx),
1132 	},
1133 	[DAI_LINK_ETDM1_IN_BE] = {
1134 		.name = "ETDM1_IN_BE",
1135 		.no_pcm = 1,
1136 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1137 			SND_SOC_DAIFMT_NB_NF |
1138 			SND_SOC_DAIFMT_CBP_CFP,
1139 		.dpcm_capture = 1,
1140 		.ignore_suspend = 1,
1141 		SND_SOC_DAILINK_REG(etdm1_in),
1142 	},
1143 	[DAI_LINK_ETDM2_IN_BE] = {
1144 		.name = "ETDM2_IN_BE",
1145 		.no_pcm = 1,
1146 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1147 			SND_SOC_DAIFMT_NB_NF |
1148 			SND_SOC_DAIFMT_CBP_CFP,
1149 		.dpcm_capture = 1,
1150 		SND_SOC_DAILINK_REG(etdm2_in),
1151 	},
1152 	[DAI_LINK_ETDM1_OUT_BE] = {
1153 		.name = "ETDM1_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(etdm1_out),
1160 	},
1161 	[DAI_LINK_ETDM2_OUT_BE] = {
1162 		.name = "ETDM2_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(etdm2_out),
1169 	},
1170 	[DAI_LINK_ETDM3_OUT_BE] = {
1171 		.name = "ETDM3_OUT_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 		SND_SOC_DAILINK_REG(etdm3_out),
1178 	},
1179 	[DAI_LINK_PCM1_BE] = {
1180 		.name = "PCM1_BE",
1181 		.no_pcm = 1,
1182 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1183 			SND_SOC_DAIFMT_NB_NF |
1184 			SND_SOC_DAIFMT_CBC_CFC,
1185 		.dpcm_playback = 1,
1186 		.dpcm_capture = 1,
1187 		SND_SOC_DAILINK_REG(pcm1),
1188 	},
1189 	[DAI_LINK_UL_SRC_BE] = {
1190 		.name = "UL_SRC_BE",
1191 		.no_pcm = 1,
1192 		.dpcm_capture = 1,
1193 		SND_SOC_DAILINK_REG(ul_src),
1194 	},
1195 
1196 	/* SOF BE */
1197 	[DAI_LINK_SOF_DL2_BE] = {
1198 		.name = "AFE_SOF_DL2",
1199 		.no_pcm = 1,
1200 		.dpcm_playback = 1,
1201 		.ops = &mt8188_sof_be_ops,
1202 		SND_SOC_DAILINK_REG(AFE_SOF_DL2),
1203 	},
1204 	[DAI_LINK_SOF_DL3_BE] = {
1205 		.name = "AFE_SOF_DL3",
1206 		.no_pcm = 1,
1207 		.dpcm_playback = 1,
1208 		.ops = &mt8188_sof_be_ops,
1209 		SND_SOC_DAILINK_REG(AFE_SOF_DL3),
1210 	},
1211 	[DAI_LINK_SOF_UL4_BE] = {
1212 		.name = "AFE_SOF_UL4",
1213 		.no_pcm = 1,
1214 		.dpcm_capture = 1,
1215 		.ops = &mt8188_sof_be_ops,
1216 		SND_SOC_DAILINK_REG(AFE_SOF_UL4),
1217 	},
1218 	[DAI_LINK_SOF_UL5_BE] = {
1219 		.name = "AFE_SOF_UL5",
1220 		.no_pcm = 1,
1221 		.dpcm_capture = 1,
1222 		.ops = &mt8188_sof_be_ops,
1223 		SND_SOC_DAILINK_REG(AFE_SOF_UL5),
1224 	},
1225 };
1226 
1227 static void mt8188_fixup_controls(struct snd_soc_card *card)
1228 {
1229 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(card);
1230 	struct mtk_platform_card_data *card_data = soc_card_data->card_data;
1231 	struct snd_kcontrol *kctl;
1232 
1233 	if (card_data->flags & (NAU8825_HS_PRESENT | RT5682S_HS_PRESENT | ES8326_HS_PRESENT)) {
1234 		struct snd_soc_dapm_widget *w, *next_w;
1235 
1236 		for_each_card_widgets_safe(card, w, next_w) {
1237 			if (strcmp(w->name, "Headphone"))
1238 				continue;
1239 
1240 			snd_soc_dapm_free_widget(w);
1241 		}
1242 
1243 		kctl = snd_ctl_find_id_mixer(card->snd_card, "Headphone Switch");
1244 		if (kctl)
1245 			snd_ctl_remove(card->snd_card, kctl);
1246 		else
1247 			dev_warn(card->dev, "Cannot find ctl : Headphone Switch\n");
1248 	}
1249 }
1250 
1251 static struct snd_soc_card mt8188_mt6359_soc_card = {
1252 	.owner = THIS_MODULE,
1253 	.dai_link = mt8188_mt6359_dai_links,
1254 	.num_links = ARRAY_SIZE(mt8188_mt6359_dai_links),
1255 	.dapm_widgets = mt8188_mt6359_widgets,
1256 	.num_dapm_widgets = ARRAY_SIZE(mt8188_mt6359_widgets),
1257 	.dapm_routes = mt8188_mt6359_routes,
1258 	.num_dapm_routes = ARRAY_SIZE(mt8188_mt6359_routes),
1259 	.controls = mt8188_mt6359_controls,
1260 	.num_controls = ARRAY_SIZE(mt8188_mt6359_controls),
1261 	.fixup_controls = mt8188_fixup_controls,
1262 };
1263 
1264 static int mt8188_mt6359_soc_card_probe(struct mtk_soc_card_data *soc_card_data, bool legacy)
1265 {
1266 	struct mtk_platform_card_data *card_data = soc_card_data->card_data;
1267 	struct snd_soc_card *card = soc_card_data->card_data->card;
1268 	struct snd_soc_dai_link *dai_link;
1269 	bool init_mt6359 = false;
1270 	bool init_es8326 = false;
1271 	bool init_nau8825 = false;
1272 	bool init_rt5682s = false;
1273 	bool init_max98390 = false;
1274 	bool init_dumb = false;
1275 	int i;
1276 
1277 	if (legacy)
1278 		return -EINVAL;
1279 
1280 	for_each_card_prelinks(card, i, dai_link) {
1281 		if (strcmp(dai_link->name, "DPTX_BE") == 0) {
1282 			if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
1283 				dai_link->init = mt8188_dptx_codec_init;
1284 		} else if (strcmp(dai_link->name, "ETDM3_OUT_BE") == 0) {
1285 			if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
1286 				dai_link->init = mt8188_hdmi_codec_init;
1287 		} else if (strcmp(dai_link->name, "DL_SRC_BE") == 0 ||
1288 			   strcmp(dai_link->name, "UL_SRC_BE") == 0) {
1289 			if (!init_mt6359) {
1290 				dai_link->init = mt8188_mt6359_init;
1291 				init_mt6359 = true;
1292 			}
1293 		} else if (strcmp(dai_link->name, "ETDM1_OUT_BE") == 0 ||
1294 			   strcmp(dai_link->name, "ETDM2_OUT_BE") == 0 ||
1295 			   strcmp(dai_link->name, "ETDM1_IN_BE") == 0 ||
1296 			   strcmp(dai_link->name, "ETDM2_IN_BE") == 0) {
1297 			if (!strcmp(dai_link->codecs->dai_name, MAX98390_CODEC_DAI)) {
1298 				/*
1299 				 * The TDM protocol settings with fixed 4 slots are defined in
1300 				 * mt8188_max98390_ops. Two amps is I2S mode,
1301 				 * SOC and codec don't require TDM settings.
1302 				 */
1303 				if (!(card_data->flags & MAX98390_TWO_AMP)) {
1304 					dai_link->ops = &mt8188_max98390_ops;
1305 				}
1306 				if (!init_max98390) {
1307 					dai_link->init = mt8188_max98390_codec_init;
1308 					init_max98390 = true;
1309 				}
1310 			} else if (!strcmp(dai_link->codecs->dai_name, NAU8825_CODEC_DAI)) {
1311 				dai_link->ops = &mt8188_nau8825_ops;
1312 				if (!init_nau8825) {
1313 					dai_link->init = mt8188_headset_codec_init;
1314 					dai_link->exit = mt8188_headset_codec_exit;
1315 					init_nau8825 = true;
1316 				}
1317 			} else if (!strcmp(dai_link->codecs->dai_name, RT5682S_CODEC_DAI)) {
1318 				dai_link->ops = &mt8188_rt5682s_i2s_ops;
1319 				if (!init_rt5682s) {
1320 					dai_link->init = mt8188_headset_codec_init;
1321 					dai_link->exit = mt8188_headset_codec_exit;
1322 					init_rt5682s = true;
1323 				}
1324 			} else if (!strcmp(dai_link->codecs->dai_name, ES8326_CODEC_DAI)) {
1325 				dai_link->ops = &mt8188_es8326_ops;
1326 				if (!init_es8326) {
1327 					dai_link->init = mt8188_headset_codec_init;
1328 					dai_link->exit = mt8188_headset_codec_exit;
1329 					init_es8326 = true;
1330 				}
1331 			} else {
1332 				if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai")) {
1333 					if (!init_dumb) {
1334 						dai_link->init = mt8188_dumb_amp_init;
1335 						init_dumb = true;
1336 					}
1337 				}
1338 			}
1339 		}
1340 	}
1341 
1342 	return 0;
1343 }
1344 
1345 static const struct mtk_sof_priv mt8188_sof_priv = {
1346 	.conn_streams = g_sof_conn_streams,
1347 	.num_streams = ARRAY_SIZE(g_sof_conn_streams),
1348 };
1349 
1350 static const struct mtk_soundcard_pdata mt8188_evb_card = {
1351 	.card_name = "mt8188_mt6359",
1352 	.card_data = &(struct mtk_platform_card_data) {
1353 		.card = &mt8188_mt6359_soc_card,
1354 		.num_jacks = MT8188_JACK_MAX,
1355 	},
1356 	.sof_priv = &mt8188_sof_priv,
1357 	.soc_probe = mt8188_mt6359_soc_card_probe,
1358 };
1359 
1360 static const struct mtk_soundcard_pdata mt8188_nau8825_card = {
1361 	.card_name = "mt8188_nau8825",
1362 	.card_data = &(struct mtk_platform_card_data) {
1363 		.card = &mt8188_mt6359_soc_card,
1364 		.num_jacks = MT8188_JACK_MAX,
1365 		.flags = NAU8825_HS_PRESENT
1366 	},
1367 	.sof_priv = &mt8188_sof_priv,
1368 	.soc_probe = mt8188_mt6359_soc_card_probe,
1369 };
1370 
1371 static const struct mtk_soundcard_pdata mt8188_rt5682s_card = {
1372 	.card_name = "mt8188_rt5682s",
1373 	.card_data = &(struct mtk_platform_card_data) {
1374 		.card = &mt8188_mt6359_soc_card,
1375 		.num_jacks = MT8188_JACK_MAX,
1376 		.flags = RT5682S_HS_PRESENT | MAX98390_TWO_AMP
1377 	},
1378 	.sof_priv = &mt8188_sof_priv,
1379 	.soc_probe = mt8188_mt6359_soc_card_probe,
1380 };
1381 
1382 static const struct mtk_soundcard_pdata mt8188_es8326_card = {
1383 	.card_name = "mt8188_es8326",
1384 	.card_data = &(struct mtk_platform_card_data) {
1385 		.card = &mt8188_mt6359_soc_card,
1386 		.num_jacks = MT8188_JACK_MAX,
1387 		.flags = ES8326_HS_PRESENT | MAX98390_TWO_AMP
1388 	},
1389 	.sof_priv = &mt8188_sof_priv,
1390 	.soc_probe = mt8188_mt6359_soc_card_probe,
1391 };
1392 
1393 static const struct of_device_id mt8188_mt6359_dt_match[] = {
1394 	{ .compatible = "mediatek,mt8188-mt6359-evb", .data = &mt8188_evb_card, },
1395 	{ .compatible = "mediatek,mt8188-nau8825", .data = &mt8188_nau8825_card, },
1396 	{ .compatible = "mediatek,mt8188-rt5682s", .data = &mt8188_rt5682s_card, },
1397 	{ .compatible = "mediatek,mt8188-es8326", .data = &mt8188_es8326_card, },
1398 	{ /* sentinel */ },
1399 };
1400 MODULE_DEVICE_TABLE(of, mt8188_mt6359_dt_match);
1401 
1402 static struct platform_driver mt8188_mt6359_driver = {
1403 	.driver = {
1404 		.name = "mt8188_mt6359",
1405 		.of_match_table = mt8188_mt6359_dt_match,
1406 		.pm = &snd_soc_pm_ops,
1407 	},
1408 	.probe = mtk_soundcard_common_probe,
1409 };
1410 
1411 module_platform_driver(mt8188_mt6359_driver);
1412 
1413 /* Module information */
1414 MODULE_DESCRIPTION("MT8188-MT6359 ALSA SoC machine driver");
1415 MODULE_AUTHOR("Trevor Wu <trevor.wu@mediatek.com>");
1416 MODULE_LICENSE("GPL");
1417 MODULE_ALIAS("mt8188 mt6359 soc card");
1418