blob: fe2d43e34216240e53d76743e89d74cb57a1884f [file] [log] [blame]
Mike Turquetteb24764902012-03-15 23:11:19 -07001/*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Standard functionality for the common clock API. See Documentation/clk.txt
10 */
11
Stephen Boyd3c373112015-06-19 15:00:46 -070012#include <linux/clk.h>
Michael Turquetteb09d6d92015-01-29 14:22:50 -080013#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020014#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070015#include <linux/module.h>
16#include <linux/mutex.h>
17#include <linux/spinlock.h>
18#include <linux/err.h>
19#include <linux/list.h>
20#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050021#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070022#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053023#include <linux/init.h>
Marek Szyprowski9a34b452017-08-21 10:04:59 +020024#include <linux/pm_runtime.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070025#include <linux/sched.h>
Stephen Boyd562ef0b2015-05-01 12:16:14 -070026#include <linux/clkdev.h>
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;
70 struct hlist_head children;
71 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010072 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080073 unsigned int notifier_count;
74#ifdef CONFIG_DEBUG_FS
75 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020076 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080077#endif
78 struct kref ref;
79};
80
Stephen Boyddfc202e2015-02-02 14:37:41 -080081#define CREATE_TRACE_POINTS
82#include <trace/events/clk.h>
83
Michael Turquetteb09d6d92015-01-29 14:22:50 -080084struct clk {
85 struct clk_core *core;
86 const char *dev_id;
87 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010088 unsigned long min_rate;
89 unsigned long max_rate;
Jerome Brunet55e9b8b2017-12-01 22:51:59 +010090 unsigned int exclusive_count;
Stephen Boyd50595f82015-02-06 11:42:44 -080091 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080092};
93
Marek Szyprowski9a34b452017-08-21 10:04:59 +020094/*** runtime pm ***/
95static int clk_pm_runtime_get(struct clk_core *core)
96{
97 int ret = 0;
98
99 if (!core->dev)
100 return 0;
101
102 ret = pm_runtime_get_sync(core->dev);
103 return ret < 0 ? ret : 0;
104}
105
106static void clk_pm_runtime_put(struct clk_core *core)
107{
108 if (!core->dev)
109 return;
110
111 pm_runtime_put_sync(core->dev);
112}
113
Mike Turquetteeab89f62013-03-28 13:59:01 -0700114/*** locking ***/
115static void clk_prepare_lock(void)
116{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700117 if (!mutex_trylock(&prepare_lock)) {
118 if (prepare_owner == current) {
119 prepare_refcnt++;
120 return;
121 }
122 mutex_lock(&prepare_lock);
123 }
124 WARN_ON_ONCE(prepare_owner != NULL);
125 WARN_ON_ONCE(prepare_refcnt != 0);
126 prepare_owner = current;
127 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700128}
129
130static void clk_prepare_unlock(void)
131{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700132 WARN_ON_ONCE(prepare_owner != current);
133 WARN_ON_ONCE(prepare_refcnt == 0);
134
135 if (--prepare_refcnt)
136 return;
137 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700138 mutex_unlock(&prepare_lock);
139}
140
141static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700142 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700143{
144 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700145
146 if (!spin_trylock_irqsave(&enable_lock, flags)) {
147 if (enable_owner == current) {
148 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700149 __acquire(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700150 return flags;
151 }
152 spin_lock_irqsave(&enable_lock, flags);
153 }
154 WARN_ON_ONCE(enable_owner != NULL);
155 WARN_ON_ONCE(enable_refcnt != 0);
156 enable_owner = current;
157 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700158 return flags;
159}
160
161static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700162 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700163{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700164 WARN_ON_ONCE(enable_owner != current);
165 WARN_ON_ONCE(enable_refcnt == 0);
166
Stephen Boyda57aa182015-07-24 12:24:48 -0700167 if (--enable_refcnt) {
168 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700169 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700170 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700171 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700172 spin_unlock_irqrestore(&enable_lock, flags);
173}
174
Jerome Brunete55a8392017-12-01 22:51:56 +0100175static bool clk_core_rate_is_protected(struct clk_core *core)
176{
177 return core->protect_count;
178}
179
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700180static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530181{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200182 bool ret = false;
183
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700184 /*
185 * .is_prepared is optional for clocks that can prepare
186 * fall back to software usage counter if it is missing
187 */
188 if (!core->ops->is_prepared)
189 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530190
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200191 if (!clk_pm_runtime_get(core)) {
192 ret = core->ops->is_prepared(core->hw);
193 clk_pm_runtime_put(core);
194 }
195
196 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530197}
198
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700199static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530200{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200201 bool ret = false;
202
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700203 /*
204 * .is_enabled is only mandatory for clocks that gate
205 * fall back to software usage counter if .is_enabled is missing
206 */
207 if (!core->ops->is_enabled)
208 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530209
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200210 /*
211 * Check if clock controller's device is runtime active before
212 * calling .is_enabled callback. If not, assume that clock is
213 * disabled, because we might be called from atomic context, from
214 * which pm_runtime_get() is not allowed.
215 * This function is called mainly from clk_disable_unused_subtree,
216 * which ensures proper runtime pm activation of controller before
217 * taking enable spinlock, but the below check is needed if one tries
218 * to call it from other places.
219 */
220 if (core->dev) {
221 pm_runtime_get_noresume(core->dev);
222 if (!pm_runtime_active(core->dev)) {
223 ret = false;
224 goto done;
225 }
226 }
227
228 ret = core->ops->is_enabled(core->hw);
229done:
230 clk_pm_runtime_put(core);
231
232 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530233}
234
Mike Turquetteb24764902012-03-15 23:11:19 -0700235/*** helper functions ***/
236
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200237const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700238{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100239 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700240}
Niels de Vos48950842012-12-13 13:12:25 +0100241EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700242
Stephen Boyde7df6f62015-08-12 13:04:56 -0700243const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700244{
245 return hw->core->name;
246}
247EXPORT_SYMBOL_GPL(clk_hw_get_name);
248
Russ Dill65800b22012-11-26 11:20:09 -0800249struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700250{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100251 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700252}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800253EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700254
Stephen Boyde7df6f62015-08-12 13:04:56 -0700255unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700256{
257 return hw->core->num_parents;
258}
259EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
260
Stephen Boyde7df6f62015-08-12 13:04:56 -0700261struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700262{
263 return hw->core->parent ? hw->core->parent->hw : NULL;
264}
265EXPORT_SYMBOL_GPL(clk_hw_get_parent);
266
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700267static struct clk_core *__clk_lookup_subtree(const char *name,
268 struct clk_core *core)
269{
270 struct clk_core *child;
271 struct clk_core *ret;
272
273 if (!strcmp(core->name, name))
274 return core;
275
276 hlist_for_each_entry(child, &core->children, child_node) {
277 ret = __clk_lookup_subtree(name, child);
278 if (ret)
279 return ret;
280 }
281
282 return NULL;
283}
284
285static struct clk_core *clk_core_lookup(const char *name)
286{
287 struct clk_core *root_clk;
288 struct clk_core *ret;
289
290 if (!name)
291 return NULL;
292
293 /* search the 'proper' clk tree first */
294 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
295 ret = __clk_lookup_subtree(name, root_clk);
296 if (ret)
297 return ret;
298 }
299
300 /* if not found, then search the orphan tree */
301 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
302 ret = __clk_lookup_subtree(name, root_clk);
303 if (ret)
304 return ret;
305 }
306
307 return NULL;
308}
309
Stephen Boydd6968fc2015-04-30 13:54:13 -0700310static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100311 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100312{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700313 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100314 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900315
316 if (!core->parents[index])
317 core->parents[index] =
318 clk_core_lookup(core->parent_names[index]);
319
320 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100321}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100322
Stephen Boyde7df6f62015-08-12 13:04:56 -0700323struct clk_hw *
324clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700325{
326 struct clk_core *parent;
327
328 parent = clk_core_get_parent_by_index(hw->core, index);
329
330 return !parent ? NULL : parent->hw;
331}
332EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
333
Russ Dill65800b22012-11-26 11:20:09 -0800334unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700335{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100336 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700337}
338
Stephen Boydd6968fc2015-04-30 13:54:13 -0700339static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700340{
341 unsigned long ret;
342
Stephen Boydd6968fc2015-04-30 13:54:13 -0700343 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530344 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700345 goto out;
346 }
347
Stephen Boydd6968fc2015-04-30 13:54:13 -0700348 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700349
Stephen Boyd47b0eeb2016-02-02 17:24:56 -0800350 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -0700351 goto out;
352
Stephen Boydd6968fc2015-04-30 13:54:13 -0700353 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530354 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700355
356out:
357 return ret;
358}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100359
Stephen Boyde7df6f62015-08-12 13:04:56 -0700360unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700361{
362 return clk_core_get_rate_nolock(hw->core);
363}
364EXPORT_SYMBOL_GPL(clk_hw_get_rate);
365
Stephen Boydd6968fc2015-04-30 13:54:13 -0700366static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100367{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700368 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100369 return 0;
370
Stephen Boydd6968fc2015-04-30 13:54:13 -0700371 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100372}
373
Russ Dill65800b22012-11-26 11:20:09 -0800374unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700375{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100376 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700377}
Thierry Redingb05c6832013-09-03 09:43:51 +0200378EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700379
Stephen Boyde7df6f62015-08-12 13:04:56 -0700380unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700381{
382 return hw->core->flags;
383}
384EXPORT_SYMBOL_GPL(clk_hw_get_flags);
385
Stephen Boyde7df6f62015-08-12 13:04:56 -0700386bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700387{
388 return clk_core_is_prepared(hw->core);
389}
390
Jerome Brunete55a8392017-12-01 22:51:56 +0100391bool clk_hw_rate_is_protected(const struct clk_hw *hw)
392{
393 return clk_core_rate_is_protected(hw->core);
394}
395
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200396bool clk_hw_is_enabled(const struct clk_hw *hw)
397{
398 return clk_core_is_enabled(hw->core);
399}
400
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100401bool __clk_is_enabled(struct clk *clk)
402{
403 if (!clk)
404 return false;
405
406 return clk_core_is_enabled(clk->core);
407}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800408EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700409
Stephen Boyd15a02c12015-01-19 18:05:28 -0800410static bool mux_is_better_rate(unsigned long rate, unsigned long now,
411 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100412{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800413 if (flags & CLK_MUX_ROUND_CLOSEST)
414 return abs(now - rate) < abs(best - rate);
415
416 return now <= rate && now > best;
417}
418
Boris Brezillon0817b622015-07-07 20:48:08 +0200419static int
420clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800421 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100422{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100423 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200424 int i, num_parents, ret;
425 unsigned long best = 0;
426 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100427
428 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100429 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
430 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200431 if (core->flags & CLK_SET_RATE_PARENT) {
432 ret = __clk_determine_rate(parent ? parent->hw : NULL,
433 &parent_req);
434 if (ret)
435 return ret;
436
437 best = parent_req.rate;
438 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100439 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200440 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100441 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200442 }
443
James Hogane366fdd2013-07-29 12:25:02 +0100444 goto out;
445 }
446
447 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100448 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100449 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100450 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100451 if (!parent)
452 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200453
454 if (core->flags & CLK_SET_RATE_PARENT) {
455 parent_req = *req;
456 ret = __clk_determine_rate(parent->hw, &parent_req);
457 if (ret)
458 continue;
459 } else {
460 parent_req.rate = clk_core_get_rate_nolock(parent);
461 }
462
463 if (mux_is_better_rate(req->rate, parent_req.rate,
464 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100465 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200466 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100467 }
468 }
469
Boris Brezillon57d866e2015-07-09 22:39:38 +0200470 if (!best_parent)
471 return -EINVAL;
472
James Hogane366fdd2013-07-29 12:25:02 +0100473out:
474 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200475 req->best_parent_hw = best_parent->hw;
476 req->best_parent_rate = best;
477 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100478
Boris Brezillon0817b622015-07-07 20:48:08 +0200479 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100480}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800481
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100482struct clk *__clk_lookup(const char *name)
483{
484 struct clk_core *core = clk_core_lookup(name);
485
486 return !core ? NULL : core->hw->clk;
487}
488
Stephen Boydd6968fc2015-04-30 13:54:13 -0700489static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100490 unsigned long *min_rate,
491 unsigned long *max_rate)
492{
493 struct clk *clk_user;
494
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700495 *min_rate = core->min_rate;
496 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100497
Stephen Boydd6968fc2015-04-30 13:54:13 -0700498 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100499 *min_rate = max(*min_rate, clk_user->min_rate);
500
Stephen Boydd6968fc2015-04-30 13:54:13 -0700501 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100502 *max_rate = min(*max_rate, clk_user->max_rate);
503}
504
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700505void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
506 unsigned long max_rate)
507{
508 hw->core->min_rate = min_rate;
509 hw->core->max_rate = max_rate;
510}
511EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
512
Stephen Boyd15a02c12015-01-19 18:05:28 -0800513/*
514 * Helper for finding best parent to provide a given frequency. This can be used
515 * directly as a determine_rate callback (e.g. for a mux), or from a more
516 * complex clock that may combine a mux with other operations.
517 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200518int __clk_mux_determine_rate(struct clk_hw *hw,
519 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800520{
Boris Brezillon0817b622015-07-07 20:48:08 +0200521 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800522}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800523EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100524
Boris Brezillon0817b622015-07-07 20:48:08 +0200525int __clk_mux_determine_rate_closest(struct clk_hw *hw,
526 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800527{
Boris Brezillon0817b622015-07-07 20:48:08 +0200528 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800529}
530EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
531
Mike Turquetteb24764902012-03-15 23:11:19 -0700532/*** clk api ***/
533
Jerome Brunete55a8392017-12-01 22:51:56 +0100534static void clk_core_rate_unprotect(struct clk_core *core)
535{
536 lockdep_assert_held(&prepare_lock);
537
538 if (!core)
539 return;
540
541 if (WARN_ON(core->protect_count == 0))
542 return;
543
544 if (--core->protect_count > 0)
545 return;
546
547 clk_core_rate_unprotect(core->parent);
548}
549
550static int clk_core_rate_nuke_protect(struct clk_core *core)
551{
552 int ret;
553
554 lockdep_assert_held(&prepare_lock);
555
556 if (!core)
557 return -EINVAL;
558
559 if (core->protect_count == 0)
560 return 0;
561
562 ret = core->protect_count;
563 core->protect_count = 1;
564 clk_core_rate_unprotect(core);
565
566 return ret;
567}
568
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100569/**
570 * clk_rate_exclusive_put - release exclusivity over clock rate control
571 * @clk: the clk over which the exclusivity is released
572 *
573 * clk_rate_exclusive_put() completes a critical section during which a clock
574 * consumer cannot tolerate any other consumer making any operation on the
575 * clock which could result in a rate change or rate glitch. Exclusive clocks
576 * cannot have their rate changed, either directly or indirectly due to changes
577 * further up the parent chain of clocks. As a result, clocks up parent chain
578 * also get under exclusive control of the calling consumer.
579 *
580 * If exlusivity is claimed more than once on clock, even by the same consumer,
581 * the rate effectively gets locked as exclusivity can't be preempted.
582 *
583 * Calls to clk_rate_exclusive_put() must be balanced with calls to
584 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
585 * error status.
586 */
587void clk_rate_exclusive_put(struct clk *clk)
588{
589 if (!clk)
590 return;
591
592 clk_prepare_lock();
593
594 /*
595 * if there is something wrong with this consumer protect count, stop
596 * here before messing with the provider
597 */
598 if (WARN_ON(clk->exclusive_count <= 0))
599 goto out;
600
601 clk_core_rate_unprotect(clk->core);
602 clk->exclusive_count--;
603out:
604 clk_prepare_unlock();
605}
606EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
607
Jerome Brunete55a8392017-12-01 22:51:56 +0100608static void clk_core_rate_protect(struct clk_core *core)
609{
610 lockdep_assert_held(&prepare_lock);
611
612 if (!core)
613 return;
614
615 if (core->protect_count == 0)
616 clk_core_rate_protect(core->parent);
617
618 core->protect_count++;
619}
620
621static void clk_core_rate_restore_protect(struct clk_core *core, int count)
622{
623 lockdep_assert_held(&prepare_lock);
624
625 if (!core)
626 return;
627
628 if (count == 0)
629 return;
630
631 clk_core_rate_protect(core);
632 core->protect_count = count;
633}
634
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100635/**
636 * clk_rate_exclusive_get - get exclusivity over the clk rate control
637 * @clk: the clk over which the exclusity of rate control is requested
638 *
639 * clk_rate_exlusive_get() begins a critical section during which a clock
640 * consumer cannot tolerate any other consumer making any operation on the
641 * clock which could result in a rate change or rate glitch. Exclusive clocks
642 * cannot have their rate changed, either directly or indirectly due to changes
643 * further up the parent chain of clocks. As a result, clocks up parent chain
644 * also get under exclusive control of the calling consumer.
645 *
646 * If exlusivity is claimed more than once on clock, even by the same consumer,
647 * the rate effectively gets locked as exclusivity can't be preempted.
648 *
649 * Calls to clk_rate_exclusive_get() should be balanced with calls to
650 * clk_rate_exclusive_put(). Calls to this function may sleep.
651 * Returns 0 on success, -EERROR otherwise
652 */
653int clk_rate_exclusive_get(struct clk *clk)
654{
655 if (!clk)
656 return 0;
657
658 clk_prepare_lock();
659 clk_core_rate_protect(clk->core);
660 clk->exclusive_count++;
661 clk_prepare_unlock();
662
663 return 0;
664}
665EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
666
Stephen Boydd6968fc2015-04-30 13:54:13 -0700667static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700668{
Stephen Boyda6334722015-05-06 17:00:54 -0700669 lockdep_assert_held(&prepare_lock);
670
Stephen Boydd6968fc2015-04-30 13:54:13 -0700671 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700672 return;
673
Stephen Boydd6968fc2015-04-30 13:54:13 -0700674 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700675 return;
676
Lee Jones2e20fbf2016-02-11 13:19:10 -0800677 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
678 return;
679
Stephen Boydd6968fc2015-04-30 13:54:13 -0700680 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700681 return;
682
Stephen Boydd6968fc2015-04-30 13:54:13 -0700683 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700684
Stephen Boydd6968fc2015-04-30 13:54:13 -0700685 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800686
Stephen Boydd6968fc2015-04-30 13:54:13 -0700687 if (core->ops->unprepare)
688 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700689
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200690 clk_pm_runtime_put(core);
691
Stephen Boydd6968fc2015-04-30 13:54:13 -0700692 trace_clk_unprepare_complete(core);
693 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700694}
695
Dong Aishenga6adc302016-06-30 17:31:11 +0800696static void clk_core_unprepare_lock(struct clk_core *core)
697{
698 clk_prepare_lock();
699 clk_core_unprepare(core);
700 clk_prepare_unlock();
701}
702
Mike Turquetteb24764902012-03-15 23:11:19 -0700703/**
704 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200705 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700706 *
707 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
708 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
709 * if the operation may sleep. One example is a clk which is accessed over
710 * I2c. In the complex case a clk gate operation may require a fast and a slow
711 * part. It is this reason that clk_unprepare and clk_disable are not mutually
712 * exclusive. In fact clk_disable must be called before clk_unprepare.
713 */
714void clk_unprepare(struct clk *clk)
715{
Stephen Boyd63589e92014-03-26 16:06:37 -0700716 if (IS_ERR_OR_NULL(clk))
717 return;
718
Dong Aishenga6adc302016-06-30 17:31:11 +0800719 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700720}
721EXPORT_SYMBOL_GPL(clk_unprepare);
722
Stephen Boydd6968fc2015-04-30 13:54:13 -0700723static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700724{
725 int ret = 0;
726
Stephen Boyda6334722015-05-06 17:00:54 -0700727 lockdep_assert_held(&prepare_lock);
728
Stephen Boydd6968fc2015-04-30 13:54:13 -0700729 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700730 return 0;
731
Stephen Boydd6968fc2015-04-30 13:54:13 -0700732 if (core->prepare_count == 0) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200733 ret = clk_pm_runtime_get(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700734 if (ret)
735 return ret;
736
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200737 ret = clk_core_prepare(core->parent);
738 if (ret)
739 goto runtime_put;
740
Stephen Boydd6968fc2015-04-30 13:54:13 -0700741 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800742
Stephen Boydd6968fc2015-04-30 13:54:13 -0700743 if (core->ops->prepare)
744 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800745
Stephen Boydd6968fc2015-04-30 13:54:13 -0700746 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800747
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200748 if (ret)
749 goto unprepare;
Mike Turquetteb24764902012-03-15 23:11:19 -0700750 }
751
Stephen Boydd6968fc2015-04-30 13:54:13 -0700752 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700753
754 return 0;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200755unprepare:
756 clk_core_unprepare(core->parent);
757runtime_put:
758 clk_pm_runtime_put(core);
759 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700760}
761
Dong Aishenga6adc302016-06-30 17:31:11 +0800762static int clk_core_prepare_lock(struct clk_core *core)
763{
764 int ret;
765
766 clk_prepare_lock();
767 ret = clk_core_prepare(core);
768 clk_prepare_unlock();
769
770 return ret;
771}
772
Mike Turquetteb24764902012-03-15 23:11:19 -0700773/**
774 * clk_prepare - prepare a clock source
775 * @clk: the clk being prepared
776 *
777 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
778 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
779 * operation may sleep. One example is a clk which is accessed over I2c. In
780 * the complex case a clk ungate operation may require a fast and a slow part.
781 * It is this reason that clk_prepare and clk_enable are not mutually
782 * exclusive. In fact clk_prepare must be called before clk_enable.
783 * Returns 0 on success, -EERROR otherwise.
784 */
785int clk_prepare(struct clk *clk)
786{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100787 if (!clk)
788 return 0;
789
Dong Aishenga6adc302016-06-30 17:31:11 +0800790 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700791}
792EXPORT_SYMBOL_GPL(clk_prepare);
793
Stephen Boydd6968fc2015-04-30 13:54:13 -0700794static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700795{
Stephen Boyda6334722015-05-06 17:00:54 -0700796 lockdep_assert_held(&enable_lock);
797
Stephen Boydd6968fc2015-04-30 13:54:13 -0700798 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700799 return;
800
Stephen Boydd6968fc2015-04-30 13:54:13 -0700801 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700802 return;
803
Lee Jones2e20fbf2016-02-11 13:19:10 -0800804 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
805 return;
806
Stephen Boydd6968fc2015-04-30 13:54:13 -0700807 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700808 return;
809
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700810 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800811
Stephen Boydd6968fc2015-04-30 13:54:13 -0700812 if (core->ops->disable)
813 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700814
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700815 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800816
Stephen Boydd6968fc2015-04-30 13:54:13 -0700817 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100818}
819
Dong Aishenga6adc302016-06-30 17:31:11 +0800820static void clk_core_disable_lock(struct clk_core *core)
821{
822 unsigned long flags;
823
824 flags = clk_enable_lock();
825 clk_core_disable(core);
826 clk_enable_unlock(flags);
827}
828
Mike Turquetteb24764902012-03-15 23:11:19 -0700829/**
830 * clk_disable - gate a clock
831 * @clk: the clk being gated
832 *
833 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
834 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
835 * clk if the operation is fast and will never sleep. One example is a
836 * SoC-internal clk which is controlled via simple register writes. In the
837 * complex case a clk gate operation may require a fast and a slow part. It is
838 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
839 * In fact clk_disable must be called before clk_unprepare.
840 */
841void clk_disable(struct clk *clk)
842{
Stephen Boyd63589e92014-03-26 16:06:37 -0700843 if (IS_ERR_OR_NULL(clk))
844 return;
845
Dong Aishenga6adc302016-06-30 17:31:11 +0800846 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700847}
848EXPORT_SYMBOL_GPL(clk_disable);
849
Stephen Boydd6968fc2015-04-30 13:54:13 -0700850static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700851{
852 int ret = 0;
853
Stephen Boyda6334722015-05-06 17:00:54 -0700854 lockdep_assert_held(&enable_lock);
855
Stephen Boydd6968fc2015-04-30 13:54:13 -0700856 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700857 return 0;
858
Stephen Boydd6968fc2015-04-30 13:54:13 -0700859 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700860 return -ESHUTDOWN;
861
Stephen Boydd6968fc2015-04-30 13:54:13 -0700862 if (core->enable_count == 0) {
863 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700864
865 if (ret)
866 return ret;
867
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700868 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800869
Stephen Boydd6968fc2015-04-30 13:54:13 -0700870 if (core->ops->enable)
871 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800872
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700873 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800874
875 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700876 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800877 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700878 }
879 }
880
Stephen Boydd6968fc2015-04-30 13:54:13 -0700881 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700882 return 0;
883}
884
Dong Aishenga6adc302016-06-30 17:31:11 +0800885static int clk_core_enable_lock(struct clk_core *core)
886{
887 unsigned long flags;
888 int ret;
889
890 flags = clk_enable_lock();
891 ret = clk_core_enable(core);
892 clk_enable_unlock(flags);
893
894 return ret;
895}
896
Mike Turquetteb24764902012-03-15 23:11:19 -0700897/**
898 * clk_enable - ungate a clock
899 * @clk: the clk being ungated
900 *
901 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
902 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
903 * if the operation will never sleep. One example is a SoC-internal clk which
904 * is controlled via simple register writes. In the complex case a clk ungate
905 * operation may require a fast and a slow part. It is this reason that
906 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
907 * must be called before clk_enable. Returns 0 on success, -EERROR
908 * otherwise.
909 */
910int clk_enable(struct clk *clk)
911{
Dong Aisheng864e1602015-04-30 14:02:19 -0700912 if (!clk)
913 return 0;
914
Dong Aishenga6adc302016-06-30 17:31:11 +0800915 return clk_core_enable_lock(clk->core);
916}
917EXPORT_SYMBOL_GPL(clk_enable);
918
919static int clk_core_prepare_enable(struct clk_core *core)
920{
921 int ret;
922
923 ret = clk_core_prepare_lock(core);
924 if (ret)
925 return ret;
926
927 ret = clk_core_enable_lock(core);
928 if (ret)
929 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700930
931 return ret;
932}
Dong Aishenga6adc302016-06-30 17:31:11 +0800933
934static void clk_core_disable_unprepare(struct clk_core *core)
935{
936 clk_core_disable_lock(core);
937 clk_core_unprepare_lock(core);
938}
Mike Turquetteb24764902012-03-15 23:11:19 -0700939
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800940static void clk_unprepare_unused_subtree(struct clk_core *core)
941{
942 struct clk_core *child;
943
944 lockdep_assert_held(&prepare_lock);
945
946 hlist_for_each_entry(child, &core->children, child_node)
947 clk_unprepare_unused_subtree(child);
948
949 if (core->prepare_count)
950 return;
951
952 if (core->flags & CLK_IGNORE_UNUSED)
953 return;
954
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200955 if (clk_pm_runtime_get(core))
956 return;
957
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800958 if (clk_core_is_prepared(core)) {
959 trace_clk_unprepare(core);
960 if (core->ops->unprepare_unused)
961 core->ops->unprepare_unused(core->hw);
962 else if (core->ops->unprepare)
963 core->ops->unprepare(core->hw);
964 trace_clk_unprepare_complete(core);
965 }
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200966
967 clk_pm_runtime_put(core);
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800968}
969
970static void clk_disable_unused_subtree(struct clk_core *core)
971{
972 struct clk_core *child;
973 unsigned long flags;
974
975 lockdep_assert_held(&prepare_lock);
976
977 hlist_for_each_entry(child, &core->children, child_node)
978 clk_disable_unused_subtree(child);
979
Dong Aishenga4b35182016-06-30 17:31:13 +0800980 if (core->flags & CLK_OPS_PARENT_ENABLE)
981 clk_core_prepare_enable(core->parent);
982
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200983 if (clk_pm_runtime_get(core))
984 goto unprepare_out;
985
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800986 flags = clk_enable_lock();
987
988 if (core->enable_count)
989 goto unlock_out;
990
991 if (core->flags & CLK_IGNORE_UNUSED)
992 goto unlock_out;
993
994 /*
995 * some gate clocks have special needs during the disable-unused
996 * sequence. call .disable_unused if available, otherwise fall
997 * back to .disable
998 */
999 if (clk_core_is_enabled(core)) {
1000 trace_clk_disable(core);
1001 if (core->ops->disable_unused)
1002 core->ops->disable_unused(core->hw);
1003 else if (core->ops->disable)
1004 core->ops->disable(core->hw);
1005 trace_clk_disable_complete(core);
1006 }
1007
1008unlock_out:
1009 clk_enable_unlock(flags);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001010 clk_pm_runtime_put(core);
1011unprepare_out:
Dong Aishenga4b35182016-06-30 17:31:13 +08001012 if (core->flags & CLK_OPS_PARENT_ENABLE)
1013 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001014}
1015
1016static bool clk_ignore_unused;
1017static int __init clk_ignore_unused_setup(char *__unused)
1018{
1019 clk_ignore_unused = true;
1020 return 1;
1021}
1022__setup("clk_ignore_unused", clk_ignore_unused_setup);
1023
1024static int clk_disable_unused(void)
1025{
1026 struct clk_core *core;
1027
1028 if (clk_ignore_unused) {
1029 pr_warn("clk: Not disabling unused clocks\n");
1030 return 0;
1031 }
1032
1033 clk_prepare_lock();
1034
1035 hlist_for_each_entry(core, &clk_root_list, child_node)
1036 clk_disable_unused_subtree(core);
1037
1038 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1039 clk_disable_unused_subtree(core);
1040
1041 hlist_for_each_entry(core, &clk_root_list, child_node)
1042 clk_unprepare_unused_subtree(core);
1043
1044 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1045 clk_unprepare_unused_subtree(core);
1046
1047 clk_prepare_unlock();
1048
1049 return 0;
1050}
1051late_initcall_sync(clk_disable_unused);
1052
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001053static int clk_core_determine_round_nolock(struct clk_core *core,
1054 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001055{
Boris Brezillon0817b622015-07-07 20:48:08 +02001056 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001057
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001058 lockdep_assert_held(&prepare_lock);
1059
Stephen Boydd6968fc2015-04-30 13:54:13 -07001060 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001061 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001062
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001063 /*
1064 * At this point, core protection will be disabled if
1065 * - if the provider is not protected at all
1066 * - if the calling consumer is the only one which has exclusivity
1067 * over the provider
1068 */
Jerome Brunete55a8392017-12-01 22:51:56 +01001069 if (clk_core_rate_is_protected(core)) {
1070 req->rate = core->rate;
1071 } else if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001072 return core->ops->determine_rate(core->hw, req);
1073 } else if (core->ops->round_rate) {
1074 rate = core->ops->round_rate(core->hw, req->rate,
1075 &req->best_parent_rate);
1076 if (rate < 0)
1077 return rate;
1078
1079 req->rate = rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001080 } else {
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001081 return -EINVAL;
Boris Brezillon0817b622015-07-07 20:48:08 +02001082 }
1083
1084 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001085}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001086
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001087static void clk_core_init_rate_req(struct clk_core * const core,
1088 struct clk_rate_request *req)
1089{
1090 struct clk_core *parent;
1091
1092 if (WARN_ON(!core || !req))
1093 return;
1094
1095 parent = core->parent;
1096 if (parent) {
1097 req->best_parent_hw = parent->hw;
1098 req->best_parent_rate = parent->rate;
1099 } else {
1100 req->best_parent_hw = NULL;
1101 req->best_parent_rate = 0;
1102 }
1103}
1104
1105static bool clk_core_can_round(struct clk_core * const core)
1106{
1107 if (core->ops->determine_rate || core->ops->round_rate)
1108 return true;
1109
1110 return false;
1111}
1112
1113static int clk_core_round_rate_nolock(struct clk_core *core,
1114 struct clk_rate_request *req)
1115{
1116 lockdep_assert_held(&prepare_lock);
1117
1118 if (!core)
1119 return 0;
1120
1121 clk_core_init_rate_req(core, req);
1122
1123 if (clk_core_can_round(core))
1124 return clk_core_determine_round_nolock(core, req);
1125 else if (core->flags & CLK_SET_RATE_PARENT)
1126 return clk_core_round_rate_nolock(core->parent, req);
1127
1128 req->rate = core->rate;
1129 return 0;
1130}
1131
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001132/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001133 * __clk_determine_rate - get the closest rate actually supported by a clock
1134 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001135 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001136 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001137 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001138 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001139int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001140{
Boris Brezillon0817b622015-07-07 20:48:08 +02001141 if (!hw) {
1142 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001143 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001144 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001145
Boris Brezillon0817b622015-07-07 20:48:08 +02001146 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001147}
1148EXPORT_SYMBOL_GPL(__clk_determine_rate);
1149
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001150unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1151{
1152 int ret;
1153 struct clk_rate_request req;
1154
1155 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1156 req.rate = rate;
1157
1158 ret = clk_core_round_rate_nolock(hw->core, &req);
1159 if (ret)
1160 return 0;
1161
1162 return req.rate;
1163}
1164EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1165
Mike Turquetteb24764902012-03-15 23:11:19 -07001166/**
1167 * clk_round_rate - round the given rate for a clk
1168 * @clk: the clk for which we are rounding a rate
1169 * @rate: the rate which is to be rounded
1170 *
1171 * Takes in a rate as input and rounds it to a rate that the clk can actually
1172 * use which is then returned. If clk doesn't support round_rate operation
1173 * then the parent rate is returned.
1174 */
1175long clk_round_rate(struct clk *clk, unsigned long rate)
1176{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001177 struct clk_rate_request req;
1178 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001179
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001180 if (!clk)
1181 return 0;
1182
Mike Turquetteeab89f62013-03-28 13:59:01 -07001183 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001184
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001185 if (clk->exclusive_count)
1186 clk_core_rate_unprotect(clk->core);
1187
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001188 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1189 req.rate = rate;
1190
1191 ret = clk_core_round_rate_nolock(clk->core, &req);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001192
1193 if (clk->exclusive_count)
1194 clk_core_rate_protect(clk->core);
1195
Mike Turquetteeab89f62013-03-28 13:59:01 -07001196 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001197
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001198 if (ret)
1199 return ret;
1200
1201 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001202}
1203EXPORT_SYMBOL_GPL(clk_round_rate);
1204
1205/**
1206 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001207 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001208 * @msg: clk notifier type (see include/linux/clk.h)
1209 * @old_rate: old clk rate
1210 * @new_rate: new clk rate
1211 *
1212 * Triggers a notifier call chain on the clk rate-change notification
1213 * for 'clk'. Passes a pointer to the struct clk and the previous
1214 * and current rates to the notifier callback. Intended to be called by
1215 * internal clock code only. Returns NOTIFY_DONE from the last driver
1216 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1217 * a driver returns that.
1218 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001219static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001220 unsigned long old_rate, unsigned long new_rate)
1221{
1222 struct clk_notifier *cn;
1223 struct clk_notifier_data cnd;
1224 int ret = NOTIFY_DONE;
1225
Mike Turquetteb24764902012-03-15 23:11:19 -07001226 cnd.old_rate = old_rate;
1227 cnd.new_rate = new_rate;
1228
1229 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001230 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001231 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001232 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1233 &cnd);
Peter De Schrijver17c34c52017-03-21 12:16:26 +02001234 if (ret & NOTIFY_STOP_MASK)
1235 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001236 }
1237 }
1238
1239 return ret;
1240}
1241
1242/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001243 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001244 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001245 *
1246 * Walks the subtree of clks starting with clk and recalculates accuracies as
1247 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001248 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001249 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001250 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001251static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001252{
1253 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001254 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001255
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001256 lockdep_assert_held(&prepare_lock);
1257
Stephen Boydd6968fc2015-04-30 13:54:13 -07001258 if (core->parent)
1259 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001260
Stephen Boydd6968fc2015-04-30 13:54:13 -07001261 if (core->ops->recalc_accuracy)
1262 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001263 parent_accuracy);
1264 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001265 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001266
Stephen Boydd6968fc2015-04-30 13:54:13 -07001267 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001268 __clk_recalc_accuracies(child);
1269}
1270
Stephen Boydd6968fc2015-04-30 13:54:13 -07001271static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001272{
1273 unsigned long accuracy;
1274
1275 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001276 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1277 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001278
Stephen Boydd6968fc2015-04-30 13:54:13 -07001279 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001280 clk_prepare_unlock();
1281
1282 return accuracy;
1283}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001284
1285/**
1286 * clk_get_accuracy - return the accuracy of clk
1287 * @clk: the clk whose accuracy is being returned
1288 *
1289 * Simply returns the cached accuracy of the clk, unless
1290 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1291 * issued.
1292 * If clk is NULL then returns 0.
1293 */
1294long clk_get_accuracy(struct clk *clk)
1295{
1296 if (!clk)
1297 return 0;
1298
1299 return clk_core_get_accuracy(clk->core);
1300}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001301EXPORT_SYMBOL_GPL(clk_get_accuracy);
1302
Stephen Boydd6968fc2015-04-30 13:54:13 -07001303static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001304 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001305{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001306 unsigned long rate = parent_rate;
1307
1308 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1309 rate = core->ops->recalc_rate(core->hw, parent_rate);
1310 clk_pm_runtime_put(core);
1311 }
1312 return rate;
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001313}
1314
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001315/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001316 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001317 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001318 * @msg: notification type (see include/linux/clk.h)
1319 *
1320 * Walks the subtree of clks starting with clk and recalculates rates as it
1321 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001322 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001323 *
1324 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1325 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001326 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001327static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001328{
1329 unsigned long old_rate;
1330 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001331 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001332
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001333 lockdep_assert_held(&prepare_lock);
1334
Stephen Boydd6968fc2015-04-30 13:54:13 -07001335 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001336
Stephen Boydd6968fc2015-04-30 13:54:13 -07001337 if (core->parent)
1338 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001339
Stephen Boydd6968fc2015-04-30 13:54:13 -07001340 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001341
1342 /*
1343 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1344 * & ABORT_RATE_CHANGE notifiers
1345 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001346 if (core->notifier_count && msg)
1347 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001348
Stephen Boydd6968fc2015-04-30 13:54:13 -07001349 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001350 __clk_recalc_rates(child, msg);
1351}
1352
Stephen Boydd6968fc2015-04-30 13:54:13 -07001353static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001354{
1355 unsigned long rate;
1356
1357 clk_prepare_lock();
1358
Stephen Boydd6968fc2015-04-30 13:54:13 -07001359 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1360 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001361
Stephen Boydd6968fc2015-04-30 13:54:13 -07001362 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001363 clk_prepare_unlock();
1364
1365 return rate;
1366}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001367
Mike Turquetteb24764902012-03-15 23:11:19 -07001368/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001369 * clk_get_rate - return the rate of clk
1370 * @clk: the clk whose rate is being returned
1371 *
1372 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1373 * is set, which means a recalc_rate will be issued.
1374 * If clk is NULL then returns 0.
1375 */
1376unsigned long clk_get_rate(struct clk *clk)
1377{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001378 if (!clk)
1379 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001380
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001381 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001382}
1383EXPORT_SYMBOL_GPL(clk_get_rate);
1384
Stephen Boydd6968fc2015-04-30 13:54:13 -07001385static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001386 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001387{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001388 int i;
James Hogan4935b222013-07-29 12:24:59 +01001389
Masahiro Yamada508f8842015-12-28 19:23:08 +09001390 if (!parent)
1391 return -EINVAL;
1392
Masahiro Yamada470b5e22015-12-28 19:23:09 +09001393 for (i = 0; i < core->num_parents; i++)
1394 if (clk_core_get_parent_by_index(core, i) == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001395 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001396
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001397 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001398}
1399
Heiko Stuebnere6500342015-04-22 22:53:05 +02001400/*
1401 * Update the orphan status of @core and all its children.
1402 */
1403static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1404{
1405 struct clk_core *child;
1406
1407 core->orphan = is_orphan;
1408
1409 hlist_for_each_entry(child, &core->children, child_node)
1410 clk_core_update_orphan_status(child, is_orphan);
1411}
1412
Stephen Boydd6968fc2015-04-30 13:54:13 -07001413static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001414{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001415 bool was_orphan = core->orphan;
1416
Stephen Boydd6968fc2015-04-30 13:54:13 -07001417 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001418
James Hogan903efc52013-08-29 12:10:51 +01001419 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001420 bool becomes_orphan = new_parent->orphan;
1421
James Hogan903efc52013-08-29 12:10:51 +01001422 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001423 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001424 new_parent->new_child = NULL;
1425
Stephen Boydd6968fc2015-04-30 13:54:13 -07001426 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001427
1428 if (was_orphan != becomes_orphan)
1429 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001430 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001431 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001432 if (!was_orphan)
1433 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001434 }
James Hogan4935b222013-07-29 12:24:59 +01001435
Stephen Boydd6968fc2015-04-30 13:54:13 -07001436 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001437}
1438
Stephen Boydd6968fc2015-04-30 13:54:13 -07001439static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001440 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001441{
1442 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001443 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001444
1445 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001446 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1447 *
1448 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001449 * clk_enable().
1450 *
1451 * If the clock is not prepared, then a race with
1452 * clk_enable/disable() is impossible since we already have the
1453 * prepare lock (future calls to clk_enable() need to be preceded by
1454 * a clk_prepare()).
1455 *
1456 * If the clock is prepared, migrate the prepared state to the new
1457 * parent and also protect against a race with clk_enable() by
1458 * forcing the clock and the new parent on. This ensures that all
1459 * future calls to clk_enable() are practically NOPs with respect to
1460 * hardware and software states.
1461 *
1462 * See also: Comment for clk_set_parent() below.
1463 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001464
1465 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1466 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1467 clk_core_prepare_enable(old_parent);
1468 clk_core_prepare_enable(parent);
1469 }
1470
1471 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001472 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001473 clk_core_prepare_enable(parent);
1474 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001475 }
1476
1477 /* update the clk tree topology */
1478 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001479 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001480 clk_enable_unlock(flags);
1481
Stephen Boyd3fa22522014-01-15 10:47:22 -08001482 return old_parent;
1483}
1484
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001485static void __clk_set_parent_after(struct clk_core *core,
1486 struct clk_core *parent,
1487 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001488{
1489 /*
1490 * Finish the migration of prepare state and undo the changes done
1491 * for preventing a race with clk_enable().
1492 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001493 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001494 clk_core_disable_lock(core);
1495 clk_core_disable_unprepare(old_parent);
1496 }
1497
1498 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1499 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1500 clk_core_disable_unprepare(parent);
1501 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001502 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001503}
1504
Stephen Boydd6968fc2015-04-30 13:54:13 -07001505static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001506 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001507{
1508 unsigned long flags;
1509 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001510 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001511
Stephen Boydd6968fc2015-04-30 13:54:13 -07001512 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001513
Stephen Boydd6968fc2015-04-30 13:54:13 -07001514 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001515
James Hogan4935b222013-07-29 12:24:59 +01001516 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001517 if (parent && core->ops->set_parent)
1518 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001519
Stephen Boydd6968fc2015-04-30 13:54:13 -07001520 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001521
James Hogan4935b222013-07-29 12:24:59 +01001522 if (ret) {
1523 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001524 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001525 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001526 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001527
James Hogan4935b222013-07-29 12:24:59 +01001528 return ret;
1529 }
1530
Stephen Boydd6968fc2015-04-30 13:54:13 -07001531 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001532
James Hogan4935b222013-07-29 12:24:59 +01001533 return 0;
1534}
1535
Ulf Hanssona093bde2012-08-31 14:21:28 +02001536/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001537 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001538 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001539 * @parent_rate: the "future" rate of clk's parent
1540 *
1541 * Walks the subtree of clks starting with clk, speculating rates as it
1542 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1543 *
1544 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1545 * pre-rate change notifications and returns early if no clks in the
1546 * subtree have subscribed to the notifications. Note that if a clk does not
1547 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001548 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001549 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001550static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001551 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001552{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001553 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001554 unsigned long new_rate;
1555 int ret = NOTIFY_DONE;
1556
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001557 lockdep_assert_held(&prepare_lock);
1558
Stephen Boydd6968fc2015-04-30 13:54:13 -07001559 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001560
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001561 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001562 if (core->notifier_count)
1563 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001564
Mike Turquette86bcfa22014-02-24 16:08:41 -08001565 if (ret & NOTIFY_STOP_MASK) {
1566 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001567 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001568 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001569 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001570
Stephen Boydd6968fc2015-04-30 13:54:13 -07001571 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001572 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001573 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001574 break;
1575 }
1576
1577out:
1578 return ret;
1579}
1580
Stephen Boydd6968fc2015-04-30 13:54:13 -07001581static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001582 struct clk_core *new_parent, u8 p_index)
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
Stephen Boydd6968fc2015-04-30 13:54:13 -07001586 core->new_rate = new_rate;
1587 core->new_parent = new_parent;
1588 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001589 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001590 core->new_child = NULL;
1591 if (new_parent && new_parent != core->parent)
1592 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001593
Stephen Boydd6968fc2015-04-30 13:54:13 -07001594 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001595 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001596 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001597 }
1598}
1599
1600/*
1601 * calculate the new rates returning the topmost clock that has to be
1602 * changed.
1603 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001604static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001605 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001606{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001607 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001608 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001609 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001610 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001611 unsigned long min_rate;
1612 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001613 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001614 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001615
Mike Turquette7452b212012-03-26 14:45:36 -07001616 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001617 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001618 return NULL;
1619
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001620 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001621 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001622 if (parent)
1623 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001624
Stephen Boydd6968fc2015-04-30 13:54:13 -07001625 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001626
James Hogan71472c02013-07-29 12:25:00 +01001627 /* find the closest rate and parent clk/rate */
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001628 if (clk_core_can_round(core)) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001629 struct clk_rate_request req;
1630
1631 req.rate = rate;
1632 req.min_rate = min_rate;
1633 req.max_rate = max_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001634
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001635 clk_core_init_rate_req(core, &req);
1636
1637 ret = clk_core_determine_round_nolock(core, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001638 if (ret < 0)
1639 return NULL;
1640
Boris Brezillon0817b622015-07-07 20:48:08 +02001641 best_parent_rate = req.best_parent_rate;
1642 new_rate = req.rate;
1643 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001644
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001645 if (new_rate < min_rate || new_rate > max_rate)
1646 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001647 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001648 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001649 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001650 return NULL;
1651 } else {
1652 /* pass-through clock with adjustable parent */
1653 top = clk_calc_new_rates(parent, rate);
1654 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001655 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001656 }
1657
James Hogan71472c02013-07-29 12:25:00 +01001658 /* some clocks must be gated to change parent */
1659 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001660 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001661 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001662 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001663 return NULL;
1664 }
1665
James Hogan71472c02013-07-29 12:25:00 +01001666 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001667 if (parent && core->num_parents > 1) {
1668 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001669 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001670 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001671 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001672 return NULL;
1673 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001674 }
1675
Stephen Boydd6968fc2015-04-30 13:54:13 -07001676 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001677 best_parent_rate != parent->rate)
1678 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001679
1680out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001681 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001682
1683 return top;
1684}
1685
1686/*
1687 * Notify about rate changes in a subtree. Always walk down the whole tree
1688 * so that in case of an error we can walk down the whole tree again and
1689 * abort the change.
1690 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001691static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001692 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001693{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001694 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001695 int ret = NOTIFY_DONE;
1696
Stephen Boydd6968fc2015-04-30 13:54:13 -07001697 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301698 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001699
Stephen Boydd6968fc2015-04-30 13:54:13 -07001700 if (core->notifier_count) {
1701 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001702 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001703 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001704 }
1705
Stephen Boydd6968fc2015-04-30 13:54:13 -07001706 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001707 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001708 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001709 continue;
1710 tmp_clk = clk_propagate_rate_change(child, event);
1711 if (tmp_clk)
1712 fail_clk = tmp_clk;
1713 }
1714
Stephen Boydd6968fc2015-04-30 13:54:13 -07001715 /* handle the new child who might not be in core->children yet */
1716 if (core->new_child) {
1717 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001718 if (tmp_clk)
1719 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001720 }
1721
1722 return fail_clk;
1723}
1724
1725/*
1726 * walk down a subtree and set the new rates notifying the rate
1727 * change on the way
1728 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001729static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001730{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001731 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001732 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001733 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001734 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001735 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001736 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001737 struct clk_core *parent = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001738
Stephen Boydd6968fc2015-04-30 13:54:13 -07001739 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001740
Dong Aishengfc8726a2016-06-30 17:31:14 +08001741 if (core->new_parent) {
1742 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001743 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001744 } else if (core->parent) {
1745 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001746 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001747 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001748
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001749 if (core->flags & CLK_SET_RATE_UNGATE) {
1750 unsigned long flags;
1751
1752 clk_core_prepare(core);
1753 flags = clk_enable_lock();
1754 clk_core_enable(core);
1755 clk_enable_unlock(flags);
1756 }
1757
Stephen Boydd6968fc2015-04-30 13:54:13 -07001758 if (core->new_parent && core->new_parent != core->parent) {
1759 old_parent = __clk_set_parent_before(core, core->new_parent);
1760 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001761
Stephen Boydd6968fc2015-04-30 13:54:13 -07001762 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001763 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001764 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001765 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001766 core->new_parent_index);
1767 } else if (core->ops->set_parent) {
1768 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001769 }
1770
Stephen Boydd6968fc2015-04-30 13:54:13 -07001771 trace_clk_set_parent_complete(core, core->new_parent);
1772 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001773 }
1774
Dong Aishengfc8726a2016-06-30 17:31:14 +08001775 if (core->flags & CLK_OPS_PARENT_ENABLE)
1776 clk_core_prepare_enable(parent);
1777
Stephen Boydd6968fc2015-04-30 13:54:13 -07001778 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001779
Stephen Boydd6968fc2015-04-30 13:54:13 -07001780 if (!skip_set_rate && core->ops->set_rate)
1781 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001782
Stephen Boydd6968fc2015-04-30 13:54:13 -07001783 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001784
Stephen Boydd6968fc2015-04-30 13:54:13 -07001785 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001786
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001787 if (core->flags & CLK_SET_RATE_UNGATE) {
1788 unsigned long flags;
1789
1790 flags = clk_enable_lock();
1791 clk_core_disable(core);
1792 clk_enable_unlock(flags);
1793 clk_core_unprepare(core);
1794 }
1795
Dong Aishengfc8726a2016-06-30 17:31:14 +08001796 if (core->flags & CLK_OPS_PARENT_ENABLE)
1797 clk_core_disable_unprepare(parent);
1798
Stephen Boydd6968fc2015-04-30 13:54:13 -07001799 if (core->notifier_count && old_rate != core->rate)
1800 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001801
Michael Turquette85e88fa2015-06-20 12:18:03 -07001802 if (core->flags & CLK_RECALC_NEW_RATES)
1803 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001804
Tero Kristo067bb172014-08-21 16:47:45 +03001805 /*
1806 * Use safe iteration, as change_rate can actually swap parents
1807 * for certain clock types.
1808 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001809 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001810 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001811 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001812 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001813 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001814 }
1815
Stephen Boydd6968fc2015-04-30 13:54:13 -07001816 /* handle the new child who might not be in core->children yet */
1817 if (core->new_child)
1818 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001819}
1820
Jerome Brunetca5e0892017-12-01 22:51:55 +01001821static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1822 unsigned long req_rate)
1823{
Jerome Brunete55a8392017-12-01 22:51:56 +01001824 int ret, cnt;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001825 struct clk_rate_request req;
1826
1827 lockdep_assert_held(&prepare_lock);
1828
1829 if (!core)
1830 return 0;
1831
Jerome Brunete55a8392017-12-01 22:51:56 +01001832 /* simulate what the rate would be if it could be freely set */
1833 cnt = clk_core_rate_nuke_protect(core);
1834 if (cnt < 0)
1835 return cnt;
1836
Jerome Brunetca5e0892017-12-01 22:51:55 +01001837 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1838 req.rate = req_rate;
1839
1840 ret = clk_core_round_rate_nolock(core, &req);
1841
Jerome Brunete55a8392017-12-01 22:51:56 +01001842 /* restore the protection */
1843 clk_core_rate_restore_protect(core, cnt);
1844
Jerome Brunetca5e0892017-12-01 22:51:55 +01001845 return ret ? 0 : req.rate;
1846}
1847
Stephen Boydd6968fc2015-04-30 13:54:13 -07001848static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001849 unsigned long req_rate)
1850{
1851 struct clk_core *top, *fail_clk;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001852 unsigned long rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001853 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001854
Stephen Boydd6968fc2015-04-30 13:54:13 -07001855 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001856 return 0;
1857
Jerome Brunetca5e0892017-12-01 22:51:55 +01001858 rate = clk_core_req_round_rate_nolock(core, req_rate);
1859
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001860 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001861 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001862 return 0;
1863
Jerome Brunete55a8392017-12-01 22:51:56 +01001864 /* fail on a direct rate set of a protected provider */
1865 if (clk_core_rate_is_protected(core))
1866 return -EBUSY;
1867
Stephen Boydd6968fc2015-04-30 13:54:13 -07001868 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001869 return -EBUSY;
1870
1871 /* calculate new rates and get the topmost changed clock */
Jerome Brunetca5e0892017-12-01 22:51:55 +01001872 top = clk_calc_new_rates(core, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001873 if (!top)
1874 return -EINVAL;
1875
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001876 ret = clk_pm_runtime_get(core);
1877 if (ret)
1878 return ret;
1879
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001880 /* notify that we are about to change rates */
1881 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1882 if (fail_clk) {
1883 pr_debug("%s: failed to set %s rate\n", __func__,
1884 fail_clk->name);
1885 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001886 ret = -EBUSY;
1887 goto err;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001888 }
1889
1890 /* change the rates */
1891 clk_change_rate(top);
1892
Stephen Boydd6968fc2015-04-30 13:54:13 -07001893 core->req_rate = req_rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001894err:
1895 clk_pm_runtime_put(core);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001896
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001897 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001898}
1899
Mike Turquetteb24764902012-03-15 23:11:19 -07001900/**
1901 * clk_set_rate - specify a new rate for clk
1902 * @clk: the clk whose rate is being changed
1903 * @rate: the new rate for clk
1904 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001905 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001906 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001907 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1908 * propagate up to clk's parent; whether or not this happens depends on the
1909 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1910 * after calling .round_rate then upstream parent propagation is ignored. If
1911 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001912 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001913 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1914 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001915 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001916 * Rate changes are accomplished via tree traversal that also recalculates the
1917 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001918 *
1919 * Returns 0 on success, -EERROR otherwise.
1920 */
1921int clk_set_rate(struct clk *clk, unsigned long rate)
1922{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001923 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001924
Mike Turquette89ac8d72013-08-21 23:58:09 -07001925 if (!clk)
1926 return 0;
1927
Mike Turquetteb24764902012-03-15 23:11:19 -07001928 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001929 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001930
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001931 if (clk->exclusive_count)
1932 clk_core_rate_unprotect(clk->core);
1933
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001934 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001935
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001936 if (clk->exclusive_count)
1937 clk_core_rate_protect(clk->core);
1938
Mike Turquetteeab89f62013-03-28 13:59:01 -07001939 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001940
1941 return ret;
1942}
1943EXPORT_SYMBOL_GPL(clk_set_rate);
1944
1945/**
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001946 * clk_set_rate_exclusive - specify a new rate get exclusive control
1947 * @clk: the clk whose rate is being changed
1948 * @rate: the new rate for clk
1949 *
1950 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
1951 * within a critical section
1952 *
1953 * This can be used initially to ensure that at least 1 consumer is
1954 * statisfied when several consumers are competing for exclusivity over the
1955 * same clock provider.
1956 *
1957 * The exclusivity is not applied if setting the rate failed.
1958 *
1959 * Calls to clk_rate_exclusive_get() should be balanced with calls to
1960 * clk_rate_exclusive_put().
1961 *
1962 * Returns 0 on success, -EERROR otherwise.
1963 */
1964int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
1965{
1966 int ret;
1967
1968 if (!clk)
1969 return 0;
1970
1971 /* prevent racing with updates to the clock topology */
1972 clk_prepare_lock();
1973
1974 /*
1975 * The temporary protection removal is not here, on purpose
1976 * This function is meant to be used instead of clk_rate_protect,
1977 * so before the consumer code path protect the clock provider
1978 */
1979
1980 ret = clk_core_set_rate_nolock(clk->core, rate);
1981 if (!ret) {
1982 clk_core_rate_protect(clk->core);
1983 clk->exclusive_count++;
1984 }
1985
1986 clk_prepare_unlock();
1987
1988 return ret;
1989}
1990EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
1991
1992/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001993 * clk_set_rate_range - set a rate range for a clock source
1994 * @clk: clock source
1995 * @min: desired minimum clock rate in Hz, inclusive
1996 * @max: desired maximum clock rate in Hz, inclusive
1997 *
1998 * Returns success (0) or negative errno.
1999 */
2000int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2001{
2002 int ret = 0;
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002003 unsigned long old_min, old_max, rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002004
2005 if (!clk)
2006 return 0;
2007
2008 if (min > max) {
2009 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2010 __func__, clk->core->name, clk->dev_id, clk->con_id,
2011 min, max);
2012 return -EINVAL;
2013 }
2014
2015 clk_prepare_lock();
2016
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002017 if (clk->exclusive_count)
2018 clk_core_rate_unprotect(clk->core);
2019
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002020 /* Save the current values in case we need to rollback the change */
2021 old_min = clk->min_rate;
2022 old_max = clk->max_rate;
2023 clk->min_rate = min;
2024 clk->max_rate = max;
2025
2026 rate = clk_core_get_rate_nolock(clk->core);
2027 if (rate < min || rate > max) {
2028 /*
2029 * FIXME:
2030 * We are in bit of trouble here, current rate is outside the
2031 * the requested range. We are going try to request appropriate
2032 * range boundary but there is a catch. It may fail for the
2033 * usual reason (clock broken, clock protected, etc) but also
2034 * because:
2035 * - round_rate() was not favorable and fell on the wrong
2036 * side of the boundary
2037 * - the determine_rate() callback does not really check for
2038 * this corner case when determining the rate
2039 */
2040
2041 if (rate < min)
2042 rate = min;
2043 else
2044 rate = max;
2045
2046 ret = clk_core_set_rate_nolock(clk->core, rate);
2047 if (ret) {
2048 /* rollback the changes */
2049 clk->min_rate = old_min;
2050 clk->max_rate = old_max;
2051 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002052 }
2053
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002054 if (clk->exclusive_count)
2055 clk_core_rate_protect(clk->core);
2056
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002057 clk_prepare_unlock();
2058
2059 return ret;
2060}
2061EXPORT_SYMBOL_GPL(clk_set_rate_range);
2062
2063/**
2064 * clk_set_min_rate - set a minimum clock rate for a clock source
2065 * @clk: clock source
2066 * @rate: desired minimum clock rate in Hz, inclusive
2067 *
2068 * Returns success (0) or negative errno.
2069 */
2070int clk_set_min_rate(struct clk *clk, unsigned long rate)
2071{
2072 if (!clk)
2073 return 0;
2074
2075 return clk_set_rate_range(clk, rate, clk->max_rate);
2076}
2077EXPORT_SYMBOL_GPL(clk_set_min_rate);
2078
2079/**
2080 * clk_set_max_rate - set a maximum clock rate for a clock source
2081 * @clk: clock source
2082 * @rate: desired maximum clock rate in Hz, inclusive
2083 *
2084 * Returns success (0) or negative errno.
2085 */
2086int clk_set_max_rate(struct clk *clk, unsigned long rate)
2087{
2088 if (!clk)
2089 return 0;
2090
2091 return clk_set_rate_range(clk, clk->min_rate, rate);
2092}
2093EXPORT_SYMBOL_GPL(clk_set_max_rate);
2094
2095/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002096 * clk_get_parent - return the parent of a clk
2097 * @clk: the clk whose parent gets returned
2098 *
2099 * Simply returns clk->parent. Returns NULL if clk is NULL.
2100 */
2101struct clk *clk_get_parent(struct clk *clk)
2102{
2103 struct clk *parent;
2104
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002105 if (!clk)
2106 return NULL;
2107
Mike Turquetteeab89f62013-03-28 13:59:01 -07002108 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002109 /* TODO: Create a per-user clk and change callers to call clk_put */
2110 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002111 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002112
2113 return parent;
2114}
2115EXPORT_SYMBOL_GPL(clk_get_parent);
2116
Stephen Boydd6968fc2015-04-30 13:54:13 -07002117static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002118{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002119 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002120
Masahiro Yamada2430a942016-02-09 20:19:14 +09002121 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002122 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002123
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002124 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002125}
2126
Stephen Boydd6968fc2015-04-30 13:54:13 -07002127static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002128 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002129{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002130 clk_reparent(core, new_parent);
2131 __clk_recalc_accuracies(core);
2132 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002133}
2134
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002135void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2136{
2137 if (!hw)
2138 return;
2139
2140 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2141}
2142
Mike Turquetteb24764902012-03-15 23:11:19 -07002143/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002144 * clk_has_parent - check if a clock is a possible parent for another
2145 * @clk: clock source
2146 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002147 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002148 * This function can be used in drivers that need to check that a clock can be
2149 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002150 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002151 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002152 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002153bool clk_has_parent(struct clk *clk, struct clk *parent)
2154{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002155 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002156 unsigned int i;
2157
2158 /* NULL clocks should be nops, so return success if either is NULL. */
2159 if (!clk || !parent)
2160 return true;
2161
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002162 core = clk->core;
2163 parent_core = parent->core;
2164
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002165 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002166 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002167 return true;
2168
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002169 for (i = 0; i < core->num_parents; i++)
2170 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002171 return true;
2172
2173 return false;
2174}
2175EXPORT_SYMBOL_GPL(clk_has_parent);
2176
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002177static int clk_core_set_parent_nolock(struct clk_core *core,
2178 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002179{
2180 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002181 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002182 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002183
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002184 lockdep_assert_held(&prepare_lock);
2185
Stephen Boydd6968fc2015-04-30 13:54:13 -07002186 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002187 return 0;
2188
Stephen Boydd6968fc2015-04-30 13:54:13 -07002189 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002190 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002191
Stephen Boydb61c43c2015-02-02 14:11:25 -08002192 /* verify ops for for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002193 if (core->num_parents > 1 && !core->ops->set_parent)
2194 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08002195
Ulf Hansson031dcc92013-04-02 23:09:38 +02002196 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002197 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2198 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002199
Jerome Brunete55a8392017-12-01 22:51:56 +01002200 if (clk_core_rate_is_protected(core))
2201 return -EBUSY;
2202
Ulf Hansson031dcc92013-04-02 23:09:38 +02002203 /* try finding the new parent index */
2204 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002205 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002206 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002207 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002208 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002209 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002210 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002211 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002212 }
2213
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002214 ret = clk_pm_runtime_get(core);
2215 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002216 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002217
Mike Turquetteb24764902012-03-15 23:11:19 -07002218 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002219 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002220
2221 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002222 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002223 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07002224
Ulf Hansson031dcc92013-04-02 23:09:38 +02002225 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002226 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002227
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002228 /* propagate rate an accuracy recalculation accordingly */
2229 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002230 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002231 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002232 __clk_recalc_rates(core, POST_RATE_CHANGE);
2233 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002234 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002235
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002236runtime_put:
2237 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002238
2239 return ret;
2240}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002241
2242/**
2243 * clk_set_parent - switch the parent of a mux clk
2244 * @clk: the mux clk whose input we are switching
2245 * @parent: the new input to clk
2246 *
2247 * Re-parent clk to use parent as its new input source. If clk is in
2248 * prepared state, the clk will get enabled for the duration of this call. If
2249 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2250 * that, the reparenting is glitchy in hardware, etc), use the
2251 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2252 *
2253 * After successfully changing clk's parent clk_set_parent will update the
2254 * clk topology, sysfs topology and propagate rate recalculation via
2255 * __clk_recalc_rates.
2256 *
2257 * Returns 0 on success, -EERROR otherwise.
2258 */
2259int clk_set_parent(struct clk *clk, struct clk *parent)
2260{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002261 int ret;
2262
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002263 if (!clk)
2264 return 0;
2265
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002266 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002267
2268 if (clk->exclusive_count)
2269 clk_core_rate_unprotect(clk->core);
2270
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002271 ret = clk_core_set_parent_nolock(clk->core,
2272 parent ? parent->core : NULL);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002273
2274 if (clk->exclusive_count)
2275 clk_core_rate_protect(clk->core);
2276
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002277 clk_prepare_unlock();
2278
2279 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002280}
Mike Turquetteb24764902012-03-15 23:11:19 -07002281EXPORT_SYMBOL_GPL(clk_set_parent);
2282
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002283static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2284{
2285 int ret = -EINVAL;
2286
2287 lockdep_assert_held(&prepare_lock);
2288
2289 if (!core)
2290 return 0;
2291
Jerome Brunete55a8392017-12-01 22:51:56 +01002292 if (clk_core_rate_is_protected(core))
2293 return -EBUSY;
2294
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002295 trace_clk_set_phase(core, degrees);
2296
2297 if (core->ops->set_phase)
2298 ret = core->ops->set_phase(core->hw, degrees);
2299
2300 trace_clk_set_phase_complete(core, degrees);
2301
2302 return ret;
2303}
2304
Mike Turquetteb24764902012-03-15 23:11:19 -07002305/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002306 * clk_set_phase - adjust the phase shift of a clock signal
2307 * @clk: clock signal source
2308 * @degrees: number of degrees the signal is shifted
2309 *
2310 * Shifts the phase of a clock signal by the specified
2311 * degrees. Returns 0 on success, -EERROR otherwise.
2312 *
2313 * This function makes no distinction about the input or reference
2314 * signal that we adjust the clock signal phase against. For example
2315 * phase locked-loop clock signal generators we may shift phase with
2316 * respect to feedback clock signal input, but for other cases the
2317 * clock phase may be shifted with respect to some other, unspecified
2318 * signal.
2319 *
2320 * Additionally the concept of phase shift does not propagate through
2321 * the clock tree hierarchy, which sets it apart from clock rates and
2322 * clock accuracy. A parent clock phase attribute does not have an
2323 * impact on the phase attribute of a child clock.
2324 */
2325int clk_set_phase(struct clk *clk, int degrees)
2326{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002327 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002328
2329 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002330 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002331
2332 /* sanity check degrees */
2333 degrees %= 360;
2334 if (degrees < 0)
2335 degrees += 360;
2336
2337 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002338
2339 if (clk->exclusive_count)
2340 clk_core_rate_unprotect(clk->core);
2341
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002342 ret = clk_core_set_phase_nolock(clk->core, degrees);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002343
2344 if (clk->exclusive_count)
2345 clk_core_rate_protect(clk->core);
2346
Mike Turquettee59c5372014-02-18 21:21:25 -08002347 clk_prepare_unlock();
2348
Mike Turquettee59c5372014-02-18 21:21:25 -08002349 return ret;
2350}
Maxime Ripard9767b042015-01-20 22:23:43 +01002351EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002352
Stephen Boydd6968fc2015-04-30 13:54:13 -07002353static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002354{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002355 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002356
2357 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07002358 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002359 clk_prepare_unlock();
2360
Mike Turquettee59c5372014-02-18 21:21:25 -08002361 return ret;
2362}
2363
2364/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002365 * clk_get_phase - return the phase shift of a clock signal
2366 * @clk: clock signal source
2367 *
2368 * Returns the phase shift of a clock node in degrees, otherwise returns
2369 * -EERROR.
2370 */
2371int clk_get_phase(struct clk *clk)
2372{
2373 if (!clk)
2374 return 0;
2375
2376 return clk_core_get_phase(clk->core);
2377}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002378EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002379
2380/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002381 * clk_is_match - check if two clk's point to the same hardware clock
2382 * @p: clk compared against q
2383 * @q: clk compared against p
2384 *
2385 * Returns true if the two struct clk pointers both point to the same hardware
2386 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2387 * share the same struct clk_core object.
2388 *
2389 * Returns false otherwise. Note that two NULL clks are treated as matching.
2390 */
2391bool clk_is_match(const struct clk *p, const struct clk *q)
2392{
2393 /* trivial case: identical struct clk's or both NULL */
2394 if (p == q)
2395 return true;
2396
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002397 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002398 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2399 if (p->core == q->core)
2400 return true;
2401
2402 return false;
2403}
2404EXPORT_SYMBOL_GPL(clk_is_match);
2405
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002406/*** debugfs support ***/
2407
2408#ifdef CONFIG_DEBUG_FS
2409#include <linux/debugfs.h>
2410
2411static struct dentry *rootdir;
2412static int inited = 0;
2413static DEFINE_MUTEX(clk_debug_lock);
2414static HLIST_HEAD(clk_debug_list);
2415
2416static struct hlist_head *all_lists[] = {
2417 &clk_root_list,
2418 &clk_orphan_list,
2419 NULL,
2420};
2421
2422static struct hlist_head *orphan_list[] = {
2423 &clk_orphan_list,
2424 NULL,
2425};
2426
2427static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2428 int level)
2429{
2430 if (!c)
2431 return;
2432
Jerome Brunetc5ce26e2017-12-01 22:51:57 +01002433 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %-3d\n",
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002434 level * 3 + 1, "",
2435 30 - level * 3, c->name,
Jerome Brunete55a8392017-12-01 22:51:56 +01002436 c->enable_count, c->prepare_count, c->protect_count,
2437 clk_core_get_rate(c), clk_core_get_accuracy(c),
2438 clk_core_get_phase(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002439}
2440
2441static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2442 int level)
2443{
2444 struct clk_core *child;
2445
2446 if (!c)
2447 return;
2448
2449 clk_summary_show_one(s, c, level);
2450
2451 hlist_for_each_entry(child, &c->children, child_node)
2452 clk_summary_show_subtree(s, child, level + 1);
2453}
2454
2455static int clk_summary_show(struct seq_file *s, void *data)
2456{
2457 struct clk_core *c;
2458 struct hlist_head **lists = (struct hlist_head **)s->private;
2459
Jerome Brunetc5ce26e2017-12-01 22:51:57 +01002460 seq_puts(s, " enable prepare protect \n");
2461 seq_puts(s, " clock count count count rate accuracy phase\n");
2462 seq_puts(s, "----------------------------------------------------------------------------------------\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002463
2464 clk_prepare_lock();
2465
2466 for (; *lists; lists++)
2467 hlist_for_each_entry(c, *lists, child_node)
2468 clk_summary_show_subtree(s, c, 0);
2469
2470 clk_prepare_unlock();
2471
2472 return 0;
2473}
2474
2475
2476static int clk_summary_open(struct inode *inode, struct file *file)
2477{
2478 return single_open(file, clk_summary_show, inode->i_private);
2479}
2480
2481static const struct file_operations clk_summary_fops = {
2482 .open = clk_summary_open,
2483 .read = seq_read,
2484 .llseek = seq_lseek,
2485 .release = single_release,
2486};
2487
2488static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2489{
2490 if (!c)
2491 return;
2492
Stefan Wahren7cb81132015-04-29 16:36:43 +00002493 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002494 seq_printf(s, "\"%s\": { ", c->name);
2495 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2496 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002497 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002498 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2499 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002500 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2501}
2502
2503static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2504{
2505 struct clk_core *child;
2506
2507 if (!c)
2508 return;
2509
2510 clk_dump_one(s, c, level);
2511
2512 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002513 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002514 clk_dump_subtree(s, child, level + 1);
2515 }
2516
Markus Elfring4d327582017-04-20 08:45:43 +02002517 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002518}
2519
2520static int clk_dump(struct seq_file *s, void *data)
2521{
2522 struct clk_core *c;
2523 bool first_node = true;
2524 struct hlist_head **lists = (struct hlist_head **)s->private;
2525
Markus Elfring4d327582017-04-20 08:45:43 +02002526 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002527 clk_prepare_lock();
2528
2529 for (; *lists; lists++) {
2530 hlist_for_each_entry(c, *lists, child_node) {
2531 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02002532 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002533 first_node = false;
2534 clk_dump_subtree(s, c, 0);
2535 }
2536 }
2537
2538 clk_prepare_unlock();
2539
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002540 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002541 return 0;
2542}
2543
2544
2545static int clk_dump_open(struct inode *inode, struct file *file)
2546{
2547 return single_open(file, clk_dump, inode->i_private);
2548}
2549
2550static const struct file_operations clk_dump_fops = {
2551 .open = clk_dump_open,
2552 .read = seq_read,
2553 .llseek = seq_lseek,
2554 .release = single_release,
2555};
2556
Peter De Schrijver92031572017-03-21 15:20:31 +02002557static int possible_parents_dump(struct seq_file *s, void *data)
2558{
2559 struct clk_core *core = s->private;
2560 int i;
2561
2562 for (i = 0; i < core->num_parents - 1; i++)
2563 seq_printf(s, "%s ", core->parent_names[i]);
2564
2565 seq_printf(s, "%s\n", core->parent_names[i]);
2566
2567 return 0;
2568}
2569
2570static int possible_parents_open(struct inode *inode, struct file *file)
2571{
2572 return single_open(file, possible_parents_dump, inode->i_private);
2573}
2574
2575static const struct file_operations possible_parents_fops = {
2576 .open = possible_parents_open,
2577 .read = seq_read,
2578 .llseek = seq_lseek,
2579 .release = single_release,
2580};
2581
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002582static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2583{
2584 struct dentry *d;
2585 int ret = -ENOMEM;
2586
2587 if (!core || !pdentry) {
2588 ret = -EINVAL;
2589 goto out;
2590 }
2591
2592 d = debugfs_create_dir(core->name, pdentry);
2593 if (!d)
2594 goto out;
2595
2596 core->dentry = d;
2597
2598 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2599 (u32 *)&core->rate);
2600 if (!d)
2601 goto err_out;
2602
2603 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2604 (u32 *)&core->accuracy);
2605 if (!d)
2606 goto err_out;
2607
2608 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2609 (u32 *)&core->phase);
2610 if (!d)
2611 goto err_out;
2612
2613 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2614 (u32 *)&core->flags);
2615 if (!d)
2616 goto err_out;
2617
2618 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2619 (u32 *)&core->prepare_count);
2620 if (!d)
2621 goto err_out;
2622
2623 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2624 (u32 *)&core->enable_count);
2625 if (!d)
2626 goto err_out;
2627
Jerome Brunete55a8392017-12-01 22:51:56 +01002628 d = debugfs_create_u32("clk_protect_count", S_IRUGO, core->dentry,
2629 (u32 *)&core->protect_count);
2630 if (!d)
2631 goto err_out;
2632
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002633 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2634 (u32 *)&core->notifier_count);
2635 if (!d)
2636 goto err_out;
2637
Peter De Schrijver92031572017-03-21 15:20:31 +02002638 if (core->num_parents > 1) {
2639 d = debugfs_create_file("clk_possible_parents", S_IRUGO,
2640 core->dentry, core, &possible_parents_fops);
2641 if (!d)
2642 goto err_out;
2643 }
2644
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002645 if (core->ops->debug_init) {
2646 ret = core->ops->debug_init(core->hw, core->dentry);
2647 if (ret)
2648 goto err_out;
2649 }
2650
2651 ret = 0;
2652 goto out;
2653
2654err_out:
2655 debugfs_remove_recursive(core->dentry);
2656 core->dentry = NULL;
2657out:
2658 return ret;
2659}
2660
2661/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002662 * clk_debug_register - add a clk node to the debugfs clk directory
2663 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002664 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002665 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2666 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002667 * will be created lazily by clk_debug_init as part of a late_initcall.
2668 */
2669static int clk_debug_register(struct clk_core *core)
2670{
2671 int ret = 0;
2672
2673 mutex_lock(&clk_debug_lock);
2674 hlist_add_head(&core->debug_node, &clk_debug_list);
2675
2676 if (!inited)
2677 goto unlock;
2678
2679 ret = clk_debug_create_one(core, rootdir);
2680unlock:
2681 mutex_unlock(&clk_debug_lock);
2682
2683 return ret;
2684}
2685
2686 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002687 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2688 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002689 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002690 * Dynamically removes a clk and all its child nodes from the
2691 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08002692 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002693 */
2694static void clk_debug_unregister(struct clk_core *core)
2695{
2696 mutex_lock(&clk_debug_lock);
2697 hlist_del_init(&core->debug_node);
2698 debugfs_remove_recursive(core->dentry);
2699 core->dentry = NULL;
2700 mutex_unlock(&clk_debug_lock);
2701}
2702
2703struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2704 void *data, const struct file_operations *fops)
2705{
2706 struct dentry *d = NULL;
2707
2708 if (hw->core->dentry)
2709 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2710 fops);
2711
2712 return d;
2713}
2714EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2715
2716/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002717 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002718 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002719 * clks are often initialized very early during boot before memory can be
2720 * dynamically allocated and well before debugfs is setup. This function
2721 * populates the debugfs clk directory once at boot-time when we know that
2722 * debugfs is setup. It should only be called once at boot-time, all other clks
2723 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002724 */
2725static int __init clk_debug_init(void)
2726{
2727 struct clk_core *core;
2728 struct dentry *d;
2729
2730 rootdir = debugfs_create_dir("clk", NULL);
2731
2732 if (!rootdir)
2733 return -ENOMEM;
2734
2735 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2736 &clk_summary_fops);
2737 if (!d)
2738 return -ENOMEM;
2739
2740 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2741 &clk_dump_fops);
2742 if (!d)
2743 return -ENOMEM;
2744
2745 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2746 &orphan_list, &clk_summary_fops);
2747 if (!d)
2748 return -ENOMEM;
2749
2750 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2751 &orphan_list, &clk_dump_fops);
2752 if (!d)
2753 return -ENOMEM;
2754
2755 mutex_lock(&clk_debug_lock);
2756 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2757 clk_debug_create_one(core, rootdir);
2758
2759 inited = 1;
2760 mutex_unlock(&clk_debug_lock);
2761
2762 return 0;
2763}
2764late_initcall(clk_debug_init);
2765#else
2766static inline int clk_debug_register(struct clk_core *core) { return 0; }
2767static inline void clk_debug_reparent(struct clk_core *core,
2768 struct clk_core *new_parent)
2769{
2770}
2771static inline void clk_debug_unregister(struct clk_core *core)
2772{
2773}
2774#endif
2775
Michael Turquette3d3801e2015-02-25 09:11:01 -08002776/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002777 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002778 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07002779 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002780 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002781 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002782 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002783static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002784{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002785 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002786 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002787 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002788 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002789
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002790 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07002791 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002792
Mike Turquetteeab89f62013-03-28 13:59:01 -07002793 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002794
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002795 ret = clk_pm_runtime_get(core);
2796 if (ret)
2797 goto unlock;
2798
Mike Turquetteb24764902012-03-15 23:11:19 -07002799 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002800 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002801 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002802 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002803 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002804 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002805 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002806
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002807 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002808 if (core->ops->set_rate &&
2809 !((core->ops->round_rate || core->ops->determine_rate) &&
2810 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002811 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2812 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002813 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002814 goto out;
2815 }
2816
Stephen Boydd6968fc2015-04-30 13:54:13 -07002817 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002818 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2819 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002820 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002821 goto out;
2822 }
2823
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09002824 if (core->num_parents > 1 && !core->ops->get_parent) {
2825 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2826 __func__, core->name);
2827 ret = -EINVAL;
2828 goto out;
2829 }
2830
Stephen Boydd6968fc2015-04-30 13:54:13 -07002831 if (core->ops->set_rate_and_parent &&
2832 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002833 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002834 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002835 ret = -EINVAL;
2836 goto out;
2837 }
2838
Mike Turquetteb24764902012-03-15 23:11:19 -07002839 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002840 for (i = 0; i < core->num_parents; i++)
2841 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002842 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002843 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002844
Stephen Boydd6968fc2015-04-30 13:54:13 -07002845 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002846
2847 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08002848 * Populate core->parent if parent has already been clk_core_init'd. If
2849 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002850 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07002851 * clk list.
2852 *
2853 * Every time a new clk is clk_init'd then we walk the list of orphan
2854 * clocks and re-parent any that are children of the clock currently
2855 * being clk_init'd.
2856 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02002857 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002858 hlist_add_head(&core->child_node,
2859 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002860 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002861 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002862 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002863 core->orphan = false;
2864 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002865 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002866 core->orphan = true;
2867 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002868
2869 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002870 * Set clk's accuracy. The preferred method is to use
2871 * .recalc_accuracy. For simple clocks and lazy developers the default
2872 * fallback is to use the parent's accuracy. If a clock doesn't have a
2873 * parent (or is orphaned) then accuracy is set to zero (perfect
2874 * clock).
2875 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002876 if (core->ops->recalc_accuracy)
2877 core->accuracy = core->ops->recalc_accuracy(core->hw,
2878 __clk_get_accuracy(core->parent));
2879 else if (core->parent)
2880 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002881 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002882 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002883
2884 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002885 * Set clk's phase.
2886 * Since a phase is by definition relative to its parent, just
2887 * query the current clock phase, or just assume it's in phase.
2888 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002889 if (core->ops->get_phase)
2890 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002891 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002892 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002893
2894 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002895 * Set clk's rate. The preferred method is to use .recalc_rate. For
2896 * simple clocks and lazy developers the default fallback is to use the
2897 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2898 * then rate is set to zero.
2899 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002900 if (core->ops->recalc_rate)
2901 rate = core->ops->recalc_rate(core->hw,
2902 clk_core_get_rate_nolock(core->parent));
2903 else if (core->parent)
2904 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002905 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002906 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002907 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002908
2909 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002910 * walk the list of orphan clocks and reparent any that newly finds a
2911 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07002912 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002913 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002914 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002915
Michael Turquette904e6ea2016-07-08 16:32:10 -07002916 /*
2917 * we could call __clk_set_parent, but that would result in a
2918 * redundant call to the .set_rate op, if it exists
2919 */
2920 if (parent) {
2921 __clk_set_parent_before(orphan, parent);
2922 __clk_set_parent_after(orphan, parent, NULL);
2923 __clk_recalc_accuracies(orphan);
2924 __clk_recalc_rates(orphan, 0);
2925 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002926 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002927
2928 /*
2929 * optional platform-specific magic
2930 *
2931 * The .init callback is not used by any of the basic clock types, but
2932 * exists for weird hardware that must perform initialization magic.
2933 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002934 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002935 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002936 if (core->ops->init)
2937 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002938
Lee Jones32b9b102016-02-11 13:19:09 -08002939 if (core->flags & CLK_IS_CRITICAL) {
Maxime Ripardef56b792016-05-13 10:00:31 +02002940 unsigned long flags;
2941
Lee Jones32b9b102016-02-11 13:19:09 -08002942 clk_core_prepare(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02002943
2944 flags = clk_enable_lock();
Lee Jones32b9b102016-02-11 13:19:09 -08002945 clk_core_enable(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02002946 clk_enable_unlock(flags);
Lee Jones32b9b102016-02-11 13:19:09 -08002947 }
2948
Stephen Boydd6968fc2015-04-30 13:54:13 -07002949 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002950out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002951 clk_pm_runtime_put(core);
2952unlock:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002953 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002954
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002955 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002956 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002957
Mike Turquetted1302a32012-03-29 14:30:40 -07002958 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002959}
2960
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002961struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2962 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002963{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002964 struct clk *clk;
2965
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002966 /* This is to allow this function to be chained to others */
Masahiro Yamadac1de1352015-11-20 14:38:49 +09002967 if (IS_ERR_OR_NULL(hw))
Masahiro Yamada8a231332016-07-19 16:28:47 +09002968 return ERR_CAST(hw);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002969
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002970 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2971 if (!clk)
2972 return ERR_PTR(-ENOMEM);
2973
2974 clk->core = hw->core;
2975 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02002976 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002977 clk->max_rate = ULONG_MAX;
2978
2979 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002980 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002981 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002982
2983 return clk;
2984}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002985
Stephen Boyd73e0e492015-02-06 11:42:43 -08002986void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002987{
2988 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002989 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002990 clk_prepare_unlock();
2991
Leonard Crestez253160a2017-02-20 15:20:56 +02002992 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002993 kfree(clk);
2994}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002995
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002996/**
2997 * clk_register - allocate a new clock, register it and return an opaque cookie
2998 * @dev: device that is registering this clock
2999 * @hw: link to hardware-specific clock data
3000 *
3001 * clk_register is the primary interface for populating the clock tree with new
3002 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05303003 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003004 * rest of the clock API. In the event of an error clk_register will return an
3005 * error code; drivers must test for an error code after calling clk_register.
3006 */
3007struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07003008{
Mike Turquetted1302a32012-03-29 14:30:40 -07003009 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003010 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003011
Stephen Boydd6968fc2015-04-30 13:54:13 -07003012 core = kzalloc(sizeof(*core), GFP_KERNEL);
3013 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003014 ret = -ENOMEM;
3015 goto fail_out;
3016 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003017
Stephen Boydd6968fc2015-04-30 13:54:13 -07003018 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3019 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003020 ret = -ENOMEM;
3021 goto fail_name;
3022 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07003023 core->ops = hw->init->ops;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003024 if (dev && pm_runtime_enabled(dev))
3025 core->dev = dev;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003026 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003027 core->owner = dev->driver->owner;
3028 core->hw = hw;
3029 core->flags = hw->init->flags;
3030 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003031 core->min_rate = 0;
3032 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003033 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003034
Mike Turquetted1302a32012-03-29 14:30:40 -07003035 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003036 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02003037 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003038
Stephen Boydd6968fc2015-04-30 13:54:13 -07003039 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003040 ret = -ENOMEM;
3041 goto fail_parent_names;
3042 }
3043
3044
3045 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003046 for (i = 0; i < core->num_parents; i++) {
3047 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003048 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003049 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003050 ret = -ENOMEM;
3051 goto fail_parent_names_copy;
3052 }
3053 }
3054
Masahiro Yamada176d1162015-12-28 19:23:00 +09003055 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3056 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3057 GFP_KERNEL);
3058 if (!core->parents) {
3059 ret = -ENOMEM;
3060 goto fail_parents;
3061 };
3062
Stephen Boydd6968fc2015-04-30 13:54:13 -07003063 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003064
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003065 hw->clk = __clk_create_clk(hw, NULL, NULL);
3066 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003067 ret = PTR_ERR(hw->clk);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003068 goto fail_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003069 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003070
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003071 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003072 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003073 return hw->clk;
3074
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003075 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003076 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003077
Masahiro Yamada176d1162015-12-28 19:23:00 +09003078fail_parents:
3079 kfree(core->parents);
Mike Turquetted1302a32012-03-29 14:30:40 -07003080fail_parent_names_copy:
3081 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003082 kfree_const(core->parent_names[i]);
3083 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07003084fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003085 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003086fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003087 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003088fail_out:
3089 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003090}
3091EXPORT_SYMBOL_GPL(clk_register);
3092
Stephen Boyd41438042016-02-05 17:02:52 -08003093/**
3094 * clk_hw_register - register a clk_hw and return an error code
3095 * @dev: device that is registering this clock
3096 * @hw: link to hardware-specific clock data
3097 *
3098 * clk_hw_register is the primary interface for populating the clock tree with
3099 * new clock nodes. It returns an integer equal to zero indicating success or
3100 * less than zero indicating failure. Drivers must test for an error code after
3101 * calling clk_hw_register().
3102 */
3103int clk_hw_register(struct device *dev, struct clk_hw *hw)
3104{
3105 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3106}
3107EXPORT_SYMBOL_GPL(clk_hw_register);
3108
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003109/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003110static void __clk_release(struct kref *ref)
3111{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003112 struct clk_core *core = container_of(ref, struct clk_core, ref);
3113 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003114
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003115 lockdep_assert_held(&prepare_lock);
3116
Stephen Boydd6968fc2015-04-30 13:54:13 -07003117 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003118 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003119 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003120
Stephen Boydd6968fc2015-04-30 13:54:13 -07003121 kfree(core->parent_names);
3122 kfree_const(core->name);
3123 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003124}
3125
3126/*
3127 * Empty clk_ops for unregistered clocks. These are used temporarily
3128 * after clk_unregister() was called on a clock and until last clock
3129 * consumer calls clk_put() and the struct clk object is freed.
3130 */
3131static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3132{
3133 return -ENXIO;
3134}
3135
3136static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3137{
3138 WARN_ON_ONCE(1);
3139}
3140
3141static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3142 unsigned long parent_rate)
3143{
3144 return -ENXIO;
3145}
3146
3147static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3148{
3149 return -ENXIO;
3150}
3151
3152static const struct clk_ops clk_nodrv_ops = {
3153 .enable = clk_nodrv_prepare_enable,
3154 .disable = clk_nodrv_disable_unprepare,
3155 .prepare = clk_nodrv_prepare_enable,
3156 .unprepare = clk_nodrv_disable_unprepare,
3157 .set_rate = clk_nodrv_set_rate,
3158 .set_parent = clk_nodrv_set_parent,
3159};
3160
Mark Brown1df5c932012-04-18 09:07:12 +01003161/**
3162 * clk_unregister - unregister a currently registered clock
3163 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003164 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003165void clk_unregister(struct clk *clk)
3166{
3167 unsigned long flags;
3168
Stephen Boyd6314b672014-09-04 23:37:49 -07003169 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3170 return;
3171
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003172 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003173
3174 clk_prepare_lock();
3175
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003176 if (clk->core->ops == &clk_nodrv_ops) {
3177 pr_err("%s: unregistered clock: %s\n", __func__,
3178 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003179 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003180 }
3181 /*
3182 * Assign empty clock ops for consumers that might still hold
3183 * a reference to this clock.
3184 */
3185 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003186 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003187 clk_enable_unlock(flags);
3188
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003189 if (!hlist_empty(&clk->core->children)) {
3190 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003191 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003192
3193 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003194 hlist_for_each_entry_safe(child, t, &clk->core->children,
3195 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01003196 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003197 }
3198
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003199 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003200
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003201 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003202 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003203 __func__, clk->core->name);
Jerome Brunete55a8392017-12-01 22:51:56 +01003204
3205 if (clk->core->protect_count)
3206 pr_warn("%s: unregistering protected clock: %s\n",
3207 __func__, clk->core->name);
3208
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003209 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003210unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003211 clk_prepare_unlock();
3212}
Mark Brown1df5c932012-04-18 09:07:12 +01003213EXPORT_SYMBOL_GPL(clk_unregister);
3214
Stephen Boyd41438042016-02-05 17:02:52 -08003215/**
3216 * clk_hw_unregister - unregister a currently registered clk_hw
3217 * @hw: hardware-specific clock data to unregister
3218 */
3219void clk_hw_unregister(struct clk_hw *hw)
3220{
3221 clk_unregister(hw->clk);
3222}
3223EXPORT_SYMBOL_GPL(clk_hw_unregister);
3224
Stephen Boyd46c87732012-09-24 13:38:04 -07003225static void devm_clk_release(struct device *dev, void *res)
3226{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003227 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003228}
3229
Stephen Boyd41438042016-02-05 17:02:52 -08003230static void devm_clk_hw_release(struct device *dev, void *res)
3231{
3232 clk_hw_unregister(*(struct clk_hw **)res);
3233}
3234
Stephen Boyd46c87732012-09-24 13:38:04 -07003235/**
3236 * devm_clk_register - resource managed clk_register()
3237 * @dev: device that is registering this clock
3238 * @hw: link to hardware-specific clock data
3239 *
3240 * Managed clk_register(). Clocks returned from this function are
3241 * automatically clk_unregister()ed on driver detach. See clk_register() for
3242 * more information.
3243 */
3244struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3245{
3246 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003247 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003248
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003249 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3250 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003251 return ERR_PTR(-ENOMEM);
3252
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003253 clk = clk_register(dev, hw);
3254 if (!IS_ERR(clk)) {
3255 *clkp = clk;
3256 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003257 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003258 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003259 }
3260
3261 return clk;
3262}
3263EXPORT_SYMBOL_GPL(devm_clk_register);
3264
Stephen Boyd41438042016-02-05 17:02:52 -08003265/**
3266 * devm_clk_hw_register - resource managed clk_hw_register()
3267 * @dev: device that is registering this clock
3268 * @hw: link to hardware-specific clock data
3269 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003270 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003271 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3272 * for more information.
3273 */
3274int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3275{
3276 struct clk_hw **hwp;
3277 int ret;
3278
3279 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3280 if (!hwp)
3281 return -ENOMEM;
3282
3283 ret = clk_hw_register(dev, hw);
3284 if (!ret) {
3285 *hwp = hw;
3286 devres_add(dev, hwp);
3287 } else {
3288 devres_free(hwp);
3289 }
3290
3291 return ret;
3292}
3293EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3294
Stephen Boyd46c87732012-09-24 13:38:04 -07003295static int devm_clk_match(struct device *dev, void *res, void *data)
3296{
3297 struct clk *c = res;
3298 if (WARN_ON(!c))
3299 return 0;
3300 return c == data;
3301}
3302
Stephen Boyd41438042016-02-05 17:02:52 -08003303static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3304{
3305 struct clk_hw *hw = res;
3306
3307 if (WARN_ON(!hw))
3308 return 0;
3309 return hw == data;
3310}
3311
Stephen Boyd46c87732012-09-24 13:38:04 -07003312/**
3313 * devm_clk_unregister - resource managed clk_unregister()
3314 * @clk: clock to unregister
3315 *
3316 * Deallocate a clock allocated with devm_clk_register(). Normally
3317 * this function will not need to be called and the resource management
3318 * code will ensure that the resource is freed.
3319 */
3320void devm_clk_unregister(struct device *dev, struct clk *clk)
3321{
3322 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3323}
3324EXPORT_SYMBOL_GPL(devm_clk_unregister);
3325
Stephen Boyd41438042016-02-05 17:02:52 -08003326/**
3327 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3328 * @dev: device that is unregistering the hardware-specific clock data
3329 * @hw: link to hardware-specific clock data
3330 *
3331 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3332 * this function will not need to be called and the resource management
3333 * code will ensure that the resource is freed.
3334 */
3335void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3336{
3337 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3338 hw));
3339}
3340EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3341
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003342/*
3343 * clkdev helpers
3344 */
3345int __clk_get(struct clk *clk)
3346{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003347 struct clk_core *core = !clk ? NULL : clk->core;
3348
3349 if (core) {
3350 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003351 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003352
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003353 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003354 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003355 return 1;
3356}
3357
3358void __clk_put(struct clk *clk)
3359{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003360 struct module *owner;
3361
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003362 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003363 return;
3364
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003365 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003366
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01003367 /*
3368 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3369 * given user should be balanced with calls to clk_rate_exclusive_put()
3370 * and by that same consumer
3371 */
3372 if (WARN_ON(clk->exclusive_count)) {
3373 /* We voiced our concern, let's sanitize the situation */
3374 clk->core->protect_count -= (clk->exclusive_count - 1);
3375 clk_core_rate_unprotect(clk->core);
3376 clk->exclusive_count = 0;
3377 }
3378
Stephen Boyd50595f82015-02-06 11:42:44 -08003379 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003380 if (clk->min_rate > clk->core->req_rate ||
3381 clk->max_rate < clk->core->req_rate)
3382 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3383
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003384 owner = clk->core->owner;
3385 kref_put(&clk->core->ref, __clk_release);
3386
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003387 clk_prepare_unlock();
3388
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003389 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003390
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003391 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003392}
3393
Mike Turquetteb24764902012-03-15 23:11:19 -07003394/*** clk rate change notifiers ***/
3395
3396/**
3397 * clk_notifier_register - add a clk rate change notifier
3398 * @clk: struct clk * to watch
3399 * @nb: struct notifier_block * with callback info
3400 *
3401 * Request notification when clk's rate changes. This uses an SRCU
3402 * notifier because we want it to block and notifier unregistrations are
3403 * uncommon. The callbacks associated with the notifier must not
3404 * re-enter into the clk framework by calling any top-level clk APIs;
3405 * this will cause a nested prepare_lock mutex.
3406 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003407 * In all notification cases (pre, post and abort rate change) the original
3408 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3409 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003410 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003411 * clk_notifier_register() must be called from non-atomic context.
3412 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3413 * allocation failure; otherwise, passes along the return value of
3414 * srcu_notifier_chain_register().
3415 */
3416int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3417{
3418 struct clk_notifier *cn;
3419 int ret = -ENOMEM;
3420
3421 if (!clk || !nb)
3422 return -EINVAL;
3423
Mike Turquetteeab89f62013-03-28 13:59:01 -07003424 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003425
3426 /* search the list of notifiers for this clk */
3427 list_for_each_entry(cn, &clk_notifier_list, node)
3428 if (cn->clk == clk)
3429 break;
3430
3431 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3432 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02003433 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003434 if (!cn)
3435 goto out;
3436
3437 cn->clk = clk;
3438 srcu_init_notifier_head(&cn->notifier_head);
3439
3440 list_add(&cn->node, &clk_notifier_list);
3441 }
3442
3443 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3444
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003445 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003446
3447out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003448 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003449
3450 return ret;
3451}
3452EXPORT_SYMBOL_GPL(clk_notifier_register);
3453
3454/**
3455 * clk_notifier_unregister - remove a clk rate change notifier
3456 * @clk: struct clk *
3457 * @nb: struct notifier_block * with callback info
3458 *
3459 * Request no further notification for changes to 'clk' and frees memory
3460 * allocated in clk_notifier_register.
3461 *
3462 * Returns -EINVAL if called with null arguments; otherwise, passes
3463 * along the return value of srcu_notifier_chain_unregister().
3464 */
3465int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3466{
3467 struct clk_notifier *cn = NULL;
3468 int ret = -EINVAL;
3469
3470 if (!clk || !nb)
3471 return -EINVAL;
3472
Mike Turquetteeab89f62013-03-28 13:59:01 -07003473 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003474
3475 list_for_each_entry(cn, &clk_notifier_list, node)
3476 if (cn->clk == clk)
3477 break;
3478
3479 if (cn->clk == clk) {
3480 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3481
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003482 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07003483
3484 /* XXX the notifier code should handle this better */
3485 if (!cn->notifier_head.head) {
3486 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08003487 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07003488 kfree(cn);
3489 }
3490
3491 } else {
3492 ret = -ENOENT;
3493 }
3494
Mike Turquetteeab89f62013-03-28 13:59:01 -07003495 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003496
3497 return ret;
3498}
3499EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05003500
3501#ifdef CONFIG_OF
3502/**
3503 * struct of_clk_provider - Clock provider registration structure
3504 * @link: Entry in global list of clock providers
3505 * @node: Pointer to device tree node of clock provider
3506 * @get: Get clock callback. Returns NULL or a struct clk for the
3507 * given clock specifier
3508 * @data: context pointer to be passed into @get callback
3509 */
3510struct of_clk_provider {
3511 struct list_head link;
3512
3513 struct device_node *node;
3514 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003515 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05003516 void *data;
3517};
3518
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303519static const struct of_device_id __clk_of_table_sentinel
3520 __used __section(__clk_of_table_end);
3521
Grant Likely766e6a42012-04-09 14:50:06 -05003522static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003523static DEFINE_MUTEX(of_clk_mutex);
3524
Grant Likely766e6a42012-04-09 14:50:06 -05003525struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3526 void *data)
3527{
3528 return data;
3529}
3530EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3531
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003532struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3533{
3534 return data;
3535}
3536EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3537
Shawn Guo494bfec2012-08-22 21:36:27 +08003538struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3539{
3540 struct clk_onecell_data *clk_data = data;
3541 unsigned int idx = clkspec->args[0];
3542
3543 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02003544 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08003545 return ERR_PTR(-EINVAL);
3546 }
3547
3548 return clk_data->clks[idx];
3549}
3550EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3551
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003552struct clk_hw *
3553of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3554{
3555 struct clk_hw_onecell_data *hw_data = data;
3556 unsigned int idx = clkspec->args[0];
3557
3558 if (idx >= hw_data->num) {
3559 pr_err("%s: invalid index %u\n", __func__, idx);
3560 return ERR_PTR(-EINVAL);
3561 }
3562
3563 return hw_data->hws[idx];
3564}
3565EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3566
Grant Likely766e6a42012-04-09 14:50:06 -05003567/**
3568 * of_clk_add_provider() - Register a clock provider for a node
3569 * @np: Device node pointer associated with clock provider
3570 * @clk_src_get: callback for decoding clock
3571 * @data: context pointer for @clk_src_get callback.
3572 */
3573int of_clk_add_provider(struct device_node *np,
3574 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3575 void *data),
3576 void *data)
3577{
3578 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003579 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003580
Markus Elfring1808a322017-04-20 09:30:52 +02003581 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05003582 if (!cp)
3583 return -ENOMEM;
3584
3585 cp->node = of_node_get(np);
3586 cp->data = data;
3587 cp->get = clk_src_get;
3588
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003589 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003590 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003591 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003592 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05003593
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003594 ret = of_clk_set_defaults(np, true);
3595 if (ret < 0)
3596 of_clk_del_provider(np);
3597
3598 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003599}
3600EXPORT_SYMBOL_GPL(of_clk_add_provider);
3601
3602/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003603 * of_clk_add_hw_provider() - Register a clock provider for a node
3604 * @np: Device node pointer associated with clock provider
3605 * @get: callback for decoding clk_hw
3606 * @data: context pointer for @get callback.
3607 */
3608int of_clk_add_hw_provider(struct device_node *np,
3609 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3610 void *data),
3611 void *data)
3612{
3613 struct of_clk_provider *cp;
3614 int ret;
3615
3616 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3617 if (!cp)
3618 return -ENOMEM;
3619
3620 cp->node = of_node_get(np);
3621 cp->data = data;
3622 cp->get_hw = get;
3623
3624 mutex_lock(&of_clk_mutex);
3625 list_add(&cp->link, &of_clk_providers);
3626 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003627 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003628
3629 ret = of_clk_set_defaults(np, true);
3630 if (ret < 0)
3631 of_clk_del_provider(np);
3632
3633 return ret;
3634}
3635EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3636
Stephen Boydaa795c42017-09-01 16:16:40 -07003637static void devm_of_clk_release_provider(struct device *dev, void *res)
3638{
3639 of_clk_del_provider(*(struct device_node **)res);
3640}
3641
3642int devm_of_clk_add_hw_provider(struct device *dev,
3643 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3644 void *data),
3645 void *data)
3646{
3647 struct device_node **ptr, *np;
3648 int ret;
3649
3650 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3651 GFP_KERNEL);
3652 if (!ptr)
3653 return -ENOMEM;
3654
3655 np = dev->of_node;
3656 ret = of_clk_add_hw_provider(np, get, data);
3657 if (!ret) {
3658 *ptr = np;
3659 devres_add(dev, ptr);
3660 } else {
3661 devres_free(ptr);
3662 }
3663
3664 return ret;
3665}
3666EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3667
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003668/**
Grant Likely766e6a42012-04-09 14:50:06 -05003669 * of_clk_del_provider() - Remove a previously registered clock provider
3670 * @np: Device node pointer associated with clock provider
3671 */
3672void of_clk_del_provider(struct device_node *np)
3673{
3674 struct of_clk_provider *cp;
3675
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003676 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003677 list_for_each_entry(cp, &of_clk_providers, link) {
3678 if (cp->node == np) {
3679 list_del(&cp->link);
3680 of_node_put(cp->node);
3681 kfree(cp);
3682 break;
3683 }
3684 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003685 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003686}
3687EXPORT_SYMBOL_GPL(of_clk_del_provider);
3688
Stephen Boydaa795c42017-09-01 16:16:40 -07003689static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3690{
3691 struct device_node **np = res;
3692
3693 if (WARN_ON(!np || !*np))
3694 return 0;
3695
3696 return *np == data;
3697}
3698
3699void devm_of_clk_del_provider(struct device *dev)
3700{
3701 int ret;
3702
3703 ret = devres_release(dev, devm_of_clk_release_provider,
3704 devm_clk_provider_match, dev->of_node);
3705
3706 WARN_ON(ret);
3707}
3708EXPORT_SYMBOL(devm_of_clk_del_provider);
3709
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003710static struct clk_hw *
3711__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3712 struct of_phandle_args *clkspec)
3713{
3714 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003715
Stephen Boyd74002fc2016-08-25 13:35:36 -07003716 if (provider->get_hw)
3717 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003718
Stephen Boyd74002fc2016-08-25 13:35:36 -07003719 clk = provider->get(clkspec, provider->data);
3720 if (IS_ERR(clk))
3721 return ERR_CAST(clk);
3722 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003723}
3724
Stephen Boyd73e0e492015-02-06 11:42:43 -08003725struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3726 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05003727{
3728 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01003729 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Stephen Boydf155d152016-08-15 14:32:23 -07003730 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -05003731
Stephen Boyd306c3422015-02-05 15:39:11 -08003732 if (!clkspec)
3733 return ERR_PTR(-EINVAL);
3734
Grant Likely766e6a42012-04-09 14:50:06 -05003735 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08003736 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003737 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07003738 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003739 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003740 clk = __clk_create_clk(hw, dev_id, con_id);
Stephen Boydf155d152016-08-15 14:32:23 -07003741 }
Stephen Boyd73e0e492015-02-06 11:42:43 -08003742
Stephen Boydf155d152016-08-15 14:32:23 -07003743 if (!IS_ERR(clk)) {
3744 if (!__clk_get(clk)) {
Stephen Boyd73e0e492015-02-06 11:42:43 -08003745 __clk_free_clk(clk);
3746 clk = ERR_PTR(-ENOENT);
3747 }
3748
Grant Likely766e6a42012-04-09 14:50:06 -05003749 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08003750 }
Grant Likely766e6a42012-04-09 14:50:06 -05003751 }
Stephen Boyd306c3422015-02-05 15:39:11 -08003752 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003753
3754 return clk;
3755}
3756
Stephen Boyd306c3422015-02-05 15:39:11 -08003757/**
3758 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3759 * @clkspec: pointer to a clock specifier data structure
3760 *
3761 * This function looks up a struct clk from the registered list of clock
3762 * providers, an input is a clock specifier data structure as returned
3763 * from the of_parse_phandle_with_args() function call.
3764 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003765struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3766{
Stephen Boyd306c3422015-02-05 15:39:11 -08003767 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05003768}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06003769EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05003770
Stephen Boyd929e7f32016-02-19 15:52:32 -08003771/**
3772 * of_clk_get_parent_count() - Count the number of clocks a device node has
3773 * @np: device node to count
3774 *
3775 * Returns: The number of clocks that are possible parents of this node
3776 */
3777unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07003778{
Stephen Boyd929e7f32016-02-19 15:52:32 -08003779 int count;
3780
3781 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3782 if (count < 0)
3783 return 0;
3784
3785 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07003786}
3787EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3788
Grant Likely766e6a42012-04-09 14:50:06 -05003789const char *of_clk_get_parent_name(struct device_node *np, int index)
3790{
3791 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003792 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003793 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003794 const __be32 *vp;
3795 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003796 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003797 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003798 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05003799
Grant Likely766e6a42012-04-09 14:50:06 -05003800 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3801 &clkspec);
3802 if (rc)
3803 return NULL;
3804
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003805 index = clkspec.args_count ? clkspec.args[0] : 0;
3806 count = 0;
3807
3808 /* if there is an indices property, use it to transfer the index
3809 * specified into an array offset for the clock-output-names property.
3810 */
3811 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3812 if (index == pv) {
3813 index = count;
3814 break;
3815 }
3816 count++;
3817 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09003818 /* We went off the end of 'clock-indices' without finding it */
3819 if (prop && !vp)
3820 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003821
Grant Likely766e6a42012-04-09 14:50:06 -05003822 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003823 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003824 &clk_name) < 0) {
3825 /*
3826 * Best effort to get the name if the clock has been
3827 * registered with the framework. If the clock isn't
3828 * registered, we return the node name as the name of
3829 * the clock as long as #clock-cells = 0.
3830 */
3831 clk = of_clk_get_from_provider(&clkspec);
3832 if (IS_ERR(clk)) {
3833 if (clkspec.args_count == 0)
3834 clk_name = clkspec.np->name;
3835 else
3836 clk_name = NULL;
3837 } else {
3838 clk_name = __clk_get_name(clk);
3839 clk_put(clk);
3840 }
3841 }
3842
Grant Likely766e6a42012-04-09 14:50:06 -05003843
3844 of_node_put(clkspec.np);
3845 return clk_name;
3846}
3847EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3848
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05003849/**
3850 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3851 * number of parents
3852 * @np: Device node pointer associated with clock provider
3853 * @parents: pointer to char array that hold the parents' names
3854 * @size: size of the @parents array
3855 *
3856 * Return: number of parents for the clock node.
3857 */
3858int of_clk_parent_fill(struct device_node *np, const char **parents,
3859 unsigned int size)
3860{
3861 unsigned int i = 0;
3862
3863 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3864 i++;
3865
3866 return i;
3867}
3868EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3869
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003870struct clock_provider {
3871 of_clk_init_cb_t clk_init_cb;
3872 struct device_node *np;
3873 struct list_head node;
3874};
3875
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003876/*
3877 * This function looks for a parent clock. If there is one, then it
3878 * checks that the provider for this parent clock was initialized, in
3879 * this case the parent clock will be ready.
3880 */
3881static int parent_ready(struct device_node *np)
3882{
3883 int i = 0;
3884
3885 while (true) {
3886 struct clk *clk = of_clk_get(np, i);
3887
3888 /* this parent is ready we can check the next one */
3889 if (!IS_ERR(clk)) {
3890 clk_put(clk);
3891 i++;
3892 continue;
3893 }
3894
3895 /* at least one parent is not ready, we exit now */
3896 if (PTR_ERR(clk) == -EPROBE_DEFER)
3897 return 0;
3898
3899 /*
3900 * Here we make assumption that the device tree is
3901 * written correctly. So an error means that there is
3902 * no more parent. As we didn't exit yet, then the
3903 * previous parent are ready. If there is no clock
3904 * parent, no need to wait for them, then we can
3905 * consider their absence as being ready
3906 */
3907 return 1;
3908 }
3909}
3910
Grant Likely766e6a42012-04-09 14:50:06 -05003911/**
Lee Jonesd56f8992016-02-11 13:19:11 -08003912 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3913 * @np: Device node pointer associated with clock provider
3914 * @index: clock index
Geert Uytterhoevenf7ae7502018-01-03 12:06:14 +01003915 * @flags: pointer to top-level framework flags
Lee Jonesd56f8992016-02-11 13:19:11 -08003916 *
3917 * Detects if the clock-critical property exists and, if so, sets the
3918 * corresponding CLK_IS_CRITICAL flag.
3919 *
3920 * Do not use this function. It exists only for legacy Device Tree
3921 * bindings, such as the one-clock-per-node style that are outdated.
3922 * Those bindings typically put all clock data into .dts and the Linux
3923 * driver has no clock data, thus making it impossible to set this flag
3924 * correctly from the driver. Only those drivers may call
3925 * of_clk_detect_critical from their setup functions.
3926 *
3927 * Return: error code or zero on success
3928 */
3929int of_clk_detect_critical(struct device_node *np,
3930 int index, unsigned long *flags)
3931{
3932 struct property *prop;
3933 const __be32 *cur;
3934 uint32_t idx;
3935
3936 if (!np || !flags)
3937 return -EINVAL;
3938
3939 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
3940 if (index == idx)
3941 *flags |= CLK_IS_CRITICAL;
3942
3943 return 0;
3944}
3945
3946/**
Grant Likely766e6a42012-04-09 14:50:06 -05003947 * of_clk_init() - Scan and init clock providers from the DT
3948 * @matches: array of compatible values and init functions for providers.
3949 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003950 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003951 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003952 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003953 */
3954void __init of_clk_init(const struct of_device_id *matches)
3955{
Alex Elder7f7ed582013-08-22 11:31:31 -05003956 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003957 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003958 struct clock_provider *clk_provider, *next;
3959 bool is_init_done;
3960 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07003961 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05003962
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303963 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003964 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303965
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003966 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003967 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003968 struct clock_provider *parent;
3969
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01003970 if (!of_device_is_available(np))
3971 continue;
3972
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003973 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
3974 if (!parent) {
3975 list_for_each_entry_safe(clk_provider, next,
3976 &clk_provider_list, node) {
3977 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003978 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003979 kfree(clk_provider);
3980 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003981 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003982 return;
3983 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003984
3985 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003986 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003987 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003988 }
3989
3990 while (!list_empty(&clk_provider_list)) {
3991 is_init_done = false;
3992 list_for_each_entry_safe(clk_provider, next,
3993 &clk_provider_list, node) {
3994 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003995
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02003996 /* Don't populate platform devices */
3997 of_node_set_flag(clk_provider->np,
3998 OF_POPULATED);
3999
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004000 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004001 of_clk_set_defaults(clk_provider->np, true);
4002
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004003 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004004 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004005 kfree(clk_provider);
4006 is_init_done = true;
4007 }
4008 }
4009
4010 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01004011 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004012 * remaining providers during the last loop, so now we
4013 * initialize all the remaining ones unconditionally
4014 * in case the clock parent was not mandatory
4015 */
4016 if (!is_init_done)
4017 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05004018 }
4019}
4020#endif