blob: 659f2b036cf5b591013c0787f5ee2f1931a4d304 [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 Boyda6334722015-05-06 17:00:54 -0700546 lockdep_assert_held(&prepare_lock);
547
Stephen Boydd6968fc2015-04-30 13:54:13 -0700548 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700549 return;
550
Stephen Boydd6968fc2015-04-30 13:54:13 -0700551 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700552 return;
553
Stephen Boydd6968fc2015-04-30 13:54:13 -0700554 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700555 return;
556
Stephen Boydd6968fc2015-04-30 13:54:13 -0700557 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700558
Stephen Boydd6968fc2015-04-30 13:54:13 -0700559 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800560
Stephen Boydd6968fc2015-04-30 13:54:13 -0700561 if (core->ops->unprepare)
562 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700563
Stephen Boydd6968fc2015-04-30 13:54:13 -0700564 trace_clk_unprepare_complete(core);
565 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700566}
567
568/**
569 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200570 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700571 *
572 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
573 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
574 * if the operation may sleep. One example is a clk which is accessed over
575 * I2c. In the complex case a clk gate operation may require a fast and a slow
576 * part. It is this reason that clk_unprepare and clk_disable are not mutually
577 * exclusive. In fact clk_disable must be called before clk_unprepare.
578 */
579void clk_unprepare(struct clk *clk)
580{
Stephen Boyd63589e92014-03-26 16:06:37 -0700581 if (IS_ERR_OR_NULL(clk))
582 return;
583
Mike Turquetteeab89f62013-03-28 13:59:01 -0700584 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100585 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700586 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700587}
588EXPORT_SYMBOL_GPL(clk_unprepare);
589
Stephen Boydd6968fc2015-04-30 13:54:13 -0700590static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700591{
592 int ret = 0;
593
Stephen Boyda6334722015-05-06 17:00:54 -0700594 lockdep_assert_held(&prepare_lock);
595
Stephen Boydd6968fc2015-04-30 13:54:13 -0700596 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700597 return 0;
598
Stephen Boydd6968fc2015-04-30 13:54:13 -0700599 if (core->prepare_count == 0) {
600 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700601 if (ret)
602 return ret;
603
Stephen Boydd6968fc2015-04-30 13:54:13 -0700604 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800605
Stephen Boydd6968fc2015-04-30 13:54:13 -0700606 if (core->ops->prepare)
607 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800608
Stephen Boydd6968fc2015-04-30 13:54:13 -0700609 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800610
611 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700612 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800613 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700614 }
615 }
616
Stephen Boydd6968fc2015-04-30 13:54:13 -0700617 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700618
619 return 0;
620}
621
622/**
623 * clk_prepare - prepare a clock source
624 * @clk: the clk being prepared
625 *
626 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
627 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
628 * operation may sleep. One example is a clk which is accessed over I2c. In
629 * the complex case a clk ungate operation may require a fast and a slow part.
630 * It is this reason that clk_prepare and clk_enable are not mutually
631 * exclusive. In fact clk_prepare must be called before clk_enable.
632 * Returns 0 on success, -EERROR otherwise.
633 */
634int clk_prepare(struct clk *clk)
635{
636 int ret;
637
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100638 if (!clk)
639 return 0;
640
Mike Turquetteeab89f62013-03-28 13:59:01 -0700641 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100642 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700643 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700644
645 return ret;
646}
647EXPORT_SYMBOL_GPL(clk_prepare);
648
Stephen Boydd6968fc2015-04-30 13:54:13 -0700649static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700650{
Stephen Boyda6334722015-05-06 17:00:54 -0700651 lockdep_assert_held(&enable_lock);
652
Stephen Boydd6968fc2015-04-30 13:54:13 -0700653 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700654 return;
655
Stephen Boydd6968fc2015-04-30 13:54:13 -0700656 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700657 return;
658
Stephen Boydd6968fc2015-04-30 13:54:13 -0700659 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700660 return;
661
Stephen Boydd6968fc2015-04-30 13:54:13 -0700662 trace_clk_disable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800663
Stephen Boydd6968fc2015-04-30 13:54:13 -0700664 if (core->ops->disable)
665 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700666
Stephen Boydd6968fc2015-04-30 13:54:13 -0700667 trace_clk_disable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800668
Stephen Boydd6968fc2015-04-30 13:54:13 -0700669 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100670}
671
Mike Turquetteb24764902012-03-15 23:11:19 -0700672/**
673 * clk_disable - gate a clock
674 * @clk: the clk being gated
675 *
676 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
677 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
678 * clk if the operation is fast and will never sleep. One example is a
679 * SoC-internal clk which is controlled via simple register writes. In the
680 * complex case a clk gate operation may require a fast and a slow part. It is
681 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
682 * In fact clk_disable must be called before clk_unprepare.
683 */
684void clk_disable(struct clk *clk)
685{
686 unsigned long flags;
687
Stephen Boyd63589e92014-03-26 16:06:37 -0700688 if (IS_ERR_OR_NULL(clk))
689 return;
690
Mike Turquetteeab89f62013-03-28 13:59:01 -0700691 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700692 clk_core_disable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700693 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700694}
695EXPORT_SYMBOL_GPL(clk_disable);
696
Stephen Boydd6968fc2015-04-30 13:54:13 -0700697static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700698{
699 int ret = 0;
700
Stephen Boyda6334722015-05-06 17:00:54 -0700701 lockdep_assert_held(&enable_lock);
702
Stephen Boydd6968fc2015-04-30 13:54:13 -0700703 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700704 return 0;
705
Stephen Boydd6968fc2015-04-30 13:54:13 -0700706 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700707 return -ESHUTDOWN;
708
Stephen Boydd6968fc2015-04-30 13:54:13 -0700709 if (core->enable_count == 0) {
710 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700711
712 if (ret)
713 return ret;
714
Stephen Boydd6968fc2015-04-30 13:54:13 -0700715 trace_clk_enable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800716
Stephen Boydd6968fc2015-04-30 13:54:13 -0700717 if (core->ops->enable)
718 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800719
Stephen Boydd6968fc2015-04-30 13:54:13 -0700720 trace_clk_enable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800721
722 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700723 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800724 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700725 }
726 }
727
Stephen Boydd6968fc2015-04-30 13:54:13 -0700728 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700729 return 0;
730}
731
732/**
733 * clk_enable - ungate a clock
734 * @clk: the clk being ungated
735 *
736 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
737 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
738 * if the operation will never sleep. One example is a SoC-internal clk which
739 * is controlled via simple register writes. In the complex case a clk ungate
740 * operation may require a fast and a slow part. It is this reason that
741 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
742 * must be called before clk_enable. Returns 0 on success, -EERROR
743 * otherwise.
744 */
745int clk_enable(struct clk *clk)
746{
747 unsigned long flags;
748 int ret;
749
Dong Aisheng864e1602015-04-30 14:02:19 -0700750 if (!clk)
751 return 0;
752
Mike Turquetteeab89f62013-03-28 13:59:01 -0700753 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700754 ret = clk_core_enable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700755 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700756
757 return ret;
758}
759EXPORT_SYMBOL_GPL(clk_enable);
760
Stephen Boydd6968fc2015-04-30 13:54:13 -0700761static unsigned long clk_core_round_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100762 unsigned long rate,
763 unsigned long min_rate,
764 unsigned long max_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -0700765{
Shawn Guo81536e02012-04-12 20:50:17 +0800766 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100767 struct clk_core *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100768 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700769
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100770 lockdep_assert_held(&prepare_lock);
771
Stephen Boydd6968fc2015-04-30 13:54:13 -0700772 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700773 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700774
Stephen Boydd6968fc2015-04-30 13:54:13 -0700775 parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +0100776 if (parent)
777 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700778
Stephen Boydd6968fc2015-04-30 13:54:13 -0700779 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100780 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700781 return core->ops->determine_rate(core->hw, rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100782 min_rate, max_rate,
783 &parent_rate, &parent_hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700784 } else if (core->ops->round_rate)
785 return core->ops->round_rate(core->hw, rate, &parent_rate);
786 else if (core->flags & CLK_SET_RATE_PARENT)
787 return clk_core_round_rate_nolock(core->parent, rate, min_rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100788 max_rate);
James Hogan71472c02013-07-29 12:25:00 +0100789 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700790 return core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700791}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100792
793/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100794 * __clk_determine_rate - get the closest rate actually supported by a clock
795 * @hw: determine the rate of this clock
796 * @rate: target rate
797 * @min_rate: returned rate must be greater than this rate
798 * @max_rate: returned rate must be less than this rate
799 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700800 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100801 */
802unsigned long __clk_determine_rate(struct clk_hw *hw,
803 unsigned long rate,
804 unsigned long min_rate,
805 unsigned long max_rate)
806{
807 if (!hw)
808 return 0;
809
810 return clk_core_round_rate_nolock(hw->core, rate, min_rate, max_rate);
811}
812EXPORT_SYMBOL_GPL(__clk_determine_rate);
813
814/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100815 * __clk_round_rate - round the given rate for a clk
816 * @clk: round the rate of this clock
817 * @rate: the rate which is to be rounded
818 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700819 * Useful for clk_ops such as .set_rate
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100820 */
821unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
822{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100823 unsigned long min_rate;
824 unsigned long max_rate;
825
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100826 if (!clk)
827 return 0;
828
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100829 clk_core_get_boundaries(clk->core, &min_rate, &max_rate);
830
831 return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100832}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +0200833EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700834
835/**
836 * clk_round_rate - round the given rate for a clk
837 * @clk: the clk for which we are rounding a rate
838 * @rate: the rate which is to be rounded
839 *
840 * Takes in a rate as input and rounds it to a rate that the clk can actually
841 * use which is then returned. If clk doesn't support round_rate operation
842 * then the parent rate is returned.
843 */
844long clk_round_rate(struct clk *clk, unsigned long rate)
845{
846 unsigned long ret;
847
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100848 if (!clk)
849 return 0;
850
Mike Turquetteeab89f62013-03-28 13:59:01 -0700851 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700852 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700853 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700854
855 return ret;
856}
857EXPORT_SYMBOL_GPL(clk_round_rate);
858
859/**
860 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -0700861 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -0700862 * @msg: clk notifier type (see include/linux/clk.h)
863 * @old_rate: old clk rate
864 * @new_rate: new clk rate
865 *
866 * Triggers a notifier call chain on the clk rate-change notification
867 * for 'clk'. Passes a pointer to the struct clk and the previous
868 * and current rates to the notifier callback. Intended to be called by
869 * internal clock code only. Returns NOTIFY_DONE from the last driver
870 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
871 * a driver returns that.
872 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700873static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -0700874 unsigned long old_rate, unsigned long new_rate)
875{
876 struct clk_notifier *cn;
877 struct clk_notifier_data cnd;
878 int ret = NOTIFY_DONE;
879
Mike Turquetteb24764902012-03-15 23:11:19 -0700880 cnd.old_rate = old_rate;
881 cnd.new_rate = new_rate;
882
883 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700884 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100885 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700886 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
887 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -0700888 }
889 }
890
891 return ret;
892}
893
894/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100895 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -0700896 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100897 *
898 * Walks the subtree of clks starting with clk and recalculates accuracies as
899 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700900 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100901 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100902 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700903static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100904{
905 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100906 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100907
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100908 lockdep_assert_held(&prepare_lock);
909
Stephen Boydd6968fc2015-04-30 13:54:13 -0700910 if (core->parent)
911 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100912
Stephen Boydd6968fc2015-04-30 13:54:13 -0700913 if (core->ops->recalc_accuracy)
914 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100915 parent_accuracy);
916 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700917 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100918
Stephen Boydd6968fc2015-04-30 13:54:13 -0700919 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100920 __clk_recalc_accuracies(child);
921}
922
Stephen Boydd6968fc2015-04-30 13:54:13 -0700923static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100924{
925 unsigned long accuracy;
926
927 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -0700928 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
929 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100930
Stephen Boydd6968fc2015-04-30 13:54:13 -0700931 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100932 clk_prepare_unlock();
933
934 return accuracy;
935}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100936
937/**
938 * clk_get_accuracy - return the accuracy of clk
939 * @clk: the clk whose accuracy is being returned
940 *
941 * Simply returns the cached accuracy of the clk, unless
942 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
943 * issued.
944 * If clk is NULL then returns 0.
945 */
946long clk_get_accuracy(struct clk *clk)
947{
948 if (!clk)
949 return 0;
950
951 return clk_core_get_accuracy(clk->core);
952}
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100953EXPORT_SYMBOL_GPL(clk_get_accuracy);
954
Stephen Boydd6968fc2015-04-30 13:54:13 -0700955static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100956 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700957{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700958 if (core->ops->recalc_rate)
959 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700960 return parent_rate;
961}
962
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100963/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700964 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -0700965 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -0700966 * @msg: notification type (see include/linux/clk.h)
967 *
968 * Walks the subtree of clks starting with clk and recalculates rates as it
969 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200970 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -0700971 *
972 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
973 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -0700974 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700975static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -0700976{
977 unsigned long old_rate;
978 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100979 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700980
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100981 lockdep_assert_held(&prepare_lock);
982
Stephen Boydd6968fc2015-04-30 13:54:13 -0700983 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700984
Stephen Boydd6968fc2015-04-30 13:54:13 -0700985 if (core->parent)
986 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700987
Stephen Boydd6968fc2015-04-30 13:54:13 -0700988 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700989
990 /*
991 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
992 * & ABORT_RATE_CHANGE notifiers
993 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700994 if (core->notifier_count && msg)
995 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700996
Stephen Boydd6968fc2015-04-30 13:54:13 -0700997 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700998 __clk_recalc_rates(child, msg);
999}
1000
Stephen Boydd6968fc2015-04-30 13:54:13 -07001001static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001002{
1003 unsigned long rate;
1004
1005 clk_prepare_lock();
1006
Stephen Boydd6968fc2015-04-30 13:54:13 -07001007 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1008 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001009
Stephen Boydd6968fc2015-04-30 13:54:13 -07001010 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001011 clk_prepare_unlock();
1012
1013 return rate;
1014}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001015
Mike Turquetteb24764902012-03-15 23:11:19 -07001016/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001017 * clk_get_rate - return the rate of clk
1018 * @clk: the clk whose rate is being returned
1019 *
1020 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1021 * is set, which means a recalc_rate will be issued.
1022 * If clk is NULL then returns 0.
1023 */
1024unsigned long clk_get_rate(struct clk *clk)
1025{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001026 if (!clk)
1027 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001028
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001029 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001030}
1031EXPORT_SYMBOL_GPL(clk_get_rate);
1032
Stephen Boydd6968fc2015-04-30 13:54:13 -07001033static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001034 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001035{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001036 int i;
James Hogan4935b222013-07-29 12:24:59 +01001037
Stephen Boydd6968fc2015-04-30 13:54:13 -07001038 if (!core->parents) {
1039 core->parents = kcalloc(core->num_parents,
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001040 sizeof(struct clk *), GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001041 if (!core->parents)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001042 return -ENOMEM;
1043 }
James Hogan4935b222013-07-29 12:24:59 +01001044
1045 /*
1046 * find index of new parent clock using cached parent ptrs,
1047 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001048 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001049 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001050 for (i = 0; i < core->num_parents; i++) {
1051 if (core->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001052 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001053
Stephen Boydd6968fc2015-04-30 13:54:13 -07001054 if (core->parents[i])
Tomasz Figada0f0b22013-09-29 02:37:16 +02001055 continue;
1056
Stephen Boydd6968fc2015-04-30 13:54:13 -07001057 if (!strcmp(core->parent_names[i], parent->name)) {
1058 core->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001059 return i;
James Hogan4935b222013-07-29 12:24:59 +01001060 }
1061 }
1062
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001063 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001064}
1065
Stephen Boydd6968fc2015-04-30 13:54:13 -07001066static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001067{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001068 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001069
James Hogan903efc52013-08-29 12:10:51 +01001070 if (new_parent) {
1071 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001072 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001073 new_parent->new_child = NULL;
1074
Stephen Boydd6968fc2015-04-30 13:54:13 -07001075 hlist_add_head(&core->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001076 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001077 hlist_add_head(&core->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001078 }
James Hogan4935b222013-07-29 12:24:59 +01001079
Stephen Boydd6968fc2015-04-30 13:54:13 -07001080 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001081}
1082
Stephen Boydd6968fc2015-04-30 13:54:13 -07001083static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001084 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001085{
1086 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001087 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001088
1089 /*
1090 * Migrate prepare state between parents and prevent race with
1091 * clk_enable().
1092 *
1093 * If the clock is not prepared, then a race with
1094 * clk_enable/disable() is impossible since we already have the
1095 * prepare lock (future calls to clk_enable() need to be preceded by
1096 * a clk_prepare()).
1097 *
1098 * If the clock is prepared, migrate the prepared state to the new
1099 * parent and also protect against a race with clk_enable() by
1100 * forcing the clock and the new parent on. This ensures that all
1101 * future calls to clk_enable() are practically NOPs with respect to
1102 * hardware and software states.
1103 *
1104 * See also: Comment for clk_set_parent() below.
1105 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001106 if (core->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001107 clk_core_prepare(parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001108 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001109 clk_core_enable(parent);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001110 clk_core_enable(core);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001111 clk_enable_unlock(flags);
James Hogan4935b222013-07-29 12:24:59 +01001112 }
1113
1114 /* update the clk tree topology */
1115 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001116 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001117 clk_enable_unlock(flags);
1118
Stephen Boyd3fa22522014-01-15 10:47:22 -08001119 return old_parent;
1120}
1121
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001122static void __clk_set_parent_after(struct clk_core *core,
1123 struct clk_core *parent,
1124 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001125{
Dong Aishengd2a5d462015-04-15 22:26:36 +08001126 unsigned long flags;
1127
Stephen Boyd3fa22522014-01-15 10:47:22 -08001128 /*
1129 * Finish the migration of prepare state and undo the changes done
1130 * for preventing a race with clk_enable().
1131 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001132 if (core->prepare_count) {
Dong Aishengd2a5d462015-04-15 22:26:36 +08001133 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001134 clk_core_disable(core);
1135 clk_core_disable(old_parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001136 clk_enable_unlock(flags);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001137 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001138 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001139}
1140
Stephen Boydd6968fc2015-04-30 13:54:13 -07001141static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001142 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001143{
1144 unsigned long flags;
1145 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001146 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001147
Stephen Boydd6968fc2015-04-30 13:54:13 -07001148 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001149
Stephen Boydd6968fc2015-04-30 13:54:13 -07001150 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001151
James Hogan4935b222013-07-29 12:24:59 +01001152 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001153 if (parent && core->ops->set_parent)
1154 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001155
Stephen Boydd6968fc2015-04-30 13:54:13 -07001156 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001157
James Hogan4935b222013-07-29 12:24:59 +01001158 if (ret) {
1159 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001160 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001161 clk_enable_unlock(flags);
1162
Stephen Boydd6968fc2015-04-30 13:54:13 -07001163 if (core->prepare_count) {
Dong Aishengd2a5d462015-04-15 22:26:36 +08001164 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001165 clk_core_disable(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001166 clk_core_disable(parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001167 clk_enable_unlock(flags);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001168 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001169 }
1170 return ret;
1171 }
1172
Stephen Boydd6968fc2015-04-30 13:54:13 -07001173 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001174
James Hogan4935b222013-07-29 12:24:59 +01001175 return 0;
1176}
1177
Ulf Hanssona093bde2012-08-31 14:21:28 +02001178/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001179 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001180 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001181 * @parent_rate: the "future" rate of clk's parent
1182 *
1183 * Walks the subtree of clks starting with clk, speculating rates as it
1184 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1185 *
1186 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1187 * pre-rate change notifications and returns early if no clks in the
1188 * subtree have subscribed to the notifications. Note that if a clk does not
1189 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001190 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001191 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001192static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001193 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001194{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001195 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001196 unsigned long new_rate;
1197 int ret = NOTIFY_DONE;
1198
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001199 lockdep_assert_held(&prepare_lock);
1200
Stephen Boydd6968fc2015-04-30 13:54:13 -07001201 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001202
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001203 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001204 if (core->notifier_count)
1205 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001206
Mike Turquette86bcfa22014-02-24 16:08:41 -08001207 if (ret & NOTIFY_STOP_MASK) {
1208 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001209 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001210 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001211 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001212
Stephen Boydd6968fc2015-04-30 13:54:13 -07001213 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001214 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001215 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001216 break;
1217 }
1218
1219out:
1220 return ret;
1221}
1222
Stephen Boydd6968fc2015-04-30 13:54:13 -07001223static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001224 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001225{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001226 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001227
Stephen Boydd6968fc2015-04-30 13:54:13 -07001228 core->new_rate = new_rate;
1229 core->new_parent = new_parent;
1230 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001231 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001232 core->new_child = NULL;
1233 if (new_parent && new_parent != core->parent)
1234 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001235
Stephen Boydd6968fc2015-04-30 13:54:13 -07001236 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001237 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001238 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001239 }
1240}
1241
1242/*
1243 * calculate the new rates returning the topmost clock that has to be
1244 * changed.
1245 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001246static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001247 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001248{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001249 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001250 struct clk_core *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001251 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001252 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001253 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001254 unsigned long min_rate;
1255 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001256 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001257 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001258
Mike Turquette7452b212012-03-26 14:45:36 -07001259 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001260 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001261 return NULL;
1262
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001263 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001264 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001265 if (parent)
1266 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001267
Stephen Boydd6968fc2015-04-30 13:54:13 -07001268 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001269
James Hogan71472c02013-07-29 12:25:00 +01001270 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001271 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001272 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001273 ret = core->ops->determine_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001274 min_rate,
1275 max_rate,
1276 &best_parent_rate,
1277 &parent_hw);
1278 if (ret < 0)
1279 return NULL;
1280
1281 new_rate = ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001282 parent = parent_hw ? parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001283 } else if (core->ops->round_rate) {
1284 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001285 &best_parent_rate);
1286 if (ret < 0)
1287 return NULL;
1288
1289 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001290 if (new_rate < min_rate || new_rate > max_rate)
1291 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001292 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001293 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001294 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001295 return NULL;
1296 } else {
1297 /* pass-through clock with adjustable parent */
1298 top = clk_calc_new_rates(parent, rate);
1299 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001300 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001301 }
1302
James Hogan71472c02013-07-29 12:25:00 +01001303 /* some clocks must be gated to change parent */
1304 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001305 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001306 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001307 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001308 return NULL;
1309 }
1310
James Hogan71472c02013-07-29 12:25:00 +01001311 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001312 if (parent && core->num_parents > 1) {
1313 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001314 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001315 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001316 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001317 return NULL;
1318 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001319 }
1320
Stephen Boydd6968fc2015-04-30 13:54:13 -07001321 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001322 best_parent_rate != parent->rate)
1323 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001324
1325out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001326 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001327
1328 return top;
1329}
1330
1331/*
1332 * Notify about rate changes in a subtree. Always walk down the whole tree
1333 * so that in case of an error we can walk down the whole tree again and
1334 * abort the change.
1335 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001336static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001337 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001338{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001339 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001340 int ret = NOTIFY_DONE;
1341
Stephen Boydd6968fc2015-04-30 13:54:13 -07001342 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301343 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001344
Stephen Boydd6968fc2015-04-30 13:54:13 -07001345 if (core->notifier_count) {
1346 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001347 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001348 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001349 }
1350
Stephen Boydd6968fc2015-04-30 13:54:13 -07001351 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001352 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001353 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001354 continue;
1355 tmp_clk = clk_propagate_rate_change(child, event);
1356 if (tmp_clk)
1357 fail_clk = tmp_clk;
1358 }
1359
Stephen Boydd6968fc2015-04-30 13:54:13 -07001360 /* handle the new child who might not be in core->children yet */
1361 if (core->new_child) {
1362 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001363 if (tmp_clk)
1364 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001365 }
1366
1367 return fail_clk;
1368}
1369
1370/*
1371 * walk down a subtree and set the new rates notifying the rate
1372 * change on the way
1373 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001374static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001375{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001376 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001377 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001378 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001379 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001380 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001381 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001382
Stephen Boydd6968fc2015-04-30 13:54:13 -07001383 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001384
Stephen Boydd6968fc2015-04-30 13:54:13 -07001385 if (core->new_parent)
1386 best_parent_rate = core->new_parent->rate;
1387 else if (core->parent)
1388 best_parent_rate = core->parent->rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001389
Stephen Boydd6968fc2015-04-30 13:54:13 -07001390 if (core->new_parent && core->new_parent != core->parent) {
1391 old_parent = __clk_set_parent_before(core, core->new_parent);
1392 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001393
Stephen Boydd6968fc2015-04-30 13:54:13 -07001394 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001395 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001396 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001397 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001398 core->new_parent_index);
1399 } else if (core->ops->set_parent) {
1400 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001401 }
1402
Stephen Boydd6968fc2015-04-30 13:54:13 -07001403 trace_clk_set_parent_complete(core, core->new_parent);
1404 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001405 }
1406
Stephen Boydd6968fc2015-04-30 13:54:13 -07001407 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001408
Stephen Boydd6968fc2015-04-30 13:54:13 -07001409 if (!skip_set_rate && core->ops->set_rate)
1410 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001411
Stephen Boydd6968fc2015-04-30 13:54:13 -07001412 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001413
Stephen Boydd6968fc2015-04-30 13:54:13 -07001414 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001415
Stephen Boydd6968fc2015-04-30 13:54:13 -07001416 if (core->notifier_count && old_rate != core->rate)
1417 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001418
Tero Kristo067bb172014-08-21 16:47:45 +03001419 /*
1420 * Use safe iteration, as change_rate can actually swap parents
1421 * for certain clock types.
1422 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001423 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001424 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001425 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001426 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001427 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001428 }
1429
Stephen Boydd6968fc2015-04-30 13:54:13 -07001430 /* handle the new child who might not be in core->children yet */
1431 if (core->new_child)
1432 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001433}
1434
Stephen Boydd6968fc2015-04-30 13:54:13 -07001435static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001436 unsigned long req_rate)
1437{
1438 struct clk_core *top, *fail_clk;
1439 unsigned long rate = req_rate;
1440 int ret = 0;
1441
Stephen Boydd6968fc2015-04-30 13:54:13 -07001442 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001443 return 0;
1444
1445 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001446 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001447 return 0;
1448
Stephen Boydd6968fc2015-04-30 13:54:13 -07001449 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001450 return -EBUSY;
1451
1452 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001453 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001454 if (!top)
1455 return -EINVAL;
1456
1457 /* notify that we are about to change rates */
1458 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1459 if (fail_clk) {
1460 pr_debug("%s: failed to set %s rate\n", __func__,
1461 fail_clk->name);
1462 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1463 return -EBUSY;
1464 }
1465
1466 /* change the rates */
1467 clk_change_rate(top);
1468
Stephen Boydd6968fc2015-04-30 13:54:13 -07001469 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001470
1471 return ret;
1472}
1473
Mike Turquetteb24764902012-03-15 23:11:19 -07001474/**
1475 * clk_set_rate - specify a new rate for clk
1476 * @clk: the clk whose rate is being changed
1477 * @rate: the new rate for clk
1478 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001479 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001480 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001481 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1482 * propagate up to clk's parent; whether or not this happens depends on the
1483 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1484 * after calling .round_rate then upstream parent propagation is ignored. If
1485 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001486 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001487 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1488 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001489 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001490 * Rate changes are accomplished via tree traversal that also recalculates the
1491 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001492 *
1493 * Returns 0 on success, -EERROR otherwise.
1494 */
1495int clk_set_rate(struct clk *clk, unsigned long rate)
1496{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001497 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001498
Mike Turquette89ac8d72013-08-21 23:58:09 -07001499 if (!clk)
1500 return 0;
1501
Mike Turquetteb24764902012-03-15 23:11:19 -07001502 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001503 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001504
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001505 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001506
Mike Turquetteeab89f62013-03-28 13:59:01 -07001507 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001508
1509 return ret;
1510}
1511EXPORT_SYMBOL_GPL(clk_set_rate);
1512
1513/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001514 * clk_set_rate_range - set a rate range for a clock source
1515 * @clk: clock source
1516 * @min: desired minimum clock rate in Hz, inclusive
1517 * @max: desired maximum clock rate in Hz, inclusive
1518 *
1519 * Returns success (0) or negative errno.
1520 */
1521int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1522{
1523 int ret = 0;
1524
1525 if (!clk)
1526 return 0;
1527
1528 if (min > max) {
1529 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1530 __func__, clk->core->name, clk->dev_id, clk->con_id,
1531 min, max);
1532 return -EINVAL;
1533 }
1534
1535 clk_prepare_lock();
1536
1537 if (min != clk->min_rate || max != clk->max_rate) {
1538 clk->min_rate = min;
1539 clk->max_rate = max;
1540 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1541 }
1542
1543 clk_prepare_unlock();
1544
1545 return ret;
1546}
1547EXPORT_SYMBOL_GPL(clk_set_rate_range);
1548
1549/**
1550 * clk_set_min_rate - set a minimum clock rate for a clock source
1551 * @clk: clock source
1552 * @rate: desired minimum clock rate in Hz, inclusive
1553 *
1554 * Returns success (0) or negative errno.
1555 */
1556int clk_set_min_rate(struct clk *clk, unsigned long rate)
1557{
1558 if (!clk)
1559 return 0;
1560
1561 return clk_set_rate_range(clk, rate, clk->max_rate);
1562}
1563EXPORT_SYMBOL_GPL(clk_set_min_rate);
1564
1565/**
1566 * clk_set_max_rate - set a maximum clock rate for a clock source
1567 * @clk: clock source
1568 * @rate: desired maximum clock rate in Hz, inclusive
1569 *
1570 * Returns success (0) or negative errno.
1571 */
1572int clk_set_max_rate(struct clk *clk, unsigned long rate)
1573{
1574 if (!clk)
1575 return 0;
1576
1577 return clk_set_rate_range(clk, clk->min_rate, rate);
1578}
1579EXPORT_SYMBOL_GPL(clk_set_max_rate);
1580
1581/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001582 * clk_get_parent - return the parent of a clk
1583 * @clk: the clk whose parent gets returned
1584 *
1585 * Simply returns clk->parent. Returns NULL if clk is NULL.
1586 */
1587struct clk *clk_get_parent(struct clk *clk)
1588{
1589 struct clk *parent;
1590
Mike Turquetteeab89f62013-03-28 13:59:01 -07001591 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001592 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001593 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001594
1595 return parent;
1596}
1597EXPORT_SYMBOL_GPL(clk_get_parent);
1598
1599/*
1600 * .get_parent is mandatory for clocks with multiple possible parents. It is
1601 * optional for single-parent clocks. Always call .get_parent if it is
1602 * available and WARN if it is missing for multi-parent clocks.
1603 *
1604 * For single-parent clocks without .get_parent, first check to see if the
1605 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001606 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001607 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001608static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001609{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001610 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001611 u8 index;
1612
1613 /* handle the trivial cases */
1614
Stephen Boydd6968fc2015-04-30 13:54:13 -07001615 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -07001616 goto out;
1617
Stephen Boydd6968fc2015-04-30 13:54:13 -07001618 if (core->num_parents == 1) {
1619 if (IS_ERR_OR_NULL(core->parent))
1620 core->parent = clk_core_lookup(core->parent_names[0]);
1621 ret = core->parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001622 goto out;
1623 }
1624
Stephen Boydd6968fc2015-04-30 13:54:13 -07001625 if (!core->ops->get_parent) {
1626 WARN(!core->ops->get_parent,
Mike Turquetteb24764902012-03-15 23:11:19 -07001627 "%s: multi-parent clocks must implement .get_parent\n",
1628 __func__);
1629 goto out;
1630 };
1631
1632 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07001633 * Do our best to cache parent clocks in core->parents. This prevents
1634 * unnecessary and expensive lookups. We don't set core->parent here;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001635 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001636 */
1637
Stephen Boydd6968fc2015-04-30 13:54:13 -07001638 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001639
Stephen Boydd6968fc2015-04-30 13:54:13 -07001640 if (!core->parents)
1641 core->parents =
1642 kcalloc(core->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001643 GFP_KERNEL);
1644
Stephen Boydd6968fc2015-04-30 13:54:13 -07001645 ret = clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001646
1647out:
1648 return ret;
1649}
1650
Stephen Boydd6968fc2015-04-30 13:54:13 -07001651static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001652 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001653{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001654 clk_reparent(core, new_parent);
1655 __clk_recalc_accuracies(core);
1656 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07001657}
1658
Mike Turquetteb24764902012-03-15 23:11:19 -07001659/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001660 * clk_has_parent - check if a clock is a possible parent for another
1661 * @clk: clock source
1662 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07001663 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001664 * This function can be used in drivers that need to check that a clock can be
1665 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001666 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001667 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001668 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001669bool clk_has_parent(struct clk *clk, struct clk *parent)
1670{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001671 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001672 unsigned int i;
1673
1674 /* NULL clocks should be nops, so return success if either is NULL. */
1675 if (!clk || !parent)
1676 return true;
1677
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001678 core = clk->core;
1679 parent_core = parent->core;
1680
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001681 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001682 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001683 return true;
1684
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001685 for (i = 0; i < core->num_parents; i++)
1686 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001687 return true;
1688
1689 return false;
1690}
1691EXPORT_SYMBOL_GPL(clk_has_parent);
1692
Stephen Boydd6968fc2015-04-30 13:54:13 -07001693static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001694{
1695 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001696 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001697 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001698
Stephen Boydd6968fc2015-04-30 13:54:13 -07001699 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07001700 return 0;
1701
Mike Turquetteb24764902012-03-15 23:11:19 -07001702 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001703 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001704
Stephen Boydd6968fc2015-04-30 13:54:13 -07001705 if (core->parent == parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001706 goto out;
1707
Stephen Boydb61c43c2015-02-02 14:11:25 -08001708 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001709 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08001710 ret = -ENOSYS;
1711 goto out;
1712 }
1713
Ulf Hansson031dcc92013-04-02 23:09:38 +02001714 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001715 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001716 ret = -EBUSY;
1717 goto out;
1718 }
1719
1720 /* try finding the new parent index */
1721 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001722 p_index = clk_fetch_parent_index(core, parent);
Ulf Hansson031dcc92013-04-02 23:09:38 +02001723 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001724 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001725 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001726 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001727 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001728 goto out;
1729 }
1730 }
1731
Mike Turquetteb24764902012-03-15 23:11:19 -07001732 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001733 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001734
1735 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001736 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001737 goto out;
1738
Ulf Hansson031dcc92013-04-02 23:09:38 +02001739 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001740 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001741
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001742 /* propagate rate an accuracy recalculation accordingly */
1743 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001744 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001745 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001746 __clk_recalc_rates(core, POST_RATE_CHANGE);
1747 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001748 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001749
1750out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001751 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001752
1753 return ret;
1754}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001755
1756/**
1757 * clk_set_parent - switch the parent of a mux clk
1758 * @clk: the mux clk whose input we are switching
1759 * @parent: the new input to clk
1760 *
1761 * Re-parent clk to use parent as its new input source. If clk is in
1762 * prepared state, the clk will get enabled for the duration of this call. If
1763 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1764 * that, the reparenting is glitchy in hardware, etc), use the
1765 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1766 *
1767 * After successfully changing clk's parent clk_set_parent will update the
1768 * clk topology, sysfs topology and propagate rate recalculation via
1769 * __clk_recalc_rates.
1770 *
1771 * Returns 0 on success, -EERROR otherwise.
1772 */
1773int clk_set_parent(struct clk *clk, struct clk *parent)
1774{
1775 if (!clk)
1776 return 0;
1777
1778 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1779}
Mike Turquetteb24764902012-03-15 23:11:19 -07001780EXPORT_SYMBOL_GPL(clk_set_parent);
1781
1782/**
Mike Turquettee59c5372014-02-18 21:21:25 -08001783 * clk_set_phase - adjust the phase shift of a clock signal
1784 * @clk: clock signal source
1785 * @degrees: number of degrees the signal is shifted
1786 *
1787 * Shifts the phase of a clock signal by the specified
1788 * degrees. Returns 0 on success, -EERROR otherwise.
1789 *
1790 * This function makes no distinction about the input or reference
1791 * signal that we adjust the clock signal phase against. For example
1792 * phase locked-loop clock signal generators we may shift phase with
1793 * respect to feedback clock signal input, but for other cases the
1794 * clock phase may be shifted with respect to some other, unspecified
1795 * signal.
1796 *
1797 * Additionally the concept of phase shift does not propagate through
1798 * the clock tree hierarchy, which sets it apart from clock rates and
1799 * clock accuracy. A parent clock phase attribute does not have an
1800 * impact on the phase attribute of a child clock.
1801 */
1802int clk_set_phase(struct clk *clk, int degrees)
1803{
Stephen Boyd08b95752015-02-02 14:09:43 -08001804 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08001805
1806 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08001807 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08001808
1809 /* sanity check degrees */
1810 degrees %= 360;
1811 if (degrees < 0)
1812 degrees += 360;
1813
1814 clk_prepare_lock();
1815
Stephen Boyddfc202e2015-02-02 14:37:41 -08001816 trace_clk_set_phase(clk->core, degrees);
1817
Stephen Boyd08b95752015-02-02 14:09:43 -08001818 if (clk->core->ops->set_phase)
1819 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08001820
Stephen Boyddfc202e2015-02-02 14:37:41 -08001821 trace_clk_set_phase_complete(clk->core, degrees);
1822
Mike Turquettee59c5372014-02-18 21:21:25 -08001823 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001824 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08001825
Mike Turquettee59c5372014-02-18 21:21:25 -08001826 clk_prepare_unlock();
1827
Mike Turquettee59c5372014-02-18 21:21:25 -08001828 return ret;
1829}
Maxime Ripard9767b042015-01-20 22:23:43 +01001830EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001831
Stephen Boydd6968fc2015-04-30 13:54:13 -07001832static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08001833{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07001834 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08001835
1836 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001837 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08001838 clk_prepare_unlock();
1839
Mike Turquettee59c5372014-02-18 21:21:25 -08001840 return ret;
1841}
1842
1843/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001844 * clk_get_phase - return the phase shift of a clock signal
1845 * @clk: clock signal source
1846 *
1847 * Returns the phase shift of a clock node in degrees, otherwise returns
1848 * -EERROR.
1849 */
1850int clk_get_phase(struct clk *clk)
1851{
1852 if (!clk)
1853 return 0;
1854
1855 return clk_core_get_phase(clk->core);
1856}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001857EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07001858
1859/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08001860 * clk_is_match - check if two clk's point to the same hardware clock
1861 * @p: clk compared against q
1862 * @q: clk compared against p
1863 *
1864 * Returns true if the two struct clk pointers both point to the same hardware
1865 * clock node. Put differently, returns true if struct clk *p and struct clk *q
1866 * share the same struct clk_core object.
1867 *
1868 * Returns false otherwise. Note that two NULL clks are treated as matching.
1869 */
1870bool clk_is_match(const struct clk *p, const struct clk *q)
1871{
1872 /* trivial case: identical struct clk's or both NULL */
1873 if (p == q)
1874 return true;
1875
1876 /* true if clk->core pointers match. Avoid derefing garbage */
1877 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
1878 if (p->core == q->core)
1879 return true;
1880
1881 return false;
1882}
1883EXPORT_SYMBOL_GPL(clk_is_match);
1884
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001885/*** debugfs support ***/
1886
1887#ifdef CONFIG_DEBUG_FS
1888#include <linux/debugfs.h>
1889
1890static struct dentry *rootdir;
1891static int inited = 0;
1892static DEFINE_MUTEX(clk_debug_lock);
1893static HLIST_HEAD(clk_debug_list);
1894
1895static struct hlist_head *all_lists[] = {
1896 &clk_root_list,
1897 &clk_orphan_list,
1898 NULL,
1899};
1900
1901static struct hlist_head *orphan_list[] = {
1902 &clk_orphan_list,
1903 NULL,
1904};
1905
1906static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
1907 int level)
1908{
1909 if (!c)
1910 return;
1911
1912 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
1913 level * 3 + 1, "",
1914 30 - level * 3, c->name,
1915 c->enable_count, c->prepare_count, clk_core_get_rate(c),
1916 clk_core_get_accuracy(c), clk_core_get_phase(c));
1917}
1918
1919static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
1920 int level)
1921{
1922 struct clk_core *child;
1923
1924 if (!c)
1925 return;
1926
1927 clk_summary_show_one(s, c, level);
1928
1929 hlist_for_each_entry(child, &c->children, child_node)
1930 clk_summary_show_subtree(s, child, level + 1);
1931}
1932
1933static int clk_summary_show(struct seq_file *s, void *data)
1934{
1935 struct clk_core *c;
1936 struct hlist_head **lists = (struct hlist_head **)s->private;
1937
1938 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
1939 seq_puts(s, "----------------------------------------------------------------------------------------\n");
1940
1941 clk_prepare_lock();
1942
1943 for (; *lists; lists++)
1944 hlist_for_each_entry(c, *lists, child_node)
1945 clk_summary_show_subtree(s, c, 0);
1946
1947 clk_prepare_unlock();
1948
1949 return 0;
1950}
1951
1952
1953static int clk_summary_open(struct inode *inode, struct file *file)
1954{
1955 return single_open(file, clk_summary_show, inode->i_private);
1956}
1957
1958static const struct file_operations clk_summary_fops = {
1959 .open = clk_summary_open,
1960 .read = seq_read,
1961 .llseek = seq_lseek,
1962 .release = single_release,
1963};
1964
1965static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
1966{
1967 if (!c)
1968 return;
1969
Stefan Wahren7cb81132015-04-29 16:36:43 +00001970 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001971 seq_printf(s, "\"%s\": { ", c->name);
1972 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
1973 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00001974 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
1975 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001976 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
1977}
1978
1979static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
1980{
1981 struct clk_core *child;
1982
1983 if (!c)
1984 return;
1985
1986 clk_dump_one(s, c, level);
1987
1988 hlist_for_each_entry(child, &c->children, child_node) {
1989 seq_printf(s, ",");
1990 clk_dump_subtree(s, child, level + 1);
1991 }
1992
1993 seq_printf(s, "}");
1994}
1995
1996static int clk_dump(struct seq_file *s, void *data)
1997{
1998 struct clk_core *c;
1999 bool first_node = true;
2000 struct hlist_head **lists = (struct hlist_head **)s->private;
2001
2002 seq_printf(s, "{");
2003
2004 clk_prepare_lock();
2005
2006 for (; *lists; lists++) {
2007 hlist_for_each_entry(c, *lists, child_node) {
2008 if (!first_node)
2009 seq_puts(s, ",");
2010 first_node = false;
2011 clk_dump_subtree(s, c, 0);
2012 }
2013 }
2014
2015 clk_prepare_unlock();
2016
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002017 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002018 return 0;
2019}
2020
2021
2022static int clk_dump_open(struct inode *inode, struct file *file)
2023{
2024 return single_open(file, clk_dump, inode->i_private);
2025}
2026
2027static const struct file_operations clk_dump_fops = {
2028 .open = clk_dump_open,
2029 .read = seq_read,
2030 .llseek = seq_lseek,
2031 .release = single_release,
2032};
2033
2034static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2035{
2036 struct dentry *d;
2037 int ret = -ENOMEM;
2038
2039 if (!core || !pdentry) {
2040 ret = -EINVAL;
2041 goto out;
2042 }
2043
2044 d = debugfs_create_dir(core->name, pdentry);
2045 if (!d)
2046 goto out;
2047
2048 core->dentry = d;
2049
2050 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2051 (u32 *)&core->rate);
2052 if (!d)
2053 goto err_out;
2054
2055 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2056 (u32 *)&core->accuracy);
2057 if (!d)
2058 goto err_out;
2059
2060 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2061 (u32 *)&core->phase);
2062 if (!d)
2063 goto err_out;
2064
2065 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2066 (u32 *)&core->flags);
2067 if (!d)
2068 goto err_out;
2069
2070 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2071 (u32 *)&core->prepare_count);
2072 if (!d)
2073 goto err_out;
2074
2075 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2076 (u32 *)&core->enable_count);
2077 if (!d)
2078 goto err_out;
2079
2080 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2081 (u32 *)&core->notifier_count);
2082 if (!d)
2083 goto err_out;
2084
2085 if (core->ops->debug_init) {
2086 ret = core->ops->debug_init(core->hw, core->dentry);
2087 if (ret)
2088 goto err_out;
2089 }
2090
2091 ret = 0;
2092 goto out;
2093
2094err_out:
2095 debugfs_remove_recursive(core->dentry);
2096 core->dentry = NULL;
2097out:
2098 return ret;
2099}
2100
2101/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002102 * clk_debug_register - add a clk node to the debugfs clk directory
2103 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002104 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002105 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2106 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002107 * will be created lazily by clk_debug_init as part of a late_initcall.
2108 */
2109static int clk_debug_register(struct clk_core *core)
2110{
2111 int ret = 0;
2112
2113 mutex_lock(&clk_debug_lock);
2114 hlist_add_head(&core->debug_node, &clk_debug_list);
2115
2116 if (!inited)
2117 goto unlock;
2118
2119 ret = clk_debug_create_one(core, rootdir);
2120unlock:
2121 mutex_unlock(&clk_debug_lock);
2122
2123 return ret;
2124}
2125
2126 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002127 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2128 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002129 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002130 * Dynamically removes a clk and all its child nodes from the
2131 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002132 * clk_debug_register in __clk_init.
2133 */
2134static void clk_debug_unregister(struct clk_core *core)
2135{
2136 mutex_lock(&clk_debug_lock);
2137 hlist_del_init(&core->debug_node);
2138 debugfs_remove_recursive(core->dentry);
2139 core->dentry = NULL;
2140 mutex_unlock(&clk_debug_lock);
2141}
2142
2143struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2144 void *data, const struct file_operations *fops)
2145{
2146 struct dentry *d = NULL;
2147
2148 if (hw->core->dentry)
2149 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2150 fops);
2151
2152 return d;
2153}
2154EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2155
2156/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002157 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002158 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002159 * clks are often initialized very early during boot before memory can be
2160 * dynamically allocated and well before debugfs is setup. This function
2161 * populates the debugfs clk directory once at boot-time when we know that
2162 * debugfs is setup. It should only be called once at boot-time, all other clks
2163 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002164 */
2165static int __init clk_debug_init(void)
2166{
2167 struct clk_core *core;
2168 struct dentry *d;
2169
2170 rootdir = debugfs_create_dir("clk", NULL);
2171
2172 if (!rootdir)
2173 return -ENOMEM;
2174
2175 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2176 &clk_summary_fops);
2177 if (!d)
2178 return -ENOMEM;
2179
2180 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2181 &clk_dump_fops);
2182 if (!d)
2183 return -ENOMEM;
2184
2185 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2186 &orphan_list, &clk_summary_fops);
2187 if (!d)
2188 return -ENOMEM;
2189
2190 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2191 &orphan_list, &clk_dump_fops);
2192 if (!d)
2193 return -ENOMEM;
2194
2195 mutex_lock(&clk_debug_lock);
2196 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2197 clk_debug_create_one(core, rootdir);
2198
2199 inited = 1;
2200 mutex_unlock(&clk_debug_lock);
2201
2202 return 0;
2203}
2204late_initcall(clk_debug_init);
2205#else
2206static inline int clk_debug_register(struct clk_core *core) { return 0; }
2207static inline void clk_debug_reparent(struct clk_core *core,
2208 struct clk_core *new_parent)
2209{
2210}
2211static inline void clk_debug_unregister(struct clk_core *core)
2212{
2213}
2214#endif
2215
Michael Turquette3d3801e2015-02-25 09:11:01 -08002216/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002217 * __clk_init - initialize the data structures in a struct clk
2218 * @dev: device initializing this clk, placeholder for now
2219 * @clk: clk being initialized
2220 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002221 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002222 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002223 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002224static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002225{
Mike Turquetted1302a32012-03-29 14:30:40 -07002226 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002227 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002228 struct hlist_node *tmp2;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002229 struct clk_core *core;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002230 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002231
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002232 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002233 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002234
Stephen Boydd6968fc2015-04-30 13:54:13 -07002235 core = clk_user->core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002236
Mike Turquetteeab89f62013-03-28 13:59:01 -07002237 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002238
2239 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002240 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002241 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002242 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002243 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002244 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002245 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002246
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002247 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002248 if (core->ops->set_rate &&
2249 !((core->ops->round_rate || core->ops->determine_rate) &&
2250 core->ops->recalc_rate)) {
James Hogan71472c02013-07-29 12:25:00 +01002251 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002252 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002253 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002254 goto out;
2255 }
2256
Stephen Boydd6968fc2015-04-30 13:54:13 -07002257 if (core->ops->set_parent && !core->ops->get_parent) {
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002258 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002259 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002260 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002261 goto out;
2262 }
2263
Stephen Boydd6968fc2015-04-30 13:54:13 -07002264 if (core->ops->set_rate_and_parent &&
2265 !(core->ops->set_parent && core->ops->set_rate)) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002266 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002267 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002268 ret = -EINVAL;
2269 goto out;
2270 }
2271
Mike Turquetteb24764902012-03-15 23:11:19 -07002272 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002273 for (i = 0; i < core->num_parents; i++)
2274 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002275 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002276 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002277
2278 /*
2279 * Allocate an array of struct clk *'s to avoid unnecessary string
2280 * look-ups of clk's possible parents. This can fail for clocks passed
Stephen Boydd6968fc2015-04-30 13:54:13 -07002281 * in to clk_init during early boot; thus any access to core->parents[]
Mike Turquetteb24764902012-03-15 23:11:19 -07002282 * must always check for a NULL pointer and try to populate it if
2283 * necessary.
2284 *
Stephen Boydd6968fc2015-04-30 13:54:13 -07002285 * If core->parents is not NULL we skip this entire block. This allows
2286 * for clock drivers to statically initialize core->parents.
Mike Turquetteb24764902012-03-15 23:11:19 -07002287 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002288 if (core->num_parents > 1 && !core->parents) {
2289 core->parents = kcalloc(core->num_parents, sizeof(struct clk *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002290 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002291 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002292 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002293 * clk_init'd; thus any access to clk->parents[] must check
2294 * for a NULL pointer. We can always perform lazy lookups for
2295 * missing parents later on.
2296 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002297 if (core->parents)
2298 for (i = 0; i < core->num_parents; i++)
2299 core->parents[i] =
2300 clk_core_lookup(core->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002301 }
2302
Stephen Boydd6968fc2015-04-30 13:54:13 -07002303 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002304
2305 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07002306 * Populate core->parent if parent has already been __clk_init'd. If
Mike Turquetteb24764902012-03-15 23:11:19 -07002307 * parent has not yet been __clk_init'd then place clk in the orphan
2308 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2309 * clk list.
2310 *
2311 * Every time a new clk is clk_init'd then we walk the list of orphan
2312 * clocks and re-parent any that are children of the clock currently
2313 * being clk_init'd.
2314 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002315 if (core->parent)
2316 hlist_add_head(&core->child_node,
2317 &core->parent->children);
2318 else if (core->flags & CLK_IS_ROOT)
2319 hlist_add_head(&core->child_node, &clk_root_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002320 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002321 hlist_add_head(&core->child_node, &clk_orphan_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002322
2323 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002324 * Set clk's accuracy. The preferred method is to use
2325 * .recalc_accuracy. For simple clocks and lazy developers the default
2326 * fallback is to use the parent's accuracy. If a clock doesn't have a
2327 * parent (or is orphaned) then accuracy is set to zero (perfect
2328 * clock).
2329 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002330 if (core->ops->recalc_accuracy)
2331 core->accuracy = core->ops->recalc_accuracy(core->hw,
2332 __clk_get_accuracy(core->parent));
2333 else if (core->parent)
2334 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002335 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002336 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002337
2338 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002339 * Set clk's phase.
2340 * Since a phase is by definition relative to its parent, just
2341 * query the current clock phase, or just assume it's in phase.
2342 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002343 if (core->ops->get_phase)
2344 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002345 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002346 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002347
2348 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002349 * Set clk's rate. The preferred method is to use .recalc_rate. For
2350 * simple clocks and lazy developers the default fallback is to use the
2351 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2352 * then rate is set to zero.
2353 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002354 if (core->ops->recalc_rate)
2355 rate = core->ops->recalc_rate(core->hw,
2356 clk_core_get_rate_nolock(core->parent));
2357 else if (core->parent)
2358 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002359 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002360 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002361 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002362
2363 /*
2364 * walk the list of orphan clocks and reparent any that are children of
2365 * this clock
2366 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002367 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002368 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002369 i = orphan->ops->get_parent(orphan->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002370 if (!strcmp(core->name, orphan->parent_names[i]))
2371 clk_core_reparent(orphan, core);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002372 continue;
2373 }
2374
Mike Turquetteb24764902012-03-15 23:11:19 -07002375 for (i = 0; i < orphan->num_parents; i++)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002376 if (!strcmp(core->name, orphan->parent_names[i])) {
2377 clk_core_reparent(orphan, core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002378 break;
2379 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002380 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002381
2382 /*
2383 * optional platform-specific magic
2384 *
2385 * The .init callback is not used by any of the basic clock types, but
2386 * exists for weird hardware that must perform initialization magic.
2387 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002388 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002389 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002390 if (core->ops->init)
2391 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002392
Stephen Boydd6968fc2015-04-30 13:54:13 -07002393 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002394out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002395 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002396
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002397 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002398 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002399
Mike Turquetted1302a32012-03-29 14:30:40 -07002400 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002401}
2402
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002403struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2404 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002405{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002406 struct clk *clk;
2407
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002408 /* This is to allow this function to be chained to others */
2409 if (!hw || IS_ERR(hw))
2410 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002411
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002412 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2413 if (!clk)
2414 return ERR_PTR(-ENOMEM);
2415
2416 clk->core = hw->core;
2417 clk->dev_id = dev_id;
2418 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002419 clk->max_rate = ULONG_MAX;
2420
2421 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002422 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002423 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002424
2425 return clk;
2426}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002427
Stephen Boyd73e0e492015-02-06 11:42:43 -08002428void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002429{
2430 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002431 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002432 clk_prepare_unlock();
2433
2434 kfree(clk);
2435}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002436
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002437/**
2438 * clk_register - allocate a new clock, register it and return an opaque cookie
2439 * @dev: device that is registering this clock
2440 * @hw: link to hardware-specific clock data
2441 *
2442 * clk_register is the primary interface for populating the clock tree with new
2443 * clock nodes. It returns a pointer to the newly allocated struct clk which
2444 * cannot be dereferenced by driver code but may be used in conjuction with the
2445 * rest of the clock API. In the event of an error clk_register will return an
2446 * error code; drivers must test for an error code after calling clk_register.
2447 */
2448struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002449{
Mike Turquetted1302a32012-03-29 14:30:40 -07002450 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002451 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002452
Stephen Boydd6968fc2015-04-30 13:54:13 -07002453 core = kzalloc(sizeof(*core), GFP_KERNEL);
2454 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002455 ret = -ENOMEM;
2456 goto fail_out;
2457 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002458
Stephen Boydd6968fc2015-04-30 13:54:13 -07002459 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2460 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002461 ret = -ENOMEM;
2462 goto fail_name;
2463 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002464 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002465 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002466 core->owner = dev->driver->owner;
2467 core->hw = hw;
2468 core->flags = hw->init->flags;
2469 core->num_parents = hw->init->num_parents;
2470 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002471
Mike Turquetted1302a32012-03-29 14:30:40 -07002472 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002473 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002474 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002475
Stephen Boydd6968fc2015-04-30 13:54:13 -07002476 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002477 ret = -ENOMEM;
2478 goto fail_parent_names;
2479 }
2480
2481
2482 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002483 for (i = 0; i < core->num_parents; i++) {
2484 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002485 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002486 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002487 ret = -ENOMEM;
2488 goto fail_parent_names_copy;
2489 }
2490 }
2491
Stephen Boydd6968fc2015-04-30 13:54:13 -07002492 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002493
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002494 hw->clk = __clk_create_clk(hw, NULL, NULL);
2495 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002496 ret = PTR_ERR(hw->clk);
2497 goto fail_parent_names_copy;
2498 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002499
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002500 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002501 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002502 return hw->clk;
2503
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002504 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002505 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002506
2507fail_parent_names_copy:
2508 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002509 kfree_const(core->parent_names[i]);
2510 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07002511fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002512 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002513fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002514 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002515fail_out:
2516 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002517}
2518EXPORT_SYMBOL_GPL(clk_register);
2519
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002520/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002521static void __clk_release(struct kref *ref)
2522{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002523 struct clk_core *core = container_of(ref, struct clk_core, ref);
2524 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002525
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002526 lockdep_assert_held(&prepare_lock);
2527
Stephen Boydd6968fc2015-04-30 13:54:13 -07002528 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002529 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002530 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002531
Stephen Boydd6968fc2015-04-30 13:54:13 -07002532 kfree(core->parent_names);
2533 kfree_const(core->name);
2534 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002535}
2536
2537/*
2538 * Empty clk_ops for unregistered clocks. These are used temporarily
2539 * after clk_unregister() was called on a clock and until last clock
2540 * consumer calls clk_put() and the struct clk object is freed.
2541 */
2542static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2543{
2544 return -ENXIO;
2545}
2546
2547static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2548{
2549 WARN_ON_ONCE(1);
2550}
2551
2552static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2553 unsigned long parent_rate)
2554{
2555 return -ENXIO;
2556}
2557
2558static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2559{
2560 return -ENXIO;
2561}
2562
2563static const struct clk_ops clk_nodrv_ops = {
2564 .enable = clk_nodrv_prepare_enable,
2565 .disable = clk_nodrv_disable_unprepare,
2566 .prepare = clk_nodrv_prepare_enable,
2567 .unprepare = clk_nodrv_disable_unprepare,
2568 .set_rate = clk_nodrv_set_rate,
2569 .set_parent = clk_nodrv_set_parent,
2570};
2571
Mark Brown1df5c932012-04-18 09:07:12 +01002572/**
2573 * clk_unregister - unregister a currently registered clock
2574 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002575 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002576void clk_unregister(struct clk *clk)
2577{
2578 unsigned long flags;
2579
Stephen Boyd6314b672014-09-04 23:37:49 -07002580 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2581 return;
2582
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002583 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002584
2585 clk_prepare_lock();
2586
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002587 if (clk->core->ops == &clk_nodrv_ops) {
2588 pr_err("%s: unregistered clock: %s\n", __func__,
2589 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002590 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002591 }
2592 /*
2593 * Assign empty clock ops for consumers that might still hold
2594 * a reference to this clock.
2595 */
2596 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002597 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002598 clk_enable_unlock(flags);
2599
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002600 if (!hlist_empty(&clk->core->children)) {
2601 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002602 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002603
2604 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002605 hlist_for_each_entry_safe(child, t, &clk->core->children,
2606 child_node)
2607 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002608 }
2609
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002610 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002611
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002612 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002613 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002614 __func__, clk->core->name);
2615 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002616
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002617 clk_prepare_unlock();
2618}
Mark Brown1df5c932012-04-18 09:07:12 +01002619EXPORT_SYMBOL_GPL(clk_unregister);
2620
Stephen Boyd46c87732012-09-24 13:38:04 -07002621static void devm_clk_release(struct device *dev, void *res)
2622{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002623 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002624}
2625
2626/**
2627 * devm_clk_register - resource managed clk_register()
2628 * @dev: device that is registering this clock
2629 * @hw: link to hardware-specific clock data
2630 *
2631 * Managed clk_register(). Clocks returned from this function are
2632 * automatically clk_unregister()ed on driver detach. See clk_register() for
2633 * more information.
2634 */
2635struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2636{
2637 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002638 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002639
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002640 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2641 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002642 return ERR_PTR(-ENOMEM);
2643
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002644 clk = clk_register(dev, hw);
2645 if (!IS_ERR(clk)) {
2646 *clkp = clk;
2647 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002648 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002649 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002650 }
2651
2652 return clk;
2653}
2654EXPORT_SYMBOL_GPL(devm_clk_register);
2655
2656static int devm_clk_match(struct device *dev, void *res, void *data)
2657{
2658 struct clk *c = res;
2659 if (WARN_ON(!c))
2660 return 0;
2661 return c == data;
2662}
2663
2664/**
2665 * devm_clk_unregister - resource managed clk_unregister()
2666 * @clk: clock to unregister
2667 *
2668 * Deallocate a clock allocated with devm_clk_register(). Normally
2669 * this function will not need to be called and the resource management
2670 * code will ensure that the resource is freed.
2671 */
2672void devm_clk_unregister(struct device *dev, struct clk *clk)
2673{
2674 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2675}
2676EXPORT_SYMBOL_GPL(devm_clk_unregister);
2677
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002678/*
2679 * clkdev helpers
2680 */
2681int __clk_get(struct clk *clk)
2682{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002683 struct clk_core *core = !clk ? NULL : clk->core;
2684
2685 if (core) {
2686 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002687 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002688
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002689 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002690 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002691 return 1;
2692}
2693
2694void __clk_put(struct clk *clk)
2695{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002696 struct module *owner;
2697
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002698 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002699 return;
2700
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002701 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002702
Stephen Boyd50595f82015-02-06 11:42:44 -08002703 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002704 if (clk->min_rate > clk->core->req_rate ||
2705 clk->max_rate < clk->core->req_rate)
2706 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2707
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002708 owner = clk->core->owner;
2709 kref_put(&clk->core->ref, __clk_release);
2710
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002711 clk_prepare_unlock();
2712
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002713 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002714
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002715 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002716}
2717
Mike Turquetteb24764902012-03-15 23:11:19 -07002718/*** clk rate change notifiers ***/
2719
2720/**
2721 * clk_notifier_register - add a clk rate change notifier
2722 * @clk: struct clk * to watch
2723 * @nb: struct notifier_block * with callback info
2724 *
2725 * Request notification when clk's rate changes. This uses an SRCU
2726 * notifier because we want it to block and notifier unregistrations are
2727 * uncommon. The callbacks associated with the notifier must not
2728 * re-enter into the clk framework by calling any top-level clk APIs;
2729 * this will cause a nested prepare_lock mutex.
2730 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002731 * In all notification cases cases (pre, post and abort rate change) the
2732 * original clock rate is passed to the callback via struct
2733 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002734 * clk_notifier_data.new_rate.
2735 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002736 * clk_notifier_register() must be called from non-atomic context.
2737 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2738 * allocation failure; otherwise, passes along the return value of
2739 * srcu_notifier_chain_register().
2740 */
2741int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2742{
2743 struct clk_notifier *cn;
2744 int ret = -ENOMEM;
2745
2746 if (!clk || !nb)
2747 return -EINVAL;
2748
Mike Turquetteeab89f62013-03-28 13:59:01 -07002749 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002750
2751 /* search the list of notifiers for this clk */
2752 list_for_each_entry(cn, &clk_notifier_list, node)
2753 if (cn->clk == clk)
2754 break;
2755
2756 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2757 if (cn->clk != clk) {
2758 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2759 if (!cn)
2760 goto out;
2761
2762 cn->clk = clk;
2763 srcu_init_notifier_head(&cn->notifier_head);
2764
2765 list_add(&cn->node, &clk_notifier_list);
2766 }
2767
2768 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2769
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002770 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002771
2772out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002773 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002774
2775 return ret;
2776}
2777EXPORT_SYMBOL_GPL(clk_notifier_register);
2778
2779/**
2780 * clk_notifier_unregister - remove a clk rate change notifier
2781 * @clk: struct clk *
2782 * @nb: struct notifier_block * with callback info
2783 *
2784 * Request no further notification for changes to 'clk' and frees memory
2785 * allocated in clk_notifier_register.
2786 *
2787 * Returns -EINVAL if called with null arguments; otherwise, passes
2788 * along the return value of srcu_notifier_chain_unregister().
2789 */
2790int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2791{
2792 struct clk_notifier *cn = NULL;
2793 int ret = -EINVAL;
2794
2795 if (!clk || !nb)
2796 return -EINVAL;
2797
Mike Turquetteeab89f62013-03-28 13:59:01 -07002798 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002799
2800 list_for_each_entry(cn, &clk_notifier_list, node)
2801 if (cn->clk == clk)
2802 break;
2803
2804 if (cn->clk == clk) {
2805 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2806
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002807 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002808
2809 /* XXX the notifier code should handle this better */
2810 if (!cn->notifier_head.head) {
2811 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002812 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002813 kfree(cn);
2814 }
2815
2816 } else {
2817 ret = -ENOENT;
2818 }
2819
Mike Turquetteeab89f62013-03-28 13:59:01 -07002820 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002821
2822 return ret;
2823}
2824EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002825
2826#ifdef CONFIG_OF
2827/**
2828 * struct of_clk_provider - Clock provider registration structure
2829 * @link: Entry in global list of clock providers
2830 * @node: Pointer to device tree node of clock provider
2831 * @get: Get clock callback. Returns NULL or a struct clk for the
2832 * given clock specifier
2833 * @data: context pointer to be passed into @get callback
2834 */
2835struct of_clk_provider {
2836 struct list_head link;
2837
2838 struct device_node *node;
2839 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2840 void *data;
2841};
2842
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302843static const struct of_device_id __clk_of_table_sentinel
2844 __used __section(__clk_of_table_end);
2845
Grant Likely766e6a42012-04-09 14:50:06 -05002846static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002847static DEFINE_MUTEX(of_clk_mutex);
2848
Grant Likely766e6a42012-04-09 14:50:06 -05002849struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2850 void *data)
2851{
2852 return data;
2853}
2854EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2855
Shawn Guo494bfec2012-08-22 21:36:27 +08002856struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2857{
2858 struct clk_onecell_data *clk_data = data;
2859 unsigned int idx = clkspec->args[0];
2860
2861 if (idx >= clk_data->clk_num) {
2862 pr_err("%s: invalid clock index %d\n", __func__, idx);
2863 return ERR_PTR(-EINVAL);
2864 }
2865
2866 return clk_data->clks[idx];
2867}
2868EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2869
Grant Likely766e6a42012-04-09 14:50:06 -05002870/**
2871 * of_clk_add_provider() - Register a clock provider for a node
2872 * @np: Device node pointer associated with clock provider
2873 * @clk_src_get: callback for decoding clock
2874 * @data: context pointer for @clk_src_get callback.
2875 */
2876int of_clk_add_provider(struct device_node *np,
2877 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2878 void *data),
2879 void *data)
2880{
2881 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002882 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002883
2884 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2885 if (!cp)
2886 return -ENOMEM;
2887
2888 cp->node = of_node_get(np);
2889 cp->data = data;
2890 cp->get = clk_src_get;
2891
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002892 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002893 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002894 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002895 pr_debug("Added clock from %s\n", np->full_name);
2896
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002897 ret = of_clk_set_defaults(np, true);
2898 if (ret < 0)
2899 of_clk_del_provider(np);
2900
2901 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002902}
2903EXPORT_SYMBOL_GPL(of_clk_add_provider);
2904
2905/**
2906 * of_clk_del_provider() - Remove a previously registered clock provider
2907 * @np: Device node pointer associated with clock provider
2908 */
2909void of_clk_del_provider(struct device_node *np)
2910{
2911 struct of_clk_provider *cp;
2912
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002913 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002914 list_for_each_entry(cp, &of_clk_providers, link) {
2915 if (cp->node == np) {
2916 list_del(&cp->link);
2917 of_node_put(cp->node);
2918 kfree(cp);
2919 break;
2920 }
2921 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002922 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002923}
2924EXPORT_SYMBOL_GPL(of_clk_del_provider);
2925
Stephen Boyd73e0e492015-02-06 11:42:43 -08002926struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
2927 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05002928{
2929 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002930 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002931
Stephen Boyd306c3422015-02-05 15:39:11 -08002932 if (!clkspec)
2933 return ERR_PTR(-EINVAL);
2934
Grant Likely766e6a42012-04-09 14:50:06 -05002935 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08002936 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002937 list_for_each_entry(provider, &of_clk_providers, link) {
2938 if (provider->node == clkspec->np)
2939 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002940 if (!IS_ERR(clk)) {
2941 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
2942 con_id);
2943
2944 if (!IS_ERR(clk) && !__clk_get(clk)) {
2945 __clk_free_clk(clk);
2946 clk = ERR_PTR(-ENOENT);
2947 }
2948
Grant Likely766e6a42012-04-09 14:50:06 -05002949 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08002950 }
Grant Likely766e6a42012-04-09 14:50:06 -05002951 }
Stephen Boyd306c3422015-02-05 15:39:11 -08002952 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002953
2954 return clk;
2955}
2956
Stephen Boyd306c3422015-02-05 15:39:11 -08002957/**
2958 * of_clk_get_from_provider() - Lookup a clock from a clock provider
2959 * @clkspec: pointer to a clock specifier data structure
2960 *
2961 * This function looks up a struct clk from the registered list of clock
2962 * providers, an input is a clock specifier data structure as returned
2963 * from the of_parse_phandle_with_args() function call.
2964 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002965struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2966{
Stephen Boyd306c3422015-02-05 15:39:11 -08002967 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05002968}
2969
Mike Turquettef6102742013-10-07 23:12:13 -07002970int of_clk_get_parent_count(struct device_node *np)
2971{
2972 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2973}
2974EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2975
Grant Likely766e6a42012-04-09 14:50:06 -05002976const char *of_clk_get_parent_name(struct device_node *np, int index)
2977{
2978 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002979 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05002980 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002981 const __be32 *vp;
2982 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05002983 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002984 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05002985
2986 if (index < 0)
2987 return NULL;
2988
2989 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2990 &clkspec);
2991 if (rc)
2992 return NULL;
2993
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002994 index = clkspec.args_count ? clkspec.args[0] : 0;
2995 count = 0;
2996
2997 /* if there is an indices property, use it to transfer the index
2998 * specified into an array offset for the clock-output-names property.
2999 */
3000 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3001 if (index == pv) {
3002 index = count;
3003 break;
3004 }
3005 count++;
3006 }
3007
Grant Likely766e6a42012-04-09 14:50:06 -05003008 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003009 index,
Grant Likely766e6a42012-04-09 14:50:06 -05003010 &clk_name) < 0)
3011 clk_name = clkspec.np->name;
3012
3013 of_node_put(clkspec.np);
3014 return clk_name;
3015}
3016EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3017
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003018struct clock_provider {
3019 of_clk_init_cb_t clk_init_cb;
3020 struct device_node *np;
3021 struct list_head node;
3022};
3023
3024static LIST_HEAD(clk_provider_list);
3025
3026/*
3027 * This function looks for a parent clock. If there is one, then it
3028 * checks that the provider for this parent clock was initialized, in
3029 * this case the parent clock will be ready.
3030 */
3031static int parent_ready(struct device_node *np)
3032{
3033 int i = 0;
3034
3035 while (true) {
3036 struct clk *clk = of_clk_get(np, i);
3037
3038 /* this parent is ready we can check the next one */
3039 if (!IS_ERR(clk)) {
3040 clk_put(clk);
3041 i++;
3042 continue;
3043 }
3044
3045 /* at least one parent is not ready, we exit now */
3046 if (PTR_ERR(clk) == -EPROBE_DEFER)
3047 return 0;
3048
3049 /*
3050 * Here we make assumption that the device tree is
3051 * written correctly. So an error means that there is
3052 * no more parent. As we didn't exit yet, then the
3053 * previous parent are ready. If there is no clock
3054 * parent, no need to wait for them, then we can
3055 * consider their absence as being ready
3056 */
3057 return 1;
3058 }
3059}
3060
Grant Likely766e6a42012-04-09 14:50:06 -05003061/**
3062 * of_clk_init() - Scan and init clock providers from the DT
3063 * @matches: array of compatible values and init functions for providers.
3064 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003065 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003066 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003067 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003068 */
3069void __init of_clk_init(const struct of_device_id *matches)
3070{
Alex Elder7f7ed582013-08-22 11:31:31 -05003071 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003072 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003073 struct clock_provider *clk_provider, *next;
3074 bool is_init_done;
3075 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05003076
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303077 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003078 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303079
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003080 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003081 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003082 struct clock_provider *parent =
3083 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
3084
3085 parent->clk_init_cb = match->data;
3086 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003087 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003088 }
3089
3090 while (!list_empty(&clk_provider_list)) {
3091 is_init_done = false;
3092 list_for_each_entry_safe(clk_provider, next,
3093 &clk_provider_list, node) {
3094 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003095
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003096 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003097 of_clk_set_defaults(clk_provider->np, true);
3098
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003099 list_del(&clk_provider->node);
3100 kfree(clk_provider);
3101 is_init_done = true;
3102 }
3103 }
3104
3105 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003106 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003107 * remaining providers during the last loop, so now we
3108 * initialize all the remaining ones unconditionally
3109 * in case the clock parent was not mandatory
3110 */
3111 if (!is_init_done)
3112 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003113 }
3114}
3115#endif