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