1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mt8195-mt6359.c  --
4  *	MT8195-MT6359 ALSA SoC machine driver code
5  *
6  * Copyright (c) 2022 MediaTek Inc.
7  * Author: Trevor Wu <trevor.wu@mediatek.com>
8  *	   YC Hung <yc.hung@mediatek.com>
9  */
10 
11 #include <linux/input.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/pm_runtime.h>
15 #include <sound/jack.h>
16 #include <sound/pcm_params.h>
17 #include <sound/rt5682.h>
18 #include <sound/soc.h>
19 #include "../../codecs/mt6359.h"
20 #include "../../codecs/rt1011.h"
21 #include "../../codecs/rt5682.h"
22 #include "../common/mtk-afe-platform-driver.h"
23 #include "../common/mtk-dsp-sof-common.h"
24 #include "../common/mtk-soc-card.h"
25 #include "mt8195-afe-clk.h"
26 #include "mt8195-afe-common.h"
27 
28 #define RT1011_SPEAKER_AMP_PRESENT		BIT(0)
29 #define RT1019_SPEAKER_AMP_PRESENT		BIT(1)
30 #define MAX98390_SPEAKER_AMP_PRESENT		BIT(2)
31 
32 #define RT1011_CODEC_DAI	"rt1011-aif"
33 #define RT1011_DEV0_NAME	"rt1011.2-0038"
34 #define RT1011_DEV1_NAME	"rt1011.2-0039"
35 
36 #define RT1019_CODEC_DAI	"HiFi"
37 #define RT1019_DEV0_NAME	"rt1019p"
38 
39 #define MAX98390_CODEC_DAI	"max98390-aif1"
40 #define MAX98390_DEV0_NAME	"max98390.2-0038" /* right */
41 #define MAX98390_DEV1_NAME	"max98390.2-0039" /* left */
42 
43 #define RT5682_CODEC_DAI	"rt5682-aif1"
44 #define RT5682_DEV0_NAME	"rt5682.2-001a"
45 
46 #define RT5682S_CODEC_DAI	"rt5682s-aif1"
47 #define RT5682S_DEV0_NAME	"rt5682s.2-001a"
48 
49 #define SOF_DMA_DL2 "SOF_DMA_DL2"
50 #define SOF_DMA_DL3 "SOF_DMA_DL3"
51 #define SOF_DMA_UL4 "SOF_DMA_UL4"
52 #define SOF_DMA_UL5 "SOF_DMA_UL5"
53 
54 struct mt8195_card_data {
55 	const char *name;
56 	unsigned long quirk;
57 };
58 
59 struct mt8195_mt6359_priv {
60 	struct snd_soc_jack headset_jack;
61 	struct snd_soc_jack dp_jack;
62 	struct snd_soc_jack hdmi_jack;
63 	struct clk *i2so1_mclk;
64 };
65 
66 static const struct snd_soc_dapm_widget mt8195_mt6359_widgets[] = {
67 	SND_SOC_DAPM_HP("Headphone", NULL),
68 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
69 	SND_SOC_DAPM_MIXER(SOF_DMA_DL2, SND_SOC_NOPM, 0, 0, NULL, 0),
70 	SND_SOC_DAPM_MIXER(SOF_DMA_DL3, SND_SOC_NOPM, 0, 0, NULL, 0),
71 	SND_SOC_DAPM_MIXER(SOF_DMA_UL4, SND_SOC_NOPM, 0, 0, NULL, 0),
72 	SND_SOC_DAPM_MIXER(SOF_DMA_UL5, SND_SOC_NOPM, 0, 0, NULL, 0),
73 };
74 
75 static const struct snd_soc_dapm_route mt8195_mt6359_routes[] = {
76 	/* headset */
77 	{ "Headphone", NULL, "HPOL" },
78 	{ "Headphone", NULL, "HPOR" },
79 	{ "IN1P", NULL, "Headset Mic" },
80 	/* SOF Uplink */
81 	{SOF_DMA_UL4, NULL, "O034"},
82 	{SOF_DMA_UL4, NULL, "O035"},
83 	{SOF_DMA_UL5, NULL, "O036"},
84 	{SOF_DMA_UL5, NULL, "O037"},
85 	/* SOF Downlink */
86 	{"I070", NULL, SOF_DMA_DL2},
87 	{"I071", NULL, SOF_DMA_DL2},
88 	{"I020", NULL, SOF_DMA_DL3},
89 	{"I021", NULL, SOF_DMA_DL3},
90 };
91 
92 static const struct snd_kcontrol_new mt8195_mt6359_controls[] = {
93 	SOC_DAPM_PIN_SWITCH("Headphone"),
94 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
95 };
96 
97 static const struct snd_soc_dapm_widget mt8195_dual_speaker_widgets[] = {
98 	SND_SOC_DAPM_SPK("Left Spk", NULL),
99 	SND_SOC_DAPM_SPK("Right Spk", NULL),
100 };
101 
102 static const struct snd_kcontrol_new mt8195_dual_speaker_controls[] = {
103 	SOC_DAPM_PIN_SWITCH("Left Spk"),
104 	SOC_DAPM_PIN_SWITCH("Right Spk"),
105 };
106 
107 static const struct snd_soc_dapm_widget mt8195_speaker_widgets[] = {
108 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
109 };
110 
111 static const struct snd_kcontrol_new mt8195_speaker_controls[] = {
112 	SOC_DAPM_PIN_SWITCH("Ext Spk"),
113 };
114 
115 static const struct snd_soc_dapm_route mt8195_rt1011_routes[] = {
116 	{ "Left Spk", NULL, "Left SPO" },
117 	{ "Right Spk", NULL, "Right SPO" },
118 };
119 
120 static const struct snd_soc_dapm_route mt8195_rt1019_routes[] = {
121 	{ "Ext Spk", NULL, "Speaker" },
122 };
123 
124 static const struct snd_soc_dapm_route mt8195_max98390_routes[] = {
125 	{ "Left Spk", NULL, "Left BE_OUT" },
126 	{ "Right Spk", NULL, "Right BE_OUT" },
127 };
128 
129 #define CKSYS_AUD_TOP_CFG 0x032c
130 #define CKSYS_AUD_TOP_MON 0x0330
131 
132 static int mt8195_mt6359_mtkaif_calibration(struct snd_soc_pcm_runtime *rtd)
133 {
134 	struct snd_soc_component *cmpnt_afe =
135 		snd_soc_rtdcom_lookup(rtd, AFE_PCM_NAME);
136 	struct snd_soc_component *cmpnt_codec =
137 		asoc_rtd_to_codec(rtd, 0)->component;
138 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt_afe);
139 	struct mt8195_afe_private *afe_priv = afe->platform_priv;
140 	struct mtkaif_param *param = &afe_priv->mtkaif_params;
141 	int chosen_phase_1, chosen_phase_2, chosen_phase_3;
142 	int prev_cycle_1, prev_cycle_2, prev_cycle_3;
143 	int test_done_1, test_done_2, test_done_3;
144 	int cycle_1, cycle_2, cycle_3;
145 	int mtkaif_chosen_phase[MT8195_MTKAIF_MISO_NUM];
146 	int mtkaif_phase_cycle[MT8195_MTKAIF_MISO_NUM];
147 	int mtkaif_calibration_num_phase;
148 	bool mtkaif_calibration_ok;
149 	unsigned int monitor;
150 	int counter;
151 	int phase;
152 	int i;
153 
154 	dev_dbg(afe->dev, "%s(), start\n", __func__);
155 
156 	param->mtkaif_calibration_ok = false;
157 	for (i = 0; i < MT8195_MTKAIF_MISO_NUM; i++) {
158 		param->mtkaif_chosen_phase[i] = -1;
159 		param->mtkaif_phase_cycle[i] = 0;
160 		mtkaif_chosen_phase[i] = -1;
161 		mtkaif_phase_cycle[i] = 0;
162 	}
163 
164 	if (IS_ERR(afe_priv->topckgen)) {
165 		dev_info(afe->dev, "%s() Cannot find topckgen controller\n",
166 			 __func__);
167 		return 0;
168 	}
169 
170 	pm_runtime_get_sync(afe->dev);
171 	mt6359_mtkaif_calibration_enable(cmpnt_codec);
172 
173 	/* set test type to synchronizer pulse */
174 	regmap_update_bits(afe_priv->topckgen,
175 			   CKSYS_AUD_TOP_CFG, 0xffff, 0x4);
176 	mtkaif_calibration_num_phase = 42;	/* mt6359: 0 ~ 42 */
177 	mtkaif_calibration_ok = true;
178 
179 	for (phase = 0;
180 	     phase <= mtkaif_calibration_num_phase && mtkaif_calibration_ok;
181 	     phase++) {
182 		mt6359_set_mtkaif_calibration_phase(cmpnt_codec,
183 						    phase, phase, phase);
184 
185 		regmap_update_bits(afe_priv->topckgen,
186 				   CKSYS_AUD_TOP_CFG, 0x1, 0x1);
187 
188 		test_done_1 = 0;
189 		test_done_2 = 0;
190 		test_done_3 = 0;
191 		cycle_1 = -1;
192 		cycle_2 = -1;
193 		cycle_3 = -1;
194 		counter = 0;
195 		while (!(test_done_1 & test_done_2 & test_done_3)) {
196 			regmap_read(afe_priv->topckgen,
197 				    CKSYS_AUD_TOP_MON, &monitor);
198 			test_done_1 = (monitor >> 28) & 0x1;
199 			test_done_2 = (monitor >> 29) & 0x1;
200 			test_done_3 = (monitor >> 30) & 0x1;
201 			if (test_done_1 == 1)
202 				cycle_1 = monitor & 0xf;
203 
204 			if (test_done_2 == 1)
205 				cycle_2 = (monitor >> 4) & 0xf;
206 
207 			if (test_done_3 == 1)
208 				cycle_3 = (monitor >> 8) & 0xf;
209 
210 			/* handle if never test done */
211 			if (++counter > 10000) {
212 				dev_info(afe->dev, "%s(), test fail, cycle_1 %d, cycle_2 %d, cycle_3 %d, monitor 0x%x\n",
213 					 __func__,
214 					 cycle_1, cycle_2, cycle_3, monitor);
215 				mtkaif_calibration_ok = false;
216 				break;
217 			}
218 		}
219 
220 		if (phase == 0) {
221 			prev_cycle_1 = cycle_1;
222 			prev_cycle_2 = cycle_2;
223 			prev_cycle_3 = cycle_3;
224 		}
225 
226 		if (cycle_1 != prev_cycle_1 &&
227 		    mtkaif_chosen_phase[MT8195_MTKAIF_MISO_0] < 0) {
228 			mtkaif_chosen_phase[MT8195_MTKAIF_MISO_0] = phase - 1;
229 			mtkaif_phase_cycle[MT8195_MTKAIF_MISO_0] = prev_cycle_1;
230 		}
231 
232 		if (cycle_2 != prev_cycle_2 &&
233 		    mtkaif_chosen_phase[MT8195_MTKAIF_MISO_1] < 0) {
234 			mtkaif_chosen_phase[MT8195_MTKAIF_MISO_1] = phase - 1;
235 			mtkaif_phase_cycle[MT8195_MTKAIF_MISO_1] = prev_cycle_2;
236 		}
237 
238 		if (cycle_3 != prev_cycle_3 &&
239 		    mtkaif_chosen_phase[MT8195_MTKAIF_MISO_2] < 0) {
240 			mtkaif_chosen_phase[MT8195_MTKAIF_MISO_2] = phase - 1;
241 			mtkaif_phase_cycle[MT8195_MTKAIF_MISO_2] = prev_cycle_3;
242 		}
243 
244 		regmap_update_bits(afe_priv->topckgen,
245 				   CKSYS_AUD_TOP_CFG, 0x1, 0x0);
246 
247 		if (mtkaif_chosen_phase[MT8195_MTKAIF_MISO_0] >= 0 &&
248 		    mtkaif_chosen_phase[MT8195_MTKAIF_MISO_1] >= 0 &&
249 		    mtkaif_chosen_phase[MT8195_MTKAIF_MISO_2] >= 0)
250 			break;
251 	}
252 
253 	if (mtkaif_chosen_phase[MT8195_MTKAIF_MISO_0] < 0) {
254 		mtkaif_calibration_ok = false;
255 		chosen_phase_1 = 0;
256 	} else {
257 		chosen_phase_1 = mtkaif_chosen_phase[MT8195_MTKAIF_MISO_0];
258 	}
259 
260 	if (mtkaif_chosen_phase[MT8195_MTKAIF_MISO_1] < 0) {
261 		mtkaif_calibration_ok = false;
262 		chosen_phase_2 = 0;
263 	} else {
264 		chosen_phase_2 = mtkaif_chosen_phase[MT8195_MTKAIF_MISO_1];
265 	}
266 
267 	if (mtkaif_chosen_phase[MT8195_MTKAIF_MISO_2] < 0) {
268 		mtkaif_calibration_ok = false;
269 		chosen_phase_3 = 0;
270 	} else {
271 		chosen_phase_3 = mtkaif_chosen_phase[MT8195_MTKAIF_MISO_2];
272 	}
273 
274 	mt6359_set_mtkaif_calibration_phase(cmpnt_codec,
275 					    chosen_phase_1,
276 					    chosen_phase_2,
277 					    chosen_phase_3);
278 
279 	mt6359_mtkaif_calibration_disable(cmpnt_codec);
280 	pm_runtime_put(afe->dev);
281 
282 	param->mtkaif_calibration_ok = mtkaif_calibration_ok;
283 	param->mtkaif_chosen_phase[MT8195_MTKAIF_MISO_0] = chosen_phase_1;
284 	param->mtkaif_chosen_phase[MT8195_MTKAIF_MISO_1] = chosen_phase_2;
285 	param->mtkaif_chosen_phase[MT8195_MTKAIF_MISO_2] = chosen_phase_3;
286 	for (i = 0; i < MT8195_MTKAIF_MISO_NUM; i++)
287 		param->mtkaif_phase_cycle[i] = mtkaif_phase_cycle[i];
288 
289 	dev_info(afe->dev, "%s(), end, calibration ok %d\n",
290 		 __func__, param->mtkaif_calibration_ok);
291 
292 	return 0;
293 }
294 
295 static int mt8195_mt6359_init(struct snd_soc_pcm_runtime *rtd)
296 {
297 	struct snd_soc_component *cmpnt_codec =
298 		asoc_rtd_to_codec(rtd, 0)->component;
299 
300 	/* set mtkaif protocol */
301 	mt6359_set_mtkaif_protocol(cmpnt_codec,
302 				   MT6359_MTKAIF_PROTOCOL_2_CLK_P2);
303 
304 	/* mtkaif calibration */
305 	mt8195_mt6359_mtkaif_calibration(rtd);
306 
307 	return 0;
308 }
309 
310 static int mt8195_hdmitx_dptx_startup(struct snd_pcm_substream *substream)
311 {
312 	static const unsigned int rates[] = {
313 		48000
314 	};
315 	static const unsigned int channels[] = {
316 		2, 4, 6, 8
317 	};
318 	static const struct snd_pcm_hw_constraint_list constraints_rates = {
319 		.count = ARRAY_SIZE(rates),
320 		.list  = rates,
321 		.mask = 0,
322 	};
323 	static const struct snd_pcm_hw_constraint_list constraints_channels = {
324 		.count = ARRAY_SIZE(channels),
325 		.list  = channels,
326 		.mask = 0,
327 	};
328 
329 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
330 	struct snd_pcm_runtime *runtime = substream->runtime;
331 	int ret;
332 
333 	ret = snd_pcm_hw_constraint_list(runtime, 0,
334 					 SNDRV_PCM_HW_PARAM_RATE,
335 					 &constraints_rates);
336 	if (ret < 0) {
337 		dev_err(rtd->dev, "hw_constraint_list rate failed\n");
338 		return ret;
339 	}
340 
341 	ret = snd_pcm_hw_constraint_list(runtime, 0,
342 					 SNDRV_PCM_HW_PARAM_CHANNELS,
343 					 &constraints_channels);
344 	if (ret < 0) {
345 		dev_err(rtd->dev, "hw_constraint_list channel failed\n");
346 		return ret;
347 	}
348 
349 	return 0;
350 }
351 
352 static const struct snd_soc_ops mt8195_hdmitx_dptx_playback_ops = {
353 	.startup = mt8195_hdmitx_dptx_startup,
354 };
355 
356 static int mt8195_dptx_hw_params(struct snd_pcm_substream *substream,
357 				 struct snd_pcm_hw_params *params)
358 {
359 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
360 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
361 
362 	return snd_soc_dai_set_sysclk(cpu_dai, 0, params_rate(params) * 256,
363 				      SND_SOC_CLOCK_OUT);
364 }
365 
366 static const struct snd_soc_ops mt8195_dptx_ops = {
367 	.hw_params = mt8195_dptx_hw_params,
368 };
369 
370 static int mt8195_dptx_codec_init(struct snd_soc_pcm_runtime *rtd)
371 {
372 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(rtd->card);
373 	struct mt8195_mt6359_priv *priv = soc_card_data->mach_priv;
374 	struct snd_soc_component *cmpnt_codec =
375 		asoc_rtd_to_codec(rtd, 0)->component;
376 	int ret;
377 
378 	ret = snd_soc_card_jack_new(rtd->card, "DP Jack", SND_JACK_LINEOUT,
379 				    &priv->dp_jack);
380 	if (ret)
381 		return ret;
382 
383 	return snd_soc_component_set_jack(cmpnt_codec, &priv->dp_jack, NULL);
384 }
385 
386 static int mt8195_hdmi_codec_init(struct snd_soc_pcm_runtime *rtd)
387 {
388 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(rtd->card);
389 	struct mt8195_mt6359_priv *priv = soc_card_data->mach_priv;
390 	struct snd_soc_component *cmpnt_codec =
391 		asoc_rtd_to_codec(rtd, 0)->component;
392 	int ret;
393 
394 	ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT,
395 				    &priv->hdmi_jack);
396 	if (ret)
397 		return ret;
398 
399 	return snd_soc_component_set_jack(cmpnt_codec, &priv->hdmi_jack, NULL);
400 }
401 
402 static int mt8195_dptx_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
403 				       struct snd_pcm_hw_params *params)
404 {
405 	/* fix BE i2s format to S24_LE, clean param mask first */
406 	snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
407 			     0, (__force unsigned int)SNDRV_PCM_FORMAT_LAST);
408 
409 	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
410 
411 	return 0;
412 }
413 
414 static int mt8195_playback_startup(struct snd_pcm_substream *substream)
415 {
416 	static const unsigned int rates[] = {
417 		48000
418 	};
419 	static const unsigned int channels[] = {
420 		2
421 	};
422 	static const struct snd_pcm_hw_constraint_list constraints_rates = {
423 		.count = ARRAY_SIZE(rates),
424 		.list  = rates,
425 		.mask = 0,
426 	};
427 	static const struct snd_pcm_hw_constraint_list constraints_channels = {
428 		.count = ARRAY_SIZE(channels),
429 		.list  = channels,
430 		.mask = 0,
431 	};
432 
433 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
434 	struct snd_pcm_runtime *runtime = substream->runtime;
435 	int ret;
436 
437 	ret = snd_pcm_hw_constraint_list(runtime, 0,
438 					 SNDRV_PCM_HW_PARAM_RATE,
439 					 &constraints_rates);
440 	if (ret < 0) {
441 		dev_err(rtd->dev, "hw_constraint_list rate failed\n");
442 		return ret;
443 	}
444 
445 	ret = snd_pcm_hw_constraint_list(runtime, 0,
446 					 SNDRV_PCM_HW_PARAM_CHANNELS,
447 					 &constraints_channels);
448 	if (ret < 0) {
449 		dev_err(rtd->dev, "hw_constraint_list channel failed\n");
450 		return ret;
451 	}
452 
453 	return 0;
454 }
455 
456 static const struct snd_soc_ops mt8195_playback_ops = {
457 	.startup = mt8195_playback_startup,
458 };
459 
460 static int mt8195_capture_startup(struct snd_pcm_substream *substream)
461 {
462 	static const unsigned int rates[] = {
463 		48000
464 	};
465 	static const unsigned int channels[] = {
466 		1, 2
467 	};
468 	static const struct snd_pcm_hw_constraint_list constraints_rates = {
469 		.count = ARRAY_SIZE(rates),
470 		.list  = rates,
471 		.mask = 0,
472 	};
473 	static const struct snd_pcm_hw_constraint_list constraints_channels = {
474 		.count = ARRAY_SIZE(channels),
475 		.list  = channels,
476 		.mask = 0,
477 	};
478 
479 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
480 	struct snd_pcm_runtime *runtime = substream->runtime;
481 	int ret;
482 
483 	ret = snd_pcm_hw_constraint_list(runtime, 0,
484 					 SNDRV_PCM_HW_PARAM_RATE,
485 					 &constraints_rates);
486 	if (ret < 0) {
487 		dev_err(rtd->dev, "hw_constraint_list rate failed\n");
488 		return ret;
489 	}
490 
491 	ret = snd_pcm_hw_constraint_list(runtime, 0,
492 					 SNDRV_PCM_HW_PARAM_CHANNELS,
493 					 &constraints_channels);
494 	if (ret < 0) {
495 		dev_err(rtd->dev, "hw_constraint_list channel failed\n");
496 		return ret;
497 	}
498 
499 	return 0;
500 }
501 
502 static const struct snd_soc_ops mt8195_capture_ops = {
503 	.startup = mt8195_capture_startup,
504 };
505 
506 static int mt8195_rt5682_etdm_hw_params(struct snd_pcm_substream *substream,
507 					struct snd_pcm_hw_params *params)
508 {
509 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
510 	struct snd_soc_card *card = rtd->card;
511 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
512 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
513 	unsigned int rate = params_rate(params);
514 	int bitwidth;
515 	int ret;
516 
517 	bitwidth = snd_pcm_format_width(params_format(params));
518 	if (bitwidth < 0) {
519 		dev_err(card->dev, "invalid bit width: %d\n", bitwidth);
520 		return bitwidth;
521 	}
522 
523 	ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x00, 0x0, 0x2, bitwidth);
524 	if (ret) {
525 		dev_err(card->dev, "failed to set tdm slot\n");
526 		return ret;
527 	}
528 
529 	ret = snd_soc_dai_set_pll(codec_dai, RT5682_PLL1, RT5682_PLL1_S_MCLK,
530 				  rate * 256, rate * 512);
531 	if (ret) {
532 		dev_err(card->dev, "failed to set pll\n");
533 		return ret;
534 	}
535 
536 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1,
537 				     rate * 512, SND_SOC_CLOCK_IN);
538 	if (ret) {
539 		dev_err(card->dev, "failed to set sysclk\n");
540 		return ret;
541 	}
542 
543 	return snd_soc_dai_set_sysclk(cpu_dai, 0, rate * 256,
544 				      SND_SOC_CLOCK_OUT);
545 }
546 
547 static const struct snd_soc_ops mt8195_rt5682_etdm_ops = {
548 	.hw_params = mt8195_rt5682_etdm_hw_params,
549 };
550 
551 static int mt8195_rt5682_init(struct snd_soc_pcm_runtime *rtd)
552 {
553 	struct snd_soc_component *cmpnt_codec =
554 		asoc_rtd_to_codec(rtd, 0)->component;
555 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(rtd->card);
556 	struct mt8195_mt6359_priv *priv = soc_card_data->mach_priv;
557 	struct snd_soc_jack *jack = &priv->headset_jack;
558 	struct snd_soc_component *cmpnt_afe =
559 		snd_soc_rtdcom_lookup(rtd, AFE_PCM_NAME);
560 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt_afe);
561 	struct mt8195_afe_private *afe_priv = afe->platform_priv;
562 	int ret;
563 
564 	priv->i2so1_mclk = afe_priv->clk[MT8195_CLK_TOP_APLL12_DIV2];
565 
566 	ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
567 				    SND_JACK_HEADSET | SND_JACK_BTN_0 |
568 				    SND_JACK_BTN_1 | SND_JACK_BTN_2 |
569 				    SND_JACK_BTN_3,
570 				    jack);
571 	if (ret) {
572 		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
573 		return ret;
574 	}
575 
576 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
577 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
578 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
579 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
580 
581 	ret = snd_soc_component_set_jack(cmpnt_codec, jack, NULL);
582 	if (ret) {
583 		dev_err(rtd->dev, "Headset Jack set failed: %d\n", ret);
584 		return ret;
585 	}
586 
587 	return 0;
588 };
589 
590 static int mt8195_rt1011_etdm_hw_params(struct snd_pcm_substream *substream,
591 					struct snd_pcm_hw_params *params)
592 {
593 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
594 	struct snd_soc_dai *codec_dai;
595 	struct snd_soc_card *card = rtd->card;
596 	int srate, i, ret;
597 
598 	srate = params_rate(params);
599 
600 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
601 		ret = snd_soc_dai_set_pll(codec_dai, 0, RT1011_PLL1_S_BCLK,
602 					  64 * srate, 256 * srate);
603 		if (ret < 0) {
604 			dev_err(card->dev, "codec_dai clock not set\n");
605 			return ret;
606 		}
607 
608 		ret = snd_soc_dai_set_sysclk(codec_dai,
609 					     RT1011_FS_SYS_PRE_S_PLL1,
610 					     256 * srate, SND_SOC_CLOCK_IN);
611 		if (ret < 0) {
612 			dev_err(card->dev, "codec_dai clock not set\n");
613 			return ret;
614 		}
615 	}
616 	return 0;
617 }
618 
619 static const struct snd_soc_ops mt8195_rt1011_etdm_ops = {
620 	.hw_params = mt8195_rt1011_etdm_hw_params,
621 };
622 
623 static int mt8195_rt1011_init(struct snd_soc_pcm_runtime *rtd)
624 {
625 	struct snd_soc_card *card = rtd->card;
626 	int ret;
627 
628 	ret = snd_soc_dapm_new_controls(&card->dapm, mt8195_dual_speaker_widgets,
629 					ARRAY_SIZE(mt8195_dual_speaker_widgets));
630 	if (ret) {
631 		dev_err(rtd->dev, "unable to add dapm controls, ret %d\n", ret);
632 		/* Don't need to add routes if widget addition failed */
633 		return ret;
634 	}
635 
636 	ret = snd_soc_add_card_controls(card, mt8195_dual_speaker_controls,
637 					ARRAY_SIZE(mt8195_dual_speaker_controls));
638 	if (ret) {
639 		dev_err(rtd->dev, "unable to add card controls, ret %d\n", ret);
640 		return ret;
641 	}
642 
643 	ret = snd_soc_dapm_add_routes(&card->dapm, mt8195_rt1011_routes,
644 				      ARRAY_SIZE(mt8195_rt1011_routes));
645 	if (ret)
646 		dev_err(rtd->dev, "unable to add dapm routes, ret %d\n", ret);
647 
648 	return ret;
649 }
650 
651 static int mt8195_rt1019_init(struct snd_soc_pcm_runtime *rtd)
652 {
653 	struct snd_soc_card *card = rtd->card;
654 	int ret;
655 
656 	ret = snd_soc_dapm_new_controls(&card->dapm, mt8195_speaker_widgets,
657 					ARRAY_SIZE(mt8195_speaker_widgets));
658 	if (ret) {
659 		dev_err(rtd->dev, "unable to add dapm controls, ret %d\n", ret);
660 		/* Don't need to add routes if widget addition failed */
661 		return ret;
662 	}
663 
664 	ret = snd_soc_add_card_controls(card, mt8195_speaker_controls,
665 					ARRAY_SIZE(mt8195_speaker_controls));
666 	if (ret) {
667 		dev_err(rtd->dev, "unable to add card controls, ret %d\n", ret);
668 		return ret;
669 	}
670 
671 	ret = snd_soc_dapm_add_routes(&card->dapm, mt8195_rt1019_routes,
672 				      ARRAY_SIZE(mt8195_rt1019_routes));
673 	if (ret)
674 		dev_err(rtd->dev, "unable to add dapm routes, ret %d\n", ret);
675 
676 	return ret;
677 }
678 
679 static int mt8195_max98390_init(struct snd_soc_pcm_runtime *rtd)
680 {
681 	struct snd_soc_card *card = rtd->card;
682 	int ret;
683 
684 	ret = snd_soc_dapm_new_controls(&card->dapm, mt8195_dual_speaker_widgets,
685 					ARRAY_SIZE(mt8195_dual_speaker_widgets));
686 	if (ret) {
687 		dev_err(rtd->dev, "unable to add dapm controls, ret %d\n", ret);
688 		/* Don't need to add routes if widget addition failed */
689 		return ret;
690 	}
691 
692 	ret = snd_soc_add_card_controls(card, mt8195_dual_speaker_controls,
693 					ARRAY_SIZE(mt8195_dual_speaker_controls));
694 	if (ret) {
695 		dev_err(rtd->dev, "unable to add card controls, ret %d\n", ret);
696 		return ret;
697 	}
698 
699 	ret = snd_soc_dapm_add_routes(&card->dapm, mt8195_max98390_routes,
700 				      ARRAY_SIZE(mt8195_max98390_routes));
701 	if (ret)
702 		dev_err(rtd->dev, "unable to add dapm routes, ret %d\n", ret);
703 
704 	return ret;
705 }
706 
707 static int mt8195_etdm_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
708 				       struct snd_pcm_hw_params *params)
709 {
710 	/* fix BE i2s format to S24_LE, clean param mask first */
711 	snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
712 			     0, (__force unsigned int)SNDRV_PCM_FORMAT_LAST);
713 
714 	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
715 
716 	return 0;
717 }
718 
719 static int mt8195_set_bias_level_post(struct snd_soc_card *card,
720 	struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
721 {
722 	struct snd_soc_component *component = dapm->component;
723 	struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(card);
724 	struct mt8195_mt6359_priv *priv = soc_card_data->mach_priv;
725 	int ret;
726 
727 	/*
728 	 * It's required to control mclk directly in the set_bias_level_post
729 	 * function for rt5682 and rt5682s codec, or the unexpected pop happens
730 	 * at the end of playback.
731 	 */
732 	if (!component ||
733 	    (strcmp(component->name, RT5682_DEV0_NAME) &&
734 	    strcmp(component->name, RT5682S_DEV0_NAME)))
735 		return 0;
736 
737 	switch (level) {
738 	case SND_SOC_BIAS_OFF:
739 		if (!__clk_is_enabled(priv->i2so1_mclk))
740 			return 0;
741 
742 		clk_disable_unprepare(priv->i2so1_mclk);
743 		dev_dbg(card->dev, "Disable i2so1 mclk\n");
744 		break;
745 	case SND_SOC_BIAS_ON:
746 		ret = clk_prepare_enable(priv->i2so1_mclk);
747 		if (ret) {
748 			dev_err(card->dev, "Can't enable i2so1 mclk: %d\n", ret);
749 			return ret;
750 		}
751 		dev_dbg(card->dev, "Enable i2so1 mclk\n");
752 		break;
753 	default:
754 		break;
755 	}
756 
757 	return 0;
758 }
759 
760 enum {
761 	DAI_LINK_DL2_FE,
762 	DAI_LINK_DL3_FE,
763 	DAI_LINK_DL6_FE,
764 	DAI_LINK_DL7_FE,
765 	DAI_LINK_DL8_FE,
766 	DAI_LINK_DL10_FE,
767 	DAI_LINK_DL11_FE,
768 	DAI_LINK_UL1_FE,
769 	DAI_LINK_UL2_FE,
770 	DAI_LINK_UL3_FE,
771 	DAI_LINK_UL4_FE,
772 	DAI_LINK_UL5_FE,
773 	DAI_LINK_UL6_FE,
774 	DAI_LINK_UL8_FE,
775 	DAI_LINK_UL9_FE,
776 	DAI_LINK_UL10_FE,
777 	DAI_LINK_DL_SRC_BE,
778 	DAI_LINK_DPTX_BE,
779 	DAI_LINK_ETDM1_IN_BE,
780 	DAI_LINK_ETDM2_IN_BE,
781 	DAI_LINK_ETDM1_OUT_BE,
782 	DAI_LINK_ETDM2_OUT_BE,
783 	DAI_LINK_ETDM3_OUT_BE,
784 	DAI_LINK_PCM1_BE,
785 	DAI_LINK_UL_SRC1_BE,
786 	DAI_LINK_UL_SRC2_BE,
787 	DAI_LINK_REGULAR_LAST = DAI_LINK_UL_SRC2_BE,
788 	DAI_LINK_SOF_START,
789 	DAI_LINK_SOF_DL2_BE = DAI_LINK_SOF_START,
790 	DAI_LINK_SOF_DL3_BE,
791 	DAI_LINK_SOF_UL4_BE,
792 	DAI_LINK_SOF_UL5_BE,
793 	DAI_LINK_SOF_END = DAI_LINK_SOF_UL5_BE,
794 };
795 
796 #define	DAI_LINK_REGULAR_NUM	(DAI_LINK_REGULAR_LAST + 1)
797 
798 /* FE */
799 SND_SOC_DAILINK_DEFS(DL2_FE,
800 		     DAILINK_COMP_ARRAY(COMP_CPU("DL2")),
801 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
802 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
803 
804 SND_SOC_DAILINK_DEFS(DL3_FE,
805 		     DAILINK_COMP_ARRAY(COMP_CPU("DL3")),
806 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
807 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
808 
809 SND_SOC_DAILINK_DEFS(DL6_FE,
810 		     DAILINK_COMP_ARRAY(COMP_CPU("DL6")),
811 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
812 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
813 
814 SND_SOC_DAILINK_DEFS(DL7_FE,
815 		     DAILINK_COMP_ARRAY(COMP_CPU("DL7")),
816 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
817 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
818 
819 SND_SOC_DAILINK_DEFS(DL8_FE,
820 		     DAILINK_COMP_ARRAY(COMP_CPU("DL8")),
821 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
822 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
823 
824 SND_SOC_DAILINK_DEFS(DL10_FE,
825 		     DAILINK_COMP_ARRAY(COMP_CPU("DL10")),
826 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
827 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
828 
829 SND_SOC_DAILINK_DEFS(DL11_FE,
830 		     DAILINK_COMP_ARRAY(COMP_CPU("DL11")),
831 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
832 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
833 
834 SND_SOC_DAILINK_DEFS(UL1_FE,
835 		     DAILINK_COMP_ARRAY(COMP_CPU("UL1")),
836 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
837 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
838 
839 SND_SOC_DAILINK_DEFS(UL2_FE,
840 		     DAILINK_COMP_ARRAY(COMP_CPU("UL2")),
841 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
842 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
843 
844 SND_SOC_DAILINK_DEFS(UL3_FE,
845 		     DAILINK_COMP_ARRAY(COMP_CPU("UL3")),
846 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
847 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
848 
849 SND_SOC_DAILINK_DEFS(UL4_FE,
850 		     DAILINK_COMP_ARRAY(COMP_CPU("UL4")),
851 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
852 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
853 
854 SND_SOC_DAILINK_DEFS(UL5_FE,
855 		     DAILINK_COMP_ARRAY(COMP_CPU("UL5")),
856 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
857 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
858 
859 SND_SOC_DAILINK_DEFS(UL6_FE,
860 		     DAILINK_COMP_ARRAY(COMP_CPU("UL6")),
861 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
862 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
863 
864 SND_SOC_DAILINK_DEFS(UL8_FE,
865 		     DAILINK_COMP_ARRAY(COMP_CPU("UL8")),
866 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
867 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
868 
869 SND_SOC_DAILINK_DEFS(UL9_FE,
870 		     DAILINK_COMP_ARRAY(COMP_CPU("UL9")),
871 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
872 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
873 
874 SND_SOC_DAILINK_DEFS(UL10_FE,
875 		     DAILINK_COMP_ARRAY(COMP_CPU("UL10")),
876 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
877 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
878 
879 /* BE */
880 SND_SOC_DAILINK_DEFS(DL_SRC_BE,
881 		     DAILINK_COMP_ARRAY(COMP_CPU("DL_SRC")),
882 		     DAILINK_COMP_ARRAY(COMP_CODEC("mt6359-sound",
883 						   "mt6359-snd-codec-aif1")),
884 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
885 
886 SND_SOC_DAILINK_DEFS(DPTX_BE,
887 		     DAILINK_COMP_ARRAY(COMP_CPU("DPTX")),
888 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
889 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
890 
891 SND_SOC_DAILINK_DEFS(ETDM1_IN_BE,
892 		     DAILINK_COMP_ARRAY(COMP_CPU("ETDM1_IN")),
893 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
894 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
895 
896 SND_SOC_DAILINK_DEFS(ETDM2_IN_BE,
897 		     DAILINK_COMP_ARRAY(COMP_CPU("ETDM2_IN")),
898 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
899 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
900 
901 SND_SOC_DAILINK_DEFS(ETDM1_OUT_BE,
902 		     DAILINK_COMP_ARRAY(COMP_CPU("ETDM1_OUT")),
903 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
904 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
905 
906 SND_SOC_DAILINK_DEFS(ETDM2_OUT_BE,
907 		     DAILINK_COMP_ARRAY(COMP_CPU("ETDM2_OUT")),
908 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
909 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
910 
911 SND_SOC_DAILINK_DEFS(ETDM3_OUT_BE,
912 		     DAILINK_COMP_ARRAY(COMP_CPU("ETDM3_OUT")),
913 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
914 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
915 
916 SND_SOC_DAILINK_DEFS(PCM1_BE,
917 		     DAILINK_COMP_ARRAY(COMP_CPU("PCM1")),
918 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
919 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
920 
921 SND_SOC_DAILINK_DEFS(UL_SRC1_BE,
922 		     DAILINK_COMP_ARRAY(COMP_CPU("UL_SRC1")),
923 		     DAILINK_COMP_ARRAY(COMP_CODEC("mt6359-sound",
924 						   "mt6359-snd-codec-aif1"),
925 					COMP_CODEC("dmic-codec",
926 						   "dmic-hifi")),
927 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
928 
929 SND_SOC_DAILINK_DEFS(UL_SRC2_BE,
930 		     DAILINK_COMP_ARRAY(COMP_CPU("UL_SRC2")),
931 		     DAILINK_COMP_ARRAY(COMP_CODEC("mt6359-sound",
932 						   "mt6359-snd-codec-aif2")),
933 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
934 
935 SND_SOC_DAILINK_DEFS(AFE_SOF_DL2,
936 		     DAILINK_COMP_ARRAY(COMP_CPU("SOF_DL2")),
937 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
938 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
939 
940 SND_SOC_DAILINK_DEFS(AFE_SOF_DL3,
941 		     DAILINK_COMP_ARRAY(COMP_CPU("SOF_DL3")),
942 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
943 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
944 
945 SND_SOC_DAILINK_DEFS(AFE_SOF_UL4,
946 		     DAILINK_COMP_ARRAY(COMP_CPU("SOF_UL4")),
947 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
948 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
949 
950 SND_SOC_DAILINK_DEFS(AFE_SOF_UL5,
951 		     DAILINK_COMP_ARRAY(COMP_CPU("SOF_UL5")),
952 		     DAILINK_COMP_ARRAY(COMP_DUMMY()),
953 		     DAILINK_COMP_ARRAY(COMP_EMPTY()));
954 
955 /* codec */
956 SND_SOC_DAILINK_DEF(rt1019_comps,
957 		    DAILINK_COMP_ARRAY(COMP_CODEC(RT1019_DEV0_NAME,
958 						  RT1019_CODEC_DAI)));
959 
960 SND_SOC_DAILINK_DEF(rt1011_comps,
961 		    DAILINK_COMP_ARRAY(COMP_CODEC(RT1011_DEV0_NAME,
962 						  RT1011_CODEC_DAI),
963 				       COMP_CODEC(RT1011_DEV1_NAME,
964 						  RT1011_CODEC_DAI)));
965 
966 SND_SOC_DAILINK_DEF(max98390_comps,
967 		    DAILINK_COMP_ARRAY(COMP_CODEC(MAX98390_DEV0_NAME,
968 						  MAX98390_CODEC_DAI),
969 				       COMP_CODEC(MAX98390_DEV1_NAME,
970 						  MAX98390_CODEC_DAI)));
971 
972 static const struct sof_conn_stream g_sof_conn_streams[] = {
973 	{ "ETDM2_OUT_BE", "AFE_SOF_DL2", SOF_DMA_DL2, SNDRV_PCM_STREAM_PLAYBACK},
974 	{ "ETDM1_OUT_BE", "AFE_SOF_DL3", SOF_DMA_DL3, SNDRV_PCM_STREAM_PLAYBACK},
975 	{ "UL_SRC1_BE", "AFE_SOF_UL4", SOF_DMA_UL4, SNDRV_PCM_STREAM_CAPTURE},
976 	{ "ETDM2_IN_BE", "AFE_SOF_UL5", SOF_DMA_UL5, SNDRV_PCM_STREAM_CAPTURE},
977 };
978 
979 static struct snd_soc_dai_link mt8195_mt6359_dai_links[] = {
980 	/* FE */
981 	[DAI_LINK_DL2_FE] = {
982 		.name = "DL2_FE",
983 		.stream_name = "DL2 Playback",
984 		.trigger = {
985 			SND_SOC_DPCM_TRIGGER_POST,
986 			SND_SOC_DPCM_TRIGGER_POST,
987 		},
988 		.dynamic = 1,
989 		.dpcm_playback = 1,
990 		.ops = &mt8195_playback_ops,
991 		SND_SOC_DAILINK_REG(DL2_FE),
992 	},
993 	[DAI_LINK_DL3_FE] = {
994 		.name = "DL3_FE",
995 		.stream_name = "DL3 Playback",
996 		.trigger = {
997 			SND_SOC_DPCM_TRIGGER_POST,
998 			SND_SOC_DPCM_TRIGGER_POST,
999 		},
1000 		.dynamic = 1,
1001 		.dpcm_playback = 1,
1002 		.ops = &mt8195_playback_ops,
1003 		SND_SOC_DAILINK_REG(DL3_FE),
1004 	},
1005 	[DAI_LINK_DL6_FE] = {
1006 		.name = "DL6_FE",
1007 		.stream_name = "DL6 Playback",
1008 		.trigger = {
1009 			SND_SOC_DPCM_TRIGGER_POST,
1010 			SND_SOC_DPCM_TRIGGER_POST,
1011 		},
1012 		.dynamic = 1,
1013 		.dpcm_playback = 1,
1014 		.ops = &mt8195_playback_ops,
1015 		SND_SOC_DAILINK_REG(DL6_FE),
1016 	},
1017 	[DAI_LINK_DL7_FE] = {
1018 		.name = "DL7_FE",
1019 		.stream_name = "DL7 Playback",
1020 		.trigger = {
1021 			SND_SOC_DPCM_TRIGGER_PRE,
1022 			SND_SOC_DPCM_TRIGGER_PRE,
1023 		},
1024 		.dynamic = 1,
1025 		.dpcm_playback = 1,
1026 		SND_SOC_DAILINK_REG(DL7_FE),
1027 	},
1028 	[DAI_LINK_DL8_FE] = {
1029 		.name = "DL8_FE",
1030 		.stream_name = "DL8 Playback",
1031 		.trigger = {
1032 			SND_SOC_DPCM_TRIGGER_POST,
1033 			SND_SOC_DPCM_TRIGGER_POST,
1034 		},
1035 		.dynamic = 1,
1036 		.dpcm_playback = 1,
1037 		.ops = &mt8195_playback_ops,
1038 		SND_SOC_DAILINK_REG(DL8_FE),
1039 	},
1040 	[DAI_LINK_DL10_FE] = {
1041 		.name = "DL10_FE",
1042 		.stream_name = "DL10 Playback",
1043 		.trigger = {
1044 			SND_SOC_DPCM_TRIGGER_POST,
1045 			SND_SOC_DPCM_TRIGGER_POST,
1046 		},
1047 		.dynamic = 1,
1048 		.dpcm_playback = 1,
1049 		.ops = &mt8195_hdmitx_dptx_playback_ops,
1050 		SND_SOC_DAILINK_REG(DL10_FE),
1051 	},
1052 	[DAI_LINK_DL11_FE] = {
1053 		.name = "DL11_FE",
1054 		.stream_name = "DL11 Playback",
1055 		.trigger = {
1056 			SND_SOC_DPCM_TRIGGER_POST,
1057 			SND_SOC_DPCM_TRIGGER_POST,
1058 		},
1059 		.dynamic = 1,
1060 		.dpcm_playback = 1,
1061 		.ops = &mt8195_playback_ops,
1062 		SND_SOC_DAILINK_REG(DL11_FE),
1063 	},
1064 	[DAI_LINK_UL1_FE] = {
1065 		.name = "UL1_FE",
1066 		.stream_name = "UL1 Capture",
1067 		.trigger = {
1068 			SND_SOC_DPCM_TRIGGER_PRE,
1069 			SND_SOC_DPCM_TRIGGER_PRE,
1070 		},
1071 		.dynamic = 1,
1072 		.dpcm_capture = 1,
1073 		SND_SOC_DAILINK_REG(UL1_FE),
1074 	},
1075 	[DAI_LINK_UL2_FE] = {
1076 		.name = "UL2_FE",
1077 		.stream_name = "UL2 Capture",
1078 		.trigger = {
1079 			SND_SOC_DPCM_TRIGGER_POST,
1080 			SND_SOC_DPCM_TRIGGER_POST,
1081 		},
1082 		.dynamic = 1,
1083 		.dpcm_capture = 1,
1084 		.ops = &mt8195_capture_ops,
1085 		SND_SOC_DAILINK_REG(UL2_FE),
1086 	},
1087 	[DAI_LINK_UL3_FE] = {
1088 		.name = "UL3_FE",
1089 		.stream_name = "UL3 Capture",
1090 		.trigger = {
1091 			SND_SOC_DPCM_TRIGGER_POST,
1092 			SND_SOC_DPCM_TRIGGER_POST,
1093 		},
1094 		.dynamic = 1,
1095 		.dpcm_capture = 1,
1096 		.ops = &mt8195_capture_ops,
1097 		SND_SOC_DAILINK_REG(UL3_FE),
1098 	},
1099 	[DAI_LINK_UL4_FE] = {
1100 		.name = "UL4_FE",
1101 		.stream_name = "UL4 Capture",
1102 		.trigger = {
1103 			SND_SOC_DPCM_TRIGGER_POST,
1104 			SND_SOC_DPCM_TRIGGER_POST,
1105 		},
1106 		.dynamic = 1,
1107 		.dpcm_capture = 1,
1108 		.ops = &mt8195_capture_ops,
1109 		SND_SOC_DAILINK_REG(UL4_FE),
1110 	},
1111 	[DAI_LINK_UL5_FE] = {
1112 		.name = "UL5_FE",
1113 		.stream_name = "UL5 Capture",
1114 		.trigger = {
1115 			SND_SOC_DPCM_TRIGGER_POST,
1116 			SND_SOC_DPCM_TRIGGER_POST,
1117 		},
1118 		.dynamic = 1,
1119 		.dpcm_capture = 1,
1120 		.ops = &mt8195_capture_ops,
1121 		SND_SOC_DAILINK_REG(UL5_FE),
1122 	},
1123 	[DAI_LINK_UL6_FE] = {
1124 		.name = "UL6_FE",
1125 		.stream_name = "UL6 Capture",
1126 		.trigger = {
1127 			SND_SOC_DPCM_TRIGGER_PRE,
1128 			SND_SOC_DPCM_TRIGGER_PRE,
1129 		},
1130 		.dynamic = 1,
1131 		.dpcm_capture = 1,
1132 		SND_SOC_DAILINK_REG(UL6_FE),
1133 	},
1134 	[DAI_LINK_UL8_FE] = {
1135 		.name = "UL8_FE",
1136 		.stream_name = "UL8 Capture",
1137 		.trigger = {
1138 			SND_SOC_DPCM_TRIGGER_POST,
1139 			SND_SOC_DPCM_TRIGGER_POST,
1140 		},
1141 		.dynamic = 1,
1142 		.dpcm_capture = 1,
1143 		.ops = &mt8195_capture_ops,
1144 		SND_SOC_DAILINK_REG(UL8_FE),
1145 	},
1146 	[DAI_LINK_UL9_FE] = {
1147 		.name = "UL9_FE",
1148 		.stream_name = "UL9 Capture",
1149 		.trigger = {
1150 			SND_SOC_DPCM_TRIGGER_POST,
1151 			SND_SOC_DPCM_TRIGGER_POST,
1152 		},
1153 		.dynamic = 1,
1154 		.dpcm_capture = 1,
1155 		.ops = &mt8195_capture_ops,
1156 		SND_SOC_DAILINK_REG(UL9_FE),
1157 	},
1158 	[DAI_LINK_UL10_FE] = {
1159 		.name = "UL10_FE",
1160 		.stream_name = "UL10 Capture",
1161 		.trigger = {
1162 			SND_SOC_DPCM_TRIGGER_POST,
1163 			SND_SOC_DPCM_TRIGGER_POST,
1164 		},
1165 		.dynamic = 1,
1166 		.dpcm_capture = 1,
1167 		.ops = &mt8195_capture_ops,
1168 		SND_SOC_DAILINK_REG(UL10_FE),
1169 	},
1170 	/* BE */
1171 	[DAI_LINK_DL_SRC_BE] = {
1172 		.name = "DL_SRC_BE",
1173 		.no_pcm = 1,
1174 		.dpcm_playback = 1,
1175 		SND_SOC_DAILINK_REG(DL_SRC_BE),
1176 	},
1177 	[DAI_LINK_DPTX_BE] = {
1178 		.name = "DPTX_BE",
1179 		.no_pcm = 1,
1180 		.dpcm_playback = 1,
1181 		.ops = &mt8195_dptx_ops,
1182 		.be_hw_params_fixup = mt8195_dptx_hw_params_fixup,
1183 		SND_SOC_DAILINK_REG(DPTX_BE),
1184 	},
1185 	[DAI_LINK_ETDM1_IN_BE] = {
1186 		.name = "ETDM1_IN_BE",
1187 		.no_pcm = 1,
1188 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1189 			SND_SOC_DAIFMT_NB_NF |
1190 			SND_SOC_DAIFMT_CBS_CFS,
1191 		.dpcm_capture = 1,
1192 		SND_SOC_DAILINK_REG(ETDM1_IN_BE),
1193 	},
1194 	[DAI_LINK_ETDM2_IN_BE] = {
1195 		.name = "ETDM2_IN_BE",
1196 		.no_pcm = 1,
1197 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1198 			SND_SOC_DAIFMT_NB_NF |
1199 			SND_SOC_DAIFMT_CBS_CFS,
1200 		.dpcm_capture = 1,
1201 		.init = mt8195_rt5682_init,
1202 		.ops = &mt8195_rt5682_etdm_ops,
1203 		.be_hw_params_fixup = mt8195_etdm_hw_params_fixup,
1204 		SND_SOC_DAILINK_REG(ETDM2_IN_BE),
1205 	},
1206 	[DAI_LINK_ETDM1_OUT_BE] = {
1207 		.name = "ETDM1_OUT_BE",
1208 		.no_pcm = 1,
1209 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1210 			SND_SOC_DAIFMT_NB_NF |
1211 			SND_SOC_DAIFMT_CBS_CFS,
1212 		.dpcm_playback = 1,
1213 		.ops = &mt8195_rt5682_etdm_ops,
1214 		.be_hw_params_fixup = mt8195_etdm_hw_params_fixup,
1215 		SND_SOC_DAILINK_REG(ETDM1_OUT_BE),
1216 	},
1217 	[DAI_LINK_ETDM2_OUT_BE] = {
1218 		.name = "ETDM2_OUT_BE",
1219 		.no_pcm = 1,
1220 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1221 			SND_SOC_DAIFMT_NB_NF |
1222 			SND_SOC_DAIFMT_CBS_CFS,
1223 		.dpcm_playback = 1,
1224 		SND_SOC_DAILINK_REG(ETDM2_OUT_BE),
1225 	},
1226 	[DAI_LINK_ETDM3_OUT_BE] = {
1227 		.name = "ETDM3_OUT_BE",
1228 		.no_pcm = 1,
1229 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1230 			SND_SOC_DAIFMT_NB_NF |
1231 			SND_SOC_DAIFMT_CBS_CFS,
1232 		.dpcm_playback = 1,
1233 		SND_SOC_DAILINK_REG(ETDM3_OUT_BE),
1234 	},
1235 	[DAI_LINK_PCM1_BE] = {
1236 		.name = "PCM1_BE",
1237 		.no_pcm = 1,
1238 		.dai_fmt = SND_SOC_DAIFMT_I2S |
1239 			SND_SOC_DAIFMT_NB_NF |
1240 			SND_SOC_DAIFMT_CBS_CFS,
1241 		.dpcm_playback = 1,
1242 		.dpcm_capture = 1,
1243 		SND_SOC_DAILINK_REG(PCM1_BE),
1244 	},
1245 	[DAI_LINK_UL_SRC1_BE] = {
1246 		.name = "UL_SRC1_BE",
1247 		.no_pcm = 1,
1248 		.dpcm_capture = 1,
1249 		SND_SOC_DAILINK_REG(UL_SRC1_BE),
1250 	},
1251 	[DAI_LINK_UL_SRC2_BE] = {
1252 		.name = "UL_SRC2_BE",
1253 		.no_pcm = 1,
1254 		.dpcm_capture = 1,
1255 		SND_SOC_DAILINK_REG(UL_SRC2_BE),
1256 	},
1257 	/* SOF BE */
1258 	[DAI_LINK_SOF_DL2_BE] = {
1259 		.name = "AFE_SOF_DL2",
1260 		.no_pcm = 1,
1261 		.dpcm_playback = 1,
1262 		SND_SOC_DAILINK_REG(AFE_SOF_DL2),
1263 	},
1264 	[DAI_LINK_SOF_DL3_BE] = {
1265 		.name = "AFE_SOF_DL3",
1266 		.no_pcm = 1,
1267 		.dpcm_playback = 1,
1268 		SND_SOC_DAILINK_REG(AFE_SOF_DL3),
1269 	},
1270 	[DAI_LINK_SOF_UL4_BE] = {
1271 		.name = "AFE_SOF_UL4",
1272 		.no_pcm = 1,
1273 		.dpcm_capture = 1,
1274 		SND_SOC_DAILINK_REG(AFE_SOF_UL4),
1275 	},
1276 	[DAI_LINK_SOF_UL5_BE] = {
1277 		.name = "AFE_SOF_UL5",
1278 		.no_pcm = 1,
1279 		.dpcm_capture = 1,
1280 		SND_SOC_DAILINK_REG(AFE_SOF_UL5),
1281 	},
1282 };
1283 
1284 static struct snd_soc_codec_conf rt1011_codec_conf[] = {
1285 	{
1286 		.dlc = COMP_CODEC_CONF(RT1011_DEV0_NAME),
1287 		.name_prefix = "Left",
1288 	},
1289 	{
1290 		.dlc = COMP_CODEC_CONF(RT1011_DEV1_NAME),
1291 		.name_prefix = "Right",
1292 	},
1293 };
1294 
1295 static struct snd_soc_codec_conf max98390_codec_conf[] = {
1296 	{
1297 		.dlc = COMP_CODEC_CONF(MAX98390_DEV0_NAME),
1298 		.name_prefix = "Right",
1299 	},
1300 	{
1301 		.dlc = COMP_CODEC_CONF(MAX98390_DEV1_NAME),
1302 		.name_prefix = "Left",
1303 	},
1304 };
1305 
1306 static struct snd_soc_card mt8195_mt6359_soc_card = {
1307 	.owner = THIS_MODULE,
1308 	.dai_link = mt8195_mt6359_dai_links,
1309 	.num_links = ARRAY_SIZE(mt8195_mt6359_dai_links),
1310 	.controls = mt8195_mt6359_controls,
1311 	.num_controls = ARRAY_SIZE(mt8195_mt6359_controls),
1312 	.dapm_widgets = mt8195_mt6359_widgets,
1313 	.num_dapm_widgets = ARRAY_SIZE(mt8195_mt6359_widgets),
1314 	.dapm_routes = mt8195_mt6359_routes,
1315 	.num_dapm_routes = ARRAY_SIZE(mt8195_mt6359_routes),
1316 	.set_bias_level_post = mt8195_set_bias_level_post,
1317 };
1318 
1319 /* fixup the BE DAI link to match any values from topology */
1320 static int mt8195_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
1321 				 struct snd_pcm_hw_params *params)
1322 {
1323 	int ret;
1324 
1325 	ret = mtk_sof_dai_link_fixup(rtd, params);
1326 
1327 	if (!strcmp(rtd->dai_link->name, "ETDM2_IN_BE") ||
1328 	    !strcmp(rtd->dai_link->name, "ETDM1_OUT_BE")) {
1329 		mt8195_etdm_hw_params_fixup(rtd, params);
1330 	}
1331 
1332 	return ret;
1333 }
1334 
1335 static int mt8195_mt6359_dev_probe(struct platform_device *pdev)
1336 {
1337 	struct snd_soc_card *card = &mt8195_mt6359_soc_card;
1338 	struct snd_soc_dai_link *dai_link;
1339 	struct mtk_soc_card_data *soc_card_data;
1340 	struct mt8195_mt6359_priv *mach_priv;
1341 	struct device_node *platform_node, *adsp_node, *dp_node, *hdmi_node;
1342 	struct mt8195_card_data *card_data;
1343 	int is5682s = 0;
1344 	int init6359 = 0;
1345 	int sof_on = 0;
1346 	int ret, i;
1347 
1348 	card_data = (struct mt8195_card_data *)of_device_get_match_data(&pdev->dev);
1349 	card->dev = &pdev->dev;
1350 
1351 	ret = snd_soc_of_parse_card_name(card, "model");
1352 	if (ret) {
1353 		dev_err(&pdev->dev, "%s new card name parsing error %d\n",
1354 			__func__, ret);
1355 		return ret;
1356 	}
1357 
1358 	if (!card->name)
1359 		card->name = card_data->name;
1360 
1361 	if (strstr(card->name, "_5682s"))
1362 		is5682s = 1;
1363 	soc_card_data = devm_kzalloc(&pdev->dev, sizeof(*card_data), GFP_KERNEL);
1364 	if (!soc_card_data)
1365 		return -ENOMEM;
1366 
1367 	mach_priv = devm_kzalloc(&pdev->dev, sizeof(*mach_priv), GFP_KERNEL);
1368 	if (!mach_priv)
1369 		return -ENOMEM;
1370 
1371 	soc_card_data->mach_priv = mach_priv;
1372 
1373 	adsp_node = of_parse_phandle(pdev->dev.of_node, "mediatek,adsp", 0);
1374 	if (adsp_node) {
1375 		struct mtk_sof_priv *sof_priv;
1376 
1377 		sof_priv = devm_kzalloc(&pdev->dev, sizeof(*sof_priv), GFP_KERNEL);
1378 		if (!sof_priv) {
1379 			ret = -ENOMEM;
1380 			goto err_kzalloc;
1381 		}
1382 		sof_priv->conn_streams = g_sof_conn_streams;
1383 		sof_priv->num_streams = ARRAY_SIZE(g_sof_conn_streams);
1384 		sof_priv->sof_dai_link_fixup = mt8195_dai_link_fixup;
1385 		soc_card_data->sof_priv = sof_priv;
1386 		card->late_probe = mtk_sof_card_late_probe;
1387 		sof_on = 1;
1388 	}
1389 
1390 	if (of_property_read_bool(pdev->dev.of_node, "mediatek,dai-link")) {
1391 		ret = mtk_sof_dailink_parse_of(card, pdev->dev.of_node,
1392 					       "mediatek,dai-link",
1393 					       mt8195_mt6359_dai_links,
1394 					       ARRAY_SIZE(mt8195_mt6359_dai_links));
1395 		if (ret) {
1396 			dev_dbg(&pdev->dev, "Parse dai-link fail\n");
1397 			goto err_parse_of;
1398 		}
1399 	} else {
1400 		if (!sof_on)
1401 			card->num_links = DAI_LINK_REGULAR_NUM;
1402 	}
1403 
1404 	platform_node = of_parse_phandle(pdev->dev.of_node,
1405 					 "mediatek,platform", 0);
1406 	if (!platform_node) {
1407 		dev_dbg(&pdev->dev, "Property 'platform' missing or invalid\n");
1408 		ret = -EINVAL;
1409 		goto err_platform_node;
1410 	}
1411 
1412 	dp_node = of_parse_phandle(pdev->dev.of_node, "mediatek,dptx-codec", 0);
1413 	hdmi_node = of_parse_phandle(pdev->dev.of_node,
1414 				     "mediatek,hdmi-codec", 0);
1415 
1416 	for_each_card_prelinks(card, i, dai_link) {
1417 		if (!dai_link->platforms->name) {
1418 			if (!strncmp(dai_link->name, "AFE_SOF", strlen("AFE_SOF")) && sof_on)
1419 				dai_link->platforms->of_node = adsp_node;
1420 			else
1421 				dai_link->platforms->of_node = platform_node;
1422 		}
1423 
1424 		if (strcmp(dai_link->name, "DPTX_BE") == 0) {
1425 			if (!dp_node) {
1426 				dev_dbg(&pdev->dev, "No property 'dptx-codec'\n");
1427 			} else {
1428 				dai_link->codecs->of_node = dp_node;
1429 				dai_link->codecs->name = NULL;
1430 				dai_link->codecs->dai_name = "i2s-hifi";
1431 				dai_link->init = mt8195_dptx_codec_init;
1432 			}
1433 		} else if (strcmp(dai_link->name, "ETDM3_OUT_BE") == 0) {
1434 			if (!hdmi_node) {
1435 				dev_dbg(&pdev->dev, "No property 'hdmi-codec'\n");
1436 			} else {
1437 				dai_link->codecs->of_node = hdmi_node;
1438 				dai_link->codecs->name = NULL;
1439 				dai_link->codecs->dai_name = "i2s-hifi";
1440 				dai_link->init = mt8195_hdmi_codec_init;
1441 			}
1442 		} else if (strcmp(dai_link->name, "ETDM1_OUT_BE") == 0 ||
1443 			   strcmp(dai_link->name, "ETDM2_IN_BE") == 0) {
1444 			dai_link->codecs->name =
1445 				is5682s ? RT5682S_DEV0_NAME : RT5682_DEV0_NAME;
1446 			dai_link->codecs->dai_name =
1447 				is5682s ? RT5682S_CODEC_DAI : RT5682_CODEC_DAI;
1448 		} else if (strcmp(dai_link->name, "DL_SRC_BE") == 0 ||
1449 			   strcmp(dai_link->name, "UL_SRC1_BE") == 0 ||
1450 			   strcmp(dai_link->name, "UL_SRC2_BE") == 0) {
1451 			if (!init6359) {
1452 				dai_link->init = mt8195_mt6359_init;
1453 				init6359 = 1;
1454 			}
1455 		} else if (strcmp(dai_link->name, "ETDM2_OUT_BE") == 0) {
1456 			switch (card_data->quirk) {
1457 			case RT1011_SPEAKER_AMP_PRESENT:
1458 				dai_link->codecs = rt1011_comps;
1459 				dai_link->num_codecs = ARRAY_SIZE(rt1011_comps);
1460 				dai_link->init = mt8195_rt1011_init;
1461 				dai_link->ops = &mt8195_rt1011_etdm_ops;
1462 				dai_link->be_hw_params_fixup = mt8195_etdm_hw_params_fixup;
1463 				card->codec_conf = rt1011_codec_conf;
1464 				card->num_configs = ARRAY_SIZE(rt1011_codec_conf);
1465 				break;
1466 			case RT1019_SPEAKER_AMP_PRESENT:
1467 				dai_link->codecs = rt1019_comps;
1468 				dai_link->num_codecs = ARRAY_SIZE(rt1019_comps);
1469 				dai_link->init = mt8195_rt1019_init;
1470 				break;
1471 			case MAX98390_SPEAKER_AMP_PRESENT:
1472 				dai_link->codecs = max98390_comps;
1473 				dai_link->num_codecs = ARRAY_SIZE(max98390_comps);
1474 				dai_link->init = mt8195_max98390_init;
1475 				card->codec_conf = max98390_codec_conf;
1476 				card->num_configs = ARRAY_SIZE(max98390_codec_conf);
1477 				break;
1478 			default:
1479 				break;
1480 			}
1481 		}
1482 	}
1483 
1484 	snd_soc_card_set_drvdata(card, soc_card_data);
1485 
1486 	ret = devm_snd_soc_register_card(&pdev->dev, card);
1487 
1488 	of_node_put(platform_node);
1489 	of_node_put(dp_node);
1490 	of_node_put(hdmi_node);
1491 err_kzalloc:
1492 err_parse_of:
1493 err_platform_node:
1494 	of_node_put(adsp_node);
1495 	return ret;
1496 }
1497 
1498 static struct mt8195_card_data mt8195_mt6359_rt1019_rt5682_card = {
1499 	.name = "mt8195_r1019_5682",
1500 	.quirk = RT1019_SPEAKER_AMP_PRESENT,
1501 };
1502 
1503 static struct mt8195_card_data mt8195_mt6359_rt1011_rt5682_card = {
1504 	.name = "mt8195_r1011_5682",
1505 	.quirk = RT1011_SPEAKER_AMP_PRESENT,
1506 };
1507 
1508 static struct mt8195_card_data mt8195_mt6359_max98390_rt5682_card = {
1509 	.name = "mt8195_m98390_r5682",
1510 	.quirk = MAX98390_SPEAKER_AMP_PRESENT,
1511 };
1512 
1513 static const struct of_device_id mt8195_mt6359_dt_match[] = {
1514 	{
1515 		.compatible = "mediatek,mt8195_mt6359_rt1019_rt5682",
1516 		.data = &mt8195_mt6359_rt1019_rt5682_card,
1517 	},
1518 	{
1519 		.compatible = "mediatek,mt8195_mt6359_rt1011_rt5682",
1520 		.data = &mt8195_mt6359_rt1011_rt5682_card,
1521 	},
1522 	{
1523 		.compatible = "mediatek,mt8195_mt6359_max98390_rt5682",
1524 		.data = &mt8195_mt6359_max98390_rt5682_card,
1525 	},
1526 	{},
1527 };
1528 
1529 static const struct dev_pm_ops mt8195_mt6359_pm_ops = {
1530 	.poweroff = snd_soc_poweroff,
1531 	.restore = snd_soc_resume,
1532 };
1533 
1534 static struct platform_driver mt8195_mt6359_driver = {
1535 	.driver = {
1536 		.name = "mt8195_mt6359",
1537 		.of_match_table = mt8195_mt6359_dt_match,
1538 		.pm = &mt8195_mt6359_pm_ops,
1539 	},
1540 	.probe = mt8195_mt6359_dev_probe,
1541 };
1542 
1543 module_platform_driver(mt8195_mt6359_driver);
1544 
1545 /* Module information */
1546 MODULE_DESCRIPTION("MT8195-MT6359 ALSA SoC machine driver");
1547 MODULE_AUTHOR("Trevor Wu <trevor.wu@mediatek.com>");
1548 MODULE_AUTHOR("YC Hung <yc.hung@mediatek.com>");
1549 MODULE_LICENSE("GPL");
1550 MODULE_ALIAS("mt8195_mt6359 soc card");
1551