blob: af011974d4ecbf1226fa0d4ecb3db66bf2514afc [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 *
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03009 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
Mike Turquetteb24764902012-03-15 23:11:19 -070010 */
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>
Mike Turquetteb24764902012-03-15 23:11:19 -070027
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020028#include "clk.h"
29
Mike Turquetteb24764902012-03-15 23:11:19 -070030static DEFINE_SPINLOCK(enable_lock);
31static DEFINE_MUTEX(prepare_lock);
32
Mike Turquette533ddeb2013-03-28 13:59:02 -070033static struct task_struct *prepare_owner;
34static struct task_struct *enable_owner;
35
36static int prepare_refcnt;
37static int enable_refcnt;
38
Mike Turquetteb24764902012-03-15 23:11:19 -070039static HLIST_HEAD(clk_root_list);
40static HLIST_HEAD(clk_orphan_list);
41static LIST_HEAD(clk_notifier_list);
42
Michael Turquetteb09d6d92015-01-29 14:22:50 -080043/*** private data structures ***/
44
45struct clk_core {
46 const char *name;
47 const struct clk_ops *ops;
48 struct clk_hw *hw;
49 struct module *owner;
Marek Szyprowski9a34b452017-08-21 10:04:59 +020050 struct device *dev;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080051 struct clk_core *parent;
52 const char **parent_names;
53 struct clk_core **parents;
54 u8 num_parents;
55 u8 new_parent_index;
56 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010057 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080058 unsigned long new_rate;
59 struct clk_core *new_parent;
60 struct clk_core *new_child;
61 unsigned long flags;
Heiko Stuebnere6500342015-04-22 22:53:05 +020062 bool orphan;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080063 unsigned int enable_count;
64 unsigned int prepare_count;
Jerome Brunete55a8392017-12-01 22:51:56 +010065 unsigned int protect_count;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070066 unsigned long min_rate;
67 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080068 unsigned long accuracy;
69 int phase;
Jerome Brunet9fba7382018-06-19 16:41:41 +020070 struct clk_duty duty;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080071 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
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200429int clk_mux_determine_rate_flags(struct clk_hw *hw,
430 struct clk_rate_request *req,
431 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}
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200491EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800492
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100493struct clk *__clk_lookup(const char *name)
494{
495 struct clk_core *core = clk_core_lookup(name);
496
497 return !core ? NULL : core->hw->clk;
498}
499
Stephen Boydd6968fc2015-04-30 13:54:13 -0700500static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100501 unsigned long *min_rate,
502 unsigned long *max_rate)
503{
504 struct clk *clk_user;
505
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700506 *min_rate = core->min_rate;
507 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100508
Stephen Boydd6968fc2015-04-30 13:54:13 -0700509 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100510 *min_rate = max(*min_rate, clk_user->min_rate);
511
Stephen Boydd6968fc2015-04-30 13:54:13 -0700512 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100513 *max_rate = min(*max_rate, clk_user->max_rate);
514}
515
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700516void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
517 unsigned long max_rate)
518{
519 hw->core->min_rate = min_rate;
520 hw->core->max_rate = max_rate;
521}
522EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
523
Stephen Boyd15a02c12015-01-19 18:05:28 -0800524/*
525 * Helper for finding best parent to provide a given frequency. This can be used
526 * directly as a determine_rate callback (e.g. for a mux), or from a more
527 * complex clock that may combine a mux with other operations.
528 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200529int __clk_mux_determine_rate(struct clk_hw *hw,
530 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800531{
Boris Brezillon0817b622015-07-07 20:48:08 +0200532 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800533}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800534EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100535
Boris Brezillon0817b622015-07-07 20:48:08 +0200536int __clk_mux_determine_rate_closest(struct clk_hw *hw,
537 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800538{
Boris Brezillon0817b622015-07-07 20:48:08 +0200539 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800540}
541EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
542
Mike Turquetteb24764902012-03-15 23:11:19 -0700543/*** clk api ***/
544
Jerome Brunete55a8392017-12-01 22:51:56 +0100545static void clk_core_rate_unprotect(struct clk_core *core)
546{
547 lockdep_assert_held(&prepare_lock);
548
549 if (!core)
550 return;
551
Fabio Estevamab525dc2018-01-16 10:50:34 -0200552 if (WARN(core->protect_count == 0,
553 "%s already unprotected\n", core->name))
Jerome Brunete55a8392017-12-01 22:51:56 +0100554 return;
555
556 if (--core->protect_count > 0)
557 return;
558
559 clk_core_rate_unprotect(core->parent);
560}
561
562static int clk_core_rate_nuke_protect(struct clk_core *core)
563{
564 int ret;
565
566 lockdep_assert_held(&prepare_lock);
567
568 if (!core)
569 return -EINVAL;
570
571 if (core->protect_count == 0)
572 return 0;
573
574 ret = core->protect_count;
575 core->protect_count = 1;
576 clk_core_rate_unprotect(core);
577
578 return ret;
579}
580
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100581/**
582 * clk_rate_exclusive_put - release exclusivity over clock rate control
583 * @clk: the clk over which the exclusivity is released
584 *
585 * clk_rate_exclusive_put() completes a critical section during which a clock
586 * consumer cannot tolerate any other consumer making any operation on the
587 * clock which could result in a rate change or rate glitch. Exclusive clocks
588 * cannot have their rate changed, either directly or indirectly due to changes
589 * further up the parent chain of clocks. As a result, clocks up parent chain
590 * also get under exclusive control of the calling consumer.
591 *
592 * If exlusivity is claimed more than once on clock, even by the same consumer,
593 * the rate effectively gets locked as exclusivity can't be preempted.
594 *
595 * Calls to clk_rate_exclusive_put() must be balanced with calls to
596 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
597 * error status.
598 */
599void clk_rate_exclusive_put(struct clk *clk)
600{
601 if (!clk)
602 return;
603
604 clk_prepare_lock();
605
606 /*
607 * if there is something wrong with this consumer protect count, stop
608 * here before messing with the provider
609 */
610 if (WARN_ON(clk->exclusive_count <= 0))
611 goto out;
612
613 clk_core_rate_unprotect(clk->core);
614 clk->exclusive_count--;
615out:
616 clk_prepare_unlock();
617}
618EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
619
Jerome Brunete55a8392017-12-01 22:51:56 +0100620static void clk_core_rate_protect(struct clk_core *core)
621{
622 lockdep_assert_held(&prepare_lock);
623
624 if (!core)
625 return;
626
627 if (core->protect_count == 0)
628 clk_core_rate_protect(core->parent);
629
630 core->protect_count++;
631}
632
633static void clk_core_rate_restore_protect(struct clk_core *core, int count)
634{
635 lockdep_assert_held(&prepare_lock);
636
637 if (!core)
638 return;
639
640 if (count == 0)
641 return;
642
643 clk_core_rate_protect(core);
644 core->protect_count = count;
645}
646
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100647/**
648 * clk_rate_exclusive_get - get exclusivity over the clk rate control
649 * @clk: the clk over which the exclusity of rate control is requested
650 *
651 * clk_rate_exlusive_get() begins a critical section during which a clock
652 * consumer cannot tolerate any other consumer making any operation on the
653 * clock which could result in a rate change or rate glitch. Exclusive clocks
654 * cannot have their rate changed, either directly or indirectly due to changes
655 * further up the parent chain of clocks. As a result, clocks up parent chain
656 * also get under exclusive control of the calling consumer.
657 *
658 * If exlusivity is claimed more than once on clock, even by the same consumer,
659 * the rate effectively gets locked as exclusivity can't be preempted.
660 *
661 * Calls to clk_rate_exclusive_get() should be balanced with calls to
662 * clk_rate_exclusive_put(). Calls to this function may sleep.
663 * Returns 0 on success, -EERROR otherwise
664 */
665int clk_rate_exclusive_get(struct clk *clk)
666{
667 if (!clk)
668 return 0;
669
670 clk_prepare_lock();
671 clk_core_rate_protect(clk->core);
672 clk->exclusive_count++;
673 clk_prepare_unlock();
674
675 return 0;
676}
677EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
678
Stephen Boydd6968fc2015-04-30 13:54:13 -0700679static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700680{
Stephen Boyda6334722015-05-06 17:00:54 -0700681 lockdep_assert_held(&prepare_lock);
682
Stephen Boydd6968fc2015-04-30 13:54:13 -0700683 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700684 return;
685
Fabio Estevamab525dc2018-01-16 10:50:34 -0200686 if (WARN(core->prepare_count == 0,
687 "%s already unprepared\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700688 return;
689
Fabio Estevamab525dc2018-01-16 10:50:34 -0200690 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
691 "Unpreparing critical %s\n", core->name))
Lee Jones2e20fbf2016-02-11 13:19:10 -0800692 return;
693
Jerome Brunet9461f7b2018-06-19 15:40:51 +0200694 if (core->flags & CLK_SET_RATE_GATE)
695 clk_core_rate_unprotect(core);
696
Stephen Boydd6968fc2015-04-30 13:54:13 -0700697 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700698 return;
699
Fabio Estevamab525dc2018-01-16 10:50:34 -0200700 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700701
Stephen Boydd6968fc2015-04-30 13:54:13 -0700702 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800703
Stephen Boydd6968fc2015-04-30 13:54:13 -0700704 if (core->ops->unprepare)
705 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700706
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200707 clk_pm_runtime_put(core);
708
Stephen Boydd6968fc2015-04-30 13:54:13 -0700709 trace_clk_unprepare_complete(core);
710 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700711}
712
Dong Aishenga6adc302016-06-30 17:31:11 +0800713static void clk_core_unprepare_lock(struct clk_core *core)
714{
715 clk_prepare_lock();
716 clk_core_unprepare(core);
717 clk_prepare_unlock();
718}
719
Mike Turquetteb24764902012-03-15 23:11:19 -0700720/**
721 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200722 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700723 *
724 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
725 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
726 * if the operation may sleep. One example is a clk which is accessed over
727 * I2c. In the complex case a clk gate operation may require a fast and a slow
728 * part. It is this reason that clk_unprepare and clk_disable are not mutually
729 * exclusive. In fact clk_disable must be called before clk_unprepare.
730 */
731void clk_unprepare(struct clk *clk)
732{
Stephen Boyd63589e92014-03-26 16:06:37 -0700733 if (IS_ERR_OR_NULL(clk))
734 return;
735
Dong Aishenga6adc302016-06-30 17:31:11 +0800736 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700737}
738EXPORT_SYMBOL_GPL(clk_unprepare);
739
Stephen Boydd6968fc2015-04-30 13:54:13 -0700740static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700741{
742 int ret = 0;
743
Stephen Boyda6334722015-05-06 17:00:54 -0700744 lockdep_assert_held(&prepare_lock);
745
Stephen Boydd6968fc2015-04-30 13:54:13 -0700746 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700747 return 0;
748
Stephen Boydd6968fc2015-04-30 13:54:13 -0700749 if (core->prepare_count == 0) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200750 ret = clk_pm_runtime_get(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700751 if (ret)
752 return ret;
753
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200754 ret = clk_core_prepare(core->parent);
755 if (ret)
756 goto runtime_put;
757
Stephen Boydd6968fc2015-04-30 13:54:13 -0700758 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800759
Stephen Boydd6968fc2015-04-30 13:54:13 -0700760 if (core->ops->prepare)
761 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800762
Stephen Boydd6968fc2015-04-30 13:54:13 -0700763 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800764
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200765 if (ret)
766 goto unprepare;
Mike Turquetteb24764902012-03-15 23:11:19 -0700767 }
768
Stephen Boydd6968fc2015-04-30 13:54:13 -0700769 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700770
Jerome Brunet9461f7b2018-06-19 15:40:51 +0200771 /*
772 * CLK_SET_RATE_GATE is a special case of clock protection
773 * Instead of a consumer claiming exclusive rate control, it is
774 * actually the provider which prevents any consumer from making any
775 * operation which could result in a rate change or rate glitch while
776 * the clock is prepared.
777 */
778 if (core->flags & CLK_SET_RATE_GATE)
779 clk_core_rate_protect(core);
780
Mike Turquetteb24764902012-03-15 23:11:19 -0700781 return 0;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200782unprepare:
783 clk_core_unprepare(core->parent);
784runtime_put:
785 clk_pm_runtime_put(core);
786 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700787}
788
Dong Aishenga6adc302016-06-30 17:31:11 +0800789static int clk_core_prepare_lock(struct clk_core *core)
790{
791 int ret;
792
793 clk_prepare_lock();
794 ret = clk_core_prepare(core);
795 clk_prepare_unlock();
796
797 return ret;
798}
799
Mike Turquetteb24764902012-03-15 23:11:19 -0700800/**
801 * clk_prepare - prepare a clock source
802 * @clk: the clk being prepared
803 *
804 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
805 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
806 * operation may sleep. One example is a clk which is accessed over I2c. In
807 * the complex case a clk ungate operation may require a fast and a slow part.
808 * It is this reason that clk_prepare and clk_enable are not mutually
809 * exclusive. In fact clk_prepare must be called before clk_enable.
810 * Returns 0 on success, -EERROR otherwise.
811 */
812int clk_prepare(struct clk *clk)
813{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100814 if (!clk)
815 return 0;
816
Dong Aishenga6adc302016-06-30 17:31:11 +0800817 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700818}
819EXPORT_SYMBOL_GPL(clk_prepare);
820
Stephen Boydd6968fc2015-04-30 13:54:13 -0700821static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700822{
Stephen Boyda6334722015-05-06 17:00:54 -0700823 lockdep_assert_held(&enable_lock);
824
Stephen Boydd6968fc2015-04-30 13:54:13 -0700825 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700826 return;
827
Fabio Estevamab525dc2018-01-16 10:50:34 -0200828 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700829 return;
830
Fabio Estevamab525dc2018-01-16 10:50:34 -0200831 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
832 "Disabling critical %s\n", core->name))
Lee Jones2e20fbf2016-02-11 13:19:10 -0800833 return;
834
Stephen Boydd6968fc2015-04-30 13:54:13 -0700835 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700836 return;
837
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700838 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800839
Stephen Boydd6968fc2015-04-30 13:54:13 -0700840 if (core->ops->disable)
841 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700842
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700843 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800844
Stephen Boydd6968fc2015-04-30 13:54:13 -0700845 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100846}
847
Dong Aishenga6adc302016-06-30 17:31:11 +0800848static void clk_core_disable_lock(struct clk_core *core)
849{
850 unsigned long flags;
851
852 flags = clk_enable_lock();
853 clk_core_disable(core);
854 clk_enable_unlock(flags);
855}
856
Mike Turquetteb24764902012-03-15 23:11:19 -0700857/**
858 * clk_disable - gate a clock
859 * @clk: the clk being gated
860 *
861 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
862 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
863 * clk if the operation is fast and will never sleep. One example is a
864 * SoC-internal clk which is controlled via simple register writes. In the
865 * complex case a clk gate operation may require a fast and a slow part. It is
866 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
867 * In fact clk_disable must be called before clk_unprepare.
868 */
869void clk_disable(struct clk *clk)
870{
Stephen Boyd63589e92014-03-26 16:06:37 -0700871 if (IS_ERR_OR_NULL(clk))
872 return;
873
Dong Aishenga6adc302016-06-30 17:31:11 +0800874 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700875}
876EXPORT_SYMBOL_GPL(clk_disable);
877
Stephen Boydd6968fc2015-04-30 13:54:13 -0700878static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700879{
880 int ret = 0;
881
Stephen Boyda6334722015-05-06 17:00:54 -0700882 lockdep_assert_held(&enable_lock);
883
Stephen Boydd6968fc2015-04-30 13:54:13 -0700884 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700885 return 0;
886
Fabio Estevamab525dc2018-01-16 10:50:34 -0200887 if (WARN(core->prepare_count == 0,
888 "Enabling unprepared %s\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700889 return -ESHUTDOWN;
890
Stephen Boydd6968fc2015-04-30 13:54:13 -0700891 if (core->enable_count == 0) {
892 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700893
894 if (ret)
895 return ret;
896
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700897 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800898
Stephen Boydd6968fc2015-04-30 13:54:13 -0700899 if (core->ops->enable)
900 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800901
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700902 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800903
904 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700905 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800906 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700907 }
908 }
909
Stephen Boydd6968fc2015-04-30 13:54:13 -0700910 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700911 return 0;
912}
913
Dong Aishenga6adc302016-06-30 17:31:11 +0800914static int clk_core_enable_lock(struct clk_core *core)
915{
916 unsigned long flags;
917 int ret;
918
919 flags = clk_enable_lock();
920 ret = clk_core_enable(core);
921 clk_enable_unlock(flags);
922
923 return ret;
924}
925
Keerthy43536542018-09-04 12:19:36 +0530926/**
927 * clk_gate_restore_context - restore context for poweroff
928 * @hw: the clk_hw pointer of clock whose state is to be restored
929 *
930 * The clock gate restore context function enables or disables
931 * the gate clocks based on the enable_count. This is done in cases
932 * where the clock context is lost and based on the enable_count
933 * the clock either needs to be enabled/disabled. This
934 * helps restore the state of gate clocks.
935 */
936void clk_gate_restore_context(struct clk_hw *hw)
937{
Stephen Boyd9be76622018-10-11 09:28:13 -0700938 struct clk_core *core = hw->core;
939
940 if (core->enable_count)
941 core->ops->enable(hw);
Keerthy43536542018-09-04 12:19:36 +0530942 else
Stephen Boyd9be76622018-10-11 09:28:13 -0700943 core->ops->disable(hw);
Keerthy43536542018-09-04 12:19:36 +0530944}
945EXPORT_SYMBOL_GPL(clk_gate_restore_context);
946
Stephen Boyd9be76622018-10-11 09:28:13 -0700947static int clk_core_save_context(struct clk_core *core)
Russ Dill8b95d1c2018-09-04 12:19:35 +0530948{
949 struct clk_core *child;
950 int ret = 0;
951
Stephen Boyd9be76622018-10-11 09:28:13 -0700952 hlist_for_each_entry(child, &core->children, child_node) {
953 ret = clk_core_save_context(child);
Russ Dill8b95d1c2018-09-04 12:19:35 +0530954 if (ret < 0)
955 return ret;
956 }
957
Stephen Boyd9be76622018-10-11 09:28:13 -0700958 if (core->ops && core->ops->save_context)
959 ret = core->ops->save_context(core->hw);
Russ Dill8b95d1c2018-09-04 12:19:35 +0530960
961 return ret;
962}
963
Stephen Boyd9be76622018-10-11 09:28:13 -0700964static void clk_core_restore_context(struct clk_core *core)
Russ Dill8b95d1c2018-09-04 12:19:35 +0530965{
966 struct clk_core *child;
967
Stephen Boyd9be76622018-10-11 09:28:13 -0700968 if (core->ops && core->ops->restore_context)
969 core->ops->restore_context(core->hw);
Russ Dill8b95d1c2018-09-04 12:19:35 +0530970
Stephen Boyd9be76622018-10-11 09:28:13 -0700971 hlist_for_each_entry(child, &core->children, child_node)
972 clk_core_restore_context(child);
Russ Dill8b95d1c2018-09-04 12:19:35 +0530973}
974
975/**
976 * clk_save_context - save clock context for poweroff
977 *
978 * Saves the context of the clock register for powerstates in which the
979 * contents of the registers will be lost. Occurs deep within the suspend
980 * code. Returns 0 on success.
981 */
982int clk_save_context(void)
983{
984 struct clk_core *clk;
985 int ret;
986
987 hlist_for_each_entry(clk, &clk_root_list, child_node) {
Stephen Boyd9be76622018-10-11 09:28:13 -0700988 ret = clk_core_save_context(clk);
Russ Dill8b95d1c2018-09-04 12:19:35 +0530989 if (ret < 0)
990 return ret;
991 }
992
993 hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
Stephen Boyd9be76622018-10-11 09:28:13 -0700994 ret = clk_core_save_context(clk);
Russ Dill8b95d1c2018-09-04 12:19:35 +0530995 if (ret < 0)
996 return ret;
997 }
998
999 return 0;
1000}
1001EXPORT_SYMBOL_GPL(clk_save_context);
1002
1003/**
1004 * clk_restore_context - restore clock context after poweroff
1005 *
1006 * Restore the saved clock context upon resume.
1007 *
1008 */
1009void clk_restore_context(void)
1010{
Stephen Boyd9be76622018-10-11 09:28:13 -07001011 struct clk_core *core;
Russ Dill8b95d1c2018-09-04 12:19:35 +05301012
Stephen Boyd9be76622018-10-11 09:28:13 -07001013 hlist_for_each_entry(core, &clk_root_list, child_node)
1014 clk_core_restore_context(core);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301015
Stephen Boyd9be76622018-10-11 09:28:13 -07001016 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1017 clk_core_restore_context(core);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301018}
1019EXPORT_SYMBOL_GPL(clk_restore_context);
1020
Mike Turquetteb24764902012-03-15 23:11:19 -07001021/**
1022 * clk_enable - ungate a clock
1023 * @clk: the clk being ungated
1024 *
1025 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1026 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1027 * if the operation will never sleep. One example is a SoC-internal clk which
1028 * is controlled via simple register writes. In the complex case a clk ungate
1029 * operation may require a fast and a slow part. It is this reason that
1030 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1031 * must be called before clk_enable. Returns 0 on success, -EERROR
1032 * otherwise.
1033 */
1034int clk_enable(struct clk *clk)
1035{
Dong Aisheng864e1602015-04-30 14:02:19 -07001036 if (!clk)
1037 return 0;
1038
Dong Aishenga6adc302016-06-30 17:31:11 +08001039 return clk_core_enable_lock(clk->core);
1040}
1041EXPORT_SYMBOL_GPL(clk_enable);
1042
1043static int clk_core_prepare_enable(struct clk_core *core)
1044{
1045 int ret;
1046
1047 ret = clk_core_prepare_lock(core);
1048 if (ret)
1049 return ret;
1050
1051 ret = clk_core_enable_lock(core);
1052 if (ret)
1053 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001054
1055 return ret;
1056}
Dong Aishenga6adc302016-06-30 17:31:11 +08001057
1058static void clk_core_disable_unprepare(struct clk_core *core)
1059{
1060 clk_core_disable_lock(core);
1061 clk_core_unprepare_lock(core);
1062}
Mike Turquetteb24764902012-03-15 23:11:19 -07001063
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001064static void clk_unprepare_unused_subtree(struct clk_core *core)
1065{
1066 struct clk_core *child;
1067
1068 lockdep_assert_held(&prepare_lock);
1069
1070 hlist_for_each_entry(child, &core->children, child_node)
1071 clk_unprepare_unused_subtree(child);
1072
1073 if (core->prepare_count)
1074 return;
1075
1076 if (core->flags & CLK_IGNORE_UNUSED)
1077 return;
1078
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001079 if (clk_pm_runtime_get(core))
1080 return;
1081
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001082 if (clk_core_is_prepared(core)) {
1083 trace_clk_unprepare(core);
1084 if (core->ops->unprepare_unused)
1085 core->ops->unprepare_unused(core->hw);
1086 else if (core->ops->unprepare)
1087 core->ops->unprepare(core->hw);
1088 trace_clk_unprepare_complete(core);
1089 }
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001090
1091 clk_pm_runtime_put(core);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001092}
1093
1094static void clk_disable_unused_subtree(struct clk_core *core)
1095{
1096 struct clk_core *child;
1097 unsigned long flags;
1098
1099 lockdep_assert_held(&prepare_lock);
1100
1101 hlist_for_each_entry(child, &core->children, child_node)
1102 clk_disable_unused_subtree(child);
1103
Dong Aishenga4b35182016-06-30 17:31:13 +08001104 if (core->flags & CLK_OPS_PARENT_ENABLE)
1105 clk_core_prepare_enable(core->parent);
1106
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001107 if (clk_pm_runtime_get(core))
1108 goto unprepare_out;
1109
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001110 flags = clk_enable_lock();
1111
1112 if (core->enable_count)
1113 goto unlock_out;
1114
1115 if (core->flags & CLK_IGNORE_UNUSED)
1116 goto unlock_out;
1117
1118 /*
1119 * some gate clocks have special needs during the disable-unused
1120 * sequence. call .disable_unused if available, otherwise fall
1121 * back to .disable
1122 */
1123 if (clk_core_is_enabled(core)) {
1124 trace_clk_disable(core);
1125 if (core->ops->disable_unused)
1126 core->ops->disable_unused(core->hw);
1127 else if (core->ops->disable)
1128 core->ops->disable(core->hw);
1129 trace_clk_disable_complete(core);
1130 }
1131
1132unlock_out:
1133 clk_enable_unlock(flags);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001134 clk_pm_runtime_put(core);
1135unprepare_out:
Dong Aishenga4b35182016-06-30 17:31:13 +08001136 if (core->flags & CLK_OPS_PARENT_ENABLE)
1137 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001138}
1139
1140static bool clk_ignore_unused;
1141static int __init clk_ignore_unused_setup(char *__unused)
1142{
1143 clk_ignore_unused = true;
1144 return 1;
1145}
1146__setup("clk_ignore_unused", clk_ignore_unused_setup);
1147
1148static int clk_disable_unused(void)
1149{
1150 struct clk_core *core;
1151
1152 if (clk_ignore_unused) {
1153 pr_warn("clk: Not disabling unused clocks\n");
1154 return 0;
1155 }
1156
1157 clk_prepare_lock();
1158
1159 hlist_for_each_entry(core, &clk_root_list, child_node)
1160 clk_disable_unused_subtree(core);
1161
1162 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1163 clk_disable_unused_subtree(core);
1164
1165 hlist_for_each_entry(core, &clk_root_list, child_node)
1166 clk_unprepare_unused_subtree(core);
1167
1168 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1169 clk_unprepare_unused_subtree(core);
1170
1171 clk_prepare_unlock();
1172
1173 return 0;
1174}
1175late_initcall_sync(clk_disable_unused);
1176
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001177static int clk_core_determine_round_nolock(struct clk_core *core,
1178 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001179{
Boris Brezillon0817b622015-07-07 20:48:08 +02001180 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001181
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001182 lockdep_assert_held(&prepare_lock);
1183
Stephen Boydd6968fc2015-04-30 13:54:13 -07001184 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001185 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001186
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001187 /*
1188 * At this point, core protection will be disabled if
1189 * - if the provider is not protected at all
1190 * - if the calling consumer is the only one which has exclusivity
1191 * over the provider
1192 */
Jerome Brunete55a8392017-12-01 22:51:56 +01001193 if (clk_core_rate_is_protected(core)) {
1194 req->rate = core->rate;
1195 } else if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001196 return core->ops->determine_rate(core->hw, req);
1197 } else if (core->ops->round_rate) {
1198 rate = core->ops->round_rate(core->hw, req->rate,
1199 &req->best_parent_rate);
1200 if (rate < 0)
1201 return rate;
1202
1203 req->rate = rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001204 } else {
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001205 return -EINVAL;
Boris Brezillon0817b622015-07-07 20:48:08 +02001206 }
1207
1208 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001209}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001210
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001211static void clk_core_init_rate_req(struct clk_core * const core,
1212 struct clk_rate_request *req)
1213{
1214 struct clk_core *parent;
1215
1216 if (WARN_ON(!core || !req))
1217 return;
1218
Mike Turquetteb24764902012-03-15 23:11:19 -07001219 parent = core->parent;
1220 if (parent) {
1221 req->best_parent_hw = parent->hw;
1222 req->best_parent_rate = parent->rate;
1223 } else {
1224 req->best_parent_hw = NULL;
1225 req->best_parent_rate = 0;
1226 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001227}
Mike Turquetteb24764902012-03-15 23:11:19 -07001228
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001229static bool clk_core_can_round(struct clk_core * const core)
1230{
1231 if (core->ops->determine_rate || core->ops->round_rate)
1232 return true;
Mike Turquetteb24764902012-03-15 23:11:19 -07001233
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001234 return false;
1235}
Mike Turquetteb24764902012-03-15 23:11:19 -07001236
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001237static int clk_core_round_rate_nolock(struct clk_core *core,
1238 struct clk_rate_request *req)
1239{
1240 lockdep_assert_held(&prepare_lock);
1241
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001242 if (!core) {
1243 req->rate = 0;
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001244 return 0;
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001245 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001246
1247 clk_core_init_rate_req(core, req);
1248
1249 if (clk_core_can_round(core))
1250 return clk_core_determine_round_nolock(core, req);
1251 else if (core->flags & CLK_SET_RATE_PARENT)
1252 return clk_core_round_rate_nolock(core->parent, req);
1253
1254 req->rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001255 return 0;
1256}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001257
1258/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001259 * __clk_determine_rate - get the closest rate actually supported by a clock
1260 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001261 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001262 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001263 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001264 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001265int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001266{
Boris Brezillon0817b622015-07-07 20:48:08 +02001267 if (!hw) {
1268 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001269 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001270 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001271
Boris Brezillon0817b622015-07-07 20:48:08 +02001272 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001273}
1274EXPORT_SYMBOL_GPL(__clk_determine_rate);
1275
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001276unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1277{
1278 int ret;
1279 struct clk_rate_request req;
1280
1281 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1282 req.rate = rate;
1283
1284 ret = clk_core_round_rate_nolock(hw->core, &req);
1285 if (ret)
1286 return 0;
1287
1288 return req.rate;
1289}
1290EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1291
Mike Turquetteb24764902012-03-15 23:11:19 -07001292/**
1293 * clk_round_rate - round the given rate for a clk
1294 * @clk: the clk for which we are rounding a rate
1295 * @rate: the rate which is to be rounded
1296 *
1297 * Takes in a rate as input and rounds it to a rate that the clk can actually
1298 * use which is then returned. If clk doesn't support round_rate operation
1299 * then the parent rate is returned.
1300 */
1301long clk_round_rate(struct clk *clk, unsigned long rate)
1302{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001303 struct clk_rate_request req;
1304 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001305
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001306 if (!clk)
1307 return 0;
1308
Mike Turquetteeab89f62013-03-28 13:59:01 -07001309 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001310
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001311 if (clk->exclusive_count)
1312 clk_core_rate_unprotect(clk->core);
1313
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001314 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1315 req.rate = rate;
1316
1317 ret = clk_core_round_rate_nolock(clk->core, &req);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001318
1319 if (clk->exclusive_count)
1320 clk_core_rate_protect(clk->core);
1321
Mike Turquetteeab89f62013-03-28 13:59:01 -07001322 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001323
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001324 if (ret)
1325 return ret;
1326
1327 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001328}
1329EXPORT_SYMBOL_GPL(clk_round_rate);
1330
1331/**
1332 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001333 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001334 * @msg: clk notifier type (see include/linux/clk.h)
1335 * @old_rate: old clk rate
1336 * @new_rate: new clk rate
1337 *
1338 * Triggers a notifier call chain on the clk rate-change notification
1339 * for 'clk'. Passes a pointer to the struct clk and the previous
1340 * and current rates to the notifier callback. Intended to be called by
1341 * internal clock code only. Returns NOTIFY_DONE from the last driver
1342 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1343 * a driver returns that.
1344 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001345static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001346 unsigned long old_rate, unsigned long new_rate)
1347{
1348 struct clk_notifier *cn;
1349 struct clk_notifier_data cnd;
1350 int ret = NOTIFY_DONE;
1351
Mike Turquetteb24764902012-03-15 23:11:19 -07001352 cnd.old_rate = old_rate;
1353 cnd.new_rate = new_rate;
1354
1355 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001356 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001357 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001358 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1359 &cnd);
Peter De Schrijver17c34c52017-03-21 12:16:26 +02001360 if (ret & NOTIFY_STOP_MASK)
1361 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001362 }
1363 }
1364
1365 return ret;
1366}
1367
1368/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001369 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001370 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001371 *
1372 * Walks the subtree of clks starting with clk and recalculates accuracies as
1373 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001374 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001375 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001376 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001377static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001378{
1379 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001380 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001381
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001382 lockdep_assert_held(&prepare_lock);
1383
Stephen Boydd6968fc2015-04-30 13:54:13 -07001384 if (core->parent)
1385 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001386
Stephen Boydd6968fc2015-04-30 13:54:13 -07001387 if (core->ops->recalc_accuracy)
1388 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001389 parent_accuracy);
1390 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001391 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001392
Stephen Boydd6968fc2015-04-30 13:54:13 -07001393 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001394 __clk_recalc_accuracies(child);
1395}
1396
Stephen Boydd6968fc2015-04-30 13:54:13 -07001397static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001398{
1399 unsigned long accuracy;
1400
1401 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001402 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1403 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001404
Stephen Boydd6968fc2015-04-30 13:54:13 -07001405 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001406 clk_prepare_unlock();
1407
1408 return accuracy;
1409}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001410
1411/**
1412 * clk_get_accuracy - return the accuracy of clk
1413 * @clk: the clk whose accuracy is being returned
1414 *
1415 * Simply returns the cached accuracy of the clk, unless
1416 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1417 * issued.
1418 * If clk is NULL then returns 0.
1419 */
1420long clk_get_accuracy(struct clk *clk)
1421{
1422 if (!clk)
1423 return 0;
1424
1425 return clk_core_get_accuracy(clk->core);
1426}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001427EXPORT_SYMBOL_GPL(clk_get_accuracy);
1428
Stephen Boydd6968fc2015-04-30 13:54:13 -07001429static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001430 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001431{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001432 unsigned long rate = parent_rate;
1433
1434 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1435 rate = core->ops->recalc_rate(core->hw, parent_rate);
1436 clk_pm_runtime_put(core);
1437 }
1438 return rate;
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001439}
1440
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001441/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001442 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001443 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001444 * @msg: notification type (see include/linux/clk.h)
1445 *
1446 * Walks the subtree of clks starting with clk and recalculates rates as it
1447 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001448 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001449 *
1450 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1451 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001452 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001453static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001454{
1455 unsigned long old_rate;
1456 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001457 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001458
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001459 lockdep_assert_held(&prepare_lock);
1460
Stephen Boydd6968fc2015-04-30 13:54:13 -07001461 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001462
Stephen Boydd6968fc2015-04-30 13:54:13 -07001463 if (core->parent)
1464 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001465
Stephen Boydd6968fc2015-04-30 13:54:13 -07001466 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001467
1468 /*
1469 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1470 * & ABORT_RATE_CHANGE notifiers
1471 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001472 if (core->notifier_count && msg)
1473 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001474
Stephen Boydd6968fc2015-04-30 13:54:13 -07001475 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001476 __clk_recalc_rates(child, msg);
1477}
1478
Stephen Boydd6968fc2015-04-30 13:54:13 -07001479static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001480{
1481 unsigned long rate;
1482
1483 clk_prepare_lock();
1484
Stephen Boydd6968fc2015-04-30 13:54:13 -07001485 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1486 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001487
Stephen Boydd6968fc2015-04-30 13:54:13 -07001488 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001489 clk_prepare_unlock();
1490
1491 return rate;
1492}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001493
Mike Turquetteb24764902012-03-15 23:11:19 -07001494/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001495 * clk_get_rate - return the rate of clk
1496 * @clk: the clk whose rate is being returned
1497 *
1498 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1499 * is set, which means a recalc_rate will be issued.
1500 * If clk is NULL then returns 0.
1501 */
1502unsigned long clk_get_rate(struct clk *clk)
1503{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001504 if (!clk)
1505 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001506
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001507 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001508}
1509EXPORT_SYMBOL_GPL(clk_get_rate);
1510
Stephen Boydd6968fc2015-04-30 13:54:13 -07001511static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001512 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001513{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001514 int i;
James Hogan4935b222013-07-29 12:24:59 +01001515
Masahiro Yamada508f8842015-12-28 19:23:08 +09001516 if (!parent)
1517 return -EINVAL;
1518
Masahiro Yamada470b5e22015-12-28 19:23:09 +09001519 for (i = 0; i < core->num_parents; i++)
1520 if (clk_core_get_parent_by_index(core, i) == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001521 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001522
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001523 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001524}
1525
Heiko Stuebnere6500342015-04-22 22:53:05 +02001526/*
1527 * Update the orphan status of @core and all its children.
1528 */
1529static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1530{
1531 struct clk_core *child;
1532
1533 core->orphan = is_orphan;
1534
1535 hlist_for_each_entry(child, &core->children, child_node)
1536 clk_core_update_orphan_status(child, is_orphan);
1537}
1538
Stephen Boydd6968fc2015-04-30 13:54:13 -07001539static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001540{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001541 bool was_orphan = core->orphan;
1542
Stephen Boydd6968fc2015-04-30 13:54:13 -07001543 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001544
James Hogan903efc52013-08-29 12:10:51 +01001545 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001546 bool becomes_orphan = new_parent->orphan;
1547
James Hogan903efc52013-08-29 12:10:51 +01001548 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001549 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001550 new_parent->new_child = NULL;
1551
Stephen Boydd6968fc2015-04-30 13:54:13 -07001552 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001553
1554 if (was_orphan != becomes_orphan)
1555 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001556 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001557 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001558 if (!was_orphan)
1559 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001560 }
James Hogan4935b222013-07-29 12:24:59 +01001561
Stephen Boydd6968fc2015-04-30 13:54:13 -07001562 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001563}
1564
Stephen Boydd6968fc2015-04-30 13:54:13 -07001565static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001566 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001567{
1568 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001569 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001570
1571 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001572 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1573 *
1574 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001575 * clk_enable().
1576 *
1577 * If the clock is not prepared, then a race with
1578 * clk_enable/disable() is impossible since we already have the
1579 * prepare lock (future calls to clk_enable() need to be preceded by
1580 * a clk_prepare()).
1581 *
1582 * If the clock is prepared, migrate the prepared state to the new
1583 * parent and also protect against a race with clk_enable() by
1584 * forcing the clock and the new parent on. This ensures that all
1585 * future calls to clk_enable() are practically NOPs with respect to
1586 * hardware and software states.
1587 *
1588 * See also: Comment for clk_set_parent() below.
1589 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001590
1591 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1592 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1593 clk_core_prepare_enable(old_parent);
1594 clk_core_prepare_enable(parent);
1595 }
1596
1597 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001598 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001599 clk_core_prepare_enable(parent);
1600 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001601 }
1602
1603 /* update the clk tree topology */
1604 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001605 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001606 clk_enable_unlock(flags);
1607
Stephen Boyd3fa22522014-01-15 10:47:22 -08001608 return old_parent;
1609}
1610
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001611static void __clk_set_parent_after(struct clk_core *core,
1612 struct clk_core *parent,
1613 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001614{
1615 /*
1616 * Finish the migration of prepare state and undo the changes done
1617 * for preventing a race with clk_enable().
1618 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001619 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001620 clk_core_disable_lock(core);
1621 clk_core_disable_unprepare(old_parent);
1622 }
1623
1624 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1625 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1626 clk_core_disable_unprepare(parent);
1627 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001628 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001629}
1630
Stephen Boydd6968fc2015-04-30 13:54:13 -07001631static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001632 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001633{
1634 unsigned long flags;
1635 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001636 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001637
Stephen Boydd6968fc2015-04-30 13:54:13 -07001638 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001639
Stephen Boydd6968fc2015-04-30 13:54:13 -07001640 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001641
James Hogan4935b222013-07-29 12:24:59 +01001642 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001643 if (parent && core->ops->set_parent)
1644 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001645
Stephen Boydd6968fc2015-04-30 13:54:13 -07001646 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001647
James Hogan4935b222013-07-29 12:24:59 +01001648 if (ret) {
1649 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001650 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001651 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001652 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001653
James Hogan4935b222013-07-29 12:24:59 +01001654 return ret;
1655 }
1656
Stephen Boydd6968fc2015-04-30 13:54:13 -07001657 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001658
James Hogan4935b222013-07-29 12:24:59 +01001659 return 0;
1660}
1661
Ulf Hanssona093bde2012-08-31 14:21:28 +02001662/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001663 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001664 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001665 * @parent_rate: the "future" rate of clk's parent
1666 *
1667 * Walks the subtree of clks starting with clk, speculating rates as it
1668 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1669 *
1670 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1671 * pre-rate change notifications and returns early if no clks in the
1672 * subtree have subscribed to the notifications. Note that if a clk does not
1673 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001674 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001675 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001676static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001677 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001678{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001679 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001680 unsigned long new_rate;
1681 int ret = NOTIFY_DONE;
1682
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001683 lockdep_assert_held(&prepare_lock);
1684
Stephen Boydd6968fc2015-04-30 13:54:13 -07001685 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001686
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001687 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001688 if (core->notifier_count)
1689 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001690
Mike Turquette86bcfa22014-02-24 16:08:41 -08001691 if (ret & NOTIFY_STOP_MASK) {
1692 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001693 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001694 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001695 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001696
Stephen Boydd6968fc2015-04-30 13:54:13 -07001697 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001698 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001699 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001700 break;
1701 }
1702
1703out:
1704 return ret;
1705}
1706
Stephen Boydd6968fc2015-04-30 13:54:13 -07001707static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001708 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001709{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001710 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001711
Stephen Boydd6968fc2015-04-30 13:54:13 -07001712 core->new_rate = new_rate;
1713 core->new_parent = new_parent;
1714 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001715 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001716 core->new_child = NULL;
1717 if (new_parent && new_parent != core->parent)
1718 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001719
Stephen Boydd6968fc2015-04-30 13:54:13 -07001720 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001721 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001722 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001723 }
1724}
1725
1726/*
1727 * calculate the new rates returning the topmost clock that has to be
1728 * changed.
1729 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001730static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001731 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001732{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001733 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001734 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001735 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001736 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001737 unsigned long min_rate;
1738 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001739 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001740 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001741
Mike Turquette7452b212012-03-26 14:45:36 -07001742 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001743 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001744 return NULL;
1745
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001746 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001747 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001748 if (parent)
1749 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001750
Stephen Boydd6968fc2015-04-30 13:54:13 -07001751 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001752
James Hogan71472c02013-07-29 12:25:00 +01001753 /* find the closest rate and parent clk/rate */
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001754 if (clk_core_can_round(core)) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001755 struct clk_rate_request req;
1756
1757 req.rate = rate;
1758 req.min_rate = min_rate;
1759 req.max_rate = max_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001760
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001761 clk_core_init_rate_req(core, &req);
1762
1763 ret = clk_core_determine_round_nolock(core, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001764 if (ret < 0)
1765 return NULL;
1766
Boris Brezillon0817b622015-07-07 20:48:08 +02001767 best_parent_rate = req.best_parent_rate;
1768 new_rate = req.rate;
1769 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001770
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001771 if (new_rate < min_rate || new_rate > max_rate)
1772 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001773 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001774 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001775 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001776 return NULL;
1777 } else {
1778 /* pass-through clock with adjustable parent */
1779 top = clk_calc_new_rates(parent, rate);
1780 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001781 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001782 }
1783
James Hogan71472c02013-07-29 12:25:00 +01001784 /* some clocks must be gated to change parent */
1785 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001786 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001787 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001788 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001789 return NULL;
1790 }
1791
James Hogan71472c02013-07-29 12:25:00 +01001792 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001793 if (parent && core->num_parents > 1) {
1794 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001795 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001796 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001797 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001798 return NULL;
1799 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001800 }
1801
Stephen Boydd6968fc2015-04-30 13:54:13 -07001802 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001803 best_parent_rate != parent->rate)
1804 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001805
1806out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001807 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001808
1809 return top;
1810}
1811
1812/*
1813 * Notify about rate changes in a subtree. Always walk down the whole tree
1814 * so that in case of an error we can walk down the whole tree again and
1815 * abort the change.
1816 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001817static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001818 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001819{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001820 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001821 int ret = NOTIFY_DONE;
1822
Stephen Boydd6968fc2015-04-30 13:54:13 -07001823 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301824 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001825
Stephen Boydd6968fc2015-04-30 13:54:13 -07001826 if (core->notifier_count) {
1827 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001828 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001829 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001830 }
1831
Stephen Boydd6968fc2015-04-30 13:54:13 -07001832 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001833 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001834 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001835 continue;
1836 tmp_clk = clk_propagate_rate_change(child, event);
1837 if (tmp_clk)
1838 fail_clk = tmp_clk;
1839 }
1840
Stephen Boydd6968fc2015-04-30 13:54:13 -07001841 /* handle the new child who might not be in core->children yet */
1842 if (core->new_child) {
1843 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001844 if (tmp_clk)
1845 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001846 }
1847
1848 return fail_clk;
1849}
1850
1851/*
1852 * walk down a subtree and set the new rates notifying the rate
1853 * change on the way
1854 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001855static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001856{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001857 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001858 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001859 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001860 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001861 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001862 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001863 struct clk_core *parent = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001864
Stephen Boydd6968fc2015-04-30 13:54:13 -07001865 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001866
Dong Aishengfc8726a2016-06-30 17:31:14 +08001867 if (core->new_parent) {
1868 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001869 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001870 } else if (core->parent) {
1871 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001872 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001873 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001874
Marek Szyprowski588fb542017-11-30 13:14:51 +01001875 if (clk_pm_runtime_get(core))
1876 return;
1877
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001878 if (core->flags & CLK_SET_RATE_UNGATE) {
1879 unsigned long flags;
1880
1881 clk_core_prepare(core);
1882 flags = clk_enable_lock();
1883 clk_core_enable(core);
1884 clk_enable_unlock(flags);
1885 }
1886
Stephen Boydd6968fc2015-04-30 13:54:13 -07001887 if (core->new_parent && core->new_parent != core->parent) {
1888 old_parent = __clk_set_parent_before(core, core->new_parent);
1889 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001890
Stephen Boydd6968fc2015-04-30 13:54:13 -07001891 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001892 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001893 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001894 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001895 core->new_parent_index);
1896 } else if (core->ops->set_parent) {
1897 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001898 }
1899
Stephen Boydd6968fc2015-04-30 13:54:13 -07001900 trace_clk_set_parent_complete(core, core->new_parent);
1901 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001902 }
1903
Dong Aishengfc8726a2016-06-30 17:31:14 +08001904 if (core->flags & CLK_OPS_PARENT_ENABLE)
1905 clk_core_prepare_enable(parent);
1906
Stephen Boydd6968fc2015-04-30 13:54:13 -07001907 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001908
Stephen Boydd6968fc2015-04-30 13:54:13 -07001909 if (!skip_set_rate && core->ops->set_rate)
1910 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001911
Stephen Boydd6968fc2015-04-30 13:54:13 -07001912 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001913
Stephen Boydd6968fc2015-04-30 13:54:13 -07001914 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001915
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001916 if (core->flags & CLK_SET_RATE_UNGATE) {
1917 unsigned long flags;
1918
1919 flags = clk_enable_lock();
1920 clk_core_disable(core);
1921 clk_enable_unlock(flags);
1922 clk_core_unprepare(core);
1923 }
1924
Dong Aishengfc8726a2016-06-30 17:31:14 +08001925 if (core->flags & CLK_OPS_PARENT_ENABLE)
1926 clk_core_disable_unprepare(parent);
1927
Stephen Boydd6968fc2015-04-30 13:54:13 -07001928 if (core->notifier_count && old_rate != core->rate)
1929 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001930
Michael Turquette85e88fa2015-06-20 12:18:03 -07001931 if (core->flags & CLK_RECALC_NEW_RATES)
1932 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001933
Tero Kristo067bb172014-08-21 16:47:45 +03001934 /*
1935 * Use safe iteration, as change_rate can actually swap parents
1936 * for certain clock types.
1937 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001938 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001939 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001940 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001941 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001942 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001943 }
1944
Stephen Boydd6968fc2015-04-30 13:54:13 -07001945 /* handle the new child who might not be in core->children yet */
1946 if (core->new_child)
1947 clk_change_rate(core->new_child);
Marek Szyprowski588fb542017-11-30 13:14:51 +01001948
1949 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001950}
1951
Jerome Brunetca5e0892017-12-01 22:51:55 +01001952static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1953 unsigned long req_rate)
1954{
Jerome Brunete55a8392017-12-01 22:51:56 +01001955 int ret, cnt;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001956 struct clk_rate_request req;
1957
1958 lockdep_assert_held(&prepare_lock);
1959
1960 if (!core)
1961 return 0;
1962
Jerome Brunete55a8392017-12-01 22:51:56 +01001963 /* simulate what the rate would be if it could be freely set */
1964 cnt = clk_core_rate_nuke_protect(core);
1965 if (cnt < 0)
1966 return cnt;
1967
Jerome Brunetca5e0892017-12-01 22:51:55 +01001968 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1969 req.rate = req_rate;
1970
1971 ret = clk_core_round_rate_nolock(core, &req);
1972
Jerome Brunete55a8392017-12-01 22:51:56 +01001973 /* restore the protection */
1974 clk_core_rate_restore_protect(core, cnt);
1975
Jerome Brunetca5e0892017-12-01 22:51:55 +01001976 return ret ? 0 : req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001977}
1978
Stephen Boydd6968fc2015-04-30 13:54:13 -07001979static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001980 unsigned long req_rate)
1981{
1982 struct clk_core *top, *fail_clk;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001983 unsigned long rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001984 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001985
Stephen Boydd6968fc2015-04-30 13:54:13 -07001986 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001987 return 0;
1988
Jerome Brunetca5e0892017-12-01 22:51:55 +01001989 rate = clk_core_req_round_rate_nolock(core, req_rate);
1990
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001991 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001992 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001993 return 0;
1994
Jerome Brunete55a8392017-12-01 22:51:56 +01001995 /* fail on a direct rate set of a protected provider */
1996 if (clk_core_rate_is_protected(core))
1997 return -EBUSY;
1998
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001999 /* calculate new rates and get the topmost changed clock */
Jerome Brunetca5e0892017-12-01 22:51:55 +01002000 top = clk_calc_new_rates(core, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002001 if (!top)
2002 return -EINVAL;
2003
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002004 ret = clk_pm_runtime_get(core);
2005 if (ret)
2006 return ret;
2007
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002008 /* notify that we are about to change rates */
2009 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
2010 if (fail_clk) {
2011 pr_debug("%s: failed to set %s rate\n", __func__,
2012 fail_clk->name);
2013 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002014 ret = -EBUSY;
2015 goto err;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002016 }
2017
2018 /* change the rates */
2019 clk_change_rate(top);
2020
Stephen Boydd6968fc2015-04-30 13:54:13 -07002021 core->req_rate = req_rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002022err:
2023 clk_pm_runtime_put(core);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002024
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002025 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002026}
2027
Mike Turquetteb24764902012-03-15 23:11:19 -07002028/**
2029 * clk_set_rate - specify a new rate for clk
2030 * @clk: the clk whose rate is being changed
2031 * @rate: the new rate for clk
2032 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002033 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07002034 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002035 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2036 * propagate up to clk's parent; whether or not this happens depends on the
2037 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2038 * after calling .round_rate then upstream parent propagation is ignored. If
2039 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002040 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07002041 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2042 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07002043 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002044 * Rate changes are accomplished via tree traversal that also recalculates the
2045 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07002046 *
2047 * Returns 0 on success, -EERROR otherwise.
2048 */
2049int clk_set_rate(struct clk *clk, unsigned long rate)
2050{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002051 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002052
Mike Turquette89ac8d72013-08-21 23:58:09 -07002053 if (!clk)
2054 return 0;
2055
Mike Turquetteb24764902012-03-15 23:11:19 -07002056 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002057 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002058
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002059 if (clk->exclusive_count)
2060 clk_core_rate_unprotect(clk->core);
2061
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002062 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002063
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002064 if (clk->exclusive_count)
2065 clk_core_rate_protect(clk->core);
2066
Mike Turquetteeab89f62013-03-28 13:59:01 -07002067 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002068
2069 return ret;
2070}
2071EXPORT_SYMBOL_GPL(clk_set_rate);
2072
2073/**
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002074 * clk_set_rate_exclusive - specify a new rate get exclusive control
2075 * @clk: the clk whose rate is being changed
2076 * @rate: the new rate for clk
2077 *
2078 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2079 * within a critical section
2080 *
2081 * This can be used initially to ensure that at least 1 consumer is
2082 * statisfied when several consumers are competing for exclusivity over the
2083 * same clock provider.
2084 *
2085 * The exclusivity is not applied if setting the rate failed.
2086 *
2087 * Calls to clk_rate_exclusive_get() should be balanced with calls to
2088 * clk_rate_exclusive_put().
2089 *
2090 * Returns 0 on success, -EERROR otherwise.
2091 */
2092int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2093{
2094 int ret;
2095
2096 if (!clk)
2097 return 0;
2098
2099 /* prevent racing with updates to the clock topology */
2100 clk_prepare_lock();
2101
2102 /*
2103 * The temporary protection removal is not here, on purpose
2104 * This function is meant to be used instead of clk_rate_protect,
2105 * so before the consumer code path protect the clock provider
2106 */
2107
2108 ret = clk_core_set_rate_nolock(clk->core, rate);
2109 if (!ret) {
2110 clk_core_rate_protect(clk->core);
2111 clk->exclusive_count++;
2112 }
2113
2114 clk_prepare_unlock();
2115
2116 return ret;
2117}
2118EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2119
2120/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002121 * clk_set_rate_range - set a rate range for a clock source
2122 * @clk: clock source
2123 * @min: desired minimum clock rate in Hz, inclusive
2124 * @max: desired maximum clock rate in Hz, inclusive
2125 *
2126 * Returns success (0) or negative errno.
2127 */
2128int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2129{
2130 int ret = 0;
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002131 unsigned long old_min, old_max, rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002132
2133 if (!clk)
2134 return 0;
2135
2136 if (min > max) {
2137 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2138 __func__, clk->core->name, clk->dev_id, clk->con_id,
2139 min, max);
2140 return -EINVAL;
2141 }
2142
2143 clk_prepare_lock();
2144
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002145 if (clk->exclusive_count)
2146 clk_core_rate_unprotect(clk->core);
2147
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002148 /* Save the current values in case we need to rollback the change */
2149 old_min = clk->min_rate;
2150 old_max = clk->max_rate;
2151 clk->min_rate = min;
2152 clk->max_rate = max;
2153
2154 rate = clk_core_get_rate_nolock(clk->core);
2155 if (rate < min || rate > max) {
2156 /*
2157 * FIXME:
2158 * We are in bit of trouble here, current rate is outside the
2159 * the requested range. We are going try to request appropriate
2160 * range boundary but there is a catch. It may fail for the
2161 * usual reason (clock broken, clock protected, etc) but also
2162 * because:
2163 * - round_rate() was not favorable and fell on the wrong
2164 * side of the boundary
2165 * - the determine_rate() callback does not really check for
2166 * this corner case when determining the rate
2167 */
2168
2169 if (rate < min)
2170 rate = min;
2171 else
2172 rate = max;
2173
2174 ret = clk_core_set_rate_nolock(clk->core, rate);
2175 if (ret) {
2176 /* rollback the changes */
2177 clk->min_rate = old_min;
2178 clk->max_rate = old_max;
2179 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002180 }
2181
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002182 if (clk->exclusive_count)
2183 clk_core_rate_protect(clk->core);
2184
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002185 clk_prepare_unlock();
2186
2187 return ret;
2188}
2189EXPORT_SYMBOL_GPL(clk_set_rate_range);
2190
2191/**
2192 * clk_set_min_rate - set a minimum clock rate for a clock source
2193 * @clk: clock source
2194 * @rate: desired minimum clock rate in Hz, inclusive
2195 *
2196 * Returns success (0) or negative errno.
2197 */
2198int clk_set_min_rate(struct clk *clk, unsigned long rate)
2199{
2200 if (!clk)
2201 return 0;
2202
2203 return clk_set_rate_range(clk, rate, clk->max_rate);
2204}
2205EXPORT_SYMBOL_GPL(clk_set_min_rate);
2206
2207/**
2208 * clk_set_max_rate - set a maximum clock rate for a clock source
2209 * @clk: clock source
2210 * @rate: desired maximum clock rate in Hz, inclusive
2211 *
2212 * Returns success (0) or negative errno.
2213 */
2214int clk_set_max_rate(struct clk *clk, unsigned long rate)
2215{
2216 if (!clk)
2217 return 0;
2218
2219 return clk_set_rate_range(clk, clk->min_rate, rate);
2220}
2221EXPORT_SYMBOL_GPL(clk_set_max_rate);
2222
2223/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002224 * clk_get_parent - return the parent of a clk
2225 * @clk: the clk whose parent gets returned
2226 *
2227 * Simply returns clk->parent. Returns NULL if clk is NULL.
2228 */
2229struct clk *clk_get_parent(struct clk *clk)
2230{
2231 struct clk *parent;
2232
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002233 if (!clk)
2234 return NULL;
2235
Mike Turquetteeab89f62013-03-28 13:59:01 -07002236 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002237 /* TODO: Create a per-user clk and change callers to call clk_put */
2238 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002239 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002240
2241 return parent;
2242}
2243EXPORT_SYMBOL_GPL(clk_get_parent);
2244
Stephen Boydd6968fc2015-04-30 13:54:13 -07002245static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002246{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002247 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002248
Masahiro Yamada2430a942016-02-09 20:19:14 +09002249 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002250 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002251
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002252 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002253}
2254
Stephen Boydd6968fc2015-04-30 13:54:13 -07002255static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002256 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002257{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002258 clk_reparent(core, new_parent);
2259 __clk_recalc_accuracies(core);
2260 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002261}
2262
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002263void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2264{
2265 if (!hw)
2266 return;
2267
2268 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2269}
2270
Mike Turquetteb24764902012-03-15 23:11:19 -07002271/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002272 * clk_has_parent - check if a clock is a possible parent for another
2273 * @clk: clock source
2274 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002275 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002276 * This function can be used in drivers that need to check that a clock can be
2277 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002278 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002279 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002280 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002281bool clk_has_parent(struct clk *clk, struct clk *parent)
2282{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002283 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002284
2285 /* NULL clocks should be nops, so return success if either is NULL. */
2286 if (!clk || !parent)
2287 return true;
2288
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002289 core = clk->core;
2290 parent_core = parent->core;
2291
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002292 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002293 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002294 return true;
2295
Yisheng Xied6347442018-05-31 19:11:14 +08002296 return match_string(core->parent_names, core->num_parents,
2297 parent_core->name) >= 0;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002298}
2299EXPORT_SYMBOL_GPL(clk_has_parent);
2300
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002301static int clk_core_set_parent_nolock(struct clk_core *core,
2302 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002303{
2304 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002305 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002306 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002307
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002308 lockdep_assert_held(&prepare_lock);
2309
Stephen Boydd6968fc2015-04-30 13:54:13 -07002310 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002311 return 0;
2312
Stephen Boydd6968fc2015-04-30 13:54:13 -07002313 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002314 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002315
Stephen Boydb61c43c2015-02-02 14:11:25 -08002316 /* verify ops for for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002317 if (core->num_parents > 1 && !core->ops->set_parent)
2318 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08002319
Ulf Hansson031dcc92013-04-02 23:09:38 +02002320 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002321 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2322 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002323
Jerome Brunete55a8392017-12-01 22:51:56 +01002324 if (clk_core_rate_is_protected(core))
2325 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002326
2327 /* try finding the new parent index */
2328 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002329 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002330 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002331 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002332 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002333 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002334 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002335 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002336 }
2337
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002338 ret = clk_pm_runtime_get(core);
2339 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002340 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002341
Mike Turquetteb24764902012-03-15 23:11:19 -07002342 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002343 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002344
2345 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002346 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002347 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07002348
Ulf Hansson031dcc92013-04-02 23:09:38 +02002349 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002350 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002351
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002352 /* propagate rate an accuracy recalculation accordingly */
2353 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002354 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002355 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002356 __clk_recalc_rates(core, POST_RATE_CHANGE);
2357 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002358 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002359
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002360runtime_put:
2361 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002362
2363 return ret;
2364}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002365
2366/**
2367 * clk_set_parent - switch the parent of a mux clk
2368 * @clk: the mux clk whose input we are switching
2369 * @parent: the new input to clk
2370 *
2371 * Re-parent clk to use parent as its new input source. If clk is in
2372 * prepared state, the clk will get enabled for the duration of this call. If
2373 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2374 * that, the reparenting is glitchy in hardware, etc), use the
2375 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2376 *
2377 * After successfully changing clk's parent clk_set_parent will update the
2378 * clk topology, sysfs topology and propagate rate recalculation via
2379 * __clk_recalc_rates.
2380 *
2381 * Returns 0 on success, -EERROR otherwise.
2382 */
2383int clk_set_parent(struct clk *clk, struct clk *parent)
2384{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002385 int ret;
2386
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002387 if (!clk)
2388 return 0;
2389
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002390 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002391
2392 if (clk->exclusive_count)
2393 clk_core_rate_unprotect(clk->core);
2394
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002395 ret = clk_core_set_parent_nolock(clk->core,
2396 parent ? parent->core : NULL);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002397
2398 if (clk->exclusive_count)
2399 clk_core_rate_protect(clk->core);
2400
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002401 clk_prepare_unlock();
2402
2403 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002404}
Mike Turquetteb24764902012-03-15 23:11:19 -07002405EXPORT_SYMBOL_GPL(clk_set_parent);
2406
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002407static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2408{
2409 int ret = -EINVAL;
2410
2411 lockdep_assert_held(&prepare_lock);
2412
2413 if (!core)
2414 return 0;
2415
Jerome Brunete55a8392017-12-01 22:51:56 +01002416 if (clk_core_rate_is_protected(core))
2417 return -EBUSY;
2418
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002419 trace_clk_set_phase(core, degrees);
2420
Shawn Lin7f95bee2018-03-08 14:49:41 +08002421 if (core->ops->set_phase) {
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002422 ret = core->ops->set_phase(core->hw, degrees);
Shawn Lin7f95bee2018-03-08 14:49:41 +08002423 if (!ret)
2424 core->phase = degrees;
2425 }
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002426
2427 trace_clk_set_phase_complete(core, degrees);
2428
2429 return ret;
2430}
2431
Mike Turquetteb24764902012-03-15 23:11:19 -07002432/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002433 * clk_set_phase - adjust the phase shift of a clock signal
2434 * @clk: clock signal source
2435 * @degrees: number of degrees the signal is shifted
2436 *
2437 * Shifts the phase of a clock signal by the specified
2438 * degrees. Returns 0 on success, -EERROR otherwise.
2439 *
2440 * This function makes no distinction about the input or reference
2441 * signal that we adjust the clock signal phase against. For example
2442 * phase locked-loop clock signal generators we may shift phase with
2443 * respect to feedback clock signal input, but for other cases the
2444 * clock phase may be shifted with respect to some other, unspecified
2445 * signal.
2446 *
2447 * Additionally the concept of phase shift does not propagate through
2448 * the clock tree hierarchy, which sets it apart from clock rates and
2449 * clock accuracy. A parent clock phase attribute does not have an
2450 * impact on the phase attribute of a child clock.
2451 */
2452int clk_set_phase(struct clk *clk, int degrees)
2453{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002454 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002455
2456 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002457 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002458
2459 /* sanity check degrees */
2460 degrees %= 360;
2461 if (degrees < 0)
2462 degrees += 360;
2463
2464 clk_prepare_lock();
2465
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002466 if (clk->exclusive_count)
2467 clk_core_rate_unprotect(clk->core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002468
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002469 ret = clk_core_set_phase_nolock(clk->core, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002470
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002471 if (clk->exclusive_count)
2472 clk_core_rate_protect(clk->core);
Mike Turquettee59c5372014-02-18 21:21:25 -08002473
Mike Turquettee59c5372014-02-18 21:21:25 -08002474 clk_prepare_unlock();
2475
Mike Turquettee59c5372014-02-18 21:21:25 -08002476 return ret;
2477}
Maxime Ripard9767b04f2015-01-20 22:23:43 +01002478EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002479
Stephen Boydd6968fc2015-04-30 13:54:13 -07002480static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002481{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002482 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002483
2484 clk_prepare_lock();
Shawn Lin1f9c63e2018-03-14 08:28:31 +08002485 /* Always try to update cached phase if possible */
2486 if (core->ops->get_phase)
2487 core->phase = core->ops->get_phase(core->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002488 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002489 clk_prepare_unlock();
2490
Mike Turquettee59c5372014-02-18 21:21:25 -08002491 return ret;
2492}
2493
2494/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002495 * clk_get_phase - return the phase shift of a clock signal
2496 * @clk: clock signal source
2497 *
2498 * Returns the phase shift of a clock node in degrees, otherwise returns
2499 * -EERROR.
2500 */
2501int clk_get_phase(struct clk *clk)
2502{
2503 if (!clk)
2504 return 0;
2505
2506 return clk_core_get_phase(clk->core);
2507}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002508EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002509
Jerome Brunet9fba7382018-06-19 16:41:41 +02002510static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2511{
2512 /* Assume a default value of 50% */
2513 core->duty.num = 1;
2514 core->duty.den = 2;
2515}
2516
2517static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2518
2519static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2520{
2521 struct clk_duty *duty = &core->duty;
2522 int ret = 0;
2523
2524 if (!core->ops->get_duty_cycle)
2525 return clk_core_update_duty_cycle_parent_nolock(core);
2526
2527 ret = core->ops->get_duty_cycle(core->hw, duty);
2528 if (ret)
2529 goto reset;
2530
2531 /* Don't trust the clock provider too much */
2532 if (duty->den == 0 || duty->num > duty->den) {
2533 ret = -EINVAL;
2534 goto reset;
2535 }
2536
2537 return 0;
2538
2539reset:
2540 clk_core_reset_duty_cycle_nolock(core);
2541 return ret;
2542}
2543
2544static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2545{
2546 int ret = 0;
2547
2548 if (core->parent &&
2549 core->flags & CLK_DUTY_CYCLE_PARENT) {
2550 ret = clk_core_update_duty_cycle_nolock(core->parent);
2551 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2552 } else {
2553 clk_core_reset_duty_cycle_nolock(core);
2554 }
2555
2556 return ret;
2557}
2558
2559static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2560 struct clk_duty *duty);
2561
2562static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2563 struct clk_duty *duty)
2564{
2565 int ret;
2566
2567 lockdep_assert_held(&prepare_lock);
2568
2569 if (clk_core_rate_is_protected(core))
2570 return -EBUSY;
2571
2572 trace_clk_set_duty_cycle(core, duty);
2573
2574 if (!core->ops->set_duty_cycle)
2575 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2576
2577 ret = core->ops->set_duty_cycle(core->hw, duty);
2578 if (!ret)
2579 memcpy(&core->duty, duty, sizeof(*duty));
2580
2581 trace_clk_set_duty_cycle_complete(core, duty);
2582
2583 return ret;
2584}
2585
2586static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2587 struct clk_duty *duty)
2588{
2589 int ret = 0;
2590
2591 if (core->parent &&
2592 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2593 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2594 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2595 }
2596
2597 return ret;
2598}
2599
2600/**
2601 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2602 * @clk: clock signal source
2603 * @num: numerator of the duty cycle ratio to be applied
2604 * @den: denominator of the duty cycle ratio to be applied
2605 *
2606 * Apply the duty cycle ratio if the ratio is valid and the clock can
2607 * perform this operation
2608 *
2609 * Returns (0) on success, a negative errno otherwise.
2610 */
2611int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2612{
2613 int ret;
2614 struct clk_duty duty;
2615
2616 if (!clk)
2617 return 0;
2618
2619 /* sanity check the ratio */
2620 if (den == 0 || num > den)
2621 return -EINVAL;
2622
2623 duty.num = num;
2624 duty.den = den;
2625
2626 clk_prepare_lock();
2627
2628 if (clk->exclusive_count)
2629 clk_core_rate_unprotect(clk->core);
2630
2631 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2632
2633 if (clk->exclusive_count)
2634 clk_core_rate_protect(clk->core);
2635
2636 clk_prepare_unlock();
2637
2638 return ret;
2639}
2640EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2641
2642static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2643 unsigned int scale)
2644{
2645 struct clk_duty *duty = &core->duty;
2646 int ret;
2647
2648 clk_prepare_lock();
2649
2650 ret = clk_core_update_duty_cycle_nolock(core);
2651 if (!ret)
2652 ret = mult_frac(scale, duty->num, duty->den);
2653
2654 clk_prepare_unlock();
2655
2656 return ret;
2657}
2658
2659/**
2660 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2661 * @clk: clock signal source
2662 * @scale: scaling factor to be applied to represent the ratio as an integer
2663 *
2664 * Returns the duty cycle ratio of a clock node multiplied by the provided
2665 * scaling factor, or negative errno on error.
2666 */
2667int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2668{
2669 if (!clk)
2670 return 0;
2671
2672 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2673}
2674EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2675
Mike Turquetteb24764902012-03-15 23:11:19 -07002676/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002677 * clk_is_match - check if two clk's point to the same hardware clock
2678 * @p: clk compared against q
2679 * @q: clk compared against p
2680 *
2681 * Returns true if the two struct clk pointers both point to the same hardware
2682 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2683 * share the same struct clk_core object.
2684 *
2685 * Returns false otherwise. Note that two NULL clks are treated as matching.
2686 */
2687bool clk_is_match(const struct clk *p, const struct clk *q)
2688{
2689 /* trivial case: identical struct clk's or both NULL */
2690 if (p == q)
2691 return true;
2692
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002693 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002694 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2695 if (p->core == q->core)
2696 return true;
2697
2698 return false;
2699}
2700EXPORT_SYMBOL_GPL(clk_is_match);
2701
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002702/*** debugfs support ***/
2703
2704#ifdef CONFIG_DEBUG_FS
2705#include <linux/debugfs.h>
2706
2707static struct dentry *rootdir;
2708static int inited = 0;
2709static DEFINE_MUTEX(clk_debug_lock);
2710static HLIST_HEAD(clk_debug_list);
2711
2712static struct hlist_head *all_lists[] = {
2713 &clk_root_list,
2714 &clk_orphan_list,
2715 NULL,
2716};
2717
2718static struct hlist_head *orphan_list[] = {
2719 &clk_orphan_list,
2720 NULL,
2721};
2722
2723static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2724 int level)
2725{
2726 if (!c)
2727 return;
2728
Jerome Brunet9fba7382018-06-19 16:41:41 +02002729 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002730 level * 3 + 1, "",
2731 30 - level * 3, c->name,
Jerome Brunete55a8392017-12-01 22:51:56 +01002732 c->enable_count, c->prepare_count, c->protect_count,
2733 clk_core_get_rate(c), clk_core_get_accuracy(c),
Jerome Brunet9fba7382018-06-19 16:41:41 +02002734 clk_core_get_phase(c),
2735 clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002736}
2737
2738static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2739 int level)
2740{
2741 struct clk_core *child;
2742
2743 if (!c)
2744 return;
2745
2746 clk_summary_show_one(s, c, level);
2747
2748 hlist_for_each_entry(child, &c->children, child_node)
2749 clk_summary_show_subtree(s, child, level + 1);
2750}
2751
2752static int clk_summary_show(struct seq_file *s, void *data)
2753{
2754 struct clk_core *c;
2755 struct hlist_head **lists = (struct hlist_head **)s->private;
2756
Jerome Brunet9fba7382018-06-19 16:41:41 +02002757 seq_puts(s, " enable prepare protect duty\n");
2758 seq_puts(s, " clock count count count rate accuracy phase cycle\n");
2759 seq_puts(s, "---------------------------------------------------------------------------------------------\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002760
2761 clk_prepare_lock();
2762
2763 for (; *lists; lists++)
2764 hlist_for_each_entry(c, *lists, child_node)
2765 clk_summary_show_subtree(s, c, 0);
2766
2767 clk_prepare_unlock();
2768
2769 return 0;
2770}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002771DEFINE_SHOW_ATTRIBUTE(clk_summary);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002772
2773static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2774{
2775 if (!c)
2776 return;
2777
Stefan Wahren7cb81132015-04-29 16:36:43 +00002778 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002779 seq_printf(s, "\"%s\": { ", c->name);
2780 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2781 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002782 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002783 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2784 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002785 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
Jerome Brunet9fba7382018-06-19 16:41:41 +02002786 seq_printf(s, "\"duty_cycle\": %u",
2787 clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002788}
2789
2790static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2791{
2792 struct clk_core *child;
2793
2794 if (!c)
2795 return;
2796
2797 clk_dump_one(s, c, level);
2798
2799 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002800 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002801 clk_dump_subtree(s, child, level + 1);
2802 }
2803
Markus Elfring4d327582017-04-20 08:45:43 +02002804 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002805}
2806
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002807static int clk_dump_show(struct seq_file *s, void *data)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002808{
2809 struct clk_core *c;
2810 bool first_node = true;
2811 struct hlist_head **lists = (struct hlist_head **)s->private;
2812
Markus Elfring4d327582017-04-20 08:45:43 +02002813 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002814 clk_prepare_lock();
2815
2816 for (; *lists; lists++) {
2817 hlist_for_each_entry(c, *lists, child_node) {
2818 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02002819 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002820 first_node = false;
2821 clk_dump_subtree(s, c, 0);
2822 }
2823 }
2824
2825 clk_prepare_unlock();
2826
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002827 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002828 return 0;
2829}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002830DEFINE_SHOW_ATTRIBUTE(clk_dump);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002831
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002832static const struct {
2833 unsigned long flag;
2834 const char *name;
2835} clk_flags[] = {
Geert Uytterhoeven40dd71c2018-07-06 17:16:54 +02002836#define ENTRY(f) { f, #f }
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002837 ENTRY(CLK_SET_RATE_GATE),
2838 ENTRY(CLK_SET_PARENT_GATE),
2839 ENTRY(CLK_SET_RATE_PARENT),
2840 ENTRY(CLK_IGNORE_UNUSED),
2841 ENTRY(CLK_IS_BASIC),
2842 ENTRY(CLK_GET_RATE_NOCACHE),
2843 ENTRY(CLK_SET_RATE_NO_REPARENT),
2844 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2845 ENTRY(CLK_RECALC_NEW_RATES),
2846 ENTRY(CLK_SET_RATE_UNGATE),
2847 ENTRY(CLK_IS_CRITICAL),
2848 ENTRY(CLK_OPS_PARENT_ENABLE),
Jerome Brunet9fba7382018-06-19 16:41:41 +02002849 ENTRY(CLK_DUTY_CYCLE_PARENT),
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002850#undef ENTRY
2851};
2852
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002853static int clk_flags_show(struct seq_file *s, void *data)
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002854{
2855 struct clk_core *core = s->private;
2856 unsigned long flags = core->flags;
2857 unsigned int i;
2858
2859 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2860 if (flags & clk_flags[i].flag) {
2861 seq_printf(s, "%s\n", clk_flags[i].name);
2862 flags &= ~clk_flags[i].flag;
2863 }
2864 }
2865 if (flags) {
2866 /* Unknown flags */
2867 seq_printf(s, "0x%lx\n", flags);
2868 }
2869
2870 return 0;
2871}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002872DEFINE_SHOW_ATTRIBUTE(clk_flags);
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002873
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002874static int possible_parents_show(struct seq_file *s, void *data)
Peter De Schrijver92031572017-03-21 15:20:31 +02002875{
2876 struct clk_core *core = s->private;
2877 int i;
2878
2879 for (i = 0; i < core->num_parents - 1; i++)
2880 seq_printf(s, "%s ", core->parent_names[i]);
2881
2882 seq_printf(s, "%s\n", core->parent_names[i]);
2883
2884 return 0;
2885}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002886DEFINE_SHOW_ATTRIBUTE(possible_parents);
Peter De Schrijver92031572017-03-21 15:20:31 +02002887
Jerome Brunet9fba7382018-06-19 16:41:41 +02002888static int clk_duty_cycle_show(struct seq_file *s, void *data)
2889{
2890 struct clk_core *core = s->private;
2891 struct clk_duty *duty = &core->duty;
2892
2893 seq_printf(s, "%u/%u\n", duty->num, duty->den);
2894
2895 return 0;
2896}
2897DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
2898
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002899static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002900{
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002901 struct dentry *root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002902
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002903 if (!core || !pdentry)
2904 return;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002905
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002906 root = debugfs_create_dir(core->name, pdentry);
2907 core->dentry = root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002908
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002909 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
2910 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
2911 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
2912 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
2913 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
2914 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
2915 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
2916 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
Jerome Brunet9fba7382018-06-19 16:41:41 +02002917 debugfs_create_file("clk_duty_cycle", 0444, root, core,
2918 &clk_duty_cycle_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002919
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002920 if (core->num_parents > 1)
2921 debugfs_create_file("clk_possible_parents", 0444, root, core,
2922 &possible_parents_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002923
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002924 if (core->ops->debug_init)
2925 core->ops->debug_init(core->hw, core->dentry);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002926}
2927
2928/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002929 * clk_debug_register - add a clk node to the debugfs clk directory
2930 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002931 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002932 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2933 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002934 * will be created lazily by clk_debug_init as part of a late_initcall.
2935 */
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002936static void clk_debug_register(struct clk_core *core)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002937{
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002938 mutex_lock(&clk_debug_lock);
2939 hlist_add_head(&core->debug_node, &clk_debug_list);
Stephen Boyddb3188fa2018-01-03 16:44:37 -08002940 if (inited)
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002941 clk_debug_create_one(core, rootdir);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002942 mutex_unlock(&clk_debug_lock);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002943}
2944
2945 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002946 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2947 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002948 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002949 * Dynamically removes a clk and all its child nodes from the
2950 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08002951 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002952 */
2953static void clk_debug_unregister(struct clk_core *core)
2954{
2955 mutex_lock(&clk_debug_lock);
2956 hlist_del_init(&core->debug_node);
2957 debugfs_remove_recursive(core->dentry);
2958 core->dentry = NULL;
2959 mutex_unlock(&clk_debug_lock);
2960}
2961
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002962/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002963 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002964 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002965 * clks are often initialized very early during boot before memory can be
2966 * dynamically allocated and well before debugfs is setup. This function
2967 * populates the debugfs clk directory once at boot-time when we know that
2968 * debugfs is setup. It should only be called once at boot-time, all other clks
2969 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002970 */
2971static int __init clk_debug_init(void)
2972{
2973 struct clk_core *core;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002974
2975 rootdir = debugfs_create_dir("clk", NULL);
2976
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002977 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
2978 &clk_summary_fops);
2979 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
2980 &clk_dump_fops);
2981 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
2982 &clk_summary_fops);
2983 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
2984 &clk_dump_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002985
2986 mutex_lock(&clk_debug_lock);
2987 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2988 clk_debug_create_one(core, rootdir);
2989
2990 inited = 1;
2991 mutex_unlock(&clk_debug_lock);
2992
2993 return 0;
2994}
2995late_initcall(clk_debug_init);
2996#else
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002997static inline void clk_debug_register(struct clk_core *core) { }
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002998static inline void clk_debug_reparent(struct clk_core *core,
2999 struct clk_core *new_parent)
3000{
3001}
3002static inline void clk_debug_unregister(struct clk_core *core)
3003{
3004}
3005#endif
3006
Michael Turquette3d3801e2015-02-25 09:11:01 -08003007/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003008 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003009 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07003010 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003011 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07003012 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07003013 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003014static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07003015{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003016 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003017 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003018 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003019 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003020
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003021 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07003022 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07003023
Mike Turquetteeab89f62013-03-28 13:59:01 -07003024 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003025
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003026 ret = clk_pm_runtime_get(core);
3027 if (ret)
3028 goto unlock;
3029
Mike Turquetteb24764902012-03-15 23:11:19 -07003030 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003031 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003032 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003033 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003034 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07003035 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07003036 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003037
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03003038 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003039 if (core->ops->set_rate &&
3040 !((core->ops->round_rate || core->ops->determine_rate) &&
3041 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003042 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3043 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003044 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003045 goto out;
3046 }
3047
Stephen Boydd6968fc2015-04-30 13:54:13 -07003048 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003049 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3050 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003051 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003052 goto out;
3053 }
3054
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09003055 if (core->num_parents > 1 && !core->ops->get_parent) {
3056 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3057 __func__, core->name);
3058 ret = -EINVAL;
3059 goto out;
3060 }
3061
Stephen Boydd6968fc2015-04-30 13:54:13 -07003062 if (core->ops->set_rate_and_parent &&
3063 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003064 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003065 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08003066 ret = -EINVAL;
3067 goto out;
3068 }
3069
Mike Turquetteb24764902012-03-15 23:11:19 -07003070 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003071 for (i = 0; i < core->num_parents; i++)
3072 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07003073 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003074 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07003075
Stephen Boydd6968fc2015-04-30 13:54:13 -07003076 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07003077
3078 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08003079 * Populate core->parent if parent has already been clk_core_init'd. If
3080 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003081 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07003082 * clk list.
3083 *
3084 * Every time a new clk is clk_init'd then we walk the list of orphan
3085 * clocks and re-parent any that are children of the clock currently
3086 * being clk_init'd.
3087 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02003088 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003089 hlist_add_head(&core->child_node,
3090 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003091 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003092 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003093 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003094 core->orphan = false;
3095 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003096 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003097 core->orphan = true;
3098 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003099
3100 /*
Jerome Brunet541deba2018-02-14 14:43:37 +01003101 * optional platform-specific magic
3102 *
3103 * The .init callback is not used by any of the basic clock types, but
3104 * exists for weird hardware that must perform initialization magic.
3105 * Please consider other ways of solving initialization problems before
3106 * using this callback, as its use is discouraged.
3107 */
3108 if (core->ops->init)
3109 core->ops->init(core->hw);
3110
3111 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003112 * Set clk's accuracy. The preferred method is to use
3113 * .recalc_accuracy. For simple clocks and lazy developers the default
3114 * fallback is to use the parent's accuracy. If a clock doesn't have a
3115 * parent (or is orphaned) then accuracy is set to zero (perfect
3116 * clock).
3117 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003118 if (core->ops->recalc_accuracy)
3119 core->accuracy = core->ops->recalc_accuracy(core->hw,
3120 __clk_get_accuracy(core->parent));
3121 else if (core->parent)
3122 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003123 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003124 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003125
3126 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02003127 * Set clk's phase.
3128 * Since a phase is by definition relative to its parent, just
3129 * query the current clock phase, or just assume it's in phase.
3130 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003131 if (core->ops->get_phase)
3132 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02003133 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003134 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02003135
3136 /*
Jerome Brunet9fba7382018-06-19 16:41:41 +02003137 * Set clk's duty cycle.
3138 */
3139 clk_core_update_duty_cycle_nolock(core);
3140
3141 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07003142 * Set clk's rate. The preferred method is to use .recalc_rate. For
3143 * simple clocks and lazy developers the default fallback is to use the
3144 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3145 * then rate is set to zero.
3146 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003147 if (core->ops->recalc_rate)
3148 rate = core->ops->recalc_rate(core->hw,
3149 clk_core_get_rate_nolock(core->parent));
3150 else if (core->parent)
3151 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003152 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003153 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003154 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003155
3156 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003157 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3158 * don't get accidentally disabled when walking the orphan tree and
3159 * reparenting clocks
3160 */
3161 if (core->flags & CLK_IS_CRITICAL) {
3162 unsigned long flags;
3163
3164 clk_core_prepare(core);
3165
3166 flags = clk_enable_lock();
3167 clk_core_enable(core);
3168 clk_enable_unlock(flags);
3169 }
3170
3171 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003172 * walk the list of orphan clocks and reparent any that newly finds a
3173 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07003174 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08003175 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003176 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01003177
Michael Turquette904e6ea2016-07-08 16:32:10 -07003178 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003179 * We need to use __clk_set_parent_before() and _after() to
3180 * to properly migrate any prepare/enable count of the orphan
3181 * clock. This is important for CLK_IS_CRITICAL clocks, which
3182 * are enabled during init but might not have a parent yet.
Michael Turquette904e6ea2016-07-08 16:32:10 -07003183 */
3184 if (parent) {
Stephen Boydf8f8f1d2017-11-02 00:36:09 -07003185 /* update the clk tree topology */
Jerome Brunet99652a42018-02-14 14:43:36 +01003186 __clk_set_parent_before(orphan, parent);
3187 __clk_set_parent_after(orphan, parent, NULL);
Michael Turquette904e6ea2016-07-08 16:32:10 -07003188 __clk_recalc_accuracies(orphan);
3189 __clk_recalc_rates(orphan, 0);
3190 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003191 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003192
Stephen Boydd6968fc2015-04-30 13:54:13 -07003193 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07003194out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003195 clk_pm_runtime_put(core);
3196unlock:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003197 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003198
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003199 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003200 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003201
Mike Turquetted1302a32012-03-29 14:30:40 -07003202 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07003203}
3204
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003205struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
3206 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003207{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003208 struct clk *clk;
3209
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003210 /* This is to allow this function to be chained to others */
Masahiro Yamadac1de1352015-11-20 14:38:49 +09003211 if (IS_ERR_OR_NULL(hw))
Masahiro Yamada8a231332016-07-19 16:28:47 +09003212 return ERR_CAST(hw);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003213
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003214 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3215 if (!clk)
3216 return ERR_PTR(-ENOMEM);
3217
3218 clk->core = hw->core;
3219 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02003220 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003221 clk->max_rate = ULONG_MAX;
3222
3223 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08003224 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003225 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003226
3227 return clk;
3228}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003229
Mikko Perttunen365f7a82018-07-11 11:21:04 +03003230/* keep in sync with __clk_put */
Stephen Boyd73e0e492015-02-06 11:42:43 -08003231void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003232{
3233 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08003234 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003235 clk_prepare_unlock();
3236
Leonard Crestez253160a2017-02-20 15:20:56 +02003237 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003238 kfree(clk);
3239}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003240
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003241/**
3242 * clk_register - allocate a new clock, register it and return an opaque cookie
3243 * @dev: device that is registering this clock
3244 * @hw: link to hardware-specific clock data
3245 *
3246 * clk_register is the primary interface for populating the clock tree with new
3247 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05303248 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003249 * rest of the clock API. In the event of an error clk_register will return an
3250 * error code; drivers must test for an error code after calling clk_register.
3251 */
3252struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07003253{
Mike Turquetted1302a32012-03-29 14:30:40 -07003254 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003255 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003256
Stephen Boydd6968fc2015-04-30 13:54:13 -07003257 core = kzalloc(sizeof(*core), GFP_KERNEL);
3258 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003259 ret = -ENOMEM;
3260 goto fail_out;
3261 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003262
Stephen Boydd6968fc2015-04-30 13:54:13 -07003263 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3264 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003265 ret = -ENOMEM;
3266 goto fail_name;
3267 }
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003268
3269 if (WARN_ON(!hw->init->ops)) {
3270 ret = -EINVAL;
3271 goto fail_ops;
3272 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07003273 core->ops = hw->init->ops;
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003274
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003275 if (dev && pm_runtime_enabled(dev))
3276 core->dev = dev;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003277 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003278 core->owner = dev->driver->owner;
3279 core->hw = hw;
3280 core->flags = hw->init->flags;
3281 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003282 core->min_rate = 0;
3283 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003284 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003285
Mike Turquetted1302a32012-03-29 14:30:40 -07003286 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003287 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02003288 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003289
Stephen Boydd6968fc2015-04-30 13:54:13 -07003290 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003291 ret = -ENOMEM;
3292 goto fail_parent_names;
3293 }
3294
3295
3296 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003297 for (i = 0; i < core->num_parents; i++) {
3298 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003299 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003300 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003301 ret = -ENOMEM;
3302 goto fail_parent_names_copy;
3303 }
3304 }
3305
Masahiro Yamada176d1162015-12-28 19:23:00 +09003306 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3307 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3308 GFP_KERNEL);
3309 if (!core->parents) {
3310 ret = -ENOMEM;
3311 goto fail_parents;
3312 };
3313
Stephen Boydd6968fc2015-04-30 13:54:13 -07003314 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003315
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003316 hw->clk = __clk_create_clk(hw, NULL, NULL);
3317 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003318 ret = PTR_ERR(hw->clk);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003319 goto fail_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003320 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003321
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003322 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003323 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003324 return hw->clk;
3325
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003326 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003327 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003328
Masahiro Yamada176d1162015-12-28 19:23:00 +09003329fail_parents:
3330 kfree(core->parents);
Mike Turquetted1302a32012-03-29 14:30:40 -07003331fail_parent_names_copy:
3332 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003333 kfree_const(core->parent_names[i]);
3334 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07003335fail_parent_names:
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003336fail_ops:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003337 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003338fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003339 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003340fail_out:
3341 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003342}
3343EXPORT_SYMBOL_GPL(clk_register);
3344
Stephen Boyd41438042016-02-05 17:02:52 -08003345/**
3346 * clk_hw_register - register a clk_hw and return an error code
3347 * @dev: device that is registering this clock
3348 * @hw: link to hardware-specific clock data
3349 *
3350 * clk_hw_register is the primary interface for populating the clock tree with
3351 * new clock nodes. It returns an integer equal to zero indicating success or
3352 * less than zero indicating failure. Drivers must test for an error code after
3353 * calling clk_hw_register().
3354 */
3355int clk_hw_register(struct device *dev, struct clk_hw *hw)
3356{
3357 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3358}
3359EXPORT_SYMBOL_GPL(clk_hw_register);
3360
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003361/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003362static void __clk_release(struct kref *ref)
3363{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003364 struct clk_core *core = container_of(ref, struct clk_core, ref);
3365 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003366
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003367 lockdep_assert_held(&prepare_lock);
3368
Stephen Boydd6968fc2015-04-30 13:54:13 -07003369 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003370 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003371 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003372
Stephen Boydd6968fc2015-04-30 13:54:13 -07003373 kfree(core->parent_names);
3374 kfree_const(core->name);
3375 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003376}
3377
3378/*
3379 * Empty clk_ops for unregistered clocks. These are used temporarily
3380 * after clk_unregister() was called on a clock and until last clock
3381 * consumer calls clk_put() and the struct clk object is freed.
3382 */
3383static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3384{
3385 return -ENXIO;
3386}
3387
3388static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3389{
3390 WARN_ON_ONCE(1);
3391}
3392
3393static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3394 unsigned long parent_rate)
3395{
3396 return -ENXIO;
3397}
3398
3399static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3400{
3401 return -ENXIO;
3402}
3403
3404static const struct clk_ops clk_nodrv_ops = {
3405 .enable = clk_nodrv_prepare_enable,
3406 .disable = clk_nodrv_disable_unprepare,
3407 .prepare = clk_nodrv_prepare_enable,
3408 .unprepare = clk_nodrv_disable_unprepare,
3409 .set_rate = clk_nodrv_set_rate,
3410 .set_parent = clk_nodrv_set_parent,
3411};
3412
Mark Brown1df5c932012-04-18 09:07:12 +01003413/**
3414 * clk_unregister - unregister a currently registered clock
3415 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003416 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003417void clk_unregister(struct clk *clk)
3418{
3419 unsigned long flags;
3420
Stephen Boyd6314b672014-09-04 23:37:49 -07003421 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3422 return;
3423
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003424 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003425
3426 clk_prepare_lock();
3427
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003428 if (clk->core->ops == &clk_nodrv_ops) {
3429 pr_err("%s: unregistered clock: %s\n", __func__,
3430 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003431 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003432 }
3433 /*
3434 * Assign empty clock ops for consumers that might still hold
3435 * a reference to this clock.
3436 */
3437 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003438 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003439 clk_enable_unlock(flags);
3440
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003441 if (!hlist_empty(&clk->core->children)) {
3442 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003443 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003444
3445 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003446 hlist_for_each_entry_safe(child, t, &clk->core->children,
3447 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01003448 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003449 }
3450
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003451 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003452
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003453 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003454 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003455 __func__, clk->core->name);
Jerome Brunete55a8392017-12-01 22:51:56 +01003456
3457 if (clk->core->protect_count)
3458 pr_warn("%s: unregistering protected clock: %s\n",
3459 __func__, clk->core->name);
3460
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003461 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003462unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003463 clk_prepare_unlock();
3464}
Mark Brown1df5c932012-04-18 09:07:12 +01003465EXPORT_SYMBOL_GPL(clk_unregister);
3466
Stephen Boyd41438042016-02-05 17:02:52 -08003467/**
3468 * clk_hw_unregister - unregister a currently registered clk_hw
3469 * @hw: hardware-specific clock data to unregister
3470 */
3471void clk_hw_unregister(struct clk_hw *hw)
3472{
3473 clk_unregister(hw->clk);
3474}
3475EXPORT_SYMBOL_GPL(clk_hw_unregister);
3476
Stephen Boyd46c87732012-09-24 13:38:04 -07003477static void devm_clk_release(struct device *dev, void *res)
3478{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003479 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003480}
3481
Stephen Boyd41438042016-02-05 17:02:52 -08003482static void devm_clk_hw_release(struct device *dev, void *res)
3483{
3484 clk_hw_unregister(*(struct clk_hw **)res);
3485}
3486
Stephen Boyd46c87732012-09-24 13:38:04 -07003487/**
3488 * devm_clk_register - resource managed clk_register()
3489 * @dev: device that is registering this clock
3490 * @hw: link to hardware-specific clock data
3491 *
3492 * Managed clk_register(). Clocks returned from this function are
3493 * automatically clk_unregister()ed on driver detach. See clk_register() for
3494 * more information.
3495 */
3496struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3497{
3498 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003499 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003500
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003501 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3502 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003503 return ERR_PTR(-ENOMEM);
3504
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003505 clk = clk_register(dev, hw);
3506 if (!IS_ERR(clk)) {
3507 *clkp = clk;
3508 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003509 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003510 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003511 }
3512
3513 return clk;
3514}
3515EXPORT_SYMBOL_GPL(devm_clk_register);
3516
Stephen Boyd41438042016-02-05 17:02:52 -08003517/**
3518 * devm_clk_hw_register - resource managed clk_hw_register()
3519 * @dev: device that is registering this clock
3520 * @hw: link to hardware-specific clock data
3521 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003522 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003523 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3524 * for more information.
3525 */
3526int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3527{
3528 struct clk_hw **hwp;
3529 int ret;
3530
3531 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3532 if (!hwp)
3533 return -ENOMEM;
3534
3535 ret = clk_hw_register(dev, hw);
3536 if (!ret) {
3537 *hwp = hw;
3538 devres_add(dev, hwp);
3539 } else {
3540 devres_free(hwp);
3541 }
3542
3543 return ret;
3544}
3545EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3546
Stephen Boyd46c87732012-09-24 13:38:04 -07003547static int devm_clk_match(struct device *dev, void *res, void *data)
3548{
3549 struct clk *c = res;
3550 if (WARN_ON(!c))
3551 return 0;
3552 return c == data;
3553}
3554
Stephen Boyd41438042016-02-05 17:02:52 -08003555static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3556{
3557 struct clk_hw *hw = res;
3558
3559 if (WARN_ON(!hw))
3560 return 0;
3561 return hw == data;
3562}
3563
Stephen Boyd46c87732012-09-24 13:38:04 -07003564/**
3565 * devm_clk_unregister - resource managed clk_unregister()
3566 * @clk: clock to unregister
3567 *
3568 * Deallocate a clock allocated with devm_clk_register(). Normally
3569 * this function will not need to be called and the resource management
3570 * code will ensure that the resource is freed.
3571 */
3572void devm_clk_unregister(struct device *dev, struct clk *clk)
3573{
3574 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3575}
3576EXPORT_SYMBOL_GPL(devm_clk_unregister);
3577
Stephen Boyd41438042016-02-05 17:02:52 -08003578/**
3579 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3580 * @dev: device that is unregistering the hardware-specific clock data
3581 * @hw: link to hardware-specific clock data
3582 *
3583 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3584 * this function will not need to be called and the resource management
3585 * code will ensure that the resource is freed.
3586 */
3587void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3588{
3589 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3590 hw));
3591}
3592EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3593
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003594/*
3595 * clkdev helpers
3596 */
3597int __clk_get(struct clk *clk)
3598{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003599 struct clk_core *core = !clk ? NULL : clk->core;
3600
3601 if (core) {
3602 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003603 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003604
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003605 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003606 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003607 return 1;
3608}
3609
Mikko Perttunen365f7a82018-07-11 11:21:04 +03003610/* keep in sync with __clk_free_clk */
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003611void __clk_put(struct clk *clk)
3612{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003613 struct module *owner;
3614
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003615 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003616 return;
3617
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003618 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003619
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01003620 /*
3621 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3622 * given user should be balanced with calls to clk_rate_exclusive_put()
3623 * and by that same consumer
3624 */
3625 if (WARN_ON(clk->exclusive_count)) {
3626 /* We voiced our concern, let's sanitize the situation */
3627 clk->core->protect_count -= (clk->exclusive_count - 1);
3628 clk_core_rate_unprotect(clk->core);
3629 clk->exclusive_count = 0;
3630 }
3631
Stephen Boyd50595f82015-02-06 11:42:44 -08003632 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003633 if (clk->min_rate > clk->core->req_rate ||
3634 clk->max_rate < clk->core->req_rate)
3635 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3636
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003637 owner = clk->core->owner;
3638 kref_put(&clk->core->ref, __clk_release);
3639
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003640 clk_prepare_unlock();
3641
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003642 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003643
Mikko Perttunen365f7a82018-07-11 11:21:04 +03003644 kfree_const(clk->con_id);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003645 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003646}
3647
Mike Turquetteb24764902012-03-15 23:11:19 -07003648/*** clk rate change notifiers ***/
3649
3650/**
3651 * clk_notifier_register - add a clk rate change notifier
3652 * @clk: struct clk * to watch
3653 * @nb: struct notifier_block * with callback info
3654 *
3655 * Request notification when clk's rate changes. This uses an SRCU
3656 * notifier because we want it to block and notifier unregistrations are
3657 * uncommon. The callbacks associated with the notifier must not
3658 * re-enter into the clk framework by calling any top-level clk APIs;
3659 * this will cause a nested prepare_lock mutex.
3660 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003661 * In all notification cases (pre, post and abort rate change) the original
3662 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3663 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003664 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003665 * clk_notifier_register() must be called from non-atomic context.
3666 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3667 * allocation failure; otherwise, passes along the return value of
3668 * srcu_notifier_chain_register().
3669 */
3670int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3671{
3672 struct clk_notifier *cn;
3673 int ret = -ENOMEM;
3674
3675 if (!clk || !nb)
3676 return -EINVAL;
3677
Mike Turquetteeab89f62013-03-28 13:59:01 -07003678 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003679
3680 /* search the list of notifiers for this clk */
3681 list_for_each_entry(cn, &clk_notifier_list, node)
3682 if (cn->clk == clk)
3683 break;
3684
3685 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3686 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02003687 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003688 if (!cn)
3689 goto out;
3690
3691 cn->clk = clk;
3692 srcu_init_notifier_head(&cn->notifier_head);
3693
3694 list_add(&cn->node, &clk_notifier_list);
3695 }
3696
3697 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3698
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003699 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003700
3701out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003702 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003703
3704 return ret;
3705}
3706EXPORT_SYMBOL_GPL(clk_notifier_register);
3707
3708/**
3709 * clk_notifier_unregister - remove a clk rate change notifier
3710 * @clk: struct clk *
3711 * @nb: struct notifier_block * with callback info
3712 *
3713 * Request no further notification for changes to 'clk' and frees memory
3714 * allocated in clk_notifier_register.
3715 *
3716 * Returns -EINVAL if called with null arguments; otherwise, passes
3717 * along the return value of srcu_notifier_chain_unregister().
3718 */
3719int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3720{
3721 struct clk_notifier *cn = NULL;
3722 int ret = -EINVAL;
3723
3724 if (!clk || !nb)
3725 return -EINVAL;
3726
Mike Turquetteeab89f62013-03-28 13:59:01 -07003727 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003728
3729 list_for_each_entry(cn, &clk_notifier_list, node)
3730 if (cn->clk == clk)
3731 break;
3732
3733 if (cn->clk == clk) {
3734 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3735
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003736 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07003737
3738 /* XXX the notifier code should handle this better */
3739 if (!cn->notifier_head.head) {
3740 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08003741 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07003742 kfree(cn);
3743 }
3744
3745 } else {
3746 ret = -ENOENT;
3747 }
3748
Mike Turquetteeab89f62013-03-28 13:59:01 -07003749 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003750
3751 return ret;
3752}
3753EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05003754
3755#ifdef CONFIG_OF
3756/**
3757 * struct of_clk_provider - Clock provider registration structure
3758 * @link: Entry in global list of clock providers
3759 * @node: Pointer to device tree node of clock provider
3760 * @get: Get clock callback. Returns NULL or a struct clk for the
3761 * given clock specifier
3762 * @data: context pointer to be passed into @get callback
3763 */
3764struct of_clk_provider {
3765 struct list_head link;
3766
3767 struct device_node *node;
3768 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003769 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05003770 void *data;
3771};
3772
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303773static const struct of_device_id __clk_of_table_sentinel
3774 __used __section(__clk_of_table_end);
3775
Grant Likely766e6a42012-04-09 14:50:06 -05003776static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003777static DEFINE_MUTEX(of_clk_mutex);
3778
Grant Likely766e6a42012-04-09 14:50:06 -05003779struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3780 void *data)
3781{
3782 return data;
3783}
3784EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3785
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003786struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3787{
3788 return data;
3789}
3790EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3791
Shawn Guo494bfec2012-08-22 21:36:27 +08003792struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3793{
3794 struct clk_onecell_data *clk_data = data;
3795 unsigned int idx = clkspec->args[0];
3796
3797 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02003798 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08003799 return ERR_PTR(-EINVAL);
3800 }
3801
3802 return clk_data->clks[idx];
3803}
3804EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3805
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003806struct clk_hw *
3807of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3808{
3809 struct clk_hw_onecell_data *hw_data = data;
3810 unsigned int idx = clkspec->args[0];
3811
3812 if (idx >= hw_data->num) {
3813 pr_err("%s: invalid index %u\n", __func__, idx);
3814 return ERR_PTR(-EINVAL);
3815 }
3816
3817 return hw_data->hws[idx];
3818}
3819EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3820
Grant Likely766e6a42012-04-09 14:50:06 -05003821/**
3822 * of_clk_add_provider() - Register a clock provider for a node
3823 * @np: Device node pointer associated with clock provider
3824 * @clk_src_get: callback for decoding clock
3825 * @data: context pointer for @clk_src_get callback.
3826 */
3827int of_clk_add_provider(struct device_node *np,
3828 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3829 void *data),
3830 void *data)
3831{
3832 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003833 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003834
Markus Elfring1808a322017-04-20 09:30:52 +02003835 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05003836 if (!cp)
3837 return -ENOMEM;
3838
3839 cp->node = of_node_get(np);
3840 cp->data = data;
3841 cp->get = clk_src_get;
3842
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003843 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003844 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003845 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003846 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05003847
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003848 ret = of_clk_set_defaults(np, true);
3849 if (ret < 0)
3850 of_clk_del_provider(np);
3851
3852 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003853}
3854EXPORT_SYMBOL_GPL(of_clk_add_provider);
3855
3856/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003857 * of_clk_add_hw_provider() - Register a clock provider for a node
3858 * @np: Device node pointer associated with clock provider
3859 * @get: callback for decoding clk_hw
3860 * @data: context pointer for @get callback.
3861 */
3862int of_clk_add_hw_provider(struct device_node *np,
3863 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3864 void *data),
3865 void *data)
3866{
3867 struct of_clk_provider *cp;
3868 int ret;
3869
3870 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3871 if (!cp)
3872 return -ENOMEM;
3873
3874 cp->node = of_node_get(np);
3875 cp->data = data;
3876 cp->get_hw = get;
3877
3878 mutex_lock(&of_clk_mutex);
3879 list_add(&cp->link, &of_clk_providers);
3880 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003881 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003882
3883 ret = of_clk_set_defaults(np, true);
3884 if (ret < 0)
3885 of_clk_del_provider(np);
3886
3887 return ret;
3888}
3889EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3890
Stephen Boydaa795c42017-09-01 16:16:40 -07003891static void devm_of_clk_release_provider(struct device *dev, void *res)
3892{
3893 of_clk_del_provider(*(struct device_node **)res);
3894}
3895
3896int devm_of_clk_add_hw_provider(struct device *dev,
3897 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3898 void *data),
3899 void *data)
3900{
3901 struct device_node **ptr, *np;
3902 int ret;
3903
3904 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3905 GFP_KERNEL);
3906 if (!ptr)
3907 return -ENOMEM;
3908
3909 np = dev->of_node;
3910 ret = of_clk_add_hw_provider(np, get, data);
3911 if (!ret) {
3912 *ptr = np;
3913 devres_add(dev, ptr);
3914 } else {
3915 devres_free(ptr);
3916 }
3917
3918 return ret;
3919}
3920EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3921
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003922/**
Grant Likely766e6a42012-04-09 14:50:06 -05003923 * of_clk_del_provider() - Remove a previously registered clock provider
3924 * @np: Device node pointer associated with clock provider
3925 */
3926void of_clk_del_provider(struct device_node *np)
3927{
3928 struct of_clk_provider *cp;
3929
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003930 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003931 list_for_each_entry(cp, &of_clk_providers, link) {
3932 if (cp->node == np) {
3933 list_del(&cp->link);
3934 of_node_put(cp->node);
3935 kfree(cp);
3936 break;
3937 }
3938 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003939 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003940}
3941EXPORT_SYMBOL_GPL(of_clk_del_provider);
3942
Stephen Boydaa795c42017-09-01 16:16:40 -07003943static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3944{
3945 struct device_node **np = res;
3946
3947 if (WARN_ON(!np || !*np))
3948 return 0;
3949
3950 return *np == data;
3951}
3952
3953void devm_of_clk_del_provider(struct device *dev)
3954{
3955 int ret;
3956
3957 ret = devres_release(dev, devm_of_clk_release_provider,
3958 devm_clk_provider_match, dev->of_node);
3959
3960 WARN_ON(ret);
3961}
3962EXPORT_SYMBOL(devm_of_clk_del_provider);
3963
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003964static struct clk_hw *
3965__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3966 struct of_phandle_args *clkspec)
3967{
3968 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003969
Stephen Boyd74002fc2016-08-25 13:35:36 -07003970 if (provider->get_hw)
3971 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003972
Stephen Boyd74002fc2016-08-25 13:35:36 -07003973 clk = provider->get(clkspec, provider->data);
3974 if (IS_ERR(clk))
3975 return ERR_CAST(clk);
3976 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003977}
3978
Stephen Boyd73e0e492015-02-06 11:42:43 -08003979struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3980 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05003981{
3982 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01003983 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Stephen Boydf155d152016-08-15 14:32:23 -07003984 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -05003985
Stephen Boyd306c3422015-02-05 15:39:11 -08003986 if (!clkspec)
3987 return ERR_PTR(-EINVAL);
3988
Grant Likely766e6a42012-04-09 14:50:06 -05003989 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08003990 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003991 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07003992 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003993 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003994 clk = __clk_create_clk(hw, dev_id, con_id);
Stephen Boydf155d152016-08-15 14:32:23 -07003995 }
Stephen Boyd73e0e492015-02-06 11:42:43 -08003996
Stephen Boydf155d152016-08-15 14:32:23 -07003997 if (!IS_ERR(clk)) {
3998 if (!__clk_get(clk)) {
Stephen Boyd73e0e492015-02-06 11:42:43 -08003999 __clk_free_clk(clk);
4000 clk = ERR_PTR(-ENOENT);
4001 }
4002
Grant Likely766e6a42012-04-09 14:50:06 -05004003 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08004004 }
Grant Likely766e6a42012-04-09 14:50:06 -05004005 }
Stephen Boyd306c3422015-02-05 15:39:11 -08004006 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004007
4008 return clk;
4009}
4010
Stephen Boyd306c3422015-02-05 15:39:11 -08004011/**
4012 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4013 * @clkspec: pointer to a clock specifier data structure
4014 *
4015 * This function looks up a struct clk from the registered list of clock
4016 * providers, an input is a clock specifier data structure as returned
4017 * from the of_parse_phandle_with_args() function call.
4018 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004019struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4020{
Stephen Boyd306c3422015-02-05 15:39:11 -08004021 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05004022}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06004023EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05004024
Stephen Boyd929e7f32016-02-19 15:52:32 -08004025/**
4026 * of_clk_get_parent_count() - Count the number of clocks a device node has
4027 * @np: device node to count
4028 *
4029 * Returns: The number of clocks that are possible parents of this node
4030 */
4031unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07004032{
Stephen Boyd929e7f32016-02-19 15:52:32 -08004033 int count;
4034
4035 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4036 if (count < 0)
4037 return 0;
4038
4039 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07004040}
4041EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4042
Grant Likely766e6a42012-04-09 14:50:06 -05004043const char *of_clk_get_parent_name(struct device_node *np, int index)
4044{
4045 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004046 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05004047 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004048 const __be32 *vp;
4049 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05004050 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004051 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004052 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05004053
Grant Likely766e6a42012-04-09 14:50:06 -05004054 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4055 &clkspec);
4056 if (rc)
4057 return NULL;
4058
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004059 index = clkspec.args_count ? clkspec.args[0] : 0;
4060 count = 0;
4061
4062 /* if there is an indices property, use it to transfer the index
4063 * specified into an array offset for the clock-output-names property.
4064 */
4065 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4066 if (index == pv) {
4067 index = count;
4068 break;
4069 }
4070 count++;
4071 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09004072 /* We went off the end of 'clock-indices' without finding it */
4073 if (prop && !vp)
4074 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004075
Grant Likely766e6a42012-04-09 14:50:06 -05004076 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004077 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004078 &clk_name) < 0) {
4079 /*
4080 * Best effort to get the name if the clock has been
4081 * registered with the framework. If the clock isn't
4082 * registered, we return the node name as the name of
4083 * the clock as long as #clock-cells = 0.
4084 */
4085 clk = of_clk_get_from_provider(&clkspec);
4086 if (IS_ERR(clk)) {
4087 if (clkspec.args_count == 0)
4088 clk_name = clkspec.np->name;
4089 else
4090 clk_name = NULL;
4091 } else {
4092 clk_name = __clk_get_name(clk);
4093 clk_put(clk);
4094 }
4095 }
4096
Grant Likely766e6a42012-04-09 14:50:06 -05004097
4098 of_node_put(clkspec.np);
4099 return clk_name;
4100}
4101EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4102
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05004103/**
4104 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4105 * number of parents
4106 * @np: Device node pointer associated with clock provider
4107 * @parents: pointer to char array that hold the parents' names
4108 * @size: size of the @parents array
4109 *
4110 * Return: number of parents for the clock node.
4111 */
4112int of_clk_parent_fill(struct device_node *np, const char **parents,
4113 unsigned int size)
4114{
4115 unsigned int i = 0;
4116
4117 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4118 i++;
4119
4120 return i;
4121}
4122EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4123
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004124struct clock_provider {
Geert Uytterhoevena59704332018-04-10 15:06:05 +02004125 void (*clk_init_cb)(struct device_node *);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004126 struct device_node *np;
4127 struct list_head node;
4128};
4129
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004130/*
4131 * This function looks for a parent clock. If there is one, then it
4132 * checks that the provider for this parent clock was initialized, in
4133 * this case the parent clock will be ready.
4134 */
4135static int parent_ready(struct device_node *np)
4136{
4137 int i = 0;
4138
4139 while (true) {
4140 struct clk *clk = of_clk_get(np, i);
4141
4142 /* this parent is ready we can check the next one */
4143 if (!IS_ERR(clk)) {
4144 clk_put(clk);
4145 i++;
4146 continue;
4147 }
4148
4149 /* at least one parent is not ready, we exit now */
4150 if (PTR_ERR(clk) == -EPROBE_DEFER)
4151 return 0;
4152
4153 /*
4154 * Here we make assumption that the device tree is
4155 * written correctly. So an error means that there is
4156 * no more parent. As we didn't exit yet, then the
4157 * previous parent are ready. If there is no clock
4158 * parent, no need to wait for them, then we can
4159 * consider their absence as being ready
4160 */
4161 return 1;
4162 }
4163}
4164
Grant Likely766e6a42012-04-09 14:50:06 -05004165/**
Lee Jonesd56f8992016-02-11 13:19:11 -08004166 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4167 * @np: Device node pointer associated with clock provider
4168 * @index: clock index
Geert Uytterhoevenf7ae7502018-01-03 12:06:14 +01004169 * @flags: pointer to top-level framework flags
Lee Jonesd56f8992016-02-11 13:19:11 -08004170 *
4171 * Detects if the clock-critical property exists and, if so, sets the
4172 * corresponding CLK_IS_CRITICAL flag.
4173 *
4174 * Do not use this function. It exists only for legacy Device Tree
4175 * bindings, such as the one-clock-per-node style that are outdated.
4176 * Those bindings typically put all clock data into .dts and the Linux
4177 * driver has no clock data, thus making it impossible to set this flag
4178 * correctly from the driver. Only those drivers may call
4179 * of_clk_detect_critical from their setup functions.
4180 *
4181 * Return: error code or zero on success
4182 */
4183int of_clk_detect_critical(struct device_node *np,
4184 int index, unsigned long *flags)
4185{
4186 struct property *prop;
4187 const __be32 *cur;
4188 uint32_t idx;
4189
4190 if (!np || !flags)
4191 return -EINVAL;
4192
4193 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4194 if (index == idx)
4195 *flags |= CLK_IS_CRITICAL;
4196
4197 return 0;
4198}
4199
4200/**
Grant Likely766e6a42012-04-09 14:50:06 -05004201 * of_clk_init() - Scan and init clock providers from the DT
4202 * @matches: array of compatible values and init functions for providers.
4203 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004204 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01004205 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004206 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05004207 */
4208void __init of_clk_init(const struct of_device_id *matches)
4209{
Alex Elder7f7ed582013-08-22 11:31:31 -05004210 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05004211 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004212 struct clock_provider *clk_provider, *next;
4213 bool is_init_done;
4214 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07004215 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05004216
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304217 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03004218 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304219
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004220 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05004221 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004222 struct clock_provider *parent;
4223
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01004224 if (!of_device_is_available(np))
4225 continue;
4226
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004227 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4228 if (!parent) {
4229 list_for_each_entry_safe(clk_provider, next,
4230 &clk_provider_list, node) {
4231 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004232 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004233 kfree(clk_provider);
4234 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004235 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004236 return;
4237 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004238
4239 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004240 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01004241 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004242 }
4243
4244 while (!list_empty(&clk_provider_list)) {
4245 is_init_done = false;
4246 list_for_each_entry_safe(clk_provider, next,
4247 &clk_provider_list, node) {
4248 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004249
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02004250 /* Don't populate platform devices */
4251 of_node_set_flag(clk_provider->np,
4252 OF_POPULATED);
4253
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004254 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004255 of_clk_set_defaults(clk_provider->np, true);
4256
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004257 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004258 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004259 kfree(clk_provider);
4260 is_init_done = true;
4261 }
4262 }
4263
4264 /*
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01004265 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004266 * remaining providers during the last loop, so now we
4267 * initialize all the remaining ones unconditionally
4268 * in case the clock parent was not mandatory
4269 */
4270 if (!is_init_done)
4271 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05004272 }
4273}
4274#endif