1a94aec03SShunli Wang // SPDX-License-Identifier: GPL-2.0
2a94aec03SShunli Wang //
3a94aec03SShunli Wang // MediaTek ALSA SoC Audio DAI TDM 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/regmap.h>
9a94aec03SShunli Wang #include <sound/pcm_params.h>
10a94aec03SShunli Wang #include "mt8183-afe-clk.h"
11a94aec03SShunli Wang #include "mt8183-afe-common.h"
12a94aec03SShunli Wang #include "mt8183-interconnection.h"
13a94aec03SShunli Wang #include "mt8183-reg.h"
14a94aec03SShunli Wang
15a94aec03SShunli Wang struct mtk_afe_tdm_priv {
16a94aec03SShunli Wang int bck_id;
17a94aec03SShunli Wang int bck_rate;
188e58c521SJiaxin Yu int tdm_out_mode;
198e58c521SJiaxin Yu int bck_invert;
208e58c521SJiaxin Yu int lck_invert;
21a94aec03SShunli Wang int mclk_id;
22a94aec03SShunli Wang int mclk_multiple; /* according to sample rate */
23a94aec03SShunli Wang int mclk_rate;
24a94aec03SShunli Wang int mclk_apll;
25a94aec03SShunli Wang };
26a94aec03SShunli Wang
27a94aec03SShunli Wang enum {
288e58c521SJiaxin Yu TDM_OUT_I2S = 0,
298e58c521SJiaxin Yu TDM_OUT_TDM = 1,
308e58c521SJiaxin Yu };
318e58c521SJiaxin Yu
328e58c521SJiaxin Yu enum {
338e58c521SJiaxin Yu TDM_BCK_NON_INV = 0,
348e58c521SJiaxin Yu TDM_BCK_INV = 1,
358e58c521SJiaxin Yu };
368e58c521SJiaxin Yu
378e58c521SJiaxin Yu enum {
388e58c521SJiaxin Yu TDM_LCK_NON_INV = 0,
398e58c521SJiaxin Yu TDM_LCK_INV = 1,
408e58c521SJiaxin Yu };
418e58c521SJiaxin Yu
428e58c521SJiaxin Yu enum {
43a94aec03SShunli Wang TDM_WLEN_16_BIT = 1,
44a94aec03SShunli Wang TDM_WLEN_32_BIT = 2,
45a94aec03SShunli Wang };
46a94aec03SShunli Wang
47a94aec03SShunli Wang enum {
48a94aec03SShunli Wang TDM_CHANNEL_BCK_16 = 0,
49a94aec03SShunli Wang TDM_CHANNEL_BCK_24 = 1,
50a94aec03SShunli Wang TDM_CHANNEL_BCK_32 = 2,
51a94aec03SShunli Wang };
52a94aec03SShunli Wang
53a94aec03SShunli Wang enum {
54a94aec03SShunli Wang TDM_CHANNEL_NUM_2 = 0,
55a94aec03SShunli Wang TDM_CHANNEL_NUM_4 = 1,
56a94aec03SShunli Wang TDM_CHANNEL_NUM_8 = 2,
57a94aec03SShunli Wang };
58a94aec03SShunli Wang
59a94aec03SShunli Wang enum {
60a94aec03SShunli Wang TDM_CH_START_O30_O31 = 0,
61a94aec03SShunli Wang TDM_CH_START_O32_O33,
62a94aec03SShunli Wang TDM_CH_START_O34_O35,
63a94aec03SShunli Wang TDM_CH_START_O36_O37,
64a94aec03SShunli Wang TDM_CH_ZERO,
65a94aec03SShunli Wang };
66a94aec03SShunli Wang
67a94aec03SShunli Wang enum {
68a94aec03SShunli Wang HDMI_BIT_WIDTH_16_BIT = 0,
69a94aec03SShunli Wang HDMI_BIT_WIDTH_32_BIT = 1,
70a94aec03SShunli Wang };
71a94aec03SShunli Wang
get_hdmi_wlen(snd_pcm_format_t format)72a94aec03SShunli Wang static unsigned int get_hdmi_wlen(snd_pcm_format_t format)
73a94aec03SShunli Wang {
74a94aec03SShunli Wang return snd_pcm_format_physical_width(format) <= 16 ?
75a94aec03SShunli Wang HDMI_BIT_WIDTH_16_BIT : HDMI_BIT_WIDTH_32_BIT;
76a94aec03SShunli Wang }
77a94aec03SShunli Wang
get_tdm_wlen(snd_pcm_format_t format)78a94aec03SShunli Wang static unsigned int get_tdm_wlen(snd_pcm_format_t format)
79a94aec03SShunli Wang {
80a94aec03SShunli Wang return snd_pcm_format_physical_width(format) <= 16 ?
81a94aec03SShunli Wang TDM_WLEN_16_BIT : TDM_WLEN_32_BIT;
82a94aec03SShunli Wang }
83a94aec03SShunli Wang
get_tdm_channel_bck(snd_pcm_format_t format)84a94aec03SShunli Wang static unsigned int get_tdm_channel_bck(snd_pcm_format_t format)
85a94aec03SShunli Wang {
86a94aec03SShunli Wang return snd_pcm_format_physical_width(format) <= 16 ?
87a94aec03SShunli Wang TDM_CHANNEL_BCK_16 : TDM_CHANNEL_BCK_32;
88a94aec03SShunli Wang }
89a94aec03SShunli Wang
get_tdm_lrck_width(snd_pcm_format_t format)90a94aec03SShunli Wang static unsigned int get_tdm_lrck_width(snd_pcm_format_t format)
91a94aec03SShunli Wang {
92a94aec03SShunli Wang return snd_pcm_format_physical_width(format) - 1;
93a94aec03SShunli Wang }
94a94aec03SShunli Wang
get_tdm_ch(unsigned int ch)95a94aec03SShunli Wang static unsigned int get_tdm_ch(unsigned int ch)
96a94aec03SShunli Wang {
97a94aec03SShunli Wang switch (ch) {
98a94aec03SShunli Wang case 1:
99a94aec03SShunli Wang case 2:
100a94aec03SShunli Wang return TDM_CHANNEL_NUM_2;
101a94aec03SShunli Wang case 3:
102a94aec03SShunli Wang case 4:
103a94aec03SShunli Wang return TDM_CHANNEL_NUM_4;
104a94aec03SShunli Wang case 5:
105a94aec03SShunli Wang case 6:
106a94aec03SShunli Wang case 7:
107a94aec03SShunli Wang case 8:
108a94aec03SShunli Wang default:
109a94aec03SShunli Wang return TDM_CHANNEL_NUM_8;
110a94aec03SShunli Wang }
111a94aec03SShunli Wang }
112a94aec03SShunli Wang
get_tdm_ch_fixup(unsigned int channels)1138e58c521SJiaxin Yu static unsigned int get_tdm_ch_fixup(unsigned int channels)
1148e58c521SJiaxin Yu {
1158e58c521SJiaxin Yu if (channels > 4)
1168e58c521SJiaxin Yu return 8;
1178e58c521SJiaxin Yu else if (channels > 2)
1188e58c521SJiaxin Yu return 4;
1198e58c521SJiaxin Yu else
1208e58c521SJiaxin Yu return 2;
1218e58c521SJiaxin Yu }
1228e58c521SJiaxin Yu
get_tdm_ch_per_sdata(unsigned int mode,unsigned int channels)1238e58c521SJiaxin Yu static unsigned int get_tdm_ch_per_sdata(unsigned int mode,
1248e58c521SJiaxin Yu unsigned int channels)
1258e58c521SJiaxin Yu {
1268e58c521SJiaxin Yu if (mode == TDM_OUT_TDM)
1278e58c521SJiaxin Yu return get_tdm_ch_fixup(channels);
1288e58c521SJiaxin Yu else
1298e58c521SJiaxin Yu return 2;
1308e58c521SJiaxin Yu }
1318e58c521SJiaxin Yu
132a94aec03SShunli Wang /* interconnection */
133a94aec03SShunli Wang enum {
134a94aec03SShunli Wang HDMI_CONN_CH0 = 0,
135a94aec03SShunli Wang HDMI_CONN_CH1,
136a94aec03SShunli Wang HDMI_CONN_CH2,
137a94aec03SShunli Wang HDMI_CONN_CH3,
138a94aec03SShunli Wang HDMI_CONN_CH4,
139a94aec03SShunli Wang HDMI_CONN_CH5,
140a94aec03SShunli Wang HDMI_CONN_CH6,
141a94aec03SShunli Wang HDMI_CONN_CH7,
142a94aec03SShunli Wang };
143a94aec03SShunli Wang
144a94aec03SShunli Wang static const char *const hdmi_conn_mux_map[] = {
145a94aec03SShunli Wang "CH0", "CH1", "CH2", "CH3",
146a94aec03SShunli Wang "CH4", "CH5", "CH6", "CH7",
147a94aec03SShunli Wang };
148a94aec03SShunli Wang
149a94aec03SShunli Wang static int hdmi_conn_mux_map_value[] = {
150a94aec03SShunli Wang HDMI_CONN_CH0,
151a94aec03SShunli Wang HDMI_CONN_CH1,
152a94aec03SShunli Wang HDMI_CONN_CH2,
153a94aec03SShunli Wang HDMI_CONN_CH3,
154a94aec03SShunli Wang HDMI_CONN_CH4,
155a94aec03SShunli Wang HDMI_CONN_CH5,
156a94aec03SShunli Wang HDMI_CONN_CH6,
157a94aec03SShunli Wang HDMI_CONN_CH7,
158a94aec03SShunli Wang };
159a94aec03SShunli Wang
160a94aec03SShunli Wang static SOC_VALUE_ENUM_SINGLE_DECL(hdmi_ch0_mux_map_enum,
161a94aec03SShunli Wang AFE_HDMI_CONN0,
162a94aec03SShunli Wang HDMI_O_0_SFT,
163a94aec03SShunli Wang HDMI_O_0_MASK,
164a94aec03SShunli Wang hdmi_conn_mux_map,
165a94aec03SShunli Wang hdmi_conn_mux_map_value);
166a94aec03SShunli Wang
167a94aec03SShunli Wang static const struct snd_kcontrol_new hdmi_ch0_mux_control =
168a94aec03SShunli Wang SOC_DAPM_ENUM("HDMI_CH0_MUX", hdmi_ch0_mux_map_enum);
169a94aec03SShunli Wang
170a94aec03SShunli Wang static SOC_VALUE_ENUM_SINGLE_DECL(hdmi_ch1_mux_map_enum,
171a94aec03SShunli Wang AFE_HDMI_CONN0,
172a94aec03SShunli Wang HDMI_O_1_SFT,
173a94aec03SShunli Wang HDMI_O_1_MASK,
174a94aec03SShunli Wang hdmi_conn_mux_map,
175a94aec03SShunli Wang hdmi_conn_mux_map_value);
176a94aec03SShunli Wang
177a94aec03SShunli Wang static const struct snd_kcontrol_new hdmi_ch1_mux_control =
178a94aec03SShunli Wang SOC_DAPM_ENUM("HDMI_CH1_MUX", hdmi_ch1_mux_map_enum);
179a94aec03SShunli Wang
180a94aec03SShunli Wang static SOC_VALUE_ENUM_SINGLE_DECL(hdmi_ch2_mux_map_enum,
181a94aec03SShunli Wang AFE_HDMI_CONN0,
182a94aec03SShunli Wang HDMI_O_2_SFT,
183a94aec03SShunli Wang HDMI_O_2_MASK,
184a94aec03SShunli Wang hdmi_conn_mux_map,
185a94aec03SShunli Wang hdmi_conn_mux_map_value);
186a94aec03SShunli Wang
187a94aec03SShunli Wang static const struct snd_kcontrol_new hdmi_ch2_mux_control =
188a94aec03SShunli Wang SOC_DAPM_ENUM("HDMI_CH2_MUX", hdmi_ch2_mux_map_enum);
189a94aec03SShunli Wang
190a94aec03SShunli Wang static SOC_VALUE_ENUM_SINGLE_DECL(hdmi_ch3_mux_map_enum,
191a94aec03SShunli Wang AFE_HDMI_CONN0,
192a94aec03SShunli Wang HDMI_O_3_SFT,
193a94aec03SShunli Wang HDMI_O_3_MASK,
194a94aec03SShunli Wang hdmi_conn_mux_map,
195a94aec03SShunli Wang hdmi_conn_mux_map_value);
196a94aec03SShunli Wang
197a94aec03SShunli Wang static const struct snd_kcontrol_new hdmi_ch3_mux_control =
198a94aec03SShunli Wang SOC_DAPM_ENUM("HDMI_CH3_MUX", hdmi_ch3_mux_map_enum);
199a94aec03SShunli Wang
200a94aec03SShunli Wang static SOC_VALUE_ENUM_SINGLE_DECL(hdmi_ch4_mux_map_enum,
201a94aec03SShunli Wang AFE_HDMI_CONN0,
202a94aec03SShunli Wang HDMI_O_4_SFT,
203a94aec03SShunli Wang HDMI_O_4_MASK,
204a94aec03SShunli Wang hdmi_conn_mux_map,
205a94aec03SShunli Wang hdmi_conn_mux_map_value);
206a94aec03SShunli Wang
207a94aec03SShunli Wang static const struct snd_kcontrol_new hdmi_ch4_mux_control =
208a94aec03SShunli Wang SOC_DAPM_ENUM("HDMI_CH4_MUX", hdmi_ch4_mux_map_enum);
209a94aec03SShunli Wang
210a94aec03SShunli Wang static SOC_VALUE_ENUM_SINGLE_DECL(hdmi_ch5_mux_map_enum,
211a94aec03SShunli Wang AFE_HDMI_CONN0,
212a94aec03SShunli Wang HDMI_O_5_SFT,
213a94aec03SShunli Wang HDMI_O_5_MASK,
214a94aec03SShunli Wang hdmi_conn_mux_map,
215a94aec03SShunli Wang hdmi_conn_mux_map_value);
216a94aec03SShunli Wang
217a94aec03SShunli Wang static const struct snd_kcontrol_new hdmi_ch5_mux_control =
218a94aec03SShunli Wang SOC_DAPM_ENUM("HDMI_CH5_MUX", hdmi_ch5_mux_map_enum);
219a94aec03SShunli Wang
220a94aec03SShunli Wang static SOC_VALUE_ENUM_SINGLE_DECL(hdmi_ch6_mux_map_enum,
221a94aec03SShunli Wang AFE_HDMI_CONN0,
222a94aec03SShunli Wang HDMI_O_6_SFT,
223a94aec03SShunli Wang HDMI_O_6_MASK,
224a94aec03SShunli Wang hdmi_conn_mux_map,
225a94aec03SShunli Wang hdmi_conn_mux_map_value);
226a94aec03SShunli Wang
227a94aec03SShunli Wang static const struct snd_kcontrol_new hdmi_ch6_mux_control =
228a94aec03SShunli Wang SOC_DAPM_ENUM("HDMI_CH6_MUX", hdmi_ch6_mux_map_enum);
229a94aec03SShunli Wang
230a94aec03SShunli Wang static SOC_VALUE_ENUM_SINGLE_DECL(hdmi_ch7_mux_map_enum,
231a94aec03SShunli Wang AFE_HDMI_CONN0,
232a94aec03SShunli Wang HDMI_O_7_SFT,
233a94aec03SShunli Wang HDMI_O_7_MASK,
234a94aec03SShunli Wang hdmi_conn_mux_map,
235a94aec03SShunli Wang hdmi_conn_mux_map_value);
236a94aec03SShunli Wang
237a94aec03SShunli Wang static const struct snd_kcontrol_new hdmi_ch7_mux_control =
238a94aec03SShunli Wang SOC_DAPM_ENUM("HDMI_CH7_MUX", hdmi_ch7_mux_map_enum);
239a94aec03SShunli Wang
240a94aec03SShunli Wang enum {
241a94aec03SShunli Wang SUPPLY_SEQ_APLL,
242a94aec03SShunli Wang SUPPLY_SEQ_TDM_MCK_EN,
243a94aec03SShunli Wang SUPPLY_SEQ_TDM_BCK_EN,
244a94aec03SShunli Wang };
245a94aec03SShunli Wang
mtk_tdm_bck_en_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)246a94aec03SShunli Wang static int mtk_tdm_bck_en_event(struct snd_soc_dapm_widget *w,
247a94aec03SShunli Wang struct snd_kcontrol *kcontrol,
248a94aec03SShunli Wang int event)
249a94aec03SShunli Wang {
250a94aec03SShunli Wang struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
251a94aec03SShunli Wang struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);
252a94aec03SShunli Wang struct mt8183_afe_private *afe_priv = afe->platform_priv;
253a94aec03SShunli Wang struct mtk_afe_tdm_priv *tdm_priv = afe_priv->dai_priv[MT8183_DAI_TDM];
254a94aec03SShunli Wang
255a94aec03SShunli Wang dev_info(cmpnt->dev, "%s(), name %s, event 0x%x\n",
256a94aec03SShunli Wang __func__, w->name, event);
257a94aec03SShunli Wang
258a94aec03SShunli Wang switch (event) {
259a94aec03SShunli Wang case SND_SOC_DAPM_PRE_PMU:
260a94aec03SShunli Wang mt8183_mck_enable(afe, tdm_priv->bck_id, tdm_priv->bck_rate);
261a94aec03SShunli Wang break;
262a94aec03SShunli Wang case SND_SOC_DAPM_POST_PMD:
263a94aec03SShunli Wang mt8183_mck_disable(afe, tdm_priv->bck_id);
264a94aec03SShunli Wang break;
265a94aec03SShunli Wang default:
266a94aec03SShunli Wang break;
267a94aec03SShunli Wang }
268a94aec03SShunli Wang
269a94aec03SShunli Wang return 0;
270a94aec03SShunli Wang }
271a94aec03SShunli Wang
mtk_tdm_mck_en_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)272a94aec03SShunli Wang static int mtk_tdm_mck_en_event(struct snd_soc_dapm_widget *w,
273a94aec03SShunli Wang struct snd_kcontrol *kcontrol,
274a94aec03SShunli Wang int event)
275a94aec03SShunli Wang {
276a94aec03SShunli Wang struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
277a94aec03SShunli Wang struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);
278a94aec03SShunli Wang struct mt8183_afe_private *afe_priv = afe->platform_priv;
279a94aec03SShunli Wang struct mtk_afe_tdm_priv *tdm_priv = afe_priv->dai_priv[MT8183_DAI_TDM];
280a94aec03SShunli Wang
281a94aec03SShunli Wang dev_info(cmpnt->dev, "%s(), name %s, event 0x%x\n",
282a94aec03SShunli Wang __func__, w->name, event);
283a94aec03SShunli Wang
284a94aec03SShunli Wang switch (event) {
285a94aec03SShunli Wang case SND_SOC_DAPM_PRE_PMU:
286a94aec03SShunli Wang mt8183_mck_enable(afe, tdm_priv->mclk_id, tdm_priv->mclk_rate);
287a94aec03SShunli Wang break;
288a94aec03SShunli Wang case SND_SOC_DAPM_POST_PMD:
289a94aec03SShunli Wang tdm_priv->mclk_rate = 0;
290a94aec03SShunli Wang mt8183_mck_disable(afe, tdm_priv->mclk_id);
291a94aec03SShunli Wang break;
292a94aec03SShunli Wang default:
293a94aec03SShunli Wang break;
294a94aec03SShunli Wang }
295a94aec03SShunli Wang
296a94aec03SShunli Wang return 0;
297a94aec03SShunli Wang }
298a94aec03SShunli Wang
299a94aec03SShunli Wang static const struct snd_soc_dapm_widget mtk_dai_tdm_widgets[] = {
300a94aec03SShunli Wang SND_SOC_DAPM_MUX("HDMI_CH0_MUX", SND_SOC_NOPM, 0, 0,
301a94aec03SShunli Wang &hdmi_ch0_mux_control),
302a94aec03SShunli Wang SND_SOC_DAPM_MUX("HDMI_CH1_MUX", SND_SOC_NOPM, 0, 0,
303a94aec03SShunli Wang &hdmi_ch1_mux_control),
304a94aec03SShunli Wang SND_SOC_DAPM_MUX("HDMI_CH2_MUX", SND_SOC_NOPM, 0, 0,
305a94aec03SShunli Wang &hdmi_ch2_mux_control),
306a94aec03SShunli Wang SND_SOC_DAPM_MUX("HDMI_CH3_MUX", SND_SOC_NOPM, 0, 0,
307a94aec03SShunli Wang &hdmi_ch3_mux_control),
308a94aec03SShunli Wang SND_SOC_DAPM_MUX("HDMI_CH4_MUX", SND_SOC_NOPM, 0, 0,
309a94aec03SShunli Wang &hdmi_ch4_mux_control),
310a94aec03SShunli Wang SND_SOC_DAPM_MUX("HDMI_CH5_MUX", SND_SOC_NOPM, 0, 0,
311a94aec03SShunli Wang &hdmi_ch5_mux_control),
312a94aec03SShunli Wang SND_SOC_DAPM_MUX("HDMI_CH6_MUX", SND_SOC_NOPM, 0, 0,
313a94aec03SShunli Wang &hdmi_ch6_mux_control),
314a94aec03SShunli Wang SND_SOC_DAPM_MUX("HDMI_CH7_MUX", SND_SOC_NOPM, 0, 0,
315a94aec03SShunli Wang &hdmi_ch7_mux_control),
316a94aec03SShunli Wang
317a94aec03SShunli Wang SND_SOC_DAPM_CLOCK_SUPPLY("aud_tdm_clk"),
318a94aec03SShunli Wang
319a94aec03SShunli Wang SND_SOC_DAPM_SUPPLY_S("TDM_BCK", SUPPLY_SEQ_TDM_BCK_EN,
320a94aec03SShunli Wang SND_SOC_NOPM, 0, 0,
321a94aec03SShunli Wang mtk_tdm_bck_en_event,
322a94aec03SShunli Wang SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
323a94aec03SShunli Wang
324a94aec03SShunli Wang SND_SOC_DAPM_SUPPLY_S("TDM_MCK", SUPPLY_SEQ_TDM_MCK_EN,
325a94aec03SShunli Wang SND_SOC_NOPM, 0, 0,
326a94aec03SShunli Wang mtk_tdm_mck_en_event,
327a94aec03SShunli Wang SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
328a94aec03SShunli Wang };
329a94aec03SShunli Wang
mtk_afe_tdm_apll_connect(struct snd_soc_dapm_widget * source,struct snd_soc_dapm_widget * sink)330a94aec03SShunli Wang static int mtk_afe_tdm_apll_connect(struct snd_soc_dapm_widget *source,
331a94aec03SShunli Wang struct snd_soc_dapm_widget *sink)
332a94aec03SShunli Wang {
333a94aec03SShunli Wang struct snd_soc_dapm_widget *w = sink;
334a94aec03SShunli Wang struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
335a94aec03SShunli Wang struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);
336a94aec03SShunli Wang struct mt8183_afe_private *afe_priv = afe->platform_priv;
337a94aec03SShunli Wang struct mtk_afe_tdm_priv *tdm_priv = afe_priv->dai_priv[MT8183_DAI_TDM];
338a94aec03SShunli Wang int cur_apll;
339a94aec03SShunli Wang
340a94aec03SShunli Wang /* which apll */
341a94aec03SShunli Wang cur_apll = mt8183_get_apll_by_name(afe, source->name);
342a94aec03SShunli Wang
343a94aec03SShunli Wang return (tdm_priv->mclk_apll == cur_apll) ? 1 : 0;
344a94aec03SShunli Wang }
345a94aec03SShunli Wang
346a94aec03SShunli Wang static const struct snd_soc_dapm_route mtk_dai_tdm_routes[] = {
347a94aec03SShunli Wang {"HDMI_CH0_MUX", "CH0", "HDMI"},
348a94aec03SShunli Wang {"HDMI_CH0_MUX", "CH1", "HDMI"},
349a94aec03SShunli Wang {"HDMI_CH0_MUX", "CH2", "HDMI"},
350a94aec03SShunli Wang {"HDMI_CH0_MUX", "CH3", "HDMI"},
351a94aec03SShunli Wang {"HDMI_CH0_MUX", "CH4", "HDMI"},
352a94aec03SShunli Wang {"HDMI_CH0_MUX", "CH5", "HDMI"},
353a94aec03SShunli Wang {"HDMI_CH0_MUX", "CH6", "HDMI"},
354a94aec03SShunli Wang {"HDMI_CH0_MUX", "CH7", "HDMI"},
355a94aec03SShunli Wang
356a94aec03SShunli Wang {"HDMI_CH1_MUX", "CH0", "HDMI"},
357a94aec03SShunli Wang {"HDMI_CH1_MUX", "CH1", "HDMI"},
358a94aec03SShunli Wang {"HDMI_CH1_MUX", "CH2", "HDMI"},
359a94aec03SShunli Wang {"HDMI_CH1_MUX", "CH3", "HDMI"},
360a94aec03SShunli Wang {"HDMI_CH1_MUX", "CH4", "HDMI"},
361a94aec03SShunli Wang {"HDMI_CH1_MUX", "CH5", "HDMI"},
362a94aec03SShunli Wang {"HDMI_CH1_MUX", "CH6", "HDMI"},
363a94aec03SShunli Wang {"HDMI_CH1_MUX", "CH7", "HDMI"},
364a94aec03SShunli Wang
365a94aec03SShunli Wang {"HDMI_CH2_MUX", "CH0", "HDMI"},
366a94aec03SShunli Wang {"HDMI_CH2_MUX", "CH1", "HDMI"},
367a94aec03SShunli Wang {"HDMI_CH2_MUX", "CH2", "HDMI"},
368a94aec03SShunli Wang {"HDMI_CH2_MUX", "CH3", "HDMI"},
369a94aec03SShunli Wang {"HDMI_CH2_MUX", "CH4", "HDMI"},
370a94aec03SShunli Wang {"HDMI_CH2_MUX", "CH5", "HDMI"},
371a94aec03SShunli Wang {"HDMI_CH2_MUX", "CH6", "HDMI"},
372a94aec03SShunli Wang {"HDMI_CH2_MUX", "CH7", "HDMI"},
373a94aec03SShunli Wang
374a94aec03SShunli Wang {"HDMI_CH3_MUX", "CH0", "HDMI"},
375a94aec03SShunli Wang {"HDMI_CH3_MUX", "CH1", "HDMI"},
376a94aec03SShunli Wang {"HDMI_CH3_MUX", "CH2", "HDMI"},
377a94aec03SShunli Wang {"HDMI_CH3_MUX", "CH3", "HDMI"},
378a94aec03SShunli Wang {"HDMI_CH3_MUX", "CH4", "HDMI"},
379a94aec03SShunli Wang {"HDMI_CH3_MUX", "CH5", "HDMI"},
380a94aec03SShunli Wang {"HDMI_CH3_MUX", "CH6", "HDMI"},
381a94aec03SShunli Wang {"HDMI_CH3_MUX", "CH7", "HDMI"},
382a94aec03SShunli Wang
383a94aec03SShunli Wang {"HDMI_CH4_MUX", "CH0", "HDMI"},
384a94aec03SShunli Wang {"HDMI_CH4_MUX", "CH1", "HDMI"},
385a94aec03SShunli Wang {"HDMI_CH4_MUX", "CH2", "HDMI"},
386a94aec03SShunli Wang {"HDMI_CH4_MUX", "CH3", "HDMI"},
387a94aec03SShunli Wang {"HDMI_CH4_MUX", "CH4", "HDMI"},
388a94aec03SShunli Wang {"HDMI_CH4_MUX", "CH5", "HDMI"},
389a94aec03SShunli Wang {"HDMI_CH4_MUX", "CH6", "HDMI"},
390a94aec03SShunli Wang {"HDMI_CH4_MUX", "CH7", "HDMI"},
391a94aec03SShunli Wang
392a94aec03SShunli Wang {"HDMI_CH5_MUX", "CH0", "HDMI"},
393a94aec03SShunli Wang {"HDMI_CH5_MUX", "CH1", "HDMI"},
394a94aec03SShunli Wang {"HDMI_CH5_MUX", "CH2", "HDMI"},
395a94aec03SShunli Wang {"HDMI_CH5_MUX", "CH3", "HDMI"},
396a94aec03SShunli Wang {"HDMI_CH5_MUX", "CH4", "HDMI"},
397a94aec03SShunli Wang {"HDMI_CH5_MUX", "CH5", "HDMI"},
398a94aec03SShunli Wang {"HDMI_CH5_MUX", "CH6", "HDMI"},
399a94aec03SShunli Wang {"HDMI_CH5_MUX", "CH7", "HDMI"},
400a94aec03SShunli Wang
401a94aec03SShunli Wang {"HDMI_CH6_MUX", "CH0", "HDMI"},
402a94aec03SShunli Wang {"HDMI_CH6_MUX", "CH1", "HDMI"},
403a94aec03SShunli Wang {"HDMI_CH6_MUX", "CH2", "HDMI"},
404a94aec03SShunli Wang {"HDMI_CH6_MUX", "CH3", "HDMI"},
405a94aec03SShunli Wang {"HDMI_CH6_MUX", "CH4", "HDMI"},
406a94aec03SShunli Wang {"HDMI_CH6_MUX", "CH5", "HDMI"},
407a94aec03SShunli Wang {"HDMI_CH6_MUX", "CH6", "HDMI"},
408a94aec03SShunli Wang {"HDMI_CH6_MUX", "CH7", "HDMI"},
409a94aec03SShunli Wang
410a94aec03SShunli Wang {"HDMI_CH7_MUX", "CH0", "HDMI"},
411a94aec03SShunli Wang {"HDMI_CH7_MUX", "CH1", "HDMI"},
412a94aec03SShunli Wang {"HDMI_CH7_MUX", "CH2", "HDMI"},
413a94aec03SShunli Wang {"HDMI_CH7_MUX", "CH3", "HDMI"},
414a94aec03SShunli Wang {"HDMI_CH7_MUX", "CH4", "HDMI"},
415a94aec03SShunli Wang {"HDMI_CH7_MUX", "CH5", "HDMI"},
416a94aec03SShunli Wang {"HDMI_CH7_MUX", "CH6", "HDMI"},
417a94aec03SShunli Wang {"HDMI_CH7_MUX", "CH7", "HDMI"},
418a94aec03SShunli Wang
419a94aec03SShunli Wang {"TDM", NULL, "HDMI_CH0_MUX"},
420a94aec03SShunli Wang {"TDM", NULL, "HDMI_CH1_MUX"},
421a94aec03SShunli Wang {"TDM", NULL, "HDMI_CH2_MUX"},
422a94aec03SShunli Wang {"TDM", NULL, "HDMI_CH3_MUX"},
423a94aec03SShunli Wang {"TDM", NULL, "HDMI_CH4_MUX"},
424a94aec03SShunli Wang {"TDM", NULL, "HDMI_CH5_MUX"},
425a94aec03SShunli Wang {"TDM", NULL, "HDMI_CH6_MUX"},
426a94aec03SShunli Wang {"TDM", NULL, "HDMI_CH7_MUX"},
427a94aec03SShunli Wang
428a94aec03SShunli Wang {"TDM", NULL, "aud_tdm_clk"},
429a94aec03SShunli Wang {"TDM", NULL, "TDM_BCK"},
430a94aec03SShunli Wang {"TDM_BCK", NULL, "TDM_MCK"},
431a94aec03SShunli Wang {"TDM_MCK", NULL, APLL1_W_NAME, mtk_afe_tdm_apll_connect},
432a94aec03SShunli Wang {"TDM_MCK", NULL, APLL2_W_NAME, mtk_afe_tdm_apll_connect},
433a94aec03SShunli Wang };
434a94aec03SShunli Wang
435a94aec03SShunli Wang /* dai ops */
mtk_dai_tdm_cal_mclk(struct mtk_base_afe * afe,struct mtk_afe_tdm_priv * tdm_priv,int freq)436a94aec03SShunli Wang static int mtk_dai_tdm_cal_mclk(struct mtk_base_afe *afe,
437a94aec03SShunli Wang struct mtk_afe_tdm_priv *tdm_priv,
438a94aec03SShunli Wang int freq)
439a94aec03SShunli Wang {
440a94aec03SShunli Wang int apll;
441a94aec03SShunli Wang int apll_rate;
442a94aec03SShunli Wang
443a94aec03SShunli Wang apll = mt8183_get_apll_by_rate(afe, freq);
444a94aec03SShunli Wang apll_rate = mt8183_get_apll_rate(afe, apll);
445a94aec03SShunli Wang
446a94aec03SShunli Wang if (!freq || freq > apll_rate) {
447a94aec03SShunli Wang dev_warn(afe->dev,
448a94aec03SShunli Wang "%s(), freq(%d Hz) invalid\n", __func__, freq);
449a94aec03SShunli Wang return -EINVAL;
450a94aec03SShunli Wang }
451a94aec03SShunli Wang
452a94aec03SShunli Wang if (apll_rate % freq != 0) {
453a94aec03SShunli Wang dev_warn(afe->dev,
454a94aec03SShunli Wang "%s(), APLL cannot generate %d Hz", __func__, freq);
455a94aec03SShunli Wang return -EINVAL;
456a94aec03SShunli Wang }
457a94aec03SShunli Wang
458a94aec03SShunli Wang tdm_priv->mclk_rate = freq;
459a94aec03SShunli Wang tdm_priv->mclk_apll = apll;
460a94aec03SShunli Wang
461a94aec03SShunli Wang return 0;
462a94aec03SShunli Wang }
463a94aec03SShunli Wang
mtk_dai_tdm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)464a94aec03SShunli Wang static int mtk_dai_tdm_hw_params(struct snd_pcm_substream *substream,
465a94aec03SShunli Wang struct snd_pcm_hw_params *params,
466a94aec03SShunli Wang struct snd_soc_dai *dai)
467a94aec03SShunli Wang {
468a94aec03SShunli Wang struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
469a94aec03SShunli Wang struct mt8183_afe_private *afe_priv = afe->platform_priv;
470a94aec03SShunli Wang int tdm_id = dai->id;
471a94aec03SShunli Wang struct mtk_afe_tdm_priv *tdm_priv = afe_priv->dai_priv[tdm_id];
4728e58c521SJiaxin Yu unsigned int tdm_out_mode = tdm_priv->tdm_out_mode;
473a94aec03SShunli Wang unsigned int rate = params_rate(params);
474a94aec03SShunli Wang unsigned int channels = params_channels(params);
4758e58c521SJiaxin Yu unsigned int out_channels_per_sdata =
4768e58c521SJiaxin Yu get_tdm_ch_per_sdata(tdm_out_mode, channels);
477a94aec03SShunli Wang snd_pcm_format_t format = params_format(params);
478a94aec03SShunli Wang unsigned int tdm_con = 0;
479a94aec03SShunli Wang
480a94aec03SShunli Wang /* calculate mclk_rate, if not set explicitly */
481a94aec03SShunli Wang if (!tdm_priv->mclk_rate) {
482a94aec03SShunli Wang tdm_priv->mclk_rate = rate * tdm_priv->mclk_multiple;
483a94aec03SShunli Wang mtk_dai_tdm_cal_mclk(afe,
484a94aec03SShunli Wang tdm_priv,
485a94aec03SShunli Wang tdm_priv->mclk_rate);
486a94aec03SShunli Wang }
487a94aec03SShunli Wang
488a94aec03SShunli Wang /* calculate bck */
489a94aec03SShunli Wang tdm_priv->bck_rate = rate *
4908e58c521SJiaxin Yu out_channels_per_sdata *
491a94aec03SShunli Wang snd_pcm_format_physical_width(format);
492a94aec03SShunli Wang
493a94aec03SShunli Wang if (tdm_priv->bck_rate > tdm_priv->mclk_rate)
494a94aec03SShunli Wang dev_warn(afe->dev, "%s(), bck_rate > mclk_rate rate", __func__);
495a94aec03SShunli Wang
496a94aec03SShunli Wang if (tdm_priv->mclk_rate % tdm_priv->bck_rate != 0)
497a94aec03SShunli Wang dev_warn(afe->dev, "%s(), bck cannot generate", __func__);
498a94aec03SShunli Wang
499a94aec03SShunli Wang dev_info(afe->dev, "%s(), id %d, rate %d, channels %d, format %d, mclk_rate %d, bck_rate %d\n",
500a94aec03SShunli Wang __func__,
501a94aec03SShunli Wang tdm_id, rate, channels, format,
502a94aec03SShunli Wang tdm_priv->mclk_rate, tdm_priv->bck_rate);
5038e58c521SJiaxin Yu dev_info(afe->dev, "%s(), out_channels_per_sdata = %d\n",
5048e58c521SJiaxin Yu __func__, out_channels_per_sdata);
505a94aec03SShunli Wang
506a94aec03SShunli Wang /* set tdm */
5078e58c521SJiaxin Yu if (tdm_priv->bck_invert)
508*2c650fd5SJiaxin Yu regmap_update_bits(afe->regmap, AUDIO_TOP_CON3,
509*2c650fd5SJiaxin Yu BCK_INVERSE_MASK_SFT,
510*2c650fd5SJiaxin Yu 0x1 << BCK_INVERSE_SFT);
5118e58c521SJiaxin Yu
5128e58c521SJiaxin Yu if (tdm_priv->lck_invert)
513a94aec03SShunli Wang tdm_con |= 1 << LRCK_INVERSE_SFT;
5148e58c521SJiaxin Yu
5158e58c521SJiaxin Yu if (tdm_priv->tdm_out_mode == TDM_OUT_I2S) {
516a94aec03SShunli Wang tdm_con |= 1 << DELAY_DATA_SFT;
5178e58c521SJiaxin Yu tdm_con |= get_tdm_lrck_width(format) << LRCK_TDM_WIDTH_SFT;
5188e58c521SJiaxin Yu } else if (tdm_priv->tdm_out_mode == TDM_OUT_TDM) {
5198e58c521SJiaxin Yu tdm_con |= 0 << DELAY_DATA_SFT;
5208e58c521SJiaxin Yu tdm_con |= 0 << LRCK_TDM_WIDTH_SFT;
5218e58c521SJiaxin Yu }
5228e58c521SJiaxin Yu
523a94aec03SShunli Wang tdm_con |= 1 << LEFT_ALIGN_SFT;
524a94aec03SShunli Wang tdm_con |= get_tdm_wlen(format) << WLEN_SFT;
5258e58c521SJiaxin Yu tdm_con |= get_tdm_ch(out_channels_per_sdata) << CHANNEL_NUM_SFT;
526a94aec03SShunli Wang tdm_con |= get_tdm_channel_bck(format) << CHANNEL_BCK_CYCLES_SFT;
527a94aec03SShunli Wang regmap_write(afe->regmap, AFE_TDM_CON1, tdm_con);
528a94aec03SShunli Wang
5298e58c521SJiaxin Yu if (out_channels_per_sdata == 2) {
530a94aec03SShunli Wang switch (channels) {
531a94aec03SShunli Wang case 1:
532a94aec03SShunli Wang case 2:
533a94aec03SShunli Wang tdm_con = TDM_CH_START_O30_O31 << ST_CH_PAIR_SOUT0_SFT;
534a94aec03SShunli Wang tdm_con |= TDM_CH_ZERO << ST_CH_PAIR_SOUT1_SFT;
535a94aec03SShunli Wang tdm_con |= TDM_CH_ZERO << ST_CH_PAIR_SOUT2_SFT;
536a94aec03SShunli Wang tdm_con |= TDM_CH_ZERO << ST_CH_PAIR_SOUT3_SFT;
537a94aec03SShunli Wang break;
538a94aec03SShunli Wang case 3:
539a94aec03SShunli Wang case 4:
540a94aec03SShunli Wang tdm_con = TDM_CH_START_O30_O31 << ST_CH_PAIR_SOUT0_SFT;
541a94aec03SShunli Wang tdm_con |= TDM_CH_START_O32_O33 << ST_CH_PAIR_SOUT1_SFT;
542a94aec03SShunli Wang tdm_con |= TDM_CH_ZERO << ST_CH_PAIR_SOUT2_SFT;
543a94aec03SShunli Wang tdm_con |= TDM_CH_ZERO << ST_CH_PAIR_SOUT3_SFT;
544a94aec03SShunli Wang break;
545a94aec03SShunli Wang case 5:
546a94aec03SShunli Wang case 6:
547a94aec03SShunli Wang tdm_con = TDM_CH_START_O30_O31 << ST_CH_PAIR_SOUT0_SFT;
548a94aec03SShunli Wang tdm_con |= TDM_CH_START_O32_O33 << ST_CH_PAIR_SOUT1_SFT;
549a94aec03SShunli Wang tdm_con |= TDM_CH_START_O34_O35 << ST_CH_PAIR_SOUT2_SFT;
550a94aec03SShunli Wang tdm_con |= TDM_CH_ZERO << ST_CH_PAIR_SOUT3_SFT;
551a94aec03SShunli Wang break;
552a94aec03SShunli Wang case 7:
553a94aec03SShunli Wang case 8:
554a94aec03SShunli Wang tdm_con = TDM_CH_START_O30_O31 << ST_CH_PAIR_SOUT0_SFT;
555a94aec03SShunli Wang tdm_con |= TDM_CH_START_O32_O33 << ST_CH_PAIR_SOUT1_SFT;
556a94aec03SShunli Wang tdm_con |= TDM_CH_START_O34_O35 << ST_CH_PAIR_SOUT2_SFT;
557a94aec03SShunli Wang tdm_con |= TDM_CH_START_O36_O37 << ST_CH_PAIR_SOUT3_SFT;
558a94aec03SShunli Wang break;
559a94aec03SShunli Wang default:
560a94aec03SShunli Wang tdm_con = 0;
561a94aec03SShunli Wang }
5628e58c521SJiaxin Yu } else {
5638e58c521SJiaxin Yu tdm_con = TDM_CH_START_O30_O31 << ST_CH_PAIR_SOUT0_SFT;
5648e58c521SJiaxin Yu tdm_con |= TDM_CH_ZERO << ST_CH_PAIR_SOUT1_SFT;
5658e58c521SJiaxin Yu tdm_con |= TDM_CH_ZERO << ST_CH_PAIR_SOUT2_SFT;
5668e58c521SJiaxin Yu tdm_con |= TDM_CH_ZERO << ST_CH_PAIR_SOUT3_SFT;
5678e58c521SJiaxin Yu }
5688e58c521SJiaxin Yu
569a94aec03SShunli Wang regmap_write(afe->regmap, AFE_TDM_CON2, tdm_con);
570a94aec03SShunli Wang
571a94aec03SShunli Wang regmap_update_bits(afe->regmap, AFE_HDMI_OUT_CON0,
572a94aec03SShunli Wang AFE_HDMI_OUT_CH_NUM_MASK_SFT,
573a94aec03SShunli Wang channels << AFE_HDMI_OUT_CH_NUM_SFT);
574a94aec03SShunli Wang
575a94aec03SShunli Wang regmap_update_bits(afe->regmap, AFE_HDMI_OUT_CON0,
576a94aec03SShunli Wang AFE_HDMI_OUT_BIT_WIDTH_MASK_SFT,
577a94aec03SShunli Wang get_hdmi_wlen(format) << AFE_HDMI_OUT_BIT_WIDTH_SFT);
578a94aec03SShunli Wang return 0;
579a94aec03SShunli Wang }
580a94aec03SShunli Wang
mtk_dai_tdm_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)581a94aec03SShunli Wang static int mtk_dai_tdm_trigger(struct snd_pcm_substream *substream,
582a94aec03SShunli Wang int cmd,
583a94aec03SShunli Wang struct snd_soc_dai *dai)
584a94aec03SShunli Wang {
585a94aec03SShunli Wang struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
586a94aec03SShunli Wang
587a94aec03SShunli Wang switch (cmd) {
588a94aec03SShunli Wang case SNDRV_PCM_TRIGGER_START:
589a94aec03SShunli Wang case SNDRV_PCM_TRIGGER_RESUME:
590a94aec03SShunli Wang /* enable Out control */
591a94aec03SShunli Wang regmap_update_bits(afe->regmap, AFE_HDMI_OUT_CON0,
592a94aec03SShunli Wang AFE_HDMI_OUT_ON_MASK_SFT,
593a94aec03SShunli Wang 0x1 << AFE_HDMI_OUT_ON_SFT);
594a94aec03SShunli Wang /* enable tdm */
595a94aec03SShunli Wang regmap_update_bits(afe->regmap, AFE_TDM_CON1,
596a94aec03SShunli Wang TDM_EN_MASK_SFT, 0x1 << TDM_EN_SFT);
597a94aec03SShunli Wang break;
598a94aec03SShunli Wang case SNDRV_PCM_TRIGGER_STOP:
599a94aec03SShunli Wang case SNDRV_PCM_TRIGGER_SUSPEND:
600a94aec03SShunli Wang /* disable tdm */
601a94aec03SShunli Wang regmap_update_bits(afe->regmap, AFE_TDM_CON1,
602a94aec03SShunli Wang TDM_EN_MASK_SFT, 0);
603a94aec03SShunli Wang /* disable Out control */
604a94aec03SShunli Wang regmap_update_bits(afe->regmap, AFE_HDMI_OUT_CON0,
605a94aec03SShunli Wang AFE_HDMI_OUT_ON_MASK_SFT,
606a94aec03SShunli Wang 0);
607a94aec03SShunli Wang break;
608a94aec03SShunli Wang default:
609a94aec03SShunli Wang return -EINVAL;
610a94aec03SShunli Wang }
611a94aec03SShunli Wang
612a94aec03SShunli Wang return 0;
613a94aec03SShunli Wang }
614a94aec03SShunli Wang
mtk_dai_tdm_set_sysclk(struct snd_soc_dai * dai,int clk_id,unsigned int freq,int dir)615a94aec03SShunli Wang static int mtk_dai_tdm_set_sysclk(struct snd_soc_dai *dai,
616a94aec03SShunli Wang int clk_id, unsigned int freq, int dir)
617a94aec03SShunli Wang {
618a94aec03SShunli Wang struct mtk_base_afe *afe = dev_get_drvdata(dai->dev);
619a94aec03SShunli Wang struct mt8183_afe_private *afe_priv = afe->platform_priv;
620a94aec03SShunli Wang struct mtk_afe_tdm_priv *tdm_priv = afe_priv->dai_priv[dai->id];
621a94aec03SShunli Wang
622a94aec03SShunli Wang if (!tdm_priv) {
623a94aec03SShunli Wang dev_warn(afe->dev, "%s(), tdm_priv == NULL", __func__);
624a94aec03SShunli Wang return -EINVAL;
625a94aec03SShunli Wang }
626a94aec03SShunli Wang
627a94aec03SShunli Wang if (dir != SND_SOC_CLOCK_OUT) {
628a94aec03SShunli Wang dev_warn(afe->dev, "%s(), dir != SND_SOC_CLOCK_OUT", __func__);
629a94aec03SShunli Wang return -EINVAL;
630a94aec03SShunli Wang }
631a94aec03SShunli Wang
632a94aec03SShunli Wang dev_info(afe->dev, "%s(), freq %d\n", __func__, freq);
633a94aec03SShunli Wang
634a94aec03SShunli Wang return mtk_dai_tdm_cal_mclk(afe, tdm_priv, freq);
635a94aec03SShunli Wang }
636a94aec03SShunli Wang
mtk_dai_tdm_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)6378e58c521SJiaxin Yu static int mtk_dai_tdm_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
6388e58c521SJiaxin Yu {
6398e58c521SJiaxin Yu struct mtk_base_afe *afe = dev_get_drvdata(dai->dev);
6408e58c521SJiaxin Yu struct mt8183_afe_private *afe_priv = afe->platform_priv;
6418e58c521SJiaxin Yu struct mtk_afe_tdm_priv *tdm_priv = afe_priv->dai_priv[dai->id];
6428e58c521SJiaxin Yu
6438e58c521SJiaxin Yu if (!tdm_priv) {
6448e58c521SJiaxin Yu dev_warn(afe->dev, "%s(), tdm_priv == NULL", __func__);
6458e58c521SJiaxin Yu return -EINVAL;
6468e58c521SJiaxin Yu }
6478e58c521SJiaxin Yu
6488e58c521SJiaxin Yu /* DAI mode*/
6498e58c521SJiaxin Yu switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
6508e58c521SJiaxin Yu case SND_SOC_DAIFMT_I2S:
6518e58c521SJiaxin Yu tdm_priv->tdm_out_mode = TDM_OUT_I2S;
6528e58c521SJiaxin Yu break;
6538e58c521SJiaxin Yu case SND_SOC_DAIFMT_DSP_A:
6548e58c521SJiaxin Yu tdm_priv->tdm_out_mode = TDM_OUT_TDM;
6558e58c521SJiaxin Yu break;
6568e58c521SJiaxin Yu default:
6578e58c521SJiaxin Yu tdm_priv->tdm_out_mode = TDM_OUT_I2S;
6588e58c521SJiaxin Yu }
6598e58c521SJiaxin Yu
6608e58c521SJiaxin Yu /* DAI clock inversion*/
6618e58c521SJiaxin Yu switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
6628e58c521SJiaxin Yu case SND_SOC_DAIFMT_NB_NF:
6638e58c521SJiaxin Yu tdm_priv->bck_invert = TDM_BCK_NON_INV;
6648e58c521SJiaxin Yu tdm_priv->lck_invert = TDM_LCK_NON_INV;
6658e58c521SJiaxin Yu break;
6668e58c521SJiaxin Yu case SND_SOC_DAIFMT_NB_IF:
6678e58c521SJiaxin Yu tdm_priv->bck_invert = TDM_BCK_NON_INV;
6688e58c521SJiaxin Yu tdm_priv->lck_invert = TDM_LCK_INV;
6698e58c521SJiaxin Yu break;
6708e58c521SJiaxin Yu case SND_SOC_DAIFMT_IB_NF:
6718e58c521SJiaxin Yu tdm_priv->bck_invert = TDM_BCK_INV;
6728e58c521SJiaxin Yu tdm_priv->lck_invert = TDM_LCK_NON_INV;
6738e58c521SJiaxin Yu break;
6748e58c521SJiaxin Yu case SND_SOC_DAIFMT_IB_IF:
6758e58c521SJiaxin Yu default:
6768e58c521SJiaxin Yu tdm_priv->bck_invert = TDM_BCK_INV;
6778e58c521SJiaxin Yu tdm_priv->lck_invert = TDM_LCK_INV;
6788e58c521SJiaxin Yu break;
6798e58c521SJiaxin Yu }
6808e58c521SJiaxin Yu
6818e58c521SJiaxin Yu return 0;
6828e58c521SJiaxin Yu }
6838e58c521SJiaxin Yu
684a94aec03SShunli Wang static const struct snd_soc_dai_ops mtk_dai_tdm_ops = {
685a94aec03SShunli Wang .hw_params = mtk_dai_tdm_hw_params,
686a94aec03SShunli Wang .trigger = mtk_dai_tdm_trigger,
687a94aec03SShunli Wang .set_sysclk = mtk_dai_tdm_set_sysclk,
6888e58c521SJiaxin Yu .set_fmt = mtk_dai_tdm_set_fmt,
689a94aec03SShunli Wang };
690a94aec03SShunli Wang
691a94aec03SShunli Wang /* dai driver */
692a94aec03SShunli Wang #define MTK_TDM_RATES (SNDRV_PCM_RATE_8000_48000 |\
693a94aec03SShunli Wang SNDRV_PCM_RATE_88200 |\
694a94aec03SShunli Wang SNDRV_PCM_RATE_96000 |\
695a94aec03SShunli Wang SNDRV_PCM_RATE_176400 |\
696a94aec03SShunli Wang SNDRV_PCM_RATE_192000)
697a94aec03SShunli Wang
698a94aec03SShunli Wang #define MTK_TDM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
699a94aec03SShunli Wang SNDRV_PCM_FMTBIT_S24_LE |\
700a94aec03SShunli Wang SNDRV_PCM_FMTBIT_S32_LE)
701a94aec03SShunli Wang
702a94aec03SShunli Wang static struct snd_soc_dai_driver mtk_dai_tdm_driver[] = {
703a94aec03SShunli Wang {
704a94aec03SShunli Wang .name = "TDM",
705a94aec03SShunli Wang .id = MT8183_DAI_TDM,
706a94aec03SShunli Wang .playback = {
707a94aec03SShunli Wang .stream_name = "TDM",
708a94aec03SShunli Wang .channels_min = 2,
709a94aec03SShunli Wang .channels_max = 8,
710a94aec03SShunli Wang .rates = MTK_TDM_RATES,
711a94aec03SShunli Wang .formats = MTK_TDM_FORMATS,
712a94aec03SShunli Wang },
713a94aec03SShunli Wang .ops = &mtk_dai_tdm_ops,
714a94aec03SShunli Wang },
715a94aec03SShunli Wang };
716a94aec03SShunli Wang
mt8183_dai_tdm_register(struct mtk_base_afe * afe)717a94aec03SShunli Wang int mt8183_dai_tdm_register(struct mtk_base_afe *afe)
718a94aec03SShunli Wang {
719a94aec03SShunli Wang struct mt8183_afe_private *afe_priv = afe->platform_priv;
720a94aec03SShunli Wang struct mtk_afe_tdm_priv *tdm_priv;
721a94aec03SShunli Wang struct mtk_base_afe_dai *dai;
722a94aec03SShunli Wang
723a94aec03SShunli Wang dai = devm_kzalloc(afe->dev, sizeof(*dai), GFP_KERNEL);
724a94aec03SShunli Wang if (!dai)
725a94aec03SShunli Wang return -ENOMEM;
726a94aec03SShunli Wang
727a94aec03SShunli Wang list_add(&dai->list, &afe->sub_dais);
728a94aec03SShunli Wang
729a94aec03SShunli Wang dai->dai_drivers = mtk_dai_tdm_driver;
730a94aec03SShunli Wang dai->num_dai_drivers = ARRAY_SIZE(mtk_dai_tdm_driver);
731a94aec03SShunli Wang
732a94aec03SShunli Wang dai->dapm_widgets = mtk_dai_tdm_widgets;
733a94aec03SShunli Wang dai->num_dapm_widgets = ARRAY_SIZE(mtk_dai_tdm_widgets);
734a94aec03SShunli Wang dai->dapm_routes = mtk_dai_tdm_routes;
735a94aec03SShunli Wang dai->num_dapm_routes = ARRAY_SIZE(mtk_dai_tdm_routes);
736a94aec03SShunli Wang
737a94aec03SShunli Wang tdm_priv = devm_kzalloc(afe->dev, sizeof(struct mtk_afe_tdm_priv),
738a94aec03SShunli Wang GFP_KERNEL);
739a94aec03SShunli Wang if (!tdm_priv)
740a94aec03SShunli Wang return -ENOMEM;
741a94aec03SShunli Wang
742a94aec03SShunli Wang tdm_priv->mclk_multiple = 128;
743a94aec03SShunli Wang tdm_priv->bck_id = MT8183_I2S4_BCK;
744a94aec03SShunli Wang tdm_priv->mclk_id = MT8183_I2S4_MCK;
745a94aec03SShunli Wang
746a94aec03SShunli Wang afe_priv->dai_priv[MT8183_DAI_TDM] = tdm_priv;
747a94aec03SShunli Wang return 0;
748a94aec03SShunli Wang }
749