blob: 48931f442de8fb7a162809d35322ff3068925b15 [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
Stephen Boydbdcf1dc2019-08-28 11:19:59 -070040static struct hlist_head *all_lists[] = {
41 &clk_root_list,
42 &clk_orphan_list,
43 NULL,
44};
45
Michael Turquetteb09d6d92015-01-29 14:22:50 -080046/*** private data structures ***/
47
Stephen Boydfc0c2092019-04-12 11:31:47 -070048struct clk_parent_map {
49 const struct clk_hw *hw;
50 struct clk_core *core;
51 const char *fw_name;
52 const char *name;
Stephen Boyd601b6e92019-04-12 11:31:49 -070053 int index;
Stephen Boydfc0c2092019-04-12 11:31:47 -070054};
55
Michael Turquetteb09d6d92015-01-29 14:22:50 -080056struct clk_core {
57 const char *name;
58 const struct clk_ops *ops;
59 struct clk_hw *hw;
60 struct module *owner;
Marek Szyprowski9a34b452017-08-21 10:04:59 +020061 struct device *dev;
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -070062 struct device_node *of_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080063 struct clk_core *parent;
Stephen Boydfc0c2092019-04-12 11:31:47 -070064 struct clk_parent_map *parents;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080065 u8 num_parents;
66 u8 new_parent_index;
67 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010068 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080069 unsigned long new_rate;
70 struct clk_core *new_parent;
71 struct clk_core *new_child;
72 unsigned long flags;
Heiko Stuebnere6500342015-04-22 22:53:05 +020073 bool orphan;
Miquel Raynal24478832018-12-04 20:24:37 +010074 bool rpm_enabled;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080075 unsigned int enable_count;
76 unsigned int prepare_count;
Jerome Brunete55a8392017-12-01 22:51:56 +010077 unsigned int protect_count;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070078 unsigned long min_rate;
79 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080080 unsigned long accuracy;
81 int phase;
Jerome Brunet9fba7382018-06-19 16:41:41 +020082 struct clk_duty duty;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080083 struct hlist_head children;
84 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010085 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080086 unsigned int notifier_count;
87#ifdef CONFIG_DEBUG_FS
88 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020089 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080090#endif
91 struct kref ref;
92};
93
Stephen Boyddfc202e2015-02-02 14:37:41 -080094#define CREATE_TRACE_POINTS
95#include <trace/events/clk.h>
96
Michael Turquetteb09d6d92015-01-29 14:22:50 -080097struct clk {
98 struct clk_core *core;
Stephen Boydefa85042018-12-11 08:34:16 -080099 struct device *dev;
Michael Turquetteb09d6d92015-01-29 14:22:50 -0800100 const char *dev_id;
101 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100102 unsigned long min_rate;
103 unsigned long max_rate;
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100104 unsigned int exclusive_count;
Stephen Boyd50595f82015-02-06 11:42:44 -0800105 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -0800106};
107
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200108/*** runtime pm ***/
109static int clk_pm_runtime_get(struct clk_core *core)
110{
Miquel Raynal24478832018-12-04 20:24:37 +0100111 int ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200112
Miquel Raynal24478832018-12-04 20:24:37 +0100113 if (!core->rpm_enabled)
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200114 return 0;
115
116 ret = pm_runtime_get_sync(core->dev);
Rafael J. Wysocki64c7d7e2020-05-21 19:08:09 +0200117 if (ret < 0) {
118 pm_runtime_put_noidle(core->dev);
119 return ret;
120 }
121 return 0;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200122}
123
124static void clk_pm_runtime_put(struct clk_core *core)
125{
Miquel Raynal24478832018-12-04 20:24:37 +0100126 if (!core->rpm_enabled)
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200127 return;
128
129 pm_runtime_put_sync(core->dev);
130}
131
Mike Turquetteeab89f62013-03-28 13:59:01 -0700132/*** locking ***/
133static void clk_prepare_lock(void)
134{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700135 if (!mutex_trylock(&prepare_lock)) {
136 if (prepare_owner == current) {
137 prepare_refcnt++;
138 return;
139 }
140 mutex_lock(&prepare_lock);
141 }
142 WARN_ON_ONCE(prepare_owner != NULL);
143 WARN_ON_ONCE(prepare_refcnt != 0);
144 prepare_owner = current;
145 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700146}
147
148static void clk_prepare_unlock(void)
149{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700150 WARN_ON_ONCE(prepare_owner != current);
151 WARN_ON_ONCE(prepare_refcnt == 0);
152
153 if (--prepare_refcnt)
154 return;
155 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700156 mutex_unlock(&prepare_lock);
157}
158
159static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700160 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700161{
162 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700163
David Lechnera12aa8a2018-01-04 19:46:08 -0600164 /*
165 * On UP systems, spin_trylock_irqsave() always returns true, even if
166 * we already hold the lock. So, in that case, we rely only on
167 * reference counting.
168 */
169 if (!IS_ENABLED(CONFIG_SMP) ||
170 !spin_trylock_irqsave(&enable_lock, flags)) {
Mike Turquette533ddeb2013-03-28 13:59:02 -0700171 if (enable_owner == current) {
172 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700173 __acquire(enable_lock);
David Lechnera12aa8a2018-01-04 19:46:08 -0600174 if (!IS_ENABLED(CONFIG_SMP))
175 local_save_flags(flags);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700176 return flags;
177 }
178 spin_lock_irqsave(&enable_lock, flags);
179 }
180 WARN_ON_ONCE(enable_owner != NULL);
181 WARN_ON_ONCE(enable_refcnt != 0);
182 enable_owner = current;
183 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700184 return flags;
185}
186
187static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700188 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700189{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700190 WARN_ON_ONCE(enable_owner != current);
191 WARN_ON_ONCE(enable_refcnt == 0);
192
Stephen Boyda57aa182015-07-24 12:24:48 -0700193 if (--enable_refcnt) {
194 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700195 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700196 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700197 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700198 spin_unlock_irqrestore(&enable_lock, flags);
199}
200
Jerome Brunete55a8392017-12-01 22:51:56 +0100201static bool clk_core_rate_is_protected(struct clk_core *core)
202{
203 return core->protect_count;
204}
205
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700206static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530207{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200208 bool ret = false;
209
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700210 /*
211 * .is_prepared is optional for clocks that can prepare
212 * fall back to software usage counter if it is missing
213 */
214 if (!core->ops->is_prepared)
215 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530216
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200217 if (!clk_pm_runtime_get(core)) {
218 ret = core->ops->is_prepared(core->hw);
219 clk_pm_runtime_put(core);
220 }
221
222 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530223}
224
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700225static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530226{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200227 bool ret = false;
228
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700229 /*
230 * .is_enabled is only mandatory for clocks that gate
231 * fall back to software usage counter if .is_enabled is missing
232 */
233 if (!core->ops->is_enabled)
234 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530235
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200236 /*
237 * Check if clock controller's device is runtime active before
238 * calling .is_enabled callback. If not, assume that clock is
239 * disabled, because we might be called from atomic context, from
240 * which pm_runtime_get() is not allowed.
241 * This function is called mainly from clk_disable_unused_subtree,
242 * which ensures proper runtime pm activation of controller before
243 * taking enable spinlock, but the below check is needed if one tries
244 * to call it from other places.
245 */
Miquel Raynal24478832018-12-04 20:24:37 +0100246 if (core->rpm_enabled) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200247 pm_runtime_get_noresume(core->dev);
248 if (!pm_runtime_active(core->dev)) {
249 ret = false;
250 goto done;
251 }
252 }
253
254 ret = core->ops->is_enabled(core->hw);
255done:
Miquel Raynal24478832018-12-04 20:24:37 +0100256 if (core->rpm_enabled)
Dong Aisheng756efe12017-12-22 17:46:04 +0800257 pm_runtime_put(core->dev);
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200258
259 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530260}
261
Mike Turquetteb24764902012-03-15 23:11:19 -0700262/*** helper functions ***/
263
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200264const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700265{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100266 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700267}
Niels de Vos48950842012-12-13 13:12:25 +0100268EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700269
Stephen Boyde7df6f62015-08-12 13:04:56 -0700270const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700271{
272 return hw->core->name;
273}
274EXPORT_SYMBOL_GPL(clk_hw_get_name);
275
Russ Dill65800b22012-11-26 11:20:09 -0800276struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700277{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100278 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700279}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800280EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700281
Stephen Boyde7df6f62015-08-12 13:04:56 -0700282unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700283{
284 return hw->core->num_parents;
285}
286EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
287
Stephen Boyde7df6f62015-08-12 13:04:56 -0700288struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700289{
290 return hw->core->parent ? hw->core->parent->hw : NULL;
291}
292EXPORT_SYMBOL_GPL(clk_hw_get_parent);
293
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700294static struct clk_core *__clk_lookup_subtree(const char *name,
295 struct clk_core *core)
296{
297 struct clk_core *child;
298 struct clk_core *ret;
299
300 if (!strcmp(core->name, name))
301 return core;
302
303 hlist_for_each_entry(child, &core->children, child_node) {
304 ret = __clk_lookup_subtree(name, child);
305 if (ret)
306 return ret;
307 }
308
309 return NULL;
310}
311
312static struct clk_core *clk_core_lookup(const char *name)
313{
314 struct clk_core *root_clk;
315 struct clk_core *ret;
316
317 if (!name)
318 return NULL;
319
320 /* search the 'proper' clk tree first */
321 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
322 ret = __clk_lookup_subtree(name, root_clk);
323 if (ret)
324 return ret;
325 }
326
327 /* if not found, then search the orphan tree */
328 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
329 ret = __clk_lookup_subtree(name, root_clk);
330 if (ret)
331 return ret;
332 }
333
334 return NULL;
335}
336
Stephen Boyd4f8c6ab2019-08-13 14:41:47 -0700337#ifdef CONFIG_OF
338static int of_parse_clkspec(const struct device_node *np, int index,
339 const char *name, struct of_phandle_args *out_args);
340static struct clk_hw *
341of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec);
342#else
343static inline int of_parse_clkspec(const struct device_node *np, int index,
344 const char *name,
345 struct of_phandle_args *out_args)
346{
347 return -ENOENT;
348}
349static inline struct clk_hw *
350of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
351{
352 return ERR_PTR(-ENOENT);
353}
354#endif
355
Stephen Boydfc0c2092019-04-12 11:31:47 -0700356/**
Stephen Boyddde4eff2019-04-12 11:31:48 -0700357 * clk_core_get - Find the clk_core parent of a clk
Stephen Boydfc0c2092019-04-12 11:31:47 -0700358 * @core: clk to find parent of
Stephen Boyd1a079562019-04-30 10:22:30 -0700359 * @p_index: parent index to search for
Stephen Boydfc0c2092019-04-12 11:31:47 -0700360 *
361 * This is the preferred method for clk providers to find the parent of a
362 * clk when that parent is external to the clk controller. The parent_names
363 * array is indexed and treated as a local name matching a string in the device
Stephen Boyddde4eff2019-04-12 11:31:48 -0700364 * node's 'clock-names' property or as the 'con_id' matching the device's
365 * dev_name() in a clk_lookup. This allows clk providers to use their own
Stephen Boydfc0c2092019-04-12 11:31:47 -0700366 * namespace instead of looking for a globally unique parent string.
367 *
368 * For example the following DT snippet would allow a clock registered by the
369 * clock-controller@c001 that has a clk_init_data::parent_data array
370 * with 'xtal' in the 'name' member to find the clock provided by the
371 * clock-controller@f00abcd without needing to get the globally unique name of
372 * the xtal clk.
373 *
374 * parent: clock-controller@f00abcd {
375 * reg = <0xf00abcd 0xabcd>;
376 * #clock-cells = <0>;
377 * };
378 *
379 * clock-controller@c001 {
380 * reg = <0xc001 0xf00d>;
381 * clocks = <&parent>;
382 * clock-names = "xtal";
383 * #clock-cells = <1>;
384 * };
385 *
386 * Returns: -ENOENT when the provider can't be found or the clk doesn't
Stephen Boyd4f8c6ab2019-08-13 14:41:47 -0700387 * exist in the provider or the name can't be found in the DT node or
388 * in a clkdev lookup. NULL when the provider knows about the clk but it
389 * isn't provided on this system.
Stephen Boydfc0c2092019-04-12 11:31:47 -0700390 * A valid clk_core pointer when the clk can be found in the provider.
391 */
Stephen Boyd1a079562019-04-30 10:22:30 -0700392static struct clk_core *clk_core_get(struct clk_core *core, u8 p_index)
Stephen Boydfc0c2092019-04-12 11:31:47 -0700393{
Stephen Boyd1a079562019-04-30 10:22:30 -0700394 const char *name = core->parents[p_index].fw_name;
395 int index = core->parents[p_index].index;
Stephen Boyddde4eff2019-04-12 11:31:48 -0700396 struct clk_hw *hw = ERR_PTR(-ENOENT);
397 struct device *dev = core->dev;
398 const char *dev_id = dev ? dev_name(dev) : NULL;
Stephen Boydfc0c2092019-04-12 11:31:47 -0700399 struct device_node *np = core->of_node;
Stephen Boyd4f8c6ab2019-08-13 14:41:47 -0700400 struct of_phandle_args clkspec;
Stephen Boydfc0c2092019-04-12 11:31:47 -0700401
Stephen Boyd4f8c6ab2019-08-13 14:41:47 -0700402 if (np && (name || index >= 0) &&
403 !of_parse_clkspec(np, index, name, &clkspec)) {
404 hw = of_clk_get_hw_from_clkspec(&clkspec);
405 of_node_put(clkspec.np);
406 } else if (name) {
407 /*
408 * If the DT search above couldn't find the provider fallback to
409 * looking up via clkdev based clk_lookups.
410 */
Stephen Boyddde4eff2019-04-12 11:31:48 -0700411 hw = clk_find_hw(dev_id, name);
Stephen Boyd4f8c6ab2019-08-13 14:41:47 -0700412 }
Stephen Boyddde4eff2019-04-12 11:31:48 -0700413
414 if (IS_ERR(hw))
Stephen Boydfc0c2092019-04-12 11:31:47 -0700415 return ERR_CAST(hw);
416
417 return hw->core;
418}
419
420static void clk_core_fill_parent_index(struct clk_core *core, u8 index)
421{
422 struct clk_parent_map *entry = &core->parents[index];
423 struct clk_core *parent = ERR_PTR(-ENOENT);
424
425 if (entry->hw) {
426 parent = entry->hw->core;
427 /*
428 * We have a direct reference but it isn't registered yet?
429 * Orphan it and let clk_reparent() update the orphan status
430 * when the parent is registered.
431 */
432 if (!parent)
433 parent = ERR_PTR(-EPROBE_DEFER);
434 } else {
Stephen Boyd1a079562019-04-30 10:22:30 -0700435 parent = clk_core_get(core, index);
Masahiro Yamada45586c72020-02-03 17:37:45 -0800436 if (PTR_ERR(parent) == -ENOENT && entry->name)
Stephen Boydfc0c2092019-04-12 11:31:47 -0700437 parent = clk_core_lookup(entry->name);
438 }
439
440 /* Only cache it if it's not an error */
441 if (!IS_ERR(parent))
442 entry->core = parent;
443}
444
Stephen Boydd6968fc2015-04-30 13:54:13 -0700445static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100446 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100447{
Stephen Boydfc0c2092019-04-12 11:31:47 -0700448 if (!core || index >= core->num_parents || !core->parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100449 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900450
Stephen Boydfc0c2092019-04-12 11:31:47 -0700451 if (!core->parents[index].core)
452 clk_core_fill_parent_index(core, index);
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900453
Stephen Boydfc0c2092019-04-12 11:31:47 -0700454 return core->parents[index].core;
James Hogan7ef3dcc2013-07-29 12:24:58 +0100455}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100456
Stephen Boyde7df6f62015-08-12 13:04:56 -0700457struct clk_hw *
458clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700459{
460 struct clk_core *parent;
461
462 parent = clk_core_get_parent_by_index(hw->core, index);
463
464 return !parent ? NULL : parent->hw;
465}
466EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
467
Russ Dill65800b22012-11-26 11:20:09 -0800468unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700469{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100470 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700471}
472
Stephen Boydd6968fc2015-04-30 13:54:13 -0700473static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700474{
Stephen Boyd73d4f942019-02-01 15:39:50 -0800475 if (!core)
476 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700477
Stephen Boyd73d4f942019-02-01 15:39:50 -0800478 if (!core->num_parents || core->parent)
479 return core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700480
Stephen Boyd73d4f942019-02-01 15:39:50 -0800481 /*
482 * Clk must have a parent because num_parents > 0 but the parent isn't
483 * known yet. Best to return 0 as the rate of this clk until we can
484 * properly recalc the rate based on the parent's rate.
485 */
486 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700487}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100488
Stephen Boyde7df6f62015-08-12 13:04:56 -0700489unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700490{
491 return clk_core_get_rate_nolock(hw->core);
492}
493EXPORT_SYMBOL_GPL(clk_hw_get_rate);
494
Stephen Boyd0daa3762020-02-05 15:28:01 -0800495static unsigned long clk_core_get_accuracy_no_lock(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100496{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700497 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100498 return 0;
499
Stephen Boydd6968fc2015-04-30 13:54:13 -0700500 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100501}
502
Stephen Boyde7df6f62015-08-12 13:04:56 -0700503unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700504{
505 return hw->core->flags;
506}
507EXPORT_SYMBOL_GPL(clk_hw_get_flags);
508
Stephen Boyde7df6f62015-08-12 13:04:56 -0700509bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700510{
511 return clk_core_is_prepared(hw->core);
512}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100513EXPORT_SYMBOL_GPL(clk_hw_is_prepared);
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700514
Jerome Brunete55a8392017-12-01 22:51:56 +0100515bool clk_hw_rate_is_protected(const struct clk_hw *hw)
516{
517 return clk_core_rate_is_protected(hw->core);
518}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100519EXPORT_SYMBOL_GPL(clk_hw_rate_is_protected);
Jerome Brunete55a8392017-12-01 22:51:56 +0100520
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200521bool clk_hw_is_enabled(const struct clk_hw *hw)
522{
523 return clk_core_is_enabled(hw->core);
524}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100525EXPORT_SYMBOL_GPL(clk_hw_is_enabled);
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200526
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100527bool __clk_is_enabled(struct clk *clk)
528{
529 if (!clk)
530 return false;
531
532 return clk_core_is_enabled(clk->core);
533}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800534EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700535
Stephen Boyd15a02c12015-01-19 18:05:28 -0800536static bool mux_is_better_rate(unsigned long rate, unsigned long now,
537 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100538{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800539 if (flags & CLK_MUX_ROUND_CLOSEST)
540 return abs(now - rate) < abs(best - rate);
541
542 return now <= rate && now > best;
543}
544
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200545int clk_mux_determine_rate_flags(struct clk_hw *hw,
546 struct clk_rate_request *req,
547 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100548{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100549 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200550 int i, num_parents, ret;
551 unsigned long best = 0;
552 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100553
554 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100555 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
556 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200557 if (core->flags & CLK_SET_RATE_PARENT) {
558 ret = __clk_determine_rate(parent ? parent->hw : NULL,
559 &parent_req);
560 if (ret)
561 return ret;
562
563 best = parent_req.rate;
564 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100565 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200566 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100567 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200568 }
569
James Hogane366fdd2013-07-29 12:25:02 +0100570 goto out;
571 }
572
573 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100574 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100575 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100576 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100577 if (!parent)
578 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200579
580 if (core->flags & CLK_SET_RATE_PARENT) {
581 parent_req = *req;
582 ret = __clk_determine_rate(parent->hw, &parent_req);
583 if (ret)
584 continue;
585 } else {
586 parent_req.rate = clk_core_get_rate_nolock(parent);
587 }
588
589 if (mux_is_better_rate(req->rate, parent_req.rate,
590 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100591 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200592 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100593 }
594 }
595
Boris Brezillon57d866e2015-07-09 22:39:38 +0200596 if (!best_parent)
597 return -EINVAL;
598
James Hogane366fdd2013-07-29 12:25:02 +0100599out:
600 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200601 req->best_parent_hw = best_parent->hw;
602 req->best_parent_rate = best;
603 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100604
Boris Brezillon0817b622015-07-07 20:48:08 +0200605 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100606}
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200607EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800608
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100609struct clk *__clk_lookup(const char *name)
610{
611 struct clk_core *core = clk_core_lookup(name);
612
613 return !core ? NULL : core->hw->clk;
614}
615
Stephen Boydd6968fc2015-04-30 13:54:13 -0700616static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100617 unsigned long *min_rate,
618 unsigned long *max_rate)
619{
620 struct clk *clk_user;
621
Leonard Crestez9f776722019-07-02 16:27:10 +0300622 lockdep_assert_held(&prepare_lock);
623
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700624 *min_rate = core->min_rate;
625 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100626
Stephen Boydd6968fc2015-04-30 13:54:13 -0700627 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100628 *min_rate = max(*min_rate, clk_user->min_rate);
629
Stephen Boydd6968fc2015-04-30 13:54:13 -0700630 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100631 *max_rate = min(*max_rate, clk_user->max_rate);
632}
633
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700634void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
635 unsigned long max_rate)
636{
637 hw->core->min_rate = min_rate;
638 hw->core->max_rate = max_rate;
639}
640EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
641
Stephen Boyd15a02c12015-01-19 18:05:28 -0800642/*
Stephen Boyd777c1a42018-12-11 13:24:50 -0800643 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
644 * @hw: mux type clk to determine rate on
645 * @req: rate request, also used to return preferred parent and frequencies
646 *
Stephen Boyd15a02c12015-01-19 18:05:28 -0800647 * Helper for finding best parent to provide a given frequency. This can be used
648 * directly as a determine_rate callback (e.g. for a mux), or from a more
649 * complex clock that may combine a mux with other operations.
Stephen Boyd777c1a42018-12-11 13:24:50 -0800650 *
651 * Returns: 0 on success, -EERROR value on error
Stephen Boyd15a02c12015-01-19 18:05:28 -0800652 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200653int __clk_mux_determine_rate(struct clk_hw *hw,
654 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800655{
Boris Brezillon0817b622015-07-07 20:48:08 +0200656 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800657}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800658EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100659
Boris Brezillon0817b622015-07-07 20:48:08 +0200660int __clk_mux_determine_rate_closest(struct clk_hw *hw,
661 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800662{
Boris Brezillon0817b622015-07-07 20:48:08 +0200663 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800664}
665EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
666
Mike Turquetteb24764902012-03-15 23:11:19 -0700667/*** clk api ***/
668
Jerome Brunete55a8392017-12-01 22:51:56 +0100669static void clk_core_rate_unprotect(struct clk_core *core)
670{
671 lockdep_assert_held(&prepare_lock);
672
673 if (!core)
674 return;
675
Fabio Estevamab525dc2018-01-16 10:50:34 -0200676 if (WARN(core->protect_count == 0,
677 "%s already unprotected\n", core->name))
Jerome Brunete55a8392017-12-01 22:51:56 +0100678 return;
679
680 if (--core->protect_count > 0)
681 return;
682
683 clk_core_rate_unprotect(core->parent);
684}
685
686static int clk_core_rate_nuke_protect(struct clk_core *core)
687{
688 int ret;
689
690 lockdep_assert_held(&prepare_lock);
691
692 if (!core)
693 return -EINVAL;
694
695 if (core->protect_count == 0)
696 return 0;
697
698 ret = core->protect_count;
699 core->protect_count = 1;
700 clk_core_rate_unprotect(core);
701
702 return ret;
703}
704
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100705/**
706 * clk_rate_exclusive_put - release exclusivity over clock rate control
707 * @clk: the clk over which the exclusivity is released
708 *
709 * clk_rate_exclusive_put() completes a critical section during which a clock
710 * consumer cannot tolerate any other consumer making any operation on the
711 * clock which could result in a rate change or rate glitch. Exclusive clocks
712 * cannot have their rate changed, either directly or indirectly due to changes
713 * further up the parent chain of clocks. As a result, clocks up parent chain
714 * also get under exclusive control of the calling consumer.
715 *
716 * If exlusivity is claimed more than once on clock, even by the same consumer,
717 * the rate effectively gets locked as exclusivity can't be preempted.
718 *
719 * Calls to clk_rate_exclusive_put() must be balanced with calls to
720 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
721 * error status.
722 */
723void clk_rate_exclusive_put(struct clk *clk)
724{
725 if (!clk)
726 return;
727
728 clk_prepare_lock();
729
730 /*
731 * if there is something wrong with this consumer protect count, stop
732 * here before messing with the provider
733 */
734 if (WARN_ON(clk->exclusive_count <= 0))
735 goto out;
736
737 clk_core_rate_unprotect(clk->core);
738 clk->exclusive_count--;
739out:
740 clk_prepare_unlock();
741}
742EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
743
Jerome Brunete55a8392017-12-01 22:51:56 +0100744static void clk_core_rate_protect(struct clk_core *core)
745{
746 lockdep_assert_held(&prepare_lock);
747
748 if (!core)
749 return;
750
751 if (core->protect_count == 0)
752 clk_core_rate_protect(core->parent);
753
754 core->protect_count++;
755}
756
757static void clk_core_rate_restore_protect(struct clk_core *core, int count)
758{
759 lockdep_assert_held(&prepare_lock);
760
761 if (!core)
762 return;
763
764 if (count == 0)
765 return;
766
767 clk_core_rate_protect(core);
768 core->protect_count = count;
769}
770
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100771/**
772 * clk_rate_exclusive_get - get exclusivity over the clk rate control
773 * @clk: the clk over which the exclusity of rate control is requested
774 *
Andy Shevchenkoa37a5a92020-03-10 15:55:07 +0200775 * clk_rate_exclusive_get() begins a critical section during which a clock
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100776 * consumer cannot tolerate any other consumer making any operation on the
777 * clock which could result in a rate change or rate glitch. Exclusive clocks
778 * cannot have their rate changed, either directly or indirectly due to changes
779 * further up the parent chain of clocks. As a result, clocks up parent chain
780 * also get under exclusive control of the calling consumer.
781 *
782 * If exlusivity is claimed more than once on clock, even by the same consumer,
783 * the rate effectively gets locked as exclusivity can't be preempted.
784 *
785 * Calls to clk_rate_exclusive_get() should be balanced with calls to
786 * clk_rate_exclusive_put(). Calls to this function may sleep.
787 * Returns 0 on success, -EERROR otherwise
788 */
789int clk_rate_exclusive_get(struct clk *clk)
790{
791 if (!clk)
792 return 0;
793
794 clk_prepare_lock();
795 clk_core_rate_protect(clk->core);
796 clk->exclusive_count++;
797 clk_prepare_unlock();
798
799 return 0;
800}
801EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
802
Stephen Boydd6968fc2015-04-30 13:54:13 -0700803static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700804{
Stephen Boyda6334722015-05-06 17:00:54 -0700805 lockdep_assert_held(&prepare_lock);
806
Stephen Boydd6968fc2015-04-30 13:54:13 -0700807 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700808 return;
809
Fabio Estevamab525dc2018-01-16 10:50:34 -0200810 if (WARN(core->prepare_count == 0,
811 "%s already unprepared\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700812 return;
813
Fabio Estevamab525dc2018-01-16 10:50:34 -0200814 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
815 "Unpreparing critical %s\n", core->name))
Lee Jones2e20fbf2016-02-11 13:19:10 -0800816 return;
817
Jerome Brunet9461f7b2018-06-19 15:40:51 +0200818 if (core->flags & CLK_SET_RATE_GATE)
819 clk_core_rate_unprotect(core);
820
Stephen Boydd6968fc2015-04-30 13:54:13 -0700821 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700822 return;
823
Fabio Estevamab525dc2018-01-16 10:50:34 -0200824 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700825
Stephen Boydd6968fc2015-04-30 13:54:13 -0700826 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800827
Stephen Boydd6968fc2015-04-30 13:54:13 -0700828 if (core->ops->unprepare)
829 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700830
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200831 clk_pm_runtime_put(core);
832
Stephen Boydd6968fc2015-04-30 13:54:13 -0700833 trace_clk_unprepare_complete(core);
834 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700835}
836
Dong Aishenga6adc302016-06-30 17:31:11 +0800837static void clk_core_unprepare_lock(struct clk_core *core)
838{
839 clk_prepare_lock();
840 clk_core_unprepare(core);
841 clk_prepare_unlock();
842}
843
Mike Turquetteb24764902012-03-15 23:11:19 -0700844/**
845 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200846 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700847 *
848 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
849 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
850 * if the operation may sleep. One example is a clk which is accessed over
851 * I2c. In the complex case a clk gate operation may require a fast and a slow
852 * part. It is this reason that clk_unprepare and clk_disable are not mutually
853 * exclusive. In fact clk_disable must be called before clk_unprepare.
854 */
855void clk_unprepare(struct clk *clk)
856{
Stephen Boyd63589e92014-03-26 16:06:37 -0700857 if (IS_ERR_OR_NULL(clk))
858 return;
859
Dong Aishenga6adc302016-06-30 17:31:11 +0800860 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700861}
862EXPORT_SYMBOL_GPL(clk_unprepare);
863
Stephen Boydd6968fc2015-04-30 13:54:13 -0700864static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700865{
866 int ret = 0;
867
Stephen Boyda6334722015-05-06 17:00:54 -0700868 lockdep_assert_held(&prepare_lock);
869
Stephen Boydd6968fc2015-04-30 13:54:13 -0700870 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700871 return 0;
872
Stephen Boydd6968fc2015-04-30 13:54:13 -0700873 if (core->prepare_count == 0) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200874 ret = clk_pm_runtime_get(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700875 if (ret)
876 return ret;
877
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200878 ret = clk_core_prepare(core->parent);
879 if (ret)
880 goto runtime_put;
881
Stephen Boydd6968fc2015-04-30 13:54:13 -0700882 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800883
Stephen Boydd6968fc2015-04-30 13:54:13 -0700884 if (core->ops->prepare)
885 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800886
Stephen Boydd6968fc2015-04-30 13:54:13 -0700887 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800888
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200889 if (ret)
890 goto unprepare;
Mike Turquetteb24764902012-03-15 23:11:19 -0700891 }
892
Stephen Boydd6968fc2015-04-30 13:54:13 -0700893 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700894
Jerome Brunet9461f7b2018-06-19 15:40:51 +0200895 /*
896 * CLK_SET_RATE_GATE is a special case of clock protection
897 * Instead of a consumer claiming exclusive rate control, it is
898 * actually the provider which prevents any consumer from making any
899 * operation which could result in a rate change or rate glitch while
900 * the clock is prepared.
901 */
902 if (core->flags & CLK_SET_RATE_GATE)
903 clk_core_rate_protect(core);
904
Mike Turquetteb24764902012-03-15 23:11:19 -0700905 return 0;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200906unprepare:
907 clk_core_unprepare(core->parent);
908runtime_put:
909 clk_pm_runtime_put(core);
910 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700911}
912
Dong Aishenga6adc302016-06-30 17:31:11 +0800913static int clk_core_prepare_lock(struct clk_core *core)
914{
915 int ret;
916
917 clk_prepare_lock();
918 ret = clk_core_prepare(core);
919 clk_prepare_unlock();
920
921 return ret;
922}
923
Mike Turquetteb24764902012-03-15 23:11:19 -0700924/**
925 * clk_prepare - prepare a clock source
926 * @clk: the clk being prepared
927 *
928 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
929 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
930 * operation may sleep. One example is a clk which is accessed over I2c. In
931 * the complex case a clk ungate operation may require a fast and a slow part.
932 * It is this reason that clk_prepare and clk_enable are not mutually
933 * exclusive. In fact clk_prepare must be called before clk_enable.
934 * Returns 0 on success, -EERROR otherwise.
935 */
936int clk_prepare(struct clk *clk)
937{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100938 if (!clk)
939 return 0;
940
Dong Aishenga6adc302016-06-30 17:31:11 +0800941 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700942}
943EXPORT_SYMBOL_GPL(clk_prepare);
944
Stephen Boydd6968fc2015-04-30 13:54:13 -0700945static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700946{
Stephen Boyda6334722015-05-06 17:00:54 -0700947 lockdep_assert_held(&enable_lock);
948
Stephen Boydd6968fc2015-04-30 13:54:13 -0700949 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700950 return;
951
Fabio Estevamab525dc2018-01-16 10:50:34 -0200952 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700953 return;
954
Fabio Estevamab525dc2018-01-16 10:50:34 -0200955 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
956 "Disabling critical %s\n", core->name))
Lee Jones2e20fbf2016-02-11 13:19:10 -0800957 return;
958
Stephen Boydd6968fc2015-04-30 13:54:13 -0700959 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700960 return;
961
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700962 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800963
Stephen Boydd6968fc2015-04-30 13:54:13 -0700964 if (core->ops->disable)
965 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700966
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700967 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800968
Stephen Boydd6968fc2015-04-30 13:54:13 -0700969 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100970}
971
Dong Aishenga6adc302016-06-30 17:31:11 +0800972static void clk_core_disable_lock(struct clk_core *core)
973{
974 unsigned long flags;
975
976 flags = clk_enable_lock();
977 clk_core_disable(core);
978 clk_enable_unlock(flags);
979}
980
Mike Turquetteb24764902012-03-15 23:11:19 -0700981/**
982 * clk_disable - gate a clock
983 * @clk: the clk being gated
984 *
985 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
986 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
987 * clk if the operation is fast and will never sleep. One example is a
988 * SoC-internal clk which is controlled via simple register writes. In the
989 * complex case a clk gate operation may require a fast and a slow part. It is
990 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
991 * In fact clk_disable must be called before clk_unprepare.
992 */
993void clk_disable(struct clk *clk)
994{
Stephen Boyd63589e92014-03-26 16:06:37 -0700995 if (IS_ERR_OR_NULL(clk))
996 return;
997
Dong Aishenga6adc302016-06-30 17:31:11 +0800998 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700999}
1000EXPORT_SYMBOL_GPL(clk_disable);
1001
Stephen Boydd6968fc2015-04-30 13:54:13 -07001002static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001003{
1004 int ret = 0;
1005
Stephen Boyda6334722015-05-06 17:00:54 -07001006 lockdep_assert_held(&enable_lock);
1007
Stephen Boydd6968fc2015-04-30 13:54:13 -07001008 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001009 return 0;
1010
Fabio Estevamab525dc2018-01-16 10:50:34 -02001011 if (WARN(core->prepare_count == 0,
1012 "Enabling unprepared %s\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -07001013 return -ESHUTDOWN;
1014
Stephen Boydd6968fc2015-04-30 13:54:13 -07001015 if (core->enable_count == 0) {
1016 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -07001017
1018 if (ret)
1019 return ret;
1020
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -07001021 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001022
Stephen Boydd6968fc2015-04-30 13:54:13 -07001023 if (core->ops->enable)
1024 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001025
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -07001026 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001027
1028 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001029 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001030 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001031 }
1032 }
1033
Stephen Boydd6968fc2015-04-30 13:54:13 -07001034 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07001035 return 0;
1036}
1037
Dong Aishenga6adc302016-06-30 17:31:11 +08001038static int clk_core_enable_lock(struct clk_core *core)
1039{
1040 unsigned long flags;
1041 int ret;
1042
1043 flags = clk_enable_lock();
1044 ret = clk_core_enable(core);
1045 clk_enable_unlock(flags);
1046
1047 return ret;
1048}
1049
Keerthy43536542018-09-04 12:19:36 +05301050/**
1051 * clk_gate_restore_context - restore context for poweroff
1052 * @hw: the clk_hw pointer of clock whose state is to be restored
1053 *
1054 * The clock gate restore context function enables or disables
1055 * the gate clocks based on the enable_count. This is done in cases
1056 * where the clock context is lost and based on the enable_count
1057 * the clock either needs to be enabled/disabled. This
1058 * helps restore the state of gate clocks.
1059 */
1060void clk_gate_restore_context(struct clk_hw *hw)
1061{
Stephen Boyd9be76622018-10-11 09:28:13 -07001062 struct clk_core *core = hw->core;
1063
1064 if (core->enable_count)
1065 core->ops->enable(hw);
Keerthy43536542018-09-04 12:19:36 +05301066 else
Stephen Boyd9be76622018-10-11 09:28:13 -07001067 core->ops->disable(hw);
Keerthy43536542018-09-04 12:19:36 +05301068}
1069EXPORT_SYMBOL_GPL(clk_gate_restore_context);
1070
Stephen Boyd9be76622018-10-11 09:28:13 -07001071static int clk_core_save_context(struct clk_core *core)
Russ Dill8b95d1c2018-09-04 12:19:35 +05301072{
1073 struct clk_core *child;
1074 int ret = 0;
1075
Stephen Boyd9be76622018-10-11 09:28:13 -07001076 hlist_for_each_entry(child, &core->children, child_node) {
1077 ret = clk_core_save_context(child);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301078 if (ret < 0)
1079 return ret;
1080 }
1081
Stephen Boyd9be76622018-10-11 09:28:13 -07001082 if (core->ops && core->ops->save_context)
1083 ret = core->ops->save_context(core->hw);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301084
1085 return ret;
1086}
1087
Stephen Boyd9be76622018-10-11 09:28:13 -07001088static void clk_core_restore_context(struct clk_core *core)
Russ Dill8b95d1c2018-09-04 12:19:35 +05301089{
1090 struct clk_core *child;
1091
Stephen Boyd9be76622018-10-11 09:28:13 -07001092 if (core->ops && core->ops->restore_context)
1093 core->ops->restore_context(core->hw);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301094
Stephen Boyd9be76622018-10-11 09:28:13 -07001095 hlist_for_each_entry(child, &core->children, child_node)
1096 clk_core_restore_context(child);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301097}
1098
1099/**
1100 * clk_save_context - save clock context for poweroff
1101 *
1102 * Saves the context of the clock register for powerstates in which the
1103 * contents of the registers will be lost. Occurs deep within the suspend
1104 * code. Returns 0 on success.
1105 */
1106int clk_save_context(void)
1107{
1108 struct clk_core *clk;
1109 int ret;
1110
1111 hlist_for_each_entry(clk, &clk_root_list, child_node) {
Stephen Boyd9be76622018-10-11 09:28:13 -07001112 ret = clk_core_save_context(clk);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301113 if (ret < 0)
1114 return ret;
1115 }
1116
1117 hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
Stephen Boyd9be76622018-10-11 09:28:13 -07001118 ret = clk_core_save_context(clk);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301119 if (ret < 0)
1120 return ret;
1121 }
1122
1123 return 0;
1124}
1125EXPORT_SYMBOL_GPL(clk_save_context);
1126
1127/**
1128 * clk_restore_context - restore clock context after poweroff
1129 *
1130 * Restore the saved clock context upon resume.
1131 *
1132 */
1133void clk_restore_context(void)
1134{
Stephen Boyd9be76622018-10-11 09:28:13 -07001135 struct clk_core *core;
Russ Dill8b95d1c2018-09-04 12:19:35 +05301136
Stephen Boyd9be76622018-10-11 09:28:13 -07001137 hlist_for_each_entry(core, &clk_root_list, child_node)
1138 clk_core_restore_context(core);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301139
Stephen Boyd9be76622018-10-11 09:28:13 -07001140 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1141 clk_core_restore_context(core);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301142}
1143EXPORT_SYMBOL_GPL(clk_restore_context);
1144
Mike Turquetteb24764902012-03-15 23:11:19 -07001145/**
1146 * clk_enable - ungate a clock
1147 * @clk: the clk being ungated
1148 *
1149 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1150 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1151 * if the operation will never sleep. One example is a SoC-internal clk which
1152 * is controlled via simple register writes. In the complex case a clk ungate
1153 * operation may require a fast and a slow part. It is this reason that
1154 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1155 * must be called before clk_enable. Returns 0 on success, -EERROR
1156 * otherwise.
1157 */
1158int clk_enable(struct clk *clk)
1159{
Dong Aisheng864e1602015-04-30 14:02:19 -07001160 if (!clk)
1161 return 0;
1162
Dong Aishenga6adc302016-06-30 17:31:11 +08001163 return clk_core_enable_lock(clk->core);
1164}
1165EXPORT_SYMBOL_GPL(clk_enable);
1166
1167static int clk_core_prepare_enable(struct clk_core *core)
1168{
1169 int ret;
1170
1171 ret = clk_core_prepare_lock(core);
1172 if (ret)
1173 return ret;
1174
1175 ret = clk_core_enable_lock(core);
1176 if (ret)
1177 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001178
1179 return ret;
1180}
Dong Aishenga6adc302016-06-30 17:31:11 +08001181
1182static void clk_core_disable_unprepare(struct clk_core *core)
1183{
1184 clk_core_disable_lock(core);
1185 clk_core_unprepare_lock(core);
1186}
Mike Turquetteb24764902012-03-15 23:11:19 -07001187
Rasmus Villemoes564f86d2019-10-04 11:48:25 +02001188static void __init clk_unprepare_unused_subtree(struct clk_core *core)
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001189{
1190 struct clk_core *child;
1191
1192 lockdep_assert_held(&prepare_lock);
1193
1194 hlist_for_each_entry(child, &core->children, child_node)
1195 clk_unprepare_unused_subtree(child);
1196
1197 if (core->prepare_count)
1198 return;
1199
1200 if (core->flags & CLK_IGNORE_UNUSED)
1201 return;
1202
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001203 if (clk_pm_runtime_get(core))
1204 return;
1205
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001206 if (clk_core_is_prepared(core)) {
1207 trace_clk_unprepare(core);
1208 if (core->ops->unprepare_unused)
1209 core->ops->unprepare_unused(core->hw);
1210 else if (core->ops->unprepare)
1211 core->ops->unprepare(core->hw);
1212 trace_clk_unprepare_complete(core);
1213 }
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001214
1215 clk_pm_runtime_put(core);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001216}
1217
Rasmus Villemoes564f86d2019-10-04 11:48:25 +02001218static void __init clk_disable_unused_subtree(struct clk_core *core)
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001219{
1220 struct clk_core *child;
1221 unsigned long flags;
1222
1223 lockdep_assert_held(&prepare_lock);
1224
1225 hlist_for_each_entry(child, &core->children, child_node)
1226 clk_disable_unused_subtree(child);
1227
Dong Aishenga4b35182016-06-30 17:31:13 +08001228 if (core->flags & CLK_OPS_PARENT_ENABLE)
1229 clk_core_prepare_enable(core->parent);
1230
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001231 if (clk_pm_runtime_get(core))
1232 goto unprepare_out;
1233
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001234 flags = clk_enable_lock();
1235
1236 if (core->enable_count)
1237 goto unlock_out;
1238
1239 if (core->flags & CLK_IGNORE_UNUSED)
1240 goto unlock_out;
1241
1242 /*
1243 * some gate clocks have special needs during the disable-unused
1244 * sequence. call .disable_unused if available, otherwise fall
1245 * back to .disable
1246 */
1247 if (clk_core_is_enabled(core)) {
1248 trace_clk_disable(core);
1249 if (core->ops->disable_unused)
1250 core->ops->disable_unused(core->hw);
1251 else if (core->ops->disable)
1252 core->ops->disable(core->hw);
1253 trace_clk_disable_complete(core);
1254 }
1255
1256unlock_out:
1257 clk_enable_unlock(flags);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001258 clk_pm_runtime_put(core);
1259unprepare_out:
Dong Aishenga4b35182016-06-30 17:31:13 +08001260 if (core->flags & CLK_OPS_PARENT_ENABLE)
1261 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001262}
1263
Rasmus Villemoes564f86d2019-10-04 11:48:25 +02001264static bool clk_ignore_unused __initdata;
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001265static int __init clk_ignore_unused_setup(char *__unused)
1266{
1267 clk_ignore_unused = true;
1268 return 1;
1269}
1270__setup("clk_ignore_unused", clk_ignore_unused_setup);
1271
Rasmus Villemoes564f86d2019-10-04 11:48:25 +02001272static int __init clk_disable_unused(void)
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001273{
1274 struct clk_core *core;
1275
1276 if (clk_ignore_unused) {
1277 pr_warn("clk: Not disabling unused clocks\n");
1278 return 0;
1279 }
1280
1281 clk_prepare_lock();
1282
1283 hlist_for_each_entry(core, &clk_root_list, child_node)
1284 clk_disable_unused_subtree(core);
1285
1286 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1287 clk_disable_unused_subtree(core);
1288
1289 hlist_for_each_entry(core, &clk_root_list, child_node)
1290 clk_unprepare_unused_subtree(core);
1291
1292 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1293 clk_unprepare_unused_subtree(core);
1294
1295 clk_prepare_unlock();
1296
1297 return 0;
1298}
1299late_initcall_sync(clk_disable_unused);
1300
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001301static int clk_core_determine_round_nolock(struct clk_core *core,
1302 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001303{
Boris Brezillon0817b622015-07-07 20:48:08 +02001304 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001305
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001306 lockdep_assert_held(&prepare_lock);
1307
Stephen Boydd6968fc2015-04-30 13:54:13 -07001308 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001309 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001310
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001311 /*
1312 * At this point, core protection will be disabled if
1313 * - if the provider is not protected at all
1314 * - if the calling consumer is the only one which has exclusivity
1315 * over the provider
1316 */
Jerome Brunete55a8392017-12-01 22:51:56 +01001317 if (clk_core_rate_is_protected(core)) {
1318 req->rate = core->rate;
1319 } else if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001320 return core->ops->determine_rate(core->hw, req);
1321 } else if (core->ops->round_rate) {
1322 rate = core->ops->round_rate(core->hw, req->rate,
1323 &req->best_parent_rate);
1324 if (rate < 0)
1325 return rate;
1326
1327 req->rate = rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001328 } else {
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001329 return -EINVAL;
Boris Brezillon0817b622015-07-07 20:48:08 +02001330 }
1331
1332 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001333}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001334
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001335static void clk_core_init_rate_req(struct clk_core * const core,
1336 struct clk_rate_request *req)
1337{
1338 struct clk_core *parent;
1339
1340 if (WARN_ON(!core || !req))
1341 return;
1342
Mike Turquetteb24764902012-03-15 23:11:19 -07001343 parent = core->parent;
1344 if (parent) {
1345 req->best_parent_hw = parent->hw;
1346 req->best_parent_rate = parent->rate;
1347 } else {
1348 req->best_parent_hw = NULL;
1349 req->best_parent_rate = 0;
1350 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001351}
Mike Turquetteb24764902012-03-15 23:11:19 -07001352
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001353static bool clk_core_can_round(struct clk_core * const core)
1354{
Geert Uytterhoeveneef1f1b2019-06-17 14:02:48 +02001355 return core->ops->determine_rate || core->ops->round_rate;
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001356}
Mike Turquetteb24764902012-03-15 23:11:19 -07001357
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001358static int clk_core_round_rate_nolock(struct clk_core *core,
1359 struct clk_rate_request *req)
1360{
1361 lockdep_assert_held(&prepare_lock);
1362
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001363 if (!core) {
1364 req->rate = 0;
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001365 return 0;
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001366 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001367
1368 clk_core_init_rate_req(core, req);
1369
1370 if (clk_core_can_round(core))
1371 return clk_core_determine_round_nolock(core, req);
1372 else if (core->flags & CLK_SET_RATE_PARENT)
1373 return clk_core_round_rate_nolock(core->parent, req);
1374
1375 req->rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001376 return 0;
1377}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001378
1379/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001380 * __clk_determine_rate - get the closest rate actually supported by a clock
1381 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001382 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001383 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001384 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001385 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001386int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001387{
Boris Brezillon0817b622015-07-07 20:48:08 +02001388 if (!hw) {
1389 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001390 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001391 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001392
Boris Brezillon0817b622015-07-07 20:48:08 +02001393 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001394}
1395EXPORT_SYMBOL_GPL(__clk_determine_rate);
1396
Sarang Mairale8c849c2020-06-10 21:19:41 -05001397/**
1398 * clk_hw_round_rate() - round the given rate for a hw clk
1399 * @hw: the hw clk for which we are rounding a rate
1400 * @rate: the rate which is to be rounded
1401 *
1402 * Takes in a rate as input and rounds it to a rate that the clk can actually
1403 * use.
1404 *
1405 * Context: prepare_lock must be held.
1406 * For clk providers to call from within clk_ops such as .round_rate,
1407 * .determine_rate.
1408 *
1409 * Return: returns rounded rate of hw clk if clk supports round_rate operation
1410 * else returns the parent rate.
1411 */
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001412unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1413{
1414 int ret;
1415 struct clk_rate_request req;
1416
1417 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1418 req.rate = rate;
1419
1420 ret = clk_core_round_rate_nolock(hw->core, &req);
1421 if (ret)
1422 return 0;
1423
1424 return req.rate;
1425}
1426EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1427
Mike Turquetteb24764902012-03-15 23:11:19 -07001428/**
1429 * clk_round_rate - round the given rate for a clk
1430 * @clk: the clk for which we are rounding a rate
1431 * @rate: the rate which is to be rounded
1432 *
1433 * Takes in a rate as input and rounds it to a rate that the clk can actually
1434 * use which is then returned. If clk doesn't support round_rate operation
1435 * then the parent rate is returned.
1436 */
1437long clk_round_rate(struct clk *clk, unsigned long rate)
1438{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001439 struct clk_rate_request req;
1440 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001441
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001442 if (!clk)
1443 return 0;
1444
Mike Turquetteeab89f62013-03-28 13:59:01 -07001445 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001446
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001447 if (clk->exclusive_count)
1448 clk_core_rate_unprotect(clk->core);
1449
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001450 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1451 req.rate = rate;
1452
1453 ret = clk_core_round_rate_nolock(clk->core, &req);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001454
1455 if (clk->exclusive_count)
1456 clk_core_rate_protect(clk->core);
1457
Mike Turquetteeab89f62013-03-28 13:59:01 -07001458 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001459
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001460 if (ret)
1461 return ret;
1462
1463 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001464}
1465EXPORT_SYMBOL_GPL(clk_round_rate);
1466
1467/**
1468 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001469 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001470 * @msg: clk notifier type (see include/linux/clk.h)
1471 * @old_rate: old clk rate
1472 * @new_rate: new clk rate
1473 *
1474 * Triggers a notifier call chain on the clk rate-change notification
1475 * for 'clk'. Passes a pointer to the struct clk and the previous
1476 * and current rates to the notifier callback. Intended to be called by
1477 * internal clock code only. Returns NOTIFY_DONE from the last driver
1478 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1479 * a driver returns that.
1480 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001481static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001482 unsigned long old_rate, unsigned long new_rate)
1483{
1484 struct clk_notifier *cn;
1485 struct clk_notifier_data cnd;
1486 int ret = NOTIFY_DONE;
1487
Mike Turquetteb24764902012-03-15 23:11:19 -07001488 cnd.old_rate = old_rate;
1489 cnd.new_rate = new_rate;
1490
1491 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001492 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001493 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001494 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1495 &cnd);
Peter De Schrijver17c34c52017-03-21 12:16:26 +02001496 if (ret & NOTIFY_STOP_MASK)
1497 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001498 }
1499 }
1500
1501 return ret;
1502}
1503
1504/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001505 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001506 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001507 *
1508 * Walks the subtree of clks starting with clk and recalculates accuracies as
1509 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001510 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001511 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001512 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001513static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001514{
1515 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001516 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001517
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001518 lockdep_assert_held(&prepare_lock);
1519
Stephen Boydd6968fc2015-04-30 13:54:13 -07001520 if (core->parent)
1521 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001522
Stephen Boydd6968fc2015-04-30 13:54:13 -07001523 if (core->ops->recalc_accuracy)
1524 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001525 parent_accuracy);
1526 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001527 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001528
Stephen Boydd6968fc2015-04-30 13:54:13 -07001529 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001530 __clk_recalc_accuracies(child);
1531}
1532
Stephen Boyd0daa3762020-02-05 15:28:01 -08001533static long clk_core_get_accuracy_recalc(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001534{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001535 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1536 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001537
Stephen Boyd0daa3762020-02-05 15:28:01 -08001538 return clk_core_get_accuracy_no_lock(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001539}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001540
1541/**
1542 * clk_get_accuracy - return the accuracy of clk
1543 * @clk: the clk whose accuracy is being returned
1544 *
1545 * Simply returns the cached accuracy of the clk, unless
1546 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1547 * issued.
1548 * If clk is NULL then returns 0.
1549 */
1550long clk_get_accuracy(struct clk *clk)
1551{
Stephen Boyd0daa3762020-02-05 15:28:01 -08001552 long accuracy;
1553
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001554 if (!clk)
1555 return 0;
1556
Stephen Boyd0daa3762020-02-05 15:28:01 -08001557 clk_prepare_lock();
1558 accuracy = clk_core_get_accuracy_recalc(clk->core);
1559 clk_prepare_unlock();
1560
1561 return accuracy;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001562}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001563EXPORT_SYMBOL_GPL(clk_get_accuracy);
1564
Stephen Boydd6968fc2015-04-30 13:54:13 -07001565static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001566 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001567{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001568 unsigned long rate = parent_rate;
1569
1570 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1571 rate = core->ops->recalc_rate(core->hw, parent_rate);
1572 clk_pm_runtime_put(core);
1573 }
1574 return rate;
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001575}
1576
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001577/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001578 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001579 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001580 * @msg: notification type (see include/linux/clk.h)
1581 *
1582 * Walks the subtree of clks starting with clk and recalculates rates as it
1583 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001584 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001585 *
1586 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1587 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001588 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001589static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001590{
1591 unsigned long old_rate;
1592 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001593 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001594
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001595 lockdep_assert_held(&prepare_lock);
1596
Stephen Boydd6968fc2015-04-30 13:54:13 -07001597 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001598
Stephen Boydd6968fc2015-04-30 13:54:13 -07001599 if (core->parent)
1600 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001601
Stephen Boydd6968fc2015-04-30 13:54:13 -07001602 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001603
1604 /*
1605 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1606 * & ABORT_RATE_CHANGE notifiers
1607 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001608 if (core->notifier_count && msg)
1609 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001610
Stephen Boydd6968fc2015-04-30 13:54:13 -07001611 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001612 __clk_recalc_rates(child, msg);
1613}
1614
Stephen Boyd0daa3762020-02-05 15:28:01 -08001615static unsigned long clk_core_get_rate_recalc(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001616{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001617 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1618 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001619
Stephen Boyd0daa3762020-02-05 15:28:01 -08001620 return clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001621}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001622
Mike Turquetteb24764902012-03-15 23:11:19 -07001623/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001624 * clk_get_rate - return the rate of clk
1625 * @clk: the clk whose rate is being returned
1626 *
1627 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1628 * is set, which means a recalc_rate will be issued.
1629 * If clk is NULL then returns 0.
1630 */
1631unsigned long clk_get_rate(struct clk *clk)
1632{
Stephen Boyd0daa3762020-02-05 15:28:01 -08001633 unsigned long rate;
1634
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001635 if (!clk)
1636 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001637
Stephen Boyd0daa3762020-02-05 15:28:01 -08001638 clk_prepare_lock();
1639 rate = clk_core_get_rate_recalc(clk->core);
1640 clk_prepare_unlock();
1641
1642 return rate;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001643}
1644EXPORT_SYMBOL_GPL(clk_get_rate);
1645
Stephen Boydd6968fc2015-04-30 13:54:13 -07001646static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001647 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001648{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001649 int i;
James Hogan4935b222013-07-29 12:24:59 +01001650
Masahiro Yamada508f8842015-12-28 19:23:08 +09001651 if (!parent)
1652 return -EINVAL;
1653
Derek Basehoreede77852018-12-20 16:31:00 -08001654 for (i = 0; i < core->num_parents; i++) {
Stephen Boyd1a079562019-04-30 10:22:30 -07001655 /* Found it first try! */
Stephen Boydfc0c2092019-04-12 11:31:47 -07001656 if (core->parents[i].core == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001657 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001658
Stephen Boyd1a079562019-04-30 10:22:30 -07001659 /* Something else is here, so keep looking */
Stephen Boydfc0c2092019-04-12 11:31:47 -07001660 if (core->parents[i].core)
Derek Basehoreede77852018-12-20 16:31:00 -08001661 continue;
1662
Stephen Boyd1a079562019-04-30 10:22:30 -07001663 /* Maybe core hasn't been cached but the hw is all we know? */
1664 if (core->parents[i].hw) {
1665 if (core->parents[i].hw == parent->hw)
1666 break;
1667
1668 /* Didn't match, but we're expecting a clk_hw */
1669 continue;
Derek Basehoreede77852018-12-20 16:31:00 -08001670 }
Stephen Boyd1a079562019-04-30 10:22:30 -07001671
1672 /* Maybe it hasn't been cached (clk_set_parent() path) */
1673 if (parent == clk_core_get(core, i))
1674 break;
1675
1676 /* Fallback to comparing globally unique names */
Martin Blumenstingl24876f02019-08-16 00:31:55 +02001677 if (core->parents[i].name &&
1678 !strcmp(parent->name, core->parents[i].name))
Stephen Boyd1a079562019-04-30 10:22:30 -07001679 break;
Derek Basehoreede77852018-12-20 16:31:00 -08001680 }
1681
Stephen Boyd1a079562019-04-30 10:22:30 -07001682 if (i == core->num_parents)
1683 return -EINVAL;
1684
1685 core->parents[i].core = parent;
1686 return i;
James Hogan4935b222013-07-29 12:24:59 +01001687}
1688
Sowjanya Komatinenid9b86cc2019-08-16 12:41:52 -07001689/**
1690 * clk_hw_get_parent_index - return the index of the parent clock
1691 * @hw: clk_hw associated with the clk being consumed
1692 *
1693 * Fetches and returns the index of parent clock. Returns -EINVAL if the given
1694 * clock does not have a current parent.
1695 */
1696int clk_hw_get_parent_index(struct clk_hw *hw)
1697{
1698 struct clk_hw *parent = clk_hw_get_parent(hw);
1699
1700 if (WARN_ON(parent == NULL))
1701 return -EINVAL;
1702
1703 return clk_fetch_parent_index(hw->core, parent->core);
1704}
1705EXPORT_SYMBOL_GPL(clk_hw_get_parent_index);
1706
Heiko Stuebnere6500342015-04-22 22:53:05 +02001707/*
1708 * Update the orphan status of @core and all its children.
1709 */
1710static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1711{
1712 struct clk_core *child;
1713
1714 core->orphan = is_orphan;
1715
1716 hlist_for_each_entry(child, &core->children, child_node)
1717 clk_core_update_orphan_status(child, is_orphan);
1718}
1719
Stephen Boydd6968fc2015-04-30 13:54:13 -07001720static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001721{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001722 bool was_orphan = core->orphan;
1723
Stephen Boydd6968fc2015-04-30 13:54:13 -07001724 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001725
James Hogan903efc52013-08-29 12:10:51 +01001726 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001727 bool becomes_orphan = new_parent->orphan;
1728
James Hogan903efc52013-08-29 12:10:51 +01001729 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001730 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001731 new_parent->new_child = NULL;
1732
Stephen Boydd6968fc2015-04-30 13:54:13 -07001733 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001734
1735 if (was_orphan != becomes_orphan)
1736 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001737 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001738 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001739 if (!was_orphan)
1740 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001741 }
James Hogan4935b222013-07-29 12:24:59 +01001742
Stephen Boydd6968fc2015-04-30 13:54:13 -07001743 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001744}
1745
Stephen Boydd6968fc2015-04-30 13:54:13 -07001746static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001747 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001748{
1749 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001750 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001751
1752 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001753 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1754 *
1755 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001756 * clk_enable().
1757 *
1758 * If the clock is not prepared, then a race with
1759 * clk_enable/disable() is impossible since we already have the
1760 * prepare lock (future calls to clk_enable() need to be preceded by
1761 * a clk_prepare()).
1762 *
1763 * If the clock is prepared, migrate the prepared state to the new
1764 * parent and also protect against a race with clk_enable() by
1765 * forcing the clock and the new parent on. This ensures that all
1766 * future calls to clk_enable() are practically NOPs with respect to
1767 * hardware and software states.
1768 *
1769 * See also: Comment for clk_set_parent() below.
1770 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001771
1772 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1773 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1774 clk_core_prepare_enable(old_parent);
1775 clk_core_prepare_enable(parent);
1776 }
1777
1778 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001779 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001780 clk_core_prepare_enable(parent);
1781 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001782 }
1783
1784 /* update the clk tree topology */
1785 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001786 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001787 clk_enable_unlock(flags);
1788
Stephen Boyd3fa22522014-01-15 10:47:22 -08001789 return old_parent;
1790}
1791
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001792static void __clk_set_parent_after(struct clk_core *core,
1793 struct clk_core *parent,
1794 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001795{
1796 /*
1797 * Finish the migration of prepare state and undo the changes done
1798 * for preventing a race with clk_enable().
1799 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001800 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001801 clk_core_disable_lock(core);
1802 clk_core_disable_unprepare(old_parent);
1803 }
1804
1805 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1806 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1807 clk_core_disable_unprepare(parent);
1808 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001809 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001810}
1811
Stephen Boydd6968fc2015-04-30 13:54:13 -07001812static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001813 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001814{
1815 unsigned long flags;
1816 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001817 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001818
Stephen Boydd6968fc2015-04-30 13:54:13 -07001819 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001820
Stephen Boydd6968fc2015-04-30 13:54:13 -07001821 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001822
James Hogan4935b222013-07-29 12:24:59 +01001823 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001824 if (parent && core->ops->set_parent)
1825 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001826
Stephen Boydd6968fc2015-04-30 13:54:13 -07001827 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001828
James Hogan4935b222013-07-29 12:24:59 +01001829 if (ret) {
1830 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001831 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001832 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001833 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001834
James Hogan4935b222013-07-29 12:24:59 +01001835 return ret;
1836 }
1837
Stephen Boydd6968fc2015-04-30 13:54:13 -07001838 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001839
James Hogan4935b222013-07-29 12:24:59 +01001840 return 0;
1841}
1842
Ulf Hanssona093bde2012-08-31 14:21:28 +02001843/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001844 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001845 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001846 * @parent_rate: the "future" rate of clk's parent
1847 *
1848 * Walks the subtree of clks starting with clk, speculating rates as it
1849 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1850 *
1851 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1852 * pre-rate change notifications and returns early if no clks in the
1853 * subtree have subscribed to the notifications. Note that if a clk does not
1854 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001855 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001856 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001857static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001858 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001859{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001860 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001861 unsigned long new_rate;
1862 int ret = NOTIFY_DONE;
1863
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001864 lockdep_assert_held(&prepare_lock);
1865
Stephen Boydd6968fc2015-04-30 13:54:13 -07001866 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001867
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001868 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001869 if (core->notifier_count)
1870 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001871
Mike Turquette86bcfa22014-02-24 16:08:41 -08001872 if (ret & NOTIFY_STOP_MASK) {
1873 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001874 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001875 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001876 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001877
Stephen Boydd6968fc2015-04-30 13:54:13 -07001878 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001879 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001880 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001881 break;
1882 }
1883
1884out:
1885 return ret;
1886}
1887
Stephen Boydd6968fc2015-04-30 13:54:13 -07001888static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001889 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001890{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001891 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001892
Stephen Boydd6968fc2015-04-30 13:54:13 -07001893 core->new_rate = new_rate;
1894 core->new_parent = new_parent;
1895 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001896 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001897 core->new_child = NULL;
1898 if (new_parent && new_parent != core->parent)
1899 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001900
Stephen Boydd6968fc2015-04-30 13:54:13 -07001901 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001902 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001903 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001904 }
1905}
1906
1907/*
1908 * calculate the new rates returning the topmost clock that has to be
1909 * changed.
1910 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001911static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001912 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001913{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001914 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001915 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001916 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001917 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001918 unsigned long min_rate;
1919 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001920 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001921 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001922
Mike Turquette7452b212012-03-26 14:45:36 -07001923 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001924 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001925 return NULL;
1926
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001927 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001928 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001929 if (parent)
1930 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001931
Stephen Boydd6968fc2015-04-30 13:54:13 -07001932 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001933
James Hogan71472c02013-07-29 12:25:00 +01001934 /* find the closest rate and parent clk/rate */
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001935 if (clk_core_can_round(core)) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001936 struct clk_rate_request req;
1937
1938 req.rate = rate;
1939 req.min_rate = min_rate;
1940 req.max_rate = max_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001941
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001942 clk_core_init_rate_req(core, &req);
1943
1944 ret = clk_core_determine_round_nolock(core, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001945 if (ret < 0)
1946 return NULL;
1947
Boris Brezillon0817b622015-07-07 20:48:08 +02001948 best_parent_rate = req.best_parent_rate;
1949 new_rate = req.rate;
1950 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001951
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001952 if (new_rate < min_rate || new_rate > max_rate)
1953 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001954 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001955 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001956 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001957 return NULL;
1958 } else {
1959 /* pass-through clock with adjustable parent */
1960 top = clk_calc_new_rates(parent, rate);
1961 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001962 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001963 }
1964
James Hogan71472c02013-07-29 12:25:00 +01001965 /* some clocks must be gated to change parent */
1966 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001967 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001968 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001969 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001970 return NULL;
1971 }
1972
James Hogan71472c02013-07-29 12:25:00 +01001973 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001974 if (parent && core->num_parents > 1) {
1975 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001976 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001977 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001978 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001979 return NULL;
1980 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001981 }
1982
Stephen Boydd6968fc2015-04-30 13:54:13 -07001983 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001984 best_parent_rate != parent->rate)
1985 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001986
1987out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001988 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001989
1990 return top;
1991}
1992
1993/*
1994 * Notify about rate changes in a subtree. Always walk down the whole tree
1995 * so that in case of an error we can walk down the whole tree again and
1996 * abort the change.
1997 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001998static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001999 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07002000{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002001 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002002 int ret = NOTIFY_DONE;
2003
Stephen Boydd6968fc2015-04-30 13:54:13 -07002004 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05302005 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002006
Stephen Boydd6968fc2015-04-30 13:54:13 -07002007 if (core->notifier_count) {
2008 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002009 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002010 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002011 }
2012
Stephen Boydd6968fc2015-04-30 13:54:13 -07002013 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01002014 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002015 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01002016 continue;
2017 tmp_clk = clk_propagate_rate_change(child, event);
2018 if (tmp_clk)
2019 fail_clk = tmp_clk;
2020 }
2021
Stephen Boydd6968fc2015-04-30 13:54:13 -07002022 /* handle the new child who might not be in core->children yet */
2023 if (core->new_child) {
2024 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01002025 if (tmp_clk)
2026 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07002027 }
2028
2029 return fail_clk;
2030}
2031
2032/*
2033 * walk down a subtree and set the new rates notifying the rate
2034 * change on the way
2035 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002036static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002037{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002038 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03002039 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07002040 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01002041 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08002042 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002043 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08002044 struct clk_core *parent = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002045
Stephen Boydd6968fc2015-04-30 13:54:13 -07002046 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002047
Dong Aishengfc8726a2016-06-30 17:31:14 +08002048 if (core->new_parent) {
2049 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002050 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08002051 } else if (core->parent) {
2052 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002053 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08002054 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01002055
Marek Szyprowski588fb542017-11-30 13:14:51 +01002056 if (clk_pm_runtime_get(core))
2057 return;
2058
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01002059 if (core->flags & CLK_SET_RATE_UNGATE) {
2060 unsigned long flags;
2061
2062 clk_core_prepare(core);
2063 flags = clk_enable_lock();
2064 clk_core_enable(core);
2065 clk_enable_unlock(flags);
2066 }
2067
Stephen Boydd6968fc2015-04-30 13:54:13 -07002068 if (core->new_parent && core->new_parent != core->parent) {
2069 old_parent = __clk_set_parent_before(core, core->new_parent);
2070 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002071
Stephen Boydd6968fc2015-04-30 13:54:13 -07002072 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002073 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002074 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08002075 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07002076 core->new_parent_index);
2077 } else if (core->ops->set_parent) {
2078 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002079 }
2080
Stephen Boydd6968fc2015-04-30 13:54:13 -07002081 trace_clk_set_parent_complete(core, core->new_parent);
2082 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002083 }
2084
Dong Aishengfc8726a2016-06-30 17:31:14 +08002085 if (core->flags & CLK_OPS_PARENT_ENABLE)
2086 clk_core_prepare_enable(parent);
2087
Stephen Boydd6968fc2015-04-30 13:54:13 -07002088 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002089
Stephen Boydd6968fc2015-04-30 13:54:13 -07002090 if (!skip_set_rate && core->ops->set_rate)
2091 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002092
Stephen Boydd6968fc2015-04-30 13:54:13 -07002093 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002094
Stephen Boydd6968fc2015-04-30 13:54:13 -07002095 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002096
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01002097 if (core->flags & CLK_SET_RATE_UNGATE) {
2098 unsigned long flags;
2099
2100 flags = clk_enable_lock();
2101 clk_core_disable(core);
2102 clk_enable_unlock(flags);
2103 clk_core_unprepare(core);
2104 }
2105
Dong Aishengfc8726a2016-06-30 17:31:14 +08002106 if (core->flags & CLK_OPS_PARENT_ENABLE)
2107 clk_core_disable_unprepare(parent);
2108
Stephen Boydd6968fc2015-04-30 13:54:13 -07002109 if (core->notifier_count && old_rate != core->rate)
2110 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002111
Michael Turquette85e88fa2015-06-20 12:18:03 -07002112 if (core->flags & CLK_RECALC_NEW_RATES)
2113 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02002114
Tero Kristo067bb172014-08-21 16:47:45 +03002115 /*
2116 * Use safe iteration, as change_rate can actually swap parents
2117 * for certain clock types.
2118 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002119 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01002120 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002121 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01002122 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07002123 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01002124 }
2125
Stephen Boydd6968fc2015-04-30 13:54:13 -07002126 /* handle the new child who might not be in core->children yet */
2127 if (core->new_child)
2128 clk_change_rate(core->new_child);
Marek Szyprowski588fb542017-11-30 13:14:51 +01002129
2130 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002131}
2132
Jerome Brunetca5e0892017-12-01 22:51:55 +01002133static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
2134 unsigned long req_rate)
2135{
Jerome Brunete55a8392017-12-01 22:51:56 +01002136 int ret, cnt;
Jerome Brunetca5e0892017-12-01 22:51:55 +01002137 struct clk_rate_request req;
2138
2139 lockdep_assert_held(&prepare_lock);
2140
2141 if (!core)
2142 return 0;
2143
Jerome Brunete55a8392017-12-01 22:51:56 +01002144 /* simulate what the rate would be if it could be freely set */
2145 cnt = clk_core_rate_nuke_protect(core);
2146 if (cnt < 0)
2147 return cnt;
2148
Jerome Brunetca5e0892017-12-01 22:51:55 +01002149 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
2150 req.rate = req_rate;
2151
2152 ret = clk_core_round_rate_nolock(core, &req);
2153
Jerome Brunete55a8392017-12-01 22:51:56 +01002154 /* restore the protection */
2155 clk_core_rate_restore_protect(core, cnt);
2156
Jerome Brunetca5e0892017-12-01 22:51:55 +01002157 return ret ? 0 : req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002158}
2159
Stephen Boydd6968fc2015-04-30 13:54:13 -07002160static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002161 unsigned long req_rate)
2162{
2163 struct clk_core *top, *fail_clk;
Jerome Brunetca5e0892017-12-01 22:51:55 +01002164 unsigned long rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002165 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002166
Stephen Boydd6968fc2015-04-30 13:54:13 -07002167 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002168 return 0;
2169
Jerome Brunetca5e0892017-12-01 22:51:55 +01002170 rate = clk_core_req_round_rate_nolock(core, req_rate);
2171
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002172 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002173 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002174 return 0;
2175
Jerome Brunete55a8392017-12-01 22:51:56 +01002176 /* fail on a direct rate set of a protected provider */
2177 if (clk_core_rate_is_protected(core))
2178 return -EBUSY;
2179
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002180 /* calculate new rates and get the topmost changed clock */
Jerome Brunetca5e0892017-12-01 22:51:55 +01002181 top = clk_calc_new_rates(core, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002182 if (!top)
2183 return -EINVAL;
2184
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002185 ret = clk_pm_runtime_get(core);
2186 if (ret)
2187 return ret;
2188
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002189 /* notify that we are about to change rates */
2190 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
2191 if (fail_clk) {
2192 pr_debug("%s: failed to set %s rate\n", __func__,
2193 fail_clk->name);
2194 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002195 ret = -EBUSY;
2196 goto err;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002197 }
2198
2199 /* change the rates */
2200 clk_change_rate(top);
2201
Stephen Boydd6968fc2015-04-30 13:54:13 -07002202 core->req_rate = req_rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002203err:
2204 clk_pm_runtime_put(core);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002205
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002206 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002207}
2208
Mike Turquetteb24764902012-03-15 23:11:19 -07002209/**
2210 * clk_set_rate - specify a new rate for clk
2211 * @clk: the clk whose rate is being changed
2212 * @rate: the new rate for clk
2213 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002214 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07002215 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002216 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2217 * propagate up to clk's parent; whether or not this happens depends on the
2218 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2219 * after calling .round_rate then upstream parent propagation is ignored. If
2220 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002221 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07002222 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2223 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07002224 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002225 * Rate changes are accomplished via tree traversal that also recalculates the
2226 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07002227 *
2228 * Returns 0 on success, -EERROR otherwise.
2229 */
2230int clk_set_rate(struct clk *clk, unsigned long rate)
2231{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002232 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002233
Mike Turquette89ac8d72013-08-21 23:58:09 -07002234 if (!clk)
2235 return 0;
2236
Mike Turquetteb24764902012-03-15 23:11:19 -07002237 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002238 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002239
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002240 if (clk->exclusive_count)
2241 clk_core_rate_unprotect(clk->core);
2242
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002243 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002244
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002245 if (clk->exclusive_count)
2246 clk_core_rate_protect(clk->core);
2247
Mike Turquetteeab89f62013-03-28 13:59:01 -07002248 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002249
2250 return ret;
2251}
2252EXPORT_SYMBOL_GPL(clk_set_rate);
2253
2254/**
Geert Uytterhoeven65e22182019-06-17 15:56:02 +02002255 * clk_set_rate_exclusive - specify a new rate and get exclusive control
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002256 * @clk: the clk whose rate is being changed
2257 * @rate: the new rate for clk
2258 *
2259 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2260 * within a critical section
2261 *
2262 * This can be used initially to ensure that at least 1 consumer is
Geert Uytterhoeven65e22182019-06-17 15:56:02 +02002263 * satisfied when several consumers are competing for exclusivity over the
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002264 * same clock provider.
2265 *
2266 * The exclusivity is not applied if setting the rate failed.
2267 *
2268 * Calls to clk_rate_exclusive_get() should be balanced with calls to
2269 * clk_rate_exclusive_put().
2270 *
2271 * Returns 0 on success, -EERROR otherwise.
2272 */
2273int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2274{
2275 int ret;
2276
2277 if (!clk)
2278 return 0;
2279
2280 /* prevent racing with updates to the clock topology */
2281 clk_prepare_lock();
2282
2283 /*
2284 * The temporary protection removal is not here, on purpose
2285 * This function is meant to be used instead of clk_rate_protect,
2286 * so before the consumer code path protect the clock provider
2287 */
2288
2289 ret = clk_core_set_rate_nolock(clk->core, rate);
2290 if (!ret) {
2291 clk_core_rate_protect(clk->core);
2292 clk->exclusive_count++;
2293 }
2294
2295 clk_prepare_unlock();
2296
2297 return ret;
2298}
2299EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2300
2301/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002302 * clk_set_rate_range - set a rate range for a clock source
2303 * @clk: clock source
2304 * @min: desired minimum clock rate in Hz, inclusive
2305 * @max: desired maximum clock rate in Hz, inclusive
2306 *
2307 * Returns success (0) or negative errno.
2308 */
2309int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2310{
2311 int ret = 0;
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002312 unsigned long old_min, old_max, rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002313
2314 if (!clk)
2315 return 0;
2316
2317 if (min > max) {
2318 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2319 __func__, clk->core->name, clk->dev_id, clk->con_id,
2320 min, max);
2321 return -EINVAL;
2322 }
2323
2324 clk_prepare_lock();
2325
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002326 if (clk->exclusive_count)
2327 clk_core_rate_unprotect(clk->core);
2328
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002329 /* Save the current values in case we need to rollback the change */
2330 old_min = clk->min_rate;
2331 old_max = clk->max_rate;
2332 clk->min_rate = min;
2333 clk->max_rate = max;
2334
2335 rate = clk_core_get_rate_nolock(clk->core);
2336 if (rate < min || rate > max) {
2337 /*
2338 * FIXME:
2339 * We are in bit of trouble here, current rate is outside the
2340 * the requested range. We are going try to request appropriate
2341 * range boundary but there is a catch. It may fail for the
2342 * usual reason (clock broken, clock protected, etc) but also
2343 * because:
2344 * - round_rate() was not favorable and fell on the wrong
2345 * side of the boundary
2346 * - the determine_rate() callback does not really check for
2347 * this corner case when determining the rate
2348 */
2349
2350 if (rate < min)
2351 rate = min;
2352 else
2353 rate = max;
2354
2355 ret = clk_core_set_rate_nolock(clk->core, rate);
2356 if (ret) {
2357 /* rollback the changes */
2358 clk->min_rate = old_min;
2359 clk->max_rate = old_max;
2360 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002361 }
2362
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002363 if (clk->exclusive_count)
2364 clk_core_rate_protect(clk->core);
2365
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002366 clk_prepare_unlock();
2367
2368 return ret;
2369}
2370EXPORT_SYMBOL_GPL(clk_set_rate_range);
2371
2372/**
2373 * clk_set_min_rate - set a minimum clock rate for a clock source
2374 * @clk: clock source
2375 * @rate: desired minimum clock rate in Hz, inclusive
2376 *
2377 * Returns success (0) or negative errno.
2378 */
2379int clk_set_min_rate(struct clk *clk, unsigned long rate)
2380{
2381 if (!clk)
2382 return 0;
2383
2384 return clk_set_rate_range(clk, rate, clk->max_rate);
2385}
2386EXPORT_SYMBOL_GPL(clk_set_min_rate);
2387
2388/**
2389 * clk_set_max_rate - set a maximum clock rate for a clock source
2390 * @clk: clock source
2391 * @rate: desired maximum clock rate in Hz, inclusive
2392 *
2393 * Returns success (0) or negative errno.
2394 */
2395int clk_set_max_rate(struct clk *clk, unsigned long rate)
2396{
2397 if (!clk)
2398 return 0;
2399
2400 return clk_set_rate_range(clk, clk->min_rate, rate);
2401}
2402EXPORT_SYMBOL_GPL(clk_set_max_rate);
2403
2404/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002405 * clk_get_parent - return the parent of a clk
2406 * @clk: the clk whose parent gets returned
2407 *
2408 * Simply returns clk->parent. Returns NULL if clk is NULL.
2409 */
2410struct clk *clk_get_parent(struct clk *clk)
2411{
2412 struct clk *parent;
2413
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002414 if (!clk)
2415 return NULL;
2416
Mike Turquetteeab89f62013-03-28 13:59:01 -07002417 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002418 /* TODO: Create a per-user clk and change callers to call clk_put */
2419 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002420 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002421
2422 return parent;
2423}
2424EXPORT_SYMBOL_GPL(clk_get_parent);
2425
Stephen Boydd6968fc2015-04-30 13:54:13 -07002426static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002427{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002428 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002429
Masahiro Yamada2430a942016-02-09 20:19:14 +09002430 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002431 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002432
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002433 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002434}
2435
Stephen Boydd6968fc2015-04-30 13:54:13 -07002436static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002437 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002438{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002439 clk_reparent(core, new_parent);
2440 __clk_recalc_accuracies(core);
2441 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002442}
2443
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002444void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2445{
2446 if (!hw)
2447 return;
2448
2449 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2450}
2451
Mike Turquetteb24764902012-03-15 23:11:19 -07002452/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002453 * clk_has_parent - check if a clock is a possible parent for another
2454 * @clk: clock source
2455 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002456 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002457 * This function can be used in drivers that need to check that a clock can be
2458 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002459 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002460 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002461 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002462bool clk_has_parent(struct clk *clk, struct clk *parent)
2463{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002464 struct clk_core *core, *parent_core;
Stephen Boydfc0c2092019-04-12 11:31:47 -07002465 int i;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002466
2467 /* NULL clocks should be nops, so return success if either is NULL. */
2468 if (!clk || !parent)
2469 return true;
2470
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002471 core = clk->core;
2472 parent_core = parent->core;
2473
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002474 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002475 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002476 return true;
2477
Stephen Boydfc0c2092019-04-12 11:31:47 -07002478 for (i = 0; i < core->num_parents; i++)
2479 if (!strcmp(core->parents[i].name, parent_core->name))
2480 return true;
2481
2482 return false;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002483}
2484EXPORT_SYMBOL_GPL(clk_has_parent);
2485
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002486static int clk_core_set_parent_nolock(struct clk_core *core,
2487 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002488{
2489 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002490 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002491 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002492
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002493 lockdep_assert_held(&prepare_lock);
2494
Stephen Boydd6968fc2015-04-30 13:54:13 -07002495 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002496 return 0;
2497
Stephen Boydd6968fc2015-04-30 13:54:13 -07002498 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002499 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002500
Rishi Guptaef13e552019-08-17 12:05:59 +05302501 /* verify ops for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002502 if (core->num_parents > 1 && !core->ops->set_parent)
2503 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08002504
Ulf Hansson031dcc92013-04-02 23:09:38 +02002505 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002506 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2507 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002508
Jerome Brunete55a8392017-12-01 22:51:56 +01002509 if (clk_core_rate_is_protected(core))
2510 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002511
2512 /* try finding the new parent index */
2513 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002514 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002515 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002516 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002517 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002518 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002519 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002520 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002521 }
2522
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002523 ret = clk_pm_runtime_get(core);
2524 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002525 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002526
Mike Turquetteb24764902012-03-15 23:11:19 -07002527 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002528 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002529
2530 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002531 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002532 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07002533
Ulf Hansson031dcc92013-04-02 23:09:38 +02002534 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002535 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002536
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002537 /* propagate rate an accuracy recalculation accordingly */
2538 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002539 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002540 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002541 __clk_recalc_rates(core, POST_RATE_CHANGE);
2542 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002543 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002544
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002545runtime_put:
2546 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002547
2548 return ret;
2549}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002550
Neil Armstrong35678942019-07-31 10:40:16 +02002551int clk_hw_set_parent(struct clk_hw *hw, struct clk_hw *parent)
2552{
2553 return clk_core_set_parent_nolock(hw->core, parent->core);
2554}
2555EXPORT_SYMBOL_GPL(clk_hw_set_parent);
2556
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002557/**
2558 * clk_set_parent - switch the parent of a mux clk
2559 * @clk: the mux clk whose input we are switching
2560 * @parent: the new input to clk
2561 *
2562 * Re-parent clk to use parent as its new input source. If clk is in
2563 * prepared state, the clk will get enabled for the duration of this call. If
2564 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2565 * that, the reparenting is glitchy in hardware, etc), use the
2566 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2567 *
2568 * After successfully changing clk's parent clk_set_parent will update the
2569 * clk topology, sysfs topology and propagate rate recalculation via
2570 * __clk_recalc_rates.
2571 *
2572 * Returns 0 on success, -EERROR otherwise.
2573 */
2574int clk_set_parent(struct clk *clk, struct clk *parent)
2575{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002576 int ret;
2577
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002578 if (!clk)
2579 return 0;
2580
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002581 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002582
2583 if (clk->exclusive_count)
2584 clk_core_rate_unprotect(clk->core);
2585
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002586 ret = clk_core_set_parent_nolock(clk->core,
2587 parent ? parent->core : NULL);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002588
2589 if (clk->exclusive_count)
2590 clk_core_rate_protect(clk->core);
2591
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002592 clk_prepare_unlock();
2593
2594 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002595}
Mike Turquetteb24764902012-03-15 23:11:19 -07002596EXPORT_SYMBOL_GPL(clk_set_parent);
2597
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002598static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2599{
2600 int ret = -EINVAL;
2601
2602 lockdep_assert_held(&prepare_lock);
2603
2604 if (!core)
2605 return 0;
2606
Jerome Brunete55a8392017-12-01 22:51:56 +01002607 if (clk_core_rate_is_protected(core))
2608 return -EBUSY;
2609
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002610 trace_clk_set_phase(core, degrees);
2611
Shawn Lin7f95bee2018-03-08 14:49:41 +08002612 if (core->ops->set_phase) {
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002613 ret = core->ops->set_phase(core->hw, degrees);
Shawn Lin7f95bee2018-03-08 14:49:41 +08002614 if (!ret)
2615 core->phase = degrees;
2616 }
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002617
2618 trace_clk_set_phase_complete(core, degrees);
2619
2620 return ret;
2621}
2622
Mike Turquetteb24764902012-03-15 23:11:19 -07002623/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002624 * clk_set_phase - adjust the phase shift of a clock signal
2625 * @clk: clock signal source
2626 * @degrees: number of degrees the signal is shifted
2627 *
2628 * Shifts the phase of a clock signal by the specified
2629 * degrees. Returns 0 on success, -EERROR otherwise.
2630 *
2631 * This function makes no distinction about the input or reference
2632 * signal that we adjust the clock signal phase against. For example
2633 * phase locked-loop clock signal generators we may shift phase with
2634 * respect to feedback clock signal input, but for other cases the
2635 * clock phase may be shifted with respect to some other, unspecified
2636 * signal.
2637 *
2638 * Additionally the concept of phase shift does not propagate through
2639 * the clock tree hierarchy, which sets it apart from clock rates and
2640 * clock accuracy. A parent clock phase attribute does not have an
2641 * impact on the phase attribute of a child clock.
2642 */
2643int clk_set_phase(struct clk *clk, int degrees)
2644{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002645 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002646
2647 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002648 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002649
2650 /* sanity check degrees */
2651 degrees %= 360;
2652 if (degrees < 0)
2653 degrees += 360;
2654
2655 clk_prepare_lock();
2656
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002657 if (clk->exclusive_count)
2658 clk_core_rate_unprotect(clk->core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002659
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002660 ret = clk_core_set_phase_nolock(clk->core, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002661
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002662 if (clk->exclusive_count)
2663 clk_core_rate_protect(clk->core);
Mike Turquettee59c5372014-02-18 21:21:25 -08002664
Mike Turquettee59c5372014-02-18 21:21:25 -08002665 clk_prepare_unlock();
2666
Mike Turquettee59c5372014-02-18 21:21:25 -08002667 return ret;
2668}
Maxime Ripard9767b04f2015-01-20 22:23:43 +01002669EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002670
Stephen Boydd6968fc2015-04-30 13:54:13 -07002671static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002672{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002673 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002674
Stephen Boydf21cf9c2020-02-05 15:27:59 -08002675 lockdep_assert_held(&prepare_lock);
2676 if (!core->ops->get_phase)
2677 return 0;
2678
Shawn Lin1f9c63e2018-03-14 08:28:31 +08002679 /* Always try to update cached phase if possible */
Stephen Boydf21cf9c2020-02-05 15:27:59 -08002680 ret = core->ops->get_phase(core->hw);
2681 if (ret >= 0)
2682 core->phase = ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002683
Mike Turquettee59c5372014-02-18 21:21:25 -08002684 return ret;
2685}
2686
2687/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002688 * clk_get_phase - return the phase shift of a clock signal
2689 * @clk: clock signal source
2690 *
2691 * Returns the phase shift of a clock node in degrees, otherwise returns
2692 * -EERROR.
2693 */
2694int clk_get_phase(struct clk *clk)
2695{
Stephen Boydf21cf9c2020-02-05 15:27:59 -08002696 int ret;
2697
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002698 if (!clk)
2699 return 0;
2700
Stephen Boydf21cf9c2020-02-05 15:27:59 -08002701 clk_prepare_lock();
2702 ret = clk_core_get_phase(clk->core);
2703 clk_prepare_unlock();
2704
2705 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002706}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002707EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002708
Jerome Brunet9fba7382018-06-19 16:41:41 +02002709static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2710{
2711 /* Assume a default value of 50% */
2712 core->duty.num = 1;
2713 core->duty.den = 2;
2714}
2715
2716static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2717
2718static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2719{
2720 struct clk_duty *duty = &core->duty;
2721 int ret = 0;
2722
2723 if (!core->ops->get_duty_cycle)
2724 return clk_core_update_duty_cycle_parent_nolock(core);
2725
2726 ret = core->ops->get_duty_cycle(core->hw, duty);
2727 if (ret)
2728 goto reset;
2729
2730 /* Don't trust the clock provider too much */
2731 if (duty->den == 0 || duty->num > duty->den) {
2732 ret = -EINVAL;
2733 goto reset;
2734 }
2735
2736 return 0;
2737
2738reset:
2739 clk_core_reset_duty_cycle_nolock(core);
2740 return ret;
2741}
2742
2743static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2744{
2745 int ret = 0;
2746
2747 if (core->parent &&
2748 core->flags & CLK_DUTY_CYCLE_PARENT) {
2749 ret = clk_core_update_duty_cycle_nolock(core->parent);
2750 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2751 } else {
2752 clk_core_reset_duty_cycle_nolock(core);
2753 }
2754
2755 return ret;
2756}
2757
2758static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2759 struct clk_duty *duty);
2760
2761static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2762 struct clk_duty *duty)
2763{
2764 int ret;
2765
2766 lockdep_assert_held(&prepare_lock);
2767
2768 if (clk_core_rate_is_protected(core))
2769 return -EBUSY;
2770
2771 trace_clk_set_duty_cycle(core, duty);
2772
2773 if (!core->ops->set_duty_cycle)
2774 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2775
2776 ret = core->ops->set_duty_cycle(core->hw, duty);
2777 if (!ret)
2778 memcpy(&core->duty, duty, sizeof(*duty));
2779
2780 trace_clk_set_duty_cycle_complete(core, duty);
2781
2782 return ret;
2783}
2784
2785static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2786 struct clk_duty *duty)
2787{
2788 int ret = 0;
2789
2790 if (core->parent &&
2791 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2792 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2793 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2794 }
2795
2796 return ret;
2797}
2798
2799/**
2800 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2801 * @clk: clock signal source
2802 * @num: numerator of the duty cycle ratio to be applied
2803 * @den: denominator of the duty cycle ratio to be applied
2804 *
2805 * Apply the duty cycle ratio if the ratio is valid and the clock can
2806 * perform this operation
2807 *
2808 * Returns (0) on success, a negative errno otherwise.
2809 */
2810int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2811{
2812 int ret;
2813 struct clk_duty duty;
2814
2815 if (!clk)
2816 return 0;
2817
2818 /* sanity check the ratio */
2819 if (den == 0 || num > den)
2820 return -EINVAL;
2821
2822 duty.num = num;
2823 duty.den = den;
2824
2825 clk_prepare_lock();
2826
2827 if (clk->exclusive_count)
2828 clk_core_rate_unprotect(clk->core);
2829
2830 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2831
2832 if (clk->exclusive_count)
2833 clk_core_rate_protect(clk->core);
2834
2835 clk_prepare_unlock();
2836
2837 return ret;
2838}
2839EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2840
2841static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2842 unsigned int scale)
2843{
2844 struct clk_duty *duty = &core->duty;
2845 int ret;
2846
2847 clk_prepare_lock();
2848
2849 ret = clk_core_update_duty_cycle_nolock(core);
2850 if (!ret)
2851 ret = mult_frac(scale, duty->num, duty->den);
2852
2853 clk_prepare_unlock();
2854
2855 return ret;
2856}
2857
2858/**
2859 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2860 * @clk: clock signal source
2861 * @scale: scaling factor to be applied to represent the ratio as an integer
2862 *
2863 * Returns the duty cycle ratio of a clock node multiplied by the provided
2864 * scaling factor, or negative errno on error.
2865 */
2866int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2867{
2868 if (!clk)
2869 return 0;
2870
2871 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2872}
2873EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2874
Mike Turquetteb24764902012-03-15 23:11:19 -07002875/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002876 * clk_is_match - check if two clk's point to the same hardware clock
2877 * @p: clk compared against q
2878 * @q: clk compared against p
2879 *
2880 * Returns true if the two struct clk pointers both point to the same hardware
2881 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2882 * share the same struct clk_core object.
2883 *
2884 * Returns false otherwise. Note that two NULL clks are treated as matching.
2885 */
2886bool clk_is_match(const struct clk *p, const struct clk *q)
2887{
2888 /* trivial case: identical struct clk's or both NULL */
2889 if (p == q)
2890 return true;
2891
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002892 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002893 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2894 if (p->core == q->core)
2895 return true;
2896
2897 return false;
2898}
2899EXPORT_SYMBOL_GPL(clk_is_match);
2900
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002901/*** debugfs support ***/
2902
2903#ifdef CONFIG_DEBUG_FS
2904#include <linux/debugfs.h>
2905
2906static struct dentry *rootdir;
2907static int inited = 0;
2908static DEFINE_MUTEX(clk_debug_lock);
2909static HLIST_HEAD(clk_debug_list);
2910
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002911static struct hlist_head *orphan_list[] = {
2912 &clk_orphan_list,
2913 NULL,
2914};
2915
2916static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2917 int level)
2918{
Stephen Boydf21cf9c2020-02-05 15:27:59 -08002919 int phase;
2920
2921 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu ",
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002922 level * 3 + 1, "",
2923 30 - level * 3, c->name,
Jerome Brunete55a8392017-12-01 22:51:56 +01002924 c->enable_count, c->prepare_count, c->protect_count,
Stephen Boyd0daa3762020-02-05 15:28:01 -08002925 clk_core_get_rate_recalc(c),
2926 clk_core_get_accuracy_recalc(c));
Stephen Boydf21cf9c2020-02-05 15:27:59 -08002927
2928 phase = clk_core_get_phase(c);
2929 if (phase >= 0)
2930 seq_printf(s, "%5d", phase);
2931 else
2932 seq_puts(s, "-----");
2933
2934 seq_printf(s, " %6d\n", clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002935}
2936
2937static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2938 int level)
2939{
2940 struct clk_core *child;
2941
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002942 clk_summary_show_one(s, c, level);
2943
2944 hlist_for_each_entry(child, &c->children, child_node)
2945 clk_summary_show_subtree(s, child, level + 1);
2946}
2947
2948static int clk_summary_show(struct seq_file *s, void *data)
2949{
2950 struct clk_core *c;
2951 struct hlist_head **lists = (struct hlist_head **)s->private;
2952
Jerome Brunet9fba7382018-06-19 16:41:41 +02002953 seq_puts(s, " enable prepare protect duty\n");
2954 seq_puts(s, " clock count count count rate accuracy phase cycle\n");
2955 seq_puts(s, "---------------------------------------------------------------------------------------------\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002956
2957 clk_prepare_lock();
2958
2959 for (; *lists; lists++)
2960 hlist_for_each_entry(c, *lists, child_node)
2961 clk_summary_show_subtree(s, c, 0);
2962
2963 clk_prepare_unlock();
2964
2965 return 0;
2966}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002967DEFINE_SHOW_ATTRIBUTE(clk_summary);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002968
2969static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2970{
Stephen Boydf21cf9c2020-02-05 15:27:59 -08002971 int phase;
Leonard Crestez1bd37a42019-07-02 16:27:09 +03002972 unsigned long min_rate, max_rate;
2973
Leonard Crestez1bd37a42019-07-02 16:27:09 +03002974 clk_core_get_boundaries(c, &min_rate, &max_rate);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002975
Stefan Wahren7cb81132015-04-29 16:36:43 +00002976 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002977 seq_printf(s, "\"%s\": { ", c->name);
2978 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2979 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002980 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
Stephen Boyd0daa3762020-02-05 15:28:01 -08002981 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate_recalc(c));
Leonard Crestez1bd37a42019-07-02 16:27:09 +03002982 seq_printf(s, "\"min_rate\": %lu,", min_rate);
2983 seq_printf(s, "\"max_rate\": %lu,", max_rate);
Stephen Boyd0daa3762020-02-05 15:28:01 -08002984 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy_recalc(c));
Stephen Boydf21cf9c2020-02-05 15:27:59 -08002985 phase = clk_core_get_phase(c);
2986 if (phase >= 0)
2987 seq_printf(s, "\"phase\": %d,", phase);
Jerome Brunet9fba7382018-06-19 16:41:41 +02002988 seq_printf(s, "\"duty_cycle\": %u",
2989 clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002990}
2991
2992static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2993{
2994 struct clk_core *child;
2995
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002996 clk_dump_one(s, c, level);
2997
2998 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002999 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003000 clk_dump_subtree(s, child, level + 1);
3001 }
3002
Markus Elfring4d327582017-04-20 08:45:43 +02003003 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003004}
3005
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02003006static int clk_dump_show(struct seq_file *s, void *data)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003007{
3008 struct clk_core *c;
3009 bool first_node = true;
3010 struct hlist_head **lists = (struct hlist_head **)s->private;
3011
Markus Elfring4d327582017-04-20 08:45:43 +02003012 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003013 clk_prepare_lock();
3014
3015 for (; *lists; lists++) {
3016 hlist_for_each_entry(c, *lists, child_node) {
3017 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02003018 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003019 first_node = false;
3020 clk_dump_subtree(s, c, 0);
3021 }
3022 }
3023
3024 clk_prepare_unlock();
3025
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05003026 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003027 return 0;
3028}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02003029DEFINE_SHOW_ATTRIBUTE(clk_dump);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003030
Geert Uytterhoeven37215da2019-08-28 15:23:06 +02003031#undef CLOCK_ALLOW_WRITE_DEBUGFS
3032#ifdef CLOCK_ALLOW_WRITE_DEBUGFS
3033/*
3034 * This can be dangerous, therefore don't provide any real compile time
3035 * configuration option for this feature.
3036 * People who want to use this will need to modify the source code directly.
3037 */
3038static int clk_rate_set(void *data, u64 val)
3039{
3040 struct clk_core *core = data;
3041 int ret;
3042
3043 clk_prepare_lock();
3044 ret = clk_core_set_rate_nolock(core, val);
3045 clk_prepare_unlock();
3046
3047 return ret;
3048}
3049
3050#define clk_rate_mode 0644
Mike Tipton03111b12020-06-29 17:30:24 -07003051
3052static int clk_prepare_enable_set(void *data, u64 val)
3053{
3054 struct clk_core *core = data;
3055 int ret = 0;
3056
3057 if (val)
3058 ret = clk_prepare_enable(core->hw->clk);
3059 else
3060 clk_disable_unprepare(core->hw->clk);
3061
3062 return ret;
3063}
3064
3065static int clk_prepare_enable_get(void *data, u64 *val)
3066{
3067 struct clk_core *core = data;
3068
3069 *val = core->enable_count && core->prepare_count;
3070 return 0;
3071}
3072
3073DEFINE_DEBUGFS_ATTRIBUTE(clk_prepare_enable_fops, clk_prepare_enable_get,
3074 clk_prepare_enable_set, "%llu\n");
3075
Geert Uytterhoeven37215da2019-08-28 15:23:06 +02003076#else
3077#define clk_rate_set NULL
3078#define clk_rate_mode 0444
3079#endif
3080
3081static int clk_rate_get(void *data, u64 *val)
3082{
3083 struct clk_core *core = data;
3084
3085 *val = core->rate;
3086 return 0;
3087}
3088
3089DEFINE_DEBUGFS_ATTRIBUTE(clk_rate_fops, clk_rate_get, clk_rate_set, "%llu\n");
3090
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01003091static const struct {
3092 unsigned long flag;
3093 const char *name;
3094} clk_flags[] = {
Geert Uytterhoeven40dd71c2018-07-06 17:16:54 +02003095#define ENTRY(f) { f, #f }
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01003096 ENTRY(CLK_SET_RATE_GATE),
3097 ENTRY(CLK_SET_PARENT_GATE),
3098 ENTRY(CLK_SET_RATE_PARENT),
3099 ENTRY(CLK_IGNORE_UNUSED),
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01003100 ENTRY(CLK_GET_RATE_NOCACHE),
3101 ENTRY(CLK_SET_RATE_NO_REPARENT),
3102 ENTRY(CLK_GET_ACCURACY_NOCACHE),
3103 ENTRY(CLK_RECALC_NEW_RATES),
3104 ENTRY(CLK_SET_RATE_UNGATE),
3105 ENTRY(CLK_IS_CRITICAL),
3106 ENTRY(CLK_OPS_PARENT_ENABLE),
Jerome Brunet9fba7382018-06-19 16:41:41 +02003107 ENTRY(CLK_DUTY_CYCLE_PARENT),
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01003108#undef ENTRY
3109};
3110
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02003111static int clk_flags_show(struct seq_file *s, void *data)
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01003112{
3113 struct clk_core *core = s->private;
3114 unsigned long flags = core->flags;
3115 unsigned int i;
3116
3117 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
3118 if (flags & clk_flags[i].flag) {
3119 seq_printf(s, "%s\n", clk_flags[i].name);
3120 flags &= ~clk_flags[i].flag;
3121 }
3122 }
3123 if (flags) {
3124 /* Unknown flags */
3125 seq_printf(s, "0x%lx\n", flags);
3126 }
3127
3128 return 0;
3129}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02003130DEFINE_SHOW_ATTRIBUTE(clk_flags);
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01003131
Stephen Boyd11f6c232019-06-24 20:01:55 -07003132static void possible_parent_show(struct seq_file *s, struct clk_core *core,
3133 unsigned int i, char terminator)
Peter De Schrijver92031572017-03-21 15:20:31 +02003134{
Chen-Yu Tsai2d156b72019-05-03 11:15:09 +08003135 struct clk_core *parent;
Peter De Schrijver92031572017-03-21 15:20:31 +02003136
Chen-Yu Tsai2d156b72019-05-03 11:15:09 +08003137 /*
3138 * Go through the following options to fetch a parent's name.
3139 *
3140 * 1. Fetch the registered parent clock and use its name
3141 * 2. Use the global (fallback) name if specified
3142 * 3. Use the local fw_name if provided
3143 * 4. Fetch parent clock's clock-output-name if DT index was set
3144 *
3145 * This may still fail in some cases, such as when the parent is
3146 * specified directly via a struct clk_hw pointer, but it isn't
3147 * registered (yet).
3148 */
Chen-Yu Tsai2d156b72019-05-03 11:15:09 +08003149 parent = clk_core_get_parent_by_index(core, i);
3150 if (parent)
Markus Elfring1ccc0dd2019-07-01 22:20:40 +02003151 seq_puts(s, parent->name);
Chen-Yu Tsai2d156b72019-05-03 11:15:09 +08003152 else if (core->parents[i].name)
Markus Elfring1ccc0dd2019-07-01 22:20:40 +02003153 seq_puts(s, core->parents[i].name);
Chen-Yu Tsai2d156b72019-05-03 11:15:09 +08003154 else if (core->parents[i].fw_name)
3155 seq_printf(s, "<%s>(fw)", core->parents[i].fw_name);
3156 else if (core->parents[i].index >= 0)
Markus Elfring1ccc0dd2019-07-01 22:20:40 +02003157 seq_puts(s,
3158 of_clk_get_parent_name(core->of_node,
3159 core->parents[i].index));
Chen-Yu Tsai2d156b72019-05-03 11:15:09 +08003160 else
3161 seq_puts(s, "(missing)");
Peter De Schrijver92031572017-03-21 15:20:31 +02003162
Stephen Boyd11f6c232019-06-24 20:01:55 -07003163 seq_putc(s, terminator);
3164}
3165
Peter De Schrijver92031572017-03-21 15:20:31 +02003166static int possible_parents_show(struct seq_file *s, void *data)
3167{
3168 struct clk_core *core = s->private;
3169 int i;
3170
3171 for (i = 0; i < core->num_parents - 1; i++)
Stephen Boyd11f6c232019-06-24 20:01:55 -07003172 possible_parent_show(s, core, i, ' ');
Peter De Schrijver92031572017-03-21 15:20:31 +02003173
Stephen Boyd11f6c232019-06-24 20:01:55 -07003174 possible_parent_show(s, core, i, '\n');
Peter De Schrijver92031572017-03-21 15:20:31 +02003175
3176 return 0;
3177}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02003178DEFINE_SHOW_ATTRIBUTE(possible_parents);
Peter De Schrijver92031572017-03-21 15:20:31 +02003179
Leonard Cresteze5e89242019-06-10 14:06:38 +03003180static int current_parent_show(struct seq_file *s, void *data)
3181{
3182 struct clk_core *core = s->private;
3183
3184 if (core->parent)
3185 seq_printf(s, "%s\n", core->parent->name);
3186
3187 return 0;
3188}
3189DEFINE_SHOW_ATTRIBUTE(current_parent);
3190
Jerome Brunet9fba7382018-06-19 16:41:41 +02003191static int clk_duty_cycle_show(struct seq_file *s, void *data)
3192{
3193 struct clk_core *core = s->private;
3194 struct clk_duty *duty = &core->duty;
3195
3196 seq_printf(s, "%u/%u\n", duty->num, duty->den);
3197
3198 return 0;
3199}
3200DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
3201
Leonard Crestez1bd37a42019-07-02 16:27:09 +03003202static int clk_min_rate_show(struct seq_file *s, void *data)
3203{
3204 struct clk_core *core = s->private;
3205 unsigned long min_rate, max_rate;
3206
3207 clk_prepare_lock();
3208 clk_core_get_boundaries(core, &min_rate, &max_rate);
3209 clk_prepare_unlock();
3210 seq_printf(s, "%lu\n", min_rate);
3211
3212 return 0;
3213}
3214DEFINE_SHOW_ATTRIBUTE(clk_min_rate);
3215
3216static int clk_max_rate_show(struct seq_file *s, void *data)
3217{
3218 struct clk_core *core = s->private;
3219 unsigned long min_rate, max_rate;
3220
3221 clk_prepare_lock();
3222 clk_core_get_boundaries(core, &min_rate, &max_rate);
3223 clk_prepare_unlock();
3224 seq_printf(s, "%lu\n", max_rate);
3225
3226 return 0;
3227}
3228DEFINE_SHOW_ATTRIBUTE(clk_max_rate);
3229
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003230static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003231{
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003232 struct dentry *root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003233
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003234 if (!core || !pdentry)
3235 return;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003236
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003237 root = debugfs_create_dir(core->name, pdentry);
3238 core->dentry = root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003239
Geert Uytterhoeven37215da2019-08-28 15:23:06 +02003240 debugfs_create_file("clk_rate", clk_rate_mode, root, core,
3241 &clk_rate_fops);
Leonard Crestez1bd37a42019-07-02 16:27:09 +03003242 debugfs_create_file("clk_min_rate", 0444, root, core, &clk_min_rate_fops);
3243 debugfs_create_file("clk_max_rate", 0444, root, core, &clk_max_rate_fops);
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003244 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
3245 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
3246 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
3247 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
3248 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
3249 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
3250 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
Jerome Brunet9fba7382018-06-19 16:41:41 +02003251 debugfs_create_file("clk_duty_cycle", 0444, root, core,
3252 &clk_duty_cycle_fops);
Mike Tipton03111b12020-06-29 17:30:24 -07003253#ifdef CLOCK_ALLOW_WRITE_DEBUGFS
3254 debugfs_create_file("clk_prepare_enable", 0644, root, core,
3255 &clk_prepare_enable_fops);
3256#endif
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003257
Leonard Cresteze5e89242019-06-10 14:06:38 +03003258 if (core->num_parents > 0)
3259 debugfs_create_file("clk_parent", 0444, root, core,
3260 &current_parent_fops);
3261
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003262 if (core->num_parents > 1)
3263 debugfs_create_file("clk_possible_parents", 0444, root, core,
3264 &possible_parents_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003265
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003266 if (core->ops->debug_init)
3267 core->ops->debug_init(core->hw, core->dentry);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003268}
3269
3270/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003271 * clk_debug_register - add a clk node to the debugfs clk directory
3272 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003273 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003274 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3275 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003276 * will be created lazily by clk_debug_init as part of a late_initcall.
3277 */
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003278static void clk_debug_register(struct clk_core *core)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003279{
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003280 mutex_lock(&clk_debug_lock);
3281 hlist_add_head(&core->debug_node, &clk_debug_list);
Stephen Boyddb3188fa2018-01-03 16:44:37 -08003282 if (inited)
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003283 clk_debug_create_one(core, rootdir);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003284 mutex_unlock(&clk_debug_lock);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003285}
3286
3287 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003288 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3289 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003290 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003291 * Dynamically removes a clk and all its child nodes from the
3292 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08003293 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003294 */
3295static void clk_debug_unregister(struct clk_core *core)
3296{
3297 mutex_lock(&clk_debug_lock);
3298 hlist_del_init(&core->debug_node);
3299 debugfs_remove_recursive(core->dentry);
3300 core->dentry = NULL;
3301 mutex_unlock(&clk_debug_lock);
3302}
3303
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003304/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003305 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003306 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003307 * clks are often initialized very early during boot before memory can be
3308 * dynamically allocated and well before debugfs is setup. This function
3309 * populates the debugfs clk directory once at boot-time when we know that
3310 * debugfs is setup. It should only be called once at boot-time, all other clks
3311 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003312 */
3313static int __init clk_debug_init(void)
3314{
3315 struct clk_core *core;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003316
3317 rootdir = debugfs_create_dir("clk", NULL);
3318
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003319 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
3320 &clk_summary_fops);
3321 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
3322 &clk_dump_fops);
3323 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
3324 &clk_summary_fops);
3325 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
3326 &clk_dump_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003327
3328 mutex_lock(&clk_debug_lock);
3329 hlist_for_each_entry(core, &clk_debug_list, debug_node)
3330 clk_debug_create_one(core, rootdir);
3331
3332 inited = 1;
3333 mutex_unlock(&clk_debug_lock);
3334
3335 return 0;
3336}
3337late_initcall(clk_debug_init);
3338#else
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003339static inline void clk_debug_register(struct clk_core *core) { }
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003340static inline void clk_debug_unregister(struct clk_core *core)
3341{
3342}
3343#endif
3344
Jerome Brunet66d95062019-12-03 09:08:05 +01003345static void clk_core_reparent_orphans_nolock(void)
3346{
3347 struct clk_core *orphan;
3348 struct hlist_node *tmp2;
3349
3350 /*
3351 * walk the list of orphan clocks and reparent any that newly finds a
3352 * parent.
3353 */
3354 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
3355 struct clk_core *parent = __clk_init_parent(orphan);
3356
3357 /*
3358 * We need to use __clk_set_parent_before() and _after() to
3359 * to properly migrate any prepare/enable count of the orphan
3360 * clock. This is important for CLK_IS_CRITICAL clocks, which
3361 * are enabled during init but might not have a parent yet.
3362 */
3363 if (parent) {
3364 /* update the clk tree topology */
3365 __clk_set_parent_before(orphan, parent);
3366 __clk_set_parent_after(orphan, parent, NULL);
3367 __clk_recalc_accuracies(orphan);
3368 __clk_recalc_rates(orphan, 0);
3369 }
3370 }
3371}
3372
Michael Turquette3d3801e2015-02-25 09:11:01 -08003373/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003374 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003375 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07003376 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003377 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07003378 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07003379 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003380static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07003381{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003382 int ret;
Stephen Boyd768a5d42020-02-05 15:28:00 -08003383 struct clk_core *parent;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003384 unsigned long rate;
Maxime Ripardc3944ec2020-02-25 14:42:48 +01003385 int phase;
Mike Turquetteb24764902012-03-15 23:11:19 -07003386
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003387 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07003388 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07003389
Mike Turquetteeab89f62013-03-28 13:59:01 -07003390 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003391
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003392 ret = clk_pm_runtime_get(core);
3393 if (ret)
3394 goto unlock;
3395
Mike Turquetteb24764902012-03-15 23:11:19 -07003396 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003397 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003398 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003399 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003400 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07003401 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07003402 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003403
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03003404 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003405 if (core->ops->set_rate &&
3406 !((core->ops->round_rate || core->ops->determine_rate) &&
3407 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003408 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3409 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003410 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003411 goto out;
3412 }
3413
Stephen Boydd6968fc2015-04-30 13:54:13 -07003414 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003415 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3416 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003417 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003418 goto out;
3419 }
3420
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09003421 if (core->num_parents > 1 && !core->ops->get_parent) {
3422 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3423 __func__, core->name);
3424 ret = -EINVAL;
3425 goto out;
3426 }
3427
Stephen Boydd6968fc2015-04-30 13:54:13 -07003428 if (core->ops->set_rate_and_parent &&
3429 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003430 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003431 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08003432 ret = -EINVAL;
3433 goto out;
3434 }
3435
Jerome Brunetf6fa75c2019-09-24 14:39:52 +02003436 /*
3437 * optional platform-specific magic
3438 *
3439 * The .init callback is not used by any of the basic clock types, but
Jerome Brunet89d079d2019-09-24 14:39:53 +02003440 * exists for weird hardware that must perform initialization magic for
3441 * CCF to get an accurate view of clock for any other callbacks. It may
3442 * also be used needs to perform dynamic allocations. Such allocation
3443 * must be freed in the terminate() callback.
3444 * This callback shall not be used to initialize the parameters state,
3445 * such as rate, parent, etc ...
Jerome Brunetf6fa75c2019-09-24 14:39:52 +02003446 *
3447 * If it exist, this callback should called before any other callback of
3448 * the clock
3449 */
Jerome Brunet89d079d2019-09-24 14:39:53 +02003450 if (core->ops->init) {
3451 ret = core->ops->init(core->hw);
3452 if (ret)
3453 goto out;
3454 }
Jerome Brunetf6fa75c2019-09-24 14:39:52 +02003455
Stephen Boyd768a5d42020-02-05 15:28:00 -08003456 parent = core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07003457
3458 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08003459 * Populate core->parent if parent has already been clk_core_init'd. If
3460 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003461 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07003462 * clk list.
3463 *
3464 * Every time a new clk is clk_init'd then we walk the list of orphan
3465 * clocks and re-parent any that are children of the clock currently
3466 * being clk_init'd.
3467 */
Stephen Boyd768a5d42020-02-05 15:28:00 -08003468 if (parent) {
3469 hlist_add_head(&core->child_node, &parent->children);
3470 core->orphan = parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003471 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003472 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003473 core->orphan = false;
3474 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003475 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003476 core->orphan = true;
3477 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003478
3479 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003480 * Set clk's accuracy. The preferred method is to use
3481 * .recalc_accuracy. For simple clocks and lazy developers the default
3482 * fallback is to use the parent's accuracy. If a clock doesn't have a
3483 * parent (or is orphaned) then accuracy is set to zero (perfect
3484 * clock).
3485 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003486 if (core->ops->recalc_accuracy)
3487 core->accuracy = core->ops->recalc_accuracy(core->hw,
Stephen Boyd0daa3762020-02-05 15:28:01 -08003488 clk_core_get_accuracy_no_lock(parent));
Stephen Boyd768a5d42020-02-05 15:28:00 -08003489 else if (parent)
3490 core->accuracy = parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003491 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003492 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003493
3494 /*
Stephen Boydf21cf9c2020-02-05 15:27:59 -08003495 * Set clk's phase by clk_core_get_phase() caching the phase.
Maxime Ripard9824cf72014-07-14 13:53:27 +02003496 * Since a phase is by definition relative to its parent, just
3497 * query the current clock phase, or just assume it's in phase.
3498 */
Maxime Ripardc3944ec2020-02-25 14:42:48 +01003499 phase = clk_core_get_phase(core);
3500 if (phase < 0) {
3501 ret = phase;
Stephen Boyd27608782020-02-05 15:28:02 -08003502 pr_warn("%s: Failed to get phase for clk '%s'\n", __func__,
3503 core->name);
3504 goto out;
3505 }
Maxime Ripard9824cf72014-07-14 13:53:27 +02003506
3507 /*
Jerome Brunet9fba7382018-06-19 16:41:41 +02003508 * Set clk's duty cycle.
3509 */
3510 clk_core_update_duty_cycle_nolock(core);
3511
3512 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07003513 * Set clk's rate. The preferred method is to use .recalc_rate. For
3514 * simple clocks and lazy developers the default fallback is to use the
3515 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3516 * then rate is set to zero.
3517 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003518 if (core->ops->recalc_rate)
3519 rate = core->ops->recalc_rate(core->hw,
Stephen Boyd768a5d42020-02-05 15:28:00 -08003520 clk_core_get_rate_nolock(parent));
3521 else if (parent)
3522 rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003523 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003524 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003525 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003526
3527 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003528 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3529 * don't get accidentally disabled when walking the orphan tree and
3530 * reparenting clocks
3531 */
3532 if (core->flags & CLK_IS_CRITICAL) {
3533 unsigned long flags;
3534
Guenter Roeck12ead772019-12-25 08:34:29 -08003535 ret = clk_core_prepare(core);
Stephen Boyd2d269992019-12-26 14:09:27 -08003536 if (ret) {
3537 pr_warn("%s: critical clk '%s' failed to prepare\n",
3538 __func__, core->name);
Guenter Roeck12ead772019-12-25 08:34:29 -08003539 goto out;
Stephen Boyd2d269992019-12-26 14:09:27 -08003540 }
Jerome Brunet99652a42018-02-14 14:43:36 +01003541
3542 flags = clk_enable_lock();
Guenter Roeck12ead772019-12-25 08:34:29 -08003543 ret = clk_core_enable(core);
Jerome Brunet99652a42018-02-14 14:43:36 +01003544 clk_enable_unlock(flags);
Guenter Roeck12ead772019-12-25 08:34:29 -08003545 if (ret) {
Stephen Boyd2d269992019-12-26 14:09:27 -08003546 pr_warn("%s: critical clk '%s' failed to enable\n",
3547 __func__, core->name);
Guenter Roeck12ead772019-12-25 08:34:29 -08003548 clk_core_unprepare(core);
3549 goto out;
Michael Turquette904e6ea2016-07-08 16:32:10 -07003550 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003551 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003552
Jerome Brunet66d95062019-12-03 09:08:05 +01003553 clk_core_reparent_orphans_nolock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003554
Mike Turquetteb24764902012-03-15 23:11:19 -07003555
Stephen Boydd6968fc2015-04-30 13:54:13 -07003556 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07003557out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003558 clk_pm_runtime_put(core);
3559unlock:
Marc Zyngier018d4672020-05-05 15:09:53 +01003560 if (ret)
3561 hlist_del_init(&core->child_node);
3562
Mike Turquetteeab89f62013-03-28 13:59:01 -07003563 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003564
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003565 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003566 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003567
Mike Turquetted1302a32012-03-29 14:30:40 -07003568 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07003569}
3570
Stephen Boyd1df40462018-12-11 08:32:04 -08003571/**
3572 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3573 * @core: clk to add consumer to
3574 * @clk: consumer to link to a clk
3575 */
3576static void clk_core_link_consumer(struct clk_core *core, struct clk *clk)
3577{
3578 clk_prepare_lock();
3579 hlist_add_head(&clk->clks_node, &core->clks);
3580 clk_prepare_unlock();
3581}
3582
3583/**
3584 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3585 * @clk: consumer to unlink
3586 */
3587static void clk_core_unlink_consumer(struct clk *clk)
3588{
3589 lockdep_assert_held(&prepare_lock);
3590 hlist_del(&clk->clks_node);
3591}
3592
3593/**
3594 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
3595 * @core: clk to allocate a consumer for
3596 * @dev_id: string describing device name
3597 * @con_id: connection ID string on device
3598 *
3599 * Returns: clk consumer left unlinked from the consumer list
3600 */
3601static struct clk *alloc_clk(struct clk_core *core, const char *dev_id,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003602 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003603{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003604 struct clk *clk;
3605
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003606 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3607 if (!clk)
3608 return ERR_PTR(-ENOMEM);
3609
Stephen Boyd1df40462018-12-11 08:32:04 -08003610 clk->core = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003611 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02003612 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003613 clk->max_rate = ULONG_MAX;
3614
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003615 return clk;
3616}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003617
Stephen Boyd1df40462018-12-11 08:32:04 -08003618/**
3619 * free_clk - Free a clk consumer
3620 * @clk: clk consumer to free
3621 *
3622 * Note, this assumes the clk has been unlinked from the clk_core consumer
3623 * list.
3624 */
3625static void free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003626{
Leonard Crestez253160a2017-02-20 15:20:56 +02003627 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003628 kfree(clk);
3629}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003630
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003631/**
Stephen Boyd1df40462018-12-11 08:32:04 -08003632 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
3633 * a clk_hw
Stephen Boydefa85042018-12-11 08:34:16 -08003634 * @dev: clk consumer device
Stephen Boyd1df40462018-12-11 08:32:04 -08003635 * @hw: clk_hw associated with the clk being consumed
3636 * @dev_id: string describing device name
3637 * @con_id: connection ID string on device
3638 *
3639 * This is the main function used to create a clk pointer for use by clk
3640 * consumers. It connects a consumer to the clk_core and clk_hw structures
3641 * used by the framework and clk provider respectively.
3642 */
Stephen Boydefa85042018-12-11 08:34:16 -08003643struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
Stephen Boyd1df40462018-12-11 08:32:04 -08003644 const char *dev_id, const char *con_id)
3645{
3646 struct clk *clk;
3647 struct clk_core *core;
3648
3649 /* This is to allow this function to be chained to others */
3650 if (IS_ERR_OR_NULL(hw))
3651 return ERR_CAST(hw);
3652
3653 core = hw->core;
3654 clk = alloc_clk(core, dev_id, con_id);
3655 if (IS_ERR(clk))
3656 return clk;
Stephen Boydefa85042018-12-11 08:34:16 -08003657 clk->dev = dev;
Stephen Boyd1df40462018-12-11 08:32:04 -08003658
3659 if (!try_module_get(core->owner)) {
3660 free_clk(clk);
3661 return ERR_PTR(-ENOENT);
3662 }
3663
3664 kref_get(&core->ref);
3665 clk_core_link_consumer(core, clk);
3666
3667 return clk;
3668}
3669
Jerome Brunet30d6f8c12020-10-21 18:21:46 +02003670/**
3671 * clk_hw_get_clk - get clk consumer given an clk_hw
3672 * @hw: clk_hw associated with the clk being consumed
3673 * @con_id: connection ID string on device
3674 *
3675 * Returns: new clk consumer
3676 * This is the function to be used by providers which need
3677 * to get a consumer clk and act on the clock element
3678 * Calls to this function must be balanced with calls clk_put()
3679 */
3680struct clk *clk_hw_get_clk(struct clk_hw *hw, const char *con_id)
3681{
3682 struct device *dev = hw->core->dev;
3683
3684 return clk_hw_create_clk(dev, hw, dev_name(dev), con_id);
3685}
3686EXPORT_SYMBOL(clk_hw_get_clk);
3687
Stephen Boydfc0c2092019-04-12 11:31:47 -07003688static int clk_cpy_name(const char **dst_p, const char *src, bool must_exist)
Mike Turquetteb24764902012-03-15 23:11:19 -07003689{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003690 const char *dst;
3691
3692 if (!src) {
3693 if (must_exist)
3694 return -EINVAL;
3695 return 0;
3696 }
3697
3698 *dst_p = dst = kstrdup_const(src, GFP_KERNEL);
3699 if (!dst)
3700 return -ENOMEM;
3701
3702 return 0;
3703}
3704
Stephen Boyd0214f332019-07-31 12:35:17 -07003705static int clk_core_populate_parent_map(struct clk_core *core,
3706 const struct clk_init_data *init)
Stephen Boydfc0c2092019-04-12 11:31:47 -07003707{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003708 u8 num_parents = init->num_parents;
3709 const char * const *parent_names = init->parent_names;
3710 const struct clk_hw **parent_hws = init->parent_hws;
3711 const struct clk_parent_data *parent_data = init->parent_data;
3712 int i, ret = 0;
3713 struct clk_parent_map *parents, *parent;
3714
3715 if (!num_parents)
3716 return 0;
3717
3718 /*
3719 * Avoid unnecessary string look-ups of clk_core's possible parents by
3720 * having a cache of names/clk_hw pointers to clk_core pointers.
3721 */
3722 parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
3723 core->parents = parents;
3724 if (!parents)
3725 return -ENOMEM;
3726
3727 /* Copy everything over because it might be __initdata */
3728 for (i = 0, parent = parents; i < num_parents; i++, parent++) {
Stephen Boyd601b6e92019-04-12 11:31:49 -07003729 parent->index = -1;
Stephen Boydfc0c2092019-04-12 11:31:47 -07003730 if (parent_names) {
3731 /* throw a WARN if any entries are NULL */
3732 WARN(!parent_names[i],
3733 "%s: invalid NULL in %s's .parent_names\n",
3734 __func__, core->name);
3735 ret = clk_cpy_name(&parent->name, parent_names[i],
3736 true);
3737 } else if (parent_data) {
3738 parent->hw = parent_data[i].hw;
Stephen Boyd601b6e92019-04-12 11:31:49 -07003739 parent->index = parent_data[i].index;
Stephen Boydfc0c2092019-04-12 11:31:47 -07003740 ret = clk_cpy_name(&parent->fw_name,
3741 parent_data[i].fw_name, false);
3742 if (!ret)
3743 ret = clk_cpy_name(&parent->name,
3744 parent_data[i].name,
3745 false);
3746 } else if (parent_hws) {
3747 parent->hw = parent_hws[i];
3748 } else {
3749 ret = -EINVAL;
3750 WARN(1, "Must specify parents if num_parents > 0\n");
3751 }
3752
3753 if (ret) {
3754 do {
3755 kfree_const(parents[i].name);
3756 kfree_const(parents[i].fw_name);
3757 } while (--i >= 0);
3758 kfree(parents);
3759
3760 return ret;
3761 }
3762 }
3763
3764 return 0;
3765}
3766
3767static void clk_core_free_parent_map(struct clk_core *core)
3768{
3769 int i = core->num_parents;
3770
3771 if (!core->num_parents)
3772 return;
3773
3774 while (--i >= 0) {
3775 kfree_const(core->parents[i].name);
3776 kfree_const(core->parents[i].fw_name);
3777 }
3778
3779 kfree(core->parents);
3780}
3781
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003782static struct clk *
3783__clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07003784{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003785 int ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003786 struct clk_core *core;
Stephen Boyd0214f332019-07-31 12:35:17 -07003787 const struct clk_init_data *init = hw->init;
3788
3789 /*
3790 * The init data is not supposed to be used outside of registration path.
3791 * Set it to NULL so that provider drivers can't use it either and so that
3792 * we catch use of hw->init early on in the core.
3793 */
3794 hw->init = NULL;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003795
Stephen Boydd6968fc2015-04-30 13:54:13 -07003796 core = kzalloc(sizeof(*core), GFP_KERNEL);
3797 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003798 ret = -ENOMEM;
3799 goto fail_out;
3800 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003801
Stephen Boyd0214f332019-07-31 12:35:17 -07003802 core->name = kstrdup_const(init->name, GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003803 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003804 ret = -ENOMEM;
3805 goto fail_name;
3806 }
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003807
Stephen Boyd0214f332019-07-31 12:35:17 -07003808 if (WARN_ON(!init->ops)) {
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003809 ret = -EINVAL;
3810 goto fail_ops;
3811 }
Stephen Boyd0214f332019-07-31 12:35:17 -07003812 core->ops = init->ops;
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003813
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003814 if (dev && pm_runtime_enabled(dev))
Miquel Raynal24478832018-12-04 20:24:37 +01003815 core->rpm_enabled = true;
3816 core->dev = dev;
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003817 core->of_node = np;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003818 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003819 core->owner = dev->driver->owner;
3820 core->hw = hw;
Stephen Boyd0214f332019-07-31 12:35:17 -07003821 core->flags = init->flags;
3822 core->num_parents = init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003823 core->min_rate = 0;
3824 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003825 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003826
Stephen Boyd0214f332019-07-31 12:35:17 -07003827 ret = clk_core_populate_parent_map(core, init);
Stephen Boydfc0c2092019-04-12 11:31:47 -07003828 if (ret)
Masahiro Yamada176d1162015-12-28 19:23:00 +09003829 goto fail_parents;
Masahiro Yamada176d1162015-12-28 19:23:00 +09003830
Stephen Boydd6968fc2015-04-30 13:54:13 -07003831 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003832
Stephen Boyd1df40462018-12-11 08:32:04 -08003833 /*
3834 * Don't call clk_hw_create_clk() here because that would pin the
3835 * provider module to itself and prevent it from ever being removed.
3836 */
3837 hw->clk = alloc_clk(core, NULL, NULL);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003838 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003839 ret = PTR_ERR(hw->clk);
Stephen Boydfc0c2092019-04-12 11:31:47 -07003840 goto fail_create_clk;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003841 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003842
Stephen Boyd1df40462018-12-11 08:32:04 -08003843 clk_core_link_consumer(hw->core, hw->clk);
3844
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003845 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003846 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003847 return hw->clk;
3848
Stephen Boyd1df40462018-12-11 08:32:04 -08003849 clk_prepare_lock();
3850 clk_core_unlink_consumer(hw->clk);
3851 clk_prepare_unlock();
3852
3853 free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003854 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003855
Stephen Boydfc0c2092019-04-12 11:31:47 -07003856fail_create_clk:
3857 clk_core_free_parent_map(core);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003858fail_parents:
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003859fail_ops:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003860 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003861fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003862 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003863fail_out:
3864 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003865}
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003866
3867/**
Stephen Boyd9011f922019-12-30 10:29:35 -08003868 * dev_or_parent_of_node() - Get device node of @dev or @dev's parent
3869 * @dev: Device to get device node of
3870 *
3871 * Return: device node pointer of @dev, or the device node pointer of
3872 * @dev->parent if dev doesn't have a device node, or NULL if neither
3873 * @dev or @dev->parent have a device node.
3874 */
3875static struct device_node *dev_or_parent_of_node(struct device *dev)
3876{
3877 struct device_node *np;
3878
3879 if (!dev)
3880 return NULL;
3881
3882 np = dev_of_node(dev);
3883 if (!np)
3884 np = dev_of_node(dev->parent);
3885
3886 return np;
3887}
3888
3889/**
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003890 * clk_register - allocate a new clock, register it and return an opaque cookie
3891 * @dev: device that is registering this clock
3892 * @hw: link to hardware-specific clock data
3893 *
Stephen Boydc1157f62019-05-07 11:46:13 -07003894 * clk_register is the *deprecated* interface for populating the clock tree with
3895 * new clock nodes. Use clk_hw_register() instead.
3896 *
3897 * Returns: a pointer to the newly allocated struct clk which
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003898 * cannot be dereferenced by driver code but may be used in conjunction with the
3899 * rest of the clock API. In the event of an error clk_register will return an
3900 * error code; drivers must test for an error code after calling clk_register.
3901 */
3902struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3903{
Stephen Boyd9011f922019-12-30 10:29:35 -08003904 return __clk_register(dev, dev_or_parent_of_node(dev), hw);
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003905}
Mike Turquetteb24764902012-03-15 23:11:19 -07003906EXPORT_SYMBOL_GPL(clk_register);
3907
Stephen Boyd41438042016-02-05 17:02:52 -08003908/**
3909 * clk_hw_register - register a clk_hw and return an error code
3910 * @dev: device that is registering this clock
3911 * @hw: link to hardware-specific clock data
3912 *
3913 * clk_hw_register is the primary interface for populating the clock tree with
3914 * new clock nodes. It returns an integer equal to zero indicating success or
3915 * less than zero indicating failure. Drivers must test for an error code after
3916 * calling clk_hw_register().
3917 */
3918int clk_hw_register(struct device *dev, struct clk_hw *hw)
3919{
Stephen Boyd9011f922019-12-30 10:29:35 -08003920 return PTR_ERR_OR_ZERO(__clk_register(dev, dev_or_parent_of_node(dev),
3921 hw));
Stephen Boyd41438042016-02-05 17:02:52 -08003922}
3923EXPORT_SYMBOL_GPL(clk_hw_register);
3924
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003925/*
3926 * of_clk_hw_register - register a clk_hw and return an error code
3927 * @node: device_node of device that is registering this clock
3928 * @hw: link to hardware-specific clock data
3929 *
3930 * of_clk_hw_register() is the primary interface for populating the clock tree
3931 * with new clock nodes when a struct device is not available, but a struct
3932 * device_node is. It returns an integer equal to zero indicating success or
3933 * less than zero indicating failure. Drivers must test for an error code after
3934 * calling of_clk_hw_register().
3935 */
3936int of_clk_hw_register(struct device_node *node, struct clk_hw *hw)
3937{
3938 return PTR_ERR_OR_ZERO(__clk_register(NULL, node, hw));
3939}
3940EXPORT_SYMBOL_GPL(of_clk_hw_register);
3941
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003942/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003943static void __clk_release(struct kref *ref)
3944{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003945 struct clk_core *core = container_of(ref, struct clk_core, ref);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003946
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003947 lockdep_assert_held(&prepare_lock);
3948
Stephen Boydfc0c2092019-04-12 11:31:47 -07003949 clk_core_free_parent_map(core);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003950 kfree_const(core->name);
3951 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003952}
3953
3954/*
3955 * Empty clk_ops for unregistered clocks. These are used temporarily
3956 * after clk_unregister() was called on a clock and until last clock
3957 * consumer calls clk_put() and the struct clk object is freed.
3958 */
3959static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3960{
3961 return -ENXIO;
3962}
3963
3964static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3965{
3966 WARN_ON_ONCE(1);
3967}
3968
3969static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3970 unsigned long parent_rate)
3971{
3972 return -ENXIO;
3973}
3974
3975static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3976{
3977 return -ENXIO;
3978}
3979
3980static const struct clk_ops clk_nodrv_ops = {
3981 .enable = clk_nodrv_prepare_enable,
3982 .disable = clk_nodrv_disable_unprepare,
3983 .prepare = clk_nodrv_prepare_enable,
3984 .unprepare = clk_nodrv_disable_unprepare,
3985 .set_rate = clk_nodrv_set_rate,
3986 .set_parent = clk_nodrv_set_parent,
3987};
3988
Stephen Boydbdcf1dc2019-08-28 11:19:59 -07003989static void clk_core_evict_parent_cache_subtree(struct clk_core *root,
3990 struct clk_core *target)
3991{
3992 int i;
3993 struct clk_core *child;
3994
3995 for (i = 0; i < root->num_parents; i++)
3996 if (root->parents[i].core == target)
3997 root->parents[i].core = NULL;
3998
3999 hlist_for_each_entry(child, &root->children, child_node)
4000 clk_core_evict_parent_cache_subtree(child, target);
4001}
4002
4003/* Remove this clk from all parent caches */
4004static void clk_core_evict_parent_cache(struct clk_core *core)
4005{
4006 struct hlist_head **lists;
4007 struct clk_core *root;
4008
4009 lockdep_assert_held(&prepare_lock);
4010
4011 for (lists = all_lists; *lists; lists++)
4012 hlist_for_each_entry(root, *lists, child_node)
4013 clk_core_evict_parent_cache_subtree(root, core);
4014
4015}
4016
Mark Brown1df5c932012-04-18 09:07:12 +01004017/**
4018 * clk_unregister - unregister a currently registered clock
4019 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01004020 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02004021void clk_unregister(struct clk *clk)
4022{
4023 unsigned long flags;
Jerome Brunetf8737442019-09-24 14:39:54 +02004024 const struct clk_ops *ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02004025
Stephen Boyd6314b672014-09-04 23:37:49 -07004026 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
4027 return;
4028
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004029 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02004030
4031 clk_prepare_lock();
4032
Jerome Brunetf8737442019-09-24 14:39:54 +02004033 ops = clk->core->ops;
4034 if (ops == &clk_nodrv_ops) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004035 pr_err("%s: unregistered clock: %s\n", __func__,
4036 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05004037 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02004038 }
4039 /*
4040 * Assign empty clock ops for consumers that might still hold
4041 * a reference to this clock.
4042 */
4043 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004044 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02004045 clk_enable_unlock(flags);
4046
Jerome Brunetf8737442019-09-24 14:39:54 +02004047 if (ops->terminate)
4048 ops->terminate(clk->core->hw);
4049
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004050 if (!hlist_empty(&clk->core->children)) {
4051 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07004052 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02004053
4054 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004055 hlist_for_each_entry_safe(child, t, &clk->core->children,
4056 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01004057 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02004058 }
4059
Stephen Boydbdcf1dc2019-08-28 11:19:59 -07004060 clk_core_evict_parent_cache(clk->core);
4061
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004062 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02004063
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004064 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02004065 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004066 __func__, clk->core->name);
Jerome Brunete55a8392017-12-01 22:51:56 +01004067
4068 if (clk->core->protect_count)
4069 pr_warn("%s: unregistering protected clock: %s\n",
4070 __func__, clk->core->name);
4071
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004072 kref_put(&clk->core->ref, __clk_release);
Kishon Vijay Abraham I82474702019-10-22 12:41:53 +05304073 free_clk(clk);
Insu Yun4106a3d2016-01-30 10:12:04 -05004074unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02004075 clk_prepare_unlock();
4076}
Mark Brown1df5c932012-04-18 09:07:12 +01004077EXPORT_SYMBOL_GPL(clk_unregister);
4078
Stephen Boyd41438042016-02-05 17:02:52 -08004079/**
4080 * clk_hw_unregister - unregister a currently registered clk_hw
4081 * @hw: hardware-specific clock data to unregister
4082 */
4083void clk_hw_unregister(struct clk_hw *hw)
4084{
4085 clk_unregister(hw->clk);
4086}
4087EXPORT_SYMBOL_GPL(clk_hw_unregister);
4088
Jerome Brunete5a4b9b2020-10-21 18:21:45 +02004089static void devm_clk_unregister_cb(struct device *dev, void *res)
Stephen Boyd46c87732012-09-24 13:38:04 -07004090{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07004091 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07004092}
4093
Jerome Brunete5a4b9b2020-10-21 18:21:45 +02004094static void devm_clk_hw_unregister_cb(struct device *dev, void *res)
Stephen Boyd41438042016-02-05 17:02:52 -08004095{
4096 clk_hw_unregister(*(struct clk_hw **)res);
4097}
4098
Stephen Boyd46c87732012-09-24 13:38:04 -07004099/**
4100 * devm_clk_register - resource managed clk_register()
4101 * @dev: device that is registering this clock
4102 * @hw: link to hardware-specific clock data
4103 *
Stephen Boyd9fe9b7a2018-12-11 10:49:40 -08004104 * Managed clk_register(). This function is *deprecated*, use devm_clk_hw_register() instead.
4105 *
4106 * Clocks returned from this function are automatically clk_unregister()ed on
4107 * driver detach. See clk_register() for more information.
Stephen Boyd46c87732012-09-24 13:38:04 -07004108 */
4109struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
4110{
4111 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07004112 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07004113
Jerome Brunete5a4b9b2020-10-21 18:21:45 +02004114 clkp = devres_alloc(devm_clk_unregister_cb, sizeof(*clkp), GFP_KERNEL);
Stephen Boyd293ba3b2014-04-18 16:29:42 -07004115 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07004116 return ERR_PTR(-ENOMEM);
4117
Stephen Boyd293ba3b2014-04-18 16:29:42 -07004118 clk = clk_register(dev, hw);
4119 if (!IS_ERR(clk)) {
4120 *clkp = clk;
4121 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07004122 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07004123 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07004124 }
4125
4126 return clk;
4127}
4128EXPORT_SYMBOL_GPL(devm_clk_register);
4129
Stephen Boyd41438042016-02-05 17:02:52 -08004130/**
4131 * devm_clk_hw_register - resource managed clk_hw_register()
4132 * @dev: device that is registering this clock
4133 * @hw: link to hardware-specific clock data
4134 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09004135 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08004136 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
4137 * for more information.
4138 */
4139int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
4140{
4141 struct clk_hw **hwp;
4142 int ret;
4143
Jerome Brunete5a4b9b2020-10-21 18:21:45 +02004144 hwp = devres_alloc(devm_clk_hw_unregister_cb, sizeof(*hwp), GFP_KERNEL);
Stephen Boyd41438042016-02-05 17:02:52 -08004145 if (!hwp)
4146 return -ENOMEM;
4147
4148 ret = clk_hw_register(dev, hw);
4149 if (!ret) {
4150 *hwp = hw;
4151 devres_add(dev, hwp);
4152 } else {
4153 devres_free(hwp);
4154 }
4155
4156 return ret;
4157}
4158EXPORT_SYMBOL_GPL(devm_clk_hw_register);
4159
Stephen Boyd46c87732012-09-24 13:38:04 -07004160static int devm_clk_match(struct device *dev, void *res, void *data)
4161{
4162 struct clk *c = res;
4163 if (WARN_ON(!c))
4164 return 0;
4165 return c == data;
4166}
4167
Stephen Boyd41438042016-02-05 17:02:52 -08004168static int devm_clk_hw_match(struct device *dev, void *res, void *data)
4169{
4170 struct clk_hw *hw = res;
4171
4172 if (WARN_ON(!hw))
4173 return 0;
4174 return hw == data;
4175}
4176
Stephen Boyd46c87732012-09-24 13:38:04 -07004177/**
4178 * devm_clk_unregister - resource managed clk_unregister()
Stephen Boyd6378cfd2020-06-22 02:09:35 -07004179 * @dev: device that is unregistering the clock data
Stephen Boyd46c87732012-09-24 13:38:04 -07004180 * @clk: clock to unregister
4181 *
4182 * Deallocate a clock allocated with devm_clk_register(). Normally
4183 * this function will not need to be called and the resource management
4184 * code will ensure that the resource is freed.
4185 */
4186void devm_clk_unregister(struct device *dev, struct clk *clk)
4187{
Jerome Brunete5a4b9b2020-10-21 18:21:45 +02004188 WARN_ON(devres_release(dev, devm_clk_unregister_cb, devm_clk_match, clk));
Stephen Boyd46c87732012-09-24 13:38:04 -07004189}
4190EXPORT_SYMBOL_GPL(devm_clk_unregister);
4191
Stephen Boyd41438042016-02-05 17:02:52 -08004192/**
4193 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
4194 * @dev: device that is unregistering the hardware-specific clock data
4195 * @hw: link to hardware-specific clock data
4196 *
4197 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
4198 * this function will not need to be called and the resource management
4199 * code will ensure that the resource is freed.
4200 */
4201void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
4202{
Jerome Brunete5a4b9b2020-10-21 18:21:45 +02004203 WARN_ON(devres_release(dev, devm_clk_hw_unregister_cb, devm_clk_hw_match,
Stephen Boyd41438042016-02-05 17:02:52 -08004204 hw));
4205}
4206EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
4207
Jerome Brunet30d6f8c12020-10-21 18:21:46 +02004208static void devm_clk_release(struct device *dev, void *res)
4209{
4210 clk_put(*(struct clk **)res);
4211}
4212
4213/**
4214 * devm_clk_hw_get_clk - resource managed clk_hw_get_clk()
4215 * @dev: device that is registering this clock
4216 * @hw: clk_hw associated with the clk being consumed
4217 * @con_id: connection ID string on device
4218 *
4219 * Managed clk_hw_get_clk(). Clocks got with this function are
4220 * automatically clk_put() on driver detach. See clk_put()
4221 * for more information.
4222 */
4223struct clk *devm_clk_hw_get_clk(struct device *dev, struct clk_hw *hw,
4224 const char *con_id)
4225{
4226 struct clk *clk;
4227 struct clk **clkp;
4228
4229 /* This should not happen because it would mean we have drivers
4230 * passing around clk_hw pointers instead of having the caller use
4231 * proper clk_get() style APIs
4232 */
4233 WARN_ON_ONCE(dev != hw->core->dev);
4234
4235 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
4236 if (!clkp)
4237 return ERR_PTR(-ENOMEM);
4238
4239 clk = clk_hw_get_clk(hw, con_id);
4240 if (!IS_ERR(clk)) {
4241 *clkp = clk;
4242 devres_add(dev, clkp);
4243 } else {
4244 devres_free(clkp);
4245 }
4246
4247 return clk;
4248}
4249EXPORT_SYMBOL_GPL(devm_clk_hw_get_clk);
4250
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02004251/*
4252 * clkdev helpers
4253 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004254
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02004255void __clk_put(struct clk *clk)
4256{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01004257 struct module *owner;
4258
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01004259 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02004260 return;
4261
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02004262 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01004263
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01004264 /*
4265 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
4266 * given user should be balanced with calls to clk_rate_exclusive_put()
4267 * and by that same consumer
4268 */
4269 if (WARN_ON(clk->exclusive_count)) {
4270 /* We voiced our concern, let's sanitize the situation */
4271 clk->core->protect_count -= (clk->exclusive_count - 1);
4272 clk_core_rate_unprotect(clk->core);
4273 clk->exclusive_count = 0;
4274 }
4275
Stephen Boyd50595f82015-02-06 11:42:44 -08004276 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01004277 if (clk->min_rate > clk->core->req_rate ||
4278 clk->max_rate < clk->core->req_rate)
4279 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
4280
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01004281 owner = clk->core->owner;
4282 kref_put(&clk->core->ref, __clk_release);
4283
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02004284 clk_prepare_unlock();
4285
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01004286 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01004287
Stephen Boyd1df40462018-12-11 08:32:04 -08004288 free_clk(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02004289}
4290
Mike Turquetteb24764902012-03-15 23:11:19 -07004291/*** clk rate change notifiers ***/
4292
4293/**
4294 * clk_notifier_register - add a clk rate change notifier
4295 * @clk: struct clk * to watch
4296 * @nb: struct notifier_block * with callback info
4297 *
4298 * Request notification when clk's rate changes. This uses an SRCU
4299 * notifier because we want it to block and notifier unregistrations are
4300 * uncommon. The callbacks associated with the notifier must not
4301 * re-enter into the clk framework by calling any top-level clk APIs;
4302 * this will cause a nested prepare_lock mutex.
4303 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09004304 * In all notification cases (pre, post and abort rate change) the original
4305 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
4306 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07004307 *
Mike Turquetteb24764902012-03-15 23:11:19 -07004308 * clk_notifier_register() must be called from non-atomic context.
4309 * Returns -EINVAL if called with null arguments, -ENOMEM upon
4310 * allocation failure; otherwise, passes along the return value of
4311 * srcu_notifier_chain_register().
4312 */
4313int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
4314{
4315 struct clk_notifier *cn;
4316 int ret = -ENOMEM;
4317
4318 if (!clk || !nb)
4319 return -EINVAL;
4320
Mike Turquetteeab89f62013-03-28 13:59:01 -07004321 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004322
4323 /* search the list of notifiers for this clk */
4324 list_for_each_entry(cn, &clk_notifier_list, node)
4325 if (cn->clk == clk)
4326 break;
4327
4328 /* if clk wasn't in the notifier list, allocate new clk_notifier */
4329 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02004330 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07004331 if (!cn)
4332 goto out;
4333
4334 cn->clk = clk;
4335 srcu_init_notifier_head(&cn->notifier_head);
4336
4337 list_add(&cn->node, &clk_notifier_list);
4338 }
4339
4340 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
4341
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004342 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07004343
4344out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07004345 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004346
4347 return ret;
4348}
4349EXPORT_SYMBOL_GPL(clk_notifier_register);
4350
4351/**
4352 * clk_notifier_unregister - remove a clk rate change notifier
4353 * @clk: struct clk *
4354 * @nb: struct notifier_block * with callback info
4355 *
4356 * Request no further notification for changes to 'clk' and frees memory
4357 * allocated in clk_notifier_register.
4358 *
4359 * Returns -EINVAL if called with null arguments; otherwise, passes
4360 * along the return value of srcu_notifier_chain_unregister().
4361 */
4362int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
4363{
4364 struct clk_notifier *cn = NULL;
4365 int ret = -EINVAL;
4366
4367 if (!clk || !nb)
4368 return -EINVAL;
4369
Mike Turquetteeab89f62013-03-28 13:59:01 -07004370 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004371
4372 list_for_each_entry(cn, &clk_notifier_list, node)
4373 if (cn->clk == clk)
4374 break;
4375
4376 if (cn->clk == clk) {
4377 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
4378
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004379 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07004380
4381 /* XXX the notifier code should handle this better */
4382 if (!cn->notifier_head.head) {
4383 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08004384 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07004385 kfree(cn);
4386 }
4387
4388 } else {
4389 ret = -ENOENT;
4390 }
4391
Mike Turquetteeab89f62013-03-28 13:59:01 -07004392 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004393
4394 return ret;
4395}
4396EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05004397
4398#ifdef CONFIG_OF
Olof Johanssonc7712562019-12-18 09:56:21 -08004399static void clk_core_reparent_orphans(void)
4400{
4401 clk_prepare_lock();
4402 clk_core_reparent_orphans_nolock();
4403 clk_prepare_unlock();
4404}
4405
Grant Likely766e6a42012-04-09 14:50:06 -05004406/**
4407 * struct of_clk_provider - Clock provider registration structure
4408 * @link: Entry in global list of clock providers
4409 * @node: Pointer to device tree node of clock provider
4410 * @get: Get clock callback. Returns NULL or a struct clk for the
4411 * given clock specifier
Stephen Boyd6378cfd2020-06-22 02:09:35 -07004412 * @get_hw: Get clk_hw callback. Returns NULL, ERR_PTR or a
4413 * struct clk_hw for the given clock specifier
Grant Likely766e6a42012-04-09 14:50:06 -05004414 * @data: context pointer to be passed into @get callback
4415 */
4416struct of_clk_provider {
4417 struct list_head link;
4418
4419 struct device_node *node;
4420 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004421 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05004422 void *data;
4423};
4424
Stephen Boyd30d5a942019-05-23 17:11:57 -07004425extern struct of_device_id __clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304426static const struct of_device_id __clk_of_table_sentinel
Joe Perches33def842020-10-21 19:36:07 -07004427 __used __section("__clk_of_table_end");
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304428
Grant Likely766e6a42012-04-09 14:50:06 -05004429static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004430static DEFINE_MUTEX(of_clk_mutex);
4431
Grant Likely766e6a42012-04-09 14:50:06 -05004432struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
4433 void *data)
4434{
4435 return data;
4436}
4437EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
4438
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004439struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
4440{
4441 return data;
4442}
4443EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
4444
Shawn Guo494bfec2012-08-22 21:36:27 +08004445struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
4446{
4447 struct clk_onecell_data *clk_data = data;
4448 unsigned int idx = clkspec->args[0];
4449
4450 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02004451 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08004452 return ERR_PTR(-EINVAL);
4453 }
4454
4455 return clk_data->clks[idx];
4456}
4457EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
4458
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004459struct clk_hw *
4460of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
4461{
4462 struct clk_hw_onecell_data *hw_data = data;
4463 unsigned int idx = clkspec->args[0];
4464
4465 if (idx >= hw_data->num) {
4466 pr_err("%s: invalid index %u\n", __func__, idx);
4467 return ERR_PTR(-EINVAL);
4468 }
4469
4470 return hw_data->hws[idx];
4471}
4472EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
4473
Grant Likely766e6a42012-04-09 14:50:06 -05004474/**
4475 * of_clk_add_provider() - Register a clock provider for a node
4476 * @np: Device node pointer associated with clock provider
4477 * @clk_src_get: callback for decoding clock
4478 * @data: context pointer for @clk_src_get callback.
Stephen Boyd9fe9b7a2018-12-11 10:49:40 -08004479 *
4480 * This function is *deprecated*. Use of_clk_add_hw_provider() instead.
Grant Likely766e6a42012-04-09 14:50:06 -05004481 */
4482int of_clk_add_provider(struct device_node *np,
4483 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
4484 void *data),
4485 void *data)
4486{
4487 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004488 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004489
Markus Elfring1808a322017-04-20 09:30:52 +02004490 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05004491 if (!cp)
4492 return -ENOMEM;
4493
4494 cp->node = of_node_get(np);
4495 cp->data = data;
4496 cp->get = clk_src_get;
4497
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004498 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004499 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004500 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05004501 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05004502
Jerome Brunet66d95062019-12-03 09:08:05 +01004503 clk_core_reparent_orphans();
4504
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004505 ret = of_clk_set_defaults(np, true);
4506 if (ret < 0)
4507 of_clk_del_provider(np);
4508
4509 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004510}
4511EXPORT_SYMBOL_GPL(of_clk_add_provider);
4512
4513/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004514 * of_clk_add_hw_provider() - Register a clock provider for a node
4515 * @np: Device node pointer associated with clock provider
4516 * @get: callback for decoding clk_hw
4517 * @data: context pointer for @get callback.
4518 */
4519int of_clk_add_hw_provider(struct device_node *np,
4520 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4521 void *data),
4522 void *data)
4523{
4524 struct of_clk_provider *cp;
4525 int ret;
4526
4527 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4528 if (!cp)
4529 return -ENOMEM;
4530
4531 cp->node = of_node_get(np);
4532 cp->data = data;
4533 cp->get_hw = get;
4534
4535 mutex_lock(&of_clk_mutex);
4536 list_add(&cp->link, &of_clk_providers);
4537 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05004538 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004539
Jerome Brunet66d95062019-12-03 09:08:05 +01004540 clk_core_reparent_orphans();
4541
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004542 ret = of_clk_set_defaults(np, true);
4543 if (ret < 0)
4544 of_clk_del_provider(np);
4545
4546 return ret;
4547}
4548EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
4549
Stephen Boydaa795c42017-09-01 16:16:40 -07004550static void devm_of_clk_release_provider(struct device *dev, void *res)
4551{
4552 of_clk_del_provider(*(struct device_node **)res);
4553}
4554
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004555/*
4556 * We allow a child device to use its parent device as the clock provider node
4557 * for cases like MFD sub-devices where the child device driver wants to use
4558 * devm_*() APIs but not list the device in DT as a sub-node.
4559 */
4560static struct device_node *get_clk_provider_node(struct device *dev)
4561{
4562 struct device_node *np, *parent_np;
4563
4564 np = dev->of_node;
4565 parent_np = dev->parent ? dev->parent->of_node : NULL;
4566
4567 if (!of_find_property(np, "#clock-cells", NULL))
4568 if (of_find_property(parent_np, "#clock-cells", NULL))
4569 np = parent_np;
4570
4571 return np;
4572}
4573
Matti Vaittinene45838b2018-12-04 13:33:48 +02004574/**
4575 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4576 * @dev: Device acting as the clock provider (used for DT node and lifetime)
4577 * @get: callback for decoding clk_hw
4578 * @data: context pointer for @get callback
4579 *
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004580 * Registers clock provider for given device's node. If the device has no DT
4581 * node or if the device node lacks of clock provider information (#clock-cells)
4582 * then the parent device's node is scanned for this information. If parent node
4583 * has the #clock-cells then it is used in registration. Provider is
4584 * automatically released at device exit.
Matti Vaittinene45838b2018-12-04 13:33:48 +02004585 *
4586 * Return: 0 on success or an errno on failure.
4587 */
Stephen Boydaa795c42017-09-01 16:16:40 -07004588int devm_of_clk_add_hw_provider(struct device *dev,
4589 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4590 void *data),
4591 void *data)
4592{
4593 struct device_node **ptr, *np;
4594 int ret;
4595
4596 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
4597 GFP_KERNEL);
4598 if (!ptr)
4599 return -ENOMEM;
4600
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004601 np = get_clk_provider_node(dev);
Stephen Boydaa795c42017-09-01 16:16:40 -07004602 ret = of_clk_add_hw_provider(np, get, data);
4603 if (!ret) {
4604 *ptr = np;
4605 devres_add(dev, ptr);
4606 } else {
4607 devres_free(ptr);
4608 }
4609
4610 return ret;
4611}
4612EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
4613
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004614/**
Grant Likely766e6a42012-04-09 14:50:06 -05004615 * of_clk_del_provider() - Remove a previously registered clock provider
4616 * @np: Device node pointer associated with clock provider
4617 */
4618void of_clk_del_provider(struct device_node *np)
4619{
4620 struct of_clk_provider *cp;
4621
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004622 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004623 list_for_each_entry(cp, &of_clk_providers, link) {
4624 if (cp->node == np) {
4625 list_del(&cp->link);
4626 of_node_put(cp->node);
4627 kfree(cp);
4628 break;
4629 }
4630 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004631 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004632}
4633EXPORT_SYMBOL_GPL(of_clk_del_provider);
4634
Stephen Boydaa795c42017-09-01 16:16:40 -07004635static int devm_clk_provider_match(struct device *dev, void *res, void *data)
4636{
4637 struct device_node **np = res;
4638
4639 if (WARN_ON(!np || !*np))
4640 return 0;
4641
4642 return *np == data;
4643}
4644
Matti Vaittinene45838b2018-12-04 13:33:48 +02004645/**
4646 * devm_of_clk_del_provider() - Remove clock provider registered using devm
4647 * @dev: Device to whose lifetime the clock provider was bound
4648 */
Stephen Boydaa795c42017-09-01 16:16:40 -07004649void devm_of_clk_del_provider(struct device *dev)
4650{
4651 int ret;
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004652 struct device_node *np = get_clk_provider_node(dev);
Stephen Boydaa795c42017-09-01 16:16:40 -07004653
4654 ret = devres_release(dev, devm_of_clk_release_provider,
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004655 devm_clk_provider_match, np);
Stephen Boydaa795c42017-09-01 16:16:40 -07004656
4657 WARN_ON(ret);
4658}
4659EXPORT_SYMBOL(devm_of_clk_del_provider);
4660
Stephen Boyd226fd702019-08-26 14:20:42 -07004661/**
4662 * of_parse_clkspec() - Parse a DT clock specifier for a given device node
4663 * @np: device node to parse clock specifier from
4664 * @index: index of phandle to parse clock out of. If index < 0, @name is used
4665 * @name: clock name to find and parse. If name is NULL, the index is used
4666 * @out_args: Result of parsing the clock specifier
4667 *
4668 * Parses a device node's "clocks" and "clock-names" properties to find the
4669 * phandle and cells for the index or name that is desired. The resulting clock
4670 * specifier is placed into @out_args, or an errno is returned when there's a
4671 * parsing error. The @index argument is ignored if @name is non-NULL.
4672 *
4673 * Example:
4674 *
4675 * phandle1: clock-controller@1 {
4676 * #clock-cells = <2>;
4677 * }
4678 *
4679 * phandle2: clock-controller@2 {
4680 * #clock-cells = <1>;
4681 * }
4682 *
4683 * clock-consumer@3 {
4684 * clocks = <&phandle1 1 2 &phandle2 3>;
4685 * clock-names = "name1", "name2";
4686 * }
4687 *
4688 * To get a device_node for `clock-controller@2' node you may call this
4689 * function a few different ways:
4690 *
4691 * of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
4692 * of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
4693 * of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
4694 *
4695 * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
4696 * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
4697 * the "clock-names" property of @np.
Stephen Boyd5dc7e842019-03-08 10:35:01 -08004698 */
Stephen Boydcf13f282018-12-19 15:09:14 -08004699static int of_parse_clkspec(const struct device_node *np, int index,
4700 const char *name, struct of_phandle_args *out_args)
Stephen Boyd44722872018-12-19 10:59:55 -08004701{
4702 int ret = -ENOENT;
4703
4704 /* Walk up the tree of devices looking for a clock property that matches */
4705 while (np) {
4706 /*
4707 * For named clocks, first look up the name in the
4708 * "clock-names" property. If it cannot be found, then index
4709 * will be an error code and of_parse_phandle_with_args() will
4710 * return -EINVAL.
4711 */
4712 if (name)
4713 index = of_property_match_string(np, "clock-names", name);
4714 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
4715 index, out_args);
4716 if (!ret)
4717 break;
4718 if (name && index >= 0)
4719 break;
4720
4721 /*
4722 * No matching clock found on this node. If the parent node
4723 * has a "clock-ranges" property, then we can try one of its
4724 * clocks.
4725 */
4726 np = np->parent;
4727 if (np && !of_get_property(np, "clock-ranges", NULL))
4728 break;
4729 index = 0;
4730 }
4731
4732 return ret;
4733}
4734
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004735static struct clk_hw *
4736__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4737 struct of_phandle_args *clkspec)
4738{
4739 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004740
Stephen Boyd74002fc2016-08-25 13:35:36 -07004741 if (provider->get_hw)
4742 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004743
Stephen Boyd74002fc2016-08-25 13:35:36 -07004744 clk = provider->get(clkspec, provider->data);
4745 if (IS_ERR(clk))
4746 return ERR_CAST(clk);
4747 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004748}
4749
Stephen Boydcf13f282018-12-19 15:09:14 -08004750static struct clk_hw *
4751of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
Grant Likely766e6a42012-04-09 14:50:06 -05004752{
4753 struct of_clk_provider *provider;
Stephen Boyd1df40462018-12-11 08:32:04 -08004754 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05004755
Stephen Boyd306c3422015-02-05 15:39:11 -08004756 if (!clkspec)
4757 return ERR_PTR(-EINVAL);
4758
Stephen Boyd306c3422015-02-05 15:39:11 -08004759 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004760 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07004761 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004762 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd1df40462018-12-11 08:32:04 -08004763 if (!IS_ERR(hw))
4764 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08004765 }
Grant Likely766e6a42012-04-09 14:50:06 -05004766 }
Stephen Boyd306c3422015-02-05 15:39:11 -08004767 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004768
Stephen Boyd44722872018-12-19 10:59:55 -08004769 return hw;
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004770}
4771
Stephen Boyd306c3422015-02-05 15:39:11 -08004772/**
4773 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4774 * @clkspec: pointer to a clock specifier data structure
4775 *
4776 * This function looks up a struct clk from the registered list of clock
4777 * providers, an input is a clock specifier data structure as returned
4778 * from the of_parse_phandle_with_args() function call.
4779 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004780struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4781{
Stephen Boyd44722872018-12-19 10:59:55 -08004782 struct clk_hw *hw = of_clk_get_hw_from_clkspec(clkspec);
4783
Stephen Boydefa85042018-12-11 08:34:16 -08004784 return clk_hw_create_clk(NULL, hw, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05004785}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06004786EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05004787
Stephen Boydcf13f282018-12-19 15:09:14 -08004788struct clk_hw *of_clk_get_hw(struct device_node *np, int index,
4789 const char *con_id)
4790{
4791 int ret;
4792 struct clk_hw *hw;
4793 struct of_phandle_args clkspec;
4794
4795 ret = of_parse_clkspec(np, index, con_id, &clkspec);
4796 if (ret)
4797 return ERR_PTR(ret);
4798
4799 hw = of_clk_get_hw_from_clkspec(&clkspec);
4800 of_node_put(clkspec.np);
4801
4802 return hw;
4803}
4804
4805static struct clk *__of_clk_get(struct device_node *np,
4806 int index, const char *dev_id,
4807 const char *con_id)
4808{
4809 struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
4810
4811 return clk_hw_create_clk(NULL, hw, dev_id, con_id);
4812}
4813
4814struct clk *of_clk_get(struct device_node *np, int index)
4815{
4816 return __of_clk_get(np, index, np->full_name, NULL);
4817}
4818EXPORT_SYMBOL(of_clk_get);
4819
4820/**
4821 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
4822 * @np: pointer to clock consumer node
4823 * @name: name of consumer's clock input, or NULL for the first clock reference
4824 *
4825 * This function parses the clocks and clock-names properties,
4826 * and uses them to look up the struct clk from the registered list of clock
4827 * providers.
4828 */
4829struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
4830{
4831 if (!np)
4832 return ERR_PTR(-ENOENT);
4833
Kuninori Morimoto65cf20a2019-03-06 16:18:28 +09004834 return __of_clk_get(np, 0, np->full_name, name);
Stephen Boydcf13f282018-12-19 15:09:14 -08004835}
4836EXPORT_SYMBOL(of_clk_get_by_name);
4837
Stephen Boyd929e7f32016-02-19 15:52:32 -08004838/**
4839 * of_clk_get_parent_count() - Count the number of clocks a device node has
4840 * @np: device node to count
4841 *
4842 * Returns: The number of clocks that are possible parents of this node
4843 */
Geert Uytterhoeven4a4472f2020-02-12 10:43:17 +01004844unsigned int of_clk_get_parent_count(const struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07004845{
Stephen Boyd929e7f32016-02-19 15:52:32 -08004846 int count;
4847
4848 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4849 if (count < 0)
4850 return 0;
4851
4852 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07004853}
4854EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4855
Geert Uytterhoeven4a4472f2020-02-12 10:43:17 +01004856const char *of_clk_get_parent_name(const struct device_node *np, int index)
Grant Likely766e6a42012-04-09 14:50:06 -05004857{
4858 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004859 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05004860 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004861 const __be32 *vp;
4862 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05004863 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004864 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004865 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05004866
Grant Likely766e6a42012-04-09 14:50:06 -05004867 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4868 &clkspec);
4869 if (rc)
4870 return NULL;
4871
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004872 index = clkspec.args_count ? clkspec.args[0] : 0;
4873 count = 0;
4874
4875 /* if there is an indices property, use it to transfer the index
4876 * specified into an array offset for the clock-output-names property.
4877 */
4878 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4879 if (index == pv) {
4880 index = count;
4881 break;
4882 }
4883 count++;
4884 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09004885 /* We went off the end of 'clock-indices' without finding it */
4886 if (prop && !vp)
4887 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004888
Grant Likely766e6a42012-04-09 14:50:06 -05004889 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004890 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004891 &clk_name) < 0) {
4892 /*
4893 * Best effort to get the name if the clock has been
4894 * registered with the framework. If the clock isn't
4895 * registered, we return the node name as the name of
4896 * the clock as long as #clock-cells = 0.
4897 */
4898 clk = of_clk_get_from_provider(&clkspec);
4899 if (IS_ERR(clk)) {
4900 if (clkspec.args_count == 0)
4901 clk_name = clkspec.np->name;
4902 else
4903 clk_name = NULL;
4904 } else {
4905 clk_name = __clk_get_name(clk);
4906 clk_put(clk);
4907 }
4908 }
4909
Grant Likely766e6a42012-04-09 14:50:06 -05004910
4911 of_node_put(clkspec.np);
4912 return clk_name;
4913}
4914EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4915
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05004916/**
4917 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4918 * number of parents
4919 * @np: Device node pointer associated with clock provider
4920 * @parents: pointer to char array that hold the parents' names
4921 * @size: size of the @parents array
4922 *
4923 * Return: number of parents for the clock node.
4924 */
4925int of_clk_parent_fill(struct device_node *np, const char **parents,
4926 unsigned int size)
4927{
4928 unsigned int i = 0;
4929
4930 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4931 i++;
4932
4933 return i;
4934}
4935EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4936
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004937struct clock_provider {
Geert Uytterhoevena59704332018-04-10 15:06:05 +02004938 void (*clk_init_cb)(struct device_node *);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004939 struct device_node *np;
4940 struct list_head node;
4941};
4942
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004943/*
4944 * This function looks for a parent clock. If there is one, then it
4945 * checks that the provider for this parent clock was initialized, in
4946 * this case the parent clock will be ready.
4947 */
4948static int parent_ready(struct device_node *np)
4949{
4950 int i = 0;
4951
4952 while (true) {
4953 struct clk *clk = of_clk_get(np, i);
4954
4955 /* this parent is ready we can check the next one */
4956 if (!IS_ERR(clk)) {
4957 clk_put(clk);
4958 i++;
4959 continue;
4960 }
4961
4962 /* at least one parent is not ready, we exit now */
4963 if (PTR_ERR(clk) == -EPROBE_DEFER)
4964 return 0;
4965
4966 /*
4967 * Here we make assumption that the device tree is
4968 * written correctly. So an error means that there is
4969 * no more parent. As we didn't exit yet, then the
4970 * previous parent are ready. If there is no clock
4971 * parent, no need to wait for them, then we can
4972 * consider their absence as being ready
4973 */
4974 return 1;
4975 }
4976}
4977
Grant Likely766e6a42012-04-09 14:50:06 -05004978/**
Lee Jonesd56f8992016-02-11 13:19:11 -08004979 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4980 * @np: Device node pointer associated with clock provider
4981 * @index: clock index
Geert Uytterhoevenf7ae7502018-01-03 12:06:14 +01004982 * @flags: pointer to top-level framework flags
Lee Jonesd56f8992016-02-11 13:19:11 -08004983 *
4984 * Detects if the clock-critical property exists and, if so, sets the
4985 * corresponding CLK_IS_CRITICAL flag.
4986 *
4987 * Do not use this function. It exists only for legacy Device Tree
4988 * bindings, such as the one-clock-per-node style that are outdated.
4989 * Those bindings typically put all clock data into .dts and the Linux
4990 * driver has no clock data, thus making it impossible to set this flag
4991 * correctly from the driver. Only those drivers may call
4992 * of_clk_detect_critical from their setup functions.
4993 *
4994 * Return: error code or zero on success
4995 */
Geert Uytterhoevenbe545c72019-12-06 14:34:14 +01004996int of_clk_detect_critical(struct device_node *np, int index,
4997 unsigned long *flags)
Lee Jonesd56f8992016-02-11 13:19:11 -08004998{
4999 struct property *prop;
5000 const __be32 *cur;
5001 uint32_t idx;
5002
5003 if (!np || !flags)
5004 return -EINVAL;
5005
5006 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
5007 if (index == idx)
5008 *flags |= CLK_IS_CRITICAL;
5009
5010 return 0;
5011}
5012
5013/**
Grant Likely766e6a42012-04-09 14:50:06 -05005014 * of_clk_init() - Scan and init clock providers from the DT
5015 * @matches: array of compatible values and init functions for providers.
5016 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01005017 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01005018 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01005019 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05005020 */
5021void __init of_clk_init(const struct of_device_id *matches)
5022{
Alex Elder7f7ed582013-08-22 11:31:31 -05005023 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05005024 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01005025 struct clock_provider *clk_provider, *next;
5026 bool is_init_done;
5027 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07005028 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05005029
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05305030 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03005031 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05305032
Gregory CLEMENT1771b102014-02-24 19:10:13 +01005033 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05005034 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07005035 struct clock_provider *parent;
5036
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01005037 if (!of_device_is_available(np))
5038 continue;
5039
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07005040 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
5041 if (!parent) {
5042 list_for_each_entry_safe(clk_provider, next,
5043 &clk_provider_list, node) {
5044 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02005045 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07005046 kfree(clk_provider);
5047 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02005048 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07005049 return;
5050 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01005051
5052 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02005053 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01005054 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01005055 }
5056
5057 while (!list_empty(&clk_provider_list)) {
5058 is_init_done = false;
5059 list_for_each_entry_safe(clk_provider, next,
5060 &clk_provider_list, node) {
5061 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02005062
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02005063 /* Don't populate platform devices */
5064 of_node_set_flag(clk_provider->np,
5065 OF_POPULATED);
5066
Gregory CLEMENT1771b102014-02-24 19:10:13 +01005067 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02005068 of_clk_set_defaults(clk_provider->np, true);
5069
Gregory CLEMENT1771b102014-02-24 19:10:13 +01005070 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02005071 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01005072 kfree(clk_provider);
5073 is_init_done = true;
5074 }
5075 }
5076
5077 /*
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01005078 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01005079 * remaining providers during the last loop, so now we
5080 * initialize all the remaining ones unconditionally
5081 * in case the clock parent was not mandatory
5082 */
5083 if (!is_init_done)
5084 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05005085 }
5086}
5087#endif