blob: 0001b91f2b6e70b1c24d8854a5a59a55a57bf888 [file] [log] [blame]
Mike Turquetteb24764902012-03-15 23:11:19 -07001/*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Standard functionality for the common clock API. See Documentation/clk.txt
10 */
11
Michael Turquetteb09d6d92015-01-29 14:22:50 -080012#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020013#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070014#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/spinlock.h>
17#include <linux/err.h>
18#include <linux/list.h>
19#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050020#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070021#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053022#include <linux/init.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070023#include <linux/sched.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070024
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020025#include "clk.h"
26
Mike Turquetteb24764902012-03-15 23:11:19 -070027static DEFINE_SPINLOCK(enable_lock);
28static DEFINE_MUTEX(prepare_lock);
29
Mike Turquette533ddeb2013-03-28 13:59:02 -070030static struct task_struct *prepare_owner;
31static struct task_struct *enable_owner;
32
33static int prepare_refcnt;
34static int enable_refcnt;
35
Mike Turquetteb24764902012-03-15 23:11:19 -070036static HLIST_HEAD(clk_root_list);
37static HLIST_HEAD(clk_orphan_list);
38static LIST_HEAD(clk_notifier_list);
39
Michael Turquetteb09d6d92015-01-29 14:22:50 -080040/*** private data structures ***/
41
42struct clk_core {
43 const char *name;
44 const struct clk_ops *ops;
45 struct clk_hw *hw;
46 struct module *owner;
47 struct clk_core *parent;
48 const char **parent_names;
49 struct clk_core **parents;
50 u8 num_parents;
51 u8 new_parent_index;
52 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010053 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080054 unsigned long new_rate;
55 struct clk_core *new_parent;
56 struct clk_core *new_child;
57 unsigned long flags;
58 unsigned int enable_count;
59 unsigned int prepare_count;
60 unsigned long accuracy;
61 int phase;
62 struct hlist_head children;
63 struct hlist_node child_node;
64 struct hlist_node debug_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010065 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080066 unsigned int notifier_count;
67#ifdef CONFIG_DEBUG_FS
68 struct dentry *dentry;
69#endif
70 struct kref ref;
71};
72
Stephen Boyddfc202e2015-02-02 14:37:41 -080073#define CREATE_TRACE_POINTS
74#include <trace/events/clk.h>
75
Michael Turquetteb09d6d92015-01-29 14:22:50 -080076struct clk {
77 struct clk_core *core;
78 const char *dev_id;
79 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010080 unsigned long min_rate;
81 unsigned long max_rate;
Stephen Boyd50595f82015-02-06 11:42:44 -080082 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080083};
84
Mike Turquetteeab89f62013-03-28 13:59:01 -070085/*** locking ***/
86static void clk_prepare_lock(void)
87{
Mike Turquette533ddeb2013-03-28 13:59:02 -070088 if (!mutex_trylock(&prepare_lock)) {
89 if (prepare_owner == current) {
90 prepare_refcnt++;
91 return;
92 }
93 mutex_lock(&prepare_lock);
94 }
95 WARN_ON_ONCE(prepare_owner != NULL);
96 WARN_ON_ONCE(prepare_refcnt != 0);
97 prepare_owner = current;
98 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -070099}
100
101static void clk_prepare_unlock(void)
102{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700103 WARN_ON_ONCE(prepare_owner != current);
104 WARN_ON_ONCE(prepare_refcnt == 0);
105
106 if (--prepare_refcnt)
107 return;
108 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700109 mutex_unlock(&prepare_lock);
110}
111
112static unsigned long clk_enable_lock(void)
113{
114 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700115
116 if (!spin_trylock_irqsave(&enable_lock, flags)) {
117 if (enable_owner == current) {
118 enable_refcnt++;
119 return flags;
120 }
121 spin_lock_irqsave(&enable_lock, flags);
122 }
123 WARN_ON_ONCE(enable_owner != NULL);
124 WARN_ON_ONCE(enable_refcnt != 0);
125 enable_owner = current;
126 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700127 return flags;
128}
129
130static void clk_enable_unlock(unsigned long flags)
131{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700132 WARN_ON_ONCE(enable_owner != current);
133 WARN_ON_ONCE(enable_refcnt == 0);
134
135 if (--enable_refcnt)
136 return;
137 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700138 spin_unlock_irqrestore(&enable_lock, flags);
139}
140
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700141static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530142{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700143 /*
144 * .is_prepared is optional for clocks that can prepare
145 * fall back to software usage counter if it is missing
146 */
147 if (!core->ops->is_prepared)
148 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530149
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700150 return core->ops->is_prepared(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530151}
152
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700153static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530154{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700155 /*
156 * .is_enabled is only mandatory for clocks that gate
157 * fall back to software usage counter if .is_enabled is missing
158 */
159 if (!core->ops->is_enabled)
160 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530161
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700162 return core->ops->is_enabled(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530163}
164
Mike Turquetteb24764902012-03-15 23:11:19 -0700165/* caller must hold prepare_lock */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700166static void clk_unprepare_unused_subtree(struct clk_core *core)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100167{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100168 struct clk_core *child;
Ulf Hansson1c155b32013-03-12 20:26:03 +0100169
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100170 lockdep_assert_held(&prepare_lock);
171
Stephen Boydd6968fc2015-04-30 13:54:13 -0700172 hlist_for_each_entry(child, &core->children, child_node)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100173 clk_unprepare_unused_subtree(child);
174
Stephen Boydd6968fc2015-04-30 13:54:13 -0700175 if (core->prepare_count)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100176 return;
177
Stephen Boydd6968fc2015-04-30 13:54:13 -0700178 if (core->flags & CLK_IGNORE_UNUSED)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100179 return;
180
Stephen Boydd6968fc2015-04-30 13:54:13 -0700181 if (clk_core_is_prepared(core)) {
182 trace_clk_unprepare(core);
183 if (core->ops->unprepare_unused)
184 core->ops->unprepare_unused(core->hw);
185 else if (core->ops->unprepare)
186 core->ops->unprepare(core->hw);
187 trace_clk_unprepare_complete(core);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100188 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100189}
190
191/* caller must hold prepare_lock */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700192static void clk_disable_unused_subtree(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700193{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100194 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700195 unsigned long flags;
196
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100197 lockdep_assert_held(&prepare_lock);
198
Stephen Boydd6968fc2015-04-30 13:54:13 -0700199 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700200 clk_disable_unused_subtree(child);
201
Mike Turquetteeab89f62013-03-28 13:59:01 -0700202 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700203
Stephen Boydd6968fc2015-04-30 13:54:13 -0700204 if (core->enable_count)
Mike Turquetteb24764902012-03-15 23:11:19 -0700205 goto unlock_out;
206
Stephen Boydd6968fc2015-04-30 13:54:13 -0700207 if (core->flags & CLK_IGNORE_UNUSED)
Mike Turquetteb24764902012-03-15 23:11:19 -0700208 goto unlock_out;
209
Mike Turquette7c045a52012-12-04 11:00:35 -0800210 /*
211 * some gate clocks have special needs during the disable-unused
212 * sequence. call .disable_unused if available, otherwise fall
213 * back to .disable
214 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700215 if (clk_core_is_enabled(core)) {
216 trace_clk_disable(core);
217 if (core->ops->disable_unused)
218 core->ops->disable_unused(core->hw);
219 else if (core->ops->disable)
220 core->ops->disable(core->hw);
221 trace_clk_disable_complete(core);
Mike Turquette7c045a52012-12-04 11:00:35 -0800222 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700223
224unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700225 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700226}
227
Olof Johansson1e435252013-04-27 14:10:18 -0700228static bool clk_ignore_unused;
229static int __init clk_ignore_unused_setup(char *__unused)
230{
231 clk_ignore_unused = true;
232 return 1;
233}
234__setup("clk_ignore_unused", clk_ignore_unused_setup);
235
Mike Turquetteb24764902012-03-15 23:11:19 -0700236static int clk_disable_unused(void)
237{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700238 struct clk_core *core;
Mike Turquetteb24764902012-03-15 23:11:19 -0700239
Olof Johansson1e435252013-04-27 14:10:18 -0700240 if (clk_ignore_unused) {
241 pr_warn("clk: Not disabling unused clocks\n");
242 return 0;
243 }
244
Mike Turquetteeab89f62013-03-28 13:59:01 -0700245 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700246
Stephen Boydd6968fc2015-04-30 13:54:13 -0700247 hlist_for_each_entry(core, &clk_root_list, child_node)
248 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700249
Stephen Boydd6968fc2015-04-30 13:54:13 -0700250 hlist_for_each_entry(core, &clk_orphan_list, child_node)
251 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700252
Stephen Boydd6968fc2015-04-30 13:54:13 -0700253 hlist_for_each_entry(core, &clk_root_list, child_node)
254 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100255
Stephen Boydd6968fc2015-04-30 13:54:13 -0700256 hlist_for_each_entry(core, &clk_orphan_list, child_node)
257 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100258
Mike Turquetteeab89f62013-03-28 13:59:01 -0700259 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700260
261 return 0;
262}
Saravana Kannand41d5802013-05-09 11:35:01 -0700263late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700264
265/*** helper functions ***/
266
Russ Dill65800b22012-11-26 11:20:09 -0800267const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700268{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100269 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700270}
Niels de Vos48950842012-12-13 13:12:25 +0100271EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700272
Russ Dill65800b22012-11-26 11:20:09 -0800273struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700274{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100275 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700276}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800277EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700278
Russ Dill65800b22012-11-26 11:20:09 -0800279u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700280{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100281 return !clk ? 0 : clk->core->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700282}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800283EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700284
Russ Dill65800b22012-11-26 11:20:09 -0800285struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700286{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100287 if (!clk)
288 return NULL;
289
290 /* TODO: Create a per-user clk and change callers to call clk_put */
291 return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700292}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800293EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700294
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700295static struct clk_core *__clk_lookup_subtree(const char *name,
296 struct clk_core *core)
297{
298 struct clk_core *child;
299 struct clk_core *ret;
300
301 if (!strcmp(core->name, name))
302 return core;
303
304 hlist_for_each_entry(child, &core->children, child_node) {
305 ret = __clk_lookup_subtree(name, child);
306 if (ret)
307 return ret;
308 }
309
310 return NULL;
311}
312
313static struct clk_core *clk_core_lookup(const char *name)
314{
315 struct clk_core *root_clk;
316 struct clk_core *ret;
317
318 if (!name)
319 return NULL;
320
321 /* search the 'proper' clk tree first */
322 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
323 ret = __clk_lookup_subtree(name, root_clk);
324 if (ret)
325 return ret;
326 }
327
328 /* if not found, then search the orphan tree */
329 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
330 ret = __clk_lookup_subtree(name, root_clk);
331 if (ret)
332 return ret;
333 }
334
335 return NULL;
336}
337
Stephen Boydd6968fc2015-04-30 13:54:13 -0700338static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100339 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100340{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700341 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100342 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700343 else if (!core->parents)
344 return clk_core_lookup(core->parent_names[index]);
345 else if (!core->parents[index])
346 return core->parents[index] =
347 clk_core_lookup(core->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100348 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700349 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100350}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100351
352struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
353{
354 struct clk_core *parent;
355
356 if (!clk)
357 return NULL;
358
359 parent = clk_core_get_parent_by_index(clk->core, index);
360
361 return !parent ? NULL : parent->hw->clk;
362}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800363EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100364
Russ Dill65800b22012-11-26 11:20:09 -0800365unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700366{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100367 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700368}
369
Stephen Boydd6968fc2015-04-30 13:54:13 -0700370static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700371{
372 unsigned long ret;
373
Stephen Boydd6968fc2015-04-30 13:54:13 -0700374 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530375 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700376 goto out;
377 }
378
Stephen Boydd6968fc2015-04-30 13:54:13 -0700379 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700380
Stephen Boydd6968fc2015-04-30 13:54:13 -0700381 if (core->flags & CLK_IS_ROOT)
Mike Turquetteb24764902012-03-15 23:11:19 -0700382 goto out;
383
Stephen Boydd6968fc2015-04-30 13:54:13 -0700384 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530385 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700386
387out:
388 return ret;
389}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100390
391unsigned long __clk_get_rate(struct clk *clk)
392{
393 if (!clk)
394 return 0;
395
396 return clk_core_get_rate_nolock(clk->core);
397}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800398EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700399
Stephen Boydd6968fc2015-04-30 13:54:13 -0700400static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100401{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700402 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100403 return 0;
404
Stephen Boydd6968fc2015-04-30 13:54:13 -0700405 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100406}
407
Russ Dill65800b22012-11-26 11:20:09 -0800408unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700409{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100410 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700411}
Thierry Redingb05c6832013-09-03 09:43:51 +0200412EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700413
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100414bool __clk_is_prepared(struct clk *clk)
415{
416 if (!clk)
417 return false;
418
419 return clk_core_is_prepared(clk->core);
420}
421
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100422bool __clk_is_enabled(struct clk *clk)
423{
424 if (!clk)
425 return false;
426
427 return clk_core_is_enabled(clk->core);
428}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800429EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700430
Stephen Boyd15a02c12015-01-19 18:05:28 -0800431static bool mux_is_better_rate(unsigned long rate, unsigned long now,
432 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100433{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800434 if (flags & CLK_MUX_ROUND_CLOSEST)
435 return abs(now - rate) < abs(best - rate);
436
437 return now <= rate && now > best;
438}
439
440static long
441clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100442 unsigned long min_rate,
443 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800444 unsigned long *best_parent_rate,
445 struct clk_hw **best_parent_p,
446 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100447{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100448 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
James Hogane366fdd2013-07-29 12:25:02 +0100449 int i, num_parents;
450 unsigned long parent_rate, best = 0;
451
452 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100453 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
454 parent = core->parent;
455 if (core->flags & CLK_SET_RATE_PARENT)
Javier Martinez Canillas9e0ad7d2015-02-12 14:58:28 +0100456 best = __clk_determine_rate(parent ? parent->hw : NULL,
457 rate, min_rate, max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100458 else if (parent)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100459 best = clk_core_get_rate_nolock(parent);
James Hogane366fdd2013-07-29 12:25:02 +0100460 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100461 best = clk_core_get_rate_nolock(core);
James Hogane366fdd2013-07-29 12:25:02 +0100462 goto out;
463 }
464
465 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100466 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100467 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100468 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100469 if (!parent)
470 continue;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100471 if (core->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100472 parent_rate = __clk_determine_rate(parent->hw, rate,
473 min_rate,
474 max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100475 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100476 parent_rate = clk_core_get_rate_nolock(parent);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800477 if (mux_is_better_rate(rate, parent_rate, best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100478 best_parent = parent;
479 best = parent_rate;
480 }
481 }
482
483out:
484 if (best_parent)
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100485 *best_parent_p = best_parent->hw;
James Hogane366fdd2013-07-29 12:25:02 +0100486 *best_parent_rate = best;
487
488 return best;
489}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800490
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100491struct clk *__clk_lookup(const char *name)
492{
493 struct clk_core *core = clk_core_lookup(name);
494
495 return !core ? NULL : core->hw->clk;
496}
497
Stephen Boydd6968fc2015-04-30 13:54:13 -0700498static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100499 unsigned long *min_rate,
500 unsigned long *max_rate)
501{
502 struct clk *clk_user;
503
504 *min_rate = 0;
505 *max_rate = ULONG_MAX;
506
Stephen Boydd6968fc2015-04-30 13:54:13 -0700507 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100508 *min_rate = max(*min_rate, clk_user->min_rate);
509
Stephen Boydd6968fc2015-04-30 13:54:13 -0700510 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100511 *max_rate = min(*max_rate, clk_user->max_rate);
512}
513
Stephen Boyd15a02c12015-01-19 18:05:28 -0800514/*
515 * Helper for finding best parent to provide a given frequency. This can be used
516 * directly as a determine_rate callback (e.g. for a mux), or from a more
517 * complex clock that may combine a mux with other operations.
518 */
519long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100520 unsigned long min_rate,
521 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800522 unsigned long *best_parent_rate,
523 struct clk_hw **best_parent_p)
524{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100525 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
526 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800527 best_parent_p, 0);
528}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800529EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100530
Stephen Boyd15a02c12015-01-19 18:05:28 -0800531long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100532 unsigned long min_rate,
533 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800534 unsigned long *best_parent_rate,
535 struct clk_hw **best_parent_p)
536{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100537 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
538 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800539 best_parent_p,
540 CLK_MUX_ROUND_CLOSEST);
541}
542EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
543
Mike Turquetteb24764902012-03-15 23:11:19 -0700544/*** clk api ***/
545
Stephen Boydd6968fc2015-04-30 13:54:13 -0700546static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700547{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700548 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700549 return;
550
Stephen Boydd6968fc2015-04-30 13:54:13 -0700551 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700552 return;
553
Stephen Boydd6968fc2015-04-30 13:54:13 -0700554 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700555 return;
556
Stephen Boydd6968fc2015-04-30 13:54:13 -0700557 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700558
Stephen Boydd6968fc2015-04-30 13:54:13 -0700559 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800560
Stephen Boydd6968fc2015-04-30 13:54:13 -0700561 if (core->ops->unprepare)
562 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700563
Stephen Boydd6968fc2015-04-30 13:54:13 -0700564 trace_clk_unprepare_complete(core);
565 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700566}
567
568/**
569 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200570 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700571 *
572 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
573 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
574 * if the operation may sleep. One example is a clk which is accessed over
575 * I2c. In the complex case a clk gate operation may require a fast and a slow
576 * part. It is this reason that clk_unprepare and clk_disable are not mutually
577 * exclusive. In fact clk_disable must be called before clk_unprepare.
578 */
579void clk_unprepare(struct clk *clk)
580{
Stephen Boyd63589e92014-03-26 16:06:37 -0700581 if (IS_ERR_OR_NULL(clk))
582 return;
583
Mike Turquetteeab89f62013-03-28 13:59:01 -0700584 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100585 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700586 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700587}
588EXPORT_SYMBOL_GPL(clk_unprepare);
589
Stephen Boydd6968fc2015-04-30 13:54:13 -0700590static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700591{
592 int ret = 0;
593
Stephen Boydd6968fc2015-04-30 13:54:13 -0700594 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700595 return 0;
596
Stephen Boydd6968fc2015-04-30 13:54:13 -0700597 if (core->prepare_count == 0) {
598 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700599 if (ret)
600 return ret;
601
Stephen Boydd6968fc2015-04-30 13:54:13 -0700602 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800603
Stephen Boydd6968fc2015-04-30 13:54:13 -0700604 if (core->ops->prepare)
605 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800606
Stephen Boydd6968fc2015-04-30 13:54:13 -0700607 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800608
609 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700610 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800611 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700612 }
613 }
614
Stephen Boydd6968fc2015-04-30 13:54:13 -0700615 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700616
617 return 0;
618}
619
620/**
621 * clk_prepare - prepare a clock source
622 * @clk: the clk being prepared
623 *
624 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
625 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
626 * operation may sleep. One example is a clk which is accessed over I2c. In
627 * the complex case a clk ungate operation may require a fast and a slow part.
628 * It is this reason that clk_prepare and clk_enable are not mutually
629 * exclusive. In fact clk_prepare must be called before clk_enable.
630 * Returns 0 on success, -EERROR otherwise.
631 */
632int clk_prepare(struct clk *clk)
633{
634 int ret;
635
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100636 if (!clk)
637 return 0;
638
Mike Turquetteeab89f62013-03-28 13:59:01 -0700639 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100640 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700641 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700642
643 return ret;
644}
645EXPORT_SYMBOL_GPL(clk_prepare);
646
Stephen Boydd6968fc2015-04-30 13:54:13 -0700647static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700648{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700649 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700650 return;
651
Stephen Boydd6968fc2015-04-30 13:54:13 -0700652 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700653 return;
654
Stephen Boydd6968fc2015-04-30 13:54:13 -0700655 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700656 return;
657
Stephen Boydd6968fc2015-04-30 13:54:13 -0700658 trace_clk_disable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800659
Stephen Boydd6968fc2015-04-30 13:54:13 -0700660 if (core->ops->disable)
661 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700662
Stephen Boydd6968fc2015-04-30 13:54:13 -0700663 trace_clk_disable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800664
Stephen Boydd6968fc2015-04-30 13:54:13 -0700665 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100666}
667
Mike Turquetteb24764902012-03-15 23:11:19 -0700668/**
669 * clk_disable - gate a clock
670 * @clk: the clk being gated
671 *
672 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
673 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
674 * clk if the operation is fast and will never sleep. One example is a
675 * SoC-internal clk which is controlled via simple register writes. In the
676 * complex case a clk gate operation may require a fast and a slow part. It is
677 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
678 * In fact clk_disable must be called before clk_unprepare.
679 */
680void clk_disable(struct clk *clk)
681{
682 unsigned long flags;
683
Stephen Boyd63589e92014-03-26 16:06:37 -0700684 if (IS_ERR_OR_NULL(clk))
685 return;
686
Mike Turquetteeab89f62013-03-28 13:59:01 -0700687 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700688 clk_core_disable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700689 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700690}
691EXPORT_SYMBOL_GPL(clk_disable);
692
Stephen Boydd6968fc2015-04-30 13:54:13 -0700693static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700694{
695 int ret = 0;
696
Stephen Boydd6968fc2015-04-30 13:54:13 -0700697 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700698 return 0;
699
Stephen Boydd6968fc2015-04-30 13:54:13 -0700700 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700701 return -ESHUTDOWN;
702
Stephen Boydd6968fc2015-04-30 13:54:13 -0700703 if (core->enable_count == 0) {
704 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700705
706 if (ret)
707 return ret;
708
Stephen Boydd6968fc2015-04-30 13:54:13 -0700709 trace_clk_enable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800710
Stephen Boydd6968fc2015-04-30 13:54:13 -0700711 if (core->ops->enable)
712 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800713
Stephen Boydd6968fc2015-04-30 13:54:13 -0700714 trace_clk_enable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800715
716 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700717 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800718 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700719 }
720 }
721
Stephen Boydd6968fc2015-04-30 13:54:13 -0700722 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700723 return 0;
724}
725
726/**
727 * clk_enable - ungate a clock
728 * @clk: the clk being ungated
729 *
730 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
731 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
732 * if the operation will never sleep. One example is a SoC-internal clk which
733 * is controlled via simple register writes. In the complex case a clk ungate
734 * operation may require a fast and a slow part. It is this reason that
735 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
736 * must be called before clk_enable. Returns 0 on success, -EERROR
737 * otherwise.
738 */
739int clk_enable(struct clk *clk)
740{
741 unsigned long flags;
742 int ret;
743
Dong Aisheng864e1602015-04-30 14:02:19 -0700744 if (!clk)
745 return 0;
746
Mike Turquetteeab89f62013-03-28 13:59:01 -0700747 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700748 ret = clk_core_enable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700749 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700750
751 return ret;
752}
753EXPORT_SYMBOL_GPL(clk_enable);
754
Stephen Boydd6968fc2015-04-30 13:54:13 -0700755static unsigned long clk_core_round_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100756 unsigned long rate,
757 unsigned long min_rate,
758 unsigned long max_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -0700759{
Shawn Guo81536e02012-04-12 20:50:17 +0800760 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100761 struct clk_core *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100762 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700763
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100764 lockdep_assert_held(&prepare_lock);
765
Stephen Boydd6968fc2015-04-30 13:54:13 -0700766 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700767 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700768
Stephen Boydd6968fc2015-04-30 13:54:13 -0700769 parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +0100770 if (parent)
771 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700772
Stephen Boydd6968fc2015-04-30 13:54:13 -0700773 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100774 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700775 return core->ops->determine_rate(core->hw, rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100776 min_rate, max_rate,
777 &parent_rate, &parent_hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700778 } else if (core->ops->round_rate)
779 return core->ops->round_rate(core->hw, rate, &parent_rate);
780 else if (core->flags & CLK_SET_RATE_PARENT)
781 return clk_core_round_rate_nolock(core->parent, rate, min_rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100782 max_rate);
James Hogan71472c02013-07-29 12:25:00 +0100783 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700784 return core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700785}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100786
787/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100788 * __clk_determine_rate - get the closest rate actually supported by a clock
789 * @hw: determine the rate of this clock
790 * @rate: target rate
791 * @min_rate: returned rate must be greater than this rate
792 * @max_rate: returned rate must be less than this rate
793 *
794 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate and
795 * .determine_rate.
796 */
797unsigned long __clk_determine_rate(struct clk_hw *hw,
798 unsigned long rate,
799 unsigned long min_rate,
800 unsigned long max_rate)
801{
802 if (!hw)
803 return 0;
804
805 return clk_core_round_rate_nolock(hw->core, rate, min_rate, max_rate);
806}
807EXPORT_SYMBOL_GPL(__clk_determine_rate);
808
809/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100810 * __clk_round_rate - round the given rate for a clk
811 * @clk: round the rate of this clock
812 * @rate: the rate which is to be rounded
813 *
814 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
815 */
816unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
817{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100818 unsigned long min_rate;
819 unsigned long max_rate;
820
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100821 if (!clk)
822 return 0;
823
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100824 clk_core_get_boundaries(clk->core, &min_rate, &max_rate);
825
826 return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100827}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +0200828EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700829
830/**
831 * clk_round_rate - round the given rate for a clk
832 * @clk: the clk for which we are rounding a rate
833 * @rate: the rate which is to be rounded
834 *
835 * Takes in a rate as input and rounds it to a rate that the clk can actually
836 * use which is then returned. If clk doesn't support round_rate operation
837 * then the parent rate is returned.
838 */
839long clk_round_rate(struct clk *clk, unsigned long rate)
840{
841 unsigned long ret;
842
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100843 if (!clk)
844 return 0;
845
Mike Turquetteeab89f62013-03-28 13:59:01 -0700846 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700847 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700848 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700849
850 return ret;
851}
852EXPORT_SYMBOL_GPL(clk_round_rate);
853
854/**
855 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -0700856 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -0700857 * @msg: clk notifier type (see include/linux/clk.h)
858 * @old_rate: old clk rate
859 * @new_rate: new clk rate
860 *
861 * Triggers a notifier call chain on the clk rate-change notification
862 * for 'clk'. Passes a pointer to the struct clk and the previous
863 * and current rates to the notifier callback. Intended to be called by
864 * internal clock code only. Returns NOTIFY_DONE from the last driver
865 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
866 * a driver returns that.
867 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700868static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -0700869 unsigned long old_rate, unsigned long new_rate)
870{
871 struct clk_notifier *cn;
872 struct clk_notifier_data cnd;
873 int ret = NOTIFY_DONE;
874
Mike Turquetteb24764902012-03-15 23:11:19 -0700875 cnd.old_rate = old_rate;
876 cnd.new_rate = new_rate;
877
878 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700879 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100880 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700881 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
882 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -0700883 }
884 }
885
886 return ret;
887}
888
889/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100890 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -0700891 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100892 *
893 * Walks the subtree of clks starting with clk and recalculates accuracies as
894 * it goes. Note that if a clk does not implement the .recalc_accuracy
895 * callback then it is assumed that the clock will take on the accuracy of it's
896 * parent.
897 *
898 * Caller must hold prepare_lock.
899 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700900static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100901{
902 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100903 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100904
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100905 lockdep_assert_held(&prepare_lock);
906
Stephen Boydd6968fc2015-04-30 13:54:13 -0700907 if (core->parent)
908 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100909
Stephen Boydd6968fc2015-04-30 13:54:13 -0700910 if (core->ops->recalc_accuracy)
911 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100912 parent_accuracy);
913 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700914 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100915
Stephen Boydd6968fc2015-04-30 13:54:13 -0700916 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100917 __clk_recalc_accuracies(child);
918}
919
Stephen Boydd6968fc2015-04-30 13:54:13 -0700920static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100921{
922 unsigned long accuracy;
923
924 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -0700925 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
926 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100927
Stephen Boydd6968fc2015-04-30 13:54:13 -0700928 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100929 clk_prepare_unlock();
930
931 return accuracy;
932}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100933
934/**
935 * clk_get_accuracy - return the accuracy of clk
936 * @clk: the clk whose accuracy is being returned
937 *
938 * Simply returns the cached accuracy of the clk, unless
939 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
940 * issued.
941 * If clk is NULL then returns 0.
942 */
943long clk_get_accuracy(struct clk *clk)
944{
945 if (!clk)
946 return 0;
947
948 return clk_core_get_accuracy(clk->core);
949}
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100950EXPORT_SYMBOL_GPL(clk_get_accuracy);
951
Stephen Boydd6968fc2015-04-30 13:54:13 -0700952static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100953 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700954{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700955 if (core->ops->recalc_rate)
956 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700957 return parent_rate;
958}
959
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100960/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700961 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -0700962 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -0700963 * @msg: notification type (see include/linux/clk.h)
964 *
965 * Walks the subtree of clks starting with clk and recalculates rates as it
966 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200967 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -0700968 *
969 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
970 * if necessary.
971 *
972 * Caller must hold prepare_lock.
973 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700974static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -0700975{
976 unsigned long old_rate;
977 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100978 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700979
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100980 lockdep_assert_held(&prepare_lock);
981
Stephen Boydd6968fc2015-04-30 13:54:13 -0700982 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700983
Stephen Boydd6968fc2015-04-30 13:54:13 -0700984 if (core->parent)
985 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700986
Stephen Boydd6968fc2015-04-30 13:54:13 -0700987 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700988
989 /*
990 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
991 * & ABORT_RATE_CHANGE notifiers
992 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700993 if (core->notifier_count && msg)
994 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700995
Stephen Boydd6968fc2015-04-30 13:54:13 -0700996 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700997 __clk_recalc_rates(child, msg);
998}
999
Stephen Boydd6968fc2015-04-30 13:54:13 -07001000static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001001{
1002 unsigned long rate;
1003
1004 clk_prepare_lock();
1005
Stephen Boydd6968fc2015-04-30 13:54:13 -07001006 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1007 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001008
Stephen Boydd6968fc2015-04-30 13:54:13 -07001009 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001010 clk_prepare_unlock();
1011
1012 return rate;
1013}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001014
Mike Turquetteb24764902012-03-15 23:11:19 -07001015/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001016 * clk_get_rate - return the rate of clk
1017 * @clk: the clk whose rate is being returned
1018 *
1019 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1020 * is set, which means a recalc_rate will be issued.
1021 * If clk is NULL then returns 0.
1022 */
1023unsigned long clk_get_rate(struct clk *clk)
1024{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001025 if (!clk)
1026 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001027
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001028 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001029}
1030EXPORT_SYMBOL_GPL(clk_get_rate);
1031
Stephen Boydd6968fc2015-04-30 13:54:13 -07001032static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001033 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001034{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001035 int i;
James Hogan4935b222013-07-29 12:24:59 +01001036
Stephen Boydd6968fc2015-04-30 13:54:13 -07001037 if (!core->parents) {
1038 core->parents = kcalloc(core->num_parents,
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001039 sizeof(struct clk *), GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001040 if (!core->parents)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001041 return -ENOMEM;
1042 }
James Hogan4935b222013-07-29 12:24:59 +01001043
1044 /*
1045 * find index of new parent clock using cached parent ptrs,
1046 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001047 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001048 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001049 for (i = 0; i < core->num_parents; i++) {
1050 if (core->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001051 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001052
Stephen Boydd6968fc2015-04-30 13:54:13 -07001053 if (core->parents[i])
Tomasz Figada0f0b22013-09-29 02:37:16 +02001054 continue;
1055
Stephen Boydd6968fc2015-04-30 13:54:13 -07001056 if (!strcmp(core->parent_names[i], parent->name)) {
1057 core->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001058 return i;
James Hogan4935b222013-07-29 12:24:59 +01001059 }
1060 }
1061
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001062 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001063}
1064
Stephen Boydd6968fc2015-04-30 13:54:13 -07001065static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001066{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001067 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001068
James Hogan903efc52013-08-29 12:10:51 +01001069 if (new_parent) {
1070 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001071 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001072 new_parent->new_child = NULL;
1073
Stephen Boydd6968fc2015-04-30 13:54:13 -07001074 hlist_add_head(&core->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001075 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001076 hlist_add_head(&core->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001077 }
James Hogan4935b222013-07-29 12:24:59 +01001078
Stephen Boydd6968fc2015-04-30 13:54:13 -07001079 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001080}
1081
Stephen Boydd6968fc2015-04-30 13:54:13 -07001082static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001083 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001084{
1085 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001086 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001087
1088 /*
1089 * Migrate prepare state between parents and prevent race with
1090 * clk_enable().
1091 *
1092 * If the clock is not prepared, then a race with
1093 * clk_enable/disable() is impossible since we already have the
1094 * prepare lock (future calls to clk_enable() need to be preceded by
1095 * a clk_prepare()).
1096 *
1097 * If the clock is prepared, migrate the prepared state to the new
1098 * parent and also protect against a race with clk_enable() by
1099 * forcing the clock and the new parent on. This ensures that all
1100 * future calls to clk_enable() are practically NOPs with respect to
1101 * hardware and software states.
1102 *
1103 * See also: Comment for clk_set_parent() below.
1104 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001105 if (core->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001106 clk_core_prepare(parent);
1107 clk_core_enable(parent);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001108 clk_core_enable(core);
James Hogan4935b222013-07-29 12:24:59 +01001109 }
1110
1111 /* update the clk tree topology */
1112 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001113 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001114 clk_enable_unlock(flags);
1115
Stephen Boyd3fa22522014-01-15 10:47:22 -08001116 return old_parent;
1117}
1118
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001119static void __clk_set_parent_after(struct clk_core *core,
1120 struct clk_core *parent,
1121 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001122{
1123 /*
1124 * Finish the migration of prepare state and undo the changes done
1125 * for preventing a race with clk_enable().
1126 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001127 if (core->prepare_count) {
1128 clk_core_disable(core);
1129 clk_core_disable(old_parent);
1130 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001131 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001132}
1133
Stephen Boydd6968fc2015-04-30 13:54:13 -07001134static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001135 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001136{
1137 unsigned long flags;
1138 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001139 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001140
Stephen Boydd6968fc2015-04-30 13:54:13 -07001141 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001142
Stephen Boydd6968fc2015-04-30 13:54:13 -07001143 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001144
James Hogan4935b222013-07-29 12:24:59 +01001145 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001146 if (parent && core->ops->set_parent)
1147 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001148
Stephen Boydd6968fc2015-04-30 13:54:13 -07001149 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001150
James Hogan4935b222013-07-29 12:24:59 +01001151 if (ret) {
1152 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001153 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001154 clk_enable_unlock(flags);
1155
Stephen Boydd6968fc2015-04-30 13:54:13 -07001156 if (core->prepare_count) {
1157 clk_core_disable(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001158 clk_core_disable(parent);
1159 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001160 }
1161 return ret;
1162 }
1163
Stephen Boydd6968fc2015-04-30 13:54:13 -07001164 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001165
James Hogan4935b222013-07-29 12:24:59 +01001166 return 0;
1167}
1168
Ulf Hanssona093bde2012-08-31 14:21:28 +02001169/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001170 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001171 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001172 * @parent_rate: the "future" rate of clk's parent
1173 *
1174 * Walks the subtree of clks starting with clk, speculating rates as it
1175 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1176 *
1177 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1178 * pre-rate change notifications and returns early if no clks in the
1179 * subtree have subscribed to the notifications. Note that if a clk does not
1180 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001181 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001182 *
1183 * Caller must hold prepare_lock.
1184 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001185static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001186 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001187{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001188 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001189 unsigned long new_rate;
1190 int ret = NOTIFY_DONE;
1191
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001192 lockdep_assert_held(&prepare_lock);
1193
Stephen Boydd6968fc2015-04-30 13:54:13 -07001194 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001195
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001196 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001197 if (core->notifier_count)
1198 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001199
Mike Turquette86bcfa22014-02-24 16:08:41 -08001200 if (ret & NOTIFY_STOP_MASK) {
1201 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001202 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001203 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001204 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001205
Stephen Boydd6968fc2015-04-30 13:54:13 -07001206 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001207 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001208 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001209 break;
1210 }
1211
1212out:
1213 return ret;
1214}
1215
Stephen Boydd6968fc2015-04-30 13:54:13 -07001216static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001217 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001218{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001219 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001220
Stephen Boydd6968fc2015-04-30 13:54:13 -07001221 core->new_rate = new_rate;
1222 core->new_parent = new_parent;
1223 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001224 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001225 core->new_child = NULL;
1226 if (new_parent && new_parent != core->parent)
1227 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001228
Stephen Boydd6968fc2015-04-30 13:54:13 -07001229 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001230 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001231 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001232 }
1233}
1234
1235/*
1236 * calculate the new rates returning the topmost clock that has to be
1237 * changed.
1238 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001239static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001240 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001241{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001242 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001243 struct clk_core *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001244 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001245 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001246 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001247 unsigned long min_rate;
1248 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001249 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001250 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001251
Mike Turquette7452b212012-03-26 14:45:36 -07001252 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001253 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001254 return NULL;
1255
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001256 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001257 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001258 if (parent)
1259 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001260
Stephen Boydd6968fc2015-04-30 13:54:13 -07001261 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001262
James Hogan71472c02013-07-29 12:25:00 +01001263 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001264 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001265 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001266 ret = core->ops->determine_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001267 min_rate,
1268 max_rate,
1269 &best_parent_rate,
1270 &parent_hw);
1271 if (ret < 0)
1272 return NULL;
1273
1274 new_rate = ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001275 parent = parent_hw ? parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001276 } else if (core->ops->round_rate) {
1277 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001278 &best_parent_rate);
1279 if (ret < 0)
1280 return NULL;
1281
1282 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001283 if (new_rate < min_rate || new_rate > max_rate)
1284 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001285 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001286 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001287 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001288 return NULL;
1289 } else {
1290 /* pass-through clock with adjustable parent */
1291 top = clk_calc_new_rates(parent, rate);
1292 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001293 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001294 }
1295
James Hogan71472c02013-07-29 12:25:00 +01001296 /* some clocks must be gated to change parent */
1297 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001298 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001299 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001300 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001301 return NULL;
1302 }
1303
James Hogan71472c02013-07-29 12:25:00 +01001304 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001305 if (parent && core->num_parents > 1) {
1306 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001307 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001308 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001309 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001310 return NULL;
1311 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001312 }
1313
Stephen Boydd6968fc2015-04-30 13:54:13 -07001314 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001315 best_parent_rate != parent->rate)
1316 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001317
1318out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001319 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001320
1321 return top;
1322}
1323
1324/*
1325 * Notify about rate changes in a subtree. Always walk down the whole tree
1326 * so that in case of an error we can walk down the whole tree again and
1327 * abort the change.
1328 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001329static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001330 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001331{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001332 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001333 int ret = NOTIFY_DONE;
1334
Stephen Boydd6968fc2015-04-30 13:54:13 -07001335 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301336 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001337
Stephen Boydd6968fc2015-04-30 13:54:13 -07001338 if (core->notifier_count) {
1339 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001340 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001341 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001342 }
1343
Stephen Boydd6968fc2015-04-30 13:54:13 -07001344 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001345 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001346 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001347 continue;
1348 tmp_clk = clk_propagate_rate_change(child, event);
1349 if (tmp_clk)
1350 fail_clk = tmp_clk;
1351 }
1352
Stephen Boydd6968fc2015-04-30 13:54:13 -07001353 /* handle the new child who might not be in core->children yet */
1354 if (core->new_child) {
1355 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001356 if (tmp_clk)
1357 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001358 }
1359
1360 return fail_clk;
1361}
1362
1363/*
1364 * walk down a subtree and set the new rates notifying the rate
1365 * change on the way
1366 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001367static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001368{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001369 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001370 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001371 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001372 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001373 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001374 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001375
Stephen Boydd6968fc2015-04-30 13:54:13 -07001376 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001377
Stephen Boydd6968fc2015-04-30 13:54:13 -07001378 if (core->new_parent)
1379 best_parent_rate = core->new_parent->rate;
1380 else if (core->parent)
1381 best_parent_rate = core->parent->rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001382
Stephen Boydd6968fc2015-04-30 13:54:13 -07001383 if (core->new_parent && core->new_parent != core->parent) {
1384 old_parent = __clk_set_parent_before(core, core->new_parent);
1385 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001386
Stephen Boydd6968fc2015-04-30 13:54:13 -07001387 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001388 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001389 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001390 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001391 core->new_parent_index);
1392 } else if (core->ops->set_parent) {
1393 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001394 }
1395
Stephen Boydd6968fc2015-04-30 13:54:13 -07001396 trace_clk_set_parent_complete(core, core->new_parent);
1397 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001398 }
1399
Stephen Boydd6968fc2015-04-30 13:54:13 -07001400 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001401
Stephen Boydd6968fc2015-04-30 13:54:13 -07001402 if (!skip_set_rate && core->ops->set_rate)
1403 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001404
Stephen Boydd6968fc2015-04-30 13:54:13 -07001405 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001406
Stephen Boydd6968fc2015-04-30 13:54:13 -07001407 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001408
Stephen Boydd6968fc2015-04-30 13:54:13 -07001409 if (core->notifier_count && old_rate != core->rate)
1410 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001411
Tero Kristo067bb172014-08-21 16:47:45 +03001412 /*
1413 * Use safe iteration, as change_rate can actually swap parents
1414 * for certain clock types.
1415 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001416 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001417 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001418 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001419 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001420 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001421 }
1422
Stephen Boydd6968fc2015-04-30 13:54:13 -07001423 /* handle the new child who might not be in core->children yet */
1424 if (core->new_child)
1425 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001426}
1427
Stephen Boydd6968fc2015-04-30 13:54:13 -07001428static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001429 unsigned long req_rate)
1430{
1431 struct clk_core *top, *fail_clk;
1432 unsigned long rate = req_rate;
1433 int ret = 0;
1434
Stephen Boydd6968fc2015-04-30 13:54:13 -07001435 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001436 return 0;
1437
1438 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001439 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001440 return 0;
1441
Stephen Boydd6968fc2015-04-30 13:54:13 -07001442 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001443 return -EBUSY;
1444
1445 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001446 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001447 if (!top)
1448 return -EINVAL;
1449
1450 /* notify that we are about to change rates */
1451 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1452 if (fail_clk) {
1453 pr_debug("%s: failed to set %s rate\n", __func__,
1454 fail_clk->name);
1455 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1456 return -EBUSY;
1457 }
1458
1459 /* change the rates */
1460 clk_change_rate(top);
1461
Stephen Boydd6968fc2015-04-30 13:54:13 -07001462 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001463
1464 return ret;
1465}
1466
Mike Turquetteb24764902012-03-15 23:11:19 -07001467/**
1468 * clk_set_rate - specify a new rate for clk
1469 * @clk: the clk whose rate is being changed
1470 * @rate: the new rate for clk
1471 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001472 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001473 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001474 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1475 * propagate up to clk's parent; whether or not this happens depends on the
1476 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1477 * after calling .round_rate then upstream parent propagation is ignored. If
1478 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001479 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001480 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1481 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001482 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001483 * Rate changes are accomplished via tree traversal that also recalculates the
1484 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001485 *
1486 * Returns 0 on success, -EERROR otherwise.
1487 */
1488int clk_set_rate(struct clk *clk, unsigned long rate)
1489{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001490 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001491
Mike Turquette89ac8d72013-08-21 23:58:09 -07001492 if (!clk)
1493 return 0;
1494
Mike Turquetteb24764902012-03-15 23:11:19 -07001495 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001496 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001497
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001498 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001499
Mike Turquetteeab89f62013-03-28 13:59:01 -07001500 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001501
1502 return ret;
1503}
1504EXPORT_SYMBOL_GPL(clk_set_rate);
1505
1506/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001507 * clk_set_rate_range - set a rate range for a clock source
1508 * @clk: clock source
1509 * @min: desired minimum clock rate in Hz, inclusive
1510 * @max: desired maximum clock rate in Hz, inclusive
1511 *
1512 * Returns success (0) or negative errno.
1513 */
1514int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1515{
1516 int ret = 0;
1517
1518 if (!clk)
1519 return 0;
1520
1521 if (min > max) {
1522 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1523 __func__, clk->core->name, clk->dev_id, clk->con_id,
1524 min, max);
1525 return -EINVAL;
1526 }
1527
1528 clk_prepare_lock();
1529
1530 if (min != clk->min_rate || max != clk->max_rate) {
1531 clk->min_rate = min;
1532 clk->max_rate = max;
1533 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1534 }
1535
1536 clk_prepare_unlock();
1537
1538 return ret;
1539}
1540EXPORT_SYMBOL_GPL(clk_set_rate_range);
1541
1542/**
1543 * clk_set_min_rate - set a minimum clock rate for a clock source
1544 * @clk: clock source
1545 * @rate: desired minimum clock rate in Hz, inclusive
1546 *
1547 * Returns success (0) or negative errno.
1548 */
1549int clk_set_min_rate(struct clk *clk, unsigned long rate)
1550{
1551 if (!clk)
1552 return 0;
1553
1554 return clk_set_rate_range(clk, rate, clk->max_rate);
1555}
1556EXPORT_SYMBOL_GPL(clk_set_min_rate);
1557
1558/**
1559 * clk_set_max_rate - set a maximum clock rate for a clock source
1560 * @clk: clock source
1561 * @rate: desired maximum clock rate in Hz, inclusive
1562 *
1563 * Returns success (0) or negative errno.
1564 */
1565int clk_set_max_rate(struct clk *clk, unsigned long rate)
1566{
1567 if (!clk)
1568 return 0;
1569
1570 return clk_set_rate_range(clk, clk->min_rate, rate);
1571}
1572EXPORT_SYMBOL_GPL(clk_set_max_rate);
1573
1574/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001575 * clk_get_parent - return the parent of a clk
1576 * @clk: the clk whose parent gets returned
1577 *
1578 * Simply returns clk->parent. Returns NULL if clk is NULL.
1579 */
1580struct clk *clk_get_parent(struct clk *clk)
1581{
1582 struct clk *parent;
1583
Mike Turquetteeab89f62013-03-28 13:59:01 -07001584 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001585 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001586 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001587
1588 return parent;
1589}
1590EXPORT_SYMBOL_GPL(clk_get_parent);
1591
1592/*
1593 * .get_parent is mandatory for clocks with multiple possible parents. It is
1594 * optional for single-parent clocks. Always call .get_parent if it is
1595 * available and WARN if it is missing for multi-parent clocks.
1596 *
1597 * For single-parent clocks without .get_parent, first check to see if the
1598 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001599 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001600 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001601static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001602{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001603 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001604 u8 index;
1605
1606 /* handle the trivial cases */
1607
Stephen Boydd6968fc2015-04-30 13:54:13 -07001608 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -07001609 goto out;
1610
Stephen Boydd6968fc2015-04-30 13:54:13 -07001611 if (core->num_parents == 1) {
1612 if (IS_ERR_OR_NULL(core->parent))
1613 core->parent = clk_core_lookup(core->parent_names[0]);
1614 ret = core->parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001615 goto out;
1616 }
1617
Stephen Boydd6968fc2015-04-30 13:54:13 -07001618 if (!core->ops->get_parent) {
1619 WARN(!core->ops->get_parent,
Mike Turquetteb24764902012-03-15 23:11:19 -07001620 "%s: multi-parent clocks must implement .get_parent\n",
1621 __func__);
1622 goto out;
1623 };
1624
1625 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07001626 * Do our best to cache parent clocks in core->parents. This prevents
1627 * unnecessary and expensive lookups. We don't set core->parent here;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001628 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001629 */
1630
Stephen Boydd6968fc2015-04-30 13:54:13 -07001631 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001632
Stephen Boydd6968fc2015-04-30 13:54:13 -07001633 if (!core->parents)
1634 core->parents =
1635 kcalloc(core->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001636 GFP_KERNEL);
1637
Stephen Boydd6968fc2015-04-30 13:54:13 -07001638 ret = clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001639
1640out:
1641 return ret;
1642}
1643
Stephen Boydd6968fc2015-04-30 13:54:13 -07001644static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001645 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001646{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001647 clk_reparent(core, new_parent);
1648 __clk_recalc_accuracies(core);
1649 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07001650}
1651
Mike Turquetteb24764902012-03-15 23:11:19 -07001652/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001653 * clk_has_parent - check if a clock is a possible parent for another
1654 * @clk: clock source
1655 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07001656 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001657 * This function can be used in drivers that need to check that a clock can be
1658 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001659 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001660 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001661 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001662bool clk_has_parent(struct clk *clk, struct clk *parent)
1663{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001664 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001665 unsigned int i;
1666
1667 /* NULL clocks should be nops, so return success if either is NULL. */
1668 if (!clk || !parent)
1669 return true;
1670
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001671 core = clk->core;
1672 parent_core = parent->core;
1673
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001674 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001675 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001676 return true;
1677
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001678 for (i = 0; i < core->num_parents; i++)
1679 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001680 return true;
1681
1682 return false;
1683}
1684EXPORT_SYMBOL_GPL(clk_has_parent);
1685
Stephen Boydd6968fc2015-04-30 13:54:13 -07001686static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001687{
1688 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001689 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001690 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001691
Stephen Boydd6968fc2015-04-30 13:54:13 -07001692 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07001693 return 0;
1694
Mike Turquetteb24764902012-03-15 23:11:19 -07001695 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001696 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001697
Stephen Boydd6968fc2015-04-30 13:54:13 -07001698 if (core->parent == parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001699 goto out;
1700
Stephen Boydb61c43c2015-02-02 14:11:25 -08001701 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001702 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08001703 ret = -ENOSYS;
1704 goto out;
1705 }
1706
Ulf Hansson031dcc92013-04-02 23:09:38 +02001707 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001708 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001709 ret = -EBUSY;
1710 goto out;
1711 }
1712
1713 /* try finding the new parent index */
1714 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001715 p_index = clk_fetch_parent_index(core, parent);
Ulf Hansson031dcc92013-04-02 23:09:38 +02001716 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001717 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001718 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001719 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001720 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001721 goto out;
1722 }
1723 }
1724
Mike Turquetteb24764902012-03-15 23:11:19 -07001725 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001726 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001727
1728 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001729 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001730 goto out;
1731
Ulf Hansson031dcc92013-04-02 23:09:38 +02001732 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001733 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001734
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001735 /* propagate rate an accuracy recalculation accordingly */
1736 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001737 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001738 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001739 __clk_recalc_rates(core, POST_RATE_CHANGE);
1740 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001741 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001742
1743out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001744 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001745
1746 return ret;
1747}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001748
1749/**
1750 * clk_set_parent - switch the parent of a mux clk
1751 * @clk: the mux clk whose input we are switching
1752 * @parent: the new input to clk
1753 *
1754 * Re-parent clk to use parent as its new input source. If clk is in
1755 * prepared state, the clk will get enabled for the duration of this call. If
1756 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1757 * that, the reparenting is glitchy in hardware, etc), use the
1758 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1759 *
1760 * After successfully changing clk's parent clk_set_parent will update the
1761 * clk topology, sysfs topology and propagate rate recalculation via
1762 * __clk_recalc_rates.
1763 *
1764 * Returns 0 on success, -EERROR otherwise.
1765 */
1766int clk_set_parent(struct clk *clk, struct clk *parent)
1767{
1768 if (!clk)
1769 return 0;
1770
1771 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1772}
Mike Turquetteb24764902012-03-15 23:11:19 -07001773EXPORT_SYMBOL_GPL(clk_set_parent);
1774
1775/**
Mike Turquettee59c5372014-02-18 21:21:25 -08001776 * clk_set_phase - adjust the phase shift of a clock signal
1777 * @clk: clock signal source
1778 * @degrees: number of degrees the signal is shifted
1779 *
1780 * Shifts the phase of a clock signal by the specified
1781 * degrees. Returns 0 on success, -EERROR otherwise.
1782 *
1783 * This function makes no distinction about the input or reference
1784 * signal that we adjust the clock signal phase against. For example
1785 * phase locked-loop clock signal generators we may shift phase with
1786 * respect to feedback clock signal input, but for other cases the
1787 * clock phase may be shifted with respect to some other, unspecified
1788 * signal.
1789 *
1790 * Additionally the concept of phase shift does not propagate through
1791 * the clock tree hierarchy, which sets it apart from clock rates and
1792 * clock accuracy. A parent clock phase attribute does not have an
1793 * impact on the phase attribute of a child clock.
1794 */
1795int clk_set_phase(struct clk *clk, int degrees)
1796{
Stephen Boyd08b95752015-02-02 14:09:43 -08001797 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08001798
1799 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08001800 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08001801
1802 /* sanity check degrees */
1803 degrees %= 360;
1804 if (degrees < 0)
1805 degrees += 360;
1806
1807 clk_prepare_lock();
1808
Stephen Boyddfc202e2015-02-02 14:37:41 -08001809 trace_clk_set_phase(clk->core, degrees);
1810
Stephen Boyd08b95752015-02-02 14:09:43 -08001811 if (clk->core->ops->set_phase)
1812 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08001813
Stephen Boyddfc202e2015-02-02 14:37:41 -08001814 trace_clk_set_phase_complete(clk->core, degrees);
1815
Mike Turquettee59c5372014-02-18 21:21:25 -08001816 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001817 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08001818
Mike Turquettee59c5372014-02-18 21:21:25 -08001819 clk_prepare_unlock();
1820
Mike Turquettee59c5372014-02-18 21:21:25 -08001821 return ret;
1822}
Maxime Ripard9767b042015-01-20 22:23:43 +01001823EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001824
Stephen Boydd6968fc2015-04-30 13:54:13 -07001825static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08001826{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07001827 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08001828
1829 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001830 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08001831 clk_prepare_unlock();
1832
Mike Turquettee59c5372014-02-18 21:21:25 -08001833 return ret;
1834}
1835
1836/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001837 * clk_get_phase - return the phase shift of a clock signal
1838 * @clk: clock signal source
1839 *
1840 * Returns the phase shift of a clock node in degrees, otherwise returns
1841 * -EERROR.
1842 */
1843int clk_get_phase(struct clk *clk)
1844{
1845 if (!clk)
1846 return 0;
1847
1848 return clk_core_get_phase(clk->core);
1849}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001850EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07001851
1852/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08001853 * clk_is_match - check if two clk's point to the same hardware clock
1854 * @p: clk compared against q
1855 * @q: clk compared against p
1856 *
1857 * Returns true if the two struct clk pointers both point to the same hardware
1858 * clock node. Put differently, returns true if struct clk *p and struct clk *q
1859 * share the same struct clk_core object.
1860 *
1861 * Returns false otherwise. Note that two NULL clks are treated as matching.
1862 */
1863bool clk_is_match(const struct clk *p, const struct clk *q)
1864{
1865 /* trivial case: identical struct clk's or both NULL */
1866 if (p == q)
1867 return true;
1868
1869 /* true if clk->core pointers match. Avoid derefing garbage */
1870 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
1871 if (p->core == q->core)
1872 return true;
1873
1874 return false;
1875}
1876EXPORT_SYMBOL_GPL(clk_is_match);
1877
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001878/*** debugfs support ***/
1879
1880#ifdef CONFIG_DEBUG_FS
1881#include <linux/debugfs.h>
1882
1883static struct dentry *rootdir;
1884static int inited = 0;
1885static DEFINE_MUTEX(clk_debug_lock);
1886static HLIST_HEAD(clk_debug_list);
1887
1888static struct hlist_head *all_lists[] = {
1889 &clk_root_list,
1890 &clk_orphan_list,
1891 NULL,
1892};
1893
1894static struct hlist_head *orphan_list[] = {
1895 &clk_orphan_list,
1896 NULL,
1897};
1898
1899static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
1900 int level)
1901{
1902 if (!c)
1903 return;
1904
1905 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
1906 level * 3 + 1, "",
1907 30 - level * 3, c->name,
1908 c->enable_count, c->prepare_count, clk_core_get_rate(c),
1909 clk_core_get_accuracy(c), clk_core_get_phase(c));
1910}
1911
1912static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
1913 int level)
1914{
1915 struct clk_core *child;
1916
1917 if (!c)
1918 return;
1919
1920 clk_summary_show_one(s, c, level);
1921
1922 hlist_for_each_entry(child, &c->children, child_node)
1923 clk_summary_show_subtree(s, child, level + 1);
1924}
1925
1926static int clk_summary_show(struct seq_file *s, void *data)
1927{
1928 struct clk_core *c;
1929 struct hlist_head **lists = (struct hlist_head **)s->private;
1930
1931 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
1932 seq_puts(s, "----------------------------------------------------------------------------------------\n");
1933
1934 clk_prepare_lock();
1935
1936 for (; *lists; lists++)
1937 hlist_for_each_entry(c, *lists, child_node)
1938 clk_summary_show_subtree(s, c, 0);
1939
1940 clk_prepare_unlock();
1941
1942 return 0;
1943}
1944
1945
1946static int clk_summary_open(struct inode *inode, struct file *file)
1947{
1948 return single_open(file, clk_summary_show, inode->i_private);
1949}
1950
1951static const struct file_operations clk_summary_fops = {
1952 .open = clk_summary_open,
1953 .read = seq_read,
1954 .llseek = seq_lseek,
1955 .release = single_release,
1956};
1957
1958static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
1959{
1960 if (!c)
1961 return;
1962
1963 seq_printf(s, "\"%s\": { ", c->name);
1964 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
1965 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
1966 seq_printf(s, "\"rate\": %lu", clk_core_get_rate(c));
1967 seq_printf(s, "\"accuracy\": %lu", clk_core_get_accuracy(c));
1968 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
1969}
1970
1971static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
1972{
1973 struct clk_core *child;
1974
1975 if (!c)
1976 return;
1977
1978 clk_dump_one(s, c, level);
1979
1980 hlist_for_each_entry(child, &c->children, child_node) {
1981 seq_printf(s, ",");
1982 clk_dump_subtree(s, child, level + 1);
1983 }
1984
1985 seq_printf(s, "}");
1986}
1987
1988static int clk_dump(struct seq_file *s, void *data)
1989{
1990 struct clk_core *c;
1991 bool first_node = true;
1992 struct hlist_head **lists = (struct hlist_head **)s->private;
1993
1994 seq_printf(s, "{");
1995
1996 clk_prepare_lock();
1997
1998 for (; *lists; lists++) {
1999 hlist_for_each_entry(c, *lists, child_node) {
2000 if (!first_node)
2001 seq_puts(s, ",");
2002 first_node = false;
2003 clk_dump_subtree(s, c, 0);
2004 }
2005 }
2006
2007 clk_prepare_unlock();
2008
2009 seq_printf(s, "}");
2010 return 0;
2011}
2012
2013
2014static int clk_dump_open(struct inode *inode, struct file *file)
2015{
2016 return single_open(file, clk_dump, inode->i_private);
2017}
2018
2019static const struct file_operations clk_dump_fops = {
2020 .open = clk_dump_open,
2021 .read = seq_read,
2022 .llseek = seq_lseek,
2023 .release = single_release,
2024};
2025
2026static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2027{
2028 struct dentry *d;
2029 int ret = -ENOMEM;
2030
2031 if (!core || !pdentry) {
2032 ret = -EINVAL;
2033 goto out;
2034 }
2035
2036 d = debugfs_create_dir(core->name, pdentry);
2037 if (!d)
2038 goto out;
2039
2040 core->dentry = d;
2041
2042 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2043 (u32 *)&core->rate);
2044 if (!d)
2045 goto err_out;
2046
2047 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2048 (u32 *)&core->accuracy);
2049 if (!d)
2050 goto err_out;
2051
2052 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2053 (u32 *)&core->phase);
2054 if (!d)
2055 goto err_out;
2056
2057 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2058 (u32 *)&core->flags);
2059 if (!d)
2060 goto err_out;
2061
2062 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2063 (u32 *)&core->prepare_count);
2064 if (!d)
2065 goto err_out;
2066
2067 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2068 (u32 *)&core->enable_count);
2069 if (!d)
2070 goto err_out;
2071
2072 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2073 (u32 *)&core->notifier_count);
2074 if (!d)
2075 goto err_out;
2076
2077 if (core->ops->debug_init) {
2078 ret = core->ops->debug_init(core->hw, core->dentry);
2079 if (ret)
2080 goto err_out;
2081 }
2082
2083 ret = 0;
2084 goto out;
2085
2086err_out:
2087 debugfs_remove_recursive(core->dentry);
2088 core->dentry = NULL;
2089out:
2090 return ret;
2091}
2092
2093/**
2094 * clk_debug_register - add a clk node to the debugfs clk tree
2095 * @core: the clk being added to the debugfs clk tree
2096 *
2097 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
2098 * initialized. Otherwise it bails out early since the debugfs clk tree
2099 * will be created lazily by clk_debug_init as part of a late_initcall.
2100 */
2101static int clk_debug_register(struct clk_core *core)
2102{
2103 int ret = 0;
2104
2105 mutex_lock(&clk_debug_lock);
2106 hlist_add_head(&core->debug_node, &clk_debug_list);
2107
2108 if (!inited)
2109 goto unlock;
2110
2111 ret = clk_debug_create_one(core, rootdir);
2112unlock:
2113 mutex_unlock(&clk_debug_lock);
2114
2115 return ret;
2116}
2117
2118 /**
2119 * clk_debug_unregister - remove a clk node from the debugfs clk tree
2120 * @core: the clk being removed from the debugfs clk tree
2121 *
2122 * Dynamically removes a clk and all it's children clk nodes from the
2123 * debugfs clk tree if clk->dentry points to debugfs created by
2124 * clk_debug_register in __clk_init.
2125 */
2126static void clk_debug_unregister(struct clk_core *core)
2127{
2128 mutex_lock(&clk_debug_lock);
2129 hlist_del_init(&core->debug_node);
2130 debugfs_remove_recursive(core->dentry);
2131 core->dentry = NULL;
2132 mutex_unlock(&clk_debug_lock);
2133}
2134
2135struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2136 void *data, const struct file_operations *fops)
2137{
2138 struct dentry *d = NULL;
2139
2140 if (hw->core->dentry)
2141 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2142 fops);
2143
2144 return d;
2145}
2146EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2147
2148/**
2149 * clk_debug_init - lazily create the debugfs clk tree visualization
2150 *
2151 * clks are often initialized very early during boot before memory can
2152 * be dynamically allocated and well before debugfs is setup.
2153 * clk_debug_init walks the clk tree hierarchy while holding
2154 * prepare_lock and creates the topology as part of a late_initcall,
2155 * thus insuring that clks initialized very early will still be
2156 * represented in the debugfs clk tree. This function should only be
2157 * called once at boot-time, and all other clks added dynamically will
2158 * be done so with clk_debug_register.
2159 */
2160static int __init clk_debug_init(void)
2161{
2162 struct clk_core *core;
2163 struct dentry *d;
2164
2165 rootdir = debugfs_create_dir("clk", NULL);
2166
2167 if (!rootdir)
2168 return -ENOMEM;
2169
2170 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2171 &clk_summary_fops);
2172 if (!d)
2173 return -ENOMEM;
2174
2175 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2176 &clk_dump_fops);
2177 if (!d)
2178 return -ENOMEM;
2179
2180 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2181 &orphan_list, &clk_summary_fops);
2182 if (!d)
2183 return -ENOMEM;
2184
2185 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2186 &orphan_list, &clk_dump_fops);
2187 if (!d)
2188 return -ENOMEM;
2189
2190 mutex_lock(&clk_debug_lock);
2191 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2192 clk_debug_create_one(core, rootdir);
2193
2194 inited = 1;
2195 mutex_unlock(&clk_debug_lock);
2196
2197 return 0;
2198}
2199late_initcall(clk_debug_init);
2200#else
2201static inline int clk_debug_register(struct clk_core *core) { return 0; }
2202static inline void clk_debug_reparent(struct clk_core *core,
2203 struct clk_core *new_parent)
2204{
2205}
2206static inline void clk_debug_unregister(struct clk_core *core)
2207{
2208}
2209#endif
2210
Michael Turquette3d3801e2015-02-25 09:11:01 -08002211/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002212 * __clk_init - initialize the data structures in a struct clk
2213 * @dev: device initializing this clk, placeholder for now
2214 * @clk: clk being initialized
2215 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002216 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002217 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002218 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002219static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002220{
Mike Turquetted1302a32012-03-29 14:30:40 -07002221 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002222 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002223 struct hlist_node *tmp2;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002224 struct clk_core *core;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002225 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002226
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002227 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002228 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002229
Stephen Boydd6968fc2015-04-30 13:54:13 -07002230 core = clk_user->core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002231
Mike Turquetteeab89f62013-03-28 13:59:01 -07002232 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002233
2234 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002235 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002236 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002237 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002238 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002239 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002240 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002241
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002242 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002243 if (core->ops->set_rate &&
2244 !((core->ops->round_rate || core->ops->determine_rate) &&
2245 core->ops->recalc_rate)) {
James Hogan71472c02013-07-29 12:25:00 +01002246 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002247 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002248 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002249 goto out;
2250 }
2251
Stephen Boydd6968fc2015-04-30 13:54:13 -07002252 if (core->ops->set_parent && !core->ops->get_parent) {
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002253 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002254 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002255 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002256 goto out;
2257 }
2258
Stephen Boydd6968fc2015-04-30 13:54:13 -07002259 if (core->ops->set_rate_and_parent &&
2260 !(core->ops->set_parent && core->ops->set_rate)) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002261 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002262 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002263 ret = -EINVAL;
2264 goto out;
2265 }
2266
Mike Turquetteb24764902012-03-15 23:11:19 -07002267 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002268 for (i = 0; i < core->num_parents; i++)
2269 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002270 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002271 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002272
2273 /*
2274 * Allocate an array of struct clk *'s to avoid unnecessary string
2275 * look-ups of clk's possible parents. This can fail for clocks passed
Stephen Boydd6968fc2015-04-30 13:54:13 -07002276 * in to clk_init during early boot; thus any access to core->parents[]
Mike Turquetteb24764902012-03-15 23:11:19 -07002277 * must always check for a NULL pointer and try to populate it if
2278 * necessary.
2279 *
Stephen Boydd6968fc2015-04-30 13:54:13 -07002280 * If core->parents is not NULL we skip this entire block. This allows
2281 * for clock drivers to statically initialize core->parents.
Mike Turquetteb24764902012-03-15 23:11:19 -07002282 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002283 if (core->num_parents > 1 && !core->parents) {
2284 core->parents = kcalloc(core->num_parents, sizeof(struct clk *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002285 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002286 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002287 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002288 * clk_init'd; thus any access to clk->parents[] must check
2289 * for a NULL pointer. We can always perform lazy lookups for
2290 * missing parents later on.
2291 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002292 if (core->parents)
2293 for (i = 0; i < core->num_parents; i++)
2294 core->parents[i] =
2295 clk_core_lookup(core->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002296 }
2297
Stephen Boydd6968fc2015-04-30 13:54:13 -07002298 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002299
2300 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07002301 * Populate core->parent if parent has already been __clk_init'd. If
Mike Turquetteb24764902012-03-15 23:11:19 -07002302 * parent has not yet been __clk_init'd then place clk in the orphan
2303 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2304 * clk list.
2305 *
2306 * Every time a new clk is clk_init'd then we walk the list of orphan
2307 * clocks and re-parent any that are children of the clock currently
2308 * being clk_init'd.
2309 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002310 if (core->parent)
2311 hlist_add_head(&core->child_node,
2312 &core->parent->children);
2313 else if (core->flags & CLK_IS_ROOT)
2314 hlist_add_head(&core->child_node, &clk_root_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002315 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002316 hlist_add_head(&core->child_node, &clk_orphan_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002317
2318 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002319 * Set clk's accuracy. The preferred method is to use
2320 * .recalc_accuracy. For simple clocks and lazy developers the default
2321 * fallback is to use the parent's accuracy. If a clock doesn't have a
2322 * parent (or is orphaned) then accuracy is set to zero (perfect
2323 * clock).
2324 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002325 if (core->ops->recalc_accuracy)
2326 core->accuracy = core->ops->recalc_accuracy(core->hw,
2327 __clk_get_accuracy(core->parent));
2328 else if (core->parent)
2329 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002330 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002331 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002332
2333 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002334 * Set clk's phase.
2335 * Since a phase is by definition relative to its parent, just
2336 * query the current clock phase, or just assume it's in phase.
2337 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002338 if (core->ops->get_phase)
2339 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002340 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002341 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002342
2343 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002344 * Set clk's rate. The preferred method is to use .recalc_rate. For
2345 * simple clocks and lazy developers the default fallback is to use the
2346 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2347 * then rate is set to zero.
2348 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002349 if (core->ops->recalc_rate)
2350 rate = core->ops->recalc_rate(core->hw,
2351 clk_core_get_rate_nolock(core->parent));
2352 else if (core->parent)
2353 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002354 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002355 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002356 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002357
2358 /*
2359 * walk the list of orphan clocks and reparent any that are children of
2360 * this clock
2361 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002362 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002363 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002364 i = orphan->ops->get_parent(orphan->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002365 if (!strcmp(core->name, orphan->parent_names[i]))
2366 clk_core_reparent(orphan, core);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002367 continue;
2368 }
2369
Mike Turquetteb24764902012-03-15 23:11:19 -07002370 for (i = 0; i < orphan->num_parents; i++)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002371 if (!strcmp(core->name, orphan->parent_names[i])) {
2372 clk_core_reparent(orphan, core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002373 break;
2374 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002375 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002376
2377 /*
2378 * optional platform-specific magic
2379 *
2380 * The .init callback is not used by any of the basic clock types, but
2381 * exists for weird hardware that must perform initialization magic.
2382 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002383 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002384 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002385 if (core->ops->init)
2386 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002387
Stephen Boydd6968fc2015-04-30 13:54:13 -07002388 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002389out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002390 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002391
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002392 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002393 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002394
Mike Turquetted1302a32012-03-29 14:30:40 -07002395 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002396}
2397
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002398struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2399 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002400{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002401 struct clk *clk;
2402
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002403 /* This is to allow this function to be chained to others */
2404 if (!hw || IS_ERR(hw))
2405 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002406
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002407 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2408 if (!clk)
2409 return ERR_PTR(-ENOMEM);
2410
2411 clk->core = hw->core;
2412 clk->dev_id = dev_id;
2413 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002414 clk->max_rate = ULONG_MAX;
2415
2416 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002417 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002418 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002419
2420 return clk;
2421}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002422
Stephen Boyd73e0e492015-02-06 11:42:43 -08002423void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002424{
2425 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002426 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002427 clk_prepare_unlock();
2428
2429 kfree(clk);
2430}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002431
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002432/**
2433 * clk_register - allocate a new clock, register it and return an opaque cookie
2434 * @dev: device that is registering this clock
2435 * @hw: link to hardware-specific clock data
2436 *
2437 * clk_register is the primary interface for populating the clock tree with new
2438 * clock nodes. It returns a pointer to the newly allocated struct clk which
2439 * cannot be dereferenced by driver code but may be used in conjuction with the
2440 * rest of the clock API. In the event of an error clk_register will return an
2441 * error code; drivers must test for an error code after calling clk_register.
2442 */
2443struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002444{
Mike Turquetted1302a32012-03-29 14:30:40 -07002445 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002446 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002447
Stephen Boydd6968fc2015-04-30 13:54:13 -07002448 core = kzalloc(sizeof(*core), GFP_KERNEL);
2449 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002450 ret = -ENOMEM;
2451 goto fail_out;
2452 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002453
Stephen Boydd6968fc2015-04-30 13:54:13 -07002454 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2455 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002456 ret = -ENOMEM;
2457 goto fail_name;
2458 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002459 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002460 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002461 core->owner = dev->driver->owner;
2462 core->hw = hw;
2463 core->flags = hw->init->flags;
2464 core->num_parents = hw->init->num_parents;
2465 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002466
Mike Turquetted1302a32012-03-29 14:30:40 -07002467 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002468 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002469 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002470
Stephen Boydd6968fc2015-04-30 13:54:13 -07002471 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002472 ret = -ENOMEM;
2473 goto fail_parent_names;
2474 }
2475
2476
2477 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002478 for (i = 0; i < core->num_parents; i++) {
2479 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002480 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002481 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002482 ret = -ENOMEM;
2483 goto fail_parent_names_copy;
2484 }
2485 }
2486
Stephen Boydd6968fc2015-04-30 13:54:13 -07002487 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002488
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002489 hw->clk = __clk_create_clk(hw, NULL, NULL);
2490 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002491 ret = PTR_ERR(hw->clk);
2492 goto fail_parent_names_copy;
2493 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002494
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002495 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002496 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002497 return hw->clk;
2498
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002499 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002500 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002501
2502fail_parent_names_copy:
2503 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002504 kfree_const(core->parent_names[i]);
2505 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07002506fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002507 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002508fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002509 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002510fail_out:
2511 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002512}
2513EXPORT_SYMBOL_GPL(clk_register);
2514
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002515/*
2516 * Free memory allocated for a clock.
2517 * Caller must hold prepare_lock.
2518 */
2519static void __clk_release(struct kref *ref)
2520{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002521 struct clk_core *core = container_of(ref, struct clk_core, ref);
2522 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002523
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002524 lockdep_assert_held(&prepare_lock);
2525
Stephen Boydd6968fc2015-04-30 13:54:13 -07002526 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002527 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002528 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002529
Stephen Boydd6968fc2015-04-30 13:54:13 -07002530 kfree(core->parent_names);
2531 kfree_const(core->name);
2532 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002533}
2534
2535/*
2536 * Empty clk_ops for unregistered clocks. These are used temporarily
2537 * after clk_unregister() was called on a clock and until last clock
2538 * consumer calls clk_put() and the struct clk object is freed.
2539 */
2540static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2541{
2542 return -ENXIO;
2543}
2544
2545static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2546{
2547 WARN_ON_ONCE(1);
2548}
2549
2550static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2551 unsigned long parent_rate)
2552{
2553 return -ENXIO;
2554}
2555
2556static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2557{
2558 return -ENXIO;
2559}
2560
2561static const struct clk_ops clk_nodrv_ops = {
2562 .enable = clk_nodrv_prepare_enable,
2563 .disable = clk_nodrv_disable_unprepare,
2564 .prepare = clk_nodrv_prepare_enable,
2565 .unprepare = clk_nodrv_disable_unprepare,
2566 .set_rate = clk_nodrv_set_rate,
2567 .set_parent = clk_nodrv_set_parent,
2568};
2569
Mark Brown1df5c932012-04-18 09:07:12 +01002570/**
2571 * clk_unregister - unregister a currently registered clock
2572 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002573 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002574void clk_unregister(struct clk *clk)
2575{
2576 unsigned long flags;
2577
Stephen Boyd6314b672014-09-04 23:37:49 -07002578 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2579 return;
2580
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002581 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002582
2583 clk_prepare_lock();
2584
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002585 if (clk->core->ops == &clk_nodrv_ops) {
2586 pr_err("%s: unregistered clock: %s\n", __func__,
2587 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002588 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002589 }
2590 /*
2591 * Assign empty clock ops for consumers that might still hold
2592 * a reference to this clock.
2593 */
2594 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002595 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002596 clk_enable_unlock(flags);
2597
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002598 if (!hlist_empty(&clk->core->children)) {
2599 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002600 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002601
2602 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002603 hlist_for_each_entry_safe(child, t, &clk->core->children,
2604 child_node)
2605 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002606 }
2607
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002608 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002609
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002610 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002611 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002612 __func__, clk->core->name);
2613 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002614
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002615 clk_prepare_unlock();
2616}
Mark Brown1df5c932012-04-18 09:07:12 +01002617EXPORT_SYMBOL_GPL(clk_unregister);
2618
Stephen Boyd46c87732012-09-24 13:38:04 -07002619static void devm_clk_release(struct device *dev, void *res)
2620{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002621 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002622}
2623
2624/**
2625 * devm_clk_register - resource managed clk_register()
2626 * @dev: device that is registering this clock
2627 * @hw: link to hardware-specific clock data
2628 *
2629 * Managed clk_register(). Clocks returned from this function are
2630 * automatically clk_unregister()ed on driver detach. See clk_register() for
2631 * more information.
2632 */
2633struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2634{
2635 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002636 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002637
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002638 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2639 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002640 return ERR_PTR(-ENOMEM);
2641
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002642 clk = clk_register(dev, hw);
2643 if (!IS_ERR(clk)) {
2644 *clkp = clk;
2645 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002646 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002647 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002648 }
2649
2650 return clk;
2651}
2652EXPORT_SYMBOL_GPL(devm_clk_register);
2653
2654static int devm_clk_match(struct device *dev, void *res, void *data)
2655{
2656 struct clk *c = res;
2657 if (WARN_ON(!c))
2658 return 0;
2659 return c == data;
2660}
2661
2662/**
2663 * devm_clk_unregister - resource managed clk_unregister()
2664 * @clk: clock to unregister
2665 *
2666 * Deallocate a clock allocated with devm_clk_register(). Normally
2667 * this function will not need to be called and the resource management
2668 * code will ensure that the resource is freed.
2669 */
2670void devm_clk_unregister(struct device *dev, struct clk *clk)
2671{
2672 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2673}
2674EXPORT_SYMBOL_GPL(devm_clk_unregister);
2675
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002676/*
2677 * clkdev helpers
2678 */
2679int __clk_get(struct clk *clk)
2680{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002681 struct clk_core *core = !clk ? NULL : clk->core;
2682
2683 if (core) {
2684 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002685 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002686
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002687 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002688 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002689 return 1;
2690}
2691
2692void __clk_put(struct clk *clk)
2693{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002694 struct module *owner;
2695
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002696 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002697 return;
2698
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002699 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002700
Stephen Boyd50595f82015-02-06 11:42:44 -08002701 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002702 if (clk->min_rate > clk->core->req_rate ||
2703 clk->max_rate < clk->core->req_rate)
2704 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2705
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002706 owner = clk->core->owner;
2707 kref_put(&clk->core->ref, __clk_release);
2708
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002709 clk_prepare_unlock();
2710
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002711 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002712
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002713 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002714}
2715
Mike Turquetteb24764902012-03-15 23:11:19 -07002716/*** clk rate change notifiers ***/
2717
2718/**
2719 * clk_notifier_register - add a clk rate change notifier
2720 * @clk: struct clk * to watch
2721 * @nb: struct notifier_block * with callback info
2722 *
2723 * Request notification when clk's rate changes. This uses an SRCU
2724 * notifier because we want it to block and notifier unregistrations are
2725 * uncommon. The callbacks associated with the notifier must not
2726 * re-enter into the clk framework by calling any top-level clk APIs;
2727 * this will cause a nested prepare_lock mutex.
2728 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002729 * In all notification cases cases (pre, post and abort rate change) the
2730 * original clock rate is passed to the callback via struct
2731 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002732 * clk_notifier_data.new_rate.
2733 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002734 * clk_notifier_register() must be called from non-atomic context.
2735 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2736 * allocation failure; otherwise, passes along the return value of
2737 * srcu_notifier_chain_register().
2738 */
2739int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2740{
2741 struct clk_notifier *cn;
2742 int ret = -ENOMEM;
2743
2744 if (!clk || !nb)
2745 return -EINVAL;
2746
Mike Turquetteeab89f62013-03-28 13:59:01 -07002747 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002748
2749 /* search the list of notifiers for this clk */
2750 list_for_each_entry(cn, &clk_notifier_list, node)
2751 if (cn->clk == clk)
2752 break;
2753
2754 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2755 if (cn->clk != clk) {
2756 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2757 if (!cn)
2758 goto out;
2759
2760 cn->clk = clk;
2761 srcu_init_notifier_head(&cn->notifier_head);
2762
2763 list_add(&cn->node, &clk_notifier_list);
2764 }
2765
2766 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2767
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002768 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002769
2770out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002771 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002772
2773 return ret;
2774}
2775EXPORT_SYMBOL_GPL(clk_notifier_register);
2776
2777/**
2778 * clk_notifier_unregister - remove a clk rate change notifier
2779 * @clk: struct clk *
2780 * @nb: struct notifier_block * with callback info
2781 *
2782 * Request no further notification for changes to 'clk' and frees memory
2783 * allocated in clk_notifier_register.
2784 *
2785 * Returns -EINVAL if called with null arguments; otherwise, passes
2786 * along the return value of srcu_notifier_chain_unregister().
2787 */
2788int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2789{
2790 struct clk_notifier *cn = NULL;
2791 int ret = -EINVAL;
2792
2793 if (!clk || !nb)
2794 return -EINVAL;
2795
Mike Turquetteeab89f62013-03-28 13:59:01 -07002796 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002797
2798 list_for_each_entry(cn, &clk_notifier_list, node)
2799 if (cn->clk == clk)
2800 break;
2801
2802 if (cn->clk == clk) {
2803 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2804
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002805 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002806
2807 /* XXX the notifier code should handle this better */
2808 if (!cn->notifier_head.head) {
2809 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002810 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002811 kfree(cn);
2812 }
2813
2814 } else {
2815 ret = -ENOENT;
2816 }
2817
Mike Turquetteeab89f62013-03-28 13:59:01 -07002818 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002819
2820 return ret;
2821}
2822EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002823
2824#ifdef CONFIG_OF
2825/**
2826 * struct of_clk_provider - Clock provider registration structure
2827 * @link: Entry in global list of clock providers
2828 * @node: Pointer to device tree node of clock provider
2829 * @get: Get clock callback. Returns NULL or a struct clk for the
2830 * given clock specifier
2831 * @data: context pointer to be passed into @get callback
2832 */
2833struct of_clk_provider {
2834 struct list_head link;
2835
2836 struct device_node *node;
2837 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2838 void *data;
2839};
2840
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302841static const struct of_device_id __clk_of_table_sentinel
2842 __used __section(__clk_of_table_end);
2843
Grant Likely766e6a42012-04-09 14:50:06 -05002844static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002845static DEFINE_MUTEX(of_clk_mutex);
2846
Grant Likely766e6a42012-04-09 14:50:06 -05002847struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2848 void *data)
2849{
2850 return data;
2851}
2852EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2853
Shawn Guo494bfec2012-08-22 21:36:27 +08002854struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2855{
2856 struct clk_onecell_data *clk_data = data;
2857 unsigned int idx = clkspec->args[0];
2858
2859 if (idx >= clk_data->clk_num) {
2860 pr_err("%s: invalid clock index %d\n", __func__, idx);
2861 return ERR_PTR(-EINVAL);
2862 }
2863
2864 return clk_data->clks[idx];
2865}
2866EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2867
Grant Likely766e6a42012-04-09 14:50:06 -05002868/**
2869 * of_clk_add_provider() - Register a clock provider for a node
2870 * @np: Device node pointer associated with clock provider
2871 * @clk_src_get: callback for decoding clock
2872 * @data: context pointer for @clk_src_get callback.
2873 */
2874int of_clk_add_provider(struct device_node *np,
2875 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2876 void *data),
2877 void *data)
2878{
2879 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002880 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002881
2882 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2883 if (!cp)
2884 return -ENOMEM;
2885
2886 cp->node = of_node_get(np);
2887 cp->data = data;
2888 cp->get = clk_src_get;
2889
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002890 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002891 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002892 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002893 pr_debug("Added clock from %s\n", np->full_name);
2894
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002895 ret = of_clk_set_defaults(np, true);
2896 if (ret < 0)
2897 of_clk_del_provider(np);
2898
2899 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002900}
2901EXPORT_SYMBOL_GPL(of_clk_add_provider);
2902
2903/**
2904 * of_clk_del_provider() - Remove a previously registered clock provider
2905 * @np: Device node pointer associated with clock provider
2906 */
2907void of_clk_del_provider(struct device_node *np)
2908{
2909 struct of_clk_provider *cp;
2910
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002911 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002912 list_for_each_entry(cp, &of_clk_providers, link) {
2913 if (cp->node == np) {
2914 list_del(&cp->link);
2915 of_node_put(cp->node);
2916 kfree(cp);
2917 break;
2918 }
2919 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002920 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002921}
2922EXPORT_SYMBOL_GPL(of_clk_del_provider);
2923
Stephen Boyd73e0e492015-02-06 11:42:43 -08002924struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
2925 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05002926{
2927 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002928 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002929
Stephen Boyd306c3422015-02-05 15:39:11 -08002930 if (!clkspec)
2931 return ERR_PTR(-EINVAL);
2932
Grant Likely766e6a42012-04-09 14:50:06 -05002933 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08002934 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002935 list_for_each_entry(provider, &of_clk_providers, link) {
2936 if (provider->node == clkspec->np)
2937 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002938 if (!IS_ERR(clk)) {
2939 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
2940 con_id);
2941
2942 if (!IS_ERR(clk) && !__clk_get(clk)) {
2943 __clk_free_clk(clk);
2944 clk = ERR_PTR(-ENOENT);
2945 }
2946
Grant Likely766e6a42012-04-09 14:50:06 -05002947 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08002948 }
Grant Likely766e6a42012-04-09 14:50:06 -05002949 }
Stephen Boyd306c3422015-02-05 15:39:11 -08002950 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002951
2952 return clk;
2953}
2954
Stephen Boyd306c3422015-02-05 15:39:11 -08002955/**
2956 * of_clk_get_from_provider() - Lookup a clock from a clock provider
2957 * @clkspec: pointer to a clock specifier data structure
2958 *
2959 * This function looks up a struct clk from the registered list of clock
2960 * providers, an input is a clock specifier data structure as returned
2961 * from the of_parse_phandle_with_args() function call.
2962 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002963struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2964{
Stephen Boyd306c3422015-02-05 15:39:11 -08002965 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05002966}
2967
Mike Turquettef6102742013-10-07 23:12:13 -07002968int of_clk_get_parent_count(struct device_node *np)
2969{
2970 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2971}
2972EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2973
Grant Likely766e6a42012-04-09 14:50:06 -05002974const char *of_clk_get_parent_name(struct device_node *np, int index)
2975{
2976 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002977 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05002978 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002979 const __be32 *vp;
2980 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05002981 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002982 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05002983
2984 if (index < 0)
2985 return NULL;
2986
2987 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2988 &clkspec);
2989 if (rc)
2990 return NULL;
2991
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002992 index = clkspec.args_count ? clkspec.args[0] : 0;
2993 count = 0;
2994
2995 /* if there is an indices property, use it to transfer the index
2996 * specified into an array offset for the clock-output-names property.
2997 */
2998 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
2999 if (index == pv) {
3000 index = count;
3001 break;
3002 }
3003 count++;
3004 }
3005
Grant Likely766e6a42012-04-09 14:50:06 -05003006 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003007 index,
Grant Likely766e6a42012-04-09 14:50:06 -05003008 &clk_name) < 0)
3009 clk_name = clkspec.np->name;
3010
3011 of_node_put(clkspec.np);
3012 return clk_name;
3013}
3014EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3015
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003016struct clock_provider {
3017 of_clk_init_cb_t clk_init_cb;
3018 struct device_node *np;
3019 struct list_head node;
3020};
3021
3022static LIST_HEAD(clk_provider_list);
3023
3024/*
3025 * This function looks for a parent clock. If there is one, then it
3026 * checks that the provider for this parent clock was initialized, in
3027 * this case the parent clock will be ready.
3028 */
3029static int parent_ready(struct device_node *np)
3030{
3031 int i = 0;
3032
3033 while (true) {
3034 struct clk *clk = of_clk_get(np, i);
3035
3036 /* this parent is ready we can check the next one */
3037 if (!IS_ERR(clk)) {
3038 clk_put(clk);
3039 i++;
3040 continue;
3041 }
3042
3043 /* at least one parent is not ready, we exit now */
3044 if (PTR_ERR(clk) == -EPROBE_DEFER)
3045 return 0;
3046
3047 /*
3048 * Here we make assumption that the device tree is
3049 * written correctly. So an error means that there is
3050 * no more parent. As we didn't exit yet, then the
3051 * previous parent are ready. If there is no clock
3052 * parent, no need to wait for them, then we can
3053 * consider their absence as being ready
3054 */
3055 return 1;
3056 }
3057}
3058
Grant Likely766e6a42012-04-09 14:50:06 -05003059/**
3060 * of_clk_init() - Scan and init clock providers from the DT
3061 * @matches: array of compatible values and init functions for providers.
3062 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003063 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003064 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003065 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003066 */
3067void __init of_clk_init(const struct of_device_id *matches)
3068{
Alex Elder7f7ed582013-08-22 11:31:31 -05003069 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003070 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003071 struct clock_provider *clk_provider, *next;
3072 bool is_init_done;
3073 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05003074
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303075 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003076 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303077
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003078 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003079 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003080 struct clock_provider *parent =
3081 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
3082
3083 parent->clk_init_cb = match->data;
3084 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003085 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003086 }
3087
3088 while (!list_empty(&clk_provider_list)) {
3089 is_init_done = false;
3090 list_for_each_entry_safe(clk_provider, next,
3091 &clk_provider_list, node) {
3092 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003093
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003094 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003095 of_clk_set_defaults(clk_provider->np, true);
3096
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003097 list_del(&clk_provider->node);
3098 kfree(clk_provider);
3099 is_init_done = true;
3100 }
3101 }
3102
3103 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003104 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003105 * remaining providers during the last loop, so now we
3106 * initialize all the remaining ones unconditionally
3107 * in case the clock parent was not mandatory
3108 */
3109 if (!is_init_done)
3110 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003111 }
3112}
3113#endif