1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2016 Maxime Ripard
5 * Maxime Ripard <maxime.ripard@free-electrons.com>
9 #include <linux/clk-provider.h>
10 #include <linux/device.h>
11 #include <linux/iopoll.h>
12 #include <linux/slab.h>
14 #include "ccu_common.h"
16 #include "ccu_reset.h"
19 const struct sunxi_ccu_desc *desc;
20 struct ccu_reset reset;
23 static DEFINE_SPINLOCK(ccu_lock);
25 void ccu_helper_wait_for_lock(struct ccu_common *common, u32 lock)
33 if (common->features & CCU_FEATURE_LOCK_REG)
34 addr = common->base + common->lock_reg;
36 addr = common->base + common->reg;
38 WARN_ON(readl_relaxed_poll_timeout(addr, reg, reg & lock, 100, 70000));
42 * This clock notifier is called when the frequency of a PLL clock is
43 * changed. In common PLL designs, changes to the dividers take effect
44 * almost immediately, while changes to the multipliers (implemented
45 * as dividers in the feedback loop) take a few cycles to work into
46 * the feedback loop for the PLL to stablize.
48 * Sometimes when the PLL clock rate is changed, the decrease in the
49 * divider is too much for the decrease in the multiplier to catch up.
50 * The PLL clock rate will spike, and in some cases, might lock up
53 * This notifier callback will gate and then ungate the clock,
54 * effectively resetting it, so it proceeds to work. Care must be
55 * taken to reparent consumers to other temporary clocks during the
56 * rate change, and that this notifier callback must be the first
59 static int ccu_pll_notifier_cb(struct notifier_block *nb,
60 unsigned long event, void *data)
62 struct ccu_pll_nb *pll = to_ccu_pll_nb(nb);
65 if (event != POST_RATE_CHANGE)
68 ccu_gate_helper_disable(pll->common, pll->enable);
70 ret = ccu_gate_helper_enable(pll->common, pll->enable);
74 ccu_helper_wait_for_lock(pll->common, pll->lock);
77 return notifier_from_errno(ret);
80 int ccu_pll_notifier_register(struct ccu_pll_nb *pll_nb)
82 pll_nb->clk_nb.notifier_call = ccu_pll_notifier_cb;
84 return clk_notifier_register(pll_nb->common->hw.clk,
88 static int sunxi_ccu_probe(struct sunxi_ccu *ccu, struct device *dev,
89 struct device_node *node, void __iomem *reg,
90 const struct sunxi_ccu_desc *desc)
92 struct ccu_reset *reset;
97 for (i = 0; i < desc->num_ccu_clks; i++) {
98 struct ccu_common *cclk = desc->ccu_clks[i];
104 cclk->lock = &ccu_lock;
107 for (i = 0; i < desc->hw_clks->num ; i++) {
108 struct clk_hw *hw = desc->hw_clks->hws[i];
114 name = hw->init->name;
116 ret = clk_hw_register(dev, hw);
118 ret = of_clk_hw_register(node, hw);
120 pr_err("Couldn't register clock %d - %s\n", i, name);
125 ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get,
131 reset->rcdev.of_node = node;
132 reset->rcdev.ops = &ccu_reset_ops;
133 reset->rcdev.owner = dev ? dev->driver->owner : THIS_MODULE;
134 reset->rcdev.nr_resets = desc->num_resets;
136 reset->lock = &ccu_lock;
137 reset->reset_map = desc->resets;
139 ret = reset_controller_register(&reset->rcdev);
141 goto err_del_provider;
146 of_clk_del_provider(node);
149 struct clk_hw *hw = desc->hw_clks->hws[i];
153 clk_hw_unregister(hw);
158 static void devm_sunxi_ccu_release(struct device *dev, void *res)
160 struct sunxi_ccu *ccu = res;
161 const struct sunxi_ccu_desc *desc = ccu->desc;
164 reset_controller_unregister(&ccu->reset.rcdev);
165 of_clk_del_provider(dev->of_node);
167 for (i = 0; i < desc->hw_clks->num; i++) {
168 struct clk_hw *hw = desc->hw_clks->hws[i];
172 clk_hw_unregister(hw);
176 int devm_sunxi_ccu_probe(struct device *dev, void __iomem *reg,
177 const struct sunxi_ccu_desc *desc)
179 struct sunxi_ccu *ccu;
182 ccu = devres_alloc(devm_sunxi_ccu_release, sizeof(*ccu), GFP_KERNEL);
186 ret = sunxi_ccu_probe(ccu, dev, dev->of_node, reg, desc);
192 devres_add(dev, ccu);
197 void of_sunxi_ccu_probe(struct device_node *node, void __iomem *reg,
198 const struct sunxi_ccu_desc *desc)
200 struct sunxi_ccu *ccu;
203 ccu = kzalloc(sizeof(*ccu), GFP_KERNEL);
207 ret = sunxi_ccu_probe(ccu, NULL, node, reg, desc);
209 pr_err("%pOF: probing clocks failed: %d\n", node, ret);