1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
5 #include <linux/module.h>
6 #include <linux/clk-provider.h>
7 #include <linux/slab.h>
10 #include <linux/platform_device.h>
13 * DOC: basic fixed multiplier and divider clock that cannot gate
15 * Traits of this clock:
16 * prepare - clk_prepare only ensures that parents are prepared
17 * enable - clk_enable only ensures that parents are enabled
18 * rate - rate is fixed. clk->rate = parent->rate / div * mult
19 * parent - fixed parent. No clk_set_parent support
22 static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
23 unsigned long parent_rate)
25 struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
26 unsigned long long int rate;
28 rate = (unsigned long long int)parent_rate * fix->mult;
29 do_div(rate, fix->div);
30 return (unsigned long)rate;
33 static long clk_factor_round_rate(struct clk_hw *hw, unsigned long rate,
36 struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
38 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
39 unsigned long best_parent;
41 best_parent = (rate / fix->mult) * fix->div;
42 *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
45 return (*prate / fix->div) * fix->mult;
48 static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate,
49 unsigned long parent_rate)
52 * We must report success but we can do so unconditionally because
53 * clk_factor_round_rate returns values that ensure this call is a
60 const struct clk_ops clk_fixed_factor_ops = {
61 .round_rate = clk_factor_round_rate,
62 .set_rate = clk_factor_set_rate,
63 .recalc_rate = clk_factor_recalc_rate,
65 EXPORT_SYMBOL_GPL(clk_fixed_factor_ops);
67 static void devm_clk_hw_register_fixed_factor_release(struct device *dev, void *res)
69 struct clk_fixed_factor *fix = res;
72 * We can not use clk_hw_unregister_fixed_factor, since it will kfree()
73 * the hw, resulting in double free. Just unregister the hw and let
74 * devres code kfree() it.
76 clk_hw_unregister(&fix->hw);
79 static struct clk_hw *
80 __clk_hw_register_fixed_factor(struct device *dev, struct device_node *np,
81 const char *name, const char *parent_name,
82 const struct clk_hw *parent_hw, int index,
83 unsigned long flags, unsigned int mult, unsigned int div,
86 struct clk_fixed_factor *fix;
87 struct clk_init_data init = { };
88 struct clk_parent_data pdata = { .index = index };
92 /* You can't use devm without a dev */
94 return ERR_PTR(-EINVAL);
97 fix = devres_alloc(devm_clk_hw_register_fixed_factor_release,
98 sizeof(*fix), GFP_KERNEL);
100 fix = kmalloc(sizeof(*fix), GFP_KERNEL);
102 return ERR_PTR(-ENOMEM);
104 /* struct clk_fixed_factor assignments */
107 fix->hw.init = &init;
110 init.ops = &clk_fixed_factor_ops;
113 init.parent_names = &parent_name;
115 init.parent_hws = &parent_hw;
117 init.parent_data = &pdata;
118 init.num_parents = 1;
122 ret = clk_hw_register(dev, hw);
124 ret = of_clk_hw_register(np, hw);
132 devres_add(dev, fix);
138 * devm_clk_hw_register_fixed_factor_index - Register a fixed factor clock with
139 * parent from DT index
140 * @dev: device that is registering this clock
141 * @name: name of this clock
142 * @index: index of phandle in @dev 'clocks' property
143 * @flags: fixed factor flags
147 * Return: Pointer to fixed factor clk_hw structure that was registered or
150 struct clk_hw *devm_clk_hw_register_fixed_factor_index(struct device *dev,
151 const char *name, unsigned int index, unsigned long flags,
152 unsigned int mult, unsigned int div)
154 return __clk_hw_register_fixed_factor(dev, NULL, name, NULL, NULL, index,
155 flags, mult, div, true);
157 EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor_index);
160 * devm_clk_hw_register_fixed_factor_parent_hw - Register a fixed factor clock with
161 * pointer to parent clock
162 * @dev: device that is registering this clock
163 * @name: name of this clock
164 * @parent_hw: pointer to parent clk
165 * @flags: fixed factor flags
169 * Return: Pointer to fixed factor clk_hw structure that was registered or
172 struct clk_hw *devm_clk_hw_register_fixed_factor_parent_hw(struct device *dev,
173 const char *name, const struct clk_hw *parent_hw,
174 unsigned long flags, unsigned int mult, unsigned int div)
176 return __clk_hw_register_fixed_factor(dev, NULL, name, NULL, parent_hw,
177 -1, flags, mult, div, true);
179 EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor_parent_hw);
181 struct clk_hw *clk_hw_register_fixed_factor_parent_hw(struct device *dev,
182 const char *name, const struct clk_hw *parent_hw,
183 unsigned long flags, unsigned int mult, unsigned int div)
185 return __clk_hw_register_fixed_factor(dev, NULL, name, NULL,
186 parent_hw, -1, flags, mult, div,
189 EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor_parent_hw);
191 struct clk_hw *clk_hw_register_fixed_factor(struct device *dev,
192 const char *name, const char *parent_name, unsigned long flags,
193 unsigned int mult, unsigned int div)
195 return __clk_hw_register_fixed_factor(dev, NULL, name, parent_name, NULL, -1,
196 flags, mult, div, false);
198 EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor);
200 struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
201 const char *parent_name, unsigned long flags,
202 unsigned int mult, unsigned int div)
206 hw = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult,
212 EXPORT_SYMBOL_GPL(clk_register_fixed_factor);
214 void clk_unregister_fixed_factor(struct clk *clk)
218 hw = __clk_get_hw(clk);
223 kfree(to_clk_fixed_factor(hw));
225 EXPORT_SYMBOL_GPL(clk_unregister_fixed_factor);
227 void clk_hw_unregister_fixed_factor(struct clk_hw *hw)
229 struct clk_fixed_factor *fix;
231 fix = to_clk_fixed_factor(hw);
233 clk_hw_unregister(hw);
236 EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_factor);
238 struct clk_hw *devm_clk_hw_register_fixed_factor(struct device *dev,
239 const char *name, const char *parent_name, unsigned long flags,
240 unsigned int mult, unsigned int div)
242 return __clk_hw_register_fixed_factor(dev, NULL, name, parent_name, NULL, -1,
243 flags, mult, div, true);
245 EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor);
248 static struct clk_hw *_of_fixed_factor_clk_setup(struct device_node *node)
251 const char *clk_name = node->name;
255 if (of_property_read_u32(node, "clock-div", &div)) {
256 pr_err("%s Fixed factor clock <%pOFn> must have a clock-div property\n",
258 return ERR_PTR(-EIO);
261 if (of_property_read_u32(node, "clock-mult", &mult)) {
262 pr_err("%s Fixed factor clock <%pOFn> must have a clock-mult property\n",
264 return ERR_PTR(-EIO);
267 of_property_read_string(node, "clock-output-names", &clk_name);
269 hw = __clk_hw_register_fixed_factor(NULL, node, clk_name, NULL, NULL, 0,
270 0, mult, div, false);
273 * Clear OF_POPULATED flag so that clock registration can be
274 * attempted again from probe function.
276 of_node_clear_flag(node, OF_POPULATED);
280 ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
282 clk_hw_unregister_fixed_factor(hw);
290 * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
291 * @node: device node for the clock
293 void __init of_fixed_factor_clk_setup(struct device_node *node)
295 _of_fixed_factor_clk_setup(node);
297 CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
298 of_fixed_factor_clk_setup);
300 static int of_fixed_factor_clk_remove(struct platform_device *pdev)
302 struct clk_hw *clk = platform_get_drvdata(pdev);
304 of_clk_del_provider(pdev->dev.of_node);
305 clk_hw_unregister_fixed_factor(clk);
310 static int of_fixed_factor_clk_probe(struct platform_device *pdev)
315 * This function is not executed when of_fixed_factor_clk_setup
318 clk = _of_fixed_factor_clk_setup(pdev->dev.of_node);
322 platform_set_drvdata(pdev, clk);
327 static const struct of_device_id of_fixed_factor_clk_ids[] = {
328 { .compatible = "fixed-factor-clock" },
331 MODULE_DEVICE_TABLE(of, of_fixed_factor_clk_ids);
333 static struct platform_driver of_fixed_factor_clk_driver = {
335 .name = "of_fixed_factor_clk",
336 .of_match_table = of_fixed_factor_clk_ids,
338 .probe = of_fixed_factor_clk_probe,
339 .remove = of_fixed_factor_clk_remove,
341 builtin_platform_driver(of_fixed_factor_clk_driver);