blob: c8ea2dd32251d1a1db4342c75801b7b40b3ce4a0 [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>
Marek Szyprowski9a34b452017-08-21 10:04:59 +020024#include <linux/pm_runtime.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070025#include <linux/sched.h>
Stephen Boyd562ef0b2015-05-01 12:16:14 -070026#include <linux/clkdev.h>
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +010027#include <linux/stringify.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070028
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020029#include "clk.h"
30
Mike Turquetteb24764902012-03-15 23:11:19 -070031static DEFINE_SPINLOCK(enable_lock);
32static DEFINE_MUTEX(prepare_lock);
33
Mike Turquette533ddeb2013-03-28 13:59:02 -070034static struct task_struct *prepare_owner;
35static struct task_struct *enable_owner;
36
37static int prepare_refcnt;
38static int enable_refcnt;
39
Mike Turquetteb24764902012-03-15 23:11:19 -070040static HLIST_HEAD(clk_root_list);
41static HLIST_HEAD(clk_orphan_list);
42static LIST_HEAD(clk_notifier_list);
43
Michael Turquetteb09d6d92015-01-29 14:22:50 -080044/*** private data structures ***/
45
46struct clk_core {
47 const char *name;
48 const struct clk_ops *ops;
49 struct clk_hw *hw;
50 struct module *owner;
Marek Szyprowski9a34b452017-08-21 10:04:59 +020051 struct device *dev;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080052 struct clk_core *parent;
53 const char **parent_names;
54 struct clk_core **parents;
55 u8 num_parents;
56 u8 new_parent_index;
57 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010058 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080059 unsigned long new_rate;
60 struct clk_core *new_parent;
61 struct clk_core *new_child;
62 unsigned long flags;
Heiko Stuebnere6500342015-04-22 22:53:05 +020063 bool orphan;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080064 unsigned int enable_count;
65 unsigned int prepare_count;
Jerome Brunete55a8392017-12-01 22:51:56 +010066 unsigned int protect_count;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070067 unsigned long min_rate;
68 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080069 unsigned long accuracy;
70 int phase;
71 struct hlist_head children;
72 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010073 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080074 unsigned int notifier_count;
75#ifdef CONFIG_DEBUG_FS
76 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020077 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080078#endif
79 struct kref ref;
80};
81
Stephen Boyddfc202e2015-02-02 14:37:41 -080082#define CREATE_TRACE_POINTS
83#include <trace/events/clk.h>
84
Michael Turquetteb09d6d92015-01-29 14:22:50 -080085struct clk {
86 struct clk_core *core;
87 const char *dev_id;
88 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010089 unsigned long min_rate;
90 unsigned long max_rate;
Jerome Brunet55e9b8b2017-12-01 22:51:59 +010091 unsigned int exclusive_count;
Stephen Boyd50595f82015-02-06 11:42:44 -080092 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080093};
94
Marek Szyprowski9a34b452017-08-21 10:04:59 +020095/*** runtime pm ***/
96static int clk_pm_runtime_get(struct clk_core *core)
97{
98 int ret = 0;
99
100 if (!core->dev)
101 return 0;
102
103 ret = pm_runtime_get_sync(core->dev);
104 return ret < 0 ? ret : 0;
105}
106
107static void clk_pm_runtime_put(struct clk_core *core)
108{
109 if (!core->dev)
110 return;
111
112 pm_runtime_put_sync(core->dev);
113}
114
Mike Turquetteeab89f62013-03-28 13:59:01 -0700115/*** locking ***/
116static void clk_prepare_lock(void)
117{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700118 if (!mutex_trylock(&prepare_lock)) {
119 if (prepare_owner == current) {
120 prepare_refcnt++;
121 return;
122 }
123 mutex_lock(&prepare_lock);
124 }
125 WARN_ON_ONCE(prepare_owner != NULL);
126 WARN_ON_ONCE(prepare_refcnt != 0);
127 prepare_owner = current;
128 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700129}
130
131static void clk_prepare_unlock(void)
132{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700133 WARN_ON_ONCE(prepare_owner != current);
134 WARN_ON_ONCE(prepare_refcnt == 0);
135
136 if (--prepare_refcnt)
137 return;
138 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700139 mutex_unlock(&prepare_lock);
140}
141
142static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700143 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700144{
145 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700146
147 if (!spin_trylock_irqsave(&enable_lock, flags)) {
148 if (enable_owner == current) {
149 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700150 __acquire(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700151 return flags;
152 }
153 spin_lock_irqsave(&enable_lock, flags);
154 }
155 WARN_ON_ONCE(enable_owner != NULL);
156 WARN_ON_ONCE(enable_refcnt != 0);
157 enable_owner = current;
158 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700159 return flags;
160}
161
162static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700163 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700164{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700165 WARN_ON_ONCE(enable_owner != current);
166 WARN_ON_ONCE(enable_refcnt == 0);
167
Stephen Boyda57aa182015-07-24 12:24:48 -0700168 if (--enable_refcnt) {
169 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700170 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700171 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700172 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700173 spin_unlock_irqrestore(&enable_lock, flags);
174}
175
Jerome Brunete55a8392017-12-01 22:51:56 +0100176static bool clk_core_rate_is_protected(struct clk_core *core)
177{
178 return core->protect_count;
179}
180
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700181static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530182{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200183 bool ret = false;
184
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700185 /*
186 * .is_prepared is optional for clocks that can prepare
187 * fall back to software usage counter if it is missing
188 */
189 if (!core->ops->is_prepared)
190 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530191
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200192 if (!clk_pm_runtime_get(core)) {
193 ret = core->ops->is_prepared(core->hw);
194 clk_pm_runtime_put(core);
195 }
196
197 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530198}
199
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700200static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530201{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200202 bool ret = false;
203
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700204 /*
205 * .is_enabled is only mandatory for clocks that gate
206 * fall back to software usage counter if .is_enabled is missing
207 */
208 if (!core->ops->is_enabled)
209 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530210
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200211 /*
212 * Check if clock controller's device is runtime active before
213 * calling .is_enabled callback. If not, assume that clock is
214 * disabled, because we might be called from atomic context, from
215 * which pm_runtime_get() is not allowed.
216 * This function is called mainly from clk_disable_unused_subtree,
217 * which ensures proper runtime pm activation of controller before
218 * taking enable spinlock, but the below check is needed if one tries
219 * to call it from other places.
220 */
221 if (core->dev) {
222 pm_runtime_get_noresume(core->dev);
223 if (!pm_runtime_active(core->dev)) {
224 ret = false;
225 goto done;
226 }
227 }
228
229 ret = core->ops->is_enabled(core->hw);
230done:
231 clk_pm_runtime_put(core);
232
233 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530234}
235
Mike Turquetteb24764902012-03-15 23:11:19 -0700236/*** helper functions ***/
237
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200238const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700239{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100240 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700241}
Niels de Vos48950842012-12-13 13:12:25 +0100242EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700243
Stephen Boyde7df6f62015-08-12 13:04:56 -0700244const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700245{
246 return hw->core->name;
247}
248EXPORT_SYMBOL_GPL(clk_hw_get_name);
249
Russ Dill65800b22012-11-26 11:20:09 -0800250struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700251{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100252 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700253}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800254EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700255
Stephen Boyde7df6f62015-08-12 13:04:56 -0700256unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700257{
258 return hw->core->num_parents;
259}
260EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
261
Stephen Boyde7df6f62015-08-12 13:04:56 -0700262struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700263{
264 return hw->core->parent ? hw->core->parent->hw : NULL;
265}
266EXPORT_SYMBOL_GPL(clk_hw_get_parent);
267
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700268static struct clk_core *__clk_lookup_subtree(const char *name,
269 struct clk_core *core)
270{
271 struct clk_core *child;
272 struct clk_core *ret;
273
274 if (!strcmp(core->name, name))
275 return core;
276
277 hlist_for_each_entry(child, &core->children, child_node) {
278 ret = __clk_lookup_subtree(name, child);
279 if (ret)
280 return ret;
281 }
282
283 return NULL;
284}
285
286static struct clk_core *clk_core_lookup(const char *name)
287{
288 struct clk_core *root_clk;
289 struct clk_core *ret;
290
291 if (!name)
292 return NULL;
293
294 /* search the 'proper' clk tree first */
295 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
296 ret = __clk_lookup_subtree(name, root_clk);
297 if (ret)
298 return ret;
299 }
300
301 /* if not found, then search the orphan tree */
302 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
303 ret = __clk_lookup_subtree(name, root_clk);
304 if (ret)
305 return ret;
306 }
307
308 return NULL;
309}
310
Stephen Boydd6968fc2015-04-30 13:54:13 -0700311static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100312 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100313{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700314 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100315 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900316
317 if (!core->parents[index])
318 core->parents[index] =
319 clk_core_lookup(core->parent_names[index]);
320
321 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100322}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100323
Stephen Boyde7df6f62015-08-12 13:04:56 -0700324struct clk_hw *
325clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700326{
327 struct clk_core *parent;
328
329 parent = clk_core_get_parent_by_index(hw->core, index);
330
331 return !parent ? NULL : parent->hw;
332}
333EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
334
Russ Dill65800b22012-11-26 11:20:09 -0800335unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700336{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100337 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700338}
339
Stephen Boydd6968fc2015-04-30 13:54:13 -0700340static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700341{
342 unsigned long ret;
343
Stephen Boydd6968fc2015-04-30 13:54:13 -0700344 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530345 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700346 goto out;
347 }
348
Stephen Boydd6968fc2015-04-30 13:54:13 -0700349 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700350
Stephen Boyd47b0eeb2016-02-02 17:24:56 -0800351 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -0700352 goto out;
353
Stephen Boydd6968fc2015-04-30 13:54:13 -0700354 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530355 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700356
357out:
358 return ret;
359}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100360
Stephen Boyde7df6f62015-08-12 13:04:56 -0700361unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700362{
363 return clk_core_get_rate_nolock(hw->core);
364}
365EXPORT_SYMBOL_GPL(clk_hw_get_rate);
366
Stephen Boydd6968fc2015-04-30 13:54:13 -0700367static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100368{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700369 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100370 return 0;
371
Stephen Boydd6968fc2015-04-30 13:54:13 -0700372 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100373}
374
Russ Dill65800b22012-11-26 11:20:09 -0800375unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700376{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100377 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700378}
Thierry Redingb05c6832013-09-03 09:43:51 +0200379EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700380
Stephen Boyde7df6f62015-08-12 13:04:56 -0700381unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700382{
383 return hw->core->flags;
384}
385EXPORT_SYMBOL_GPL(clk_hw_get_flags);
386
Stephen Boyde7df6f62015-08-12 13:04:56 -0700387bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700388{
389 return clk_core_is_prepared(hw->core);
390}
391
Jerome Brunete55a8392017-12-01 22:51:56 +0100392bool clk_hw_rate_is_protected(const struct clk_hw *hw)
393{
394 return clk_core_rate_is_protected(hw->core);
395}
396
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200397bool clk_hw_is_enabled(const struct clk_hw *hw)
398{
399 return clk_core_is_enabled(hw->core);
400}
401
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100402bool __clk_is_enabled(struct clk *clk)
403{
404 if (!clk)
405 return false;
406
407 return clk_core_is_enabled(clk->core);
408}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800409EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700410
Stephen Boyd15a02c12015-01-19 18:05:28 -0800411static bool mux_is_better_rate(unsigned long rate, unsigned long now,
412 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100413{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800414 if (flags & CLK_MUX_ROUND_CLOSEST)
415 return abs(now - rate) < abs(best - rate);
416
417 return now <= rate && now > best;
418}
419
Boris Brezillon0817b622015-07-07 20:48:08 +0200420static int
421clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800422 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100423{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100424 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200425 int i, num_parents, ret;
426 unsigned long best = 0;
427 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100428
429 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100430 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
431 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200432 if (core->flags & CLK_SET_RATE_PARENT) {
433 ret = __clk_determine_rate(parent ? parent->hw : NULL,
434 &parent_req);
435 if (ret)
436 return ret;
437
438 best = parent_req.rate;
439 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100440 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200441 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100442 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200443 }
444
James Hogane366fdd2013-07-29 12:25:02 +0100445 goto out;
446 }
447
448 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100449 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100450 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100451 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100452 if (!parent)
453 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200454
455 if (core->flags & CLK_SET_RATE_PARENT) {
456 parent_req = *req;
457 ret = __clk_determine_rate(parent->hw, &parent_req);
458 if (ret)
459 continue;
460 } else {
461 parent_req.rate = clk_core_get_rate_nolock(parent);
462 }
463
464 if (mux_is_better_rate(req->rate, parent_req.rate,
465 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100466 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200467 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100468 }
469 }
470
Boris Brezillon57d866e2015-07-09 22:39:38 +0200471 if (!best_parent)
472 return -EINVAL;
473
James Hogane366fdd2013-07-29 12:25:02 +0100474out:
475 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200476 req->best_parent_hw = best_parent->hw;
477 req->best_parent_rate = best;
478 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100479
Boris Brezillon0817b622015-07-07 20:48:08 +0200480 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100481}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800482
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100483struct clk *__clk_lookup(const char *name)
484{
485 struct clk_core *core = clk_core_lookup(name);
486
487 return !core ? NULL : core->hw->clk;
488}
489
Stephen Boydd6968fc2015-04-30 13:54:13 -0700490static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100491 unsigned long *min_rate,
492 unsigned long *max_rate)
493{
494 struct clk *clk_user;
495
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700496 *min_rate = core->min_rate;
497 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100498
Stephen Boydd6968fc2015-04-30 13:54:13 -0700499 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100500 *min_rate = max(*min_rate, clk_user->min_rate);
501
Stephen Boydd6968fc2015-04-30 13:54:13 -0700502 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100503 *max_rate = min(*max_rate, clk_user->max_rate);
504}
505
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700506void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
507 unsigned long max_rate)
508{
509 hw->core->min_rate = min_rate;
510 hw->core->max_rate = max_rate;
511}
512EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
513
Stephen Boyd15a02c12015-01-19 18:05:28 -0800514/*
515 * Helper for finding best parent to provide a given frequency. This can be used
516 * directly as a determine_rate callback (e.g. for a mux), or from a more
517 * complex clock that may combine a mux with other operations.
518 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200519int __clk_mux_determine_rate(struct clk_hw *hw,
520 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800521{
Boris Brezillon0817b622015-07-07 20:48:08 +0200522 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800523}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800524EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100525
Boris Brezillon0817b622015-07-07 20:48:08 +0200526int __clk_mux_determine_rate_closest(struct clk_hw *hw,
527 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800528{
Boris Brezillon0817b622015-07-07 20:48:08 +0200529 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800530}
531EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
532
Mike Turquetteb24764902012-03-15 23:11:19 -0700533/*** clk api ***/
534
Jerome Brunete55a8392017-12-01 22:51:56 +0100535static void clk_core_rate_unprotect(struct clk_core *core)
536{
537 lockdep_assert_held(&prepare_lock);
538
539 if (!core)
540 return;
541
542 if (WARN_ON(core->protect_count == 0))
543 return;
544
545 if (--core->protect_count > 0)
546 return;
547
548 clk_core_rate_unprotect(core->parent);
549}
550
551static int clk_core_rate_nuke_protect(struct clk_core *core)
552{
553 int ret;
554
555 lockdep_assert_held(&prepare_lock);
556
557 if (!core)
558 return -EINVAL;
559
560 if (core->protect_count == 0)
561 return 0;
562
563 ret = core->protect_count;
564 core->protect_count = 1;
565 clk_core_rate_unprotect(core);
566
567 return ret;
568}
569
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100570/**
571 * clk_rate_exclusive_put - release exclusivity over clock rate control
572 * @clk: the clk over which the exclusivity is released
573 *
574 * clk_rate_exclusive_put() completes a critical section during which a clock
575 * consumer cannot tolerate any other consumer making any operation on the
576 * clock which could result in a rate change or rate glitch. Exclusive clocks
577 * cannot have their rate changed, either directly or indirectly due to changes
578 * further up the parent chain of clocks. As a result, clocks up parent chain
579 * also get under exclusive control of the calling consumer.
580 *
581 * If exlusivity is claimed more than once on clock, even by the same consumer,
582 * the rate effectively gets locked as exclusivity can't be preempted.
583 *
584 * Calls to clk_rate_exclusive_put() must be balanced with calls to
585 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
586 * error status.
587 */
588void clk_rate_exclusive_put(struct clk *clk)
589{
590 if (!clk)
591 return;
592
593 clk_prepare_lock();
594
595 /*
596 * if there is something wrong with this consumer protect count, stop
597 * here before messing with the provider
598 */
599 if (WARN_ON(clk->exclusive_count <= 0))
600 goto out;
601
602 clk_core_rate_unprotect(clk->core);
603 clk->exclusive_count--;
604out:
605 clk_prepare_unlock();
606}
607EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
608
Jerome Brunete55a8392017-12-01 22:51:56 +0100609static void clk_core_rate_protect(struct clk_core *core)
610{
611 lockdep_assert_held(&prepare_lock);
612
613 if (!core)
614 return;
615
616 if (core->protect_count == 0)
617 clk_core_rate_protect(core->parent);
618
619 core->protect_count++;
620}
621
622static void clk_core_rate_restore_protect(struct clk_core *core, int count)
623{
624 lockdep_assert_held(&prepare_lock);
625
626 if (!core)
627 return;
628
629 if (count == 0)
630 return;
631
632 clk_core_rate_protect(core);
633 core->protect_count = count;
634}
635
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100636/**
637 * clk_rate_exclusive_get - get exclusivity over the clk rate control
638 * @clk: the clk over which the exclusity of rate control is requested
639 *
640 * clk_rate_exlusive_get() begins a critical section during which a clock
641 * consumer cannot tolerate any other consumer making any operation on the
642 * clock which could result in a rate change or rate glitch. Exclusive clocks
643 * cannot have their rate changed, either directly or indirectly due to changes
644 * further up the parent chain of clocks. As a result, clocks up parent chain
645 * also get under exclusive control of the calling consumer.
646 *
647 * If exlusivity is claimed more than once on clock, even by the same consumer,
648 * the rate effectively gets locked as exclusivity can't be preempted.
649 *
650 * Calls to clk_rate_exclusive_get() should be balanced with calls to
651 * clk_rate_exclusive_put(). Calls to this function may sleep.
652 * Returns 0 on success, -EERROR otherwise
653 */
654int clk_rate_exclusive_get(struct clk *clk)
655{
656 if (!clk)
657 return 0;
658
659 clk_prepare_lock();
660 clk_core_rate_protect(clk->core);
661 clk->exclusive_count++;
662 clk_prepare_unlock();
663
664 return 0;
665}
666EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
667
Stephen Boydd6968fc2015-04-30 13:54:13 -0700668static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700669{
Stephen Boyda6334722015-05-06 17:00:54 -0700670 lockdep_assert_held(&prepare_lock);
671
Stephen Boydd6968fc2015-04-30 13:54:13 -0700672 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700673 return;
674
Stephen Boydd6968fc2015-04-30 13:54:13 -0700675 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700676 return;
677
Lee Jones2e20fbf2016-02-11 13:19:10 -0800678 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
679 return;
680
Stephen Boydd6968fc2015-04-30 13:54:13 -0700681 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700682 return;
683
Stephen Boydd6968fc2015-04-30 13:54:13 -0700684 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700685
Stephen Boydd6968fc2015-04-30 13:54:13 -0700686 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800687
Stephen Boydd6968fc2015-04-30 13:54:13 -0700688 if (core->ops->unprepare)
689 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700690
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200691 clk_pm_runtime_put(core);
692
Stephen Boydd6968fc2015-04-30 13:54:13 -0700693 trace_clk_unprepare_complete(core);
694 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700695}
696
Dong Aishenga6adc302016-06-30 17:31:11 +0800697static void clk_core_unprepare_lock(struct clk_core *core)
698{
699 clk_prepare_lock();
700 clk_core_unprepare(core);
701 clk_prepare_unlock();
702}
703
Mike Turquetteb24764902012-03-15 23:11:19 -0700704/**
705 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200706 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700707 *
708 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
709 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
710 * if the operation may sleep. One example is a clk which is accessed over
711 * I2c. In the complex case a clk gate operation may require a fast and a slow
712 * part. It is this reason that clk_unprepare and clk_disable are not mutually
713 * exclusive. In fact clk_disable must be called before clk_unprepare.
714 */
715void clk_unprepare(struct clk *clk)
716{
Stephen Boyd63589e92014-03-26 16:06:37 -0700717 if (IS_ERR_OR_NULL(clk))
718 return;
719
Dong Aishenga6adc302016-06-30 17:31:11 +0800720 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700721}
722EXPORT_SYMBOL_GPL(clk_unprepare);
723
Stephen Boydd6968fc2015-04-30 13:54:13 -0700724static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700725{
726 int ret = 0;
727
Stephen Boyda6334722015-05-06 17:00:54 -0700728 lockdep_assert_held(&prepare_lock);
729
Stephen Boydd6968fc2015-04-30 13:54:13 -0700730 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700731 return 0;
732
Stephen Boydd6968fc2015-04-30 13:54:13 -0700733 if (core->prepare_count == 0) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200734 ret = clk_pm_runtime_get(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700735 if (ret)
736 return ret;
737
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200738 ret = clk_core_prepare(core->parent);
739 if (ret)
740 goto runtime_put;
741
Stephen Boydd6968fc2015-04-30 13:54:13 -0700742 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800743
Stephen Boydd6968fc2015-04-30 13:54:13 -0700744 if (core->ops->prepare)
745 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800746
Stephen Boydd6968fc2015-04-30 13:54:13 -0700747 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800748
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200749 if (ret)
750 goto unprepare;
Mike Turquetteb24764902012-03-15 23:11:19 -0700751 }
752
Stephen Boydd6968fc2015-04-30 13:54:13 -0700753 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700754
755 return 0;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200756unprepare:
757 clk_core_unprepare(core->parent);
758runtime_put:
759 clk_pm_runtime_put(core);
760 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700761}
762
Dong Aishenga6adc302016-06-30 17:31:11 +0800763static int clk_core_prepare_lock(struct clk_core *core)
764{
765 int ret;
766
767 clk_prepare_lock();
768 ret = clk_core_prepare(core);
769 clk_prepare_unlock();
770
771 return ret;
772}
773
Mike Turquetteb24764902012-03-15 23:11:19 -0700774/**
775 * clk_prepare - prepare a clock source
776 * @clk: the clk being prepared
777 *
778 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
779 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
780 * operation may sleep. One example is a clk which is accessed over I2c. In
781 * the complex case a clk ungate operation may require a fast and a slow part.
782 * It is this reason that clk_prepare and clk_enable are not mutually
783 * exclusive. In fact clk_prepare must be called before clk_enable.
784 * Returns 0 on success, -EERROR otherwise.
785 */
786int clk_prepare(struct clk *clk)
787{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100788 if (!clk)
789 return 0;
790
Dong Aishenga6adc302016-06-30 17:31:11 +0800791 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700792}
793EXPORT_SYMBOL_GPL(clk_prepare);
794
Stephen Boydd6968fc2015-04-30 13:54:13 -0700795static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700796{
Stephen Boyda6334722015-05-06 17:00:54 -0700797 lockdep_assert_held(&enable_lock);
798
Stephen Boydd6968fc2015-04-30 13:54:13 -0700799 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700800 return;
801
Stephen Boydd6968fc2015-04-30 13:54:13 -0700802 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700803 return;
804
Lee Jones2e20fbf2016-02-11 13:19:10 -0800805 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
806 return;
807
Stephen Boydd6968fc2015-04-30 13:54:13 -0700808 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700809 return;
810
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700811 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800812
Stephen Boydd6968fc2015-04-30 13:54:13 -0700813 if (core->ops->disable)
814 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700815
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700816 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800817
Stephen Boydd6968fc2015-04-30 13:54:13 -0700818 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100819}
820
Dong Aishenga6adc302016-06-30 17:31:11 +0800821static void clk_core_disable_lock(struct clk_core *core)
822{
823 unsigned long flags;
824
825 flags = clk_enable_lock();
826 clk_core_disable(core);
827 clk_enable_unlock(flags);
828}
829
Mike Turquetteb24764902012-03-15 23:11:19 -0700830/**
831 * clk_disable - gate a clock
832 * @clk: the clk being gated
833 *
834 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
835 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
836 * clk if the operation is fast and will never sleep. One example is a
837 * SoC-internal clk which is controlled via simple register writes. In the
838 * complex case a clk gate operation may require a fast and a slow part. It is
839 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
840 * In fact clk_disable must be called before clk_unprepare.
841 */
842void clk_disable(struct clk *clk)
843{
Stephen Boyd63589e92014-03-26 16:06:37 -0700844 if (IS_ERR_OR_NULL(clk))
845 return;
846
Dong Aishenga6adc302016-06-30 17:31:11 +0800847 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700848}
849EXPORT_SYMBOL_GPL(clk_disable);
850
Stephen Boydd6968fc2015-04-30 13:54:13 -0700851static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700852{
853 int ret = 0;
854
Stephen Boyda6334722015-05-06 17:00:54 -0700855 lockdep_assert_held(&enable_lock);
856
Stephen Boydd6968fc2015-04-30 13:54:13 -0700857 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700858 return 0;
859
Stephen Boydd6968fc2015-04-30 13:54:13 -0700860 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700861 return -ESHUTDOWN;
862
Stephen Boydd6968fc2015-04-30 13:54:13 -0700863 if (core->enable_count == 0) {
864 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700865
866 if (ret)
867 return ret;
868
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700869 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800870
Stephen Boydd6968fc2015-04-30 13:54:13 -0700871 if (core->ops->enable)
872 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800873
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700874 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800875
876 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700877 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800878 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700879 }
880 }
881
Stephen Boydd6968fc2015-04-30 13:54:13 -0700882 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700883 return 0;
884}
885
Dong Aishenga6adc302016-06-30 17:31:11 +0800886static int clk_core_enable_lock(struct clk_core *core)
887{
888 unsigned long flags;
889 int ret;
890
891 flags = clk_enable_lock();
892 ret = clk_core_enable(core);
893 clk_enable_unlock(flags);
894
895 return ret;
896}
897
Mike Turquetteb24764902012-03-15 23:11:19 -0700898/**
899 * clk_enable - ungate a clock
900 * @clk: the clk being ungated
901 *
902 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
903 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
904 * if the operation will never sleep. One example is a SoC-internal clk which
905 * is controlled via simple register writes. In the complex case a clk ungate
906 * operation may require a fast and a slow part. It is this reason that
907 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
908 * must be called before clk_enable. Returns 0 on success, -EERROR
909 * otherwise.
910 */
911int clk_enable(struct clk *clk)
912{
Dong Aisheng864e1602015-04-30 14:02:19 -0700913 if (!clk)
914 return 0;
915
Dong Aishenga6adc302016-06-30 17:31:11 +0800916 return clk_core_enable_lock(clk->core);
917}
918EXPORT_SYMBOL_GPL(clk_enable);
919
920static int clk_core_prepare_enable(struct clk_core *core)
921{
922 int ret;
923
924 ret = clk_core_prepare_lock(core);
925 if (ret)
926 return ret;
927
928 ret = clk_core_enable_lock(core);
929 if (ret)
930 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700931
932 return ret;
933}
Dong Aishenga6adc302016-06-30 17:31:11 +0800934
935static void clk_core_disable_unprepare(struct clk_core *core)
936{
937 clk_core_disable_lock(core);
938 clk_core_unprepare_lock(core);
939}
Mike Turquetteb24764902012-03-15 23:11:19 -0700940
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800941static void clk_unprepare_unused_subtree(struct clk_core *core)
942{
943 struct clk_core *child;
944
945 lockdep_assert_held(&prepare_lock);
946
947 hlist_for_each_entry(child, &core->children, child_node)
948 clk_unprepare_unused_subtree(child);
949
950 if (core->prepare_count)
951 return;
952
953 if (core->flags & CLK_IGNORE_UNUSED)
954 return;
955
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200956 if (clk_pm_runtime_get(core))
957 return;
958
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800959 if (clk_core_is_prepared(core)) {
960 trace_clk_unprepare(core);
961 if (core->ops->unprepare_unused)
962 core->ops->unprepare_unused(core->hw);
963 else if (core->ops->unprepare)
964 core->ops->unprepare(core->hw);
965 trace_clk_unprepare_complete(core);
966 }
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200967
968 clk_pm_runtime_put(core);
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800969}
970
971static void clk_disable_unused_subtree(struct clk_core *core)
972{
973 struct clk_core *child;
974 unsigned long flags;
975
976 lockdep_assert_held(&prepare_lock);
977
978 hlist_for_each_entry(child, &core->children, child_node)
979 clk_disable_unused_subtree(child);
980
Dong Aishenga4b35182016-06-30 17:31:13 +0800981 if (core->flags & CLK_OPS_PARENT_ENABLE)
982 clk_core_prepare_enable(core->parent);
983
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200984 if (clk_pm_runtime_get(core))
985 goto unprepare_out;
986
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800987 flags = clk_enable_lock();
988
989 if (core->enable_count)
990 goto unlock_out;
991
992 if (core->flags & CLK_IGNORE_UNUSED)
993 goto unlock_out;
994
995 /*
996 * some gate clocks have special needs during the disable-unused
997 * sequence. call .disable_unused if available, otherwise fall
998 * back to .disable
999 */
1000 if (clk_core_is_enabled(core)) {
1001 trace_clk_disable(core);
1002 if (core->ops->disable_unused)
1003 core->ops->disable_unused(core->hw);
1004 else if (core->ops->disable)
1005 core->ops->disable(core->hw);
1006 trace_clk_disable_complete(core);
1007 }
1008
1009unlock_out:
1010 clk_enable_unlock(flags);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001011 clk_pm_runtime_put(core);
1012unprepare_out:
Dong Aishenga4b35182016-06-30 17:31:13 +08001013 if (core->flags & CLK_OPS_PARENT_ENABLE)
1014 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001015}
1016
1017static bool clk_ignore_unused;
1018static int __init clk_ignore_unused_setup(char *__unused)
1019{
1020 clk_ignore_unused = true;
1021 return 1;
1022}
1023__setup("clk_ignore_unused", clk_ignore_unused_setup);
1024
1025static int clk_disable_unused(void)
1026{
1027 struct clk_core *core;
1028
1029 if (clk_ignore_unused) {
1030 pr_warn("clk: Not disabling unused clocks\n");
1031 return 0;
1032 }
1033
1034 clk_prepare_lock();
1035
1036 hlist_for_each_entry(core, &clk_root_list, child_node)
1037 clk_disable_unused_subtree(core);
1038
1039 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1040 clk_disable_unused_subtree(core);
1041
1042 hlist_for_each_entry(core, &clk_root_list, child_node)
1043 clk_unprepare_unused_subtree(core);
1044
1045 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1046 clk_unprepare_unused_subtree(core);
1047
1048 clk_prepare_unlock();
1049
1050 return 0;
1051}
1052late_initcall_sync(clk_disable_unused);
1053
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001054static int clk_core_determine_round_nolock(struct clk_core *core,
1055 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001056{
Boris Brezillon0817b622015-07-07 20:48:08 +02001057 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001058
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001059 lockdep_assert_held(&prepare_lock);
1060
Stephen Boydd6968fc2015-04-30 13:54:13 -07001061 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001062 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001063
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001064 /*
1065 * At this point, core protection will be disabled if
1066 * - if the provider is not protected at all
1067 * - if the calling consumer is the only one which has exclusivity
1068 * over the provider
1069 */
Jerome Brunete55a8392017-12-01 22:51:56 +01001070 if (clk_core_rate_is_protected(core)) {
1071 req->rate = core->rate;
1072 } else if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001073 return core->ops->determine_rate(core->hw, req);
1074 } else if (core->ops->round_rate) {
1075 rate = core->ops->round_rate(core->hw, req->rate,
1076 &req->best_parent_rate);
1077 if (rate < 0)
1078 return rate;
1079
1080 req->rate = rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001081 } else {
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001082 return -EINVAL;
Boris Brezillon0817b622015-07-07 20:48:08 +02001083 }
1084
1085 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001086}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001087
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001088static void clk_core_init_rate_req(struct clk_core * const core,
1089 struct clk_rate_request *req)
1090{
1091 struct clk_core *parent;
1092
1093 if (WARN_ON(!core || !req))
1094 return;
1095
1096 parent = core->parent;
1097 if (parent) {
1098 req->best_parent_hw = parent->hw;
1099 req->best_parent_rate = parent->rate;
1100 } else {
1101 req->best_parent_hw = NULL;
1102 req->best_parent_rate = 0;
1103 }
1104}
1105
1106static bool clk_core_can_round(struct clk_core * const core)
1107{
1108 if (core->ops->determine_rate || core->ops->round_rate)
1109 return true;
1110
1111 return false;
1112}
1113
1114static int clk_core_round_rate_nolock(struct clk_core *core,
1115 struct clk_rate_request *req)
1116{
1117 lockdep_assert_held(&prepare_lock);
1118
1119 if (!core)
1120 return 0;
1121
1122 clk_core_init_rate_req(core, req);
1123
1124 if (clk_core_can_round(core))
1125 return clk_core_determine_round_nolock(core, req);
1126 else if (core->flags & CLK_SET_RATE_PARENT)
1127 return clk_core_round_rate_nolock(core->parent, req);
1128
1129 req->rate = core->rate;
1130 return 0;
1131}
1132
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001133/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001134 * __clk_determine_rate - get the closest rate actually supported by a clock
1135 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001136 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001137 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001138 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001139 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001140int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001141{
Boris Brezillon0817b622015-07-07 20:48:08 +02001142 if (!hw) {
1143 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001144 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001145 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001146
Boris Brezillon0817b622015-07-07 20:48:08 +02001147 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001148}
1149EXPORT_SYMBOL_GPL(__clk_determine_rate);
1150
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001151unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1152{
1153 int ret;
1154 struct clk_rate_request req;
1155
1156 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1157 req.rate = rate;
1158
1159 ret = clk_core_round_rate_nolock(hw->core, &req);
1160 if (ret)
1161 return 0;
1162
1163 return req.rate;
1164}
1165EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1166
Mike Turquetteb24764902012-03-15 23:11:19 -07001167/**
1168 * clk_round_rate - round the given rate for a clk
1169 * @clk: the clk for which we are rounding a rate
1170 * @rate: the rate which is to be rounded
1171 *
1172 * Takes in a rate as input and rounds it to a rate that the clk can actually
1173 * use which is then returned. If clk doesn't support round_rate operation
1174 * then the parent rate is returned.
1175 */
1176long clk_round_rate(struct clk *clk, unsigned long rate)
1177{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001178 struct clk_rate_request req;
1179 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001180
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001181 if (!clk)
1182 return 0;
1183
Mike Turquetteeab89f62013-03-28 13:59:01 -07001184 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001185
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001186 if (clk->exclusive_count)
1187 clk_core_rate_unprotect(clk->core);
1188
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001189 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1190 req.rate = rate;
1191
1192 ret = clk_core_round_rate_nolock(clk->core, &req);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001193
1194 if (clk->exclusive_count)
1195 clk_core_rate_protect(clk->core);
1196
Mike Turquetteeab89f62013-03-28 13:59:01 -07001197 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001198
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001199 if (ret)
1200 return ret;
1201
1202 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001203}
1204EXPORT_SYMBOL_GPL(clk_round_rate);
1205
1206/**
1207 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001208 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001209 * @msg: clk notifier type (see include/linux/clk.h)
1210 * @old_rate: old clk rate
1211 * @new_rate: new clk rate
1212 *
1213 * Triggers a notifier call chain on the clk rate-change notification
1214 * for 'clk'. Passes a pointer to the struct clk and the previous
1215 * and current rates to the notifier callback. Intended to be called by
1216 * internal clock code only. Returns NOTIFY_DONE from the last driver
1217 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1218 * a driver returns that.
1219 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001220static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001221 unsigned long old_rate, unsigned long new_rate)
1222{
1223 struct clk_notifier *cn;
1224 struct clk_notifier_data cnd;
1225 int ret = NOTIFY_DONE;
1226
Mike Turquetteb24764902012-03-15 23:11:19 -07001227 cnd.old_rate = old_rate;
1228 cnd.new_rate = new_rate;
1229
1230 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001231 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001232 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001233 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1234 &cnd);
Peter De Schrijver17c34c52017-03-21 12:16:26 +02001235 if (ret & NOTIFY_STOP_MASK)
1236 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001237 }
1238 }
1239
1240 return ret;
1241}
1242
1243/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001244 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001245 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001246 *
1247 * Walks the subtree of clks starting with clk and recalculates accuracies as
1248 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001249 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001250 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001251 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001252static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001253{
1254 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001255 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001256
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001257 lockdep_assert_held(&prepare_lock);
1258
Stephen Boydd6968fc2015-04-30 13:54:13 -07001259 if (core->parent)
1260 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001261
Stephen Boydd6968fc2015-04-30 13:54:13 -07001262 if (core->ops->recalc_accuracy)
1263 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001264 parent_accuracy);
1265 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001266 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001267
Stephen Boydd6968fc2015-04-30 13:54:13 -07001268 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001269 __clk_recalc_accuracies(child);
1270}
1271
Stephen Boydd6968fc2015-04-30 13:54:13 -07001272static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001273{
1274 unsigned long accuracy;
1275
1276 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001277 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1278 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001279
Stephen Boydd6968fc2015-04-30 13:54:13 -07001280 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001281 clk_prepare_unlock();
1282
1283 return accuracy;
1284}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001285
1286/**
1287 * clk_get_accuracy - return the accuracy of clk
1288 * @clk: the clk whose accuracy is being returned
1289 *
1290 * Simply returns the cached accuracy of the clk, unless
1291 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1292 * issued.
1293 * If clk is NULL then returns 0.
1294 */
1295long clk_get_accuracy(struct clk *clk)
1296{
1297 if (!clk)
1298 return 0;
1299
1300 return clk_core_get_accuracy(clk->core);
1301}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001302EXPORT_SYMBOL_GPL(clk_get_accuracy);
1303
Stephen Boydd6968fc2015-04-30 13:54:13 -07001304static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001305 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001306{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001307 unsigned long rate = parent_rate;
1308
1309 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1310 rate = core->ops->recalc_rate(core->hw, parent_rate);
1311 clk_pm_runtime_put(core);
1312 }
1313 return rate;
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001314}
1315
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001316/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001317 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001318 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001319 * @msg: notification type (see include/linux/clk.h)
1320 *
1321 * Walks the subtree of clks starting with clk and recalculates rates as it
1322 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001323 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001324 *
1325 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1326 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001327 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001328static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001329{
1330 unsigned long old_rate;
1331 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001332 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001333
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001334 lockdep_assert_held(&prepare_lock);
1335
Stephen Boydd6968fc2015-04-30 13:54:13 -07001336 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001337
Stephen Boydd6968fc2015-04-30 13:54:13 -07001338 if (core->parent)
1339 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001340
Stephen Boydd6968fc2015-04-30 13:54:13 -07001341 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001342
1343 /*
1344 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1345 * & ABORT_RATE_CHANGE notifiers
1346 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001347 if (core->notifier_count && msg)
1348 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001349
Stephen Boydd6968fc2015-04-30 13:54:13 -07001350 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001351 __clk_recalc_rates(child, msg);
1352}
1353
Stephen Boydd6968fc2015-04-30 13:54:13 -07001354static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001355{
1356 unsigned long rate;
1357
1358 clk_prepare_lock();
1359
Stephen Boydd6968fc2015-04-30 13:54:13 -07001360 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1361 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001362
Stephen Boydd6968fc2015-04-30 13:54:13 -07001363 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001364 clk_prepare_unlock();
1365
1366 return rate;
1367}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001368
Mike Turquetteb24764902012-03-15 23:11:19 -07001369/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001370 * clk_get_rate - return the rate of clk
1371 * @clk: the clk whose rate is being returned
1372 *
1373 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1374 * is set, which means a recalc_rate will be issued.
1375 * If clk is NULL then returns 0.
1376 */
1377unsigned long clk_get_rate(struct clk *clk)
1378{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001379 if (!clk)
1380 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001381
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001382 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001383}
1384EXPORT_SYMBOL_GPL(clk_get_rate);
1385
Stephen Boydd6968fc2015-04-30 13:54:13 -07001386static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001387 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001388{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001389 int i;
James Hogan4935b222013-07-29 12:24:59 +01001390
Masahiro Yamada508f8842015-12-28 19:23:08 +09001391 if (!parent)
1392 return -EINVAL;
1393
Masahiro Yamada470b5e22015-12-28 19:23:09 +09001394 for (i = 0; i < core->num_parents; i++)
1395 if (clk_core_get_parent_by_index(core, i) == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001396 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001397
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001398 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001399}
1400
Heiko Stuebnere6500342015-04-22 22:53:05 +02001401/*
1402 * Update the orphan status of @core and all its children.
1403 */
1404static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1405{
1406 struct clk_core *child;
1407
1408 core->orphan = is_orphan;
1409
1410 hlist_for_each_entry(child, &core->children, child_node)
1411 clk_core_update_orphan_status(child, is_orphan);
1412}
1413
Stephen Boydd6968fc2015-04-30 13:54:13 -07001414static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001415{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001416 bool was_orphan = core->orphan;
1417
Stephen Boydd6968fc2015-04-30 13:54:13 -07001418 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001419
James Hogan903efc52013-08-29 12:10:51 +01001420 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001421 bool becomes_orphan = new_parent->orphan;
1422
James Hogan903efc52013-08-29 12:10:51 +01001423 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001424 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001425 new_parent->new_child = NULL;
1426
Stephen Boydd6968fc2015-04-30 13:54:13 -07001427 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001428
1429 if (was_orphan != becomes_orphan)
1430 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001431 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001432 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001433 if (!was_orphan)
1434 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001435 }
James Hogan4935b222013-07-29 12:24:59 +01001436
Stephen Boydd6968fc2015-04-30 13:54:13 -07001437 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001438}
1439
Stephen Boydd6968fc2015-04-30 13:54:13 -07001440static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001441 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001442{
1443 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001444 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001445
1446 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001447 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1448 *
1449 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001450 * clk_enable().
1451 *
1452 * If the clock is not prepared, then a race with
1453 * clk_enable/disable() is impossible since we already have the
1454 * prepare lock (future calls to clk_enable() need to be preceded by
1455 * a clk_prepare()).
1456 *
1457 * If the clock is prepared, migrate the prepared state to the new
1458 * parent and also protect against a race with clk_enable() by
1459 * forcing the clock and the new parent on. This ensures that all
1460 * future calls to clk_enable() are practically NOPs with respect to
1461 * hardware and software states.
1462 *
1463 * See also: Comment for clk_set_parent() below.
1464 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001465
1466 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1467 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1468 clk_core_prepare_enable(old_parent);
1469 clk_core_prepare_enable(parent);
1470 }
1471
1472 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001473 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001474 clk_core_prepare_enable(parent);
1475 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001476 }
1477
1478 /* update the clk tree topology */
1479 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001480 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001481 clk_enable_unlock(flags);
1482
Stephen Boyd3fa22522014-01-15 10:47:22 -08001483 return old_parent;
1484}
1485
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001486static void __clk_set_parent_after(struct clk_core *core,
1487 struct clk_core *parent,
1488 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001489{
1490 /*
1491 * Finish the migration of prepare state and undo the changes done
1492 * for preventing a race with clk_enable().
1493 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001494 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001495 clk_core_disable_lock(core);
1496 clk_core_disable_unprepare(old_parent);
1497 }
1498
1499 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1500 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1501 clk_core_disable_unprepare(parent);
1502 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001503 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001504}
1505
Stephen Boydd6968fc2015-04-30 13:54:13 -07001506static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001507 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001508{
1509 unsigned long flags;
1510 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001511 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001512
Stephen Boydd6968fc2015-04-30 13:54:13 -07001513 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001514
Stephen Boydd6968fc2015-04-30 13:54:13 -07001515 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001516
James Hogan4935b222013-07-29 12:24:59 +01001517 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001518 if (parent && core->ops->set_parent)
1519 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001520
Stephen Boydd6968fc2015-04-30 13:54:13 -07001521 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001522
James Hogan4935b222013-07-29 12:24:59 +01001523 if (ret) {
1524 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001525 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001526 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001527 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001528
James Hogan4935b222013-07-29 12:24:59 +01001529 return ret;
1530 }
1531
Stephen Boydd6968fc2015-04-30 13:54:13 -07001532 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001533
James Hogan4935b222013-07-29 12:24:59 +01001534 return 0;
1535}
1536
Ulf Hanssona093bde2012-08-31 14:21:28 +02001537/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001538 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001539 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001540 * @parent_rate: the "future" rate of clk's parent
1541 *
1542 * Walks the subtree of clks starting with clk, speculating rates as it
1543 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1544 *
1545 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1546 * pre-rate change notifications and returns early if no clks in the
1547 * subtree have subscribed to the notifications. Note that if a clk does not
1548 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001549 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001550 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001551static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001552 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001553{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001554 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001555 unsigned long new_rate;
1556 int ret = NOTIFY_DONE;
1557
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001558 lockdep_assert_held(&prepare_lock);
1559
Stephen Boydd6968fc2015-04-30 13:54:13 -07001560 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001561
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001562 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001563 if (core->notifier_count)
1564 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001565
Mike Turquette86bcfa22014-02-24 16:08:41 -08001566 if (ret & NOTIFY_STOP_MASK) {
1567 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001568 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001569 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001570 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001571
Stephen Boydd6968fc2015-04-30 13:54:13 -07001572 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001573 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001574 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001575 break;
1576 }
1577
1578out:
1579 return ret;
1580}
1581
Stephen Boydd6968fc2015-04-30 13:54:13 -07001582static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001583 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001584{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001585 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001586
Stephen Boydd6968fc2015-04-30 13:54:13 -07001587 core->new_rate = new_rate;
1588 core->new_parent = new_parent;
1589 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001590 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001591 core->new_child = NULL;
1592 if (new_parent && new_parent != core->parent)
1593 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001594
Stephen Boydd6968fc2015-04-30 13:54:13 -07001595 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001596 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001597 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001598 }
1599}
1600
1601/*
1602 * calculate the new rates returning the topmost clock that has to be
1603 * changed.
1604 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001605static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001606 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001607{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001608 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001609 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001610 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001611 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001612 unsigned long min_rate;
1613 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001614 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001615 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001616
Mike Turquette7452b212012-03-26 14:45:36 -07001617 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001618 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001619 return NULL;
1620
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001621 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001622 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001623 if (parent)
1624 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001625
Stephen Boydd6968fc2015-04-30 13:54:13 -07001626 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001627
James Hogan71472c02013-07-29 12:25:00 +01001628 /* find the closest rate and parent clk/rate */
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001629 if (clk_core_can_round(core)) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001630 struct clk_rate_request req;
1631
1632 req.rate = rate;
1633 req.min_rate = min_rate;
1634 req.max_rate = max_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001635
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001636 clk_core_init_rate_req(core, &req);
1637
1638 ret = clk_core_determine_round_nolock(core, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001639 if (ret < 0)
1640 return NULL;
1641
Boris Brezillon0817b622015-07-07 20:48:08 +02001642 best_parent_rate = req.best_parent_rate;
1643 new_rate = req.rate;
1644 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001645
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001646 if (new_rate < min_rate || new_rate > max_rate)
1647 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001648 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001649 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001650 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001651 return NULL;
1652 } else {
1653 /* pass-through clock with adjustable parent */
1654 top = clk_calc_new_rates(parent, rate);
1655 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001656 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001657 }
1658
James Hogan71472c02013-07-29 12:25:00 +01001659 /* some clocks must be gated to change parent */
1660 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001661 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001662 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001663 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001664 return NULL;
1665 }
1666
James Hogan71472c02013-07-29 12:25:00 +01001667 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001668 if (parent && core->num_parents > 1) {
1669 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001670 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001671 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001672 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001673 return NULL;
1674 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001675 }
1676
Stephen Boydd6968fc2015-04-30 13:54:13 -07001677 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001678 best_parent_rate != parent->rate)
1679 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001680
1681out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001682 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001683
1684 return top;
1685}
1686
1687/*
1688 * Notify about rate changes in a subtree. Always walk down the whole tree
1689 * so that in case of an error we can walk down the whole tree again and
1690 * abort the change.
1691 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001692static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001693 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001694{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001695 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001696 int ret = NOTIFY_DONE;
1697
Stephen Boydd6968fc2015-04-30 13:54:13 -07001698 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301699 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001700
Stephen Boydd6968fc2015-04-30 13:54:13 -07001701 if (core->notifier_count) {
1702 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001703 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001704 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001705 }
1706
Stephen Boydd6968fc2015-04-30 13:54:13 -07001707 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001708 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001709 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001710 continue;
1711 tmp_clk = clk_propagate_rate_change(child, event);
1712 if (tmp_clk)
1713 fail_clk = tmp_clk;
1714 }
1715
Stephen Boydd6968fc2015-04-30 13:54:13 -07001716 /* handle the new child who might not be in core->children yet */
1717 if (core->new_child) {
1718 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001719 if (tmp_clk)
1720 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001721 }
1722
1723 return fail_clk;
1724}
1725
1726/*
1727 * walk down a subtree and set the new rates notifying the rate
1728 * change on the way
1729 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001730static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001731{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001732 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001733 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001734 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001735 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001736 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001737 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001738 struct clk_core *parent = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001739
Stephen Boydd6968fc2015-04-30 13:54:13 -07001740 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001741
Dong Aishengfc8726a2016-06-30 17:31:14 +08001742 if (core->new_parent) {
1743 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001744 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001745 } else if (core->parent) {
1746 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001747 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001748 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001749
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001750 if (core->flags & CLK_SET_RATE_UNGATE) {
1751 unsigned long flags;
1752
1753 clk_core_prepare(core);
1754 flags = clk_enable_lock();
1755 clk_core_enable(core);
1756 clk_enable_unlock(flags);
1757 }
1758
Stephen Boydd6968fc2015-04-30 13:54:13 -07001759 if (core->new_parent && core->new_parent != core->parent) {
1760 old_parent = __clk_set_parent_before(core, core->new_parent);
1761 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001762
Stephen Boydd6968fc2015-04-30 13:54:13 -07001763 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001764 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001765 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001766 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001767 core->new_parent_index);
1768 } else if (core->ops->set_parent) {
1769 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001770 }
1771
Stephen Boydd6968fc2015-04-30 13:54:13 -07001772 trace_clk_set_parent_complete(core, core->new_parent);
1773 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001774 }
1775
Dong Aishengfc8726a2016-06-30 17:31:14 +08001776 if (core->flags & CLK_OPS_PARENT_ENABLE)
1777 clk_core_prepare_enable(parent);
1778
Stephen Boydd6968fc2015-04-30 13:54:13 -07001779 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001780
Stephen Boydd6968fc2015-04-30 13:54:13 -07001781 if (!skip_set_rate && core->ops->set_rate)
1782 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001783
Stephen Boydd6968fc2015-04-30 13:54:13 -07001784 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001785
Stephen Boydd6968fc2015-04-30 13:54:13 -07001786 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001787
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001788 if (core->flags & CLK_SET_RATE_UNGATE) {
1789 unsigned long flags;
1790
1791 flags = clk_enable_lock();
1792 clk_core_disable(core);
1793 clk_enable_unlock(flags);
1794 clk_core_unprepare(core);
1795 }
1796
Dong Aishengfc8726a2016-06-30 17:31:14 +08001797 if (core->flags & CLK_OPS_PARENT_ENABLE)
1798 clk_core_disable_unprepare(parent);
1799
Stephen Boydd6968fc2015-04-30 13:54:13 -07001800 if (core->notifier_count && old_rate != core->rate)
1801 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001802
Michael Turquette85e88fa2015-06-20 12:18:03 -07001803 if (core->flags & CLK_RECALC_NEW_RATES)
1804 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001805
Tero Kristo067bb172014-08-21 16:47:45 +03001806 /*
1807 * Use safe iteration, as change_rate can actually swap parents
1808 * for certain clock types.
1809 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001810 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001811 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001812 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001813 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001814 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001815 }
1816
Stephen Boydd6968fc2015-04-30 13:54:13 -07001817 /* handle the new child who might not be in core->children yet */
1818 if (core->new_child)
1819 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001820}
1821
Jerome Brunetca5e0892017-12-01 22:51:55 +01001822static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1823 unsigned long req_rate)
1824{
Jerome Brunete55a8392017-12-01 22:51:56 +01001825 int ret, cnt;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001826 struct clk_rate_request req;
1827
1828 lockdep_assert_held(&prepare_lock);
1829
1830 if (!core)
1831 return 0;
1832
Jerome Brunete55a8392017-12-01 22:51:56 +01001833 /* simulate what the rate would be if it could be freely set */
1834 cnt = clk_core_rate_nuke_protect(core);
1835 if (cnt < 0)
1836 return cnt;
1837
Jerome Brunetca5e0892017-12-01 22:51:55 +01001838 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1839 req.rate = req_rate;
1840
1841 ret = clk_core_round_rate_nolock(core, &req);
1842
Jerome Brunete55a8392017-12-01 22:51:56 +01001843 /* restore the protection */
1844 clk_core_rate_restore_protect(core, cnt);
1845
Jerome Brunetca5e0892017-12-01 22:51:55 +01001846 return ret ? 0 : req.rate;
1847}
1848
Stephen Boydd6968fc2015-04-30 13:54:13 -07001849static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001850 unsigned long req_rate)
1851{
1852 struct clk_core *top, *fail_clk;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001853 unsigned long rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001854 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001855
Stephen Boydd6968fc2015-04-30 13:54:13 -07001856 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001857 return 0;
1858
Jerome Brunetca5e0892017-12-01 22:51:55 +01001859 rate = clk_core_req_round_rate_nolock(core, req_rate);
1860
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001861 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001862 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001863 return 0;
1864
Jerome Brunete55a8392017-12-01 22:51:56 +01001865 /* fail on a direct rate set of a protected provider */
1866 if (clk_core_rate_is_protected(core))
1867 return -EBUSY;
1868
Stephen Boydd6968fc2015-04-30 13:54:13 -07001869 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001870 return -EBUSY;
1871
1872 /* calculate new rates and get the topmost changed clock */
Jerome Brunetca5e0892017-12-01 22:51:55 +01001873 top = clk_calc_new_rates(core, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001874 if (!top)
1875 return -EINVAL;
1876
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001877 ret = clk_pm_runtime_get(core);
1878 if (ret)
1879 return ret;
1880
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001881 /* notify that we are about to change rates */
1882 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1883 if (fail_clk) {
1884 pr_debug("%s: failed to set %s rate\n", __func__,
1885 fail_clk->name);
1886 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001887 ret = -EBUSY;
1888 goto err;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001889 }
1890
1891 /* change the rates */
1892 clk_change_rate(top);
1893
Stephen Boydd6968fc2015-04-30 13:54:13 -07001894 core->req_rate = req_rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001895err:
1896 clk_pm_runtime_put(core);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001897
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001898 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001899}
1900
Mike Turquetteb24764902012-03-15 23:11:19 -07001901/**
1902 * clk_set_rate - specify a new rate for clk
1903 * @clk: the clk whose rate is being changed
1904 * @rate: the new rate for clk
1905 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001906 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001907 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001908 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1909 * propagate up to clk's parent; whether or not this happens depends on the
1910 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1911 * after calling .round_rate then upstream parent propagation is ignored. If
1912 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001913 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001914 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1915 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001916 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001917 * Rate changes are accomplished via tree traversal that also recalculates the
1918 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001919 *
1920 * Returns 0 on success, -EERROR otherwise.
1921 */
1922int clk_set_rate(struct clk *clk, unsigned long rate)
1923{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001924 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001925
Mike Turquette89ac8d72013-08-21 23:58:09 -07001926 if (!clk)
1927 return 0;
1928
Mike Turquetteb24764902012-03-15 23:11:19 -07001929 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001930 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001931
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001932 if (clk->exclusive_count)
1933 clk_core_rate_unprotect(clk->core);
1934
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001935 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001936
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001937 if (clk->exclusive_count)
1938 clk_core_rate_protect(clk->core);
1939
Mike Turquetteeab89f62013-03-28 13:59:01 -07001940 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001941
1942 return ret;
1943}
1944EXPORT_SYMBOL_GPL(clk_set_rate);
1945
1946/**
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001947 * clk_set_rate_exclusive - specify a new rate get exclusive control
1948 * @clk: the clk whose rate is being changed
1949 * @rate: the new rate for clk
1950 *
1951 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
1952 * within a critical section
1953 *
1954 * This can be used initially to ensure that at least 1 consumer is
1955 * statisfied when several consumers are competing for exclusivity over the
1956 * same clock provider.
1957 *
1958 * The exclusivity is not applied if setting the rate failed.
1959 *
1960 * Calls to clk_rate_exclusive_get() should be balanced with calls to
1961 * clk_rate_exclusive_put().
1962 *
1963 * Returns 0 on success, -EERROR otherwise.
1964 */
1965int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
1966{
1967 int ret;
1968
1969 if (!clk)
1970 return 0;
1971
1972 /* prevent racing with updates to the clock topology */
1973 clk_prepare_lock();
1974
1975 /*
1976 * The temporary protection removal is not here, on purpose
1977 * This function is meant to be used instead of clk_rate_protect,
1978 * so before the consumer code path protect the clock provider
1979 */
1980
1981 ret = clk_core_set_rate_nolock(clk->core, rate);
1982 if (!ret) {
1983 clk_core_rate_protect(clk->core);
1984 clk->exclusive_count++;
1985 }
1986
1987 clk_prepare_unlock();
1988
1989 return ret;
1990}
1991EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
1992
1993/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001994 * clk_set_rate_range - set a rate range for a clock source
1995 * @clk: clock source
1996 * @min: desired minimum clock rate in Hz, inclusive
1997 * @max: desired maximum clock rate in Hz, inclusive
1998 *
1999 * Returns success (0) or negative errno.
2000 */
2001int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2002{
2003 int ret = 0;
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002004 unsigned long old_min, old_max, rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002005
2006 if (!clk)
2007 return 0;
2008
2009 if (min > max) {
2010 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2011 __func__, clk->core->name, clk->dev_id, clk->con_id,
2012 min, max);
2013 return -EINVAL;
2014 }
2015
2016 clk_prepare_lock();
2017
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002018 if (clk->exclusive_count)
2019 clk_core_rate_unprotect(clk->core);
2020
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002021 /* Save the current values in case we need to rollback the change */
2022 old_min = clk->min_rate;
2023 old_max = clk->max_rate;
2024 clk->min_rate = min;
2025 clk->max_rate = max;
2026
2027 rate = clk_core_get_rate_nolock(clk->core);
2028 if (rate < min || rate > max) {
2029 /*
2030 * FIXME:
2031 * We are in bit of trouble here, current rate is outside the
2032 * the requested range. We are going try to request appropriate
2033 * range boundary but there is a catch. It may fail for the
2034 * usual reason (clock broken, clock protected, etc) but also
2035 * because:
2036 * - round_rate() was not favorable and fell on the wrong
2037 * side of the boundary
2038 * - the determine_rate() callback does not really check for
2039 * this corner case when determining the rate
2040 */
2041
2042 if (rate < min)
2043 rate = min;
2044 else
2045 rate = max;
2046
2047 ret = clk_core_set_rate_nolock(clk->core, rate);
2048 if (ret) {
2049 /* rollback the changes */
2050 clk->min_rate = old_min;
2051 clk->max_rate = old_max;
2052 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002053 }
2054
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002055 if (clk->exclusive_count)
2056 clk_core_rate_protect(clk->core);
2057
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002058 clk_prepare_unlock();
2059
2060 return ret;
2061}
2062EXPORT_SYMBOL_GPL(clk_set_rate_range);
2063
2064/**
2065 * clk_set_min_rate - set a minimum clock rate for a clock source
2066 * @clk: clock source
2067 * @rate: desired minimum clock rate in Hz, inclusive
2068 *
2069 * Returns success (0) or negative errno.
2070 */
2071int clk_set_min_rate(struct clk *clk, unsigned long rate)
2072{
2073 if (!clk)
2074 return 0;
2075
2076 return clk_set_rate_range(clk, rate, clk->max_rate);
2077}
2078EXPORT_SYMBOL_GPL(clk_set_min_rate);
2079
2080/**
2081 * clk_set_max_rate - set a maximum clock rate for a clock source
2082 * @clk: clock source
2083 * @rate: desired maximum clock rate in Hz, inclusive
2084 *
2085 * Returns success (0) or negative errno.
2086 */
2087int clk_set_max_rate(struct clk *clk, unsigned long rate)
2088{
2089 if (!clk)
2090 return 0;
2091
2092 return clk_set_rate_range(clk, clk->min_rate, rate);
2093}
2094EXPORT_SYMBOL_GPL(clk_set_max_rate);
2095
2096/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002097 * clk_get_parent - return the parent of a clk
2098 * @clk: the clk whose parent gets returned
2099 *
2100 * Simply returns clk->parent. Returns NULL if clk is NULL.
2101 */
2102struct clk *clk_get_parent(struct clk *clk)
2103{
2104 struct clk *parent;
2105
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002106 if (!clk)
2107 return NULL;
2108
Mike Turquetteeab89f62013-03-28 13:59:01 -07002109 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002110 /* TODO: Create a per-user clk and change callers to call clk_put */
2111 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002112 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002113
2114 return parent;
2115}
2116EXPORT_SYMBOL_GPL(clk_get_parent);
2117
Stephen Boydd6968fc2015-04-30 13:54:13 -07002118static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002119{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002120 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002121
Masahiro Yamada2430a942016-02-09 20:19:14 +09002122 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002123 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002124
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002125 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002126}
2127
Stephen Boydd6968fc2015-04-30 13:54:13 -07002128static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002129 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002130{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002131 clk_reparent(core, new_parent);
2132 __clk_recalc_accuracies(core);
2133 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002134}
2135
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002136void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2137{
2138 if (!hw)
2139 return;
2140
2141 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2142}
2143
Mike Turquetteb24764902012-03-15 23:11:19 -07002144/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002145 * clk_has_parent - check if a clock is a possible parent for another
2146 * @clk: clock source
2147 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002148 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002149 * This function can be used in drivers that need to check that a clock can be
2150 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002151 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002152 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002153 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002154bool clk_has_parent(struct clk *clk, struct clk *parent)
2155{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002156 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002157 unsigned int i;
2158
2159 /* NULL clocks should be nops, so return success if either is NULL. */
2160 if (!clk || !parent)
2161 return true;
2162
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002163 core = clk->core;
2164 parent_core = parent->core;
2165
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002166 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002167 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002168 return true;
2169
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002170 for (i = 0; i < core->num_parents; i++)
2171 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002172 return true;
2173
2174 return false;
2175}
2176EXPORT_SYMBOL_GPL(clk_has_parent);
2177
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002178static int clk_core_set_parent_nolock(struct clk_core *core,
2179 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002180{
2181 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002182 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002183 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002184
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002185 lockdep_assert_held(&prepare_lock);
2186
Stephen Boydd6968fc2015-04-30 13:54:13 -07002187 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002188 return 0;
2189
Stephen Boydd6968fc2015-04-30 13:54:13 -07002190 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002191 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002192
Stephen Boydb61c43c2015-02-02 14:11:25 -08002193 /* verify ops for for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002194 if (core->num_parents > 1 && !core->ops->set_parent)
2195 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08002196
Ulf Hansson031dcc92013-04-02 23:09:38 +02002197 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002198 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2199 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002200
Jerome Brunete55a8392017-12-01 22:51:56 +01002201 if (clk_core_rate_is_protected(core))
2202 return -EBUSY;
2203
Ulf Hansson031dcc92013-04-02 23:09:38 +02002204 /* try finding the new parent index */
2205 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002206 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002207 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002208 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002209 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002210 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002211 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002212 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002213 }
2214
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002215 ret = clk_pm_runtime_get(core);
2216 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002217 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002218
Mike Turquetteb24764902012-03-15 23:11:19 -07002219 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002220 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002221
2222 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002223 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002224 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07002225
Ulf Hansson031dcc92013-04-02 23:09:38 +02002226 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002227 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002228
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002229 /* propagate rate an accuracy recalculation accordingly */
2230 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002231 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002232 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002233 __clk_recalc_rates(core, POST_RATE_CHANGE);
2234 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002235 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002236
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002237runtime_put:
2238 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002239
2240 return ret;
2241}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002242
2243/**
2244 * clk_set_parent - switch the parent of a mux clk
2245 * @clk: the mux clk whose input we are switching
2246 * @parent: the new input to clk
2247 *
2248 * Re-parent clk to use parent as its new input source. If clk is in
2249 * prepared state, the clk will get enabled for the duration of this call. If
2250 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2251 * that, the reparenting is glitchy in hardware, etc), use the
2252 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2253 *
2254 * After successfully changing clk's parent clk_set_parent will update the
2255 * clk topology, sysfs topology and propagate rate recalculation via
2256 * __clk_recalc_rates.
2257 *
2258 * Returns 0 on success, -EERROR otherwise.
2259 */
2260int clk_set_parent(struct clk *clk, struct clk *parent)
2261{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002262 int ret;
2263
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002264 if (!clk)
2265 return 0;
2266
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002267 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002268
2269 if (clk->exclusive_count)
2270 clk_core_rate_unprotect(clk->core);
2271
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002272 ret = clk_core_set_parent_nolock(clk->core,
2273 parent ? parent->core : NULL);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002274
2275 if (clk->exclusive_count)
2276 clk_core_rate_protect(clk->core);
2277
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002278 clk_prepare_unlock();
2279
2280 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002281}
Mike Turquetteb24764902012-03-15 23:11:19 -07002282EXPORT_SYMBOL_GPL(clk_set_parent);
2283
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002284static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2285{
2286 int ret = -EINVAL;
2287
2288 lockdep_assert_held(&prepare_lock);
2289
2290 if (!core)
2291 return 0;
2292
Jerome Brunete55a8392017-12-01 22:51:56 +01002293 if (clk_core_rate_is_protected(core))
2294 return -EBUSY;
2295
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002296 trace_clk_set_phase(core, degrees);
2297
2298 if (core->ops->set_phase)
2299 ret = core->ops->set_phase(core->hw, degrees);
2300
2301 trace_clk_set_phase_complete(core, degrees);
2302
2303 return ret;
2304}
2305
Mike Turquetteb24764902012-03-15 23:11:19 -07002306/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002307 * clk_set_phase - adjust the phase shift of a clock signal
2308 * @clk: clock signal source
2309 * @degrees: number of degrees the signal is shifted
2310 *
2311 * Shifts the phase of a clock signal by the specified
2312 * degrees. Returns 0 on success, -EERROR otherwise.
2313 *
2314 * This function makes no distinction about the input or reference
2315 * signal that we adjust the clock signal phase against. For example
2316 * phase locked-loop clock signal generators we may shift phase with
2317 * respect to feedback clock signal input, but for other cases the
2318 * clock phase may be shifted with respect to some other, unspecified
2319 * signal.
2320 *
2321 * Additionally the concept of phase shift does not propagate through
2322 * the clock tree hierarchy, which sets it apart from clock rates and
2323 * clock accuracy. A parent clock phase attribute does not have an
2324 * impact on the phase attribute of a child clock.
2325 */
2326int clk_set_phase(struct clk *clk, int degrees)
2327{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002328 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002329
2330 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002331 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002332
2333 /* sanity check degrees */
2334 degrees %= 360;
2335 if (degrees < 0)
2336 degrees += 360;
2337
2338 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002339
2340 if (clk->exclusive_count)
2341 clk_core_rate_unprotect(clk->core);
2342
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002343 ret = clk_core_set_phase_nolock(clk->core, degrees);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002344
2345 if (clk->exclusive_count)
2346 clk_core_rate_protect(clk->core);
2347
Mike Turquettee59c5372014-02-18 21:21:25 -08002348 clk_prepare_unlock();
2349
Mike Turquettee59c5372014-02-18 21:21:25 -08002350 return ret;
2351}
Maxime Ripard9767b04f2015-01-20 22:23:43 +01002352EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002353
Stephen Boydd6968fc2015-04-30 13:54:13 -07002354static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002355{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002356 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002357
2358 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07002359 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002360 clk_prepare_unlock();
2361
Mike Turquettee59c5372014-02-18 21:21:25 -08002362 return ret;
2363}
2364
2365/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002366 * clk_get_phase - return the phase shift of a clock signal
2367 * @clk: clock signal source
2368 *
2369 * Returns the phase shift of a clock node in degrees, otherwise returns
2370 * -EERROR.
2371 */
2372int clk_get_phase(struct clk *clk)
2373{
2374 if (!clk)
2375 return 0;
2376
2377 return clk_core_get_phase(clk->core);
2378}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002379EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002380
2381/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002382 * clk_is_match - check if two clk's point to the same hardware clock
2383 * @p: clk compared against q
2384 * @q: clk compared against p
2385 *
2386 * Returns true if the two struct clk pointers both point to the same hardware
2387 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2388 * share the same struct clk_core object.
2389 *
2390 * Returns false otherwise. Note that two NULL clks are treated as matching.
2391 */
2392bool clk_is_match(const struct clk *p, const struct clk *q)
2393{
2394 /* trivial case: identical struct clk's or both NULL */
2395 if (p == q)
2396 return true;
2397
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002398 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002399 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2400 if (p->core == q->core)
2401 return true;
2402
2403 return false;
2404}
2405EXPORT_SYMBOL_GPL(clk_is_match);
2406
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002407/*** debugfs support ***/
2408
2409#ifdef CONFIG_DEBUG_FS
2410#include <linux/debugfs.h>
2411
2412static struct dentry *rootdir;
2413static int inited = 0;
2414static DEFINE_MUTEX(clk_debug_lock);
2415static HLIST_HEAD(clk_debug_list);
2416
2417static struct hlist_head *all_lists[] = {
2418 &clk_root_list,
2419 &clk_orphan_list,
2420 NULL,
2421};
2422
2423static struct hlist_head *orphan_list[] = {
2424 &clk_orphan_list,
2425 NULL,
2426};
2427
2428static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2429 int level)
2430{
2431 if (!c)
2432 return;
2433
Jerome Brunetc5ce26e2017-12-01 22:51:57 +01002434 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %-3d\n",
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002435 level * 3 + 1, "",
2436 30 - level * 3, c->name,
Jerome Brunete55a8392017-12-01 22:51:56 +01002437 c->enable_count, c->prepare_count, c->protect_count,
2438 clk_core_get_rate(c), clk_core_get_accuracy(c),
2439 clk_core_get_phase(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002440}
2441
2442static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2443 int level)
2444{
2445 struct clk_core *child;
2446
2447 if (!c)
2448 return;
2449
2450 clk_summary_show_one(s, c, level);
2451
2452 hlist_for_each_entry(child, &c->children, child_node)
2453 clk_summary_show_subtree(s, child, level + 1);
2454}
2455
2456static int clk_summary_show(struct seq_file *s, void *data)
2457{
2458 struct clk_core *c;
2459 struct hlist_head **lists = (struct hlist_head **)s->private;
2460
Jerome Brunetc5ce26e2017-12-01 22:51:57 +01002461 seq_puts(s, " enable prepare protect \n");
2462 seq_puts(s, " clock count count count rate accuracy phase\n");
2463 seq_puts(s, "----------------------------------------------------------------------------------------\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002464
2465 clk_prepare_lock();
2466
2467 for (; *lists; lists++)
2468 hlist_for_each_entry(c, *lists, child_node)
2469 clk_summary_show_subtree(s, c, 0);
2470
2471 clk_prepare_unlock();
2472
2473 return 0;
2474}
2475
2476
2477static int clk_summary_open(struct inode *inode, struct file *file)
2478{
2479 return single_open(file, clk_summary_show, inode->i_private);
2480}
2481
2482static const struct file_operations clk_summary_fops = {
2483 .open = clk_summary_open,
2484 .read = seq_read,
2485 .llseek = seq_lseek,
2486 .release = single_release,
2487};
2488
2489static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2490{
2491 if (!c)
2492 return;
2493
Stefan Wahren7cb81132015-04-29 16:36:43 +00002494 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002495 seq_printf(s, "\"%s\": { ", c->name);
2496 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2497 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002498 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002499 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2500 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002501 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2502}
2503
2504static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2505{
2506 struct clk_core *child;
2507
2508 if (!c)
2509 return;
2510
2511 clk_dump_one(s, c, level);
2512
2513 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002514 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002515 clk_dump_subtree(s, child, level + 1);
2516 }
2517
Markus Elfring4d327582017-04-20 08:45:43 +02002518 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002519}
2520
2521static int clk_dump(struct seq_file *s, void *data)
2522{
2523 struct clk_core *c;
2524 bool first_node = true;
2525 struct hlist_head **lists = (struct hlist_head **)s->private;
2526
Markus Elfring4d327582017-04-20 08:45:43 +02002527 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002528 clk_prepare_lock();
2529
2530 for (; *lists; lists++) {
2531 hlist_for_each_entry(c, *lists, child_node) {
2532 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02002533 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002534 first_node = false;
2535 clk_dump_subtree(s, c, 0);
2536 }
2537 }
2538
2539 clk_prepare_unlock();
2540
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002541 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002542 return 0;
2543}
2544
2545
2546static int clk_dump_open(struct inode *inode, struct file *file)
2547{
2548 return single_open(file, clk_dump, inode->i_private);
2549}
2550
2551static const struct file_operations clk_dump_fops = {
2552 .open = clk_dump_open,
2553 .read = seq_read,
2554 .llseek = seq_lseek,
2555 .release = single_release,
2556};
2557
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002558static const struct {
2559 unsigned long flag;
2560 const char *name;
2561} clk_flags[] = {
2562#define ENTRY(f) { f, __stringify(f) }
2563 ENTRY(CLK_SET_RATE_GATE),
2564 ENTRY(CLK_SET_PARENT_GATE),
2565 ENTRY(CLK_SET_RATE_PARENT),
2566 ENTRY(CLK_IGNORE_UNUSED),
2567 ENTRY(CLK_IS_BASIC),
2568 ENTRY(CLK_GET_RATE_NOCACHE),
2569 ENTRY(CLK_SET_RATE_NO_REPARENT),
2570 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2571 ENTRY(CLK_RECALC_NEW_RATES),
2572 ENTRY(CLK_SET_RATE_UNGATE),
2573 ENTRY(CLK_IS_CRITICAL),
2574 ENTRY(CLK_OPS_PARENT_ENABLE),
2575#undef ENTRY
2576};
2577
2578static int clk_flags_dump(struct seq_file *s, void *data)
2579{
2580 struct clk_core *core = s->private;
2581 unsigned long flags = core->flags;
2582 unsigned int i;
2583
2584 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2585 if (flags & clk_flags[i].flag) {
2586 seq_printf(s, "%s\n", clk_flags[i].name);
2587 flags &= ~clk_flags[i].flag;
2588 }
2589 }
2590 if (flags) {
2591 /* Unknown flags */
2592 seq_printf(s, "0x%lx\n", flags);
2593 }
2594
2595 return 0;
2596}
2597
2598static int clk_flags_open(struct inode *inode, struct file *file)
2599{
2600 return single_open(file, clk_flags_dump, inode->i_private);
2601}
2602
2603static const struct file_operations clk_flags_fops = {
2604 .open = clk_flags_open,
2605 .read = seq_read,
2606 .llseek = seq_lseek,
2607 .release = single_release,
2608};
2609
Peter De Schrijver92031572017-03-21 15:20:31 +02002610static int possible_parents_dump(struct seq_file *s, void *data)
2611{
2612 struct clk_core *core = s->private;
2613 int i;
2614
2615 for (i = 0; i < core->num_parents - 1; i++)
2616 seq_printf(s, "%s ", core->parent_names[i]);
2617
2618 seq_printf(s, "%s\n", core->parent_names[i]);
2619
2620 return 0;
2621}
2622
2623static int possible_parents_open(struct inode *inode, struct file *file)
2624{
2625 return single_open(file, possible_parents_dump, inode->i_private);
2626}
2627
2628static const struct file_operations possible_parents_fops = {
2629 .open = possible_parents_open,
2630 .read = seq_read,
2631 .llseek = seq_lseek,
2632 .release = single_release,
2633};
2634
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002635static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2636{
2637 struct dentry *d;
2638 int ret = -ENOMEM;
2639
2640 if (!core || !pdentry) {
2641 ret = -EINVAL;
2642 goto out;
2643 }
2644
2645 d = debugfs_create_dir(core->name, pdentry);
2646 if (!d)
2647 goto out;
2648
2649 core->dentry = d;
2650
Geert Uytterhoeven4c8326d2018-01-03 12:06:15 +01002651 d = debugfs_create_ulong("clk_rate", 0444, core->dentry, &core->rate);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002652 if (!d)
2653 goto err_out;
2654
Geert Uytterhoeven4c8326d2018-01-03 12:06:15 +01002655 d = debugfs_create_ulong("clk_accuracy", 0444, core->dentry,
2656 &core->accuracy);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002657 if (!d)
2658 goto err_out;
2659
Geert Uytterhoeven4c8326d2018-01-03 12:06:15 +01002660 d = debugfs_create_u32("clk_phase", 0444, core->dentry, &core->phase);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002661 if (!d)
2662 goto err_out;
2663
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002664 d = debugfs_create_file("clk_flags", 0444, core->dentry, core,
2665 &clk_flags_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002666 if (!d)
2667 goto err_out;
2668
Geert Uytterhoeven4c8326d2018-01-03 12:06:15 +01002669 d = debugfs_create_u32("clk_prepare_count", 0444, core->dentry,
2670 &core->prepare_count);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002671 if (!d)
2672 goto err_out;
2673
Geert Uytterhoeven4c8326d2018-01-03 12:06:15 +01002674 d = debugfs_create_u32("clk_enable_count", 0444, core->dentry,
2675 &core->enable_count);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002676 if (!d)
2677 goto err_out;
2678
Geert Uytterhoeven4c8326d2018-01-03 12:06:15 +01002679 d = debugfs_create_u32("clk_protect_count", 0444, core->dentry,
2680 &core->protect_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002681 if (!d)
2682 goto err_out;
2683
Geert Uytterhoeven4c8326d2018-01-03 12:06:15 +01002684 d = debugfs_create_u32("clk_notifier_count", 0444, core->dentry,
2685 &core->notifier_count);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002686 if (!d)
2687 goto err_out;
2688
Peter De Schrijver92031572017-03-21 15:20:31 +02002689 if (core->num_parents > 1) {
Geert Uytterhoeven4c8326d2018-01-03 12:06:15 +01002690 d = debugfs_create_file("clk_possible_parents", 0444,
Peter De Schrijver92031572017-03-21 15:20:31 +02002691 core->dentry, core, &possible_parents_fops);
2692 if (!d)
2693 goto err_out;
2694 }
2695
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002696 if (core->ops->debug_init) {
2697 ret = core->ops->debug_init(core->hw, core->dentry);
2698 if (ret)
2699 goto err_out;
2700 }
2701
2702 ret = 0;
2703 goto out;
2704
2705err_out:
2706 debugfs_remove_recursive(core->dentry);
2707 core->dentry = NULL;
2708out:
2709 return ret;
2710}
2711
2712/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002713 * clk_debug_register - add a clk node to the debugfs clk directory
2714 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002715 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002716 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2717 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002718 * will be created lazily by clk_debug_init as part of a late_initcall.
2719 */
2720static int clk_debug_register(struct clk_core *core)
2721{
2722 int ret = 0;
2723
2724 mutex_lock(&clk_debug_lock);
2725 hlist_add_head(&core->debug_node, &clk_debug_list);
2726
2727 if (!inited)
2728 goto unlock;
2729
2730 ret = clk_debug_create_one(core, rootdir);
2731unlock:
2732 mutex_unlock(&clk_debug_lock);
2733
2734 return ret;
2735}
2736
2737 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002738 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2739 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002740 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002741 * Dynamically removes a clk and all its child nodes from the
2742 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08002743 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002744 */
2745static void clk_debug_unregister(struct clk_core *core)
2746{
2747 mutex_lock(&clk_debug_lock);
2748 hlist_del_init(&core->debug_node);
2749 debugfs_remove_recursive(core->dentry);
2750 core->dentry = NULL;
2751 mutex_unlock(&clk_debug_lock);
2752}
2753
2754struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2755 void *data, const struct file_operations *fops)
2756{
2757 struct dentry *d = NULL;
2758
2759 if (hw->core->dentry)
2760 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2761 fops);
2762
2763 return d;
2764}
2765EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2766
2767/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002768 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002769 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002770 * clks are often initialized very early during boot before memory can be
2771 * dynamically allocated and well before debugfs is setup. This function
2772 * populates the debugfs clk directory once at boot-time when we know that
2773 * debugfs is setup. It should only be called once at boot-time, all other clks
2774 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002775 */
2776static int __init clk_debug_init(void)
2777{
2778 struct clk_core *core;
2779 struct dentry *d;
2780
2781 rootdir = debugfs_create_dir("clk", NULL);
2782
2783 if (!rootdir)
2784 return -ENOMEM;
2785
Geert Uytterhoeven4c8326d2018-01-03 12:06:15 +01002786 d = debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002787 &clk_summary_fops);
2788 if (!d)
2789 return -ENOMEM;
2790
Geert Uytterhoeven4c8326d2018-01-03 12:06:15 +01002791 d = debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002792 &clk_dump_fops);
2793 if (!d)
2794 return -ENOMEM;
2795
Geert Uytterhoeven4c8326d2018-01-03 12:06:15 +01002796 d = debugfs_create_file("clk_orphan_summary", 0444, rootdir,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002797 &orphan_list, &clk_summary_fops);
2798 if (!d)
2799 return -ENOMEM;
2800
Geert Uytterhoeven4c8326d2018-01-03 12:06:15 +01002801 d = debugfs_create_file("clk_orphan_dump", 0444, rootdir,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002802 &orphan_list, &clk_dump_fops);
2803 if (!d)
2804 return -ENOMEM;
2805
2806 mutex_lock(&clk_debug_lock);
2807 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2808 clk_debug_create_one(core, rootdir);
2809
2810 inited = 1;
2811 mutex_unlock(&clk_debug_lock);
2812
2813 return 0;
2814}
2815late_initcall(clk_debug_init);
2816#else
2817static inline int clk_debug_register(struct clk_core *core) { return 0; }
2818static inline void clk_debug_reparent(struct clk_core *core,
2819 struct clk_core *new_parent)
2820{
2821}
2822static inline void clk_debug_unregister(struct clk_core *core)
2823{
2824}
2825#endif
2826
Michael Turquette3d3801e2015-02-25 09:11:01 -08002827/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002828 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002829 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07002830 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002831 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002832 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002833 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002834static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002835{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002836 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002837 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002838 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002839 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002840
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002841 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07002842 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002843
Mike Turquetteeab89f62013-03-28 13:59:01 -07002844 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002845
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002846 ret = clk_pm_runtime_get(core);
2847 if (ret)
2848 goto unlock;
2849
Mike Turquetteb24764902012-03-15 23:11:19 -07002850 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002851 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002852 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002853 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002854 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002855 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002856 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002857
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002858 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002859 if (core->ops->set_rate &&
2860 !((core->ops->round_rate || core->ops->determine_rate) &&
2861 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002862 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2863 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002864 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002865 goto out;
2866 }
2867
Stephen Boydd6968fc2015-04-30 13:54:13 -07002868 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002869 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2870 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002871 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002872 goto out;
2873 }
2874
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09002875 if (core->num_parents > 1 && !core->ops->get_parent) {
2876 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2877 __func__, core->name);
2878 ret = -EINVAL;
2879 goto out;
2880 }
2881
Stephen Boydd6968fc2015-04-30 13:54:13 -07002882 if (core->ops->set_rate_and_parent &&
2883 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002884 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002885 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002886 ret = -EINVAL;
2887 goto out;
2888 }
2889
Mike Turquetteb24764902012-03-15 23:11:19 -07002890 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002891 for (i = 0; i < core->num_parents; i++)
2892 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002893 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002894 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002895
Stephen Boydd6968fc2015-04-30 13:54:13 -07002896 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002897
2898 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08002899 * Populate core->parent if parent has already been clk_core_init'd. If
2900 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002901 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07002902 * clk list.
2903 *
2904 * Every time a new clk is clk_init'd then we walk the list of orphan
2905 * clocks and re-parent any that are children of the clock currently
2906 * being clk_init'd.
2907 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02002908 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002909 hlist_add_head(&core->child_node,
2910 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002911 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002912 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002913 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002914 core->orphan = false;
2915 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002916 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002917 core->orphan = true;
2918 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002919
2920 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002921 * Set clk's accuracy. The preferred method is to use
2922 * .recalc_accuracy. For simple clocks and lazy developers the default
2923 * fallback is to use the parent's accuracy. If a clock doesn't have a
2924 * parent (or is orphaned) then accuracy is set to zero (perfect
2925 * clock).
2926 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002927 if (core->ops->recalc_accuracy)
2928 core->accuracy = core->ops->recalc_accuracy(core->hw,
2929 __clk_get_accuracy(core->parent));
2930 else if (core->parent)
2931 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002932 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002933 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002934
2935 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002936 * Set clk's phase.
2937 * Since a phase is by definition relative to its parent, just
2938 * query the current clock phase, or just assume it's in phase.
2939 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002940 if (core->ops->get_phase)
2941 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002942 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002943 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002944
2945 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002946 * Set clk's rate. The preferred method is to use .recalc_rate. For
2947 * simple clocks and lazy developers the default fallback is to use the
2948 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2949 * then rate is set to zero.
2950 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002951 if (core->ops->recalc_rate)
2952 rate = core->ops->recalc_rate(core->hw,
2953 clk_core_get_rate_nolock(core->parent));
2954 else if (core->parent)
2955 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002956 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002957 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002958 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002959
2960 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002961 * walk the list of orphan clocks and reparent any that newly finds a
2962 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07002963 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002964 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002965 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002966
Michael Turquette904e6ea2016-07-08 16:32:10 -07002967 /*
2968 * we could call __clk_set_parent, but that would result in a
2969 * redundant call to the .set_rate op, if it exists
2970 */
2971 if (parent) {
2972 __clk_set_parent_before(orphan, parent);
2973 __clk_set_parent_after(orphan, parent, NULL);
2974 __clk_recalc_accuracies(orphan);
2975 __clk_recalc_rates(orphan, 0);
2976 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002977 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002978
2979 /*
2980 * optional platform-specific magic
2981 *
2982 * The .init callback is not used by any of the basic clock types, but
2983 * exists for weird hardware that must perform initialization magic.
2984 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002985 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002986 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002987 if (core->ops->init)
2988 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002989
Lee Jones32b9b102016-02-11 13:19:09 -08002990 if (core->flags & CLK_IS_CRITICAL) {
Maxime Ripardef56b792016-05-13 10:00:31 +02002991 unsigned long flags;
2992
Lee Jones32b9b102016-02-11 13:19:09 -08002993 clk_core_prepare(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02002994
2995 flags = clk_enable_lock();
Lee Jones32b9b102016-02-11 13:19:09 -08002996 clk_core_enable(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02002997 clk_enable_unlock(flags);
Lee Jones32b9b102016-02-11 13:19:09 -08002998 }
2999
Stephen Boydd6968fc2015-04-30 13:54:13 -07003000 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07003001out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003002 clk_pm_runtime_put(core);
3003unlock:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003004 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003005
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003006 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003007 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003008
Mike Turquetted1302a32012-03-29 14:30:40 -07003009 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07003010}
3011
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003012struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
3013 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003014{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003015 struct clk *clk;
3016
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003017 /* This is to allow this function to be chained to others */
Masahiro Yamadac1de1352015-11-20 14:38:49 +09003018 if (IS_ERR_OR_NULL(hw))
Masahiro Yamada8a231332016-07-19 16:28:47 +09003019 return ERR_CAST(hw);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003020
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003021 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3022 if (!clk)
3023 return ERR_PTR(-ENOMEM);
3024
3025 clk->core = hw->core;
3026 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02003027 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003028 clk->max_rate = ULONG_MAX;
3029
3030 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08003031 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003032 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003033
3034 return clk;
3035}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003036
Stephen Boyd73e0e492015-02-06 11:42:43 -08003037void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003038{
3039 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08003040 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003041 clk_prepare_unlock();
3042
Leonard Crestez253160a2017-02-20 15:20:56 +02003043 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003044 kfree(clk);
3045}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003046
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003047/**
3048 * clk_register - allocate a new clock, register it and return an opaque cookie
3049 * @dev: device that is registering this clock
3050 * @hw: link to hardware-specific clock data
3051 *
3052 * clk_register is the primary interface for populating the clock tree with new
3053 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05303054 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003055 * rest of the clock API. In the event of an error clk_register will return an
3056 * error code; drivers must test for an error code after calling clk_register.
3057 */
3058struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07003059{
Mike Turquetted1302a32012-03-29 14:30:40 -07003060 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003061 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003062
Stephen Boydd6968fc2015-04-30 13:54:13 -07003063 core = kzalloc(sizeof(*core), GFP_KERNEL);
3064 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003065 ret = -ENOMEM;
3066 goto fail_out;
3067 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003068
Stephen Boydd6968fc2015-04-30 13:54:13 -07003069 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3070 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003071 ret = -ENOMEM;
3072 goto fail_name;
3073 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07003074 core->ops = hw->init->ops;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003075 if (dev && pm_runtime_enabled(dev))
3076 core->dev = dev;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003077 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003078 core->owner = dev->driver->owner;
3079 core->hw = hw;
3080 core->flags = hw->init->flags;
3081 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003082 core->min_rate = 0;
3083 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003084 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003085
Mike Turquetted1302a32012-03-29 14:30:40 -07003086 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003087 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02003088 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003089
Stephen Boydd6968fc2015-04-30 13:54:13 -07003090 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003091 ret = -ENOMEM;
3092 goto fail_parent_names;
3093 }
3094
3095
3096 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003097 for (i = 0; i < core->num_parents; i++) {
3098 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003099 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003100 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003101 ret = -ENOMEM;
3102 goto fail_parent_names_copy;
3103 }
3104 }
3105
Masahiro Yamada176d1162015-12-28 19:23:00 +09003106 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3107 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3108 GFP_KERNEL);
3109 if (!core->parents) {
3110 ret = -ENOMEM;
3111 goto fail_parents;
3112 };
3113
Stephen Boydd6968fc2015-04-30 13:54:13 -07003114 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003115
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003116 hw->clk = __clk_create_clk(hw, NULL, NULL);
3117 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003118 ret = PTR_ERR(hw->clk);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003119 goto fail_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003120 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003121
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003122 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003123 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003124 return hw->clk;
3125
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003126 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003127 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003128
Masahiro Yamada176d1162015-12-28 19:23:00 +09003129fail_parents:
3130 kfree(core->parents);
Mike Turquetted1302a32012-03-29 14:30:40 -07003131fail_parent_names_copy:
3132 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003133 kfree_const(core->parent_names[i]);
3134 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07003135fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003136 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003137fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003138 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003139fail_out:
3140 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003141}
3142EXPORT_SYMBOL_GPL(clk_register);
3143
Stephen Boyd41438042016-02-05 17:02:52 -08003144/**
3145 * clk_hw_register - register a clk_hw and return an error code
3146 * @dev: device that is registering this clock
3147 * @hw: link to hardware-specific clock data
3148 *
3149 * clk_hw_register is the primary interface for populating the clock tree with
3150 * new clock nodes. It returns an integer equal to zero indicating success or
3151 * less than zero indicating failure. Drivers must test for an error code after
3152 * calling clk_hw_register().
3153 */
3154int clk_hw_register(struct device *dev, struct clk_hw *hw)
3155{
3156 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3157}
3158EXPORT_SYMBOL_GPL(clk_hw_register);
3159
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003160/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003161static void __clk_release(struct kref *ref)
3162{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003163 struct clk_core *core = container_of(ref, struct clk_core, ref);
3164 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003165
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003166 lockdep_assert_held(&prepare_lock);
3167
Stephen Boydd6968fc2015-04-30 13:54:13 -07003168 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003169 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003170 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003171
Stephen Boydd6968fc2015-04-30 13:54:13 -07003172 kfree(core->parent_names);
3173 kfree_const(core->name);
3174 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003175}
3176
3177/*
3178 * Empty clk_ops for unregistered clocks. These are used temporarily
3179 * after clk_unregister() was called on a clock and until last clock
3180 * consumer calls clk_put() and the struct clk object is freed.
3181 */
3182static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3183{
3184 return -ENXIO;
3185}
3186
3187static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3188{
3189 WARN_ON_ONCE(1);
3190}
3191
3192static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3193 unsigned long parent_rate)
3194{
3195 return -ENXIO;
3196}
3197
3198static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3199{
3200 return -ENXIO;
3201}
3202
3203static const struct clk_ops clk_nodrv_ops = {
3204 .enable = clk_nodrv_prepare_enable,
3205 .disable = clk_nodrv_disable_unprepare,
3206 .prepare = clk_nodrv_prepare_enable,
3207 .unprepare = clk_nodrv_disable_unprepare,
3208 .set_rate = clk_nodrv_set_rate,
3209 .set_parent = clk_nodrv_set_parent,
3210};
3211
Mark Brown1df5c932012-04-18 09:07:12 +01003212/**
3213 * clk_unregister - unregister a currently registered clock
3214 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003215 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003216void clk_unregister(struct clk *clk)
3217{
3218 unsigned long flags;
3219
Stephen Boyd6314b672014-09-04 23:37:49 -07003220 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3221 return;
3222
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003223 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003224
3225 clk_prepare_lock();
3226
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003227 if (clk->core->ops == &clk_nodrv_ops) {
3228 pr_err("%s: unregistered clock: %s\n", __func__,
3229 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003230 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003231 }
3232 /*
3233 * Assign empty clock ops for consumers that might still hold
3234 * a reference to this clock.
3235 */
3236 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003237 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003238 clk_enable_unlock(flags);
3239
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003240 if (!hlist_empty(&clk->core->children)) {
3241 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003242 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003243
3244 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003245 hlist_for_each_entry_safe(child, t, &clk->core->children,
3246 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01003247 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003248 }
3249
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003250 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003251
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003252 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003253 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003254 __func__, clk->core->name);
Jerome Brunete55a8392017-12-01 22:51:56 +01003255
3256 if (clk->core->protect_count)
3257 pr_warn("%s: unregistering protected clock: %s\n",
3258 __func__, clk->core->name);
3259
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003260 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003261unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003262 clk_prepare_unlock();
3263}
Mark Brown1df5c932012-04-18 09:07:12 +01003264EXPORT_SYMBOL_GPL(clk_unregister);
3265
Stephen Boyd41438042016-02-05 17:02:52 -08003266/**
3267 * clk_hw_unregister - unregister a currently registered clk_hw
3268 * @hw: hardware-specific clock data to unregister
3269 */
3270void clk_hw_unregister(struct clk_hw *hw)
3271{
3272 clk_unregister(hw->clk);
3273}
3274EXPORT_SYMBOL_GPL(clk_hw_unregister);
3275
Stephen Boyd46c87732012-09-24 13:38:04 -07003276static void devm_clk_release(struct device *dev, void *res)
3277{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003278 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003279}
3280
Stephen Boyd41438042016-02-05 17:02:52 -08003281static void devm_clk_hw_release(struct device *dev, void *res)
3282{
3283 clk_hw_unregister(*(struct clk_hw **)res);
3284}
3285
Stephen Boyd46c87732012-09-24 13:38:04 -07003286/**
3287 * devm_clk_register - resource managed clk_register()
3288 * @dev: device that is registering this clock
3289 * @hw: link to hardware-specific clock data
3290 *
3291 * Managed clk_register(). Clocks returned from this function are
3292 * automatically clk_unregister()ed on driver detach. See clk_register() for
3293 * more information.
3294 */
3295struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3296{
3297 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003298 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003299
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003300 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3301 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003302 return ERR_PTR(-ENOMEM);
3303
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003304 clk = clk_register(dev, hw);
3305 if (!IS_ERR(clk)) {
3306 *clkp = clk;
3307 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003308 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003309 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003310 }
3311
3312 return clk;
3313}
3314EXPORT_SYMBOL_GPL(devm_clk_register);
3315
Stephen Boyd41438042016-02-05 17:02:52 -08003316/**
3317 * devm_clk_hw_register - resource managed clk_hw_register()
3318 * @dev: device that is registering this clock
3319 * @hw: link to hardware-specific clock data
3320 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003321 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003322 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3323 * for more information.
3324 */
3325int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3326{
3327 struct clk_hw **hwp;
3328 int ret;
3329
3330 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3331 if (!hwp)
3332 return -ENOMEM;
3333
3334 ret = clk_hw_register(dev, hw);
3335 if (!ret) {
3336 *hwp = hw;
3337 devres_add(dev, hwp);
3338 } else {
3339 devres_free(hwp);
3340 }
3341
3342 return ret;
3343}
3344EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3345
Stephen Boyd46c87732012-09-24 13:38:04 -07003346static int devm_clk_match(struct device *dev, void *res, void *data)
3347{
3348 struct clk *c = res;
3349 if (WARN_ON(!c))
3350 return 0;
3351 return c == data;
3352}
3353
Stephen Boyd41438042016-02-05 17:02:52 -08003354static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3355{
3356 struct clk_hw *hw = res;
3357
3358 if (WARN_ON(!hw))
3359 return 0;
3360 return hw == data;
3361}
3362
Stephen Boyd46c87732012-09-24 13:38:04 -07003363/**
3364 * devm_clk_unregister - resource managed clk_unregister()
3365 * @clk: clock to unregister
3366 *
3367 * Deallocate a clock allocated with devm_clk_register(). Normally
3368 * this function will not need to be called and the resource management
3369 * code will ensure that the resource is freed.
3370 */
3371void devm_clk_unregister(struct device *dev, struct clk *clk)
3372{
3373 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3374}
3375EXPORT_SYMBOL_GPL(devm_clk_unregister);
3376
Stephen Boyd41438042016-02-05 17:02:52 -08003377/**
3378 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3379 * @dev: device that is unregistering the hardware-specific clock data
3380 * @hw: link to hardware-specific clock data
3381 *
3382 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3383 * this function will not need to be called and the resource management
3384 * code will ensure that the resource is freed.
3385 */
3386void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3387{
3388 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3389 hw));
3390}
3391EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3392
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003393/*
3394 * clkdev helpers
3395 */
3396int __clk_get(struct clk *clk)
3397{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003398 struct clk_core *core = !clk ? NULL : clk->core;
3399
3400 if (core) {
3401 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003402 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003403
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003404 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003405 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003406 return 1;
3407}
3408
3409void __clk_put(struct clk *clk)
3410{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003411 struct module *owner;
3412
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003413 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003414 return;
3415
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003416 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003417
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01003418 /*
3419 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3420 * given user should be balanced with calls to clk_rate_exclusive_put()
3421 * and by that same consumer
3422 */
3423 if (WARN_ON(clk->exclusive_count)) {
3424 /* We voiced our concern, let's sanitize the situation */
3425 clk->core->protect_count -= (clk->exclusive_count - 1);
3426 clk_core_rate_unprotect(clk->core);
3427 clk->exclusive_count = 0;
3428 }
3429
Stephen Boyd50595f82015-02-06 11:42:44 -08003430 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003431 if (clk->min_rate > clk->core->req_rate ||
3432 clk->max_rate < clk->core->req_rate)
3433 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3434
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003435 owner = clk->core->owner;
3436 kref_put(&clk->core->ref, __clk_release);
3437
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003438 clk_prepare_unlock();
3439
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003440 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003441
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003442 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003443}
3444
Mike Turquetteb24764902012-03-15 23:11:19 -07003445/*** clk rate change notifiers ***/
3446
3447/**
3448 * clk_notifier_register - add a clk rate change notifier
3449 * @clk: struct clk * to watch
3450 * @nb: struct notifier_block * with callback info
3451 *
3452 * Request notification when clk's rate changes. This uses an SRCU
3453 * notifier because we want it to block and notifier unregistrations are
3454 * uncommon. The callbacks associated with the notifier must not
3455 * re-enter into the clk framework by calling any top-level clk APIs;
3456 * this will cause a nested prepare_lock mutex.
3457 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003458 * In all notification cases (pre, post and abort rate change) the original
3459 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3460 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003461 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003462 * clk_notifier_register() must be called from non-atomic context.
3463 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3464 * allocation failure; otherwise, passes along the return value of
3465 * srcu_notifier_chain_register().
3466 */
3467int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3468{
3469 struct clk_notifier *cn;
3470 int ret = -ENOMEM;
3471
3472 if (!clk || !nb)
3473 return -EINVAL;
3474
Mike Turquetteeab89f62013-03-28 13:59:01 -07003475 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003476
3477 /* search the list of notifiers for this clk */
3478 list_for_each_entry(cn, &clk_notifier_list, node)
3479 if (cn->clk == clk)
3480 break;
3481
3482 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3483 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02003484 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003485 if (!cn)
3486 goto out;
3487
3488 cn->clk = clk;
3489 srcu_init_notifier_head(&cn->notifier_head);
3490
3491 list_add(&cn->node, &clk_notifier_list);
3492 }
3493
3494 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3495
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003496 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003497
3498out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003499 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003500
3501 return ret;
3502}
3503EXPORT_SYMBOL_GPL(clk_notifier_register);
3504
3505/**
3506 * clk_notifier_unregister - remove a clk rate change notifier
3507 * @clk: struct clk *
3508 * @nb: struct notifier_block * with callback info
3509 *
3510 * Request no further notification for changes to 'clk' and frees memory
3511 * allocated in clk_notifier_register.
3512 *
3513 * Returns -EINVAL if called with null arguments; otherwise, passes
3514 * along the return value of srcu_notifier_chain_unregister().
3515 */
3516int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3517{
3518 struct clk_notifier *cn = NULL;
3519 int ret = -EINVAL;
3520
3521 if (!clk || !nb)
3522 return -EINVAL;
3523
Mike Turquetteeab89f62013-03-28 13:59:01 -07003524 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003525
3526 list_for_each_entry(cn, &clk_notifier_list, node)
3527 if (cn->clk == clk)
3528 break;
3529
3530 if (cn->clk == clk) {
3531 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3532
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003533 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07003534
3535 /* XXX the notifier code should handle this better */
3536 if (!cn->notifier_head.head) {
3537 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08003538 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07003539 kfree(cn);
3540 }
3541
3542 } else {
3543 ret = -ENOENT;
3544 }
3545
Mike Turquetteeab89f62013-03-28 13:59:01 -07003546 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003547
3548 return ret;
3549}
3550EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05003551
3552#ifdef CONFIG_OF
3553/**
3554 * struct of_clk_provider - Clock provider registration structure
3555 * @link: Entry in global list of clock providers
3556 * @node: Pointer to device tree node of clock provider
3557 * @get: Get clock callback. Returns NULL or a struct clk for the
3558 * given clock specifier
3559 * @data: context pointer to be passed into @get callback
3560 */
3561struct of_clk_provider {
3562 struct list_head link;
3563
3564 struct device_node *node;
3565 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003566 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05003567 void *data;
3568};
3569
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303570static const struct of_device_id __clk_of_table_sentinel
3571 __used __section(__clk_of_table_end);
3572
Grant Likely766e6a42012-04-09 14:50:06 -05003573static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003574static DEFINE_MUTEX(of_clk_mutex);
3575
Grant Likely766e6a42012-04-09 14:50:06 -05003576struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3577 void *data)
3578{
3579 return data;
3580}
3581EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3582
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003583struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3584{
3585 return data;
3586}
3587EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3588
Shawn Guo494bfec2012-08-22 21:36:27 +08003589struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3590{
3591 struct clk_onecell_data *clk_data = data;
3592 unsigned int idx = clkspec->args[0];
3593
3594 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02003595 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08003596 return ERR_PTR(-EINVAL);
3597 }
3598
3599 return clk_data->clks[idx];
3600}
3601EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3602
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003603struct clk_hw *
3604of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3605{
3606 struct clk_hw_onecell_data *hw_data = data;
3607 unsigned int idx = clkspec->args[0];
3608
3609 if (idx >= hw_data->num) {
3610 pr_err("%s: invalid index %u\n", __func__, idx);
3611 return ERR_PTR(-EINVAL);
3612 }
3613
3614 return hw_data->hws[idx];
3615}
3616EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3617
Grant Likely766e6a42012-04-09 14:50:06 -05003618/**
3619 * of_clk_add_provider() - Register a clock provider for a node
3620 * @np: Device node pointer associated with clock provider
3621 * @clk_src_get: callback for decoding clock
3622 * @data: context pointer for @clk_src_get callback.
3623 */
3624int of_clk_add_provider(struct device_node *np,
3625 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3626 void *data),
3627 void *data)
3628{
3629 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003630 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003631
Markus Elfring1808a322017-04-20 09:30:52 +02003632 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05003633 if (!cp)
3634 return -ENOMEM;
3635
3636 cp->node = of_node_get(np);
3637 cp->data = data;
3638 cp->get = clk_src_get;
3639
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003640 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003641 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003642 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003643 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05003644
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003645 ret = of_clk_set_defaults(np, true);
3646 if (ret < 0)
3647 of_clk_del_provider(np);
3648
3649 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003650}
3651EXPORT_SYMBOL_GPL(of_clk_add_provider);
3652
3653/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003654 * of_clk_add_hw_provider() - Register a clock provider for a node
3655 * @np: Device node pointer associated with clock provider
3656 * @get: callback for decoding clk_hw
3657 * @data: context pointer for @get callback.
3658 */
3659int of_clk_add_hw_provider(struct device_node *np,
3660 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3661 void *data),
3662 void *data)
3663{
3664 struct of_clk_provider *cp;
3665 int ret;
3666
3667 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3668 if (!cp)
3669 return -ENOMEM;
3670
3671 cp->node = of_node_get(np);
3672 cp->data = data;
3673 cp->get_hw = get;
3674
3675 mutex_lock(&of_clk_mutex);
3676 list_add(&cp->link, &of_clk_providers);
3677 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003678 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003679
3680 ret = of_clk_set_defaults(np, true);
3681 if (ret < 0)
3682 of_clk_del_provider(np);
3683
3684 return ret;
3685}
3686EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3687
Stephen Boydaa795c42017-09-01 16:16:40 -07003688static void devm_of_clk_release_provider(struct device *dev, void *res)
3689{
3690 of_clk_del_provider(*(struct device_node **)res);
3691}
3692
3693int devm_of_clk_add_hw_provider(struct device *dev,
3694 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3695 void *data),
3696 void *data)
3697{
3698 struct device_node **ptr, *np;
3699 int ret;
3700
3701 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3702 GFP_KERNEL);
3703 if (!ptr)
3704 return -ENOMEM;
3705
3706 np = dev->of_node;
3707 ret = of_clk_add_hw_provider(np, get, data);
3708 if (!ret) {
3709 *ptr = np;
3710 devres_add(dev, ptr);
3711 } else {
3712 devres_free(ptr);
3713 }
3714
3715 return ret;
3716}
3717EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3718
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003719/**
Grant Likely766e6a42012-04-09 14:50:06 -05003720 * of_clk_del_provider() - Remove a previously registered clock provider
3721 * @np: Device node pointer associated with clock provider
3722 */
3723void of_clk_del_provider(struct device_node *np)
3724{
3725 struct of_clk_provider *cp;
3726
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003727 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003728 list_for_each_entry(cp, &of_clk_providers, link) {
3729 if (cp->node == np) {
3730 list_del(&cp->link);
3731 of_node_put(cp->node);
3732 kfree(cp);
3733 break;
3734 }
3735 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003736 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003737}
3738EXPORT_SYMBOL_GPL(of_clk_del_provider);
3739
Stephen Boydaa795c42017-09-01 16:16:40 -07003740static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3741{
3742 struct device_node **np = res;
3743
3744 if (WARN_ON(!np || !*np))
3745 return 0;
3746
3747 return *np == data;
3748}
3749
3750void devm_of_clk_del_provider(struct device *dev)
3751{
3752 int ret;
3753
3754 ret = devres_release(dev, devm_of_clk_release_provider,
3755 devm_clk_provider_match, dev->of_node);
3756
3757 WARN_ON(ret);
3758}
3759EXPORT_SYMBOL(devm_of_clk_del_provider);
3760
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003761static struct clk_hw *
3762__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3763 struct of_phandle_args *clkspec)
3764{
3765 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003766
Stephen Boyd74002fc2016-08-25 13:35:36 -07003767 if (provider->get_hw)
3768 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003769
Stephen Boyd74002fc2016-08-25 13:35:36 -07003770 clk = provider->get(clkspec, provider->data);
3771 if (IS_ERR(clk))
3772 return ERR_CAST(clk);
3773 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003774}
3775
Stephen Boyd73e0e492015-02-06 11:42:43 -08003776struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3777 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05003778{
3779 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01003780 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Stephen Boydf155d152016-08-15 14:32:23 -07003781 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -05003782
Stephen Boyd306c3422015-02-05 15:39:11 -08003783 if (!clkspec)
3784 return ERR_PTR(-EINVAL);
3785
Grant Likely766e6a42012-04-09 14:50:06 -05003786 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08003787 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003788 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07003789 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003790 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003791 clk = __clk_create_clk(hw, dev_id, con_id);
Stephen Boydf155d152016-08-15 14:32:23 -07003792 }
Stephen Boyd73e0e492015-02-06 11:42:43 -08003793
Stephen Boydf155d152016-08-15 14:32:23 -07003794 if (!IS_ERR(clk)) {
3795 if (!__clk_get(clk)) {
Stephen Boyd73e0e492015-02-06 11:42:43 -08003796 __clk_free_clk(clk);
3797 clk = ERR_PTR(-ENOENT);
3798 }
3799
Grant Likely766e6a42012-04-09 14:50:06 -05003800 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08003801 }
Grant Likely766e6a42012-04-09 14:50:06 -05003802 }
Stephen Boyd306c3422015-02-05 15:39:11 -08003803 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003804
3805 return clk;
3806}
3807
Stephen Boyd306c3422015-02-05 15:39:11 -08003808/**
3809 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3810 * @clkspec: pointer to a clock specifier data structure
3811 *
3812 * This function looks up a struct clk from the registered list of clock
3813 * providers, an input is a clock specifier data structure as returned
3814 * from the of_parse_phandle_with_args() function call.
3815 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003816struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3817{
Stephen Boyd306c3422015-02-05 15:39:11 -08003818 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05003819}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06003820EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05003821
Stephen Boyd929e7f32016-02-19 15:52:32 -08003822/**
3823 * of_clk_get_parent_count() - Count the number of clocks a device node has
3824 * @np: device node to count
3825 *
3826 * Returns: The number of clocks that are possible parents of this node
3827 */
3828unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07003829{
Stephen Boyd929e7f32016-02-19 15:52:32 -08003830 int count;
3831
3832 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3833 if (count < 0)
3834 return 0;
3835
3836 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07003837}
3838EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3839
Grant Likely766e6a42012-04-09 14:50:06 -05003840const char *of_clk_get_parent_name(struct device_node *np, int index)
3841{
3842 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003843 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003844 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003845 const __be32 *vp;
3846 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003847 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003848 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003849 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05003850
Grant Likely766e6a42012-04-09 14:50:06 -05003851 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3852 &clkspec);
3853 if (rc)
3854 return NULL;
3855
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003856 index = clkspec.args_count ? clkspec.args[0] : 0;
3857 count = 0;
3858
3859 /* if there is an indices property, use it to transfer the index
3860 * specified into an array offset for the clock-output-names property.
3861 */
3862 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3863 if (index == pv) {
3864 index = count;
3865 break;
3866 }
3867 count++;
3868 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09003869 /* We went off the end of 'clock-indices' without finding it */
3870 if (prop && !vp)
3871 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003872
Grant Likely766e6a42012-04-09 14:50:06 -05003873 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003874 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003875 &clk_name) < 0) {
3876 /*
3877 * Best effort to get the name if the clock has been
3878 * registered with the framework. If the clock isn't
3879 * registered, we return the node name as the name of
3880 * the clock as long as #clock-cells = 0.
3881 */
3882 clk = of_clk_get_from_provider(&clkspec);
3883 if (IS_ERR(clk)) {
3884 if (clkspec.args_count == 0)
3885 clk_name = clkspec.np->name;
3886 else
3887 clk_name = NULL;
3888 } else {
3889 clk_name = __clk_get_name(clk);
3890 clk_put(clk);
3891 }
3892 }
3893
Grant Likely766e6a42012-04-09 14:50:06 -05003894
3895 of_node_put(clkspec.np);
3896 return clk_name;
3897}
3898EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3899
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05003900/**
3901 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3902 * number of parents
3903 * @np: Device node pointer associated with clock provider
3904 * @parents: pointer to char array that hold the parents' names
3905 * @size: size of the @parents array
3906 *
3907 * Return: number of parents for the clock node.
3908 */
3909int of_clk_parent_fill(struct device_node *np, const char **parents,
3910 unsigned int size)
3911{
3912 unsigned int i = 0;
3913
3914 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3915 i++;
3916
3917 return i;
3918}
3919EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3920
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003921struct clock_provider {
3922 of_clk_init_cb_t clk_init_cb;
3923 struct device_node *np;
3924 struct list_head node;
3925};
3926
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003927/*
3928 * This function looks for a parent clock. If there is one, then it
3929 * checks that the provider for this parent clock was initialized, in
3930 * this case the parent clock will be ready.
3931 */
3932static int parent_ready(struct device_node *np)
3933{
3934 int i = 0;
3935
3936 while (true) {
3937 struct clk *clk = of_clk_get(np, i);
3938
3939 /* this parent is ready we can check the next one */
3940 if (!IS_ERR(clk)) {
3941 clk_put(clk);
3942 i++;
3943 continue;
3944 }
3945
3946 /* at least one parent is not ready, we exit now */
3947 if (PTR_ERR(clk) == -EPROBE_DEFER)
3948 return 0;
3949
3950 /*
3951 * Here we make assumption that the device tree is
3952 * written correctly. So an error means that there is
3953 * no more parent. As we didn't exit yet, then the
3954 * previous parent are ready. If there is no clock
3955 * parent, no need to wait for them, then we can
3956 * consider their absence as being ready
3957 */
3958 return 1;
3959 }
3960}
3961
Grant Likely766e6a42012-04-09 14:50:06 -05003962/**
Lee Jonesd56f8992016-02-11 13:19:11 -08003963 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3964 * @np: Device node pointer associated with clock provider
3965 * @index: clock index
Geert Uytterhoevenf7ae7502018-01-03 12:06:14 +01003966 * @flags: pointer to top-level framework flags
Lee Jonesd56f8992016-02-11 13:19:11 -08003967 *
3968 * Detects if the clock-critical property exists and, if so, sets the
3969 * corresponding CLK_IS_CRITICAL flag.
3970 *
3971 * Do not use this function. It exists only for legacy Device Tree
3972 * bindings, such as the one-clock-per-node style that are outdated.
3973 * Those bindings typically put all clock data into .dts and the Linux
3974 * driver has no clock data, thus making it impossible to set this flag
3975 * correctly from the driver. Only those drivers may call
3976 * of_clk_detect_critical from their setup functions.
3977 *
3978 * Return: error code or zero on success
3979 */
3980int of_clk_detect_critical(struct device_node *np,
3981 int index, unsigned long *flags)
3982{
3983 struct property *prop;
3984 const __be32 *cur;
3985 uint32_t idx;
3986
3987 if (!np || !flags)
3988 return -EINVAL;
3989
3990 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
3991 if (index == idx)
3992 *flags |= CLK_IS_CRITICAL;
3993
3994 return 0;
3995}
3996
3997/**
Grant Likely766e6a42012-04-09 14:50:06 -05003998 * of_clk_init() - Scan and init clock providers from the DT
3999 * @matches: array of compatible values and init functions for providers.
4000 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004001 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01004002 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004003 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05004004 */
4005void __init of_clk_init(const struct of_device_id *matches)
4006{
Alex Elder7f7ed582013-08-22 11:31:31 -05004007 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05004008 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004009 struct clock_provider *clk_provider, *next;
4010 bool is_init_done;
4011 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07004012 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05004013
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304014 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03004015 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304016
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004017 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05004018 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004019 struct clock_provider *parent;
4020
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01004021 if (!of_device_is_available(np))
4022 continue;
4023
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004024 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4025 if (!parent) {
4026 list_for_each_entry_safe(clk_provider, next,
4027 &clk_provider_list, node) {
4028 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004029 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004030 kfree(clk_provider);
4031 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004032 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004033 return;
4034 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004035
4036 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004037 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01004038 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004039 }
4040
4041 while (!list_empty(&clk_provider_list)) {
4042 is_init_done = false;
4043 list_for_each_entry_safe(clk_provider, next,
4044 &clk_provider_list, node) {
4045 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004046
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02004047 /* Don't populate platform devices */
4048 of_node_set_flag(clk_provider->np,
4049 OF_POPULATED);
4050
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004051 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004052 of_clk_set_defaults(clk_provider->np, true);
4053
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004054 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004055 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004056 kfree(clk_provider);
4057 is_init_done = true;
4058 }
4059 }
4060
4061 /*
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01004062 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004063 * remaining providers during the last loop, so now we
4064 * initialize all the remaining ones unconditionally
4065 * in case the clock parent was not mandatory
4066 */
4067 if (!is_init_done)
4068 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05004069 }
4070}
4071#endif