blob: 8e728f395b542b07405f484eeb03c83a789b90bf [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;
2003
2004 if (!clk)
2005 return 0;
2006
2007 if (min > max) {
2008 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2009 __func__, clk->core->name, clk->dev_id, clk->con_id,
2010 min, max);
2011 return -EINVAL;
2012 }
2013
2014 clk_prepare_lock();
2015
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002016 if (clk->exclusive_count)
2017 clk_core_rate_unprotect(clk->core);
2018
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002019 if (min != clk->min_rate || max != clk->max_rate) {
2020 clk->min_rate = min;
2021 clk->max_rate = max;
2022 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2023 }
2024
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002025 if (clk->exclusive_count)
2026 clk_core_rate_protect(clk->core);
2027
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002028 clk_prepare_unlock();
2029
2030 return ret;
2031}
2032EXPORT_SYMBOL_GPL(clk_set_rate_range);
2033
2034/**
2035 * clk_set_min_rate - set a minimum clock rate for a clock source
2036 * @clk: clock source
2037 * @rate: desired minimum clock rate in Hz, inclusive
2038 *
2039 * Returns success (0) or negative errno.
2040 */
2041int clk_set_min_rate(struct clk *clk, unsigned long rate)
2042{
2043 if (!clk)
2044 return 0;
2045
2046 return clk_set_rate_range(clk, rate, clk->max_rate);
2047}
2048EXPORT_SYMBOL_GPL(clk_set_min_rate);
2049
2050/**
2051 * clk_set_max_rate - set a maximum clock rate for a clock source
2052 * @clk: clock source
2053 * @rate: desired maximum clock rate in Hz, inclusive
2054 *
2055 * Returns success (0) or negative errno.
2056 */
2057int clk_set_max_rate(struct clk *clk, unsigned long rate)
2058{
2059 if (!clk)
2060 return 0;
2061
2062 return clk_set_rate_range(clk, clk->min_rate, rate);
2063}
2064EXPORT_SYMBOL_GPL(clk_set_max_rate);
2065
2066/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002067 * clk_get_parent - return the parent of a clk
2068 * @clk: the clk whose parent gets returned
2069 *
2070 * Simply returns clk->parent. Returns NULL if clk is NULL.
2071 */
2072struct clk *clk_get_parent(struct clk *clk)
2073{
2074 struct clk *parent;
2075
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002076 if (!clk)
2077 return NULL;
2078
Mike Turquetteeab89f62013-03-28 13:59:01 -07002079 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002080 /* TODO: Create a per-user clk and change callers to call clk_put */
2081 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002082 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002083
2084 return parent;
2085}
2086EXPORT_SYMBOL_GPL(clk_get_parent);
2087
Stephen Boydd6968fc2015-04-30 13:54:13 -07002088static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002089{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002090 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002091
Masahiro Yamada2430a942016-02-09 20:19:14 +09002092 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002093 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002094
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002095 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002096}
2097
Stephen Boydd6968fc2015-04-30 13:54:13 -07002098static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002099 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002100{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002101 clk_reparent(core, new_parent);
2102 __clk_recalc_accuracies(core);
2103 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002104}
2105
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002106void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2107{
2108 if (!hw)
2109 return;
2110
2111 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2112}
2113
Mike Turquetteb24764902012-03-15 23:11:19 -07002114/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002115 * clk_has_parent - check if a clock is a possible parent for another
2116 * @clk: clock source
2117 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002118 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002119 * This function can be used in drivers that need to check that a clock can be
2120 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002121 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002122 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002123 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002124bool clk_has_parent(struct clk *clk, struct clk *parent)
2125{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002126 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002127 unsigned int i;
2128
2129 /* NULL clocks should be nops, so return success if either is NULL. */
2130 if (!clk || !parent)
2131 return true;
2132
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002133 core = clk->core;
2134 parent_core = parent->core;
2135
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002136 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002137 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002138 return true;
2139
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002140 for (i = 0; i < core->num_parents; i++)
2141 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002142 return true;
2143
2144 return false;
2145}
2146EXPORT_SYMBOL_GPL(clk_has_parent);
2147
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002148static int clk_core_set_parent_nolock(struct clk_core *core,
2149 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002150{
2151 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002152 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002153 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002154
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002155 lockdep_assert_held(&prepare_lock);
2156
Stephen Boydd6968fc2015-04-30 13:54:13 -07002157 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002158 return 0;
2159
Stephen Boydd6968fc2015-04-30 13:54:13 -07002160 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002161 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002162
Stephen Boydb61c43c2015-02-02 14:11:25 -08002163 /* verify ops for for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002164 if (core->num_parents > 1 && !core->ops->set_parent)
2165 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08002166
Ulf Hansson031dcc92013-04-02 23:09:38 +02002167 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002168 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2169 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002170
Jerome Brunete55a8392017-12-01 22:51:56 +01002171 if (clk_core_rate_is_protected(core))
2172 return -EBUSY;
2173
Ulf Hansson031dcc92013-04-02 23:09:38 +02002174 /* try finding the new parent index */
2175 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002176 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002177 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002178 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002179 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002180 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002181 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002182 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002183 }
2184
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002185 ret = clk_pm_runtime_get(core);
2186 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002187 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002188
Mike Turquetteb24764902012-03-15 23:11:19 -07002189 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002190 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002191
2192 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002193 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002194 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07002195
Ulf Hansson031dcc92013-04-02 23:09:38 +02002196 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002197 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002198
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002199 /* propagate rate an accuracy recalculation accordingly */
2200 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002201 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002202 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002203 __clk_recalc_rates(core, POST_RATE_CHANGE);
2204 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002205 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002206
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002207runtime_put:
2208 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002209
2210 return ret;
2211}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002212
2213/**
2214 * clk_set_parent - switch the parent of a mux clk
2215 * @clk: the mux clk whose input we are switching
2216 * @parent: the new input to clk
2217 *
2218 * Re-parent clk to use parent as its new input source. If clk is in
2219 * prepared state, the clk will get enabled for the duration of this call. If
2220 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2221 * that, the reparenting is glitchy in hardware, etc), use the
2222 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2223 *
2224 * After successfully changing clk's parent clk_set_parent will update the
2225 * clk topology, sysfs topology and propagate rate recalculation via
2226 * __clk_recalc_rates.
2227 *
2228 * Returns 0 on success, -EERROR otherwise.
2229 */
2230int clk_set_parent(struct clk *clk, struct clk *parent)
2231{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002232 int ret;
2233
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002234 if (!clk)
2235 return 0;
2236
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002237 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002238
2239 if (clk->exclusive_count)
2240 clk_core_rate_unprotect(clk->core);
2241
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002242 ret = clk_core_set_parent_nolock(clk->core,
2243 parent ? parent->core : NULL);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002244
2245 if (clk->exclusive_count)
2246 clk_core_rate_protect(clk->core);
2247
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002248 clk_prepare_unlock();
2249
2250 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002251}
Mike Turquetteb24764902012-03-15 23:11:19 -07002252EXPORT_SYMBOL_GPL(clk_set_parent);
2253
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002254static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2255{
2256 int ret = -EINVAL;
2257
2258 lockdep_assert_held(&prepare_lock);
2259
2260 if (!core)
2261 return 0;
2262
Jerome Brunete55a8392017-12-01 22:51:56 +01002263 if (clk_core_rate_is_protected(core))
2264 return -EBUSY;
2265
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002266 trace_clk_set_phase(core, degrees);
2267
2268 if (core->ops->set_phase)
2269 ret = core->ops->set_phase(core->hw, degrees);
2270
2271 trace_clk_set_phase_complete(core, degrees);
2272
2273 return ret;
2274}
2275
Mike Turquetteb24764902012-03-15 23:11:19 -07002276/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002277 * clk_set_phase - adjust the phase shift of a clock signal
2278 * @clk: clock signal source
2279 * @degrees: number of degrees the signal is shifted
2280 *
2281 * Shifts the phase of a clock signal by the specified
2282 * degrees. Returns 0 on success, -EERROR otherwise.
2283 *
2284 * This function makes no distinction about the input or reference
2285 * signal that we adjust the clock signal phase against. For example
2286 * phase locked-loop clock signal generators we may shift phase with
2287 * respect to feedback clock signal input, but for other cases the
2288 * clock phase may be shifted with respect to some other, unspecified
2289 * signal.
2290 *
2291 * Additionally the concept of phase shift does not propagate through
2292 * the clock tree hierarchy, which sets it apart from clock rates and
2293 * clock accuracy. A parent clock phase attribute does not have an
2294 * impact on the phase attribute of a child clock.
2295 */
2296int clk_set_phase(struct clk *clk, int degrees)
2297{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002298 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002299
2300 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002301 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002302
2303 /* sanity check degrees */
2304 degrees %= 360;
2305 if (degrees < 0)
2306 degrees += 360;
2307
2308 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002309
2310 if (clk->exclusive_count)
2311 clk_core_rate_unprotect(clk->core);
2312
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002313 ret = clk_core_set_phase_nolock(clk->core, degrees);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002314
2315 if (clk->exclusive_count)
2316 clk_core_rate_protect(clk->core);
2317
Mike Turquettee59c5372014-02-18 21:21:25 -08002318 clk_prepare_unlock();
2319
Mike Turquettee59c5372014-02-18 21:21:25 -08002320 return ret;
2321}
Maxime Ripard9767b042015-01-20 22:23:43 +01002322EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002323
Stephen Boydd6968fc2015-04-30 13:54:13 -07002324static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002325{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002326 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002327
2328 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07002329 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002330 clk_prepare_unlock();
2331
Mike Turquettee59c5372014-02-18 21:21:25 -08002332 return ret;
2333}
2334
2335/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002336 * clk_get_phase - return the phase shift of a clock signal
2337 * @clk: clock signal source
2338 *
2339 * Returns the phase shift of a clock node in degrees, otherwise returns
2340 * -EERROR.
2341 */
2342int clk_get_phase(struct clk *clk)
2343{
2344 if (!clk)
2345 return 0;
2346
2347 return clk_core_get_phase(clk->core);
2348}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002349EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002350
2351/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002352 * clk_is_match - check if two clk's point to the same hardware clock
2353 * @p: clk compared against q
2354 * @q: clk compared against p
2355 *
2356 * Returns true if the two struct clk pointers both point to the same hardware
2357 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2358 * share the same struct clk_core object.
2359 *
2360 * Returns false otherwise. Note that two NULL clks are treated as matching.
2361 */
2362bool clk_is_match(const struct clk *p, const struct clk *q)
2363{
2364 /* trivial case: identical struct clk's or both NULL */
2365 if (p == q)
2366 return true;
2367
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002368 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002369 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2370 if (p->core == q->core)
2371 return true;
2372
2373 return false;
2374}
2375EXPORT_SYMBOL_GPL(clk_is_match);
2376
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002377/*** debugfs support ***/
2378
2379#ifdef CONFIG_DEBUG_FS
2380#include <linux/debugfs.h>
2381
2382static struct dentry *rootdir;
2383static int inited = 0;
2384static DEFINE_MUTEX(clk_debug_lock);
2385static HLIST_HEAD(clk_debug_list);
2386
2387static struct hlist_head *all_lists[] = {
2388 &clk_root_list,
2389 &clk_orphan_list,
2390 NULL,
2391};
2392
2393static struct hlist_head *orphan_list[] = {
2394 &clk_orphan_list,
2395 NULL,
2396};
2397
2398static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2399 int level)
2400{
2401 if (!c)
2402 return;
2403
Jerome Brunetc5ce26e2017-12-01 22:51:57 +01002404 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %-3d\n",
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002405 level * 3 + 1, "",
2406 30 - level * 3, c->name,
Jerome Brunete55a8392017-12-01 22:51:56 +01002407 c->enable_count, c->prepare_count, c->protect_count,
2408 clk_core_get_rate(c), clk_core_get_accuracy(c),
2409 clk_core_get_phase(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002410}
2411
2412static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2413 int level)
2414{
2415 struct clk_core *child;
2416
2417 if (!c)
2418 return;
2419
2420 clk_summary_show_one(s, c, level);
2421
2422 hlist_for_each_entry(child, &c->children, child_node)
2423 clk_summary_show_subtree(s, child, level + 1);
2424}
2425
2426static int clk_summary_show(struct seq_file *s, void *data)
2427{
2428 struct clk_core *c;
2429 struct hlist_head **lists = (struct hlist_head **)s->private;
2430
Jerome Brunetc5ce26e2017-12-01 22:51:57 +01002431 seq_puts(s, " enable prepare protect \n");
2432 seq_puts(s, " clock count count count rate accuracy phase\n");
2433 seq_puts(s, "----------------------------------------------------------------------------------------\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002434
2435 clk_prepare_lock();
2436
2437 for (; *lists; lists++)
2438 hlist_for_each_entry(c, *lists, child_node)
2439 clk_summary_show_subtree(s, c, 0);
2440
2441 clk_prepare_unlock();
2442
2443 return 0;
2444}
2445
2446
2447static int clk_summary_open(struct inode *inode, struct file *file)
2448{
2449 return single_open(file, clk_summary_show, inode->i_private);
2450}
2451
2452static const struct file_operations clk_summary_fops = {
2453 .open = clk_summary_open,
2454 .read = seq_read,
2455 .llseek = seq_lseek,
2456 .release = single_release,
2457};
2458
2459static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2460{
2461 if (!c)
2462 return;
2463
Stefan Wahren7cb81132015-04-29 16:36:43 +00002464 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002465 seq_printf(s, "\"%s\": { ", c->name);
2466 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2467 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002468 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002469 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2470 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002471 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2472}
2473
2474static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2475{
2476 struct clk_core *child;
2477
2478 if (!c)
2479 return;
2480
2481 clk_dump_one(s, c, level);
2482
2483 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002484 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002485 clk_dump_subtree(s, child, level + 1);
2486 }
2487
Markus Elfring4d327582017-04-20 08:45:43 +02002488 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002489}
2490
2491static int clk_dump(struct seq_file *s, void *data)
2492{
2493 struct clk_core *c;
2494 bool first_node = true;
2495 struct hlist_head **lists = (struct hlist_head **)s->private;
2496
Markus Elfring4d327582017-04-20 08:45:43 +02002497 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002498 clk_prepare_lock();
2499
2500 for (; *lists; lists++) {
2501 hlist_for_each_entry(c, *lists, child_node) {
2502 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02002503 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002504 first_node = false;
2505 clk_dump_subtree(s, c, 0);
2506 }
2507 }
2508
2509 clk_prepare_unlock();
2510
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002511 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002512 return 0;
2513}
2514
2515
2516static int clk_dump_open(struct inode *inode, struct file *file)
2517{
2518 return single_open(file, clk_dump, inode->i_private);
2519}
2520
2521static const struct file_operations clk_dump_fops = {
2522 .open = clk_dump_open,
2523 .read = seq_read,
2524 .llseek = seq_lseek,
2525 .release = single_release,
2526};
2527
Peter De Schrijver92031572017-03-21 15:20:31 +02002528static int possible_parents_dump(struct seq_file *s, void *data)
2529{
2530 struct clk_core *core = s->private;
2531 int i;
2532
2533 for (i = 0; i < core->num_parents - 1; i++)
2534 seq_printf(s, "%s ", core->parent_names[i]);
2535
2536 seq_printf(s, "%s\n", core->parent_names[i]);
2537
2538 return 0;
2539}
2540
2541static int possible_parents_open(struct inode *inode, struct file *file)
2542{
2543 return single_open(file, possible_parents_dump, inode->i_private);
2544}
2545
2546static const struct file_operations possible_parents_fops = {
2547 .open = possible_parents_open,
2548 .read = seq_read,
2549 .llseek = seq_lseek,
2550 .release = single_release,
2551};
2552
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002553static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2554{
2555 struct dentry *d;
2556 int ret = -ENOMEM;
2557
2558 if (!core || !pdentry) {
2559 ret = -EINVAL;
2560 goto out;
2561 }
2562
2563 d = debugfs_create_dir(core->name, pdentry);
2564 if (!d)
2565 goto out;
2566
2567 core->dentry = d;
2568
2569 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2570 (u32 *)&core->rate);
2571 if (!d)
2572 goto err_out;
2573
2574 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2575 (u32 *)&core->accuracy);
2576 if (!d)
2577 goto err_out;
2578
2579 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2580 (u32 *)&core->phase);
2581 if (!d)
2582 goto err_out;
2583
2584 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2585 (u32 *)&core->flags);
2586 if (!d)
2587 goto err_out;
2588
2589 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2590 (u32 *)&core->prepare_count);
2591 if (!d)
2592 goto err_out;
2593
2594 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2595 (u32 *)&core->enable_count);
2596 if (!d)
2597 goto err_out;
2598
Jerome Brunete55a8392017-12-01 22:51:56 +01002599 d = debugfs_create_u32("clk_protect_count", S_IRUGO, core->dentry,
2600 (u32 *)&core->protect_count);
2601 if (!d)
2602 goto err_out;
2603
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002604 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2605 (u32 *)&core->notifier_count);
2606 if (!d)
2607 goto err_out;
2608
Peter De Schrijver92031572017-03-21 15:20:31 +02002609 if (core->num_parents > 1) {
2610 d = debugfs_create_file("clk_possible_parents", S_IRUGO,
2611 core->dentry, core, &possible_parents_fops);
2612 if (!d)
2613 goto err_out;
2614 }
2615
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002616 if (core->ops->debug_init) {
2617 ret = core->ops->debug_init(core->hw, core->dentry);
2618 if (ret)
2619 goto err_out;
2620 }
2621
2622 ret = 0;
2623 goto out;
2624
2625err_out:
2626 debugfs_remove_recursive(core->dentry);
2627 core->dentry = NULL;
2628out:
2629 return ret;
2630}
2631
2632/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002633 * clk_debug_register - add a clk node to the debugfs clk directory
2634 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002635 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002636 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2637 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002638 * will be created lazily by clk_debug_init as part of a late_initcall.
2639 */
2640static int clk_debug_register(struct clk_core *core)
2641{
2642 int ret = 0;
2643
2644 mutex_lock(&clk_debug_lock);
2645 hlist_add_head(&core->debug_node, &clk_debug_list);
2646
2647 if (!inited)
2648 goto unlock;
2649
2650 ret = clk_debug_create_one(core, rootdir);
2651unlock:
2652 mutex_unlock(&clk_debug_lock);
2653
2654 return ret;
2655}
2656
2657 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002658 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2659 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002660 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002661 * Dynamically removes a clk and all its child nodes from the
2662 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08002663 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002664 */
2665static void clk_debug_unregister(struct clk_core *core)
2666{
2667 mutex_lock(&clk_debug_lock);
2668 hlist_del_init(&core->debug_node);
2669 debugfs_remove_recursive(core->dentry);
2670 core->dentry = NULL;
2671 mutex_unlock(&clk_debug_lock);
2672}
2673
2674struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2675 void *data, const struct file_operations *fops)
2676{
2677 struct dentry *d = NULL;
2678
2679 if (hw->core->dentry)
2680 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2681 fops);
2682
2683 return d;
2684}
2685EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2686
2687/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002688 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002689 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002690 * clks are often initialized very early during boot before memory can be
2691 * dynamically allocated and well before debugfs is setup. This function
2692 * populates the debugfs clk directory once at boot-time when we know that
2693 * debugfs is setup. It should only be called once at boot-time, all other clks
2694 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002695 */
2696static int __init clk_debug_init(void)
2697{
2698 struct clk_core *core;
2699 struct dentry *d;
2700
2701 rootdir = debugfs_create_dir("clk", NULL);
2702
2703 if (!rootdir)
2704 return -ENOMEM;
2705
2706 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2707 &clk_summary_fops);
2708 if (!d)
2709 return -ENOMEM;
2710
2711 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2712 &clk_dump_fops);
2713 if (!d)
2714 return -ENOMEM;
2715
2716 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2717 &orphan_list, &clk_summary_fops);
2718 if (!d)
2719 return -ENOMEM;
2720
2721 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2722 &orphan_list, &clk_dump_fops);
2723 if (!d)
2724 return -ENOMEM;
2725
2726 mutex_lock(&clk_debug_lock);
2727 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2728 clk_debug_create_one(core, rootdir);
2729
2730 inited = 1;
2731 mutex_unlock(&clk_debug_lock);
2732
2733 return 0;
2734}
2735late_initcall(clk_debug_init);
2736#else
2737static inline int clk_debug_register(struct clk_core *core) { return 0; }
2738static inline void clk_debug_reparent(struct clk_core *core,
2739 struct clk_core *new_parent)
2740{
2741}
2742static inline void clk_debug_unregister(struct clk_core *core)
2743{
2744}
2745#endif
2746
Michael Turquette3d3801e2015-02-25 09:11:01 -08002747/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002748 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002749 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07002750 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002751 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002752 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002753 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002754static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002755{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002756 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002757 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002758 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002759 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002760
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002761 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07002762 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002763
Mike Turquetteeab89f62013-03-28 13:59:01 -07002764 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002765
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002766 ret = clk_pm_runtime_get(core);
2767 if (ret)
2768 goto unlock;
2769
Mike Turquetteb24764902012-03-15 23:11:19 -07002770 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002771 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002772 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002773 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002774 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002775 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002776 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002777
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002778 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002779 if (core->ops->set_rate &&
2780 !((core->ops->round_rate || core->ops->determine_rate) &&
2781 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002782 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2783 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002784 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002785 goto out;
2786 }
2787
Stephen Boydd6968fc2015-04-30 13:54:13 -07002788 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002789 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2790 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002791 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002792 goto out;
2793 }
2794
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09002795 if (core->num_parents > 1 && !core->ops->get_parent) {
2796 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2797 __func__, core->name);
2798 ret = -EINVAL;
2799 goto out;
2800 }
2801
Stephen Boydd6968fc2015-04-30 13:54:13 -07002802 if (core->ops->set_rate_and_parent &&
2803 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002804 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002805 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002806 ret = -EINVAL;
2807 goto out;
2808 }
2809
Mike Turquetteb24764902012-03-15 23:11:19 -07002810 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002811 for (i = 0; i < core->num_parents; i++)
2812 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002813 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002814 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002815
Stephen Boydd6968fc2015-04-30 13:54:13 -07002816 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002817
2818 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08002819 * Populate core->parent if parent has already been clk_core_init'd. If
2820 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002821 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07002822 * clk list.
2823 *
2824 * Every time a new clk is clk_init'd then we walk the list of orphan
2825 * clocks and re-parent any that are children of the clock currently
2826 * being clk_init'd.
2827 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02002828 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002829 hlist_add_head(&core->child_node,
2830 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002831 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002832 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002833 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002834 core->orphan = false;
2835 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002836 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002837 core->orphan = true;
2838 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002839
2840 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002841 * Set clk's accuracy. The preferred method is to use
2842 * .recalc_accuracy. For simple clocks and lazy developers the default
2843 * fallback is to use the parent's accuracy. If a clock doesn't have a
2844 * parent (or is orphaned) then accuracy is set to zero (perfect
2845 * clock).
2846 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002847 if (core->ops->recalc_accuracy)
2848 core->accuracy = core->ops->recalc_accuracy(core->hw,
2849 __clk_get_accuracy(core->parent));
2850 else if (core->parent)
2851 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002852 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002853 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002854
2855 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002856 * Set clk's phase.
2857 * Since a phase is by definition relative to its parent, just
2858 * query the current clock phase, or just assume it's in phase.
2859 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002860 if (core->ops->get_phase)
2861 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002862 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002863 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002864
2865 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002866 * Set clk's rate. The preferred method is to use .recalc_rate. For
2867 * simple clocks and lazy developers the default fallback is to use the
2868 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2869 * then rate is set to zero.
2870 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002871 if (core->ops->recalc_rate)
2872 rate = core->ops->recalc_rate(core->hw,
2873 clk_core_get_rate_nolock(core->parent));
2874 else if (core->parent)
2875 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002876 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002877 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002878 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002879
2880 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002881 * walk the list of orphan clocks and reparent any that newly finds a
2882 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07002883 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002884 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002885 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002886
Michael Turquette904e6ea2016-07-08 16:32:10 -07002887 /*
2888 * we could call __clk_set_parent, but that would result in a
2889 * redundant call to the .set_rate op, if it exists
2890 */
2891 if (parent) {
2892 __clk_set_parent_before(orphan, parent);
2893 __clk_set_parent_after(orphan, parent, NULL);
2894 __clk_recalc_accuracies(orphan);
2895 __clk_recalc_rates(orphan, 0);
2896 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002897 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002898
2899 /*
2900 * optional platform-specific magic
2901 *
2902 * The .init callback is not used by any of the basic clock types, but
2903 * exists for weird hardware that must perform initialization magic.
2904 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002905 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002906 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002907 if (core->ops->init)
2908 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002909
Lee Jones32b9b102016-02-11 13:19:09 -08002910 if (core->flags & CLK_IS_CRITICAL) {
Maxime Ripardef56b792016-05-13 10:00:31 +02002911 unsigned long flags;
2912
Lee Jones32b9b102016-02-11 13:19:09 -08002913 clk_core_prepare(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02002914
2915 flags = clk_enable_lock();
Lee Jones32b9b102016-02-11 13:19:09 -08002916 clk_core_enable(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02002917 clk_enable_unlock(flags);
Lee Jones32b9b102016-02-11 13:19:09 -08002918 }
2919
Stephen Boydd6968fc2015-04-30 13:54:13 -07002920 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002921out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002922 clk_pm_runtime_put(core);
2923unlock:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002924 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002925
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002926 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002927 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002928
Mike Turquetted1302a32012-03-29 14:30:40 -07002929 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002930}
2931
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002932struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2933 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002934{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002935 struct clk *clk;
2936
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002937 /* This is to allow this function to be chained to others */
Masahiro Yamadac1de1352015-11-20 14:38:49 +09002938 if (IS_ERR_OR_NULL(hw))
Masahiro Yamada8a231332016-07-19 16:28:47 +09002939 return ERR_CAST(hw);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002940
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002941 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2942 if (!clk)
2943 return ERR_PTR(-ENOMEM);
2944
2945 clk->core = hw->core;
2946 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02002947 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002948 clk->max_rate = ULONG_MAX;
2949
2950 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002951 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002952 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002953
2954 return clk;
2955}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002956
Stephen Boyd73e0e492015-02-06 11:42:43 -08002957void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002958{
2959 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002960 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002961 clk_prepare_unlock();
2962
Leonard Crestez253160a2017-02-20 15:20:56 +02002963 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002964 kfree(clk);
2965}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002966
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002967/**
2968 * clk_register - allocate a new clock, register it and return an opaque cookie
2969 * @dev: device that is registering this clock
2970 * @hw: link to hardware-specific clock data
2971 *
2972 * clk_register is the primary interface for populating the clock tree with new
2973 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05302974 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002975 * rest of the clock API. In the event of an error clk_register will return an
2976 * error code; drivers must test for an error code after calling clk_register.
2977 */
2978struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002979{
Mike Turquetted1302a32012-03-29 14:30:40 -07002980 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002981 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002982
Stephen Boydd6968fc2015-04-30 13:54:13 -07002983 core = kzalloc(sizeof(*core), GFP_KERNEL);
2984 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002985 ret = -ENOMEM;
2986 goto fail_out;
2987 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002988
Stephen Boydd6968fc2015-04-30 13:54:13 -07002989 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2990 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002991 ret = -ENOMEM;
2992 goto fail_name;
2993 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002994 core->ops = hw->init->ops;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002995 if (dev && pm_runtime_enabled(dev))
2996 core->dev = dev;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002997 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002998 core->owner = dev->driver->owner;
2999 core->hw = hw;
3000 core->flags = hw->init->flags;
3001 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003002 core->min_rate = 0;
3003 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003004 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003005
Mike Turquetted1302a32012-03-29 14:30:40 -07003006 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003007 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02003008 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003009
Stephen Boydd6968fc2015-04-30 13:54:13 -07003010 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003011 ret = -ENOMEM;
3012 goto fail_parent_names;
3013 }
3014
3015
3016 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003017 for (i = 0; i < core->num_parents; i++) {
3018 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003019 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003020 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003021 ret = -ENOMEM;
3022 goto fail_parent_names_copy;
3023 }
3024 }
3025
Masahiro Yamada176d1162015-12-28 19:23:00 +09003026 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3027 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3028 GFP_KERNEL);
3029 if (!core->parents) {
3030 ret = -ENOMEM;
3031 goto fail_parents;
3032 };
3033
Stephen Boydd6968fc2015-04-30 13:54:13 -07003034 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003035
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003036 hw->clk = __clk_create_clk(hw, NULL, NULL);
3037 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003038 ret = PTR_ERR(hw->clk);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003039 goto fail_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003040 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003041
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003042 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003043 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003044 return hw->clk;
3045
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003046 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003047 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003048
Masahiro Yamada176d1162015-12-28 19:23:00 +09003049fail_parents:
3050 kfree(core->parents);
Mike Turquetted1302a32012-03-29 14:30:40 -07003051fail_parent_names_copy:
3052 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003053 kfree_const(core->parent_names[i]);
3054 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07003055fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003056 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003057fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003058 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003059fail_out:
3060 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003061}
3062EXPORT_SYMBOL_GPL(clk_register);
3063
Stephen Boyd41438042016-02-05 17:02:52 -08003064/**
3065 * clk_hw_register - register a clk_hw and return an error code
3066 * @dev: device that is registering this clock
3067 * @hw: link to hardware-specific clock data
3068 *
3069 * clk_hw_register is the primary interface for populating the clock tree with
3070 * new clock nodes. It returns an integer equal to zero indicating success or
3071 * less than zero indicating failure. Drivers must test for an error code after
3072 * calling clk_hw_register().
3073 */
3074int clk_hw_register(struct device *dev, struct clk_hw *hw)
3075{
3076 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3077}
3078EXPORT_SYMBOL_GPL(clk_hw_register);
3079
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003080/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003081static void __clk_release(struct kref *ref)
3082{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003083 struct clk_core *core = container_of(ref, struct clk_core, ref);
3084 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003085
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003086 lockdep_assert_held(&prepare_lock);
3087
Stephen Boydd6968fc2015-04-30 13:54:13 -07003088 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003089 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003090 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003091
Stephen Boydd6968fc2015-04-30 13:54:13 -07003092 kfree(core->parent_names);
3093 kfree_const(core->name);
3094 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003095}
3096
3097/*
3098 * Empty clk_ops for unregistered clocks. These are used temporarily
3099 * after clk_unregister() was called on a clock and until last clock
3100 * consumer calls clk_put() and the struct clk object is freed.
3101 */
3102static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3103{
3104 return -ENXIO;
3105}
3106
3107static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3108{
3109 WARN_ON_ONCE(1);
3110}
3111
3112static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3113 unsigned long parent_rate)
3114{
3115 return -ENXIO;
3116}
3117
3118static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3119{
3120 return -ENXIO;
3121}
3122
3123static const struct clk_ops clk_nodrv_ops = {
3124 .enable = clk_nodrv_prepare_enable,
3125 .disable = clk_nodrv_disable_unprepare,
3126 .prepare = clk_nodrv_prepare_enable,
3127 .unprepare = clk_nodrv_disable_unprepare,
3128 .set_rate = clk_nodrv_set_rate,
3129 .set_parent = clk_nodrv_set_parent,
3130};
3131
Mark Brown1df5c932012-04-18 09:07:12 +01003132/**
3133 * clk_unregister - unregister a currently registered clock
3134 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003135 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003136void clk_unregister(struct clk *clk)
3137{
3138 unsigned long flags;
3139
Stephen Boyd6314b672014-09-04 23:37:49 -07003140 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3141 return;
3142
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003143 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003144
3145 clk_prepare_lock();
3146
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003147 if (clk->core->ops == &clk_nodrv_ops) {
3148 pr_err("%s: unregistered clock: %s\n", __func__,
3149 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003150 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003151 }
3152 /*
3153 * Assign empty clock ops for consumers that might still hold
3154 * a reference to this clock.
3155 */
3156 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003157 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003158 clk_enable_unlock(flags);
3159
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003160 if (!hlist_empty(&clk->core->children)) {
3161 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003162 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003163
3164 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003165 hlist_for_each_entry_safe(child, t, &clk->core->children,
3166 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01003167 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003168 }
3169
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003170 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003171
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003172 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003173 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003174 __func__, clk->core->name);
Jerome Brunete55a8392017-12-01 22:51:56 +01003175
3176 if (clk->core->protect_count)
3177 pr_warn("%s: unregistering protected clock: %s\n",
3178 __func__, clk->core->name);
3179
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003180 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003181unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003182 clk_prepare_unlock();
3183}
Mark Brown1df5c932012-04-18 09:07:12 +01003184EXPORT_SYMBOL_GPL(clk_unregister);
3185
Stephen Boyd41438042016-02-05 17:02:52 -08003186/**
3187 * clk_hw_unregister - unregister a currently registered clk_hw
3188 * @hw: hardware-specific clock data to unregister
3189 */
3190void clk_hw_unregister(struct clk_hw *hw)
3191{
3192 clk_unregister(hw->clk);
3193}
3194EXPORT_SYMBOL_GPL(clk_hw_unregister);
3195
Stephen Boyd46c87732012-09-24 13:38:04 -07003196static void devm_clk_release(struct device *dev, void *res)
3197{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003198 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003199}
3200
Stephen Boyd41438042016-02-05 17:02:52 -08003201static void devm_clk_hw_release(struct device *dev, void *res)
3202{
3203 clk_hw_unregister(*(struct clk_hw **)res);
3204}
3205
Stephen Boyd46c87732012-09-24 13:38:04 -07003206/**
3207 * devm_clk_register - resource managed clk_register()
3208 * @dev: device that is registering this clock
3209 * @hw: link to hardware-specific clock data
3210 *
3211 * Managed clk_register(). Clocks returned from this function are
3212 * automatically clk_unregister()ed on driver detach. See clk_register() for
3213 * more information.
3214 */
3215struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3216{
3217 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003218 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003219
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003220 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3221 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003222 return ERR_PTR(-ENOMEM);
3223
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003224 clk = clk_register(dev, hw);
3225 if (!IS_ERR(clk)) {
3226 *clkp = clk;
3227 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003228 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003229 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003230 }
3231
3232 return clk;
3233}
3234EXPORT_SYMBOL_GPL(devm_clk_register);
3235
Stephen Boyd41438042016-02-05 17:02:52 -08003236/**
3237 * devm_clk_hw_register - resource managed clk_hw_register()
3238 * @dev: device that is registering this clock
3239 * @hw: link to hardware-specific clock data
3240 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003241 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003242 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3243 * for more information.
3244 */
3245int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3246{
3247 struct clk_hw **hwp;
3248 int ret;
3249
3250 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3251 if (!hwp)
3252 return -ENOMEM;
3253
3254 ret = clk_hw_register(dev, hw);
3255 if (!ret) {
3256 *hwp = hw;
3257 devres_add(dev, hwp);
3258 } else {
3259 devres_free(hwp);
3260 }
3261
3262 return ret;
3263}
3264EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3265
Stephen Boyd46c87732012-09-24 13:38:04 -07003266static int devm_clk_match(struct device *dev, void *res, void *data)
3267{
3268 struct clk *c = res;
3269 if (WARN_ON(!c))
3270 return 0;
3271 return c == data;
3272}
3273
Stephen Boyd41438042016-02-05 17:02:52 -08003274static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3275{
3276 struct clk_hw *hw = res;
3277
3278 if (WARN_ON(!hw))
3279 return 0;
3280 return hw == data;
3281}
3282
Stephen Boyd46c87732012-09-24 13:38:04 -07003283/**
3284 * devm_clk_unregister - resource managed clk_unregister()
3285 * @clk: clock to unregister
3286 *
3287 * Deallocate a clock allocated with devm_clk_register(). Normally
3288 * this function will not need to be called and the resource management
3289 * code will ensure that the resource is freed.
3290 */
3291void devm_clk_unregister(struct device *dev, struct clk *clk)
3292{
3293 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3294}
3295EXPORT_SYMBOL_GPL(devm_clk_unregister);
3296
Stephen Boyd41438042016-02-05 17:02:52 -08003297/**
3298 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3299 * @dev: device that is unregistering the hardware-specific clock data
3300 * @hw: link to hardware-specific clock data
3301 *
3302 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3303 * this function will not need to be called and the resource management
3304 * code will ensure that the resource is freed.
3305 */
3306void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3307{
3308 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3309 hw));
3310}
3311EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3312
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003313/*
3314 * clkdev helpers
3315 */
3316int __clk_get(struct clk *clk)
3317{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003318 struct clk_core *core = !clk ? NULL : clk->core;
3319
3320 if (core) {
3321 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003322 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003323
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003324 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003325 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003326 return 1;
3327}
3328
3329void __clk_put(struct clk *clk)
3330{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003331 struct module *owner;
3332
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003333 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003334 return;
3335
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003336 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003337
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01003338 /*
3339 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3340 * given user should be balanced with calls to clk_rate_exclusive_put()
3341 * and by that same consumer
3342 */
3343 if (WARN_ON(clk->exclusive_count)) {
3344 /* We voiced our concern, let's sanitize the situation */
3345 clk->core->protect_count -= (clk->exclusive_count - 1);
3346 clk_core_rate_unprotect(clk->core);
3347 clk->exclusive_count = 0;
3348 }
3349
Stephen Boyd50595f82015-02-06 11:42:44 -08003350 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003351 if (clk->min_rate > clk->core->req_rate ||
3352 clk->max_rate < clk->core->req_rate)
3353 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3354
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003355 owner = clk->core->owner;
3356 kref_put(&clk->core->ref, __clk_release);
3357
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003358 clk_prepare_unlock();
3359
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003360 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003361
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003362 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003363}
3364
Mike Turquetteb24764902012-03-15 23:11:19 -07003365/*** clk rate change notifiers ***/
3366
3367/**
3368 * clk_notifier_register - add a clk rate change notifier
3369 * @clk: struct clk * to watch
3370 * @nb: struct notifier_block * with callback info
3371 *
3372 * Request notification when clk's rate changes. This uses an SRCU
3373 * notifier because we want it to block and notifier unregistrations are
3374 * uncommon. The callbacks associated with the notifier must not
3375 * re-enter into the clk framework by calling any top-level clk APIs;
3376 * this will cause a nested prepare_lock mutex.
3377 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003378 * In all notification cases (pre, post and abort rate change) the original
3379 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3380 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003381 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003382 * clk_notifier_register() must be called from non-atomic context.
3383 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3384 * allocation failure; otherwise, passes along the return value of
3385 * srcu_notifier_chain_register().
3386 */
3387int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3388{
3389 struct clk_notifier *cn;
3390 int ret = -ENOMEM;
3391
3392 if (!clk || !nb)
3393 return -EINVAL;
3394
Mike Turquetteeab89f62013-03-28 13:59:01 -07003395 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003396
3397 /* search the list of notifiers for this clk */
3398 list_for_each_entry(cn, &clk_notifier_list, node)
3399 if (cn->clk == clk)
3400 break;
3401
3402 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3403 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02003404 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003405 if (!cn)
3406 goto out;
3407
3408 cn->clk = clk;
3409 srcu_init_notifier_head(&cn->notifier_head);
3410
3411 list_add(&cn->node, &clk_notifier_list);
3412 }
3413
3414 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3415
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003416 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003417
3418out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003419 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003420
3421 return ret;
3422}
3423EXPORT_SYMBOL_GPL(clk_notifier_register);
3424
3425/**
3426 * clk_notifier_unregister - remove a clk rate change notifier
3427 * @clk: struct clk *
3428 * @nb: struct notifier_block * with callback info
3429 *
3430 * Request no further notification for changes to 'clk' and frees memory
3431 * allocated in clk_notifier_register.
3432 *
3433 * Returns -EINVAL if called with null arguments; otherwise, passes
3434 * along the return value of srcu_notifier_chain_unregister().
3435 */
3436int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3437{
3438 struct clk_notifier *cn = NULL;
3439 int ret = -EINVAL;
3440
3441 if (!clk || !nb)
3442 return -EINVAL;
3443
Mike Turquetteeab89f62013-03-28 13:59:01 -07003444 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003445
3446 list_for_each_entry(cn, &clk_notifier_list, node)
3447 if (cn->clk == clk)
3448 break;
3449
3450 if (cn->clk == clk) {
3451 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3452
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003453 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07003454
3455 /* XXX the notifier code should handle this better */
3456 if (!cn->notifier_head.head) {
3457 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08003458 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07003459 kfree(cn);
3460 }
3461
3462 } else {
3463 ret = -ENOENT;
3464 }
3465
Mike Turquetteeab89f62013-03-28 13:59:01 -07003466 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003467
3468 return ret;
3469}
3470EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05003471
3472#ifdef CONFIG_OF
3473/**
3474 * struct of_clk_provider - Clock provider registration structure
3475 * @link: Entry in global list of clock providers
3476 * @node: Pointer to device tree node of clock provider
3477 * @get: Get clock callback. Returns NULL or a struct clk for the
3478 * given clock specifier
3479 * @data: context pointer to be passed into @get callback
3480 */
3481struct of_clk_provider {
3482 struct list_head link;
3483
3484 struct device_node *node;
3485 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003486 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05003487 void *data;
3488};
3489
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303490static const struct of_device_id __clk_of_table_sentinel
3491 __used __section(__clk_of_table_end);
3492
Grant Likely766e6a42012-04-09 14:50:06 -05003493static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003494static DEFINE_MUTEX(of_clk_mutex);
3495
Grant Likely766e6a42012-04-09 14:50:06 -05003496struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3497 void *data)
3498{
3499 return data;
3500}
3501EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3502
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003503struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3504{
3505 return data;
3506}
3507EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3508
Shawn Guo494bfec2012-08-22 21:36:27 +08003509struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3510{
3511 struct clk_onecell_data *clk_data = data;
3512 unsigned int idx = clkspec->args[0];
3513
3514 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02003515 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08003516 return ERR_PTR(-EINVAL);
3517 }
3518
3519 return clk_data->clks[idx];
3520}
3521EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3522
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003523struct clk_hw *
3524of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3525{
3526 struct clk_hw_onecell_data *hw_data = data;
3527 unsigned int idx = clkspec->args[0];
3528
3529 if (idx >= hw_data->num) {
3530 pr_err("%s: invalid index %u\n", __func__, idx);
3531 return ERR_PTR(-EINVAL);
3532 }
3533
3534 return hw_data->hws[idx];
3535}
3536EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3537
Grant Likely766e6a42012-04-09 14:50:06 -05003538/**
3539 * of_clk_add_provider() - Register a clock provider for a node
3540 * @np: Device node pointer associated with clock provider
3541 * @clk_src_get: callback for decoding clock
3542 * @data: context pointer for @clk_src_get callback.
3543 */
3544int of_clk_add_provider(struct device_node *np,
3545 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3546 void *data),
3547 void *data)
3548{
3549 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003550 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003551
Markus Elfring1808a322017-04-20 09:30:52 +02003552 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05003553 if (!cp)
3554 return -ENOMEM;
3555
3556 cp->node = of_node_get(np);
3557 cp->data = data;
3558 cp->get = clk_src_get;
3559
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003560 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003561 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003562 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003563 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05003564
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003565 ret = of_clk_set_defaults(np, true);
3566 if (ret < 0)
3567 of_clk_del_provider(np);
3568
3569 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003570}
3571EXPORT_SYMBOL_GPL(of_clk_add_provider);
3572
3573/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003574 * of_clk_add_hw_provider() - Register a clock provider for a node
3575 * @np: Device node pointer associated with clock provider
3576 * @get: callback for decoding clk_hw
3577 * @data: context pointer for @get callback.
3578 */
3579int of_clk_add_hw_provider(struct device_node *np,
3580 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3581 void *data),
3582 void *data)
3583{
3584 struct of_clk_provider *cp;
3585 int ret;
3586
3587 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3588 if (!cp)
3589 return -ENOMEM;
3590
3591 cp->node = of_node_get(np);
3592 cp->data = data;
3593 cp->get_hw = get;
3594
3595 mutex_lock(&of_clk_mutex);
3596 list_add(&cp->link, &of_clk_providers);
3597 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003598 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003599
3600 ret = of_clk_set_defaults(np, true);
3601 if (ret < 0)
3602 of_clk_del_provider(np);
3603
3604 return ret;
3605}
3606EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3607
Stephen Boydaa795c42017-09-01 16:16:40 -07003608static void devm_of_clk_release_provider(struct device *dev, void *res)
3609{
3610 of_clk_del_provider(*(struct device_node **)res);
3611}
3612
3613int devm_of_clk_add_hw_provider(struct device *dev,
3614 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3615 void *data),
3616 void *data)
3617{
3618 struct device_node **ptr, *np;
3619 int ret;
3620
3621 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3622 GFP_KERNEL);
3623 if (!ptr)
3624 return -ENOMEM;
3625
3626 np = dev->of_node;
3627 ret = of_clk_add_hw_provider(np, get, data);
3628 if (!ret) {
3629 *ptr = np;
3630 devres_add(dev, ptr);
3631 } else {
3632 devres_free(ptr);
3633 }
3634
3635 return ret;
3636}
3637EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3638
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003639/**
Grant Likely766e6a42012-04-09 14:50:06 -05003640 * of_clk_del_provider() - Remove a previously registered clock provider
3641 * @np: Device node pointer associated with clock provider
3642 */
3643void of_clk_del_provider(struct device_node *np)
3644{
3645 struct of_clk_provider *cp;
3646
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003647 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003648 list_for_each_entry(cp, &of_clk_providers, link) {
3649 if (cp->node == np) {
3650 list_del(&cp->link);
3651 of_node_put(cp->node);
3652 kfree(cp);
3653 break;
3654 }
3655 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003656 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003657}
3658EXPORT_SYMBOL_GPL(of_clk_del_provider);
3659
Stephen Boydaa795c42017-09-01 16:16:40 -07003660static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3661{
3662 struct device_node **np = res;
3663
3664 if (WARN_ON(!np || !*np))
3665 return 0;
3666
3667 return *np == data;
3668}
3669
3670void devm_of_clk_del_provider(struct device *dev)
3671{
3672 int ret;
3673
3674 ret = devres_release(dev, devm_of_clk_release_provider,
3675 devm_clk_provider_match, dev->of_node);
3676
3677 WARN_ON(ret);
3678}
3679EXPORT_SYMBOL(devm_of_clk_del_provider);
3680
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003681static struct clk_hw *
3682__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3683 struct of_phandle_args *clkspec)
3684{
3685 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003686
Stephen Boyd74002fc2016-08-25 13:35:36 -07003687 if (provider->get_hw)
3688 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003689
Stephen Boyd74002fc2016-08-25 13:35:36 -07003690 clk = provider->get(clkspec, provider->data);
3691 if (IS_ERR(clk))
3692 return ERR_CAST(clk);
3693 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003694}
3695
Stephen Boyd73e0e492015-02-06 11:42:43 -08003696struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3697 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05003698{
3699 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01003700 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Stephen Boydf155d152016-08-15 14:32:23 -07003701 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -05003702
Stephen Boyd306c3422015-02-05 15:39:11 -08003703 if (!clkspec)
3704 return ERR_PTR(-EINVAL);
3705
Grant Likely766e6a42012-04-09 14:50:06 -05003706 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08003707 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003708 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07003709 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003710 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003711 clk = __clk_create_clk(hw, dev_id, con_id);
Stephen Boydf155d152016-08-15 14:32:23 -07003712 }
Stephen Boyd73e0e492015-02-06 11:42:43 -08003713
Stephen Boydf155d152016-08-15 14:32:23 -07003714 if (!IS_ERR(clk)) {
3715 if (!__clk_get(clk)) {
Stephen Boyd73e0e492015-02-06 11:42:43 -08003716 __clk_free_clk(clk);
3717 clk = ERR_PTR(-ENOENT);
3718 }
3719
Grant Likely766e6a42012-04-09 14:50:06 -05003720 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08003721 }
Grant Likely766e6a42012-04-09 14:50:06 -05003722 }
Stephen Boyd306c3422015-02-05 15:39:11 -08003723 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003724
3725 return clk;
3726}
3727
Stephen Boyd306c3422015-02-05 15:39:11 -08003728/**
3729 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3730 * @clkspec: pointer to a clock specifier data structure
3731 *
3732 * This function looks up a struct clk from the registered list of clock
3733 * providers, an input is a clock specifier data structure as returned
3734 * from the of_parse_phandle_with_args() function call.
3735 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003736struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3737{
Stephen Boyd306c3422015-02-05 15:39:11 -08003738 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05003739}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06003740EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05003741
Stephen Boyd929e7f32016-02-19 15:52:32 -08003742/**
3743 * of_clk_get_parent_count() - Count the number of clocks a device node has
3744 * @np: device node to count
3745 *
3746 * Returns: The number of clocks that are possible parents of this node
3747 */
3748unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07003749{
Stephen Boyd929e7f32016-02-19 15:52:32 -08003750 int count;
3751
3752 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3753 if (count < 0)
3754 return 0;
3755
3756 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07003757}
3758EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3759
Grant Likely766e6a42012-04-09 14:50:06 -05003760const char *of_clk_get_parent_name(struct device_node *np, int index)
3761{
3762 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003763 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003764 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003765 const __be32 *vp;
3766 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003767 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003768 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003769 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05003770
Grant Likely766e6a42012-04-09 14:50:06 -05003771 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3772 &clkspec);
3773 if (rc)
3774 return NULL;
3775
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003776 index = clkspec.args_count ? clkspec.args[0] : 0;
3777 count = 0;
3778
3779 /* if there is an indices property, use it to transfer the index
3780 * specified into an array offset for the clock-output-names property.
3781 */
3782 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3783 if (index == pv) {
3784 index = count;
3785 break;
3786 }
3787 count++;
3788 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09003789 /* We went off the end of 'clock-indices' without finding it */
3790 if (prop && !vp)
3791 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003792
Grant Likely766e6a42012-04-09 14:50:06 -05003793 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003794 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003795 &clk_name) < 0) {
3796 /*
3797 * Best effort to get the name if the clock has been
3798 * registered with the framework. If the clock isn't
3799 * registered, we return the node name as the name of
3800 * the clock as long as #clock-cells = 0.
3801 */
3802 clk = of_clk_get_from_provider(&clkspec);
3803 if (IS_ERR(clk)) {
3804 if (clkspec.args_count == 0)
3805 clk_name = clkspec.np->name;
3806 else
3807 clk_name = NULL;
3808 } else {
3809 clk_name = __clk_get_name(clk);
3810 clk_put(clk);
3811 }
3812 }
3813
Grant Likely766e6a42012-04-09 14:50:06 -05003814
3815 of_node_put(clkspec.np);
3816 return clk_name;
3817}
3818EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3819
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05003820/**
3821 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3822 * number of parents
3823 * @np: Device node pointer associated with clock provider
3824 * @parents: pointer to char array that hold the parents' names
3825 * @size: size of the @parents array
3826 *
3827 * Return: number of parents for the clock node.
3828 */
3829int of_clk_parent_fill(struct device_node *np, const char **parents,
3830 unsigned int size)
3831{
3832 unsigned int i = 0;
3833
3834 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3835 i++;
3836
3837 return i;
3838}
3839EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3840
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003841struct clock_provider {
3842 of_clk_init_cb_t clk_init_cb;
3843 struct device_node *np;
3844 struct list_head node;
3845};
3846
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003847/*
3848 * This function looks for a parent clock. If there is one, then it
3849 * checks that the provider for this parent clock was initialized, in
3850 * this case the parent clock will be ready.
3851 */
3852static int parent_ready(struct device_node *np)
3853{
3854 int i = 0;
3855
3856 while (true) {
3857 struct clk *clk = of_clk_get(np, i);
3858
3859 /* this parent is ready we can check the next one */
3860 if (!IS_ERR(clk)) {
3861 clk_put(clk);
3862 i++;
3863 continue;
3864 }
3865
3866 /* at least one parent is not ready, we exit now */
3867 if (PTR_ERR(clk) == -EPROBE_DEFER)
3868 return 0;
3869
3870 /*
3871 * Here we make assumption that the device tree is
3872 * written correctly. So an error means that there is
3873 * no more parent. As we didn't exit yet, then the
3874 * previous parent are ready. If there is no clock
3875 * parent, no need to wait for them, then we can
3876 * consider their absence as being ready
3877 */
3878 return 1;
3879 }
3880}
3881
Grant Likely766e6a42012-04-09 14:50:06 -05003882/**
Lee Jonesd56f8992016-02-11 13:19:11 -08003883 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3884 * @np: Device node pointer associated with clock provider
3885 * @index: clock index
3886 * @flags: pointer to clk_core->flags
3887 *
3888 * Detects if the clock-critical property exists and, if so, sets the
3889 * corresponding CLK_IS_CRITICAL flag.
3890 *
3891 * Do not use this function. It exists only for legacy Device Tree
3892 * bindings, such as the one-clock-per-node style that are outdated.
3893 * Those bindings typically put all clock data into .dts and the Linux
3894 * driver has no clock data, thus making it impossible to set this flag
3895 * correctly from the driver. Only those drivers may call
3896 * of_clk_detect_critical from their setup functions.
3897 *
3898 * Return: error code or zero on success
3899 */
3900int of_clk_detect_critical(struct device_node *np,
3901 int index, unsigned long *flags)
3902{
3903 struct property *prop;
3904 const __be32 *cur;
3905 uint32_t idx;
3906
3907 if (!np || !flags)
3908 return -EINVAL;
3909
3910 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
3911 if (index == idx)
3912 *flags |= CLK_IS_CRITICAL;
3913
3914 return 0;
3915}
3916
3917/**
Grant Likely766e6a42012-04-09 14:50:06 -05003918 * of_clk_init() - Scan and init clock providers from the DT
3919 * @matches: array of compatible values and init functions for providers.
3920 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003921 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003922 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003923 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003924 */
3925void __init of_clk_init(const struct of_device_id *matches)
3926{
Alex Elder7f7ed582013-08-22 11:31:31 -05003927 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003928 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003929 struct clock_provider *clk_provider, *next;
3930 bool is_init_done;
3931 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07003932 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05003933
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303934 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003935 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303936
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003937 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003938 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003939 struct clock_provider *parent;
3940
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01003941 if (!of_device_is_available(np))
3942 continue;
3943
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003944 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
3945 if (!parent) {
3946 list_for_each_entry_safe(clk_provider, next,
3947 &clk_provider_list, node) {
3948 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003949 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003950 kfree(clk_provider);
3951 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003952 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003953 return;
3954 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003955
3956 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003957 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003958 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003959 }
3960
3961 while (!list_empty(&clk_provider_list)) {
3962 is_init_done = false;
3963 list_for_each_entry_safe(clk_provider, next,
3964 &clk_provider_list, node) {
3965 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003966
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02003967 /* Don't populate platform devices */
3968 of_node_set_flag(clk_provider->np,
3969 OF_POPULATED);
3970
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003971 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003972 of_clk_set_defaults(clk_provider->np, true);
3973
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003974 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003975 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003976 kfree(clk_provider);
3977 is_init_done = true;
3978 }
3979 }
3980
3981 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003982 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003983 * remaining providers during the last loop, so now we
3984 * initialize all the remaining ones unconditionally
3985 * in case the clock parent was not mandatory
3986 */
3987 if (!is_init_done)
3988 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003989 }
3990}
3991#endif