Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2015, 2018, The Linux Foundation. All rights reserved.
0004  * Copyright (c) 2021, Qualcomm Innovation Center, Inc. All rights reserved.
0005  */
0006 
0007 #include <linux/kernel.h>
0008 #include <linux/export.h>
0009 #include <linux/clk-provider.h>
0010 #include <linux/regmap.h>
0011 #include <linux/delay.h>
0012 
0013 #include "clk-alpha-pll.h"
0014 #include "common.h"
0015 
0016 #define PLL_MODE(p)     ((p)->offset + 0x0)
0017 # define PLL_OUTCTRL        BIT(0)
0018 # define PLL_BYPASSNL       BIT(1)
0019 # define PLL_RESET_N        BIT(2)
0020 # define PLL_OFFLINE_REQ    BIT(7)
0021 # define PLL_LOCK_COUNT_SHIFT   8
0022 # define PLL_LOCK_COUNT_MASK    0x3f
0023 # define PLL_BIAS_COUNT_SHIFT   14
0024 # define PLL_BIAS_COUNT_MASK    0x3f
0025 # define PLL_VOTE_FSM_ENA   BIT(20)
0026 # define PLL_FSM_ENA        BIT(20)
0027 # define PLL_VOTE_FSM_RESET BIT(21)
0028 # define PLL_UPDATE     BIT(22)
0029 # define PLL_UPDATE_BYPASS  BIT(23)
0030 # define PLL_OFFLINE_ACK    BIT(28)
0031 # define ALPHA_PLL_ACK_LATCH    BIT(29)
0032 # define PLL_ACTIVE_FLAG    BIT(30)
0033 # define PLL_LOCK_DET       BIT(31)
0034 
0035 #define PLL_L_VAL(p)        ((p)->offset + (p)->regs[PLL_OFF_L_VAL])
0036 #define PLL_CAL_L_VAL(p)    ((p)->offset + (p)->regs[PLL_OFF_CAL_L_VAL])
0037 #define PLL_ALPHA_VAL(p)    ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL])
0038 #define PLL_ALPHA_VAL_U(p)  ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL_U])
0039 
0040 #define PLL_USER_CTL(p)     ((p)->offset + (p)->regs[PLL_OFF_USER_CTL])
0041 # define PLL_POST_DIV_SHIFT 8
0042 # define PLL_POST_DIV_MASK(p)   GENMASK((p)->width, 0)
0043 # define PLL_ALPHA_EN       BIT(24)
0044 # define PLL_ALPHA_MODE     BIT(25)
0045 # define PLL_VCO_SHIFT      20
0046 # define PLL_VCO_MASK       0x3
0047 
0048 #define PLL_USER_CTL_U(p)   ((p)->offset + (p)->regs[PLL_OFF_USER_CTL_U])
0049 #define PLL_USER_CTL_U1(p)  ((p)->offset + (p)->regs[PLL_OFF_USER_CTL_U1])
0050 
0051 #define PLL_CONFIG_CTL(p)   ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL])
0052 #define PLL_CONFIG_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL_U])
0053 #define PLL_CONFIG_CTL_U1(p)    ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL_U1])
0054 #define PLL_TEST_CTL(p)     ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL])
0055 #define PLL_TEST_CTL_U(p)   ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL_U])
0056 #define PLL_TEST_CTL_U1(p)     ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL_U1])
0057 #define PLL_STATUS(p)       ((p)->offset + (p)->regs[PLL_OFF_STATUS])
0058 #define PLL_OPMODE(p)       ((p)->offset + (p)->regs[PLL_OFF_OPMODE])
0059 #define PLL_FRAC(p)     ((p)->offset + (p)->regs[PLL_OFF_FRAC])
0060 
0061 const u8 clk_alpha_pll_regs[][PLL_OFF_MAX_REGS] = {
0062     [CLK_ALPHA_PLL_TYPE_DEFAULT] =  {
0063         [PLL_OFF_L_VAL] = 0x04,
0064         [PLL_OFF_ALPHA_VAL] = 0x08,
0065         [PLL_OFF_ALPHA_VAL_U] = 0x0c,
0066         [PLL_OFF_USER_CTL] = 0x10,
0067         [PLL_OFF_USER_CTL_U] = 0x14,
0068         [PLL_OFF_CONFIG_CTL] = 0x18,
0069         [PLL_OFF_TEST_CTL] = 0x1c,
0070         [PLL_OFF_TEST_CTL_U] = 0x20,
0071         [PLL_OFF_STATUS] = 0x24,
0072     },
0073     [CLK_ALPHA_PLL_TYPE_HUAYRA] =  {
0074         [PLL_OFF_L_VAL] = 0x04,
0075         [PLL_OFF_ALPHA_VAL] = 0x08,
0076         [PLL_OFF_USER_CTL] = 0x10,
0077         [PLL_OFF_CONFIG_CTL] = 0x14,
0078         [PLL_OFF_CONFIG_CTL_U] = 0x18,
0079         [PLL_OFF_TEST_CTL] = 0x1c,
0080         [PLL_OFF_TEST_CTL_U] = 0x20,
0081         [PLL_OFF_STATUS] = 0x24,
0082     },
0083     [CLK_ALPHA_PLL_TYPE_BRAMMO] =  {
0084         [PLL_OFF_L_VAL] = 0x04,
0085         [PLL_OFF_ALPHA_VAL] = 0x08,
0086         [PLL_OFF_ALPHA_VAL_U] = 0x0c,
0087         [PLL_OFF_USER_CTL] = 0x10,
0088         [PLL_OFF_CONFIG_CTL] = 0x18,
0089         [PLL_OFF_TEST_CTL] = 0x1c,
0090         [PLL_OFF_STATUS] = 0x24,
0091     },
0092     [CLK_ALPHA_PLL_TYPE_FABIA] =  {
0093         [PLL_OFF_L_VAL] = 0x04,
0094         [PLL_OFF_USER_CTL] = 0x0c,
0095         [PLL_OFF_USER_CTL_U] = 0x10,
0096         [PLL_OFF_CONFIG_CTL] = 0x14,
0097         [PLL_OFF_CONFIG_CTL_U] = 0x18,
0098         [PLL_OFF_TEST_CTL] = 0x1c,
0099         [PLL_OFF_TEST_CTL_U] = 0x20,
0100         [PLL_OFF_STATUS] = 0x24,
0101         [PLL_OFF_OPMODE] = 0x2c,
0102         [PLL_OFF_FRAC] = 0x38,
0103     },
0104     [CLK_ALPHA_PLL_TYPE_TRION] = {
0105         [PLL_OFF_L_VAL] = 0x04,
0106         [PLL_OFF_CAL_L_VAL] = 0x08,
0107         [PLL_OFF_USER_CTL] = 0x0c,
0108         [PLL_OFF_USER_CTL_U] = 0x10,
0109         [PLL_OFF_USER_CTL_U1] = 0x14,
0110         [PLL_OFF_CONFIG_CTL] = 0x18,
0111         [PLL_OFF_CONFIG_CTL_U] = 0x1c,
0112         [PLL_OFF_CONFIG_CTL_U1] = 0x20,
0113         [PLL_OFF_TEST_CTL] = 0x24,
0114         [PLL_OFF_TEST_CTL_U] = 0x28,
0115         [PLL_OFF_TEST_CTL_U1] = 0x2c,
0116         [PLL_OFF_STATUS] = 0x30,
0117         [PLL_OFF_OPMODE] = 0x38,
0118         [PLL_OFF_ALPHA_VAL] = 0x40,
0119     },
0120     [CLK_ALPHA_PLL_TYPE_AGERA] =  {
0121         [PLL_OFF_L_VAL] = 0x04,
0122         [PLL_OFF_ALPHA_VAL] = 0x08,
0123         [PLL_OFF_USER_CTL] = 0x0c,
0124         [PLL_OFF_CONFIG_CTL] = 0x10,
0125         [PLL_OFF_CONFIG_CTL_U] = 0x14,
0126         [PLL_OFF_TEST_CTL] = 0x18,
0127         [PLL_OFF_TEST_CTL_U] = 0x1c,
0128         [PLL_OFF_STATUS] = 0x2c,
0129     },
0130     [CLK_ALPHA_PLL_TYPE_ZONDA] =  {
0131         [PLL_OFF_L_VAL] = 0x04,
0132         [PLL_OFF_ALPHA_VAL] = 0x08,
0133         [PLL_OFF_USER_CTL] = 0x0c,
0134         [PLL_OFF_CONFIG_CTL] = 0x10,
0135         [PLL_OFF_CONFIG_CTL_U] = 0x14,
0136         [PLL_OFF_CONFIG_CTL_U1] = 0x18,
0137         [PLL_OFF_TEST_CTL] = 0x1c,
0138         [PLL_OFF_TEST_CTL_U] = 0x20,
0139         [PLL_OFF_TEST_CTL_U1] = 0x24,
0140         [PLL_OFF_OPMODE] = 0x28,
0141         [PLL_OFF_STATUS] = 0x38,
0142     },
0143     [CLK_ALPHA_PLL_TYPE_LUCID_EVO] = {
0144         [PLL_OFF_OPMODE] = 0x04,
0145         [PLL_OFF_STATUS] = 0x0c,
0146         [PLL_OFF_L_VAL] = 0x10,
0147         [PLL_OFF_ALPHA_VAL] = 0x14,
0148         [PLL_OFF_USER_CTL] = 0x18,
0149         [PLL_OFF_USER_CTL_U] = 0x1c,
0150         [PLL_OFF_CONFIG_CTL] = 0x20,
0151         [PLL_OFF_CONFIG_CTL_U] = 0x24,
0152         [PLL_OFF_CONFIG_CTL_U1] = 0x28,
0153         [PLL_OFF_TEST_CTL] = 0x2c,
0154         [PLL_OFF_TEST_CTL_U] = 0x30,
0155         [PLL_OFF_TEST_CTL_U1] = 0x34,
0156     },
0157     [CLK_ALPHA_PLL_TYPE_RIVIAN_EVO] = {
0158         [PLL_OFF_OPMODE] = 0x04,
0159         [PLL_OFF_STATUS] = 0x0c,
0160         [PLL_OFF_L_VAL] = 0x10,
0161         [PLL_OFF_USER_CTL] = 0x14,
0162         [PLL_OFF_USER_CTL_U] = 0x18,
0163         [PLL_OFF_CONFIG_CTL] = 0x1c,
0164         [PLL_OFF_CONFIG_CTL_U] = 0x20,
0165         [PLL_OFF_CONFIG_CTL_U1] = 0x24,
0166         [PLL_OFF_TEST_CTL] = 0x28,
0167         [PLL_OFF_TEST_CTL_U] = 0x2c,
0168     },
0169 };
0170 EXPORT_SYMBOL_GPL(clk_alpha_pll_regs);
0171 
0172 /*
0173  * Even though 40 bits are present, use only 32 for ease of calculation.
0174  */
0175 #define ALPHA_REG_BITWIDTH  40
0176 #define ALPHA_REG_16BIT_WIDTH   16
0177 #define ALPHA_BITWIDTH      32U
0178 #define ALPHA_SHIFT(w)      min(w, ALPHA_BITWIDTH)
0179 
0180 #define PLL_HUAYRA_M_WIDTH      8
0181 #define PLL_HUAYRA_M_SHIFT      8
0182 #define PLL_HUAYRA_M_MASK       0xff
0183 #define PLL_HUAYRA_N_SHIFT      0
0184 #define PLL_HUAYRA_N_MASK       0xff
0185 #define PLL_HUAYRA_ALPHA_WIDTH      16
0186 
0187 #define PLL_STANDBY     0x0
0188 #define PLL_RUN         0x1
0189 #define PLL_OUT_MASK        0x7
0190 #define PLL_RATE_MARGIN     500
0191 
0192 /* TRION PLL specific settings and offsets */
0193 #define TRION_PLL_CAL_VAL   0x44
0194 #define TRION_PCAL_DONE     BIT(26)
0195 
0196 /* LUCID PLL specific settings and offsets */
0197 #define LUCID_PCAL_DONE     BIT(27)
0198 
0199 /* LUCID 5LPE PLL specific settings and offsets */
0200 #define LUCID_5LPE_PCAL_DONE        BIT(11)
0201 #define LUCID_5LPE_ALPHA_PLL_ACK_LATCH  BIT(13)
0202 #define LUCID_5LPE_PLL_LATCH_INPUT  BIT(14)
0203 #define LUCID_5LPE_ENABLE_VOTE_RUN  BIT(21)
0204 
0205 /* LUCID EVO PLL specific settings and offsets */
0206 #define LUCID_EVO_PCAL_NOT_DONE     BIT(8)
0207 #define LUCID_EVO_ENABLE_VOTE_RUN       BIT(25)
0208 #define LUCID_EVO_PLL_L_VAL_MASK        GENMASK(15, 0)
0209 #define LUCID_EVO_PLL_CAL_L_VAL_SHIFT   16
0210 
0211 /* ZONDA PLL specific */
0212 #define ZONDA_PLL_OUT_MASK  0xf
0213 #define ZONDA_STAY_IN_CFA   BIT(16)
0214 #define ZONDA_PLL_FREQ_LOCK_DET BIT(29)
0215 
0216 #define pll_alpha_width(p)                  \
0217         ((PLL_ALPHA_VAL_U(p) - PLL_ALPHA_VAL(p) == 4) ? \
0218                  ALPHA_REG_BITWIDTH : ALPHA_REG_16BIT_WIDTH)
0219 
0220 #define pll_has_64bit_config(p) ((PLL_CONFIG_CTL_U(p) - PLL_CONFIG_CTL(p)) == 4)
0221 
0222 #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
0223                        struct clk_alpha_pll, clkr)
0224 
0225 #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
0226                        struct clk_alpha_pll_postdiv, clkr)
0227 
0228 static int wait_for_pll(struct clk_alpha_pll *pll, u32 mask, bool inverse,
0229             const char *action)
0230 {
0231     u32 val;
0232     int count;
0233     int ret;
0234     const char *name = clk_hw_get_name(&pll->clkr.hw);
0235 
0236     ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
0237     if (ret)
0238         return ret;
0239 
0240     for (count = 200; count > 0; count--) {
0241         ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
0242         if (ret)
0243             return ret;
0244         if (inverse && !(val & mask))
0245             return 0;
0246         else if ((val & mask) == mask)
0247             return 0;
0248 
0249         udelay(1);
0250     }
0251 
0252     WARN(1, "%s failed to %s!\n", name, action);
0253     return -ETIMEDOUT;
0254 }
0255 
0256 #define wait_for_pll_enable_active(pll) \
0257     wait_for_pll(pll, PLL_ACTIVE_FLAG, 0, "enable")
0258 
0259 #define wait_for_pll_enable_lock(pll) \
0260     wait_for_pll(pll, PLL_LOCK_DET, 0, "enable")
0261 
0262 #define wait_for_zonda_pll_freq_lock(pll) \
0263     wait_for_pll(pll, ZONDA_PLL_FREQ_LOCK_DET, 0, "freq enable")
0264 
0265 #define wait_for_pll_disable(pll) \
0266     wait_for_pll(pll, PLL_ACTIVE_FLAG, 1, "disable")
0267 
0268 #define wait_for_pll_offline(pll) \
0269     wait_for_pll(pll, PLL_OFFLINE_ACK, 0, "offline")
0270 
0271 #define wait_for_pll_update(pll) \
0272     wait_for_pll(pll, PLL_UPDATE, 1, "update")
0273 
0274 #define wait_for_pll_update_ack_set(pll) \
0275     wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 0, "update_ack_set")
0276 
0277 #define wait_for_pll_update_ack_clear(pll) \
0278     wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 1, "update_ack_clear")
0279 
0280 static void clk_alpha_pll_write_config(struct regmap *regmap, unsigned int reg,
0281                     unsigned int val)
0282 {
0283     if (val)
0284         regmap_write(regmap, reg, val);
0285 }
0286 
0287 void clk_alpha_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
0288                  const struct alpha_pll_config *config)
0289 {
0290     u32 val, mask;
0291 
0292     regmap_write(regmap, PLL_L_VAL(pll), config->l);
0293     regmap_write(regmap, PLL_ALPHA_VAL(pll), config->alpha);
0294     regmap_write(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
0295 
0296     if (pll_has_64bit_config(pll))
0297         regmap_write(regmap, PLL_CONFIG_CTL_U(pll),
0298                  config->config_ctl_hi_val);
0299 
0300     if (pll_alpha_width(pll) > 32)
0301         regmap_write(regmap, PLL_ALPHA_VAL_U(pll), config->alpha_hi);
0302 
0303     val = config->main_output_mask;
0304     val |= config->aux_output_mask;
0305     val |= config->aux2_output_mask;
0306     val |= config->early_output_mask;
0307     val |= config->pre_div_val;
0308     val |= config->post_div_val;
0309     val |= config->vco_val;
0310     val |= config->alpha_en_mask;
0311     val |= config->alpha_mode_mask;
0312 
0313     mask = config->main_output_mask;
0314     mask |= config->aux_output_mask;
0315     mask |= config->aux2_output_mask;
0316     mask |= config->early_output_mask;
0317     mask |= config->pre_div_mask;
0318     mask |= config->post_div_mask;
0319     mask |= config->vco_mask;
0320 
0321     regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
0322 
0323     if (pll->flags & SUPPORTS_FSM_MODE)
0324         qcom_pll_set_fsm_mode(regmap, PLL_MODE(pll), 6, 0);
0325 }
0326 EXPORT_SYMBOL_GPL(clk_alpha_pll_configure);
0327 
0328 static int clk_alpha_pll_hwfsm_enable(struct clk_hw *hw)
0329 {
0330     int ret;
0331     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0332     u32 val;
0333 
0334     ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
0335     if (ret)
0336         return ret;
0337 
0338     val |= PLL_FSM_ENA;
0339 
0340     if (pll->flags & SUPPORTS_OFFLINE_REQ)
0341         val &= ~PLL_OFFLINE_REQ;
0342 
0343     ret = regmap_write(pll->clkr.regmap, PLL_MODE(pll), val);
0344     if (ret)
0345         return ret;
0346 
0347     /* Make sure enable request goes through before waiting for update */
0348     mb();
0349 
0350     return wait_for_pll_enable_active(pll);
0351 }
0352 
0353 static void clk_alpha_pll_hwfsm_disable(struct clk_hw *hw)
0354 {
0355     int ret;
0356     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0357     u32 val;
0358 
0359     ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
0360     if (ret)
0361         return;
0362 
0363     if (pll->flags & SUPPORTS_OFFLINE_REQ) {
0364         ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
0365                      PLL_OFFLINE_REQ, PLL_OFFLINE_REQ);
0366         if (ret)
0367             return;
0368 
0369         ret = wait_for_pll_offline(pll);
0370         if (ret)
0371             return;
0372     }
0373 
0374     /* Disable hwfsm */
0375     ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
0376                  PLL_FSM_ENA, 0);
0377     if (ret)
0378         return;
0379 
0380     wait_for_pll_disable(pll);
0381 }
0382 
0383 static int pll_is_enabled(struct clk_hw *hw, u32 mask)
0384 {
0385     int ret;
0386     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0387     u32 val;
0388 
0389     ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
0390     if (ret)
0391         return ret;
0392 
0393     return !!(val & mask);
0394 }
0395 
0396 static int clk_alpha_pll_hwfsm_is_enabled(struct clk_hw *hw)
0397 {
0398     return pll_is_enabled(hw, PLL_ACTIVE_FLAG);
0399 }
0400 
0401 static int clk_alpha_pll_is_enabled(struct clk_hw *hw)
0402 {
0403     return pll_is_enabled(hw, PLL_LOCK_DET);
0404 }
0405 
0406 static int clk_alpha_pll_enable(struct clk_hw *hw)
0407 {
0408     int ret;
0409     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0410     u32 val, mask;
0411 
0412     mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
0413     ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
0414     if (ret)
0415         return ret;
0416 
0417     /* If in FSM mode, just vote for it */
0418     if (val & PLL_VOTE_FSM_ENA) {
0419         ret = clk_enable_regmap(hw);
0420         if (ret)
0421             return ret;
0422         return wait_for_pll_enable_active(pll);
0423     }
0424 
0425     /* Skip if already enabled */
0426     if ((val & mask) == mask)
0427         return 0;
0428 
0429     ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
0430                  PLL_BYPASSNL, PLL_BYPASSNL);
0431     if (ret)
0432         return ret;
0433 
0434     /*
0435      * H/W requires a 5us delay between disabling the bypass and
0436      * de-asserting the reset.
0437      */
0438     mb();
0439     udelay(5);
0440 
0441     ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
0442                  PLL_RESET_N, PLL_RESET_N);
0443     if (ret)
0444         return ret;
0445 
0446     ret = wait_for_pll_enable_lock(pll);
0447     if (ret)
0448         return ret;
0449 
0450     ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
0451                  PLL_OUTCTRL, PLL_OUTCTRL);
0452 
0453     /* Ensure that the write above goes through before returning. */
0454     mb();
0455     return ret;
0456 }
0457 
0458 static void clk_alpha_pll_disable(struct clk_hw *hw)
0459 {
0460     int ret;
0461     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0462     u32 val, mask;
0463 
0464     ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
0465     if (ret)
0466         return;
0467 
0468     /* If in FSM mode, just unvote it */
0469     if (val & PLL_VOTE_FSM_ENA) {
0470         clk_disable_regmap(hw);
0471         return;
0472     }
0473 
0474     mask = PLL_OUTCTRL;
0475     regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
0476 
0477     /* Delay of 2 output clock ticks required until output is disabled */
0478     mb();
0479     udelay(1);
0480 
0481     mask = PLL_RESET_N | PLL_BYPASSNL;
0482     regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
0483 }
0484 
0485 static unsigned long
0486 alpha_pll_calc_rate(u64 prate, u32 l, u32 a, u32 alpha_width)
0487 {
0488     return (prate * l) + ((prate * a) >> ALPHA_SHIFT(alpha_width));
0489 }
0490 
0491 static unsigned long
0492 alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a,
0493              u32 alpha_width)
0494 {
0495     u64 remainder;
0496     u64 quotient;
0497 
0498     quotient = rate;
0499     remainder = do_div(quotient, prate);
0500     *l = quotient;
0501 
0502     if (!remainder) {
0503         *a = 0;
0504         return rate;
0505     }
0506 
0507     /* Upper ALPHA_BITWIDTH bits of Alpha */
0508     quotient = remainder << ALPHA_SHIFT(alpha_width);
0509 
0510     remainder = do_div(quotient, prate);
0511 
0512     if (remainder)
0513         quotient++;
0514 
0515     *a = quotient;
0516     return alpha_pll_calc_rate(prate, *l, *a, alpha_width);
0517 }
0518 
0519 static const struct pll_vco *
0520 alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
0521 {
0522     const struct pll_vco *v = pll->vco_table;
0523     const struct pll_vco *end = v + pll->num_vco;
0524 
0525     for (; v < end; v++)
0526         if (rate >= v->min_freq && rate <= v->max_freq)
0527             return v;
0528 
0529     return NULL;
0530 }
0531 
0532 static unsigned long
0533 clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
0534 {
0535     u32 l, low, high, ctl;
0536     u64 a = 0, prate = parent_rate;
0537     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0538     u32 alpha_width = pll_alpha_width(pll);
0539 
0540     regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
0541 
0542     regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
0543     if (ctl & PLL_ALPHA_EN) {
0544         regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &low);
0545         if (alpha_width > 32) {
0546             regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll),
0547                     &high);
0548             a = (u64)high << 32 | low;
0549         } else {
0550             a = low & GENMASK(alpha_width - 1, 0);
0551         }
0552 
0553         if (alpha_width > ALPHA_BITWIDTH)
0554             a >>= alpha_width - ALPHA_BITWIDTH;
0555     }
0556 
0557     return alpha_pll_calc_rate(prate, l, a, alpha_width);
0558 }
0559 
0560 
0561 static int __clk_alpha_pll_update_latch(struct clk_alpha_pll *pll)
0562 {
0563     int ret;
0564     u32 mode;
0565 
0566     regmap_read(pll->clkr.regmap, PLL_MODE(pll), &mode);
0567 
0568     /* Latch the input to the PLL */
0569     regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE,
0570                PLL_UPDATE);
0571 
0572     /* Wait for 2 reference cycle before checking ACK bit */
0573     udelay(1);
0574 
0575     /*
0576      * PLL will latch the new L, Alpha and freq control word.
0577      * PLL will respond by raising PLL_ACK_LATCH output when new programming
0578      * has been latched in and PLL is being updated. When
0579      * UPDATE_LOGIC_BYPASS bit is not set, PLL_UPDATE will be cleared
0580      * automatically by hardware when PLL_ACK_LATCH is asserted by PLL.
0581      */
0582     if (mode & PLL_UPDATE_BYPASS) {
0583         ret = wait_for_pll_update_ack_set(pll);
0584         if (ret)
0585             return ret;
0586 
0587         regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE, 0);
0588     } else {
0589         ret = wait_for_pll_update(pll);
0590         if (ret)
0591             return ret;
0592     }
0593 
0594     ret = wait_for_pll_update_ack_clear(pll);
0595     if (ret)
0596         return ret;
0597 
0598     /* Wait for PLL output to stabilize */
0599     udelay(10);
0600 
0601     return 0;
0602 }
0603 
0604 static int clk_alpha_pll_update_latch(struct clk_alpha_pll *pll,
0605                       int (*is_enabled)(struct clk_hw *))
0606 {
0607     if (!is_enabled(&pll->clkr.hw) ||
0608         !(pll->flags & SUPPORTS_DYNAMIC_UPDATE))
0609         return 0;
0610 
0611     return __clk_alpha_pll_update_latch(pll);
0612 }
0613 
0614 static int __clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
0615                     unsigned long prate,
0616                     int (*is_enabled)(struct clk_hw *))
0617 {
0618     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0619     const struct pll_vco *vco;
0620     u32 l, alpha_width = pll_alpha_width(pll);
0621     u64 a;
0622 
0623     rate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
0624     vco = alpha_pll_find_vco(pll, rate);
0625     if (pll->vco_table && !vco) {
0626         pr_err("%s: alpha pll not in a valid vco range\n",
0627                clk_hw_get_name(hw));
0628         return -EINVAL;
0629     }
0630 
0631     regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
0632 
0633     if (alpha_width > ALPHA_BITWIDTH)
0634         a <<= alpha_width - ALPHA_BITWIDTH;
0635 
0636     if (alpha_width > 32)
0637         regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll), a >> 32);
0638 
0639     regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
0640 
0641     if (vco) {
0642         regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
0643                    PLL_VCO_MASK << PLL_VCO_SHIFT,
0644                    vco->val << PLL_VCO_SHIFT);
0645     }
0646 
0647     regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
0648                PLL_ALPHA_EN, PLL_ALPHA_EN);
0649 
0650     return clk_alpha_pll_update_latch(pll, is_enabled);
0651 }
0652 
0653 static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
0654                   unsigned long prate)
0655 {
0656     return __clk_alpha_pll_set_rate(hw, rate, prate,
0657                     clk_alpha_pll_is_enabled);
0658 }
0659 
0660 static int clk_alpha_pll_hwfsm_set_rate(struct clk_hw *hw, unsigned long rate,
0661                     unsigned long prate)
0662 {
0663     return __clk_alpha_pll_set_rate(hw, rate, prate,
0664                     clk_alpha_pll_hwfsm_is_enabled);
0665 }
0666 
0667 static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
0668                      unsigned long *prate)
0669 {
0670     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0671     u32 l, alpha_width = pll_alpha_width(pll);
0672     u64 a;
0673     unsigned long min_freq, max_freq;
0674 
0675     rate = alpha_pll_round_rate(rate, *prate, &l, &a, alpha_width);
0676     if (!pll->vco_table || alpha_pll_find_vco(pll, rate))
0677         return rate;
0678 
0679     min_freq = pll->vco_table[0].min_freq;
0680     max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
0681 
0682     return clamp(rate, min_freq, max_freq);
0683 }
0684 
0685 static unsigned long
0686 alpha_huayra_pll_calc_rate(u64 prate, u32 l, u32 a)
0687 {
0688     /*
0689      * a contains 16 bit alpha_val in two’s complement number in the range
0690      * of [-0.5, 0.5).
0691      */
0692     if (a >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
0693         l -= 1;
0694 
0695     return (prate * l) + (prate * a >> PLL_HUAYRA_ALPHA_WIDTH);
0696 }
0697 
0698 static unsigned long
0699 alpha_huayra_pll_round_rate(unsigned long rate, unsigned long prate,
0700                 u32 *l, u32 *a)
0701 {
0702     u64 remainder;
0703     u64 quotient;
0704 
0705     quotient = rate;
0706     remainder = do_div(quotient, prate);
0707     *l = quotient;
0708 
0709     if (!remainder) {
0710         *a = 0;
0711         return rate;
0712     }
0713 
0714     quotient = remainder << PLL_HUAYRA_ALPHA_WIDTH;
0715     remainder = do_div(quotient, prate);
0716 
0717     if (remainder)
0718         quotient++;
0719 
0720     /*
0721      * alpha_val should be in two’s complement number in the range
0722      * of [-0.5, 0.5) so if quotient >= 0.5 then increment the l value
0723      * since alpha value will be subtracted in this case.
0724      */
0725     if (quotient >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
0726         *l += 1;
0727 
0728     *a = quotient;
0729     return alpha_huayra_pll_calc_rate(prate, *l, *a);
0730 }
0731 
0732 static unsigned long
0733 alpha_pll_huayra_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
0734 {
0735     u64 rate = parent_rate, tmp;
0736     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0737     u32 l, alpha = 0, ctl, alpha_m, alpha_n;
0738 
0739     regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
0740     regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
0741 
0742     if (ctl & PLL_ALPHA_EN) {
0743         regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &alpha);
0744         /*
0745          * Depending upon alpha_mode, it can be treated as M/N value or
0746          * as a two’s complement number. When alpha_mode=1,
0747          * pll_alpha_val<15:8>=M and pll_apla_val<7:0>=N
0748          *
0749          *      Fout=FIN*(L+(M/N))
0750          *
0751          * M is a signed number (-128 to 127) and N is unsigned
0752          * (0 to 255). M/N has to be within +/-0.5.
0753          *
0754          * When alpha_mode=0, it is a two’s complement number in the
0755          * range [-0.5, 0.5).
0756          *
0757          *      Fout=FIN*(L+(alpha_val)/2^16)
0758          *
0759          * where alpha_val is two’s complement number.
0760          */
0761         if (!(ctl & PLL_ALPHA_MODE))
0762             return alpha_huayra_pll_calc_rate(rate, l, alpha);
0763 
0764         alpha_m = alpha >> PLL_HUAYRA_M_SHIFT & PLL_HUAYRA_M_MASK;
0765         alpha_n = alpha >> PLL_HUAYRA_N_SHIFT & PLL_HUAYRA_N_MASK;
0766 
0767         rate *= l;
0768         tmp = parent_rate;
0769         if (alpha_m >= BIT(PLL_HUAYRA_M_WIDTH - 1)) {
0770             alpha_m = BIT(PLL_HUAYRA_M_WIDTH) - alpha_m;
0771             tmp *= alpha_m;
0772             do_div(tmp, alpha_n);
0773             rate -= tmp;
0774         } else {
0775             tmp *= alpha_m;
0776             do_div(tmp, alpha_n);
0777             rate += tmp;
0778         }
0779 
0780         return rate;
0781     }
0782 
0783     return alpha_huayra_pll_calc_rate(rate, l, alpha);
0784 }
0785 
0786 static int alpha_pll_huayra_set_rate(struct clk_hw *hw, unsigned long rate,
0787                      unsigned long prate)
0788 {
0789     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0790     u32 l, a, ctl, cur_alpha = 0;
0791 
0792     rate = alpha_huayra_pll_round_rate(rate, prate, &l, &a);
0793 
0794     regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
0795 
0796     if (ctl & PLL_ALPHA_EN)
0797         regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &cur_alpha);
0798 
0799     /*
0800      * Huayra PLL supports PLL dynamic programming. User can change L_VAL,
0801      * without having to go through the power on sequence.
0802      */
0803     if (clk_alpha_pll_is_enabled(hw)) {
0804         if (cur_alpha != a) {
0805             pr_err("%s: clock needs to be gated\n",
0806                    clk_hw_get_name(hw));
0807             return -EBUSY;
0808         }
0809 
0810         regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
0811         /* Ensure that the write above goes to detect L val change. */
0812         mb();
0813         return wait_for_pll_enable_lock(pll);
0814     }
0815 
0816     regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
0817     regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
0818 
0819     if (a == 0)
0820         regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
0821                    PLL_ALPHA_EN, 0x0);
0822     else
0823         regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
0824                    PLL_ALPHA_EN | PLL_ALPHA_MODE, PLL_ALPHA_EN);
0825 
0826     return 0;
0827 }
0828 
0829 static long alpha_pll_huayra_round_rate(struct clk_hw *hw, unsigned long rate,
0830                     unsigned long *prate)
0831 {
0832     u32 l, a;
0833 
0834     return alpha_huayra_pll_round_rate(rate, *prate, &l, &a);
0835 }
0836 
0837 static int trion_pll_is_enabled(struct clk_alpha_pll *pll,
0838                 struct regmap *regmap)
0839 {
0840     u32 mode_val, opmode_val;
0841     int ret;
0842 
0843     ret = regmap_read(regmap, PLL_MODE(pll), &mode_val);
0844     ret |= regmap_read(regmap, PLL_OPMODE(pll), &opmode_val);
0845     if (ret)
0846         return 0;
0847 
0848     return ((opmode_val & PLL_RUN) && (mode_val & PLL_OUTCTRL));
0849 }
0850 
0851 static int clk_trion_pll_is_enabled(struct clk_hw *hw)
0852 {
0853     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0854 
0855     return trion_pll_is_enabled(pll, pll->clkr.regmap);
0856 }
0857 
0858 static int clk_trion_pll_enable(struct clk_hw *hw)
0859 {
0860     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0861     struct regmap *regmap = pll->clkr.regmap;
0862     u32 val;
0863     int ret;
0864 
0865     ret = regmap_read(regmap, PLL_MODE(pll), &val);
0866     if (ret)
0867         return ret;
0868 
0869     /* If in FSM mode, just vote for it */
0870     if (val & PLL_VOTE_FSM_ENA) {
0871         ret = clk_enable_regmap(hw);
0872         if (ret)
0873             return ret;
0874         return wait_for_pll_enable_active(pll);
0875     }
0876 
0877     /* Set operation mode to RUN */
0878     regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
0879 
0880     ret = wait_for_pll_enable_lock(pll);
0881     if (ret)
0882         return ret;
0883 
0884     /* Enable the PLL outputs */
0885     ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
0886                  PLL_OUT_MASK, PLL_OUT_MASK);
0887     if (ret)
0888         return ret;
0889 
0890     /* Enable the global PLL outputs */
0891     return regmap_update_bits(regmap, PLL_MODE(pll),
0892                  PLL_OUTCTRL, PLL_OUTCTRL);
0893 }
0894 
0895 static void clk_trion_pll_disable(struct clk_hw *hw)
0896 {
0897     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0898     struct regmap *regmap = pll->clkr.regmap;
0899     u32 val;
0900     int ret;
0901 
0902     ret = regmap_read(regmap, PLL_MODE(pll), &val);
0903     if (ret)
0904         return;
0905 
0906     /* If in FSM mode, just unvote it */
0907     if (val & PLL_VOTE_FSM_ENA) {
0908         clk_disable_regmap(hw);
0909         return;
0910     }
0911 
0912     /* Disable the global PLL output */
0913     ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
0914     if (ret)
0915         return;
0916 
0917     /* Disable the PLL outputs */
0918     ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
0919                  PLL_OUT_MASK, 0);
0920     if (ret)
0921         return;
0922 
0923     /* Place the PLL mode in STANDBY */
0924     regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
0925     regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
0926 }
0927 
0928 static unsigned long
0929 clk_trion_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
0930 {
0931     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
0932     u32 l, frac, alpha_width = pll_alpha_width(pll);
0933 
0934     regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
0935     regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &frac);
0936 
0937     return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
0938 }
0939 
0940 const struct clk_ops clk_alpha_pll_fixed_ops = {
0941     .enable = clk_alpha_pll_enable,
0942     .disable = clk_alpha_pll_disable,
0943     .is_enabled = clk_alpha_pll_is_enabled,
0944     .recalc_rate = clk_alpha_pll_recalc_rate,
0945 };
0946 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_ops);
0947 
0948 const struct clk_ops clk_alpha_pll_ops = {
0949     .enable = clk_alpha_pll_enable,
0950     .disable = clk_alpha_pll_disable,
0951     .is_enabled = clk_alpha_pll_is_enabled,
0952     .recalc_rate = clk_alpha_pll_recalc_rate,
0953     .round_rate = clk_alpha_pll_round_rate,
0954     .set_rate = clk_alpha_pll_set_rate,
0955 };
0956 EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
0957 
0958 const struct clk_ops clk_alpha_pll_huayra_ops = {
0959     .enable = clk_alpha_pll_enable,
0960     .disable = clk_alpha_pll_disable,
0961     .is_enabled = clk_alpha_pll_is_enabled,
0962     .recalc_rate = alpha_pll_huayra_recalc_rate,
0963     .round_rate = alpha_pll_huayra_round_rate,
0964     .set_rate = alpha_pll_huayra_set_rate,
0965 };
0966 EXPORT_SYMBOL_GPL(clk_alpha_pll_huayra_ops);
0967 
0968 const struct clk_ops clk_alpha_pll_hwfsm_ops = {
0969     .enable = clk_alpha_pll_hwfsm_enable,
0970     .disable = clk_alpha_pll_hwfsm_disable,
0971     .is_enabled = clk_alpha_pll_hwfsm_is_enabled,
0972     .recalc_rate = clk_alpha_pll_recalc_rate,
0973     .round_rate = clk_alpha_pll_round_rate,
0974     .set_rate = clk_alpha_pll_hwfsm_set_rate,
0975 };
0976 EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops);
0977 
0978 const struct clk_ops clk_alpha_pll_fixed_trion_ops = {
0979     .enable = clk_trion_pll_enable,
0980     .disable = clk_trion_pll_disable,
0981     .is_enabled = clk_trion_pll_is_enabled,
0982     .recalc_rate = clk_trion_pll_recalc_rate,
0983     .round_rate = clk_alpha_pll_round_rate,
0984 };
0985 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_trion_ops);
0986 
0987 static unsigned long
0988 clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
0989 {
0990     struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
0991     u32 ctl;
0992 
0993     regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
0994 
0995     ctl >>= PLL_POST_DIV_SHIFT;
0996     ctl &= PLL_POST_DIV_MASK(pll);
0997 
0998     return parent_rate >> fls(ctl);
0999 }
1000 
1001 static const struct clk_div_table clk_alpha_div_table[] = {
1002     { 0x0, 1 },
1003     { 0x1, 2 },
1004     { 0x3, 4 },
1005     { 0x7, 8 },
1006     { 0xf, 16 },
1007     { }
1008 };
1009 
1010 static const struct clk_div_table clk_alpha_2bit_div_table[] = {
1011     { 0x0, 1 },
1012     { 0x1, 2 },
1013     { 0x3, 4 },
1014     { }
1015 };
1016 
1017 static long
1018 clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
1019                  unsigned long *prate)
1020 {
1021     struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1022     const struct clk_div_table *table;
1023 
1024     if (pll->width == 2)
1025         table = clk_alpha_2bit_div_table;
1026     else
1027         table = clk_alpha_div_table;
1028 
1029     return divider_round_rate(hw, rate, prate, table,
1030                   pll->width, CLK_DIVIDER_POWER_OF_TWO);
1031 }
1032 
1033 static long
1034 clk_alpha_pll_postdiv_round_ro_rate(struct clk_hw *hw, unsigned long rate,
1035                     unsigned long *prate)
1036 {
1037     struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1038     u32 ctl, div;
1039 
1040     regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
1041 
1042     ctl >>= PLL_POST_DIV_SHIFT;
1043     ctl &= BIT(pll->width) - 1;
1044     div = 1 << fls(ctl);
1045 
1046     if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
1047         *prate = clk_hw_round_rate(clk_hw_get_parent(hw), div * rate);
1048 
1049     return DIV_ROUND_UP_ULL((u64)*prate, div);
1050 }
1051 
1052 static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
1053                       unsigned long parent_rate)
1054 {
1055     struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1056     int div;
1057 
1058     /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
1059     div = DIV_ROUND_UP_ULL(parent_rate, rate) - 1;
1060 
1061     return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
1062                   PLL_POST_DIV_MASK(pll) << PLL_POST_DIV_SHIFT,
1063                   div << PLL_POST_DIV_SHIFT);
1064 }
1065 
1066 const struct clk_ops clk_alpha_pll_postdiv_ops = {
1067     .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
1068     .round_rate = clk_alpha_pll_postdiv_round_rate,
1069     .set_rate = clk_alpha_pll_postdiv_set_rate,
1070 };
1071 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);
1072 
1073 const struct clk_ops clk_alpha_pll_postdiv_ro_ops = {
1074     .round_rate = clk_alpha_pll_postdiv_round_ro_rate,
1075     .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
1076 };
1077 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ro_ops);
1078 
1079 void clk_fabia_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
1080                  const struct alpha_pll_config *config)
1081 {
1082     u32 val, mask;
1083 
1084     clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
1085     clk_alpha_pll_write_config(regmap, PLL_FRAC(pll), config->alpha);
1086     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll),
1087                         config->config_ctl_val);
1088     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll),
1089                         config->config_ctl_hi_val);
1090     clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll),
1091                         config->user_ctl_val);
1092     clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll),
1093                         config->user_ctl_hi_val);
1094     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll),
1095                         config->test_ctl_val);
1096     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll),
1097                         config->test_ctl_hi_val);
1098 
1099     if (config->post_div_mask) {
1100         mask = config->post_div_mask;
1101         val = config->post_div_val;
1102         regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
1103     }
1104 
1105     regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
1106                             PLL_UPDATE_BYPASS);
1107 
1108     regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
1109 }
1110 EXPORT_SYMBOL_GPL(clk_fabia_pll_configure);
1111 
1112 static int alpha_pll_fabia_enable(struct clk_hw *hw)
1113 {
1114     int ret;
1115     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1116     u32 val, opmode_val;
1117     struct regmap *regmap = pll->clkr.regmap;
1118 
1119     ret = regmap_read(regmap, PLL_MODE(pll), &val);
1120     if (ret)
1121         return ret;
1122 
1123     /* If in FSM mode, just vote for it */
1124     if (val & PLL_VOTE_FSM_ENA) {
1125         ret = clk_enable_regmap(hw);
1126         if (ret)
1127             return ret;
1128         return wait_for_pll_enable_active(pll);
1129     }
1130 
1131     ret = regmap_read(regmap, PLL_OPMODE(pll), &opmode_val);
1132     if (ret)
1133         return ret;
1134 
1135     /* Skip If PLL is already running */
1136     if ((opmode_val & PLL_RUN) && (val & PLL_OUTCTRL))
1137         return 0;
1138 
1139     ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
1140     if (ret)
1141         return ret;
1142 
1143     ret = regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1144     if (ret)
1145         return ret;
1146 
1147     ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N,
1148                  PLL_RESET_N);
1149     if (ret)
1150         return ret;
1151 
1152     ret = regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
1153     if (ret)
1154         return ret;
1155 
1156     ret = wait_for_pll_enable_lock(pll);
1157     if (ret)
1158         return ret;
1159 
1160     ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
1161                  PLL_OUT_MASK, PLL_OUT_MASK);
1162     if (ret)
1163         return ret;
1164 
1165     return regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL,
1166                  PLL_OUTCTRL);
1167 }
1168 
1169 static void alpha_pll_fabia_disable(struct clk_hw *hw)
1170 {
1171     int ret;
1172     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1173     u32 val;
1174     struct regmap *regmap = pll->clkr.regmap;
1175 
1176     ret = regmap_read(regmap, PLL_MODE(pll), &val);
1177     if (ret)
1178         return;
1179 
1180     /* If in FSM mode, just unvote it */
1181     if (val & PLL_FSM_ENA) {
1182         clk_disable_regmap(hw);
1183         return;
1184     }
1185 
1186     ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
1187     if (ret)
1188         return;
1189 
1190     /* Disable main outputs */
1191     ret = regmap_update_bits(regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, 0);
1192     if (ret)
1193         return;
1194 
1195     /* Place the PLL in STANDBY */
1196     regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1197 }
1198 
1199 static unsigned long alpha_pll_fabia_recalc_rate(struct clk_hw *hw,
1200                         unsigned long parent_rate)
1201 {
1202     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1203     u32 l, frac, alpha_width = pll_alpha_width(pll);
1204 
1205     regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
1206     regmap_read(pll->clkr.regmap, PLL_FRAC(pll), &frac);
1207 
1208     return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
1209 }
1210 
1211 /*
1212  * Due to limited number of bits for fractional rate programming, the
1213  * rounded up rate could be marginally higher than the requested rate.
1214  */
1215 static int alpha_pll_check_rate_margin(struct clk_hw *hw,
1216             unsigned long rrate, unsigned long rate)
1217 {
1218     unsigned long rate_margin = rate + PLL_RATE_MARGIN;
1219 
1220     if (rrate > rate_margin || rrate < rate) {
1221         pr_err("%s: Rounded rate %lu not within range [%lu, %lu)\n",
1222                clk_hw_get_name(hw), rrate, rate, rate_margin);
1223         return -EINVAL;
1224     }
1225 
1226     return 0;
1227 }
1228 
1229 static int alpha_pll_fabia_set_rate(struct clk_hw *hw, unsigned long rate,
1230                         unsigned long prate)
1231 {
1232     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1233     u32 l, alpha_width = pll_alpha_width(pll);
1234     unsigned long rrate;
1235     int ret;
1236     u64 a;
1237 
1238     rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
1239 
1240     ret = alpha_pll_check_rate_margin(hw, rrate, rate);
1241     if (ret < 0)
1242         return ret;
1243 
1244     regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
1245     regmap_write(pll->clkr.regmap, PLL_FRAC(pll), a);
1246 
1247     return __clk_alpha_pll_update_latch(pll);
1248 }
1249 
1250 static int alpha_pll_fabia_prepare(struct clk_hw *hw)
1251 {
1252     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1253     const struct pll_vco *vco;
1254     struct clk_hw *parent_hw;
1255     unsigned long cal_freq, rrate;
1256     u32 cal_l, val, alpha_width = pll_alpha_width(pll);
1257     const char *name = clk_hw_get_name(hw);
1258     u64 a;
1259     int ret;
1260 
1261     /* Check if calibration needs to be done i.e. PLL is in reset */
1262     ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
1263     if (ret)
1264         return ret;
1265 
1266     /* Return early if calibration is not needed. */
1267     if (val & PLL_RESET_N)
1268         return 0;
1269 
1270     vco = alpha_pll_find_vco(pll, clk_hw_get_rate(hw));
1271     if (!vco) {
1272         pr_err("%s: alpha pll not in a valid vco range\n", name);
1273         return -EINVAL;
1274     }
1275 
1276     cal_freq = DIV_ROUND_CLOSEST((pll->vco_table[0].min_freq +
1277                 pll->vco_table[0].max_freq) * 54, 100);
1278 
1279     parent_hw = clk_hw_get_parent(hw);
1280     if (!parent_hw)
1281         return -EINVAL;
1282 
1283     rrate = alpha_pll_round_rate(cal_freq, clk_hw_get_rate(parent_hw),
1284                     &cal_l, &a, alpha_width);
1285 
1286     ret = alpha_pll_check_rate_margin(hw, rrate, cal_freq);
1287     if (ret < 0)
1288         return ret;
1289 
1290     /* Setup PLL for calibration frequency */
1291     regmap_write(pll->clkr.regmap, PLL_CAL_L_VAL(pll), cal_l);
1292 
1293     /* Bringup the PLL at calibration frequency */
1294     ret = clk_alpha_pll_enable(hw);
1295     if (ret) {
1296         pr_err("%s: alpha pll calibration failed\n", name);
1297         return ret;
1298     }
1299 
1300     clk_alpha_pll_disable(hw);
1301 
1302     return 0;
1303 }
1304 
1305 const struct clk_ops clk_alpha_pll_fabia_ops = {
1306     .prepare = alpha_pll_fabia_prepare,
1307     .enable = alpha_pll_fabia_enable,
1308     .disable = alpha_pll_fabia_disable,
1309     .is_enabled = clk_alpha_pll_is_enabled,
1310     .set_rate = alpha_pll_fabia_set_rate,
1311     .recalc_rate = alpha_pll_fabia_recalc_rate,
1312     .round_rate = clk_alpha_pll_round_rate,
1313 };
1314 EXPORT_SYMBOL_GPL(clk_alpha_pll_fabia_ops);
1315 
1316 const struct clk_ops clk_alpha_pll_fixed_fabia_ops = {
1317     .enable = alpha_pll_fabia_enable,
1318     .disable = alpha_pll_fabia_disable,
1319     .is_enabled = clk_alpha_pll_is_enabled,
1320     .recalc_rate = alpha_pll_fabia_recalc_rate,
1321     .round_rate = clk_alpha_pll_round_rate,
1322 };
1323 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_fabia_ops);
1324 
1325 static unsigned long clk_alpha_pll_postdiv_fabia_recalc_rate(struct clk_hw *hw,
1326                     unsigned long parent_rate)
1327 {
1328     struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1329     u32 i, div = 1, val;
1330     int ret;
1331 
1332     ret = regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &val);
1333     if (ret)
1334         return ret;
1335 
1336     val >>= pll->post_div_shift;
1337     val &= BIT(pll->width) - 1;
1338 
1339     for (i = 0; i < pll->num_post_div; i++) {
1340         if (pll->post_div_table[i].val == val) {
1341             div = pll->post_div_table[i].div;
1342             break;
1343         }
1344     }
1345 
1346     return (parent_rate / div);
1347 }
1348 
1349 static unsigned long
1350 clk_trion_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
1351 {
1352     struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1353     struct regmap *regmap = pll->clkr.regmap;
1354     u32 i, div = 1, val;
1355 
1356     regmap_read(regmap, PLL_USER_CTL(pll), &val);
1357 
1358     val >>= pll->post_div_shift;
1359     val &= PLL_POST_DIV_MASK(pll);
1360 
1361     for (i = 0; i < pll->num_post_div; i++) {
1362         if (pll->post_div_table[i].val == val) {
1363             div = pll->post_div_table[i].div;
1364             break;
1365         }
1366     }
1367 
1368     return (parent_rate / div);
1369 }
1370 
1371 static long
1372 clk_trion_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
1373                  unsigned long *prate)
1374 {
1375     struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1376 
1377     return divider_round_rate(hw, rate, prate, pll->post_div_table,
1378                   pll->width, CLK_DIVIDER_ROUND_CLOSEST);
1379 };
1380 
1381 static int
1382 clk_trion_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
1383                    unsigned long parent_rate)
1384 {
1385     struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1386     struct regmap *regmap = pll->clkr.regmap;
1387     int i, val = 0, div;
1388 
1389     div = DIV_ROUND_UP_ULL(parent_rate, rate);
1390     for (i = 0; i < pll->num_post_div; i++) {
1391         if (pll->post_div_table[i].div == div) {
1392             val = pll->post_div_table[i].val;
1393             break;
1394         }
1395     }
1396 
1397     return regmap_update_bits(regmap, PLL_USER_CTL(pll),
1398                   PLL_POST_DIV_MASK(pll) << PLL_POST_DIV_SHIFT,
1399                   val << PLL_POST_DIV_SHIFT);
1400 }
1401 
1402 const struct clk_ops clk_alpha_pll_postdiv_trion_ops = {
1403     .recalc_rate = clk_trion_pll_postdiv_recalc_rate,
1404     .round_rate = clk_trion_pll_postdiv_round_rate,
1405     .set_rate = clk_trion_pll_postdiv_set_rate,
1406 };
1407 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_trion_ops);
1408 
1409 static long clk_alpha_pll_postdiv_fabia_round_rate(struct clk_hw *hw,
1410                 unsigned long rate, unsigned long *prate)
1411 {
1412     struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1413 
1414     return divider_round_rate(hw, rate, prate, pll->post_div_table,
1415                 pll->width, CLK_DIVIDER_ROUND_CLOSEST);
1416 }
1417 
1418 static int clk_alpha_pll_postdiv_fabia_set_rate(struct clk_hw *hw,
1419                 unsigned long rate, unsigned long parent_rate)
1420 {
1421     struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1422     int i, val = 0, div, ret;
1423 
1424     /*
1425      * If the PLL is in FSM mode, then treat set_rate callback as a
1426      * no-operation.
1427      */
1428     ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
1429     if (ret)
1430         return ret;
1431 
1432     if (val & PLL_VOTE_FSM_ENA)
1433         return 0;
1434 
1435     div = DIV_ROUND_UP_ULL(parent_rate, rate);
1436     for (i = 0; i < pll->num_post_div; i++) {
1437         if (pll->post_div_table[i].div == div) {
1438             val = pll->post_div_table[i].val;
1439             break;
1440         }
1441     }
1442 
1443     return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
1444                 (BIT(pll->width) - 1) << pll->post_div_shift,
1445                 val << pll->post_div_shift);
1446 }
1447 
1448 const struct clk_ops clk_alpha_pll_postdiv_fabia_ops = {
1449     .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
1450     .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
1451     .set_rate = clk_alpha_pll_postdiv_fabia_set_rate,
1452 };
1453 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_fabia_ops);
1454 
1455 /**
1456  * clk_trion_pll_configure - configure the trion pll
1457  *
1458  * @pll: clk alpha pll
1459  * @regmap: register map
1460  * @config: configuration to apply for pll
1461  */
1462 void clk_trion_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
1463                  const struct alpha_pll_config *config)
1464 {
1465     /*
1466      * If the bootloader left the PLL enabled it's likely that there are
1467      * RCGs that will lock up if we disable the PLL below.
1468      */
1469     if (trion_pll_is_enabled(pll, regmap)) {
1470         pr_debug("Trion PLL is already enabled, skipping configuration\n");
1471         return;
1472     }
1473 
1474     clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
1475     regmap_write(regmap, PLL_CAL_L_VAL(pll), TRION_PLL_CAL_VAL);
1476     clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
1477     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll),
1478                      config->config_ctl_val);
1479     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll),
1480                      config->config_ctl_hi_val);
1481     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll),
1482                      config->config_ctl_hi1_val);
1483     clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll),
1484                     config->user_ctl_val);
1485     clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll),
1486                     config->user_ctl_hi_val);
1487     clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U1(pll),
1488                     config->user_ctl_hi1_val);
1489     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll),
1490                     config->test_ctl_val);
1491     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll),
1492                     config->test_ctl_hi_val);
1493     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U1(pll),
1494                     config->test_ctl_hi1_val);
1495 
1496     regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
1497                PLL_UPDATE_BYPASS);
1498 
1499     /* Disable PLL output */
1500     regmap_update_bits(regmap, PLL_MODE(pll),  PLL_OUTCTRL, 0);
1501 
1502     /* Set operation mode to OFF */
1503     regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1504 
1505     /* Place the PLL in STANDBY mode */
1506     regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
1507 }
1508 EXPORT_SYMBOL_GPL(clk_trion_pll_configure);
1509 
1510 /*
1511  * The TRION PLL requires a power-on self-calibration which happens when the
1512  * PLL comes out of reset. Calibrate in case it is not completed.
1513  */
1514 static int __alpha_pll_trion_prepare(struct clk_hw *hw, u32 pcal_done)
1515 {
1516     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1517     u32 val;
1518     int ret;
1519 
1520     /* Return early if calibration is not needed. */
1521     regmap_read(pll->clkr.regmap, PLL_STATUS(pll), &val);
1522     if (val & pcal_done)
1523         return 0;
1524 
1525     /* On/off to calibrate */
1526     ret = clk_trion_pll_enable(hw);
1527     if (!ret)
1528         clk_trion_pll_disable(hw);
1529 
1530     return ret;
1531 }
1532 
1533 static int alpha_pll_trion_prepare(struct clk_hw *hw)
1534 {
1535     return __alpha_pll_trion_prepare(hw, TRION_PCAL_DONE);
1536 }
1537 
1538 static int alpha_pll_lucid_prepare(struct clk_hw *hw)
1539 {
1540     return __alpha_pll_trion_prepare(hw, LUCID_PCAL_DONE);
1541 }
1542 
1543 static int __alpha_pll_trion_set_rate(struct clk_hw *hw, unsigned long rate,
1544                       unsigned long prate, u32 latch_bit, u32 latch_ack)
1545 {
1546     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1547     unsigned long rrate;
1548     u32 val, l, alpha_width = pll_alpha_width(pll);
1549     u64 a;
1550     int ret;
1551 
1552     rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
1553 
1554     ret = alpha_pll_check_rate_margin(hw, rrate, rate);
1555     if (ret < 0)
1556         return ret;
1557 
1558     regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
1559     regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
1560 
1561     /* Latch the PLL input */
1562     ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), latch_bit, latch_bit);
1563     if (ret)
1564         return ret;
1565 
1566     /* Wait for 2 reference cycles before checking the ACK bit. */
1567     udelay(1);
1568     regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
1569     if (!(val & latch_ack)) {
1570         pr_err("Lucid PLL latch failed. Output may be unstable!\n");
1571         return -EINVAL;
1572     }
1573 
1574     /* Return the latch input to 0 */
1575     ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), latch_bit, 0);
1576     if (ret)
1577         return ret;
1578 
1579     if (clk_hw_is_enabled(hw)) {
1580         ret = wait_for_pll_enable_lock(pll);
1581         if (ret)
1582             return ret;
1583     }
1584 
1585     /* Wait for PLL output to stabilize */
1586     udelay(100);
1587     return 0;
1588 }
1589 
1590 static int alpha_pll_trion_set_rate(struct clk_hw *hw, unsigned long rate,
1591                     unsigned long prate)
1592 {
1593     return __alpha_pll_trion_set_rate(hw, rate, prate, PLL_UPDATE, ALPHA_PLL_ACK_LATCH);
1594 }
1595 
1596 const struct clk_ops clk_alpha_pll_trion_ops = {
1597     .prepare = alpha_pll_trion_prepare,
1598     .enable = clk_trion_pll_enable,
1599     .disable = clk_trion_pll_disable,
1600     .is_enabled = clk_trion_pll_is_enabled,
1601     .recalc_rate = clk_trion_pll_recalc_rate,
1602     .round_rate = clk_alpha_pll_round_rate,
1603     .set_rate = alpha_pll_trion_set_rate,
1604 };
1605 EXPORT_SYMBOL_GPL(clk_alpha_pll_trion_ops);
1606 
1607 const struct clk_ops clk_alpha_pll_lucid_ops = {
1608     .prepare = alpha_pll_lucid_prepare,
1609     .enable = clk_trion_pll_enable,
1610     .disable = clk_trion_pll_disable,
1611     .is_enabled = clk_trion_pll_is_enabled,
1612     .recalc_rate = clk_trion_pll_recalc_rate,
1613     .round_rate = clk_alpha_pll_round_rate,
1614     .set_rate = alpha_pll_trion_set_rate,
1615 };
1616 EXPORT_SYMBOL_GPL(clk_alpha_pll_lucid_ops);
1617 
1618 const struct clk_ops clk_alpha_pll_postdiv_lucid_ops = {
1619     .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
1620     .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
1621     .set_rate = clk_alpha_pll_postdiv_fabia_set_rate,
1622 };
1623 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_lucid_ops);
1624 
1625 void clk_agera_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
1626             const struct alpha_pll_config *config)
1627 {
1628     clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
1629     clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
1630     clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll),
1631                             config->user_ctl_val);
1632     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll),
1633                         config->config_ctl_val);
1634     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll),
1635                         config->config_ctl_hi_val);
1636     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll),
1637                         config->test_ctl_val);
1638     clk_alpha_pll_write_config(regmap,  PLL_TEST_CTL_U(pll),
1639                         config->test_ctl_hi_val);
1640 }
1641 EXPORT_SYMBOL_GPL(clk_agera_pll_configure);
1642 
1643 static int clk_alpha_pll_agera_set_rate(struct clk_hw *hw, unsigned long rate,
1644                             unsigned long prate)
1645 {
1646     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1647     u32 l, alpha_width = pll_alpha_width(pll);
1648     int ret;
1649     unsigned long rrate;
1650     u64 a;
1651 
1652     rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
1653     ret = alpha_pll_check_rate_margin(hw, rrate, rate);
1654     if (ret < 0)
1655         return ret;
1656 
1657     /* change L_VAL without having to go through the power on sequence */
1658     regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
1659     regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
1660 
1661     if (clk_hw_is_enabled(hw))
1662         return wait_for_pll_enable_lock(pll);
1663 
1664     return 0;
1665 }
1666 
1667 const struct clk_ops clk_alpha_pll_agera_ops = {
1668     .enable = clk_alpha_pll_enable,
1669     .disable = clk_alpha_pll_disable,
1670     .is_enabled = clk_alpha_pll_is_enabled,
1671     .recalc_rate = alpha_pll_fabia_recalc_rate,
1672     .round_rate = clk_alpha_pll_round_rate,
1673     .set_rate = clk_alpha_pll_agera_set_rate,
1674 };
1675 EXPORT_SYMBOL_GPL(clk_alpha_pll_agera_ops);
1676 
1677 static int alpha_pll_lucid_5lpe_enable(struct clk_hw *hw)
1678 {
1679     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1680     u32 val;
1681     int ret;
1682 
1683     ret = regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &val);
1684     if (ret)
1685         return ret;
1686 
1687     /* If in FSM mode, just vote for it */
1688     if (val & LUCID_5LPE_ENABLE_VOTE_RUN) {
1689         ret = clk_enable_regmap(hw);
1690         if (ret)
1691             return ret;
1692         return wait_for_pll_enable_lock(pll);
1693     }
1694 
1695     /* Check if PLL is already enabled, return if enabled */
1696     ret = trion_pll_is_enabled(pll, pll->clkr.regmap);
1697     if (ret < 0)
1698         return ret;
1699 
1700     ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
1701     if (ret)
1702         return ret;
1703 
1704     regmap_write(pll->clkr.regmap, PLL_OPMODE(pll), PLL_RUN);
1705 
1706     ret = wait_for_pll_enable_lock(pll);
1707     if (ret)
1708         return ret;
1709 
1710     /* Enable the PLL outputs */
1711     ret = regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, PLL_OUT_MASK);
1712     if (ret)
1713         return ret;
1714 
1715     /* Enable the global PLL outputs */
1716     return regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_OUTCTRL, PLL_OUTCTRL);
1717 }
1718 
1719 static void alpha_pll_lucid_5lpe_disable(struct clk_hw *hw)
1720 {
1721     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1722     u32 val;
1723     int ret;
1724 
1725     ret = regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &val);
1726     if (ret)
1727         return;
1728 
1729     /* If in FSM mode, just unvote it */
1730     if (val & LUCID_5LPE_ENABLE_VOTE_RUN) {
1731         clk_disable_regmap(hw);
1732         return;
1733     }
1734 
1735     /* Disable the global PLL output */
1736     ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
1737     if (ret)
1738         return;
1739 
1740     /* Disable the PLL outputs */
1741     ret = regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, 0);
1742     if (ret)
1743         return;
1744 
1745     /* Place the PLL mode in STANDBY */
1746     regmap_write(pll->clkr.regmap, PLL_OPMODE(pll), PLL_STANDBY);
1747 }
1748 
1749 /*
1750  * The Lucid 5LPE PLL requires a power-on self-calibration which happens
1751  * when the PLL comes out of reset. Calibrate in case it is not completed.
1752  */
1753 static int alpha_pll_lucid_5lpe_prepare(struct clk_hw *hw)
1754 {
1755     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1756     struct clk_hw *p;
1757     u32 val = 0;
1758     int ret;
1759 
1760     /* Return early if calibration is not needed. */
1761     regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
1762     if (val & LUCID_5LPE_PCAL_DONE)
1763         return 0;
1764 
1765     p = clk_hw_get_parent(hw);
1766     if (!p)
1767         return -EINVAL;
1768 
1769     ret = alpha_pll_lucid_5lpe_enable(hw);
1770     if (ret)
1771         return ret;
1772 
1773     alpha_pll_lucid_5lpe_disable(hw);
1774 
1775     return 0;
1776 }
1777 
1778 static int alpha_pll_lucid_5lpe_set_rate(struct clk_hw *hw, unsigned long rate,
1779                      unsigned long prate)
1780 {
1781     return __alpha_pll_trion_set_rate(hw, rate, prate,
1782                       LUCID_5LPE_PLL_LATCH_INPUT,
1783                       LUCID_5LPE_ALPHA_PLL_ACK_LATCH);
1784 }
1785 
1786 static int __clk_lucid_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
1787                         unsigned long parent_rate,
1788                         unsigned long enable_vote_run)
1789 {
1790     struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
1791     struct regmap *regmap = pll->clkr.regmap;
1792     int i, val, div, ret;
1793     u32 mask;
1794 
1795     /*
1796      * If the PLL is in FSM mode, then treat set_rate callback as a
1797      * no-operation.
1798      */
1799     ret = regmap_read(regmap, PLL_USER_CTL(pll), &val);
1800     if (ret)
1801         return ret;
1802 
1803     if (val & enable_vote_run)
1804         return 0;
1805 
1806     if (!pll->post_div_table) {
1807         pr_err("Missing the post_div_table for the %s PLL\n",
1808                clk_hw_get_name(&pll->clkr.hw));
1809         return -EINVAL;
1810     }
1811 
1812     div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
1813     for (i = 0; i < pll->num_post_div; i++) {
1814         if (pll->post_div_table[i].div == div) {
1815             val = pll->post_div_table[i].val;
1816             break;
1817         }
1818     }
1819 
1820     mask = GENMASK(pll->width + pll->post_div_shift - 1, pll->post_div_shift);
1821     return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
1822                   mask, val << pll->post_div_shift);
1823 }
1824 
1825 static int clk_lucid_5lpe_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
1826                            unsigned long parent_rate)
1827 {
1828     return __clk_lucid_pll_postdiv_set_rate(hw, rate, parent_rate, LUCID_5LPE_ENABLE_VOTE_RUN);
1829 }
1830 
1831 const struct clk_ops clk_alpha_pll_lucid_5lpe_ops = {
1832     .prepare = alpha_pll_lucid_5lpe_prepare,
1833     .enable = alpha_pll_lucid_5lpe_enable,
1834     .disable = alpha_pll_lucid_5lpe_disable,
1835     .is_enabled = clk_trion_pll_is_enabled,
1836     .recalc_rate = clk_trion_pll_recalc_rate,
1837     .round_rate = clk_alpha_pll_round_rate,
1838     .set_rate = alpha_pll_lucid_5lpe_set_rate,
1839 };
1840 EXPORT_SYMBOL_GPL(clk_alpha_pll_lucid_5lpe_ops);
1841 
1842 const struct clk_ops clk_alpha_pll_fixed_lucid_5lpe_ops = {
1843     .enable = alpha_pll_lucid_5lpe_enable,
1844     .disable = alpha_pll_lucid_5lpe_disable,
1845     .is_enabled = clk_trion_pll_is_enabled,
1846     .recalc_rate = clk_trion_pll_recalc_rate,
1847     .round_rate = clk_alpha_pll_round_rate,
1848 };
1849 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_lucid_5lpe_ops);
1850 
1851 const struct clk_ops clk_alpha_pll_postdiv_lucid_5lpe_ops = {
1852     .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
1853     .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
1854     .set_rate = clk_lucid_5lpe_pll_postdiv_set_rate,
1855 };
1856 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_lucid_5lpe_ops);
1857 
1858 void clk_zonda_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
1859                  const struct alpha_pll_config *config)
1860 {
1861     clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
1862     clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
1863     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
1864     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll), config->config_ctl_hi_val);
1865     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll), config->config_ctl_hi1_val);
1866     clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll), config->user_ctl_val);
1867     clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll), config->user_ctl_hi_val);
1868     clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U1(pll), config->user_ctl_hi1_val);
1869     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll), config->test_ctl_val);
1870     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll), config->test_ctl_hi_val);
1871     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U1(pll), config->test_ctl_hi1_val);
1872 
1873     regmap_update_bits(regmap, PLL_MODE(pll), PLL_BYPASSNL, 0);
1874 
1875     /* Disable PLL output */
1876     regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
1877 
1878     /* Set operation mode to OFF */
1879     regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
1880 
1881     /* Place the PLL in STANDBY mode */
1882     regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
1883 }
1884 EXPORT_SYMBOL_GPL(clk_zonda_pll_configure);
1885 
1886 static int clk_zonda_pll_enable(struct clk_hw *hw)
1887 {
1888     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1889     struct regmap *regmap = pll->clkr.regmap;
1890     u32 val;
1891     int ret;
1892 
1893     regmap_read(regmap, PLL_MODE(pll), &val);
1894 
1895     /* If in FSM mode, just vote for it */
1896     if (val & PLL_VOTE_FSM_ENA) {
1897         ret = clk_enable_regmap(hw);
1898         if (ret)
1899             return ret;
1900         return wait_for_pll_enable_active(pll);
1901     }
1902 
1903     /* Get the PLL out of bypass mode */
1904     regmap_update_bits(regmap, PLL_MODE(pll), PLL_BYPASSNL, PLL_BYPASSNL);
1905 
1906     /*
1907      * H/W requires a 1us delay between disabling the bypass and
1908      * de-asserting the reset.
1909      */
1910     udelay(1);
1911 
1912     regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
1913 
1914     /* Set operation mode to RUN */
1915     regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
1916 
1917     regmap_read(regmap, PLL_TEST_CTL(pll), &val);
1918 
1919     /* If cfa mode then poll for freq lock */
1920     if (val & ZONDA_STAY_IN_CFA)
1921         ret = wait_for_zonda_pll_freq_lock(pll);
1922     else
1923         ret = wait_for_pll_enable_lock(pll);
1924     if (ret)
1925         return ret;
1926 
1927     /* Enable the PLL outputs */
1928     regmap_update_bits(regmap, PLL_USER_CTL(pll), ZONDA_PLL_OUT_MASK, ZONDA_PLL_OUT_MASK);
1929 
1930     /* Enable the global PLL outputs */
1931     regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, PLL_OUTCTRL);
1932 
1933     return 0;
1934 }
1935 
1936 static void clk_zonda_pll_disable(struct clk_hw *hw)
1937 {
1938     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1939     struct regmap *regmap = pll->clkr.regmap;
1940     u32 val;
1941 
1942     regmap_read(regmap, PLL_MODE(pll), &val);
1943 
1944     /* If in FSM mode, just unvote it */
1945     if (val & PLL_VOTE_FSM_ENA) {
1946         clk_disable_regmap(hw);
1947         return;
1948     }
1949 
1950     /* Disable the global PLL output */
1951     regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
1952 
1953     /* Disable the PLL outputs */
1954     regmap_update_bits(regmap, PLL_USER_CTL(pll), ZONDA_PLL_OUT_MASK, 0);
1955 
1956     /* Put the PLL in bypass and reset */
1957     regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N | PLL_BYPASSNL, 0);
1958 
1959     /* Place the PLL mode in OFF state */
1960     regmap_write(regmap, PLL_OPMODE(pll), 0x0);
1961 }
1962 
1963 static int clk_zonda_pll_set_rate(struct clk_hw *hw, unsigned long rate,
1964                   unsigned long prate)
1965 {
1966     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
1967     unsigned long rrate;
1968     u32 test_ctl_val;
1969     u32 l, alpha_width = pll_alpha_width(pll);
1970     u64 a;
1971     int ret;
1972 
1973     rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
1974 
1975     ret = alpha_pll_check_rate_margin(hw, rrate, rate);
1976     if (ret < 0)
1977         return ret;
1978 
1979     regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
1980     regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
1981 
1982     /* Wait before polling for the frequency latch */
1983     udelay(5);
1984 
1985     /* Read stay in cfa mode */
1986     regmap_read(pll->clkr.regmap, PLL_TEST_CTL(pll), &test_ctl_val);
1987 
1988     /* If cfa mode then poll for freq lock */
1989     if (test_ctl_val & ZONDA_STAY_IN_CFA)
1990         ret = wait_for_zonda_pll_freq_lock(pll);
1991     else
1992         ret = wait_for_pll_enable_lock(pll);
1993     if (ret)
1994         return ret;
1995 
1996     /* Wait for PLL output to stabilize */
1997     udelay(100);
1998     return 0;
1999 }
2000 
2001 const struct clk_ops clk_alpha_pll_zonda_ops = {
2002     .enable = clk_zonda_pll_enable,
2003     .disable = clk_zonda_pll_disable,
2004     .is_enabled = clk_trion_pll_is_enabled,
2005     .recalc_rate = clk_trion_pll_recalc_rate,
2006     .round_rate = clk_alpha_pll_round_rate,
2007     .set_rate = clk_zonda_pll_set_rate,
2008 };
2009 EXPORT_SYMBOL_GPL(clk_alpha_pll_zonda_ops);
2010 
2011 void clk_lucid_evo_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
2012                  const struct alpha_pll_config *config)
2013 {
2014     u32 lval = config->l;
2015 
2016     lval |= TRION_PLL_CAL_VAL << LUCID_EVO_PLL_CAL_L_VAL_SHIFT;
2017     clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), lval);
2018     clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
2019     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
2020     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll), config->config_ctl_hi_val);
2021     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll), config->config_ctl_hi1_val);
2022     clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll), config->user_ctl_val);
2023     clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll), config->user_ctl_hi_val);
2024     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll), config->test_ctl_val);
2025     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll), config->test_ctl_hi_val);
2026     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U1(pll), config->test_ctl_hi1_val);
2027 
2028     /* Disable PLL output */
2029     regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
2030 
2031     /* Set operation mode to STANDBY and de-assert the reset */
2032     regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
2033     regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
2034 }
2035 EXPORT_SYMBOL_GPL(clk_lucid_evo_pll_configure);
2036 
2037 static int alpha_pll_lucid_evo_enable(struct clk_hw *hw)
2038 {
2039     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
2040     struct regmap *regmap = pll->clkr.regmap;
2041     u32 val;
2042     int ret;
2043 
2044     ret = regmap_read(regmap, PLL_USER_CTL(pll), &val);
2045     if (ret)
2046         return ret;
2047 
2048     /* If in FSM mode, just vote for it */
2049     if (val & LUCID_EVO_ENABLE_VOTE_RUN) {
2050         ret = clk_enable_regmap(hw);
2051         if (ret)
2052             return ret;
2053         return wait_for_pll_enable_lock(pll);
2054     }
2055 
2056     /* Check if PLL is already enabled */
2057     ret = trion_pll_is_enabled(pll, regmap);
2058     if (ret < 0) {
2059         return ret;
2060     } else if (ret) {
2061         pr_warn("%s PLL is already enabled\n", clk_hw_get_name(&pll->clkr.hw));
2062         return 0;
2063     }
2064 
2065     ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
2066     if (ret)
2067         return ret;
2068 
2069     /* Set operation mode to RUN */
2070     regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
2071 
2072     ret = wait_for_pll_enable_lock(pll);
2073     if (ret)
2074         return ret;
2075 
2076     /* Enable the PLL outputs */
2077     ret = regmap_update_bits(regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, PLL_OUT_MASK);
2078     if (ret)
2079         return ret;
2080 
2081     /* Enable the global PLL outputs */
2082     ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, PLL_OUTCTRL);
2083     if (ret)
2084         return ret;
2085 
2086     /* Ensure that the write above goes through before returning. */
2087     mb();
2088     return ret;
2089 }
2090 
2091 static void alpha_pll_lucid_evo_disable(struct clk_hw *hw)
2092 {
2093     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
2094     struct regmap *regmap = pll->clkr.regmap;
2095     u32 val;
2096     int ret;
2097 
2098     ret = regmap_read(regmap, PLL_USER_CTL(pll), &val);
2099     if (ret)
2100         return;
2101 
2102     /* If in FSM mode, just unvote it */
2103     if (val & LUCID_EVO_ENABLE_VOTE_RUN) {
2104         clk_disable_regmap(hw);
2105         return;
2106     }
2107 
2108     /* Disable the global PLL output */
2109     ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
2110     if (ret)
2111         return;
2112 
2113     /* Disable the PLL outputs */
2114     ret = regmap_update_bits(regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, 0);
2115     if (ret)
2116         return;
2117 
2118     /* Place the PLL mode in STANDBY */
2119     regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
2120 }
2121 
2122 static int alpha_pll_lucid_evo_prepare(struct clk_hw *hw)
2123 {
2124     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
2125     struct clk_hw *p;
2126     u32 val = 0;
2127     int ret;
2128 
2129     /* Return early if calibration is not needed. */
2130     regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
2131     if (!(val & LUCID_EVO_PCAL_NOT_DONE))
2132         return 0;
2133 
2134     p = clk_hw_get_parent(hw);
2135     if (!p)
2136         return -EINVAL;
2137 
2138     ret = alpha_pll_lucid_evo_enable(hw);
2139     if (ret)
2140         return ret;
2141 
2142     alpha_pll_lucid_evo_disable(hw);
2143 
2144     return 0;
2145 }
2146 
2147 static unsigned long alpha_pll_lucid_evo_recalc_rate(struct clk_hw *hw,
2148                              unsigned long parent_rate)
2149 {
2150     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
2151     struct regmap *regmap = pll->clkr.regmap;
2152     u32 l, frac;
2153 
2154     regmap_read(regmap, PLL_L_VAL(pll), &l);
2155     l &= LUCID_EVO_PLL_L_VAL_MASK;
2156     regmap_read(regmap, PLL_ALPHA_VAL(pll), &frac);
2157 
2158     return alpha_pll_calc_rate(parent_rate, l, frac, pll_alpha_width(pll));
2159 }
2160 
2161 static int clk_lucid_evo_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
2162                           unsigned long parent_rate)
2163 {
2164     return __clk_lucid_pll_postdiv_set_rate(hw, rate, parent_rate, LUCID_EVO_ENABLE_VOTE_RUN);
2165 }
2166 
2167 const struct clk_ops clk_alpha_pll_fixed_lucid_evo_ops = {
2168     .enable = alpha_pll_lucid_evo_enable,
2169     .disable = alpha_pll_lucid_evo_disable,
2170     .is_enabled = clk_trion_pll_is_enabled,
2171     .recalc_rate = alpha_pll_lucid_evo_recalc_rate,
2172     .round_rate = clk_alpha_pll_round_rate,
2173 };
2174 EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_lucid_evo_ops);
2175 
2176 const struct clk_ops clk_alpha_pll_postdiv_lucid_evo_ops = {
2177     .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
2178     .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
2179     .set_rate = clk_lucid_evo_pll_postdiv_set_rate,
2180 };
2181 EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_lucid_evo_ops);
2182 
2183 const struct clk_ops clk_alpha_pll_lucid_evo_ops = {
2184     .prepare = alpha_pll_lucid_evo_prepare,
2185     .enable = alpha_pll_lucid_evo_enable,
2186     .disable = alpha_pll_lucid_evo_disable,
2187     .is_enabled = clk_trion_pll_is_enabled,
2188     .recalc_rate = alpha_pll_lucid_evo_recalc_rate,
2189     .round_rate = clk_alpha_pll_round_rate,
2190     .set_rate = alpha_pll_lucid_5lpe_set_rate,
2191 };
2192 EXPORT_SYMBOL_GPL(clk_alpha_pll_lucid_evo_ops);
2193 
2194 void clk_rivian_evo_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
2195                   const struct alpha_pll_config *config)
2196 {
2197     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
2198     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll), config->config_ctl_hi_val);
2199     clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll), config->config_ctl_hi1_val);
2200     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll), config->test_ctl_val);
2201     clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll), config->test_ctl_hi_val);
2202     clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
2203     clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll), config->user_ctl_val);
2204     clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll), config->user_ctl_hi_val);
2205 
2206     regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
2207 
2208     regmap_update_bits(regmap, PLL_MODE(pll),
2209                PLL_RESET_N | PLL_BYPASSNL | PLL_OUTCTRL,
2210                PLL_RESET_N | PLL_BYPASSNL);
2211 }
2212 EXPORT_SYMBOL_GPL(clk_rivian_evo_pll_configure);
2213 
2214 static unsigned long clk_rivian_evo_pll_recalc_rate(struct clk_hw *hw,
2215                             unsigned long parent_rate)
2216 {
2217     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
2218     u32 l;
2219 
2220     regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
2221 
2222     return parent_rate * l;
2223 }
2224 
2225 static long clk_rivian_evo_pll_round_rate(struct clk_hw *hw, unsigned long rate,
2226                       unsigned long *prate)
2227 {
2228     struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
2229     unsigned long min_freq, max_freq;
2230     u32 l;
2231     u64 a;
2232 
2233     rate = alpha_pll_round_rate(rate, *prate, &l, &a, 0);
2234     if (!pll->vco_table || alpha_pll_find_vco(pll, rate))
2235         return rate;
2236 
2237     min_freq = pll->vco_table[0].min_freq;
2238     max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
2239 
2240     return clamp(rate, min_freq, max_freq);
2241 }
2242 
2243 const struct clk_ops clk_alpha_pll_rivian_evo_ops = {
2244     .enable = alpha_pll_lucid_5lpe_enable,
2245     .disable = alpha_pll_lucid_5lpe_disable,
2246     .is_enabled = clk_trion_pll_is_enabled,
2247     .recalc_rate = clk_rivian_evo_pll_recalc_rate,
2248     .round_rate = clk_rivian_evo_pll_round_rate,
2249 };
2250 EXPORT_SYMBOL_GPL(clk_alpha_pll_rivian_evo_ops);