blob: 4e4b6d36761265c7094cc9cfaf2c328f20673992 [file] [log] [blame]
Stephen Boyde1bd55e2018-12-11 09:57:48 -08001// SPDX-License-Identifier: GPL-2.0
Sascha Hauerf0948f52012-05-03 15:36:14 +05302/*
3 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
Sascha Hauerf0948f52012-05-03 15:36:14 +05304 */
5#include <linux/module.h>
6#include <linux/clk-provider.h>
7#include <linux/slab.h>
8#include <linux/err.h>
Gregory CLEMENT79b16642013-04-12 13:57:44 +02009#include <linux/of.h>
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +020010#include <linux/platform_device.h>
Sascha Hauerf0948f52012-05-03 15:36:14 +053011
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
Sascha Hauerf0948f52012-05-03 15:36:14 +053022static 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);
Haojian Zhuangbab53302012-12-03 16:14:37 +080026 unsigned long long int rate;
Sascha Hauerf0948f52012-05-03 15:36:14 +053027
Haojian Zhuangbab53302012-12-03 16:14:37 +080028 rate = (unsigned long long int)parent_rate * fix->mult;
29 do_div(rate, fix->div);
30 return (unsigned long)rate;
Sascha Hauerf0948f52012-05-03 15:36:14 +053031}
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
Stephen Boyd98d8a602015-06-29 16:56:30 -070038 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
Sascha Hauerf0948f52012-05-03 15:36:14 +053039 unsigned long best_parent;
40
41 best_parent = (rate / fix->mult) * fix->div;
Stephen Boyd2f508a92015-07-30 17:20:57 -070042 *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
Sascha Hauerf0948f52012-05-03 15:36:14 +053043 }
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{
Daniel Thompson3037e9e2015-06-10 21:04:54 +010051 /*
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
Sascha Hauerf0948f52012-05-03 15:36:14 +053057 return 0;
58}
59
Daniel Thompson3037e9e2015-06-10 21:04:54 +010060const struct clk_ops clk_fixed_factor_ops = {
Sascha Hauerf0948f52012-05-03 15:36:14 +053061 .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
Daniel Palmer0b9266d2021-02-11 14:22:02 +090067static void devm_clk_hw_register_fixed_factor_release(struct device *dev, void *res)
68{
Dmitry Baryshkov50ce68262021-04-07 02:06:06 +030069 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);
Daniel Palmer0b9266d2021-02-11 14:22:02 +090077}
78
Stephen Boydecbf3f12019-04-12 11:31:50 -070079static struct clk_hw *
80__clk_hw_register_fixed_factor(struct device *dev, struct device_node *np,
81 const char *name, const char *parent_name, int index,
Daniel Palmer0b9266d2021-02-11 14:22:02 +090082 unsigned long flags, unsigned int mult, unsigned int div,
83 bool devm)
Sascha Hauerf0948f52012-05-03 15:36:14 +053084{
85 struct clk_fixed_factor *fix;
Stephen Boyde4818d62019-04-23 10:46:51 -070086 struct clk_init_data init = { };
Stephen Boydecbf3f12019-04-12 11:31:50 -070087 struct clk_parent_data pdata = { .index = index };
Stephen Boyd0759ac82016-02-07 00:11:06 -080088 struct clk_hw *hw;
89 int ret;
Sascha Hauerf0948f52012-05-03 15:36:14 +053090
Daniel Palmer0b9266d2021-02-11 14:22:02 +090091 /* You can't use devm without a dev */
92 if (devm && !dev)
93 return ERR_PTR(-EINVAL);
94
95 if (devm)
96 fix = devres_alloc(devm_clk_hw_register_fixed_factor_release,
97 sizeof(*fix), GFP_KERNEL);
98 else
99 fix = kmalloc(sizeof(*fix), GFP_KERNEL);
Stephen Boydd122db72015-05-14 16:47:10 -0700100 if (!fix)
Sascha Hauerf0948f52012-05-03 15:36:14 +0530101 return ERR_PTR(-ENOMEM);
Sascha Hauerf0948f52012-05-03 15:36:14 +0530102
103 /* struct clk_fixed_factor assignments */
104 fix->mult = mult;
105 fix->div = div;
106 fix->hw.init = &init;
107
108 init.name = name;
109 init.ops = &clk_fixed_factor_ops;
Stephen Boyd90b6c5c2019-04-25 10:57:37 -0700110 init.flags = flags;
Stephen Boydecbf3f12019-04-12 11:31:50 -0700111 if (parent_name)
112 init.parent_names = &parent_name;
113 else
114 init.parent_data = &pdata;
Sascha Hauerf0948f52012-05-03 15:36:14 +0530115 init.num_parents = 1;
116
Stephen Boyd0759ac82016-02-07 00:11:06 -0800117 hw = &fix->hw;
Stephen Boydecbf3f12019-04-12 11:31:50 -0700118 if (dev)
119 ret = clk_hw_register(dev, hw);
120 else
121 ret = of_clk_hw_register(np, hw);
Stephen Boyd0759ac82016-02-07 00:11:06 -0800122 if (ret) {
Daniel Palmer0b9266d2021-02-11 14:22:02 +0900123 if (devm)
124 devres_free(fix);
125 else
126 kfree(fix);
Stephen Boyd0759ac82016-02-07 00:11:06 -0800127 hw = ERR_PTR(ret);
Daniel Palmer0b9266d2021-02-11 14:22:02 +0900128 } else if (devm)
129 devres_add(dev, fix);
Sascha Hauerf0948f52012-05-03 15:36:14 +0530130
Stephen Boyd0759ac82016-02-07 00:11:06 -0800131 return hw;
132}
Stephen Boydecbf3f12019-04-12 11:31:50 -0700133
134struct clk_hw *clk_hw_register_fixed_factor(struct device *dev,
135 const char *name, const char *parent_name, unsigned long flags,
136 unsigned int mult, unsigned int div)
137{
138 return __clk_hw_register_fixed_factor(dev, NULL, name, parent_name, -1,
Daniel Palmer0b9266d2021-02-11 14:22:02 +0900139 flags, mult, div, false);
Stephen Boydecbf3f12019-04-12 11:31:50 -0700140}
Stephen Boyd0759ac82016-02-07 00:11:06 -0800141EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor);
142
143struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
144 const char *parent_name, unsigned long flags,
145 unsigned int mult, unsigned int div)
146{
147 struct clk_hw *hw;
148
149 hw = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult,
150 div);
151 if (IS_ERR(hw))
152 return ERR_CAST(hw);
153 return hw->clk;
Sascha Hauerf0948f52012-05-03 15:36:14 +0530154}
Mike Turquette5cfe10b2013-08-15 19:06:29 -0700155EXPORT_SYMBOL_GPL(clk_register_fixed_factor);
156
Masahiro Yamadacbf95912016-01-06 13:25:09 +0900157void clk_unregister_fixed_factor(struct clk *clk)
158{
159 struct clk_hw *hw;
160
161 hw = __clk_get_hw(clk);
162 if (!hw)
163 return;
164
165 clk_unregister(clk);
166 kfree(to_clk_fixed_factor(hw));
167}
168EXPORT_SYMBOL_GPL(clk_unregister_fixed_factor);
169
Stephen Boyd0759ac82016-02-07 00:11:06 -0800170void clk_hw_unregister_fixed_factor(struct clk_hw *hw)
171{
172 struct clk_fixed_factor *fix;
173
174 fix = to_clk_fixed_factor(hw);
175
176 clk_hw_unregister(hw);
177 kfree(fix);
178}
179EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_factor);
180
Daniel Palmer0b9266d2021-02-11 14:22:02 +0900181struct clk_hw *devm_clk_hw_register_fixed_factor(struct device *dev,
182 const char *name, const char *parent_name, unsigned long flags,
183 unsigned int mult, unsigned int div)
184{
185 return __clk_hw_register_fixed_factor(dev, NULL, name, parent_name, -1,
186 flags, mult, div, true);
187}
188EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor);
189
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200190#ifdef CONFIG_OF
Maxime Riparde6cbf992016-06-22 11:15:54 +0200191static const struct of_device_id set_rate_parent_matches[] = {
192 { .compatible = "allwinner,sun4i-a10-pll3-2x-clk" },
193 { /* Sentinel */ },
194};
195
Stephen Boydecbf3f12019-04-12 11:31:50 -0700196static struct clk_hw *_of_fixed_factor_clk_setup(struct device_node *node)
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200197{
Stephen Boydecbf3f12019-04-12 11:31:50 -0700198 struct clk_hw *hw;
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200199 const char *clk_name = node->name;
Maxime Riparde6cbf992016-06-22 11:15:54 +0200200 unsigned long flags = 0;
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200201 u32 div, mult;
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +0200202 int ret;
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200203
204 if (of_property_read_u32(node, "clock-div", &div)) {
Rob Herringe665f022018-08-28 10:44:29 -0500205 pr_err("%s Fixed factor clock <%pOFn> must have a clock-div property\n",
206 __func__, node);
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +0200207 return ERR_PTR(-EIO);
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200208 }
209
210 if (of_property_read_u32(node, "clock-mult", &mult)) {
Rob Herringe665f022018-08-28 10:44:29 -0500211 pr_err("%s Fixed factor clock <%pOFn> must have a clock-mult property\n",
212 __func__, node);
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +0200213 return ERR_PTR(-EIO);
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200214 }
215
216 of_property_read_string(node, "clock-output-names", &clk_name);
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200217
Maxime Riparde6cbf992016-06-22 11:15:54 +0200218 if (of_match_node(set_rate_parent_matches, node))
219 flags |= CLK_SET_RATE_PARENT;
220
Stephen Boydecbf3f12019-04-12 11:31:50 -0700221 hw = __clk_hw_register_fixed_factor(NULL, node, clk_name, NULL, 0,
Daniel Palmer0b9266d2021-02-11 14:22:02 +0900222 flags, mult, div, false);
Stephen Boydecbf3f12019-04-12 11:31:50 -0700223 if (IS_ERR(hw)) {
Rajan Vajaf6dab422018-07-17 06:17:00 -0700224 /*
Rajan Vajaf6dab422018-07-17 06:17:00 -0700225 * Clear OF_POPULATED flag so that clock registration can be
226 * attempted again from probe function.
227 */
228 of_node_clear_flag(node, OF_POPULATED);
Stephen Boydecbf3f12019-04-12 11:31:50 -0700229 return ERR_CAST(hw);
Rajan Vajaf6dab422018-07-17 06:17:00 -0700230 }
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +0200231
Stephen Boydecbf3f12019-04-12 11:31:50 -0700232 ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +0200233 if (ret) {
Stephen Boydecbf3f12019-04-12 11:31:50 -0700234 clk_hw_unregister_fixed_factor(hw);
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +0200235 return ERR_PTR(ret);
236 }
237
Stephen Boydecbf3f12019-04-12 11:31:50 -0700238 return hw;
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +0200239}
240
241/**
242 * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
Krzysztof Kozlowski52ba4fa2020-09-16 18:17:36 +0200243 * @node: device node for the clock
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +0200244 */
245void __init of_fixed_factor_clk_setup(struct device_node *node)
246{
247 _of_fixed_factor_clk_setup(node);
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200248}
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200249CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
250 of_fixed_factor_clk_setup);
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +0200251
252static int of_fixed_factor_clk_remove(struct platform_device *pdev)
253{
Stephen Boydecbf3f12019-04-12 11:31:50 -0700254 struct clk_hw *clk = platform_get_drvdata(pdev);
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +0200255
Ricardo Ribalda Delgadof98e8a52018-11-01 14:15:49 +0100256 of_clk_del_provider(pdev->dev.of_node);
Stephen Boydecbf3f12019-04-12 11:31:50 -0700257 clk_hw_unregister_fixed_factor(clk);
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +0200258
259 return 0;
260}
261
262static int of_fixed_factor_clk_probe(struct platform_device *pdev)
263{
Stephen Boydecbf3f12019-04-12 11:31:50 -0700264 struct clk_hw *clk;
Ricardo Ribalda Delgado971451b2016-07-05 18:23:33 +0200265
266 /*
267 * This function is not executed when of_fixed_factor_clk_setup
268 * succeeded.
269 */
270 clk = _of_fixed_factor_clk_setup(pdev->dev.of_node);
271 if (IS_ERR(clk))
272 return PTR_ERR(clk);
273
274 platform_set_drvdata(pdev, clk);
275
276 return 0;
277}
278
279static const struct of_device_id of_fixed_factor_clk_ids[] = {
280 { .compatible = "fixed-factor-clock" },
281 { }
282};
283MODULE_DEVICE_TABLE(of, of_fixed_factor_clk_ids);
284
285static struct platform_driver of_fixed_factor_clk_driver = {
286 .driver = {
287 .name = "of_fixed_factor_clk",
288 .of_match_table = of_fixed_factor_clk_ids,
289 },
290 .probe = of_fixed_factor_clk_probe,
291 .remove = of_fixed_factor_clk_remove,
292};
293builtin_platform_driver(of_fixed_factor_clk_driver);
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200294#endif