xref: /openbmc/linux/sound/soc/mediatek/mt8183/mt8183-dai-i2s.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1a94aec03SShunli Wang // SPDX-License-Identifier: GPL-2.0
2a94aec03SShunli Wang //
3a94aec03SShunli Wang // MediaTek ALSA SoC Audio DAI I2S Control
4a94aec03SShunli Wang //
5a94aec03SShunli Wang // Copyright (c) 2018 MediaTek Inc.
6a94aec03SShunli Wang // Author: KaiChieh Chuang <kaichieh.chuang@mediatek.com>
7a94aec03SShunli Wang 
8a94aec03SShunli Wang #include <linux/bitops.h>
9a94aec03SShunli Wang #include <linux/regmap.h>
10a94aec03SShunli Wang #include <sound/pcm_params.h>
11a94aec03SShunli Wang #include "mt8183-afe-clk.h"
12a94aec03SShunli Wang #include "mt8183-afe-common.h"
13a94aec03SShunli Wang #include "mt8183-interconnection.h"
14a94aec03SShunli Wang #include "mt8183-reg.h"
15a94aec03SShunli Wang 
16a94aec03SShunli Wang enum {
17a94aec03SShunli Wang 	I2S_FMT_EIAJ = 0,
18a94aec03SShunli Wang 	I2S_FMT_I2S = 1,
19a94aec03SShunli Wang };
20a94aec03SShunli Wang 
21a94aec03SShunli Wang enum {
22a94aec03SShunli Wang 	I2S_WLEN_16_BIT = 0,
23a94aec03SShunli Wang 	I2S_WLEN_32_BIT = 1,
24a94aec03SShunli Wang };
25a94aec03SShunli Wang 
26a94aec03SShunli Wang enum {
27a94aec03SShunli Wang 	I2S_HD_NORMAL = 0,
28a94aec03SShunli Wang 	I2S_HD_LOW_JITTER = 1,
29a94aec03SShunli Wang };
30a94aec03SShunli Wang 
31a94aec03SShunli Wang enum {
32a94aec03SShunli Wang 	I2S1_SEL_O28_O29 = 0,
33a94aec03SShunli Wang 	I2S1_SEL_O03_O04 = 1,
34a94aec03SShunli Wang };
35a94aec03SShunli Wang 
36a94aec03SShunli Wang enum {
37a94aec03SShunli Wang 	I2S_IN_PAD_CONNSYS = 0,
38a94aec03SShunli Wang 	I2S_IN_PAD_IO_MUX = 1,
39a94aec03SShunli Wang };
40a94aec03SShunli Wang 
41a94aec03SShunli Wang struct mtk_afe_i2s_priv {
42a94aec03SShunli Wang 	int id;
43a94aec03SShunli Wang 	int rate; /* for determine which apll to use */
44a94aec03SShunli Wang 	int low_jitter_en;
45a94aec03SShunli Wang 
46a94aec03SShunli Wang 	int share_i2s_id;
47a94aec03SShunli Wang 
48a94aec03SShunli Wang 	int mclk_id;
49a94aec03SShunli Wang 	int mclk_rate;
50a94aec03SShunli Wang 	int mclk_apll;
51767ee388STzung-Bi Shih 
52767ee388STzung-Bi Shih 	int use_eiaj;
53a94aec03SShunli Wang };
54a94aec03SShunli Wang 
get_i2s_wlen(snd_pcm_format_t format)55a94aec03SShunli Wang static unsigned int get_i2s_wlen(snd_pcm_format_t format)
56a94aec03SShunli Wang {
57a94aec03SShunli Wang 	return snd_pcm_format_physical_width(format) <= 16 ?
58a94aec03SShunli Wang 	       I2S_WLEN_16_BIT : I2S_WLEN_32_BIT;
59a94aec03SShunli Wang }
60a94aec03SShunli Wang 
61a94aec03SShunli Wang #define MTK_AFE_I2S0_KCONTROL_NAME "I2S0_HD_Mux"
62a94aec03SShunli Wang #define MTK_AFE_I2S1_KCONTROL_NAME "I2S1_HD_Mux"
63a94aec03SShunli Wang #define MTK_AFE_I2S2_KCONTROL_NAME "I2S2_HD_Mux"
64a94aec03SShunli Wang #define MTK_AFE_I2S3_KCONTROL_NAME "I2S3_HD_Mux"
65a94aec03SShunli Wang #define MTK_AFE_I2S5_KCONTROL_NAME "I2S5_HD_Mux"
66a94aec03SShunli Wang 
67a94aec03SShunli Wang #define I2S0_HD_EN_W_NAME "I2S0_HD_EN"
68a94aec03SShunli Wang #define I2S1_HD_EN_W_NAME "I2S1_HD_EN"
69a94aec03SShunli Wang #define I2S2_HD_EN_W_NAME "I2S2_HD_EN"
70a94aec03SShunli Wang #define I2S3_HD_EN_W_NAME "I2S3_HD_EN"
71a94aec03SShunli Wang #define I2S5_HD_EN_W_NAME "I2S5_HD_EN"
72a94aec03SShunli Wang 
73a94aec03SShunli Wang #define I2S0_MCLK_EN_W_NAME "I2S0_MCLK_EN"
74a94aec03SShunli Wang #define I2S1_MCLK_EN_W_NAME "I2S1_MCLK_EN"
75a94aec03SShunli Wang #define I2S2_MCLK_EN_W_NAME "I2S2_MCLK_EN"
76a94aec03SShunli Wang #define I2S3_MCLK_EN_W_NAME "I2S3_MCLK_EN"
77a94aec03SShunli Wang #define I2S5_MCLK_EN_W_NAME "I2S5_MCLK_EN"
78a94aec03SShunli Wang 
get_i2s_id_by_name(struct mtk_base_afe * afe,const char * name)79a94aec03SShunli Wang static int get_i2s_id_by_name(struct mtk_base_afe *afe,
80a94aec03SShunli Wang 			      const char *name)
81a94aec03SShunli Wang {
82a94aec03SShunli Wang 	if (strncmp(name, "I2S0", 4) == 0)
83a94aec03SShunli Wang 		return MT8183_DAI_I2S_0;
84a94aec03SShunli Wang 	else if (strncmp(name, "I2S1", 4) == 0)
85a94aec03SShunli Wang 		return MT8183_DAI_I2S_1;
86a94aec03SShunli Wang 	else if (strncmp(name, "I2S2", 4) == 0)
87a94aec03SShunli Wang 		return MT8183_DAI_I2S_2;
88a94aec03SShunli Wang 	else if (strncmp(name, "I2S3", 4) == 0)
89a94aec03SShunli Wang 		return MT8183_DAI_I2S_3;
90a94aec03SShunli Wang 	else if (strncmp(name, "I2S5", 4) == 0)
91a94aec03SShunli Wang 		return MT8183_DAI_I2S_5;
92a94aec03SShunli Wang 	else
93a94aec03SShunli Wang 		return -EINVAL;
94a94aec03SShunli Wang }
95a94aec03SShunli Wang 
get_i2s_priv_by_name(struct mtk_base_afe * afe,const char * name)96a94aec03SShunli Wang static struct mtk_afe_i2s_priv *get_i2s_priv_by_name(struct mtk_base_afe *afe,
97a94aec03SShunli Wang 						     const char *name)
98a94aec03SShunli Wang {
99a94aec03SShunli Wang 	struct mt8183_afe_private *afe_priv = afe->platform_priv;
100a94aec03SShunli Wang 	int dai_id = get_i2s_id_by_name(afe, name);
101a94aec03SShunli Wang 
102a94aec03SShunli Wang 	if (dai_id < 0)
103a94aec03SShunli Wang 		return NULL;
104a94aec03SShunli Wang 
105a94aec03SShunli Wang 	return afe_priv->dai_priv[dai_id];
106a94aec03SShunli Wang }
107a94aec03SShunli Wang 
108a94aec03SShunli Wang /* low jitter control */
109a94aec03SShunli Wang static const char * const mt8183_i2s_hd_str[] = {
110a94aec03SShunli Wang 	"Normal", "Low_Jitter"
111a94aec03SShunli Wang };
112a94aec03SShunli Wang 
113a94aec03SShunli Wang static const struct soc_enum mt8183_i2s_enum[] = {
114a94aec03SShunli Wang 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(mt8183_i2s_hd_str),
115a94aec03SShunli Wang 			    mt8183_i2s_hd_str),
116a94aec03SShunli Wang };
117a94aec03SShunli Wang 
mt8183_i2s_hd_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)118a94aec03SShunli Wang static int mt8183_i2s_hd_get(struct snd_kcontrol *kcontrol,
119a94aec03SShunli Wang 			     struct snd_ctl_elem_value *ucontrol)
120a94aec03SShunli Wang {
121a94aec03SShunli Wang 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
122a94aec03SShunli Wang 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);
123a94aec03SShunli Wang 	struct mtk_afe_i2s_priv *i2s_priv;
124a94aec03SShunli Wang 
125a94aec03SShunli Wang 	i2s_priv = get_i2s_priv_by_name(afe, kcontrol->id.name);
126a94aec03SShunli Wang 
127a94aec03SShunli Wang 	if (!i2s_priv) {
128a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), i2s_priv == NULL", __func__);
129a94aec03SShunli Wang 		return -EINVAL;
130a94aec03SShunli Wang 	}
131a94aec03SShunli Wang 
132a94aec03SShunli Wang 	ucontrol->value.integer.value[0] = i2s_priv->low_jitter_en;
133a94aec03SShunli Wang 
134a94aec03SShunli Wang 	return 0;
135a94aec03SShunli Wang }
136a94aec03SShunli Wang 
mt8183_i2s_hd_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)137a94aec03SShunli Wang static int mt8183_i2s_hd_set(struct snd_kcontrol *kcontrol,
138a94aec03SShunli Wang 			     struct snd_ctl_elem_value *ucontrol)
139a94aec03SShunli Wang {
140a94aec03SShunli Wang 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
141a94aec03SShunli Wang 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);
142a94aec03SShunli Wang 	struct mtk_afe_i2s_priv *i2s_priv;
143a94aec03SShunli Wang 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
144*18f51ed0SMark Brown 	int hd_en, change;
145a94aec03SShunli Wang 
146a94aec03SShunli Wang 	if (ucontrol->value.enumerated.item[0] >= e->items)
147a94aec03SShunli Wang 		return -EINVAL;
148a94aec03SShunli Wang 
149a94aec03SShunli Wang 	hd_en = ucontrol->value.integer.value[0];
150a94aec03SShunli Wang 
151a94aec03SShunli Wang 	i2s_priv = get_i2s_priv_by_name(afe, kcontrol->id.name);
152a94aec03SShunli Wang 
153a94aec03SShunli Wang 	if (!i2s_priv) {
154a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), i2s_priv == NULL", __func__);
155a94aec03SShunli Wang 		return -EINVAL;
156a94aec03SShunli Wang 	}
157a94aec03SShunli Wang 
158*18f51ed0SMark Brown 	change = i2s_priv->low_jitter_en != hd_en;
159a94aec03SShunli Wang 	i2s_priv->low_jitter_en = hd_en;
160a94aec03SShunli Wang 
161*18f51ed0SMark Brown 	return change;
162a94aec03SShunli Wang }
163a94aec03SShunli Wang 
164a94aec03SShunli Wang static const struct snd_kcontrol_new mtk_dai_i2s_controls[] = {
165a94aec03SShunli Wang 	SOC_ENUM_EXT(MTK_AFE_I2S0_KCONTROL_NAME, mt8183_i2s_enum[0],
166a94aec03SShunli Wang 		     mt8183_i2s_hd_get, mt8183_i2s_hd_set),
167a94aec03SShunli Wang 	SOC_ENUM_EXT(MTK_AFE_I2S1_KCONTROL_NAME, mt8183_i2s_enum[0],
168a94aec03SShunli Wang 		     mt8183_i2s_hd_get, mt8183_i2s_hd_set),
169a94aec03SShunli Wang 	SOC_ENUM_EXT(MTK_AFE_I2S2_KCONTROL_NAME, mt8183_i2s_enum[0],
170a94aec03SShunli Wang 		     mt8183_i2s_hd_get, mt8183_i2s_hd_set),
171a94aec03SShunli Wang 	SOC_ENUM_EXT(MTK_AFE_I2S3_KCONTROL_NAME, mt8183_i2s_enum[0],
172a94aec03SShunli Wang 		     mt8183_i2s_hd_get, mt8183_i2s_hd_set),
173a94aec03SShunli Wang 	SOC_ENUM_EXT(MTK_AFE_I2S5_KCONTROL_NAME, mt8183_i2s_enum[0],
174a94aec03SShunli Wang 		     mt8183_i2s_hd_get, mt8183_i2s_hd_set),
175a94aec03SShunli Wang };
176a94aec03SShunli Wang 
177a94aec03SShunli Wang /* dai component */
178a94aec03SShunli Wang /* interconnection */
179a94aec03SShunli Wang static const struct snd_kcontrol_new mtk_i2s3_ch1_mix[] = {
180a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL1_CH1", AFE_CONN0, I_DL1_CH1, 1, 0),
181a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL2_CH1", AFE_CONN0, I_DL2_CH1, 1, 0),
182a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL3_CH1", AFE_CONN0, I_DL3_CH1, 1, 0),
183a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("ADDA_UL_CH1", AFE_CONN0,
184a94aec03SShunli Wang 				    I_ADDA_UL_CH1, 1, 0),
185a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_1_CAP_CH1", AFE_CONN0,
186a94aec03SShunli Wang 				    I_PCM_1_CAP_CH1, 1, 0),
187a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_2_CAP_CH1", AFE_CONN0,
188a94aec03SShunli Wang 				    I_PCM_2_CAP_CH1, 1, 0),
189a94aec03SShunli Wang };
190a94aec03SShunli Wang 
191a94aec03SShunli Wang static const struct snd_kcontrol_new mtk_i2s3_ch2_mix[] = {
192a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL1_CH2", AFE_CONN1, I_DL1_CH2, 1, 0),
193a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL2_CH2", AFE_CONN1, I_DL2_CH2, 1, 0),
194a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL3_CH2", AFE_CONN1, I_DL3_CH2, 1, 0),
195a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("ADDA_UL_CH2", AFE_CONN1,
196a94aec03SShunli Wang 				    I_ADDA_UL_CH2, 1, 0),
197a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_1_CAP_CH1", AFE_CONN1,
198a94aec03SShunli Wang 				    I_PCM_1_CAP_CH1, 1, 0),
199a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_2_CAP_CH1", AFE_CONN1,
200a94aec03SShunli Wang 				    I_PCM_2_CAP_CH1, 1, 0),
201a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_1_CAP_CH2", AFE_CONN1,
202a94aec03SShunli Wang 				    I_PCM_1_CAP_CH2, 1, 0),
203a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_2_CAP_CH2", AFE_CONN1,
204a94aec03SShunli Wang 				    I_PCM_2_CAP_CH2, 1, 0),
205a94aec03SShunli Wang };
206a94aec03SShunli Wang 
207a94aec03SShunli Wang static const struct snd_kcontrol_new mtk_i2s1_ch1_mix[] = {
208a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL1_CH1", AFE_CONN28, I_DL1_CH1, 1, 0),
209a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL2_CH1", AFE_CONN28, I_DL2_CH1, 1, 0),
210a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL3_CH1", AFE_CONN28, I_DL3_CH1, 1, 0),
211a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("ADDA_UL_CH1", AFE_CONN28,
212a94aec03SShunli Wang 				    I_ADDA_UL_CH1, 1, 0),
213a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_1_CAP_CH1", AFE_CONN28,
214a94aec03SShunli Wang 				    I_PCM_1_CAP_CH1, 1, 0),
215a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_2_CAP_CH1", AFE_CONN28,
216a94aec03SShunli Wang 				    I_PCM_2_CAP_CH1, 1, 0),
217a94aec03SShunli Wang };
218a94aec03SShunli Wang 
219a94aec03SShunli Wang static const struct snd_kcontrol_new mtk_i2s1_ch2_mix[] = {
220a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL1_CH2", AFE_CONN29, I_DL1_CH2, 1, 0),
221a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL2_CH2", AFE_CONN29, I_DL2_CH2, 1, 0),
222a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL3_CH2", AFE_CONN29, I_DL3_CH2, 1, 0),
223a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("ADDA_UL_CH2", AFE_CONN29,
224a94aec03SShunli Wang 				    I_ADDA_UL_CH2, 1, 0),
225a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_1_CAP_CH1", AFE_CONN29,
226a94aec03SShunli Wang 				    I_PCM_1_CAP_CH1, 1, 0),
227a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_2_CAP_CH1", AFE_CONN29,
228a94aec03SShunli Wang 				    I_PCM_2_CAP_CH1, 1, 0),
229a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_1_CAP_CH2", AFE_CONN29,
230a94aec03SShunli Wang 				    I_PCM_1_CAP_CH2, 1, 0),
231a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_2_CAP_CH2", AFE_CONN29,
232a94aec03SShunli Wang 				    I_PCM_2_CAP_CH2, 1, 0),
233a94aec03SShunli Wang };
234a94aec03SShunli Wang 
235a94aec03SShunli Wang static const struct snd_kcontrol_new mtk_i2s5_ch1_mix[] = {
236a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL1_CH1", AFE_CONN30, I_DL1_CH1, 1, 0),
237a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL2_CH1", AFE_CONN30, I_DL2_CH1, 1, 0),
238a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL3_CH1", AFE_CONN30, I_DL3_CH1, 1, 0),
239a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("ADDA_UL_CH1", AFE_CONN30,
240a94aec03SShunli Wang 				    I_ADDA_UL_CH1, 1, 0),
241a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_1_CAP_CH1", AFE_CONN30,
242a94aec03SShunli Wang 				    I_PCM_1_CAP_CH1, 1, 0),
243a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_2_CAP_CH1", AFE_CONN30,
244a94aec03SShunli Wang 				    I_PCM_2_CAP_CH1, 1, 0),
245a94aec03SShunli Wang };
246a94aec03SShunli Wang 
247a94aec03SShunli Wang static const struct snd_kcontrol_new mtk_i2s5_ch2_mix[] = {
248a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL1_CH2", AFE_CONN31, I_DL1_CH2, 1, 0),
249a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL2_CH2", AFE_CONN31, I_DL2_CH2, 1, 0),
250a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("DL3_CH2", AFE_CONN31, I_DL3_CH2, 1, 0),
251a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("ADDA_UL_CH2", AFE_CONN31,
252a94aec03SShunli Wang 				    I_ADDA_UL_CH2, 1, 0),
253a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_1_CAP_CH1", AFE_CONN31,
254a94aec03SShunli Wang 				    I_PCM_1_CAP_CH1, 1, 0),
255a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_2_CAP_CH1", AFE_CONN31,
256a94aec03SShunli Wang 				    I_PCM_2_CAP_CH1, 1, 0),
257a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_1_CAP_CH2", AFE_CONN31,
258a94aec03SShunli Wang 				    I_PCM_1_CAP_CH2, 1, 0),
259a94aec03SShunli Wang 	SOC_DAPM_SINGLE_AUTODISABLE("PCM_2_CAP_CH2", AFE_CONN31,
260a94aec03SShunli Wang 				    I_PCM_2_CAP_CH2, 1, 0),
261a94aec03SShunli Wang };
262a94aec03SShunli Wang 
263a94aec03SShunli Wang enum {
264a94aec03SShunli Wang 	SUPPLY_SEQ_APLL,
265a94aec03SShunli Wang 	SUPPLY_SEQ_I2S_MCLK_EN,
266a94aec03SShunli Wang 	SUPPLY_SEQ_I2S_HD_EN,
267a94aec03SShunli Wang 	SUPPLY_SEQ_I2S_EN,
268a94aec03SShunli Wang };
269a94aec03SShunli Wang 
mtk_apll_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)270a94aec03SShunli Wang static int mtk_apll_event(struct snd_soc_dapm_widget *w,
271a94aec03SShunli Wang 			  struct snd_kcontrol *kcontrol,
272a94aec03SShunli Wang 			  int event)
273a94aec03SShunli Wang {
274a94aec03SShunli Wang 	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
275a94aec03SShunli Wang 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);
276a94aec03SShunli Wang 
277a94aec03SShunli Wang 	switch (event) {
278a94aec03SShunli Wang 	case SND_SOC_DAPM_PRE_PMU:
279a94aec03SShunli Wang 		if (strcmp(w->name, APLL1_W_NAME) == 0)
280a94aec03SShunli Wang 			mt8183_apll1_enable(afe);
281a94aec03SShunli Wang 		else
282a94aec03SShunli Wang 			mt8183_apll2_enable(afe);
283a94aec03SShunli Wang 		break;
284a94aec03SShunli Wang 	case SND_SOC_DAPM_POST_PMD:
285a94aec03SShunli Wang 		if (strcmp(w->name, APLL1_W_NAME) == 0)
286a94aec03SShunli Wang 			mt8183_apll1_disable(afe);
287a94aec03SShunli Wang 		else
288a94aec03SShunli Wang 			mt8183_apll2_disable(afe);
289a94aec03SShunli Wang 		break;
290a94aec03SShunli Wang 	default:
291a94aec03SShunli Wang 		break;
292a94aec03SShunli Wang 	}
293a94aec03SShunli Wang 
294a94aec03SShunli Wang 	return 0;
295a94aec03SShunli Wang }
296a94aec03SShunli Wang 
mtk_mclk_en_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)297a94aec03SShunli Wang static int mtk_mclk_en_event(struct snd_soc_dapm_widget *w,
298a94aec03SShunli Wang 			     struct snd_kcontrol *kcontrol,
299a94aec03SShunli Wang 			     int event)
300a94aec03SShunli Wang {
301a94aec03SShunli Wang 	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
302a94aec03SShunli Wang 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);
303a94aec03SShunli Wang 	struct mtk_afe_i2s_priv *i2s_priv;
304a94aec03SShunli Wang 
305a94aec03SShunli Wang 	i2s_priv = get_i2s_priv_by_name(afe, w->name);
306a94aec03SShunli Wang 
307a94aec03SShunli Wang 	if (!i2s_priv) {
308a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), i2s_priv == NULL", __func__);
309a94aec03SShunli Wang 		return -EINVAL;
310a94aec03SShunli Wang 	}
311a94aec03SShunli Wang 
312a94aec03SShunli Wang 	switch (event) {
313a94aec03SShunli Wang 	case SND_SOC_DAPM_PRE_PMU:
314a94aec03SShunli Wang 		mt8183_mck_enable(afe, i2s_priv->mclk_id, i2s_priv->mclk_rate);
315a94aec03SShunli Wang 		break;
316a94aec03SShunli Wang 	case SND_SOC_DAPM_POST_PMD:
317a94aec03SShunli Wang 		i2s_priv->mclk_rate = 0;
318a94aec03SShunli Wang 		mt8183_mck_disable(afe, i2s_priv->mclk_id);
319a94aec03SShunli Wang 		break;
320a94aec03SShunli Wang 	default:
321a94aec03SShunli Wang 		break;
322a94aec03SShunli Wang 	}
323a94aec03SShunli Wang 
324a94aec03SShunli Wang 	return 0;
325a94aec03SShunli Wang }
326a94aec03SShunli Wang 
327a94aec03SShunli Wang static const struct snd_soc_dapm_widget mtk_dai_i2s_widgets[] = {
328a94aec03SShunli Wang 	SND_SOC_DAPM_MIXER("I2S1_CH1", SND_SOC_NOPM, 0, 0,
329a94aec03SShunli Wang 			   mtk_i2s1_ch1_mix,
330a94aec03SShunli Wang 			   ARRAY_SIZE(mtk_i2s1_ch1_mix)),
331a94aec03SShunli Wang 	SND_SOC_DAPM_MIXER("I2S1_CH2", SND_SOC_NOPM, 0, 0,
332a94aec03SShunli Wang 			   mtk_i2s1_ch2_mix,
333a94aec03SShunli Wang 			   ARRAY_SIZE(mtk_i2s1_ch2_mix)),
334a94aec03SShunli Wang 
335a94aec03SShunli Wang 	SND_SOC_DAPM_MIXER("I2S3_CH1", SND_SOC_NOPM, 0, 0,
336a94aec03SShunli Wang 			   mtk_i2s3_ch1_mix,
337a94aec03SShunli Wang 			   ARRAY_SIZE(mtk_i2s3_ch1_mix)),
338a94aec03SShunli Wang 	SND_SOC_DAPM_MIXER("I2S3_CH2", SND_SOC_NOPM, 0, 0,
339a94aec03SShunli Wang 			   mtk_i2s3_ch2_mix,
340a94aec03SShunli Wang 			   ARRAY_SIZE(mtk_i2s3_ch2_mix)),
341a94aec03SShunli Wang 
342a94aec03SShunli Wang 	SND_SOC_DAPM_MIXER("I2S5_CH1", SND_SOC_NOPM, 0, 0,
343a94aec03SShunli Wang 			   mtk_i2s5_ch1_mix,
344a94aec03SShunli Wang 			   ARRAY_SIZE(mtk_i2s5_ch1_mix)),
345a94aec03SShunli Wang 	SND_SOC_DAPM_MIXER("I2S5_CH2", SND_SOC_NOPM, 0, 0,
346a94aec03SShunli Wang 			   mtk_i2s5_ch2_mix,
347a94aec03SShunli Wang 			   ARRAY_SIZE(mtk_i2s5_ch2_mix)),
348a94aec03SShunli Wang 
349a94aec03SShunli Wang 	/* i2s en*/
350a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S("I2S0_EN", SUPPLY_SEQ_I2S_EN,
351a94aec03SShunli Wang 			      AFE_I2S_CON, I2S_EN_SFT, 0,
352a94aec03SShunli Wang 			      NULL, 0),
353a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S("I2S1_EN", SUPPLY_SEQ_I2S_EN,
354a94aec03SShunli Wang 			      AFE_I2S_CON1, I2S_EN_SFT, 0,
355a94aec03SShunli Wang 			      NULL, 0),
356a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S("I2S2_EN", SUPPLY_SEQ_I2S_EN,
357a94aec03SShunli Wang 			      AFE_I2S_CON2, I2S_EN_SFT, 0,
358a94aec03SShunli Wang 			      NULL, 0),
359a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S("I2S3_EN", SUPPLY_SEQ_I2S_EN,
360a94aec03SShunli Wang 			      AFE_I2S_CON3, I2S_EN_SFT, 0,
361a94aec03SShunli Wang 			      NULL, 0),
362a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S("I2S5_EN", SUPPLY_SEQ_I2S_EN,
363a94aec03SShunli Wang 			      AFE_I2S_CON4, I2S5_EN_SFT, 0,
364a94aec03SShunli Wang 			      NULL, 0),
365a94aec03SShunli Wang 	/* i2s hd en */
366a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S(I2S0_HD_EN_W_NAME, SUPPLY_SEQ_I2S_HD_EN,
367a94aec03SShunli Wang 			      AFE_I2S_CON, I2S1_HD_EN_SFT, 0,
368a94aec03SShunli Wang 			      NULL, 0),
369a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S(I2S1_HD_EN_W_NAME, SUPPLY_SEQ_I2S_HD_EN,
370a94aec03SShunli Wang 			      AFE_I2S_CON1, I2S2_HD_EN_SFT, 0,
371a94aec03SShunli Wang 			      NULL, 0),
372a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S(I2S2_HD_EN_W_NAME, SUPPLY_SEQ_I2S_HD_EN,
373a94aec03SShunli Wang 			      AFE_I2S_CON2, I2S3_HD_EN_SFT, 0,
374a94aec03SShunli Wang 			      NULL, 0),
375a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S(I2S3_HD_EN_W_NAME, SUPPLY_SEQ_I2S_HD_EN,
376a94aec03SShunli Wang 			      AFE_I2S_CON3, I2S4_HD_EN_SFT, 0,
377a94aec03SShunli Wang 			      NULL, 0),
378a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S(I2S5_HD_EN_W_NAME, SUPPLY_SEQ_I2S_HD_EN,
379a94aec03SShunli Wang 			      AFE_I2S_CON4, I2S5_HD_EN_SFT, 0,
380a94aec03SShunli Wang 			      NULL, 0),
381a94aec03SShunli Wang 
382a94aec03SShunli Wang 	/* i2s mclk en */
383a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S(I2S0_MCLK_EN_W_NAME, SUPPLY_SEQ_I2S_MCLK_EN,
384a94aec03SShunli Wang 			      SND_SOC_NOPM, 0, 0,
385a94aec03SShunli Wang 			      mtk_mclk_en_event,
386a94aec03SShunli Wang 			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
387a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S(I2S1_MCLK_EN_W_NAME, SUPPLY_SEQ_I2S_MCLK_EN,
388a94aec03SShunli Wang 			      SND_SOC_NOPM, 0, 0,
389a94aec03SShunli Wang 			      mtk_mclk_en_event,
390a94aec03SShunli Wang 			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
391a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S(I2S2_MCLK_EN_W_NAME, SUPPLY_SEQ_I2S_MCLK_EN,
392a94aec03SShunli Wang 			      SND_SOC_NOPM, 0, 0,
393a94aec03SShunli Wang 			      mtk_mclk_en_event,
394a94aec03SShunli Wang 			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
395a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S(I2S3_MCLK_EN_W_NAME, SUPPLY_SEQ_I2S_MCLK_EN,
396a94aec03SShunli Wang 			      SND_SOC_NOPM, 0, 0,
397a94aec03SShunli Wang 			      mtk_mclk_en_event,
398a94aec03SShunli Wang 			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
399a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S(I2S5_MCLK_EN_W_NAME, SUPPLY_SEQ_I2S_MCLK_EN,
400a94aec03SShunli Wang 			      SND_SOC_NOPM, 0, 0,
401a94aec03SShunli Wang 			      mtk_mclk_en_event,
402a94aec03SShunli Wang 			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
403a94aec03SShunli Wang 
404a94aec03SShunli Wang 	/* apll */
405a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S(APLL1_W_NAME, SUPPLY_SEQ_APLL,
406a94aec03SShunli Wang 			      SND_SOC_NOPM, 0, 0,
407a94aec03SShunli Wang 			      mtk_apll_event,
408a94aec03SShunli Wang 			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
409a94aec03SShunli Wang 	SND_SOC_DAPM_SUPPLY_S(APLL2_W_NAME, SUPPLY_SEQ_APLL,
410a94aec03SShunli Wang 			      SND_SOC_NOPM, 0, 0,
411a94aec03SShunli Wang 			      mtk_apll_event,
412a94aec03SShunli Wang 			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
413a94aec03SShunli Wang };
414a94aec03SShunli Wang 
mtk_afe_i2s_share_connect(struct snd_soc_dapm_widget * source,struct snd_soc_dapm_widget * sink)415a94aec03SShunli Wang static int mtk_afe_i2s_share_connect(struct snd_soc_dapm_widget *source,
416a94aec03SShunli Wang 				     struct snd_soc_dapm_widget *sink)
417a94aec03SShunli Wang {
418a94aec03SShunli Wang 	struct snd_soc_dapm_widget *w = sink;
419a94aec03SShunli Wang 	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
420a94aec03SShunli Wang 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);
421a94aec03SShunli Wang 	struct mtk_afe_i2s_priv *i2s_priv;
422a94aec03SShunli Wang 
423a94aec03SShunli Wang 	i2s_priv = get_i2s_priv_by_name(afe, sink->name);
424a94aec03SShunli Wang 
425a94aec03SShunli Wang 	if (!i2s_priv) {
426a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), i2s_priv == NULL", __func__);
427a94aec03SShunli Wang 		return 0;
428a94aec03SShunli Wang 	}
429a94aec03SShunli Wang 
430a94aec03SShunli Wang 	if (i2s_priv->share_i2s_id < 0)
431a94aec03SShunli Wang 		return 0;
432a94aec03SShunli Wang 
433a94aec03SShunli Wang 	return i2s_priv->share_i2s_id == get_i2s_id_by_name(afe, source->name);
434a94aec03SShunli Wang }
435a94aec03SShunli Wang 
mtk_afe_i2s_hd_connect(struct snd_soc_dapm_widget * source,struct snd_soc_dapm_widget * sink)436a94aec03SShunli Wang static int mtk_afe_i2s_hd_connect(struct snd_soc_dapm_widget *source,
437a94aec03SShunli Wang 				  struct snd_soc_dapm_widget *sink)
438a94aec03SShunli Wang {
439a94aec03SShunli Wang 	struct snd_soc_dapm_widget *w = sink;
440a94aec03SShunli Wang 	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
441a94aec03SShunli Wang 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);
442a94aec03SShunli Wang 	struct mtk_afe_i2s_priv *i2s_priv;
443a94aec03SShunli Wang 
444a94aec03SShunli Wang 	i2s_priv = get_i2s_priv_by_name(afe, sink->name);
445a94aec03SShunli Wang 
446a94aec03SShunli Wang 	if (!i2s_priv) {
447a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), i2s_priv == NULL", __func__);
448a94aec03SShunli Wang 		return 0;
449a94aec03SShunli Wang 	}
450a94aec03SShunli Wang 
451a94aec03SShunli Wang 	if (get_i2s_id_by_name(afe, sink->name) ==
452a94aec03SShunli Wang 	    get_i2s_id_by_name(afe, source->name))
453a94aec03SShunli Wang 		return i2s_priv->low_jitter_en;
454a94aec03SShunli Wang 
455a94aec03SShunli Wang 	/* check if share i2s need hd en */
456a94aec03SShunli Wang 	if (i2s_priv->share_i2s_id < 0)
457a94aec03SShunli Wang 		return 0;
458a94aec03SShunli Wang 
459a94aec03SShunli Wang 	if (i2s_priv->share_i2s_id == get_i2s_id_by_name(afe, source->name))
460a94aec03SShunli Wang 		return i2s_priv->low_jitter_en;
461a94aec03SShunli Wang 
462a94aec03SShunli Wang 	return 0;
463a94aec03SShunli Wang }
464a94aec03SShunli Wang 
mtk_afe_i2s_apll_connect(struct snd_soc_dapm_widget * source,struct snd_soc_dapm_widget * sink)465a94aec03SShunli Wang static int mtk_afe_i2s_apll_connect(struct snd_soc_dapm_widget *source,
466a94aec03SShunli Wang 				    struct snd_soc_dapm_widget *sink)
467a94aec03SShunli Wang {
468a94aec03SShunli Wang 	struct snd_soc_dapm_widget *w = sink;
469a94aec03SShunli Wang 	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
470a94aec03SShunli Wang 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);
471a94aec03SShunli Wang 	struct mtk_afe_i2s_priv *i2s_priv;
472a94aec03SShunli Wang 	int cur_apll;
473a94aec03SShunli Wang 	int i2s_need_apll;
474a94aec03SShunli Wang 
475a94aec03SShunli Wang 	i2s_priv = get_i2s_priv_by_name(afe, w->name);
476a94aec03SShunli Wang 
477a94aec03SShunli Wang 	if (!i2s_priv) {
478a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), i2s_priv == NULL", __func__);
479a94aec03SShunli Wang 		return 0;
480a94aec03SShunli Wang 	}
481a94aec03SShunli Wang 
482a94aec03SShunli Wang 	/* which apll */
483a94aec03SShunli Wang 	cur_apll = mt8183_get_apll_by_name(afe, source->name);
484a94aec03SShunli Wang 
485a94aec03SShunli Wang 	/* choose APLL from i2s rate */
486a94aec03SShunli Wang 	i2s_need_apll = mt8183_get_apll_by_rate(afe, i2s_priv->rate);
487a94aec03SShunli Wang 
488a94aec03SShunli Wang 	return (i2s_need_apll == cur_apll) ? 1 : 0;
489a94aec03SShunli Wang }
490a94aec03SShunli Wang 
mtk_afe_i2s_mclk_connect(struct snd_soc_dapm_widget * source,struct snd_soc_dapm_widget * sink)491a94aec03SShunli Wang static int mtk_afe_i2s_mclk_connect(struct snd_soc_dapm_widget *source,
492a94aec03SShunli Wang 				    struct snd_soc_dapm_widget *sink)
493a94aec03SShunli Wang {
494a94aec03SShunli Wang 	struct snd_soc_dapm_widget *w = sink;
495a94aec03SShunli Wang 	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
496a94aec03SShunli Wang 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);
497a94aec03SShunli Wang 	struct mtk_afe_i2s_priv *i2s_priv;
498a94aec03SShunli Wang 
499a94aec03SShunli Wang 	i2s_priv = get_i2s_priv_by_name(afe, sink->name);
500a94aec03SShunli Wang 
501a94aec03SShunli Wang 	if (!i2s_priv) {
502a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), i2s_priv == NULL", __func__);
503a94aec03SShunli Wang 		return 0;
504a94aec03SShunli Wang 	}
505a94aec03SShunli Wang 
506a94aec03SShunli Wang 	if (get_i2s_id_by_name(afe, sink->name) ==
507a94aec03SShunli Wang 	    get_i2s_id_by_name(afe, source->name))
508a94aec03SShunli Wang 		return (i2s_priv->mclk_rate > 0) ? 1 : 0;
509a94aec03SShunli Wang 
510a94aec03SShunli Wang 	/* check if share i2s need mclk */
511a94aec03SShunli Wang 	if (i2s_priv->share_i2s_id < 0)
512a94aec03SShunli Wang 		return 0;
513a94aec03SShunli Wang 
514a94aec03SShunli Wang 	if (i2s_priv->share_i2s_id == get_i2s_id_by_name(afe, source->name))
515a94aec03SShunli Wang 		return (i2s_priv->mclk_rate > 0) ? 1 : 0;
516a94aec03SShunli Wang 
517a94aec03SShunli Wang 	return 0;
518a94aec03SShunli Wang }
519a94aec03SShunli Wang 
mtk_afe_mclk_apll_connect(struct snd_soc_dapm_widget * source,struct snd_soc_dapm_widget * sink)520a94aec03SShunli Wang static int mtk_afe_mclk_apll_connect(struct snd_soc_dapm_widget *source,
521a94aec03SShunli Wang 				     struct snd_soc_dapm_widget *sink)
522a94aec03SShunli Wang {
523a94aec03SShunli Wang 	struct snd_soc_dapm_widget *w = sink;
524a94aec03SShunli Wang 	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
525a94aec03SShunli Wang 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);
526a94aec03SShunli Wang 	struct mtk_afe_i2s_priv *i2s_priv;
527a94aec03SShunli Wang 	int cur_apll;
528a94aec03SShunli Wang 
529a94aec03SShunli Wang 	i2s_priv = get_i2s_priv_by_name(afe, w->name);
530a94aec03SShunli Wang 
531a94aec03SShunli Wang 	if (!i2s_priv) {
532a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), i2s_priv == NULL", __func__);
533a94aec03SShunli Wang 		return 0;
534a94aec03SShunli Wang 	}
535a94aec03SShunli Wang 
536a94aec03SShunli Wang 	/* which apll */
537a94aec03SShunli Wang 	cur_apll = mt8183_get_apll_by_name(afe, source->name);
538a94aec03SShunli Wang 
539a94aec03SShunli Wang 	return (i2s_priv->mclk_apll == cur_apll) ? 1 : 0;
540a94aec03SShunli Wang }
541a94aec03SShunli Wang 
542a94aec03SShunli Wang static const struct snd_soc_dapm_route mtk_dai_i2s_routes[] = {
543a94aec03SShunli Wang 	/* i2s0 */
544a94aec03SShunli Wang 	{"I2S0", NULL, "I2S0_EN"},
545a94aec03SShunli Wang 	{"I2S0", NULL, "I2S1_EN", mtk_afe_i2s_share_connect},
546a94aec03SShunli Wang 	{"I2S0", NULL, "I2S2_EN", mtk_afe_i2s_share_connect},
547a94aec03SShunli Wang 	{"I2S0", NULL, "I2S3_EN", mtk_afe_i2s_share_connect},
548a94aec03SShunli Wang 	{"I2S0", NULL, "I2S5_EN", mtk_afe_i2s_share_connect},
549a94aec03SShunli Wang 
550a94aec03SShunli Wang 	{"I2S0", NULL, I2S0_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
551a94aec03SShunli Wang 	{"I2S0", NULL, I2S1_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
552a94aec03SShunli Wang 	{"I2S0", NULL, I2S2_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
553a94aec03SShunli Wang 	{"I2S0", NULL, I2S3_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
554a94aec03SShunli Wang 	{"I2S0", NULL, I2S5_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
555a94aec03SShunli Wang 	{I2S0_HD_EN_W_NAME, NULL, APLL1_W_NAME, mtk_afe_i2s_apll_connect},
556a94aec03SShunli Wang 	{I2S0_HD_EN_W_NAME, NULL, APLL2_W_NAME, mtk_afe_i2s_apll_connect},
557a94aec03SShunli Wang 
558a94aec03SShunli Wang 	{"I2S0", NULL, I2S0_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
559a94aec03SShunli Wang 	{"I2S0", NULL, I2S1_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
560a94aec03SShunli Wang 	{"I2S0", NULL, I2S2_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
561a94aec03SShunli Wang 	{"I2S0", NULL, I2S3_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
562a94aec03SShunli Wang 	{"I2S0", NULL, I2S5_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
563a94aec03SShunli Wang 	{I2S0_MCLK_EN_W_NAME, NULL, APLL1_W_NAME, mtk_afe_mclk_apll_connect},
564a94aec03SShunli Wang 	{I2S0_MCLK_EN_W_NAME, NULL, APLL2_W_NAME, mtk_afe_mclk_apll_connect},
565a94aec03SShunli Wang 
566a94aec03SShunli Wang 	/* i2s1 */
567a94aec03SShunli Wang 	{"I2S1_CH1", "DL1_CH1", "DL1"},
568a94aec03SShunli Wang 	{"I2S1_CH2", "DL1_CH2", "DL1"},
569a94aec03SShunli Wang 
570a94aec03SShunli Wang 	{"I2S1_CH1", "DL2_CH1", "DL2"},
571a94aec03SShunli Wang 	{"I2S1_CH2", "DL2_CH2", "DL2"},
572a94aec03SShunli Wang 
573a94aec03SShunli Wang 	{"I2S1_CH1", "DL3_CH1", "DL3"},
574a94aec03SShunli Wang 	{"I2S1_CH2", "DL3_CH2", "DL3"},
575a94aec03SShunli Wang 
576a94aec03SShunli Wang 	{"I2S1", NULL, "I2S1_CH1"},
577a94aec03SShunli Wang 	{"I2S1", NULL, "I2S1_CH2"},
578a94aec03SShunli Wang 
579a94aec03SShunli Wang 	{"I2S1", NULL, "I2S0_EN", mtk_afe_i2s_share_connect},
580a94aec03SShunli Wang 	{"I2S1", NULL, "I2S1_EN"},
581a94aec03SShunli Wang 	{"I2S1", NULL, "I2S2_EN", mtk_afe_i2s_share_connect},
582a94aec03SShunli Wang 	{"I2S1", NULL, "I2S3_EN", mtk_afe_i2s_share_connect},
583a94aec03SShunli Wang 	{"I2S1", NULL, "I2S5_EN", mtk_afe_i2s_share_connect},
584a94aec03SShunli Wang 
585a94aec03SShunli Wang 	{"I2S1", NULL, I2S0_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
586a94aec03SShunli Wang 	{"I2S1", NULL, I2S1_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
587a94aec03SShunli Wang 	{"I2S1", NULL, I2S2_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
588a94aec03SShunli Wang 	{"I2S1", NULL, I2S3_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
589a94aec03SShunli Wang 	{"I2S1", NULL, I2S5_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
590a94aec03SShunli Wang 	{I2S1_HD_EN_W_NAME, NULL, APLL1_W_NAME, mtk_afe_i2s_apll_connect},
591a94aec03SShunli Wang 	{I2S1_HD_EN_W_NAME, NULL, APLL2_W_NAME, mtk_afe_i2s_apll_connect},
592a94aec03SShunli Wang 
593a94aec03SShunli Wang 	{"I2S1", NULL, I2S0_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
594a94aec03SShunli Wang 	{"I2S1", NULL, I2S1_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
595a94aec03SShunli Wang 	{"I2S1", NULL, I2S2_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
596a94aec03SShunli Wang 	{"I2S1", NULL, I2S3_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
597a94aec03SShunli Wang 	{"I2S1", NULL, I2S5_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
598a94aec03SShunli Wang 	{I2S1_MCLK_EN_W_NAME, NULL, APLL1_W_NAME, mtk_afe_mclk_apll_connect},
599a94aec03SShunli Wang 	{I2S1_MCLK_EN_W_NAME, NULL, APLL2_W_NAME, mtk_afe_mclk_apll_connect},
600a94aec03SShunli Wang 
601a94aec03SShunli Wang 	/* i2s2 */
602a94aec03SShunli Wang 	{"I2S2", NULL, "I2S0_EN", mtk_afe_i2s_share_connect},
603a94aec03SShunli Wang 	{"I2S2", NULL, "I2S1_EN", mtk_afe_i2s_share_connect},
604a94aec03SShunli Wang 	{"I2S2", NULL, "I2S2_EN"},
605a94aec03SShunli Wang 	{"I2S2", NULL, "I2S3_EN", mtk_afe_i2s_share_connect},
606a94aec03SShunli Wang 	{"I2S2", NULL, "I2S5_EN", mtk_afe_i2s_share_connect},
607a94aec03SShunli Wang 
608a94aec03SShunli Wang 	{"I2S2", NULL, I2S0_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
609a94aec03SShunli Wang 	{"I2S2", NULL, I2S1_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
610a94aec03SShunli Wang 	{"I2S2", NULL, I2S2_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
611a94aec03SShunli Wang 	{"I2S2", NULL, I2S3_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
612a94aec03SShunli Wang 	{"I2S2", NULL, I2S5_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
613a94aec03SShunli Wang 	{I2S2_HD_EN_W_NAME, NULL, APLL1_W_NAME, mtk_afe_i2s_apll_connect},
614a94aec03SShunli Wang 	{I2S2_HD_EN_W_NAME, NULL, APLL2_W_NAME, mtk_afe_i2s_apll_connect},
615a94aec03SShunli Wang 
616a94aec03SShunli Wang 	{"I2S2", NULL, I2S0_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
617a94aec03SShunli Wang 	{"I2S2", NULL, I2S1_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
618a94aec03SShunli Wang 	{"I2S2", NULL, I2S2_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
619a94aec03SShunli Wang 	{"I2S2", NULL, I2S3_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
620a94aec03SShunli Wang 	{"I2S2", NULL, I2S5_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
621a94aec03SShunli Wang 	{I2S2_MCLK_EN_W_NAME, NULL, APLL1_W_NAME, mtk_afe_mclk_apll_connect},
622a94aec03SShunli Wang 	{I2S2_MCLK_EN_W_NAME, NULL, APLL2_W_NAME, mtk_afe_mclk_apll_connect},
623a94aec03SShunli Wang 
624a94aec03SShunli Wang 	/* i2s3 */
625a94aec03SShunli Wang 	{"I2S3_CH1", "DL1_CH1", "DL1"},
626a94aec03SShunli Wang 	{"I2S3_CH2", "DL1_CH2", "DL1"},
627a94aec03SShunli Wang 
628a94aec03SShunli Wang 	{"I2S3_CH1", "DL2_CH1", "DL2"},
629a94aec03SShunli Wang 	{"I2S3_CH2", "DL2_CH2", "DL2"},
630a94aec03SShunli Wang 
631a94aec03SShunli Wang 	{"I2S3_CH1", "DL3_CH1", "DL3"},
632a94aec03SShunli Wang 	{"I2S3_CH2", "DL3_CH2", "DL3"},
633a94aec03SShunli Wang 
634a94aec03SShunli Wang 	{"I2S3", NULL, "I2S3_CH1"},
635a94aec03SShunli Wang 	{"I2S3", NULL, "I2S3_CH2"},
636a94aec03SShunli Wang 
637a94aec03SShunli Wang 	{"I2S3", NULL, "I2S0_EN", mtk_afe_i2s_share_connect},
638a94aec03SShunli Wang 	{"I2S3", NULL, "I2S1_EN", mtk_afe_i2s_share_connect},
639a94aec03SShunli Wang 	{"I2S3", NULL, "I2S2_EN", mtk_afe_i2s_share_connect},
640a94aec03SShunli Wang 	{"I2S3", NULL, "I2S3_EN"},
641a94aec03SShunli Wang 	{"I2S3", NULL, "I2S5_EN", mtk_afe_i2s_share_connect},
642a94aec03SShunli Wang 
643a94aec03SShunli Wang 	{"I2S3", NULL, I2S0_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
644a94aec03SShunli Wang 	{"I2S3", NULL, I2S1_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
645a94aec03SShunli Wang 	{"I2S3", NULL, I2S2_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
646a94aec03SShunli Wang 	{"I2S3", NULL, I2S3_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
647a94aec03SShunli Wang 	{"I2S3", NULL, I2S5_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
648a94aec03SShunli Wang 	{I2S3_HD_EN_W_NAME, NULL, APLL1_W_NAME, mtk_afe_i2s_apll_connect},
649a94aec03SShunli Wang 	{I2S3_HD_EN_W_NAME, NULL, APLL2_W_NAME, mtk_afe_i2s_apll_connect},
650a94aec03SShunli Wang 
651a94aec03SShunli Wang 	{"I2S3", NULL, I2S0_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
652a94aec03SShunli Wang 	{"I2S3", NULL, I2S1_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
653a94aec03SShunli Wang 	{"I2S3", NULL, I2S2_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
654a94aec03SShunli Wang 	{"I2S3", NULL, I2S3_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
655a94aec03SShunli Wang 	{"I2S3", NULL, I2S5_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
656a94aec03SShunli Wang 	{I2S3_MCLK_EN_W_NAME, NULL, APLL1_W_NAME, mtk_afe_mclk_apll_connect},
657a94aec03SShunli Wang 	{I2S3_MCLK_EN_W_NAME, NULL, APLL2_W_NAME, mtk_afe_mclk_apll_connect},
658a94aec03SShunli Wang 
659a94aec03SShunli Wang 	/* i2s5 */
660a94aec03SShunli Wang 	{"I2S5_CH1", "DL1_CH1", "DL1"},
661a94aec03SShunli Wang 	{"I2S5_CH2", "DL1_CH2", "DL1"},
662a94aec03SShunli Wang 
663a94aec03SShunli Wang 	{"I2S5_CH1", "DL2_CH1", "DL2"},
664a94aec03SShunli Wang 	{"I2S5_CH2", "DL2_CH2", "DL2"},
665a94aec03SShunli Wang 
666a94aec03SShunli Wang 	{"I2S5_CH1", "DL3_CH1", "DL3"},
667a94aec03SShunli Wang 	{"I2S5_CH2", "DL3_CH2", "DL3"},
668a94aec03SShunli Wang 
669a94aec03SShunli Wang 	{"I2S5", NULL, "I2S5_CH1"},
670a94aec03SShunli Wang 	{"I2S5", NULL, "I2S5_CH2"},
671a94aec03SShunli Wang 
672a94aec03SShunli Wang 	{"I2S5", NULL, "I2S0_EN", mtk_afe_i2s_share_connect},
673a94aec03SShunli Wang 	{"I2S5", NULL, "I2S1_EN", mtk_afe_i2s_share_connect},
674a94aec03SShunli Wang 	{"I2S5", NULL, "I2S2_EN", mtk_afe_i2s_share_connect},
675a94aec03SShunli Wang 	{"I2S5", NULL, "I2S3_EN", mtk_afe_i2s_share_connect},
676a94aec03SShunli Wang 	{"I2S5", NULL, "I2S5_EN"},
677a94aec03SShunli Wang 
678a94aec03SShunli Wang 	{"I2S5", NULL, I2S0_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
679a94aec03SShunli Wang 	{"I2S5", NULL, I2S1_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
680a94aec03SShunli Wang 	{"I2S5", NULL, I2S2_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
681a94aec03SShunli Wang 	{"I2S5", NULL, I2S3_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
682a94aec03SShunli Wang 	{"I2S5", NULL, I2S5_HD_EN_W_NAME, mtk_afe_i2s_hd_connect},
683a94aec03SShunli Wang 	{I2S5_HD_EN_W_NAME, NULL, APLL1_W_NAME, mtk_afe_i2s_apll_connect},
684a94aec03SShunli Wang 	{I2S5_HD_EN_W_NAME, NULL, APLL2_W_NAME, mtk_afe_i2s_apll_connect},
685a94aec03SShunli Wang 
686a94aec03SShunli Wang 	{"I2S5", NULL, I2S0_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
687a94aec03SShunli Wang 	{"I2S5", NULL, I2S1_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
688a94aec03SShunli Wang 	{"I2S5", NULL, I2S2_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
689a94aec03SShunli Wang 	{"I2S5", NULL, I2S3_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
690a94aec03SShunli Wang 	{"I2S5", NULL, I2S5_MCLK_EN_W_NAME, mtk_afe_i2s_mclk_connect},
691a94aec03SShunli Wang 	{I2S5_MCLK_EN_W_NAME, NULL, APLL1_W_NAME, mtk_afe_mclk_apll_connect},
692a94aec03SShunli Wang 	{I2S5_MCLK_EN_W_NAME, NULL, APLL2_W_NAME, mtk_afe_mclk_apll_connect},
693a94aec03SShunli Wang };
694a94aec03SShunli Wang 
695a94aec03SShunli Wang /* dai ops */
mtk_dai_i2s_config(struct mtk_base_afe * afe,struct snd_pcm_hw_params * params,int i2s_id)696a94aec03SShunli Wang static int mtk_dai_i2s_config(struct mtk_base_afe *afe,
697a94aec03SShunli Wang 			      struct snd_pcm_hw_params *params,
698a94aec03SShunli Wang 			      int i2s_id)
699a94aec03SShunli Wang {
700a94aec03SShunli Wang 	struct mt8183_afe_private *afe_priv = afe->platform_priv;
701a94aec03SShunli Wang 	struct mtk_afe_i2s_priv *i2s_priv = afe_priv->dai_priv[i2s_id];
702a94aec03SShunli Wang 
703a94aec03SShunli Wang 	unsigned int rate = params_rate(params);
704a94aec03SShunli Wang 	unsigned int rate_reg = mt8183_rate_transform(afe->dev,
705a94aec03SShunli Wang 						      rate, i2s_id);
706a94aec03SShunli Wang 	snd_pcm_format_t format = params_format(params);
707767ee388STzung-Bi Shih 	unsigned int i2s_con = 0, fmt_con = I2S_FMT_I2S << I2S_FMT_SFT;
708a94aec03SShunli Wang 	int ret = 0;
709a94aec03SShunli Wang 
710767ee388STzung-Bi Shih 	if (i2s_priv) {
711a94aec03SShunli Wang 		i2s_priv->rate = rate;
712767ee388STzung-Bi Shih 
713767ee388STzung-Bi Shih 		if (i2s_priv->use_eiaj)
714767ee388STzung-Bi Shih 			fmt_con = I2S_FMT_EIAJ << I2S_FMT_SFT;
715767ee388STzung-Bi Shih 	} else {
716a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), i2s_priv == NULL", __func__);
717767ee388STzung-Bi Shih 	}
718a94aec03SShunli Wang 
719a94aec03SShunli Wang 	switch (i2s_id) {
720a94aec03SShunli Wang 	case MT8183_DAI_I2S_0:
721a94aec03SShunli Wang 		regmap_update_bits(afe->regmap, AFE_DAC_CON1,
722a94aec03SShunli Wang 				   I2S_MODE_MASK_SFT, rate_reg << I2S_MODE_SFT);
723a94aec03SShunli Wang 		i2s_con = I2S_IN_PAD_IO_MUX << I2SIN_PAD_SEL_SFT;
724767ee388STzung-Bi Shih 		i2s_con |= fmt_con;
725a94aec03SShunli Wang 		i2s_con |= get_i2s_wlen(format) << I2S_WLEN_SFT;
726a94aec03SShunli Wang 		regmap_update_bits(afe->regmap, AFE_I2S_CON,
727a94aec03SShunli Wang 				   0xffffeffe, i2s_con);
728a94aec03SShunli Wang 		break;
729a94aec03SShunli Wang 	case MT8183_DAI_I2S_1:
730a94aec03SShunli Wang 		i2s_con = I2S1_SEL_O28_O29 << I2S2_SEL_O03_O04_SFT;
731a94aec03SShunli Wang 		i2s_con |= rate_reg << I2S2_OUT_MODE_SFT;
732767ee388STzung-Bi Shih 		i2s_con |= fmt_con;
733a94aec03SShunli Wang 		i2s_con |= get_i2s_wlen(format) << I2S2_WLEN_SFT;
734a94aec03SShunli Wang 		regmap_update_bits(afe->regmap, AFE_I2S_CON1,
735a94aec03SShunli Wang 				   0xffffeffe, i2s_con);
736a94aec03SShunli Wang 		break;
737a94aec03SShunli Wang 	case MT8183_DAI_I2S_2:
738a94aec03SShunli Wang 		i2s_con = 8 << I2S3_UPDATE_WORD_SFT;
739a94aec03SShunli Wang 		i2s_con |= rate_reg << I2S3_OUT_MODE_SFT;
740767ee388STzung-Bi Shih 		i2s_con |= fmt_con;
741a94aec03SShunli Wang 		i2s_con |= get_i2s_wlen(format) << I2S3_WLEN_SFT;
742a94aec03SShunli Wang 		regmap_update_bits(afe->regmap, AFE_I2S_CON2,
743a94aec03SShunli Wang 				   0xffffeffe, i2s_con);
744a94aec03SShunli Wang 		break;
745a94aec03SShunli Wang 	case MT8183_DAI_I2S_3:
746a94aec03SShunli Wang 		i2s_con = rate_reg << I2S4_OUT_MODE_SFT;
747767ee388STzung-Bi Shih 		i2s_con |= fmt_con;
748a94aec03SShunli Wang 		i2s_con |= get_i2s_wlen(format) << I2S4_WLEN_SFT;
749a94aec03SShunli Wang 		regmap_update_bits(afe->regmap, AFE_I2S_CON3,
750a94aec03SShunli Wang 				   0xffffeffe, i2s_con);
751a94aec03SShunli Wang 		break;
752a94aec03SShunli Wang 	case MT8183_DAI_I2S_5:
753a94aec03SShunli Wang 		i2s_con = rate_reg << I2S5_OUT_MODE_SFT;
754767ee388STzung-Bi Shih 		i2s_con |= fmt_con;
755a94aec03SShunli Wang 		i2s_con |= get_i2s_wlen(format) << I2S5_WLEN_SFT;
756a94aec03SShunli Wang 		regmap_update_bits(afe->regmap, AFE_I2S_CON4,
757a94aec03SShunli Wang 				   0xffffeffe, i2s_con);
758a94aec03SShunli Wang 		break;
759a94aec03SShunli Wang 	default:
760a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), id %d not support\n",
761a94aec03SShunli Wang 			 __func__, i2s_id);
762a94aec03SShunli Wang 		return -EINVAL;
763a94aec03SShunli Wang 	}
764a94aec03SShunli Wang 
765a94aec03SShunli Wang 	/* set share i2s */
766a94aec03SShunli Wang 	if (i2s_priv && i2s_priv->share_i2s_id >= 0)
767a94aec03SShunli Wang 		ret = mtk_dai_i2s_config(afe, params, i2s_priv->share_i2s_id);
768a94aec03SShunli Wang 
769a94aec03SShunli Wang 	return ret;
770a94aec03SShunli Wang }
771a94aec03SShunli Wang 
mtk_dai_i2s_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)772a94aec03SShunli Wang static int mtk_dai_i2s_hw_params(struct snd_pcm_substream *substream,
773a94aec03SShunli Wang 				 struct snd_pcm_hw_params *params,
774a94aec03SShunli Wang 				 struct snd_soc_dai *dai)
775a94aec03SShunli Wang {
776a94aec03SShunli Wang 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
777a94aec03SShunli Wang 
778a94aec03SShunli Wang 	return mtk_dai_i2s_config(afe, params, dai->id);
779a94aec03SShunli Wang }
780a94aec03SShunli Wang 
mtk_dai_i2s_set_sysclk(struct snd_soc_dai * dai,int clk_id,unsigned int freq,int dir)781a94aec03SShunli Wang static int mtk_dai_i2s_set_sysclk(struct snd_soc_dai *dai,
782a94aec03SShunli Wang 				  int clk_id, unsigned int freq, int dir)
783a94aec03SShunli Wang {
784a94aec03SShunli Wang 	struct mtk_base_afe *afe = dev_get_drvdata(dai->dev);
785a94aec03SShunli Wang 	struct mt8183_afe_private *afe_priv = afe->platform_priv;
786a94aec03SShunli Wang 	struct mtk_afe_i2s_priv *i2s_priv = afe_priv->dai_priv[dai->id];
787a94aec03SShunli Wang 	int apll;
788a94aec03SShunli Wang 	int apll_rate;
789a94aec03SShunli Wang 
790a94aec03SShunli Wang 	if (!i2s_priv) {
791a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), i2s_priv == NULL", __func__);
792a94aec03SShunli Wang 		return -EINVAL;
793a94aec03SShunli Wang 	}
794a94aec03SShunli Wang 
795a94aec03SShunli Wang 	if (dir != SND_SOC_CLOCK_OUT) {
796a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), dir != SND_SOC_CLOCK_OUT", __func__);
797a94aec03SShunli Wang 		return -EINVAL;
798a94aec03SShunli Wang 	}
799a94aec03SShunli Wang 
800a94aec03SShunli Wang 	apll = mt8183_get_apll_by_rate(afe, freq);
801a94aec03SShunli Wang 	apll_rate = mt8183_get_apll_rate(afe, apll);
802a94aec03SShunli Wang 
803a94aec03SShunli Wang 	if (freq > apll_rate) {
804a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), freq > apll rate", __func__);
805a94aec03SShunli Wang 		return -EINVAL;
806a94aec03SShunli Wang 	}
807a94aec03SShunli Wang 
808a94aec03SShunli Wang 	if (apll_rate % freq != 0) {
809a94aec03SShunli Wang 		dev_warn(afe->dev, "%s(), APLL cannot generate freq Hz",
810a94aec03SShunli Wang 			 __func__);
811a94aec03SShunli Wang 		return -EINVAL;
812a94aec03SShunli Wang 	}
813a94aec03SShunli Wang 
814a94aec03SShunli Wang 	i2s_priv->mclk_rate = freq;
815a94aec03SShunli Wang 	i2s_priv->mclk_apll = apll;
816a94aec03SShunli Wang 
817a94aec03SShunli Wang 	if (i2s_priv->share_i2s_id > 0) {
818a94aec03SShunli Wang 		struct mtk_afe_i2s_priv *share_i2s_priv;
819a94aec03SShunli Wang 
820a94aec03SShunli Wang 		share_i2s_priv = afe_priv->dai_priv[i2s_priv->share_i2s_id];
821a94aec03SShunli Wang 		if (!share_i2s_priv) {
822a94aec03SShunli Wang 			dev_warn(afe->dev, "%s(), share_i2s_priv == NULL",
823a94aec03SShunli Wang 				 __func__);
824a94aec03SShunli Wang 			return -EINVAL;
825a94aec03SShunli Wang 		}
826a94aec03SShunli Wang 
827a94aec03SShunli Wang 		share_i2s_priv->mclk_rate = i2s_priv->mclk_rate;
828a94aec03SShunli Wang 		share_i2s_priv->mclk_apll = i2s_priv->mclk_apll;
829a94aec03SShunli Wang 	}
830a94aec03SShunli Wang 
831a94aec03SShunli Wang 	return 0;
832a94aec03SShunli Wang }
833a94aec03SShunli Wang 
mtk_dai_i2s_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)834767ee388STzung-Bi Shih static int mtk_dai_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
835767ee388STzung-Bi Shih {
836767ee388STzung-Bi Shih 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
837767ee388STzung-Bi Shih 	struct mt8183_afe_private *afe_priv = afe->platform_priv;
838767ee388STzung-Bi Shih 	struct mtk_afe_i2s_priv *i2s_priv;
839767ee388STzung-Bi Shih 
840767ee388STzung-Bi Shih 	switch (dai->id) {
841767ee388STzung-Bi Shih 	case MT8183_DAI_I2S_0:
842767ee388STzung-Bi Shih 	case MT8183_DAI_I2S_1:
843767ee388STzung-Bi Shih 	case MT8183_DAI_I2S_2:
844767ee388STzung-Bi Shih 	case MT8183_DAI_I2S_3:
845767ee388STzung-Bi Shih 	case MT8183_DAI_I2S_5:
846767ee388STzung-Bi Shih 		break;
847767ee388STzung-Bi Shih 	default:
848767ee388STzung-Bi Shih 		dev_warn(afe->dev, "%s(), id %d not support\n",
849767ee388STzung-Bi Shih 			 __func__, dai->id);
850767ee388STzung-Bi Shih 		return -EINVAL;
851767ee388STzung-Bi Shih 	}
852767ee388STzung-Bi Shih 	i2s_priv = afe_priv->dai_priv[dai->id];
853767ee388STzung-Bi Shih 
854767ee388STzung-Bi Shih 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
855767ee388STzung-Bi Shih 	case SND_SOC_DAIFMT_LEFT_J:
856767ee388STzung-Bi Shih 		i2s_priv->use_eiaj = 1;
857767ee388STzung-Bi Shih 		break;
858767ee388STzung-Bi Shih 	case SND_SOC_DAIFMT_I2S:
859767ee388STzung-Bi Shih 		i2s_priv->use_eiaj = 0;
860767ee388STzung-Bi Shih 		break;
861767ee388STzung-Bi Shih 	default:
862767ee388STzung-Bi Shih 		dev_warn(afe->dev, "%s(), DAI format %d not support\n",
863767ee388STzung-Bi Shih 			 __func__, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
864767ee388STzung-Bi Shih 		return -EINVAL;
865767ee388STzung-Bi Shih 	}
866767ee388STzung-Bi Shih 
867767ee388STzung-Bi Shih 	return 0;
868767ee388STzung-Bi Shih }
869767ee388STzung-Bi Shih 
870a94aec03SShunli Wang static const struct snd_soc_dai_ops mtk_dai_i2s_ops = {
871a94aec03SShunli Wang 	.hw_params = mtk_dai_i2s_hw_params,
872a94aec03SShunli Wang 	.set_sysclk = mtk_dai_i2s_set_sysclk,
873767ee388STzung-Bi Shih 	.set_fmt = mtk_dai_i2s_set_fmt,
874a94aec03SShunli Wang };
875a94aec03SShunli Wang 
876a94aec03SShunli Wang /* dai driver */
877a94aec03SShunli Wang #define MTK_I2S_RATES (SNDRV_PCM_RATE_8000_48000 |\
878a94aec03SShunli Wang 		       SNDRV_PCM_RATE_88200 |\
879a94aec03SShunli Wang 		       SNDRV_PCM_RATE_96000 |\
880a94aec03SShunli Wang 		       SNDRV_PCM_RATE_176400 |\
881a94aec03SShunli Wang 		       SNDRV_PCM_RATE_192000)
882a94aec03SShunli Wang 
883a94aec03SShunli Wang #define MTK_I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
884a94aec03SShunli Wang 			 SNDRV_PCM_FMTBIT_S24_LE |\
885a94aec03SShunli Wang 			 SNDRV_PCM_FMTBIT_S32_LE)
886a94aec03SShunli Wang 
887a94aec03SShunli Wang static struct snd_soc_dai_driver mtk_dai_i2s_driver[] = {
888a94aec03SShunli Wang 	{
889a94aec03SShunli Wang 		.name = "I2S0",
890a94aec03SShunli Wang 		.id = MT8183_DAI_I2S_0,
891a94aec03SShunli Wang 		.capture = {
892a94aec03SShunli Wang 			.stream_name = "I2S0",
893a94aec03SShunli Wang 			.channels_min = 1,
894a94aec03SShunli Wang 			.channels_max = 2,
895a94aec03SShunli Wang 			.rates = MTK_I2S_RATES,
896a94aec03SShunli Wang 			.formats = MTK_I2S_FORMATS,
897a94aec03SShunli Wang 		},
898a94aec03SShunli Wang 		.ops = &mtk_dai_i2s_ops,
899a94aec03SShunli Wang 	},
900a94aec03SShunli Wang 	{
901a94aec03SShunli Wang 		.name = "I2S1",
902a94aec03SShunli Wang 		.id = MT8183_DAI_I2S_1,
903a94aec03SShunli Wang 		.playback = {
904a94aec03SShunli Wang 			.stream_name = "I2S1",
905a94aec03SShunli Wang 			.channels_min = 1,
906a94aec03SShunli Wang 			.channels_max = 2,
907a94aec03SShunli Wang 			.rates = MTK_I2S_RATES,
908a94aec03SShunli Wang 			.formats = MTK_I2S_FORMATS,
909a94aec03SShunli Wang 		},
910a94aec03SShunli Wang 		.ops = &mtk_dai_i2s_ops,
911a94aec03SShunli Wang 	},
912a94aec03SShunli Wang 	{
913a94aec03SShunli Wang 		.name = "I2S2",
914a94aec03SShunli Wang 		.id = MT8183_DAI_I2S_2,
915a94aec03SShunli Wang 		.capture = {
916a94aec03SShunli Wang 			.stream_name = "I2S2",
917a94aec03SShunli Wang 			.channels_min = 1,
918a94aec03SShunli Wang 			.channels_max = 2,
919a94aec03SShunli Wang 			.rates = MTK_I2S_RATES,
920a94aec03SShunli Wang 			.formats = MTK_I2S_FORMATS,
921a94aec03SShunli Wang 		},
922a94aec03SShunli Wang 		.ops = &mtk_dai_i2s_ops,
923a94aec03SShunli Wang 	},
924a94aec03SShunli Wang 	{
925a94aec03SShunli Wang 		.name = "I2S3",
926a94aec03SShunli Wang 		.id = MT8183_DAI_I2S_3,
927a94aec03SShunli Wang 		.playback = {
928a94aec03SShunli Wang 			.stream_name = "I2S3",
929a94aec03SShunli Wang 			.channels_min = 1,
930a94aec03SShunli Wang 			.channels_max = 2,
931a94aec03SShunli Wang 			.rates = MTK_I2S_RATES,
932a94aec03SShunli Wang 			.formats = MTK_I2S_FORMATS,
933a94aec03SShunli Wang 		},
934a94aec03SShunli Wang 		.ops = &mtk_dai_i2s_ops,
935a94aec03SShunli Wang 	},
936a94aec03SShunli Wang 	{
937a94aec03SShunli Wang 		.name = "I2S5",
938a94aec03SShunli Wang 		.id = MT8183_DAI_I2S_5,
939a94aec03SShunli Wang 		.playback = {
940a94aec03SShunli Wang 			.stream_name = "I2S5",
941a94aec03SShunli Wang 			.channels_min = 1,
942a94aec03SShunli Wang 			.channels_max = 2,
943a94aec03SShunli Wang 			.rates = MTK_I2S_RATES,
944a94aec03SShunli Wang 			.formats = MTK_I2S_FORMATS,
945a94aec03SShunli Wang 		},
946a94aec03SShunli Wang 		.ops = &mtk_dai_i2s_ops,
947a94aec03SShunli Wang 	},
948a94aec03SShunli Wang };
949a94aec03SShunli Wang 
950a94aec03SShunli Wang /* this enum is merely for mtk_afe_i2s_priv declare */
951a94aec03SShunli Wang enum {
952a94aec03SShunli Wang 	DAI_I2S0 = 0,
953a94aec03SShunli Wang 	DAI_I2S1,
954a94aec03SShunli Wang 	DAI_I2S2,
955a94aec03SShunli Wang 	DAI_I2S3,
956a94aec03SShunli Wang 	DAI_I2S5,
957a94aec03SShunli Wang 	DAI_I2S_NUM,
958a94aec03SShunli Wang };
959a94aec03SShunli Wang 
960a94aec03SShunli Wang static const struct mtk_afe_i2s_priv mt8183_i2s_priv[DAI_I2S_NUM] = {
961a94aec03SShunli Wang 	[DAI_I2S0] = {
962a94aec03SShunli Wang 		.id = MT8183_DAI_I2S_0,
963a94aec03SShunli Wang 		.mclk_id = MT8183_I2S0_MCK,
964a94aec03SShunli Wang 		.share_i2s_id = -1,
965a94aec03SShunli Wang 	},
966a94aec03SShunli Wang 	[DAI_I2S1] = {
967a94aec03SShunli Wang 		.id = MT8183_DAI_I2S_1,
968a94aec03SShunli Wang 		.mclk_id = MT8183_I2S1_MCK,
969a94aec03SShunli Wang 		.share_i2s_id = -1,
970a94aec03SShunli Wang 	},
971a94aec03SShunli Wang 	[DAI_I2S2] = {
972a94aec03SShunli Wang 		.id = MT8183_DAI_I2S_2,
973a94aec03SShunli Wang 		.mclk_id = MT8183_I2S2_MCK,
974a94aec03SShunli Wang 		.share_i2s_id = -1,
975a94aec03SShunli Wang 	},
976a94aec03SShunli Wang 	[DAI_I2S3] = {
977a94aec03SShunli Wang 		.id = MT8183_DAI_I2S_3,
978a94aec03SShunli Wang 		.mclk_id = MT8183_I2S3_MCK,
979a94aec03SShunli Wang 		.share_i2s_id = -1,
980a94aec03SShunli Wang 	},
981a94aec03SShunli Wang 	[DAI_I2S5] = {
982a94aec03SShunli Wang 		.id = MT8183_DAI_I2S_5,
983a94aec03SShunli Wang 		.mclk_id = MT8183_I2S5_MCK,
984a94aec03SShunli Wang 		.share_i2s_id = -1,
985a94aec03SShunli Wang 	},
986a94aec03SShunli Wang };
987a94aec03SShunli Wang 
988fea84890SNícolas F. R. A. Prado /**
989fea84890SNícolas F. R. A. Prado  * mt8183_dai_i2s_set_share() - Set up I2S ports to share a single clock.
990fea84890SNícolas F. R. A. Prado  * @afe: Pointer to &struct mtk_base_afe
991fea84890SNícolas F. R. A. Prado  * @main_i2s_name: The name of the I2S port that will provide the clock
992fea84890SNícolas F. R. A. Prado  * @secondary_i2s_name: The name of the I2S port that will use this clock
993fea84890SNícolas F. R. A. Prado  */
mt8183_dai_i2s_set_share(struct mtk_base_afe * afe,const char * main_i2s_name,const char * secondary_i2s_name)994fea84890SNícolas F. R. A. Prado int mt8183_dai_i2s_set_share(struct mtk_base_afe *afe, const char *main_i2s_name,
995fea84890SNícolas F. R. A. Prado 			     const char *secondary_i2s_name)
996fea84890SNícolas F. R. A. Prado {
997fea84890SNícolas F. R. A. Prado 	struct mtk_afe_i2s_priv *secondary_i2s_priv;
998fea84890SNícolas F. R. A. Prado 	int main_i2s_id;
999fea84890SNícolas F. R. A. Prado 
1000fea84890SNícolas F. R. A. Prado 	secondary_i2s_priv = get_i2s_priv_by_name(afe, secondary_i2s_name);
1001fea84890SNícolas F. R. A. Prado 	if (!secondary_i2s_priv)
1002fea84890SNícolas F. R. A. Prado 		return -EINVAL;
1003fea84890SNícolas F. R. A. Prado 
1004fea84890SNícolas F. R. A. Prado 	main_i2s_id = get_i2s_id_by_name(afe, main_i2s_name);
1005fea84890SNícolas F. R. A. Prado 	if (main_i2s_id < 0)
1006fea84890SNícolas F. R. A. Prado 		return main_i2s_id;
1007fea84890SNícolas F. R. A. Prado 
1008fea84890SNícolas F. R. A. Prado 	secondary_i2s_priv->share_i2s_id = main_i2s_id;
1009fea84890SNícolas F. R. A. Prado 
1010fea84890SNícolas F. R. A. Prado 	return 0;
1011fea84890SNícolas F. R. A. Prado }
1012fea84890SNícolas F. R. A. Prado EXPORT_SYMBOL_GPL(mt8183_dai_i2s_set_share);
1013fea84890SNícolas F. R. A. Prado 
mt8183_dai_i2s_set_priv(struct mtk_base_afe * afe)101403bf3aebSWei Yongjun static int mt8183_dai_i2s_set_priv(struct mtk_base_afe *afe)
1015a94aec03SShunli Wang {
1016a94aec03SShunli Wang 	struct mt8183_afe_private *afe_priv = afe->platform_priv;
1017a94aec03SShunli Wang 	struct mtk_afe_i2s_priv *i2s_priv;
1018a94aec03SShunli Wang 	int i;
1019a94aec03SShunli Wang 
1020a94aec03SShunli Wang 	for (i = 0; i < DAI_I2S_NUM; i++) {
1021a94aec03SShunli Wang 		i2s_priv = devm_kzalloc(afe->dev,
1022a94aec03SShunli Wang 					sizeof(struct mtk_afe_i2s_priv),
1023a94aec03SShunli Wang 					GFP_KERNEL);
1024a94aec03SShunli Wang 		if (!i2s_priv)
1025a94aec03SShunli Wang 			return -ENOMEM;
1026a94aec03SShunli Wang 
1027a94aec03SShunli Wang 		memcpy(i2s_priv, &mt8183_i2s_priv[i],
1028a94aec03SShunli Wang 		       sizeof(struct mtk_afe_i2s_priv));
1029a94aec03SShunli Wang 
1030a94aec03SShunli Wang 		afe_priv->dai_priv[mt8183_i2s_priv[i].id] = i2s_priv;
1031a94aec03SShunli Wang 	}
1032a94aec03SShunli Wang 
1033a94aec03SShunli Wang 	return 0;
1034a94aec03SShunli Wang }
1035a94aec03SShunli Wang 
mt8183_dai_i2s_register(struct mtk_base_afe * afe)1036a94aec03SShunli Wang int mt8183_dai_i2s_register(struct mtk_base_afe *afe)
1037a94aec03SShunli Wang {
1038a94aec03SShunli Wang 	struct mtk_base_afe_dai *dai;
1039a94aec03SShunli Wang 	int ret;
1040a94aec03SShunli Wang 
1041a94aec03SShunli Wang 	dai = devm_kzalloc(afe->dev, sizeof(*dai), GFP_KERNEL);
1042a94aec03SShunli Wang 	if (!dai)
1043a94aec03SShunli Wang 		return -ENOMEM;
1044a94aec03SShunli Wang 
1045a94aec03SShunli Wang 	list_add(&dai->list, &afe->sub_dais);
1046a94aec03SShunli Wang 
1047a94aec03SShunli Wang 	dai->dai_drivers = mtk_dai_i2s_driver;
1048a94aec03SShunli Wang 	dai->num_dai_drivers = ARRAY_SIZE(mtk_dai_i2s_driver);
1049a94aec03SShunli Wang 
1050a94aec03SShunli Wang 	dai->controls = mtk_dai_i2s_controls;
1051a94aec03SShunli Wang 	dai->num_controls = ARRAY_SIZE(mtk_dai_i2s_controls);
1052a94aec03SShunli Wang 	dai->dapm_widgets = mtk_dai_i2s_widgets;
1053a94aec03SShunli Wang 	dai->num_dapm_widgets = ARRAY_SIZE(mtk_dai_i2s_widgets);
1054a94aec03SShunli Wang 	dai->dapm_routes = mtk_dai_i2s_routes;
1055a94aec03SShunli Wang 	dai->num_dapm_routes = ARRAY_SIZE(mtk_dai_i2s_routes);
1056a94aec03SShunli Wang 
1057a94aec03SShunli Wang 	/* set all dai i2s private data */
1058a94aec03SShunli Wang 	ret = mt8183_dai_i2s_set_priv(afe);
1059a94aec03SShunli Wang 	if (ret)
1060a94aec03SShunli Wang 		return ret;
1061a94aec03SShunli Wang 
1062a94aec03SShunli Wang 	return 0;
1063a94aec03SShunli Wang }
1064