Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * PLL clock driver for TI Davinci SoCs
0004  *
0005  * Copyright (C) 2018 David Lechner <david@lechnology.com>
0006  *
0007  * Based on arch/arm/mach-davinci/clock.c
0008  * Copyright (C) 2006-2007 Texas Instruments.
0009  * Copyright (C) 2008-2009 Deep Root Systems, LLC
0010  */
0011 
0012 #include <linux/clk-provider.h>
0013 #include <linux/clk.h>
0014 #include <linux/clk/davinci.h>
0015 #include <linux/delay.h>
0016 #include <linux/err.h>
0017 #include <linux/io.h>
0018 #include <linux/kernel.h>
0019 #include <linux/mfd/syscon.h>
0020 #include <linux/notifier.h>
0021 #include <linux/of_address.h>
0022 #include <linux/of_device.h>
0023 #include <linux/of.h>
0024 #include <linux/platform_data/clk-davinci-pll.h>
0025 #include <linux/platform_device.h>
0026 #include <linux/regmap.h>
0027 #include <linux/slab.h>
0028 #include <linux/types.h>
0029 
0030 #include "pll.h"
0031 
0032 #define MAX_NAME_SIZE   20
0033 #define OSCIN_CLK_NAME  "oscin"
0034 
0035 #define REVID       0x000
0036 #define PLLCTL      0x100
0037 #define OCSEL       0x104
0038 #define PLLSECCTL   0x108
0039 #define PLLM        0x110
0040 #define PREDIV      0x114
0041 #define PLLDIV1     0x118
0042 #define PLLDIV2     0x11c
0043 #define PLLDIV3     0x120
0044 #define OSCDIV      0x124
0045 #define POSTDIV     0x128
0046 #define BPDIV       0x12c
0047 #define PLLCMD      0x138
0048 #define PLLSTAT     0x13c
0049 #define ALNCTL      0x140
0050 #define DCHANGE     0x144
0051 #define CKEN        0x148
0052 #define CKSTAT      0x14c
0053 #define SYSTAT      0x150
0054 #define PLLDIV4     0x160
0055 #define PLLDIV5     0x164
0056 #define PLLDIV6     0x168
0057 #define PLLDIV7     0x16c
0058 #define PLLDIV8     0x170
0059 #define PLLDIV9     0x174
0060 
0061 #define PLLCTL_PLLEN        BIT(0)
0062 #define PLLCTL_PLLPWRDN     BIT(1)
0063 #define PLLCTL_PLLRST       BIT(3)
0064 #define PLLCTL_PLLDIS       BIT(4)
0065 #define PLLCTL_PLLENSRC     BIT(5)
0066 #define PLLCTL_CLKMODE      BIT(8)
0067 
0068 /* shared by most *DIV registers */
0069 #define DIV_RATIO_SHIFT     0
0070 #define DIV_RATIO_WIDTH     5
0071 #define DIV_ENABLE_SHIFT    15
0072 
0073 #define PLLCMD_GOSET        BIT(0)
0074 #define PLLSTAT_GOSTAT      BIT(0)
0075 
0076 #define CKEN_OBSCLK_SHIFT   1
0077 #define CKEN_AUXEN_SHIFT    0
0078 
0079 /*
0080  * OMAP-L138 system reference guide recommends a wait for 4 OSCIN/CLKIN
0081  * cycles to ensure that the PLLC has switched to bypass mode. Delay of 1us
0082  * ensures we are good for all > 4MHz OSCIN/CLKIN inputs. Typically the input
0083  * is ~25MHz. Units are micro seconds.
0084  */
0085 #define PLL_BYPASS_TIME     1
0086 
0087 /* From OMAP-L138 datasheet table 6-4. Units are micro seconds */
0088 #define PLL_RESET_TIME      1
0089 
0090 /*
0091  * From OMAP-L138 datasheet table 6-4; assuming prediv = 1, sqrt(pllm) = 4
0092  * Units are micro seconds.
0093  */
0094 #define PLL_LOCK_TIME       20
0095 
0096 /**
0097  * struct davinci_pll_clk - Main PLL clock (aka PLLOUT)
0098  * @hw: clk_hw for the pll
0099  * @base: Base memory address
0100  * @pllm_min: The minimum allowable PLLM[PLLM] value
0101  * @pllm_max: The maxiumum allowable PLLM[PLLM] value
0102  * @pllm_mask: Bitmask for PLLM[PLLM] value
0103  */
0104 struct davinci_pll_clk {
0105     struct clk_hw hw;
0106     void __iomem *base;
0107     u32 pllm_min;
0108     u32 pllm_max;
0109     u32 pllm_mask;
0110 };
0111 
0112 #define to_davinci_pll_clk(_hw) \
0113     container_of((_hw), struct davinci_pll_clk, hw)
0114 
0115 static unsigned long davinci_pll_recalc_rate(struct clk_hw *hw,
0116                          unsigned long parent_rate)
0117 {
0118     struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
0119     unsigned long rate = parent_rate;
0120     u32 mult;
0121 
0122     mult = readl(pll->base + PLLM) & pll->pllm_mask;
0123     rate *= mult + 1;
0124 
0125     return rate;
0126 }
0127 
0128 static int davinci_pll_determine_rate(struct clk_hw *hw,
0129                       struct clk_rate_request *req)
0130 {
0131     struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
0132     struct clk_hw *parent = req->best_parent_hw;
0133     unsigned long parent_rate = req->best_parent_rate;
0134     unsigned long rate = req->rate;
0135     unsigned long best_rate, r;
0136     u32 mult;
0137 
0138     /* there is a limited range of valid outputs (see datasheet) */
0139     if (rate < req->min_rate)
0140         return -EINVAL;
0141 
0142     rate = min(rate, req->max_rate);
0143     mult = rate / parent_rate;
0144     best_rate = parent_rate * mult;
0145 
0146     /* easy case when there is no PREDIV */
0147     if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
0148         if (best_rate < req->min_rate)
0149             return -EINVAL;
0150 
0151         if (mult < pll->pllm_min || mult > pll->pllm_max)
0152             return -EINVAL;
0153 
0154         req->rate = best_rate;
0155 
0156         return 0;
0157     }
0158 
0159     /* see if the PREDIV clock can help us */
0160     best_rate = 0;
0161 
0162     for (mult = pll->pllm_min; mult <= pll->pllm_max; mult++) {
0163         parent_rate = clk_hw_round_rate(parent, rate / mult);
0164         r = parent_rate * mult;
0165         if (r < req->min_rate)
0166             continue;
0167         if (r > rate || r > req->max_rate)
0168             break;
0169         if (r > best_rate) {
0170             best_rate = r;
0171             req->rate = best_rate;
0172             req->best_parent_rate = parent_rate;
0173             if (best_rate == rate)
0174                 break;
0175         }
0176     }
0177 
0178     return 0;
0179 }
0180 
0181 static int davinci_pll_set_rate(struct clk_hw *hw, unsigned long rate,
0182                 unsigned long parent_rate)
0183 {
0184     struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
0185     u32 mult;
0186 
0187     mult = rate / parent_rate;
0188     writel(mult - 1, pll->base + PLLM);
0189 
0190     return 0;
0191 }
0192 
0193 #ifdef CONFIG_DEBUG_FS
0194 static void davinci_pll_debug_init(struct clk_hw *hw, struct dentry *dentry);
0195 #else
0196 #define davinci_pll_debug_init NULL
0197 #endif
0198 
0199 static const struct clk_ops davinci_pll_ops = {
0200     .recalc_rate    = davinci_pll_recalc_rate,
0201     .determine_rate = davinci_pll_determine_rate,
0202     .set_rate   = davinci_pll_set_rate,
0203     .debug_init = davinci_pll_debug_init,
0204 };
0205 
0206 /* PLLM works differently on DM365 */
0207 static unsigned long dm365_pll_recalc_rate(struct clk_hw *hw,
0208                        unsigned long parent_rate)
0209 {
0210     struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
0211     unsigned long rate = parent_rate;
0212     u32 mult;
0213 
0214     mult = readl(pll->base + PLLM) & pll->pllm_mask;
0215     rate *= mult * 2;
0216 
0217     return rate;
0218 }
0219 
0220 static const struct clk_ops dm365_pll_ops = {
0221     .recalc_rate    = dm365_pll_recalc_rate,
0222     .debug_init = davinci_pll_debug_init,
0223 };
0224 
0225 /**
0226  * davinci_pll_div_register - common *DIV clock implementation
0227  * @dev: The PLL platform device or NULL
0228  * @name: the clock name
0229  * @parent_name: the parent clock name
0230  * @reg: the *DIV register
0231  * @fixed: if true, the divider is a fixed value
0232  * @flags: bitmap of CLK_* flags from clock-provider.h
0233  */
0234 static struct clk *davinci_pll_div_register(struct device *dev,
0235                         const char *name,
0236                         const char *parent_name,
0237                         void __iomem *reg,
0238                         bool fixed, u32 flags)
0239 {
0240     const char * const *parent_names = parent_name ? &parent_name : NULL;
0241     int num_parents = parent_name ? 1 : 0;
0242     const struct clk_ops *divider_ops = &clk_divider_ops;
0243     struct clk_gate *gate;
0244     struct clk_divider *divider;
0245     struct clk *clk;
0246     int ret;
0247 
0248     gate = kzalloc(sizeof(*gate), GFP_KERNEL);
0249     if (!gate)
0250         return ERR_PTR(-ENOMEM);
0251 
0252     gate->reg = reg;
0253     gate->bit_idx = DIV_ENABLE_SHIFT;
0254 
0255     divider = kzalloc(sizeof(*divider), GFP_KERNEL);
0256     if (!divider) {
0257         ret = -ENOMEM;
0258         goto err_free_gate;
0259     }
0260 
0261     divider->reg = reg;
0262     divider->shift = DIV_RATIO_SHIFT;
0263     divider->width = DIV_RATIO_WIDTH;
0264 
0265     if (fixed) {
0266         divider->flags |= CLK_DIVIDER_READ_ONLY;
0267         divider_ops = &clk_divider_ro_ops;
0268     }
0269 
0270     clk = clk_register_composite(dev, name, parent_names, num_parents,
0271                      NULL, NULL, &divider->hw, divider_ops,
0272                      &gate->hw, &clk_gate_ops, flags);
0273     if (IS_ERR(clk)) {
0274         ret = PTR_ERR(clk);
0275         goto err_free_divider;
0276     }
0277 
0278     return clk;
0279 
0280 err_free_divider:
0281     kfree(divider);
0282 err_free_gate:
0283     kfree(gate);
0284 
0285     return ERR_PTR(ret);
0286 }
0287 
0288 struct davinci_pllen_clk {
0289     struct clk_hw hw;
0290     void __iomem *base;
0291 };
0292 
0293 #define to_davinci_pllen_clk(_hw) \
0294     container_of((_hw), struct davinci_pllen_clk, hw)
0295 
0296 static const struct clk_ops davinci_pllen_ops = {
0297     /* this clocks just uses the clock notification feature */
0298 };
0299 
0300 /*
0301  * The PLL has to be switched into bypass mode while we are chaning the rate,
0302  * so we do that on the PLLEN clock since it is the end of the line. This will
0303  * switch to bypass before any of the parent clocks (PREDIV, PLL, POSTDIV) are
0304  * changed and will switch back to the PLL after the changes have been made.
0305  */
0306 static int davinci_pllen_rate_change(struct notifier_block *nb,
0307                      unsigned long flags, void *data)
0308 {
0309     struct clk_notifier_data *cnd = data;
0310     struct clk_hw *hw = __clk_get_hw(cnd->clk);
0311     struct davinci_pllen_clk *pll = to_davinci_pllen_clk(hw);
0312     u32 ctrl;
0313 
0314     ctrl = readl(pll->base + PLLCTL);
0315 
0316     if (flags == PRE_RATE_CHANGE) {
0317         /* Switch the PLL to bypass mode */
0318         ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
0319         writel(ctrl, pll->base + PLLCTL);
0320 
0321         udelay(PLL_BYPASS_TIME);
0322 
0323         /* Reset and enable PLL */
0324         ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
0325         writel(ctrl, pll->base + PLLCTL);
0326     } else {
0327         udelay(PLL_RESET_TIME);
0328 
0329         /* Bring PLL out of reset */
0330         ctrl |= PLLCTL_PLLRST;
0331         writel(ctrl, pll->base + PLLCTL);
0332 
0333         udelay(PLL_LOCK_TIME);
0334 
0335         /* Remove PLL from bypass mode */
0336         ctrl |= PLLCTL_PLLEN;
0337         writel(ctrl, pll->base + PLLCTL);
0338     }
0339 
0340     return NOTIFY_OK;
0341 }
0342 
0343 static struct notifier_block davinci_pllen_notifier = {
0344     .notifier_call = davinci_pllen_rate_change,
0345 };
0346 
0347 /**
0348  * davinci_pll_clk_register - Register a PLL clock
0349  * @dev: The PLL platform device or NULL
0350  * @info: The device-specific clock info
0351  * @parent_name: The parent clock name
0352  * @base: The PLL's memory region
0353  * @cfgchip: CFGCHIP syscon regmap for info->unlock_reg or NULL
0354  *
0355  * This creates a series of clocks that represent the PLL.
0356  *
0357  *     OSCIN > [PREDIV >] PLL > [POSTDIV >] PLLEN
0358  *
0359  * - OSCIN is the parent clock (on secondary PLL, may come from primary PLL)
0360  * - PREDIV and POSTDIV are optional (depends on the PLL controller)
0361  * - PLL is the PLL output (aka PLLOUT)
0362  * - PLLEN is the bypass multiplexer
0363  *
0364  * Returns: The PLLOUT clock or a negative error code.
0365  */
0366 struct clk *davinci_pll_clk_register(struct device *dev,
0367                      const struct davinci_pll_clk_info *info,
0368                      const char *parent_name,
0369                      void __iomem *base,
0370                      struct regmap *cfgchip)
0371 {
0372     char prediv_name[MAX_NAME_SIZE];
0373     char pllout_name[MAX_NAME_SIZE];
0374     char postdiv_name[MAX_NAME_SIZE];
0375     char pllen_name[MAX_NAME_SIZE];
0376     struct clk_init_data init;
0377     struct davinci_pll_clk *pllout;
0378     struct davinci_pllen_clk *pllen;
0379     struct clk *oscin_clk = NULL;
0380     struct clk *prediv_clk = NULL;
0381     struct clk *pllout_clk;
0382     struct clk *postdiv_clk = NULL;
0383     struct clk *pllen_clk;
0384     int ret;
0385 
0386     if (info->flags & PLL_HAS_CLKMODE) {
0387         /*
0388          * If a PLL has PLLCTL[CLKMODE], then it is the primary PLL.
0389          * We register a clock named "oscin" that serves as the internal
0390          * "input clock" domain shared by both PLLs (if there are 2)
0391          * and will be the parent clock to the AUXCLK, SYSCLKBP and
0392          * OBSCLK domains. NB: The various TRMs use "OSCIN" to mean
0393          * a number of different things. In this driver we use it to
0394          * mean the signal after the PLLCTL[CLKMODE] switch.
0395          */
0396         oscin_clk = clk_register_fixed_factor(dev, OSCIN_CLK_NAME,
0397                               parent_name, 0, 1, 1);
0398         if (IS_ERR(oscin_clk))
0399             return oscin_clk;
0400 
0401         parent_name = OSCIN_CLK_NAME;
0402     }
0403 
0404     if (info->flags & PLL_HAS_PREDIV) {
0405         bool fixed = info->flags & PLL_PREDIV_FIXED_DIV;
0406         u32 flags = 0;
0407 
0408         snprintf(prediv_name, MAX_NAME_SIZE, "%s_prediv", info->name);
0409 
0410         if (info->flags & PLL_PREDIV_ALWAYS_ENABLED)
0411             flags |= CLK_IS_CRITICAL;
0412 
0413         /* Some? DM355 chips don't correctly report the PREDIV value */
0414         if (info->flags & PLL_PREDIV_FIXED8)
0415             prediv_clk = clk_register_fixed_factor(dev, prediv_name,
0416                             parent_name, flags, 1, 8);
0417         else
0418             prediv_clk = davinci_pll_div_register(dev, prediv_name,
0419                 parent_name, base + PREDIV, fixed, flags);
0420         if (IS_ERR(prediv_clk)) {
0421             ret = PTR_ERR(prediv_clk);
0422             goto err_unregister_oscin;
0423         }
0424 
0425         parent_name = prediv_name;
0426     }
0427 
0428     /* Unlock writing to PLL registers */
0429     if (info->unlock_reg) {
0430         if (IS_ERR_OR_NULL(cfgchip))
0431             dev_warn(dev, "Failed to get CFGCHIP (%ld)\n",
0432                  PTR_ERR(cfgchip));
0433         else
0434             regmap_write_bits(cfgchip, info->unlock_reg,
0435                       info->unlock_mask, 0);
0436     }
0437 
0438     pllout = kzalloc(sizeof(*pllout), GFP_KERNEL);
0439     if (!pllout) {
0440         ret = -ENOMEM;
0441         goto err_unregister_prediv;
0442     }
0443 
0444     snprintf(pllout_name, MAX_NAME_SIZE, "%s_pllout", info->name);
0445 
0446     init.name = pllout_name;
0447     if (info->flags & PLL_PLLM_2X)
0448         init.ops = &dm365_pll_ops;
0449     else
0450         init.ops = &davinci_pll_ops;
0451     init.parent_names = &parent_name;
0452     init.num_parents = 1;
0453     init.flags = 0;
0454 
0455     if (info->flags & PLL_HAS_PREDIV)
0456         init.flags |= CLK_SET_RATE_PARENT;
0457 
0458     pllout->hw.init = &init;
0459     pllout->base = base;
0460     pllout->pllm_mask = info->pllm_mask;
0461     pllout->pllm_min = info->pllm_min;
0462     pllout->pllm_max = info->pllm_max;
0463 
0464     pllout_clk = clk_register(dev, &pllout->hw);
0465     if (IS_ERR(pllout_clk)) {
0466         ret = PTR_ERR(pllout_clk);
0467         goto err_free_pllout;
0468     }
0469 
0470     clk_hw_set_rate_range(&pllout->hw, info->pllout_min_rate,
0471                   info->pllout_max_rate);
0472 
0473     parent_name = pllout_name;
0474 
0475     if (info->flags & PLL_HAS_POSTDIV) {
0476         bool fixed = info->flags & PLL_POSTDIV_FIXED_DIV;
0477         u32 flags = CLK_SET_RATE_PARENT;
0478 
0479         snprintf(postdiv_name, MAX_NAME_SIZE, "%s_postdiv", info->name);
0480 
0481         if (info->flags & PLL_POSTDIV_ALWAYS_ENABLED)
0482             flags |= CLK_IS_CRITICAL;
0483 
0484         postdiv_clk = davinci_pll_div_register(dev, postdiv_name,
0485                 parent_name, base + POSTDIV, fixed, flags);
0486         if (IS_ERR(postdiv_clk)) {
0487             ret = PTR_ERR(postdiv_clk);
0488             goto err_unregister_pllout;
0489         }
0490 
0491         parent_name = postdiv_name;
0492     }
0493 
0494     pllen = kzalloc(sizeof(*pllen), GFP_KERNEL);
0495     if (!pllen) {
0496         ret = -ENOMEM;
0497         goto err_unregister_postdiv;
0498     }
0499 
0500     snprintf(pllen_name, MAX_NAME_SIZE, "%s_pllen", info->name);
0501 
0502     init.name = pllen_name;
0503     init.ops = &davinci_pllen_ops;
0504     init.parent_names = &parent_name;
0505     init.num_parents = 1;
0506     init.flags = CLK_SET_RATE_PARENT;
0507 
0508     pllen->hw.init = &init;
0509     pllen->base = base;
0510 
0511     pllen_clk = clk_register(dev, &pllen->hw);
0512     if (IS_ERR(pllen_clk)) {
0513         ret = PTR_ERR(pllen_clk);
0514         goto err_free_pllen;
0515     }
0516 
0517     clk_notifier_register(pllen_clk, &davinci_pllen_notifier);
0518 
0519     return pllout_clk;
0520 
0521 err_free_pllen:
0522     kfree(pllen);
0523 err_unregister_postdiv:
0524     clk_unregister(postdiv_clk);
0525 err_unregister_pllout:
0526     clk_unregister(pllout_clk);
0527 err_free_pllout:
0528     kfree(pllout);
0529 err_unregister_prediv:
0530     clk_unregister(prediv_clk);
0531 err_unregister_oscin:
0532     clk_unregister(oscin_clk);
0533 
0534     return ERR_PTR(ret);
0535 }
0536 
0537 /**
0538  * davinci_pll_auxclk_register - Register bypass clock (AUXCLK)
0539  * @dev: The PLL platform device or NULL
0540  * @name: The clock name
0541  * @base: The PLL memory region
0542  */
0543 struct clk *davinci_pll_auxclk_register(struct device *dev,
0544                     const char *name,
0545                     void __iomem *base)
0546 {
0547     return clk_register_gate(dev, name, OSCIN_CLK_NAME, 0, base + CKEN,
0548                  CKEN_AUXEN_SHIFT, 0, NULL);
0549 }
0550 
0551 /**
0552  * davinci_pll_sysclkbp_clk_register - Register bypass divider clock (SYSCLKBP)
0553  * @dev: The PLL platform device or NULL
0554  * @name: The clock name
0555  * @base: The PLL memory region
0556  */
0557 struct clk *davinci_pll_sysclkbp_clk_register(struct device *dev,
0558                           const char *name,
0559                           void __iomem *base)
0560 {
0561     return clk_register_divider(dev, name, OSCIN_CLK_NAME, 0, base + BPDIV,
0562                     DIV_RATIO_SHIFT, DIV_RATIO_WIDTH,
0563                     CLK_DIVIDER_READ_ONLY, NULL);
0564 }
0565 
0566 /**
0567  * davinci_pll_obsclk_register - Register oscillator divider clock (OBSCLK)
0568  * @dev: The PLL platform device or NULL
0569  * @info: The clock info
0570  * @base: The PLL memory region
0571  */
0572 struct clk *
0573 davinci_pll_obsclk_register(struct device *dev,
0574                 const struct davinci_pll_obsclk_info *info,
0575                 void __iomem *base)
0576 {
0577     struct clk_mux *mux;
0578     struct clk_gate *gate;
0579     struct clk_divider *divider;
0580     struct clk *clk;
0581     u32 oscdiv;
0582     int ret;
0583 
0584     mux = kzalloc(sizeof(*mux), GFP_KERNEL);
0585     if (!mux)
0586         return ERR_PTR(-ENOMEM);
0587 
0588     mux->reg = base + OCSEL;
0589     mux->table = info->table;
0590     mux->mask = info->ocsrc_mask;
0591 
0592     gate = kzalloc(sizeof(*gate), GFP_KERNEL);
0593     if (!gate) {
0594         ret = -ENOMEM;
0595         goto err_free_mux;
0596     }
0597 
0598     gate->reg = base + CKEN;
0599     gate->bit_idx = CKEN_OBSCLK_SHIFT;
0600 
0601     divider = kzalloc(sizeof(*divider), GFP_KERNEL);
0602     if (!divider) {
0603         ret = -ENOMEM;
0604         goto err_free_gate;
0605     }
0606 
0607     divider->reg = base + OSCDIV;
0608     divider->shift = DIV_RATIO_SHIFT;
0609     divider->width = DIV_RATIO_WIDTH;
0610 
0611     /* make sure divider is enabled just in case bootloader disabled it */
0612     oscdiv = readl(base + OSCDIV);
0613     oscdiv |= BIT(DIV_ENABLE_SHIFT);
0614     writel(oscdiv, base + OSCDIV);
0615 
0616     clk = clk_register_composite(dev, info->name, info->parent_names,
0617                      info->num_parents,
0618                      &mux->hw, &clk_mux_ops,
0619                      &divider->hw, &clk_divider_ops,
0620                      &gate->hw, &clk_gate_ops, 0);
0621 
0622     if (IS_ERR(clk)) {
0623         ret = PTR_ERR(clk);
0624         goto err_free_divider;
0625     }
0626 
0627     return clk;
0628 
0629 err_free_divider:
0630     kfree(divider);
0631 err_free_gate:
0632     kfree(gate);
0633 err_free_mux:
0634     kfree(mux);
0635 
0636     return ERR_PTR(ret);
0637 }
0638 
0639 /* The PLL SYSCLKn clocks have a mechanism for synchronizing rate changes. */
0640 static int davinci_pll_sysclk_rate_change(struct notifier_block *nb,
0641                       unsigned long flags, void *data)
0642 {
0643     struct clk_notifier_data *cnd = data;
0644     struct clk_hw *hw = __clk_get_hw(clk_get_parent(cnd->clk));
0645     struct davinci_pllen_clk *pll = to_davinci_pllen_clk(hw);
0646     u32 pllcmd, pllstat;
0647 
0648     switch (flags) {
0649     case POST_RATE_CHANGE:
0650         /* apply the changes */
0651         pllcmd = readl(pll->base + PLLCMD);
0652         pllcmd |= PLLCMD_GOSET;
0653         writel(pllcmd, pll->base + PLLCMD);
0654         fallthrough;
0655     case PRE_RATE_CHANGE:
0656         /* Wait until for outstanding changes to take effect */
0657         do {
0658             pllstat = readl(pll->base + PLLSTAT);
0659         } while (pllstat & PLLSTAT_GOSTAT);
0660         break;
0661     }
0662 
0663     return NOTIFY_OK;
0664 }
0665 
0666 static struct notifier_block davinci_pll_sysclk_notifier = {
0667     .notifier_call = davinci_pll_sysclk_rate_change,
0668 };
0669 
0670 /**
0671  * davinci_pll_sysclk_register - Register divider clocks (SYSCLKn)
0672  * @dev: The PLL platform device or NULL
0673  * @info: The clock info
0674  * @base: The PLL memory region
0675  */
0676 struct clk *
0677 davinci_pll_sysclk_register(struct device *dev,
0678                 const struct davinci_pll_sysclk_info *info,
0679                 void __iomem *base)
0680 {
0681     const struct clk_ops *divider_ops = &clk_divider_ops;
0682     struct clk_gate *gate;
0683     struct clk_divider *divider;
0684     struct clk *clk;
0685     u32 reg;
0686     u32 flags = 0;
0687     int ret;
0688 
0689     /* PLLDIVn registers are not entirely consecutive */
0690     if (info->id < 4)
0691         reg = PLLDIV1 + 4 * (info->id - 1);
0692     else
0693         reg = PLLDIV4 + 4 * (info->id - 4);
0694 
0695     gate = kzalloc(sizeof(*gate), GFP_KERNEL);
0696     if (!gate)
0697         return ERR_PTR(-ENOMEM);
0698 
0699     gate->reg = base + reg;
0700     gate->bit_idx = DIV_ENABLE_SHIFT;
0701 
0702     divider = kzalloc(sizeof(*divider), GFP_KERNEL);
0703     if (!divider) {
0704         ret = -ENOMEM;
0705         goto err_free_gate;
0706     }
0707 
0708     divider->reg = base + reg;
0709     divider->shift = DIV_RATIO_SHIFT;
0710     divider->width = info->ratio_width;
0711     divider->flags = 0;
0712 
0713     if (info->flags & SYSCLK_FIXED_DIV) {
0714         divider->flags |= CLK_DIVIDER_READ_ONLY;
0715         divider_ops = &clk_divider_ro_ops;
0716     }
0717 
0718     /* Only the ARM clock can change the parent PLL rate */
0719     if (info->flags & SYSCLK_ARM_RATE)
0720         flags |= CLK_SET_RATE_PARENT;
0721 
0722     if (info->flags & SYSCLK_ALWAYS_ENABLED)
0723         flags |= CLK_IS_CRITICAL;
0724 
0725     clk = clk_register_composite(dev, info->name, &info->parent_name, 1,
0726                      NULL, NULL, &divider->hw, divider_ops,
0727                      &gate->hw, &clk_gate_ops, flags);
0728     if (IS_ERR(clk)) {
0729         ret = PTR_ERR(clk);
0730         goto err_free_divider;
0731     }
0732 
0733     clk_notifier_register(clk, &davinci_pll_sysclk_notifier);
0734 
0735     return clk;
0736 
0737 err_free_divider:
0738     kfree(divider);
0739 err_free_gate:
0740     kfree(gate);
0741 
0742     return ERR_PTR(ret);
0743 }
0744 
0745 int of_davinci_pll_init(struct device *dev, struct device_node *node,
0746             const struct davinci_pll_clk_info *info,
0747             const struct davinci_pll_obsclk_info *obsclk_info,
0748             const struct davinci_pll_sysclk_info **div_info,
0749             u8 max_sysclk_id,
0750             void __iomem *base,
0751             struct regmap *cfgchip)
0752 {
0753     struct device_node *child;
0754     const char *parent_name;
0755     struct clk *clk;
0756 
0757     if (info->flags & PLL_HAS_CLKMODE)
0758         parent_name = of_clk_get_parent_name(node, 0);
0759     else
0760         parent_name = OSCIN_CLK_NAME;
0761 
0762     clk = davinci_pll_clk_register(dev, info, parent_name, base, cfgchip);
0763     if (IS_ERR(clk)) {
0764         dev_err(dev, "failed to register %s\n", info->name);
0765         return PTR_ERR(clk);
0766     }
0767 
0768     child = of_get_child_by_name(node, "pllout");
0769     if (of_device_is_available(child))
0770         of_clk_add_provider(child, of_clk_src_simple_get, clk);
0771     of_node_put(child);
0772 
0773     child = of_get_child_by_name(node, "sysclk");
0774     if (of_device_is_available(child)) {
0775         struct clk_onecell_data *clk_data;
0776         struct clk **clks;
0777         int n_clks =  max_sysclk_id + 1;
0778         int i;
0779 
0780         clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
0781         if (!clk_data) {
0782             of_node_put(child);
0783             return -ENOMEM;
0784         }
0785 
0786         clks = kmalloc_array(n_clks, sizeof(*clks), GFP_KERNEL);
0787         if (!clks) {
0788             kfree(clk_data);
0789             of_node_put(child);
0790             return -ENOMEM;
0791         }
0792 
0793         clk_data->clks = clks;
0794         clk_data->clk_num = n_clks;
0795 
0796         for (i = 0; i < n_clks; i++)
0797             clks[i] = ERR_PTR(-ENOENT);
0798 
0799         for (; *div_info; div_info++) {
0800             clk = davinci_pll_sysclk_register(dev, *div_info, base);
0801             if (IS_ERR(clk))
0802                 dev_warn(dev, "failed to register %s (%ld)\n",
0803                      (*div_info)->name, PTR_ERR(clk));
0804             else
0805                 clks[(*div_info)->id] = clk;
0806         }
0807         of_clk_add_provider(child, of_clk_src_onecell_get, clk_data);
0808     }
0809     of_node_put(child);
0810 
0811     child = of_get_child_by_name(node, "auxclk");
0812     if (of_device_is_available(child)) {
0813         char child_name[MAX_NAME_SIZE];
0814 
0815         snprintf(child_name, MAX_NAME_SIZE, "%s_auxclk", info->name);
0816 
0817         clk = davinci_pll_auxclk_register(dev, child_name, base);
0818         if (IS_ERR(clk))
0819             dev_warn(dev, "failed to register %s (%ld)\n",
0820                  child_name, PTR_ERR(clk));
0821         else
0822             of_clk_add_provider(child, of_clk_src_simple_get, clk);
0823     }
0824     of_node_put(child);
0825 
0826     child = of_get_child_by_name(node, "obsclk");
0827     if (of_device_is_available(child)) {
0828         if (obsclk_info)
0829             clk = davinci_pll_obsclk_register(dev, obsclk_info, base);
0830         else
0831             clk = ERR_PTR(-EINVAL);
0832 
0833         if (IS_ERR(clk))
0834             dev_warn(dev, "failed to register obsclk (%ld)\n",
0835                  PTR_ERR(clk));
0836         else
0837             of_clk_add_provider(child, of_clk_src_simple_get, clk);
0838     }
0839     of_node_put(child);
0840 
0841     return 0;
0842 }
0843 
0844 static struct davinci_pll_platform_data *davinci_pll_get_pdata(struct device *dev)
0845 {
0846     struct davinci_pll_platform_data *pdata = dev_get_platdata(dev);
0847 
0848     /*
0849      * Platform data is optional, so allocate a new struct if one was not
0850      * provided. For device tree, this will always be the case.
0851      */
0852     if (!pdata)
0853         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
0854     if (!pdata)
0855         return NULL;
0856 
0857     /* for device tree, we need to fill in the struct */
0858     if (dev->of_node)
0859         pdata->cfgchip =
0860             syscon_regmap_lookup_by_compatible("ti,da830-cfgchip");
0861 
0862     return pdata;
0863 }
0864 
0865 /* needed in early boot for clocksource/clockevent */
0866 #ifdef CONFIG_ARCH_DAVINCI_DA850
0867 CLK_OF_DECLARE(da850_pll0, "ti,da850-pll0", of_da850_pll0_init);
0868 #endif
0869 
0870 static const struct of_device_id davinci_pll_of_match[] = {
0871 #ifdef CONFIG_ARCH_DAVINCI_DA850
0872     { .compatible = "ti,da850-pll1", .data = of_da850_pll1_init },
0873 #endif
0874     { }
0875 };
0876 
0877 static const struct platform_device_id davinci_pll_id_table[] = {
0878 #ifdef CONFIG_ARCH_DAVINCI_DA830
0879     { .name = "da830-pll",   .driver_data = (kernel_ulong_t)da830_pll_init   },
0880 #endif
0881 #ifdef CONFIG_ARCH_DAVINCI_DA850
0882     { .name = "da850-pll0",  .driver_data = (kernel_ulong_t)da850_pll0_init  },
0883     { .name = "da850-pll1",  .driver_data = (kernel_ulong_t)da850_pll1_init  },
0884 #endif
0885 #ifdef CONFIG_ARCH_DAVINCI_DM355
0886     { .name = "dm355-pll1",  .driver_data = (kernel_ulong_t)dm355_pll1_init  },
0887     { .name = "dm355-pll2",  .driver_data = (kernel_ulong_t)dm355_pll2_init  },
0888 #endif
0889 #ifdef CONFIG_ARCH_DAVINCI_DM365
0890     { .name = "dm365-pll1",  .driver_data = (kernel_ulong_t)dm365_pll1_init  },
0891     { .name = "dm365-pll2",  .driver_data = (kernel_ulong_t)dm365_pll2_init  },
0892 #endif
0893 #ifdef CONFIG_ARCH_DAVINCI_DM644x
0894     { .name = "dm644x-pll1", .driver_data = (kernel_ulong_t)dm644x_pll1_init },
0895     { .name = "dm644x-pll2", .driver_data = (kernel_ulong_t)dm644x_pll2_init },
0896 #endif
0897 #ifdef CONFIG_ARCH_DAVINCI_DM646x
0898     { .name = "dm646x-pll1", .driver_data = (kernel_ulong_t)dm646x_pll1_init },
0899     { .name = "dm646x-pll2", .driver_data = (kernel_ulong_t)dm646x_pll2_init },
0900 #endif
0901     { }
0902 };
0903 
0904 typedef int (*davinci_pll_init)(struct device *dev, void __iomem *base,
0905                 struct regmap *cfgchip);
0906 
0907 static int davinci_pll_probe(struct platform_device *pdev)
0908 {
0909     struct device *dev = &pdev->dev;
0910     struct davinci_pll_platform_data *pdata;
0911     const struct of_device_id *of_id;
0912     davinci_pll_init pll_init = NULL;
0913     void __iomem *base;
0914 
0915     of_id = of_match_device(davinci_pll_of_match, dev);
0916     if (of_id)
0917         pll_init = of_id->data;
0918     else if (pdev->id_entry)
0919         pll_init = (void *)pdev->id_entry->driver_data;
0920 
0921     if (!pll_init) {
0922         dev_err(dev, "unable to find driver data\n");
0923         return -EINVAL;
0924     }
0925 
0926     pdata = davinci_pll_get_pdata(dev);
0927     if (!pdata) {
0928         dev_err(dev, "missing platform data\n");
0929         return -EINVAL;
0930     }
0931 
0932     base = devm_platform_ioremap_resource(pdev, 0);
0933     if (IS_ERR(base))
0934         return PTR_ERR(base);
0935 
0936     return pll_init(dev, base, pdata->cfgchip);
0937 }
0938 
0939 static struct platform_driver davinci_pll_driver = {
0940     .probe      = davinci_pll_probe,
0941     .driver     = {
0942         .name       = "davinci-pll-clk",
0943         .of_match_table = davinci_pll_of_match,
0944     },
0945     .id_table   = davinci_pll_id_table,
0946 };
0947 
0948 static int __init davinci_pll_driver_init(void)
0949 {
0950     return platform_driver_register(&davinci_pll_driver);
0951 }
0952 
0953 /* has to be postcore_initcall because PSC devices depend on PLL parent clocks */
0954 postcore_initcall(davinci_pll_driver_init);
0955 
0956 #ifdef CONFIG_DEBUG_FS
0957 #include <linux/debugfs.h>
0958 
0959 #define DEBUG_REG(n)    \
0960 {           \
0961     .name   = #n,   \
0962     .offset = n,    \
0963 }
0964 
0965 static const struct debugfs_reg32 davinci_pll_regs[] = {
0966     DEBUG_REG(REVID),
0967     DEBUG_REG(PLLCTL),
0968     DEBUG_REG(OCSEL),
0969     DEBUG_REG(PLLSECCTL),
0970     DEBUG_REG(PLLM),
0971     DEBUG_REG(PREDIV),
0972     DEBUG_REG(PLLDIV1),
0973     DEBUG_REG(PLLDIV2),
0974     DEBUG_REG(PLLDIV3),
0975     DEBUG_REG(OSCDIV),
0976     DEBUG_REG(POSTDIV),
0977     DEBUG_REG(BPDIV),
0978     DEBUG_REG(PLLCMD),
0979     DEBUG_REG(PLLSTAT),
0980     DEBUG_REG(ALNCTL),
0981     DEBUG_REG(DCHANGE),
0982     DEBUG_REG(CKEN),
0983     DEBUG_REG(CKSTAT),
0984     DEBUG_REG(SYSTAT),
0985     DEBUG_REG(PLLDIV4),
0986     DEBUG_REG(PLLDIV5),
0987     DEBUG_REG(PLLDIV6),
0988     DEBUG_REG(PLLDIV7),
0989     DEBUG_REG(PLLDIV8),
0990     DEBUG_REG(PLLDIV9),
0991 };
0992 
0993 static void davinci_pll_debug_init(struct clk_hw *hw, struct dentry *dentry)
0994 {
0995     struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
0996     struct debugfs_regset32 *regset;
0997 
0998     regset = kzalloc(sizeof(*regset), GFP_KERNEL);
0999     if (!regset)
1000         return;
1001 
1002     regset->regs = davinci_pll_regs;
1003     regset->nregs = ARRAY_SIZE(davinci_pll_regs);
1004     regset->base = pll->base;
1005 
1006     debugfs_create_regset32("registers", 0400, dentry, regset);
1007 }
1008 #endif