blob: 43b7e7227056be2d80ea34e8e62d0b99a6e38453 [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
Stephen Boydd6968fc2015-04-30 13:54:13 -0700165static void clk_unprepare_unused_subtree(struct clk_core *core)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100166{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100167 struct clk_core *child;
Ulf Hansson1c155b32013-03-12 20:26:03 +0100168
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100169 lockdep_assert_held(&prepare_lock);
170
Stephen Boydd6968fc2015-04-30 13:54:13 -0700171 hlist_for_each_entry(child, &core->children, child_node)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100172 clk_unprepare_unused_subtree(child);
173
Stephen Boydd6968fc2015-04-30 13:54:13 -0700174 if (core->prepare_count)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100175 return;
176
Stephen Boydd6968fc2015-04-30 13:54:13 -0700177 if (core->flags & CLK_IGNORE_UNUSED)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100178 return;
179
Stephen Boydd6968fc2015-04-30 13:54:13 -0700180 if (clk_core_is_prepared(core)) {
181 trace_clk_unprepare(core);
182 if (core->ops->unprepare_unused)
183 core->ops->unprepare_unused(core->hw);
184 else if (core->ops->unprepare)
185 core->ops->unprepare(core->hw);
186 trace_clk_unprepare_complete(core);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100187 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100188}
189
Stephen Boydd6968fc2015-04-30 13:54:13 -0700190static void clk_disable_unused_subtree(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700191{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100192 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700193 unsigned long flags;
194
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100195 lockdep_assert_held(&prepare_lock);
196
Stephen Boydd6968fc2015-04-30 13:54:13 -0700197 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700198 clk_disable_unused_subtree(child);
199
Mike Turquetteeab89f62013-03-28 13:59:01 -0700200 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700201
Stephen Boydd6968fc2015-04-30 13:54:13 -0700202 if (core->enable_count)
Mike Turquetteb24764902012-03-15 23:11:19 -0700203 goto unlock_out;
204
Stephen Boydd6968fc2015-04-30 13:54:13 -0700205 if (core->flags & CLK_IGNORE_UNUSED)
Mike Turquetteb24764902012-03-15 23:11:19 -0700206 goto unlock_out;
207
Mike Turquette7c045a52012-12-04 11:00:35 -0800208 /*
209 * some gate clocks have special needs during the disable-unused
210 * sequence. call .disable_unused if available, otherwise fall
211 * back to .disable
212 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700213 if (clk_core_is_enabled(core)) {
214 trace_clk_disable(core);
215 if (core->ops->disable_unused)
216 core->ops->disable_unused(core->hw);
217 else if (core->ops->disable)
218 core->ops->disable(core->hw);
219 trace_clk_disable_complete(core);
Mike Turquette7c045a52012-12-04 11:00:35 -0800220 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700221
222unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700223 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700224}
225
Olof Johansson1e435252013-04-27 14:10:18 -0700226static bool clk_ignore_unused;
227static int __init clk_ignore_unused_setup(char *__unused)
228{
229 clk_ignore_unused = true;
230 return 1;
231}
232__setup("clk_ignore_unused", clk_ignore_unused_setup);
233
Mike Turquetteb24764902012-03-15 23:11:19 -0700234static int clk_disable_unused(void)
235{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700236 struct clk_core *core;
Mike Turquetteb24764902012-03-15 23:11:19 -0700237
Olof Johansson1e435252013-04-27 14:10:18 -0700238 if (clk_ignore_unused) {
239 pr_warn("clk: Not disabling unused clocks\n");
240 return 0;
241 }
242
Mike Turquetteeab89f62013-03-28 13:59:01 -0700243 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700244
Stephen Boydd6968fc2015-04-30 13:54:13 -0700245 hlist_for_each_entry(core, &clk_root_list, child_node)
246 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700247
Stephen Boydd6968fc2015-04-30 13:54:13 -0700248 hlist_for_each_entry(core, &clk_orphan_list, child_node)
249 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700250
Stephen Boydd6968fc2015-04-30 13:54:13 -0700251 hlist_for_each_entry(core, &clk_root_list, child_node)
252 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100253
Stephen Boydd6968fc2015-04-30 13:54:13 -0700254 hlist_for_each_entry(core, &clk_orphan_list, child_node)
255 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100256
Mike Turquetteeab89f62013-03-28 13:59:01 -0700257 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700258
259 return 0;
260}
Saravana Kannand41d5802013-05-09 11:35:01 -0700261late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700262
263/*** helper functions ***/
264
Russ Dill65800b22012-11-26 11:20:09 -0800265const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700266{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100267 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700268}
Niels de Vos48950842012-12-13 13:12:25 +0100269EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700270
Russ Dill65800b22012-11-26 11:20:09 -0800271struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700272{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100273 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700274}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800275EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700276
Russ Dill65800b22012-11-26 11:20:09 -0800277u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700278{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100279 return !clk ? 0 : clk->core->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700280}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800281EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700282
Russ Dill65800b22012-11-26 11:20:09 -0800283struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700284{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100285 if (!clk)
286 return NULL;
287
288 /* TODO: Create a per-user clk and change callers to call clk_put */
289 return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700290}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800291EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700292
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700293static struct clk_core *__clk_lookup_subtree(const char *name,
294 struct clk_core *core)
295{
296 struct clk_core *child;
297 struct clk_core *ret;
298
299 if (!strcmp(core->name, name))
300 return core;
301
302 hlist_for_each_entry(child, &core->children, child_node) {
303 ret = __clk_lookup_subtree(name, child);
304 if (ret)
305 return ret;
306 }
307
308 return NULL;
309}
310
311static struct clk_core *clk_core_lookup(const char *name)
312{
313 struct clk_core *root_clk;
314 struct clk_core *ret;
315
316 if (!name)
317 return NULL;
318
319 /* search the 'proper' clk tree first */
320 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
321 ret = __clk_lookup_subtree(name, root_clk);
322 if (ret)
323 return ret;
324 }
325
326 /* if not found, then search the orphan tree */
327 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
328 ret = __clk_lookup_subtree(name, root_clk);
329 if (ret)
330 return ret;
331 }
332
333 return NULL;
334}
335
Stephen Boydd6968fc2015-04-30 13:54:13 -0700336static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100337 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100338{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700339 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100340 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700341 else if (!core->parents)
342 return clk_core_lookup(core->parent_names[index]);
343 else if (!core->parents[index])
344 return core->parents[index] =
345 clk_core_lookup(core->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100346 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700347 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100348}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100349
350struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
351{
352 struct clk_core *parent;
353
354 if (!clk)
355 return NULL;
356
357 parent = clk_core_get_parent_by_index(clk->core, index);
358
359 return !parent ? NULL : parent->hw->clk;
360}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800361EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100362
Russ Dill65800b22012-11-26 11:20:09 -0800363unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700364{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100365 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700366}
367
Stephen Boydd6968fc2015-04-30 13:54:13 -0700368static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700369{
370 unsigned long ret;
371
Stephen Boydd6968fc2015-04-30 13:54:13 -0700372 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530373 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700374 goto out;
375 }
376
Stephen Boydd6968fc2015-04-30 13:54:13 -0700377 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700378
Stephen Boydd6968fc2015-04-30 13:54:13 -0700379 if (core->flags & CLK_IS_ROOT)
Mike Turquetteb24764902012-03-15 23:11:19 -0700380 goto out;
381
Stephen Boydd6968fc2015-04-30 13:54:13 -0700382 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530383 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700384
385out:
386 return ret;
387}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100388
389unsigned long __clk_get_rate(struct clk *clk)
390{
391 if (!clk)
392 return 0;
393
394 return clk_core_get_rate_nolock(clk->core);
395}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800396EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700397
Stephen Boydd6968fc2015-04-30 13:54:13 -0700398static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100399{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700400 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100401 return 0;
402
Stephen Boydd6968fc2015-04-30 13:54:13 -0700403 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100404}
405
Russ Dill65800b22012-11-26 11:20:09 -0800406unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700407{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100408 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700409}
Thierry Redingb05c6832013-09-03 09:43:51 +0200410EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700411
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100412bool __clk_is_prepared(struct clk *clk)
413{
414 if (!clk)
415 return false;
416
417 return clk_core_is_prepared(clk->core);
418}
419
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100420bool __clk_is_enabled(struct clk *clk)
421{
422 if (!clk)
423 return false;
424
425 return clk_core_is_enabled(clk->core);
426}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800427EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700428
Stephen Boyd15a02c12015-01-19 18:05:28 -0800429static bool mux_is_better_rate(unsigned long rate, unsigned long now,
430 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100431{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800432 if (flags & CLK_MUX_ROUND_CLOSEST)
433 return abs(now - rate) < abs(best - rate);
434
435 return now <= rate && now > best;
436}
437
438static long
439clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100440 unsigned long min_rate,
441 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800442 unsigned long *best_parent_rate,
443 struct clk_hw **best_parent_p,
444 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100445{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100446 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
James Hogane366fdd2013-07-29 12:25:02 +0100447 int i, num_parents;
448 unsigned long parent_rate, best = 0;
449
450 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100451 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
452 parent = core->parent;
453 if (core->flags & CLK_SET_RATE_PARENT)
Javier Martinez Canillas9e0ad7d2015-02-12 14:58:28 +0100454 best = __clk_determine_rate(parent ? parent->hw : NULL,
455 rate, min_rate, max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100456 else if (parent)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100457 best = clk_core_get_rate_nolock(parent);
James Hogane366fdd2013-07-29 12:25:02 +0100458 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100459 best = clk_core_get_rate_nolock(core);
James Hogane366fdd2013-07-29 12:25:02 +0100460 goto out;
461 }
462
463 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100464 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100465 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100466 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100467 if (!parent)
468 continue;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100469 if (core->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100470 parent_rate = __clk_determine_rate(parent->hw, rate,
471 min_rate,
472 max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100473 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100474 parent_rate = clk_core_get_rate_nolock(parent);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800475 if (mux_is_better_rate(rate, parent_rate, best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100476 best_parent = parent;
477 best = parent_rate;
478 }
479 }
480
481out:
482 if (best_parent)
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100483 *best_parent_p = best_parent->hw;
James Hogane366fdd2013-07-29 12:25:02 +0100484 *best_parent_rate = best;
485
486 return best;
487}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800488
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100489struct clk *__clk_lookup(const char *name)
490{
491 struct clk_core *core = clk_core_lookup(name);
492
493 return !core ? NULL : core->hw->clk;
494}
495
Stephen Boydd6968fc2015-04-30 13:54:13 -0700496static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100497 unsigned long *min_rate,
498 unsigned long *max_rate)
499{
500 struct clk *clk_user;
501
502 *min_rate = 0;
503 *max_rate = ULONG_MAX;
504
Stephen Boydd6968fc2015-04-30 13:54:13 -0700505 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100506 *min_rate = max(*min_rate, clk_user->min_rate);
507
Stephen Boydd6968fc2015-04-30 13:54:13 -0700508 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100509 *max_rate = min(*max_rate, clk_user->max_rate);
510}
511
Stephen Boyd15a02c12015-01-19 18:05:28 -0800512/*
513 * Helper for finding best parent to provide a given frequency. This can be used
514 * directly as a determine_rate callback (e.g. for a mux), or from a more
515 * complex clock that may combine a mux with other operations.
516 */
517long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100518 unsigned long min_rate,
519 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800520 unsigned long *best_parent_rate,
521 struct clk_hw **best_parent_p)
522{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100523 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
524 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800525 best_parent_p, 0);
526}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800527EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100528
Stephen Boyd15a02c12015-01-19 18:05:28 -0800529long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100530 unsigned long min_rate,
531 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800532 unsigned long *best_parent_rate,
533 struct clk_hw **best_parent_p)
534{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100535 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
536 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800537 best_parent_p,
538 CLK_MUX_ROUND_CLOSEST);
539}
540EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
541
Mike Turquetteb24764902012-03-15 23:11:19 -0700542/*** clk api ***/
543
Stephen Boydd6968fc2015-04-30 13:54:13 -0700544static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700545{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700546 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700547 return;
548
Stephen Boydd6968fc2015-04-30 13:54:13 -0700549 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700550 return;
551
Stephen Boydd6968fc2015-04-30 13:54:13 -0700552 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700553 return;
554
Stephen Boydd6968fc2015-04-30 13:54:13 -0700555 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700556
Stephen Boydd6968fc2015-04-30 13:54:13 -0700557 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800558
Stephen Boydd6968fc2015-04-30 13:54:13 -0700559 if (core->ops->unprepare)
560 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700561
Stephen Boydd6968fc2015-04-30 13:54:13 -0700562 trace_clk_unprepare_complete(core);
563 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700564}
565
566/**
567 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200568 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700569 *
570 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
571 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
572 * if the operation may sleep. One example is a clk which is accessed over
573 * I2c. In the complex case a clk gate operation may require a fast and a slow
574 * part. It is this reason that clk_unprepare and clk_disable are not mutually
575 * exclusive. In fact clk_disable must be called before clk_unprepare.
576 */
577void clk_unprepare(struct clk *clk)
578{
Stephen Boyd63589e92014-03-26 16:06:37 -0700579 if (IS_ERR_OR_NULL(clk))
580 return;
581
Mike Turquetteeab89f62013-03-28 13:59:01 -0700582 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100583 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700584 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700585}
586EXPORT_SYMBOL_GPL(clk_unprepare);
587
Stephen Boydd6968fc2015-04-30 13:54:13 -0700588static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700589{
590 int ret = 0;
591
Stephen Boydd6968fc2015-04-30 13:54:13 -0700592 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700593 return 0;
594
Stephen Boydd6968fc2015-04-30 13:54:13 -0700595 if (core->prepare_count == 0) {
596 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700597 if (ret)
598 return ret;
599
Stephen Boydd6968fc2015-04-30 13:54:13 -0700600 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800601
Stephen Boydd6968fc2015-04-30 13:54:13 -0700602 if (core->ops->prepare)
603 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800604
Stephen Boydd6968fc2015-04-30 13:54:13 -0700605 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800606
607 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700608 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800609 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700610 }
611 }
612
Stephen Boydd6968fc2015-04-30 13:54:13 -0700613 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700614
615 return 0;
616}
617
618/**
619 * clk_prepare - prepare a clock source
620 * @clk: the clk being prepared
621 *
622 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
623 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
624 * operation may sleep. One example is a clk which is accessed over I2c. In
625 * the complex case a clk ungate operation may require a fast and a slow part.
626 * It is this reason that clk_prepare and clk_enable are not mutually
627 * exclusive. In fact clk_prepare must be called before clk_enable.
628 * Returns 0 on success, -EERROR otherwise.
629 */
630int clk_prepare(struct clk *clk)
631{
632 int ret;
633
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100634 if (!clk)
635 return 0;
636
Mike Turquetteeab89f62013-03-28 13:59:01 -0700637 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100638 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700639 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700640
641 return ret;
642}
643EXPORT_SYMBOL_GPL(clk_prepare);
644
Stephen Boydd6968fc2015-04-30 13:54:13 -0700645static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700646{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700647 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700648 return;
649
Stephen Boydd6968fc2015-04-30 13:54:13 -0700650 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700651 return;
652
Stephen Boydd6968fc2015-04-30 13:54:13 -0700653 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700654 return;
655
Stephen Boydd6968fc2015-04-30 13:54:13 -0700656 trace_clk_disable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800657
Stephen Boydd6968fc2015-04-30 13:54:13 -0700658 if (core->ops->disable)
659 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700660
Stephen Boydd6968fc2015-04-30 13:54:13 -0700661 trace_clk_disable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800662
Stephen Boydd6968fc2015-04-30 13:54:13 -0700663 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100664}
665
Mike Turquetteb24764902012-03-15 23:11:19 -0700666/**
667 * clk_disable - gate a clock
668 * @clk: the clk being gated
669 *
670 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
671 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
672 * clk if the operation is fast and will never sleep. One example is a
673 * SoC-internal clk which is controlled via simple register writes. In the
674 * complex case a clk gate operation may require a fast and a slow part. It is
675 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
676 * In fact clk_disable must be called before clk_unprepare.
677 */
678void clk_disable(struct clk *clk)
679{
680 unsigned long flags;
681
Stephen Boyd63589e92014-03-26 16:06:37 -0700682 if (IS_ERR_OR_NULL(clk))
683 return;
684
Mike Turquetteeab89f62013-03-28 13:59:01 -0700685 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700686 clk_core_disable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700687 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700688}
689EXPORT_SYMBOL_GPL(clk_disable);
690
Stephen Boydd6968fc2015-04-30 13:54:13 -0700691static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700692{
693 int ret = 0;
694
Stephen Boydd6968fc2015-04-30 13:54:13 -0700695 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700696 return 0;
697
Stephen Boydd6968fc2015-04-30 13:54:13 -0700698 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700699 return -ESHUTDOWN;
700
Stephen Boydd6968fc2015-04-30 13:54:13 -0700701 if (core->enable_count == 0) {
702 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700703
704 if (ret)
705 return ret;
706
Stephen Boydd6968fc2015-04-30 13:54:13 -0700707 trace_clk_enable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800708
Stephen Boydd6968fc2015-04-30 13:54:13 -0700709 if (core->ops->enable)
710 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800711
Stephen Boydd6968fc2015-04-30 13:54:13 -0700712 trace_clk_enable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800713
714 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700715 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800716 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700717 }
718 }
719
Stephen Boydd6968fc2015-04-30 13:54:13 -0700720 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700721 return 0;
722}
723
724/**
725 * clk_enable - ungate a clock
726 * @clk: the clk being ungated
727 *
728 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
729 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
730 * if the operation will never sleep. One example is a SoC-internal clk which
731 * is controlled via simple register writes. In the complex case a clk ungate
732 * operation may require a fast and a slow part. It is this reason that
733 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
734 * must be called before clk_enable. Returns 0 on success, -EERROR
735 * otherwise.
736 */
737int clk_enable(struct clk *clk)
738{
739 unsigned long flags;
740 int ret;
741
Dong Aisheng864e1602015-04-30 14:02:19 -0700742 if (!clk)
743 return 0;
744
Mike Turquetteeab89f62013-03-28 13:59:01 -0700745 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700746 ret = clk_core_enable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700747 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700748
749 return ret;
750}
751EXPORT_SYMBOL_GPL(clk_enable);
752
Stephen Boydd6968fc2015-04-30 13:54:13 -0700753static unsigned long clk_core_round_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100754 unsigned long rate,
755 unsigned long min_rate,
756 unsigned long max_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -0700757{
Shawn Guo81536e02012-04-12 20:50:17 +0800758 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100759 struct clk_core *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100760 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700761
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100762 lockdep_assert_held(&prepare_lock);
763
Stephen Boydd6968fc2015-04-30 13:54:13 -0700764 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700765 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700766
Stephen Boydd6968fc2015-04-30 13:54:13 -0700767 parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +0100768 if (parent)
769 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700770
Stephen Boydd6968fc2015-04-30 13:54:13 -0700771 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100772 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700773 return core->ops->determine_rate(core->hw, rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100774 min_rate, max_rate,
775 &parent_rate, &parent_hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700776 } else if (core->ops->round_rate)
777 return core->ops->round_rate(core->hw, rate, &parent_rate);
778 else if (core->flags & CLK_SET_RATE_PARENT)
779 return clk_core_round_rate_nolock(core->parent, rate, min_rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100780 max_rate);
James Hogan71472c02013-07-29 12:25:00 +0100781 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700782 return core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700783}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100784
785/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100786 * __clk_determine_rate - get the closest rate actually supported by a clock
787 * @hw: determine the rate of this clock
788 * @rate: target rate
789 * @min_rate: returned rate must be greater than this rate
790 * @max_rate: returned rate must be less than this rate
791 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700792 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100793 */
794unsigned long __clk_determine_rate(struct clk_hw *hw,
795 unsigned long rate,
796 unsigned long min_rate,
797 unsigned long max_rate)
798{
799 if (!hw)
800 return 0;
801
802 return clk_core_round_rate_nolock(hw->core, rate, min_rate, max_rate);
803}
804EXPORT_SYMBOL_GPL(__clk_determine_rate);
805
806/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100807 * __clk_round_rate - round the given rate for a clk
808 * @clk: round the rate of this clock
809 * @rate: the rate which is to be rounded
810 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700811 * Useful for clk_ops such as .set_rate
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100812 */
813unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
814{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100815 unsigned long min_rate;
816 unsigned long max_rate;
817
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100818 if (!clk)
819 return 0;
820
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100821 clk_core_get_boundaries(clk->core, &min_rate, &max_rate);
822
823 return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100824}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +0200825EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700826
827/**
828 * clk_round_rate - round the given rate for a clk
829 * @clk: the clk for which we are rounding a rate
830 * @rate: the rate which is to be rounded
831 *
832 * Takes in a rate as input and rounds it to a rate that the clk can actually
833 * use which is then returned. If clk doesn't support round_rate operation
834 * then the parent rate is returned.
835 */
836long clk_round_rate(struct clk *clk, unsigned long rate)
837{
838 unsigned long ret;
839
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100840 if (!clk)
841 return 0;
842
Mike Turquetteeab89f62013-03-28 13:59:01 -0700843 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700844 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700845 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700846
847 return ret;
848}
849EXPORT_SYMBOL_GPL(clk_round_rate);
850
851/**
852 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -0700853 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -0700854 * @msg: clk notifier type (see include/linux/clk.h)
855 * @old_rate: old clk rate
856 * @new_rate: new clk rate
857 *
858 * Triggers a notifier call chain on the clk rate-change notification
859 * for 'clk'. Passes a pointer to the struct clk and the previous
860 * and current rates to the notifier callback. Intended to be called by
861 * internal clock code only. Returns NOTIFY_DONE from the last driver
862 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
863 * a driver returns that.
864 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700865static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -0700866 unsigned long old_rate, unsigned long new_rate)
867{
868 struct clk_notifier *cn;
869 struct clk_notifier_data cnd;
870 int ret = NOTIFY_DONE;
871
Mike Turquetteb24764902012-03-15 23:11:19 -0700872 cnd.old_rate = old_rate;
873 cnd.new_rate = new_rate;
874
875 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700876 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100877 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700878 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
879 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -0700880 }
881 }
882
883 return ret;
884}
885
886/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100887 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -0700888 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100889 *
890 * Walks the subtree of clks starting with clk and recalculates accuracies as
891 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700892 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100893 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100894 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700895static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100896{
897 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100898 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100899
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100900 lockdep_assert_held(&prepare_lock);
901
Stephen Boydd6968fc2015-04-30 13:54:13 -0700902 if (core->parent)
903 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100904
Stephen Boydd6968fc2015-04-30 13:54:13 -0700905 if (core->ops->recalc_accuracy)
906 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100907 parent_accuracy);
908 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700909 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100910
Stephen Boydd6968fc2015-04-30 13:54:13 -0700911 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100912 __clk_recalc_accuracies(child);
913}
914
Stephen Boydd6968fc2015-04-30 13:54:13 -0700915static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100916{
917 unsigned long accuracy;
918
919 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -0700920 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
921 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100922
Stephen Boydd6968fc2015-04-30 13:54:13 -0700923 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100924 clk_prepare_unlock();
925
926 return accuracy;
927}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100928
929/**
930 * clk_get_accuracy - return the accuracy of clk
931 * @clk: the clk whose accuracy is being returned
932 *
933 * Simply returns the cached accuracy of the clk, unless
934 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
935 * issued.
936 * If clk is NULL then returns 0.
937 */
938long clk_get_accuracy(struct clk *clk)
939{
940 if (!clk)
941 return 0;
942
943 return clk_core_get_accuracy(clk->core);
944}
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100945EXPORT_SYMBOL_GPL(clk_get_accuracy);
946
Stephen Boydd6968fc2015-04-30 13:54:13 -0700947static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100948 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700949{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700950 if (core->ops->recalc_rate)
951 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700952 return parent_rate;
953}
954
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100955/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700956 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -0700957 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -0700958 * @msg: notification type (see include/linux/clk.h)
959 *
960 * Walks the subtree of clks starting with clk and recalculates rates as it
961 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200962 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -0700963 *
964 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
965 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -0700966 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700967static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -0700968{
969 unsigned long old_rate;
970 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100971 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700972
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100973 lockdep_assert_held(&prepare_lock);
974
Stephen Boydd6968fc2015-04-30 13:54:13 -0700975 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700976
Stephen Boydd6968fc2015-04-30 13:54:13 -0700977 if (core->parent)
978 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700979
Stephen Boydd6968fc2015-04-30 13:54:13 -0700980 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700981
982 /*
983 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
984 * & ABORT_RATE_CHANGE notifiers
985 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700986 if (core->notifier_count && msg)
987 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700988
Stephen Boydd6968fc2015-04-30 13:54:13 -0700989 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700990 __clk_recalc_rates(child, msg);
991}
992
Stephen Boydd6968fc2015-04-30 13:54:13 -0700993static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100994{
995 unsigned long rate;
996
997 clk_prepare_lock();
998
Stephen Boydd6968fc2015-04-30 13:54:13 -0700999 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1000 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001001
Stephen Boydd6968fc2015-04-30 13:54:13 -07001002 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001003 clk_prepare_unlock();
1004
1005 return rate;
1006}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001007
Mike Turquetteb24764902012-03-15 23:11:19 -07001008/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001009 * clk_get_rate - return the rate of clk
1010 * @clk: the clk whose rate is being returned
1011 *
1012 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1013 * is set, which means a recalc_rate will be issued.
1014 * If clk is NULL then returns 0.
1015 */
1016unsigned long clk_get_rate(struct clk *clk)
1017{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001018 if (!clk)
1019 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001020
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001021 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001022}
1023EXPORT_SYMBOL_GPL(clk_get_rate);
1024
Stephen Boydd6968fc2015-04-30 13:54:13 -07001025static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001026 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001027{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001028 int i;
James Hogan4935b222013-07-29 12:24:59 +01001029
Stephen Boydd6968fc2015-04-30 13:54:13 -07001030 if (!core->parents) {
1031 core->parents = kcalloc(core->num_parents,
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001032 sizeof(struct clk *), GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001033 if (!core->parents)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001034 return -ENOMEM;
1035 }
James Hogan4935b222013-07-29 12:24:59 +01001036
1037 /*
1038 * find index of new parent clock using cached parent ptrs,
1039 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001040 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001041 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001042 for (i = 0; i < core->num_parents; i++) {
1043 if (core->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001044 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001045
Stephen Boydd6968fc2015-04-30 13:54:13 -07001046 if (core->parents[i])
Tomasz Figada0f0b22013-09-29 02:37:16 +02001047 continue;
1048
Stephen Boydd6968fc2015-04-30 13:54:13 -07001049 if (!strcmp(core->parent_names[i], parent->name)) {
1050 core->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001051 return i;
James Hogan4935b222013-07-29 12:24:59 +01001052 }
1053 }
1054
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001055 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001056}
1057
Stephen Boydd6968fc2015-04-30 13:54:13 -07001058static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001059{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001060 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001061
James Hogan903efc52013-08-29 12:10:51 +01001062 if (new_parent) {
1063 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001064 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001065 new_parent->new_child = NULL;
1066
Stephen Boydd6968fc2015-04-30 13:54:13 -07001067 hlist_add_head(&core->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001068 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001069 hlist_add_head(&core->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001070 }
James Hogan4935b222013-07-29 12:24:59 +01001071
Stephen Boydd6968fc2015-04-30 13:54:13 -07001072 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001073}
1074
Stephen Boydd6968fc2015-04-30 13:54:13 -07001075static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001076 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001077{
1078 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001079 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001080
1081 /*
1082 * Migrate prepare state between parents and prevent race with
1083 * clk_enable().
1084 *
1085 * If the clock is not prepared, then a race with
1086 * clk_enable/disable() is impossible since we already have the
1087 * prepare lock (future calls to clk_enable() need to be preceded by
1088 * a clk_prepare()).
1089 *
1090 * If the clock is prepared, migrate the prepared state to the new
1091 * parent and also protect against a race with clk_enable() by
1092 * forcing the clock and the new parent on. This ensures that all
1093 * future calls to clk_enable() are practically NOPs with respect to
1094 * hardware and software states.
1095 *
1096 * See also: Comment for clk_set_parent() below.
1097 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001098 if (core->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001099 clk_core_prepare(parent);
1100 clk_core_enable(parent);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001101 clk_core_enable(core);
James Hogan4935b222013-07-29 12:24:59 +01001102 }
1103
1104 /* update the clk tree topology */
1105 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001106 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001107 clk_enable_unlock(flags);
1108
Stephen Boyd3fa22522014-01-15 10:47:22 -08001109 return old_parent;
1110}
1111
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001112static void __clk_set_parent_after(struct clk_core *core,
1113 struct clk_core *parent,
1114 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001115{
1116 /*
1117 * Finish the migration of prepare state and undo the changes done
1118 * for preventing a race with clk_enable().
1119 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001120 if (core->prepare_count) {
1121 clk_core_disable(core);
1122 clk_core_disable(old_parent);
1123 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001124 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001125}
1126
Stephen Boydd6968fc2015-04-30 13:54:13 -07001127static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001128 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001129{
1130 unsigned long flags;
1131 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001132 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001133
Stephen Boydd6968fc2015-04-30 13:54:13 -07001134 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001135
Stephen Boydd6968fc2015-04-30 13:54:13 -07001136 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001137
James Hogan4935b222013-07-29 12:24:59 +01001138 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001139 if (parent && core->ops->set_parent)
1140 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001141
Stephen Boydd6968fc2015-04-30 13:54:13 -07001142 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001143
James Hogan4935b222013-07-29 12:24:59 +01001144 if (ret) {
1145 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001146 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001147 clk_enable_unlock(flags);
1148
Stephen Boydd6968fc2015-04-30 13:54:13 -07001149 if (core->prepare_count) {
1150 clk_core_disable(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001151 clk_core_disable(parent);
1152 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001153 }
1154 return ret;
1155 }
1156
Stephen Boydd6968fc2015-04-30 13:54:13 -07001157 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001158
James Hogan4935b222013-07-29 12:24:59 +01001159 return 0;
1160}
1161
Ulf Hanssona093bde2012-08-31 14:21:28 +02001162/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001163 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001164 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001165 * @parent_rate: the "future" rate of clk's parent
1166 *
1167 * Walks the subtree of clks starting with clk, speculating rates as it
1168 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1169 *
1170 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1171 * pre-rate change notifications and returns early if no clks in the
1172 * subtree have subscribed to the notifications. Note that if a clk does not
1173 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001174 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001175 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001176static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001177 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001178{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001179 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001180 unsigned long new_rate;
1181 int ret = NOTIFY_DONE;
1182
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001183 lockdep_assert_held(&prepare_lock);
1184
Stephen Boydd6968fc2015-04-30 13:54:13 -07001185 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001186
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001187 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001188 if (core->notifier_count)
1189 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001190
Mike Turquette86bcfa22014-02-24 16:08:41 -08001191 if (ret & NOTIFY_STOP_MASK) {
1192 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001193 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001194 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001195 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001196
Stephen Boydd6968fc2015-04-30 13:54:13 -07001197 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001198 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001199 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001200 break;
1201 }
1202
1203out:
1204 return ret;
1205}
1206
Stephen Boydd6968fc2015-04-30 13:54:13 -07001207static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001208 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001209{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001210 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001211
Stephen Boydd6968fc2015-04-30 13:54:13 -07001212 core->new_rate = new_rate;
1213 core->new_parent = new_parent;
1214 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001215 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001216 core->new_child = NULL;
1217 if (new_parent && new_parent != core->parent)
1218 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001219
Stephen Boydd6968fc2015-04-30 13:54:13 -07001220 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001221 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001222 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001223 }
1224}
1225
1226/*
1227 * calculate the new rates returning the topmost clock that has to be
1228 * changed.
1229 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001230static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001231 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001232{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001233 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001234 struct clk_core *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001235 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001236 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001237 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001238 unsigned long min_rate;
1239 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001240 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001241 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001242
Mike Turquette7452b212012-03-26 14:45:36 -07001243 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001244 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001245 return NULL;
1246
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001247 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001248 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001249 if (parent)
1250 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001251
Stephen Boydd6968fc2015-04-30 13:54:13 -07001252 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001253
James Hogan71472c02013-07-29 12:25:00 +01001254 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001255 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001256 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001257 ret = core->ops->determine_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001258 min_rate,
1259 max_rate,
1260 &best_parent_rate,
1261 &parent_hw);
1262 if (ret < 0)
1263 return NULL;
1264
1265 new_rate = ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001266 parent = parent_hw ? parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001267 } else if (core->ops->round_rate) {
1268 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001269 &best_parent_rate);
1270 if (ret < 0)
1271 return NULL;
1272
1273 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001274 if (new_rate < min_rate || new_rate > max_rate)
1275 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001276 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001277 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001278 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001279 return NULL;
1280 } else {
1281 /* pass-through clock with adjustable parent */
1282 top = clk_calc_new_rates(parent, rate);
1283 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001284 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001285 }
1286
James Hogan71472c02013-07-29 12:25:00 +01001287 /* some clocks must be gated to change parent */
1288 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001289 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001290 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001291 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001292 return NULL;
1293 }
1294
James Hogan71472c02013-07-29 12:25:00 +01001295 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001296 if (parent && core->num_parents > 1) {
1297 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001298 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001299 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001300 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001301 return NULL;
1302 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001303 }
1304
Stephen Boydd6968fc2015-04-30 13:54:13 -07001305 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001306 best_parent_rate != parent->rate)
1307 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001308
1309out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001310 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001311
1312 return top;
1313}
1314
1315/*
1316 * Notify about rate changes in a subtree. Always walk down the whole tree
1317 * so that in case of an error we can walk down the whole tree again and
1318 * abort the change.
1319 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001320static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001321 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001322{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001323 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001324 int ret = NOTIFY_DONE;
1325
Stephen Boydd6968fc2015-04-30 13:54:13 -07001326 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301327 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001328
Stephen Boydd6968fc2015-04-30 13:54:13 -07001329 if (core->notifier_count) {
1330 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001331 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001332 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001333 }
1334
Stephen Boydd6968fc2015-04-30 13:54:13 -07001335 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001336 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001337 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001338 continue;
1339 tmp_clk = clk_propagate_rate_change(child, event);
1340 if (tmp_clk)
1341 fail_clk = tmp_clk;
1342 }
1343
Stephen Boydd6968fc2015-04-30 13:54:13 -07001344 /* handle the new child who might not be in core->children yet */
1345 if (core->new_child) {
1346 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001347 if (tmp_clk)
1348 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001349 }
1350
1351 return fail_clk;
1352}
1353
1354/*
1355 * walk down a subtree and set the new rates notifying the rate
1356 * change on the way
1357 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001358static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001359{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001360 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001361 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001362 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001363 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001364 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001365 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001366
Stephen Boydd6968fc2015-04-30 13:54:13 -07001367 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001368
Stephen Boydd6968fc2015-04-30 13:54:13 -07001369 if (core->new_parent)
1370 best_parent_rate = core->new_parent->rate;
1371 else if (core->parent)
1372 best_parent_rate = core->parent->rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001373
Stephen Boydd6968fc2015-04-30 13:54:13 -07001374 if (core->new_parent && core->new_parent != core->parent) {
1375 old_parent = __clk_set_parent_before(core, core->new_parent);
1376 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001377
Stephen Boydd6968fc2015-04-30 13:54:13 -07001378 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001379 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001380 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001381 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001382 core->new_parent_index);
1383 } else if (core->ops->set_parent) {
1384 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001385 }
1386
Stephen Boydd6968fc2015-04-30 13:54:13 -07001387 trace_clk_set_parent_complete(core, core->new_parent);
1388 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001389 }
1390
Stephen Boydd6968fc2015-04-30 13:54:13 -07001391 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001392
Stephen Boydd6968fc2015-04-30 13:54:13 -07001393 if (!skip_set_rate && core->ops->set_rate)
1394 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001395
Stephen Boydd6968fc2015-04-30 13:54:13 -07001396 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001397
Stephen Boydd6968fc2015-04-30 13:54:13 -07001398 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001399
Stephen Boydd6968fc2015-04-30 13:54:13 -07001400 if (core->notifier_count && old_rate != core->rate)
1401 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001402
Tero Kristo067bb172014-08-21 16:47:45 +03001403 /*
1404 * Use safe iteration, as change_rate can actually swap parents
1405 * for certain clock types.
1406 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001407 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001408 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001409 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001410 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001411 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001412 }
1413
Stephen Boydd6968fc2015-04-30 13:54:13 -07001414 /* handle the new child who might not be in core->children yet */
1415 if (core->new_child)
1416 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001417}
1418
Stephen Boydd6968fc2015-04-30 13:54:13 -07001419static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001420 unsigned long req_rate)
1421{
1422 struct clk_core *top, *fail_clk;
1423 unsigned long rate = req_rate;
1424 int ret = 0;
1425
Stephen Boydd6968fc2015-04-30 13:54:13 -07001426 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001427 return 0;
1428
1429 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001430 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001431 return 0;
1432
Stephen Boydd6968fc2015-04-30 13:54:13 -07001433 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001434 return -EBUSY;
1435
1436 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001437 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001438 if (!top)
1439 return -EINVAL;
1440
1441 /* notify that we are about to change rates */
1442 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1443 if (fail_clk) {
1444 pr_debug("%s: failed to set %s rate\n", __func__,
1445 fail_clk->name);
1446 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1447 return -EBUSY;
1448 }
1449
1450 /* change the rates */
1451 clk_change_rate(top);
1452
Stephen Boydd6968fc2015-04-30 13:54:13 -07001453 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001454
1455 return ret;
1456}
1457
Mike Turquetteb24764902012-03-15 23:11:19 -07001458/**
1459 * clk_set_rate - specify a new rate for clk
1460 * @clk: the clk whose rate is being changed
1461 * @rate: the new rate for clk
1462 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001463 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001464 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001465 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1466 * propagate up to clk's parent; whether or not this happens depends on the
1467 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1468 * after calling .round_rate then upstream parent propagation is ignored. If
1469 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001470 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001471 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1472 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001473 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001474 * Rate changes are accomplished via tree traversal that also recalculates the
1475 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001476 *
1477 * Returns 0 on success, -EERROR otherwise.
1478 */
1479int clk_set_rate(struct clk *clk, unsigned long rate)
1480{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001481 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001482
Mike Turquette89ac8d72013-08-21 23:58:09 -07001483 if (!clk)
1484 return 0;
1485
Mike Turquetteb24764902012-03-15 23:11:19 -07001486 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001487 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001488
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001489 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001490
Mike Turquetteeab89f62013-03-28 13:59:01 -07001491 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001492
1493 return ret;
1494}
1495EXPORT_SYMBOL_GPL(clk_set_rate);
1496
1497/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001498 * clk_set_rate_range - set a rate range for a clock source
1499 * @clk: clock source
1500 * @min: desired minimum clock rate in Hz, inclusive
1501 * @max: desired maximum clock rate in Hz, inclusive
1502 *
1503 * Returns success (0) or negative errno.
1504 */
1505int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1506{
1507 int ret = 0;
1508
1509 if (!clk)
1510 return 0;
1511
1512 if (min > max) {
1513 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1514 __func__, clk->core->name, clk->dev_id, clk->con_id,
1515 min, max);
1516 return -EINVAL;
1517 }
1518
1519 clk_prepare_lock();
1520
1521 if (min != clk->min_rate || max != clk->max_rate) {
1522 clk->min_rate = min;
1523 clk->max_rate = max;
1524 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1525 }
1526
1527 clk_prepare_unlock();
1528
1529 return ret;
1530}
1531EXPORT_SYMBOL_GPL(clk_set_rate_range);
1532
1533/**
1534 * clk_set_min_rate - set a minimum clock rate for a clock source
1535 * @clk: clock source
1536 * @rate: desired minimum clock rate in Hz, inclusive
1537 *
1538 * Returns success (0) or negative errno.
1539 */
1540int clk_set_min_rate(struct clk *clk, unsigned long rate)
1541{
1542 if (!clk)
1543 return 0;
1544
1545 return clk_set_rate_range(clk, rate, clk->max_rate);
1546}
1547EXPORT_SYMBOL_GPL(clk_set_min_rate);
1548
1549/**
1550 * clk_set_max_rate - set a maximum clock rate for a clock source
1551 * @clk: clock source
1552 * @rate: desired maximum clock rate in Hz, inclusive
1553 *
1554 * Returns success (0) or negative errno.
1555 */
1556int clk_set_max_rate(struct clk *clk, unsigned long rate)
1557{
1558 if (!clk)
1559 return 0;
1560
1561 return clk_set_rate_range(clk, clk->min_rate, rate);
1562}
1563EXPORT_SYMBOL_GPL(clk_set_max_rate);
1564
1565/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001566 * clk_get_parent - return the parent of a clk
1567 * @clk: the clk whose parent gets returned
1568 *
1569 * Simply returns clk->parent. Returns NULL if clk is NULL.
1570 */
1571struct clk *clk_get_parent(struct clk *clk)
1572{
1573 struct clk *parent;
1574
Mike Turquetteeab89f62013-03-28 13:59:01 -07001575 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001576 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001577 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001578
1579 return parent;
1580}
1581EXPORT_SYMBOL_GPL(clk_get_parent);
1582
1583/*
1584 * .get_parent is mandatory for clocks with multiple possible parents. It is
1585 * optional for single-parent clocks. Always call .get_parent if it is
1586 * available and WARN if it is missing for multi-parent clocks.
1587 *
1588 * For single-parent clocks without .get_parent, first check to see if the
1589 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001590 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001591 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001592static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001593{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001594 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001595 u8 index;
1596
1597 /* handle the trivial cases */
1598
Stephen Boydd6968fc2015-04-30 13:54:13 -07001599 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -07001600 goto out;
1601
Stephen Boydd6968fc2015-04-30 13:54:13 -07001602 if (core->num_parents == 1) {
1603 if (IS_ERR_OR_NULL(core->parent))
1604 core->parent = clk_core_lookup(core->parent_names[0]);
1605 ret = core->parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001606 goto out;
1607 }
1608
Stephen Boydd6968fc2015-04-30 13:54:13 -07001609 if (!core->ops->get_parent) {
1610 WARN(!core->ops->get_parent,
Mike Turquetteb24764902012-03-15 23:11:19 -07001611 "%s: multi-parent clocks must implement .get_parent\n",
1612 __func__);
1613 goto out;
1614 };
1615
1616 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07001617 * Do our best to cache parent clocks in core->parents. This prevents
1618 * unnecessary and expensive lookups. We don't set core->parent here;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001619 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001620 */
1621
Stephen Boydd6968fc2015-04-30 13:54:13 -07001622 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001623
Stephen Boydd6968fc2015-04-30 13:54:13 -07001624 if (!core->parents)
1625 core->parents =
1626 kcalloc(core->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001627 GFP_KERNEL);
1628
Stephen Boydd6968fc2015-04-30 13:54:13 -07001629 ret = clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001630
1631out:
1632 return ret;
1633}
1634
Stephen Boydd6968fc2015-04-30 13:54:13 -07001635static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001636 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001637{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001638 clk_reparent(core, new_parent);
1639 __clk_recalc_accuracies(core);
1640 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07001641}
1642
Mike Turquetteb24764902012-03-15 23:11:19 -07001643/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001644 * clk_has_parent - check if a clock is a possible parent for another
1645 * @clk: clock source
1646 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07001647 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001648 * This function can be used in drivers that need to check that a clock can be
1649 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001650 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001651 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001652 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001653bool clk_has_parent(struct clk *clk, struct clk *parent)
1654{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001655 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001656 unsigned int i;
1657
1658 /* NULL clocks should be nops, so return success if either is NULL. */
1659 if (!clk || !parent)
1660 return true;
1661
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001662 core = clk->core;
1663 parent_core = parent->core;
1664
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001665 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001666 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001667 return true;
1668
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001669 for (i = 0; i < core->num_parents; i++)
1670 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001671 return true;
1672
1673 return false;
1674}
1675EXPORT_SYMBOL_GPL(clk_has_parent);
1676
Stephen Boydd6968fc2015-04-30 13:54:13 -07001677static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001678{
1679 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001680 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001681 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001682
Stephen Boydd6968fc2015-04-30 13:54:13 -07001683 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07001684 return 0;
1685
Mike Turquetteb24764902012-03-15 23:11:19 -07001686 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001687 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001688
Stephen Boydd6968fc2015-04-30 13:54:13 -07001689 if (core->parent == parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001690 goto out;
1691
Stephen Boydb61c43c2015-02-02 14:11:25 -08001692 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001693 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08001694 ret = -ENOSYS;
1695 goto out;
1696 }
1697
Ulf Hansson031dcc92013-04-02 23:09:38 +02001698 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001699 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001700 ret = -EBUSY;
1701 goto out;
1702 }
1703
1704 /* try finding the new parent index */
1705 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001706 p_index = clk_fetch_parent_index(core, parent);
Ulf Hansson031dcc92013-04-02 23:09:38 +02001707 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001708 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001709 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001710 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001711 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001712 goto out;
1713 }
1714 }
1715
Mike Turquetteb24764902012-03-15 23:11:19 -07001716 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001717 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001718
1719 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001720 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001721 goto out;
1722
Ulf Hansson031dcc92013-04-02 23:09:38 +02001723 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001724 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001725
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001726 /* propagate rate an accuracy recalculation accordingly */
1727 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001728 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001729 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001730 __clk_recalc_rates(core, POST_RATE_CHANGE);
1731 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001732 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001733
1734out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001735 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001736
1737 return ret;
1738}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001739
1740/**
1741 * clk_set_parent - switch the parent of a mux clk
1742 * @clk: the mux clk whose input we are switching
1743 * @parent: the new input to clk
1744 *
1745 * Re-parent clk to use parent as its new input source. If clk is in
1746 * prepared state, the clk will get enabled for the duration of this call. If
1747 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1748 * that, the reparenting is glitchy in hardware, etc), use the
1749 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1750 *
1751 * After successfully changing clk's parent clk_set_parent will update the
1752 * clk topology, sysfs topology and propagate rate recalculation via
1753 * __clk_recalc_rates.
1754 *
1755 * Returns 0 on success, -EERROR otherwise.
1756 */
1757int clk_set_parent(struct clk *clk, struct clk *parent)
1758{
1759 if (!clk)
1760 return 0;
1761
1762 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1763}
Mike Turquetteb24764902012-03-15 23:11:19 -07001764EXPORT_SYMBOL_GPL(clk_set_parent);
1765
1766/**
Mike Turquettee59c5372014-02-18 21:21:25 -08001767 * clk_set_phase - adjust the phase shift of a clock signal
1768 * @clk: clock signal source
1769 * @degrees: number of degrees the signal is shifted
1770 *
1771 * Shifts the phase of a clock signal by the specified
1772 * degrees. Returns 0 on success, -EERROR otherwise.
1773 *
1774 * This function makes no distinction about the input or reference
1775 * signal that we adjust the clock signal phase against. For example
1776 * phase locked-loop clock signal generators we may shift phase with
1777 * respect to feedback clock signal input, but for other cases the
1778 * clock phase may be shifted with respect to some other, unspecified
1779 * signal.
1780 *
1781 * Additionally the concept of phase shift does not propagate through
1782 * the clock tree hierarchy, which sets it apart from clock rates and
1783 * clock accuracy. A parent clock phase attribute does not have an
1784 * impact on the phase attribute of a child clock.
1785 */
1786int clk_set_phase(struct clk *clk, int degrees)
1787{
Stephen Boyd08b95752015-02-02 14:09:43 -08001788 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08001789
1790 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08001791 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08001792
1793 /* sanity check degrees */
1794 degrees %= 360;
1795 if (degrees < 0)
1796 degrees += 360;
1797
1798 clk_prepare_lock();
1799
Stephen Boyddfc202e2015-02-02 14:37:41 -08001800 trace_clk_set_phase(clk->core, degrees);
1801
Stephen Boyd08b95752015-02-02 14:09:43 -08001802 if (clk->core->ops->set_phase)
1803 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08001804
Stephen Boyddfc202e2015-02-02 14:37:41 -08001805 trace_clk_set_phase_complete(clk->core, degrees);
1806
Mike Turquettee59c5372014-02-18 21:21:25 -08001807 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001808 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08001809
Mike Turquettee59c5372014-02-18 21:21:25 -08001810 clk_prepare_unlock();
1811
Mike Turquettee59c5372014-02-18 21:21:25 -08001812 return ret;
1813}
Maxime Ripard9767b042015-01-20 22:23:43 +01001814EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001815
Stephen Boydd6968fc2015-04-30 13:54:13 -07001816static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08001817{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07001818 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08001819
1820 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001821 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08001822 clk_prepare_unlock();
1823
Mike Turquettee59c5372014-02-18 21:21:25 -08001824 return ret;
1825}
1826
1827/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001828 * clk_get_phase - return the phase shift of a clock signal
1829 * @clk: clock signal source
1830 *
1831 * Returns the phase shift of a clock node in degrees, otherwise returns
1832 * -EERROR.
1833 */
1834int clk_get_phase(struct clk *clk)
1835{
1836 if (!clk)
1837 return 0;
1838
1839 return clk_core_get_phase(clk->core);
1840}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001841EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07001842
1843/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08001844 * clk_is_match - check if two clk's point to the same hardware clock
1845 * @p: clk compared against q
1846 * @q: clk compared against p
1847 *
1848 * Returns true if the two struct clk pointers both point to the same hardware
1849 * clock node. Put differently, returns true if struct clk *p and struct clk *q
1850 * share the same struct clk_core object.
1851 *
1852 * Returns false otherwise. Note that two NULL clks are treated as matching.
1853 */
1854bool clk_is_match(const struct clk *p, const struct clk *q)
1855{
1856 /* trivial case: identical struct clk's or both NULL */
1857 if (p == q)
1858 return true;
1859
1860 /* true if clk->core pointers match. Avoid derefing garbage */
1861 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
1862 if (p->core == q->core)
1863 return true;
1864
1865 return false;
1866}
1867EXPORT_SYMBOL_GPL(clk_is_match);
1868
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001869/*** debugfs support ***/
1870
1871#ifdef CONFIG_DEBUG_FS
1872#include <linux/debugfs.h>
1873
1874static struct dentry *rootdir;
1875static int inited = 0;
1876static DEFINE_MUTEX(clk_debug_lock);
1877static HLIST_HEAD(clk_debug_list);
1878
1879static struct hlist_head *all_lists[] = {
1880 &clk_root_list,
1881 &clk_orphan_list,
1882 NULL,
1883};
1884
1885static struct hlist_head *orphan_list[] = {
1886 &clk_orphan_list,
1887 NULL,
1888};
1889
1890static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
1891 int level)
1892{
1893 if (!c)
1894 return;
1895
1896 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
1897 level * 3 + 1, "",
1898 30 - level * 3, c->name,
1899 c->enable_count, c->prepare_count, clk_core_get_rate(c),
1900 clk_core_get_accuracy(c), clk_core_get_phase(c));
1901}
1902
1903static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
1904 int level)
1905{
1906 struct clk_core *child;
1907
1908 if (!c)
1909 return;
1910
1911 clk_summary_show_one(s, c, level);
1912
1913 hlist_for_each_entry(child, &c->children, child_node)
1914 clk_summary_show_subtree(s, child, level + 1);
1915}
1916
1917static int clk_summary_show(struct seq_file *s, void *data)
1918{
1919 struct clk_core *c;
1920 struct hlist_head **lists = (struct hlist_head **)s->private;
1921
1922 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
1923 seq_puts(s, "----------------------------------------------------------------------------------------\n");
1924
1925 clk_prepare_lock();
1926
1927 for (; *lists; lists++)
1928 hlist_for_each_entry(c, *lists, child_node)
1929 clk_summary_show_subtree(s, c, 0);
1930
1931 clk_prepare_unlock();
1932
1933 return 0;
1934}
1935
1936
1937static int clk_summary_open(struct inode *inode, struct file *file)
1938{
1939 return single_open(file, clk_summary_show, inode->i_private);
1940}
1941
1942static const struct file_operations clk_summary_fops = {
1943 .open = clk_summary_open,
1944 .read = seq_read,
1945 .llseek = seq_lseek,
1946 .release = single_release,
1947};
1948
1949static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
1950{
1951 if (!c)
1952 return;
1953
1954 seq_printf(s, "\"%s\": { ", c->name);
1955 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
1956 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
1957 seq_printf(s, "\"rate\": %lu", clk_core_get_rate(c));
1958 seq_printf(s, "\"accuracy\": %lu", clk_core_get_accuracy(c));
1959 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
1960}
1961
1962static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
1963{
1964 struct clk_core *child;
1965
1966 if (!c)
1967 return;
1968
1969 clk_dump_one(s, c, level);
1970
1971 hlist_for_each_entry(child, &c->children, child_node) {
1972 seq_printf(s, ",");
1973 clk_dump_subtree(s, child, level + 1);
1974 }
1975
1976 seq_printf(s, "}");
1977}
1978
1979static int clk_dump(struct seq_file *s, void *data)
1980{
1981 struct clk_core *c;
1982 bool first_node = true;
1983 struct hlist_head **lists = (struct hlist_head **)s->private;
1984
1985 seq_printf(s, "{");
1986
1987 clk_prepare_lock();
1988
1989 for (; *lists; lists++) {
1990 hlist_for_each_entry(c, *lists, child_node) {
1991 if (!first_node)
1992 seq_puts(s, ",");
1993 first_node = false;
1994 clk_dump_subtree(s, c, 0);
1995 }
1996 }
1997
1998 clk_prepare_unlock();
1999
2000 seq_printf(s, "}");
2001 return 0;
2002}
2003
2004
2005static int clk_dump_open(struct inode *inode, struct file *file)
2006{
2007 return single_open(file, clk_dump, inode->i_private);
2008}
2009
2010static const struct file_operations clk_dump_fops = {
2011 .open = clk_dump_open,
2012 .read = seq_read,
2013 .llseek = seq_lseek,
2014 .release = single_release,
2015};
2016
2017static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2018{
2019 struct dentry *d;
2020 int ret = -ENOMEM;
2021
2022 if (!core || !pdentry) {
2023 ret = -EINVAL;
2024 goto out;
2025 }
2026
2027 d = debugfs_create_dir(core->name, pdentry);
2028 if (!d)
2029 goto out;
2030
2031 core->dentry = d;
2032
2033 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2034 (u32 *)&core->rate);
2035 if (!d)
2036 goto err_out;
2037
2038 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2039 (u32 *)&core->accuracy);
2040 if (!d)
2041 goto err_out;
2042
2043 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2044 (u32 *)&core->phase);
2045 if (!d)
2046 goto err_out;
2047
2048 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2049 (u32 *)&core->flags);
2050 if (!d)
2051 goto err_out;
2052
2053 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2054 (u32 *)&core->prepare_count);
2055 if (!d)
2056 goto err_out;
2057
2058 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2059 (u32 *)&core->enable_count);
2060 if (!d)
2061 goto err_out;
2062
2063 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2064 (u32 *)&core->notifier_count);
2065 if (!d)
2066 goto err_out;
2067
2068 if (core->ops->debug_init) {
2069 ret = core->ops->debug_init(core->hw, core->dentry);
2070 if (ret)
2071 goto err_out;
2072 }
2073
2074 ret = 0;
2075 goto out;
2076
2077err_out:
2078 debugfs_remove_recursive(core->dentry);
2079 core->dentry = NULL;
2080out:
2081 return ret;
2082}
2083
2084/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002085 * clk_debug_register - add a clk node to the debugfs clk directory
2086 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002087 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002088 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2089 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002090 * will be created lazily by clk_debug_init as part of a late_initcall.
2091 */
2092static int clk_debug_register(struct clk_core *core)
2093{
2094 int ret = 0;
2095
2096 mutex_lock(&clk_debug_lock);
2097 hlist_add_head(&core->debug_node, &clk_debug_list);
2098
2099 if (!inited)
2100 goto unlock;
2101
2102 ret = clk_debug_create_one(core, rootdir);
2103unlock:
2104 mutex_unlock(&clk_debug_lock);
2105
2106 return ret;
2107}
2108
2109 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002110 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2111 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002112 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002113 * Dynamically removes a clk and all its child nodes from the
2114 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002115 * clk_debug_register in __clk_init.
2116 */
2117static void clk_debug_unregister(struct clk_core *core)
2118{
2119 mutex_lock(&clk_debug_lock);
2120 hlist_del_init(&core->debug_node);
2121 debugfs_remove_recursive(core->dentry);
2122 core->dentry = NULL;
2123 mutex_unlock(&clk_debug_lock);
2124}
2125
2126struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2127 void *data, const struct file_operations *fops)
2128{
2129 struct dentry *d = NULL;
2130
2131 if (hw->core->dentry)
2132 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2133 fops);
2134
2135 return d;
2136}
2137EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2138
2139/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002140 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002141 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002142 * clks are often initialized very early during boot before memory can be
2143 * dynamically allocated and well before debugfs is setup. This function
2144 * populates the debugfs clk directory once at boot-time when we know that
2145 * debugfs is setup. It should only be called once at boot-time, all other clks
2146 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002147 */
2148static int __init clk_debug_init(void)
2149{
2150 struct clk_core *core;
2151 struct dentry *d;
2152
2153 rootdir = debugfs_create_dir("clk", NULL);
2154
2155 if (!rootdir)
2156 return -ENOMEM;
2157
2158 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2159 &clk_summary_fops);
2160 if (!d)
2161 return -ENOMEM;
2162
2163 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2164 &clk_dump_fops);
2165 if (!d)
2166 return -ENOMEM;
2167
2168 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2169 &orphan_list, &clk_summary_fops);
2170 if (!d)
2171 return -ENOMEM;
2172
2173 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2174 &orphan_list, &clk_dump_fops);
2175 if (!d)
2176 return -ENOMEM;
2177
2178 mutex_lock(&clk_debug_lock);
2179 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2180 clk_debug_create_one(core, rootdir);
2181
2182 inited = 1;
2183 mutex_unlock(&clk_debug_lock);
2184
2185 return 0;
2186}
2187late_initcall(clk_debug_init);
2188#else
2189static inline int clk_debug_register(struct clk_core *core) { return 0; }
2190static inline void clk_debug_reparent(struct clk_core *core,
2191 struct clk_core *new_parent)
2192{
2193}
2194static inline void clk_debug_unregister(struct clk_core *core)
2195{
2196}
2197#endif
2198
Michael Turquette3d3801e2015-02-25 09:11:01 -08002199/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002200 * __clk_init - initialize the data structures in a struct clk
2201 * @dev: device initializing this clk, placeholder for now
2202 * @clk: clk being initialized
2203 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002204 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002205 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002206 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002207static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002208{
Mike Turquetted1302a32012-03-29 14:30:40 -07002209 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002210 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002211 struct hlist_node *tmp2;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002212 struct clk_core *core;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002213 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002214
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002215 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002216 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002217
Stephen Boydd6968fc2015-04-30 13:54:13 -07002218 core = clk_user->core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002219
Mike Turquetteeab89f62013-03-28 13:59:01 -07002220 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002221
2222 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002223 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002224 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002225 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002226 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002227 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002228 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002229
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002230 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002231 if (core->ops->set_rate &&
2232 !((core->ops->round_rate || core->ops->determine_rate) &&
2233 core->ops->recalc_rate)) {
James Hogan71472c02013-07-29 12:25:00 +01002234 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002235 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002236 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002237 goto out;
2238 }
2239
Stephen Boydd6968fc2015-04-30 13:54:13 -07002240 if (core->ops->set_parent && !core->ops->get_parent) {
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002241 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002242 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002243 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002244 goto out;
2245 }
2246
Stephen Boydd6968fc2015-04-30 13:54:13 -07002247 if (core->ops->set_rate_and_parent &&
2248 !(core->ops->set_parent && core->ops->set_rate)) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002249 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002250 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002251 ret = -EINVAL;
2252 goto out;
2253 }
2254
Mike Turquetteb24764902012-03-15 23:11:19 -07002255 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002256 for (i = 0; i < core->num_parents; i++)
2257 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002258 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002259 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002260
2261 /*
2262 * Allocate an array of struct clk *'s to avoid unnecessary string
2263 * look-ups of clk's possible parents. This can fail for clocks passed
Stephen Boydd6968fc2015-04-30 13:54:13 -07002264 * in to clk_init during early boot; thus any access to core->parents[]
Mike Turquetteb24764902012-03-15 23:11:19 -07002265 * must always check for a NULL pointer and try to populate it if
2266 * necessary.
2267 *
Stephen Boydd6968fc2015-04-30 13:54:13 -07002268 * If core->parents is not NULL we skip this entire block. This allows
2269 * for clock drivers to statically initialize core->parents.
Mike Turquetteb24764902012-03-15 23:11:19 -07002270 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002271 if (core->num_parents > 1 && !core->parents) {
2272 core->parents = kcalloc(core->num_parents, sizeof(struct clk *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002273 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002274 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002275 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002276 * clk_init'd; thus any access to clk->parents[] must check
2277 * for a NULL pointer. We can always perform lazy lookups for
2278 * missing parents later on.
2279 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002280 if (core->parents)
2281 for (i = 0; i < core->num_parents; i++)
2282 core->parents[i] =
2283 clk_core_lookup(core->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002284 }
2285
Stephen Boydd6968fc2015-04-30 13:54:13 -07002286 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002287
2288 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07002289 * Populate core->parent if parent has already been __clk_init'd. If
Mike Turquetteb24764902012-03-15 23:11:19 -07002290 * parent has not yet been __clk_init'd then place clk in the orphan
2291 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2292 * clk list.
2293 *
2294 * Every time a new clk is clk_init'd then we walk the list of orphan
2295 * clocks and re-parent any that are children of the clock currently
2296 * being clk_init'd.
2297 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002298 if (core->parent)
2299 hlist_add_head(&core->child_node,
2300 &core->parent->children);
2301 else if (core->flags & CLK_IS_ROOT)
2302 hlist_add_head(&core->child_node, &clk_root_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002303 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002304 hlist_add_head(&core->child_node, &clk_orphan_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002305
2306 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002307 * Set clk's accuracy. The preferred method is to use
2308 * .recalc_accuracy. For simple clocks and lazy developers the default
2309 * fallback is to use the parent's accuracy. If a clock doesn't have a
2310 * parent (or is orphaned) then accuracy is set to zero (perfect
2311 * clock).
2312 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002313 if (core->ops->recalc_accuracy)
2314 core->accuracy = core->ops->recalc_accuracy(core->hw,
2315 __clk_get_accuracy(core->parent));
2316 else if (core->parent)
2317 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002318 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002319 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002320
2321 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002322 * Set clk's phase.
2323 * Since a phase is by definition relative to its parent, just
2324 * query the current clock phase, or just assume it's in phase.
2325 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002326 if (core->ops->get_phase)
2327 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002328 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002329 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002330
2331 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002332 * Set clk's rate. The preferred method is to use .recalc_rate. For
2333 * simple clocks and lazy developers the default fallback is to use the
2334 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2335 * then rate is set to zero.
2336 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002337 if (core->ops->recalc_rate)
2338 rate = core->ops->recalc_rate(core->hw,
2339 clk_core_get_rate_nolock(core->parent));
2340 else if (core->parent)
2341 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002342 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002343 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002344 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002345
2346 /*
2347 * walk the list of orphan clocks and reparent any that are children of
2348 * this clock
2349 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002350 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002351 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002352 i = orphan->ops->get_parent(orphan->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002353 if (!strcmp(core->name, orphan->parent_names[i]))
2354 clk_core_reparent(orphan, core);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002355 continue;
2356 }
2357
Mike Turquetteb24764902012-03-15 23:11:19 -07002358 for (i = 0; i < orphan->num_parents; i++)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002359 if (!strcmp(core->name, orphan->parent_names[i])) {
2360 clk_core_reparent(orphan, core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002361 break;
2362 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002363 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002364
2365 /*
2366 * optional platform-specific magic
2367 *
2368 * The .init callback is not used by any of the basic clock types, but
2369 * exists for weird hardware that must perform initialization magic.
2370 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002371 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002372 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002373 if (core->ops->init)
2374 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002375
Stephen Boydd6968fc2015-04-30 13:54:13 -07002376 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002377out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002378 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002379
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002380 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002381 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002382
Mike Turquetted1302a32012-03-29 14:30:40 -07002383 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002384}
2385
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002386struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2387 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002388{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002389 struct clk *clk;
2390
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002391 /* This is to allow this function to be chained to others */
2392 if (!hw || IS_ERR(hw))
2393 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002394
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002395 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2396 if (!clk)
2397 return ERR_PTR(-ENOMEM);
2398
2399 clk->core = hw->core;
2400 clk->dev_id = dev_id;
2401 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002402 clk->max_rate = ULONG_MAX;
2403
2404 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002405 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002406 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002407
2408 return clk;
2409}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002410
Stephen Boyd73e0e492015-02-06 11:42:43 -08002411void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002412{
2413 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002414 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002415 clk_prepare_unlock();
2416
2417 kfree(clk);
2418}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002419
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002420/**
2421 * clk_register - allocate a new clock, register it and return an opaque cookie
2422 * @dev: device that is registering this clock
2423 * @hw: link to hardware-specific clock data
2424 *
2425 * clk_register is the primary interface for populating the clock tree with new
2426 * clock nodes. It returns a pointer to the newly allocated struct clk which
2427 * cannot be dereferenced by driver code but may be used in conjuction with the
2428 * rest of the clock API. In the event of an error clk_register will return an
2429 * error code; drivers must test for an error code after calling clk_register.
2430 */
2431struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002432{
Mike Turquetted1302a32012-03-29 14:30:40 -07002433 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002434 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002435
Stephen Boydd6968fc2015-04-30 13:54:13 -07002436 core = kzalloc(sizeof(*core), GFP_KERNEL);
2437 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002438 ret = -ENOMEM;
2439 goto fail_out;
2440 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002441
Stephen Boydd6968fc2015-04-30 13:54:13 -07002442 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2443 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002444 ret = -ENOMEM;
2445 goto fail_name;
2446 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002447 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002448 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002449 core->owner = dev->driver->owner;
2450 core->hw = hw;
2451 core->flags = hw->init->flags;
2452 core->num_parents = hw->init->num_parents;
2453 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002454
Mike Turquetted1302a32012-03-29 14:30:40 -07002455 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002456 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002457 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002458
Stephen Boydd6968fc2015-04-30 13:54:13 -07002459 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002460 ret = -ENOMEM;
2461 goto fail_parent_names;
2462 }
2463
2464
2465 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002466 for (i = 0; i < core->num_parents; i++) {
2467 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002468 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002469 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002470 ret = -ENOMEM;
2471 goto fail_parent_names_copy;
2472 }
2473 }
2474
Stephen Boydd6968fc2015-04-30 13:54:13 -07002475 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002476
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002477 hw->clk = __clk_create_clk(hw, NULL, NULL);
2478 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002479 ret = PTR_ERR(hw->clk);
2480 goto fail_parent_names_copy;
2481 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002482
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002483 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002484 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002485 return hw->clk;
2486
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002487 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002488 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002489
2490fail_parent_names_copy:
2491 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002492 kfree_const(core->parent_names[i]);
2493 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07002494fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002495 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002496fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002497 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002498fail_out:
2499 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002500}
2501EXPORT_SYMBOL_GPL(clk_register);
2502
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002503/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002504static void __clk_release(struct kref *ref)
2505{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002506 struct clk_core *core = container_of(ref, struct clk_core, ref);
2507 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002508
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002509 lockdep_assert_held(&prepare_lock);
2510
Stephen Boydd6968fc2015-04-30 13:54:13 -07002511 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002512 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002513 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002514
Stephen Boydd6968fc2015-04-30 13:54:13 -07002515 kfree(core->parent_names);
2516 kfree_const(core->name);
2517 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002518}
2519
2520/*
2521 * Empty clk_ops for unregistered clocks. These are used temporarily
2522 * after clk_unregister() was called on a clock and until last clock
2523 * consumer calls clk_put() and the struct clk object is freed.
2524 */
2525static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2526{
2527 return -ENXIO;
2528}
2529
2530static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2531{
2532 WARN_ON_ONCE(1);
2533}
2534
2535static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2536 unsigned long parent_rate)
2537{
2538 return -ENXIO;
2539}
2540
2541static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2542{
2543 return -ENXIO;
2544}
2545
2546static const struct clk_ops clk_nodrv_ops = {
2547 .enable = clk_nodrv_prepare_enable,
2548 .disable = clk_nodrv_disable_unprepare,
2549 .prepare = clk_nodrv_prepare_enable,
2550 .unprepare = clk_nodrv_disable_unprepare,
2551 .set_rate = clk_nodrv_set_rate,
2552 .set_parent = clk_nodrv_set_parent,
2553};
2554
Mark Brown1df5c932012-04-18 09:07:12 +01002555/**
2556 * clk_unregister - unregister a currently registered clock
2557 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002558 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002559void clk_unregister(struct clk *clk)
2560{
2561 unsigned long flags;
2562
Stephen Boyd6314b672014-09-04 23:37:49 -07002563 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2564 return;
2565
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002566 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002567
2568 clk_prepare_lock();
2569
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002570 if (clk->core->ops == &clk_nodrv_ops) {
2571 pr_err("%s: unregistered clock: %s\n", __func__,
2572 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002573 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002574 }
2575 /*
2576 * Assign empty clock ops for consumers that might still hold
2577 * a reference to this clock.
2578 */
2579 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002580 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002581 clk_enable_unlock(flags);
2582
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002583 if (!hlist_empty(&clk->core->children)) {
2584 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002585 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002586
2587 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002588 hlist_for_each_entry_safe(child, t, &clk->core->children,
2589 child_node)
2590 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002591 }
2592
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002593 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002594
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002595 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002596 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002597 __func__, clk->core->name);
2598 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002599
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002600 clk_prepare_unlock();
2601}
Mark Brown1df5c932012-04-18 09:07:12 +01002602EXPORT_SYMBOL_GPL(clk_unregister);
2603
Stephen Boyd46c87732012-09-24 13:38:04 -07002604static void devm_clk_release(struct device *dev, void *res)
2605{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002606 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002607}
2608
2609/**
2610 * devm_clk_register - resource managed clk_register()
2611 * @dev: device that is registering this clock
2612 * @hw: link to hardware-specific clock data
2613 *
2614 * Managed clk_register(). Clocks returned from this function are
2615 * automatically clk_unregister()ed on driver detach. See clk_register() for
2616 * more information.
2617 */
2618struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2619{
2620 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002621 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002622
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002623 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2624 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002625 return ERR_PTR(-ENOMEM);
2626
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002627 clk = clk_register(dev, hw);
2628 if (!IS_ERR(clk)) {
2629 *clkp = clk;
2630 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002631 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002632 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002633 }
2634
2635 return clk;
2636}
2637EXPORT_SYMBOL_GPL(devm_clk_register);
2638
2639static int devm_clk_match(struct device *dev, void *res, void *data)
2640{
2641 struct clk *c = res;
2642 if (WARN_ON(!c))
2643 return 0;
2644 return c == data;
2645}
2646
2647/**
2648 * devm_clk_unregister - resource managed clk_unregister()
2649 * @clk: clock to unregister
2650 *
2651 * Deallocate a clock allocated with devm_clk_register(). Normally
2652 * this function will not need to be called and the resource management
2653 * code will ensure that the resource is freed.
2654 */
2655void devm_clk_unregister(struct device *dev, struct clk *clk)
2656{
2657 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2658}
2659EXPORT_SYMBOL_GPL(devm_clk_unregister);
2660
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002661/*
2662 * clkdev helpers
2663 */
2664int __clk_get(struct clk *clk)
2665{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002666 struct clk_core *core = !clk ? NULL : clk->core;
2667
2668 if (core) {
2669 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002670 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002671
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002672 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002673 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002674 return 1;
2675}
2676
2677void __clk_put(struct clk *clk)
2678{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002679 struct module *owner;
2680
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002681 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002682 return;
2683
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002684 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002685
Stephen Boyd50595f82015-02-06 11:42:44 -08002686 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002687 if (clk->min_rate > clk->core->req_rate ||
2688 clk->max_rate < clk->core->req_rate)
2689 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2690
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002691 owner = clk->core->owner;
2692 kref_put(&clk->core->ref, __clk_release);
2693
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002694 clk_prepare_unlock();
2695
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002696 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002697
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002698 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002699}
2700
Mike Turquetteb24764902012-03-15 23:11:19 -07002701/*** clk rate change notifiers ***/
2702
2703/**
2704 * clk_notifier_register - add a clk rate change notifier
2705 * @clk: struct clk * to watch
2706 * @nb: struct notifier_block * with callback info
2707 *
2708 * Request notification when clk's rate changes. This uses an SRCU
2709 * notifier because we want it to block and notifier unregistrations are
2710 * uncommon. The callbacks associated with the notifier must not
2711 * re-enter into the clk framework by calling any top-level clk APIs;
2712 * this will cause a nested prepare_lock mutex.
2713 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002714 * In all notification cases cases (pre, post and abort rate change) the
2715 * original clock rate is passed to the callback via struct
2716 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002717 * clk_notifier_data.new_rate.
2718 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002719 * clk_notifier_register() must be called from non-atomic context.
2720 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2721 * allocation failure; otherwise, passes along the return value of
2722 * srcu_notifier_chain_register().
2723 */
2724int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2725{
2726 struct clk_notifier *cn;
2727 int ret = -ENOMEM;
2728
2729 if (!clk || !nb)
2730 return -EINVAL;
2731
Mike Turquetteeab89f62013-03-28 13:59:01 -07002732 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002733
2734 /* search the list of notifiers for this clk */
2735 list_for_each_entry(cn, &clk_notifier_list, node)
2736 if (cn->clk == clk)
2737 break;
2738
2739 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2740 if (cn->clk != clk) {
2741 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2742 if (!cn)
2743 goto out;
2744
2745 cn->clk = clk;
2746 srcu_init_notifier_head(&cn->notifier_head);
2747
2748 list_add(&cn->node, &clk_notifier_list);
2749 }
2750
2751 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2752
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002753 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002754
2755out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002756 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002757
2758 return ret;
2759}
2760EXPORT_SYMBOL_GPL(clk_notifier_register);
2761
2762/**
2763 * clk_notifier_unregister - remove a clk rate change notifier
2764 * @clk: struct clk *
2765 * @nb: struct notifier_block * with callback info
2766 *
2767 * Request no further notification for changes to 'clk' and frees memory
2768 * allocated in clk_notifier_register.
2769 *
2770 * Returns -EINVAL if called with null arguments; otherwise, passes
2771 * along the return value of srcu_notifier_chain_unregister().
2772 */
2773int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2774{
2775 struct clk_notifier *cn = NULL;
2776 int ret = -EINVAL;
2777
2778 if (!clk || !nb)
2779 return -EINVAL;
2780
Mike Turquetteeab89f62013-03-28 13:59:01 -07002781 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002782
2783 list_for_each_entry(cn, &clk_notifier_list, node)
2784 if (cn->clk == clk)
2785 break;
2786
2787 if (cn->clk == clk) {
2788 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2789
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002790 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002791
2792 /* XXX the notifier code should handle this better */
2793 if (!cn->notifier_head.head) {
2794 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002795 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002796 kfree(cn);
2797 }
2798
2799 } else {
2800 ret = -ENOENT;
2801 }
2802
Mike Turquetteeab89f62013-03-28 13:59:01 -07002803 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002804
2805 return ret;
2806}
2807EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002808
2809#ifdef CONFIG_OF
2810/**
2811 * struct of_clk_provider - Clock provider registration structure
2812 * @link: Entry in global list of clock providers
2813 * @node: Pointer to device tree node of clock provider
2814 * @get: Get clock callback. Returns NULL or a struct clk for the
2815 * given clock specifier
2816 * @data: context pointer to be passed into @get callback
2817 */
2818struct of_clk_provider {
2819 struct list_head link;
2820
2821 struct device_node *node;
2822 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2823 void *data;
2824};
2825
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302826static const struct of_device_id __clk_of_table_sentinel
2827 __used __section(__clk_of_table_end);
2828
Grant Likely766e6a42012-04-09 14:50:06 -05002829static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002830static DEFINE_MUTEX(of_clk_mutex);
2831
Grant Likely766e6a42012-04-09 14:50:06 -05002832struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2833 void *data)
2834{
2835 return data;
2836}
2837EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2838
Shawn Guo494bfec2012-08-22 21:36:27 +08002839struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2840{
2841 struct clk_onecell_data *clk_data = data;
2842 unsigned int idx = clkspec->args[0];
2843
2844 if (idx >= clk_data->clk_num) {
2845 pr_err("%s: invalid clock index %d\n", __func__, idx);
2846 return ERR_PTR(-EINVAL);
2847 }
2848
2849 return clk_data->clks[idx];
2850}
2851EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2852
Grant Likely766e6a42012-04-09 14:50:06 -05002853/**
2854 * of_clk_add_provider() - Register a clock provider for a node
2855 * @np: Device node pointer associated with clock provider
2856 * @clk_src_get: callback for decoding clock
2857 * @data: context pointer for @clk_src_get callback.
2858 */
2859int of_clk_add_provider(struct device_node *np,
2860 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2861 void *data),
2862 void *data)
2863{
2864 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002865 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002866
2867 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2868 if (!cp)
2869 return -ENOMEM;
2870
2871 cp->node = of_node_get(np);
2872 cp->data = data;
2873 cp->get = clk_src_get;
2874
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002875 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002876 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002877 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002878 pr_debug("Added clock from %s\n", np->full_name);
2879
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002880 ret = of_clk_set_defaults(np, true);
2881 if (ret < 0)
2882 of_clk_del_provider(np);
2883
2884 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002885}
2886EXPORT_SYMBOL_GPL(of_clk_add_provider);
2887
2888/**
2889 * of_clk_del_provider() - Remove a previously registered clock provider
2890 * @np: Device node pointer associated with clock provider
2891 */
2892void of_clk_del_provider(struct device_node *np)
2893{
2894 struct of_clk_provider *cp;
2895
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002896 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002897 list_for_each_entry(cp, &of_clk_providers, link) {
2898 if (cp->node == np) {
2899 list_del(&cp->link);
2900 of_node_put(cp->node);
2901 kfree(cp);
2902 break;
2903 }
2904 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002905 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002906}
2907EXPORT_SYMBOL_GPL(of_clk_del_provider);
2908
Stephen Boyd73e0e492015-02-06 11:42:43 -08002909struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
2910 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05002911{
2912 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002913 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002914
Stephen Boyd306c3422015-02-05 15:39:11 -08002915 if (!clkspec)
2916 return ERR_PTR(-EINVAL);
2917
Grant Likely766e6a42012-04-09 14:50:06 -05002918 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08002919 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002920 list_for_each_entry(provider, &of_clk_providers, link) {
2921 if (provider->node == clkspec->np)
2922 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002923 if (!IS_ERR(clk)) {
2924 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
2925 con_id);
2926
2927 if (!IS_ERR(clk) && !__clk_get(clk)) {
2928 __clk_free_clk(clk);
2929 clk = ERR_PTR(-ENOENT);
2930 }
2931
Grant Likely766e6a42012-04-09 14:50:06 -05002932 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08002933 }
Grant Likely766e6a42012-04-09 14:50:06 -05002934 }
Stephen Boyd306c3422015-02-05 15:39:11 -08002935 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002936
2937 return clk;
2938}
2939
Stephen Boyd306c3422015-02-05 15:39:11 -08002940/**
2941 * of_clk_get_from_provider() - Lookup a clock from a clock provider
2942 * @clkspec: pointer to a clock specifier data structure
2943 *
2944 * This function looks up a struct clk from the registered list of clock
2945 * providers, an input is a clock specifier data structure as returned
2946 * from the of_parse_phandle_with_args() function call.
2947 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002948struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2949{
Stephen Boyd306c3422015-02-05 15:39:11 -08002950 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05002951}
2952
Mike Turquettef6102742013-10-07 23:12:13 -07002953int of_clk_get_parent_count(struct device_node *np)
2954{
2955 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2956}
2957EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2958
Grant Likely766e6a42012-04-09 14:50:06 -05002959const char *of_clk_get_parent_name(struct device_node *np, int index)
2960{
2961 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002962 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05002963 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002964 const __be32 *vp;
2965 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05002966 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002967 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05002968
2969 if (index < 0)
2970 return NULL;
2971
2972 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2973 &clkspec);
2974 if (rc)
2975 return NULL;
2976
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002977 index = clkspec.args_count ? clkspec.args[0] : 0;
2978 count = 0;
2979
2980 /* if there is an indices property, use it to transfer the index
2981 * specified into an array offset for the clock-output-names property.
2982 */
2983 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
2984 if (index == pv) {
2985 index = count;
2986 break;
2987 }
2988 count++;
2989 }
2990
Grant Likely766e6a42012-04-09 14:50:06 -05002991 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002992 index,
Grant Likely766e6a42012-04-09 14:50:06 -05002993 &clk_name) < 0)
2994 clk_name = clkspec.np->name;
2995
2996 of_node_put(clkspec.np);
2997 return clk_name;
2998}
2999EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3000
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003001struct clock_provider {
3002 of_clk_init_cb_t clk_init_cb;
3003 struct device_node *np;
3004 struct list_head node;
3005};
3006
3007static LIST_HEAD(clk_provider_list);
3008
3009/*
3010 * This function looks for a parent clock. If there is one, then it
3011 * checks that the provider for this parent clock was initialized, in
3012 * this case the parent clock will be ready.
3013 */
3014static int parent_ready(struct device_node *np)
3015{
3016 int i = 0;
3017
3018 while (true) {
3019 struct clk *clk = of_clk_get(np, i);
3020
3021 /* this parent is ready we can check the next one */
3022 if (!IS_ERR(clk)) {
3023 clk_put(clk);
3024 i++;
3025 continue;
3026 }
3027
3028 /* at least one parent is not ready, we exit now */
3029 if (PTR_ERR(clk) == -EPROBE_DEFER)
3030 return 0;
3031
3032 /*
3033 * Here we make assumption that the device tree is
3034 * written correctly. So an error means that there is
3035 * no more parent. As we didn't exit yet, then the
3036 * previous parent are ready. If there is no clock
3037 * parent, no need to wait for them, then we can
3038 * consider their absence as being ready
3039 */
3040 return 1;
3041 }
3042}
3043
Grant Likely766e6a42012-04-09 14:50:06 -05003044/**
3045 * of_clk_init() - Scan and init clock providers from the DT
3046 * @matches: array of compatible values and init functions for providers.
3047 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003048 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003049 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003050 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003051 */
3052void __init of_clk_init(const struct of_device_id *matches)
3053{
Alex Elder7f7ed582013-08-22 11:31:31 -05003054 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003055 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003056 struct clock_provider *clk_provider, *next;
3057 bool is_init_done;
3058 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05003059
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303060 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003061 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303062
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003063 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003064 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003065 struct clock_provider *parent =
3066 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
3067
3068 parent->clk_init_cb = match->data;
3069 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003070 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003071 }
3072
3073 while (!list_empty(&clk_provider_list)) {
3074 is_init_done = false;
3075 list_for_each_entry_safe(clk_provider, next,
3076 &clk_provider_list, node) {
3077 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003078
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003079 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003080 of_clk_set_defaults(clk_provider->np, true);
3081
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003082 list_del(&clk_provider->node);
3083 kfree(clk_provider);
3084 is_init_done = true;
3085 }
3086 }
3087
3088 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003089 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003090 * remaining providers during the last loop, so now we
3091 * initialize all the remaining ones unconditionally
3092 * in case the clock parent was not mandatory
3093 */
3094 if (!is_init_done)
3095 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003096 }
3097}
3098#endif