1f6b02647STrevor Wu // SPDX-License-Identifier: GPL-2.0
2f6b02647STrevor Wu /*
3f6b02647STrevor Wu * mt8188-afe-clk.c -- MediaTek 8188 afe clock ctrl
4f6b02647STrevor Wu *
5f6b02647STrevor Wu * Copyright (c) 2022 MediaTek Inc.
6f6b02647STrevor Wu * Author: Bicycle Tsai <bicycle.tsai@mediatek.com>
7f6b02647STrevor Wu * Trevor Wu <trevor.wu@mediatek.com>
8f6b02647STrevor Wu * Chun-Chia Chiu <chun-chia.chiu@mediatek.com>
9f6b02647STrevor Wu */
10f6b02647STrevor Wu
11f6b02647STrevor Wu #include <linux/clk.h>
12f6b02647STrevor Wu
13f6b02647STrevor Wu #include "mt8188-afe-common.h"
14f6b02647STrevor Wu #include "mt8188-afe-clk.h"
15f6b02647STrevor Wu #include "mt8188-audsys-clk.h"
16f6b02647STrevor Wu #include "mt8188-reg.h"
17f6b02647STrevor Wu
18f6b02647STrevor Wu static const char *aud_clks[MT8188_CLK_NUM] = {
19f6b02647STrevor Wu /* xtal */
20f6b02647STrevor Wu [MT8188_CLK_XTAL_26M] = "clk26m",
21f6b02647STrevor Wu
22f6b02647STrevor Wu /* pll */
23f6b02647STrevor Wu [MT8188_CLK_APMIXED_APLL1] = "apll1",
24f6b02647STrevor Wu [MT8188_CLK_APMIXED_APLL2] = "apll2",
25f6b02647STrevor Wu
26f6b02647STrevor Wu /* divider */
279be0213aSTrevor Wu [MT8188_CLK_TOP_APLL1_D4] = "apll1_d4",
28*2e5c422aSTrevor Wu [MT8188_CLK_TOP_APLL2_D4] = "apll2_d4",
29f6b02647STrevor Wu [MT8188_CLK_TOP_APLL12_DIV0] = "apll12_div0",
30f6b02647STrevor Wu [MT8188_CLK_TOP_APLL12_DIV1] = "apll12_div1",
31f6b02647STrevor Wu [MT8188_CLK_TOP_APLL12_DIV2] = "apll12_div2",
32f6b02647STrevor Wu [MT8188_CLK_TOP_APLL12_DIV3] = "apll12_div3",
33*2e5c422aSTrevor Wu [MT8188_CLK_TOP_APLL12_DIV4] = "apll12_div4",
34f6b02647STrevor Wu [MT8188_CLK_TOP_APLL12_DIV9] = "apll12_div9",
35f6b02647STrevor Wu
36f6b02647STrevor Wu /* mux */
37f6b02647STrevor Wu [MT8188_CLK_TOP_A1SYS_HP_SEL] = "top_a1sys_hp",
38*2e5c422aSTrevor Wu [MT8188_CLK_TOP_A2SYS_SEL] = "top_a2sys",
39*2e5c422aSTrevor Wu [MT8188_CLK_TOP_AUD_IEC_SEL] = "top_aud_iec",
40f6b02647STrevor Wu [MT8188_CLK_TOP_AUD_INTBUS_SEL] = "top_aud_intbus",
41f6b02647STrevor Wu [MT8188_CLK_TOP_AUDIO_H_SEL] = "top_audio_h",
42f6b02647STrevor Wu [MT8188_CLK_TOP_AUDIO_LOCAL_BUS_SEL] = "top_audio_local_bus",
43f6b02647STrevor Wu [MT8188_CLK_TOP_DPTX_M_SEL] = "top_dptx",
44f6b02647STrevor Wu [MT8188_CLK_TOP_I2SO1_M_SEL] = "top_i2so1",
45f6b02647STrevor Wu [MT8188_CLK_TOP_I2SO2_M_SEL] = "top_i2so2",
46f6b02647STrevor Wu [MT8188_CLK_TOP_I2SI1_M_SEL] = "top_i2si1",
47f6b02647STrevor Wu [MT8188_CLK_TOP_I2SI2_M_SEL] = "top_i2si2",
48f6b02647STrevor Wu
49f6b02647STrevor Wu /* clock gate */
50f6b02647STrevor Wu [MT8188_CLK_ADSP_AUDIO_26M] = "adsp_audio_26m",
51f6b02647STrevor Wu /* afe clock gate */
52f6b02647STrevor Wu [MT8188_CLK_AUD_AFE] = "aud_afe",
53f6b02647STrevor Wu [MT8188_CLK_AUD_APLL1_TUNER] = "aud_apll1_tuner",
54f6b02647STrevor Wu [MT8188_CLK_AUD_APLL2_TUNER] = "aud_apll2_tuner",
55f6b02647STrevor Wu [MT8188_CLK_AUD_APLL] = "aud_apll",
56f6b02647STrevor Wu [MT8188_CLK_AUD_APLL2] = "aud_apll2",
57f6b02647STrevor Wu [MT8188_CLK_AUD_DAC] = "aud_dac",
58f6b02647STrevor Wu [MT8188_CLK_AUD_ADC] = "aud_adc",
59f6b02647STrevor Wu [MT8188_CLK_AUD_DAC_HIRES] = "aud_dac_hires",
60f6b02647STrevor Wu [MT8188_CLK_AUD_A1SYS_HP] = "aud_a1sys_hp",
61f6b02647STrevor Wu [MT8188_CLK_AUD_ADC_HIRES] = "aud_adc_hires",
62f6b02647STrevor Wu [MT8188_CLK_AUD_I2SIN] = "aud_i2sin",
63f6b02647STrevor Wu [MT8188_CLK_AUD_TDM_IN] = "aud_tdm_in",
64f6b02647STrevor Wu [MT8188_CLK_AUD_I2S_OUT] = "aud_i2s_out",
65f6b02647STrevor Wu [MT8188_CLK_AUD_TDM_OUT] = "aud_tdm_out",
66f6b02647STrevor Wu [MT8188_CLK_AUD_HDMI_OUT] = "aud_hdmi_out",
67f6b02647STrevor Wu [MT8188_CLK_AUD_ASRC11] = "aud_asrc11",
68f6b02647STrevor Wu [MT8188_CLK_AUD_ASRC12] = "aud_asrc12",
69f6b02647STrevor Wu [MT8188_CLK_AUD_A1SYS] = "aud_a1sys",
70f6b02647STrevor Wu [MT8188_CLK_AUD_A2SYS] = "aud_a2sys",
71f6b02647STrevor Wu [MT8188_CLK_AUD_PCMIF] = "aud_pcmif",
72f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_UL1] = "aud_memif_ul1",
73f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_UL2] = "aud_memif_ul2",
74f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_UL3] = "aud_memif_ul3",
75f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_UL4] = "aud_memif_ul4",
76f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_UL5] = "aud_memif_ul5",
77f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_UL6] = "aud_memif_ul6",
78f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_UL8] = "aud_memif_ul8",
79f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_UL9] = "aud_memif_ul9",
80f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_UL10] = "aud_memif_ul10",
81f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_DL2] = "aud_memif_dl2",
82f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_DL3] = "aud_memif_dl3",
83f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_DL6] = "aud_memif_dl6",
84f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_DL7] = "aud_memif_dl7",
85f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_DL8] = "aud_memif_dl8",
86f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_DL10] = "aud_memif_dl10",
87f6b02647STrevor Wu [MT8188_CLK_AUD_MEMIF_DL11] = "aud_memif_dl11",
88f6b02647STrevor Wu };
89f6b02647STrevor Wu
90f6b02647STrevor Wu struct mt8188_afe_tuner_cfg {
91f6b02647STrevor Wu unsigned int id;
92f6b02647STrevor Wu int apll_div_reg;
93f6b02647STrevor Wu unsigned int apll_div_shift;
94f6b02647STrevor Wu unsigned int apll_div_maskbit;
95f6b02647STrevor Wu unsigned int apll_div_default;
96f6b02647STrevor Wu int ref_ck_sel_reg;
97f6b02647STrevor Wu unsigned int ref_ck_sel_shift;
98f6b02647STrevor Wu unsigned int ref_ck_sel_maskbit;
99f6b02647STrevor Wu unsigned int ref_ck_sel_default;
100f6b02647STrevor Wu int tuner_en_reg;
101f6b02647STrevor Wu unsigned int tuner_en_shift;
102f6b02647STrevor Wu unsigned int tuner_en_maskbit;
103f6b02647STrevor Wu int upper_bound_reg;
104f6b02647STrevor Wu unsigned int upper_bound_shift;
105f6b02647STrevor Wu unsigned int upper_bound_maskbit;
106f6b02647STrevor Wu unsigned int upper_bound_default;
107f6b02647STrevor Wu spinlock_t ctrl_lock; /* lock for apll tuner ctrl*/
108f6b02647STrevor Wu int ref_cnt;
109f6b02647STrevor Wu };
110f6b02647STrevor Wu
111f6b02647STrevor Wu static struct mt8188_afe_tuner_cfg
112f6b02647STrevor Wu mt8188_afe_tuner_cfgs[MT8188_AUD_PLL_NUM] = {
113f6b02647STrevor Wu [MT8188_AUD_PLL1] = {
114f6b02647STrevor Wu .id = MT8188_AUD_PLL1,
115f6b02647STrevor Wu .apll_div_reg = AFE_APLL_TUNER_CFG,
116f6b02647STrevor Wu .apll_div_shift = 4,
117f6b02647STrevor Wu .apll_div_maskbit = 0xf,
118f6b02647STrevor Wu .apll_div_default = 0x7,
119f6b02647STrevor Wu .ref_ck_sel_reg = AFE_APLL_TUNER_CFG,
120f6b02647STrevor Wu .ref_ck_sel_shift = 1,
121f6b02647STrevor Wu .ref_ck_sel_maskbit = 0x3,
122f6b02647STrevor Wu .ref_ck_sel_default = 0x2,
123f6b02647STrevor Wu .tuner_en_reg = AFE_APLL_TUNER_CFG,
124f6b02647STrevor Wu .tuner_en_shift = 0,
125f6b02647STrevor Wu .tuner_en_maskbit = 0x1,
126f6b02647STrevor Wu .upper_bound_reg = AFE_APLL_TUNER_CFG,
127f6b02647STrevor Wu .upper_bound_shift = 8,
128f6b02647STrevor Wu .upper_bound_maskbit = 0xff,
129f6b02647STrevor Wu .upper_bound_default = 0x3,
130f6b02647STrevor Wu },
131f6b02647STrevor Wu [MT8188_AUD_PLL2] = {
132f6b02647STrevor Wu .id = MT8188_AUD_PLL2,
133f6b02647STrevor Wu .apll_div_reg = AFE_APLL_TUNER_CFG1,
134f6b02647STrevor Wu .apll_div_shift = 4,
135f6b02647STrevor Wu .apll_div_maskbit = 0xf,
136f6b02647STrevor Wu .apll_div_default = 0x7,
137f6b02647STrevor Wu .ref_ck_sel_reg = AFE_APLL_TUNER_CFG1,
138f6b02647STrevor Wu .ref_ck_sel_shift = 1,
139f6b02647STrevor Wu .ref_ck_sel_maskbit = 0x3,
140f6b02647STrevor Wu .ref_ck_sel_default = 0x1,
141f6b02647STrevor Wu .tuner_en_reg = AFE_APLL_TUNER_CFG1,
142f6b02647STrevor Wu .tuner_en_shift = 0,
143f6b02647STrevor Wu .tuner_en_maskbit = 0x1,
144f6b02647STrevor Wu .upper_bound_reg = AFE_APLL_TUNER_CFG1,
145f6b02647STrevor Wu .upper_bound_shift = 8,
146f6b02647STrevor Wu .upper_bound_maskbit = 0xff,
147f6b02647STrevor Wu .upper_bound_default = 0x3,
148f6b02647STrevor Wu },
149f6b02647STrevor Wu [MT8188_AUD_PLL3] = {
150f6b02647STrevor Wu .id = MT8188_AUD_PLL3,
151f6b02647STrevor Wu .apll_div_reg = AFE_EARC_APLL_TUNER_CFG,
152f6b02647STrevor Wu .apll_div_shift = 4,
153f6b02647STrevor Wu .apll_div_maskbit = 0x3f,
154f6b02647STrevor Wu .apll_div_default = 0x3,
155f6b02647STrevor Wu .ref_ck_sel_reg = AFE_EARC_APLL_TUNER_CFG,
156f6b02647STrevor Wu .ref_ck_sel_shift = 24,
157f6b02647STrevor Wu .ref_ck_sel_maskbit = 0x3,
158f6b02647STrevor Wu .ref_ck_sel_default = 0x0,
159f6b02647STrevor Wu .tuner_en_reg = AFE_EARC_APLL_TUNER_CFG,
160f6b02647STrevor Wu .tuner_en_shift = 0,
161f6b02647STrevor Wu .tuner_en_maskbit = 0x1,
162f6b02647STrevor Wu .upper_bound_reg = AFE_EARC_APLL_TUNER_CFG,
163f6b02647STrevor Wu .upper_bound_shift = 12,
164f6b02647STrevor Wu .upper_bound_maskbit = 0xff,
165f6b02647STrevor Wu .upper_bound_default = 0x4,
166f6b02647STrevor Wu },
167f6b02647STrevor Wu [MT8188_AUD_PLL4] = {
168f6b02647STrevor Wu .id = MT8188_AUD_PLL4,
169f6b02647STrevor Wu .apll_div_reg = AFE_SPDIFIN_APLL_TUNER_CFG,
170f6b02647STrevor Wu .apll_div_shift = 4,
171f6b02647STrevor Wu .apll_div_maskbit = 0x3f,
172f6b02647STrevor Wu .apll_div_default = 0x7,
173f6b02647STrevor Wu .ref_ck_sel_reg = AFE_SPDIFIN_APLL_TUNER_CFG1,
174f6b02647STrevor Wu .ref_ck_sel_shift = 8,
175f6b02647STrevor Wu .ref_ck_sel_maskbit = 0x1,
176f6b02647STrevor Wu .ref_ck_sel_default = 0,
177f6b02647STrevor Wu .tuner_en_reg = AFE_SPDIFIN_APLL_TUNER_CFG,
178f6b02647STrevor Wu .tuner_en_shift = 0,
179f6b02647STrevor Wu .tuner_en_maskbit = 0x1,
180f6b02647STrevor Wu .upper_bound_reg = AFE_SPDIFIN_APLL_TUNER_CFG,
181f6b02647STrevor Wu .upper_bound_shift = 12,
182f6b02647STrevor Wu .upper_bound_maskbit = 0xff,
183f6b02647STrevor Wu .upper_bound_default = 0x4,
184f6b02647STrevor Wu },
185f6b02647STrevor Wu [MT8188_AUD_PLL5] = {
186f6b02647STrevor Wu .id = MT8188_AUD_PLL5,
187f6b02647STrevor Wu .apll_div_reg = AFE_LINEIN_APLL_TUNER_CFG,
188f6b02647STrevor Wu .apll_div_shift = 4,
189f6b02647STrevor Wu .apll_div_maskbit = 0x3f,
190f6b02647STrevor Wu .apll_div_default = 0x3,
191f6b02647STrevor Wu .ref_ck_sel_reg = AFE_LINEIN_APLL_TUNER_CFG,
192f6b02647STrevor Wu .ref_ck_sel_shift = 24,
193f6b02647STrevor Wu .ref_ck_sel_maskbit = 0x1,
194f6b02647STrevor Wu .ref_ck_sel_default = 0,
195f6b02647STrevor Wu .tuner_en_reg = AFE_LINEIN_APLL_TUNER_CFG,
196f6b02647STrevor Wu .tuner_en_shift = 0,
197f6b02647STrevor Wu .tuner_en_maskbit = 0x1,
198f6b02647STrevor Wu .upper_bound_reg = AFE_LINEIN_APLL_TUNER_CFG,
199f6b02647STrevor Wu .upper_bound_shift = 12,
200f6b02647STrevor Wu .upper_bound_maskbit = 0xff,
201f6b02647STrevor Wu .upper_bound_default = 0x4,
202f6b02647STrevor Wu },
203f6b02647STrevor Wu };
204f6b02647STrevor Wu
mt8188_afe_found_apll_tuner(unsigned int id)205f6b02647STrevor Wu static struct mt8188_afe_tuner_cfg *mt8188_afe_found_apll_tuner(unsigned int id)
206f6b02647STrevor Wu {
207f6b02647STrevor Wu if (id >= MT8188_AUD_PLL_NUM)
208f6b02647STrevor Wu return NULL;
209f6b02647STrevor Wu
210f6b02647STrevor Wu return &mt8188_afe_tuner_cfgs[id];
211f6b02647STrevor Wu }
212f6b02647STrevor Wu
mt8188_afe_init_apll_tuner(unsigned int id)213f6b02647STrevor Wu static int mt8188_afe_init_apll_tuner(unsigned int id)
214f6b02647STrevor Wu {
215f6b02647STrevor Wu struct mt8188_afe_tuner_cfg *cfg = mt8188_afe_found_apll_tuner(id);
216f6b02647STrevor Wu
217f6b02647STrevor Wu if (!cfg)
218f6b02647STrevor Wu return -EINVAL;
219f6b02647STrevor Wu
220f6b02647STrevor Wu cfg->ref_cnt = 0;
221f6b02647STrevor Wu spin_lock_init(&cfg->ctrl_lock);
222f6b02647STrevor Wu
223f6b02647STrevor Wu return 0;
224f6b02647STrevor Wu }
225f6b02647STrevor Wu
mt8188_afe_setup_apll_tuner(struct mtk_base_afe * afe,unsigned int id)226f6b02647STrevor Wu static int mt8188_afe_setup_apll_tuner(struct mtk_base_afe *afe, unsigned int id)
227f6b02647STrevor Wu {
228f6b02647STrevor Wu const struct mt8188_afe_tuner_cfg *cfg = mt8188_afe_found_apll_tuner(id);
229f6b02647STrevor Wu
230f6b02647STrevor Wu if (!cfg)
231f6b02647STrevor Wu return -EINVAL;
232f6b02647STrevor Wu
233f6b02647STrevor Wu regmap_update_bits(afe->regmap,
234f6b02647STrevor Wu cfg->apll_div_reg,
235f6b02647STrevor Wu cfg->apll_div_maskbit << cfg->apll_div_shift,
236f6b02647STrevor Wu cfg->apll_div_default << cfg->apll_div_shift);
237f6b02647STrevor Wu
238f6b02647STrevor Wu regmap_update_bits(afe->regmap,
239f6b02647STrevor Wu cfg->ref_ck_sel_reg,
240f6b02647STrevor Wu cfg->ref_ck_sel_maskbit << cfg->ref_ck_sel_shift,
241f6b02647STrevor Wu cfg->ref_ck_sel_default << cfg->ref_ck_sel_shift);
242f6b02647STrevor Wu
243f6b02647STrevor Wu regmap_update_bits(afe->regmap,
244f6b02647STrevor Wu cfg->upper_bound_reg,
245f6b02647STrevor Wu cfg->upper_bound_maskbit << cfg->upper_bound_shift,
246f6b02647STrevor Wu cfg->upper_bound_default << cfg->upper_bound_shift);
247f6b02647STrevor Wu
248f6b02647STrevor Wu return 0;
249f6b02647STrevor Wu }
250f6b02647STrevor Wu
mt8188_afe_enable_tuner_clk(struct mtk_base_afe * afe,unsigned int id)251f6b02647STrevor Wu static int mt8188_afe_enable_tuner_clk(struct mtk_base_afe *afe,
252f6b02647STrevor Wu unsigned int id)
253f6b02647STrevor Wu {
254f6b02647STrevor Wu struct mt8188_afe_private *afe_priv = afe->platform_priv;
255f6b02647STrevor Wu
256f6b02647STrevor Wu switch (id) {
257f6b02647STrevor Wu case MT8188_AUD_PLL1:
258f6b02647STrevor Wu mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_APLL]);
259f6b02647STrevor Wu mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_APLL1_TUNER]);
260f6b02647STrevor Wu break;
261f6b02647STrevor Wu case MT8188_AUD_PLL2:
262f6b02647STrevor Wu mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_APLL2]);
263f6b02647STrevor Wu mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_APLL2_TUNER]);
264f6b02647STrevor Wu break;
265f6b02647STrevor Wu default:
266f6b02647STrevor Wu return -EINVAL;
267f6b02647STrevor Wu }
268f6b02647STrevor Wu
269f6b02647STrevor Wu return 0;
270f6b02647STrevor Wu }
271f6b02647STrevor Wu
mt8188_afe_disable_tuner_clk(struct mtk_base_afe * afe,unsigned int id)272f6b02647STrevor Wu static int mt8188_afe_disable_tuner_clk(struct mtk_base_afe *afe,
273f6b02647STrevor Wu unsigned int id)
274f6b02647STrevor Wu {
275f6b02647STrevor Wu struct mt8188_afe_private *afe_priv = afe->platform_priv;
276f6b02647STrevor Wu
277f6b02647STrevor Wu switch (id) {
278f6b02647STrevor Wu case MT8188_AUD_PLL1:
279f6b02647STrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_APLL1_TUNER]);
280f6b02647STrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_APLL]);
281f6b02647STrevor Wu break;
282f6b02647STrevor Wu case MT8188_AUD_PLL2:
283f6b02647STrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_APLL2_TUNER]);
284f6b02647STrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_APLL2]);
285f6b02647STrevor Wu break;
286f6b02647STrevor Wu default:
287f6b02647STrevor Wu return -EINVAL;
288f6b02647STrevor Wu }
289f6b02647STrevor Wu
290f6b02647STrevor Wu return 0;
291f6b02647STrevor Wu }
292f6b02647STrevor Wu
mt8188_afe_enable_apll_tuner(struct mtk_base_afe * afe,unsigned int id)293f6b02647STrevor Wu static int mt8188_afe_enable_apll_tuner(struct mtk_base_afe *afe, unsigned int id)
294f6b02647STrevor Wu {
295f6b02647STrevor Wu struct mt8188_afe_tuner_cfg *cfg = mt8188_afe_found_apll_tuner(id);
296f6b02647STrevor Wu unsigned long flags;
297f6b02647STrevor Wu int ret;
298f6b02647STrevor Wu
299f6b02647STrevor Wu if (!cfg)
300f6b02647STrevor Wu return -EINVAL;
301f6b02647STrevor Wu
302f6b02647STrevor Wu ret = mt8188_afe_setup_apll_tuner(afe, id);
303f6b02647STrevor Wu if (ret)
304f6b02647STrevor Wu return ret;
305f6b02647STrevor Wu
306f6b02647STrevor Wu ret = mt8188_afe_enable_tuner_clk(afe, id);
307f6b02647STrevor Wu if (ret)
308f6b02647STrevor Wu return ret;
309f6b02647STrevor Wu
310f6b02647STrevor Wu spin_lock_irqsave(&cfg->ctrl_lock, flags);
311f6b02647STrevor Wu
312f6b02647STrevor Wu cfg->ref_cnt++;
313f6b02647STrevor Wu if (cfg->ref_cnt == 1)
314f6b02647STrevor Wu regmap_update_bits(afe->regmap,
315f6b02647STrevor Wu cfg->tuner_en_reg,
316f6b02647STrevor Wu cfg->tuner_en_maskbit << cfg->tuner_en_shift,
317f6b02647STrevor Wu BIT(cfg->tuner_en_shift));
318f6b02647STrevor Wu
319f6b02647STrevor Wu spin_unlock_irqrestore(&cfg->ctrl_lock, flags);
320f6b02647STrevor Wu
321f6b02647STrevor Wu return 0;
322f6b02647STrevor Wu }
323f6b02647STrevor Wu
mt8188_afe_disable_apll_tuner(struct mtk_base_afe * afe,unsigned int id)324f6b02647STrevor Wu static int mt8188_afe_disable_apll_tuner(struct mtk_base_afe *afe, unsigned int id)
325f6b02647STrevor Wu {
326f6b02647STrevor Wu struct mt8188_afe_tuner_cfg *cfg = mt8188_afe_found_apll_tuner(id);
327f6b02647STrevor Wu unsigned long flags;
328f6b02647STrevor Wu int ret;
329f6b02647STrevor Wu
330f6b02647STrevor Wu if (!cfg)
331f6b02647STrevor Wu return -EINVAL;
332f6b02647STrevor Wu
333f6b02647STrevor Wu spin_lock_irqsave(&cfg->ctrl_lock, flags);
334f6b02647STrevor Wu
335f6b02647STrevor Wu cfg->ref_cnt--;
336f6b02647STrevor Wu if (cfg->ref_cnt == 0)
337f6b02647STrevor Wu regmap_update_bits(afe->regmap,
338f6b02647STrevor Wu cfg->tuner_en_reg,
339f6b02647STrevor Wu cfg->tuner_en_maskbit << cfg->tuner_en_shift,
340f6b02647STrevor Wu 0 << cfg->tuner_en_shift);
341f6b02647STrevor Wu else if (cfg->ref_cnt < 0)
342f6b02647STrevor Wu cfg->ref_cnt = 0;
343f6b02647STrevor Wu
344f6b02647STrevor Wu spin_unlock_irqrestore(&cfg->ctrl_lock, flags);
345f6b02647STrevor Wu
346f6b02647STrevor Wu ret = mt8188_afe_disable_tuner_clk(afe, id);
347f6b02647STrevor Wu if (ret)
348f6b02647STrevor Wu return ret;
349f6b02647STrevor Wu
350f6b02647STrevor Wu return 0;
351f6b02647STrevor Wu }
352f6b02647STrevor Wu
mt8188_afe_get_mclk_source_clk_id(int sel)353f6b02647STrevor Wu int mt8188_afe_get_mclk_source_clk_id(int sel)
354f6b02647STrevor Wu {
355f6b02647STrevor Wu switch (sel) {
356f6b02647STrevor Wu case MT8188_MCK_SEL_26M:
357f6b02647STrevor Wu return MT8188_CLK_XTAL_26M;
358f6b02647STrevor Wu case MT8188_MCK_SEL_APLL1:
359f6b02647STrevor Wu return MT8188_CLK_APMIXED_APLL1;
360f6b02647STrevor Wu case MT8188_MCK_SEL_APLL2:
361f6b02647STrevor Wu return MT8188_CLK_APMIXED_APLL2;
362f6b02647STrevor Wu default:
363f6b02647STrevor Wu return -EINVAL;
364f6b02647STrevor Wu }
365f6b02647STrevor Wu }
366f6b02647STrevor Wu
mt8188_afe_get_mclk_source_rate(struct mtk_base_afe * afe,int apll)367f6b02647STrevor Wu int mt8188_afe_get_mclk_source_rate(struct mtk_base_afe *afe, int apll)
368f6b02647STrevor Wu {
369f6b02647STrevor Wu struct mt8188_afe_private *afe_priv = afe->platform_priv;
370f6b02647STrevor Wu int clk_id = mt8188_afe_get_mclk_source_clk_id(apll);
371f6b02647STrevor Wu
372f6b02647STrevor Wu if (clk_id < 0) {
373f6b02647STrevor Wu dev_dbg(afe->dev, "invalid clk id\n");
374f6b02647STrevor Wu return 0;
375f6b02647STrevor Wu }
376f6b02647STrevor Wu
377f6b02647STrevor Wu return clk_get_rate(afe_priv->clk[clk_id]);
378f6b02647STrevor Wu }
379f6b02647STrevor Wu
mt8188_afe_get_default_mclk_source_by_rate(int rate)380f6b02647STrevor Wu int mt8188_afe_get_default_mclk_source_by_rate(int rate)
381f6b02647STrevor Wu {
382f6b02647STrevor Wu return ((rate % 8000) == 0) ?
383f6b02647STrevor Wu MT8188_MCK_SEL_APLL1 : MT8188_MCK_SEL_APLL2;
384f6b02647STrevor Wu }
385f6b02647STrevor Wu
mt8188_get_apll_by_rate(struct mtk_base_afe * afe,int rate)3869be0213aSTrevor Wu int mt8188_get_apll_by_rate(struct mtk_base_afe *afe, int rate)
3879be0213aSTrevor Wu {
3889be0213aSTrevor Wu return ((rate % 8000) == 0) ? MT8188_AUD_PLL1 : MT8188_AUD_PLL2;
3899be0213aSTrevor Wu }
3909be0213aSTrevor Wu
mt8188_get_apll_by_name(struct mtk_base_afe * afe,const char * name)3919be0213aSTrevor Wu int mt8188_get_apll_by_name(struct mtk_base_afe *afe, const char *name)
3929be0213aSTrevor Wu {
3939be0213aSTrevor Wu if (strcmp(name, APLL1_W_NAME) == 0)
3949be0213aSTrevor Wu return MT8188_AUD_PLL1;
3959be0213aSTrevor Wu
3969be0213aSTrevor Wu return MT8188_AUD_PLL2;
3979be0213aSTrevor Wu }
3989be0213aSTrevor Wu
mt8188_afe_init_clock(struct mtk_base_afe * afe)399f6b02647STrevor Wu int mt8188_afe_init_clock(struct mtk_base_afe *afe)
400f6b02647STrevor Wu {
401f6b02647STrevor Wu struct mt8188_afe_private *afe_priv = afe->platform_priv;
402f6b02647STrevor Wu int i, ret;
403f6b02647STrevor Wu
404f6b02647STrevor Wu ret = mt8188_audsys_clk_register(afe);
405f6b02647STrevor Wu if (ret) {
406f6b02647STrevor Wu dev_err(afe->dev, "register audsys clk fail %d\n", ret);
407f6b02647STrevor Wu return ret;
408f6b02647STrevor Wu }
409f6b02647STrevor Wu
410f6b02647STrevor Wu afe_priv->clk =
411f6b02647STrevor Wu devm_kcalloc(afe->dev, MT8188_CLK_NUM, sizeof(*afe_priv->clk),
412f6b02647STrevor Wu GFP_KERNEL);
413f6b02647STrevor Wu if (!afe_priv->clk)
414f6b02647STrevor Wu return -ENOMEM;
415f6b02647STrevor Wu
416f6b02647STrevor Wu for (i = 0; i < MT8188_CLK_NUM; i++) {
417f6b02647STrevor Wu afe_priv->clk[i] = devm_clk_get(afe->dev, aud_clks[i]);
418f6b02647STrevor Wu if (IS_ERR(afe_priv->clk[i])) {
419f6b02647STrevor Wu dev_err(afe->dev, "%s(), devm_clk_get %s fail, ret %ld\n",
420f6b02647STrevor Wu __func__, aud_clks[i],
421f6b02647STrevor Wu PTR_ERR(afe_priv->clk[i]));
422f6b02647STrevor Wu return PTR_ERR(afe_priv->clk[i]);
423f6b02647STrevor Wu }
424f6b02647STrevor Wu }
425f6b02647STrevor Wu
426f6b02647STrevor Wu /* initial tuner */
427f6b02647STrevor Wu for (i = 0; i < MT8188_AUD_PLL_NUM; i++) {
428f6b02647STrevor Wu ret = mt8188_afe_init_apll_tuner(i);
429f6b02647STrevor Wu if (ret) {
430f6b02647STrevor Wu dev_info(afe->dev, "%s(), init apll_tuner%d failed",
431f6b02647STrevor Wu __func__, (i + 1));
432f6b02647STrevor Wu return -EINVAL;
433f6b02647STrevor Wu }
434f6b02647STrevor Wu }
435f6b02647STrevor Wu
436f6b02647STrevor Wu return 0;
437f6b02647STrevor Wu }
438f6b02647STrevor Wu
mt8188_afe_enable_clk(struct mtk_base_afe * afe,struct clk * clk)439f6b02647STrevor Wu int mt8188_afe_enable_clk(struct mtk_base_afe *afe, struct clk *clk)
440f6b02647STrevor Wu {
441f6b02647STrevor Wu int ret;
442f6b02647STrevor Wu
443f6b02647STrevor Wu if (clk) {
444f6b02647STrevor Wu ret = clk_prepare_enable(clk);
445f6b02647STrevor Wu if (ret) {
446f6b02647STrevor Wu dev_dbg(afe->dev, "%s(), failed to enable clk\n",
447f6b02647STrevor Wu __func__);
448f6b02647STrevor Wu return ret;
449f6b02647STrevor Wu }
450f6b02647STrevor Wu } else {
451f6b02647STrevor Wu dev_dbg(afe->dev, "NULL clk\n");
452f6b02647STrevor Wu }
453f6b02647STrevor Wu return 0;
454f6b02647STrevor Wu }
455f6b02647STrevor Wu EXPORT_SYMBOL_GPL(mt8188_afe_enable_clk);
456f6b02647STrevor Wu
mt8188_afe_disable_clk(struct mtk_base_afe * afe,struct clk * clk)457f6b02647STrevor Wu void mt8188_afe_disable_clk(struct mtk_base_afe *afe, struct clk *clk)
458f6b02647STrevor Wu {
459f6b02647STrevor Wu if (clk)
460f6b02647STrevor Wu clk_disable_unprepare(clk);
461f6b02647STrevor Wu else
462f6b02647STrevor Wu dev_dbg(afe->dev, "NULL clk\n");
463f6b02647STrevor Wu }
464f6b02647STrevor Wu EXPORT_SYMBOL_GPL(mt8188_afe_disable_clk);
465f6b02647STrevor Wu
mt8188_afe_set_clk_rate(struct mtk_base_afe * afe,struct clk * clk,unsigned int rate)466f6b02647STrevor Wu int mt8188_afe_set_clk_rate(struct mtk_base_afe *afe, struct clk *clk,
467f6b02647STrevor Wu unsigned int rate)
468f6b02647STrevor Wu {
469f6b02647STrevor Wu int ret;
470f6b02647STrevor Wu
471f6b02647STrevor Wu if (clk) {
472f6b02647STrevor Wu ret = clk_set_rate(clk, rate);
473f6b02647STrevor Wu if (ret) {
474f6b02647STrevor Wu dev_dbg(afe->dev, "%s(), failed to set clk rate\n",
475f6b02647STrevor Wu __func__);
476f6b02647STrevor Wu return ret;
477f6b02647STrevor Wu }
478f6b02647STrevor Wu }
479f6b02647STrevor Wu
480f6b02647STrevor Wu return 0;
481f6b02647STrevor Wu }
482f6b02647STrevor Wu
mt8188_afe_set_clk_parent(struct mtk_base_afe * afe,struct clk * clk,struct clk * parent)483f6b02647STrevor Wu int mt8188_afe_set_clk_parent(struct mtk_base_afe *afe, struct clk *clk,
484f6b02647STrevor Wu struct clk *parent)
485f6b02647STrevor Wu {
486f6b02647STrevor Wu int ret;
487f6b02647STrevor Wu
488f6b02647STrevor Wu if (clk && parent) {
489f6b02647STrevor Wu ret = clk_set_parent(clk, parent);
490f6b02647STrevor Wu if (ret) {
4919be0213aSTrevor Wu dev_dbg(afe->dev, "%s(), failed to set clk parent %d\n",
4929be0213aSTrevor Wu __func__, ret);
493f6b02647STrevor Wu return ret;
494f6b02647STrevor Wu }
495f6b02647STrevor Wu }
496f6b02647STrevor Wu
497f6b02647STrevor Wu return 0;
498f6b02647STrevor Wu }
499f6b02647STrevor Wu
get_top_cg_reg(unsigned int cg_type)500f6b02647STrevor Wu static unsigned int get_top_cg_reg(unsigned int cg_type)
501f6b02647STrevor Wu {
502f6b02647STrevor Wu switch (cg_type) {
503f6b02647STrevor Wu case MT8188_TOP_CG_A1SYS_TIMING:
504f6b02647STrevor Wu case MT8188_TOP_CG_A2SYS_TIMING:
505f6b02647STrevor Wu case MT8188_TOP_CG_26M_TIMING:
506f6b02647STrevor Wu return ASYS_TOP_CON;
507f6b02647STrevor Wu default:
508f6b02647STrevor Wu return 0;
509f6b02647STrevor Wu }
510f6b02647STrevor Wu }
511f6b02647STrevor Wu
get_top_cg_mask(unsigned int cg_type)512f6b02647STrevor Wu static unsigned int get_top_cg_mask(unsigned int cg_type)
513f6b02647STrevor Wu {
514f6b02647STrevor Wu switch (cg_type) {
515f6b02647STrevor Wu case MT8188_TOP_CG_A1SYS_TIMING:
516f6b02647STrevor Wu return ASYS_TOP_CON_A1SYS_TIMING_ON;
517f6b02647STrevor Wu case MT8188_TOP_CG_A2SYS_TIMING:
518f6b02647STrevor Wu return ASYS_TOP_CON_A2SYS_TIMING_ON;
519f6b02647STrevor Wu case MT8188_TOP_CG_26M_TIMING:
520f6b02647STrevor Wu return ASYS_TOP_CON_26M_TIMING_ON;
521f6b02647STrevor Wu default:
522f6b02647STrevor Wu return 0;
523f6b02647STrevor Wu }
524f6b02647STrevor Wu }
525f6b02647STrevor Wu
get_top_cg_on_val(unsigned int cg_type)526f6b02647STrevor Wu static unsigned int get_top_cg_on_val(unsigned int cg_type)
527f6b02647STrevor Wu {
528f6b02647STrevor Wu switch (cg_type) {
529f6b02647STrevor Wu case MT8188_TOP_CG_A1SYS_TIMING:
530f6b02647STrevor Wu case MT8188_TOP_CG_A2SYS_TIMING:
531f6b02647STrevor Wu case MT8188_TOP_CG_26M_TIMING:
532f6b02647STrevor Wu return get_top_cg_mask(cg_type);
533f6b02647STrevor Wu default:
534f6b02647STrevor Wu return 0;
535f6b02647STrevor Wu }
536f6b02647STrevor Wu }
537f6b02647STrevor Wu
get_top_cg_off_val(unsigned int cg_type)538f6b02647STrevor Wu static unsigned int get_top_cg_off_val(unsigned int cg_type)
539f6b02647STrevor Wu {
540f6b02647STrevor Wu switch (cg_type) {
541f6b02647STrevor Wu case MT8188_TOP_CG_A1SYS_TIMING:
542f6b02647STrevor Wu case MT8188_TOP_CG_A2SYS_TIMING:
543f6b02647STrevor Wu case MT8188_TOP_CG_26M_TIMING:
544f6b02647STrevor Wu return 0;
545f6b02647STrevor Wu default:
546f6b02647STrevor Wu return get_top_cg_mask(cg_type);
547f6b02647STrevor Wu }
548f6b02647STrevor Wu }
549f6b02647STrevor Wu
mt8188_afe_enable_top_cg(struct mtk_base_afe * afe,unsigned int cg_type)550f6b02647STrevor Wu static int mt8188_afe_enable_top_cg(struct mtk_base_afe *afe, unsigned int cg_type)
551f6b02647STrevor Wu {
552f6b02647STrevor Wu unsigned int reg = get_top_cg_reg(cg_type);
553f6b02647STrevor Wu unsigned int mask = get_top_cg_mask(cg_type);
554f6b02647STrevor Wu unsigned int val = get_top_cg_on_val(cg_type);
555f6b02647STrevor Wu
556f6b02647STrevor Wu regmap_update_bits(afe->regmap, reg, mask, val);
557f6b02647STrevor Wu
558f6b02647STrevor Wu return 0;
559f6b02647STrevor Wu }
560f6b02647STrevor Wu
mt8188_afe_disable_top_cg(struct mtk_base_afe * afe,unsigned int cg_type)561f6b02647STrevor Wu static int mt8188_afe_disable_top_cg(struct mtk_base_afe *afe, unsigned int cg_type)
562f6b02647STrevor Wu {
563f6b02647STrevor Wu unsigned int reg = get_top_cg_reg(cg_type);
564f6b02647STrevor Wu unsigned int mask = get_top_cg_mask(cg_type);
565f6b02647STrevor Wu unsigned int val = get_top_cg_off_val(cg_type);
566f6b02647STrevor Wu
567f6b02647STrevor Wu regmap_update_bits(afe->regmap, reg, mask, val);
568f6b02647STrevor Wu
569f6b02647STrevor Wu return 0;
570f6b02647STrevor Wu }
571f6b02647STrevor Wu
mt8188_afe_enable_reg_rw_clk(struct mtk_base_afe * afe)572f6b02647STrevor Wu int mt8188_afe_enable_reg_rw_clk(struct mtk_base_afe *afe)
573f6b02647STrevor Wu {
574f6b02647STrevor Wu struct mt8188_afe_private *afe_priv = afe->platform_priv;
575f6b02647STrevor Wu
576f6b02647STrevor Wu /* bus clock for AFE external access, like DRAM */
577f6b02647STrevor Wu mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_TOP_AUDIO_LOCAL_BUS_SEL]);
578f6b02647STrevor Wu
579f6b02647STrevor Wu /* bus clock for AFE internal access, like AFE SRAM */
580f6b02647STrevor Wu mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_TOP_AUD_INTBUS_SEL]);
581f6b02647STrevor Wu
582f6b02647STrevor Wu /* audio 26m clock source */
583f6b02647STrevor Wu mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_ADSP_AUDIO_26M]);
584f6b02647STrevor Wu
585f6b02647STrevor Wu /* AFE hw clock */
586f6b02647STrevor Wu mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_AFE]);
587f6b02647STrevor Wu mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_A1SYS_HP]);
588f6b02647STrevor Wu mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_A1SYS]);
589f6b02647STrevor Wu
590f6b02647STrevor Wu return 0;
591f6b02647STrevor Wu }
592f6b02647STrevor Wu
mt8188_afe_disable_reg_rw_clk(struct mtk_base_afe * afe)593f6b02647STrevor Wu int mt8188_afe_disable_reg_rw_clk(struct mtk_base_afe *afe)
594f6b02647STrevor Wu {
595f6b02647STrevor Wu struct mt8188_afe_private *afe_priv = afe->platform_priv;
596f6b02647STrevor Wu
597f6b02647STrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_A1SYS]);
598f6b02647STrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_A1SYS_HP]);
599f6b02647STrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_AFE]);
600f6b02647STrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_ADSP_AUDIO_26M]);
601f6b02647STrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_TOP_AUD_INTBUS_SEL]);
602f6b02647STrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_TOP_AUDIO_LOCAL_BUS_SEL]);
603f6b02647STrevor Wu
604f6b02647STrevor Wu return 0;
605f6b02647STrevor Wu }
606f6b02647STrevor Wu
mt8188_afe_enable_afe_on(struct mtk_base_afe * afe)607f6b02647STrevor Wu static int mt8188_afe_enable_afe_on(struct mtk_base_afe *afe)
608f6b02647STrevor Wu {
609f6b02647STrevor Wu regmap_update_bits(afe->regmap, AFE_DAC_CON0, 0x1, 0x1);
610f6b02647STrevor Wu return 0;
611f6b02647STrevor Wu }
612f6b02647STrevor Wu
mt8188_afe_disable_afe_on(struct mtk_base_afe * afe)613f6b02647STrevor Wu static int mt8188_afe_disable_afe_on(struct mtk_base_afe *afe)
614f6b02647STrevor Wu {
615f6b02647STrevor Wu regmap_update_bits(afe->regmap, AFE_DAC_CON0, 0x1, 0x0);
616f6b02647STrevor Wu return 0;
617f6b02647STrevor Wu }
618f6b02647STrevor Wu
mt8188_afe_enable_a1sys(struct mtk_base_afe * afe)6199be0213aSTrevor Wu static int mt8188_afe_enable_a1sys(struct mtk_base_afe *afe)
6209be0213aSTrevor Wu {
6219be0213aSTrevor Wu struct mt8188_afe_private *afe_priv = afe->platform_priv;
6229be0213aSTrevor Wu int ret;
6239be0213aSTrevor Wu
6249be0213aSTrevor Wu ret = mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_A1SYS]);
6259be0213aSTrevor Wu if (ret)
6269be0213aSTrevor Wu return ret;
6279be0213aSTrevor Wu
6289be0213aSTrevor Wu return mt8188_afe_enable_top_cg(afe, MT8188_TOP_CG_A1SYS_TIMING);
6299be0213aSTrevor Wu }
6309be0213aSTrevor Wu
mt8188_afe_disable_a1sys(struct mtk_base_afe * afe)6319be0213aSTrevor Wu static int mt8188_afe_disable_a1sys(struct mtk_base_afe *afe)
632f6b02647STrevor Wu {
633f6b02647STrevor Wu struct mt8188_afe_private *afe_priv = afe->platform_priv;
634f6b02647STrevor Wu
6359be0213aSTrevor Wu mt8188_afe_disable_top_cg(afe, MT8188_TOP_CG_A1SYS_TIMING);
6369be0213aSTrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_A1SYS]);
6379be0213aSTrevor Wu return 0;
6389be0213aSTrevor Wu }
639f6b02647STrevor Wu
mt8188_afe_enable_a2sys(struct mtk_base_afe * afe)6409be0213aSTrevor Wu static int mt8188_afe_enable_a2sys(struct mtk_base_afe *afe)
6419be0213aSTrevor Wu {
6429be0213aSTrevor Wu struct mt8188_afe_private *afe_priv = afe->platform_priv;
6439be0213aSTrevor Wu int ret;
6449be0213aSTrevor Wu
6459be0213aSTrevor Wu ret = mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_A2SYS]);
6469be0213aSTrevor Wu if (ret)
6479be0213aSTrevor Wu return ret;
6489be0213aSTrevor Wu
6499be0213aSTrevor Wu return mt8188_afe_enable_top_cg(afe, MT8188_TOP_CG_A2SYS_TIMING);
6509be0213aSTrevor Wu }
6519be0213aSTrevor Wu
mt8188_afe_disable_a2sys(struct mtk_base_afe * afe)6529be0213aSTrevor Wu static int mt8188_afe_disable_a2sys(struct mtk_base_afe *afe)
6539be0213aSTrevor Wu {
6549be0213aSTrevor Wu struct mt8188_afe_private *afe_priv = afe->platform_priv;
6559be0213aSTrevor Wu
6569be0213aSTrevor Wu mt8188_afe_disable_top_cg(afe, MT8188_TOP_CG_A2SYS_TIMING);
6579be0213aSTrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_AUD_A2SYS]);
6589be0213aSTrevor Wu return 0;
6599be0213aSTrevor Wu }
6609be0213aSTrevor Wu
mt8188_apll1_enable(struct mtk_base_afe * afe)6619be0213aSTrevor Wu int mt8188_apll1_enable(struct mtk_base_afe *afe)
6629be0213aSTrevor Wu {
6639be0213aSTrevor Wu struct mt8188_afe_private *afe_priv = afe->platform_priv;
6649be0213aSTrevor Wu int ret;
6659be0213aSTrevor Wu
6669be0213aSTrevor Wu ret = mt8188_afe_enable_clk(afe, afe_priv->clk[MT8188_CLK_TOP_APLL1_D4]);
6679be0213aSTrevor Wu if (ret)
6689be0213aSTrevor Wu return ret;
6699be0213aSTrevor Wu
6709be0213aSTrevor Wu ret = mt8188_afe_set_clk_parent(afe, afe_priv->clk[MT8188_CLK_TOP_A1SYS_HP_SEL],
6719be0213aSTrevor Wu afe_priv->clk[MT8188_CLK_TOP_APLL1_D4]);
6729be0213aSTrevor Wu if (ret)
6739be0213aSTrevor Wu goto err_clk_parent;
6749be0213aSTrevor Wu
6759be0213aSTrevor Wu ret = mt8188_afe_enable_apll_tuner(afe, MT8188_AUD_PLL1);
6769be0213aSTrevor Wu if (ret)
6779be0213aSTrevor Wu goto err_apll_tuner;
6789be0213aSTrevor Wu
6799be0213aSTrevor Wu ret = mt8188_afe_enable_a1sys(afe);
6809be0213aSTrevor Wu if (ret)
6819be0213aSTrevor Wu goto err_a1sys;
6829be0213aSTrevor Wu
6839be0213aSTrevor Wu return 0;
6849be0213aSTrevor Wu
6859be0213aSTrevor Wu err_a1sys:
6869be0213aSTrevor Wu mt8188_afe_disable_apll_tuner(afe, MT8188_AUD_PLL1);
6879be0213aSTrevor Wu err_apll_tuner:
6889be0213aSTrevor Wu mt8188_afe_set_clk_parent(afe, afe_priv->clk[MT8188_CLK_TOP_A1SYS_HP_SEL],
6899be0213aSTrevor Wu afe_priv->clk[MT8188_CLK_XTAL_26M]);
6909be0213aSTrevor Wu err_clk_parent:
6919be0213aSTrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_TOP_APLL1_D4]);
6929be0213aSTrevor Wu
6939be0213aSTrevor Wu return ret;
6949be0213aSTrevor Wu }
6959be0213aSTrevor Wu
mt8188_apll1_disable(struct mtk_base_afe * afe)6969be0213aSTrevor Wu int mt8188_apll1_disable(struct mtk_base_afe *afe)
6979be0213aSTrevor Wu {
6989be0213aSTrevor Wu struct mt8188_afe_private *afe_priv = afe->platform_priv;
6999be0213aSTrevor Wu
7009be0213aSTrevor Wu mt8188_afe_disable_a1sys(afe);
7019be0213aSTrevor Wu mt8188_afe_disable_apll_tuner(afe, MT8188_AUD_PLL1);
7029be0213aSTrevor Wu mt8188_afe_set_clk_parent(afe, afe_priv->clk[MT8188_CLK_TOP_A1SYS_HP_SEL],
7039be0213aSTrevor Wu afe_priv->clk[MT8188_CLK_XTAL_26M]);
7049be0213aSTrevor Wu mt8188_afe_disable_clk(afe, afe_priv->clk[MT8188_CLK_TOP_APLL1_D4]);
705f6b02647STrevor Wu
706f6b02647STrevor Wu return 0;
707f6b02647STrevor Wu }
708f6b02647STrevor Wu
mt8188_apll2_enable(struct mtk_base_afe * afe)7099be0213aSTrevor Wu int mt8188_apll2_enable(struct mtk_base_afe *afe)
710f6b02647STrevor Wu {
7119be0213aSTrevor Wu int ret;
712f6b02647STrevor Wu
7139be0213aSTrevor Wu ret = mt8188_afe_enable_apll_tuner(afe, MT8188_AUD_PLL2);
7149be0213aSTrevor Wu if (ret)
7159be0213aSTrevor Wu return ret;
716f6b02647STrevor Wu
7179be0213aSTrevor Wu ret = mt8188_afe_enable_a2sys(afe);
7189be0213aSTrevor Wu if (ret)
7199be0213aSTrevor Wu goto err_a2sys;
720f6b02647STrevor Wu
721f6b02647STrevor Wu return 0;
7229be0213aSTrevor Wu err_a2sys:
7239be0213aSTrevor Wu mt8188_afe_disable_apll_tuner(afe, MT8188_AUD_PLL2);
7249be0213aSTrevor Wu
7259be0213aSTrevor Wu return ret;
7269be0213aSTrevor Wu }
7279be0213aSTrevor Wu
mt8188_apll2_disable(struct mtk_base_afe * afe)7289be0213aSTrevor Wu int mt8188_apll2_disable(struct mtk_base_afe *afe)
7299be0213aSTrevor Wu {
7309be0213aSTrevor Wu mt8188_afe_disable_a2sys(afe);
7319be0213aSTrevor Wu mt8188_afe_disable_apll_tuner(afe, MT8188_AUD_PLL2);
7329be0213aSTrevor Wu return 0;
733f6b02647STrevor Wu }
734f6b02647STrevor Wu
mt8188_afe_enable_main_clock(struct mtk_base_afe * afe)735f6b02647STrevor Wu int mt8188_afe_enable_main_clock(struct mtk_base_afe *afe)
736f6b02647STrevor Wu {
7379be0213aSTrevor Wu mt8188_afe_enable_top_cg(afe, MT8188_TOP_CG_26M_TIMING);
738f6b02647STrevor Wu mt8188_afe_enable_afe_on(afe);
739f6b02647STrevor Wu return 0;
740f6b02647STrevor Wu }
741f6b02647STrevor Wu
mt8188_afe_disable_main_clock(struct mtk_base_afe * afe)742f6b02647STrevor Wu int mt8188_afe_disable_main_clock(struct mtk_base_afe *afe)
743f6b02647STrevor Wu {
744f6b02647STrevor Wu mt8188_afe_disable_afe_on(afe);
7459be0213aSTrevor Wu mt8188_afe_disable_top_cg(afe, MT8188_TOP_CG_26M_TIMING);
746f6b02647STrevor Wu return 0;
747f6b02647STrevor Wu }
748