Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
4 */
5#include <linux/module.h>
6#include <linux/clk-provider.h>
7#include <linux/slab.h>
8#include <linux/err.h>
9#include <linux/of.h>
10#include <linux/platform_device.h>
11
12/*
13 * DOC: basic fixed multiplier and divider clock that cannot gate
14 *
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
20 */
21
22static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
23 unsigned long parent_rate)
24{
25 struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
26 unsigned long long int rate;
27
28 rate = (unsigned long long int)parent_rate * fix->mult;
29 do_div(rate, fix->div);
30 return (unsigned long)rate;
31}
32
33static long clk_factor_round_rate(struct clk_hw *hw, unsigned long rate,
34 unsigned long *prate)
35{
36 struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
37
38 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
39 unsigned long best_parent;
40
41 best_parent = (rate / fix->mult) * fix->div;
42 *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
43 }
44
45 return (*prate / fix->div) * fix->mult;
46}
47
48static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate,
49 unsigned long parent_rate)
50{
51 /*
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
54 * nop.
55 */
56
57 return 0;
58}
59
60const 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,
64};
65EXPORT_SYMBOL_GPL(clk_fixed_factor_ops);
66
67static void devm_clk_hw_register_fixed_factor_release(struct device *dev, void *res)
68{
69 struct clk_fixed_factor *fix = res;
70
71 /*
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.
75 */
76 clk_hw_unregister(&fix->hw);
77}
78
79static 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,
84 bool devm)
85{
86 struct clk_fixed_factor *fix;
87 struct clk_init_data init = { };
88 struct clk_parent_data pdata = { .index = index };
89 struct clk_hw *hw;
90 int ret;
91
92 /* You can't use devm without a dev */
93 if (devm && !dev)
94 return ERR_PTR(-EINVAL);
95
96 if (devm)
97 fix = devres_alloc(devm_clk_hw_register_fixed_factor_release,
98 sizeof(*fix), GFP_KERNEL);
99 else
100 fix = kmalloc(sizeof(*fix), GFP_KERNEL);
101 if (!fix)
102 return ERR_PTR(-ENOMEM);
103
104 /* struct clk_fixed_factor assignments */
105 fix->mult = mult;
106 fix->div = div;
107 fix->hw.init = &init;
108
109 init.name = name;
110 init.ops = &clk_fixed_factor_ops;
111 init.flags = flags;
112 if (parent_name)
113 init.parent_names = &parent_name;
114 else if (parent_hw)
115 init.parent_hws = &parent_hw;
116 else
117 init.parent_data = &pdata;
118 init.num_parents = 1;
119
120 hw = &fix->hw;
121 if (dev)
122 ret = clk_hw_register(dev, hw);
123 else
124 ret = of_clk_hw_register(np, hw);
125 if (ret) {
126 if (devm)
127 devres_free(fix);
128 else
129 kfree(fix);
130 hw = ERR_PTR(ret);
131 } else if (devm)
132 devres_add(dev, fix);
133
134 return hw;
135}
136
137/**
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
144 * @mult: multiplier
145 * @div: divider
146 *
147 * Return: Pointer to fixed factor clk_hw structure that was registered or
148 * an error pointer.
149 */
150struct 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)
153{
154 return __clk_hw_register_fixed_factor(dev, NULL, name, NULL, NULL, index,
155 flags, mult, div, true);
156}
157EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor_index);
158
159/**
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
166 * @mult: multiplier
167 * @div: divider
168 *
169 * Return: Pointer to fixed factor clk_hw structure that was registered or
170 * an error pointer.
171 */
172struct 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)
175{
176 return __clk_hw_register_fixed_factor(dev, NULL, name, NULL, parent_hw,
177 -1, flags, mult, div, true);
178}
179EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor_parent_hw);
180
181struct 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)
184{
185 return __clk_hw_register_fixed_factor(dev, NULL, name, NULL,
186 parent_hw, -1, flags, mult, div,
187 false);
188}
189EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor_parent_hw);
190
191struct 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)
194{
195 return __clk_hw_register_fixed_factor(dev, NULL, name, parent_name, NULL, -1,
196 flags, mult, div, false);
197}
198EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor);
199
200struct 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)
203{
204 struct clk_hw *hw;
205
206 hw = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult,
207 div);
208 if (IS_ERR(hw))
209 return ERR_CAST(hw);
210 return hw->clk;
211}
212EXPORT_SYMBOL_GPL(clk_register_fixed_factor);
213
214void clk_unregister_fixed_factor(struct clk *clk)
215{
216 struct clk_hw *hw;
217
218 hw = __clk_get_hw(clk);
219 if (!hw)
220 return;
221
222 clk_unregister(clk);
223 kfree(to_clk_fixed_factor(hw));
224}
225EXPORT_SYMBOL_GPL(clk_unregister_fixed_factor);
226
227void clk_hw_unregister_fixed_factor(struct clk_hw *hw)
228{
229 struct clk_fixed_factor *fix;
230
231 fix = to_clk_fixed_factor(hw);
232
233 clk_hw_unregister(hw);
234 kfree(fix);
235}
236EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_factor);
237
238struct 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)
241{
242 return __clk_hw_register_fixed_factor(dev, NULL, name, parent_name, NULL, -1,
243 flags, mult, div, true);
244}
245EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor);
246
247#ifdef CONFIG_OF
248static struct clk_hw *_of_fixed_factor_clk_setup(struct device_node *node)
249{
250 struct clk_hw *hw;
251 const char *clk_name = node->name;
252 u32 div, mult;
253 int ret;
254
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",
257 __func__, node);
258 return ERR_PTR(-EIO);
259 }
260
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",
263 __func__, node);
264 return ERR_PTR(-EIO);
265 }
266
267 of_property_read_string(node, "clock-output-names", &clk_name);
268
269 hw = __clk_hw_register_fixed_factor(NULL, node, clk_name, NULL, NULL, 0,
270 0, mult, div, false);
271 if (IS_ERR(hw)) {
272 /*
273 * Clear OF_POPULATED flag so that clock registration can be
274 * attempted again from probe function.
275 */
276 of_node_clear_flag(node, OF_POPULATED);
277 return ERR_CAST(hw);
278 }
279
280 ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
281 if (ret) {
282 clk_hw_unregister_fixed_factor(hw);
283 return ERR_PTR(ret);
284 }
285
286 return hw;
287}
288
289/**
290 * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
291 * @node: device node for the clock
292 */
293void __init of_fixed_factor_clk_setup(struct device_node *node)
294{
295 _of_fixed_factor_clk_setup(node);
296}
297CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
298 of_fixed_factor_clk_setup);
299
300static int of_fixed_factor_clk_remove(struct platform_device *pdev)
301{
302 struct clk_hw *clk = platform_get_drvdata(pdev);
303
304 of_clk_del_provider(pdev->dev.of_node);
305 clk_hw_unregister_fixed_factor(clk);
306
307 return 0;
308}
309
310static int of_fixed_factor_clk_probe(struct platform_device *pdev)
311{
312 struct clk_hw *clk;
313
314 /*
315 * This function is not executed when of_fixed_factor_clk_setup
316 * succeeded.
317 */
318 clk = _of_fixed_factor_clk_setup(pdev->dev.of_node);
319 if (IS_ERR(clk))
320 return PTR_ERR(clk);
321
322 platform_set_drvdata(pdev, clk);
323
324 return 0;
325}
326
327static const struct of_device_id of_fixed_factor_clk_ids[] = {
328 { .compatible = "fixed-factor-clock" },
329 { }
330};
331MODULE_DEVICE_TABLE(of, of_fixed_factor_clk_ids);
332
333static struct platform_driver of_fixed_factor_clk_driver = {
334 .driver = {
335 .name = "of_fixed_factor_clk",
336 .of_match_table = of_fixed_factor_clk_ids,
337 },
338 .probe = of_fixed_factor_clk_probe,
339 .remove = of_fixed_factor_clk_remove,
340};
341builtin_platform_driver(of_fixed_factor_clk_driver);
342#endif
1/*
2 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Standard functionality for the common clock API.
9 */
10#include <linux/module.h>
11#include <linux/clk-provider.h>
12#include <linux/slab.h>
13#include <linux/err.h>
14#include <linux/of.h>
15#include <linux/platform_device.h>
16
17/*
18 * DOC: basic fixed multiplier and divider clock that cannot gate
19 *
20 * Traits of this clock:
21 * prepare - clk_prepare only ensures that parents are prepared
22 * enable - clk_enable only ensures that parents are enabled
23 * rate - rate is fixed. clk->rate = parent->rate / div * mult
24 * parent - fixed parent. No clk_set_parent support
25 */
26
27static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
28 unsigned long parent_rate)
29{
30 struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
31 unsigned long long int rate;
32
33 rate = (unsigned long long int)parent_rate * fix->mult;
34 do_div(rate, fix->div);
35 return (unsigned long)rate;
36}
37
38static long clk_factor_round_rate(struct clk_hw *hw, unsigned long rate,
39 unsigned long *prate)
40{
41 struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
42
43 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
44 unsigned long best_parent;
45
46 best_parent = (rate / fix->mult) * fix->div;
47 *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
48 }
49
50 return (*prate / fix->div) * fix->mult;
51}
52
53static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate,
54 unsigned long parent_rate)
55{
56 /*
57 * We must report success but we can do so unconditionally because
58 * clk_factor_round_rate returns values that ensure this call is a
59 * nop.
60 */
61
62 return 0;
63}
64
65const struct clk_ops clk_fixed_factor_ops = {
66 .round_rate = clk_factor_round_rate,
67 .set_rate = clk_factor_set_rate,
68 .recalc_rate = clk_factor_recalc_rate,
69};
70EXPORT_SYMBOL_GPL(clk_fixed_factor_ops);
71
72struct clk_hw *clk_hw_register_fixed_factor(struct device *dev,
73 const char *name, const char *parent_name, unsigned long flags,
74 unsigned int mult, unsigned int div)
75{
76 struct clk_fixed_factor *fix;
77 struct clk_init_data init;
78 struct clk_hw *hw;
79 int ret;
80
81 fix = kmalloc(sizeof(*fix), GFP_KERNEL);
82 if (!fix)
83 return ERR_PTR(-ENOMEM);
84
85 /* struct clk_fixed_factor assignments */
86 fix->mult = mult;
87 fix->div = div;
88 fix->hw.init = &init;
89
90 init.name = name;
91 init.ops = &clk_fixed_factor_ops;
92 init.flags = flags | CLK_IS_BASIC;
93 init.parent_names = &parent_name;
94 init.num_parents = 1;
95
96 hw = &fix->hw;
97 ret = clk_hw_register(dev, hw);
98 if (ret) {
99 kfree(fix);
100 hw = ERR_PTR(ret);
101 }
102
103 return hw;
104}
105EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor);
106
107struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
108 const char *parent_name, unsigned long flags,
109 unsigned int mult, unsigned int div)
110{
111 struct clk_hw *hw;
112
113 hw = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult,
114 div);
115 if (IS_ERR(hw))
116 return ERR_CAST(hw);
117 return hw->clk;
118}
119EXPORT_SYMBOL_GPL(clk_register_fixed_factor);
120
121void clk_unregister_fixed_factor(struct clk *clk)
122{
123 struct clk_hw *hw;
124
125 hw = __clk_get_hw(clk);
126 if (!hw)
127 return;
128
129 clk_unregister(clk);
130 kfree(to_clk_fixed_factor(hw));
131}
132EXPORT_SYMBOL_GPL(clk_unregister_fixed_factor);
133
134void clk_hw_unregister_fixed_factor(struct clk_hw *hw)
135{
136 struct clk_fixed_factor *fix;
137
138 fix = to_clk_fixed_factor(hw);
139
140 clk_hw_unregister(hw);
141 kfree(fix);
142}
143EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_factor);
144
145#ifdef CONFIG_OF
146static const struct of_device_id set_rate_parent_matches[] = {
147 { .compatible = "allwinner,sun4i-a10-pll3-2x-clk" },
148 { /* Sentinel */ },
149};
150
151static struct clk *_of_fixed_factor_clk_setup(struct device_node *node)
152{
153 struct clk *clk;
154 const char *clk_name = node->name;
155 const char *parent_name;
156 unsigned long flags = 0;
157 u32 div, mult;
158 int ret;
159
160 if (of_property_read_u32(node, "clock-div", &div)) {
161 pr_err("%s Fixed factor clock <%s> must have a clock-div property\n",
162 __func__, node->name);
163 return ERR_PTR(-EIO);
164 }
165
166 if (of_property_read_u32(node, "clock-mult", &mult)) {
167 pr_err("%s Fixed factor clock <%s> must have a clock-mult property\n",
168 __func__, node->name);
169 return ERR_PTR(-EIO);
170 }
171
172 of_property_read_string(node, "clock-output-names", &clk_name);
173 parent_name = of_clk_get_parent_name(node, 0);
174
175 if (of_match_node(set_rate_parent_matches, node))
176 flags |= CLK_SET_RATE_PARENT;
177
178 clk = clk_register_fixed_factor(NULL, clk_name, parent_name, flags,
179 mult, div);
180 if (IS_ERR(clk))
181 return clk;
182
183 ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
184 if (ret) {
185 clk_unregister(clk);
186 return ERR_PTR(ret);
187 }
188
189 return clk;
190}
191
192/**
193 * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
194 */
195void __init of_fixed_factor_clk_setup(struct device_node *node)
196{
197 _of_fixed_factor_clk_setup(node);
198}
199CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
200 of_fixed_factor_clk_setup);
201
202static int of_fixed_factor_clk_remove(struct platform_device *pdev)
203{
204 struct clk *clk = platform_get_drvdata(pdev);
205
206 clk_unregister_fixed_factor(clk);
207
208 return 0;
209}
210
211static int of_fixed_factor_clk_probe(struct platform_device *pdev)
212{
213 struct clk *clk;
214
215 /*
216 * This function is not executed when of_fixed_factor_clk_setup
217 * succeeded.
218 */
219 clk = _of_fixed_factor_clk_setup(pdev->dev.of_node);
220 if (IS_ERR(clk))
221 return PTR_ERR(clk);
222
223 platform_set_drvdata(pdev, clk);
224
225 return 0;
226}
227
228static const struct of_device_id of_fixed_factor_clk_ids[] = {
229 { .compatible = "fixed-factor-clock" },
230 { }
231};
232MODULE_DEVICE_TABLE(of, of_fixed_factor_clk_ids);
233
234static struct platform_driver of_fixed_factor_clk_driver = {
235 .driver = {
236 .name = "of_fixed_factor_clk",
237 .of_match_table = of_fixed_factor_clk_ids,
238 },
239 .probe = of_fixed_factor_clk_probe,
240 .remove = of_fixed_factor_clk_remove,
241};
242builtin_platform_driver(of_fixed_factor_clk_driver);
243#endif