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