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