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