blob: f6fe5e5595ca3ae56e0fc301b4c68641232dd7bd [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;
Stephen Boyd50595f82015-02-06 11:42:44 -080090 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080091};
92
Marek Szyprowski9a34b452017-08-21 10:04:59 +020093/*** runtime pm ***/
94static int clk_pm_runtime_get(struct clk_core *core)
95{
96 int ret = 0;
97
98 if (!core->dev)
99 return 0;
100
101 ret = pm_runtime_get_sync(core->dev);
102 return ret < 0 ? ret : 0;
103}
104
105static void clk_pm_runtime_put(struct clk_core *core)
106{
107 if (!core->dev)
108 return;
109
110 pm_runtime_put_sync(core->dev);
111}
112
Mike Turquetteeab89f62013-03-28 13:59:01 -0700113/*** locking ***/
114static void clk_prepare_lock(void)
115{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700116 if (!mutex_trylock(&prepare_lock)) {
117 if (prepare_owner == current) {
118 prepare_refcnt++;
119 return;
120 }
121 mutex_lock(&prepare_lock);
122 }
123 WARN_ON_ONCE(prepare_owner != NULL);
124 WARN_ON_ONCE(prepare_refcnt != 0);
125 prepare_owner = current;
126 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700127}
128
129static void clk_prepare_unlock(void)
130{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700131 WARN_ON_ONCE(prepare_owner != current);
132 WARN_ON_ONCE(prepare_refcnt == 0);
133
134 if (--prepare_refcnt)
135 return;
136 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700137 mutex_unlock(&prepare_lock);
138}
139
140static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700141 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700142{
143 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700144
145 if (!spin_trylock_irqsave(&enable_lock, flags)) {
146 if (enable_owner == current) {
147 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700148 __acquire(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700149 return flags;
150 }
151 spin_lock_irqsave(&enable_lock, flags);
152 }
153 WARN_ON_ONCE(enable_owner != NULL);
154 WARN_ON_ONCE(enable_refcnt != 0);
155 enable_owner = current;
156 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700157 return flags;
158}
159
160static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700161 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700162{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700163 WARN_ON_ONCE(enable_owner != current);
164 WARN_ON_ONCE(enable_refcnt == 0);
165
Stephen Boyda57aa182015-07-24 12:24:48 -0700166 if (--enable_refcnt) {
167 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700168 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700169 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700170 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700171 spin_unlock_irqrestore(&enable_lock, flags);
172}
173
Jerome Brunete55a8392017-12-01 22:51:56 +0100174static bool clk_core_rate_is_protected(struct clk_core *core)
175{
176 return core->protect_count;
177}
178
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700179static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530180{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200181 bool ret = false;
182
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700183 /*
184 * .is_prepared is optional for clocks that can prepare
185 * fall back to software usage counter if it is missing
186 */
187 if (!core->ops->is_prepared)
188 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530189
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200190 if (!clk_pm_runtime_get(core)) {
191 ret = core->ops->is_prepared(core->hw);
192 clk_pm_runtime_put(core);
193 }
194
195 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530196}
197
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700198static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530199{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200200 bool ret = false;
201
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700202 /*
203 * .is_enabled is only mandatory for clocks that gate
204 * fall back to software usage counter if .is_enabled is missing
205 */
206 if (!core->ops->is_enabled)
207 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530208
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200209 /*
210 * Check if clock controller's device is runtime active before
211 * calling .is_enabled callback. If not, assume that clock is
212 * disabled, because we might be called from atomic context, from
213 * which pm_runtime_get() is not allowed.
214 * This function is called mainly from clk_disable_unused_subtree,
215 * which ensures proper runtime pm activation of controller before
216 * taking enable spinlock, but the below check is needed if one tries
217 * to call it from other places.
218 */
219 if (core->dev) {
220 pm_runtime_get_noresume(core->dev);
221 if (!pm_runtime_active(core->dev)) {
222 ret = false;
223 goto done;
224 }
225 }
226
227 ret = core->ops->is_enabled(core->hw);
228done:
229 clk_pm_runtime_put(core);
230
231 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530232}
233
Mike Turquetteb24764902012-03-15 23:11:19 -0700234/*** helper functions ***/
235
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200236const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700237{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100238 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700239}
Niels de Vos48950842012-12-13 13:12:25 +0100240EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700241
Stephen Boyde7df6f62015-08-12 13:04:56 -0700242const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700243{
244 return hw->core->name;
245}
246EXPORT_SYMBOL_GPL(clk_hw_get_name);
247
Russ Dill65800b22012-11-26 11:20:09 -0800248struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700249{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100250 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700251}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800252EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700253
Stephen Boyde7df6f62015-08-12 13:04:56 -0700254unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700255{
256 return hw->core->num_parents;
257}
258EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
259
Stephen Boyde7df6f62015-08-12 13:04:56 -0700260struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700261{
262 return hw->core->parent ? hw->core->parent->hw : NULL;
263}
264EXPORT_SYMBOL_GPL(clk_hw_get_parent);
265
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700266static struct clk_core *__clk_lookup_subtree(const char *name,
267 struct clk_core *core)
268{
269 struct clk_core *child;
270 struct clk_core *ret;
271
272 if (!strcmp(core->name, name))
273 return core;
274
275 hlist_for_each_entry(child, &core->children, child_node) {
276 ret = __clk_lookup_subtree(name, child);
277 if (ret)
278 return ret;
279 }
280
281 return NULL;
282}
283
284static struct clk_core *clk_core_lookup(const char *name)
285{
286 struct clk_core *root_clk;
287 struct clk_core *ret;
288
289 if (!name)
290 return NULL;
291
292 /* search the 'proper' clk tree first */
293 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
294 ret = __clk_lookup_subtree(name, root_clk);
295 if (ret)
296 return ret;
297 }
298
299 /* if not found, then search the orphan tree */
300 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
301 ret = __clk_lookup_subtree(name, root_clk);
302 if (ret)
303 return ret;
304 }
305
306 return NULL;
307}
308
Stephen Boydd6968fc2015-04-30 13:54:13 -0700309static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100310 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100311{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700312 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100313 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900314
315 if (!core->parents[index])
316 core->parents[index] =
317 clk_core_lookup(core->parent_names[index]);
318
319 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100320}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100321
Stephen Boyde7df6f62015-08-12 13:04:56 -0700322struct clk_hw *
323clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700324{
325 struct clk_core *parent;
326
327 parent = clk_core_get_parent_by_index(hw->core, index);
328
329 return !parent ? NULL : parent->hw;
330}
331EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
332
Russ Dill65800b22012-11-26 11:20:09 -0800333unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700334{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100335 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700336}
337
Stephen Boydd6968fc2015-04-30 13:54:13 -0700338static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700339{
340 unsigned long ret;
341
Stephen Boydd6968fc2015-04-30 13:54:13 -0700342 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530343 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700344 goto out;
345 }
346
Stephen Boydd6968fc2015-04-30 13:54:13 -0700347 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700348
Stephen Boyd47b0eeb2016-02-02 17:24:56 -0800349 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -0700350 goto out;
351
Stephen Boydd6968fc2015-04-30 13:54:13 -0700352 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530353 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700354
355out:
356 return ret;
357}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100358
Stephen Boyde7df6f62015-08-12 13:04:56 -0700359unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700360{
361 return clk_core_get_rate_nolock(hw->core);
362}
363EXPORT_SYMBOL_GPL(clk_hw_get_rate);
364
Stephen Boydd6968fc2015-04-30 13:54:13 -0700365static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100366{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700367 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100368 return 0;
369
Stephen Boydd6968fc2015-04-30 13:54:13 -0700370 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100371}
372
Russ Dill65800b22012-11-26 11:20:09 -0800373unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700374{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100375 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700376}
Thierry Redingb05c6832013-09-03 09:43:51 +0200377EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700378
Stephen Boyde7df6f62015-08-12 13:04:56 -0700379unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700380{
381 return hw->core->flags;
382}
383EXPORT_SYMBOL_GPL(clk_hw_get_flags);
384
Stephen Boyde7df6f62015-08-12 13:04:56 -0700385bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700386{
387 return clk_core_is_prepared(hw->core);
388}
389
Jerome Brunete55a8392017-12-01 22:51:56 +0100390bool clk_hw_rate_is_protected(const struct clk_hw *hw)
391{
392 return clk_core_rate_is_protected(hw->core);
393}
394
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200395bool clk_hw_is_enabled(const struct clk_hw *hw)
396{
397 return clk_core_is_enabled(hw->core);
398}
399
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100400bool __clk_is_enabled(struct clk *clk)
401{
402 if (!clk)
403 return false;
404
405 return clk_core_is_enabled(clk->core);
406}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800407EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700408
Stephen Boyd15a02c12015-01-19 18:05:28 -0800409static bool mux_is_better_rate(unsigned long rate, unsigned long now,
410 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100411{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800412 if (flags & CLK_MUX_ROUND_CLOSEST)
413 return abs(now - rate) < abs(best - rate);
414
415 return now <= rate && now > best;
416}
417
Boris Brezillon0817b622015-07-07 20:48:08 +0200418static int
419clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800420 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100421{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100422 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200423 int i, num_parents, ret;
424 unsigned long best = 0;
425 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100426
427 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100428 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
429 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200430 if (core->flags & CLK_SET_RATE_PARENT) {
431 ret = __clk_determine_rate(parent ? parent->hw : NULL,
432 &parent_req);
433 if (ret)
434 return ret;
435
436 best = parent_req.rate;
437 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100438 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200439 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100440 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200441 }
442
James Hogane366fdd2013-07-29 12:25:02 +0100443 goto out;
444 }
445
446 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100447 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100448 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100449 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100450 if (!parent)
451 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200452
453 if (core->flags & CLK_SET_RATE_PARENT) {
454 parent_req = *req;
455 ret = __clk_determine_rate(parent->hw, &parent_req);
456 if (ret)
457 continue;
458 } else {
459 parent_req.rate = clk_core_get_rate_nolock(parent);
460 }
461
462 if (mux_is_better_rate(req->rate, parent_req.rate,
463 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100464 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200465 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100466 }
467 }
468
Boris Brezillon57d866e2015-07-09 22:39:38 +0200469 if (!best_parent)
470 return -EINVAL;
471
James Hogane366fdd2013-07-29 12:25:02 +0100472out:
473 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200474 req->best_parent_hw = best_parent->hw;
475 req->best_parent_rate = best;
476 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100477
Boris Brezillon0817b622015-07-07 20:48:08 +0200478 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100479}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800480
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100481struct clk *__clk_lookup(const char *name)
482{
483 struct clk_core *core = clk_core_lookup(name);
484
485 return !core ? NULL : core->hw->clk;
486}
487
Stephen Boydd6968fc2015-04-30 13:54:13 -0700488static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100489 unsigned long *min_rate,
490 unsigned long *max_rate)
491{
492 struct clk *clk_user;
493
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700494 *min_rate = core->min_rate;
495 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100496
Stephen Boydd6968fc2015-04-30 13:54:13 -0700497 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100498 *min_rate = max(*min_rate, clk_user->min_rate);
499
Stephen Boydd6968fc2015-04-30 13:54:13 -0700500 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100501 *max_rate = min(*max_rate, clk_user->max_rate);
502}
503
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700504void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
505 unsigned long max_rate)
506{
507 hw->core->min_rate = min_rate;
508 hw->core->max_rate = max_rate;
509}
510EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
511
Stephen Boyd15a02c12015-01-19 18:05:28 -0800512/*
513 * Helper for finding best parent to provide a given frequency. This can be used
514 * directly as a determine_rate callback (e.g. for a mux), or from a more
515 * complex clock that may combine a mux with other operations.
516 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200517int __clk_mux_determine_rate(struct clk_hw *hw,
518 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800519{
Boris Brezillon0817b622015-07-07 20:48:08 +0200520 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800521}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800522EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100523
Boris Brezillon0817b622015-07-07 20:48:08 +0200524int __clk_mux_determine_rate_closest(struct clk_hw *hw,
525 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800526{
Boris Brezillon0817b622015-07-07 20:48:08 +0200527 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800528}
529EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
530
Mike Turquetteb24764902012-03-15 23:11:19 -0700531/*** clk api ***/
532
Jerome Brunete55a8392017-12-01 22:51:56 +0100533static void clk_core_rate_unprotect(struct clk_core *core)
534{
535 lockdep_assert_held(&prepare_lock);
536
537 if (!core)
538 return;
539
540 if (WARN_ON(core->protect_count == 0))
541 return;
542
543 if (--core->protect_count > 0)
544 return;
545
546 clk_core_rate_unprotect(core->parent);
547}
548
549static int clk_core_rate_nuke_protect(struct clk_core *core)
550{
551 int ret;
552
553 lockdep_assert_held(&prepare_lock);
554
555 if (!core)
556 return -EINVAL;
557
558 if (core->protect_count == 0)
559 return 0;
560
561 ret = core->protect_count;
562 core->protect_count = 1;
563 clk_core_rate_unprotect(core);
564
565 return ret;
566}
567
568static void clk_core_rate_protect(struct clk_core *core)
569{
570 lockdep_assert_held(&prepare_lock);
571
572 if (!core)
573 return;
574
575 if (core->protect_count == 0)
576 clk_core_rate_protect(core->parent);
577
578 core->protect_count++;
579}
580
581static void clk_core_rate_restore_protect(struct clk_core *core, int count)
582{
583 lockdep_assert_held(&prepare_lock);
584
585 if (!core)
586 return;
587
588 if (count == 0)
589 return;
590
591 clk_core_rate_protect(core);
592 core->protect_count = count;
593}
594
Stephen Boydd6968fc2015-04-30 13:54:13 -0700595static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700596{
Stephen Boyda6334722015-05-06 17:00:54 -0700597 lockdep_assert_held(&prepare_lock);
598
Stephen Boydd6968fc2015-04-30 13:54:13 -0700599 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700600 return;
601
Stephen Boydd6968fc2015-04-30 13:54:13 -0700602 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700603 return;
604
Lee Jones2e20fbf2016-02-11 13:19:10 -0800605 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
606 return;
607
Stephen Boydd6968fc2015-04-30 13:54:13 -0700608 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700609 return;
610
Stephen Boydd6968fc2015-04-30 13:54:13 -0700611 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700612
Stephen Boydd6968fc2015-04-30 13:54:13 -0700613 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800614
Stephen Boydd6968fc2015-04-30 13:54:13 -0700615 if (core->ops->unprepare)
616 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700617
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200618 clk_pm_runtime_put(core);
619
Stephen Boydd6968fc2015-04-30 13:54:13 -0700620 trace_clk_unprepare_complete(core);
621 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700622}
623
Dong Aishenga6adc302016-06-30 17:31:11 +0800624static void clk_core_unprepare_lock(struct clk_core *core)
625{
626 clk_prepare_lock();
627 clk_core_unprepare(core);
628 clk_prepare_unlock();
629}
630
Mike Turquetteb24764902012-03-15 23:11:19 -0700631/**
632 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200633 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700634 *
635 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
636 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
637 * if the operation may sleep. One example is a clk which is accessed over
638 * I2c. In the complex case a clk gate operation may require a fast and a slow
639 * part. It is this reason that clk_unprepare and clk_disable are not mutually
640 * exclusive. In fact clk_disable must be called before clk_unprepare.
641 */
642void clk_unprepare(struct clk *clk)
643{
Stephen Boyd63589e92014-03-26 16:06:37 -0700644 if (IS_ERR_OR_NULL(clk))
645 return;
646
Dong Aishenga6adc302016-06-30 17:31:11 +0800647 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700648}
649EXPORT_SYMBOL_GPL(clk_unprepare);
650
Stephen Boydd6968fc2015-04-30 13:54:13 -0700651static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700652{
653 int ret = 0;
654
Stephen Boyda6334722015-05-06 17:00:54 -0700655 lockdep_assert_held(&prepare_lock);
656
Stephen Boydd6968fc2015-04-30 13:54:13 -0700657 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700658 return 0;
659
Stephen Boydd6968fc2015-04-30 13:54:13 -0700660 if (core->prepare_count == 0) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200661 ret = clk_pm_runtime_get(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700662 if (ret)
663 return ret;
664
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200665 ret = clk_core_prepare(core->parent);
666 if (ret)
667 goto runtime_put;
668
Stephen Boydd6968fc2015-04-30 13:54:13 -0700669 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800670
Stephen Boydd6968fc2015-04-30 13:54:13 -0700671 if (core->ops->prepare)
672 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800673
Stephen Boydd6968fc2015-04-30 13:54:13 -0700674 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800675
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200676 if (ret)
677 goto unprepare;
Mike Turquetteb24764902012-03-15 23:11:19 -0700678 }
679
Stephen Boydd6968fc2015-04-30 13:54:13 -0700680 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700681
682 return 0;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200683unprepare:
684 clk_core_unprepare(core->parent);
685runtime_put:
686 clk_pm_runtime_put(core);
687 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700688}
689
Dong Aishenga6adc302016-06-30 17:31:11 +0800690static int clk_core_prepare_lock(struct clk_core *core)
691{
692 int ret;
693
694 clk_prepare_lock();
695 ret = clk_core_prepare(core);
696 clk_prepare_unlock();
697
698 return ret;
699}
700
Mike Turquetteb24764902012-03-15 23:11:19 -0700701/**
702 * clk_prepare - prepare a clock source
703 * @clk: the clk being prepared
704 *
705 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
706 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
707 * operation may sleep. One example is a clk which is accessed over I2c. In
708 * the complex case a clk ungate operation may require a fast and a slow part.
709 * It is this reason that clk_prepare and clk_enable are not mutually
710 * exclusive. In fact clk_prepare must be called before clk_enable.
711 * Returns 0 on success, -EERROR otherwise.
712 */
713int clk_prepare(struct clk *clk)
714{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100715 if (!clk)
716 return 0;
717
Dong Aishenga6adc302016-06-30 17:31:11 +0800718 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700719}
720EXPORT_SYMBOL_GPL(clk_prepare);
721
Stephen Boydd6968fc2015-04-30 13:54:13 -0700722static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700723{
Stephen Boyda6334722015-05-06 17:00:54 -0700724 lockdep_assert_held(&enable_lock);
725
Stephen Boydd6968fc2015-04-30 13:54:13 -0700726 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700727 return;
728
Stephen Boydd6968fc2015-04-30 13:54:13 -0700729 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700730 return;
731
Lee Jones2e20fbf2016-02-11 13:19:10 -0800732 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
733 return;
734
Stephen Boydd6968fc2015-04-30 13:54:13 -0700735 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700736 return;
737
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700738 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800739
Stephen Boydd6968fc2015-04-30 13:54:13 -0700740 if (core->ops->disable)
741 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700742
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700743 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800744
Stephen Boydd6968fc2015-04-30 13:54:13 -0700745 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100746}
747
Dong Aishenga6adc302016-06-30 17:31:11 +0800748static void clk_core_disable_lock(struct clk_core *core)
749{
750 unsigned long flags;
751
752 flags = clk_enable_lock();
753 clk_core_disable(core);
754 clk_enable_unlock(flags);
755}
756
Mike Turquetteb24764902012-03-15 23:11:19 -0700757/**
758 * clk_disable - gate a clock
759 * @clk: the clk being gated
760 *
761 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
762 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
763 * clk if the operation is fast and will never sleep. One example is a
764 * SoC-internal clk which is controlled via simple register writes. In the
765 * complex case a clk gate operation may require a fast and a slow part. It is
766 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
767 * In fact clk_disable must be called before clk_unprepare.
768 */
769void clk_disable(struct clk *clk)
770{
Stephen Boyd63589e92014-03-26 16:06:37 -0700771 if (IS_ERR_OR_NULL(clk))
772 return;
773
Dong Aishenga6adc302016-06-30 17:31:11 +0800774 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700775}
776EXPORT_SYMBOL_GPL(clk_disable);
777
Stephen Boydd6968fc2015-04-30 13:54:13 -0700778static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700779{
780 int ret = 0;
781
Stephen Boyda6334722015-05-06 17:00:54 -0700782 lockdep_assert_held(&enable_lock);
783
Stephen Boydd6968fc2015-04-30 13:54:13 -0700784 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700785 return 0;
786
Stephen Boydd6968fc2015-04-30 13:54:13 -0700787 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700788 return -ESHUTDOWN;
789
Stephen Boydd6968fc2015-04-30 13:54:13 -0700790 if (core->enable_count == 0) {
791 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700792
793 if (ret)
794 return ret;
795
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700796 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800797
Stephen Boydd6968fc2015-04-30 13:54:13 -0700798 if (core->ops->enable)
799 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800800
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700801 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800802
803 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700804 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800805 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700806 }
807 }
808
Stephen Boydd6968fc2015-04-30 13:54:13 -0700809 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700810 return 0;
811}
812
Dong Aishenga6adc302016-06-30 17:31:11 +0800813static int clk_core_enable_lock(struct clk_core *core)
814{
815 unsigned long flags;
816 int ret;
817
818 flags = clk_enable_lock();
819 ret = clk_core_enable(core);
820 clk_enable_unlock(flags);
821
822 return ret;
823}
824
Mike Turquetteb24764902012-03-15 23:11:19 -0700825/**
826 * clk_enable - ungate a clock
827 * @clk: the clk being ungated
828 *
829 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
830 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
831 * if the operation will never sleep. One example is a SoC-internal clk which
832 * is controlled via simple register writes. In the complex case a clk ungate
833 * operation may require a fast and a slow part. It is this reason that
834 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
835 * must be called before clk_enable. Returns 0 on success, -EERROR
836 * otherwise.
837 */
838int clk_enable(struct clk *clk)
839{
Dong Aisheng864e1602015-04-30 14:02:19 -0700840 if (!clk)
841 return 0;
842
Dong Aishenga6adc302016-06-30 17:31:11 +0800843 return clk_core_enable_lock(clk->core);
844}
845EXPORT_SYMBOL_GPL(clk_enable);
846
847static int clk_core_prepare_enable(struct clk_core *core)
848{
849 int ret;
850
851 ret = clk_core_prepare_lock(core);
852 if (ret)
853 return ret;
854
855 ret = clk_core_enable_lock(core);
856 if (ret)
857 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700858
859 return ret;
860}
Dong Aishenga6adc302016-06-30 17:31:11 +0800861
862static void clk_core_disable_unprepare(struct clk_core *core)
863{
864 clk_core_disable_lock(core);
865 clk_core_unprepare_lock(core);
866}
Mike Turquetteb24764902012-03-15 23:11:19 -0700867
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800868static void clk_unprepare_unused_subtree(struct clk_core *core)
869{
870 struct clk_core *child;
871
872 lockdep_assert_held(&prepare_lock);
873
874 hlist_for_each_entry(child, &core->children, child_node)
875 clk_unprepare_unused_subtree(child);
876
877 if (core->prepare_count)
878 return;
879
880 if (core->flags & CLK_IGNORE_UNUSED)
881 return;
882
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200883 if (clk_pm_runtime_get(core))
884 return;
885
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800886 if (clk_core_is_prepared(core)) {
887 trace_clk_unprepare(core);
888 if (core->ops->unprepare_unused)
889 core->ops->unprepare_unused(core->hw);
890 else if (core->ops->unprepare)
891 core->ops->unprepare(core->hw);
892 trace_clk_unprepare_complete(core);
893 }
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200894
895 clk_pm_runtime_put(core);
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800896}
897
898static void clk_disable_unused_subtree(struct clk_core *core)
899{
900 struct clk_core *child;
901 unsigned long flags;
902
903 lockdep_assert_held(&prepare_lock);
904
905 hlist_for_each_entry(child, &core->children, child_node)
906 clk_disable_unused_subtree(child);
907
Dong Aishenga4b35182016-06-30 17:31:13 +0800908 if (core->flags & CLK_OPS_PARENT_ENABLE)
909 clk_core_prepare_enable(core->parent);
910
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200911 if (clk_pm_runtime_get(core))
912 goto unprepare_out;
913
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800914 flags = clk_enable_lock();
915
916 if (core->enable_count)
917 goto unlock_out;
918
919 if (core->flags & CLK_IGNORE_UNUSED)
920 goto unlock_out;
921
922 /*
923 * some gate clocks have special needs during the disable-unused
924 * sequence. call .disable_unused if available, otherwise fall
925 * back to .disable
926 */
927 if (clk_core_is_enabled(core)) {
928 trace_clk_disable(core);
929 if (core->ops->disable_unused)
930 core->ops->disable_unused(core->hw);
931 else if (core->ops->disable)
932 core->ops->disable(core->hw);
933 trace_clk_disable_complete(core);
934 }
935
936unlock_out:
937 clk_enable_unlock(flags);
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200938 clk_pm_runtime_put(core);
939unprepare_out:
Dong Aishenga4b35182016-06-30 17:31:13 +0800940 if (core->flags & CLK_OPS_PARENT_ENABLE)
941 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800942}
943
944static bool clk_ignore_unused;
945static int __init clk_ignore_unused_setup(char *__unused)
946{
947 clk_ignore_unused = true;
948 return 1;
949}
950__setup("clk_ignore_unused", clk_ignore_unused_setup);
951
952static int clk_disable_unused(void)
953{
954 struct clk_core *core;
955
956 if (clk_ignore_unused) {
957 pr_warn("clk: Not disabling unused clocks\n");
958 return 0;
959 }
960
961 clk_prepare_lock();
962
963 hlist_for_each_entry(core, &clk_root_list, child_node)
964 clk_disable_unused_subtree(core);
965
966 hlist_for_each_entry(core, &clk_orphan_list, child_node)
967 clk_disable_unused_subtree(core);
968
969 hlist_for_each_entry(core, &clk_root_list, child_node)
970 clk_unprepare_unused_subtree(core);
971
972 hlist_for_each_entry(core, &clk_orphan_list, child_node)
973 clk_unprepare_unused_subtree(core);
974
975 clk_prepare_unlock();
976
977 return 0;
978}
979late_initcall_sync(clk_disable_unused);
980
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +0100981static int clk_core_determine_round_nolock(struct clk_core *core,
982 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -0700983{
Boris Brezillon0817b622015-07-07 20:48:08 +0200984 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700985
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100986 lockdep_assert_held(&prepare_lock);
987
Stephen Boydd6968fc2015-04-30 13:54:13 -0700988 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700989 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700990
Jerome Brunete55a8392017-12-01 22:51:56 +0100991 if (clk_core_rate_is_protected(core)) {
992 req->rate = core->rate;
993 } else if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +0200994 return core->ops->determine_rate(core->hw, req);
995 } else if (core->ops->round_rate) {
996 rate = core->ops->round_rate(core->hw, req->rate,
997 &req->best_parent_rate);
998 if (rate < 0)
999 return rate;
1000
1001 req->rate = rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001002 } else {
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001003 return -EINVAL;
Boris Brezillon0817b622015-07-07 20:48:08 +02001004 }
1005
1006 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001007}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001008
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001009static void clk_core_init_rate_req(struct clk_core * const core,
1010 struct clk_rate_request *req)
1011{
1012 struct clk_core *parent;
1013
1014 if (WARN_ON(!core || !req))
1015 return;
1016
1017 parent = core->parent;
1018 if (parent) {
1019 req->best_parent_hw = parent->hw;
1020 req->best_parent_rate = parent->rate;
1021 } else {
1022 req->best_parent_hw = NULL;
1023 req->best_parent_rate = 0;
1024 }
1025}
1026
1027static bool clk_core_can_round(struct clk_core * const core)
1028{
1029 if (core->ops->determine_rate || core->ops->round_rate)
1030 return true;
1031
1032 return false;
1033}
1034
1035static int clk_core_round_rate_nolock(struct clk_core *core,
1036 struct clk_rate_request *req)
1037{
1038 lockdep_assert_held(&prepare_lock);
1039
1040 if (!core)
1041 return 0;
1042
1043 clk_core_init_rate_req(core, req);
1044
1045 if (clk_core_can_round(core))
1046 return clk_core_determine_round_nolock(core, req);
1047 else if (core->flags & CLK_SET_RATE_PARENT)
1048 return clk_core_round_rate_nolock(core->parent, req);
1049
1050 req->rate = core->rate;
1051 return 0;
1052}
1053
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001054/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001055 * __clk_determine_rate - get the closest rate actually supported by a clock
1056 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001057 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001058 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001059 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001060 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001061int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001062{
Boris Brezillon0817b622015-07-07 20:48:08 +02001063 if (!hw) {
1064 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001065 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001066 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001067
Boris Brezillon0817b622015-07-07 20:48:08 +02001068 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001069}
1070EXPORT_SYMBOL_GPL(__clk_determine_rate);
1071
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001072unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1073{
1074 int ret;
1075 struct clk_rate_request req;
1076
1077 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1078 req.rate = rate;
1079
1080 ret = clk_core_round_rate_nolock(hw->core, &req);
1081 if (ret)
1082 return 0;
1083
1084 return req.rate;
1085}
1086EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1087
Mike Turquetteb24764902012-03-15 23:11:19 -07001088/**
1089 * clk_round_rate - round the given rate for a clk
1090 * @clk: the clk for which we are rounding a rate
1091 * @rate: the rate which is to be rounded
1092 *
1093 * Takes in a rate as input and rounds it to a rate that the clk can actually
1094 * use which is then returned. If clk doesn't support round_rate operation
1095 * then the parent rate is returned.
1096 */
1097long clk_round_rate(struct clk *clk, unsigned long rate)
1098{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001099 struct clk_rate_request req;
1100 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001101
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001102 if (!clk)
1103 return 0;
1104
Mike Turquetteeab89f62013-03-28 13:59:01 -07001105 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001106
1107 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1108 req.rate = rate;
1109
1110 ret = clk_core_round_rate_nolock(clk->core, &req);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001111 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001112
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001113 if (ret)
1114 return ret;
1115
1116 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001117}
1118EXPORT_SYMBOL_GPL(clk_round_rate);
1119
1120/**
1121 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001122 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001123 * @msg: clk notifier type (see include/linux/clk.h)
1124 * @old_rate: old clk rate
1125 * @new_rate: new clk rate
1126 *
1127 * Triggers a notifier call chain on the clk rate-change notification
1128 * for 'clk'. Passes a pointer to the struct clk and the previous
1129 * and current rates to the notifier callback. Intended to be called by
1130 * internal clock code only. Returns NOTIFY_DONE from the last driver
1131 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1132 * a driver returns that.
1133 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001134static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001135 unsigned long old_rate, unsigned long new_rate)
1136{
1137 struct clk_notifier *cn;
1138 struct clk_notifier_data cnd;
1139 int ret = NOTIFY_DONE;
1140
Mike Turquetteb24764902012-03-15 23:11:19 -07001141 cnd.old_rate = old_rate;
1142 cnd.new_rate = new_rate;
1143
1144 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001145 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001146 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001147 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1148 &cnd);
Peter De Schrijver17c34c52017-03-21 12:16:26 +02001149 if (ret & NOTIFY_STOP_MASK)
1150 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001151 }
1152 }
1153
1154 return ret;
1155}
1156
1157/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001158 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001159 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001160 *
1161 * Walks the subtree of clks starting with clk and recalculates accuracies as
1162 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001163 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001164 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001165 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001166static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001167{
1168 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001169 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001170
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001171 lockdep_assert_held(&prepare_lock);
1172
Stephen Boydd6968fc2015-04-30 13:54:13 -07001173 if (core->parent)
1174 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001175
Stephen Boydd6968fc2015-04-30 13:54:13 -07001176 if (core->ops->recalc_accuracy)
1177 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001178 parent_accuracy);
1179 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001180 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001181
Stephen Boydd6968fc2015-04-30 13:54:13 -07001182 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001183 __clk_recalc_accuracies(child);
1184}
1185
Stephen Boydd6968fc2015-04-30 13:54:13 -07001186static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001187{
1188 unsigned long accuracy;
1189
1190 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001191 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1192 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001193
Stephen Boydd6968fc2015-04-30 13:54:13 -07001194 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001195 clk_prepare_unlock();
1196
1197 return accuracy;
1198}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001199
1200/**
1201 * clk_get_accuracy - return the accuracy of clk
1202 * @clk: the clk whose accuracy is being returned
1203 *
1204 * Simply returns the cached accuracy of the clk, unless
1205 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1206 * issued.
1207 * If clk is NULL then returns 0.
1208 */
1209long clk_get_accuracy(struct clk *clk)
1210{
1211 if (!clk)
1212 return 0;
1213
1214 return clk_core_get_accuracy(clk->core);
1215}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001216EXPORT_SYMBOL_GPL(clk_get_accuracy);
1217
Stephen Boydd6968fc2015-04-30 13:54:13 -07001218static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001219 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001220{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001221 unsigned long rate = parent_rate;
1222
1223 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1224 rate = core->ops->recalc_rate(core->hw, parent_rate);
1225 clk_pm_runtime_put(core);
1226 }
1227 return rate;
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001228}
1229
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001230/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001231 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001232 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001233 * @msg: notification type (see include/linux/clk.h)
1234 *
1235 * Walks the subtree of clks starting with clk and recalculates rates as it
1236 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001237 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001238 *
1239 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1240 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001241 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001242static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001243{
1244 unsigned long old_rate;
1245 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001246 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001247
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001248 lockdep_assert_held(&prepare_lock);
1249
Stephen Boydd6968fc2015-04-30 13:54:13 -07001250 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001251
Stephen Boydd6968fc2015-04-30 13:54:13 -07001252 if (core->parent)
1253 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001254
Stephen Boydd6968fc2015-04-30 13:54:13 -07001255 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001256
1257 /*
1258 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1259 * & ABORT_RATE_CHANGE notifiers
1260 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001261 if (core->notifier_count && msg)
1262 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001263
Stephen Boydd6968fc2015-04-30 13:54:13 -07001264 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001265 __clk_recalc_rates(child, msg);
1266}
1267
Stephen Boydd6968fc2015-04-30 13:54:13 -07001268static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001269{
1270 unsigned long rate;
1271
1272 clk_prepare_lock();
1273
Stephen Boydd6968fc2015-04-30 13:54:13 -07001274 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1275 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001276
Stephen Boydd6968fc2015-04-30 13:54:13 -07001277 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001278 clk_prepare_unlock();
1279
1280 return rate;
1281}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001282
Mike Turquetteb24764902012-03-15 23:11:19 -07001283/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001284 * clk_get_rate - return the rate of clk
1285 * @clk: the clk whose rate is being returned
1286 *
1287 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1288 * is set, which means a recalc_rate will be issued.
1289 * If clk is NULL then returns 0.
1290 */
1291unsigned long clk_get_rate(struct clk *clk)
1292{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001293 if (!clk)
1294 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001295
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001296 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001297}
1298EXPORT_SYMBOL_GPL(clk_get_rate);
1299
Stephen Boydd6968fc2015-04-30 13:54:13 -07001300static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001301 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001302{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001303 int i;
James Hogan4935b222013-07-29 12:24:59 +01001304
Masahiro Yamada508f8842015-12-28 19:23:08 +09001305 if (!parent)
1306 return -EINVAL;
1307
Masahiro Yamada470b5e22015-12-28 19:23:09 +09001308 for (i = 0; i < core->num_parents; i++)
1309 if (clk_core_get_parent_by_index(core, i) == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001310 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001311
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001312 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001313}
1314
Heiko Stuebnere6500342015-04-22 22:53:05 +02001315/*
1316 * Update the orphan status of @core and all its children.
1317 */
1318static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1319{
1320 struct clk_core *child;
1321
1322 core->orphan = is_orphan;
1323
1324 hlist_for_each_entry(child, &core->children, child_node)
1325 clk_core_update_orphan_status(child, is_orphan);
1326}
1327
Stephen Boydd6968fc2015-04-30 13:54:13 -07001328static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001329{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001330 bool was_orphan = core->orphan;
1331
Stephen Boydd6968fc2015-04-30 13:54:13 -07001332 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001333
James Hogan903efc52013-08-29 12:10:51 +01001334 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001335 bool becomes_orphan = new_parent->orphan;
1336
James Hogan903efc52013-08-29 12:10:51 +01001337 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001338 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001339 new_parent->new_child = NULL;
1340
Stephen Boydd6968fc2015-04-30 13:54:13 -07001341 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001342
1343 if (was_orphan != becomes_orphan)
1344 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001345 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001346 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001347 if (!was_orphan)
1348 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001349 }
James Hogan4935b222013-07-29 12:24:59 +01001350
Stephen Boydd6968fc2015-04-30 13:54:13 -07001351 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001352}
1353
Stephen Boydd6968fc2015-04-30 13:54:13 -07001354static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001355 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001356{
1357 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001358 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001359
1360 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001361 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1362 *
1363 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001364 * clk_enable().
1365 *
1366 * If the clock is not prepared, then a race with
1367 * clk_enable/disable() is impossible since we already have the
1368 * prepare lock (future calls to clk_enable() need to be preceded by
1369 * a clk_prepare()).
1370 *
1371 * If the clock is prepared, migrate the prepared state to the new
1372 * parent and also protect against a race with clk_enable() by
1373 * forcing the clock and the new parent on. This ensures that all
1374 * future calls to clk_enable() are practically NOPs with respect to
1375 * hardware and software states.
1376 *
1377 * See also: Comment for clk_set_parent() below.
1378 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001379
1380 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1381 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1382 clk_core_prepare_enable(old_parent);
1383 clk_core_prepare_enable(parent);
1384 }
1385
1386 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001387 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001388 clk_core_prepare_enable(parent);
1389 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001390 }
1391
1392 /* update the clk tree topology */
1393 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001394 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001395 clk_enable_unlock(flags);
1396
Stephen Boyd3fa22522014-01-15 10:47:22 -08001397 return old_parent;
1398}
1399
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001400static void __clk_set_parent_after(struct clk_core *core,
1401 struct clk_core *parent,
1402 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001403{
1404 /*
1405 * Finish the migration of prepare state and undo the changes done
1406 * for preventing a race with clk_enable().
1407 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001408 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001409 clk_core_disable_lock(core);
1410 clk_core_disable_unprepare(old_parent);
1411 }
1412
1413 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1414 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1415 clk_core_disable_unprepare(parent);
1416 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001417 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001418}
1419
Stephen Boydd6968fc2015-04-30 13:54:13 -07001420static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001421 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001422{
1423 unsigned long flags;
1424 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001425 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001426
Stephen Boydd6968fc2015-04-30 13:54:13 -07001427 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001428
Stephen Boydd6968fc2015-04-30 13:54:13 -07001429 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001430
James Hogan4935b222013-07-29 12:24:59 +01001431 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001432 if (parent && core->ops->set_parent)
1433 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001434
Stephen Boydd6968fc2015-04-30 13:54:13 -07001435 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001436
James Hogan4935b222013-07-29 12:24:59 +01001437 if (ret) {
1438 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001439 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001440 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001441 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001442
James Hogan4935b222013-07-29 12:24:59 +01001443 return ret;
1444 }
1445
Stephen Boydd6968fc2015-04-30 13:54:13 -07001446 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001447
James Hogan4935b222013-07-29 12:24:59 +01001448 return 0;
1449}
1450
Ulf Hanssona093bde2012-08-31 14:21:28 +02001451/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001452 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001453 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001454 * @parent_rate: the "future" rate of clk's parent
1455 *
1456 * Walks the subtree of clks starting with clk, speculating rates as it
1457 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1458 *
1459 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1460 * pre-rate change notifications and returns early if no clks in the
1461 * subtree have subscribed to the notifications. Note that if a clk does not
1462 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001463 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001464 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001465static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001466 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001467{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001468 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001469 unsigned long new_rate;
1470 int ret = NOTIFY_DONE;
1471
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001472 lockdep_assert_held(&prepare_lock);
1473
Stephen Boydd6968fc2015-04-30 13:54:13 -07001474 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001475
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001476 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001477 if (core->notifier_count)
1478 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001479
Mike Turquette86bcfa22014-02-24 16:08:41 -08001480 if (ret & NOTIFY_STOP_MASK) {
1481 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001482 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001483 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001484 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001485
Stephen Boydd6968fc2015-04-30 13:54:13 -07001486 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001487 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001488 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001489 break;
1490 }
1491
1492out:
1493 return ret;
1494}
1495
Stephen Boydd6968fc2015-04-30 13:54:13 -07001496static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001497 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001498{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001499 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001500
Stephen Boydd6968fc2015-04-30 13:54:13 -07001501 core->new_rate = new_rate;
1502 core->new_parent = new_parent;
1503 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001504 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001505 core->new_child = NULL;
1506 if (new_parent && new_parent != core->parent)
1507 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001508
Stephen Boydd6968fc2015-04-30 13:54:13 -07001509 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001510 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001511 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001512 }
1513}
1514
1515/*
1516 * calculate the new rates returning the topmost clock that has to be
1517 * changed.
1518 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001519static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001520 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001521{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001522 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001523 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001524 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001525 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001526 unsigned long min_rate;
1527 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001528 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001529 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001530
Mike Turquette7452b212012-03-26 14:45:36 -07001531 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001532 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001533 return NULL;
1534
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001535 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001536 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001537 if (parent)
1538 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001539
Stephen Boydd6968fc2015-04-30 13:54:13 -07001540 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001541
James Hogan71472c02013-07-29 12:25:00 +01001542 /* find the closest rate and parent clk/rate */
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001543 if (clk_core_can_round(core)) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001544 struct clk_rate_request req;
1545
1546 req.rate = rate;
1547 req.min_rate = min_rate;
1548 req.max_rate = max_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001549
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001550 clk_core_init_rate_req(core, &req);
1551
1552 ret = clk_core_determine_round_nolock(core, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001553 if (ret < 0)
1554 return NULL;
1555
Boris Brezillon0817b622015-07-07 20:48:08 +02001556 best_parent_rate = req.best_parent_rate;
1557 new_rate = req.rate;
1558 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001559
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001560 if (new_rate < min_rate || new_rate > max_rate)
1561 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001562 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001563 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001564 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001565 return NULL;
1566 } else {
1567 /* pass-through clock with adjustable parent */
1568 top = clk_calc_new_rates(parent, rate);
1569 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001570 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001571 }
1572
James Hogan71472c02013-07-29 12:25:00 +01001573 /* some clocks must be gated to change parent */
1574 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001575 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001576 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001577 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001578 return NULL;
1579 }
1580
James Hogan71472c02013-07-29 12:25:00 +01001581 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001582 if (parent && core->num_parents > 1) {
1583 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001584 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001585 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001586 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001587 return NULL;
1588 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001589 }
1590
Stephen Boydd6968fc2015-04-30 13:54:13 -07001591 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001592 best_parent_rate != parent->rate)
1593 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001594
1595out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001596 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001597
1598 return top;
1599}
1600
1601/*
1602 * Notify about rate changes in a subtree. Always walk down the whole tree
1603 * so that in case of an error we can walk down the whole tree again and
1604 * abort the change.
1605 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001606static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001607 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001608{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001609 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001610 int ret = NOTIFY_DONE;
1611
Stephen Boydd6968fc2015-04-30 13:54:13 -07001612 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301613 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001614
Stephen Boydd6968fc2015-04-30 13:54:13 -07001615 if (core->notifier_count) {
1616 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001617 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001618 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001619 }
1620
Stephen Boydd6968fc2015-04-30 13:54:13 -07001621 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001622 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001623 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001624 continue;
1625 tmp_clk = clk_propagate_rate_change(child, event);
1626 if (tmp_clk)
1627 fail_clk = tmp_clk;
1628 }
1629
Stephen Boydd6968fc2015-04-30 13:54:13 -07001630 /* handle the new child who might not be in core->children yet */
1631 if (core->new_child) {
1632 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001633 if (tmp_clk)
1634 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001635 }
1636
1637 return fail_clk;
1638}
1639
1640/*
1641 * walk down a subtree and set the new rates notifying the rate
1642 * change on the way
1643 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001644static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001645{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001646 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001647 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001648 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001649 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001650 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001651 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001652 struct clk_core *parent = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001653
Stephen Boydd6968fc2015-04-30 13:54:13 -07001654 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001655
Dong Aishengfc8726a2016-06-30 17:31:14 +08001656 if (core->new_parent) {
1657 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001658 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001659 } else if (core->parent) {
1660 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001661 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001662 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001663
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001664 if (core->flags & CLK_SET_RATE_UNGATE) {
1665 unsigned long flags;
1666
1667 clk_core_prepare(core);
1668 flags = clk_enable_lock();
1669 clk_core_enable(core);
1670 clk_enable_unlock(flags);
1671 }
1672
Stephen Boydd6968fc2015-04-30 13:54:13 -07001673 if (core->new_parent && core->new_parent != core->parent) {
1674 old_parent = __clk_set_parent_before(core, core->new_parent);
1675 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001676
Stephen Boydd6968fc2015-04-30 13:54:13 -07001677 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001678 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001679 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001680 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001681 core->new_parent_index);
1682 } else if (core->ops->set_parent) {
1683 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001684 }
1685
Stephen Boydd6968fc2015-04-30 13:54:13 -07001686 trace_clk_set_parent_complete(core, core->new_parent);
1687 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001688 }
1689
Dong Aishengfc8726a2016-06-30 17:31:14 +08001690 if (core->flags & CLK_OPS_PARENT_ENABLE)
1691 clk_core_prepare_enable(parent);
1692
Stephen Boydd6968fc2015-04-30 13:54:13 -07001693 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001694
Stephen Boydd6968fc2015-04-30 13:54:13 -07001695 if (!skip_set_rate && core->ops->set_rate)
1696 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001697
Stephen Boydd6968fc2015-04-30 13:54:13 -07001698 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001699
Stephen Boydd6968fc2015-04-30 13:54:13 -07001700 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001701
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001702 if (core->flags & CLK_SET_RATE_UNGATE) {
1703 unsigned long flags;
1704
1705 flags = clk_enable_lock();
1706 clk_core_disable(core);
1707 clk_enable_unlock(flags);
1708 clk_core_unprepare(core);
1709 }
1710
Dong Aishengfc8726a2016-06-30 17:31:14 +08001711 if (core->flags & CLK_OPS_PARENT_ENABLE)
1712 clk_core_disable_unprepare(parent);
1713
Stephen Boydd6968fc2015-04-30 13:54:13 -07001714 if (core->notifier_count && old_rate != core->rate)
1715 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001716
Michael Turquette85e88fa2015-06-20 12:18:03 -07001717 if (core->flags & CLK_RECALC_NEW_RATES)
1718 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001719
Tero Kristo067bb172014-08-21 16:47:45 +03001720 /*
1721 * Use safe iteration, as change_rate can actually swap parents
1722 * for certain clock types.
1723 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001724 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001725 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001726 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001727 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001728 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001729 }
1730
Stephen Boydd6968fc2015-04-30 13:54:13 -07001731 /* handle the new child who might not be in core->children yet */
1732 if (core->new_child)
1733 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001734}
1735
Jerome Brunetca5e0892017-12-01 22:51:55 +01001736static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1737 unsigned long req_rate)
1738{
Jerome Brunete55a8392017-12-01 22:51:56 +01001739 int ret, cnt;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001740 struct clk_rate_request req;
1741
1742 lockdep_assert_held(&prepare_lock);
1743
1744 if (!core)
1745 return 0;
1746
Jerome Brunete55a8392017-12-01 22:51:56 +01001747 /* simulate what the rate would be if it could be freely set */
1748 cnt = clk_core_rate_nuke_protect(core);
1749 if (cnt < 0)
1750 return cnt;
1751
Jerome Brunetca5e0892017-12-01 22:51:55 +01001752 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1753 req.rate = req_rate;
1754
1755 ret = clk_core_round_rate_nolock(core, &req);
1756
Jerome Brunete55a8392017-12-01 22:51:56 +01001757 /* restore the protection */
1758 clk_core_rate_restore_protect(core, cnt);
1759
Jerome Brunetca5e0892017-12-01 22:51:55 +01001760 return ret ? 0 : req.rate;
1761}
1762
Stephen Boydd6968fc2015-04-30 13:54:13 -07001763static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001764 unsigned long req_rate)
1765{
1766 struct clk_core *top, *fail_clk;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001767 unsigned long rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001768 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001769
Stephen Boydd6968fc2015-04-30 13:54:13 -07001770 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001771 return 0;
1772
Jerome Brunetca5e0892017-12-01 22:51:55 +01001773 rate = clk_core_req_round_rate_nolock(core, req_rate);
1774
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001775 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001776 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001777 return 0;
1778
Jerome Brunete55a8392017-12-01 22:51:56 +01001779 /* fail on a direct rate set of a protected provider */
1780 if (clk_core_rate_is_protected(core))
1781 return -EBUSY;
1782
Stephen Boydd6968fc2015-04-30 13:54:13 -07001783 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001784 return -EBUSY;
1785
1786 /* calculate new rates and get the topmost changed clock */
Jerome Brunetca5e0892017-12-01 22:51:55 +01001787 top = clk_calc_new_rates(core, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001788 if (!top)
1789 return -EINVAL;
1790
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001791 ret = clk_pm_runtime_get(core);
1792 if (ret)
1793 return ret;
1794
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001795 /* notify that we are about to change rates */
1796 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1797 if (fail_clk) {
1798 pr_debug("%s: failed to set %s rate\n", __func__,
1799 fail_clk->name);
1800 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001801 ret = -EBUSY;
1802 goto err;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001803 }
1804
1805 /* change the rates */
1806 clk_change_rate(top);
1807
Stephen Boydd6968fc2015-04-30 13:54:13 -07001808 core->req_rate = req_rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001809err:
1810 clk_pm_runtime_put(core);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001811
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001812 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001813}
1814
Mike Turquetteb24764902012-03-15 23:11:19 -07001815/**
1816 * clk_set_rate - specify a new rate for clk
1817 * @clk: the clk whose rate is being changed
1818 * @rate: the new rate for clk
1819 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001820 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001821 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001822 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1823 * propagate up to clk's parent; whether or not this happens depends on the
1824 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1825 * after calling .round_rate then upstream parent propagation is ignored. If
1826 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001827 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001828 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1829 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001830 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001831 * Rate changes are accomplished via tree traversal that also recalculates the
1832 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001833 *
1834 * Returns 0 on success, -EERROR otherwise.
1835 */
1836int clk_set_rate(struct clk *clk, unsigned long rate)
1837{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001838 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001839
Mike Turquette89ac8d72013-08-21 23:58:09 -07001840 if (!clk)
1841 return 0;
1842
Mike Turquetteb24764902012-03-15 23:11:19 -07001843 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001844 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001845
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001846 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001847
Mike Turquetteeab89f62013-03-28 13:59:01 -07001848 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001849
1850 return ret;
1851}
1852EXPORT_SYMBOL_GPL(clk_set_rate);
1853
1854/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001855 * clk_set_rate_range - set a rate range for a clock source
1856 * @clk: clock source
1857 * @min: desired minimum clock rate in Hz, inclusive
1858 * @max: desired maximum clock rate in Hz, inclusive
1859 *
1860 * Returns success (0) or negative errno.
1861 */
1862int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1863{
1864 int ret = 0;
1865
1866 if (!clk)
1867 return 0;
1868
1869 if (min > max) {
1870 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1871 __func__, clk->core->name, clk->dev_id, clk->con_id,
1872 min, max);
1873 return -EINVAL;
1874 }
1875
1876 clk_prepare_lock();
1877
1878 if (min != clk->min_rate || max != clk->max_rate) {
1879 clk->min_rate = min;
1880 clk->max_rate = max;
1881 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1882 }
1883
1884 clk_prepare_unlock();
1885
1886 return ret;
1887}
1888EXPORT_SYMBOL_GPL(clk_set_rate_range);
1889
1890/**
1891 * clk_set_min_rate - set a minimum clock rate for a clock source
1892 * @clk: clock source
1893 * @rate: desired minimum clock rate in Hz, inclusive
1894 *
1895 * Returns success (0) or negative errno.
1896 */
1897int clk_set_min_rate(struct clk *clk, unsigned long rate)
1898{
1899 if (!clk)
1900 return 0;
1901
1902 return clk_set_rate_range(clk, rate, clk->max_rate);
1903}
1904EXPORT_SYMBOL_GPL(clk_set_min_rate);
1905
1906/**
1907 * clk_set_max_rate - set a maximum clock rate for a clock source
1908 * @clk: clock source
1909 * @rate: desired maximum clock rate in Hz, inclusive
1910 *
1911 * Returns success (0) or negative errno.
1912 */
1913int clk_set_max_rate(struct clk *clk, unsigned long rate)
1914{
1915 if (!clk)
1916 return 0;
1917
1918 return clk_set_rate_range(clk, clk->min_rate, rate);
1919}
1920EXPORT_SYMBOL_GPL(clk_set_max_rate);
1921
1922/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001923 * clk_get_parent - return the parent of a clk
1924 * @clk: the clk whose parent gets returned
1925 *
1926 * Simply returns clk->parent. Returns NULL if clk is NULL.
1927 */
1928struct clk *clk_get_parent(struct clk *clk)
1929{
1930 struct clk *parent;
1931
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001932 if (!clk)
1933 return NULL;
1934
Mike Turquetteeab89f62013-03-28 13:59:01 -07001935 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001936 /* TODO: Create a per-user clk and change callers to call clk_put */
1937 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07001938 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001939
1940 return parent;
1941}
1942EXPORT_SYMBOL_GPL(clk_get_parent);
1943
Stephen Boydd6968fc2015-04-30 13:54:13 -07001944static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001945{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09001946 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001947
Masahiro Yamada2430a942016-02-09 20:19:14 +09001948 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09001949 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001950
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09001951 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001952}
1953
Stephen Boydd6968fc2015-04-30 13:54:13 -07001954static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001955 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001956{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001957 clk_reparent(core, new_parent);
1958 __clk_recalc_accuracies(core);
1959 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07001960}
1961
Tomeu Vizoso42c86542015-03-11 11:34:25 +01001962void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
1963{
1964 if (!hw)
1965 return;
1966
1967 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
1968}
1969
Mike Turquetteb24764902012-03-15 23:11:19 -07001970/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001971 * clk_has_parent - check if a clock is a possible parent for another
1972 * @clk: clock source
1973 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07001974 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001975 * This function can be used in drivers that need to check that a clock can be
1976 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001977 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001978 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001979 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001980bool clk_has_parent(struct clk *clk, struct clk *parent)
1981{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001982 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001983 unsigned int i;
1984
1985 /* NULL clocks should be nops, so return success if either is NULL. */
1986 if (!clk || !parent)
1987 return true;
1988
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001989 core = clk->core;
1990 parent_core = parent->core;
1991
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001992 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001993 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001994 return true;
1995
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001996 for (i = 0; i < core->num_parents; i++)
1997 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001998 return true;
1999
2000 return false;
2001}
2002EXPORT_SYMBOL_GPL(clk_has_parent);
2003
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002004static int clk_core_set_parent_nolock(struct clk_core *core,
2005 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002006{
2007 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002008 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002009 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002010
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002011 lockdep_assert_held(&prepare_lock);
2012
Stephen Boydd6968fc2015-04-30 13:54:13 -07002013 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002014 return 0;
2015
Stephen Boydd6968fc2015-04-30 13:54:13 -07002016 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002017 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002018
Stephen Boydb61c43c2015-02-02 14:11:25 -08002019 /* verify ops for for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002020 if (core->num_parents > 1 && !core->ops->set_parent)
2021 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08002022
Ulf Hansson031dcc92013-04-02 23:09:38 +02002023 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002024 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2025 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002026
Jerome Brunete55a8392017-12-01 22:51:56 +01002027 if (clk_core_rate_is_protected(core))
2028 return -EBUSY;
2029
Ulf Hansson031dcc92013-04-02 23:09:38 +02002030 /* try finding the new parent index */
2031 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002032 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002033 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002034 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002035 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002036 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002037 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002038 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002039 }
2040
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002041 ret = clk_pm_runtime_get(core);
2042 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002043 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002044
Mike Turquetteb24764902012-03-15 23:11:19 -07002045 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002046 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002047
2048 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002049 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002050 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07002051
Ulf Hansson031dcc92013-04-02 23:09:38 +02002052 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002053 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002054
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002055 /* propagate rate an accuracy recalculation accordingly */
2056 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002057 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002058 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002059 __clk_recalc_rates(core, POST_RATE_CHANGE);
2060 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002061 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002062
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002063runtime_put:
2064 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002065
2066 return ret;
2067}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002068
2069/**
2070 * clk_set_parent - switch the parent of a mux clk
2071 * @clk: the mux clk whose input we are switching
2072 * @parent: the new input to clk
2073 *
2074 * Re-parent clk to use parent as its new input source. If clk is in
2075 * prepared state, the clk will get enabled for the duration of this call. If
2076 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2077 * that, the reparenting is glitchy in hardware, etc), use the
2078 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2079 *
2080 * After successfully changing clk's parent clk_set_parent will update the
2081 * clk topology, sysfs topology and propagate rate recalculation via
2082 * __clk_recalc_rates.
2083 *
2084 * Returns 0 on success, -EERROR otherwise.
2085 */
2086int clk_set_parent(struct clk *clk, struct clk *parent)
2087{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002088 int ret;
2089
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002090 if (!clk)
2091 return 0;
2092
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002093 clk_prepare_lock();
2094 ret = clk_core_set_parent_nolock(clk->core,
2095 parent ? parent->core : NULL);
2096 clk_prepare_unlock();
2097
2098 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002099}
Mike Turquetteb24764902012-03-15 23:11:19 -07002100EXPORT_SYMBOL_GPL(clk_set_parent);
2101
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002102static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2103{
2104 int ret = -EINVAL;
2105
2106 lockdep_assert_held(&prepare_lock);
2107
2108 if (!core)
2109 return 0;
2110
Jerome Brunete55a8392017-12-01 22:51:56 +01002111 if (clk_core_rate_is_protected(core))
2112 return -EBUSY;
2113
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002114 trace_clk_set_phase(core, degrees);
2115
2116 if (core->ops->set_phase)
2117 ret = core->ops->set_phase(core->hw, degrees);
2118
2119 trace_clk_set_phase_complete(core, degrees);
2120
2121 return ret;
2122}
2123
Mike Turquetteb24764902012-03-15 23:11:19 -07002124/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002125 * clk_set_phase - adjust the phase shift of a clock signal
2126 * @clk: clock signal source
2127 * @degrees: number of degrees the signal is shifted
2128 *
2129 * Shifts the phase of a clock signal by the specified
2130 * degrees. Returns 0 on success, -EERROR otherwise.
2131 *
2132 * This function makes no distinction about the input or reference
2133 * signal that we adjust the clock signal phase against. For example
2134 * phase locked-loop clock signal generators we may shift phase with
2135 * respect to feedback clock signal input, but for other cases the
2136 * clock phase may be shifted with respect to some other, unspecified
2137 * signal.
2138 *
2139 * Additionally the concept of phase shift does not propagate through
2140 * the clock tree hierarchy, which sets it apart from clock rates and
2141 * clock accuracy. A parent clock phase attribute does not have an
2142 * impact on the phase attribute of a child clock.
2143 */
2144int clk_set_phase(struct clk *clk, int degrees)
2145{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002146 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002147
2148 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002149 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002150
2151 /* sanity check degrees */
2152 degrees %= 360;
2153 if (degrees < 0)
2154 degrees += 360;
2155
2156 clk_prepare_lock();
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002157 ret = clk_core_set_phase_nolock(clk->core, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002158 clk_prepare_unlock();
2159
Mike Turquettee59c5372014-02-18 21:21:25 -08002160 return ret;
2161}
Maxime Ripard9767b042015-01-20 22:23:43 +01002162EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002163
Stephen Boydd6968fc2015-04-30 13:54:13 -07002164static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002165{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002166 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002167
2168 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07002169 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002170 clk_prepare_unlock();
2171
Mike Turquettee59c5372014-02-18 21:21:25 -08002172 return ret;
2173}
2174
2175/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002176 * clk_get_phase - return the phase shift of a clock signal
2177 * @clk: clock signal source
2178 *
2179 * Returns the phase shift of a clock node in degrees, otherwise returns
2180 * -EERROR.
2181 */
2182int clk_get_phase(struct clk *clk)
2183{
2184 if (!clk)
2185 return 0;
2186
2187 return clk_core_get_phase(clk->core);
2188}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002189EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002190
2191/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002192 * clk_is_match - check if two clk's point to the same hardware clock
2193 * @p: clk compared against q
2194 * @q: clk compared against p
2195 *
2196 * Returns true if the two struct clk pointers both point to the same hardware
2197 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2198 * share the same struct clk_core object.
2199 *
2200 * Returns false otherwise. Note that two NULL clks are treated as matching.
2201 */
2202bool clk_is_match(const struct clk *p, const struct clk *q)
2203{
2204 /* trivial case: identical struct clk's or both NULL */
2205 if (p == q)
2206 return true;
2207
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002208 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002209 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2210 if (p->core == q->core)
2211 return true;
2212
2213 return false;
2214}
2215EXPORT_SYMBOL_GPL(clk_is_match);
2216
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002217/*** debugfs support ***/
2218
2219#ifdef CONFIG_DEBUG_FS
2220#include <linux/debugfs.h>
2221
2222static struct dentry *rootdir;
2223static int inited = 0;
2224static DEFINE_MUTEX(clk_debug_lock);
2225static HLIST_HEAD(clk_debug_list);
2226
2227static struct hlist_head *all_lists[] = {
2228 &clk_root_list,
2229 &clk_orphan_list,
2230 NULL,
2231};
2232
2233static struct hlist_head *orphan_list[] = {
2234 &clk_orphan_list,
2235 NULL,
2236};
2237
2238static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2239 int level)
2240{
2241 if (!c)
2242 return;
2243
Jerome Brunetc5ce26e2017-12-01 22:51:57 +01002244 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %-3d\n",
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002245 level * 3 + 1, "",
2246 30 - level * 3, c->name,
Jerome Brunete55a8392017-12-01 22:51:56 +01002247 c->enable_count, c->prepare_count, c->protect_count,
2248 clk_core_get_rate(c), clk_core_get_accuracy(c),
2249 clk_core_get_phase(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002250}
2251
2252static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2253 int level)
2254{
2255 struct clk_core *child;
2256
2257 if (!c)
2258 return;
2259
2260 clk_summary_show_one(s, c, level);
2261
2262 hlist_for_each_entry(child, &c->children, child_node)
2263 clk_summary_show_subtree(s, child, level + 1);
2264}
2265
2266static int clk_summary_show(struct seq_file *s, void *data)
2267{
2268 struct clk_core *c;
2269 struct hlist_head **lists = (struct hlist_head **)s->private;
2270
Jerome Brunetc5ce26e2017-12-01 22:51:57 +01002271 seq_puts(s, " enable prepare protect \n");
2272 seq_puts(s, " clock count count count rate accuracy phase\n");
2273 seq_puts(s, "----------------------------------------------------------------------------------------\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002274
2275 clk_prepare_lock();
2276
2277 for (; *lists; lists++)
2278 hlist_for_each_entry(c, *lists, child_node)
2279 clk_summary_show_subtree(s, c, 0);
2280
2281 clk_prepare_unlock();
2282
2283 return 0;
2284}
2285
2286
2287static int clk_summary_open(struct inode *inode, struct file *file)
2288{
2289 return single_open(file, clk_summary_show, inode->i_private);
2290}
2291
2292static const struct file_operations clk_summary_fops = {
2293 .open = clk_summary_open,
2294 .read = seq_read,
2295 .llseek = seq_lseek,
2296 .release = single_release,
2297};
2298
2299static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2300{
2301 if (!c)
2302 return;
2303
Stefan Wahren7cb81132015-04-29 16:36:43 +00002304 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002305 seq_printf(s, "\"%s\": { ", c->name);
2306 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2307 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002308 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002309 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2310 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002311 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2312}
2313
2314static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2315{
2316 struct clk_core *child;
2317
2318 if (!c)
2319 return;
2320
2321 clk_dump_one(s, c, level);
2322
2323 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002324 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002325 clk_dump_subtree(s, child, level + 1);
2326 }
2327
Markus Elfring4d327582017-04-20 08:45:43 +02002328 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002329}
2330
2331static int clk_dump(struct seq_file *s, void *data)
2332{
2333 struct clk_core *c;
2334 bool first_node = true;
2335 struct hlist_head **lists = (struct hlist_head **)s->private;
2336
Markus Elfring4d327582017-04-20 08:45:43 +02002337 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002338 clk_prepare_lock();
2339
2340 for (; *lists; lists++) {
2341 hlist_for_each_entry(c, *lists, child_node) {
2342 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02002343 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002344 first_node = false;
2345 clk_dump_subtree(s, c, 0);
2346 }
2347 }
2348
2349 clk_prepare_unlock();
2350
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002351 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002352 return 0;
2353}
2354
2355
2356static int clk_dump_open(struct inode *inode, struct file *file)
2357{
2358 return single_open(file, clk_dump, inode->i_private);
2359}
2360
2361static const struct file_operations clk_dump_fops = {
2362 .open = clk_dump_open,
2363 .read = seq_read,
2364 .llseek = seq_lseek,
2365 .release = single_release,
2366};
2367
Peter De Schrijver92031572017-03-21 15:20:31 +02002368static int possible_parents_dump(struct seq_file *s, void *data)
2369{
2370 struct clk_core *core = s->private;
2371 int i;
2372
2373 for (i = 0; i < core->num_parents - 1; i++)
2374 seq_printf(s, "%s ", core->parent_names[i]);
2375
2376 seq_printf(s, "%s\n", core->parent_names[i]);
2377
2378 return 0;
2379}
2380
2381static int possible_parents_open(struct inode *inode, struct file *file)
2382{
2383 return single_open(file, possible_parents_dump, inode->i_private);
2384}
2385
2386static const struct file_operations possible_parents_fops = {
2387 .open = possible_parents_open,
2388 .read = seq_read,
2389 .llseek = seq_lseek,
2390 .release = single_release,
2391};
2392
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002393static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2394{
2395 struct dentry *d;
2396 int ret = -ENOMEM;
2397
2398 if (!core || !pdentry) {
2399 ret = -EINVAL;
2400 goto out;
2401 }
2402
2403 d = debugfs_create_dir(core->name, pdentry);
2404 if (!d)
2405 goto out;
2406
2407 core->dentry = d;
2408
2409 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2410 (u32 *)&core->rate);
2411 if (!d)
2412 goto err_out;
2413
2414 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2415 (u32 *)&core->accuracy);
2416 if (!d)
2417 goto err_out;
2418
2419 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2420 (u32 *)&core->phase);
2421 if (!d)
2422 goto err_out;
2423
2424 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2425 (u32 *)&core->flags);
2426 if (!d)
2427 goto err_out;
2428
2429 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2430 (u32 *)&core->prepare_count);
2431 if (!d)
2432 goto err_out;
2433
2434 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2435 (u32 *)&core->enable_count);
2436 if (!d)
2437 goto err_out;
2438
Jerome Brunete55a8392017-12-01 22:51:56 +01002439 d = debugfs_create_u32("clk_protect_count", S_IRUGO, core->dentry,
2440 (u32 *)&core->protect_count);
2441 if (!d)
2442 goto err_out;
2443
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002444 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2445 (u32 *)&core->notifier_count);
2446 if (!d)
2447 goto err_out;
2448
Peter De Schrijver92031572017-03-21 15:20:31 +02002449 if (core->num_parents > 1) {
2450 d = debugfs_create_file("clk_possible_parents", S_IRUGO,
2451 core->dentry, core, &possible_parents_fops);
2452 if (!d)
2453 goto err_out;
2454 }
2455
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002456 if (core->ops->debug_init) {
2457 ret = core->ops->debug_init(core->hw, core->dentry);
2458 if (ret)
2459 goto err_out;
2460 }
2461
2462 ret = 0;
2463 goto out;
2464
2465err_out:
2466 debugfs_remove_recursive(core->dentry);
2467 core->dentry = NULL;
2468out:
2469 return ret;
2470}
2471
2472/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002473 * clk_debug_register - add a clk node to the debugfs clk directory
2474 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002475 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002476 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2477 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002478 * will be created lazily by clk_debug_init as part of a late_initcall.
2479 */
2480static int clk_debug_register(struct clk_core *core)
2481{
2482 int ret = 0;
2483
2484 mutex_lock(&clk_debug_lock);
2485 hlist_add_head(&core->debug_node, &clk_debug_list);
2486
2487 if (!inited)
2488 goto unlock;
2489
2490 ret = clk_debug_create_one(core, rootdir);
2491unlock:
2492 mutex_unlock(&clk_debug_lock);
2493
2494 return ret;
2495}
2496
2497 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002498 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2499 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002500 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002501 * Dynamically removes a clk and all its child nodes from the
2502 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08002503 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002504 */
2505static void clk_debug_unregister(struct clk_core *core)
2506{
2507 mutex_lock(&clk_debug_lock);
2508 hlist_del_init(&core->debug_node);
2509 debugfs_remove_recursive(core->dentry);
2510 core->dentry = NULL;
2511 mutex_unlock(&clk_debug_lock);
2512}
2513
2514struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2515 void *data, const struct file_operations *fops)
2516{
2517 struct dentry *d = NULL;
2518
2519 if (hw->core->dentry)
2520 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2521 fops);
2522
2523 return d;
2524}
2525EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2526
2527/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002528 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002529 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002530 * clks are often initialized very early during boot before memory can be
2531 * dynamically allocated and well before debugfs is setup. This function
2532 * populates the debugfs clk directory once at boot-time when we know that
2533 * debugfs is setup. It should only be called once at boot-time, all other clks
2534 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002535 */
2536static int __init clk_debug_init(void)
2537{
2538 struct clk_core *core;
2539 struct dentry *d;
2540
2541 rootdir = debugfs_create_dir("clk", NULL);
2542
2543 if (!rootdir)
2544 return -ENOMEM;
2545
2546 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2547 &clk_summary_fops);
2548 if (!d)
2549 return -ENOMEM;
2550
2551 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2552 &clk_dump_fops);
2553 if (!d)
2554 return -ENOMEM;
2555
2556 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2557 &orphan_list, &clk_summary_fops);
2558 if (!d)
2559 return -ENOMEM;
2560
2561 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2562 &orphan_list, &clk_dump_fops);
2563 if (!d)
2564 return -ENOMEM;
2565
2566 mutex_lock(&clk_debug_lock);
2567 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2568 clk_debug_create_one(core, rootdir);
2569
2570 inited = 1;
2571 mutex_unlock(&clk_debug_lock);
2572
2573 return 0;
2574}
2575late_initcall(clk_debug_init);
2576#else
2577static inline int clk_debug_register(struct clk_core *core) { return 0; }
2578static inline void clk_debug_reparent(struct clk_core *core,
2579 struct clk_core *new_parent)
2580{
2581}
2582static inline void clk_debug_unregister(struct clk_core *core)
2583{
2584}
2585#endif
2586
Michael Turquette3d3801e2015-02-25 09:11:01 -08002587/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002588 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002589 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07002590 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002591 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002592 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002593 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002594static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002595{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002596 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002597 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002598 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002599 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002600
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002601 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07002602 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002603
Mike Turquetteeab89f62013-03-28 13:59:01 -07002604 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002605
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002606 ret = clk_pm_runtime_get(core);
2607 if (ret)
2608 goto unlock;
2609
Mike Turquetteb24764902012-03-15 23:11:19 -07002610 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002611 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002612 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002613 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002614 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002615 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002616 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002617
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002618 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002619 if (core->ops->set_rate &&
2620 !((core->ops->round_rate || core->ops->determine_rate) &&
2621 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002622 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2623 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002624 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002625 goto out;
2626 }
2627
Stephen Boydd6968fc2015-04-30 13:54:13 -07002628 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002629 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2630 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002631 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002632 goto out;
2633 }
2634
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09002635 if (core->num_parents > 1 && !core->ops->get_parent) {
2636 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2637 __func__, core->name);
2638 ret = -EINVAL;
2639 goto out;
2640 }
2641
Stephen Boydd6968fc2015-04-30 13:54:13 -07002642 if (core->ops->set_rate_and_parent &&
2643 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002644 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002645 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002646 ret = -EINVAL;
2647 goto out;
2648 }
2649
Mike Turquetteb24764902012-03-15 23:11:19 -07002650 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002651 for (i = 0; i < core->num_parents; i++)
2652 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002653 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002654 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002655
Stephen Boydd6968fc2015-04-30 13:54:13 -07002656 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002657
2658 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08002659 * Populate core->parent if parent has already been clk_core_init'd. If
2660 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002661 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07002662 * clk list.
2663 *
2664 * Every time a new clk is clk_init'd then we walk the list of orphan
2665 * clocks and re-parent any that are children of the clock currently
2666 * being clk_init'd.
2667 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02002668 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002669 hlist_add_head(&core->child_node,
2670 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002671 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002672 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002673 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002674 core->orphan = false;
2675 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002676 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002677 core->orphan = true;
2678 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002679
2680 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002681 * Set clk's accuracy. The preferred method is to use
2682 * .recalc_accuracy. For simple clocks and lazy developers the default
2683 * fallback is to use the parent's accuracy. If a clock doesn't have a
2684 * parent (or is orphaned) then accuracy is set to zero (perfect
2685 * clock).
2686 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002687 if (core->ops->recalc_accuracy)
2688 core->accuracy = core->ops->recalc_accuracy(core->hw,
2689 __clk_get_accuracy(core->parent));
2690 else if (core->parent)
2691 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002692 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002693 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002694
2695 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002696 * Set clk's phase.
2697 * Since a phase is by definition relative to its parent, just
2698 * query the current clock phase, or just assume it's in phase.
2699 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002700 if (core->ops->get_phase)
2701 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002702 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002703 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002704
2705 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002706 * Set clk's rate. The preferred method is to use .recalc_rate. For
2707 * simple clocks and lazy developers the default fallback is to use the
2708 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2709 * then rate is set to zero.
2710 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002711 if (core->ops->recalc_rate)
2712 rate = core->ops->recalc_rate(core->hw,
2713 clk_core_get_rate_nolock(core->parent));
2714 else if (core->parent)
2715 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002716 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002717 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002718 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002719
2720 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002721 * walk the list of orphan clocks and reparent any that newly finds a
2722 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07002723 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002724 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002725 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002726
Michael Turquette904e6ea2016-07-08 16:32:10 -07002727 /*
2728 * we could call __clk_set_parent, but that would result in a
2729 * redundant call to the .set_rate op, if it exists
2730 */
2731 if (parent) {
2732 __clk_set_parent_before(orphan, parent);
2733 __clk_set_parent_after(orphan, parent, NULL);
2734 __clk_recalc_accuracies(orphan);
2735 __clk_recalc_rates(orphan, 0);
2736 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002737 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002738
2739 /*
2740 * optional platform-specific magic
2741 *
2742 * The .init callback is not used by any of the basic clock types, but
2743 * exists for weird hardware that must perform initialization magic.
2744 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002745 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002746 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002747 if (core->ops->init)
2748 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002749
Lee Jones32b9b102016-02-11 13:19:09 -08002750 if (core->flags & CLK_IS_CRITICAL) {
Maxime Ripardef56b792016-05-13 10:00:31 +02002751 unsigned long flags;
2752
Lee Jones32b9b102016-02-11 13:19:09 -08002753 clk_core_prepare(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02002754
2755 flags = clk_enable_lock();
Lee Jones32b9b102016-02-11 13:19:09 -08002756 clk_core_enable(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02002757 clk_enable_unlock(flags);
Lee Jones32b9b102016-02-11 13:19:09 -08002758 }
2759
Stephen Boydd6968fc2015-04-30 13:54:13 -07002760 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002761out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002762 clk_pm_runtime_put(core);
2763unlock:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002764 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002765
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002766 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002767 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002768
Mike Turquetted1302a32012-03-29 14:30:40 -07002769 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002770}
2771
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002772struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2773 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002774{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002775 struct clk *clk;
2776
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002777 /* This is to allow this function to be chained to others */
Masahiro Yamadac1de1352015-11-20 14:38:49 +09002778 if (IS_ERR_OR_NULL(hw))
Masahiro Yamada8a231332016-07-19 16:28:47 +09002779 return ERR_CAST(hw);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002780
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002781 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2782 if (!clk)
2783 return ERR_PTR(-ENOMEM);
2784
2785 clk->core = hw->core;
2786 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02002787 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002788 clk->max_rate = ULONG_MAX;
2789
2790 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002791 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002792 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002793
2794 return clk;
2795}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002796
Stephen Boyd73e0e492015-02-06 11:42:43 -08002797void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002798{
2799 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002800 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002801 clk_prepare_unlock();
2802
Leonard Crestez253160a2017-02-20 15:20:56 +02002803 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002804 kfree(clk);
2805}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002806
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002807/**
2808 * clk_register - allocate a new clock, register it and return an opaque cookie
2809 * @dev: device that is registering this clock
2810 * @hw: link to hardware-specific clock data
2811 *
2812 * clk_register is the primary interface for populating the clock tree with new
2813 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05302814 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002815 * rest of the clock API. In the event of an error clk_register will return an
2816 * error code; drivers must test for an error code after calling clk_register.
2817 */
2818struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002819{
Mike Turquetted1302a32012-03-29 14:30:40 -07002820 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002821 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002822
Stephen Boydd6968fc2015-04-30 13:54:13 -07002823 core = kzalloc(sizeof(*core), GFP_KERNEL);
2824 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002825 ret = -ENOMEM;
2826 goto fail_out;
2827 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002828
Stephen Boydd6968fc2015-04-30 13:54:13 -07002829 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2830 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002831 ret = -ENOMEM;
2832 goto fail_name;
2833 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002834 core->ops = hw->init->ops;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002835 if (dev && pm_runtime_enabled(dev))
2836 core->dev = dev;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002837 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002838 core->owner = dev->driver->owner;
2839 core->hw = hw;
2840 core->flags = hw->init->flags;
2841 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07002842 core->min_rate = 0;
2843 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002844 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002845
Mike Turquetted1302a32012-03-29 14:30:40 -07002846 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002847 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002848 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002849
Stephen Boydd6968fc2015-04-30 13:54:13 -07002850 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002851 ret = -ENOMEM;
2852 goto fail_parent_names;
2853 }
2854
2855
2856 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002857 for (i = 0; i < core->num_parents; i++) {
2858 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002859 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002860 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002861 ret = -ENOMEM;
2862 goto fail_parent_names_copy;
2863 }
2864 }
2865
Masahiro Yamada176d1162015-12-28 19:23:00 +09002866 /* avoid unnecessary string look-ups of clk_core's possible parents. */
2867 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
2868 GFP_KERNEL);
2869 if (!core->parents) {
2870 ret = -ENOMEM;
2871 goto fail_parents;
2872 };
2873
Stephen Boydd6968fc2015-04-30 13:54:13 -07002874 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002875
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002876 hw->clk = __clk_create_clk(hw, NULL, NULL);
2877 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002878 ret = PTR_ERR(hw->clk);
Masahiro Yamada176d1162015-12-28 19:23:00 +09002879 goto fail_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002880 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002881
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002882 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002883 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002884 return hw->clk;
2885
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002886 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002887 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002888
Masahiro Yamada176d1162015-12-28 19:23:00 +09002889fail_parents:
2890 kfree(core->parents);
Mike Turquetted1302a32012-03-29 14:30:40 -07002891fail_parent_names_copy:
2892 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002893 kfree_const(core->parent_names[i]);
2894 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07002895fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002896 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002897fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002898 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002899fail_out:
2900 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002901}
2902EXPORT_SYMBOL_GPL(clk_register);
2903
Stephen Boyd41438042016-02-05 17:02:52 -08002904/**
2905 * clk_hw_register - register a clk_hw and return an error code
2906 * @dev: device that is registering this clock
2907 * @hw: link to hardware-specific clock data
2908 *
2909 * clk_hw_register is the primary interface for populating the clock tree with
2910 * new clock nodes. It returns an integer equal to zero indicating success or
2911 * less than zero indicating failure. Drivers must test for an error code after
2912 * calling clk_hw_register().
2913 */
2914int clk_hw_register(struct device *dev, struct clk_hw *hw)
2915{
2916 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
2917}
2918EXPORT_SYMBOL_GPL(clk_hw_register);
2919
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002920/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002921static void __clk_release(struct kref *ref)
2922{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002923 struct clk_core *core = container_of(ref, struct clk_core, ref);
2924 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002925
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002926 lockdep_assert_held(&prepare_lock);
2927
Stephen Boydd6968fc2015-04-30 13:54:13 -07002928 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002929 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002930 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002931
Stephen Boydd6968fc2015-04-30 13:54:13 -07002932 kfree(core->parent_names);
2933 kfree_const(core->name);
2934 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002935}
2936
2937/*
2938 * Empty clk_ops for unregistered clocks. These are used temporarily
2939 * after clk_unregister() was called on a clock and until last clock
2940 * consumer calls clk_put() and the struct clk object is freed.
2941 */
2942static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2943{
2944 return -ENXIO;
2945}
2946
2947static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2948{
2949 WARN_ON_ONCE(1);
2950}
2951
2952static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2953 unsigned long parent_rate)
2954{
2955 return -ENXIO;
2956}
2957
2958static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2959{
2960 return -ENXIO;
2961}
2962
2963static const struct clk_ops clk_nodrv_ops = {
2964 .enable = clk_nodrv_prepare_enable,
2965 .disable = clk_nodrv_disable_unprepare,
2966 .prepare = clk_nodrv_prepare_enable,
2967 .unprepare = clk_nodrv_disable_unprepare,
2968 .set_rate = clk_nodrv_set_rate,
2969 .set_parent = clk_nodrv_set_parent,
2970};
2971
Mark Brown1df5c932012-04-18 09:07:12 +01002972/**
2973 * clk_unregister - unregister a currently registered clock
2974 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002975 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002976void clk_unregister(struct clk *clk)
2977{
2978 unsigned long flags;
2979
Stephen Boyd6314b672014-09-04 23:37:49 -07002980 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2981 return;
2982
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002983 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002984
2985 clk_prepare_lock();
2986
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002987 if (clk->core->ops == &clk_nodrv_ops) {
2988 pr_err("%s: unregistered clock: %s\n", __func__,
2989 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05002990 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002991 }
2992 /*
2993 * Assign empty clock ops for consumers that might still hold
2994 * a reference to this clock.
2995 */
2996 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002997 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002998 clk_enable_unlock(flags);
2999
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003000 if (!hlist_empty(&clk->core->children)) {
3001 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003002 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003003
3004 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003005 hlist_for_each_entry_safe(child, t, &clk->core->children,
3006 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01003007 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003008 }
3009
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003010 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003011
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003012 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003013 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003014 __func__, clk->core->name);
Jerome Brunete55a8392017-12-01 22:51:56 +01003015
3016 if (clk->core->protect_count)
3017 pr_warn("%s: unregistering protected clock: %s\n",
3018 __func__, clk->core->name);
3019
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003020 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003021unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003022 clk_prepare_unlock();
3023}
Mark Brown1df5c932012-04-18 09:07:12 +01003024EXPORT_SYMBOL_GPL(clk_unregister);
3025
Stephen Boyd41438042016-02-05 17:02:52 -08003026/**
3027 * clk_hw_unregister - unregister a currently registered clk_hw
3028 * @hw: hardware-specific clock data to unregister
3029 */
3030void clk_hw_unregister(struct clk_hw *hw)
3031{
3032 clk_unregister(hw->clk);
3033}
3034EXPORT_SYMBOL_GPL(clk_hw_unregister);
3035
Stephen Boyd46c87732012-09-24 13:38:04 -07003036static void devm_clk_release(struct device *dev, void *res)
3037{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003038 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003039}
3040
Stephen Boyd41438042016-02-05 17:02:52 -08003041static void devm_clk_hw_release(struct device *dev, void *res)
3042{
3043 clk_hw_unregister(*(struct clk_hw **)res);
3044}
3045
Stephen Boyd46c87732012-09-24 13:38:04 -07003046/**
3047 * devm_clk_register - resource managed clk_register()
3048 * @dev: device that is registering this clock
3049 * @hw: link to hardware-specific clock data
3050 *
3051 * Managed clk_register(). Clocks returned from this function are
3052 * automatically clk_unregister()ed on driver detach. See clk_register() for
3053 * more information.
3054 */
3055struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3056{
3057 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003058 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003059
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003060 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3061 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003062 return ERR_PTR(-ENOMEM);
3063
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003064 clk = clk_register(dev, hw);
3065 if (!IS_ERR(clk)) {
3066 *clkp = clk;
3067 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003068 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003069 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003070 }
3071
3072 return clk;
3073}
3074EXPORT_SYMBOL_GPL(devm_clk_register);
3075
Stephen Boyd41438042016-02-05 17:02:52 -08003076/**
3077 * devm_clk_hw_register - resource managed clk_hw_register()
3078 * @dev: device that is registering this clock
3079 * @hw: link to hardware-specific clock data
3080 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003081 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003082 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3083 * for more information.
3084 */
3085int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3086{
3087 struct clk_hw **hwp;
3088 int ret;
3089
3090 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3091 if (!hwp)
3092 return -ENOMEM;
3093
3094 ret = clk_hw_register(dev, hw);
3095 if (!ret) {
3096 *hwp = hw;
3097 devres_add(dev, hwp);
3098 } else {
3099 devres_free(hwp);
3100 }
3101
3102 return ret;
3103}
3104EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3105
Stephen Boyd46c87732012-09-24 13:38:04 -07003106static int devm_clk_match(struct device *dev, void *res, void *data)
3107{
3108 struct clk *c = res;
3109 if (WARN_ON(!c))
3110 return 0;
3111 return c == data;
3112}
3113
Stephen Boyd41438042016-02-05 17:02:52 -08003114static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3115{
3116 struct clk_hw *hw = res;
3117
3118 if (WARN_ON(!hw))
3119 return 0;
3120 return hw == data;
3121}
3122
Stephen Boyd46c87732012-09-24 13:38:04 -07003123/**
3124 * devm_clk_unregister - resource managed clk_unregister()
3125 * @clk: clock to unregister
3126 *
3127 * Deallocate a clock allocated with devm_clk_register(). Normally
3128 * this function will not need to be called and the resource management
3129 * code will ensure that the resource is freed.
3130 */
3131void devm_clk_unregister(struct device *dev, struct clk *clk)
3132{
3133 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3134}
3135EXPORT_SYMBOL_GPL(devm_clk_unregister);
3136
Stephen Boyd41438042016-02-05 17:02:52 -08003137/**
3138 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3139 * @dev: device that is unregistering the hardware-specific clock data
3140 * @hw: link to hardware-specific clock data
3141 *
3142 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3143 * this function will not need to be called and the resource management
3144 * code will ensure that the resource is freed.
3145 */
3146void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3147{
3148 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3149 hw));
3150}
3151EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3152
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003153/*
3154 * clkdev helpers
3155 */
3156int __clk_get(struct clk *clk)
3157{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003158 struct clk_core *core = !clk ? NULL : clk->core;
3159
3160 if (core) {
3161 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003162 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003163
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003164 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003165 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003166 return 1;
3167}
3168
3169void __clk_put(struct clk *clk)
3170{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003171 struct module *owner;
3172
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003173 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003174 return;
3175
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003176 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003177
Stephen Boyd50595f82015-02-06 11:42:44 -08003178 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003179 if (clk->min_rate > clk->core->req_rate ||
3180 clk->max_rate < clk->core->req_rate)
3181 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3182
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003183 owner = clk->core->owner;
3184 kref_put(&clk->core->ref, __clk_release);
3185
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003186 clk_prepare_unlock();
3187
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003188 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003189
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003190 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003191}
3192
Mike Turquetteb24764902012-03-15 23:11:19 -07003193/*** clk rate change notifiers ***/
3194
3195/**
3196 * clk_notifier_register - add a clk rate change notifier
3197 * @clk: struct clk * to watch
3198 * @nb: struct notifier_block * with callback info
3199 *
3200 * Request notification when clk's rate changes. This uses an SRCU
3201 * notifier because we want it to block and notifier unregistrations are
3202 * uncommon. The callbacks associated with the notifier must not
3203 * re-enter into the clk framework by calling any top-level clk APIs;
3204 * this will cause a nested prepare_lock mutex.
3205 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003206 * In all notification cases (pre, post and abort rate change) the original
3207 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3208 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003209 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003210 * clk_notifier_register() must be called from non-atomic context.
3211 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3212 * allocation failure; otherwise, passes along the return value of
3213 * srcu_notifier_chain_register().
3214 */
3215int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3216{
3217 struct clk_notifier *cn;
3218 int ret = -ENOMEM;
3219
3220 if (!clk || !nb)
3221 return -EINVAL;
3222
Mike Turquetteeab89f62013-03-28 13:59:01 -07003223 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003224
3225 /* search the list of notifiers for this clk */
3226 list_for_each_entry(cn, &clk_notifier_list, node)
3227 if (cn->clk == clk)
3228 break;
3229
3230 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3231 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02003232 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003233 if (!cn)
3234 goto out;
3235
3236 cn->clk = clk;
3237 srcu_init_notifier_head(&cn->notifier_head);
3238
3239 list_add(&cn->node, &clk_notifier_list);
3240 }
3241
3242 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3243
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003244 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003245
3246out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003247 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003248
3249 return ret;
3250}
3251EXPORT_SYMBOL_GPL(clk_notifier_register);
3252
3253/**
3254 * clk_notifier_unregister - remove a clk rate change notifier
3255 * @clk: struct clk *
3256 * @nb: struct notifier_block * with callback info
3257 *
3258 * Request no further notification for changes to 'clk' and frees memory
3259 * allocated in clk_notifier_register.
3260 *
3261 * Returns -EINVAL if called with null arguments; otherwise, passes
3262 * along the return value of srcu_notifier_chain_unregister().
3263 */
3264int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3265{
3266 struct clk_notifier *cn = NULL;
3267 int ret = -EINVAL;
3268
3269 if (!clk || !nb)
3270 return -EINVAL;
3271
Mike Turquetteeab89f62013-03-28 13:59:01 -07003272 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003273
3274 list_for_each_entry(cn, &clk_notifier_list, node)
3275 if (cn->clk == clk)
3276 break;
3277
3278 if (cn->clk == clk) {
3279 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3280
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003281 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07003282
3283 /* XXX the notifier code should handle this better */
3284 if (!cn->notifier_head.head) {
3285 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08003286 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07003287 kfree(cn);
3288 }
3289
3290 } else {
3291 ret = -ENOENT;
3292 }
3293
Mike Turquetteeab89f62013-03-28 13:59:01 -07003294 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003295
3296 return ret;
3297}
3298EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05003299
3300#ifdef CONFIG_OF
3301/**
3302 * struct of_clk_provider - Clock provider registration structure
3303 * @link: Entry in global list of clock providers
3304 * @node: Pointer to device tree node of clock provider
3305 * @get: Get clock callback. Returns NULL or a struct clk for the
3306 * given clock specifier
3307 * @data: context pointer to be passed into @get callback
3308 */
3309struct of_clk_provider {
3310 struct list_head link;
3311
3312 struct device_node *node;
3313 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003314 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05003315 void *data;
3316};
3317
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303318static const struct of_device_id __clk_of_table_sentinel
3319 __used __section(__clk_of_table_end);
3320
Grant Likely766e6a42012-04-09 14:50:06 -05003321static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003322static DEFINE_MUTEX(of_clk_mutex);
3323
Grant Likely766e6a42012-04-09 14:50:06 -05003324struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3325 void *data)
3326{
3327 return data;
3328}
3329EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3330
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003331struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3332{
3333 return data;
3334}
3335EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3336
Shawn Guo494bfec2012-08-22 21:36:27 +08003337struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3338{
3339 struct clk_onecell_data *clk_data = data;
3340 unsigned int idx = clkspec->args[0];
3341
3342 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02003343 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08003344 return ERR_PTR(-EINVAL);
3345 }
3346
3347 return clk_data->clks[idx];
3348}
3349EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3350
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003351struct clk_hw *
3352of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3353{
3354 struct clk_hw_onecell_data *hw_data = data;
3355 unsigned int idx = clkspec->args[0];
3356
3357 if (idx >= hw_data->num) {
3358 pr_err("%s: invalid index %u\n", __func__, idx);
3359 return ERR_PTR(-EINVAL);
3360 }
3361
3362 return hw_data->hws[idx];
3363}
3364EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3365
Grant Likely766e6a42012-04-09 14:50:06 -05003366/**
3367 * of_clk_add_provider() - Register a clock provider for a node
3368 * @np: Device node pointer associated with clock provider
3369 * @clk_src_get: callback for decoding clock
3370 * @data: context pointer for @clk_src_get callback.
3371 */
3372int of_clk_add_provider(struct device_node *np,
3373 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3374 void *data),
3375 void *data)
3376{
3377 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003378 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003379
Markus Elfring1808a322017-04-20 09:30:52 +02003380 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05003381 if (!cp)
3382 return -ENOMEM;
3383
3384 cp->node = of_node_get(np);
3385 cp->data = data;
3386 cp->get = clk_src_get;
3387
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003388 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003389 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003390 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003391 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05003392
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003393 ret = of_clk_set_defaults(np, true);
3394 if (ret < 0)
3395 of_clk_del_provider(np);
3396
3397 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003398}
3399EXPORT_SYMBOL_GPL(of_clk_add_provider);
3400
3401/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003402 * of_clk_add_hw_provider() - Register a clock provider for a node
3403 * @np: Device node pointer associated with clock provider
3404 * @get: callback for decoding clk_hw
3405 * @data: context pointer for @get callback.
3406 */
3407int of_clk_add_hw_provider(struct device_node *np,
3408 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3409 void *data),
3410 void *data)
3411{
3412 struct of_clk_provider *cp;
3413 int ret;
3414
3415 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3416 if (!cp)
3417 return -ENOMEM;
3418
3419 cp->node = of_node_get(np);
3420 cp->data = data;
3421 cp->get_hw = get;
3422
3423 mutex_lock(&of_clk_mutex);
3424 list_add(&cp->link, &of_clk_providers);
3425 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003426 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003427
3428 ret = of_clk_set_defaults(np, true);
3429 if (ret < 0)
3430 of_clk_del_provider(np);
3431
3432 return ret;
3433}
3434EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3435
Stephen Boydaa795c42017-09-01 16:16:40 -07003436static void devm_of_clk_release_provider(struct device *dev, void *res)
3437{
3438 of_clk_del_provider(*(struct device_node **)res);
3439}
3440
3441int devm_of_clk_add_hw_provider(struct device *dev,
3442 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3443 void *data),
3444 void *data)
3445{
3446 struct device_node **ptr, *np;
3447 int ret;
3448
3449 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3450 GFP_KERNEL);
3451 if (!ptr)
3452 return -ENOMEM;
3453
3454 np = dev->of_node;
3455 ret = of_clk_add_hw_provider(np, get, data);
3456 if (!ret) {
3457 *ptr = np;
3458 devres_add(dev, ptr);
3459 } else {
3460 devres_free(ptr);
3461 }
3462
3463 return ret;
3464}
3465EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3466
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003467/**
Grant Likely766e6a42012-04-09 14:50:06 -05003468 * of_clk_del_provider() - Remove a previously registered clock provider
3469 * @np: Device node pointer associated with clock provider
3470 */
3471void of_clk_del_provider(struct device_node *np)
3472{
3473 struct of_clk_provider *cp;
3474
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003475 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003476 list_for_each_entry(cp, &of_clk_providers, link) {
3477 if (cp->node == np) {
3478 list_del(&cp->link);
3479 of_node_put(cp->node);
3480 kfree(cp);
3481 break;
3482 }
3483 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003484 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003485}
3486EXPORT_SYMBOL_GPL(of_clk_del_provider);
3487
Stephen Boydaa795c42017-09-01 16:16:40 -07003488static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3489{
3490 struct device_node **np = res;
3491
3492 if (WARN_ON(!np || !*np))
3493 return 0;
3494
3495 return *np == data;
3496}
3497
3498void devm_of_clk_del_provider(struct device *dev)
3499{
3500 int ret;
3501
3502 ret = devres_release(dev, devm_of_clk_release_provider,
3503 devm_clk_provider_match, dev->of_node);
3504
3505 WARN_ON(ret);
3506}
3507EXPORT_SYMBOL(devm_of_clk_del_provider);
3508
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003509static struct clk_hw *
3510__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3511 struct of_phandle_args *clkspec)
3512{
3513 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003514
Stephen Boyd74002fc2016-08-25 13:35:36 -07003515 if (provider->get_hw)
3516 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003517
Stephen Boyd74002fc2016-08-25 13:35:36 -07003518 clk = provider->get(clkspec, provider->data);
3519 if (IS_ERR(clk))
3520 return ERR_CAST(clk);
3521 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003522}
3523
Stephen Boyd73e0e492015-02-06 11:42:43 -08003524struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3525 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05003526{
3527 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01003528 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Stephen Boydf155d152016-08-15 14:32:23 -07003529 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -05003530
Stephen Boyd306c3422015-02-05 15:39:11 -08003531 if (!clkspec)
3532 return ERR_PTR(-EINVAL);
3533
Grant Likely766e6a42012-04-09 14:50:06 -05003534 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08003535 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003536 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07003537 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003538 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003539 clk = __clk_create_clk(hw, dev_id, con_id);
Stephen Boydf155d152016-08-15 14:32:23 -07003540 }
Stephen Boyd73e0e492015-02-06 11:42:43 -08003541
Stephen Boydf155d152016-08-15 14:32:23 -07003542 if (!IS_ERR(clk)) {
3543 if (!__clk_get(clk)) {
Stephen Boyd73e0e492015-02-06 11:42:43 -08003544 __clk_free_clk(clk);
3545 clk = ERR_PTR(-ENOENT);
3546 }
3547
Grant Likely766e6a42012-04-09 14:50:06 -05003548 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08003549 }
Grant Likely766e6a42012-04-09 14:50:06 -05003550 }
Stephen Boyd306c3422015-02-05 15:39:11 -08003551 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003552
3553 return clk;
3554}
3555
Stephen Boyd306c3422015-02-05 15:39:11 -08003556/**
3557 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3558 * @clkspec: pointer to a clock specifier data structure
3559 *
3560 * This function looks up a struct clk from the registered list of clock
3561 * providers, an input is a clock specifier data structure as returned
3562 * from the of_parse_phandle_with_args() function call.
3563 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003564struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3565{
Stephen Boyd306c3422015-02-05 15:39:11 -08003566 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05003567}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06003568EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05003569
Stephen Boyd929e7f32016-02-19 15:52:32 -08003570/**
3571 * of_clk_get_parent_count() - Count the number of clocks a device node has
3572 * @np: device node to count
3573 *
3574 * Returns: The number of clocks that are possible parents of this node
3575 */
3576unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07003577{
Stephen Boyd929e7f32016-02-19 15:52:32 -08003578 int count;
3579
3580 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3581 if (count < 0)
3582 return 0;
3583
3584 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07003585}
3586EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3587
Grant Likely766e6a42012-04-09 14:50:06 -05003588const char *of_clk_get_parent_name(struct device_node *np, int index)
3589{
3590 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003591 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003592 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003593 const __be32 *vp;
3594 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003595 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003596 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003597 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05003598
Grant Likely766e6a42012-04-09 14:50:06 -05003599 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3600 &clkspec);
3601 if (rc)
3602 return NULL;
3603
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003604 index = clkspec.args_count ? clkspec.args[0] : 0;
3605 count = 0;
3606
3607 /* if there is an indices property, use it to transfer the index
3608 * specified into an array offset for the clock-output-names property.
3609 */
3610 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3611 if (index == pv) {
3612 index = count;
3613 break;
3614 }
3615 count++;
3616 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09003617 /* We went off the end of 'clock-indices' without finding it */
3618 if (prop && !vp)
3619 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003620
Grant Likely766e6a42012-04-09 14:50:06 -05003621 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003622 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003623 &clk_name) < 0) {
3624 /*
3625 * Best effort to get the name if the clock has been
3626 * registered with the framework. If the clock isn't
3627 * registered, we return the node name as the name of
3628 * the clock as long as #clock-cells = 0.
3629 */
3630 clk = of_clk_get_from_provider(&clkspec);
3631 if (IS_ERR(clk)) {
3632 if (clkspec.args_count == 0)
3633 clk_name = clkspec.np->name;
3634 else
3635 clk_name = NULL;
3636 } else {
3637 clk_name = __clk_get_name(clk);
3638 clk_put(clk);
3639 }
3640 }
3641
Grant Likely766e6a42012-04-09 14:50:06 -05003642
3643 of_node_put(clkspec.np);
3644 return clk_name;
3645}
3646EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3647
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05003648/**
3649 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3650 * number of parents
3651 * @np: Device node pointer associated with clock provider
3652 * @parents: pointer to char array that hold the parents' names
3653 * @size: size of the @parents array
3654 *
3655 * Return: number of parents for the clock node.
3656 */
3657int of_clk_parent_fill(struct device_node *np, const char **parents,
3658 unsigned int size)
3659{
3660 unsigned int i = 0;
3661
3662 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3663 i++;
3664
3665 return i;
3666}
3667EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3668
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003669struct clock_provider {
3670 of_clk_init_cb_t clk_init_cb;
3671 struct device_node *np;
3672 struct list_head node;
3673};
3674
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003675/*
3676 * This function looks for a parent clock. If there is one, then it
3677 * checks that the provider for this parent clock was initialized, in
3678 * this case the parent clock will be ready.
3679 */
3680static int parent_ready(struct device_node *np)
3681{
3682 int i = 0;
3683
3684 while (true) {
3685 struct clk *clk = of_clk_get(np, i);
3686
3687 /* this parent is ready we can check the next one */
3688 if (!IS_ERR(clk)) {
3689 clk_put(clk);
3690 i++;
3691 continue;
3692 }
3693
3694 /* at least one parent is not ready, we exit now */
3695 if (PTR_ERR(clk) == -EPROBE_DEFER)
3696 return 0;
3697
3698 /*
3699 * Here we make assumption that the device tree is
3700 * written correctly. So an error means that there is
3701 * no more parent. As we didn't exit yet, then the
3702 * previous parent are ready. If there is no clock
3703 * parent, no need to wait for them, then we can
3704 * consider their absence as being ready
3705 */
3706 return 1;
3707 }
3708}
3709
Grant Likely766e6a42012-04-09 14:50:06 -05003710/**
Lee Jonesd56f8992016-02-11 13:19:11 -08003711 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3712 * @np: Device node pointer associated with clock provider
3713 * @index: clock index
3714 * @flags: pointer to clk_core->flags
3715 *
3716 * Detects if the clock-critical property exists and, if so, sets the
3717 * corresponding CLK_IS_CRITICAL flag.
3718 *
3719 * Do not use this function. It exists only for legacy Device Tree
3720 * bindings, such as the one-clock-per-node style that are outdated.
3721 * Those bindings typically put all clock data into .dts and the Linux
3722 * driver has no clock data, thus making it impossible to set this flag
3723 * correctly from the driver. Only those drivers may call
3724 * of_clk_detect_critical from their setup functions.
3725 *
3726 * Return: error code or zero on success
3727 */
3728int of_clk_detect_critical(struct device_node *np,
3729 int index, unsigned long *flags)
3730{
3731 struct property *prop;
3732 const __be32 *cur;
3733 uint32_t idx;
3734
3735 if (!np || !flags)
3736 return -EINVAL;
3737
3738 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
3739 if (index == idx)
3740 *flags |= CLK_IS_CRITICAL;
3741
3742 return 0;
3743}
3744
3745/**
Grant Likely766e6a42012-04-09 14:50:06 -05003746 * of_clk_init() - Scan and init clock providers from the DT
3747 * @matches: array of compatible values and init functions for providers.
3748 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003749 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003750 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003751 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003752 */
3753void __init of_clk_init(const struct of_device_id *matches)
3754{
Alex Elder7f7ed582013-08-22 11:31:31 -05003755 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003756 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003757 struct clock_provider *clk_provider, *next;
3758 bool is_init_done;
3759 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07003760 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05003761
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303762 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003763 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303764
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003765 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003766 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003767 struct clock_provider *parent;
3768
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01003769 if (!of_device_is_available(np))
3770 continue;
3771
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003772 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
3773 if (!parent) {
3774 list_for_each_entry_safe(clk_provider, next,
3775 &clk_provider_list, node) {
3776 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003777 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003778 kfree(clk_provider);
3779 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003780 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003781 return;
3782 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003783
3784 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003785 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003786 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003787 }
3788
3789 while (!list_empty(&clk_provider_list)) {
3790 is_init_done = false;
3791 list_for_each_entry_safe(clk_provider, next,
3792 &clk_provider_list, node) {
3793 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003794
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02003795 /* Don't populate platform devices */
3796 of_node_set_flag(clk_provider->np,
3797 OF_POPULATED);
3798
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003799 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003800 of_clk_set_defaults(clk_provider->np, true);
3801
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003802 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003803 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003804 kfree(clk_provider);
3805 is_init_done = true;
3806 }
3807 }
3808
3809 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003810 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003811 * remaining providers during the last loop, so now we
3812 * initialize all the remaining ones unconditionally
3813 * in case the clock parent was not mandatory
3814 */
3815 if (!is_init_done)
3816 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003817 }
3818}
3819#endif