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