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