blob: 1ac237fe2fdbcb33301f244f34b91c7e09829a84 [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;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070061 unsigned long min_rate;
62 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080063 unsigned long accuracy;
64 int phase;
65 struct hlist_head children;
66 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010067 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080068 unsigned int notifier_count;
69#ifdef CONFIG_DEBUG_FS
70 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020071 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080072#endif
73 struct kref ref;
74};
75
Stephen Boyddfc202e2015-02-02 14:37:41 -080076#define CREATE_TRACE_POINTS
77#include <trace/events/clk.h>
78
Michael Turquetteb09d6d92015-01-29 14:22:50 -080079struct clk {
80 struct clk_core *core;
81 const char *dev_id;
82 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010083 unsigned long min_rate;
84 unsigned long max_rate;
Stephen Boyd50595f82015-02-06 11:42:44 -080085 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080086};
87
Mike Turquetteeab89f62013-03-28 13:59:01 -070088/*** locking ***/
89static void clk_prepare_lock(void)
90{
Mike Turquette533ddeb2013-03-28 13:59:02 -070091 if (!mutex_trylock(&prepare_lock)) {
92 if (prepare_owner == current) {
93 prepare_refcnt++;
94 return;
95 }
96 mutex_lock(&prepare_lock);
97 }
98 WARN_ON_ONCE(prepare_owner != NULL);
99 WARN_ON_ONCE(prepare_refcnt != 0);
100 prepare_owner = current;
101 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700102}
103
104static void clk_prepare_unlock(void)
105{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700106 WARN_ON_ONCE(prepare_owner != current);
107 WARN_ON_ONCE(prepare_refcnt == 0);
108
109 if (--prepare_refcnt)
110 return;
111 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700112 mutex_unlock(&prepare_lock);
113}
114
115static unsigned long clk_enable_lock(void)
116{
117 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700118
119 if (!spin_trylock_irqsave(&enable_lock, flags)) {
120 if (enable_owner == current) {
121 enable_refcnt++;
122 return flags;
123 }
124 spin_lock_irqsave(&enable_lock, flags);
125 }
126 WARN_ON_ONCE(enable_owner != NULL);
127 WARN_ON_ONCE(enable_refcnt != 0);
128 enable_owner = current;
129 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700130 return flags;
131}
132
133static void clk_enable_unlock(unsigned long flags)
134{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700135 WARN_ON_ONCE(enable_owner != current);
136 WARN_ON_ONCE(enable_refcnt == 0);
137
138 if (--enable_refcnt)
139 return;
140 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700141 spin_unlock_irqrestore(&enable_lock, flags);
142}
143
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700144static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530145{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700146 /*
147 * .is_prepared is optional for clocks that can prepare
148 * fall back to software usage counter if it is missing
149 */
150 if (!core->ops->is_prepared)
151 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530152
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700153 return core->ops->is_prepared(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530154}
155
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700156static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530157{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700158 /*
159 * .is_enabled is only mandatory for clocks that gate
160 * fall back to software usage counter if .is_enabled is missing
161 */
162 if (!core->ops->is_enabled)
163 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530164
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700165 return core->ops->is_enabled(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530166}
167
Stephen Boydd6968fc2015-04-30 13:54:13 -0700168static void clk_unprepare_unused_subtree(struct clk_core *core)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100169{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100170 struct clk_core *child;
Ulf Hansson1c155b32013-03-12 20:26:03 +0100171
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100172 lockdep_assert_held(&prepare_lock);
173
Stephen Boydd6968fc2015-04-30 13:54:13 -0700174 hlist_for_each_entry(child, &core->children, child_node)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100175 clk_unprepare_unused_subtree(child);
176
Stephen Boydd6968fc2015-04-30 13:54:13 -0700177 if (core->prepare_count)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100178 return;
179
Stephen Boydd6968fc2015-04-30 13:54:13 -0700180 if (core->flags & CLK_IGNORE_UNUSED)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100181 return;
182
Stephen Boydd6968fc2015-04-30 13:54:13 -0700183 if (clk_core_is_prepared(core)) {
184 trace_clk_unprepare(core);
185 if (core->ops->unprepare_unused)
186 core->ops->unprepare_unused(core->hw);
187 else if (core->ops->unprepare)
188 core->ops->unprepare(core->hw);
189 trace_clk_unprepare_complete(core);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100190 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100191}
192
Stephen Boydd6968fc2015-04-30 13:54:13 -0700193static void clk_disable_unused_subtree(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700194{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100195 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700196 unsigned long flags;
197
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100198 lockdep_assert_held(&prepare_lock);
199
Stephen Boydd6968fc2015-04-30 13:54:13 -0700200 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700201 clk_disable_unused_subtree(child);
202
Mike Turquetteeab89f62013-03-28 13:59:01 -0700203 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700204
Stephen Boydd6968fc2015-04-30 13:54:13 -0700205 if (core->enable_count)
Mike Turquetteb24764902012-03-15 23:11:19 -0700206 goto unlock_out;
207
Stephen Boydd6968fc2015-04-30 13:54:13 -0700208 if (core->flags & CLK_IGNORE_UNUSED)
Mike Turquetteb24764902012-03-15 23:11:19 -0700209 goto unlock_out;
210
Mike Turquette7c045a52012-12-04 11:00:35 -0800211 /*
212 * some gate clocks have special needs during the disable-unused
213 * sequence. call .disable_unused if available, otherwise fall
214 * back to .disable
215 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700216 if (clk_core_is_enabled(core)) {
217 trace_clk_disable(core);
218 if (core->ops->disable_unused)
219 core->ops->disable_unused(core->hw);
220 else if (core->ops->disable)
221 core->ops->disable(core->hw);
222 trace_clk_disable_complete(core);
Mike Turquette7c045a52012-12-04 11:00:35 -0800223 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700224
225unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700226 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700227}
228
Olof Johansson1e435252013-04-27 14:10:18 -0700229static bool clk_ignore_unused;
230static int __init clk_ignore_unused_setup(char *__unused)
231{
232 clk_ignore_unused = true;
233 return 1;
234}
235__setup("clk_ignore_unused", clk_ignore_unused_setup);
236
Mike Turquetteb24764902012-03-15 23:11:19 -0700237static int clk_disable_unused(void)
238{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700239 struct clk_core *core;
Mike Turquetteb24764902012-03-15 23:11:19 -0700240
Olof Johansson1e435252013-04-27 14:10:18 -0700241 if (clk_ignore_unused) {
242 pr_warn("clk: Not disabling unused clocks\n");
243 return 0;
244 }
245
Mike Turquetteeab89f62013-03-28 13:59:01 -0700246 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700247
Stephen Boydd6968fc2015-04-30 13:54:13 -0700248 hlist_for_each_entry(core, &clk_root_list, child_node)
249 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700250
Stephen Boydd6968fc2015-04-30 13:54:13 -0700251 hlist_for_each_entry(core, &clk_orphan_list, child_node)
252 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700253
Stephen Boydd6968fc2015-04-30 13:54:13 -0700254 hlist_for_each_entry(core, &clk_root_list, child_node)
255 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100256
Stephen Boydd6968fc2015-04-30 13:54:13 -0700257 hlist_for_each_entry(core, &clk_orphan_list, child_node)
258 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100259
Mike Turquetteeab89f62013-03-28 13:59:01 -0700260 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700261
262 return 0;
263}
Saravana Kannand41d5802013-05-09 11:35:01 -0700264late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700265
266/*** helper functions ***/
267
Russ Dill65800b22012-11-26 11:20:09 -0800268const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700269{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100270 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700271}
Niels de Vos48950842012-12-13 13:12:25 +0100272EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700273
Russ Dill65800b22012-11-26 11:20:09 -0800274struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700275{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100276 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700277}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800278EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700279
Russ Dill65800b22012-11-26 11:20:09 -0800280u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700281{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100282 return !clk ? 0 : clk->core->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700283}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800284EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700285
Russ Dill65800b22012-11-26 11:20:09 -0800286struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700287{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100288 if (!clk)
289 return NULL;
290
291 /* TODO: Create a per-user clk and change callers to call clk_put */
292 return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700293}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800294EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700295
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700296static struct clk_core *__clk_lookup_subtree(const char *name,
297 struct clk_core *core)
298{
299 struct clk_core *child;
300 struct clk_core *ret;
301
302 if (!strcmp(core->name, name))
303 return core;
304
305 hlist_for_each_entry(child, &core->children, child_node) {
306 ret = __clk_lookup_subtree(name, child);
307 if (ret)
308 return ret;
309 }
310
311 return NULL;
312}
313
314static struct clk_core *clk_core_lookup(const char *name)
315{
316 struct clk_core *root_clk;
317 struct clk_core *ret;
318
319 if (!name)
320 return NULL;
321
322 /* search the 'proper' clk tree first */
323 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
324 ret = __clk_lookup_subtree(name, root_clk);
325 if (ret)
326 return ret;
327 }
328
329 /* if not found, then search the orphan tree */
330 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
331 ret = __clk_lookup_subtree(name, root_clk);
332 if (ret)
333 return ret;
334 }
335
336 return NULL;
337}
338
Stephen Boydd6968fc2015-04-30 13:54:13 -0700339static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100340 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100341{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700342 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100343 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700344 else if (!core->parents)
345 return clk_core_lookup(core->parent_names[index]);
346 else if (!core->parents[index])
347 return core->parents[index] =
348 clk_core_lookup(core->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100349 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700350 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100351}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100352
353struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
354{
355 struct clk_core *parent;
356
357 if (!clk)
358 return NULL;
359
360 parent = clk_core_get_parent_by_index(clk->core, index);
361
362 return !parent ? NULL : parent->hw->clk;
363}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800364EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100365
Russ Dill65800b22012-11-26 11:20:09 -0800366unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700367{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100368 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700369}
370
Stephen Boydd6968fc2015-04-30 13:54:13 -0700371static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700372{
373 unsigned long ret;
374
Stephen Boydd6968fc2015-04-30 13:54:13 -0700375 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530376 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700377 goto out;
378 }
379
Stephen Boydd6968fc2015-04-30 13:54:13 -0700380 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700381
Stephen Boydd6968fc2015-04-30 13:54:13 -0700382 if (core->flags & CLK_IS_ROOT)
Mike Turquetteb24764902012-03-15 23:11:19 -0700383 goto out;
384
Stephen Boydd6968fc2015-04-30 13:54:13 -0700385 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530386 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700387
388out:
389 return ret;
390}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100391
392unsigned long __clk_get_rate(struct clk *clk)
393{
394 if (!clk)
395 return 0;
396
397 return clk_core_get_rate_nolock(clk->core);
398}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800399EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700400
Stephen Boydd6968fc2015-04-30 13:54:13 -0700401static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100402{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700403 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100404 return 0;
405
Stephen Boydd6968fc2015-04-30 13:54:13 -0700406 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100407}
408
Russ Dill65800b22012-11-26 11:20:09 -0800409unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700410{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100411 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700412}
Thierry Redingb05c6832013-09-03 09:43:51 +0200413EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700414
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100415bool __clk_is_prepared(struct clk *clk)
416{
417 if (!clk)
418 return false;
419
420 return clk_core_is_prepared(clk->core);
421}
422
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100423bool __clk_is_enabled(struct clk *clk)
424{
425 if (!clk)
426 return false;
427
428 return clk_core_is_enabled(clk->core);
429}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800430EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700431
Stephen Boyd15a02c12015-01-19 18:05:28 -0800432static bool mux_is_better_rate(unsigned long rate, unsigned long now,
433 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100434{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800435 if (flags & CLK_MUX_ROUND_CLOSEST)
436 return abs(now - rate) < abs(best - rate);
437
438 return now <= rate && now > best;
439}
440
Boris Brezillon0817b622015-07-07 20:48:08 +0200441static int
442clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800443 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100444{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100445 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200446 int i, num_parents, ret;
447 unsigned long best = 0;
448 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100449
450 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100451 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
452 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200453 if (core->flags & CLK_SET_RATE_PARENT) {
454 ret = __clk_determine_rate(parent ? parent->hw : NULL,
455 &parent_req);
456 if (ret)
457 return ret;
458
459 best = parent_req.rate;
460 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100461 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200462 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100463 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200464 }
465
James Hogane366fdd2013-07-29 12:25:02 +0100466 goto out;
467 }
468
469 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100470 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100471 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100472 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100473 if (!parent)
474 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200475
476 if (core->flags & CLK_SET_RATE_PARENT) {
477 parent_req = *req;
478 ret = __clk_determine_rate(parent->hw, &parent_req);
479 if (ret)
480 continue;
481 } else {
482 parent_req.rate = clk_core_get_rate_nolock(parent);
483 }
484
485 if (mux_is_better_rate(req->rate, parent_req.rate,
486 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100487 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200488 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100489 }
490 }
491
Boris Brezillon57d866e2015-07-09 22:39:38 +0200492 if (!best_parent)
493 return -EINVAL;
494
James Hogane366fdd2013-07-29 12:25:02 +0100495out:
496 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200497 req->best_parent_hw = best_parent->hw;
498 req->best_parent_rate = best;
499 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100500
Boris Brezillon0817b622015-07-07 20:48:08 +0200501 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100502}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800503
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100504struct clk *__clk_lookup(const char *name)
505{
506 struct clk_core *core = clk_core_lookup(name);
507
508 return !core ? NULL : core->hw->clk;
509}
510
Stephen Boydd6968fc2015-04-30 13:54:13 -0700511static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100512 unsigned long *min_rate,
513 unsigned long *max_rate)
514{
515 struct clk *clk_user;
516
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700517 *min_rate = core->min_rate;
518 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100519
Stephen Boydd6968fc2015-04-30 13:54:13 -0700520 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100521 *min_rate = max(*min_rate, clk_user->min_rate);
522
Stephen Boydd6968fc2015-04-30 13:54:13 -0700523 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100524 *max_rate = min(*max_rate, clk_user->max_rate);
525}
526
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700527void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
528 unsigned long max_rate)
529{
530 hw->core->min_rate = min_rate;
531 hw->core->max_rate = max_rate;
532}
533EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
534
Stephen Boyd15a02c12015-01-19 18:05:28 -0800535/*
536 * Helper for finding best parent to provide a given frequency. This can be used
537 * directly as a determine_rate callback (e.g. for a mux), or from a more
538 * complex clock that may combine a mux with other operations.
539 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200540int __clk_mux_determine_rate(struct clk_hw *hw,
541 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800542{
Boris Brezillon0817b622015-07-07 20:48:08 +0200543 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800544}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800545EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100546
Boris Brezillon0817b622015-07-07 20:48:08 +0200547int __clk_mux_determine_rate_closest(struct clk_hw *hw,
548 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800549{
Boris Brezillon0817b622015-07-07 20:48:08 +0200550 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800551}
552EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
553
Mike Turquetteb24764902012-03-15 23:11:19 -0700554/*** clk api ***/
555
Stephen Boydd6968fc2015-04-30 13:54:13 -0700556static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700557{
Stephen Boyda6334722015-05-06 17:00:54 -0700558 lockdep_assert_held(&prepare_lock);
559
Stephen Boydd6968fc2015-04-30 13:54:13 -0700560 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700561 return;
562
Stephen Boydd6968fc2015-04-30 13:54:13 -0700563 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700564 return;
565
Stephen Boydd6968fc2015-04-30 13:54:13 -0700566 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700567 return;
568
Stephen Boydd6968fc2015-04-30 13:54:13 -0700569 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700570
Stephen Boydd6968fc2015-04-30 13:54:13 -0700571 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800572
Stephen Boydd6968fc2015-04-30 13:54:13 -0700573 if (core->ops->unprepare)
574 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700575
Stephen Boydd6968fc2015-04-30 13:54:13 -0700576 trace_clk_unprepare_complete(core);
577 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700578}
579
580/**
581 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200582 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700583 *
584 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
585 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
586 * if the operation may sleep. One example is a clk which is accessed over
587 * I2c. In the complex case a clk gate operation may require a fast and a slow
588 * part. It is this reason that clk_unprepare and clk_disable are not mutually
589 * exclusive. In fact clk_disable must be called before clk_unprepare.
590 */
591void clk_unprepare(struct clk *clk)
592{
Stephen Boyd63589e92014-03-26 16:06:37 -0700593 if (IS_ERR_OR_NULL(clk))
594 return;
595
Mike Turquetteeab89f62013-03-28 13:59:01 -0700596 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100597 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700598 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700599}
600EXPORT_SYMBOL_GPL(clk_unprepare);
601
Stephen Boydd6968fc2015-04-30 13:54:13 -0700602static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700603{
604 int ret = 0;
605
Stephen Boyda6334722015-05-06 17:00:54 -0700606 lockdep_assert_held(&prepare_lock);
607
Stephen Boydd6968fc2015-04-30 13:54:13 -0700608 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700609 return 0;
610
Stephen Boydd6968fc2015-04-30 13:54:13 -0700611 if (core->prepare_count == 0) {
612 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700613 if (ret)
614 return ret;
615
Stephen Boydd6968fc2015-04-30 13:54:13 -0700616 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800617
Stephen Boydd6968fc2015-04-30 13:54:13 -0700618 if (core->ops->prepare)
619 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800620
Stephen Boydd6968fc2015-04-30 13:54:13 -0700621 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800622
623 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700624 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800625 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700626 }
627 }
628
Stephen Boydd6968fc2015-04-30 13:54:13 -0700629 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700630
631 return 0;
632}
633
634/**
635 * clk_prepare - prepare a clock source
636 * @clk: the clk being prepared
637 *
638 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
639 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
640 * operation may sleep. One example is a clk which is accessed over I2c. In
641 * the complex case a clk ungate operation may require a fast and a slow part.
642 * It is this reason that clk_prepare and clk_enable are not mutually
643 * exclusive. In fact clk_prepare must be called before clk_enable.
644 * Returns 0 on success, -EERROR otherwise.
645 */
646int clk_prepare(struct clk *clk)
647{
648 int ret;
649
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100650 if (!clk)
651 return 0;
652
Mike Turquetteeab89f62013-03-28 13:59:01 -0700653 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100654 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700655 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700656
657 return ret;
658}
659EXPORT_SYMBOL_GPL(clk_prepare);
660
Stephen Boydd6968fc2015-04-30 13:54:13 -0700661static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700662{
Stephen Boyda6334722015-05-06 17:00:54 -0700663 lockdep_assert_held(&enable_lock);
664
Stephen Boydd6968fc2015-04-30 13:54:13 -0700665 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700666 return;
667
Stephen Boydd6968fc2015-04-30 13:54:13 -0700668 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700669 return;
670
Stephen Boydd6968fc2015-04-30 13:54:13 -0700671 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700672 return;
673
Stephen Boydd6968fc2015-04-30 13:54:13 -0700674 trace_clk_disable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800675
Stephen Boydd6968fc2015-04-30 13:54:13 -0700676 if (core->ops->disable)
677 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700678
Stephen Boydd6968fc2015-04-30 13:54:13 -0700679 trace_clk_disable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800680
Stephen Boydd6968fc2015-04-30 13:54:13 -0700681 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100682}
683
Mike Turquetteb24764902012-03-15 23:11:19 -0700684/**
685 * clk_disable - gate a clock
686 * @clk: the clk being gated
687 *
688 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
689 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
690 * clk if the operation is fast and will never sleep. One example is a
691 * SoC-internal clk which is controlled via simple register writes. In the
692 * complex case a clk gate operation may require a fast and a slow part. It is
693 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
694 * In fact clk_disable must be called before clk_unprepare.
695 */
696void clk_disable(struct clk *clk)
697{
698 unsigned long flags;
699
Stephen Boyd63589e92014-03-26 16:06:37 -0700700 if (IS_ERR_OR_NULL(clk))
701 return;
702
Mike Turquetteeab89f62013-03-28 13:59:01 -0700703 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700704 clk_core_disable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700705 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700706}
707EXPORT_SYMBOL_GPL(clk_disable);
708
Stephen Boydd6968fc2015-04-30 13:54:13 -0700709static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700710{
711 int ret = 0;
712
Stephen Boyda6334722015-05-06 17:00:54 -0700713 lockdep_assert_held(&enable_lock);
714
Stephen Boydd6968fc2015-04-30 13:54:13 -0700715 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700716 return 0;
717
Stephen Boydd6968fc2015-04-30 13:54:13 -0700718 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700719 return -ESHUTDOWN;
720
Stephen Boydd6968fc2015-04-30 13:54:13 -0700721 if (core->enable_count == 0) {
722 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700723
724 if (ret)
725 return ret;
726
Stephen Boydd6968fc2015-04-30 13:54:13 -0700727 trace_clk_enable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800728
Stephen Boydd6968fc2015-04-30 13:54:13 -0700729 if (core->ops->enable)
730 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800731
Stephen Boydd6968fc2015-04-30 13:54:13 -0700732 trace_clk_enable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800733
734 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700735 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800736 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700737 }
738 }
739
Stephen Boydd6968fc2015-04-30 13:54:13 -0700740 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700741 return 0;
742}
743
744/**
745 * clk_enable - ungate a clock
746 * @clk: the clk being ungated
747 *
748 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
749 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
750 * if the operation will never sleep. One example is a SoC-internal clk which
751 * is controlled via simple register writes. In the complex case a clk ungate
752 * operation may require a fast and a slow part. It is this reason that
753 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
754 * must be called before clk_enable. Returns 0 on success, -EERROR
755 * otherwise.
756 */
757int clk_enable(struct clk *clk)
758{
759 unsigned long flags;
760 int ret;
761
Dong Aisheng864e1602015-04-30 14:02:19 -0700762 if (!clk)
763 return 0;
764
Mike Turquetteeab89f62013-03-28 13:59:01 -0700765 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700766 ret = clk_core_enable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700767 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700768
769 return ret;
770}
771EXPORT_SYMBOL_GPL(clk_enable);
772
Boris Brezillon0817b622015-07-07 20:48:08 +0200773static int clk_core_round_rate_nolock(struct clk_core *core,
774 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -0700775{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100776 struct clk_core *parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200777 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700778
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100779 lockdep_assert_held(&prepare_lock);
780
Stephen Boydd6968fc2015-04-30 13:54:13 -0700781 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700782 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700783
Stephen Boydd6968fc2015-04-30 13:54:13 -0700784 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200785 if (parent) {
786 req->best_parent_hw = parent->hw;
787 req->best_parent_rate = parent->rate;
788 } else {
789 req->best_parent_hw = NULL;
790 req->best_parent_rate = 0;
791 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700792
Stephen Boydd6968fc2015-04-30 13:54:13 -0700793 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +0200794 return core->ops->determine_rate(core->hw, req);
795 } else if (core->ops->round_rate) {
796 rate = core->ops->round_rate(core->hw, req->rate,
797 &req->best_parent_rate);
798 if (rate < 0)
799 return rate;
800
801 req->rate = rate;
802 } else if (core->flags & CLK_SET_RATE_PARENT) {
803 return clk_core_round_rate_nolock(parent, req);
804 } else {
805 req->rate = core->rate;
806 }
807
808 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700809}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100810
811/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100812 * __clk_determine_rate - get the closest rate actually supported by a clock
813 * @hw: determine the rate of this clock
814 * @rate: target rate
815 * @min_rate: returned rate must be greater than this rate
816 * @max_rate: returned rate must be less than this rate
817 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700818 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100819 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200820int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100821{
Boris Brezillon0817b622015-07-07 20:48:08 +0200822 if (!hw) {
823 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100824 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +0200825 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100826
Boris Brezillon0817b622015-07-07 20:48:08 +0200827 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100828}
829EXPORT_SYMBOL_GPL(__clk_determine_rate);
830
831/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100832 * __clk_round_rate - round the given rate for a clk
833 * @clk: round the rate of this clock
834 * @rate: the rate which is to be rounded
835 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700836 * Useful for clk_ops such as .set_rate
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100837 */
838unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
839{
Boris Brezillon0817b622015-07-07 20:48:08 +0200840 struct clk_rate_request req;
841 int ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100842
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100843 if (!clk)
844 return 0;
845
Boris Brezillon0817b622015-07-07 20:48:08 +0200846 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
847 req.rate = rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100848
Boris Brezillon0817b622015-07-07 20:48:08 +0200849 ret = clk_core_round_rate_nolock(clk->core, &req);
850 if (ret)
851 return 0;
852
853 return req.rate;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100854}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +0200855EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700856
857/**
858 * clk_round_rate - round the given rate for a clk
859 * @clk: the clk for which we are rounding a rate
860 * @rate: the rate which is to be rounded
861 *
862 * Takes in a rate as input and rounds it to a rate that the clk can actually
863 * use which is then returned. If clk doesn't support round_rate operation
864 * then the parent rate is returned.
865 */
866long clk_round_rate(struct clk *clk, unsigned long rate)
867{
868 unsigned long ret;
869
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100870 if (!clk)
871 return 0;
872
Mike Turquetteeab89f62013-03-28 13:59:01 -0700873 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700874 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700875 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700876
877 return ret;
878}
879EXPORT_SYMBOL_GPL(clk_round_rate);
880
881/**
882 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -0700883 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -0700884 * @msg: clk notifier type (see include/linux/clk.h)
885 * @old_rate: old clk rate
886 * @new_rate: new clk rate
887 *
888 * Triggers a notifier call chain on the clk rate-change notification
889 * for 'clk'. Passes a pointer to the struct clk and the previous
890 * and current rates to the notifier callback. Intended to be called by
891 * internal clock code only. Returns NOTIFY_DONE from the last driver
892 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
893 * a driver returns that.
894 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700895static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -0700896 unsigned long old_rate, unsigned long new_rate)
897{
898 struct clk_notifier *cn;
899 struct clk_notifier_data cnd;
900 int ret = NOTIFY_DONE;
901
Mike Turquetteb24764902012-03-15 23:11:19 -0700902 cnd.old_rate = old_rate;
903 cnd.new_rate = new_rate;
904
905 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700906 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100907 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700908 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
909 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -0700910 }
911 }
912
913 return ret;
914}
915
916/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100917 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -0700918 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100919 *
920 * Walks the subtree of clks starting with clk and recalculates accuracies as
921 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700922 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100923 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100924 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700925static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100926{
927 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100928 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100929
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100930 lockdep_assert_held(&prepare_lock);
931
Stephen Boydd6968fc2015-04-30 13:54:13 -0700932 if (core->parent)
933 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100934
Stephen Boydd6968fc2015-04-30 13:54:13 -0700935 if (core->ops->recalc_accuracy)
936 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100937 parent_accuracy);
938 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700939 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100940
Stephen Boydd6968fc2015-04-30 13:54:13 -0700941 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100942 __clk_recalc_accuracies(child);
943}
944
Stephen Boydd6968fc2015-04-30 13:54:13 -0700945static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100946{
947 unsigned long accuracy;
948
949 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -0700950 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
951 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100952
Stephen Boydd6968fc2015-04-30 13:54:13 -0700953 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100954 clk_prepare_unlock();
955
956 return accuracy;
957}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100958
959/**
960 * clk_get_accuracy - return the accuracy of clk
961 * @clk: the clk whose accuracy is being returned
962 *
963 * Simply returns the cached accuracy of the clk, unless
964 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
965 * issued.
966 * If clk is NULL then returns 0.
967 */
968long clk_get_accuracy(struct clk *clk)
969{
970 if (!clk)
971 return 0;
972
973 return clk_core_get_accuracy(clk->core);
974}
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100975EXPORT_SYMBOL_GPL(clk_get_accuracy);
976
Stephen Boydd6968fc2015-04-30 13:54:13 -0700977static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100978 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700979{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700980 if (core->ops->recalc_rate)
981 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -0700982 return parent_rate;
983}
984
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100985/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700986 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -0700987 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -0700988 * @msg: notification type (see include/linux/clk.h)
989 *
990 * Walks the subtree of clks starting with clk and recalculates rates as it
991 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200992 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -0700993 *
994 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
995 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -0700996 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700997static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -0700998{
999 unsigned long old_rate;
1000 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001001 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001002
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001003 lockdep_assert_held(&prepare_lock);
1004
Stephen Boydd6968fc2015-04-30 13:54:13 -07001005 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001006
Stephen Boydd6968fc2015-04-30 13:54:13 -07001007 if (core->parent)
1008 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001009
Stephen Boydd6968fc2015-04-30 13:54:13 -07001010 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001011
1012 /*
1013 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1014 * & ABORT_RATE_CHANGE notifiers
1015 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001016 if (core->notifier_count && msg)
1017 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001018
Stephen Boydd6968fc2015-04-30 13:54:13 -07001019 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001020 __clk_recalc_rates(child, msg);
1021}
1022
Stephen Boydd6968fc2015-04-30 13:54:13 -07001023static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001024{
1025 unsigned long rate;
1026
1027 clk_prepare_lock();
1028
Stephen Boydd6968fc2015-04-30 13:54:13 -07001029 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1030 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001031
Stephen Boydd6968fc2015-04-30 13:54:13 -07001032 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001033 clk_prepare_unlock();
1034
1035 return rate;
1036}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001037
Mike Turquetteb24764902012-03-15 23:11:19 -07001038/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001039 * clk_get_rate - return the rate of clk
1040 * @clk: the clk whose rate is being returned
1041 *
1042 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1043 * is set, which means a recalc_rate will be issued.
1044 * If clk is NULL then returns 0.
1045 */
1046unsigned long clk_get_rate(struct clk *clk)
1047{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001048 if (!clk)
1049 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001050
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001051 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001052}
1053EXPORT_SYMBOL_GPL(clk_get_rate);
1054
Stephen Boydd6968fc2015-04-30 13:54:13 -07001055static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001056 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001057{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001058 int i;
James Hogan4935b222013-07-29 12:24:59 +01001059
Stephen Boydd6968fc2015-04-30 13:54:13 -07001060 if (!core->parents) {
1061 core->parents = kcalloc(core->num_parents,
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001062 sizeof(struct clk *), GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001063 if (!core->parents)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001064 return -ENOMEM;
1065 }
James Hogan4935b222013-07-29 12:24:59 +01001066
1067 /*
1068 * find index of new parent clock using cached parent ptrs,
1069 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001070 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001071 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001072 for (i = 0; i < core->num_parents; i++) {
1073 if (core->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001074 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001075
Stephen Boydd6968fc2015-04-30 13:54:13 -07001076 if (core->parents[i])
Tomasz Figada0f0b22013-09-29 02:37:16 +02001077 continue;
1078
Stephen Boydd6968fc2015-04-30 13:54:13 -07001079 if (!strcmp(core->parent_names[i], parent->name)) {
1080 core->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001081 return i;
James Hogan4935b222013-07-29 12:24:59 +01001082 }
1083 }
1084
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001085 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001086}
1087
Stephen Boydd6968fc2015-04-30 13:54:13 -07001088static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001089{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001090 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001091
James Hogan903efc52013-08-29 12:10:51 +01001092 if (new_parent) {
1093 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001094 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001095 new_parent->new_child = NULL;
1096
Stephen Boydd6968fc2015-04-30 13:54:13 -07001097 hlist_add_head(&core->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001098 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001099 hlist_add_head(&core->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001100 }
James Hogan4935b222013-07-29 12:24:59 +01001101
Stephen Boydd6968fc2015-04-30 13:54:13 -07001102 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001103}
1104
Stephen Boydd6968fc2015-04-30 13:54:13 -07001105static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001106 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001107{
1108 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001109 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001110
1111 /*
1112 * Migrate prepare state between parents and prevent race with
1113 * clk_enable().
1114 *
1115 * If the clock is not prepared, then a race with
1116 * clk_enable/disable() is impossible since we already have the
1117 * prepare lock (future calls to clk_enable() need to be preceded by
1118 * a clk_prepare()).
1119 *
1120 * If the clock is prepared, migrate the prepared state to the new
1121 * parent and also protect against a race with clk_enable() by
1122 * forcing the clock and the new parent on. This ensures that all
1123 * future calls to clk_enable() are practically NOPs with respect to
1124 * hardware and software states.
1125 *
1126 * See also: Comment for clk_set_parent() below.
1127 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001128 if (core->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001129 clk_core_prepare(parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001130 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001131 clk_core_enable(parent);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001132 clk_core_enable(core);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001133 clk_enable_unlock(flags);
James Hogan4935b222013-07-29 12:24:59 +01001134 }
1135
1136 /* update the clk tree topology */
1137 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001138 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001139 clk_enable_unlock(flags);
1140
Stephen Boyd3fa22522014-01-15 10:47:22 -08001141 return old_parent;
1142}
1143
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001144static void __clk_set_parent_after(struct clk_core *core,
1145 struct clk_core *parent,
1146 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001147{
Dong Aishengd2a5d462015-04-15 22:26:36 +08001148 unsigned long flags;
1149
Stephen Boyd3fa22522014-01-15 10:47:22 -08001150 /*
1151 * Finish the migration of prepare state and undo the changes done
1152 * for preventing a race with clk_enable().
1153 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001154 if (core->prepare_count) {
Dong Aishengd2a5d462015-04-15 22:26:36 +08001155 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001156 clk_core_disable(core);
1157 clk_core_disable(old_parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001158 clk_enable_unlock(flags);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001159 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001160 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001161}
1162
Stephen Boydd6968fc2015-04-30 13:54:13 -07001163static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001164 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001165{
1166 unsigned long flags;
1167 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001168 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001169
Stephen Boydd6968fc2015-04-30 13:54:13 -07001170 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001171
Stephen Boydd6968fc2015-04-30 13:54:13 -07001172 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001173
James Hogan4935b222013-07-29 12:24:59 +01001174 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001175 if (parent && core->ops->set_parent)
1176 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001177
Stephen Boydd6968fc2015-04-30 13:54:13 -07001178 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001179
James Hogan4935b222013-07-29 12:24:59 +01001180 if (ret) {
1181 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001182 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001183 clk_enable_unlock(flags);
1184
Stephen Boydd6968fc2015-04-30 13:54:13 -07001185 if (core->prepare_count) {
Dong Aishengd2a5d462015-04-15 22:26:36 +08001186 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001187 clk_core_disable(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001188 clk_core_disable(parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001189 clk_enable_unlock(flags);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001190 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001191 }
1192 return ret;
1193 }
1194
Stephen Boydd6968fc2015-04-30 13:54:13 -07001195 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001196
James Hogan4935b222013-07-29 12:24:59 +01001197 return 0;
1198}
1199
Ulf Hanssona093bde2012-08-31 14:21:28 +02001200/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001201 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001202 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001203 * @parent_rate: the "future" rate of clk's parent
1204 *
1205 * Walks the subtree of clks starting with clk, speculating rates as it
1206 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1207 *
1208 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1209 * pre-rate change notifications and returns early if no clks in the
1210 * subtree have subscribed to the notifications. Note that if a clk does not
1211 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001212 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001213 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001214static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001215 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001216{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001217 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001218 unsigned long new_rate;
1219 int ret = NOTIFY_DONE;
1220
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001221 lockdep_assert_held(&prepare_lock);
1222
Stephen Boydd6968fc2015-04-30 13:54:13 -07001223 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001224
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001225 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001226 if (core->notifier_count)
1227 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001228
Mike Turquette86bcfa22014-02-24 16:08:41 -08001229 if (ret & NOTIFY_STOP_MASK) {
1230 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001231 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001232 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001233 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001234
Stephen Boydd6968fc2015-04-30 13:54:13 -07001235 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001236 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001237 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001238 break;
1239 }
1240
1241out:
1242 return ret;
1243}
1244
Stephen Boydd6968fc2015-04-30 13:54:13 -07001245static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001246 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001247{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001248 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001249
Stephen Boydd6968fc2015-04-30 13:54:13 -07001250 core->new_rate = new_rate;
1251 core->new_parent = new_parent;
1252 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001253 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001254 core->new_child = NULL;
1255 if (new_parent && new_parent != core->parent)
1256 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001257
Stephen Boydd6968fc2015-04-30 13:54:13 -07001258 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001259 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001260 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001261 }
1262}
1263
1264/*
1265 * calculate the new rates returning the topmost clock that has to be
1266 * changed.
1267 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001268static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001269 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001270{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001271 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001272 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001273 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001274 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001275 unsigned long min_rate;
1276 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001277 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001278 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001279
Mike Turquette7452b212012-03-26 14:45:36 -07001280 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001281 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001282 return NULL;
1283
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001284 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001285 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001286 if (parent)
1287 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001288
Stephen Boydd6968fc2015-04-30 13:54:13 -07001289 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001290
James Hogan71472c02013-07-29 12:25:00 +01001291 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001292 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001293 struct clk_rate_request req;
1294
1295 req.rate = rate;
1296 req.min_rate = min_rate;
1297 req.max_rate = max_rate;
1298 if (parent) {
1299 req.best_parent_hw = parent->hw;
1300 req.best_parent_rate = parent->rate;
1301 } else {
1302 req.best_parent_hw = NULL;
1303 req.best_parent_rate = 0;
1304 }
1305
1306 ret = core->ops->determine_rate(core->hw, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001307 if (ret < 0)
1308 return NULL;
1309
Boris Brezillon0817b622015-07-07 20:48:08 +02001310 best_parent_rate = req.best_parent_rate;
1311 new_rate = req.rate;
1312 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001313 } else if (core->ops->round_rate) {
1314 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon0817b622015-07-07 20:48:08 +02001315 &best_parent_rate);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001316 if (ret < 0)
1317 return NULL;
1318
1319 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001320 if (new_rate < min_rate || new_rate > max_rate)
1321 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001322 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001323 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001324 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001325 return NULL;
1326 } else {
1327 /* pass-through clock with adjustable parent */
1328 top = clk_calc_new_rates(parent, rate);
1329 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001330 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001331 }
1332
James Hogan71472c02013-07-29 12:25:00 +01001333 /* some clocks must be gated to change parent */
1334 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001335 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001336 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001337 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001338 return NULL;
1339 }
1340
James Hogan71472c02013-07-29 12:25:00 +01001341 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001342 if (parent && core->num_parents > 1) {
1343 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001344 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001345 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001346 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001347 return NULL;
1348 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001349 }
1350
Stephen Boydd6968fc2015-04-30 13:54:13 -07001351 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001352 best_parent_rate != parent->rate)
1353 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001354
1355out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001356 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001357
1358 return top;
1359}
1360
1361/*
1362 * Notify about rate changes in a subtree. Always walk down the whole tree
1363 * so that in case of an error we can walk down the whole tree again and
1364 * abort the change.
1365 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001366static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001367 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001368{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001369 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001370 int ret = NOTIFY_DONE;
1371
Stephen Boydd6968fc2015-04-30 13:54:13 -07001372 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301373 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001374
Stephen Boydd6968fc2015-04-30 13:54:13 -07001375 if (core->notifier_count) {
1376 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001377 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001378 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001379 }
1380
Stephen Boydd6968fc2015-04-30 13:54:13 -07001381 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001382 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001383 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001384 continue;
1385 tmp_clk = clk_propagate_rate_change(child, event);
1386 if (tmp_clk)
1387 fail_clk = tmp_clk;
1388 }
1389
Stephen Boydd6968fc2015-04-30 13:54:13 -07001390 /* handle the new child who might not be in core->children yet */
1391 if (core->new_child) {
1392 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001393 if (tmp_clk)
1394 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001395 }
1396
1397 return fail_clk;
1398}
1399
1400/*
1401 * walk down a subtree and set the new rates notifying the rate
1402 * change on the way
1403 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001404static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001405{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001406 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001407 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001408 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001409 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001410 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001411 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001412
Stephen Boydd6968fc2015-04-30 13:54:13 -07001413 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001414
Stephen Boydd6968fc2015-04-30 13:54:13 -07001415 if (core->new_parent)
1416 best_parent_rate = core->new_parent->rate;
1417 else if (core->parent)
1418 best_parent_rate = core->parent->rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001419
Stephen Boydd6968fc2015-04-30 13:54:13 -07001420 if (core->new_parent && core->new_parent != core->parent) {
1421 old_parent = __clk_set_parent_before(core, core->new_parent);
1422 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001423
Stephen Boydd6968fc2015-04-30 13:54:13 -07001424 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001425 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001426 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001427 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001428 core->new_parent_index);
1429 } else if (core->ops->set_parent) {
1430 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001431 }
1432
Stephen Boydd6968fc2015-04-30 13:54:13 -07001433 trace_clk_set_parent_complete(core, core->new_parent);
1434 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001435 }
1436
Stephen Boydd6968fc2015-04-30 13:54:13 -07001437 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001438
Stephen Boydd6968fc2015-04-30 13:54:13 -07001439 if (!skip_set_rate && core->ops->set_rate)
1440 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001441
Stephen Boydd6968fc2015-04-30 13:54:13 -07001442 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001443
Stephen Boydd6968fc2015-04-30 13:54:13 -07001444 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001445
Stephen Boydd6968fc2015-04-30 13:54:13 -07001446 if (core->notifier_count && old_rate != core->rate)
1447 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001448
Michael Turquette85e88fa2015-06-20 12:18:03 -07001449 if (core->flags & CLK_RECALC_NEW_RATES)
1450 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001451
Tero Kristo067bb172014-08-21 16:47:45 +03001452 /*
1453 * Use safe iteration, as change_rate can actually swap parents
1454 * for certain clock types.
1455 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001456 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001457 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001458 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001459 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001460 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001461 }
1462
Stephen Boydd6968fc2015-04-30 13:54:13 -07001463 /* handle the new child who might not be in core->children yet */
1464 if (core->new_child)
1465 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001466}
1467
Stephen Boydd6968fc2015-04-30 13:54:13 -07001468static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001469 unsigned long req_rate)
1470{
1471 struct clk_core *top, *fail_clk;
1472 unsigned long rate = req_rate;
1473 int ret = 0;
1474
Stephen Boydd6968fc2015-04-30 13:54:13 -07001475 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001476 return 0;
1477
1478 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001479 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001480 return 0;
1481
Stephen Boydd6968fc2015-04-30 13:54:13 -07001482 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001483 return -EBUSY;
1484
1485 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001486 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001487 if (!top)
1488 return -EINVAL;
1489
1490 /* notify that we are about to change rates */
1491 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1492 if (fail_clk) {
1493 pr_debug("%s: failed to set %s rate\n", __func__,
1494 fail_clk->name);
1495 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1496 return -EBUSY;
1497 }
1498
1499 /* change the rates */
1500 clk_change_rate(top);
1501
Stephen Boydd6968fc2015-04-30 13:54:13 -07001502 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001503
1504 return ret;
1505}
1506
Mike Turquetteb24764902012-03-15 23:11:19 -07001507/**
1508 * clk_set_rate - specify a new rate for clk
1509 * @clk: the clk whose rate is being changed
1510 * @rate: the new rate for clk
1511 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001512 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001513 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001514 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1515 * propagate up to clk's parent; whether or not this happens depends on the
1516 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1517 * after calling .round_rate then upstream parent propagation is ignored. If
1518 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001519 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001520 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1521 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001522 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001523 * Rate changes are accomplished via tree traversal that also recalculates the
1524 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001525 *
1526 * Returns 0 on success, -EERROR otherwise.
1527 */
1528int clk_set_rate(struct clk *clk, unsigned long rate)
1529{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001530 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001531
Mike Turquette89ac8d72013-08-21 23:58:09 -07001532 if (!clk)
1533 return 0;
1534
Mike Turquetteb24764902012-03-15 23:11:19 -07001535 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001536 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001537
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001538 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001539
Mike Turquetteeab89f62013-03-28 13:59:01 -07001540 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001541
1542 return ret;
1543}
1544EXPORT_SYMBOL_GPL(clk_set_rate);
1545
1546/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001547 * clk_set_rate_range - set a rate range for a clock source
1548 * @clk: clock source
1549 * @min: desired minimum clock rate in Hz, inclusive
1550 * @max: desired maximum clock rate in Hz, inclusive
1551 *
1552 * Returns success (0) or negative errno.
1553 */
1554int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1555{
1556 int ret = 0;
1557
1558 if (!clk)
1559 return 0;
1560
1561 if (min > max) {
1562 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1563 __func__, clk->core->name, clk->dev_id, clk->con_id,
1564 min, max);
1565 return -EINVAL;
1566 }
1567
1568 clk_prepare_lock();
1569
1570 if (min != clk->min_rate || max != clk->max_rate) {
1571 clk->min_rate = min;
1572 clk->max_rate = max;
1573 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1574 }
1575
1576 clk_prepare_unlock();
1577
1578 return ret;
1579}
1580EXPORT_SYMBOL_GPL(clk_set_rate_range);
1581
1582/**
1583 * clk_set_min_rate - set a minimum clock rate for a clock source
1584 * @clk: clock source
1585 * @rate: desired minimum clock rate in Hz, inclusive
1586 *
1587 * Returns success (0) or negative errno.
1588 */
1589int clk_set_min_rate(struct clk *clk, unsigned long rate)
1590{
1591 if (!clk)
1592 return 0;
1593
1594 return clk_set_rate_range(clk, rate, clk->max_rate);
1595}
1596EXPORT_SYMBOL_GPL(clk_set_min_rate);
1597
1598/**
1599 * clk_set_max_rate - set a maximum clock rate for a clock source
1600 * @clk: clock source
1601 * @rate: desired maximum clock rate in Hz, inclusive
1602 *
1603 * Returns success (0) or negative errno.
1604 */
1605int clk_set_max_rate(struct clk *clk, unsigned long rate)
1606{
1607 if (!clk)
1608 return 0;
1609
1610 return clk_set_rate_range(clk, clk->min_rate, rate);
1611}
1612EXPORT_SYMBOL_GPL(clk_set_max_rate);
1613
1614/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001615 * clk_get_parent - return the parent of a clk
1616 * @clk: the clk whose parent gets returned
1617 *
1618 * Simply returns clk->parent. Returns NULL if clk is NULL.
1619 */
1620struct clk *clk_get_parent(struct clk *clk)
1621{
1622 struct clk *parent;
1623
Mike Turquetteeab89f62013-03-28 13:59:01 -07001624 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001625 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001626 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001627
1628 return parent;
1629}
1630EXPORT_SYMBOL_GPL(clk_get_parent);
1631
1632/*
1633 * .get_parent is mandatory for clocks with multiple possible parents. It is
1634 * optional for single-parent clocks. Always call .get_parent if it is
1635 * available and WARN if it is missing for multi-parent clocks.
1636 *
1637 * For single-parent clocks without .get_parent, first check to see if the
1638 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001639 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001640 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001641static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001642{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001643 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001644 u8 index;
1645
1646 /* handle the trivial cases */
1647
Stephen Boydd6968fc2015-04-30 13:54:13 -07001648 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -07001649 goto out;
1650
Stephen Boydd6968fc2015-04-30 13:54:13 -07001651 if (core->num_parents == 1) {
1652 if (IS_ERR_OR_NULL(core->parent))
1653 core->parent = clk_core_lookup(core->parent_names[0]);
1654 ret = core->parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001655 goto out;
1656 }
1657
Stephen Boydd6968fc2015-04-30 13:54:13 -07001658 if (!core->ops->get_parent) {
1659 WARN(!core->ops->get_parent,
Mike Turquetteb24764902012-03-15 23:11:19 -07001660 "%s: multi-parent clocks must implement .get_parent\n",
1661 __func__);
1662 goto out;
1663 };
1664
1665 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07001666 * Do our best to cache parent clocks in core->parents. This prevents
1667 * unnecessary and expensive lookups. We don't set core->parent here;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001668 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001669 */
1670
Stephen Boydd6968fc2015-04-30 13:54:13 -07001671 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001672
Stephen Boydd6968fc2015-04-30 13:54:13 -07001673 if (!core->parents)
1674 core->parents =
1675 kcalloc(core->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001676 GFP_KERNEL);
1677
Stephen Boydd6968fc2015-04-30 13:54:13 -07001678 ret = clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001679
1680out:
1681 return ret;
1682}
1683
Stephen Boydd6968fc2015-04-30 13:54:13 -07001684static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001685 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001686{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001687 clk_reparent(core, new_parent);
1688 __clk_recalc_accuracies(core);
1689 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07001690}
1691
Tomeu Vizoso42c86542015-03-11 11:34:25 +01001692void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
1693{
1694 if (!hw)
1695 return;
1696
1697 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
1698}
1699
Mike Turquetteb24764902012-03-15 23:11:19 -07001700/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001701 * clk_has_parent - check if a clock is a possible parent for another
1702 * @clk: clock source
1703 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07001704 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001705 * This function can be used in drivers that need to check that a clock can be
1706 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001707 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001708 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001709 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001710bool clk_has_parent(struct clk *clk, struct clk *parent)
1711{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001712 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001713 unsigned int i;
1714
1715 /* NULL clocks should be nops, so return success if either is NULL. */
1716 if (!clk || !parent)
1717 return true;
1718
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001719 core = clk->core;
1720 parent_core = parent->core;
1721
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001722 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001723 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001724 return true;
1725
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001726 for (i = 0; i < core->num_parents; i++)
1727 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001728 return true;
1729
1730 return false;
1731}
1732EXPORT_SYMBOL_GPL(clk_has_parent);
1733
Stephen Boydd6968fc2015-04-30 13:54:13 -07001734static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001735{
1736 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001737 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001738 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001739
Stephen Boydd6968fc2015-04-30 13:54:13 -07001740 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07001741 return 0;
1742
Mike Turquetteb24764902012-03-15 23:11:19 -07001743 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001744 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001745
Stephen Boydd6968fc2015-04-30 13:54:13 -07001746 if (core->parent == parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001747 goto out;
1748
Stephen Boydb61c43c2015-02-02 14:11:25 -08001749 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001750 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08001751 ret = -ENOSYS;
1752 goto out;
1753 }
1754
Ulf Hansson031dcc92013-04-02 23:09:38 +02001755 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001756 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001757 ret = -EBUSY;
1758 goto out;
1759 }
1760
1761 /* try finding the new parent index */
1762 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001763 p_index = clk_fetch_parent_index(core, parent);
Ulf Hansson031dcc92013-04-02 23:09:38 +02001764 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001765 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001766 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001767 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001768 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001769 goto out;
1770 }
1771 }
1772
Mike Turquetteb24764902012-03-15 23:11:19 -07001773 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001774 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001775
1776 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001777 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001778 goto out;
1779
Ulf Hansson031dcc92013-04-02 23:09:38 +02001780 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001781 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001782
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001783 /* propagate rate an accuracy recalculation accordingly */
1784 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001785 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001786 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001787 __clk_recalc_rates(core, POST_RATE_CHANGE);
1788 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001789 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001790
1791out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001792 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001793
1794 return ret;
1795}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001796
1797/**
1798 * clk_set_parent - switch the parent of a mux clk
1799 * @clk: the mux clk whose input we are switching
1800 * @parent: the new input to clk
1801 *
1802 * Re-parent clk to use parent as its new input source. If clk is in
1803 * prepared state, the clk will get enabled for the duration of this call. If
1804 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1805 * that, the reparenting is glitchy in hardware, etc), use the
1806 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1807 *
1808 * After successfully changing clk's parent clk_set_parent will update the
1809 * clk topology, sysfs topology and propagate rate recalculation via
1810 * __clk_recalc_rates.
1811 *
1812 * Returns 0 on success, -EERROR otherwise.
1813 */
1814int clk_set_parent(struct clk *clk, struct clk *parent)
1815{
1816 if (!clk)
1817 return 0;
1818
1819 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1820}
Mike Turquetteb24764902012-03-15 23:11:19 -07001821EXPORT_SYMBOL_GPL(clk_set_parent);
1822
1823/**
Mike Turquettee59c5372014-02-18 21:21:25 -08001824 * clk_set_phase - adjust the phase shift of a clock signal
1825 * @clk: clock signal source
1826 * @degrees: number of degrees the signal is shifted
1827 *
1828 * Shifts the phase of a clock signal by the specified
1829 * degrees. Returns 0 on success, -EERROR otherwise.
1830 *
1831 * This function makes no distinction about the input or reference
1832 * signal that we adjust the clock signal phase against. For example
1833 * phase locked-loop clock signal generators we may shift phase with
1834 * respect to feedback clock signal input, but for other cases the
1835 * clock phase may be shifted with respect to some other, unspecified
1836 * signal.
1837 *
1838 * Additionally the concept of phase shift does not propagate through
1839 * the clock tree hierarchy, which sets it apart from clock rates and
1840 * clock accuracy. A parent clock phase attribute does not have an
1841 * impact on the phase attribute of a child clock.
1842 */
1843int clk_set_phase(struct clk *clk, int degrees)
1844{
Stephen Boyd08b95752015-02-02 14:09:43 -08001845 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08001846
1847 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08001848 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08001849
1850 /* sanity check degrees */
1851 degrees %= 360;
1852 if (degrees < 0)
1853 degrees += 360;
1854
1855 clk_prepare_lock();
1856
Stephen Boyddfc202e2015-02-02 14:37:41 -08001857 trace_clk_set_phase(clk->core, degrees);
1858
Stephen Boyd08b95752015-02-02 14:09:43 -08001859 if (clk->core->ops->set_phase)
1860 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08001861
Stephen Boyddfc202e2015-02-02 14:37:41 -08001862 trace_clk_set_phase_complete(clk->core, degrees);
1863
Mike Turquettee59c5372014-02-18 21:21:25 -08001864 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001865 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08001866
Mike Turquettee59c5372014-02-18 21:21:25 -08001867 clk_prepare_unlock();
1868
Mike Turquettee59c5372014-02-18 21:21:25 -08001869 return ret;
1870}
Maxime Ripard9767b042015-01-20 22:23:43 +01001871EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001872
Stephen Boydd6968fc2015-04-30 13:54:13 -07001873static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08001874{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07001875 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08001876
1877 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001878 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08001879 clk_prepare_unlock();
1880
Mike Turquettee59c5372014-02-18 21:21:25 -08001881 return ret;
1882}
1883
1884/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001885 * clk_get_phase - return the phase shift of a clock signal
1886 * @clk: clock signal source
1887 *
1888 * Returns the phase shift of a clock node in degrees, otherwise returns
1889 * -EERROR.
1890 */
1891int clk_get_phase(struct clk *clk)
1892{
1893 if (!clk)
1894 return 0;
1895
1896 return clk_core_get_phase(clk->core);
1897}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001898EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07001899
1900/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08001901 * clk_is_match - check if two clk's point to the same hardware clock
1902 * @p: clk compared against q
1903 * @q: clk compared against p
1904 *
1905 * Returns true if the two struct clk pointers both point to the same hardware
1906 * clock node. Put differently, returns true if struct clk *p and struct clk *q
1907 * share the same struct clk_core object.
1908 *
1909 * Returns false otherwise. Note that two NULL clks are treated as matching.
1910 */
1911bool clk_is_match(const struct clk *p, const struct clk *q)
1912{
1913 /* trivial case: identical struct clk's or both NULL */
1914 if (p == q)
1915 return true;
1916
1917 /* true if clk->core pointers match. Avoid derefing garbage */
1918 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
1919 if (p->core == q->core)
1920 return true;
1921
1922 return false;
1923}
1924EXPORT_SYMBOL_GPL(clk_is_match);
1925
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001926/*** debugfs support ***/
1927
1928#ifdef CONFIG_DEBUG_FS
1929#include <linux/debugfs.h>
1930
1931static struct dentry *rootdir;
1932static int inited = 0;
1933static DEFINE_MUTEX(clk_debug_lock);
1934static HLIST_HEAD(clk_debug_list);
1935
1936static struct hlist_head *all_lists[] = {
1937 &clk_root_list,
1938 &clk_orphan_list,
1939 NULL,
1940};
1941
1942static struct hlist_head *orphan_list[] = {
1943 &clk_orphan_list,
1944 NULL,
1945};
1946
1947static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
1948 int level)
1949{
1950 if (!c)
1951 return;
1952
1953 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
1954 level * 3 + 1, "",
1955 30 - level * 3, c->name,
1956 c->enable_count, c->prepare_count, clk_core_get_rate(c),
1957 clk_core_get_accuracy(c), clk_core_get_phase(c));
1958}
1959
1960static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
1961 int level)
1962{
1963 struct clk_core *child;
1964
1965 if (!c)
1966 return;
1967
1968 clk_summary_show_one(s, c, level);
1969
1970 hlist_for_each_entry(child, &c->children, child_node)
1971 clk_summary_show_subtree(s, child, level + 1);
1972}
1973
1974static int clk_summary_show(struct seq_file *s, void *data)
1975{
1976 struct clk_core *c;
1977 struct hlist_head **lists = (struct hlist_head **)s->private;
1978
1979 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
1980 seq_puts(s, "----------------------------------------------------------------------------------------\n");
1981
1982 clk_prepare_lock();
1983
1984 for (; *lists; lists++)
1985 hlist_for_each_entry(c, *lists, child_node)
1986 clk_summary_show_subtree(s, c, 0);
1987
1988 clk_prepare_unlock();
1989
1990 return 0;
1991}
1992
1993
1994static int clk_summary_open(struct inode *inode, struct file *file)
1995{
1996 return single_open(file, clk_summary_show, inode->i_private);
1997}
1998
1999static const struct file_operations clk_summary_fops = {
2000 .open = clk_summary_open,
2001 .read = seq_read,
2002 .llseek = seq_lseek,
2003 .release = single_release,
2004};
2005
2006static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2007{
2008 if (!c)
2009 return;
2010
Stefan Wahren7cb81132015-04-29 16:36:43 +00002011 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002012 seq_printf(s, "\"%s\": { ", c->name);
2013 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2014 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002015 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2016 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002017 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2018}
2019
2020static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2021{
2022 struct clk_core *child;
2023
2024 if (!c)
2025 return;
2026
2027 clk_dump_one(s, c, level);
2028
2029 hlist_for_each_entry(child, &c->children, child_node) {
2030 seq_printf(s, ",");
2031 clk_dump_subtree(s, child, level + 1);
2032 }
2033
2034 seq_printf(s, "}");
2035}
2036
2037static int clk_dump(struct seq_file *s, void *data)
2038{
2039 struct clk_core *c;
2040 bool first_node = true;
2041 struct hlist_head **lists = (struct hlist_head **)s->private;
2042
2043 seq_printf(s, "{");
2044
2045 clk_prepare_lock();
2046
2047 for (; *lists; lists++) {
2048 hlist_for_each_entry(c, *lists, child_node) {
2049 if (!first_node)
2050 seq_puts(s, ",");
2051 first_node = false;
2052 clk_dump_subtree(s, c, 0);
2053 }
2054 }
2055
2056 clk_prepare_unlock();
2057
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002058 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002059 return 0;
2060}
2061
2062
2063static int clk_dump_open(struct inode *inode, struct file *file)
2064{
2065 return single_open(file, clk_dump, inode->i_private);
2066}
2067
2068static const struct file_operations clk_dump_fops = {
2069 .open = clk_dump_open,
2070 .read = seq_read,
2071 .llseek = seq_lseek,
2072 .release = single_release,
2073};
2074
2075static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2076{
2077 struct dentry *d;
2078 int ret = -ENOMEM;
2079
2080 if (!core || !pdentry) {
2081 ret = -EINVAL;
2082 goto out;
2083 }
2084
2085 d = debugfs_create_dir(core->name, pdentry);
2086 if (!d)
2087 goto out;
2088
2089 core->dentry = d;
2090
2091 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2092 (u32 *)&core->rate);
2093 if (!d)
2094 goto err_out;
2095
2096 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2097 (u32 *)&core->accuracy);
2098 if (!d)
2099 goto err_out;
2100
2101 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2102 (u32 *)&core->phase);
2103 if (!d)
2104 goto err_out;
2105
2106 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2107 (u32 *)&core->flags);
2108 if (!d)
2109 goto err_out;
2110
2111 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2112 (u32 *)&core->prepare_count);
2113 if (!d)
2114 goto err_out;
2115
2116 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2117 (u32 *)&core->enable_count);
2118 if (!d)
2119 goto err_out;
2120
2121 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2122 (u32 *)&core->notifier_count);
2123 if (!d)
2124 goto err_out;
2125
2126 if (core->ops->debug_init) {
2127 ret = core->ops->debug_init(core->hw, core->dentry);
2128 if (ret)
2129 goto err_out;
2130 }
2131
2132 ret = 0;
2133 goto out;
2134
2135err_out:
2136 debugfs_remove_recursive(core->dentry);
2137 core->dentry = NULL;
2138out:
2139 return ret;
2140}
2141
2142/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002143 * clk_debug_register - add a clk node to the debugfs clk directory
2144 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002145 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002146 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2147 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002148 * will be created lazily by clk_debug_init as part of a late_initcall.
2149 */
2150static int clk_debug_register(struct clk_core *core)
2151{
2152 int ret = 0;
2153
2154 mutex_lock(&clk_debug_lock);
2155 hlist_add_head(&core->debug_node, &clk_debug_list);
2156
2157 if (!inited)
2158 goto unlock;
2159
2160 ret = clk_debug_create_one(core, rootdir);
2161unlock:
2162 mutex_unlock(&clk_debug_lock);
2163
2164 return ret;
2165}
2166
2167 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002168 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2169 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002170 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002171 * Dynamically removes a clk and all its child nodes from the
2172 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002173 * clk_debug_register in __clk_init.
2174 */
2175static void clk_debug_unregister(struct clk_core *core)
2176{
2177 mutex_lock(&clk_debug_lock);
2178 hlist_del_init(&core->debug_node);
2179 debugfs_remove_recursive(core->dentry);
2180 core->dentry = NULL;
2181 mutex_unlock(&clk_debug_lock);
2182}
2183
2184struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2185 void *data, const struct file_operations *fops)
2186{
2187 struct dentry *d = NULL;
2188
2189 if (hw->core->dentry)
2190 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2191 fops);
2192
2193 return d;
2194}
2195EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2196
2197/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002198 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002199 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002200 * clks are often initialized very early during boot before memory can be
2201 * dynamically allocated and well before debugfs is setup. This function
2202 * populates the debugfs clk directory once at boot-time when we know that
2203 * debugfs is setup. It should only be called once at boot-time, all other clks
2204 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002205 */
2206static int __init clk_debug_init(void)
2207{
2208 struct clk_core *core;
2209 struct dentry *d;
2210
2211 rootdir = debugfs_create_dir("clk", NULL);
2212
2213 if (!rootdir)
2214 return -ENOMEM;
2215
2216 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2217 &clk_summary_fops);
2218 if (!d)
2219 return -ENOMEM;
2220
2221 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2222 &clk_dump_fops);
2223 if (!d)
2224 return -ENOMEM;
2225
2226 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2227 &orphan_list, &clk_summary_fops);
2228 if (!d)
2229 return -ENOMEM;
2230
2231 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2232 &orphan_list, &clk_dump_fops);
2233 if (!d)
2234 return -ENOMEM;
2235
2236 mutex_lock(&clk_debug_lock);
2237 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2238 clk_debug_create_one(core, rootdir);
2239
2240 inited = 1;
2241 mutex_unlock(&clk_debug_lock);
2242
2243 return 0;
2244}
2245late_initcall(clk_debug_init);
2246#else
2247static inline int clk_debug_register(struct clk_core *core) { return 0; }
2248static inline void clk_debug_reparent(struct clk_core *core,
2249 struct clk_core *new_parent)
2250{
2251}
2252static inline void clk_debug_unregister(struct clk_core *core)
2253{
2254}
2255#endif
2256
Michael Turquette3d3801e2015-02-25 09:11:01 -08002257/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002258 * __clk_init - initialize the data structures in a struct clk
2259 * @dev: device initializing this clk, placeholder for now
2260 * @clk: clk being initialized
2261 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002262 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002263 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002264 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002265static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002266{
Mike Turquetted1302a32012-03-29 14:30:40 -07002267 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002268 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002269 struct hlist_node *tmp2;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002270 struct clk_core *core;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002271 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002272
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002273 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002274 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002275
Stephen Boydd6968fc2015-04-30 13:54:13 -07002276 core = clk_user->core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002277
Mike Turquetteeab89f62013-03-28 13:59:01 -07002278 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002279
2280 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002281 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002282 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002283 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002284 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002285 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002286 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002287
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002288 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002289 if (core->ops->set_rate &&
2290 !((core->ops->round_rate || core->ops->determine_rate) &&
2291 core->ops->recalc_rate)) {
James Hogan71472c02013-07-29 12:25:00 +01002292 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002293 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002294 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002295 goto out;
2296 }
2297
Stephen Boydd6968fc2015-04-30 13:54:13 -07002298 if (core->ops->set_parent && !core->ops->get_parent) {
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002299 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002300 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002301 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002302 goto out;
2303 }
2304
Stephen Boydd6968fc2015-04-30 13:54:13 -07002305 if (core->ops->set_rate_and_parent &&
2306 !(core->ops->set_parent && core->ops->set_rate)) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002307 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002308 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002309 ret = -EINVAL;
2310 goto out;
2311 }
2312
Mike Turquetteb24764902012-03-15 23:11:19 -07002313 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002314 for (i = 0; i < core->num_parents; i++)
2315 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002316 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002317 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002318
2319 /*
2320 * Allocate an array of struct clk *'s to avoid unnecessary string
2321 * look-ups of clk's possible parents. This can fail for clocks passed
Stephen Boydd6968fc2015-04-30 13:54:13 -07002322 * in to clk_init during early boot; thus any access to core->parents[]
Mike Turquetteb24764902012-03-15 23:11:19 -07002323 * must always check for a NULL pointer and try to populate it if
2324 * necessary.
2325 *
Stephen Boydd6968fc2015-04-30 13:54:13 -07002326 * If core->parents is not NULL we skip this entire block. This allows
2327 * for clock drivers to statically initialize core->parents.
Mike Turquetteb24764902012-03-15 23:11:19 -07002328 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002329 if (core->num_parents > 1 && !core->parents) {
2330 core->parents = kcalloc(core->num_parents, sizeof(struct clk *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002331 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002332 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002333 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002334 * clk_init'd; thus any access to clk->parents[] must check
2335 * for a NULL pointer. We can always perform lazy lookups for
2336 * missing parents later on.
2337 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002338 if (core->parents)
2339 for (i = 0; i < core->num_parents; i++)
2340 core->parents[i] =
2341 clk_core_lookup(core->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002342 }
2343
Stephen Boydd6968fc2015-04-30 13:54:13 -07002344 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002345
2346 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07002347 * Populate core->parent if parent has already been __clk_init'd. If
Mike Turquetteb24764902012-03-15 23:11:19 -07002348 * parent has not yet been __clk_init'd then place clk in the orphan
2349 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2350 * clk list.
2351 *
2352 * Every time a new clk is clk_init'd then we walk the list of orphan
2353 * clocks and re-parent any that are children of the clock currently
2354 * being clk_init'd.
2355 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002356 if (core->parent)
2357 hlist_add_head(&core->child_node,
2358 &core->parent->children);
2359 else if (core->flags & CLK_IS_ROOT)
2360 hlist_add_head(&core->child_node, &clk_root_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002361 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002362 hlist_add_head(&core->child_node, &clk_orphan_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002363
2364 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002365 * Set clk's accuracy. The preferred method is to use
2366 * .recalc_accuracy. For simple clocks and lazy developers the default
2367 * fallback is to use the parent's accuracy. If a clock doesn't have a
2368 * parent (or is orphaned) then accuracy is set to zero (perfect
2369 * clock).
2370 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002371 if (core->ops->recalc_accuracy)
2372 core->accuracy = core->ops->recalc_accuracy(core->hw,
2373 __clk_get_accuracy(core->parent));
2374 else if (core->parent)
2375 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002376 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002377 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002378
2379 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002380 * Set clk's phase.
2381 * Since a phase is by definition relative to its parent, just
2382 * query the current clock phase, or just assume it's in phase.
2383 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002384 if (core->ops->get_phase)
2385 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002386 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002387 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002388
2389 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002390 * Set clk's rate. The preferred method is to use .recalc_rate. For
2391 * simple clocks and lazy developers the default fallback is to use the
2392 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2393 * then rate is set to zero.
2394 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002395 if (core->ops->recalc_rate)
2396 rate = core->ops->recalc_rate(core->hw,
2397 clk_core_get_rate_nolock(core->parent));
2398 else if (core->parent)
2399 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002400 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002401 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002402 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002403
2404 /*
2405 * walk the list of orphan clocks and reparent any that are children of
2406 * this clock
2407 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002408 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002409 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002410 i = orphan->ops->get_parent(orphan->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002411 if (!strcmp(core->name, orphan->parent_names[i]))
2412 clk_core_reparent(orphan, core);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002413 continue;
2414 }
2415
Mike Turquetteb24764902012-03-15 23:11:19 -07002416 for (i = 0; i < orphan->num_parents; i++)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002417 if (!strcmp(core->name, orphan->parent_names[i])) {
2418 clk_core_reparent(orphan, core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002419 break;
2420 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002421 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002422
2423 /*
2424 * optional platform-specific magic
2425 *
2426 * The .init callback is not used by any of the basic clock types, but
2427 * exists for weird hardware that must perform initialization magic.
2428 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002429 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002430 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002431 if (core->ops->init)
2432 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002433
Stephen Boydd6968fc2015-04-30 13:54:13 -07002434 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002435out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002436 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002437
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002438 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002439 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002440
Mike Turquetted1302a32012-03-29 14:30:40 -07002441 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002442}
2443
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002444struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2445 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002446{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002447 struct clk *clk;
2448
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002449 /* This is to allow this function to be chained to others */
2450 if (!hw || IS_ERR(hw))
2451 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002452
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002453 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2454 if (!clk)
2455 return ERR_PTR(-ENOMEM);
2456
2457 clk->core = hw->core;
2458 clk->dev_id = dev_id;
2459 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002460 clk->max_rate = ULONG_MAX;
2461
2462 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002463 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002464 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002465
2466 return clk;
2467}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002468
Stephen Boyd73e0e492015-02-06 11:42:43 -08002469void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002470{
2471 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002472 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002473 clk_prepare_unlock();
2474
2475 kfree(clk);
2476}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002477
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002478/**
2479 * clk_register - allocate a new clock, register it and return an opaque cookie
2480 * @dev: device that is registering this clock
2481 * @hw: link to hardware-specific clock data
2482 *
2483 * clk_register is the primary interface for populating the clock tree with new
2484 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05302485 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002486 * rest of the clock API. In the event of an error clk_register will return an
2487 * error code; drivers must test for an error code after calling clk_register.
2488 */
2489struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002490{
Mike Turquetted1302a32012-03-29 14:30:40 -07002491 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002492 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002493
Stephen Boydd6968fc2015-04-30 13:54:13 -07002494 core = kzalloc(sizeof(*core), GFP_KERNEL);
2495 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002496 ret = -ENOMEM;
2497 goto fail_out;
2498 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002499
Stephen Boydd6968fc2015-04-30 13:54:13 -07002500 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2501 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002502 ret = -ENOMEM;
2503 goto fail_name;
2504 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002505 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002506 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002507 core->owner = dev->driver->owner;
2508 core->hw = hw;
2509 core->flags = hw->init->flags;
2510 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07002511 core->min_rate = 0;
2512 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002513 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002514
Mike Turquetted1302a32012-03-29 14:30:40 -07002515 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002516 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002517 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002518
Stephen Boydd6968fc2015-04-30 13:54:13 -07002519 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002520 ret = -ENOMEM;
2521 goto fail_parent_names;
2522 }
2523
2524
2525 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002526 for (i = 0; i < core->num_parents; i++) {
2527 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002528 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002529 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002530 ret = -ENOMEM;
2531 goto fail_parent_names_copy;
2532 }
2533 }
2534
Stephen Boydd6968fc2015-04-30 13:54:13 -07002535 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002536
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002537 hw->clk = __clk_create_clk(hw, NULL, NULL);
2538 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002539 ret = PTR_ERR(hw->clk);
2540 goto fail_parent_names_copy;
2541 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002542
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002543 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002544 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002545 return hw->clk;
2546
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002547 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002548 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002549
2550fail_parent_names_copy:
2551 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002552 kfree_const(core->parent_names[i]);
2553 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07002554fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002555 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002556fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002557 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002558fail_out:
2559 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002560}
2561EXPORT_SYMBOL_GPL(clk_register);
2562
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002563/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002564static void __clk_release(struct kref *ref)
2565{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002566 struct clk_core *core = container_of(ref, struct clk_core, ref);
2567 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002568
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002569 lockdep_assert_held(&prepare_lock);
2570
Stephen Boydd6968fc2015-04-30 13:54:13 -07002571 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002572 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002573 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002574
Stephen Boydd6968fc2015-04-30 13:54:13 -07002575 kfree(core->parent_names);
2576 kfree_const(core->name);
2577 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002578}
2579
2580/*
2581 * Empty clk_ops for unregistered clocks. These are used temporarily
2582 * after clk_unregister() was called on a clock and until last clock
2583 * consumer calls clk_put() and the struct clk object is freed.
2584 */
2585static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2586{
2587 return -ENXIO;
2588}
2589
2590static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2591{
2592 WARN_ON_ONCE(1);
2593}
2594
2595static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2596 unsigned long parent_rate)
2597{
2598 return -ENXIO;
2599}
2600
2601static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2602{
2603 return -ENXIO;
2604}
2605
2606static const struct clk_ops clk_nodrv_ops = {
2607 .enable = clk_nodrv_prepare_enable,
2608 .disable = clk_nodrv_disable_unprepare,
2609 .prepare = clk_nodrv_prepare_enable,
2610 .unprepare = clk_nodrv_disable_unprepare,
2611 .set_rate = clk_nodrv_set_rate,
2612 .set_parent = clk_nodrv_set_parent,
2613};
2614
Mark Brown1df5c932012-04-18 09:07:12 +01002615/**
2616 * clk_unregister - unregister a currently registered clock
2617 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002618 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002619void clk_unregister(struct clk *clk)
2620{
2621 unsigned long flags;
2622
Stephen Boyd6314b672014-09-04 23:37:49 -07002623 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2624 return;
2625
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002626 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002627
2628 clk_prepare_lock();
2629
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002630 if (clk->core->ops == &clk_nodrv_ops) {
2631 pr_err("%s: unregistered clock: %s\n", __func__,
2632 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002633 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002634 }
2635 /*
2636 * Assign empty clock ops for consumers that might still hold
2637 * a reference to this clock.
2638 */
2639 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002640 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002641 clk_enable_unlock(flags);
2642
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002643 if (!hlist_empty(&clk->core->children)) {
2644 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002645 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002646
2647 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002648 hlist_for_each_entry_safe(child, t, &clk->core->children,
2649 child_node)
2650 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002651 }
2652
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002653 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002654
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002655 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002656 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002657 __func__, clk->core->name);
2658 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002659
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002660 clk_prepare_unlock();
2661}
Mark Brown1df5c932012-04-18 09:07:12 +01002662EXPORT_SYMBOL_GPL(clk_unregister);
2663
Stephen Boyd46c87732012-09-24 13:38:04 -07002664static void devm_clk_release(struct device *dev, void *res)
2665{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002666 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002667}
2668
2669/**
2670 * devm_clk_register - resource managed clk_register()
2671 * @dev: device that is registering this clock
2672 * @hw: link to hardware-specific clock data
2673 *
2674 * Managed clk_register(). Clocks returned from this function are
2675 * automatically clk_unregister()ed on driver detach. See clk_register() for
2676 * more information.
2677 */
2678struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2679{
2680 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002681 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002682
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002683 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2684 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002685 return ERR_PTR(-ENOMEM);
2686
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002687 clk = clk_register(dev, hw);
2688 if (!IS_ERR(clk)) {
2689 *clkp = clk;
2690 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002691 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002692 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002693 }
2694
2695 return clk;
2696}
2697EXPORT_SYMBOL_GPL(devm_clk_register);
2698
2699static int devm_clk_match(struct device *dev, void *res, void *data)
2700{
2701 struct clk *c = res;
2702 if (WARN_ON(!c))
2703 return 0;
2704 return c == data;
2705}
2706
2707/**
2708 * devm_clk_unregister - resource managed clk_unregister()
2709 * @clk: clock to unregister
2710 *
2711 * Deallocate a clock allocated with devm_clk_register(). Normally
2712 * this function will not need to be called and the resource management
2713 * code will ensure that the resource is freed.
2714 */
2715void devm_clk_unregister(struct device *dev, struct clk *clk)
2716{
2717 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2718}
2719EXPORT_SYMBOL_GPL(devm_clk_unregister);
2720
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002721/*
2722 * clkdev helpers
2723 */
2724int __clk_get(struct clk *clk)
2725{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002726 struct clk_core *core = !clk ? NULL : clk->core;
2727
2728 if (core) {
2729 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002730 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002731
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002732 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002733 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002734 return 1;
2735}
2736
2737void __clk_put(struct clk *clk)
2738{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002739 struct module *owner;
2740
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002741 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002742 return;
2743
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002744 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002745
Stephen Boyd50595f82015-02-06 11:42:44 -08002746 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002747 if (clk->min_rate > clk->core->req_rate ||
2748 clk->max_rate < clk->core->req_rate)
2749 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2750
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002751 owner = clk->core->owner;
2752 kref_put(&clk->core->ref, __clk_release);
2753
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002754 clk_prepare_unlock();
2755
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002756 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002757
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002758 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002759}
2760
Mike Turquetteb24764902012-03-15 23:11:19 -07002761/*** clk rate change notifiers ***/
2762
2763/**
2764 * clk_notifier_register - add a clk rate change notifier
2765 * @clk: struct clk * to watch
2766 * @nb: struct notifier_block * with callback info
2767 *
2768 * Request notification when clk's rate changes. This uses an SRCU
2769 * notifier because we want it to block and notifier unregistrations are
2770 * uncommon. The callbacks associated with the notifier must not
2771 * re-enter into the clk framework by calling any top-level clk APIs;
2772 * this will cause a nested prepare_lock mutex.
2773 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002774 * In all notification cases cases (pre, post and abort rate change) the
2775 * original clock rate is passed to the callback via struct
2776 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002777 * clk_notifier_data.new_rate.
2778 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002779 * clk_notifier_register() must be called from non-atomic context.
2780 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2781 * allocation failure; otherwise, passes along the return value of
2782 * srcu_notifier_chain_register().
2783 */
2784int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2785{
2786 struct clk_notifier *cn;
2787 int ret = -ENOMEM;
2788
2789 if (!clk || !nb)
2790 return -EINVAL;
2791
Mike Turquetteeab89f62013-03-28 13:59:01 -07002792 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002793
2794 /* search the list of notifiers for this clk */
2795 list_for_each_entry(cn, &clk_notifier_list, node)
2796 if (cn->clk == clk)
2797 break;
2798
2799 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2800 if (cn->clk != clk) {
2801 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2802 if (!cn)
2803 goto out;
2804
2805 cn->clk = clk;
2806 srcu_init_notifier_head(&cn->notifier_head);
2807
2808 list_add(&cn->node, &clk_notifier_list);
2809 }
2810
2811 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2812
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002813 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002814
2815out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002816 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002817
2818 return ret;
2819}
2820EXPORT_SYMBOL_GPL(clk_notifier_register);
2821
2822/**
2823 * clk_notifier_unregister - remove a clk rate change notifier
2824 * @clk: struct clk *
2825 * @nb: struct notifier_block * with callback info
2826 *
2827 * Request no further notification for changes to 'clk' and frees memory
2828 * allocated in clk_notifier_register.
2829 *
2830 * Returns -EINVAL if called with null arguments; otherwise, passes
2831 * along the return value of srcu_notifier_chain_unregister().
2832 */
2833int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2834{
2835 struct clk_notifier *cn = NULL;
2836 int ret = -EINVAL;
2837
2838 if (!clk || !nb)
2839 return -EINVAL;
2840
Mike Turquetteeab89f62013-03-28 13:59:01 -07002841 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002842
2843 list_for_each_entry(cn, &clk_notifier_list, node)
2844 if (cn->clk == clk)
2845 break;
2846
2847 if (cn->clk == clk) {
2848 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2849
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002850 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002851
2852 /* XXX the notifier code should handle this better */
2853 if (!cn->notifier_head.head) {
2854 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002855 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002856 kfree(cn);
2857 }
2858
2859 } else {
2860 ret = -ENOENT;
2861 }
2862
Mike Turquetteeab89f62013-03-28 13:59:01 -07002863 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002864
2865 return ret;
2866}
2867EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002868
2869#ifdef CONFIG_OF
2870/**
2871 * struct of_clk_provider - Clock provider registration structure
2872 * @link: Entry in global list of clock providers
2873 * @node: Pointer to device tree node of clock provider
2874 * @get: Get clock callback. Returns NULL or a struct clk for the
2875 * given clock specifier
2876 * @data: context pointer to be passed into @get callback
2877 */
2878struct of_clk_provider {
2879 struct list_head link;
2880
2881 struct device_node *node;
2882 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2883 void *data;
2884};
2885
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302886static const struct of_device_id __clk_of_table_sentinel
2887 __used __section(__clk_of_table_end);
2888
Grant Likely766e6a42012-04-09 14:50:06 -05002889static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002890static DEFINE_MUTEX(of_clk_mutex);
2891
Grant Likely766e6a42012-04-09 14:50:06 -05002892struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2893 void *data)
2894{
2895 return data;
2896}
2897EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2898
Shawn Guo494bfec2012-08-22 21:36:27 +08002899struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2900{
2901 struct clk_onecell_data *clk_data = data;
2902 unsigned int idx = clkspec->args[0];
2903
2904 if (idx >= clk_data->clk_num) {
2905 pr_err("%s: invalid clock index %d\n", __func__, idx);
2906 return ERR_PTR(-EINVAL);
2907 }
2908
2909 return clk_data->clks[idx];
2910}
2911EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2912
Grant Likely766e6a42012-04-09 14:50:06 -05002913/**
2914 * of_clk_add_provider() - Register a clock provider for a node
2915 * @np: Device node pointer associated with clock provider
2916 * @clk_src_get: callback for decoding clock
2917 * @data: context pointer for @clk_src_get callback.
2918 */
2919int of_clk_add_provider(struct device_node *np,
2920 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2921 void *data),
2922 void *data)
2923{
2924 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002925 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002926
2927 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2928 if (!cp)
2929 return -ENOMEM;
2930
2931 cp->node = of_node_get(np);
2932 cp->data = data;
2933 cp->get = clk_src_get;
2934
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002935 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002936 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002937 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002938 pr_debug("Added clock from %s\n", np->full_name);
2939
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002940 ret = of_clk_set_defaults(np, true);
2941 if (ret < 0)
2942 of_clk_del_provider(np);
2943
2944 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002945}
2946EXPORT_SYMBOL_GPL(of_clk_add_provider);
2947
2948/**
2949 * of_clk_del_provider() - Remove a previously registered clock provider
2950 * @np: Device node pointer associated with clock provider
2951 */
2952void of_clk_del_provider(struct device_node *np)
2953{
2954 struct of_clk_provider *cp;
2955
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002956 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002957 list_for_each_entry(cp, &of_clk_providers, link) {
2958 if (cp->node == np) {
2959 list_del(&cp->link);
2960 of_node_put(cp->node);
2961 kfree(cp);
2962 break;
2963 }
2964 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002965 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002966}
2967EXPORT_SYMBOL_GPL(of_clk_del_provider);
2968
Stephen Boyd73e0e492015-02-06 11:42:43 -08002969struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
2970 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05002971{
2972 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002973 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002974
Stephen Boyd306c3422015-02-05 15:39:11 -08002975 if (!clkspec)
2976 return ERR_PTR(-EINVAL);
2977
Grant Likely766e6a42012-04-09 14:50:06 -05002978 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08002979 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002980 list_for_each_entry(provider, &of_clk_providers, link) {
2981 if (provider->node == clkspec->np)
2982 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002983 if (!IS_ERR(clk)) {
2984 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
2985 con_id);
2986
2987 if (!IS_ERR(clk) && !__clk_get(clk)) {
2988 __clk_free_clk(clk);
2989 clk = ERR_PTR(-ENOENT);
2990 }
2991
Grant Likely766e6a42012-04-09 14:50:06 -05002992 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08002993 }
Grant Likely766e6a42012-04-09 14:50:06 -05002994 }
Stephen Boyd306c3422015-02-05 15:39:11 -08002995 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002996
2997 return clk;
2998}
2999
Stephen Boyd306c3422015-02-05 15:39:11 -08003000/**
3001 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3002 * @clkspec: pointer to a clock specifier data structure
3003 *
3004 * This function looks up a struct clk from the registered list of clock
3005 * providers, an input is a clock specifier data structure as returned
3006 * from the of_parse_phandle_with_args() function call.
3007 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003008struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3009{
Stephen Boyd306c3422015-02-05 15:39:11 -08003010 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05003011}
3012
Mike Turquettef6102742013-10-07 23:12:13 -07003013int of_clk_get_parent_count(struct device_node *np)
3014{
3015 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
3016}
3017EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3018
Grant Likely766e6a42012-04-09 14:50:06 -05003019const char *of_clk_get_parent_name(struct device_node *np, int index)
3020{
3021 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003022 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003023 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003024 const __be32 *vp;
3025 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003026 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003027 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05003028
3029 if (index < 0)
3030 return NULL;
3031
3032 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3033 &clkspec);
3034 if (rc)
3035 return NULL;
3036
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003037 index = clkspec.args_count ? clkspec.args[0] : 0;
3038 count = 0;
3039
3040 /* if there is an indices property, use it to transfer the index
3041 * specified into an array offset for the clock-output-names property.
3042 */
3043 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3044 if (index == pv) {
3045 index = count;
3046 break;
3047 }
3048 count++;
3049 }
3050
Grant Likely766e6a42012-04-09 14:50:06 -05003051 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003052 index,
Grant Likely766e6a42012-04-09 14:50:06 -05003053 &clk_name) < 0)
3054 clk_name = clkspec.np->name;
3055
3056 of_node_put(clkspec.np);
3057 return clk_name;
3058}
3059EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3060
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05003061/**
3062 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3063 * number of parents
3064 * @np: Device node pointer associated with clock provider
3065 * @parents: pointer to char array that hold the parents' names
3066 * @size: size of the @parents array
3067 *
3068 * Return: number of parents for the clock node.
3069 */
3070int of_clk_parent_fill(struct device_node *np, const char **parents,
3071 unsigned int size)
3072{
3073 unsigned int i = 0;
3074
3075 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3076 i++;
3077
3078 return i;
3079}
3080EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3081
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003082struct clock_provider {
3083 of_clk_init_cb_t clk_init_cb;
3084 struct device_node *np;
3085 struct list_head node;
3086};
3087
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003088/*
3089 * This function looks for a parent clock. If there is one, then it
3090 * checks that the provider for this parent clock was initialized, in
3091 * this case the parent clock will be ready.
3092 */
3093static int parent_ready(struct device_node *np)
3094{
3095 int i = 0;
3096
3097 while (true) {
3098 struct clk *clk = of_clk_get(np, i);
3099
3100 /* this parent is ready we can check the next one */
3101 if (!IS_ERR(clk)) {
3102 clk_put(clk);
3103 i++;
3104 continue;
3105 }
3106
3107 /* at least one parent is not ready, we exit now */
3108 if (PTR_ERR(clk) == -EPROBE_DEFER)
3109 return 0;
3110
3111 /*
3112 * Here we make assumption that the device tree is
3113 * written correctly. So an error means that there is
3114 * no more parent. As we didn't exit yet, then the
3115 * previous parent are ready. If there is no clock
3116 * parent, no need to wait for them, then we can
3117 * consider their absence as being ready
3118 */
3119 return 1;
3120 }
3121}
3122
Grant Likely766e6a42012-04-09 14:50:06 -05003123/**
3124 * of_clk_init() - Scan and init clock providers from the DT
3125 * @matches: array of compatible values and init functions for providers.
3126 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003127 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003128 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003129 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003130 */
3131void __init of_clk_init(const struct of_device_id *matches)
3132{
Alex Elder7f7ed582013-08-22 11:31:31 -05003133 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003134 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003135 struct clock_provider *clk_provider, *next;
3136 bool is_init_done;
3137 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07003138 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05003139
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303140 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003141 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303142
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003143 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003144 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003145 struct clock_provider *parent;
3146
3147 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
3148 if (!parent) {
3149 list_for_each_entry_safe(clk_provider, next,
3150 &clk_provider_list, node) {
3151 list_del(&clk_provider->node);
3152 kfree(clk_provider);
3153 }
3154 return;
3155 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003156
3157 parent->clk_init_cb = match->data;
3158 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003159 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003160 }
3161
3162 while (!list_empty(&clk_provider_list)) {
3163 is_init_done = false;
3164 list_for_each_entry_safe(clk_provider, next,
3165 &clk_provider_list, node) {
3166 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003167
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003168 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003169 of_clk_set_defaults(clk_provider->np, true);
3170
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003171 list_del(&clk_provider->node);
3172 kfree(clk_provider);
3173 is_init_done = true;
3174 }
3175 }
3176
3177 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003178 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003179 * remaining providers during the last loop, so now we
3180 * initialize all the remaining ones unconditionally
3181 * in case the clock parent was not mandatory
3182 */
3183 if (!is_init_done)
3184 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003185 }
3186}
3187#endif