blob: a7cee0fac071a6a2fced846ac216ee2c450f465a [file] [log] [blame]
Stephen Boydebafb632018-12-11 09:43:03 -08001// SPDX-License-Identifier: GPL-2.0
Mike Turquetteb24764902012-03-15 23:11:19 -07002/*
3 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
4 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
5 *
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03006 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
Mike Turquetteb24764902012-03-15 23:11:19 -07007 */
8
Stephen Boyd3c373112015-06-19 15:00:46 -07009#include <linux/clk.h>
Michael Turquetteb09d6d92015-01-29 14:22:50 -080010#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020011#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070012#include <linux/module.h>
13#include <linux/mutex.h>
14#include <linux/spinlock.h>
15#include <linux/err.h>
16#include <linux/list.h>
17#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050018#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070019#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053020#include <linux/init.h>
Marek Szyprowski9a34b452017-08-21 10:04:59 +020021#include <linux/pm_runtime.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070022#include <linux/sched.h>
Stephen Boyd562ef0b2015-05-01 12:16:14 -070023#include <linux/clkdev.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070024
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020025#include "clk.h"
26
Mike Turquetteb24764902012-03-15 23:11:19 -070027static DEFINE_SPINLOCK(enable_lock);
28static DEFINE_MUTEX(prepare_lock);
29
Mike Turquette533ddeb2013-03-28 13:59:02 -070030static struct task_struct *prepare_owner;
31static struct task_struct *enable_owner;
32
33static int prepare_refcnt;
34static int enable_refcnt;
35
Mike Turquetteb24764902012-03-15 23:11:19 -070036static HLIST_HEAD(clk_root_list);
37static HLIST_HEAD(clk_orphan_list);
38static LIST_HEAD(clk_notifier_list);
39
Michael Turquetteb09d6d92015-01-29 14:22:50 -080040/*** private data structures ***/
41
Stephen Boydfc0c2092019-04-12 11:31:47 -070042struct clk_parent_map {
43 const struct clk_hw *hw;
44 struct clk_core *core;
45 const char *fw_name;
46 const char *name;
Stephen Boyd601b6e92019-04-12 11:31:49 -070047 int index;
Stephen Boydfc0c2092019-04-12 11:31:47 -070048};
49
Michael Turquetteb09d6d92015-01-29 14:22:50 -080050struct clk_core {
51 const char *name;
52 const struct clk_ops *ops;
53 struct clk_hw *hw;
54 struct module *owner;
Marek Szyprowski9a34b452017-08-21 10:04:59 +020055 struct device *dev;
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -070056 struct device_node *of_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080057 struct clk_core *parent;
Stephen Boydfc0c2092019-04-12 11:31:47 -070058 struct clk_parent_map *parents;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080059 u8 num_parents;
60 u8 new_parent_index;
61 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010062 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080063 unsigned long new_rate;
64 struct clk_core *new_parent;
65 struct clk_core *new_child;
66 unsigned long flags;
Heiko Stuebnere6500342015-04-22 22:53:05 +020067 bool orphan;
Miquel Raynal24478832018-12-04 20:24:37 +010068 bool rpm_enabled;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080069 unsigned int enable_count;
70 unsigned int prepare_count;
Jerome Brunete55a8392017-12-01 22:51:56 +010071 unsigned int protect_count;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070072 unsigned long min_rate;
73 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080074 unsigned long accuracy;
75 int phase;
Jerome Brunet9fba7382018-06-19 16:41:41 +020076 struct clk_duty duty;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080077 struct hlist_head children;
78 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010079 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080080 unsigned int notifier_count;
81#ifdef CONFIG_DEBUG_FS
82 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020083 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080084#endif
85 struct kref ref;
86};
87
Stephen Boyddfc202e2015-02-02 14:37:41 -080088#define CREATE_TRACE_POINTS
89#include <trace/events/clk.h>
90
Michael Turquetteb09d6d92015-01-29 14:22:50 -080091struct clk {
92 struct clk_core *core;
Stephen Boydefa85042018-12-11 08:34:16 -080093 struct device *dev;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080094 const char *dev_id;
95 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010096 unsigned long min_rate;
97 unsigned long max_rate;
Jerome Brunet55e9b8b2017-12-01 22:51:59 +010098 unsigned int exclusive_count;
Stephen Boyd50595f82015-02-06 11:42:44 -080099 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -0800100};
101
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200102/*** runtime pm ***/
103static int clk_pm_runtime_get(struct clk_core *core)
104{
Miquel Raynal24478832018-12-04 20:24:37 +0100105 int ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200106
Miquel Raynal24478832018-12-04 20:24:37 +0100107 if (!core->rpm_enabled)
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200108 return 0;
109
110 ret = pm_runtime_get_sync(core->dev);
111 return ret < 0 ? ret : 0;
112}
113
114static void clk_pm_runtime_put(struct clk_core *core)
115{
Miquel Raynal24478832018-12-04 20:24:37 +0100116 if (!core->rpm_enabled)
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200117 return;
118
119 pm_runtime_put_sync(core->dev);
120}
121
Mike Turquetteeab89f62013-03-28 13:59:01 -0700122/*** locking ***/
123static void clk_prepare_lock(void)
124{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700125 if (!mutex_trylock(&prepare_lock)) {
126 if (prepare_owner == current) {
127 prepare_refcnt++;
128 return;
129 }
130 mutex_lock(&prepare_lock);
131 }
132 WARN_ON_ONCE(prepare_owner != NULL);
133 WARN_ON_ONCE(prepare_refcnt != 0);
134 prepare_owner = current;
135 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700136}
137
138static void clk_prepare_unlock(void)
139{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700140 WARN_ON_ONCE(prepare_owner != current);
141 WARN_ON_ONCE(prepare_refcnt == 0);
142
143 if (--prepare_refcnt)
144 return;
145 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700146 mutex_unlock(&prepare_lock);
147}
148
149static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700150 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700151{
152 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700153
David Lechnera12aa8a2018-01-04 19:46:08 -0600154 /*
155 * On UP systems, spin_trylock_irqsave() always returns true, even if
156 * we already hold the lock. So, in that case, we rely only on
157 * reference counting.
158 */
159 if (!IS_ENABLED(CONFIG_SMP) ||
160 !spin_trylock_irqsave(&enable_lock, flags)) {
Mike Turquette533ddeb2013-03-28 13:59:02 -0700161 if (enable_owner == current) {
162 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700163 __acquire(enable_lock);
David Lechnera12aa8a2018-01-04 19:46:08 -0600164 if (!IS_ENABLED(CONFIG_SMP))
165 local_save_flags(flags);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700166 return flags;
167 }
168 spin_lock_irqsave(&enable_lock, flags);
169 }
170 WARN_ON_ONCE(enable_owner != NULL);
171 WARN_ON_ONCE(enable_refcnt != 0);
172 enable_owner = current;
173 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700174 return flags;
175}
176
177static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700178 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700179{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700180 WARN_ON_ONCE(enable_owner != current);
181 WARN_ON_ONCE(enable_refcnt == 0);
182
Stephen Boyda57aa182015-07-24 12:24:48 -0700183 if (--enable_refcnt) {
184 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700185 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700186 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700187 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700188 spin_unlock_irqrestore(&enable_lock, flags);
189}
190
Jerome Brunete55a8392017-12-01 22:51:56 +0100191static bool clk_core_rate_is_protected(struct clk_core *core)
192{
193 return core->protect_count;
194}
195
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700196static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530197{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200198 bool ret = false;
199
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700200 /*
201 * .is_prepared is optional for clocks that can prepare
202 * fall back to software usage counter if it is missing
203 */
204 if (!core->ops->is_prepared)
205 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530206
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200207 if (!clk_pm_runtime_get(core)) {
208 ret = core->ops->is_prepared(core->hw);
209 clk_pm_runtime_put(core);
210 }
211
212 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530213}
214
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700215static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530216{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200217 bool ret = false;
218
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700219 /*
220 * .is_enabled is only mandatory for clocks that gate
221 * fall back to software usage counter if .is_enabled is missing
222 */
223 if (!core->ops->is_enabled)
224 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530225
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200226 /*
227 * Check if clock controller's device is runtime active before
228 * calling .is_enabled callback. If not, assume that clock is
229 * disabled, because we might be called from atomic context, from
230 * which pm_runtime_get() is not allowed.
231 * This function is called mainly from clk_disable_unused_subtree,
232 * which ensures proper runtime pm activation of controller before
233 * taking enable spinlock, but the below check is needed if one tries
234 * to call it from other places.
235 */
Miquel Raynal24478832018-12-04 20:24:37 +0100236 if (core->rpm_enabled) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200237 pm_runtime_get_noresume(core->dev);
238 if (!pm_runtime_active(core->dev)) {
239 ret = false;
240 goto done;
241 }
242 }
243
244 ret = core->ops->is_enabled(core->hw);
245done:
Miquel Raynal24478832018-12-04 20:24:37 +0100246 if (core->rpm_enabled)
Dong Aisheng756efe12017-12-22 17:46:04 +0800247 pm_runtime_put(core->dev);
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200248
249 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530250}
251
Mike Turquetteb24764902012-03-15 23:11:19 -0700252/*** helper functions ***/
253
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200254const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700255{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100256 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700257}
Niels de Vos48950842012-12-13 13:12:25 +0100258EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700259
Stephen Boyde7df6f62015-08-12 13:04:56 -0700260const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700261{
262 return hw->core->name;
263}
264EXPORT_SYMBOL_GPL(clk_hw_get_name);
265
Russ Dill65800b22012-11-26 11:20:09 -0800266struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700267{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100268 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700269}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800270EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700271
Stephen Boyde7df6f62015-08-12 13:04:56 -0700272unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700273{
274 return hw->core->num_parents;
275}
276EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
277
Stephen Boyde7df6f62015-08-12 13:04:56 -0700278struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700279{
280 return hw->core->parent ? hw->core->parent->hw : NULL;
281}
282EXPORT_SYMBOL_GPL(clk_hw_get_parent);
283
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700284static struct clk_core *__clk_lookup_subtree(const char *name,
285 struct clk_core *core)
286{
287 struct clk_core *child;
288 struct clk_core *ret;
289
290 if (!strcmp(core->name, name))
291 return core;
292
293 hlist_for_each_entry(child, &core->children, child_node) {
294 ret = __clk_lookup_subtree(name, child);
295 if (ret)
296 return ret;
297 }
298
299 return NULL;
300}
301
302static struct clk_core *clk_core_lookup(const char *name)
303{
304 struct clk_core *root_clk;
305 struct clk_core *ret;
306
307 if (!name)
308 return NULL;
309
310 /* search the 'proper' clk tree first */
311 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
312 ret = __clk_lookup_subtree(name, root_clk);
313 if (ret)
314 return ret;
315 }
316
317 /* if not found, then search the orphan tree */
318 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
319 ret = __clk_lookup_subtree(name, root_clk);
320 if (ret)
321 return ret;
322 }
323
324 return NULL;
325}
326
Stephen Boydfc0c2092019-04-12 11:31:47 -0700327/**
Stephen Boyddde4eff2019-04-12 11:31:48 -0700328 * clk_core_get - Find the clk_core parent of a clk
Stephen Boydfc0c2092019-04-12 11:31:47 -0700329 * @core: clk to find parent of
Stephen Boyd1a079562019-04-30 10:22:30 -0700330 * @p_index: parent index to search for
Stephen Boydfc0c2092019-04-12 11:31:47 -0700331 *
332 * This is the preferred method for clk providers to find the parent of a
333 * clk when that parent is external to the clk controller. The parent_names
334 * array is indexed and treated as a local name matching a string in the device
Stephen Boyddde4eff2019-04-12 11:31:48 -0700335 * node's 'clock-names' property or as the 'con_id' matching the device's
336 * dev_name() in a clk_lookup. This allows clk providers to use their own
Stephen Boydfc0c2092019-04-12 11:31:47 -0700337 * namespace instead of looking for a globally unique parent string.
338 *
339 * For example the following DT snippet would allow a clock registered by the
340 * clock-controller@c001 that has a clk_init_data::parent_data array
341 * with 'xtal' in the 'name' member to find the clock provided by the
342 * clock-controller@f00abcd without needing to get the globally unique name of
343 * the xtal clk.
344 *
345 * parent: clock-controller@f00abcd {
346 * reg = <0xf00abcd 0xabcd>;
347 * #clock-cells = <0>;
348 * };
349 *
350 * clock-controller@c001 {
351 * reg = <0xc001 0xf00d>;
352 * clocks = <&parent>;
353 * clock-names = "xtal";
354 * #clock-cells = <1>;
355 * };
356 *
357 * Returns: -ENOENT when the provider can't be found or the clk doesn't
358 * exist in the provider. -EINVAL when the name can't be found. NULL when the
359 * provider knows about the clk but it isn't provided on this system.
360 * A valid clk_core pointer when the clk can be found in the provider.
361 */
Stephen Boyd1a079562019-04-30 10:22:30 -0700362static struct clk_core *clk_core_get(struct clk_core *core, u8 p_index)
Stephen Boydfc0c2092019-04-12 11:31:47 -0700363{
Stephen Boyd1a079562019-04-30 10:22:30 -0700364 const char *name = core->parents[p_index].fw_name;
365 int index = core->parents[p_index].index;
Stephen Boyddde4eff2019-04-12 11:31:48 -0700366 struct clk_hw *hw = ERR_PTR(-ENOENT);
367 struct device *dev = core->dev;
368 const char *dev_id = dev ? dev_name(dev) : NULL;
Stephen Boydfc0c2092019-04-12 11:31:47 -0700369 struct device_node *np = core->of_node;
370
Stephen Boydc8edb312019-06-14 10:46:06 -0700371 if (np && (name || index >= 0))
Stephen Boyd601b6e92019-04-12 11:31:49 -0700372 hw = of_clk_get_hw(np, index, name);
Stephen Boydfc0c2092019-04-12 11:31:47 -0700373
Stephen Boyddde4eff2019-04-12 11:31:48 -0700374 /*
375 * If the DT search above couldn't find the provider or the provider
376 * didn't know about this clk, fallback to looking up via clkdev based
377 * clk_lookups
378 */
Stephen Boyd601b6e92019-04-12 11:31:49 -0700379 if (PTR_ERR(hw) == -ENOENT && name)
Stephen Boyddde4eff2019-04-12 11:31:48 -0700380 hw = clk_find_hw(dev_id, name);
381
382 if (IS_ERR(hw))
Stephen Boydfc0c2092019-04-12 11:31:47 -0700383 return ERR_CAST(hw);
384
385 return hw->core;
386}
387
388static void clk_core_fill_parent_index(struct clk_core *core, u8 index)
389{
390 struct clk_parent_map *entry = &core->parents[index];
391 struct clk_core *parent = ERR_PTR(-ENOENT);
392
393 if (entry->hw) {
394 parent = entry->hw->core;
395 /*
396 * We have a direct reference but it isn't registered yet?
397 * Orphan it and let clk_reparent() update the orphan status
398 * when the parent is registered.
399 */
400 if (!parent)
401 parent = ERR_PTR(-EPROBE_DEFER);
402 } else {
Stephen Boyd1a079562019-04-30 10:22:30 -0700403 parent = clk_core_get(core, index);
Stephen Boydfc0c2092019-04-12 11:31:47 -0700404 if (IS_ERR(parent) && PTR_ERR(parent) == -ENOENT)
405 parent = clk_core_lookup(entry->name);
406 }
407
408 /* Only cache it if it's not an error */
409 if (!IS_ERR(parent))
410 entry->core = parent;
411}
412
Stephen Boydd6968fc2015-04-30 13:54:13 -0700413static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100414 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100415{
Stephen Boydfc0c2092019-04-12 11:31:47 -0700416 if (!core || index >= core->num_parents || !core->parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100417 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900418
Stephen Boydfc0c2092019-04-12 11:31:47 -0700419 if (!core->parents[index].core)
420 clk_core_fill_parent_index(core, index);
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900421
Stephen Boydfc0c2092019-04-12 11:31:47 -0700422 return core->parents[index].core;
James Hogan7ef3dcc2013-07-29 12:24:58 +0100423}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100424
Stephen Boyde7df6f62015-08-12 13:04:56 -0700425struct clk_hw *
426clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700427{
428 struct clk_core *parent;
429
430 parent = clk_core_get_parent_by_index(hw->core, index);
431
432 return !parent ? NULL : parent->hw;
433}
434EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
435
Russ Dill65800b22012-11-26 11:20:09 -0800436unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700437{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100438 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700439}
440
Stephen Boydd6968fc2015-04-30 13:54:13 -0700441static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700442{
Stephen Boyd73d4f942019-02-01 15:39:50 -0800443 if (!core)
444 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700445
Stephen Boyd73d4f942019-02-01 15:39:50 -0800446 if (!core->num_parents || core->parent)
447 return core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700448
Stephen Boyd73d4f942019-02-01 15:39:50 -0800449 /*
450 * Clk must have a parent because num_parents > 0 but the parent isn't
451 * known yet. Best to return 0 as the rate of this clk until we can
452 * properly recalc the rate based on the parent's rate.
453 */
454 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700455}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100456
Stephen Boyde7df6f62015-08-12 13:04:56 -0700457unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700458{
459 return clk_core_get_rate_nolock(hw->core);
460}
461EXPORT_SYMBOL_GPL(clk_hw_get_rate);
462
Stephen Boydd6968fc2015-04-30 13:54:13 -0700463static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100464{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700465 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100466 return 0;
467
Stephen Boydd6968fc2015-04-30 13:54:13 -0700468 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100469}
470
Russ Dill65800b22012-11-26 11:20:09 -0800471unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700472{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100473 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700474}
Thierry Redingb05c6832013-09-03 09:43:51 +0200475EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700476
Stephen Boyde7df6f62015-08-12 13:04:56 -0700477unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700478{
479 return hw->core->flags;
480}
481EXPORT_SYMBOL_GPL(clk_hw_get_flags);
482
Stephen Boyde7df6f62015-08-12 13:04:56 -0700483bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700484{
485 return clk_core_is_prepared(hw->core);
486}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100487EXPORT_SYMBOL_GPL(clk_hw_is_prepared);
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700488
Jerome Brunete55a8392017-12-01 22:51:56 +0100489bool clk_hw_rate_is_protected(const struct clk_hw *hw)
490{
491 return clk_core_rate_is_protected(hw->core);
492}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100493EXPORT_SYMBOL_GPL(clk_hw_rate_is_protected);
Jerome Brunete55a8392017-12-01 22:51:56 +0100494
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200495bool clk_hw_is_enabled(const struct clk_hw *hw)
496{
497 return clk_core_is_enabled(hw->core);
498}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100499EXPORT_SYMBOL_GPL(clk_hw_is_enabled);
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200500
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100501bool __clk_is_enabled(struct clk *clk)
502{
503 if (!clk)
504 return false;
505
506 return clk_core_is_enabled(clk->core);
507}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800508EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700509
Stephen Boyd15a02c12015-01-19 18:05:28 -0800510static bool mux_is_better_rate(unsigned long rate, unsigned long now,
511 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100512{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800513 if (flags & CLK_MUX_ROUND_CLOSEST)
514 return abs(now - rate) < abs(best - rate);
515
516 return now <= rate && now > best;
517}
518
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200519int clk_mux_determine_rate_flags(struct clk_hw *hw,
520 struct clk_rate_request *req,
521 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100522{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100523 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200524 int i, num_parents, ret;
525 unsigned long best = 0;
526 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100527
528 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100529 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
530 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200531 if (core->flags & CLK_SET_RATE_PARENT) {
532 ret = __clk_determine_rate(parent ? parent->hw : NULL,
533 &parent_req);
534 if (ret)
535 return ret;
536
537 best = parent_req.rate;
538 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100539 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200540 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100541 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200542 }
543
James Hogane366fdd2013-07-29 12:25:02 +0100544 goto out;
545 }
546
547 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100548 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100549 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100550 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100551 if (!parent)
552 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200553
554 if (core->flags & CLK_SET_RATE_PARENT) {
555 parent_req = *req;
556 ret = __clk_determine_rate(parent->hw, &parent_req);
557 if (ret)
558 continue;
559 } else {
560 parent_req.rate = clk_core_get_rate_nolock(parent);
561 }
562
563 if (mux_is_better_rate(req->rate, parent_req.rate,
564 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100565 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200566 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100567 }
568 }
569
Boris Brezillon57d866e2015-07-09 22:39:38 +0200570 if (!best_parent)
571 return -EINVAL;
572
James Hogane366fdd2013-07-29 12:25:02 +0100573out:
574 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200575 req->best_parent_hw = best_parent->hw;
576 req->best_parent_rate = best;
577 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100578
Boris Brezillon0817b622015-07-07 20:48:08 +0200579 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100580}
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200581EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800582
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100583struct clk *__clk_lookup(const char *name)
584{
585 struct clk_core *core = clk_core_lookup(name);
586
587 return !core ? NULL : core->hw->clk;
588}
589
Stephen Boydd6968fc2015-04-30 13:54:13 -0700590static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100591 unsigned long *min_rate,
592 unsigned long *max_rate)
593{
594 struct clk *clk_user;
595
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700596 *min_rate = core->min_rate;
597 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100598
Stephen Boydd6968fc2015-04-30 13:54:13 -0700599 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100600 *min_rate = max(*min_rate, clk_user->min_rate);
601
Stephen Boydd6968fc2015-04-30 13:54:13 -0700602 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100603 *max_rate = min(*max_rate, clk_user->max_rate);
604}
605
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700606void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
607 unsigned long max_rate)
608{
609 hw->core->min_rate = min_rate;
610 hw->core->max_rate = max_rate;
611}
612EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
613
Stephen Boyd15a02c12015-01-19 18:05:28 -0800614/*
Stephen Boyd777c1a42018-12-11 13:24:50 -0800615 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
616 * @hw: mux type clk to determine rate on
617 * @req: rate request, also used to return preferred parent and frequencies
618 *
Stephen Boyd15a02c12015-01-19 18:05:28 -0800619 * Helper for finding best parent to provide a given frequency. This can be used
620 * directly as a determine_rate callback (e.g. for a mux), or from a more
621 * complex clock that may combine a mux with other operations.
Stephen Boyd777c1a42018-12-11 13:24:50 -0800622 *
623 * Returns: 0 on success, -EERROR value on error
Stephen Boyd15a02c12015-01-19 18:05:28 -0800624 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200625int __clk_mux_determine_rate(struct clk_hw *hw,
626 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800627{
Boris Brezillon0817b622015-07-07 20:48:08 +0200628 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800629}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800630EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100631
Boris Brezillon0817b622015-07-07 20:48:08 +0200632int __clk_mux_determine_rate_closest(struct clk_hw *hw,
633 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800634{
Boris Brezillon0817b622015-07-07 20:48:08 +0200635 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800636}
637EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
638
Mike Turquetteb24764902012-03-15 23:11:19 -0700639/*** clk api ***/
640
Jerome Brunete55a8392017-12-01 22:51:56 +0100641static void clk_core_rate_unprotect(struct clk_core *core)
642{
643 lockdep_assert_held(&prepare_lock);
644
645 if (!core)
646 return;
647
Fabio Estevamab525dc2018-01-16 10:50:34 -0200648 if (WARN(core->protect_count == 0,
649 "%s already unprotected\n", core->name))
Jerome Brunete55a8392017-12-01 22:51:56 +0100650 return;
651
652 if (--core->protect_count > 0)
653 return;
654
655 clk_core_rate_unprotect(core->parent);
656}
657
658static int clk_core_rate_nuke_protect(struct clk_core *core)
659{
660 int ret;
661
662 lockdep_assert_held(&prepare_lock);
663
664 if (!core)
665 return -EINVAL;
666
667 if (core->protect_count == 0)
668 return 0;
669
670 ret = core->protect_count;
671 core->protect_count = 1;
672 clk_core_rate_unprotect(core);
673
674 return ret;
675}
676
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100677/**
678 * clk_rate_exclusive_put - release exclusivity over clock rate control
679 * @clk: the clk over which the exclusivity is released
680 *
681 * clk_rate_exclusive_put() completes a critical section during which a clock
682 * consumer cannot tolerate any other consumer making any operation on the
683 * clock which could result in a rate change or rate glitch. Exclusive clocks
684 * cannot have their rate changed, either directly or indirectly due to changes
685 * further up the parent chain of clocks. As a result, clocks up parent chain
686 * also get under exclusive control of the calling consumer.
687 *
688 * If exlusivity is claimed more than once on clock, even by the same consumer,
689 * the rate effectively gets locked as exclusivity can't be preempted.
690 *
691 * Calls to clk_rate_exclusive_put() must be balanced with calls to
692 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
693 * error status.
694 */
695void clk_rate_exclusive_put(struct clk *clk)
696{
697 if (!clk)
698 return;
699
700 clk_prepare_lock();
701
702 /*
703 * if there is something wrong with this consumer protect count, stop
704 * here before messing with the provider
705 */
706 if (WARN_ON(clk->exclusive_count <= 0))
707 goto out;
708
709 clk_core_rate_unprotect(clk->core);
710 clk->exclusive_count--;
711out:
712 clk_prepare_unlock();
713}
714EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
715
Jerome Brunete55a8392017-12-01 22:51:56 +0100716static void clk_core_rate_protect(struct clk_core *core)
717{
718 lockdep_assert_held(&prepare_lock);
719
720 if (!core)
721 return;
722
723 if (core->protect_count == 0)
724 clk_core_rate_protect(core->parent);
725
726 core->protect_count++;
727}
728
729static void clk_core_rate_restore_protect(struct clk_core *core, int count)
730{
731 lockdep_assert_held(&prepare_lock);
732
733 if (!core)
734 return;
735
736 if (count == 0)
737 return;
738
739 clk_core_rate_protect(core);
740 core->protect_count = count;
741}
742
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100743/**
744 * clk_rate_exclusive_get - get exclusivity over the clk rate control
745 * @clk: the clk over which the exclusity of rate control is requested
746 *
747 * clk_rate_exlusive_get() begins a critical section during which a clock
748 * consumer cannot tolerate any other consumer making any operation on the
749 * clock which could result in a rate change or rate glitch. Exclusive clocks
750 * cannot have their rate changed, either directly or indirectly due to changes
751 * further up the parent chain of clocks. As a result, clocks up parent chain
752 * also get under exclusive control of the calling consumer.
753 *
754 * If exlusivity is claimed more than once on clock, even by the same consumer,
755 * the rate effectively gets locked as exclusivity can't be preempted.
756 *
757 * Calls to clk_rate_exclusive_get() should be balanced with calls to
758 * clk_rate_exclusive_put(). Calls to this function may sleep.
759 * Returns 0 on success, -EERROR otherwise
760 */
761int clk_rate_exclusive_get(struct clk *clk)
762{
763 if (!clk)
764 return 0;
765
766 clk_prepare_lock();
767 clk_core_rate_protect(clk->core);
768 clk->exclusive_count++;
769 clk_prepare_unlock();
770
771 return 0;
772}
773EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
774
Stephen Boydd6968fc2015-04-30 13:54:13 -0700775static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700776{
Stephen Boyda6334722015-05-06 17:00:54 -0700777 lockdep_assert_held(&prepare_lock);
778
Stephen Boydd6968fc2015-04-30 13:54:13 -0700779 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700780 return;
781
Fabio Estevamab525dc2018-01-16 10:50:34 -0200782 if (WARN(core->prepare_count == 0,
783 "%s already unprepared\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700784 return;
785
Fabio Estevamab525dc2018-01-16 10:50:34 -0200786 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
787 "Unpreparing critical %s\n", core->name))
Lee Jones2e20fbf2016-02-11 13:19:10 -0800788 return;
789
Jerome Brunet9461f7b2018-06-19 15:40:51 +0200790 if (core->flags & CLK_SET_RATE_GATE)
791 clk_core_rate_unprotect(core);
792
Stephen Boydd6968fc2015-04-30 13:54:13 -0700793 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700794 return;
795
Fabio Estevamab525dc2018-01-16 10:50:34 -0200796 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700797
Stephen Boydd6968fc2015-04-30 13:54:13 -0700798 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800799
Stephen Boydd6968fc2015-04-30 13:54:13 -0700800 if (core->ops->unprepare)
801 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700802
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200803 clk_pm_runtime_put(core);
804
Stephen Boydd6968fc2015-04-30 13:54:13 -0700805 trace_clk_unprepare_complete(core);
806 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700807}
808
Dong Aishenga6adc302016-06-30 17:31:11 +0800809static void clk_core_unprepare_lock(struct clk_core *core)
810{
811 clk_prepare_lock();
812 clk_core_unprepare(core);
813 clk_prepare_unlock();
814}
815
Mike Turquetteb24764902012-03-15 23:11:19 -0700816/**
817 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200818 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700819 *
820 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
821 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
822 * if the operation may sleep. One example is a clk which is accessed over
823 * I2c. In the complex case a clk gate operation may require a fast and a slow
824 * part. It is this reason that clk_unprepare and clk_disable are not mutually
825 * exclusive. In fact clk_disable must be called before clk_unprepare.
826 */
827void clk_unprepare(struct clk *clk)
828{
Stephen Boyd63589e92014-03-26 16:06:37 -0700829 if (IS_ERR_OR_NULL(clk))
830 return;
831
Dong Aishenga6adc302016-06-30 17:31:11 +0800832 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700833}
834EXPORT_SYMBOL_GPL(clk_unprepare);
835
Stephen Boydd6968fc2015-04-30 13:54:13 -0700836static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700837{
838 int ret = 0;
839
Stephen Boyda6334722015-05-06 17:00:54 -0700840 lockdep_assert_held(&prepare_lock);
841
Stephen Boydd6968fc2015-04-30 13:54:13 -0700842 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700843 return 0;
844
Stephen Boydd6968fc2015-04-30 13:54:13 -0700845 if (core->prepare_count == 0) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200846 ret = clk_pm_runtime_get(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700847 if (ret)
848 return ret;
849
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200850 ret = clk_core_prepare(core->parent);
851 if (ret)
852 goto runtime_put;
853
Stephen Boydd6968fc2015-04-30 13:54:13 -0700854 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800855
Stephen Boydd6968fc2015-04-30 13:54:13 -0700856 if (core->ops->prepare)
857 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800858
Stephen Boydd6968fc2015-04-30 13:54:13 -0700859 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800860
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200861 if (ret)
862 goto unprepare;
Mike Turquetteb24764902012-03-15 23:11:19 -0700863 }
864
Stephen Boydd6968fc2015-04-30 13:54:13 -0700865 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700866
Jerome Brunet9461f7b2018-06-19 15:40:51 +0200867 /*
868 * CLK_SET_RATE_GATE is a special case of clock protection
869 * Instead of a consumer claiming exclusive rate control, it is
870 * actually the provider which prevents any consumer from making any
871 * operation which could result in a rate change or rate glitch while
872 * the clock is prepared.
873 */
874 if (core->flags & CLK_SET_RATE_GATE)
875 clk_core_rate_protect(core);
876
Mike Turquetteb24764902012-03-15 23:11:19 -0700877 return 0;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200878unprepare:
879 clk_core_unprepare(core->parent);
880runtime_put:
881 clk_pm_runtime_put(core);
882 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700883}
884
Dong Aishenga6adc302016-06-30 17:31:11 +0800885static int clk_core_prepare_lock(struct clk_core *core)
886{
887 int ret;
888
889 clk_prepare_lock();
890 ret = clk_core_prepare(core);
891 clk_prepare_unlock();
892
893 return ret;
894}
895
Mike Turquetteb24764902012-03-15 23:11:19 -0700896/**
897 * clk_prepare - prepare a clock source
898 * @clk: the clk being prepared
899 *
900 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
901 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
902 * operation may sleep. One example is a clk which is accessed over I2c. In
903 * the complex case a clk ungate operation may require a fast and a slow part.
904 * It is this reason that clk_prepare and clk_enable are not mutually
905 * exclusive. In fact clk_prepare must be called before clk_enable.
906 * Returns 0 on success, -EERROR otherwise.
907 */
908int clk_prepare(struct clk *clk)
909{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100910 if (!clk)
911 return 0;
912
Dong Aishenga6adc302016-06-30 17:31:11 +0800913 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700914}
915EXPORT_SYMBOL_GPL(clk_prepare);
916
Stephen Boydd6968fc2015-04-30 13:54:13 -0700917static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700918{
Stephen Boyda6334722015-05-06 17:00:54 -0700919 lockdep_assert_held(&enable_lock);
920
Stephen Boydd6968fc2015-04-30 13:54:13 -0700921 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700922 return;
923
Fabio Estevamab525dc2018-01-16 10:50:34 -0200924 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700925 return;
926
Fabio Estevamab525dc2018-01-16 10:50:34 -0200927 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
928 "Disabling critical %s\n", core->name))
Lee Jones2e20fbf2016-02-11 13:19:10 -0800929 return;
930
Stephen Boydd6968fc2015-04-30 13:54:13 -0700931 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700932 return;
933
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700934 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800935
Stephen Boydd6968fc2015-04-30 13:54:13 -0700936 if (core->ops->disable)
937 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700938
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700939 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800940
Stephen Boydd6968fc2015-04-30 13:54:13 -0700941 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100942}
943
Dong Aishenga6adc302016-06-30 17:31:11 +0800944static void clk_core_disable_lock(struct clk_core *core)
945{
946 unsigned long flags;
947
948 flags = clk_enable_lock();
949 clk_core_disable(core);
950 clk_enable_unlock(flags);
951}
952
Mike Turquetteb24764902012-03-15 23:11:19 -0700953/**
954 * clk_disable - gate a clock
955 * @clk: the clk being gated
956 *
957 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
958 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
959 * clk if the operation is fast and will never sleep. One example is a
960 * SoC-internal clk which is controlled via simple register writes. In the
961 * complex case a clk gate operation may require a fast and a slow part. It is
962 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
963 * In fact clk_disable must be called before clk_unprepare.
964 */
965void clk_disable(struct clk *clk)
966{
Stephen Boyd63589e92014-03-26 16:06:37 -0700967 if (IS_ERR_OR_NULL(clk))
968 return;
969
Dong Aishenga6adc302016-06-30 17:31:11 +0800970 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700971}
972EXPORT_SYMBOL_GPL(clk_disable);
973
Stephen Boydd6968fc2015-04-30 13:54:13 -0700974static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700975{
976 int ret = 0;
977
Stephen Boyda6334722015-05-06 17:00:54 -0700978 lockdep_assert_held(&enable_lock);
979
Stephen Boydd6968fc2015-04-30 13:54:13 -0700980 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700981 return 0;
982
Fabio Estevamab525dc2018-01-16 10:50:34 -0200983 if (WARN(core->prepare_count == 0,
984 "Enabling unprepared %s\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700985 return -ESHUTDOWN;
986
Stephen Boydd6968fc2015-04-30 13:54:13 -0700987 if (core->enable_count == 0) {
988 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700989
990 if (ret)
991 return ret;
992
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700993 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800994
Stephen Boydd6968fc2015-04-30 13:54:13 -0700995 if (core->ops->enable)
996 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800997
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700998 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800999
1000 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001001 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001002 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001003 }
1004 }
1005
Stephen Boydd6968fc2015-04-30 13:54:13 -07001006 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07001007 return 0;
1008}
1009
Dong Aishenga6adc302016-06-30 17:31:11 +08001010static int clk_core_enable_lock(struct clk_core *core)
1011{
1012 unsigned long flags;
1013 int ret;
1014
1015 flags = clk_enable_lock();
1016 ret = clk_core_enable(core);
1017 clk_enable_unlock(flags);
1018
1019 return ret;
1020}
1021
Keerthy43536542018-09-04 12:19:36 +05301022/**
1023 * clk_gate_restore_context - restore context for poweroff
1024 * @hw: the clk_hw pointer of clock whose state is to be restored
1025 *
1026 * The clock gate restore context function enables or disables
1027 * the gate clocks based on the enable_count. This is done in cases
1028 * where the clock context is lost and based on the enable_count
1029 * the clock either needs to be enabled/disabled. This
1030 * helps restore the state of gate clocks.
1031 */
1032void clk_gate_restore_context(struct clk_hw *hw)
1033{
Stephen Boyd9be76622018-10-11 09:28:13 -07001034 struct clk_core *core = hw->core;
1035
1036 if (core->enable_count)
1037 core->ops->enable(hw);
Keerthy43536542018-09-04 12:19:36 +05301038 else
Stephen Boyd9be76622018-10-11 09:28:13 -07001039 core->ops->disable(hw);
Keerthy43536542018-09-04 12:19:36 +05301040}
1041EXPORT_SYMBOL_GPL(clk_gate_restore_context);
1042
Stephen Boyd9be76622018-10-11 09:28:13 -07001043static int clk_core_save_context(struct clk_core *core)
Russ Dill8b95d1c2018-09-04 12:19:35 +05301044{
1045 struct clk_core *child;
1046 int ret = 0;
1047
Stephen Boyd9be76622018-10-11 09:28:13 -07001048 hlist_for_each_entry(child, &core->children, child_node) {
1049 ret = clk_core_save_context(child);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301050 if (ret < 0)
1051 return ret;
1052 }
1053
Stephen Boyd9be76622018-10-11 09:28:13 -07001054 if (core->ops && core->ops->save_context)
1055 ret = core->ops->save_context(core->hw);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301056
1057 return ret;
1058}
1059
Stephen Boyd9be76622018-10-11 09:28:13 -07001060static void clk_core_restore_context(struct clk_core *core)
Russ Dill8b95d1c2018-09-04 12:19:35 +05301061{
1062 struct clk_core *child;
1063
Stephen Boyd9be76622018-10-11 09:28:13 -07001064 if (core->ops && core->ops->restore_context)
1065 core->ops->restore_context(core->hw);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301066
Stephen Boyd9be76622018-10-11 09:28:13 -07001067 hlist_for_each_entry(child, &core->children, child_node)
1068 clk_core_restore_context(child);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301069}
1070
1071/**
1072 * clk_save_context - save clock context for poweroff
1073 *
1074 * Saves the context of the clock register for powerstates in which the
1075 * contents of the registers will be lost. Occurs deep within the suspend
1076 * code. Returns 0 on success.
1077 */
1078int clk_save_context(void)
1079{
1080 struct clk_core *clk;
1081 int ret;
1082
1083 hlist_for_each_entry(clk, &clk_root_list, child_node) {
Stephen Boyd9be76622018-10-11 09:28:13 -07001084 ret = clk_core_save_context(clk);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301085 if (ret < 0)
1086 return ret;
1087 }
1088
1089 hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
Stephen Boyd9be76622018-10-11 09:28:13 -07001090 ret = clk_core_save_context(clk);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301091 if (ret < 0)
1092 return ret;
1093 }
1094
1095 return 0;
1096}
1097EXPORT_SYMBOL_GPL(clk_save_context);
1098
1099/**
1100 * clk_restore_context - restore clock context after poweroff
1101 *
1102 * Restore the saved clock context upon resume.
1103 *
1104 */
1105void clk_restore_context(void)
1106{
Stephen Boyd9be76622018-10-11 09:28:13 -07001107 struct clk_core *core;
Russ Dill8b95d1c2018-09-04 12:19:35 +05301108
Stephen Boyd9be76622018-10-11 09:28:13 -07001109 hlist_for_each_entry(core, &clk_root_list, child_node)
1110 clk_core_restore_context(core);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301111
Stephen Boyd9be76622018-10-11 09:28:13 -07001112 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1113 clk_core_restore_context(core);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301114}
1115EXPORT_SYMBOL_GPL(clk_restore_context);
1116
Mike Turquetteb24764902012-03-15 23:11:19 -07001117/**
1118 * clk_enable - ungate a clock
1119 * @clk: the clk being ungated
1120 *
1121 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1122 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1123 * if the operation will never sleep. One example is a SoC-internal clk which
1124 * is controlled via simple register writes. In the complex case a clk ungate
1125 * operation may require a fast and a slow part. It is this reason that
1126 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1127 * must be called before clk_enable. Returns 0 on success, -EERROR
1128 * otherwise.
1129 */
1130int clk_enable(struct clk *clk)
1131{
Dong Aisheng864e1602015-04-30 14:02:19 -07001132 if (!clk)
1133 return 0;
1134
Dong Aishenga6adc302016-06-30 17:31:11 +08001135 return clk_core_enable_lock(clk->core);
1136}
1137EXPORT_SYMBOL_GPL(clk_enable);
1138
1139static int clk_core_prepare_enable(struct clk_core *core)
1140{
1141 int ret;
1142
1143 ret = clk_core_prepare_lock(core);
1144 if (ret)
1145 return ret;
1146
1147 ret = clk_core_enable_lock(core);
1148 if (ret)
1149 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001150
1151 return ret;
1152}
Dong Aishenga6adc302016-06-30 17:31:11 +08001153
1154static void clk_core_disable_unprepare(struct clk_core *core)
1155{
1156 clk_core_disable_lock(core);
1157 clk_core_unprepare_lock(core);
1158}
Mike Turquetteb24764902012-03-15 23:11:19 -07001159
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001160static void clk_unprepare_unused_subtree(struct clk_core *core)
1161{
1162 struct clk_core *child;
1163
1164 lockdep_assert_held(&prepare_lock);
1165
1166 hlist_for_each_entry(child, &core->children, child_node)
1167 clk_unprepare_unused_subtree(child);
1168
1169 if (core->prepare_count)
1170 return;
1171
1172 if (core->flags & CLK_IGNORE_UNUSED)
1173 return;
1174
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001175 if (clk_pm_runtime_get(core))
1176 return;
1177
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001178 if (clk_core_is_prepared(core)) {
1179 trace_clk_unprepare(core);
1180 if (core->ops->unprepare_unused)
1181 core->ops->unprepare_unused(core->hw);
1182 else if (core->ops->unprepare)
1183 core->ops->unprepare(core->hw);
1184 trace_clk_unprepare_complete(core);
1185 }
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001186
1187 clk_pm_runtime_put(core);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001188}
1189
1190static void clk_disable_unused_subtree(struct clk_core *core)
1191{
1192 struct clk_core *child;
1193 unsigned long flags;
1194
1195 lockdep_assert_held(&prepare_lock);
1196
1197 hlist_for_each_entry(child, &core->children, child_node)
1198 clk_disable_unused_subtree(child);
1199
Dong Aishenga4b35182016-06-30 17:31:13 +08001200 if (core->flags & CLK_OPS_PARENT_ENABLE)
1201 clk_core_prepare_enable(core->parent);
1202
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001203 if (clk_pm_runtime_get(core))
1204 goto unprepare_out;
1205
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001206 flags = clk_enable_lock();
1207
1208 if (core->enable_count)
1209 goto unlock_out;
1210
1211 if (core->flags & CLK_IGNORE_UNUSED)
1212 goto unlock_out;
1213
1214 /*
1215 * some gate clocks have special needs during the disable-unused
1216 * sequence. call .disable_unused if available, otherwise fall
1217 * back to .disable
1218 */
1219 if (clk_core_is_enabled(core)) {
1220 trace_clk_disable(core);
1221 if (core->ops->disable_unused)
1222 core->ops->disable_unused(core->hw);
1223 else if (core->ops->disable)
1224 core->ops->disable(core->hw);
1225 trace_clk_disable_complete(core);
1226 }
1227
1228unlock_out:
1229 clk_enable_unlock(flags);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001230 clk_pm_runtime_put(core);
1231unprepare_out:
Dong Aishenga4b35182016-06-30 17:31:13 +08001232 if (core->flags & CLK_OPS_PARENT_ENABLE)
1233 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001234}
1235
1236static bool clk_ignore_unused;
1237static int __init clk_ignore_unused_setup(char *__unused)
1238{
1239 clk_ignore_unused = true;
1240 return 1;
1241}
1242__setup("clk_ignore_unused", clk_ignore_unused_setup);
1243
1244static int clk_disable_unused(void)
1245{
1246 struct clk_core *core;
1247
1248 if (clk_ignore_unused) {
1249 pr_warn("clk: Not disabling unused clocks\n");
1250 return 0;
1251 }
1252
1253 clk_prepare_lock();
1254
1255 hlist_for_each_entry(core, &clk_root_list, child_node)
1256 clk_disable_unused_subtree(core);
1257
1258 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1259 clk_disable_unused_subtree(core);
1260
1261 hlist_for_each_entry(core, &clk_root_list, child_node)
1262 clk_unprepare_unused_subtree(core);
1263
1264 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1265 clk_unprepare_unused_subtree(core);
1266
1267 clk_prepare_unlock();
1268
1269 return 0;
1270}
1271late_initcall_sync(clk_disable_unused);
1272
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001273static int clk_core_determine_round_nolock(struct clk_core *core,
1274 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001275{
Boris Brezillon0817b622015-07-07 20:48:08 +02001276 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001277
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001278 lockdep_assert_held(&prepare_lock);
1279
Stephen Boydd6968fc2015-04-30 13:54:13 -07001280 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001281 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001282
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001283 /*
1284 * At this point, core protection will be disabled if
1285 * - if the provider is not protected at all
1286 * - if the calling consumer is the only one which has exclusivity
1287 * over the provider
1288 */
Jerome Brunete55a8392017-12-01 22:51:56 +01001289 if (clk_core_rate_is_protected(core)) {
1290 req->rate = core->rate;
1291 } else if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001292 return core->ops->determine_rate(core->hw, req);
1293 } else if (core->ops->round_rate) {
1294 rate = core->ops->round_rate(core->hw, req->rate,
1295 &req->best_parent_rate);
1296 if (rate < 0)
1297 return rate;
1298
1299 req->rate = rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001300 } else {
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001301 return -EINVAL;
Boris Brezillon0817b622015-07-07 20:48:08 +02001302 }
1303
1304 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001305}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001306
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001307static void clk_core_init_rate_req(struct clk_core * const core,
1308 struct clk_rate_request *req)
1309{
1310 struct clk_core *parent;
1311
1312 if (WARN_ON(!core || !req))
1313 return;
1314
Mike Turquetteb24764902012-03-15 23:11:19 -07001315 parent = core->parent;
1316 if (parent) {
1317 req->best_parent_hw = parent->hw;
1318 req->best_parent_rate = parent->rate;
1319 } else {
1320 req->best_parent_hw = NULL;
1321 req->best_parent_rate = 0;
1322 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001323}
Mike Turquetteb24764902012-03-15 23:11:19 -07001324
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001325static bool clk_core_can_round(struct clk_core * const core)
1326{
Geert Uytterhoeveneef1f1b2019-06-17 14:02:48 +02001327 return core->ops->determine_rate || core->ops->round_rate;
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001328}
Mike Turquetteb24764902012-03-15 23:11:19 -07001329
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001330static int clk_core_round_rate_nolock(struct clk_core *core,
1331 struct clk_rate_request *req)
1332{
1333 lockdep_assert_held(&prepare_lock);
1334
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001335 if (!core) {
1336 req->rate = 0;
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001337 return 0;
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001338 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001339
1340 clk_core_init_rate_req(core, req);
1341
1342 if (clk_core_can_round(core))
1343 return clk_core_determine_round_nolock(core, req);
1344 else if (core->flags & CLK_SET_RATE_PARENT)
1345 return clk_core_round_rate_nolock(core->parent, req);
1346
1347 req->rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001348 return 0;
1349}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001350
1351/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001352 * __clk_determine_rate - get the closest rate actually supported by a clock
1353 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001354 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001355 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001356 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001357 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001358int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001359{
Boris Brezillon0817b622015-07-07 20:48:08 +02001360 if (!hw) {
1361 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001362 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001363 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001364
Boris Brezillon0817b622015-07-07 20:48:08 +02001365 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001366}
1367EXPORT_SYMBOL_GPL(__clk_determine_rate);
1368
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001369unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1370{
1371 int ret;
1372 struct clk_rate_request req;
1373
1374 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1375 req.rate = rate;
1376
1377 ret = clk_core_round_rate_nolock(hw->core, &req);
1378 if (ret)
1379 return 0;
1380
1381 return req.rate;
1382}
1383EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1384
Mike Turquetteb24764902012-03-15 23:11:19 -07001385/**
1386 * clk_round_rate - round the given rate for a clk
1387 * @clk: the clk for which we are rounding a rate
1388 * @rate: the rate which is to be rounded
1389 *
1390 * Takes in a rate as input and rounds it to a rate that the clk can actually
1391 * use which is then returned. If clk doesn't support round_rate operation
1392 * then the parent rate is returned.
1393 */
1394long clk_round_rate(struct clk *clk, unsigned long rate)
1395{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001396 struct clk_rate_request req;
1397 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001398
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001399 if (!clk)
1400 return 0;
1401
Mike Turquetteeab89f62013-03-28 13:59:01 -07001402 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001403
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001404 if (clk->exclusive_count)
1405 clk_core_rate_unprotect(clk->core);
1406
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001407 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1408 req.rate = rate;
1409
1410 ret = clk_core_round_rate_nolock(clk->core, &req);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001411
1412 if (clk->exclusive_count)
1413 clk_core_rate_protect(clk->core);
1414
Mike Turquetteeab89f62013-03-28 13:59:01 -07001415 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001416
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001417 if (ret)
1418 return ret;
1419
1420 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001421}
1422EXPORT_SYMBOL_GPL(clk_round_rate);
1423
1424/**
1425 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001426 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001427 * @msg: clk notifier type (see include/linux/clk.h)
1428 * @old_rate: old clk rate
1429 * @new_rate: new clk rate
1430 *
1431 * Triggers a notifier call chain on the clk rate-change notification
1432 * for 'clk'. Passes a pointer to the struct clk and the previous
1433 * and current rates to the notifier callback. Intended to be called by
1434 * internal clock code only. Returns NOTIFY_DONE from the last driver
1435 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1436 * a driver returns that.
1437 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001438static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001439 unsigned long old_rate, unsigned long new_rate)
1440{
1441 struct clk_notifier *cn;
1442 struct clk_notifier_data cnd;
1443 int ret = NOTIFY_DONE;
1444
Mike Turquetteb24764902012-03-15 23:11:19 -07001445 cnd.old_rate = old_rate;
1446 cnd.new_rate = new_rate;
1447
1448 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001449 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001450 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001451 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1452 &cnd);
Peter De Schrijver17c34c52017-03-21 12:16:26 +02001453 if (ret & NOTIFY_STOP_MASK)
1454 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001455 }
1456 }
1457
1458 return ret;
1459}
1460
1461/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001462 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001463 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001464 *
1465 * Walks the subtree of clks starting with clk and recalculates accuracies as
1466 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001467 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001468 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001469 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001470static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001471{
1472 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001473 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001474
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001475 lockdep_assert_held(&prepare_lock);
1476
Stephen Boydd6968fc2015-04-30 13:54:13 -07001477 if (core->parent)
1478 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001479
Stephen Boydd6968fc2015-04-30 13:54:13 -07001480 if (core->ops->recalc_accuracy)
1481 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001482 parent_accuracy);
1483 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001484 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001485
Stephen Boydd6968fc2015-04-30 13:54:13 -07001486 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001487 __clk_recalc_accuracies(child);
1488}
1489
Stephen Boydd6968fc2015-04-30 13:54:13 -07001490static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001491{
1492 unsigned long accuracy;
1493
1494 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001495 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1496 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001497
Stephen Boydd6968fc2015-04-30 13:54:13 -07001498 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001499 clk_prepare_unlock();
1500
1501 return accuracy;
1502}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001503
1504/**
1505 * clk_get_accuracy - return the accuracy of clk
1506 * @clk: the clk whose accuracy is being returned
1507 *
1508 * Simply returns the cached accuracy of the clk, unless
1509 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1510 * issued.
1511 * If clk is NULL then returns 0.
1512 */
1513long clk_get_accuracy(struct clk *clk)
1514{
1515 if (!clk)
1516 return 0;
1517
1518 return clk_core_get_accuracy(clk->core);
1519}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001520EXPORT_SYMBOL_GPL(clk_get_accuracy);
1521
Stephen Boydd6968fc2015-04-30 13:54:13 -07001522static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001523 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001524{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001525 unsigned long rate = parent_rate;
1526
1527 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1528 rate = core->ops->recalc_rate(core->hw, parent_rate);
1529 clk_pm_runtime_put(core);
1530 }
1531 return rate;
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001532}
1533
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001534/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001535 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001536 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001537 * @msg: notification type (see include/linux/clk.h)
1538 *
1539 * Walks the subtree of clks starting with clk and recalculates rates as it
1540 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001541 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001542 *
1543 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1544 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001545 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001546static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001547{
1548 unsigned long old_rate;
1549 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001550 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001551
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001552 lockdep_assert_held(&prepare_lock);
1553
Stephen Boydd6968fc2015-04-30 13:54:13 -07001554 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001555
Stephen Boydd6968fc2015-04-30 13:54:13 -07001556 if (core->parent)
1557 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001558
Stephen Boydd6968fc2015-04-30 13:54:13 -07001559 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001560
1561 /*
1562 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1563 * & ABORT_RATE_CHANGE notifiers
1564 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001565 if (core->notifier_count && msg)
1566 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001567
Stephen Boydd6968fc2015-04-30 13:54:13 -07001568 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001569 __clk_recalc_rates(child, msg);
1570}
1571
Stephen Boydd6968fc2015-04-30 13:54:13 -07001572static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001573{
1574 unsigned long rate;
1575
1576 clk_prepare_lock();
1577
Stephen Boydd6968fc2015-04-30 13:54:13 -07001578 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1579 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001580
Stephen Boydd6968fc2015-04-30 13:54:13 -07001581 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001582 clk_prepare_unlock();
1583
1584 return rate;
1585}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001586
Mike Turquetteb24764902012-03-15 23:11:19 -07001587/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001588 * clk_get_rate - return the rate of clk
1589 * @clk: the clk whose rate is being returned
1590 *
1591 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1592 * is set, which means a recalc_rate will be issued.
1593 * If clk is NULL then returns 0.
1594 */
1595unsigned long clk_get_rate(struct clk *clk)
1596{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001597 if (!clk)
1598 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001599
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001600 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001601}
1602EXPORT_SYMBOL_GPL(clk_get_rate);
1603
Stephen Boydd6968fc2015-04-30 13:54:13 -07001604static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001605 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001606{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001607 int i;
James Hogan4935b222013-07-29 12:24:59 +01001608
Masahiro Yamada508f8842015-12-28 19:23:08 +09001609 if (!parent)
1610 return -EINVAL;
1611
Derek Basehoreede77852018-12-20 16:31:00 -08001612 for (i = 0; i < core->num_parents; i++) {
Stephen Boyd1a079562019-04-30 10:22:30 -07001613 /* Found it first try! */
Stephen Boydfc0c2092019-04-12 11:31:47 -07001614 if (core->parents[i].core == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001615 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001616
Stephen Boyd1a079562019-04-30 10:22:30 -07001617 /* Something else is here, so keep looking */
Stephen Boydfc0c2092019-04-12 11:31:47 -07001618 if (core->parents[i].core)
Derek Basehoreede77852018-12-20 16:31:00 -08001619 continue;
1620
Stephen Boyd1a079562019-04-30 10:22:30 -07001621 /* Maybe core hasn't been cached but the hw is all we know? */
1622 if (core->parents[i].hw) {
1623 if (core->parents[i].hw == parent->hw)
1624 break;
1625
1626 /* Didn't match, but we're expecting a clk_hw */
1627 continue;
Derek Basehoreede77852018-12-20 16:31:00 -08001628 }
Stephen Boyd1a079562019-04-30 10:22:30 -07001629
1630 /* Maybe it hasn't been cached (clk_set_parent() path) */
1631 if (parent == clk_core_get(core, i))
1632 break;
1633
1634 /* Fallback to comparing globally unique names */
1635 if (!strcmp(parent->name, core->parents[i].name))
1636 break;
Derek Basehoreede77852018-12-20 16:31:00 -08001637 }
1638
Stephen Boyd1a079562019-04-30 10:22:30 -07001639 if (i == core->num_parents)
1640 return -EINVAL;
1641
1642 core->parents[i].core = parent;
1643 return i;
James Hogan4935b222013-07-29 12:24:59 +01001644}
1645
Heiko Stuebnere6500342015-04-22 22:53:05 +02001646/*
1647 * Update the orphan status of @core and all its children.
1648 */
1649static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1650{
1651 struct clk_core *child;
1652
1653 core->orphan = is_orphan;
1654
1655 hlist_for_each_entry(child, &core->children, child_node)
1656 clk_core_update_orphan_status(child, is_orphan);
1657}
1658
Stephen Boydd6968fc2015-04-30 13:54:13 -07001659static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001660{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001661 bool was_orphan = core->orphan;
1662
Stephen Boydd6968fc2015-04-30 13:54:13 -07001663 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001664
James Hogan903efc52013-08-29 12:10:51 +01001665 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001666 bool becomes_orphan = new_parent->orphan;
1667
James Hogan903efc52013-08-29 12:10:51 +01001668 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001669 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001670 new_parent->new_child = NULL;
1671
Stephen Boydd6968fc2015-04-30 13:54:13 -07001672 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001673
1674 if (was_orphan != becomes_orphan)
1675 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001676 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001677 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001678 if (!was_orphan)
1679 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001680 }
James Hogan4935b222013-07-29 12:24:59 +01001681
Stephen Boydd6968fc2015-04-30 13:54:13 -07001682 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001683}
1684
Stephen Boydd6968fc2015-04-30 13:54:13 -07001685static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001686 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001687{
1688 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001689 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001690
1691 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001692 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1693 *
1694 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001695 * clk_enable().
1696 *
1697 * If the clock is not prepared, then a race with
1698 * clk_enable/disable() is impossible since we already have the
1699 * prepare lock (future calls to clk_enable() need to be preceded by
1700 * a clk_prepare()).
1701 *
1702 * If the clock is prepared, migrate the prepared state to the new
1703 * parent and also protect against a race with clk_enable() by
1704 * forcing the clock and the new parent on. This ensures that all
1705 * future calls to clk_enable() are practically NOPs with respect to
1706 * hardware and software states.
1707 *
1708 * See also: Comment for clk_set_parent() below.
1709 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001710
1711 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1712 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1713 clk_core_prepare_enable(old_parent);
1714 clk_core_prepare_enable(parent);
1715 }
1716
1717 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001718 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001719 clk_core_prepare_enable(parent);
1720 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001721 }
1722
1723 /* update the clk tree topology */
1724 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001725 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001726 clk_enable_unlock(flags);
1727
Stephen Boyd3fa22522014-01-15 10:47:22 -08001728 return old_parent;
1729}
1730
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001731static void __clk_set_parent_after(struct clk_core *core,
1732 struct clk_core *parent,
1733 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001734{
1735 /*
1736 * Finish the migration of prepare state and undo the changes done
1737 * for preventing a race with clk_enable().
1738 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001739 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001740 clk_core_disable_lock(core);
1741 clk_core_disable_unprepare(old_parent);
1742 }
1743
1744 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1745 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1746 clk_core_disable_unprepare(parent);
1747 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001748 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001749}
1750
Stephen Boydd6968fc2015-04-30 13:54:13 -07001751static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001752 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001753{
1754 unsigned long flags;
1755 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001756 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001757
Stephen Boydd6968fc2015-04-30 13:54:13 -07001758 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001759
Stephen Boydd6968fc2015-04-30 13:54:13 -07001760 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001761
James Hogan4935b222013-07-29 12:24:59 +01001762 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001763 if (parent && core->ops->set_parent)
1764 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001765
Stephen Boydd6968fc2015-04-30 13:54:13 -07001766 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001767
James Hogan4935b222013-07-29 12:24:59 +01001768 if (ret) {
1769 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001770 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001771 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001772 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001773
James Hogan4935b222013-07-29 12:24:59 +01001774 return ret;
1775 }
1776
Stephen Boydd6968fc2015-04-30 13:54:13 -07001777 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001778
James Hogan4935b222013-07-29 12:24:59 +01001779 return 0;
1780}
1781
Ulf Hanssona093bde2012-08-31 14:21:28 +02001782/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001783 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001784 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001785 * @parent_rate: the "future" rate of clk's parent
1786 *
1787 * Walks the subtree of clks starting with clk, speculating rates as it
1788 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1789 *
1790 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1791 * pre-rate change notifications and returns early if no clks in the
1792 * subtree have subscribed to the notifications. Note that if a clk does not
1793 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001794 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001795 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001796static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001797 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001798{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001799 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001800 unsigned long new_rate;
1801 int ret = NOTIFY_DONE;
1802
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001803 lockdep_assert_held(&prepare_lock);
1804
Stephen Boydd6968fc2015-04-30 13:54:13 -07001805 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001806
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001807 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001808 if (core->notifier_count)
1809 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001810
Mike Turquette86bcfa22014-02-24 16:08:41 -08001811 if (ret & NOTIFY_STOP_MASK) {
1812 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001813 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001814 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001815 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001816
Stephen Boydd6968fc2015-04-30 13:54:13 -07001817 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001818 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001819 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001820 break;
1821 }
1822
1823out:
1824 return ret;
1825}
1826
Stephen Boydd6968fc2015-04-30 13:54:13 -07001827static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001828 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001829{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001830 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001831
Stephen Boydd6968fc2015-04-30 13:54:13 -07001832 core->new_rate = new_rate;
1833 core->new_parent = new_parent;
1834 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001835 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001836 core->new_child = NULL;
1837 if (new_parent && new_parent != core->parent)
1838 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001839
Stephen Boydd6968fc2015-04-30 13:54:13 -07001840 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001841 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001842 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001843 }
1844}
1845
1846/*
1847 * calculate the new rates returning the topmost clock that has to be
1848 * changed.
1849 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001850static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001851 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001852{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001853 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001854 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001855 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001856 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001857 unsigned long min_rate;
1858 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001859 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001860 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001861
Mike Turquette7452b212012-03-26 14:45:36 -07001862 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001863 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001864 return NULL;
1865
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001866 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001867 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001868 if (parent)
1869 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001870
Stephen Boydd6968fc2015-04-30 13:54:13 -07001871 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001872
James Hogan71472c02013-07-29 12:25:00 +01001873 /* find the closest rate and parent clk/rate */
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001874 if (clk_core_can_round(core)) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001875 struct clk_rate_request req;
1876
1877 req.rate = rate;
1878 req.min_rate = min_rate;
1879 req.max_rate = max_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001880
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001881 clk_core_init_rate_req(core, &req);
1882
1883 ret = clk_core_determine_round_nolock(core, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001884 if (ret < 0)
1885 return NULL;
1886
Boris Brezillon0817b622015-07-07 20:48:08 +02001887 best_parent_rate = req.best_parent_rate;
1888 new_rate = req.rate;
1889 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001890
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001891 if (new_rate < min_rate || new_rate > max_rate)
1892 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001893 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001894 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001895 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001896 return NULL;
1897 } else {
1898 /* pass-through clock with adjustable parent */
1899 top = clk_calc_new_rates(parent, rate);
1900 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001901 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001902 }
1903
James Hogan71472c02013-07-29 12:25:00 +01001904 /* some clocks must be gated to change parent */
1905 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001906 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001907 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001908 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001909 return NULL;
1910 }
1911
James Hogan71472c02013-07-29 12:25:00 +01001912 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001913 if (parent && core->num_parents > 1) {
1914 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001915 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001916 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001917 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001918 return NULL;
1919 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001920 }
1921
Stephen Boydd6968fc2015-04-30 13:54:13 -07001922 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001923 best_parent_rate != parent->rate)
1924 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001925
1926out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001927 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001928
1929 return top;
1930}
1931
1932/*
1933 * Notify about rate changes in a subtree. Always walk down the whole tree
1934 * so that in case of an error we can walk down the whole tree again and
1935 * abort the change.
1936 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001937static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001938 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001939{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001940 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001941 int ret = NOTIFY_DONE;
1942
Stephen Boydd6968fc2015-04-30 13:54:13 -07001943 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301944 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001945
Stephen Boydd6968fc2015-04-30 13:54:13 -07001946 if (core->notifier_count) {
1947 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001948 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001949 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001950 }
1951
Stephen Boydd6968fc2015-04-30 13:54:13 -07001952 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001953 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001954 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001955 continue;
1956 tmp_clk = clk_propagate_rate_change(child, event);
1957 if (tmp_clk)
1958 fail_clk = tmp_clk;
1959 }
1960
Stephen Boydd6968fc2015-04-30 13:54:13 -07001961 /* handle the new child who might not be in core->children yet */
1962 if (core->new_child) {
1963 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001964 if (tmp_clk)
1965 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001966 }
1967
1968 return fail_clk;
1969}
1970
1971/*
1972 * walk down a subtree and set the new rates notifying the rate
1973 * change on the way
1974 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001975static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001976{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001977 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001978 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001979 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001980 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001981 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001982 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001983 struct clk_core *parent = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001984
Stephen Boydd6968fc2015-04-30 13:54:13 -07001985 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001986
Dong Aishengfc8726a2016-06-30 17:31:14 +08001987 if (core->new_parent) {
1988 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001989 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001990 } else if (core->parent) {
1991 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001992 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001993 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001994
Marek Szyprowski588fb542017-11-30 13:14:51 +01001995 if (clk_pm_runtime_get(core))
1996 return;
1997
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001998 if (core->flags & CLK_SET_RATE_UNGATE) {
1999 unsigned long flags;
2000
2001 clk_core_prepare(core);
2002 flags = clk_enable_lock();
2003 clk_core_enable(core);
2004 clk_enable_unlock(flags);
2005 }
2006
Stephen Boydd6968fc2015-04-30 13:54:13 -07002007 if (core->new_parent && core->new_parent != core->parent) {
2008 old_parent = __clk_set_parent_before(core, core->new_parent);
2009 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002010
Stephen Boydd6968fc2015-04-30 13:54:13 -07002011 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002012 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002013 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08002014 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07002015 core->new_parent_index);
2016 } else if (core->ops->set_parent) {
2017 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002018 }
2019
Stephen Boydd6968fc2015-04-30 13:54:13 -07002020 trace_clk_set_parent_complete(core, core->new_parent);
2021 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002022 }
2023
Dong Aishengfc8726a2016-06-30 17:31:14 +08002024 if (core->flags & CLK_OPS_PARENT_ENABLE)
2025 clk_core_prepare_enable(parent);
2026
Stephen Boydd6968fc2015-04-30 13:54:13 -07002027 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002028
Stephen Boydd6968fc2015-04-30 13:54:13 -07002029 if (!skip_set_rate && core->ops->set_rate)
2030 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002031
Stephen Boydd6968fc2015-04-30 13:54:13 -07002032 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002033
Stephen Boydd6968fc2015-04-30 13:54:13 -07002034 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002035
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01002036 if (core->flags & CLK_SET_RATE_UNGATE) {
2037 unsigned long flags;
2038
2039 flags = clk_enable_lock();
2040 clk_core_disable(core);
2041 clk_enable_unlock(flags);
2042 clk_core_unprepare(core);
2043 }
2044
Dong Aishengfc8726a2016-06-30 17:31:14 +08002045 if (core->flags & CLK_OPS_PARENT_ENABLE)
2046 clk_core_disable_unprepare(parent);
2047
Stephen Boydd6968fc2015-04-30 13:54:13 -07002048 if (core->notifier_count && old_rate != core->rate)
2049 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002050
Michael Turquette85e88fa2015-06-20 12:18:03 -07002051 if (core->flags & CLK_RECALC_NEW_RATES)
2052 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02002053
Tero Kristo067bb172014-08-21 16:47:45 +03002054 /*
2055 * Use safe iteration, as change_rate can actually swap parents
2056 * for certain clock types.
2057 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002058 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01002059 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002060 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01002061 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07002062 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01002063 }
2064
Stephen Boydd6968fc2015-04-30 13:54:13 -07002065 /* handle the new child who might not be in core->children yet */
2066 if (core->new_child)
2067 clk_change_rate(core->new_child);
Marek Szyprowski588fb542017-11-30 13:14:51 +01002068
2069 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002070}
2071
Jerome Brunetca5e0892017-12-01 22:51:55 +01002072static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
2073 unsigned long req_rate)
2074{
Jerome Brunete55a8392017-12-01 22:51:56 +01002075 int ret, cnt;
Jerome Brunetca5e0892017-12-01 22:51:55 +01002076 struct clk_rate_request req;
2077
2078 lockdep_assert_held(&prepare_lock);
2079
2080 if (!core)
2081 return 0;
2082
Jerome Brunete55a8392017-12-01 22:51:56 +01002083 /* simulate what the rate would be if it could be freely set */
2084 cnt = clk_core_rate_nuke_protect(core);
2085 if (cnt < 0)
2086 return cnt;
2087
Jerome Brunetca5e0892017-12-01 22:51:55 +01002088 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
2089 req.rate = req_rate;
2090
2091 ret = clk_core_round_rate_nolock(core, &req);
2092
Jerome Brunete55a8392017-12-01 22:51:56 +01002093 /* restore the protection */
2094 clk_core_rate_restore_protect(core, cnt);
2095
Jerome Brunetca5e0892017-12-01 22:51:55 +01002096 return ret ? 0 : req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002097}
2098
Stephen Boydd6968fc2015-04-30 13:54:13 -07002099static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002100 unsigned long req_rate)
2101{
2102 struct clk_core *top, *fail_clk;
Jerome Brunetca5e0892017-12-01 22:51:55 +01002103 unsigned long rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002104 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002105
Stephen Boydd6968fc2015-04-30 13:54:13 -07002106 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002107 return 0;
2108
Jerome Brunetca5e0892017-12-01 22:51:55 +01002109 rate = clk_core_req_round_rate_nolock(core, req_rate);
2110
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002111 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002112 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002113 return 0;
2114
Jerome Brunete55a8392017-12-01 22:51:56 +01002115 /* fail on a direct rate set of a protected provider */
2116 if (clk_core_rate_is_protected(core))
2117 return -EBUSY;
2118
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002119 /* calculate new rates and get the topmost changed clock */
Jerome Brunetca5e0892017-12-01 22:51:55 +01002120 top = clk_calc_new_rates(core, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002121 if (!top)
2122 return -EINVAL;
2123
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002124 ret = clk_pm_runtime_get(core);
2125 if (ret)
2126 return ret;
2127
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002128 /* notify that we are about to change rates */
2129 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
2130 if (fail_clk) {
2131 pr_debug("%s: failed to set %s rate\n", __func__,
2132 fail_clk->name);
2133 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002134 ret = -EBUSY;
2135 goto err;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002136 }
2137
2138 /* change the rates */
2139 clk_change_rate(top);
2140
Stephen Boydd6968fc2015-04-30 13:54:13 -07002141 core->req_rate = req_rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002142err:
2143 clk_pm_runtime_put(core);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002144
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002145 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002146}
2147
Mike Turquetteb24764902012-03-15 23:11:19 -07002148/**
2149 * clk_set_rate - specify a new rate for clk
2150 * @clk: the clk whose rate is being changed
2151 * @rate: the new rate for clk
2152 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002153 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07002154 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002155 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2156 * propagate up to clk's parent; whether or not this happens depends on the
2157 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2158 * after calling .round_rate then upstream parent propagation is ignored. If
2159 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002160 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07002161 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2162 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07002163 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002164 * Rate changes are accomplished via tree traversal that also recalculates the
2165 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07002166 *
2167 * Returns 0 on success, -EERROR otherwise.
2168 */
2169int clk_set_rate(struct clk *clk, unsigned long rate)
2170{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002171 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002172
Mike Turquette89ac8d72013-08-21 23:58:09 -07002173 if (!clk)
2174 return 0;
2175
Mike Turquetteb24764902012-03-15 23:11:19 -07002176 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002177 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002178
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002179 if (clk->exclusive_count)
2180 clk_core_rate_unprotect(clk->core);
2181
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002182 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002183
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002184 if (clk->exclusive_count)
2185 clk_core_rate_protect(clk->core);
2186
Mike Turquetteeab89f62013-03-28 13:59:01 -07002187 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002188
2189 return ret;
2190}
2191EXPORT_SYMBOL_GPL(clk_set_rate);
2192
2193/**
Geert Uytterhoeven65e22182019-06-17 15:56:02 +02002194 * clk_set_rate_exclusive - specify a new rate and get exclusive control
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002195 * @clk: the clk whose rate is being changed
2196 * @rate: the new rate for clk
2197 *
2198 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2199 * within a critical section
2200 *
2201 * This can be used initially to ensure that at least 1 consumer is
Geert Uytterhoeven65e22182019-06-17 15:56:02 +02002202 * satisfied when several consumers are competing for exclusivity over the
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002203 * same clock provider.
2204 *
2205 * The exclusivity is not applied if setting the rate failed.
2206 *
2207 * Calls to clk_rate_exclusive_get() should be balanced with calls to
2208 * clk_rate_exclusive_put().
2209 *
2210 * Returns 0 on success, -EERROR otherwise.
2211 */
2212int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2213{
2214 int ret;
2215
2216 if (!clk)
2217 return 0;
2218
2219 /* prevent racing with updates to the clock topology */
2220 clk_prepare_lock();
2221
2222 /*
2223 * The temporary protection removal is not here, on purpose
2224 * This function is meant to be used instead of clk_rate_protect,
2225 * so before the consumer code path protect the clock provider
2226 */
2227
2228 ret = clk_core_set_rate_nolock(clk->core, rate);
2229 if (!ret) {
2230 clk_core_rate_protect(clk->core);
2231 clk->exclusive_count++;
2232 }
2233
2234 clk_prepare_unlock();
2235
2236 return ret;
2237}
2238EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2239
2240/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002241 * clk_set_rate_range - set a rate range for a clock source
2242 * @clk: clock source
2243 * @min: desired minimum clock rate in Hz, inclusive
2244 * @max: desired maximum clock rate in Hz, inclusive
2245 *
2246 * Returns success (0) or negative errno.
2247 */
2248int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2249{
2250 int ret = 0;
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002251 unsigned long old_min, old_max, rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002252
2253 if (!clk)
2254 return 0;
2255
2256 if (min > max) {
2257 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2258 __func__, clk->core->name, clk->dev_id, clk->con_id,
2259 min, max);
2260 return -EINVAL;
2261 }
2262
2263 clk_prepare_lock();
2264
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002265 if (clk->exclusive_count)
2266 clk_core_rate_unprotect(clk->core);
2267
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002268 /* Save the current values in case we need to rollback the change */
2269 old_min = clk->min_rate;
2270 old_max = clk->max_rate;
2271 clk->min_rate = min;
2272 clk->max_rate = max;
2273
2274 rate = clk_core_get_rate_nolock(clk->core);
2275 if (rate < min || rate > max) {
2276 /*
2277 * FIXME:
2278 * We are in bit of trouble here, current rate is outside the
2279 * the requested range. We are going try to request appropriate
2280 * range boundary but there is a catch. It may fail for the
2281 * usual reason (clock broken, clock protected, etc) but also
2282 * because:
2283 * - round_rate() was not favorable and fell on the wrong
2284 * side of the boundary
2285 * - the determine_rate() callback does not really check for
2286 * this corner case when determining the rate
2287 */
2288
2289 if (rate < min)
2290 rate = min;
2291 else
2292 rate = max;
2293
2294 ret = clk_core_set_rate_nolock(clk->core, rate);
2295 if (ret) {
2296 /* rollback the changes */
2297 clk->min_rate = old_min;
2298 clk->max_rate = old_max;
2299 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002300 }
2301
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002302 if (clk->exclusive_count)
2303 clk_core_rate_protect(clk->core);
2304
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002305 clk_prepare_unlock();
2306
2307 return ret;
2308}
2309EXPORT_SYMBOL_GPL(clk_set_rate_range);
2310
2311/**
2312 * clk_set_min_rate - set a minimum clock rate for a clock source
2313 * @clk: clock source
2314 * @rate: desired minimum clock rate in Hz, inclusive
2315 *
2316 * Returns success (0) or negative errno.
2317 */
2318int clk_set_min_rate(struct clk *clk, unsigned long rate)
2319{
2320 if (!clk)
2321 return 0;
2322
2323 return clk_set_rate_range(clk, rate, clk->max_rate);
2324}
2325EXPORT_SYMBOL_GPL(clk_set_min_rate);
2326
2327/**
2328 * clk_set_max_rate - set a maximum clock rate for a clock source
2329 * @clk: clock source
2330 * @rate: desired maximum clock rate in Hz, inclusive
2331 *
2332 * Returns success (0) or negative errno.
2333 */
2334int clk_set_max_rate(struct clk *clk, unsigned long rate)
2335{
2336 if (!clk)
2337 return 0;
2338
2339 return clk_set_rate_range(clk, clk->min_rate, rate);
2340}
2341EXPORT_SYMBOL_GPL(clk_set_max_rate);
2342
2343/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002344 * clk_get_parent - return the parent of a clk
2345 * @clk: the clk whose parent gets returned
2346 *
2347 * Simply returns clk->parent. Returns NULL if clk is NULL.
2348 */
2349struct clk *clk_get_parent(struct clk *clk)
2350{
2351 struct clk *parent;
2352
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002353 if (!clk)
2354 return NULL;
2355
Mike Turquetteeab89f62013-03-28 13:59:01 -07002356 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002357 /* TODO: Create a per-user clk and change callers to call clk_put */
2358 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002359 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002360
2361 return parent;
2362}
2363EXPORT_SYMBOL_GPL(clk_get_parent);
2364
Stephen Boydd6968fc2015-04-30 13:54:13 -07002365static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002366{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002367 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002368
Masahiro Yamada2430a942016-02-09 20:19:14 +09002369 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002370 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002371
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002372 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002373}
2374
Stephen Boydd6968fc2015-04-30 13:54:13 -07002375static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002376 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002377{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002378 clk_reparent(core, new_parent);
2379 __clk_recalc_accuracies(core);
2380 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002381}
2382
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002383void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2384{
2385 if (!hw)
2386 return;
2387
2388 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2389}
2390
Mike Turquetteb24764902012-03-15 23:11:19 -07002391/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002392 * clk_has_parent - check if a clock is a possible parent for another
2393 * @clk: clock source
2394 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002395 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002396 * This function can be used in drivers that need to check that a clock can be
2397 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002398 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002399 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002400 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002401bool clk_has_parent(struct clk *clk, struct clk *parent)
2402{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002403 struct clk_core *core, *parent_core;
Stephen Boydfc0c2092019-04-12 11:31:47 -07002404 int i;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002405
2406 /* NULL clocks should be nops, so return success if either is NULL. */
2407 if (!clk || !parent)
2408 return true;
2409
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002410 core = clk->core;
2411 parent_core = parent->core;
2412
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002413 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002414 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002415 return true;
2416
Stephen Boydfc0c2092019-04-12 11:31:47 -07002417 for (i = 0; i < core->num_parents; i++)
2418 if (!strcmp(core->parents[i].name, parent_core->name))
2419 return true;
2420
2421 return false;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002422}
2423EXPORT_SYMBOL_GPL(clk_has_parent);
2424
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002425static int clk_core_set_parent_nolock(struct clk_core *core,
2426 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002427{
2428 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002429 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002430 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002431
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002432 lockdep_assert_held(&prepare_lock);
2433
Stephen Boydd6968fc2015-04-30 13:54:13 -07002434 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002435 return 0;
2436
Stephen Boydd6968fc2015-04-30 13:54:13 -07002437 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002438 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002439
Stephen Boydb61c43c2015-02-02 14:11:25 -08002440 /* verify ops for for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002441 if (core->num_parents > 1 && !core->ops->set_parent)
2442 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08002443
Ulf Hansson031dcc92013-04-02 23:09:38 +02002444 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002445 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2446 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002447
Jerome Brunete55a8392017-12-01 22:51:56 +01002448 if (clk_core_rate_is_protected(core))
2449 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002450
2451 /* try finding the new parent index */
2452 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002453 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002454 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002455 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002456 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002457 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002458 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002459 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002460 }
2461
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002462 ret = clk_pm_runtime_get(core);
2463 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002464 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002465
Mike Turquetteb24764902012-03-15 23:11:19 -07002466 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002467 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002468
2469 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002470 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002471 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07002472
Ulf Hansson031dcc92013-04-02 23:09:38 +02002473 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002474 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002475
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002476 /* propagate rate an accuracy recalculation accordingly */
2477 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002478 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002479 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002480 __clk_recalc_rates(core, POST_RATE_CHANGE);
2481 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002482 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002483
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002484runtime_put:
2485 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002486
2487 return ret;
2488}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002489
2490/**
2491 * clk_set_parent - switch the parent of a mux clk
2492 * @clk: the mux clk whose input we are switching
2493 * @parent: the new input to clk
2494 *
2495 * Re-parent clk to use parent as its new input source. If clk is in
2496 * prepared state, the clk will get enabled for the duration of this call. If
2497 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2498 * that, the reparenting is glitchy in hardware, etc), use the
2499 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2500 *
2501 * After successfully changing clk's parent clk_set_parent will update the
2502 * clk topology, sysfs topology and propagate rate recalculation via
2503 * __clk_recalc_rates.
2504 *
2505 * Returns 0 on success, -EERROR otherwise.
2506 */
2507int clk_set_parent(struct clk *clk, struct clk *parent)
2508{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002509 int ret;
2510
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002511 if (!clk)
2512 return 0;
2513
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002514 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002515
2516 if (clk->exclusive_count)
2517 clk_core_rate_unprotect(clk->core);
2518
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002519 ret = clk_core_set_parent_nolock(clk->core,
2520 parent ? parent->core : NULL);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002521
2522 if (clk->exclusive_count)
2523 clk_core_rate_protect(clk->core);
2524
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002525 clk_prepare_unlock();
2526
2527 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002528}
Mike Turquetteb24764902012-03-15 23:11:19 -07002529EXPORT_SYMBOL_GPL(clk_set_parent);
2530
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002531static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2532{
2533 int ret = -EINVAL;
2534
2535 lockdep_assert_held(&prepare_lock);
2536
2537 if (!core)
2538 return 0;
2539
Jerome Brunete55a8392017-12-01 22:51:56 +01002540 if (clk_core_rate_is_protected(core))
2541 return -EBUSY;
2542
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002543 trace_clk_set_phase(core, degrees);
2544
Shawn Lin7f95bee2018-03-08 14:49:41 +08002545 if (core->ops->set_phase) {
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002546 ret = core->ops->set_phase(core->hw, degrees);
Shawn Lin7f95bee2018-03-08 14:49:41 +08002547 if (!ret)
2548 core->phase = degrees;
2549 }
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002550
2551 trace_clk_set_phase_complete(core, degrees);
2552
2553 return ret;
2554}
2555
Mike Turquetteb24764902012-03-15 23:11:19 -07002556/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002557 * clk_set_phase - adjust the phase shift of a clock signal
2558 * @clk: clock signal source
2559 * @degrees: number of degrees the signal is shifted
2560 *
2561 * Shifts the phase of a clock signal by the specified
2562 * degrees. Returns 0 on success, -EERROR otherwise.
2563 *
2564 * This function makes no distinction about the input or reference
2565 * signal that we adjust the clock signal phase against. For example
2566 * phase locked-loop clock signal generators we may shift phase with
2567 * respect to feedback clock signal input, but for other cases the
2568 * clock phase may be shifted with respect to some other, unspecified
2569 * signal.
2570 *
2571 * Additionally the concept of phase shift does not propagate through
2572 * the clock tree hierarchy, which sets it apart from clock rates and
2573 * clock accuracy. A parent clock phase attribute does not have an
2574 * impact on the phase attribute of a child clock.
2575 */
2576int clk_set_phase(struct clk *clk, int degrees)
2577{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002578 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002579
2580 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002581 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002582
2583 /* sanity check degrees */
2584 degrees %= 360;
2585 if (degrees < 0)
2586 degrees += 360;
2587
2588 clk_prepare_lock();
2589
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002590 if (clk->exclusive_count)
2591 clk_core_rate_unprotect(clk->core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002592
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002593 ret = clk_core_set_phase_nolock(clk->core, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002594
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002595 if (clk->exclusive_count)
2596 clk_core_rate_protect(clk->core);
Mike Turquettee59c5372014-02-18 21:21:25 -08002597
Mike Turquettee59c5372014-02-18 21:21:25 -08002598 clk_prepare_unlock();
2599
Mike Turquettee59c5372014-02-18 21:21:25 -08002600 return ret;
2601}
Maxime Ripard9767b04f2015-01-20 22:23:43 +01002602EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002603
Stephen Boydd6968fc2015-04-30 13:54:13 -07002604static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002605{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002606 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002607
2608 clk_prepare_lock();
Shawn Lin1f9c63e2018-03-14 08:28:31 +08002609 /* Always try to update cached phase if possible */
2610 if (core->ops->get_phase)
2611 core->phase = core->ops->get_phase(core->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002612 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002613 clk_prepare_unlock();
2614
Mike Turquettee59c5372014-02-18 21:21:25 -08002615 return ret;
2616}
2617
2618/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002619 * clk_get_phase - return the phase shift of a clock signal
2620 * @clk: clock signal source
2621 *
2622 * Returns the phase shift of a clock node in degrees, otherwise returns
2623 * -EERROR.
2624 */
2625int clk_get_phase(struct clk *clk)
2626{
2627 if (!clk)
2628 return 0;
2629
2630 return clk_core_get_phase(clk->core);
2631}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002632EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002633
Jerome Brunet9fba7382018-06-19 16:41:41 +02002634static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2635{
2636 /* Assume a default value of 50% */
2637 core->duty.num = 1;
2638 core->duty.den = 2;
2639}
2640
2641static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2642
2643static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2644{
2645 struct clk_duty *duty = &core->duty;
2646 int ret = 0;
2647
2648 if (!core->ops->get_duty_cycle)
2649 return clk_core_update_duty_cycle_parent_nolock(core);
2650
2651 ret = core->ops->get_duty_cycle(core->hw, duty);
2652 if (ret)
2653 goto reset;
2654
2655 /* Don't trust the clock provider too much */
2656 if (duty->den == 0 || duty->num > duty->den) {
2657 ret = -EINVAL;
2658 goto reset;
2659 }
2660
2661 return 0;
2662
2663reset:
2664 clk_core_reset_duty_cycle_nolock(core);
2665 return ret;
2666}
2667
2668static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2669{
2670 int ret = 0;
2671
2672 if (core->parent &&
2673 core->flags & CLK_DUTY_CYCLE_PARENT) {
2674 ret = clk_core_update_duty_cycle_nolock(core->parent);
2675 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2676 } else {
2677 clk_core_reset_duty_cycle_nolock(core);
2678 }
2679
2680 return ret;
2681}
2682
2683static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2684 struct clk_duty *duty);
2685
2686static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2687 struct clk_duty *duty)
2688{
2689 int ret;
2690
2691 lockdep_assert_held(&prepare_lock);
2692
2693 if (clk_core_rate_is_protected(core))
2694 return -EBUSY;
2695
2696 trace_clk_set_duty_cycle(core, duty);
2697
2698 if (!core->ops->set_duty_cycle)
2699 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2700
2701 ret = core->ops->set_duty_cycle(core->hw, duty);
2702 if (!ret)
2703 memcpy(&core->duty, duty, sizeof(*duty));
2704
2705 trace_clk_set_duty_cycle_complete(core, duty);
2706
2707 return ret;
2708}
2709
2710static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2711 struct clk_duty *duty)
2712{
2713 int ret = 0;
2714
2715 if (core->parent &&
2716 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2717 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2718 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2719 }
2720
2721 return ret;
2722}
2723
2724/**
2725 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2726 * @clk: clock signal source
2727 * @num: numerator of the duty cycle ratio to be applied
2728 * @den: denominator of the duty cycle ratio to be applied
2729 *
2730 * Apply the duty cycle ratio if the ratio is valid and the clock can
2731 * perform this operation
2732 *
2733 * Returns (0) on success, a negative errno otherwise.
2734 */
2735int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2736{
2737 int ret;
2738 struct clk_duty duty;
2739
2740 if (!clk)
2741 return 0;
2742
2743 /* sanity check the ratio */
2744 if (den == 0 || num > den)
2745 return -EINVAL;
2746
2747 duty.num = num;
2748 duty.den = den;
2749
2750 clk_prepare_lock();
2751
2752 if (clk->exclusive_count)
2753 clk_core_rate_unprotect(clk->core);
2754
2755 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2756
2757 if (clk->exclusive_count)
2758 clk_core_rate_protect(clk->core);
2759
2760 clk_prepare_unlock();
2761
2762 return ret;
2763}
2764EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2765
2766static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2767 unsigned int scale)
2768{
2769 struct clk_duty *duty = &core->duty;
2770 int ret;
2771
2772 clk_prepare_lock();
2773
2774 ret = clk_core_update_duty_cycle_nolock(core);
2775 if (!ret)
2776 ret = mult_frac(scale, duty->num, duty->den);
2777
2778 clk_prepare_unlock();
2779
2780 return ret;
2781}
2782
2783/**
2784 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2785 * @clk: clock signal source
2786 * @scale: scaling factor to be applied to represent the ratio as an integer
2787 *
2788 * Returns the duty cycle ratio of a clock node multiplied by the provided
2789 * scaling factor, or negative errno on error.
2790 */
2791int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2792{
2793 if (!clk)
2794 return 0;
2795
2796 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2797}
2798EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2799
Mike Turquetteb24764902012-03-15 23:11:19 -07002800/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002801 * clk_is_match - check if two clk's point to the same hardware clock
2802 * @p: clk compared against q
2803 * @q: clk compared against p
2804 *
2805 * Returns true if the two struct clk pointers both point to the same hardware
2806 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2807 * share the same struct clk_core object.
2808 *
2809 * Returns false otherwise. Note that two NULL clks are treated as matching.
2810 */
2811bool clk_is_match(const struct clk *p, const struct clk *q)
2812{
2813 /* trivial case: identical struct clk's or both NULL */
2814 if (p == q)
2815 return true;
2816
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002817 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002818 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2819 if (p->core == q->core)
2820 return true;
2821
2822 return false;
2823}
2824EXPORT_SYMBOL_GPL(clk_is_match);
2825
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002826/*** debugfs support ***/
2827
2828#ifdef CONFIG_DEBUG_FS
2829#include <linux/debugfs.h>
2830
2831static struct dentry *rootdir;
2832static int inited = 0;
2833static DEFINE_MUTEX(clk_debug_lock);
2834static HLIST_HEAD(clk_debug_list);
2835
2836static struct hlist_head *all_lists[] = {
2837 &clk_root_list,
2838 &clk_orphan_list,
2839 NULL,
2840};
2841
2842static struct hlist_head *orphan_list[] = {
2843 &clk_orphan_list,
2844 NULL,
2845};
2846
2847static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2848 int level)
2849{
2850 if (!c)
2851 return;
2852
Jerome Brunet9fba7382018-06-19 16:41:41 +02002853 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002854 level * 3 + 1, "",
2855 30 - level * 3, c->name,
Jerome Brunete55a8392017-12-01 22:51:56 +01002856 c->enable_count, c->prepare_count, c->protect_count,
2857 clk_core_get_rate(c), clk_core_get_accuracy(c),
Jerome Brunet9fba7382018-06-19 16:41:41 +02002858 clk_core_get_phase(c),
2859 clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002860}
2861
2862static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2863 int level)
2864{
2865 struct clk_core *child;
2866
2867 if (!c)
2868 return;
2869
2870 clk_summary_show_one(s, c, level);
2871
2872 hlist_for_each_entry(child, &c->children, child_node)
2873 clk_summary_show_subtree(s, child, level + 1);
2874}
2875
2876static int clk_summary_show(struct seq_file *s, void *data)
2877{
2878 struct clk_core *c;
2879 struct hlist_head **lists = (struct hlist_head **)s->private;
2880
Jerome Brunet9fba7382018-06-19 16:41:41 +02002881 seq_puts(s, " enable prepare protect duty\n");
2882 seq_puts(s, " clock count count count rate accuracy phase cycle\n");
2883 seq_puts(s, "---------------------------------------------------------------------------------------------\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002884
2885 clk_prepare_lock();
2886
2887 for (; *lists; lists++)
2888 hlist_for_each_entry(c, *lists, child_node)
2889 clk_summary_show_subtree(s, c, 0);
2890
2891 clk_prepare_unlock();
2892
2893 return 0;
2894}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002895DEFINE_SHOW_ATTRIBUTE(clk_summary);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002896
2897static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2898{
Leonard Crestez1bd37a42019-07-02 16:27:09 +03002899 unsigned long min_rate, max_rate;
2900
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002901 if (!c)
2902 return;
2903
Leonard Crestez1bd37a42019-07-02 16:27:09 +03002904 clk_core_get_boundaries(c, &min_rate, &max_rate);
2905
Stefan Wahren7cb81132015-04-29 16:36:43 +00002906 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002907 seq_printf(s, "\"%s\": { ", c->name);
2908 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2909 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002910 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002911 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
Leonard Crestez1bd37a42019-07-02 16:27:09 +03002912 seq_printf(s, "\"min_rate\": %lu,", min_rate);
2913 seq_printf(s, "\"max_rate\": %lu,", max_rate);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002914 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Lubomir Rintelc6e90992019-01-04 23:05:49 +01002915 seq_printf(s, "\"phase\": %d,", clk_core_get_phase(c));
Jerome Brunet9fba7382018-06-19 16:41:41 +02002916 seq_printf(s, "\"duty_cycle\": %u",
2917 clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002918}
2919
2920static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2921{
2922 struct clk_core *child;
2923
2924 if (!c)
2925 return;
2926
2927 clk_dump_one(s, c, level);
2928
2929 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002930 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002931 clk_dump_subtree(s, child, level + 1);
2932 }
2933
Markus Elfring4d327582017-04-20 08:45:43 +02002934 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002935}
2936
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002937static int clk_dump_show(struct seq_file *s, void *data)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002938{
2939 struct clk_core *c;
2940 bool first_node = true;
2941 struct hlist_head **lists = (struct hlist_head **)s->private;
2942
Markus Elfring4d327582017-04-20 08:45:43 +02002943 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002944 clk_prepare_lock();
2945
2946 for (; *lists; lists++) {
2947 hlist_for_each_entry(c, *lists, child_node) {
2948 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02002949 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002950 first_node = false;
2951 clk_dump_subtree(s, c, 0);
2952 }
2953 }
2954
2955 clk_prepare_unlock();
2956
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002957 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002958 return 0;
2959}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002960DEFINE_SHOW_ATTRIBUTE(clk_dump);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002961
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002962static const struct {
2963 unsigned long flag;
2964 const char *name;
2965} clk_flags[] = {
Geert Uytterhoeven40dd71c2018-07-06 17:16:54 +02002966#define ENTRY(f) { f, #f }
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002967 ENTRY(CLK_SET_RATE_GATE),
2968 ENTRY(CLK_SET_PARENT_GATE),
2969 ENTRY(CLK_SET_RATE_PARENT),
2970 ENTRY(CLK_IGNORE_UNUSED),
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002971 ENTRY(CLK_GET_RATE_NOCACHE),
2972 ENTRY(CLK_SET_RATE_NO_REPARENT),
2973 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2974 ENTRY(CLK_RECALC_NEW_RATES),
2975 ENTRY(CLK_SET_RATE_UNGATE),
2976 ENTRY(CLK_IS_CRITICAL),
2977 ENTRY(CLK_OPS_PARENT_ENABLE),
Jerome Brunet9fba7382018-06-19 16:41:41 +02002978 ENTRY(CLK_DUTY_CYCLE_PARENT),
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002979#undef ENTRY
2980};
2981
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002982static int clk_flags_show(struct seq_file *s, void *data)
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002983{
2984 struct clk_core *core = s->private;
2985 unsigned long flags = core->flags;
2986 unsigned int i;
2987
2988 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2989 if (flags & clk_flags[i].flag) {
2990 seq_printf(s, "%s\n", clk_flags[i].name);
2991 flags &= ~clk_flags[i].flag;
2992 }
2993 }
2994 if (flags) {
2995 /* Unknown flags */
2996 seq_printf(s, "0x%lx\n", flags);
2997 }
2998
2999 return 0;
3000}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02003001DEFINE_SHOW_ATTRIBUTE(clk_flags);
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01003002
Stephen Boyd11f6c232019-06-24 20:01:55 -07003003static void possible_parent_show(struct seq_file *s, struct clk_core *core,
3004 unsigned int i, char terminator)
Peter De Schrijver92031572017-03-21 15:20:31 +02003005{
Chen-Yu Tsai2d156b72019-05-03 11:15:09 +08003006 struct clk_core *parent;
Peter De Schrijver92031572017-03-21 15:20:31 +02003007
Chen-Yu Tsai2d156b72019-05-03 11:15:09 +08003008 /*
3009 * Go through the following options to fetch a parent's name.
3010 *
3011 * 1. Fetch the registered parent clock and use its name
3012 * 2. Use the global (fallback) name if specified
3013 * 3. Use the local fw_name if provided
3014 * 4. Fetch parent clock's clock-output-name if DT index was set
3015 *
3016 * This may still fail in some cases, such as when the parent is
3017 * specified directly via a struct clk_hw pointer, but it isn't
3018 * registered (yet).
3019 */
Chen-Yu Tsai2d156b72019-05-03 11:15:09 +08003020 parent = clk_core_get_parent_by_index(core, i);
3021 if (parent)
3022 seq_printf(s, "%s", parent->name);
3023 else if (core->parents[i].name)
3024 seq_printf(s, "%s", core->parents[i].name);
3025 else if (core->parents[i].fw_name)
3026 seq_printf(s, "<%s>(fw)", core->parents[i].fw_name);
3027 else if (core->parents[i].index >= 0)
3028 seq_printf(s, "%s",
3029 of_clk_get_parent_name(core->of_node,
3030 core->parents[i].index));
3031 else
3032 seq_puts(s, "(missing)");
Peter De Schrijver92031572017-03-21 15:20:31 +02003033
Stephen Boyd11f6c232019-06-24 20:01:55 -07003034 seq_putc(s, terminator);
3035}
3036
Peter De Schrijver92031572017-03-21 15:20:31 +02003037static int possible_parents_show(struct seq_file *s, void *data)
3038{
3039 struct clk_core *core = s->private;
3040 int i;
3041
3042 for (i = 0; i < core->num_parents - 1; i++)
Stephen Boyd11f6c232019-06-24 20:01:55 -07003043 possible_parent_show(s, core, i, ' ');
Peter De Schrijver92031572017-03-21 15:20:31 +02003044
Stephen Boyd11f6c232019-06-24 20:01:55 -07003045 possible_parent_show(s, core, i, '\n');
Peter De Schrijver92031572017-03-21 15:20:31 +02003046
3047 return 0;
3048}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02003049DEFINE_SHOW_ATTRIBUTE(possible_parents);
Peter De Schrijver92031572017-03-21 15:20:31 +02003050
Leonard Cresteze5e89242019-06-10 14:06:38 +03003051static int current_parent_show(struct seq_file *s, void *data)
3052{
3053 struct clk_core *core = s->private;
3054
3055 if (core->parent)
3056 seq_printf(s, "%s\n", core->parent->name);
3057
3058 return 0;
3059}
3060DEFINE_SHOW_ATTRIBUTE(current_parent);
3061
Jerome Brunet9fba7382018-06-19 16:41:41 +02003062static int clk_duty_cycle_show(struct seq_file *s, void *data)
3063{
3064 struct clk_core *core = s->private;
3065 struct clk_duty *duty = &core->duty;
3066
3067 seq_printf(s, "%u/%u\n", duty->num, duty->den);
3068
3069 return 0;
3070}
3071DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
3072
Leonard Crestez1bd37a42019-07-02 16:27:09 +03003073static int clk_min_rate_show(struct seq_file *s, void *data)
3074{
3075 struct clk_core *core = s->private;
3076 unsigned long min_rate, max_rate;
3077
3078 clk_prepare_lock();
3079 clk_core_get_boundaries(core, &min_rate, &max_rate);
3080 clk_prepare_unlock();
3081 seq_printf(s, "%lu\n", min_rate);
3082
3083 return 0;
3084}
3085DEFINE_SHOW_ATTRIBUTE(clk_min_rate);
3086
3087static int clk_max_rate_show(struct seq_file *s, void *data)
3088{
3089 struct clk_core *core = s->private;
3090 unsigned long min_rate, max_rate;
3091
3092 clk_prepare_lock();
3093 clk_core_get_boundaries(core, &min_rate, &max_rate);
3094 clk_prepare_unlock();
3095 seq_printf(s, "%lu\n", max_rate);
3096
3097 return 0;
3098}
3099DEFINE_SHOW_ATTRIBUTE(clk_max_rate);
3100
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003101static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003102{
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003103 struct dentry *root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003104
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003105 if (!core || !pdentry)
3106 return;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003107
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003108 root = debugfs_create_dir(core->name, pdentry);
3109 core->dentry = root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003110
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003111 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
Leonard Crestez1bd37a42019-07-02 16:27:09 +03003112 debugfs_create_file("clk_min_rate", 0444, root, core, &clk_min_rate_fops);
3113 debugfs_create_file("clk_max_rate", 0444, root, core, &clk_max_rate_fops);
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003114 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
3115 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
3116 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
3117 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
3118 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
3119 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
3120 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
Jerome Brunet9fba7382018-06-19 16:41:41 +02003121 debugfs_create_file("clk_duty_cycle", 0444, root, core,
3122 &clk_duty_cycle_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003123
Leonard Cresteze5e89242019-06-10 14:06:38 +03003124 if (core->num_parents > 0)
3125 debugfs_create_file("clk_parent", 0444, root, core,
3126 &current_parent_fops);
3127
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003128 if (core->num_parents > 1)
3129 debugfs_create_file("clk_possible_parents", 0444, root, core,
3130 &possible_parents_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003131
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003132 if (core->ops->debug_init)
3133 core->ops->debug_init(core->hw, core->dentry);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003134}
3135
3136/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003137 * clk_debug_register - add a clk node to the debugfs clk directory
3138 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003139 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003140 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3141 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003142 * will be created lazily by clk_debug_init as part of a late_initcall.
3143 */
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003144static void clk_debug_register(struct clk_core *core)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003145{
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003146 mutex_lock(&clk_debug_lock);
3147 hlist_add_head(&core->debug_node, &clk_debug_list);
Stephen Boyddb3188fa2018-01-03 16:44:37 -08003148 if (inited)
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003149 clk_debug_create_one(core, rootdir);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003150 mutex_unlock(&clk_debug_lock);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003151}
3152
3153 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003154 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3155 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003156 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003157 * Dynamically removes a clk and all its child nodes from the
3158 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08003159 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003160 */
3161static void clk_debug_unregister(struct clk_core *core)
3162{
3163 mutex_lock(&clk_debug_lock);
3164 hlist_del_init(&core->debug_node);
3165 debugfs_remove_recursive(core->dentry);
3166 core->dentry = NULL;
3167 mutex_unlock(&clk_debug_lock);
3168}
3169
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003170/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003171 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003172 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003173 * clks are often initialized very early during boot before memory can be
3174 * dynamically allocated and well before debugfs is setup. This function
3175 * populates the debugfs clk directory once at boot-time when we know that
3176 * debugfs is setup. It should only be called once at boot-time, all other clks
3177 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003178 */
3179static int __init clk_debug_init(void)
3180{
3181 struct clk_core *core;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003182
3183 rootdir = debugfs_create_dir("clk", NULL);
3184
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003185 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
3186 &clk_summary_fops);
3187 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
3188 &clk_dump_fops);
3189 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
3190 &clk_summary_fops);
3191 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
3192 &clk_dump_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003193
3194 mutex_lock(&clk_debug_lock);
3195 hlist_for_each_entry(core, &clk_debug_list, debug_node)
3196 clk_debug_create_one(core, rootdir);
3197
3198 inited = 1;
3199 mutex_unlock(&clk_debug_lock);
3200
3201 return 0;
3202}
3203late_initcall(clk_debug_init);
3204#else
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003205static inline void clk_debug_register(struct clk_core *core) { }
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003206static inline void clk_debug_reparent(struct clk_core *core,
3207 struct clk_core *new_parent)
3208{
3209}
3210static inline void clk_debug_unregister(struct clk_core *core)
3211{
3212}
3213#endif
3214
Michael Turquette3d3801e2015-02-25 09:11:01 -08003215/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003216 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003217 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07003218 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003219 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07003220 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07003221 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003222static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07003223{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003224 int ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003225 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003226 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003227 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003228
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003229 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07003230 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07003231
Mike Turquetteeab89f62013-03-28 13:59:01 -07003232 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003233
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003234 ret = clk_pm_runtime_get(core);
3235 if (ret)
3236 goto unlock;
3237
Mike Turquetteb24764902012-03-15 23:11:19 -07003238 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003239 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003240 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003241 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003242 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07003243 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07003244 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003245
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03003246 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003247 if (core->ops->set_rate &&
3248 !((core->ops->round_rate || core->ops->determine_rate) &&
3249 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003250 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3251 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003252 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003253 goto out;
3254 }
3255
Stephen Boydd6968fc2015-04-30 13:54:13 -07003256 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003257 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3258 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003259 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003260 goto out;
3261 }
3262
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09003263 if (core->num_parents > 1 && !core->ops->get_parent) {
3264 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3265 __func__, core->name);
3266 ret = -EINVAL;
3267 goto out;
3268 }
3269
Stephen Boydd6968fc2015-04-30 13:54:13 -07003270 if (core->ops->set_rate_and_parent &&
3271 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003272 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003273 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08003274 ret = -EINVAL;
3275 goto out;
3276 }
3277
Stephen Boydd6968fc2015-04-30 13:54:13 -07003278 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07003279
3280 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08003281 * Populate core->parent if parent has already been clk_core_init'd. If
3282 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003283 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07003284 * clk list.
3285 *
3286 * Every time a new clk is clk_init'd then we walk the list of orphan
3287 * clocks and re-parent any that are children of the clock currently
3288 * being clk_init'd.
3289 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02003290 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003291 hlist_add_head(&core->child_node,
3292 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003293 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003294 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003295 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003296 core->orphan = false;
3297 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003298 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003299 core->orphan = true;
3300 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003301
3302 /*
Jerome Brunet541deba2018-02-14 14:43:37 +01003303 * optional platform-specific magic
3304 *
3305 * The .init callback is not used by any of the basic clock types, but
3306 * exists for weird hardware that must perform initialization magic.
3307 * Please consider other ways of solving initialization problems before
3308 * using this callback, as its use is discouraged.
3309 */
3310 if (core->ops->init)
3311 core->ops->init(core->hw);
3312
3313 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003314 * Set clk's accuracy. The preferred method is to use
3315 * .recalc_accuracy. For simple clocks and lazy developers the default
3316 * fallback is to use the parent's accuracy. If a clock doesn't have a
3317 * parent (or is orphaned) then accuracy is set to zero (perfect
3318 * clock).
3319 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003320 if (core->ops->recalc_accuracy)
3321 core->accuracy = core->ops->recalc_accuracy(core->hw,
3322 __clk_get_accuracy(core->parent));
3323 else if (core->parent)
3324 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003325 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003326 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003327
3328 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02003329 * Set clk's phase.
3330 * Since a phase is by definition relative to its parent, just
3331 * query the current clock phase, or just assume it's in phase.
3332 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003333 if (core->ops->get_phase)
3334 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02003335 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003336 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02003337
3338 /*
Jerome Brunet9fba7382018-06-19 16:41:41 +02003339 * Set clk's duty cycle.
3340 */
3341 clk_core_update_duty_cycle_nolock(core);
3342
3343 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07003344 * Set clk's rate. The preferred method is to use .recalc_rate. For
3345 * simple clocks and lazy developers the default fallback is to use the
3346 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3347 * then rate is set to zero.
3348 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003349 if (core->ops->recalc_rate)
3350 rate = core->ops->recalc_rate(core->hw,
3351 clk_core_get_rate_nolock(core->parent));
3352 else if (core->parent)
3353 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003354 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003355 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003356 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003357
3358 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003359 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3360 * don't get accidentally disabled when walking the orphan tree and
3361 * reparenting clocks
3362 */
3363 if (core->flags & CLK_IS_CRITICAL) {
3364 unsigned long flags;
3365
3366 clk_core_prepare(core);
3367
3368 flags = clk_enable_lock();
3369 clk_core_enable(core);
3370 clk_enable_unlock(flags);
3371 }
3372
3373 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003374 * walk the list of orphan clocks and reparent any that newly finds a
3375 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07003376 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08003377 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003378 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01003379
Michael Turquette904e6ea2016-07-08 16:32:10 -07003380 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003381 * We need to use __clk_set_parent_before() and _after() to
3382 * to properly migrate any prepare/enable count of the orphan
3383 * clock. This is important for CLK_IS_CRITICAL clocks, which
3384 * are enabled during init but might not have a parent yet.
Michael Turquette904e6ea2016-07-08 16:32:10 -07003385 */
3386 if (parent) {
Stephen Boydf8f8f1d2017-11-02 00:36:09 -07003387 /* update the clk tree topology */
Jerome Brunet99652a42018-02-14 14:43:36 +01003388 __clk_set_parent_before(orphan, parent);
3389 __clk_set_parent_after(orphan, parent, NULL);
Michael Turquette904e6ea2016-07-08 16:32:10 -07003390 __clk_recalc_accuracies(orphan);
3391 __clk_recalc_rates(orphan, 0);
3392 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003393 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003394
Stephen Boydd6968fc2015-04-30 13:54:13 -07003395 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07003396out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003397 clk_pm_runtime_put(core);
3398unlock:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003399 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003400
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003401 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003402 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003403
Mike Turquetted1302a32012-03-29 14:30:40 -07003404 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07003405}
3406
Stephen Boyd1df40462018-12-11 08:32:04 -08003407/**
3408 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3409 * @core: clk to add consumer to
3410 * @clk: consumer to link to a clk
3411 */
3412static void clk_core_link_consumer(struct clk_core *core, struct clk *clk)
3413{
3414 clk_prepare_lock();
3415 hlist_add_head(&clk->clks_node, &core->clks);
3416 clk_prepare_unlock();
3417}
3418
3419/**
3420 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3421 * @clk: consumer to unlink
3422 */
3423static void clk_core_unlink_consumer(struct clk *clk)
3424{
3425 lockdep_assert_held(&prepare_lock);
3426 hlist_del(&clk->clks_node);
3427}
3428
3429/**
3430 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
3431 * @core: clk to allocate a consumer for
3432 * @dev_id: string describing device name
3433 * @con_id: connection ID string on device
3434 *
3435 * Returns: clk consumer left unlinked from the consumer list
3436 */
3437static struct clk *alloc_clk(struct clk_core *core, const char *dev_id,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003438 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003439{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003440 struct clk *clk;
3441
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003442 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3443 if (!clk)
3444 return ERR_PTR(-ENOMEM);
3445
Stephen Boyd1df40462018-12-11 08:32:04 -08003446 clk->core = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003447 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02003448 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003449 clk->max_rate = ULONG_MAX;
3450
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003451 return clk;
3452}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003453
Stephen Boyd1df40462018-12-11 08:32:04 -08003454/**
3455 * free_clk - Free a clk consumer
3456 * @clk: clk consumer to free
3457 *
3458 * Note, this assumes the clk has been unlinked from the clk_core consumer
3459 * list.
3460 */
3461static void free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003462{
Leonard Crestez253160a2017-02-20 15:20:56 +02003463 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003464 kfree(clk);
3465}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003466
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003467/**
Stephen Boyd1df40462018-12-11 08:32:04 -08003468 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
3469 * a clk_hw
Stephen Boydefa85042018-12-11 08:34:16 -08003470 * @dev: clk consumer device
Stephen Boyd1df40462018-12-11 08:32:04 -08003471 * @hw: clk_hw associated with the clk being consumed
3472 * @dev_id: string describing device name
3473 * @con_id: connection ID string on device
3474 *
3475 * This is the main function used to create a clk pointer for use by clk
3476 * consumers. It connects a consumer to the clk_core and clk_hw structures
3477 * used by the framework and clk provider respectively.
3478 */
Stephen Boydefa85042018-12-11 08:34:16 -08003479struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
Stephen Boyd1df40462018-12-11 08:32:04 -08003480 const char *dev_id, const char *con_id)
3481{
3482 struct clk *clk;
3483 struct clk_core *core;
3484
3485 /* This is to allow this function to be chained to others */
3486 if (IS_ERR_OR_NULL(hw))
3487 return ERR_CAST(hw);
3488
3489 core = hw->core;
3490 clk = alloc_clk(core, dev_id, con_id);
3491 if (IS_ERR(clk))
3492 return clk;
Stephen Boydefa85042018-12-11 08:34:16 -08003493 clk->dev = dev;
Stephen Boyd1df40462018-12-11 08:32:04 -08003494
3495 if (!try_module_get(core->owner)) {
3496 free_clk(clk);
3497 return ERR_PTR(-ENOENT);
3498 }
3499
3500 kref_get(&core->ref);
3501 clk_core_link_consumer(core, clk);
3502
3503 return clk;
3504}
3505
Stephen Boydfc0c2092019-04-12 11:31:47 -07003506static int clk_cpy_name(const char **dst_p, const char *src, bool must_exist)
Mike Turquetteb24764902012-03-15 23:11:19 -07003507{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003508 const char *dst;
3509
3510 if (!src) {
3511 if (must_exist)
3512 return -EINVAL;
3513 return 0;
3514 }
3515
3516 *dst_p = dst = kstrdup_const(src, GFP_KERNEL);
3517 if (!dst)
3518 return -ENOMEM;
3519
3520 return 0;
3521}
3522
3523static int clk_core_populate_parent_map(struct clk_core *core)
3524{
3525 const struct clk_init_data *init = core->hw->init;
3526 u8 num_parents = init->num_parents;
3527 const char * const *parent_names = init->parent_names;
3528 const struct clk_hw **parent_hws = init->parent_hws;
3529 const struct clk_parent_data *parent_data = init->parent_data;
3530 int i, ret = 0;
3531 struct clk_parent_map *parents, *parent;
3532
3533 if (!num_parents)
3534 return 0;
3535
3536 /*
3537 * Avoid unnecessary string look-ups of clk_core's possible parents by
3538 * having a cache of names/clk_hw pointers to clk_core pointers.
3539 */
3540 parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
3541 core->parents = parents;
3542 if (!parents)
3543 return -ENOMEM;
3544
3545 /* Copy everything over because it might be __initdata */
3546 for (i = 0, parent = parents; i < num_parents; i++, parent++) {
Stephen Boyd601b6e92019-04-12 11:31:49 -07003547 parent->index = -1;
Stephen Boydfc0c2092019-04-12 11:31:47 -07003548 if (parent_names) {
3549 /* throw a WARN if any entries are NULL */
3550 WARN(!parent_names[i],
3551 "%s: invalid NULL in %s's .parent_names\n",
3552 __func__, core->name);
3553 ret = clk_cpy_name(&parent->name, parent_names[i],
3554 true);
3555 } else if (parent_data) {
3556 parent->hw = parent_data[i].hw;
Stephen Boyd601b6e92019-04-12 11:31:49 -07003557 parent->index = parent_data[i].index;
Stephen Boydfc0c2092019-04-12 11:31:47 -07003558 ret = clk_cpy_name(&parent->fw_name,
3559 parent_data[i].fw_name, false);
3560 if (!ret)
3561 ret = clk_cpy_name(&parent->name,
3562 parent_data[i].name,
3563 false);
3564 } else if (parent_hws) {
3565 parent->hw = parent_hws[i];
3566 } else {
3567 ret = -EINVAL;
3568 WARN(1, "Must specify parents if num_parents > 0\n");
3569 }
3570
3571 if (ret) {
3572 do {
3573 kfree_const(parents[i].name);
3574 kfree_const(parents[i].fw_name);
3575 } while (--i >= 0);
3576 kfree(parents);
3577
3578 return ret;
3579 }
3580 }
3581
3582 return 0;
3583}
3584
3585static void clk_core_free_parent_map(struct clk_core *core)
3586{
3587 int i = core->num_parents;
3588
3589 if (!core->num_parents)
3590 return;
3591
3592 while (--i >= 0) {
3593 kfree_const(core->parents[i].name);
3594 kfree_const(core->parents[i].fw_name);
3595 }
3596
3597 kfree(core->parents);
3598}
3599
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003600static struct clk *
3601__clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07003602{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003603 int ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003604 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003605
Stephen Boydd6968fc2015-04-30 13:54:13 -07003606 core = kzalloc(sizeof(*core), GFP_KERNEL);
3607 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003608 ret = -ENOMEM;
3609 goto fail_out;
3610 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003611
Stephen Boydd6968fc2015-04-30 13:54:13 -07003612 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3613 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003614 ret = -ENOMEM;
3615 goto fail_name;
3616 }
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003617
3618 if (WARN_ON(!hw->init->ops)) {
3619 ret = -EINVAL;
3620 goto fail_ops;
3621 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07003622 core->ops = hw->init->ops;
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003623
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003624 if (dev && pm_runtime_enabled(dev))
Miquel Raynal24478832018-12-04 20:24:37 +01003625 core->rpm_enabled = true;
3626 core->dev = dev;
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003627 core->of_node = np;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003628 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003629 core->owner = dev->driver->owner;
3630 core->hw = hw;
3631 core->flags = hw->init->flags;
3632 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003633 core->min_rate = 0;
3634 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003635 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003636
Stephen Boydfc0c2092019-04-12 11:31:47 -07003637 ret = clk_core_populate_parent_map(core);
3638 if (ret)
Masahiro Yamada176d1162015-12-28 19:23:00 +09003639 goto fail_parents;
Masahiro Yamada176d1162015-12-28 19:23:00 +09003640
Stephen Boydd6968fc2015-04-30 13:54:13 -07003641 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003642
Stephen Boyd1df40462018-12-11 08:32:04 -08003643 /*
3644 * Don't call clk_hw_create_clk() here because that would pin the
3645 * provider module to itself and prevent it from ever being removed.
3646 */
3647 hw->clk = alloc_clk(core, NULL, NULL);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003648 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003649 ret = PTR_ERR(hw->clk);
Stephen Boydfc0c2092019-04-12 11:31:47 -07003650 goto fail_create_clk;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003651 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003652
Stephen Boyd1df40462018-12-11 08:32:04 -08003653 clk_core_link_consumer(hw->core, hw->clk);
3654
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003655 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003656 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003657 return hw->clk;
3658
Stephen Boyd1df40462018-12-11 08:32:04 -08003659 clk_prepare_lock();
3660 clk_core_unlink_consumer(hw->clk);
3661 clk_prepare_unlock();
3662
3663 free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003664 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003665
Stephen Boydfc0c2092019-04-12 11:31:47 -07003666fail_create_clk:
3667 clk_core_free_parent_map(core);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003668fail_parents:
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003669fail_ops:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003670 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003671fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003672 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003673fail_out:
3674 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003675}
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003676
3677/**
3678 * clk_register - allocate a new clock, register it and return an opaque cookie
3679 * @dev: device that is registering this clock
3680 * @hw: link to hardware-specific clock data
3681 *
Stephen Boydc1157f62019-05-07 11:46:13 -07003682 * clk_register is the *deprecated* interface for populating the clock tree with
3683 * new clock nodes. Use clk_hw_register() instead.
3684 *
3685 * Returns: a pointer to the newly allocated struct clk which
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003686 * cannot be dereferenced by driver code but may be used in conjunction with the
3687 * rest of the clock API. In the event of an error clk_register will return an
3688 * error code; drivers must test for an error code after calling clk_register.
3689 */
3690struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3691{
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003692 return __clk_register(dev, dev_of_node(dev), hw);
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003693}
Mike Turquetteb24764902012-03-15 23:11:19 -07003694EXPORT_SYMBOL_GPL(clk_register);
3695
Stephen Boyd41438042016-02-05 17:02:52 -08003696/**
3697 * clk_hw_register - register a clk_hw and return an error code
3698 * @dev: device that is registering this clock
3699 * @hw: link to hardware-specific clock data
3700 *
3701 * clk_hw_register is the primary interface for populating the clock tree with
3702 * new clock nodes. It returns an integer equal to zero indicating success or
3703 * less than zero indicating failure. Drivers must test for an error code after
3704 * calling clk_hw_register().
3705 */
3706int clk_hw_register(struct device *dev, struct clk_hw *hw)
3707{
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003708 return PTR_ERR_OR_ZERO(__clk_register(dev, dev_of_node(dev), hw));
Stephen Boyd41438042016-02-05 17:02:52 -08003709}
3710EXPORT_SYMBOL_GPL(clk_hw_register);
3711
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003712/*
3713 * of_clk_hw_register - register a clk_hw and return an error code
3714 * @node: device_node of device that is registering this clock
3715 * @hw: link to hardware-specific clock data
3716 *
3717 * of_clk_hw_register() is the primary interface for populating the clock tree
3718 * with new clock nodes when a struct device is not available, but a struct
3719 * device_node is. It returns an integer equal to zero indicating success or
3720 * less than zero indicating failure. Drivers must test for an error code after
3721 * calling of_clk_hw_register().
3722 */
3723int of_clk_hw_register(struct device_node *node, struct clk_hw *hw)
3724{
3725 return PTR_ERR_OR_ZERO(__clk_register(NULL, node, hw));
3726}
3727EXPORT_SYMBOL_GPL(of_clk_hw_register);
3728
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003729/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003730static void __clk_release(struct kref *ref)
3731{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003732 struct clk_core *core = container_of(ref, struct clk_core, ref);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003733
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003734 lockdep_assert_held(&prepare_lock);
3735
Stephen Boydfc0c2092019-04-12 11:31:47 -07003736 clk_core_free_parent_map(core);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003737 kfree_const(core->name);
3738 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003739}
3740
3741/*
3742 * Empty clk_ops for unregistered clocks. These are used temporarily
3743 * after clk_unregister() was called on a clock and until last clock
3744 * consumer calls clk_put() and the struct clk object is freed.
3745 */
3746static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3747{
3748 return -ENXIO;
3749}
3750
3751static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3752{
3753 WARN_ON_ONCE(1);
3754}
3755
3756static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3757 unsigned long parent_rate)
3758{
3759 return -ENXIO;
3760}
3761
3762static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3763{
3764 return -ENXIO;
3765}
3766
3767static const struct clk_ops clk_nodrv_ops = {
3768 .enable = clk_nodrv_prepare_enable,
3769 .disable = clk_nodrv_disable_unprepare,
3770 .prepare = clk_nodrv_prepare_enable,
3771 .unprepare = clk_nodrv_disable_unprepare,
3772 .set_rate = clk_nodrv_set_rate,
3773 .set_parent = clk_nodrv_set_parent,
3774};
3775
Mark Brown1df5c932012-04-18 09:07:12 +01003776/**
3777 * clk_unregister - unregister a currently registered clock
3778 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003779 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003780void clk_unregister(struct clk *clk)
3781{
3782 unsigned long flags;
3783
Stephen Boyd6314b672014-09-04 23:37:49 -07003784 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3785 return;
3786
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003787 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003788
3789 clk_prepare_lock();
3790
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003791 if (clk->core->ops == &clk_nodrv_ops) {
3792 pr_err("%s: unregistered clock: %s\n", __func__,
3793 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003794 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003795 }
3796 /*
3797 * Assign empty clock ops for consumers that might still hold
3798 * a reference to this clock.
3799 */
3800 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003801 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003802 clk_enable_unlock(flags);
3803
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003804 if (!hlist_empty(&clk->core->children)) {
3805 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003806 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003807
3808 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003809 hlist_for_each_entry_safe(child, t, &clk->core->children,
3810 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01003811 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003812 }
3813
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003814 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003815
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003816 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003817 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003818 __func__, clk->core->name);
Jerome Brunete55a8392017-12-01 22:51:56 +01003819
3820 if (clk->core->protect_count)
3821 pr_warn("%s: unregistering protected clock: %s\n",
3822 __func__, clk->core->name);
3823
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003824 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003825unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003826 clk_prepare_unlock();
3827}
Mark Brown1df5c932012-04-18 09:07:12 +01003828EXPORT_SYMBOL_GPL(clk_unregister);
3829
Stephen Boyd41438042016-02-05 17:02:52 -08003830/**
3831 * clk_hw_unregister - unregister a currently registered clk_hw
3832 * @hw: hardware-specific clock data to unregister
3833 */
3834void clk_hw_unregister(struct clk_hw *hw)
3835{
3836 clk_unregister(hw->clk);
3837}
3838EXPORT_SYMBOL_GPL(clk_hw_unregister);
3839
Stephen Boyd46c87732012-09-24 13:38:04 -07003840static void devm_clk_release(struct device *dev, void *res)
3841{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003842 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003843}
3844
Stephen Boyd41438042016-02-05 17:02:52 -08003845static void devm_clk_hw_release(struct device *dev, void *res)
3846{
3847 clk_hw_unregister(*(struct clk_hw **)res);
3848}
3849
Stephen Boyd46c87732012-09-24 13:38:04 -07003850/**
3851 * devm_clk_register - resource managed clk_register()
3852 * @dev: device that is registering this clock
3853 * @hw: link to hardware-specific clock data
3854 *
Stephen Boyd9fe9b7a2018-12-11 10:49:40 -08003855 * Managed clk_register(). This function is *deprecated*, use devm_clk_hw_register() instead.
3856 *
3857 * Clocks returned from this function are automatically clk_unregister()ed on
3858 * driver detach. See clk_register() for more information.
Stephen Boyd46c87732012-09-24 13:38:04 -07003859 */
3860struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3861{
3862 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003863 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003864
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003865 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3866 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003867 return ERR_PTR(-ENOMEM);
3868
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003869 clk = clk_register(dev, hw);
3870 if (!IS_ERR(clk)) {
3871 *clkp = clk;
3872 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003873 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003874 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003875 }
3876
3877 return clk;
3878}
3879EXPORT_SYMBOL_GPL(devm_clk_register);
3880
Stephen Boyd41438042016-02-05 17:02:52 -08003881/**
3882 * devm_clk_hw_register - resource managed clk_hw_register()
3883 * @dev: device that is registering this clock
3884 * @hw: link to hardware-specific clock data
3885 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003886 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003887 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3888 * for more information.
3889 */
3890int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3891{
3892 struct clk_hw **hwp;
3893 int ret;
3894
3895 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3896 if (!hwp)
3897 return -ENOMEM;
3898
3899 ret = clk_hw_register(dev, hw);
3900 if (!ret) {
3901 *hwp = hw;
3902 devres_add(dev, hwp);
3903 } else {
3904 devres_free(hwp);
3905 }
3906
3907 return ret;
3908}
3909EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3910
Stephen Boyd46c87732012-09-24 13:38:04 -07003911static int devm_clk_match(struct device *dev, void *res, void *data)
3912{
3913 struct clk *c = res;
3914 if (WARN_ON(!c))
3915 return 0;
3916 return c == data;
3917}
3918
Stephen Boyd41438042016-02-05 17:02:52 -08003919static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3920{
3921 struct clk_hw *hw = res;
3922
3923 if (WARN_ON(!hw))
3924 return 0;
3925 return hw == data;
3926}
3927
Stephen Boyd46c87732012-09-24 13:38:04 -07003928/**
3929 * devm_clk_unregister - resource managed clk_unregister()
3930 * @clk: clock to unregister
3931 *
3932 * Deallocate a clock allocated with devm_clk_register(). Normally
3933 * this function will not need to be called and the resource management
3934 * code will ensure that the resource is freed.
3935 */
3936void devm_clk_unregister(struct device *dev, struct clk *clk)
3937{
3938 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3939}
3940EXPORT_SYMBOL_GPL(devm_clk_unregister);
3941
Stephen Boyd41438042016-02-05 17:02:52 -08003942/**
3943 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3944 * @dev: device that is unregistering the hardware-specific clock data
3945 * @hw: link to hardware-specific clock data
3946 *
3947 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3948 * this function will not need to be called and the resource management
3949 * code will ensure that the resource is freed.
3950 */
3951void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3952{
3953 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3954 hw));
3955}
3956EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3957
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003958/*
3959 * clkdev helpers
3960 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003961
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003962void __clk_put(struct clk *clk)
3963{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003964 struct module *owner;
3965
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003966 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003967 return;
3968
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003969 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003970
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01003971 /*
3972 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3973 * given user should be balanced with calls to clk_rate_exclusive_put()
3974 * and by that same consumer
3975 */
3976 if (WARN_ON(clk->exclusive_count)) {
3977 /* We voiced our concern, let's sanitize the situation */
3978 clk->core->protect_count -= (clk->exclusive_count - 1);
3979 clk_core_rate_unprotect(clk->core);
3980 clk->exclusive_count = 0;
3981 }
3982
Stephen Boyd50595f82015-02-06 11:42:44 -08003983 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003984 if (clk->min_rate > clk->core->req_rate ||
3985 clk->max_rate < clk->core->req_rate)
3986 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3987
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003988 owner = clk->core->owner;
3989 kref_put(&clk->core->ref, __clk_release);
3990
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003991 clk_prepare_unlock();
3992
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003993 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003994
Stephen Boyd1df40462018-12-11 08:32:04 -08003995 free_clk(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003996}
3997
Mike Turquetteb24764902012-03-15 23:11:19 -07003998/*** clk rate change notifiers ***/
3999
4000/**
4001 * clk_notifier_register - add a clk rate change notifier
4002 * @clk: struct clk * to watch
4003 * @nb: struct notifier_block * with callback info
4004 *
4005 * Request notification when clk's rate changes. This uses an SRCU
4006 * notifier because we want it to block and notifier unregistrations are
4007 * uncommon. The callbacks associated with the notifier must not
4008 * re-enter into the clk framework by calling any top-level clk APIs;
4009 * this will cause a nested prepare_lock mutex.
4010 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09004011 * In all notification cases (pre, post and abort rate change) the original
4012 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
4013 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07004014 *
Mike Turquetteb24764902012-03-15 23:11:19 -07004015 * clk_notifier_register() must be called from non-atomic context.
4016 * Returns -EINVAL if called with null arguments, -ENOMEM upon
4017 * allocation failure; otherwise, passes along the return value of
4018 * srcu_notifier_chain_register().
4019 */
4020int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
4021{
4022 struct clk_notifier *cn;
4023 int ret = -ENOMEM;
4024
4025 if (!clk || !nb)
4026 return -EINVAL;
4027
Mike Turquetteeab89f62013-03-28 13:59:01 -07004028 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004029
4030 /* search the list of notifiers for this clk */
4031 list_for_each_entry(cn, &clk_notifier_list, node)
4032 if (cn->clk == clk)
4033 break;
4034
4035 /* if clk wasn't in the notifier list, allocate new clk_notifier */
4036 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02004037 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07004038 if (!cn)
4039 goto out;
4040
4041 cn->clk = clk;
4042 srcu_init_notifier_head(&cn->notifier_head);
4043
4044 list_add(&cn->node, &clk_notifier_list);
4045 }
4046
4047 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
4048
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004049 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07004050
4051out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07004052 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004053
4054 return ret;
4055}
4056EXPORT_SYMBOL_GPL(clk_notifier_register);
4057
4058/**
4059 * clk_notifier_unregister - remove a clk rate change notifier
4060 * @clk: struct clk *
4061 * @nb: struct notifier_block * with callback info
4062 *
4063 * Request no further notification for changes to 'clk' and frees memory
4064 * allocated in clk_notifier_register.
4065 *
4066 * Returns -EINVAL if called with null arguments; otherwise, passes
4067 * along the return value of srcu_notifier_chain_unregister().
4068 */
4069int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
4070{
4071 struct clk_notifier *cn = NULL;
4072 int ret = -EINVAL;
4073
4074 if (!clk || !nb)
4075 return -EINVAL;
4076
Mike Turquetteeab89f62013-03-28 13:59:01 -07004077 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004078
4079 list_for_each_entry(cn, &clk_notifier_list, node)
4080 if (cn->clk == clk)
4081 break;
4082
4083 if (cn->clk == clk) {
4084 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
4085
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004086 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07004087
4088 /* XXX the notifier code should handle this better */
4089 if (!cn->notifier_head.head) {
4090 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08004091 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07004092 kfree(cn);
4093 }
4094
4095 } else {
4096 ret = -ENOENT;
4097 }
4098
Mike Turquetteeab89f62013-03-28 13:59:01 -07004099 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004100
4101 return ret;
4102}
4103EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05004104
4105#ifdef CONFIG_OF
4106/**
4107 * struct of_clk_provider - Clock provider registration structure
4108 * @link: Entry in global list of clock providers
4109 * @node: Pointer to device tree node of clock provider
4110 * @get: Get clock callback. Returns NULL or a struct clk for the
4111 * given clock specifier
4112 * @data: context pointer to be passed into @get callback
4113 */
4114struct of_clk_provider {
4115 struct list_head link;
4116
4117 struct device_node *node;
4118 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004119 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05004120 void *data;
4121};
4122
Stephen Boyd30d5a942019-05-23 17:11:57 -07004123extern struct of_device_id __clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304124static const struct of_device_id __clk_of_table_sentinel
4125 __used __section(__clk_of_table_end);
4126
Grant Likely766e6a42012-04-09 14:50:06 -05004127static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004128static DEFINE_MUTEX(of_clk_mutex);
4129
Grant Likely766e6a42012-04-09 14:50:06 -05004130struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
4131 void *data)
4132{
4133 return data;
4134}
4135EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
4136
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004137struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
4138{
4139 return data;
4140}
4141EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
4142
Shawn Guo494bfec2012-08-22 21:36:27 +08004143struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
4144{
4145 struct clk_onecell_data *clk_data = data;
4146 unsigned int idx = clkspec->args[0];
4147
4148 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02004149 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08004150 return ERR_PTR(-EINVAL);
4151 }
4152
4153 return clk_data->clks[idx];
4154}
4155EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
4156
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004157struct clk_hw *
4158of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
4159{
4160 struct clk_hw_onecell_data *hw_data = data;
4161 unsigned int idx = clkspec->args[0];
4162
4163 if (idx >= hw_data->num) {
4164 pr_err("%s: invalid index %u\n", __func__, idx);
4165 return ERR_PTR(-EINVAL);
4166 }
4167
4168 return hw_data->hws[idx];
4169}
4170EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
4171
Grant Likely766e6a42012-04-09 14:50:06 -05004172/**
4173 * of_clk_add_provider() - Register a clock provider for a node
4174 * @np: Device node pointer associated with clock provider
4175 * @clk_src_get: callback for decoding clock
4176 * @data: context pointer for @clk_src_get callback.
Stephen Boyd9fe9b7a2018-12-11 10:49:40 -08004177 *
4178 * This function is *deprecated*. Use of_clk_add_hw_provider() instead.
Grant Likely766e6a42012-04-09 14:50:06 -05004179 */
4180int of_clk_add_provider(struct device_node *np,
4181 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
4182 void *data),
4183 void *data)
4184{
4185 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004186 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004187
Markus Elfring1808a322017-04-20 09:30:52 +02004188 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05004189 if (!cp)
4190 return -ENOMEM;
4191
4192 cp->node = of_node_get(np);
4193 cp->data = data;
4194 cp->get = clk_src_get;
4195
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004196 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004197 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004198 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05004199 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05004200
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004201 ret = of_clk_set_defaults(np, true);
4202 if (ret < 0)
4203 of_clk_del_provider(np);
4204
4205 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004206}
4207EXPORT_SYMBOL_GPL(of_clk_add_provider);
4208
4209/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004210 * of_clk_add_hw_provider() - Register a clock provider for a node
4211 * @np: Device node pointer associated with clock provider
4212 * @get: callback for decoding clk_hw
4213 * @data: context pointer for @get callback.
4214 */
4215int of_clk_add_hw_provider(struct device_node *np,
4216 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4217 void *data),
4218 void *data)
4219{
4220 struct of_clk_provider *cp;
4221 int ret;
4222
4223 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4224 if (!cp)
4225 return -ENOMEM;
4226
4227 cp->node = of_node_get(np);
4228 cp->data = data;
4229 cp->get_hw = get;
4230
4231 mutex_lock(&of_clk_mutex);
4232 list_add(&cp->link, &of_clk_providers);
4233 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05004234 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004235
4236 ret = of_clk_set_defaults(np, true);
4237 if (ret < 0)
4238 of_clk_del_provider(np);
4239
4240 return ret;
4241}
4242EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
4243
Stephen Boydaa795c42017-09-01 16:16:40 -07004244static void devm_of_clk_release_provider(struct device *dev, void *res)
4245{
4246 of_clk_del_provider(*(struct device_node **)res);
4247}
4248
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004249/*
4250 * We allow a child device to use its parent device as the clock provider node
4251 * for cases like MFD sub-devices where the child device driver wants to use
4252 * devm_*() APIs but not list the device in DT as a sub-node.
4253 */
4254static struct device_node *get_clk_provider_node(struct device *dev)
4255{
4256 struct device_node *np, *parent_np;
4257
4258 np = dev->of_node;
4259 parent_np = dev->parent ? dev->parent->of_node : NULL;
4260
4261 if (!of_find_property(np, "#clock-cells", NULL))
4262 if (of_find_property(parent_np, "#clock-cells", NULL))
4263 np = parent_np;
4264
4265 return np;
4266}
4267
Matti Vaittinene45838b2018-12-04 13:33:48 +02004268/**
4269 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4270 * @dev: Device acting as the clock provider (used for DT node and lifetime)
4271 * @get: callback for decoding clk_hw
4272 * @data: context pointer for @get callback
4273 *
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004274 * Registers clock provider for given device's node. If the device has no DT
4275 * node or if the device node lacks of clock provider information (#clock-cells)
4276 * then the parent device's node is scanned for this information. If parent node
4277 * has the #clock-cells then it is used in registration. Provider is
4278 * automatically released at device exit.
Matti Vaittinene45838b2018-12-04 13:33:48 +02004279 *
4280 * Return: 0 on success or an errno on failure.
4281 */
Stephen Boydaa795c42017-09-01 16:16:40 -07004282int devm_of_clk_add_hw_provider(struct device *dev,
4283 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4284 void *data),
4285 void *data)
4286{
4287 struct device_node **ptr, *np;
4288 int ret;
4289
4290 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
4291 GFP_KERNEL);
4292 if (!ptr)
4293 return -ENOMEM;
4294
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004295 np = get_clk_provider_node(dev);
Stephen Boydaa795c42017-09-01 16:16:40 -07004296 ret = of_clk_add_hw_provider(np, get, data);
4297 if (!ret) {
4298 *ptr = np;
4299 devres_add(dev, ptr);
4300 } else {
4301 devres_free(ptr);
4302 }
4303
4304 return ret;
4305}
4306EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
4307
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004308/**
Grant Likely766e6a42012-04-09 14:50:06 -05004309 * of_clk_del_provider() - Remove a previously registered clock provider
4310 * @np: Device node pointer associated with clock provider
4311 */
4312void of_clk_del_provider(struct device_node *np)
4313{
4314 struct of_clk_provider *cp;
4315
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004316 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004317 list_for_each_entry(cp, &of_clk_providers, link) {
4318 if (cp->node == np) {
4319 list_del(&cp->link);
4320 of_node_put(cp->node);
4321 kfree(cp);
4322 break;
4323 }
4324 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004325 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004326}
4327EXPORT_SYMBOL_GPL(of_clk_del_provider);
4328
Stephen Boydaa795c42017-09-01 16:16:40 -07004329static int devm_clk_provider_match(struct device *dev, void *res, void *data)
4330{
4331 struct device_node **np = res;
4332
4333 if (WARN_ON(!np || !*np))
4334 return 0;
4335
4336 return *np == data;
4337}
4338
Matti Vaittinene45838b2018-12-04 13:33:48 +02004339/**
4340 * devm_of_clk_del_provider() - Remove clock provider registered using devm
4341 * @dev: Device to whose lifetime the clock provider was bound
4342 */
Stephen Boydaa795c42017-09-01 16:16:40 -07004343void devm_of_clk_del_provider(struct device *dev)
4344{
4345 int ret;
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004346 struct device_node *np = get_clk_provider_node(dev);
Stephen Boydaa795c42017-09-01 16:16:40 -07004347
4348 ret = devres_release(dev, devm_of_clk_release_provider,
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004349 devm_clk_provider_match, np);
Stephen Boydaa795c42017-09-01 16:16:40 -07004350
4351 WARN_ON(ret);
4352}
4353EXPORT_SYMBOL(devm_of_clk_del_provider);
4354
Stephen Boyd5dc7e842019-03-08 10:35:01 -08004355/*
4356 * Beware the return values when np is valid, but no clock provider is found.
4357 * If name == NULL, the function returns -ENOENT.
4358 * If name != NULL, the function returns -EINVAL. This is because
4359 * of_parse_phandle_with_args() is called even if of_property_match_string()
4360 * returns an error.
4361 */
Stephen Boydcf13f282018-12-19 15:09:14 -08004362static int of_parse_clkspec(const struct device_node *np, int index,
4363 const char *name, struct of_phandle_args *out_args)
Stephen Boyd44722872018-12-19 10:59:55 -08004364{
4365 int ret = -ENOENT;
4366
4367 /* Walk up the tree of devices looking for a clock property that matches */
4368 while (np) {
4369 /*
4370 * For named clocks, first look up the name in the
4371 * "clock-names" property. If it cannot be found, then index
4372 * will be an error code and of_parse_phandle_with_args() will
4373 * return -EINVAL.
4374 */
4375 if (name)
4376 index = of_property_match_string(np, "clock-names", name);
4377 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
4378 index, out_args);
4379 if (!ret)
4380 break;
4381 if (name && index >= 0)
4382 break;
4383
4384 /*
4385 * No matching clock found on this node. If the parent node
4386 * has a "clock-ranges" property, then we can try one of its
4387 * clocks.
4388 */
4389 np = np->parent;
4390 if (np && !of_get_property(np, "clock-ranges", NULL))
4391 break;
4392 index = 0;
4393 }
4394
4395 return ret;
4396}
4397
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004398static struct clk_hw *
4399__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4400 struct of_phandle_args *clkspec)
4401{
4402 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004403
Stephen Boyd74002fc2016-08-25 13:35:36 -07004404 if (provider->get_hw)
4405 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004406
Stephen Boyd74002fc2016-08-25 13:35:36 -07004407 clk = provider->get(clkspec, provider->data);
4408 if (IS_ERR(clk))
4409 return ERR_CAST(clk);
4410 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004411}
4412
Stephen Boydcf13f282018-12-19 15:09:14 -08004413static struct clk_hw *
4414of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
Grant Likely766e6a42012-04-09 14:50:06 -05004415{
4416 struct of_clk_provider *provider;
Stephen Boyd1df40462018-12-11 08:32:04 -08004417 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05004418
Stephen Boyd306c3422015-02-05 15:39:11 -08004419 if (!clkspec)
4420 return ERR_PTR(-EINVAL);
4421
Stephen Boyd306c3422015-02-05 15:39:11 -08004422 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004423 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07004424 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004425 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd1df40462018-12-11 08:32:04 -08004426 if (!IS_ERR(hw))
4427 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08004428 }
Grant Likely766e6a42012-04-09 14:50:06 -05004429 }
Stephen Boyd306c3422015-02-05 15:39:11 -08004430 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004431
Stephen Boyd44722872018-12-19 10:59:55 -08004432 return hw;
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004433}
4434
Stephen Boyd306c3422015-02-05 15:39:11 -08004435/**
4436 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4437 * @clkspec: pointer to a clock specifier data structure
4438 *
4439 * This function looks up a struct clk from the registered list of clock
4440 * providers, an input is a clock specifier data structure as returned
4441 * from the of_parse_phandle_with_args() function call.
4442 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004443struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4444{
Stephen Boyd44722872018-12-19 10:59:55 -08004445 struct clk_hw *hw = of_clk_get_hw_from_clkspec(clkspec);
4446
Stephen Boydefa85042018-12-11 08:34:16 -08004447 return clk_hw_create_clk(NULL, hw, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05004448}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06004449EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05004450
Stephen Boydcf13f282018-12-19 15:09:14 -08004451struct clk_hw *of_clk_get_hw(struct device_node *np, int index,
4452 const char *con_id)
4453{
4454 int ret;
4455 struct clk_hw *hw;
4456 struct of_phandle_args clkspec;
4457
4458 ret = of_parse_clkspec(np, index, con_id, &clkspec);
4459 if (ret)
4460 return ERR_PTR(ret);
4461
4462 hw = of_clk_get_hw_from_clkspec(&clkspec);
4463 of_node_put(clkspec.np);
4464
4465 return hw;
4466}
4467
4468static struct clk *__of_clk_get(struct device_node *np,
4469 int index, const char *dev_id,
4470 const char *con_id)
4471{
4472 struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
4473
4474 return clk_hw_create_clk(NULL, hw, dev_id, con_id);
4475}
4476
4477struct clk *of_clk_get(struct device_node *np, int index)
4478{
4479 return __of_clk_get(np, index, np->full_name, NULL);
4480}
4481EXPORT_SYMBOL(of_clk_get);
4482
4483/**
4484 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
4485 * @np: pointer to clock consumer node
4486 * @name: name of consumer's clock input, or NULL for the first clock reference
4487 *
4488 * This function parses the clocks and clock-names properties,
4489 * and uses them to look up the struct clk from the registered list of clock
4490 * providers.
4491 */
4492struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
4493{
4494 if (!np)
4495 return ERR_PTR(-ENOENT);
4496
Kuninori Morimoto65cf20a2019-03-06 16:18:28 +09004497 return __of_clk_get(np, 0, np->full_name, name);
Stephen Boydcf13f282018-12-19 15:09:14 -08004498}
4499EXPORT_SYMBOL(of_clk_get_by_name);
4500
Stephen Boyd929e7f32016-02-19 15:52:32 -08004501/**
4502 * of_clk_get_parent_count() - Count the number of clocks a device node has
4503 * @np: device node to count
4504 *
4505 * Returns: The number of clocks that are possible parents of this node
4506 */
4507unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07004508{
Stephen Boyd929e7f32016-02-19 15:52:32 -08004509 int count;
4510
4511 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4512 if (count < 0)
4513 return 0;
4514
4515 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07004516}
4517EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4518
Grant Likely766e6a42012-04-09 14:50:06 -05004519const char *of_clk_get_parent_name(struct device_node *np, int index)
4520{
4521 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004522 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05004523 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004524 const __be32 *vp;
4525 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05004526 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004527 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004528 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05004529
Grant Likely766e6a42012-04-09 14:50:06 -05004530 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4531 &clkspec);
4532 if (rc)
4533 return NULL;
4534
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004535 index = clkspec.args_count ? clkspec.args[0] : 0;
4536 count = 0;
4537
4538 /* if there is an indices property, use it to transfer the index
4539 * specified into an array offset for the clock-output-names property.
4540 */
4541 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4542 if (index == pv) {
4543 index = count;
4544 break;
4545 }
4546 count++;
4547 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09004548 /* We went off the end of 'clock-indices' without finding it */
4549 if (prop && !vp)
4550 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004551
Grant Likely766e6a42012-04-09 14:50:06 -05004552 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004553 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004554 &clk_name) < 0) {
4555 /*
4556 * Best effort to get the name if the clock has been
4557 * registered with the framework. If the clock isn't
4558 * registered, we return the node name as the name of
4559 * the clock as long as #clock-cells = 0.
4560 */
4561 clk = of_clk_get_from_provider(&clkspec);
4562 if (IS_ERR(clk)) {
4563 if (clkspec.args_count == 0)
4564 clk_name = clkspec.np->name;
4565 else
4566 clk_name = NULL;
4567 } else {
4568 clk_name = __clk_get_name(clk);
4569 clk_put(clk);
4570 }
4571 }
4572
Grant Likely766e6a42012-04-09 14:50:06 -05004573
4574 of_node_put(clkspec.np);
4575 return clk_name;
4576}
4577EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4578
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05004579/**
4580 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4581 * number of parents
4582 * @np: Device node pointer associated with clock provider
4583 * @parents: pointer to char array that hold the parents' names
4584 * @size: size of the @parents array
4585 *
4586 * Return: number of parents for the clock node.
4587 */
4588int of_clk_parent_fill(struct device_node *np, const char **parents,
4589 unsigned int size)
4590{
4591 unsigned int i = 0;
4592
4593 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4594 i++;
4595
4596 return i;
4597}
4598EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4599
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004600struct clock_provider {
Geert Uytterhoevena59704332018-04-10 15:06:05 +02004601 void (*clk_init_cb)(struct device_node *);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004602 struct device_node *np;
4603 struct list_head node;
4604};
4605
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004606/*
4607 * This function looks for a parent clock. If there is one, then it
4608 * checks that the provider for this parent clock was initialized, in
4609 * this case the parent clock will be ready.
4610 */
4611static int parent_ready(struct device_node *np)
4612{
4613 int i = 0;
4614
4615 while (true) {
4616 struct clk *clk = of_clk_get(np, i);
4617
4618 /* this parent is ready we can check the next one */
4619 if (!IS_ERR(clk)) {
4620 clk_put(clk);
4621 i++;
4622 continue;
4623 }
4624
4625 /* at least one parent is not ready, we exit now */
4626 if (PTR_ERR(clk) == -EPROBE_DEFER)
4627 return 0;
4628
4629 /*
4630 * Here we make assumption that the device tree is
4631 * written correctly. So an error means that there is
4632 * no more parent. As we didn't exit yet, then the
4633 * previous parent are ready. If there is no clock
4634 * parent, no need to wait for them, then we can
4635 * consider their absence as being ready
4636 */
4637 return 1;
4638 }
4639}
4640
Grant Likely766e6a42012-04-09 14:50:06 -05004641/**
Lee Jonesd56f8992016-02-11 13:19:11 -08004642 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4643 * @np: Device node pointer associated with clock provider
4644 * @index: clock index
Geert Uytterhoevenf7ae7502018-01-03 12:06:14 +01004645 * @flags: pointer to top-level framework flags
Lee Jonesd56f8992016-02-11 13:19:11 -08004646 *
4647 * Detects if the clock-critical property exists and, if so, sets the
4648 * corresponding CLK_IS_CRITICAL flag.
4649 *
4650 * Do not use this function. It exists only for legacy Device Tree
4651 * bindings, such as the one-clock-per-node style that are outdated.
4652 * Those bindings typically put all clock data into .dts and the Linux
4653 * driver has no clock data, thus making it impossible to set this flag
4654 * correctly from the driver. Only those drivers may call
4655 * of_clk_detect_critical from their setup functions.
4656 *
4657 * Return: error code or zero on success
4658 */
4659int of_clk_detect_critical(struct device_node *np,
4660 int index, unsigned long *flags)
4661{
4662 struct property *prop;
4663 const __be32 *cur;
4664 uint32_t idx;
4665
4666 if (!np || !flags)
4667 return -EINVAL;
4668
4669 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4670 if (index == idx)
4671 *flags |= CLK_IS_CRITICAL;
4672
4673 return 0;
4674}
4675
4676/**
Grant Likely766e6a42012-04-09 14:50:06 -05004677 * of_clk_init() - Scan and init clock providers from the DT
4678 * @matches: array of compatible values and init functions for providers.
4679 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004680 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01004681 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004682 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05004683 */
4684void __init of_clk_init(const struct of_device_id *matches)
4685{
Alex Elder7f7ed582013-08-22 11:31:31 -05004686 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05004687 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004688 struct clock_provider *clk_provider, *next;
4689 bool is_init_done;
4690 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07004691 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05004692
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304693 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03004694 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304695
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004696 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05004697 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004698 struct clock_provider *parent;
4699
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01004700 if (!of_device_is_available(np))
4701 continue;
4702
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004703 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4704 if (!parent) {
4705 list_for_each_entry_safe(clk_provider, next,
4706 &clk_provider_list, node) {
4707 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004708 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004709 kfree(clk_provider);
4710 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004711 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004712 return;
4713 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004714
4715 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004716 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01004717 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004718 }
4719
4720 while (!list_empty(&clk_provider_list)) {
4721 is_init_done = false;
4722 list_for_each_entry_safe(clk_provider, next,
4723 &clk_provider_list, node) {
4724 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004725
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02004726 /* Don't populate platform devices */
4727 of_node_set_flag(clk_provider->np,
4728 OF_POPULATED);
4729
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004730 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004731 of_clk_set_defaults(clk_provider->np, true);
4732
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004733 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004734 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004735 kfree(clk_provider);
4736 is_init_done = true;
4737 }
4738 }
4739
4740 /*
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01004741 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004742 * remaining providers during the last loop, so now we
4743 * initialize all the remaining ones unconditionally
4744 * in case the clock parent was not mandatory
4745 */
4746 if (!is_init_done)
4747 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05004748 }
4749}
4750#endif