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