xref: /linux/sound/soc/mediatek/mt8365/mt8365-afe-clk.c (revision 63157d994025639075b3faa372976a96186322c1)
1ef307b40SAlexandre Mergnat // SPDX-License-Identifier: GPL-2.0
2ef307b40SAlexandre Mergnat /*
3ef307b40SAlexandre Mergnat  * MediaTek 8365 AFE clock control
4ef307b40SAlexandre Mergnat  *
5ef307b40SAlexandre Mergnat  * Copyright (c) 2024 MediaTek Inc.
6ef307b40SAlexandre Mergnat  * Authors: Jia Zeng <jia.zeng@mediatek.com>
7ef307b40SAlexandre Mergnat  *          Alexandre Mergnat <amergnat@baylibre.com>
8ef307b40SAlexandre Mergnat  */
9ef307b40SAlexandre Mergnat 
10ef307b40SAlexandre Mergnat #include "mt8365-afe-clk.h"
11ef307b40SAlexandre Mergnat #include "mt8365-afe-common.h"
12ef307b40SAlexandre Mergnat #include "mt8365-reg.h"
13ef307b40SAlexandre Mergnat #include "../common/mtk-base-afe.h"
14ef307b40SAlexandre Mergnat #include <linux/device.h>
15ef307b40SAlexandre Mergnat #include <linux/mfd/syscon.h>
16ef307b40SAlexandre Mergnat 
17ef307b40SAlexandre Mergnat static const char *aud_clks[MT8365_CLK_NUM] = {
18ef307b40SAlexandre Mergnat 	[MT8365_CLK_TOP_AUD_SEL] = "top_audio_sel",
19ef307b40SAlexandre Mergnat 	[MT8365_CLK_AUD_I2S0_M] = "audio_i2s0_m",
20ef307b40SAlexandre Mergnat 	[MT8365_CLK_AUD_I2S1_M] = "audio_i2s1_m",
21ef307b40SAlexandre Mergnat 	[MT8365_CLK_AUD_I2S2_M] = "audio_i2s2_m",
22ef307b40SAlexandre Mergnat 	[MT8365_CLK_AUD_I2S3_M] = "audio_i2s3_m",
23ef307b40SAlexandre Mergnat 	[MT8365_CLK_ENGEN1] = "engen1",
24ef307b40SAlexandre Mergnat 	[MT8365_CLK_ENGEN2] = "engen2",
25ef307b40SAlexandre Mergnat 	[MT8365_CLK_AUD1] = "aud1",
26ef307b40SAlexandre Mergnat 	[MT8365_CLK_AUD2] = "aud2",
27ef307b40SAlexandre Mergnat 	[MT8365_CLK_I2S0_M_SEL] = "i2s0_m_sel",
28ef307b40SAlexandre Mergnat 	[MT8365_CLK_I2S1_M_SEL] = "i2s1_m_sel",
29ef307b40SAlexandre Mergnat 	[MT8365_CLK_I2S2_M_SEL] = "i2s2_m_sel",
30ef307b40SAlexandre Mergnat 	[MT8365_CLK_I2S3_M_SEL] = "i2s3_m_sel",
31ef307b40SAlexandre Mergnat 	[MT8365_CLK_CLK26M] = "top_clk26m_clk",
32ef307b40SAlexandre Mergnat };
33ef307b40SAlexandre Mergnat 
34ef307b40SAlexandre Mergnat int mt8365_afe_init_audio_clk(struct mtk_base_afe *afe)
35ef307b40SAlexandre Mergnat {
36ef307b40SAlexandre Mergnat 	size_t i;
37ef307b40SAlexandre Mergnat 	struct mt8365_afe_private *afe_priv = afe->platform_priv;
38ef307b40SAlexandre Mergnat 
39ef307b40SAlexandre Mergnat 	for (i = 0; i < ARRAY_SIZE(aud_clks); i++) {
40ef307b40SAlexandre Mergnat 		afe_priv->clocks[i] = devm_clk_get(afe->dev, aud_clks[i]);
41ef307b40SAlexandre Mergnat 		if (IS_ERR(afe_priv->clocks[i])) {
42ef307b40SAlexandre Mergnat 			dev_err(afe->dev, "%s devm_clk_get %s fail\n",
43ef307b40SAlexandre Mergnat 				__func__, aud_clks[i]);
44ef307b40SAlexandre Mergnat 			return PTR_ERR(afe_priv->clocks[i]);
45ef307b40SAlexandre Mergnat 		}
46ef307b40SAlexandre Mergnat 	}
47ef307b40SAlexandre Mergnat 	return 0;
48ef307b40SAlexandre Mergnat }
49ef307b40SAlexandre Mergnat 
50ef307b40SAlexandre Mergnat void mt8365_afe_disable_clk(struct mtk_base_afe *afe, struct clk *clk)
51ef307b40SAlexandre Mergnat {
52ef307b40SAlexandre Mergnat 	if (clk)
53ef307b40SAlexandre Mergnat 		clk_disable_unprepare(clk);
54ef307b40SAlexandre Mergnat }
55ef307b40SAlexandre Mergnat 
56ef307b40SAlexandre Mergnat int mt8365_afe_set_clk_rate(struct mtk_base_afe *afe, struct clk *clk,
57ef307b40SAlexandre Mergnat 			    unsigned int rate)
58ef307b40SAlexandre Mergnat {
59ef307b40SAlexandre Mergnat 	int ret;
60ef307b40SAlexandre Mergnat 
61ef307b40SAlexandre Mergnat 	if (clk) {
62ef307b40SAlexandre Mergnat 		ret = clk_set_rate(clk, rate);
63ef307b40SAlexandre Mergnat 		if (ret) {
64ef307b40SAlexandre Mergnat 			dev_err(afe->dev, "Failed to set rate\n");
65ef307b40SAlexandre Mergnat 			return ret;
66ef307b40SAlexandre Mergnat 		}
67ef307b40SAlexandre Mergnat 	}
68ef307b40SAlexandre Mergnat 	return 0;
69ef307b40SAlexandre Mergnat }
70ef307b40SAlexandre Mergnat 
71ef307b40SAlexandre Mergnat int mt8365_afe_set_clk_parent(struct mtk_base_afe *afe, struct clk *clk,
72ef307b40SAlexandre Mergnat 			      struct clk *parent)
73ef307b40SAlexandre Mergnat {
74ef307b40SAlexandre Mergnat 	int ret;
75ef307b40SAlexandre Mergnat 
76ef307b40SAlexandre Mergnat 	if (clk && parent) {
77ef307b40SAlexandre Mergnat 		ret = clk_set_parent(clk, parent);
78ef307b40SAlexandre Mergnat 		if (ret) {
79ef307b40SAlexandre Mergnat 			dev_err(afe->dev, "Failed to set parent\n");
80ef307b40SAlexandre Mergnat 			return ret;
81ef307b40SAlexandre Mergnat 		}
82ef307b40SAlexandre Mergnat 	}
83ef307b40SAlexandre Mergnat 	return 0;
84ef307b40SAlexandre Mergnat }
85ef307b40SAlexandre Mergnat 
86ef307b40SAlexandre Mergnat static unsigned int get_top_cg_reg(unsigned int cg_type)
87ef307b40SAlexandre Mergnat {
88ef307b40SAlexandre Mergnat 	switch (cg_type) {
89ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_AFE:
90ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_I2S_IN:
91ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_22M:
92ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_24M:
93ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_INTDIR_CK:
94ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_APLL2_TUNER:
95ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_APLL_TUNER:
96ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_SPDIF:
97ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_TDM_OUT:
98ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_TDM_IN:
99ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_ADC:
100ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_DAC:
101ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_DAC_PREDIS:
102ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_TML:
103ef307b40SAlexandre Mergnat 		return AUDIO_TOP_CON0;
104ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_I2S1_BCLK:
105ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_I2S2_BCLK:
106ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_I2S3_BCLK:
107ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_I2S4_BCLK:
108ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_DMIC0_ADC:
109ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_DMIC1_ADC:
110ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_DMIC2_ADC:
111ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_DMIC3_ADC:
112ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_CONNSYS_I2S_ASRC:
113ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_GENERAL1_ASRC:
114ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_GENERAL2_ASRC:
115ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_TDM_ASRC:
116ef307b40SAlexandre Mergnat 		return AUDIO_TOP_CON1;
117ef307b40SAlexandre Mergnat 	default:
118ef307b40SAlexandre Mergnat 		return 0;
119ef307b40SAlexandre Mergnat 	}
120ef307b40SAlexandre Mergnat }
121ef307b40SAlexandre Mergnat 
122ef307b40SAlexandre Mergnat static unsigned int get_top_cg_mask(unsigned int cg_type)
123ef307b40SAlexandre Mergnat {
124ef307b40SAlexandre Mergnat 	switch (cg_type) {
125ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_AFE:
126ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_AFE;
127ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_I2S_IN:
128ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_I2S_IN;
129ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_22M:
130ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_22M;
131ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_24M:
132ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_24M;
133ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_INTDIR_CK:
134ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_INTDIR;
135ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_APLL2_TUNER:
136ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_APLL2_TUNER;
137ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_APLL_TUNER:
138ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_APLL_TUNER;
139ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_SPDIF:
140ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_SPDIF;
141ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_TDM_OUT:
142ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_TDM_OUT;
143ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_TDM_IN:
144ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_TDM_IN;
145ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_ADC:
146ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_ADC;
147ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_DAC:
148ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_DAC;
149ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_DAC_PREDIS:
150ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_DAC_PREDIS;
151ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_TML:
152ef307b40SAlexandre Mergnat 		return AUD_TCON0_PDN_TML;
153ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_I2S1_BCLK:
154ef307b40SAlexandre Mergnat 		return AUD_TCON1_PDN_I2S1_BCLK;
155ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_I2S2_BCLK:
156ef307b40SAlexandre Mergnat 		return AUD_TCON1_PDN_I2S2_BCLK;
157ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_I2S3_BCLK:
158ef307b40SAlexandre Mergnat 		return AUD_TCON1_PDN_I2S3_BCLK;
159ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_I2S4_BCLK:
160ef307b40SAlexandre Mergnat 		return AUD_TCON1_PDN_I2S4_BCLK;
161ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_DMIC0_ADC:
162ef307b40SAlexandre Mergnat 		return AUD_TCON1_PDN_DMIC0_ADC;
163ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_DMIC1_ADC:
164ef307b40SAlexandre Mergnat 		return AUD_TCON1_PDN_DMIC1_ADC;
165ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_DMIC2_ADC:
166ef307b40SAlexandre Mergnat 		return AUD_TCON1_PDN_DMIC2_ADC;
167ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_DMIC3_ADC:
168ef307b40SAlexandre Mergnat 		return AUD_TCON1_PDN_DMIC3_ADC;
169ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_CONNSYS_I2S_ASRC:
170ef307b40SAlexandre Mergnat 		return AUD_TCON1_PDN_CONNSYS_I2S_ASRC;
171ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_GENERAL1_ASRC:
172ef307b40SAlexandre Mergnat 		return AUD_TCON1_PDN_GENERAL1_ASRC;
173ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_GENERAL2_ASRC:
174ef307b40SAlexandre Mergnat 		return AUD_TCON1_PDN_GENERAL2_ASRC;
175ef307b40SAlexandre Mergnat 	case MT8365_TOP_CG_TDM_ASRC:
176ef307b40SAlexandre Mergnat 		return AUD_TCON1_PDN_TDM_ASRC;
177ef307b40SAlexandre Mergnat 	default:
178ef307b40SAlexandre Mergnat 		return 0;
179ef307b40SAlexandre Mergnat 	}
180ef307b40SAlexandre Mergnat }
181ef307b40SAlexandre Mergnat 
182ef307b40SAlexandre Mergnat static unsigned int get_top_cg_on_val(unsigned int cg_type)
183ef307b40SAlexandre Mergnat {
184ef307b40SAlexandre Mergnat 	return 0;
185ef307b40SAlexandre Mergnat }
186ef307b40SAlexandre Mergnat 
187ef307b40SAlexandre Mergnat static unsigned int get_top_cg_off_val(unsigned int cg_type)
188ef307b40SAlexandre Mergnat {
189ef307b40SAlexandre Mergnat 	return get_top_cg_mask(cg_type);
190ef307b40SAlexandre Mergnat }
191ef307b40SAlexandre Mergnat 
192ef307b40SAlexandre Mergnat int mt8365_afe_enable_top_cg(struct mtk_base_afe *afe, unsigned int cg_type)
193ef307b40SAlexandre Mergnat {
194ef307b40SAlexandre Mergnat 	struct mt8365_afe_private *afe_priv = afe->platform_priv;
195ef307b40SAlexandre Mergnat 	unsigned int reg = get_top_cg_reg(cg_type);
196ef307b40SAlexandre Mergnat 	unsigned int mask = get_top_cg_mask(cg_type);
197ef307b40SAlexandre Mergnat 	unsigned int val = get_top_cg_on_val(cg_type);
198ef307b40SAlexandre Mergnat 	unsigned long flags;
199ef307b40SAlexandre Mergnat 
200ef307b40SAlexandre Mergnat 	spin_lock_irqsave(&afe_priv->afe_ctrl_lock, flags);
201ef307b40SAlexandre Mergnat 
202ef307b40SAlexandre Mergnat 	afe_priv->top_cg_ref_cnt[cg_type]++;
203ef307b40SAlexandre Mergnat 	if (afe_priv->top_cg_ref_cnt[cg_type] == 1)
204ef307b40SAlexandre Mergnat 		regmap_update_bits(afe->regmap, reg, mask, val);
205ef307b40SAlexandre Mergnat 
206ef307b40SAlexandre Mergnat 	spin_unlock_irqrestore(&afe_priv->afe_ctrl_lock, flags);
207ef307b40SAlexandre Mergnat 
208ef307b40SAlexandre Mergnat 	return 0;
209ef307b40SAlexandre Mergnat }
210ef307b40SAlexandre Mergnat 
211ef307b40SAlexandre Mergnat int mt8365_afe_disable_top_cg(struct mtk_base_afe *afe, unsigned int cg_type)
212ef307b40SAlexandre Mergnat {
213ef307b40SAlexandre Mergnat 	struct mt8365_afe_private *afe_priv = afe->platform_priv;
214ef307b40SAlexandre Mergnat 	unsigned int reg = get_top_cg_reg(cg_type);
215ef307b40SAlexandre Mergnat 	unsigned int mask = get_top_cg_mask(cg_type);
216ef307b40SAlexandre Mergnat 	unsigned int val = get_top_cg_off_val(cg_type);
217ef307b40SAlexandre Mergnat 	unsigned long flags;
218ef307b40SAlexandre Mergnat 
219ef307b40SAlexandre Mergnat 	spin_lock_irqsave(&afe_priv->afe_ctrl_lock, flags);
220ef307b40SAlexandre Mergnat 
221ef307b40SAlexandre Mergnat 	afe_priv->top_cg_ref_cnt[cg_type]--;
222ef307b40SAlexandre Mergnat 	if (afe_priv->top_cg_ref_cnt[cg_type] == 0)
223ef307b40SAlexandre Mergnat 		regmap_update_bits(afe->regmap, reg, mask, val);
224ef307b40SAlexandre Mergnat 	else if (afe_priv->top_cg_ref_cnt[cg_type] < 0)
225ef307b40SAlexandre Mergnat 		afe_priv->top_cg_ref_cnt[cg_type] = 0;
226ef307b40SAlexandre Mergnat 
227ef307b40SAlexandre Mergnat 	spin_unlock_irqrestore(&afe_priv->afe_ctrl_lock, flags);
228ef307b40SAlexandre Mergnat 
229ef307b40SAlexandre Mergnat 	return 0;
230ef307b40SAlexandre Mergnat }
231ef307b40SAlexandre Mergnat 
232ef307b40SAlexandre Mergnat int mt8365_afe_enable_main_clk(struct mtk_base_afe *afe)
233ef307b40SAlexandre Mergnat {
234ef307b40SAlexandre Mergnat 	struct mt8365_afe_private *afe_priv = afe->platform_priv;
235ef307b40SAlexandre Mergnat 
236ef307b40SAlexandre Mergnat 	clk_prepare_enable(afe_priv->clocks[MT8365_CLK_TOP_AUD_SEL]);
237ef307b40SAlexandre Mergnat 	mt8365_afe_enable_top_cg(afe, MT8365_TOP_CG_AFE);
238ef307b40SAlexandre Mergnat 	mt8365_afe_enable_afe_on(afe);
239ef307b40SAlexandre Mergnat 
240ef307b40SAlexandre Mergnat 	return 0;
241ef307b40SAlexandre Mergnat }
242ef307b40SAlexandre Mergnat 
243ef307b40SAlexandre Mergnat int mt8365_afe_disable_main_clk(struct mtk_base_afe *afe)
244ef307b40SAlexandre Mergnat {
245ef307b40SAlexandre Mergnat 	struct mt8365_afe_private *afe_priv = afe->platform_priv;
246ef307b40SAlexandre Mergnat 
247ef307b40SAlexandre Mergnat 	mt8365_afe_disable_afe_on(afe);
248ef307b40SAlexandre Mergnat 	mt8365_afe_disable_top_cg(afe, MT8365_TOP_CG_AFE);
249ef307b40SAlexandre Mergnat 	mt8365_afe_disable_clk(afe, afe_priv->clocks[MT8365_CLK_TOP_AUD_SEL]);
250ef307b40SAlexandre Mergnat 
251ef307b40SAlexandre Mergnat 	return 0;
252ef307b40SAlexandre Mergnat }
253ef307b40SAlexandre Mergnat 
254ef307b40SAlexandre Mergnat int mt8365_afe_emi_clk_on(struct mtk_base_afe *afe)
255ef307b40SAlexandre Mergnat {
256ef307b40SAlexandre Mergnat 	return 0;
257ef307b40SAlexandre Mergnat }
258ef307b40SAlexandre Mergnat 
259ef307b40SAlexandre Mergnat int mt8365_afe_emi_clk_off(struct mtk_base_afe *afe)
260ef307b40SAlexandre Mergnat {
261ef307b40SAlexandre Mergnat 	return 0;
262ef307b40SAlexandre Mergnat }
263ef307b40SAlexandre Mergnat 
264ef307b40SAlexandre Mergnat int mt8365_afe_enable_afe_on(struct mtk_base_afe *afe)
265ef307b40SAlexandre Mergnat {
266ef307b40SAlexandre Mergnat 	struct mt8365_afe_private *afe_priv = afe->platform_priv;
267ef307b40SAlexandre Mergnat 	unsigned long flags;
268ef307b40SAlexandre Mergnat 
269ef307b40SAlexandre Mergnat 	spin_lock_irqsave(&afe_priv->afe_ctrl_lock, flags);
270ef307b40SAlexandre Mergnat 
271ef307b40SAlexandre Mergnat 	afe_priv->afe_on_ref_cnt++;
272ef307b40SAlexandre Mergnat 	if (afe_priv->afe_on_ref_cnt == 1)
273ef307b40SAlexandre Mergnat 		regmap_update_bits(afe->regmap, AFE_DAC_CON0, 0x1, 0x1);
274ef307b40SAlexandre Mergnat 
275ef307b40SAlexandre Mergnat 	spin_unlock_irqrestore(&afe_priv->afe_ctrl_lock, flags);
276ef307b40SAlexandre Mergnat 
277ef307b40SAlexandre Mergnat 	return 0;
278ef307b40SAlexandre Mergnat }
279ef307b40SAlexandre Mergnat 
280ef307b40SAlexandre Mergnat int mt8365_afe_disable_afe_on(struct mtk_base_afe *afe)
281ef307b40SAlexandre Mergnat {
282ef307b40SAlexandre Mergnat 	struct mt8365_afe_private *afe_priv = afe->platform_priv;
283ef307b40SAlexandre Mergnat 	unsigned long flags;
284ef307b40SAlexandre Mergnat 
285ef307b40SAlexandre Mergnat 	spin_lock_irqsave(&afe_priv->afe_ctrl_lock, flags);
286ef307b40SAlexandre Mergnat 
287ef307b40SAlexandre Mergnat 	afe_priv->afe_on_ref_cnt--;
288ef307b40SAlexandre Mergnat 	if (afe_priv->afe_on_ref_cnt == 0)
289ef307b40SAlexandre Mergnat 		regmap_update_bits(afe->regmap, AFE_DAC_CON0, 0x1, 0x0);
290ef307b40SAlexandre Mergnat 	else if (afe_priv->afe_on_ref_cnt < 0)
291ef307b40SAlexandre Mergnat 		afe_priv->afe_on_ref_cnt = 0;
292ef307b40SAlexandre Mergnat 
293ef307b40SAlexandre Mergnat 	spin_unlock_irqrestore(&afe_priv->afe_ctrl_lock, flags);
294ef307b40SAlexandre Mergnat 
295ef307b40SAlexandre Mergnat 	return 0;
296ef307b40SAlexandre Mergnat }
297ef307b40SAlexandre Mergnat 
298*63157d99SMark Brown static int mt8365_afe_hd_engen_enable(struct mtk_base_afe *afe, bool apll1)
299ef307b40SAlexandre Mergnat {
300ef307b40SAlexandre Mergnat 	if (apll1)
301ef307b40SAlexandre Mergnat 		regmap_update_bits(afe->regmap, AFE_HD_ENGEN_ENABLE,
302ef307b40SAlexandre Mergnat 				   AFE_22M_PLL_EN, AFE_22M_PLL_EN);
303ef307b40SAlexandre Mergnat 	else
304ef307b40SAlexandre Mergnat 		regmap_update_bits(afe->regmap, AFE_HD_ENGEN_ENABLE,
305ef307b40SAlexandre Mergnat 				   AFE_24M_PLL_EN, AFE_24M_PLL_EN);
306ef307b40SAlexandre Mergnat 
307ef307b40SAlexandre Mergnat 	return 0;
308ef307b40SAlexandre Mergnat }
309ef307b40SAlexandre Mergnat 
310*63157d99SMark Brown static int mt8365_afe_hd_engen_disable(struct mtk_base_afe *afe, bool apll1)
311ef307b40SAlexandre Mergnat {
312ef307b40SAlexandre Mergnat 	if (apll1)
313ef307b40SAlexandre Mergnat 		regmap_update_bits(afe->regmap, AFE_HD_ENGEN_ENABLE,
314ef307b40SAlexandre Mergnat 				   AFE_22M_PLL_EN, ~AFE_22M_PLL_EN);
315ef307b40SAlexandre Mergnat 	else
316ef307b40SAlexandre Mergnat 		regmap_update_bits(afe->regmap, AFE_HD_ENGEN_ENABLE,
317ef307b40SAlexandre Mergnat 				   AFE_24M_PLL_EN, ~AFE_24M_PLL_EN);
318ef307b40SAlexandre Mergnat 
319ef307b40SAlexandre Mergnat 	return 0;
320ef307b40SAlexandre Mergnat }
321ef307b40SAlexandre Mergnat 
322ef307b40SAlexandre Mergnat int mt8365_afe_enable_apll_tuner_cfg(struct mtk_base_afe *afe, unsigned int apll)
323ef307b40SAlexandre Mergnat {
324ef307b40SAlexandre Mergnat 	struct mt8365_afe_private *afe_priv = afe->platform_priv;
325ef307b40SAlexandre Mergnat 
326ef307b40SAlexandre Mergnat 	mutex_lock(&afe_priv->afe_clk_mutex);
327ef307b40SAlexandre Mergnat 
328ef307b40SAlexandre Mergnat 	afe_priv->apll_tuner_ref_cnt[apll]++;
329ef307b40SAlexandre Mergnat 	if (afe_priv->apll_tuner_ref_cnt[apll] != 1) {
330ef307b40SAlexandre Mergnat 		mutex_unlock(&afe_priv->afe_clk_mutex);
331ef307b40SAlexandre Mergnat 		return 0;
332ef307b40SAlexandre Mergnat 	}
333ef307b40SAlexandre Mergnat 
334ef307b40SAlexandre Mergnat 	if (apll == MT8365_AFE_APLL1) {
335ef307b40SAlexandre Mergnat 		regmap_update_bits(afe->regmap, AFE_APLL_TUNER_CFG,
336ef307b40SAlexandre Mergnat 				   AFE_APLL_TUNER_CFG_MASK, 0x432);
337ef307b40SAlexandre Mergnat 		regmap_update_bits(afe->regmap, AFE_APLL_TUNER_CFG,
338ef307b40SAlexandre Mergnat 				   AFE_APLL_TUNER_CFG_EN_MASK, 0x1);
339ef307b40SAlexandre Mergnat 	} else {
340ef307b40SAlexandre Mergnat 		regmap_update_bits(afe->regmap, AFE_APLL_TUNER_CFG1,
341ef307b40SAlexandre Mergnat 				   AFE_APLL_TUNER_CFG1_MASK, 0x434);
342ef307b40SAlexandre Mergnat 		regmap_update_bits(afe->regmap, AFE_APLL_TUNER_CFG1,
343ef307b40SAlexandre Mergnat 				   AFE_APLL_TUNER_CFG1_EN_MASK, 0x1);
344ef307b40SAlexandre Mergnat 	}
345ef307b40SAlexandre Mergnat 
346ef307b40SAlexandre Mergnat 	mutex_unlock(&afe_priv->afe_clk_mutex);
347ef307b40SAlexandre Mergnat 	return 0;
348ef307b40SAlexandre Mergnat }
349ef307b40SAlexandre Mergnat 
350ef307b40SAlexandre Mergnat int mt8365_afe_disable_apll_tuner_cfg(struct mtk_base_afe *afe, unsigned int apll)
351ef307b40SAlexandre Mergnat {
352ef307b40SAlexandre Mergnat 	struct mt8365_afe_private *afe_priv = afe->platform_priv;
353ef307b40SAlexandre Mergnat 
354ef307b40SAlexandre Mergnat 	mutex_lock(&afe_priv->afe_clk_mutex);
355ef307b40SAlexandre Mergnat 
356ef307b40SAlexandre Mergnat 	afe_priv->apll_tuner_ref_cnt[apll]--;
357ef307b40SAlexandre Mergnat 	if (afe_priv->apll_tuner_ref_cnt[apll] == 0) {
358ef307b40SAlexandre Mergnat 		if (apll == MT8365_AFE_APLL1)
359ef307b40SAlexandre Mergnat 			regmap_update_bits(afe->regmap, AFE_APLL_TUNER_CFG,
360ef307b40SAlexandre Mergnat 					   AFE_APLL_TUNER_CFG_EN_MASK, 0x0);
361ef307b40SAlexandre Mergnat 		else
362ef307b40SAlexandre Mergnat 			regmap_update_bits(afe->regmap, AFE_APLL_TUNER_CFG1,
363ef307b40SAlexandre Mergnat 					   AFE_APLL_TUNER_CFG1_EN_MASK, 0x0);
364ef307b40SAlexandre Mergnat 
365ef307b40SAlexandre Mergnat 	} else if (afe_priv->apll_tuner_ref_cnt[apll] < 0) {
366ef307b40SAlexandre Mergnat 		afe_priv->apll_tuner_ref_cnt[apll] = 0;
367ef307b40SAlexandre Mergnat 	}
368ef307b40SAlexandre Mergnat 
369ef307b40SAlexandre Mergnat 	mutex_unlock(&afe_priv->afe_clk_mutex);
370ef307b40SAlexandre Mergnat 	return 0;
371ef307b40SAlexandre Mergnat }
372ef307b40SAlexandre Mergnat 
373ef307b40SAlexandre Mergnat int mt8365_afe_enable_apll_associated_cfg(struct mtk_base_afe *afe, unsigned int apll)
374ef307b40SAlexandre Mergnat {
375ef307b40SAlexandre Mergnat 	struct mt8365_afe_private *afe_priv = afe->platform_priv;
376ef307b40SAlexandre Mergnat 
377ef307b40SAlexandre Mergnat 	if (apll == MT8365_AFE_APLL1) {
378ef307b40SAlexandre Mergnat 		if (clk_prepare_enable(afe_priv->clocks[MT8365_CLK_ENGEN1])) {
379ef307b40SAlexandre Mergnat 			dev_info(afe->dev, "%s Failed to enable ENGEN1 clk\n",
380ef307b40SAlexandre Mergnat 				 __func__);
381ef307b40SAlexandre Mergnat 			return 0;
382ef307b40SAlexandre Mergnat 		}
383ef307b40SAlexandre Mergnat 		mt8365_afe_enable_top_cg(afe, MT8365_TOP_CG_22M);
384ef307b40SAlexandre Mergnat 		mt8365_afe_hd_engen_enable(afe, true);
385ef307b40SAlexandre Mergnat 		mt8365_afe_enable_top_cg(afe, MT8365_TOP_CG_APLL_TUNER);
386ef307b40SAlexandre Mergnat 		mt8365_afe_enable_apll_tuner_cfg(afe, MT8365_AFE_APLL1);
387ef307b40SAlexandre Mergnat 	} else {
388ef307b40SAlexandre Mergnat 		if (clk_prepare_enable(afe_priv->clocks[MT8365_CLK_ENGEN2])) {
389ef307b40SAlexandre Mergnat 			dev_info(afe->dev, "%s Failed to enable ENGEN2 clk\n",
390ef307b40SAlexandre Mergnat 				 __func__);
391ef307b40SAlexandre Mergnat 			return 0;
392ef307b40SAlexandre Mergnat 		}
393ef307b40SAlexandre Mergnat 		mt8365_afe_enable_top_cg(afe, MT8365_TOP_CG_24M);
394ef307b40SAlexandre Mergnat 		mt8365_afe_hd_engen_enable(afe, false);
395ef307b40SAlexandre Mergnat 		mt8365_afe_enable_top_cg(afe, MT8365_TOP_CG_APLL2_TUNER);
396ef307b40SAlexandre Mergnat 		mt8365_afe_enable_apll_tuner_cfg(afe, MT8365_AFE_APLL2);
397ef307b40SAlexandre Mergnat 	}
398ef307b40SAlexandre Mergnat 
399ef307b40SAlexandre Mergnat 	return 0;
400ef307b40SAlexandre Mergnat }
401ef307b40SAlexandre Mergnat 
402ef307b40SAlexandre Mergnat int mt8365_afe_disable_apll_associated_cfg(struct mtk_base_afe *afe, unsigned int apll)
403ef307b40SAlexandre Mergnat {
404ef307b40SAlexandre Mergnat 	struct mt8365_afe_private *afe_priv = afe->platform_priv;
405ef307b40SAlexandre Mergnat 
406ef307b40SAlexandre Mergnat 	if (apll == MT8365_AFE_APLL1) {
407ef307b40SAlexandre Mergnat 		mt8365_afe_disable_apll_tuner_cfg(afe, MT8365_AFE_APLL1);
408ef307b40SAlexandre Mergnat 		mt8365_afe_disable_top_cg(afe, MT8365_TOP_CG_APLL_TUNER);
409ef307b40SAlexandre Mergnat 		mt8365_afe_hd_engen_disable(afe, true);
410ef307b40SAlexandre Mergnat 		mt8365_afe_disable_top_cg(afe, MT8365_TOP_CG_22M);
411ef307b40SAlexandre Mergnat 		clk_disable_unprepare(afe_priv->clocks[MT8365_CLK_ENGEN1]);
412ef307b40SAlexandre Mergnat 	} else {
413ef307b40SAlexandre Mergnat 		mt8365_afe_disable_apll_tuner_cfg(afe, MT8365_AFE_APLL2);
414ef307b40SAlexandre Mergnat 		mt8365_afe_disable_top_cg(afe, MT8365_TOP_CG_APLL2_TUNER);
415ef307b40SAlexandre Mergnat 		mt8365_afe_hd_engen_disable(afe, false);
416ef307b40SAlexandre Mergnat 		mt8365_afe_disable_top_cg(afe, MT8365_TOP_CG_24M);
417ef307b40SAlexandre Mergnat 		clk_disable_unprepare(afe_priv->clocks[MT8365_CLK_ENGEN2]);
418ef307b40SAlexandre Mergnat 	}
419ef307b40SAlexandre Mergnat 
420ef307b40SAlexandre Mergnat 	return 0;
421ef307b40SAlexandre Mergnat }
422