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