blob: 89e531a3f3844365eef2bde3693080ebc7325fdc [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
Stephen Boyd3c373112015-06-19 15:00:46 -070012#include <linux/clk.h>
Michael Turquetteb09d6d92015-01-29 14:22:50 -080013#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020014#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070015#include <linux/module.h>
16#include <linux/mutex.h>
17#include <linux/spinlock.h>
18#include <linux/err.h>
19#include <linux/list.h>
20#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050021#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070022#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053023#include <linux/init.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070024#include <linux/sched.h>
Stephen Boyd562ef0b2015-05-01 12:16:14 -070025#include <linux/clkdev.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070026
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020027#include "clk.h"
28
Mike Turquetteb24764902012-03-15 23:11:19 -070029static DEFINE_SPINLOCK(enable_lock);
30static DEFINE_MUTEX(prepare_lock);
31
Mike Turquette533ddeb2013-03-28 13:59:02 -070032static struct task_struct *prepare_owner;
33static struct task_struct *enable_owner;
34
35static int prepare_refcnt;
36static int enable_refcnt;
37
Mike Turquetteb24764902012-03-15 23:11:19 -070038static HLIST_HEAD(clk_root_list);
39static HLIST_HEAD(clk_orphan_list);
40static LIST_HEAD(clk_notifier_list);
41
Michael Turquetteb09d6d92015-01-29 14:22:50 -080042/*** private data structures ***/
43
44struct clk_core {
45 const char *name;
46 const struct clk_ops *ops;
47 struct clk_hw *hw;
48 struct module *owner;
49 struct clk_core *parent;
50 const char **parent_names;
51 struct clk_core **parents;
52 u8 num_parents;
53 u8 new_parent_index;
54 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010055 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080056 unsigned long new_rate;
57 struct clk_core *new_parent;
58 struct clk_core *new_child;
59 unsigned long flags;
60 unsigned int enable_count;
61 unsigned int prepare_count;
62 unsigned long accuracy;
63 int phase;
64 struct hlist_head children;
65 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010066 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080067 unsigned int notifier_count;
68#ifdef CONFIG_DEBUG_FS
69 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020070 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080071#endif
72 struct kref ref;
73};
74
Stephen Boyddfc202e2015-02-02 14:37:41 -080075#define CREATE_TRACE_POINTS
76#include <trace/events/clk.h>
77
Michael Turquetteb09d6d92015-01-29 14:22:50 -080078struct clk {
79 struct clk_core *core;
80 const char *dev_id;
81 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010082 unsigned long min_rate;
83 unsigned long max_rate;
Stephen Boyd50595f82015-02-06 11:42:44 -080084 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080085};
86
Mike Turquetteeab89f62013-03-28 13:59:01 -070087/*** locking ***/
88static void clk_prepare_lock(void)
89{
Mike Turquette533ddeb2013-03-28 13:59:02 -070090 if (!mutex_trylock(&prepare_lock)) {
91 if (prepare_owner == current) {
92 prepare_refcnt++;
93 return;
94 }
95 mutex_lock(&prepare_lock);
96 }
97 WARN_ON_ONCE(prepare_owner != NULL);
98 WARN_ON_ONCE(prepare_refcnt != 0);
99 prepare_owner = current;
100 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700101}
102
103static void clk_prepare_unlock(void)
104{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700105 WARN_ON_ONCE(prepare_owner != current);
106 WARN_ON_ONCE(prepare_refcnt == 0);
107
108 if (--prepare_refcnt)
109 return;
110 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700111 mutex_unlock(&prepare_lock);
112}
113
114static unsigned long clk_enable_lock(void)
115{
116 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700117
118 if (!spin_trylock_irqsave(&enable_lock, flags)) {
119 if (enable_owner == current) {
120 enable_refcnt++;
121 return flags;
122 }
123 spin_lock_irqsave(&enable_lock, flags);
124 }
125 WARN_ON_ONCE(enable_owner != NULL);
126 WARN_ON_ONCE(enable_refcnt != 0);
127 enable_owner = current;
128 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700129 return flags;
130}
131
132static void clk_enable_unlock(unsigned long flags)
133{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700134 WARN_ON_ONCE(enable_owner != current);
135 WARN_ON_ONCE(enable_refcnt == 0);
136
137 if (--enable_refcnt)
138 return;
139 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700140 spin_unlock_irqrestore(&enable_lock, flags);
141}
142
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700143static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530144{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700145 /*
146 * .is_prepared is optional for clocks that can prepare
147 * fall back to software usage counter if it is missing
148 */
149 if (!core->ops->is_prepared)
150 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530151
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700152 return core->ops->is_prepared(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530153}
154
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700155static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530156{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700157 /*
158 * .is_enabled is only mandatory for clocks that gate
159 * fall back to software usage counter if .is_enabled is missing
160 */
161 if (!core->ops->is_enabled)
162 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530163
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700164 return core->ops->is_enabled(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530165}
166
Stephen Boydd6968fc2015-04-30 13:54:13 -0700167static void clk_unprepare_unused_subtree(struct clk_core *core)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100168{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100169 struct clk_core *child;
Ulf Hansson1c155b32013-03-12 20:26:03 +0100170
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100171 lockdep_assert_held(&prepare_lock);
172
Stephen Boydd6968fc2015-04-30 13:54:13 -0700173 hlist_for_each_entry(child, &core->children, child_node)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100174 clk_unprepare_unused_subtree(child);
175
Stephen Boydd6968fc2015-04-30 13:54:13 -0700176 if (core->prepare_count)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100177 return;
178
Stephen Boydd6968fc2015-04-30 13:54:13 -0700179 if (core->flags & CLK_IGNORE_UNUSED)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100180 return;
181
Stephen Boydd6968fc2015-04-30 13:54:13 -0700182 if (clk_core_is_prepared(core)) {
183 trace_clk_unprepare(core);
184 if (core->ops->unprepare_unused)
185 core->ops->unprepare_unused(core->hw);
186 else if (core->ops->unprepare)
187 core->ops->unprepare(core->hw);
188 trace_clk_unprepare_complete(core);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100189 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100190}
191
Stephen Boydd6968fc2015-04-30 13:54:13 -0700192static void clk_disable_unused_subtree(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700193{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100194 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700195 unsigned long flags;
196
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100197 lockdep_assert_held(&prepare_lock);
198
Stephen Boydd6968fc2015-04-30 13:54:13 -0700199 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700200 clk_disable_unused_subtree(child);
201
Mike Turquetteeab89f62013-03-28 13:59:01 -0700202 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700203
Stephen Boydd6968fc2015-04-30 13:54:13 -0700204 if (core->enable_count)
Mike Turquetteb24764902012-03-15 23:11:19 -0700205 goto unlock_out;
206
Stephen Boydd6968fc2015-04-30 13:54:13 -0700207 if (core->flags & CLK_IGNORE_UNUSED)
Mike Turquetteb24764902012-03-15 23:11:19 -0700208 goto unlock_out;
209
Mike Turquette7c045a52012-12-04 11:00:35 -0800210 /*
211 * some gate clocks have special needs during the disable-unused
212 * sequence. call .disable_unused if available, otherwise fall
213 * back to .disable
214 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700215 if (clk_core_is_enabled(core)) {
216 trace_clk_disable(core);
217 if (core->ops->disable_unused)
218 core->ops->disable_unused(core->hw);
219 else if (core->ops->disable)
220 core->ops->disable(core->hw);
221 trace_clk_disable_complete(core);
Mike Turquette7c045a52012-12-04 11:00:35 -0800222 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700223
224unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700225 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700226}
227
Olof Johansson1e435252013-04-27 14:10:18 -0700228static bool clk_ignore_unused;
229static int __init clk_ignore_unused_setup(char *__unused)
230{
231 clk_ignore_unused = true;
232 return 1;
233}
234__setup("clk_ignore_unused", clk_ignore_unused_setup);
235
Mike Turquetteb24764902012-03-15 23:11:19 -0700236static int clk_disable_unused(void)
237{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700238 struct clk_core *core;
Mike Turquetteb24764902012-03-15 23:11:19 -0700239
Olof Johansson1e435252013-04-27 14:10:18 -0700240 if (clk_ignore_unused) {
241 pr_warn("clk: Not disabling unused clocks\n");
242 return 0;
243 }
244
Mike Turquetteeab89f62013-03-28 13:59:01 -0700245 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700246
Stephen Boydd6968fc2015-04-30 13:54:13 -0700247 hlist_for_each_entry(core, &clk_root_list, child_node)
248 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700249
Stephen Boydd6968fc2015-04-30 13:54:13 -0700250 hlist_for_each_entry(core, &clk_orphan_list, child_node)
251 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700252
Stephen Boydd6968fc2015-04-30 13:54:13 -0700253 hlist_for_each_entry(core, &clk_root_list, child_node)
254 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100255
Stephen Boydd6968fc2015-04-30 13:54:13 -0700256 hlist_for_each_entry(core, &clk_orphan_list, child_node)
257 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100258
Mike Turquetteeab89f62013-03-28 13:59:01 -0700259 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700260
261 return 0;
262}
Saravana Kannand41d5802013-05-09 11:35:01 -0700263late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700264
265/*** helper functions ***/
266
Russ Dill65800b22012-11-26 11:20:09 -0800267const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700268{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100269 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700270}
Niels de Vos48950842012-12-13 13:12:25 +0100271EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700272
Russ Dill65800b22012-11-26 11:20:09 -0800273struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700274{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100275 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700276}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800277EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700278
Russ Dill65800b22012-11-26 11:20:09 -0800279u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700280{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100281 return !clk ? 0 : clk->core->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700282}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800283EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700284
Russ Dill65800b22012-11-26 11:20:09 -0800285struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700286{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100287 if (!clk)
288 return NULL;
289
290 /* TODO: Create a per-user clk and change callers to call clk_put */
291 return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700292}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800293EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700294
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700295static struct clk_core *__clk_lookup_subtree(const char *name,
296 struct clk_core *core)
297{
298 struct clk_core *child;
299 struct clk_core *ret;
300
301 if (!strcmp(core->name, name))
302 return core;
303
304 hlist_for_each_entry(child, &core->children, child_node) {
305 ret = __clk_lookup_subtree(name, child);
306 if (ret)
307 return ret;
308 }
309
310 return NULL;
311}
312
313static struct clk_core *clk_core_lookup(const char *name)
314{
315 struct clk_core *root_clk;
316 struct clk_core *ret;
317
318 if (!name)
319 return NULL;
320
321 /* search the 'proper' clk tree first */
322 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
323 ret = __clk_lookup_subtree(name, root_clk);
324 if (ret)
325 return ret;
326 }
327
328 /* if not found, then search the orphan tree */
329 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
330 ret = __clk_lookup_subtree(name, root_clk);
331 if (ret)
332 return ret;
333 }
334
335 return NULL;
336}
337
Stephen Boydd6968fc2015-04-30 13:54:13 -0700338static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100339 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100340{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700341 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100342 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700343 else if (!core->parents)
344 return clk_core_lookup(core->parent_names[index]);
345 else if (!core->parents[index])
346 return core->parents[index] =
347 clk_core_lookup(core->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100348 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700349 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100350}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100351
352struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
353{
354 struct clk_core *parent;
355
356 if (!clk)
357 return NULL;
358
359 parent = clk_core_get_parent_by_index(clk->core, index);
360
361 return !parent ? NULL : parent->hw->clk;
362}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800363EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100364
Russ Dill65800b22012-11-26 11:20:09 -0800365unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700366{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100367 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700368}
369
Stephen Boydd6968fc2015-04-30 13:54:13 -0700370static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700371{
372 unsigned long ret;
373
Stephen Boydd6968fc2015-04-30 13:54:13 -0700374 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530375 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700376 goto out;
377 }
378
Stephen Boydd6968fc2015-04-30 13:54:13 -0700379 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700380
Stephen Boydd6968fc2015-04-30 13:54:13 -0700381 if (core->flags & CLK_IS_ROOT)
Mike Turquetteb24764902012-03-15 23:11:19 -0700382 goto out;
383
Stephen Boydd6968fc2015-04-30 13:54:13 -0700384 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530385 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700386
387out:
388 return ret;
389}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100390
391unsigned long __clk_get_rate(struct clk *clk)
392{
393 if (!clk)
394 return 0;
395
396 return clk_core_get_rate_nolock(clk->core);
397}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800398EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700399
Stephen Boydd6968fc2015-04-30 13:54:13 -0700400static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100401{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700402 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100403 return 0;
404
Stephen Boydd6968fc2015-04-30 13:54:13 -0700405 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100406}
407
Russ Dill65800b22012-11-26 11:20:09 -0800408unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700409{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100410 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700411}
Thierry Redingb05c6832013-09-03 09:43:51 +0200412EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700413
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100414bool __clk_is_prepared(struct clk *clk)
415{
416 if (!clk)
417 return false;
418
419 return clk_core_is_prepared(clk->core);
420}
421
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100422bool __clk_is_enabled(struct clk *clk)
423{
424 if (!clk)
425 return false;
426
427 return clk_core_is_enabled(clk->core);
428}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800429EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700430
Stephen Boyd15a02c12015-01-19 18:05:28 -0800431static bool mux_is_better_rate(unsigned long rate, unsigned long now,
432 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100433{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800434 if (flags & CLK_MUX_ROUND_CLOSEST)
435 return abs(now - rate) < abs(best - rate);
436
437 return now <= rate && now > best;
438}
439
440static long
441clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100442 unsigned long min_rate,
443 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800444 unsigned long *best_parent_rate,
445 struct clk_hw **best_parent_p,
446 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100447{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100448 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
James Hogane366fdd2013-07-29 12:25:02 +0100449 int i, num_parents;
450 unsigned long parent_rate, best = 0;
451
452 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100453 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
454 parent = core->parent;
455 if (core->flags & CLK_SET_RATE_PARENT)
Javier Martinez Canillas9e0ad7d2015-02-12 14:58:28 +0100456 best = __clk_determine_rate(parent ? parent->hw : NULL,
457 rate, min_rate, max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100458 else if (parent)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100459 best = clk_core_get_rate_nolock(parent);
James Hogane366fdd2013-07-29 12:25:02 +0100460 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100461 best = clk_core_get_rate_nolock(core);
James Hogane366fdd2013-07-29 12:25:02 +0100462 goto out;
463 }
464
465 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100466 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100467 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100468 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100469 if (!parent)
470 continue;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100471 if (core->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100472 parent_rate = __clk_determine_rate(parent->hw, rate,
473 min_rate,
474 max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100475 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100476 parent_rate = clk_core_get_rate_nolock(parent);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800477 if (mux_is_better_rate(rate, parent_rate, best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100478 best_parent = parent;
479 best = parent_rate;
480 }
481 }
482
483out:
484 if (best_parent)
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100485 *best_parent_p = best_parent->hw;
James Hogane366fdd2013-07-29 12:25:02 +0100486 *best_parent_rate = best;
487
488 return best;
489}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800490
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100491struct clk *__clk_lookup(const char *name)
492{
493 struct clk_core *core = clk_core_lookup(name);
494
495 return !core ? NULL : core->hw->clk;
496}
497
Stephen Boydd6968fc2015-04-30 13:54:13 -0700498static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100499 unsigned long *min_rate,
500 unsigned long *max_rate)
501{
502 struct clk *clk_user;
503
504 *min_rate = 0;
505 *max_rate = ULONG_MAX;
506
Stephen Boydd6968fc2015-04-30 13:54:13 -0700507 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100508 *min_rate = max(*min_rate, clk_user->min_rate);
509
Stephen Boydd6968fc2015-04-30 13:54:13 -0700510 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100511 *max_rate = min(*max_rate, clk_user->max_rate);
512}
513
Stephen Boyd15a02c12015-01-19 18:05:28 -0800514/*
515 * Helper for finding best parent to provide a given frequency. This can be used
516 * directly as a determine_rate callback (e.g. for a mux), or from a more
517 * complex clock that may combine a mux with other operations.
518 */
519long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100520 unsigned long min_rate,
521 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800522 unsigned long *best_parent_rate,
523 struct clk_hw **best_parent_p)
524{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100525 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
526 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800527 best_parent_p, 0);
528}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800529EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100530
Stephen Boyd15a02c12015-01-19 18:05:28 -0800531long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100532 unsigned long min_rate,
533 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800534 unsigned long *best_parent_rate,
535 struct clk_hw **best_parent_p)
536{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100537 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
538 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800539 best_parent_p,
540 CLK_MUX_ROUND_CLOSEST);
541}
542EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
543
Mike Turquetteb24764902012-03-15 23:11:19 -0700544/*** clk api ***/
545
Stephen Boydd6968fc2015-04-30 13:54:13 -0700546static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700547{
Stephen Boyda6334722015-05-06 17:00:54 -0700548 lockdep_assert_held(&prepare_lock);
549
Stephen Boydd6968fc2015-04-30 13:54:13 -0700550 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700551 return;
552
Stephen Boydd6968fc2015-04-30 13:54:13 -0700553 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700554 return;
555
Stephen Boydd6968fc2015-04-30 13:54:13 -0700556 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700557 return;
558
Stephen Boydd6968fc2015-04-30 13:54:13 -0700559 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700560
Stephen Boydd6968fc2015-04-30 13:54:13 -0700561 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800562
Stephen Boydd6968fc2015-04-30 13:54:13 -0700563 if (core->ops->unprepare)
564 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700565
Stephen Boydd6968fc2015-04-30 13:54:13 -0700566 trace_clk_unprepare_complete(core);
567 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700568}
569
570/**
571 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200572 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700573 *
574 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
575 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
576 * if the operation may sleep. One example is a clk which is accessed over
577 * I2c. In the complex case a clk gate operation may require a fast and a slow
578 * part. It is this reason that clk_unprepare and clk_disable are not mutually
579 * exclusive. In fact clk_disable must be called before clk_unprepare.
580 */
581void clk_unprepare(struct clk *clk)
582{
Stephen Boyd63589e92014-03-26 16:06:37 -0700583 if (IS_ERR_OR_NULL(clk))
584 return;
585
Mike Turquetteeab89f62013-03-28 13:59:01 -0700586 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100587 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700588 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700589}
590EXPORT_SYMBOL_GPL(clk_unprepare);
591
Stephen Boydd6968fc2015-04-30 13:54:13 -0700592static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700593{
594 int ret = 0;
595
Stephen Boyda6334722015-05-06 17:00:54 -0700596 lockdep_assert_held(&prepare_lock);
597
Stephen Boydd6968fc2015-04-30 13:54:13 -0700598 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700599 return 0;
600
Stephen Boydd6968fc2015-04-30 13:54:13 -0700601 if (core->prepare_count == 0) {
602 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700603 if (ret)
604 return ret;
605
Stephen Boydd6968fc2015-04-30 13:54:13 -0700606 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800607
Stephen Boydd6968fc2015-04-30 13:54:13 -0700608 if (core->ops->prepare)
609 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800610
Stephen Boydd6968fc2015-04-30 13:54:13 -0700611 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800612
613 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700614 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800615 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700616 }
617 }
618
Stephen Boydd6968fc2015-04-30 13:54:13 -0700619 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700620
621 return 0;
622}
623
624/**
625 * clk_prepare - prepare a clock source
626 * @clk: the clk being prepared
627 *
628 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
629 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
630 * operation may sleep. One example is a clk which is accessed over I2c. In
631 * the complex case a clk ungate operation may require a fast and a slow part.
632 * It is this reason that clk_prepare and clk_enable are not mutually
633 * exclusive. In fact clk_prepare must be called before clk_enable.
634 * Returns 0 on success, -EERROR otherwise.
635 */
636int clk_prepare(struct clk *clk)
637{
638 int ret;
639
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100640 if (!clk)
641 return 0;
642
Mike Turquetteeab89f62013-03-28 13:59:01 -0700643 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100644 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700645 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700646
647 return ret;
648}
649EXPORT_SYMBOL_GPL(clk_prepare);
650
Stephen Boydd6968fc2015-04-30 13:54:13 -0700651static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700652{
Stephen Boyda6334722015-05-06 17:00:54 -0700653 lockdep_assert_held(&enable_lock);
654
Stephen Boydd6968fc2015-04-30 13:54:13 -0700655 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700656 return;
657
Stephen Boydd6968fc2015-04-30 13:54:13 -0700658 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700659 return;
660
Stephen Boydd6968fc2015-04-30 13:54:13 -0700661 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700662 return;
663
Stephen Boydd6968fc2015-04-30 13:54:13 -0700664 trace_clk_disable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800665
Stephen Boydd6968fc2015-04-30 13:54:13 -0700666 if (core->ops->disable)
667 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700668
Stephen Boydd6968fc2015-04-30 13:54:13 -0700669 trace_clk_disable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800670
Stephen Boydd6968fc2015-04-30 13:54:13 -0700671 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100672}
673
Mike Turquetteb24764902012-03-15 23:11:19 -0700674/**
675 * clk_disable - gate a clock
676 * @clk: the clk being gated
677 *
678 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
679 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
680 * clk if the operation is fast and will never sleep. One example is a
681 * SoC-internal clk which is controlled via simple register writes. In the
682 * complex case a clk gate operation may require a fast and a slow part. It is
683 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
684 * In fact clk_disable must be called before clk_unprepare.
685 */
686void clk_disable(struct clk *clk)
687{
688 unsigned long flags;
689
Stephen Boyd63589e92014-03-26 16:06:37 -0700690 if (IS_ERR_OR_NULL(clk))
691 return;
692
Mike Turquetteeab89f62013-03-28 13:59:01 -0700693 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700694 clk_core_disable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700695 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700696}
697EXPORT_SYMBOL_GPL(clk_disable);
698
Stephen Boydd6968fc2015-04-30 13:54:13 -0700699static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700700{
701 int ret = 0;
702
Stephen Boyda6334722015-05-06 17:00:54 -0700703 lockdep_assert_held(&enable_lock);
704
Stephen Boydd6968fc2015-04-30 13:54:13 -0700705 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700706 return 0;
707
Stephen Boydd6968fc2015-04-30 13:54:13 -0700708 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700709 return -ESHUTDOWN;
710
Stephen Boydd6968fc2015-04-30 13:54:13 -0700711 if (core->enable_count == 0) {
712 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700713
714 if (ret)
715 return ret;
716
Stephen Boydd6968fc2015-04-30 13:54:13 -0700717 trace_clk_enable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800718
Stephen Boydd6968fc2015-04-30 13:54:13 -0700719 if (core->ops->enable)
720 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800721
Stephen Boydd6968fc2015-04-30 13:54:13 -0700722 trace_clk_enable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800723
724 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700725 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800726 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700727 }
728 }
729
Stephen Boydd6968fc2015-04-30 13:54:13 -0700730 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700731 return 0;
732}
733
734/**
735 * clk_enable - ungate a clock
736 * @clk: the clk being ungated
737 *
738 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
739 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
740 * if the operation will never sleep. One example is a SoC-internal clk which
741 * is controlled via simple register writes. In the complex case a clk ungate
742 * operation may require a fast and a slow part. It is this reason that
743 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
744 * must be called before clk_enable. Returns 0 on success, -EERROR
745 * otherwise.
746 */
747int clk_enable(struct clk *clk)
748{
749 unsigned long flags;
750 int ret;
751
Dong Aisheng864e1602015-04-30 14:02:19 -0700752 if (!clk)
753 return 0;
754
Mike Turquetteeab89f62013-03-28 13:59:01 -0700755 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700756 ret = clk_core_enable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700757 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700758
759 return ret;
760}
761EXPORT_SYMBOL_GPL(clk_enable);
762
Stephen Boydd6968fc2015-04-30 13:54:13 -0700763static unsigned long clk_core_round_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100764 unsigned long rate,
765 unsigned long min_rate,
766 unsigned long max_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -0700767{
Shawn Guo81536e02012-04-12 20:50:17 +0800768 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100769 struct clk_core *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100770 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700771
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100772 lockdep_assert_held(&prepare_lock);
773
Stephen Boydd6968fc2015-04-30 13:54:13 -0700774 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700775 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700776
Stephen Boydd6968fc2015-04-30 13:54:13 -0700777 parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +0100778 if (parent)
779 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700780
Stephen Boydd6968fc2015-04-30 13:54:13 -0700781 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100782 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700783 return core->ops->determine_rate(core->hw, rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100784 min_rate, max_rate,
785 &parent_rate, &parent_hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700786 } else if (core->ops->round_rate)
787 return core->ops->round_rate(core->hw, rate, &parent_rate);
788 else if (core->flags & CLK_SET_RATE_PARENT)
789 return clk_core_round_rate_nolock(core->parent, rate, min_rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100790 max_rate);
James Hogan71472c02013-07-29 12:25:00 +0100791 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700792 return core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700793}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100794
795/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100796 * __clk_determine_rate - get the closest rate actually supported by a clock
797 * @hw: determine the rate of this clock
798 * @rate: target rate
799 * @min_rate: returned rate must be greater than this rate
800 * @max_rate: returned rate must be less than this rate
801 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700802 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100803 */
804unsigned long __clk_determine_rate(struct clk_hw *hw,
805 unsigned long rate,
806 unsigned long min_rate,
807 unsigned long max_rate)
808{
809 if (!hw)
810 return 0;
811
812 return clk_core_round_rate_nolock(hw->core, rate, min_rate, max_rate);
813}
814EXPORT_SYMBOL_GPL(__clk_determine_rate);
815
816/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100817 * __clk_round_rate - round the given rate for a clk
818 * @clk: round the rate of this clock
819 * @rate: the rate which is to be rounded
820 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700821 * Useful for clk_ops such as .set_rate
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100822 */
823unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
824{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100825 unsigned long min_rate;
826 unsigned long max_rate;
827
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100828 if (!clk)
829 return 0;
830
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100831 clk_core_get_boundaries(clk->core, &min_rate, &max_rate);
832
833 return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100834}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +0200835EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700836
837/**
838 * clk_round_rate - round the given rate for a clk
839 * @clk: the clk for which we are rounding a rate
840 * @rate: the rate which is to be rounded
841 *
842 * Takes in a rate as input and rounds it to a rate that the clk can actually
843 * use which is then returned. If clk doesn't support round_rate operation
844 * then the parent rate is returned.
845 */
846long clk_round_rate(struct clk *clk, unsigned long rate)
847{
848 unsigned long ret;
849
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100850 if (!clk)
851 return 0;
852
Mike Turquetteeab89f62013-03-28 13:59:01 -0700853 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700854 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700855 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700856
857 return ret;
858}
859EXPORT_SYMBOL_GPL(clk_round_rate);
860
861/**
862 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -0700863 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -0700864 * @msg: clk notifier type (see include/linux/clk.h)
865 * @old_rate: old clk rate
866 * @new_rate: new clk rate
867 *
868 * Triggers a notifier call chain on the clk rate-change notification
869 * for 'clk'. Passes a pointer to the struct clk and the previous
870 * and current rates to the notifier callback. Intended to be called by
871 * internal clock code only. Returns NOTIFY_DONE from the last driver
872 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
873 * a driver returns that.
874 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700875static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -0700876 unsigned long old_rate, unsigned long new_rate)
877{
878 struct clk_notifier *cn;
879 struct clk_notifier_data cnd;
880 int ret = NOTIFY_DONE;
881
Mike Turquetteb24764902012-03-15 23:11:19 -0700882 cnd.old_rate = old_rate;
883 cnd.new_rate = new_rate;
884
885 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700886 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100887 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700888 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
889 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -0700890 }
891 }
892
893 return ret;
894}
895
896/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100897 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -0700898 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100899 *
900 * Walks the subtree of clks starting with clk and recalculates accuracies as
901 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700902 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100903 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100904 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700905static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100906{
907 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100908 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100909
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100910 lockdep_assert_held(&prepare_lock);
911
Stephen Boydd6968fc2015-04-30 13:54:13 -0700912 if (core->parent)
913 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100914
Stephen Boydd6968fc2015-04-30 13:54:13 -0700915 if (core->ops->recalc_accuracy)
916 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100917 parent_accuracy);
918 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700919 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100920
Stephen Boydd6968fc2015-04-30 13:54:13 -0700921 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100922 __clk_recalc_accuracies(child);
923}
924
Stephen Boydd6968fc2015-04-30 13:54:13 -0700925static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100926{
927 unsigned long accuracy;
928
929 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -0700930 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
931 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100932
Stephen Boydd6968fc2015-04-30 13:54:13 -0700933 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100934 clk_prepare_unlock();
935
936 return accuracy;
937}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100938
939/**
940 * clk_get_accuracy - return the accuracy of clk
941 * @clk: the clk whose accuracy is being returned
942 *
943 * Simply returns the cached accuracy of the clk, unless
944 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
945 * issued.
946 * If clk is NULL then returns 0.
947 */
948long clk_get_accuracy(struct clk *clk)
949{
950 if (!clk)
951 return 0;
952
953 return clk_core_get_accuracy(clk->core);
954}
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100955EXPORT_SYMBOL_GPL(clk_get_accuracy);
956
Stephen Boydd6968fc2015-04-30 13:54:13 -0700957static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100958 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700959{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700960 if (core->ops->recalc_rate)
961 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700962 return parent_rate;
963}
964
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100965/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700966 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -0700967 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -0700968 * @msg: notification type (see include/linux/clk.h)
969 *
970 * Walks the subtree of clks starting with clk and recalculates rates as it
971 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200972 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -0700973 *
974 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
975 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -0700976 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700977static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -0700978{
979 unsigned long old_rate;
980 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100981 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700982
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100983 lockdep_assert_held(&prepare_lock);
984
Stephen Boydd6968fc2015-04-30 13:54:13 -0700985 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700986
Stephen Boydd6968fc2015-04-30 13:54:13 -0700987 if (core->parent)
988 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700989
Stephen Boydd6968fc2015-04-30 13:54:13 -0700990 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700991
992 /*
993 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
994 * & ABORT_RATE_CHANGE notifiers
995 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700996 if (core->notifier_count && msg)
997 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700998
Stephen Boydd6968fc2015-04-30 13:54:13 -0700999 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001000 __clk_recalc_rates(child, msg);
1001}
1002
Stephen Boydd6968fc2015-04-30 13:54:13 -07001003static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001004{
1005 unsigned long rate;
1006
1007 clk_prepare_lock();
1008
Stephen Boydd6968fc2015-04-30 13:54:13 -07001009 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1010 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001011
Stephen Boydd6968fc2015-04-30 13:54:13 -07001012 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001013 clk_prepare_unlock();
1014
1015 return rate;
1016}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001017
Mike Turquetteb24764902012-03-15 23:11:19 -07001018/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001019 * clk_get_rate - return the rate of clk
1020 * @clk: the clk whose rate is being returned
1021 *
1022 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1023 * is set, which means a recalc_rate will be issued.
1024 * If clk is NULL then returns 0.
1025 */
1026unsigned long clk_get_rate(struct clk *clk)
1027{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001028 if (!clk)
1029 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001030
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001031 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001032}
1033EXPORT_SYMBOL_GPL(clk_get_rate);
1034
Stephen Boydd6968fc2015-04-30 13:54:13 -07001035static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001036 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001037{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001038 int i;
James Hogan4935b222013-07-29 12:24:59 +01001039
Stephen Boydd6968fc2015-04-30 13:54:13 -07001040 if (!core->parents) {
1041 core->parents = kcalloc(core->num_parents,
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001042 sizeof(struct clk *), GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001043 if (!core->parents)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001044 return -ENOMEM;
1045 }
James Hogan4935b222013-07-29 12:24:59 +01001046
1047 /*
1048 * find index of new parent clock using cached parent ptrs,
1049 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001050 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001051 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001052 for (i = 0; i < core->num_parents; i++) {
1053 if (core->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001054 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001055
Stephen Boydd6968fc2015-04-30 13:54:13 -07001056 if (core->parents[i])
Tomasz Figada0f0b22013-09-29 02:37:16 +02001057 continue;
1058
Stephen Boydd6968fc2015-04-30 13:54:13 -07001059 if (!strcmp(core->parent_names[i], parent->name)) {
1060 core->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001061 return i;
James Hogan4935b222013-07-29 12:24:59 +01001062 }
1063 }
1064
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001065 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001066}
1067
Stephen Boydd6968fc2015-04-30 13:54:13 -07001068static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001069{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001070 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001071
James Hogan903efc52013-08-29 12:10:51 +01001072 if (new_parent) {
1073 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001074 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001075 new_parent->new_child = NULL;
1076
Stephen Boydd6968fc2015-04-30 13:54:13 -07001077 hlist_add_head(&core->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001078 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001079 hlist_add_head(&core->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001080 }
James Hogan4935b222013-07-29 12:24:59 +01001081
Stephen Boydd6968fc2015-04-30 13:54:13 -07001082 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001083}
1084
Stephen Boydd6968fc2015-04-30 13:54:13 -07001085static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001086 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001087{
1088 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001089 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001090
1091 /*
1092 * Migrate prepare state between parents and prevent race with
1093 * clk_enable().
1094 *
1095 * If the clock is not prepared, then a race with
1096 * clk_enable/disable() is impossible since we already have the
1097 * prepare lock (future calls to clk_enable() need to be preceded by
1098 * a clk_prepare()).
1099 *
1100 * If the clock is prepared, migrate the prepared state to the new
1101 * parent and also protect against a race with clk_enable() by
1102 * forcing the clock and the new parent on. This ensures that all
1103 * future calls to clk_enable() are practically NOPs with respect to
1104 * hardware and software states.
1105 *
1106 * See also: Comment for clk_set_parent() below.
1107 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001108 if (core->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001109 clk_core_prepare(parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001110 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001111 clk_core_enable(parent);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001112 clk_core_enable(core);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001113 clk_enable_unlock(flags);
James Hogan4935b222013-07-29 12:24:59 +01001114 }
1115
1116 /* update the clk tree topology */
1117 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001118 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001119 clk_enable_unlock(flags);
1120
Stephen Boyd3fa22522014-01-15 10:47:22 -08001121 return old_parent;
1122}
1123
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001124static void __clk_set_parent_after(struct clk_core *core,
1125 struct clk_core *parent,
1126 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001127{
Dong Aishengd2a5d462015-04-15 22:26:36 +08001128 unsigned long flags;
1129
Stephen Boyd3fa22522014-01-15 10:47:22 -08001130 /*
1131 * Finish the migration of prepare state and undo the changes done
1132 * for preventing a race with clk_enable().
1133 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001134 if (core->prepare_count) {
Dong Aishengd2a5d462015-04-15 22:26:36 +08001135 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001136 clk_core_disable(core);
1137 clk_core_disable(old_parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001138 clk_enable_unlock(flags);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001139 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001140 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001141}
1142
Stephen Boydd6968fc2015-04-30 13:54:13 -07001143static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001144 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001145{
1146 unsigned long flags;
1147 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001148 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001149
Stephen Boydd6968fc2015-04-30 13:54:13 -07001150 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001151
Stephen Boydd6968fc2015-04-30 13:54:13 -07001152 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001153
James Hogan4935b222013-07-29 12:24:59 +01001154 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001155 if (parent && core->ops->set_parent)
1156 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001157
Stephen Boydd6968fc2015-04-30 13:54:13 -07001158 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001159
James Hogan4935b222013-07-29 12:24:59 +01001160 if (ret) {
1161 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001162 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001163 clk_enable_unlock(flags);
1164
Stephen Boydd6968fc2015-04-30 13:54:13 -07001165 if (core->prepare_count) {
Dong Aishengd2a5d462015-04-15 22:26:36 +08001166 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001167 clk_core_disable(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001168 clk_core_disable(parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001169 clk_enable_unlock(flags);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001170 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001171 }
1172 return ret;
1173 }
1174
Stephen Boydd6968fc2015-04-30 13:54:13 -07001175 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001176
James Hogan4935b222013-07-29 12:24:59 +01001177 return 0;
1178}
1179
Ulf Hanssona093bde2012-08-31 14:21:28 +02001180/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001181 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001182 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001183 * @parent_rate: the "future" rate of clk's parent
1184 *
1185 * Walks the subtree of clks starting with clk, speculating rates as it
1186 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1187 *
1188 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1189 * pre-rate change notifications and returns early if no clks in the
1190 * subtree have subscribed to the notifications. Note that if a clk does not
1191 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001192 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001193 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001194static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001195 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001196{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001197 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001198 unsigned long new_rate;
1199 int ret = NOTIFY_DONE;
1200
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001201 lockdep_assert_held(&prepare_lock);
1202
Stephen Boydd6968fc2015-04-30 13:54:13 -07001203 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001204
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001205 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001206 if (core->notifier_count)
1207 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001208
Mike Turquette86bcfa22014-02-24 16:08:41 -08001209 if (ret & NOTIFY_STOP_MASK) {
1210 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001211 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001212 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001213 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001214
Stephen Boydd6968fc2015-04-30 13:54:13 -07001215 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001216 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001217 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001218 break;
1219 }
1220
1221out:
1222 return ret;
1223}
1224
Stephen Boydd6968fc2015-04-30 13:54:13 -07001225static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001226 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001227{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001228 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001229
Stephen Boydd6968fc2015-04-30 13:54:13 -07001230 core->new_rate = new_rate;
1231 core->new_parent = new_parent;
1232 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001233 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001234 core->new_child = NULL;
1235 if (new_parent && new_parent != core->parent)
1236 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001237
Stephen Boydd6968fc2015-04-30 13:54:13 -07001238 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001239 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001240 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001241 }
1242}
1243
1244/*
1245 * calculate the new rates returning the topmost clock that has to be
1246 * changed.
1247 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001248static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001249 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001250{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001251 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001252 struct clk_core *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001253 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001254 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001255 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001256 unsigned long min_rate;
1257 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001258 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001259 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001260
Mike Turquette7452b212012-03-26 14:45:36 -07001261 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001262 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001263 return NULL;
1264
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001265 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001266 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001267 if (parent)
1268 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001269
Stephen Boydd6968fc2015-04-30 13:54:13 -07001270 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001271
James Hogan71472c02013-07-29 12:25:00 +01001272 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001273 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001274 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001275 ret = core->ops->determine_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001276 min_rate,
1277 max_rate,
1278 &best_parent_rate,
1279 &parent_hw);
1280 if (ret < 0)
1281 return NULL;
1282
1283 new_rate = ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001284 parent = parent_hw ? parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001285 } else if (core->ops->round_rate) {
1286 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001287 &best_parent_rate);
1288 if (ret < 0)
1289 return NULL;
1290
1291 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001292 if (new_rate < min_rate || new_rate > max_rate)
1293 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001294 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001295 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001296 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001297 return NULL;
1298 } else {
1299 /* pass-through clock with adjustable parent */
1300 top = clk_calc_new_rates(parent, rate);
1301 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001302 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001303 }
1304
James Hogan71472c02013-07-29 12:25:00 +01001305 /* some clocks must be gated to change parent */
1306 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001307 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001308 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001309 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001310 return NULL;
1311 }
1312
James Hogan71472c02013-07-29 12:25:00 +01001313 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001314 if (parent && core->num_parents > 1) {
1315 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001316 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001317 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001318 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001319 return NULL;
1320 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001321 }
1322
Stephen Boydd6968fc2015-04-30 13:54:13 -07001323 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001324 best_parent_rate != parent->rate)
1325 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001326
1327out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001328 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001329
1330 return top;
1331}
1332
1333/*
1334 * Notify about rate changes in a subtree. Always walk down the whole tree
1335 * so that in case of an error we can walk down the whole tree again and
1336 * abort the change.
1337 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001338static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001339 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001340{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001341 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001342 int ret = NOTIFY_DONE;
1343
Stephen Boydd6968fc2015-04-30 13:54:13 -07001344 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301345 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001346
Stephen Boydd6968fc2015-04-30 13:54:13 -07001347 if (core->notifier_count) {
1348 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001349 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001350 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001351 }
1352
Stephen Boydd6968fc2015-04-30 13:54:13 -07001353 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001354 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001355 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001356 continue;
1357 tmp_clk = clk_propagate_rate_change(child, event);
1358 if (tmp_clk)
1359 fail_clk = tmp_clk;
1360 }
1361
Stephen Boydd6968fc2015-04-30 13:54:13 -07001362 /* handle the new child who might not be in core->children yet */
1363 if (core->new_child) {
1364 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001365 if (tmp_clk)
1366 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001367 }
1368
1369 return fail_clk;
1370}
1371
1372/*
1373 * walk down a subtree and set the new rates notifying the rate
1374 * change on the way
1375 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001376static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001377{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001378 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001379 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001380 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001381 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001382 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001383 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001384
Stephen Boydd6968fc2015-04-30 13:54:13 -07001385 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001386
Stephen Boydd6968fc2015-04-30 13:54:13 -07001387 if (core->new_parent)
1388 best_parent_rate = core->new_parent->rate;
1389 else if (core->parent)
1390 best_parent_rate = core->parent->rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001391
Stephen Boydd6968fc2015-04-30 13:54:13 -07001392 if (core->new_parent && core->new_parent != core->parent) {
1393 old_parent = __clk_set_parent_before(core, core->new_parent);
1394 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001395
Stephen Boydd6968fc2015-04-30 13:54:13 -07001396 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001397 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001398 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001399 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001400 core->new_parent_index);
1401 } else if (core->ops->set_parent) {
1402 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001403 }
1404
Stephen Boydd6968fc2015-04-30 13:54:13 -07001405 trace_clk_set_parent_complete(core, core->new_parent);
1406 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001407 }
1408
Stephen Boydd6968fc2015-04-30 13:54:13 -07001409 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001410
Stephen Boydd6968fc2015-04-30 13:54:13 -07001411 if (!skip_set_rate && core->ops->set_rate)
1412 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001413
Stephen Boydd6968fc2015-04-30 13:54:13 -07001414 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001415
Stephen Boydd6968fc2015-04-30 13:54:13 -07001416 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001417
Stephen Boydd6968fc2015-04-30 13:54:13 -07001418 if (core->notifier_count && old_rate != core->rate)
1419 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001420
Michael Turquette85e88fa2015-06-20 12:18:03 -07001421 if (core->flags & CLK_RECALC_NEW_RATES)
1422 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001423
Tero Kristo067bb172014-08-21 16:47:45 +03001424 /*
1425 * Use safe iteration, as change_rate can actually swap parents
1426 * for certain clock types.
1427 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001428 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001429 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001430 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001431 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001432 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001433 }
1434
Stephen Boydd6968fc2015-04-30 13:54:13 -07001435 /* handle the new child who might not be in core->children yet */
1436 if (core->new_child)
1437 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001438}
1439
Stephen Boydd6968fc2015-04-30 13:54:13 -07001440static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001441 unsigned long req_rate)
1442{
1443 struct clk_core *top, *fail_clk;
1444 unsigned long rate = req_rate;
1445 int ret = 0;
1446
Stephen Boydd6968fc2015-04-30 13:54:13 -07001447 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001448 return 0;
1449
1450 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001451 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001452 return 0;
1453
Stephen Boydd6968fc2015-04-30 13:54:13 -07001454 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001455 return -EBUSY;
1456
1457 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001458 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001459 if (!top)
1460 return -EINVAL;
1461
1462 /* notify that we are about to change rates */
1463 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1464 if (fail_clk) {
1465 pr_debug("%s: failed to set %s rate\n", __func__,
1466 fail_clk->name);
1467 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1468 return -EBUSY;
1469 }
1470
1471 /* change the rates */
1472 clk_change_rate(top);
1473
Stephen Boydd6968fc2015-04-30 13:54:13 -07001474 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001475
1476 return ret;
1477}
1478
Mike Turquetteb24764902012-03-15 23:11:19 -07001479/**
1480 * clk_set_rate - specify a new rate for clk
1481 * @clk: the clk whose rate is being changed
1482 * @rate: the new rate for clk
1483 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001484 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001485 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001486 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1487 * propagate up to clk's parent; whether or not this happens depends on the
1488 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1489 * after calling .round_rate then upstream parent propagation is ignored. If
1490 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001491 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001492 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1493 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001494 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001495 * Rate changes are accomplished via tree traversal that also recalculates the
1496 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001497 *
1498 * Returns 0 on success, -EERROR otherwise.
1499 */
1500int clk_set_rate(struct clk *clk, unsigned long rate)
1501{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001502 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001503
Mike Turquette89ac8d72013-08-21 23:58:09 -07001504 if (!clk)
1505 return 0;
1506
Mike Turquetteb24764902012-03-15 23:11:19 -07001507 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001508 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001509
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001510 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001511
Mike Turquetteeab89f62013-03-28 13:59:01 -07001512 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001513
1514 return ret;
1515}
1516EXPORT_SYMBOL_GPL(clk_set_rate);
1517
1518/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001519 * clk_set_rate_range - set a rate range for a clock source
1520 * @clk: clock source
1521 * @min: desired minimum clock rate in Hz, inclusive
1522 * @max: desired maximum clock rate in Hz, inclusive
1523 *
1524 * Returns success (0) or negative errno.
1525 */
1526int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1527{
1528 int ret = 0;
1529
1530 if (!clk)
1531 return 0;
1532
1533 if (min > max) {
1534 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1535 __func__, clk->core->name, clk->dev_id, clk->con_id,
1536 min, max);
1537 return -EINVAL;
1538 }
1539
1540 clk_prepare_lock();
1541
1542 if (min != clk->min_rate || max != clk->max_rate) {
1543 clk->min_rate = min;
1544 clk->max_rate = max;
1545 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1546 }
1547
1548 clk_prepare_unlock();
1549
1550 return ret;
1551}
1552EXPORT_SYMBOL_GPL(clk_set_rate_range);
1553
1554/**
1555 * clk_set_min_rate - set a minimum clock rate for a clock source
1556 * @clk: clock source
1557 * @rate: desired minimum clock rate in Hz, inclusive
1558 *
1559 * Returns success (0) or negative errno.
1560 */
1561int clk_set_min_rate(struct clk *clk, unsigned long rate)
1562{
1563 if (!clk)
1564 return 0;
1565
1566 return clk_set_rate_range(clk, rate, clk->max_rate);
1567}
1568EXPORT_SYMBOL_GPL(clk_set_min_rate);
1569
1570/**
1571 * clk_set_max_rate - set a maximum clock rate for a clock source
1572 * @clk: clock source
1573 * @rate: desired maximum clock rate in Hz, inclusive
1574 *
1575 * Returns success (0) or negative errno.
1576 */
1577int clk_set_max_rate(struct clk *clk, unsigned long rate)
1578{
1579 if (!clk)
1580 return 0;
1581
1582 return clk_set_rate_range(clk, clk->min_rate, rate);
1583}
1584EXPORT_SYMBOL_GPL(clk_set_max_rate);
1585
1586/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001587 * clk_get_parent - return the parent of a clk
1588 * @clk: the clk whose parent gets returned
1589 *
1590 * Simply returns clk->parent. Returns NULL if clk is NULL.
1591 */
1592struct clk *clk_get_parent(struct clk *clk)
1593{
1594 struct clk *parent;
1595
Mike Turquetteeab89f62013-03-28 13:59:01 -07001596 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001597 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001598 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001599
1600 return parent;
1601}
1602EXPORT_SYMBOL_GPL(clk_get_parent);
1603
1604/*
1605 * .get_parent is mandatory for clocks with multiple possible parents. It is
1606 * optional for single-parent clocks. Always call .get_parent if it is
1607 * available and WARN if it is missing for multi-parent clocks.
1608 *
1609 * For single-parent clocks without .get_parent, first check to see if the
1610 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001611 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001612 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001613static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001614{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001615 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001616 u8 index;
1617
1618 /* handle the trivial cases */
1619
Stephen Boydd6968fc2015-04-30 13:54:13 -07001620 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -07001621 goto out;
1622
Stephen Boydd6968fc2015-04-30 13:54:13 -07001623 if (core->num_parents == 1) {
1624 if (IS_ERR_OR_NULL(core->parent))
1625 core->parent = clk_core_lookup(core->parent_names[0]);
1626 ret = core->parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001627 goto out;
1628 }
1629
Stephen Boydd6968fc2015-04-30 13:54:13 -07001630 if (!core->ops->get_parent) {
1631 WARN(!core->ops->get_parent,
Mike Turquetteb24764902012-03-15 23:11:19 -07001632 "%s: multi-parent clocks must implement .get_parent\n",
1633 __func__);
1634 goto out;
1635 };
1636
1637 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07001638 * Do our best to cache parent clocks in core->parents. This prevents
1639 * unnecessary and expensive lookups. We don't set core->parent here;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001640 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001641 */
1642
Stephen Boydd6968fc2015-04-30 13:54:13 -07001643 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001644
Stephen Boydd6968fc2015-04-30 13:54:13 -07001645 if (!core->parents)
1646 core->parents =
1647 kcalloc(core->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001648 GFP_KERNEL);
1649
Stephen Boydd6968fc2015-04-30 13:54:13 -07001650 ret = clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001651
1652out:
1653 return ret;
1654}
1655
Stephen Boydd6968fc2015-04-30 13:54:13 -07001656static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001657 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001658{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001659 clk_reparent(core, new_parent);
1660 __clk_recalc_accuracies(core);
1661 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07001662}
1663
Tomeu Vizoso42c86542015-03-11 11:34:25 +01001664void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
1665{
1666 if (!hw)
1667 return;
1668
1669 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
1670}
1671
Mike Turquetteb24764902012-03-15 23:11:19 -07001672/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001673 * clk_has_parent - check if a clock is a possible parent for another
1674 * @clk: clock source
1675 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07001676 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001677 * This function can be used in drivers that need to check that a clock can be
1678 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001679 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001680 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001681 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001682bool clk_has_parent(struct clk *clk, struct clk *parent)
1683{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001684 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001685 unsigned int i;
1686
1687 /* NULL clocks should be nops, so return success if either is NULL. */
1688 if (!clk || !parent)
1689 return true;
1690
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001691 core = clk->core;
1692 parent_core = parent->core;
1693
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001694 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001695 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001696 return true;
1697
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001698 for (i = 0; i < core->num_parents; i++)
1699 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001700 return true;
1701
1702 return false;
1703}
1704EXPORT_SYMBOL_GPL(clk_has_parent);
1705
Stephen Boydd6968fc2015-04-30 13:54:13 -07001706static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001707{
1708 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001709 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001710 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001711
Stephen Boydd6968fc2015-04-30 13:54:13 -07001712 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07001713 return 0;
1714
Mike Turquetteb24764902012-03-15 23:11:19 -07001715 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001716 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001717
Stephen Boydd6968fc2015-04-30 13:54:13 -07001718 if (core->parent == parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001719 goto out;
1720
Stephen Boydb61c43c2015-02-02 14:11:25 -08001721 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001722 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08001723 ret = -ENOSYS;
1724 goto out;
1725 }
1726
Ulf Hansson031dcc92013-04-02 23:09:38 +02001727 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001728 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001729 ret = -EBUSY;
1730 goto out;
1731 }
1732
1733 /* try finding the new parent index */
1734 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001735 p_index = clk_fetch_parent_index(core, parent);
Ulf Hansson031dcc92013-04-02 23:09:38 +02001736 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001737 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001738 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001739 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001740 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001741 goto out;
1742 }
1743 }
1744
Mike Turquetteb24764902012-03-15 23:11:19 -07001745 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001746 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001747
1748 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001749 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001750 goto out;
1751
Ulf Hansson031dcc92013-04-02 23:09:38 +02001752 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001753 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001754
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001755 /* propagate rate an accuracy recalculation accordingly */
1756 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001757 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001758 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001759 __clk_recalc_rates(core, POST_RATE_CHANGE);
1760 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001761 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001762
1763out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001764 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001765
1766 return ret;
1767}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001768
1769/**
1770 * clk_set_parent - switch the parent of a mux clk
1771 * @clk: the mux clk whose input we are switching
1772 * @parent: the new input to clk
1773 *
1774 * Re-parent clk to use parent as its new input source. If clk is in
1775 * prepared state, the clk will get enabled for the duration of this call. If
1776 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1777 * that, the reparenting is glitchy in hardware, etc), use the
1778 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1779 *
1780 * After successfully changing clk's parent clk_set_parent will update the
1781 * clk topology, sysfs topology and propagate rate recalculation via
1782 * __clk_recalc_rates.
1783 *
1784 * Returns 0 on success, -EERROR otherwise.
1785 */
1786int clk_set_parent(struct clk *clk, struct clk *parent)
1787{
1788 if (!clk)
1789 return 0;
1790
1791 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1792}
Mike Turquetteb24764902012-03-15 23:11:19 -07001793EXPORT_SYMBOL_GPL(clk_set_parent);
1794
1795/**
Mike Turquettee59c5372014-02-18 21:21:25 -08001796 * clk_set_phase - adjust the phase shift of a clock signal
1797 * @clk: clock signal source
1798 * @degrees: number of degrees the signal is shifted
1799 *
1800 * Shifts the phase of a clock signal by the specified
1801 * degrees. Returns 0 on success, -EERROR otherwise.
1802 *
1803 * This function makes no distinction about the input or reference
1804 * signal that we adjust the clock signal phase against. For example
1805 * phase locked-loop clock signal generators we may shift phase with
1806 * respect to feedback clock signal input, but for other cases the
1807 * clock phase may be shifted with respect to some other, unspecified
1808 * signal.
1809 *
1810 * Additionally the concept of phase shift does not propagate through
1811 * the clock tree hierarchy, which sets it apart from clock rates and
1812 * clock accuracy. A parent clock phase attribute does not have an
1813 * impact on the phase attribute of a child clock.
1814 */
1815int clk_set_phase(struct clk *clk, int degrees)
1816{
Stephen Boyd08b95752015-02-02 14:09:43 -08001817 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08001818
1819 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08001820 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08001821
1822 /* sanity check degrees */
1823 degrees %= 360;
1824 if (degrees < 0)
1825 degrees += 360;
1826
1827 clk_prepare_lock();
1828
Stephen Boyddfc202e2015-02-02 14:37:41 -08001829 trace_clk_set_phase(clk->core, degrees);
1830
Stephen Boyd08b95752015-02-02 14:09:43 -08001831 if (clk->core->ops->set_phase)
1832 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08001833
Stephen Boyddfc202e2015-02-02 14:37:41 -08001834 trace_clk_set_phase_complete(clk->core, degrees);
1835
Mike Turquettee59c5372014-02-18 21:21:25 -08001836 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001837 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08001838
Mike Turquettee59c5372014-02-18 21:21:25 -08001839 clk_prepare_unlock();
1840
Mike Turquettee59c5372014-02-18 21:21:25 -08001841 return ret;
1842}
Maxime Ripard9767b042015-01-20 22:23:43 +01001843EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001844
Stephen Boydd6968fc2015-04-30 13:54:13 -07001845static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08001846{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07001847 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08001848
1849 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001850 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08001851 clk_prepare_unlock();
1852
Mike Turquettee59c5372014-02-18 21:21:25 -08001853 return ret;
1854}
1855
1856/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001857 * clk_get_phase - return the phase shift of a clock signal
1858 * @clk: clock signal source
1859 *
1860 * Returns the phase shift of a clock node in degrees, otherwise returns
1861 * -EERROR.
1862 */
1863int clk_get_phase(struct clk *clk)
1864{
1865 if (!clk)
1866 return 0;
1867
1868 return clk_core_get_phase(clk->core);
1869}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001870EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07001871
1872/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08001873 * clk_is_match - check if two clk's point to the same hardware clock
1874 * @p: clk compared against q
1875 * @q: clk compared against p
1876 *
1877 * Returns true if the two struct clk pointers both point to the same hardware
1878 * clock node. Put differently, returns true if struct clk *p and struct clk *q
1879 * share the same struct clk_core object.
1880 *
1881 * Returns false otherwise. Note that two NULL clks are treated as matching.
1882 */
1883bool clk_is_match(const struct clk *p, const struct clk *q)
1884{
1885 /* trivial case: identical struct clk's or both NULL */
1886 if (p == q)
1887 return true;
1888
1889 /* true if clk->core pointers match. Avoid derefing garbage */
1890 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
1891 if (p->core == q->core)
1892 return true;
1893
1894 return false;
1895}
1896EXPORT_SYMBOL_GPL(clk_is_match);
1897
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001898/*** debugfs support ***/
1899
1900#ifdef CONFIG_DEBUG_FS
1901#include <linux/debugfs.h>
1902
1903static struct dentry *rootdir;
1904static int inited = 0;
1905static DEFINE_MUTEX(clk_debug_lock);
1906static HLIST_HEAD(clk_debug_list);
1907
1908static struct hlist_head *all_lists[] = {
1909 &clk_root_list,
1910 &clk_orphan_list,
1911 NULL,
1912};
1913
1914static struct hlist_head *orphan_list[] = {
1915 &clk_orphan_list,
1916 NULL,
1917};
1918
1919static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
1920 int level)
1921{
1922 if (!c)
1923 return;
1924
1925 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
1926 level * 3 + 1, "",
1927 30 - level * 3, c->name,
1928 c->enable_count, c->prepare_count, clk_core_get_rate(c),
1929 clk_core_get_accuracy(c), clk_core_get_phase(c));
1930}
1931
1932static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
1933 int level)
1934{
1935 struct clk_core *child;
1936
1937 if (!c)
1938 return;
1939
1940 clk_summary_show_one(s, c, level);
1941
1942 hlist_for_each_entry(child, &c->children, child_node)
1943 clk_summary_show_subtree(s, child, level + 1);
1944}
1945
1946static int clk_summary_show(struct seq_file *s, void *data)
1947{
1948 struct clk_core *c;
1949 struct hlist_head **lists = (struct hlist_head **)s->private;
1950
1951 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
1952 seq_puts(s, "----------------------------------------------------------------------------------------\n");
1953
1954 clk_prepare_lock();
1955
1956 for (; *lists; lists++)
1957 hlist_for_each_entry(c, *lists, child_node)
1958 clk_summary_show_subtree(s, c, 0);
1959
1960 clk_prepare_unlock();
1961
1962 return 0;
1963}
1964
1965
1966static int clk_summary_open(struct inode *inode, struct file *file)
1967{
1968 return single_open(file, clk_summary_show, inode->i_private);
1969}
1970
1971static const struct file_operations clk_summary_fops = {
1972 .open = clk_summary_open,
1973 .read = seq_read,
1974 .llseek = seq_lseek,
1975 .release = single_release,
1976};
1977
1978static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
1979{
1980 if (!c)
1981 return;
1982
Stefan Wahren7cb81132015-04-29 16:36:43 +00001983 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001984 seq_printf(s, "\"%s\": { ", c->name);
1985 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
1986 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00001987 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
1988 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001989 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
1990}
1991
1992static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
1993{
1994 struct clk_core *child;
1995
1996 if (!c)
1997 return;
1998
1999 clk_dump_one(s, c, level);
2000
2001 hlist_for_each_entry(child, &c->children, child_node) {
2002 seq_printf(s, ",");
2003 clk_dump_subtree(s, child, level + 1);
2004 }
2005
2006 seq_printf(s, "}");
2007}
2008
2009static int clk_dump(struct seq_file *s, void *data)
2010{
2011 struct clk_core *c;
2012 bool first_node = true;
2013 struct hlist_head **lists = (struct hlist_head **)s->private;
2014
2015 seq_printf(s, "{");
2016
2017 clk_prepare_lock();
2018
2019 for (; *lists; lists++) {
2020 hlist_for_each_entry(c, *lists, child_node) {
2021 if (!first_node)
2022 seq_puts(s, ",");
2023 first_node = false;
2024 clk_dump_subtree(s, c, 0);
2025 }
2026 }
2027
2028 clk_prepare_unlock();
2029
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002030 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002031 return 0;
2032}
2033
2034
2035static int clk_dump_open(struct inode *inode, struct file *file)
2036{
2037 return single_open(file, clk_dump, inode->i_private);
2038}
2039
2040static const struct file_operations clk_dump_fops = {
2041 .open = clk_dump_open,
2042 .read = seq_read,
2043 .llseek = seq_lseek,
2044 .release = single_release,
2045};
2046
2047static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2048{
2049 struct dentry *d;
2050 int ret = -ENOMEM;
2051
2052 if (!core || !pdentry) {
2053 ret = -EINVAL;
2054 goto out;
2055 }
2056
2057 d = debugfs_create_dir(core->name, pdentry);
2058 if (!d)
2059 goto out;
2060
2061 core->dentry = d;
2062
2063 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2064 (u32 *)&core->rate);
2065 if (!d)
2066 goto err_out;
2067
2068 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2069 (u32 *)&core->accuracy);
2070 if (!d)
2071 goto err_out;
2072
2073 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2074 (u32 *)&core->phase);
2075 if (!d)
2076 goto err_out;
2077
2078 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2079 (u32 *)&core->flags);
2080 if (!d)
2081 goto err_out;
2082
2083 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2084 (u32 *)&core->prepare_count);
2085 if (!d)
2086 goto err_out;
2087
2088 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2089 (u32 *)&core->enable_count);
2090 if (!d)
2091 goto err_out;
2092
2093 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2094 (u32 *)&core->notifier_count);
2095 if (!d)
2096 goto err_out;
2097
2098 if (core->ops->debug_init) {
2099 ret = core->ops->debug_init(core->hw, core->dentry);
2100 if (ret)
2101 goto err_out;
2102 }
2103
2104 ret = 0;
2105 goto out;
2106
2107err_out:
2108 debugfs_remove_recursive(core->dentry);
2109 core->dentry = NULL;
2110out:
2111 return ret;
2112}
2113
2114/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002115 * clk_debug_register - add a clk node to the debugfs clk directory
2116 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002117 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002118 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2119 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002120 * will be created lazily by clk_debug_init as part of a late_initcall.
2121 */
2122static int clk_debug_register(struct clk_core *core)
2123{
2124 int ret = 0;
2125
2126 mutex_lock(&clk_debug_lock);
2127 hlist_add_head(&core->debug_node, &clk_debug_list);
2128
2129 if (!inited)
2130 goto unlock;
2131
2132 ret = clk_debug_create_one(core, rootdir);
2133unlock:
2134 mutex_unlock(&clk_debug_lock);
2135
2136 return ret;
2137}
2138
2139 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002140 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2141 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002142 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002143 * Dynamically removes a clk and all its child nodes from the
2144 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002145 * clk_debug_register in __clk_init.
2146 */
2147static void clk_debug_unregister(struct clk_core *core)
2148{
2149 mutex_lock(&clk_debug_lock);
2150 hlist_del_init(&core->debug_node);
2151 debugfs_remove_recursive(core->dentry);
2152 core->dentry = NULL;
2153 mutex_unlock(&clk_debug_lock);
2154}
2155
2156struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2157 void *data, const struct file_operations *fops)
2158{
2159 struct dentry *d = NULL;
2160
2161 if (hw->core->dentry)
2162 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2163 fops);
2164
2165 return d;
2166}
2167EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2168
2169/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002170 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002171 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002172 * clks are often initialized very early during boot before memory can be
2173 * dynamically allocated and well before debugfs is setup. This function
2174 * populates the debugfs clk directory once at boot-time when we know that
2175 * debugfs is setup. It should only be called once at boot-time, all other clks
2176 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002177 */
2178static int __init clk_debug_init(void)
2179{
2180 struct clk_core *core;
2181 struct dentry *d;
2182
2183 rootdir = debugfs_create_dir("clk", NULL);
2184
2185 if (!rootdir)
2186 return -ENOMEM;
2187
2188 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2189 &clk_summary_fops);
2190 if (!d)
2191 return -ENOMEM;
2192
2193 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2194 &clk_dump_fops);
2195 if (!d)
2196 return -ENOMEM;
2197
2198 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2199 &orphan_list, &clk_summary_fops);
2200 if (!d)
2201 return -ENOMEM;
2202
2203 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2204 &orphan_list, &clk_dump_fops);
2205 if (!d)
2206 return -ENOMEM;
2207
2208 mutex_lock(&clk_debug_lock);
2209 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2210 clk_debug_create_one(core, rootdir);
2211
2212 inited = 1;
2213 mutex_unlock(&clk_debug_lock);
2214
2215 return 0;
2216}
2217late_initcall(clk_debug_init);
2218#else
2219static inline int clk_debug_register(struct clk_core *core) { return 0; }
2220static inline void clk_debug_reparent(struct clk_core *core,
2221 struct clk_core *new_parent)
2222{
2223}
2224static inline void clk_debug_unregister(struct clk_core *core)
2225{
2226}
2227#endif
2228
Michael Turquette3d3801e2015-02-25 09:11:01 -08002229/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002230 * __clk_init - initialize the data structures in a struct clk
2231 * @dev: device initializing this clk, placeholder for now
2232 * @clk: clk being initialized
2233 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002234 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002235 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002236 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002237static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002238{
Mike Turquetted1302a32012-03-29 14:30:40 -07002239 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002240 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002241 struct hlist_node *tmp2;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002242 struct clk_core *core;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002243 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002244
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002245 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002246 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002247
Stephen Boydd6968fc2015-04-30 13:54:13 -07002248 core = clk_user->core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002249
Mike Turquetteeab89f62013-03-28 13:59:01 -07002250 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002251
2252 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002253 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002254 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002255 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002256 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002257 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002258 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002259
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002260 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002261 if (core->ops->set_rate &&
2262 !((core->ops->round_rate || core->ops->determine_rate) &&
2263 core->ops->recalc_rate)) {
James Hogan71472c02013-07-29 12:25:00 +01002264 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002265 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002266 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002267 goto out;
2268 }
2269
Stephen Boydd6968fc2015-04-30 13:54:13 -07002270 if (core->ops->set_parent && !core->ops->get_parent) {
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002271 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002272 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002273 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002274 goto out;
2275 }
2276
Stephen Boydd6968fc2015-04-30 13:54:13 -07002277 if (core->ops->set_rate_and_parent &&
2278 !(core->ops->set_parent && core->ops->set_rate)) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002279 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002280 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002281 ret = -EINVAL;
2282 goto out;
2283 }
2284
Mike Turquetteb24764902012-03-15 23:11:19 -07002285 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002286 for (i = 0; i < core->num_parents; i++)
2287 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002288 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002289 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002290
2291 /*
2292 * Allocate an array of struct clk *'s to avoid unnecessary string
2293 * look-ups of clk's possible parents. This can fail for clocks passed
Stephen Boydd6968fc2015-04-30 13:54:13 -07002294 * in to clk_init during early boot; thus any access to core->parents[]
Mike Turquetteb24764902012-03-15 23:11:19 -07002295 * must always check for a NULL pointer and try to populate it if
2296 * necessary.
2297 *
Stephen Boydd6968fc2015-04-30 13:54:13 -07002298 * If core->parents is not NULL we skip this entire block. This allows
2299 * for clock drivers to statically initialize core->parents.
Mike Turquetteb24764902012-03-15 23:11:19 -07002300 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002301 if (core->num_parents > 1 && !core->parents) {
2302 core->parents = kcalloc(core->num_parents, sizeof(struct clk *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002303 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002304 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002305 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002306 * clk_init'd; thus any access to clk->parents[] must check
2307 * for a NULL pointer. We can always perform lazy lookups for
2308 * missing parents later on.
2309 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002310 if (core->parents)
2311 for (i = 0; i < core->num_parents; i++)
2312 core->parents[i] =
2313 clk_core_lookup(core->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002314 }
2315
Stephen Boydd6968fc2015-04-30 13:54:13 -07002316 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002317
2318 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07002319 * Populate core->parent if parent has already been __clk_init'd. If
Mike Turquetteb24764902012-03-15 23:11:19 -07002320 * parent has not yet been __clk_init'd then place clk in the orphan
2321 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2322 * clk list.
2323 *
2324 * Every time a new clk is clk_init'd then we walk the list of orphan
2325 * clocks and re-parent any that are children of the clock currently
2326 * being clk_init'd.
2327 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002328 if (core->parent)
2329 hlist_add_head(&core->child_node,
2330 &core->parent->children);
2331 else if (core->flags & CLK_IS_ROOT)
2332 hlist_add_head(&core->child_node, &clk_root_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002333 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002334 hlist_add_head(&core->child_node, &clk_orphan_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002335
2336 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002337 * Set clk's accuracy. The preferred method is to use
2338 * .recalc_accuracy. For simple clocks and lazy developers the default
2339 * fallback is to use the parent's accuracy. If a clock doesn't have a
2340 * parent (or is orphaned) then accuracy is set to zero (perfect
2341 * clock).
2342 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002343 if (core->ops->recalc_accuracy)
2344 core->accuracy = core->ops->recalc_accuracy(core->hw,
2345 __clk_get_accuracy(core->parent));
2346 else if (core->parent)
2347 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002348 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002349 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002350
2351 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002352 * Set clk's phase.
2353 * Since a phase is by definition relative to its parent, just
2354 * query the current clock phase, or just assume it's in phase.
2355 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002356 if (core->ops->get_phase)
2357 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002358 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002359 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002360
2361 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002362 * Set clk's rate. The preferred method is to use .recalc_rate. For
2363 * simple clocks and lazy developers the default fallback is to use the
2364 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2365 * then rate is set to zero.
2366 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002367 if (core->ops->recalc_rate)
2368 rate = core->ops->recalc_rate(core->hw,
2369 clk_core_get_rate_nolock(core->parent));
2370 else if (core->parent)
2371 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002372 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002373 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002374 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002375
2376 /*
2377 * walk the list of orphan clocks and reparent any that are children of
2378 * this clock
2379 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002380 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002381 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002382 i = orphan->ops->get_parent(orphan->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002383 if (!strcmp(core->name, orphan->parent_names[i]))
2384 clk_core_reparent(orphan, core);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002385 continue;
2386 }
2387
Mike Turquetteb24764902012-03-15 23:11:19 -07002388 for (i = 0; i < orphan->num_parents; i++)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002389 if (!strcmp(core->name, orphan->parent_names[i])) {
2390 clk_core_reparent(orphan, core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002391 break;
2392 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002393 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002394
2395 /*
2396 * optional platform-specific magic
2397 *
2398 * The .init callback is not used by any of the basic clock types, but
2399 * exists for weird hardware that must perform initialization magic.
2400 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002401 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002402 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002403 if (core->ops->init)
2404 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002405
Stephen Boydd6968fc2015-04-30 13:54:13 -07002406 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002407out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002408 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002409
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002410 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002411 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002412
Mike Turquetted1302a32012-03-29 14:30:40 -07002413 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002414}
2415
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002416struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2417 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002418{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002419 struct clk *clk;
2420
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002421 /* This is to allow this function to be chained to others */
2422 if (!hw || IS_ERR(hw))
2423 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002424
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002425 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2426 if (!clk)
2427 return ERR_PTR(-ENOMEM);
2428
2429 clk->core = hw->core;
2430 clk->dev_id = dev_id;
2431 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002432 clk->max_rate = ULONG_MAX;
2433
2434 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002435 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002436 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002437
2438 return clk;
2439}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002440
Stephen Boyd73e0e492015-02-06 11:42:43 -08002441void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002442{
2443 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002444 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002445 clk_prepare_unlock();
2446
2447 kfree(clk);
2448}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002449
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002450/**
2451 * clk_register - allocate a new clock, register it and return an opaque cookie
2452 * @dev: device that is registering this clock
2453 * @hw: link to hardware-specific clock data
2454 *
2455 * clk_register is the primary interface for populating the clock tree with new
2456 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05302457 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002458 * rest of the clock API. In the event of an error clk_register will return an
2459 * error code; drivers must test for an error code after calling clk_register.
2460 */
2461struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002462{
Mike Turquetted1302a32012-03-29 14:30:40 -07002463 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002464 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002465
Stephen Boydd6968fc2015-04-30 13:54:13 -07002466 core = kzalloc(sizeof(*core), GFP_KERNEL);
2467 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002468 ret = -ENOMEM;
2469 goto fail_out;
2470 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002471
Stephen Boydd6968fc2015-04-30 13:54:13 -07002472 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2473 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002474 ret = -ENOMEM;
2475 goto fail_name;
2476 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002477 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002478 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002479 core->owner = dev->driver->owner;
2480 core->hw = hw;
2481 core->flags = hw->init->flags;
2482 core->num_parents = hw->init->num_parents;
2483 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002484
Mike Turquetted1302a32012-03-29 14:30:40 -07002485 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002486 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002487 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002488
Stephen Boydd6968fc2015-04-30 13:54:13 -07002489 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002490 ret = -ENOMEM;
2491 goto fail_parent_names;
2492 }
2493
2494
2495 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002496 for (i = 0; i < core->num_parents; i++) {
2497 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002498 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002499 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002500 ret = -ENOMEM;
2501 goto fail_parent_names_copy;
2502 }
2503 }
2504
Stephen Boydd6968fc2015-04-30 13:54:13 -07002505 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002506
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002507 hw->clk = __clk_create_clk(hw, NULL, NULL);
2508 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002509 ret = PTR_ERR(hw->clk);
2510 goto fail_parent_names_copy;
2511 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002512
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002513 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002514 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002515 return hw->clk;
2516
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002517 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002518 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002519
2520fail_parent_names_copy:
2521 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002522 kfree_const(core->parent_names[i]);
2523 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07002524fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002525 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002526fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002527 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002528fail_out:
2529 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002530}
2531EXPORT_SYMBOL_GPL(clk_register);
2532
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002533/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002534static void __clk_release(struct kref *ref)
2535{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002536 struct clk_core *core = container_of(ref, struct clk_core, ref);
2537 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002538
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002539 lockdep_assert_held(&prepare_lock);
2540
Stephen Boydd6968fc2015-04-30 13:54:13 -07002541 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002542 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002543 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002544
Stephen Boydd6968fc2015-04-30 13:54:13 -07002545 kfree(core->parent_names);
2546 kfree_const(core->name);
2547 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002548}
2549
2550/*
2551 * Empty clk_ops for unregistered clocks. These are used temporarily
2552 * after clk_unregister() was called on a clock and until last clock
2553 * consumer calls clk_put() and the struct clk object is freed.
2554 */
2555static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2556{
2557 return -ENXIO;
2558}
2559
2560static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2561{
2562 WARN_ON_ONCE(1);
2563}
2564
2565static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2566 unsigned long parent_rate)
2567{
2568 return -ENXIO;
2569}
2570
2571static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2572{
2573 return -ENXIO;
2574}
2575
2576static const struct clk_ops clk_nodrv_ops = {
2577 .enable = clk_nodrv_prepare_enable,
2578 .disable = clk_nodrv_disable_unprepare,
2579 .prepare = clk_nodrv_prepare_enable,
2580 .unprepare = clk_nodrv_disable_unprepare,
2581 .set_rate = clk_nodrv_set_rate,
2582 .set_parent = clk_nodrv_set_parent,
2583};
2584
Mark Brown1df5c932012-04-18 09:07:12 +01002585/**
2586 * clk_unregister - unregister a currently registered clock
2587 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002588 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002589void clk_unregister(struct clk *clk)
2590{
2591 unsigned long flags;
2592
Stephen Boyd6314b672014-09-04 23:37:49 -07002593 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2594 return;
2595
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002596 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002597
2598 clk_prepare_lock();
2599
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002600 if (clk->core->ops == &clk_nodrv_ops) {
2601 pr_err("%s: unregistered clock: %s\n", __func__,
2602 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002603 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002604 }
2605 /*
2606 * Assign empty clock ops for consumers that might still hold
2607 * a reference to this clock.
2608 */
2609 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002610 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002611 clk_enable_unlock(flags);
2612
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002613 if (!hlist_empty(&clk->core->children)) {
2614 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002615 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002616
2617 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002618 hlist_for_each_entry_safe(child, t, &clk->core->children,
2619 child_node)
2620 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002621 }
2622
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002623 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002624
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002625 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002626 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002627 __func__, clk->core->name);
2628 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002629
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002630 clk_prepare_unlock();
2631}
Mark Brown1df5c932012-04-18 09:07:12 +01002632EXPORT_SYMBOL_GPL(clk_unregister);
2633
Stephen Boyd46c87732012-09-24 13:38:04 -07002634static void devm_clk_release(struct device *dev, void *res)
2635{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002636 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002637}
2638
2639/**
2640 * devm_clk_register - resource managed clk_register()
2641 * @dev: device that is registering this clock
2642 * @hw: link to hardware-specific clock data
2643 *
2644 * Managed clk_register(). Clocks returned from this function are
2645 * automatically clk_unregister()ed on driver detach. See clk_register() for
2646 * more information.
2647 */
2648struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2649{
2650 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002651 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002652
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002653 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2654 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002655 return ERR_PTR(-ENOMEM);
2656
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002657 clk = clk_register(dev, hw);
2658 if (!IS_ERR(clk)) {
2659 *clkp = clk;
2660 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002661 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002662 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002663 }
2664
2665 return clk;
2666}
2667EXPORT_SYMBOL_GPL(devm_clk_register);
2668
2669static int devm_clk_match(struct device *dev, void *res, void *data)
2670{
2671 struct clk *c = res;
2672 if (WARN_ON(!c))
2673 return 0;
2674 return c == data;
2675}
2676
2677/**
2678 * devm_clk_unregister - resource managed clk_unregister()
2679 * @clk: clock to unregister
2680 *
2681 * Deallocate a clock allocated with devm_clk_register(). Normally
2682 * this function will not need to be called and the resource management
2683 * code will ensure that the resource is freed.
2684 */
2685void devm_clk_unregister(struct device *dev, struct clk *clk)
2686{
2687 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2688}
2689EXPORT_SYMBOL_GPL(devm_clk_unregister);
2690
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002691/*
2692 * clkdev helpers
2693 */
2694int __clk_get(struct clk *clk)
2695{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002696 struct clk_core *core = !clk ? NULL : clk->core;
2697
2698 if (core) {
2699 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002700 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002701
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002702 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002703 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002704 return 1;
2705}
2706
2707void __clk_put(struct clk *clk)
2708{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002709 struct module *owner;
2710
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002711 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002712 return;
2713
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002714 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002715
Stephen Boyd50595f82015-02-06 11:42:44 -08002716 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002717 if (clk->min_rate > clk->core->req_rate ||
2718 clk->max_rate < clk->core->req_rate)
2719 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2720
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002721 owner = clk->core->owner;
2722 kref_put(&clk->core->ref, __clk_release);
2723
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002724 clk_prepare_unlock();
2725
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002726 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002727
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002728 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002729}
2730
Mike Turquetteb24764902012-03-15 23:11:19 -07002731/*** clk rate change notifiers ***/
2732
2733/**
2734 * clk_notifier_register - add a clk rate change notifier
2735 * @clk: struct clk * to watch
2736 * @nb: struct notifier_block * with callback info
2737 *
2738 * Request notification when clk's rate changes. This uses an SRCU
2739 * notifier because we want it to block and notifier unregistrations are
2740 * uncommon. The callbacks associated with the notifier must not
2741 * re-enter into the clk framework by calling any top-level clk APIs;
2742 * this will cause a nested prepare_lock mutex.
2743 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002744 * In all notification cases cases (pre, post and abort rate change) the
2745 * original clock rate is passed to the callback via struct
2746 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002747 * clk_notifier_data.new_rate.
2748 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002749 * clk_notifier_register() must be called from non-atomic context.
2750 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2751 * allocation failure; otherwise, passes along the return value of
2752 * srcu_notifier_chain_register().
2753 */
2754int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2755{
2756 struct clk_notifier *cn;
2757 int ret = -ENOMEM;
2758
2759 if (!clk || !nb)
2760 return -EINVAL;
2761
Mike Turquetteeab89f62013-03-28 13:59:01 -07002762 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002763
2764 /* search the list of notifiers for this clk */
2765 list_for_each_entry(cn, &clk_notifier_list, node)
2766 if (cn->clk == clk)
2767 break;
2768
2769 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2770 if (cn->clk != clk) {
2771 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2772 if (!cn)
2773 goto out;
2774
2775 cn->clk = clk;
2776 srcu_init_notifier_head(&cn->notifier_head);
2777
2778 list_add(&cn->node, &clk_notifier_list);
2779 }
2780
2781 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2782
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002783 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002784
2785out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002786 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002787
2788 return ret;
2789}
2790EXPORT_SYMBOL_GPL(clk_notifier_register);
2791
2792/**
2793 * clk_notifier_unregister - remove a clk rate change notifier
2794 * @clk: struct clk *
2795 * @nb: struct notifier_block * with callback info
2796 *
2797 * Request no further notification for changes to 'clk' and frees memory
2798 * allocated in clk_notifier_register.
2799 *
2800 * Returns -EINVAL if called with null arguments; otherwise, passes
2801 * along the return value of srcu_notifier_chain_unregister().
2802 */
2803int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2804{
2805 struct clk_notifier *cn = NULL;
2806 int ret = -EINVAL;
2807
2808 if (!clk || !nb)
2809 return -EINVAL;
2810
Mike Turquetteeab89f62013-03-28 13:59:01 -07002811 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002812
2813 list_for_each_entry(cn, &clk_notifier_list, node)
2814 if (cn->clk == clk)
2815 break;
2816
2817 if (cn->clk == clk) {
2818 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2819
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002820 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002821
2822 /* XXX the notifier code should handle this better */
2823 if (!cn->notifier_head.head) {
2824 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002825 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002826 kfree(cn);
2827 }
2828
2829 } else {
2830 ret = -ENOENT;
2831 }
2832
Mike Turquetteeab89f62013-03-28 13:59:01 -07002833 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002834
2835 return ret;
2836}
2837EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002838
2839#ifdef CONFIG_OF
2840/**
2841 * struct of_clk_provider - Clock provider registration structure
2842 * @link: Entry in global list of clock providers
2843 * @node: Pointer to device tree node of clock provider
2844 * @get: Get clock callback. Returns NULL or a struct clk for the
2845 * given clock specifier
2846 * @data: context pointer to be passed into @get callback
2847 */
2848struct of_clk_provider {
2849 struct list_head link;
2850
2851 struct device_node *node;
2852 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2853 void *data;
2854};
2855
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302856static const struct of_device_id __clk_of_table_sentinel
2857 __used __section(__clk_of_table_end);
2858
Grant Likely766e6a42012-04-09 14:50:06 -05002859static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002860static DEFINE_MUTEX(of_clk_mutex);
2861
Grant Likely766e6a42012-04-09 14:50:06 -05002862struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2863 void *data)
2864{
2865 return data;
2866}
2867EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2868
Shawn Guo494bfec2012-08-22 21:36:27 +08002869struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2870{
2871 struct clk_onecell_data *clk_data = data;
2872 unsigned int idx = clkspec->args[0];
2873
2874 if (idx >= clk_data->clk_num) {
2875 pr_err("%s: invalid clock index %d\n", __func__, idx);
2876 return ERR_PTR(-EINVAL);
2877 }
2878
2879 return clk_data->clks[idx];
2880}
2881EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2882
Grant Likely766e6a42012-04-09 14:50:06 -05002883/**
2884 * of_clk_add_provider() - Register a clock provider for a node
2885 * @np: Device node pointer associated with clock provider
2886 * @clk_src_get: callback for decoding clock
2887 * @data: context pointer for @clk_src_get callback.
2888 */
2889int of_clk_add_provider(struct device_node *np,
2890 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2891 void *data),
2892 void *data)
2893{
2894 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002895 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002896
2897 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2898 if (!cp)
2899 return -ENOMEM;
2900
2901 cp->node = of_node_get(np);
2902 cp->data = data;
2903 cp->get = clk_src_get;
2904
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002905 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002906 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002907 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002908 pr_debug("Added clock from %s\n", np->full_name);
2909
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002910 ret = of_clk_set_defaults(np, true);
2911 if (ret < 0)
2912 of_clk_del_provider(np);
2913
2914 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002915}
2916EXPORT_SYMBOL_GPL(of_clk_add_provider);
2917
2918/**
2919 * of_clk_del_provider() - Remove a previously registered clock provider
2920 * @np: Device node pointer associated with clock provider
2921 */
2922void of_clk_del_provider(struct device_node *np)
2923{
2924 struct of_clk_provider *cp;
2925
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002926 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002927 list_for_each_entry(cp, &of_clk_providers, link) {
2928 if (cp->node == np) {
2929 list_del(&cp->link);
2930 of_node_put(cp->node);
2931 kfree(cp);
2932 break;
2933 }
2934 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002935 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002936}
2937EXPORT_SYMBOL_GPL(of_clk_del_provider);
2938
Stephen Boyd73e0e492015-02-06 11:42:43 -08002939struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
2940 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05002941{
2942 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002943 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002944
Stephen Boyd306c3422015-02-05 15:39:11 -08002945 if (!clkspec)
2946 return ERR_PTR(-EINVAL);
2947
Grant Likely766e6a42012-04-09 14:50:06 -05002948 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08002949 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002950 list_for_each_entry(provider, &of_clk_providers, link) {
2951 if (provider->node == clkspec->np)
2952 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002953 if (!IS_ERR(clk)) {
2954 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
2955 con_id);
2956
2957 if (!IS_ERR(clk) && !__clk_get(clk)) {
2958 __clk_free_clk(clk);
2959 clk = ERR_PTR(-ENOENT);
2960 }
2961
Grant Likely766e6a42012-04-09 14:50:06 -05002962 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08002963 }
Grant Likely766e6a42012-04-09 14:50:06 -05002964 }
Stephen Boyd306c3422015-02-05 15:39:11 -08002965 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002966
2967 return clk;
2968}
2969
Stephen Boyd306c3422015-02-05 15:39:11 -08002970/**
2971 * of_clk_get_from_provider() - Lookup a clock from a clock provider
2972 * @clkspec: pointer to a clock specifier data structure
2973 *
2974 * This function looks up a struct clk from the registered list of clock
2975 * providers, an input is a clock specifier data structure as returned
2976 * from the of_parse_phandle_with_args() function call.
2977 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002978struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2979{
Stephen Boyd306c3422015-02-05 15:39:11 -08002980 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05002981}
2982
Mike Turquettef6102742013-10-07 23:12:13 -07002983int of_clk_get_parent_count(struct device_node *np)
2984{
2985 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2986}
2987EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2988
Grant Likely766e6a42012-04-09 14:50:06 -05002989const char *of_clk_get_parent_name(struct device_node *np, int index)
2990{
2991 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002992 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05002993 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002994 const __be32 *vp;
2995 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05002996 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002997 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05002998
2999 if (index < 0)
3000 return NULL;
3001
3002 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3003 &clkspec);
3004 if (rc)
3005 return NULL;
3006
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003007 index = clkspec.args_count ? clkspec.args[0] : 0;
3008 count = 0;
3009
3010 /* if there is an indices property, use it to transfer the index
3011 * specified into an array offset for the clock-output-names property.
3012 */
3013 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3014 if (index == pv) {
3015 index = count;
3016 break;
3017 }
3018 count++;
3019 }
3020
Grant Likely766e6a42012-04-09 14:50:06 -05003021 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003022 index,
Grant Likely766e6a42012-04-09 14:50:06 -05003023 &clk_name) < 0)
3024 clk_name = clkspec.np->name;
3025
3026 of_node_put(clkspec.np);
3027 return clk_name;
3028}
3029EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3030
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05003031/**
3032 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3033 * number of parents
3034 * @np: Device node pointer associated with clock provider
3035 * @parents: pointer to char array that hold the parents' names
3036 * @size: size of the @parents array
3037 *
3038 * Return: number of parents for the clock node.
3039 */
3040int of_clk_parent_fill(struct device_node *np, const char **parents,
3041 unsigned int size)
3042{
3043 unsigned int i = 0;
3044
3045 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3046 i++;
3047
3048 return i;
3049}
3050EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3051
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003052struct clock_provider {
3053 of_clk_init_cb_t clk_init_cb;
3054 struct device_node *np;
3055 struct list_head node;
3056};
3057
3058static LIST_HEAD(clk_provider_list);
3059
3060/*
3061 * This function looks for a parent clock. If there is one, then it
3062 * checks that the provider for this parent clock was initialized, in
3063 * this case the parent clock will be ready.
3064 */
3065static int parent_ready(struct device_node *np)
3066{
3067 int i = 0;
3068
3069 while (true) {
3070 struct clk *clk = of_clk_get(np, i);
3071
3072 /* this parent is ready we can check the next one */
3073 if (!IS_ERR(clk)) {
3074 clk_put(clk);
3075 i++;
3076 continue;
3077 }
3078
3079 /* at least one parent is not ready, we exit now */
3080 if (PTR_ERR(clk) == -EPROBE_DEFER)
3081 return 0;
3082
3083 /*
3084 * Here we make assumption that the device tree is
3085 * written correctly. So an error means that there is
3086 * no more parent. As we didn't exit yet, then the
3087 * previous parent are ready. If there is no clock
3088 * parent, no need to wait for them, then we can
3089 * consider their absence as being ready
3090 */
3091 return 1;
3092 }
3093}
3094
Grant Likely766e6a42012-04-09 14:50:06 -05003095/**
3096 * of_clk_init() - Scan and init clock providers from the DT
3097 * @matches: array of compatible values and init functions for providers.
3098 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003099 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003100 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003101 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003102 */
3103void __init of_clk_init(const struct of_device_id *matches)
3104{
Alex Elder7f7ed582013-08-22 11:31:31 -05003105 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003106 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003107 struct clock_provider *clk_provider, *next;
3108 bool is_init_done;
3109 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05003110
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303111 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003112 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303113
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003114 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003115 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003116 struct clock_provider *parent =
3117 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
3118
3119 parent->clk_init_cb = match->data;
3120 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003121 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003122 }
3123
3124 while (!list_empty(&clk_provider_list)) {
3125 is_init_done = false;
3126 list_for_each_entry_safe(clk_provider, next,
3127 &clk_provider_list, node) {
3128 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003129
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003130 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003131 of_clk_set_defaults(clk_provider->np, true);
3132
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003133 list_del(&clk_provider->node);
3134 kfree(clk_provider);
3135 is_init_done = true;
3136 }
3137 }
3138
3139 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003140 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003141 * remaining providers during the last loop, so now we
3142 * initialize all the remaining ones unconditionally
3143 * in case the clock parent was not mandatory
3144 */
3145 if (!is_init_done)
3146 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003147 }
3148}
3149#endif