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