blob: 50f5c73de688a5e4f02a201d90b618538b49c221 [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;
47};
48
Michael Turquetteb09d6d92015-01-29 14:22:50 -080049struct clk_core {
50 const char *name;
51 const struct clk_ops *ops;
52 struct clk_hw *hw;
53 struct module *owner;
Marek Szyprowski9a34b452017-08-21 10:04:59 +020054 struct device *dev;
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -070055 struct device_node *of_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080056 struct clk_core *parent;
Stephen Boydfc0c2092019-04-12 11:31:47 -070057 struct clk_parent_map *parents;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080058 u8 num_parents;
59 u8 new_parent_index;
60 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010061 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080062 unsigned long new_rate;
63 struct clk_core *new_parent;
64 struct clk_core *new_child;
65 unsigned long flags;
Heiko Stuebnere6500342015-04-22 22:53:05 +020066 bool orphan;
Miquel Raynal24478832018-12-04 20:24:37 +010067 bool rpm_enabled;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080068 unsigned int enable_count;
69 unsigned int prepare_count;
Jerome Brunete55a8392017-12-01 22:51:56 +010070 unsigned int protect_count;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070071 unsigned long min_rate;
72 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080073 unsigned long accuracy;
74 int phase;
Jerome Brunet9fba7382018-06-19 16:41:41 +020075 struct clk_duty duty;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080076 struct hlist_head children;
77 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010078 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080079 unsigned int notifier_count;
80#ifdef CONFIG_DEBUG_FS
81 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020082 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080083#endif
84 struct kref ref;
85};
86
Stephen Boyddfc202e2015-02-02 14:37:41 -080087#define CREATE_TRACE_POINTS
88#include <trace/events/clk.h>
89
Michael Turquetteb09d6d92015-01-29 14:22:50 -080090struct clk {
91 struct clk_core *core;
Stephen Boydefa85042018-12-11 08:34:16 -080092 struct device *dev;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080093 const char *dev_id;
94 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010095 unsigned long min_rate;
96 unsigned long max_rate;
Jerome Brunet55e9b8b2017-12-01 22:51:59 +010097 unsigned int exclusive_count;
Stephen Boyd50595f82015-02-06 11:42:44 -080098 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080099};
100
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200101/*** runtime pm ***/
102static int clk_pm_runtime_get(struct clk_core *core)
103{
Miquel Raynal24478832018-12-04 20:24:37 +0100104 int ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200105
Miquel Raynal24478832018-12-04 20:24:37 +0100106 if (!core->rpm_enabled)
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200107 return 0;
108
109 ret = pm_runtime_get_sync(core->dev);
110 return ret < 0 ? ret : 0;
111}
112
113static void clk_pm_runtime_put(struct clk_core *core)
114{
Miquel Raynal24478832018-12-04 20:24:37 +0100115 if (!core->rpm_enabled)
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200116 return;
117
118 pm_runtime_put_sync(core->dev);
119}
120
Mike Turquetteeab89f62013-03-28 13:59:01 -0700121/*** locking ***/
122static void clk_prepare_lock(void)
123{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700124 if (!mutex_trylock(&prepare_lock)) {
125 if (prepare_owner == current) {
126 prepare_refcnt++;
127 return;
128 }
129 mutex_lock(&prepare_lock);
130 }
131 WARN_ON_ONCE(prepare_owner != NULL);
132 WARN_ON_ONCE(prepare_refcnt != 0);
133 prepare_owner = current;
134 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700135}
136
137static void clk_prepare_unlock(void)
138{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700139 WARN_ON_ONCE(prepare_owner != current);
140 WARN_ON_ONCE(prepare_refcnt == 0);
141
142 if (--prepare_refcnt)
143 return;
144 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700145 mutex_unlock(&prepare_lock);
146}
147
148static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700149 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700150{
151 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700152
David Lechnera12aa8a2018-01-04 19:46:08 -0600153 /*
154 * On UP systems, spin_trylock_irqsave() always returns true, even if
155 * we already hold the lock. So, in that case, we rely only on
156 * reference counting.
157 */
158 if (!IS_ENABLED(CONFIG_SMP) ||
159 !spin_trylock_irqsave(&enable_lock, flags)) {
Mike Turquette533ddeb2013-03-28 13:59:02 -0700160 if (enable_owner == current) {
161 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700162 __acquire(enable_lock);
David Lechnera12aa8a2018-01-04 19:46:08 -0600163 if (!IS_ENABLED(CONFIG_SMP))
164 local_save_flags(flags);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700165 return flags;
166 }
167 spin_lock_irqsave(&enable_lock, flags);
168 }
169 WARN_ON_ONCE(enable_owner != NULL);
170 WARN_ON_ONCE(enable_refcnt != 0);
171 enable_owner = current;
172 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700173 return flags;
174}
175
176static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700177 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700178{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700179 WARN_ON_ONCE(enable_owner != current);
180 WARN_ON_ONCE(enable_refcnt == 0);
181
Stephen Boyda57aa182015-07-24 12:24:48 -0700182 if (--enable_refcnt) {
183 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700184 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700185 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700186 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700187 spin_unlock_irqrestore(&enable_lock, flags);
188}
189
Jerome Brunete55a8392017-12-01 22:51:56 +0100190static bool clk_core_rate_is_protected(struct clk_core *core)
191{
192 return core->protect_count;
193}
194
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700195static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530196{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200197 bool ret = false;
198
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700199 /*
200 * .is_prepared is optional for clocks that can prepare
201 * fall back to software usage counter if it is missing
202 */
203 if (!core->ops->is_prepared)
204 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530205
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200206 if (!clk_pm_runtime_get(core)) {
207 ret = core->ops->is_prepared(core->hw);
208 clk_pm_runtime_put(core);
209 }
210
211 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530212}
213
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700214static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530215{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200216 bool ret = false;
217
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700218 /*
219 * .is_enabled is only mandatory for clocks that gate
220 * fall back to software usage counter if .is_enabled is missing
221 */
222 if (!core->ops->is_enabled)
223 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530224
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200225 /*
226 * Check if clock controller's device is runtime active before
227 * calling .is_enabled callback. If not, assume that clock is
228 * disabled, because we might be called from atomic context, from
229 * which pm_runtime_get() is not allowed.
230 * This function is called mainly from clk_disable_unused_subtree,
231 * which ensures proper runtime pm activation of controller before
232 * taking enable spinlock, but the below check is needed if one tries
233 * to call it from other places.
234 */
Miquel Raynal24478832018-12-04 20:24:37 +0100235 if (core->rpm_enabled) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200236 pm_runtime_get_noresume(core->dev);
237 if (!pm_runtime_active(core->dev)) {
238 ret = false;
239 goto done;
240 }
241 }
242
243 ret = core->ops->is_enabled(core->hw);
244done:
Miquel Raynal24478832018-12-04 20:24:37 +0100245 if (core->rpm_enabled)
Dong Aisheng756efe12017-12-22 17:46:04 +0800246 pm_runtime_put(core->dev);
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200247
248 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530249}
250
Mike Turquetteb24764902012-03-15 23:11:19 -0700251/*** helper functions ***/
252
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200253const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700254{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100255 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700256}
Niels de Vos48950842012-12-13 13:12:25 +0100257EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700258
Stephen Boyde7df6f62015-08-12 13:04:56 -0700259const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700260{
261 return hw->core->name;
262}
263EXPORT_SYMBOL_GPL(clk_hw_get_name);
264
Russ Dill65800b22012-11-26 11:20:09 -0800265struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700266{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100267 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700268}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800269EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700270
Stephen Boyde7df6f62015-08-12 13:04:56 -0700271unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700272{
273 return hw->core->num_parents;
274}
275EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
276
Stephen Boyde7df6f62015-08-12 13:04:56 -0700277struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700278{
279 return hw->core->parent ? hw->core->parent->hw : NULL;
280}
281EXPORT_SYMBOL_GPL(clk_hw_get_parent);
282
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700283static struct clk_core *__clk_lookup_subtree(const char *name,
284 struct clk_core *core)
285{
286 struct clk_core *child;
287 struct clk_core *ret;
288
289 if (!strcmp(core->name, name))
290 return core;
291
292 hlist_for_each_entry(child, &core->children, child_node) {
293 ret = __clk_lookup_subtree(name, child);
294 if (ret)
295 return ret;
296 }
297
298 return NULL;
299}
300
301static struct clk_core *clk_core_lookup(const char *name)
302{
303 struct clk_core *root_clk;
304 struct clk_core *ret;
305
306 if (!name)
307 return NULL;
308
309 /* search the 'proper' clk tree first */
310 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
311 ret = __clk_lookup_subtree(name, root_clk);
312 if (ret)
313 return ret;
314 }
315
316 /* if not found, then search the orphan tree */
317 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
318 ret = __clk_lookup_subtree(name, root_clk);
319 if (ret)
320 return ret;
321 }
322
323 return NULL;
324}
325
Stephen Boydfc0c2092019-04-12 11:31:47 -0700326/**
Stephen Boyddde4eff2019-04-12 11:31:48 -0700327 * clk_core_get - Find the clk_core parent of a clk
Stephen Boydfc0c2092019-04-12 11:31:47 -0700328 * @core: clk to find parent of
Stephen Boyddde4eff2019-04-12 11:31:48 -0700329 * @name: name to search for
Stephen Boydfc0c2092019-04-12 11:31:47 -0700330 *
331 * This is the preferred method for clk providers to find the parent of a
332 * clk when that parent is external to the clk controller. The parent_names
333 * array is indexed and treated as a local name matching a string in the device
Stephen Boyddde4eff2019-04-12 11:31:48 -0700334 * node's 'clock-names' property or as the 'con_id' matching the device's
335 * dev_name() in a clk_lookup. This allows clk providers to use their own
Stephen Boydfc0c2092019-04-12 11:31:47 -0700336 * namespace instead of looking for a globally unique parent string.
337 *
338 * For example the following DT snippet would allow a clock registered by the
339 * clock-controller@c001 that has a clk_init_data::parent_data array
340 * with 'xtal' in the 'name' member to find the clock provided by the
341 * clock-controller@f00abcd without needing to get the globally unique name of
342 * the xtal clk.
343 *
344 * parent: clock-controller@f00abcd {
345 * reg = <0xf00abcd 0xabcd>;
346 * #clock-cells = <0>;
347 * };
348 *
349 * clock-controller@c001 {
350 * reg = <0xc001 0xf00d>;
351 * clocks = <&parent>;
352 * clock-names = "xtal";
353 * #clock-cells = <1>;
354 * };
355 *
356 * Returns: -ENOENT when the provider can't be found or the clk doesn't
357 * exist in the provider. -EINVAL when the name can't be found. NULL when the
358 * provider knows about the clk but it isn't provided on this system.
359 * A valid clk_core pointer when the clk can be found in the provider.
360 */
361static struct clk_core *clk_core_get(struct clk_core *core, const char *name)
362{
Stephen Boyddde4eff2019-04-12 11:31:48 -0700363 struct clk_hw *hw = ERR_PTR(-ENOENT);
364 struct device *dev = core->dev;
365 const char *dev_id = dev ? dev_name(dev) : NULL;
Stephen Boydfc0c2092019-04-12 11:31:47 -0700366 struct device_node *np = core->of_node;
367
Stephen Boyddde4eff2019-04-12 11:31:48 -0700368 if (np)
369 hw = of_clk_get_hw(np, -1, name);
Stephen Boydfc0c2092019-04-12 11:31:47 -0700370
Stephen Boyddde4eff2019-04-12 11:31:48 -0700371 /*
372 * If the DT search above couldn't find the provider or the provider
373 * didn't know about this clk, fallback to looking up via clkdev based
374 * clk_lookups
375 */
376 if (PTR_ERR(hw) == -ENOENT)
377 hw = clk_find_hw(dev_id, name);
378
379 if (IS_ERR(hw))
Stephen Boydfc0c2092019-04-12 11:31:47 -0700380 return ERR_CAST(hw);
381
382 return hw->core;
383}
384
385static void clk_core_fill_parent_index(struct clk_core *core, u8 index)
386{
387 struct clk_parent_map *entry = &core->parents[index];
388 struct clk_core *parent = ERR_PTR(-ENOENT);
389
390 if (entry->hw) {
391 parent = entry->hw->core;
392 /*
393 * We have a direct reference but it isn't registered yet?
394 * Orphan it and let clk_reparent() update the orphan status
395 * when the parent is registered.
396 */
397 if (!parent)
398 parent = ERR_PTR(-EPROBE_DEFER);
399 } else {
400 if (entry->fw_name)
401 parent = clk_core_get(core, entry->fw_name);
402 if (IS_ERR(parent) && PTR_ERR(parent) == -ENOENT)
403 parent = clk_core_lookup(entry->name);
404 }
405
406 /* Only cache it if it's not an error */
407 if (!IS_ERR(parent))
408 entry->core = parent;
409}
410
Stephen Boydd6968fc2015-04-30 13:54:13 -0700411static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100412 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100413{
Stephen Boydfc0c2092019-04-12 11:31:47 -0700414 if (!core || index >= core->num_parents || !core->parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100415 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900416
Stephen Boydfc0c2092019-04-12 11:31:47 -0700417 if (!core->parents[index].core)
418 clk_core_fill_parent_index(core, index);
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900419
Stephen Boydfc0c2092019-04-12 11:31:47 -0700420 return core->parents[index].core;
James Hogan7ef3dcc2013-07-29 12:24:58 +0100421}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100422
Stephen Boyde7df6f62015-08-12 13:04:56 -0700423struct clk_hw *
424clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700425{
426 struct clk_core *parent;
427
428 parent = clk_core_get_parent_by_index(hw->core, index);
429
430 return !parent ? NULL : parent->hw;
431}
432EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
433
Russ Dill65800b22012-11-26 11:20:09 -0800434unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700435{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100436 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700437}
438
Stephen Boydd6968fc2015-04-30 13:54:13 -0700439static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700440{
441 unsigned long ret;
442
Stephen Boydd6968fc2015-04-30 13:54:13 -0700443 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530444 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700445 goto out;
446 }
447
Stephen Boydd6968fc2015-04-30 13:54:13 -0700448 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700449
Stephen Boyd47b0eeb2016-02-02 17:24:56 -0800450 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -0700451 goto out;
452
Stephen Boydd6968fc2015-04-30 13:54:13 -0700453 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530454 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700455
456out:
457 return ret;
458}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100459
Stephen Boyde7df6f62015-08-12 13:04:56 -0700460unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700461{
462 return clk_core_get_rate_nolock(hw->core);
463}
464EXPORT_SYMBOL_GPL(clk_hw_get_rate);
465
Stephen Boydd6968fc2015-04-30 13:54:13 -0700466static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100467{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700468 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100469 return 0;
470
Stephen Boydd6968fc2015-04-30 13:54:13 -0700471 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100472}
473
Russ Dill65800b22012-11-26 11:20:09 -0800474unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700475{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100476 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700477}
Thierry Redingb05c6832013-09-03 09:43:51 +0200478EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700479
Stephen Boyde7df6f62015-08-12 13:04:56 -0700480unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700481{
482 return hw->core->flags;
483}
484EXPORT_SYMBOL_GPL(clk_hw_get_flags);
485
Stephen Boyde7df6f62015-08-12 13:04:56 -0700486bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700487{
488 return clk_core_is_prepared(hw->core);
489}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100490EXPORT_SYMBOL_GPL(clk_hw_is_prepared);
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700491
Jerome Brunete55a8392017-12-01 22:51:56 +0100492bool clk_hw_rate_is_protected(const struct clk_hw *hw)
493{
494 return clk_core_rate_is_protected(hw->core);
495}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100496EXPORT_SYMBOL_GPL(clk_hw_rate_is_protected);
Jerome Brunete55a8392017-12-01 22:51:56 +0100497
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200498bool clk_hw_is_enabled(const struct clk_hw *hw)
499{
500 return clk_core_is_enabled(hw->core);
501}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100502EXPORT_SYMBOL_GPL(clk_hw_is_enabled);
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200503
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100504bool __clk_is_enabled(struct clk *clk)
505{
506 if (!clk)
507 return false;
508
509 return clk_core_is_enabled(clk->core);
510}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800511EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700512
Stephen Boyd15a02c12015-01-19 18:05:28 -0800513static bool mux_is_better_rate(unsigned long rate, unsigned long now,
514 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100515{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800516 if (flags & CLK_MUX_ROUND_CLOSEST)
517 return abs(now - rate) < abs(best - rate);
518
519 return now <= rate && now > best;
520}
521
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200522int clk_mux_determine_rate_flags(struct clk_hw *hw,
523 struct clk_rate_request *req,
524 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100525{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100526 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200527 int i, num_parents, ret;
528 unsigned long best = 0;
529 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100530
531 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100532 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
533 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200534 if (core->flags & CLK_SET_RATE_PARENT) {
535 ret = __clk_determine_rate(parent ? parent->hw : NULL,
536 &parent_req);
537 if (ret)
538 return ret;
539
540 best = parent_req.rate;
541 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100542 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200543 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100544 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200545 }
546
James Hogane366fdd2013-07-29 12:25:02 +0100547 goto out;
548 }
549
550 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100551 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100552 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100553 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100554 if (!parent)
555 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200556
557 if (core->flags & CLK_SET_RATE_PARENT) {
558 parent_req = *req;
559 ret = __clk_determine_rate(parent->hw, &parent_req);
560 if (ret)
561 continue;
562 } else {
563 parent_req.rate = clk_core_get_rate_nolock(parent);
564 }
565
566 if (mux_is_better_rate(req->rate, parent_req.rate,
567 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100568 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200569 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100570 }
571 }
572
Boris Brezillon57d866e2015-07-09 22:39:38 +0200573 if (!best_parent)
574 return -EINVAL;
575
James Hogane366fdd2013-07-29 12:25:02 +0100576out:
577 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200578 req->best_parent_hw = best_parent->hw;
579 req->best_parent_rate = best;
580 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100581
Boris Brezillon0817b622015-07-07 20:48:08 +0200582 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100583}
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200584EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800585
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100586struct clk *__clk_lookup(const char *name)
587{
588 struct clk_core *core = clk_core_lookup(name);
589
590 return !core ? NULL : core->hw->clk;
591}
592
Stephen Boydd6968fc2015-04-30 13:54:13 -0700593static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100594 unsigned long *min_rate,
595 unsigned long *max_rate)
596{
597 struct clk *clk_user;
598
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700599 *min_rate = core->min_rate;
600 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100601
Stephen Boydd6968fc2015-04-30 13:54:13 -0700602 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100603 *min_rate = max(*min_rate, clk_user->min_rate);
604
Stephen Boydd6968fc2015-04-30 13:54:13 -0700605 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100606 *max_rate = min(*max_rate, clk_user->max_rate);
607}
608
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700609void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
610 unsigned long max_rate)
611{
612 hw->core->min_rate = min_rate;
613 hw->core->max_rate = max_rate;
614}
615EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
616
Stephen Boyd15a02c12015-01-19 18:05:28 -0800617/*
618 * Helper for finding best parent to provide a given frequency. This can be used
619 * directly as a determine_rate callback (e.g. for a mux), or from a more
620 * complex clock that may combine a mux with other operations.
621 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200622int __clk_mux_determine_rate(struct clk_hw *hw,
623 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800624{
Boris Brezillon0817b622015-07-07 20:48:08 +0200625 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800626}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800627EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100628
Boris Brezillon0817b622015-07-07 20:48:08 +0200629int __clk_mux_determine_rate_closest(struct clk_hw *hw,
630 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800631{
Boris Brezillon0817b622015-07-07 20:48:08 +0200632 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800633}
634EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
635
Mike Turquetteb24764902012-03-15 23:11:19 -0700636/*** clk api ***/
637
Jerome Brunete55a8392017-12-01 22:51:56 +0100638static void clk_core_rate_unprotect(struct clk_core *core)
639{
640 lockdep_assert_held(&prepare_lock);
641
642 if (!core)
643 return;
644
Fabio Estevamab525dc2018-01-16 10:50:34 -0200645 if (WARN(core->protect_count == 0,
646 "%s already unprotected\n", core->name))
Jerome Brunete55a8392017-12-01 22:51:56 +0100647 return;
648
649 if (--core->protect_count > 0)
650 return;
651
652 clk_core_rate_unprotect(core->parent);
653}
654
655static int clk_core_rate_nuke_protect(struct clk_core *core)
656{
657 int ret;
658
659 lockdep_assert_held(&prepare_lock);
660
661 if (!core)
662 return -EINVAL;
663
664 if (core->protect_count == 0)
665 return 0;
666
667 ret = core->protect_count;
668 core->protect_count = 1;
669 clk_core_rate_unprotect(core);
670
671 return ret;
672}
673
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100674/**
675 * clk_rate_exclusive_put - release exclusivity over clock rate control
676 * @clk: the clk over which the exclusivity is released
677 *
678 * clk_rate_exclusive_put() completes a critical section during which a clock
679 * consumer cannot tolerate any other consumer making any operation on the
680 * clock which could result in a rate change or rate glitch. Exclusive clocks
681 * cannot have their rate changed, either directly or indirectly due to changes
682 * further up the parent chain of clocks. As a result, clocks up parent chain
683 * also get under exclusive control of the calling consumer.
684 *
685 * If exlusivity is claimed more than once on clock, even by the same consumer,
686 * the rate effectively gets locked as exclusivity can't be preempted.
687 *
688 * Calls to clk_rate_exclusive_put() must be balanced with calls to
689 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
690 * error status.
691 */
692void clk_rate_exclusive_put(struct clk *clk)
693{
694 if (!clk)
695 return;
696
697 clk_prepare_lock();
698
699 /*
700 * if there is something wrong with this consumer protect count, stop
701 * here before messing with the provider
702 */
703 if (WARN_ON(clk->exclusive_count <= 0))
704 goto out;
705
706 clk_core_rate_unprotect(clk->core);
707 clk->exclusive_count--;
708out:
709 clk_prepare_unlock();
710}
711EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
712
Jerome Brunete55a8392017-12-01 22:51:56 +0100713static void clk_core_rate_protect(struct clk_core *core)
714{
715 lockdep_assert_held(&prepare_lock);
716
717 if (!core)
718 return;
719
720 if (core->protect_count == 0)
721 clk_core_rate_protect(core->parent);
722
723 core->protect_count++;
724}
725
726static void clk_core_rate_restore_protect(struct clk_core *core, int count)
727{
728 lockdep_assert_held(&prepare_lock);
729
730 if (!core)
731 return;
732
733 if (count == 0)
734 return;
735
736 clk_core_rate_protect(core);
737 core->protect_count = count;
738}
739
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100740/**
741 * clk_rate_exclusive_get - get exclusivity over the clk rate control
742 * @clk: the clk over which the exclusity of rate control is requested
743 *
744 * clk_rate_exlusive_get() begins a critical section during which a clock
745 * consumer cannot tolerate any other consumer making any operation on the
746 * clock which could result in a rate change or rate glitch. Exclusive clocks
747 * cannot have their rate changed, either directly or indirectly due to changes
748 * further up the parent chain of clocks. As a result, clocks up parent chain
749 * also get under exclusive control of the calling consumer.
750 *
751 * If exlusivity is claimed more than once on clock, even by the same consumer,
752 * the rate effectively gets locked as exclusivity can't be preempted.
753 *
754 * Calls to clk_rate_exclusive_get() should be balanced with calls to
755 * clk_rate_exclusive_put(). Calls to this function may sleep.
756 * Returns 0 on success, -EERROR otherwise
757 */
758int clk_rate_exclusive_get(struct clk *clk)
759{
760 if (!clk)
761 return 0;
762
763 clk_prepare_lock();
764 clk_core_rate_protect(clk->core);
765 clk->exclusive_count++;
766 clk_prepare_unlock();
767
768 return 0;
769}
770EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
771
Stephen Boydd6968fc2015-04-30 13:54:13 -0700772static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700773{
Stephen Boyda6334722015-05-06 17:00:54 -0700774 lockdep_assert_held(&prepare_lock);
775
Stephen Boydd6968fc2015-04-30 13:54:13 -0700776 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700777 return;
778
Fabio Estevamab525dc2018-01-16 10:50:34 -0200779 if (WARN(core->prepare_count == 0,
780 "%s already unprepared\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700781 return;
782
Fabio Estevamab525dc2018-01-16 10:50:34 -0200783 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
784 "Unpreparing critical %s\n", core->name))
Lee Jones2e20fbf2016-02-11 13:19:10 -0800785 return;
786
Jerome Brunet9461f7b2018-06-19 15:40:51 +0200787 if (core->flags & CLK_SET_RATE_GATE)
788 clk_core_rate_unprotect(core);
789
Stephen Boydd6968fc2015-04-30 13:54:13 -0700790 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700791 return;
792
Fabio Estevamab525dc2018-01-16 10:50:34 -0200793 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700794
Stephen Boydd6968fc2015-04-30 13:54:13 -0700795 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800796
Stephen Boydd6968fc2015-04-30 13:54:13 -0700797 if (core->ops->unprepare)
798 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700799
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200800 clk_pm_runtime_put(core);
801
Stephen Boydd6968fc2015-04-30 13:54:13 -0700802 trace_clk_unprepare_complete(core);
803 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700804}
805
Dong Aishenga6adc302016-06-30 17:31:11 +0800806static void clk_core_unprepare_lock(struct clk_core *core)
807{
808 clk_prepare_lock();
809 clk_core_unprepare(core);
810 clk_prepare_unlock();
811}
812
Mike Turquetteb24764902012-03-15 23:11:19 -0700813/**
814 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200815 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700816 *
817 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
818 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
819 * if the operation may sleep. One example is a clk which is accessed over
820 * I2c. In the complex case a clk gate operation may require a fast and a slow
821 * part. It is this reason that clk_unprepare and clk_disable are not mutually
822 * exclusive. In fact clk_disable must be called before clk_unprepare.
823 */
824void clk_unprepare(struct clk *clk)
825{
Stephen Boyd63589e92014-03-26 16:06:37 -0700826 if (IS_ERR_OR_NULL(clk))
827 return;
828
Dong Aishenga6adc302016-06-30 17:31:11 +0800829 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700830}
831EXPORT_SYMBOL_GPL(clk_unprepare);
832
Stephen Boydd6968fc2015-04-30 13:54:13 -0700833static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700834{
835 int ret = 0;
836
Stephen Boyda6334722015-05-06 17:00:54 -0700837 lockdep_assert_held(&prepare_lock);
838
Stephen Boydd6968fc2015-04-30 13:54:13 -0700839 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700840 return 0;
841
Stephen Boydd6968fc2015-04-30 13:54:13 -0700842 if (core->prepare_count == 0) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200843 ret = clk_pm_runtime_get(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700844 if (ret)
845 return ret;
846
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200847 ret = clk_core_prepare(core->parent);
848 if (ret)
849 goto runtime_put;
850
Stephen Boydd6968fc2015-04-30 13:54:13 -0700851 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800852
Stephen Boydd6968fc2015-04-30 13:54:13 -0700853 if (core->ops->prepare)
854 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800855
Stephen Boydd6968fc2015-04-30 13:54:13 -0700856 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800857
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200858 if (ret)
859 goto unprepare;
Mike Turquetteb24764902012-03-15 23:11:19 -0700860 }
861
Stephen Boydd6968fc2015-04-30 13:54:13 -0700862 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700863
Jerome Brunet9461f7b2018-06-19 15:40:51 +0200864 /*
865 * CLK_SET_RATE_GATE is a special case of clock protection
866 * Instead of a consumer claiming exclusive rate control, it is
867 * actually the provider which prevents any consumer from making any
868 * operation which could result in a rate change or rate glitch while
869 * the clock is prepared.
870 */
871 if (core->flags & CLK_SET_RATE_GATE)
872 clk_core_rate_protect(core);
873
Mike Turquetteb24764902012-03-15 23:11:19 -0700874 return 0;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200875unprepare:
876 clk_core_unprepare(core->parent);
877runtime_put:
878 clk_pm_runtime_put(core);
879 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700880}
881
Dong Aishenga6adc302016-06-30 17:31:11 +0800882static int clk_core_prepare_lock(struct clk_core *core)
883{
884 int ret;
885
886 clk_prepare_lock();
887 ret = clk_core_prepare(core);
888 clk_prepare_unlock();
889
890 return ret;
891}
892
Mike Turquetteb24764902012-03-15 23:11:19 -0700893/**
894 * clk_prepare - prepare a clock source
895 * @clk: the clk being prepared
896 *
897 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
898 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
899 * operation may sleep. One example is a clk which is accessed over I2c. In
900 * the complex case a clk ungate operation may require a fast and a slow part.
901 * It is this reason that clk_prepare and clk_enable are not mutually
902 * exclusive. In fact clk_prepare must be called before clk_enable.
903 * Returns 0 on success, -EERROR otherwise.
904 */
905int clk_prepare(struct clk *clk)
906{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100907 if (!clk)
908 return 0;
909
Dong Aishenga6adc302016-06-30 17:31:11 +0800910 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700911}
912EXPORT_SYMBOL_GPL(clk_prepare);
913
Stephen Boydd6968fc2015-04-30 13:54:13 -0700914static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700915{
Stephen Boyda6334722015-05-06 17:00:54 -0700916 lockdep_assert_held(&enable_lock);
917
Stephen Boydd6968fc2015-04-30 13:54:13 -0700918 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700919 return;
920
Fabio Estevamab525dc2018-01-16 10:50:34 -0200921 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700922 return;
923
Fabio Estevamab525dc2018-01-16 10:50:34 -0200924 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
925 "Disabling critical %s\n", core->name))
Lee Jones2e20fbf2016-02-11 13:19:10 -0800926 return;
927
Stephen Boydd6968fc2015-04-30 13:54:13 -0700928 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700929 return;
930
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700931 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800932
Stephen Boydd6968fc2015-04-30 13:54:13 -0700933 if (core->ops->disable)
934 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700935
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700936 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800937
Stephen Boydd6968fc2015-04-30 13:54:13 -0700938 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100939}
940
Dong Aishenga6adc302016-06-30 17:31:11 +0800941static void clk_core_disable_lock(struct clk_core *core)
942{
943 unsigned long flags;
944
945 flags = clk_enable_lock();
946 clk_core_disable(core);
947 clk_enable_unlock(flags);
948}
949
Mike Turquetteb24764902012-03-15 23:11:19 -0700950/**
951 * clk_disable - gate a clock
952 * @clk: the clk being gated
953 *
954 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
955 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
956 * clk if the operation is fast and will never sleep. One example is a
957 * SoC-internal clk which is controlled via simple register writes. In the
958 * complex case a clk gate operation may require a fast and a slow part. It is
959 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
960 * In fact clk_disable must be called before clk_unprepare.
961 */
962void clk_disable(struct clk *clk)
963{
Stephen Boyd63589e92014-03-26 16:06:37 -0700964 if (IS_ERR_OR_NULL(clk))
965 return;
966
Dong Aishenga6adc302016-06-30 17:31:11 +0800967 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700968}
969EXPORT_SYMBOL_GPL(clk_disable);
970
Stephen Boydd6968fc2015-04-30 13:54:13 -0700971static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700972{
973 int ret = 0;
974
Stephen Boyda6334722015-05-06 17:00:54 -0700975 lockdep_assert_held(&enable_lock);
976
Stephen Boydd6968fc2015-04-30 13:54:13 -0700977 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700978 return 0;
979
Fabio Estevamab525dc2018-01-16 10:50:34 -0200980 if (WARN(core->prepare_count == 0,
981 "Enabling unprepared %s\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700982 return -ESHUTDOWN;
983
Stephen Boydd6968fc2015-04-30 13:54:13 -0700984 if (core->enable_count == 0) {
985 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700986
987 if (ret)
988 return ret;
989
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700990 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800991
Stephen Boydd6968fc2015-04-30 13:54:13 -0700992 if (core->ops->enable)
993 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800994
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700995 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800996
997 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700998 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800999 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001000 }
1001 }
1002
Stephen Boydd6968fc2015-04-30 13:54:13 -07001003 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07001004 return 0;
1005}
1006
Dong Aishenga6adc302016-06-30 17:31:11 +08001007static int clk_core_enable_lock(struct clk_core *core)
1008{
1009 unsigned long flags;
1010 int ret;
1011
1012 flags = clk_enable_lock();
1013 ret = clk_core_enable(core);
1014 clk_enable_unlock(flags);
1015
1016 return ret;
1017}
1018
Keerthy43536542018-09-04 12:19:36 +05301019/**
1020 * clk_gate_restore_context - restore context for poweroff
1021 * @hw: the clk_hw pointer of clock whose state is to be restored
1022 *
1023 * The clock gate restore context function enables or disables
1024 * the gate clocks based on the enable_count. This is done in cases
1025 * where the clock context is lost and based on the enable_count
1026 * the clock either needs to be enabled/disabled. This
1027 * helps restore the state of gate clocks.
1028 */
1029void clk_gate_restore_context(struct clk_hw *hw)
1030{
Stephen Boyd9be76622018-10-11 09:28:13 -07001031 struct clk_core *core = hw->core;
1032
1033 if (core->enable_count)
1034 core->ops->enable(hw);
Keerthy43536542018-09-04 12:19:36 +05301035 else
Stephen Boyd9be76622018-10-11 09:28:13 -07001036 core->ops->disable(hw);
Keerthy43536542018-09-04 12:19:36 +05301037}
1038EXPORT_SYMBOL_GPL(clk_gate_restore_context);
1039
Stephen Boyd9be76622018-10-11 09:28:13 -07001040static int clk_core_save_context(struct clk_core *core)
Russ Dill8b95d1c2018-09-04 12:19:35 +05301041{
1042 struct clk_core *child;
1043 int ret = 0;
1044
Stephen Boyd9be76622018-10-11 09:28:13 -07001045 hlist_for_each_entry(child, &core->children, child_node) {
1046 ret = clk_core_save_context(child);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301047 if (ret < 0)
1048 return ret;
1049 }
1050
Stephen Boyd9be76622018-10-11 09:28:13 -07001051 if (core->ops && core->ops->save_context)
1052 ret = core->ops->save_context(core->hw);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301053
1054 return ret;
1055}
1056
Stephen Boyd9be76622018-10-11 09:28:13 -07001057static void clk_core_restore_context(struct clk_core *core)
Russ Dill8b95d1c2018-09-04 12:19:35 +05301058{
1059 struct clk_core *child;
1060
Stephen Boyd9be76622018-10-11 09:28:13 -07001061 if (core->ops && core->ops->restore_context)
1062 core->ops->restore_context(core->hw);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301063
Stephen Boyd9be76622018-10-11 09:28:13 -07001064 hlist_for_each_entry(child, &core->children, child_node)
1065 clk_core_restore_context(child);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301066}
1067
1068/**
1069 * clk_save_context - save clock context for poweroff
1070 *
1071 * Saves the context of the clock register for powerstates in which the
1072 * contents of the registers will be lost. Occurs deep within the suspend
1073 * code. Returns 0 on success.
1074 */
1075int clk_save_context(void)
1076{
1077 struct clk_core *clk;
1078 int ret;
1079
1080 hlist_for_each_entry(clk, &clk_root_list, child_node) {
Stephen Boyd9be76622018-10-11 09:28:13 -07001081 ret = clk_core_save_context(clk);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301082 if (ret < 0)
1083 return ret;
1084 }
1085
1086 hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
Stephen Boyd9be76622018-10-11 09:28:13 -07001087 ret = clk_core_save_context(clk);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301088 if (ret < 0)
1089 return ret;
1090 }
1091
1092 return 0;
1093}
1094EXPORT_SYMBOL_GPL(clk_save_context);
1095
1096/**
1097 * clk_restore_context - restore clock context after poweroff
1098 *
1099 * Restore the saved clock context upon resume.
1100 *
1101 */
1102void clk_restore_context(void)
1103{
Stephen Boyd9be76622018-10-11 09:28:13 -07001104 struct clk_core *core;
Russ Dill8b95d1c2018-09-04 12:19:35 +05301105
Stephen Boyd9be76622018-10-11 09:28:13 -07001106 hlist_for_each_entry(core, &clk_root_list, child_node)
1107 clk_core_restore_context(core);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301108
Stephen Boyd9be76622018-10-11 09:28:13 -07001109 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1110 clk_core_restore_context(core);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301111}
1112EXPORT_SYMBOL_GPL(clk_restore_context);
1113
Mike Turquetteb24764902012-03-15 23:11:19 -07001114/**
1115 * clk_enable - ungate a clock
1116 * @clk: the clk being ungated
1117 *
1118 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1119 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1120 * if the operation will never sleep. One example is a SoC-internal clk which
1121 * is controlled via simple register writes. In the complex case a clk ungate
1122 * operation may require a fast and a slow part. It is this reason that
1123 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1124 * must be called before clk_enable. Returns 0 on success, -EERROR
1125 * otherwise.
1126 */
1127int clk_enable(struct clk *clk)
1128{
Dong Aisheng864e1602015-04-30 14:02:19 -07001129 if (!clk)
1130 return 0;
1131
Dong Aishenga6adc302016-06-30 17:31:11 +08001132 return clk_core_enable_lock(clk->core);
1133}
1134EXPORT_SYMBOL_GPL(clk_enable);
1135
1136static int clk_core_prepare_enable(struct clk_core *core)
1137{
1138 int ret;
1139
1140 ret = clk_core_prepare_lock(core);
1141 if (ret)
1142 return ret;
1143
1144 ret = clk_core_enable_lock(core);
1145 if (ret)
1146 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001147
1148 return ret;
1149}
Dong Aishenga6adc302016-06-30 17:31:11 +08001150
1151static void clk_core_disable_unprepare(struct clk_core *core)
1152{
1153 clk_core_disable_lock(core);
1154 clk_core_unprepare_lock(core);
1155}
Mike Turquetteb24764902012-03-15 23:11:19 -07001156
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001157static void clk_unprepare_unused_subtree(struct clk_core *core)
1158{
1159 struct clk_core *child;
1160
1161 lockdep_assert_held(&prepare_lock);
1162
1163 hlist_for_each_entry(child, &core->children, child_node)
1164 clk_unprepare_unused_subtree(child);
1165
1166 if (core->prepare_count)
1167 return;
1168
1169 if (core->flags & CLK_IGNORE_UNUSED)
1170 return;
1171
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001172 if (clk_pm_runtime_get(core))
1173 return;
1174
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001175 if (clk_core_is_prepared(core)) {
1176 trace_clk_unprepare(core);
1177 if (core->ops->unprepare_unused)
1178 core->ops->unprepare_unused(core->hw);
1179 else if (core->ops->unprepare)
1180 core->ops->unprepare(core->hw);
1181 trace_clk_unprepare_complete(core);
1182 }
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001183
1184 clk_pm_runtime_put(core);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001185}
1186
1187static void clk_disable_unused_subtree(struct clk_core *core)
1188{
1189 struct clk_core *child;
1190 unsigned long flags;
1191
1192 lockdep_assert_held(&prepare_lock);
1193
1194 hlist_for_each_entry(child, &core->children, child_node)
1195 clk_disable_unused_subtree(child);
1196
Dong Aishenga4b35182016-06-30 17:31:13 +08001197 if (core->flags & CLK_OPS_PARENT_ENABLE)
1198 clk_core_prepare_enable(core->parent);
1199
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001200 if (clk_pm_runtime_get(core))
1201 goto unprepare_out;
1202
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001203 flags = clk_enable_lock();
1204
1205 if (core->enable_count)
1206 goto unlock_out;
1207
1208 if (core->flags & CLK_IGNORE_UNUSED)
1209 goto unlock_out;
1210
1211 /*
1212 * some gate clocks have special needs during the disable-unused
1213 * sequence. call .disable_unused if available, otherwise fall
1214 * back to .disable
1215 */
1216 if (clk_core_is_enabled(core)) {
1217 trace_clk_disable(core);
1218 if (core->ops->disable_unused)
1219 core->ops->disable_unused(core->hw);
1220 else if (core->ops->disable)
1221 core->ops->disable(core->hw);
1222 trace_clk_disable_complete(core);
1223 }
1224
1225unlock_out:
1226 clk_enable_unlock(flags);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001227 clk_pm_runtime_put(core);
1228unprepare_out:
Dong Aishenga4b35182016-06-30 17:31:13 +08001229 if (core->flags & CLK_OPS_PARENT_ENABLE)
1230 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001231}
1232
1233static bool clk_ignore_unused;
1234static int __init clk_ignore_unused_setup(char *__unused)
1235{
1236 clk_ignore_unused = true;
1237 return 1;
1238}
1239__setup("clk_ignore_unused", clk_ignore_unused_setup);
1240
1241static int clk_disable_unused(void)
1242{
1243 struct clk_core *core;
1244
1245 if (clk_ignore_unused) {
1246 pr_warn("clk: Not disabling unused clocks\n");
1247 return 0;
1248 }
1249
1250 clk_prepare_lock();
1251
1252 hlist_for_each_entry(core, &clk_root_list, child_node)
1253 clk_disable_unused_subtree(core);
1254
1255 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1256 clk_disable_unused_subtree(core);
1257
1258 hlist_for_each_entry(core, &clk_root_list, child_node)
1259 clk_unprepare_unused_subtree(core);
1260
1261 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1262 clk_unprepare_unused_subtree(core);
1263
1264 clk_prepare_unlock();
1265
1266 return 0;
1267}
1268late_initcall_sync(clk_disable_unused);
1269
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001270static int clk_core_determine_round_nolock(struct clk_core *core,
1271 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001272{
Boris Brezillon0817b622015-07-07 20:48:08 +02001273 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001274
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001275 lockdep_assert_held(&prepare_lock);
1276
Stephen Boydd6968fc2015-04-30 13:54:13 -07001277 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001278 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001279
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001280 /*
1281 * At this point, core protection will be disabled if
1282 * - if the provider is not protected at all
1283 * - if the calling consumer is the only one which has exclusivity
1284 * over the provider
1285 */
Jerome Brunete55a8392017-12-01 22:51:56 +01001286 if (clk_core_rate_is_protected(core)) {
1287 req->rate = core->rate;
1288 } else if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001289 return core->ops->determine_rate(core->hw, req);
1290 } else if (core->ops->round_rate) {
1291 rate = core->ops->round_rate(core->hw, req->rate,
1292 &req->best_parent_rate);
1293 if (rate < 0)
1294 return rate;
1295
1296 req->rate = rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001297 } else {
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001298 return -EINVAL;
Boris Brezillon0817b622015-07-07 20:48:08 +02001299 }
1300
1301 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001302}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001303
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001304static void clk_core_init_rate_req(struct clk_core * const core,
1305 struct clk_rate_request *req)
1306{
1307 struct clk_core *parent;
1308
1309 if (WARN_ON(!core || !req))
1310 return;
1311
Mike Turquetteb24764902012-03-15 23:11:19 -07001312 parent = core->parent;
1313 if (parent) {
1314 req->best_parent_hw = parent->hw;
1315 req->best_parent_rate = parent->rate;
1316 } else {
1317 req->best_parent_hw = NULL;
1318 req->best_parent_rate = 0;
1319 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001320}
Mike Turquetteb24764902012-03-15 23:11:19 -07001321
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001322static bool clk_core_can_round(struct clk_core * const core)
1323{
1324 if (core->ops->determine_rate || core->ops->round_rate)
1325 return true;
Mike Turquetteb24764902012-03-15 23:11:19 -07001326
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001327 return false;
1328}
Mike Turquetteb24764902012-03-15 23:11:19 -07001329
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001330static int clk_core_round_rate_nolock(struct clk_core *core,
1331 struct clk_rate_request *req)
1332{
1333 lockdep_assert_held(&prepare_lock);
1334
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001335 if (!core) {
1336 req->rate = 0;
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001337 return 0;
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001338 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001339
1340 clk_core_init_rate_req(core, req);
1341
1342 if (clk_core_can_round(core))
1343 return clk_core_determine_round_nolock(core, req);
1344 else if (core->flags & CLK_SET_RATE_PARENT)
1345 return clk_core_round_rate_nolock(core->parent, req);
1346
1347 req->rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001348 return 0;
1349}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001350
1351/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001352 * __clk_determine_rate - get the closest rate actually supported by a clock
1353 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001354 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001355 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001356 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001357 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001358int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001359{
Boris Brezillon0817b622015-07-07 20:48:08 +02001360 if (!hw) {
1361 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001362 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001363 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001364
Boris Brezillon0817b622015-07-07 20:48:08 +02001365 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001366}
1367EXPORT_SYMBOL_GPL(__clk_determine_rate);
1368
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001369unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1370{
1371 int ret;
1372 struct clk_rate_request req;
1373
1374 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1375 req.rate = rate;
1376
1377 ret = clk_core_round_rate_nolock(hw->core, &req);
1378 if (ret)
1379 return 0;
1380
1381 return req.rate;
1382}
1383EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1384
Mike Turquetteb24764902012-03-15 23:11:19 -07001385/**
1386 * clk_round_rate - round the given rate for a clk
1387 * @clk: the clk for which we are rounding a rate
1388 * @rate: the rate which is to be rounded
1389 *
1390 * Takes in a rate as input and rounds it to a rate that the clk can actually
1391 * use which is then returned. If clk doesn't support round_rate operation
1392 * then the parent rate is returned.
1393 */
1394long clk_round_rate(struct clk *clk, unsigned long rate)
1395{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001396 struct clk_rate_request req;
1397 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001398
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001399 if (!clk)
1400 return 0;
1401
Mike Turquetteeab89f62013-03-28 13:59:01 -07001402 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001403
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001404 if (clk->exclusive_count)
1405 clk_core_rate_unprotect(clk->core);
1406
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001407 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1408 req.rate = rate;
1409
1410 ret = clk_core_round_rate_nolock(clk->core, &req);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001411
1412 if (clk->exclusive_count)
1413 clk_core_rate_protect(clk->core);
1414
Mike Turquetteeab89f62013-03-28 13:59:01 -07001415 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001416
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001417 if (ret)
1418 return ret;
1419
1420 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001421}
1422EXPORT_SYMBOL_GPL(clk_round_rate);
1423
1424/**
1425 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001426 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001427 * @msg: clk notifier type (see include/linux/clk.h)
1428 * @old_rate: old clk rate
1429 * @new_rate: new clk rate
1430 *
1431 * Triggers a notifier call chain on the clk rate-change notification
1432 * for 'clk'. Passes a pointer to the struct clk and the previous
1433 * and current rates to the notifier callback. Intended to be called by
1434 * internal clock code only. Returns NOTIFY_DONE from the last driver
1435 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1436 * a driver returns that.
1437 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001438static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001439 unsigned long old_rate, unsigned long new_rate)
1440{
1441 struct clk_notifier *cn;
1442 struct clk_notifier_data cnd;
1443 int ret = NOTIFY_DONE;
1444
Mike Turquetteb24764902012-03-15 23:11:19 -07001445 cnd.old_rate = old_rate;
1446 cnd.new_rate = new_rate;
1447
1448 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001449 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001450 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001451 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1452 &cnd);
Peter De Schrijver17c34c52017-03-21 12:16:26 +02001453 if (ret & NOTIFY_STOP_MASK)
1454 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001455 }
1456 }
1457
1458 return ret;
1459}
1460
1461/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001462 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001463 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001464 *
1465 * Walks the subtree of clks starting with clk and recalculates accuracies as
1466 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001467 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001468 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001469 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001470static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001471{
1472 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001473 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001474
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001475 lockdep_assert_held(&prepare_lock);
1476
Stephen Boydd6968fc2015-04-30 13:54:13 -07001477 if (core->parent)
1478 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001479
Stephen Boydd6968fc2015-04-30 13:54:13 -07001480 if (core->ops->recalc_accuracy)
1481 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001482 parent_accuracy);
1483 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001484 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001485
Stephen Boydd6968fc2015-04-30 13:54:13 -07001486 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001487 __clk_recalc_accuracies(child);
1488}
1489
Stephen Boydd6968fc2015-04-30 13:54:13 -07001490static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001491{
1492 unsigned long accuracy;
1493
1494 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001495 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1496 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001497
Stephen Boydd6968fc2015-04-30 13:54:13 -07001498 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001499 clk_prepare_unlock();
1500
1501 return accuracy;
1502}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001503
1504/**
1505 * clk_get_accuracy - return the accuracy of clk
1506 * @clk: the clk whose accuracy is being returned
1507 *
1508 * Simply returns the cached accuracy of the clk, unless
1509 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1510 * issued.
1511 * If clk is NULL then returns 0.
1512 */
1513long clk_get_accuracy(struct clk *clk)
1514{
1515 if (!clk)
1516 return 0;
1517
1518 return clk_core_get_accuracy(clk->core);
1519}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001520EXPORT_SYMBOL_GPL(clk_get_accuracy);
1521
Stephen Boydd6968fc2015-04-30 13:54:13 -07001522static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001523 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001524{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001525 unsigned long rate = parent_rate;
1526
1527 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1528 rate = core->ops->recalc_rate(core->hw, parent_rate);
1529 clk_pm_runtime_put(core);
1530 }
1531 return rate;
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001532}
1533
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001534/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001535 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001536 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001537 * @msg: notification type (see include/linux/clk.h)
1538 *
1539 * Walks the subtree of clks starting with clk and recalculates rates as it
1540 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001541 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001542 *
1543 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1544 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001545 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001546static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001547{
1548 unsigned long old_rate;
1549 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001550 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001551
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001552 lockdep_assert_held(&prepare_lock);
1553
Stephen Boydd6968fc2015-04-30 13:54:13 -07001554 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001555
Stephen Boydd6968fc2015-04-30 13:54:13 -07001556 if (core->parent)
1557 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001558
Stephen Boydd6968fc2015-04-30 13:54:13 -07001559 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001560
1561 /*
1562 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1563 * & ABORT_RATE_CHANGE notifiers
1564 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001565 if (core->notifier_count && msg)
1566 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001567
Stephen Boydd6968fc2015-04-30 13:54:13 -07001568 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001569 __clk_recalc_rates(child, msg);
1570}
1571
Stephen Boydd6968fc2015-04-30 13:54:13 -07001572static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001573{
1574 unsigned long rate;
1575
1576 clk_prepare_lock();
1577
Stephen Boydd6968fc2015-04-30 13:54:13 -07001578 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1579 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001580
Stephen Boydd6968fc2015-04-30 13:54:13 -07001581 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001582 clk_prepare_unlock();
1583
1584 return rate;
1585}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001586
Mike Turquetteb24764902012-03-15 23:11:19 -07001587/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001588 * clk_get_rate - return the rate of clk
1589 * @clk: the clk whose rate is being returned
1590 *
1591 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1592 * is set, which means a recalc_rate will be issued.
1593 * If clk is NULL then returns 0.
1594 */
1595unsigned long clk_get_rate(struct clk *clk)
1596{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001597 if (!clk)
1598 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001599
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001600 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001601}
1602EXPORT_SYMBOL_GPL(clk_get_rate);
1603
Stephen Boydd6968fc2015-04-30 13:54:13 -07001604static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001605 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001606{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001607 int i;
James Hogan4935b222013-07-29 12:24:59 +01001608
Masahiro Yamada508f8842015-12-28 19:23:08 +09001609 if (!parent)
1610 return -EINVAL;
1611
Derek Basehoreede77852018-12-20 16:31:00 -08001612 for (i = 0; i < core->num_parents; i++) {
Stephen Boydfc0c2092019-04-12 11:31:47 -07001613 if (core->parents[i].core == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001614 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001615
Stephen Boydfc0c2092019-04-12 11:31:47 -07001616 if (core->parents[i].core)
Derek Basehoreede77852018-12-20 16:31:00 -08001617 continue;
1618
1619 /* Fallback to comparing globally unique names */
Stephen Boydfc0c2092019-04-12 11:31:47 -07001620 if (!strcmp(parent->name, core->parents[i].name)) {
1621 core->parents[i].core = parent;
Derek Basehoreede77852018-12-20 16:31:00 -08001622 return i;
1623 }
1624 }
1625
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001626 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001627}
1628
Heiko Stuebnere6500342015-04-22 22:53:05 +02001629/*
1630 * Update the orphan status of @core and all its children.
1631 */
1632static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1633{
1634 struct clk_core *child;
1635
1636 core->orphan = is_orphan;
1637
1638 hlist_for_each_entry(child, &core->children, child_node)
1639 clk_core_update_orphan_status(child, is_orphan);
1640}
1641
Stephen Boydd6968fc2015-04-30 13:54:13 -07001642static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001643{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001644 bool was_orphan = core->orphan;
1645
Stephen Boydd6968fc2015-04-30 13:54:13 -07001646 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001647
James Hogan903efc52013-08-29 12:10:51 +01001648 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001649 bool becomes_orphan = new_parent->orphan;
1650
James Hogan903efc52013-08-29 12:10:51 +01001651 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001652 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001653 new_parent->new_child = NULL;
1654
Stephen Boydd6968fc2015-04-30 13:54:13 -07001655 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001656
1657 if (was_orphan != becomes_orphan)
1658 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001659 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001660 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001661 if (!was_orphan)
1662 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001663 }
James Hogan4935b222013-07-29 12:24:59 +01001664
Stephen Boydd6968fc2015-04-30 13:54:13 -07001665 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001666}
1667
Stephen Boydd6968fc2015-04-30 13:54:13 -07001668static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001669 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001670{
1671 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001672 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001673
1674 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001675 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1676 *
1677 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001678 * clk_enable().
1679 *
1680 * If the clock is not prepared, then a race with
1681 * clk_enable/disable() is impossible since we already have the
1682 * prepare lock (future calls to clk_enable() need to be preceded by
1683 * a clk_prepare()).
1684 *
1685 * If the clock is prepared, migrate the prepared state to the new
1686 * parent and also protect against a race with clk_enable() by
1687 * forcing the clock and the new parent on. This ensures that all
1688 * future calls to clk_enable() are practically NOPs with respect to
1689 * hardware and software states.
1690 *
1691 * See also: Comment for clk_set_parent() below.
1692 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001693
1694 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1695 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1696 clk_core_prepare_enable(old_parent);
1697 clk_core_prepare_enable(parent);
1698 }
1699
1700 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001701 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001702 clk_core_prepare_enable(parent);
1703 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001704 }
1705
1706 /* update the clk tree topology */
1707 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001708 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001709 clk_enable_unlock(flags);
1710
Stephen Boyd3fa22522014-01-15 10:47:22 -08001711 return old_parent;
1712}
1713
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001714static void __clk_set_parent_after(struct clk_core *core,
1715 struct clk_core *parent,
1716 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001717{
1718 /*
1719 * Finish the migration of prepare state and undo the changes done
1720 * for preventing a race with clk_enable().
1721 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001722 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001723 clk_core_disable_lock(core);
1724 clk_core_disable_unprepare(old_parent);
1725 }
1726
1727 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1728 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1729 clk_core_disable_unprepare(parent);
1730 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001731 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001732}
1733
Stephen Boydd6968fc2015-04-30 13:54:13 -07001734static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001735 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001736{
1737 unsigned long flags;
1738 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001739 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001740
Stephen Boydd6968fc2015-04-30 13:54:13 -07001741 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001742
Stephen Boydd6968fc2015-04-30 13:54:13 -07001743 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001744
James Hogan4935b222013-07-29 12:24:59 +01001745 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001746 if (parent && core->ops->set_parent)
1747 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001748
Stephen Boydd6968fc2015-04-30 13:54:13 -07001749 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001750
James Hogan4935b222013-07-29 12:24:59 +01001751 if (ret) {
1752 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001753 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001754 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001755 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001756
James Hogan4935b222013-07-29 12:24:59 +01001757 return ret;
1758 }
1759
Stephen Boydd6968fc2015-04-30 13:54:13 -07001760 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001761
James Hogan4935b222013-07-29 12:24:59 +01001762 return 0;
1763}
1764
Ulf Hanssona093bde2012-08-31 14:21:28 +02001765/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001766 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001767 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001768 * @parent_rate: the "future" rate of clk's parent
1769 *
1770 * Walks the subtree of clks starting with clk, speculating rates as it
1771 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1772 *
1773 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1774 * pre-rate change notifications and returns early if no clks in the
1775 * subtree have subscribed to the notifications. Note that if a clk does not
1776 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001777 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001778 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001779static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001780 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001781{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001782 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001783 unsigned long new_rate;
1784 int ret = NOTIFY_DONE;
1785
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001786 lockdep_assert_held(&prepare_lock);
1787
Stephen Boydd6968fc2015-04-30 13:54:13 -07001788 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001789
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001790 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001791 if (core->notifier_count)
1792 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001793
Mike Turquette86bcfa22014-02-24 16:08:41 -08001794 if (ret & NOTIFY_STOP_MASK) {
1795 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001796 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001797 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001798 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001799
Stephen Boydd6968fc2015-04-30 13:54:13 -07001800 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001801 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001802 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001803 break;
1804 }
1805
1806out:
1807 return ret;
1808}
1809
Stephen Boydd6968fc2015-04-30 13:54:13 -07001810static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001811 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001812{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001813 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001814
Stephen Boydd6968fc2015-04-30 13:54:13 -07001815 core->new_rate = new_rate;
1816 core->new_parent = new_parent;
1817 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001818 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001819 core->new_child = NULL;
1820 if (new_parent && new_parent != core->parent)
1821 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001822
Stephen Boydd6968fc2015-04-30 13:54:13 -07001823 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001824 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001825 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001826 }
1827}
1828
1829/*
1830 * calculate the new rates returning the topmost clock that has to be
1831 * changed.
1832 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001833static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001834 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001835{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001836 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001837 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001838 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001839 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001840 unsigned long min_rate;
1841 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001842 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001843 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001844
Mike Turquette7452b212012-03-26 14:45:36 -07001845 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001846 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001847 return NULL;
1848
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001849 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001850 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001851 if (parent)
1852 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001853
Stephen Boydd6968fc2015-04-30 13:54:13 -07001854 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001855
James Hogan71472c02013-07-29 12:25:00 +01001856 /* find the closest rate and parent clk/rate */
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001857 if (clk_core_can_round(core)) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001858 struct clk_rate_request req;
1859
1860 req.rate = rate;
1861 req.min_rate = min_rate;
1862 req.max_rate = max_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001863
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001864 clk_core_init_rate_req(core, &req);
1865
1866 ret = clk_core_determine_round_nolock(core, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001867 if (ret < 0)
1868 return NULL;
1869
Boris Brezillon0817b622015-07-07 20:48:08 +02001870 best_parent_rate = req.best_parent_rate;
1871 new_rate = req.rate;
1872 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001873
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001874 if (new_rate < min_rate || new_rate > max_rate)
1875 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001876 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001877 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001878 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001879 return NULL;
1880 } else {
1881 /* pass-through clock with adjustable parent */
1882 top = clk_calc_new_rates(parent, rate);
1883 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001884 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001885 }
1886
James Hogan71472c02013-07-29 12:25:00 +01001887 /* some clocks must be gated to change parent */
1888 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001889 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001890 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001891 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001892 return NULL;
1893 }
1894
James Hogan71472c02013-07-29 12:25:00 +01001895 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001896 if (parent && core->num_parents > 1) {
1897 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001898 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001899 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001900 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001901 return NULL;
1902 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001903 }
1904
Stephen Boydd6968fc2015-04-30 13:54:13 -07001905 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001906 best_parent_rate != parent->rate)
1907 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001908
1909out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001910 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001911
1912 return top;
1913}
1914
1915/*
1916 * Notify about rate changes in a subtree. Always walk down the whole tree
1917 * so that in case of an error we can walk down the whole tree again and
1918 * abort the change.
1919 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001920static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001921 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001922{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001923 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001924 int ret = NOTIFY_DONE;
1925
Stephen Boydd6968fc2015-04-30 13:54:13 -07001926 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301927 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001928
Stephen Boydd6968fc2015-04-30 13:54:13 -07001929 if (core->notifier_count) {
1930 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001931 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001932 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001933 }
1934
Stephen Boydd6968fc2015-04-30 13:54:13 -07001935 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001936 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001937 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001938 continue;
1939 tmp_clk = clk_propagate_rate_change(child, event);
1940 if (tmp_clk)
1941 fail_clk = tmp_clk;
1942 }
1943
Stephen Boydd6968fc2015-04-30 13:54:13 -07001944 /* handle the new child who might not be in core->children yet */
1945 if (core->new_child) {
1946 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001947 if (tmp_clk)
1948 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001949 }
1950
1951 return fail_clk;
1952}
1953
1954/*
1955 * walk down a subtree and set the new rates notifying the rate
1956 * change on the way
1957 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001958static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001959{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001960 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001961 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001962 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001963 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001964 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001965 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001966 struct clk_core *parent = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001967
Stephen Boydd6968fc2015-04-30 13:54:13 -07001968 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001969
Dong Aishengfc8726a2016-06-30 17:31:14 +08001970 if (core->new_parent) {
1971 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001972 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001973 } else if (core->parent) {
1974 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001975 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001976 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001977
Marek Szyprowski588fb542017-11-30 13:14:51 +01001978 if (clk_pm_runtime_get(core))
1979 return;
1980
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001981 if (core->flags & CLK_SET_RATE_UNGATE) {
1982 unsigned long flags;
1983
1984 clk_core_prepare(core);
1985 flags = clk_enable_lock();
1986 clk_core_enable(core);
1987 clk_enable_unlock(flags);
1988 }
1989
Stephen Boydd6968fc2015-04-30 13:54:13 -07001990 if (core->new_parent && core->new_parent != core->parent) {
1991 old_parent = __clk_set_parent_before(core, core->new_parent);
1992 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001993
Stephen Boydd6968fc2015-04-30 13:54:13 -07001994 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001995 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001996 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001997 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001998 core->new_parent_index);
1999 } else if (core->ops->set_parent) {
2000 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002001 }
2002
Stephen Boydd6968fc2015-04-30 13:54:13 -07002003 trace_clk_set_parent_complete(core, core->new_parent);
2004 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002005 }
2006
Dong Aishengfc8726a2016-06-30 17:31:14 +08002007 if (core->flags & CLK_OPS_PARENT_ENABLE)
2008 clk_core_prepare_enable(parent);
2009
Stephen Boydd6968fc2015-04-30 13:54:13 -07002010 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002011
Stephen Boydd6968fc2015-04-30 13:54:13 -07002012 if (!skip_set_rate && core->ops->set_rate)
2013 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002014
Stephen Boydd6968fc2015-04-30 13:54:13 -07002015 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002016
Stephen Boydd6968fc2015-04-30 13:54:13 -07002017 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002018
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01002019 if (core->flags & CLK_SET_RATE_UNGATE) {
2020 unsigned long flags;
2021
2022 flags = clk_enable_lock();
2023 clk_core_disable(core);
2024 clk_enable_unlock(flags);
2025 clk_core_unprepare(core);
2026 }
2027
Dong Aishengfc8726a2016-06-30 17:31:14 +08002028 if (core->flags & CLK_OPS_PARENT_ENABLE)
2029 clk_core_disable_unprepare(parent);
2030
Stephen Boydd6968fc2015-04-30 13:54:13 -07002031 if (core->notifier_count && old_rate != core->rate)
2032 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002033
Michael Turquette85e88fa2015-06-20 12:18:03 -07002034 if (core->flags & CLK_RECALC_NEW_RATES)
2035 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02002036
Tero Kristo067bb172014-08-21 16:47:45 +03002037 /*
2038 * Use safe iteration, as change_rate can actually swap parents
2039 * for certain clock types.
2040 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002041 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01002042 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002043 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01002044 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07002045 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01002046 }
2047
Stephen Boydd6968fc2015-04-30 13:54:13 -07002048 /* handle the new child who might not be in core->children yet */
2049 if (core->new_child)
2050 clk_change_rate(core->new_child);
Marek Szyprowski588fb542017-11-30 13:14:51 +01002051
2052 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002053}
2054
Jerome Brunetca5e0892017-12-01 22:51:55 +01002055static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
2056 unsigned long req_rate)
2057{
Jerome Brunete55a8392017-12-01 22:51:56 +01002058 int ret, cnt;
Jerome Brunetca5e0892017-12-01 22:51:55 +01002059 struct clk_rate_request req;
2060
2061 lockdep_assert_held(&prepare_lock);
2062
2063 if (!core)
2064 return 0;
2065
Jerome Brunete55a8392017-12-01 22:51:56 +01002066 /* simulate what the rate would be if it could be freely set */
2067 cnt = clk_core_rate_nuke_protect(core);
2068 if (cnt < 0)
2069 return cnt;
2070
Jerome Brunetca5e0892017-12-01 22:51:55 +01002071 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
2072 req.rate = req_rate;
2073
2074 ret = clk_core_round_rate_nolock(core, &req);
2075
Jerome Brunete55a8392017-12-01 22:51:56 +01002076 /* restore the protection */
2077 clk_core_rate_restore_protect(core, cnt);
2078
Jerome Brunetca5e0892017-12-01 22:51:55 +01002079 return ret ? 0 : req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002080}
2081
Stephen Boydd6968fc2015-04-30 13:54:13 -07002082static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002083 unsigned long req_rate)
2084{
2085 struct clk_core *top, *fail_clk;
Jerome Brunetca5e0892017-12-01 22:51:55 +01002086 unsigned long rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002087 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002088
Stephen Boydd6968fc2015-04-30 13:54:13 -07002089 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002090 return 0;
2091
Jerome Brunetca5e0892017-12-01 22:51:55 +01002092 rate = clk_core_req_round_rate_nolock(core, req_rate);
2093
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002094 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002095 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002096 return 0;
2097
Jerome Brunete55a8392017-12-01 22:51:56 +01002098 /* fail on a direct rate set of a protected provider */
2099 if (clk_core_rate_is_protected(core))
2100 return -EBUSY;
2101
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002102 /* calculate new rates and get the topmost changed clock */
Jerome Brunetca5e0892017-12-01 22:51:55 +01002103 top = clk_calc_new_rates(core, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002104 if (!top)
2105 return -EINVAL;
2106
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002107 ret = clk_pm_runtime_get(core);
2108 if (ret)
2109 return ret;
2110
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002111 /* notify that we are about to change rates */
2112 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
2113 if (fail_clk) {
2114 pr_debug("%s: failed to set %s rate\n", __func__,
2115 fail_clk->name);
2116 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002117 ret = -EBUSY;
2118 goto err;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002119 }
2120
2121 /* change the rates */
2122 clk_change_rate(top);
2123
Stephen Boydd6968fc2015-04-30 13:54:13 -07002124 core->req_rate = req_rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002125err:
2126 clk_pm_runtime_put(core);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002127
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002128 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002129}
2130
Mike Turquetteb24764902012-03-15 23:11:19 -07002131/**
2132 * clk_set_rate - specify a new rate for clk
2133 * @clk: the clk whose rate is being changed
2134 * @rate: the new rate for clk
2135 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002136 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07002137 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002138 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2139 * propagate up to clk's parent; whether or not this happens depends on the
2140 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2141 * after calling .round_rate then upstream parent propagation is ignored. If
2142 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002143 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07002144 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2145 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07002146 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002147 * Rate changes are accomplished via tree traversal that also recalculates the
2148 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07002149 *
2150 * Returns 0 on success, -EERROR otherwise.
2151 */
2152int clk_set_rate(struct clk *clk, unsigned long rate)
2153{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002154 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002155
Mike Turquette89ac8d72013-08-21 23:58:09 -07002156 if (!clk)
2157 return 0;
2158
Mike Turquetteb24764902012-03-15 23:11:19 -07002159 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002160 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002161
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002162 if (clk->exclusive_count)
2163 clk_core_rate_unprotect(clk->core);
2164
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002165 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002166
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002167 if (clk->exclusive_count)
2168 clk_core_rate_protect(clk->core);
2169
Mike Turquetteeab89f62013-03-28 13:59:01 -07002170 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002171
2172 return ret;
2173}
2174EXPORT_SYMBOL_GPL(clk_set_rate);
2175
2176/**
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002177 * clk_set_rate_exclusive - specify a new rate get exclusive control
2178 * @clk: the clk whose rate is being changed
2179 * @rate: the new rate for clk
2180 *
2181 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2182 * within a critical section
2183 *
2184 * This can be used initially to ensure that at least 1 consumer is
2185 * statisfied when several consumers are competing for exclusivity over the
2186 * same clock provider.
2187 *
2188 * The exclusivity is not applied if setting the rate failed.
2189 *
2190 * Calls to clk_rate_exclusive_get() should be balanced with calls to
2191 * clk_rate_exclusive_put().
2192 *
2193 * Returns 0 on success, -EERROR otherwise.
2194 */
2195int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2196{
2197 int ret;
2198
2199 if (!clk)
2200 return 0;
2201
2202 /* prevent racing with updates to the clock topology */
2203 clk_prepare_lock();
2204
2205 /*
2206 * The temporary protection removal is not here, on purpose
2207 * This function is meant to be used instead of clk_rate_protect,
2208 * so before the consumer code path protect the clock provider
2209 */
2210
2211 ret = clk_core_set_rate_nolock(clk->core, rate);
2212 if (!ret) {
2213 clk_core_rate_protect(clk->core);
2214 clk->exclusive_count++;
2215 }
2216
2217 clk_prepare_unlock();
2218
2219 return ret;
2220}
2221EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2222
2223/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002224 * clk_set_rate_range - set a rate range for a clock source
2225 * @clk: clock source
2226 * @min: desired minimum clock rate in Hz, inclusive
2227 * @max: desired maximum clock rate in Hz, inclusive
2228 *
2229 * Returns success (0) or negative errno.
2230 */
2231int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2232{
2233 int ret = 0;
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002234 unsigned long old_min, old_max, rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002235
2236 if (!clk)
2237 return 0;
2238
2239 if (min > max) {
2240 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2241 __func__, clk->core->name, clk->dev_id, clk->con_id,
2242 min, max);
2243 return -EINVAL;
2244 }
2245
2246 clk_prepare_lock();
2247
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002248 if (clk->exclusive_count)
2249 clk_core_rate_unprotect(clk->core);
2250
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002251 /* Save the current values in case we need to rollback the change */
2252 old_min = clk->min_rate;
2253 old_max = clk->max_rate;
2254 clk->min_rate = min;
2255 clk->max_rate = max;
2256
2257 rate = clk_core_get_rate_nolock(clk->core);
2258 if (rate < min || rate > max) {
2259 /*
2260 * FIXME:
2261 * We are in bit of trouble here, current rate is outside the
2262 * the requested range. We are going try to request appropriate
2263 * range boundary but there is a catch. It may fail for the
2264 * usual reason (clock broken, clock protected, etc) but also
2265 * because:
2266 * - round_rate() was not favorable and fell on the wrong
2267 * side of the boundary
2268 * - the determine_rate() callback does not really check for
2269 * this corner case when determining the rate
2270 */
2271
2272 if (rate < min)
2273 rate = min;
2274 else
2275 rate = max;
2276
2277 ret = clk_core_set_rate_nolock(clk->core, rate);
2278 if (ret) {
2279 /* rollback the changes */
2280 clk->min_rate = old_min;
2281 clk->max_rate = old_max;
2282 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002283 }
2284
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002285 if (clk->exclusive_count)
2286 clk_core_rate_protect(clk->core);
2287
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002288 clk_prepare_unlock();
2289
2290 return ret;
2291}
2292EXPORT_SYMBOL_GPL(clk_set_rate_range);
2293
2294/**
2295 * clk_set_min_rate - set a minimum clock rate for a clock source
2296 * @clk: clock source
2297 * @rate: desired minimum clock rate in Hz, inclusive
2298 *
2299 * Returns success (0) or negative errno.
2300 */
2301int clk_set_min_rate(struct clk *clk, unsigned long rate)
2302{
2303 if (!clk)
2304 return 0;
2305
2306 return clk_set_rate_range(clk, rate, clk->max_rate);
2307}
2308EXPORT_SYMBOL_GPL(clk_set_min_rate);
2309
2310/**
2311 * clk_set_max_rate - set a maximum clock rate for a clock source
2312 * @clk: clock source
2313 * @rate: desired maximum clock rate in Hz, inclusive
2314 *
2315 * Returns success (0) or negative errno.
2316 */
2317int clk_set_max_rate(struct clk *clk, unsigned long rate)
2318{
2319 if (!clk)
2320 return 0;
2321
2322 return clk_set_rate_range(clk, clk->min_rate, rate);
2323}
2324EXPORT_SYMBOL_GPL(clk_set_max_rate);
2325
2326/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002327 * clk_get_parent - return the parent of a clk
2328 * @clk: the clk whose parent gets returned
2329 *
2330 * Simply returns clk->parent. Returns NULL if clk is NULL.
2331 */
2332struct clk *clk_get_parent(struct clk *clk)
2333{
2334 struct clk *parent;
2335
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002336 if (!clk)
2337 return NULL;
2338
Mike Turquetteeab89f62013-03-28 13:59:01 -07002339 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002340 /* TODO: Create a per-user clk and change callers to call clk_put */
2341 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002342 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002343
2344 return parent;
2345}
2346EXPORT_SYMBOL_GPL(clk_get_parent);
2347
Stephen Boydd6968fc2015-04-30 13:54:13 -07002348static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002349{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002350 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002351
Masahiro Yamada2430a942016-02-09 20:19:14 +09002352 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002353 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002354
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002355 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002356}
2357
Stephen Boydd6968fc2015-04-30 13:54:13 -07002358static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002359 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002360{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002361 clk_reparent(core, new_parent);
2362 __clk_recalc_accuracies(core);
2363 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002364}
2365
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002366void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2367{
2368 if (!hw)
2369 return;
2370
2371 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2372}
2373
Mike Turquetteb24764902012-03-15 23:11:19 -07002374/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002375 * clk_has_parent - check if a clock is a possible parent for another
2376 * @clk: clock source
2377 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002378 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002379 * This function can be used in drivers that need to check that a clock can be
2380 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002381 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002382 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002383 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002384bool clk_has_parent(struct clk *clk, struct clk *parent)
2385{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002386 struct clk_core *core, *parent_core;
Stephen Boydfc0c2092019-04-12 11:31:47 -07002387 int i;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002388
2389 /* NULL clocks should be nops, so return success if either is NULL. */
2390 if (!clk || !parent)
2391 return true;
2392
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002393 core = clk->core;
2394 parent_core = parent->core;
2395
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002396 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002397 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002398 return true;
2399
Stephen Boydfc0c2092019-04-12 11:31:47 -07002400 for (i = 0; i < core->num_parents; i++)
2401 if (!strcmp(core->parents[i].name, parent_core->name))
2402 return true;
2403
2404 return false;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002405}
2406EXPORT_SYMBOL_GPL(clk_has_parent);
2407
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002408static int clk_core_set_parent_nolock(struct clk_core *core,
2409 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002410{
2411 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002412 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002413 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002414
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002415 lockdep_assert_held(&prepare_lock);
2416
Stephen Boydd6968fc2015-04-30 13:54:13 -07002417 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002418 return 0;
2419
Stephen Boydd6968fc2015-04-30 13:54:13 -07002420 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002421 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002422
Stephen Boydb61c43c2015-02-02 14:11:25 -08002423 /* verify ops for for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002424 if (core->num_parents > 1 && !core->ops->set_parent)
2425 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08002426
Ulf Hansson031dcc92013-04-02 23:09:38 +02002427 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002428 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2429 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002430
Jerome Brunete55a8392017-12-01 22:51:56 +01002431 if (clk_core_rate_is_protected(core))
2432 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002433
2434 /* try finding the new parent index */
2435 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002436 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002437 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002438 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002439 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002440 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002441 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002442 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002443 }
2444
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002445 ret = clk_pm_runtime_get(core);
2446 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002447 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002448
Mike Turquetteb24764902012-03-15 23:11:19 -07002449 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002450 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002451
2452 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002453 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002454 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07002455
Ulf Hansson031dcc92013-04-02 23:09:38 +02002456 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002457 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002458
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002459 /* propagate rate an accuracy recalculation accordingly */
2460 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002461 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002462 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002463 __clk_recalc_rates(core, POST_RATE_CHANGE);
2464 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002465 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002466
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002467runtime_put:
2468 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002469
2470 return ret;
2471}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002472
2473/**
2474 * clk_set_parent - switch the parent of a mux clk
2475 * @clk: the mux clk whose input we are switching
2476 * @parent: the new input to clk
2477 *
2478 * Re-parent clk to use parent as its new input source. If clk is in
2479 * prepared state, the clk will get enabled for the duration of this call. If
2480 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2481 * that, the reparenting is glitchy in hardware, etc), use the
2482 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2483 *
2484 * After successfully changing clk's parent clk_set_parent will update the
2485 * clk topology, sysfs topology and propagate rate recalculation via
2486 * __clk_recalc_rates.
2487 *
2488 * Returns 0 on success, -EERROR otherwise.
2489 */
2490int clk_set_parent(struct clk *clk, struct clk *parent)
2491{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002492 int ret;
2493
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002494 if (!clk)
2495 return 0;
2496
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002497 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002498
2499 if (clk->exclusive_count)
2500 clk_core_rate_unprotect(clk->core);
2501
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002502 ret = clk_core_set_parent_nolock(clk->core,
2503 parent ? parent->core : NULL);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002504
2505 if (clk->exclusive_count)
2506 clk_core_rate_protect(clk->core);
2507
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002508 clk_prepare_unlock();
2509
2510 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002511}
Mike Turquetteb24764902012-03-15 23:11:19 -07002512EXPORT_SYMBOL_GPL(clk_set_parent);
2513
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002514static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2515{
2516 int ret = -EINVAL;
2517
2518 lockdep_assert_held(&prepare_lock);
2519
2520 if (!core)
2521 return 0;
2522
Jerome Brunete55a8392017-12-01 22:51:56 +01002523 if (clk_core_rate_is_protected(core))
2524 return -EBUSY;
2525
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002526 trace_clk_set_phase(core, degrees);
2527
Shawn Lin7f95bee2018-03-08 14:49:41 +08002528 if (core->ops->set_phase) {
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002529 ret = core->ops->set_phase(core->hw, degrees);
Shawn Lin7f95bee2018-03-08 14:49:41 +08002530 if (!ret)
2531 core->phase = degrees;
2532 }
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002533
2534 trace_clk_set_phase_complete(core, degrees);
2535
2536 return ret;
2537}
2538
Mike Turquetteb24764902012-03-15 23:11:19 -07002539/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002540 * clk_set_phase - adjust the phase shift of a clock signal
2541 * @clk: clock signal source
2542 * @degrees: number of degrees the signal is shifted
2543 *
2544 * Shifts the phase of a clock signal by the specified
2545 * degrees. Returns 0 on success, -EERROR otherwise.
2546 *
2547 * This function makes no distinction about the input or reference
2548 * signal that we adjust the clock signal phase against. For example
2549 * phase locked-loop clock signal generators we may shift phase with
2550 * respect to feedback clock signal input, but for other cases the
2551 * clock phase may be shifted with respect to some other, unspecified
2552 * signal.
2553 *
2554 * Additionally the concept of phase shift does not propagate through
2555 * the clock tree hierarchy, which sets it apart from clock rates and
2556 * clock accuracy. A parent clock phase attribute does not have an
2557 * impact on the phase attribute of a child clock.
2558 */
2559int clk_set_phase(struct clk *clk, int degrees)
2560{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002561 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002562
2563 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002564 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002565
2566 /* sanity check degrees */
2567 degrees %= 360;
2568 if (degrees < 0)
2569 degrees += 360;
2570
2571 clk_prepare_lock();
2572
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002573 if (clk->exclusive_count)
2574 clk_core_rate_unprotect(clk->core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002575
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002576 ret = clk_core_set_phase_nolock(clk->core, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002577
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002578 if (clk->exclusive_count)
2579 clk_core_rate_protect(clk->core);
Mike Turquettee59c5372014-02-18 21:21:25 -08002580
Mike Turquettee59c5372014-02-18 21:21:25 -08002581 clk_prepare_unlock();
2582
Mike Turquettee59c5372014-02-18 21:21:25 -08002583 return ret;
2584}
Maxime Ripard9767b04f2015-01-20 22:23:43 +01002585EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002586
Stephen Boydd6968fc2015-04-30 13:54:13 -07002587static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002588{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002589 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002590
2591 clk_prepare_lock();
Shawn Lin1f9c63e2018-03-14 08:28:31 +08002592 /* Always try to update cached phase if possible */
2593 if (core->ops->get_phase)
2594 core->phase = core->ops->get_phase(core->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002595 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002596 clk_prepare_unlock();
2597
Mike Turquettee59c5372014-02-18 21:21:25 -08002598 return ret;
2599}
2600
2601/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002602 * clk_get_phase - return the phase shift of a clock signal
2603 * @clk: clock signal source
2604 *
2605 * Returns the phase shift of a clock node in degrees, otherwise returns
2606 * -EERROR.
2607 */
2608int clk_get_phase(struct clk *clk)
2609{
2610 if (!clk)
2611 return 0;
2612
2613 return clk_core_get_phase(clk->core);
2614}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002615EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002616
Jerome Brunet9fba7382018-06-19 16:41:41 +02002617static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2618{
2619 /* Assume a default value of 50% */
2620 core->duty.num = 1;
2621 core->duty.den = 2;
2622}
2623
2624static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2625
2626static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2627{
2628 struct clk_duty *duty = &core->duty;
2629 int ret = 0;
2630
2631 if (!core->ops->get_duty_cycle)
2632 return clk_core_update_duty_cycle_parent_nolock(core);
2633
2634 ret = core->ops->get_duty_cycle(core->hw, duty);
2635 if (ret)
2636 goto reset;
2637
2638 /* Don't trust the clock provider too much */
2639 if (duty->den == 0 || duty->num > duty->den) {
2640 ret = -EINVAL;
2641 goto reset;
2642 }
2643
2644 return 0;
2645
2646reset:
2647 clk_core_reset_duty_cycle_nolock(core);
2648 return ret;
2649}
2650
2651static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2652{
2653 int ret = 0;
2654
2655 if (core->parent &&
2656 core->flags & CLK_DUTY_CYCLE_PARENT) {
2657 ret = clk_core_update_duty_cycle_nolock(core->parent);
2658 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2659 } else {
2660 clk_core_reset_duty_cycle_nolock(core);
2661 }
2662
2663 return ret;
2664}
2665
2666static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2667 struct clk_duty *duty);
2668
2669static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2670 struct clk_duty *duty)
2671{
2672 int ret;
2673
2674 lockdep_assert_held(&prepare_lock);
2675
2676 if (clk_core_rate_is_protected(core))
2677 return -EBUSY;
2678
2679 trace_clk_set_duty_cycle(core, duty);
2680
2681 if (!core->ops->set_duty_cycle)
2682 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2683
2684 ret = core->ops->set_duty_cycle(core->hw, duty);
2685 if (!ret)
2686 memcpy(&core->duty, duty, sizeof(*duty));
2687
2688 trace_clk_set_duty_cycle_complete(core, duty);
2689
2690 return ret;
2691}
2692
2693static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2694 struct clk_duty *duty)
2695{
2696 int ret = 0;
2697
2698 if (core->parent &&
2699 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2700 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2701 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2702 }
2703
2704 return ret;
2705}
2706
2707/**
2708 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2709 * @clk: clock signal source
2710 * @num: numerator of the duty cycle ratio to be applied
2711 * @den: denominator of the duty cycle ratio to be applied
2712 *
2713 * Apply the duty cycle ratio if the ratio is valid and the clock can
2714 * perform this operation
2715 *
2716 * Returns (0) on success, a negative errno otherwise.
2717 */
2718int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2719{
2720 int ret;
2721 struct clk_duty duty;
2722
2723 if (!clk)
2724 return 0;
2725
2726 /* sanity check the ratio */
2727 if (den == 0 || num > den)
2728 return -EINVAL;
2729
2730 duty.num = num;
2731 duty.den = den;
2732
2733 clk_prepare_lock();
2734
2735 if (clk->exclusive_count)
2736 clk_core_rate_unprotect(clk->core);
2737
2738 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2739
2740 if (clk->exclusive_count)
2741 clk_core_rate_protect(clk->core);
2742
2743 clk_prepare_unlock();
2744
2745 return ret;
2746}
2747EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2748
2749static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2750 unsigned int scale)
2751{
2752 struct clk_duty *duty = &core->duty;
2753 int ret;
2754
2755 clk_prepare_lock();
2756
2757 ret = clk_core_update_duty_cycle_nolock(core);
2758 if (!ret)
2759 ret = mult_frac(scale, duty->num, duty->den);
2760
2761 clk_prepare_unlock();
2762
2763 return ret;
2764}
2765
2766/**
2767 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2768 * @clk: clock signal source
2769 * @scale: scaling factor to be applied to represent the ratio as an integer
2770 *
2771 * Returns the duty cycle ratio of a clock node multiplied by the provided
2772 * scaling factor, or negative errno on error.
2773 */
2774int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2775{
2776 if (!clk)
2777 return 0;
2778
2779 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2780}
2781EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2782
Mike Turquetteb24764902012-03-15 23:11:19 -07002783/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002784 * clk_is_match - check if two clk's point to the same hardware clock
2785 * @p: clk compared against q
2786 * @q: clk compared against p
2787 *
2788 * Returns true if the two struct clk pointers both point to the same hardware
2789 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2790 * share the same struct clk_core object.
2791 *
2792 * Returns false otherwise. Note that two NULL clks are treated as matching.
2793 */
2794bool clk_is_match(const struct clk *p, const struct clk *q)
2795{
2796 /* trivial case: identical struct clk's or both NULL */
2797 if (p == q)
2798 return true;
2799
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002800 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002801 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2802 if (p->core == q->core)
2803 return true;
2804
2805 return false;
2806}
2807EXPORT_SYMBOL_GPL(clk_is_match);
2808
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002809/*** debugfs support ***/
2810
2811#ifdef CONFIG_DEBUG_FS
2812#include <linux/debugfs.h>
2813
2814static struct dentry *rootdir;
2815static int inited = 0;
2816static DEFINE_MUTEX(clk_debug_lock);
2817static HLIST_HEAD(clk_debug_list);
2818
2819static struct hlist_head *all_lists[] = {
2820 &clk_root_list,
2821 &clk_orphan_list,
2822 NULL,
2823};
2824
2825static struct hlist_head *orphan_list[] = {
2826 &clk_orphan_list,
2827 NULL,
2828};
2829
2830static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2831 int level)
2832{
2833 if (!c)
2834 return;
2835
Jerome Brunet9fba7382018-06-19 16:41:41 +02002836 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002837 level * 3 + 1, "",
2838 30 - level * 3, c->name,
Jerome Brunete55a8392017-12-01 22:51:56 +01002839 c->enable_count, c->prepare_count, c->protect_count,
2840 clk_core_get_rate(c), clk_core_get_accuracy(c),
Jerome Brunet9fba7382018-06-19 16:41:41 +02002841 clk_core_get_phase(c),
2842 clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002843}
2844
2845static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2846 int level)
2847{
2848 struct clk_core *child;
2849
2850 if (!c)
2851 return;
2852
2853 clk_summary_show_one(s, c, level);
2854
2855 hlist_for_each_entry(child, &c->children, child_node)
2856 clk_summary_show_subtree(s, child, level + 1);
2857}
2858
2859static int clk_summary_show(struct seq_file *s, void *data)
2860{
2861 struct clk_core *c;
2862 struct hlist_head **lists = (struct hlist_head **)s->private;
2863
Jerome Brunet9fba7382018-06-19 16:41:41 +02002864 seq_puts(s, " enable prepare protect duty\n");
2865 seq_puts(s, " clock count count count rate accuracy phase cycle\n");
2866 seq_puts(s, "---------------------------------------------------------------------------------------------\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002867
2868 clk_prepare_lock();
2869
2870 for (; *lists; lists++)
2871 hlist_for_each_entry(c, *lists, child_node)
2872 clk_summary_show_subtree(s, c, 0);
2873
2874 clk_prepare_unlock();
2875
2876 return 0;
2877}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002878DEFINE_SHOW_ATTRIBUTE(clk_summary);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002879
2880static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2881{
2882 if (!c)
2883 return;
2884
Stefan Wahren7cb81132015-04-29 16:36:43 +00002885 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002886 seq_printf(s, "\"%s\": { ", c->name);
2887 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2888 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002889 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002890 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2891 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Lubomir Rintelc6e90992019-01-04 23:05:49 +01002892 seq_printf(s, "\"phase\": %d,", clk_core_get_phase(c));
Jerome Brunet9fba7382018-06-19 16:41:41 +02002893 seq_printf(s, "\"duty_cycle\": %u",
2894 clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002895}
2896
2897static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2898{
2899 struct clk_core *child;
2900
2901 if (!c)
2902 return;
2903
2904 clk_dump_one(s, c, level);
2905
2906 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002907 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002908 clk_dump_subtree(s, child, level + 1);
2909 }
2910
Markus Elfring4d327582017-04-20 08:45:43 +02002911 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002912}
2913
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002914static int clk_dump_show(struct seq_file *s, void *data)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002915{
2916 struct clk_core *c;
2917 bool first_node = true;
2918 struct hlist_head **lists = (struct hlist_head **)s->private;
2919
Markus Elfring4d327582017-04-20 08:45:43 +02002920 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002921 clk_prepare_lock();
2922
2923 for (; *lists; lists++) {
2924 hlist_for_each_entry(c, *lists, child_node) {
2925 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02002926 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002927 first_node = false;
2928 clk_dump_subtree(s, c, 0);
2929 }
2930 }
2931
2932 clk_prepare_unlock();
2933
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002934 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002935 return 0;
2936}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002937DEFINE_SHOW_ATTRIBUTE(clk_dump);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002938
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002939static const struct {
2940 unsigned long flag;
2941 const char *name;
2942} clk_flags[] = {
Geert Uytterhoeven40dd71c2018-07-06 17:16:54 +02002943#define ENTRY(f) { f, #f }
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002944 ENTRY(CLK_SET_RATE_GATE),
2945 ENTRY(CLK_SET_PARENT_GATE),
2946 ENTRY(CLK_SET_RATE_PARENT),
2947 ENTRY(CLK_IGNORE_UNUSED),
2948 ENTRY(CLK_IS_BASIC),
2949 ENTRY(CLK_GET_RATE_NOCACHE),
2950 ENTRY(CLK_SET_RATE_NO_REPARENT),
2951 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2952 ENTRY(CLK_RECALC_NEW_RATES),
2953 ENTRY(CLK_SET_RATE_UNGATE),
2954 ENTRY(CLK_IS_CRITICAL),
2955 ENTRY(CLK_OPS_PARENT_ENABLE),
Jerome Brunet9fba7382018-06-19 16:41:41 +02002956 ENTRY(CLK_DUTY_CYCLE_PARENT),
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002957#undef ENTRY
2958};
2959
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002960static int clk_flags_show(struct seq_file *s, void *data)
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002961{
2962 struct clk_core *core = s->private;
2963 unsigned long flags = core->flags;
2964 unsigned int i;
2965
2966 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2967 if (flags & clk_flags[i].flag) {
2968 seq_printf(s, "%s\n", clk_flags[i].name);
2969 flags &= ~clk_flags[i].flag;
2970 }
2971 }
2972 if (flags) {
2973 /* Unknown flags */
2974 seq_printf(s, "0x%lx\n", flags);
2975 }
2976
2977 return 0;
2978}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002979DEFINE_SHOW_ATTRIBUTE(clk_flags);
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002980
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002981static int possible_parents_show(struct seq_file *s, void *data)
Peter De Schrijver92031572017-03-21 15:20:31 +02002982{
2983 struct clk_core *core = s->private;
2984 int i;
2985
2986 for (i = 0; i < core->num_parents - 1; i++)
Stephen Boydfc0c2092019-04-12 11:31:47 -07002987 seq_printf(s, "%s ", core->parents[i].name);
Peter De Schrijver92031572017-03-21 15:20:31 +02002988
Stephen Boydfc0c2092019-04-12 11:31:47 -07002989 seq_printf(s, "%s\n", core->parents[i].name);
Peter De Schrijver92031572017-03-21 15:20:31 +02002990
2991 return 0;
2992}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002993DEFINE_SHOW_ATTRIBUTE(possible_parents);
Peter De Schrijver92031572017-03-21 15:20:31 +02002994
Jerome Brunet9fba7382018-06-19 16:41:41 +02002995static int clk_duty_cycle_show(struct seq_file *s, void *data)
2996{
2997 struct clk_core *core = s->private;
2998 struct clk_duty *duty = &core->duty;
2999
3000 seq_printf(s, "%u/%u\n", duty->num, duty->den);
3001
3002 return 0;
3003}
3004DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
3005
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003006static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003007{
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003008 struct dentry *root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003009
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003010 if (!core || !pdentry)
3011 return;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003012
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003013 root = debugfs_create_dir(core->name, pdentry);
3014 core->dentry = root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003015
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003016 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
3017 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
3018 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
3019 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
3020 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
3021 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
3022 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
3023 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
Jerome Brunet9fba7382018-06-19 16:41:41 +02003024 debugfs_create_file("clk_duty_cycle", 0444, root, core,
3025 &clk_duty_cycle_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003026
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003027 if (core->num_parents > 1)
3028 debugfs_create_file("clk_possible_parents", 0444, root, core,
3029 &possible_parents_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003030
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003031 if (core->ops->debug_init)
3032 core->ops->debug_init(core->hw, core->dentry);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003033}
3034
3035/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003036 * clk_debug_register - add a clk node to the debugfs clk directory
3037 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003038 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003039 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3040 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003041 * will be created lazily by clk_debug_init as part of a late_initcall.
3042 */
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003043static void clk_debug_register(struct clk_core *core)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003044{
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003045 mutex_lock(&clk_debug_lock);
3046 hlist_add_head(&core->debug_node, &clk_debug_list);
Stephen Boyddb3188fa2018-01-03 16:44:37 -08003047 if (inited)
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003048 clk_debug_create_one(core, rootdir);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003049 mutex_unlock(&clk_debug_lock);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003050}
3051
3052 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003053 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3054 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003055 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003056 * Dynamically removes a clk and all its child nodes from the
3057 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08003058 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003059 */
3060static void clk_debug_unregister(struct clk_core *core)
3061{
3062 mutex_lock(&clk_debug_lock);
3063 hlist_del_init(&core->debug_node);
3064 debugfs_remove_recursive(core->dentry);
3065 core->dentry = NULL;
3066 mutex_unlock(&clk_debug_lock);
3067}
3068
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003069/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003070 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003071 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003072 * clks are often initialized very early during boot before memory can be
3073 * dynamically allocated and well before debugfs is setup. This function
3074 * populates the debugfs clk directory once at boot-time when we know that
3075 * debugfs is setup. It should only be called once at boot-time, all other clks
3076 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003077 */
3078static int __init clk_debug_init(void)
3079{
3080 struct clk_core *core;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003081
3082 rootdir = debugfs_create_dir("clk", NULL);
3083
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003084 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
3085 &clk_summary_fops);
3086 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
3087 &clk_dump_fops);
3088 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
3089 &clk_summary_fops);
3090 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
3091 &clk_dump_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003092
3093 mutex_lock(&clk_debug_lock);
3094 hlist_for_each_entry(core, &clk_debug_list, debug_node)
3095 clk_debug_create_one(core, rootdir);
3096
3097 inited = 1;
3098 mutex_unlock(&clk_debug_lock);
3099
3100 return 0;
3101}
3102late_initcall(clk_debug_init);
3103#else
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003104static inline void clk_debug_register(struct clk_core *core) { }
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003105static inline void clk_debug_reparent(struct clk_core *core,
3106 struct clk_core *new_parent)
3107{
3108}
3109static inline void clk_debug_unregister(struct clk_core *core)
3110{
3111}
3112#endif
3113
Michael Turquette3d3801e2015-02-25 09:11:01 -08003114/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003115 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003116 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07003117 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003118 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07003119 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07003120 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003121static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07003122{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003123 int ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003124 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003125 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003126 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003127
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003128 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07003129 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07003130
Mike Turquetteeab89f62013-03-28 13:59:01 -07003131 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003132
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003133 ret = clk_pm_runtime_get(core);
3134 if (ret)
3135 goto unlock;
3136
Mike Turquetteb24764902012-03-15 23:11:19 -07003137 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003138 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003139 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003140 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003141 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07003142 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07003143 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003144
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03003145 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003146 if (core->ops->set_rate &&
3147 !((core->ops->round_rate || core->ops->determine_rate) &&
3148 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003149 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3150 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003151 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003152 goto out;
3153 }
3154
Stephen Boydd6968fc2015-04-30 13:54:13 -07003155 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003156 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3157 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003158 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003159 goto out;
3160 }
3161
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09003162 if (core->num_parents > 1 && !core->ops->get_parent) {
3163 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3164 __func__, core->name);
3165 ret = -EINVAL;
3166 goto out;
3167 }
3168
Stephen Boydd6968fc2015-04-30 13:54:13 -07003169 if (core->ops->set_rate_and_parent &&
3170 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003171 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003172 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08003173 ret = -EINVAL;
3174 goto out;
3175 }
3176
Stephen Boydd6968fc2015-04-30 13:54:13 -07003177 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07003178
3179 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08003180 * Populate core->parent if parent has already been clk_core_init'd. If
3181 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003182 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07003183 * clk list.
3184 *
3185 * Every time a new clk is clk_init'd then we walk the list of orphan
3186 * clocks and re-parent any that are children of the clock currently
3187 * being clk_init'd.
3188 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02003189 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003190 hlist_add_head(&core->child_node,
3191 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003192 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003193 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003194 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003195 core->orphan = false;
3196 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003197 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003198 core->orphan = true;
3199 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003200
3201 /*
Jerome Brunet541deba2018-02-14 14:43:37 +01003202 * optional platform-specific magic
3203 *
3204 * The .init callback is not used by any of the basic clock types, but
3205 * exists for weird hardware that must perform initialization magic.
3206 * Please consider other ways of solving initialization problems before
3207 * using this callback, as its use is discouraged.
3208 */
3209 if (core->ops->init)
3210 core->ops->init(core->hw);
3211
3212 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003213 * Set clk's accuracy. The preferred method is to use
3214 * .recalc_accuracy. For simple clocks and lazy developers the default
3215 * fallback is to use the parent's accuracy. If a clock doesn't have a
3216 * parent (or is orphaned) then accuracy is set to zero (perfect
3217 * clock).
3218 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003219 if (core->ops->recalc_accuracy)
3220 core->accuracy = core->ops->recalc_accuracy(core->hw,
3221 __clk_get_accuracy(core->parent));
3222 else if (core->parent)
3223 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003224 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003225 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003226
3227 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02003228 * Set clk's phase.
3229 * Since a phase is by definition relative to its parent, just
3230 * query the current clock phase, or just assume it's in phase.
3231 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003232 if (core->ops->get_phase)
3233 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02003234 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003235 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02003236
3237 /*
Jerome Brunet9fba7382018-06-19 16:41:41 +02003238 * Set clk's duty cycle.
3239 */
3240 clk_core_update_duty_cycle_nolock(core);
3241
3242 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07003243 * Set clk's rate. The preferred method is to use .recalc_rate. For
3244 * simple clocks and lazy developers the default fallback is to use the
3245 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3246 * then rate is set to zero.
3247 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003248 if (core->ops->recalc_rate)
3249 rate = core->ops->recalc_rate(core->hw,
3250 clk_core_get_rate_nolock(core->parent));
3251 else if (core->parent)
3252 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003253 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003254 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003255 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003256
3257 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003258 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3259 * don't get accidentally disabled when walking the orphan tree and
3260 * reparenting clocks
3261 */
3262 if (core->flags & CLK_IS_CRITICAL) {
3263 unsigned long flags;
3264
3265 clk_core_prepare(core);
3266
3267 flags = clk_enable_lock();
3268 clk_core_enable(core);
3269 clk_enable_unlock(flags);
3270 }
3271
3272 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003273 * walk the list of orphan clocks and reparent any that newly finds a
3274 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07003275 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08003276 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003277 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01003278
Michael Turquette904e6ea2016-07-08 16:32:10 -07003279 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003280 * We need to use __clk_set_parent_before() and _after() to
3281 * to properly migrate any prepare/enable count of the orphan
3282 * clock. This is important for CLK_IS_CRITICAL clocks, which
3283 * are enabled during init but might not have a parent yet.
Michael Turquette904e6ea2016-07-08 16:32:10 -07003284 */
3285 if (parent) {
Stephen Boydf8f8f1d2017-11-02 00:36:09 -07003286 /* update the clk tree topology */
Jerome Brunet99652a42018-02-14 14:43:36 +01003287 __clk_set_parent_before(orphan, parent);
3288 __clk_set_parent_after(orphan, parent, NULL);
Michael Turquette904e6ea2016-07-08 16:32:10 -07003289 __clk_recalc_accuracies(orphan);
3290 __clk_recalc_rates(orphan, 0);
3291 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003292 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003293
Stephen Boydd6968fc2015-04-30 13:54:13 -07003294 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07003295out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003296 clk_pm_runtime_put(core);
3297unlock:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003298 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003299
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003300 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003301 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003302
Mike Turquetted1302a32012-03-29 14:30:40 -07003303 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07003304}
3305
Stephen Boyd1df40462018-12-11 08:32:04 -08003306/**
3307 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3308 * @core: clk to add consumer to
3309 * @clk: consumer to link to a clk
3310 */
3311static void clk_core_link_consumer(struct clk_core *core, struct clk *clk)
3312{
3313 clk_prepare_lock();
3314 hlist_add_head(&clk->clks_node, &core->clks);
3315 clk_prepare_unlock();
3316}
3317
3318/**
3319 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3320 * @clk: consumer to unlink
3321 */
3322static void clk_core_unlink_consumer(struct clk *clk)
3323{
3324 lockdep_assert_held(&prepare_lock);
3325 hlist_del(&clk->clks_node);
3326}
3327
3328/**
3329 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
3330 * @core: clk to allocate a consumer for
3331 * @dev_id: string describing device name
3332 * @con_id: connection ID string on device
3333 *
3334 * Returns: clk consumer left unlinked from the consumer list
3335 */
3336static struct clk *alloc_clk(struct clk_core *core, const char *dev_id,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003337 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003338{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003339 struct clk *clk;
3340
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003341 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3342 if (!clk)
3343 return ERR_PTR(-ENOMEM);
3344
Stephen Boyd1df40462018-12-11 08:32:04 -08003345 clk->core = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003346 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02003347 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003348 clk->max_rate = ULONG_MAX;
3349
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003350 return clk;
3351}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003352
Stephen Boyd1df40462018-12-11 08:32:04 -08003353/**
3354 * free_clk - Free a clk consumer
3355 * @clk: clk consumer to free
3356 *
3357 * Note, this assumes the clk has been unlinked from the clk_core consumer
3358 * list.
3359 */
3360static void free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003361{
Leonard Crestez253160a2017-02-20 15:20:56 +02003362 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003363 kfree(clk);
3364}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003365
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003366/**
Stephen Boyd1df40462018-12-11 08:32:04 -08003367 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
3368 * a clk_hw
Stephen Boydefa85042018-12-11 08:34:16 -08003369 * @dev: clk consumer device
Stephen Boyd1df40462018-12-11 08:32:04 -08003370 * @hw: clk_hw associated with the clk being consumed
3371 * @dev_id: string describing device name
3372 * @con_id: connection ID string on device
3373 *
3374 * This is the main function used to create a clk pointer for use by clk
3375 * consumers. It connects a consumer to the clk_core and clk_hw structures
3376 * used by the framework and clk provider respectively.
3377 */
Stephen Boydefa85042018-12-11 08:34:16 -08003378struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
Stephen Boyd1df40462018-12-11 08:32:04 -08003379 const char *dev_id, const char *con_id)
3380{
3381 struct clk *clk;
3382 struct clk_core *core;
3383
3384 /* This is to allow this function to be chained to others */
3385 if (IS_ERR_OR_NULL(hw))
3386 return ERR_CAST(hw);
3387
3388 core = hw->core;
3389 clk = alloc_clk(core, dev_id, con_id);
3390 if (IS_ERR(clk))
3391 return clk;
Stephen Boydefa85042018-12-11 08:34:16 -08003392 clk->dev = dev;
Stephen Boyd1df40462018-12-11 08:32:04 -08003393
3394 if (!try_module_get(core->owner)) {
3395 free_clk(clk);
3396 return ERR_PTR(-ENOENT);
3397 }
3398
3399 kref_get(&core->ref);
3400 clk_core_link_consumer(core, clk);
3401
3402 return clk;
3403}
3404
Stephen Boydfc0c2092019-04-12 11:31:47 -07003405static int clk_cpy_name(const char **dst_p, const char *src, bool must_exist)
3406{
3407 const char *dst;
3408
3409 if (!src) {
3410 if (must_exist)
3411 return -EINVAL;
3412 return 0;
3413 }
3414
3415 *dst_p = dst = kstrdup_const(src, GFP_KERNEL);
3416 if (!dst)
3417 return -ENOMEM;
3418
3419 return 0;
3420}
3421
3422static int clk_core_populate_parent_map(struct clk_core *core)
3423{
3424 const struct clk_init_data *init = core->hw->init;
3425 u8 num_parents = init->num_parents;
3426 const char * const *parent_names = init->parent_names;
3427 const struct clk_hw **parent_hws = init->parent_hws;
3428 const struct clk_parent_data *parent_data = init->parent_data;
3429 int i, ret = 0;
3430 struct clk_parent_map *parents, *parent;
3431
3432 if (!num_parents)
3433 return 0;
3434
3435 /*
3436 * Avoid unnecessary string look-ups of clk_core's possible parents by
3437 * having a cache of names/clk_hw pointers to clk_core pointers.
3438 */
3439 parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
3440 core->parents = parents;
3441 if (!parents)
3442 return -ENOMEM;
3443
3444 /* Copy everything over because it might be __initdata */
3445 for (i = 0, parent = parents; i < num_parents; i++, parent++) {
3446 if (parent_names) {
3447 /* throw a WARN if any entries are NULL */
3448 WARN(!parent_names[i],
3449 "%s: invalid NULL in %s's .parent_names\n",
3450 __func__, core->name);
3451 ret = clk_cpy_name(&parent->name, parent_names[i],
3452 true);
3453 } else if (parent_data) {
3454 parent->hw = parent_data[i].hw;
3455 ret = clk_cpy_name(&parent->fw_name,
3456 parent_data[i].fw_name, false);
3457 if (!ret)
3458 ret = clk_cpy_name(&parent->name,
3459 parent_data[i].name,
3460 false);
3461 } else if (parent_hws) {
3462 parent->hw = parent_hws[i];
3463 } else {
3464 ret = -EINVAL;
3465 WARN(1, "Must specify parents if num_parents > 0\n");
3466 }
3467
3468 if (ret) {
3469 do {
3470 kfree_const(parents[i].name);
3471 kfree_const(parents[i].fw_name);
3472 } while (--i >= 0);
3473 kfree(parents);
3474
3475 return ret;
3476 }
3477 }
3478
3479 return 0;
3480}
3481
3482static void clk_core_free_parent_map(struct clk_core *core)
3483{
3484 int i = core->num_parents;
3485
3486 if (!core->num_parents)
3487 return;
3488
3489 while (--i >= 0) {
3490 kfree_const(core->parents[i].name);
3491 kfree_const(core->parents[i].fw_name);
3492 }
3493
3494 kfree(core->parents);
3495}
3496
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003497static struct clk *
3498__clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07003499{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003500 int ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003501 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003502
Stephen Boydd6968fc2015-04-30 13:54:13 -07003503 core = kzalloc(sizeof(*core), GFP_KERNEL);
3504 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003505 ret = -ENOMEM;
3506 goto fail_out;
3507 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003508
Stephen Boydd6968fc2015-04-30 13:54:13 -07003509 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3510 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003511 ret = -ENOMEM;
3512 goto fail_name;
3513 }
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003514
3515 if (WARN_ON(!hw->init->ops)) {
3516 ret = -EINVAL;
3517 goto fail_ops;
3518 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07003519 core->ops = hw->init->ops;
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003520
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003521 if (dev && pm_runtime_enabled(dev))
Miquel Raynal24478832018-12-04 20:24:37 +01003522 core->rpm_enabled = true;
3523 core->dev = dev;
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003524 core->of_node = np;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003525 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003526 core->owner = dev->driver->owner;
3527 core->hw = hw;
3528 core->flags = hw->init->flags;
3529 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003530 core->min_rate = 0;
3531 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003532 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003533
Stephen Boydfc0c2092019-04-12 11:31:47 -07003534 ret = clk_core_populate_parent_map(core);
3535 if (ret)
Masahiro Yamada176d1162015-12-28 19:23:00 +09003536 goto fail_parents;
Masahiro Yamada176d1162015-12-28 19:23:00 +09003537
Stephen Boydd6968fc2015-04-30 13:54:13 -07003538 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003539
Stephen Boyd1df40462018-12-11 08:32:04 -08003540 /*
3541 * Don't call clk_hw_create_clk() here because that would pin the
3542 * provider module to itself and prevent it from ever being removed.
3543 */
3544 hw->clk = alloc_clk(core, NULL, NULL);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003545 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003546 ret = PTR_ERR(hw->clk);
Stephen Boydfc0c2092019-04-12 11:31:47 -07003547 goto fail_create_clk;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003548 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003549
Stephen Boyd1df40462018-12-11 08:32:04 -08003550 clk_core_link_consumer(hw->core, hw->clk);
3551
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003552 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003553 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003554 return hw->clk;
3555
Stephen Boyd1df40462018-12-11 08:32:04 -08003556 clk_prepare_lock();
3557 clk_core_unlink_consumer(hw->clk);
3558 clk_prepare_unlock();
3559
3560 free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003561 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003562
Stephen Boydfc0c2092019-04-12 11:31:47 -07003563fail_create_clk:
3564 clk_core_free_parent_map(core);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003565fail_parents:
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003566fail_ops:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003567 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003568fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003569 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003570fail_out:
3571 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003572}
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003573
3574/**
3575 * clk_register - allocate a new clock, register it and return an opaque cookie
3576 * @dev: device that is registering this clock
3577 * @hw: link to hardware-specific clock data
3578 *
3579 * clk_register is the primary interface for populating the clock tree with new
3580 * clock nodes. It returns a pointer to the newly allocated struct clk which
3581 * cannot be dereferenced by driver code but may be used in conjunction with the
3582 * rest of the clock API. In the event of an error clk_register will return an
3583 * error code; drivers must test for an error code after calling clk_register.
3584 */
3585struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3586{
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003587 return __clk_register(dev, dev_of_node(dev), hw);
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003588}
Mike Turquetteb24764902012-03-15 23:11:19 -07003589EXPORT_SYMBOL_GPL(clk_register);
3590
Stephen Boyd41438042016-02-05 17:02:52 -08003591/**
3592 * clk_hw_register - register a clk_hw and return an error code
3593 * @dev: device that is registering this clock
3594 * @hw: link to hardware-specific clock data
3595 *
3596 * clk_hw_register is the primary interface for populating the clock tree with
3597 * new clock nodes. It returns an integer equal to zero indicating success or
3598 * less than zero indicating failure. Drivers must test for an error code after
3599 * calling clk_hw_register().
3600 */
3601int clk_hw_register(struct device *dev, struct clk_hw *hw)
3602{
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003603 return PTR_ERR_OR_ZERO(__clk_register(dev, dev_of_node(dev), hw));
Stephen Boyd41438042016-02-05 17:02:52 -08003604}
3605EXPORT_SYMBOL_GPL(clk_hw_register);
3606
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003607/*
3608 * of_clk_hw_register - register a clk_hw and return an error code
3609 * @node: device_node of device that is registering this clock
3610 * @hw: link to hardware-specific clock data
3611 *
3612 * of_clk_hw_register() is the primary interface for populating the clock tree
3613 * with new clock nodes when a struct device is not available, but a struct
3614 * device_node is. It returns an integer equal to zero indicating success or
3615 * less than zero indicating failure. Drivers must test for an error code after
3616 * calling of_clk_hw_register().
3617 */
3618int of_clk_hw_register(struct device_node *node, struct clk_hw *hw)
3619{
3620 return PTR_ERR_OR_ZERO(__clk_register(NULL, node, hw));
3621}
3622EXPORT_SYMBOL_GPL(of_clk_hw_register);
3623
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003624/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003625static void __clk_release(struct kref *ref)
3626{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003627 struct clk_core *core = container_of(ref, struct clk_core, ref);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003628
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003629 lockdep_assert_held(&prepare_lock);
3630
Stephen Boydfc0c2092019-04-12 11:31:47 -07003631 clk_core_free_parent_map(core);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003632 kfree_const(core->name);
3633 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003634}
3635
3636/*
3637 * Empty clk_ops for unregistered clocks. These are used temporarily
3638 * after clk_unregister() was called on a clock and until last clock
3639 * consumer calls clk_put() and the struct clk object is freed.
3640 */
3641static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3642{
3643 return -ENXIO;
3644}
3645
3646static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3647{
3648 WARN_ON_ONCE(1);
3649}
3650
3651static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3652 unsigned long parent_rate)
3653{
3654 return -ENXIO;
3655}
3656
3657static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3658{
3659 return -ENXIO;
3660}
3661
3662static const struct clk_ops clk_nodrv_ops = {
3663 .enable = clk_nodrv_prepare_enable,
3664 .disable = clk_nodrv_disable_unprepare,
3665 .prepare = clk_nodrv_prepare_enable,
3666 .unprepare = clk_nodrv_disable_unprepare,
3667 .set_rate = clk_nodrv_set_rate,
3668 .set_parent = clk_nodrv_set_parent,
3669};
3670
Mark Brown1df5c932012-04-18 09:07:12 +01003671/**
3672 * clk_unregister - unregister a currently registered clock
3673 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003674 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003675void clk_unregister(struct clk *clk)
3676{
3677 unsigned long flags;
3678
Stephen Boyd6314b672014-09-04 23:37:49 -07003679 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3680 return;
3681
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003682 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003683
3684 clk_prepare_lock();
3685
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003686 if (clk->core->ops == &clk_nodrv_ops) {
3687 pr_err("%s: unregistered clock: %s\n", __func__,
3688 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003689 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003690 }
3691 /*
3692 * Assign empty clock ops for consumers that might still hold
3693 * a reference to this clock.
3694 */
3695 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003696 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003697 clk_enable_unlock(flags);
3698
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003699 if (!hlist_empty(&clk->core->children)) {
3700 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003701 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003702
3703 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003704 hlist_for_each_entry_safe(child, t, &clk->core->children,
3705 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01003706 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003707 }
3708
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003709 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003710
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003711 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003712 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003713 __func__, clk->core->name);
Jerome Brunete55a8392017-12-01 22:51:56 +01003714
3715 if (clk->core->protect_count)
3716 pr_warn("%s: unregistering protected clock: %s\n",
3717 __func__, clk->core->name);
3718
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003719 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003720unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003721 clk_prepare_unlock();
3722}
Mark Brown1df5c932012-04-18 09:07:12 +01003723EXPORT_SYMBOL_GPL(clk_unregister);
3724
Stephen Boyd41438042016-02-05 17:02:52 -08003725/**
3726 * clk_hw_unregister - unregister a currently registered clk_hw
3727 * @hw: hardware-specific clock data to unregister
3728 */
3729void clk_hw_unregister(struct clk_hw *hw)
3730{
3731 clk_unregister(hw->clk);
3732}
3733EXPORT_SYMBOL_GPL(clk_hw_unregister);
3734
Stephen Boyd46c87732012-09-24 13:38:04 -07003735static void devm_clk_release(struct device *dev, void *res)
3736{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003737 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003738}
3739
Stephen Boyd41438042016-02-05 17:02:52 -08003740static void devm_clk_hw_release(struct device *dev, void *res)
3741{
3742 clk_hw_unregister(*(struct clk_hw **)res);
3743}
3744
Stephen Boyd46c87732012-09-24 13:38:04 -07003745/**
3746 * devm_clk_register - resource managed clk_register()
3747 * @dev: device that is registering this clock
3748 * @hw: link to hardware-specific clock data
3749 *
3750 * Managed clk_register(). Clocks returned from this function are
3751 * automatically clk_unregister()ed on driver detach. See clk_register() for
3752 * more information.
3753 */
3754struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3755{
3756 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003757 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003758
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003759 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3760 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003761 return ERR_PTR(-ENOMEM);
3762
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003763 clk = clk_register(dev, hw);
3764 if (!IS_ERR(clk)) {
3765 *clkp = clk;
3766 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003767 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003768 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003769 }
3770
3771 return clk;
3772}
3773EXPORT_SYMBOL_GPL(devm_clk_register);
3774
Stephen Boyd41438042016-02-05 17:02:52 -08003775/**
3776 * devm_clk_hw_register - resource managed clk_hw_register()
3777 * @dev: device that is registering this clock
3778 * @hw: link to hardware-specific clock data
3779 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003780 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003781 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3782 * for more information.
3783 */
3784int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3785{
3786 struct clk_hw **hwp;
3787 int ret;
3788
3789 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3790 if (!hwp)
3791 return -ENOMEM;
3792
3793 ret = clk_hw_register(dev, hw);
3794 if (!ret) {
3795 *hwp = hw;
3796 devres_add(dev, hwp);
3797 } else {
3798 devres_free(hwp);
3799 }
3800
3801 return ret;
3802}
3803EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3804
Stephen Boyd46c87732012-09-24 13:38:04 -07003805static int devm_clk_match(struct device *dev, void *res, void *data)
3806{
3807 struct clk *c = res;
3808 if (WARN_ON(!c))
3809 return 0;
3810 return c == data;
3811}
3812
Stephen Boyd41438042016-02-05 17:02:52 -08003813static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3814{
3815 struct clk_hw *hw = res;
3816
3817 if (WARN_ON(!hw))
3818 return 0;
3819 return hw == data;
3820}
3821
Stephen Boyd46c87732012-09-24 13:38:04 -07003822/**
3823 * devm_clk_unregister - resource managed clk_unregister()
3824 * @clk: clock to unregister
3825 *
3826 * Deallocate a clock allocated with devm_clk_register(). Normally
3827 * this function will not need to be called and the resource management
3828 * code will ensure that the resource is freed.
3829 */
3830void devm_clk_unregister(struct device *dev, struct clk *clk)
3831{
3832 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3833}
3834EXPORT_SYMBOL_GPL(devm_clk_unregister);
3835
Stephen Boyd41438042016-02-05 17:02:52 -08003836/**
3837 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3838 * @dev: device that is unregistering the hardware-specific clock data
3839 * @hw: link to hardware-specific clock data
3840 *
3841 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3842 * this function will not need to be called and the resource management
3843 * code will ensure that the resource is freed.
3844 */
3845void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3846{
3847 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3848 hw));
3849}
3850EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3851
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003852/*
3853 * clkdev helpers
3854 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003855
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003856void __clk_put(struct clk *clk)
3857{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003858 struct module *owner;
3859
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003860 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003861 return;
3862
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003863 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003864
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01003865 /*
3866 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3867 * given user should be balanced with calls to clk_rate_exclusive_put()
3868 * and by that same consumer
3869 */
3870 if (WARN_ON(clk->exclusive_count)) {
3871 /* We voiced our concern, let's sanitize the situation */
3872 clk->core->protect_count -= (clk->exclusive_count - 1);
3873 clk_core_rate_unprotect(clk->core);
3874 clk->exclusive_count = 0;
3875 }
3876
Stephen Boyd50595f82015-02-06 11:42:44 -08003877 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003878 if (clk->min_rate > clk->core->req_rate ||
3879 clk->max_rate < clk->core->req_rate)
3880 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3881
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003882 owner = clk->core->owner;
3883 kref_put(&clk->core->ref, __clk_release);
3884
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003885 clk_prepare_unlock();
3886
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003887 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003888
Stephen Boyd1df40462018-12-11 08:32:04 -08003889 free_clk(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003890}
3891
Mike Turquetteb24764902012-03-15 23:11:19 -07003892/*** clk rate change notifiers ***/
3893
3894/**
3895 * clk_notifier_register - add a clk rate change notifier
3896 * @clk: struct clk * to watch
3897 * @nb: struct notifier_block * with callback info
3898 *
3899 * Request notification when clk's rate changes. This uses an SRCU
3900 * notifier because we want it to block and notifier unregistrations are
3901 * uncommon. The callbacks associated with the notifier must not
3902 * re-enter into the clk framework by calling any top-level clk APIs;
3903 * this will cause a nested prepare_lock mutex.
3904 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003905 * In all notification cases (pre, post and abort rate change) the original
3906 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3907 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003908 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003909 * clk_notifier_register() must be called from non-atomic context.
3910 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3911 * allocation failure; otherwise, passes along the return value of
3912 * srcu_notifier_chain_register().
3913 */
3914int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3915{
3916 struct clk_notifier *cn;
3917 int ret = -ENOMEM;
3918
3919 if (!clk || !nb)
3920 return -EINVAL;
3921
Mike Turquetteeab89f62013-03-28 13:59:01 -07003922 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003923
3924 /* search the list of notifiers for this clk */
3925 list_for_each_entry(cn, &clk_notifier_list, node)
3926 if (cn->clk == clk)
3927 break;
3928
3929 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3930 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02003931 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003932 if (!cn)
3933 goto out;
3934
3935 cn->clk = clk;
3936 srcu_init_notifier_head(&cn->notifier_head);
3937
3938 list_add(&cn->node, &clk_notifier_list);
3939 }
3940
3941 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3942
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003943 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003944
3945out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003946 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003947
3948 return ret;
3949}
3950EXPORT_SYMBOL_GPL(clk_notifier_register);
3951
3952/**
3953 * clk_notifier_unregister - remove a clk rate change notifier
3954 * @clk: struct clk *
3955 * @nb: struct notifier_block * with callback info
3956 *
3957 * Request no further notification for changes to 'clk' and frees memory
3958 * allocated in clk_notifier_register.
3959 *
3960 * Returns -EINVAL if called with null arguments; otherwise, passes
3961 * along the return value of srcu_notifier_chain_unregister().
3962 */
3963int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3964{
3965 struct clk_notifier *cn = NULL;
3966 int ret = -EINVAL;
3967
3968 if (!clk || !nb)
3969 return -EINVAL;
3970
Mike Turquetteeab89f62013-03-28 13:59:01 -07003971 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003972
3973 list_for_each_entry(cn, &clk_notifier_list, node)
3974 if (cn->clk == clk)
3975 break;
3976
3977 if (cn->clk == clk) {
3978 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3979
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003980 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07003981
3982 /* XXX the notifier code should handle this better */
3983 if (!cn->notifier_head.head) {
3984 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08003985 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07003986 kfree(cn);
3987 }
3988
3989 } else {
3990 ret = -ENOENT;
3991 }
3992
Mike Turquetteeab89f62013-03-28 13:59:01 -07003993 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003994
3995 return ret;
3996}
3997EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05003998
3999#ifdef CONFIG_OF
4000/**
4001 * struct of_clk_provider - Clock provider registration structure
4002 * @link: Entry in global list of clock providers
4003 * @node: Pointer to device tree node of clock provider
4004 * @get: Get clock callback. Returns NULL or a struct clk for the
4005 * given clock specifier
4006 * @data: context pointer to be passed into @get callback
4007 */
4008struct of_clk_provider {
4009 struct list_head link;
4010
4011 struct device_node *node;
4012 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004013 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05004014 void *data;
4015};
4016
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304017static const struct of_device_id __clk_of_table_sentinel
4018 __used __section(__clk_of_table_end);
4019
Grant Likely766e6a42012-04-09 14:50:06 -05004020static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004021static DEFINE_MUTEX(of_clk_mutex);
4022
Grant Likely766e6a42012-04-09 14:50:06 -05004023struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
4024 void *data)
4025{
4026 return data;
4027}
4028EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
4029
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004030struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
4031{
4032 return data;
4033}
4034EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
4035
Shawn Guo494bfec2012-08-22 21:36:27 +08004036struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
4037{
4038 struct clk_onecell_data *clk_data = data;
4039 unsigned int idx = clkspec->args[0];
4040
4041 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02004042 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08004043 return ERR_PTR(-EINVAL);
4044 }
4045
4046 return clk_data->clks[idx];
4047}
4048EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
4049
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004050struct clk_hw *
4051of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
4052{
4053 struct clk_hw_onecell_data *hw_data = data;
4054 unsigned int idx = clkspec->args[0];
4055
4056 if (idx >= hw_data->num) {
4057 pr_err("%s: invalid index %u\n", __func__, idx);
4058 return ERR_PTR(-EINVAL);
4059 }
4060
4061 return hw_data->hws[idx];
4062}
4063EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
4064
Grant Likely766e6a42012-04-09 14:50:06 -05004065/**
4066 * of_clk_add_provider() - Register a clock provider for a node
4067 * @np: Device node pointer associated with clock provider
4068 * @clk_src_get: callback for decoding clock
4069 * @data: context pointer for @clk_src_get callback.
4070 */
4071int of_clk_add_provider(struct device_node *np,
4072 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
4073 void *data),
4074 void *data)
4075{
4076 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004077 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004078
Markus Elfring1808a322017-04-20 09:30:52 +02004079 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05004080 if (!cp)
4081 return -ENOMEM;
4082
4083 cp->node = of_node_get(np);
4084 cp->data = data;
4085 cp->get = clk_src_get;
4086
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004087 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004088 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004089 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05004090 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05004091
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004092 ret = of_clk_set_defaults(np, true);
4093 if (ret < 0)
4094 of_clk_del_provider(np);
4095
4096 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004097}
4098EXPORT_SYMBOL_GPL(of_clk_add_provider);
4099
4100/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004101 * of_clk_add_hw_provider() - Register a clock provider for a node
4102 * @np: Device node pointer associated with clock provider
4103 * @get: callback for decoding clk_hw
4104 * @data: context pointer for @get callback.
4105 */
4106int of_clk_add_hw_provider(struct device_node *np,
4107 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4108 void *data),
4109 void *data)
4110{
4111 struct of_clk_provider *cp;
4112 int ret;
4113
4114 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4115 if (!cp)
4116 return -ENOMEM;
4117
4118 cp->node = of_node_get(np);
4119 cp->data = data;
4120 cp->get_hw = get;
4121
4122 mutex_lock(&of_clk_mutex);
4123 list_add(&cp->link, &of_clk_providers);
4124 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05004125 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004126
4127 ret = of_clk_set_defaults(np, true);
4128 if (ret < 0)
4129 of_clk_del_provider(np);
4130
4131 return ret;
4132}
4133EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
4134
Stephen Boydaa795c42017-09-01 16:16:40 -07004135static void devm_of_clk_release_provider(struct device *dev, void *res)
4136{
4137 of_clk_del_provider(*(struct device_node **)res);
4138}
4139
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004140/*
4141 * We allow a child device to use its parent device as the clock provider node
4142 * for cases like MFD sub-devices where the child device driver wants to use
4143 * devm_*() APIs but not list the device in DT as a sub-node.
4144 */
4145static struct device_node *get_clk_provider_node(struct device *dev)
4146{
4147 struct device_node *np, *parent_np;
4148
4149 np = dev->of_node;
4150 parent_np = dev->parent ? dev->parent->of_node : NULL;
4151
4152 if (!of_find_property(np, "#clock-cells", NULL))
4153 if (of_find_property(parent_np, "#clock-cells", NULL))
4154 np = parent_np;
4155
4156 return np;
4157}
4158
Matti Vaittinene45838b2018-12-04 13:33:48 +02004159/**
4160 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4161 * @dev: Device acting as the clock provider (used for DT node and lifetime)
4162 * @get: callback for decoding clk_hw
4163 * @data: context pointer for @get callback
4164 *
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004165 * Registers clock provider for given device's node. If the device has no DT
4166 * node or if the device node lacks of clock provider information (#clock-cells)
4167 * then the parent device's node is scanned for this information. If parent node
4168 * has the #clock-cells then it is used in registration. Provider is
4169 * automatically released at device exit.
Matti Vaittinene45838b2018-12-04 13:33:48 +02004170 *
4171 * Return: 0 on success or an errno on failure.
4172 */
Stephen Boydaa795c42017-09-01 16:16:40 -07004173int devm_of_clk_add_hw_provider(struct device *dev,
4174 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4175 void *data),
4176 void *data)
4177{
4178 struct device_node **ptr, *np;
4179 int ret;
4180
4181 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
4182 GFP_KERNEL);
4183 if (!ptr)
4184 return -ENOMEM;
4185
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004186 np = get_clk_provider_node(dev);
Stephen Boydaa795c42017-09-01 16:16:40 -07004187 ret = of_clk_add_hw_provider(np, get, data);
4188 if (!ret) {
4189 *ptr = np;
4190 devres_add(dev, ptr);
4191 } else {
4192 devres_free(ptr);
4193 }
4194
4195 return ret;
4196}
4197EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
4198
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004199/**
Grant Likely766e6a42012-04-09 14:50:06 -05004200 * of_clk_del_provider() - Remove a previously registered clock provider
4201 * @np: Device node pointer associated with clock provider
4202 */
4203void of_clk_del_provider(struct device_node *np)
4204{
4205 struct of_clk_provider *cp;
4206
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004207 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004208 list_for_each_entry(cp, &of_clk_providers, link) {
4209 if (cp->node == np) {
4210 list_del(&cp->link);
4211 of_node_put(cp->node);
4212 kfree(cp);
4213 break;
4214 }
4215 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004216 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004217}
4218EXPORT_SYMBOL_GPL(of_clk_del_provider);
4219
Stephen Boydaa795c42017-09-01 16:16:40 -07004220static int devm_clk_provider_match(struct device *dev, void *res, void *data)
4221{
4222 struct device_node **np = res;
4223
4224 if (WARN_ON(!np || !*np))
4225 return 0;
4226
4227 return *np == data;
4228}
4229
Matti Vaittinene45838b2018-12-04 13:33:48 +02004230/**
4231 * devm_of_clk_del_provider() - Remove clock provider registered using devm
4232 * @dev: Device to whose lifetime the clock provider was bound
4233 */
Stephen Boydaa795c42017-09-01 16:16:40 -07004234void devm_of_clk_del_provider(struct device *dev)
4235{
4236 int ret;
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004237 struct device_node *np = get_clk_provider_node(dev);
Stephen Boydaa795c42017-09-01 16:16:40 -07004238
4239 ret = devres_release(dev, devm_of_clk_release_provider,
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004240 devm_clk_provider_match, np);
Stephen Boydaa795c42017-09-01 16:16:40 -07004241
4242 WARN_ON(ret);
4243}
4244EXPORT_SYMBOL(devm_of_clk_del_provider);
4245
Stephen Boyd5dc7e842019-03-08 10:35:01 -08004246/*
4247 * Beware the return values when np is valid, but no clock provider is found.
4248 * If name == NULL, the function returns -ENOENT.
4249 * If name != NULL, the function returns -EINVAL. This is because
4250 * of_parse_phandle_with_args() is called even if of_property_match_string()
4251 * returns an error.
4252 */
Stephen Boydcf13f282018-12-19 15:09:14 -08004253static int of_parse_clkspec(const struct device_node *np, int index,
4254 const char *name, struct of_phandle_args *out_args)
Stephen Boyd44722872018-12-19 10:59:55 -08004255{
4256 int ret = -ENOENT;
4257
4258 /* Walk up the tree of devices looking for a clock property that matches */
4259 while (np) {
4260 /*
4261 * For named clocks, first look up the name in the
4262 * "clock-names" property. If it cannot be found, then index
4263 * will be an error code and of_parse_phandle_with_args() will
4264 * return -EINVAL.
4265 */
4266 if (name)
4267 index = of_property_match_string(np, "clock-names", name);
4268 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
4269 index, out_args);
4270 if (!ret)
4271 break;
4272 if (name && index >= 0)
4273 break;
4274
4275 /*
4276 * No matching clock found on this node. If the parent node
4277 * has a "clock-ranges" property, then we can try one of its
4278 * clocks.
4279 */
4280 np = np->parent;
4281 if (np && !of_get_property(np, "clock-ranges", NULL))
4282 break;
4283 index = 0;
4284 }
4285
4286 return ret;
4287}
4288
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004289static struct clk_hw *
4290__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4291 struct of_phandle_args *clkspec)
4292{
4293 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004294
Stephen Boyd74002fc2016-08-25 13:35:36 -07004295 if (provider->get_hw)
4296 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004297
Stephen Boyd74002fc2016-08-25 13:35:36 -07004298 clk = provider->get(clkspec, provider->data);
4299 if (IS_ERR(clk))
4300 return ERR_CAST(clk);
4301 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004302}
4303
Stephen Boydcf13f282018-12-19 15:09:14 -08004304static struct clk_hw *
4305of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
Grant Likely766e6a42012-04-09 14:50:06 -05004306{
4307 struct of_clk_provider *provider;
Stephen Boyd1df40462018-12-11 08:32:04 -08004308 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05004309
Stephen Boyd306c3422015-02-05 15:39:11 -08004310 if (!clkspec)
4311 return ERR_PTR(-EINVAL);
4312
Stephen Boyd306c3422015-02-05 15:39:11 -08004313 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004314 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07004315 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004316 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd1df40462018-12-11 08:32:04 -08004317 if (!IS_ERR(hw))
4318 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08004319 }
Grant Likely766e6a42012-04-09 14:50:06 -05004320 }
Stephen Boyd306c3422015-02-05 15:39:11 -08004321 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004322
Stephen Boyd44722872018-12-19 10:59:55 -08004323 return hw;
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004324}
4325
Stephen Boyd306c3422015-02-05 15:39:11 -08004326/**
4327 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4328 * @clkspec: pointer to a clock specifier data structure
4329 *
4330 * This function looks up a struct clk from the registered list of clock
4331 * providers, an input is a clock specifier data structure as returned
4332 * from the of_parse_phandle_with_args() function call.
4333 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004334struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4335{
Stephen Boyd44722872018-12-19 10:59:55 -08004336 struct clk_hw *hw = of_clk_get_hw_from_clkspec(clkspec);
4337
Stephen Boydefa85042018-12-11 08:34:16 -08004338 return clk_hw_create_clk(NULL, hw, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05004339}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06004340EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05004341
Stephen Boydcf13f282018-12-19 15:09:14 -08004342struct clk_hw *of_clk_get_hw(struct device_node *np, int index,
4343 const char *con_id)
4344{
4345 int ret;
4346 struct clk_hw *hw;
4347 struct of_phandle_args clkspec;
4348
4349 ret = of_parse_clkspec(np, index, con_id, &clkspec);
4350 if (ret)
4351 return ERR_PTR(ret);
4352
4353 hw = of_clk_get_hw_from_clkspec(&clkspec);
4354 of_node_put(clkspec.np);
4355
4356 return hw;
4357}
4358
4359static struct clk *__of_clk_get(struct device_node *np,
4360 int index, const char *dev_id,
4361 const char *con_id)
4362{
4363 struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
4364
4365 return clk_hw_create_clk(NULL, hw, dev_id, con_id);
4366}
4367
4368struct clk *of_clk_get(struct device_node *np, int index)
4369{
4370 return __of_clk_get(np, index, np->full_name, NULL);
4371}
4372EXPORT_SYMBOL(of_clk_get);
4373
4374/**
4375 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
4376 * @np: pointer to clock consumer node
4377 * @name: name of consumer's clock input, or NULL for the first clock reference
4378 *
4379 * This function parses the clocks and clock-names properties,
4380 * and uses them to look up the struct clk from the registered list of clock
4381 * providers.
4382 */
4383struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
4384{
4385 if (!np)
4386 return ERR_PTR(-ENOENT);
4387
Kuninori Morimoto65cf20a2019-03-06 16:18:28 +09004388 return __of_clk_get(np, 0, np->full_name, name);
Stephen Boydcf13f282018-12-19 15:09:14 -08004389}
4390EXPORT_SYMBOL(of_clk_get_by_name);
4391
Stephen Boyd929e7f32016-02-19 15:52:32 -08004392/**
4393 * of_clk_get_parent_count() - Count the number of clocks a device node has
4394 * @np: device node to count
4395 *
4396 * Returns: The number of clocks that are possible parents of this node
4397 */
4398unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07004399{
Stephen Boyd929e7f32016-02-19 15:52:32 -08004400 int count;
4401
4402 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4403 if (count < 0)
4404 return 0;
4405
4406 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07004407}
4408EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4409
Grant Likely766e6a42012-04-09 14:50:06 -05004410const char *of_clk_get_parent_name(struct device_node *np, int index)
4411{
4412 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004413 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05004414 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004415 const __be32 *vp;
4416 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05004417 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004418 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004419 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05004420
Grant Likely766e6a42012-04-09 14:50:06 -05004421 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4422 &clkspec);
4423 if (rc)
4424 return NULL;
4425
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004426 index = clkspec.args_count ? clkspec.args[0] : 0;
4427 count = 0;
4428
4429 /* if there is an indices property, use it to transfer the index
4430 * specified into an array offset for the clock-output-names property.
4431 */
4432 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4433 if (index == pv) {
4434 index = count;
4435 break;
4436 }
4437 count++;
4438 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09004439 /* We went off the end of 'clock-indices' without finding it */
4440 if (prop && !vp)
4441 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004442
Grant Likely766e6a42012-04-09 14:50:06 -05004443 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004444 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004445 &clk_name) < 0) {
4446 /*
4447 * Best effort to get the name if the clock has been
4448 * registered with the framework. If the clock isn't
4449 * registered, we return the node name as the name of
4450 * the clock as long as #clock-cells = 0.
4451 */
4452 clk = of_clk_get_from_provider(&clkspec);
4453 if (IS_ERR(clk)) {
4454 if (clkspec.args_count == 0)
4455 clk_name = clkspec.np->name;
4456 else
4457 clk_name = NULL;
4458 } else {
4459 clk_name = __clk_get_name(clk);
4460 clk_put(clk);
4461 }
4462 }
4463
Grant Likely766e6a42012-04-09 14:50:06 -05004464
4465 of_node_put(clkspec.np);
4466 return clk_name;
4467}
4468EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4469
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05004470/**
4471 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4472 * number of parents
4473 * @np: Device node pointer associated with clock provider
4474 * @parents: pointer to char array that hold the parents' names
4475 * @size: size of the @parents array
4476 *
4477 * Return: number of parents for the clock node.
4478 */
4479int of_clk_parent_fill(struct device_node *np, const char **parents,
4480 unsigned int size)
4481{
4482 unsigned int i = 0;
4483
4484 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4485 i++;
4486
4487 return i;
4488}
4489EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4490
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004491struct clock_provider {
Geert Uytterhoevena59704332018-04-10 15:06:05 +02004492 void (*clk_init_cb)(struct device_node *);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004493 struct device_node *np;
4494 struct list_head node;
4495};
4496
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004497/*
4498 * This function looks for a parent clock. If there is one, then it
4499 * checks that the provider for this parent clock was initialized, in
4500 * this case the parent clock will be ready.
4501 */
4502static int parent_ready(struct device_node *np)
4503{
4504 int i = 0;
4505
4506 while (true) {
4507 struct clk *clk = of_clk_get(np, i);
4508
4509 /* this parent is ready we can check the next one */
4510 if (!IS_ERR(clk)) {
4511 clk_put(clk);
4512 i++;
4513 continue;
4514 }
4515
4516 /* at least one parent is not ready, we exit now */
4517 if (PTR_ERR(clk) == -EPROBE_DEFER)
4518 return 0;
4519
4520 /*
4521 * Here we make assumption that the device tree is
4522 * written correctly. So an error means that there is
4523 * no more parent. As we didn't exit yet, then the
4524 * previous parent are ready. If there is no clock
4525 * parent, no need to wait for them, then we can
4526 * consider their absence as being ready
4527 */
4528 return 1;
4529 }
4530}
4531
Grant Likely766e6a42012-04-09 14:50:06 -05004532/**
Lee Jonesd56f8992016-02-11 13:19:11 -08004533 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4534 * @np: Device node pointer associated with clock provider
4535 * @index: clock index
Geert Uytterhoevenf7ae7502018-01-03 12:06:14 +01004536 * @flags: pointer to top-level framework flags
Lee Jonesd56f8992016-02-11 13:19:11 -08004537 *
4538 * Detects if the clock-critical property exists and, if so, sets the
4539 * corresponding CLK_IS_CRITICAL flag.
4540 *
4541 * Do not use this function. It exists only for legacy Device Tree
4542 * bindings, such as the one-clock-per-node style that are outdated.
4543 * Those bindings typically put all clock data into .dts and the Linux
4544 * driver has no clock data, thus making it impossible to set this flag
4545 * correctly from the driver. Only those drivers may call
4546 * of_clk_detect_critical from their setup functions.
4547 *
4548 * Return: error code or zero on success
4549 */
4550int of_clk_detect_critical(struct device_node *np,
4551 int index, unsigned long *flags)
4552{
4553 struct property *prop;
4554 const __be32 *cur;
4555 uint32_t idx;
4556
4557 if (!np || !flags)
4558 return -EINVAL;
4559
4560 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4561 if (index == idx)
4562 *flags |= CLK_IS_CRITICAL;
4563
4564 return 0;
4565}
4566
4567/**
Grant Likely766e6a42012-04-09 14:50:06 -05004568 * of_clk_init() - Scan and init clock providers from the DT
4569 * @matches: array of compatible values and init functions for providers.
4570 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004571 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01004572 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004573 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05004574 */
4575void __init of_clk_init(const struct of_device_id *matches)
4576{
Alex Elder7f7ed582013-08-22 11:31:31 -05004577 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05004578 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004579 struct clock_provider *clk_provider, *next;
4580 bool is_init_done;
4581 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07004582 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05004583
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304584 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03004585 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304586
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004587 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05004588 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004589 struct clock_provider *parent;
4590
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01004591 if (!of_device_is_available(np))
4592 continue;
4593
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004594 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4595 if (!parent) {
4596 list_for_each_entry_safe(clk_provider, next,
4597 &clk_provider_list, node) {
4598 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004599 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004600 kfree(clk_provider);
4601 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004602 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004603 return;
4604 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004605
4606 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004607 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01004608 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004609 }
4610
4611 while (!list_empty(&clk_provider_list)) {
4612 is_init_done = false;
4613 list_for_each_entry_safe(clk_provider, next,
4614 &clk_provider_list, node) {
4615 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004616
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02004617 /* Don't populate platform devices */
4618 of_node_set_flag(clk_provider->np,
4619 OF_POPULATED);
4620
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004621 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004622 of_clk_set_defaults(clk_provider->np, true);
4623
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004624 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004625 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004626 kfree(clk_provider);
4627 is_init_done = true;
4628 }
4629 }
4630
4631 /*
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01004632 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004633 * remaining providers during the last loop, so now we
4634 * initialize all the remaining ones unconditionally
4635 * in case the clock parent was not mandatory
4636 */
4637 if (!is_init_done)
4638 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05004639 }
4640}
4641#endif