blob: a30fd30f49489b2d3cda468ff3205d891615401b [file] [log] [blame]
Mike Turquetteb24764902012-03-15 23:11:19 -07001/*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Standard functionality for the common clock API. See Documentation/clk.txt
10 */
11
Stephen Boyd3c373112015-06-19 15:00:46 -070012#include <linux/clk.h>
Michael Turquetteb09d6d92015-01-29 14:22:50 -080013#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020014#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070015#include <linux/module.h>
16#include <linux/mutex.h>
17#include <linux/spinlock.h>
18#include <linux/err.h>
19#include <linux/list.h>
20#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050021#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070022#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053023#include <linux/init.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070024#include <linux/sched.h>
Stephen Boyd562ef0b2015-05-01 12:16:14 -070025#include <linux/clkdev.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070026
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020027#include "clk.h"
28
Mike Turquetteb24764902012-03-15 23:11:19 -070029static DEFINE_SPINLOCK(enable_lock);
30static DEFINE_MUTEX(prepare_lock);
31
Mike Turquette533ddeb2013-03-28 13:59:02 -070032static struct task_struct *prepare_owner;
33static struct task_struct *enable_owner;
34
35static int prepare_refcnt;
36static int enable_refcnt;
37
Mike Turquetteb24764902012-03-15 23:11:19 -070038static HLIST_HEAD(clk_root_list);
39static HLIST_HEAD(clk_orphan_list);
40static LIST_HEAD(clk_notifier_list);
41
Michael Turquetteb09d6d92015-01-29 14:22:50 -080042/*** private data structures ***/
43
44struct clk_core {
45 const char *name;
46 const struct clk_ops *ops;
47 struct clk_hw *hw;
48 struct module *owner;
49 struct clk_core *parent;
50 const char **parent_names;
51 struct clk_core **parents;
52 u8 num_parents;
53 u8 new_parent_index;
54 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010055 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080056 unsigned long new_rate;
57 struct clk_core *new_parent;
58 struct clk_core *new_child;
59 unsigned long flags;
Heiko Stuebnere6500342015-04-22 22:53:05 +020060 bool orphan;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080061 unsigned int enable_count;
62 unsigned int prepare_count;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070063 unsigned long min_rate;
64 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080065 unsigned long accuracy;
66 int phase;
67 struct hlist_head children;
68 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010069 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080070 unsigned int notifier_count;
71#ifdef CONFIG_DEBUG_FS
72 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020073 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080074#endif
75 struct kref ref;
76};
77
Stephen Boyddfc202e2015-02-02 14:37:41 -080078#define CREATE_TRACE_POINTS
79#include <trace/events/clk.h>
80
Michael Turquetteb09d6d92015-01-29 14:22:50 -080081struct clk {
82 struct clk_core *core;
83 const char *dev_id;
84 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010085 unsigned long min_rate;
86 unsigned long max_rate;
Stephen Boyd50595f82015-02-06 11:42:44 -080087 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080088};
89
Mike Turquetteeab89f62013-03-28 13:59:01 -070090/*** locking ***/
91static void clk_prepare_lock(void)
92{
Mike Turquette533ddeb2013-03-28 13:59:02 -070093 if (!mutex_trylock(&prepare_lock)) {
94 if (prepare_owner == current) {
95 prepare_refcnt++;
96 return;
97 }
98 mutex_lock(&prepare_lock);
99 }
100 WARN_ON_ONCE(prepare_owner != NULL);
101 WARN_ON_ONCE(prepare_refcnt != 0);
102 prepare_owner = current;
103 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700104}
105
106static void clk_prepare_unlock(void)
107{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700108 WARN_ON_ONCE(prepare_owner != current);
109 WARN_ON_ONCE(prepare_refcnt == 0);
110
111 if (--prepare_refcnt)
112 return;
113 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700114 mutex_unlock(&prepare_lock);
115}
116
117static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700118 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700119{
120 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700121
122 if (!spin_trylock_irqsave(&enable_lock, flags)) {
123 if (enable_owner == current) {
124 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700125 __acquire(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700126 return flags;
127 }
128 spin_lock_irqsave(&enable_lock, flags);
129 }
130 WARN_ON_ONCE(enable_owner != NULL);
131 WARN_ON_ONCE(enable_refcnt != 0);
132 enable_owner = current;
133 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700134 return flags;
135}
136
137static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700138 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700139{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700140 WARN_ON_ONCE(enable_owner != current);
141 WARN_ON_ONCE(enable_refcnt == 0);
142
Stephen Boyda57aa182015-07-24 12:24:48 -0700143 if (--enable_refcnt) {
144 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700145 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700146 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700147 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700148 spin_unlock_irqrestore(&enable_lock, flags);
149}
150
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700151static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530152{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700153 /*
154 * .is_prepared is optional for clocks that can prepare
155 * fall back to software usage counter if it is missing
156 */
157 if (!core->ops->is_prepared)
158 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530159
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700160 return core->ops->is_prepared(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530161}
162
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700163static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530164{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700165 /*
166 * .is_enabled is only mandatory for clocks that gate
167 * fall back to software usage counter if .is_enabled is missing
168 */
169 if (!core->ops->is_enabled)
170 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530171
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700172 return core->ops->is_enabled(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530173}
174
Stephen Boydd6968fc2015-04-30 13:54:13 -0700175static void clk_unprepare_unused_subtree(struct clk_core *core)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100176{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100177 struct clk_core *child;
Ulf Hansson1c155b32013-03-12 20:26:03 +0100178
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100179 lockdep_assert_held(&prepare_lock);
180
Stephen Boydd6968fc2015-04-30 13:54:13 -0700181 hlist_for_each_entry(child, &core->children, child_node)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100182 clk_unprepare_unused_subtree(child);
183
Stephen Boydd6968fc2015-04-30 13:54:13 -0700184 if (core->prepare_count)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100185 return;
186
Stephen Boydd6968fc2015-04-30 13:54:13 -0700187 if (core->flags & CLK_IGNORE_UNUSED)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100188 return;
189
Stephen Boydd6968fc2015-04-30 13:54:13 -0700190 if (clk_core_is_prepared(core)) {
191 trace_clk_unprepare(core);
192 if (core->ops->unprepare_unused)
193 core->ops->unprepare_unused(core->hw);
194 else if (core->ops->unprepare)
195 core->ops->unprepare(core->hw);
196 trace_clk_unprepare_complete(core);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100197 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100198}
199
Stephen Boydd6968fc2015-04-30 13:54:13 -0700200static void clk_disable_unused_subtree(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700201{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100202 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700203 unsigned long flags;
204
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100205 lockdep_assert_held(&prepare_lock);
206
Stephen Boydd6968fc2015-04-30 13:54:13 -0700207 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700208 clk_disable_unused_subtree(child);
209
Mike Turquetteeab89f62013-03-28 13:59:01 -0700210 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700211
Stephen Boydd6968fc2015-04-30 13:54:13 -0700212 if (core->enable_count)
Mike Turquetteb24764902012-03-15 23:11:19 -0700213 goto unlock_out;
214
Stephen Boydd6968fc2015-04-30 13:54:13 -0700215 if (core->flags & CLK_IGNORE_UNUSED)
Mike Turquetteb24764902012-03-15 23:11:19 -0700216 goto unlock_out;
217
Mike Turquette7c045a52012-12-04 11:00:35 -0800218 /*
219 * some gate clocks have special needs during the disable-unused
220 * sequence. call .disable_unused if available, otherwise fall
221 * back to .disable
222 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700223 if (clk_core_is_enabled(core)) {
224 trace_clk_disable(core);
225 if (core->ops->disable_unused)
226 core->ops->disable_unused(core->hw);
227 else if (core->ops->disable)
228 core->ops->disable(core->hw);
229 trace_clk_disable_complete(core);
Mike Turquette7c045a52012-12-04 11:00:35 -0800230 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700231
232unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700233 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700234}
235
Olof Johansson1e435252013-04-27 14:10:18 -0700236static bool clk_ignore_unused;
237static int __init clk_ignore_unused_setup(char *__unused)
238{
239 clk_ignore_unused = true;
240 return 1;
241}
242__setup("clk_ignore_unused", clk_ignore_unused_setup);
243
Mike Turquetteb24764902012-03-15 23:11:19 -0700244static int clk_disable_unused(void)
245{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700246 struct clk_core *core;
Mike Turquetteb24764902012-03-15 23:11:19 -0700247
Olof Johansson1e435252013-04-27 14:10:18 -0700248 if (clk_ignore_unused) {
249 pr_warn("clk: Not disabling unused clocks\n");
250 return 0;
251 }
252
Mike Turquetteeab89f62013-03-28 13:59:01 -0700253 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700254
Stephen Boydd6968fc2015-04-30 13:54:13 -0700255 hlist_for_each_entry(core, &clk_root_list, child_node)
256 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700257
Stephen Boydd6968fc2015-04-30 13:54:13 -0700258 hlist_for_each_entry(core, &clk_orphan_list, child_node)
259 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700260
Stephen Boydd6968fc2015-04-30 13:54:13 -0700261 hlist_for_each_entry(core, &clk_root_list, child_node)
262 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100263
Stephen Boydd6968fc2015-04-30 13:54:13 -0700264 hlist_for_each_entry(core, &clk_orphan_list, child_node)
265 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100266
Mike Turquetteeab89f62013-03-28 13:59:01 -0700267 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700268
269 return 0;
270}
Saravana Kannand41d5802013-05-09 11:35:01 -0700271late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700272
273/*** helper functions ***/
274
Russ Dill65800b22012-11-26 11:20:09 -0800275const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700276{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100277 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700278}
Niels de Vos48950842012-12-13 13:12:25 +0100279EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700280
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700281const char *clk_hw_get_name(struct clk_hw *hw)
282{
283 return hw->core->name;
284}
285EXPORT_SYMBOL_GPL(clk_hw_get_name);
286
Russ Dill65800b22012-11-26 11:20:09 -0800287struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700288{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100289 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700290}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800291EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700292
Russ Dill65800b22012-11-26 11:20:09 -0800293u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700294{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100295 return !clk ? 0 : clk->core->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700296}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800297EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700298
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700299unsigned int clk_hw_get_num_parents(struct clk_hw *hw)
300{
301 return hw->core->num_parents;
302}
303EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
304
Russ Dill65800b22012-11-26 11:20:09 -0800305struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700306{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100307 if (!clk)
308 return NULL;
309
310 /* TODO: Create a per-user clk and change callers to call clk_put */
311 return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700312}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800313EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700314
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700315struct clk_hw *clk_hw_get_parent(struct clk_hw *hw)
316{
317 return hw->core->parent ? hw->core->parent->hw : NULL;
318}
319EXPORT_SYMBOL_GPL(clk_hw_get_parent);
320
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700321static struct clk_core *__clk_lookup_subtree(const char *name,
322 struct clk_core *core)
323{
324 struct clk_core *child;
325 struct clk_core *ret;
326
327 if (!strcmp(core->name, name))
328 return core;
329
330 hlist_for_each_entry(child, &core->children, child_node) {
331 ret = __clk_lookup_subtree(name, child);
332 if (ret)
333 return ret;
334 }
335
336 return NULL;
337}
338
339static struct clk_core *clk_core_lookup(const char *name)
340{
341 struct clk_core *root_clk;
342 struct clk_core *ret;
343
344 if (!name)
345 return NULL;
346
347 /* search the 'proper' clk tree first */
348 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
349 ret = __clk_lookup_subtree(name, root_clk);
350 if (ret)
351 return ret;
352 }
353
354 /* if not found, then search the orphan tree */
355 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
356 ret = __clk_lookup_subtree(name, root_clk);
357 if (ret)
358 return ret;
359 }
360
361 return NULL;
362}
363
Stephen Boydd6968fc2015-04-30 13:54:13 -0700364static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100365 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100366{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700367 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100368 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700369 else if (!core->parents)
370 return clk_core_lookup(core->parent_names[index]);
371 else if (!core->parents[index])
372 return core->parents[index] =
373 clk_core_lookup(core->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100374 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700375 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100376}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100377
378struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
379{
380 struct clk_core *parent;
381
382 if (!clk)
383 return NULL;
384
385 parent = clk_core_get_parent_by_index(clk->core, index);
386
387 return !parent ? NULL : parent->hw->clk;
388}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800389EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100390
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700391struct clk_hw *clk_hw_get_parent_by_index(struct clk_hw *hw, unsigned int index)
392{
393 struct clk_core *parent;
394
395 parent = clk_core_get_parent_by_index(hw->core, index);
396
397 return !parent ? NULL : parent->hw;
398}
399EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
400
Russ Dill65800b22012-11-26 11:20:09 -0800401unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700402{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100403 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700404}
405
Stephen Boydd6968fc2015-04-30 13:54:13 -0700406static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700407{
408 unsigned long ret;
409
Stephen Boydd6968fc2015-04-30 13:54:13 -0700410 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530411 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700412 goto out;
413 }
414
Stephen Boydd6968fc2015-04-30 13:54:13 -0700415 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700416
Stephen Boydd6968fc2015-04-30 13:54:13 -0700417 if (core->flags & CLK_IS_ROOT)
Mike Turquetteb24764902012-03-15 23:11:19 -0700418 goto out;
419
Stephen Boydd6968fc2015-04-30 13:54:13 -0700420 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530421 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700422
423out:
424 return ret;
425}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100426
427unsigned long __clk_get_rate(struct clk *clk)
428{
429 if (!clk)
430 return 0;
431
432 return clk_core_get_rate_nolock(clk->core);
433}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800434EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700435
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700436unsigned long clk_hw_get_rate(struct clk_hw *hw)
437{
438 return clk_core_get_rate_nolock(hw->core);
439}
440EXPORT_SYMBOL_GPL(clk_hw_get_rate);
441
Stephen Boydd6968fc2015-04-30 13:54:13 -0700442static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100443{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700444 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100445 return 0;
446
Stephen Boydd6968fc2015-04-30 13:54:13 -0700447 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100448}
449
Russ Dill65800b22012-11-26 11:20:09 -0800450unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700451{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100452 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700453}
Thierry Redingb05c6832013-09-03 09:43:51 +0200454EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700455
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700456unsigned long clk_hw_get_flags(struct clk_hw *hw)
457{
458 return hw->core->flags;
459}
460EXPORT_SYMBOL_GPL(clk_hw_get_flags);
461
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100462bool __clk_is_prepared(struct clk *clk)
463{
464 if (!clk)
465 return false;
466
467 return clk_core_is_prepared(clk->core);
468}
469
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700470bool clk_hw_is_prepared(struct clk_hw *hw)
471{
472 return clk_core_is_prepared(hw->core);
473}
474
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100475bool __clk_is_enabled(struct clk *clk)
476{
477 if (!clk)
478 return false;
479
480 return clk_core_is_enabled(clk->core);
481}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800482EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700483
Stephen Boyd15a02c12015-01-19 18:05:28 -0800484static bool mux_is_better_rate(unsigned long rate, unsigned long now,
485 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100486{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800487 if (flags & CLK_MUX_ROUND_CLOSEST)
488 return abs(now - rate) < abs(best - rate);
489
490 return now <= rate && now > best;
491}
492
Boris Brezillon0817b622015-07-07 20:48:08 +0200493static int
494clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800495 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100496{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100497 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200498 int i, num_parents, ret;
499 unsigned long best = 0;
500 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100501
502 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100503 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
504 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200505 if (core->flags & CLK_SET_RATE_PARENT) {
506 ret = __clk_determine_rate(parent ? parent->hw : NULL,
507 &parent_req);
508 if (ret)
509 return ret;
510
511 best = parent_req.rate;
512 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100513 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200514 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100515 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200516 }
517
James Hogane366fdd2013-07-29 12:25:02 +0100518 goto out;
519 }
520
521 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100522 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100523 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100524 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100525 if (!parent)
526 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200527
528 if (core->flags & CLK_SET_RATE_PARENT) {
529 parent_req = *req;
530 ret = __clk_determine_rate(parent->hw, &parent_req);
531 if (ret)
532 continue;
533 } else {
534 parent_req.rate = clk_core_get_rate_nolock(parent);
535 }
536
537 if (mux_is_better_rate(req->rate, parent_req.rate,
538 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100539 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200540 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100541 }
542 }
543
Boris Brezillon57d866e2015-07-09 22:39:38 +0200544 if (!best_parent)
545 return -EINVAL;
546
James Hogane366fdd2013-07-29 12:25:02 +0100547out:
548 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200549 req->best_parent_hw = best_parent->hw;
550 req->best_parent_rate = best;
551 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100552
Boris Brezillon0817b622015-07-07 20:48:08 +0200553 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100554}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800555
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100556struct clk *__clk_lookup(const char *name)
557{
558 struct clk_core *core = clk_core_lookup(name);
559
560 return !core ? NULL : core->hw->clk;
561}
562
Stephen Boydd6968fc2015-04-30 13:54:13 -0700563static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100564 unsigned long *min_rate,
565 unsigned long *max_rate)
566{
567 struct clk *clk_user;
568
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700569 *min_rate = core->min_rate;
570 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100571
Stephen Boydd6968fc2015-04-30 13:54:13 -0700572 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100573 *min_rate = max(*min_rate, clk_user->min_rate);
574
Stephen Boydd6968fc2015-04-30 13:54:13 -0700575 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100576 *max_rate = min(*max_rate, clk_user->max_rate);
577}
578
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700579void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
580 unsigned long max_rate)
581{
582 hw->core->min_rate = min_rate;
583 hw->core->max_rate = max_rate;
584}
585EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
586
Stephen Boyd15a02c12015-01-19 18:05:28 -0800587/*
588 * Helper for finding best parent to provide a given frequency. This can be used
589 * directly as a determine_rate callback (e.g. for a mux), or from a more
590 * complex clock that may combine a mux with other operations.
591 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200592int __clk_mux_determine_rate(struct clk_hw *hw,
593 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800594{
Boris Brezillon0817b622015-07-07 20:48:08 +0200595 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800596}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800597EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100598
Boris Brezillon0817b622015-07-07 20:48:08 +0200599int __clk_mux_determine_rate_closest(struct clk_hw *hw,
600 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800601{
Boris Brezillon0817b622015-07-07 20:48:08 +0200602 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800603}
604EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
605
Mike Turquetteb24764902012-03-15 23:11:19 -0700606/*** clk api ***/
607
Stephen Boydd6968fc2015-04-30 13:54:13 -0700608static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700609{
Stephen Boyda6334722015-05-06 17:00:54 -0700610 lockdep_assert_held(&prepare_lock);
611
Stephen Boydd6968fc2015-04-30 13:54:13 -0700612 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700613 return;
614
Stephen Boydd6968fc2015-04-30 13:54:13 -0700615 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700616 return;
617
Stephen Boydd6968fc2015-04-30 13:54:13 -0700618 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700619 return;
620
Stephen Boydd6968fc2015-04-30 13:54:13 -0700621 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700622
Stephen Boydd6968fc2015-04-30 13:54:13 -0700623 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800624
Stephen Boydd6968fc2015-04-30 13:54:13 -0700625 if (core->ops->unprepare)
626 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700627
Stephen Boydd6968fc2015-04-30 13:54:13 -0700628 trace_clk_unprepare_complete(core);
629 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700630}
631
632/**
633 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200634 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700635 *
636 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
637 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
638 * if the operation may sleep. One example is a clk which is accessed over
639 * I2c. In the complex case a clk gate operation may require a fast and a slow
640 * part. It is this reason that clk_unprepare and clk_disable are not mutually
641 * exclusive. In fact clk_disable must be called before clk_unprepare.
642 */
643void clk_unprepare(struct clk *clk)
644{
Stephen Boyd63589e92014-03-26 16:06:37 -0700645 if (IS_ERR_OR_NULL(clk))
646 return;
647
Mike Turquetteeab89f62013-03-28 13:59:01 -0700648 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100649 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700650 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700651}
652EXPORT_SYMBOL_GPL(clk_unprepare);
653
Stephen Boydd6968fc2015-04-30 13:54:13 -0700654static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700655{
656 int ret = 0;
657
Stephen Boyda6334722015-05-06 17:00:54 -0700658 lockdep_assert_held(&prepare_lock);
659
Stephen Boydd6968fc2015-04-30 13:54:13 -0700660 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700661 return 0;
662
Stephen Boydd6968fc2015-04-30 13:54:13 -0700663 if (core->prepare_count == 0) {
664 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700665 if (ret)
666 return ret;
667
Stephen Boydd6968fc2015-04-30 13:54:13 -0700668 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800669
Stephen Boydd6968fc2015-04-30 13:54:13 -0700670 if (core->ops->prepare)
671 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800672
Stephen Boydd6968fc2015-04-30 13:54:13 -0700673 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800674
675 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700676 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800677 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700678 }
679 }
680
Stephen Boydd6968fc2015-04-30 13:54:13 -0700681 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700682
683 return 0;
684}
685
686/**
687 * clk_prepare - prepare a clock source
688 * @clk: the clk being prepared
689 *
690 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
691 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
692 * operation may sleep. One example is a clk which is accessed over I2c. In
693 * the complex case a clk ungate operation may require a fast and a slow part.
694 * It is this reason that clk_prepare and clk_enable are not mutually
695 * exclusive. In fact clk_prepare must be called before clk_enable.
696 * Returns 0 on success, -EERROR otherwise.
697 */
698int clk_prepare(struct clk *clk)
699{
700 int ret;
701
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100702 if (!clk)
703 return 0;
704
Mike Turquetteeab89f62013-03-28 13:59:01 -0700705 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100706 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700707 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700708
709 return ret;
710}
711EXPORT_SYMBOL_GPL(clk_prepare);
712
Stephen Boydd6968fc2015-04-30 13:54:13 -0700713static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700714{
Stephen Boyda6334722015-05-06 17:00:54 -0700715 lockdep_assert_held(&enable_lock);
716
Stephen Boydd6968fc2015-04-30 13:54:13 -0700717 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700718 return;
719
Stephen Boydd6968fc2015-04-30 13:54:13 -0700720 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700721 return;
722
Stephen Boydd6968fc2015-04-30 13:54:13 -0700723 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700724 return;
725
Stephen Boydd6968fc2015-04-30 13:54:13 -0700726 trace_clk_disable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800727
Stephen Boydd6968fc2015-04-30 13:54:13 -0700728 if (core->ops->disable)
729 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700730
Stephen Boydd6968fc2015-04-30 13:54:13 -0700731 trace_clk_disable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800732
Stephen Boydd6968fc2015-04-30 13:54:13 -0700733 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100734}
735
Mike Turquetteb24764902012-03-15 23:11:19 -0700736/**
737 * clk_disable - gate a clock
738 * @clk: the clk being gated
739 *
740 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
741 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
742 * clk if the operation is fast and will never sleep. One example is a
743 * SoC-internal clk which is controlled via simple register writes. In the
744 * complex case a clk gate operation may require a fast and a slow part. It is
745 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
746 * In fact clk_disable must be called before clk_unprepare.
747 */
748void clk_disable(struct clk *clk)
749{
750 unsigned long flags;
751
Stephen Boyd63589e92014-03-26 16:06:37 -0700752 if (IS_ERR_OR_NULL(clk))
753 return;
754
Mike Turquetteeab89f62013-03-28 13:59:01 -0700755 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700756 clk_core_disable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700757 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700758}
759EXPORT_SYMBOL_GPL(clk_disable);
760
Stephen Boydd6968fc2015-04-30 13:54:13 -0700761static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700762{
763 int ret = 0;
764
Stephen Boyda6334722015-05-06 17:00:54 -0700765 lockdep_assert_held(&enable_lock);
766
Stephen Boydd6968fc2015-04-30 13:54:13 -0700767 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700768 return 0;
769
Stephen Boydd6968fc2015-04-30 13:54:13 -0700770 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700771 return -ESHUTDOWN;
772
Stephen Boydd6968fc2015-04-30 13:54:13 -0700773 if (core->enable_count == 0) {
774 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700775
776 if (ret)
777 return ret;
778
Stephen Boydd6968fc2015-04-30 13:54:13 -0700779 trace_clk_enable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800780
Stephen Boydd6968fc2015-04-30 13:54:13 -0700781 if (core->ops->enable)
782 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800783
Stephen Boydd6968fc2015-04-30 13:54:13 -0700784 trace_clk_enable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800785
786 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700787 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800788 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700789 }
790 }
791
Stephen Boydd6968fc2015-04-30 13:54:13 -0700792 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700793 return 0;
794}
795
796/**
797 * clk_enable - ungate a clock
798 * @clk: the clk being ungated
799 *
800 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
801 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
802 * if the operation will never sleep. One example is a SoC-internal clk which
803 * is controlled via simple register writes. In the complex case a clk ungate
804 * operation may require a fast and a slow part. It is this reason that
805 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
806 * must be called before clk_enable. Returns 0 on success, -EERROR
807 * otherwise.
808 */
809int clk_enable(struct clk *clk)
810{
811 unsigned long flags;
812 int ret;
813
Dong Aisheng864e1602015-04-30 14:02:19 -0700814 if (!clk)
815 return 0;
816
Mike Turquetteeab89f62013-03-28 13:59:01 -0700817 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -0700818 ret = clk_core_enable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700819 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700820
821 return ret;
822}
823EXPORT_SYMBOL_GPL(clk_enable);
824
Boris Brezillon0817b622015-07-07 20:48:08 +0200825static int clk_core_round_rate_nolock(struct clk_core *core,
826 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -0700827{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100828 struct clk_core *parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200829 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700830
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100831 lockdep_assert_held(&prepare_lock);
832
Stephen Boydd6968fc2015-04-30 13:54:13 -0700833 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700834 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700835
Stephen Boydd6968fc2015-04-30 13:54:13 -0700836 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200837 if (parent) {
838 req->best_parent_hw = parent->hw;
839 req->best_parent_rate = parent->rate;
840 } else {
841 req->best_parent_hw = NULL;
842 req->best_parent_rate = 0;
843 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700844
Stephen Boydd6968fc2015-04-30 13:54:13 -0700845 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +0200846 return core->ops->determine_rate(core->hw, req);
847 } else if (core->ops->round_rate) {
848 rate = core->ops->round_rate(core->hw, req->rate,
849 &req->best_parent_rate);
850 if (rate < 0)
851 return rate;
852
853 req->rate = rate;
854 } else if (core->flags & CLK_SET_RATE_PARENT) {
855 return clk_core_round_rate_nolock(parent, req);
856 } else {
857 req->rate = core->rate;
858 }
859
860 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700861}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100862
863/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100864 * __clk_determine_rate - get the closest rate actually supported by a clock
865 * @hw: determine the rate of this clock
866 * @rate: target rate
867 * @min_rate: returned rate must be greater than this rate
868 * @max_rate: returned rate must be less than this rate
869 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700870 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100871 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200872int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100873{
Boris Brezillon0817b622015-07-07 20:48:08 +0200874 if (!hw) {
875 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100876 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +0200877 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100878
Boris Brezillon0817b622015-07-07 20:48:08 +0200879 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100880}
881EXPORT_SYMBOL_GPL(__clk_determine_rate);
882
883/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100884 * __clk_round_rate - round the given rate for a clk
885 * @clk: round the rate of this clock
886 * @rate: the rate which is to be rounded
887 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700888 * Useful for clk_ops such as .set_rate
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100889 */
890unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
891{
Boris Brezillon0817b622015-07-07 20:48:08 +0200892 struct clk_rate_request req;
893 int ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100894
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100895 if (!clk)
896 return 0;
897
Boris Brezillon0817b622015-07-07 20:48:08 +0200898 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
899 req.rate = rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100900
Boris Brezillon0817b622015-07-07 20:48:08 +0200901 ret = clk_core_round_rate_nolock(clk->core, &req);
902 if (ret)
903 return 0;
904
905 return req.rate;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100906}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +0200907EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700908
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700909unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
910{
911 int ret;
912 struct clk_rate_request req;
913
914 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
915 req.rate = rate;
916
917 ret = clk_core_round_rate_nolock(hw->core, &req);
918 if (ret)
919 return 0;
920
921 return req.rate;
922}
923EXPORT_SYMBOL_GPL(clk_hw_round_rate);
924
Mike Turquetteb24764902012-03-15 23:11:19 -0700925/**
926 * clk_round_rate - round the given rate for a clk
927 * @clk: the clk for which we are rounding a rate
928 * @rate: the rate which is to be rounded
929 *
930 * Takes in a rate as input and rounds it to a rate that the clk can actually
931 * use which is then returned. If clk doesn't support round_rate operation
932 * then the parent rate is returned.
933 */
934long clk_round_rate(struct clk *clk, unsigned long rate)
935{
936 unsigned long ret;
937
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100938 if (!clk)
939 return 0;
940
Mike Turquetteeab89f62013-03-28 13:59:01 -0700941 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700942 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700943 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700944
945 return ret;
946}
947EXPORT_SYMBOL_GPL(clk_round_rate);
948
949/**
950 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -0700951 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -0700952 * @msg: clk notifier type (see include/linux/clk.h)
953 * @old_rate: old clk rate
954 * @new_rate: new clk rate
955 *
956 * Triggers a notifier call chain on the clk rate-change notification
957 * for 'clk'. Passes a pointer to the struct clk and the previous
958 * and current rates to the notifier callback. Intended to be called by
959 * internal clock code only. Returns NOTIFY_DONE from the last driver
960 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
961 * a driver returns that.
962 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700963static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -0700964 unsigned long old_rate, unsigned long new_rate)
965{
966 struct clk_notifier *cn;
967 struct clk_notifier_data cnd;
968 int ret = NOTIFY_DONE;
969
Mike Turquetteb24764902012-03-15 23:11:19 -0700970 cnd.old_rate = old_rate;
971 cnd.new_rate = new_rate;
972
973 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700974 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100975 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700976 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
977 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -0700978 }
979 }
980
981 return ret;
982}
983
984/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100985 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -0700986 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100987 *
988 * Walks the subtree of clks starting with clk and recalculates accuracies as
989 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700990 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100991 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100992 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700993static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100994{
995 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100996 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100997
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100998 lockdep_assert_held(&prepare_lock);
999
Stephen Boydd6968fc2015-04-30 13:54:13 -07001000 if (core->parent)
1001 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001002
Stephen Boydd6968fc2015-04-30 13:54:13 -07001003 if (core->ops->recalc_accuracy)
1004 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001005 parent_accuracy);
1006 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001007 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001008
Stephen Boydd6968fc2015-04-30 13:54:13 -07001009 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001010 __clk_recalc_accuracies(child);
1011}
1012
Stephen Boydd6968fc2015-04-30 13:54:13 -07001013static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001014{
1015 unsigned long accuracy;
1016
1017 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001018 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1019 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001020
Stephen Boydd6968fc2015-04-30 13:54:13 -07001021 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001022 clk_prepare_unlock();
1023
1024 return accuracy;
1025}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001026
1027/**
1028 * clk_get_accuracy - return the accuracy of clk
1029 * @clk: the clk whose accuracy is being returned
1030 *
1031 * Simply returns the cached accuracy of the clk, unless
1032 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1033 * issued.
1034 * If clk is NULL then returns 0.
1035 */
1036long clk_get_accuracy(struct clk *clk)
1037{
1038 if (!clk)
1039 return 0;
1040
1041 return clk_core_get_accuracy(clk->core);
1042}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001043EXPORT_SYMBOL_GPL(clk_get_accuracy);
1044
Stephen Boydd6968fc2015-04-30 13:54:13 -07001045static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001046 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001047{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001048 if (core->ops->recalc_rate)
1049 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001050 return parent_rate;
1051}
1052
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001053/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001054 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001055 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001056 * @msg: notification type (see include/linux/clk.h)
1057 *
1058 * Walks the subtree of clks starting with clk and recalculates rates as it
1059 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001060 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001061 *
1062 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1063 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001064 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001065static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001066{
1067 unsigned long old_rate;
1068 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001069 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001070
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001071 lockdep_assert_held(&prepare_lock);
1072
Stephen Boydd6968fc2015-04-30 13:54:13 -07001073 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001074
Stephen Boydd6968fc2015-04-30 13:54:13 -07001075 if (core->parent)
1076 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001077
Stephen Boydd6968fc2015-04-30 13:54:13 -07001078 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001079
1080 /*
1081 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1082 * & ABORT_RATE_CHANGE notifiers
1083 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001084 if (core->notifier_count && msg)
1085 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001086
Stephen Boydd6968fc2015-04-30 13:54:13 -07001087 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001088 __clk_recalc_rates(child, msg);
1089}
1090
Stephen Boydd6968fc2015-04-30 13:54:13 -07001091static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001092{
1093 unsigned long rate;
1094
1095 clk_prepare_lock();
1096
Stephen Boydd6968fc2015-04-30 13:54:13 -07001097 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1098 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001099
Stephen Boydd6968fc2015-04-30 13:54:13 -07001100 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001101 clk_prepare_unlock();
1102
1103 return rate;
1104}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001105
Mike Turquetteb24764902012-03-15 23:11:19 -07001106/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001107 * clk_get_rate - return the rate of clk
1108 * @clk: the clk whose rate is being returned
1109 *
1110 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1111 * is set, which means a recalc_rate will be issued.
1112 * If clk is NULL then returns 0.
1113 */
1114unsigned long clk_get_rate(struct clk *clk)
1115{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001116 if (!clk)
1117 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001118
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001119 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001120}
1121EXPORT_SYMBOL_GPL(clk_get_rate);
1122
Stephen Boydd6968fc2015-04-30 13:54:13 -07001123static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001124 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001125{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001126 int i;
James Hogan4935b222013-07-29 12:24:59 +01001127
Stephen Boydd6968fc2015-04-30 13:54:13 -07001128 if (!core->parents) {
1129 core->parents = kcalloc(core->num_parents,
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001130 sizeof(struct clk *), GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001131 if (!core->parents)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001132 return -ENOMEM;
1133 }
James Hogan4935b222013-07-29 12:24:59 +01001134
1135 /*
1136 * find index of new parent clock using cached parent ptrs,
1137 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001138 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001139 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001140 for (i = 0; i < core->num_parents; i++) {
1141 if (core->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001142 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001143
Stephen Boydd6968fc2015-04-30 13:54:13 -07001144 if (core->parents[i])
Tomasz Figada0f0b22013-09-29 02:37:16 +02001145 continue;
1146
Stephen Boydd6968fc2015-04-30 13:54:13 -07001147 if (!strcmp(core->parent_names[i], parent->name)) {
1148 core->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001149 return i;
James Hogan4935b222013-07-29 12:24:59 +01001150 }
1151 }
1152
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001153 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001154}
1155
Heiko Stuebnere6500342015-04-22 22:53:05 +02001156/*
1157 * Update the orphan status of @core and all its children.
1158 */
1159static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1160{
1161 struct clk_core *child;
1162
1163 core->orphan = is_orphan;
1164
1165 hlist_for_each_entry(child, &core->children, child_node)
1166 clk_core_update_orphan_status(child, is_orphan);
1167}
1168
Stephen Boydd6968fc2015-04-30 13:54:13 -07001169static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001170{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001171 bool was_orphan = core->orphan;
1172
Stephen Boydd6968fc2015-04-30 13:54:13 -07001173 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001174
James Hogan903efc52013-08-29 12:10:51 +01001175 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001176 bool becomes_orphan = new_parent->orphan;
1177
James Hogan903efc52013-08-29 12:10:51 +01001178 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001179 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001180 new_parent->new_child = NULL;
1181
Stephen Boydd6968fc2015-04-30 13:54:13 -07001182 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001183
1184 if (was_orphan != becomes_orphan)
1185 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001186 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001187 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001188 if (!was_orphan)
1189 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001190 }
James Hogan4935b222013-07-29 12:24:59 +01001191
Stephen Boydd6968fc2015-04-30 13:54:13 -07001192 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001193}
1194
Stephen Boydd6968fc2015-04-30 13:54:13 -07001195static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001196 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001197{
1198 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001199 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001200
1201 /*
1202 * Migrate prepare state between parents and prevent race with
1203 * clk_enable().
1204 *
1205 * If the clock is not prepared, then a race with
1206 * clk_enable/disable() is impossible since we already have the
1207 * prepare lock (future calls to clk_enable() need to be preceded by
1208 * a clk_prepare()).
1209 *
1210 * If the clock is prepared, migrate the prepared state to the new
1211 * parent and also protect against a race with clk_enable() by
1212 * forcing the clock and the new parent on. This ensures that all
1213 * future calls to clk_enable() are practically NOPs with respect to
1214 * hardware and software states.
1215 *
1216 * See also: Comment for clk_set_parent() below.
1217 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001218 if (core->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001219 clk_core_prepare(parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001220 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001221 clk_core_enable(parent);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001222 clk_core_enable(core);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001223 clk_enable_unlock(flags);
James Hogan4935b222013-07-29 12:24:59 +01001224 }
1225
1226 /* update the clk tree topology */
1227 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001228 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001229 clk_enable_unlock(flags);
1230
Stephen Boyd3fa22522014-01-15 10:47:22 -08001231 return old_parent;
1232}
1233
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001234static void __clk_set_parent_after(struct clk_core *core,
1235 struct clk_core *parent,
1236 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001237{
Dong Aishengd2a5d462015-04-15 22:26:36 +08001238 unsigned long flags;
1239
Stephen Boyd3fa22522014-01-15 10:47:22 -08001240 /*
1241 * Finish the migration of prepare state and undo the changes done
1242 * for preventing a race with clk_enable().
1243 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001244 if (core->prepare_count) {
Dong Aishengd2a5d462015-04-15 22:26:36 +08001245 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001246 clk_core_disable(core);
1247 clk_core_disable(old_parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001248 clk_enable_unlock(flags);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001249 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001250 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001251}
1252
Stephen Boydd6968fc2015-04-30 13:54:13 -07001253static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001254 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001255{
1256 unsigned long flags;
1257 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001258 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001259
Stephen Boydd6968fc2015-04-30 13:54:13 -07001260 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001261
Stephen Boydd6968fc2015-04-30 13:54:13 -07001262 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001263
James Hogan4935b222013-07-29 12:24:59 +01001264 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001265 if (parent && core->ops->set_parent)
1266 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001267
Stephen Boydd6968fc2015-04-30 13:54:13 -07001268 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001269
James Hogan4935b222013-07-29 12:24:59 +01001270 if (ret) {
1271 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001272 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001273 clk_enable_unlock(flags);
1274
Stephen Boydd6968fc2015-04-30 13:54:13 -07001275 if (core->prepare_count) {
Dong Aishengd2a5d462015-04-15 22:26:36 +08001276 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001277 clk_core_disable(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001278 clk_core_disable(parent);
Dong Aishengd2a5d462015-04-15 22:26:36 +08001279 clk_enable_unlock(flags);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001280 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001281 }
1282 return ret;
1283 }
1284
Stephen Boydd6968fc2015-04-30 13:54:13 -07001285 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001286
James Hogan4935b222013-07-29 12:24:59 +01001287 return 0;
1288}
1289
Ulf Hanssona093bde2012-08-31 14:21:28 +02001290/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001291 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001292 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001293 * @parent_rate: the "future" rate of clk's parent
1294 *
1295 * Walks the subtree of clks starting with clk, speculating rates as it
1296 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1297 *
1298 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1299 * pre-rate change notifications and returns early if no clks in the
1300 * subtree have subscribed to the notifications. Note that if a clk does not
1301 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001302 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001303 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001304static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001305 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001306{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001307 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001308 unsigned long new_rate;
1309 int ret = NOTIFY_DONE;
1310
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001311 lockdep_assert_held(&prepare_lock);
1312
Stephen Boydd6968fc2015-04-30 13:54:13 -07001313 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001314
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001315 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001316 if (core->notifier_count)
1317 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001318
Mike Turquette86bcfa22014-02-24 16:08:41 -08001319 if (ret & NOTIFY_STOP_MASK) {
1320 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001321 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001322 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001323 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001324
Stephen Boydd6968fc2015-04-30 13:54:13 -07001325 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001326 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001327 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001328 break;
1329 }
1330
1331out:
1332 return ret;
1333}
1334
Stephen Boydd6968fc2015-04-30 13:54:13 -07001335static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001336 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001337{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001338 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001339
Stephen Boydd6968fc2015-04-30 13:54:13 -07001340 core->new_rate = new_rate;
1341 core->new_parent = new_parent;
1342 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001343 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001344 core->new_child = NULL;
1345 if (new_parent && new_parent != core->parent)
1346 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001347
Stephen Boydd6968fc2015-04-30 13:54:13 -07001348 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001349 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001350 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001351 }
1352}
1353
1354/*
1355 * calculate the new rates returning the topmost clock that has to be
1356 * changed.
1357 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001358static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001359 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001360{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001361 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001362 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001363 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001364 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001365 unsigned long min_rate;
1366 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001367 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001368 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001369
Mike Turquette7452b212012-03-26 14:45:36 -07001370 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001371 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001372 return NULL;
1373
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001374 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001375 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001376 if (parent)
1377 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001378
Stephen Boydd6968fc2015-04-30 13:54:13 -07001379 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001380
James Hogan71472c02013-07-29 12:25:00 +01001381 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001382 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001383 struct clk_rate_request req;
1384
1385 req.rate = rate;
1386 req.min_rate = min_rate;
1387 req.max_rate = max_rate;
1388 if (parent) {
1389 req.best_parent_hw = parent->hw;
1390 req.best_parent_rate = parent->rate;
1391 } else {
1392 req.best_parent_hw = NULL;
1393 req.best_parent_rate = 0;
1394 }
1395
1396 ret = core->ops->determine_rate(core->hw, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001397 if (ret < 0)
1398 return NULL;
1399
Boris Brezillon0817b622015-07-07 20:48:08 +02001400 best_parent_rate = req.best_parent_rate;
1401 new_rate = req.rate;
1402 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001403 } else if (core->ops->round_rate) {
1404 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon0817b622015-07-07 20:48:08 +02001405 &best_parent_rate);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001406 if (ret < 0)
1407 return NULL;
1408
1409 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001410 if (new_rate < min_rate || new_rate > max_rate)
1411 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001412 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001413 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001414 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001415 return NULL;
1416 } else {
1417 /* pass-through clock with adjustable parent */
1418 top = clk_calc_new_rates(parent, rate);
1419 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001420 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001421 }
1422
James Hogan71472c02013-07-29 12:25:00 +01001423 /* some clocks must be gated to change parent */
1424 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001425 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001426 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001427 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001428 return NULL;
1429 }
1430
James Hogan71472c02013-07-29 12:25:00 +01001431 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001432 if (parent && core->num_parents > 1) {
1433 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001434 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001435 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001436 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001437 return NULL;
1438 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001439 }
1440
Stephen Boydd6968fc2015-04-30 13:54:13 -07001441 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001442 best_parent_rate != parent->rate)
1443 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001444
1445out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001446 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001447
1448 return top;
1449}
1450
1451/*
1452 * Notify about rate changes in a subtree. Always walk down the whole tree
1453 * so that in case of an error we can walk down the whole tree again and
1454 * abort the change.
1455 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001456static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001457 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001458{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001459 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001460 int ret = NOTIFY_DONE;
1461
Stephen Boydd6968fc2015-04-30 13:54:13 -07001462 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301463 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001464
Stephen Boydd6968fc2015-04-30 13:54:13 -07001465 if (core->notifier_count) {
1466 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001467 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001468 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001469 }
1470
Stephen Boydd6968fc2015-04-30 13:54:13 -07001471 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001472 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001473 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001474 continue;
1475 tmp_clk = clk_propagate_rate_change(child, event);
1476 if (tmp_clk)
1477 fail_clk = tmp_clk;
1478 }
1479
Stephen Boydd6968fc2015-04-30 13:54:13 -07001480 /* handle the new child who might not be in core->children yet */
1481 if (core->new_child) {
1482 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001483 if (tmp_clk)
1484 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001485 }
1486
1487 return fail_clk;
1488}
1489
1490/*
1491 * walk down a subtree and set the new rates notifying the rate
1492 * change on the way
1493 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001494static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001495{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001496 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001497 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001498 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001499 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001500 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001501 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001502
Stephen Boydd6968fc2015-04-30 13:54:13 -07001503 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001504
Stephen Boydd6968fc2015-04-30 13:54:13 -07001505 if (core->new_parent)
1506 best_parent_rate = core->new_parent->rate;
1507 else if (core->parent)
1508 best_parent_rate = core->parent->rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001509
Stephen Boydd6968fc2015-04-30 13:54:13 -07001510 if (core->new_parent && core->new_parent != core->parent) {
1511 old_parent = __clk_set_parent_before(core, core->new_parent);
1512 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001513
Stephen Boydd6968fc2015-04-30 13:54:13 -07001514 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001515 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001516 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001517 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001518 core->new_parent_index);
1519 } else if (core->ops->set_parent) {
1520 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001521 }
1522
Stephen Boydd6968fc2015-04-30 13:54:13 -07001523 trace_clk_set_parent_complete(core, core->new_parent);
1524 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001525 }
1526
Stephen Boydd6968fc2015-04-30 13:54:13 -07001527 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001528
Stephen Boydd6968fc2015-04-30 13:54:13 -07001529 if (!skip_set_rate && core->ops->set_rate)
1530 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001531
Stephen Boydd6968fc2015-04-30 13:54:13 -07001532 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001533
Stephen Boydd6968fc2015-04-30 13:54:13 -07001534 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001535
Stephen Boydd6968fc2015-04-30 13:54:13 -07001536 if (core->notifier_count && old_rate != core->rate)
1537 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001538
Michael Turquette85e88fa2015-06-20 12:18:03 -07001539 if (core->flags & CLK_RECALC_NEW_RATES)
1540 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001541
Tero Kristo067bb172014-08-21 16:47:45 +03001542 /*
1543 * Use safe iteration, as change_rate can actually swap parents
1544 * for certain clock types.
1545 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001546 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001547 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001548 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001549 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001550 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001551 }
1552
Stephen Boydd6968fc2015-04-30 13:54:13 -07001553 /* handle the new child who might not be in core->children yet */
1554 if (core->new_child)
1555 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001556}
1557
Stephen Boydd6968fc2015-04-30 13:54:13 -07001558static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001559 unsigned long req_rate)
1560{
1561 struct clk_core *top, *fail_clk;
1562 unsigned long rate = req_rate;
1563 int ret = 0;
1564
Stephen Boydd6968fc2015-04-30 13:54:13 -07001565 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001566 return 0;
1567
1568 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001569 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001570 return 0;
1571
Stephen Boydd6968fc2015-04-30 13:54:13 -07001572 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001573 return -EBUSY;
1574
1575 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001576 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001577 if (!top)
1578 return -EINVAL;
1579
1580 /* notify that we are about to change rates */
1581 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1582 if (fail_clk) {
1583 pr_debug("%s: failed to set %s rate\n", __func__,
1584 fail_clk->name);
1585 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1586 return -EBUSY;
1587 }
1588
1589 /* change the rates */
1590 clk_change_rate(top);
1591
Stephen Boydd6968fc2015-04-30 13:54:13 -07001592 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001593
1594 return ret;
1595}
1596
Mike Turquetteb24764902012-03-15 23:11:19 -07001597/**
1598 * clk_set_rate - specify a new rate for clk
1599 * @clk: the clk whose rate is being changed
1600 * @rate: the new rate for clk
1601 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001602 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001603 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001604 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1605 * propagate up to clk's parent; whether or not this happens depends on the
1606 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1607 * after calling .round_rate then upstream parent propagation is ignored. If
1608 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001609 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001610 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1611 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001612 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001613 * Rate changes are accomplished via tree traversal that also recalculates the
1614 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001615 *
1616 * Returns 0 on success, -EERROR otherwise.
1617 */
1618int clk_set_rate(struct clk *clk, unsigned long rate)
1619{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001620 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001621
Mike Turquette89ac8d72013-08-21 23:58:09 -07001622 if (!clk)
1623 return 0;
1624
Mike Turquetteb24764902012-03-15 23:11:19 -07001625 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001626 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001627
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001628 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001629
Mike Turquetteeab89f62013-03-28 13:59:01 -07001630 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001631
1632 return ret;
1633}
1634EXPORT_SYMBOL_GPL(clk_set_rate);
1635
1636/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001637 * clk_set_rate_range - set a rate range for a clock source
1638 * @clk: clock source
1639 * @min: desired minimum clock rate in Hz, inclusive
1640 * @max: desired maximum clock rate in Hz, inclusive
1641 *
1642 * Returns success (0) or negative errno.
1643 */
1644int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1645{
1646 int ret = 0;
1647
1648 if (!clk)
1649 return 0;
1650
1651 if (min > max) {
1652 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1653 __func__, clk->core->name, clk->dev_id, clk->con_id,
1654 min, max);
1655 return -EINVAL;
1656 }
1657
1658 clk_prepare_lock();
1659
1660 if (min != clk->min_rate || max != clk->max_rate) {
1661 clk->min_rate = min;
1662 clk->max_rate = max;
1663 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1664 }
1665
1666 clk_prepare_unlock();
1667
1668 return ret;
1669}
1670EXPORT_SYMBOL_GPL(clk_set_rate_range);
1671
1672/**
1673 * clk_set_min_rate - set a minimum clock rate for a clock source
1674 * @clk: clock source
1675 * @rate: desired minimum clock rate in Hz, inclusive
1676 *
1677 * Returns success (0) or negative errno.
1678 */
1679int clk_set_min_rate(struct clk *clk, unsigned long rate)
1680{
1681 if (!clk)
1682 return 0;
1683
1684 return clk_set_rate_range(clk, rate, clk->max_rate);
1685}
1686EXPORT_SYMBOL_GPL(clk_set_min_rate);
1687
1688/**
1689 * clk_set_max_rate - set a maximum clock rate for a clock source
1690 * @clk: clock source
1691 * @rate: desired maximum clock rate in Hz, inclusive
1692 *
1693 * Returns success (0) or negative errno.
1694 */
1695int clk_set_max_rate(struct clk *clk, unsigned long rate)
1696{
1697 if (!clk)
1698 return 0;
1699
1700 return clk_set_rate_range(clk, clk->min_rate, rate);
1701}
1702EXPORT_SYMBOL_GPL(clk_set_max_rate);
1703
1704/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001705 * clk_get_parent - return the parent of a clk
1706 * @clk: the clk whose parent gets returned
1707 *
1708 * Simply returns clk->parent. Returns NULL if clk is NULL.
1709 */
1710struct clk *clk_get_parent(struct clk *clk)
1711{
1712 struct clk *parent;
1713
Mike Turquetteeab89f62013-03-28 13:59:01 -07001714 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001715 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001716 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001717
1718 return parent;
1719}
1720EXPORT_SYMBOL_GPL(clk_get_parent);
1721
1722/*
1723 * .get_parent is mandatory for clocks with multiple possible parents. It is
1724 * optional for single-parent clocks. Always call .get_parent if it is
1725 * available and WARN if it is missing for multi-parent clocks.
1726 *
1727 * For single-parent clocks without .get_parent, first check to see if the
1728 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001729 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001730 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001731static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001732{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001733 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001734 u8 index;
1735
1736 /* handle the trivial cases */
1737
Stephen Boydd6968fc2015-04-30 13:54:13 -07001738 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -07001739 goto out;
1740
Stephen Boydd6968fc2015-04-30 13:54:13 -07001741 if (core->num_parents == 1) {
1742 if (IS_ERR_OR_NULL(core->parent))
1743 core->parent = clk_core_lookup(core->parent_names[0]);
1744 ret = core->parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001745 goto out;
1746 }
1747
Stephen Boydd6968fc2015-04-30 13:54:13 -07001748 if (!core->ops->get_parent) {
1749 WARN(!core->ops->get_parent,
Mike Turquetteb24764902012-03-15 23:11:19 -07001750 "%s: multi-parent clocks must implement .get_parent\n",
1751 __func__);
1752 goto out;
1753 };
1754
1755 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07001756 * Do our best to cache parent clocks in core->parents. This prevents
1757 * unnecessary and expensive lookups. We don't set core->parent here;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001758 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001759 */
1760
Stephen Boydd6968fc2015-04-30 13:54:13 -07001761 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001762
Stephen Boydd6968fc2015-04-30 13:54:13 -07001763 if (!core->parents)
1764 core->parents =
1765 kcalloc(core->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001766 GFP_KERNEL);
1767
Stephen Boydd6968fc2015-04-30 13:54:13 -07001768 ret = clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001769
1770out:
1771 return ret;
1772}
1773
Stephen Boydd6968fc2015-04-30 13:54:13 -07001774static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001775 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001776{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001777 clk_reparent(core, new_parent);
1778 __clk_recalc_accuracies(core);
1779 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07001780}
1781
Tomeu Vizoso42c86542015-03-11 11:34:25 +01001782void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
1783{
1784 if (!hw)
1785 return;
1786
1787 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
1788}
1789
Mike Turquetteb24764902012-03-15 23:11:19 -07001790/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001791 * clk_has_parent - check if a clock is a possible parent for another
1792 * @clk: clock source
1793 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07001794 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001795 * This function can be used in drivers that need to check that a clock can be
1796 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001797 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001798 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001799 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001800bool clk_has_parent(struct clk *clk, struct clk *parent)
1801{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001802 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001803 unsigned int i;
1804
1805 /* NULL clocks should be nops, so return success if either is NULL. */
1806 if (!clk || !parent)
1807 return true;
1808
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001809 core = clk->core;
1810 parent_core = parent->core;
1811
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001812 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001813 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001814 return true;
1815
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001816 for (i = 0; i < core->num_parents; i++)
1817 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001818 return true;
1819
1820 return false;
1821}
1822EXPORT_SYMBOL_GPL(clk_has_parent);
1823
Stephen Boydd6968fc2015-04-30 13:54:13 -07001824static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001825{
1826 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001827 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001828 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001829
Stephen Boydd6968fc2015-04-30 13:54:13 -07001830 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07001831 return 0;
1832
Mike Turquetteb24764902012-03-15 23:11:19 -07001833 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001834 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001835
Stephen Boydd6968fc2015-04-30 13:54:13 -07001836 if (core->parent == parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001837 goto out;
1838
Stephen Boydb61c43c2015-02-02 14:11:25 -08001839 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001840 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08001841 ret = -ENOSYS;
1842 goto out;
1843 }
1844
Ulf Hansson031dcc92013-04-02 23:09:38 +02001845 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001846 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001847 ret = -EBUSY;
1848 goto out;
1849 }
1850
1851 /* try finding the new parent index */
1852 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001853 p_index = clk_fetch_parent_index(core, parent);
Ulf Hansson031dcc92013-04-02 23:09:38 +02001854 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001855 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001856 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001857 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001858 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001859 goto out;
1860 }
1861 }
1862
Mike Turquetteb24764902012-03-15 23:11:19 -07001863 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001864 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001865
1866 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001867 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001868 goto out;
1869
Ulf Hansson031dcc92013-04-02 23:09:38 +02001870 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001871 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001872
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001873 /* propagate rate an accuracy recalculation accordingly */
1874 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001875 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001876 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001877 __clk_recalc_rates(core, POST_RATE_CHANGE);
1878 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001879 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001880
1881out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001882 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001883
1884 return ret;
1885}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001886
1887/**
1888 * clk_set_parent - switch the parent of a mux clk
1889 * @clk: the mux clk whose input we are switching
1890 * @parent: the new input to clk
1891 *
1892 * Re-parent clk to use parent as its new input source. If clk is in
1893 * prepared state, the clk will get enabled for the duration of this call. If
1894 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1895 * that, the reparenting is glitchy in hardware, etc), use the
1896 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1897 *
1898 * After successfully changing clk's parent clk_set_parent will update the
1899 * clk topology, sysfs topology and propagate rate recalculation via
1900 * __clk_recalc_rates.
1901 *
1902 * Returns 0 on success, -EERROR otherwise.
1903 */
1904int clk_set_parent(struct clk *clk, struct clk *parent)
1905{
1906 if (!clk)
1907 return 0;
1908
1909 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1910}
Mike Turquetteb24764902012-03-15 23:11:19 -07001911EXPORT_SYMBOL_GPL(clk_set_parent);
1912
1913/**
Mike Turquettee59c5372014-02-18 21:21:25 -08001914 * clk_set_phase - adjust the phase shift of a clock signal
1915 * @clk: clock signal source
1916 * @degrees: number of degrees the signal is shifted
1917 *
1918 * Shifts the phase of a clock signal by the specified
1919 * degrees. Returns 0 on success, -EERROR otherwise.
1920 *
1921 * This function makes no distinction about the input or reference
1922 * signal that we adjust the clock signal phase against. For example
1923 * phase locked-loop clock signal generators we may shift phase with
1924 * respect to feedback clock signal input, but for other cases the
1925 * clock phase may be shifted with respect to some other, unspecified
1926 * signal.
1927 *
1928 * Additionally the concept of phase shift does not propagate through
1929 * the clock tree hierarchy, which sets it apart from clock rates and
1930 * clock accuracy. A parent clock phase attribute does not have an
1931 * impact on the phase attribute of a child clock.
1932 */
1933int clk_set_phase(struct clk *clk, int degrees)
1934{
Stephen Boyd08b95752015-02-02 14:09:43 -08001935 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08001936
1937 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08001938 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08001939
1940 /* sanity check degrees */
1941 degrees %= 360;
1942 if (degrees < 0)
1943 degrees += 360;
1944
1945 clk_prepare_lock();
1946
Stephen Boyddfc202e2015-02-02 14:37:41 -08001947 trace_clk_set_phase(clk->core, degrees);
1948
Stephen Boyd08b95752015-02-02 14:09:43 -08001949 if (clk->core->ops->set_phase)
1950 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08001951
Stephen Boyddfc202e2015-02-02 14:37:41 -08001952 trace_clk_set_phase_complete(clk->core, degrees);
1953
Mike Turquettee59c5372014-02-18 21:21:25 -08001954 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001955 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08001956
Mike Turquettee59c5372014-02-18 21:21:25 -08001957 clk_prepare_unlock();
1958
Mike Turquettee59c5372014-02-18 21:21:25 -08001959 return ret;
1960}
Maxime Ripard9767b042015-01-20 22:23:43 +01001961EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001962
Stephen Boydd6968fc2015-04-30 13:54:13 -07001963static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08001964{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07001965 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08001966
1967 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001968 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08001969 clk_prepare_unlock();
1970
Mike Turquettee59c5372014-02-18 21:21:25 -08001971 return ret;
1972}
1973
1974/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001975 * clk_get_phase - return the phase shift of a clock signal
1976 * @clk: clock signal source
1977 *
1978 * Returns the phase shift of a clock node in degrees, otherwise returns
1979 * -EERROR.
1980 */
1981int clk_get_phase(struct clk *clk)
1982{
1983 if (!clk)
1984 return 0;
1985
1986 return clk_core_get_phase(clk->core);
1987}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07001988EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07001989
1990/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08001991 * clk_is_match - check if two clk's point to the same hardware clock
1992 * @p: clk compared against q
1993 * @q: clk compared against p
1994 *
1995 * Returns true if the two struct clk pointers both point to the same hardware
1996 * clock node. Put differently, returns true if struct clk *p and struct clk *q
1997 * share the same struct clk_core object.
1998 *
1999 * Returns false otherwise. Note that two NULL clks are treated as matching.
2000 */
2001bool clk_is_match(const struct clk *p, const struct clk *q)
2002{
2003 /* trivial case: identical struct clk's or both NULL */
2004 if (p == q)
2005 return true;
2006
2007 /* true if clk->core pointers match. Avoid derefing garbage */
2008 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2009 if (p->core == q->core)
2010 return true;
2011
2012 return false;
2013}
2014EXPORT_SYMBOL_GPL(clk_is_match);
2015
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002016/*** debugfs support ***/
2017
2018#ifdef CONFIG_DEBUG_FS
2019#include <linux/debugfs.h>
2020
2021static struct dentry *rootdir;
2022static int inited = 0;
2023static DEFINE_MUTEX(clk_debug_lock);
2024static HLIST_HEAD(clk_debug_list);
2025
2026static struct hlist_head *all_lists[] = {
2027 &clk_root_list,
2028 &clk_orphan_list,
2029 NULL,
2030};
2031
2032static struct hlist_head *orphan_list[] = {
2033 &clk_orphan_list,
2034 NULL,
2035};
2036
2037static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2038 int level)
2039{
2040 if (!c)
2041 return;
2042
2043 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
2044 level * 3 + 1, "",
2045 30 - level * 3, c->name,
2046 c->enable_count, c->prepare_count, clk_core_get_rate(c),
2047 clk_core_get_accuracy(c), clk_core_get_phase(c));
2048}
2049
2050static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2051 int level)
2052{
2053 struct clk_core *child;
2054
2055 if (!c)
2056 return;
2057
2058 clk_summary_show_one(s, c, level);
2059
2060 hlist_for_each_entry(child, &c->children, child_node)
2061 clk_summary_show_subtree(s, child, level + 1);
2062}
2063
2064static int clk_summary_show(struct seq_file *s, void *data)
2065{
2066 struct clk_core *c;
2067 struct hlist_head **lists = (struct hlist_head **)s->private;
2068
2069 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
2070 seq_puts(s, "----------------------------------------------------------------------------------------\n");
2071
2072 clk_prepare_lock();
2073
2074 for (; *lists; lists++)
2075 hlist_for_each_entry(c, *lists, child_node)
2076 clk_summary_show_subtree(s, c, 0);
2077
2078 clk_prepare_unlock();
2079
2080 return 0;
2081}
2082
2083
2084static int clk_summary_open(struct inode *inode, struct file *file)
2085{
2086 return single_open(file, clk_summary_show, inode->i_private);
2087}
2088
2089static const struct file_operations clk_summary_fops = {
2090 .open = clk_summary_open,
2091 .read = seq_read,
2092 .llseek = seq_lseek,
2093 .release = single_release,
2094};
2095
2096static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2097{
2098 if (!c)
2099 return;
2100
Stefan Wahren7cb81132015-04-29 16:36:43 +00002101 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002102 seq_printf(s, "\"%s\": { ", c->name);
2103 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2104 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002105 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2106 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002107 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2108}
2109
2110static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2111{
2112 struct clk_core *child;
2113
2114 if (!c)
2115 return;
2116
2117 clk_dump_one(s, c, level);
2118
2119 hlist_for_each_entry(child, &c->children, child_node) {
2120 seq_printf(s, ",");
2121 clk_dump_subtree(s, child, level + 1);
2122 }
2123
2124 seq_printf(s, "}");
2125}
2126
2127static int clk_dump(struct seq_file *s, void *data)
2128{
2129 struct clk_core *c;
2130 bool first_node = true;
2131 struct hlist_head **lists = (struct hlist_head **)s->private;
2132
2133 seq_printf(s, "{");
2134
2135 clk_prepare_lock();
2136
2137 for (; *lists; lists++) {
2138 hlist_for_each_entry(c, *lists, child_node) {
2139 if (!first_node)
2140 seq_puts(s, ",");
2141 first_node = false;
2142 clk_dump_subtree(s, c, 0);
2143 }
2144 }
2145
2146 clk_prepare_unlock();
2147
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002148 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002149 return 0;
2150}
2151
2152
2153static int clk_dump_open(struct inode *inode, struct file *file)
2154{
2155 return single_open(file, clk_dump, inode->i_private);
2156}
2157
2158static const struct file_operations clk_dump_fops = {
2159 .open = clk_dump_open,
2160 .read = seq_read,
2161 .llseek = seq_lseek,
2162 .release = single_release,
2163};
2164
2165static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2166{
2167 struct dentry *d;
2168 int ret = -ENOMEM;
2169
2170 if (!core || !pdentry) {
2171 ret = -EINVAL;
2172 goto out;
2173 }
2174
2175 d = debugfs_create_dir(core->name, pdentry);
2176 if (!d)
2177 goto out;
2178
2179 core->dentry = d;
2180
2181 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2182 (u32 *)&core->rate);
2183 if (!d)
2184 goto err_out;
2185
2186 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2187 (u32 *)&core->accuracy);
2188 if (!d)
2189 goto err_out;
2190
2191 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2192 (u32 *)&core->phase);
2193 if (!d)
2194 goto err_out;
2195
2196 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2197 (u32 *)&core->flags);
2198 if (!d)
2199 goto err_out;
2200
2201 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2202 (u32 *)&core->prepare_count);
2203 if (!d)
2204 goto err_out;
2205
2206 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2207 (u32 *)&core->enable_count);
2208 if (!d)
2209 goto err_out;
2210
2211 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2212 (u32 *)&core->notifier_count);
2213 if (!d)
2214 goto err_out;
2215
2216 if (core->ops->debug_init) {
2217 ret = core->ops->debug_init(core->hw, core->dentry);
2218 if (ret)
2219 goto err_out;
2220 }
2221
2222 ret = 0;
2223 goto out;
2224
2225err_out:
2226 debugfs_remove_recursive(core->dentry);
2227 core->dentry = NULL;
2228out:
2229 return ret;
2230}
2231
2232/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002233 * clk_debug_register - add a clk node to the debugfs clk directory
2234 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002235 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002236 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2237 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002238 * will be created lazily by clk_debug_init as part of a late_initcall.
2239 */
2240static int clk_debug_register(struct clk_core *core)
2241{
2242 int ret = 0;
2243
2244 mutex_lock(&clk_debug_lock);
2245 hlist_add_head(&core->debug_node, &clk_debug_list);
2246
2247 if (!inited)
2248 goto unlock;
2249
2250 ret = clk_debug_create_one(core, rootdir);
2251unlock:
2252 mutex_unlock(&clk_debug_lock);
2253
2254 return ret;
2255}
2256
2257 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002258 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2259 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002260 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002261 * Dynamically removes a clk and all its child nodes from the
2262 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002263 * clk_debug_register in __clk_init.
2264 */
2265static void clk_debug_unregister(struct clk_core *core)
2266{
2267 mutex_lock(&clk_debug_lock);
2268 hlist_del_init(&core->debug_node);
2269 debugfs_remove_recursive(core->dentry);
2270 core->dentry = NULL;
2271 mutex_unlock(&clk_debug_lock);
2272}
2273
2274struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2275 void *data, const struct file_operations *fops)
2276{
2277 struct dentry *d = NULL;
2278
2279 if (hw->core->dentry)
2280 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2281 fops);
2282
2283 return d;
2284}
2285EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2286
2287/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002288 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002289 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002290 * clks are often initialized very early during boot before memory can be
2291 * dynamically allocated and well before debugfs is setup. This function
2292 * populates the debugfs clk directory once at boot-time when we know that
2293 * debugfs is setup. It should only be called once at boot-time, all other clks
2294 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002295 */
2296static int __init clk_debug_init(void)
2297{
2298 struct clk_core *core;
2299 struct dentry *d;
2300
2301 rootdir = debugfs_create_dir("clk", NULL);
2302
2303 if (!rootdir)
2304 return -ENOMEM;
2305
2306 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2307 &clk_summary_fops);
2308 if (!d)
2309 return -ENOMEM;
2310
2311 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2312 &clk_dump_fops);
2313 if (!d)
2314 return -ENOMEM;
2315
2316 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2317 &orphan_list, &clk_summary_fops);
2318 if (!d)
2319 return -ENOMEM;
2320
2321 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2322 &orphan_list, &clk_dump_fops);
2323 if (!d)
2324 return -ENOMEM;
2325
2326 mutex_lock(&clk_debug_lock);
2327 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2328 clk_debug_create_one(core, rootdir);
2329
2330 inited = 1;
2331 mutex_unlock(&clk_debug_lock);
2332
2333 return 0;
2334}
2335late_initcall(clk_debug_init);
2336#else
2337static inline int clk_debug_register(struct clk_core *core) { return 0; }
2338static inline void clk_debug_reparent(struct clk_core *core,
2339 struct clk_core *new_parent)
2340{
2341}
2342static inline void clk_debug_unregister(struct clk_core *core)
2343{
2344}
2345#endif
2346
Michael Turquette3d3801e2015-02-25 09:11:01 -08002347/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002348 * __clk_init - initialize the data structures in a struct clk
2349 * @dev: device initializing this clk, placeholder for now
2350 * @clk: clk being initialized
2351 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002352 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002353 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002354 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002355static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002356{
Mike Turquetted1302a32012-03-29 14:30:40 -07002357 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002358 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002359 struct hlist_node *tmp2;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002360 struct clk_core *core;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002361 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002362
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002363 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002364 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002365
Stephen Boydd6968fc2015-04-30 13:54:13 -07002366 core = clk_user->core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002367
Mike Turquetteeab89f62013-03-28 13:59:01 -07002368 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002369
2370 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002371 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002372 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002373 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002374 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002375 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002376 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002377
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002378 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002379 if (core->ops->set_rate &&
2380 !((core->ops->round_rate || core->ops->determine_rate) &&
2381 core->ops->recalc_rate)) {
James Hogan71472c02013-07-29 12:25:00 +01002382 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002383 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002384 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002385 goto out;
2386 }
2387
Stephen Boydd6968fc2015-04-30 13:54:13 -07002388 if (core->ops->set_parent && !core->ops->get_parent) {
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002389 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002390 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002391 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002392 goto out;
2393 }
2394
Stephen Boydd6968fc2015-04-30 13:54:13 -07002395 if (core->ops->set_rate_and_parent &&
2396 !(core->ops->set_parent && core->ops->set_rate)) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002397 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002398 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002399 ret = -EINVAL;
2400 goto out;
2401 }
2402
Mike Turquetteb24764902012-03-15 23:11:19 -07002403 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002404 for (i = 0; i < core->num_parents; i++)
2405 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002406 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002407 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002408
2409 /*
2410 * Allocate an array of struct clk *'s to avoid unnecessary string
2411 * look-ups of clk's possible parents. This can fail for clocks passed
Stephen Boydd6968fc2015-04-30 13:54:13 -07002412 * in to clk_init during early boot; thus any access to core->parents[]
Mike Turquetteb24764902012-03-15 23:11:19 -07002413 * must always check for a NULL pointer and try to populate it if
2414 * necessary.
2415 *
Stephen Boydd6968fc2015-04-30 13:54:13 -07002416 * If core->parents is not NULL we skip this entire block. This allows
2417 * for clock drivers to statically initialize core->parents.
Mike Turquetteb24764902012-03-15 23:11:19 -07002418 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002419 if (core->num_parents > 1 && !core->parents) {
2420 core->parents = kcalloc(core->num_parents, sizeof(struct clk *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002421 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002422 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002423 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002424 * clk_init'd; thus any access to clk->parents[] must check
2425 * for a NULL pointer. We can always perform lazy lookups for
2426 * missing parents later on.
2427 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002428 if (core->parents)
2429 for (i = 0; i < core->num_parents; i++)
2430 core->parents[i] =
2431 clk_core_lookup(core->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002432 }
2433
Stephen Boydd6968fc2015-04-30 13:54:13 -07002434 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002435
2436 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07002437 * Populate core->parent if parent has already been __clk_init'd. If
Mike Turquetteb24764902012-03-15 23:11:19 -07002438 * parent has not yet been __clk_init'd then place clk in the orphan
2439 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2440 * clk list.
2441 *
2442 * Every time a new clk is clk_init'd then we walk the list of orphan
2443 * clocks and re-parent any that are children of the clock currently
2444 * being clk_init'd.
2445 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02002446 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002447 hlist_add_head(&core->child_node,
2448 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002449 core->orphan = core->parent->orphan;
2450 } else if (core->flags & CLK_IS_ROOT) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002451 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002452 core->orphan = false;
2453 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002454 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002455 core->orphan = true;
2456 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002457
2458 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002459 * Set clk's accuracy. The preferred method is to use
2460 * .recalc_accuracy. For simple clocks and lazy developers the default
2461 * fallback is to use the parent's accuracy. If a clock doesn't have a
2462 * parent (or is orphaned) then accuracy is set to zero (perfect
2463 * clock).
2464 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002465 if (core->ops->recalc_accuracy)
2466 core->accuracy = core->ops->recalc_accuracy(core->hw,
2467 __clk_get_accuracy(core->parent));
2468 else if (core->parent)
2469 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002470 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002471 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002472
2473 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002474 * Set clk's phase.
2475 * Since a phase is by definition relative to its parent, just
2476 * query the current clock phase, or just assume it's in phase.
2477 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002478 if (core->ops->get_phase)
2479 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002480 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002481 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002482
2483 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002484 * Set clk's rate. The preferred method is to use .recalc_rate. For
2485 * simple clocks and lazy developers the default fallback is to use the
2486 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2487 * then rate is set to zero.
2488 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002489 if (core->ops->recalc_rate)
2490 rate = core->ops->recalc_rate(core->hw,
2491 clk_core_get_rate_nolock(core->parent));
2492 else if (core->parent)
2493 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002494 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002495 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002496 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002497
2498 /*
2499 * walk the list of orphan clocks and reparent any that are children of
2500 * this clock
2501 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002502 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002503 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002504 i = orphan->ops->get_parent(orphan->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002505 if (!strcmp(core->name, orphan->parent_names[i]))
2506 clk_core_reparent(orphan, core);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002507 continue;
2508 }
2509
Mike Turquetteb24764902012-03-15 23:11:19 -07002510 for (i = 0; i < orphan->num_parents; i++)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002511 if (!strcmp(core->name, orphan->parent_names[i])) {
2512 clk_core_reparent(orphan, core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002513 break;
2514 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002515 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002516
2517 /*
2518 * optional platform-specific magic
2519 *
2520 * The .init callback is not used by any of the basic clock types, but
2521 * exists for weird hardware that must perform initialization magic.
2522 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002523 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002524 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002525 if (core->ops->init)
2526 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002527
Stephen Boydd6968fc2015-04-30 13:54:13 -07002528 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002529out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002530 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002531
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002532 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002533 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002534
Mike Turquetted1302a32012-03-29 14:30:40 -07002535 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002536}
2537
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002538struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2539 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002540{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002541 struct clk *clk;
2542
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002543 /* This is to allow this function to be chained to others */
2544 if (!hw || IS_ERR(hw))
2545 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002546
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002547 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2548 if (!clk)
2549 return ERR_PTR(-ENOMEM);
2550
2551 clk->core = hw->core;
2552 clk->dev_id = dev_id;
2553 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002554 clk->max_rate = ULONG_MAX;
2555
2556 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002557 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002558 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002559
2560 return clk;
2561}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002562
Stephen Boyd73e0e492015-02-06 11:42:43 -08002563void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002564{
2565 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002566 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002567 clk_prepare_unlock();
2568
2569 kfree(clk);
2570}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002571
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002572/**
2573 * clk_register - allocate a new clock, register it and return an opaque cookie
2574 * @dev: device that is registering this clock
2575 * @hw: link to hardware-specific clock data
2576 *
2577 * clk_register is the primary interface for populating the clock tree with new
2578 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05302579 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002580 * rest of the clock API. In the event of an error clk_register will return an
2581 * error code; drivers must test for an error code after calling clk_register.
2582 */
2583struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002584{
Mike Turquetted1302a32012-03-29 14:30:40 -07002585 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002586 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002587
Stephen Boydd6968fc2015-04-30 13:54:13 -07002588 core = kzalloc(sizeof(*core), GFP_KERNEL);
2589 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002590 ret = -ENOMEM;
2591 goto fail_out;
2592 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002593
Stephen Boydd6968fc2015-04-30 13:54:13 -07002594 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2595 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002596 ret = -ENOMEM;
2597 goto fail_name;
2598 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002599 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002600 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002601 core->owner = dev->driver->owner;
2602 core->hw = hw;
2603 core->flags = hw->init->flags;
2604 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07002605 core->min_rate = 0;
2606 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002607 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002608
Mike Turquetted1302a32012-03-29 14:30:40 -07002609 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002610 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002611 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002612
Stephen Boydd6968fc2015-04-30 13:54:13 -07002613 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002614 ret = -ENOMEM;
2615 goto fail_parent_names;
2616 }
2617
2618
2619 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002620 for (i = 0; i < core->num_parents; i++) {
2621 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002622 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002623 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002624 ret = -ENOMEM;
2625 goto fail_parent_names_copy;
2626 }
2627 }
2628
Stephen Boydd6968fc2015-04-30 13:54:13 -07002629 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002630
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002631 hw->clk = __clk_create_clk(hw, NULL, NULL);
2632 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002633 ret = PTR_ERR(hw->clk);
2634 goto fail_parent_names_copy;
2635 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002636
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002637 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002638 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002639 return hw->clk;
2640
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002641 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002642 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002643
2644fail_parent_names_copy:
2645 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002646 kfree_const(core->parent_names[i]);
2647 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07002648fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002649 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002650fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002651 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002652fail_out:
2653 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002654}
2655EXPORT_SYMBOL_GPL(clk_register);
2656
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002657/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002658static void __clk_release(struct kref *ref)
2659{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002660 struct clk_core *core = container_of(ref, struct clk_core, ref);
2661 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002662
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002663 lockdep_assert_held(&prepare_lock);
2664
Stephen Boydd6968fc2015-04-30 13:54:13 -07002665 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002666 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002667 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002668
Stephen Boydd6968fc2015-04-30 13:54:13 -07002669 kfree(core->parent_names);
2670 kfree_const(core->name);
2671 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002672}
2673
2674/*
2675 * Empty clk_ops for unregistered clocks. These are used temporarily
2676 * after clk_unregister() was called on a clock and until last clock
2677 * consumer calls clk_put() and the struct clk object is freed.
2678 */
2679static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2680{
2681 return -ENXIO;
2682}
2683
2684static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2685{
2686 WARN_ON_ONCE(1);
2687}
2688
2689static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2690 unsigned long parent_rate)
2691{
2692 return -ENXIO;
2693}
2694
2695static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2696{
2697 return -ENXIO;
2698}
2699
2700static const struct clk_ops clk_nodrv_ops = {
2701 .enable = clk_nodrv_prepare_enable,
2702 .disable = clk_nodrv_disable_unprepare,
2703 .prepare = clk_nodrv_prepare_enable,
2704 .unprepare = clk_nodrv_disable_unprepare,
2705 .set_rate = clk_nodrv_set_rate,
2706 .set_parent = clk_nodrv_set_parent,
2707};
2708
Mark Brown1df5c932012-04-18 09:07:12 +01002709/**
2710 * clk_unregister - unregister a currently registered clock
2711 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002712 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002713void clk_unregister(struct clk *clk)
2714{
2715 unsigned long flags;
2716
Stephen Boyd6314b672014-09-04 23:37:49 -07002717 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2718 return;
2719
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002720 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002721
2722 clk_prepare_lock();
2723
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002724 if (clk->core->ops == &clk_nodrv_ops) {
2725 pr_err("%s: unregistered clock: %s\n", __func__,
2726 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002727 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002728 }
2729 /*
2730 * Assign empty clock ops for consumers that might still hold
2731 * a reference to this clock.
2732 */
2733 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002734 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002735 clk_enable_unlock(flags);
2736
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002737 if (!hlist_empty(&clk->core->children)) {
2738 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002739 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002740
2741 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002742 hlist_for_each_entry_safe(child, t, &clk->core->children,
2743 child_node)
2744 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002745 }
2746
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002747 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002748
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002749 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002750 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002751 __func__, clk->core->name);
2752 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002753
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002754 clk_prepare_unlock();
2755}
Mark Brown1df5c932012-04-18 09:07:12 +01002756EXPORT_SYMBOL_GPL(clk_unregister);
2757
Stephen Boyd46c87732012-09-24 13:38:04 -07002758static void devm_clk_release(struct device *dev, void *res)
2759{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002760 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002761}
2762
2763/**
2764 * devm_clk_register - resource managed clk_register()
2765 * @dev: device that is registering this clock
2766 * @hw: link to hardware-specific clock data
2767 *
2768 * Managed clk_register(). Clocks returned from this function are
2769 * automatically clk_unregister()ed on driver detach. See clk_register() for
2770 * more information.
2771 */
2772struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2773{
2774 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002775 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002776
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002777 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2778 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002779 return ERR_PTR(-ENOMEM);
2780
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002781 clk = clk_register(dev, hw);
2782 if (!IS_ERR(clk)) {
2783 *clkp = clk;
2784 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002785 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002786 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002787 }
2788
2789 return clk;
2790}
2791EXPORT_SYMBOL_GPL(devm_clk_register);
2792
2793static int devm_clk_match(struct device *dev, void *res, void *data)
2794{
2795 struct clk *c = res;
2796 if (WARN_ON(!c))
2797 return 0;
2798 return c == data;
2799}
2800
2801/**
2802 * devm_clk_unregister - resource managed clk_unregister()
2803 * @clk: clock to unregister
2804 *
2805 * Deallocate a clock allocated with devm_clk_register(). Normally
2806 * this function will not need to be called and the resource management
2807 * code will ensure that the resource is freed.
2808 */
2809void devm_clk_unregister(struct device *dev, struct clk *clk)
2810{
2811 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2812}
2813EXPORT_SYMBOL_GPL(devm_clk_unregister);
2814
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002815/*
2816 * clkdev helpers
2817 */
2818int __clk_get(struct clk *clk)
2819{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002820 struct clk_core *core = !clk ? NULL : clk->core;
2821
2822 if (core) {
2823 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002824 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002825
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002826 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002827 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002828 return 1;
2829}
2830
2831void __clk_put(struct clk *clk)
2832{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002833 struct module *owner;
2834
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002835 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002836 return;
2837
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002838 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002839
Stephen Boyd50595f82015-02-06 11:42:44 -08002840 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002841 if (clk->min_rate > clk->core->req_rate ||
2842 clk->max_rate < clk->core->req_rate)
2843 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2844
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002845 owner = clk->core->owner;
2846 kref_put(&clk->core->ref, __clk_release);
2847
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002848 clk_prepare_unlock();
2849
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002850 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002851
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002852 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002853}
2854
Mike Turquetteb24764902012-03-15 23:11:19 -07002855/*** clk rate change notifiers ***/
2856
2857/**
2858 * clk_notifier_register - add a clk rate change notifier
2859 * @clk: struct clk * to watch
2860 * @nb: struct notifier_block * with callback info
2861 *
2862 * Request notification when clk's rate changes. This uses an SRCU
2863 * notifier because we want it to block and notifier unregistrations are
2864 * uncommon. The callbacks associated with the notifier must not
2865 * re-enter into the clk framework by calling any top-level clk APIs;
2866 * this will cause a nested prepare_lock mutex.
2867 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002868 * In all notification cases cases (pre, post and abort rate change) the
2869 * original clock rate is passed to the callback via struct
2870 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002871 * clk_notifier_data.new_rate.
2872 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002873 * clk_notifier_register() must be called from non-atomic context.
2874 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2875 * allocation failure; otherwise, passes along the return value of
2876 * srcu_notifier_chain_register().
2877 */
2878int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2879{
2880 struct clk_notifier *cn;
2881 int ret = -ENOMEM;
2882
2883 if (!clk || !nb)
2884 return -EINVAL;
2885
Mike Turquetteeab89f62013-03-28 13:59:01 -07002886 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002887
2888 /* search the list of notifiers for this clk */
2889 list_for_each_entry(cn, &clk_notifier_list, node)
2890 if (cn->clk == clk)
2891 break;
2892
2893 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2894 if (cn->clk != clk) {
2895 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2896 if (!cn)
2897 goto out;
2898
2899 cn->clk = clk;
2900 srcu_init_notifier_head(&cn->notifier_head);
2901
2902 list_add(&cn->node, &clk_notifier_list);
2903 }
2904
2905 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2906
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002907 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002908
2909out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002910 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002911
2912 return ret;
2913}
2914EXPORT_SYMBOL_GPL(clk_notifier_register);
2915
2916/**
2917 * clk_notifier_unregister - remove a clk rate change notifier
2918 * @clk: struct clk *
2919 * @nb: struct notifier_block * with callback info
2920 *
2921 * Request no further notification for changes to 'clk' and frees memory
2922 * allocated in clk_notifier_register.
2923 *
2924 * Returns -EINVAL if called with null arguments; otherwise, passes
2925 * along the return value of srcu_notifier_chain_unregister().
2926 */
2927int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2928{
2929 struct clk_notifier *cn = NULL;
2930 int ret = -EINVAL;
2931
2932 if (!clk || !nb)
2933 return -EINVAL;
2934
Mike Turquetteeab89f62013-03-28 13:59:01 -07002935 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002936
2937 list_for_each_entry(cn, &clk_notifier_list, node)
2938 if (cn->clk == clk)
2939 break;
2940
2941 if (cn->clk == clk) {
2942 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2943
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002944 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002945
2946 /* XXX the notifier code should handle this better */
2947 if (!cn->notifier_head.head) {
2948 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002949 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002950 kfree(cn);
2951 }
2952
2953 } else {
2954 ret = -ENOENT;
2955 }
2956
Mike Turquetteeab89f62013-03-28 13:59:01 -07002957 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002958
2959 return ret;
2960}
2961EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002962
2963#ifdef CONFIG_OF
2964/**
2965 * struct of_clk_provider - Clock provider registration structure
2966 * @link: Entry in global list of clock providers
2967 * @node: Pointer to device tree node of clock provider
2968 * @get: Get clock callback. Returns NULL or a struct clk for the
2969 * given clock specifier
2970 * @data: context pointer to be passed into @get callback
2971 */
2972struct of_clk_provider {
2973 struct list_head link;
2974
2975 struct device_node *node;
2976 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2977 void *data;
2978};
2979
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302980static const struct of_device_id __clk_of_table_sentinel
2981 __used __section(__clk_of_table_end);
2982
Grant Likely766e6a42012-04-09 14:50:06 -05002983static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002984static DEFINE_MUTEX(of_clk_mutex);
2985
Grant Likely766e6a42012-04-09 14:50:06 -05002986struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2987 void *data)
2988{
2989 return data;
2990}
2991EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2992
Shawn Guo494bfec2012-08-22 21:36:27 +08002993struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2994{
2995 struct clk_onecell_data *clk_data = data;
2996 unsigned int idx = clkspec->args[0];
2997
2998 if (idx >= clk_data->clk_num) {
2999 pr_err("%s: invalid clock index %d\n", __func__, idx);
3000 return ERR_PTR(-EINVAL);
3001 }
3002
3003 return clk_data->clks[idx];
3004}
3005EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3006
Grant Likely766e6a42012-04-09 14:50:06 -05003007/**
3008 * of_clk_add_provider() - Register a clock provider for a node
3009 * @np: Device node pointer associated with clock provider
3010 * @clk_src_get: callback for decoding clock
3011 * @data: context pointer for @clk_src_get callback.
3012 */
3013int of_clk_add_provider(struct device_node *np,
3014 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3015 void *data),
3016 void *data)
3017{
3018 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003019 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003020
3021 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
3022 if (!cp)
3023 return -ENOMEM;
3024
3025 cp->node = of_node_get(np);
3026 cp->data = data;
3027 cp->get = clk_src_get;
3028
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003029 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003030 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003031 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003032 pr_debug("Added clock from %s\n", np->full_name);
3033
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003034 ret = of_clk_set_defaults(np, true);
3035 if (ret < 0)
3036 of_clk_del_provider(np);
3037
3038 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003039}
3040EXPORT_SYMBOL_GPL(of_clk_add_provider);
3041
3042/**
3043 * of_clk_del_provider() - Remove a previously registered clock provider
3044 * @np: Device node pointer associated with clock provider
3045 */
3046void of_clk_del_provider(struct device_node *np)
3047{
3048 struct of_clk_provider *cp;
3049
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003050 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003051 list_for_each_entry(cp, &of_clk_providers, link) {
3052 if (cp->node == np) {
3053 list_del(&cp->link);
3054 of_node_put(cp->node);
3055 kfree(cp);
3056 break;
3057 }
3058 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003059 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003060}
3061EXPORT_SYMBOL_GPL(of_clk_del_provider);
3062
Stephen Boyd73e0e492015-02-06 11:42:43 -08003063struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3064 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05003065{
3066 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01003067 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05003068
Stephen Boyd306c3422015-02-05 15:39:11 -08003069 if (!clkspec)
3070 return ERR_PTR(-EINVAL);
3071
Grant Likely766e6a42012-04-09 14:50:06 -05003072 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08003073 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003074 list_for_each_entry(provider, &of_clk_providers, link) {
3075 if (provider->node == clkspec->np)
3076 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08003077 if (!IS_ERR(clk)) {
3078 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
3079 con_id);
3080
3081 if (!IS_ERR(clk) && !__clk_get(clk)) {
3082 __clk_free_clk(clk);
3083 clk = ERR_PTR(-ENOENT);
3084 }
3085
Grant Likely766e6a42012-04-09 14:50:06 -05003086 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08003087 }
Grant Likely766e6a42012-04-09 14:50:06 -05003088 }
Stephen Boyd306c3422015-02-05 15:39:11 -08003089 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003090
3091 return clk;
3092}
3093
Stephen Boyd306c3422015-02-05 15:39:11 -08003094/**
3095 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3096 * @clkspec: pointer to a clock specifier data structure
3097 *
3098 * This function looks up a struct clk from the registered list of clock
3099 * providers, an input is a clock specifier data structure as returned
3100 * from the of_parse_phandle_with_args() function call.
3101 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003102struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3103{
Stephen Boyd306c3422015-02-05 15:39:11 -08003104 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05003105}
3106
Mike Turquettef6102742013-10-07 23:12:13 -07003107int of_clk_get_parent_count(struct device_node *np)
3108{
3109 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
3110}
3111EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3112
Grant Likely766e6a42012-04-09 14:50:06 -05003113const char *of_clk_get_parent_name(struct device_node *np, int index)
3114{
3115 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003116 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003117 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003118 const __be32 *vp;
3119 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003120 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003121 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05003122
3123 if (index < 0)
3124 return NULL;
3125
3126 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3127 &clkspec);
3128 if (rc)
3129 return NULL;
3130
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003131 index = clkspec.args_count ? clkspec.args[0] : 0;
3132 count = 0;
3133
3134 /* if there is an indices property, use it to transfer the index
3135 * specified into an array offset for the clock-output-names property.
3136 */
3137 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3138 if (index == pv) {
3139 index = count;
3140 break;
3141 }
3142 count++;
3143 }
3144
Grant Likely766e6a42012-04-09 14:50:06 -05003145 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003146 index,
Grant Likely766e6a42012-04-09 14:50:06 -05003147 &clk_name) < 0)
3148 clk_name = clkspec.np->name;
3149
3150 of_node_put(clkspec.np);
3151 return clk_name;
3152}
3153EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3154
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05003155/**
3156 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3157 * number of parents
3158 * @np: Device node pointer associated with clock provider
3159 * @parents: pointer to char array that hold the parents' names
3160 * @size: size of the @parents array
3161 *
3162 * Return: number of parents for the clock node.
3163 */
3164int of_clk_parent_fill(struct device_node *np, const char **parents,
3165 unsigned int size)
3166{
3167 unsigned int i = 0;
3168
3169 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3170 i++;
3171
3172 return i;
3173}
3174EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3175
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003176struct clock_provider {
3177 of_clk_init_cb_t clk_init_cb;
3178 struct device_node *np;
3179 struct list_head node;
3180};
3181
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003182/*
3183 * This function looks for a parent clock. If there is one, then it
3184 * checks that the provider for this parent clock was initialized, in
3185 * this case the parent clock will be ready.
3186 */
3187static int parent_ready(struct device_node *np)
3188{
3189 int i = 0;
3190
3191 while (true) {
3192 struct clk *clk = of_clk_get(np, i);
3193
3194 /* this parent is ready we can check the next one */
3195 if (!IS_ERR(clk)) {
3196 clk_put(clk);
3197 i++;
3198 continue;
3199 }
3200
3201 /* at least one parent is not ready, we exit now */
3202 if (PTR_ERR(clk) == -EPROBE_DEFER)
3203 return 0;
3204
3205 /*
3206 * Here we make assumption that the device tree is
3207 * written correctly. So an error means that there is
3208 * no more parent. As we didn't exit yet, then the
3209 * previous parent are ready. If there is no clock
3210 * parent, no need to wait for them, then we can
3211 * consider their absence as being ready
3212 */
3213 return 1;
3214 }
3215}
3216
Grant Likely766e6a42012-04-09 14:50:06 -05003217/**
3218 * of_clk_init() - Scan and init clock providers from the DT
3219 * @matches: array of compatible values and init functions for providers.
3220 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003221 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003222 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003223 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003224 */
3225void __init of_clk_init(const struct of_device_id *matches)
3226{
Alex Elder7f7ed582013-08-22 11:31:31 -05003227 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003228 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003229 struct clock_provider *clk_provider, *next;
3230 bool is_init_done;
3231 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07003232 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05003233
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303234 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003235 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303236
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003237 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003238 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003239 struct clock_provider *parent;
3240
3241 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
3242 if (!parent) {
3243 list_for_each_entry_safe(clk_provider, next,
3244 &clk_provider_list, node) {
3245 list_del(&clk_provider->node);
3246 kfree(clk_provider);
3247 }
3248 return;
3249 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003250
3251 parent->clk_init_cb = match->data;
3252 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003253 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003254 }
3255
3256 while (!list_empty(&clk_provider_list)) {
3257 is_init_done = false;
3258 list_for_each_entry_safe(clk_provider, next,
3259 &clk_provider_list, node) {
3260 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003261
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003262 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003263 of_clk_set_defaults(clk_provider->np, true);
3264
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003265 list_del(&clk_provider->node);
3266 kfree(clk_provider);
3267 is_init_done = true;
3268 }
3269 }
3270
3271 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003272 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003273 * remaining providers during the last loop, so now we
3274 * initialize all the remaining ones unconditionally
3275 * in case the clock parent was not mandatory
3276 */
3277 if (!is_init_done)
3278 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003279 }
3280}
3281#endif