1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // mt8183-da7219-max98357.c
4 //	--  MT8183-DA7219-MAX98357 ALSA SoC machine driver
5 //
6 // Copyright (c) 2018 MediaTek Inc.
7 // Author: Shunli Wang <shunli.wang@mediatek.com>
8 
9 #include <linux/input.h>
10 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/pinctrl/consumer.h>
13 #include <sound/hdmi-codec.h>
14 #include <sound/jack.h>
15 #include <sound/pcm_params.h>
16 #include <sound/soc.h>
17 
18 #include "../../codecs/da7219-aad.h"
19 #include "../../codecs/da7219.h"
20 #include "../../codecs/rt1015.h"
21 #include "mt8183-afe-common.h"
22 
23 #define DA7219_CODEC_DAI "da7219-hifi"
24 #define DA7219_DEV_NAME "da7219.5-001a"
25 #define RT1015_CODEC_DAI "rt1015-aif"
26 #define RT1015_DEV0_NAME "rt1015.6-0028"
27 #define RT1015_DEV1_NAME "rt1015.6-0029"
28 
29 struct mt8183_da7219_max98357_priv {
30 	struct snd_soc_jack headset_jack, hdmi_jack;
31 };
32 
33 static int mt8183_mt6358_i2s_hw_params(struct snd_pcm_substream *substream,
34 				       struct snd_pcm_hw_params *params)
35 {
36 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
37 	unsigned int rate = params_rate(params);
38 	unsigned int mclk_fs_ratio = 128;
39 	unsigned int mclk_fs = rate * mclk_fs_ratio;
40 
41 	return snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0),
42 				      0, mclk_fs, SND_SOC_CLOCK_OUT);
43 }
44 
45 static const struct snd_soc_ops mt8183_mt6358_i2s_ops = {
46 	.hw_params = mt8183_mt6358_i2s_hw_params,
47 };
48 
49 static int mt8183_da7219_i2s_hw_params(struct snd_pcm_substream *substream,
50 				       struct snd_pcm_hw_params *params)
51 {
52 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
53 	struct snd_soc_dai *codec_dai;
54 	unsigned int rate = params_rate(params);
55 	unsigned int mclk_fs_ratio = 256;
56 	unsigned int mclk_fs = rate * mclk_fs_ratio;
57 	unsigned int freq;
58 	int ret = 0, j;
59 
60 	ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), 0,
61 				     mclk_fs, SND_SOC_CLOCK_OUT);
62 	if (ret < 0)
63 		dev_err(rtd->dev, "failed to set cpu dai sysclk\n");
64 
65 	for_each_rtd_codec_dais(rtd, j, codec_dai) {
66 		if (!strcmp(codec_dai->component->name, DA7219_DEV_NAME)) {
67 			ret = snd_soc_dai_set_sysclk(codec_dai,
68 						     DA7219_CLKSRC_MCLK,
69 						     mclk_fs,
70 						     SND_SOC_CLOCK_IN);
71 			if (ret < 0)
72 				dev_err(rtd->dev, "failed to set sysclk\n");
73 
74 			if ((rate % 8000) == 0)
75 				freq = DA7219_PLL_FREQ_OUT_98304;
76 			else
77 				freq = DA7219_PLL_FREQ_OUT_90316;
78 
79 			ret = snd_soc_dai_set_pll(codec_dai, 0,
80 						  DA7219_SYSCLK_PLL_SRM,
81 						  0, freq);
82 			if (ret)
83 				dev_err(rtd->dev, "failed to start PLL: %d\n",
84 					ret);
85 		}
86 	}
87 
88 	return ret;
89 }
90 
91 static int mt8183_da7219_hw_free(struct snd_pcm_substream *substream)
92 {
93 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
94 	struct snd_soc_dai *codec_dai;
95 	int ret = 0, j;
96 
97 	for_each_rtd_codec_dais(rtd, j, codec_dai) {
98 		if (!strcmp(codec_dai->component->name, DA7219_DEV_NAME)) {
99 			ret = snd_soc_dai_set_pll(codec_dai,
100 						  0, DA7219_SYSCLK_MCLK, 0, 0);
101 			if (ret < 0) {
102 				dev_err(rtd->dev, "failed to stop PLL: %d\n",
103 					ret);
104 				break;
105 			}
106 		}
107 	}
108 
109 	return ret;
110 }
111 
112 static const struct snd_soc_ops mt8183_da7219_i2s_ops = {
113 	.hw_params = mt8183_da7219_i2s_hw_params,
114 	.hw_free = mt8183_da7219_hw_free,
115 };
116 
117 static int
118 mt8183_da7219_rt1015_i2s_hw_params(struct snd_pcm_substream *substream,
119 				   struct snd_pcm_hw_params *params)
120 {
121 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
122 	unsigned int rate = params_rate(params);
123 	struct snd_soc_dai *codec_dai;
124 	int ret = 0, i;
125 
126 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
127 		if (!strcmp(codec_dai->component->name, RT1015_DEV0_NAME) ||
128 		    !strcmp(codec_dai->component->name, RT1015_DEV1_NAME)) {
129 			ret = snd_soc_dai_set_bclk_ratio(codec_dai, 64);
130 			if (ret) {
131 				dev_err(rtd->dev, "failed to set bclk ratio\n");
132 				return ret;
133 			}
134 
135 			ret = snd_soc_dai_set_pll(codec_dai, 0,
136 						  RT1015_PLL_S_BCLK,
137 						  rate * 64, rate * 256);
138 			if (ret) {
139 				dev_err(rtd->dev, "failed to set pll\n");
140 				return ret;
141 			}
142 
143 			ret = snd_soc_dai_set_sysclk(codec_dai,
144 						     RT1015_SCLK_S_PLL,
145 						     rate * 256,
146 						     SND_SOC_CLOCK_IN);
147 			if (ret) {
148 				dev_err(rtd->dev, "failed to set sysclk\n");
149 				return ret;
150 			}
151 		}
152 	}
153 
154 	return mt8183_da7219_i2s_hw_params(substream, params);
155 }
156 
157 static const struct snd_soc_ops mt8183_da7219_rt1015_i2s_ops = {
158 	.hw_params = mt8183_da7219_rt1015_i2s_hw_params,
159 	.hw_free = mt8183_da7219_hw_free,
160 };
161 
162 static int mt8183_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
163 				      struct snd_pcm_hw_params *params)
164 {
165 	/* fix BE i2s format to 32bit, clean param mask first */
166 	snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
167 			     0, SNDRV_PCM_FORMAT_LAST);
168 
169 	params_set_format(params, SNDRV_PCM_FORMAT_S32_LE);
170 
171 	return 0;
172 }
173 
174 static int mt8183_rt1015_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
175 					     struct snd_pcm_hw_params *params)
176 {
177 	/* fix BE i2s format to 32bit, clean param mask first */
178 	snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
179 			     0, SNDRV_PCM_FORMAT_LAST);
180 
181 	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
182 
183 	return 0;
184 }
185 
186 static int
187 mt8183_da7219_max98357_startup(
188 	struct snd_pcm_substream *substream)
189 {
190 	static const unsigned int rates[] = {
191 		48000,
192 	};
193 	static const struct snd_pcm_hw_constraint_list constraints_rates = {
194 		.count = ARRAY_SIZE(rates),
195 		.list  = rates,
196 		.mask = 0,
197 	};
198 	static const unsigned int channels[] = {
199 		2,
200 	};
201 	static const struct snd_pcm_hw_constraint_list constraints_channels = {
202 		.count = ARRAY_SIZE(channels),
203 		.list = channels,
204 		.mask = 0,
205 	};
206 
207 	struct snd_pcm_runtime *runtime = substream->runtime;
208 
209 	snd_pcm_hw_constraint_list(runtime, 0,
210 			SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
211 	runtime->hw.channels_max = 2;
212 	snd_pcm_hw_constraint_list(runtime, 0,
213 			SNDRV_PCM_HW_PARAM_CHANNELS,
214 			&constraints_channels);
215 
216 	runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
217 	snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
218 
219 	return 0;
220 }
221 
222 static const struct snd_soc_ops mt8183_da7219_max98357_ops = {
223 	.startup = mt8183_da7219_max98357_startup,
224 };
225 
226 static int
227 mt8183_da7219_max98357_bt_sco_startup(
228 	struct snd_pcm_substream *substream)
229 {
230 	static const unsigned int rates[] = {
231 		8000, 16000
232 	};
233 	static const struct snd_pcm_hw_constraint_list constraints_rates = {
234 		.count = ARRAY_SIZE(rates),
235 		.list  = rates,
236 		.mask = 0,
237 	};
238 	static const unsigned int channels[] = {
239 		1,
240 	};
241 	static const struct snd_pcm_hw_constraint_list constraints_channels = {
242 		.count = ARRAY_SIZE(channels),
243 		.list = channels,
244 		.mask = 0,
245 	};
246 
247 	struct snd_pcm_runtime *runtime = substream->runtime;
248 
249 	snd_pcm_hw_constraint_list(runtime, 0,
250 			SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
251 	runtime->hw.channels_max = 1;
252 	snd_pcm_hw_constraint_list(runtime, 0,
253 			SNDRV_PCM_HW_PARAM_CHANNELS,
254 			&constraints_channels);
255 
256 	runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
257 	snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
258 
259 	return 0;
260 }
261 
262 static const struct snd_soc_ops mt8183_da7219_max98357_bt_sco_ops = {
263 	.startup = mt8183_da7219_max98357_bt_sco_startup,
264 };
265 
266 /* FE */
267 SND_SOC_DAILINK_DEFS(playback1,
268 	DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
269 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
270 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
271 
272 SND_SOC_DAILINK_DEFS(playback2,
273 	DAILINK_COMP_ARRAY(COMP_CPU("DL2")),
274 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
275 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
276 
277 SND_SOC_DAILINK_DEFS(playback3,
278 	DAILINK_COMP_ARRAY(COMP_CPU("DL3")),
279 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
280 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
281 
282 SND_SOC_DAILINK_DEFS(capture1,
283 	DAILINK_COMP_ARRAY(COMP_CPU("UL1")),
284 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
285 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
286 
287 SND_SOC_DAILINK_DEFS(capture2,
288 	DAILINK_COMP_ARRAY(COMP_CPU("UL2")),
289 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
290 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
291 
292 SND_SOC_DAILINK_DEFS(capture3,
293 	DAILINK_COMP_ARRAY(COMP_CPU("UL3")),
294 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
295 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
296 
297 SND_SOC_DAILINK_DEFS(capture_mono,
298 	DAILINK_COMP_ARRAY(COMP_CPU("UL_MONO_1")),
299 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
300 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
301 
302 SND_SOC_DAILINK_DEFS(playback_hdmi,
303 	DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
304 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
305 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
306 
307 /* BE */
308 SND_SOC_DAILINK_DEFS(primary_codec,
309 	DAILINK_COMP_ARRAY(COMP_CPU("ADDA")),
310 	DAILINK_COMP_ARRAY(COMP_CODEC("mt6358-sound", "mt6358-snd-codec-aif1")),
311 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
312 
313 SND_SOC_DAILINK_DEFS(pcm1,
314 	DAILINK_COMP_ARRAY(COMP_CPU("PCM 1")),
315 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
316 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
317 
318 SND_SOC_DAILINK_DEFS(pcm2,
319 	DAILINK_COMP_ARRAY(COMP_CPU("PCM 2")),
320 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
321 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
322 
323 SND_SOC_DAILINK_DEFS(i2s0,
324 	DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
325 	DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")),
326 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
327 
328 SND_SOC_DAILINK_DEFS(i2s1,
329 	DAILINK_COMP_ARRAY(COMP_CPU("I2S1")),
330 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
331 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
332 
333 SND_SOC_DAILINK_DEFS(i2s2,
334 	DAILINK_COMP_ARRAY(COMP_CPU("I2S2")),
335 	DAILINK_COMP_ARRAY(COMP_CODEC(DA7219_DEV_NAME, DA7219_CODEC_DAI)),
336 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
337 
338 SND_SOC_DAILINK_DEFS(i2s3_max98357a,
339 	DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
340 	DAILINK_COMP_ARRAY(COMP_CODEC("max98357a", "HiFi"),
341 			   COMP_CODEC(DA7219_DEV_NAME, DA7219_CODEC_DAI)),
342 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
343 
344 SND_SOC_DAILINK_DEFS(i2s3_rt1015,
345 	DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
346 	DAILINK_COMP_ARRAY(COMP_CODEC(RT1015_DEV0_NAME, RT1015_CODEC_DAI),
347 			   COMP_CODEC(RT1015_DEV1_NAME, RT1015_CODEC_DAI),
348 			   COMP_CODEC(DA7219_DEV_NAME, DA7219_CODEC_DAI)),
349 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
350 
351 SND_SOC_DAILINK_DEFS(i2s3_rt1015p,
352 	DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
353 	DAILINK_COMP_ARRAY(COMP_CODEC("rt1015p", "HiFi"),
354 			   COMP_CODEC(DA7219_DEV_NAME, DA7219_CODEC_DAI)),
355 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
356 
357 SND_SOC_DAILINK_DEFS(i2s5,
358 	DAILINK_COMP_ARRAY(COMP_CPU("I2S5")),
359 	DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")),
360 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
361 
362 SND_SOC_DAILINK_DEFS(tdm,
363 	DAILINK_COMP_ARRAY(COMP_CPU("TDM")),
364 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
365 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
366 
367 static int mt8183_da7219_max98357_hdmi_init(struct snd_soc_pcm_runtime *rtd)
368 {
369 	struct mt8183_da7219_max98357_priv *priv =
370 		snd_soc_card_get_drvdata(rtd->card);
371 	int ret;
372 
373 	ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT,
374 				    &priv->hdmi_jack, NULL, 0);
375 	if (ret)
376 		return ret;
377 
378 	return hdmi_codec_set_jack_detect(asoc_rtd_to_codec(rtd, 0)->component,
379 					  &priv->hdmi_jack);
380 }
381 
382 static struct snd_soc_dai_link mt8183_da7219_dai_links[] = {
383 	/* FE */
384 	{
385 		.name = "Playback_1",
386 		.stream_name = "Playback_1",
387 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
388 			    SND_SOC_DPCM_TRIGGER_PRE},
389 		.dynamic = 1,
390 		.dpcm_playback = 1,
391 		.ops = &mt8183_da7219_max98357_ops,
392 		SND_SOC_DAILINK_REG(playback1),
393 	},
394 	{
395 		.name = "Playback_2",
396 		.stream_name = "Playback_2",
397 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
398 			    SND_SOC_DPCM_TRIGGER_PRE},
399 		.dynamic = 1,
400 		.dpcm_playback = 1,
401 		.ops = &mt8183_da7219_max98357_bt_sco_ops,
402 		SND_SOC_DAILINK_REG(playback2),
403 	},
404 	{
405 		.name = "Playback_3",
406 		.stream_name = "Playback_3",
407 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
408 			    SND_SOC_DPCM_TRIGGER_PRE},
409 		.dynamic = 1,
410 		.dpcm_playback = 1,
411 		SND_SOC_DAILINK_REG(playback3),
412 	},
413 	{
414 		.name = "Capture_1",
415 		.stream_name = "Capture_1",
416 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
417 			    SND_SOC_DPCM_TRIGGER_PRE},
418 		.dynamic = 1,
419 		.dpcm_capture = 1,
420 		.ops = &mt8183_da7219_max98357_bt_sco_ops,
421 		SND_SOC_DAILINK_REG(capture1),
422 	},
423 	{
424 		.name = "Capture_2",
425 		.stream_name = "Capture_2",
426 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
427 			    SND_SOC_DPCM_TRIGGER_PRE},
428 		.dynamic = 1,
429 		.dpcm_capture = 1,
430 		SND_SOC_DAILINK_REG(capture2),
431 	},
432 	{
433 		.name = "Capture_3",
434 		.stream_name = "Capture_3",
435 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
436 			    SND_SOC_DPCM_TRIGGER_PRE},
437 		.dynamic = 1,
438 		.dpcm_capture = 1,
439 		.ops = &mt8183_da7219_max98357_ops,
440 		SND_SOC_DAILINK_REG(capture3),
441 	},
442 	{
443 		.name = "Capture_Mono_1",
444 		.stream_name = "Capture_Mono_1",
445 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
446 			    SND_SOC_DPCM_TRIGGER_PRE},
447 		.dynamic = 1,
448 		.dpcm_capture = 1,
449 		SND_SOC_DAILINK_REG(capture_mono),
450 	},
451 	{
452 		.name = "Playback_HDMI",
453 		.stream_name = "Playback_HDMI",
454 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
455 			    SND_SOC_DPCM_TRIGGER_PRE},
456 		.dynamic = 1,
457 		.dpcm_playback = 1,
458 		SND_SOC_DAILINK_REG(playback_hdmi),
459 	},
460 	/* BE */
461 	{
462 		.name = "Primary Codec",
463 		.no_pcm = 1,
464 		.dpcm_playback = 1,
465 		.dpcm_capture = 1,
466 		.ignore_suspend = 1,
467 		SND_SOC_DAILINK_REG(primary_codec),
468 	},
469 	{
470 		.name = "PCM 1",
471 		.no_pcm = 1,
472 		.dpcm_playback = 1,
473 		.dpcm_capture = 1,
474 		.ignore_suspend = 1,
475 		SND_SOC_DAILINK_REG(pcm1),
476 	},
477 	{
478 		.name = "PCM 2",
479 		.no_pcm = 1,
480 		.dpcm_playback = 1,
481 		.dpcm_capture = 1,
482 		.ignore_suspend = 1,
483 		SND_SOC_DAILINK_REG(pcm2),
484 	},
485 	{
486 		.name = "I2S0",
487 		.no_pcm = 1,
488 		.dpcm_capture = 1,
489 		.ignore_suspend = 1,
490 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
491 		.ops = &mt8183_mt6358_i2s_ops,
492 		SND_SOC_DAILINK_REG(i2s0),
493 	},
494 	{
495 		.name = "I2S1",
496 		.no_pcm = 1,
497 		.dpcm_playback = 1,
498 		.ignore_suspend = 1,
499 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
500 		.ops = &mt8183_mt6358_i2s_ops,
501 		SND_SOC_DAILINK_REG(i2s1),
502 	},
503 	{
504 		.name = "I2S2",
505 		.no_pcm = 1,
506 		.dpcm_capture = 1,
507 		.ignore_suspend = 1,
508 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
509 		.ops = &mt8183_da7219_i2s_ops,
510 		SND_SOC_DAILINK_REG(i2s2),
511 	},
512 	{
513 		.name = "I2S3",
514 		.no_pcm = 1,
515 		.dpcm_playback = 1,
516 		.ignore_suspend = 1,
517 	},
518 	{
519 		.name = "I2S5",
520 		.no_pcm = 1,
521 		.dpcm_playback = 1,
522 		.ignore_suspend = 1,
523 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
524 		.ops = &mt8183_mt6358_i2s_ops,
525 		SND_SOC_DAILINK_REG(i2s5),
526 	},
527 	{
528 		.name = "TDM",
529 		.no_pcm = 1,
530 		.dai_fmt = SND_SOC_DAIFMT_I2S |
531 			   SND_SOC_DAIFMT_IB_IF |
532 			   SND_SOC_DAIFMT_CBM_CFM,
533 		.dpcm_playback = 1,
534 		.ignore_suspend = 1,
535 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
536 		.init = mt8183_da7219_max98357_hdmi_init,
537 		SND_SOC_DAILINK_REG(tdm),
538 	},
539 };
540 
541 static int
542 mt8183_da7219_max98357_headset_init(struct snd_soc_component *component)
543 {
544 	int ret;
545 	struct mt8183_da7219_max98357_priv *priv =
546 			snd_soc_card_get_drvdata(component->card);
547 
548 	/* Enable Headset and 4 Buttons Jack detection */
549 	ret = snd_soc_card_jack_new(component->card,
550 				    "Headset Jack",
551 				    SND_JACK_HEADSET |
552 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
553 				    SND_JACK_BTN_2 | SND_JACK_BTN_3,
554 				    &priv->headset_jack,
555 				    NULL, 0);
556 	if (ret)
557 		return ret;
558 
559 	snd_jack_set_key(
560 		priv->headset_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
561 	snd_jack_set_key(
562 		priv->headset_jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
563 	snd_jack_set_key(
564 		priv->headset_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
565 	snd_jack_set_key(
566 		priv->headset_jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
567 
568 	da7219_aad_jack_det(component, &priv->headset_jack);
569 
570 	return 0;
571 }
572 
573 static struct snd_soc_aux_dev mt8183_da7219_max98357_headset_dev = {
574 	.dlc = COMP_EMPTY(),
575 	.init = mt8183_da7219_max98357_headset_init,
576 };
577 
578 static struct snd_soc_codec_conf mt6358_codec_conf[] = {
579 	{
580 		.dlc = COMP_CODEC_CONF("mt6358-sound"),
581 		.name_prefix = "Mt6358",
582 	},
583 };
584 
585 static const struct snd_kcontrol_new mt8183_da7219_max98357_snd_controls[] = {
586 	SOC_DAPM_PIN_SWITCH("Speakers"),
587 };
588 
589 static const
590 struct snd_soc_dapm_widget mt8183_da7219_max98357_dapm_widgets[] = {
591 	SND_SOC_DAPM_SPK("Speakers", NULL),
592 	SND_SOC_DAPM_PINCTRL("TDM_OUT_PINCTRL",
593 			     "aud_tdm_out_on", "aud_tdm_out_off"),
594 };
595 
596 static const struct snd_soc_dapm_route mt8183_da7219_max98357_dapm_routes[] = {
597 	{"Speakers", NULL, "Speaker"},
598 	{"I2S Playback", NULL, "TDM_OUT_PINCTRL"},
599 };
600 
601 static struct snd_soc_card mt8183_da7219_max98357_card = {
602 	.name = "mt8183_da7219_max98357",
603 	.owner = THIS_MODULE,
604 	.controls = mt8183_da7219_max98357_snd_controls,
605 	.num_controls = ARRAY_SIZE(mt8183_da7219_max98357_snd_controls),
606 	.dapm_widgets = mt8183_da7219_max98357_dapm_widgets,
607 	.num_dapm_widgets = ARRAY_SIZE(mt8183_da7219_max98357_dapm_widgets),
608 	.dapm_routes = mt8183_da7219_max98357_dapm_routes,
609 	.num_dapm_routes = ARRAY_SIZE(mt8183_da7219_max98357_dapm_routes),
610 	.dai_link = mt8183_da7219_dai_links,
611 	.num_links = ARRAY_SIZE(mt8183_da7219_dai_links),
612 	.aux_dev = &mt8183_da7219_max98357_headset_dev,
613 	.num_aux_devs = 1,
614 	.codec_conf = mt6358_codec_conf,
615 	.num_configs = ARRAY_SIZE(mt6358_codec_conf),
616 };
617 
618 static struct snd_soc_codec_conf mt8183_da7219_rt1015_codec_conf[] = {
619 	{
620 		.dlc = COMP_CODEC_CONF("mt6358-sound"),
621 		.name_prefix = "Mt6358",
622 	},
623 	{
624 		.dlc = COMP_CODEC_CONF(RT1015_DEV0_NAME),
625 		.name_prefix = "Left",
626 	},
627 	{
628 		.dlc = COMP_CODEC_CONF(RT1015_DEV1_NAME),
629 		.name_prefix = "Right",
630 	},
631 };
632 
633 static struct snd_soc_card mt8183_da7219_rt1015_card = {
634 	.name = "mt8183_da7219_rt1015",
635 	.owner = THIS_MODULE,
636 	.controls = mt8183_da7219_max98357_snd_controls,
637 	.num_controls = ARRAY_SIZE(mt8183_da7219_max98357_snd_controls),
638 	.dapm_widgets = mt8183_da7219_max98357_dapm_widgets,
639 	.num_dapm_widgets = ARRAY_SIZE(mt8183_da7219_max98357_dapm_widgets),
640 	.dapm_routes = mt8183_da7219_max98357_dapm_routes,
641 	.num_dapm_routes = ARRAY_SIZE(mt8183_da7219_max98357_dapm_routes),
642 	.dai_link = mt8183_da7219_dai_links,
643 	.num_links = ARRAY_SIZE(mt8183_da7219_dai_links),
644 	.aux_dev = &mt8183_da7219_max98357_headset_dev,
645 	.num_aux_devs = 1,
646 	.codec_conf = mt8183_da7219_rt1015_codec_conf,
647 	.num_configs = ARRAY_SIZE(mt8183_da7219_rt1015_codec_conf),
648 };
649 
650 static struct snd_soc_card mt8183_da7219_rt1015p_card = {
651 	.name = "mt8183_da7219_rt1015p",
652 	.owner = THIS_MODULE,
653 	.controls = mt8183_da7219_max98357_snd_controls,
654 	.num_controls = ARRAY_SIZE(mt8183_da7219_max98357_snd_controls),
655 	.dapm_widgets = mt8183_da7219_max98357_dapm_widgets,
656 	.num_dapm_widgets = ARRAY_SIZE(mt8183_da7219_max98357_dapm_widgets),
657 	.dapm_routes = mt8183_da7219_max98357_dapm_routes,
658 	.num_dapm_routes = ARRAY_SIZE(mt8183_da7219_max98357_dapm_routes),
659 	.dai_link = mt8183_da7219_dai_links,
660 	.num_links = ARRAY_SIZE(mt8183_da7219_dai_links),
661 	.aux_dev = &mt8183_da7219_max98357_headset_dev,
662 	.num_aux_devs = 1,
663 	.codec_conf = mt6358_codec_conf,
664 	.num_configs = ARRAY_SIZE(mt6358_codec_conf),
665 };
666 
667 static int mt8183_da7219_max98357_dev_probe(struct platform_device *pdev)
668 {
669 	struct snd_soc_card *card;
670 	struct device_node *platform_node, *hdmi_codec;
671 	struct snd_soc_dai_link *dai_link;
672 	struct mt8183_da7219_max98357_priv *priv;
673 	struct pinctrl *pinctrl;
674 	const struct of_device_id *match;
675 	int ret, i;
676 
677 	platform_node = of_parse_phandle(pdev->dev.of_node,
678 					 "mediatek,platform", 0);
679 	if (!platform_node) {
680 		dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
681 		return -EINVAL;
682 	}
683 
684 	match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
685 	if (!match || !match->data)
686 		return -EINVAL;
687 
688 	card = (struct snd_soc_card *)match->data;
689 	card->dev = &pdev->dev;
690 
691 	hdmi_codec = of_parse_phandle(pdev->dev.of_node,
692 				      "mediatek,hdmi-codec", 0);
693 
694 	for_each_card_prelinks(card, i, dai_link) {
695 		if (strcmp(dai_link->name, "I2S3") == 0) {
696 			if (card == &mt8183_da7219_max98357_card) {
697 				dai_link->be_hw_params_fixup =
698 					mt8183_i2s_hw_params_fixup;
699 				dai_link->ops = &mt8183_mt6358_i2s_ops;
700 				dai_link->cpus = i2s3_max98357a_cpus;
701 				dai_link->num_cpus =
702 					ARRAY_SIZE(i2s3_max98357a_cpus);
703 				dai_link->codecs = i2s3_max98357a_codecs;
704 				dai_link->num_codecs =
705 					ARRAY_SIZE(i2s3_max98357a_codecs);
706 				dai_link->platforms = i2s3_max98357a_platforms;
707 				dai_link->num_platforms =
708 					ARRAY_SIZE(i2s3_max98357a_platforms);
709 			} else if (card == &mt8183_da7219_rt1015_card) {
710 				dai_link->be_hw_params_fixup =
711 					mt8183_rt1015_i2s_hw_params_fixup;
712 				dai_link->ops = &mt8183_da7219_rt1015_i2s_ops;
713 				dai_link->cpus = i2s3_rt1015_cpus;
714 				dai_link->num_cpus =
715 					ARRAY_SIZE(i2s3_rt1015_cpus);
716 				dai_link->codecs = i2s3_rt1015_codecs;
717 				dai_link->num_codecs =
718 					ARRAY_SIZE(i2s3_rt1015_codecs);
719 				dai_link->platforms = i2s3_rt1015_platforms;
720 				dai_link->num_platforms =
721 					ARRAY_SIZE(i2s3_rt1015_platforms);
722 			} else if (card == &mt8183_da7219_rt1015p_card) {
723 				dai_link->be_hw_params_fixup =
724 					mt8183_rt1015_i2s_hw_params_fixup;
725 				dai_link->ops = &mt8183_da7219_i2s_ops;
726 				dai_link->cpus = i2s3_rt1015p_cpus;
727 				dai_link->num_cpus =
728 					ARRAY_SIZE(i2s3_rt1015p_cpus);
729 				dai_link->codecs = i2s3_rt1015p_codecs;
730 				dai_link->num_codecs =
731 					ARRAY_SIZE(i2s3_rt1015p_codecs);
732 				dai_link->platforms = i2s3_rt1015p_platforms;
733 				dai_link->num_platforms =
734 					ARRAY_SIZE(i2s3_rt1015p_platforms);
735 			}
736 		}
737 
738 		if (hdmi_codec && strcmp(dai_link->name, "TDM") == 0)
739 			dai_link->codecs->of_node = hdmi_codec;
740 
741 		if (!dai_link->platforms->name)
742 			dai_link->platforms->of_node = platform_node;
743 	}
744 
745 	mt8183_da7219_max98357_headset_dev.dlc.of_node =
746 		of_parse_phandle(pdev->dev.of_node,
747 				 "mediatek,headset-codec", 0);
748 	if (!mt8183_da7219_max98357_headset_dev.dlc.of_node) {
749 		dev_err(&pdev->dev,
750 			"Property 'mediatek,headset-codec' missing/invalid\n");
751 		return -EINVAL;
752 	}
753 
754 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
755 	if (!priv)
756 		return -ENOMEM;
757 
758 	snd_soc_card_set_drvdata(card, priv);
759 
760 	pinctrl = devm_pinctrl_get_select(&pdev->dev, PINCTRL_STATE_DEFAULT);
761 	if (IS_ERR(pinctrl)) {
762 		ret = PTR_ERR(pinctrl);
763 		dev_err(&pdev->dev, "%s failed to select default state %d\n",
764 			__func__, ret);
765 		return ret;
766 	}
767 
768 	return devm_snd_soc_register_card(&pdev->dev, card);
769 }
770 
771 #ifdef CONFIG_OF
772 static const struct of_device_id mt8183_da7219_max98357_dt_match[] = {
773 	{
774 		.compatible = "mediatek,mt8183_da7219_max98357",
775 		.data = &mt8183_da7219_max98357_card,
776 	},
777 	{
778 		.compatible = "mediatek,mt8183_da7219_rt1015",
779 		.data = &mt8183_da7219_rt1015_card,
780 	},
781 	{
782 		.compatible = "mediatek,mt8183_da7219_rt1015p",
783 		.data = &mt8183_da7219_rt1015p_card,
784 	},
785 	{}
786 };
787 #endif
788 
789 static struct platform_driver mt8183_da7219_max98357_driver = {
790 	.driver = {
791 		.name = "mt8183_da7219",
792 #ifdef CONFIG_OF
793 		.of_match_table = mt8183_da7219_max98357_dt_match,
794 #endif
795 	},
796 	.probe = mt8183_da7219_max98357_dev_probe,
797 };
798 
799 module_platform_driver(mt8183_da7219_max98357_driver);
800 
801 /* Module information */
802 MODULE_DESCRIPTION("MT8183-DA7219-MAX98357 ALSA SoC machine driver");
803 MODULE_AUTHOR("Shunli Wang <shunli.wang@mediatek.com>");
804 MODULE_LICENSE("GPL v2");
805 MODULE_ALIAS("mt8183_da7219_max98357 soc card");
806