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