blob: c907289ff03c992c8889566a2bb45dae906303cd [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
Boris Brezillon0817b622015-07-07 20:48:08 +0200439static int
440clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800441 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100442{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100443 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200444 int i, num_parents, ret;
445 unsigned long best = 0;
446 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100447
448 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100449 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
450 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200451 if (core->flags & CLK_SET_RATE_PARENT) {
452 ret = __clk_determine_rate(parent ? parent->hw : NULL,
453 &parent_req);
454 if (ret)
455 return ret;
456
457 best = parent_req.rate;
458 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100459 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200460 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100461 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200462 }
463
James Hogane366fdd2013-07-29 12:25:02 +0100464 goto out;
465 }
466
467 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100468 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100469 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100470 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100471 if (!parent)
472 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200473
474 if (core->flags & CLK_SET_RATE_PARENT) {
475 parent_req = *req;
476 ret = __clk_determine_rate(parent->hw, &parent_req);
477 if (ret)
478 continue;
479 } else {
480 parent_req.rate = clk_core_get_rate_nolock(parent);
481 }
482
483 if (mux_is_better_rate(req->rate, parent_req.rate,
484 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100485 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200486 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100487 }
488 }
489
Boris Brezillon57d866e2015-07-09 22:39:38 +0200490 if (!best_parent)
491 return -EINVAL;
492
James Hogane366fdd2013-07-29 12:25:02 +0100493out:
494 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200495 req->best_parent_hw = best_parent->hw;
496 req->best_parent_rate = best;
497 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100498
Boris Brezillon0817b622015-07-07 20:48:08 +0200499 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100500}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800501
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100502struct clk *__clk_lookup(const char *name)
503{
504 struct clk_core *core = clk_core_lookup(name);
505
506 return !core ? NULL : core->hw->clk;
507}
508
Stephen Boydd6968fc2015-04-30 13:54:13 -0700509static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100510 unsigned long *min_rate,
511 unsigned long *max_rate)
512{
513 struct clk *clk_user;
514
515 *min_rate = 0;
516 *max_rate = ULONG_MAX;
517
Stephen Boydd6968fc2015-04-30 13:54:13 -0700518 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100519 *min_rate = max(*min_rate, clk_user->min_rate);
520
Stephen Boydd6968fc2015-04-30 13:54:13 -0700521 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100522 *max_rate = min(*max_rate, clk_user->max_rate);
523}
524
Stephen Boyd15a02c12015-01-19 18:05:28 -0800525/*
526 * Helper for finding best parent to provide a given frequency. This can be used
527 * directly as a determine_rate callback (e.g. for a mux), or from a more
528 * complex clock that may combine a mux with other operations.
529 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200530int __clk_mux_determine_rate(struct clk_hw *hw,
531 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800532{
Boris Brezillon0817b622015-07-07 20:48:08 +0200533 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800534}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800535EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100536
Boris Brezillon0817b622015-07-07 20:48:08 +0200537int __clk_mux_determine_rate_closest(struct clk_hw *hw,
538 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800539{
Boris Brezillon0817b622015-07-07 20:48:08 +0200540 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800541}
542EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
543
Mike Turquetteb24764902012-03-15 23:11:19 -0700544/*** clk api ***/
545
Stephen Boydd6968fc2015-04-30 13:54:13 -0700546static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700547{
Stephen Boyda6334722015-05-06 17:00:54 -0700548 lockdep_assert_held(&prepare_lock);
549
Stephen Boydd6968fc2015-04-30 13:54:13 -0700550 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700551 return;
552
Stephen Boydd6968fc2015-04-30 13:54:13 -0700553 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700554 return;
555
Stephen Boydd6968fc2015-04-30 13:54:13 -0700556 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700557 return;
558
Stephen Boydd6968fc2015-04-30 13:54:13 -0700559 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700560
Stephen Boydd6968fc2015-04-30 13:54:13 -0700561 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800562
Stephen Boydd6968fc2015-04-30 13:54:13 -0700563 if (core->ops->unprepare)
564 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700565
Stephen Boydd6968fc2015-04-30 13:54:13 -0700566 trace_clk_unprepare_complete(core);
567 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700568}
569
570/**
571 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200572 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700573 *
574 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
575 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
576 * if the operation may sleep. One example is a clk which is accessed over
577 * I2c. In the complex case a clk gate operation may require a fast and a slow
578 * part. It is this reason that clk_unprepare and clk_disable are not mutually
579 * exclusive. In fact clk_disable must be called before clk_unprepare.
580 */
581void clk_unprepare(struct clk *clk)
582{
Stephen Boyd63589e92014-03-26 16:06:37 -0700583 if (IS_ERR_OR_NULL(clk))
584 return;
585
Mike Turquetteeab89f62013-03-28 13:59:01 -0700586 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100587 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700588 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700589}
590EXPORT_SYMBOL_GPL(clk_unprepare);
591
Stephen Boydd6968fc2015-04-30 13:54:13 -0700592static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700593{
594 int ret = 0;
595
Stephen Boyda6334722015-05-06 17:00:54 -0700596 lockdep_assert_held(&prepare_lock);
597
Stephen Boydd6968fc2015-04-30 13:54:13 -0700598 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700599 return 0;
600
Stephen Boydd6968fc2015-04-30 13:54:13 -0700601 if (core->prepare_count == 0) {
602 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700603 if (ret)
604 return ret;
605
Stephen Boydd6968fc2015-04-30 13:54:13 -0700606 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800607
Stephen Boydd6968fc2015-04-30 13:54:13 -0700608 if (core->ops->prepare)
609 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800610
Stephen Boydd6968fc2015-04-30 13:54:13 -0700611 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800612
613 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700614 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800615 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700616 }
617 }
618
Stephen Boydd6968fc2015-04-30 13:54:13 -0700619 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700620
621 return 0;
622}
623
624/**
625 * clk_prepare - prepare a clock source
626 * @clk: the clk being prepared
627 *
628 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
629 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
630 * operation may sleep. One example is a clk which is accessed over I2c. In
631 * the complex case a clk ungate operation may require a fast and a slow part.
632 * It is this reason that clk_prepare and clk_enable are not mutually
633 * exclusive. In fact clk_prepare must be called before clk_enable.
634 * Returns 0 on success, -EERROR otherwise.
635 */
636int clk_prepare(struct clk *clk)
637{
638 int ret;
639
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100640 if (!clk)
641 return 0;
642
Mike Turquetteeab89f62013-03-28 13:59:01 -0700643 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100644 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700645 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700646
647 return ret;
648}
649EXPORT_SYMBOL_GPL(clk_prepare);
650
Stephen Boydd6968fc2015-04-30 13:54:13 -0700651static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700652{
Stephen Boyda6334722015-05-06 17:00:54 -0700653 lockdep_assert_held(&enable_lock);
654
Stephen Boydd6968fc2015-04-30 13:54:13 -0700655 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700656 return;
657
Stephen Boydd6968fc2015-04-30 13:54:13 -0700658 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700659 return;
660
Stephen Boydd6968fc2015-04-30 13:54:13 -0700661 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700662 return;
663
Stephen Boydd6968fc2015-04-30 13:54:13 -0700664 trace_clk_disable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800665
Stephen Boydd6968fc2015-04-30 13:54:13 -0700666 if (core->ops->disable)
667 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700668
Stephen Boydd6968fc2015-04-30 13:54:13 -0700669 trace_clk_disable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800670
Stephen Boydd6968fc2015-04-30 13:54:13 -0700671 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100672}
673
Mike Turquetteb24764902012-03-15 23:11:19 -0700674/**
675 * clk_disable - gate a clock
676 * @clk: the clk being gated
677 *
678 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
679 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
680 * clk if the operation is fast and will never sleep. One example is a
681 * SoC-internal clk which is controlled via simple register writes. In the
682 * complex case a clk gate operation may require a fast and a slow part. It is
683 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
684 * In fact clk_disable must be called before clk_unprepare.
685 */
686void clk_disable(struct clk *clk)
687{
688 unsigned long flags;
689
Stephen Boyd63589e92014-03-26 16:06:37 -0700690 if (IS_ERR_OR_NULL(clk))
691 return;
692
Mike Turquetteeab89f62013-03-28 13:59:01 -0700693 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700694 clk_core_disable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700695 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700696}
697EXPORT_SYMBOL_GPL(clk_disable);
698
Stephen Boydd6968fc2015-04-30 13:54:13 -0700699static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700700{
701 int ret = 0;
702
Stephen Boyda6334722015-05-06 17:00:54 -0700703 lockdep_assert_held(&enable_lock);
704
Stephen Boydd6968fc2015-04-30 13:54:13 -0700705 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700706 return 0;
707
Stephen Boydd6968fc2015-04-30 13:54:13 -0700708 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700709 return -ESHUTDOWN;
710
Stephen Boydd6968fc2015-04-30 13:54:13 -0700711 if (core->enable_count == 0) {
712 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700713
714 if (ret)
715 return ret;
716
Stephen Boydd6968fc2015-04-30 13:54:13 -0700717 trace_clk_enable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800718
Stephen Boydd6968fc2015-04-30 13:54:13 -0700719 if (core->ops->enable)
720 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800721
Stephen Boydd6968fc2015-04-30 13:54:13 -0700722 trace_clk_enable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800723
724 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700725 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800726 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700727 }
728 }
729
Stephen Boydd6968fc2015-04-30 13:54:13 -0700730 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700731 return 0;
732}
733
734/**
735 * clk_enable - ungate a clock
736 * @clk: the clk being ungated
737 *
738 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
739 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
740 * if the operation will never sleep. One example is a SoC-internal clk which
741 * is controlled via simple register writes. In the complex case a clk ungate
742 * operation may require a fast and a slow part. It is this reason that
743 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
744 * must be called before clk_enable. Returns 0 on success, -EERROR
745 * otherwise.
746 */
747int clk_enable(struct clk *clk)
748{
749 unsigned long flags;
750 int ret;
751
Dong Aisheng864e1602015-04-30 14:02:19 -0700752 if (!clk)
753 return 0;
754
Mike Turquetteeab89f62013-03-28 13:59:01 -0700755 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700756 ret = clk_core_enable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700757 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700758
759 return ret;
760}
761EXPORT_SYMBOL_GPL(clk_enable);
762
Boris Brezillon0817b622015-07-07 20:48:08 +0200763static int clk_core_round_rate_nolock(struct clk_core *core,
764 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -0700765{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100766 struct clk_core *parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200767 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700768
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100769 lockdep_assert_held(&prepare_lock);
770
Stephen Boydd6968fc2015-04-30 13:54:13 -0700771 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700772 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700773
Stephen Boydd6968fc2015-04-30 13:54:13 -0700774 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200775 if (parent) {
776 req->best_parent_hw = parent->hw;
777 req->best_parent_rate = parent->rate;
778 } else {
779 req->best_parent_hw = NULL;
780 req->best_parent_rate = 0;
781 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700782
Stephen Boydd6968fc2015-04-30 13:54:13 -0700783 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +0200784 return core->ops->determine_rate(core->hw, req);
785 } else if (core->ops->round_rate) {
786 rate = core->ops->round_rate(core->hw, req->rate,
787 &req->best_parent_rate);
788 if (rate < 0)
789 return rate;
790
791 req->rate = rate;
792 } else if (core->flags & CLK_SET_RATE_PARENT) {
793 return clk_core_round_rate_nolock(parent, req);
794 } else {
795 req->rate = core->rate;
796 }
797
798 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700799}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100800
801/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100802 * __clk_determine_rate - get the closest rate actually supported by a clock
803 * @hw: determine the rate of this clock
804 * @rate: target rate
805 * @min_rate: returned rate must be greater than this rate
806 * @max_rate: returned rate must be less than this rate
807 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700808 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100809 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200810int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100811{
Boris Brezillon0817b622015-07-07 20:48:08 +0200812 if (!hw) {
813 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100814 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +0200815 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100816
Boris Brezillon0817b622015-07-07 20:48:08 +0200817 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100818}
819EXPORT_SYMBOL_GPL(__clk_determine_rate);
820
821/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100822 * __clk_round_rate - round the given rate for a clk
823 * @clk: round the rate of this clock
824 * @rate: the rate which is to be rounded
825 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700826 * Useful for clk_ops such as .set_rate
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100827 */
828unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
829{
Boris Brezillon0817b622015-07-07 20:48:08 +0200830 struct clk_rate_request req;
831 int ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100832
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100833 if (!clk)
834 return 0;
835
Boris Brezillon0817b622015-07-07 20:48:08 +0200836 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
837 req.rate = rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100838
Boris Brezillon0817b622015-07-07 20:48:08 +0200839 ret = clk_core_round_rate_nolock(clk->core, &req);
840 if (ret)
841 return 0;
842
843 return req.rate;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100844}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +0200845EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700846
847/**
848 * clk_round_rate - round the given rate for a clk
849 * @clk: the clk for which we are rounding a rate
850 * @rate: the rate which is to be rounded
851 *
852 * Takes in a rate as input and rounds it to a rate that the clk can actually
853 * use which is then returned. If clk doesn't support round_rate operation
854 * then the parent rate is returned.
855 */
856long clk_round_rate(struct clk *clk, unsigned long rate)
857{
858 unsigned long ret;
859
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100860 if (!clk)
861 return 0;
862
Mike Turquetteeab89f62013-03-28 13:59:01 -0700863 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700864 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700865 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700866
867 return ret;
868}
869EXPORT_SYMBOL_GPL(clk_round_rate);
870
871/**
872 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -0700873 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -0700874 * @msg: clk notifier type (see include/linux/clk.h)
875 * @old_rate: old clk rate
876 * @new_rate: new clk rate
877 *
878 * Triggers a notifier call chain on the clk rate-change notification
879 * for 'clk'. Passes a pointer to the struct clk and the previous
880 * and current rates to the notifier callback. Intended to be called by
881 * internal clock code only. Returns NOTIFY_DONE from the last driver
882 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
883 * a driver returns that.
884 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700885static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -0700886 unsigned long old_rate, unsigned long new_rate)
887{
888 struct clk_notifier *cn;
889 struct clk_notifier_data cnd;
890 int ret = NOTIFY_DONE;
891
Mike Turquetteb24764902012-03-15 23:11:19 -0700892 cnd.old_rate = old_rate;
893 cnd.new_rate = new_rate;
894
895 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700896 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100897 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700898 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
899 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -0700900 }
901 }
902
903 return ret;
904}
905
906/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100907 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -0700908 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100909 *
910 * Walks the subtree of clks starting with clk and recalculates accuracies as
911 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700912 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100913 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100914 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700915static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100916{
917 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100918 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100919
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100920 lockdep_assert_held(&prepare_lock);
921
Stephen Boydd6968fc2015-04-30 13:54:13 -0700922 if (core->parent)
923 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100924
Stephen Boydd6968fc2015-04-30 13:54:13 -0700925 if (core->ops->recalc_accuracy)
926 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100927 parent_accuracy);
928 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700929 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100930
Stephen Boydd6968fc2015-04-30 13:54:13 -0700931 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100932 __clk_recalc_accuracies(child);
933}
934
Stephen Boydd6968fc2015-04-30 13:54:13 -0700935static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100936{
937 unsigned long accuracy;
938
939 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -0700940 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
941 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100942
Stephen Boydd6968fc2015-04-30 13:54:13 -0700943 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100944 clk_prepare_unlock();
945
946 return accuracy;
947}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100948
949/**
950 * clk_get_accuracy - return the accuracy of clk
951 * @clk: the clk whose accuracy is being returned
952 *
953 * Simply returns the cached accuracy of the clk, unless
954 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
955 * issued.
956 * If clk is NULL then returns 0.
957 */
958long clk_get_accuracy(struct clk *clk)
959{
960 if (!clk)
961 return 0;
962
963 return clk_core_get_accuracy(clk->core);
964}
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100965EXPORT_SYMBOL_GPL(clk_get_accuracy);
966
Stephen Boydd6968fc2015-04-30 13:54:13 -0700967static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100968 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700969{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700970 if (core->ops->recalc_rate)
971 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700972 return parent_rate;
973}
974
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100975/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700976 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -0700977 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -0700978 * @msg: notification type (see include/linux/clk.h)
979 *
980 * Walks the subtree of clks starting with clk and recalculates rates as it
981 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200982 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -0700983 *
984 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
985 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -0700986 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700987static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -0700988{
989 unsigned long old_rate;
990 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100991 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700992
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100993 lockdep_assert_held(&prepare_lock);
994
Stephen Boydd6968fc2015-04-30 13:54:13 -0700995 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700996
Stephen Boydd6968fc2015-04-30 13:54:13 -0700997 if (core->parent)
998 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700999
Stephen Boydd6968fc2015-04-30 13:54:13 -07001000 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001001
1002 /*
1003 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1004 * & ABORT_RATE_CHANGE notifiers
1005 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001006 if (core->notifier_count && msg)
1007 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001008
Stephen Boydd6968fc2015-04-30 13:54:13 -07001009 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001010 __clk_recalc_rates(child, msg);
1011}
1012
Stephen Boydd6968fc2015-04-30 13:54:13 -07001013static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001014{
1015 unsigned long rate;
1016
1017 clk_prepare_lock();
1018
Stephen Boydd6968fc2015-04-30 13:54:13 -07001019 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1020 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001021
Stephen Boydd6968fc2015-04-30 13:54:13 -07001022 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001023 clk_prepare_unlock();
1024
1025 return rate;
1026}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001027
Mike Turquetteb24764902012-03-15 23:11:19 -07001028/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001029 * clk_get_rate - return the rate of clk
1030 * @clk: the clk whose rate is being returned
1031 *
1032 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1033 * is set, which means a recalc_rate will be issued.
1034 * If clk is NULL then returns 0.
1035 */
1036unsigned long clk_get_rate(struct clk *clk)
1037{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001038 if (!clk)
1039 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001040
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001041 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001042}
1043EXPORT_SYMBOL_GPL(clk_get_rate);
1044
Stephen Boydd6968fc2015-04-30 13:54:13 -07001045static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001046 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001047{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001048 int i;
James Hogan4935b222013-07-29 12:24:59 +01001049
Stephen Boydd6968fc2015-04-30 13:54:13 -07001050 if (!core->parents) {
1051 core->parents = kcalloc(core->num_parents,
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001052 sizeof(struct clk *), GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001053 if (!core->parents)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001054 return -ENOMEM;
1055 }
James Hogan4935b222013-07-29 12:24:59 +01001056
1057 /*
1058 * find index of new parent clock using cached parent ptrs,
1059 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001060 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001061 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001062 for (i = 0; i < core->num_parents; i++) {
1063 if (core->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001064 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001065
Stephen Boydd6968fc2015-04-30 13:54:13 -07001066 if (core->parents[i])
Tomasz Figada0f0b22013-09-29 02:37:16 +02001067 continue;
1068
Stephen Boydd6968fc2015-04-30 13:54:13 -07001069 if (!strcmp(core->parent_names[i], parent->name)) {
1070 core->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001071 return i;
James Hogan4935b222013-07-29 12:24:59 +01001072 }
1073 }
1074
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001075 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001076}
1077
Stephen Boydd6968fc2015-04-30 13:54:13 -07001078static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001079{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001080 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001081
James Hogan903efc52013-08-29 12:10:51 +01001082 if (new_parent) {
1083 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001084 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001085 new_parent->new_child = NULL;
1086
Stephen Boydd6968fc2015-04-30 13:54:13 -07001087 hlist_add_head(&core->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001088 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001089 hlist_add_head(&core->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001090 }
James Hogan4935b222013-07-29 12:24:59 +01001091
Stephen Boydd6968fc2015-04-30 13:54:13 -07001092 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001093}
1094
Stephen Boydd6968fc2015-04-30 13:54:13 -07001095static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001096 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001097{
1098 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001099 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001100
1101 /*
1102 * Migrate prepare state between parents and prevent race with
1103 * clk_enable().
1104 *
1105 * If the clock is not prepared, then a race with
1106 * clk_enable/disable() is impossible since we already have the
1107 * prepare lock (future calls to clk_enable() need to be preceded by
1108 * a clk_prepare()).
1109 *
1110 * If the clock is prepared, migrate the prepared state to the new
1111 * parent and also protect against a race with clk_enable() by
1112 * forcing the clock and the new parent on. This ensures that all
1113 * future calls to clk_enable() are practically NOPs with respect to
1114 * hardware and software states.
1115 *
1116 * See also: Comment for clk_set_parent() below.
1117 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001118 if (core->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001119 clk_core_prepare(parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001120 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001121 clk_core_enable(parent);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001122 clk_core_enable(core);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001123 clk_enable_unlock(flags);
James Hogan4935b222013-07-29 12:24:59 +01001124 }
1125
1126 /* update the clk tree topology */
1127 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001128 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001129 clk_enable_unlock(flags);
1130
Stephen Boyd3fa22522014-01-15 10:47:22 -08001131 return old_parent;
1132}
1133
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001134static void __clk_set_parent_after(struct clk_core *core,
1135 struct clk_core *parent,
1136 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001137{
Dong Aishengd2a5d462015-04-15 22:26:36 +08001138 unsigned long flags;
1139
Stephen Boyd3fa22522014-01-15 10:47:22 -08001140 /*
1141 * Finish the migration of prepare state and undo the changes done
1142 * for preventing a race with clk_enable().
1143 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001144 if (core->prepare_count) {
Dong Aishengd2a5d462015-04-15 22:26:36 +08001145 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001146 clk_core_disable(core);
1147 clk_core_disable(old_parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001148 clk_enable_unlock(flags);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001149 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001150 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001151}
1152
Stephen Boydd6968fc2015-04-30 13:54:13 -07001153static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001154 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001155{
1156 unsigned long flags;
1157 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001158 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001159
Stephen Boydd6968fc2015-04-30 13:54:13 -07001160 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001161
Stephen Boydd6968fc2015-04-30 13:54:13 -07001162 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001163
James Hogan4935b222013-07-29 12:24:59 +01001164 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001165 if (parent && core->ops->set_parent)
1166 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001167
Stephen Boydd6968fc2015-04-30 13:54:13 -07001168 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001169
James Hogan4935b222013-07-29 12:24:59 +01001170 if (ret) {
1171 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001172 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001173 clk_enable_unlock(flags);
1174
Stephen Boydd6968fc2015-04-30 13:54:13 -07001175 if (core->prepare_count) {
Dong Aishengd2a5d462015-04-15 22:26:36 +08001176 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001177 clk_core_disable(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001178 clk_core_disable(parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001179 clk_enable_unlock(flags);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001180 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001181 }
1182 return ret;
1183 }
1184
Stephen Boydd6968fc2015-04-30 13:54:13 -07001185 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001186
James Hogan4935b222013-07-29 12:24:59 +01001187 return 0;
1188}
1189
Ulf Hanssona093bde2012-08-31 14:21:28 +02001190/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001191 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001192 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001193 * @parent_rate: the "future" rate of clk's parent
1194 *
1195 * Walks the subtree of clks starting with clk, speculating rates as it
1196 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1197 *
1198 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1199 * pre-rate change notifications and returns early if no clks in the
1200 * subtree have subscribed to the notifications. Note that if a clk does not
1201 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001202 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001203 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001204static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001205 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001206{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001207 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001208 unsigned long new_rate;
1209 int ret = NOTIFY_DONE;
1210
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001211 lockdep_assert_held(&prepare_lock);
1212
Stephen Boydd6968fc2015-04-30 13:54:13 -07001213 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001214
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001215 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001216 if (core->notifier_count)
1217 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001218
Mike Turquette86bcfa22014-02-24 16:08:41 -08001219 if (ret & NOTIFY_STOP_MASK) {
1220 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001221 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001222 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001223 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001224
Stephen Boydd6968fc2015-04-30 13:54:13 -07001225 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001226 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001227 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001228 break;
1229 }
1230
1231out:
1232 return ret;
1233}
1234
Stephen Boydd6968fc2015-04-30 13:54:13 -07001235static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001236 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001237{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001238 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001239
Stephen Boydd6968fc2015-04-30 13:54:13 -07001240 core->new_rate = new_rate;
1241 core->new_parent = new_parent;
1242 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001243 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001244 core->new_child = NULL;
1245 if (new_parent && new_parent != core->parent)
1246 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001247
Stephen Boydd6968fc2015-04-30 13:54:13 -07001248 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001249 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001250 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001251 }
1252}
1253
1254/*
1255 * calculate the new rates returning the topmost clock that has to be
1256 * changed.
1257 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001258static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001259 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001260{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001261 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001262 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001263 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001264 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001265 unsigned long min_rate;
1266 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001267 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001268 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001269
Mike Turquette7452b212012-03-26 14:45:36 -07001270 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001271 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001272 return NULL;
1273
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001274 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001275 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001276 if (parent)
1277 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001278
Stephen Boydd6968fc2015-04-30 13:54:13 -07001279 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001280
James Hogan71472c02013-07-29 12:25:00 +01001281 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001282 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001283 struct clk_rate_request req;
1284
1285 req.rate = rate;
1286 req.min_rate = min_rate;
1287 req.max_rate = max_rate;
1288 if (parent) {
1289 req.best_parent_hw = parent->hw;
1290 req.best_parent_rate = parent->rate;
1291 } else {
1292 req.best_parent_hw = NULL;
1293 req.best_parent_rate = 0;
1294 }
1295
1296 ret = core->ops->determine_rate(core->hw, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001297 if (ret < 0)
1298 return NULL;
1299
Boris Brezillon0817b622015-07-07 20:48:08 +02001300 best_parent_rate = req.best_parent_rate;
1301 new_rate = req.rate;
1302 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001303 } else if (core->ops->round_rate) {
1304 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon0817b622015-07-07 20:48:08 +02001305 &best_parent_rate);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001306 if (ret < 0)
1307 return NULL;
1308
1309 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001310 if (new_rate < min_rate || new_rate > max_rate)
1311 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001312 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001313 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001314 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001315 return NULL;
1316 } else {
1317 /* pass-through clock with adjustable parent */
1318 top = clk_calc_new_rates(parent, rate);
1319 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001320 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001321 }
1322
James Hogan71472c02013-07-29 12:25:00 +01001323 /* some clocks must be gated to change parent */
1324 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001325 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001326 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001327 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001328 return NULL;
1329 }
1330
James Hogan71472c02013-07-29 12:25:00 +01001331 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001332 if (parent && core->num_parents > 1) {
1333 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001334 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001335 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001336 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001337 return NULL;
1338 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001339 }
1340
Stephen Boydd6968fc2015-04-30 13:54:13 -07001341 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001342 best_parent_rate != parent->rate)
1343 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001344
1345out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001346 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001347
1348 return top;
1349}
1350
1351/*
1352 * Notify about rate changes in a subtree. Always walk down the whole tree
1353 * so that in case of an error we can walk down the whole tree again and
1354 * abort the change.
1355 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001356static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001357 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001358{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001359 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001360 int ret = NOTIFY_DONE;
1361
Stephen Boydd6968fc2015-04-30 13:54:13 -07001362 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301363 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001364
Stephen Boydd6968fc2015-04-30 13:54:13 -07001365 if (core->notifier_count) {
1366 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001367 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001368 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001369 }
1370
Stephen Boydd6968fc2015-04-30 13:54:13 -07001371 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001372 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001373 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001374 continue;
1375 tmp_clk = clk_propagate_rate_change(child, event);
1376 if (tmp_clk)
1377 fail_clk = tmp_clk;
1378 }
1379
Stephen Boydd6968fc2015-04-30 13:54:13 -07001380 /* handle the new child who might not be in core->children yet */
1381 if (core->new_child) {
1382 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001383 if (tmp_clk)
1384 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001385 }
1386
1387 return fail_clk;
1388}
1389
1390/*
1391 * walk down a subtree and set the new rates notifying the rate
1392 * change on the way
1393 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001394static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001395{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001396 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001397 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001398 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001399 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001400 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001401 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001402
Stephen Boydd6968fc2015-04-30 13:54:13 -07001403 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001404
Stephen Boydd6968fc2015-04-30 13:54:13 -07001405 if (core->new_parent)
1406 best_parent_rate = core->new_parent->rate;
1407 else if (core->parent)
1408 best_parent_rate = core->parent->rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001409
Stephen Boydd6968fc2015-04-30 13:54:13 -07001410 if (core->new_parent && core->new_parent != core->parent) {
1411 old_parent = __clk_set_parent_before(core, core->new_parent);
1412 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001413
Stephen Boydd6968fc2015-04-30 13:54:13 -07001414 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001415 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001416 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001417 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001418 core->new_parent_index);
1419 } else if (core->ops->set_parent) {
1420 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001421 }
1422
Stephen Boydd6968fc2015-04-30 13:54:13 -07001423 trace_clk_set_parent_complete(core, core->new_parent);
1424 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001425 }
1426
Stephen Boydd6968fc2015-04-30 13:54:13 -07001427 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001428
Stephen Boydd6968fc2015-04-30 13:54:13 -07001429 if (!skip_set_rate && core->ops->set_rate)
1430 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001431
Stephen Boydd6968fc2015-04-30 13:54:13 -07001432 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001433
Stephen Boydd6968fc2015-04-30 13:54:13 -07001434 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001435
Stephen Boydd6968fc2015-04-30 13:54:13 -07001436 if (core->notifier_count && old_rate != core->rate)
1437 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001438
Michael Turquette85e88fa2015-06-20 12:18:03 -07001439 if (core->flags & CLK_RECALC_NEW_RATES)
1440 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001441
Tero Kristo067bb172014-08-21 16:47:45 +03001442 /*
1443 * Use safe iteration, as change_rate can actually swap parents
1444 * for certain clock types.
1445 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001446 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001447 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001448 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001449 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001450 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001451 }
1452
Stephen Boydd6968fc2015-04-30 13:54:13 -07001453 /* handle the new child who might not be in core->children yet */
1454 if (core->new_child)
1455 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001456}
1457
Stephen Boydd6968fc2015-04-30 13:54:13 -07001458static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001459 unsigned long req_rate)
1460{
1461 struct clk_core *top, *fail_clk;
1462 unsigned long rate = req_rate;
1463 int ret = 0;
1464
Stephen Boydd6968fc2015-04-30 13:54:13 -07001465 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001466 return 0;
1467
1468 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001469 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001470 return 0;
1471
Stephen Boydd6968fc2015-04-30 13:54:13 -07001472 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001473 return -EBUSY;
1474
1475 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001476 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001477 if (!top)
1478 return -EINVAL;
1479
1480 /* notify that we are about to change rates */
1481 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1482 if (fail_clk) {
1483 pr_debug("%s: failed to set %s rate\n", __func__,
1484 fail_clk->name);
1485 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1486 return -EBUSY;
1487 }
1488
1489 /* change the rates */
1490 clk_change_rate(top);
1491
Stephen Boydd6968fc2015-04-30 13:54:13 -07001492 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001493
1494 return ret;
1495}
1496
Mike Turquetteb24764902012-03-15 23:11:19 -07001497/**
1498 * clk_set_rate - specify a new rate for clk
1499 * @clk: the clk whose rate is being changed
1500 * @rate: the new rate for clk
1501 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001502 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001503 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001504 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1505 * propagate up to clk's parent; whether or not this happens depends on the
1506 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1507 * after calling .round_rate then upstream parent propagation is ignored. If
1508 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001509 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001510 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1511 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001512 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001513 * Rate changes are accomplished via tree traversal that also recalculates the
1514 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001515 *
1516 * Returns 0 on success, -EERROR otherwise.
1517 */
1518int clk_set_rate(struct clk *clk, unsigned long rate)
1519{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001520 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001521
Mike Turquette89ac8d72013-08-21 23:58:09 -07001522 if (!clk)
1523 return 0;
1524
Mike Turquetteb24764902012-03-15 23:11:19 -07001525 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001526 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001527
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001528 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001529
Mike Turquetteeab89f62013-03-28 13:59:01 -07001530 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001531
1532 return ret;
1533}
1534EXPORT_SYMBOL_GPL(clk_set_rate);
1535
1536/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001537 * clk_set_rate_range - set a rate range for a clock source
1538 * @clk: clock source
1539 * @min: desired minimum clock rate in Hz, inclusive
1540 * @max: desired maximum clock rate in Hz, inclusive
1541 *
1542 * Returns success (0) or negative errno.
1543 */
1544int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1545{
1546 int ret = 0;
1547
1548 if (!clk)
1549 return 0;
1550
1551 if (min > max) {
1552 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1553 __func__, clk->core->name, clk->dev_id, clk->con_id,
1554 min, max);
1555 return -EINVAL;
1556 }
1557
1558 clk_prepare_lock();
1559
1560 if (min != clk->min_rate || max != clk->max_rate) {
1561 clk->min_rate = min;
1562 clk->max_rate = max;
1563 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1564 }
1565
1566 clk_prepare_unlock();
1567
1568 return ret;
1569}
1570EXPORT_SYMBOL_GPL(clk_set_rate_range);
1571
1572/**
1573 * clk_set_min_rate - set a minimum clock rate for a clock source
1574 * @clk: clock source
1575 * @rate: desired minimum clock rate in Hz, inclusive
1576 *
1577 * Returns success (0) or negative errno.
1578 */
1579int clk_set_min_rate(struct clk *clk, unsigned long rate)
1580{
1581 if (!clk)
1582 return 0;
1583
1584 return clk_set_rate_range(clk, rate, clk->max_rate);
1585}
1586EXPORT_SYMBOL_GPL(clk_set_min_rate);
1587
1588/**
1589 * clk_set_max_rate - set a maximum clock rate for a clock source
1590 * @clk: clock source
1591 * @rate: desired maximum clock rate in Hz, inclusive
1592 *
1593 * Returns success (0) or negative errno.
1594 */
1595int clk_set_max_rate(struct clk *clk, unsigned long rate)
1596{
1597 if (!clk)
1598 return 0;
1599
1600 return clk_set_rate_range(clk, clk->min_rate, rate);
1601}
1602EXPORT_SYMBOL_GPL(clk_set_max_rate);
1603
1604/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001605 * clk_get_parent - return the parent of a clk
1606 * @clk: the clk whose parent gets returned
1607 *
1608 * Simply returns clk->parent. Returns NULL if clk is NULL.
1609 */
1610struct clk *clk_get_parent(struct clk *clk)
1611{
1612 struct clk *parent;
1613
Mike Turquetteeab89f62013-03-28 13:59:01 -07001614 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001615 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001616 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001617
1618 return parent;
1619}
1620EXPORT_SYMBOL_GPL(clk_get_parent);
1621
1622/*
1623 * .get_parent is mandatory for clocks with multiple possible parents. It is
1624 * optional for single-parent clocks. Always call .get_parent if it is
1625 * available and WARN if it is missing for multi-parent clocks.
1626 *
1627 * For single-parent clocks without .get_parent, first check to see if the
1628 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001629 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001630 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001631static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001632{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001633 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001634 u8 index;
1635
1636 /* handle the trivial cases */
1637
Stephen Boydd6968fc2015-04-30 13:54:13 -07001638 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -07001639 goto out;
1640
Stephen Boydd6968fc2015-04-30 13:54:13 -07001641 if (core->num_parents == 1) {
1642 if (IS_ERR_OR_NULL(core->parent))
1643 core->parent = clk_core_lookup(core->parent_names[0]);
1644 ret = core->parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001645 goto out;
1646 }
1647
Stephen Boydd6968fc2015-04-30 13:54:13 -07001648 if (!core->ops->get_parent) {
1649 WARN(!core->ops->get_parent,
Mike Turquetteb24764902012-03-15 23:11:19 -07001650 "%s: multi-parent clocks must implement .get_parent\n",
1651 __func__);
1652 goto out;
1653 };
1654
1655 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07001656 * Do our best to cache parent clocks in core->parents. This prevents
1657 * unnecessary and expensive lookups. We don't set core->parent here;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001658 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001659 */
1660
Stephen Boydd6968fc2015-04-30 13:54:13 -07001661 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001662
Stephen Boydd6968fc2015-04-30 13:54:13 -07001663 if (!core->parents)
1664 core->parents =
1665 kcalloc(core->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001666 GFP_KERNEL);
1667
Stephen Boydd6968fc2015-04-30 13:54:13 -07001668 ret = clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001669
1670out:
1671 return ret;
1672}
1673
Stephen Boydd6968fc2015-04-30 13:54:13 -07001674static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001675 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001676{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001677 clk_reparent(core, new_parent);
1678 __clk_recalc_accuracies(core);
1679 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07001680}
1681
Tomeu Vizoso42c86542015-03-11 11:34:25 +01001682void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
1683{
1684 if (!hw)
1685 return;
1686
1687 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
1688}
1689
Mike Turquetteb24764902012-03-15 23:11:19 -07001690/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001691 * clk_has_parent - check if a clock is a possible parent for another
1692 * @clk: clock source
1693 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07001694 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001695 * This function can be used in drivers that need to check that a clock can be
1696 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001697 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001698 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001699 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001700bool clk_has_parent(struct clk *clk, struct clk *parent)
1701{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001702 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001703 unsigned int i;
1704
1705 /* NULL clocks should be nops, so return success if either is NULL. */
1706 if (!clk || !parent)
1707 return true;
1708
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001709 core = clk->core;
1710 parent_core = parent->core;
1711
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001712 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001713 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001714 return true;
1715
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001716 for (i = 0; i < core->num_parents; i++)
1717 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001718 return true;
1719
1720 return false;
1721}
1722EXPORT_SYMBOL_GPL(clk_has_parent);
1723
Stephen Boydd6968fc2015-04-30 13:54:13 -07001724static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001725{
1726 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001727 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001728 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001729
Stephen Boydd6968fc2015-04-30 13:54:13 -07001730 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07001731 return 0;
1732
Mike Turquetteb24764902012-03-15 23:11:19 -07001733 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001734 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001735
Stephen Boydd6968fc2015-04-30 13:54:13 -07001736 if (core->parent == parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001737 goto out;
1738
Stephen Boydb61c43c2015-02-02 14:11:25 -08001739 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001740 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08001741 ret = -ENOSYS;
1742 goto out;
1743 }
1744
Ulf Hansson031dcc92013-04-02 23:09:38 +02001745 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001746 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001747 ret = -EBUSY;
1748 goto out;
1749 }
1750
1751 /* try finding the new parent index */
1752 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001753 p_index = clk_fetch_parent_index(core, parent);
Ulf Hansson031dcc92013-04-02 23:09:38 +02001754 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001755 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001756 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001757 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001758 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001759 goto out;
1760 }
1761 }
1762
Mike Turquetteb24764902012-03-15 23:11:19 -07001763 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001764 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001765
1766 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001767 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001768 goto out;
1769
Ulf Hansson031dcc92013-04-02 23:09:38 +02001770 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001771 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001772
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001773 /* propagate rate an accuracy recalculation accordingly */
1774 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001775 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001776 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001777 __clk_recalc_rates(core, POST_RATE_CHANGE);
1778 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001779 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001780
1781out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001782 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001783
1784 return ret;
1785}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001786
1787/**
1788 * clk_set_parent - switch the parent of a mux clk
1789 * @clk: the mux clk whose input we are switching
1790 * @parent: the new input to clk
1791 *
1792 * Re-parent clk to use parent as its new input source. If clk is in
1793 * prepared state, the clk will get enabled for the duration of this call. If
1794 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1795 * that, the reparenting is glitchy in hardware, etc), use the
1796 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1797 *
1798 * After successfully changing clk's parent clk_set_parent will update the
1799 * clk topology, sysfs topology and propagate rate recalculation via
1800 * __clk_recalc_rates.
1801 *
1802 * Returns 0 on success, -EERROR otherwise.
1803 */
1804int clk_set_parent(struct clk *clk, struct clk *parent)
1805{
1806 if (!clk)
1807 return 0;
1808
1809 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1810}
Mike Turquetteb24764902012-03-15 23:11:19 -07001811EXPORT_SYMBOL_GPL(clk_set_parent);
1812
1813/**
Mike Turquettee59c5372014-02-18 21:21:25 -08001814 * clk_set_phase - adjust the phase shift of a clock signal
1815 * @clk: clock signal source
1816 * @degrees: number of degrees the signal is shifted
1817 *
1818 * Shifts the phase of a clock signal by the specified
1819 * degrees. Returns 0 on success, -EERROR otherwise.
1820 *
1821 * This function makes no distinction about the input or reference
1822 * signal that we adjust the clock signal phase against. For example
1823 * phase locked-loop clock signal generators we may shift phase with
1824 * respect to feedback clock signal input, but for other cases the
1825 * clock phase may be shifted with respect to some other, unspecified
1826 * signal.
1827 *
1828 * Additionally the concept of phase shift does not propagate through
1829 * the clock tree hierarchy, which sets it apart from clock rates and
1830 * clock accuracy. A parent clock phase attribute does not have an
1831 * impact on the phase attribute of a child clock.
1832 */
1833int clk_set_phase(struct clk *clk, int degrees)
1834{
Stephen Boyd08b95752015-02-02 14:09:43 -08001835 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08001836
1837 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08001838 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08001839
1840 /* sanity check degrees */
1841 degrees %= 360;
1842 if (degrees < 0)
1843 degrees += 360;
1844
1845 clk_prepare_lock();
1846
Stephen Boyddfc202e2015-02-02 14:37:41 -08001847 trace_clk_set_phase(clk->core, degrees);
1848
Stephen Boyd08b95752015-02-02 14:09:43 -08001849 if (clk->core->ops->set_phase)
1850 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08001851
Stephen Boyddfc202e2015-02-02 14:37:41 -08001852 trace_clk_set_phase_complete(clk->core, degrees);
1853
Mike Turquettee59c5372014-02-18 21:21:25 -08001854 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001855 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08001856
Mike Turquettee59c5372014-02-18 21:21:25 -08001857 clk_prepare_unlock();
1858
Mike Turquettee59c5372014-02-18 21:21:25 -08001859 return ret;
1860}
Maxime Ripard9767b042015-01-20 22:23:43 +01001861EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001862
Stephen Boydd6968fc2015-04-30 13:54:13 -07001863static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08001864{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07001865 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08001866
1867 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001868 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08001869 clk_prepare_unlock();
1870
Mike Turquettee59c5372014-02-18 21:21:25 -08001871 return ret;
1872}
1873
1874/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001875 * clk_get_phase - return the phase shift of a clock signal
1876 * @clk: clock signal source
1877 *
1878 * Returns the phase shift of a clock node in degrees, otherwise returns
1879 * -EERROR.
1880 */
1881int clk_get_phase(struct clk *clk)
1882{
1883 if (!clk)
1884 return 0;
1885
1886 return clk_core_get_phase(clk->core);
1887}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001888EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07001889
1890/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08001891 * clk_is_match - check if two clk's point to the same hardware clock
1892 * @p: clk compared against q
1893 * @q: clk compared against p
1894 *
1895 * Returns true if the two struct clk pointers both point to the same hardware
1896 * clock node. Put differently, returns true if struct clk *p and struct clk *q
1897 * share the same struct clk_core object.
1898 *
1899 * Returns false otherwise. Note that two NULL clks are treated as matching.
1900 */
1901bool clk_is_match(const struct clk *p, const struct clk *q)
1902{
1903 /* trivial case: identical struct clk's or both NULL */
1904 if (p == q)
1905 return true;
1906
1907 /* true if clk->core pointers match. Avoid derefing garbage */
1908 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
1909 if (p->core == q->core)
1910 return true;
1911
1912 return false;
1913}
1914EXPORT_SYMBOL_GPL(clk_is_match);
1915
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001916/*** debugfs support ***/
1917
1918#ifdef CONFIG_DEBUG_FS
1919#include <linux/debugfs.h>
1920
1921static struct dentry *rootdir;
1922static int inited = 0;
1923static DEFINE_MUTEX(clk_debug_lock);
1924static HLIST_HEAD(clk_debug_list);
1925
1926static struct hlist_head *all_lists[] = {
1927 &clk_root_list,
1928 &clk_orphan_list,
1929 NULL,
1930};
1931
1932static struct hlist_head *orphan_list[] = {
1933 &clk_orphan_list,
1934 NULL,
1935};
1936
1937static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
1938 int level)
1939{
1940 if (!c)
1941 return;
1942
1943 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
1944 level * 3 + 1, "",
1945 30 - level * 3, c->name,
1946 c->enable_count, c->prepare_count, clk_core_get_rate(c),
1947 clk_core_get_accuracy(c), clk_core_get_phase(c));
1948}
1949
1950static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
1951 int level)
1952{
1953 struct clk_core *child;
1954
1955 if (!c)
1956 return;
1957
1958 clk_summary_show_one(s, c, level);
1959
1960 hlist_for_each_entry(child, &c->children, child_node)
1961 clk_summary_show_subtree(s, child, level + 1);
1962}
1963
1964static int clk_summary_show(struct seq_file *s, void *data)
1965{
1966 struct clk_core *c;
1967 struct hlist_head **lists = (struct hlist_head **)s->private;
1968
1969 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
1970 seq_puts(s, "----------------------------------------------------------------------------------------\n");
1971
1972 clk_prepare_lock();
1973
1974 for (; *lists; lists++)
1975 hlist_for_each_entry(c, *lists, child_node)
1976 clk_summary_show_subtree(s, c, 0);
1977
1978 clk_prepare_unlock();
1979
1980 return 0;
1981}
1982
1983
1984static int clk_summary_open(struct inode *inode, struct file *file)
1985{
1986 return single_open(file, clk_summary_show, inode->i_private);
1987}
1988
1989static const struct file_operations clk_summary_fops = {
1990 .open = clk_summary_open,
1991 .read = seq_read,
1992 .llseek = seq_lseek,
1993 .release = single_release,
1994};
1995
1996static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
1997{
1998 if (!c)
1999 return;
2000
Stefan Wahren7cb81132015-04-29 16:36:43 +00002001 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002002 seq_printf(s, "\"%s\": { ", c->name);
2003 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2004 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002005 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2006 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002007 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2008}
2009
2010static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2011{
2012 struct clk_core *child;
2013
2014 if (!c)
2015 return;
2016
2017 clk_dump_one(s, c, level);
2018
2019 hlist_for_each_entry(child, &c->children, child_node) {
2020 seq_printf(s, ",");
2021 clk_dump_subtree(s, child, level + 1);
2022 }
2023
2024 seq_printf(s, "}");
2025}
2026
2027static int clk_dump(struct seq_file *s, void *data)
2028{
2029 struct clk_core *c;
2030 bool first_node = true;
2031 struct hlist_head **lists = (struct hlist_head **)s->private;
2032
2033 seq_printf(s, "{");
2034
2035 clk_prepare_lock();
2036
2037 for (; *lists; lists++) {
2038 hlist_for_each_entry(c, *lists, child_node) {
2039 if (!first_node)
2040 seq_puts(s, ",");
2041 first_node = false;
2042 clk_dump_subtree(s, c, 0);
2043 }
2044 }
2045
2046 clk_prepare_unlock();
2047
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002048 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002049 return 0;
2050}
2051
2052
2053static int clk_dump_open(struct inode *inode, struct file *file)
2054{
2055 return single_open(file, clk_dump, inode->i_private);
2056}
2057
2058static const struct file_operations clk_dump_fops = {
2059 .open = clk_dump_open,
2060 .read = seq_read,
2061 .llseek = seq_lseek,
2062 .release = single_release,
2063};
2064
2065static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2066{
2067 struct dentry *d;
2068 int ret = -ENOMEM;
2069
2070 if (!core || !pdentry) {
2071 ret = -EINVAL;
2072 goto out;
2073 }
2074
2075 d = debugfs_create_dir(core->name, pdentry);
2076 if (!d)
2077 goto out;
2078
2079 core->dentry = d;
2080
2081 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2082 (u32 *)&core->rate);
2083 if (!d)
2084 goto err_out;
2085
2086 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2087 (u32 *)&core->accuracy);
2088 if (!d)
2089 goto err_out;
2090
2091 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2092 (u32 *)&core->phase);
2093 if (!d)
2094 goto err_out;
2095
2096 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2097 (u32 *)&core->flags);
2098 if (!d)
2099 goto err_out;
2100
2101 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2102 (u32 *)&core->prepare_count);
2103 if (!d)
2104 goto err_out;
2105
2106 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2107 (u32 *)&core->enable_count);
2108 if (!d)
2109 goto err_out;
2110
2111 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2112 (u32 *)&core->notifier_count);
2113 if (!d)
2114 goto err_out;
2115
2116 if (core->ops->debug_init) {
2117 ret = core->ops->debug_init(core->hw, core->dentry);
2118 if (ret)
2119 goto err_out;
2120 }
2121
2122 ret = 0;
2123 goto out;
2124
2125err_out:
2126 debugfs_remove_recursive(core->dentry);
2127 core->dentry = NULL;
2128out:
2129 return ret;
2130}
2131
2132/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002133 * clk_debug_register - add a clk node to the debugfs clk directory
2134 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002135 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002136 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2137 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002138 * will be created lazily by clk_debug_init as part of a late_initcall.
2139 */
2140static int clk_debug_register(struct clk_core *core)
2141{
2142 int ret = 0;
2143
2144 mutex_lock(&clk_debug_lock);
2145 hlist_add_head(&core->debug_node, &clk_debug_list);
2146
2147 if (!inited)
2148 goto unlock;
2149
2150 ret = clk_debug_create_one(core, rootdir);
2151unlock:
2152 mutex_unlock(&clk_debug_lock);
2153
2154 return ret;
2155}
2156
2157 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002158 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2159 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002160 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002161 * Dynamically removes a clk and all its child nodes from the
2162 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002163 * clk_debug_register in __clk_init.
2164 */
2165static void clk_debug_unregister(struct clk_core *core)
2166{
2167 mutex_lock(&clk_debug_lock);
2168 hlist_del_init(&core->debug_node);
2169 debugfs_remove_recursive(core->dentry);
2170 core->dentry = NULL;
2171 mutex_unlock(&clk_debug_lock);
2172}
2173
2174struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2175 void *data, const struct file_operations *fops)
2176{
2177 struct dentry *d = NULL;
2178
2179 if (hw->core->dentry)
2180 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2181 fops);
2182
2183 return d;
2184}
2185EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2186
2187/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002188 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002189 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002190 * clks are often initialized very early during boot before memory can be
2191 * dynamically allocated and well before debugfs is setup. This function
2192 * populates the debugfs clk directory once at boot-time when we know that
2193 * debugfs is setup. It should only be called once at boot-time, all other clks
2194 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002195 */
2196static int __init clk_debug_init(void)
2197{
2198 struct clk_core *core;
2199 struct dentry *d;
2200
2201 rootdir = debugfs_create_dir("clk", NULL);
2202
2203 if (!rootdir)
2204 return -ENOMEM;
2205
2206 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2207 &clk_summary_fops);
2208 if (!d)
2209 return -ENOMEM;
2210
2211 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2212 &clk_dump_fops);
2213 if (!d)
2214 return -ENOMEM;
2215
2216 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2217 &orphan_list, &clk_summary_fops);
2218 if (!d)
2219 return -ENOMEM;
2220
2221 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2222 &orphan_list, &clk_dump_fops);
2223 if (!d)
2224 return -ENOMEM;
2225
2226 mutex_lock(&clk_debug_lock);
2227 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2228 clk_debug_create_one(core, rootdir);
2229
2230 inited = 1;
2231 mutex_unlock(&clk_debug_lock);
2232
2233 return 0;
2234}
2235late_initcall(clk_debug_init);
2236#else
2237static inline int clk_debug_register(struct clk_core *core) { return 0; }
2238static inline void clk_debug_reparent(struct clk_core *core,
2239 struct clk_core *new_parent)
2240{
2241}
2242static inline void clk_debug_unregister(struct clk_core *core)
2243{
2244}
2245#endif
2246
Michael Turquette3d3801e2015-02-25 09:11:01 -08002247/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002248 * __clk_init - initialize the data structures in a struct clk
2249 * @dev: device initializing this clk, placeholder for now
2250 * @clk: clk being initialized
2251 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002252 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002253 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002254 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002255static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002256{
Mike Turquetted1302a32012-03-29 14:30:40 -07002257 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002258 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002259 struct hlist_node *tmp2;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002260 struct clk_core *core;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002261 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002262
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002263 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002264 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002265
Stephen Boydd6968fc2015-04-30 13:54:13 -07002266 core = clk_user->core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002267
Mike Turquetteeab89f62013-03-28 13:59:01 -07002268 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002269
2270 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002271 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002272 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002273 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002274 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002275 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002276 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002277
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002278 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002279 if (core->ops->set_rate &&
2280 !((core->ops->round_rate || core->ops->determine_rate) &&
2281 core->ops->recalc_rate)) {
James Hogan71472c02013-07-29 12:25:00 +01002282 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002283 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002284 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002285 goto out;
2286 }
2287
Stephen Boydd6968fc2015-04-30 13:54:13 -07002288 if (core->ops->set_parent && !core->ops->get_parent) {
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002289 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002290 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002291 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002292 goto out;
2293 }
2294
Stephen Boydd6968fc2015-04-30 13:54:13 -07002295 if (core->ops->set_rate_and_parent &&
2296 !(core->ops->set_parent && core->ops->set_rate)) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002297 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002298 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002299 ret = -EINVAL;
2300 goto out;
2301 }
2302
Mike Turquetteb24764902012-03-15 23:11:19 -07002303 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002304 for (i = 0; i < core->num_parents; i++)
2305 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002306 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002307 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002308
2309 /*
2310 * Allocate an array of struct clk *'s to avoid unnecessary string
2311 * look-ups of clk's possible parents. This can fail for clocks passed
Stephen Boydd6968fc2015-04-30 13:54:13 -07002312 * in to clk_init during early boot; thus any access to core->parents[]
Mike Turquetteb24764902012-03-15 23:11:19 -07002313 * must always check for a NULL pointer and try to populate it if
2314 * necessary.
2315 *
Stephen Boydd6968fc2015-04-30 13:54:13 -07002316 * If core->parents is not NULL we skip this entire block. This allows
2317 * for clock drivers to statically initialize core->parents.
Mike Turquetteb24764902012-03-15 23:11:19 -07002318 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002319 if (core->num_parents > 1 && !core->parents) {
2320 core->parents = kcalloc(core->num_parents, sizeof(struct clk *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002321 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002322 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002323 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002324 * clk_init'd; thus any access to clk->parents[] must check
2325 * for a NULL pointer. We can always perform lazy lookups for
2326 * missing parents later on.
2327 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002328 if (core->parents)
2329 for (i = 0; i < core->num_parents; i++)
2330 core->parents[i] =
2331 clk_core_lookup(core->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002332 }
2333
Stephen Boydd6968fc2015-04-30 13:54:13 -07002334 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002335
2336 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07002337 * Populate core->parent if parent has already been __clk_init'd. If
Mike Turquetteb24764902012-03-15 23:11:19 -07002338 * parent has not yet been __clk_init'd then place clk in the orphan
2339 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2340 * clk list.
2341 *
2342 * Every time a new clk is clk_init'd then we walk the list of orphan
2343 * clocks and re-parent any that are children of the clock currently
2344 * being clk_init'd.
2345 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002346 if (core->parent)
2347 hlist_add_head(&core->child_node,
2348 &core->parent->children);
2349 else if (core->flags & CLK_IS_ROOT)
2350 hlist_add_head(&core->child_node, &clk_root_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002351 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002352 hlist_add_head(&core->child_node, &clk_orphan_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002353
2354 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002355 * Set clk's accuracy. The preferred method is to use
2356 * .recalc_accuracy. For simple clocks and lazy developers the default
2357 * fallback is to use the parent's accuracy. If a clock doesn't have a
2358 * parent (or is orphaned) then accuracy is set to zero (perfect
2359 * clock).
2360 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002361 if (core->ops->recalc_accuracy)
2362 core->accuracy = core->ops->recalc_accuracy(core->hw,
2363 __clk_get_accuracy(core->parent));
2364 else if (core->parent)
2365 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002366 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002367 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002368
2369 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002370 * Set clk's phase.
2371 * Since a phase is by definition relative to its parent, just
2372 * query the current clock phase, or just assume it's in phase.
2373 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002374 if (core->ops->get_phase)
2375 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002376 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002377 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002378
2379 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002380 * Set clk's rate. The preferred method is to use .recalc_rate. For
2381 * simple clocks and lazy developers the default fallback is to use the
2382 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2383 * then rate is set to zero.
2384 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002385 if (core->ops->recalc_rate)
2386 rate = core->ops->recalc_rate(core->hw,
2387 clk_core_get_rate_nolock(core->parent));
2388 else if (core->parent)
2389 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002390 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002391 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002392 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002393
2394 /*
2395 * walk the list of orphan clocks and reparent any that are children of
2396 * this clock
2397 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002398 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002399 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002400 i = orphan->ops->get_parent(orphan->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002401 if (!strcmp(core->name, orphan->parent_names[i]))
2402 clk_core_reparent(orphan, core);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002403 continue;
2404 }
2405
Mike Turquetteb24764902012-03-15 23:11:19 -07002406 for (i = 0; i < orphan->num_parents; i++)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002407 if (!strcmp(core->name, orphan->parent_names[i])) {
2408 clk_core_reparent(orphan, core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002409 break;
2410 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002411 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002412
2413 /*
2414 * optional platform-specific magic
2415 *
2416 * The .init callback is not used by any of the basic clock types, but
2417 * exists for weird hardware that must perform initialization magic.
2418 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002419 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002420 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002421 if (core->ops->init)
2422 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002423
Stephen Boydd6968fc2015-04-30 13:54:13 -07002424 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002425out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002426 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002427
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002428 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002429 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002430
Mike Turquetted1302a32012-03-29 14:30:40 -07002431 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002432}
2433
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002434struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2435 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002436{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002437 struct clk *clk;
2438
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002439 /* This is to allow this function to be chained to others */
2440 if (!hw || IS_ERR(hw))
2441 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002442
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002443 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2444 if (!clk)
2445 return ERR_PTR(-ENOMEM);
2446
2447 clk->core = hw->core;
2448 clk->dev_id = dev_id;
2449 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002450 clk->max_rate = ULONG_MAX;
2451
2452 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002453 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002454 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002455
2456 return clk;
2457}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002458
Stephen Boyd73e0e492015-02-06 11:42:43 -08002459void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002460{
2461 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002462 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002463 clk_prepare_unlock();
2464
2465 kfree(clk);
2466}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002467
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002468/**
2469 * clk_register - allocate a new clock, register it and return an opaque cookie
2470 * @dev: device that is registering this clock
2471 * @hw: link to hardware-specific clock data
2472 *
2473 * clk_register is the primary interface for populating the clock tree with new
2474 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05302475 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002476 * rest of the clock API. In the event of an error clk_register will return an
2477 * error code; drivers must test for an error code after calling clk_register.
2478 */
2479struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002480{
Mike Turquetted1302a32012-03-29 14:30:40 -07002481 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002482 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002483
Stephen Boydd6968fc2015-04-30 13:54:13 -07002484 core = kzalloc(sizeof(*core), GFP_KERNEL);
2485 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002486 ret = -ENOMEM;
2487 goto fail_out;
2488 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002489
Stephen Boydd6968fc2015-04-30 13:54:13 -07002490 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2491 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002492 ret = -ENOMEM;
2493 goto fail_name;
2494 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002495 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002496 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002497 core->owner = dev->driver->owner;
2498 core->hw = hw;
2499 core->flags = hw->init->flags;
2500 core->num_parents = hw->init->num_parents;
2501 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002502
Mike Turquetted1302a32012-03-29 14:30:40 -07002503 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002504 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002505 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002506
Stephen Boydd6968fc2015-04-30 13:54:13 -07002507 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002508 ret = -ENOMEM;
2509 goto fail_parent_names;
2510 }
2511
2512
2513 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002514 for (i = 0; i < core->num_parents; i++) {
2515 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002516 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002517 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002518 ret = -ENOMEM;
2519 goto fail_parent_names_copy;
2520 }
2521 }
2522
Stephen Boydd6968fc2015-04-30 13:54:13 -07002523 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002524
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002525 hw->clk = __clk_create_clk(hw, NULL, NULL);
2526 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002527 ret = PTR_ERR(hw->clk);
2528 goto fail_parent_names_copy;
2529 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002530
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002531 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002532 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002533 return hw->clk;
2534
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002535 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002536 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002537
2538fail_parent_names_copy:
2539 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002540 kfree_const(core->parent_names[i]);
2541 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07002542fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002543 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002544fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002545 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002546fail_out:
2547 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002548}
2549EXPORT_SYMBOL_GPL(clk_register);
2550
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002551/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002552static void __clk_release(struct kref *ref)
2553{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002554 struct clk_core *core = container_of(ref, struct clk_core, ref);
2555 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002556
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002557 lockdep_assert_held(&prepare_lock);
2558
Stephen Boydd6968fc2015-04-30 13:54:13 -07002559 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002560 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002561 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002562
Stephen Boydd6968fc2015-04-30 13:54:13 -07002563 kfree(core->parent_names);
2564 kfree_const(core->name);
2565 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002566}
2567
2568/*
2569 * Empty clk_ops for unregistered clocks. These are used temporarily
2570 * after clk_unregister() was called on a clock and until last clock
2571 * consumer calls clk_put() and the struct clk object is freed.
2572 */
2573static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2574{
2575 return -ENXIO;
2576}
2577
2578static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2579{
2580 WARN_ON_ONCE(1);
2581}
2582
2583static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2584 unsigned long parent_rate)
2585{
2586 return -ENXIO;
2587}
2588
2589static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2590{
2591 return -ENXIO;
2592}
2593
2594static const struct clk_ops clk_nodrv_ops = {
2595 .enable = clk_nodrv_prepare_enable,
2596 .disable = clk_nodrv_disable_unprepare,
2597 .prepare = clk_nodrv_prepare_enable,
2598 .unprepare = clk_nodrv_disable_unprepare,
2599 .set_rate = clk_nodrv_set_rate,
2600 .set_parent = clk_nodrv_set_parent,
2601};
2602
Mark Brown1df5c932012-04-18 09:07:12 +01002603/**
2604 * clk_unregister - unregister a currently registered clock
2605 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002606 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002607void clk_unregister(struct clk *clk)
2608{
2609 unsigned long flags;
2610
Stephen Boyd6314b672014-09-04 23:37:49 -07002611 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2612 return;
2613
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002614 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002615
2616 clk_prepare_lock();
2617
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002618 if (clk->core->ops == &clk_nodrv_ops) {
2619 pr_err("%s: unregistered clock: %s\n", __func__,
2620 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002621 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002622 }
2623 /*
2624 * Assign empty clock ops for consumers that might still hold
2625 * a reference to this clock.
2626 */
2627 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002628 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002629 clk_enable_unlock(flags);
2630
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002631 if (!hlist_empty(&clk->core->children)) {
2632 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002633 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002634
2635 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002636 hlist_for_each_entry_safe(child, t, &clk->core->children,
2637 child_node)
2638 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002639 }
2640
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002641 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002642
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002643 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002644 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002645 __func__, clk->core->name);
2646 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002647
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002648 clk_prepare_unlock();
2649}
Mark Brown1df5c932012-04-18 09:07:12 +01002650EXPORT_SYMBOL_GPL(clk_unregister);
2651
Stephen Boyd46c87732012-09-24 13:38:04 -07002652static void devm_clk_release(struct device *dev, void *res)
2653{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002654 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002655}
2656
2657/**
2658 * devm_clk_register - resource managed clk_register()
2659 * @dev: device that is registering this clock
2660 * @hw: link to hardware-specific clock data
2661 *
2662 * Managed clk_register(). Clocks returned from this function are
2663 * automatically clk_unregister()ed on driver detach. See clk_register() for
2664 * more information.
2665 */
2666struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2667{
2668 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002669 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002670
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002671 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2672 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002673 return ERR_PTR(-ENOMEM);
2674
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002675 clk = clk_register(dev, hw);
2676 if (!IS_ERR(clk)) {
2677 *clkp = clk;
2678 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002679 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002680 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002681 }
2682
2683 return clk;
2684}
2685EXPORT_SYMBOL_GPL(devm_clk_register);
2686
2687static int devm_clk_match(struct device *dev, void *res, void *data)
2688{
2689 struct clk *c = res;
2690 if (WARN_ON(!c))
2691 return 0;
2692 return c == data;
2693}
2694
2695/**
2696 * devm_clk_unregister - resource managed clk_unregister()
2697 * @clk: clock to unregister
2698 *
2699 * Deallocate a clock allocated with devm_clk_register(). Normally
2700 * this function will not need to be called and the resource management
2701 * code will ensure that the resource is freed.
2702 */
2703void devm_clk_unregister(struct device *dev, struct clk *clk)
2704{
2705 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2706}
2707EXPORT_SYMBOL_GPL(devm_clk_unregister);
2708
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002709/*
2710 * clkdev helpers
2711 */
2712int __clk_get(struct clk *clk)
2713{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002714 struct clk_core *core = !clk ? NULL : clk->core;
2715
2716 if (core) {
2717 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002718 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002719
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002720 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002721 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002722 return 1;
2723}
2724
2725void __clk_put(struct clk *clk)
2726{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002727 struct module *owner;
2728
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002729 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002730 return;
2731
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002732 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002733
Stephen Boyd50595f82015-02-06 11:42:44 -08002734 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002735 if (clk->min_rate > clk->core->req_rate ||
2736 clk->max_rate < clk->core->req_rate)
2737 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2738
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002739 owner = clk->core->owner;
2740 kref_put(&clk->core->ref, __clk_release);
2741
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002742 clk_prepare_unlock();
2743
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002744 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002745
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002746 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002747}
2748
Mike Turquetteb24764902012-03-15 23:11:19 -07002749/*** clk rate change notifiers ***/
2750
2751/**
2752 * clk_notifier_register - add a clk rate change notifier
2753 * @clk: struct clk * to watch
2754 * @nb: struct notifier_block * with callback info
2755 *
2756 * Request notification when clk's rate changes. This uses an SRCU
2757 * notifier because we want it to block and notifier unregistrations are
2758 * uncommon. The callbacks associated with the notifier must not
2759 * re-enter into the clk framework by calling any top-level clk APIs;
2760 * this will cause a nested prepare_lock mutex.
2761 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002762 * In all notification cases cases (pre, post and abort rate change) the
2763 * original clock rate is passed to the callback via struct
2764 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002765 * clk_notifier_data.new_rate.
2766 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002767 * clk_notifier_register() must be called from non-atomic context.
2768 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2769 * allocation failure; otherwise, passes along the return value of
2770 * srcu_notifier_chain_register().
2771 */
2772int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2773{
2774 struct clk_notifier *cn;
2775 int ret = -ENOMEM;
2776
2777 if (!clk || !nb)
2778 return -EINVAL;
2779
Mike Turquetteeab89f62013-03-28 13:59:01 -07002780 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002781
2782 /* search the list of notifiers for this clk */
2783 list_for_each_entry(cn, &clk_notifier_list, node)
2784 if (cn->clk == clk)
2785 break;
2786
2787 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2788 if (cn->clk != clk) {
2789 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2790 if (!cn)
2791 goto out;
2792
2793 cn->clk = clk;
2794 srcu_init_notifier_head(&cn->notifier_head);
2795
2796 list_add(&cn->node, &clk_notifier_list);
2797 }
2798
2799 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2800
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002801 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002802
2803out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002804 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002805
2806 return ret;
2807}
2808EXPORT_SYMBOL_GPL(clk_notifier_register);
2809
2810/**
2811 * clk_notifier_unregister - remove a clk rate change notifier
2812 * @clk: struct clk *
2813 * @nb: struct notifier_block * with callback info
2814 *
2815 * Request no further notification for changes to 'clk' and frees memory
2816 * allocated in clk_notifier_register.
2817 *
2818 * Returns -EINVAL if called with null arguments; otherwise, passes
2819 * along the return value of srcu_notifier_chain_unregister().
2820 */
2821int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2822{
2823 struct clk_notifier *cn = NULL;
2824 int ret = -EINVAL;
2825
2826 if (!clk || !nb)
2827 return -EINVAL;
2828
Mike Turquetteeab89f62013-03-28 13:59:01 -07002829 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002830
2831 list_for_each_entry(cn, &clk_notifier_list, node)
2832 if (cn->clk == clk)
2833 break;
2834
2835 if (cn->clk == clk) {
2836 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2837
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002838 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002839
2840 /* XXX the notifier code should handle this better */
2841 if (!cn->notifier_head.head) {
2842 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002843 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002844 kfree(cn);
2845 }
2846
2847 } else {
2848 ret = -ENOENT;
2849 }
2850
Mike Turquetteeab89f62013-03-28 13:59:01 -07002851 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002852
2853 return ret;
2854}
2855EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002856
2857#ifdef CONFIG_OF
2858/**
2859 * struct of_clk_provider - Clock provider registration structure
2860 * @link: Entry in global list of clock providers
2861 * @node: Pointer to device tree node of clock provider
2862 * @get: Get clock callback. Returns NULL or a struct clk for the
2863 * given clock specifier
2864 * @data: context pointer to be passed into @get callback
2865 */
2866struct of_clk_provider {
2867 struct list_head link;
2868
2869 struct device_node *node;
2870 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2871 void *data;
2872};
2873
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302874static const struct of_device_id __clk_of_table_sentinel
2875 __used __section(__clk_of_table_end);
2876
Grant Likely766e6a42012-04-09 14:50:06 -05002877static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002878static DEFINE_MUTEX(of_clk_mutex);
2879
Grant Likely766e6a42012-04-09 14:50:06 -05002880struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2881 void *data)
2882{
2883 return data;
2884}
2885EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2886
Shawn Guo494bfec2012-08-22 21:36:27 +08002887struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2888{
2889 struct clk_onecell_data *clk_data = data;
2890 unsigned int idx = clkspec->args[0];
2891
2892 if (idx >= clk_data->clk_num) {
2893 pr_err("%s: invalid clock index %d\n", __func__, idx);
2894 return ERR_PTR(-EINVAL);
2895 }
2896
2897 return clk_data->clks[idx];
2898}
2899EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2900
Grant Likely766e6a42012-04-09 14:50:06 -05002901/**
2902 * of_clk_add_provider() - Register a clock provider for a node
2903 * @np: Device node pointer associated with clock provider
2904 * @clk_src_get: callback for decoding clock
2905 * @data: context pointer for @clk_src_get callback.
2906 */
2907int of_clk_add_provider(struct device_node *np,
2908 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2909 void *data),
2910 void *data)
2911{
2912 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002913 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002914
2915 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2916 if (!cp)
2917 return -ENOMEM;
2918
2919 cp->node = of_node_get(np);
2920 cp->data = data;
2921 cp->get = clk_src_get;
2922
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002923 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002924 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002925 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002926 pr_debug("Added clock from %s\n", np->full_name);
2927
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002928 ret = of_clk_set_defaults(np, true);
2929 if (ret < 0)
2930 of_clk_del_provider(np);
2931
2932 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002933}
2934EXPORT_SYMBOL_GPL(of_clk_add_provider);
2935
2936/**
2937 * of_clk_del_provider() - Remove a previously registered clock provider
2938 * @np: Device node pointer associated with clock provider
2939 */
2940void of_clk_del_provider(struct device_node *np)
2941{
2942 struct of_clk_provider *cp;
2943
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002944 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002945 list_for_each_entry(cp, &of_clk_providers, link) {
2946 if (cp->node == np) {
2947 list_del(&cp->link);
2948 of_node_put(cp->node);
2949 kfree(cp);
2950 break;
2951 }
2952 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002953 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002954}
2955EXPORT_SYMBOL_GPL(of_clk_del_provider);
2956
Stephen Boyd73e0e492015-02-06 11:42:43 -08002957struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
2958 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05002959{
2960 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002961 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002962
Stephen Boyd306c3422015-02-05 15:39:11 -08002963 if (!clkspec)
2964 return ERR_PTR(-EINVAL);
2965
Grant Likely766e6a42012-04-09 14:50:06 -05002966 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08002967 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002968 list_for_each_entry(provider, &of_clk_providers, link) {
2969 if (provider->node == clkspec->np)
2970 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002971 if (!IS_ERR(clk)) {
2972 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
2973 con_id);
2974
2975 if (!IS_ERR(clk) && !__clk_get(clk)) {
2976 __clk_free_clk(clk);
2977 clk = ERR_PTR(-ENOENT);
2978 }
2979
Grant Likely766e6a42012-04-09 14:50:06 -05002980 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08002981 }
Grant Likely766e6a42012-04-09 14:50:06 -05002982 }
Stephen Boyd306c3422015-02-05 15:39:11 -08002983 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002984
2985 return clk;
2986}
2987
Stephen Boyd306c3422015-02-05 15:39:11 -08002988/**
2989 * of_clk_get_from_provider() - Lookup a clock from a clock provider
2990 * @clkspec: pointer to a clock specifier data structure
2991 *
2992 * This function looks up a struct clk from the registered list of clock
2993 * providers, an input is a clock specifier data structure as returned
2994 * from the of_parse_phandle_with_args() function call.
2995 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002996struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2997{
Stephen Boyd306c3422015-02-05 15:39:11 -08002998 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05002999}
3000
Mike Turquettef6102742013-10-07 23:12:13 -07003001int of_clk_get_parent_count(struct device_node *np)
3002{
3003 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
3004}
3005EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3006
Grant Likely766e6a42012-04-09 14:50:06 -05003007const char *of_clk_get_parent_name(struct device_node *np, int index)
3008{
3009 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003010 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003011 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003012 const __be32 *vp;
3013 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003014 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003015 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05003016
3017 if (index < 0)
3018 return NULL;
3019
3020 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3021 &clkspec);
3022 if (rc)
3023 return NULL;
3024
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003025 index = clkspec.args_count ? clkspec.args[0] : 0;
3026 count = 0;
3027
3028 /* if there is an indices property, use it to transfer the index
3029 * specified into an array offset for the clock-output-names property.
3030 */
3031 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3032 if (index == pv) {
3033 index = count;
3034 break;
3035 }
3036 count++;
3037 }
3038
Grant Likely766e6a42012-04-09 14:50:06 -05003039 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003040 index,
Grant Likely766e6a42012-04-09 14:50:06 -05003041 &clk_name) < 0)
3042 clk_name = clkspec.np->name;
3043
3044 of_node_put(clkspec.np);
3045 return clk_name;
3046}
3047EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3048
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05003049/**
3050 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3051 * number of parents
3052 * @np: Device node pointer associated with clock provider
3053 * @parents: pointer to char array that hold the parents' names
3054 * @size: size of the @parents array
3055 *
3056 * Return: number of parents for the clock node.
3057 */
3058int of_clk_parent_fill(struct device_node *np, const char **parents,
3059 unsigned int size)
3060{
3061 unsigned int i = 0;
3062
3063 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3064 i++;
3065
3066 return i;
3067}
3068EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3069
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003070struct clock_provider {
3071 of_clk_init_cb_t clk_init_cb;
3072 struct device_node *np;
3073 struct list_head node;
3074};
3075
3076static LIST_HEAD(clk_provider_list);
3077
3078/*
3079 * This function looks for a parent clock. If there is one, then it
3080 * checks that the provider for this parent clock was initialized, in
3081 * this case the parent clock will be ready.
3082 */
3083static int parent_ready(struct device_node *np)
3084{
3085 int i = 0;
3086
3087 while (true) {
3088 struct clk *clk = of_clk_get(np, i);
3089
3090 /* this parent is ready we can check the next one */
3091 if (!IS_ERR(clk)) {
3092 clk_put(clk);
3093 i++;
3094 continue;
3095 }
3096
3097 /* at least one parent is not ready, we exit now */
3098 if (PTR_ERR(clk) == -EPROBE_DEFER)
3099 return 0;
3100
3101 /*
3102 * Here we make assumption that the device tree is
3103 * written correctly. So an error means that there is
3104 * no more parent. As we didn't exit yet, then the
3105 * previous parent are ready. If there is no clock
3106 * parent, no need to wait for them, then we can
3107 * consider their absence as being ready
3108 */
3109 return 1;
3110 }
3111}
3112
Grant Likely766e6a42012-04-09 14:50:06 -05003113/**
3114 * of_clk_init() - Scan and init clock providers from the DT
3115 * @matches: array of compatible values and init functions for providers.
3116 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003117 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003118 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003119 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003120 */
3121void __init of_clk_init(const struct of_device_id *matches)
3122{
Alex Elder7f7ed582013-08-22 11:31:31 -05003123 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003124 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003125 struct clock_provider *clk_provider, *next;
3126 bool is_init_done;
3127 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05003128
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303129 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003130 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303131
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003132 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003133 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003134 struct clock_provider *parent =
3135 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
3136
3137 parent->clk_init_cb = match->data;
3138 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003139 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003140 }
3141
3142 while (!list_empty(&clk_provider_list)) {
3143 is_init_done = false;
3144 list_for_each_entry_safe(clk_provider, next,
3145 &clk_provider_list, node) {
3146 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003147
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003148 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003149 of_clk_set_defaults(clk_provider->np, true);
3150
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003151 list_del(&clk_provider->node);
3152 kfree(clk_provider);
3153 is_init_done = true;
3154 }
3155 }
3156
3157 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003158 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003159 * remaining providers during the last loop, so now we
3160 * initialize all the remaining ones unconditionally
3161 * in case the clock parent was not mandatory
3162 */
3163 if (!is_init_done)
3164 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003165 }
3166}
3167#endif