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