Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 2016 Maxime Ripard
0004  *
0005  * Maxime Ripard <maxime.ripard@free-electrons.com>
0006  */
0007 
0008 #include <linux/clk.h>
0009 #include <linux/clk-provider.h>
0010 #include <linux/device.h>
0011 #include <linux/iopoll.h>
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 
0015 #include "ccu_common.h"
0016 #include "ccu_gate.h"
0017 #include "ccu_reset.h"
0018 
0019 struct sunxi_ccu {
0020     const struct sunxi_ccu_desc *desc;
0021     spinlock_t          lock;
0022     struct ccu_reset        reset;
0023 };
0024 
0025 void ccu_helper_wait_for_lock(struct ccu_common *common, u32 lock)
0026 {
0027     void __iomem *addr;
0028     u32 reg;
0029 
0030     if (!lock)
0031         return;
0032 
0033     if (common->features & CCU_FEATURE_LOCK_REG)
0034         addr = common->base + common->lock_reg;
0035     else
0036         addr = common->base + common->reg;
0037 
0038     WARN_ON(readl_relaxed_poll_timeout(addr, reg, reg & lock, 100, 70000));
0039 }
0040 EXPORT_SYMBOL_NS_GPL(ccu_helper_wait_for_lock, SUNXI_CCU);
0041 
0042 /*
0043  * This clock notifier is called when the frequency of a PLL clock is
0044  * changed. In common PLL designs, changes to the dividers take effect
0045  * almost immediately, while changes to the multipliers (implemented
0046  * as dividers in the feedback loop) take a few cycles to work into
0047  * the feedback loop for the PLL to stablize.
0048  *
0049  * Sometimes when the PLL clock rate is changed, the decrease in the
0050  * divider is too much for the decrease in the multiplier to catch up.
0051  * The PLL clock rate will spike, and in some cases, might lock up
0052  * completely.
0053  *
0054  * This notifier callback will gate and then ungate the clock,
0055  * effectively resetting it, so it proceeds to work. Care must be
0056  * taken to reparent consumers to other temporary clocks during the
0057  * rate change, and that this notifier callback must be the first
0058  * to be registered.
0059  */
0060 static int ccu_pll_notifier_cb(struct notifier_block *nb,
0061                    unsigned long event, void *data)
0062 {
0063     struct ccu_pll_nb *pll = to_ccu_pll_nb(nb);
0064     int ret = 0;
0065 
0066     if (event != POST_RATE_CHANGE)
0067         goto out;
0068 
0069     ccu_gate_helper_disable(pll->common, pll->enable);
0070 
0071     ret = ccu_gate_helper_enable(pll->common, pll->enable);
0072     if (ret)
0073         goto out;
0074 
0075     ccu_helper_wait_for_lock(pll->common, pll->lock);
0076 
0077 out:
0078     return notifier_from_errno(ret);
0079 }
0080 
0081 int ccu_pll_notifier_register(struct ccu_pll_nb *pll_nb)
0082 {
0083     pll_nb->clk_nb.notifier_call = ccu_pll_notifier_cb;
0084 
0085     return clk_notifier_register(pll_nb->common->hw.clk,
0086                      &pll_nb->clk_nb);
0087 }
0088 EXPORT_SYMBOL_NS_GPL(ccu_pll_notifier_register, SUNXI_CCU);
0089 
0090 static int sunxi_ccu_probe(struct sunxi_ccu *ccu, struct device *dev,
0091                struct device_node *node, void __iomem *reg,
0092                const struct sunxi_ccu_desc *desc)
0093 {
0094     struct ccu_reset *reset;
0095     int i, ret;
0096 
0097     ccu->desc = desc;
0098 
0099     spin_lock_init(&ccu->lock);
0100 
0101     for (i = 0; i < desc->num_ccu_clks; i++) {
0102         struct ccu_common *cclk = desc->ccu_clks[i];
0103 
0104         if (!cclk)
0105             continue;
0106 
0107         cclk->base = reg;
0108         cclk->lock = &ccu->lock;
0109     }
0110 
0111     for (i = 0; i < desc->hw_clks->num ; i++) {
0112         struct clk_hw *hw = desc->hw_clks->hws[i];
0113         const char *name;
0114 
0115         if (!hw)
0116             continue;
0117 
0118         name = hw->init->name;
0119         if (dev)
0120             ret = clk_hw_register(dev, hw);
0121         else
0122             ret = of_clk_hw_register(node, hw);
0123         if (ret) {
0124             pr_err("Couldn't register clock %d - %s\n", i, name);
0125             goto err_clk_unreg;
0126         }
0127     }
0128 
0129     ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get,
0130                      desc->hw_clks);
0131     if (ret)
0132         goto err_clk_unreg;
0133 
0134     reset = &ccu->reset;
0135     reset->rcdev.of_node = node;
0136     reset->rcdev.ops = &ccu_reset_ops;
0137     reset->rcdev.owner = dev ? dev->driver->owner : THIS_MODULE;
0138     reset->rcdev.nr_resets = desc->num_resets;
0139     reset->base = reg;
0140     reset->lock = &ccu->lock;
0141     reset->reset_map = desc->resets;
0142 
0143     ret = reset_controller_register(&reset->rcdev);
0144     if (ret)
0145         goto err_del_provider;
0146 
0147     return 0;
0148 
0149 err_del_provider:
0150     of_clk_del_provider(node);
0151 err_clk_unreg:
0152     while (--i >= 0) {
0153         struct clk_hw *hw = desc->hw_clks->hws[i];
0154 
0155         if (!hw)
0156             continue;
0157         clk_hw_unregister(hw);
0158     }
0159     return ret;
0160 }
0161 
0162 static void devm_sunxi_ccu_release(struct device *dev, void *res)
0163 {
0164     struct sunxi_ccu *ccu = res;
0165     const struct sunxi_ccu_desc *desc = ccu->desc;
0166     int i;
0167 
0168     reset_controller_unregister(&ccu->reset.rcdev);
0169     of_clk_del_provider(dev->of_node);
0170 
0171     for (i = 0; i < desc->hw_clks->num; i++) {
0172         struct clk_hw *hw = desc->hw_clks->hws[i];
0173 
0174         if (!hw)
0175             continue;
0176         clk_hw_unregister(hw);
0177     }
0178 }
0179 
0180 int devm_sunxi_ccu_probe(struct device *dev, void __iomem *reg,
0181              const struct sunxi_ccu_desc *desc)
0182 {
0183     struct sunxi_ccu *ccu;
0184     int ret;
0185 
0186     ccu = devres_alloc(devm_sunxi_ccu_release, sizeof(*ccu), GFP_KERNEL);
0187     if (!ccu)
0188         return -ENOMEM;
0189 
0190     ret = sunxi_ccu_probe(ccu, dev, dev->of_node, reg, desc);
0191     if (ret) {
0192         devres_free(ccu);
0193         return ret;
0194     }
0195 
0196     devres_add(dev, ccu);
0197 
0198     return 0;
0199 }
0200 EXPORT_SYMBOL_NS_GPL(devm_sunxi_ccu_probe, SUNXI_CCU);
0201 
0202 void of_sunxi_ccu_probe(struct device_node *node, void __iomem *reg,
0203             const struct sunxi_ccu_desc *desc)
0204 {
0205     struct sunxi_ccu *ccu;
0206     int ret;
0207 
0208     ccu = kzalloc(sizeof(*ccu), GFP_KERNEL);
0209     if (!ccu)
0210         return;
0211 
0212     ret = sunxi_ccu_probe(ccu, NULL, node, reg, desc);
0213     if (ret) {
0214         pr_err("%pOF: probing clocks failed: %d\n", node, ret);
0215         kfree(ccu);
0216     }
0217 }
0218 
0219 MODULE_LICENSE("GPL");