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 
22 static 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 
33 static 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 
48 static 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 
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,
64 };
65 EXPORT_SYMBOL_GPL(clk_fixed_factor_ops);
66 
67 static void devm_clk_hw_register_fixed_factor_release(struct device *dev, void *res)
68 {
69 	clk_hw_unregister_fixed_factor(&((struct clk_fixed_factor *)res)->hw);
70 }
71 
72 static struct clk_hw *
73 __clk_hw_register_fixed_factor(struct device *dev, struct device_node *np,
74 		const char *name, const char *parent_name, int index,
75 		unsigned long flags, unsigned int mult, unsigned int div,
76 		bool devm)
77 {
78 	struct clk_fixed_factor *fix;
79 	struct clk_init_data init = { };
80 	struct clk_parent_data pdata = { .index = index };
81 	struct clk_hw *hw;
82 	int ret;
83 
84 	/* You can't use devm without a dev */
85 	if (devm && !dev)
86 		return ERR_PTR(-EINVAL);
87 
88 	if (devm)
89 		fix = devres_alloc(devm_clk_hw_register_fixed_factor_release,
90 				sizeof(*fix), GFP_KERNEL);
91 	else
92 		fix = kmalloc(sizeof(*fix), GFP_KERNEL);
93 	if (!fix)
94 		return ERR_PTR(-ENOMEM);
95 
96 	/* struct clk_fixed_factor assignments */
97 	fix->mult = mult;
98 	fix->div = div;
99 	fix->hw.init = &init;
100 
101 	init.name = name;
102 	init.ops = &clk_fixed_factor_ops;
103 	init.flags = flags;
104 	if (parent_name)
105 		init.parent_names = &parent_name;
106 	else
107 		init.parent_data = &pdata;
108 	init.num_parents = 1;
109 
110 	hw = &fix->hw;
111 	if (dev)
112 		ret = clk_hw_register(dev, hw);
113 	else
114 		ret = of_clk_hw_register(np, hw);
115 	if (ret) {
116 		if (devm)
117 			devres_free(fix);
118 		else
119 			kfree(fix);
120 		hw = ERR_PTR(ret);
121 	} else if (devm)
122 		devres_add(dev, fix);
123 
124 	return hw;
125 }
126 
127 struct clk_hw *clk_hw_register_fixed_factor(struct device *dev,
128 		const char *name, const char *parent_name, unsigned long flags,
129 		unsigned int mult, unsigned int div)
130 {
131 	return __clk_hw_register_fixed_factor(dev, NULL, name, parent_name, -1,
132 					      flags, mult, div, false);
133 }
134 EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor);
135 
136 struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
137 		const char *parent_name, unsigned long flags,
138 		unsigned int mult, unsigned int div)
139 {
140 	struct clk_hw *hw;
141 
142 	hw = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult,
143 					  div);
144 	if (IS_ERR(hw))
145 		return ERR_CAST(hw);
146 	return hw->clk;
147 }
148 EXPORT_SYMBOL_GPL(clk_register_fixed_factor);
149 
150 void clk_unregister_fixed_factor(struct clk *clk)
151 {
152 	struct clk_hw *hw;
153 
154 	hw = __clk_get_hw(clk);
155 	if (!hw)
156 		return;
157 
158 	clk_unregister(clk);
159 	kfree(to_clk_fixed_factor(hw));
160 }
161 EXPORT_SYMBOL_GPL(clk_unregister_fixed_factor);
162 
163 void clk_hw_unregister_fixed_factor(struct clk_hw *hw)
164 {
165 	struct clk_fixed_factor *fix;
166 
167 	fix = to_clk_fixed_factor(hw);
168 
169 	clk_hw_unregister(hw);
170 	kfree(fix);
171 }
172 EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_factor);
173 
174 struct clk_hw *devm_clk_hw_register_fixed_factor(struct device *dev,
175 		const char *name, const char *parent_name, unsigned long flags,
176 		unsigned int mult, unsigned int div)
177 {
178 	return __clk_hw_register_fixed_factor(dev, NULL, name, parent_name, -1,
179 			flags, mult, div, true);
180 }
181 EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor);
182 
183 #ifdef CONFIG_OF
184 static const struct of_device_id set_rate_parent_matches[] = {
185 	{ .compatible = "allwinner,sun4i-a10-pll3-2x-clk" },
186 	{ /* Sentinel */ },
187 };
188 
189 static struct clk_hw *_of_fixed_factor_clk_setup(struct device_node *node)
190 {
191 	struct clk_hw *hw;
192 	const char *clk_name = node->name;
193 	unsigned long flags = 0;
194 	u32 div, mult;
195 	int ret;
196 
197 	if (of_property_read_u32(node, "clock-div", &div)) {
198 		pr_err("%s Fixed factor clock <%pOFn> must have a clock-div property\n",
199 			__func__, node);
200 		return ERR_PTR(-EIO);
201 	}
202 
203 	if (of_property_read_u32(node, "clock-mult", &mult)) {
204 		pr_err("%s Fixed factor clock <%pOFn> must have a clock-mult property\n",
205 			__func__, node);
206 		return ERR_PTR(-EIO);
207 	}
208 
209 	of_property_read_string(node, "clock-output-names", &clk_name);
210 
211 	if (of_match_node(set_rate_parent_matches, node))
212 		flags |= CLK_SET_RATE_PARENT;
213 
214 	hw = __clk_hw_register_fixed_factor(NULL, node, clk_name, NULL, 0,
215 					    flags, mult, div, false);
216 	if (IS_ERR(hw)) {
217 		/*
218 		 * Clear OF_POPULATED flag so that clock registration can be
219 		 * attempted again from probe function.
220 		 */
221 		of_node_clear_flag(node, OF_POPULATED);
222 		return ERR_CAST(hw);
223 	}
224 
225 	ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
226 	if (ret) {
227 		clk_hw_unregister_fixed_factor(hw);
228 		return ERR_PTR(ret);
229 	}
230 
231 	return hw;
232 }
233 
234 /**
235  * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
236  * @node:	device node for the clock
237  */
238 void __init of_fixed_factor_clk_setup(struct device_node *node)
239 {
240 	_of_fixed_factor_clk_setup(node);
241 }
242 CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
243 		of_fixed_factor_clk_setup);
244 
245 static int of_fixed_factor_clk_remove(struct platform_device *pdev)
246 {
247 	struct clk_hw *clk = platform_get_drvdata(pdev);
248 
249 	of_clk_del_provider(pdev->dev.of_node);
250 	clk_hw_unregister_fixed_factor(clk);
251 
252 	return 0;
253 }
254 
255 static int of_fixed_factor_clk_probe(struct platform_device *pdev)
256 {
257 	struct clk_hw *clk;
258 
259 	/*
260 	 * This function is not executed when of_fixed_factor_clk_setup
261 	 * succeeded.
262 	 */
263 	clk = _of_fixed_factor_clk_setup(pdev->dev.of_node);
264 	if (IS_ERR(clk))
265 		return PTR_ERR(clk);
266 
267 	platform_set_drvdata(pdev, clk);
268 
269 	return 0;
270 }
271 
272 static const struct of_device_id of_fixed_factor_clk_ids[] = {
273 	{ .compatible = "fixed-factor-clock" },
274 	{ }
275 };
276 MODULE_DEVICE_TABLE(of, of_fixed_factor_clk_ids);
277 
278 static struct platform_driver of_fixed_factor_clk_driver = {
279 	.driver = {
280 		.name = "of_fixed_factor_clk",
281 		.of_match_table = of_fixed_factor_clk_ids,
282 	},
283 	.probe = of_fixed_factor_clk_probe,
284 	.remove = of_fixed_factor_clk_remove,
285 };
286 builtin_platform_driver(of_fixed_factor_clk_driver);
287 #endif
288