1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
4 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
5 *
6 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
7 */
8
9 #include <linux/clk.h>
10 #include <linux/clk-provider.h>
11 #include <linux/clk/clk-conf.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/spinlock.h>
15 #include <linux/err.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
18 #include <linux/of.h>
19 #include <linux/device.h>
20 #include <linux/init.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/sched.h>
23 #include <linux/clkdev.h>
24
25 #include "clk.h"
26
27 static DEFINE_SPINLOCK(enable_lock);
28 static DEFINE_MUTEX(prepare_lock);
29
30 static struct task_struct *prepare_owner;
31 static struct task_struct *enable_owner;
32
33 static int prepare_refcnt;
34 static int enable_refcnt;
35
36 static HLIST_HEAD(clk_root_list);
37 static HLIST_HEAD(clk_orphan_list);
38 static LIST_HEAD(clk_notifier_list);
39
40 static struct hlist_head *all_lists[] = {
41 &clk_root_list,
42 &clk_orphan_list,
43 NULL,
44 };
45
46 /*** private data structures ***/
47
48 struct clk_parent_map {
49 const struct clk_hw *hw;
50 struct clk_core *core;
51 const char *fw_name;
52 const char *name;
53 int index;
54 };
55
56 struct clk_core {
57 const char *name;
58 const struct clk_ops *ops;
59 struct clk_hw *hw;
60 struct module *owner;
61 struct device *dev;
62 struct device_node *of_node;
63 struct clk_core *parent;
64 struct clk_parent_map *parents;
65 u8 num_parents;
66 u8 new_parent_index;
67 unsigned long rate;
68 unsigned long req_rate;
69 unsigned long new_rate;
70 struct clk_core *new_parent;
71 struct clk_core *new_child;
72 unsigned long flags;
73 bool orphan;
74 bool rpm_enabled;
75 unsigned int enable_count;
76 unsigned int prepare_count;
77 unsigned int protect_count;
78 unsigned long min_rate;
79 unsigned long max_rate;
80 unsigned long accuracy;
81 int phase;
82 struct clk_duty duty;
83 struct hlist_head children;
84 struct hlist_node child_node;
85 struct hlist_head clks;
86 unsigned int notifier_count;
87 #ifdef CONFIG_DEBUG_FS
88 struct dentry *dentry;
89 struct hlist_node debug_node;
90 #endif
91 struct kref ref;
92 };
93
94 #define CREATE_TRACE_POINTS
95 #include <trace/events/clk.h>
96
97 struct clk {
98 struct clk_core *core;
99 struct device *dev;
100 const char *dev_id;
101 const char *con_id;
102 unsigned long min_rate;
103 unsigned long max_rate;
104 unsigned int exclusive_count;
105 struct hlist_node clks_node;
106 };
107
108 /*** runtime pm ***/
clk_pm_runtime_get(struct clk_core * core)109 static int clk_pm_runtime_get(struct clk_core *core)
110 {
111 int ret;
112
113 if (!core->rpm_enabled)
114 return 0;
115
116 ret = pm_runtime_get_sync(core->dev);
117 if (ret < 0) {
118 pm_runtime_put_noidle(core->dev);
119 return ret;
120 }
121 return 0;
122 }
123
clk_pm_runtime_put(struct clk_core * core)124 static void clk_pm_runtime_put(struct clk_core *core)
125 {
126 if (!core->rpm_enabled)
127 return;
128
129 pm_runtime_put_sync(core->dev);
130 }
131
132 /*** locking ***/
clk_prepare_lock(void)133 static void clk_prepare_lock(void)
134 {
135 if (!mutex_trylock(&prepare_lock)) {
136 if (prepare_owner == current) {
137 prepare_refcnt++;
138 return;
139 }
140 mutex_lock(&prepare_lock);
141 }
142 WARN_ON_ONCE(prepare_owner != NULL);
143 WARN_ON_ONCE(prepare_refcnt != 0);
144 prepare_owner = current;
145 prepare_refcnt = 1;
146 }
147
clk_prepare_unlock(void)148 static void clk_prepare_unlock(void)
149 {
150 WARN_ON_ONCE(prepare_owner != current);
151 WARN_ON_ONCE(prepare_refcnt == 0);
152
153 if (--prepare_refcnt)
154 return;
155 prepare_owner = NULL;
156 mutex_unlock(&prepare_lock);
157 }
158
clk_enable_lock(void)159 static unsigned long clk_enable_lock(void)
160 __acquires(enable_lock)
161 {
162 unsigned long flags;
163
164 /*
165 * On UP systems, spin_trylock_irqsave() always returns true, even if
166 * we already hold the lock. So, in that case, we rely only on
167 * reference counting.
168 */
169 if (!IS_ENABLED(CONFIG_SMP) ||
170 !spin_trylock_irqsave(&enable_lock, flags)) {
171 if (enable_owner == current) {
172 enable_refcnt++;
173 __acquire(enable_lock);
174 if (!IS_ENABLED(CONFIG_SMP))
175 local_save_flags(flags);
176 return flags;
177 }
178 spin_lock_irqsave(&enable_lock, flags);
179 }
180 WARN_ON_ONCE(enable_owner != NULL);
181 WARN_ON_ONCE(enable_refcnt != 0);
182 enable_owner = current;
183 enable_refcnt = 1;
184 return flags;
185 }
186
clk_enable_unlock(unsigned long flags)187 static void clk_enable_unlock(unsigned long flags)
188 __releases(enable_lock)
189 {
190 WARN_ON_ONCE(enable_owner != current);
191 WARN_ON_ONCE(enable_refcnt == 0);
192
193 if (--enable_refcnt) {
194 __release(enable_lock);
195 return;
196 }
197 enable_owner = NULL;
198 spin_unlock_irqrestore(&enable_lock, flags);
199 }
200
clk_core_rate_is_protected(struct clk_core * core)201 static bool clk_core_rate_is_protected(struct clk_core *core)
202 {
203 return core->protect_count;
204 }
205
clk_core_is_prepared(struct clk_core * core)206 static bool clk_core_is_prepared(struct clk_core *core)
207 {
208 bool ret = false;
209
210 /*
211 * .is_prepared is optional for clocks that can prepare
212 * fall back to software usage counter if it is missing
213 */
214 if (!core->ops->is_prepared)
215 return core->prepare_count;
216
217 if (!clk_pm_runtime_get(core)) {
218 ret = core->ops->is_prepared(core->hw);
219 clk_pm_runtime_put(core);
220 }
221
222 return ret;
223 }
224
clk_core_is_enabled(struct clk_core * core)225 static bool clk_core_is_enabled(struct clk_core *core)
226 {
227 bool ret = false;
228
229 /*
230 * .is_enabled is only mandatory for clocks that gate
231 * fall back to software usage counter if .is_enabled is missing
232 */
233 if (!core->ops->is_enabled)
234 return core->enable_count;
235
236 /*
237 * Check if clock controller's device is runtime active before
238 * calling .is_enabled callback. If not, assume that clock is
239 * disabled, because we might be called from atomic context, from
240 * which pm_runtime_get() is not allowed.
241 * This function is called mainly from clk_disable_unused_subtree,
242 * which ensures proper runtime pm activation of controller before
243 * taking enable spinlock, but the below check is needed if one tries
244 * to call it from other places.
245 */
246 if (core->rpm_enabled) {
247 pm_runtime_get_noresume(core->dev);
248 if (!pm_runtime_active(core->dev)) {
249 ret = false;
250 goto done;
251 }
252 }
253
254 /*
255 * This could be called with the enable lock held, or from atomic
256 * context. If the parent isn't enabled already, we can't do
257 * anything here. We can also assume this clock isn't enabled.
258 */
259 if ((core->flags & CLK_OPS_PARENT_ENABLE) && core->parent)
260 if (!clk_core_is_enabled(core->parent)) {
261 ret = false;
262 goto done;
263 }
264
265 ret = core->ops->is_enabled(core->hw);
266 done:
267 if (core->rpm_enabled)
268 pm_runtime_put(core->dev);
269
270 return ret;
271 }
272
273 /*** helper functions ***/
274
__clk_get_name(const struct clk * clk)275 const char *__clk_get_name(const struct clk *clk)
276 {
277 return !clk ? NULL : clk->core->name;
278 }
279 EXPORT_SYMBOL_GPL(__clk_get_name);
280
clk_hw_get_name(const struct clk_hw * hw)281 const char *clk_hw_get_name(const struct clk_hw *hw)
282 {
283 return hw->core->name;
284 }
285 EXPORT_SYMBOL_GPL(clk_hw_get_name);
286
__clk_get_hw(struct clk * clk)287 struct clk_hw *__clk_get_hw(struct clk *clk)
288 {
289 return !clk ? NULL : clk->core->hw;
290 }
291 EXPORT_SYMBOL_GPL(__clk_get_hw);
292
clk_hw_get_num_parents(const struct clk_hw * hw)293 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
294 {
295 return hw->core->num_parents;
296 }
297 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
298
clk_hw_get_parent(const struct clk_hw * hw)299 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
300 {
301 return hw->core->parent ? hw->core->parent->hw : NULL;
302 }
303 EXPORT_SYMBOL_GPL(clk_hw_get_parent);
304
__clk_lookup_subtree(const char * name,struct clk_core * core)305 static struct clk_core *__clk_lookup_subtree(const char *name,
306 struct clk_core *core)
307 {
308 struct clk_core *child;
309 struct clk_core *ret;
310
311 if (!strcmp(core->name, name))
312 return core;
313
314 hlist_for_each_entry(child, &core->children, child_node) {
315 ret = __clk_lookup_subtree(name, child);
316 if (ret)
317 return ret;
318 }
319
320 return NULL;
321 }
322
clk_core_lookup(const char * name)323 static struct clk_core *clk_core_lookup(const char *name)
324 {
325 struct clk_core *root_clk;
326 struct clk_core *ret;
327
328 if (!name)
329 return NULL;
330
331 /* search the 'proper' clk tree first */
332 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
333 ret = __clk_lookup_subtree(name, root_clk);
334 if (ret)
335 return ret;
336 }
337
338 /* if not found, then search the orphan tree */
339 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
340 ret = __clk_lookup_subtree(name, root_clk);
341 if (ret)
342 return ret;
343 }
344
345 return NULL;
346 }
347
348 #ifdef CONFIG_OF
349 static int of_parse_clkspec(const struct device_node *np, int index,
350 const char *name, struct of_phandle_args *out_args);
351 static struct clk_hw *
352 of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec);
353 #else
of_parse_clkspec(const struct device_node * np,int index,const char * name,struct of_phandle_args * out_args)354 static inline int of_parse_clkspec(const struct device_node *np, int index,
355 const char *name,
356 struct of_phandle_args *out_args)
357 {
358 return -ENOENT;
359 }
360 static inline struct clk_hw *
of_clk_get_hw_from_clkspec(struct of_phandle_args * clkspec)361 of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
362 {
363 return ERR_PTR(-ENOENT);
364 }
365 #endif
366
367 /**
368 * clk_core_get - Find the clk_core parent of a clk
369 * @core: clk to find parent of
370 * @p_index: parent index to search for
371 *
372 * This is the preferred method for clk providers to find the parent of a
373 * clk when that parent is external to the clk controller. The parent_names
374 * array is indexed and treated as a local name matching a string in the device
375 * node's 'clock-names' property or as the 'con_id' matching the device's
376 * dev_name() in a clk_lookup. This allows clk providers to use their own
377 * namespace instead of looking for a globally unique parent string.
378 *
379 * For example the following DT snippet would allow a clock registered by the
380 * clock-controller@c001 that has a clk_init_data::parent_data array
381 * with 'xtal' in the 'name' member to find the clock provided by the
382 * clock-controller@f00abcd without needing to get the globally unique name of
383 * the xtal clk.
384 *
385 * parent: clock-controller@f00abcd {
386 * reg = <0xf00abcd 0xabcd>;
387 * #clock-cells = <0>;
388 * };
389 *
390 * clock-controller@c001 {
391 * reg = <0xc001 0xf00d>;
392 * clocks = <&parent>;
393 * clock-names = "xtal";
394 * #clock-cells = <1>;
395 * };
396 *
397 * Returns: -ENOENT when the provider can't be found or the clk doesn't
398 * exist in the provider or the name can't be found in the DT node or
399 * in a clkdev lookup. NULL when the provider knows about the clk but it
400 * isn't provided on this system.
401 * A valid clk_core pointer when the clk can be found in the provider.
402 */
clk_core_get(struct clk_core * core,u8 p_index)403 static struct clk_core *clk_core_get(struct clk_core *core, u8 p_index)
404 {
405 const char *name = core->parents[p_index].fw_name;
406 int index = core->parents[p_index].index;
407 struct clk_hw *hw = ERR_PTR(-ENOENT);
408 struct device *dev = core->dev;
409 const char *dev_id = dev ? dev_name(dev) : NULL;
410 struct device_node *np = core->of_node;
411 struct of_phandle_args clkspec;
412
413 if (np && (name || index >= 0) &&
414 !of_parse_clkspec(np, index, name, &clkspec)) {
415 hw = of_clk_get_hw_from_clkspec(&clkspec);
416 of_node_put(clkspec.np);
417 } else if (name) {
418 /*
419 * If the DT search above couldn't find the provider fallback to
420 * looking up via clkdev based clk_lookups.
421 */
422 hw = clk_find_hw(dev_id, name);
423 }
424
425 if (IS_ERR(hw))
426 return ERR_CAST(hw);
427
428 return hw->core;
429 }
430
clk_core_fill_parent_index(struct clk_core * core,u8 index)431 static void clk_core_fill_parent_index(struct clk_core *core, u8 index)
432 {
433 struct clk_parent_map *entry = &core->parents[index];
434 struct clk_core *parent = ERR_PTR(-ENOENT);
435
436 if (entry->hw) {
437 parent = entry->hw->core;
438 /*
439 * We have a direct reference but it isn't registered yet?
440 * Orphan it and let clk_reparent() update the orphan status
441 * when the parent is registered.
442 */
443 if (!parent)
444 parent = ERR_PTR(-EPROBE_DEFER);
445 } else {
446 parent = clk_core_get(core, index);
447 if (PTR_ERR(parent) == -ENOENT && entry->name)
448 parent = clk_core_lookup(entry->name);
449 }
450
451 /* Only cache it if it's not an error */
452 if (!IS_ERR(parent))
453 entry->core = parent;
454 }
455
clk_core_get_parent_by_index(struct clk_core * core,u8 index)456 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
457 u8 index)
458 {
459 if (!core || index >= core->num_parents || !core->parents)
460 return NULL;
461
462 if (!core->parents[index].core)
463 clk_core_fill_parent_index(core, index);
464
465 return core->parents[index].core;
466 }
467
468 struct clk_hw *
clk_hw_get_parent_by_index(const struct clk_hw * hw,unsigned int index)469 clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
470 {
471 struct clk_core *parent;
472
473 parent = clk_core_get_parent_by_index(hw->core, index);
474
475 return !parent ? NULL : parent->hw;
476 }
477 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
478
__clk_get_enable_count(struct clk * clk)479 unsigned int __clk_get_enable_count(struct clk *clk)
480 {
481 return !clk ? 0 : clk->core->enable_count;
482 }
483
clk_core_get_rate_nolock(struct clk_core * core)484 static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
485 {
486 if (!core)
487 return 0;
488
489 if (!core->num_parents || core->parent)
490 return core->rate;
491
492 /*
493 * Clk must have a parent because num_parents > 0 but the parent isn't
494 * known yet. Best to return 0 as the rate of this clk until we can
495 * properly recalc the rate based on the parent's rate.
496 */
497 return 0;
498 }
499
clk_hw_get_rate(const struct clk_hw * hw)500 unsigned long clk_hw_get_rate(const struct clk_hw *hw)
501 {
502 return clk_core_get_rate_nolock(hw->core);
503 }
504 EXPORT_SYMBOL_GPL(clk_hw_get_rate);
505
clk_core_get_accuracy_no_lock(struct clk_core * core)506 static unsigned long clk_core_get_accuracy_no_lock(struct clk_core *core)
507 {
508 if (!core)
509 return 0;
510
511 return core->accuracy;
512 }
513
clk_hw_get_flags(const struct clk_hw * hw)514 unsigned long clk_hw_get_flags(const struct clk_hw *hw)
515 {
516 return hw->core->flags;
517 }
518 EXPORT_SYMBOL_GPL(clk_hw_get_flags);
519
clk_hw_is_prepared(const struct clk_hw * hw)520 bool clk_hw_is_prepared(const struct clk_hw *hw)
521 {
522 return clk_core_is_prepared(hw->core);
523 }
524 EXPORT_SYMBOL_GPL(clk_hw_is_prepared);
525
clk_hw_rate_is_protected(const struct clk_hw * hw)526 bool clk_hw_rate_is_protected(const struct clk_hw *hw)
527 {
528 return clk_core_rate_is_protected(hw->core);
529 }
530 EXPORT_SYMBOL_GPL(clk_hw_rate_is_protected);
531
clk_hw_is_enabled(const struct clk_hw * hw)532 bool clk_hw_is_enabled(const struct clk_hw *hw)
533 {
534 return clk_core_is_enabled(hw->core);
535 }
536 EXPORT_SYMBOL_GPL(clk_hw_is_enabled);
537
__clk_is_enabled(struct clk * clk)538 bool __clk_is_enabled(struct clk *clk)
539 {
540 if (!clk)
541 return false;
542
543 return clk_core_is_enabled(clk->core);
544 }
545 EXPORT_SYMBOL_GPL(__clk_is_enabled);
546
mux_is_better_rate(unsigned long rate,unsigned long now,unsigned long best,unsigned long flags)547 static bool mux_is_better_rate(unsigned long rate, unsigned long now,
548 unsigned long best, unsigned long flags)
549 {
550 if (flags & CLK_MUX_ROUND_CLOSEST)
551 return abs(now - rate) < abs(best - rate);
552
553 return now <= rate && now > best;
554 }
555
clk_mux_determine_rate_flags(struct clk_hw * hw,struct clk_rate_request * req,unsigned long flags)556 int clk_mux_determine_rate_flags(struct clk_hw *hw,
557 struct clk_rate_request *req,
558 unsigned long flags)
559 {
560 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
561 int i, num_parents, ret;
562 unsigned long best = 0;
563 struct clk_rate_request parent_req = *req;
564
565 /* if NO_REPARENT flag set, pass through to current parent */
566 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
567 parent = core->parent;
568 if (core->flags & CLK_SET_RATE_PARENT) {
569 ret = __clk_determine_rate(parent ? parent->hw : NULL,
570 &parent_req);
571 if (ret)
572 return ret;
573
574 best = parent_req.rate;
575 } else if (parent) {
576 best = clk_core_get_rate_nolock(parent);
577 } else {
578 best = clk_core_get_rate_nolock(core);
579 }
580
581 goto out;
582 }
583
584 /* find the parent that can provide the fastest rate <= rate */
585 num_parents = core->num_parents;
586 for (i = 0; i < num_parents; i++) {
587 parent = clk_core_get_parent_by_index(core, i);
588 if (!parent)
589 continue;
590
591 if (core->flags & CLK_SET_RATE_PARENT) {
592 parent_req = *req;
593 ret = __clk_determine_rate(parent->hw, &parent_req);
594 if (ret)
595 continue;
596 } else {
597 parent_req.rate = clk_core_get_rate_nolock(parent);
598 }
599
600 if (mux_is_better_rate(req->rate, parent_req.rate,
601 best, flags)) {
602 best_parent = parent;
603 best = parent_req.rate;
604 }
605 }
606
607 if (!best_parent)
608 return -EINVAL;
609
610 out:
611 if (best_parent)
612 req->best_parent_hw = best_parent->hw;
613 req->best_parent_rate = best;
614 req->rate = best;
615
616 return 0;
617 }
618 EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
619
__clk_lookup(const char * name)620 struct clk *__clk_lookup(const char *name)
621 {
622 struct clk_core *core = clk_core_lookup(name);
623
624 return !core ? NULL : core->hw->clk;
625 }
626
clk_core_get_boundaries(struct clk_core * core,unsigned long * min_rate,unsigned long * max_rate)627 static void clk_core_get_boundaries(struct clk_core *core,
628 unsigned long *min_rate,
629 unsigned long *max_rate)
630 {
631 struct clk *clk_user;
632
633 lockdep_assert_held(&prepare_lock);
634
635 *min_rate = core->min_rate;
636 *max_rate = core->max_rate;
637
638 hlist_for_each_entry(clk_user, &core->clks, clks_node)
639 *min_rate = max(*min_rate, clk_user->min_rate);
640
641 hlist_for_each_entry(clk_user, &core->clks, clks_node)
642 *max_rate = min(*max_rate, clk_user->max_rate);
643 }
644
clk_core_check_boundaries(struct clk_core * core,unsigned long min_rate,unsigned long max_rate)645 static bool clk_core_check_boundaries(struct clk_core *core,
646 unsigned long min_rate,
647 unsigned long max_rate)
648 {
649 struct clk *user;
650
651 lockdep_assert_held(&prepare_lock);
652
653 if (min_rate > core->max_rate || max_rate < core->min_rate)
654 return false;
655
656 hlist_for_each_entry(user, &core->clks, clks_node)
657 if (min_rate > user->max_rate || max_rate < user->min_rate)
658 return false;
659
660 return true;
661 }
662
clk_hw_set_rate_range(struct clk_hw * hw,unsigned long min_rate,unsigned long max_rate)663 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
664 unsigned long max_rate)
665 {
666 hw->core->min_rate = min_rate;
667 hw->core->max_rate = max_rate;
668 }
669 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
670
671 /*
672 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
673 * @hw: mux type clk to determine rate on
674 * @req: rate request, also used to return preferred parent and frequencies
675 *
676 * Helper for finding best parent to provide a given frequency. This can be used
677 * directly as a determine_rate callback (e.g. for a mux), or from a more
678 * complex clock that may combine a mux with other operations.
679 *
680 * Returns: 0 on success, -EERROR value on error
681 */
__clk_mux_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)682 int __clk_mux_determine_rate(struct clk_hw *hw,
683 struct clk_rate_request *req)
684 {
685 return clk_mux_determine_rate_flags(hw, req, 0);
686 }
687 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
688
__clk_mux_determine_rate_closest(struct clk_hw * hw,struct clk_rate_request * req)689 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
690 struct clk_rate_request *req)
691 {
692 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
693 }
694 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
695
696 /*** clk api ***/
697
clk_core_rate_unprotect(struct clk_core * core)698 static void clk_core_rate_unprotect(struct clk_core *core)
699 {
700 lockdep_assert_held(&prepare_lock);
701
702 if (!core)
703 return;
704
705 if (WARN(core->protect_count == 0,
706 "%s already unprotected\n", core->name))
707 return;
708
709 if (--core->protect_count > 0)
710 return;
711
712 clk_core_rate_unprotect(core->parent);
713 }
714
clk_core_rate_nuke_protect(struct clk_core * core)715 static int clk_core_rate_nuke_protect(struct clk_core *core)
716 {
717 int ret;
718
719 lockdep_assert_held(&prepare_lock);
720
721 if (!core)
722 return -EINVAL;
723
724 if (core->protect_count == 0)
725 return 0;
726
727 ret = core->protect_count;
728 core->protect_count = 1;
729 clk_core_rate_unprotect(core);
730
731 return ret;
732 }
733
734 /**
735 * clk_rate_exclusive_put - release exclusivity over clock rate control
736 * @clk: the clk over which the exclusivity is released
737 *
738 * clk_rate_exclusive_put() completes a critical section during which a clock
739 * consumer cannot tolerate any other consumer making any operation on the
740 * clock which could result in a rate change or rate glitch. Exclusive clocks
741 * cannot have their rate changed, either directly or indirectly due to changes
742 * further up the parent chain of clocks. As a result, clocks up parent chain
743 * also get under exclusive control of the calling consumer.
744 *
745 * If exlusivity is claimed more than once on clock, even by the same consumer,
746 * the rate effectively gets locked as exclusivity can't be preempted.
747 *
748 * Calls to clk_rate_exclusive_put() must be balanced with calls to
749 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
750 * error status.
751 */
clk_rate_exclusive_put(struct clk * clk)752 void clk_rate_exclusive_put(struct clk *clk)
753 {
754 if (!clk)
755 return;
756
757 clk_prepare_lock();
758
759 /*
760 * if there is something wrong with this consumer protect count, stop
761 * here before messing with the provider
762 */
763 if (WARN_ON(clk->exclusive_count <= 0))
764 goto out;
765
766 clk_core_rate_unprotect(clk->core);
767 clk->exclusive_count--;
768 out:
769 clk_prepare_unlock();
770 }
771 EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
772
clk_core_rate_protect(struct clk_core * core)773 static void clk_core_rate_protect(struct clk_core *core)
774 {
775 lockdep_assert_held(&prepare_lock);
776
777 if (!core)
778 return;
779
780 if (core->protect_count == 0)
781 clk_core_rate_protect(core->parent);
782
783 core->protect_count++;
784 }
785
clk_core_rate_restore_protect(struct clk_core * core,int count)786 static void clk_core_rate_restore_protect(struct clk_core *core, int count)
787 {
788 lockdep_assert_held(&prepare_lock);
789
790 if (!core)
791 return;
792
793 if (count == 0)
794 return;
795
796 clk_core_rate_protect(core);
797 core->protect_count = count;
798 }
799
800 /**
801 * clk_rate_exclusive_get - get exclusivity over the clk rate control
802 * @clk: the clk over which the exclusity of rate control is requested
803 *
804 * clk_rate_exclusive_get() begins a critical section during which a clock
805 * consumer cannot tolerate any other consumer making any operation on the
806 * clock which could result in a rate change or rate glitch. Exclusive clocks
807 * cannot have their rate changed, either directly or indirectly due to changes
808 * further up the parent chain of clocks. As a result, clocks up parent chain
809 * also get under exclusive control of the calling consumer.
810 *
811 * If exlusivity is claimed more than once on clock, even by the same consumer,
812 * the rate effectively gets locked as exclusivity can't be preempted.
813 *
814 * Calls to clk_rate_exclusive_get() should be balanced with calls to
815 * clk_rate_exclusive_put(). Calls to this function may sleep.
816 * Returns 0 on success, -EERROR otherwise
817 */
clk_rate_exclusive_get(struct clk * clk)818 int clk_rate_exclusive_get(struct clk *clk)
819 {
820 if (!clk)
821 return 0;
822
823 clk_prepare_lock();
824 clk_core_rate_protect(clk->core);
825 clk->exclusive_count++;
826 clk_prepare_unlock();
827
828 return 0;
829 }
830 EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
831
clk_core_unprepare(struct clk_core * core)832 static void clk_core_unprepare(struct clk_core *core)
833 {
834 lockdep_assert_held(&prepare_lock);
835
836 if (!core)
837 return;
838
839 if (WARN(core->prepare_count == 0,
840 "%s already unprepared\n", core->name))
841 return;
842
843 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
844 "Unpreparing critical %s\n", core->name))
845 return;
846
847 if (core->flags & CLK_SET_RATE_GATE)
848 clk_core_rate_unprotect(core);
849
850 if (--core->prepare_count > 0)
851 return;
852
853 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
854
855 trace_clk_unprepare(core);
856
857 if (core->ops->unprepare)
858 core->ops->unprepare(core->hw);
859
860 trace_clk_unprepare_complete(core);
861 clk_core_unprepare(core->parent);
862 clk_pm_runtime_put(core);
863 }
864
clk_core_unprepare_lock(struct clk_core * core)865 static void clk_core_unprepare_lock(struct clk_core *core)
866 {
867 clk_prepare_lock();
868 clk_core_unprepare(core);
869 clk_prepare_unlock();
870 }
871
872 /**
873 * clk_unprepare - undo preparation of a clock source
874 * @clk: the clk being unprepared
875 *
876 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
877 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
878 * if the operation may sleep. One example is a clk which is accessed over
879 * I2c. In the complex case a clk gate operation may require a fast and a slow
880 * part. It is this reason that clk_unprepare and clk_disable are not mutually
881 * exclusive. In fact clk_disable must be called before clk_unprepare.
882 */
clk_unprepare(struct clk * clk)883 void clk_unprepare(struct clk *clk)
884 {
885 if (IS_ERR_OR_NULL(clk))
886 return;
887
888 clk_core_unprepare_lock(clk->core);
889 }
890 EXPORT_SYMBOL_GPL(clk_unprepare);
891
clk_core_prepare(struct clk_core * core)892 static int clk_core_prepare(struct clk_core *core)
893 {
894 int ret = 0;
895
896 lockdep_assert_held(&prepare_lock);
897
898 if (!core)
899 return 0;
900
901 if (core->prepare_count == 0) {
902 ret = clk_pm_runtime_get(core);
903 if (ret)
904 return ret;
905
906 ret = clk_core_prepare(core->parent);
907 if (ret)
908 goto runtime_put;
909
910 trace_clk_prepare(core);
911
912 if (core->ops->prepare)
913 ret = core->ops->prepare(core->hw);
914
915 trace_clk_prepare_complete(core);
916
917 if (ret)
918 goto unprepare;
919 }
920
921 core->prepare_count++;
922
923 /*
924 * CLK_SET_RATE_GATE is a special case of clock protection
925 * Instead of a consumer claiming exclusive rate control, it is
926 * actually the provider which prevents any consumer from making any
927 * operation which could result in a rate change or rate glitch while
928 * the clock is prepared.
929 */
930 if (core->flags & CLK_SET_RATE_GATE)
931 clk_core_rate_protect(core);
932
933 return 0;
934 unprepare:
935 clk_core_unprepare(core->parent);
936 runtime_put:
937 clk_pm_runtime_put(core);
938 return ret;
939 }
940
clk_core_prepare_lock(struct clk_core * core)941 static int clk_core_prepare_lock(struct clk_core *core)
942 {
943 int ret;
944
945 clk_prepare_lock();
946 ret = clk_core_prepare(core);
947 clk_prepare_unlock();
948
949 return ret;
950 }
951
952 /**
953 * clk_prepare - prepare a clock source
954 * @clk: the clk being prepared
955 *
956 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
957 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
958 * operation may sleep. One example is a clk which is accessed over I2c. In
959 * the complex case a clk ungate operation may require a fast and a slow part.
960 * It is this reason that clk_prepare and clk_enable are not mutually
961 * exclusive. In fact clk_prepare must be called before clk_enable.
962 * Returns 0 on success, -EERROR otherwise.
963 */
clk_prepare(struct clk * clk)964 int clk_prepare(struct clk *clk)
965 {
966 if (!clk)
967 return 0;
968
969 return clk_core_prepare_lock(clk->core);
970 }
971 EXPORT_SYMBOL_GPL(clk_prepare);
972
clk_core_disable(struct clk_core * core)973 static void clk_core_disable(struct clk_core *core)
974 {
975 lockdep_assert_held(&enable_lock);
976
977 if (!core)
978 return;
979
980 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
981 return;
982
983 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
984 "Disabling critical %s\n", core->name))
985 return;
986
987 if (--core->enable_count > 0)
988 return;
989
990 trace_clk_disable_rcuidle(core);
991
992 if (core->ops->disable)
993 core->ops->disable(core->hw);
994
995 trace_clk_disable_complete_rcuidle(core);
996
997 clk_core_disable(core->parent);
998 }
999
clk_core_disable_lock(struct clk_core * core)1000 static void clk_core_disable_lock(struct clk_core *core)
1001 {
1002 unsigned long flags;
1003
1004 flags = clk_enable_lock();
1005 clk_core_disable(core);
1006 clk_enable_unlock(flags);
1007 }
1008
1009 /**
1010 * clk_disable - gate a clock
1011 * @clk: the clk being gated
1012 *
1013 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
1014 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
1015 * clk if the operation is fast and will never sleep. One example is a
1016 * SoC-internal clk which is controlled via simple register writes. In the
1017 * complex case a clk gate operation may require a fast and a slow part. It is
1018 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
1019 * In fact clk_disable must be called before clk_unprepare.
1020 */
clk_disable(struct clk * clk)1021 void clk_disable(struct clk *clk)
1022 {
1023 if (IS_ERR_OR_NULL(clk))
1024 return;
1025
1026 clk_core_disable_lock(clk->core);
1027 }
1028 EXPORT_SYMBOL_GPL(clk_disable);
1029
clk_core_enable(struct clk_core * core)1030 static int clk_core_enable(struct clk_core *core)
1031 {
1032 int ret = 0;
1033
1034 lockdep_assert_held(&enable_lock);
1035
1036 if (!core)
1037 return 0;
1038
1039 if (WARN(core->prepare_count == 0,
1040 "Enabling unprepared %s\n", core->name))
1041 return -ESHUTDOWN;
1042
1043 if (core->enable_count == 0) {
1044 ret = clk_core_enable(core->parent);
1045
1046 if (ret)
1047 return ret;
1048
1049 trace_clk_enable_rcuidle(core);
1050
1051 if (core->ops->enable)
1052 ret = core->ops->enable(core->hw);
1053
1054 trace_clk_enable_complete_rcuidle(core);
1055
1056 if (ret) {
1057 clk_core_disable(core->parent);
1058 return ret;
1059 }
1060 }
1061
1062 core->enable_count++;
1063 return 0;
1064 }
1065
clk_core_enable_lock(struct clk_core * core)1066 static int clk_core_enable_lock(struct clk_core *core)
1067 {
1068 unsigned long flags;
1069 int ret;
1070
1071 flags = clk_enable_lock();
1072 ret = clk_core_enable(core);
1073 clk_enable_unlock(flags);
1074
1075 return ret;
1076 }
1077
1078 /**
1079 * clk_gate_restore_context - restore context for poweroff
1080 * @hw: the clk_hw pointer of clock whose state is to be restored
1081 *
1082 * The clock gate restore context function enables or disables
1083 * the gate clocks based on the enable_count. This is done in cases
1084 * where the clock context is lost and based on the enable_count
1085 * the clock either needs to be enabled/disabled. This
1086 * helps restore the state of gate clocks.
1087 */
clk_gate_restore_context(struct clk_hw * hw)1088 void clk_gate_restore_context(struct clk_hw *hw)
1089 {
1090 struct clk_core *core = hw->core;
1091
1092 if (core->enable_count)
1093 core->ops->enable(hw);
1094 else
1095 core->ops->disable(hw);
1096 }
1097 EXPORT_SYMBOL_GPL(clk_gate_restore_context);
1098
clk_core_save_context(struct clk_core * core)1099 static int clk_core_save_context(struct clk_core *core)
1100 {
1101 struct clk_core *child;
1102 int ret = 0;
1103
1104 hlist_for_each_entry(child, &core->children, child_node) {
1105 ret = clk_core_save_context(child);
1106 if (ret < 0)
1107 return ret;
1108 }
1109
1110 if (core->ops && core->ops->save_context)
1111 ret = core->ops->save_context(core->hw);
1112
1113 return ret;
1114 }
1115
clk_core_restore_context(struct clk_core * core)1116 static void clk_core_restore_context(struct clk_core *core)
1117 {
1118 struct clk_core *child;
1119
1120 if (core->ops && core->ops->restore_context)
1121 core->ops->restore_context(core->hw);
1122
1123 hlist_for_each_entry(child, &core->children, child_node)
1124 clk_core_restore_context(child);
1125 }
1126
1127 /**
1128 * clk_save_context - save clock context for poweroff
1129 *
1130 * Saves the context of the clock register for powerstates in which the
1131 * contents of the registers will be lost. Occurs deep within the suspend
1132 * code. Returns 0 on success.
1133 */
clk_save_context(void)1134 int clk_save_context(void)
1135 {
1136 struct clk_core *clk;
1137 int ret;
1138
1139 hlist_for_each_entry(clk, &clk_root_list, child_node) {
1140 ret = clk_core_save_context(clk);
1141 if (ret < 0)
1142 return ret;
1143 }
1144
1145 hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
1146 ret = clk_core_save_context(clk);
1147 if (ret < 0)
1148 return ret;
1149 }
1150
1151 return 0;
1152 }
1153 EXPORT_SYMBOL_GPL(clk_save_context);
1154
1155 /**
1156 * clk_restore_context - restore clock context after poweroff
1157 *
1158 * Restore the saved clock context upon resume.
1159 *
1160 */
clk_restore_context(void)1161 void clk_restore_context(void)
1162 {
1163 struct clk_core *core;
1164
1165 hlist_for_each_entry(core, &clk_root_list, child_node)
1166 clk_core_restore_context(core);
1167
1168 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1169 clk_core_restore_context(core);
1170 }
1171 EXPORT_SYMBOL_GPL(clk_restore_context);
1172
1173 /**
1174 * clk_enable - ungate a clock
1175 * @clk: the clk being ungated
1176 *
1177 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1178 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1179 * if the operation will never sleep. One example is a SoC-internal clk which
1180 * is controlled via simple register writes. In the complex case a clk ungate
1181 * operation may require a fast and a slow part. It is this reason that
1182 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1183 * must be called before clk_enable. Returns 0 on success, -EERROR
1184 * otherwise.
1185 */
clk_enable(struct clk * clk)1186 int clk_enable(struct clk *clk)
1187 {
1188 if (!clk)
1189 return 0;
1190
1191 return clk_core_enable_lock(clk->core);
1192 }
1193 EXPORT_SYMBOL_GPL(clk_enable);
1194
clk_core_prepare_enable(struct clk_core * core)1195 static int clk_core_prepare_enable(struct clk_core *core)
1196 {
1197 int ret;
1198
1199 ret = clk_core_prepare_lock(core);
1200 if (ret)
1201 return ret;
1202
1203 ret = clk_core_enable_lock(core);
1204 if (ret)
1205 clk_core_unprepare_lock(core);
1206
1207 return ret;
1208 }
1209
clk_core_disable_unprepare(struct clk_core * core)1210 static void clk_core_disable_unprepare(struct clk_core *core)
1211 {
1212 clk_core_disable_lock(core);
1213 clk_core_unprepare_lock(core);
1214 }
1215
clk_unprepare_unused_subtree(struct clk_core * core)1216 static void __init clk_unprepare_unused_subtree(struct clk_core *core)
1217 {
1218 struct clk_core *child;
1219
1220 lockdep_assert_held(&prepare_lock);
1221
1222 hlist_for_each_entry(child, &core->children, child_node)
1223 clk_unprepare_unused_subtree(child);
1224
1225 if (core->prepare_count)
1226 return;
1227
1228 if (core->flags & CLK_IGNORE_UNUSED)
1229 return;
1230
1231 if (clk_pm_runtime_get(core))
1232 return;
1233
1234 if (clk_core_is_prepared(core)) {
1235 trace_clk_unprepare(core);
1236 if (core->ops->unprepare_unused)
1237 core->ops->unprepare_unused(core->hw);
1238 else if (core->ops->unprepare)
1239 core->ops->unprepare(core->hw);
1240 trace_clk_unprepare_complete(core);
1241 }
1242
1243 clk_pm_runtime_put(core);
1244 }
1245
clk_disable_unused_subtree(struct clk_core * core)1246 static void __init clk_disable_unused_subtree(struct clk_core *core)
1247 {
1248 struct clk_core *child;
1249 unsigned long flags;
1250
1251 lockdep_assert_held(&prepare_lock);
1252
1253 hlist_for_each_entry(child, &core->children, child_node)
1254 clk_disable_unused_subtree(child);
1255
1256 if (core->flags & CLK_OPS_PARENT_ENABLE)
1257 clk_core_prepare_enable(core->parent);
1258
1259 if (clk_pm_runtime_get(core))
1260 goto unprepare_out;
1261
1262 flags = clk_enable_lock();
1263
1264 if (core->enable_count)
1265 goto unlock_out;
1266
1267 if (core->flags & CLK_IGNORE_UNUSED)
1268 goto unlock_out;
1269
1270 /*
1271 * some gate clocks have special needs during the disable-unused
1272 * sequence. call .disable_unused if available, otherwise fall
1273 * back to .disable
1274 */
1275 if (clk_core_is_enabled(core)) {
1276 trace_clk_disable(core);
1277 if (core->ops->disable_unused)
1278 core->ops->disable_unused(core->hw);
1279 else if (core->ops->disable)
1280 core->ops->disable(core->hw);
1281 trace_clk_disable_complete(core);
1282 }
1283
1284 unlock_out:
1285 clk_enable_unlock(flags);
1286 clk_pm_runtime_put(core);
1287 unprepare_out:
1288 if (core->flags & CLK_OPS_PARENT_ENABLE)
1289 clk_core_disable_unprepare(core->parent);
1290 }
1291
1292 static bool clk_ignore_unused __initdata;
clk_ignore_unused_setup(char * __unused)1293 static int __init clk_ignore_unused_setup(char *__unused)
1294 {
1295 clk_ignore_unused = true;
1296 return 1;
1297 }
1298 __setup("clk_ignore_unused", clk_ignore_unused_setup);
1299
clk_disable_unused(void)1300 static int __init clk_disable_unused(void)
1301 {
1302 struct clk_core *core;
1303
1304 if (clk_ignore_unused) {
1305 pr_warn("clk: Not disabling unused clocks\n");
1306 return 0;
1307 }
1308
1309 clk_prepare_lock();
1310
1311 hlist_for_each_entry(core, &clk_root_list, child_node)
1312 clk_disable_unused_subtree(core);
1313
1314 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1315 clk_disable_unused_subtree(core);
1316
1317 hlist_for_each_entry(core, &clk_root_list, child_node)
1318 clk_unprepare_unused_subtree(core);
1319
1320 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1321 clk_unprepare_unused_subtree(core);
1322
1323 clk_prepare_unlock();
1324
1325 return 0;
1326 }
1327 late_initcall_sync(clk_disable_unused);
1328
clk_core_determine_round_nolock(struct clk_core * core,struct clk_rate_request * req)1329 static int clk_core_determine_round_nolock(struct clk_core *core,
1330 struct clk_rate_request *req)
1331 {
1332 long rate;
1333
1334 lockdep_assert_held(&prepare_lock);
1335
1336 if (!core)
1337 return 0;
1338
1339 /*
1340 * At this point, core protection will be disabled if
1341 * - if the provider is not protected at all
1342 * - if the calling consumer is the only one which has exclusivity
1343 * over the provider
1344 */
1345 if (clk_core_rate_is_protected(core)) {
1346 req->rate = core->rate;
1347 } else if (core->ops->determine_rate) {
1348 return core->ops->determine_rate(core->hw, req);
1349 } else if (core->ops->round_rate) {
1350 rate = core->ops->round_rate(core->hw, req->rate,
1351 &req->best_parent_rate);
1352 if (rate < 0)
1353 return rate;
1354
1355 req->rate = rate;
1356 } else {
1357 return -EINVAL;
1358 }
1359
1360 return 0;
1361 }
1362
clk_core_init_rate_req(struct clk_core * const core,struct clk_rate_request * req)1363 static void clk_core_init_rate_req(struct clk_core * const core,
1364 struct clk_rate_request *req)
1365 {
1366 struct clk_core *parent;
1367
1368 if (WARN_ON(!core || !req))
1369 return;
1370
1371 parent = core->parent;
1372 if (parent) {
1373 req->best_parent_hw = parent->hw;
1374 req->best_parent_rate = parent->rate;
1375 } else {
1376 req->best_parent_hw = NULL;
1377 req->best_parent_rate = 0;
1378 }
1379 }
1380
clk_core_can_round(struct clk_core * const core)1381 static bool clk_core_can_round(struct clk_core * const core)
1382 {
1383 return core->ops->determine_rate || core->ops->round_rate;
1384 }
1385
clk_core_round_rate_nolock(struct clk_core * core,struct clk_rate_request * req)1386 static int clk_core_round_rate_nolock(struct clk_core *core,
1387 struct clk_rate_request *req)
1388 {
1389 lockdep_assert_held(&prepare_lock);
1390
1391 if (!core) {
1392 req->rate = 0;
1393 return 0;
1394 }
1395
1396 clk_core_init_rate_req(core, req);
1397
1398 if (clk_core_can_round(core))
1399 return clk_core_determine_round_nolock(core, req);
1400 else if (core->flags & CLK_SET_RATE_PARENT)
1401 return clk_core_round_rate_nolock(core->parent, req);
1402
1403 req->rate = core->rate;
1404 return 0;
1405 }
1406
1407 /**
1408 * __clk_determine_rate - get the closest rate actually supported by a clock
1409 * @hw: determine the rate of this clock
1410 * @req: target rate request
1411 *
1412 * Useful for clk_ops such as .set_rate and .determine_rate.
1413 */
__clk_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)1414 int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
1415 {
1416 if (!hw) {
1417 req->rate = 0;
1418 return 0;
1419 }
1420
1421 return clk_core_round_rate_nolock(hw->core, req);
1422 }
1423 EXPORT_SYMBOL_GPL(__clk_determine_rate);
1424
1425 /**
1426 * clk_hw_round_rate() - round the given rate for a hw clk
1427 * @hw: the hw clk for which we are rounding a rate
1428 * @rate: the rate which is to be rounded
1429 *
1430 * Takes in a rate as input and rounds it to a rate that the clk can actually
1431 * use.
1432 *
1433 * Context: prepare_lock must be held.
1434 * For clk providers to call from within clk_ops such as .round_rate,
1435 * .determine_rate.
1436 *
1437 * Return: returns rounded rate of hw clk if clk supports round_rate operation
1438 * else returns the parent rate.
1439 */
clk_hw_round_rate(struct clk_hw * hw,unsigned long rate)1440 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1441 {
1442 int ret;
1443 struct clk_rate_request req;
1444
1445 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1446 req.rate = rate;
1447
1448 ret = clk_core_round_rate_nolock(hw->core, &req);
1449 if (ret)
1450 return 0;
1451
1452 return req.rate;
1453 }
1454 EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1455
1456 /**
1457 * clk_round_rate - round the given rate for a clk
1458 * @clk: the clk for which we are rounding a rate
1459 * @rate: the rate which is to be rounded
1460 *
1461 * Takes in a rate as input and rounds it to a rate that the clk can actually
1462 * use which is then returned. If clk doesn't support round_rate operation
1463 * then the parent rate is returned.
1464 */
clk_round_rate(struct clk * clk,unsigned long rate)1465 long clk_round_rate(struct clk *clk, unsigned long rate)
1466 {
1467 struct clk_rate_request req;
1468 int ret;
1469
1470 if (!clk)
1471 return 0;
1472
1473 clk_prepare_lock();
1474
1475 if (clk->exclusive_count)
1476 clk_core_rate_unprotect(clk->core);
1477
1478 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1479 req.rate = rate;
1480
1481 ret = clk_core_round_rate_nolock(clk->core, &req);
1482
1483 if (clk->exclusive_count)
1484 clk_core_rate_protect(clk->core);
1485
1486 clk_prepare_unlock();
1487
1488 if (ret)
1489 return ret;
1490
1491 return req.rate;
1492 }
1493 EXPORT_SYMBOL_GPL(clk_round_rate);
1494
1495 /**
1496 * __clk_notify - call clk notifier chain
1497 * @core: clk that is changing rate
1498 * @msg: clk notifier type (see include/linux/clk.h)
1499 * @old_rate: old clk rate
1500 * @new_rate: new clk rate
1501 *
1502 * Triggers a notifier call chain on the clk rate-change notification
1503 * for 'clk'. Passes a pointer to the struct clk and the previous
1504 * and current rates to the notifier callback. Intended to be called by
1505 * internal clock code only. Returns NOTIFY_DONE from the last driver
1506 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1507 * a driver returns that.
1508 */
__clk_notify(struct clk_core * core,unsigned long msg,unsigned long old_rate,unsigned long new_rate)1509 static int __clk_notify(struct clk_core *core, unsigned long msg,
1510 unsigned long old_rate, unsigned long new_rate)
1511 {
1512 struct clk_notifier *cn;
1513 struct clk_notifier_data cnd;
1514 int ret = NOTIFY_DONE;
1515
1516 cnd.old_rate = old_rate;
1517 cnd.new_rate = new_rate;
1518
1519 list_for_each_entry(cn, &clk_notifier_list, node) {
1520 if (cn->clk->core == core) {
1521 cnd.clk = cn->clk;
1522 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1523 &cnd);
1524 if (ret & NOTIFY_STOP_MASK)
1525 return ret;
1526 }
1527 }
1528
1529 return ret;
1530 }
1531
1532 /**
1533 * __clk_recalc_accuracies
1534 * @core: first clk in the subtree
1535 *
1536 * Walks the subtree of clks starting with clk and recalculates accuracies as
1537 * it goes. Note that if a clk does not implement the .recalc_accuracy
1538 * callback then it is assumed that the clock will take on the accuracy of its
1539 * parent.
1540 */
__clk_recalc_accuracies(struct clk_core * core)1541 static void __clk_recalc_accuracies(struct clk_core *core)
1542 {
1543 unsigned long parent_accuracy = 0;
1544 struct clk_core *child;
1545
1546 lockdep_assert_held(&prepare_lock);
1547
1548 if (core->parent)
1549 parent_accuracy = core->parent->accuracy;
1550
1551 if (core->ops->recalc_accuracy)
1552 core->accuracy = core->ops->recalc_accuracy(core->hw,
1553 parent_accuracy);
1554 else
1555 core->accuracy = parent_accuracy;
1556
1557 hlist_for_each_entry(child, &core->children, child_node)
1558 __clk_recalc_accuracies(child);
1559 }
1560
clk_core_get_accuracy_recalc(struct clk_core * core)1561 static long clk_core_get_accuracy_recalc(struct clk_core *core)
1562 {
1563 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1564 __clk_recalc_accuracies(core);
1565
1566 return clk_core_get_accuracy_no_lock(core);
1567 }
1568
1569 /**
1570 * clk_get_accuracy - return the accuracy of clk
1571 * @clk: the clk whose accuracy is being returned
1572 *
1573 * Simply returns the cached accuracy of the clk, unless
1574 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1575 * issued.
1576 * If clk is NULL then returns 0.
1577 */
clk_get_accuracy(struct clk * clk)1578 long clk_get_accuracy(struct clk *clk)
1579 {
1580 long accuracy;
1581
1582 if (!clk)
1583 return 0;
1584
1585 clk_prepare_lock();
1586 accuracy = clk_core_get_accuracy_recalc(clk->core);
1587 clk_prepare_unlock();
1588
1589 return accuracy;
1590 }
1591 EXPORT_SYMBOL_GPL(clk_get_accuracy);
1592
clk_recalc(struct clk_core * core,unsigned long parent_rate)1593 static unsigned long clk_recalc(struct clk_core *core,
1594 unsigned long parent_rate)
1595 {
1596 unsigned long rate = parent_rate;
1597
1598 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1599 rate = core->ops->recalc_rate(core->hw, parent_rate);
1600 clk_pm_runtime_put(core);
1601 }
1602 return rate;
1603 }
1604
1605 /**
1606 * __clk_recalc_rates
1607 * @core: first clk in the subtree
1608 * @msg: notification type (see include/linux/clk.h)
1609 *
1610 * Walks the subtree of clks starting with clk and recalculates rates as it
1611 * goes. Note that if a clk does not implement the .recalc_rate callback then
1612 * it is assumed that the clock will take on the rate of its parent.
1613 *
1614 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1615 * if necessary.
1616 */
__clk_recalc_rates(struct clk_core * core,unsigned long msg)1617 static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1618 {
1619 unsigned long old_rate;
1620 unsigned long parent_rate = 0;
1621 struct clk_core *child;
1622
1623 lockdep_assert_held(&prepare_lock);
1624
1625 old_rate = core->rate;
1626
1627 if (core->parent)
1628 parent_rate = core->parent->rate;
1629
1630 core->rate = clk_recalc(core, parent_rate);
1631
1632 /*
1633 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1634 * & ABORT_RATE_CHANGE notifiers
1635 */
1636 if (core->notifier_count && msg)
1637 __clk_notify(core, msg, old_rate, core->rate);
1638
1639 hlist_for_each_entry(child, &core->children, child_node)
1640 __clk_recalc_rates(child, msg);
1641 }
1642
clk_core_get_rate_recalc(struct clk_core * core)1643 static unsigned long clk_core_get_rate_recalc(struct clk_core *core)
1644 {
1645 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1646 __clk_recalc_rates(core, 0);
1647
1648 return clk_core_get_rate_nolock(core);
1649 }
1650
1651 /**
1652 * clk_get_rate - return the rate of clk
1653 * @clk: the clk whose rate is being returned
1654 *
1655 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1656 * is set, which means a recalc_rate will be issued.
1657 * If clk is NULL then returns 0.
1658 */
clk_get_rate(struct clk * clk)1659 unsigned long clk_get_rate(struct clk *clk)
1660 {
1661 unsigned long rate;
1662
1663 if (!clk)
1664 return 0;
1665
1666 clk_prepare_lock();
1667 rate = clk_core_get_rate_recalc(clk->core);
1668 clk_prepare_unlock();
1669
1670 return rate;
1671 }
1672 EXPORT_SYMBOL_GPL(clk_get_rate);
1673
clk_fetch_parent_index(struct clk_core * core,struct clk_core * parent)1674 static int clk_fetch_parent_index(struct clk_core *core,
1675 struct clk_core *parent)
1676 {
1677 int i;
1678
1679 if (!parent)
1680 return -EINVAL;
1681
1682 for (i = 0; i < core->num_parents; i++) {
1683 /* Found it first try! */
1684 if (core->parents[i].core == parent)
1685 return i;
1686
1687 /* Something else is here, so keep looking */
1688 if (core->parents[i].core)
1689 continue;
1690
1691 /* Maybe core hasn't been cached but the hw is all we know? */
1692 if (core->parents[i].hw) {
1693 if (core->parents[i].hw == parent->hw)
1694 break;
1695
1696 /* Didn't match, but we're expecting a clk_hw */
1697 continue;
1698 }
1699
1700 /* Maybe it hasn't been cached (clk_set_parent() path) */
1701 if (parent == clk_core_get(core, i))
1702 break;
1703
1704 /* Fallback to comparing globally unique names */
1705 if (core->parents[i].name &&
1706 !strcmp(parent->name, core->parents[i].name))
1707 break;
1708 }
1709
1710 if (i == core->num_parents)
1711 return -EINVAL;
1712
1713 core->parents[i].core = parent;
1714 return i;
1715 }
1716
1717 /**
1718 * clk_hw_get_parent_index - return the index of the parent clock
1719 * @hw: clk_hw associated with the clk being consumed
1720 *
1721 * Fetches and returns the index of parent clock. Returns -EINVAL if the given
1722 * clock does not have a current parent.
1723 */
clk_hw_get_parent_index(struct clk_hw * hw)1724 int clk_hw_get_parent_index(struct clk_hw *hw)
1725 {
1726 struct clk_hw *parent = clk_hw_get_parent(hw);
1727
1728 if (WARN_ON(parent == NULL))
1729 return -EINVAL;
1730
1731 return clk_fetch_parent_index(hw->core, parent->core);
1732 }
1733 EXPORT_SYMBOL_GPL(clk_hw_get_parent_index);
1734
1735 /*
1736 * Update the orphan status of @core and all its children.
1737 */
clk_core_update_orphan_status(struct clk_core * core,bool is_orphan)1738 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1739 {
1740 struct clk_core *child;
1741
1742 core->orphan = is_orphan;
1743
1744 hlist_for_each_entry(child, &core->children, child_node)
1745 clk_core_update_orphan_status(child, is_orphan);
1746 }
1747
clk_reparent(struct clk_core * core,struct clk_core * new_parent)1748 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1749 {
1750 bool was_orphan = core->orphan;
1751
1752 hlist_del(&core->child_node);
1753
1754 if (new_parent) {
1755 bool becomes_orphan = new_parent->orphan;
1756
1757 /* avoid duplicate POST_RATE_CHANGE notifications */
1758 if (new_parent->new_child == core)
1759 new_parent->new_child = NULL;
1760
1761 hlist_add_head(&core->child_node, &new_parent->children);
1762
1763 if (was_orphan != becomes_orphan)
1764 clk_core_update_orphan_status(core, becomes_orphan);
1765 } else {
1766 hlist_add_head(&core->child_node, &clk_orphan_list);
1767 if (!was_orphan)
1768 clk_core_update_orphan_status(core, true);
1769 }
1770
1771 core->parent = new_parent;
1772 }
1773
__clk_set_parent_before(struct clk_core * core,struct clk_core * parent)1774 static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1775 struct clk_core *parent)
1776 {
1777 unsigned long flags;
1778 struct clk_core *old_parent = core->parent;
1779
1780 /*
1781 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1782 *
1783 * 2. Migrate prepare state between parents and prevent race with
1784 * clk_enable().
1785 *
1786 * If the clock is not prepared, then a race with
1787 * clk_enable/disable() is impossible since we already have the
1788 * prepare lock (future calls to clk_enable() need to be preceded by
1789 * a clk_prepare()).
1790 *
1791 * If the clock is prepared, migrate the prepared state to the new
1792 * parent and also protect against a race with clk_enable() by
1793 * forcing the clock and the new parent on. This ensures that all
1794 * future calls to clk_enable() are practically NOPs with respect to
1795 * hardware and software states.
1796 *
1797 * See also: Comment for clk_set_parent() below.
1798 */
1799
1800 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1801 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1802 clk_core_prepare_enable(old_parent);
1803 clk_core_prepare_enable(parent);
1804 }
1805
1806 /* migrate prepare count if > 0 */
1807 if (core->prepare_count) {
1808 clk_core_prepare_enable(parent);
1809 clk_core_enable_lock(core);
1810 }
1811
1812 /* update the clk tree topology */
1813 flags = clk_enable_lock();
1814 clk_reparent(core, parent);
1815 clk_enable_unlock(flags);
1816
1817 return old_parent;
1818 }
1819
__clk_set_parent_after(struct clk_core * core,struct clk_core * parent,struct clk_core * old_parent)1820 static void __clk_set_parent_after(struct clk_core *core,
1821 struct clk_core *parent,
1822 struct clk_core *old_parent)
1823 {
1824 /*
1825 * Finish the migration of prepare state and undo the changes done
1826 * for preventing a race with clk_enable().
1827 */
1828 if (core->prepare_count) {
1829 clk_core_disable_lock(core);
1830 clk_core_disable_unprepare(old_parent);
1831 }
1832
1833 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1834 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1835 clk_core_disable_unprepare(parent);
1836 clk_core_disable_unprepare(old_parent);
1837 }
1838 }
1839
__clk_set_parent(struct clk_core * core,struct clk_core * parent,u8 p_index)1840 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1841 u8 p_index)
1842 {
1843 unsigned long flags;
1844 int ret = 0;
1845 struct clk_core *old_parent;
1846
1847 old_parent = __clk_set_parent_before(core, parent);
1848
1849 trace_clk_set_parent(core, parent);
1850
1851 /* change clock input source */
1852 if (parent && core->ops->set_parent)
1853 ret = core->ops->set_parent(core->hw, p_index);
1854
1855 trace_clk_set_parent_complete(core, parent);
1856
1857 if (ret) {
1858 flags = clk_enable_lock();
1859 clk_reparent(core, old_parent);
1860 clk_enable_unlock(flags);
1861 __clk_set_parent_after(core, old_parent, parent);
1862
1863 return ret;
1864 }
1865
1866 __clk_set_parent_after(core, parent, old_parent);
1867
1868 return 0;
1869 }
1870
1871 /**
1872 * __clk_speculate_rates
1873 * @core: first clk in the subtree
1874 * @parent_rate: the "future" rate of clk's parent
1875 *
1876 * Walks the subtree of clks starting with clk, speculating rates as it
1877 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1878 *
1879 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1880 * pre-rate change notifications and returns early if no clks in the
1881 * subtree have subscribed to the notifications. Note that if a clk does not
1882 * implement the .recalc_rate callback then it is assumed that the clock will
1883 * take on the rate of its parent.
1884 */
__clk_speculate_rates(struct clk_core * core,unsigned long parent_rate)1885 static int __clk_speculate_rates(struct clk_core *core,
1886 unsigned long parent_rate)
1887 {
1888 struct clk_core *child;
1889 unsigned long new_rate;
1890 int ret = NOTIFY_DONE;
1891
1892 lockdep_assert_held(&prepare_lock);
1893
1894 new_rate = clk_recalc(core, parent_rate);
1895
1896 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1897 if (core->notifier_count)
1898 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1899
1900 if (ret & NOTIFY_STOP_MASK) {
1901 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1902 __func__, core->name, ret);
1903 goto out;
1904 }
1905
1906 hlist_for_each_entry(child, &core->children, child_node) {
1907 ret = __clk_speculate_rates(child, new_rate);
1908 if (ret & NOTIFY_STOP_MASK)
1909 break;
1910 }
1911
1912 out:
1913 return ret;
1914 }
1915
clk_calc_subtree(struct clk_core * core,unsigned long new_rate,struct clk_core * new_parent,u8 p_index)1916 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1917 struct clk_core *new_parent, u8 p_index)
1918 {
1919 struct clk_core *child;
1920
1921 core->new_rate = new_rate;
1922 core->new_parent = new_parent;
1923 core->new_parent_index = p_index;
1924 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1925 core->new_child = NULL;
1926 if (new_parent && new_parent != core->parent)
1927 new_parent->new_child = core;
1928
1929 hlist_for_each_entry(child, &core->children, child_node) {
1930 child->new_rate = clk_recalc(child, new_rate);
1931 clk_calc_subtree(child, child->new_rate, NULL, 0);
1932 }
1933 }
1934
1935 /*
1936 * calculate the new rates returning the topmost clock that has to be
1937 * changed.
1938 */
clk_calc_new_rates(struct clk_core * core,unsigned long rate)1939 static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1940 unsigned long rate)
1941 {
1942 struct clk_core *top = core;
1943 struct clk_core *old_parent, *parent;
1944 unsigned long best_parent_rate = 0;
1945 unsigned long new_rate;
1946 unsigned long min_rate;
1947 unsigned long max_rate;
1948 int p_index = 0;
1949 long ret;
1950
1951 /* sanity */
1952 if (IS_ERR_OR_NULL(core))
1953 return NULL;
1954
1955 /* save parent rate, if it exists */
1956 parent = old_parent = core->parent;
1957 if (parent)
1958 best_parent_rate = parent->rate;
1959
1960 clk_core_get_boundaries(core, &min_rate, &max_rate);
1961
1962 /* find the closest rate and parent clk/rate */
1963 if (clk_core_can_round(core)) {
1964 struct clk_rate_request req;
1965
1966 req.rate = rate;
1967 req.min_rate = min_rate;
1968 req.max_rate = max_rate;
1969
1970 clk_core_init_rate_req(core, &req);
1971
1972 ret = clk_core_determine_round_nolock(core, &req);
1973 if (ret < 0)
1974 return NULL;
1975
1976 best_parent_rate = req.best_parent_rate;
1977 new_rate = req.rate;
1978 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1979
1980 if (new_rate < min_rate || new_rate > max_rate)
1981 return NULL;
1982 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1983 /* pass-through clock without adjustable parent */
1984 core->new_rate = core->rate;
1985 return NULL;
1986 } else {
1987 /* pass-through clock with adjustable parent */
1988 top = clk_calc_new_rates(parent, rate);
1989 new_rate = parent->new_rate;
1990 goto out;
1991 }
1992
1993 /* some clocks must be gated to change parent */
1994 if (parent != old_parent &&
1995 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1996 pr_debug("%s: %s not gated but wants to reparent\n",
1997 __func__, core->name);
1998 return NULL;
1999 }
2000
2001 /* try finding the new parent index */
2002 if (parent && core->num_parents > 1) {
2003 p_index = clk_fetch_parent_index(core, parent);
2004 if (p_index < 0) {
2005 pr_debug("%s: clk %s can not be parent of clk %s\n",
2006 __func__, parent->name, core->name);
2007 return NULL;
2008 }
2009 }
2010
2011 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
2012 best_parent_rate != parent->rate)
2013 top = clk_calc_new_rates(parent, best_parent_rate);
2014
2015 out:
2016 clk_calc_subtree(core, new_rate, parent, p_index);
2017
2018 return top;
2019 }
2020
2021 /*
2022 * Notify about rate changes in a subtree. Always walk down the whole tree
2023 * so that in case of an error we can walk down the whole tree again and
2024 * abort the change.
2025 */
clk_propagate_rate_change(struct clk_core * core,unsigned long event)2026 static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
2027 unsigned long event)
2028 {
2029 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
2030 int ret = NOTIFY_DONE;
2031
2032 if (core->rate == core->new_rate)
2033 return NULL;
2034
2035 if (core->notifier_count) {
2036 ret = __clk_notify(core, event, core->rate, core->new_rate);
2037 if (ret & NOTIFY_STOP_MASK)
2038 fail_clk = core;
2039 }
2040
2041 hlist_for_each_entry(child, &core->children, child_node) {
2042 /* Skip children who will be reparented to another clock */
2043 if (child->new_parent && child->new_parent != core)
2044 continue;
2045 tmp_clk = clk_propagate_rate_change(child, event);
2046 if (tmp_clk)
2047 fail_clk = tmp_clk;
2048 }
2049
2050 /* handle the new child who might not be in core->children yet */
2051 if (core->new_child) {
2052 tmp_clk = clk_propagate_rate_change(core->new_child, event);
2053 if (tmp_clk)
2054 fail_clk = tmp_clk;
2055 }
2056
2057 return fail_clk;
2058 }
2059
2060 /*
2061 * walk down a subtree and set the new rates notifying the rate
2062 * change on the way
2063 */
clk_change_rate(struct clk_core * core)2064 static void clk_change_rate(struct clk_core *core)
2065 {
2066 struct clk_core *child;
2067 struct hlist_node *tmp;
2068 unsigned long old_rate;
2069 unsigned long best_parent_rate = 0;
2070 bool skip_set_rate = false;
2071 struct clk_core *old_parent;
2072 struct clk_core *parent = NULL;
2073
2074 old_rate = core->rate;
2075
2076 if (core->new_parent) {
2077 parent = core->new_parent;
2078 best_parent_rate = core->new_parent->rate;
2079 } else if (core->parent) {
2080 parent = core->parent;
2081 best_parent_rate = core->parent->rate;
2082 }
2083
2084 if (clk_pm_runtime_get(core))
2085 return;
2086
2087 if (core->flags & CLK_SET_RATE_UNGATE) {
2088 unsigned long flags;
2089
2090 clk_core_prepare(core);
2091 flags = clk_enable_lock();
2092 clk_core_enable(core);
2093 clk_enable_unlock(flags);
2094 }
2095
2096 if (core->new_parent && core->new_parent != core->parent) {
2097 old_parent = __clk_set_parent_before(core, core->new_parent);
2098 trace_clk_set_parent(core, core->new_parent);
2099
2100 if (core->ops->set_rate_and_parent) {
2101 skip_set_rate = true;
2102 core->ops->set_rate_and_parent(core->hw, core->new_rate,
2103 best_parent_rate,
2104 core->new_parent_index);
2105 } else if (core->ops->set_parent) {
2106 core->ops->set_parent(core->hw, core->new_parent_index);
2107 }
2108
2109 trace_clk_set_parent_complete(core, core->new_parent);
2110 __clk_set_parent_after(core, core->new_parent, old_parent);
2111 }
2112
2113 if (core->flags & CLK_OPS_PARENT_ENABLE)
2114 clk_core_prepare_enable(parent);
2115
2116 trace_clk_set_rate(core, core->new_rate);
2117
2118 if (!skip_set_rate && core->ops->set_rate)
2119 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
2120
2121 trace_clk_set_rate_complete(core, core->new_rate);
2122
2123 core->rate = clk_recalc(core, best_parent_rate);
2124
2125 if (core->flags & CLK_SET_RATE_UNGATE) {
2126 unsigned long flags;
2127
2128 flags = clk_enable_lock();
2129 clk_core_disable(core);
2130 clk_enable_unlock(flags);
2131 clk_core_unprepare(core);
2132 }
2133
2134 if (core->flags & CLK_OPS_PARENT_ENABLE)
2135 clk_core_disable_unprepare(parent);
2136
2137 if (core->notifier_count && old_rate != core->rate)
2138 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
2139
2140 if (core->flags & CLK_RECALC_NEW_RATES)
2141 (void)clk_calc_new_rates(core, core->new_rate);
2142
2143 /*
2144 * Use safe iteration, as change_rate can actually swap parents
2145 * for certain clock types.
2146 */
2147 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
2148 /* Skip children who will be reparented to another clock */
2149 if (child->new_parent && child->new_parent != core)
2150 continue;
2151 clk_change_rate(child);
2152 }
2153
2154 /* handle the new child who might not be in core->children yet */
2155 if (core->new_child)
2156 clk_change_rate(core->new_child);
2157
2158 clk_pm_runtime_put(core);
2159 }
2160
clk_core_req_round_rate_nolock(struct clk_core * core,unsigned long req_rate)2161 static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
2162 unsigned long req_rate)
2163 {
2164 int ret, cnt;
2165 struct clk_rate_request req;
2166
2167 lockdep_assert_held(&prepare_lock);
2168
2169 if (!core)
2170 return 0;
2171
2172 /* simulate what the rate would be if it could be freely set */
2173 cnt = clk_core_rate_nuke_protect(core);
2174 if (cnt < 0)
2175 return cnt;
2176
2177 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
2178 req.rate = req_rate;
2179
2180 ret = clk_core_round_rate_nolock(core, &req);
2181
2182 /* restore the protection */
2183 clk_core_rate_restore_protect(core, cnt);
2184
2185 return ret ? 0 : req.rate;
2186 }
2187
clk_core_set_rate_nolock(struct clk_core * core,unsigned long req_rate)2188 static int clk_core_set_rate_nolock(struct clk_core *core,
2189 unsigned long req_rate)
2190 {
2191 struct clk_core *top, *fail_clk;
2192 unsigned long rate;
2193 int ret = 0;
2194
2195 if (!core)
2196 return 0;
2197
2198 rate = clk_core_req_round_rate_nolock(core, req_rate);
2199
2200 /* bail early if nothing to do */
2201 if (rate == clk_core_get_rate_nolock(core))
2202 return 0;
2203
2204 /* fail on a direct rate set of a protected provider */
2205 if (clk_core_rate_is_protected(core))
2206 return -EBUSY;
2207
2208 /* calculate new rates and get the topmost changed clock */
2209 top = clk_calc_new_rates(core, req_rate);
2210 if (!top)
2211 return -EINVAL;
2212
2213 ret = clk_pm_runtime_get(core);
2214 if (ret)
2215 return ret;
2216
2217 /* notify that we are about to change rates */
2218 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
2219 if (fail_clk) {
2220 pr_debug("%s: failed to set %s rate\n", __func__,
2221 fail_clk->name);
2222 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
2223 ret = -EBUSY;
2224 goto err;
2225 }
2226
2227 /* change the rates */
2228 clk_change_rate(top);
2229
2230 core->req_rate = req_rate;
2231 err:
2232 clk_pm_runtime_put(core);
2233
2234 return ret;
2235 }
2236
2237 /**
2238 * clk_set_rate - specify a new rate for clk
2239 * @clk: the clk whose rate is being changed
2240 * @rate: the new rate for clk
2241 *
2242 * In the simplest case clk_set_rate will only adjust the rate of clk.
2243 *
2244 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2245 * propagate up to clk's parent; whether or not this happens depends on the
2246 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2247 * after calling .round_rate then upstream parent propagation is ignored. If
2248 * *parent_rate comes back with a new rate for clk's parent then we propagate
2249 * up to clk's parent and set its rate. Upward propagation will continue
2250 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2251 * .round_rate stops requesting changes to clk's parent_rate.
2252 *
2253 * Rate changes are accomplished via tree traversal that also recalculates the
2254 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
2255 *
2256 * Returns 0 on success, -EERROR otherwise.
2257 */
clk_set_rate(struct clk * clk,unsigned long rate)2258 int clk_set_rate(struct clk *clk, unsigned long rate)
2259 {
2260 int ret;
2261
2262 if (!clk)
2263 return 0;
2264
2265 /* prevent racing with updates to the clock topology */
2266 clk_prepare_lock();
2267
2268 if (clk->exclusive_count)
2269 clk_core_rate_unprotect(clk->core);
2270
2271 ret = clk_core_set_rate_nolock(clk->core, rate);
2272
2273 if (clk->exclusive_count)
2274 clk_core_rate_protect(clk->core);
2275
2276 clk_prepare_unlock();
2277
2278 return ret;
2279 }
2280 EXPORT_SYMBOL_GPL(clk_set_rate);
2281
2282 /**
2283 * clk_set_rate_exclusive - specify a new rate and get exclusive control
2284 * @clk: the clk whose rate is being changed
2285 * @rate: the new rate for clk
2286 *
2287 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2288 * within a critical section
2289 *
2290 * This can be used initially to ensure that at least 1 consumer is
2291 * satisfied when several consumers are competing for exclusivity over the
2292 * same clock provider.
2293 *
2294 * The exclusivity is not applied if setting the rate failed.
2295 *
2296 * Calls to clk_rate_exclusive_get() should be balanced with calls to
2297 * clk_rate_exclusive_put().
2298 *
2299 * Returns 0 on success, -EERROR otherwise.
2300 */
clk_set_rate_exclusive(struct clk * clk,unsigned long rate)2301 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2302 {
2303 int ret;
2304
2305 if (!clk)
2306 return 0;
2307
2308 /* prevent racing with updates to the clock topology */
2309 clk_prepare_lock();
2310
2311 /*
2312 * The temporary protection removal is not here, on purpose
2313 * This function is meant to be used instead of clk_rate_protect,
2314 * so before the consumer code path protect the clock provider
2315 */
2316
2317 ret = clk_core_set_rate_nolock(clk->core, rate);
2318 if (!ret) {
2319 clk_core_rate_protect(clk->core);
2320 clk->exclusive_count++;
2321 }
2322
2323 clk_prepare_unlock();
2324
2325 return ret;
2326 }
2327 EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2328
2329 /**
2330 * clk_set_rate_range - set a rate range for a clock source
2331 * @clk: clock source
2332 * @min: desired minimum clock rate in Hz, inclusive
2333 * @max: desired maximum clock rate in Hz, inclusive
2334 *
2335 * Returns success (0) or negative errno.
2336 */
clk_set_rate_range(struct clk * clk,unsigned long min,unsigned long max)2337 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2338 {
2339 int ret = 0;
2340 unsigned long old_min, old_max, rate;
2341
2342 if (!clk)
2343 return 0;
2344
2345 if (min > max) {
2346 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2347 __func__, clk->core->name, clk->dev_id, clk->con_id,
2348 min, max);
2349 return -EINVAL;
2350 }
2351
2352 clk_prepare_lock();
2353
2354 if (clk->exclusive_count)
2355 clk_core_rate_unprotect(clk->core);
2356
2357 /* Save the current values in case we need to rollback the change */
2358 old_min = clk->min_rate;
2359 old_max = clk->max_rate;
2360 clk->min_rate = min;
2361 clk->max_rate = max;
2362
2363 if (!clk_core_check_boundaries(clk->core, min, max)) {
2364 ret = -EINVAL;
2365 goto out;
2366 }
2367
2368 rate = clk_core_get_rate_nolock(clk->core);
2369 if (rate < min || rate > max) {
2370 /*
2371 * FIXME:
2372 * We are in bit of trouble here, current rate is outside the
2373 * the requested range. We are going try to request appropriate
2374 * range boundary but there is a catch. It may fail for the
2375 * usual reason (clock broken, clock protected, etc) but also
2376 * because:
2377 * - round_rate() was not favorable and fell on the wrong
2378 * side of the boundary
2379 * - the determine_rate() callback does not really check for
2380 * this corner case when determining the rate
2381 */
2382
2383 if (rate < min)
2384 rate = min;
2385 else
2386 rate = max;
2387
2388 ret = clk_core_set_rate_nolock(clk->core, rate);
2389 if (ret) {
2390 /* rollback the changes */
2391 clk->min_rate = old_min;
2392 clk->max_rate = old_max;
2393 }
2394 }
2395
2396 out:
2397 if (clk->exclusive_count)
2398 clk_core_rate_protect(clk->core);
2399
2400 clk_prepare_unlock();
2401
2402 return ret;
2403 }
2404 EXPORT_SYMBOL_GPL(clk_set_rate_range);
2405
2406 /**
2407 * clk_set_min_rate - set a minimum clock rate for a clock source
2408 * @clk: clock source
2409 * @rate: desired minimum clock rate in Hz, inclusive
2410 *
2411 * Returns success (0) or negative errno.
2412 */
clk_set_min_rate(struct clk * clk,unsigned long rate)2413 int clk_set_min_rate(struct clk *clk, unsigned long rate)
2414 {
2415 if (!clk)
2416 return 0;
2417
2418 return clk_set_rate_range(clk, rate, clk->max_rate);
2419 }
2420 EXPORT_SYMBOL_GPL(clk_set_min_rate);
2421
2422 /**
2423 * clk_set_max_rate - set a maximum clock rate for a clock source
2424 * @clk: clock source
2425 * @rate: desired maximum clock rate in Hz, inclusive
2426 *
2427 * Returns success (0) or negative errno.
2428 */
clk_set_max_rate(struct clk * clk,unsigned long rate)2429 int clk_set_max_rate(struct clk *clk, unsigned long rate)
2430 {
2431 if (!clk)
2432 return 0;
2433
2434 return clk_set_rate_range(clk, clk->min_rate, rate);
2435 }
2436 EXPORT_SYMBOL_GPL(clk_set_max_rate);
2437
2438 /**
2439 * clk_get_parent - return the parent of a clk
2440 * @clk: the clk whose parent gets returned
2441 *
2442 * Simply returns clk->parent. Returns NULL if clk is NULL.
2443 */
clk_get_parent(struct clk * clk)2444 struct clk *clk_get_parent(struct clk *clk)
2445 {
2446 struct clk *parent;
2447
2448 if (!clk)
2449 return NULL;
2450
2451 clk_prepare_lock();
2452 /* TODO: Create a per-user clk and change callers to call clk_put */
2453 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
2454 clk_prepare_unlock();
2455
2456 return parent;
2457 }
2458 EXPORT_SYMBOL_GPL(clk_get_parent);
2459
__clk_init_parent(struct clk_core * core)2460 static struct clk_core *__clk_init_parent(struct clk_core *core)
2461 {
2462 u8 index = 0;
2463
2464 if (core->num_parents > 1 && core->ops->get_parent)
2465 index = core->ops->get_parent(core->hw);
2466
2467 return clk_core_get_parent_by_index(core, index);
2468 }
2469
clk_core_reparent(struct clk_core * core,struct clk_core * new_parent)2470 static void clk_core_reparent(struct clk_core *core,
2471 struct clk_core *new_parent)
2472 {
2473 clk_reparent(core, new_parent);
2474 __clk_recalc_accuracies(core);
2475 __clk_recalc_rates(core, POST_RATE_CHANGE);
2476 }
2477
clk_hw_reparent(struct clk_hw * hw,struct clk_hw * new_parent)2478 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2479 {
2480 if (!hw)
2481 return;
2482
2483 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2484 }
2485
2486 /**
2487 * clk_has_parent - check if a clock is a possible parent for another
2488 * @clk: clock source
2489 * @parent: parent clock source
2490 *
2491 * This function can be used in drivers that need to check that a clock can be
2492 * the parent of another without actually changing the parent.
2493 *
2494 * Returns true if @parent is a possible parent for @clk, false otherwise.
2495 */
clk_has_parent(struct clk * clk,struct clk * parent)2496 bool clk_has_parent(struct clk *clk, struct clk *parent)
2497 {
2498 struct clk_core *core, *parent_core;
2499 int i;
2500
2501 /* NULL clocks should be nops, so return success if either is NULL. */
2502 if (!clk || !parent)
2503 return true;
2504
2505 core = clk->core;
2506 parent_core = parent->core;
2507
2508 /* Optimize for the case where the parent is already the parent. */
2509 if (core->parent == parent_core)
2510 return true;
2511
2512 for (i = 0; i < core->num_parents; i++)
2513 if (!strcmp(core->parents[i].name, parent_core->name))
2514 return true;
2515
2516 return false;
2517 }
2518 EXPORT_SYMBOL_GPL(clk_has_parent);
2519
clk_core_set_parent_nolock(struct clk_core * core,struct clk_core * parent)2520 static int clk_core_set_parent_nolock(struct clk_core *core,
2521 struct clk_core *parent)
2522 {
2523 int ret = 0;
2524 int p_index = 0;
2525 unsigned long p_rate = 0;
2526
2527 lockdep_assert_held(&prepare_lock);
2528
2529 if (!core)
2530 return 0;
2531
2532 if (core->parent == parent)
2533 return 0;
2534
2535 /* verify ops for multi-parent clks */
2536 if (core->num_parents > 1 && !core->ops->set_parent)
2537 return -EPERM;
2538
2539 /* check that we are allowed to re-parent if the clock is in use */
2540 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2541 return -EBUSY;
2542
2543 if (clk_core_rate_is_protected(core))
2544 return -EBUSY;
2545
2546 /* try finding the new parent index */
2547 if (parent) {
2548 p_index = clk_fetch_parent_index(core, parent);
2549 if (p_index < 0) {
2550 pr_debug("%s: clk %s can not be parent of clk %s\n",
2551 __func__, parent->name, core->name);
2552 return p_index;
2553 }
2554 p_rate = parent->rate;
2555 }
2556
2557 ret = clk_pm_runtime_get(core);
2558 if (ret)
2559 return ret;
2560
2561 /* propagate PRE_RATE_CHANGE notifications */
2562 ret = __clk_speculate_rates(core, p_rate);
2563
2564 /* abort if a driver objects */
2565 if (ret & NOTIFY_STOP_MASK)
2566 goto runtime_put;
2567
2568 /* do the re-parent */
2569 ret = __clk_set_parent(core, parent, p_index);
2570
2571 /* propagate rate an accuracy recalculation accordingly */
2572 if (ret) {
2573 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
2574 } else {
2575 __clk_recalc_rates(core, POST_RATE_CHANGE);
2576 __clk_recalc_accuracies(core);
2577 }
2578
2579 runtime_put:
2580 clk_pm_runtime_put(core);
2581
2582 return ret;
2583 }
2584
clk_hw_set_parent(struct clk_hw * hw,struct clk_hw * parent)2585 int clk_hw_set_parent(struct clk_hw *hw, struct clk_hw *parent)
2586 {
2587 return clk_core_set_parent_nolock(hw->core, parent->core);
2588 }
2589 EXPORT_SYMBOL_GPL(clk_hw_set_parent);
2590
2591 /**
2592 * clk_set_parent - switch the parent of a mux clk
2593 * @clk: the mux clk whose input we are switching
2594 * @parent: the new input to clk
2595 *
2596 * Re-parent clk to use parent as its new input source. If clk is in
2597 * prepared state, the clk will get enabled for the duration of this call. If
2598 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2599 * that, the reparenting is glitchy in hardware, etc), use the
2600 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2601 *
2602 * After successfully changing clk's parent clk_set_parent will update the
2603 * clk topology, sysfs topology and propagate rate recalculation via
2604 * __clk_recalc_rates.
2605 *
2606 * Returns 0 on success, -EERROR otherwise.
2607 */
clk_set_parent(struct clk * clk,struct clk * parent)2608 int clk_set_parent(struct clk *clk, struct clk *parent)
2609 {
2610 int ret;
2611
2612 if (!clk)
2613 return 0;
2614
2615 clk_prepare_lock();
2616
2617 if (clk->exclusive_count)
2618 clk_core_rate_unprotect(clk->core);
2619
2620 ret = clk_core_set_parent_nolock(clk->core,
2621 parent ? parent->core : NULL);
2622
2623 if (clk->exclusive_count)
2624 clk_core_rate_protect(clk->core);
2625
2626 clk_prepare_unlock();
2627
2628 return ret;
2629 }
2630 EXPORT_SYMBOL_GPL(clk_set_parent);
2631
clk_core_set_phase_nolock(struct clk_core * core,int degrees)2632 static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2633 {
2634 int ret = -EINVAL;
2635
2636 lockdep_assert_held(&prepare_lock);
2637
2638 if (!core)
2639 return 0;
2640
2641 if (clk_core_rate_is_protected(core))
2642 return -EBUSY;
2643
2644 trace_clk_set_phase(core, degrees);
2645
2646 if (core->ops->set_phase) {
2647 ret = core->ops->set_phase(core->hw, degrees);
2648 if (!ret)
2649 core->phase = degrees;
2650 }
2651
2652 trace_clk_set_phase_complete(core, degrees);
2653
2654 return ret;
2655 }
2656
2657 /**
2658 * clk_set_phase - adjust the phase shift of a clock signal
2659 * @clk: clock signal source
2660 * @degrees: number of degrees the signal is shifted
2661 *
2662 * Shifts the phase of a clock signal by the specified
2663 * degrees. Returns 0 on success, -EERROR otherwise.
2664 *
2665 * This function makes no distinction about the input or reference
2666 * signal that we adjust the clock signal phase against. For example
2667 * phase locked-loop clock signal generators we may shift phase with
2668 * respect to feedback clock signal input, but for other cases the
2669 * clock phase may be shifted with respect to some other, unspecified
2670 * signal.
2671 *
2672 * Additionally the concept of phase shift does not propagate through
2673 * the clock tree hierarchy, which sets it apart from clock rates and
2674 * clock accuracy. A parent clock phase attribute does not have an
2675 * impact on the phase attribute of a child clock.
2676 */
clk_set_phase(struct clk * clk,int degrees)2677 int clk_set_phase(struct clk *clk, int degrees)
2678 {
2679 int ret;
2680
2681 if (!clk)
2682 return 0;
2683
2684 /* sanity check degrees */
2685 degrees %= 360;
2686 if (degrees < 0)
2687 degrees += 360;
2688
2689 clk_prepare_lock();
2690
2691 if (clk->exclusive_count)
2692 clk_core_rate_unprotect(clk->core);
2693
2694 ret = clk_core_set_phase_nolock(clk->core, degrees);
2695
2696 if (clk->exclusive_count)
2697 clk_core_rate_protect(clk->core);
2698
2699 clk_prepare_unlock();
2700
2701 return ret;
2702 }
2703 EXPORT_SYMBOL_GPL(clk_set_phase);
2704
clk_core_get_phase(struct clk_core * core)2705 static int clk_core_get_phase(struct clk_core *core)
2706 {
2707 int ret;
2708
2709 lockdep_assert_held(&prepare_lock);
2710 if (!core->ops->get_phase)
2711 return 0;
2712
2713 /* Always try to update cached phase if possible */
2714 ret = core->ops->get_phase(core->hw);
2715 if (ret >= 0)
2716 core->phase = ret;
2717
2718 return ret;
2719 }
2720
2721 /**
2722 * clk_get_phase - return the phase shift of a clock signal
2723 * @clk: clock signal source
2724 *
2725 * Returns the phase shift of a clock node in degrees, otherwise returns
2726 * -EERROR.
2727 */
clk_get_phase(struct clk * clk)2728 int clk_get_phase(struct clk *clk)
2729 {
2730 int ret;
2731
2732 if (!clk)
2733 return 0;
2734
2735 clk_prepare_lock();
2736 ret = clk_core_get_phase(clk->core);
2737 clk_prepare_unlock();
2738
2739 return ret;
2740 }
2741 EXPORT_SYMBOL_GPL(clk_get_phase);
2742
clk_core_reset_duty_cycle_nolock(struct clk_core * core)2743 static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2744 {
2745 /* Assume a default value of 50% */
2746 core->duty.num = 1;
2747 core->duty.den = 2;
2748 }
2749
2750 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2751
clk_core_update_duty_cycle_nolock(struct clk_core * core)2752 static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2753 {
2754 struct clk_duty *duty = &core->duty;
2755 int ret = 0;
2756
2757 if (!core->ops->get_duty_cycle)
2758 return clk_core_update_duty_cycle_parent_nolock(core);
2759
2760 ret = core->ops->get_duty_cycle(core->hw, duty);
2761 if (ret)
2762 goto reset;
2763
2764 /* Don't trust the clock provider too much */
2765 if (duty->den == 0 || duty->num > duty->den) {
2766 ret = -EINVAL;
2767 goto reset;
2768 }
2769
2770 return 0;
2771
2772 reset:
2773 clk_core_reset_duty_cycle_nolock(core);
2774 return ret;
2775 }
2776
clk_core_update_duty_cycle_parent_nolock(struct clk_core * core)2777 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2778 {
2779 int ret = 0;
2780
2781 if (core->parent &&
2782 core->flags & CLK_DUTY_CYCLE_PARENT) {
2783 ret = clk_core_update_duty_cycle_nolock(core->parent);
2784 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2785 } else {
2786 clk_core_reset_duty_cycle_nolock(core);
2787 }
2788
2789 return ret;
2790 }
2791
2792 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2793 struct clk_duty *duty);
2794
clk_core_set_duty_cycle_nolock(struct clk_core * core,struct clk_duty * duty)2795 static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2796 struct clk_duty *duty)
2797 {
2798 int ret;
2799
2800 lockdep_assert_held(&prepare_lock);
2801
2802 if (clk_core_rate_is_protected(core))
2803 return -EBUSY;
2804
2805 trace_clk_set_duty_cycle(core, duty);
2806
2807 if (!core->ops->set_duty_cycle)
2808 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2809
2810 ret = core->ops->set_duty_cycle(core->hw, duty);
2811 if (!ret)
2812 memcpy(&core->duty, duty, sizeof(*duty));
2813
2814 trace_clk_set_duty_cycle_complete(core, duty);
2815
2816 return ret;
2817 }
2818
clk_core_set_duty_cycle_parent_nolock(struct clk_core * core,struct clk_duty * duty)2819 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2820 struct clk_duty *duty)
2821 {
2822 int ret = 0;
2823
2824 if (core->parent &&
2825 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2826 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2827 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2828 }
2829
2830 return ret;
2831 }
2832
2833 /**
2834 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2835 * @clk: clock signal source
2836 * @num: numerator of the duty cycle ratio to be applied
2837 * @den: denominator of the duty cycle ratio to be applied
2838 *
2839 * Apply the duty cycle ratio if the ratio is valid and the clock can
2840 * perform this operation
2841 *
2842 * Returns (0) on success, a negative errno otherwise.
2843 */
clk_set_duty_cycle(struct clk * clk,unsigned int num,unsigned int den)2844 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2845 {
2846 int ret;
2847 struct clk_duty duty;
2848
2849 if (!clk)
2850 return 0;
2851
2852 /* sanity check the ratio */
2853 if (den == 0 || num > den)
2854 return -EINVAL;
2855
2856 duty.num = num;
2857 duty.den = den;
2858
2859 clk_prepare_lock();
2860
2861 if (clk->exclusive_count)
2862 clk_core_rate_unprotect(clk->core);
2863
2864 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2865
2866 if (clk->exclusive_count)
2867 clk_core_rate_protect(clk->core);
2868
2869 clk_prepare_unlock();
2870
2871 return ret;
2872 }
2873 EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2874
clk_core_get_scaled_duty_cycle(struct clk_core * core,unsigned int scale)2875 static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2876 unsigned int scale)
2877 {
2878 struct clk_duty *duty = &core->duty;
2879 int ret;
2880
2881 clk_prepare_lock();
2882
2883 ret = clk_core_update_duty_cycle_nolock(core);
2884 if (!ret)
2885 ret = mult_frac(scale, duty->num, duty->den);
2886
2887 clk_prepare_unlock();
2888
2889 return ret;
2890 }
2891
2892 /**
2893 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2894 * @clk: clock signal source
2895 * @scale: scaling factor to be applied to represent the ratio as an integer
2896 *
2897 * Returns the duty cycle ratio of a clock node multiplied by the provided
2898 * scaling factor, or negative errno on error.
2899 */
clk_get_scaled_duty_cycle(struct clk * clk,unsigned int scale)2900 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2901 {
2902 if (!clk)
2903 return 0;
2904
2905 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2906 }
2907 EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2908
2909 /**
2910 * clk_is_match - check if two clk's point to the same hardware clock
2911 * @p: clk compared against q
2912 * @q: clk compared against p
2913 *
2914 * Returns true if the two struct clk pointers both point to the same hardware
2915 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2916 * share the same struct clk_core object.
2917 *
2918 * Returns false otherwise. Note that two NULL clks are treated as matching.
2919 */
clk_is_match(const struct clk * p,const struct clk * q)2920 bool clk_is_match(const struct clk *p, const struct clk *q)
2921 {
2922 /* trivial case: identical struct clk's or both NULL */
2923 if (p == q)
2924 return true;
2925
2926 /* true if clk->core pointers match. Avoid dereferencing garbage */
2927 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2928 if (p->core == q->core)
2929 return true;
2930
2931 return false;
2932 }
2933 EXPORT_SYMBOL_GPL(clk_is_match);
2934
2935 /*** debugfs support ***/
2936
2937 #ifdef CONFIG_DEBUG_FS
2938 #include <linux/debugfs.h>
2939
2940 static struct dentry *rootdir;
2941 static int inited = 0;
2942 static DEFINE_MUTEX(clk_debug_lock);
2943 static HLIST_HEAD(clk_debug_list);
2944
2945 static struct hlist_head *orphan_list[] = {
2946 &clk_orphan_list,
2947 NULL,
2948 };
2949
clk_summary_show_one(struct seq_file * s,struct clk_core * c,int level)2950 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2951 int level)
2952 {
2953 int phase;
2954
2955 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu ",
2956 level * 3 + 1, "",
2957 30 - level * 3, c->name,
2958 c->enable_count, c->prepare_count, c->protect_count,
2959 clk_core_get_rate_recalc(c),
2960 clk_core_get_accuracy_recalc(c));
2961
2962 phase = clk_core_get_phase(c);
2963 if (phase >= 0)
2964 seq_printf(s, "%5d", phase);
2965 else
2966 seq_puts(s, "-----");
2967
2968 seq_printf(s, " %6d\n", clk_core_get_scaled_duty_cycle(c, 100000));
2969 }
2970
clk_summary_show_subtree(struct seq_file * s,struct clk_core * c,int level)2971 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2972 int level)
2973 {
2974 struct clk_core *child;
2975
2976 clk_summary_show_one(s, c, level);
2977
2978 hlist_for_each_entry(child, &c->children, child_node)
2979 clk_summary_show_subtree(s, child, level + 1);
2980 }
2981
clk_summary_show(struct seq_file * s,void * data)2982 static int clk_summary_show(struct seq_file *s, void *data)
2983 {
2984 struct clk_core *c;
2985 struct hlist_head **lists = (struct hlist_head **)s->private;
2986
2987 seq_puts(s, " enable prepare protect duty\n");
2988 seq_puts(s, " clock count count count rate accuracy phase cycle\n");
2989 seq_puts(s, "---------------------------------------------------------------------------------------------\n");
2990
2991 clk_prepare_lock();
2992
2993 for (; *lists; lists++)
2994 hlist_for_each_entry(c, *lists, child_node)
2995 clk_summary_show_subtree(s, c, 0);
2996
2997 clk_prepare_unlock();
2998
2999 return 0;
3000 }
3001 DEFINE_SHOW_ATTRIBUTE(clk_summary);
3002
clk_dump_one(struct seq_file * s,struct clk_core * c,int level)3003 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
3004 {
3005 int phase;
3006 unsigned long min_rate, max_rate;
3007
3008 clk_core_get_boundaries(c, &min_rate, &max_rate);
3009
3010 /* This should be JSON format, i.e. elements separated with a comma */
3011 seq_printf(s, "\"%s\": { ", c->name);
3012 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
3013 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
3014 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
3015 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate_recalc(c));
3016 seq_printf(s, "\"min_rate\": %lu,", min_rate);
3017 seq_printf(s, "\"max_rate\": %lu,", max_rate);
3018 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy_recalc(c));
3019 phase = clk_core_get_phase(c);
3020 if (phase >= 0)
3021 seq_printf(s, "\"phase\": %d,", phase);
3022 seq_printf(s, "\"duty_cycle\": %u",
3023 clk_core_get_scaled_duty_cycle(c, 100000));
3024 }
3025
clk_dump_subtree(struct seq_file * s,struct clk_core * c,int level)3026 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
3027 {
3028 struct clk_core *child;
3029
3030 clk_dump_one(s, c, level);
3031
3032 hlist_for_each_entry(child, &c->children, child_node) {
3033 seq_putc(s, ',');
3034 clk_dump_subtree(s, child, level + 1);
3035 }
3036
3037 seq_putc(s, '}');
3038 }
3039
clk_dump_show(struct seq_file * s,void * data)3040 static int clk_dump_show(struct seq_file *s, void *data)
3041 {
3042 struct clk_core *c;
3043 bool first_node = true;
3044 struct hlist_head **lists = (struct hlist_head **)s->private;
3045
3046 seq_putc(s, '{');
3047 clk_prepare_lock();
3048
3049 for (; *lists; lists++) {
3050 hlist_for_each_entry(c, *lists, child_node) {
3051 if (!first_node)
3052 seq_putc(s, ',');
3053 first_node = false;
3054 clk_dump_subtree(s, c, 0);
3055 }
3056 }
3057
3058 clk_prepare_unlock();
3059
3060 seq_puts(s, "}\n");
3061 return 0;
3062 }
3063 DEFINE_SHOW_ATTRIBUTE(clk_dump);
3064
3065 #undef CLOCK_ALLOW_WRITE_DEBUGFS
3066 #ifdef CLOCK_ALLOW_WRITE_DEBUGFS
3067 /*
3068 * This can be dangerous, therefore don't provide any real compile time
3069 * configuration option for this feature.
3070 * People who want to use this will need to modify the source code directly.
3071 */
clk_rate_set(void * data,u64 val)3072 static int clk_rate_set(void *data, u64 val)
3073 {
3074 struct clk_core *core = data;
3075 int ret;
3076
3077 clk_prepare_lock();
3078 ret = clk_core_set_rate_nolock(core, val);
3079 clk_prepare_unlock();
3080
3081 return ret;
3082 }
3083
3084 #define clk_rate_mode 0644
3085
clk_prepare_enable_set(void * data,u64 val)3086 static int clk_prepare_enable_set(void *data, u64 val)
3087 {
3088 struct clk_core *core = data;
3089 int ret = 0;
3090
3091 if (val)
3092 ret = clk_prepare_enable(core->hw->clk);
3093 else
3094 clk_disable_unprepare(core->hw->clk);
3095
3096 return ret;
3097 }
3098
clk_prepare_enable_get(void * data,u64 * val)3099 static int clk_prepare_enable_get(void *data, u64 *val)
3100 {
3101 struct clk_core *core = data;
3102
3103 *val = core->enable_count && core->prepare_count;
3104 return 0;
3105 }
3106
3107 DEFINE_DEBUGFS_ATTRIBUTE(clk_prepare_enable_fops, clk_prepare_enable_get,
3108 clk_prepare_enable_set, "%llu\n");
3109
3110 #else
3111 #define clk_rate_set NULL
3112 #define clk_rate_mode 0444
3113 #endif
3114
clk_rate_get(void * data,u64 * val)3115 static int clk_rate_get(void *data, u64 *val)
3116 {
3117 struct clk_core *core = data;
3118
3119 *val = core->rate;
3120 return 0;
3121 }
3122
3123 DEFINE_DEBUGFS_ATTRIBUTE(clk_rate_fops, clk_rate_get, clk_rate_set, "%llu\n");
3124
3125 static const struct {
3126 unsigned long flag;
3127 const char *name;
3128 } clk_flags[] = {
3129 #define ENTRY(f) { f, #f }
3130 ENTRY(CLK_SET_RATE_GATE),
3131 ENTRY(CLK_SET_PARENT_GATE),
3132 ENTRY(CLK_SET_RATE_PARENT),
3133 ENTRY(CLK_IGNORE_UNUSED),
3134 ENTRY(CLK_GET_RATE_NOCACHE),
3135 ENTRY(CLK_SET_RATE_NO_REPARENT),
3136 ENTRY(CLK_GET_ACCURACY_NOCACHE),
3137 ENTRY(CLK_RECALC_NEW_RATES),
3138 ENTRY(CLK_SET_RATE_UNGATE),
3139 ENTRY(CLK_IS_CRITICAL),
3140 ENTRY(CLK_OPS_PARENT_ENABLE),
3141 ENTRY(CLK_DUTY_CYCLE_PARENT),
3142 #undef ENTRY
3143 };
3144
clk_flags_show(struct seq_file * s,void * data)3145 static int clk_flags_show(struct seq_file *s, void *data)
3146 {
3147 struct clk_core *core = s->private;
3148 unsigned long flags = core->flags;
3149 unsigned int i;
3150
3151 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
3152 if (flags & clk_flags[i].flag) {
3153 seq_printf(s, "%s\n", clk_flags[i].name);
3154 flags &= ~clk_flags[i].flag;
3155 }
3156 }
3157 if (flags) {
3158 /* Unknown flags */
3159 seq_printf(s, "0x%lx\n", flags);
3160 }
3161
3162 return 0;
3163 }
3164 DEFINE_SHOW_ATTRIBUTE(clk_flags);
3165
possible_parent_show(struct seq_file * s,struct clk_core * core,unsigned int i,char terminator)3166 static void possible_parent_show(struct seq_file *s, struct clk_core *core,
3167 unsigned int i, char terminator)
3168 {
3169 struct clk_core *parent;
3170
3171 /*
3172 * Go through the following options to fetch a parent's name.
3173 *
3174 * 1. Fetch the registered parent clock and use its name
3175 * 2. Use the global (fallback) name if specified
3176 * 3. Use the local fw_name if provided
3177 * 4. Fetch parent clock's clock-output-name if DT index was set
3178 *
3179 * This may still fail in some cases, such as when the parent is
3180 * specified directly via a struct clk_hw pointer, but it isn't
3181 * registered (yet).
3182 */
3183 parent = clk_core_get_parent_by_index(core, i);
3184 if (parent)
3185 seq_puts(s, parent->name);
3186 else if (core->parents[i].name)
3187 seq_puts(s, core->parents[i].name);
3188 else if (core->parents[i].fw_name)
3189 seq_printf(s, "<%s>(fw)", core->parents[i].fw_name);
3190 else if (core->parents[i].index >= 0)
3191 seq_puts(s,
3192 of_clk_get_parent_name(core->of_node,
3193 core->parents[i].index));
3194 else
3195 seq_puts(s, "(missing)");
3196
3197 seq_putc(s, terminator);
3198 }
3199
possible_parents_show(struct seq_file * s,void * data)3200 static int possible_parents_show(struct seq_file *s, void *data)
3201 {
3202 struct clk_core *core = s->private;
3203 int i;
3204
3205 for (i = 0; i < core->num_parents - 1; i++)
3206 possible_parent_show(s, core, i, ' ');
3207
3208 possible_parent_show(s, core, i, '\n');
3209
3210 return 0;
3211 }
3212 DEFINE_SHOW_ATTRIBUTE(possible_parents);
3213
current_parent_show(struct seq_file * s,void * data)3214 static int current_parent_show(struct seq_file *s, void *data)
3215 {
3216 struct clk_core *core = s->private;
3217
3218 if (core->parent)
3219 seq_printf(s, "%s\n", core->parent->name);
3220
3221 return 0;
3222 }
3223 DEFINE_SHOW_ATTRIBUTE(current_parent);
3224
clk_duty_cycle_show(struct seq_file * s,void * data)3225 static int clk_duty_cycle_show(struct seq_file *s, void *data)
3226 {
3227 struct clk_core *core = s->private;
3228 struct clk_duty *duty = &core->duty;
3229
3230 seq_printf(s, "%u/%u\n", duty->num, duty->den);
3231
3232 return 0;
3233 }
3234 DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
3235
clk_min_rate_show(struct seq_file * s,void * data)3236 static int clk_min_rate_show(struct seq_file *s, void *data)
3237 {
3238 struct clk_core *core = s->private;
3239 unsigned long min_rate, max_rate;
3240
3241 clk_prepare_lock();
3242 clk_core_get_boundaries(core, &min_rate, &max_rate);
3243 clk_prepare_unlock();
3244 seq_printf(s, "%lu\n", min_rate);
3245
3246 return 0;
3247 }
3248 DEFINE_SHOW_ATTRIBUTE(clk_min_rate);
3249
clk_max_rate_show(struct seq_file * s,void * data)3250 static int clk_max_rate_show(struct seq_file *s, void *data)
3251 {
3252 struct clk_core *core = s->private;
3253 unsigned long min_rate, max_rate;
3254
3255 clk_prepare_lock();
3256 clk_core_get_boundaries(core, &min_rate, &max_rate);
3257 clk_prepare_unlock();
3258 seq_printf(s, "%lu\n", max_rate);
3259
3260 return 0;
3261 }
3262 DEFINE_SHOW_ATTRIBUTE(clk_max_rate);
3263
clk_debug_create_one(struct clk_core * core,struct dentry * pdentry)3264 static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
3265 {
3266 struct dentry *root;
3267
3268 if (!core || !pdentry)
3269 return;
3270
3271 root = debugfs_create_dir(core->name, pdentry);
3272 core->dentry = root;
3273
3274 debugfs_create_file("clk_rate", clk_rate_mode, root, core,
3275 &clk_rate_fops);
3276 debugfs_create_file("clk_min_rate", 0444, root, core, &clk_min_rate_fops);
3277 debugfs_create_file("clk_max_rate", 0444, root, core, &clk_max_rate_fops);
3278 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
3279 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
3280 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
3281 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
3282 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
3283 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
3284 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
3285 debugfs_create_file("clk_duty_cycle", 0444, root, core,
3286 &clk_duty_cycle_fops);
3287 #ifdef CLOCK_ALLOW_WRITE_DEBUGFS
3288 debugfs_create_file("clk_prepare_enable", 0644, root, core,
3289 &clk_prepare_enable_fops);
3290 #endif
3291
3292 if (core->num_parents > 0)
3293 debugfs_create_file("clk_parent", 0444, root, core,
3294 ¤t_parent_fops);
3295
3296 if (core->num_parents > 1)
3297 debugfs_create_file("clk_possible_parents", 0444, root, core,
3298 &possible_parents_fops);
3299
3300 if (core->ops->debug_init)
3301 core->ops->debug_init(core->hw, core->dentry);
3302 }
3303
3304 /**
3305 * clk_debug_register - add a clk node to the debugfs clk directory
3306 * @core: the clk being added to the debugfs clk directory
3307 *
3308 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3309 * initialized. Otherwise it bails out early since the debugfs clk directory
3310 * will be created lazily by clk_debug_init as part of a late_initcall.
3311 */
clk_debug_register(struct clk_core * core)3312 static void clk_debug_register(struct clk_core *core)
3313 {
3314 mutex_lock(&clk_debug_lock);
3315 hlist_add_head(&core->debug_node, &clk_debug_list);
3316 if (inited)
3317 clk_debug_create_one(core, rootdir);
3318 mutex_unlock(&clk_debug_lock);
3319 }
3320
3321 /**
3322 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3323 * @core: the clk being removed from the debugfs clk directory
3324 *
3325 * Dynamically removes a clk and all its child nodes from the
3326 * debugfs clk directory if clk->dentry points to debugfs created by
3327 * clk_debug_register in __clk_core_init.
3328 */
clk_debug_unregister(struct clk_core * core)3329 static void clk_debug_unregister(struct clk_core *core)
3330 {
3331 mutex_lock(&clk_debug_lock);
3332 hlist_del_init(&core->debug_node);
3333 debugfs_remove_recursive(core->dentry);
3334 core->dentry = NULL;
3335 mutex_unlock(&clk_debug_lock);
3336 }
3337
3338 /**
3339 * clk_debug_init - lazily populate the debugfs clk directory
3340 *
3341 * clks are often initialized very early during boot before memory can be
3342 * dynamically allocated and well before debugfs is setup. This function
3343 * populates the debugfs clk directory once at boot-time when we know that
3344 * debugfs is setup. It should only be called once at boot-time, all other clks
3345 * added dynamically will be done so with clk_debug_register.
3346 */
clk_debug_init(void)3347 static int __init clk_debug_init(void)
3348 {
3349 struct clk_core *core;
3350
3351 #ifdef CLOCK_ALLOW_WRITE_DEBUGFS
3352 pr_warn("\n");
3353 pr_warn("********************************************************************\n");
3354 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3355 pr_warn("** **\n");
3356 pr_warn("** WRITEABLE clk DebugFS SUPPORT HAS BEEN ENABLED IN THIS KERNEL **\n");
3357 pr_warn("** **\n");
3358 pr_warn("** This means that this kernel is built to expose clk operations **\n");
3359 pr_warn("** such as parent or rate setting, enabling, disabling, etc. **\n");
3360 pr_warn("** to userspace, which may compromise security on your system. **\n");
3361 pr_warn("** **\n");
3362 pr_warn("** If you see this message and you are not debugging the **\n");
3363 pr_warn("** kernel, report this immediately to your vendor! **\n");
3364 pr_warn("** **\n");
3365 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3366 pr_warn("********************************************************************\n");
3367 #endif
3368
3369 rootdir = debugfs_create_dir("clk", NULL);
3370
3371 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
3372 &clk_summary_fops);
3373 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
3374 &clk_dump_fops);
3375 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
3376 &clk_summary_fops);
3377 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
3378 &clk_dump_fops);
3379
3380 mutex_lock(&clk_debug_lock);
3381 hlist_for_each_entry(core, &clk_debug_list, debug_node)
3382 clk_debug_create_one(core, rootdir);
3383
3384 inited = 1;
3385 mutex_unlock(&clk_debug_lock);
3386
3387 return 0;
3388 }
3389 late_initcall(clk_debug_init);
3390 #else
clk_debug_register(struct clk_core * core)3391 static inline void clk_debug_register(struct clk_core *core) { }
clk_debug_unregister(struct clk_core * core)3392 static inline void clk_debug_unregister(struct clk_core *core)
3393 {
3394 }
3395 #endif
3396
clk_core_reparent_orphans_nolock(void)3397 static void clk_core_reparent_orphans_nolock(void)
3398 {
3399 struct clk_core *orphan;
3400 struct hlist_node *tmp2;
3401
3402 /*
3403 * walk the list of orphan clocks and reparent any that newly finds a
3404 * parent.
3405 */
3406 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
3407 struct clk_core *parent = __clk_init_parent(orphan);
3408
3409 /*
3410 * We need to use __clk_set_parent_before() and _after() to
3411 * to properly migrate any prepare/enable count of the orphan
3412 * clock. This is important for CLK_IS_CRITICAL clocks, which
3413 * are enabled during init but might not have a parent yet.
3414 */
3415 if (parent) {
3416 /* update the clk tree topology */
3417 __clk_set_parent_before(orphan, parent);
3418 __clk_set_parent_after(orphan, parent, NULL);
3419 __clk_recalc_accuracies(orphan);
3420 __clk_recalc_rates(orphan, 0);
3421
3422 /*
3423 * __clk_init_parent() will set the initial req_rate to
3424 * 0 if the clock doesn't have clk_ops::recalc_rate and
3425 * is an orphan when it's registered.
3426 *
3427 * 'req_rate' is used by clk_set_rate_range() and
3428 * clk_put() to trigger a clk_set_rate() call whenever
3429 * the boundaries are modified. Let's make sure
3430 * 'req_rate' is set to something non-zero so that
3431 * clk_set_rate_range() doesn't drop the frequency.
3432 */
3433 orphan->req_rate = orphan->rate;
3434 }
3435 }
3436 }
3437
3438 /**
3439 * __clk_core_init - initialize the data structures in a struct clk_core
3440 * @core: clk_core being initialized
3441 *
3442 * Initializes the lists in struct clk_core, queries the hardware for the
3443 * parent and rate and sets them both.
3444 */
__clk_core_init(struct clk_core * core)3445 static int __clk_core_init(struct clk_core *core)
3446 {
3447 int ret;
3448 struct clk_core *parent;
3449 unsigned long rate;
3450 int phase;
3451
3452 if (!core)
3453 return -EINVAL;
3454
3455 clk_prepare_lock();
3456
3457 /*
3458 * Set hw->core after grabbing the prepare_lock to synchronize with
3459 * callers of clk_core_fill_parent_index() where we treat hw->core
3460 * being NULL as the clk not being registered yet. This is crucial so
3461 * that clks aren't parented until their parent is fully registered.
3462 */
3463 core->hw->core = core;
3464
3465 ret = clk_pm_runtime_get(core);
3466 if (ret)
3467 goto unlock;
3468
3469 /* check to see if a clock with this name is already registered */
3470 if (clk_core_lookup(core->name)) {
3471 pr_debug("%s: clk %s already initialized\n",
3472 __func__, core->name);
3473 ret = -EEXIST;
3474 goto out;
3475 }
3476
3477 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
3478 if (core->ops->set_rate &&
3479 !((core->ops->round_rate || core->ops->determine_rate) &&
3480 core->ops->recalc_rate)) {
3481 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3482 __func__, core->name);
3483 ret = -EINVAL;
3484 goto out;
3485 }
3486
3487 if (core->ops->set_parent && !core->ops->get_parent) {
3488 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3489 __func__, core->name);
3490 ret = -EINVAL;
3491 goto out;
3492 }
3493
3494 if (core->num_parents > 1 && !core->ops->get_parent) {
3495 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3496 __func__, core->name);
3497 ret = -EINVAL;
3498 goto out;
3499 }
3500
3501 if (core->ops->set_rate_and_parent &&
3502 !(core->ops->set_parent && core->ops->set_rate)) {
3503 pr_err("%s: %s must implement .set_parent & .set_rate\n",
3504 __func__, core->name);
3505 ret = -EINVAL;
3506 goto out;
3507 }
3508
3509 /*
3510 * optional platform-specific magic
3511 *
3512 * The .init callback is not used by any of the basic clock types, but
3513 * exists for weird hardware that must perform initialization magic for
3514 * CCF to get an accurate view of clock for any other callbacks. It may
3515 * also be used needs to perform dynamic allocations. Such allocation
3516 * must be freed in the terminate() callback.
3517 * This callback shall not be used to initialize the parameters state,
3518 * such as rate, parent, etc ...
3519 *
3520 * If it exist, this callback should called before any other callback of
3521 * the clock
3522 */
3523 if (core->ops->init) {
3524 ret = core->ops->init(core->hw);
3525 if (ret)
3526 goto out;
3527 }
3528
3529 parent = core->parent = __clk_init_parent(core);
3530
3531 /*
3532 * Populate core->parent if parent has already been clk_core_init'd. If
3533 * parent has not yet been clk_core_init'd then place clk in the orphan
3534 * list. If clk doesn't have any parents then place it in the root
3535 * clk list.
3536 *
3537 * Every time a new clk is clk_init'd then we walk the list of orphan
3538 * clocks and re-parent any that are children of the clock currently
3539 * being clk_init'd.
3540 */
3541 if (parent) {
3542 hlist_add_head(&core->child_node, &parent->children);
3543 core->orphan = parent->orphan;
3544 } else if (!core->num_parents) {
3545 hlist_add_head(&core->child_node, &clk_root_list);
3546 core->orphan = false;
3547 } else {
3548 hlist_add_head(&core->child_node, &clk_orphan_list);
3549 core->orphan = true;
3550 }
3551
3552 /*
3553 * Set clk's accuracy. The preferred method is to use
3554 * .recalc_accuracy. For simple clocks and lazy developers the default
3555 * fallback is to use the parent's accuracy. If a clock doesn't have a
3556 * parent (or is orphaned) then accuracy is set to zero (perfect
3557 * clock).
3558 */
3559 if (core->ops->recalc_accuracy)
3560 core->accuracy = core->ops->recalc_accuracy(core->hw,
3561 clk_core_get_accuracy_no_lock(parent));
3562 else if (parent)
3563 core->accuracy = parent->accuracy;
3564 else
3565 core->accuracy = 0;
3566
3567 /*
3568 * Set clk's phase by clk_core_get_phase() caching the phase.
3569 * Since a phase is by definition relative to its parent, just
3570 * query the current clock phase, or just assume it's in phase.
3571 */
3572 phase = clk_core_get_phase(core);
3573 if (phase < 0) {
3574 ret = phase;
3575 pr_warn("%s: Failed to get phase for clk '%s'\n", __func__,
3576 core->name);
3577 goto out;
3578 }
3579
3580 /*
3581 * Set clk's duty cycle.
3582 */
3583 clk_core_update_duty_cycle_nolock(core);
3584
3585 /*
3586 * Set clk's rate. The preferred method is to use .recalc_rate. For
3587 * simple clocks and lazy developers the default fallback is to use the
3588 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3589 * then rate is set to zero.
3590 */
3591 if (core->ops->recalc_rate)
3592 rate = core->ops->recalc_rate(core->hw,
3593 clk_core_get_rate_nolock(parent));
3594 else if (parent)
3595 rate = parent->rate;
3596 else
3597 rate = 0;
3598 core->rate = core->req_rate = rate;
3599
3600 /*
3601 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3602 * don't get accidentally disabled when walking the orphan tree and
3603 * reparenting clocks
3604 */
3605 if (core->flags & CLK_IS_CRITICAL) {
3606 unsigned long flags;
3607
3608 ret = clk_core_prepare(core);
3609 if (ret) {
3610 pr_warn("%s: critical clk '%s' failed to prepare\n",
3611 __func__, core->name);
3612 goto out;
3613 }
3614
3615 flags = clk_enable_lock();
3616 ret = clk_core_enable(core);
3617 clk_enable_unlock(flags);
3618 if (ret) {
3619 pr_warn("%s: critical clk '%s' failed to enable\n",
3620 __func__, core->name);
3621 clk_core_unprepare(core);
3622 goto out;
3623 }
3624 }
3625
3626 clk_core_reparent_orphans_nolock();
3627
3628
3629 kref_init(&core->ref);
3630 out:
3631 clk_pm_runtime_put(core);
3632 unlock:
3633 if (ret) {
3634 hlist_del_init(&core->child_node);
3635 core->hw->core = NULL;
3636 }
3637
3638 clk_prepare_unlock();
3639
3640 if (!ret)
3641 clk_debug_register(core);
3642
3643 return ret;
3644 }
3645
3646 /**
3647 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3648 * @core: clk to add consumer to
3649 * @clk: consumer to link to a clk
3650 */
clk_core_link_consumer(struct clk_core * core,struct clk * clk)3651 static void clk_core_link_consumer(struct clk_core *core, struct clk *clk)
3652 {
3653 clk_prepare_lock();
3654 hlist_add_head(&clk->clks_node, &core->clks);
3655 clk_prepare_unlock();
3656 }
3657
3658 /**
3659 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3660 * @clk: consumer to unlink
3661 */
clk_core_unlink_consumer(struct clk * clk)3662 static void clk_core_unlink_consumer(struct clk *clk)
3663 {
3664 lockdep_assert_held(&prepare_lock);
3665 hlist_del(&clk->clks_node);
3666 }
3667
3668 /**
3669 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
3670 * @core: clk to allocate a consumer for
3671 * @dev_id: string describing device name
3672 * @con_id: connection ID string on device
3673 *
3674 * Returns: clk consumer left unlinked from the consumer list
3675 */
alloc_clk(struct clk_core * core,const char * dev_id,const char * con_id)3676 static struct clk *alloc_clk(struct clk_core *core, const char *dev_id,
3677 const char *con_id)
3678 {
3679 struct clk *clk;
3680
3681 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3682 if (!clk)
3683 return ERR_PTR(-ENOMEM);
3684
3685 clk->core = core;
3686 clk->dev_id = dev_id;
3687 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
3688 clk->max_rate = ULONG_MAX;
3689
3690 return clk;
3691 }
3692
3693 /**
3694 * free_clk - Free a clk consumer
3695 * @clk: clk consumer to free
3696 *
3697 * Note, this assumes the clk has been unlinked from the clk_core consumer
3698 * list.
3699 */
free_clk(struct clk * clk)3700 static void free_clk(struct clk *clk)
3701 {
3702 kfree_const(clk->con_id);
3703 kfree(clk);
3704 }
3705
3706 /**
3707 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
3708 * a clk_hw
3709 * @dev: clk consumer device
3710 * @hw: clk_hw associated with the clk being consumed
3711 * @dev_id: string describing device name
3712 * @con_id: connection ID string on device
3713 *
3714 * This is the main function used to create a clk pointer for use by clk
3715 * consumers. It connects a consumer to the clk_core and clk_hw structures
3716 * used by the framework and clk provider respectively.
3717 */
clk_hw_create_clk(struct device * dev,struct clk_hw * hw,const char * dev_id,const char * con_id)3718 struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
3719 const char *dev_id, const char *con_id)
3720 {
3721 struct clk *clk;
3722 struct clk_core *core;
3723
3724 /* This is to allow this function to be chained to others */
3725 if (IS_ERR_OR_NULL(hw))
3726 return ERR_CAST(hw);
3727
3728 core = hw->core;
3729 clk = alloc_clk(core, dev_id, con_id);
3730 if (IS_ERR(clk))
3731 return clk;
3732 clk->dev = dev;
3733
3734 if (!try_module_get(core->owner)) {
3735 free_clk(clk);
3736 return ERR_PTR(-ENOENT);
3737 }
3738
3739 kref_get(&core->ref);
3740 clk_core_link_consumer(core, clk);
3741
3742 return clk;
3743 }
3744
clk_cpy_name(const char ** dst_p,const char * src,bool must_exist)3745 static int clk_cpy_name(const char **dst_p, const char *src, bool must_exist)
3746 {
3747 const char *dst;
3748
3749 if (!src) {
3750 if (must_exist)
3751 return -EINVAL;
3752 return 0;
3753 }
3754
3755 *dst_p = dst = kstrdup_const(src, GFP_KERNEL);
3756 if (!dst)
3757 return -ENOMEM;
3758
3759 return 0;
3760 }
3761
clk_core_populate_parent_map(struct clk_core * core,const struct clk_init_data * init)3762 static int clk_core_populate_parent_map(struct clk_core *core,
3763 const struct clk_init_data *init)
3764 {
3765 u8 num_parents = init->num_parents;
3766 const char * const *parent_names = init->parent_names;
3767 const struct clk_hw **parent_hws = init->parent_hws;
3768 const struct clk_parent_data *parent_data = init->parent_data;
3769 int i, ret = 0;
3770 struct clk_parent_map *parents, *parent;
3771
3772 if (!num_parents)
3773 return 0;
3774
3775 /*
3776 * Avoid unnecessary string look-ups of clk_core's possible parents by
3777 * having a cache of names/clk_hw pointers to clk_core pointers.
3778 */
3779 parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
3780 core->parents = parents;
3781 if (!parents)
3782 return -ENOMEM;
3783
3784 /* Copy everything over because it might be __initdata */
3785 for (i = 0, parent = parents; i < num_parents; i++, parent++) {
3786 parent->index = -1;
3787 if (parent_names) {
3788 /* throw a WARN if any entries are NULL */
3789 WARN(!parent_names[i],
3790 "%s: invalid NULL in %s's .parent_names\n",
3791 __func__, core->name);
3792 ret = clk_cpy_name(&parent->name, parent_names[i],
3793 true);
3794 } else if (parent_data) {
3795 parent->hw = parent_data[i].hw;
3796 parent->index = parent_data[i].index;
3797 ret = clk_cpy_name(&parent->fw_name,
3798 parent_data[i].fw_name, false);
3799 if (!ret)
3800 ret = clk_cpy_name(&parent->name,
3801 parent_data[i].name,
3802 false);
3803 } else if (parent_hws) {
3804 parent->hw = parent_hws[i];
3805 } else {
3806 ret = -EINVAL;
3807 WARN(1, "Must specify parents if num_parents > 0\n");
3808 }
3809
3810 if (ret) {
3811 do {
3812 kfree_const(parents[i].name);
3813 kfree_const(parents[i].fw_name);
3814 } while (--i >= 0);
3815 kfree(parents);
3816
3817 return ret;
3818 }
3819 }
3820
3821 return 0;
3822 }
3823
clk_core_free_parent_map(struct clk_core * core)3824 static void clk_core_free_parent_map(struct clk_core *core)
3825 {
3826 int i = core->num_parents;
3827
3828 if (!core->num_parents)
3829 return;
3830
3831 while (--i >= 0) {
3832 kfree_const(core->parents[i].name);
3833 kfree_const(core->parents[i].fw_name);
3834 }
3835
3836 kfree(core->parents);
3837 }
3838
3839 static struct clk *
__clk_register(struct device * dev,struct device_node * np,struct clk_hw * hw)3840 __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
3841 {
3842 int ret;
3843 struct clk_core *core;
3844 const struct clk_init_data *init = hw->init;
3845
3846 /*
3847 * The init data is not supposed to be used outside of registration path.
3848 * Set it to NULL so that provider drivers can't use it either and so that
3849 * we catch use of hw->init early on in the core.
3850 */
3851 hw->init = NULL;
3852
3853 core = kzalloc(sizeof(*core), GFP_KERNEL);
3854 if (!core) {
3855 ret = -ENOMEM;
3856 goto fail_out;
3857 }
3858
3859 core->name = kstrdup_const(init->name, GFP_KERNEL);
3860 if (!core->name) {
3861 ret = -ENOMEM;
3862 goto fail_name;
3863 }
3864
3865 if (WARN_ON(!init->ops)) {
3866 ret = -EINVAL;
3867 goto fail_ops;
3868 }
3869 core->ops = init->ops;
3870
3871 if (dev && pm_runtime_enabled(dev))
3872 core->rpm_enabled = true;
3873 core->dev = dev;
3874 core->of_node = np;
3875 if (dev && dev->driver)
3876 core->owner = dev->driver->owner;
3877 core->hw = hw;
3878 core->flags = init->flags;
3879 core->num_parents = init->num_parents;
3880 core->min_rate = 0;
3881 core->max_rate = ULONG_MAX;
3882
3883 ret = clk_core_populate_parent_map(core, init);
3884 if (ret)
3885 goto fail_parents;
3886
3887 INIT_HLIST_HEAD(&core->clks);
3888
3889 /*
3890 * Don't call clk_hw_create_clk() here because that would pin the
3891 * provider module to itself and prevent it from ever being removed.
3892 */
3893 hw->clk = alloc_clk(core, NULL, NULL);
3894 if (IS_ERR(hw->clk)) {
3895 ret = PTR_ERR(hw->clk);
3896 goto fail_create_clk;
3897 }
3898
3899 clk_core_link_consumer(core, hw->clk);
3900
3901 ret = __clk_core_init(core);
3902 if (!ret)
3903 return hw->clk;
3904
3905 clk_prepare_lock();
3906 clk_core_unlink_consumer(hw->clk);
3907 clk_prepare_unlock();
3908
3909 free_clk(hw->clk);
3910 hw->clk = NULL;
3911
3912 fail_create_clk:
3913 clk_core_free_parent_map(core);
3914 fail_parents:
3915 fail_ops:
3916 kfree_const(core->name);
3917 fail_name:
3918 kfree(core);
3919 fail_out:
3920 return ERR_PTR(ret);
3921 }
3922
3923 /**
3924 * dev_or_parent_of_node() - Get device node of @dev or @dev's parent
3925 * @dev: Device to get device node of
3926 *
3927 * Return: device node pointer of @dev, or the device node pointer of
3928 * @dev->parent if dev doesn't have a device node, or NULL if neither
3929 * @dev or @dev->parent have a device node.
3930 */
dev_or_parent_of_node(struct device * dev)3931 static struct device_node *dev_or_parent_of_node(struct device *dev)
3932 {
3933 struct device_node *np;
3934
3935 if (!dev)
3936 return NULL;
3937
3938 np = dev_of_node(dev);
3939 if (!np)
3940 np = dev_of_node(dev->parent);
3941
3942 return np;
3943 }
3944
3945 /**
3946 * clk_register - allocate a new clock, register it and return an opaque cookie
3947 * @dev: device that is registering this clock
3948 * @hw: link to hardware-specific clock data
3949 *
3950 * clk_register is the *deprecated* interface for populating the clock tree with
3951 * new clock nodes. Use clk_hw_register() instead.
3952 *
3953 * Returns: a pointer to the newly allocated struct clk which
3954 * cannot be dereferenced by driver code but may be used in conjunction with the
3955 * rest of the clock API. In the event of an error clk_register will return an
3956 * error code; drivers must test for an error code after calling clk_register.
3957 */
clk_register(struct device * dev,struct clk_hw * hw)3958 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3959 {
3960 return __clk_register(dev, dev_or_parent_of_node(dev), hw);
3961 }
3962 EXPORT_SYMBOL_GPL(clk_register);
3963
3964 /**
3965 * clk_hw_register - register a clk_hw and return an error code
3966 * @dev: device that is registering this clock
3967 * @hw: link to hardware-specific clock data
3968 *
3969 * clk_hw_register is the primary interface for populating the clock tree with
3970 * new clock nodes. It returns an integer equal to zero indicating success or
3971 * less than zero indicating failure. Drivers must test for an error code after
3972 * calling clk_hw_register().
3973 */
clk_hw_register(struct device * dev,struct clk_hw * hw)3974 int clk_hw_register(struct device *dev, struct clk_hw *hw)
3975 {
3976 return PTR_ERR_OR_ZERO(__clk_register(dev, dev_or_parent_of_node(dev),
3977 hw));
3978 }
3979 EXPORT_SYMBOL_GPL(clk_hw_register);
3980
3981 /*
3982 * of_clk_hw_register - register a clk_hw and return an error code
3983 * @node: device_node of device that is registering this clock
3984 * @hw: link to hardware-specific clock data
3985 *
3986 * of_clk_hw_register() is the primary interface for populating the clock tree
3987 * with new clock nodes when a struct device is not available, but a struct
3988 * device_node is. It returns an integer equal to zero indicating success or
3989 * less than zero indicating failure. Drivers must test for an error code after
3990 * calling of_clk_hw_register().
3991 */
of_clk_hw_register(struct device_node * node,struct clk_hw * hw)3992 int of_clk_hw_register(struct device_node *node, struct clk_hw *hw)
3993 {
3994 return PTR_ERR_OR_ZERO(__clk_register(NULL, node, hw));
3995 }
3996 EXPORT_SYMBOL_GPL(of_clk_hw_register);
3997
3998 /* Free memory allocated for a clock. */
__clk_release(struct kref * ref)3999 static void __clk_release(struct kref *ref)
4000 {
4001 struct clk_core *core = container_of(ref, struct clk_core, ref);
4002
4003 lockdep_assert_held(&prepare_lock);
4004
4005 clk_core_free_parent_map(core);
4006 kfree_const(core->name);
4007 kfree(core);
4008 }
4009
4010 /*
4011 * Empty clk_ops for unregistered clocks. These are used temporarily
4012 * after clk_unregister() was called on a clock and until last clock
4013 * consumer calls clk_put() and the struct clk object is freed.
4014 */
clk_nodrv_prepare_enable(struct clk_hw * hw)4015 static int clk_nodrv_prepare_enable(struct clk_hw *hw)
4016 {
4017 return -ENXIO;
4018 }
4019
clk_nodrv_disable_unprepare(struct clk_hw * hw)4020 static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
4021 {
4022 WARN_ON_ONCE(1);
4023 }
4024
clk_nodrv_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)4025 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
4026 unsigned long parent_rate)
4027 {
4028 return -ENXIO;
4029 }
4030
clk_nodrv_set_parent(struct clk_hw * hw,u8 index)4031 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
4032 {
4033 return -ENXIO;
4034 }
4035
4036 static const struct clk_ops clk_nodrv_ops = {
4037 .enable = clk_nodrv_prepare_enable,
4038 .disable = clk_nodrv_disable_unprepare,
4039 .prepare = clk_nodrv_prepare_enable,
4040 .unprepare = clk_nodrv_disable_unprepare,
4041 .set_rate = clk_nodrv_set_rate,
4042 .set_parent = clk_nodrv_set_parent,
4043 };
4044
clk_core_evict_parent_cache_subtree(struct clk_core * root,struct clk_core * target)4045 static void clk_core_evict_parent_cache_subtree(struct clk_core *root,
4046 struct clk_core *target)
4047 {
4048 int i;
4049 struct clk_core *child;
4050
4051 for (i = 0; i < root->num_parents; i++)
4052 if (root->parents[i].core == target)
4053 root->parents[i].core = NULL;
4054
4055 hlist_for_each_entry(child, &root->children, child_node)
4056 clk_core_evict_parent_cache_subtree(child, target);
4057 }
4058
4059 /* Remove this clk from all parent caches */
clk_core_evict_parent_cache(struct clk_core * core)4060 static void clk_core_evict_parent_cache(struct clk_core *core)
4061 {
4062 struct hlist_head **lists;
4063 struct clk_core *root;
4064
4065 lockdep_assert_held(&prepare_lock);
4066
4067 for (lists = all_lists; *lists; lists++)
4068 hlist_for_each_entry(root, *lists, child_node)
4069 clk_core_evict_parent_cache_subtree(root, core);
4070
4071 }
4072
4073 /**
4074 * clk_unregister - unregister a currently registered clock
4075 * @clk: clock to unregister
4076 */
clk_unregister(struct clk * clk)4077 void clk_unregister(struct clk *clk)
4078 {
4079 unsigned long flags;
4080 const struct clk_ops *ops;
4081
4082 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
4083 return;
4084
4085 clk_debug_unregister(clk->core);
4086
4087 clk_prepare_lock();
4088
4089 ops = clk->core->ops;
4090 if (ops == &clk_nodrv_ops) {
4091 pr_err("%s: unregistered clock: %s\n", __func__,
4092 clk->core->name);
4093 goto unlock;
4094 }
4095 /*
4096 * Assign empty clock ops for consumers that might still hold
4097 * a reference to this clock.
4098 */
4099 flags = clk_enable_lock();
4100 clk->core->ops = &clk_nodrv_ops;
4101 clk_enable_unlock(flags);
4102
4103 if (ops->terminate)
4104 ops->terminate(clk->core->hw);
4105
4106 if (!hlist_empty(&clk->core->children)) {
4107 struct clk_core *child;
4108 struct hlist_node *t;
4109
4110 /* Reparent all children to the orphan list. */
4111 hlist_for_each_entry_safe(child, t, &clk->core->children,
4112 child_node)
4113 clk_core_set_parent_nolock(child, NULL);
4114 }
4115
4116 clk_core_evict_parent_cache(clk->core);
4117
4118 hlist_del_init(&clk->core->child_node);
4119
4120 if (clk->core->prepare_count)
4121 pr_warn("%s: unregistering prepared clock: %s\n",
4122 __func__, clk->core->name);
4123
4124 if (clk->core->protect_count)
4125 pr_warn("%s: unregistering protected clock: %s\n",
4126 __func__, clk->core->name);
4127
4128 kref_put(&clk->core->ref, __clk_release);
4129 free_clk(clk);
4130 unlock:
4131 clk_prepare_unlock();
4132 }
4133 EXPORT_SYMBOL_GPL(clk_unregister);
4134
4135 /**
4136 * clk_hw_unregister - unregister a currently registered clk_hw
4137 * @hw: hardware-specific clock data to unregister
4138 */
clk_hw_unregister(struct clk_hw * hw)4139 void clk_hw_unregister(struct clk_hw *hw)
4140 {
4141 clk_unregister(hw->clk);
4142 }
4143 EXPORT_SYMBOL_GPL(clk_hw_unregister);
4144
devm_clk_release(struct device * dev,void * res)4145 static void devm_clk_release(struct device *dev, void *res)
4146 {
4147 clk_unregister(*(struct clk **)res);
4148 }
4149
devm_clk_hw_release(struct device * dev,void * res)4150 static void devm_clk_hw_release(struct device *dev, void *res)
4151 {
4152 clk_hw_unregister(*(struct clk_hw **)res);
4153 }
4154
4155 /**
4156 * devm_clk_register - resource managed clk_register()
4157 * @dev: device that is registering this clock
4158 * @hw: link to hardware-specific clock data
4159 *
4160 * Managed clk_register(). This function is *deprecated*, use devm_clk_hw_register() instead.
4161 *
4162 * Clocks returned from this function are automatically clk_unregister()ed on
4163 * driver detach. See clk_register() for more information.
4164 */
devm_clk_register(struct device * dev,struct clk_hw * hw)4165 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
4166 {
4167 struct clk *clk;
4168 struct clk **clkp;
4169
4170 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
4171 if (!clkp)
4172 return ERR_PTR(-ENOMEM);
4173
4174 clk = clk_register(dev, hw);
4175 if (!IS_ERR(clk)) {
4176 *clkp = clk;
4177 devres_add(dev, clkp);
4178 } else {
4179 devres_free(clkp);
4180 }
4181
4182 return clk;
4183 }
4184 EXPORT_SYMBOL_GPL(devm_clk_register);
4185
4186 /**
4187 * devm_clk_hw_register - resource managed clk_hw_register()
4188 * @dev: device that is registering this clock
4189 * @hw: link to hardware-specific clock data
4190 *
4191 * Managed clk_hw_register(). Clocks registered by this function are
4192 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
4193 * for more information.
4194 */
devm_clk_hw_register(struct device * dev,struct clk_hw * hw)4195 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
4196 {
4197 struct clk_hw **hwp;
4198 int ret;
4199
4200 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
4201 if (!hwp)
4202 return -ENOMEM;
4203
4204 ret = clk_hw_register(dev, hw);
4205 if (!ret) {
4206 *hwp = hw;
4207 devres_add(dev, hwp);
4208 } else {
4209 devres_free(hwp);
4210 }
4211
4212 return ret;
4213 }
4214 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
4215
devm_clk_match(struct device * dev,void * res,void * data)4216 static int devm_clk_match(struct device *dev, void *res, void *data)
4217 {
4218 struct clk *c = res;
4219 if (WARN_ON(!c))
4220 return 0;
4221 return c == data;
4222 }
4223
devm_clk_hw_match(struct device * dev,void * res,void * data)4224 static int devm_clk_hw_match(struct device *dev, void *res, void *data)
4225 {
4226 struct clk_hw *hw = res;
4227
4228 if (WARN_ON(!hw))
4229 return 0;
4230 return hw == data;
4231 }
4232
4233 /**
4234 * devm_clk_unregister - resource managed clk_unregister()
4235 * @dev: device that is unregistering the clock data
4236 * @clk: clock to unregister
4237 *
4238 * Deallocate a clock allocated with devm_clk_register(). Normally
4239 * this function will not need to be called and the resource management
4240 * code will ensure that the resource is freed.
4241 */
devm_clk_unregister(struct device * dev,struct clk * clk)4242 void devm_clk_unregister(struct device *dev, struct clk *clk)
4243 {
4244 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
4245 }
4246 EXPORT_SYMBOL_GPL(devm_clk_unregister);
4247
4248 /**
4249 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
4250 * @dev: device that is unregistering the hardware-specific clock data
4251 * @hw: link to hardware-specific clock data
4252 *
4253 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
4254 * this function will not need to be called and the resource management
4255 * code will ensure that the resource is freed.
4256 */
devm_clk_hw_unregister(struct device * dev,struct clk_hw * hw)4257 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
4258 {
4259 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
4260 hw));
4261 }
4262 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
4263
4264 /*
4265 * clkdev helpers
4266 */
4267
__clk_put(struct clk * clk)4268 void __clk_put(struct clk *clk)
4269 {
4270 struct module *owner;
4271
4272 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
4273 return;
4274
4275 clk_prepare_lock();
4276
4277 /*
4278 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
4279 * given user should be balanced with calls to clk_rate_exclusive_put()
4280 * and by that same consumer
4281 */
4282 if (WARN_ON(clk->exclusive_count)) {
4283 /* We voiced our concern, let's sanitize the situation */
4284 clk->core->protect_count -= (clk->exclusive_count - 1);
4285 clk_core_rate_unprotect(clk->core);
4286 clk->exclusive_count = 0;
4287 }
4288
4289 hlist_del(&clk->clks_node);
4290 if (clk->min_rate > clk->core->req_rate ||
4291 clk->max_rate < clk->core->req_rate)
4292 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
4293
4294 owner = clk->core->owner;
4295 kref_put(&clk->core->ref, __clk_release);
4296
4297 clk_prepare_unlock();
4298
4299 module_put(owner);
4300
4301 free_clk(clk);
4302 }
4303
4304 /*** clk rate change notifiers ***/
4305
4306 /**
4307 * clk_notifier_register - add a clk rate change notifier
4308 * @clk: struct clk * to watch
4309 * @nb: struct notifier_block * with callback info
4310 *
4311 * Request notification when clk's rate changes. This uses an SRCU
4312 * notifier because we want it to block and notifier unregistrations are
4313 * uncommon. The callbacks associated with the notifier must not
4314 * re-enter into the clk framework by calling any top-level clk APIs;
4315 * this will cause a nested prepare_lock mutex.
4316 *
4317 * In all notification cases (pre, post and abort rate change) the original
4318 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
4319 * and the new frequency is passed via struct clk_notifier_data.new_rate.
4320 *
4321 * clk_notifier_register() must be called from non-atomic context.
4322 * Returns -EINVAL if called with null arguments, -ENOMEM upon
4323 * allocation failure; otherwise, passes along the return value of
4324 * srcu_notifier_chain_register().
4325 */
clk_notifier_register(struct clk * clk,struct notifier_block * nb)4326 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
4327 {
4328 struct clk_notifier *cn;
4329 int ret = -ENOMEM;
4330
4331 if (!clk || !nb)
4332 return -EINVAL;
4333
4334 clk_prepare_lock();
4335
4336 /* search the list of notifiers for this clk */
4337 list_for_each_entry(cn, &clk_notifier_list, node)
4338 if (cn->clk == clk)
4339 goto found;
4340
4341 /* if clk wasn't in the notifier list, allocate new clk_notifier */
4342 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
4343 if (!cn)
4344 goto out;
4345
4346 cn->clk = clk;
4347 srcu_init_notifier_head(&cn->notifier_head);
4348
4349 list_add(&cn->node, &clk_notifier_list);
4350
4351 found:
4352 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
4353
4354 clk->core->notifier_count++;
4355
4356 out:
4357 clk_prepare_unlock();
4358
4359 return ret;
4360 }
4361 EXPORT_SYMBOL_GPL(clk_notifier_register);
4362
4363 /**
4364 * clk_notifier_unregister - remove a clk rate change notifier
4365 * @clk: struct clk *
4366 * @nb: struct notifier_block * with callback info
4367 *
4368 * Request no further notification for changes to 'clk' and frees memory
4369 * allocated in clk_notifier_register.
4370 *
4371 * Returns -EINVAL if called with null arguments; otherwise, passes
4372 * along the return value of srcu_notifier_chain_unregister().
4373 */
clk_notifier_unregister(struct clk * clk,struct notifier_block * nb)4374 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
4375 {
4376 struct clk_notifier *cn;
4377 int ret = -ENOENT;
4378
4379 if (!clk || !nb)
4380 return -EINVAL;
4381
4382 clk_prepare_lock();
4383
4384 list_for_each_entry(cn, &clk_notifier_list, node) {
4385 if (cn->clk == clk) {
4386 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
4387
4388 clk->core->notifier_count--;
4389
4390 /* XXX the notifier code should handle this better */
4391 if (!cn->notifier_head.head) {
4392 srcu_cleanup_notifier_head(&cn->notifier_head);
4393 list_del(&cn->node);
4394 kfree(cn);
4395 }
4396 break;
4397 }
4398 }
4399
4400 clk_prepare_unlock();
4401
4402 return ret;
4403 }
4404 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
4405
4406 #ifdef CONFIG_OF
clk_core_reparent_orphans(void)4407 static void clk_core_reparent_orphans(void)
4408 {
4409 clk_prepare_lock();
4410 clk_core_reparent_orphans_nolock();
4411 clk_prepare_unlock();
4412 }
4413
4414 /**
4415 * struct of_clk_provider - Clock provider registration structure
4416 * @link: Entry in global list of clock providers
4417 * @node: Pointer to device tree node of clock provider
4418 * @get: Get clock callback. Returns NULL or a struct clk for the
4419 * given clock specifier
4420 * @get_hw: Get clk_hw callback. Returns NULL, ERR_PTR or a
4421 * struct clk_hw for the given clock specifier
4422 * @data: context pointer to be passed into @get callback
4423 */
4424 struct of_clk_provider {
4425 struct list_head link;
4426
4427 struct device_node *node;
4428 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
4429 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
4430 void *data;
4431 };
4432
4433 extern struct of_device_id __clk_of_table;
4434 static const struct of_device_id __clk_of_table_sentinel
4435 __used __section("__clk_of_table_end");
4436
4437 static LIST_HEAD(of_clk_providers);
4438 static DEFINE_MUTEX(of_clk_mutex);
4439
of_clk_src_simple_get(struct of_phandle_args * clkspec,void * data)4440 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
4441 void *data)
4442 {
4443 return data;
4444 }
4445 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
4446
of_clk_hw_simple_get(struct of_phandle_args * clkspec,void * data)4447 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
4448 {
4449 return data;
4450 }
4451 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
4452
of_clk_src_onecell_get(struct of_phandle_args * clkspec,void * data)4453 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
4454 {
4455 struct clk_onecell_data *clk_data = data;
4456 unsigned int idx = clkspec->args[0];
4457
4458 if (idx >= clk_data->clk_num) {
4459 pr_err("%s: invalid clock index %u\n", __func__, idx);
4460 return ERR_PTR(-EINVAL);
4461 }
4462
4463 return clk_data->clks[idx];
4464 }
4465 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
4466
4467 struct clk_hw *
of_clk_hw_onecell_get(struct of_phandle_args * clkspec,void * data)4468 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
4469 {
4470 struct clk_hw_onecell_data *hw_data = data;
4471 unsigned int idx = clkspec->args[0];
4472
4473 if (idx >= hw_data->num) {
4474 pr_err("%s: invalid index %u\n", __func__, idx);
4475 return ERR_PTR(-EINVAL);
4476 }
4477
4478 return hw_data->hws[idx];
4479 }
4480 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
4481
4482 /**
4483 * of_clk_add_provider() - Register a clock provider for a node
4484 * @np: Device node pointer associated with clock provider
4485 * @clk_src_get: callback for decoding clock
4486 * @data: context pointer for @clk_src_get callback.
4487 *
4488 * This function is *deprecated*. Use of_clk_add_hw_provider() instead.
4489 */
of_clk_add_provider(struct device_node * np,struct clk * (* clk_src_get)(struct of_phandle_args * clkspec,void * data),void * data)4490 int of_clk_add_provider(struct device_node *np,
4491 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
4492 void *data),
4493 void *data)
4494 {
4495 struct of_clk_provider *cp;
4496 int ret;
4497
4498 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4499 if (!cp)
4500 return -ENOMEM;
4501
4502 cp->node = of_node_get(np);
4503 cp->data = data;
4504 cp->get = clk_src_get;
4505
4506 mutex_lock(&of_clk_mutex);
4507 list_add(&cp->link, &of_clk_providers);
4508 mutex_unlock(&of_clk_mutex);
4509 pr_debug("Added clock from %pOF\n", np);
4510
4511 clk_core_reparent_orphans();
4512
4513 ret = of_clk_set_defaults(np, true);
4514 if (ret < 0)
4515 of_clk_del_provider(np);
4516
4517 return ret;
4518 }
4519 EXPORT_SYMBOL_GPL(of_clk_add_provider);
4520
4521 /**
4522 * of_clk_add_hw_provider() - Register a clock provider for a node
4523 * @np: Device node pointer associated with clock provider
4524 * @get: callback for decoding clk_hw
4525 * @data: context pointer for @get callback.
4526 */
of_clk_add_hw_provider(struct device_node * np,struct clk_hw * (* get)(struct of_phandle_args * clkspec,void * data),void * data)4527 int of_clk_add_hw_provider(struct device_node *np,
4528 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4529 void *data),
4530 void *data)
4531 {
4532 struct of_clk_provider *cp;
4533 int ret;
4534
4535 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4536 if (!cp)
4537 return -ENOMEM;
4538
4539 cp->node = of_node_get(np);
4540 cp->data = data;
4541 cp->get_hw = get;
4542
4543 mutex_lock(&of_clk_mutex);
4544 list_add(&cp->link, &of_clk_providers);
4545 mutex_unlock(&of_clk_mutex);
4546 pr_debug("Added clk_hw provider from %pOF\n", np);
4547
4548 clk_core_reparent_orphans();
4549
4550 ret = of_clk_set_defaults(np, true);
4551 if (ret < 0)
4552 of_clk_del_provider(np);
4553
4554 return ret;
4555 }
4556 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
4557
devm_of_clk_release_provider(struct device * dev,void * res)4558 static void devm_of_clk_release_provider(struct device *dev, void *res)
4559 {
4560 of_clk_del_provider(*(struct device_node **)res);
4561 }
4562
4563 /*
4564 * We allow a child device to use its parent device as the clock provider node
4565 * for cases like MFD sub-devices where the child device driver wants to use
4566 * devm_*() APIs but not list the device in DT as a sub-node.
4567 */
get_clk_provider_node(struct device * dev)4568 static struct device_node *get_clk_provider_node(struct device *dev)
4569 {
4570 struct device_node *np, *parent_np;
4571
4572 np = dev->of_node;
4573 parent_np = dev->parent ? dev->parent->of_node : NULL;
4574
4575 if (!of_find_property(np, "#clock-cells", NULL))
4576 if (of_find_property(parent_np, "#clock-cells", NULL))
4577 np = parent_np;
4578
4579 return np;
4580 }
4581
4582 /**
4583 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4584 * @dev: Device acting as the clock provider (used for DT node and lifetime)
4585 * @get: callback for decoding clk_hw
4586 * @data: context pointer for @get callback
4587 *
4588 * Registers clock provider for given device's node. If the device has no DT
4589 * node or if the device node lacks of clock provider information (#clock-cells)
4590 * then the parent device's node is scanned for this information. If parent node
4591 * has the #clock-cells then it is used in registration. Provider is
4592 * automatically released at device exit.
4593 *
4594 * Return: 0 on success or an errno on failure.
4595 */
devm_of_clk_add_hw_provider(struct device * dev,struct clk_hw * (* get)(struct of_phandle_args * clkspec,void * data),void * data)4596 int devm_of_clk_add_hw_provider(struct device *dev,
4597 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4598 void *data),
4599 void *data)
4600 {
4601 struct device_node **ptr, *np;
4602 int ret;
4603
4604 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
4605 GFP_KERNEL);
4606 if (!ptr)
4607 return -ENOMEM;
4608
4609 np = get_clk_provider_node(dev);
4610 ret = of_clk_add_hw_provider(np, get, data);
4611 if (!ret) {
4612 *ptr = np;
4613 devres_add(dev, ptr);
4614 } else {
4615 devres_free(ptr);
4616 }
4617
4618 return ret;
4619 }
4620 EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
4621
4622 /**
4623 * of_clk_del_provider() - Remove a previously registered clock provider
4624 * @np: Device node pointer associated with clock provider
4625 */
of_clk_del_provider(struct device_node * np)4626 void of_clk_del_provider(struct device_node *np)
4627 {
4628 struct of_clk_provider *cp;
4629
4630 mutex_lock(&of_clk_mutex);
4631 list_for_each_entry(cp, &of_clk_providers, link) {
4632 if (cp->node == np) {
4633 list_del(&cp->link);
4634 of_node_put(cp->node);
4635 kfree(cp);
4636 break;
4637 }
4638 }
4639 mutex_unlock(&of_clk_mutex);
4640 }
4641 EXPORT_SYMBOL_GPL(of_clk_del_provider);
4642
devm_clk_provider_match(struct device * dev,void * res,void * data)4643 static int devm_clk_provider_match(struct device *dev, void *res, void *data)
4644 {
4645 struct device_node **np = res;
4646
4647 if (WARN_ON(!np || !*np))
4648 return 0;
4649
4650 return *np == data;
4651 }
4652
4653 /**
4654 * devm_of_clk_del_provider() - Remove clock provider registered using devm
4655 * @dev: Device to whose lifetime the clock provider was bound
4656 */
devm_of_clk_del_provider(struct device * dev)4657 void devm_of_clk_del_provider(struct device *dev)
4658 {
4659 int ret;
4660 struct device_node *np = get_clk_provider_node(dev);
4661
4662 ret = devres_release(dev, devm_of_clk_release_provider,
4663 devm_clk_provider_match, np);
4664
4665 WARN_ON(ret);
4666 }
4667 EXPORT_SYMBOL(devm_of_clk_del_provider);
4668
4669 /**
4670 * of_parse_clkspec() - Parse a DT clock specifier for a given device node
4671 * @np: device node to parse clock specifier from
4672 * @index: index of phandle to parse clock out of. If index < 0, @name is used
4673 * @name: clock name to find and parse. If name is NULL, the index is used
4674 * @out_args: Result of parsing the clock specifier
4675 *
4676 * Parses a device node's "clocks" and "clock-names" properties to find the
4677 * phandle and cells for the index or name that is desired. The resulting clock
4678 * specifier is placed into @out_args, or an errno is returned when there's a
4679 * parsing error. The @index argument is ignored if @name is non-NULL.
4680 *
4681 * Example:
4682 *
4683 * phandle1: clock-controller@1 {
4684 * #clock-cells = <2>;
4685 * }
4686 *
4687 * phandle2: clock-controller@2 {
4688 * #clock-cells = <1>;
4689 * }
4690 *
4691 * clock-consumer@3 {
4692 * clocks = <&phandle1 1 2 &phandle2 3>;
4693 * clock-names = "name1", "name2";
4694 * }
4695 *
4696 * To get a device_node for `clock-controller@2' node you may call this
4697 * function a few different ways:
4698 *
4699 * of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
4700 * of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
4701 * of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
4702 *
4703 * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
4704 * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
4705 * the "clock-names" property of @np.
4706 */
of_parse_clkspec(const struct device_node * np,int index,const char * name,struct of_phandle_args * out_args)4707 static int of_parse_clkspec(const struct device_node *np, int index,
4708 const char *name, struct of_phandle_args *out_args)
4709 {
4710 int ret = -ENOENT;
4711
4712 /* Walk up the tree of devices looking for a clock property that matches */
4713 while (np) {
4714 /*
4715 * For named clocks, first look up the name in the
4716 * "clock-names" property. If it cannot be found, then index
4717 * will be an error code and of_parse_phandle_with_args() will
4718 * return -EINVAL.
4719 */
4720 if (name)
4721 index = of_property_match_string(np, "clock-names", name);
4722 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
4723 index, out_args);
4724 if (!ret)
4725 break;
4726 if (name && index >= 0)
4727 break;
4728
4729 /*
4730 * No matching clock found on this node. If the parent node
4731 * has a "clock-ranges" property, then we can try one of its
4732 * clocks.
4733 */
4734 np = np->parent;
4735 if (np && !of_get_property(np, "clock-ranges", NULL))
4736 break;
4737 index = 0;
4738 }
4739
4740 return ret;
4741 }
4742
4743 static struct clk_hw *
__of_clk_get_hw_from_provider(struct of_clk_provider * provider,struct of_phandle_args * clkspec)4744 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4745 struct of_phandle_args *clkspec)
4746 {
4747 struct clk *clk;
4748
4749 if (provider->get_hw)
4750 return provider->get_hw(clkspec, provider->data);
4751
4752 clk = provider->get(clkspec, provider->data);
4753 if (IS_ERR(clk))
4754 return ERR_CAST(clk);
4755 return __clk_get_hw(clk);
4756 }
4757
4758 static struct clk_hw *
of_clk_get_hw_from_clkspec(struct of_phandle_args * clkspec)4759 of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
4760 {
4761 struct of_clk_provider *provider;
4762 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
4763
4764 if (!clkspec)
4765 return ERR_PTR(-EINVAL);
4766
4767 mutex_lock(&of_clk_mutex);
4768 list_for_each_entry(provider, &of_clk_providers, link) {
4769 if (provider->node == clkspec->np) {
4770 hw = __of_clk_get_hw_from_provider(provider, clkspec);
4771 if (!IS_ERR(hw))
4772 break;
4773 }
4774 }
4775 mutex_unlock(&of_clk_mutex);
4776
4777 return hw;
4778 }
4779
4780 /**
4781 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4782 * @clkspec: pointer to a clock specifier data structure
4783 *
4784 * This function looks up a struct clk from the registered list of clock
4785 * providers, an input is a clock specifier data structure as returned
4786 * from the of_parse_phandle_with_args() function call.
4787 */
of_clk_get_from_provider(struct of_phandle_args * clkspec)4788 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4789 {
4790 struct clk_hw *hw = of_clk_get_hw_from_clkspec(clkspec);
4791
4792 return clk_hw_create_clk(NULL, hw, NULL, __func__);
4793 }
4794 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
4795
of_clk_get_hw(struct device_node * np,int index,const char * con_id)4796 struct clk_hw *of_clk_get_hw(struct device_node *np, int index,
4797 const char *con_id)
4798 {
4799 int ret;
4800 struct clk_hw *hw;
4801 struct of_phandle_args clkspec;
4802
4803 ret = of_parse_clkspec(np, index, con_id, &clkspec);
4804 if (ret)
4805 return ERR_PTR(ret);
4806
4807 hw = of_clk_get_hw_from_clkspec(&clkspec);
4808 of_node_put(clkspec.np);
4809
4810 return hw;
4811 }
4812
__of_clk_get(struct device_node * np,int index,const char * dev_id,const char * con_id)4813 static struct clk *__of_clk_get(struct device_node *np,
4814 int index, const char *dev_id,
4815 const char *con_id)
4816 {
4817 struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
4818
4819 return clk_hw_create_clk(NULL, hw, dev_id, con_id);
4820 }
4821
of_clk_get(struct device_node * np,int index)4822 struct clk *of_clk_get(struct device_node *np, int index)
4823 {
4824 return __of_clk_get(np, index, np->full_name, NULL);
4825 }
4826 EXPORT_SYMBOL(of_clk_get);
4827
4828 /**
4829 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
4830 * @np: pointer to clock consumer node
4831 * @name: name of consumer's clock input, or NULL for the first clock reference
4832 *
4833 * This function parses the clocks and clock-names properties,
4834 * and uses them to look up the struct clk from the registered list of clock
4835 * providers.
4836 */
of_clk_get_by_name(struct device_node * np,const char * name)4837 struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
4838 {
4839 if (!np)
4840 return ERR_PTR(-ENOENT);
4841
4842 return __of_clk_get(np, 0, np->full_name, name);
4843 }
4844 EXPORT_SYMBOL(of_clk_get_by_name);
4845
4846 /**
4847 * of_clk_get_parent_count() - Count the number of clocks a device node has
4848 * @np: device node to count
4849 *
4850 * Returns: The number of clocks that are possible parents of this node
4851 */
of_clk_get_parent_count(const struct device_node * np)4852 unsigned int of_clk_get_parent_count(const struct device_node *np)
4853 {
4854 int count;
4855
4856 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4857 if (count < 0)
4858 return 0;
4859
4860 return count;
4861 }
4862 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4863
of_clk_get_parent_name(const struct device_node * np,int index)4864 const char *of_clk_get_parent_name(const struct device_node *np, int index)
4865 {
4866 struct of_phandle_args clkspec;
4867 struct property *prop;
4868 const char *clk_name;
4869 const __be32 *vp;
4870 u32 pv;
4871 int rc;
4872 int count;
4873 struct clk *clk;
4874
4875 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4876 &clkspec);
4877 if (rc)
4878 return NULL;
4879
4880 index = clkspec.args_count ? clkspec.args[0] : 0;
4881 count = 0;
4882
4883 /* if there is an indices property, use it to transfer the index
4884 * specified into an array offset for the clock-output-names property.
4885 */
4886 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4887 if (index == pv) {
4888 index = count;
4889 break;
4890 }
4891 count++;
4892 }
4893 /* We went off the end of 'clock-indices' without finding it */
4894 if (prop && !vp)
4895 return NULL;
4896
4897 if (of_property_read_string_index(clkspec.np, "clock-output-names",
4898 index,
4899 &clk_name) < 0) {
4900 /*
4901 * Best effort to get the name if the clock has been
4902 * registered with the framework. If the clock isn't
4903 * registered, we return the node name as the name of
4904 * the clock as long as #clock-cells = 0.
4905 */
4906 clk = of_clk_get_from_provider(&clkspec);
4907 if (IS_ERR(clk)) {
4908 if (clkspec.args_count == 0)
4909 clk_name = clkspec.np->name;
4910 else
4911 clk_name = NULL;
4912 } else {
4913 clk_name = __clk_get_name(clk);
4914 clk_put(clk);
4915 }
4916 }
4917
4918
4919 of_node_put(clkspec.np);
4920 return clk_name;
4921 }
4922 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4923
4924 /**
4925 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4926 * number of parents
4927 * @np: Device node pointer associated with clock provider
4928 * @parents: pointer to char array that hold the parents' names
4929 * @size: size of the @parents array
4930 *
4931 * Return: number of parents for the clock node.
4932 */
of_clk_parent_fill(struct device_node * np,const char ** parents,unsigned int size)4933 int of_clk_parent_fill(struct device_node *np, const char **parents,
4934 unsigned int size)
4935 {
4936 unsigned int i = 0;
4937
4938 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4939 i++;
4940
4941 return i;
4942 }
4943 EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4944
4945 struct clock_provider {
4946 void (*clk_init_cb)(struct device_node *);
4947 struct device_node *np;
4948 struct list_head node;
4949 };
4950
4951 /*
4952 * This function looks for a parent clock. If there is one, then it
4953 * checks that the provider for this parent clock was initialized, in
4954 * this case the parent clock will be ready.
4955 */
parent_ready(struct device_node * np)4956 static int parent_ready(struct device_node *np)
4957 {
4958 int i = 0;
4959
4960 while (true) {
4961 struct clk *clk = of_clk_get(np, i);
4962
4963 /* this parent is ready we can check the next one */
4964 if (!IS_ERR(clk)) {
4965 clk_put(clk);
4966 i++;
4967 continue;
4968 }
4969
4970 /* at least one parent is not ready, we exit now */
4971 if (PTR_ERR(clk) == -EPROBE_DEFER)
4972 return 0;
4973
4974 /*
4975 * Here we make assumption that the device tree is
4976 * written correctly. So an error means that there is
4977 * no more parent. As we didn't exit yet, then the
4978 * previous parent are ready. If there is no clock
4979 * parent, no need to wait for them, then we can
4980 * consider their absence as being ready
4981 */
4982 return 1;
4983 }
4984 }
4985
4986 /**
4987 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4988 * @np: Device node pointer associated with clock provider
4989 * @index: clock index
4990 * @flags: pointer to top-level framework flags
4991 *
4992 * Detects if the clock-critical property exists and, if so, sets the
4993 * corresponding CLK_IS_CRITICAL flag.
4994 *
4995 * Do not use this function. It exists only for legacy Device Tree
4996 * bindings, such as the one-clock-per-node style that are outdated.
4997 * Those bindings typically put all clock data into .dts and the Linux
4998 * driver has no clock data, thus making it impossible to set this flag
4999 * correctly from the driver. Only those drivers may call
5000 * of_clk_detect_critical from their setup functions.
5001 *
5002 * Return: error code or zero on success
5003 */
of_clk_detect_critical(struct device_node * np,int index,unsigned long * flags)5004 int of_clk_detect_critical(struct device_node *np, int index,
5005 unsigned long *flags)
5006 {
5007 struct property *prop;
5008 const __be32 *cur;
5009 uint32_t idx;
5010
5011 if (!np || !flags)
5012 return -EINVAL;
5013
5014 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
5015 if (index == idx)
5016 *flags |= CLK_IS_CRITICAL;
5017
5018 return 0;
5019 }
5020
5021 /**
5022 * of_clk_init() - Scan and init clock providers from the DT
5023 * @matches: array of compatible values and init functions for providers.
5024 *
5025 * This function scans the device tree for matching clock providers
5026 * and calls their initialization functions. It also does it by trying
5027 * to follow the dependencies.
5028 */
of_clk_init(const struct of_device_id * matches)5029 void __init of_clk_init(const struct of_device_id *matches)
5030 {
5031 const struct of_device_id *match;
5032 struct device_node *np;
5033 struct clock_provider *clk_provider, *next;
5034 bool is_init_done;
5035 bool force = false;
5036 LIST_HEAD(clk_provider_list);
5037
5038 if (!matches)
5039 matches = &__clk_of_table;
5040
5041 /* First prepare the list of the clocks providers */
5042 for_each_matching_node_and_match(np, matches, &match) {
5043 struct clock_provider *parent;
5044
5045 if (!of_device_is_available(np))
5046 continue;
5047
5048 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
5049 if (!parent) {
5050 list_for_each_entry_safe(clk_provider, next,
5051 &clk_provider_list, node) {
5052 list_del(&clk_provider->node);
5053 of_node_put(clk_provider->np);
5054 kfree(clk_provider);
5055 }
5056 of_node_put(np);
5057 return;
5058 }
5059
5060 parent->clk_init_cb = match->data;
5061 parent->np = of_node_get(np);
5062 list_add_tail(&parent->node, &clk_provider_list);
5063 }
5064
5065 while (!list_empty(&clk_provider_list)) {
5066 is_init_done = false;
5067 list_for_each_entry_safe(clk_provider, next,
5068 &clk_provider_list, node) {
5069 if (force || parent_ready(clk_provider->np)) {
5070
5071 /* Don't populate platform devices */
5072 of_node_set_flag(clk_provider->np,
5073 OF_POPULATED);
5074
5075 clk_provider->clk_init_cb(clk_provider->np);
5076 of_clk_set_defaults(clk_provider->np, true);
5077
5078 list_del(&clk_provider->node);
5079 of_node_put(clk_provider->np);
5080 kfree(clk_provider);
5081 is_init_done = true;
5082 }
5083 }
5084
5085 /*
5086 * We didn't manage to initialize any of the
5087 * remaining providers during the last loop, so now we
5088 * initialize all the remaining ones unconditionally
5089 * in case the clock parent was not mandatory
5090 */
5091 if (!is_init_done)
5092 force = true;
5093 }
5094 }
5095 #endif
5096