1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2013 NVIDIA CORPORATION. All rights reserved.
6 #include <linux/clk-provider.h>
7 #include <linux/device.h>
9 #include <linux/slab.h>
11 static u8 clk_composite_get_parent(struct clk_hw *hw)
13 struct clk_composite *composite = to_clk_composite(hw);
14 const struct clk_ops *mux_ops = composite->mux_ops;
15 struct clk_hw *mux_hw = composite->mux_hw;
17 __clk_hw_set_clk(mux_hw, hw);
19 return mux_ops->get_parent(mux_hw);
22 static int clk_composite_set_parent(struct clk_hw *hw, u8 index)
24 struct clk_composite *composite = to_clk_composite(hw);
25 const struct clk_ops *mux_ops = composite->mux_ops;
26 struct clk_hw *mux_hw = composite->mux_hw;
28 __clk_hw_set_clk(mux_hw, hw);
30 return mux_ops->set_parent(mux_hw, index);
33 static unsigned long clk_composite_recalc_rate(struct clk_hw *hw,
34 unsigned long parent_rate)
36 struct clk_composite *composite = to_clk_composite(hw);
37 const struct clk_ops *rate_ops = composite->rate_ops;
38 struct clk_hw *rate_hw = composite->rate_hw;
40 __clk_hw_set_clk(rate_hw, hw);
42 return rate_ops->recalc_rate(rate_hw, parent_rate);
45 static int clk_composite_determine_rate_for_parent(struct clk_hw *rate_hw,
46 struct clk_rate_request *req,
47 struct clk_hw *parent_hw,
48 const struct clk_ops *rate_ops)
52 req->best_parent_hw = parent_hw;
53 req->best_parent_rate = clk_hw_get_rate(parent_hw);
55 if (rate_ops->determine_rate)
56 return rate_ops->determine_rate(rate_hw, req);
58 rate = rate_ops->round_rate(rate_hw, req->rate,
59 &req->best_parent_rate);
68 static int clk_composite_determine_rate(struct clk_hw *hw,
69 struct clk_rate_request *req)
71 struct clk_composite *composite = to_clk_composite(hw);
72 const struct clk_ops *rate_ops = composite->rate_ops;
73 const struct clk_ops *mux_ops = composite->mux_ops;
74 struct clk_hw *rate_hw = composite->rate_hw;
75 struct clk_hw *mux_hw = composite->mux_hw;
76 struct clk_hw *parent;
77 unsigned long rate_diff;
78 unsigned long best_rate_diff = ULONG_MAX;
79 unsigned long best_rate = 0;
82 if (rate_hw && rate_ops &&
83 (rate_ops->determine_rate || rate_ops->round_rate) &&
84 mux_hw && mux_ops && mux_ops->set_parent) {
85 req->best_parent_hw = NULL;
87 if (clk_hw_get_flags(hw) & CLK_SET_RATE_NO_REPARENT) {
88 struct clk_rate_request tmp_req = *req;
90 parent = clk_hw_get_parent(mux_hw);
92 ret = clk_composite_determine_rate_for_parent(rate_hw,
99 req->rate = tmp_req.rate;
100 req->best_parent_hw = tmp_req.best_parent_hw;
101 req->best_parent_rate = tmp_req.best_parent_rate;
106 for (i = 0; i < clk_hw_get_num_parents(mux_hw); i++) {
107 struct clk_rate_request tmp_req = *req;
109 parent = clk_hw_get_parent_by_index(mux_hw, i);
113 ret = clk_composite_determine_rate_for_parent(rate_hw,
120 rate_diff = abs(req->rate - tmp_req.rate);
122 if (!rate_diff || !req->best_parent_hw
123 || best_rate_diff > rate_diff) {
124 req->best_parent_hw = parent;
125 req->best_parent_rate = tmp_req.best_parent_rate;
126 best_rate_diff = rate_diff;
127 best_rate = tmp_req.rate;
134 req->rate = best_rate;
136 } else if (rate_hw && rate_ops && rate_ops->determine_rate) {
137 __clk_hw_set_clk(rate_hw, hw);
138 return rate_ops->determine_rate(rate_hw, req);
139 } else if (mux_hw && mux_ops && mux_ops->determine_rate) {
140 __clk_hw_set_clk(mux_hw, hw);
141 return mux_ops->determine_rate(mux_hw, req);
143 pr_err("clk: clk_composite_determine_rate function called, but no mux or rate callback set!\n");
148 static long clk_composite_round_rate(struct clk_hw *hw, unsigned long rate,
149 unsigned long *prate)
151 struct clk_composite *composite = to_clk_composite(hw);
152 const struct clk_ops *rate_ops = composite->rate_ops;
153 struct clk_hw *rate_hw = composite->rate_hw;
155 __clk_hw_set_clk(rate_hw, hw);
157 return rate_ops->round_rate(rate_hw, rate, prate);
160 static int clk_composite_set_rate(struct clk_hw *hw, unsigned long rate,
161 unsigned long parent_rate)
163 struct clk_composite *composite = to_clk_composite(hw);
164 const struct clk_ops *rate_ops = composite->rate_ops;
165 struct clk_hw *rate_hw = composite->rate_hw;
167 __clk_hw_set_clk(rate_hw, hw);
169 return rate_ops->set_rate(rate_hw, rate, parent_rate);
172 static int clk_composite_set_rate_and_parent(struct clk_hw *hw,
174 unsigned long parent_rate,
177 struct clk_composite *composite = to_clk_composite(hw);
178 const struct clk_ops *rate_ops = composite->rate_ops;
179 const struct clk_ops *mux_ops = composite->mux_ops;
180 struct clk_hw *rate_hw = composite->rate_hw;
181 struct clk_hw *mux_hw = composite->mux_hw;
182 unsigned long temp_rate;
184 __clk_hw_set_clk(rate_hw, hw);
185 __clk_hw_set_clk(mux_hw, hw);
187 temp_rate = rate_ops->recalc_rate(rate_hw, parent_rate);
188 if (temp_rate > rate) {
189 rate_ops->set_rate(rate_hw, rate, parent_rate);
190 mux_ops->set_parent(mux_hw, index);
192 mux_ops->set_parent(mux_hw, index);
193 rate_ops->set_rate(rate_hw, rate, parent_rate);
199 static int clk_composite_is_enabled(struct clk_hw *hw)
201 struct clk_composite *composite = to_clk_composite(hw);
202 const struct clk_ops *gate_ops = composite->gate_ops;
203 struct clk_hw *gate_hw = composite->gate_hw;
205 __clk_hw_set_clk(gate_hw, hw);
207 return gate_ops->is_enabled(gate_hw);
210 static int clk_composite_enable(struct clk_hw *hw)
212 struct clk_composite *composite = to_clk_composite(hw);
213 const struct clk_ops *gate_ops = composite->gate_ops;
214 struct clk_hw *gate_hw = composite->gate_hw;
216 __clk_hw_set_clk(gate_hw, hw);
218 return gate_ops->enable(gate_hw);
221 static void clk_composite_disable(struct clk_hw *hw)
223 struct clk_composite *composite = to_clk_composite(hw);
224 const struct clk_ops *gate_ops = composite->gate_ops;
225 struct clk_hw *gate_hw = composite->gate_hw;
227 __clk_hw_set_clk(gate_hw, hw);
229 gate_ops->disable(gate_hw);
232 static struct clk_hw *__clk_hw_register_composite(struct device *dev,
233 const char *name, const char * const *parent_names,
234 const struct clk_parent_data *pdata, int num_parents,
235 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
236 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
237 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
241 struct clk_init_data init = {};
242 struct clk_composite *composite;
243 struct clk_ops *clk_composite_ops;
246 composite = kzalloc(sizeof(*composite), GFP_KERNEL);
248 return ERR_PTR(-ENOMEM);
253 init.parent_names = parent_names;
255 init.parent_data = pdata;
256 init.num_parents = num_parents;
259 clk_composite_ops = &composite->ops;
261 if (mux_hw && mux_ops) {
262 if (!mux_ops->get_parent) {
263 hw = ERR_PTR(-EINVAL);
267 composite->mux_hw = mux_hw;
268 composite->mux_ops = mux_ops;
269 clk_composite_ops->get_parent = clk_composite_get_parent;
270 if (mux_ops->set_parent)
271 clk_composite_ops->set_parent = clk_composite_set_parent;
272 if (mux_ops->determine_rate)
273 clk_composite_ops->determine_rate = clk_composite_determine_rate;
276 if (rate_hw && rate_ops) {
277 if (!rate_ops->recalc_rate) {
278 hw = ERR_PTR(-EINVAL);
281 clk_composite_ops->recalc_rate = clk_composite_recalc_rate;
283 if (rate_ops->determine_rate)
284 clk_composite_ops->determine_rate =
285 clk_composite_determine_rate;
286 else if (rate_ops->round_rate)
287 clk_composite_ops->round_rate =
288 clk_composite_round_rate;
290 /* .set_rate requires either .round_rate or .determine_rate */
291 if (rate_ops->set_rate) {
292 if (rate_ops->determine_rate || rate_ops->round_rate)
293 clk_composite_ops->set_rate =
294 clk_composite_set_rate;
296 WARN(1, "%s: missing round_rate op is required\n",
300 composite->rate_hw = rate_hw;
301 composite->rate_ops = rate_ops;
304 if (mux_hw && mux_ops && rate_hw && rate_ops) {
305 if (mux_ops->set_parent && rate_ops->set_rate)
306 clk_composite_ops->set_rate_and_parent =
307 clk_composite_set_rate_and_parent;
310 if (gate_hw && gate_ops) {
311 if (!gate_ops->is_enabled || !gate_ops->enable ||
312 !gate_ops->disable) {
313 hw = ERR_PTR(-EINVAL);
317 composite->gate_hw = gate_hw;
318 composite->gate_ops = gate_ops;
319 clk_composite_ops->is_enabled = clk_composite_is_enabled;
320 clk_composite_ops->enable = clk_composite_enable;
321 clk_composite_ops->disable = clk_composite_disable;
324 init.ops = clk_composite_ops;
325 composite->hw.init = &init;
327 ret = clk_hw_register(dev, hw);
333 if (composite->mux_hw)
334 composite->mux_hw->clk = hw->clk;
336 if (composite->rate_hw)
337 composite->rate_hw->clk = hw->clk;
339 if (composite->gate_hw)
340 composite->gate_hw->clk = hw->clk;
349 struct clk_hw *clk_hw_register_composite(struct device *dev, const char *name,
350 const char * const *parent_names, int num_parents,
351 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
352 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
353 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
356 return __clk_hw_register_composite(dev, name, parent_names, NULL,
357 num_parents, mux_hw, mux_ops,
358 rate_hw, rate_ops, gate_hw,
361 EXPORT_SYMBOL_GPL(clk_hw_register_composite);
363 struct clk_hw *clk_hw_register_composite_pdata(struct device *dev,
365 const struct clk_parent_data *parent_data,
367 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
368 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
369 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
372 return __clk_hw_register_composite(dev, name, NULL, parent_data,
373 num_parents, mux_hw, mux_ops,
374 rate_hw, rate_ops, gate_hw,
378 struct clk *clk_register_composite(struct device *dev, const char *name,
379 const char * const *parent_names, int num_parents,
380 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
381 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
382 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
387 hw = clk_hw_register_composite(dev, name, parent_names, num_parents,
388 mux_hw, mux_ops, rate_hw, rate_ops, gate_hw, gate_ops,
394 EXPORT_SYMBOL_GPL(clk_register_composite);
396 struct clk *clk_register_composite_pdata(struct device *dev, const char *name,
397 const struct clk_parent_data *parent_data,
399 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
400 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
401 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
406 hw = clk_hw_register_composite_pdata(dev, name, parent_data,
407 num_parents, mux_hw, mux_ops, rate_hw, rate_ops,
408 gate_hw, gate_ops, flags);
414 void clk_unregister_composite(struct clk *clk)
416 struct clk_composite *composite;
419 hw = __clk_get_hw(clk);
423 composite = to_clk_composite(hw);
429 void clk_hw_unregister_composite(struct clk_hw *hw)
431 struct clk_composite *composite;
433 composite = to_clk_composite(hw);
435 clk_hw_unregister(hw);
438 EXPORT_SYMBOL_GPL(clk_hw_unregister_composite);
440 static void devm_clk_hw_release_composite(struct device *dev, void *res)
442 clk_hw_unregister_composite(*(struct clk_hw **)res);
445 static struct clk_hw *__devm_clk_hw_register_composite(struct device *dev,
446 const char *name, const char * const *parent_names,
447 const struct clk_parent_data *pdata, int num_parents,
448 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
449 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
450 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
453 struct clk_hw **ptr, *hw;
455 ptr = devres_alloc(devm_clk_hw_release_composite, sizeof(*ptr),
458 return ERR_PTR(-ENOMEM);
460 hw = __clk_hw_register_composite(dev, name, parent_names, pdata,
461 num_parents, mux_hw, mux_ops, rate_hw,
462 rate_ops, gate_hw, gate_ops, flags);
466 devres_add(dev, ptr);
474 struct clk_hw *devm_clk_hw_register_composite_pdata(struct device *dev,
476 const struct clk_parent_data *parent_data,
478 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
479 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
480 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
483 return __devm_clk_hw_register_composite(dev, name, NULL, parent_data,
484 num_parents, mux_hw, mux_ops,
485 rate_hw, rate_ops, gate_hw,