blob: 059e5d25c9bac688c794224cc8c92a02bebb23c8 [file] [log] [blame]
Mike Turquetteb24764902012-03-15 23:11:19 -07001/*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Standard functionality for the common clock API. See Documentation/clk.txt
10 */
11
Michael Turquetteb09d6d92015-01-29 14:22:50 -080012#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020013#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070014#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/spinlock.h>
17#include <linux/err.h>
18#include <linux/list.h>
19#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050020#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070021#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053022#include <linux/init.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070023#include <linux/sched.h>
Stephen Boyd562ef0b2015-05-01 12:16:14 -070024#include <linux/clkdev.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070025
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020026#include "clk.h"
27
Mike Turquetteb24764902012-03-15 23:11:19 -070028static DEFINE_SPINLOCK(enable_lock);
29static DEFINE_MUTEX(prepare_lock);
30
Mike Turquette533ddeb2013-03-28 13:59:02 -070031static struct task_struct *prepare_owner;
32static struct task_struct *enable_owner;
33
34static int prepare_refcnt;
35static int enable_refcnt;
36
Mike Turquetteb24764902012-03-15 23:11:19 -070037static HLIST_HEAD(clk_root_list);
38static HLIST_HEAD(clk_orphan_list);
39static LIST_HEAD(clk_notifier_list);
40
Michael Turquetteb09d6d92015-01-29 14:22:50 -080041/*** private data structures ***/
42
43struct clk_core {
44 const char *name;
45 const struct clk_ops *ops;
46 struct clk_hw *hw;
47 struct module *owner;
48 struct clk_core *parent;
49 const char **parent_names;
50 struct clk_core **parents;
51 u8 num_parents;
52 u8 new_parent_index;
53 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010054 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080055 unsigned long new_rate;
56 struct clk_core *new_parent;
57 struct clk_core *new_child;
58 unsigned long flags;
59 unsigned int enable_count;
60 unsigned int prepare_count;
61 unsigned long accuracy;
62 int phase;
63 struct hlist_head children;
64 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010065 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080066 unsigned int notifier_count;
67#ifdef CONFIG_DEBUG_FS
68 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020069 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080070#endif
71 struct kref ref;
72};
73
Stephen Boyddfc202e2015-02-02 14:37:41 -080074#define CREATE_TRACE_POINTS
75#include <trace/events/clk.h>
76
Michael Turquetteb09d6d92015-01-29 14:22:50 -080077struct clk {
78 struct clk_core *core;
79 const char *dev_id;
80 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010081 unsigned long min_rate;
82 unsigned long max_rate;
Stephen Boyd50595f82015-02-06 11:42:44 -080083 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080084};
85
Mike Turquetteeab89f62013-03-28 13:59:01 -070086/*** locking ***/
87static void clk_prepare_lock(void)
88{
Mike Turquette533ddeb2013-03-28 13:59:02 -070089 if (!mutex_trylock(&prepare_lock)) {
90 if (prepare_owner == current) {
91 prepare_refcnt++;
92 return;
93 }
94 mutex_lock(&prepare_lock);
95 }
96 WARN_ON_ONCE(prepare_owner != NULL);
97 WARN_ON_ONCE(prepare_refcnt != 0);
98 prepare_owner = current;
99 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700100}
101
102static void clk_prepare_unlock(void)
103{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700104 WARN_ON_ONCE(prepare_owner != current);
105 WARN_ON_ONCE(prepare_refcnt == 0);
106
107 if (--prepare_refcnt)
108 return;
109 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700110 mutex_unlock(&prepare_lock);
111}
112
113static unsigned long clk_enable_lock(void)
114{
115 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700116
117 if (!spin_trylock_irqsave(&enable_lock, flags)) {
118 if (enable_owner == current) {
119 enable_refcnt++;
120 return flags;
121 }
122 spin_lock_irqsave(&enable_lock, flags);
123 }
124 WARN_ON_ONCE(enable_owner != NULL);
125 WARN_ON_ONCE(enable_refcnt != 0);
126 enable_owner = current;
127 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700128 return flags;
129}
130
131static void clk_enable_unlock(unsigned long flags)
132{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700133 WARN_ON_ONCE(enable_owner != current);
134 WARN_ON_ONCE(enable_refcnt == 0);
135
136 if (--enable_refcnt)
137 return;
138 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700139 spin_unlock_irqrestore(&enable_lock, flags);
140}
141
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700142static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530143{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700144 /*
145 * .is_prepared is optional for clocks that can prepare
146 * fall back to software usage counter if it is missing
147 */
148 if (!core->ops->is_prepared)
149 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530150
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700151 return core->ops->is_prepared(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530152}
153
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700154static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530155{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700156 /*
157 * .is_enabled is only mandatory for clocks that gate
158 * fall back to software usage counter if .is_enabled is missing
159 */
160 if (!core->ops->is_enabled)
161 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530162
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700163 return core->ops->is_enabled(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530164}
165
Stephen Boydd6968fc2015-04-30 13:54:13 -0700166static void clk_unprepare_unused_subtree(struct clk_core *core)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100167{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100168 struct clk_core *child;
Ulf Hansson1c155b32013-03-12 20:26:03 +0100169
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100170 lockdep_assert_held(&prepare_lock);
171
Stephen Boydd6968fc2015-04-30 13:54:13 -0700172 hlist_for_each_entry(child, &core->children, child_node)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100173 clk_unprepare_unused_subtree(child);
174
Stephen Boydd6968fc2015-04-30 13:54:13 -0700175 if (core->prepare_count)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100176 return;
177
Stephen Boydd6968fc2015-04-30 13:54:13 -0700178 if (core->flags & CLK_IGNORE_UNUSED)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100179 return;
180
Stephen Boydd6968fc2015-04-30 13:54:13 -0700181 if (clk_core_is_prepared(core)) {
182 trace_clk_unprepare(core);
183 if (core->ops->unprepare_unused)
184 core->ops->unprepare_unused(core->hw);
185 else if (core->ops->unprepare)
186 core->ops->unprepare(core->hw);
187 trace_clk_unprepare_complete(core);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100188 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100189}
190
Stephen Boydd6968fc2015-04-30 13:54:13 -0700191static void clk_disable_unused_subtree(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700192{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100193 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700194 unsigned long flags;
195
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100196 lockdep_assert_held(&prepare_lock);
197
Stephen Boydd6968fc2015-04-30 13:54:13 -0700198 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700199 clk_disable_unused_subtree(child);
200
Mike Turquetteeab89f62013-03-28 13:59:01 -0700201 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700202
Stephen Boydd6968fc2015-04-30 13:54:13 -0700203 if (core->enable_count)
Mike Turquetteb24764902012-03-15 23:11:19 -0700204 goto unlock_out;
205
Stephen Boydd6968fc2015-04-30 13:54:13 -0700206 if (core->flags & CLK_IGNORE_UNUSED)
Mike Turquetteb24764902012-03-15 23:11:19 -0700207 goto unlock_out;
208
Mike Turquette7c045a52012-12-04 11:00:35 -0800209 /*
210 * some gate clocks have special needs during the disable-unused
211 * sequence. call .disable_unused if available, otherwise fall
212 * back to .disable
213 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700214 if (clk_core_is_enabled(core)) {
215 trace_clk_disable(core);
216 if (core->ops->disable_unused)
217 core->ops->disable_unused(core->hw);
218 else if (core->ops->disable)
219 core->ops->disable(core->hw);
220 trace_clk_disable_complete(core);
Mike Turquette7c045a52012-12-04 11:00:35 -0800221 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700222
223unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700224 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700225}
226
Olof Johansson1e435252013-04-27 14:10:18 -0700227static bool clk_ignore_unused;
228static int __init clk_ignore_unused_setup(char *__unused)
229{
230 clk_ignore_unused = true;
231 return 1;
232}
233__setup("clk_ignore_unused", clk_ignore_unused_setup);
234
Mike Turquetteb24764902012-03-15 23:11:19 -0700235static int clk_disable_unused(void)
236{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700237 struct clk_core *core;
Mike Turquetteb24764902012-03-15 23:11:19 -0700238
Olof Johansson1e435252013-04-27 14:10:18 -0700239 if (clk_ignore_unused) {
240 pr_warn("clk: Not disabling unused clocks\n");
241 return 0;
242 }
243
Mike Turquetteeab89f62013-03-28 13:59:01 -0700244 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700245
Stephen Boydd6968fc2015-04-30 13:54:13 -0700246 hlist_for_each_entry(core, &clk_root_list, child_node)
247 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700248
Stephen Boydd6968fc2015-04-30 13:54:13 -0700249 hlist_for_each_entry(core, &clk_orphan_list, child_node)
250 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700251
Stephen Boydd6968fc2015-04-30 13:54:13 -0700252 hlist_for_each_entry(core, &clk_root_list, child_node)
253 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100254
Stephen Boydd6968fc2015-04-30 13:54:13 -0700255 hlist_for_each_entry(core, &clk_orphan_list, child_node)
256 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100257
Mike Turquetteeab89f62013-03-28 13:59:01 -0700258 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700259
260 return 0;
261}
Saravana Kannand41d5802013-05-09 11:35:01 -0700262late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700263
264/*** helper functions ***/
265
Russ Dill65800b22012-11-26 11:20:09 -0800266const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700267{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100268 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700269}
Niels de Vos48950842012-12-13 13:12:25 +0100270EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700271
Russ Dill65800b22012-11-26 11:20:09 -0800272struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700273{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100274 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700275}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800276EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700277
Russ Dill65800b22012-11-26 11:20:09 -0800278u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700279{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100280 return !clk ? 0 : clk->core->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700281}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800282EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700283
Russ Dill65800b22012-11-26 11:20:09 -0800284struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700285{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100286 if (!clk)
287 return NULL;
288
289 /* TODO: Create a per-user clk and change callers to call clk_put */
290 return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700291}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800292EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700293
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700294static struct clk_core *__clk_lookup_subtree(const char *name,
295 struct clk_core *core)
296{
297 struct clk_core *child;
298 struct clk_core *ret;
299
300 if (!strcmp(core->name, name))
301 return core;
302
303 hlist_for_each_entry(child, &core->children, child_node) {
304 ret = __clk_lookup_subtree(name, child);
305 if (ret)
306 return ret;
307 }
308
309 return NULL;
310}
311
312static struct clk_core *clk_core_lookup(const char *name)
313{
314 struct clk_core *root_clk;
315 struct clk_core *ret;
316
317 if (!name)
318 return NULL;
319
320 /* search the 'proper' clk tree first */
321 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
322 ret = __clk_lookup_subtree(name, root_clk);
323 if (ret)
324 return ret;
325 }
326
327 /* if not found, then search the orphan tree */
328 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
329 ret = __clk_lookup_subtree(name, root_clk);
330 if (ret)
331 return ret;
332 }
333
334 return NULL;
335}
336
Stephen Boydd6968fc2015-04-30 13:54:13 -0700337static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100338 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100339{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700340 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100341 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700342 else if (!core->parents)
343 return clk_core_lookup(core->parent_names[index]);
344 else if (!core->parents[index])
345 return core->parents[index] =
346 clk_core_lookup(core->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100347 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700348 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100349}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100350
351struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
352{
353 struct clk_core *parent;
354
355 if (!clk)
356 return NULL;
357
358 parent = clk_core_get_parent_by_index(clk->core, index);
359
360 return !parent ? NULL : parent->hw->clk;
361}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800362EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100363
Russ Dill65800b22012-11-26 11:20:09 -0800364unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700365{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100366 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700367}
368
Stephen Boydd6968fc2015-04-30 13:54:13 -0700369static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700370{
371 unsigned long ret;
372
Stephen Boydd6968fc2015-04-30 13:54:13 -0700373 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530374 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700375 goto out;
376 }
377
Stephen Boydd6968fc2015-04-30 13:54:13 -0700378 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700379
Stephen Boydd6968fc2015-04-30 13:54:13 -0700380 if (core->flags & CLK_IS_ROOT)
Mike Turquetteb24764902012-03-15 23:11:19 -0700381 goto out;
382
Stephen Boydd6968fc2015-04-30 13:54:13 -0700383 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530384 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700385
386out:
387 return ret;
388}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100389
390unsigned long __clk_get_rate(struct clk *clk)
391{
392 if (!clk)
393 return 0;
394
395 return clk_core_get_rate_nolock(clk->core);
396}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800397EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700398
Stephen Boydd6968fc2015-04-30 13:54:13 -0700399static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100400{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700401 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100402 return 0;
403
Stephen Boydd6968fc2015-04-30 13:54:13 -0700404 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100405}
406
Russ Dill65800b22012-11-26 11:20:09 -0800407unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700408{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100409 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700410}
Thierry Redingb05c6832013-09-03 09:43:51 +0200411EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700412
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100413bool __clk_is_prepared(struct clk *clk)
414{
415 if (!clk)
416 return false;
417
418 return clk_core_is_prepared(clk->core);
419}
420
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100421bool __clk_is_enabled(struct clk *clk)
422{
423 if (!clk)
424 return false;
425
426 return clk_core_is_enabled(clk->core);
427}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800428EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700429
Stephen Boyd15a02c12015-01-19 18:05:28 -0800430static bool mux_is_better_rate(unsigned long rate, unsigned long now,
431 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100432{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800433 if (flags & CLK_MUX_ROUND_CLOSEST)
434 return abs(now - rate) < abs(best - rate);
435
436 return now <= rate && now > best;
437}
438
439static long
440clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100441 unsigned long min_rate,
442 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800443 unsigned long *best_parent_rate,
444 struct clk_hw **best_parent_p,
445 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100446{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100447 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
James Hogane366fdd2013-07-29 12:25:02 +0100448 int i, num_parents;
449 unsigned long parent_rate, best = 0;
450
451 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100452 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
453 parent = core->parent;
454 if (core->flags & CLK_SET_RATE_PARENT)
Javier Martinez Canillas9e0ad7d2015-02-12 14:58:28 +0100455 best = __clk_determine_rate(parent ? parent->hw : NULL,
456 rate, min_rate, max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100457 else if (parent)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100458 best = clk_core_get_rate_nolock(parent);
James Hogane366fdd2013-07-29 12:25:02 +0100459 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100460 best = clk_core_get_rate_nolock(core);
James Hogane366fdd2013-07-29 12:25:02 +0100461 goto out;
462 }
463
464 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100465 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100466 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100467 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100468 if (!parent)
469 continue;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100470 if (core->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100471 parent_rate = __clk_determine_rate(parent->hw, rate,
472 min_rate,
473 max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100474 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100475 parent_rate = clk_core_get_rate_nolock(parent);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800476 if (mux_is_better_rate(rate, parent_rate, best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100477 best_parent = parent;
478 best = parent_rate;
479 }
480 }
481
482out:
483 if (best_parent)
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100484 *best_parent_p = best_parent->hw;
James Hogane366fdd2013-07-29 12:25:02 +0100485 *best_parent_rate = best;
486
487 return best;
488}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800489
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100490struct clk *__clk_lookup(const char *name)
491{
492 struct clk_core *core = clk_core_lookup(name);
493
494 return !core ? NULL : core->hw->clk;
495}
496
Stephen Boydd6968fc2015-04-30 13:54:13 -0700497static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100498 unsigned long *min_rate,
499 unsigned long *max_rate)
500{
501 struct clk *clk_user;
502
503 *min_rate = 0;
504 *max_rate = ULONG_MAX;
505
Stephen Boydd6968fc2015-04-30 13:54:13 -0700506 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100507 *min_rate = max(*min_rate, clk_user->min_rate);
508
Stephen Boydd6968fc2015-04-30 13:54:13 -0700509 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100510 *max_rate = min(*max_rate, clk_user->max_rate);
511}
512
Stephen Boyd15a02c12015-01-19 18:05:28 -0800513/*
514 * Helper for finding best parent to provide a given frequency. This can be used
515 * directly as a determine_rate callback (e.g. for a mux), or from a more
516 * complex clock that may combine a mux with other operations.
517 */
518long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100519 unsigned long min_rate,
520 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800521 unsigned long *best_parent_rate,
522 struct clk_hw **best_parent_p)
523{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100524 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
525 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800526 best_parent_p, 0);
527}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800528EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100529
Stephen Boyd15a02c12015-01-19 18:05:28 -0800530long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100531 unsigned long min_rate,
532 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800533 unsigned long *best_parent_rate,
534 struct clk_hw **best_parent_p)
535{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100536 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
537 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800538 best_parent_p,
539 CLK_MUX_ROUND_CLOSEST);
540}
541EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
542
Mike Turquetteb24764902012-03-15 23:11:19 -0700543/*** clk api ***/
544
Stephen Boydd6968fc2015-04-30 13:54:13 -0700545static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700546{
Stephen Boyda6334722015-05-06 17:00:54 -0700547 lockdep_assert_held(&prepare_lock);
548
Stephen Boydd6968fc2015-04-30 13:54:13 -0700549 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700550 return;
551
Stephen Boydd6968fc2015-04-30 13:54:13 -0700552 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700553 return;
554
Stephen Boydd6968fc2015-04-30 13:54:13 -0700555 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700556 return;
557
Stephen Boydd6968fc2015-04-30 13:54:13 -0700558 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700559
Stephen Boydd6968fc2015-04-30 13:54:13 -0700560 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800561
Stephen Boydd6968fc2015-04-30 13:54:13 -0700562 if (core->ops->unprepare)
563 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700564
Stephen Boydd6968fc2015-04-30 13:54:13 -0700565 trace_clk_unprepare_complete(core);
566 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700567}
568
569/**
570 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200571 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700572 *
573 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
574 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
575 * if the operation may sleep. One example is a clk which is accessed over
576 * I2c. In the complex case a clk gate operation may require a fast and a slow
577 * part. It is this reason that clk_unprepare and clk_disable are not mutually
578 * exclusive. In fact clk_disable must be called before clk_unprepare.
579 */
580void clk_unprepare(struct clk *clk)
581{
Stephen Boyd63589e92014-03-26 16:06:37 -0700582 if (IS_ERR_OR_NULL(clk))
583 return;
584
Mike Turquetteeab89f62013-03-28 13:59:01 -0700585 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100586 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700587 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700588}
589EXPORT_SYMBOL_GPL(clk_unprepare);
590
Stephen Boydd6968fc2015-04-30 13:54:13 -0700591static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700592{
593 int ret = 0;
594
Stephen Boyda6334722015-05-06 17:00:54 -0700595 lockdep_assert_held(&prepare_lock);
596
Stephen Boydd6968fc2015-04-30 13:54:13 -0700597 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700598 return 0;
599
Stephen Boydd6968fc2015-04-30 13:54:13 -0700600 if (core->prepare_count == 0) {
601 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700602 if (ret)
603 return ret;
604
Stephen Boydd6968fc2015-04-30 13:54:13 -0700605 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800606
Stephen Boydd6968fc2015-04-30 13:54:13 -0700607 if (core->ops->prepare)
608 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800609
Stephen Boydd6968fc2015-04-30 13:54:13 -0700610 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800611
612 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700613 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800614 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700615 }
616 }
617
Stephen Boydd6968fc2015-04-30 13:54:13 -0700618 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700619
620 return 0;
621}
622
623/**
624 * clk_prepare - prepare a clock source
625 * @clk: the clk being prepared
626 *
627 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
628 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
629 * operation may sleep. One example is a clk which is accessed over I2c. In
630 * the complex case a clk ungate operation may require a fast and a slow part.
631 * It is this reason that clk_prepare and clk_enable are not mutually
632 * exclusive. In fact clk_prepare must be called before clk_enable.
633 * Returns 0 on success, -EERROR otherwise.
634 */
635int clk_prepare(struct clk *clk)
636{
637 int ret;
638
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100639 if (!clk)
640 return 0;
641
Mike Turquetteeab89f62013-03-28 13:59:01 -0700642 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100643 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700644 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700645
646 return ret;
647}
648EXPORT_SYMBOL_GPL(clk_prepare);
649
Stephen Boydd6968fc2015-04-30 13:54:13 -0700650static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700651{
Stephen Boyda6334722015-05-06 17:00:54 -0700652 lockdep_assert_held(&enable_lock);
653
Stephen Boydd6968fc2015-04-30 13:54:13 -0700654 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700655 return;
656
Stephen Boydd6968fc2015-04-30 13:54:13 -0700657 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700658 return;
659
Stephen Boydd6968fc2015-04-30 13:54:13 -0700660 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700661 return;
662
Stephen Boydd6968fc2015-04-30 13:54:13 -0700663 trace_clk_disable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800664
Stephen Boydd6968fc2015-04-30 13:54:13 -0700665 if (core->ops->disable)
666 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700667
Stephen Boydd6968fc2015-04-30 13:54:13 -0700668 trace_clk_disable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800669
Stephen Boydd6968fc2015-04-30 13:54:13 -0700670 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100671}
672
Mike Turquetteb24764902012-03-15 23:11:19 -0700673/**
674 * clk_disable - gate a clock
675 * @clk: the clk being gated
676 *
677 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
678 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
679 * clk if the operation is fast and will never sleep. One example is a
680 * SoC-internal clk which is controlled via simple register writes. In the
681 * complex case a clk gate operation may require a fast and a slow part. It is
682 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
683 * In fact clk_disable must be called before clk_unprepare.
684 */
685void clk_disable(struct clk *clk)
686{
687 unsigned long flags;
688
Stephen Boyd63589e92014-03-26 16:06:37 -0700689 if (IS_ERR_OR_NULL(clk))
690 return;
691
Mike Turquetteeab89f62013-03-28 13:59:01 -0700692 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700693 clk_core_disable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700694 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700695}
696EXPORT_SYMBOL_GPL(clk_disable);
697
Stephen Boydd6968fc2015-04-30 13:54:13 -0700698static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700699{
700 int ret = 0;
701
Stephen Boyda6334722015-05-06 17:00:54 -0700702 lockdep_assert_held(&enable_lock);
703
Stephen Boydd6968fc2015-04-30 13:54:13 -0700704 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700705 return 0;
706
Stephen Boydd6968fc2015-04-30 13:54:13 -0700707 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700708 return -ESHUTDOWN;
709
Stephen Boydd6968fc2015-04-30 13:54:13 -0700710 if (core->enable_count == 0) {
711 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700712
713 if (ret)
714 return ret;
715
Stephen Boydd6968fc2015-04-30 13:54:13 -0700716 trace_clk_enable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800717
Stephen Boydd6968fc2015-04-30 13:54:13 -0700718 if (core->ops->enable)
719 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800720
Stephen Boydd6968fc2015-04-30 13:54:13 -0700721 trace_clk_enable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800722
723 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700724 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800725 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700726 }
727 }
728
Stephen Boydd6968fc2015-04-30 13:54:13 -0700729 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700730 return 0;
731}
732
733/**
734 * clk_enable - ungate a clock
735 * @clk: the clk being ungated
736 *
737 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
738 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
739 * if the operation will never sleep. One example is a SoC-internal clk which
740 * is controlled via simple register writes. In the complex case a clk ungate
741 * operation may require a fast and a slow part. It is this reason that
742 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
743 * must be called before clk_enable. Returns 0 on success, -EERROR
744 * otherwise.
745 */
746int clk_enable(struct clk *clk)
747{
748 unsigned long flags;
749 int ret;
750
Dong Aisheng864e1602015-04-30 14:02:19 -0700751 if (!clk)
752 return 0;
753
Mike Turquetteeab89f62013-03-28 13:59:01 -0700754 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700755 ret = clk_core_enable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700756 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700757
758 return ret;
759}
760EXPORT_SYMBOL_GPL(clk_enable);
761
Stephen Boydd6968fc2015-04-30 13:54:13 -0700762static unsigned long clk_core_round_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100763 unsigned long rate,
764 unsigned long min_rate,
765 unsigned long max_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -0700766{
Shawn Guo81536e02012-04-12 20:50:17 +0800767 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100768 struct clk_core *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100769 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700770
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100771 lockdep_assert_held(&prepare_lock);
772
Stephen Boydd6968fc2015-04-30 13:54:13 -0700773 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700774 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700775
Stephen Boydd6968fc2015-04-30 13:54:13 -0700776 parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +0100777 if (parent)
778 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700779
Stephen Boydd6968fc2015-04-30 13:54:13 -0700780 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100781 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700782 return core->ops->determine_rate(core->hw, rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100783 min_rate, max_rate,
784 &parent_rate, &parent_hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700785 } else if (core->ops->round_rate)
786 return core->ops->round_rate(core->hw, rate, &parent_rate);
787 else if (core->flags & CLK_SET_RATE_PARENT)
788 return clk_core_round_rate_nolock(core->parent, rate, min_rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100789 max_rate);
James Hogan71472c02013-07-29 12:25:00 +0100790 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700791 return core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700792}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100793
794/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100795 * __clk_determine_rate - get the closest rate actually supported by a clock
796 * @hw: determine the rate of this clock
797 * @rate: target rate
798 * @min_rate: returned rate must be greater than this rate
799 * @max_rate: returned rate must be less than this rate
800 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700801 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100802 */
803unsigned long __clk_determine_rate(struct clk_hw *hw,
804 unsigned long rate,
805 unsigned long min_rate,
806 unsigned long max_rate)
807{
808 if (!hw)
809 return 0;
810
811 return clk_core_round_rate_nolock(hw->core, rate, min_rate, max_rate);
812}
813EXPORT_SYMBOL_GPL(__clk_determine_rate);
814
815/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100816 * __clk_round_rate - round the given rate for a clk
817 * @clk: round the rate of this clock
818 * @rate: the rate which is to be rounded
819 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700820 * Useful for clk_ops such as .set_rate
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100821 */
822unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
823{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100824 unsigned long min_rate;
825 unsigned long max_rate;
826
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100827 if (!clk)
828 return 0;
829
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100830 clk_core_get_boundaries(clk->core, &min_rate, &max_rate);
831
832 return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100833}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +0200834EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700835
836/**
837 * clk_round_rate - round the given rate for a clk
838 * @clk: the clk for which we are rounding a rate
839 * @rate: the rate which is to be rounded
840 *
841 * Takes in a rate as input and rounds it to a rate that the clk can actually
842 * use which is then returned. If clk doesn't support round_rate operation
843 * then the parent rate is returned.
844 */
845long clk_round_rate(struct clk *clk, unsigned long rate)
846{
847 unsigned long ret;
848
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100849 if (!clk)
850 return 0;
851
Mike Turquetteeab89f62013-03-28 13:59:01 -0700852 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700853 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700854 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700855
856 return ret;
857}
858EXPORT_SYMBOL_GPL(clk_round_rate);
859
860/**
861 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -0700862 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -0700863 * @msg: clk notifier type (see include/linux/clk.h)
864 * @old_rate: old clk rate
865 * @new_rate: new clk rate
866 *
867 * Triggers a notifier call chain on the clk rate-change notification
868 * for 'clk'. Passes a pointer to the struct clk and the previous
869 * and current rates to the notifier callback. Intended to be called by
870 * internal clock code only. Returns NOTIFY_DONE from the last driver
871 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
872 * a driver returns that.
873 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700874static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -0700875 unsigned long old_rate, unsigned long new_rate)
876{
877 struct clk_notifier *cn;
878 struct clk_notifier_data cnd;
879 int ret = NOTIFY_DONE;
880
Mike Turquetteb24764902012-03-15 23:11:19 -0700881 cnd.old_rate = old_rate;
882 cnd.new_rate = new_rate;
883
884 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700885 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100886 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700887 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
888 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -0700889 }
890 }
891
892 return ret;
893}
894
895/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100896 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -0700897 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100898 *
899 * Walks the subtree of clks starting with clk and recalculates accuracies as
900 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700901 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100902 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100903 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700904static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100905{
906 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100907 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100908
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100909 lockdep_assert_held(&prepare_lock);
910
Stephen Boydd6968fc2015-04-30 13:54:13 -0700911 if (core->parent)
912 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100913
Stephen Boydd6968fc2015-04-30 13:54:13 -0700914 if (core->ops->recalc_accuracy)
915 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100916 parent_accuracy);
917 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700918 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100919
Stephen Boydd6968fc2015-04-30 13:54:13 -0700920 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100921 __clk_recalc_accuracies(child);
922}
923
Stephen Boydd6968fc2015-04-30 13:54:13 -0700924static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100925{
926 unsigned long accuracy;
927
928 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -0700929 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
930 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100931
Stephen Boydd6968fc2015-04-30 13:54:13 -0700932 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100933 clk_prepare_unlock();
934
935 return accuracy;
936}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100937
938/**
939 * clk_get_accuracy - return the accuracy of clk
940 * @clk: the clk whose accuracy is being returned
941 *
942 * Simply returns the cached accuracy of the clk, unless
943 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
944 * issued.
945 * If clk is NULL then returns 0.
946 */
947long clk_get_accuracy(struct clk *clk)
948{
949 if (!clk)
950 return 0;
951
952 return clk_core_get_accuracy(clk->core);
953}
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100954EXPORT_SYMBOL_GPL(clk_get_accuracy);
955
Stephen Boydd6968fc2015-04-30 13:54:13 -0700956static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100957 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700958{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700959 if (core->ops->recalc_rate)
960 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700961 return parent_rate;
962}
963
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100964/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700965 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -0700966 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -0700967 * @msg: notification type (see include/linux/clk.h)
968 *
969 * Walks the subtree of clks starting with clk and recalculates rates as it
970 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200971 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -0700972 *
973 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
974 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -0700975 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700976static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -0700977{
978 unsigned long old_rate;
979 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100980 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700981
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100982 lockdep_assert_held(&prepare_lock);
983
Stephen Boydd6968fc2015-04-30 13:54:13 -0700984 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700985
Stephen Boydd6968fc2015-04-30 13:54:13 -0700986 if (core->parent)
987 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700988
Stephen Boydd6968fc2015-04-30 13:54:13 -0700989 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700990
991 /*
992 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
993 * & ABORT_RATE_CHANGE notifiers
994 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700995 if (core->notifier_count && msg)
996 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700997
Stephen Boydd6968fc2015-04-30 13:54:13 -0700998 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700999 __clk_recalc_rates(child, msg);
1000}
1001
Stephen Boydd6968fc2015-04-30 13:54:13 -07001002static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001003{
1004 unsigned long rate;
1005
1006 clk_prepare_lock();
1007
Stephen Boydd6968fc2015-04-30 13:54:13 -07001008 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1009 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001010
Stephen Boydd6968fc2015-04-30 13:54:13 -07001011 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001012 clk_prepare_unlock();
1013
1014 return rate;
1015}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001016
Mike Turquetteb24764902012-03-15 23:11:19 -07001017/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001018 * clk_get_rate - return the rate of clk
1019 * @clk: the clk whose rate is being returned
1020 *
1021 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1022 * is set, which means a recalc_rate will be issued.
1023 * If clk is NULL then returns 0.
1024 */
1025unsigned long clk_get_rate(struct clk *clk)
1026{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001027 if (!clk)
1028 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001029
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001030 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001031}
1032EXPORT_SYMBOL_GPL(clk_get_rate);
1033
Stephen Boydd6968fc2015-04-30 13:54:13 -07001034static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001035 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001036{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001037 int i;
James Hogan4935b222013-07-29 12:24:59 +01001038
Stephen Boydd6968fc2015-04-30 13:54:13 -07001039 if (!core->parents) {
1040 core->parents = kcalloc(core->num_parents,
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001041 sizeof(struct clk *), GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001042 if (!core->parents)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001043 return -ENOMEM;
1044 }
James Hogan4935b222013-07-29 12:24:59 +01001045
1046 /*
1047 * find index of new parent clock using cached parent ptrs,
1048 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001049 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001050 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001051 for (i = 0; i < core->num_parents; i++) {
1052 if (core->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001053 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001054
Stephen Boydd6968fc2015-04-30 13:54:13 -07001055 if (core->parents[i])
Tomasz Figada0f0b22013-09-29 02:37:16 +02001056 continue;
1057
Stephen Boydd6968fc2015-04-30 13:54:13 -07001058 if (!strcmp(core->parent_names[i], parent->name)) {
1059 core->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001060 return i;
James Hogan4935b222013-07-29 12:24:59 +01001061 }
1062 }
1063
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001064 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001065}
1066
Stephen Boydd6968fc2015-04-30 13:54:13 -07001067static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001068{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001069 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001070
James Hogan903efc52013-08-29 12:10:51 +01001071 if (new_parent) {
1072 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001073 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001074 new_parent->new_child = NULL;
1075
Stephen Boydd6968fc2015-04-30 13:54:13 -07001076 hlist_add_head(&core->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001077 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001078 hlist_add_head(&core->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001079 }
James Hogan4935b222013-07-29 12:24:59 +01001080
Stephen Boydd6968fc2015-04-30 13:54:13 -07001081 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001082}
1083
Stephen Boydd6968fc2015-04-30 13:54:13 -07001084static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001085 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001086{
1087 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001088 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001089
1090 /*
1091 * Migrate prepare state between parents and prevent race with
1092 * clk_enable().
1093 *
1094 * If the clock is not prepared, then a race with
1095 * clk_enable/disable() is impossible since we already have the
1096 * prepare lock (future calls to clk_enable() need to be preceded by
1097 * a clk_prepare()).
1098 *
1099 * If the clock is prepared, migrate the prepared state to the new
1100 * parent and also protect against a race with clk_enable() by
1101 * forcing the clock and the new parent on. This ensures that all
1102 * future calls to clk_enable() are practically NOPs with respect to
1103 * hardware and software states.
1104 *
1105 * See also: Comment for clk_set_parent() below.
1106 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001107 if (core->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001108 clk_core_prepare(parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001109 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001110 clk_core_enable(parent);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001111 clk_core_enable(core);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001112 clk_enable_unlock(flags);
James Hogan4935b222013-07-29 12:24:59 +01001113 }
1114
1115 /* update the clk tree topology */
1116 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001117 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001118 clk_enable_unlock(flags);
1119
Stephen Boyd3fa22522014-01-15 10:47:22 -08001120 return old_parent;
1121}
1122
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001123static void __clk_set_parent_after(struct clk_core *core,
1124 struct clk_core *parent,
1125 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001126{
Dong Aishengd2a5d462015-04-15 22:26:36 +08001127 unsigned long flags;
1128
Stephen Boyd3fa22522014-01-15 10:47:22 -08001129 /*
1130 * Finish the migration of prepare state and undo the changes done
1131 * for preventing a race with clk_enable().
1132 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001133 if (core->prepare_count) {
Dong Aishengd2a5d462015-04-15 22:26:36 +08001134 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001135 clk_core_disable(core);
1136 clk_core_disable(old_parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001137 clk_enable_unlock(flags);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001138 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001139 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001140}
1141
Stephen Boydd6968fc2015-04-30 13:54:13 -07001142static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001143 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001144{
1145 unsigned long flags;
1146 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001147 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001148
Stephen Boydd6968fc2015-04-30 13:54:13 -07001149 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001150
Stephen Boydd6968fc2015-04-30 13:54:13 -07001151 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001152
James Hogan4935b222013-07-29 12:24:59 +01001153 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001154 if (parent && core->ops->set_parent)
1155 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001156
Stephen Boydd6968fc2015-04-30 13:54:13 -07001157 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001158
James Hogan4935b222013-07-29 12:24:59 +01001159 if (ret) {
1160 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001161 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001162 clk_enable_unlock(flags);
1163
Stephen Boydd6968fc2015-04-30 13:54:13 -07001164 if (core->prepare_count) {
Dong Aishengd2a5d462015-04-15 22:26:36 +08001165 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001166 clk_core_disable(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001167 clk_core_disable(parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001168 clk_enable_unlock(flags);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001169 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001170 }
1171 return ret;
1172 }
1173
Stephen Boydd6968fc2015-04-30 13:54:13 -07001174 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001175
James Hogan4935b222013-07-29 12:24:59 +01001176 return 0;
1177}
1178
Ulf Hanssona093bde2012-08-31 14:21:28 +02001179/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001180 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001181 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001182 * @parent_rate: the "future" rate of clk's parent
1183 *
1184 * Walks the subtree of clks starting with clk, speculating rates as it
1185 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1186 *
1187 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1188 * pre-rate change notifications and returns early if no clks in the
1189 * subtree have subscribed to the notifications. Note that if a clk does not
1190 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001191 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001192 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001193static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001194 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001195{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001196 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001197 unsigned long new_rate;
1198 int ret = NOTIFY_DONE;
1199
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001200 lockdep_assert_held(&prepare_lock);
1201
Stephen Boydd6968fc2015-04-30 13:54:13 -07001202 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001203
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001204 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001205 if (core->notifier_count)
1206 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001207
Mike Turquette86bcfa22014-02-24 16:08:41 -08001208 if (ret & NOTIFY_STOP_MASK) {
1209 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001210 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001211 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001212 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001213
Stephen Boydd6968fc2015-04-30 13:54:13 -07001214 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001215 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001216 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001217 break;
1218 }
1219
1220out:
1221 return ret;
1222}
1223
Stephen Boydd6968fc2015-04-30 13:54:13 -07001224static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001225 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001226{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001227 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001228
Stephen Boydd6968fc2015-04-30 13:54:13 -07001229 core->new_rate = new_rate;
1230 core->new_parent = new_parent;
1231 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001232 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001233 core->new_child = NULL;
1234 if (new_parent && new_parent != core->parent)
1235 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001236
Stephen Boydd6968fc2015-04-30 13:54:13 -07001237 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001238 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001239 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001240 }
1241}
1242
1243/*
1244 * calculate the new rates returning the topmost clock that has to be
1245 * changed.
1246 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001247static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001248 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001249{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001250 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001251 struct clk_core *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001252 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001253 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001254 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001255 unsigned long min_rate;
1256 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001257 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001258 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001259
Mike Turquette7452b212012-03-26 14:45:36 -07001260 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001261 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001262 return NULL;
1263
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001264 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001265 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001266 if (parent)
1267 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001268
Stephen Boydd6968fc2015-04-30 13:54:13 -07001269 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001270
James Hogan71472c02013-07-29 12:25:00 +01001271 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001272 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001273 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001274 ret = core->ops->determine_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001275 min_rate,
1276 max_rate,
1277 &best_parent_rate,
1278 &parent_hw);
1279 if (ret < 0)
1280 return NULL;
1281
1282 new_rate = ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001283 parent = parent_hw ? parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001284 } else if (core->ops->round_rate) {
1285 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001286 &best_parent_rate);
1287 if (ret < 0)
1288 return NULL;
1289
1290 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001291 if (new_rate < min_rate || new_rate > max_rate)
1292 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001293 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001294 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001295 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001296 return NULL;
1297 } else {
1298 /* pass-through clock with adjustable parent */
1299 top = clk_calc_new_rates(parent, rate);
1300 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001301 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001302 }
1303
James Hogan71472c02013-07-29 12:25:00 +01001304 /* some clocks must be gated to change parent */
1305 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001306 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001307 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001308 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001309 return NULL;
1310 }
1311
James Hogan71472c02013-07-29 12:25:00 +01001312 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001313 if (parent && core->num_parents > 1) {
1314 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001315 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001316 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001317 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001318 return NULL;
1319 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001320 }
1321
Stephen Boydd6968fc2015-04-30 13:54:13 -07001322 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001323 best_parent_rate != parent->rate)
1324 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001325
1326out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001327 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001328
1329 return top;
1330}
1331
1332/*
1333 * Notify about rate changes in a subtree. Always walk down the whole tree
1334 * so that in case of an error we can walk down the whole tree again and
1335 * abort the change.
1336 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001337static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001338 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001339{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001340 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001341 int ret = NOTIFY_DONE;
1342
Stephen Boydd6968fc2015-04-30 13:54:13 -07001343 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301344 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001345
Stephen Boydd6968fc2015-04-30 13:54:13 -07001346 if (core->notifier_count) {
1347 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001348 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001349 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001350 }
1351
Stephen Boydd6968fc2015-04-30 13:54:13 -07001352 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001353 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001354 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001355 continue;
1356 tmp_clk = clk_propagate_rate_change(child, event);
1357 if (tmp_clk)
1358 fail_clk = tmp_clk;
1359 }
1360
Stephen Boydd6968fc2015-04-30 13:54:13 -07001361 /* handle the new child who might not be in core->children yet */
1362 if (core->new_child) {
1363 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001364 if (tmp_clk)
1365 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001366 }
1367
1368 return fail_clk;
1369}
1370
1371/*
1372 * walk down a subtree and set the new rates notifying the rate
1373 * change on the way
1374 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001375static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001376{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001377 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001378 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001379 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001380 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001381 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001382 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001383
Stephen Boydd6968fc2015-04-30 13:54:13 -07001384 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001385
Stephen Boydd6968fc2015-04-30 13:54:13 -07001386 if (core->new_parent)
1387 best_parent_rate = core->new_parent->rate;
1388 else if (core->parent)
1389 best_parent_rate = core->parent->rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001390
Stephen Boydd6968fc2015-04-30 13:54:13 -07001391 if (core->new_parent && core->new_parent != core->parent) {
1392 old_parent = __clk_set_parent_before(core, core->new_parent);
1393 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001394
Stephen Boydd6968fc2015-04-30 13:54:13 -07001395 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001396 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001397 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001398 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001399 core->new_parent_index);
1400 } else if (core->ops->set_parent) {
1401 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001402 }
1403
Stephen Boydd6968fc2015-04-30 13:54:13 -07001404 trace_clk_set_parent_complete(core, core->new_parent);
1405 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001406 }
1407
Stephen Boydd6968fc2015-04-30 13:54:13 -07001408 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001409
Stephen Boydd6968fc2015-04-30 13:54:13 -07001410 if (!skip_set_rate && core->ops->set_rate)
1411 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001412
Stephen Boydd6968fc2015-04-30 13:54:13 -07001413 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001414
Stephen Boydd6968fc2015-04-30 13:54:13 -07001415 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001416
Stephen Boydd6968fc2015-04-30 13:54:13 -07001417 if (core->notifier_count && old_rate != core->rate)
1418 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001419
Michael Turquette85e88fa2015-06-20 12:18:03 -07001420 if (core->flags & CLK_RECALC_NEW_RATES)
1421 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001422
Tero Kristo067bb172014-08-21 16:47:45 +03001423 /*
1424 * Use safe iteration, as change_rate can actually swap parents
1425 * for certain clock types.
1426 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001427 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001428 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001429 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001430 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001431 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001432 }
1433
Stephen Boydd6968fc2015-04-30 13:54:13 -07001434 /* handle the new child who might not be in core->children yet */
1435 if (core->new_child)
1436 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001437}
1438
Stephen Boydd6968fc2015-04-30 13:54:13 -07001439static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001440 unsigned long req_rate)
1441{
1442 struct clk_core *top, *fail_clk;
1443 unsigned long rate = req_rate;
1444 int ret = 0;
1445
Stephen Boydd6968fc2015-04-30 13:54:13 -07001446 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001447 return 0;
1448
1449 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001450 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001451 return 0;
1452
Stephen Boydd6968fc2015-04-30 13:54:13 -07001453 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001454 return -EBUSY;
1455
1456 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001457 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001458 if (!top)
1459 return -EINVAL;
1460
1461 /* notify that we are about to change rates */
1462 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1463 if (fail_clk) {
1464 pr_debug("%s: failed to set %s rate\n", __func__,
1465 fail_clk->name);
1466 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1467 return -EBUSY;
1468 }
1469
1470 /* change the rates */
1471 clk_change_rate(top);
1472
Stephen Boydd6968fc2015-04-30 13:54:13 -07001473 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001474
1475 return ret;
1476}
1477
Mike Turquetteb24764902012-03-15 23:11:19 -07001478/**
1479 * clk_set_rate - specify a new rate for clk
1480 * @clk: the clk whose rate is being changed
1481 * @rate: the new rate for clk
1482 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001483 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001484 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001485 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1486 * propagate up to clk's parent; whether or not this happens depends on the
1487 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1488 * after calling .round_rate then upstream parent propagation is ignored. If
1489 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001490 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001491 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1492 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001493 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001494 * Rate changes are accomplished via tree traversal that also recalculates the
1495 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001496 *
1497 * Returns 0 on success, -EERROR otherwise.
1498 */
1499int clk_set_rate(struct clk *clk, unsigned long rate)
1500{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001501 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001502
Mike Turquette89ac8d72013-08-21 23:58:09 -07001503 if (!clk)
1504 return 0;
1505
Mike Turquetteb24764902012-03-15 23:11:19 -07001506 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001507 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001508
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001509 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001510
Mike Turquetteeab89f62013-03-28 13:59:01 -07001511 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001512
1513 return ret;
1514}
1515EXPORT_SYMBOL_GPL(clk_set_rate);
1516
1517/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001518 * clk_set_rate_range - set a rate range for a clock source
1519 * @clk: clock source
1520 * @min: desired minimum clock rate in Hz, inclusive
1521 * @max: desired maximum clock rate in Hz, inclusive
1522 *
1523 * Returns success (0) or negative errno.
1524 */
1525int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1526{
1527 int ret = 0;
1528
1529 if (!clk)
1530 return 0;
1531
1532 if (min > max) {
1533 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1534 __func__, clk->core->name, clk->dev_id, clk->con_id,
1535 min, max);
1536 return -EINVAL;
1537 }
1538
1539 clk_prepare_lock();
1540
1541 if (min != clk->min_rate || max != clk->max_rate) {
1542 clk->min_rate = min;
1543 clk->max_rate = max;
1544 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1545 }
1546
1547 clk_prepare_unlock();
1548
1549 return ret;
1550}
1551EXPORT_SYMBOL_GPL(clk_set_rate_range);
1552
1553/**
1554 * clk_set_min_rate - set a minimum clock rate for a clock source
1555 * @clk: clock source
1556 * @rate: desired minimum clock rate in Hz, inclusive
1557 *
1558 * Returns success (0) or negative errno.
1559 */
1560int clk_set_min_rate(struct clk *clk, unsigned long rate)
1561{
1562 if (!clk)
1563 return 0;
1564
1565 return clk_set_rate_range(clk, rate, clk->max_rate);
1566}
1567EXPORT_SYMBOL_GPL(clk_set_min_rate);
1568
1569/**
1570 * clk_set_max_rate - set a maximum clock rate for a clock source
1571 * @clk: clock source
1572 * @rate: desired maximum clock rate in Hz, inclusive
1573 *
1574 * Returns success (0) or negative errno.
1575 */
1576int clk_set_max_rate(struct clk *clk, unsigned long rate)
1577{
1578 if (!clk)
1579 return 0;
1580
1581 return clk_set_rate_range(clk, clk->min_rate, rate);
1582}
1583EXPORT_SYMBOL_GPL(clk_set_max_rate);
1584
1585/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001586 * clk_get_parent - return the parent of a clk
1587 * @clk: the clk whose parent gets returned
1588 *
1589 * Simply returns clk->parent. Returns NULL if clk is NULL.
1590 */
1591struct clk *clk_get_parent(struct clk *clk)
1592{
1593 struct clk *parent;
1594
Mike Turquetteeab89f62013-03-28 13:59:01 -07001595 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001596 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001597 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001598
1599 return parent;
1600}
1601EXPORT_SYMBOL_GPL(clk_get_parent);
1602
1603/*
1604 * .get_parent is mandatory for clocks with multiple possible parents. It is
1605 * optional for single-parent clocks. Always call .get_parent if it is
1606 * available and WARN if it is missing for multi-parent clocks.
1607 *
1608 * For single-parent clocks without .get_parent, first check to see if the
1609 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001610 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001611 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001612static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001613{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001614 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001615 u8 index;
1616
1617 /* handle the trivial cases */
1618
Stephen Boydd6968fc2015-04-30 13:54:13 -07001619 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -07001620 goto out;
1621
Stephen Boydd6968fc2015-04-30 13:54:13 -07001622 if (core->num_parents == 1) {
1623 if (IS_ERR_OR_NULL(core->parent))
1624 core->parent = clk_core_lookup(core->parent_names[0]);
1625 ret = core->parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001626 goto out;
1627 }
1628
Stephen Boydd6968fc2015-04-30 13:54:13 -07001629 if (!core->ops->get_parent) {
1630 WARN(!core->ops->get_parent,
Mike Turquetteb24764902012-03-15 23:11:19 -07001631 "%s: multi-parent clocks must implement .get_parent\n",
1632 __func__);
1633 goto out;
1634 };
1635
1636 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07001637 * Do our best to cache parent clocks in core->parents. This prevents
1638 * unnecessary and expensive lookups. We don't set core->parent here;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001639 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001640 */
1641
Stephen Boydd6968fc2015-04-30 13:54:13 -07001642 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001643
Stephen Boydd6968fc2015-04-30 13:54:13 -07001644 if (!core->parents)
1645 core->parents =
1646 kcalloc(core->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001647 GFP_KERNEL);
1648
Stephen Boydd6968fc2015-04-30 13:54:13 -07001649 ret = clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001650
1651out:
1652 return ret;
1653}
1654
Stephen Boydd6968fc2015-04-30 13:54:13 -07001655static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001656 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001657{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001658 clk_reparent(core, new_parent);
1659 __clk_recalc_accuracies(core);
1660 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07001661}
1662
Mike Turquetteb24764902012-03-15 23:11:19 -07001663/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001664 * clk_has_parent - check if a clock is a possible parent for another
1665 * @clk: clock source
1666 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07001667 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001668 * This function can be used in drivers that need to check that a clock can be
1669 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001670 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001671 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001672 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001673bool clk_has_parent(struct clk *clk, struct clk *parent)
1674{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001675 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001676 unsigned int i;
1677
1678 /* NULL clocks should be nops, so return success if either is NULL. */
1679 if (!clk || !parent)
1680 return true;
1681
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001682 core = clk->core;
1683 parent_core = parent->core;
1684
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001685 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001686 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001687 return true;
1688
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001689 for (i = 0; i < core->num_parents; i++)
1690 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001691 return true;
1692
1693 return false;
1694}
1695EXPORT_SYMBOL_GPL(clk_has_parent);
1696
Stephen Boydd6968fc2015-04-30 13:54:13 -07001697static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001698{
1699 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001700 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001701 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001702
Stephen Boydd6968fc2015-04-30 13:54:13 -07001703 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07001704 return 0;
1705
Mike Turquetteb24764902012-03-15 23:11:19 -07001706 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001707 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001708
Stephen Boydd6968fc2015-04-30 13:54:13 -07001709 if (core->parent == parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001710 goto out;
1711
Stephen Boydb61c43c2015-02-02 14:11:25 -08001712 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001713 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08001714 ret = -ENOSYS;
1715 goto out;
1716 }
1717
Ulf Hansson031dcc92013-04-02 23:09:38 +02001718 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001719 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001720 ret = -EBUSY;
1721 goto out;
1722 }
1723
1724 /* try finding the new parent index */
1725 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001726 p_index = clk_fetch_parent_index(core, parent);
Ulf Hansson031dcc92013-04-02 23:09:38 +02001727 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001728 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001729 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001730 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001731 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001732 goto out;
1733 }
1734 }
1735
Mike Turquetteb24764902012-03-15 23:11:19 -07001736 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001737 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001738
1739 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001740 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001741 goto out;
1742
Ulf Hansson031dcc92013-04-02 23:09:38 +02001743 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001744 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001745
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001746 /* propagate rate an accuracy recalculation accordingly */
1747 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001748 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001749 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001750 __clk_recalc_rates(core, POST_RATE_CHANGE);
1751 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001752 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001753
1754out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001755 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001756
1757 return ret;
1758}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001759
1760/**
1761 * clk_set_parent - switch the parent of a mux clk
1762 * @clk: the mux clk whose input we are switching
1763 * @parent: the new input to clk
1764 *
1765 * Re-parent clk to use parent as its new input source. If clk is in
1766 * prepared state, the clk will get enabled for the duration of this call. If
1767 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1768 * that, the reparenting is glitchy in hardware, etc), use the
1769 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1770 *
1771 * After successfully changing clk's parent clk_set_parent will update the
1772 * clk topology, sysfs topology and propagate rate recalculation via
1773 * __clk_recalc_rates.
1774 *
1775 * Returns 0 on success, -EERROR otherwise.
1776 */
1777int clk_set_parent(struct clk *clk, struct clk *parent)
1778{
1779 if (!clk)
1780 return 0;
1781
1782 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1783}
Mike Turquetteb24764902012-03-15 23:11:19 -07001784EXPORT_SYMBOL_GPL(clk_set_parent);
1785
1786/**
Mike Turquettee59c5372014-02-18 21:21:25 -08001787 * clk_set_phase - adjust the phase shift of a clock signal
1788 * @clk: clock signal source
1789 * @degrees: number of degrees the signal is shifted
1790 *
1791 * Shifts the phase of a clock signal by the specified
1792 * degrees. Returns 0 on success, -EERROR otherwise.
1793 *
1794 * This function makes no distinction about the input or reference
1795 * signal that we adjust the clock signal phase against. For example
1796 * phase locked-loop clock signal generators we may shift phase with
1797 * respect to feedback clock signal input, but for other cases the
1798 * clock phase may be shifted with respect to some other, unspecified
1799 * signal.
1800 *
1801 * Additionally the concept of phase shift does not propagate through
1802 * the clock tree hierarchy, which sets it apart from clock rates and
1803 * clock accuracy. A parent clock phase attribute does not have an
1804 * impact on the phase attribute of a child clock.
1805 */
1806int clk_set_phase(struct clk *clk, int degrees)
1807{
Stephen Boyd08b95752015-02-02 14:09:43 -08001808 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08001809
1810 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08001811 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08001812
1813 /* sanity check degrees */
1814 degrees %= 360;
1815 if (degrees < 0)
1816 degrees += 360;
1817
1818 clk_prepare_lock();
1819
Stephen Boyddfc202e2015-02-02 14:37:41 -08001820 trace_clk_set_phase(clk->core, degrees);
1821
Stephen Boyd08b95752015-02-02 14:09:43 -08001822 if (clk->core->ops->set_phase)
1823 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08001824
Stephen Boyddfc202e2015-02-02 14:37:41 -08001825 trace_clk_set_phase_complete(clk->core, degrees);
1826
Mike Turquettee59c5372014-02-18 21:21:25 -08001827 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001828 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08001829
Mike Turquettee59c5372014-02-18 21:21:25 -08001830 clk_prepare_unlock();
1831
Mike Turquettee59c5372014-02-18 21:21:25 -08001832 return ret;
1833}
Maxime Ripard9767b042015-01-20 22:23:43 +01001834EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001835
Stephen Boydd6968fc2015-04-30 13:54:13 -07001836static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08001837{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07001838 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08001839
1840 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001841 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08001842 clk_prepare_unlock();
1843
Mike Turquettee59c5372014-02-18 21:21:25 -08001844 return ret;
1845}
1846
1847/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001848 * clk_get_phase - return the phase shift of a clock signal
1849 * @clk: clock signal source
1850 *
1851 * Returns the phase shift of a clock node in degrees, otherwise returns
1852 * -EERROR.
1853 */
1854int clk_get_phase(struct clk *clk)
1855{
1856 if (!clk)
1857 return 0;
1858
1859 return clk_core_get_phase(clk->core);
1860}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001861EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07001862
1863/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08001864 * clk_is_match - check if two clk's point to the same hardware clock
1865 * @p: clk compared against q
1866 * @q: clk compared against p
1867 *
1868 * Returns true if the two struct clk pointers both point to the same hardware
1869 * clock node. Put differently, returns true if struct clk *p and struct clk *q
1870 * share the same struct clk_core object.
1871 *
1872 * Returns false otherwise. Note that two NULL clks are treated as matching.
1873 */
1874bool clk_is_match(const struct clk *p, const struct clk *q)
1875{
1876 /* trivial case: identical struct clk's or both NULL */
1877 if (p == q)
1878 return true;
1879
1880 /* true if clk->core pointers match. Avoid derefing garbage */
1881 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
1882 if (p->core == q->core)
1883 return true;
1884
1885 return false;
1886}
1887EXPORT_SYMBOL_GPL(clk_is_match);
1888
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001889/*** debugfs support ***/
1890
1891#ifdef CONFIG_DEBUG_FS
1892#include <linux/debugfs.h>
1893
1894static struct dentry *rootdir;
1895static int inited = 0;
1896static DEFINE_MUTEX(clk_debug_lock);
1897static HLIST_HEAD(clk_debug_list);
1898
1899static struct hlist_head *all_lists[] = {
1900 &clk_root_list,
1901 &clk_orphan_list,
1902 NULL,
1903};
1904
1905static struct hlist_head *orphan_list[] = {
1906 &clk_orphan_list,
1907 NULL,
1908};
1909
1910static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
1911 int level)
1912{
1913 if (!c)
1914 return;
1915
1916 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
1917 level * 3 + 1, "",
1918 30 - level * 3, c->name,
1919 c->enable_count, c->prepare_count, clk_core_get_rate(c),
1920 clk_core_get_accuracy(c), clk_core_get_phase(c));
1921}
1922
1923static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
1924 int level)
1925{
1926 struct clk_core *child;
1927
1928 if (!c)
1929 return;
1930
1931 clk_summary_show_one(s, c, level);
1932
1933 hlist_for_each_entry(child, &c->children, child_node)
1934 clk_summary_show_subtree(s, child, level + 1);
1935}
1936
1937static int clk_summary_show(struct seq_file *s, void *data)
1938{
1939 struct clk_core *c;
1940 struct hlist_head **lists = (struct hlist_head **)s->private;
1941
1942 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
1943 seq_puts(s, "----------------------------------------------------------------------------------------\n");
1944
1945 clk_prepare_lock();
1946
1947 for (; *lists; lists++)
1948 hlist_for_each_entry(c, *lists, child_node)
1949 clk_summary_show_subtree(s, c, 0);
1950
1951 clk_prepare_unlock();
1952
1953 return 0;
1954}
1955
1956
1957static int clk_summary_open(struct inode *inode, struct file *file)
1958{
1959 return single_open(file, clk_summary_show, inode->i_private);
1960}
1961
1962static const struct file_operations clk_summary_fops = {
1963 .open = clk_summary_open,
1964 .read = seq_read,
1965 .llseek = seq_lseek,
1966 .release = single_release,
1967};
1968
1969static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
1970{
1971 if (!c)
1972 return;
1973
Stefan Wahren7cb81132015-04-29 16:36:43 +00001974 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001975 seq_printf(s, "\"%s\": { ", c->name);
1976 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
1977 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00001978 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
1979 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001980 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
1981}
1982
1983static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
1984{
1985 struct clk_core *child;
1986
1987 if (!c)
1988 return;
1989
1990 clk_dump_one(s, c, level);
1991
1992 hlist_for_each_entry(child, &c->children, child_node) {
1993 seq_printf(s, ",");
1994 clk_dump_subtree(s, child, level + 1);
1995 }
1996
1997 seq_printf(s, "}");
1998}
1999
2000static int clk_dump(struct seq_file *s, void *data)
2001{
2002 struct clk_core *c;
2003 bool first_node = true;
2004 struct hlist_head **lists = (struct hlist_head **)s->private;
2005
2006 seq_printf(s, "{");
2007
2008 clk_prepare_lock();
2009
2010 for (; *lists; lists++) {
2011 hlist_for_each_entry(c, *lists, child_node) {
2012 if (!first_node)
2013 seq_puts(s, ",");
2014 first_node = false;
2015 clk_dump_subtree(s, c, 0);
2016 }
2017 }
2018
2019 clk_prepare_unlock();
2020
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002021 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002022 return 0;
2023}
2024
2025
2026static int clk_dump_open(struct inode *inode, struct file *file)
2027{
2028 return single_open(file, clk_dump, inode->i_private);
2029}
2030
2031static const struct file_operations clk_dump_fops = {
2032 .open = clk_dump_open,
2033 .read = seq_read,
2034 .llseek = seq_lseek,
2035 .release = single_release,
2036};
2037
2038static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2039{
2040 struct dentry *d;
2041 int ret = -ENOMEM;
2042
2043 if (!core || !pdentry) {
2044 ret = -EINVAL;
2045 goto out;
2046 }
2047
2048 d = debugfs_create_dir(core->name, pdentry);
2049 if (!d)
2050 goto out;
2051
2052 core->dentry = d;
2053
2054 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2055 (u32 *)&core->rate);
2056 if (!d)
2057 goto err_out;
2058
2059 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2060 (u32 *)&core->accuracy);
2061 if (!d)
2062 goto err_out;
2063
2064 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2065 (u32 *)&core->phase);
2066 if (!d)
2067 goto err_out;
2068
2069 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2070 (u32 *)&core->flags);
2071 if (!d)
2072 goto err_out;
2073
2074 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2075 (u32 *)&core->prepare_count);
2076 if (!d)
2077 goto err_out;
2078
2079 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2080 (u32 *)&core->enable_count);
2081 if (!d)
2082 goto err_out;
2083
2084 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2085 (u32 *)&core->notifier_count);
2086 if (!d)
2087 goto err_out;
2088
2089 if (core->ops->debug_init) {
2090 ret = core->ops->debug_init(core->hw, core->dentry);
2091 if (ret)
2092 goto err_out;
2093 }
2094
2095 ret = 0;
2096 goto out;
2097
2098err_out:
2099 debugfs_remove_recursive(core->dentry);
2100 core->dentry = NULL;
2101out:
2102 return ret;
2103}
2104
2105/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002106 * clk_debug_register - add a clk node to the debugfs clk directory
2107 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002108 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002109 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2110 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002111 * will be created lazily by clk_debug_init as part of a late_initcall.
2112 */
2113static int clk_debug_register(struct clk_core *core)
2114{
2115 int ret = 0;
2116
2117 mutex_lock(&clk_debug_lock);
2118 hlist_add_head(&core->debug_node, &clk_debug_list);
2119
2120 if (!inited)
2121 goto unlock;
2122
2123 ret = clk_debug_create_one(core, rootdir);
2124unlock:
2125 mutex_unlock(&clk_debug_lock);
2126
2127 return ret;
2128}
2129
2130 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002131 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2132 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002133 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002134 * Dynamically removes a clk and all its child nodes from the
2135 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002136 * clk_debug_register in __clk_init.
2137 */
2138static void clk_debug_unregister(struct clk_core *core)
2139{
2140 mutex_lock(&clk_debug_lock);
2141 hlist_del_init(&core->debug_node);
2142 debugfs_remove_recursive(core->dentry);
2143 core->dentry = NULL;
2144 mutex_unlock(&clk_debug_lock);
2145}
2146
2147struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2148 void *data, const struct file_operations *fops)
2149{
2150 struct dentry *d = NULL;
2151
2152 if (hw->core->dentry)
2153 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2154 fops);
2155
2156 return d;
2157}
2158EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2159
2160/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002161 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002162 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002163 * clks are often initialized very early during boot before memory can be
2164 * dynamically allocated and well before debugfs is setup. This function
2165 * populates the debugfs clk directory once at boot-time when we know that
2166 * debugfs is setup. It should only be called once at boot-time, all other clks
2167 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002168 */
2169static int __init clk_debug_init(void)
2170{
2171 struct clk_core *core;
2172 struct dentry *d;
2173
2174 rootdir = debugfs_create_dir("clk", NULL);
2175
2176 if (!rootdir)
2177 return -ENOMEM;
2178
2179 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2180 &clk_summary_fops);
2181 if (!d)
2182 return -ENOMEM;
2183
2184 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2185 &clk_dump_fops);
2186 if (!d)
2187 return -ENOMEM;
2188
2189 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2190 &orphan_list, &clk_summary_fops);
2191 if (!d)
2192 return -ENOMEM;
2193
2194 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2195 &orphan_list, &clk_dump_fops);
2196 if (!d)
2197 return -ENOMEM;
2198
2199 mutex_lock(&clk_debug_lock);
2200 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2201 clk_debug_create_one(core, rootdir);
2202
2203 inited = 1;
2204 mutex_unlock(&clk_debug_lock);
2205
2206 return 0;
2207}
2208late_initcall(clk_debug_init);
2209#else
2210static inline int clk_debug_register(struct clk_core *core) { return 0; }
2211static inline void clk_debug_reparent(struct clk_core *core,
2212 struct clk_core *new_parent)
2213{
2214}
2215static inline void clk_debug_unregister(struct clk_core *core)
2216{
2217}
2218#endif
2219
Michael Turquette3d3801e2015-02-25 09:11:01 -08002220/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002221 * __clk_init - initialize the data structures in a struct clk
2222 * @dev: device initializing this clk, placeholder for now
2223 * @clk: clk being initialized
2224 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002225 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002226 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002227 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002228static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002229{
Mike Turquetted1302a32012-03-29 14:30:40 -07002230 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002231 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002232 struct hlist_node *tmp2;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002233 struct clk_core *core;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002234 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002235
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002236 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002237 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002238
Stephen Boydd6968fc2015-04-30 13:54:13 -07002239 core = clk_user->core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002240
Mike Turquetteeab89f62013-03-28 13:59:01 -07002241 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002242
2243 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002244 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002245 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002246 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002247 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002248 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002249 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002250
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002251 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002252 if (core->ops->set_rate &&
2253 !((core->ops->round_rate || core->ops->determine_rate) &&
2254 core->ops->recalc_rate)) {
James Hogan71472c02013-07-29 12:25:00 +01002255 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002256 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002257 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002258 goto out;
2259 }
2260
Stephen Boydd6968fc2015-04-30 13:54:13 -07002261 if (core->ops->set_parent && !core->ops->get_parent) {
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002262 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002263 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002264 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002265 goto out;
2266 }
2267
Stephen Boydd6968fc2015-04-30 13:54:13 -07002268 if (core->ops->set_rate_and_parent &&
2269 !(core->ops->set_parent && core->ops->set_rate)) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002270 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002271 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002272 ret = -EINVAL;
2273 goto out;
2274 }
2275
Mike Turquetteb24764902012-03-15 23:11:19 -07002276 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002277 for (i = 0; i < core->num_parents; i++)
2278 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002279 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002280 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002281
2282 /*
2283 * Allocate an array of struct clk *'s to avoid unnecessary string
2284 * look-ups of clk's possible parents. This can fail for clocks passed
Stephen Boydd6968fc2015-04-30 13:54:13 -07002285 * in to clk_init during early boot; thus any access to core->parents[]
Mike Turquetteb24764902012-03-15 23:11:19 -07002286 * must always check for a NULL pointer and try to populate it if
2287 * necessary.
2288 *
Stephen Boydd6968fc2015-04-30 13:54:13 -07002289 * If core->parents is not NULL we skip this entire block. This allows
2290 * for clock drivers to statically initialize core->parents.
Mike Turquetteb24764902012-03-15 23:11:19 -07002291 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002292 if (core->num_parents > 1 && !core->parents) {
2293 core->parents = kcalloc(core->num_parents, sizeof(struct clk *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002294 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002295 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002296 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002297 * clk_init'd; thus any access to clk->parents[] must check
2298 * for a NULL pointer. We can always perform lazy lookups for
2299 * missing parents later on.
2300 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002301 if (core->parents)
2302 for (i = 0; i < core->num_parents; i++)
2303 core->parents[i] =
2304 clk_core_lookup(core->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002305 }
2306
Stephen Boydd6968fc2015-04-30 13:54:13 -07002307 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002308
2309 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07002310 * Populate core->parent if parent has already been __clk_init'd. If
Mike Turquetteb24764902012-03-15 23:11:19 -07002311 * parent has not yet been __clk_init'd then place clk in the orphan
2312 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2313 * clk list.
2314 *
2315 * Every time a new clk is clk_init'd then we walk the list of orphan
2316 * clocks and re-parent any that are children of the clock currently
2317 * being clk_init'd.
2318 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002319 if (core->parent)
2320 hlist_add_head(&core->child_node,
2321 &core->parent->children);
2322 else if (core->flags & CLK_IS_ROOT)
2323 hlist_add_head(&core->child_node, &clk_root_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002324 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002325 hlist_add_head(&core->child_node, &clk_orphan_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002326
2327 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002328 * Set clk's accuracy. The preferred method is to use
2329 * .recalc_accuracy. For simple clocks and lazy developers the default
2330 * fallback is to use the parent's accuracy. If a clock doesn't have a
2331 * parent (or is orphaned) then accuracy is set to zero (perfect
2332 * clock).
2333 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002334 if (core->ops->recalc_accuracy)
2335 core->accuracy = core->ops->recalc_accuracy(core->hw,
2336 __clk_get_accuracy(core->parent));
2337 else if (core->parent)
2338 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002339 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002340 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002341
2342 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002343 * Set clk's phase.
2344 * Since a phase is by definition relative to its parent, just
2345 * query the current clock phase, or just assume it's in phase.
2346 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002347 if (core->ops->get_phase)
2348 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002349 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002350 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002351
2352 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002353 * Set clk's rate. The preferred method is to use .recalc_rate. For
2354 * simple clocks and lazy developers the default fallback is to use the
2355 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2356 * then rate is set to zero.
2357 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002358 if (core->ops->recalc_rate)
2359 rate = core->ops->recalc_rate(core->hw,
2360 clk_core_get_rate_nolock(core->parent));
2361 else if (core->parent)
2362 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002363 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002364 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002365 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002366
2367 /*
2368 * walk the list of orphan clocks and reparent any that are children of
2369 * this clock
2370 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002371 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002372 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002373 i = orphan->ops->get_parent(orphan->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002374 if (!strcmp(core->name, orphan->parent_names[i]))
2375 clk_core_reparent(orphan, core);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002376 continue;
2377 }
2378
Mike Turquetteb24764902012-03-15 23:11:19 -07002379 for (i = 0; i < orphan->num_parents; i++)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002380 if (!strcmp(core->name, orphan->parent_names[i])) {
2381 clk_core_reparent(orphan, core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002382 break;
2383 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002384 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002385
2386 /*
2387 * optional platform-specific magic
2388 *
2389 * The .init callback is not used by any of the basic clock types, but
2390 * exists for weird hardware that must perform initialization magic.
2391 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002392 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002393 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002394 if (core->ops->init)
2395 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002396
Stephen Boydd6968fc2015-04-30 13:54:13 -07002397 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002398out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002399 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002400
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002401 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002402 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002403
Mike Turquetted1302a32012-03-29 14:30:40 -07002404 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002405}
2406
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002407struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2408 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002409{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002410 struct clk *clk;
2411
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002412 /* This is to allow this function to be chained to others */
2413 if (!hw || IS_ERR(hw))
2414 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002415
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002416 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2417 if (!clk)
2418 return ERR_PTR(-ENOMEM);
2419
2420 clk->core = hw->core;
2421 clk->dev_id = dev_id;
2422 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002423 clk->max_rate = ULONG_MAX;
2424
2425 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002426 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002427 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002428
2429 return clk;
2430}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002431
Stephen Boyd73e0e492015-02-06 11:42:43 -08002432void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002433{
2434 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002435 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002436 clk_prepare_unlock();
2437
2438 kfree(clk);
2439}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002440
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002441/**
2442 * clk_register - allocate a new clock, register it and return an opaque cookie
2443 * @dev: device that is registering this clock
2444 * @hw: link to hardware-specific clock data
2445 *
2446 * clk_register is the primary interface for populating the clock tree with new
2447 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05302448 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002449 * rest of the clock API. In the event of an error clk_register will return an
2450 * error code; drivers must test for an error code after calling clk_register.
2451 */
2452struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002453{
Mike Turquetted1302a32012-03-29 14:30:40 -07002454 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002455 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002456
Stephen Boydd6968fc2015-04-30 13:54:13 -07002457 core = kzalloc(sizeof(*core), GFP_KERNEL);
2458 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002459 ret = -ENOMEM;
2460 goto fail_out;
2461 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002462
Stephen Boydd6968fc2015-04-30 13:54:13 -07002463 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2464 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002465 ret = -ENOMEM;
2466 goto fail_name;
2467 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002468 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002469 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002470 core->owner = dev->driver->owner;
2471 core->hw = hw;
2472 core->flags = hw->init->flags;
2473 core->num_parents = hw->init->num_parents;
2474 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002475
Mike Turquetted1302a32012-03-29 14:30:40 -07002476 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002477 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002478 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002479
Stephen Boydd6968fc2015-04-30 13:54:13 -07002480 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002481 ret = -ENOMEM;
2482 goto fail_parent_names;
2483 }
2484
2485
2486 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002487 for (i = 0; i < core->num_parents; i++) {
2488 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002489 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002490 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002491 ret = -ENOMEM;
2492 goto fail_parent_names_copy;
2493 }
2494 }
2495
Stephen Boydd6968fc2015-04-30 13:54:13 -07002496 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002497
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002498 hw->clk = __clk_create_clk(hw, NULL, NULL);
2499 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002500 ret = PTR_ERR(hw->clk);
2501 goto fail_parent_names_copy;
2502 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002503
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002504 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002505 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002506 return hw->clk;
2507
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002508 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002509 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002510
2511fail_parent_names_copy:
2512 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002513 kfree_const(core->parent_names[i]);
2514 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07002515fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002516 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002517fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002518 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002519fail_out:
2520 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002521}
2522EXPORT_SYMBOL_GPL(clk_register);
2523
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002524/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002525static void __clk_release(struct kref *ref)
2526{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002527 struct clk_core *core = container_of(ref, struct clk_core, ref);
2528 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002529
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002530 lockdep_assert_held(&prepare_lock);
2531
Stephen Boydd6968fc2015-04-30 13:54:13 -07002532 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002533 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002534 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002535
Stephen Boydd6968fc2015-04-30 13:54:13 -07002536 kfree(core->parent_names);
2537 kfree_const(core->name);
2538 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002539}
2540
2541/*
2542 * Empty clk_ops for unregistered clocks. These are used temporarily
2543 * after clk_unregister() was called on a clock and until last clock
2544 * consumer calls clk_put() and the struct clk object is freed.
2545 */
2546static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2547{
2548 return -ENXIO;
2549}
2550
2551static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2552{
2553 WARN_ON_ONCE(1);
2554}
2555
2556static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2557 unsigned long parent_rate)
2558{
2559 return -ENXIO;
2560}
2561
2562static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2563{
2564 return -ENXIO;
2565}
2566
2567static const struct clk_ops clk_nodrv_ops = {
2568 .enable = clk_nodrv_prepare_enable,
2569 .disable = clk_nodrv_disable_unprepare,
2570 .prepare = clk_nodrv_prepare_enable,
2571 .unprepare = clk_nodrv_disable_unprepare,
2572 .set_rate = clk_nodrv_set_rate,
2573 .set_parent = clk_nodrv_set_parent,
2574};
2575
Mark Brown1df5c932012-04-18 09:07:12 +01002576/**
2577 * clk_unregister - unregister a currently registered clock
2578 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002579 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002580void clk_unregister(struct clk *clk)
2581{
2582 unsigned long flags;
2583
Stephen Boyd6314b672014-09-04 23:37:49 -07002584 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2585 return;
2586
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002587 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002588
2589 clk_prepare_lock();
2590
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002591 if (clk->core->ops == &clk_nodrv_ops) {
2592 pr_err("%s: unregistered clock: %s\n", __func__,
2593 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002594 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002595 }
2596 /*
2597 * Assign empty clock ops for consumers that might still hold
2598 * a reference to this clock.
2599 */
2600 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002601 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002602 clk_enable_unlock(flags);
2603
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002604 if (!hlist_empty(&clk->core->children)) {
2605 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002606 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002607
2608 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002609 hlist_for_each_entry_safe(child, t, &clk->core->children,
2610 child_node)
2611 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002612 }
2613
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002614 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002615
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002616 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002617 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002618 __func__, clk->core->name);
2619 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002620
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002621 clk_prepare_unlock();
2622}
Mark Brown1df5c932012-04-18 09:07:12 +01002623EXPORT_SYMBOL_GPL(clk_unregister);
2624
Stephen Boyd46c87732012-09-24 13:38:04 -07002625static void devm_clk_release(struct device *dev, void *res)
2626{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002627 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002628}
2629
2630/**
2631 * devm_clk_register - resource managed clk_register()
2632 * @dev: device that is registering this clock
2633 * @hw: link to hardware-specific clock data
2634 *
2635 * Managed clk_register(). Clocks returned from this function are
2636 * automatically clk_unregister()ed on driver detach. See clk_register() for
2637 * more information.
2638 */
2639struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2640{
2641 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002642 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002643
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002644 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2645 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002646 return ERR_PTR(-ENOMEM);
2647
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002648 clk = clk_register(dev, hw);
2649 if (!IS_ERR(clk)) {
2650 *clkp = clk;
2651 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002652 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002653 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002654 }
2655
2656 return clk;
2657}
2658EXPORT_SYMBOL_GPL(devm_clk_register);
2659
2660static int devm_clk_match(struct device *dev, void *res, void *data)
2661{
2662 struct clk *c = res;
2663 if (WARN_ON(!c))
2664 return 0;
2665 return c == data;
2666}
2667
2668/**
2669 * devm_clk_unregister - resource managed clk_unregister()
2670 * @clk: clock to unregister
2671 *
2672 * Deallocate a clock allocated with devm_clk_register(). Normally
2673 * this function will not need to be called and the resource management
2674 * code will ensure that the resource is freed.
2675 */
2676void devm_clk_unregister(struct device *dev, struct clk *clk)
2677{
2678 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2679}
2680EXPORT_SYMBOL_GPL(devm_clk_unregister);
2681
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002682/*
2683 * clkdev helpers
2684 */
2685int __clk_get(struct clk *clk)
2686{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002687 struct clk_core *core = !clk ? NULL : clk->core;
2688
2689 if (core) {
2690 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002691 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002692
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002693 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002694 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002695 return 1;
2696}
2697
2698void __clk_put(struct clk *clk)
2699{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002700 struct module *owner;
2701
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002702 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002703 return;
2704
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002705 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002706
Stephen Boyd50595f82015-02-06 11:42:44 -08002707 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002708 if (clk->min_rate > clk->core->req_rate ||
2709 clk->max_rate < clk->core->req_rate)
2710 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2711
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002712 owner = clk->core->owner;
2713 kref_put(&clk->core->ref, __clk_release);
2714
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002715 clk_prepare_unlock();
2716
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002717 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002718
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002719 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002720}
2721
Mike Turquetteb24764902012-03-15 23:11:19 -07002722/*** clk rate change notifiers ***/
2723
2724/**
2725 * clk_notifier_register - add a clk rate change notifier
2726 * @clk: struct clk * to watch
2727 * @nb: struct notifier_block * with callback info
2728 *
2729 * Request notification when clk's rate changes. This uses an SRCU
2730 * notifier because we want it to block and notifier unregistrations are
2731 * uncommon. The callbacks associated with the notifier must not
2732 * re-enter into the clk framework by calling any top-level clk APIs;
2733 * this will cause a nested prepare_lock mutex.
2734 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002735 * In all notification cases cases (pre, post and abort rate change) the
2736 * original clock rate is passed to the callback via struct
2737 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002738 * clk_notifier_data.new_rate.
2739 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002740 * clk_notifier_register() must be called from non-atomic context.
2741 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2742 * allocation failure; otherwise, passes along the return value of
2743 * srcu_notifier_chain_register().
2744 */
2745int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2746{
2747 struct clk_notifier *cn;
2748 int ret = -ENOMEM;
2749
2750 if (!clk || !nb)
2751 return -EINVAL;
2752
Mike Turquetteeab89f62013-03-28 13:59:01 -07002753 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002754
2755 /* search the list of notifiers for this clk */
2756 list_for_each_entry(cn, &clk_notifier_list, node)
2757 if (cn->clk == clk)
2758 break;
2759
2760 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2761 if (cn->clk != clk) {
2762 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2763 if (!cn)
2764 goto out;
2765
2766 cn->clk = clk;
2767 srcu_init_notifier_head(&cn->notifier_head);
2768
2769 list_add(&cn->node, &clk_notifier_list);
2770 }
2771
2772 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2773
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002774 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002775
2776out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002777 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002778
2779 return ret;
2780}
2781EXPORT_SYMBOL_GPL(clk_notifier_register);
2782
2783/**
2784 * clk_notifier_unregister - remove a clk rate change notifier
2785 * @clk: struct clk *
2786 * @nb: struct notifier_block * with callback info
2787 *
2788 * Request no further notification for changes to 'clk' and frees memory
2789 * allocated in clk_notifier_register.
2790 *
2791 * Returns -EINVAL if called with null arguments; otherwise, passes
2792 * along the return value of srcu_notifier_chain_unregister().
2793 */
2794int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2795{
2796 struct clk_notifier *cn = NULL;
2797 int ret = -EINVAL;
2798
2799 if (!clk || !nb)
2800 return -EINVAL;
2801
Mike Turquetteeab89f62013-03-28 13:59:01 -07002802 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002803
2804 list_for_each_entry(cn, &clk_notifier_list, node)
2805 if (cn->clk == clk)
2806 break;
2807
2808 if (cn->clk == clk) {
2809 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2810
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002811 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002812
2813 /* XXX the notifier code should handle this better */
2814 if (!cn->notifier_head.head) {
2815 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002816 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002817 kfree(cn);
2818 }
2819
2820 } else {
2821 ret = -ENOENT;
2822 }
2823
Mike Turquetteeab89f62013-03-28 13:59:01 -07002824 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002825
2826 return ret;
2827}
2828EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002829
2830#ifdef CONFIG_OF
2831/**
2832 * struct of_clk_provider - Clock provider registration structure
2833 * @link: Entry in global list of clock providers
2834 * @node: Pointer to device tree node of clock provider
2835 * @get: Get clock callback. Returns NULL or a struct clk for the
2836 * given clock specifier
2837 * @data: context pointer to be passed into @get callback
2838 */
2839struct of_clk_provider {
2840 struct list_head link;
2841
2842 struct device_node *node;
2843 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2844 void *data;
2845};
2846
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302847static const struct of_device_id __clk_of_table_sentinel
2848 __used __section(__clk_of_table_end);
2849
Grant Likely766e6a42012-04-09 14:50:06 -05002850static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002851static DEFINE_MUTEX(of_clk_mutex);
2852
Grant Likely766e6a42012-04-09 14:50:06 -05002853struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2854 void *data)
2855{
2856 return data;
2857}
2858EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2859
Shawn Guo494bfec2012-08-22 21:36:27 +08002860struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2861{
2862 struct clk_onecell_data *clk_data = data;
2863 unsigned int idx = clkspec->args[0];
2864
2865 if (idx >= clk_data->clk_num) {
2866 pr_err("%s: invalid clock index %d\n", __func__, idx);
2867 return ERR_PTR(-EINVAL);
2868 }
2869
2870 return clk_data->clks[idx];
2871}
2872EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2873
Grant Likely766e6a42012-04-09 14:50:06 -05002874/**
2875 * of_clk_add_provider() - Register a clock provider for a node
2876 * @np: Device node pointer associated with clock provider
2877 * @clk_src_get: callback for decoding clock
2878 * @data: context pointer for @clk_src_get callback.
2879 */
2880int of_clk_add_provider(struct device_node *np,
2881 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2882 void *data),
2883 void *data)
2884{
2885 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002886 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002887
2888 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2889 if (!cp)
2890 return -ENOMEM;
2891
2892 cp->node = of_node_get(np);
2893 cp->data = data;
2894 cp->get = clk_src_get;
2895
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002896 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002897 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002898 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002899 pr_debug("Added clock from %s\n", np->full_name);
2900
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002901 ret = of_clk_set_defaults(np, true);
2902 if (ret < 0)
2903 of_clk_del_provider(np);
2904
2905 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002906}
2907EXPORT_SYMBOL_GPL(of_clk_add_provider);
2908
2909/**
2910 * of_clk_del_provider() - Remove a previously registered clock provider
2911 * @np: Device node pointer associated with clock provider
2912 */
2913void of_clk_del_provider(struct device_node *np)
2914{
2915 struct of_clk_provider *cp;
2916
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002917 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002918 list_for_each_entry(cp, &of_clk_providers, link) {
2919 if (cp->node == np) {
2920 list_del(&cp->link);
2921 of_node_put(cp->node);
2922 kfree(cp);
2923 break;
2924 }
2925 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002926 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002927}
2928EXPORT_SYMBOL_GPL(of_clk_del_provider);
2929
Stephen Boyd73e0e492015-02-06 11:42:43 -08002930struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
2931 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05002932{
2933 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002934 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002935
Stephen Boyd306c3422015-02-05 15:39:11 -08002936 if (!clkspec)
2937 return ERR_PTR(-EINVAL);
2938
Grant Likely766e6a42012-04-09 14:50:06 -05002939 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08002940 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002941 list_for_each_entry(provider, &of_clk_providers, link) {
2942 if (provider->node == clkspec->np)
2943 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002944 if (!IS_ERR(clk)) {
2945 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
2946 con_id);
2947
2948 if (!IS_ERR(clk) && !__clk_get(clk)) {
2949 __clk_free_clk(clk);
2950 clk = ERR_PTR(-ENOENT);
2951 }
2952
Grant Likely766e6a42012-04-09 14:50:06 -05002953 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08002954 }
Grant Likely766e6a42012-04-09 14:50:06 -05002955 }
Stephen Boyd306c3422015-02-05 15:39:11 -08002956 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002957
2958 return clk;
2959}
2960
Stephen Boyd306c3422015-02-05 15:39:11 -08002961/**
2962 * of_clk_get_from_provider() - Lookup a clock from a clock provider
2963 * @clkspec: pointer to a clock specifier data structure
2964 *
2965 * This function looks up a struct clk from the registered list of clock
2966 * providers, an input is a clock specifier data structure as returned
2967 * from the of_parse_phandle_with_args() function call.
2968 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002969struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2970{
Stephen Boyd306c3422015-02-05 15:39:11 -08002971 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05002972}
2973
Mike Turquettef6102742013-10-07 23:12:13 -07002974int of_clk_get_parent_count(struct device_node *np)
2975{
2976 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2977}
2978EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2979
Grant Likely766e6a42012-04-09 14:50:06 -05002980const char *of_clk_get_parent_name(struct device_node *np, int index)
2981{
2982 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002983 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05002984 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002985 const __be32 *vp;
2986 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05002987 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002988 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05002989
2990 if (index < 0)
2991 return NULL;
2992
2993 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2994 &clkspec);
2995 if (rc)
2996 return NULL;
2997
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002998 index = clkspec.args_count ? clkspec.args[0] : 0;
2999 count = 0;
3000
3001 /* if there is an indices property, use it to transfer the index
3002 * specified into an array offset for the clock-output-names property.
3003 */
3004 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3005 if (index == pv) {
3006 index = count;
3007 break;
3008 }
3009 count++;
3010 }
3011
Grant Likely766e6a42012-04-09 14:50:06 -05003012 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003013 index,
Grant Likely766e6a42012-04-09 14:50:06 -05003014 &clk_name) < 0)
3015 clk_name = clkspec.np->name;
3016
3017 of_node_put(clkspec.np);
3018 return clk_name;
3019}
3020EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3021
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05003022/**
3023 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3024 * number of parents
3025 * @np: Device node pointer associated with clock provider
3026 * @parents: pointer to char array that hold the parents' names
3027 * @size: size of the @parents array
3028 *
3029 * Return: number of parents for the clock node.
3030 */
3031int of_clk_parent_fill(struct device_node *np, const char **parents,
3032 unsigned int size)
3033{
3034 unsigned int i = 0;
3035
3036 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3037 i++;
3038
3039 return i;
3040}
3041EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3042
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003043struct clock_provider {
3044 of_clk_init_cb_t clk_init_cb;
3045 struct device_node *np;
3046 struct list_head node;
3047};
3048
3049static LIST_HEAD(clk_provider_list);
3050
3051/*
3052 * This function looks for a parent clock. If there is one, then it
3053 * checks that the provider for this parent clock was initialized, in
3054 * this case the parent clock will be ready.
3055 */
3056static int parent_ready(struct device_node *np)
3057{
3058 int i = 0;
3059
3060 while (true) {
3061 struct clk *clk = of_clk_get(np, i);
3062
3063 /* this parent is ready we can check the next one */
3064 if (!IS_ERR(clk)) {
3065 clk_put(clk);
3066 i++;
3067 continue;
3068 }
3069
3070 /* at least one parent is not ready, we exit now */
3071 if (PTR_ERR(clk) == -EPROBE_DEFER)
3072 return 0;
3073
3074 /*
3075 * Here we make assumption that the device tree is
3076 * written correctly. So an error means that there is
3077 * no more parent. As we didn't exit yet, then the
3078 * previous parent are ready. If there is no clock
3079 * parent, no need to wait for them, then we can
3080 * consider their absence as being ready
3081 */
3082 return 1;
3083 }
3084}
3085
Grant Likely766e6a42012-04-09 14:50:06 -05003086/**
3087 * of_clk_init() - Scan and init clock providers from the DT
3088 * @matches: array of compatible values and init functions for providers.
3089 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003090 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003091 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003092 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003093 */
3094void __init of_clk_init(const struct of_device_id *matches)
3095{
Alex Elder7f7ed582013-08-22 11:31:31 -05003096 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003097 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003098 struct clock_provider *clk_provider, *next;
3099 bool is_init_done;
3100 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05003101
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303102 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003103 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303104
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003105 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003106 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003107 struct clock_provider *parent =
3108 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
3109
3110 parent->clk_init_cb = match->data;
3111 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003112 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003113 }
3114
3115 while (!list_empty(&clk_provider_list)) {
3116 is_init_done = false;
3117 list_for_each_entry_safe(clk_provider, next,
3118 &clk_provider_list, node) {
3119 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003120
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003121 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003122 of_clk_set_defaults(clk_provider->np, true);
3123
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003124 list_del(&clk_provider->node);
3125 kfree(clk_provider);
3126 is_init_done = true;
3127 }
3128 }
3129
3130 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003131 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003132 * remaining providers during the last loop, so now we
3133 * initialize all the remaining ones unconditionally
3134 * in case the clock parent was not mandatory
3135 */
3136 if (!is_init_done)
3137 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003138 }
3139}
3140#endif