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