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