blob: dc05cb33976140784e168afe46ea09d2e3da4e18 [file] [log] [blame]
Stephen Boydebafb632018-12-11 09:43:03 -08001// SPDX-License-Identifier: GPL-2.0
Mike Turquetteb24764902012-03-15 23:11:19 -07002/*
3 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
4 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
5 *
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03006 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
Mike Turquetteb24764902012-03-15 23:11:19 -07007 */
8
Stephen Boyd3c373112015-06-19 15:00:46 -07009#include <linux/clk.h>
Michael Turquetteb09d6d92015-01-29 14:22:50 -080010#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020011#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070012#include <linux/module.h>
13#include <linux/mutex.h>
14#include <linux/spinlock.h>
15#include <linux/err.h>
16#include <linux/list.h>
17#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050018#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070019#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053020#include <linux/init.h>
Marek Szyprowski9a34b452017-08-21 10:04:59 +020021#include <linux/pm_runtime.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070022#include <linux/sched.h>
Stephen Boyd562ef0b2015-05-01 12:16:14 -070023#include <linux/clkdev.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070024
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020025#include "clk.h"
26
Mike Turquetteb24764902012-03-15 23:11:19 -070027static DEFINE_SPINLOCK(enable_lock);
28static DEFINE_MUTEX(prepare_lock);
29
Mike Turquette533ddeb2013-03-28 13:59:02 -070030static struct task_struct *prepare_owner;
31static struct task_struct *enable_owner;
32
33static int prepare_refcnt;
34static int enable_refcnt;
35
Mike Turquetteb24764902012-03-15 23:11:19 -070036static HLIST_HEAD(clk_root_list);
37static HLIST_HEAD(clk_orphan_list);
38static LIST_HEAD(clk_notifier_list);
39
Michael Turquetteb09d6d92015-01-29 14:22:50 -080040/*** private data structures ***/
41
Stephen Boydfc0c2092019-04-12 11:31:47 -070042struct clk_parent_map {
43 const struct clk_hw *hw;
44 struct clk_core *core;
45 const char *fw_name;
46 const char *name;
Stephen Boyd601b6e92019-04-12 11:31:49 -070047 int index;
Stephen Boydfc0c2092019-04-12 11:31:47 -070048};
49
Michael Turquetteb09d6d92015-01-29 14:22:50 -080050struct clk_core {
51 const char *name;
52 const struct clk_ops *ops;
53 struct clk_hw *hw;
54 struct module *owner;
Marek Szyprowski9a34b452017-08-21 10:04:59 +020055 struct device *dev;
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -070056 struct device_node *of_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080057 struct clk_core *parent;
Stephen Boydfc0c2092019-04-12 11:31:47 -070058 struct clk_parent_map *parents;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080059 u8 num_parents;
60 u8 new_parent_index;
61 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010062 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080063 unsigned long new_rate;
64 struct clk_core *new_parent;
65 struct clk_core *new_child;
66 unsigned long flags;
Heiko Stuebnere6500342015-04-22 22:53:05 +020067 bool orphan;
Miquel Raynal24478832018-12-04 20:24:37 +010068 bool rpm_enabled;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080069 unsigned int enable_count;
70 unsigned int prepare_count;
Jerome Brunete55a8392017-12-01 22:51:56 +010071 unsigned int protect_count;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070072 unsigned long min_rate;
73 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080074 unsigned long accuracy;
75 int phase;
Jerome Brunet9fba7382018-06-19 16:41:41 +020076 struct clk_duty duty;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080077 struct hlist_head children;
78 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010079 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080080 unsigned int notifier_count;
81#ifdef CONFIG_DEBUG_FS
82 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020083 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080084#endif
85 struct kref ref;
86};
87
Stephen Boyddfc202e2015-02-02 14:37:41 -080088#define CREATE_TRACE_POINTS
89#include <trace/events/clk.h>
90
Michael Turquetteb09d6d92015-01-29 14:22:50 -080091struct clk {
92 struct clk_core *core;
Stephen Boydefa85042018-12-11 08:34:16 -080093 struct device *dev;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080094 const char *dev_id;
95 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010096 unsigned long min_rate;
97 unsigned long max_rate;
Jerome Brunet55e9b8b2017-12-01 22:51:59 +010098 unsigned int exclusive_count;
Stephen Boyd50595f82015-02-06 11:42:44 -080099 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -0800100};
101
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200102/*** runtime pm ***/
103static int clk_pm_runtime_get(struct clk_core *core)
104{
Miquel Raynal24478832018-12-04 20:24:37 +0100105 int ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200106
Miquel Raynal24478832018-12-04 20:24:37 +0100107 if (!core->rpm_enabled)
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200108 return 0;
109
110 ret = pm_runtime_get_sync(core->dev);
111 return ret < 0 ? ret : 0;
112}
113
114static void clk_pm_runtime_put(struct clk_core *core)
115{
Miquel Raynal24478832018-12-04 20:24:37 +0100116 if (!core->rpm_enabled)
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200117 return;
118
119 pm_runtime_put_sync(core->dev);
120}
121
Mike Turquetteeab89f62013-03-28 13:59:01 -0700122/*** locking ***/
123static void clk_prepare_lock(void)
124{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700125 if (!mutex_trylock(&prepare_lock)) {
126 if (prepare_owner == current) {
127 prepare_refcnt++;
128 return;
129 }
130 mutex_lock(&prepare_lock);
131 }
132 WARN_ON_ONCE(prepare_owner != NULL);
133 WARN_ON_ONCE(prepare_refcnt != 0);
134 prepare_owner = current;
135 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700136}
137
138static void clk_prepare_unlock(void)
139{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700140 WARN_ON_ONCE(prepare_owner != current);
141 WARN_ON_ONCE(prepare_refcnt == 0);
142
143 if (--prepare_refcnt)
144 return;
145 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700146 mutex_unlock(&prepare_lock);
147}
148
149static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700150 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700151{
152 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700153
David Lechnera12aa8a2018-01-04 19:46:08 -0600154 /*
155 * On UP systems, spin_trylock_irqsave() always returns true, even if
156 * we already hold the lock. So, in that case, we rely only on
157 * reference counting.
158 */
159 if (!IS_ENABLED(CONFIG_SMP) ||
160 !spin_trylock_irqsave(&enable_lock, flags)) {
Mike Turquette533ddeb2013-03-28 13:59:02 -0700161 if (enable_owner == current) {
162 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700163 __acquire(enable_lock);
David Lechnera12aa8a2018-01-04 19:46:08 -0600164 if (!IS_ENABLED(CONFIG_SMP))
165 local_save_flags(flags);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700166 return flags;
167 }
168 spin_lock_irqsave(&enable_lock, flags);
169 }
170 WARN_ON_ONCE(enable_owner != NULL);
171 WARN_ON_ONCE(enable_refcnt != 0);
172 enable_owner = current;
173 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700174 return flags;
175}
176
177static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700178 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700179{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700180 WARN_ON_ONCE(enable_owner != current);
181 WARN_ON_ONCE(enable_refcnt == 0);
182
Stephen Boyda57aa182015-07-24 12:24:48 -0700183 if (--enable_refcnt) {
184 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700185 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700186 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700187 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700188 spin_unlock_irqrestore(&enable_lock, flags);
189}
190
Jerome Brunete55a8392017-12-01 22:51:56 +0100191static bool clk_core_rate_is_protected(struct clk_core *core)
192{
193 return core->protect_count;
194}
195
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700196static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530197{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200198 bool ret = false;
199
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700200 /*
201 * .is_prepared is optional for clocks that can prepare
202 * fall back to software usage counter if it is missing
203 */
204 if (!core->ops->is_prepared)
205 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530206
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200207 if (!clk_pm_runtime_get(core)) {
208 ret = core->ops->is_prepared(core->hw);
209 clk_pm_runtime_put(core);
210 }
211
212 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530213}
214
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700215static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530216{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200217 bool ret = false;
218
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700219 /*
220 * .is_enabled is only mandatory for clocks that gate
221 * fall back to software usage counter if .is_enabled is missing
222 */
223 if (!core->ops->is_enabled)
224 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530225
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200226 /*
227 * Check if clock controller's device is runtime active before
228 * calling .is_enabled callback. If not, assume that clock is
229 * disabled, because we might be called from atomic context, from
230 * which pm_runtime_get() is not allowed.
231 * This function is called mainly from clk_disable_unused_subtree,
232 * which ensures proper runtime pm activation of controller before
233 * taking enable spinlock, but the below check is needed if one tries
234 * to call it from other places.
235 */
Miquel Raynal24478832018-12-04 20:24:37 +0100236 if (core->rpm_enabled) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200237 pm_runtime_get_noresume(core->dev);
238 if (!pm_runtime_active(core->dev)) {
239 ret = false;
240 goto done;
241 }
242 }
243
244 ret = core->ops->is_enabled(core->hw);
245done:
Miquel Raynal24478832018-12-04 20:24:37 +0100246 if (core->rpm_enabled)
Dong Aisheng756efe12017-12-22 17:46:04 +0800247 pm_runtime_put(core->dev);
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200248
249 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530250}
251
Mike Turquetteb24764902012-03-15 23:11:19 -0700252/*** helper functions ***/
253
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200254const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700255{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100256 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700257}
Niels de Vos48950842012-12-13 13:12:25 +0100258EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700259
Stephen Boyde7df6f62015-08-12 13:04:56 -0700260const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700261{
262 return hw->core->name;
263}
264EXPORT_SYMBOL_GPL(clk_hw_get_name);
265
Russ Dill65800b22012-11-26 11:20:09 -0800266struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700267{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100268 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700269}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800270EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700271
Stephen Boyde7df6f62015-08-12 13:04:56 -0700272unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700273{
274 return hw->core->num_parents;
275}
276EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
277
Stephen Boyde7df6f62015-08-12 13:04:56 -0700278struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700279{
280 return hw->core->parent ? hw->core->parent->hw : NULL;
281}
282EXPORT_SYMBOL_GPL(clk_hw_get_parent);
283
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700284static struct clk_core *__clk_lookup_subtree(const char *name,
285 struct clk_core *core)
286{
287 struct clk_core *child;
288 struct clk_core *ret;
289
290 if (!strcmp(core->name, name))
291 return core;
292
293 hlist_for_each_entry(child, &core->children, child_node) {
294 ret = __clk_lookup_subtree(name, child);
295 if (ret)
296 return ret;
297 }
298
299 return NULL;
300}
301
302static struct clk_core *clk_core_lookup(const char *name)
303{
304 struct clk_core *root_clk;
305 struct clk_core *ret;
306
307 if (!name)
308 return NULL;
309
310 /* search the 'proper' clk tree first */
311 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
312 ret = __clk_lookup_subtree(name, root_clk);
313 if (ret)
314 return ret;
315 }
316
317 /* if not found, then search the orphan tree */
318 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
319 ret = __clk_lookup_subtree(name, root_clk);
320 if (ret)
321 return ret;
322 }
323
324 return NULL;
325}
326
Stephen Boydfc0c2092019-04-12 11:31:47 -0700327/**
Stephen Boyddde4eff2019-04-12 11:31:48 -0700328 * clk_core_get - Find the clk_core parent of a clk
Stephen Boydfc0c2092019-04-12 11:31:47 -0700329 * @core: clk to find parent of
Stephen Boyd601b6e92019-04-12 11:31:49 -0700330 * @name: name to search for (if string based)
331 * @index: index to use for search (if DT index based)
Stephen Boydfc0c2092019-04-12 11:31:47 -0700332 *
333 * This is the preferred method for clk providers to find the parent of a
334 * clk when that parent is external to the clk controller. The parent_names
335 * array is indexed and treated as a local name matching a string in the device
Stephen Boyddde4eff2019-04-12 11:31:48 -0700336 * node's 'clock-names' property or as the 'con_id' matching the device's
337 * dev_name() in a clk_lookup. This allows clk providers to use their own
Stephen Boydfc0c2092019-04-12 11:31:47 -0700338 * namespace instead of looking for a globally unique parent string.
339 *
340 * For example the following DT snippet would allow a clock registered by the
341 * clock-controller@c001 that has a clk_init_data::parent_data array
342 * with 'xtal' in the 'name' member to find the clock provided by the
343 * clock-controller@f00abcd without needing to get the globally unique name of
344 * the xtal clk.
345 *
346 * parent: clock-controller@f00abcd {
347 * reg = <0xf00abcd 0xabcd>;
348 * #clock-cells = <0>;
349 * };
350 *
351 * clock-controller@c001 {
352 * reg = <0xc001 0xf00d>;
353 * clocks = <&parent>;
354 * clock-names = "xtal";
355 * #clock-cells = <1>;
356 * };
357 *
358 * Returns: -ENOENT when the provider can't be found or the clk doesn't
359 * exist in the provider. -EINVAL when the name can't be found. NULL when the
360 * provider knows about the clk but it isn't provided on this system.
361 * A valid clk_core pointer when the clk can be found in the provider.
362 */
Stephen Boyd601b6e92019-04-12 11:31:49 -0700363static struct clk_core *clk_core_get(struct clk_core *core, const char *name,
364 int index)
Stephen Boydfc0c2092019-04-12 11:31:47 -0700365{
Stephen Boyddde4eff2019-04-12 11:31:48 -0700366 struct clk_hw *hw = ERR_PTR(-ENOENT);
367 struct device *dev = core->dev;
368 const char *dev_id = dev ? dev_name(dev) : NULL;
Stephen Boydfc0c2092019-04-12 11:31:47 -0700369 struct device_node *np = core->of_node;
370
Stephen Boyd601b6e92019-04-12 11:31:49 -0700371 if (np && index >= 0)
372 hw = of_clk_get_hw(np, index, name);
Stephen Boydfc0c2092019-04-12 11:31:47 -0700373
Stephen Boyddde4eff2019-04-12 11:31:48 -0700374 /*
375 * If the DT search above couldn't find the provider or the provider
376 * didn't know about this clk, fallback to looking up via clkdev based
377 * clk_lookups
378 */
Stephen Boyd601b6e92019-04-12 11:31:49 -0700379 if (PTR_ERR(hw) == -ENOENT && name)
Stephen Boyddde4eff2019-04-12 11:31:48 -0700380 hw = clk_find_hw(dev_id, name);
381
382 if (IS_ERR(hw))
Stephen Boydfc0c2092019-04-12 11:31:47 -0700383 return ERR_CAST(hw);
384
385 return hw->core;
386}
387
388static void clk_core_fill_parent_index(struct clk_core *core, u8 index)
389{
390 struct clk_parent_map *entry = &core->parents[index];
391 struct clk_core *parent = ERR_PTR(-ENOENT);
392
393 if (entry->hw) {
394 parent = entry->hw->core;
395 /*
396 * We have a direct reference but it isn't registered yet?
397 * Orphan it and let clk_reparent() update the orphan status
398 * when the parent is registered.
399 */
400 if (!parent)
401 parent = ERR_PTR(-EPROBE_DEFER);
402 } else {
Stephen Boyd601b6e92019-04-12 11:31:49 -0700403 parent = clk_core_get(core, entry->fw_name, entry->index);
Stephen Boydfc0c2092019-04-12 11:31:47 -0700404 if (IS_ERR(parent) && PTR_ERR(parent) == -ENOENT)
405 parent = clk_core_lookup(entry->name);
406 }
407
408 /* Only cache it if it's not an error */
409 if (!IS_ERR(parent))
410 entry->core = parent;
411}
412
Stephen Boydd6968fc2015-04-30 13:54:13 -0700413static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100414 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100415{
Stephen Boydfc0c2092019-04-12 11:31:47 -0700416 if (!core || index >= core->num_parents || !core->parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100417 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900418
Stephen Boydfc0c2092019-04-12 11:31:47 -0700419 if (!core->parents[index].core)
420 clk_core_fill_parent_index(core, index);
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900421
Stephen Boydfc0c2092019-04-12 11:31:47 -0700422 return core->parents[index].core;
James Hogan7ef3dcc2013-07-29 12:24:58 +0100423}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100424
Stephen Boyde7df6f62015-08-12 13:04:56 -0700425struct clk_hw *
426clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700427{
428 struct clk_core *parent;
429
430 parent = clk_core_get_parent_by_index(hw->core, index);
431
432 return !parent ? NULL : parent->hw;
433}
434EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
435
Russ Dill65800b22012-11-26 11:20:09 -0800436unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700437{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100438 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700439}
440
Stephen Boydd6968fc2015-04-30 13:54:13 -0700441static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700442{
443 unsigned long ret;
444
Stephen Boydd6968fc2015-04-30 13:54:13 -0700445 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530446 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700447 goto out;
448 }
449
Stephen Boydd6968fc2015-04-30 13:54:13 -0700450 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700451
Stephen Boyd47b0eeb2016-02-02 17:24:56 -0800452 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -0700453 goto out;
454
Stephen Boydd6968fc2015-04-30 13:54:13 -0700455 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530456 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700457
458out:
459 return ret;
460}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100461
Stephen Boyde7df6f62015-08-12 13:04:56 -0700462unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700463{
464 return clk_core_get_rate_nolock(hw->core);
465}
466EXPORT_SYMBOL_GPL(clk_hw_get_rate);
467
Stephen Boydd6968fc2015-04-30 13:54:13 -0700468static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100469{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700470 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100471 return 0;
472
Stephen Boydd6968fc2015-04-30 13:54:13 -0700473 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100474}
475
Russ Dill65800b22012-11-26 11:20:09 -0800476unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700477{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100478 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700479}
Thierry Redingb05c6832013-09-03 09:43:51 +0200480EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700481
Stephen Boyde7df6f62015-08-12 13:04:56 -0700482unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700483{
484 return hw->core->flags;
485}
486EXPORT_SYMBOL_GPL(clk_hw_get_flags);
487
Stephen Boyde7df6f62015-08-12 13:04:56 -0700488bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700489{
490 return clk_core_is_prepared(hw->core);
491}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100492EXPORT_SYMBOL_GPL(clk_hw_is_prepared);
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700493
Jerome Brunete55a8392017-12-01 22:51:56 +0100494bool clk_hw_rate_is_protected(const struct clk_hw *hw)
495{
496 return clk_core_rate_is_protected(hw->core);
497}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100498EXPORT_SYMBOL_GPL(clk_hw_rate_is_protected);
Jerome Brunete55a8392017-12-01 22:51:56 +0100499
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200500bool clk_hw_is_enabled(const struct clk_hw *hw)
501{
502 return clk_core_is_enabled(hw->core);
503}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100504EXPORT_SYMBOL_GPL(clk_hw_is_enabled);
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200505
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100506bool __clk_is_enabled(struct clk *clk)
507{
508 if (!clk)
509 return false;
510
511 return clk_core_is_enabled(clk->core);
512}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800513EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700514
Stephen Boyd15a02c12015-01-19 18:05:28 -0800515static bool mux_is_better_rate(unsigned long rate, unsigned long now,
516 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100517{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800518 if (flags & CLK_MUX_ROUND_CLOSEST)
519 return abs(now - rate) < abs(best - rate);
520
521 return now <= rate && now > best;
522}
523
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200524int clk_mux_determine_rate_flags(struct clk_hw *hw,
525 struct clk_rate_request *req,
526 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100527{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100528 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200529 int i, num_parents, ret;
530 unsigned long best = 0;
531 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100532
533 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100534 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
535 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200536 if (core->flags & CLK_SET_RATE_PARENT) {
537 ret = __clk_determine_rate(parent ? parent->hw : NULL,
538 &parent_req);
539 if (ret)
540 return ret;
541
542 best = parent_req.rate;
543 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100544 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200545 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100546 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200547 }
548
James Hogane366fdd2013-07-29 12:25:02 +0100549 goto out;
550 }
551
552 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100553 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100554 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100555 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100556 if (!parent)
557 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200558
559 if (core->flags & CLK_SET_RATE_PARENT) {
560 parent_req = *req;
561 ret = __clk_determine_rate(parent->hw, &parent_req);
562 if (ret)
563 continue;
564 } else {
565 parent_req.rate = clk_core_get_rate_nolock(parent);
566 }
567
568 if (mux_is_better_rate(req->rate, parent_req.rate,
569 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100570 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200571 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100572 }
573 }
574
Boris Brezillon57d866e2015-07-09 22:39:38 +0200575 if (!best_parent)
576 return -EINVAL;
577
James Hogane366fdd2013-07-29 12:25:02 +0100578out:
579 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200580 req->best_parent_hw = best_parent->hw;
581 req->best_parent_rate = best;
582 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100583
Boris Brezillon0817b622015-07-07 20:48:08 +0200584 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100585}
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200586EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800587
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100588struct clk *__clk_lookup(const char *name)
589{
590 struct clk_core *core = clk_core_lookup(name);
591
592 return !core ? NULL : core->hw->clk;
593}
594
Stephen Boydd6968fc2015-04-30 13:54:13 -0700595static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100596 unsigned long *min_rate,
597 unsigned long *max_rate)
598{
599 struct clk *clk_user;
600
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700601 *min_rate = core->min_rate;
602 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100603
Stephen Boydd6968fc2015-04-30 13:54:13 -0700604 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100605 *min_rate = max(*min_rate, clk_user->min_rate);
606
Stephen Boydd6968fc2015-04-30 13:54:13 -0700607 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100608 *max_rate = min(*max_rate, clk_user->max_rate);
609}
610
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700611void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
612 unsigned long max_rate)
613{
614 hw->core->min_rate = min_rate;
615 hw->core->max_rate = max_rate;
616}
617EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
618
Stephen Boyd15a02c12015-01-19 18:05:28 -0800619/*
620 * Helper for finding best parent to provide a given frequency. This can be used
621 * directly as a determine_rate callback (e.g. for a mux), or from a more
622 * complex clock that may combine a mux with other operations.
623 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200624int __clk_mux_determine_rate(struct clk_hw *hw,
625 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800626{
Boris Brezillon0817b622015-07-07 20:48:08 +0200627 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800628}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800629EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100630
Boris Brezillon0817b622015-07-07 20:48:08 +0200631int __clk_mux_determine_rate_closest(struct clk_hw *hw,
632 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800633{
Boris Brezillon0817b622015-07-07 20:48:08 +0200634 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800635}
636EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
637
Mike Turquetteb24764902012-03-15 23:11:19 -0700638/*** clk api ***/
639
Jerome Brunete55a8392017-12-01 22:51:56 +0100640static void clk_core_rate_unprotect(struct clk_core *core)
641{
642 lockdep_assert_held(&prepare_lock);
643
644 if (!core)
645 return;
646
Fabio Estevamab525dc2018-01-16 10:50:34 -0200647 if (WARN(core->protect_count == 0,
648 "%s already unprotected\n", core->name))
Jerome Brunete55a8392017-12-01 22:51:56 +0100649 return;
650
651 if (--core->protect_count > 0)
652 return;
653
654 clk_core_rate_unprotect(core->parent);
655}
656
657static int clk_core_rate_nuke_protect(struct clk_core *core)
658{
659 int ret;
660
661 lockdep_assert_held(&prepare_lock);
662
663 if (!core)
664 return -EINVAL;
665
666 if (core->protect_count == 0)
667 return 0;
668
669 ret = core->protect_count;
670 core->protect_count = 1;
671 clk_core_rate_unprotect(core);
672
673 return ret;
674}
675
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100676/**
677 * clk_rate_exclusive_put - release exclusivity over clock rate control
678 * @clk: the clk over which the exclusivity is released
679 *
680 * clk_rate_exclusive_put() completes a critical section during which a clock
681 * consumer cannot tolerate any other consumer making any operation on the
682 * clock which could result in a rate change or rate glitch. Exclusive clocks
683 * cannot have their rate changed, either directly or indirectly due to changes
684 * further up the parent chain of clocks. As a result, clocks up parent chain
685 * also get under exclusive control of the calling consumer.
686 *
687 * If exlusivity is claimed more than once on clock, even by the same consumer,
688 * the rate effectively gets locked as exclusivity can't be preempted.
689 *
690 * Calls to clk_rate_exclusive_put() must be balanced with calls to
691 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
692 * error status.
693 */
694void clk_rate_exclusive_put(struct clk *clk)
695{
696 if (!clk)
697 return;
698
699 clk_prepare_lock();
700
701 /*
702 * if there is something wrong with this consumer protect count, stop
703 * here before messing with the provider
704 */
705 if (WARN_ON(clk->exclusive_count <= 0))
706 goto out;
707
708 clk_core_rate_unprotect(clk->core);
709 clk->exclusive_count--;
710out:
711 clk_prepare_unlock();
712}
713EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
714
Jerome Brunete55a8392017-12-01 22:51:56 +0100715static void clk_core_rate_protect(struct clk_core *core)
716{
717 lockdep_assert_held(&prepare_lock);
718
719 if (!core)
720 return;
721
722 if (core->protect_count == 0)
723 clk_core_rate_protect(core->parent);
724
725 core->protect_count++;
726}
727
728static void clk_core_rate_restore_protect(struct clk_core *core, int count)
729{
730 lockdep_assert_held(&prepare_lock);
731
732 if (!core)
733 return;
734
735 if (count == 0)
736 return;
737
738 clk_core_rate_protect(core);
739 core->protect_count = count;
740}
741
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100742/**
743 * clk_rate_exclusive_get - get exclusivity over the clk rate control
744 * @clk: the clk over which the exclusity of rate control is requested
745 *
746 * clk_rate_exlusive_get() begins a critical section during which a clock
747 * consumer cannot tolerate any other consumer making any operation on the
748 * clock which could result in a rate change or rate glitch. Exclusive clocks
749 * cannot have their rate changed, either directly or indirectly due to changes
750 * further up the parent chain of clocks. As a result, clocks up parent chain
751 * also get under exclusive control of the calling consumer.
752 *
753 * If exlusivity is claimed more than once on clock, even by the same consumer,
754 * the rate effectively gets locked as exclusivity can't be preempted.
755 *
756 * Calls to clk_rate_exclusive_get() should be balanced with calls to
757 * clk_rate_exclusive_put(). Calls to this function may sleep.
758 * Returns 0 on success, -EERROR otherwise
759 */
760int clk_rate_exclusive_get(struct clk *clk)
761{
762 if (!clk)
763 return 0;
764
765 clk_prepare_lock();
766 clk_core_rate_protect(clk->core);
767 clk->exclusive_count++;
768 clk_prepare_unlock();
769
770 return 0;
771}
772EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
773
Stephen Boydd6968fc2015-04-30 13:54:13 -0700774static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700775{
Stephen Boyda6334722015-05-06 17:00:54 -0700776 lockdep_assert_held(&prepare_lock);
777
Stephen Boydd6968fc2015-04-30 13:54:13 -0700778 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700779 return;
780
Fabio Estevamab525dc2018-01-16 10:50:34 -0200781 if (WARN(core->prepare_count == 0,
782 "%s already unprepared\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700783 return;
784
Fabio Estevamab525dc2018-01-16 10:50:34 -0200785 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
786 "Unpreparing critical %s\n", core->name))
Lee Jones2e20fbf2016-02-11 13:19:10 -0800787 return;
788
Jerome Brunet9461f7b2018-06-19 15:40:51 +0200789 if (core->flags & CLK_SET_RATE_GATE)
790 clk_core_rate_unprotect(core);
791
Stephen Boydd6968fc2015-04-30 13:54:13 -0700792 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700793 return;
794
Fabio Estevamab525dc2018-01-16 10:50:34 -0200795 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700796
Stephen Boydd6968fc2015-04-30 13:54:13 -0700797 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800798
Stephen Boydd6968fc2015-04-30 13:54:13 -0700799 if (core->ops->unprepare)
800 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700801
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200802 clk_pm_runtime_put(core);
803
Stephen Boydd6968fc2015-04-30 13:54:13 -0700804 trace_clk_unprepare_complete(core);
805 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700806}
807
Dong Aishenga6adc302016-06-30 17:31:11 +0800808static void clk_core_unprepare_lock(struct clk_core *core)
809{
810 clk_prepare_lock();
811 clk_core_unprepare(core);
812 clk_prepare_unlock();
813}
814
Mike Turquetteb24764902012-03-15 23:11:19 -0700815/**
816 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200817 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700818 *
819 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
820 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
821 * if the operation may sleep. One example is a clk which is accessed over
822 * I2c. In the complex case a clk gate operation may require a fast and a slow
823 * part. It is this reason that clk_unprepare and clk_disable are not mutually
824 * exclusive. In fact clk_disable must be called before clk_unprepare.
825 */
826void clk_unprepare(struct clk *clk)
827{
Stephen Boyd63589e92014-03-26 16:06:37 -0700828 if (IS_ERR_OR_NULL(clk))
829 return;
830
Dong Aishenga6adc302016-06-30 17:31:11 +0800831 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700832}
833EXPORT_SYMBOL_GPL(clk_unprepare);
834
Stephen Boydd6968fc2015-04-30 13:54:13 -0700835static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700836{
837 int ret = 0;
838
Stephen Boyda6334722015-05-06 17:00:54 -0700839 lockdep_assert_held(&prepare_lock);
840
Stephen Boydd6968fc2015-04-30 13:54:13 -0700841 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700842 return 0;
843
Stephen Boydd6968fc2015-04-30 13:54:13 -0700844 if (core->prepare_count == 0) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200845 ret = clk_pm_runtime_get(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700846 if (ret)
847 return ret;
848
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200849 ret = clk_core_prepare(core->parent);
850 if (ret)
851 goto runtime_put;
852
Stephen Boydd6968fc2015-04-30 13:54:13 -0700853 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800854
Stephen Boydd6968fc2015-04-30 13:54:13 -0700855 if (core->ops->prepare)
856 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800857
Stephen Boydd6968fc2015-04-30 13:54:13 -0700858 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800859
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200860 if (ret)
861 goto unprepare;
Mike Turquetteb24764902012-03-15 23:11:19 -0700862 }
863
Stephen Boydd6968fc2015-04-30 13:54:13 -0700864 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700865
Jerome Brunet9461f7b2018-06-19 15:40:51 +0200866 /*
867 * CLK_SET_RATE_GATE is a special case of clock protection
868 * Instead of a consumer claiming exclusive rate control, it is
869 * actually the provider which prevents any consumer from making any
870 * operation which could result in a rate change or rate glitch while
871 * the clock is prepared.
872 */
873 if (core->flags & CLK_SET_RATE_GATE)
874 clk_core_rate_protect(core);
875
Mike Turquetteb24764902012-03-15 23:11:19 -0700876 return 0;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200877unprepare:
878 clk_core_unprepare(core->parent);
879runtime_put:
880 clk_pm_runtime_put(core);
881 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700882}
883
Dong Aishenga6adc302016-06-30 17:31:11 +0800884static int clk_core_prepare_lock(struct clk_core *core)
885{
886 int ret;
887
888 clk_prepare_lock();
889 ret = clk_core_prepare(core);
890 clk_prepare_unlock();
891
892 return ret;
893}
894
Mike Turquetteb24764902012-03-15 23:11:19 -0700895/**
896 * clk_prepare - prepare a clock source
897 * @clk: the clk being prepared
898 *
899 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
900 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
901 * operation may sleep. One example is a clk which is accessed over I2c. In
902 * the complex case a clk ungate operation may require a fast and a slow part.
903 * It is this reason that clk_prepare and clk_enable are not mutually
904 * exclusive. In fact clk_prepare must be called before clk_enable.
905 * Returns 0 on success, -EERROR otherwise.
906 */
907int clk_prepare(struct clk *clk)
908{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100909 if (!clk)
910 return 0;
911
Dong Aishenga6adc302016-06-30 17:31:11 +0800912 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700913}
914EXPORT_SYMBOL_GPL(clk_prepare);
915
Stephen Boydd6968fc2015-04-30 13:54:13 -0700916static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700917{
Stephen Boyda6334722015-05-06 17:00:54 -0700918 lockdep_assert_held(&enable_lock);
919
Stephen Boydd6968fc2015-04-30 13:54:13 -0700920 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700921 return;
922
Fabio Estevamab525dc2018-01-16 10:50:34 -0200923 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700924 return;
925
Fabio Estevamab525dc2018-01-16 10:50:34 -0200926 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
927 "Disabling critical %s\n", core->name))
Lee Jones2e20fbf2016-02-11 13:19:10 -0800928 return;
929
Stephen Boydd6968fc2015-04-30 13:54:13 -0700930 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700931 return;
932
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700933 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800934
Stephen Boydd6968fc2015-04-30 13:54:13 -0700935 if (core->ops->disable)
936 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700937
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700938 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800939
Stephen Boydd6968fc2015-04-30 13:54:13 -0700940 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100941}
942
Dong Aishenga6adc302016-06-30 17:31:11 +0800943static void clk_core_disable_lock(struct clk_core *core)
944{
945 unsigned long flags;
946
947 flags = clk_enable_lock();
948 clk_core_disable(core);
949 clk_enable_unlock(flags);
950}
951
Mike Turquetteb24764902012-03-15 23:11:19 -0700952/**
953 * clk_disable - gate a clock
954 * @clk: the clk being gated
955 *
956 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
957 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
958 * clk if the operation is fast and will never sleep. One example is a
959 * SoC-internal clk which is controlled via simple register writes. In the
960 * complex case a clk gate operation may require a fast and a slow part. It is
961 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
962 * In fact clk_disable must be called before clk_unprepare.
963 */
964void clk_disable(struct clk *clk)
965{
Stephen Boyd63589e92014-03-26 16:06:37 -0700966 if (IS_ERR_OR_NULL(clk))
967 return;
968
Dong Aishenga6adc302016-06-30 17:31:11 +0800969 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700970}
971EXPORT_SYMBOL_GPL(clk_disable);
972
Stephen Boydd6968fc2015-04-30 13:54:13 -0700973static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700974{
975 int ret = 0;
976
Stephen Boyda6334722015-05-06 17:00:54 -0700977 lockdep_assert_held(&enable_lock);
978
Stephen Boydd6968fc2015-04-30 13:54:13 -0700979 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700980 return 0;
981
Fabio Estevamab525dc2018-01-16 10:50:34 -0200982 if (WARN(core->prepare_count == 0,
983 "Enabling unprepared %s\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700984 return -ESHUTDOWN;
985
Stephen Boydd6968fc2015-04-30 13:54:13 -0700986 if (core->enable_count == 0) {
987 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700988
989 if (ret)
990 return ret;
991
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700992 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800993
Stephen Boydd6968fc2015-04-30 13:54:13 -0700994 if (core->ops->enable)
995 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800996
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700997 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800998
999 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001000 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001001 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001002 }
1003 }
1004
Stephen Boydd6968fc2015-04-30 13:54:13 -07001005 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07001006 return 0;
1007}
1008
Dong Aishenga6adc302016-06-30 17:31:11 +08001009static int clk_core_enable_lock(struct clk_core *core)
1010{
1011 unsigned long flags;
1012 int ret;
1013
1014 flags = clk_enable_lock();
1015 ret = clk_core_enable(core);
1016 clk_enable_unlock(flags);
1017
1018 return ret;
1019}
1020
Keerthy43536542018-09-04 12:19:36 +05301021/**
1022 * clk_gate_restore_context - restore context for poweroff
1023 * @hw: the clk_hw pointer of clock whose state is to be restored
1024 *
1025 * The clock gate restore context function enables or disables
1026 * the gate clocks based on the enable_count. This is done in cases
1027 * where the clock context is lost and based on the enable_count
1028 * the clock either needs to be enabled/disabled. This
1029 * helps restore the state of gate clocks.
1030 */
1031void clk_gate_restore_context(struct clk_hw *hw)
1032{
Stephen Boyd9be76622018-10-11 09:28:13 -07001033 struct clk_core *core = hw->core;
1034
1035 if (core->enable_count)
1036 core->ops->enable(hw);
Keerthy43536542018-09-04 12:19:36 +05301037 else
Stephen Boyd9be76622018-10-11 09:28:13 -07001038 core->ops->disable(hw);
Keerthy43536542018-09-04 12:19:36 +05301039}
1040EXPORT_SYMBOL_GPL(clk_gate_restore_context);
1041
Stephen Boyd9be76622018-10-11 09:28:13 -07001042static int clk_core_save_context(struct clk_core *core)
Russ Dill8b95d1c2018-09-04 12:19:35 +05301043{
1044 struct clk_core *child;
1045 int ret = 0;
1046
Stephen Boyd9be76622018-10-11 09:28:13 -07001047 hlist_for_each_entry(child, &core->children, child_node) {
1048 ret = clk_core_save_context(child);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301049 if (ret < 0)
1050 return ret;
1051 }
1052
Stephen Boyd9be76622018-10-11 09:28:13 -07001053 if (core->ops && core->ops->save_context)
1054 ret = core->ops->save_context(core->hw);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301055
1056 return ret;
1057}
1058
Stephen Boyd9be76622018-10-11 09:28:13 -07001059static void clk_core_restore_context(struct clk_core *core)
Russ Dill8b95d1c2018-09-04 12:19:35 +05301060{
1061 struct clk_core *child;
1062
Stephen Boyd9be76622018-10-11 09:28:13 -07001063 if (core->ops && core->ops->restore_context)
1064 core->ops->restore_context(core->hw);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301065
Stephen Boyd9be76622018-10-11 09:28:13 -07001066 hlist_for_each_entry(child, &core->children, child_node)
1067 clk_core_restore_context(child);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301068}
1069
1070/**
1071 * clk_save_context - save clock context for poweroff
1072 *
1073 * Saves the context of the clock register for powerstates in which the
1074 * contents of the registers will be lost. Occurs deep within the suspend
1075 * code. Returns 0 on success.
1076 */
1077int clk_save_context(void)
1078{
1079 struct clk_core *clk;
1080 int ret;
1081
1082 hlist_for_each_entry(clk, &clk_root_list, child_node) {
Stephen Boyd9be76622018-10-11 09:28:13 -07001083 ret = clk_core_save_context(clk);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301084 if (ret < 0)
1085 return ret;
1086 }
1087
1088 hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
Stephen Boyd9be76622018-10-11 09:28:13 -07001089 ret = clk_core_save_context(clk);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301090 if (ret < 0)
1091 return ret;
1092 }
1093
1094 return 0;
1095}
1096EXPORT_SYMBOL_GPL(clk_save_context);
1097
1098/**
1099 * clk_restore_context - restore clock context after poweroff
1100 *
1101 * Restore the saved clock context upon resume.
1102 *
1103 */
1104void clk_restore_context(void)
1105{
Stephen Boyd9be76622018-10-11 09:28:13 -07001106 struct clk_core *core;
Russ Dill8b95d1c2018-09-04 12:19:35 +05301107
Stephen Boyd9be76622018-10-11 09:28:13 -07001108 hlist_for_each_entry(core, &clk_root_list, child_node)
1109 clk_core_restore_context(core);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301110
Stephen Boyd9be76622018-10-11 09:28:13 -07001111 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1112 clk_core_restore_context(core);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301113}
1114EXPORT_SYMBOL_GPL(clk_restore_context);
1115
Mike Turquetteb24764902012-03-15 23:11:19 -07001116/**
1117 * clk_enable - ungate a clock
1118 * @clk: the clk being ungated
1119 *
1120 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1121 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1122 * if the operation will never sleep. One example is a SoC-internal clk which
1123 * is controlled via simple register writes. In the complex case a clk ungate
1124 * operation may require a fast and a slow part. It is this reason that
1125 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1126 * must be called before clk_enable. Returns 0 on success, -EERROR
1127 * otherwise.
1128 */
1129int clk_enable(struct clk *clk)
1130{
Dong Aisheng864e1602015-04-30 14:02:19 -07001131 if (!clk)
1132 return 0;
1133
Dong Aishenga6adc302016-06-30 17:31:11 +08001134 return clk_core_enable_lock(clk->core);
1135}
1136EXPORT_SYMBOL_GPL(clk_enable);
1137
1138static int clk_core_prepare_enable(struct clk_core *core)
1139{
1140 int ret;
1141
1142 ret = clk_core_prepare_lock(core);
1143 if (ret)
1144 return ret;
1145
1146 ret = clk_core_enable_lock(core);
1147 if (ret)
1148 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001149
1150 return ret;
1151}
Dong Aishenga6adc302016-06-30 17:31:11 +08001152
1153static void clk_core_disable_unprepare(struct clk_core *core)
1154{
1155 clk_core_disable_lock(core);
1156 clk_core_unprepare_lock(core);
1157}
Mike Turquetteb24764902012-03-15 23:11:19 -07001158
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001159static void clk_unprepare_unused_subtree(struct clk_core *core)
1160{
1161 struct clk_core *child;
1162
1163 lockdep_assert_held(&prepare_lock);
1164
1165 hlist_for_each_entry(child, &core->children, child_node)
1166 clk_unprepare_unused_subtree(child);
1167
1168 if (core->prepare_count)
1169 return;
1170
1171 if (core->flags & CLK_IGNORE_UNUSED)
1172 return;
1173
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001174 if (clk_pm_runtime_get(core))
1175 return;
1176
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001177 if (clk_core_is_prepared(core)) {
1178 trace_clk_unprepare(core);
1179 if (core->ops->unprepare_unused)
1180 core->ops->unprepare_unused(core->hw);
1181 else if (core->ops->unprepare)
1182 core->ops->unprepare(core->hw);
1183 trace_clk_unprepare_complete(core);
1184 }
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001185
1186 clk_pm_runtime_put(core);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001187}
1188
1189static void clk_disable_unused_subtree(struct clk_core *core)
1190{
1191 struct clk_core *child;
1192 unsigned long flags;
1193
1194 lockdep_assert_held(&prepare_lock);
1195
1196 hlist_for_each_entry(child, &core->children, child_node)
1197 clk_disable_unused_subtree(child);
1198
Dong Aishenga4b35182016-06-30 17:31:13 +08001199 if (core->flags & CLK_OPS_PARENT_ENABLE)
1200 clk_core_prepare_enable(core->parent);
1201
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001202 if (clk_pm_runtime_get(core))
1203 goto unprepare_out;
1204
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001205 flags = clk_enable_lock();
1206
1207 if (core->enable_count)
1208 goto unlock_out;
1209
1210 if (core->flags & CLK_IGNORE_UNUSED)
1211 goto unlock_out;
1212
1213 /*
1214 * some gate clocks have special needs during the disable-unused
1215 * sequence. call .disable_unused if available, otherwise fall
1216 * back to .disable
1217 */
1218 if (clk_core_is_enabled(core)) {
1219 trace_clk_disable(core);
1220 if (core->ops->disable_unused)
1221 core->ops->disable_unused(core->hw);
1222 else if (core->ops->disable)
1223 core->ops->disable(core->hw);
1224 trace_clk_disable_complete(core);
1225 }
1226
1227unlock_out:
1228 clk_enable_unlock(flags);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001229 clk_pm_runtime_put(core);
1230unprepare_out:
Dong Aishenga4b35182016-06-30 17:31:13 +08001231 if (core->flags & CLK_OPS_PARENT_ENABLE)
1232 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001233}
1234
1235static bool clk_ignore_unused;
1236static int __init clk_ignore_unused_setup(char *__unused)
1237{
1238 clk_ignore_unused = true;
1239 return 1;
1240}
1241__setup("clk_ignore_unused", clk_ignore_unused_setup);
1242
1243static int clk_disable_unused(void)
1244{
1245 struct clk_core *core;
1246
1247 if (clk_ignore_unused) {
1248 pr_warn("clk: Not disabling unused clocks\n");
1249 return 0;
1250 }
1251
1252 clk_prepare_lock();
1253
1254 hlist_for_each_entry(core, &clk_root_list, child_node)
1255 clk_disable_unused_subtree(core);
1256
1257 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1258 clk_disable_unused_subtree(core);
1259
1260 hlist_for_each_entry(core, &clk_root_list, child_node)
1261 clk_unprepare_unused_subtree(core);
1262
1263 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1264 clk_unprepare_unused_subtree(core);
1265
1266 clk_prepare_unlock();
1267
1268 return 0;
1269}
1270late_initcall_sync(clk_disable_unused);
1271
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001272static int clk_core_determine_round_nolock(struct clk_core *core,
1273 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001274{
Boris Brezillon0817b622015-07-07 20:48:08 +02001275 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001276
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001277 lockdep_assert_held(&prepare_lock);
1278
Stephen Boydd6968fc2015-04-30 13:54:13 -07001279 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001280 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001281
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001282 /*
1283 * At this point, core protection will be disabled if
1284 * - if the provider is not protected at all
1285 * - if the calling consumer is the only one which has exclusivity
1286 * over the provider
1287 */
Jerome Brunete55a8392017-12-01 22:51:56 +01001288 if (clk_core_rate_is_protected(core)) {
1289 req->rate = core->rate;
1290 } else if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001291 return core->ops->determine_rate(core->hw, req);
1292 } else if (core->ops->round_rate) {
1293 rate = core->ops->round_rate(core->hw, req->rate,
1294 &req->best_parent_rate);
1295 if (rate < 0)
1296 return rate;
1297
1298 req->rate = rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001299 } else {
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001300 return -EINVAL;
Boris Brezillon0817b622015-07-07 20:48:08 +02001301 }
1302
1303 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001304}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001305
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001306static void clk_core_init_rate_req(struct clk_core * const core,
1307 struct clk_rate_request *req)
1308{
1309 struct clk_core *parent;
1310
1311 if (WARN_ON(!core || !req))
1312 return;
1313
Mike Turquetteb24764902012-03-15 23:11:19 -07001314 parent = core->parent;
1315 if (parent) {
1316 req->best_parent_hw = parent->hw;
1317 req->best_parent_rate = parent->rate;
1318 } else {
1319 req->best_parent_hw = NULL;
1320 req->best_parent_rate = 0;
1321 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001322}
Mike Turquetteb24764902012-03-15 23:11:19 -07001323
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001324static bool clk_core_can_round(struct clk_core * const core)
1325{
1326 if (core->ops->determine_rate || core->ops->round_rate)
1327 return true;
Mike Turquetteb24764902012-03-15 23:11:19 -07001328
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001329 return false;
1330}
Mike Turquetteb24764902012-03-15 23:11:19 -07001331
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001332static int clk_core_round_rate_nolock(struct clk_core *core,
1333 struct clk_rate_request *req)
1334{
1335 lockdep_assert_held(&prepare_lock);
1336
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001337 if (!core) {
1338 req->rate = 0;
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001339 return 0;
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001340 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001341
1342 clk_core_init_rate_req(core, req);
1343
1344 if (clk_core_can_round(core))
1345 return clk_core_determine_round_nolock(core, req);
1346 else if (core->flags & CLK_SET_RATE_PARENT)
1347 return clk_core_round_rate_nolock(core->parent, req);
1348
1349 req->rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001350 return 0;
1351}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001352
1353/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001354 * __clk_determine_rate - get the closest rate actually supported by a clock
1355 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001356 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001357 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001358 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001359 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001360int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001361{
Boris Brezillon0817b622015-07-07 20:48:08 +02001362 if (!hw) {
1363 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001364 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001365 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001366
Boris Brezillon0817b622015-07-07 20:48:08 +02001367 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001368}
1369EXPORT_SYMBOL_GPL(__clk_determine_rate);
1370
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001371unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1372{
1373 int ret;
1374 struct clk_rate_request req;
1375
1376 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1377 req.rate = rate;
1378
1379 ret = clk_core_round_rate_nolock(hw->core, &req);
1380 if (ret)
1381 return 0;
1382
1383 return req.rate;
1384}
1385EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1386
Mike Turquetteb24764902012-03-15 23:11:19 -07001387/**
1388 * clk_round_rate - round the given rate for a clk
1389 * @clk: the clk for which we are rounding a rate
1390 * @rate: the rate which is to be rounded
1391 *
1392 * Takes in a rate as input and rounds it to a rate that the clk can actually
1393 * use which is then returned. If clk doesn't support round_rate operation
1394 * then the parent rate is returned.
1395 */
1396long clk_round_rate(struct clk *clk, unsigned long rate)
1397{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001398 struct clk_rate_request req;
1399 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001400
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001401 if (!clk)
1402 return 0;
1403
Mike Turquetteeab89f62013-03-28 13:59:01 -07001404 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001405
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001406 if (clk->exclusive_count)
1407 clk_core_rate_unprotect(clk->core);
1408
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001409 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1410 req.rate = rate;
1411
1412 ret = clk_core_round_rate_nolock(clk->core, &req);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001413
1414 if (clk->exclusive_count)
1415 clk_core_rate_protect(clk->core);
1416
Mike Turquetteeab89f62013-03-28 13:59:01 -07001417 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001418
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001419 if (ret)
1420 return ret;
1421
1422 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001423}
1424EXPORT_SYMBOL_GPL(clk_round_rate);
1425
1426/**
1427 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001428 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001429 * @msg: clk notifier type (see include/linux/clk.h)
1430 * @old_rate: old clk rate
1431 * @new_rate: new clk rate
1432 *
1433 * Triggers a notifier call chain on the clk rate-change notification
1434 * for 'clk'. Passes a pointer to the struct clk and the previous
1435 * and current rates to the notifier callback. Intended to be called by
1436 * internal clock code only. Returns NOTIFY_DONE from the last driver
1437 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1438 * a driver returns that.
1439 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001440static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001441 unsigned long old_rate, unsigned long new_rate)
1442{
1443 struct clk_notifier *cn;
1444 struct clk_notifier_data cnd;
1445 int ret = NOTIFY_DONE;
1446
Mike Turquetteb24764902012-03-15 23:11:19 -07001447 cnd.old_rate = old_rate;
1448 cnd.new_rate = new_rate;
1449
1450 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001451 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001452 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001453 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1454 &cnd);
Peter De Schrijver17c34c52017-03-21 12:16:26 +02001455 if (ret & NOTIFY_STOP_MASK)
1456 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001457 }
1458 }
1459
1460 return ret;
1461}
1462
1463/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001464 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001465 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001466 *
1467 * Walks the subtree of clks starting with clk and recalculates accuracies as
1468 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001469 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001470 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001471 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001472static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001473{
1474 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001475 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001476
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001477 lockdep_assert_held(&prepare_lock);
1478
Stephen Boydd6968fc2015-04-30 13:54:13 -07001479 if (core->parent)
1480 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001481
Stephen Boydd6968fc2015-04-30 13:54:13 -07001482 if (core->ops->recalc_accuracy)
1483 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001484 parent_accuracy);
1485 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001486 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001487
Stephen Boydd6968fc2015-04-30 13:54:13 -07001488 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001489 __clk_recalc_accuracies(child);
1490}
1491
Stephen Boydd6968fc2015-04-30 13:54:13 -07001492static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001493{
1494 unsigned long accuracy;
1495
1496 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001497 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1498 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001499
Stephen Boydd6968fc2015-04-30 13:54:13 -07001500 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001501 clk_prepare_unlock();
1502
1503 return accuracy;
1504}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001505
1506/**
1507 * clk_get_accuracy - return the accuracy of clk
1508 * @clk: the clk whose accuracy is being returned
1509 *
1510 * Simply returns the cached accuracy of the clk, unless
1511 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1512 * issued.
1513 * If clk is NULL then returns 0.
1514 */
1515long clk_get_accuracy(struct clk *clk)
1516{
1517 if (!clk)
1518 return 0;
1519
1520 return clk_core_get_accuracy(clk->core);
1521}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001522EXPORT_SYMBOL_GPL(clk_get_accuracy);
1523
Stephen Boydd6968fc2015-04-30 13:54:13 -07001524static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001525 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001526{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001527 unsigned long rate = parent_rate;
1528
1529 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1530 rate = core->ops->recalc_rate(core->hw, parent_rate);
1531 clk_pm_runtime_put(core);
1532 }
1533 return rate;
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001534}
1535
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001536/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001537 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001538 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001539 * @msg: notification type (see include/linux/clk.h)
1540 *
1541 * Walks the subtree of clks starting with clk and recalculates rates as it
1542 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001543 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001544 *
1545 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1546 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001547 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001548static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001549{
1550 unsigned long old_rate;
1551 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001552 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001553
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001554 lockdep_assert_held(&prepare_lock);
1555
Stephen Boydd6968fc2015-04-30 13:54:13 -07001556 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001557
Stephen Boydd6968fc2015-04-30 13:54:13 -07001558 if (core->parent)
1559 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001560
Stephen Boydd6968fc2015-04-30 13:54:13 -07001561 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001562
1563 /*
1564 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1565 * & ABORT_RATE_CHANGE notifiers
1566 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001567 if (core->notifier_count && msg)
1568 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001569
Stephen Boydd6968fc2015-04-30 13:54:13 -07001570 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001571 __clk_recalc_rates(child, msg);
1572}
1573
Stephen Boydd6968fc2015-04-30 13:54:13 -07001574static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001575{
1576 unsigned long rate;
1577
1578 clk_prepare_lock();
1579
Stephen Boydd6968fc2015-04-30 13:54:13 -07001580 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1581 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001582
Stephen Boydd6968fc2015-04-30 13:54:13 -07001583 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001584 clk_prepare_unlock();
1585
1586 return rate;
1587}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001588
Mike Turquetteb24764902012-03-15 23:11:19 -07001589/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001590 * clk_get_rate - return the rate of clk
1591 * @clk: the clk whose rate is being returned
1592 *
1593 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1594 * is set, which means a recalc_rate will be issued.
1595 * If clk is NULL then returns 0.
1596 */
1597unsigned long clk_get_rate(struct clk *clk)
1598{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001599 if (!clk)
1600 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001601
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001602 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001603}
1604EXPORT_SYMBOL_GPL(clk_get_rate);
1605
Stephen Boydd6968fc2015-04-30 13:54:13 -07001606static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001607 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001608{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001609 int i;
James Hogan4935b222013-07-29 12:24:59 +01001610
Masahiro Yamada508f8842015-12-28 19:23:08 +09001611 if (!parent)
1612 return -EINVAL;
1613
Derek Basehoreede77852018-12-20 16:31:00 -08001614 for (i = 0; i < core->num_parents; i++) {
Stephen Boydfc0c2092019-04-12 11:31:47 -07001615 if (core->parents[i].core == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001616 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001617
Stephen Boydfc0c2092019-04-12 11:31:47 -07001618 if (core->parents[i].core)
Derek Basehoreede77852018-12-20 16:31:00 -08001619 continue;
1620
1621 /* Fallback to comparing globally unique names */
Stephen Boydfc0c2092019-04-12 11:31:47 -07001622 if (!strcmp(parent->name, core->parents[i].name)) {
1623 core->parents[i].core = parent;
Derek Basehoreede77852018-12-20 16:31:00 -08001624 return i;
1625 }
1626 }
1627
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001628 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001629}
1630
Heiko Stuebnere6500342015-04-22 22:53:05 +02001631/*
1632 * Update the orphan status of @core and all its children.
1633 */
1634static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1635{
1636 struct clk_core *child;
1637
1638 core->orphan = is_orphan;
1639
1640 hlist_for_each_entry(child, &core->children, child_node)
1641 clk_core_update_orphan_status(child, is_orphan);
1642}
1643
Stephen Boydd6968fc2015-04-30 13:54:13 -07001644static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001645{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001646 bool was_orphan = core->orphan;
1647
Stephen Boydd6968fc2015-04-30 13:54:13 -07001648 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001649
James Hogan903efc52013-08-29 12:10:51 +01001650 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001651 bool becomes_orphan = new_parent->orphan;
1652
James Hogan903efc52013-08-29 12:10:51 +01001653 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001654 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001655 new_parent->new_child = NULL;
1656
Stephen Boydd6968fc2015-04-30 13:54:13 -07001657 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001658
1659 if (was_orphan != becomes_orphan)
1660 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001661 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001662 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001663 if (!was_orphan)
1664 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001665 }
James Hogan4935b222013-07-29 12:24:59 +01001666
Stephen Boydd6968fc2015-04-30 13:54:13 -07001667 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001668}
1669
Stephen Boydd6968fc2015-04-30 13:54:13 -07001670static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001671 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001672{
1673 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001674 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001675
1676 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001677 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1678 *
1679 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001680 * clk_enable().
1681 *
1682 * If the clock is not prepared, then a race with
1683 * clk_enable/disable() is impossible since we already have the
1684 * prepare lock (future calls to clk_enable() need to be preceded by
1685 * a clk_prepare()).
1686 *
1687 * If the clock is prepared, migrate the prepared state to the new
1688 * parent and also protect against a race with clk_enable() by
1689 * forcing the clock and the new parent on. This ensures that all
1690 * future calls to clk_enable() are practically NOPs with respect to
1691 * hardware and software states.
1692 *
1693 * See also: Comment for clk_set_parent() below.
1694 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001695
1696 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1697 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1698 clk_core_prepare_enable(old_parent);
1699 clk_core_prepare_enable(parent);
1700 }
1701
1702 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001703 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001704 clk_core_prepare_enable(parent);
1705 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001706 }
1707
1708 /* update the clk tree topology */
1709 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001710 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001711 clk_enable_unlock(flags);
1712
Stephen Boyd3fa22522014-01-15 10:47:22 -08001713 return old_parent;
1714}
1715
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001716static void __clk_set_parent_after(struct clk_core *core,
1717 struct clk_core *parent,
1718 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001719{
1720 /*
1721 * Finish the migration of prepare state and undo the changes done
1722 * for preventing a race with clk_enable().
1723 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001724 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001725 clk_core_disable_lock(core);
1726 clk_core_disable_unprepare(old_parent);
1727 }
1728
1729 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1730 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1731 clk_core_disable_unprepare(parent);
1732 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001733 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001734}
1735
Stephen Boydd6968fc2015-04-30 13:54:13 -07001736static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001737 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001738{
1739 unsigned long flags;
1740 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001741 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001742
Stephen Boydd6968fc2015-04-30 13:54:13 -07001743 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001744
Stephen Boydd6968fc2015-04-30 13:54:13 -07001745 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001746
James Hogan4935b222013-07-29 12:24:59 +01001747 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001748 if (parent && core->ops->set_parent)
1749 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001750
Stephen Boydd6968fc2015-04-30 13:54:13 -07001751 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001752
James Hogan4935b222013-07-29 12:24:59 +01001753 if (ret) {
1754 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001755 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001756 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001757 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001758
James Hogan4935b222013-07-29 12:24:59 +01001759 return ret;
1760 }
1761
Stephen Boydd6968fc2015-04-30 13:54:13 -07001762 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001763
James Hogan4935b222013-07-29 12:24:59 +01001764 return 0;
1765}
1766
Ulf Hanssona093bde2012-08-31 14:21:28 +02001767/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001768 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001769 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001770 * @parent_rate: the "future" rate of clk's parent
1771 *
1772 * Walks the subtree of clks starting with clk, speculating rates as it
1773 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1774 *
1775 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1776 * pre-rate change notifications and returns early if no clks in the
1777 * subtree have subscribed to the notifications. Note that if a clk does not
1778 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001779 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001780 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001781static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001782 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001783{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001784 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001785 unsigned long new_rate;
1786 int ret = NOTIFY_DONE;
1787
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001788 lockdep_assert_held(&prepare_lock);
1789
Stephen Boydd6968fc2015-04-30 13:54:13 -07001790 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001791
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001792 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001793 if (core->notifier_count)
1794 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001795
Mike Turquette86bcfa22014-02-24 16:08:41 -08001796 if (ret & NOTIFY_STOP_MASK) {
1797 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001798 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001799 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001800 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001801
Stephen Boydd6968fc2015-04-30 13:54:13 -07001802 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001803 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001804 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001805 break;
1806 }
1807
1808out:
1809 return ret;
1810}
1811
Stephen Boydd6968fc2015-04-30 13:54:13 -07001812static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001813 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001814{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001815 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001816
Stephen Boydd6968fc2015-04-30 13:54:13 -07001817 core->new_rate = new_rate;
1818 core->new_parent = new_parent;
1819 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001820 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001821 core->new_child = NULL;
1822 if (new_parent && new_parent != core->parent)
1823 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001824
Stephen Boydd6968fc2015-04-30 13:54:13 -07001825 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001826 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001827 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001828 }
1829}
1830
1831/*
1832 * calculate the new rates returning the topmost clock that has to be
1833 * changed.
1834 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001835static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001836 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001837{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001838 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001839 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001840 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001841 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001842 unsigned long min_rate;
1843 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001844 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001845 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001846
Mike Turquette7452b212012-03-26 14:45:36 -07001847 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001848 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001849 return NULL;
1850
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001851 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001852 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001853 if (parent)
1854 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001855
Stephen Boydd6968fc2015-04-30 13:54:13 -07001856 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001857
James Hogan71472c02013-07-29 12:25:00 +01001858 /* find the closest rate and parent clk/rate */
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001859 if (clk_core_can_round(core)) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001860 struct clk_rate_request req;
1861
1862 req.rate = rate;
1863 req.min_rate = min_rate;
1864 req.max_rate = max_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001865
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001866 clk_core_init_rate_req(core, &req);
1867
1868 ret = clk_core_determine_round_nolock(core, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001869 if (ret < 0)
1870 return NULL;
1871
Boris Brezillon0817b622015-07-07 20:48:08 +02001872 best_parent_rate = req.best_parent_rate;
1873 new_rate = req.rate;
1874 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001875
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001876 if (new_rate < min_rate || new_rate > max_rate)
1877 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001878 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001879 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001880 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001881 return NULL;
1882 } else {
1883 /* pass-through clock with adjustable parent */
1884 top = clk_calc_new_rates(parent, rate);
1885 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001886 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001887 }
1888
James Hogan71472c02013-07-29 12:25:00 +01001889 /* some clocks must be gated to change parent */
1890 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001891 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001892 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001893 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001894 return NULL;
1895 }
1896
James Hogan71472c02013-07-29 12:25:00 +01001897 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001898 if (parent && core->num_parents > 1) {
1899 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001900 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001901 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001902 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001903 return NULL;
1904 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001905 }
1906
Stephen Boydd6968fc2015-04-30 13:54:13 -07001907 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001908 best_parent_rate != parent->rate)
1909 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001910
1911out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001912 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001913
1914 return top;
1915}
1916
1917/*
1918 * Notify about rate changes in a subtree. Always walk down the whole tree
1919 * so that in case of an error we can walk down the whole tree again and
1920 * abort the change.
1921 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001922static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001923 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001924{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001925 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001926 int ret = NOTIFY_DONE;
1927
Stephen Boydd6968fc2015-04-30 13:54:13 -07001928 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301929 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001930
Stephen Boydd6968fc2015-04-30 13:54:13 -07001931 if (core->notifier_count) {
1932 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001933 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001934 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001935 }
1936
Stephen Boydd6968fc2015-04-30 13:54:13 -07001937 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001938 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001939 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001940 continue;
1941 tmp_clk = clk_propagate_rate_change(child, event);
1942 if (tmp_clk)
1943 fail_clk = tmp_clk;
1944 }
1945
Stephen Boydd6968fc2015-04-30 13:54:13 -07001946 /* handle the new child who might not be in core->children yet */
1947 if (core->new_child) {
1948 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001949 if (tmp_clk)
1950 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001951 }
1952
1953 return fail_clk;
1954}
1955
1956/*
1957 * walk down a subtree and set the new rates notifying the rate
1958 * change on the way
1959 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001960static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001961{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001962 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001963 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001964 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001965 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001966 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001967 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001968 struct clk_core *parent = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001969
Stephen Boydd6968fc2015-04-30 13:54:13 -07001970 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001971
Dong Aishengfc8726a2016-06-30 17:31:14 +08001972 if (core->new_parent) {
1973 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001974 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001975 } else if (core->parent) {
1976 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001977 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001978 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001979
Marek Szyprowski588fb542017-11-30 13:14:51 +01001980 if (clk_pm_runtime_get(core))
1981 return;
1982
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001983 if (core->flags & CLK_SET_RATE_UNGATE) {
1984 unsigned long flags;
1985
1986 clk_core_prepare(core);
1987 flags = clk_enable_lock();
1988 clk_core_enable(core);
1989 clk_enable_unlock(flags);
1990 }
1991
Stephen Boydd6968fc2015-04-30 13:54:13 -07001992 if (core->new_parent && core->new_parent != core->parent) {
1993 old_parent = __clk_set_parent_before(core, core->new_parent);
1994 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001995
Stephen Boydd6968fc2015-04-30 13:54:13 -07001996 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001997 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001998 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001999 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07002000 core->new_parent_index);
2001 } else if (core->ops->set_parent) {
2002 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002003 }
2004
Stephen Boydd6968fc2015-04-30 13:54:13 -07002005 trace_clk_set_parent_complete(core, core->new_parent);
2006 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002007 }
2008
Dong Aishengfc8726a2016-06-30 17:31:14 +08002009 if (core->flags & CLK_OPS_PARENT_ENABLE)
2010 clk_core_prepare_enable(parent);
2011
Stephen Boydd6968fc2015-04-30 13:54:13 -07002012 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002013
Stephen Boydd6968fc2015-04-30 13:54:13 -07002014 if (!skip_set_rate && core->ops->set_rate)
2015 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002016
Stephen Boydd6968fc2015-04-30 13:54:13 -07002017 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002018
Stephen Boydd6968fc2015-04-30 13:54:13 -07002019 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002020
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01002021 if (core->flags & CLK_SET_RATE_UNGATE) {
2022 unsigned long flags;
2023
2024 flags = clk_enable_lock();
2025 clk_core_disable(core);
2026 clk_enable_unlock(flags);
2027 clk_core_unprepare(core);
2028 }
2029
Dong Aishengfc8726a2016-06-30 17:31:14 +08002030 if (core->flags & CLK_OPS_PARENT_ENABLE)
2031 clk_core_disable_unprepare(parent);
2032
Stephen Boydd6968fc2015-04-30 13:54:13 -07002033 if (core->notifier_count && old_rate != core->rate)
2034 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002035
Michael Turquette85e88fa2015-06-20 12:18:03 -07002036 if (core->flags & CLK_RECALC_NEW_RATES)
2037 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02002038
Tero Kristo067bb172014-08-21 16:47:45 +03002039 /*
2040 * Use safe iteration, as change_rate can actually swap parents
2041 * for certain clock types.
2042 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002043 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01002044 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002045 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01002046 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07002047 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01002048 }
2049
Stephen Boydd6968fc2015-04-30 13:54:13 -07002050 /* handle the new child who might not be in core->children yet */
2051 if (core->new_child)
2052 clk_change_rate(core->new_child);
Marek Szyprowski588fb542017-11-30 13:14:51 +01002053
2054 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002055}
2056
Jerome Brunetca5e0892017-12-01 22:51:55 +01002057static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
2058 unsigned long req_rate)
2059{
Jerome Brunete55a8392017-12-01 22:51:56 +01002060 int ret, cnt;
Jerome Brunetca5e0892017-12-01 22:51:55 +01002061 struct clk_rate_request req;
2062
2063 lockdep_assert_held(&prepare_lock);
2064
2065 if (!core)
2066 return 0;
2067
Jerome Brunete55a8392017-12-01 22:51:56 +01002068 /* simulate what the rate would be if it could be freely set */
2069 cnt = clk_core_rate_nuke_protect(core);
2070 if (cnt < 0)
2071 return cnt;
2072
Jerome Brunetca5e0892017-12-01 22:51:55 +01002073 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
2074 req.rate = req_rate;
2075
2076 ret = clk_core_round_rate_nolock(core, &req);
2077
Jerome Brunete55a8392017-12-01 22:51:56 +01002078 /* restore the protection */
2079 clk_core_rate_restore_protect(core, cnt);
2080
Jerome Brunetca5e0892017-12-01 22:51:55 +01002081 return ret ? 0 : req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002082}
2083
Stephen Boydd6968fc2015-04-30 13:54:13 -07002084static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002085 unsigned long req_rate)
2086{
2087 struct clk_core *top, *fail_clk;
Jerome Brunetca5e0892017-12-01 22:51:55 +01002088 unsigned long rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002089 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002090
Stephen Boydd6968fc2015-04-30 13:54:13 -07002091 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002092 return 0;
2093
Jerome Brunetca5e0892017-12-01 22:51:55 +01002094 rate = clk_core_req_round_rate_nolock(core, req_rate);
2095
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002096 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002097 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002098 return 0;
2099
Jerome Brunete55a8392017-12-01 22:51:56 +01002100 /* fail on a direct rate set of a protected provider */
2101 if (clk_core_rate_is_protected(core))
2102 return -EBUSY;
2103
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002104 /* calculate new rates and get the topmost changed clock */
Jerome Brunetca5e0892017-12-01 22:51:55 +01002105 top = clk_calc_new_rates(core, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002106 if (!top)
2107 return -EINVAL;
2108
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002109 ret = clk_pm_runtime_get(core);
2110 if (ret)
2111 return ret;
2112
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002113 /* notify that we are about to change rates */
2114 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
2115 if (fail_clk) {
2116 pr_debug("%s: failed to set %s rate\n", __func__,
2117 fail_clk->name);
2118 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002119 ret = -EBUSY;
2120 goto err;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002121 }
2122
2123 /* change the rates */
2124 clk_change_rate(top);
2125
Stephen Boydd6968fc2015-04-30 13:54:13 -07002126 core->req_rate = req_rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002127err:
2128 clk_pm_runtime_put(core);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002129
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002130 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002131}
2132
Mike Turquetteb24764902012-03-15 23:11:19 -07002133/**
2134 * clk_set_rate - specify a new rate for clk
2135 * @clk: the clk whose rate is being changed
2136 * @rate: the new rate for clk
2137 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002138 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07002139 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002140 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2141 * propagate up to clk's parent; whether or not this happens depends on the
2142 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2143 * after calling .round_rate then upstream parent propagation is ignored. If
2144 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002145 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07002146 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2147 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07002148 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002149 * Rate changes are accomplished via tree traversal that also recalculates the
2150 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07002151 *
2152 * Returns 0 on success, -EERROR otherwise.
2153 */
2154int clk_set_rate(struct clk *clk, unsigned long rate)
2155{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002156 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002157
Mike Turquette89ac8d72013-08-21 23:58:09 -07002158 if (!clk)
2159 return 0;
2160
Mike Turquetteb24764902012-03-15 23:11:19 -07002161 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002162 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002163
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002164 if (clk->exclusive_count)
2165 clk_core_rate_unprotect(clk->core);
2166
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002167 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002168
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002169 if (clk->exclusive_count)
2170 clk_core_rate_protect(clk->core);
2171
Mike Turquetteeab89f62013-03-28 13:59:01 -07002172 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002173
2174 return ret;
2175}
2176EXPORT_SYMBOL_GPL(clk_set_rate);
2177
2178/**
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002179 * clk_set_rate_exclusive - specify a new rate get exclusive control
2180 * @clk: the clk whose rate is being changed
2181 * @rate: the new rate for clk
2182 *
2183 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2184 * within a critical section
2185 *
2186 * This can be used initially to ensure that at least 1 consumer is
2187 * statisfied when several consumers are competing for exclusivity over the
2188 * same clock provider.
2189 *
2190 * The exclusivity is not applied if setting the rate failed.
2191 *
2192 * Calls to clk_rate_exclusive_get() should be balanced with calls to
2193 * clk_rate_exclusive_put().
2194 *
2195 * Returns 0 on success, -EERROR otherwise.
2196 */
2197int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2198{
2199 int ret;
2200
2201 if (!clk)
2202 return 0;
2203
2204 /* prevent racing with updates to the clock topology */
2205 clk_prepare_lock();
2206
2207 /*
2208 * The temporary protection removal is not here, on purpose
2209 * This function is meant to be used instead of clk_rate_protect,
2210 * so before the consumer code path protect the clock provider
2211 */
2212
2213 ret = clk_core_set_rate_nolock(clk->core, rate);
2214 if (!ret) {
2215 clk_core_rate_protect(clk->core);
2216 clk->exclusive_count++;
2217 }
2218
2219 clk_prepare_unlock();
2220
2221 return ret;
2222}
2223EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2224
2225/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002226 * clk_set_rate_range - set a rate range for a clock source
2227 * @clk: clock source
2228 * @min: desired minimum clock rate in Hz, inclusive
2229 * @max: desired maximum clock rate in Hz, inclusive
2230 *
2231 * Returns success (0) or negative errno.
2232 */
2233int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2234{
2235 int ret = 0;
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002236 unsigned long old_min, old_max, rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002237
2238 if (!clk)
2239 return 0;
2240
2241 if (min > max) {
2242 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2243 __func__, clk->core->name, clk->dev_id, clk->con_id,
2244 min, max);
2245 return -EINVAL;
2246 }
2247
2248 clk_prepare_lock();
2249
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002250 if (clk->exclusive_count)
2251 clk_core_rate_unprotect(clk->core);
2252
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002253 /* Save the current values in case we need to rollback the change */
2254 old_min = clk->min_rate;
2255 old_max = clk->max_rate;
2256 clk->min_rate = min;
2257 clk->max_rate = max;
2258
2259 rate = clk_core_get_rate_nolock(clk->core);
2260 if (rate < min || rate > max) {
2261 /*
2262 * FIXME:
2263 * We are in bit of trouble here, current rate is outside the
2264 * the requested range. We are going try to request appropriate
2265 * range boundary but there is a catch. It may fail for the
2266 * usual reason (clock broken, clock protected, etc) but also
2267 * because:
2268 * - round_rate() was not favorable and fell on the wrong
2269 * side of the boundary
2270 * - the determine_rate() callback does not really check for
2271 * this corner case when determining the rate
2272 */
2273
2274 if (rate < min)
2275 rate = min;
2276 else
2277 rate = max;
2278
2279 ret = clk_core_set_rate_nolock(clk->core, rate);
2280 if (ret) {
2281 /* rollback the changes */
2282 clk->min_rate = old_min;
2283 clk->max_rate = old_max;
2284 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002285 }
2286
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002287 if (clk->exclusive_count)
2288 clk_core_rate_protect(clk->core);
2289
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002290 clk_prepare_unlock();
2291
2292 return ret;
2293}
2294EXPORT_SYMBOL_GPL(clk_set_rate_range);
2295
2296/**
2297 * clk_set_min_rate - set a minimum clock rate for a clock source
2298 * @clk: clock source
2299 * @rate: desired minimum clock rate in Hz, inclusive
2300 *
2301 * Returns success (0) or negative errno.
2302 */
2303int clk_set_min_rate(struct clk *clk, unsigned long rate)
2304{
2305 if (!clk)
2306 return 0;
2307
2308 return clk_set_rate_range(clk, rate, clk->max_rate);
2309}
2310EXPORT_SYMBOL_GPL(clk_set_min_rate);
2311
2312/**
2313 * clk_set_max_rate - set a maximum clock rate for a clock source
2314 * @clk: clock source
2315 * @rate: desired maximum clock rate in Hz, inclusive
2316 *
2317 * Returns success (0) or negative errno.
2318 */
2319int clk_set_max_rate(struct clk *clk, unsigned long rate)
2320{
2321 if (!clk)
2322 return 0;
2323
2324 return clk_set_rate_range(clk, clk->min_rate, rate);
2325}
2326EXPORT_SYMBOL_GPL(clk_set_max_rate);
2327
2328/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002329 * clk_get_parent - return the parent of a clk
2330 * @clk: the clk whose parent gets returned
2331 *
2332 * Simply returns clk->parent. Returns NULL if clk is NULL.
2333 */
2334struct clk *clk_get_parent(struct clk *clk)
2335{
2336 struct clk *parent;
2337
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002338 if (!clk)
2339 return NULL;
2340
Mike Turquetteeab89f62013-03-28 13:59:01 -07002341 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002342 /* TODO: Create a per-user clk and change callers to call clk_put */
2343 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002344 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002345
2346 return parent;
2347}
2348EXPORT_SYMBOL_GPL(clk_get_parent);
2349
Stephen Boydd6968fc2015-04-30 13:54:13 -07002350static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002351{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002352 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002353
Masahiro Yamada2430a942016-02-09 20:19:14 +09002354 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002355 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002356
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002357 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002358}
2359
Stephen Boydd6968fc2015-04-30 13:54:13 -07002360static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002361 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002362{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002363 clk_reparent(core, new_parent);
2364 __clk_recalc_accuracies(core);
2365 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002366}
2367
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002368void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2369{
2370 if (!hw)
2371 return;
2372
2373 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2374}
2375
Mike Turquetteb24764902012-03-15 23:11:19 -07002376/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002377 * clk_has_parent - check if a clock is a possible parent for another
2378 * @clk: clock source
2379 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002380 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002381 * This function can be used in drivers that need to check that a clock can be
2382 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002383 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002384 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002385 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002386bool clk_has_parent(struct clk *clk, struct clk *parent)
2387{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002388 struct clk_core *core, *parent_core;
Stephen Boydfc0c2092019-04-12 11:31:47 -07002389 int i;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002390
2391 /* NULL clocks should be nops, so return success if either is NULL. */
2392 if (!clk || !parent)
2393 return true;
2394
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002395 core = clk->core;
2396 parent_core = parent->core;
2397
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002398 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002399 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002400 return true;
2401
Stephen Boydfc0c2092019-04-12 11:31:47 -07002402 for (i = 0; i < core->num_parents; i++)
2403 if (!strcmp(core->parents[i].name, parent_core->name))
2404 return true;
2405
2406 return false;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002407}
2408EXPORT_SYMBOL_GPL(clk_has_parent);
2409
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002410static int clk_core_set_parent_nolock(struct clk_core *core,
2411 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002412{
2413 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002414 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002415 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002416
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002417 lockdep_assert_held(&prepare_lock);
2418
Stephen Boydd6968fc2015-04-30 13:54:13 -07002419 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002420 return 0;
2421
Stephen Boydd6968fc2015-04-30 13:54:13 -07002422 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002423 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002424
Stephen Boydb61c43c2015-02-02 14:11:25 -08002425 /* verify ops for for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002426 if (core->num_parents > 1 && !core->ops->set_parent)
2427 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08002428
Ulf Hansson031dcc92013-04-02 23:09:38 +02002429 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002430 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2431 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002432
Jerome Brunete55a8392017-12-01 22:51:56 +01002433 if (clk_core_rate_is_protected(core))
2434 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002435
2436 /* try finding the new parent index */
2437 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002438 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002439 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002440 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002441 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002442 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002443 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002444 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002445 }
2446
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002447 ret = clk_pm_runtime_get(core);
2448 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002449 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002450
Mike Turquetteb24764902012-03-15 23:11:19 -07002451 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002452 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002453
2454 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002455 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002456 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07002457
Ulf Hansson031dcc92013-04-02 23:09:38 +02002458 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002459 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002460
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002461 /* propagate rate an accuracy recalculation accordingly */
2462 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002463 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002464 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002465 __clk_recalc_rates(core, POST_RATE_CHANGE);
2466 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002467 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002468
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002469runtime_put:
2470 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002471
2472 return ret;
2473}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002474
2475/**
2476 * clk_set_parent - switch the parent of a mux clk
2477 * @clk: the mux clk whose input we are switching
2478 * @parent: the new input to clk
2479 *
2480 * Re-parent clk to use parent as its new input source. If clk is in
2481 * prepared state, the clk will get enabled for the duration of this call. If
2482 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2483 * that, the reparenting is glitchy in hardware, etc), use the
2484 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2485 *
2486 * After successfully changing clk's parent clk_set_parent will update the
2487 * clk topology, sysfs topology and propagate rate recalculation via
2488 * __clk_recalc_rates.
2489 *
2490 * Returns 0 on success, -EERROR otherwise.
2491 */
2492int clk_set_parent(struct clk *clk, struct clk *parent)
2493{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002494 int ret;
2495
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002496 if (!clk)
2497 return 0;
2498
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002499 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002500
2501 if (clk->exclusive_count)
2502 clk_core_rate_unprotect(clk->core);
2503
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002504 ret = clk_core_set_parent_nolock(clk->core,
2505 parent ? parent->core : NULL);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002506
2507 if (clk->exclusive_count)
2508 clk_core_rate_protect(clk->core);
2509
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002510 clk_prepare_unlock();
2511
2512 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002513}
Mike Turquetteb24764902012-03-15 23:11:19 -07002514EXPORT_SYMBOL_GPL(clk_set_parent);
2515
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002516static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2517{
2518 int ret = -EINVAL;
2519
2520 lockdep_assert_held(&prepare_lock);
2521
2522 if (!core)
2523 return 0;
2524
Jerome Brunete55a8392017-12-01 22:51:56 +01002525 if (clk_core_rate_is_protected(core))
2526 return -EBUSY;
2527
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002528 trace_clk_set_phase(core, degrees);
2529
Shawn Lin7f95bee2018-03-08 14:49:41 +08002530 if (core->ops->set_phase) {
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002531 ret = core->ops->set_phase(core->hw, degrees);
Shawn Lin7f95bee2018-03-08 14:49:41 +08002532 if (!ret)
2533 core->phase = degrees;
2534 }
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002535
2536 trace_clk_set_phase_complete(core, degrees);
2537
2538 return ret;
2539}
2540
Mike Turquetteb24764902012-03-15 23:11:19 -07002541/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002542 * clk_set_phase - adjust the phase shift of a clock signal
2543 * @clk: clock signal source
2544 * @degrees: number of degrees the signal is shifted
2545 *
2546 * Shifts the phase of a clock signal by the specified
2547 * degrees. Returns 0 on success, -EERROR otherwise.
2548 *
2549 * This function makes no distinction about the input or reference
2550 * signal that we adjust the clock signal phase against. For example
2551 * phase locked-loop clock signal generators we may shift phase with
2552 * respect to feedback clock signal input, but for other cases the
2553 * clock phase may be shifted with respect to some other, unspecified
2554 * signal.
2555 *
2556 * Additionally the concept of phase shift does not propagate through
2557 * the clock tree hierarchy, which sets it apart from clock rates and
2558 * clock accuracy. A parent clock phase attribute does not have an
2559 * impact on the phase attribute of a child clock.
2560 */
2561int clk_set_phase(struct clk *clk, int degrees)
2562{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002563 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002564
2565 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002566 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002567
2568 /* sanity check degrees */
2569 degrees %= 360;
2570 if (degrees < 0)
2571 degrees += 360;
2572
2573 clk_prepare_lock();
2574
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002575 if (clk->exclusive_count)
2576 clk_core_rate_unprotect(clk->core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002577
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002578 ret = clk_core_set_phase_nolock(clk->core, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002579
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002580 if (clk->exclusive_count)
2581 clk_core_rate_protect(clk->core);
Mike Turquettee59c5372014-02-18 21:21:25 -08002582
Mike Turquettee59c5372014-02-18 21:21:25 -08002583 clk_prepare_unlock();
2584
Mike Turquettee59c5372014-02-18 21:21:25 -08002585 return ret;
2586}
Maxime Ripard9767b04f2015-01-20 22:23:43 +01002587EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002588
Stephen Boydd6968fc2015-04-30 13:54:13 -07002589static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002590{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002591 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002592
2593 clk_prepare_lock();
Shawn Lin1f9c63e2018-03-14 08:28:31 +08002594 /* Always try to update cached phase if possible */
2595 if (core->ops->get_phase)
2596 core->phase = core->ops->get_phase(core->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002597 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002598 clk_prepare_unlock();
2599
Mike Turquettee59c5372014-02-18 21:21:25 -08002600 return ret;
2601}
2602
2603/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002604 * clk_get_phase - return the phase shift of a clock signal
2605 * @clk: clock signal source
2606 *
2607 * Returns the phase shift of a clock node in degrees, otherwise returns
2608 * -EERROR.
2609 */
2610int clk_get_phase(struct clk *clk)
2611{
2612 if (!clk)
2613 return 0;
2614
2615 return clk_core_get_phase(clk->core);
2616}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002617EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002618
Jerome Brunet9fba7382018-06-19 16:41:41 +02002619static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2620{
2621 /* Assume a default value of 50% */
2622 core->duty.num = 1;
2623 core->duty.den = 2;
2624}
2625
2626static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2627
2628static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2629{
2630 struct clk_duty *duty = &core->duty;
2631 int ret = 0;
2632
2633 if (!core->ops->get_duty_cycle)
2634 return clk_core_update_duty_cycle_parent_nolock(core);
2635
2636 ret = core->ops->get_duty_cycle(core->hw, duty);
2637 if (ret)
2638 goto reset;
2639
2640 /* Don't trust the clock provider too much */
2641 if (duty->den == 0 || duty->num > duty->den) {
2642 ret = -EINVAL;
2643 goto reset;
2644 }
2645
2646 return 0;
2647
2648reset:
2649 clk_core_reset_duty_cycle_nolock(core);
2650 return ret;
2651}
2652
2653static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2654{
2655 int ret = 0;
2656
2657 if (core->parent &&
2658 core->flags & CLK_DUTY_CYCLE_PARENT) {
2659 ret = clk_core_update_duty_cycle_nolock(core->parent);
2660 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2661 } else {
2662 clk_core_reset_duty_cycle_nolock(core);
2663 }
2664
2665 return ret;
2666}
2667
2668static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2669 struct clk_duty *duty);
2670
2671static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2672 struct clk_duty *duty)
2673{
2674 int ret;
2675
2676 lockdep_assert_held(&prepare_lock);
2677
2678 if (clk_core_rate_is_protected(core))
2679 return -EBUSY;
2680
2681 trace_clk_set_duty_cycle(core, duty);
2682
2683 if (!core->ops->set_duty_cycle)
2684 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2685
2686 ret = core->ops->set_duty_cycle(core->hw, duty);
2687 if (!ret)
2688 memcpy(&core->duty, duty, sizeof(*duty));
2689
2690 trace_clk_set_duty_cycle_complete(core, duty);
2691
2692 return ret;
2693}
2694
2695static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2696 struct clk_duty *duty)
2697{
2698 int ret = 0;
2699
2700 if (core->parent &&
2701 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2702 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2703 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2704 }
2705
2706 return ret;
2707}
2708
2709/**
2710 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2711 * @clk: clock signal source
2712 * @num: numerator of the duty cycle ratio to be applied
2713 * @den: denominator of the duty cycle ratio to be applied
2714 *
2715 * Apply the duty cycle ratio if the ratio is valid and the clock can
2716 * perform this operation
2717 *
2718 * Returns (0) on success, a negative errno otherwise.
2719 */
2720int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2721{
2722 int ret;
2723 struct clk_duty duty;
2724
2725 if (!clk)
2726 return 0;
2727
2728 /* sanity check the ratio */
2729 if (den == 0 || num > den)
2730 return -EINVAL;
2731
2732 duty.num = num;
2733 duty.den = den;
2734
2735 clk_prepare_lock();
2736
2737 if (clk->exclusive_count)
2738 clk_core_rate_unprotect(clk->core);
2739
2740 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2741
2742 if (clk->exclusive_count)
2743 clk_core_rate_protect(clk->core);
2744
2745 clk_prepare_unlock();
2746
2747 return ret;
2748}
2749EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2750
2751static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2752 unsigned int scale)
2753{
2754 struct clk_duty *duty = &core->duty;
2755 int ret;
2756
2757 clk_prepare_lock();
2758
2759 ret = clk_core_update_duty_cycle_nolock(core);
2760 if (!ret)
2761 ret = mult_frac(scale, duty->num, duty->den);
2762
2763 clk_prepare_unlock();
2764
2765 return ret;
2766}
2767
2768/**
2769 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2770 * @clk: clock signal source
2771 * @scale: scaling factor to be applied to represent the ratio as an integer
2772 *
2773 * Returns the duty cycle ratio of a clock node multiplied by the provided
2774 * scaling factor, or negative errno on error.
2775 */
2776int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2777{
2778 if (!clk)
2779 return 0;
2780
2781 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2782}
2783EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2784
Mike Turquetteb24764902012-03-15 23:11:19 -07002785/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002786 * clk_is_match - check if two clk's point to the same hardware clock
2787 * @p: clk compared against q
2788 * @q: clk compared against p
2789 *
2790 * Returns true if the two struct clk pointers both point to the same hardware
2791 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2792 * share the same struct clk_core object.
2793 *
2794 * Returns false otherwise. Note that two NULL clks are treated as matching.
2795 */
2796bool clk_is_match(const struct clk *p, const struct clk *q)
2797{
2798 /* trivial case: identical struct clk's or both NULL */
2799 if (p == q)
2800 return true;
2801
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002802 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002803 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2804 if (p->core == q->core)
2805 return true;
2806
2807 return false;
2808}
2809EXPORT_SYMBOL_GPL(clk_is_match);
2810
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002811/*** debugfs support ***/
2812
2813#ifdef CONFIG_DEBUG_FS
2814#include <linux/debugfs.h>
2815
2816static struct dentry *rootdir;
2817static int inited = 0;
2818static DEFINE_MUTEX(clk_debug_lock);
2819static HLIST_HEAD(clk_debug_list);
2820
2821static struct hlist_head *all_lists[] = {
2822 &clk_root_list,
2823 &clk_orphan_list,
2824 NULL,
2825};
2826
2827static struct hlist_head *orphan_list[] = {
2828 &clk_orphan_list,
2829 NULL,
2830};
2831
2832static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2833 int level)
2834{
2835 if (!c)
2836 return;
2837
Jerome Brunet9fba7382018-06-19 16:41:41 +02002838 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002839 level * 3 + 1, "",
2840 30 - level * 3, c->name,
Jerome Brunete55a8392017-12-01 22:51:56 +01002841 c->enable_count, c->prepare_count, c->protect_count,
2842 clk_core_get_rate(c), clk_core_get_accuracy(c),
Jerome Brunet9fba7382018-06-19 16:41:41 +02002843 clk_core_get_phase(c),
2844 clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002845}
2846
2847static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2848 int level)
2849{
2850 struct clk_core *child;
2851
2852 if (!c)
2853 return;
2854
2855 clk_summary_show_one(s, c, level);
2856
2857 hlist_for_each_entry(child, &c->children, child_node)
2858 clk_summary_show_subtree(s, child, level + 1);
2859}
2860
2861static int clk_summary_show(struct seq_file *s, void *data)
2862{
2863 struct clk_core *c;
2864 struct hlist_head **lists = (struct hlist_head **)s->private;
2865
Jerome Brunet9fba7382018-06-19 16:41:41 +02002866 seq_puts(s, " enable prepare protect duty\n");
2867 seq_puts(s, " clock count count count rate accuracy phase cycle\n");
2868 seq_puts(s, "---------------------------------------------------------------------------------------------\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002869
2870 clk_prepare_lock();
2871
2872 for (; *lists; lists++)
2873 hlist_for_each_entry(c, *lists, child_node)
2874 clk_summary_show_subtree(s, c, 0);
2875
2876 clk_prepare_unlock();
2877
2878 return 0;
2879}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002880DEFINE_SHOW_ATTRIBUTE(clk_summary);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002881
2882static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2883{
2884 if (!c)
2885 return;
2886
Stefan Wahren7cb81132015-04-29 16:36:43 +00002887 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002888 seq_printf(s, "\"%s\": { ", c->name);
2889 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2890 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002891 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002892 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2893 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Lubomir Rintelc6e90992019-01-04 23:05:49 +01002894 seq_printf(s, "\"phase\": %d,", clk_core_get_phase(c));
Jerome Brunet9fba7382018-06-19 16:41:41 +02002895 seq_printf(s, "\"duty_cycle\": %u",
2896 clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002897}
2898
2899static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2900{
2901 struct clk_core *child;
2902
2903 if (!c)
2904 return;
2905
2906 clk_dump_one(s, c, level);
2907
2908 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002909 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002910 clk_dump_subtree(s, child, level + 1);
2911 }
2912
Markus Elfring4d327582017-04-20 08:45:43 +02002913 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002914}
2915
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002916static int clk_dump_show(struct seq_file *s, void *data)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002917{
2918 struct clk_core *c;
2919 bool first_node = true;
2920 struct hlist_head **lists = (struct hlist_head **)s->private;
2921
Markus Elfring4d327582017-04-20 08:45:43 +02002922 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002923 clk_prepare_lock();
2924
2925 for (; *lists; lists++) {
2926 hlist_for_each_entry(c, *lists, child_node) {
2927 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02002928 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002929 first_node = false;
2930 clk_dump_subtree(s, c, 0);
2931 }
2932 }
2933
2934 clk_prepare_unlock();
2935
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002936 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002937 return 0;
2938}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002939DEFINE_SHOW_ATTRIBUTE(clk_dump);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002940
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002941static const struct {
2942 unsigned long flag;
2943 const char *name;
2944} clk_flags[] = {
Geert Uytterhoeven40dd71c2018-07-06 17:16:54 +02002945#define ENTRY(f) { f, #f }
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002946 ENTRY(CLK_SET_RATE_GATE),
2947 ENTRY(CLK_SET_PARENT_GATE),
2948 ENTRY(CLK_SET_RATE_PARENT),
2949 ENTRY(CLK_IGNORE_UNUSED),
2950 ENTRY(CLK_IS_BASIC),
2951 ENTRY(CLK_GET_RATE_NOCACHE),
2952 ENTRY(CLK_SET_RATE_NO_REPARENT),
2953 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2954 ENTRY(CLK_RECALC_NEW_RATES),
2955 ENTRY(CLK_SET_RATE_UNGATE),
2956 ENTRY(CLK_IS_CRITICAL),
2957 ENTRY(CLK_OPS_PARENT_ENABLE),
Jerome Brunet9fba7382018-06-19 16:41:41 +02002958 ENTRY(CLK_DUTY_CYCLE_PARENT),
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002959#undef ENTRY
2960};
2961
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002962static int clk_flags_show(struct seq_file *s, void *data)
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002963{
2964 struct clk_core *core = s->private;
2965 unsigned long flags = core->flags;
2966 unsigned int i;
2967
2968 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2969 if (flags & clk_flags[i].flag) {
2970 seq_printf(s, "%s\n", clk_flags[i].name);
2971 flags &= ~clk_flags[i].flag;
2972 }
2973 }
2974 if (flags) {
2975 /* Unknown flags */
2976 seq_printf(s, "0x%lx\n", flags);
2977 }
2978
2979 return 0;
2980}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002981DEFINE_SHOW_ATTRIBUTE(clk_flags);
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002982
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002983static int possible_parents_show(struct seq_file *s, void *data)
Peter De Schrijver92031572017-03-21 15:20:31 +02002984{
2985 struct clk_core *core = s->private;
2986 int i;
2987
2988 for (i = 0; i < core->num_parents - 1; i++)
Stephen Boydfc0c2092019-04-12 11:31:47 -07002989 seq_printf(s, "%s ", core->parents[i].name);
Peter De Schrijver92031572017-03-21 15:20:31 +02002990
Stephen Boydfc0c2092019-04-12 11:31:47 -07002991 seq_printf(s, "%s\n", core->parents[i].name);
Peter De Schrijver92031572017-03-21 15:20:31 +02002992
2993 return 0;
2994}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002995DEFINE_SHOW_ATTRIBUTE(possible_parents);
Peter De Schrijver92031572017-03-21 15:20:31 +02002996
Jerome Brunet9fba7382018-06-19 16:41:41 +02002997static int clk_duty_cycle_show(struct seq_file *s, void *data)
2998{
2999 struct clk_core *core = s->private;
3000 struct clk_duty *duty = &core->duty;
3001
3002 seq_printf(s, "%u/%u\n", duty->num, duty->den);
3003
3004 return 0;
3005}
3006DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
3007
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003008static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003009{
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003010 struct dentry *root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003011
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003012 if (!core || !pdentry)
3013 return;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003014
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003015 root = debugfs_create_dir(core->name, pdentry);
3016 core->dentry = root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003017
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003018 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
3019 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
3020 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
3021 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
3022 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
3023 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
3024 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
3025 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
Jerome Brunet9fba7382018-06-19 16:41:41 +02003026 debugfs_create_file("clk_duty_cycle", 0444, root, core,
3027 &clk_duty_cycle_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003028
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003029 if (core->num_parents > 1)
3030 debugfs_create_file("clk_possible_parents", 0444, root, core,
3031 &possible_parents_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003032
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003033 if (core->ops->debug_init)
3034 core->ops->debug_init(core->hw, core->dentry);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003035}
3036
3037/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003038 * clk_debug_register - add a clk node to the debugfs clk directory
3039 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003040 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003041 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3042 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003043 * will be created lazily by clk_debug_init as part of a late_initcall.
3044 */
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003045static void clk_debug_register(struct clk_core *core)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003046{
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003047 mutex_lock(&clk_debug_lock);
3048 hlist_add_head(&core->debug_node, &clk_debug_list);
Stephen Boyddb3188fa2018-01-03 16:44:37 -08003049 if (inited)
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003050 clk_debug_create_one(core, rootdir);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003051 mutex_unlock(&clk_debug_lock);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003052}
3053
3054 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003055 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3056 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003057 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003058 * Dynamically removes a clk and all its child nodes from the
3059 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08003060 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003061 */
3062static void clk_debug_unregister(struct clk_core *core)
3063{
3064 mutex_lock(&clk_debug_lock);
3065 hlist_del_init(&core->debug_node);
3066 debugfs_remove_recursive(core->dentry);
3067 core->dentry = NULL;
3068 mutex_unlock(&clk_debug_lock);
3069}
3070
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003071/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003072 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003073 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003074 * clks are often initialized very early during boot before memory can be
3075 * dynamically allocated and well before debugfs is setup. This function
3076 * populates the debugfs clk directory once at boot-time when we know that
3077 * debugfs is setup. It should only be called once at boot-time, all other clks
3078 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003079 */
3080static int __init clk_debug_init(void)
3081{
3082 struct clk_core *core;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003083
3084 rootdir = debugfs_create_dir("clk", NULL);
3085
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003086 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
3087 &clk_summary_fops);
3088 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
3089 &clk_dump_fops);
3090 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
3091 &clk_summary_fops);
3092 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
3093 &clk_dump_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003094
3095 mutex_lock(&clk_debug_lock);
3096 hlist_for_each_entry(core, &clk_debug_list, debug_node)
3097 clk_debug_create_one(core, rootdir);
3098
3099 inited = 1;
3100 mutex_unlock(&clk_debug_lock);
3101
3102 return 0;
3103}
3104late_initcall(clk_debug_init);
3105#else
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003106static inline void clk_debug_register(struct clk_core *core) { }
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003107static inline void clk_debug_reparent(struct clk_core *core,
3108 struct clk_core *new_parent)
3109{
3110}
3111static inline void clk_debug_unregister(struct clk_core *core)
3112{
3113}
3114#endif
3115
Michael Turquette3d3801e2015-02-25 09:11:01 -08003116/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003117 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003118 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07003119 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003120 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07003121 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07003122 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003123static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07003124{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003125 int ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003126 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003127 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003128 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003129
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003130 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07003131 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07003132
Mike Turquetteeab89f62013-03-28 13:59:01 -07003133 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003134
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003135 ret = clk_pm_runtime_get(core);
3136 if (ret)
3137 goto unlock;
3138
Mike Turquetteb24764902012-03-15 23:11:19 -07003139 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003140 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003141 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003142 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003143 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07003144 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07003145 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003146
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03003147 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003148 if (core->ops->set_rate &&
3149 !((core->ops->round_rate || core->ops->determine_rate) &&
3150 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003151 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3152 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003153 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003154 goto out;
3155 }
3156
Stephen Boydd6968fc2015-04-30 13:54:13 -07003157 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003158 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3159 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003160 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003161 goto out;
3162 }
3163
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09003164 if (core->num_parents > 1 && !core->ops->get_parent) {
3165 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3166 __func__, core->name);
3167 ret = -EINVAL;
3168 goto out;
3169 }
3170
Stephen Boydd6968fc2015-04-30 13:54:13 -07003171 if (core->ops->set_rate_and_parent &&
3172 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003173 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003174 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08003175 ret = -EINVAL;
3176 goto out;
3177 }
3178
Stephen Boydd6968fc2015-04-30 13:54:13 -07003179 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07003180
3181 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08003182 * Populate core->parent if parent has already been clk_core_init'd. If
3183 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003184 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07003185 * clk list.
3186 *
3187 * Every time a new clk is clk_init'd then we walk the list of orphan
3188 * clocks and re-parent any that are children of the clock currently
3189 * being clk_init'd.
3190 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02003191 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003192 hlist_add_head(&core->child_node,
3193 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003194 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003195 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003196 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003197 core->orphan = false;
3198 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003199 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003200 core->orphan = true;
3201 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003202
3203 /*
Jerome Brunet541deba2018-02-14 14:43:37 +01003204 * optional platform-specific magic
3205 *
3206 * The .init callback is not used by any of the basic clock types, but
3207 * exists for weird hardware that must perform initialization magic.
3208 * Please consider other ways of solving initialization problems before
3209 * using this callback, as its use is discouraged.
3210 */
3211 if (core->ops->init)
3212 core->ops->init(core->hw);
3213
3214 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003215 * Set clk's accuracy. The preferred method is to use
3216 * .recalc_accuracy. For simple clocks and lazy developers the default
3217 * fallback is to use the parent's accuracy. If a clock doesn't have a
3218 * parent (or is orphaned) then accuracy is set to zero (perfect
3219 * clock).
3220 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003221 if (core->ops->recalc_accuracy)
3222 core->accuracy = core->ops->recalc_accuracy(core->hw,
3223 __clk_get_accuracy(core->parent));
3224 else if (core->parent)
3225 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003226 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003227 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003228
3229 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02003230 * Set clk's phase.
3231 * Since a phase is by definition relative to its parent, just
3232 * query the current clock phase, or just assume it's in phase.
3233 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003234 if (core->ops->get_phase)
3235 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02003236 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003237 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02003238
3239 /*
Jerome Brunet9fba7382018-06-19 16:41:41 +02003240 * Set clk's duty cycle.
3241 */
3242 clk_core_update_duty_cycle_nolock(core);
3243
3244 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07003245 * Set clk's rate. The preferred method is to use .recalc_rate. For
3246 * simple clocks and lazy developers the default fallback is to use the
3247 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3248 * then rate is set to zero.
3249 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003250 if (core->ops->recalc_rate)
3251 rate = core->ops->recalc_rate(core->hw,
3252 clk_core_get_rate_nolock(core->parent));
3253 else if (core->parent)
3254 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003255 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003256 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003257 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003258
3259 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003260 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3261 * don't get accidentally disabled when walking the orphan tree and
3262 * reparenting clocks
3263 */
3264 if (core->flags & CLK_IS_CRITICAL) {
3265 unsigned long flags;
3266
3267 clk_core_prepare(core);
3268
3269 flags = clk_enable_lock();
3270 clk_core_enable(core);
3271 clk_enable_unlock(flags);
3272 }
3273
3274 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003275 * walk the list of orphan clocks and reparent any that newly finds a
3276 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07003277 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08003278 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003279 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01003280
Michael Turquette904e6ea2016-07-08 16:32:10 -07003281 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003282 * We need to use __clk_set_parent_before() and _after() to
3283 * to properly migrate any prepare/enable count of the orphan
3284 * clock. This is important for CLK_IS_CRITICAL clocks, which
3285 * are enabled during init but might not have a parent yet.
Michael Turquette904e6ea2016-07-08 16:32:10 -07003286 */
3287 if (parent) {
Stephen Boydf8f8f1d2017-11-02 00:36:09 -07003288 /* update the clk tree topology */
Jerome Brunet99652a42018-02-14 14:43:36 +01003289 __clk_set_parent_before(orphan, parent);
3290 __clk_set_parent_after(orphan, parent, NULL);
Michael Turquette904e6ea2016-07-08 16:32:10 -07003291 __clk_recalc_accuracies(orphan);
3292 __clk_recalc_rates(orphan, 0);
3293 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003294 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003295
Stephen Boydd6968fc2015-04-30 13:54:13 -07003296 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07003297out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003298 clk_pm_runtime_put(core);
3299unlock:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003300 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003301
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003302 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003303 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003304
Mike Turquetted1302a32012-03-29 14:30:40 -07003305 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07003306}
3307
Stephen Boyd1df40462018-12-11 08:32:04 -08003308/**
3309 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3310 * @core: clk to add consumer to
3311 * @clk: consumer to link to a clk
3312 */
3313static void clk_core_link_consumer(struct clk_core *core, struct clk *clk)
3314{
3315 clk_prepare_lock();
3316 hlist_add_head(&clk->clks_node, &core->clks);
3317 clk_prepare_unlock();
3318}
3319
3320/**
3321 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3322 * @clk: consumer to unlink
3323 */
3324static void clk_core_unlink_consumer(struct clk *clk)
3325{
3326 lockdep_assert_held(&prepare_lock);
3327 hlist_del(&clk->clks_node);
3328}
3329
3330/**
3331 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
3332 * @core: clk to allocate a consumer for
3333 * @dev_id: string describing device name
3334 * @con_id: connection ID string on device
3335 *
3336 * Returns: clk consumer left unlinked from the consumer list
3337 */
3338static struct clk *alloc_clk(struct clk_core *core, const char *dev_id,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003339 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003340{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003341 struct clk *clk;
3342
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003343 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3344 if (!clk)
3345 return ERR_PTR(-ENOMEM);
3346
Stephen Boyd1df40462018-12-11 08:32:04 -08003347 clk->core = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003348 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02003349 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003350 clk->max_rate = ULONG_MAX;
3351
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003352 return clk;
3353}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003354
Stephen Boyd1df40462018-12-11 08:32:04 -08003355/**
3356 * free_clk - Free a clk consumer
3357 * @clk: clk consumer to free
3358 *
3359 * Note, this assumes the clk has been unlinked from the clk_core consumer
3360 * list.
3361 */
3362static void free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003363{
Leonard Crestez253160a2017-02-20 15:20:56 +02003364 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003365 kfree(clk);
3366}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003367
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003368/**
Stephen Boyd1df40462018-12-11 08:32:04 -08003369 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
3370 * a clk_hw
Stephen Boydefa85042018-12-11 08:34:16 -08003371 * @dev: clk consumer device
Stephen Boyd1df40462018-12-11 08:32:04 -08003372 * @hw: clk_hw associated with the clk being consumed
3373 * @dev_id: string describing device name
3374 * @con_id: connection ID string on device
3375 *
3376 * This is the main function used to create a clk pointer for use by clk
3377 * consumers. It connects a consumer to the clk_core and clk_hw structures
3378 * used by the framework and clk provider respectively.
3379 */
Stephen Boydefa85042018-12-11 08:34:16 -08003380struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
Stephen Boyd1df40462018-12-11 08:32:04 -08003381 const char *dev_id, const char *con_id)
3382{
3383 struct clk *clk;
3384 struct clk_core *core;
3385
3386 /* This is to allow this function to be chained to others */
3387 if (IS_ERR_OR_NULL(hw))
3388 return ERR_CAST(hw);
3389
3390 core = hw->core;
3391 clk = alloc_clk(core, dev_id, con_id);
3392 if (IS_ERR(clk))
3393 return clk;
Stephen Boydefa85042018-12-11 08:34:16 -08003394 clk->dev = dev;
Stephen Boyd1df40462018-12-11 08:32:04 -08003395
3396 if (!try_module_get(core->owner)) {
3397 free_clk(clk);
3398 return ERR_PTR(-ENOENT);
3399 }
3400
3401 kref_get(&core->ref);
3402 clk_core_link_consumer(core, clk);
3403
3404 return clk;
3405}
3406
Stephen Boydfc0c2092019-04-12 11:31:47 -07003407static int clk_cpy_name(const char **dst_p, const char *src, bool must_exist)
3408{
3409 const char *dst;
3410
3411 if (!src) {
3412 if (must_exist)
3413 return -EINVAL;
3414 return 0;
3415 }
3416
3417 *dst_p = dst = kstrdup_const(src, GFP_KERNEL);
3418 if (!dst)
3419 return -ENOMEM;
3420
3421 return 0;
3422}
3423
3424static int clk_core_populate_parent_map(struct clk_core *core)
3425{
3426 const struct clk_init_data *init = core->hw->init;
3427 u8 num_parents = init->num_parents;
3428 const char * const *parent_names = init->parent_names;
3429 const struct clk_hw **parent_hws = init->parent_hws;
3430 const struct clk_parent_data *parent_data = init->parent_data;
3431 int i, ret = 0;
3432 struct clk_parent_map *parents, *parent;
3433
3434 if (!num_parents)
3435 return 0;
3436
3437 /*
3438 * Avoid unnecessary string look-ups of clk_core's possible parents by
3439 * having a cache of names/clk_hw pointers to clk_core pointers.
3440 */
3441 parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
3442 core->parents = parents;
3443 if (!parents)
3444 return -ENOMEM;
3445
3446 /* Copy everything over because it might be __initdata */
3447 for (i = 0, parent = parents; i < num_parents; i++, parent++) {
Stephen Boyd601b6e92019-04-12 11:31:49 -07003448 parent->index = -1;
Stephen Boydfc0c2092019-04-12 11:31:47 -07003449 if (parent_names) {
3450 /* throw a WARN if any entries are NULL */
3451 WARN(!parent_names[i],
3452 "%s: invalid NULL in %s's .parent_names\n",
3453 __func__, core->name);
3454 ret = clk_cpy_name(&parent->name, parent_names[i],
3455 true);
3456 } else if (parent_data) {
3457 parent->hw = parent_data[i].hw;
Stephen Boyd601b6e92019-04-12 11:31:49 -07003458 parent->index = parent_data[i].index;
Stephen Boydfc0c2092019-04-12 11:31:47 -07003459 ret = clk_cpy_name(&parent->fw_name,
3460 parent_data[i].fw_name, false);
3461 if (!ret)
3462 ret = clk_cpy_name(&parent->name,
3463 parent_data[i].name,
3464 false);
3465 } else if (parent_hws) {
3466 parent->hw = parent_hws[i];
3467 } else {
3468 ret = -EINVAL;
3469 WARN(1, "Must specify parents if num_parents > 0\n");
3470 }
3471
3472 if (ret) {
3473 do {
3474 kfree_const(parents[i].name);
3475 kfree_const(parents[i].fw_name);
3476 } while (--i >= 0);
3477 kfree(parents);
3478
3479 return ret;
3480 }
3481 }
3482
3483 return 0;
3484}
3485
3486static void clk_core_free_parent_map(struct clk_core *core)
3487{
3488 int i = core->num_parents;
3489
3490 if (!core->num_parents)
3491 return;
3492
3493 while (--i >= 0) {
3494 kfree_const(core->parents[i].name);
3495 kfree_const(core->parents[i].fw_name);
3496 }
3497
3498 kfree(core->parents);
3499}
3500
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003501static struct clk *
3502__clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07003503{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003504 int ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003505 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003506
Stephen Boydd6968fc2015-04-30 13:54:13 -07003507 core = kzalloc(sizeof(*core), GFP_KERNEL);
3508 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003509 ret = -ENOMEM;
3510 goto fail_out;
3511 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003512
Stephen Boydd6968fc2015-04-30 13:54:13 -07003513 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3514 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003515 ret = -ENOMEM;
3516 goto fail_name;
3517 }
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003518
3519 if (WARN_ON(!hw->init->ops)) {
3520 ret = -EINVAL;
3521 goto fail_ops;
3522 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07003523 core->ops = hw->init->ops;
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003524
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003525 if (dev && pm_runtime_enabled(dev))
Miquel Raynal24478832018-12-04 20:24:37 +01003526 core->rpm_enabled = true;
3527 core->dev = dev;
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003528 core->of_node = np;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003529 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003530 core->owner = dev->driver->owner;
3531 core->hw = hw;
3532 core->flags = hw->init->flags;
3533 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003534 core->min_rate = 0;
3535 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003536 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003537
Stephen Boydfc0c2092019-04-12 11:31:47 -07003538 ret = clk_core_populate_parent_map(core);
3539 if (ret)
Masahiro Yamada176d1162015-12-28 19:23:00 +09003540 goto fail_parents;
Masahiro Yamada176d1162015-12-28 19:23:00 +09003541
Stephen Boydd6968fc2015-04-30 13:54:13 -07003542 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003543
Stephen Boyd1df40462018-12-11 08:32:04 -08003544 /*
3545 * Don't call clk_hw_create_clk() here because that would pin the
3546 * provider module to itself and prevent it from ever being removed.
3547 */
3548 hw->clk = alloc_clk(core, NULL, NULL);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003549 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003550 ret = PTR_ERR(hw->clk);
Stephen Boydfc0c2092019-04-12 11:31:47 -07003551 goto fail_create_clk;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003552 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003553
Stephen Boyd1df40462018-12-11 08:32:04 -08003554 clk_core_link_consumer(hw->core, hw->clk);
3555
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003556 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003557 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003558 return hw->clk;
3559
Stephen Boyd1df40462018-12-11 08:32:04 -08003560 clk_prepare_lock();
3561 clk_core_unlink_consumer(hw->clk);
3562 clk_prepare_unlock();
3563
3564 free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003565 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003566
Stephen Boydfc0c2092019-04-12 11:31:47 -07003567fail_create_clk:
3568 clk_core_free_parent_map(core);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003569fail_parents:
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003570fail_ops:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003571 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003572fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003573 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003574fail_out:
3575 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003576}
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003577
3578/**
3579 * clk_register - allocate a new clock, register it and return an opaque cookie
3580 * @dev: device that is registering this clock
3581 * @hw: link to hardware-specific clock data
3582 *
3583 * clk_register is the primary interface for populating the clock tree with new
3584 * clock nodes. It returns a pointer to the newly allocated struct clk which
3585 * cannot be dereferenced by driver code but may be used in conjunction with the
3586 * rest of the clock API. In the event of an error clk_register will return an
3587 * error code; drivers must test for an error code after calling clk_register.
3588 */
3589struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3590{
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003591 return __clk_register(dev, dev_of_node(dev), hw);
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003592}
Mike Turquetteb24764902012-03-15 23:11:19 -07003593EXPORT_SYMBOL_GPL(clk_register);
3594
Stephen Boyd41438042016-02-05 17:02:52 -08003595/**
3596 * clk_hw_register - register a clk_hw and return an error code
3597 * @dev: device that is registering this clock
3598 * @hw: link to hardware-specific clock data
3599 *
3600 * clk_hw_register is the primary interface for populating the clock tree with
3601 * new clock nodes. It returns an integer equal to zero indicating success or
3602 * less than zero indicating failure. Drivers must test for an error code after
3603 * calling clk_hw_register().
3604 */
3605int clk_hw_register(struct device *dev, struct clk_hw *hw)
3606{
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003607 return PTR_ERR_OR_ZERO(__clk_register(dev, dev_of_node(dev), hw));
Stephen Boyd41438042016-02-05 17:02:52 -08003608}
3609EXPORT_SYMBOL_GPL(clk_hw_register);
3610
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003611/*
3612 * of_clk_hw_register - register a clk_hw and return an error code
3613 * @node: device_node of device that is registering this clock
3614 * @hw: link to hardware-specific clock data
3615 *
3616 * of_clk_hw_register() is the primary interface for populating the clock tree
3617 * with new clock nodes when a struct device is not available, but a struct
3618 * device_node is. It returns an integer equal to zero indicating success or
3619 * less than zero indicating failure. Drivers must test for an error code after
3620 * calling of_clk_hw_register().
3621 */
3622int of_clk_hw_register(struct device_node *node, struct clk_hw *hw)
3623{
3624 return PTR_ERR_OR_ZERO(__clk_register(NULL, node, hw));
3625}
3626EXPORT_SYMBOL_GPL(of_clk_hw_register);
3627
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003628/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003629static void __clk_release(struct kref *ref)
3630{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003631 struct clk_core *core = container_of(ref, struct clk_core, ref);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003632
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003633 lockdep_assert_held(&prepare_lock);
3634
Stephen Boydfc0c2092019-04-12 11:31:47 -07003635 clk_core_free_parent_map(core);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003636 kfree_const(core->name);
3637 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003638}
3639
3640/*
3641 * Empty clk_ops for unregistered clocks. These are used temporarily
3642 * after clk_unregister() was called on a clock and until last clock
3643 * consumer calls clk_put() and the struct clk object is freed.
3644 */
3645static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3646{
3647 return -ENXIO;
3648}
3649
3650static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3651{
3652 WARN_ON_ONCE(1);
3653}
3654
3655static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3656 unsigned long parent_rate)
3657{
3658 return -ENXIO;
3659}
3660
3661static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3662{
3663 return -ENXIO;
3664}
3665
3666static const struct clk_ops clk_nodrv_ops = {
3667 .enable = clk_nodrv_prepare_enable,
3668 .disable = clk_nodrv_disable_unprepare,
3669 .prepare = clk_nodrv_prepare_enable,
3670 .unprepare = clk_nodrv_disable_unprepare,
3671 .set_rate = clk_nodrv_set_rate,
3672 .set_parent = clk_nodrv_set_parent,
3673};
3674
Mark Brown1df5c932012-04-18 09:07:12 +01003675/**
3676 * clk_unregister - unregister a currently registered clock
3677 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003678 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003679void clk_unregister(struct clk *clk)
3680{
3681 unsigned long flags;
3682
Stephen Boyd6314b672014-09-04 23:37:49 -07003683 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3684 return;
3685
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003686 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003687
3688 clk_prepare_lock();
3689
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003690 if (clk->core->ops == &clk_nodrv_ops) {
3691 pr_err("%s: unregistered clock: %s\n", __func__,
3692 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003693 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003694 }
3695 /*
3696 * Assign empty clock ops for consumers that might still hold
3697 * a reference to this clock.
3698 */
3699 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003700 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003701 clk_enable_unlock(flags);
3702
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003703 if (!hlist_empty(&clk->core->children)) {
3704 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003705 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003706
3707 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003708 hlist_for_each_entry_safe(child, t, &clk->core->children,
3709 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01003710 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003711 }
3712
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003713 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003714
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003715 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003716 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003717 __func__, clk->core->name);
Jerome Brunete55a8392017-12-01 22:51:56 +01003718
3719 if (clk->core->protect_count)
3720 pr_warn("%s: unregistering protected clock: %s\n",
3721 __func__, clk->core->name);
3722
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003723 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003724unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003725 clk_prepare_unlock();
3726}
Mark Brown1df5c932012-04-18 09:07:12 +01003727EXPORT_SYMBOL_GPL(clk_unregister);
3728
Stephen Boyd41438042016-02-05 17:02:52 -08003729/**
3730 * clk_hw_unregister - unregister a currently registered clk_hw
3731 * @hw: hardware-specific clock data to unregister
3732 */
3733void clk_hw_unregister(struct clk_hw *hw)
3734{
3735 clk_unregister(hw->clk);
3736}
3737EXPORT_SYMBOL_GPL(clk_hw_unregister);
3738
Stephen Boyd46c87732012-09-24 13:38:04 -07003739static void devm_clk_release(struct device *dev, void *res)
3740{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003741 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003742}
3743
Stephen Boyd41438042016-02-05 17:02:52 -08003744static void devm_clk_hw_release(struct device *dev, void *res)
3745{
3746 clk_hw_unregister(*(struct clk_hw **)res);
3747}
3748
Stephen Boyd46c87732012-09-24 13:38:04 -07003749/**
3750 * devm_clk_register - resource managed clk_register()
3751 * @dev: device that is registering this clock
3752 * @hw: link to hardware-specific clock data
3753 *
3754 * Managed clk_register(). Clocks returned from this function are
3755 * automatically clk_unregister()ed on driver detach. See clk_register() for
3756 * more information.
3757 */
3758struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3759{
3760 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003761 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003762
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003763 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3764 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003765 return ERR_PTR(-ENOMEM);
3766
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003767 clk = clk_register(dev, hw);
3768 if (!IS_ERR(clk)) {
3769 *clkp = clk;
3770 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003771 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003772 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003773 }
3774
3775 return clk;
3776}
3777EXPORT_SYMBOL_GPL(devm_clk_register);
3778
Stephen Boyd41438042016-02-05 17:02:52 -08003779/**
3780 * devm_clk_hw_register - resource managed clk_hw_register()
3781 * @dev: device that is registering this clock
3782 * @hw: link to hardware-specific clock data
3783 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003784 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003785 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3786 * for more information.
3787 */
3788int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3789{
3790 struct clk_hw **hwp;
3791 int ret;
3792
3793 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3794 if (!hwp)
3795 return -ENOMEM;
3796
3797 ret = clk_hw_register(dev, hw);
3798 if (!ret) {
3799 *hwp = hw;
3800 devres_add(dev, hwp);
3801 } else {
3802 devres_free(hwp);
3803 }
3804
3805 return ret;
3806}
3807EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3808
Stephen Boyd46c87732012-09-24 13:38:04 -07003809static int devm_clk_match(struct device *dev, void *res, void *data)
3810{
3811 struct clk *c = res;
3812 if (WARN_ON(!c))
3813 return 0;
3814 return c == data;
3815}
3816
Stephen Boyd41438042016-02-05 17:02:52 -08003817static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3818{
3819 struct clk_hw *hw = res;
3820
3821 if (WARN_ON(!hw))
3822 return 0;
3823 return hw == data;
3824}
3825
Stephen Boyd46c87732012-09-24 13:38:04 -07003826/**
3827 * devm_clk_unregister - resource managed clk_unregister()
3828 * @clk: clock to unregister
3829 *
3830 * Deallocate a clock allocated with devm_clk_register(). Normally
3831 * this function will not need to be called and the resource management
3832 * code will ensure that the resource is freed.
3833 */
3834void devm_clk_unregister(struct device *dev, struct clk *clk)
3835{
3836 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3837}
3838EXPORT_SYMBOL_GPL(devm_clk_unregister);
3839
Stephen Boyd41438042016-02-05 17:02:52 -08003840/**
3841 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3842 * @dev: device that is unregistering the hardware-specific clock data
3843 * @hw: link to hardware-specific clock data
3844 *
3845 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3846 * this function will not need to be called and the resource management
3847 * code will ensure that the resource is freed.
3848 */
3849void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3850{
3851 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3852 hw));
3853}
3854EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3855
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003856/*
3857 * clkdev helpers
3858 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003859
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003860void __clk_put(struct clk *clk)
3861{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003862 struct module *owner;
3863
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003864 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003865 return;
3866
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003867 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003868
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01003869 /*
3870 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3871 * given user should be balanced with calls to clk_rate_exclusive_put()
3872 * and by that same consumer
3873 */
3874 if (WARN_ON(clk->exclusive_count)) {
3875 /* We voiced our concern, let's sanitize the situation */
3876 clk->core->protect_count -= (clk->exclusive_count - 1);
3877 clk_core_rate_unprotect(clk->core);
3878 clk->exclusive_count = 0;
3879 }
3880
Stephen Boyd50595f82015-02-06 11:42:44 -08003881 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003882 if (clk->min_rate > clk->core->req_rate ||
3883 clk->max_rate < clk->core->req_rate)
3884 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3885
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003886 owner = clk->core->owner;
3887 kref_put(&clk->core->ref, __clk_release);
3888
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003889 clk_prepare_unlock();
3890
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003891 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003892
Stephen Boyd1df40462018-12-11 08:32:04 -08003893 free_clk(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003894}
3895
Mike Turquetteb24764902012-03-15 23:11:19 -07003896/*** clk rate change notifiers ***/
3897
3898/**
3899 * clk_notifier_register - add a clk rate change notifier
3900 * @clk: struct clk * to watch
3901 * @nb: struct notifier_block * with callback info
3902 *
3903 * Request notification when clk's rate changes. This uses an SRCU
3904 * notifier because we want it to block and notifier unregistrations are
3905 * uncommon. The callbacks associated with the notifier must not
3906 * re-enter into the clk framework by calling any top-level clk APIs;
3907 * this will cause a nested prepare_lock mutex.
3908 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003909 * In all notification cases (pre, post and abort rate change) the original
3910 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3911 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003912 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003913 * clk_notifier_register() must be called from non-atomic context.
3914 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3915 * allocation failure; otherwise, passes along the return value of
3916 * srcu_notifier_chain_register().
3917 */
3918int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3919{
3920 struct clk_notifier *cn;
3921 int ret = -ENOMEM;
3922
3923 if (!clk || !nb)
3924 return -EINVAL;
3925
Mike Turquetteeab89f62013-03-28 13:59:01 -07003926 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003927
3928 /* search the list of notifiers for this clk */
3929 list_for_each_entry(cn, &clk_notifier_list, node)
3930 if (cn->clk == clk)
3931 break;
3932
3933 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3934 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02003935 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003936 if (!cn)
3937 goto out;
3938
3939 cn->clk = clk;
3940 srcu_init_notifier_head(&cn->notifier_head);
3941
3942 list_add(&cn->node, &clk_notifier_list);
3943 }
3944
3945 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3946
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003947 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003948
3949out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003950 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003951
3952 return ret;
3953}
3954EXPORT_SYMBOL_GPL(clk_notifier_register);
3955
3956/**
3957 * clk_notifier_unregister - remove a clk rate change notifier
3958 * @clk: struct clk *
3959 * @nb: struct notifier_block * with callback info
3960 *
3961 * Request no further notification for changes to 'clk' and frees memory
3962 * allocated in clk_notifier_register.
3963 *
3964 * Returns -EINVAL if called with null arguments; otherwise, passes
3965 * along the return value of srcu_notifier_chain_unregister().
3966 */
3967int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3968{
3969 struct clk_notifier *cn = NULL;
3970 int ret = -EINVAL;
3971
3972 if (!clk || !nb)
3973 return -EINVAL;
3974
Mike Turquetteeab89f62013-03-28 13:59:01 -07003975 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003976
3977 list_for_each_entry(cn, &clk_notifier_list, node)
3978 if (cn->clk == clk)
3979 break;
3980
3981 if (cn->clk == clk) {
3982 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3983
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003984 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07003985
3986 /* XXX the notifier code should handle this better */
3987 if (!cn->notifier_head.head) {
3988 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08003989 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07003990 kfree(cn);
3991 }
3992
3993 } else {
3994 ret = -ENOENT;
3995 }
3996
Mike Turquetteeab89f62013-03-28 13:59:01 -07003997 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003998
3999 return ret;
4000}
4001EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05004002
4003#ifdef CONFIG_OF
4004/**
4005 * struct of_clk_provider - Clock provider registration structure
4006 * @link: Entry in global list of clock providers
4007 * @node: Pointer to device tree node of clock provider
4008 * @get: Get clock callback. Returns NULL or a struct clk for the
4009 * given clock specifier
4010 * @data: context pointer to be passed into @get callback
4011 */
4012struct of_clk_provider {
4013 struct list_head link;
4014
4015 struct device_node *node;
4016 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004017 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05004018 void *data;
4019};
4020
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304021static const struct of_device_id __clk_of_table_sentinel
4022 __used __section(__clk_of_table_end);
4023
Grant Likely766e6a42012-04-09 14:50:06 -05004024static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004025static DEFINE_MUTEX(of_clk_mutex);
4026
Grant Likely766e6a42012-04-09 14:50:06 -05004027struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
4028 void *data)
4029{
4030 return data;
4031}
4032EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
4033
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004034struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
4035{
4036 return data;
4037}
4038EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
4039
Shawn Guo494bfec2012-08-22 21:36:27 +08004040struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
4041{
4042 struct clk_onecell_data *clk_data = data;
4043 unsigned int idx = clkspec->args[0];
4044
4045 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02004046 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08004047 return ERR_PTR(-EINVAL);
4048 }
4049
4050 return clk_data->clks[idx];
4051}
4052EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
4053
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004054struct clk_hw *
4055of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
4056{
4057 struct clk_hw_onecell_data *hw_data = data;
4058 unsigned int idx = clkspec->args[0];
4059
4060 if (idx >= hw_data->num) {
4061 pr_err("%s: invalid index %u\n", __func__, idx);
4062 return ERR_PTR(-EINVAL);
4063 }
4064
4065 return hw_data->hws[idx];
4066}
4067EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
4068
Grant Likely766e6a42012-04-09 14:50:06 -05004069/**
4070 * of_clk_add_provider() - Register a clock provider for a node
4071 * @np: Device node pointer associated with clock provider
4072 * @clk_src_get: callback for decoding clock
4073 * @data: context pointer for @clk_src_get callback.
4074 */
4075int of_clk_add_provider(struct device_node *np,
4076 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
4077 void *data),
4078 void *data)
4079{
4080 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004081 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004082
Markus Elfring1808a322017-04-20 09:30:52 +02004083 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05004084 if (!cp)
4085 return -ENOMEM;
4086
4087 cp->node = of_node_get(np);
4088 cp->data = data;
4089 cp->get = clk_src_get;
4090
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004091 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004092 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004093 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05004094 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05004095
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004096 ret = of_clk_set_defaults(np, true);
4097 if (ret < 0)
4098 of_clk_del_provider(np);
4099
4100 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004101}
4102EXPORT_SYMBOL_GPL(of_clk_add_provider);
4103
4104/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004105 * of_clk_add_hw_provider() - Register a clock provider for a node
4106 * @np: Device node pointer associated with clock provider
4107 * @get: callback for decoding clk_hw
4108 * @data: context pointer for @get callback.
4109 */
4110int of_clk_add_hw_provider(struct device_node *np,
4111 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4112 void *data),
4113 void *data)
4114{
4115 struct of_clk_provider *cp;
4116 int ret;
4117
4118 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4119 if (!cp)
4120 return -ENOMEM;
4121
4122 cp->node = of_node_get(np);
4123 cp->data = data;
4124 cp->get_hw = get;
4125
4126 mutex_lock(&of_clk_mutex);
4127 list_add(&cp->link, &of_clk_providers);
4128 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05004129 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004130
4131 ret = of_clk_set_defaults(np, true);
4132 if (ret < 0)
4133 of_clk_del_provider(np);
4134
4135 return ret;
4136}
4137EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
4138
Stephen Boydaa795c42017-09-01 16:16:40 -07004139static void devm_of_clk_release_provider(struct device *dev, void *res)
4140{
4141 of_clk_del_provider(*(struct device_node **)res);
4142}
4143
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004144/*
4145 * We allow a child device to use its parent device as the clock provider node
4146 * for cases like MFD sub-devices where the child device driver wants to use
4147 * devm_*() APIs but not list the device in DT as a sub-node.
4148 */
4149static struct device_node *get_clk_provider_node(struct device *dev)
4150{
4151 struct device_node *np, *parent_np;
4152
4153 np = dev->of_node;
4154 parent_np = dev->parent ? dev->parent->of_node : NULL;
4155
4156 if (!of_find_property(np, "#clock-cells", NULL))
4157 if (of_find_property(parent_np, "#clock-cells", NULL))
4158 np = parent_np;
4159
4160 return np;
4161}
4162
Matti Vaittinene45838b2018-12-04 13:33:48 +02004163/**
4164 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4165 * @dev: Device acting as the clock provider (used for DT node and lifetime)
4166 * @get: callback for decoding clk_hw
4167 * @data: context pointer for @get callback
4168 *
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004169 * Registers clock provider for given device's node. If the device has no DT
4170 * node or if the device node lacks of clock provider information (#clock-cells)
4171 * then the parent device's node is scanned for this information. If parent node
4172 * has the #clock-cells then it is used in registration. Provider is
4173 * automatically released at device exit.
Matti Vaittinene45838b2018-12-04 13:33:48 +02004174 *
4175 * Return: 0 on success or an errno on failure.
4176 */
Stephen Boydaa795c42017-09-01 16:16:40 -07004177int devm_of_clk_add_hw_provider(struct device *dev,
4178 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4179 void *data),
4180 void *data)
4181{
4182 struct device_node **ptr, *np;
4183 int ret;
4184
4185 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
4186 GFP_KERNEL);
4187 if (!ptr)
4188 return -ENOMEM;
4189
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004190 np = get_clk_provider_node(dev);
Stephen Boydaa795c42017-09-01 16:16:40 -07004191 ret = of_clk_add_hw_provider(np, get, data);
4192 if (!ret) {
4193 *ptr = np;
4194 devres_add(dev, ptr);
4195 } else {
4196 devres_free(ptr);
4197 }
4198
4199 return ret;
4200}
4201EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
4202
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004203/**
Grant Likely766e6a42012-04-09 14:50:06 -05004204 * of_clk_del_provider() - Remove a previously registered clock provider
4205 * @np: Device node pointer associated with clock provider
4206 */
4207void of_clk_del_provider(struct device_node *np)
4208{
4209 struct of_clk_provider *cp;
4210
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004211 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004212 list_for_each_entry(cp, &of_clk_providers, link) {
4213 if (cp->node == np) {
4214 list_del(&cp->link);
4215 of_node_put(cp->node);
4216 kfree(cp);
4217 break;
4218 }
4219 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004220 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004221}
4222EXPORT_SYMBOL_GPL(of_clk_del_provider);
4223
Stephen Boydaa795c42017-09-01 16:16:40 -07004224static int devm_clk_provider_match(struct device *dev, void *res, void *data)
4225{
4226 struct device_node **np = res;
4227
4228 if (WARN_ON(!np || !*np))
4229 return 0;
4230
4231 return *np == data;
4232}
4233
Matti Vaittinene45838b2018-12-04 13:33:48 +02004234/**
4235 * devm_of_clk_del_provider() - Remove clock provider registered using devm
4236 * @dev: Device to whose lifetime the clock provider was bound
4237 */
Stephen Boydaa795c42017-09-01 16:16:40 -07004238void devm_of_clk_del_provider(struct device *dev)
4239{
4240 int ret;
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004241 struct device_node *np = get_clk_provider_node(dev);
Stephen Boydaa795c42017-09-01 16:16:40 -07004242
4243 ret = devres_release(dev, devm_of_clk_release_provider,
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004244 devm_clk_provider_match, np);
Stephen Boydaa795c42017-09-01 16:16:40 -07004245
4246 WARN_ON(ret);
4247}
4248EXPORT_SYMBOL(devm_of_clk_del_provider);
4249
Stephen Boyd5dc7e842019-03-08 10:35:01 -08004250/*
4251 * Beware the return values when np is valid, but no clock provider is found.
4252 * If name == NULL, the function returns -ENOENT.
4253 * If name != NULL, the function returns -EINVAL. This is because
4254 * of_parse_phandle_with_args() is called even if of_property_match_string()
4255 * returns an error.
4256 */
Stephen Boydcf13f282018-12-19 15:09:14 -08004257static int of_parse_clkspec(const struct device_node *np, int index,
4258 const char *name, struct of_phandle_args *out_args)
Stephen Boyd44722872018-12-19 10:59:55 -08004259{
4260 int ret = -ENOENT;
4261
4262 /* Walk up the tree of devices looking for a clock property that matches */
4263 while (np) {
4264 /*
4265 * For named clocks, first look up the name in the
4266 * "clock-names" property. If it cannot be found, then index
4267 * will be an error code and of_parse_phandle_with_args() will
4268 * return -EINVAL.
4269 */
4270 if (name)
4271 index = of_property_match_string(np, "clock-names", name);
4272 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
4273 index, out_args);
4274 if (!ret)
4275 break;
4276 if (name && index >= 0)
4277 break;
4278
4279 /*
4280 * No matching clock found on this node. If the parent node
4281 * has a "clock-ranges" property, then we can try one of its
4282 * clocks.
4283 */
4284 np = np->parent;
4285 if (np && !of_get_property(np, "clock-ranges", NULL))
4286 break;
4287 index = 0;
4288 }
4289
4290 return ret;
4291}
4292
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004293static struct clk_hw *
4294__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4295 struct of_phandle_args *clkspec)
4296{
4297 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004298
Stephen Boyd74002fc2016-08-25 13:35:36 -07004299 if (provider->get_hw)
4300 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004301
Stephen Boyd74002fc2016-08-25 13:35:36 -07004302 clk = provider->get(clkspec, provider->data);
4303 if (IS_ERR(clk))
4304 return ERR_CAST(clk);
4305 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004306}
4307
Stephen Boydcf13f282018-12-19 15:09:14 -08004308static struct clk_hw *
4309of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
Grant Likely766e6a42012-04-09 14:50:06 -05004310{
4311 struct of_clk_provider *provider;
Stephen Boyd1df40462018-12-11 08:32:04 -08004312 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05004313
Stephen Boyd306c3422015-02-05 15:39:11 -08004314 if (!clkspec)
4315 return ERR_PTR(-EINVAL);
4316
Stephen Boyd306c3422015-02-05 15:39:11 -08004317 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004318 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07004319 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004320 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd1df40462018-12-11 08:32:04 -08004321 if (!IS_ERR(hw))
4322 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08004323 }
Grant Likely766e6a42012-04-09 14:50:06 -05004324 }
Stephen Boyd306c3422015-02-05 15:39:11 -08004325 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004326
Stephen Boyd44722872018-12-19 10:59:55 -08004327 return hw;
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004328}
4329
Stephen Boyd306c3422015-02-05 15:39:11 -08004330/**
4331 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4332 * @clkspec: pointer to a clock specifier data structure
4333 *
4334 * This function looks up a struct clk from the registered list of clock
4335 * providers, an input is a clock specifier data structure as returned
4336 * from the of_parse_phandle_with_args() function call.
4337 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004338struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4339{
Stephen Boyd44722872018-12-19 10:59:55 -08004340 struct clk_hw *hw = of_clk_get_hw_from_clkspec(clkspec);
4341
Stephen Boydefa85042018-12-11 08:34:16 -08004342 return clk_hw_create_clk(NULL, hw, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05004343}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06004344EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05004345
Stephen Boydcf13f282018-12-19 15:09:14 -08004346struct clk_hw *of_clk_get_hw(struct device_node *np, int index,
4347 const char *con_id)
4348{
4349 int ret;
4350 struct clk_hw *hw;
4351 struct of_phandle_args clkspec;
4352
4353 ret = of_parse_clkspec(np, index, con_id, &clkspec);
4354 if (ret)
4355 return ERR_PTR(ret);
4356
4357 hw = of_clk_get_hw_from_clkspec(&clkspec);
4358 of_node_put(clkspec.np);
4359
4360 return hw;
4361}
4362
4363static struct clk *__of_clk_get(struct device_node *np,
4364 int index, const char *dev_id,
4365 const char *con_id)
4366{
4367 struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
4368
4369 return clk_hw_create_clk(NULL, hw, dev_id, con_id);
4370}
4371
4372struct clk *of_clk_get(struct device_node *np, int index)
4373{
4374 return __of_clk_get(np, index, np->full_name, NULL);
4375}
4376EXPORT_SYMBOL(of_clk_get);
4377
4378/**
4379 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
4380 * @np: pointer to clock consumer node
4381 * @name: name of consumer's clock input, or NULL for the first clock reference
4382 *
4383 * This function parses the clocks and clock-names properties,
4384 * and uses them to look up the struct clk from the registered list of clock
4385 * providers.
4386 */
4387struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
4388{
4389 if (!np)
4390 return ERR_PTR(-ENOENT);
4391
Kuninori Morimoto65cf20a2019-03-06 16:18:28 +09004392 return __of_clk_get(np, 0, np->full_name, name);
Stephen Boydcf13f282018-12-19 15:09:14 -08004393}
4394EXPORT_SYMBOL(of_clk_get_by_name);
4395
Stephen Boyd929e7f32016-02-19 15:52:32 -08004396/**
4397 * of_clk_get_parent_count() - Count the number of clocks a device node has
4398 * @np: device node to count
4399 *
4400 * Returns: The number of clocks that are possible parents of this node
4401 */
4402unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07004403{
Stephen Boyd929e7f32016-02-19 15:52:32 -08004404 int count;
4405
4406 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4407 if (count < 0)
4408 return 0;
4409
4410 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07004411}
4412EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4413
Grant Likely766e6a42012-04-09 14:50:06 -05004414const char *of_clk_get_parent_name(struct device_node *np, int index)
4415{
4416 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004417 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05004418 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004419 const __be32 *vp;
4420 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05004421 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004422 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004423 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05004424
Grant Likely766e6a42012-04-09 14:50:06 -05004425 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4426 &clkspec);
4427 if (rc)
4428 return NULL;
4429
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004430 index = clkspec.args_count ? clkspec.args[0] : 0;
4431 count = 0;
4432
4433 /* if there is an indices property, use it to transfer the index
4434 * specified into an array offset for the clock-output-names property.
4435 */
4436 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4437 if (index == pv) {
4438 index = count;
4439 break;
4440 }
4441 count++;
4442 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09004443 /* We went off the end of 'clock-indices' without finding it */
4444 if (prop && !vp)
4445 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004446
Grant Likely766e6a42012-04-09 14:50:06 -05004447 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004448 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004449 &clk_name) < 0) {
4450 /*
4451 * Best effort to get the name if the clock has been
4452 * registered with the framework. If the clock isn't
4453 * registered, we return the node name as the name of
4454 * the clock as long as #clock-cells = 0.
4455 */
4456 clk = of_clk_get_from_provider(&clkspec);
4457 if (IS_ERR(clk)) {
4458 if (clkspec.args_count == 0)
4459 clk_name = clkspec.np->name;
4460 else
4461 clk_name = NULL;
4462 } else {
4463 clk_name = __clk_get_name(clk);
4464 clk_put(clk);
4465 }
4466 }
4467
Grant Likely766e6a42012-04-09 14:50:06 -05004468
4469 of_node_put(clkspec.np);
4470 return clk_name;
4471}
4472EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4473
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05004474/**
4475 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4476 * number of parents
4477 * @np: Device node pointer associated with clock provider
4478 * @parents: pointer to char array that hold the parents' names
4479 * @size: size of the @parents array
4480 *
4481 * Return: number of parents for the clock node.
4482 */
4483int of_clk_parent_fill(struct device_node *np, const char **parents,
4484 unsigned int size)
4485{
4486 unsigned int i = 0;
4487
4488 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4489 i++;
4490
4491 return i;
4492}
4493EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4494
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004495struct clock_provider {
Geert Uytterhoevena59704332018-04-10 15:06:05 +02004496 void (*clk_init_cb)(struct device_node *);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004497 struct device_node *np;
4498 struct list_head node;
4499};
4500
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004501/*
4502 * This function looks for a parent clock. If there is one, then it
4503 * checks that the provider for this parent clock was initialized, in
4504 * this case the parent clock will be ready.
4505 */
4506static int parent_ready(struct device_node *np)
4507{
4508 int i = 0;
4509
4510 while (true) {
4511 struct clk *clk = of_clk_get(np, i);
4512
4513 /* this parent is ready we can check the next one */
4514 if (!IS_ERR(clk)) {
4515 clk_put(clk);
4516 i++;
4517 continue;
4518 }
4519
4520 /* at least one parent is not ready, we exit now */
4521 if (PTR_ERR(clk) == -EPROBE_DEFER)
4522 return 0;
4523
4524 /*
4525 * Here we make assumption that the device tree is
4526 * written correctly. So an error means that there is
4527 * no more parent. As we didn't exit yet, then the
4528 * previous parent are ready. If there is no clock
4529 * parent, no need to wait for them, then we can
4530 * consider their absence as being ready
4531 */
4532 return 1;
4533 }
4534}
4535
Grant Likely766e6a42012-04-09 14:50:06 -05004536/**
Lee Jonesd56f8992016-02-11 13:19:11 -08004537 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4538 * @np: Device node pointer associated with clock provider
4539 * @index: clock index
Geert Uytterhoevenf7ae7502018-01-03 12:06:14 +01004540 * @flags: pointer to top-level framework flags
Lee Jonesd56f8992016-02-11 13:19:11 -08004541 *
4542 * Detects if the clock-critical property exists and, if so, sets the
4543 * corresponding CLK_IS_CRITICAL flag.
4544 *
4545 * Do not use this function. It exists only for legacy Device Tree
4546 * bindings, such as the one-clock-per-node style that are outdated.
4547 * Those bindings typically put all clock data into .dts and the Linux
4548 * driver has no clock data, thus making it impossible to set this flag
4549 * correctly from the driver. Only those drivers may call
4550 * of_clk_detect_critical from their setup functions.
4551 *
4552 * Return: error code or zero on success
4553 */
4554int of_clk_detect_critical(struct device_node *np,
4555 int index, unsigned long *flags)
4556{
4557 struct property *prop;
4558 const __be32 *cur;
4559 uint32_t idx;
4560
4561 if (!np || !flags)
4562 return -EINVAL;
4563
4564 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4565 if (index == idx)
4566 *flags |= CLK_IS_CRITICAL;
4567
4568 return 0;
4569}
4570
4571/**
Grant Likely766e6a42012-04-09 14:50:06 -05004572 * of_clk_init() - Scan and init clock providers from the DT
4573 * @matches: array of compatible values and init functions for providers.
4574 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004575 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01004576 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004577 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05004578 */
4579void __init of_clk_init(const struct of_device_id *matches)
4580{
Alex Elder7f7ed582013-08-22 11:31:31 -05004581 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05004582 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004583 struct clock_provider *clk_provider, *next;
4584 bool is_init_done;
4585 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07004586 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05004587
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304588 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03004589 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304590
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004591 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05004592 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004593 struct clock_provider *parent;
4594
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01004595 if (!of_device_is_available(np))
4596 continue;
4597
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004598 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4599 if (!parent) {
4600 list_for_each_entry_safe(clk_provider, next,
4601 &clk_provider_list, node) {
4602 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004603 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004604 kfree(clk_provider);
4605 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004606 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004607 return;
4608 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004609
4610 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004611 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01004612 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004613 }
4614
4615 while (!list_empty(&clk_provider_list)) {
4616 is_init_done = false;
4617 list_for_each_entry_safe(clk_provider, next,
4618 &clk_provider_list, node) {
4619 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004620
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02004621 /* Don't populate platform devices */
4622 of_node_set_flag(clk_provider->np,
4623 OF_POPULATED);
4624
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004625 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004626 of_clk_set_defaults(clk_provider->np, true);
4627
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004628 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004629 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004630 kfree(clk_provider);
4631 is_init_done = true;
4632 }
4633 }
4634
4635 /*
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01004636 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004637 * remaining providers during the last loop, so now we
4638 * initialize all the remaining ones unconditionally
4639 * in case the clock parent was not mandatory
4640 */
4641 if (!is_init_done)
4642 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05004643 }
4644}
4645#endif