blob: 71621a171f8af7ec2020a2ad87ceaaac86916911 [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
Mike Turquetteb24764902012-03-15 23:11:19 -0700926/**
927 * clk_enable - ungate a clock
928 * @clk: the clk being ungated
929 *
930 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
931 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
932 * if the operation will never sleep. One example is a SoC-internal clk which
933 * is controlled via simple register writes. In the complex case a clk ungate
934 * operation may require a fast and a slow part. It is this reason that
935 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
936 * must be called before clk_enable. Returns 0 on success, -EERROR
937 * otherwise.
938 */
939int clk_enable(struct clk *clk)
940{
Dong Aisheng864e1602015-04-30 14:02:19 -0700941 if (!clk)
942 return 0;
943
Dong Aishenga6adc302016-06-30 17:31:11 +0800944 return clk_core_enable_lock(clk->core);
945}
946EXPORT_SYMBOL_GPL(clk_enable);
947
948static int clk_core_prepare_enable(struct clk_core *core)
949{
950 int ret;
951
952 ret = clk_core_prepare_lock(core);
953 if (ret)
954 return ret;
955
956 ret = clk_core_enable_lock(core);
957 if (ret)
958 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700959
960 return ret;
961}
Dong Aishenga6adc302016-06-30 17:31:11 +0800962
963static void clk_core_disable_unprepare(struct clk_core *core)
964{
965 clk_core_disable_lock(core);
966 clk_core_unprepare_lock(core);
967}
Mike Turquetteb24764902012-03-15 23:11:19 -0700968
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800969static void clk_unprepare_unused_subtree(struct clk_core *core)
970{
971 struct clk_core *child;
972
973 lockdep_assert_held(&prepare_lock);
974
975 hlist_for_each_entry(child, &core->children, child_node)
976 clk_unprepare_unused_subtree(child);
977
978 if (core->prepare_count)
979 return;
980
981 if (core->flags & CLK_IGNORE_UNUSED)
982 return;
983
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200984 if (clk_pm_runtime_get(core))
985 return;
986
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800987 if (clk_core_is_prepared(core)) {
988 trace_clk_unprepare(core);
989 if (core->ops->unprepare_unused)
990 core->ops->unprepare_unused(core->hw);
991 else if (core->ops->unprepare)
992 core->ops->unprepare(core->hw);
993 trace_clk_unprepare_complete(core);
994 }
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200995
996 clk_pm_runtime_put(core);
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800997}
998
999static void clk_disable_unused_subtree(struct clk_core *core)
1000{
1001 struct clk_core *child;
1002 unsigned long flags;
1003
1004 lockdep_assert_held(&prepare_lock);
1005
1006 hlist_for_each_entry(child, &core->children, child_node)
1007 clk_disable_unused_subtree(child);
1008
Dong Aishenga4b35182016-06-30 17:31:13 +08001009 if (core->flags & CLK_OPS_PARENT_ENABLE)
1010 clk_core_prepare_enable(core->parent);
1011
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001012 if (clk_pm_runtime_get(core))
1013 goto unprepare_out;
1014
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001015 flags = clk_enable_lock();
1016
1017 if (core->enable_count)
1018 goto unlock_out;
1019
1020 if (core->flags & CLK_IGNORE_UNUSED)
1021 goto unlock_out;
1022
1023 /*
1024 * some gate clocks have special needs during the disable-unused
1025 * sequence. call .disable_unused if available, otherwise fall
1026 * back to .disable
1027 */
1028 if (clk_core_is_enabled(core)) {
1029 trace_clk_disable(core);
1030 if (core->ops->disable_unused)
1031 core->ops->disable_unused(core->hw);
1032 else if (core->ops->disable)
1033 core->ops->disable(core->hw);
1034 trace_clk_disable_complete(core);
1035 }
1036
1037unlock_out:
1038 clk_enable_unlock(flags);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001039 clk_pm_runtime_put(core);
1040unprepare_out:
Dong Aishenga4b35182016-06-30 17:31:13 +08001041 if (core->flags & CLK_OPS_PARENT_ENABLE)
1042 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001043}
1044
1045static bool clk_ignore_unused;
1046static int __init clk_ignore_unused_setup(char *__unused)
1047{
1048 clk_ignore_unused = true;
1049 return 1;
1050}
1051__setup("clk_ignore_unused", clk_ignore_unused_setup);
1052
1053static int clk_disable_unused(void)
1054{
1055 struct clk_core *core;
1056
1057 if (clk_ignore_unused) {
1058 pr_warn("clk: Not disabling unused clocks\n");
1059 return 0;
1060 }
1061
1062 clk_prepare_lock();
1063
1064 hlist_for_each_entry(core, &clk_root_list, child_node)
1065 clk_disable_unused_subtree(core);
1066
1067 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1068 clk_disable_unused_subtree(core);
1069
1070 hlist_for_each_entry(core, &clk_root_list, child_node)
1071 clk_unprepare_unused_subtree(core);
1072
1073 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1074 clk_unprepare_unused_subtree(core);
1075
1076 clk_prepare_unlock();
1077
1078 return 0;
1079}
1080late_initcall_sync(clk_disable_unused);
1081
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001082static int clk_core_determine_round_nolock(struct clk_core *core,
1083 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001084{
Boris Brezillon0817b622015-07-07 20:48:08 +02001085 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001086
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001087 lockdep_assert_held(&prepare_lock);
1088
Stephen Boydd6968fc2015-04-30 13:54:13 -07001089 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001090 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001091
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001092 /*
1093 * At this point, core protection will be disabled if
1094 * - if the provider is not protected at all
1095 * - if the calling consumer is the only one which has exclusivity
1096 * over the provider
1097 */
Jerome Brunete55a8392017-12-01 22:51:56 +01001098 if (clk_core_rate_is_protected(core)) {
1099 req->rate = core->rate;
1100 } else if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001101 return core->ops->determine_rate(core->hw, req);
1102 } else if (core->ops->round_rate) {
1103 rate = core->ops->round_rate(core->hw, req->rate,
1104 &req->best_parent_rate);
1105 if (rate < 0)
1106 return rate;
1107
1108 req->rate = rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001109 } else {
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001110 return -EINVAL;
Boris Brezillon0817b622015-07-07 20:48:08 +02001111 }
1112
1113 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001114}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001115
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001116static void clk_core_init_rate_req(struct clk_core * const core,
1117 struct clk_rate_request *req)
1118{
1119 struct clk_core *parent;
1120
1121 if (WARN_ON(!core || !req))
1122 return;
1123
Mike Turquetteb24764902012-03-15 23:11:19 -07001124 parent = core->parent;
1125 if (parent) {
1126 req->best_parent_hw = parent->hw;
1127 req->best_parent_rate = parent->rate;
1128 } else {
1129 req->best_parent_hw = NULL;
1130 req->best_parent_rate = 0;
1131 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001132}
Mike Turquetteb24764902012-03-15 23:11:19 -07001133
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001134static bool clk_core_can_round(struct clk_core * const core)
1135{
1136 if (core->ops->determine_rate || core->ops->round_rate)
1137 return true;
Mike Turquetteb24764902012-03-15 23:11:19 -07001138
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001139 return false;
1140}
Mike Turquetteb24764902012-03-15 23:11:19 -07001141
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001142static int clk_core_round_rate_nolock(struct clk_core *core,
1143 struct clk_rate_request *req)
1144{
1145 lockdep_assert_held(&prepare_lock);
1146
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001147 if (!core) {
1148 req->rate = 0;
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001149 return 0;
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001150 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001151
1152 clk_core_init_rate_req(core, req);
1153
1154 if (clk_core_can_round(core))
1155 return clk_core_determine_round_nolock(core, req);
1156 else if (core->flags & CLK_SET_RATE_PARENT)
1157 return clk_core_round_rate_nolock(core->parent, req);
1158
1159 req->rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001160 return 0;
1161}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001162
1163/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001164 * __clk_determine_rate - get the closest rate actually supported by a clock
1165 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001166 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001167 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001168 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001169 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001170int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001171{
Boris Brezillon0817b622015-07-07 20:48:08 +02001172 if (!hw) {
1173 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001174 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001175 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001176
Boris Brezillon0817b622015-07-07 20:48:08 +02001177 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001178}
1179EXPORT_SYMBOL_GPL(__clk_determine_rate);
1180
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001181unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1182{
1183 int ret;
1184 struct clk_rate_request req;
1185
1186 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1187 req.rate = rate;
1188
1189 ret = clk_core_round_rate_nolock(hw->core, &req);
1190 if (ret)
1191 return 0;
1192
1193 return req.rate;
1194}
1195EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1196
Mike Turquetteb24764902012-03-15 23:11:19 -07001197/**
1198 * clk_round_rate - round the given rate for a clk
1199 * @clk: the clk for which we are rounding a rate
1200 * @rate: the rate which is to be rounded
1201 *
1202 * Takes in a rate as input and rounds it to a rate that the clk can actually
1203 * use which is then returned. If clk doesn't support round_rate operation
1204 * then the parent rate is returned.
1205 */
1206long clk_round_rate(struct clk *clk, unsigned long rate)
1207{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001208 struct clk_rate_request req;
1209 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001210
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001211 if (!clk)
1212 return 0;
1213
Mike Turquetteeab89f62013-03-28 13:59:01 -07001214 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001215
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001216 if (clk->exclusive_count)
1217 clk_core_rate_unprotect(clk->core);
1218
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001219 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1220 req.rate = rate;
1221
1222 ret = clk_core_round_rate_nolock(clk->core, &req);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001223
1224 if (clk->exclusive_count)
1225 clk_core_rate_protect(clk->core);
1226
Mike Turquetteeab89f62013-03-28 13:59:01 -07001227 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001228
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001229 if (ret)
1230 return ret;
1231
1232 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001233}
1234EXPORT_SYMBOL_GPL(clk_round_rate);
1235
1236/**
1237 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001238 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001239 * @msg: clk notifier type (see include/linux/clk.h)
1240 * @old_rate: old clk rate
1241 * @new_rate: new clk rate
1242 *
1243 * Triggers a notifier call chain on the clk rate-change notification
1244 * for 'clk'. Passes a pointer to the struct clk and the previous
1245 * and current rates to the notifier callback. Intended to be called by
1246 * internal clock code only. Returns NOTIFY_DONE from the last driver
1247 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1248 * a driver returns that.
1249 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001250static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001251 unsigned long old_rate, unsigned long new_rate)
1252{
1253 struct clk_notifier *cn;
1254 struct clk_notifier_data cnd;
1255 int ret = NOTIFY_DONE;
1256
Mike Turquetteb24764902012-03-15 23:11:19 -07001257 cnd.old_rate = old_rate;
1258 cnd.new_rate = new_rate;
1259
1260 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001261 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001262 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001263 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1264 &cnd);
Peter De Schrijver17c34c52017-03-21 12:16:26 +02001265 if (ret & NOTIFY_STOP_MASK)
1266 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001267 }
1268 }
1269
1270 return ret;
1271}
1272
1273/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001274 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001275 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001276 *
1277 * Walks the subtree of clks starting with clk and recalculates accuracies as
1278 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001279 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001280 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001281 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001282static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001283{
1284 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001285 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001286
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001287 lockdep_assert_held(&prepare_lock);
1288
Stephen Boydd6968fc2015-04-30 13:54:13 -07001289 if (core->parent)
1290 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001291
Stephen Boydd6968fc2015-04-30 13:54:13 -07001292 if (core->ops->recalc_accuracy)
1293 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001294 parent_accuracy);
1295 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001296 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001297
Stephen Boydd6968fc2015-04-30 13:54:13 -07001298 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001299 __clk_recalc_accuracies(child);
1300}
1301
Stephen Boydd6968fc2015-04-30 13:54:13 -07001302static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001303{
1304 unsigned long accuracy;
1305
1306 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001307 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1308 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001309
Stephen Boydd6968fc2015-04-30 13:54:13 -07001310 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001311 clk_prepare_unlock();
1312
1313 return accuracy;
1314}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001315
1316/**
1317 * clk_get_accuracy - return the accuracy of clk
1318 * @clk: the clk whose accuracy is being returned
1319 *
1320 * Simply returns the cached accuracy of the clk, unless
1321 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1322 * issued.
1323 * If clk is NULL then returns 0.
1324 */
1325long clk_get_accuracy(struct clk *clk)
1326{
1327 if (!clk)
1328 return 0;
1329
1330 return clk_core_get_accuracy(clk->core);
1331}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001332EXPORT_SYMBOL_GPL(clk_get_accuracy);
1333
Stephen Boydd6968fc2015-04-30 13:54:13 -07001334static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001335 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001336{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001337 unsigned long rate = parent_rate;
1338
1339 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1340 rate = core->ops->recalc_rate(core->hw, parent_rate);
1341 clk_pm_runtime_put(core);
1342 }
1343 return rate;
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001344}
1345
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001346/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001347 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001348 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001349 * @msg: notification type (see include/linux/clk.h)
1350 *
1351 * Walks the subtree of clks starting with clk and recalculates rates as it
1352 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001353 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001354 *
1355 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1356 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001357 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001358static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001359{
1360 unsigned long old_rate;
1361 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001362 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001363
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001364 lockdep_assert_held(&prepare_lock);
1365
Stephen Boydd6968fc2015-04-30 13:54:13 -07001366 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001367
Stephen Boydd6968fc2015-04-30 13:54:13 -07001368 if (core->parent)
1369 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001370
Stephen Boydd6968fc2015-04-30 13:54:13 -07001371 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001372
1373 /*
1374 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1375 * & ABORT_RATE_CHANGE notifiers
1376 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001377 if (core->notifier_count && msg)
1378 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001379
Stephen Boydd6968fc2015-04-30 13:54:13 -07001380 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001381 __clk_recalc_rates(child, msg);
1382}
1383
Stephen Boydd6968fc2015-04-30 13:54:13 -07001384static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001385{
1386 unsigned long rate;
1387
1388 clk_prepare_lock();
1389
Stephen Boydd6968fc2015-04-30 13:54:13 -07001390 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1391 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001392
Stephen Boydd6968fc2015-04-30 13:54:13 -07001393 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001394 clk_prepare_unlock();
1395
1396 return rate;
1397}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001398
Mike Turquetteb24764902012-03-15 23:11:19 -07001399/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001400 * clk_get_rate - return the rate of clk
1401 * @clk: the clk whose rate is being returned
1402 *
1403 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1404 * is set, which means a recalc_rate will be issued.
1405 * If clk is NULL then returns 0.
1406 */
1407unsigned long clk_get_rate(struct clk *clk)
1408{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001409 if (!clk)
1410 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001411
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001412 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001413}
1414EXPORT_SYMBOL_GPL(clk_get_rate);
1415
Stephen Boydd6968fc2015-04-30 13:54:13 -07001416static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001417 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001418{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001419 int i;
James Hogan4935b222013-07-29 12:24:59 +01001420
Masahiro Yamada508f8842015-12-28 19:23:08 +09001421 if (!parent)
1422 return -EINVAL;
1423
Masahiro Yamada470b5e22015-12-28 19:23:09 +09001424 for (i = 0; i < core->num_parents; i++)
1425 if (clk_core_get_parent_by_index(core, i) == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001426 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001427
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001428 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001429}
1430
Heiko Stuebnere6500342015-04-22 22:53:05 +02001431/*
1432 * Update the orphan status of @core and all its children.
1433 */
1434static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1435{
1436 struct clk_core *child;
1437
1438 core->orphan = is_orphan;
1439
1440 hlist_for_each_entry(child, &core->children, child_node)
1441 clk_core_update_orphan_status(child, is_orphan);
1442}
1443
Stephen Boydd6968fc2015-04-30 13:54:13 -07001444static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001445{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001446 bool was_orphan = core->orphan;
1447
Stephen Boydd6968fc2015-04-30 13:54:13 -07001448 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001449
James Hogan903efc52013-08-29 12:10:51 +01001450 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001451 bool becomes_orphan = new_parent->orphan;
1452
James Hogan903efc52013-08-29 12:10:51 +01001453 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001454 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001455 new_parent->new_child = NULL;
1456
Stephen Boydd6968fc2015-04-30 13:54:13 -07001457 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001458
1459 if (was_orphan != becomes_orphan)
1460 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001461 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001462 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001463 if (!was_orphan)
1464 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001465 }
James Hogan4935b222013-07-29 12:24:59 +01001466
Stephen Boydd6968fc2015-04-30 13:54:13 -07001467 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001468}
1469
Stephen Boydd6968fc2015-04-30 13:54:13 -07001470static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001471 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001472{
1473 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001474 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001475
1476 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001477 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1478 *
1479 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001480 * clk_enable().
1481 *
1482 * If the clock is not prepared, then a race with
1483 * clk_enable/disable() is impossible since we already have the
1484 * prepare lock (future calls to clk_enable() need to be preceded by
1485 * a clk_prepare()).
1486 *
1487 * If the clock is prepared, migrate the prepared state to the new
1488 * parent and also protect against a race with clk_enable() by
1489 * forcing the clock and the new parent on. This ensures that all
1490 * future calls to clk_enable() are practically NOPs with respect to
1491 * hardware and software states.
1492 *
1493 * See also: Comment for clk_set_parent() below.
1494 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001495
1496 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1497 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1498 clk_core_prepare_enable(old_parent);
1499 clk_core_prepare_enable(parent);
1500 }
1501
1502 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001503 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001504 clk_core_prepare_enable(parent);
1505 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001506 }
1507
1508 /* update the clk tree topology */
1509 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001510 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001511 clk_enable_unlock(flags);
1512
Stephen Boyd3fa22522014-01-15 10:47:22 -08001513 return old_parent;
1514}
1515
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001516static void __clk_set_parent_after(struct clk_core *core,
1517 struct clk_core *parent,
1518 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001519{
1520 /*
1521 * Finish the migration of prepare state and undo the changes done
1522 * for preventing a race with clk_enable().
1523 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001524 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001525 clk_core_disable_lock(core);
1526 clk_core_disable_unprepare(old_parent);
1527 }
1528
1529 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1530 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1531 clk_core_disable_unprepare(parent);
1532 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001533 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001534}
1535
Stephen Boydd6968fc2015-04-30 13:54:13 -07001536static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001537 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001538{
1539 unsigned long flags;
1540 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001541 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001542
Stephen Boydd6968fc2015-04-30 13:54:13 -07001543 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001544
Stephen Boydd6968fc2015-04-30 13:54:13 -07001545 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001546
James Hogan4935b222013-07-29 12:24:59 +01001547 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001548 if (parent && core->ops->set_parent)
1549 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001550
Stephen Boydd6968fc2015-04-30 13:54:13 -07001551 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001552
James Hogan4935b222013-07-29 12:24:59 +01001553 if (ret) {
1554 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001555 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001556 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001557 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001558
James Hogan4935b222013-07-29 12:24:59 +01001559 return ret;
1560 }
1561
Stephen Boydd6968fc2015-04-30 13:54:13 -07001562 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001563
James Hogan4935b222013-07-29 12:24:59 +01001564 return 0;
1565}
1566
Ulf Hanssona093bde2012-08-31 14:21:28 +02001567/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001568 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001569 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001570 * @parent_rate: the "future" rate of clk's parent
1571 *
1572 * Walks the subtree of clks starting with clk, speculating rates as it
1573 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1574 *
1575 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1576 * pre-rate change notifications and returns early if no clks in the
1577 * subtree have subscribed to the notifications. Note that if a clk does not
1578 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001579 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001580 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001581static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001582 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001583{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001584 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001585 unsigned long new_rate;
1586 int ret = NOTIFY_DONE;
1587
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001588 lockdep_assert_held(&prepare_lock);
1589
Stephen Boydd6968fc2015-04-30 13:54:13 -07001590 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001591
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001592 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001593 if (core->notifier_count)
1594 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001595
Mike Turquette86bcfa22014-02-24 16:08:41 -08001596 if (ret & NOTIFY_STOP_MASK) {
1597 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001598 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001599 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001600 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001601
Stephen Boydd6968fc2015-04-30 13:54:13 -07001602 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001603 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001604 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001605 break;
1606 }
1607
1608out:
1609 return ret;
1610}
1611
Stephen Boydd6968fc2015-04-30 13:54:13 -07001612static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001613 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001614{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001615 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001616
Stephen Boydd6968fc2015-04-30 13:54:13 -07001617 core->new_rate = new_rate;
1618 core->new_parent = new_parent;
1619 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001620 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001621 core->new_child = NULL;
1622 if (new_parent && new_parent != core->parent)
1623 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001624
Stephen Boydd6968fc2015-04-30 13:54:13 -07001625 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001626 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001627 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001628 }
1629}
1630
1631/*
1632 * calculate the new rates returning the topmost clock that has to be
1633 * changed.
1634 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001635static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001636 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001637{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001638 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001639 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001640 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001641 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001642 unsigned long min_rate;
1643 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001644 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001645 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001646
Mike Turquette7452b212012-03-26 14:45:36 -07001647 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001648 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001649 return NULL;
1650
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001651 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001652 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001653 if (parent)
1654 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001655
Stephen Boydd6968fc2015-04-30 13:54:13 -07001656 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001657
James Hogan71472c02013-07-29 12:25:00 +01001658 /* find the closest rate and parent clk/rate */
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001659 if (clk_core_can_round(core)) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001660 struct clk_rate_request req;
1661
1662 req.rate = rate;
1663 req.min_rate = min_rate;
1664 req.max_rate = max_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001665
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001666 clk_core_init_rate_req(core, &req);
1667
1668 ret = clk_core_determine_round_nolock(core, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001669 if (ret < 0)
1670 return NULL;
1671
Boris Brezillon0817b622015-07-07 20:48:08 +02001672 best_parent_rate = req.best_parent_rate;
1673 new_rate = req.rate;
1674 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001675
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001676 if (new_rate < min_rate || new_rate > max_rate)
1677 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001678 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001679 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001680 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001681 return NULL;
1682 } else {
1683 /* pass-through clock with adjustable parent */
1684 top = clk_calc_new_rates(parent, rate);
1685 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001686 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001687 }
1688
James Hogan71472c02013-07-29 12:25:00 +01001689 /* some clocks must be gated to change parent */
1690 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001691 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001692 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001693 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001694 return NULL;
1695 }
1696
James Hogan71472c02013-07-29 12:25:00 +01001697 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001698 if (parent && core->num_parents > 1) {
1699 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001700 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001701 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001702 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001703 return NULL;
1704 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001705 }
1706
Stephen Boydd6968fc2015-04-30 13:54:13 -07001707 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001708 best_parent_rate != parent->rate)
1709 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001710
1711out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001712 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001713
1714 return top;
1715}
1716
1717/*
1718 * Notify about rate changes in a subtree. Always walk down the whole tree
1719 * so that in case of an error we can walk down the whole tree again and
1720 * abort the change.
1721 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001722static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001723 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001724{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001725 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001726 int ret = NOTIFY_DONE;
1727
Stephen Boydd6968fc2015-04-30 13:54:13 -07001728 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301729 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001730
Stephen Boydd6968fc2015-04-30 13:54:13 -07001731 if (core->notifier_count) {
1732 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001733 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001734 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001735 }
1736
Stephen Boydd6968fc2015-04-30 13:54:13 -07001737 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001738 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001739 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001740 continue;
1741 tmp_clk = clk_propagate_rate_change(child, event);
1742 if (tmp_clk)
1743 fail_clk = tmp_clk;
1744 }
1745
Stephen Boydd6968fc2015-04-30 13:54:13 -07001746 /* handle the new child who might not be in core->children yet */
1747 if (core->new_child) {
1748 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001749 if (tmp_clk)
1750 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001751 }
1752
1753 return fail_clk;
1754}
1755
1756/*
1757 * walk down a subtree and set the new rates notifying the rate
1758 * change on the way
1759 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001760static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001761{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001762 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001763 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001764 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001765 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001766 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001767 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001768 struct clk_core *parent = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001769
Stephen Boydd6968fc2015-04-30 13:54:13 -07001770 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001771
Dong Aishengfc8726a2016-06-30 17:31:14 +08001772 if (core->new_parent) {
1773 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001774 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001775 } else if (core->parent) {
1776 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001777 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001778 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001779
Marek Szyprowski588fb542017-11-30 13:14:51 +01001780 if (clk_pm_runtime_get(core))
1781 return;
1782
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001783 if (core->flags & CLK_SET_RATE_UNGATE) {
1784 unsigned long flags;
1785
1786 clk_core_prepare(core);
1787 flags = clk_enable_lock();
1788 clk_core_enable(core);
1789 clk_enable_unlock(flags);
1790 }
1791
Stephen Boydd6968fc2015-04-30 13:54:13 -07001792 if (core->new_parent && core->new_parent != core->parent) {
1793 old_parent = __clk_set_parent_before(core, core->new_parent);
1794 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001795
Stephen Boydd6968fc2015-04-30 13:54:13 -07001796 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001797 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001798 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001799 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001800 core->new_parent_index);
1801 } else if (core->ops->set_parent) {
1802 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001803 }
1804
Stephen Boydd6968fc2015-04-30 13:54:13 -07001805 trace_clk_set_parent_complete(core, core->new_parent);
1806 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001807 }
1808
Dong Aishengfc8726a2016-06-30 17:31:14 +08001809 if (core->flags & CLK_OPS_PARENT_ENABLE)
1810 clk_core_prepare_enable(parent);
1811
Stephen Boydd6968fc2015-04-30 13:54:13 -07001812 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001813
Stephen Boydd6968fc2015-04-30 13:54:13 -07001814 if (!skip_set_rate && core->ops->set_rate)
1815 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001816
Stephen Boydd6968fc2015-04-30 13:54:13 -07001817 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001818
Stephen Boydd6968fc2015-04-30 13:54:13 -07001819 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001820
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001821 if (core->flags & CLK_SET_RATE_UNGATE) {
1822 unsigned long flags;
1823
1824 flags = clk_enable_lock();
1825 clk_core_disable(core);
1826 clk_enable_unlock(flags);
1827 clk_core_unprepare(core);
1828 }
1829
Dong Aishengfc8726a2016-06-30 17:31:14 +08001830 if (core->flags & CLK_OPS_PARENT_ENABLE)
1831 clk_core_disable_unprepare(parent);
1832
Stephen Boydd6968fc2015-04-30 13:54:13 -07001833 if (core->notifier_count && old_rate != core->rate)
1834 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001835
Michael Turquette85e88fa2015-06-20 12:18:03 -07001836 if (core->flags & CLK_RECALC_NEW_RATES)
1837 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001838
Tero Kristo067bb172014-08-21 16:47:45 +03001839 /*
1840 * Use safe iteration, as change_rate can actually swap parents
1841 * for certain clock types.
1842 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001843 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001844 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001845 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001846 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001847 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001848 }
1849
Stephen Boydd6968fc2015-04-30 13:54:13 -07001850 /* handle the new child who might not be in core->children yet */
1851 if (core->new_child)
1852 clk_change_rate(core->new_child);
Marek Szyprowski588fb542017-11-30 13:14:51 +01001853
1854 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001855}
1856
Jerome Brunetca5e0892017-12-01 22:51:55 +01001857static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1858 unsigned long req_rate)
1859{
Jerome Brunete55a8392017-12-01 22:51:56 +01001860 int ret, cnt;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001861 struct clk_rate_request req;
1862
1863 lockdep_assert_held(&prepare_lock);
1864
1865 if (!core)
1866 return 0;
1867
Jerome Brunete55a8392017-12-01 22:51:56 +01001868 /* simulate what the rate would be if it could be freely set */
1869 cnt = clk_core_rate_nuke_protect(core);
1870 if (cnt < 0)
1871 return cnt;
1872
Jerome Brunetca5e0892017-12-01 22:51:55 +01001873 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1874 req.rate = req_rate;
1875
1876 ret = clk_core_round_rate_nolock(core, &req);
1877
Jerome Brunete55a8392017-12-01 22:51:56 +01001878 /* restore the protection */
1879 clk_core_rate_restore_protect(core, cnt);
1880
Jerome Brunetca5e0892017-12-01 22:51:55 +01001881 return ret ? 0 : req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001882}
1883
Stephen Boydd6968fc2015-04-30 13:54:13 -07001884static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001885 unsigned long req_rate)
1886{
1887 struct clk_core *top, *fail_clk;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001888 unsigned long rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001889 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001890
Stephen Boydd6968fc2015-04-30 13:54:13 -07001891 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001892 return 0;
1893
Jerome Brunetca5e0892017-12-01 22:51:55 +01001894 rate = clk_core_req_round_rate_nolock(core, req_rate);
1895
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001896 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001897 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001898 return 0;
1899
Jerome Brunete55a8392017-12-01 22:51:56 +01001900 /* fail on a direct rate set of a protected provider */
1901 if (clk_core_rate_is_protected(core))
1902 return -EBUSY;
1903
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001904 /* calculate new rates and get the topmost changed clock */
Jerome Brunetca5e0892017-12-01 22:51:55 +01001905 top = clk_calc_new_rates(core, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001906 if (!top)
1907 return -EINVAL;
1908
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001909 ret = clk_pm_runtime_get(core);
1910 if (ret)
1911 return ret;
1912
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001913 /* notify that we are about to change rates */
1914 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1915 if (fail_clk) {
1916 pr_debug("%s: failed to set %s rate\n", __func__,
1917 fail_clk->name);
1918 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001919 ret = -EBUSY;
1920 goto err;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001921 }
1922
1923 /* change the rates */
1924 clk_change_rate(top);
1925
Stephen Boydd6968fc2015-04-30 13:54:13 -07001926 core->req_rate = req_rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001927err:
1928 clk_pm_runtime_put(core);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001929
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001930 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001931}
1932
Mike Turquetteb24764902012-03-15 23:11:19 -07001933/**
1934 * clk_set_rate - specify a new rate for clk
1935 * @clk: the clk whose rate is being changed
1936 * @rate: the new rate for clk
1937 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001938 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001939 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001940 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1941 * propagate up to clk's parent; whether or not this happens depends on the
1942 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1943 * after calling .round_rate then upstream parent propagation is ignored. If
1944 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001945 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001946 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1947 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001948 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001949 * Rate changes are accomplished via tree traversal that also recalculates the
1950 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001951 *
1952 * Returns 0 on success, -EERROR otherwise.
1953 */
1954int clk_set_rate(struct clk *clk, unsigned long rate)
1955{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001956 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001957
Mike Turquette89ac8d72013-08-21 23:58:09 -07001958 if (!clk)
1959 return 0;
1960
Mike Turquetteb24764902012-03-15 23:11:19 -07001961 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001962 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001963
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001964 if (clk->exclusive_count)
1965 clk_core_rate_unprotect(clk->core);
1966
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001967 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001968
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001969 if (clk->exclusive_count)
1970 clk_core_rate_protect(clk->core);
1971
Mike Turquetteeab89f62013-03-28 13:59:01 -07001972 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001973
1974 return ret;
1975}
1976EXPORT_SYMBOL_GPL(clk_set_rate);
1977
1978/**
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001979 * clk_set_rate_exclusive - specify a new rate get exclusive control
1980 * @clk: the clk whose rate is being changed
1981 * @rate: the new rate for clk
1982 *
1983 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
1984 * within a critical section
1985 *
1986 * This can be used initially to ensure that at least 1 consumer is
1987 * statisfied when several consumers are competing for exclusivity over the
1988 * same clock provider.
1989 *
1990 * The exclusivity is not applied if setting the rate failed.
1991 *
1992 * Calls to clk_rate_exclusive_get() should be balanced with calls to
1993 * clk_rate_exclusive_put().
1994 *
1995 * Returns 0 on success, -EERROR otherwise.
1996 */
1997int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
1998{
1999 int ret;
2000
2001 if (!clk)
2002 return 0;
2003
2004 /* prevent racing with updates to the clock topology */
2005 clk_prepare_lock();
2006
2007 /*
2008 * The temporary protection removal is not here, on purpose
2009 * This function is meant to be used instead of clk_rate_protect,
2010 * so before the consumer code path protect the clock provider
2011 */
2012
2013 ret = clk_core_set_rate_nolock(clk->core, rate);
2014 if (!ret) {
2015 clk_core_rate_protect(clk->core);
2016 clk->exclusive_count++;
2017 }
2018
2019 clk_prepare_unlock();
2020
2021 return ret;
2022}
2023EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2024
2025/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002026 * clk_set_rate_range - set a rate range for a clock source
2027 * @clk: clock source
2028 * @min: desired minimum clock rate in Hz, inclusive
2029 * @max: desired maximum clock rate in Hz, inclusive
2030 *
2031 * Returns success (0) or negative errno.
2032 */
2033int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2034{
2035 int ret = 0;
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002036 unsigned long old_min, old_max, rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002037
2038 if (!clk)
2039 return 0;
2040
2041 if (min > max) {
2042 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2043 __func__, clk->core->name, clk->dev_id, clk->con_id,
2044 min, max);
2045 return -EINVAL;
2046 }
2047
2048 clk_prepare_lock();
2049
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002050 if (clk->exclusive_count)
2051 clk_core_rate_unprotect(clk->core);
2052
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002053 /* Save the current values in case we need to rollback the change */
2054 old_min = clk->min_rate;
2055 old_max = clk->max_rate;
2056 clk->min_rate = min;
2057 clk->max_rate = max;
2058
2059 rate = clk_core_get_rate_nolock(clk->core);
2060 if (rate < min || rate > max) {
2061 /*
2062 * FIXME:
2063 * We are in bit of trouble here, current rate is outside the
2064 * the requested range. We are going try to request appropriate
2065 * range boundary but there is a catch. It may fail for the
2066 * usual reason (clock broken, clock protected, etc) but also
2067 * because:
2068 * - round_rate() was not favorable and fell on the wrong
2069 * side of the boundary
2070 * - the determine_rate() callback does not really check for
2071 * this corner case when determining the rate
2072 */
2073
2074 if (rate < min)
2075 rate = min;
2076 else
2077 rate = max;
2078
2079 ret = clk_core_set_rate_nolock(clk->core, rate);
2080 if (ret) {
2081 /* rollback the changes */
2082 clk->min_rate = old_min;
2083 clk->max_rate = old_max;
2084 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002085 }
2086
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002087 if (clk->exclusive_count)
2088 clk_core_rate_protect(clk->core);
2089
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002090 clk_prepare_unlock();
2091
2092 return ret;
2093}
2094EXPORT_SYMBOL_GPL(clk_set_rate_range);
2095
2096/**
2097 * clk_set_min_rate - set a minimum clock rate for a clock source
2098 * @clk: clock source
2099 * @rate: desired minimum clock rate in Hz, inclusive
2100 *
2101 * Returns success (0) or negative errno.
2102 */
2103int clk_set_min_rate(struct clk *clk, unsigned long rate)
2104{
2105 if (!clk)
2106 return 0;
2107
2108 return clk_set_rate_range(clk, rate, clk->max_rate);
2109}
2110EXPORT_SYMBOL_GPL(clk_set_min_rate);
2111
2112/**
2113 * clk_set_max_rate - set a maximum clock rate for a clock source
2114 * @clk: clock source
2115 * @rate: desired maximum clock rate in Hz, inclusive
2116 *
2117 * Returns success (0) or negative errno.
2118 */
2119int clk_set_max_rate(struct clk *clk, unsigned long rate)
2120{
2121 if (!clk)
2122 return 0;
2123
2124 return clk_set_rate_range(clk, clk->min_rate, rate);
2125}
2126EXPORT_SYMBOL_GPL(clk_set_max_rate);
2127
2128/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002129 * clk_get_parent - return the parent of a clk
2130 * @clk: the clk whose parent gets returned
2131 *
2132 * Simply returns clk->parent. Returns NULL if clk is NULL.
2133 */
2134struct clk *clk_get_parent(struct clk *clk)
2135{
2136 struct clk *parent;
2137
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002138 if (!clk)
2139 return NULL;
2140
Mike Turquetteeab89f62013-03-28 13:59:01 -07002141 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002142 /* TODO: Create a per-user clk and change callers to call clk_put */
2143 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002144 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002145
2146 return parent;
2147}
2148EXPORT_SYMBOL_GPL(clk_get_parent);
2149
Stephen Boydd6968fc2015-04-30 13:54:13 -07002150static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002151{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002152 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002153
Masahiro Yamada2430a942016-02-09 20:19:14 +09002154 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002155 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002156
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002157 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002158}
2159
Stephen Boydd6968fc2015-04-30 13:54:13 -07002160static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002161 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002162{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002163 clk_reparent(core, new_parent);
2164 __clk_recalc_accuracies(core);
2165 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002166}
2167
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002168void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2169{
2170 if (!hw)
2171 return;
2172
2173 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2174}
2175
Mike Turquetteb24764902012-03-15 23:11:19 -07002176/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002177 * clk_has_parent - check if a clock is a possible parent for another
2178 * @clk: clock source
2179 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002180 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002181 * This function can be used in drivers that need to check that a clock can be
2182 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002183 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002184 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002185 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002186bool clk_has_parent(struct clk *clk, struct clk *parent)
2187{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002188 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002189
2190 /* NULL clocks should be nops, so return success if either is NULL. */
2191 if (!clk || !parent)
2192 return true;
2193
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002194 core = clk->core;
2195 parent_core = parent->core;
2196
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002197 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002198 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002199 return true;
2200
Yisheng Xied6347442018-05-31 19:11:14 +08002201 return match_string(core->parent_names, core->num_parents,
2202 parent_core->name) >= 0;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002203}
2204EXPORT_SYMBOL_GPL(clk_has_parent);
2205
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002206static int clk_core_set_parent_nolock(struct clk_core *core,
2207 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002208{
2209 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002210 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002211 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002212
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002213 lockdep_assert_held(&prepare_lock);
2214
Stephen Boydd6968fc2015-04-30 13:54:13 -07002215 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002216 return 0;
2217
Stephen Boydd6968fc2015-04-30 13:54:13 -07002218 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002219 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002220
Stephen Boydb61c43c2015-02-02 14:11:25 -08002221 /* verify ops for for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002222 if (core->num_parents > 1 && !core->ops->set_parent)
2223 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08002224
Ulf Hansson031dcc92013-04-02 23:09:38 +02002225 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002226 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2227 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002228
Jerome Brunete55a8392017-12-01 22:51:56 +01002229 if (clk_core_rate_is_protected(core))
2230 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002231
2232 /* try finding the new parent index */
2233 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002234 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002235 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002236 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002237 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002238 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002239 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002240 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002241 }
2242
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002243 ret = clk_pm_runtime_get(core);
2244 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002245 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002246
Mike Turquetteb24764902012-03-15 23:11:19 -07002247 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002248 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002249
2250 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002251 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002252 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07002253
Ulf Hansson031dcc92013-04-02 23:09:38 +02002254 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002255 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002256
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002257 /* propagate rate an accuracy recalculation accordingly */
2258 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002259 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002260 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002261 __clk_recalc_rates(core, POST_RATE_CHANGE);
2262 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002263 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002264
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002265runtime_put:
2266 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002267
2268 return ret;
2269}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002270
2271/**
2272 * clk_set_parent - switch the parent of a mux clk
2273 * @clk: the mux clk whose input we are switching
2274 * @parent: the new input to clk
2275 *
2276 * Re-parent clk to use parent as its new input source. If clk is in
2277 * prepared state, the clk will get enabled for the duration of this call. If
2278 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2279 * that, the reparenting is glitchy in hardware, etc), use the
2280 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2281 *
2282 * After successfully changing clk's parent clk_set_parent will update the
2283 * clk topology, sysfs topology and propagate rate recalculation via
2284 * __clk_recalc_rates.
2285 *
2286 * Returns 0 on success, -EERROR otherwise.
2287 */
2288int clk_set_parent(struct clk *clk, struct clk *parent)
2289{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002290 int ret;
2291
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002292 if (!clk)
2293 return 0;
2294
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002295 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002296
2297 if (clk->exclusive_count)
2298 clk_core_rate_unprotect(clk->core);
2299
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002300 ret = clk_core_set_parent_nolock(clk->core,
2301 parent ? parent->core : NULL);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002302
2303 if (clk->exclusive_count)
2304 clk_core_rate_protect(clk->core);
2305
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002306 clk_prepare_unlock();
2307
2308 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002309}
Mike Turquetteb24764902012-03-15 23:11:19 -07002310EXPORT_SYMBOL_GPL(clk_set_parent);
2311
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002312static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2313{
2314 int ret = -EINVAL;
2315
2316 lockdep_assert_held(&prepare_lock);
2317
2318 if (!core)
2319 return 0;
2320
Jerome Brunete55a8392017-12-01 22:51:56 +01002321 if (clk_core_rate_is_protected(core))
2322 return -EBUSY;
2323
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002324 trace_clk_set_phase(core, degrees);
2325
Shawn Lin7f95bee2018-03-08 14:49:41 +08002326 if (core->ops->set_phase) {
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002327 ret = core->ops->set_phase(core->hw, degrees);
Shawn Lin7f95bee2018-03-08 14:49:41 +08002328 if (!ret)
2329 core->phase = degrees;
2330 }
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002331
2332 trace_clk_set_phase_complete(core, degrees);
2333
2334 return ret;
2335}
2336
Mike Turquetteb24764902012-03-15 23:11:19 -07002337/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002338 * clk_set_phase - adjust the phase shift of a clock signal
2339 * @clk: clock signal source
2340 * @degrees: number of degrees the signal is shifted
2341 *
2342 * Shifts the phase of a clock signal by the specified
2343 * degrees. Returns 0 on success, -EERROR otherwise.
2344 *
2345 * This function makes no distinction about the input or reference
2346 * signal that we adjust the clock signal phase against. For example
2347 * phase locked-loop clock signal generators we may shift phase with
2348 * respect to feedback clock signal input, but for other cases the
2349 * clock phase may be shifted with respect to some other, unspecified
2350 * signal.
2351 *
2352 * Additionally the concept of phase shift does not propagate through
2353 * the clock tree hierarchy, which sets it apart from clock rates and
2354 * clock accuracy. A parent clock phase attribute does not have an
2355 * impact on the phase attribute of a child clock.
2356 */
2357int clk_set_phase(struct clk *clk, int degrees)
2358{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002359 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002360
2361 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002362 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002363
2364 /* sanity check degrees */
2365 degrees %= 360;
2366 if (degrees < 0)
2367 degrees += 360;
2368
2369 clk_prepare_lock();
2370
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002371 if (clk->exclusive_count)
2372 clk_core_rate_unprotect(clk->core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002373
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002374 ret = clk_core_set_phase_nolock(clk->core, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002375
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002376 if (clk->exclusive_count)
2377 clk_core_rate_protect(clk->core);
Mike Turquettee59c5372014-02-18 21:21:25 -08002378
Mike Turquettee59c5372014-02-18 21:21:25 -08002379 clk_prepare_unlock();
2380
Mike Turquettee59c5372014-02-18 21:21:25 -08002381 return ret;
2382}
Maxime Ripard9767b042015-01-20 22:23:43 +01002383EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002384
Stephen Boydd6968fc2015-04-30 13:54:13 -07002385static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002386{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002387 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002388
2389 clk_prepare_lock();
Shawn Lin1f9c63e2018-03-14 08:28:31 +08002390 /* Always try to update cached phase if possible */
2391 if (core->ops->get_phase)
2392 core->phase = core->ops->get_phase(core->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002393 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002394 clk_prepare_unlock();
2395
Mike Turquettee59c5372014-02-18 21:21:25 -08002396 return ret;
2397}
2398
2399/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002400 * clk_get_phase - return the phase shift of a clock signal
2401 * @clk: clock signal source
2402 *
2403 * Returns the phase shift of a clock node in degrees, otherwise returns
2404 * -EERROR.
2405 */
2406int clk_get_phase(struct clk *clk)
2407{
2408 if (!clk)
2409 return 0;
2410
2411 return clk_core_get_phase(clk->core);
2412}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002413EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002414
Jerome Brunet9fba7382018-06-19 16:41:41 +02002415static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2416{
2417 /* Assume a default value of 50% */
2418 core->duty.num = 1;
2419 core->duty.den = 2;
2420}
2421
2422static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2423
2424static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2425{
2426 struct clk_duty *duty = &core->duty;
2427 int ret = 0;
2428
2429 if (!core->ops->get_duty_cycle)
2430 return clk_core_update_duty_cycle_parent_nolock(core);
2431
2432 ret = core->ops->get_duty_cycle(core->hw, duty);
2433 if (ret)
2434 goto reset;
2435
2436 /* Don't trust the clock provider too much */
2437 if (duty->den == 0 || duty->num > duty->den) {
2438 ret = -EINVAL;
2439 goto reset;
2440 }
2441
2442 return 0;
2443
2444reset:
2445 clk_core_reset_duty_cycle_nolock(core);
2446 return ret;
2447}
2448
2449static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2450{
2451 int ret = 0;
2452
2453 if (core->parent &&
2454 core->flags & CLK_DUTY_CYCLE_PARENT) {
2455 ret = clk_core_update_duty_cycle_nolock(core->parent);
2456 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2457 } else {
2458 clk_core_reset_duty_cycle_nolock(core);
2459 }
2460
2461 return ret;
2462}
2463
2464static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2465 struct clk_duty *duty);
2466
2467static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2468 struct clk_duty *duty)
2469{
2470 int ret;
2471
2472 lockdep_assert_held(&prepare_lock);
2473
2474 if (clk_core_rate_is_protected(core))
2475 return -EBUSY;
2476
2477 trace_clk_set_duty_cycle(core, duty);
2478
2479 if (!core->ops->set_duty_cycle)
2480 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2481
2482 ret = core->ops->set_duty_cycle(core->hw, duty);
2483 if (!ret)
2484 memcpy(&core->duty, duty, sizeof(*duty));
2485
2486 trace_clk_set_duty_cycle_complete(core, duty);
2487
2488 return ret;
2489}
2490
2491static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2492 struct clk_duty *duty)
2493{
2494 int ret = 0;
2495
2496 if (core->parent &&
2497 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2498 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2499 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2500 }
2501
2502 return ret;
2503}
2504
2505/**
2506 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2507 * @clk: clock signal source
2508 * @num: numerator of the duty cycle ratio to be applied
2509 * @den: denominator of the duty cycle ratio to be applied
2510 *
2511 * Apply the duty cycle ratio if the ratio is valid and the clock can
2512 * perform this operation
2513 *
2514 * Returns (0) on success, a negative errno otherwise.
2515 */
2516int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2517{
2518 int ret;
2519 struct clk_duty duty;
2520
2521 if (!clk)
2522 return 0;
2523
2524 /* sanity check the ratio */
2525 if (den == 0 || num > den)
2526 return -EINVAL;
2527
2528 duty.num = num;
2529 duty.den = den;
2530
2531 clk_prepare_lock();
2532
2533 if (clk->exclusive_count)
2534 clk_core_rate_unprotect(clk->core);
2535
2536 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2537
2538 if (clk->exclusive_count)
2539 clk_core_rate_protect(clk->core);
2540
2541 clk_prepare_unlock();
2542
2543 return ret;
2544}
2545EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2546
2547static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2548 unsigned int scale)
2549{
2550 struct clk_duty *duty = &core->duty;
2551 int ret;
2552
2553 clk_prepare_lock();
2554
2555 ret = clk_core_update_duty_cycle_nolock(core);
2556 if (!ret)
2557 ret = mult_frac(scale, duty->num, duty->den);
2558
2559 clk_prepare_unlock();
2560
2561 return ret;
2562}
2563
2564/**
2565 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2566 * @clk: clock signal source
2567 * @scale: scaling factor to be applied to represent the ratio as an integer
2568 *
2569 * Returns the duty cycle ratio of a clock node multiplied by the provided
2570 * scaling factor, or negative errno on error.
2571 */
2572int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2573{
2574 if (!clk)
2575 return 0;
2576
2577 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2578}
2579EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2580
Mike Turquetteb24764902012-03-15 23:11:19 -07002581/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002582 * clk_is_match - check if two clk's point to the same hardware clock
2583 * @p: clk compared against q
2584 * @q: clk compared against p
2585 *
2586 * Returns true if the two struct clk pointers both point to the same hardware
2587 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2588 * share the same struct clk_core object.
2589 *
2590 * Returns false otherwise. Note that two NULL clks are treated as matching.
2591 */
2592bool clk_is_match(const struct clk *p, const struct clk *q)
2593{
2594 /* trivial case: identical struct clk's or both NULL */
2595 if (p == q)
2596 return true;
2597
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002598 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002599 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2600 if (p->core == q->core)
2601 return true;
2602
2603 return false;
2604}
2605EXPORT_SYMBOL_GPL(clk_is_match);
2606
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002607/*** debugfs support ***/
2608
2609#ifdef CONFIG_DEBUG_FS
2610#include <linux/debugfs.h>
2611
2612static struct dentry *rootdir;
2613static int inited = 0;
2614static DEFINE_MUTEX(clk_debug_lock);
2615static HLIST_HEAD(clk_debug_list);
2616
2617static struct hlist_head *all_lists[] = {
2618 &clk_root_list,
2619 &clk_orphan_list,
2620 NULL,
2621};
2622
2623static struct hlist_head *orphan_list[] = {
2624 &clk_orphan_list,
2625 NULL,
2626};
2627
2628static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2629 int level)
2630{
2631 if (!c)
2632 return;
2633
Jerome Brunet9fba7382018-06-19 16:41:41 +02002634 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002635 level * 3 + 1, "",
2636 30 - level * 3, c->name,
Jerome Brunete55a8392017-12-01 22:51:56 +01002637 c->enable_count, c->prepare_count, c->protect_count,
2638 clk_core_get_rate(c), clk_core_get_accuracy(c),
Jerome Brunet9fba7382018-06-19 16:41:41 +02002639 clk_core_get_phase(c),
2640 clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002641}
2642
2643static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2644 int level)
2645{
2646 struct clk_core *child;
2647
2648 if (!c)
2649 return;
2650
2651 clk_summary_show_one(s, c, level);
2652
2653 hlist_for_each_entry(child, &c->children, child_node)
2654 clk_summary_show_subtree(s, child, level + 1);
2655}
2656
2657static int clk_summary_show(struct seq_file *s, void *data)
2658{
2659 struct clk_core *c;
2660 struct hlist_head **lists = (struct hlist_head **)s->private;
2661
Jerome Brunet9fba7382018-06-19 16:41:41 +02002662 seq_puts(s, " enable prepare protect duty\n");
2663 seq_puts(s, " clock count count count rate accuracy phase cycle\n");
2664 seq_puts(s, "---------------------------------------------------------------------------------------------\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002665
2666 clk_prepare_lock();
2667
2668 for (; *lists; lists++)
2669 hlist_for_each_entry(c, *lists, child_node)
2670 clk_summary_show_subtree(s, c, 0);
2671
2672 clk_prepare_unlock();
2673
2674 return 0;
2675}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002676DEFINE_SHOW_ATTRIBUTE(clk_summary);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002677
2678static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2679{
2680 if (!c)
2681 return;
2682
Stefan Wahren7cb81132015-04-29 16:36:43 +00002683 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002684 seq_printf(s, "\"%s\": { ", c->name);
2685 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2686 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002687 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002688 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2689 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Lubomir Rintel71943c32019-01-04 23:05:49 +01002690 seq_printf(s, "\"phase\": %d,", clk_core_get_phase(c));
Jerome Brunet9fba7382018-06-19 16:41:41 +02002691 seq_printf(s, "\"duty_cycle\": %u",
2692 clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002693}
2694
2695static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2696{
2697 struct clk_core *child;
2698
2699 if (!c)
2700 return;
2701
2702 clk_dump_one(s, c, level);
2703
2704 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002705 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002706 clk_dump_subtree(s, child, level + 1);
2707 }
2708
Markus Elfring4d327582017-04-20 08:45:43 +02002709 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002710}
2711
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002712static int clk_dump_show(struct seq_file *s, void *data)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002713{
2714 struct clk_core *c;
2715 bool first_node = true;
2716 struct hlist_head **lists = (struct hlist_head **)s->private;
2717
Markus Elfring4d327582017-04-20 08:45:43 +02002718 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002719 clk_prepare_lock();
2720
2721 for (; *lists; lists++) {
2722 hlist_for_each_entry(c, *lists, child_node) {
2723 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02002724 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002725 first_node = false;
2726 clk_dump_subtree(s, c, 0);
2727 }
2728 }
2729
2730 clk_prepare_unlock();
2731
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002732 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002733 return 0;
2734}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002735DEFINE_SHOW_ATTRIBUTE(clk_dump);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002736
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002737static const struct {
2738 unsigned long flag;
2739 const char *name;
2740} clk_flags[] = {
Geert Uytterhoeven40dd71c2018-07-06 17:16:54 +02002741#define ENTRY(f) { f, #f }
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002742 ENTRY(CLK_SET_RATE_GATE),
2743 ENTRY(CLK_SET_PARENT_GATE),
2744 ENTRY(CLK_SET_RATE_PARENT),
2745 ENTRY(CLK_IGNORE_UNUSED),
2746 ENTRY(CLK_IS_BASIC),
2747 ENTRY(CLK_GET_RATE_NOCACHE),
2748 ENTRY(CLK_SET_RATE_NO_REPARENT),
2749 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2750 ENTRY(CLK_RECALC_NEW_RATES),
2751 ENTRY(CLK_SET_RATE_UNGATE),
2752 ENTRY(CLK_IS_CRITICAL),
2753 ENTRY(CLK_OPS_PARENT_ENABLE),
Jerome Brunet9fba7382018-06-19 16:41:41 +02002754 ENTRY(CLK_DUTY_CYCLE_PARENT),
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002755#undef ENTRY
2756};
2757
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002758static int clk_flags_show(struct seq_file *s, void *data)
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002759{
2760 struct clk_core *core = s->private;
2761 unsigned long flags = core->flags;
2762 unsigned int i;
2763
2764 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2765 if (flags & clk_flags[i].flag) {
2766 seq_printf(s, "%s\n", clk_flags[i].name);
2767 flags &= ~clk_flags[i].flag;
2768 }
2769 }
2770 if (flags) {
2771 /* Unknown flags */
2772 seq_printf(s, "0x%lx\n", flags);
2773 }
2774
2775 return 0;
2776}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002777DEFINE_SHOW_ATTRIBUTE(clk_flags);
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002778
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002779static int possible_parents_show(struct seq_file *s, void *data)
Peter De Schrijver92031572017-03-21 15:20:31 +02002780{
2781 struct clk_core *core = s->private;
2782 int i;
2783
2784 for (i = 0; i < core->num_parents - 1; i++)
2785 seq_printf(s, "%s ", core->parent_names[i]);
2786
2787 seq_printf(s, "%s\n", core->parent_names[i]);
2788
2789 return 0;
2790}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002791DEFINE_SHOW_ATTRIBUTE(possible_parents);
Peter De Schrijver92031572017-03-21 15:20:31 +02002792
Jerome Brunet9fba7382018-06-19 16:41:41 +02002793static int clk_duty_cycle_show(struct seq_file *s, void *data)
2794{
2795 struct clk_core *core = s->private;
2796 struct clk_duty *duty = &core->duty;
2797
2798 seq_printf(s, "%u/%u\n", duty->num, duty->den);
2799
2800 return 0;
2801}
2802DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
2803
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002804static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002805{
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002806 struct dentry *root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002807
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002808 if (!core || !pdentry)
2809 return;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002810
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002811 root = debugfs_create_dir(core->name, pdentry);
2812 core->dentry = root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002813
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002814 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
2815 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
2816 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
2817 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
2818 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
2819 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
2820 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
2821 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
Jerome Brunet9fba7382018-06-19 16:41:41 +02002822 debugfs_create_file("clk_duty_cycle", 0444, root, core,
2823 &clk_duty_cycle_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002824
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002825 if (core->num_parents > 1)
2826 debugfs_create_file("clk_possible_parents", 0444, root, core,
2827 &possible_parents_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002828
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002829 if (core->ops->debug_init)
2830 core->ops->debug_init(core->hw, core->dentry);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002831}
2832
2833/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002834 * clk_debug_register - add a clk node to the debugfs clk directory
2835 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002836 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002837 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2838 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002839 * will be created lazily by clk_debug_init as part of a late_initcall.
2840 */
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002841static void clk_debug_register(struct clk_core *core)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002842{
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002843 mutex_lock(&clk_debug_lock);
2844 hlist_add_head(&core->debug_node, &clk_debug_list);
Stephen Boyddb3188f2018-01-03 16:44:37 -08002845 if (inited)
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002846 clk_debug_create_one(core, rootdir);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002847 mutex_unlock(&clk_debug_lock);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002848}
2849
2850 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002851 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2852 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002853 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002854 * Dynamically removes a clk and all its child nodes from the
2855 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08002856 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002857 */
2858static void clk_debug_unregister(struct clk_core *core)
2859{
2860 mutex_lock(&clk_debug_lock);
2861 hlist_del_init(&core->debug_node);
2862 debugfs_remove_recursive(core->dentry);
2863 core->dentry = NULL;
2864 mutex_unlock(&clk_debug_lock);
2865}
2866
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002867/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002868 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002869 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002870 * clks are often initialized very early during boot before memory can be
2871 * dynamically allocated and well before debugfs is setup. This function
2872 * populates the debugfs clk directory once at boot-time when we know that
2873 * debugfs is setup. It should only be called once at boot-time, all other clks
2874 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002875 */
2876static int __init clk_debug_init(void)
2877{
2878 struct clk_core *core;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002879
2880 rootdir = debugfs_create_dir("clk", NULL);
2881
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002882 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
2883 &clk_summary_fops);
2884 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
2885 &clk_dump_fops);
2886 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
2887 &clk_summary_fops);
2888 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
2889 &clk_dump_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002890
2891 mutex_lock(&clk_debug_lock);
2892 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2893 clk_debug_create_one(core, rootdir);
2894
2895 inited = 1;
2896 mutex_unlock(&clk_debug_lock);
2897
2898 return 0;
2899}
2900late_initcall(clk_debug_init);
2901#else
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02002902static inline void clk_debug_register(struct clk_core *core) { }
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002903static inline void clk_debug_reparent(struct clk_core *core,
2904 struct clk_core *new_parent)
2905{
2906}
2907static inline void clk_debug_unregister(struct clk_core *core)
2908{
2909}
2910#endif
2911
Michael Turquette3d3801e2015-02-25 09:11:01 -08002912/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002913 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002914 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07002915 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002916 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002917 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002918 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002919static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002920{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002921 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002922 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002923 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002924 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002925
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002926 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07002927 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002928
Mike Turquetteeab89f62013-03-28 13:59:01 -07002929 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002930
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002931 ret = clk_pm_runtime_get(core);
2932 if (ret)
2933 goto unlock;
2934
Mike Turquetteb24764902012-03-15 23:11:19 -07002935 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002936 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002937 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002938 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002939 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002940 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002941 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002942
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03002943 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002944 if (core->ops->set_rate &&
2945 !((core->ops->round_rate || core->ops->determine_rate) &&
2946 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002947 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2948 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002949 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002950 goto out;
2951 }
2952
Stephen Boydd6968fc2015-04-30 13:54:13 -07002953 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002954 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2955 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002956 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002957 goto out;
2958 }
2959
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09002960 if (core->num_parents > 1 && !core->ops->get_parent) {
2961 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2962 __func__, core->name);
2963 ret = -EINVAL;
2964 goto out;
2965 }
2966
Stephen Boydd6968fc2015-04-30 13:54:13 -07002967 if (core->ops->set_rate_and_parent &&
2968 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002969 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002970 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002971 ret = -EINVAL;
2972 goto out;
2973 }
2974
Mike Turquetteb24764902012-03-15 23:11:19 -07002975 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002976 for (i = 0; i < core->num_parents; i++)
2977 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002978 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002979 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002980
Stephen Boydd6968fc2015-04-30 13:54:13 -07002981 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002982
2983 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08002984 * Populate core->parent if parent has already been clk_core_init'd. If
2985 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002986 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07002987 * clk list.
2988 *
2989 * Every time a new clk is clk_init'd then we walk the list of orphan
2990 * clocks and re-parent any that are children of the clock currently
2991 * being clk_init'd.
2992 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02002993 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002994 hlist_add_head(&core->child_node,
2995 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002996 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002997 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002998 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002999 core->orphan = false;
3000 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003001 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003002 core->orphan = true;
3003 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003004
3005 /*
Jerome Brunet541deba2018-02-14 14:43:37 +01003006 * optional platform-specific magic
3007 *
3008 * The .init callback is not used by any of the basic clock types, but
3009 * exists for weird hardware that must perform initialization magic.
3010 * Please consider other ways of solving initialization problems before
3011 * using this callback, as its use is discouraged.
3012 */
3013 if (core->ops->init)
3014 core->ops->init(core->hw);
3015
3016 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003017 * Set clk's accuracy. The preferred method is to use
3018 * .recalc_accuracy. For simple clocks and lazy developers the default
3019 * fallback is to use the parent's accuracy. If a clock doesn't have a
3020 * parent (or is orphaned) then accuracy is set to zero (perfect
3021 * clock).
3022 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003023 if (core->ops->recalc_accuracy)
3024 core->accuracy = core->ops->recalc_accuracy(core->hw,
3025 __clk_get_accuracy(core->parent));
3026 else if (core->parent)
3027 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003028 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003029 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003030
3031 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02003032 * Set clk's phase.
3033 * Since a phase is by definition relative to its parent, just
3034 * query the current clock phase, or just assume it's in phase.
3035 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003036 if (core->ops->get_phase)
3037 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02003038 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003039 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02003040
3041 /*
Jerome Brunet9fba7382018-06-19 16:41:41 +02003042 * Set clk's duty cycle.
3043 */
3044 clk_core_update_duty_cycle_nolock(core);
3045
3046 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07003047 * Set clk's rate. The preferred method is to use .recalc_rate. For
3048 * simple clocks and lazy developers the default fallback is to use the
3049 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3050 * then rate is set to zero.
3051 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003052 if (core->ops->recalc_rate)
3053 rate = core->ops->recalc_rate(core->hw,
3054 clk_core_get_rate_nolock(core->parent));
3055 else if (core->parent)
3056 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003057 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003058 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003059 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003060
3061 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003062 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3063 * don't get accidentally disabled when walking the orphan tree and
3064 * reparenting clocks
3065 */
3066 if (core->flags & CLK_IS_CRITICAL) {
3067 unsigned long flags;
3068
Guenter Roeck5f047e32019-12-25 08:34:29 -08003069 ret = clk_core_prepare(core);
3070 if (ret)
3071 goto out;
Jerome Brunet99652a42018-02-14 14:43:36 +01003072
3073 flags = clk_enable_lock();
Guenter Roeck5f047e32019-12-25 08:34:29 -08003074 ret = clk_core_enable(core);
Jerome Brunet99652a42018-02-14 14:43:36 +01003075 clk_enable_unlock(flags);
Guenter Roeck5f047e32019-12-25 08:34:29 -08003076 if (ret) {
3077 clk_core_unprepare(core);
3078 goto out;
3079 }
Jerome Brunet99652a42018-02-14 14:43:36 +01003080 }
3081
3082 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003083 * walk the list of orphan clocks and reparent any that newly finds a
3084 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07003085 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08003086 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003087 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01003088
Michael Turquette904e6ea2016-07-08 16:32:10 -07003089 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003090 * We need to use __clk_set_parent_before() and _after() to
3091 * to properly migrate any prepare/enable count of the orphan
3092 * clock. This is important for CLK_IS_CRITICAL clocks, which
3093 * are enabled during init but might not have a parent yet.
Michael Turquette904e6ea2016-07-08 16:32:10 -07003094 */
3095 if (parent) {
Stephen Boydf8f8f1d2017-11-02 00:36:09 -07003096 /* update the clk tree topology */
Jerome Brunet99652a42018-02-14 14:43:36 +01003097 __clk_set_parent_before(orphan, parent);
3098 __clk_set_parent_after(orphan, parent, NULL);
Michael Turquette904e6ea2016-07-08 16:32:10 -07003099 __clk_recalc_accuracies(orphan);
3100 __clk_recalc_rates(orphan, 0);
3101 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003102 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003103
Stephen Boydd6968fc2015-04-30 13:54:13 -07003104 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07003105out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003106 clk_pm_runtime_put(core);
3107unlock:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003108 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003109
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003110 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003111 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003112
Mike Turquetted1302a32012-03-29 14:30:40 -07003113 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07003114}
3115
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003116struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
3117 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003118{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003119 struct clk *clk;
3120
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003121 /* This is to allow this function to be chained to others */
Masahiro Yamadac1de1352015-11-20 14:38:49 +09003122 if (IS_ERR_OR_NULL(hw))
Masahiro Yamada8a231332016-07-19 16:28:47 +09003123 return ERR_CAST(hw);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003124
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003125 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3126 if (!clk)
3127 return ERR_PTR(-ENOMEM);
3128
3129 clk->core = hw->core;
3130 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02003131 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003132 clk->max_rate = ULONG_MAX;
3133
3134 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08003135 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003136 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003137
3138 return clk;
3139}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003140
Mikko Perttunen365f7a82018-07-11 11:21:04 +03003141/* keep in sync with __clk_put */
Stephen Boyd73e0e492015-02-06 11:42:43 -08003142void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003143{
3144 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08003145 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003146 clk_prepare_unlock();
3147
Leonard Crestez253160a2017-02-20 15:20:56 +02003148 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003149 kfree(clk);
3150}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003151
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003152/**
3153 * clk_register - allocate a new clock, register it and return an opaque cookie
3154 * @dev: device that is registering this clock
3155 * @hw: link to hardware-specific clock data
3156 *
3157 * clk_register is the primary interface for populating the clock tree with new
3158 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05303159 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003160 * rest of the clock API. In the event of an error clk_register will return an
3161 * error code; drivers must test for an error code after calling clk_register.
3162 */
3163struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07003164{
Mike Turquetted1302a32012-03-29 14:30:40 -07003165 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003166 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003167
Stephen Boydd6968fc2015-04-30 13:54:13 -07003168 core = kzalloc(sizeof(*core), GFP_KERNEL);
3169 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003170 ret = -ENOMEM;
3171 goto fail_out;
3172 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003173
Stephen Boydd6968fc2015-04-30 13:54:13 -07003174 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3175 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003176 ret = -ENOMEM;
3177 goto fail_name;
3178 }
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003179
3180 if (WARN_ON(!hw->init->ops)) {
3181 ret = -EINVAL;
3182 goto fail_ops;
3183 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07003184 core->ops = hw->init->ops;
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003185
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003186 if (dev && pm_runtime_enabled(dev))
3187 core->dev = dev;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003188 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003189 core->owner = dev->driver->owner;
3190 core->hw = hw;
3191 core->flags = hw->init->flags;
3192 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003193 core->min_rate = 0;
3194 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003195 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003196
Mike Turquetted1302a32012-03-29 14:30:40 -07003197 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003198 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02003199 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003200
Stephen Boydd6968fc2015-04-30 13:54:13 -07003201 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003202 ret = -ENOMEM;
3203 goto fail_parent_names;
3204 }
3205
3206
3207 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003208 for (i = 0; i < core->num_parents; i++) {
3209 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003210 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003211 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003212 ret = -ENOMEM;
3213 goto fail_parent_names_copy;
3214 }
3215 }
3216
Masahiro Yamada176d1162015-12-28 19:23:00 +09003217 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3218 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3219 GFP_KERNEL);
3220 if (!core->parents) {
3221 ret = -ENOMEM;
3222 goto fail_parents;
3223 };
3224
Stephen Boydd6968fc2015-04-30 13:54:13 -07003225 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003226
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003227 hw->clk = __clk_create_clk(hw, NULL, NULL);
3228 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003229 ret = PTR_ERR(hw->clk);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003230 goto fail_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003231 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003232
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003233 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003234 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003235 return hw->clk;
3236
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003237 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003238 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003239
Masahiro Yamada176d1162015-12-28 19:23:00 +09003240fail_parents:
3241 kfree(core->parents);
Mike Turquetted1302a32012-03-29 14:30:40 -07003242fail_parent_names_copy:
3243 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003244 kfree_const(core->parent_names[i]);
3245 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07003246fail_parent_names:
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003247fail_ops:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003248 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003249fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003250 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003251fail_out:
3252 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003253}
3254EXPORT_SYMBOL_GPL(clk_register);
3255
Stephen Boyd41438042016-02-05 17:02:52 -08003256/**
3257 * clk_hw_register - register a clk_hw and return an error code
3258 * @dev: device that is registering this clock
3259 * @hw: link to hardware-specific clock data
3260 *
3261 * clk_hw_register is the primary interface for populating the clock tree with
3262 * new clock nodes. It returns an integer equal to zero indicating success or
3263 * less than zero indicating failure. Drivers must test for an error code after
3264 * calling clk_hw_register().
3265 */
3266int clk_hw_register(struct device *dev, struct clk_hw *hw)
3267{
3268 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3269}
3270EXPORT_SYMBOL_GPL(clk_hw_register);
3271
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003272/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003273static void __clk_release(struct kref *ref)
3274{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003275 struct clk_core *core = container_of(ref, struct clk_core, ref);
3276 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003277
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003278 lockdep_assert_held(&prepare_lock);
3279
Stephen Boydd6968fc2015-04-30 13:54:13 -07003280 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003281 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003282 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003283
Stephen Boydd6968fc2015-04-30 13:54:13 -07003284 kfree(core->parent_names);
3285 kfree_const(core->name);
3286 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003287}
3288
3289/*
3290 * Empty clk_ops for unregistered clocks. These are used temporarily
3291 * after clk_unregister() was called on a clock and until last clock
3292 * consumer calls clk_put() and the struct clk object is freed.
3293 */
3294static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3295{
3296 return -ENXIO;
3297}
3298
3299static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3300{
3301 WARN_ON_ONCE(1);
3302}
3303
3304static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3305 unsigned long parent_rate)
3306{
3307 return -ENXIO;
3308}
3309
3310static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3311{
3312 return -ENXIO;
3313}
3314
3315static const struct clk_ops clk_nodrv_ops = {
3316 .enable = clk_nodrv_prepare_enable,
3317 .disable = clk_nodrv_disable_unprepare,
3318 .prepare = clk_nodrv_prepare_enable,
3319 .unprepare = clk_nodrv_disable_unprepare,
3320 .set_rate = clk_nodrv_set_rate,
3321 .set_parent = clk_nodrv_set_parent,
3322};
3323
Mark Brown1df5c932012-04-18 09:07:12 +01003324/**
3325 * clk_unregister - unregister a currently registered clock
3326 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003327 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003328void clk_unregister(struct clk *clk)
3329{
3330 unsigned long flags;
3331
Stephen Boyd6314b672014-09-04 23:37:49 -07003332 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3333 return;
3334
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003335 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003336
3337 clk_prepare_lock();
3338
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003339 if (clk->core->ops == &clk_nodrv_ops) {
3340 pr_err("%s: unregistered clock: %s\n", __func__,
3341 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003342 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003343 }
3344 /*
3345 * Assign empty clock ops for consumers that might still hold
3346 * a reference to this clock.
3347 */
3348 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003349 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003350 clk_enable_unlock(flags);
3351
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003352 if (!hlist_empty(&clk->core->children)) {
3353 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003354 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003355
3356 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003357 hlist_for_each_entry_safe(child, t, &clk->core->children,
3358 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01003359 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003360 }
3361
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003362 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003363
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003364 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003365 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003366 __func__, clk->core->name);
Jerome Brunete55a8392017-12-01 22:51:56 +01003367
3368 if (clk->core->protect_count)
3369 pr_warn("%s: unregistering protected clock: %s\n",
3370 __func__, clk->core->name);
3371
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003372 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003373unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003374 clk_prepare_unlock();
3375}
Mark Brown1df5c932012-04-18 09:07:12 +01003376EXPORT_SYMBOL_GPL(clk_unregister);
3377
Stephen Boyd41438042016-02-05 17:02:52 -08003378/**
3379 * clk_hw_unregister - unregister a currently registered clk_hw
3380 * @hw: hardware-specific clock data to unregister
3381 */
3382void clk_hw_unregister(struct clk_hw *hw)
3383{
3384 clk_unregister(hw->clk);
3385}
3386EXPORT_SYMBOL_GPL(clk_hw_unregister);
3387
Stephen Boyd46c87732012-09-24 13:38:04 -07003388static void devm_clk_release(struct device *dev, void *res)
3389{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003390 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003391}
3392
Stephen Boyd41438042016-02-05 17:02:52 -08003393static void devm_clk_hw_release(struct device *dev, void *res)
3394{
3395 clk_hw_unregister(*(struct clk_hw **)res);
3396}
3397
Stephen Boyd46c87732012-09-24 13:38:04 -07003398/**
3399 * devm_clk_register - resource managed clk_register()
3400 * @dev: device that is registering this clock
3401 * @hw: link to hardware-specific clock data
3402 *
3403 * Managed clk_register(). Clocks returned from this function are
3404 * automatically clk_unregister()ed on driver detach. See clk_register() for
3405 * more information.
3406 */
3407struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3408{
3409 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003410 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003411
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003412 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3413 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003414 return ERR_PTR(-ENOMEM);
3415
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003416 clk = clk_register(dev, hw);
3417 if (!IS_ERR(clk)) {
3418 *clkp = clk;
3419 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003420 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003421 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003422 }
3423
3424 return clk;
3425}
3426EXPORT_SYMBOL_GPL(devm_clk_register);
3427
Stephen Boyd41438042016-02-05 17:02:52 -08003428/**
3429 * devm_clk_hw_register - resource managed clk_hw_register()
3430 * @dev: device that is registering this clock
3431 * @hw: link to hardware-specific clock data
3432 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003433 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003434 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3435 * for more information.
3436 */
3437int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3438{
3439 struct clk_hw **hwp;
3440 int ret;
3441
3442 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3443 if (!hwp)
3444 return -ENOMEM;
3445
3446 ret = clk_hw_register(dev, hw);
3447 if (!ret) {
3448 *hwp = hw;
3449 devres_add(dev, hwp);
3450 } else {
3451 devres_free(hwp);
3452 }
3453
3454 return ret;
3455}
3456EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3457
Stephen Boyd46c87732012-09-24 13:38:04 -07003458static int devm_clk_match(struct device *dev, void *res, void *data)
3459{
3460 struct clk *c = res;
3461 if (WARN_ON(!c))
3462 return 0;
3463 return c == data;
3464}
3465
Stephen Boyd41438042016-02-05 17:02:52 -08003466static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3467{
3468 struct clk_hw *hw = res;
3469
3470 if (WARN_ON(!hw))
3471 return 0;
3472 return hw == data;
3473}
3474
Stephen Boyd46c87732012-09-24 13:38:04 -07003475/**
3476 * devm_clk_unregister - resource managed clk_unregister()
3477 * @clk: clock to unregister
3478 *
3479 * Deallocate a clock allocated with devm_clk_register(). Normally
3480 * this function will not need to be called and the resource management
3481 * code will ensure that the resource is freed.
3482 */
3483void devm_clk_unregister(struct device *dev, struct clk *clk)
3484{
3485 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3486}
3487EXPORT_SYMBOL_GPL(devm_clk_unregister);
3488
Stephen Boyd41438042016-02-05 17:02:52 -08003489/**
3490 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3491 * @dev: device that is unregistering the hardware-specific clock data
3492 * @hw: link to hardware-specific clock data
3493 *
3494 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3495 * this function will not need to be called and the resource management
3496 * code will ensure that the resource is freed.
3497 */
3498void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3499{
3500 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3501 hw));
3502}
3503EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3504
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003505/*
3506 * clkdev helpers
3507 */
3508int __clk_get(struct clk *clk)
3509{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003510 struct clk_core *core = !clk ? NULL : clk->core;
3511
3512 if (core) {
3513 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003514 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003515
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003516 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003517 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003518 return 1;
3519}
3520
Mikko Perttunen365f7a82018-07-11 11:21:04 +03003521/* keep in sync with __clk_free_clk */
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003522void __clk_put(struct clk *clk)
3523{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003524 struct module *owner;
3525
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003526 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003527 return;
3528
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003529 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003530
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01003531 /*
3532 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3533 * given user should be balanced with calls to clk_rate_exclusive_put()
3534 * and by that same consumer
3535 */
3536 if (WARN_ON(clk->exclusive_count)) {
3537 /* We voiced our concern, let's sanitize the situation */
3538 clk->core->protect_count -= (clk->exclusive_count - 1);
3539 clk_core_rate_unprotect(clk->core);
3540 clk->exclusive_count = 0;
3541 }
3542
Stephen Boyd50595f82015-02-06 11:42:44 -08003543 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003544 if (clk->min_rate > clk->core->req_rate ||
3545 clk->max_rate < clk->core->req_rate)
3546 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3547
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003548 owner = clk->core->owner;
3549 kref_put(&clk->core->ref, __clk_release);
3550
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003551 clk_prepare_unlock();
3552
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003553 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003554
Mikko Perttunen365f7a82018-07-11 11:21:04 +03003555 kfree_const(clk->con_id);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003556 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003557}
3558
Mike Turquetteb24764902012-03-15 23:11:19 -07003559/*** clk rate change notifiers ***/
3560
3561/**
3562 * clk_notifier_register - add a clk rate change notifier
3563 * @clk: struct clk * to watch
3564 * @nb: struct notifier_block * with callback info
3565 *
3566 * Request notification when clk's rate changes. This uses an SRCU
3567 * notifier because we want it to block and notifier unregistrations are
3568 * uncommon. The callbacks associated with the notifier must not
3569 * re-enter into the clk framework by calling any top-level clk APIs;
3570 * this will cause a nested prepare_lock mutex.
3571 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003572 * In all notification cases (pre, post and abort rate change) the original
3573 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3574 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003575 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003576 * clk_notifier_register() must be called from non-atomic context.
3577 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3578 * allocation failure; otherwise, passes along the return value of
3579 * srcu_notifier_chain_register().
3580 */
3581int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3582{
3583 struct clk_notifier *cn;
3584 int ret = -ENOMEM;
3585
3586 if (!clk || !nb)
3587 return -EINVAL;
3588
Mike Turquetteeab89f62013-03-28 13:59:01 -07003589 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003590
3591 /* search the list of notifiers for this clk */
3592 list_for_each_entry(cn, &clk_notifier_list, node)
3593 if (cn->clk == clk)
3594 break;
3595
3596 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3597 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02003598 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003599 if (!cn)
3600 goto out;
3601
3602 cn->clk = clk;
3603 srcu_init_notifier_head(&cn->notifier_head);
3604
3605 list_add(&cn->node, &clk_notifier_list);
3606 }
3607
3608 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3609
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003610 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003611
3612out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003613 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003614
3615 return ret;
3616}
3617EXPORT_SYMBOL_GPL(clk_notifier_register);
3618
3619/**
3620 * clk_notifier_unregister - remove a clk rate change notifier
3621 * @clk: struct clk *
3622 * @nb: struct notifier_block * with callback info
3623 *
3624 * Request no further notification for changes to 'clk' and frees memory
3625 * allocated in clk_notifier_register.
3626 *
3627 * Returns -EINVAL if called with null arguments; otherwise, passes
3628 * along the return value of srcu_notifier_chain_unregister().
3629 */
3630int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3631{
3632 struct clk_notifier *cn = NULL;
3633 int ret = -EINVAL;
3634
3635 if (!clk || !nb)
3636 return -EINVAL;
3637
Mike Turquetteeab89f62013-03-28 13:59:01 -07003638 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003639
3640 list_for_each_entry(cn, &clk_notifier_list, node)
3641 if (cn->clk == clk)
3642 break;
3643
3644 if (cn->clk == clk) {
3645 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3646
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003647 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07003648
3649 /* XXX the notifier code should handle this better */
3650 if (!cn->notifier_head.head) {
3651 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08003652 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07003653 kfree(cn);
3654 }
3655
3656 } else {
3657 ret = -ENOENT;
3658 }
3659
Mike Turquetteeab89f62013-03-28 13:59:01 -07003660 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003661
3662 return ret;
3663}
3664EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05003665
3666#ifdef CONFIG_OF
3667/**
3668 * struct of_clk_provider - Clock provider registration structure
3669 * @link: Entry in global list of clock providers
3670 * @node: Pointer to device tree node of clock provider
3671 * @get: Get clock callback. Returns NULL or a struct clk for the
3672 * given clock specifier
3673 * @data: context pointer to be passed into @get callback
3674 */
3675struct of_clk_provider {
3676 struct list_head link;
3677
3678 struct device_node *node;
3679 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003680 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05003681 void *data;
3682};
3683
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303684static const struct of_device_id __clk_of_table_sentinel
3685 __used __section(__clk_of_table_end);
3686
Grant Likely766e6a42012-04-09 14:50:06 -05003687static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003688static DEFINE_MUTEX(of_clk_mutex);
3689
Grant Likely766e6a42012-04-09 14:50:06 -05003690struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3691 void *data)
3692{
3693 return data;
3694}
3695EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3696
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003697struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3698{
3699 return data;
3700}
3701EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3702
Shawn Guo494bfec2012-08-22 21:36:27 +08003703struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3704{
3705 struct clk_onecell_data *clk_data = data;
3706 unsigned int idx = clkspec->args[0];
3707
3708 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02003709 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08003710 return ERR_PTR(-EINVAL);
3711 }
3712
3713 return clk_data->clks[idx];
3714}
3715EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3716
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003717struct clk_hw *
3718of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3719{
3720 struct clk_hw_onecell_data *hw_data = data;
3721 unsigned int idx = clkspec->args[0];
3722
3723 if (idx >= hw_data->num) {
3724 pr_err("%s: invalid index %u\n", __func__, idx);
3725 return ERR_PTR(-EINVAL);
3726 }
3727
3728 return hw_data->hws[idx];
3729}
3730EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3731
Grant Likely766e6a42012-04-09 14:50:06 -05003732/**
3733 * of_clk_add_provider() - Register a clock provider for a node
3734 * @np: Device node pointer associated with clock provider
3735 * @clk_src_get: callback for decoding clock
3736 * @data: context pointer for @clk_src_get callback.
3737 */
3738int of_clk_add_provider(struct device_node *np,
3739 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3740 void *data),
3741 void *data)
3742{
3743 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003744 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003745
Markus Elfring1808a322017-04-20 09:30:52 +02003746 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05003747 if (!cp)
3748 return -ENOMEM;
3749
3750 cp->node = of_node_get(np);
3751 cp->data = data;
3752 cp->get = clk_src_get;
3753
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003754 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003755 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003756 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003757 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05003758
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003759 ret = of_clk_set_defaults(np, true);
3760 if (ret < 0)
3761 of_clk_del_provider(np);
3762
3763 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003764}
3765EXPORT_SYMBOL_GPL(of_clk_add_provider);
3766
3767/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003768 * of_clk_add_hw_provider() - Register a clock provider for a node
3769 * @np: Device node pointer associated with clock provider
3770 * @get: callback for decoding clk_hw
3771 * @data: context pointer for @get callback.
3772 */
3773int of_clk_add_hw_provider(struct device_node *np,
3774 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3775 void *data),
3776 void *data)
3777{
3778 struct of_clk_provider *cp;
3779 int ret;
3780
3781 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3782 if (!cp)
3783 return -ENOMEM;
3784
3785 cp->node = of_node_get(np);
3786 cp->data = data;
3787 cp->get_hw = get;
3788
3789 mutex_lock(&of_clk_mutex);
3790 list_add(&cp->link, &of_clk_providers);
3791 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003792 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003793
3794 ret = of_clk_set_defaults(np, true);
3795 if (ret < 0)
3796 of_clk_del_provider(np);
3797
3798 return ret;
3799}
3800EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3801
Stephen Boydaa795c42017-09-01 16:16:40 -07003802static void devm_of_clk_release_provider(struct device *dev, void *res)
3803{
3804 of_clk_del_provider(*(struct device_node **)res);
3805}
3806
3807int devm_of_clk_add_hw_provider(struct device *dev,
3808 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3809 void *data),
3810 void *data)
3811{
3812 struct device_node **ptr, *np;
3813 int ret;
3814
3815 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3816 GFP_KERNEL);
3817 if (!ptr)
3818 return -ENOMEM;
3819
3820 np = dev->of_node;
3821 ret = of_clk_add_hw_provider(np, get, data);
3822 if (!ret) {
3823 *ptr = np;
3824 devres_add(dev, ptr);
3825 } else {
3826 devres_free(ptr);
3827 }
3828
3829 return ret;
3830}
3831EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3832
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003833/**
Grant Likely766e6a42012-04-09 14:50:06 -05003834 * of_clk_del_provider() - Remove a previously registered clock provider
3835 * @np: Device node pointer associated with clock provider
3836 */
3837void of_clk_del_provider(struct device_node *np)
3838{
3839 struct of_clk_provider *cp;
3840
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003841 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003842 list_for_each_entry(cp, &of_clk_providers, link) {
3843 if (cp->node == np) {
3844 list_del(&cp->link);
3845 of_node_put(cp->node);
3846 kfree(cp);
3847 break;
3848 }
3849 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003850 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003851}
3852EXPORT_SYMBOL_GPL(of_clk_del_provider);
3853
Stephen Boydaa795c42017-09-01 16:16:40 -07003854static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3855{
3856 struct device_node **np = res;
3857
3858 if (WARN_ON(!np || !*np))
3859 return 0;
3860
3861 return *np == data;
3862}
3863
3864void devm_of_clk_del_provider(struct device *dev)
3865{
3866 int ret;
3867
3868 ret = devres_release(dev, devm_of_clk_release_provider,
3869 devm_clk_provider_match, dev->of_node);
3870
3871 WARN_ON(ret);
3872}
3873EXPORT_SYMBOL(devm_of_clk_del_provider);
3874
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003875static struct clk_hw *
3876__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3877 struct of_phandle_args *clkspec)
3878{
3879 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003880
Stephen Boyd74002fc2016-08-25 13:35:36 -07003881 if (provider->get_hw)
3882 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003883
Stephen Boyd74002fc2016-08-25 13:35:36 -07003884 clk = provider->get(clkspec, provider->data);
3885 if (IS_ERR(clk))
3886 return ERR_CAST(clk);
3887 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003888}
3889
Stephen Boyd73e0e492015-02-06 11:42:43 -08003890struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3891 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05003892{
3893 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01003894 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Stephen Boydf155d152016-08-15 14:32:23 -07003895 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -05003896
Stephen Boyd306c3422015-02-05 15:39:11 -08003897 if (!clkspec)
3898 return ERR_PTR(-EINVAL);
3899
Grant Likely766e6a42012-04-09 14:50:06 -05003900 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08003901 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003902 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07003903 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003904 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003905 clk = __clk_create_clk(hw, dev_id, con_id);
Stephen Boydf155d152016-08-15 14:32:23 -07003906 }
Stephen Boyd73e0e492015-02-06 11:42:43 -08003907
Stephen Boydf155d152016-08-15 14:32:23 -07003908 if (!IS_ERR(clk)) {
3909 if (!__clk_get(clk)) {
Stephen Boyd73e0e492015-02-06 11:42:43 -08003910 __clk_free_clk(clk);
3911 clk = ERR_PTR(-ENOENT);
3912 }
3913
Grant Likely766e6a42012-04-09 14:50:06 -05003914 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08003915 }
Grant Likely766e6a42012-04-09 14:50:06 -05003916 }
Stephen Boyd306c3422015-02-05 15:39:11 -08003917 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003918
3919 return clk;
3920}
3921
Stephen Boyd306c3422015-02-05 15:39:11 -08003922/**
3923 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3924 * @clkspec: pointer to a clock specifier data structure
3925 *
3926 * This function looks up a struct clk from the registered list of clock
3927 * providers, an input is a clock specifier data structure as returned
3928 * from the of_parse_phandle_with_args() function call.
3929 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003930struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3931{
Stephen Boyd306c3422015-02-05 15:39:11 -08003932 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05003933}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06003934EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05003935
Stephen Boyd929e7f32016-02-19 15:52:32 -08003936/**
3937 * of_clk_get_parent_count() - Count the number of clocks a device node has
3938 * @np: device node to count
3939 *
3940 * Returns: The number of clocks that are possible parents of this node
3941 */
3942unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07003943{
Stephen Boyd929e7f32016-02-19 15:52:32 -08003944 int count;
3945
3946 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3947 if (count < 0)
3948 return 0;
3949
3950 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07003951}
3952EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3953
Grant Likely766e6a42012-04-09 14:50:06 -05003954const char *of_clk_get_parent_name(struct device_node *np, int index)
3955{
3956 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003957 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003958 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003959 const __be32 *vp;
3960 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003961 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003962 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003963 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05003964
Grant Likely766e6a42012-04-09 14:50:06 -05003965 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3966 &clkspec);
3967 if (rc)
3968 return NULL;
3969
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003970 index = clkspec.args_count ? clkspec.args[0] : 0;
3971 count = 0;
3972
3973 /* if there is an indices property, use it to transfer the index
3974 * specified into an array offset for the clock-output-names property.
3975 */
3976 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3977 if (index == pv) {
3978 index = count;
3979 break;
3980 }
3981 count++;
3982 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09003983 /* We went off the end of 'clock-indices' without finding it */
3984 if (prop && !vp)
3985 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003986
Grant Likely766e6a42012-04-09 14:50:06 -05003987 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003988 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003989 &clk_name) < 0) {
3990 /*
3991 * Best effort to get the name if the clock has been
3992 * registered with the framework. If the clock isn't
3993 * registered, we return the node name as the name of
3994 * the clock as long as #clock-cells = 0.
3995 */
3996 clk = of_clk_get_from_provider(&clkspec);
3997 if (IS_ERR(clk)) {
3998 if (clkspec.args_count == 0)
3999 clk_name = clkspec.np->name;
4000 else
4001 clk_name = NULL;
4002 } else {
4003 clk_name = __clk_get_name(clk);
4004 clk_put(clk);
4005 }
4006 }
4007
Grant Likely766e6a42012-04-09 14:50:06 -05004008
4009 of_node_put(clkspec.np);
4010 return clk_name;
4011}
4012EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4013
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05004014/**
4015 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4016 * number of parents
4017 * @np: Device node pointer associated with clock provider
4018 * @parents: pointer to char array that hold the parents' names
4019 * @size: size of the @parents array
4020 *
4021 * Return: number of parents for the clock node.
4022 */
4023int of_clk_parent_fill(struct device_node *np, const char **parents,
4024 unsigned int size)
4025{
4026 unsigned int i = 0;
4027
4028 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4029 i++;
4030
4031 return i;
4032}
4033EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4034
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004035struct clock_provider {
Geert Uytterhoevena59704332018-04-10 15:06:05 +02004036 void (*clk_init_cb)(struct device_node *);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004037 struct device_node *np;
4038 struct list_head node;
4039};
4040
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004041/*
4042 * This function looks for a parent clock. If there is one, then it
4043 * checks that the provider for this parent clock was initialized, in
4044 * this case the parent clock will be ready.
4045 */
4046static int parent_ready(struct device_node *np)
4047{
4048 int i = 0;
4049
4050 while (true) {
4051 struct clk *clk = of_clk_get(np, i);
4052
4053 /* this parent is ready we can check the next one */
4054 if (!IS_ERR(clk)) {
4055 clk_put(clk);
4056 i++;
4057 continue;
4058 }
4059
4060 /* at least one parent is not ready, we exit now */
4061 if (PTR_ERR(clk) == -EPROBE_DEFER)
4062 return 0;
4063
4064 /*
4065 * Here we make assumption that the device tree is
4066 * written correctly. So an error means that there is
4067 * no more parent. As we didn't exit yet, then the
4068 * previous parent are ready. If there is no clock
4069 * parent, no need to wait for them, then we can
4070 * consider their absence as being ready
4071 */
4072 return 1;
4073 }
4074}
4075
Grant Likely766e6a42012-04-09 14:50:06 -05004076/**
Lee Jonesd56f8992016-02-11 13:19:11 -08004077 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4078 * @np: Device node pointer associated with clock provider
4079 * @index: clock index
Geert Uytterhoevenf7ae7502018-01-03 12:06:14 +01004080 * @flags: pointer to top-level framework flags
Lee Jonesd56f8992016-02-11 13:19:11 -08004081 *
4082 * Detects if the clock-critical property exists and, if so, sets the
4083 * corresponding CLK_IS_CRITICAL flag.
4084 *
4085 * Do not use this function. It exists only for legacy Device Tree
4086 * bindings, such as the one-clock-per-node style that are outdated.
4087 * Those bindings typically put all clock data into .dts and the Linux
4088 * driver has no clock data, thus making it impossible to set this flag
4089 * correctly from the driver. Only those drivers may call
4090 * of_clk_detect_critical from their setup functions.
4091 *
4092 * Return: error code or zero on success
4093 */
4094int of_clk_detect_critical(struct device_node *np,
4095 int index, unsigned long *flags)
4096{
4097 struct property *prop;
4098 const __be32 *cur;
4099 uint32_t idx;
4100
4101 if (!np || !flags)
4102 return -EINVAL;
4103
4104 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4105 if (index == idx)
4106 *flags |= CLK_IS_CRITICAL;
4107
4108 return 0;
4109}
4110
4111/**
Grant Likely766e6a42012-04-09 14:50:06 -05004112 * of_clk_init() - Scan and init clock providers from the DT
4113 * @matches: array of compatible values and init functions for providers.
4114 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004115 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01004116 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004117 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05004118 */
4119void __init of_clk_init(const struct of_device_id *matches)
4120{
Alex Elder7f7ed582013-08-22 11:31:31 -05004121 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05004122 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004123 struct clock_provider *clk_provider, *next;
4124 bool is_init_done;
4125 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07004126 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05004127
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304128 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03004129 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304130
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004131 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05004132 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004133 struct clock_provider *parent;
4134
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01004135 if (!of_device_is_available(np))
4136 continue;
4137
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004138 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4139 if (!parent) {
4140 list_for_each_entry_safe(clk_provider, next,
4141 &clk_provider_list, node) {
4142 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004143 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004144 kfree(clk_provider);
4145 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004146 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004147 return;
4148 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004149
4150 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004151 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01004152 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004153 }
4154
4155 while (!list_empty(&clk_provider_list)) {
4156 is_init_done = false;
4157 list_for_each_entry_safe(clk_provider, next,
4158 &clk_provider_list, node) {
4159 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004160
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02004161 /* Don't populate platform devices */
4162 of_node_set_flag(clk_provider->np,
4163 OF_POPULATED);
4164
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004165 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004166 of_clk_set_defaults(clk_provider->np, true);
4167
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004168 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004169 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004170 kfree(clk_provider);
4171 is_init_done = true;
4172 }
4173 }
4174
4175 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01004176 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004177 * remaining providers during the last loop, so now we
4178 * initialize all the remaining ones unconditionally
4179 * in case the clock parent was not mandatory
4180 */
4181 if (!is_init_done)
4182 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05004183 }
4184}
4185#endif