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