blob: 4289c519af1be74e4ca7708427f90a125270fd37 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Standard functionality for the common clock API. See Documentation/clk.txt
10 */
11
12#include <linux/clk.h>
13#include <linux/clk-provider.h>
14#include <linux/clk/clk-conf.h>
15#include <linux/module.h>
16#include <linux/mutex.h>
17#include <linux/spinlock.h>
18#include <linux/err.h>
19#include <linux/list.h>
20#include <linux/slab.h>
21#include <linux/of.h>
22#include <linux/device.h>
23#include <linux/init.h>
24#include <linux/sched.h>
25#include <linux/clkdev.h>
26
27#include "clk.h"
28
29static DEFINE_SPINLOCK(enable_lock);
30static DEFINE_MUTEX(prepare_lock);
31
32static struct task_struct *prepare_owner;
33static struct task_struct *enable_owner;
34
35static int prepare_refcnt;
36static int enable_refcnt;
37
38static HLIST_HEAD(clk_root_list);
39static HLIST_HEAD(clk_orphan_list);
40static LIST_HEAD(clk_notifier_list);
41
42static struct hlist_head *all_lists[] = {
43 &clk_root_list,
44 &clk_orphan_list,
45 NULL,
46};
47
48static struct hlist_head *orphan_list[] = {
49 &clk_orphan_list,
50 NULL,
51};
52
53/*** private data structures ***/
54
55struct clk_core {
56 const char *name;
57 const struct clk_ops *ops;
58 struct clk_hw *hw;
59 struct module *owner;
60 struct clk_core *parent;
61 const char **parent_names;
62 struct clk_core **parents;
63 u8 num_parents;
64 u8 new_parent_index;
65 unsigned long rate;
66 unsigned long req_rate;
67 unsigned long new_rate;
68 struct clk_core *new_parent;
69 struct clk_core *new_child;
70 unsigned long flags;
71 bool orphan;
72 unsigned int enable_count;
73 unsigned int prepare_count;
74 unsigned long min_rate;
75 unsigned long max_rate;
76 unsigned long accuracy;
77 int phase;
78 struct hlist_head children;
79 struct hlist_node child_node;
80 struct hlist_head clks;
81 unsigned int notifier_count;
82#ifdef CONFIG_DEBUG_FS
83 struct dentry *dentry;
84 struct hlist_node debug_node;
85#endif
86 struct kref ref;
87};
88
89#define CREATE_TRACE_POINTS
90#include <trace/events/clk.h>
91
92struct clk {
93 struct clk_core *core;
94 const char *dev_id;
95 const char *con_id;
96 unsigned long min_rate;
97 unsigned long max_rate;
98 struct hlist_node clks_node;
99};
100
101/*** locking ***/
102static void clk_prepare_lock(void)
103{
104 if (!mutex_trylock(&prepare_lock)) {
105 if (prepare_owner == current) {
106 prepare_refcnt++;
107 return;
108 }
109 mutex_lock(&prepare_lock);
110 }
111 WARN_ON_ONCE(prepare_owner != NULL);
112 WARN_ON_ONCE(prepare_refcnt != 0);
113 prepare_owner = current;
114 prepare_refcnt = 1;
115}
116
117static void clk_prepare_unlock(void)
118{
119 WARN_ON_ONCE(prepare_owner != current);
120 WARN_ON_ONCE(prepare_refcnt == 0);
121
122 if (--prepare_refcnt)
123 return;
124 prepare_owner = NULL;
125 mutex_unlock(&prepare_lock);
126}
127
128static unsigned long clk_enable_lock(void)
129 __acquires(enable_lock)
130{
131 unsigned long flags;
132
133 if (!spin_trylock_irqsave(&enable_lock, flags)) {
134 if (enable_owner == current) {
135 enable_refcnt++;
136 __acquire(enable_lock);
137 return flags;
138 }
139 spin_lock_irqsave(&enable_lock, flags);
140 }
141 WARN_ON_ONCE(enable_owner != NULL);
142 WARN_ON_ONCE(enable_refcnt != 0);
143 enable_owner = current;
144 enable_refcnt = 1;
145 return flags;
146}
147
148static void clk_enable_unlock(unsigned long flags)
149 __releases(enable_lock)
150{
151 WARN_ON_ONCE(enable_owner != current);
152 WARN_ON_ONCE(enable_refcnt == 0);
153
154 if (--enable_refcnt) {
155 __release(enable_lock);
156 return;
157 }
158 enable_owner = NULL;
159 spin_unlock_irqrestore(&enable_lock, flags);
160}
161
162static bool clk_core_is_prepared(struct clk_core *core)
163{
164 /*
165 * .is_prepared is optional for clocks that can prepare
166 * fall back to software usage counter if it is missing
167 */
168 if (!core->ops->is_prepared)
169 return core->prepare_count;
170
171 return core->ops->is_prepared(core->hw);
172}
173
174static bool clk_core_is_enabled(struct clk_core *core)
175{
176 /*
177 * .is_enabled is only mandatory for clocks that gate
178 * fall back to software usage counter if .is_enabled is missing
179 */
180 if (!core->ops->is_enabled)
181 return core->enable_count;
182
183 return core->ops->is_enabled(core->hw);
184}
185
186/*** helper functions ***/
187
188const char *__clk_get_name(const struct clk *clk)
189{
190 return !clk ? NULL : clk->core->name;
191}
192EXPORT_SYMBOL_GPL(__clk_get_name);
193
194const char *clk_hw_get_name(const struct clk_hw *hw)
195{
196 return hw->core->name;
197}
198EXPORT_SYMBOL_GPL(clk_hw_get_name);
199
200struct clk_hw *__clk_get_hw(struct clk *clk)
201{
202 return !clk ? NULL : clk->core->hw;
203}
204EXPORT_SYMBOL_GPL(__clk_get_hw);
205
206unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
207{
208 return hw->core->num_parents;
209}
210EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
211
212struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
213{
214 return hw->core->parent ? hw->core->parent->hw : NULL;
215}
216EXPORT_SYMBOL_GPL(clk_hw_get_parent);
217
218static struct clk_core *__clk_lookup_subtree(const char *name,
219 struct clk_core *core)
220{
221 struct clk_core *child;
222 struct clk_core *ret;
223
224 if (!strcmp(core->name, name))
225 return core;
226
227 hlist_for_each_entry(child, &core->children, child_node) {
228 ret = __clk_lookup_subtree(name, child);
229 if (ret)
230 return ret;
231 }
232
233 return NULL;
234}
235
236static struct clk_core *clk_core_lookup(const char *name)
237{
238 struct clk_core *root_clk;
239 struct clk_core *ret;
240
241 if (!name)
242 return NULL;
243
244 /* search the 'proper' clk tree first */
245 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
246 ret = __clk_lookup_subtree(name, root_clk);
247 if (ret)
248 return ret;
249 }
250
251 /* if not found, then search the orphan tree */
252 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
253 ret = __clk_lookup_subtree(name, root_clk);
254 if (ret)
255 return ret;
256 }
257
258 return NULL;
259}
260
261static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
262 u8 index)
263{
264 if (!core || index >= core->num_parents)
265 return NULL;
266
267 if (!core->parents[index])
268 core->parents[index] =
269 clk_core_lookup(core->parent_names[index]);
270
271 return core->parents[index];
272}
273
274struct clk_hw *
275clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
276{
277 struct clk_core *parent;
278
279 parent = clk_core_get_parent_by_index(hw->core, index);
280
281 return !parent ? NULL : parent->hw;
282}
283EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
284
285unsigned int __clk_get_enable_count(struct clk *clk)
286{
287 return !clk ? 0 : clk->core->enable_count;
288}
289
290static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
291{
292 unsigned long ret;
293
294 if (!core) {
295 ret = 0;
296 goto out;
297 }
298
299 ret = core->rate;
300
301 if (!core->num_parents)
302 goto out;
303
304 if (!core->parent)
305 ret = 0;
306
307out:
308 return ret;
309}
310
311unsigned long clk_hw_get_rate(const struct clk_hw *hw)
312{
313 return clk_core_get_rate_nolock(hw->core);
314}
315EXPORT_SYMBOL_GPL(clk_hw_get_rate);
316
317static unsigned long __clk_get_accuracy(struct clk_core *core)
318{
319 if (!core)
320 return 0;
321
322 return core->accuracy;
323}
324
325unsigned long __clk_get_flags(struct clk *clk)
326{
327 return !clk ? 0 : clk->core->flags;
328}
329EXPORT_SYMBOL_GPL(__clk_get_flags);
330
331unsigned long clk_hw_get_flags(const struct clk_hw *hw)
332{
333 return hw->core->flags;
334}
335EXPORT_SYMBOL_GPL(clk_hw_get_flags);
336
337bool clk_hw_is_prepared(const struct clk_hw *hw)
338{
339 return clk_core_is_prepared(hw->core);
340}
341
342bool clk_hw_is_enabled(const struct clk_hw *hw)
343{
344 return clk_core_is_enabled(hw->core);
345}
346
347bool __clk_is_enabled(struct clk *clk)
348{
349 if (!clk)
350 return false;
351
352 return clk_core_is_enabled(clk->core);
353}
354EXPORT_SYMBOL_GPL(__clk_is_enabled);
355
356static bool mux_is_better_rate(unsigned long rate, unsigned long now,
357 unsigned long best, unsigned long flags)
358{
359 if (flags & CLK_MUX_ROUND_CLOSEST)
360 return abs(now - rate) < abs(best - rate);
361
362 return now <= rate && now > best;
363}
364
365int clk_mux_determine_rate_flags(struct clk_hw *hw,
366 struct clk_rate_request *req,
367 unsigned long flags)
368{
369 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
370 int i, num_parents, ret;
371 unsigned long best = 0;
372 struct clk_rate_request parent_req = *req;
373
374 /* if NO_REPARENT flag set, pass through to current parent */
375 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
376 parent = core->parent;
377 if (core->flags & CLK_SET_RATE_PARENT) {
378 ret = __clk_determine_rate(parent ? parent->hw : NULL,
379 &parent_req);
380 if (ret)
381 return ret;
382
383 best = parent_req.rate;
384 } else if (parent) {
385 best = clk_core_get_rate_nolock(parent);
386 } else {
387 best = clk_core_get_rate_nolock(core);
388 }
389
390 goto out;
391 }
392
393 /* find the parent that can provide the fastest rate <= rate */
394 num_parents = core->num_parents;
395 for (i = 0; i < num_parents; i++) {
396 parent = clk_core_get_parent_by_index(core, i);
397 if (!parent)
398 continue;
399
400 if (core->flags & CLK_SET_RATE_PARENT) {
401 parent_req = *req;
402 ret = __clk_determine_rate(parent->hw, &parent_req);
403 if (ret)
404 continue;
405 } else {
406 parent_req.rate = clk_core_get_rate_nolock(parent);
407 }
408
409 if (mux_is_better_rate(req->rate, parent_req.rate,
410 best, flags)) {
411 best_parent = parent;
412 best = parent_req.rate;
413 }
414 }
415
416 if (!best_parent)
417 return -EINVAL;
418
419out:
420 if (best_parent)
421 req->best_parent_hw = best_parent->hw;
422 req->best_parent_rate = best;
423 req->rate = best;
424
425 return 0;
426}
427EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
428
429struct clk *__clk_lookup(const char *name)
430{
431 struct clk_core *core = clk_core_lookup(name);
432
433 return !core ? NULL : core->hw->clk;
434}
435
436static void clk_core_get_boundaries(struct clk_core *core,
437 unsigned long *min_rate,
438 unsigned long *max_rate)
439{
440 struct clk *clk_user;
441
442 *min_rate = core->min_rate;
443 *max_rate = core->max_rate;
444
445 hlist_for_each_entry(clk_user, &core->clks, clks_node)
446 *min_rate = max(*min_rate, clk_user->min_rate);
447
448 hlist_for_each_entry(clk_user, &core->clks, clks_node)
449 *max_rate = min(*max_rate, clk_user->max_rate);
450}
451
452void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
453 unsigned long max_rate)
454{
455 hw->core->min_rate = min_rate;
456 hw->core->max_rate = max_rate;
457}
458EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
459
460/*
461 * Helper for finding best parent to provide a given frequency. This can be used
462 * directly as a determine_rate callback (e.g. for a mux), or from a more
463 * complex clock that may combine a mux with other operations.
464 */
465int __clk_mux_determine_rate(struct clk_hw *hw,
466 struct clk_rate_request *req)
467{
468 return clk_mux_determine_rate_flags(hw, req, 0);
469}
470EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
471
472int __clk_mux_determine_rate_closest(struct clk_hw *hw,
473 struct clk_rate_request *req)
474{
475 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
476}
477EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
478
479/*** clk api ***/
480
481static void clk_core_unprepare(struct clk_core *core)
482{
483 lockdep_assert_held(&prepare_lock);
484
485 if (!core)
486 return;
487
488 if (WARN_ON(core->prepare_count == 0))
489 return;
490
491 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
492 return;
493
494 if (--core->prepare_count > 0)
495 return;
496
497 WARN_ON(core->enable_count > 0);
498
499 trace_clk_unprepare(core);
500
501 if (core->ops->unprepare)
502 core->ops->unprepare(core->hw);
503
504 trace_clk_unprepare_complete(core);
505 clk_core_unprepare(core->parent);
506}
507
508static void clk_core_unprepare_lock(struct clk_core *core)
509{
510 clk_prepare_lock();
511 clk_core_unprepare(core);
512 clk_prepare_unlock();
513}
514
515/**
516 * clk_unprepare - undo preparation of a clock source
517 * @clk: the clk being unprepared
518 *
519 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
520 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
521 * if the operation may sleep. One example is a clk which is accessed over
522 * I2c. In the complex case a clk gate operation may require a fast and a slow
523 * part. It is this reason that clk_unprepare and clk_disable are not mutually
524 * exclusive. In fact clk_disable must be called before clk_unprepare.
525 */
526void clk_unprepare(struct clk *clk)
527{
528 if (IS_ERR_OR_NULL(clk))
529 return;
530
531 clk_core_unprepare_lock(clk->core);
532}
533EXPORT_SYMBOL_GPL(clk_unprepare);
534
535static int clk_core_prepare(struct clk_core *core)
536{
537 int ret = 0;
538
539 lockdep_assert_held(&prepare_lock);
540
541 if (!core)
542 return 0;
543
544 if (core->prepare_count == 0) {
545 ret = clk_core_prepare(core->parent);
546 if (ret)
547 return ret;
548
549 trace_clk_prepare(core);
550
551 if (core->ops->prepare)
552 ret = core->ops->prepare(core->hw);
553
554 trace_clk_prepare_complete(core);
555
556 if (ret) {
557 clk_core_unprepare(core->parent);
558 return ret;
559 }
560 }
561
562 core->prepare_count++;
563
564 return 0;
565}
566
567static int clk_core_prepare_lock(struct clk_core *core)
568{
569 int ret;
570
571 clk_prepare_lock();
572 ret = clk_core_prepare(core);
573 clk_prepare_unlock();
574
575 return ret;
576}
577
578/**
579 * clk_prepare - prepare a clock source
580 * @clk: the clk being prepared
581 *
582 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
583 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
584 * operation may sleep. One example is a clk which is accessed over I2c. In
585 * the complex case a clk ungate operation may require a fast and a slow part.
586 * It is this reason that clk_prepare and clk_enable are not mutually
587 * exclusive. In fact clk_prepare must be called before clk_enable.
588 * Returns 0 on success, -EERROR otherwise.
589 */
590int clk_prepare(struct clk *clk)
591{
592 if (!clk)
593 return 0;
594
595 return clk_core_prepare_lock(clk->core);
596}
597EXPORT_SYMBOL_GPL(clk_prepare);
598
599static void clk_core_disable(struct clk_core *core)
600{
601 lockdep_assert_held(&enable_lock);
602
603 if (!core)
604 return;
605
606 if (WARN_ON(core->enable_count == 0))
607 return;
608
609 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
610 return;
611
612 if (--core->enable_count > 0)
613 return;
614
615 trace_clk_disable_rcuidle(core);
616
617 if (core->ops->disable)
618 core->ops->disable(core->hw);
619
620 trace_clk_disable_complete_rcuidle(core);
621
622 clk_core_disable(core->parent);
623}
624
625static void clk_core_disable_lock(struct clk_core *core)
626{
627 unsigned long flags;
628
629 flags = clk_enable_lock();
630 clk_core_disable(core);
631 clk_enable_unlock(flags);
632}
633
634/**
635 * clk_disable - gate a clock
636 * @clk: the clk being gated
637 *
638 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
639 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
640 * clk if the operation is fast and will never sleep. One example is a
641 * SoC-internal clk which is controlled via simple register writes. In the
642 * complex case a clk gate operation may require a fast and a slow part. It is
643 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
644 * In fact clk_disable must be called before clk_unprepare.
645 */
646void clk_disable(struct clk *clk)
647{
648 if (IS_ERR_OR_NULL(clk))
649 return;
650
651 clk_core_disable_lock(clk->core);
652}
653EXPORT_SYMBOL_GPL(clk_disable);
654
655static int clk_core_enable(struct clk_core *core)
656{
657 int ret = 0;
658
659 lockdep_assert_held(&enable_lock);
660
661 if (!core)
662 return 0;
663
664 if (WARN_ON(core->prepare_count == 0))
665 return -ESHUTDOWN;
666
667 if (core->enable_count == 0) {
668 ret = clk_core_enable(core->parent);
669
670 if (ret)
671 return ret;
672
673 trace_clk_enable_rcuidle(core);
674
675 if (core->ops->enable)
676 ret = core->ops->enable(core->hw);
677
678 trace_clk_enable_complete_rcuidle(core);
679
680 if (ret) {
681 clk_core_disable(core->parent);
682 return ret;
683 }
684 }
685
686 core->enable_count++;
687 return 0;
688}
689
690static int clk_core_enable_lock(struct clk_core *core)
691{
692 unsigned long flags;
693 int ret;
694
695 flags = clk_enable_lock();
696 ret = clk_core_enable(core);
697 clk_enable_unlock(flags);
698
699 return ret;
700}
701
702/**
703 * clk_enable - ungate a clock
704 * @clk: the clk being ungated
705 *
706 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
707 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
708 * if the operation will never sleep. One example is a SoC-internal clk which
709 * is controlled via simple register writes. In the complex case a clk ungate
710 * operation may require a fast and a slow part. It is this reason that
711 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
712 * must be called before clk_enable. Returns 0 on success, -EERROR
713 * otherwise.
714 */
715int clk_enable(struct clk *clk)
716{
717 if (!clk)
718 return 0;
719
720 return clk_core_enable_lock(clk->core);
721}
722EXPORT_SYMBOL_GPL(clk_enable);
723
724static int clk_core_prepare_enable(struct clk_core *core)
725{
726 int ret;
727
728 ret = clk_core_prepare_lock(core);
729 if (ret)
730 return ret;
731
732 ret = clk_core_enable_lock(core);
733 if (ret)
734 clk_core_unprepare_lock(core);
735
736 return ret;
737}
738
739static void clk_core_disable_unprepare(struct clk_core *core)
740{
741 clk_core_disable_lock(core);
742 clk_core_unprepare_lock(core);
743}
744
745static void clk_unprepare_unused_subtree(struct clk_core *core)
746{
747 struct clk_core *child;
748
749 lockdep_assert_held(&prepare_lock);
750
751 hlist_for_each_entry(child, &core->children, child_node)
752 clk_unprepare_unused_subtree(child);
753
754 if (core->prepare_count)
755 return;
756
757 if (core->flags & CLK_IGNORE_UNUSED)
758 return;
759
760 if (clk_core_is_prepared(core)) {
761 trace_clk_unprepare(core);
762 if (core->ops->unprepare_unused)
763 core->ops->unprepare_unused(core->hw);
764 else if (core->ops->unprepare)
765 core->ops->unprepare(core->hw);
766 trace_clk_unprepare_complete(core);
767 }
768}
769
770static void clk_disable_unused_subtree(struct clk_core *core)
771{
772 struct clk_core *child;
773 unsigned long flags;
774
775 lockdep_assert_held(&prepare_lock);
776
777 hlist_for_each_entry(child, &core->children, child_node)
778 clk_disable_unused_subtree(child);
779
780 if (core->flags & CLK_OPS_PARENT_ENABLE)
781 clk_core_prepare_enable(core->parent);
782
783 flags = clk_enable_lock();
784
785 if (core->enable_count)
786 goto unlock_out;
787
788 if (core->flags & CLK_IGNORE_UNUSED)
789 goto unlock_out;
790
791 /*
792 * some gate clocks have special needs during the disable-unused
793 * sequence. call .disable_unused if available, otherwise fall
794 * back to .disable
795 */
796 if (clk_core_is_enabled(core)) {
797 trace_clk_disable(core);
798 if (core->ops->disable_unused)
799 core->ops->disable_unused(core->hw);
800 else if (core->ops->disable)
801 core->ops->disable(core->hw);
802 trace_clk_disable_complete(core);
803 }
804
805unlock_out:
806 clk_enable_unlock(flags);
807 if (core->flags & CLK_OPS_PARENT_ENABLE)
808 clk_core_disable_unprepare(core->parent);
809}
810
811static bool clk_ignore_unused;
812static int __init clk_ignore_unused_setup(char *__unused)
813{
814 clk_ignore_unused = true;
815 return 1;
816}
817__setup("clk_ignore_unused", clk_ignore_unused_setup);
818
819static int clk_disable_unused(void)
820{
821 struct clk_core *core;
822
823 if (clk_ignore_unused) {
824 pr_warn("clk: Not disabling unused clocks\n");
825 return 0;
826 }
827
828 clk_prepare_lock();
829
830 hlist_for_each_entry(core, &clk_root_list, child_node)
831 clk_disable_unused_subtree(core);
832
833 hlist_for_each_entry(core, &clk_orphan_list, child_node)
834 clk_disable_unused_subtree(core);
835
836 hlist_for_each_entry(core, &clk_root_list, child_node)
837 clk_unprepare_unused_subtree(core);
838
839 hlist_for_each_entry(core, &clk_orphan_list, child_node)
840 clk_unprepare_unused_subtree(core);
841
842 clk_prepare_unlock();
843
844 return 0;
845}
846late_initcall_sync(clk_disable_unused);
847
848static int clk_core_round_rate_nolock(struct clk_core *core,
849 struct clk_rate_request *req)
850{
851 struct clk_core *parent;
852 long rate;
853
854 lockdep_assert_held(&prepare_lock);
855
856 if (!core)
857 return 0;
858
859 parent = core->parent;
860 if (parent) {
861 req->best_parent_hw = parent->hw;
862 req->best_parent_rate = parent->rate;
863 } else {
864 req->best_parent_hw = NULL;
865 req->best_parent_rate = 0;
866 }
867
868 if (core->ops->determine_rate) {
869 return core->ops->determine_rate(core->hw, req);
870 } else if (core->ops->round_rate) {
871 rate = core->ops->round_rate(core->hw, req->rate,
872 &req->best_parent_rate);
873 if (rate < 0)
874 return rate;
875
876 req->rate = rate;
877 } else if (core->flags & CLK_SET_RATE_PARENT) {
878 return clk_core_round_rate_nolock(parent, req);
879 } else {
880 req->rate = core->rate;
881 }
882
883 return 0;
884}
885
886/**
887 * __clk_determine_rate - get the closest rate actually supported by a clock
888 * @hw: determine the rate of this clock
889 * @req: target rate request
890 *
891 * Useful for clk_ops such as .set_rate and .determine_rate.
892 */
893int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
894{
895 if (!hw) {
896 req->rate = 0;
897 return 0;
898 }
899
900 return clk_core_round_rate_nolock(hw->core, req);
901}
902EXPORT_SYMBOL_GPL(__clk_determine_rate);
903
904unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
905{
906 int ret;
907 struct clk_rate_request req;
908
909 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
910 req.rate = rate;
911
912 ret = clk_core_round_rate_nolock(hw->core, &req);
913 if (ret)
914 return 0;
915
916 return req.rate;
917}
918EXPORT_SYMBOL_GPL(clk_hw_round_rate);
919
920/**
921 * clk_round_rate - round the given rate for a clk
922 * @clk: the clk for which we are rounding a rate
923 * @rate: the rate which is to be rounded
924 *
925 * Takes in a rate as input and rounds it to a rate that the clk can actually
926 * use which is then returned. If clk doesn't support round_rate operation
927 * then the parent rate is returned.
928 */
929long clk_round_rate(struct clk *clk, unsigned long rate)
930{
931 struct clk_rate_request req;
932 int ret;
933
934 if (!clk)
935 return 0;
936
937 clk_prepare_lock();
938
939 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
940 req.rate = rate;
941
942 ret = clk_core_round_rate_nolock(clk->core, &req);
943 clk_prepare_unlock();
944
945 if (ret)
946 return ret;
947
948 return req.rate;
949}
950EXPORT_SYMBOL_GPL(clk_round_rate);
951
952/**
953 * __clk_notify - call clk notifier chain
954 * @core: clk that is changing rate
955 * @msg: clk notifier type (see include/linux/clk.h)
956 * @old_rate: old clk rate
957 * @new_rate: new clk rate
958 *
959 * Triggers a notifier call chain on the clk rate-change notification
960 * for 'clk'. Passes a pointer to the struct clk and the previous
961 * and current rates to the notifier callback. Intended to be called by
962 * internal clock code only. Returns NOTIFY_DONE from the last driver
963 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
964 * a driver returns that.
965 */
966static int __clk_notify(struct clk_core *core, unsigned long msg,
967 unsigned long old_rate, unsigned long new_rate)
968{
969 struct clk_notifier *cn;
970 struct clk_notifier_data cnd;
971 int ret = NOTIFY_DONE;
972
973 cnd.old_rate = old_rate;
974 cnd.new_rate = new_rate;
975
976 list_for_each_entry(cn, &clk_notifier_list, node) {
977 if (cn->clk->core == core) {
978 cnd.clk = cn->clk;
979 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
980 &cnd);
981 if (ret & NOTIFY_STOP_MASK)
982 return ret;
983 }
984 }
985
986 return ret;
987}
988
989/**
990 * __clk_recalc_accuracies
991 * @core: first clk in the subtree
992 *
993 * Walks the subtree of clks starting with clk and recalculates accuracies as
994 * it goes. Note that if a clk does not implement the .recalc_accuracy
995 * callback then it is assumed that the clock will take on the accuracy of its
996 * parent.
997 */
998static void __clk_recalc_accuracies(struct clk_core *core)
999{
1000 unsigned long parent_accuracy = 0;
1001 struct clk_core *child;
1002
1003 lockdep_assert_held(&prepare_lock);
1004
1005 if (core->parent)
1006 parent_accuracy = core->parent->accuracy;
1007
1008 if (core->ops->recalc_accuracy)
1009 core->accuracy = core->ops->recalc_accuracy(core->hw,
1010 parent_accuracy);
1011 else
1012 core->accuracy = parent_accuracy;
1013
1014 hlist_for_each_entry(child, &core->children, child_node)
1015 __clk_recalc_accuracies(child);
1016}
1017
1018static long clk_core_get_accuracy(struct clk_core *core)
1019{
1020 unsigned long accuracy;
1021
1022 clk_prepare_lock();
1023 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1024 __clk_recalc_accuracies(core);
1025
1026 accuracy = __clk_get_accuracy(core);
1027 clk_prepare_unlock();
1028
1029 return accuracy;
1030}
1031
1032/**
1033 * clk_get_accuracy - return the accuracy of clk
1034 * @clk: the clk whose accuracy is being returned
1035 *
1036 * Simply returns the cached accuracy of the clk, unless
1037 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1038 * issued.
1039 * If clk is NULL then returns 0.
1040 */
1041long clk_get_accuracy(struct clk *clk)
1042{
1043 if (!clk)
1044 return 0;
1045
1046 return clk_core_get_accuracy(clk->core);
1047}
1048EXPORT_SYMBOL_GPL(clk_get_accuracy);
1049
1050static unsigned long clk_recalc(struct clk_core *core,
1051 unsigned long parent_rate)
1052{
1053 if (core->ops->recalc_rate)
1054 return core->ops->recalc_rate(core->hw, parent_rate);
1055 return parent_rate;
1056}
1057
1058/**
1059 * __clk_recalc_rates
1060 * @core: first clk in the subtree
1061 * @msg: notification type (see include/linux/clk.h)
1062 *
1063 * Walks the subtree of clks starting with clk and recalculates rates as it
1064 * goes. Note that if a clk does not implement the .recalc_rate callback then
1065 * it is assumed that the clock will take on the rate of its parent.
1066 *
1067 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1068 * if necessary.
1069 */
1070static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1071{
1072 unsigned long old_rate;
1073 unsigned long parent_rate = 0;
1074 struct clk_core *child;
1075
1076 lockdep_assert_held(&prepare_lock);
1077
1078 old_rate = core->rate;
1079
1080 if (core->parent)
1081 parent_rate = core->parent->rate;
1082
1083 core->rate = clk_recalc(core, parent_rate);
1084
1085 /*
1086 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1087 * & ABORT_RATE_CHANGE notifiers
1088 */
1089 if (core->notifier_count && msg)
1090 __clk_notify(core, msg, old_rate, core->rate);
1091
1092 hlist_for_each_entry(child, &core->children, child_node)
1093 __clk_recalc_rates(child, msg);
1094}
1095
1096static unsigned long clk_core_get_rate(struct clk_core *core)
1097{
1098 unsigned long rate;
1099
1100 clk_prepare_lock();
1101
1102 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1103 __clk_recalc_rates(core, 0);
1104
1105 rate = clk_core_get_rate_nolock(core);
1106 clk_prepare_unlock();
1107
1108 return rate;
1109}
1110
1111/**
1112 * clk_get_rate - return the rate of clk
1113 * @clk: the clk whose rate is being returned
1114 *
1115 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1116 * is set, which means a recalc_rate will be issued.
1117 * If clk is NULL then returns 0.
1118 */
1119unsigned long clk_get_rate(struct clk *clk)
1120{
1121 if (!clk)
1122 return 0;
1123
1124 return clk_core_get_rate(clk->core);
1125}
1126EXPORT_SYMBOL_GPL(clk_get_rate);
1127
1128static int clk_fetch_parent_index(struct clk_core *core,
1129 struct clk_core *parent)
1130{
1131 int i;
1132
1133 if (!parent)
1134 return -EINVAL;
1135
1136 for (i = 0; i < core->num_parents; i++)
1137 if (clk_core_get_parent_by_index(core, i) == parent)
1138 return i;
1139
1140 return -EINVAL;
1141}
1142
1143/*
1144 * Update the orphan status of @core and all its children.
1145 */
1146static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1147{
1148 struct clk_core *child;
1149
1150 core->orphan = is_orphan;
1151
1152 hlist_for_each_entry(child, &core->children, child_node)
1153 clk_core_update_orphan_status(child, is_orphan);
1154}
1155
1156static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1157{
1158 bool was_orphan = core->orphan;
1159
1160 hlist_del(&core->child_node);
1161
1162 if (new_parent) {
1163 bool becomes_orphan = new_parent->orphan;
1164
1165 /* avoid duplicate POST_RATE_CHANGE notifications */
1166 if (new_parent->new_child == core)
1167 new_parent->new_child = NULL;
1168
1169 hlist_add_head(&core->child_node, &new_parent->children);
1170
1171 if (was_orphan != becomes_orphan)
1172 clk_core_update_orphan_status(core, becomes_orphan);
1173 } else {
1174 hlist_add_head(&core->child_node, &clk_orphan_list);
1175 if (!was_orphan)
1176 clk_core_update_orphan_status(core, true);
1177 }
1178
1179 core->parent = new_parent;
1180}
1181
1182static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1183 struct clk_core *parent)
1184{
1185 unsigned long flags;
1186 struct clk_core *old_parent = core->parent;
1187
1188 /*
1189 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1190 *
1191 * 2. Migrate prepare state between parents and prevent race with
1192 * clk_enable().
1193 *
1194 * If the clock is not prepared, then a race with
1195 * clk_enable/disable() is impossible since we already have the
1196 * prepare lock (future calls to clk_enable() need to be preceded by
1197 * a clk_prepare()).
1198 *
1199 * If the clock is prepared, migrate the prepared state to the new
1200 * parent and also protect against a race with clk_enable() by
1201 * forcing the clock and the new parent on. This ensures that all
1202 * future calls to clk_enable() are practically NOPs with respect to
1203 * hardware and software states.
1204 *
1205 * See also: Comment for clk_set_parent() below.
1206 */
1207
1208 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1209 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1210 clk_core_prepare_enable(old_parent);
1211 clk_core_prepare_enable(parent);
1212 }
1213
1214 /* migrate prepare count if > 0 */
1215 if (core->prepare_count) {
1216 clk_core_prepare_enable(parent);
1217 clk_core_enable_lock(core);
1218 }
1219
1220 /* update the clk tree topology */
1221 flags = clk_enable_lock();
1222 clk_reparent(core, parent);
1223 clk_enable_unlock(flags);
1224
1225 return old_parent;
1226}
1227
1228static void __clk_set_parent_after(struct clk_core *core,
1229 struct clk_core *parent,
1230 struct clk_core *old_parent)
1231{
1232 /*
1233 * Finish the migration of prepare state and undo the changes done
1234 * for preventing a race with clk_enable().
1235 */
1236 if (core->prepare_count) {
1237 clk_core_disable_lock(core);
1238 clk_core_disable_unprepare(old_parent);
1239 }
1240
1241 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1242 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1243 clk_core_disable_unprepare(parent);
1244 clk_core_disable_unprepare(old_parent);
1245 }
1246}
1247
1248static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1249 u8 p_index)
1250{
1251 unsigned long flags;
1252 int ret = 0;
1253 struct clk_core *old_parent;
1254
1255 old_parent = __clk_set_parent_before(core, parent);
1256
1257 trace_clk_set_parent(core, parent);
1258
1259 /* change clock input source */
1260 if (parent && core->ops->set_parent)
1261 ret = core->ops->set_parent(core->hw, p_index);
1262
1263 trace_clk_set_parent_complete(core, parent);
1264
1265 if (ret) {
1266 flags = clk_enable_lock();
1267 clk_reparent(core, old_parent);
1268 clk_enable_unlock(flags);
1269 __clk_set_parent_after(core, old_parent, parent);
1270
1271 return ret;
1272 }
1273
1274 __clk_set_parent_after(core, parent, old_parent);
1275
1276 return 0;
1277}
1278
1279/**
1280 * __clk_speculate_rates
1281 * @core: first clk in the subtree
1282 * @parent_rate: the "future" rate of clk's parent
1283 *
1284 * Walks the subtree of clks starting with clk, speculating rates as it
1285 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1286 *
1287 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1288 * pre-rate change notifications and returns early if no clks in the
1289 * subtree have subscribed to the notifications. Note that if a clk does not
1290 * implement the .recalc_rate callback then it is assumed that the clock will
1291 * take on the rate of its parent.
1292 */
1293static int __clk_speculate_rates(struct clk_core *core,
1294 unsigned long parent_rate)
1295{
1296 struct clk_core *child;
1297 unsigned long new_rate;
1298 int ret = NOTIFY_DONE;
1299
1300 lockdep_assert_held(&prepare_lock);
1301
1302 new_rate = clk_recalc(core, parent_rate);
1303
1304 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1305 if (core->notifier_count)
1306 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1307
1308 if (ret & NOTIFY_STOP_MASK) {
1309 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1310 __func__, core->name, ret);
1311 goto out;
1312 }
1313
1314 hlist_for_each_entry(child, &core->children, child_node) {
1315 ret = __clk_speculate_rates(child, new_rate);
1316 if (ret & NOTIFY_STOP_MASK)
1317 break;
1318 }
1319
1320out:
1321 return ret;
1322}
1323
1324static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1325 struct clk_core *new_parent, u8 p_index)
1326{
1327 struct clk_core *child;
1328
1329 core->new_rate = new_rate;
1330 core->new_parent = new_parent;
1331 core->new_parent_index = p_index;
1332 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1333 core->new_child = NULL;
1334 if (new_parent && new_parent != core->parent)
1335 new_parent->new_child = core;
1336
1337 hlist_for_each_entry(child, &core->children, child_node) {
1338 child->new_rate = clk_recalc(child, new_rate);
1339 clk_calc_subtree(child, child->new_rate, NULL, 0);
1340 }
1341}
1342
1343/*
1344 * calculate the new rates returning the topmost clock that has to be
1345 * changed.
1346 */
1347static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1348 unsigned long rate)
1349{
1350 struct clk_core *top = core;
1351 struct clk_core *old_parent, *parent;
1352 unsigned long best_parent_rate = 0;
1353 unsigned long new_rate;
1354 unsigned long min_rate;
1355 unsigned long max_rate;
1356 int p_index = 0;
1357 long ret;
1358
1359 /* sanity */
1360 if (IS_ERR_OR_NULL(core))
1361 return NULL;
1362
1363 /* save parent rate, if it exists */
1364 parent = old_parent = core->parent;
1365 if (parent)
1366 best_parent_rate = parent->rate;
1367
1368 clk_core_get_boundaries(core, &min_rate, &max_rate);
1369
1370 /* find the closest rate and parent clk/rate */
1371 if (core->ops->determine_rate) {
1372 struct clk_rate_request req;
1373
1374 req.rate = rate;
1375 req.min_rate = min_rate;
1376 req.max_rate = max_rate;
1377 if (parent) {
1378 req.best_parent_hw = parent->hw;
1379 req.best_parent_rate = parent->rate;
1380 } else {
1381 req.best_parent_hw = NULL;
1382 req.best_parent_rate = 0;
1383 }
1384
1385 ret = core->ops->determine_rate(core->hw, &req);
1386 if (ret < 0)
1387 return NULL;
1388
1389 best_parent_rate = req.best_parent_rate;
1390 new_rate = req.rate;
1391 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1392 } else if (core->ops->round_rate) {
1393 ret = core->ops->round_rate(core->hw, rate,
1394 &best_parent_rate);
1395 if (ret < 0)
1396 return NULL;
1397
1398 new_rate = ret;
1399 if (new_rate < min_rate || new_rate > max_rate)
1400 return NULL;
1401 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1402 /* pass-through clock without adjustable parent */
1403 core->new_rate = core->rate;
1404 return NULL;
1405 } else {
1406 /* pass-through clock with adjustable parent */
1407 top = clk_calc_new_rates(parent, rate);
1408 new_rate = parent->new_rate;
1409 goto out;
1410 }
1411
1412 /* some clocks must be gated to change parent */
1413 if (parent != old_parent &&
1414 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1415 pr_debug("%s: %s not gated but wants to reparent\n",
1416 __func__, core->name);
1417 return NULL;
1418 }
1419
1420 /* try finding the new parent index */
1421 if (parent && core->num_parents > 1) {
1422 p_index = clk_fetch_parent_index(core, parent);
1423 if (p_index < 0) {
1424 pr_debug("%s: clk %s can not be parent of clk %s\n",
1425 __func__, parent->name, core->name);
1426 return NULL;
1427 }
1428 }
1429
1430 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1431 best_parent_rate != parent->rate)
1432 top = clk_calc_new_rates(parent, best_parent_rate);
1433
1434out:
1435 clk_calc_subtree(core, new_rate, parent, p_index);
1436
1437 return top;
1438}
1439
1440/*
1441 * Notify about rate changes in a subtree. Always walk down the whole tree
1442 * so that in case of an error we can walk down the whole tree again and
1443 * abort the change.
1444 */
1445static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
1446 unsigned long event)
1447{
1448 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
1449 int ret = NOTIFY_DONE;
1450
1451 if (core->rate == core->new_rate)
1452 return NULL;
1453
1454 if (core->notifier_count) {
1455 ret = __clk_notify(core, event, core->rate, core->new_rate);
1456 if (ret & NOTIFY_STOP_MASK)
1457 fail_clk = core;
1458 }
1459
1460 hlist_for_each_entry(child, &core->children, child_node) {
1461 /* Skip children who will be reparented to another clock */
1462 if (child->new_parent && child->new_parent != core)
1463 continue;
1464 tmp_clk = clk_propagate_rate_change(child, event);
1465 if (tmp_clk)
1466 fail_clk = tmp_clk;
1467 }
1468
1469 /* handle the new child who might not be in core->children yet */
1470 if (core->new_child) {
1471 tmp_clk = clk_propagate_rate_change(core->new_child, event);
1472 if (tmp_clk)
1473 fail_clk = tmp_clk;
1474 }
1475
1476 return fail_clk;
1477}
1478
1479/*
1480 * walk down a subtree and set the new rates notifying the rate
1481 * change on the way
1482 */
1483static void clk_change_rate(struct clk_core *core)
1484{
1485 struct clk_core *child;
1486 struct hlist_node *tmp;
1487 unsigned long old_rate;
1488 unsigned long best_parent_rate = 0;
1489 bool skip_set_rate = false;
1490 struct clk_core *old_parent;
1491 struct clk_core *parent = NULL;
1492
1493 old_rate = core->rate;
1494
1495 if (core->new_parent) {
1496 parent = core->new_parent;
1497 best_parent_rate = core->new_parent->rate;
1498 } else if (core->parent) {
1499 parent = core->parent;
1500 best_parent_rate = core->parent->rate;
1501 }
1502
1503 if (core->flags & CLK_SET_RATE_UNGATE) {
1504 unsigned long flags;
1505
1506 clk_core_prepare(core);
1507 flags = clk_enable_lock();
1508 clk_core_enable(core);
1509 clk_enable_unlock(flags);
1510 }
1511
1512 if (core->new_parent && core->new_parent != core->parent) {
1513 old_parent = __clk_set_parent_before(core, core->new_parent);
1514 trace_clk_set_parent(core, core->new_parent);
1515
1516 if (core->ops->set_rate_and_parent) {
1517 skip_set_rate = true;
1518 core->ops->set_rate_and_parent(core->hw, core->new_rate,
1519 best_parent_rate,
1520 core->new_parent_index);
1521 } else if (core->ops->set_parent) {
1522 core->ops->set_parent(core->hw, core->new_parent_index);
1523 }
1524
1525 trace_clk_set_parent_complete(core, core->new_parent);
1526 __clk_set_parent_after(core, core->new_parent, old_parent);
1527 }
1528
1529 if (core->flags & CLK_OPS_PARENT_ENABLE)
1530 clk_core_prepare_enable(parent);
1531
1532 trace_clk_set_rate(core, core->new_rate);
1533
1534 if (!skip_set_rate && core->ops->set_rate)
1535 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
1536
1537 trace_clk_set_rate_complete(core, core->new_rate);
1538
1539 core->rate = clk_recalc(core, best_parent_rate);
1540
1541 if (core->flags & CLK_SET_RATE_UNGATE) {
1542 unsigned long flags;
1543
1544 flags = clk_enable_lock();
1545 clk_core_disable(core);
1546 clk_enable_unlock(flags);
1547 clk_core_unprepare(core);
1548 }
1549
1550 if (core->flags & CLK_OPS_PARENT_ENABLE)
1551 clk_core_disable_unprepare(parent);
1552
1553 if (core->notifier_count && old_rate != core->rate)
1554 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
1555
1556 if (core->flags & CLK_RECALC_NEW_RATES)
1557 (void)clk_calc_new_rates(core, core->new_rate);
1558
1559 /*
1560 * Use safe iteration, as change_rate can actually swap parents
1561 * for certain clock types.
1562 */
1563 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
1564 /* Skip children who will be reparented to another clock */
1565 if (child->new_parent && child->new_parent != core)
1566 continue;
1567 clk_change_rate(child);
1568 }
1569
1570 /* handle the new child who might not be in core->children yet */
1571 if (core->new_child)
1572 clk_change_rate(core->new_child);
1573}
1574
1575static int clk_core_set_rate_nolock(struct clk_core *core,
1576 unsigned long req_rate)
1577{
1578 struct clk_core *top, *fail_clk;
1579 unsigned long rate = req_rate;
1580
1581 if (!core)
1582 return 0;
1583
1584 /* bail early if nothing to do */
1585 if (rate == clk_core_get_rate_nolock(core))
1586 return 0;
1587
1588 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
1589 return -EBUSY;
1590
1591 /* calculate new rates and get the topmost changed clock */
1592 top = clk_calc_new_rates(core, rate);
1593 if (!top)
1594 return -EINVAL;
1595
1596 /* notify that we are about to change rates */
1597 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1598 if (fail_clk) {
1599 pr_debug("%s: failed to set %s rate\n", __func__,
1600 fail_clk->name);
1601 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1602 return -EBUSY;
1603 }
1604
1605 /* change the rates */
1606 clk_change_rate(top);
1607
1608 core->req_rate = req_rate;
1609
1610 return 0;
1611}
1612
1613/**
1614 * clk_set_rate - specify a new rate for clk
1615 * @clk: the clk whose rate is being changed
1616 * @rate: the new rate for clk
1617 *
1618 * In the simplest case clk_set_rate will only adjust the rate of clk.
1619 *
1620 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1621 * propagate up to clk's parent; whether or not this happens depends on the
1622 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1623 * after calling .round_rate then upstream parent propagation is ignored. If
1624 * *parent_rate comes back with a new rate for clk's parent then we propagate
1625 * up to clk's parent and set its rate. Upward propagation will continue
1626 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1627 * .round_rate stops requesting changes to clk's parent_rate.
1628 *
1629 * Rate changes are accomplished via tree traversal that also recalculates the
1630 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
1631 *
1632 * Returns 0 on success, -EERROR otherwise.
1633 */
1634int clk_set_rate(struct clk *clk, unsigned long rate)
1635{
1636 int ret;
1637
1638 if (!clk)
1639 return 0;
1640
1641 /* prevent racing with updates to the clock topology */
1642 clk_prepare_lock();
1643
1644 ret = clk_core_set_rate_nolock(clk->core, rate);
1645
1646 clk_prepare_unlock();
1647
1648 return ret;
1649}
1650EXPORT_SYMBOL_GPL(clk_set_rate);
1651
1652/**
1653 * clk_set_rate_range - set a rate range for a clock source
1654 * @clk: clock source
1655 * @min: desired minimum clock rate in Hz, inclusive
1656 * @max: desired maximum clock rate in Hz, inclusive
1657 *
1658 * Returns success (0) or negative errno.
1659 */
1660int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1661{
1662 int ret = 0;
1663
1664 if (!clk)
1665 return 0;
1666
1667 if (min > max) {
1668 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1669 __func__, clk->core->name, clk->dev_id, clk->con_id,
1670 min, max);
1671 return -EINVAL;
1672 }
1673
1674 clk_prepare_lock();
1675
1676 if (min != clk->min_rate || max != clk->max_rate) {
1677 clk->min_rate = min;
1678 clk->max_rate = max;
1679 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1680 }
1681
1682 clk_prepare_unlock();
1683
1684 return ret;
1685}
1686EXPORT_SYMBOL_GPL(clk_set_rate_range);
1687
1688/**
1689 * clk_set_min_rate - set a minimum clock rate for a clock source
1690 * @clk: clock source
1691 * @rate: desired minimum clock rate in Hz, inclusive
1692 *
1693 * Returns success (0) or negative errno.
1694 */
1695int clk_set_min_rate(struct clk *clk, unsigned long rate)
1696{
1697 if (!clk)
1698 return 0;
1699
1700 return clk_set_rate_range(clk, rate, clk->max_rate);
1701}
1702EXPORT_SYMBOL_GPL(clk_set_min_rate);
1703
1704/**
1705 * clk_set_max_rate - set a maximum clock rate for a clock source
1706 * @clk: clock source
1707 * @rate: desired maximum clock rate in Hz, inclusive
1708 *
1709 * Returns success (0) or negative errno.
1710 */
1711int clk_set_max_rate(struct clk *clk, unsigned long rate)
1712{
1713 if (!clk)
1714 return 0;
1715
1716 return clk_set_rate_range(clk, clk->min_rate, rate);
1717}
1718EXPORT_SYMBOL_GPL(clk_set_max_rate);
1719
1720/**
1721 * clk_get_parent - return the parent of a clk
1722 * @clk: the clk whose parent gets returned
1723 *
1724 * Simply returns clk->parent. Returns NULL if clk is NULL.
1725 */
1726struct clk *clk_get_parent(struct clk *clk)
1727{
1728 struct clk *parent;
1729
1730 if (!clk)
1731 return NULL;
1732
1733 clk_prepare_lock();
1734 /* TODO: Create a per-user clk and change callers to call clk_put */
1735 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
1736 clk_prepare_unlock();
1737
1738 return parent;
1739}
1740EXPORT_SYMBOL_GPL(clk_get_parent);
1741
1742static struct clk_core *__clk_init_parent(struct clk_core *core)
1743{
1744 u8 index = 0;
1745
1746 if (core->num_parents > 1 && core->ops->get_parent)
1747 index = core->ops->get_parent(core->hw);
1748
1749 return clk_core_get_parent_by_index(core, index);
1750}
1751
1752static void clk_core_reparent(struct clk_core *core,
1753 struct clk_core *new_parent)
1754{
1755 clk_reparent(core, new_parent);
1756 __clk_recalc_accuracies(core);
1757 __clk_recalc_rates(core, POST_RATE_CHANGE);
1758}
1759
1760void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
1761{
1762 if (!hw)
1763 return;
1764
1765 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
1766}
1767
1768/**
1769 * clk_has_parent - check if a clock is a possible parent for another
1770 * @clk: clock source
1771 * @parent: parent clock source
1772 *
1773 * This function can be used in drivers that need to check that a clock can be
1774 * the parent of another without actually changing the parent.
1775 *
1776 * Returns true if @parent is a possible parent for @clk, false otherwise.
1777 */
1778bool clk_has_parent(struct clk *clk, struct clk *parent)
1779{
1780 struct clk_core *core, *parent_core;
1781 unsigned int i;
1782
1783 /* NULL clocks should be nops, so return success if either is NULL. */
1784 if (!clk || !parent)
1785 return true;
1786
1787 core = clk->core;
1788 parent_core = parent->core;
1789
1790 /* Optimize for the case where the parent is already the parent. */
1791 if (core->parent == parent_core)
1792 return true;
1793
1794 for (i = 0; i < core->num_parents; i++)
1795 if (strcmp(core->parent_names[i], parent_core->name) == 0)
1796 return true;
1797
1798 return false;
1799}
1800EXPORT_SYMBOL_GPL(clk_has_parent);
1801
1802static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
1803{
1804 int ret = 0;
1805 int p_index = 0;
1806 unsigned long p_rate = 0;
1807
1808 if (!core)
1809 return 0;
1810
1811 /* prevent racing with updates to the clock topology */
1812 clk_prepare_lock();
1813
1814 if (core->parent == parent)
1815 goto out;
1816
1817 /* verify ops for for multi-parent clks */
1818 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
1819 ret = -ENOSYS;
1820 goto out;
1821 }
1822
1823 /* check that we are allowed to re-parent if the clock is in use */
1824 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1825 ret = -EBUSY;
1826 goto out;
1827 }
1828
1829 /* try finding the new parent index */
1830 if (parent) {
1831 p_index = clk_fetch_parent_index(core, parent);
1832 if (p_index < 0) {
1833 pr_debug("%s: clk %s can not be parent of clk %s\n",
1834 __func__, parent->name, core->name);
1835 ret = p_index;
1836 goto out;
1837 }
1838 p_rate = parent->rate;
1839 }
1840
1841 /* propagate PRE_RATE_CHANGE notifications */
1842 ret = __clk_speculate_rates(core, p_rate);
1843
1844 /* abort if a driver objects */
1845 if (ret & NOTIFY_STOP_MASK)
1846 goto out;
1847
1848 /* do the re-parent */
1849 ret = __clk_set_parent(core, parent, p_index);
1850
1851 /* propagate rate an accuracy recalculation accordingly */
1852 if (ret) {
1853 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
1854 } else {
1855 __clk_recalc_rates(core, POST_RATE_CHANGE);
1856 __clk_recalc_accuracies(core);
1857 }
1858
1859out:
1860 clk_prepare_unlock();
1861
1862 return ret;
1863}
1864
1865/**
1866 * clk_set_parent - switch the parent of a mux clk
1867 * @clk: the mux clk whose input we are switching
1868 * @parent: the new input to clk
1869 *
1870 * Re-parent clk to use parent as its new input source. If clk is in
1871 * prepared state, the clk will get enabled for the duration of this call. If
1872 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1873 * that, the reparenting is glitchy in hardware, etc), use the
1874 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1875 *
1876 * After successfully changing clk's parent clk_set_parent will update the
1877 * clk topology, sysfs topology and propagate rate recalculation via
1878 * __clk_recalc_rates.
1879 *
1880 * Returns 0 on success, -EERROR otherwise.
1881 */
1882int clk_set_parent(struct clk *clk, struct clk *parent)
1883{
1884 if (!clk)
1885 return 0;
1886
1887 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1888}
1889EXPORT_SYMBOL_GPL(clk_set_parent);
1890
1891/**
1892 * clk_set_phase - adjust the phase shift of a clock signal
1893 * @clk: clock signal source
1894 * @degrees: number of degrees the signal is shifted
1895 *
1896 * Shifts the phase of a clock signal by the specified
1897 * degrees. Returns 0 on success, -EERROR otherwise.
1898 *
1899 * This function makes no distinction about the input or reference
1900 * signal that we adjust the clock signal phase against. For example
1901 * phase locked-loop clock signal generators we may shift phase with
1902 * respect to feedback clock signal input, but for other cases the
1903 * clock phase may be shifted with respect to some other, unspecified
1904 * signal.
1905 *
1906 * Additionally the concept of phase shift does not propagate through
1907 * the clock tree hierarchy, which sets it apart from clock rates and
1908 * clock accuracy. A parent clock phase attribute does not have an
1909 * impact on the phase attribute of a child clock.
1910 */
1911int clk_set_phase(struct clk *clk, int degrees)
1912{
1913 int ret = -EINVAL;
1914
1915 if (!clk)
1916 return 0;
1917
1918 /* sanity check degrees */
1919 degrees %= 360;
1920 if (degrees < 0)
1921 degrees += 360;
1922
1923 clk_prepare_lock();
1924
1925 trace_clk_set_phase(clk->core, degrees);
1926
1927 if (clk->core->ops->set_phase)
1928 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
1929
1930 trace_clk_set_phase_complete(clk->core, degrees);
1931
1932 if (!ret)
1933 clk->core->phase = degrees;
1934
1935 clk_prepare_unlock();
1936
1937 return ret;
1938}
1939EXPORT_SYMBOL_GPL(clk_set_phase);
1940
1941static int clk_core_get_phase(struct clk_core *core)
1942{
1943 int ret;
1944
1945 clk_prepare_lock();
1946 /* Always try to update cached phase if possible */
1947 if (core->ops->get_phase)
1948 core->phase = core->ops->get_phase(core->hw);
1949 ret = core->phase;
1950 clk_prepare_unlock();
1951
1952 return ret;
1953}
1954
1955/**
1956 * clk_get_phase - return the phase shift of a clock signal
1957 * @clk: clock signal source
1958 *
1959 * Returns the phase shift of a clock node in degrees, otherwise returns
1960 * -EERROR.
1961 */
1962int clk_get_phase(struct clk *clk)
1963{
1964 if (!clk)
1965 return 0;
1966
1967 return clk_core_get_phase(clk->core);
1968}
1969EXPORT_SYMBOL_GPL(clk_get_phase);
1970
1971/**
1972 * clk_is_match - check if two clk's point to the same hardware clock
1973 * @p: clk compared against q
1974 * @q: clk compared against p
1975 *
1976 * Returns true if the two struct clk pointers both point to the same hardware
1977 * clock node. Put differently, returns true if struct clk *p and struct clk *q
1978 * share the same struct clk_core object.
1979 *
1980 * Returns false otherwise. Note that two NULL clks are treated as matching.
1981 */
1982bool clk_is_match(const struct clk *p, const struct clk *q)
1983{
1984 /* trivial case: identical struct clk's or both NULL */
1985 if (p == q)
1986 return true;
1987
1988 /* true if clk->core pointers match. Avoid dereferencing garbage */
1989 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
1990 if (p->core == q->core)
1991 return true;
1992
1993 return false;
1994}
1995EXPORT_SYMBOL_GPL(clk_is_match);
1996
1997/*** debugfs support ***/
1998
1999#ifdef CONFIG_DEBUG_FS
2000#include <linux/debugfs.h>
2001
2002static struct dentry *rootdir;
2003static int inited = 0;
2004static DEFINE_MUTEX(clk_debug_lock);
2005static HLIST_HEAD(clk_debug_list);
2006
2007static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2008 int level)
2009{
2010 if (!c)
2011 return;
2012
2013 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
2014 level * 3 + 1, "",
2015 30 - level * 3, c->name,
2016 c->enable_count, c->prepare_count, clk_core_get_rate(c),
2017 clk_core_get_accuracy(c), clk_core_get_phase(c));
2018}
2019
2020static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2021 int level)
2022{
2023 struct clk_core *child;
2024
2025 if (!c)
2026 return;
2027
2028 clk_summary_show_one(s, c, level);
2029
2030 hlist_for_each_entry(child, &c->children, child_node)
2031 clk_summary_show_subtree(s, child, level + 1);
2032}
2033
2034static int clk_summary_show(struct seq_file *s, void *data)
2035{
2036 struct clk_core *c;
2037 struct hlist_head **lists = (struct hlist_head **)s->private;
2038
2039 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
2040 seq_puts(s, "----------------------------------------------------------------------------------------\n");
2041
2042 clk_prepare_lock();
2043
2044 for (; *lists; lists++)
2045 hlist_for_each_entry(c, *lists, child_node)
2046 clk_summary_show_subtree(s, c, 0);
2047
2048 clk_prepare_unlock();
2049
2050 return 0;
2051}
2052
2053
2054static int clk_summary_open(struct inode *inode, struct file *file)
2055{
2056 return single_open(file, clk_summary_show, inode->i_private);
2057}
2058
2059static const struct file_operations clk_summary_fops = {
2060 .open = clk_summary_open,
2061 .read = seq_read,
2062 .llseek = seq_lseek,
2063 .release = single_release,
2064};
2065
2066static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2067{
2068 if (!c)
2069 return;
2070
2071 /* This should be JSON format, i.e. elements separated with a comma */
2072 seq_printf(s, "\"%s\": { ", c->name);
2073 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2074 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
2075 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2076 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
2077 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2078}
2079
2080static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2081{
2082 struct clk_core *child;
2083
2084 if (!c)
2085 return;
2086
2087 clk_dump_one(s, c, level);
2088
2089 hlist_for_each_entry(child, &c->children, child_node) {
2090 seq_putc(s, ',');
2091 clk_dump_subtree(s, child, level + 1);
2092 }
2093
2094 seq_putc(s, '}');
2095}
2096
2097static int clk_dump(struct seq_file *s, void *data)
2098{
2099 struct clk_core *c;
2100 bool first_node = true;
2101 struct hlist_head **lists = (struct hlist_head **)s->private;
2102
2103 seq_putc(s, '{');
2104 clk_prepare_lock();
2105
2106 for (; *lists; lists++) {
2107 hlist_for_each_entry(c, *lists, child_node) {
2108 if (!first_node)
2109 seq_putc(s, ',');
2110 first_node = false;
2111 clk_dump_subtree(s, c, 0);
2112 }
2113 }
2114
2115 clk_prepare_unlock();
2116
2117 seq_puts(s, "}\n");
2118 return 0;
2119}
2120
2121
2122static int clk_dump_open(struct inode *inode, struct file *file)
2123{
2124 return single_open(file, clk_dump, inode->i_private);
2125}
2126
2127static const struct file_operations clk_dump_fops = {
2128 .open = clk_dump_open,
2129 .read = seq_read,
2130 .llseek = seq_lseek,
2131 .release = single_release,
2132};
2133
2134static int possible_parents_dump(struct seq_file *s, void *data)
2135{
2136 struct clk_core *core = s->private;
2137 int i;
2138
2139 for (i = 0; i < core->num_parents - 1; i++)
2140 seq_printf(s, "%s ", core->parent_names[i]);
2141
2142 seq_printf(s, "%s\n", core->parent_names[i]);
2143
2144 return 0;
2145}
2146
2147static int possible_parents_open(struct inode *inode, struct file *file)
2148{
2149 return single_open(file, possible_parents_dump, inode->i_private);
2150}
2151
2152static const struct file_operations possible_parents_fops = {
2153 .open = possible_parents_open,
2154 .read = seq_read,
2155 .llseek = seq_lseek,
2156 .release = single_release,
2157};
2158
2159static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2160{
2161 struct dentry *d;
2162 int ret = -ENOMEM;
2163
2164 if (!core || !pdentry) {
2165 ret = -EINVAL;
2166 goto out;
2167 }
2168
2169 d = debugfs_create_dir(core->name, pdentry);
2170 if (!d)
2171 goto out;
2172
2173 core->dentry = d;
2174
2175 d = debugfs_create_ulong("clk_rate", 0444, core->dentry, &core->rate);
2176 if (!d)
2177 goto err_out;
2178
2179 d = debugfs_create_ulong("clk_accuracy", 0444, core->dentry,
2180 &core->accuracy);
2181 if (!d)
2182 goto err_out;
2183
2184 d = debugfs_create_u32("clk_phase", 0444, core->dentry, &core->phase);
2185 if (!d)
2186 goto err_out;
2187
2188 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2189 (u32 *)&core->flags);
2190 if (!d)
2191 goto err_out;
2192
2193 d = debugfs_create_u32("clk_prepare_count", 0444, core->dentry,
2194 &core->prepare_count);
2195 if (!d)
2196 goto err_out;
2197
2198 d = debugfs_create_u32("clk_enable_count", 0444, core->dentry,
2199 &core->enable_count);
2200 if (!d)
2201 goto err_out;
2202
2203 d = debugfs_create_u32("clk_notifier_count", 0444, core->dentry,
2204 &core->notifier_count);
2205 if (!d)
2206 goto err_out;
2207
2208 if (core->num_parents > 1) {
2209 d = debugfs_create_file("clk_possible_parents", 0444,
2210 core->dentry, core, &possible_parents_fops);
2211 if (!d)
2212 goto err_out;
2213 }
2214
2215 if (core->ops->debug_init) {
2216 ret = core->ops->debug_init(core->hw, core->dentry);
2217 if (ret)
2218 goto err_out;
2219 }
2220
2221 ret = 0;
2222 goto out;
2223
2224err_out:
2225 debugfs_remove_recursive(core->dentry);
2226 core->dentry = NULL;
2227out:
2228 return ret;
2229}
2230
2231/**
2232 * clk_debug_register - add a clk node to the debugfs clk directory
2233 * @core: the clk being added to the debugfs clk directory
2234 *
2235 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2236 * initialized. Otherwise it bails out early since the debugfs clk directory
2237 * will be created lazily by clk_debug_init as part of a late_initcall.
2238 */
2239static int clk_debug_register(struct clk_core *core)
2240{
2241 int ret = 0;
2242
2243 mutex_lock(&clk_debug_lock);
2244 hlist_add_head(&core->debug_node, &clk_debug_list);
2245
2246 if (!inited)
2247 goto unlock;
2248
2249 ret = clk_debug_create_one(core, rootdir);
2250unlock:
2251 mutex_unlock(&clk_debug_lock);
2252
2253 return ret;
2254}
2255
2256 /**
2257 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2258 * @core: the clk being removed from the debugfs clk directory
2259 *
2260 * Dynamically removes a clk and all its child nodes from the
2261 * debugfs clk directory if clk->dentry points to debugfs created by
2262 * clk_debug_register in __clk_core_init.
2263 */
2264static void clk_debug_unregister(struct clk_core *core)
2265{
2266 mutex_lock(&clk_debug_lock);
2267 hlist_del_init(&core->debug_node);
2268 debugfs_remove_recursive(core->dentry);
2269 core->dentry = NULL;
2270 mutex_unlock(&clk_debug_lock);
2271}
2272
2273struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2274 void *data, const struct file_operations *fops)
2275{
2276 struct dentry *d = NULL;
2277
2278 if (hw->core->dentry)
2279 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2280 fops);
2281
2282 return d;
2283}
2284EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2285
2286/**
2287 * clk_debug_init - lazily populate the debugfs clk directory
2288 *
2289 * clks are often initialized very early during boot before memory can be
2290 * dynamically allocated and well before debugfs is setup. This function
2291 * populates the debugfs clk directory once at boot-time when we know that
2292 * debugfs is setup. It should only be called once at boot-time, all other clks
2293 * added dynamically will be done so with clk_debug_register.
2294 */
2295static int __init clk_debug_init(void)
2296{
2297 struct clk_core *core;
2298 struct dentry *d;
2299
2300 rootdir = debugfs_create_dir("clk", NULL);
2301
2302 if (!rootdir)
2303 return -ENOMEM;
2304
2305 d = debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
2306 &clk_summary_fops);
2307 if (!d)
2308 return -ENOMEM;
2309
2310 d = debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
2311 &clk_dump_fops);
2312 if (!d)
2313 return -ENOMEM;
2314
2315 d = debugfs_create_file("clk_orphan_summary", 0444, rootdir,
2316 &orphan_list, &clk_summary_fops);
2317 if (!d)
2318 return -ENOMEM;
2319
2320 d = debugfs_create_file("clk_orphan_dump", 0444, rootdir,
2321 &orphan_list, &clk_dump_fops);
2322 if (!d)
2323 return -ENOMEM;
2324
2325 mutex_lock(&clk_debug_lock);
2326 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2327 clk_debug_create_one(core, rootdir);
2328
2329 inited = 1;
2330 mutex_unlock(&clk_debug_lock);
2331
2332 return 0;
2333}
2334late_initcall(clk_debug_init);
2335#else
2336static inline int clk_debug_register(struct clk_core *core) { return 0; }
2337static inline void clk_debug_reparent(struct clk_core *core,
2338 struct clk_core *new_parent)
2339{
2340}
2341static inline void clk_debug_unregister(struct clk_core *core)
2342{
2343}
2344#endif
2345
2346/**
2347 * __clk_core_init - initialize the data structures in a struct clk_core
2348 * @core: clk_core being initialized
2349 *
2350 * Initializes the lists in struct clk_core, queries the hardware for the
2351 * parent and rate and sets them both.
2352 */
2353static int __clk_core_init(struct clk_core *core)
2354{
2355 int i, ret = 0;
2356 struct clk_core *orphan;
2357 struct hlist_node *tmp2;
2358 unsigned long rate;
2359
2360 if (!core)
2361 return -EINVAL;
2362
2363 clk_prepare_lock();
2364
2365 /* check to see if a clock with this name is already registered */
2366 if (clk_core_lookup(core->name)) {
2367 pr_debug("%s: clk %s already initialized\n",
2368 __func__, core->name);
2369 ret = -EEXIST;
2370 goto out;
2371 }
2372
2373 /* check that clk_ops are sane. See Documentation/clk.txt */
2374 if (core->ops->set_rate &&
2375 !((core->ops->round_rate || core->ops->determine_rate) &&
2376 core->ops->recalc_rate)) {
2377 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2378 __func__, core->name);
2379 ret = -EINVAL;
2380 goto out;
2381 }
2382
2383 if (core->ops->set_parent && !core->ops->get_parent) {
2384 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2385 __func__, core->name);
2386 ret = -EINVAL;
2387 goto out;
2388 }
2389
2390 if (core->num_parents > 1 && !core->ops->get_parent) {
2391 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2392 __func__, core->name);
2393 ret = -EINVAL;
2394 goto out;
2395 }
2396
2397 if (core->ops->set_rate_and_parent &&
2398 !(core->ops->set_parent && core->ops->set_rate)) {
2399 pr_err("%s: %s must implement .set_parent & .set_rate\n",
2400 __func__, core->name);
2401 ret = -EINVAL;
2402 goto out;
2403 }
2404
2405 /* throw a WARN if any entries in parent_names are NULL */
2406 for (i = 0; i < core->num_parents; i++)
2407 WARN(!core->parent_names[i],
2408 "%s: invalid NULL in %s's .parent_names\n",
2409 __func__, core->name);
2410
2411 core->parent = __clk_init_parent(core);
2412
2413 /*
2414 * Populate core->parent if parent has already been clk_core_init'd. If
2415 * parent has not yet been clk_core_init'd then place clk in the orphan
2416 * list. If clk doesn't have any parents then place it in the root
2417 * clk list.
2418 *
2419 * Every time a new clk is clk_init'd then we walk the list of orphan
2420 * clocks and re-parent any that are children of the clock currently
2421 * being clk_init'd.
2422 */
2423 if (core->parent) {
2424 hlist_add_head(&core->child_node,
2425 &core->parent->children);
2426 core->orphan = core->parent->orphan;
2427 } else if (!core->num_parents) {
2428 hlist_add_head(&core->child_node, &clk_root_list);
2429 core->orphan = false;
2430 } else {
2431 hlist_add_head(&core->child_node, &clk_orphan_list);
2432 core->orphan = true;
2433 }
2434
2435 /*
2436 * Set clk's accuracy. The preferred method is to use
2437 * .recalc_accuracy. For simple clocks and lazy developers the default
2438 * fallback is to use the parent's accuracy. If a clock doesn't have a
2439 * parent (or is orphaned) then accuracy is set to zero (perfect
2440 * clock).
2441 */
2442 if (core->ops->recalc_accuracy)
2443 core->accuracy = core->ops->recalc_accuracy(core->hw,
2444 __clk_get_accuracy(core->parent));
2445 else if (core->parent)
2446 core->accuracy = core->parent->accuracy;
2447 else
2448 core->accuracy = 0;
2449
2450 /*
2451 * Set clk's phase.
2452 * Since a phase is by definition relative to its parent, just
2453 * query the current clock phase, or just assume it's in phase.
2454 */
2455 if (core->ops->get_phase)
2456 core->phase = core->ops->get_phase(core->hw);
2457 else
2458 core->phase = 0;
2459
2460 /*
2461 * Set clk's rate. The preferred method is to use .recalc_rate. For
2462 * simple clocks and lazy developers the default fallback is to use the
2463 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2464 * then rate is set to zero.
2465 */
2466 if (core->ops->recalc_rate)
2467 rate = core->ops->recalc_rate(core->hw,
2468 clk_core_get_rate_nolock(core->parent));
2469 else if (core->parent)
2470 rate = core->parent->rate;
2471 else
2472 rate = 0;
2473 core->rate = core->req_rate = rate;
2474
2475 /*
2476 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
2477 * don't get accidentally disabled when walking the orphan tree and
2478 * reparenting clocks
2479 */
2480 if (core->flags & CLK_IS_CRITICAL) {
2481 unsigned long flags;
2482
2483 ret = clk_core_prepare(core);
2484 if (ret)
2485 goto out;
2486
2487 flags = clk_enable_lock();
2488 ret = clk_core_enable(core);
2489 clk_enable_unlock(flags);
2490 if (ret) {
2491 clk_core_unprepare(core);
2492 goto out;
2493 }
2494 }
2495
2496 /*
2497 * walk the list of orphan clocks and reparent any that newly finds a
2498 * parent.
2499 */
2500 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
2501 struct clk_core *parent = __clk_init_parent(orphan);
2502
2503 /*
2504 * We need to use __clk_set_parent_before() and _after() to
2505 * to properly migrate any prepare/enable count of the orphan
2506 * clock. This is important for CLK_IS_CRITICAL clocks, which
2507 * are enabled during init but might not have a parent yet.
2508 */
2509 if (parent) {
2510 /* update the clk tree topology */
2511 __clk_set_parent_before(orphan, parent);
2512 __clk_set_parent_after(orphan, parent, NULL);
2513 __clk_recalc_accuracies(orphan);
2514 __clk_recalc_rates(orphan, 0);
2515 }
2516 }
2517
2518 /*
2519 * optional platform-specific magic
2520 *
2521 * The .init callback is not used by any of the basic clock types, but
2522 * exists for weird hardware that must perform initialization magic.
2523 * Please consider other ways of solving initialization problems before
2524 * using this callback, as its use is discouraged.
2525 */
2526 if (core->ops->init)
2527 core->ops->init(core->hw);
2528
2529 kref_init(&core->ref);
2530out:
2531 clk_prepare_unlock();
2532
2533 if (!ret)
2534 clk_debug_register(core);
2535
2536 return ret;
2537}
2538
2539struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2540 const char *con_id)
2541{
2542 struct clk *clk;
2543
2544 /* This is to allow this function to be chained to others */
2545 if (IS_ERR_OR_NULL(hw))
2546 return ERR_CAST(hw);
2547
2548 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2549 if (!clk)
2550 return ERR_PTR(-ENOMEM);
2551
2552 clk->core = hw->core;
2553 clk->dev_id = dev_id;
2554 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
2555 clk->max_rate = ULONG_MAX;
2556
2557 clk_prepare_lock();
2558 hlist_add_head(&clk->clks_node, &hw->core->clks);
2559 clk_prepare_unlock();
2560
2561 return clk;
2562}
2563
2564/* keep in sync with __clk_put */
2565void __clk_free_clk(struct clk *clk)
2566{
2567 clk_prepare_lock();
2568 hlist_del(&clk->clks_node);
2569 clk_prepare_unlock();
2570
2571 kfree_const(clk->con_id);
2572 kfree(clk);
2573}
2574
2575/**
2576 * clk_register - allocate a new clock, register it and return an opaque cookie
2577 * @dev: device that is registering this clock
2578 * @hw: link to hardware-specific clock data
2579 *
2580 * clk_register is the primary interface for populating the clock tree with new
2581 * clock nodes. It returns a pointer to the newly allocated struct clk which
2582 * cannot be dereferenced by driver code but may be used in conjunction with the
2583 * rest of the clock API. In the event of an error clk_register will return an
2584 * error code; drivers must test for an error code after calling clk_register.
2585 */
2586struct clk *clk_register(struct device *dev, struct clk_hw *hw)
2587{
2588 int i, ret;
2589 struct clk_core *core;
2590
2591 core = kzalloc(sizeof(*core), GFP_KERNEL);
2592 if (!core) {
2593 ret = -ENOMEM;
2594 goto fail_out;
2595 }
2596
2597 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2598 if (!core->name) {
2599 ret = -ENOMEM;
2600 goto fail_name;
2601 }
2602 core->ops = hw->init->ops;
2603 if (dev && dev->driver)
2604 core->owner = dev->driver->owner;
2605 core->hw = hw;
2606 core->flags = hw->init->flags;
2607 core->num_parents = hw->init->num_parents;
2608 core->min_rate = 0;
2609 core->max_rate = ULONG_MAX;
2610 hw->core = core;
2611
2612 /* allocate local copy in case parent_names is __initdata */
2613 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
2614 GFP_KERNEL);
2615
2616 if (!core->parent_names) {
2617 ret = -ENOMEM;
2618 goto fail_parent_names;
2619 }
2620
2621
2622 /* copy each string name in case parent_names is __initdata */
2623 for (i = 0; i < core->num_parents; i++) {
2624 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
2625 GFP_KERNEL);
2626 if (!core->parent_names[i]) {
2627 ret = -ENOMEM;
2628 goto fail_parent_names_copy;
2629 }
2630 }
2631
2632 /* avoid unnecessary string look-ups of clk_core's possible parents. */
2633 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
2634 GFP_KERNEL);
2635 if (!core->parents) {
2636 ret = -ENOMEM;
2637 goto fail_parents;
2638 };
2639
2640 INIT_HLIST_HEAD(&core->clks);
2641
2642 hw->clk = __clk_create_clk(hw, NULL, NULL);
2643 if (IS_ERR(hw->clk)) {
2644 ret = PTR_ERR(hw->clk);
2645 goto fail_parents;
2646 }
2647
2648 ret = __clk_core_init(core);
2649 if (!ret)
2650 return hw->clk;
2651
2652 __clk_free_clk(hw->clk);
2653 hw->clk = NULL;
2654
2655fail_parents:
2656 kfree(core->parents);
2657fail_parent_names_copy:
2658 while (--i >= 0)
2659 kfree_const(core->parent_names[i]);
2660 kfree(core->parent_names);
2661fail_parent_names:
2662 kfree_const(core->name);
2663fail_name:
2664 kfree(core);
2665fail_out:
2666 return ERR_PTR(ret);
2667}
2668EXPORT_SYMBOL_GPL(clk_register);
2669
2670/**
2671 * clk_hw_register - register a clk_hw and return an error code
2672 * @dev: device that is registering this clock
2673 * @hw: link to hardware-specific clock data
2674 *
2675 * clk_hw_register is the primary interface for populating the clock tree with
2676 * new clock nodes. It returns an integer equal to zero indicating success or
2677 * less than zero indicating failure. Drivers must test for an error code after
2678 * calling clk_hw_register().
2679 */
2680int clk_hw_register(struct device *dev, struct clk_hw *hw)
2681{
2682 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
2683}
2684EXPORT_SYMBOL_GPL(clk_hw_register);
2685
2686/* Free memory allocated for a clock. */
2687static void __clk_release(struct kref *ref)
2688{
2689 struct clk_core *core = container_of(ref, struct clk_core, ref);
2690 int i = core->num_parents;
2691
2692 lockdep_assert_held(&prepare_lock);
2693
2694 kfree(core->parents);
2695 while (--i >= 0)
2696 kfree_const(core->parent_names[i]);
2697
2698 kfree(core->parent_names);
2699 kfree_const(core->name);
2700 kfree(core);
2701}
2702
2703/*
2704 * Empty clk_ops for unregistered clocks. These are used temporarily
2705 * after clk_unregister() was called on a clock and until last clock
2706 * consumer calls clk_put() and the struct clk object is freed.
2707 */
2708static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2709{
2710 return -ENXIO;
2711}
2712
2713static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2714{
2715 WARN_ON_ONCE(1);
2716}
2717
2718static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2719 unsigned long parent_rate)
2720{
2721 return -ENXIO;
2722}
2723
2724static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2725{
2726 return -ENXIO;
2727}
2728
2729static const struct clk_ops clk_nodrv_ops = {
2730 .enable = clk_nodrv_prepare_enable,
2731 .disable = clk_nodrv_disable_unprepare,
2732 .prepare = clk_nodrv_prepare_enable,
2733 .unprepare = clk_nodrv_disable_unprepare,
2734 .set_rate = clk_nodrv_set_rate,
2735 .set_parent = clk_nodrv_set_parent,
2736};
2737
2738static void clk_core_evict_parent_cache_subtree(struct clk_core *root,
2739 struct clk_core *target)
2740{
2741 int i;
2742 struct clk_core *child;
2743
2744 for (i = 0; i < root->num_parents; i++)
2745 if (root->parents[i] == target)
2746 root->parents[i] = NULL;
2747
2748 hlist_for_each_entry(child, &root->children, child_node)
2749 clk_core_evict_parent_cache_subtree(child, target);
2750}
2751
2752/* Remove this clk from all parent caches */
2753static void clk_core_evict_parent_cache(struct clk_core *core)
2754{
2755 struct hlist_head **lists;
2756 struct clk_core *root;
2757
2758 lockdep_assert_held(&prepare_lock);
2759
2760 for (lists = all_lists; *lists; lists++)
2761 hlist_for_each_entry(root, *lists, child_node)
2762 clk_core_evict_parent_cache_subtree(root, core);
2763
2764}
2765
2766/**
2767 * clk_unregister - unregister a currently registered clock
2768 * @clk: clock to unregister
2769 */
2770void clk_unregister(struct clk *clk)
2771{
2772 unsigned long flags;
2773
2774 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2775 return;
2776
2777 clk_debug_unregister(clk->core);
2778
2779 clk_prepare_lock();
2780
2781 if (clk->core->ops == &clk_nodrv_ops) {
2782 pr_err("%s: unregistered clock: %s\n", __func__,
2783 clk->core->name);
2784 goto unlock;
2785 }
2786 /*
2787 * Assign empty clock ops for consumers that might still hold
2788 * a reference to this clock.
2789 */
2790 flags = clk_enable_lock();
2791 clk->core->ops = &clk_nodrv_ops;
2792 clk_enable_unlock(flags);
2793
2794 if (!hlist_empty(&clk->core->children)) {
2795 struct clk_core *child;
2796 struct hlist_node *t;
2797
2798 /* Reparent all children to the orphan list. */
2799 hlist_for_each_entry_safe(child, t, &clk->core->children,
2800 child_node)
2801 clk_core_set_parent(child, NULL);
2802 }
2803
2804 clk_core_evict_parent_cache(clk->core);
2805
2806 hlist_del_init(&clk->core->child_node);
2807
2808 if (clk->core->prepare_count)
2809 pr_warn("%s: unregistering prepared clock: %s\n",
2810 __func__, clk->core->name);
2811 kref_put(&clk->core->ref, __clk_release);
2812unlock:
2813 clk_prepare_unlock();
2814}
2815EXPORT_SYMBOL_GPL(clk_unregister);
2816
2817/**
2818 * clk_hw_unregister - unregister a currently registered clk_hw
2819 * @hw: hardware-specific clock data to unregister
2820 */
2821void clk_hw_unregister(struct clk_hw *hw)
2822{
2823 clk_unregister(hw->clk);
2824}
2825EXPORT_SYMBOL_GPL(clk_hw_unregister);
2826
2827static void devm_clk_release(struct device *dev, void *res)
2828{
2829 clk_unregister(*(struct clk **)res);
2830}
2831
2832static void devm_clk_hw_release(struct device *dev, void *res)
2833{
2834 clk_hw_unregister(*(struct clk_hw **)res);
2835}
2836
2837/**
2838 * devm_clk_register - resource managed clk_register()
2839 * @dev: device that is registering this clock
2840 * @hw: link to hardware-specific clock data
2841 *
2842 * Managed clk_register(). Clocks returned from this function are
2843 * automatically clk_unregister()ed on driver detach. See clk_register() for
2844 * more information.
2845 */
2846struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2847{
2848 struct clk *clk;
2849 struct clk **clkp;
2850
2851 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2852 if (!clkp)
2853 return ERR_PTR(-ENOMEM);
2854
2855 clk = clk_register(dev, hw);
2856 if (!IS_ERR(clk)) {
2857 *clkp = clk;
2858 devres_add(dev, clkp);
2859 } else {
2860 devres_free(clkp);
2861 }
2862
2863 return clk;
2864}
2865EXPORT_SYMBOL_GPL(devm_clk_register);
2866
2867/**
2868 * devm_clk_hw_register - resource managed clk_hw_register()
2869 * @dev: device that is registering this clock
2870 * @hw: link to hardware-specific clock data
2871 *
2872 * Managed clk_hw_register(). Clocks registered by this function are
2873 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
2874 * for more information.
2875 */
2876int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
2877{
2878 struct clk_hw **hwp;
2879 int ret;
2880
2881 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
2882 if (!hwp)
2883 return -ENOMEM;
2884
2885 ret = clk_hw_register(dev, hw);
2886 if (!ret) {
2887 *hwp = hw;
2888 devres_add(dev, hwp);
2889 } else {
2890 devres_free(hwp);
2891 }
2892
2893 return ret;
2894}
2895EXPORT_SYMBOL_GPL(devm_clk_hw_register);
2896
2897static int devm_clk_match(struct device *dev, void *res, void *data)
2898{
2899 struct clk *c = res;
2900 if (WARN_ON(!c))
2901 return 0;
2902 return c == data;
2903}
2904
2905static int devm_clk_hw_match(struct device *dev, void *res, void *data)
2906{
2907 struct clk_hw *hw = res;
2908
2909 if (WARN_ON(!hw))
2910 return 0;
2911 return hw == data;
2912}
2913
2914/**
2915 * devm_clk_unregister - resource managed clk_unregister()
2916 * @clk: clock to unregister
2917 *
2918 * Deallocate a clock allocated with devm_clk_register(). Normally
2919 * this function will not need to be called and the resource management
2920 * code will ensure that the resource is freed.
2921 */
2922void devm_clk_unregister(struct device *dev, struct clk *clk)
2923{
2924 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2925}
2926EXPORT_SYMBOL_GPL(devm_clk_unregister);
2927
2928/**
2929 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
2930 * @dev: device that is unregistering the hardware-specific clock data
2931 * @hw: link to hardware-specific clock data
2932 *
2933 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
2934 * this function will not need to be called and the resource management
2935 * code will ensure that the resource is freed.
2936 */
2937void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
2938{
2939 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
2940 hw));
2941}
2942EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
2943
2944/*
2945 * clkdev helpers
2946 */
2947int __clk_get(struct clk *clk)
2948{
2949 struct clk_core *core = !clk ? NULL : clk->core;
2950
2951 if (core) {
2952 if (!try_module_get(core->owner))
2953 return 0;
2954
2955 kref_get(&core->ref);
2956 }
2957 return 1;
2958}
2959
2960/* keep in sync with __clk_free_clk */
2961void __clk_put(struct clk *clk)
2962{
2963 struct module *owner;
2964
2965 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2966 return;
2967
2968 clk_prepare_lock();
2969
2970 hlist_del(&clk->clks_node);
2971 if (clk->min_rate > clk->core->req_rate ||
2972 clk->max_rate < clk->core->req_rate)
2973 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2974
2975 owner = clk->core->owner;
2976 kref_put(&clk->core->ref, __clk_release);
2977
2978 clk_prepare_unlock();
2979
2980 module_put(owner);
2981
2982 kfree_const(clk->con_id);
2983 kfree(clk);
2984}
2985
2986/*** clk rate change notifiers ***/
2987
2988/**
2989 * clk_notifier_register - add a clk rate change notifier
2990 * @clk: struct clk * to watch
2991 * @nb: struct notifier_block * with callback info
2992 *
2993 * Request notification when clk's rate changes. This uses an SRCU
2994 * notifier because we want it to block and notifier unregistrations are
2995 * uncommon. The callbacks associated with the notifier must not
2996 * re-enter into the clk framework by calling any top-level clk APIs;
2997 * this will cause a nested prepare_lock mutex.
2998 *
2999 * In all notification cases (pre, post and abort rate change) the original
3000 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3001 * and the new frequency is passed via struct clk_notifier_data.new_rate.
3002 *
3003 * clk_notifier_register() must be called from non-atomic context.
3004 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3005 * allocation failure; otherwise, passes along the return value of
3006 * srcu_notifier_chain_register().
3007 */
3008int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3009{
3010 struct clk_notifier *cn;
3011 int ret = -ENOMEM;
3012
3013 if (!clk || !nb)
3014 return -EINVAL;
3015
3016 clk_prepare_lock();
3017
3018 /* search the list of notifiers for this clk */
3019 list_for_each_entry(cn, &clk_notifier_list, node)
3020 if (cn->clk == clk)
3021 break;
3022
3023 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3024 if (cn->clk != clk) {
3025 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
3026 if (!cn)
3027 goto out;
3028
3029 cn->clk = clk;
3030 srcu_init_notifier_head(&cn->notifier_head);
3031
3032 list_add(&cn->node, &clk_notifier_list);
3033 }
3034
3035 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3036
3037 clk->core->notifier_count++;
3038
3039out:
3040 clk_prepare_unlock();
3041
3042 return ret;
3043}
3044EXPORT_SYMBOL_GPL(clk_notifier_register);
3045
3046/**
3047 * clk_notifier_unregister - remove a clk rate change notifier
3048 * @clk: struct clk *
3049 * @nb: struct notifier_block * with callback info
3050 *
3051 * Request no further notification for changes to 'clk' and frees memory
3052 * allocated in clk_notifier_register.
3053 *
3054 * Returns -EINVAL if called with null arguments; otherwise, passes
3055 * along the return value of srcu_notifier_chain_unregister().
3056 */
3057int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3058{
3059 struct clk_notifier *cn = NULL;
3060 int ret = -EINVAL;
3061
3062 if (!clk || !nb)
3063 return -EINVAL;
3064
3065 clk_prepare_lock();
3066
3067 list_for_each_entry(cn, &clk_notifier_list, node)
3068 if (cn->clk == clk)
3069 break;
3070
3071 if (cn->clk == clk) {
3072 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3073
3074 clk->core->notifier_count--;
3075
3076 /* XXX the notifier code should handle this better */
3077 if (!cn->notifier_head.head) {
3078 srcu_cleanup_notifier_head(&cn->notifier_head);
3079 list_del(&cn->node);
3080 kfree(cn);
3081 }
3082
3083 } else {
3084 ret = -ENOENT;
3085 }
3086
3087 clk_prepare_unlock();
3088
3089 return ret;
3090}
3091EXPORT_SYMBOL_GPL(clk_notifier_unregister);
3092
3093#ifdef CONFIG_OF
3094/**
3095 * struct of_clk_provider - Clock provider registration structure
3096 * @link: Entry in global list of clock providers
3097 * @node: Pointer to device tree node of clock provider
3098 * @get: Get clock callback. Returns NULL or a struct clk for the
3099 * given clock specifier
3100 * @data: context pointer to be passed into @get callback
3101 */
3102struct of_clk_provider {
3103 struct list_head link;
3104
3105 struct device_node *node;
3106 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
3107 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
3108 void *data;
3109};
3110
3111static const struct of_device_id __clk_of_table_sentinel
3112 __used __section(__clk_of_table_end);
3113
3114static LIST_HEAD(of_clk_providers);
3115static DEFINE_MUTEX(of_clk_mutex);
3116
3117struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3118 void *data)
3119{
3120 return data;
3121}
3122EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3123
3124struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3125{
3126 return data;
3127}
3128EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3129
3130struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3131{
3132 struct clk_onecell_data *clk_data = data;
3133 unsigned int idx = clkspec->args[0];
3134
3135 if (idx >= clk_data->clk_num) {
3136 pr_err("%s: invalid clock index %u\n", __func__, idx);
3137 return ERR_PTR(-EINVAL);
3138 }
3139
3140 return clk_data->clks[idx];
3141}
3142EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3143
3144struct clk_hw *
3145of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3146{
3147 struct clk_hw_onecell_data *hw_data = data;
3148 unsigned int idx = clkspec->args[0];
3149
3150 if (idx >= hw_data->num) {
3151 pr_err("%s: invalid index %u\n", __func__, idx);
3152 return ERR_PTR(-EINVAL);
3153 }
3154
3155 return hw_data->hws[idx];
3156}
3157EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3158
3159/**
3160 * of_clk_add_provider() - Register a clock provider for a node
3161 * @np: Device node pointer associated with clock provider
3162 * @clk_src_get: callback for decoding clock
3163 * @data: context pointer for @clk_src_get callback.
3164 */
3165int of_clk_add_provider(struct device_node *np,
3166 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3167 void *data),
3168 void *data)
3169{
3170 struct of_clk_provider *cp;
3171 int ret;
3172
3173 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3174 if (!cp)
3175 return -ENOMEM;
3176
3177 cp->node = of_node_get(np);
3178 cp->data = data;
3179 cp->get = clk_src_get;
3180
3181 mutex_lock(&of_clk_mutex);
3182 list_add(&cp->link, &of_clk_providers);
3183 mutex_unlock(&of_clk_mutex);
3184 pr_debug("Added clock from %pOF\n", np);
3185
3186 ret = of_clk_set_defaults(np, true);
3187 if (ret < 0)
3188 of_clk_del_provider(np);
3189
3190 return ret;
3191}
3192EXPORT_SYMBOL_GPL(of_clk_add_provider);
3193
3194/**
3195 * of_clk_add_hw_provider() - Register a clock provider for a node
3196 * @np: Device node pointer associated with clock provider
3197 * @get: callback for decoding clk_hw
3198 * @data: context pointer for @get callback.
3199 */
3200int of_clk_add_hw_provider(struct device_node *np,
3201 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3202 void *data),
3203 void *data)
3204{
3205 struct of_clk_provider *cp;
3206 int ret;
3207
3208 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3209 if (!cp)
3210 return -ENOMEM;
3211
3212 cp->node = of_node_get(np);
3213 cp->data = data;
3214 cp->get_hw = get;
3215
3216 mutex_lock(&of_clk_mutex);
3217 list_add(&cp->link, &of_clk_providers);
3218 mutex_unlock(&of_clk_mutex);
3219 pr_debug("Added clk_hw provider from %pOF\n", np);
3220
3221 ret = of_clk_set_defaults(np, true);
3222 if (ret < 0)
3223 of_clk_del_provider(np);
3224
3225 return ret;
3226}
3227EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3228
3229/**
3230 * of_clk_del_provider() - Remove a previously registered clock provider
3231 * @np: Device node pointer associated with clock provider
3232 */
3233void of_clk_del_provider(struct device_node *np)
3234{
3235 struct of_clk_provider *cp;
3236
3237 mutex_lock(&of_clk_mutex);
3238 list_for_each_entry(cp, &of_clk_providers, link) {
3239 if (cp->node == np) {
3240 list_del(&cp->link);
3241 of_node_put(cp->node);
3242 kfree(cp);
3243 break;
3244 }
3245 }
3246 mutex_unlock(&of_clk_mutex);
3247}
3248EXPORT_SYMBOL_GPL(of_clk_del_provider);
3249
3250static struct clk_hw *
3251__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3252 struct of_phandle_args *clkspec)
3253{
3254 struct clk *clk;
3255
3256 if (provider->get_hw)
3257 return provider->get_hw(clkspec, provider->data);
3258
3259 clk = provider->get(clkspec, provider->data);
3260 if (IS_ERR(clk))
3261 return ERR_CAST(clk);
3262 return __clk_get_hw(clk);
3263}
3264
3265struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3266 const char *dev_id, const char *con_id)
3267{
3268 struct of_clk_provider *provider;
3269 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
3270 struct clk_hw *hw;
3271
3272 if (!clkspec)
3273 return ERR_PTR(-EINVAL);
3274
3275 /* Check if we have such a provider in our array */
3276 mutex_lock(&of_clk_mutex);
3277 list_for_each_entry(provider, &of_clk_providers, link) {
3278 if (provider->node == clkspec->np) {
3279 hw = __of_clk_get_hw_from_provider(provider, clkspec);
3280 clk = __clk_create_clk(hw, dev_id, con_id);
3281 }
3282
3283 if (!IS_ERR(clk)) {
3284 if (!__clk_get(clk)) {
3285 __clk_free_clk(clk);
3286 clk = ERR_PTR(-ENOENT);
3287 }
3288
3289 break;
3290 }
3291 }
3292 mutex_unlock(&of_clk_mutex);
3293
3294 return clk;
3295}
3296
3297/**
3298 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3299 * @clkspec: pointer to a clock specifier data structure
3300 *
3301 * This function looks up a struct clk from the registered list of clock
3302 * providers, an input is a clock specifier data structure as returned
3303 * from the of_parse_phandle_with_args() function call.
3304 */
3305struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3306{
3307 return __of_clk_get_from_provider(clkspec, NULL, __func__);
3308}
3309EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
3310
3311/**
3312 * of_clk_get_parent_count() - Count the number of clocks a device node has
3313 * @np: device node to count
3314 *
3315 * Returns: The number of clocks that are possible parents of this node
3316 */
3317unsigned int of_clk_get_parent_count(struct device_node *np)
3318{
3319 int count;
3320
3321 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3322 if (count < 0)
3323 return 0;
3324
3325 return count;
3326}
3327EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3328
3329const char *of_clk_get_parent_name(struct device_node *np, int index)
3330{
3331 struct of_phandle_args clkspec;
3332 struct property *prop;
3333 const char *clk_name;
3334 const __be32 *vp;
3335 u32 pv;
3336 int rc;
3337 int count;
3338 struct clk *clk;
3339
3340 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3341 &clkspec);
3342 if (rc)
3343 return NULL;
3344
3345 index = clkspec.args_count ? clkspec.args[0] : 0;
3346 count = 0;
3347
3348 /* if there is an indices property, use it to transfer the index
3349 * specified into an array offset for the clock-output-names property.
3350 */
3351 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3352 if (index == pv) {
3353 index = count;
3354 break;
3355 }
3356 count++;
3357 }
3358 /* We went off the end of 'clock-indices' without finding it */
3359 if (prop && !vp)
3360 return NULL;
3361
3362 if (of_property_read_string_index(clkspec.np, "clock-output-names",
3363 index,
3364 &clk_name) < 0) {
3365 /*
3366 * Best effort to get the name if the clock has been
3367 * registered with the framework. If the clock isn't
3368 * registered, we return the node name as the name of
3369 * the clock as long as #clock-cells = 0.
3370 */
3371 clk = of_clk_get_from_provider(&clkspec);
3372 if (IS_ERR(clk)) {
3373 if (clkspec.args_count == 0)
3374 clk_name = clkspec.np->name;
3375 else
3376 clk_name = NULL;
3377 } else {
3378 clk_name = __clk_get_name(clk);
3379 clk_put(clk);
3380 }
3381 }
3382
3383
3384 of_node_put(clkspec.np);
3385 return clk_name;
3386}
3387EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3388
3389/**
3390 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3391 * number of parents
3392 * @np: Device node pointer associated with clock provider
3393 * @parents: pointer to char array that hold the parents' names
3394 * @size: size of the @parents array
3395 *
3396 * Return: number of parents for the clock node.
3397 */
3398int of_clk_parent_fill(struct device_node *np, const char **parents,
3399 unsigned int size)
3400{
3401 unsigned int i = 0;
3402
3403 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3404 i++;
3405
3406 return i;
3407}
3408EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3409
3410struct clock_provider {
3411 of_clk_init_cb_t clk_init_cb;
3412 struct device_node *np;
3413 struct list_head node;
3414};
3415
3416/*
3417 * This function looks for a parent clock. If there is one, then it
3418 * checks that the provider for this parent clock was initialized, in
3419 * this case the parent clock will be ready.
3420 */
3421static int parent_ready(struct device_node *np)
3422{
3423 int i = 0;
3424
3425 while (true) {
3426 struct clk *clk = of_clk_get(np, i);
3427
3428 /* this parent is ready we can check the next one */
3429 if (!IS_ERR(clk)) {
3430 clk_put(clk);
3431 i++;
3432 continue;
3433 }
3434
3435 /* at least one parent is not ready, we exit now */
3436 if (PTR_ERR(clk) == -EPROBE_DEFER)
3437 return 0;
3438
3439 /*
3440 * Here we make assumption that the device tree is
3441 * written correctly. So an error means that there is
3442 * no more parent. As we didn't exit yet, then the
3443 * previous parent are ready. If there is no clock
3444 * parent, no need to wait for them, then we can
3445 * consider their absence as being ready
3446 */
3447 return 1;
3448 }
3449}
3450
3451/**
3452 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3453 * @np: Device node pointer associated with clock provider
3454 * @index: clock index
3455 * @flags: pointer to clk_core->flags
3456 *
3457 * Detects if the clock-critical property exists and, if so, sets the
3458 * corresponding CLK_IS_CRITICAL flag.
3459 *
3460 * Do not use this function. It exists only for legacy Device Tree
3461 * bindings, such as the one-clock-per-node style that are outdated.
3462 * Those bindings typically put all clock data into .dts and the Linux
3463 * driver has no clock data, thus making it impossible to set this flag
3464 * correctly from the driver. Only those drivers may call
3465 * of_clk_detect_critical from their setup functions.
3466 *
3467 * Return: error code or zero on success
3468 */
3469int of_clk_detect_critical(struct device_node *np,
3470 int index, unsigned long *flags)
3471{
3472 struct property *prop;
3473 const __be32 *cur;
3474 uint32_t idx;
3475
3476 if (!np || !flags)
3477 return -EINVAL;
3478
3479 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
3480 if (index == idx)
3481 *flags |= CLK_IS_CRITICAL;
3482
3483 return 0;
3484}
3485
3486/**
3487 * of_clk_init() - Scan and init clock providers from the DT
3488 * @matches: array of compatible values and init functions for providers.
3489 *
3490 * This function scans the device tree for matching clock providers
3491 * and calls their initialization functions. It also does it by trying
3492 * to follow the dependencies.
3493 */
3494void __init of_clk_init(const struct of_device_id *matches)
3495{
3496 const struct of_device_id *match;
3497 struct device_node *np;
3498 struct clock_provider *clk_provider, *next;
3499 bool is_init_done;
3500 bool force = false;
3501 LIST_HEAD(clk_provider_list);
3502
3503 if (!matches)
3504 matches = &__clk_of_table;
3505
3506 /* First prepare the list of the clocks providers */
3507 for_each_matching_node_and_match(np, matches, &match) {
3508 struct clock_provider *parent;
3509
3510 if (!of_device_is_available(np))
3511 continue;
3512
3513 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
3514 if (!parent) {
3515 list_for_each_entry_safe(clk_provider, next,
3516 &clk_provider_list, node) {
3517 list_del(&clk_provider->node);
3518 of_node_put(clk_provider->np);
3519 kfree(clk_provider);
3520 }
3521 of_node_put(np);
3522 return;
3523 }
3524
3525 parent->clk_init_cb = match->data;
3526 parent->np = of_node_get(np);
3527 list_add_tail(&parent->node, &clk_provider_list);
3528 }
3529
3530 while (!list_empty(&clk_provider_list)) {
3531 is_init_done = false;
3532 list_for_each_entry_safe(clk_provider, next,
3533 &clk_provider_list, node) {
3534 if (force || parent_ready(clk_provider->np)) {
3535
3536 /* Don't populate platform devices */
3537 of_node_set_flag(clk_provider->np,
3538 OF_POPULATED);
3539
3540 clk_provider->clk_init_cb(clk_provider->np);
3541 of_clk_set_defaults(clk_provider->np, true);
3542
3543 list_del(&clk_provider->node);
3544 of_node_put(clk_provider->np);
3545 kfree(clk_provider);
3546 is_init_done = true;
3547 }
3548 }
3549
3550 /*
3551 * We didn't manage to initialize any of the
3552 * remaining providers during the last loop, so now we
3553 * initialize all the remaining ones unconditionally
3554 * in case the clock parent was not mandatory
3555 */
3556 if (!is_init_done)
3557 force = true;
3558 }
3559}
3560#endif