blob: 59207cd6138ae6dbc9ecaec36224e1659a96606b [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
David Lechnera12aa8a2018-01-04 19:46:08 -0600147 /*
148 * On UP systems, spin_trylock_irqsave() always returns true, even if
149 * we already hold the lock. So, in that case, we rely only on
150 * reference counting.
151 */
152 if (!IS_ENABLED(CONFIG_SMP) ||
153 !spin_trylock_irqsave(&enable_lock, flags)) {
Mike Turquette533ddeb2013-03-28 13:59:02 -0700154 if (enable_owner == current) {
155 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700156 __acquire(enable_lock);
David Lechnera12aa8a2018-01-04 19:46:08 -0600157 if (!IS_ENABLED(CONFIG_SMP))
158 local_save_flags(flags);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700159 return flags;
160 }
161 spin_lock_irqsave(&enable_lock, flags);
162 }
163 WARN_ON_ONCE(enable_owner != NULL);
164 WARN_ON_ONCE(enable_refcnt != 0);
165 enable_owner = current;
166 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700167 return flags;
168}
169
170static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700171 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700172{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700173 WARN_ON_ONCE(enable_owner != current);
174 WARN_ON_ONCE(enable_refcnt == 0);
175
Stephen Boyda57aa182015-07-24 12:24:48 -0700176 if (--enable_refcnt) {
177 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700178 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700179 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700180 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700181 spin_unlock_irqrestore(&enable_lock, flags);
182}
183
Jerome Brunete55a8392017-12-01 22:51:56 +0100184static bool clk_core_rate_is_protected(struct clk_core *core)
185{
186 return core->protect_count;
187}
188
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700189static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530190{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200191 bool ret = false;
192
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700193 /*
194 * .is_prepared is optional for clocks that can prepare
195 * fall back to software usage counter if it is missing
196 */
197 if (!core->ops->is_prepared)
198 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530199
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200200 if (!clk_pm_runtime_get(core)) {
201 ret = core->ops->is_prepared(core->hw);
202 clk_pm_runtime_put(core);
203 }
204
205 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530206}
207
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700208static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530209{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200210 bool ret = false;
211
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700212 /*
213 * .is_enabled is only mandatory for clocks that gate
214 * fall back to software usage counter if .is_enabled is missing
215 */
216 if (!core->ops->is_enabled)
217 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530218
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200219 /*
220 * Check if clock controller's device is runtime active before
221 * calling .is_enabled callback. If not, assume that clock is
222 * disabled, because we might be called from atomic context, from
223 * which pm_runtime_get() is not allowed.
224 * This function is called mainly from clk_disable_unused_subtree,
225 * which ensures proper runtime pm activation of controller before
226 * taking enable spinlock, but the below check is needed if one tries
227 * to call it from other places.
228 */
229 if (core->dev) {
230 pm_runtime_get_noresume(core->dev);
231 if (!pm_runtime_active(core->dev)) {
232 ret = false;
233 goto done;
234 }
235 }
236
237 ret = core->ops->is_enabled(core->hw);
238done:
Dong Aisheng756efe12017-12-22 17:46:04 +0800239 if (core->dev)
240 pm_runtime_put(core->dev);
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200241
242 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530243}
244
Mike Turquetteb24764902012-03-15 23:11:19 -0700245/*** helper functions ***/
246
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200247const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700248{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100249 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700250}
Niels de Vos48950842012-12-13 13:12:25 +0100251EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700252
Stephen Boyde7df6f62015-08-12 13:04:56 -0700253const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700254{
255 return hw->core->name;
256}
257EXPORT_SYMBOL_GPL(clk_hw_get_name);
258
Russ Dill65800b22012-11-26 11:20:09 -0800259struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700260{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100261 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700262}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800263EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700264
Stephen Boyde7df6f62015-08-12 13:04:56 -0700265unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700266{
267 return hw->core->num_parents;
268}
269EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
270
Stephen Boyde7df6f62015-08-12 13:04:56 -0700271struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700272{
273 return hw->core->parent ? hw->core->parent->hw : NULL;
274}
275EXPORT_SYMBOL_GPL(clk_hw_get_parent);
276
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700277static struct clk_core *__clk_lookup_subtree(const char *name,
278 struct clk_core *core)
279{
280 struct clk_core *child;
281 struct clk_core *ret;
282
283 if (!strcmp(core->name, name))
284 return core;
285
286 hlist_for_each_entry(child, &core->children, child_node) {
287 ret = __clk_lookup_subtree(name, child);
288 if (ret)
289 return ret;
290 }
291
292 return NULL;
293}
294
295static struct clk_core *clk_core_lookup(const char *name)
296{
297 struct clk_core *root_clk;
298 struct clk_core *ret;
299
300 if (!name)
301 return NULL;
302
303 /* search the 'proper' clk tree first */
304 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
305 ret = __clk_lookup_subtree(name, root_clk);
306 if (ret)
307 return ret;
308 }
309
310 /* if not found, then search the orphan tree */
311 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
312 ret = __clk_lookup_subtree(name, root_clk);
313 if (ret)
314 return ret;
315 }
316
317 return NULL;
318}
319
Stephen Boydd6968fc2015-04-30 13:54:13 -0700320static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100321 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100322{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700323 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100324 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900325
326 if (!core->parents[index])
327 core->parents[index] =
328 clk_core_lookup(core->parent_names[index]);
329
330 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100331}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100332
Stephen Boyde7df6f62015-08-12 13:04:56 -0700333struct clk_hw *
334clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700335{
336 struct clk_core *parent;
337
338 parent = clk_core_get_parent_by_index(hw->core, index);
339
340 return !parent ? NULL : parent->hw;
341}
342EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
343
Russ Dill65800b22012-11-26 11:20:09 -0800344unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700345{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100346 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700347}
348
Stephen Boydd6968fc2015-04-30 13:54:13 -0700349static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700350{
351 unsigned long ret;
352
Stephen Boydd6968fc2015-04-30 13:54:13 -0700353 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530354 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700355 goto out;
356 }
357
Stephen Boydd6968fc2015-04-30 13:54:13 -0700358 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700359
Stephen Boyd47b0eeb2016-02-02 17:24:56 -0800360 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -0700361 goto out;
362
Stephen Boydd6968fc2015-04-30 13:54:13 -0700363 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530364 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700365
366out:
367 return ret;
368}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100369
Stephen Boyde7df6f62015-08-12 13:04:56 -0700370unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700371{
372 return clk_core_get_rate_nolock(hw->core);
373}
374EXPORT_SYMBOL_GPL(clk_hw_get_rate);
375
Stephen Boydd6968fc2015-04-30 13:54:13 -0700376static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100377{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700378 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100379 return 0;
380
Stephen Boydd6968fc2015-04-30 13:54:13 -0700381 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100382}
383
Russ Dill65800b22012-11-26 11:20:09 -0800384unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700385{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100386 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700387}
Thierry Redingb05c6832013-09-03 09:43:51 +0200388EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700389
Stephen Boyde7df6f62015-08-12 13:04:56 -0700390unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700391{
392 return hw->core->flags;
393}
394EXPORT_SYMBOL_GPL(clk_hw_get_flags);
395
Stephen Boyde7df6f62015-08-12 13:04:56 -0700396bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700397{
398 return clk_core_is_prepared(hw->core);
399}
400
Jerome Brunete55a8392017-12-01 22:51:56 +0100401bool clk_hw_rate_is_protected(const struct clk_hw *hw)
402{
403 return clk_core_rate_is_protected(hw->core);
404}
405
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200406bool clk_hw_is_enabled(const struct clk_hw *hw)
407{
408 return clk_core_is_enabled(hw->core);
409}
410
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100411bool __clk_is_enabled(struct clk *clk)
412{
413 if (!clk)
414 return false;
415
416 return clk_core_is_enabled(clk->core);
417}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800418EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700419
Stephen Boyd15a02c12015-01-19 18:05:28 -0800420static bool mux_is_better_rate(unsigned long rate, unsigned long now,
421 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100422{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800423 if (flags & CLK_MUX_ROUND_CLOSEST)
424 return abs(now - rate) < abs(best - rate);
425
426 return now <= rate && now > best;
427}
428
Boris Brezillon0817b622015-07-07 20:48:08 +0200429static int
430clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800431 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100432{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100433 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200434 int i, num_parents, ret;
435 unsigned long best = 0;
436 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100437
438 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100439 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
440 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200441 if (core->flags & CLK_SET_RATE_PARENT) {
442 ret = __clk_determine_rate(parent ? parent->hw : NULL,
443 &parent_req);
444 if (ret)
445 return ret;
446
447 best = parent_req.rate;
448 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100449 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200450 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100451 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200452 }
453
James Hogane366fdd2013-07-29 12:25:02 +0100454 goto out;
455 }
456
457 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100458 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100459 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100460 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100461 if (!parent)
462 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200463
464 if (core->flags & CLK_SET_RATE_PARENT) {
465 parent_req = *req;
466 ret = __clk_determine_rate(parent->hw, &parent_req);
467 if (ret)
468 continue;
469 } else {
470 parent_req.rate = clk_core_get_rate_nolock(parent);
471 }
472
473 if (mux_is_better_rate(req->rate, parent_req.rate,
474 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100475 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200476 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100477 }
478 }
479
Boris Brezillon57d866e2015-07-09 22:39:38 +0200480 if (!best_parent)
481 return -EINVAL;
482
James Hogane366fdd2013-07-29 12:25:02 +0100483out:
484 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200485 req->best_parent_hw = best_parent->hw;
486 req->best_parent_rate = best;
487 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100488
Boris Brezillon0817b622015-07-07 20:48:08 +0200489 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100490}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800491
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100492struct clk *__clk_lookup(const char *name)
493{
494 struct clk_core *core = clk_core_lookup(name);
495
496 return !core ? NULL : core->hw->clk;
497}
498
Stephen Boydd6968fc2015-04-30 13:54:13 -0700499static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100500 unsigned long *min_rate,
501 unsigned long *max_rate)
502{
503 struct clk *clk_user;
504
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700505 *min_rate = core->min_rate;
506 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100507
Stephen Boydd6968fc2015-04-30 13:54:13 -0700508 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100509 *min_rate = max(*min_rate, clk_user->min_rate);
510
Stephen Boydd6968fc2015-04-30 13:54:13 -0700511 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100512 *max_rate = min(*max_rate, clk_user->max_rate);
513}
514
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700515void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
516 unsigned long max_rate)
517{
518 hw->core->min_rate = min_rate;
519 hw->core->max_rate = max_rate;
520}
521EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
522
Stephen Boyd15a02c12015-01-19 18:05:28 -0800523/*
524 * Helper for finding best parent to provide a given frequency. This can be used
525 * directly as a determine_rate callback (e.g. for a mux), or from a more
526 * complex clock that may combine a mux with other operations.
527 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200528int __clk_mux_determine_rate(struct clk_hw *hw,
529 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800530{
Boris Brezillon0817b622015-07-07 20:48:08 +0200531 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800532}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800533EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100534
Boris Brezillon0817b622015-07-07 20:48:08 +0200535int __clk_mux_determine_rate_closest(struct clk_hw *hw,
536 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800537{
Boris Brezillon0817b622015-07-07 20:48:08 +0200538 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800539}
540EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
541
Mike Turquetteb24764902012-03-15 23:11:19 -0700542/*** clk api ***/
543
Jerome Brunete55a8392017-12-01 22:51:56 +0100544static void clk_core_rate_unprotect(struct clk_core *core)
545{
546 lockdep_assert_held(&prepare_lock);
547
548 if (!core)
549 return;
550
551 if (WARN_ON(core->protect_count == 0))
552 return;
553
554 if (--core->protect_count > 0)
555 return;
556
557 clk_core_rate_unprotect(core->parent);
558}
559
560static int clk_core_rate_nuke_protect(struct clk_core *core)
561{
562 int ret;
563
564 lockdep_assert_held(&prepare_lock);
565
566 if (!core)
567 return -EINVAL;
568
569 if (core->protect_count == 0)
570 return 0;
571
572 ret = core->protect_count;
573 core->protect_count = 1;
574 clk_core_rate_unprotect(core);
575
576 return ret;
577}
578
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100579/**
580 * clk_rate_exclusive_put - release exclusivity over clock rate control
581 * @clk: the clk over which the exclusivity is released
582 *
583 * clk_rate_exclusive_put() completes a critical section during which a clock
584 * consumer cannot tolerate any other consumer making any operation on the
585 * clock which could result in a rate change or rate glitch. Exclusive clocks
586 * cannot have their rate changed, either directly or indirectly due to changes
587 * further up the parent chain of clocks. As a result, clocks up parent chain
588 * also get under exclusive control of the calling consumer.
589 *
590 * If exlusivity is claimed more than once on clock, even by the same consumer,
591 * the rate effectively gets locked as exclusivity can't be preempted.
592 *
593 * Calls to clk_rate_exclusive_put() must be balanced with calls to
594 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
595 * error status.
596 */
597void clk_rate_exclusive_put(struct clk *clk)
598{
599 if (!clk)
600 return;
601
602 clk_prepare_lock();
603
604 /*
605 * if there is something wrong with this consumer protect count, stop
606 * here before messing with the provider
607 */
608 if (WARN_ON(clk->exclusive_count <= 0))
609 goto out;
610
611 clk_core_rate_unprotect(clk->core);
612 clk->exclusive_count--;
613out:
614 clk_prepare_unlock();
615}
616EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
617
Jerome Brunete55a8392017-12-01 22:51:56 +0100618static void clk_core_rate_protect(struct clk_core *core)
619{
620 lockdep_assert_held(&prepare_lock);
621
622 if (!core)
623 return;
624
625 if (core->protect_count == 0)
626 clk_core_rate_protect(core->parent);
627
628 core->protect_count++;
629}
630
631static void clk_core_rate_restore_protect(struct clk_core *core, int count)
632{
633 lockdep_assert_held(&prepare_lock);
634
635 if (!core)
636 return;
637
638 if (count == 0)
639 return;
640
641 clk_core_rate_protect(core);
642 core->protect_count = count;
643}
644
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100645/**
646 * clk_rate_exclusive_get - get exclusivity over the clk rate control
647 * @clk: the clk over which the exclusity of rate control is requested
648 *
649 * clk_rate_exlusive_get() begins a critical section during which a clock
650 * consumer cannot tolerate any other consumer making any operation on the
651 * clock which could result in a rate change or rate glitch. Exclusive clocks
652 * cannot have their rate changed, either directly or indirectly due to changes
653 * further up the parent chain of clocks. As a result, clocks up parent chain
654 * also get under exclusive control of the calling consumer.
655 *
656 * If exlusivity is claimed more than once on clock, even by the same consumer,
657 * the rate effectively gets locked as exclusivity can't be preempted.
658 *
659 * Calls to clk_rate_exclusive_get() should be balanced with calls to
660 * clk_rate_exclusive_put(). Calls to this function may sleep.
661 * Returns 0 on success, -EERROR otherwise
662 */
663int clk_rate_exclusive_get(struct clk *clk)
664{
665 if (!clk)
666 return 0;
667
668 clk_prepare_lock();
669 clk_core_rate_protect(clk->core);
670 clk->exclusive_count++;
671 clk_prepare_unlock();
672
673 return 0;
674}
675EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
676
Stephen Boydd6968fc2015-04-30 13:54:13 -0700677static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700678{
Stephen Boyda6334722015-05-06 17:00:54 -0700679 lockdep_assert_held(&prepare_lock);
680
Stephen Boydd6968fc2015-04-30 13:54:13 -0700681 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700682 return;
683
Stephen Boydd6968fc2015-04-30 13:54:13 -0700684 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700685 return;
686
Lee Jones2e20fbf2016-02-11 13:19:10 -0800687 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
688 return;
689
Stephen Boydd6968fc2015-04-30 13:54:13 -0700690 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700691 return;
692
Stephen Boydd6968fc2015-04-30 13:54:13 -0700693 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700694
Stephen Boydd6968fc2015-04-30 13:54:13 -0700695 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800696
Stephen Boydd6968fc2015-04-30 13:54:13 -0700697 if (core->ops->unprepare)
698 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700699
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200700 clk_pm_runtime_put(core);
701
Stephen Boydd6968fc2015-04-30 13:54:13 -0700702 trace_clk_unprepare_complete(core);
703 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700704}
705
Dong Aishenga6adc302016-06-30 17:31:11 +0800706static void clk_core_unprepare_lock(struct clk_core *core)
707{
708 clk_prepare_lock();
709 clk_core_unprepare(core);
710 clk_prepare_unlock();
711}
712
Mike Turquetteb24764902012-03-15 23:11:19 -0700713/**
714 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200715 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700716 *
717 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
718 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
719 * if the operation may sleep. One example is a clk which is accessed over
720 * I2c. In the complex case a clk gate operation may require a fast and a slow
721 * part. It is this reason that clk_unprepare and clk_disable are not mutually
722 * exclusive. In fact clk_disable must be called before clk_unprepare.
723 */
724void clk_unprepare(struct clk *clk)
725{
Stephen Boyd63589e92014-03-26 16:06:37 -0700726 if (IS_ERR_OR_NULL(clk))
727 return;
728
Dong Aishenga6adc302016-06-30 17:31:11 +0800729 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700730}
731EXPORT_SYMBOL_GPL(clk_unprepare);
732
Stephen Boydd6968fc2015-04-30 13:54:13 -0700733static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700734{
735 int ret = 0;
736
Stephen Boyda6334722015-05-06 17:00:54 -0700737 lockdep_assert_held(&prepare_lock);
738
Stephen Boydd6968fc2015-04-30 13:54:13 -0700739 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700740 return 0;
741
Stephen Boydd6968fc2015-04-30 13:54:13 -0700742 if (core->prepare_count == 0) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200743 ret = clk_pm_runtime_get(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700744 if (ret)
745 return ret;
746
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200747 ret = clk_core_prepare(core->parent);
748 if (ret)
749 goto runtime_put;
750
Stephen Boydd6968fc2015-04-30 13:54:13 -0700751 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800752
Stephen Boydd6968fc2015-04-30 13:54:13 -0700753 if (core->ops->prepare)
754 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800755
Stephen Boydd6968fc2015-04-30 13:54:13 -0700756 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800757
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200758 if (ret)
759 goto unprepare;
Mike Turquetteb24764902012-03-15 23:11:19 -0700760 }
761
Stephen Boydd6968fc2015-04-30 13:54:13 -0700762 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700763
764 return 0;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200765unprepare:
766 clk_core_unprepare(core->parent);
767runtime_put:
768 clk_pm_runtime_put(core);
769 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700770}
771
Dong Aishenga6adc302016-06-30 17:31:11 +0800772static int clk_core_prepare_lock(struct clk_core *core)
773{
774 int ret;
775
776 clk_prepare_lock();
777 ret = clk_core_prepare(core);
778 clk_prepare_unlock();
779
780 return ret;
781}
782
Mike Turquetteb24764902012-03-15 23:11:19 -0700783/**
784 * clk_prepare - prepare a clock source
785 * @clk: the clk being prepared
786 *
787 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
788 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
789 * operation may sleep. One example is a clk which is accessed over I2c. In
790 * the complex case a clk ungate operation may require a fast and a slow part.
791 * It is this reason that clk_prepare and clk_enable are not mutually
792 * exclusive. In fact clk_prepare must be called before clk_enable.
793 * Returns 0 on success, -EERROR otherwise.
794 */
795int clk_prepare(struct clk *clk)
796{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100797 if (!clk)
798 return 0;
799
Dong Aishenga6adc302016-06-30 17:31:11 +0800800 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700801}
802EXPORT_SYMBOL_GPL(clk_prepare);
803
Stephen Boydd6968fc2015-04-30 13:54:13 -0700804static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700805{
Stephen Boyda6334722015-05-06 17:00:54 -0700806 lockdep_assert_held(&enable_lock);
807
Stephen Boydd6968fc2015-04-30 13:54:13 -0700808 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700809 return;
810
Stephen Boydd6968fc2015-04-30 13:54:13 -0700811 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700812 return;
813
Lee Jones2e20fbf2016-02-11 13:19:10 -0800814 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
815 return;
816
Stephen Boydd6968fc2015-04-30 13:54:13 -0700817 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700818 return;
819
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700820 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800821
Stephen Boydd6968fc2015-04-30 13:54:13 -0700822 if (core->ops->disable)
823 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700824
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700825 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800826
Stephen Boydd6968fc2015-04-30 13:54:13 -0700827 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100828}
829
Dong Aishenga6adc302016-06-30 17:31:11 +0800830static void clk_core_disable_lock(struct clk_core *core)
831{
832 unsigned long flags;
833
834 flags = clk_enable_lock();
835 clk_core_disable(core);
836 clk_enable_unlock(flags);
837}
838
Mike Turquetteb24764902012-03-15 23:11:19 -0700839/**
840 * clk_disable - gate a clock
841 * @clk: the clk being gated
842 *
843 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
844 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
845 * clk if the operation is fast and will never sleep. One example is a
846 * SoC-internal clk which is controlled via simple register writes. In the
847 * complex case a clk gate operation may require a fast and a slow part. It is
848 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
849 * In fact clk_disable must be called before clk_unprepare.
850 */
851void clk_disable(struct clk *clk)
852{
Stephen Boyd63589e92014-03-26 16:06:37 -0700853 if (IS_ERR_OR_NULL(clk))
854 return;
855
Dong Aishenga6adc302016-06-30 17:31:11 +0800856 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700857}
858EXPORT_SYMBOL_GPL(clk_disable);
859
Stephen Boydd6968fc2015-04-30 13:54:13 -0700860static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700861{
862 int ret = 0;
863
Stephen Boyda6334722015-05-06 17:00:54 -0700864 lockdep_assert_held(&enable_lock);
865
Stephen Boydd6968fc2015-04-30 13:54:13 -0700866 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700867 return 0;
868
Stephen Boydd6968fc2015-04-30 13:54:13 -0700869 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700870 return -ESHUTDOWN;
871
Stephen Boydd6968fc2015-04-30 13:54:13 -0700872 if (core->enable_count == 0) {
873 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700874
875 if (ret)
876 return ret;
877
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700878 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800879
Stephen Boydd6968fc2015-04-30 13:54:13 -0700880 if (core->ops->enable)
881 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800882
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700883 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800884
885 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700886 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800887 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700888 }
889 }
890
Stephen Boydd6968fc2015-04-30 13:54:13 -0700891 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700892 return 0;
893}
894
Dong Aishenga6adc302016-06-30 17:31:11 +0800895static int clk_core_enable_lock(struct clk_core *core)
896{
897 unsigned long flags;
898 int ret;
899
900 flags = clk_enable_lock();
901 ret = clk_core_enable(core);
902 clk_enable_unlock(flags);
903
904 return ret;
905}
906
Mike Turquetteb24764902012-03-15 23:11:19 -0700907/**
908 * clk_enable - ungate a clock
909 * @clk: the clk being ungated
910 *
911 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
912 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
913 * if the operation will never sleep. One example is a SoC-internal clk which
914 * is controlled via simple register writes. In the complex case a clk ungate
915 * operation may require a fast and a slow part. It is this reason that
916 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
917 * must be called before clk_enable. Returns 0 on success, -EERROR
918 * otherwise.
919 */
920int clk_enable(struct clk *clk)
921{
Dong Aisheng864e1602015-04-30 14:02:19 -0700922 if (!clk)
923 return 0;
924
Dong Aishenga6adc302016-06-30 17:31:11 +0800925 return clk_core_enable_lock(clk->core);
926}
927EXPORT_SYMBOL_GPL(clk_enable);
928
929static int clk_core_prepare_enable(struct clk_core *core)
930{
931 int ret;
932
933 ret = clk_core_prepare_lock(core);
934 if (ret)
935 return ret;
936
937 ret = clk_core_enable_lock(core);
938 if (ret)
939 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700940
941 return ret;
942}
Dong Aishenga6adc302016-06-30 17:31:11 +0800943
944static void clk_core_disable_unprepare(struct clk_core *core)
945{
946 clk_core_disable_lock(core);
947 clk_core_unprepare_lock(core);
948}
Mike Turquetteb24764902012-03-15 23:11:19 -0700949
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800950static void clk_unprepare_unused_subtree(struct clk_core *core)
951{
952 struct clk_core *child;
953
954 lockdep_assert_held(&prepare_lock);
955
956 hlist_for_each_entry(child, &core->children, child_node)
957 clk_unprepare_unused_subtree(child);
958
959 if (core->prepare_count)
960 return;
961
962 if (core->flags & CLK_IGNORE_UNUSED)
963 return;
964
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200965 if (clk_pm_runtime_get(core))
966 return;
967
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800968 if (clk_core_is_prepared(core)) {
969 trace_clk_unprepare(core);
970 if (core->ops->unprepare_unused)
971 core->ops->unprepare_unused(core->hw);
972 else if (core->ops->unprepare)
973 core->ops->unprepare(core->hw);
974 trace_clk_unprepare_complete(core);
975 }
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200976
977 clk_pm_runtime_put(core);
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800978}
979
980static void clk_disable_unused_subtree(struct clk_core *core)
981{
982 struct clk_core *child;
983 unsigned long flags;
984
985 lockdep_assert_held(&prepare_lock);
986
987 hlist_for_each_entry(child, &core->children, child_node)
988 clk_disable_unused_subtree(child);
989
Dong Aishenga4b35182016-06-30 17:31:13 +0800990 if (core->flags & CLK_OPS_PARENT_ENABLE)
991 clk_core_prepare_enable(core->parent);
992
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200993 if (clk_pm_runtime_get(core))
994 goto unprepare_out;
995
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800996 flags = clk_enable_lock();
997
998 if (core->enable_count)
999 goto unlock_out;
1000
1001 if (core->flags & CLK_IGNORE_UNUSED)
1002 goto unlock_out;
1003
1004 /*
1005 * some gate clocks have special needs during the disable-unused
1006 * sequence. call .disable_unused if available, otherwise fall
1007 * back to .disable
1008 */
1009 if (clk_core_is_enabled(core)) {
1010 trace_clk_disable(core);
1011 if (core->ops->disable_unused)
1012 core->ops->disable_unused(core->hw);
1013 else if (core->ops->disable)
1014 core->ops->disable(core->hw);
1015 trace_clk_disable_complete(core);
1016 }
1017
1018unlock_out:
1019 clk_enable_unlock(flags);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001020 clk_pm_runtime_put(core);
1021unprepare_out:
Dong Aishenga4b35182016-06-30 17:31:13 +08001022 if (core->flags & CLK_OPS_PARENT_ENABLE)
1023 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001024}
1025
1026static bool clk_ignore_unused;
1027static int __init clk_ignore_unused_setup(char *__unused)
1028{
1029 clk_ignore_unused = true;
1030 return 1;
1031}
1032__setup("clk_ignore_unused", clk_ignore_unused_setup);
1033
1034static int clk_disable_unused(void)
1035{
1036 struct clk_core *core;
1037
1038 if (clk_ignore_unused) {
1039 pr_warn("clk: Not disabling unused clocks\n");
1040 return 0;
1041 }
1042
1043 clk_prepare_lock();
1044
1045 hlist_for_each_entry(core, &clk_root_list, child_node)
1046 clk_disable_unused_subtree(core);
1047
1048 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1049 clk_disable_unused_subtree(core);
1050
1051 hlist_for_each_entry(core, &clk_root_list, child_node)
1052 clk_unprepare_unused_subtree(core);
1053
1054 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1055 clk_unprepare_unused_subtree(core);
1056
1057 clk_prepare_unlock();
1058
1059 return 0;
1060}
1061late_initcall_sync(clk_disable_unused);
1062
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001063static int clk_core_determine_round_nolock(struct clk_core *core,
1064 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001065{
Boris Brezillon0817b622015-07-07 20:48:08 +02001066 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001067
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001068 lockdep_assert_held(&prepare_lock);
1069
Stephen Boydd6968fc2015-04-30 13:54:13 -07001070 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001071 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001072
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001073 /*
1074 * At this point, core protection will be disabled if
1075 * - if the provider is not protected at all
1076 * - if the calling consumer is the only one which has exclusivity
1077 * over the provider
1078 */
Jerome Brunete55a8392017-12-01 22:51:56 +01001079 if (clk_core_rate_is_protected(core)) {
1080 req->rate = core->rate;
1081 } else if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001082 return core->ops->determine_rate(core->hw, req);
1083 } else if (core->ops->round_rate) {
1084 rate = core->ops->round_rate(core->hw, req->rate,
1085 &req->best_parent_rate);
1086 if (rate < 0)
1087 return rate;
1088
1089 req->rate = rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001090 } else {
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001091 return -EINVAL;
Boris Brezillon0817b622015-07-07 20:48:08 +02001092 }
1093
1094 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001095}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001096
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001097static void clk_core_init_rate_req(struct clk_core * const core,
1098 struct clk_rate_request *req)
1099{
1100 struct clk_core *parent;
1101
1102 if (WARN_ON(!core || !req))
1103 return;
1104
Mike Turquetteb24764902012-03-15 23:11:19 -07001105 parent = core->parent;
1106 if (parent) {
1107 req->best_parent_hw = parent->hw;
1108 req->best_parent_rate = parent->rate;
1109 } else {
1110 req->best_parent_hw = NULL;
1111 req->best_parent_rate = 0;
1112 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001113}
Mike Turquetteb24764902012-03-15 23:11:19 -07001114
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001115static bool clk_core_can_round(struct clk_core * const core)
1116{
1117 if (core->ops->determine_rate || core->ops->round_rate)
1118 return true;
Mike Turquetteb24764902012-03-15 23:11:19 -07001119
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001120 return false;
1121}
Mike Turquetteb24764902012-03-15 23:11:19 -07001122
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001123static int clk_core_round_rate_nolock(struct clk_core *core,
1124 struct clk_rate_request *req)
1125{
1126 lockdep_assert_held(&prepare_lock);
1127
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001128 if (!core) {
1129 req->rate = 0;
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001130 return 0;
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001131 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001132
1133 clk_core_init_rate_req(core, req);
1134
1135 if (clk_core_can_round(core))
1136 return clk_core_determine_round_nolock(core, req);
1137 else if (core->flags & CLK_SET_RATE_PARENT)
1138 return clk_core_round_rate_nolock(core->parent, req);
1139
1140 req->rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001141 return 0;
1142}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001143
1144/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001145 * __clk_determine_rate - get the closest rate actually supported by a clock
1146 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001147 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001148 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001149 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001150 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001151int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001152{
Boris Brezillon0817b622015-07-07 20:48:08 +02001153 if (!hw) {
1154 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001155 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001156 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001157
Boris Brezillon0817b622015-07-07 20:48:08 +02001158 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001159}
1160EXPORT_SYMBOL_GPL(__clk_determine_rate);
1161
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001162unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1163{
1164 int ret;
1165 struct clk_rate_request req;
1166
1167 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1168 req.rate = rate;
1169
1170 ret = clk_core_round_rate_nolock(hw->core, &req);
1171 if (ret)
1172 return 0;
1173
1174 return req.rate;
1175}
1176EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1177
Mike Turquetteb24764902012-03-15 23:11:19 -07001178/**
1179 * clk_round_rate - round the given rate for a clk
1180 * @clk: the clk for which we are rounding a rate
1181 * @rate: the rate which is to be rounded
1182 *
1183 * Takes in a rate as input and rounds it to a rate that the clk can actually
1184 * use which is then returned. If clk doesn't support round_rate operation
1185 * then the parent rate is returned.
1186 */
1187long clk_round_rate(struct clk *clk, unsigned long rate)
1188{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001189 struct clk_rate_request req;
1190 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001191
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001192 if (!clk)
1193 return 0;
1194
Mike Turquetteeab89f62013-03-28 13:59:01 -07001195 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001196
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001197 if (clk->exclusive_count)
1198 clk_core_rate_unprotect(clk->core);
1199
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001200 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1201 req.rate = rate;
1202
1203 ret = clk_core_round_rate_nolock(clk->core, &req);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001204
1205 if (clk->exclusive_count)
1206 clk_core_rate_protect(clk->core);
1207
Mike Turquetteeab89f62013-03-28 13:59:01 -07001208 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001209
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001210 if (ret)
1211 return ret;
1212
1213 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001214}
1215EXPORT_SYMBOL_GPL(clk_round_rate);
1216
1217/**
1218 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001219 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001220 * @msg: clk notifier type (see include/linux/clk.h)
1221 * @old_rate: old clk rate
1222 * @new_rate: new clk rate
1223 *
1224 * Triggers a notifier call chain on the clk rate-change notification
1225 * for 'clk'. Passes a pointer to the struct clk and the previous
1226 * and current rates to the notifier callback. Intended to be called by
1227 * internal clock code only. Returns NOTIFY_DONE from the last driver
1228 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1229 * a driver returns that.
1230 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001231static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001232 unsigned long old_rate, unsigned long new_rate)
1233{
1234 struct clk_notifier *cn;
1235 struct clk_notifier_data cnd;
1236 int ret = NOTIFY_DONE;
1237
Mike Turquetteb24764902012-03-15 23:11:19 -07001238 cnd.old_rate = old_rate;
1239 cnd.new_rate = new_rate;
1240
1241 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001242 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001243 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001244 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1245 &cnd);
Peter De Schrijver17c34c52017-03-21 12:16:26 +02001246 if (ret & NOTIFY_STOP_MASK)
1247 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001248 }
1249 }
1250
1251 return ret;
1252}
1253
1254/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001255 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001256 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001257 *
1258 * Walks the subtree of clks starting with clk and recalculates accuracies as
1259 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001260 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001261 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001262 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001263static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001264{
1265 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001266 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001267
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001268 lockdep_assert_held(&prepare_lock);
1269
Stephen Boydd6968fc2015-04-30 13:54:13 -07001270 if (core->parent)
1271 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001272
Stephen Boydd6968fc2015-04-30 13:54:13 -07001273 if (core->ops->recalc_accuracy)
1274 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001275 parent_accuracy);
1276 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001277 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001278
Stephen Boydd6968fc2015-04-30 13:54:13 -07001279 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001280 __clk_recalc_accuracies(child);
1281}
1282
Stephen Boydd6968fc2015-04-30 13:54:13 -07001283static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001284{
1285 unsigned long accuracy;
1286
1287 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001288 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1289 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001290
Stephen Boydd6968fc2015-04-30 13:54:13 -07001291 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001292 clk_prepare_unlock();
1293
1294 return accuracy;
1295}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001296
1297/**
1298 * clk_get_accuracy - return the accuracy of clk
1299 * @clk: the clk whose accuracy is being returned
1300 *
1301 * Simply returns the cached accuracy of the clk, unless
1302 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1303 * issued.
1304 * If clk is NULL then returns 0.
1305 */
1306long clk_get_accuracy(struct clk *clk)
1307{
1308 if (!clk)
1309 return 0;
1310
1311 return clk_core_get_accuracy(clk->core);
1312}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001313EXPORT_SYMBOL_GPL(clk_get_accuracy);
1314
Stephen Boydd6968fc2015-04-30 13:54:13 -07001315static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001316 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001317{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001318 unsigned long rate = parent_rate;
1319
1320 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1321 rate = core->ops->recalc_rate(core->hw, parent_rate);
1322 clk_pm_runtime_put(core);
1323 }
1324 return rate;
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001325}
1326
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001327/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001328 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001329 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001330 * @msg: notification type (see include/linux/clk.h)
1331 *
1332 * Walks the subtree of clks starting with clk and recalculates rates as it
1333 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001334 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001335 *
1336 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1337 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001338 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001339static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001340{
1341 unsigned long old_rate;
1342 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001343 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001344
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001345 lockdep_assert_held(&prepare_lock);
1346
Stephen Boydd6968fc2015-04-30 13:54:13 -07001347 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001348
Stephen Boydd6968fc2015-04-30 13:54:13 -07001349 if (core->parent)
1350 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001351
Stephen Boydd6968fc2015-04-30 13:54:13 -07001352 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001353
1354 /*
1355 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1356 * & ABORT_RATE_CHANGE notifiers
1357 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001358 if (core->notifier_count && msg)
1359 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001360
Stephen Boydd6968fc2015-04-30 13:54:13 -07001361 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001362 __clk_recalc_rates(child, msg);
1363}
1364
Stephen Boydd6968fc2015-04-30 13:54:13 -07001365static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001366{
1367 unsigned long rate;
1368
1369 clk_prepare_lock();
1370
Stephen Boydd6968fc2015-04-30 13:54:13 -07001371 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1372 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001373
Stephen Boydd6968fc2015-04-30 13:54:13 -07001374 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001375 clk_prepare_unlock();
1376
1377 return rate;
1378}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001379
Mike Turquetteb24764902012-03-15 23:11:19 -07001380/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001381 * clk_get_rate - return the rate of clk
1382 * @clk: the clk whose rate is being returned
1383 *
1384 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1385 * is set, which means a recalc_rate will be issued.
1386 * If clk is NULL then returns 0.
1387 */
1388unsigned long clk_get_rate(struct clk *clk)
1389{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001390 if (!clk)
1391 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001392
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001393 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001394}
1395EXPORT_SYMBOL_GPL(clk_get_rate);
1396
Stephen Boydd6968fc2015-04-30 13:54:13 -07001397static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001398 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001399{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001400 int i;
James Hogan4935b222013-07-29 12:24:59 +01001401
Masahiro Yamada508f8842015-12-28 19:23:08 +09001402 if (!parent)
1403 return -EINVAL;
1404
Masahiro Yamada470b5e22015-12-28 19:23:09 +09001405 for (i = 0; i < core->num_parents; i++)
1406 if (clk_core_get_parent_by_index(core, i) == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001407 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001408
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001409 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001410}
1411
Heiko Stuebnere6500342015-04-22 22:53:05 +02001412/*
1413 * Update the orphan status of @core and all its children.
1414 */
1415static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1416{
1417 struct clk_core *child;
1418
1419 core->orphan = is_orphan;
1420
1421 hlist_for_each_entry(child, &core->children, child_node)
1422 clk_core_update_orphan_status(child, is_orphan);
1423}
1424
Stephen Boydd6968fc2015-04-30 13:54:13 -07001425static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001426{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001427 bool was_orphan = core->orphan;
1428
Stephen Boydd6968fc2015-04-30 13:54:13 -07001429 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001430
James Hogan903efc52013-08-29 12:10:51 +01001431 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001432 bool becomes_orphan = new_parent->orphan;
1433
James Hogan903efc52013-08-29 12:10:51 +01001434 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001435 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001436 new_parent->new_child = NULL;
1437
Stephen Boydd6968fc2015-04-30 13:54:13 -07001438 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001439
1440 if (was_orphan != becomes_orphan)
1441 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001442 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001443 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001444 if (!was_orphan)
1445 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001446 }
James Hogan4935b222013-07-29 12:24:59 +01001447
Stephen Boydd6968fc2015-04-30 13:54:13 -07001448 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001449}
1450
Stephen Boydd6968fc2015-04-30 13:54:13 -07001451static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001452 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001453{
1454 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001455 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001456
1457 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001458 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1459 *
1460 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001461 * clk_enable().
1462 *
1463 * If the clock is not prepared, then a race with
1464 * clk_enable/disable() is impossible since we already have the
1465 * prepare lock (future calls to clk_enable() need to be preceded by
1466 * a clk_prepare()).
1467 *
1468 * If the clock is prepared, migrate the prepared state to the new
1469 * parent and also protect against a race with clk_enable() by
1470 * forcing the clock and the new parent on. This ensures that all
1471 * future calls to clk_enable() are practically NOPs with respect to
1472 * hardware and software states.
1473 *
1474 * See also: Comment for clk_set_parent() below.
1475 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001476
1477 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1478 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1479 clk_core_prepare_enable(old_parent);
1480 clk_core_prepare_enable(parent);
1481 }
1482
1483 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001484 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001485 clk_core_prepare_enable(parent);
1486 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001487 }
1488
1489 /* update the clk tree topology */
1490 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001491 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001492 clk_enable_unlock(flags);
1493
Stephen Boyd3fa22522014-01-15 10:47:22 -08001494 return old_parent;
1495}
1496
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001497static void __clk_set_parent_after(struct clk_core *core,
1498 struct clk_core *parent,
1499 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001500{
1501 /*
1502 * Finish the migration of prepare state and undo the changes done
1503 * for preventing a race with clk_enable().
1504 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001505 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001506 clk_core_disable_lock(core);
1507 clk_core_disable_unprepare(old_parent);
1508 }
1509
1510 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1511 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1512 clk_core_disable_unprepare(parent);
1513 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001514 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001515}
1516
Stephen Boydd6968fc2015-04-30 13:54:13 -07001517static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001518 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001519{
1520 unsigned long flags;
1521 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001522 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001523
Stephen Boydd6968fc2015-04-30 13:54:13 -07001524 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001525
Stephen Boydd6968fc2015-04-30 13:54:13 -07001526 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001527
James Hogan4935b222013-07-29 12:24:59 +01001528 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001529 if (parent && core->ops->set_parent)
1530 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001531
Stephen Boydd6968fc2015-04-30 13:54:13 -07001532 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001533
James Hogan4935b222013-07-29 12:24:59 +01001534 if (ret) {
1535 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001536 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001537 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001538 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001539
James Hogan4935b222013-07-29 12:24:59 +01001540 return ret;
1541 }
1542
Stephen Boydd6968fc2015-04-30 13:54:13 -07001543 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001544
James Hogan4935b222013-07-29 12:24:59 +01001545 return 0;
1546}
1547
Ulf Hanssona093bde2012-08-31 14:21:28 +02001548/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001549 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001550 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001551 * @parent_rate: the "future" rate of clk's parent
1552 *
1553 * Walks the subtree of clks starting with clk, speculating rates as it
1554 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1555 *
1556 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1557 * pre-rate change notifications and returns early if no clks in the
1558 * subtree have subscribed to the notifications. Note that if a clk does not
1559 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001560 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001561 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001562static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001563 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001564{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001565 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001566 unsigned long new_rate;
1567 int ret = NOTIFY_DONE;
1568
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001569 lockdep_assert_held(&prepare_lock);
1570
Stephen Boydd6968fc2015-04-30 13:54:13 -07001571 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001572
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001573 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001574 if (core->notifier_count)
1575 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001576
Mike Turquette86bcfa22014-02-24 16:08:41 -08001577 if (ret & NOTIFY_STOP_MASK) {
1578 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001579 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001580 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001581 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001582
Stephen Boydd6968fc2015-04-30 13:54:13 -07001583 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001584 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001585 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001586 break;
1587 }
1588
1589out:
1590 return ret;
1591}
1592
Stephen Boydd6968fc2015-04-30 13:54:13 -07001593static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001594 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001595{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001596 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001597
Stephen Boydd6968fc2015-04-30 13:54:13 -07001598 core->new_rate = new_rate;
1599 core->new_parent = new_parent;
1600 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001601 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001602 core->new_child = NULL;
1603 if (new_parent && new_parent != core->parent)
1604 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001605
Stephen Boydd6968fc2015-04-30 13:54:13 -07001606 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001607 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001608 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001609 }
1610}
1611
1612/*
1613 * calculate the new rates returning the topmost clock that has to be
1614 * changed.
1615 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001616static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001617 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001618{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001619 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001620 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001621 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001622 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001623 unsigned long min_rate;
1624 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001625 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001626 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001627
Mike Turquette7452b212012-03-26 14:45:36 -07001628 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001629 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001630 return NULL;
1631
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001632 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001633 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001634 if (parent)
1635 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001636
Stephen Boydd6968fc2015-04-30 13:54:13 -07001637 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001638
James Hogan71472c02013-07-29 12:25:00 +01001639 /* find the closest rate and parent clk/rate */
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001640 if (clk_core_can_round(core)) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001641 struct clk_rate_request req;
1642
1643 req.rate = rate;
1644 req.min_rate = min_rate;
1645 req.max_rate = max_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001646
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001647 clk_core_init_rate_req(core, &req);
1648
1649 ret = clk_core_determine_round_nolock(core, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001650 if (ret < 0)
1651 return NULL;
1652
Boris Brezillon0817b622015-07-07 20:48:08 +02001653 best_parent_rate = req.best_parent_rate;
1654 new_rate = req.rate;
1655 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001656
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001657 if (new_rate < min_rate || new_rate > max_rate)
1658 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001659 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001660 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001661 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001662 return NULL;
1663 } else {
1664 /* pass-through clock with adjustable parent */
1665 top = clk_calc_new_rates(parent, rate);
1666 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001667 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001668 }
1669
James Hogan71472c02013-07-29 12:25:00 +01001670 /* some clocks must be gated to change parent */
1671 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001672 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001673 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001674 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001675 return NULL;
1676 }
1677
James Hogan71472c02013-07-29 12:25:00 +01001678 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001679 if (parent && core->num_parents > 1) {
1680 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001681 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001682 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001683 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001684 return NULL;
1685 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001686 }
1687
Stephen Boydd6968fc2015-04-30 13:54:13 -07001688 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001689 best_parent_rate != parent->rate)
1690 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001691
1692out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001693 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001694
1695 return top;
1696}
1697
1698/*
1699 * Notify about rate changes in a subtree. Always walk down the whole tree
1700 * so that in case of an error we can walk down the whole tree again and
1701 * abort the change.
1702 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001703static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001704 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001705{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001706 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001707 int ret = NOTIFY_DONE;
1708
Stephen Boydd6968fc2015-04-30 13:54:13 -07001709 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301710 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001711
Stephen Boydd6968fc2015-04-30 13:54:13 -07001712 if (core->notifier_count) {
1713 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001714 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001715 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001716 }
1717
Stephen Boydd6968fc2015-04-30 13:54:13 -07001718 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001719 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001720 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001721 continue;
1722 tmp_clk = clk_propagate_rate_change(child, event);
1723 if (tmp_clk)
1724 fail_clk = tmp_clk;
1725 }
1726
Stephen Boydd6968fc2015-04-30 13:54:13 -07001727 /* handle the new child who might not be in core->children yet */
1728 if (core->new_child) {
1729 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001730 if (tmp_clk)
1731 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001732 }
1733
1734 return fail_clk;
1735}
1736
1737/*
1738 * walk down a subtree and set the new rates notifying the rate
1739 * change on the way
1740 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001741static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001742{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001743 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001744 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001745 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001746 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001747 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001748 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001749 struct clk_core *parent = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001750
Stephen Boydd6968fc2015-04-30 13:54:13 -07001751 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001752
Dong Aishengfc8726a2016-06-30 17:31:14 +08001753 if (core->new_parent) {
1754 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001755 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001756 } else if (core->parent) {
1757 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001758 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001759 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001760
Marek Szyprowski588fb542017-11-30 13:14:51 +01001761 if (clk_pm_runtime_get(core))
1762 return;
1763
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001764 if (core->flags & CLK_SET_RATE_UNGATE) {
1765 unsigned long flags;
1766
1767 clk_core_prepare(core);
1768 flags = clk_enable_lock();
1769 clk_core_enable(core);
1770 clk_enable_unlock(flags);
1771 }
1772
Stephen Boydd6968fc2015-04-30 13:54:13 -07001773 if (core->new_parent && core->new_parent != core->parent) {
1774 old_parent = __clk_set_parent_before(core, core->new_parent);
1775 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001776
Stephen Boydd6968fc2015-04-30 13:54:13 -07001777 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001778 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001779 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001780 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001781 core->new_parent_index);
1782 } else if (core->ops->set_parent) {
1783 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001784 }
1785
Stephen Boydd6968fc2015-04-30 13:54:13 -07001786 trace_clk_set_parent_complete(core, core->new_parent);
1787 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001788 }
1789
Dong Aishengfc8726a2016-06-30 17:31:14 +08001790 if (core->flags & CLK_OPS_PARENT_ENABLE)
1791 clk_core_prepare_enable(parent);
1792
Stephen Boydd6968fc2015-04-30 13:54:13 -07001793 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001794
Stephen Boydd6968fc2015-04-30 13:54:13 -07001795 if (!skip_set_rate && core->ops->set_rate)
1796 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001797
Stephen Boydd6968fc2015-04-30 13:54:13 -07001798 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001799
Stephen Boydd6968fc2015-04-30 13:54:13 -07001800 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001801
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001802 if (core->flags & CLK_SET_RATE_UNGATE) {
1803 unsigned long flags;
1804
1805 flags = clk_enable_lock();
1806 clk_core_disable(core);
1807 clk_enable_unlock(flags);
1808 clk_core_unprepare(core);
1809 }
1810
Dong Aishengfc8726a2016-06-30 17:31:14 +08001811 if (core->flags & CLK_OPS_PARENT_ENABLE)
1812 clk_core_disable_unprepare(parent);
1813
Stephen Boydd6968fc2015-04-30 13:54:13 -07001814 if (core->notifier_count && old_rate != core->rate)
1815 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001816
Michael Turquette85e88fa2015-06-20 12:18:03 -07001817 if (core->flags & CLK_RECALC_NEW_RATES)
1818 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001819
Tero Kristo067bb172014-08-21 16:47:45 +03001820 /*
1821 * Use safe iteration, as change_rate can actually swap parents
1822 * for certain clock types.
1823 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001824 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001825 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001826 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001827 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001828 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001829 }
1830
Stephen Boydd6968fc2015-04-30 13:54:13 -07001831 /* handle the new child who might not be in core->children yet */
1832 if (core->new_child)
1833 clk_change_rate(core->new_child);
Marek Szyprowski588fb542017-11-30 13:14:51 +01001834
1835 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001836}
1837
Jerome Brunetca5e0892017-12-01 22:51:55 +01001838static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1839 unsigned long req_rate)
1840{
Jerome Brunete55a8392017-12-01 22:51:56 +01001841 int ret, cnt;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001842 struct clk_rate_request req;
1843
1844 lockdep_assert_held(&prepare_lock);
1845
1846 if (!core)
1847 return 0;
1848
Jerome Brunete55a8392017-12-01 22:51:56 +01001849 /* simulate what the rate would be if it could be freely set */
1850 cnt = clk_core_rate_nuke_protect(core);
1851 if (cnt < 0)
1852 return cnt;
1853
Jerome Brunetca5e0892017-12-01 22:51:55 +01001854 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1855 req.rate = req_rate;
1856
1857 ret = clk_core_round_rate_nolock(core, &req);
1858
Jerome Brunete55a8392017-12-01 22:51:56 +01001859 /* restore the protection */
1860 clk_core_rate_restore_protect(core, cnt);
1861
Jerome Brunetca5e0892017-12-01 22:51:55 +01001862 return ret ? 0 : req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001863}
1864
Stephen Boydd6968fc2015-04-30 13:54:13 -07001865static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001866 unsigned long req_rate)
1867{
1868 struct clk_core *top, *fail_clk;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001869 unsigned long rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001870 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001871
Stephen Boydd6968fc2015-04-30 13:54:13 -07001872 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001873 return 0;
1874
Jerome Brunetca5e0892017-12-01 22:51:55 +01001875 rate = clk_core_req_round_rate_nolock(core, req_rate);
1876
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001877 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001878 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001879 return 0;
1880
Jerome Brunete55a8392017-12-01 22:51:56 +01001881 /* fail on a direct rate set of a protected provider */
1882 if (clk_core_rate_is_protected(core))
1883 return -EBUSY;
1884
Stephen Boydd6968fc2015-04-30 13:54:13 -07001885 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001886 return -EBUSY;
1887
1888 /* calculate new rates and get the topmost changed clock */
Jerome Brunetca5e0892017-12-01 22:51:55 +01001889 top = clk_calc_new_rates(core, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001890 if (!top)
1891 return -EINVAL;
1892
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001893 ret = clk_pm_runtime_get(core);
1894 if (ret)
1895 return ret;
1896
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001897 /* notify that we are about to change rates */
1898 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1899 if (fail_clk) {
1900 pr_debug("%s: failed to set %s rate\n", __func__,
1901 fail_clk->name);
1902 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001903 ret = -EBUSY;
1904 goto err;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001905 }
1906
1907 /* change the rates */
1908 clk_change_rate(top);
1909
Stephen Boydd6968fc2015-04-30 13:54:13 -07001910 core->req_rate = req_rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001911err:
1912 clk_pm_runtime_put(core);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001913
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001914 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001915}
1916
Mike Turquetteb24764902012-03-15 23:11:19 -07001917/**
1918 * clk_set_rate - specify a new rate for clk
1919 * @clk: the clk whose rate is being changed
1920 * @rate: the new rate for clk
1921 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001922 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001923 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001924 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1925 * propagate up to clk's parent; whether or not this happens depends on the
1926 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1927 * after calling .round_rate then upstream parent propagation is ignored. If
1928 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001929 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001930 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1931 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001932 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001933 * Rate changes are accomplished via tree traversal that also recalculates the
1934 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001935 *
1936 * Returns 0 on success, -EERROR otherwise.
1937 */
1938int clk_set_rate(struct clk *clk, unsigned long rate)
1939{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001940 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001941
Mike Turquette89ac8d72013-08-21 23:58:09 -07001942 if (!clk)
1943 return 0;
1944
Mike Turquetteb24764902012-03-15 23:11:19 -07001945 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001946 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001947
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001948 if (clk->exclusive_count)
1949 clk_core_rate_unprotect(clk->core);
1950
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001951 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001952
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001953 if (clk->exclusive_count)
1954 clk_core_rate_protect(clk->core);
1955
Mike Turquetteeab89f62013-03-28 13:59:01 -07001956 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001957
1958 return ret;
1959}
1960EXPORT_SYMBOL_GPL(clk_set_rate);
1961
1962/**
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001963 * clk_set_rate_exclusive - specify a new rate get exclusive control
1964 * @clk: the clk whose rate is being changed
1965 * @rate: the new rate for clk
1966 *
1967 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
1968 * within a critical section
1969 *
1970 * This can be used initially to ensure that at least 1 consumer is
1971 * statisfied when several consumers are competing for exclusivity over the
1972 * same clock provider.
1973 *
1974 * The exclusivity is not applied if setting the rate failed.
1975 *
1976 * Calls to clk_rate_exclusive_get() should be balanced with calls to
1977 * clk_rate_exclusive_put().
1978 *
1979 * Returns 0 on success, -EERROR otherwise.
1980 */
1981int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
1982{
1983 int ret;
1984
1985 if (!clk)
1986 return 0;
1987
1988 /* prevent racing with updates to the clock topology */
1989 clk_prepare_lock();
1990
1991 /*
1992 * The temporary protection removal is not here, on purpose
1993 * This function is meant to be used instead of clk_rate_protect,
1994 * so before the consumer code path protect the clock provider
1995 */
1996
1997 ret = clk_core_set_rate_nolock(clk->core, rate);
1998 if (!ret) {
1999 clk_core_rate_protect(clk->core);
2000 clk->exclusive_count++;
2001 }
2002
2003 clk_prepare_unlock();
2004
2005 return ret;
2006}
2007EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2008
2009/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002010 * clk_set_rate_range - set a rate range for a clock source
2011 * @clk: clock source
2012 * @min: desired minimum clock rate in Hz, inclusive
2013 * @max: desired maximum clock rate in Hz, inclusive
2014 *
2015 * Returns success (0) or negative errno.
2016 */
2017int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2018{
2019 int ret = 0;
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002020 unsigned long old_min, old_max, rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002021
2022 if (!clk)
2023 return 0;
2024
2025 if (min > max) {
2026 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2027 __func__, clk->core->name, clk->dev_id, clk->con_id,
2028 min, max);
2029 return -EINVAL;
2030 }
2031
2032 clk_prepare_lock();
2033
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002034 if (clk->exclusive_count)
2035 clk_core_rate_unprotect(clk->core);
2036
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002037 /* Save the current values in case we need to rollback the change */
2038 old_min = clk->min_rate;
2039 old_max = clk->max_rate;
2040 clk->min_rate = min;
2041 clk->max_rate = max;
2042
2043 rate = clk_core_get_rate_nolock(clk->core);
2044 if (rate < min || rate > max) {
2045 /*
2046 * FIXME:
2047 * We are in bit of trouble here, current rate is outside the
2048 * the requested range. We are going try to request appropriate
2049 * range boundary but there is a catch. It may fail for the
2050 * usual reason (clock broken, clock protected, etc) but also
2051 * because:
2052 * - round_rate() was not favorable and fell on the wrong
2053 * side of the boundary
2054 * - the determine_rate() callback does not really check for
2055 * this corner case when determining the rate
2056 */
2057
2058 if (rate < min)
2059 rate = min;
2060 else
2061 rate = max;
2062
2063 ret = clk_core_set_rate_nolock(clk->core, rate);
2064 if (ret) {
2065 /* rollback the changes */
2066 clk->min_rate = old_min;
2067 clk->max_rate = old_max;
2068 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002069 }
2070
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002071 if (clk->exclusive_count)
2072 clk_core_rate_protect(clk->core);
2073
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002074 clk_prepare_unlock();
2075
2076 return ret;
2077}
2078EXPORT_SYMBOL_GPL(clk_set_rate_range);
2079
2080/**
2081 * clk_set_min_rate - set a minimum clock rate for a clock source
2082 * @clk: clock source
2083 * @rate: desired minimum clock rate in Hz, inclusive
2084 *
2085 * Returns success (0) or negative errno.
2086 */
2087int clk_set_min_rate(struct clk *clk, unsigned long rate)
2088{
2089 if (!clk)
2090 return 0;
2091
2092 return clk_set_rate_range(clk, rate, clk->max_rate);
2093}
2094EXPORT_SYMBOL_GPL(clk_set_min_rate);
2095
2096/**
2097 * clk_set_max_rate - set a maximum clock rate for a clock source
2098 * @clk: clock source
2099 * @rate: desired maximum clock rate in Hz, inclusive
2100 *
2101 * Returns success (0) or negative errno.
2102 */
2103int clk_set_max_rate(struct clk *clk, unsigned long rate)
2104{
2105 if (!clk)
2106 return 0;
2107
2108 return clk_set_rate_range(clk, clk->min_rate, rate);
2109}
2110EXPORT_SYMBOL_GPL(clk_set_max_rate);
2111
2112/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002113 * clk_get_parent - return the parent of a clk
2114 * @clk: the clk whose parent gets returned
2115 *
2116 * Simply returns clk->parent. Returns NULL if clk is NULL.
2117 */
2118struct clk *clk_get_parent(struct clk *clk)
2119{
2120 struct clk *parent;
2121
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002122 if (!clk)
2123 return NULL;
2124
Mike Turquetteeab89f62013-03-28 13:59:01 -07002125 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002126 /* TODO: Create a per-user clk and change callers to call clk_put */
2127 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002128 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002129
2130 return parent;
2131}
2132EXPORT_SYMBOL_GPL(clk_get_parent);
2133
Stephen Boydd6968fc2015-04-30 13:54:13 -07002134static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002135{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002136 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002137
Masahiro Yamada2430a942016-02-09 20:19:14 +09002138 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002139 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002140
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002141 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002142}
2143
Stephen Boydd6968fc2015-04-30 13:54:13 -07002144static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002145 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002146{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002147 clk_reparent(core, new_parent);
2148 __clk_recalc_accuracies(core);
2149 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002150}
2151
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002152void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2153{
2154 if (!hw)
2155 return;
2156
2157 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2158}
2159
Mike Turquetteb24764902012-03-15 23:11:19 -07002160/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002161 * clk_has_parent - check if a clock is a possible parent for another
2162 * @clk: clock source
2163 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002164 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002165 * This function can be used in drivers that need to check that a clock can be
2166 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002167 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002168 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002169 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002170bool clk_has_parent(struct clk *clk, struct clk *parent)
2171{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002172 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002173 unsigned int i;
2174
2175 /* NULL clocks should be nops, so return success if either is NULL. */
2176 if (!clk || !parent)
2177 return true;
2178
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002179 core = clk->core;
2180 parent_core = parent->core;
2181
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002182 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002183 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002184 return true;
2185
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002186 for (i = 0; i < core->num_parents; i++)
2187 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002188 return true;
2189
2190 return false;
2191}
2192EXPORT_SYMBOL_GPL(clk_has_parent);
2193
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002194static int clk_core_set_parent_nolock(struct clk_core *core,
2195 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002196{
2197 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002198 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002199 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002200
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002201 lockdep_assert_held(&prepare_lock);
2202
Stephen Boydd6968fc2015-04-30 13:54:13 -07002203 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002204 return 0;
2205
Stephen Boydd6968fc2015-04-30 13:54:13 -07002206 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002207 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002208
Stephen Boydb61c43c2015-02-02 14:11:25 -08002209 /* verify ops for for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002210 if (core->num_parents > 1 && !core->ops->set_parent)
2211 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08002212
Ulf Hansson031dcc92013-04-02 23:09:38 +02002213 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002214 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2215 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002216
Jerome Brunete55a8392017-12-01 22:51:56 +01002217 if (clk_core_rate_is_protected(core))
2218 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002219
2220 /* try finding the new parent index */
2221 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002222 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002223 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002224 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002225 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002226 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002227 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002228 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002229 }
2230
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002231 ret = clk_pm_runtime_get(core);
2232 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002233 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002234
Mike Turquetteb24764902012-03-15 23:11:19 -07002235 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002236 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002237
2238 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002239 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002240 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07002241
Ulf Hansson031dcc92013-04-02 23:09:38 +02002242 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002243 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002244
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002245 /* propagate rate an accuracy recalculation accordingly */
2246 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002247 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002248 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002249 __clk_recalc_rates(core, POST_RATE_CHANGE);
2250 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002251 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002252
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002253runtime_put:
2254 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002255
2256 return ret;
2257}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002258
2259/**
2260 * clk_set_parent - switch the parent of a mux clk
2261 * @clk: the mux clk whose input we are switching
2262 * @parent: the new input to clk
2263 *
2264 * Re-parent clk to use parent as its new input source. If clk is in
2265 * prepared state, the clk will get enabled for the duration of this call. If
2266 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2267 * that, the reparenting is glitchy in hardware, etc), use the
2268 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2269 *
2270 * After successfully changing clk's parent clk_set_parent will update the
2271 * clk topology, sysfs topology and propagate rate recalculation via
2272 * __clk_recalc_rates.
2273 *
2274 * Returns 0 on success, -EERROR otherwise.
2275 */
2276int clk_set_parent(struct clk *clk, struct clk *parent)
2277{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002278 int ret;
2279
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002280 if (!clk)
2281 return 0;
2282
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002283 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002284
2285 if (clk->exclusive_count)
2286 clk_core_rate_unprotect(clk->core);
2287
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002288 ret = clk_core_set_parent_nolock(clk->core,
2289 parent ? parent->core : NULL);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002290
2291 if (clk->exclusive_count)
2292 clk_core_rate_protect(clk->core);
2293
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002294 clk_prepare_unlock();
2295
2296 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002297}
Mike Turquetteb24764902012-03-15 23:11:19 -07002298EXPORT_SYMBOL_GPL(clk_set_parent);
2299
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002300static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2301{
2302 int ret = -EINVAL;
2303
2304 lockdep_assert_held(&prepare_lock);
2305
2306 if (!core)
2307 return 0;
2308
Jerome Brunete55a8392017-12-01 22:51:56 +01002309 if (clk_core_rate_is_protected(core))
2310 return -EBUSY;
2311
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002312 trace_clk_set_phase(core, degrees);
2313
Shawn Lin7f95bee2018-03-08 14:49:41 +08002314 if (core->ops->set_phase) {
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002315 ret = core->ops->set_phase(core->hw, degrees);
Shawn Lin7f95bee2018-03-08 14:49:41 +08002316 if (!ret)
2317 core->phase = degrees;
2318 }
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002319
2320 trace_clk_set_phase_complete(core, degrees);
2321
2322 return ret;
2323}
2324
Mike Turquetteb24764902012-03-15 23:11:19 -07002325/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002326 * clk_set_phase - adjust the phase shift of a clock signal
2327 * @clk: clock signal source
2328 * @degrees: number of degrees the signal is shifted
2329 *
2330 * Shifts the phase of a clock signal by the specified
2331 * degrees. Returns 0 on success, -EERROR otherwise.
2332 *
2333 * This function makes no distinction about the input or reference
2334 * signal that we adjust the clock signal phase against. For example
2335 * phase locked-loop clock signal generators we may shift phase with
2336 * respect to feedback clock signal input, but for other cases the
2337 * clock phase may be shifted with respect to some other, unspecified
2338 * signal.
2339 *
2340 * Additionally the concept of phase shift does not propagate through
2341 * the clock tree hierarchy, which sets it apart from clock rates and
2342 * clock accuracy. A parent clock phase attribute does not have an
2343 * impact on the phase attribute of a child clock.
2344 */
2345int clk_set_phase(struct clk *clk, int degrees)
2346{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002347 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002348
2349 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002350 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002351
2352 /* sanity check degrees */
2353 degrees %= 360;
2354 if (degrees < 0)
2355 degrees += 360;
2356
2357 clk_prepare_lock();
2358
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002359 if (clk->exclusive_count)
2360 clk_core_rate_unprotect(clk->core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002361
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002362 ret = clk_core_set_phase_nolock(clk->core, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002363
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002364 if (clk->exclusive_count)
2365 clk_core_rate_protect(clk->core);
Mike Turquettee59c5372014-02-18 21:21:25 -08002366
Mike Turquettee59c5372014-02-18 21:21:25 -08002367 clk_prepare_unlock();
2368
Mike Turquettee59c5372014-02-18 21:21:25 -08002369 return ret;
2370}
Maxime Ripard9767b042015-01-20 22:23:43 +01002371EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002372
Stephen Boydd6968fc2015-04-30 13:54:13 -07002373static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002374{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002375 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002376
2377 clk_prepare_lock();
Shawn Lin1f9c63e2018-03-14 08:28:31 +08002378 /* Always try to update cached phase if possible */
2379 if (core->ops->get_phase)
2380 core->phase = core->ops->get_phase(core->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002381 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002382 clk_prepare_unlock();
2383
Mike Turquettee59c5372014-02-18 21:21:25 -08002384 return ret;
2385}
2386
2387/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002388 * clk_get_phase - return the phase shift of a clock signal
2389 * @clk: clock signal source
2390 *
2391 * Returns the phase shift of a clock node in degrees, otherwise returns
2392 * -EERROR.
2393 */
2394int clk_get_phase(struct clk *clk)
2395{
2396 if (!clk)
2397 return 0;
2398
2399 return clk_core_get_phase(clk->core);
2400}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002401EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002402
2403/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002404 * clk_is_match - check if two clk's point to the same hardware clock
2405 * @p: clk compared against q
2406 * @q: clk compared against p
2407 *
2408 * Returns true if the two struct clk pointers both point to the same hardware
2409 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2410 * share the same struct clk_core object.
2411 *
2412 * Returns false otherwise. Note that two NULL clks are treated as matching.
2413 */
2414bool clk_is_match(const struct clk *p, const struct clk *q)
2415{
2416 /* trivial case: identical struct clk's or both NULL */
2417 if (p == q)
2418 return true;
2419
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002420 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002421 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2422 if (p->core == q->core)
2423 return true;
2424
2425 return false;
2426}
2427EXPORT_SYMBOL_GPL(clk_is_match);
2428
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002429/*** debugfs support ***/
2430
2431#ifdef CONFIG_DEBUG_FS
2432#include <linux/debugfs.h>
2433
2434static struct dentry *rootdir;
2435static int inited = 0;
2436static DEFINE_MUTEX(clk_debug_lock);
2437static HLIST_HEAD(clk_debug_list);
2438
2439static struct hlist_head *all_lists[] = {
2440 &clk_root_list,
2441 &clk_orphan_list,
2442 NULL,
2443};
2444
2445static struct hlist_head *orphan_list[] = {
2446 &clk_orphan_list,
2447 NULL,
2448};
2449
2450static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2451 int level)
2452{
2453 if (!c)
2454 return;
2455
Jerome Brunetc5ce26e2017-12-01 22:51:57 +01002456 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %-3d\n",
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002457 level * 3 + 1, "",
2458 30 - level * 3, c->name,
Jerome Brunete55a8392017-12-01 22:51:56 +01002459 c->enable_count, c->prepare_count, c->protect_count,
2460 clk_core_get_rate(c), clk_core_get_accuracy(c),
2461 clk_core_get_phase(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002462}
2463
2464static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2465 int level)
2466{
2467 struct clk_core *child;
2468
2469 if (!c)
2470 return;
2471
2472 clk_summary_show_one(s, c, level);
2473
2474 hlist_for_each_entry(child, &c->children, child_node)
2475 clk_summary_show_subtree(s, child, level + 1);
2476}
2477
2478static int clk_summary_show(struct seq_file *s, void *data)
2479{
2480 struct clk_core *c;
2481 struct hlist_head **lists = (struct hlist_head **)s->private;
2482
Jerome Brunetc5ce26e2017-12-01 22:51:57 +01002483 seq_puts(s, " enable prepare protect \n");
2484 seq_puts(s, " clock count count count rate accuracy phase\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002485 seq_puts(s, "----------------------------------------------------------------------------------------\n");
2486
2487 clk_prepare_lock();
2488
2489 for (; *lists; lists++)
2490 hlist_for_each_entry(c, *lists, child_node)
2491 clk_summary_show_subtree(s, c, 0);
2492
2493 clk_prepare_unlock();
2494
2495 return 0;
2496}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002497DEFINE_SHOW_ATTRIBUTE(clk_summary);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002498
2499static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2500{
2501 if (!c)
2502 return;
2503
Stefan Wahren7cb81132015-04-29 16:36:43 +00002504 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002505 seq_printf(s, "\"%s\": { ", c->name);
2506 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2507 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002508 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002509 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2510 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002511 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2512}
2513
2514static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2515{
2516 struct clk_core *child;
2517
2518 if (!c)
2519 return;
2520
2521 clk_dump_one(s, c, level);
2522
2523 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002524 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002525 clk_dump_subtree(s, child, level + 1);
2526 }
2527
Markus Elfring4d327582017-04-20 08:45:43 +02002528 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002529}
2530
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002531static int clk_dump_show(struct seq_file *s, void *data)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002532{
2533 struct clk_core *c;
2534 bool first_node = true;
2535 struct hlist_head **lists = (struct hlist_head **)s->private;
2536
Markus Elfring4d327582017-04-20 08:45:43 +02002537 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002538 clk_prepare_lock();
2539
2540 for (; *lists; lists++) {
2541 hlist_for_each_entry(c, *lists, child_node) {
2542 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02002543 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002544 first_node = false;
2545 clk_dump_subtree(s, c, 0);
2546 }
2547 }
2548
2549 clk_prepare_unlock();
2550
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002551 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002552 return 0;
2553}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002554DEFINE_SHOW_ATTRIBUTE(clk_dump);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002555
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002556static const struct {
2557 unsigned long flag;
2558 const char *name;
2559} clk_flags[] = {
2560#define ENTRY(f) { f, __stringify(f) }
2561 ENTRY(CLK_SET_RATE_GATE),
2562 ENTRY(CLK_SET_PARENT_GATE),
2563 ENTRY(CLK_SET_RATE_PARENT),
2564 ENTRY(CLK_IGNORE_UNUSED),
2565 ENTRY(CLK_IS_BASIC),
2566 ENTRY(CLK_GET_RATE_NOCACHE),
2567 ENTRY(CLK_SET_RATE_NO_REPARENT),
2568 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2569 ENTRY(CLK_RECALC_NEW_RATES),
2570 ENTRY(CLK_SET_RATE_UNGATE),
2571 ENTRY(CLK_IS_CRITICAL),
2572 ENTRY(CLK_OPS_PARENT_ENABLE),
2573#undef ENTRY
2574};
2575
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002576static int clk_flags_show(struct seq_file *s, void *data)
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002577{
2578 struct clk_core *core = s->private;
2579 unsigned long flags = core->flags;
2580 unsigned int i;
2581
2582 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2583 if (flags & clk_flags[i].flag) {
2584 seq_printf(s, "%s\n", clk_flags[i].name);
2585 flags &= ~clk_flags[i].flag;
2586 }
2587 }
2588 if (flags) {
2589 /* Unknown flags */
2590 seq_printf(s, "0x%lx\n", flags);
2591 }
2592
2593 return 0;
2594}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002595DEFINE_SHOW_ATTRIBUTE(clk_flags);
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002596
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002597static int possible_parents_show(struct seq_file *s, void *data)
Peter De Schrijver92031572017-03-21 15:20:31 +02002598{
2599 struct clk_core *core = s->private;
2600 int i;
2601
2602 for (i = 0; i < core->num_parents - 1; i++)
2603 seq_printf(s, "%s ", core->parent_names[i]);
2604
2605 seq_printf(s, "%s\n", core->parent_names[i]);
2606
2607 return 0;
2608}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002609DEFINE_SHOW_ATTRIBUTE(possible_parents);
Peter De Schrijver92031572017-03-21 15:20:31 +02002610
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002611static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002612{
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002613 struct dentry *root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002614
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002615 if (!core || !pdentry)
2616 return;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002617
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002618 root = debugfs_create_dir(core->name, pdentry);
2619 core->dentry = root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002620
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002621 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
2622 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
2623 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
2624 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
2625 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
2626 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
2627 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
2628 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002629
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002630 if (core->num_parents > 1)
2631 debugfs_create_file("clk_possible_parents", 0444, root, core,
2632 &possible_parents_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002633
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002634 if (core->ops->debug_init)
2635 core->ops->debug_init(core->hw, core->dentry);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002636}
2637
2638/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002639 * clk_debug_register - add a clk node to the debugfs clk directory
2640 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002641 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002642 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2643 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002644 * will be created lazily by clk_debug_init as part of a late_initcall.
2645 */
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002646static void clk_debug_register(struct clk_core *core)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002647{
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002648 mutex_lock(&clk_debug_lock);
2649 hlist_add_head(&core->debug_node, &clk_debug_list);
Stephen Boyddb3188f2018-01-03 16:44:37 -08002650 if (inited)
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002651 clk_debug_create_one(core, rootdir);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002652 mutex_unlock(&clk_debug_lock);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002653}
2654
2655 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002656 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2657 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002658 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002659 * Dynamically removes a clk and all its child nodes from the
2660 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08002661 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002662 */
2663static void clk_debug_unregister(struct clk_core *core)
2664{
2665 mutex_lock(&clk_debug_lock);
2666 hlist_del_init(&core->debug_node);
2667 debugfs_remove_recursive(core->dentry);
2668 core->dentry = NULL;
2669 mutex_unlock(&clk_debug_lock);
2670}
2671
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002672/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002673 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002674 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002675 * clks are often initialized very early during boot before memory can be
2676 * dynamically allocated and well before debugfs is setup. This function
2677 * populates the debugfs clk directory once at boot-time when we know that
2678 * debugfs is setup. It should only be called once at boot-time, all other clks
2679 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002680 */
2681static int __init clk_debug_init(void)
2682{
2683 struct clk_core *core;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002684
2685 rootdir = debugfs_create_dir("clk", NULL);
2686
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002687 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
2688 &clk_summary_fops);
2689 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
2690 &clk_dump_fops);
2691 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
2692 &clk_summary_fops);
2693 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
2694 &clk_dump_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002695
2696 mutex_lock(&clk_debug_lock);
2697 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2698 clk_debug_create_one(core, rootdir);
2699
2700 inited = 1;
2701 mutex_unlock(&clk_debug_lock);
2702
2703 return 0;
2704}
2705late_initcall(clk_debug_init);
2706#else
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002707static inline void clk_debug_register(struct clk_core *core) { }
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002708static inline void clk_debug_reparent(struct clk_core *core,
2709 struct clk_core *new_parent)
2710{
2711}
2712static inline void clk_debug_unregister(struct clk_core *core)
2713{
2714}
2715#endif
2716
Michael Turquette3d3801e2015-02-25 09:11:01 -08002717/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002718 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002719 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07002720 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002721 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002722 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002723 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002724static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002725{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002726 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002727 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002728 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002729 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002730
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002731 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07002732 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002733
Mike Turquetteeab89f62013-03-28 13:59:01 -07002734 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002735
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002736 ret = clk_pm_runtime_get(core);
2737 if (ret)
2738 goto unlock;
2739
Mike Turquetteb24764902012-03-15 23:11:19 -07002740 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002741 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002742 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002743 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002744 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002745 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002746 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002747
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002748 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002749 if (core->ops->set_rate &&
2750 !((core->ops->round_rate || core->ops->determine_rate) &&
2751 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002752 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2753 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002754 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002755 goto out;
2756 }
2757
Stephen Boydd6968fc2015-04-30 13:54:13 -07002758 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002759 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2760 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002761 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002762 goto out;
2763 }
2764
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09002765 if (core->num_parents > 1 && !core->ops->get_parent) {
2766 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2767 __func__, core->name);
2768 ret = -EINVAL;
2769 goto out;
2770 }
2771
Stephen Boydd6968fc2015-04-30 13:54:13 -07002772 if (core->ops->set_rate_and_parent &&
2773 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002774 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002775 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002776 ret = -EINVAL;
2777 goto out;
2778 }
2779
Mike Turquetteb24764902012-03-15 23:11:19 -07002780 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002781 for (i = 0; i < core->num_parents; i++)
2782 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002783 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002784 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002785
Stephen Boydd6968fc2015-04-30 13:54:13 -07002786 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002787
2788 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08002789 * Populate core->parent if parent has already been clk_core_init'd. If
2790 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002791 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07002792 * clk list.
2793 *
2794 * Every time a new clk is clk_init'd then we walk the list of orphan
2795 * clocks and re-parent any that are children of the clock currently
2796 * being clk_init'd.
2797 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02002798 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002799 hlist_add_head(&core->child_node,
2800 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002801 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002802 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002803 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002804 core->orphan = false;
2805 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002806 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002807 core->orphan = true;
2808 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002809
2810 /*
Jerome Brunet541deba2018-02-14 14:43:37 +01002811 * optional platform-specific magic
2812 *
2813 * The .init callback is not used by any of the basic clock types, but
2814 * exists for weird hardware that must perform initialization magic.
2815 * Please consider other ways of solving initialization problems before
2816 * using this callback, as its use is discouraged.
2817 */
2818 if (core->ops->init)
2819 core->ops->init(core->hw);
2820
2821 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002822 * Set clk's accuracy. The preferred method is to use
2823 * .recalc_accuracy. For simple clocks and lazy developers the default
2824 * fallback is to use the parent's accuracy. If a clock doesn't have a
2825 * parent (or is orphaned) then accuracy is set to zero (perfect
2826 * clock).
2827 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002828 if (core->ops->recalc_accuracy)
2829 core->accuracy = core->ops->recalc_accuracy(core->hw,
2830 __clk_get_accuracy(core->parent));
2831 else if (core->parent)
2832 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002833 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002834 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002835
2836 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002837 * Set clk's phase.
2838 * Since a phase is by definition relative to its parent, just
2839 * query the current clock phase, or just assume it's in phase.
2840 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002841 if (core->ops->get_phase)
2842 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002843 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002844 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002845
2846 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002847 * Set clk's rate. The preferred method is to use .recalc_rate. For
2848 * simple clocks and lazy developers the default fallback is to use the
2849 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2850 * then rate is set to zero.
2851 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002852 if (core->ops->recalc_rate)
2853 rate = core->ops->recalc_rate(core->hw,
2854 clk_core_get_rate_nolock(core->parent));
2855 else if (core->parent)
2856 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002857 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002858 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002859 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002860
2861 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01002862 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
2863 * don't get accidentally disabled when walking the orphan tree and
2864 * reparenting clocks
2865 */
2866 if (core->flags & CLK_IS_CRITICAL) {
2867 unsigned long flags;
2868
2869 clk_core_prepare(core);
2870
2871 flags = clk_enable_lock();
2872 clk_core_enable(core);
2873 clk_enable_unlock(flags);
2874 }
2875
2876 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002877 * walk the list of orphan clocks and reparent any that newly finds a
2878 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07002879 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002880 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002881 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002882
Michael Turquette904e6ea2016-07-08 16:32:10 -07002883 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01002884 * We need to use __clk_set_parent_before() and _after() to
2885 * to properly migrate any prepare/enable count of the orphan
2886 * clock. This is important for CLK_IS_CRITICAL clocks, which
2887 * are enabled during init but might not have a parent yet.
Michael Turquette904e6ea2016-07-08 16:32:10 -07002888 */
2889 if (parent) {
Stephen Boydf8f8f1d2017-11-02 00:36:09 -07002890 /* update the clk tree topology */
Jerome Brunet99652a42018-02-14 14:43:36 +01002891 __clk_set_parent_before(orphan, parent);
2892 __clk_set_parent_after(orphan, parent, NULL);
Michael Turquette904e6ea2016-07-08 16:32:10 -07002893 __clk_recalc_accuracies(orphan);
2894 __clk_recalc_rates(orphan, 0);
2895 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002896 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002897
Stephen Boydd6968fc2015-04-30 13:54:13 -07002898 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002899out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002900 clk_pm_runtime_put(core);
2901unlock:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002902 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002903
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002904 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002905 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002906
Mike Turquetted1302a32012-03-29 14:30:40 -07002907 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002908}
2909
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002910struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2911 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002912{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002913 struct clk *clk;
2914
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002915 /* This is to allow this function to be chained to others */
Masahiro Yamadac1de1352015-11-20 14:38:49 +09002916 if (IS_ERR_OR_NULL(hw))
Masahiro Yamada8a231332016-07-19 16:28:47 +09002917 return ERR_CAST(hw);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002918
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002919 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2920 if (!clk)
2921 return ERR_PTR(-ENOMEM);
2922
2923 clk->core = hw->core;
2924 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02002925 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002926 clk->max_rate = ULONG_MAX;
2927
2928 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002929 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002930 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002931
2932 return clk;
2933}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002934
Stephen Boyd73e0e492015-02-06 11:42:43 -08002935void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002936{
2937 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002938 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002939 clk_prepare_unlock();
2940
Leonard Crestez253160a2017-02-20 15:20:56 +02002941 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002942 kfree(clk);
2943}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002944
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002945/**
2946 * clk_register - allocate a new clock, register it and return an opaque cookie
2947 * @dev: device that is registering this clock
2948 * @hw: link to hardware-specific clock data
2949 *
2950 * clk_register is the primary interface for populating the clock tree with new
2951 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05302952 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002953 * rest of the clock API. In the event of an error clk_register will return an
2954 * error code; drivers must test for an error code after calling clk_register.
2955 */
2956struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002957{
Mike Turquetted1302a32012-03-29 14:30:40 -07002958 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002959 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002960
Stephen Boydd6968fc2015-04-30 13:54:13 -07002961 core = kzalloc(sizeof(*core), GFP_KERNEL);
2962 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002963 ret = -ENOMEM;
2964 goto fail_out;
2965 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002966
Stephen Boydd6968fc2015-04-30 13:54:13 -07002967 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2968 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002969 ret = -ENOMEM;
2970 goto fail_name;
2971 }
Jerome Brunet29fd2a32017-12-19 09:33:29 +01002972
2973 if (WARN_ON(!hw->init->ops)) {
2974 ret = -EINVAL;
2975 goto fail_ops;
2976 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002977 core->ops = hw->init->ops;
Jerome Brunet29fd2a32017-12-19 09:33:29 +01002978
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002979 if (dev && pm_runtime_enabled(dev))
2980 core->dev = dev;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002981 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002982 core->owner = dev->driver->owner;
2983 core->hw = hw;
2984 core->flags = hw->init->flags;
2985 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07002986 core->min_rate = 0;
2987 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002988 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002989
Mike Turquetted1302a32012-03-29 14:30:40 -07002990 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002991 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002992 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002993
Stephen Boydd6968fc2015-04-30 13:54:13 -07002994 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002995 ret = -ENOMEM;
2996 goto fail_parent_names;
2997 }
2998
2999
3000 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003001 for (i = 0; i < core->num_parents; i++) {
3002 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003003 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003004 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003005 ret = -ENOMEM;
3006 goto fail_parent_names_copy;
3007 }
3008 }
3009
Masahiro Yamada176d1162015-12-28 19:23:00 +09003010 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3011 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3012 GFP_KERNEL);
3013 if (!core->parents) {
3014 ret = -ENOMEM;
3015 goto fail_parents;
3016 };
3017
Stephen Boydd6968fc2015-04-30 13:54:13 -07003018 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003019
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003020 hw->clk = __clk_create_clk(hw, NULL, NULL);
3021 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003022 ret = PTR_ERR(hw->clk);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003023 goto fail_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003024 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003025
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003026 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003027 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003028 return hw->clk;
3029
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003030 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003031 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003032
Masahiro Yamada176d1162015-12-28 19:23:00 +09003033fail_parents:
3034 kfree(core->parents);
Mike Turquetted1302a32012-03-29 14:30:40 -07003035fail_parent_names_copy:
3036 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003037 kfree_const(core->parent_names[i]);
3038 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07003039fail_parent_names:
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003040fail_ops:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003041 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003042fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003043 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003044fail_out:
3045 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003046}
3047EXPORT_SYMBOL_GPL(clk_register);
3048
Stephen Boyd41438042016-02-05 17:02:52 -08003049/**
3050 * clk_hw_register - register a clk_hw and return an error code
3051 * @dev: device that is registering this clock
3052 * @hw: link to hardware-specific clock data
3053 *
3054 * clk_hw_register is the primary interface for populating the clock tree with
3055 * new clock nodes. It returns an integer equal to zero indicating success or
3056 * less than zero indicating failure. Drivers must test for an error code after
3057 * calling clk_hw_register().
3058 */
3059int clk_hw_register(struct device *dev, struct clk_hw *hw)
3060{
3061 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3062}
3063EXPORT_SYMBOL_GPL(clk_hw_register);
3064
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003065/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003066static void __clk_release(struct kref *ref)
3067{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003068 struct clk_core *core = container_of(ref, struct clk_core, ref);
3069 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003070
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003071 lockdep_assert_held(&prepare_lock);
3072
Stephen Boydd6968fc2015-04-30 13:54:13 -07003073 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003074 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003075 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003076
Stephen Boydd6968fc2015-04-30 13:54:13 -07003077 kfree(core->parent_names);
3078 kfree_const(core->name);
3079 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003080}
3081
3082/*
3083 * Empty clk_ops for unregistered clocks. These are used temporarily
3084 * after clk_unregister() was called on a clock and until last clock
3085 * consumer calls clk_put() and the struct clk object is freed.
3086 */
3087static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3088{
3089 return -ENXIO;
3090}
3091
3092static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3093{
3094 WARN_ON_ONCE(1);
3095}
3096
3097static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3098 unsigned long parent_rate)
3099{
3100 return -ENXIO;
3101}
3102
3103static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3104{
3105 return -ENXIO;
3106}
3107
3108static const struct clk_ops clk_nodrv_ops = {
3109 .enable = clk_nodrv_prepare_enable,
3110 .disable = clk_nodrv_disable_unprepare,
3111 .prepare = clk_nodrv_prepare_enable,
3112 .unprepare = clk_nodrv_disable_unprepare,
3113 .set_rate = clk_nodrv_set_rate,
3114 .set_parent = clk_nodrv_set_parent,
3115};
3116
Mark Brown1df5c932012-04-18 09:07:12 +01003117/**
3118 * clk_unregister - unregister a currently registered clock
3119 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003120 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003121void clk_unregister(struct clk *clk)
3122{
3123 unsigned long flags;
3124
Stephen Boyd6314b672014-09-04 23:37:49 -07003125 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3126 return;
3127
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003128 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003129
3130 clk_prepare_lock();
3131
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003132 if (clk->core->ops == &clk_nodrv_ops) {
3133 pr_err("%s: unregistered clock: %s\n", __func__,
3134 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003135 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003136 }
3137 /*
3138 * Assign empty clock ops for consumers that might still hold
3139 * a reference to this clock.
3140 */
3141 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003142 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003143 clk_enable_unlock(flags);
3144
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003145 if (!hlist_empty(&clk->core->children)) {
3146 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003147 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003148
3149 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003150 hlist_for_each_entry_safe(child, t, &clk->core->children,
3151 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01003152 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003153 }
3154
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003155 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003156
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003157 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003158 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003159 __func__, clk->core->name);
Jerome Brunete55a8392017-12-01 22:51:56 +01003160
3161 if (clk->core->protect_count)
3162 pr_warn("%s: unregistering protected clock: %s\n",
3163 __func__, clk->core->name);
3164
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003165 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003166unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003167 clk_prepare_unlock();
3168}
Mark Brown1df5c932012-04-18 09:07:12 +01003169EXPORT_SYMBOL_GPL(clk_unregister);
3170
Stephen Boyd41438042016-02-05 17:02:52 -08003171/**
3172 * clk_hw_unregister - unregister a currently registered clk_hw
3173 * @hw: hardware-specific clock data to unregister
3174 */
3175void clk_hw_unregister(struct clk_hw *hw)
3176{
3177 clk_unregister(hw->clk);
3178}
3179EXPORT_SYMBOL_GPL(clk_hw_unregister);
3180
Stephen Boyd46c87732012-09-24 13:38:04 -07003181static void devm_clk_release(struct device *dev, void *res)
3182{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003183 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003184}
3185
Stephen Boyd41438042016-02-05 17:02:52 -08003186static void devm_clk_hw_release(struct device *dev, void *res)
3187{
3188 clk_hw_unregister(*(struct clk_hw **)res);
3189}
3190
Stephen Boyd46c87732012-09-24 13:38:04 -07003191/**
3192 * devm_clk_register - resource managed clk_register()
3193 * @dev: device that is registering this clock
3194 * @hw: link to hardware-specific clock data
3195 *
3196 * Managed clk_register(). Clocks returned from this function are
3197 * automatically clk_unregister()ed on driver detach. See clk_register() for
3198 * more information.
3199 */
3200struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3201{
3202 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003203 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003204
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003205 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3206 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003207 return ERR_PTR(-ENOMEM);
3208
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003209 clk = clk_register(dev, hw);
3210 if (!IS_ERR(clk)) {
3211 *clkp = clk;
3212 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003213 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003214 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003215 }
3216
3217 return clk;
3218}
3219EXPORT_SYMBOL_GPL(devm_clk_register);
3220
Stephen Boyd41438042016-02-05 17:02:52 -08003221/**
3222 * devm_clk_hw_register - resource managed clk_hw_register()
3223 * @dev: device that is registering this clock
3224 * @hw: link to hardware-specific clock data
3225 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003226 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003227 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3228 * for more information.
3229 */
3230int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3231{
3232 struct clk_hw **hwp;
3233 int ret;
3234
3235 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3236 if (!hwp)
3237 return -ENOMEM;
3238
3239 ret = clk_hw_register(dev, hw);
3240 if (!ret) {
3241 *hwp = hw;
3242 devres_add(dev, hwp);
3243 } else {
3244 devres_free(hwp);
3245 }
3246
3247 return ret;
3248}
3249EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3250
Stephen Boyd46c87732012-09-24 13:38:04 -07003251static int devm_clk_match(struct device *dev, void *res, void *data)
3252{
3253 struct clk *c = res;
3254 if (WARN_ON(!c))
3255 return 0;
3256 return c == data;
3257}
3258
Stephen Boyd41438042016-02-05 17:02:52 -08003259static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3260{
3261 struct clk_hw *hw = res;
3262
3263 if (WARN_ON(!hw))
3264 return 0;
3265 return hw == data;
3266}
3267
Stephen Boyd46c87732012-09-24 13:38:04 -07003268/**
3269 * devm_clk_unregister - resource managed clk_unregister()
3270 * @clk: clock to unregister
3271 *
3272 * Deallocate a clock allocated with devm_clk_register(). Normally
3273 * this function will not need to be called and the resource management
3274 * code will ensure that the resource is freed.
3275 */
3276void devm_clk_unregister(struct device *dev, struct clk *clk)
3277{
3278 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3279}
3280EXPORT_SYMBOL_GPL(devm_clk_unregister);
3281
Stephen Boyd41438042016-02-05 17:02:52 -08003282/**
3283 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3284 * @dev: device that is unregistering the hardware-specific clock data
3285 * @hw: link to hardware-specific clock data
3286 *
3287 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3288 * this function will not need to be called and the resource management
3289 * code will ensure that the resource is freed.
3290 */
3291void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3292{
3293 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3294 hw));
3295}
3296EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3297
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003298/*
3299 * clkdev helpers
3300 */
3301int __clk_get(struct clk *clk)
3302{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003303 struct clk_core *core = !clk ? NULL : clk->core;
3304
3305 if (core) {
3306 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003307 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003308
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003309 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003310 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003311 return 1;
3312}
3313
3314void __clk_put(struct clk *clk)
3315{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003316 struct module *owner;
3317
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003318 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003319 return;
3320
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003321 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003322
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01003323 /*
3324 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3325 * given user should be balanced with calls to clk_rate_exclusive_put()
3326 * and by that same consumer
3327 */
3328 if (WARN_ON(clk->exclusive_count)) {
3329 /* We voiced our concern, let's sanitize the situation */
3330 clk->core->protect_count -= (clk->exclusive_count - 1);
3331 clk_core_rate_unprotect(clk->core);
3332 clk->exclusive_count = 0;
3333 }
3334
Stephen Boyd50595f82015-02-06 11:42:44 -08003335 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003336 if (clk->min_rate > clk->core->req_rate ||
3337 clk->max_rate < clk->core->req_rate)
3338 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3339
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003340 owner = clk->core->owner;
3341 kref_put(&clk->core->ref, __clk_release);
3342
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003343 clk_prepare_unlock();
3344
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003345 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003346
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003347 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003348}
3349
Mike Turquetteb24764902012-03-15 23:11:19 -07003350/*** clk rate change notifiers ***/
3351
3352/**
3353 * clk_notifier_register - add a clk rate change notifier
3354 * @clk: struct clk * to watch
3355 * @nb: struct notifier_block * with callback info
3356 *
3357 * Request notification when clk's rate changes. This uses an SRCU
3358 * notifier because we want it to block and notifier unregistrations are
3359 * uncommon. The callbacks associated with the notifier must not
3360 * re-enter into the clk framework by calling any top-level clk APIs;
3361 * this will cause a nested prepare_lock mutex.
3362 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003363 * In all notification cases (pre, post and abort rate change) the original
3364 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3365 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003366 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003367 * clk_notifier_register() must be called from non-atomic context.
3368 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3369 * allocation failure; otherwise, passes along the return value of
3370 * srcu_notifier_chain_register().
3371 */
3372int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3373{
3374 struct clk_notifier *cn;
3375 int ret = -ENOMEM;
3376
3377 if (!clk || !nb)
3378 return -EINVAL;
3379
Mike Turquetteeab89f62013-03-28 13:59:01 -07003380 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003381
3382 /* search the list of notifiers for this clk */
3383 list_for_each_entry(cn, &clk_notifier_list, node)
3384 if (cn->clk == clk)
3385 break;
3386
3387 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3388 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02003389 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003390 if (!cn)
3391 goto out;
3392
3393 cn->clk = clk;
3394 srcu_init_notifier_head(&cn->notifier_head);
3395
3396 list_add(&cn->node, &clk_notifier_list);
3397 }
3398
3399 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3400
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003401 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003402
3403out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003404 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003405
3406 return ret;
3407}
3408EXPORT_SYMBOL_GPL(clk_notifier_register);
3409
3410/**
3411 * clk_notifier_unregister - remove a clk rate change notifier
3412 * @clk: struct clk *
3413 * @nb: struct notifier_block * with callback info
3414 *
3415 * Request no further notification for changes to 'clk' and frees memory
3416 * allocated in clk_notifier_register.
3417 *
3418 * Returns -EINVAL if called with null arguments; otherwise, passes
3419 * along the return value of srcu_notifier_chain_unregister().
3420 */
3421int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3422{
3423 struct clk_notifier *cn = NULL;
3424 int ret = -EINVAL;
3425
3426 if (!clk || !nb)
3427 return -EINVAL;
3428
Mike Turquetteeab89f62013-03-28 13:59:01 -07003429 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003430
3431 list_for_each_entry(cn, &clk_notifier_list, node)
3432 if (cn->clk == clk)
3433 break;
3434
3435 if (cn->clk == clk) {
3436 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3437
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003438 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07003439
3440 /* XXX the notifier code should handle this better */
3441 if (!cn->notifier_head.head) {
3442 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08003443 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07003444 kfree(cn);
3445 }
3446
3447 } else {
3448 ret = -ENOENT;
3449 }
3450
Mike Turquetteeab89f62013-03-28 13:59:01 -07003451 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003452
3453 return ret;
3454}
3455EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05003456
3457#ifdef CONFIG_OF
3458/**
3459 * struct of_clk_provider - Clock provider registration structure
3460 * @link: Entry in global list of clock providers
3461 * @node: Pointer to device tree node of clock provider
3462 * @get: Get clock callback. Returns NULL or a struct clk for the
3463 * given clock specifier
3464 * @data: context pointer to be passed into @get callback
3465 */
3466struct of_clk_provider {
3467 struct list_head link;
3468
3469 struct device_node *node;
3470 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003471 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05003472 void *data;
3473};
3474
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303475static const struct of_device_id __clk_of_table_sentinel
3476 __used __section(__clk_of_table_end);
3477
Grant Likely766e6a42012-04-09 14:50:06 -05003478static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003479static DEFINE_MUTEX(of_clk_mutex);
3480
Grant Likely766e6a42012-04-09 14:50:06 -05003481struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3482 void *data)
3483{
3484 return data;
3485}
3486EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3487
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003488struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3489{
3490 return data;
3491}
3492EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3493
Shawn Guo494bfec2012-08-22 21:36:27 +08003494struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3495{
3496 struct clk_onecell_data *clk_data = data;
3497 unsigned int idx = clkspec->args[0];
3498
3499 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02003500 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08003501 return ERR_PTR(-EINVAL);
3502 }
3503
3504 return clk_data->clks[idx];
3505}
3506EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3507
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003508struct clk_hw *
3509of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3510{
3511 struct clk_hw_onecell_data *hw_data = data;
3512 unsigned int idx = clkspec->args[0];
3513
3514 if (idx >= hw_data->num) {
3515 pr_err("%s: invalid index %u\n", __func__, idx);
3516 return ERR_PTR(-EINVAL);
3517 }
3518
3519 return hw_data->hws[idx];
3520}
3521EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3522
Grant Likely766e6a42012-04-09 14:50:06 -05003523/**
3524 * of_clk_add_provider() - Register a clock provider for a node
3525 * @np: Device node pointer associated with clock provider
3526 * @clk_src_get: callback for decoding clock
3527 * @data: context pointer for @clk_src_get callback.
3528 */
3529int of_clk_add_provider(struct device_node *np,
3530 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3531 void *data),
3532 void *data)
3533{
3534 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003535 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003536
Markus Elfring1808a322017-04-20 09:30:52 +02003537 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05003538 if (!cp)
3539 return -ENOMEM;
3540
3541 cp->node = of_node_get(np);
3542 cp->data = data;
3543 cp->get = clk_src_get;
3544
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003545 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003546 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003547 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003548 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05003549
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003550 ret = of_clk_set_defaults(np, true);
3551 if (ret < 0)
3552 of_clk_del_provider(np);
3553
3554 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003555}
3556EXPORT_SYMBOL_GPL(of_clk_add_provider);
3557
3558/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003559 * of_clk_add_hw_provider() - Register a clock provider for a node
3560 * @np: Device node pointer associated with clock provider
3561 * @get: callback for decoding clk_hw
3562 * @data: context pointer for @get callback.
3563 */
3564int of_clk_add_hw_provider(struct device_node *np,
3565 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3566 void *data),
3567 void *data)
3568{
3569 struct of_clk_provider *cp;
3570 int ret;
3571
3572 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3573 if (!cp)
3574 return -ENOMEM;
3575
3576 cp->node = of_node_get(np);
3577 cp->data = data;
3578 cp->get_hw = get;
3579
3580 mutex_lock(&of_clk_mutex);
3581 list_add(&cp->link, &of_clk_providers);
3582 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003583 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003584
3585 ret = of_clk_set_defaults(np, true);
3586 if (ret < 0)
3587 of_clk_del_provider(np);
3588
3589 return ret;
3590}
3591EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3592
Stephen Boydaa795c42017-09-01 16:16:40 -07003593static void devm_of_clk_release_provider(struct device *dev, void *res)
3594{
3595 of_clk_del_provider(*(struct device_node **)res);
3596}
3597
3598int devm_of_clk_add_hw_provider(struct device *dev,
3599 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3600 void *data),
3601 void *data)
3602{
3603 struct device_node **ptr, *np;
3604 int ret;
3605
3606 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3607 GFP_KERNEL);
3608 if (!ptr)
3609 return -ENOMEM;
3610
3611 np = dev->of_node;
3612 ret = of_clk_add_hw_provider(np, get, data);
3613 if (!ret) {
3614 *ptr = np;
3615 devres_add(dev, ptr);
3616 } else {
3617 devres_free(ptr);
3618 }
3619
3620 return ret;
3621}
3622EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3623
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003624/**
Grant Likely766e6a42012-04-09 14:50:06 -05003625 * of_clk_del_provider() - Remove a previously registered clock provider
3626 * @np: Device node pointer associated with clock provider
3627 */
3628void of_clk_del_provider(struct device_node *np)
3629{
3630 struct of_clk_provider *cp;
3631
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003632 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003633 list_for_each_entry(cp, &of_clk_providers, link) {
3634 if (cp->node == np) {
3635 list_del(&cp->link);
3636 of_node_put(cp->node);
3637 kfree(cp);
3638 break;
3639 }
3640 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003641 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003642}
3643EXPORT_SYMBOL_GPL(of_clk_del_provider);
3644
Stephen Boydaa795c42017-09-01 16:16:40 -07003645static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3646{
3647 struct device_node **np = res;
3648
3649 if (WARN_ON(!np || !*np))
3650 return 0;
3651
3652 return *np == data;
3653}
3654
3655void devm_of_clk_del_provider(struct device *dev)
3656{
3657 int ret;
3658
3659 ret = devres_release(dev, devm_of_clk_release_provider,
3660 devm_clk_provider_match, dev->of_node);
3661
3662 WARN_ON(ret);
3663}
3664EXPORT_SYMBOL(devm_of_clk_del_provider);
3665
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003666static struct clk_hw *
3667__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3668 struct of_phandle_args *clkspec)
3669{
3670 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003671
Stephen Boyd74002fc2016-08-25 13:35:36 -07003672 if (provider->get_hw)
3673 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003674
Stephen Boyd74002fc2016-08-25 13:35:36 -07003675 clk = provider->get(clkspec, provider->data);
3676 if (IS_ERR(clk))
3677 return ERR_CAST(clk);
3678 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003679}
3680
Stephen Boyd73e0e492015-02-06 11:42:43 -08003681struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3682 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05003683{
3684 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01003685 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Stephen Boydf155d152016-08-15 14:32:23 -07003686 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -05003687
Stephen Boyd306c3422015-02-05 15:39:11 -08003688 if (!clkspec)
3689 return ERR_PTR(-EINVAL);
3690
Grant Likely766e6a42012-04-09 14:50:06 -05003691 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08003692 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003693 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07003694 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003695 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003696 clk = __clk_create_clk(hw, dev_id, con_id);
Stephen Boydf155d152016-08-15 14:32:23 -07003697 }
Stephen Boyd73e0e492015-02-06 11:42:43 -08003698
Stephen Boydf155d152016-08-15 14:32:23 -07003699 if (!IS_ERR(clk)) {
3700 if (!__clk_get(clk)) {
Stephen Boyd73e0e492015-02-06 11:42:43 -08003701 __clk_free_clk(clk);
3702 clk = ERR_PTR(-ENOENT);
3703 }
3704
Grant Likely766e6a42012-04-09 14:50:06 -05003705 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08003706 }
Grant Likely766e6a42012-04-09 14:50:06 -05003707 }
Stephen Boyd306c3422015-02-05 15:39:11 -08003708 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003709
3710 return clk;
3711}
3712
Stephen Boyd306c3422015-02-05 15:39:11 -08003713/**
3714 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3715 * @clkspec: pointer to a clock specifier data structure
3716 *
3717 * This function looks up a struct clk from the registered list of clock
3718 * providers, an input is a clock specifier data structure as returned
3719 * from the of_parse_phandle_with_args() function call.
3720 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003721struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3722{
Stephen Boyd306c3422015-02-05 15:39:11 -08003723 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05003724}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06003725EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05003726
Stephen Boyd929e7f32016-02-19 15:52:32 -08003727/**
3728 * of_clk_get_parent_count() - Count the number of clocks a device node has
3729 * @np: device node to count
3730 *
3731 * Returns: The number of clocks that are possible parents of this node
3732 */
3733unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07003734{
Stephen Boyd929e7f32016-02-19 15:52:32 -08003735 int count;
3736
3737 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3738 if (count < 0)
3739 return 0;
3740
3741 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07003742}
3743EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3744
Grant Likely766e6a42012-04-09 14:50:06 -05003745const char *of_clk_get_parent_name(struct device_node *np, int index)
3746{
3747 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003748 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003749 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003750 const __be32 *vp;
3751 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003752 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003753 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003754 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05003755
Grant Likely766e6a42012-04-09 14:50:06 -05003756 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3757 &clkspec);
3758 if (rc)
3759 return NULL;
3760
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003761 index = clkspec.args_count ? clkspec.args[0] : 0;
3762 count = 0;
3763
3764 /* if there is an indices property, use it to transfer the index
3765 * specified into an array offset for the clock-output-names property.
3766 */
3767 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3768 if (index == pv) {
3769 index = count;
3770 break;
3771 }
3772 count++;
3773 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09003774 /* We went off the end of 'clock-indices' without finding it */
3775 if (prop && !vp)
3776 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003777
Grant Likely766e6a42012-04-09 14:50:06 -05003778 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003779 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003780 &clk_name) < 0) {
3781 /*
3782 * Best effort to get the name if the clock has been
3783 * registered with the framework. If the clock isn't
3784 * registered, we return the node name as the name of
3785 * the clock as long as #clock-cells = 0.
3786 */
3787 clk = of_clk_get_from_provider(&clkspec);
3788 if (IS_ERR(clk)) {
3789 if (clkspec.args_count == 0)
3790 clk_name = clkspec.np->name;
3791 else
3792 clk_name = NULL;
3793 } else {
3794 clk_name = __clk_get_name(clk);
3795 clk_put(clk);
3796 }
3797 }
3798
Grant Likely766e6a42012-04-09 14:50:06 -05003799
3800 of_node_put(clkspec.np);
3801 return clk_name;
3802}
3803EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3804
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05003805/**
3806 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3807 * number of parents
3808 * @np: Device node pointer associated with clock provider
3809 * @parents: pointer to char array that hold the parents' names
3810 * @size: size of the @parents array
3811 *
3812 * Return: number of parents for the clock node.
3813 */
3814int of_clk_parent_fill(struct device_node *np, const char **parents,
3815 unsigned int size)
3816{
3817 unsigned int i = 0;
3818
3819 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3820 i++;
3821
3822 return i;
3823}
3824EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3825
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003826struct clock_provider {
3827 of_clk_init_cb_t clk_init_cb;
3828 struct device_node *np;
3829 struct list_head node;
3830};
3831
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003832/*
3833 * This function looks for a parent clock. If there is one, then it
3834 * checks that the provider for this parent clock was initialized, in
3835 * this case the parent clock will be ready.
3836 */
3837static int parent_ready(struct device_node *np)
3838{
3839 int i = 0;
3840
3841 while (true) {
3842 struct clk *clk = of_clk_get(np, i);
3843
3844 /* this parent is ready we can check the next one */
3845 if (!IS_ERR(clk)) {
3846 clk_put(clk);
3847 i++;
3848 continue;
3849 }
3850
3851 /* at least one parent is not ready, we exit now */
3852 if (PTR_ERR(clk) == -EPROBE_DEFER)
3853 return 0;
3854
3855 /*
3856 * Here we make assumption that the device tree is
3857 * written correctly. So an error means that there is
3858 * no more parent. As we didn't exit yet, then the
3859 * previous parent are ready. If there is no clock
3860 * parent, no need to wait for them, then we can
3861 * consider their absence as being ready
3862 */
3863 return 1;
3864 }
3865}
3866
Grant Likely766e6a42012-04-09 14:50:06 -05003867/**
Lee Jonesd56f8992016-02-11 13:19:11 -08003868 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3869 * @np: Device node pointer associated with clock provider
3870 * @index: clock index
Geert Uytterhoevenf7ae7502018-01-03 12:06:14 +01003871 * @flags: pointer to top-level framework flags
Lee Jonesd56f8992016-02-11 13:19:11 -08003872 *
3873 * Detects if the clock-critical property exists and, if so, sets the
3874 * corresponding CLK_IS_CRITICAL flag.
3875 *
3876 * Do not use this function. It exists only for legacy Device Tree
3877 * bindings, such as the one-clock-per-node style that are outdated.
3878 * Those bindings typically put all clock data into .dts and the Linux
3879 * driver has no clock data, thus making it impossible to set this flag
3880 * correctly from the driver. Only those drivers may call
3881 * of_clk_detect_critical from their setup functions.
3882 *
3883 * Return: error code or zero on success
3884 */
3885int of_clk_detect_critical(struct device_node *np,
3886 int index, unsigned long *flags)
3887{
3888 struct property *prop;
3889 const __be32 *cur;
3890 uint32_t idx;
3891
3892 if (!np || !flags)
3893 return -EINVAL;
3894
3895 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
3896 if (index == idx)
3897 *flags |= CLK_IS_CRITICAL;
3898
3899 return 0;
3900}
3901
3902/**
Grant Likely766e6a42012-04-09 14:50:06 -05003903 * of_clk_init() - Scan and init clock providers from the DT
3904 * @matches: array of compatible values and init functions for providers.
3905 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003906 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003907 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003908 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003909 */
3910void __init of_clk_init(const struct of_device_id *matches)
3911{
Alex Elder7f7ed582013-08-22 11:31:31 -05003912 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003913 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003914 struct clock_provider *clk_provider, *next;
3915 bool is_init_done;
3916 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07003917 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05003918
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303919 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003920 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303921
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003922 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003923 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003924 struct clock_provider *parent;
3925
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01003926 if (!of_device_is_available(np))
3927 continue;
3928
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003929 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
3930 if (!parent) {
3931 list_for_each_entry_safe(clk_provider, next,
3932 &clk_provider_list, node) {
3933 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003934 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003935 kfree(clk_provider);
3936 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003937 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003938 return;
3939 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003940
3941 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003942 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003943 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003944 }
3945
3946 while (!list_empty(&clk_provider_list)) {
3947 is_init_done = false;
3948 list_for_each_entry_safe(clk_provider, next,
3949 &clk_provider_list, node) {
3950 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003951
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02003952 /* Don't populate platform devices */
3953 of_node_set_flag(clk_provider->np,
3954 OF_POPULATED);
3955
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003956 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003957 of_clk_set_defaults(clk_provider->np, true);
3958
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003959 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003960 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003961 kfree(clk_provider);
3962 is_init_done = true;
3963 }
3964 }
3965
3966 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003967 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003968 * remaining providers during the last loop, so now we
3969 * initialize all the remaining ones unconditionally
3970 * in case the clock parent was not mandatory
3971 */
3972 if (!is_init_done)
3973 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003974 }
3975}
3976#endif