1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2016 Maxime Ripard
9 #include <linux/clk-provider.h>
10 #include <linux/device.h>
11 #include <linux/iopoll.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
15 #include "ccu_common.h"
17 #include "ccu_reset.h"
20 const struct sunxi_ccu_desc *desc;
22 struct ccu_reset reset;
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));
40 EXPORT_SYMBOL_NS_GPL(ccu_helper_wait_for_lock, SUNXI_CCU);
43 * This clock notifier is called when the frequency of a PLL clock is
44 * changed. In common PLL designs, changes to the dividers take effect
45 * almost immediately, while changes to the multipliers (implemented
46 * as dividers in the feedback loop) take a few cycles to work into
47 * the feedback loop for the PLL to stablize.
49 * Sometimes when the PLL clock rate is changed, the decrease in the
50 * divider is too much for the decrease in the multiplier to catch up.
51 * The PLL clock rate will spike, and in some cases, might lock up
54 * This notifier callback will gate and then ungate the clock,
55 * effectively resetting it, so it proceeds to work. Care must be
56 * taken to reparent consumers to other temporary clocks during the
57 * rate change, and that this notifier callback must be the first
60 static int ccu_pll_notifier_cb(struct notifier_block *nb,
61 unsigned long event, void *data)
63 struct ccu_pll_nb *pll = to_ccu_pll_nb(nb);
66 if (event != POST_RATE_CHANGE)
69 ccu_gate_helper_disable(pll->common, pll->enable);
71 ret = ccu_gate_helper_enable(pll->common, pll->enable);
75 ccu_helper_wait_for_lock(pll->common, pll->lock);
78 return notifier_from_errno(ret);
81 int ccu_pll_notifier_register(struct ccu_pll_nb *pll_nb)
83 pll_nb->clk_nb.notifier_call = ccu_pll_notifier_cb;
85 return clk_notifier_register(pll_nb->common->hw.clk,
88 EXPORT_SYMBOL_NS_GPL(ccu_pll_notifier_register, SUNXI_CCU);
90 static int sunxi_ccu_probe(struct sunxi_ccu *ccu, struct device *dev,
91 struct device_node *node, void __iomem *reg,
92 const struct sunxi_ccu_desc *desc)
94 struct ccu_reset *reset;
99 spin_lock_init(&ccu->lock);
101 for (i = 0; i < desc->num_ccu_clks; i++) {
102 struct ccu_common *cclk = desc->ccu_clks[i];
108 cclk->lock = &ccu->lock;
111 for (i = 0; i < desc->hw_clks->num ; i++) {
112 struct clk_hw *hw = desc->hw_clks->hws[i];
118 name = hw->init->name;
120 ret = clk_hw_register(dev, hw);
122 ret = of_clk_hw_register(node, hw);
124 pr_err("Couldn't register clock %d - %s\n", i, name);
129 ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get,
135 reset->rcdev.of_node = node;
136 reset->rcdev.ops = &ccu_reset_ops;
137 reset->rcdev.owner = dev ? dev->driver->owner : THIS_MODULE;
138 reset->rcdev.nr_resets = desc->num_resets;
140 reset->lock = &ccu->lock;
141 reset->reset_map = desc->resets;
143 ret = reset_controller_register(&reset->rcdev);
145 goto err_del_provider;
150 of_clk_del_provider(node);
153 struct clk_hw *hw = desc->hw_clks->hws[i];
157 clk_hw_unregister(hw);
162 static void devm_sunxi_ccu_release(struct device *dev, void *res)
164 struct sunxi_ccu *ccu = res;
165 const struct sunxi_ccu_desc *desc = ccu->desc;
168 reset_controller_unregister(&ccu->reset.rcdev);
169 of_clk_del_provider(dev->of_node);
171 for (i = 0; i < desc->hw_clks->num; i++) {
172 struct clk_hw *hw = desc->hw_clks->hws[i];
176 clk_hw_unregister(hw);
180 int devm_sunxi_ccu_probe(struct device *dev, void __iomem *reg,
181 const struct sunxi_ccu_desc *desc)
183 struct sunxi_ccu *ccu;
186 ccu = devres_alloc(devm_sunxi_ccu_release, sizeof(*ccu), GFP_KERNEL);
190 ret = sunxi_ccu_probe(ccu, dev, dev->of_node, reg, desc);
196 devres_add(dev, ccu);
200 EXPORT_SYMBOL_NS_GPL(devm_sunxi_ccu_probe, SUNXI_CCU);
202 void of_sunxi_ccu_probe(struct device_node *node, void __iomem *reg,
203 const struct sunxi_ccu_desc *desc)
205 struct sunxi_ccu *ccu;
208 ccu = kzalloc(sizeof(*ccu), GFP_KERNEL);
212 ret = sunxi_ccu_probe(ccu, NULL, node, reg, desc);
214 pr_err("%pOF: probing clocks failed: %d\n", node, ret);
219 MODULE_LICENSE("GPL");