xref: /linux/sound/soc/mediatek/mt8183/mt8183-mt6358-ts3a227-max98357.c (revision de96bd7b7e9b4cf855fb6d1c7ce32a15dfbdfd92)
111c02690SShunli Wang // SPDX-License-Identifier: GPL-2.0
211c02690SShunli Wang //
311c02690SShunli Wang // mt8183-mt6358.c  --
411c02690SShunli Wang //	MT8183-MT6358-TS3A227-MAX98357 ALSA SoC machine driver
511c02690SShunli Wang //
611c02690SShunli Wang // Copyright (c) 2018 MediaTek Inc.
711c02690SShunli Wang // Author: Shunli Wang <shunli.wang@mediatek.com>
811c02690SShunli Wang 
911c02690SShunli Wang #include <linux/module.h>
10f4fb4fefSTzung-Bi Shih #include <linux/of_device.h>
114dae01c2STzung-Bi Shih #include <linux/pinctrl/consumer.h>
124dae01c2STzung-Bi Shih #include <sound/jack.h>
1311c02690SShunli Wang #include <sound/pcm_params.h>
1411c02690SShunli Wang #include <sound/soc.h>
1511c02690SShunli Wang 
16f4fb4fefSTzung-Bi Shih #include "../../codecs/rt1015.h"
1711c02690SShunli Wang #include "../../codecs/ts3a227e.h"
184dae01c2STzung-Bi Shih #include "mt8183-afe-common.h"
1911c02690SShunli Wang 
20f4fb4fefSTzung-Bi Shih #define RT1015_CODEC_DAI "rt1015-aif"
21f4fb4fefSTzung-Bi Shih #define RT1015_DEV0_NAME "rt1015.6-0028"
22f4fb4fefSTzung-Bi Shih #define RT1015_DEV1_NAME "rt1015.6-0029"
23f4fb4fefSTzung-Bi Shih 
2496ed7698STzung-Bi Shih enum PINCTRL_PIN_STATE {
2596ed7698STzung-Bi Shih 	PIN_STATE_DEFAULT = 0,
2696ed7698STzung-Bi Shih 	PIN_TDM_OUT_ON,
2796ed7698STzung-Bi Shih 	PIN_TDM_OUT_OFF,
282cc3cd5fSTzung-Bi Shih 	PIN_WOV,
2996ed7698STzung-Bi Shih 	PIN_STATE_MAX
3096ed7698STzung-Bi Shih };
3196ed7698STzung-Bi Shih 
3296ed7698STzung-Bi Shih static const char * const mt8183_pin_str[PIN_STATE_MAX] = {
332cc3cd5fSTzung-Bi Shih 	"default", "aud_tdm_out_on", "aud_tdm_out_off", "wov",
3496ed7698STzung-Bi Shih };
3596ed7698STzung-Bi Shih 
3696ed7698STzung-Bi Shih struct mt8183_mt6358_ts3a227_max98357_priv {
3796ed7698STzung-Bi Shih 	struct pinctrl *pinctrl;
3896ed7698STzung-Bi Shih 	struct pinctrl_state *pin_states[PIN_STATE_MAX];
3988abbf62STzung-Bi Shih 	struct snd_soc_jack headset_jack, hdmi_jack;
4096ed7698STzung-Bi Shih };
4196ed7698STzung-Bi Shih 
4211c02690SShunli Wang static int mt8183_mt6358_i2s_hw_params(struct snd_pcm_substream *substream,
4311c02690SShunli Wang 				       struct snd_pcm_hw_params *params)
4411c02690SShunli Wang {
450cd08b10SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
4611c02690SShunli Wang 	unsigned int rate = params_rate(params);
4711c02690SShunli Wang 	unsigned int mclk_fs_ratio = 128;
4811c02690SShunli Wang 	unsigned int mclk_fs = rate * mclk_fs_ratio;
4911c02690SShunli Wang 
50c8ac8212SKuninori Morimoto 	return snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0),
5111c02690SShunli Wang 				      0, mclk_fs, SND_SOC_CLOCK_OUT);
5211c02690SShunli Wang }
5311c02690SShunli Wang 
5411c02690SShunli Wang static const struct snd_soc_ops mt8183_mt6358_i2s_ops = {
5511c02690SShunli Wang 	.hw_params = mt8183_mt6358_i2s_hw_params,
5611c02690SShunli Wang };
5711c02690SShunli Wang 
58f4fb4fefSTzung-Bi Shih static int
59f4fb4fefSTzung-Bi Shih mt8183_mt6358_rt1015_i2s_hw_params(struct snd_pcm_substream *substream,
60f4fb4fefSTzung-Bi Shih 				   struct snd_pcm_hw_params *params)
61f4fb4fefSTzung-Bi Shih {
620cd08b10SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
63f4fb4fefSTzung-Bi Shih 	unsigned int rate = params_rate(params);
64f4fb4fefSTzung-Bi Shih 	unsigned int mclk_fs_ratio = 128;
65f4fb4fefSTzung-Bi Shih 	unsigned int mclk_fs = rate * mclk_fs_ratio;
66f4fb4fefSTzung-Bi Shih 	struct snd_soc_card *card = rtd->card;
67f4fb4fefSTzung-Bi Shih 	struct snd_soc_dai *codec_dai;
68f4fb4fefSTzung-Bi Shih 	int ret, i;
69f4fb4fefSTzung-Bi Shih 
70f4fb4fefSTzung-Bi Shih 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
71f4fb4fefSTzung-Bi Shih 		ret = snd_soc_dai_set_bclk_ratio(codec_dai, 64);
72f4fb4fefSTzung-Bi Shih 		if (ret < 0) {
73f4fb4fefSTzung-Bi Shih 			dev_err(card->dev, "failed to set bclk ratio\n");
74f4fb4fefSTzung-Bi Shih 			return ret;
75f4fb4fefSTzung-Bi Shih 		}
76f4fb4fefSTzung-Bi Shih 
77f4fb4fefSTzung-Bi Shih 		ret = snd_soc_dai_set_pll(codec_dai, 0, RT1015_PLL_S_BCLK,
78f4fb4fefSTzung-Bi Shih 				rate * 64, rate * 256);
79f4fb4fefSTzung-Bi Shih 		if (ret < 0) {
80f4fb4fefSTzung-Bi Shih 			dev_err(card->dev, "failed to set pll\n");
81f4fb4fefSTzung-Bi Shih 			return ret;
82f4fb4fefSTzung-Bi Shih 		}
83f4fb4fefSTzung-Bi Shih 
84f4fb4fefSTzung-Bi Shih 		ret = snd_soc_dai_set_sysclk(codec_dai, RT1015_SCLK_S_PLL,
85f4fb4fefSTzung-Bi Shih 				rate * 256, SND_SOC_CLOCK_IN);
86f4fb4fefSTzung-Bi Shih 		if (ret < 0) {
87f4fb4fefSTzung-Bi Shih 			dev_err(card->dev, "failed to set sysclk\n");
88f4fb4fefSTzung-Bi Shih 			return ret;
89f4fb4fefSTzung-Bi Shih 		}
90f4fb4fefSTzung-Bi Shih 	}
91f4fb4fefSTzung-Bi Shih 
92f4fb4fefSTzung-Bi Shih 	return snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0),
93f4fb4fefSTzung-Bi Shih 				      0, mclk_fs, SND_SOC_CLOCK_OUT);
94f4fb4fefSTzung-Bi Shih }
95f4fb4fefSTzung-Bi Shih 
96f4fb4fefSTzung-Bi Shih static const struct snd_soc_ops mt8183_mt6358_rt1015_i2s_ops = {
97f4fb4fefSTzung-Bi Shih 	.hw_params = mt8183_mt6358_rt1015_i2s_hw_params,
98f4fb4fefSTzung-Bi Shih };
99f4fb4fefSTzung-Bi Shih 
10011c02690SShunli Wang static int mt8183_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
10111c02690SShunli Wang 				      struct snd_pcm_hw_params *params)
10211c02690SShunli Wang {
10311c02690SShunli Wang 	dev_dbg(rtd->dev, "%s(), fix format to 32bit\n", __func__);
10411c02690SShunli Wang 
10511c02690SShunli Wang 	/* fix BE i2s format to 32bit, clean param mask first */
10611c02690SShunli Wang 	snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
10711c02690SShunli Wang 			     0, SNDRV_PCM_FORMAT_LAST);
10811c02690SShunli Wang 
10911c02690SShunli Wang 	params_set_format(params, SNDRV_PCM_FORMAT_S32_LE);
11011c02690SShunli Wang 	return 0;
11111c02690SShunli Wang }
11211c02690SShunli Wang 
113f4fb4fefSTzung-Bi Shih static int mt8183_rt1015_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
114f4fb4fefSTzung-Bi Shih 					     struct snd_pcm_hw_params *params)
115f4fb4fefSTzung-Bi Shih {
116f4fb4fefSTzung-Bi Shih 	dev_dbg(rtd->dev, "%s(), fix format to 32bit\n", __func__);
117f4fb4fefSTzung-Bi Shih 
118f4fb4fefSTzung-Bi Shih 	/* fix BE i2s format to 32bit, clean param mask first */
119f4fb4fefSTzung-Bi Shih 	snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
120f4fb4fefSTzung-Bi Shih 			     0, SNDRV_PCM_FORMAT_LAST);
121f4fb4fefSTzung-Bi Shih 
122f4fb4fefSTzung-Bi Shih 	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
123f4fb4fefSTzung-Bi Shih 	return 0;
124f4fb4fefSTzung-Bi Shih }
125f4fb4fefSTzung-Bi Shih 
1261df1e545STzung-Bi Shih static int
1271df1e545STzung-Bi Shih mt8183_mt6358_ts3a227_max98357_bt_sco_startup(
1281df1e545STzung-Bi Shih 	struct snd_pcm_substream *substream)
1291df1e545STzung-Bi Shih {
1301df1e545STzung-Bi Shih 	static const unsigned int rates[] = {
1311df1e545STzung-Bi Shih 		8000, 16000
1321df1e545STzung-Bi Shih 	};
1331df1e545STzung-Bi Shih 	static const struct snd_pcm_hw_constraint_list constraints_rates = {
1341df1e545STzung-Bi Shih 		.count = ARRAY_SIZE(rates),
1351df1e545STzung-Bi Shih 		.list  = rates,
1361df1e545STzung-Bi Shih 		.mask = 0,
1371df1e545STzung-Bi Shih 	};
1381df1e545STzung-Bi Shih 	static const unsigned int channels[] = {
1391df1e545STzung-Bi Shih 		1,
1401df1e545STzung-Bi Shih 	};
1411df1e545STzung-Bi Shih 	static const struct snd_pcm_hw_constraint_list constraints_channels = {
1421df1e545STzung-Bi Shih 		.count = ARRAY_SIZE(channels),
1431df1e545STzung-Bi Shih 		.list = channels,
1441df1e545STzung-Bi Shih 		.mask = 0,
1451df1e545STzung-Bi Shih 	};
1461df1e545STzung-Bi Shih 
1471df1e545STzung-Bi Shih 	struct snd_pcm_runtime *runtime = substream->runtime;
1481df1e545STzung-Bi Shih 
1491df1e545STzung-Bi Shih 	snd_pcm_hw_constraint_list(runtime, 0,
1501df1e545STzung-Bi Shih 			SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
1511df1e545STzung-Bi Shih 	runtime->hw.channels_max = 1;
1521df1e545STzung-Bi Shih 	snd_pcm_hw_constraint_list(runtime, 0,
1531df1e545STzung-Bi Shih 			SNDRV_PCM_HW_PARAM_CHANNELS,
1541df1e545STzung-Bi Shih 			&constraints_channels);
1551df1e545STzung-Bi Shih 
1561df1e545STzung-Bi Shih 	runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
1571df1e545STzung-Bi Shih 	snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
1581df1e545STzung-Bi Shih 
1591df1e545STzung-Bi Shih 	return 0;
1601df1e545STzung-Bi Shih }
1611df1e545STzung-Bi Shih 
1621df1e545STzung-Bi Shih static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_bt_sco_ops = {
1631df1e545STzung-Bi Shih 	.startup = mt8183_mt6358_ts3a227_max98357_bt_sco_startup,
1641df1e545STzung-Bi Shih };
1651df1e545STzung-Bi Shih 
166fa284fd0SKuninori Morimoto /* FE */
167fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(playback1,
168fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
169fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
170fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
171fa284fd0SKuninori Morimoto 
172fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(playback2,
173fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("DL2")),
174fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
175fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
176fa284fd0SKuninori Morimoto 
177fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(playback3,
178fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("DL3")),
179fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
180fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
181fa284fd0SKuninori Morimoto 
182fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(capture1,
183fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("UL1")),
184fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
185fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
186fa284fd0SKuninori Morimoto 
187fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(capture2,
188fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("UL2")),
189fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
190fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
191fa284fd0SKuninori Morimoto 
192fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(capture3,
193fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("UL3")),
194fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
195fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
196fa284fd0SKuninori Morimoto 
197fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(capture_mono,
198fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("UL_MONO_1")),
199fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
200fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
201fa284fd0SKuninori Morimoto 
202fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(playback_hdmi,
203fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
204fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
205fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
206fa284fd0SKuninori Morimoto 
2072cc3cd5fSTzung-Bi Shih SND_SOC_DAILINK_DEFS(wake_on_voice,
2082cc3cd5fSTzung-Bi Shih 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
2092cc3cd5fSTzung-Bi Shih 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
2102cc3cd5fSTzung-Bi Shih 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
2112cc3cd5fSTzung-Bi Shih 
212fa284fd0SKuninori Morimoto /* BE */
213fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(primary_codec,
214fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("ADDA")),
215fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CODEC("mt6358-sound", "mt6358-snd-codec-aif1")),
216fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
217fa284fd0SKuninori Morimoto 
218fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(pcm1,
219fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("PCM 1")),
220fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
221fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
222fa284fd0SKuninori Morimoto 
223fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(pcm2,
224fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("PCM 2")),
225fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
226fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
227fa284fd0SKuninori Morimoto 
228fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(i2s0,
229fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
230fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")),
231fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
232fa284fd0SKuninori Morimoto 
233fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(i2s1,
234fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("I2S1")),
235fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
236fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
237fa284fd0SKuninori Morimoto 
238fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(i2s2,
239fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("I2S2")),
240fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
241fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
242fa284fd0SKuninori Morimoto 
243f4fb4fefSTzung-Bi Shih SND_SOC_DAILINK_DEFS(i2s3_max98357a,
244fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
245fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CODEC("max98357a", "HiFi")),
246fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
247fa284fd0SKuninori Morimoto 
248f4fb4fefSTzung-Bi Shih SND_SOC_DAILINK_DEFS(i2s3_rt1015,
249f4fb4fefSTzung-Bi Shih 	DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
250f4fb4fefSTzung-Bi Shih 	DAILINK_COMP_ARRAY(COMP_CODEC(RT1015_DEV0_NAME, RT1015_CODEC_DAI),
251f4fb4fefSTzung-Bi Shih 			   COMP_CODEC(RT1015_DEV1_NAME, RT1015_CODEC_DAI)),
252f4fb4fefSTzung-Bi Shih 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
253f4fb4fefSTzung-Bi Shih 
254fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(i2s5,
255fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("I2S5")),
256fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")),
257fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
258fa284fd0SKuninori Morimoto 
259fa284fd0SKuninori Morimoto SND_SOC_DAILINK_DEFS(tdm,
260fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("TDM")),
261f2024dc5STzung-Bi Shih 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
262fa284fd0SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
263fa284fd0SKuninori Morimoto 
2646191cbdeSJiaxin Yu static int mt8183_mt6358_tdm_startup(struct snd_pcm_substream *substream)
2656191cbdeSJiaxin Yu {
2660cd08b10SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
2676191cbdeSJiaxin Yu 	struct mt8183_mt6358_ts3a227_max98357_priv *priv =
2686191cbdeSJiaxin Yu 		snd_soc_card_get_drvdata(rtd->card);
2696191cbdeSJiaxin Yu 	int ret;
2706191cbdeSJiaxin Yu 
2716191cbdeSJiaxin Yu 	if (IS_ERR(priv->pin_states[PIN_TDM_OUT_ON]))
2726191cbdeSJiaxin Yu 		return PTR_ERR(priv->pin_states[PIN_TDM_OUT_ON]);
2736191cbdeSJiaxin Yu 
2746191cbdeSJiaxin Yu 	ret = pinctrl_select_state(priv->pinctrl,
2756191cbdeSJiaxin Yu 				   priv->pin_states[PIN_TDM_OUT_ON]);
2766191cbdeSJiaxin Yu 	if (ret)
2776191cbdeSJiaxin Yu 		dev_err(rtd->card->dev, "%s failed to select state %d\n",
2786191cbdeSJiaxin Yu 			__func__, ret);
2796191cbdeSJiaxin Yu 
2806191cbdeSJiaxin Yu 	return ret;
2816191cbdeSJiaxin Yu }
2826191cbdeSJiaxin Yu 
2836191cbdeSJiaxin Yu static void mt8183_mt6358_tdm_shutdown(struct snd_pcm_substream *substream)
2846191cbdeSJiaxin Yu {
2850cd08b10SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
2866191cbdeSJiaxin Yu 	struct mt8183_mt6358_ts3a227_max98357_priv *priv =
2876191cbdeSJiaxin Yu 		snd_soc_card_get_drvdata(rtd->card);
2886191cbdeSJiaxin Yu 	int ret;
2896191cbdeSJiaxin Yu 
2906191cbdeSJiaxin Yu 	if (IS_ERR(priv->pin_states[PIN_TDM_OUT_OFF]))
2916191cbdeSJiaxin Yu 		return;
2926191cbdeSJiaxin Yu 
2936191cbdeSJiaxin Yu 	ret = pinctrl_select_state(priv->pinctrl,
2946191cbdeSJiaxin Yu 				   priv->pin_states[PIN_TDM_OUT_OFF]);
2956191cbdeSJiaxin Yu 	if (ret)
2966191cbdeSJiaxin Yu 		dev_err(rtd->card->dev, "%s failed to select state %d\n",
2976191cbdeSJiaxin Yu 			__func__, ret);
2986191cbdeSJiaxin Yu }
2996191cbdeSJiaxin Yu 
3006191cbdeSJiaxin Yu static struct snd_soc_ops mt8183_mt6358_tdm_ops = {
3016191cbdeSJiaxin Yu 	.startup = mt8183_mt6358_tdm_startup,
3026191cbdeSJiaxin Yu 	.shutdown = mt8183_mt6358_tdm_shutdown,
3036191cbdeSJiaxin Yu };
3046191cbdeSJiaxin Yu 
3052cc3cd5fSTzung-Bi Shih static int
3062cc3cd5fSTzung-Bi Shih mt8183_mt6358_ts3a227_max98357_wov_startup(
3072cc3cd5fSTzung-Bi Shih 	struct snd_pcm_substream *substream)
3082cc3cd5fSTzung-Bi Shih {
3090cd08b10SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
3102cc3cd5fSTzung-Bi Shih 	struct snd_soc_card *card = rtd->card;
3112cc3cd5fSTzung-Bi Shih 	struct mt8183_mt6358_ts3a227_max98357_priv *priv =
3122cc3cd5fSTzung-Bi Shih 			snd_soc_card_get_drvdata(card);
3132cc3cd5fSTzung-Bi Shih 
3142cc3cd5fSTzung-Bi Shih 	return pinctrl_select_state(priv->pinctrl,
3152cc3cd5fSTzung-Bi Shih 				    priv->pin_states[PIN_WOV]);
3162cc3cd5fSTzung-Bi Shih }
3172cc3cd5fSTzung-Bi Shih 
3182cc3cd5fSTzung-Bi Shih static void
3192cc3cd5fSTzung-Bi Shih mt8183_mt6358_ts3a227_max98357_wov_shutdown(
3202cc3cd5fSTzung-Bi Shih 	struct snd_pcm_substream *substream)
3212cc3cd5fSTzung-Bi Shih {
3220cd08b10SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
3232cc3cd5fSTzung-Bi Shih 	struct snd_soc_card *card = rtd->card;
3242cc3cd5fSTzung-Bi Shih 	struct mt8183_mt6358_ts3a227_max98357_priv *priv =
3252cc3cd5fSTzung-Bi Shih 			snd_soc_card_get_drvdata(card);
3262cc3cd5fSTzung-Bi Shih 	int ret;
3272cc3cd5fSTzung-Bi Shih 
3282cc3cd5fSTzung-Bi Shih 	ret = pinctrl_select_state(priv->pinctrl,
3292cc3cd5fSTzung-Bi Shih 				   priv->pin_states[PIN_STATE_DEFAULT]);
3302cc3cd5fSTzung-Bi Shih 	if (ret)
3312cc3cd5fSTzung-Bi Shih 		dev_err(card->dev, "%s failed to select state %d\n",
3322cc3cd5fSTzung-Bi Shih 			__func__, ret);
3332cc3cd5fSTzung-Bi Shih }
3342cc3cd5fSTzung-Bi Shih 
3352cc3cd5fSTzung-Bi Shih static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_wov_ops = {
3362cc3cd5fSTzung-Bi Shih 	.startup = mt8183_mt6358_ts3a227_max98357_wov_startup,
3372cc3cd5fSTzung-Bi Shih 	.shutdown = mt8183_mt6358_ts3a227_max98357_wov_shutdown,
3382cc3cd5fSTzung-Bi Shih };
3392cc3cd5fSTzung-Bi Shih 
34088abbf62STzung-Bi Shih static int
34188abbf62STzung-Bi Shih mt8183_mt6358_ts3a227_max98357_hdmi_init(struct snd_soc_pcm_runtime *rtd)
34288abbf62STzung-Bi Shih {
34388abbf62STzung-Bi Shih 	struct mt8183_mt6358_ts3a227_max98357_priv *priv =
34488abbf62STzung-Bi Shih 		snd_soc_card_get_drvdata(rtd->card);
34588abbf62STzung-Bi Shih 	int ret;
34688abbf62STzung-Bi Shih 
34788abbf62STzung-Bi Shih 	ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT,
34888abbf62STzung-Bi Shih 				    &priv->hdmi_jack, NULL, 0);
34988abbf62STzung-Bi Shih 	if (ret)
35088abbf62STzung-Bi Shih 		return ret;
35188abbf62STzung-Bi Shih 
35255c5cc63SCheng-Yi Chiang 	return snd_soc_component_set_jack(asoc_rtd_to_codec(rtd, 0)->component,
35355c5cc63SCheng-Yi Chiang 					  &priv->hdmi_jack, NULL);
35488abbf62STzung-Bi Shih }
35588abbf62STzung-Bi Shih 
356f4fb4fefSTzung-Bi Shih static struct snd_soc_dai_link mt8183_mt6358_ts3a227_dai_links[] = {
35711c02690SShunli Wang 	/* FE */
35811c02690SShunli Wang 	{
35911c02690SShunli Wang 		.name = "Playback_1",
36011c02690SShunli Wang 		.stream_name = "Playback_1",
36111c02690SShunli Wang 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
36211c02690SShunli Wang 			    SND_SOC_DPCM_TRIGGER_PRE},
36311c02690SShunli Wang 		.dynamic = 1,
36411c02690SShunli Wang 		.dpcm_playback = 1,
365fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(playback1),
36611c02690SShunli Wang 	},
36711c02690SShunli Wang 	{
36811c02690SShunli Wang 		.name = "Playback_2",
36911c02690SShunli Wang 		.stream_name = "Playback_2",
37011c02690SShunli Wang 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
37111c02690SShunli Wang 			    SND_SOC_DPCM_TRIGGER_PRE},
37211c02690SShunli Wang 		.dynamic = 1,
37311c02690SShunli Wang 		.dpcm_playback = 1,
3741df1e545STzung-Bi Shih 		.ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops,
375fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(playback2),
37611c02690SShunli Wang 	},
37711c02690SShunli Wang 	{
37811c02690SShunli Wang 		.name = "Playback_3",
37911c02690SShunli Wang 		.stream_name = "Playback_3",
38011c02690SShunli Wang 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
38111c02690SShunli Wang 			    SND_SOC_DPCM_TRIGGER_PRE},
38211c02690SShunli Wang 		.dynamic = 1,
38311c02690SShunli Wang 		.dpcm_playback = 1,
384fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(playback3),
38511c02690SShunli Wang 	},
38611c02690SShunli Wang 	{
38711c02690SShunli Wang 		.name = "Capture_1",
38811c02690SShunli Wang 		.stream_name = "Capture_1",
38911c02690SShunli Wang 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
39011c02690SShunli Wang 			    SND_SOC_DPCM_TRIGGER_PRE},
39111c02690SShunli Wang 		.dynamic = 1,
39211c02690SShunli Wang 		.dpcm_capture = 1,
3931df1e545STzung-Bi Shih 		.ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops,
394fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(capture1),
39511c02690SShunli Wang 	},
39611c02690SShunli Wang 	{
39711c02690SShunli Wang 		.name = "Capture_2",
39811c02690SShunli Wang 		.stream_name = "Capture_2",
39911c02690SShunli Wang 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
40011c02690SShunli Wang 			    SND_SOC_DPCM_TRIGGER_PRE},
40111c02690SShunli Wang 		.dynamic = 1,
40211c02690SShunli Wang 		.dpcm_capture = 1,
403fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(capture2),
40411c02690SShunli Wang 	},
40511c02690SShunli Wang 	{
40611c02690SShunli Wang 		.name = "Capture_3",
40711c02690SShunli Wang 		.stream_name = "Capture_3",
40811c02690SShunli Wang 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
40911c02690SShunli Wang 			    SND_SOC_DPCM_TRIGGER_PRE},
41011c02690SShunli Wang 		.dynamic = 1,
41111c02690SShunli Wang 		.dpcm_capture = 1,
412fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(capture3),
41311c02690SShunli Wang 	},
41411c02690SShunli Wang 	{
41511c02690SShunli Wang 		.name = "Capture_Mono_1",
41611c02690SShunli Wang 		.stream_name = "Capture_Mono_1",
41711c02690SShunli Wang 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
41811c02690SShunli Wang 			    SND_SOC_DPCM_TRIGGER_PRE},
41911c02690SShunli Wang 		.dynamic = 1,
42011c02690SShunli Wang 		.dpcm_capture = 1,
421fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(capture_mono),
42211c02690SShunli Wang 	},
42311c02690SShunli Wang 	{
42411c02690SShunli Wang 		.name = "Playback_HDMI",
42511c02690SShunli Wang 		.stream_name = "Playback_HDMI",
42611c02690SShunli Wang 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
42711c02690SShunli Wang 			    SND_SOC_DPCM_TRIGGER_PRE},
42811c02690SShunli Wang 		.dynamic = 1,
42911c02690SShunli Wang 		.dpcm_playback = 1,
430fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(playback_hdmi),
43111c02690SShunli Wang 	},
4322cc3cd5fSTzung-Bi Shih 	{
4332cc3cd5fSTzung-Bi Shih 		.name = "Wake on Voice",
4342cc3cd5fSTzung-Bi Shih 		.stream_name = "Wake on Voice",
4352cc3cd5fSTzung-Bi Shih 		.ignore_suspend = 1,
4362cc3cd5fSTzung-Bi Shih 		.ignore = 1,
4372cc3cd5fSTzung-Bi Shih 		SND_SOC_DAILINK_REG(wake_on_voice),
4382cc3cd5fSTzung-Bi Shih 		.ops = &mt8183_mt6358_ts3a227_max98357_wov_ops,
4392cc3cd5fSTzung-Bi Shih 	},
4402cc3cd5fSTzung-Bi Shih 
44111c02690SShunli Wang 	/* BE */
44211c02690SShunli Wang 	{
44311c02690SShunli Wang 		.name = "Primary Codec",
44411c02690SShunli Wang 		.no_pcm = 1,
44511c02690SShunli Wang 		.dpcm_playback = 1,
44611c02690SShunli Wang 		.dpcm_capture = 1,
44711c02690SShunli Wang 		.ignore_suspend = 1,
448fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(primary_codec),
44911c02690SShunli Wang 	},
45011c02690SShunli Wang 	{
45111c02690SShunli Wang 		.name = "PCM 1",
45211c02690SShunli Wang 		.no_pcm = 1,
45311c02690SShunli Wang 		.dpcm_playback = 1,
45411c02690SShunli Wang 		.dpcm_capture = 1,
45511c02690SShunli Wang 		.ignore_suspend = 1,
456fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(pcm1),
45711c02690SShunli Wang 	},
45811c02690SShunli Wang 	{
45911c02690SShunli Wang 		.name = "PCM 2",
46011c02690SShunli Wang 		.no_pcm = 1,
46111c02690SShunli Wang 		.dpcm_playback = 1,
46211c02690SShunli Wang 		.dpcm_capture = 1,
46311c02690SShunli Wang 		.ignore_suspend = 1,
464fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(pcm2),
46511c02690SShunli Wang 	},
46611c02690SShunli Wang 	{
46711c02690SShunli Wang 		.name = "I2S0",
46811c02690SShunli Wang 		.no_pcm = 1,
46911c02690SShunli Wang 		.dpcm_capture = 1,
47011c02690SShunli Wang 		.ignore_suspend = 1,
47111c02690SShunli Wang 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
47211c02690SShunli Wang 		.ops = &mt8183_mt6358_i2s_ops,
473fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(i2s0),
47411c02690SShunli Wang 	},
47511c02690SShunli Wang 	{
47611c02690SShunli Wang 		.name = "I2S1",
47711c02690SShunli Wang 		.no_pcm = 1,
47811c02690SShunli Wang 		.dpcm_playback = 1,
47911c02690SShunli Wang 		.ignore_suspend = 1,
48011c02690SShunli Wang 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
48111c02690SShunli Wang 		.ops = &mt8183_mt6358_i2s_ops,
482fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(i2s1),
48311c02690SShunli Wang 	},
48411c02690SShunli Wang 	{
48511c02690SShunli Wang 		.name = "I2S2",
48611c02690SShunli Wang 		.no_pcm = 1,
48711c02690SShunli Wang 		.dpcm_capture = 1,
48811c02690SShunli Wang 		.ignore_suspend = 1,
48911c02690SShunli Wang 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
49011c02690SShunli Wang 		.ops = &mt8183_mt6358_i2s_ops,
491fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(i2s2),
49211c02690SShunli Wang 	},
49311c02690SShunli Wang 	{
49411c02690SShunli Wang 		.name = "I2S3",
49511c02690SShunli Wang 		.no_pcm = 1,
49611c02690SShunli Wang 		.dpcm_playback = 1,
49711c02690SShunli Wang 		.ignore_suspend = 1,
49811c02690SShunli Wang 	},
49911c02690SShunli Wang 	{
50011c02690SShunli Wang 		.name = "I2S5",
50111c02690SShunli Wang 		.no_pcm = 1,
50211c02690SShunli Wang 		.dpcm_playback = 1,
50311c02690SShunli Wang 		.ignore_suspend = 1,
50411c02690SShunli Wang 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
50511c02690SShunli Wang 		.ops = &mt8183_mt6358_i2s_ops,
506fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(i2s5),
50711c02690SShunli Wang 	},
50811c02690SShunli Wang 	{
50911c02690SShunli Wang 		.name = "TDM",
51011c02690SShunli Wang 		.no_pcm = 1,
5118e58c521SJiaxin Yu 		.dai_fmt = SND_SOC_DAIFMT_I2S |
5128e58c521SJiaxin Yu 			   SND_SOC_DAIFMT_IB_IF |
5138e58c521SJiaxin Yu 			   SND_SOC_DAIFMT_CBM_CFM,
51411c02690SShunli Wang 		.dpcm_playback = 1,
51511c02690SShunli Wang 		.ignore_suspend = 1,
5166191cbdeSJiaxin Yu 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
5176191cbdeSJiaxin Yu 		.ops = &mt8183_mt6358_tdm_ops,
51888abbf62STzung-Bi Shih 		.init = mt8183_mt6358_ts3a227_max98357_hdmi_init,
519fa284fd0SKuninori Morimoto 		SND_SOC_DAILINK_REG(tdm),
52011c02690SShunli Wang 	},
52111c02690SShunli Wang };
52211c02690SShunli Wang 
52311c02690SShunli Wang static struct snd_soc_card mt8183_mt6358_ts3a227_max98357_card = {
52411c02690SShunli Wang 	.name = "mt8183_mt6358_ts3a227_max98357",
52511c02690SShunli Wang 	.owner = THIS_MODULE,
526f4fb4fefSTzung-Bi Shih 	.dai_link = mt8183_mt6358_ts3a227_dai_links,
527f4fb4fefSTzung-Bi Shih 	.num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
528f4fb4fefSTzung-Bi Shih };
529f4fb4fefSTzung-Bi Shih 
53008145535STzung-Bi Shih static struct snd_soc_card mt8183_mt6358_ts3a227_max98357b_card = {
53108145535STzung-Bi Shih 	.name = "mt8183_mt6358_ts3a227_max98357b",
53208145535STzung-Bi Shih 	.owner = THIS_MODULE,
53308145535STzung-Bi Shih 	.dai_link = mt8183_mt6358_ts3a227_dai_links,
53408145535STzung-Bi Shih 	.num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
53508145535STzung-Bi Shih };
53608145535STzung-Bi Shih 
537f4fb4fefSTzung-Bi Shih static struct snd_soc_codec_conf mt8183_mt6358_ts3a227_rt1015_amp_conf[] = {
538f4fb4fefSTzung-Bi Shih 	{
539f4fb4fefSTzung-Bi Shih 		.dlc = COMP_CODEC_CONF(RT1015_DEV0_NAME),
540f4fb4fefSTzung-Bi Shih 		.name_prefix = "Left",
541f4fb4fefSTzung-Bi Shih 	},
542f4fb4fefSTzung-Bi Shih 	{
543f4fb4fefSTzung-Bi Shih 		.dlc = COMP_CODEC_CONF(RT1015_DEV1_NAME),
544f4fb4fefSTzung-Bi Shih 		.name_prefix = "Right",
545f4fb4fefSTzung-Bi Shih 	},
546f4fb4fefSTzung-Bi Shih };
547f4fb4fefSTzung-Bi Shih 
548f4fb4fefSTzung-Bi Shih static struct snd_soc_card mt8183_mt6358_ts3a227_rt1015_card = {
549f4fb4fefSTzung-Bi Shih 	.name = "mt8183_mt6358_ts3a227_rt1015",
550f4fb4fefSTzung-Bi Shih 	.owner = THIS_MODULE,
551f4fb4fefSTzung-Bi Shih 	.dai_link = mt8183_mt6358_ts3a227_dai_links,
552f4fb4fefSTzung-Bi Shih 	.num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
553f4fb4fefSTzung-Bi Shih 	.codec_conf = mt8183_mt6358_ts3a227_rt1015_amp_conf,
554f4fb4fefSTzung-Bi Shih 	.num_configs = ARRAY_SIZE(mt8183_mt6358_ts3a227_rt1015_amp_conf),
55511c02690SShunli Wang };
55611c02690SShunli Wang 
55711c02690SShunli Wang static int
55811c02690SShunli Wang mt8183_mt6358_ts3a227_max98357_headset_init(struct snd_soc_component *component)
55911c02690SShunli Wang {
56011c02690SShunli Wang 	int ret;
56195d779b4STzung-Bi Shih 	struct mt8183_mt6358_ts3a227_max98357_priv *priv =
56295d779b4STzung-Bi Shih 			snd_soc_card_get_drvdata(component->card);
56311c02690SShunli Wang 
56411c02690SShunli Wang 	/* Enable Headset and 4 Buttons Jack detection */
565f4fb4fefSTzung-Bi Shih 	ret = snd_soc_card_jack_new(component->card,
56611c02690SShunli Wang 				    "Headset Jack",
56711c02690SShunli Wang 				    SND_JACK_HEADSET |
56811c02690SShunli Wang 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
56911c02690SShunli Wang 				    SND_JACK_BTN_2 | SND_JACK_BTN_3,
57095d779b4STzung-Bi Shih 				    &priv->headset_jack,
571108d0d40STzung-Bi Shih 				    NULL, 0);
57211c02690SShunli Wang 	if (ret)
57311c02690SShunli Wang 		return ret;
57411c02690SShunli Wang 
57595d779b4STzung-Bi Shih 	ret = ts3a227e_enable_jack_detect(component, &priv->headset_jack);
57611c02690SShunli Wang 
57711c02690SShunli Wang 	return ret;
57811c02690SShunli Wang }
57911c02690SShunli Wang 
580fc347483STzung-Bi Shih static struct snd_soc_aux_dev mt8183_mt6358_ts3a227_max98357_headset_dev = {
581fc347483STzung-Bi Shih 	.dlc = COMP_EMPTY(),
582fc347483STzung-Bi Shih 	.init = mt8183_mt6358_ts3a227_max98357_headset_init,
583fc347483STzung-Bi Shih };
584fc347483STzung-Bi Shih 
58511c02690SShunli Wang static int
58611c02690SShunli Wang mt8183_mt6358_ts3a227_max98357_dev_probe(struct platform_device *pdev)
58711c02690SShunli Wang {
588f4fb4fefSTzung-Bi Shih 	struct snd_soc_card *card;
589f2024dc5STzung-Bi Shih 	struct device_node *platform_node, *ec_codec, *hdmi_codec;
59011c02690SShunli Wang 	struct snd_soc_dai_link *dai_link;
5916191cbdeSJiaxin Yu 	struct mt8183_mt6358_ts3a227_max98357_priv *priv;
592f4fb4fefSTzung-Bi Shih 	const struct of_device_id *match;
593f4fb4fefSTzung-Bi Shih 	int ret, i;
59411c02690SShunli Wang 
59511c02690SShunli Wang 	platform_node = of_parse_phandle(pdev->dev.of_node,
59611c02690SShunli Wang 					 "mediatek,platform", 0);
59711c02690SShunli Wang 	if (!platform_node) {
59811c02690SShunli Wang 		dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
59911c02690SShunli Wang 		return -EINVAL;
60011c02690SShunli Wang 	}
60111c02690SShunli Wang 
602f4fb4fefSTzung-Bi Shih 	match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
603f4fb4fefSTzung-Bi Shih 	if (!match || !match->data)
604f4fb4fefSTzung-Bi Shih 		return -EINVAL;
605f4fb4fefSTzung-Bi Shih 
606f4fb4fefSTzung-Bi Shih 	card = (struct snd_soc_card *)match->data;
607f4fb4fefSTzung-Bi Shih 	card->dev = &pdev->dev;
608f4fb4fefSTzung-Bi Shih 
6092cc3cd5fSTzung-Bi Shih 	ec_codec = of_parse_phandle(pdev->dev.of_node, "mediatek,ec-codec", 0);
610f2024dc5STzung-Bi Shih 	hdmi_codec = of_parse_phandle(pdev->dev.of_node,
611f2024dc5STzung-Bi Shih 				      "mediatek,hdmi-codec", 0);
6122cc3cd5fSTzung-Bi Shih 
61311c02690SShunli Wang 	for_each_card_prelinks(card, i, dai_link) {
6142cc3cd5fSTzung-Bi Shih 		if (ec_codec && strcmp(dai_link->name, "Wake on Voice") == 0) {
6152cc3cd5fSTzung-Bi Shih 			dai_link->cpus[0].name = NULL;
6162cc3cd5fSTzung-Bi Shih 			dai_link->cpus[0].of_node = ec_codec;
6172cc3cd5fSTzung-Bi Shih 			dai_link->cpus[0].dai_name = NULL;
6182cc3cd5fSTzung-Bi Shih 			dai_link->codecs[0].name = NULL;
6192cc3cd5fSTzung-Bi Shih 			dai_link->codecs[0].of_node = ec_codec;
6202cc3cd5fSTzung-Bi Shih 			dai_link->codecs[0].dai_name = "Wake on Voice";
6212cc3cd5fSTzung-Bi Shih 			dai_link->platforms[0].of_node = ec_codec;
6222cc3cd5fSTzung-Bi Shih 			dai_link->ignore = 0;
62311c02690SShunli Wang 		}
624f4fb4fefSTzung-Bi Shih 
625f4fb4fefSTzung-Bi Shih 		if (strcmp(dai_link->name, "I2S3") == 0) {
62608145535STzung-Bi Shih 			if (card == &mt8183_mt6358_ts3a227_max98357_card ||
62708145535STzung-Bi Shih 			    card == &mt8183_mt6358_ts3a227_max98357b_card) {
628f4fb4fefSTzung-Bi Shih 				dai_link->be_hw_params_fixup =
629f4fb4fefSTzung-Bi Shih 					mt8183_i2s_hw_params_fixup;
630f4fb4fefSTzung-Bi Shih 				dai_link->ops = &mt8183_mt6358_i2s_ops;
631f4fb4fefSTzung-Bi Shih 				dai_link->cpus = i2s3_max98357a_cpus;
632f4fb4fefSTzung-Bi Shih 				dai_link->num_cpus =
633f4fb4fefSTzung-Bi Shih 					ARRAY_SIZE(i2s3_max98357a_cpus);
634f4fb4fefSTzung-Bi Shih 				dai_link->codecs = i2s3_max98357a_codecs;
635f4fb4fefSTzung-Bi Shih 				dai_link->num_codecs =
636f4fb4fefSTzung-Bi Shih 					ARRAY_SIZE(i2s3_max98357a_codecs);
637f4fb4fefSTzung-Bi Shih 				dai_link->platforms = i2s3_max98357a_platforms;
638f4fb4fefSTzung-Bi Shih 				dai_link->num_platforms =
639f4fb4fefSTzung-Bi Shih 					ARRAY_SIZE(i2s3_max98357a_platforms);
640f4fb4fefSTzung-Bi Shih 			} else if (card == &mt8183_mt6358_ts3a227_rt1015_card) {
641f4fb4fefSTzung-Bi Shih 				dai_link->be_hw_params_fixup =
642f4fb4fefSTzung-Bi Shih 					mt8183_rt1015_i2s_hw_params_fixup;
643f4fb4fefSTzung-Bi Shih 				dai_link->ops = &mt8183_mt6358_rt1015_i2s_ops;
644f4fb4fefSTzung-Bi Shih 				dai_link->cpus = i2s3_rt1015_cpus;
645f4fb4fefSTzung-Bi Shih 				dai_link->num_cpus =
646f4fb4fefSTzung-Bi Shih 					ARRAY_SIZE(i2s3_rt1015_cpus);
647f4fb4fefSTzung-Bi Shih 				dai_link->codecs = i2s3_rt1015_codecs;
648f4fb4fefSTzung-Bi Shih 				dai_link->num_codecs =
649f4fb4fefSTzung-Bi Shih 					ARRAY_SIZE(i2s3_rt1015_codecs);
650f4fb4fefSTzung-Bi Shih 				dai_link->platforms = i2s3_rt1015_platforms;
651f4fb4fefSTzung-Bi Shih 				dai_link->num_platforms =
652f4fb4fefSTzung-Bi Shih 					ARRAY_SIZE(i2s3_rt1015_platforms);
653f4fb4fefSTzung-Bi Shih 			}
654f4fb4fefSTzung-Bi Shih 		}
655f4fb4fefSTzung-Bi Shih 
65608145535STzung-Bi Shih 		if (card == &mt8183_mt6358_ts3a227_max98357b_card) {
65708145535STzung-Bi Shih 			if (strcmp(dai_link->name, "I2S2") == 0 ||
65808145535STzung-Bi Shih 			    strcmp(dai_link->name, "I2S3") == 0)
65908145535STzung-Bi Shih 				dai_link->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
66008145535STzung-Bi Shih 						    SND_SOC_DAIFMT_NB_NF |
66108145535STzung-Bi Shih 						    SND_SOC_DAIFMT_CBM_CFM;
66208145535STzung-Bi Shih 		}
66308145535STzung-Bi Shih 
664f2024dc5STzung-Bi Shih 		if (hdmi_codec && strcmp(dai_link->name, "TDM") == 0)
665f2024dc5STzung-Bi Shih 			dai_link->codecs->of_node = hdmi_codec;
666f2024dc5STzung-Bi Shih 
667f4fb4fefSTzung-Bi Shih 		if (!dai_link->platforms->name)
668f4fb4fefSTzung-Bi Shih 			dai_link->platforms->of_node = platform_node;
6692cc3cd5fSTzung-Bi Shih 	}
67011c02690SShunli Wang 
671b812cd58SKuninori Morimoto 	mt8183_mt6358_ts3a227_max98357_headset_dev.dlc.of_node =
67211c02690SShunli Wang 		of_parse_phandle(pdev->dev.of_node,
67311c02690SShunli Wang 				 "mediatek,headset-codec", 0);
674b812cd58SKuninori Morimoto 	if (mt8183_mt6358_ts3a227_max98357_headset_dev.dlc.of_node) {
675a962a809STzung-Bi Shih 		card->aux_dev = &mt8183_mt6358_ts3a227_max98357_headset_dev;
676a962a809STzung-Bi Shih 		card->num_aux_devs = 1;
67711c02690SShunli Wang 	}
67811c02690SShunli Wang 
6796191cbdeSJiaxin Yu 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
6806191cbdeSJiaxin Yu 	if (!priv)
6816191cbdeSJiaxin Yu 		return -ENOMEM;
6826191cbdeSJiaxin Yu 
6836191cbdeSJiaxin Yu 	snd_soc_card_set_drvdata(card, priv);
6846191cbdeSJiaxin Yu 
6856191cbdeSJiaxin Yu 	priv->pinctrl = devm_pinctrl_get(&pdev->dev);
6866191cbdeSJiaxin Yu 	if (IS_ERR(priv->pinctrl)) {
6876191cbdeSJiaxin Yu 		dev_err(&pdev->dev, "%s devm_pinctrl_get failed\n",
68811c02690SShunli Wang 			__func__);
6896191cbdeSJiaxin Yu 		return PTR_ERR(priv->pinctrl);
6906191cbdeSJiaxin Yu 	}
6916191cbdeSJiaxin Yu 
6926191cbdeSJiaxin Yu 	for (i = 0; i < PIN_STATE_MAX; i++) {
6936191cbdeSJiaxin Yu 		priv->pin_states[i] = pinctrl_lookup_state(priv->pinctrl,
6946191cbdeSJiaxin Yu 							   mt8183_pin_str[i]);
6956191cbdeSJiaxin Yu 		if (IS_ERR(priv->pin_states[i])) {
6966191cbdeSJiaxin Yu 			ret = PTR_ERR(priv->pin_states[i]);
6976191cbdeSJiaxin Yu 			dev_info(&pdev->dev, "%s Can't find pin state %s %d\n",
6986191cbdeSJiaxin Yu 				 __func__, mt8183_pin_str[i], ret);
6996191cbdeSJiaxin Yu 		}
7006191cbdeSJiaxin Yu 	}
7016191cbdeSJiaxin Yu 
7026191cbdeSJiaxin Yu 	if (!IS_ERR(priv->pin_states[PIN_TDM_OUT_OFF])) {
7036191cbdeSJiaxin Yu 		ret = pinctrl_select_state(priv->pinctrl,
7046191cbdeSJiaxin Yu 					   priv->pin_states[PIN_TDM_OUT_OFF]);
7056191cbdeSJiaxin Yu 		if (ret)
7066191cbdeSJiaxin Yu 			dev_info(&pdev->dev,
7076191cbdeSJiaxin Yu 				 "%s failed to select state %d\n",
7086191cbdeSJiaxin Yu 				 __func__, ret);
7096191cbdeSJiaxin Yu 	}
7106191cbdeSJiaxin Yu 
7116191cbdeSJiaxin Yu 	if (!IS_ERR(priv->pin_states[PIN_STATE_DEFAULT])) {
7126191cbdeSJiaxin Yu 		ret = pinctrl_select_state(priv->pinctrl,
7136191cbdeSJiaxin Yu 					   priv->pin_states[PIN_STATE_DEFAULT]);
7146191cbdeSJiaxin Yu 		if (ret)
7156191cbdeSJiaxin Yu 			dev_info(&pdev->dev,
7166191cbdeSJiaxin Yu 				 "%s failed to select state %d\n",
7176191cbdeSJiaxin Yu 				 __func__, ret);
71811c02690SShunli Wang 	}
71911c02690SShunli Wang 
72063ba8e4dSTzung-Bi Shih 	return devm_snd_soc_register_card(&pdev->dev, card);
72111c02690SShunli Wang }
72211c02690SShunli Wang 
72311c02690SShunli Wang #ifdef CONFIG_OF
72411c02690SShunli Wang static const struct of_device_id mt8183_mt6358_ts3a227_max98357_dt_match[] = {
725f4fb4fefSTzung-Bi Shih 	{
726f4fb4fefSTzung-Bi Shih 		.compatible = "mediatek,mt8183_mt6358_ts3a227_max98357",
727f4fb4fefSTzung-Bi Shih 		.data = &mt8183_mt6358_ts3a227_max98357_card,
728f4fb4fefSTzung-Bi Shih 	},
729f4fb4fefSTzung-Bi Shih 	{
73008145535STzung-Bi Shih 		.compatible = "mediatek,mt8183_mt6358_ts3a227_max98357b",
73108145535STzung-Bi Shih 		.data = &mt8183_mt6358_ts3a227_max98357b_card,
73208145535STzung-Bi Shih 	},
73308145535STzung-Bi Shih 	{
734f4fb4fefSTzung-Bi Shih 		.compatible = "mediatek,mt8183_mt6358_ts3a227_rt1015",
735f4fb4fefSTzung-Bi Shih 		.data = &mt8183_mt6358_ts3a227_rt1015_card,
736f4fb4fefSTzung-Bi Shih 	},
73711c02690SShunli Wang 	{}
73811c02690SShunli Wang };
73911c02690SShunli Wang #endif
74011c02690SShunli Wang 
74111c02690SShunli Wang static struct platform_driver mt8183_mt6358_ts3a227_max98357_driver = {
74211c02690SShunli Wang 	.driver = {
743f4fb4fefSTzung-Bi Shih 		.name = "mt8183_mt6358_ts3a227",
74411c02690SShunli Wang #ifdef CONFIG_OF
74511c02690SShunli Wang 		.of_match_table = mt8183_mt6358_ts3a227_max98357_dt_match,
74611c02690SShunli Wang #endif
747*de96bd7bSTzung-Bi Shih 		.pm = &snd_soc_pm_ops,
74811c02690SShunli Wang 	},
74911c02690SShunli Wang 	.probe = mt8183_mt6358_ts3a227_max98357_dev_probe,
75011c02690SShunli Wang };
75111c02690SShunli Wang 
75211c02690SShunli Wang module_platform_driver(mt8183_mt6358_ts3a227_max98357_driver);
75311c02690SShunli Wang 
75411c02690SShunli Wang /* Module information */
75511c02690SShunli Wang MODULE_DESCRIPTION("MT8183-MT6358-TS3A227-MAX98357 ALSA SoC machine driver");
75611c02690SShunli Wang MODULE_AUTHOR("Shunli Wang <shunli.wang@mediatek.com>");
75711c02690SShunli Wang MODULE_LICENSE("GPL v2");
75811c02690SShunli Wang MODULE_ALIAS("mt8183_mt6358_ts3a227_max98357 soc card");
759