1
2
3
4
5
6
7
8
9
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/pm_runtime.h>
25#include <linux/sched.h>
26#include <linux/clkdev.h>
27
28#include "clk.h"
29
30static DEFINE_SPINLOCK(enable_lock);
31static DEFINE_MUTEX(prepare_lock);
32
33static struct task_struct *prepare_owner;
34static struct task_struct *enable_owner;
35
36static int prepare_refcnt;
37static int enable_refcnt;
38
39static HLIST_HEAD(clk_root_list);
40static HLIST_HEAD(clk_orphan_list);
41static LIST_HEAD(clk_notifier_list);
42
43
44
45struct clk_core {
46 const char *name;
47 const struct clk_ops *ops;
48 struct clk_hw *hw;
49 struct module *owner;
50 struct device *dev;
51 struct clk_core *parent;
52 const char **parent_names;
53 struct clk_core **parents;
54 u8 num_parents;
55 u8 new_parent_index;
56 unsigned long rate;
57 unsigned long req_rate;
58 unsigned long new_rate;
59 struct clk_core *new_parent;
60 struct clk_core *new_child;
61 unsigned long flags;
62 bool orphan;
63 unsigned int enable_count;
64 unsigned int prepare_count;
65 unsigned int protect_count;
66 unsigned long min_rate;
67 unsigned long max_rate;
68 unsigned long accuracy;
69 int phase;
70 struct hlist_head children;
71 struct hlist_node child_node;
72 struct hlist_head clks;
73 unsigned int notifier_count;
74#ifdef CONFIG_DEBUG_FS
75 struct dentry *dentry;
76 struct hlist_node debug_node;
77#endif
78 struct kref ref;
79};
80
81#define CREATE_TRACE_POINTS
82#include <trace/events/clk.h>
83
84struct clk {
85 struct clk_core *core;
86 const char *dev_id;
87 const char *con_id;
88 unsigned long min_rate;
89 unsigned long max_rate;
90 unsigned int exclusive_count;
91 struct hlist_node clks_node;
92};
93
94
95static int clk_pm_runtime_get(struct clk_core *core)
96{
97 int ret = 0;
98
99 if (!core->dev)
100 return 0;
101
102 ret = pm_runtime_get_sync(core->dev);
103 return ret < 0 ? ret : 0;
104}
105
106static void clk_pm_runtime_put(struct clk_core *core)
107{
108 if (!core->dev)
109 return;
110
111 pm_runtime_put_sync(core->dev);
112}
113
114
115static void clk_prepare_lock(void)
116{
117 if (!mutex_trylock(&prepare_lock)) {
118 if (prepare_owner == current) {
119 prepare_refcnt++;
120 return;
121 }
122 mutex_lock(&prepare_lock);
123 }
124 WARN_ON_ONCE(prepare_owner != NULL);
125 WARN_ON_ONCE(prepare_refcnt != 0);
126 prepare_owner = current;
127 prepare_refcnt = 1;
128}
129
130static void clk_prepare_unlock(void)
131{
132 WARN_ON_ONCE(prepare_owner != current);
133 WARN_ON_ONCE(prepare_refcnt == 0);
134
135 if (--prepare_refcnt)
136 return;
137 prepare_owner = NULL;
138 mutex_unlock(&prepare_lock);
139}
140
141static unsigned long clk_enable_lock(void)
142 __acquires(enable_lock)
143{
144 unsigned long flags;
145
146
147
148
149
150
151 if (!IS_ENABLED(CONFIG_SMP) ||
152 !spin_trylock_irqsave(&enable_lock, flags)) {
153 if (enable_owner == current) {
154 enable_refcnt++;
155 __acquire(enable_lock);
156 if (!IS_ENABLED(CONFIG_SMP))
157 local_save_flags(flags);
158 return flags;
159 }
160 spin_lock_irqsave(&enable_lock, flags);
161 }
162 WARN_ON_ONCE(enable_owner != NULL);
163 WARN_ON_ONCE(enable_refcnt != 0);
164 enable_owner = current;
165 enable_refcnt = 1;
166 return flags;
167}
168
169static void clk_enable_unlock(unsigned long flags)
170 __releases(enable_lock)
171{
172 WARN_ON_ONCE(enable_owner != current);
173 WARN_ON_ONCE(enable_refcnt == 0);
174
175 if (--enable_refcnt) {
176 __release(enable_lock);
177 return;
178 }
179 enable_owner = NULL;
180 spin_unlock_irqrestore(&enable_lock, flags);
181}
182
183static bool clk_core_rate_is_protected(struct clk_core *core)
184{
185 return core->protect_count;
186}
187
188static bool clk_core_is_prepared(struct clk_core *core)
189{
190 bool ret = false;
191
192
193
194
195
196 if (!core->ops->is_prepared)
197 return core->prepare_count;
198
199 if (!clk_pm_runtime_get(core)) {
200 ret = core->ops->is_prepared(core->hw);
201 clk_pm_runtime_put(core);
202 }
203
204 return ret;
205}
206
207static bool clk_core_is_enabled(struct clk_core *core)
208{
209 bool ret = false;
210
211
212
213
214
215 if (!core->ops->is_enabled)
216 return core->enable_count;
217
218
219
220
221
222
223
224
225
226
227
228 if (core->dev) {
229 pm_runtime_get_noresume(core->dev);
230 if (!pm_runtime_active(core->dev)) {
231 ret = false;
232 goto done;
233 }
234 }
235
236 ret = core->ops->is_enabled(core->hw);
237done:
238 if (core->dev)
239 pm_runtime_put(core->dev);
240
241 return ret;
242}
243
244
245
246const char *__clk_get_name(const struct clk *clk)
247{
248 return !clk ? NULL : clk->core->name;
249}
250EXPORT_SYMBOL_GPL(__clk_get_name);
251
252const char *clk_hw_get_name(const struct clk_hw *hw)
253{
254 return hw->core->name;
255}
256EXPORT_SYMBOL_GPL(clk_hw_get_name);
257
258struct clk_hw *__clk_get_hw(struct clk *clk)
259{
260 return !clk ? NULL : clk->core->hw;
261}
262EXPORT_SYMBOL_GPL(__clk_get_hw);
263
264unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
265{
266 return hw->core->num_parents;
267}
268EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
269
270struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
271{
272 return hw->core->parent ? hw->core->parent->hw : NULL;
273}
274EXPORT_SYMBOL_GPL(clk_hw_get_parent);
275
276static struct clk_core *__clk_lookup_subtree(const char *name,
277 struct clk_core *core)
278{
279 struct clk_core *child;
280 struct clk_core *ret;
281
282 if (!strcmp(core->name, name))
283 return core;
284
285 hlist_for_each_entry(child, &core->children, child_node) {
286 ret = __clk_lookup_subtree(name, child);
287 if (ret)
288 return ret;
289 }
290
291 return NULL;
292}
293
294static struct clk_core *clk_core_lookup(const char *name)
295{
296 struct clk_core *root_clk;
297 struct clk_core *ret;
298
299 if (!name)
300 return NULL;
301
302
303 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
304 ret = __clk_lookup_subtree(name, root_clk);
305 if (ret)
306 return ret;
307 }
308
309
310 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
311 ret = __clk_lookup_subtree(name, root_clk);
312 if (ret)
313 return ret;
314 }
315
316 return NULL;
317}
318
319static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
320 u8 index)
321{
322 if (!core || index >= core->num_parents)
323 return NULL;
324
325 if (!core->parents[index])
326 core->parents[index] =
327 clk_core_lookup(core->parent_names[index]);
328
329 return core->parents[index];
330}
331
332struct clk_hw *
333clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
334{
335 struct clk_core *parent;
336
337 parent = clk_core_get_parent_by_index(hw->core, index);
338
339 return !parent ? NULL : parent->hw;
340}
341EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
342
343unsigned int __clk_get_enable_count(struct clk *clk)
344{
345 return !clk ? 0 : clk->core->enable_count;
346}
347
348static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
349{
350 unsigned long ret;
351
352 if (!core) {
353 ret = 0;
354 goto out;
355 }
356
357 ret = core->rate;
358
359 if (!core->num_parents)
360 goto out;
361
362 if (!core->parent)
363 ret = 0;
364
365out:
366 return ret;
367}
368
369unsigned long clk_hw_get_rate(const struct clk_hw *hw)
370{
371 return clk_core_get_rate_nolock(hw->core);
372}
373EXPORT_SYMBOL_GPL(clk_hw_get_rate);
374
375static unsigned long __clk_get_accuracy(struct clk_core *core)
376{
377 if (!core)
378 return 0;
379
380 return core->accuracy;
381}
382
383unsigned long __clk_get_flags(struct clk *clk)
384{
385 return !clk ? 0 : clk->core->flags;
386}
387EXPORT_SYMBOL_GPL(__clk_get_flags);
388
389unsigned long clk_hw_get_flags(const struct clk_hw *hw)
390{
391 return hw->core->flags;
392}
393EXPORT_SYMBOL_GPL(clk_hw_get_flags);
394
395bool clk_hw_is_prepared(const struct clk_hw *hw)
396{
397 return clk_core_is_prepared(hw->core);
398}
399
400bool clk_hw_rate_is_protected(const struct clk_hw *hw)
401{
402 return clk_core_rate_is_protected(hw->core);
403}
404
405bool clk_hw_is_enabled(const struct clk_hw *hw)
406{
407 return clk_core_is_enabled(hw->core);
408}
409
410bool __clk_is_enabled(struct clk *clk)
411{
412 if (!clk)
413 return false;
414
415 return clk_core_is_enabled(clk->core);
416}
417EXPORT_SYMBOL_GPL(__clk_is_enabled);
418
419static bool mux_is_better_rate(unsigned long rate, unsigned long now,
420 unsigned long best, unsigned long flags)
421{
422 if (flags & CLK_MUX_ROUND_CLOSEST)
423 return abs(now - rate) < abs(best - rate);
424
425 return now <= rate && now > best;
426}
427
428int clk_mux_determine_rate_flags(struct clk_hw *hw,
429 struct clk_rate_request *req,
430 unsigned long flags)
431{
432 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
433 int i, num_parents, ret;
434 unsigned long best = 0;
435 struct clk_rate_request parent_req = *req;
436
437
438 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
439 parent = core->parent;
440 if (core->flags & CLK_SET_RATE_PARENT) {
441 ret = __clk_determine_rate(parent ? parent->hw : NULL,
442 &parent_req);
443 if (ret)
444 return ret;
445
446 best = parent_req.rate;
447 } else if (parent) {
448 best = clk_core_get_rate_nolock(parent);
449 } else {
450 best = clk_core_get_rate_nolock(core);
451 }
452
453 goto out;
454 }
455
456
457 num_parents = core->num_parents;
458 for (i = 0; i < num_parents; i++) {
459 parent = clk_core_get_parent_by_index(core, i);
460 if (!parent)
461 continue;
462
463 if (core->flags & CLK_SET_RATE_PARENT) {
464 parent_req = *req;
465 ret = __clk_determine_rate(parent->hw, &parent_req);
466 if (ret)
467 continue;
468 } else {
469 parent_req.rate = clk_core_get_rate_nolock(parent);
470 }
471
472 if (mux_is_better_rate(req->rate, parent_req.rate,
473 best, flags)) {
474 best_parent = parent;
475 best = parent_req.rate;
476 }
477 }
478
479 if (!best_parent)
480 return -EINVAL;
481
482out:
483 if (best_parent)
484 req->best_parent_hw = best_parent->hw;
485 req->best_parent_rate = best;
486 req->rate = best;
487
488 return 0;
489}
490EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
491
492struct clk *__clk_lookup(const char *name)
493{
494 struct clk_core *core = clk_core_lookup(name);
495
496 return !core ? NULL : core->hw->clk;
497}
498
499static void clk_core_get_boundaries(struct clk_core *core,
500 unsigned long *min_rate,
501 unsigned long *max_rate)
502{
503 struct clk *clk_user;
504
505 *min_rate = core->min_rate;
506 *max_rate = core->max_rate;
507
508 hlist_for_each_entry(clk_user, &core->clks, clks_node)
509 *min_rate = max(*min_rate, clk_user->min_rate);
510
511 hlist_for_each_entry(clk_user, &core->clks, clks_node)
512 *max_rate = min(*max_rate, clk_user->max_rate);
513}
514
515void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
516 unsigned long max_rate)
517{
518 hw->core->min_rate = min_rate;
519 hw->core->max_rate = max_rate;
520}
521EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
522
523
524
525
526
527
528int __clk_mux_determine_rate(struct clk_hw *hw,
529 struct clk_rate_request *req)
530{
531 return clk_mux_determine_rate_flags(hw, req, 0);
532}
533EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
534
535int __clk_mux_determine_rate_closest(struct clk_hw *hw,
536 struct clk_rate_request *req)
537{
538 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
539}
540EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
541
542
543
544static void clk_core_rate_unprotect(struct clk_core *core)
545{
546 lockdep_assert_held(&prepare_lock);
547
548 if (!core)
549 return;
550
551 if (WARN(core->protect_count == 0,
552 "%s already unprotected\n", core->name))
553 return;
554
555 if (--core->protect_count > 0)
556 return;
557
558 clk_core_rate_unprotect(core->parent);
559}
560
561static int clk_core_rate_nuke_protect(struct clk_core *core)
562{
563 int ret;
564
565 lockdep_assert_held(&prepare_lock);
566
567 if (!core)
568 return -EINVAL;
569
570 if (core->protect_count == 0)
571 return 0;
572
573 ret = core->protect_count;
574 core->protect_count = 1;
575 clk_core_rate_unprotect(core);
576
577 return ret;
578}
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598void clk_rate_exclusive_put(struct clk *clk)
599{
600 if (!clk)
601 return;
602
603 clk_prepare_lock();
604
605
606
607
608
609 if (WARN_ON(clk->exclusive_count <= 0))
610 goto out;
611
612 clk_core_rate_unprotect(clk->core);
613 clk->exclusive_count--;
614out:
615 clk_prepare_unlock();
616}
617EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
618
619static void clk_core_rate_protect(struct clk_core *core)
620{
621 lockdep_assert_held(&prepare_lock);
622
623 if (!core)
624 return;
625
626 if (core->protect_count == 0)
627 clk_core_rate_protect(core->parent);
628
629 core->protect_count++;
630}
631
632static void clk_core_rate_restore_protect(struct clk_core *core, int count)
633{
634 lockdep_assert_held(&prepare_lock);
635
636 if (!core)
637 return;
638
639 if (count == 0)
640 return;
641
642 clk_core_rate_protect(core);
643 core->protect_count = count;
644}
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664int clk_rate_exclusive_get(struct clk *clk)
665{
666 if (!clk)
667 return 0;
668
669 clk_prepare_lock();
670 clk_core_rate_protect(clk->core);
671 clk->exclusive_count++;
672 clk_prepare_unlock();
673
674 return 0;
675}
676EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
677
678static void clk_core_unprepare(struct clk_core *core)
679{
680 lockdep_assert_held(&prepare_lock);
681
682 if (!core)
683 return;
684
685 if (WARN(core->prepare_count == 0,
686 "%s already unprepared\n", core->name))
687 return;
688
689 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
690 "Unpreparing critical %s\n", core->name))
691 return;
692
693 if (--core->prepare_count > 0)
694 return;
695
696 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
697
698 trace_clk_unprepare(core);
699
700 if (core->ops->unprepare)
701 core->ops->unprepare(core->hw);
702
703 clk_pm_runtime_put(core);
704
705 trace_clk_unprepare_complete(core);
706 clk_core_unprepare(core->parent);
707}
708
709static void clk_core_unprepare_lock(struct clk_core *core)
710{
711 clk_prepare_lock();
712 clk_core_unprepare(core);
713 clk_prepare_unlock();
714}
715
716
717
718
719
720
721
722
723
724
725
726
727void clk_unprepare(struct clk *clk)
728{
729 if (IS_ERR_OR_NULL(clk))
730 return;
731
732 clk_core_unprepare_lock(clk->core);
733}
734EXPORT_SYMBOL_GPL(clk_unprepare);
735
736static int clk_core_prepare(struct clk_core *core)
737{
738 int ret = 0;
739
740 lockdep_assert_held(&prepare_lock);
741
742 if (!core)
743 return 0;
744
745 if (core->prepare_count == 0) {
746 ret = clk_pm_runtime_get(core);
747 if (ret)
748 return ret;
749
750 ret = clk_core_prepare(core->parent);
751 if (ret)
752 goto runtime_put;
753
754 trace_clk_prepare(core);
755
756 if (core->ops->prepare)
757 ret = core->ops->prepare(core->hw);
758
759 trace_clk_prepare_complete(core);
760
761 if (ret)
762 goto unprepare;
763 }
764
765 core->prepare_count++;
766
767 return 0;
768unprepare:
769 clk_core_unprepare(core->parent);
770runtime_put:
771 clk_pm_runtime_put(core);
772 return ret;
773}
774
775static int clk_core_prepare_lock(struct clk_core *core)
776{
777 int ret;
778
779 clk_prepare_lock();
780 ret = clk_core_prepare(core);
781 clk_prepare_unlock();
782
783 return ret;
784}
785
786
787
788
789
790
791
792
793
794
795
796
797
798int clk_prepare(struct clk *clk)
799{
800 if (!clk)
801 return 0;
802
803 return clk_core_prepare_lock(clk->core);
804}
805EXPORT_SYMBOL_GPL(clk_prepare);
806
807static void clk_core_disable(struct clk_core *core)
808{
809 lockdep_assert_held(&enable_lock);
810
811 if (!core)
812 return;
813
814 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
815 return;
816
817 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
818 "Disabling critical %s\n", core->name))
819 return;
820
821 if (--core->enable_count > 0)
822 return;
823
824 trace_clk_disable_rcuidle(core);
825
826 if (core->ops->disable)
827 core->ops->disable(core->hw);
828
829 trace_clk_disable_complete_rcuidle(core);
830
831 clk_core_disable(core->parent);
832}
833
834static void clk_core_disable_lock(struct clk_core *core)
835{
836 unsigned long flags;
837
838 flags = clk_enable_lock();
839 clk_core_disable(core);
840 clk_enable_unlock(flags);
841}
842
843
844
845
846
847
848
849
850
851
852
853
854
855void clk_disable(struct clk *clk)
856{
857 if (IS_ERR_OR_NULL(clk))
858 return;
859
860 clk_core_disable_lock(clk->core);
861}
862EXPORT_SYMBOL_GPL(clk_disable);
863
864static int clk_core_enable(struct clk_core *core)
865{
866 int ret = 0;
867
868 lockdep_assert_held(&enable_lock);
869
870 if (!core)
871 return 0;
872
873 if (WARN(core->prepare_count == 0,
874 "Enabling unprepared %s\n", core->name))
875 return -ESHUTDOWN;
876
877 if (core->enable_count == 0) {
878 ret = clk_core_enable(core->parent);
879
880 if (ret)
881 return ret;
882
883 trace_clk_enable_rcuidle(core);
884
885 if (core->ops->enable)
886 ret = core->ops->enable(core->hw);
887
888 trace_clk_enable_complete_rcuidle(core);
889
890 if (ret) {
891 clk_core_disable(core->parent);
892 return ret;
893 }
894 }
895
896 core->enable_count++;
897 return 0;
898}
899
900static int clk_core_enable_lock(struct clk_core *core)
901{
902 unsigned long flags;
903 int ret;
904
905 flags = clk_enable_lock();
906 ret = clk_core_enable(core);
907 clk_enable_unlock(flags);
908
909 return ret;
910}
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925int clk_enable(struct clk *clk)
926{
927 if (!clk)
928 return 0;
929
930 return clk_core_enable_lock(clk->core);
931}
932EXPORT_SYMBOL_GPL(clk_enable);
933
934static int clk_core_prepare_enable(struct clk_core *core)
935{
936 int ret;
937
938 ret = clk_core_prepare_lock(core);
939 if (ret)
940 return ret;
941
942 ret = clk_core_enable_lock(core);
943 if (ret)
944 clk_core_unprepare_lock(core);
945
946 return ret;
947}
948
949static void clk_core_disable_unprepare(struct clk_core *core)
950{
951 clk_core_disable_lock(core);
952 clk_core_unprepare_lock(core);
953}
954
955static void clk_unprepare_unused_subtree(struct clk_core *core)
956{
957 struct clk_core *child;
958
959 lockdep_assert_held(&prepare_lock);
960
961 hlist_for_each_entry(child, &core->children, child_node)
962 clk_unprepare_unused_subtree(child);
963
964 if (core->prepare_count)
965 return;
966
967 if (core->flags & CLK_IGNORE_UNUSED)
968 return;
969
970 if (clk_pm_runtime_get(core))
971 return;
972
973 if (clk_core_is_prepared(core)) {
974 trace_clk_unprepare(core);
975 if (core->ops->unprepare_unused)
976 core->ops->unprepare_unused(core->hw);
977 else if (core->ops->unprepare)
978 core->ops->unprepare(core->hw);
979 trace_clk_unprepare_complete(core);
980 }
981
982 clk_pm_runtime_put(core);
983}
984
985static void clk_disable_unused_subtree(struct clk_core *core)
986{
987 struct clk_core *child;
988 unsigned long flags;
989
990 lockdep_assert_held(&prepare_lock);
991
992 hlist_for_each_entry(child, &core->children, child_node)
993 clk_disable_unused_subtree(child);
994
995 if (core->flags & CLK_OPS_PARENT_ENABLE)
996 clk_core_prepare_enable(core->parent);
997
998 if (clk_pm_runtime_get(core))
999 goto unprepare_out;
1000
1001 flags = clk_enable_lock();
1002
1003 if (core->enable_count)
1004 goto unlock_out;
1005
1006 if (core->flags & CLK_IGNORE_UNUSED)
1007 goto unlock_out;
1008
1009
1010
1011
1012
1013
1014 if (clk_core_is_enabled(core)) {
1015 trace_clk_disable(core);
1016 if (core->ops->disable_unused)
1017 core->ops->disable_unused(core->hw);
1018 else if (core->ops->disable)
1019 core->ops->disable(core->hw);
1020 trace_clk_disable_complete(core);
1021 }
1022
1023unlock_out:
1024 clk_enable_unlock(flags);
1025 clk_pm_runtime_put(core);
1026unprepare_out:
1027 if (core->flags & CLK_OPS_PARENT_ENABLE)
1028 clk_core_disable_unprepare(core->parent);
1029}
1030
1031static bool clk_ignore_unused;
1032static int __init clk_ignore_unused_setup(char *__unused)
1033{
1034 clk_ignore_unused = true;
1035 return 1;
1036}
1037__setup("clk_ignore_unused", clk_ignore_unused_setup);
1038
1039static int clk_disable_unused(void)
1040{
1041 struct clk_core *core;
1042
1043 if (clk_ignore_unused) {
1044 pr_warn("clk: Not disabling unused clocks\n");
1045 return 0;
1046 }
1047
1048 clk_prepare_lock();
1049
1050 hlist_for_each_entry(core, &clk_root_list, child_node)
1051 clk_disable_unused_subtree(core);
1052
1053 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1054 clk_disable_unused_subtree(core);
1055
1056 hlist_for_each_entry(core, &clk_root_list, child_node)
1057 clk_unprepare_unused_subtree(core);
1058
1059 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1060 clk_unprepare_unused_subtree(core);
1061
1062 clk_prepare_unlock();
1063
1064 return 0;
1065}
1066late_initcall_sync(clk_disable_unused);
1067
1068static int clk_core_determine_round_nolock(struct clk_core *core,
1069 struct clk_rate_request *req)
1070{
1071 long rate;
1072
1073 lockdep_assert_held(&prepare_lock);
1074
1075 if (!core)
1076 return 0;
1077
1078
1079
1080
1081
1082
1083
1084 if (clk_core_rate_is_protected(core)) {
1085 req->rate = core->rate;
1086 } else if (core->ops->determine_rate) {
1087 return core->ops->determine_rate(core->hw, req);
1088 } else if (core->ops->round_rate) {
1089 rate = core->ops->round_rate(core->hw, req->rate,
1090 &req->best_parent_rate);
1091 if (rate < 0)
1092 return rate;
1093
1094 req->rate = rate;
1095 } else {
1096 return -EINVAL;
1097 }
1098
1099 return 0;
1100}
1101
1102static void clk_core_init_rate_req(struct clk_core * const core,
1103 struct clk_rate_request *req)
1104{
1105 struct clk_core *parent;
1106
1107 if (WARN_ON(!core || !req))
1108 return;
1109
1110 parent = core->parent;
1111 if (parent) {
1112 req->best_parent_hw = parent->hw;
1113 req->best_parent_rate = parent->rate;
1114 } else {
1115 req->best_parent_hw = NULL;
1116 req->best_parent_rate = 0;
1117 }
1118}
1119
1120static bool clk_core_can_round(struct clk_core * const core)
1121{
1122 if (core->ops->determine_rate || core->ops->round_rate)
1123 return true;
1124
1125 return false;
1126}
1127
1128static int clk_core_round_rate_nolock(struct clk_core *core,
1129 struct clk_rate_request *req)
1130{
1131 lockdep_assert_held(&prepare_lock);
1132
1133 if (!core) {
1134 req->rate = 0;
1135 return 0;
1136 }
1137
1138 clk_core_init_rate_req(core, req);
1139
1140 if (clk_core_can_round(core))
1141 return clk_core_determine_round_nolock(core, req);
1142 else if (core->flags & CLK_SET_RATE_PARENT)
1143 return clk_core_round_rate_nolock(core->parent, req);
1144
1145 req->rate = core->rate;
1146 return 0;
1147}
1148
1149
1150
1151
1152
1153
1154
1155
1156int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
1157{
1158 if (!hw) {
1159 req->rate = 0;
1160 return 0;
1161 }
1162
1163 return clk_core_round_rate_nolock(hw->core, req);
1164}
1165EXPORT_SYMBOL_GPL(__clk_determine_rate);
1166
1167unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1168{
1169 int ret;
1170 struct clk_rate_request req;
1171
1172 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1173 req.rate = rate;
1174
1175 ret = clk_core_round_rate_nolock(hw->core, &req);
1176 if (ret)
1177 return 0;
1178
1179 return req.rate;
1180}
1181EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192long clk_round_rate(struct clk *clk, unsigned long rate)
1193{
1194 struct clk_rate_request req;
1195 int ret;
1196
1197 if (!clk)
1198 return 0;
1199
1200 clk_prepare_lock();
1201
1202 if (clk->exclusive_count)
1203 clk_core_rate_unprotect(clk->core);
1204
1205 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1206 req.rate = rate;
1207
1208 ret = clk_core_round_rate_nolock(clk->core, &req);
1209
1210 if (clk->exclusive_count)
1211 clk_core_rate_protect(clk->core);
1212
1213 clk_prepare_unlock();
1214
1215 if (ret)
1216 return ret;
1217
1218 return req.rate;
1219}
1220EXPORT_SYMBOL_GPL(clk_round_rate);
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236static int __clk_notify(struct clk_core *core, unsigned long msg,
1237 unsigned long old_rate, unsigned long new_rate)
1238{
1239 struct clk_notifier *cn;
1240 struct clk_notifier_data cnd;
1241 int ret = NOTIFY_DONE;
1242
1243 cnd.old_rate = old_rate;
1244 cnd.new_rate = new_rate;
1245
1246 list_for_each_entry(cn, &clk_notifier_list, node) {
1247 if (cn->clk->core == core) {
1248 cnd.clk = cn->clk;
1249 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1250 &cnd);
1251 if (ret & NOTIFY_STOP_MASK)
1252 return ret;
1253 }
1254 }
1255
1256 return ret;
1257}
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268static void __clk_recalc_accuracies(struct clk_core *core)
1269{
1270 unsigned long parent_accuracy = 0;
1271 struct clk_core *child;
1272
1273 lockdep_assert_held(&prepare_lock);
1274
1275 if (core->parent)
1276 parent_accuracy = core->parent->accuracy;
1277
1278 if (core->ops->recalc_accuracy)
1279 core->accuracy = core->ops->recalc_accuracy(core->hw,
1280 parent_accuracy);
1281 else
1282 core->accuracy = parent_accuracy;
1283
1284 hlist_for_each_entry(child, &core->children, child_node)
1285 __clk_recalc_accuracies(child);
1286}
1287
1288static long clk_core_get_accuracy(struct clk_core *core)
1289{
1290 unsigned long accuracy;
1291
1292 clk_prepare_lock();
1293 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1294 __clk_recalc_accuracies(core);
1295
1296 accuracy = __clk_get_accuracy(core);
1297 clk_prepare_unlock();
1298
1299 return accuracy;
1300}
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311long clk_get_accuracy(struct clk *clk)
1312{
1313 if (!clk)
1314 return 0;
1315
1316 return clk_core_get_accuracy(clk->core);
1317}
1318EXPORT_SYMBOL_GPL(clk_get_accuracy);
1319
1320static unsigned long clk_recalc(struct clk_core *core,
1321 unsigned long parent_rate)
1322{
1323 unsigned long rate = parent_rate;
1324
1325 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1326 rate = core->ops->recalc_rate(core->hw, parent_rate);
1327 clk_pm_runtime_put(core);
1328 }
1329 return rate;
1330}
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1345{
1346 unsigned long old_rate;
1347 unsigned long parent_rate = 0;
1348 struct clk_core *child;
1349
1350 lockdep_assert_held(&prepare_lock);
1351
1352 old_rate = core->rate;
1353
1354 if (core->parent)
1355 parent_rate = core->parent->rate;
1356
1357 core->rate = clk_recalc(core, parent_rate);
1358
1359
1360
1361
1362
1363 if (core->notifier_count && msg)
1364 __clk_notify(core, msg, old_rate, core->rate);
1365
1366 hlist_for_each_entry(child, &core->children, child_node)
1367 __clk_recalc_rates(child, msg);
1368}
1369
1370static unsigned long clk_core_get_rate(struct clk_core *core)
1371{
1372 unsigned long rate;
1373
1374 clk_prepare_lock();
1375
1376 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1377 __clk_recalc_rates(core, 0);
1378
1379 rate = clk_core_get_rate_nolock(core);
1380 clk_prepare_unlock();
1381
1382 return rate;
1383}
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393unsigned long clk_get_rate(struct clk *clk)
1394{
1395 if (!clk)
1396 return 0;
1397
1398 return clk_core_get_rate(clk->core);
1399}
1400EXPORT_SYMBOL_GPL(clk_get_rate);
1401
1402static int clk_fetch_parent_index(struct clk_core *core,
1403 struct clk_core *parent)
1404{
1405 int i;
1406
1407 if (!parent)
1408 return -EINVAL;
1409
1410 for (i = 0; i < core->num_parents; i++)
1411 if (clk_core_get_parent_by_index(core, i) == parent)
1412 return i;
1413
1414 return -EINVAL;
1415}
1416
1417
1418
1419
1420static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1421{
1422 struct clk_core *child;
1423
1424 core->orphan = is_orphan;
1425
1426 hlist_for_each_entry(child, &core->children, child_node)
1427 clk_core_update_orphan_status(child, is_orphan);
1428}
1429
1430static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1431{
1432 bool was_orphan = core->orphan;
1433
1434 hlist_del(&core->child_node);
1435
1436 if (new_parent) {
1437 bool becomes_orphan = new_parent->orphan;
1438
1439
1440 if (new_parent->new_child == core)
1441 new_parent->new_child = NULL;
1442
1443 hlist_add_head(&core->child_node, &new_parent->children);
1444
1445 if (was_orphan != becomes_orphan)
1446 clk_core_update_orphan_status(core, becomes_orphan);
1447 } else {
1448 hlist_add_head(&core->child_node, &clk_orphan_list);
1449 if (!was_orphan)
1450 clk_core_update_orphan_status(core, true);
1451 }
1452
1453 core->parent = new_parent;
1454}
1455
1456static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1457 struct clk_core *parent)
1458{
1459 unsigned long flags;
1460 struct clk_core *old_parent = core->parent;
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1484 clk_core_prepare_enable(old_parent);
1485 clk_core_prepare_enable(parent);
1486 }
1487
1488
1489 if (core->prepare_count) {
1490 clk_core_prepare_enable(parent);
1491 clk_core_enable_lock(core);
1492 }
1493
1494
1495 flags = clk_enable_lock();
1496 clk_reparent(core, parent);
1497 clk_enable_unlock(flags);
1498
1499 return old_parent;
1500}
1501
1502static void __clk_set_parent_after(struct clk_core *core,
1503 struct clk_core *parent,
1504 struct clk_core *old_parent)
1505{
1506
1507
1508
1509
1510 if (core->prepare_count) {
1511 clk_core_disable_lock(core);
1512 clk_core_disable_unprepare(old_parent);
1513 }
1514
1515
1516 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1517 clk_core_disable_unprepare(parent);
1518 clk_core_disable_unprepare(old_parent);
1519 }
1520}
1521
1522static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1523 u8 p_index)
1524{
1525 unsigned long flags;
1526 int ret = 0;
1527 struct clk_core *old_parent;
1528
1529 old_parent = __clk_set_parent_before(core, parent);
1530
1531 trace_clk_set_parent(core, parent);
1532
1533
1534 if (parent && core->ops->set_parent)
1535 ret = core->ops->set_parent(core->hw, p_index);
1536
1537 trace_clk_set_parent_complete(core, parent);
1538
1539 if (ret) {
1540 flags = clk_enable_lock();
1541 clk_reparent(core, old_parent);
1542 clk_enable_unlock(flags);
1543 __clk_set_parent_after(core, old_parent, parent);
1544
1545 return ret;
1546 }
1547
1548 __clk_set_parent_after(core, parent, old_parent);
1549
1550 return 0;
1551}
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567static int __clk_speculate_rates(struct clk_core *core,
1568 unsigned long parent_rate)
1569{
1570 struct clk_core *child;
1571 unsigned long new_rate;
1572 int ret = NOTIFY_DONE;
1573
1574 lockdep_assert_held(&prepare_lock);
1575
1576 new_rate = clk_recalc(core, parent_rate);
1577
1578
1579 if (core->notifier_count)
1580 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1581
1582 if (ret & NOTIFY_STOP_MASK) {
1583 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1584 __func__, core->name, ret);
1585 goto out;
1586 }
1587
1588 hlist_for_each_entry(child, &core->children, child_node) {
1589 ret = __clk_speculate_rates(child, new_rate);
1590 if (ret & NOTIFY_STOP_MASK)
1591 break;
1592 }
1593
1594out:
1595 return ret;
1596}
1597
1598static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1599 struct clk_core *new_parent, u8 p_index)
1600{
1601 struct clk_core *child;
1602
1603 core->new_rate = new_rate;
1604 core->new_parent = new_parent;
1605 core->new_parent_index = p_index;
1606
1607 core->new_child = NULL;
1608 if (new_parent && new_parent != core->parent)
1609 new_parent->new_child = core;
1610
1611 hlist_for_each_entry(child, &core->children, child_node) {
1612 child->new_rate = clk_recalc(child, new_rate);
1613 clk_calc_subtree(child, child->new_rate, NULL, 0);
1614 }
1615}
1616
1617
1618
1619
1620
1621static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1622 unsigned long rate)
1623{
1624 struct clk_core *top = core;
1625 struct clk_core *old_parent, *parent;
1626 unsigned long best_parent_rate = 0;
1627 unsigned long new_rate;
1628 unsigned long min_rate;
1629 unsigned long max_rate;
1630 int p_index = 0;
1631 long ret;
1632
1633
1634 if (IS_ERR_OR_NULL(core))
1635 return NULL;
1636
1637
1638 parent = old_parent = core->parent;
1639 if (parent)
1640 best_parent_rate = parent->rate;
1641
1642 clk_core_get_boundaries(core, &min_rate, &max_rate);
1643
1644
1645 if (clk_core_can_round(core)) {
1646 struct clk_rate_request req;
1647
1648 req.rate = rate;
1649 req.min_rate = min_rate;
1650 req.max_rate = max_rate;
1651
1652 clk_core_init_rate_req(core, &req);
1653
1654 ret = clk_core_determine_round_nolock(core, &req);
1655 if (ret < 0)
1656 return NULL;
1657
1658 best_parent_rate = req.best_parent_rate;
1659 new_rate = req.rate;
1660 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1661
1662 if (new_rate < min_rate || new_rate > max_rate)
1663 return NULL;
1664 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1665
1666 core->new_rate = core->rate;
1667 return NULL;
1668 } else {
1669
1670 top = clk_calc_new_rates(parent, rate);
1671 new_rate = parent->new_rate;
1672 goto out;
1673 }
1674
1675
1676 if (parent != old_parent &&
1677 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1678 pr_debug("%s: %s not gated but wants to reparent\n",
1679 __func__, core->name);
1680 return NULL;
1681 }
1682
1683
1684 if (parent && core->num_parents > 1) {
1685 p_index = clk_fetch_parent_index(core, parent);
1686 if (p_index < 0) {
1687 pr_debug("%s: clk %s can not be parent of clk %s\n",
1688 __func__, parent->name, core->name);
1689 return NULL;
1690 }
1691 }
1692
1693 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1694 best_parent_rate != parent->rate)
1695 top = clk_calc_new_rates(parent, best_parent_rate);
1696
1697out:
1698 clk_calc_subtree(core, new_rate, parent, p_index);
1699
1700 return top;
1701}
1702
1703
1704
1705
1706
1707
1708static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
1709 unsigned long event)
1710{
1711 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
1712 int ret = NOTIFY_DONE;
1713
1714 if (core->rate == core->new_rate)
1715 return NULL;
1716
1717 if (core->notifier_count) {
1718 ret = __clk_notify(core, event, core->rate, core->new_rate);
1719 if (ret & NOTIFY_STOP_MASK)
1720 fail_clk = core;
1721 }
1722
1723 hlist_for_each_entry(child, &core->children, child_node) {
1724
1725 if (child->new_parent && child->new_parent != core)
1726 continue;
1727 tmp_clk = clk_propagate_rate_change(child, event);
1728 if (tmp_clk)
1729 fail_clk = tmp_clk;
1730 }
1731
1732
1733 if (core->new_child) {
1734 tmp_clk = clk_propagate_rate_change(core->new_child, event);
1735 if (tmp_clk)
1736 fail_clk = tmp_clk;
1737 }
1738
1739 return fail_clk;
1740}
1741
1742
1743
1744
1745
1746static void clk_change_rate(struct clk_core *core)
1747{
1748 struct clk_core *child;
1749 struct hlist_node *tmp;
1750 unsigned long old_rate;
1751 unsigned long best_parent_rate = 0;
1752 bool skip_set_rate = false;
1753 struct clk_core *old_parent;
1754 struct clk_core *parent = NULL;
1755
1756 old_rate = core->rate;
1757
1758 if (core->new_parent) {
1759 parent = core->new_parent;
1760 best_parent_rate = core->new_parent->rate;
1761 } else if (core->parent) {
1762 parent = core->parent;
1763 best_parent_rate = core->parent->rate;
1764 }
1765
1766 if (clk_pm_runtime_get(core))
1767 return;
1768
1769 if (core->flags & CLK_SET_RATE_UNGATE) {
1770 unsigned long flags;
1771
1772 clk_core_prepare(core);
1773 flags = clk_enable_lock();
1774 clk_core_enable(core);
1775 clk_enable_unlock(flags);
1776 }
1777
1778 if (core->new_parent && core->new_parent != core->parent) {
1779 old_parent = __clk_set_parent_before(core, core->new_parent);
1780 trace_clk_set_parent(core, core->new_parent);
1781
1782 if (core->ops->set_rate_and_parent) {
1783 skip_set_rate = true;
1784 core->ops->set_rate_and_parent(core->hw, core->new_rate,
1785 best_parent_rate,
1786 core->new_parent_index);
1787 } else if (core->ops->set_parent) {
1788 core->ops->set_parent(core->hw, core->new_parent_index);
1789 }
1790
1791 trace_clk_set_parent_complete(core, core->new_parent);
1792 __clk_set_parent_after(core, core->new_parent, old_parent);
1793 }
1794
1795 if (core->flags & CLK_OPS_PARENT_ENABLE)
1796 clk_core_prepare_enable(parent);
1797
1798 trace_clk_set_rate(core, core->new_rate);
1799
1800 if (!skip_set_rate && core->ops->set_rate)
1801 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
1802
1803 trace_clk_set_rate_complete(core, core->new_rate);
1804
1805 core->rate = clk_recalc(core, best_parent_rate);
1806
1807 if (core->flags & CLK_SET_RATE_UNGATE) {
1808 unsigned long flags;
1809
1810 flags = clk_enable_lock();
1811 clk_core_disable(core);
1812 clk_enable_unlock(flags);
1813 clk_core_unprepare(core);
1814 }
1815
1816 if (core->flags & CLK_OPS_PARENT_ENABLE)
1817 clk_core_disable_unprepare(parent);
1818
1819 if (core->notifier_count && old_rate != core->rate)
1820 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
1821
1822 if (core->flags & CLK_RECALC_NEW_RATES)
1823 (void)clk_calc_new_rates(core, core->new_rate);
1824
1825
1826
1827
1828
1829 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
1830
1831 if (child->new_parent && child->new_parent != core)
1832 continue;
1833 clk_change_rate(child);
1834 }
1835
1836
1837 if (core->new_child)
1838 clk_change_rate(core->new_child);
1839
1840 clk_pm_runtime_put(core);
1841}
1842
1843static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1844 unsigned long req_rate)
1845{
1846 int ret, cnt;
1847 struct clk_rate_request req;
1848
1849 lockdep_assert_held(&prepare_lock);
1850
1851 if (!core)
1852 return 0;
1853
1854
1855 cnt = clk_core_rate_nuke_protect(core);
1856 if (cnt < 0)
1857 return cnt;
1858
1859 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1860 req.rate = req_rate;
1861
1862 ret = clk_core_round_rate_nolock(core, &req);
1863
1864
1865 clk_core_rate_restore_protect(core, cnt);
1866
1867 return ret ? 0 : req.rate;
1868}
1869
1870static int clk_core_set_rate_nolock(struct clk_core *core,
1871 unsigned long req_rate)
1872{
1873 struct clk_core *top, *fail_clk;
1874 unsigned long rate;
1875 int ret = 0;
1876
1877 if (!core)
1878 return 0;
1879
1880 rate = clk_core_req_round_rate_nolock(core, req_rate);
1881
1882
1883 if (rate == clk_core_get_rate_nolock(core))
1884 return 0;
1885
1886
1887 if (clk_core_rate_is_protected(core))
1888 return -EBUSY;
1889
1890 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
1891 return -EBUSY;
1892
1893
1894 top = clk_calc_new_rates(core, req_rate);
1895 if (!top)
1896 return -EINVAL;
1897
1898 ret = clk_pm_runtime_get(core);
1899 if (ret)
1900 return ret;
1901
1902
1903 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1904 if (fail_clk) {
1905 pr_debug("%s: failed to set %s rate\n", __func__,
1906 fail_clk->name);
1907 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1908 ret = -EBUSY;
1909 goto err;
1910 }
1911
1912
1913 clk_change_rate(top);
1914
1915 core->req_rate = req_rate;
1916err:
1917 clk_pm_runtime_put(core);
1918
1919 return ret;
1920}
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943int clk_set_rate(struct clk *clk, unsigned long rate)
1944{
1945 int ret;
1946
1947 if (!clk)
1948 return 0;
1949
1950
1951 clk_prepare_lock();
1952
1953 if (clk->exclusive_count)
1954 clk_core_rate_unprotect(clk->core);
1955
1956 ret = clk_core_set_rate_nolock(clk->core, rate);
1957
1958 if (clk->exclusive_count)
1959 clk_core_rate_protect(clk->core);
1960
1961 clk_prepare_unlock();
1962
1963 return ret;
1964}
1965EXPORT_SYMBOL_GPL(clk_set_rate);
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
1987{
1988 int ret;
1989
1990 if (!clk)
1991 return 0;
1992
1993
1994 clk_prepare_lock();
1995
1996
1997
1998
1999
2000
2001
2002 ret = clk_core_set_rate_nolock(clk->core, rate);
2003 if (!ret) {
2004 clk_core_rate_protect(clk->core);
2005 clk->exclusive_count++;
2006 }
2007
2008 clk_prepare_unlock();
2009
2010 return ret;
2011}
2012EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2023{
2024 int ret = 0;
2025 unsigned long old_min, old_max, rate;
2026
2027 if (!clk)
2028 return 0;
2029
2030 if (min > max) {
2031 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2032 __func__, clk->core->name, clk->dev_id, clk->con_id,
2033 min, max);
2034 return -EINVAL;
2035 }
2036
2037 clk_prepare_lock();
2038
2039 if (clk->exclusive_count)
2040 clk_core_rate_unprotect(clk->core);
2041
2042
2043 old_min = clk->min_rate;
2044 old_max = clk->max_rate;
2045 clk->min_rate = min;
2046 clk->max_rate = max;
2047
2048 rate = clk_core_get_rate_nolock(clk->core);
2049 if (rate < min || rate > max) {
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063 if (rate < min)
2064 rate = min;
2065 else
2066 rate = max;
2067
2068 ret = clk_core_set_rate_nolock(clk->core, rate);
2069 if (ret) {
2070
2071 clk->min_rate = old_min;
2072 clk->max_rate = old_max;
2073 }
2074 }
2075
2076 if (clk->exclusive_count)
2077 clk_core_rate_protect(clk->core);
2078
2079 clk_prepare_unlock();
2080
2081 return ret;
2082}
2083EXPORT_SYMBOL_GPL(clk_set_rate_range);
2084
2085
2086
2087
2088
2089
2090
2091
2092int clk_set_min_rate(struct clk *clk, unsigned long rate)
2093{
2094 if (!clk)
2095 return 0;
2096
2097 return clk_set_rate_range(clk, rate, clk->max_rate);
2098}
2099EXPORT_SYMBOL_GPL(clk_set_min_rate);
2100
2101
2102
2103
2104
2105
2106
2107
2108int clk_set_max_rate(struct clk *clk, unsigned long rate)
2109{
2110 if (!clk)
2111 return 0;
2112
2113 return clk_set_rate_range(clk, clk->min_rate, rate);
2114}
2115EXPORT_SYMBOL_GPL(clk_set_max_rate);
2116
2117
2118
2119
2120
2121
2122
2123struct clk *clk_get_parent(struct clk *clk)
2124{
2125 struct clk *parent;
2126
2127 if (!clk)
2128 return NULL;
2129
2130 clk_prepare_lock();
2131
2132 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
2133 clk_prepare_unlock();
2134
2135 return parent;
2136}
2137EXPORT_SYMBOL_GPL(clk_get_parent);
2138
2139static struct clk_core *__clk_init_parent(struct clk_core *core)
2140{
2141 u8 index = 0;
2142
2143 if (core->num_parents > 1 && core->ops->get_parent)
2144 index = core->ops->get_parent(core->hw);
2145
2146 return clk_core_get_parent_by_index(core, index);
2147}
2148
2149static void clk_core_reparent(struct clk_core *core,
2150 struct clk_core *new_parent)
2151{
2152 clk_reparent(core, new_parent);
2153 __clk_recalc_accuracies(core);
2154 __clk_recalc_rates(core, POST_RATE_CHANGE);
2155}
2156
2157void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2158{
2159 if (!hw)
2160 return;
2161
2162 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2163}
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175bool clk_has_parent(struct clk *clk, struct clk *parent)
2176{
2177 struct clk_core *core, *parent_core;
2178
2179
2180 if (!clk || !parent)
2181 return true;
2182
2183 core = clk->core;
2184 parent_core = parent->core;
2185
2186
2187 if (core->parent == parent_core)
2188 return true;
2189
2190 return match_string(core->parent_names, core->num_parents,
2191 parent_core->name) >= 0;
2192}
2193EXPORT_SYMBOL_GPL(clk_has_parent);
2194
2195static int clk_core_set_parent_nolock(struct clk_core *core,
2196 struct clk_core *parent)
2197{
2198 int ret = 0;
2199 int p_index = 0;
2200 unsigned long p_rate = 0;
2201
2202 lockdep_assert_held(&prepare_lock);
2203
2204 if (!core)
2205 return 0;
2206
2207 if (core->parent == parent)
2208 return 0;
2209
2210
2211 if (core->num_parents > 1 && !core->ops->set_parent)
2212 return -EPERM;
2213
2214
2215 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2216 return -EBUSY;
2217
2218 if (clk_core_rate_is_protected(core))
2219 return -EBUSY;
2220
2221
2222 if (parent) {
2223 p_index = clk_fetch_parent_index(core, parent);
2224 if (p_index < 0) {
2225 pr_debug("%s: clk %s can not be parent of clk %s\n",
2226 __func__, parent->name, core->name);
2227 return p_index;
2228 }
2229 p_rate = parent->rate;
2230 }
2231
2232 ret = clk_pm_runtime_get(core);
2233 if (ret)
2234 return ret;
2235
2236
2237 ret = __clk_speculate_rates(core, p_rate);
2238
2239
2240 if (ret & NOTIFY_STOP_MASK)
2241 goto runtime_put;
2242
2243
2244 ret = __clk_set_parent(core, parent, p_index);
2245
2246
2247 if (ret) {
2248 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
2249 } else {
2250 __clk_recalc_rates(core, POST_RATE_CHANGE);
2251 __clk_recalc_accuracies(core);
2252 }
2253
2254runtime_put:
2255 clk_pm_runtime_put(core);
2256
2257 return ret;
2258}
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277int clk_set_parent(struct clk *clk, struct clk *parent)
2278{
2279 int ret;
2280
2281 if (!clk)
2282 return 0;
2283
2284 clk_prepare_lock();
2285
2286 if (clk->exclusive_count)
2287 clk_core_rate_unprotect(clk->core);
2288
2289 ret = clk_core_set_parent_nolock(clk->core,
2290 parent ? parent->core : NULL);
2291
2292 if (clk->exclusive_count)
2293 clk_core_rate_protect(clk->core);
2294
2295 clk_prepare_unlock();
2296
2297 return ret;
2298}
2299EXPORT_SYMBOL_GPL(clk_set_parent);
2300
2301static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2302{
2303 int ret = -EINVAL;
2304
2305 lockdep_assert_held(&prepare_lock);
2306
2307 if (!core)
2308 return 0;
2309
2310 if (clk_core_rate_is_protected(core))
2311 return -EBUSY;
2312
2313 trace_clk_set_phase(core, degrees);
2314
2315 if (core->ops->set_phase) {
2316 ret = core->ops->set_phase(core->hw, degrees);
2317 if (!ret)
2318 core->phase = degrees;
2319 }
2320
2321 trace_clk_set_phase_complete(core, degrees);
2322
2323 return ret;
2324}
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346int clk_set_phase(struct clk *clk, int degrees)
2347{
2348 int ret;
2349
2350 if (!clk)
2351 return 0;
2352
2353
2354 degrees %= 360;
2355 if (degrees < 0)
2356 degrees += 360;
2357
2358 clk_prepare_lock();
2359
2360 if (clk->exclusive_count)
2361 clk_core_rate_unprotect(clk->core);
2362
2363 ret = clk_core_set_phase_nolock(clk->core, degrees);
2364
2365 if (clk->exclusive_count)
2366 clk_core_rate_protect(clk->core);
2367
2368 clk_prepare_unlock();
2369
2370 return ret;
2371}
2372EXPORT_SYMBOL_GPL(clk_set_phase);
2373
2374static int clk_core_get_phase(struct clk_core *core)
2375{
2376 int ret;
2377
2378 clk_prepare_lock();
2379
2380 if (core->ops->get_phase)
2381 core->phase = core->ops->get_phase(core->hw);
2382 ret = core->phase;
2383 clk_prepare_unlock();
2384
2385 return ret;
2386}
2387
2388
2389
2390
2391
2392
2393
2394
2395int clk_get_phase(struct clk *clk)
2396{
2397 if (!clk)
2398 return 0;
2399
2400 return clk_core_get_phase(clk->core);
2401}
2402EXPORT_SYMBOL_GPL(clk_get_phase);
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415bool clk_is_match(const struct clk *p, const struct clk *q)
2416{
2417
2418 if (p == q)
2419 return true;
2420
2421
2422 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2423 if (p->core == q->core)
2424 return true;
2425
2426 return false;
2427}
2428EXPORT_SYMBOL_GPL(clk_is_match);
2429
2430
2431
2432#ifdef CONFIG_DEBUG_FS
2433#include <linux/debugfs.h>
2434
2435static struct dentry *rootdir;
2436static int inited = 0;
2437static DEFINE_MUTEX(clk_debug_lock);
2438static HLIST_HEAD(clk_debug_list);
2439
2440static struct hlist_head *all_lists[] = {
2441 &clk_root_list,
2442 &clk_orphan_list,
2443 NULL,
2444};
2445
2446static struct hlist_head *orphan_list[] = {
2447 &clk_orphan_list,
2448 NULL,
2449};
2450
2451static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2452 int level)
2453{
2454 if (!c)
2455 return;
2456
2457 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %-3d\n",
2458 level * 3 + 1, "",
2459 30 - level * 3, c->name,
2460 c->enable_count, c->prepare_count, c->protect_count,
2461 clk_core_get_rate(c), clk_core_get_accuracy(c),
2462 clk_core_get_phase(c));
2463}
2464
2465static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2466 int level)
2467{
2468 struct clk_core *child;
2469
2470 if (!c)
2471 return;
2472
2473 clk_summary_show_one(s, c, level);
2474
2475 hlist_for_each_entry(child, &c->children, child_node)
2476 clk_summary_show_subtree(s, child, level + 1);
2477}
2478
2479static int clk_summary_show(struct seq_file *s, void *data)
2480{
2481 struct clk_core *c;
2482 struct hlist_head **lists = (struct hlist_head **)s->private;
2483
2484 seq_puts(s, " enable prepare protect \n");
2485 seq_puts(s, " clock count count count rate accuracy phase\n");
2486 seq_puts(s, "----------------------------------------------------------------------------------------\n");
2487
2488 clk_prepare_lock();
2489
2490 for (; *lists; lists++)
2491 hlist_for_each_entry(c, *lists, child_node)
2492 clk_summary_show_subtree(s, c, 0);
2493
2494 clk_prepare_unlock();
2495
2496 return 0;
2497}
2498DEFINE_SHOW_ATTRIBUTE(clk_summary);
2499
2500static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2501{
2502 if (!c)
2503 return;
2504
2505
2506 seq_printf(s, "\"%s\": { ", c->name);
2507 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2508 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
2509 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
2510 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2511 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
2512 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2513}
2514
2515static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2516{
2517 struct clk_core *child;
2518
2519 if (!c)
2520 return;
2521
2522 clk_dump_one(s, c, level);
2523
2524 hlist_for_each_entry(child, &c->children, child_node) {
2525 seq_putc(s, ',');
2526 clk_dump_subtree(s, child, level + 1);
2527 }
2528
2529 seq_putc(s, '}');
2530}
2531
2532static int clk_dump_show(struct seq_file *s, void *data)
2533{
2534 struct clk_core *c;
2535 bool first_node = true;
2536 struct hlist_head **lists = (struct hlist_head **)s->private;
2537
2538 seq_putc(s, '{');
2539 clk_prepare_lock();
2540
2541 for (; *lists; lists++) {
2542 hlist_for_each_entry(c, *lists, child_node) {
2543 if (!first_node)
2544 seq_putc(s, ',');
2545 first_node = false;
2546 clk_dump_subtree(s, c, 0);
2547 }
2548 }
2549
2550 clk_prepare_unlock();
2551
2552 seq_puts(s, "}\n");
2553 return 0;
2554}
2555DEFINE_SHOW_ATTRIBUTE(clk_dump);
2556
2557static const struct {
2558 unsigned long flag;
2559 const char *name;
2560} clk_flags[] = {
2561#define ENTRY(f) { f, #f }
2562 ENTRY(CLK_SET_RATE_GATE),
2563 ENTRY(CLK_SET_PARENT_GATE),
2564 ENTRY(CLK_SET_RATE_PARENT),
2565 ENTRY(CLK_IGNORE_UNUSED),
2566 ENTRY(CLK_IS_BASIC),
2567 ENTRY(CLK_GET_RATE_NOCACHE),
2568 ENTRY(CLK_SET_RATE_NO_REPARENT),
2569 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2570 ENTRY(CLK_RECALC_NEW_RATES),
2571 ENTRY(CLK_SET_RATE_UNGATE),
2572 ENTRY(CLK_IS_CRITICAL),
2573 ENTRY(CLK_OPS_PARENT_ENABLE),
2574#undef ENTRY
2575};
2576
2577static int clk_flags_show(struct seq_file *s, void *data)
2578{
2579 struct clk_core *core = s->private;
2580 unsigned long flags = core->flags;
2581 unsigned int i;
2582
2583 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2584 if (flags & clk_flags[i].flag) {
2585 seq_printf(s, "%s\n", clk_flags[i].name);
2586 flags &= ~clk_flags[i].flag;
2587 }
2588 }
2589 if (flags) {
2590
2591 seq_printf(s, "0x%lx\n", flags);
2592 }
2593
2594 return 0;
2595}
2596DEFINE_SHOW_ATTRIBUTE(clk_flags);
2597
2598static int possible_parents_show(struct seq_file *s, void *data)
2599{
2600 struct clk_core *core = s->private;
2601 int i;
2602
2603 for (i = 0; i < core->num_parents - 1; i++)
2604 seq_printf(s, "%s ", core->parent_names[i]);
2605
2606 seq_printf(s, "%s\n", core->parent_names[i]);
2607
2608 return 0;
2609}
2610DEFINE_SHOW_ATTRIBUTE(possible_parents);
2611
2612static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2613{
2614 struct dentry *root;
2615
2616 if (!core || !pdentry)
2617 return;
2618
2619 root = debugfs_create_dir(core->name, pdentry);
2620 core->dentry = root;
2621
2622 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
2623 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
2624 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
2625 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
2626 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
2627 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
2628 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
2629 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
2630
2631 if (core->num_parents > 1)
2632 debugfs_create_file("clk_possible_parents", 0444, root, core,
2633 &possible_parents_fops);
2634
2635 if (core->ops->debug_init)
2636 core->ops->debug_init(core->hw, core->dentry);
2637}
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647static void clk_debug_register(struct clk_core *core)
2648{
2649 mutex_lock(&clk_debug_lock);
2650 hlist_add_head(&core->debug_node, &clk_debug_list);
2651 if (inited)
2652 clk_debug_create_one(core, rootdir);
2653 mutex_unlock(&clk_debug_lock);
2654}
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664static void clk_debug_unregister(struct clk_core *core)
2665{
2666 mutex_lock(&clk_debug_lock);
2667 hlist_del_init(&core->debug_node);
2668 debugfs_remove_recursive(core->dentry);
2669 core->dentry = NULL;
2670 mutex_unlock(&clk_debug_lock);
2671}
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682static int __init clk_debug_init(void)
2683{
2684 struct clk_core *core;
2685
2686 rootdir = debugfs_create_dir("clk", NULL);
2687
2688 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
2689 &clk_summary_fops);
2690 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
2691 &clk_dump_fops);
2692 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
2693 &clk_summary_fops);
2694 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
2695 &clk_dump_fops);
2696
2697 mutex_lock(&clk_debug_lock);
2698 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2699 clk_debug_create_one(core, rootdir);
2700
2701 inited = 1;
2702 mutex_unlock(&clk_debug_lock);
2703
2704 return 0;
2705}
2706late_initcall(clk_debug_init);
2707#else
2708static inline void clk_debug_register(struct clk_core *core) { }
2709static inline void clk_debug_reparent(struct clk_core *core,
2710 struct clk_core *new_parent)
2711{
2712}
2713static inline void clk_debug_unregister(struct clk_core *core)
2714{
2715}
2716#endif
2717
2718
2719
2720
2721
2722
2723
2724
2725static int __clk_core_init(struct clk_core *core)
2726{
2727 int i, ret;
2728 struct clk_core *orphan;
2729 struct hlist_node *tmp2;
2730 unsigned long rate;
2731
2732 if (!core)
2733 return -EINVAL;
2734
2735 clk_prepare_lock();
2736
2737 ret = clk_pm_runtime_get(core);
2738 if (ret)
2739 goto unlock;
2740
2741
2742 if (clk_core_lookup(core->name)) {
2743 pr_debug("%s: clk %s already initialized\n",
2744 __func__, core->name);
2745 ret = -EEXIST;
2746 goto out;
2747 }
2748
2749
2750 if (core->ops->set_rate &&
2751 !((core->ops->round_rate || core->ops->determine_rate) &&
2752 core->ops->recalc_rate)) {
2753 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2754 __func__, core->name);
2755 ret = -EINVAL;
2756 goto out;
2757 }
2758
2759 if (core->ops->set_parent && !core->ops->get_parent) {
2760 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2761 __func__, core->name);
2762 ret = -EINVAL;
2763 goto out;
2764 }
2765
2766 if (core->num_parents > 1 && !core->ops->get_parent) {
2767 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2768 __func__, core->name);
2769 ret = -EINVAL;
2770 goto out;
2771 }
2772
2773 if (core->ops->set_rate_and_parent &&
2774 !(core->ops->set_parent && core->ops->set_rate)) {
2775 pr_err("%s: %s must implement .set_parent & .set_rate\n",
2776 __func__, core->name);
2777 ret = -EINVAL;
2778 goto out;
2779 }
2780
2781
2782 for (i = 0; i < core->num_parents; i++)
2783 WARN(!core->parent_names[i],
2784 "%s: invalid NULL in %s's .parent_names\n",
2785 __func__, core->name);
2786
2787 core->parent = __clk_init_parent(core);
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799 if (core->parent) {
2800 hlist_add_head(&core->child_node,
2801 &core->parent->children);
2802 core->orphan = core->parent->orphan;
2803 } else if (!core->num_parents) {
2804 hlist_add_head(&core->child_node, &clk_root_list);
2805 core->orphan = false;
2806 } else {
2807 hlist_add_head(&core->child_node, &clk_orphan_list);
2808 core->orphan = true;
2809 }
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819 if (core->ops->init)
2820 core->ops->init(core->hw);
2821
2822
2823
2824
2825
2826
2827
2828
2829 if (core->ops->recalc_accuracy)
2830 core->accuracy = core->ops->recalc_accuracy(core->hw,
2831 __clk_get_accuracy(core->parent));
2832 else if (core->parent)
2833 core->accuracy = core->parent->accuracy;
2834 else
2835 core->accuracy = 0;
2836
2837
2838
2839
2840
2841
2842 if (core->ops->get_phase)
2843 core->phase = core->ops->get_phase(core->hw);
2844 else
2845 core->phase = 0;
2846
2847
2848
2849
2850
2851
2852
2853 if (core->ops->recalc_rate)
2854 rate = core->ops->recalc_rate(core->hw,
2855 clk_core_get_rate_nolock(core->parent));
2856 else if (core->parent)
2857 rate = core->parent->rate;
2858 else
2859 rate = 0;
2860 core->rate = core->req_rate = rate;
2861
2862
2863
2864
2865
2866
2867 if (core->flags & CLK_IS_CRITICAL) {
2868 unsigned long flags;
2869
2870 clk_core_prepare(core);
2871
2872 flags = clk_enable_lock();
2873 clk_core_enable(core);
2874 clk_enable_unlock(flags);
2875 }
2876
2877
2878
2879
2880
2881 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
2882 struct clk_core *parent = __clk_init_parent(orphan);
2883
2884
2885
2886
2887
2888
2889
2890 if (parent) {
2891
2892 __clk_set_parent_before(orphan, parent);
2893 __clk_set_parent_after(orphan, parent, NULL);
2894 __clk_recalc_accuracies(orphan);
2895 __clk_recalc_rates(orphan, 0);
2896 }
2897 }
2898
2899 kref_init(&core->ref);
2900out:
2901 clk_pm_runtime_put(core);
2902unlock:
2903 clk_prepare_unlock();
2904
2905 if (!ret)
2906 clk_debug_register(core);
2907
2908 return ret;
2909}
2910
2911struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2912 const char *con_id)
2913{
2914 struct clk *clk;
2915
2916
2917 if (IS_ERR_OR_NULL(hw))
2918 return ERR_CAST(hw);
2919
2920 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2921 if (!clk)
2922 return ERR_PTR(-ENOMEM);
2923
2924 clk->core = hw->core;
2925 clk->dev_id = dev_id;
2926 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
2927 clk->max_rate = ULONG_MAX;
2928
2929 clk_prepare_lock();
2930 hlist_add_head(&clk->clks_node, &hw->core->clks);
2931 clk_prepare_unlock();
2932
2933 return clk;
2934}
2935
2936void __clk_free_clk(struct clk *clk)
2937{
2938 clk_prepare_lock();
2939 hlist_del(&clk->clks_node);
2940 clk_prepare_unlock();
2941
2942 kfree_const(clk->con_id);
2943 kfree(clk);
2944}
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957struct clk *clk_register(struct device *dev, struct clk_hw *hw)
2958{
2959 int i, ret;
2960 struct clk_core *core;
2961
2962 core = kzalloc(sizeof(*core), GFP_KERNEL);
2963 if (!core) {
2964 ret = -ENOMEM;
2965 goto fail_out;
2966 }
2967
2968 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2969 if (!core->name) {
2970 ret = -ENOMEM;
2971 goto fail_name;
2972 }
2973
2974 if (WARN_ON(!hw->init->ops)) {
2975 ret = -EINVAL;
2976 goto fail_ops;
2977 }
2978 core->ops = hw->init->ops;
2979
2980 if (dev && pm_runtime_enabled(dev))
2981 core->dev = dev;
2982 if (dev && dev->driver)
2983 core->owner = dev->driver->owner;
2984 core->hw = hw;
2985 core->flags = hw->init->flags;
2986 core->num_parents = hw->init->num_parents;
2987 core->min_rate = 0;
2988 core->max_rate = ULONG_MAX;
2989 hw->core = core;
2990
2991
2992 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
2993 GFP_KERNEL);
2994
2995 if (!core->parent_names) {
2996 ret = -ENOMEM;
2997 goto fail_parent_names;
2998 }
2999
3000
3001
3002 for (i = 0; i < core->num_parents; i++) {
3003 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
3004 GFP_KERNEL);
3005 if (!core->parent_names[i]) {
3006 ret = -ENOMEM;
3007 goto fail_parent_names_copy;
3008 }
3009 }
3010
3011
3012 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3013 GFP_KERNEL);
3014 if (!core->parents) {
3015 ret = -ENOMEM;
3016 goto fail_parents;
3017 };
3018
3019 INIT_HLIST_HEAD(&core->clks);
3020
3021 hw->clk = __clk_create_clk(hw, NULL, NULL);
3022 if (IS_ERR(hw->clk)) {
3023 ret = PTR_ERR(hw->clk);
3024 goto fail_parents;
3025 }
3026
3027 ret = __clk_core_init(core);
3028 if (!ret)
3029 return hw->clk;
3030
3031 __clk_free_clk(hw->clk);
3032 hw->clk = NULL;
3033
3034fail_parents:
3035 kfree(core->parents);
3036fail_parent_names_copy:
3037 while (--i >= 0)
3038 kfree_const(core->parent_names[i]);
3039 kfree(core->parent_names);
3040fail_parent_names:
3041fail_ops:
3042 kfree_const(core->name);
3043fail_name:
3044 kfree(core);
3045fail_out:
3046 return ERR_PTR(ret);
3047}
3048EXPORT_SYMBOL_GPL(clk_register);
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060int clk_hw_register(struct device *dev, struct clk_hw *hw)
3061{
3062 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3063}
3064EXPORT_SYMBOL_GPL(clk_hw_register);
3065
3066
3067static void __clk_release(struct kref *ref)
3068{
3069 struct clk_core *core = container_of(ref, struct clk_core, ref);
3070 int i = core->num_parents;
3071
3072 lockdep_assert_held(&prepare_lock);
3073
3074 kfree(core->parents);
3075 while (--i >= 0)
3076 kfree_const(core->parent_names[i]);
3077
3078 kfree(core->parent_names);
3079 kfree_const(core->name);
3080 kfree(core);
3081}
3082
3083
3084
3085
3086
3087
3088static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3089{
3090 return -ENXIO;
3091}
3092
3093static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3094{
3095 WARN_ON_ONCE(1);
3096}
3097
3098static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3099 unsigned long parent_rate)
3100{
3101 return -ENXIO;
3102}
3103
3104static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3105{
3106 return -ENXIO;
3107}
3108
3109static const struct clk_ops clk_nodrv_ops = {
3110 .enable = clk_nodrv_prepare_enable,
3111 .disable = clk_nodrv_disable_unprepare,
3112 .prepare = clk_nodrv_prepare_enable,
3113 .unprepare = clk_nodrv_disable_unprepare,
3114 .set_rate = clk_nodrv_set_rate,
3115 .set_parent = clk_nodrv_set_parent,
3116};
3117
3118
3119
3120
3121
3122void clk_unregister(struct clk *clk)
3123{
3124 unsigned long flags;
3125
3126 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3127 return;
3128
3129 clk_debug_unregister(clk->core);
3130
3131 clk_prepare_lock();
3132
3133 if (clk->core->ops == &clk_nodrv_ops) {
3134 pr_err("%s: unregistered clock: %s\n", __func__,
3135 clk->core->name);
3136 goto unlock;
3137 }
3138
3139
3140
3141
3142 flags = clk_enable_lock();
3143 clk->core->ops = &clk_nodrv_ops;
3144 clk_enable_unlock(flags);
3145
3146 if (!hlist_empty(&clk->core->children)) {
3147 struct clk_core *child;
3148 struct hlist_node *t;
3149
3150
3151 hlist_for_each_entry_safe(child, t, &clk->core->children,
3152 child_node)
3153 clk_core_set_parent_nolock(child, NULL);
3154 }
3155
3156 hlist_del_init(&clk->core->child_node);
3157
3158 if (clk->core->prepare_count)
3159 pr_warn("%s: unregistering prepared clock: %s\n",
3160 __func__, clk->core->name);
3161
3162 if (clk->core->protect_count)
3163 pr_warn("%s: unregistering protected clock: %s\n",
3164 __func__, clk->core->name);
3165
3166 kref_put(&clk->core->ref, __clk_release);
3167unlock:
3168 clk_prepare_unlock();
3169}
3170EXPORT_SYMBOL_GPL(clk_unregister);
3171
3172
3173
3174
3175
3176void clk_hw_unregister(struct clk_hw *hw)
3177{
3178 clk_unregister(hw->clk);
3179}
3180EXPORT_SYMBOL_GPL(clk_hw_unregister);
3181
3182static void devm_clk_release(struct device *dev, void *res)
3183{
3184 clk_unregister(*(struct clk **)res);
3185}
3186
3187static void devm_clk_hw_release(struct device *dev, void *res)
3188{
3189 clk_hw_unregister(*(struct clk_hw **)res);
3190}
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3202{
3203 struct clk *clk;
3204 struct clk **clkp;
3205
3206 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3207 if (!clkp)
3208 return ERR_PTR(-ENOMEM);
3209
3210 clk = clk_register(dev, hw);
3211 if (!IS_ERR(clk)) {
3212 *clkp = clk;
3213 devres_add(dev, clkp);
3214 } else {
3215 devres_free(clkp);
3216 }
3217
3218 return clk;
3219}
3220EXPORT_SYMBOL_GPL(devm_clk_register);
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3232{
3233 struct clk_hw **hwp;
3234 int ret;
3235
3236 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3237 if (!hwp)
3238 return -ENOMEM;
3239
3240 ret = clk_hw_register(dev, hw);
3241 if (!ret) {
3242 *hwp = hw;
3243 devres_add(dev, hwp);
3244 } else {
3245 devres_free(hwp);
3246 }
3247
3248 return ret;
3249}
3250EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3251
3252static int devm_clk_match(struct device *dev, void *res, void *data)
3253{
3254 struct clk *c = res;
3255 if (WARN_ON(!c))
3256 return 0;
3257 return c == data;
3258}
3259
3260static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3261{
3262 struct clk_hw *hw = res;
3263
3264 if (WARN_ON(!hw))
3265 return 0;
3266 return hw == data;
3267}
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277void devm_clk_unregister(struct device *dev, struct clk *clk)
3278{
3279 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3280}
3281EXPORT_SYMBOL_GPL(devm_clk_unregister);
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3293{
3294 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3295 hw));
3296}
3297EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3298
3299
3300
3301
3302int __clk_get(struct clk *clk)
3303{
3304 struct clk_core *core = !clk ? NULL : clk->core;
3305
3306 if (core) {
3307 if (!try_module_get(core->owner))
3308 return 0;
3309
3310 kref_get(&core->ref);
3311 }
3312 return 1;
3313}
3314
3315void __clk_put(struct clk *clk)
3316{
3317 struct module *owner;
3318
3319 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3320 return;
3321
3322 clk_prepare_lock();
3323
3324
3325
3326
3327
3328
3329 if (WARN_ON(clk->exclusive_count)) {
3330
3331 clk->core->protect_count -= (clk->exclusive_count - 1);
3332 clk_core_rate_unprotect(clk->core);
3333 clk->exclusive_count = 0;
3334 }
3335
3336 hlist_del(&clk->clks_node);
3337 if (clk->min_rate > clk->core->req_rate ||
3338 clk->max_rate < clk->core->req_rate)
3339 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3340
3341 owner = clk->core->owner;
3342 kref_put(&clk->core->ref, __clk_release);
3343
3344 clk_prepare_unlock();
3345
3346 module_put(owner);
3347
3348 kfree(clk);
3349}
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3374{
3375 struct clk_notifier *cn;
3376 int ret = -ENOMEM;
3377
3378 if (!clk || !nb)
3379 return -EINVAL;
3380
3381 clk_prepare_lock();
3382
3383
3384 list_for_each_entry(cn, &clk_notifier_list, node)
3385 if (cn->clk == clk)
3386 break;
3387
3388
3389 if (cn->clk != clk) {
3390 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
3391 if (!cn)
3392 goto out;
3393
3394 cn->clk = clk;
3395 srcu_init_notifier_head(&cn->notifier_head);
3396
3397 list_add(&cn->node, &clk_notifier_list);
3398 }
3399
3400 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3401
3402 clk->core->notifier_count++;
3403
3404out:
3405 clk_prepare_unlock();
3406
3407 return ret;
3408}
3409EXPORT_SYMBOL_GPL(clk_notifier_register);
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3423{
3424 struct clk_notifier *cn = NULL;
3425 int ret = -EINVAL;
3426
3427 if (!clk || !nb)
3428 return -EINVAL;
3429
3430 clk_prepare_lock();
3431
3432 list_for_each_entry(cn, &clk_notifier_list, node)
3433 if (cn->clk == clk)
3434 break;
3435
3436 if (cn->clk == clk) {
3437 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3438
3439 clk->core->notifier_count--;
3440
3441
3442 if (!cn->notifier_head.head) {
3443 srcu_cleanup_notifier_head(&cn->notifier_head);
3444 list_del(&cn->node);
3445 kfree(cn);
3446 }
3447
3448 } else {
3449 ret = -ENOENT;
3450 }
3451
3452 clk_prepare_unlock();
3453
3454 return ret;
3455}
3456EXPORT_SYMBOL_GPL(clk_notifier_unregister);
3457
3458#ifdef CONFIG_OF
3459
3460
3461
3462
3463
3464
3465
3466
3467struct of_clk_provider {
3468 struct list_head link;
3469
3470 struct device_node *node;
3471 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
3472 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
3473 void *data;
3474};
3475
3476static const struct of_device_id __clk_of_table_sentinel
3477 __used __section(__clk_of_table_end);
3478
3479static LIST_HEAD(of_clk_providers);
3480static DEFINE_MUTEX(of_clk_mutex);
3481
3482struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3483 void *data)
3484{
3485 return data;
3486}
3487EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3488
3489struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3490{
3491 return data;
3492}
3493EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3494
3495struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3496{
3497 struct clk_onecell_data *clk_data = data;
3498 unsigned int idx = clkspec->args[0];
3499
3500 if (idx >= clk_data->clk_num) {
3501 pr_err("%s: invalid clock index %u\n", __func__, idx);
3502 return ERR_PTR(-EINVAL);
3503 }
3504
3505 return clk_data->clks[idx];
3506}
3507EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3508
3509struct clk_hw *
3510of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3511{
3512 struct clk_hw_onecell_data *hw_data = data;
3513 unsigned int idx = clkspec->args[0];
3514
3515 if (idx >= hw_data->num) {
3516 pr_err("%s: invalid index %u\n", __func__, idx);
3517 return ERR_PTR(-EINVAL);
3518 }
3519
3520 return hw_data->hws[idx];
3521}
3522EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3523
3524
3525
3526
3527
3528
3529
3530int of_clk_add_provider(struct device_node *np,
3531 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3532 void *data),
3533 void *data)
3534{
3535 struct of_clk_provider *cp;
3536 int ret;
3537
3538 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3539 if (!cp)
3540 return -ENOMEM;
3541
3542 cp->node = of_node_get(np);
3543 cp->data = data;
3544 cp->get = clk_src_get;
3545
3546 mutex_lock(&of_clk_mutex);
3547 list_add(&cp->link, &of_clk_providers);
3548 mutex_unlock(&of_clk_mutex);
3549 pr_debug("Added clock from %pOF\n", np);
3550
3551 ret = of_clk_set_defaults(np, true);
3552 if (ret < 0)
3553 of_clk_del_provider(np);
3554
3555 return ret;
3556}
3557EXPORT_SYMBOL_GPL(of_clk_add_provider);
3558
3559
3560
3561
3562
3563
3564
3565int of_clk_add_hw_provider(struct device_node *np,
3566 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3567 void *data),
3568 void *data)
3569{
3570 struct of_clk_provider *cp;
3571 int ret;
3572
3573 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3574 if (!cp)
3575 return -ENOMEM;
3576
3577 cp->node = of_node_get(np);
3578 cp->data = data;
3579 cp->get_hw = get;
3580
3581 mutex_lock(&of_clk_mutex);
3582 list_add(&cp->link, &of_clk_providers);
3583 mutex_unlock(&of_clk_mutex);
3584 pr_debug("Added clk_hw provider from %pOF\n", np);
3585
3586 ret = of_clk_set_defaults(np, true);
3587 if (ret < 0)
3588 of_clk_del_provider(np);
3589
3590 return ret;
3591}
3592EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3593
3594static void devm_of_clk_release_provider(struct device *dev, void *res)
3595{
3596 of_clk_del_provider(*(struct device_node **)res);
3597}
3598
3599int devm_of_clk_add_hw_provider(struct device *dev,
3600 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3601 void *data),
3602 void *data)
3603{
3604 struct device_node **ptr, *np;
3605 int ret;
3606
3607 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3608 GFP_KERNEL);
3609 if (!ptr)
3610 return -ENOMEM;
3611
3612 np = dev->of_node;
3613 ret = of_clk_add_hw_provider(np, get, data);
3614 if (!ret) {
3615 *ptr = np;
3616 devres_add(dev, ptr);
3617 } else {
3618 devres_free(ptr);
3619 }
3620
3621 return ret;
3622}
3623EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3624
3625
3626
3627
3628
3629void of_clk_del_provider(struct device_node *np)
3630{
3631 struct of_clk_provider *cp;
3632
3633 mutex_lock(&of_clk_mutex);
3634 list_for_each_entry(cp, &of_clk_providers, link) {
3635 if (cp->node == np) {
3636 list_del(&cp->link);
3637 of_node_put(cp->node);
3638 kfree(cp);
3639 break;
3640 }
3641 }
3642 mutex_unlock(&of_clk_mutex);
3643}
3644EXPORT_SYMBOL_GPL(of_clk_del_provider);
3645
3646static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3647{
3648 struct device_node **np = res;
3649
3650 if (WARN_ON(!np || !*np))
3651 return 0;
3652
3653 return *np == data;
3654}
3655
3656void devm_of_clk_del_provider(struct device *dev)
3657{
3658 int ret;
3659
3660 ret = devres_release(dev, devm_of_clk_release_provider,
3661 devm_clk_provider_match, dev->of_node);
3662
3663 WARN_ON(ret);
3664}
3665EXPORT_SYMBOL(devm_of_clk_del_provider);
3666
3667static struct clk_hw *
3668__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3669 struct of_phandle_args *clkspec)
3670{
3671 struct clk *clk;
3672
3673 if (provider->get_hw)
3674 return provider->get_hw(clkspec, provider->data);
3675
3676 clk = provider->get(clkspec, provider->data);
3677 if (IS_ERR(clk))
3678 return ERR_CAST(clk);
3679 return __clk_get_hw(clk);
3680}
3681
3682struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3683 const char *dev_id, const char *con_id)
3684{
3685 struct of_clk_provider *provider;
3686 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
3687 struct clk_hw *hw;
3688
3689 if (!clkspec)
3690 return ERR_PTR(-EINVAL);
3691
3692
3693 mutex_lock(&of_clk_mutex);
3694 list_for_each_entry(provider, &of_clk_providers, link) {
3695 if (provider->node == clkspec->np) {
3696 hw = __of_clk_get_hw_from_provider(provider, clkspec);
3697 clk = __clk_create_clk(hw, dev_id, con_id);
3698 }
3699
3700 if (!IS_ERR(clk)) {
3701 if (!__clk_get(clk)) {
3702 __clk_free_clk(clk);
3703 clk = ERR_PTR(-ENOENT);
3704 }
3705
3706 break;
3707 }
3708 }
3709 mutex_unlock(&of_clk_mutex);
3710
3711 return clk;
3712}
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3723{
3724 return __of_clk_get_from_provider(clkspec, NULL, __func__);
3725}
3726EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
3727
3728
3729
3730
3731
3732
3733
3734unsigned int of_clk_get_parent_count(struct device_node *np)
3735{
3736 int count;
3737
3738 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3739 if (count < 0)
3740 return 0;
3741
3742 return count;
3743}
3744EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3745
3746const char *of_clk_get_parent_name(struct device_node *np, int index)
3747{
3748 struct of_phandle_args clkspec;
3749 struct property *prop;
3750 const char *clk_name;
3751 const __be32 *vp;
3752 u32 pv;
3753 int rc;
3754 int count;
3755 struct clk *clk;
3756
3757 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3758 &clkspec);
3759 if (rc)
3760 return NULL;
3761
3762 index = clkspec.args_count ? clkspec.args[0] : 0;
3763 count = 0;
3764
3765
3766
3767
3768 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3769 if (index == pv) {
3770 index = count;
3771 break;
3772 }
3773 count++;
3774 }
3775
3776 if (prop && !vp)
3777 return NULL;
3778
3779 if (of_property_read_string_index(clkspec.np, "clock-output-names",
3780 index,
3781 &clk_name) < 0) {
3782
3783
3784
3785
3786
3787
3788 clk = of_clk_get_from_provider(&clkspec);
3789 if (IS_ERR(clk)) {
3790 if (clkspec.args_count == 0)
3791 clk_name = clkspec.np->name;
3792 else
3793 clk_name = NULL;
3794 } else {
3795 clk_name = __clk_get_name(clk);
3796 clk_put(clk);
3797 }
3798 }
3799
3800
3801 of_node_put(clkspec.np);
3802 return clk_name;
3803}
3804EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815int of_clk_parent_fill(struct device_node *np, const char **parents,
3816 unsigned int size)
3817{
3818 unsigned int i = 0;
3819
3820 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3821 i++;
3822
3823 return i;
3824}
3825EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3826
3827struct clock_provider {
3828 void (*clk_init_cb)(struct device_node *);
3829 struct device_node *np;
3830 struct list_head node;
3831};
3832
3833
3834
3835
3836
3837
3838static int parent_ready(struct device_node *np)
3839{
3840 int i = 0;
3841
3842 while (true) {
3843 struct clk *clk = of_clk_get(np, i);
3844
3845
3846 if (!IS_ERR(clk)) {
3847 clk_put(clk);
3848 i++;
3849 continue;
3850 }
3851
3852
3853 if (PTR_ERR(clk) == -EPROBE_DEFER)
3854 return 0;
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864 return 1;
3865 }
3866}
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886int of_clk_detect_critical(struct device_node *np,
3887 int index, unsigned long *flags)
3888{
3889 struct property *prop;
3890 const __be32 *cur;
3891 uint32_t idx;
3892
3893 if (!np || !flags)
3894 return -EINVAL;
3895
3896 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
3897 if (index == idx)
3898 *flags |= CLK_IS_CRITICAL;
3899
3900 return 0;
3901}
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911void __init of_clk_init(const struct of_device_id *matches)
3912{
3913 const struct of_device_id *match;
3914 struct device_node *np;
3915 struct clock_provider *clk_provider, *next;
3916 bool is_init_done;
3917 bool force = false;
3918 LIST_HEAD(clk_provider_list);
3919
3920 if (!matches)
3921 matches = &__clk_of_table;
3922
3923
3924 for_each_matching_node_and_match(np, matches, &match) {
3925 struct clock_provider *parent;
3926
3927 if (!of_device_is_available(np))
3928 continue;
3929
3930 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
3931 if (!parent) {
3932 list_for_each_entry_safe(clk_provider, next,
3933 &clk_provider_list, node) {
3934 list_del(&clk_provider->node);
3935 of_node_put(clk_provider->np);
3936 kfree(clk_provider);
3937 }
3938 of_node_put(np);
3939 return;
3940 }
3941
3942 parent->clk_init_cb = match->data;
3943 parent->np = of_node_get(np);
3944 list_add_tail(&parent->node, &clk_provider_list);
3945 }
3946
3947 while (!list_empty(&clk_provider_list)) {
3948 is_init_done = false;
3949 list_for_each_entry_safe(clk_provider, next,
3950 &clk_provider_list, node) {
3951 if (force || parent_ready(clk_provider->np)) {
3952
3953
3954 of_node_set_flag(clk_provider->np,
3955 OF_POPULATED);
3956
3957 clk_provider->clk_init_cb(clk_provider->np);
3958 of_clk_set_defaults(clk_provider->np, true);
3959
3960 list_del(&clk_provider->node);
3961 of_node_put(clk_provider->np);
3962 kfree(clk_provider);
3963 is_init_done = true;
3964 }
3965 }
3966
3967
3968
3969
3970
3971
3972
3973 if (!is_init_done)
3974 force = true;
3975 }
3976}
3977#endif
3978