blob: f9076c74bf0d81684cfc2dbe2a83f7fca12caf48 [file] [log] [blame]
Stephen Boydebafb632018-12-11 09:43:03 -08001// SPDX-License-Identifier: GPL-2.0
Mike Turquetteb24764902012-03-15 23:11:19 -07002/*
3 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
4 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
5 *
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03006 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
Mike Turquetteb24764902012-03-15 23:11:19 -07007 */
8
Stephen Boyd3c373112015-06-19 15:00:46 -07009#include <linux/clk.h>
Michael Turquetteb09d6d92015-01-29 14:22:50 -080010#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020011#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070012#include <linux/module.h>
13#include <linux/mutex.h>
14#include <linux/spinlock.h>
15#include <linux/err.h>
16#include <linux/list.h>
17#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050018#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070019#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053020#include <linux/init.h>
Marek Szyprowski9a34b452017-08-21 10:04:59 +020021#include <linux/pm_runtime.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070022#include <linux/sched.h>
Stephen Boyd562ef0b2015-05-01 12:16:14 -070023#include <linux/clkdev.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070024
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020025#include "clk.h"
26
Mike Turquetteb24764902012-03-15 23:11:19 -070027static DEFINE_SPINLOCK(enable_lock);
28static DEFINE_MUTEX(prepare_lock);
29
Mike Turquette533ddeb2013-03-28 13:59:02 -070030static struct task_struct *prepare_owner;
31static struct task_struct *enable_owner;
32
33static int prepare_refcnt;
34static int enable_refcnt;
35
Mike Turquetteb24764902012-03-15 23:11:19 -070036static HLIST_HEAD(clk_root_list);
37static HLIST_HEAD(clk_orphan_list);
38static LIST_HEAD(clk_notifier_list);
39
Stephen Boydbdcf1dc2019-08-28 11:19:59 -070040static struct hlist_head *all_lists[] = {
41 &clk_root_list,
42 &clk_orphan_list,
43 NULL,
44};
45
Michael Turquetteb09d6d92015-01-29 14:22:50 -080046/*** private data structures ***/
47
Stephen Boydfc0c2092019-04-12 11:31:47 -070048struct clk_parent_map {
49 const struct clk_hw *hw;
50 struct clk_core *core;
51 const char *fw_name;
52 const char *name;
Stephen Boyd601b6e92019-04-12 11:31:49 -070053 int index;
Stephen Boydfc0c2092019-04-12 11:31:47 -070054};
55
Michael Turquetteb09d6d92015-01-29 14:22:50 -080056struct clk_core {
57 const char *name;
58 const struct clk_ops *ops;
59 struct clk_hw *hw;
60 struct module *owner;
Marek Szyprowski9a34b452017-08-21 10:04:59 +020061 struct device *dev;
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -070062 struct device_node *of_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080063 struct clk_core *parent;
Stephen Boydfc0c2092019-04-12 11:31:47 -070064 struct clk_parent_map *parents;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080065 u8 num_parents;
66 u8 new_parent_index;
67 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010068 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080069 unsigned long new_rate;
70 struct clk_core *new_parent;
71 struct clk_core *new_child;
72 unsigned long flags;
Heiko Stuebnere6500342015-04-22 22:53:05 +020073 bool orphan;
Miquel Raynal24478832018-12-04 20:24:37 +010074 bool rpm_enabled;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080075 unsigned int enable_count;
76 unsigned int prepare_count;
Jerome Brunete55a8392017-12-01 22:51:56 +010077 unsigned int protect_count;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070078 unsigned long min_rate;
79 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080080 unsigned long accuracy;
81 int phase;
Jerome Brunet9fba7382018-06-19 16:41:41 +020082 struct clk_duty duty;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080083 struct hlist_head children;
84 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010085 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080086 unsigned int notifier_count;
87#ifdef CONFIG_DEBUG_FS
88 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020089 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080090#endif
91 struct kref ref;
92};
93
Stephen Boyddfc202e2015-02-02 14:37:41 -080094#define CREATE_TRACE_POINTS
95#include <trace/events/clk.h>
96
Michael Turquetteb09d6d92015-01-29 14:22:50 -080097struct clk {
98 struct clk_core *core;
Stephen Boydefa85042018-12-11 08:34:16 -080099 struct device *dev;
Michael Turquetteb09d6d92015-01-29 14:22:50 -0800100 const char *dev_id;
101 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100102 unsigned long min_rate;
103 unsigned long max_rate;
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100104 unsigned int exclusive_count;
Stephen Boyd50595f82015-02-06 11:42:44 -0800105 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -0800106};
107
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200108/*** runtime pm ***/
109static int clk_pm_runtime_get(struct clk_core *core)
110{
Miquel Raynal24478832018-12-04 20:24:37 +0100111 int ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200112
Miquel Raynal24478832018-12-04 20:24:37 +0100113 if (!core->rpm_enabled)
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200114 return 0;
115
116 ret = pm_runtime_get_sync(core->dev);
117 return ret < 0 ? ret : 0;
118}
119
120static void clk_pm_runtime_put(struct clk_core *core)
121{
Miquel Raynal24478832018-12-04 20:24:37 +0100122 if (!core->rpm_enabled)
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200123 return;
124
125 pm_runtime_put_sync(core->dev);
126}
127
Mike Turquetteeab89f62013-03-28 13:59:01 -0700128/*** locking ***/
129static void clk_prepare_lock(void)
130{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700131 if (!mutex_trylock(&prepare_lock)) {
132 if (prepare_owner == current) {
133 prepare_refcnt++;
134 return;
135 }
136 mutex_lock(&prepare_lock);
137 }
138 WARN_ON_ONCE(prepare_owner != NULL);
139 WARN_ON_ONCE(prepare_refcnt != 0);
140 prepare_owner = current;
141 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700142}
143
144static void clk_prepare_unlock(void)
145{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700146 WARN_ON_ONCE(prepare_owner != current);
147 WARN_ON_ONCE(prepare_refcnt == 0);
148
149 if (--prepare_refcnt)
150 return;
151 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700152 mutex_unlock(&prepare_lock);
153}
154
155static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700156 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700157{
158 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700159
David Lechnera12aa8a2018-01-04 19:46:08 -0600160 /*
161 * On UP systems, spin_trylock_irqsave() always returns true, even if
162 * we already hold the lock. So, in that case, we rely only on
163 * reference counting.
164 */
165 if (!IS_ENABLED(CONFIG_SMP) ||
166 !spin_trylock_irqsave(&enable_lock, flags)) {
Mike Turquette533ddeb2013-03-28 13:59:02 -0700167 if (enable_owner == current) {
168 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700169 __acquire(enable_lock);
David Lechnera12aa8a2018-01-04 19:46:08 -0600170 if (!IS_ENABLED(CONFIG_SMP))
171 local_save_flags(flags);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700172 return flags;
173 }
174 spin_lock_irqsave(&enable_lock, flags);
175 }
176 WARN_ON_ONCE(enable_owner != NULL);
177 WARN_ON_ONCE(enable_refcnt != 0);
178 enable_owner = current;
179 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700180 return flags;
181}
182
183static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700184 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700185{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700186 WARN_ON_ONCE(enable_owner != current);
187 WARN_ON_ONCE(enable_refcnt == 0);
188
Stephen Boyda57aa182015-07-24 12:24:48 -0700189 if (--enable_refcnt) {
190 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700191 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700192 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700193 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700194 spin_unlock_irqrestore(&enable_lock, flags);
195}
196
Jerome Brunete55a8392017-12-01 22:51:56 +0100197static bool clk_core_rate_is_protected(struct clk_core *core)
198{
199 return core->protect_count;
200}
201
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700202static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530203{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200204 bool ret = false;
205
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700206 /*
207 * .is_prepared is optional for clocks that can prepare
208 * fall back to software usage counter if it is missing
209 */
210 if (!core->ops->is_prepared)
211 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530212
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200213 if (!clk_pm_runtime_get(core)) {
214 ret = core->ops->is_prepared(core->hw);
215 clk_pm_runtime_put(core);
216 }
217
218 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530219}
220
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700221static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530222{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200223 bool ret = false;
224
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700225 /*
226 * .is_enabled is only mandatory for clocks that gate
227 * fall back to software usage counter if .is_enabled is missing
228 */
229 if (!core->ops->is_enabled)
230 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530231
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200232 /*
233 * Check if clock controller's device is runtime active before
234 * calling .is_enabled callback. If not, assume that clock is
235 * disabled, because we might be called from atomic context, from
236 * which pm_runtime_get() is not allowed.
237 * This function is called mainly from clk_disable_unused_subtree,
238 * which ensures proper runtime pm activation of controller before
239 * taking enable spinlock, but the below check is needed if one tries
240 * to call it from other places.
241 */
Miquel Raynal24478832018-12-04 20:24:37 +0100242 if (core->rpm_enabled) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200243 pm_runtime_get_noresume(core->dev);
244 if (!pm_runtime_active(core->dev)) {
245 ret = false;
246 goto done;
247 }
248 }
249
250 ret = core->ops->is_enabled(core->hw);
251done:
Miquel Raynal24478832018-12-04 20:24:37 +0100252 if (core->rpm_enabled)
Dong Aisheng756efe12017-12-22 17:46:04 +0800253 pm_runtime_put(core->dev);
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200254
255 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530256}
257
Mike Turquetteb24764902012-03-15 23:11:19 -0700258/*** helper functions ***/
259
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200260const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700261{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100262 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700263}
Niels de Vos48950842012-12-13 13:12:25 +0100264EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700265
Stephen Boyde7df6f62015-08-12 13:04:56 -0700266const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700267{
268 return hw->core->name;
269}
270EXPORT_SYMBOL_GPL(clk_hw_get_name);
271
Russ Dill65800b22012-11-26 11:20:09 -0800272struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700273{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100274 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700275}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800276EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700277
Stephen Boyde7df6f62015-08-12 13:04:56 -0700278unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700279{
280 return hw->core->num_parents;
281}
282EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
283
Stephen Boyde7df6f62015-08-12 13:04:56 -0700284struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700285{
286 return hw->core->parent ? hw->core->parent->hw : NULL;
287}
288EXPORT_SYMBOL_GPL(clk_hw_get_parent);
289
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700290static struct clk_core *__clk_lookup_subtree(const char *name,
291 struct clk_core *core)
292{
293 struct clk_core *child;
294 struct clk_core *ret;
295
296 if (!strcmp(core->name, name))
297 return core;
298
299 hlist_for_each_entry(child, &core->children, child_node) {
300 ret = __clk_lookup_subtree(name, child);
301 if (ret)
302 return ret;
303 }
304
305 return NULL;
306}
307
308static struct clk_core *clk_core_lookup(const char *name)
309{
310 struct clk_core *root_clk;
311 struct clk_core *ret;
312
313 if (!name)
314 return NULL;
315
316 /* search the 'proper' clk tree first */
317 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
318 ret = __clk_lookup_subtree(name, root_clk);
319 if (ret)
320 return ret;
321 }
322
323 /* if not found, then search the orphan tree */
324 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
325 ret = __clk_lookup_subtree(name, root_clk);
326 if (ret)
327 return ret;
328 }
329
330 return NULL;
331}
332
Stephen Boydfc0c2092019-04-12 11:31:47 -0700333/**
Stephen Boyddde4eff2019-04-12 11:31:48 -0700334 * clk_core_get - Find the clk_core parent of a clk
Stephen Boydfc0c2092019-04-12 11:31:47 -0700335 * @core: clk to find parent of
Stephen Boyd1a079562019-04-30 10:22:30 -0700336 * @p_index: parent index to search for
Stephen Boydfc0c2092019-04-12 11:31:47 -0700337 *
338 * This is the preferred method for clk providers to find the parent of a
339 * clk when that parent is external to the clk controller. The parent_names
340 * array is indexed and treated as a local name matching a string in the device
Stephen Boyddde4eff2019-04-12 11:31:48 -0700341 * node's 'clock-names' property or as the 'con_id' matching the device's
342 * dev_name() in a clk_lookup. This allows clk providers to use their own
Stephen Boydfc0c2092019-04-12 11:31:47 -0700343 * namespace instead of looking for a globally unique parent string.
344 *
345 * For example the following DT snippet would allow a clock registered by the
346 * clock-controller@c001 that has a clk_init_data::parent_data array
347 * with 'xtal' in the 'name' member to find the clock provided by the
348 * clock-controller@f00abcd without needing to get the globally unique name of
349 * the xtal clk.
350 *
351 * parent: clock-controller@f00abcd {
352 * reg = <0xf00abcd 0xabcd>;
353 * #clock-cells = <0>;
354 * };
355 *
356 * clock-controller@c001 {
357 * reg = <0xc001 0xf00d>;
358 * clocks = <&parent>;
359 * clock-names = "xtal";
360 * #clock-cells = <1>;
361 * };
362 *
363 * Returns: -ENOENT when the provider can't be found or the clk doesn't
364 * exist in the provider. -EINVAL when the name can't be found. NULL when the
365 * provider knows about the clk but it isn't provided on this system.
366 * A valid clk_core pointer when the clk can be found in the provider.
367 */
Stephen Boyd1a079562019-04-30 10:22:30 -0700368static struct clk_core *clk_core_get(struct clk_core *core, u8 p_index)
Stephen Boydfc0c2092019-04-12 11:31:47 -0700369{
Stephen Boyd1a079562019-04-30 10:22:30 -0700370 const char *name = core->parents[p_index].fw_name;
371 int index = core->parents[p_index].index;
Stephen Boyddde4eff2019-04-12 11:31:48 -0700372 struct clk_hw *hw = ERR_PTR(-ENOENT);
373 struct device *dev = core->dev;
374 const char *dev_id = dev ? dev_name(dev) : NULL;
Stephen Boydfc0c2092019-04-12 11:31:47 -0700375 struct device_node *np = core->of_node;
376
Stephen Boydc8edb312019-06-14 10:46:06 -0700377 if (np && (name || index >= 0))
Stephen Boyd601b6e92019-04-12 11:31:49 -0700378 hw = of_clk_get_hw(np, index, name);
Stephen Boydfc0c2092019-04-12 11:31:47 -0700379
Stephen Boyddde4eff2019-04-12 11:31:48 -0700380 /*
381 * If the DT search above couldn't find the provider or the provider
382 * didn't know about this clk, fallback to looking up via clkdev based
383 * clk_lookups
384 */
Stephen Boyd601b6e92019-04-12 11:31:49 -0700385 if (PTR_ERR(hw) == -ENOENT && name)
Stephen Boyddde4eff2019-04-12 11:31:48 -0700386 hw = clk_find_hw(dev_id, name);
387
388 if (IS_ERR(hw))
Stephen Boydfc0c2092019-04-12 11:31:47 -0700389 return ERR_CAST(hw);
390
391 return hw->core;
392}
393
394static void clk_core_fill_parent_index(struct clk_core *core, u8 index)
395{
396 struct clk_parent_map *entry = &core->parents[index];
397 struct clk_core *parent = ERR_PTR(-ENOENT);
398
399 if (entry->hw) {
400 parent = entry->hw->core;
401 /*
402 * We have a direct reference but it isn't registered yet?
403 * Orphan it and let clk_reparent() update the orphan status
404 * when the parent is registered.
405 */
406 if (!parent)
407 parent = ERR_PTR(-EPROBE_DEFER);
408 } else {
Stephen Boyd1a079562019-04-30 10:22:30 -0700409 parent = clk_core_get(core, index);
Stephen Boydfc0c2092019-04-12 11:31:47 -0700410 if (IS_ERR(parent) && PTR_ERR(parent) == -ENOENT)
411 parent = clk_core_lookup(entry->name);
412 }
413
414 /* Only cache it if it's not an error */
415 if (!IS_ERR(parent))
416 entry->core = parent;
417}
418
Stephen Boydd6968fc2015-04-30 13:54:13 -0700419static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100420 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100421{
Stephen Boydfc0c2092019-04-12 11:31:47 -0700422 if (!core || index >= core->num_parents || !core->parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100423 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900424
Stephen Boydfc0c2092019-04-12 11:31:47 -0700425 if (!core->parents[index].core)
426 clk_core_fill_parent_index(core, index);
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900427
Stephen Boydfc0c2092019-04-12 11:31:47 -0700428 return core->parents[index].core;
James Hogan7ef3dcc2013-07-29 12:24:58 +0100429}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100430
Stephen Boyde7df6f62015-08-12 13:04:56 -0700431struct clk_hw *
432clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700433{
434 struct clk_core *parent;
435
436 parent = clk_core_get_parent_by_index(hw->core, index);
437
438 return !parent ? NULL : parent->hw;
439}
440EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
441
Russ Dill65800b22012-11-26 11:20:09 -0800442unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700443{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100444 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700445}
446
Stephen Boydd6968fc2015-04-30 13:54:13 -0700447static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700448{
Stephen Boyd73d4f942019-02-01 15:39:50 -0800449 if (!core)
450 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700451
Stephen Boyd73d4f942019-02-01 15:39:50 -0800452 if (!core->num_parents || core->parent)
453 return core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700454
Stephen Boyd73d4f942019-02-01 15:39:50 -0800455 /*
456 * Clk must have a parent because num_parents > 0 but the parent isn't
457 * known yet. Best to return 0 as the rate of this clk until we can
458 * properly recalc the rate based on the parent's rate.
459 */
460 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700461}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100462
Stephen Boyde7df6f62015-08-12 13:04:56 -0700463unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700464{
465 return clk_core_get_rate_nolock(hw->core);
466}
467EXPORT_SYMBOL_GPL(clk_hw_get_rate);
468
Stephen Boydd6968fc2015-04-30 13:54:13 -0700469static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100470{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700471 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100472 return 0;
473
Stephen Boydd6968fc2015-04-30 13:54:13 -0700474 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100475}
476
Russ Dill65800b22012-11-26 11:20:09 -0800477unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700478{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100479 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700480}
Thierry Redingb05c6832013-09-03 09:43:51 +0200481EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700482
Stephen Boyde7df6f62015-08-12 13:04:56 -0700483unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700484{
485 return hw->core->flags;
486}
487EXPORT_SYMBOL_GPL(clk_hw_get_flags);
488
Stephen Boyde7df6f62015-08-12 13:04:56 -0700489bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700490{
491 return clk_core_is_prepared(hw->core);
492}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100493EXPORT_SYMBOL_GPL(clk_hw_is_prepared);
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700494
Jerome Brunete55a8392017-12-01 22:51:56 +0100495bool clk_hw_rate_is_protected(const struct clk_hw *hw)
496{
497 return clk_core_rate_is_protected(hw->core);
498}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100499EXPORT_SYMBOL_GPL(clk_hw_rate_is_protected);
Jerome Brunete55a8392017-12-01 22:51:56 +0100500
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200501bool clk_hw_is_enabled(const struct clk_hw *hw)
502{
503 return clk_core_is_enabled(hw->core);
504}
Jerome Brunet12aa3772019-02-01 13:58:38 +0100505EXPORT_SYMBOL_GPL(clk_hw_is_enabled);
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200506
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100507bool __clk_is_enabled(struct clk *clk)
508{
509 if (!clk)
510 return false;
511
512 return clk_core_is_enabled(clk->core);
513}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800514EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700515
Stephen Boyd15a02c12015-01-19 18:05:28 -0800516static bool mux_is_better_rate(unsigned long rate, unsigned long now,
517 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100518{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800519 if (flags & CLK_MUX_ROUND_CLOSEST)
520 return abs(now - rate) < abs(best - rate);
521
522 return now <= rate && now > best;
523}
524
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200525int clk_mux_determine_rate_flags(struct clk_hw *hw,
526 struct clk_rate_request *req,
527 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100528{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100529 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200530 int i, num_parents, ret;
531 unsigned long best = 0;
532 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100533
534 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100535 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
536 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200537 if (core->flags & CLK_SET_RATE_PARENT) {
538 ret = __clk_determine_rate(parent ? parent->hw : NULL,
539 &parent_req);
540 if (ret)
541 return ret;
542
543 best = parent_req.rate;
544 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100545 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200546 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100547 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200548 }
549
James Hogane366fdd2013-07-29 12:25:02 +0100550 goto out;
551 }
552
553 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100554 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100555 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100556 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100557 if (!parent)
558 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200559
560 if (core->flags & CLK_SET_RATE_PARENT) {
561 parent_req = *req;
562 ret = __clk_determine_rate(parent->hw, &parent_req);
563 if (ret)
564 continue;
565 } else {
566 parent_req.rate = clk_core_get_rate_nolock(parent);
567 }
568
569 if (mux_is_better_rate(req->rate, parent_req.rate,
570 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100571 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200572 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100573 }
574 }
575
Boris Brezillon57d866e2015-07-09 22:39:38 +0200576 if (!best_parent)
577 return -EINVAL;
578
James Hogane366fdd2013-07-29 12:25:02 +0100579out:
580 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200581 req->best_parent_hw = best_parent->hw;
582 req->best_parent_rate = best;
583 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100584
Boris Brezillon0817b622015-07-07 20:48:08 +0200585 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100586}
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200587EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800588
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100589struct clk *__clk_lookup(const char *name)
590{
591 struct clk_core *core = clk_core_lookup(name);
592
593 return !core ? NULL : core->hw->clk;
594}
595
Stephen Boydd6968fc2015-04-30 13:54:13 -0700596static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100597 unsigned long *min_rate,
598 unsigned long *max_rate)
599{
600 struct clk *clk_user;
601
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700602 *min_rate = core->min_rate;
603 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100604
Stephen Boydd6968fc2015-04-30 13:54:13 -0700605 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100606 *min_rate = max(*min_rate, clk_user->min_rate);
607
Stephen Boydd6968fc2015-04-30 13:54:13 -0700608 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100609 *max_rate = min(*max_rate, clk_user->max_rate);
610}
611
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700612void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
613 unsigned long max_rate)
614{
615 hw->core->min_rate = min_rate;
616 hw->core->max_rate = max_rate;
617}
618EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
619
Stephen Boyd15a02c12015-01-19 18:05:28 -0800620/*
Stephen Boyd777c1a42018-12-11 13:24:50 -0800621 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
622 * @hw: mux type clk to determine rate on
623 * @req: rate request, also used to return preferred parent and frequencies
624 *
Stephen Boyd15a02c12015-01-19 18:05:28 -0800625 * Helper for finding best parent to provide a given frequency. This can be used
626 * directly as a determine_rate callback (e.g. for a mux), or from a more
627 * complex clock that may combine a mux with other operations.
Stephen Boyd777c1a42018-12-11 13:24:50 -0800628 *
629 * Returns: 0 on success, -EERROR value on error
Stephen Boyd15a02c12015-01-19 18:05:28 -0800630 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200631int __clk_mux_determine_rate(struct clk_hw *hw,
632 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800633{
Boris Brezillon0817b622015-07-07 20:48:08 +0200634 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800635}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800636EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100637
Boris Brezillon0817b622015-07-07 20:48:08 +0200638int __clk_mux_determine_rate_closest(struct clk_hw *hw,
639 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800640{
Boris Brezillon0817b622015-07-07 20:48:08 +0200641 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800642}
643EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
644
Mike Turquetteb24764902012-03-15 23:11:19 -0700645/*** clk api ***/
646
Jerome Brunete55a8392017-12-01 22:51:56 +0100647static void clk_core_rate_unprotect(struct clk_core *core)
648{
649 lockdep_assert_held(&prepare_lock);
650
651 if (!core)
652 return;
653
Fabio Estevamab525dc2018-01-16 10:50:34 -0200654 if (WARN(core->protect_count == 0,
655 "%s already unprotected\n", core->name))
Jerome Brunete55a8392017-12-01 22:51:56 +0100656 return;
657
658 if (--core->protect_count > 0)
659 return;
660
661 clk_core_rate_unprotect(core->parent);
662}
663
664static int clk_core_rate_nuke_protect(struct clk_core *core)
665{
666 int ret;
667
668 lockdep_assert_held(&prepare_lock);
669
670 if (!core)
671 return -EINVAL;
672
673 if (core->protect_count == 0)
674 return 0;
675
676 ret = core->protect_count;
677 core->protect_count = 1;
678 clk_core_rate_unprotect(core);
679
680 return ret;
681}
682
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100683/**
684 * clk_rate_exclusive_put - release exclusivity over clock rate control
685 * @clk: the clk over which the exclusivity is released
686 *
687 * clk_rate_exclusive_put() completes a critical section during which a clock
688 * consumer cannot tolerate any other consumer making any operation on the
689 * clock which could result in a rate change or rate glitch. Exclusive clocks
690 * cannot have their rate changed, either directly or indirectly due to changes
691 * further up the parent chain of clocks. As a result, clocks up parent chain
692 * also get under exclusive control of the calling consumer.
693 *
694 * If exlusivity is claimed more than once on clock, even by the same consumer,
695 * the rate effectively gets locked as exclusivity can't be preempted.
696 *
697 * Calls to clk_rate_exclusive_put() must be balanced with calls to
698 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
699 * error status.
700 */
701void clk_rate_exclusive_put(struct clk *clk)
702{
703 if (!clk)
704 return;
705
706 clk_prepare_lock();
707
708 /*
709 * if there is something wrong with this consumer protect count, stop
710 * here before messing with the provider
711 */
712 if (WARN_ON(clk->exclusive_count <= 0))
713 goto out;
714
715 clk_core_rate_unprotect(clk->core);
716 clk->exclusive_count--;
717out:
718 clk_prepare_unlock();
719}
720EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
721
Jerome Brunete55a8392017-12-01 22:51:56 +0100722static void clk_core_rate_protect(struct clk_core *core)
723{
724 lockdep_assert_held(&prepare_lock);
725
726 if (!core)
727 return;
728
729 if (core->protect_count == 0)
730 clk_core_rate_protect(core->parent);
731
732 core->protect_count++;
733}
734
735static void clk_core_rate_restore_protect(struct clk_core *core, int count)
736{
737 lockdep_assert_held(&prepare_lock);
738
739 if (!core)
740 return;
741
742 if (count == 0)
743 return;
744
745 clk_core_rate_protect(core);
746 core->protect_count = count;
747}
748
Jerome Brunet55e9b8b2017-12-01 22:51:59 +0100749/**
750 * clk_rate_exclusive_get - get exclusivity over the clk rate control
751 * @clk: the clk over which the exclusity of rate control is requested
752 *
753 * clk_rate_exlusive_get() begins a critical section during which a clock
754 * consumer cannot tolerate any other consumer making any operation on the
755 * clock which could result in a rate change or rate glitch. Exclusive clocks
756 * cannot have their rate changed, either directly or indirectly due to changes
757 * further up the parent chain of clocks. As a result, clocks up parent chain
758 * also get under exclusive control of the calling consumer.
759 *
760 * If exlusivity is claimed more than once on clock, even by the same consumer,
761 * the rate effectively gets locked as exclusivity can't be preempted.
762 *
763 * Calls to clk_rate_exclusive_get() should be balanced with calls to
764 * clk_rate_exclusive_put(). Calls to this function may sleep.
765 * Returns 0 on success, -EERROR otherwise
766 */
767int clk_rate_exclusive_get(struct clk *clk)
768{
769 if (!clk)
770 return 0;
771
772 clk_prepare_lock();
773 clk_core_rate_protect(clk->core);
774 clk->exclusive_count++;
775 clk_prepare_unlock();
776
777 return 0;
778}
779EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
780
Stephen Boydd6968fc2015-04-30 13:54:13 -0700781static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700782{
Stephen Boyda6334722015-05-06 17:00:54 -0700783 lockdep_assert_held(&prepare_lock);
784
Stephen Boydd6968fc2015-04-30 13:54:13 -0700785 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700786 return;
787
Fabio Estevamab525dc2018-01-16 10:50:34 -0200788 if (WARN(core->prepare_count == 0,
789 "%s already unprepared\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700790 return;
791
Fabio Estevamab525dc2018-01-16 10:50:34 -0200792 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
793 "Unpreparing critical %s\n", core->name))
Lee Jones2e20fbf2016-02-11 13:19:10 -0800794 return;
795
Jerome Brunet9461f7b2018-06-19 15:40:51 +0200796 if (core->flags & CLK_SET_RATE_GATE)
797 clk_core_rate_unprotect(core);
798
Stephen Boydd6968fc2015-04-30 13:54:13 -0700799 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700800 return;
801
Fabio Estevamab525dc2018-01-16 10:50:34 -0200802 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700803
Stephen Boydd6968fc2015-04-30 13:54:13 -0700804 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800805
Stephen Boydd6968fc2015-04-30 13:54:13 -0700806 if (core->ops->unprepare)
807 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700808
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200809 clk_pm_runtime_put(core);
810
Stephen Boydd6968fc2015-04-30 13:54:13 -0700811 trace_clk_unprepare_complete(core);
812 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700813}
814
Dong Aishenga6adc302016-06-30 17:31:11 +0800815static void clk_core_unprepare_lock(struct clk_core *core)
816{
817 clk_prepare_lock();
818 clk_core_unprepare(core);
819 clk_prepare_unlock();
820}
821
Mike Turquetteb24764902012-03-15 23:11:19 -0700822/**
823 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200824 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700825 *
826 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
827 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
828 * if the operation may sleep. One example is a clk which is accessed over
829 * I2c. In the complex case a clk gate operation may require a fast and a slow
830 * part. It is this reason that clk_unprepare and clk_disable are not mutually
831 * exclusive. In fact clk_disable must be called before clk_unprepare.
832 */
833void clk_unprepare(struct clk *clk)
834{
Stephen Boyd63589e92014-03-26 16:06:37 -0700835 if (IS_ERR_OR_NULL(clk))
836 return;
837
Dong Aishenga6adc302016-06-30 17:31:11 +0800838 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700839}
840EXPORT_SYMBOL_GPL(clk_unprepare);
841
Stephen Boydd6968fc2015-04-30 13:54:13 -0700842static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700843{
844 int ret = 0;
845
Stephen Boyda6334722015-05-06 17:00:54 -0700846 lockdep_assert_held(&prepare_lock);
847
Stephen Boydd6968fc2015-04-30 13:54:13 -0700848 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700849 return 0;
850
Stephen Boydd6968fc2015-04-30 13:54:13 -0700851 if (core->prepare_count == 0) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200852 ret = clk_pm_runtime_get(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700853 if (ret)
854 return ret;
855
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200856 ret = clk_core_prepare(core->parent);
857 if (ret)
858 goto runtime_put;
859
Stephen Boydd6968fc2015-04-30 13:54:13 -0700860 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800861
Stephen Boydd6968fc2015-04-30 13:54:13 -0700862 if (core->ops->prepare)
863 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800864
Stephen Boydd6968fc2015-04-30 13:54:13 -0700865 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800866
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200867 if (ret)
868 goto unprepare;
Mike Turquetteb24764902012-03-15 23:11:19 -0700869 }
870
Stephen Boydd6968fc2015-04-30 13:54:13 -0700871 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700872
Jerome Brunet9461f7b2018-06-19 15:40:51 +0200873 /*
874 * CLK_SET_RATE_GATE is a special case of clock protection
875 * Instead of a consumer claiming exclusive rate control, it is
876 * actually the provider which prevents any consumer from making any
877 * operation which could result in a rate change or rate glitch while
878 * the clock is prepared.
879 */
880 if (core->flags & CLK_SET_RATE_GATE)
881 clk_core_rate_protect(core);
882
Mike Turquetteb24764902012-03-15 23:11:19 -0700883 return 0;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200884unprepare:
885 clk_core_unprepare(core->parent);
886runtime_put:
887 clk_pm_runtime_put(core);
888 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700889}
890
Dong Aishenga6adc302016-06-30 17:31:11 +0800891static int clk_core_prepare_lock(struct clk_core *core)
892{
893 int ret;
894
895 clk_prepare_lock();
896 ret = clk_core_prepare(core);
897 clk_prepare_unlock();
898
899 return ret;
900}
901
Mike Turquetteb24764902012-03-15 23:11:19 -0700902/**
903 * clk_prepare - prepare a clock source
904 * @clk: the clk being prepared
905 *
906 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
907 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
908 * operation may sleep. One example is a clk which is accessed over I2c. In
909 * the complex case a clk ungate operation may require a fast and a slow part.
910 * It is this reason that clk_prepare and clk_enable are not mutually
911 * exclusive. In fact clk_prepare must be called before clk_enable.
912 * Returns 0 on success, -EERROR otherwise.
913 */
914int clk_prepare(struct clk *clk)
915{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100916 if (!clk)
917 return 0;
918
Dong Aishenga6adc302016-06-30 17:31:11 +0800919 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700920}
921EXPORT_SYMBOL_GPL(clk_prepare);
922
Stephen Boydd6968fc2015-04-30 13:54:13 -0700923static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700924{
Stephen Boyda6334722015-05-06 17:00:54 -0700925 lockdep_assert_held(&enable_lock);
926
Stephen Boydd6968fc2015-04-30 13:54:13 -0700927 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700928 return;
929
Fabio Estevamab525dc2018-01-16 10:50:34 -0200930 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700931 return;
932
Fabio Estevamab525dc2018-01-16 10:50:34 -0200933 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
934 "Disabling critical %s\n", core->name))
Lee Jones2e20fbf2016-02-11 13:19:10 -0800935 return;
936
Stephen Boydd6968fc2015-04-30 13:54:13 -0700937 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700938 return;
939
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700940 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800941
Stephen Boydd6968fc2015-04-30 13:54:13 -0700942 if (core->ops->disable)
943 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700944
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700945 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800946
Stephen Boydd6968fc2015-04-30 13:54:13 -0700947 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100948}
949
Dong Aishenga6adc302016-06-30 17:31:11 +0800950static void clk_core_disable_lock(struct clk_core *core)
951{
952 unsigned long flags;
953
954 flags = clk_enable_lock();
955 clk_core_disable(core);
956 clk_enable_unlock(flags);
957}
958
Mike Turquetteb24764902012-03-15 23:11:19 -0700959/**
960 * clk_disable - gate a clock
961 * @clk: the clk being gated
962 *
963 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
964 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
965 * clk if the operation is fast and will never sleep. One example is a
966 * SoC-internal clk which is controlled via simple register writes. In the
967 * complex case a clk gate operation may require a fast and a slow part. It is
968 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
969 * In fact clk_disable must be called before clk_unprepare.
970 */
971void clk_disable(struct clk *clk)
972{
Stephen Boyd63589e92014-03-26 16:06:37 -0700973 if (IS_ERR_OR_NULL(clk))
974 return;
975
Dong Aishenga6adc302016-06-30 17:31:11 +0800976 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700977}
978EXPORT_SYMBOL_GPL(clk_disable);
979
Stephen Boydd6968fc2015-04-30 13:54:13 -0700980static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700981{
982 int ret = 0;
983
Stephen Boyda6334722015-05-06 17:00:54 -0700984 lockdep_assert_held(&enable_lock);
985
Stephen Boydd6968fc2015-04-30 13:54:13 -0700986 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700987 return 0;
988
Fabio Estevamab525dc2018-01-16 10:50:34 -0200989 if (WARN(core->prepare_count == 0,
990 "Enabling unprepared %s\n", core->name))
Mike Turquetteb24764902012-03-15 23:11:19 -0700991 return -ESHUTDOWN;
992
Stephen Boydd6968fc2015-04-30 13:54:13 -0700993 if (core->enable_count == 0) {
994 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700995
996 if (ret)
997 return ret;
998
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700999 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001000
Stephen Boydd6968fc2015-04-30 13:54:13 -07001001 if (core->ops->enable)
1002 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001003
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -07001004 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001005
1006 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001007 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001008 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001009 }
1010 }
1011
Stephen Boydd6968fc2015-04-30 13:54:13 -07001012 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07001013 return 0;
1014}
1015
Dong Aishenga6adc302016-06-30 17:31:11 +08001016static int clk_core_enable_lock(struct clk_core *core)
1017{
1018 unsigned long flags;
1019 int ret;
1020
1021 flags = clk_enable_lock();
1022 ret = clk_core_enable(core);
1023 clk_enable_unlock(flags);
1024
1025 return ret;
1026}
1027
Keerthy43536542018-09-04 12:19:36 +05301028/**
1029 * clk_gate_restore_context - restore context for poweroff
1030 * @hw: the clk_hw pointer of clock whose state is to be restored
1031 *
1032 * The clock gate restore context function enables or disables
1033 * the gate clocks based on the enable_count. This is done in cases
1034 * where the clock context is lost and based on the enable_count
1035 * the clock either needs to be enabled/disabled. This
1036 * helps restore the state of gate clocks.
1037 */
1038void clk_gate_restore_context(struct clk_hw *hw)
1039{
Stephen Boyd9be76622018-10-11 09:28:13 -07001040 struct clk_core *core = hw->core;
1041
1042 if (core->enable_count)
1043 core->ops->enable(hw);
Keerthy43536542018-09-04 12:19:36 +05301044 else
Stephen Boyd9be76622018-10-11 09:28:13 -07001045 core->ops->disable(hw);
Keerthy43536542018-09-04 12:19:36 +05301046}
1047EXPORT_SYMBOL_GPL(clk_gate_restore_context);
1048
Stephen Boyd9be76622018-10-11 09:28:13 -07001049static int clk_core_save_context(struct clk_core *core)
Russ Dill8b95d1c2018-09-04 12:19:35 +05301050{
1051 struct clk_core *child;
1052 int ret = 0;
1053
Stephen Boyd9be76622018-10-11 09:28:13 -07001054 hlist_for_each_entry(child, &core->children, child_node) {
1055 ret = clk_core_save_context(child);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301056 if (ret < 0)
1057 return ret;
1058 }
1059
Stephen Boyd9be76622018-10-11 09:28:13 -07001060 if (core->ops && core->ops->save_context)
1061 ret = core->ops->save_context(core->hw);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301062
1063 return ret;
1064}
1065
Stephen Boyd9be76622018-10-11 09:28:13 -07001066static void clk_core_restore_context(struct clk_core *core)
Russ Dill8b95d1c2018-09-04 12:19:35 +05301067{
1068 struct clk_core *child;
1069
Stephen Boyd9be76622018-10-11 09:28:13 -07001070 if (core->ops && core->ops->restore_context)
1071 core->ops->restore_context(core->hw);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301072
Stephen Boyd9be76622018-10-11 09:28:13 -07001073 hlist_for_each_entry(child, &core->children, child_node)
1074 clk_core_restore_context(child);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301075}
1076
1077/**
1078 * clk_save_context - save clock context for poweroff
1079 *
1080 * Saves the context of the clock register for powerstates in which the
1081 * contents of the registers will be lost. Occurs deep within the suspend
1082 * code. Returns 0 on success.
1083 */
1084int clk_save_context(void)
1085{
1086 struct clk_core *clk;
1087 int ret;
1088
1089 hlist_for_each_entry(clk, &clk_root_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 hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
Stephen Boyd9be76622018-10-11 09:28:13 -07001096 ret = clk_core_save_context(clk);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301097 if (ret < 0)
1098 return ret;
1099 }
1100
1101 return 0;
1102}
1103EXPORT_SYMBOL_GPL(clk_save_context);
1104
1105/**
1106 * clk_restore_context - restore clock context after poweroff
1107 *
1108 * Restore the saved clock context upon resume.
1109 *
1110 */
1111void clk_restore_context(void)
1112{
Stephen Boyd9be76622018-10-11 09:28:13 -07001113 struct clk_core *core;
Russ Dill8b95d1c2018-09-04 12:19:35 +05301114
Stephen Boyd9be76622018-10-11 09:28:13 -07001115 hlist_for_each_entry(core, &clk_root_list, child_node)
1116 clk_core_restore_context(core);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301117
Stephen Boyd9be76622018-10-11 09:28:13 -07001118 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1119 clk_core_restore_context(core);
Russ Dill8b95d1c2018-09-04 12:19:35 +05301120}
1121EXPORT_SYMBOL_GPL(clk_restore_context);
1122
Mike Turquetteb24764902012-03-15 23:11:19 -07001123/**
1124 * clk_enable - ungate a clock
1125 * @clk: the clk being ungated
1126 *
1127 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1128 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1129 * if the operation will never sleep. One example is a SoC-internal clk which
1130 * is controlled via simple register writes. In the complex case a clk ungate
1131 * operation may require a fast and a slow part. It is this reason that
1132 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1133 * must be called before clk_enable. Returns 0 on success, -EERROR
1134 * otherwise.
1135 */
1136int clk_enable(struct clk *clk)
1137{
Dong Aisheng864e1602015-04-30 14:02:19 -07001138 if (!clk)
1139 return 0;
1140
Dong Aishenga6adc302016-06-30 17:31:11 +08001141 return clk_core_enable_lock(clk->core);
1142}
1143EXPORT_SYMBOL_GPL(clk_enable);
1144
1145static int clk_core_prepare_enable(struct clk_core *core)
1146{
1147 int ret;
1148
1149 ret = clk_core_prepare_lock(core);
1150 if (ret)
1151 return ret;
1152
1153 ret = clk_core_enable_lock(core);
1154 if (ret)
1155 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001156
1157 return ret;
1158}
Dong Aishenga6adc302016-06-30 17:31:11 +08001159
1160static void clk_core_disable_unprepare(struct clk_core *core)
1161{
1162 clk_core_disable_lock(core);
1163 clk_core_unprepare_lock(core);
1164}
Mike Turquetteb24764902012-03-15 23:11:19 -07001165
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001166static void clk_unprepare_unused_subtree(struct clk_core *core)
1167{
1168 struct clk_core *child;
1169
1170 lockdep_assert_held(&prepare_lock);
1171
1172 hlist_for_each_entry(child, &core->children, child_node)
1173 clk_unprepare_unused_subtree(child);
1174
1175 if (core->prepare_count)
1176 return;
1177
1178 if (core->flags & CLK_IGNORE_UNUSED)
1179 return;
1180
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001181 if (clk_pm_runtime_get(core))
1182 return;
1183
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001184 if (clk_core_is_prepared(core)) {
1185 trace_clk_unprepare(core);
1186 if (core->ops->unprepare_unused)
1187 core->ops->unprepare_unused(core->hw);
1188 else if (core->ops->unprepare)
1189 core->ops->unprepare(core->hw);
1190 trace_clk_unprepare_complete(core);
1191 }
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001192
1193 clk_pm_runtime_put(core);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001194}
1195
1196static void clk_disable_unused_subtree(struct clk_core *core)
1197{
1198 struct clk_core *child;
1199 unsigned long flags;
1200
1201 lockdep_assert_held(&prepare_lock);
1202
1203 hlist_for_each_entry(child, &core->children, child_node)
1204 clk_disable_unused_subtree(child);
1205
Dong Aishenga4b35182016-06-30 17:31:13 +08001206 if (core->flags & CLK_OPS_PARENT_ENABLE)
1207 clk_core_prepare_enable(core->parent);
1208
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001209 if (clk_pm_runtime_get(core))
1210 goto unprepare_out;
1211
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001212 flags = clk_enable_lock();
1213
1214 if (core->enable_count)
1215 goto unlock_out;
1216
1217 if (core->flags & CLK_IGNORE_UNUSED)
1218 goto unlock_out;
1219
1220 /*
1221 * some gate clocks have special needs during the disable-unused
1222 * sequence. call .disable_unused if available, otherwise fall
1223 * back to .disable
1224 */
1225 if (clk_core_is_enabled(core)) {
1226 trace_clk_disable(core);
1227 if (core->ops->disable_unused)
1228 core->ops->disable_unused(core->hw);
1229 else if (core->ops->disable)
1230 core->ops->disable(core->hw);
1231 trace_clk_disable_complete(core);
1232 }
1233
1234unlock_out:
1235 clk_enable_unlock(flags);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001236 clk_pm_runtime_put(core);
1237unprepare_out:
Dong Aishenga4b35182016-06-30 17:31:13 +08001238 if (core->flags & CLK_OPS_PARENT_ENABLE)
1239 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001240}
1241
1242static bool clk_ignore_unused;
1243static int __init clk_ignore_unused_setup(char *__unused)
1244{
1245 clk_ignore_unused = true;
1246 return 1;
1247}
1248__setup("clk_ignore_unused", clk_ignore_unused_setup);
1249
1250static int clk_disable_unused(void)
1251{
1252 struct clk_core *core;
1253
1254 if (clk_ignore_unused) {
1255 pr_warn("clk: Not disabling unused clocks\n");
1256 return 0;
1257 }
1258
1259 clk_prepare_lock();
1260
1261 hlist_for_each_entry(core, &clk_root_list, child_node)
1262 clk_disable_unused_subtree(core);
1263
1264 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1265 clk_disable_unused_subtree(core);
1266
1267 hlist_for_each_entry(core, &clk_root_list, child_node)
1268 clk_unprepare_unused_subtree(core);
1269
1270 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1271 clk_unprepare_unused_subtree(core);
1272
1273 clk_prepare_unlock();
1274
1275 return 0;
1276}
1277late_initcall_sync(clk_disable_unused);
1278
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001279static int clk_core_determine_round_nolock(struct clk_core *core,
1280 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001281{
Boris Brezillon0817b622015-07-07 20:48:08 +02001282 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001283
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001284 lockdep_assert_held(&prepare_lock);
1285
Stephen Boydd6968fc2015-04-30 13:54:13 -07001286 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001287 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001288
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001289 /*
1290 * At this point, core protection will be disabled if
1291 * - if the provider is not protected at all
1292 * - if the calling consumer is the only one which has exclusivity
1293 * over the provider
1294 */
Jerome Brunete55a8392017-12-01 22:51:56 +01001295 if (clk_core_rate_is_protected(core)) {
1296 req->rate = core->rate;
1297 } else if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001298 return core->ops->determine_rate(core->hw, req);
1299 } else if (core->ops->round_rate) {
1300 rate = core->ops->round_rate(core->hw, req->rate,
1301 &req->best_parent_rate);
1302 if (rate < 0)
1303 return rate;
1304
1305 req->rate = rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001306 } else {
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001307 return -EINVAL;
Boris Brezillon0817b622015-07-07 20:48:08 +02001308 }
1309
1310 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001311}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001312
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001313static void clk_core_init_rate_req(struct clk_core * const core,
1314 struct clk_rate_request *req)
1315{
1316 struct clk_core *parent;
1317
1318 if (WARN_ON(!core || !req))
1319 return;
1320
Mike Turquetteb24764902012-03-15 23:11:19 -07001321 parent = core->parent;
1322 if (parent) {
1323 req->best_parent_hw = parent->hw;
1324 req->best_parent_rate = parent->rate;
1325 } else {
1326 req->best_parent_hw = NULL;
1327 req->best_parent_rate = 0;
1328 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001329}
Mike Turquetteb24764902012-03-15 23:11:19 -07001330
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001331static bool clk_core_can_round(struct clk_core * const core)
1332{
Geert Uytterhoeveneef1f1b2019-06-17 14:02:48 +02001333 return core->ops->determine_rate || core->ops->round_rate;
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001334}
Mike Turquetteb24764902012-03-15 23:11:19 -07001335
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001336static int clk_core_round_rate_nolock(struct clk_core *core,
1337 struct clk_rate_request *req)
1338{
1339 lockdep_assert_held(&prepare_lock);
1340
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001341 if (!core) {
1342 req->rate = 0;
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001343 return 0;
Jerome Brunet04bf9ab2018-02-14 14:43:35 +01001344 }
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001345
1346 clk_core_init_rate_req(core, req);
1347
1348 if (clk_core_can_round(core))
1349 return clk_core_determine_round_nolock(core, req);
1350 else if (core->flags & CLK_SET_RATE_PARENT)
1351 return clk_core_round_rate_nolock(core->parent, req);
1352
1353 req->rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001354 return 0;
1355}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001356
1357/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001358 * __clk_determine_rate - get the closest rate actually supported by a clock
1359 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001360 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001361 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001362 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001363 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001364int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001365{
Boris Brezillon0817b622015-07-07 20:48:08 +02001366 if (!hw) {
1367 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001368 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001369 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001370
Boris Brezillon0817b622015-07-07 20:48:08 +02001371 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001372}
1373EXPORT_SYMBOL_GPL(__clk_determine_rate);
1374
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001375unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1376{
1377 int ret;
1378 struct clk_rate_request req;
1379
1380 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1381 req.rate = rate;
1382
1383 ret = clk_core_round_rate_nolock(hw->core, &req);
1384 if (ret)
1385 return 0;
1386
1387 return req.rate;
1388}
1389EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1390
Mike Turquetteb24764902012-03-15 23:11:19 -07001391/**
1392 * clk_round_rate - round the given rate for a clk
1393 * @clk: the clk for which we are rounding a rate
1394 * @rate: the rate which is to be rounded
1395 *
1396 * Takes in a rate as input and rounds it to a rate that the clk can actually
1397 * use which is then returned. If clk doesn't support round_rate operation
1398 * then the parent rate is returned.
1399 */
1400long clk_round_rate(struct clk *clk, unsigned long rate)
1401{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001402 struct clk_rate_request req;
1403 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001404
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001405 if (!clk)
1406 return 0;
1407
Mike Turquetteeab89f62013-03-28 13:59:01 -07001408 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001409
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001410 if (clk->exclusive_count)
1411 clk_core_rate_unprotect(clk->core);
1412
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001413 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1414 req.rate = rate;
1415
1416 ret = clk_core_round_rate_nolock(clk->core, &req);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01001417
1418 if (clk->exclusive_count)
1419 clk_core_rate_protect(clk->core);
1420
Mike Turquetteeab89f62013-03-28 13:59:01 -07001421 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001422
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001423 if (ret)
1424 return ret;
1425
1426 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001427}
1428EXPORT_SYMBOL_GPL(clk_round_rate);
1429
1430/**
1431 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001432 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001433 * @msg: clk notifier type (see include/linux/clk.h)
1434 * @old_rate: old clk rate
1435 * @new_rate: new clk rate
1436 *
1437 * Triggers a notifier call chain on the clk rate-change notification
1438 * for 'clk'. Passes a pointer to the struct clk and the previous
1439 * and current rates to the notifier callback. Intended to be called by
1440 * internal clock code only. Returns NOTIFY_DONE from the last driver
1441 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1442 * a driver returns that.
1443 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001444static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001445 unsigned long old_rate, unsigned long new_rate)
1446{
1447 struct clk_notifier *cn;
1448 struct clk_notifier_data cnd;
1449 int ret = NOTIFY_DONE;
1450
Mike Turquetteb24764902012-03-15 23:11:19 -07001451 cnd.old_rate = old_rate;
1452 cnd.new_rate = new_rate;
1453
1454 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001455 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001456 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001457 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1458 &cnd);
Peter De Schrijver17c34c52017-03-21 12:16:26 +02001459 if (ret & NOTIFY_STOP_MASK)
1460 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001461 }
1462 }
1463
1464 return ret;
1465}
1466
1467/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001468 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001469 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001470 *
1471 * Walks the subtree of clks starting with clk and recalculates accuracies as
1472 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001473 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001474 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001475 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001476static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001477{
1478 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001479 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001480
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001481 lockdep_assert_held(&prepare_lock);
1482
Stephen Boydd6968fc2015-04-30 13:54:13 -07001483 if (core->parent)
1484 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001485
Stephen Boydd6968fc2015-04-30 13:54:13 -07001486 if (core->ops->recalc_accuracy)
1487 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001488 parent_accuracy);
1489 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001490 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001491
Stephen Boydd6968fc2015-04-30 13:54:13 -07001492 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001493 __clk_recalc_accuracies(child);
1494}
1495
Stephen Boydd6968fc2015-04-30 13:54:13 -07001496static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001497{
1498 unsigned long accuracy;
1499
1500 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001501 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1502 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001503
Stephen Boydd6968fc2015-04-30 13:54:13 -07001504 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001505 clk_prepare_unlock();
1506
1507 return accuracy;
1508}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001509
1510/**
1511 * clk_get_accuracy - return the accuracy of clk
1512 * @clk: the clk whose accuracy is being returned
1513 *
1514 * Simply returns the cached accuracy of the clk, unless
1515 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1516 * issued.
1517 * If clk is NULL then returns 0.
1518 */
1519long clk_get_accuracy(struct clk *clk)
1520{
1521 if (!clk)
1522 return 0;
1523
1524 return clk_core_get_accuracy(clk->core);
1525}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001526EXPORT_SYMBOL_GPL(clk_get_accuracy);
1527
Stephen Boydd6968fc2015-04-30 13:54:13 -07001528static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001529 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001530{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001531 unsigned long rate = parent_rate;
1532
1533 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1534 rate = core->ops->recalc_rate(core->hw, parent_rate);
1535 clk_pm_runtime_put(core);
1536 }
1537 return rate;
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001538}
1539
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001540/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001541 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001542 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001543 * @msg: notification type (see include/linux/clk.h)
1544 *
1545 * Walks the subtree of clks starting with clk and recalculates rates as it
1546 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001547 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001548 *
1549 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1550 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001551 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001552static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001553{
1554 unsigned long old_rate;
1555 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001556 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001557
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001558 lockdep_assert_held(&prepare_lock);
1559
Stephen Boydd6968fc2015-04-30 13:54:13 -07001560 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001561
Stephen Boydd6968fc2015-04-30 13:54:13 -07001562 if (core->parent)
1563 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001564
Stephen Boydd6968fc2015-04-30 13:54:13 -07001565 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001566
1567 /*
1568 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1569 * & ABORT_RATE_CHANGE notifiers
1570 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001571 if (core->notifier_count && msg)
1572 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001573
Stephen Boydd6968fc2015-04-30 13:54:13 -07001574 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001575 __clk_recalc_rates(child, msg);
1576}
1577
Stephen Boydd6968fc2015-04-30 13:54:13 -07001578static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001579{
1580 unsigned long rate;
1581
1582 clk_prepare_lock();
1583
Stephen Boydd6968fc2015-04-30 13:54:13 -07001584 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1585 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001586
Stephen Boydd6968fc2015-04-30 13:54:13 -07001587 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001588 clk_prepare_unlock();
1589
1590 return rate;
1591}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001592
Mike Turquetteb24764902012-03-15 23:11:19 -07001593/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001594 * clk_get_rate - return the rate of clk
1595 * @clk: the clk whose rate is being returned
1596 *
1597 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1598 * is set, which means a recalc_rate will be issued.
1599 * If clk is NULL then returns 0.
1600 */
1601unsigned long clk_get_rate(struct clk *clk)
1602{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001603 if (!clk)
1604 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001605
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001606 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001607}
1608EXPORT_SYMBOL_GPL(clk_get_rate);
1609
Stephen Boydd6968fc2015-04-30 13:54:13 -07001610static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001611 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001612{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001613 int i;
James Hogan4935b222013-07-29 12:24:59 +01001614
Masahiro Yamada508f8842015-12-28 19:23:08 +09001615 if (!parent)
1616 return -EINVAL;
1617
Derek Basehoreede77852018-12-20 16:31:00 -08001618 for (i = 0; i < core->num_parents; i++) {
Stephen Boyd1a079562019-04-30 10:22:30 -07001619 /* Found it first try! */
Stephen Boydfc0c2092019-04-12 11:31:47 -07001620 if (core->parents[i].core == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001621 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001622
Stephen Boyd1a079562019-04-30 10:22:30 -07001623 /* Something else is here, so keep looking */
Stephen Boydfc0c2092019-04-12 11:31:47 -07001624 if (core->parents[i].core)
Derek Basehoreede77852018-12-20 16:31:00 -08001625 continue;
1626
Stephen Boyd1a079562019-04-30 10:22:30 -07001627 /* Maybe core hasn't been cached but the hw is all we know? */
1628 if (core->parents[i].hw) {
1629 if (core->parents[i].hw == parent->hw)
1630 break;
1631
1632 /* Didn't match, but we're expecting a clk_hw */
1633 continue;
Derek Basehoreede77852018-12-20 16:31:00 -08001634 }
Stephen Boyd1a079562019-04-30 10:22:30 -07001635
1636 /* Maybe it hasn't been cached (clk_set_parent() path) */
1637 if (parent == clk_core_get(core, i))
1638 break;
1639
1640 /* Fallback to comparing globally unique names */
1641 if (!strcmp(parent->name, core->parents[i].name))
1642 break;
Derek Basehoreede77852018-12-20 16:31:00 -08001643 }
1644
Stephen Boyd1a079562019-04-30 10:22:30 -07001645 if (i == core->num_parents)
1646 return -EINVAL;
1647
1648 core->parents[i].core = parent;
1649 return i;
James Hogan4935b222013-07-29 12:24:59 +01001650}
1651
Heiko Stuebnere6500342015-04-22 22:53:05 +02001652/*
1653 * Update the orphan status of @core and all its children.
1654 */
1655static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1656{
1657 struct clk_core *child;
1658
1659 core->orphan = is_orphan;
1660
1661 hlist_for_each_entry(child, &core->children, child_node)
1662 clk_core_update_orphan_status(child, is_orphan);
1663}
1664
Stephen Boydd6968fc2015-04-30 13:54:13 -07001665static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001666{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001667 bool was_orphan = core->orphan;
1668
Stephen Boydd6968fc2015-04-30 13:54:13 -07001669 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001670
James Hogan903efc52013-08-29 12:10:51 +01001671 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001672 bool becomes_orphan = new_parent->orphan;
1673
James Hogan903efc52013-08-29 12:10:51 +01001674 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001675 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001676 new_parent->new_child = NULL;
1677
Stephen Boydd6968fc2015-04-30 13:54:13 -07001678 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001679
1680 if (was_orphan != becomes_orphan)
1681 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001682 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001683 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001684 if (!was_orphan)
1685 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001686 }
James Hogan4935b222013-07-29 12:24:59 +01001687
Stephen Boydd6968fc2015-04-30 13:54:13 -07001688 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001689}
1690
Stephen Boydd6968fc2015-04-30 13:54:13 -07001691static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001692 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001693{
1694 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001695 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001696
1697 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001698 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1699 *
1700 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001701 * clk_enable().
1702 *
1703 * If the clock is not prepared, then a race with
1704 * clk_enable/disable() is impossible since we already have the
1705 * prepare lock (future calls to clk_enable() need to be preceded by
1706 * a clk_prepare()).
1707 *
1708 * If the clock is prepared, migrate the prepared state to the new
1709 * parent and also protect against a race with clk_enable() by
1710 * forcing the clock and the new parent on. This ensures that all
1711 * future calls to clk_enable() are practically NOPs with respect to
1712 * hardware and software states.
1713 *
1714 * See also: Comment for clk_set_parent() below.
1715 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001716
1717 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1718 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1719 clk_core_prepare_enable(old_parent);
1720 clk_core_prepare_enable(parent);
1721 }
1722
1723 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001724 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001725 clk_core_prepare_enable(parent);
1726 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001727 }
1728
1729 /* update the clk tree topology */
1730 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001731 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001732 clk_enable_unlock(flags);
1733
Stephen Boyd3fa22522014-01-15 10:47:22 -08001734 return old_parent;
1735}
1736
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001737static void __clk_set_parent_after(struct clk_core *core,
1738 struct clk_core *parent,
1739 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001740{
1741 /*
1742 * Finish the migration of prepare state and undo the changes done
1743 * for preventing a race with clk_enable().
1744 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001745 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001746 clk_core_disable_lock(core);
1747 clk_core_disable_unprepare(old_parent);
1748 }
1749
1750 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1751 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1752 clk_core_disable_unprepare(parent);
1753 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001754 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001755}
1756
Stephen Boydd6968fc2015-04-30 13:54:13 -07001757static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001758 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001759{
1760 unsigned long flags;
1761 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001762 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001763
Stephen Boydd6968fc2015-04-30 13:54:13 -07001764 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001765
Stephen Boydd6968fc2015-04-30 13:54:13 -07001766 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001767
James Hogan4935b222013-07-29 12:24:59 +01001768 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001769 if (parent && core->ops->set_parent)
1770 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001771
Stephen Boydd6968fc2015-04-30 13:54:13 -07001772 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001773
James Hogan4935b222013-07-29 12:24:59 +01001774 if (ret) {
1775 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001776 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001777 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001778 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001779
James Hogan4935b222013-07-29 12:24:59 +01001780 return ret;
1781 }
1782
Stephen Boydd6968fc2015-04-30 13:54:13 -07001783 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001784
James Hogan4935b222013-07-29 12:24:59 +01001785 return 0;
1786}
1787
Ulf Hanssona093bde2012-08-31 14:21:28 +02001788/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001789 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001790 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001791 * @parent_rate: the "future" rate of clk's parent
1792 *
1793 * Walks the subtree of clks starting with clk, speculating rates as it
1794 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1795 *
1796 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1797 * pre-rate change notifications and returns early if no clks in the
1798 * subtree have subscribed to the notifications. Note that if a clk does not
1799 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001800 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001801 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001802static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001803 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001804{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001805 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001806 unsigned long new_rate;
1807 int ret = NOTIFY_DONE;
1808
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001809 lockdep_assert_held(&prepare_lock);
1810
Stephen Boydd6968fc2015-04-30 13:54:13 -07001811 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001812
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001813 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001814 if (core->notifier_count)
1815 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001816
Mike Turquette86bcfa22014-02-24 16:08:41 -08001817 if (ret & NOTIFY_STOP_MASK) {
1818 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001819 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001820 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001821 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001822
Stephen Boydd6968fc2015-04-30 13:54:13 -07001823 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001824 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001825 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001826 break;
1827 }
1828
1829out:
1830 return ret;
1831}
1832
Stephen Boydd6968fc2015-04-30 13:54:13 -07001833static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001834 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001835{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001836 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001837
Stephen Boydd6968fc2015-04-30 13:54:13 -07001838 core->new_rate = new_rate;
1839 core->new_parent = new_parent;
1840 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001841 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001842 core->new_child = NULL;
1843 if (new_parent && new_parent != core->parent)
1844 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001845
Stephen Boydd6968fc2015-04-30 13:54:13 -07001846 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001847 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001848 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001849 }
1850}
1851
1852/*
1853 * calculate the new rates returning the topmost clock that has to be
1854 * changed.
1855 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001856static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001857 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001858{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001859 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001860 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001861 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001862 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001863 unsigned long min_rate;
1864 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001865 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001866 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001867
Mike Turquette7452b212012-03-26 14:45:36 -07001868 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001869 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001870 return NULL;
1871
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001872 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001873 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001874 if (parent)
1875 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001876
Stephen Boydd6968fc2015-04-30 13:54:13 -07001877 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001878
James Hogan71472c02013-07-29 12:25:00 +01001879 /* find the closest rate and parent clk/rate */
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001880 if (clk_core_can_round(core)) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001881 struct clk_rate_request req;
1882
1883 req.rate = rate;
1884 req.min_rate = min_rate;
1885 req.max_rate = max_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001886
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001887 clk_core_init_rate_req(core, &req);
1888
1889 ret = clk_core_determine_round_nolock(core, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001890 if (ret < 0)
1891 return NULL;
1892
Boris Brezillon0817b622015-07-07 20:48:08 +02001893 best_parent_rate = req.best_parent_rate;
1894 new_rate = req.rate;
1895 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001896
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001897 if (new_rate < min_rate || new_rate > max_rate)
1898 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001899 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001900 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001901 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001902 return NULL;
1903 } else {
1904 /* pass-through clock with adjustable parent */
1905 top = clk_calc_new_rates(parent, rate);
1906 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001907 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001908 }
1909
James Hogan71472c02013-07-29 12:25:00 +01001910 /* some clocks must be gated to change parent */
1911 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001912 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001913 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001914 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001915 return NULL;
1916 }
1917
James Hogan71472c02013-07-29 12:25:00 +01001918 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001919 if (parent && core->num_parents > 1) {
1920 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001921 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001922 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001923 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001924 return NULL;
1925 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001926 }
1927
Stephen Boydd6968fc2015-04-30 13:54:13 -07001928 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001929 best_parent_rate != parent->rate)
1930 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001931
1932out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001933 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001934
1935 return top;
1936}
1937
1938/*
1939 * Notify about rate changes in a subtree. Always walk down the whole tree
1940 * so that in case of an error we can walk down the whole tree again and
1941 * abort the change.
1942 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001943static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001944 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001945{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001946 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001947 int ret = NOTIFY_DONE;
1948
Stephen Boydd6968fc2015-04-30 13:54:13 -07001949 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301950 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001951
Stephen Boydd6968fc2015-04-30 13:54:13 -07001952 if (core->notifier_count) {
1953 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001954 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001955 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001956 }
1957
Stephen Boydd6968fc2015-04-30 13:54:13 -07001958 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001959 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001960 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001961 continue;
1962 tmp_clk = clk_propagate_rate_change(child, event);
1963 if (tmp_clk)
1964 fail_clk = tmp_clk;
1965 }
1966
Stephen Boydd6968fc2015-04-30 13:54:13 -07001967 /* handle the new child who might not be in core->children yet */
1968 if (core->new_child) {
1969 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001970 if (tmp_clk)
1971 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001972 }
1973
1974 return fail_clk;
1975}
1976
1977/*
1978 * walk down a subtree and set the new rates notifying the rate
1979 * change on the way
1980 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001981static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001982{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001983 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001984 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001985 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001986 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001987 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001988 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001989 struct clk_core *parent = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001990
Stephen Boydd6968fc2015-04-30 13:54:13 -07001991 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001992
Dong Aishengfc8726a2016-06-30 17:31:14 +08001993 if (core->new_parent) {
1994 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001995 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001996 } else if (core->parent) {
1997 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001998 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001999 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01002000
Marek Szyprowski588fb542017-11-30 13:14:51 +01002001 if (clk_pm_runtime_get(core))
2002 return;
2003
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01002004 if (core->flags & CLK_SET_RATE_UNGATE) {
2005 unsigned long flags;
2006
2007 clk_core_prepare(core);
2008 flags = clk_enable_lock();
2009 clk_core_enable(core);
2010 clk_enable_unlock(flags);
2011 }
2012
Stephen Boydd6968fc2015-04-30 13:54:13 -07002013 if (core->new_parent && core->new_parent != core->parent) {
2014 old_parent = __clk_set_parent_before(core, core->new_parent);
2015 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002016
Stephen Boydd6968fc2015-04-30 13:54:13 -07002017 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002018 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002019 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08002020 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07002021 core->new_parent_index);
2022 } else if (core->ops->set_parent) {
2023 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002024 }
2025
Stephen Boydd6968fc2015-04-30 13:54:13 -07002026 trace_clk_set_parent_complete(core, core->new_parent);
2027 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002028 }
2029
Dong Aishengfc8726a2016-06-30 17:31:14 +08002030 if (core->flags & CLK_OPS_PARENT_ENABLE)
2031 clk_core_prepare_enable(parent);
2032
Stephen Boydd6968fc2015-04-30 13:54:13 -07002033 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002034
Stephen Boydd6968fc2015-04-30 13:54:13 -07002035 if (!skip_set_rate && core->ops->set_rate)
2036 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002037
Stephen Boydd6968fc2015-04-30 13:54:13 -07002038 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002039
Stephen Boydd6968fc2015-04-30 13:54:13 -07002040 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002041
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01002042 if (core->flags & CLK_SET_RATE_UNGATE) {
2043 unsigned long flags;
2044
2045 flags = clk_enable_lock();
2046 clk_core_disable(core);
2047 clk_enable_unlock(flags);
2048 clk_core_unprepare(core);
2049 }
2050
Dong Aishengfc8726a2016-06-30 17:31:14 +08002051 if (core->flags & CLK_OPS_PARENT_ENABLE)
2052 clk_core_disable_unprepare(parent);
2053
Stephen Boydd6968fc2015-04-30 13:54:13 -07002054 if (core->notifier_count && old_rate != core->rate)
2055 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002056
Michael Turquette85e88fa2015-06-20 12:18:03 -07002057 if (core->flags & CLK_RECALC_NEW_RATES)
2058 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02002059
Tero Kristo067bb172014-08-21 16:47:45 +03002060 /*
2061 * Use safe iteration, as change_rate can actually swap parents
2062 * for certain clock types.
2063 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002064 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01002065 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002066 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01002067 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07002068 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01002069 }
2070
Stephen Boydd6968fc2015-04-30 13:54:13 -07002071 /* handle the new child who might not be in core->children yet */
2072 if (core->new_child)
2073 clk_change_rate(core->new_child);
Marek Szyprowski588fb542017-11-30 13:14:51 +01002074
2075 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002076}
2077
Jerome Brunetca5e0892017-12-01 22:51:55 +01002078static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
2079 unsigned long req_rate)
2080{
Jerome Brunete55a8392017-12-01 22:51:56 +01002081 int ret, cnt;
Jerome Brunetca5e0892017-12-01 22:51:55 +01002082 struct clk_rate_request req;
2083
2084 lockdep_assert_held(&prepare_lock);
2085
2086 if (!core)
2087 return 0;
2088
Jerome Brunete55a8392017-12-01 22:51:56 +01002089 /* simulate what the rate would be if it could be freely set */
2090 cnt = clk_core_rate_nuke_protect(core);
2091 if (cnt < 0)
2092 return cnt;
2093
Jerome Brunetca5e0892017-12-01 22:51:55 +01002094 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
2095 req.rate = req_rate;
2096
2097 ret = clk_core_round_rate_nolock(core, &req);
2098
Jerome Brunete55a8392017-12-01 22:51:56 +01002099 /* restore the protection */
2100 clk_core_rate_restore_protect(core, cnt);
2101
Jerome Brunetca5e0892017-12-01 22:51:55 +01002102 return ret ? 0 : req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002103}
2104
Stephen Boydd6968fc2015-04-30 13:54:13 -07002105static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002106 unsigned long req_rate)
2107{
2108 struct clk_core *top, *fail_clk;
Jerome Brunetca5e0892017-12-01 22:51:55 +01002109 unsigned long rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002110 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002111
Stephen Boydd6968fc2015-04-30 13:54:13 -07002112 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002113 return 0;
2114
Jerome Brunetca5e0892017-12-01 22:51:55 +01002115 rate = clk_core_req_round_rate_nolock(core, req_rate);
2116
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002117 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002118 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002119 return 0;
2120
Jerome Brunete55a8392017-12-01 22:51:56 +01002121 /* fail on a direct rate set of a protected provider */
2122 if (clk_core_rate_is_protected(core))
2123 return -EBUSY;
2124
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002125 /* calculate new rates and get the topmost changed clock */
Jerome Brunetca5e0892017-12-01 22:51:55 +01002126 top = clk_calc_new_rates(core, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002127 if (!top)
2128 return -EINVAL;
2129
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002130 ret = clk_pm_runtime_get(core);
2131 if (ret)
2132 return ret;
2133
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002134 /* notify that we are about to change rates */
2135 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
2136 if (fail_clk) {
2137 pr_debug("%s: failed to set %s rate\n", __func__,
2138 fail_clk->name);
2139 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002140 ret = -EBUSY;
2141 goto err;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002142 }
2143
2144 /* change the rates */
2145 clk_change_rate(top);
2146
Stephen Boydd6968fc2015-04-30 13:54:13 -07002147 core->req_rate = req_rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002148err:
2149 clk_pm_runtime_put(core);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002150
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002151 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002152}
2153
Mike Turquetteb24764902012-03-15 23:11:19 -07002154/**
2155 * clk_set_rate - specify a new rate for clk
2156 * @clk: the clk whose rate is being changed
2157 * @rate: the new rate for clk
2158 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002159 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07002160 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002161 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2162 * propagate up to clk's parent; whether or not this happens depends on the
2163 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2164 * after calling .round_rate then upstream parent propagation is ignored. If
2165 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002166 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07002167 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2168 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07002169 *
Mike Turquette5654dc92012-03-26 11:51:34 -07002170 * Rate changes are accomplished via tree traversal that also recalculates the
2171 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07002172 *
2173 * Returns 0 on success, -EERROR otherwise.
2174 */
2175int clk_set_rate(struct clk *clk, unsigned long rate)
2176{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002177 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002178
Mike Turquette89ac8d72013-08-21 23:58:09 -07002179 if (!clk)
2180 return 0;
2181
Mike Turquetteb24764902012-03-15 23:11:19 -07002182 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002183 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002184
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002185 if (clk->exclusive_count)
2186 clk_core_rate_unprotect(clk->core);
2187
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002188 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002189
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002190 if (clk->exclusive_count)
2191 clk_core_rate_protect(clk->core);
2192
Mike Turquetteeab89f62013-03-28 13:59:01 -07002193 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002194
2195 return ret;
2196}
2197EXPORT_SYMBOL_GPL(clk_set_rate);
2198
2199/**
Geert Uytterhoeven65e22182019-06-17 15:56:02 +02002200 * clk_set_rate_exclusive - specify a new rate and get exclusive control
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002201 * @clk: the clk whose rate is being changed
2202 * @rate: the new rate for clk
2203 *
2204 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2205 * within a critical section
2206 *
2207 * This can be used initially to ensure that at least 1 consumer is
Geert Uytterhoeven65e22182019-06-17 15:56:02 +02002208 * satisfied when several consumers are competing for exclusivity over the
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002209 * same clock provider.
2210 *
2211 * The exclusivity is not applied if setting the rate failed.
2212 *
2213 * Calls to clk_rate_exclusive_get() should be balanced with calls to
2214 * clk_rate_exclusive_put().
2215 *
2216 * Returns 0 on success, -EERROR otherwise.
2217 */
2218int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2219{
2220 int ret;
2221
2222 if (!clk)
2223 return 0;
2224
2225 /* prevent racing with updates to the clock topology */
2226 clk_prepare_lock();
2227
2228 /*
2229 * The temporary protection removal is not here, on purpose
2230 * This function is meant to be used instead of clk_rate_protect,
2231 * so before the consumer code path protect the clock provider
2232 */
2233
2234 ret = clk_core_set_rate_nolock(clk->core, rate);
2235 if (!ret) {
2236 clk_core_rate_protect(clk->core);
2237 clk->exclusive_count++;
2238 }
2239
2240 clk_prepare_unlock();
2241
2242 return ret;
2243}
2244EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2245
2246/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002247 * clk_set_rate_range - set a rate range for a clock source
2248 * @clk: clock source
2249 * @min: desired minimum clock rate in Hz, inclusive
2250 * @max: desired maximum clock rate in Hz, inclusive
2251 *
2252 * Returns success (0) or negative errno.
2253 */
2254int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2255{
2256 int ret = 0;
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002257 unsigned long old_min, old_max, rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002258
2259 if (!clk)
2260 return 0;
2261
2262 if (min > max) {
2263 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2264 __func__, clk->core->name, clk->dev_id, clk->con_id,
2265 min, max);
2266 return -EINVAL;
2267 }
2268
2269 clk_prepare_lock();
2270
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002271 if (clk->exclusive_count)
2272 clk_core_rate_unprotect(clk->core);
2273
Jerome Brunet6562fbc2017-12-01 22:52:00 +01002274 /* Save the current values in case we need to rollback the change */
2275 old_min = clk->min_rate;
2276 old_max = clk->max_rate;
2277 clk->min_rate = min;
2278 clk->max_rate = max;
2279
2280 rate = clk_core_get_rate_nolock(clk->core);
2281 if (rate < min || rate > max) {
2282 /*
2283 * FIXME:
2284 * We are in bit of trouble here, current rate is outside the
2285 * the requested range. We are going try to request appropriate
2286 * range boundary but there is a catch. It may fail for the
2287 * usual reason (clock broken, clock protected, etc) but also
2288 * because:
2289 * - round_rate() was not favorable and fell on the wrong
2290 * side of the boundary
2291 * - the determine_rate() callback does not really check for
2292 * this corner case when determining the rate
2293 */
2294
2295 if (rate < min)
2296 rate = min;
2297 else
2298 rate = max;
2299
2300 ret = clk_core_set_rate_nolock(clk->core, rate);
2301 if (ret) {
2302 /* rollback the changes */
2303 clk->min_rate = old_min;
2304 clk->max_rate = old_max;
2305 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002306 }
2307
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002308 if (clk->exclusive_count)
2309 clk_core_rate_protect(clk->core);
2310
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002311 clk_prepare_unlock();
2312
2313 return ret;
2314}
2315EXPORT_SYMBOL_GPL(clk_set_rate_range);
2316
2317/**
2318 * clk_set_min_rate - set a minimum clock rate for a clock source
2319 * @clk: clock source
2320 * @rate: desired minimum clock rate in Hz, inclusive
2321 *
2322 * Returns success (0) or negative errno.
2323 */
2324int clk_set_min_rate(struct clk *clk, unsigned long rate)
2325{
2326 if (!clk)
2327 return 0;
2328
2329 return clk_set_rate_range(clk, rate, clk->max_rate);
2330}
2331EXPORT_SYMBOL_GPL(clk_set_min_rate);
2332
2333/**
2334 * clk_set_max_rate - set a maximum clock rate for a clock source
2335 * @clk: clock source
2336 * @rate: desired maximum clock rate in Hz, inclusive
2337 *
2338 * Returns success (0) or negative errno.
2339 */
2340int clk_set_max_rate(struct clk *clk, unsigned long rate)
2341{
2342 if (!clk)
2343 return 0;
2344
2345 return clk_set_rate_range(clk, clk->min_rate, rate);
2346}
2347EXPORT_SYMBOL_GPL(clk_set_max_rate);
2348
2349/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002350 * clk_get_parent - return the parent of a clk
2351 * @clk: the clk whose parent gets returned
2352 *
2353 * Simply returns clk->parent. Returns NULL if clk is NULL.
2354 */
2355struct clk *clk_get_parent(struct clk *clk)
2356{
2357 struct clk *parent;
2358
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002359 if (!clk)
2360 return NULL;
2361
Mike Turquetteeab89f62013-03-28 13:59:01 -07002362 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002363 /* TODO: Create a per-user clk and change callers to call clk_put */
2364 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002365 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002366
2367 return parent;
2368}
2369EXPORT_SYMBOL_GPL(clk_get_parent);
2370
Stephen Boydd6968fc2015-04-30 13:54:13 -07002371static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002372{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002373 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002374
Masahiro Yamada2430a942016-02-09 20:19:14 +09002375 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002376 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002377
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002378 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002379}
2380
Stephen Boydd6968fc2015-04-30 13:54:13 -07002381static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002382 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002383{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002384 clk_reparent(core, new_parent);
2385 __clk_recalc_accuracies(core);
2386 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002387}
2388
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002389void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2390{
2391 if (!hw)
2392 return;
2393
2394 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2395}
2396
Mike Turquetteb24764902012-03-15 23:11:19 -07002397/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002398 * clk_has_parent - check if a clock is a possible parent for another
2399 * @clk: clock source
2400 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002401 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002402 * This function can be used in drivers that need to check that a clock can be
2403 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002404 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002405 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002406 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002407bool clk_has_parent(struct clk *clk, struct clk *parent)
2408{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002409 struct clk_core *core, *parent_core;
Stephen Boydfc0c2092019-04-12 11:31:47 -07002410 int i;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002411
2412 /* NULL clocks should be nops, so return success if either is NULL. */
2413 if (!clk || !parent)
2414 return true;
2415
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002416 core = clk->core;
2417 parent_core = parent->core;
2418
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002419 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002420 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002421 return true;
2422
Stephen Boydfc0c2092019-04-12 11:31:47 -07002423 for (i = 0; i < core->num_parents; i++)
2424 if (!strcmp(core->parents[i].name, parent_core->name))
2425 return true;
2426
2427 return false;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002428}
2429EXPORT_SYMBOL_GPL(clk_has_parent);
2430
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002431static int clk_core_set_parent_nolock(struct clk_core *core,
2432 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002433{
2434 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002435 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002436 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002437
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002438 lockdep_assert_held(&prepare_lock);
2439
Stephen Boydd6968fc2015-04-30 13:54:13 -07002440 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002441 return 0;
2442
Stephen Boydd6968fc2015-04-30 13:54:13 -07002443 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002444 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002445
Stephen Boydb61c43c2015-02-02 14:11:25 -08002446 /* verify ops for for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002447 if (core->num_parents > 1 && !core->ops->set_parent)
2448 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08002449
Ulf Hansson031dcc92013-04-02 23:09:38 +02002450 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002451 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2452 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002453
Jerome Brunete55a8392017-12-01 22:51:56 +01002454 if (clk_core_rate_is_protected(core))
2455 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002456
2457 /* try finding the new parent index */
2458 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002459 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002460 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002461 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002462 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002463 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002464 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002465 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002466 }
2467
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002468 ret = clk_pm_runtime_get(core);
2469 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002470 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002471
Mike Turquetteb24764902012-03-15 23:11:19 -07002472 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002473 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002474
2475 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002476 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002477 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07002478
Ulf Hansson031dcc92013-04-02 23:09:38 +02002479 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002480 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002481
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002482 /* propagate rate an accuracy recalculation accordingly */
2483 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002484 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002485 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002486 __clk_recalc_rates(core, POST_RATE_CHANGE);
2487 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002488 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002489
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002490runtime_put:
2491 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002492
2493 return ret;
2494}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002495
2496/**
2497 * clk_set_parent - switch the parent of a mux clk
2498 * @clk: the mux clk whose input we are switching
2499 * @parent: the new input to clk
2500 *
2501 * Re-parent clk to use parent as its new input source. If clk is in
2502 * prepared state, the clk will get enabled for the duration of this call. If
2503 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2504 * that, the reparenting is glitchy in hardware, etc), use the
2505 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2506 *
2507 * After successfully changing clk's parent clk_set_parent will update the
2508 * clk topology, sysfs topology and propagate rate recalculation via
2509 * __clk_recalc_rates.
2510 *
2511 * Returns 0 on success, -EERROR otherwise.
2512 */
2513int clk_set_parent(struct clk *clk, struct clk *parent)
2514{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002515 int ret;
2516
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002517 if (!clk)
2518 return 0;
2519
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002520 clk_prepare_lock();
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002521
2522 if (clk->exclusive_count)
2523 clk_core_rate_unprotect(clk->core);
2524
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002525 ret = clk_core_set_parent_nolock(clk->core,
2526 parent ? parent->core : NULL);
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002527
2528 if (clk->exclusive_count)
2529 clk_core_rate_protect(clk->core);
2530
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002531 clk_prepare_unlock();
2532
2533 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002534}
Mike Turquetteb24764902012-03-15 23:11:19 -07002535EXPORT_SYMBOL_GPL(clk_set_parent);
2536
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002537static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2538{
2539 int ret = -EINVAL;
2540
2541 lockdep_assert_held(&prepare_lock);
2542
2543 if (!core)
2544 return 0;
2545
Jerome Brunete55a8392017-12-01 22:51:56 +01002546 if (clk_core_rate_is_protected(core))
2547 return -EBUSY;
2548
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002549 trace_clk_set_phase(core, degrees);
2550
Shawn Lin7f95bee2018-03-08 14:49:41 +08002551 if (core->ops->set_phase) {
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002552 ret = core->ops->set_phase(core->hw, degrees);
Shawn Lin7f95bee2018-03-08 14:49:41 +08002553 if (!ret)
2554 core->phase = degrees;
2555 }
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002556
2557 trace_clk_set_phase_complete(core, degrees);
2558
2559 return ret;
2560}
2561
Mike Turquetteb24764902012-03-15 23:11:19 -07002562/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002563 * clk_set_phase - adjust the phase shift of a clock signal
2564 * @clk: clock signal source
2565 * @degrees: number of degrees the signal is shifted
2566 *
2567 * Shifts the phase of a clock signal by the specified
2568 * degrees. Returns 0 on success, -EERROR otherwise.
2569 *
2570 * This function makes no distinction about the input or reference
2571 * signal that we adjust the clock signal phase against. For example
2572 * phase locked-loop clock signal generators we may shift phase with
2573 * respect to feedback clock signal input, but for other cases the
2574 * clock phase may be shifted with respect to some other, unspecified
2575 * signal.
2576 *
2577 * Additionally the concept of phase shift does not propagate through
2578 * the clock tree hierarchy, which sets it apart from clock rates and
2579 * clock accuracy. A parent clock phase attribute does not have an
2580 * impact on the phase attribute of a child clock.
2581 */
2582int clk_set_phase(struct clk *clk, int degrees)
2583{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002584 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002585
2586 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002587 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002588
2589 /* sanity check degrees */
2590 degrees %= 360;
2591 if (degrees < 0)
2592 degrees += 360;
2593
2594 clk_prepare_lock();
2595
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002596 if (clk->exclusive_count)
2597 clk_core_rate_unprotect(clk->core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08002598
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002599 ret = clk_core_set_phase_nolock(clk->core, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002600
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01002601 if (clk->exclusive_count)
2602 clk_core_rate_protect(clk->core);
Mike Turquettee59c5372014-02-18 21:21:25 -08002603
Mike Turquettee59c5372014-02-18 21:21:25 -08002604 clk_prepare_unlock();
2605
Mike Turquettee59c5372014-02-18 21:21:25 -08002606 return ret;
2607}
Maxime Ripard9767b04f2015-01-20 22:23:43 +01002608EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002609
Stephen Boydd6968fc2015-04-30 13:54:13 -07002610static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002611{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002612 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002613
2614 clk_prepare_lock();
Shawn Lin1f9c63e2018-03-14 08:28:31 +08002615 /* Always try to update cached phase if possible */
2616 if (core->ops->get_phase)
2617 core->phase = core->ops->get_phase(core->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002618 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002619 clk_prepare_unlock();
2620
Mike Turquettee59c5372014-02-18 21:21:25 -08002621 return ret;
2622}
2623
2624/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002625 * clk_get_phase - return the phase shift of a clock signal
2626 * @clk: clock signal source
2627 *
2628 * Returns the phase shift of a clock node in degrees, otherwise returns
2629 * -EERROR.
2630 */
2631int clk_get_phase(struct clk *clk)
2632{
2633 if (!clk)
2634 return 0;
2635
2636 return clk_core_get_phase(clk->core);
2637}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002638EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002639
Jerome Brunet9fba7382018-06-19 16:41:41 +02002640static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2641{
2642 /* Assume a default value of 50% */
2643 core->duty.num = 1;
2644 core->duty.den = 2;
2645}
2646
2647static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2648
2649static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2650{
2651 struct clk_duty *duty = &core->duty;
2652 int ret = 0;
2653
2654 if (!core->ops->get_duty_cycle)
2655 return clk_core_update_duty_cycle_parent_nolock(core);
2656
2657 ret = core->ops->get_duty_cycle(core->hw, duty);
2658 if (ret)
2659 goto reset;
2660
2661 /* Don't trust the clock provider too much */
2662 if (duty->den == 0 || duty->num > duty->den) {
2663 ret = -EINVAL;
2664 goto reset;
2665 }
2666
2667 return 0;
2668
2669reset:
2670 clk_core_reset_duty_cycle_nolock(core);
2671 return ret;
2672}
2673
2674static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2675{
2676 int ret = 0;
2677
2678 if (core->parent &&
2679 core->flags & CLK_DUTY_CYCLE_PARENT) {
2680 ret = clk_core_update_duty_cycle_nolock(core->parent);
2681 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2682 } else {
2683 clk_core_reset_duty_cycle_nolock(core);
2684 }
2685
2686 return ret;
2687}
2688
2689static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2690 struct clk_duty *duty);
2691
2692static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2693 struct clk_duty *duty)
2694{
2695 int ret;
2696
2697 lockdep_assert_held(&prepare_lock);
2698
2699 if (clk_core_rate_is_protected(core))
2700 return -EBUSY;
2701
2702 trace_clk_set_duty_cycle(core, duty);
2703
2704 if (!core->ops->set_duty_cycle)
2705 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2706
2707 ret = core->ops->set_duty_cycle(core->hw, duty);
2708 if (!ret)
2709 memcpy(&core->duty, duty, sizeof(*duty));
2710
2711 trace_clk_set_duty_cycle_complete(core, duty);
2712
2713 return ret;
2714}
2715
2716static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2717 struct clk_duty *duty)
2718{
2719 int ret = 0;
2720
2721 if (core->parent &&
2722 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2723 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2724 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2725 }
2726
2727 return ret;
2728}
2729
2730/**
2731 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2732 * @clk: clock signal source
2733 * @num: numerator of the duty cycle ratio to be applied
2734 * @den: denominator of the duty cycle ratio to be applied
2735 *
2736 * Apply the duty cycle ratio if the ratio is valid and the clock can
2737 * perform this operation
2738 *
2739 * Returns (0) on success, a negative errno otherwise.
2740 */
2741int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2742{
2743 int ret;
2744 struct clk_duty duty;
2745
2746 if (!clk)
2747 return 0;
2748
2749 /* sanity check the ratio */
2750 if (den == 0 || num > den)
2751 return -EINVAL;
2752
2753 duty.num = num;
2754 duty.den = den;
2755
2756 clk_prepare_lock();
2757
2758 if (clk->exclusive_count)
2759 clk_core_rate_unprotect(clk->core);
2760
2761 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2762
2763 if (clk->exclusive_count)
2764 clk_core_rate_protect(clk->core);
2765
2766 clk_prepare_unlock();
2767
2768 return ret;
2769}
2770EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2771
2772static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2773 unsigned int scale)
2774{
2775 struct clk_duty *duty = &core->duty;
2776 int ret;
2777
2778 clk_prepare_lock();
2779
2780 ret = clk_core_update_duty_cycle_nolock(core);
2781 if (!ret)
2782 ret = mult_frac(scale, duty->num, duty->den);
2783
2784 clk_prepare_unlock();
2785
2786 return ret;
2787}
2788
2789/**
2790 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2791 * @clk: clock signal source
2792 * @scale: scaling factor to be applied to represent the ratio as an integer
2793 *
2794 * Returns the duty cycle ratio of a clock node multiplied by the provided
2795 * scaling factor, or negative errno on error.
2796 */
2797int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2798{
2799 if (!clk)
2800 return 0;
2801
2802 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2803}
2804EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2805
Mike Turquetteb24764902012-03-15 23:11:19 -07002806/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002807 * clk_is_match - check if two clk's point to the same hardware clock
2808 * @p: clk compared against q
2809 * @q: clk compared against p
2810 *
2811 * Returns true if the two struct clk pointers both point to the same hardware
2812 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2813 * share the same struct clk_core object.
2814 *
2815 * Returns false otherwise. Note that two NULL clks are treated as matching.
2816 */
2817bool clk_is_match(const struct clk *p, const struct clk *q)
2818{
2819 /* trivial case: identical struct clk's or both NULL */
2820 if (p == q)
2821 return true;
2822
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002823 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002824 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2825 if (p->core == q->core)
2826 return true;
2827
2828 return false;
2829}
2830EXPORT_SYMBOL_GPL(clk_is_match);
2831
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002832/*** debugfs support ***/
2833
2834#ifdef CONFIG_DEBUG_FS
2835#include <linux/debugfs.h>
2836
2837static struct dentry *rootdir;
2838static int inited = 0;
2839static DEFINE_MUTEX(clk_debug_lock);
2840static HLIST_HEAD(clk_debug_list);
2841
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002842static 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{
2899 if (!c)
2900 return;
2901
Stefan Wahren7cb81132015-04-29 16:36:43 +00002902 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002903 seq_printf(s, "\"%s\": { ", c->name);
2904 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2905 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Jerome Brunete55a8392017-12-01 22:51:56 +01002906 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002907 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2908 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Lubomir Rintelc6e90992019-01-04 23:05:49 +01002909 seq_printf(s, "\"phase\": %d,", clk_core_get_phase(c));
Jerome Brunet9fba7382018-06-19 16:41:41 +02002910 seq_printf(s, "\"duty_cycle\": %u",
2911 clk_core_get_scaled_duty_cycle(c, 100000));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002912}
2913
2914static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2915{
2916 struct clk_core *child;
2917
2918 if (!c)
2919 return;
2920
2921 clk_dump_one(s, c, level);
2922
2923 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002924 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002925 clk_dump_subtree(s, child, level + 1);
2926 }
2927
Markus Elfring4d327582017-04-20 08:45:43 +02002928 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002929}
2930
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002931static int clk_dump_show(struct seq_file *s, void *data)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002932{
2933 struct clk_core *c;
2934 bool first_node = true;
2935 struct hlist_head **lists = (struct hlist_head **)s->private;
2936
Markus Elfring4d327582017-04-20 08:45:43 +02002937 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002938 clk_prepare_lock();
2939
2940 for (; *lists; lists++) {
2941 hlist_for_each_entry(c, *lists, child_node) {
2942 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02002943 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002944 first_node = false;
2945 clk_dump_subtree(s, c, 0);
2946 }
2947 }
2948
2949 clk_prepare_unlock();
2950
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002951 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002952 return 0;
2953}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002954DEFINE_SHOW_ATTRIBUTE(clk_dump);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002955
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002956static const struct {
2957 unsigned long flag;
2958 const char *name;
2959} clk_flags[] = {
Geert Uytterhoeven40dd71c2018-07-06 17:16:54 +02002960#define ENTRY(f) { f, #f }
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002961 ENTRY(CLK_SET_RATE_GATE),
2962 ENTRY(CLK_SET_PARENT_GATE),
2963 ENTRY(CLK_SET_RATE_PARENT),
2964 ENTRY(CLK_IGNORE_UNUSED),
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002965 ENTRY(CLK_GET_RATE_NOCACHE),
2966 ENTRY(CLK_SET_RATE_NO_REPARENT),
2967 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2968 ENTRY(CLK_RECALC_NEW_RATES),
2969 ENTRY(CLK_SET_RATE_UNGATE),
2970 ENTRY(CLK_IS_CRITICAL),
2971 ENTRY(CLK_OPS_PARENT_ENABLE),
Jerome Brunet9fba7382018-06-19 16:41:41 +02002972 ENTRY(CLK_DUTY_CYCLE_PARENT),
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002973#undef ENTRY
2974};
2975
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002976static int clk_flags_show(struct seq_file *s, void *data)
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002977{
2978 struct clk_core *core = s->private;
2979 unsigned long flags = core->flags;
2980 unsigned int i;
2981
2982 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2983 if (flags & clk_flags[i].flag) {
2984 seq_printf(s, "%s\n", clk_flags[i].name);
2985 flags &= ~clk_flags[i].flag;
2986 }
2987 }
2988 if (flags) {
2989 /* Unknown flags */
2990 seq_printf(s, "0x%lx\n", flags);
2991 }
2992
2993 return 0;
2994}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02002995DEFINE_SHOW_ATTRIBUTE(clk_flags);
Geert Uytterhoevena6059ab2018-01-03 12:06:16 +01002996
Stephen Boyd11f6c232019-06-24 20:01:55 -07002997static void possible_parent_show(struct seq_file *s, struct clk_core *core,
2998 unsigned int i, char terminator)
Peter De Schrijver92031572017-03-21 15:20:31 +02002999{
Chen-Yu Tsai2d156b72019-05-03 11:15:09 +08003000 struct clk_core *parent;
Peter De Schrijver92031572017-03-21 15:20:31 +02003001
Chen-Yu Tsai2d156b72019-05-03 11:15:09 +08003002 /*
3003 * Go through the following options to fetch a parent's name.
3004 *
3005 * 1. Fetch the registered parent clock and use its name
3006 * 2. Use the global (fallback) name if specified
3007 * 3. Use the local fw_name if provided
3008 * 4. Fetch parent clock's clock-output-name if DT index was set
3009 *
3010 * This may still fail in some cases, such as when the parent is
3011 * specified directly via a struct clk_hw pointer, but it isn't
3012 * registered (yet).
3013 */
Chen-Yu Tsai2d156b72019-05-03 11:15:09 +08003014 parent = clk_core_get_parent_by_index(core, i);
3015 if (parent)
3016 seq_printf(s, "%s", parent->name);
3017 else if (core->parents[i].name)
3018 seq_printf(s, "%s", core->parents[i].name);
3019 else if (core->parents[i].fw_name)
3020 seq_printf(s, "<%s>(fw)", core->parents[i].fw_name);
3021 else if (core->parents[i].index >= 0)
3022 seq_printf(s, "%s",
3023 of_clk_get_parent_name(core->of_node,
3024 core->parents[i].index));
3025 else
3026 seq_puts(s, "(missing)");
Peter De Schrijver92031572017-03-21 15:20:31 +02003027
Stephen Boyd11f6c232019-06-24 20:01:55 -07003028 seq_putc(s, terminator);
3029}
3030
Peter De Schrijver92031572017-03-21 15:20:31 +02003031static int possible_parents_show(struct seq_file *s, void *data)
3032{
3033 struct clk_core *core = s->private;
3034 int i;
3035
3036 for (i = 0; i < core->num_parents - 1; i++)
Stephen Boyd11f6c232019-06-24 20:01:55 -07003037 possible_parent_show(s, core, i, ' ');
Peter De Schrijver92031572017-03-21 15:20:31 +02003038
Stephen Boyd11f6c232019-06-24 20:01:55 -07003039 possible_parent_show(s, core, i, '\n');
Peter De Schrijver92031572017-03-21 15:20:31 +02003040
3041 return 0;
3042}
Andy Shevchenkofec0ef32018-02-14 17:48:00 +02003043DEFINE_SHOW_ATTRIBUTE(possible_parents);
Peter De Schrijver92031572017-03-21 15:20:31 +02003044
Leonard Cresteze5e89242019-06-10 14:06:38 +03003045static int current_parent_show(struct seq_file *s, void *data)
3046{
3047 struct clk_core *core = s->private;
3048
3049 if (core->parent)
3050 seq_printf(s, "%s\n", core->parent->name);
3051
3052 return 0;
3053}
3054DEFINE_SHOW_ATTRIBUTE(current_parent);
3055
Jerome Brunet9fba7382018-06-19 16:41:41 +02003056static int clk_duty_cycle_show(struct seq_file *s, void *data)
3057{
3058 struct clk_core *core = s->private;
3059 struct clk_duty *duty = &core->duty;
3060
3061 seq_printf(s, "%u/%u\n", duty->num, duty->den);
3062
3063 return 0;
3064}
3065DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
3066
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003067static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003068{
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003069 struct dentry *root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003070
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003071 if (!core || !pdentry)
3072 return;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003073
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003074 root = debugfs_create_dir(core->name, pdentry);
3075 core->dentry = root;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003076
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003077 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
3078 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
3079 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
3080 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
3081 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
3082 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
3083 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
3084 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
Jerome Brunet9fba7382018-06-19 16:41:41 +02003085 debugfs_create_file("clk_duty_cycle", 0444, root, core,
3086 &clk_duty_cycle_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003087
Leonard Cresteze5e89242019-06-10 14:06:38 +03003088 if (core->num_parents > 0)
3089 debugfs_create_file("clk_parent", 0444, root, core,
3090 &current_parent_fops);
3091
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003092 if (core->num_parents > 1)
3093 debugfs_create_file("clk_possible_parents", 0444, root, core,
3094 &possible_parents_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003095
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003096 if (core->ops->debug_init)
3097 core->ops->debug_init(core->hw, core->dentry);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003098}
3099
3100/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003101 * clk_debug_register - add a clk node to the debugfs clk directory
3102 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003103 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003104 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3105 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003106 * will be created lazily by clk_debug_init as part of a late_initcall.
3107 */
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003108static void clk_debug_register(struct clk_core *core)
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003109{
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003110 mutex_lock(&clk_debug_lock);
3111 hlist_add_head(&core->debug_node, &clk_debug_list);
Stephen Boyddb3188fa2018-01-03 16:44:37 -08003112 if (inited)
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003113 clk_debug_create_one(core, rootdir);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003114 mutex_unlock(&clk_debug_lock);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003115}
3116
3117 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003118 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3119 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003120 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003121 * Dynamically removes a clk and all its child nodes from the
3122 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08003123 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003124 */
3125static void clk_debug_unregister(struct clk_core *core)
3126{
3127 mutex_lock(&clk_debug_lock);
3128 hlist_del_init(&core->debug_node);
3129 debugfs_remove_recursive(core->dentry);
3130 core->dentry = NULL;
3131 mutex_unlock(&clk_debug_lock);
3132}
3133
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003134/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003135 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003136 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003137 * clks are often initialized very early during boot before memory can be
3138 * dynamically allocated and well before debugfs is setup. This function
3139 * populates the debugfs clk directory once at boot-time when we know that
3140 * debugfs is setup. It should only be called once at boot-time, all other clks
3141 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003142 */
3143static int __init clk_debug_init(void)
3144{
3145 struct clk_core *core;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003146
3147 rootdir = debugfs_create_dir("clk", NULL);
3148
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003149 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
3150 &clk_summary_fops);
3151 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
3152 &clk_dump_fops);
3153 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
3154 &clk_summary_fops);
3155 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
3156 &clk_dump_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003157
3158 mutex_lock(&clk_debug_lock);
3159 hlist_for_each_entry(core, &clk_debug_list, debug_node)
3160 clk_debug_create_one(core, rootdir);
3161
3162 inited = 1;
3163 mutex_unlock(&clk_debug_lock);
3164
3165 return 0;
3166}
3167late_initcall(clk_debug_init);
3168#else
Greg Kroah-Hartman8a26bbb2018-05-29 18:08:00 +02003169static inline void clk_debug_register(struct clk_core *core) { }
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003170static inline void clk_debug_reparent(struct clk_core *core,
3171 struct clk_core *new_parent)
3172{
3173}
3174static inline void clk_debug_unregister(struct clk_core *core)
3175{
3176}
3177#endif
3178
Michael Turquette3d3801e2015-02-25 09:11:01 -08003179/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003180 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003181 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07003182 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003183 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07003184 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07003185 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003186static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07003187{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003188 int ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003189 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003190 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003191 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003192
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003193 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07003194 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07003195
Mike Turquetteeab89f62013-03-28 13:59:01 -07003196 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003197
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003198 ret = clk_pm_runtime_get(core);
3199 if (ret)
3200 goto unlock;
3201
Mike Turquetteb24764902012-03-15 23:11:19 -07003202 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003203 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003204 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003205 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003206 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07003207 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07003208 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003209
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03003210 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003211 if (core->ops->set_rate &&
3212 !((core->ops->round_rate || core->ops->determine_rate) &&
3213 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003214 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3215 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003216 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003217 goto out;
3218 }
3219
Stephen Boydd6968fc2015-04-30 13:54:13 -07003220 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003221 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3222 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003223 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003224 goto out;
3225 }
3226
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09003227 if (core->num_parents > 1 && !core->ops->get_parent) {
3228 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3229 __func__, core->name);
3230 ret = -EINVAL;
3231 goto out;
3232 }
3233
Stephen Boydd6968fc2015-04-30 13:54:13 -07003234 if (core->ops->set_rate_and_parent &&
3235 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003236 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003237 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08003238 ret = -EINVAL;
3239 goto out;
3240 }
3241
Stephen Boydd6968fc2015-04-30 13:54:13 -07003242 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07003243
3244 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08003245 * Populate core->parent if parent has already been clk_core_init'd. If
3246 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003247 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07003248 * clk list.
3249 *
3250 * Every time a new clk is clk_init'd then we walk the list of orphan
3251 * clocks and re-parent any that are children of the clock currently
3252 * being clk_init'd.
3253 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02003254 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003255 hlist_add_head(&core->child_node,
3256 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003257 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003258 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003259 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003260 core->orphan = false;
3261 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003262 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003263 core->orphan = true;
3264 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003265
3266 /*
Jerome Brunet541deba2018-02-14 14:43:37 +01003267 * optional platform-specific magic
3268 *
3269 * The .init callback is not used by any of the basic clock types, but
3270 * exists for weird hardware that must perform initialization magic.
3271 * Please consider other ways of solving initialization problems before
3272 * using this callback, as its use is discouraged.
3273 */
3274 if (core->ops->init)
3275 core->ops->init(core->hw);
3276
3277 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003278 * Set clk's accuracy. The preferred method is to use
3279 * .recalc_accuracy. For simple clocks and lazy developers the default
3280 * fallback is to use the parent's accuracy. If a clock doesn't have a
3281 * parent (or is orphaned) then accuracy is set to zero (perfect
3282 * clock).
3283 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003284 if (core->ops->recalc_accuracy)
3285 core->accuracy = core->ops->recalc_accuracy(core->hw,
3286 __clk_get_accuracy(core->parent));
3287 else if (core->parent)
3288 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003289 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003290 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003291
3292 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02003293 * Set clk's phase.
3294 * Since a phase is by definition relative to its parent, just
3295 * query the current clock phase, or just assume it's in phase.
3296 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003297 if (core->ops->get_phase)
3298 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02003299 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003300 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02003301
3302 /*
Jerome Brunet9fba7382018-06-19 16:41:41 +02003303 * Set clk's duty cycle.
3304 */
3305 clk_core_update_duty_cycle_nolock(core);
3306
3307 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07003308 * Set clk's rate. The preferred method is to use .recalc_rate. For
3309 * simple clocks and lazy developers the default fallback is to use the
3310 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3311 * then rate is set to zero.
3312 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003313 if (core->ops->recalc_rate)
3314 rate = core->ops->recalc_rate(core->hw,
3315 clk_core_get_rate_nolock(core->parent));
3316 else if (core->parent)
3317 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003318 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003319 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003320 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003321
3322 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003323 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3324 * don't get accidentally disabled when walking the orphan tree and
3325 * reparenting clocks
3326 */
3327 if (core->flags & CLK_IS_CRITICAL) {
3328 unsigned long flags;
3329
3330 clk_core_prepare(core);
3331
3332 flags = clk_enable_lock();
3333 clk_core_enable(core);
3334 clk_enable_unlock(flags);
3335 }
3336
3337 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003338 * walk the list of orphan clocks and reparent any that newly finds a
3339 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07003340 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08003341 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003342 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01003343
Michael Turquette904e6ea2016-07-08 16:32:10 -07003344 /*
Jerome Brunet99652a42018-02-14 14:43:36 +01003345 * We need to use __clk_set_parent_before() and _after() to
3346 * to properly migrate any prepare/enable count of the orphan
3347 * clock. This is important for CLK_IS_CRITICAL clocks, which
3348 * are enabled during init but might not have a parent yet.
Michael Turquette904e6ea2016-07-08 16:32:10 -07003349 */
3350 if (parent) {
Stephen Boydf8f8f1d2017-11-02 00:36:09 -07003351 /* update the clk tree topology */
Jerome Brunet99652a42018-02-14 14:43:36 +01003352 __clk_set_parent_before(orphan, parent);
3353 __clk_set_parent_after(orphan, parent, NULL);
Michael Turquette904e6ea2016-07-08 16:32:10 -07003354 __clk_recalc_accuracies(orphan);
3355 __clk_recalc_rates(orphan, 0);
3356 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003357 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003358
Stephen Boydd6968fc2015-04-30 13:54:13 -07003359 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07003360out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003361 clk_pm_runtime_put(core);
3362unlock:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003363 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003364
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003365 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003366 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003367
Mike Turquetted1302a32012-03-29 14:30:40 -07003368 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07003369}
3370
Stephen Boyd1df40462018-12-11 08:32:04 -08003371/**
3372 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3373 * @core: clk to add consumer to
3374 * @clk: consumer to link to a clk
3375 */
3376static void clk_core_link_consumer(struct clk_core *core, struct clk *clk)
3377{
3378 clk_prepare_lock();
3379 hlist_add_head(&clk->clks_node, &core->clks);
3380 clk_prepare_unlock();
3381}
3382
3383/**
3384 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3385 * @clk: consumer to unlink
3386 */
3387static void clk_core_unlink_consumer(struct clk *clk)
3388{
3389 lockdep_assert_held(&prepare_lock);
3390 hlist_del(&clk->clks_node);
3391}
3392
3393/**
3394 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
3395 * @core: clk to allocate a consumer for
3396 * @dev_id: string describing device name
3397 * @con_id: connection ID string on device
3398 *
3399 * Returns: clk consumer left unlinked from the consumer list
3400 */
3401static struct clk *alloc_clk(struct clk_core *core, const char *dev_id,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003402 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003403{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003404 struct clk *clk;
3405
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003406 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3407 if (!clk)
3408 return ERR_PTR(-ENOMEM);
3409
Stephen Boyd1df40462018-12-11 08:32:04 -08003410 clk->core = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003411 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02003412 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003413 clk->max_rate = ULONG_MAX;
3414
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003415 return clk;
3416}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003417
Stephen Boyd1df40462018-12-11 08:32:04 -08003418/**
3419 * free_clk - Free a clk consumer
3420 * @clk: clk consumer to free
3421 *
3422 * Note, this assumes the clk has been unlinked from the clk_core consumer
3423 * list.
3424 */
3425static void free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003426{
Leonard Crestez253160a2017-02-20 15:20:56 +02003427 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003428 kfree(clk);
3429}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003430
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003431/**
Stephen Boyd1df40462018-12-11 08:32:04 -08003432 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
3433 * a clk_hw
Stephen Boydefa85042018-12-11 08:34:16 -08003434 * @dev: clk consumer device
Stephen Boyd1df40462018-12-11 08:32:04 -08003435 * @hw: clk_hw associated with the clk being consumed
3436 * @dev_id: string describing device name
3437 * @con_id: connection ID string on device
3438 *
3439 * This is the main function used to create a clk pointer for use by clk
3440 * consumers. It connects a consumer to the clk_core and clk_hw structures
3441 * used by the framework and clk provider respectively.
3442 */
Stephen Boydefa85042018-12-11 08:34:16 -08003443struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
Stephen Boyd1df40462018-12-11 08:32:04 -08003444 const char *dev_id, const char *con_id)
3445{
3446 struct clk *clk;
3447 struct clk_core *core;
3448
3449 /* This is to allow this function to be chained to others */
3450 if (IS_ERR_OR_NULL(hw))
3451 return ERR_CAST(hw);
3452
3453 core = hw->core;
3454 clk = alloc_clk(core, dev_id, con_id);
3455 if (IS_ERR(clk))
3456 return clk;
Stephen Boydefa85042018-12-11 08:34:16 -08003457 clk->dev = dev;
Stephen Boyd1df40462018-12-11 08:32:04 -08003458
3459 if (!try_module_get(core->owner)) {
3460 free_clk(clk);
3461 return ERR_PTR(-ENOENT);
3462 }
3463
3464 kref_get(&core->ref);
3465 clk_core_link_consumer(core, clk);
3466
3467 return clk;
3468}
3469
Stephen Boydfc0c2092019-04-12 11:31:47 -07003470static int clk_cpy_name(const char **dst_p, const char *src, bool must_exist)
Mike Turquetteb24764902012-03-15 23:11:19 -07003471{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003472 const char *dst;
3473
3474 if (!src) {
3475 if (must_exist)
3476 return -EINVAL;
3477 return 0;
3478 }
3479
3480 *dst_p = dst = kstrdup_const(src, GFP_KERNEL);
3481 if (!dst)
3482 return -ENOMEM;
3483
3484 return 0;
3485}
3486
3487static int clk_core_populate_parent_map(struct clk_core *core)
3488{
3489 const struct clk_init_data *init = core->hw->init;
3490 u8 num_parents = init->num_parents;
3491 const char * const *parent_names = init->parent_names;
3492 const struct clk_hw **parent_hws = init->parent_hws;
3493 const struct clk_parent_data *parent_data = init->parent_data;
3494 int i, ret = 0;
3495 struct clk_parent_map *parents, *parent;
3496
3497 if (!num_parents)
3498 return 0;
3499
3500 /*
3501 * Avoid unnecessary string look-ups of clk_core's possible parents by
3502 * having a cache of names/clk_hw pointers to clk_core pointers.
3503 */
3504 parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
3505 core->parents = parents;
3506 if (!parents)
3507 return -ENOMEM;
3508
3509 /* Copy everything over because it might be __initdata */
3510 for (i = 0, parent = parents; i < num_parents; i++, parent++) {
Stephen Boyd601b6e92019-04-12 11:31:49 -07003511 parent->index = -1;
Stephen Boydfc0c2092019-04-12 11:31:47 -07003512 if (parent_names) {
3513 /* throw a WARN if any entries are NULL */
3514 WARN(!parent_names[i],
3515 "%s: invalid NULL in %s's .parent_names\n",
3516 __func__, core->name);
3517 ret = clk_cpy_name(&parent->name, parent_names[i],
3518 true);
3519 } else if (parent_data) {
3520 parent->hw = parent_data[i].hw;
Stephen Boyd601b6e92019-04-12 11:31:49 -07003521 parent->index = parent_data[i].index;
Stephen Boydfc0c2092019-04-12 11:31:47 -07003522 ret = clk_cpy_name(&parent->fw_name,
3523 parent_data[i].fw_name, false);
3524 if (!ret)
3525 ret = clk_cpy_name(&parent->name,
3526 parent_data[i].name,
3527 false);
3528 } else if (parent_hws) {
3529 parent->hw = parent_hws[i];
3530 } else {
3531 ret = -EINVAL;
3532 WARN(1, "Must specify parents if num_parents > 0\n");
3533 }
3534
3535 if (ret) {
3536 do {
3537 kfree_const(parents[i].name);
3538 kfree_const(parents[i].fw_name);
3539 } while (--i >= 0);
3540 kfree(parents);
3541
3542 return ret;
3543 }
3544 }
3545
3546 return 0;
3547}
3548
3549static void clk_core_free_parent_map(struct clk_core *core)
3550{
3551 int i = core->num_parents;
3552
3553 if (!core->num_parents)
3554 return;
3555
3556 while (--i >= 0) {
3557 kfree_const(core->parents[i].name);
3558 kfree_const(core->parents[i].fw_name);
3559 }
3560
3561 kfree(core->parents);
3562}
3563
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003564static struct clk *
3565__clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07003566{
Stephen Boydfc0c2092019-04-12 11:31:47 -07003567 int ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003568 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003569
Stephen Boydd6968fc2015-04-30 13:54:13 -07003570 core = kzalloc(sizeof(*core), GFP_KERNEL);
3571 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003572 ret = -ENOMEM;
3573 goto fail_out;
3574 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003575
Stephen Boydd6968fc2015-04-30 13:54:13 -07003576 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3577 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003578 ret = -ENOMEM;
3579 goto fail_name;
3580 }
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003581
3582 if (WARN_ON(!hw->init->ops)) {
3583 ret = -EINVAL;
3584 goto fail_ops;
3585 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07003586 core->ops = hw->init->ops;
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003587
Marek Szyprowski9a34b452017-08-21 10:04:59 +02003588 if (dev && pm_runtime_enabled(dev))
Miquel Raynal24478832018-12-04 20:24:37 +01003589 core->rpm_enabled = true;
3590 core->dev = dev;
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003591 core->of_node = np;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003592 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003593 core->owner = dev->driver->owner;
3594 core->hw = hw;
3595 core->flags = hw->init->flags;
3596 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003597 core->min_rate = 0;
3598 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003599 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003600
Stephen Boydfc0c2092019-04-12 11:31:47 -07003601 ret = clk_core_populate_parent_map(core);
3602 if (ret)
Masahiro Yamada176d1162015-12-28 19:23:00 +09003603 goto fail_parents;
Masahiro Yamada176d1162015-12-28 19:23:00 +09003604
Stephen Boydd6968fc2015-04-30 13:54:13 -07003605 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003606
Stephen Boyd1df40462018-12-11 08:32:04 -08003607 /*
3608 * Don't call clk_hw_create_clk() here because that would pin the
3609 * provider module to itself and prevent it from ever being removed.
3610 */
3611 hw->clk = alloc_clk(core, NULL, NULL);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003612 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003613 ret = PTR_ERR(hw->clk);
Stephen Boydfc0c2092019-04-12 11:31:47 -07003614 goto fail_create_clk;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003615 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003616
Stephen Boyd1df40462018-12-11 08:32:04 -08003617 clk_core_link_consumer(hw->core, hw->clk);
3618
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003619 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003620 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003621 return hw->clk;
3622
Stephen Boyd1df40462018-12-11 08:32:04 -08003623 clk_prepare_lock();
3624 clk_core_unlink_consumer(hw->clk);
3625 clk_prepare_unlock();
3626
3627 free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003628 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003629
Stephen Boydfc0c2092019-04-12 11:31:47 -07003630fail_create_clk:
3631 clk_core_free_parent_map(core);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003632fail_parents:
Jerome Brunet29fd2a32017-12-19 09:33:29 +01003633fail_ops:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003634 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003635fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003636 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003637fail_out:
3638 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003639}
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003640
3641/**
3642 * clk_register - allocate a new clock, register it and return an opaque cookie
3643 * @dev: device that is registering this clock
3644 * @hw: link to hardware-specific clock data
3645 *
Stephen Boydc1157f62019-05-07 11:46:13 -07003646 * clk_register is the *deprecated* interface for populating the clock tree with
3647 * new clock nodes. Use clk_hw_register() instead.
3648 *
3649 * Returns: a pointer to the newly allocated struct clk which
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003650 * cannot be dereferenced by driver code but may be used in conjunction with the
3651 * rest of the clock API. In the event of an error clk_register will return an
3652 * error code; drivers must test for an error code after calling clk_register.
3653 */
3654struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3655{
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003656 return __clk_register(dev, dev_of_node(dev), hw);
Stephen Boydfceaa7d2019-04-12 11:31:44 -07003657}
Mike Turquetteb24764902012-03-15 23:11:19 -07003658EXPORT_SYMBOL_GPL(clk_register);
3659
Stephen Boyd41438042016-02-05 17:02:52 -08003660/**
3661 * clk_hw_register - register a clk_hw and return an error code
3662 * @dev: device that is registering this clock
3663 * @hw: link to hardware-specific clock data
3664 *
3665 * clk_hw_register is the primary interface for populating the clock tree with
3666 * new clock nodes. It returns an integer equal to zero indicating success or
3667 * less than zero indicating failure. Drivers must test for an error code after
3668 * calling clk_hw_register().
3669 */
3670int clk_hw_register(struct device *dev, struct clk_hw *hw)
3671{
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003672 return PTR_ERR_OR_ZERO(__clk_register(dev, dev_of_node(dev), hw));
Stephen Boyd41438042016-02-05 17:02:52 -08003673}
3674EXPORT_SYMBOL_GPL(clk_hw_register);
3675
Stephen Boyd89a5ddcc2019-04-12 11:31:46 -07003676/*
3677 * of_clk_hw_register - register a clk_hw and return an error code
3678 * @node: device_node of device that is registering this clock
3679 * @hw: link to hardware-specific clock data
3680 *
3681 * of_clk_hw_register() is the primary interface for populating the clock tree
3682 * with new clock nodes when a struct device is not available, but a struct
3683 * device_node is. It returns an integer equal to zero indicating success or
3684 * less than zero indicating failure. Drivers must test for an error code after
3685 * calling of_clk_hw_register().
3686 */
3687int of_clk_hw_register(struct device_node *node, struct clk_hw *hw)
3688{
3689 return PTR_ERR_OR_ZERO(__clk_register(NULL, node, hw));
3690}
3691EXPORT_SYMBOL_GPL(of_clk_hw_register);
3692
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003693/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003694static void __clk_release(struct kref *ref)
3695{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003696 struct clk_core *core = container_of(ref, struct clk_core, ref);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003697
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003698 lockdep_assert_held(&prepare_lock);
3699
Stephen Boydfc0c2092019-04-12 11:31:47 -07003700 clk_core_free_parent_map(core);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003701 kfree_const(core->name);
3702 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003703}
3704
3705/*
3706 * Empty clk_ops for unregistered clocks. These are used temporarily
3707 * after clk_unregister() was called on a clock and until last clock
3708 * consumer calls clk_put() and the struct clk object is freed.
3709 */
3710static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3711{
3712 return -ENXIO;
3713}
3714
3715static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3716{
3717 WARN_ON_ONCE(1);
3718}
3719
3720static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3721 unsigned long parent_rate)
3722{
3723 return -ENXIO;
3724}
3725
3726static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3727{
3728 return -ENXIO;
3729}
3730
3731static const struct clk_ops clk_nodrv_ops = {
3732 .enable = clk_nodrv_prepare_enable,
3733 .disable = clk_nodrv_disable_unprepare,
3734 .prepare = clk_nodrv_prepare_enable,
3735 .unprepare = clk_nodrv_disable_unprepare,
3736 .set_rate = clk_nodrv_set_rate,
3737 .set_parent = clk_nodrv_set_parent,
3738};
3739
Stephen Boydbdcf1dc2019-08-28 11:19:59 -07003740static void clk_core_evict_parent_cache_subtree(struct clk_core *root,
3741 struct clk_core *target)
3742{
3743 int i;
3744 struct clk_core *child;
3745
3746 for (i = 0; i < root->num_parents; i++)
3747 if (root->parents[i].core == target)
3748 root->parents[i].core = NULL;
3749
3750 hlist_for_each_entry(child, &root->children, child_node)
3751 clk_core_evict_parent_cache_subtree(child, target);
3752}
3753
3754/* Remove this clk from all parent caches */
3755static void clk_core_evict_parent_cache(struct clk_core *core)
3756{
3757 struct hlist_head **lists;
3758 struct clk_core *root;
3759
3760 lockdep_assert_held(&prepare_lock);
3761
3762 for (lists = all_lists; *lists; lists++)
3763 hlist_for_each_entry(root, *lists, child_node)
3764 clk_core_evict_parent_cache_subtree(root, core);
3765
3766}
3767
Mark Brown1df5c932012-04-18 09:07:12 +01003768/**
3769 * clk_unregister - unregister a currently registered clock
3770 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003771 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003772void clk_unregister(struct clk *clk)
3773{
3774 unsigned long flags;
3775
Stephen Boyd6314b672014-09-04 23:37:49 -07003776 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3777 return;
3778
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003779 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003780
3781 clk_prepare_lock();
3782
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003783 if (clk->core->ops == &clk_nodrv_ops) {
3784 pr_err("%s: unregistered clock: %s\n", __func__,
3785 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003786 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003787 }
3788 /*
3789 * Assign empty clock ops for consumers that might still hold
3790 * a reference to this clock.
3791 */
3792 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003793 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003794 clk_enable_unlock(flags);
3795
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003796 if (!hlist_empty(&clk->core->children)) {
3797 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003798 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003799
3800 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003801 hlist_for_each_entry_safe(child, t, &clk->core->children,
3802 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01003803 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003804 }
3805
Stephen Boydbdcf1dc2019-08-28 11:19:59 -07003806 clk_core_evict_parent_cache(clk->core);
3807
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003808 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003809
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003810 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003811 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003812 __func__, clk->core->name);
Jerome Brunete55a8392017-12-01 22:51:56 +01003813
3814 if (clk->core->protect_count)
3815 pr_warn("%s: unregistering protected clock: %s\n",
3816 __func__, clk->core->name);
3817
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003818 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003819unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003820 clk_prepare_unlock();
3821}
Mark Brown1df5c932012-04-18 09:07:12 +01003822EXPORT_SYMBOL_GPL(clk_unregister);
3823
Stephen Boyd41438042016-02-05 17:02:52 -08003824/**
3825 * clk_hw_unregister - unregister a currently registered clk_hw
3826 * @hw: hardware-specific clock data to unregister
3827 */
3828void clk_hw_unregister(struct clk_hw *hw)
3829{
3830 clk_unregister(hw->clk);
3831}
3832EXPORT_SYMBOL_GPL(clk_hw_unregister);
3833
Stephen Boyd46c87732012-09-24 13:38:04 -07003834static void devm_clk_release(struct device *dev, void *res)
3835{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003836 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003837}
3838
Stephen Boyd41438042016-02-05 17:02:52 -08003839static void devm_clk_hw_release(struct device *dev, void *res)
3840{
3841 clk_hw_unregister(*(struct clk_hw **)res);
3842}
3843
Stephen Boyd46c87732012-09-24 13:38:04 -07003844/**
3845 * devm_clk_register - resource managed clk_register()
3846 * @dev: device that is registering this clock
3847 * @hw: link to hardware-specific clock data
3848 *
Stephen Boyd9fe9b7a2018-12-11 10:49:40 -08003849 * Managed clk_register(). This function is *deprecated*, use devm_clk_hw_register() instead.
3850 *
3851 * Clocks returned from this function are automatically clk_unregister()ed on
3852 * driver detach. See clk_register() for more information.
Stephen Boyd46c87732012-09-24 13:38:04 -07003853 */
3854struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3855{
3856 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003857 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003858
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003859 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3860 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003861 return ERR_PTR(-ENOMEM);
3862
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003863 clk = clk_register(dev, hw);
3864 if (!IS_ERR(clk)) {
3865 *clkp = clk;
3866 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003867 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003868 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003869 }
3870
3871 return clk;
3872}
3873EXPORT_SYMBOL_GPL(devm_clk_register);
3874
Stephen Boyd41438042016-02-05 17:02:52 -08003875/**
3876 * devm_clk_hw_register - resource managed clk_hw_register()
3877 * @dev: device that is registering this clock
3878 * @hw: link to hardware-specific clock data
3879 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003880 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003881 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3882 * for more information.
3883 */
3884int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3885{
3886 struct clk_hw **hwp;
3887 int ret;
3888
3889 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3890 if (!hwp)
3891 return -ENOMEM;
3892
3893 ret = clk_hw_register(dev, hw);
3894 if (!ret) {
3895 *hwp = hw;
3896 devres_add(dev, hwp);
3897 } else {
3898 devres_free(hwp);
3899 }
3900
3901 return ret;
3902}
3903EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3904
Stephen Boyd46c87732012-09-24 13:38:04 -07003905static int devm_clk_match(struct device *dev, void *res, void *data)
3906{
3907 struct clk *c = res;
3908 if (WARN_ON(!c))
3909 return 0;
3910 return c == data;
3911}
3912
Stephen Boyd41438042016-02-05 17:02:52 -08003913static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3914{
3915 struct clk_hw *hw = res;
3916
3917 if (WARN_ON(!hw))
3918 return 0;
3919 return hw == data;
3920}
3921
Stephen Boyd46c87732012-09-24 13:38:04 -07003922/**
3923 * devm_clk_unregister - resource managed clk_unregister()
3924 * @clk: clock to unregister
3925 *
3926 * Deallocate a clock allocated with devm_clk_register(). Normally
3927 * this function will not need to be called and the resource management
3928 * code will ensure that the resource is freed.
3929 */
3930void devm_clk_unregister(struct device *dev, struct clk *clk)
3931{
3932 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3933}
3934EXPORT_SYMBOL_GPL(devm_clk_unregister);
3935
Stephen Boyd41438042016-02-05 17:02:52 -08003936/**
3937 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3938 * @dev: device that is unregistering the hardware-specific clock data
3939 * @hw: link to hardware-specific clock data
3940 *
3941 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3942 * this function will not need to be called and the resource management
3943 * code will ensure that the resource is freed.
3944 */
3945void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3946{
3947 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3948 hw));
3949}
3950EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3951
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003952/*
3953 * clkdev helpers
3954 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003955
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003956void __clk_put(struct clk *clk)
3957{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003958 struct module *owner;
3959
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003960 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003961 return;
3962
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003963 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003964
Jerome Brunet55e9b8b2017-12-01 22:51:59 +01003965 /*
3966 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3967 * given user should be balanced with calls to clk_rate_exclusive_put()
3968 * and by that same consumer
3969 */
3970 if (WARN_ON(clk->exclusive_count)) {
3971 /* We voiced our concern, let's sanitize the situation */
3972 clk->core->protect_count -= (clk->exclusive_count - 1);
3973 clk_core_rate_unprotect(clk->core);
3974 clk->exclusive_count = 0;
3975 }
3976
Stephen Boyd50595f82015-02-06 11:42:44 -08003977 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003978 if (clk->min_rate > clk->core->req_rate ||
3979 clk->max_rate < clk->core->req_rate)
3980 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3981
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003982 owner = clk->core->owner;
3983 kref_put(&clk->core->ref, __clk_release);
3984
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003985 clk_prepare_unlock();
3986
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003987 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003988
Stephen Boyd1df40462018-12-11 08:32:04 -08003989 free_clk(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003990}
3991
Mike Turquetteb24764902012-03-15 23:11:19 -07003992/*** clk rate change notifiers ***/
3993
3994/**
3995 * clk_notifier_register - add a clk rate change notifier
3996 * @clk: struct clk * to watch
3997 * @nb: struct notifier_block * with callback info
3998 *
3999 * Request notification when clk's rate changes. This uses an SRCU
4000 * notifier because we want it to block and notifier unregistrations are
4001 * uncommon. The callbacks associated with the notifier must not
4002 * re-enter into the clk framework by calling any top-level clk APIs;
4003 * this will cause a nested prepare_lock mutex.
4004 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09004005 * In all notification cases (pre, post and abort rate change) the original
4006 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
4007 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07004008 *
Mike Turquetteb24764902012-03-15 23:11:19 -07004009 * clk_notifier_register() must be called from non-atomic context.
4010 * Returns -EINVAL if called with null arguments, -ENOMEM upon
4011 * allocation failure; otherwise, passes along the return value of
4012 * srcu_notifier_chain_register().
4013 */
4014int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
4015{
4016 struct clk_notifier *cn;
4017 int ret = -ENOMEM;
4018
4019 if (!clk || !nb)
4020 return -EINVAL;
4021
Mike Turquetteeab89f62013-03-28 13:59:01 -07004022 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004023
4024 /* search the list of notifiers for this clk */
4025 list_for_each_entry(cn, &clk_notifier_list, node)
4026 if (cn->clk == clk)
4027 break;
4028
4029 /* if clk wasn't in the notifier list, allocate new clk_notifier */
4030 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02004031 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07004032 if (!cn)
4033 goto out;
4034
4035 cn->clk = clk;
4036 srcu_init_notifier_head(&cn->notifier_head);
4037
4038 list_add(&cn->node, &clk_notifier_list);
4039 }
4040
4041 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
4042
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004043 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07004044
4045out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07004046 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004047
4048 return ret;
4049}
4050EXPORT_SYMBOL_GPL(clk_notifier_register);
4051
4052/**
4053 * clk_notifier_unregister - remove a clk rate change notifier
4054 * @clk: struct clk *
4055 * @nb: struct notifier_block * with callback info
4056 *
4057 * Request no further notification for changes to 'clk' and frees memory
4058 * allocated in clk_notifier_register.
4059 *
4060 * Returns -EINVAL if called with null arguments; otherwise, passes
4061 * along the return value of srcu_notifier_chain_unregister().
4062 */
4063int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
4064{
4065 struct clk_notifier *cn = NULL;
4066 int ret = -EINVAL;
4067
4068 if (!clk || !nb)
4069 return -EINVAL;
4070
Mike Turquetteeab89f62013-03-28 13:59:01 -07004071 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004072
4073 list_for_each_entry(cn, &clk_notifier_list, node)
4074 if (cn->clk == clk)
4075 break;
4076
4077 if (cn->clk == clk) {
4078 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
4079
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004080 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07004081
4082 /* XXX the notifier code should handle this better */
4083 if (!cn->notifier_head.head) {
4084 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08004085 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07004086 kfree(cn);
4087 }
4088
4089 } else {
4090 ret = -ENOENT;
4091 }
4092
Mike Turquetteeab89f62013-03-28 13:59:01 -07004093 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004094
4095 return ret;
4096}
4097EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05004098
4099#ifdef CONFIG_OF
4100/**
4101 * struct of_clk_provider - Clock provider registration structure
4102 * @link: Entry in global list of clock providers
4103 * @node: Pointer to device tree node of clock provider
4104 * @get: Get clock callback. Returns NULL or a struct clk for the
4105 * given clock specifier
4106 * @data: context pointer to be passed into @get callback
4107 */
4108struct of_clk_provider {
4109 struct list_head link;
4110
4111 struct device_node *node;
4112 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004113 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05004114 void *data;
4115};
4116
Stephen Boyd30d5a942019-05-23 17:11:57 -07004117extern struct of_device_id __clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304118static const struct of_device_id __clk_of_table_sentinel
4119 __used __section(__clk_of_table_end);
4120
Grant Likely766e6a42012-04-09 14:50:06 -05004121static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004122static DEFINE_MUTEX(of_clk_mutex);
4123
Grant Likely766e6a42012-04-09 14:50:06 -05004124struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
4125 void *data)
4126{
4127 return data;
4128}
4129EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
4130
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004131struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
4132{
4133 return data;
4134}
4135EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
4136
Shawn Guo494bfec2012-08-22 21:36:27 +08004137struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
4138{
4139 struct clk_onecell_data *clk_data = data;
4140 unsigned int idx = clkspec->args[0];
4141
4142 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02004143 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08004144 return ERR_PTR(-EINVAL);
4145 }
4146
4147 return clk_data->clks[idx];
4148}
4149EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
4150
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004151struct clk_hw *
4152of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
4153{
4154 struct clk_hw_onecell_data *hw_data = data;
4155 unsigned int idx = clkspec->args[0];
4156
4157 if (idx >= hw_data->num) {
4158 pr_err("%s: invalid index %u\n", __func__, idx);
4159 return ERR_PTR(-EINVAL);
4160 }
4161
4162 return hw_data->hws[idx];
4163}
4164EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
4165
Grant Likely766e6a42012-04-09 14:50:06 -05004166/**
4167 * of_clk_add_provider() - Register a clock provider for a node
4168 * @np: Device node pointer associated with clock provider
4169 * @clk_src_get: callback for decoding clock
4170 * @data: context pointer for @clk_src_get callback.
Stephen Boyd9fe9b7a2018-12-11 10:49:40 -08004171 *
4172 * This function is *deprecated*. Use of_clk_add_hw_provider() instead.
Grant Likely766e6a42012-04-09 14:50:06 -05004173 */
4174int of_clk_add_provider(struct device_node *np,
4175 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
4176 void *data),
4177 void *data)
4178{
4179 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004180 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004181
Markus Elfring1808a322017-04-20 09:30:52 +02004182 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05004183 if (!cp)
4184 return -ENOMEM;
4185
4186 cp->node = of_node_get(np);
4187 cp->data = data;
4188 cp->get = clk_src_get;
4189
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004190 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004191 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004192 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05004193 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05004194
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004195 ret = of_clk_set_defaults(np, true);
4196 if (ret < 0)
4197 of_clk_del_provider(np);
4198
4199 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004200}
4201EXPORT_SYMBOL_GPL(of_clk_add_provider);
4202
4203/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004204 * of_clk_add_hw_provider() - Register a clock provider for a node
4205 * @np: Device node pointer associated with clock provider
4206 * @get: callback for decoding clk_hw
4207 * @data: context pointer for @get callback.
4208 */
4209int of_clk_add_hw_provider(struct device_node *np,
4210 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4211 void *data),
4212 void *data)
4213{
4214 struct of_clk_provider *cp;
4215 int ret;
4216
4217 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4218 if (!cp)
4219 return -ENOMEM;
4220
4221 cp->node = of_node_get(np);
4222 cp->data = data;
4223 cp->get_hw = get;
4224
4225 mutex_lock(&of_clk_mutex);
4226 list_add(&cp->link, &of_clk_providers);
4227 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05004228 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004229
4230 ret = of_clk_set_defaults(np, true);
4231 if (ret < 0)
4232 of_clk_del_provider(np);
4233
4234 return ret;
4235}
4236EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
4237
Stephen Boydaa795c42017-09-01 16:16:40 -07004238static void devm_of_clk_release_provider(struct device *dev, void *res)
4239{
4240 of_clk_del_provider(*(struct device_node **)res);
4241}
4242
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004243/*
4244 * We allow a child device to use its parent device as the clock provider node
4245 * for cases like MFD sub-devices where the child device driver wants to use
4246 * devm_*() APIs but not list the device in DT as a sub-node.
4247 */
4248static struct device_node *get_clk_provider_node(struct device *dev)
4249{
4250 struct device_node *np, *parent_np;
4251
4252 np = dev->of_node;
4253 parent_np = dev->parent ? dev->parent->of_node : NULL;
4254
4255 if (!of_find_property(np, "#clock-cells", NULL))
4256 if (of_find_property(parent_np, "#clock-cells", NULL))
4257 np = parent_np;
4258
4259 return np;
4260}
4261
Matti Vaittinene45838b2018-12-04 13:33:48 +02004262/**
4263 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4264 * @dev: Device acting as the clock provider (used for DT node and lifetime)
4265 * @get: callback for decoding clk_hw
4266 * @data: context pointer for @get callback
4267 *
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004268 * Registers clock provider for given device's node. If the device has no DT
4269 * node or if the device node lacks of clock provider information (#clock-cells)
4270 * then the parent device's node is scanned for this information. If parent node
4271 * has the #clock-cells then it is used in registration. Provider is
4272 * automatically released at device exit.
Matti Vaittinene45838b2018-12-04 13:33:48 +02004273 *
4274 * Return: 0 on success or an errno on failure.
4275 */
Stephen Boydaa795c42017-09-01 16:16:40 -07004276int devm_of_clk_add_hw_provider(struct device *dev,
4277 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4278 void *data),
4279 void *data)
4280{
4281 struct device_node **ptr, *np;
4282 int ret;
4283
4284 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
4285 GFP_KERNEL);
4286 if (!ptr)
4287 return -ENOMEM;
4288
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004289 np = get_clk_provider_node(dev);
Stephen Boydaa795c42017-09-01 16:16:40 -07004290 ret = of_clk_add_hw_provider(np, get, data);
4291 if (!ret) {
4292 *ptr = np;
4293 devres_add(dev, ptr);
4294 } else {
4295 devres_free(ptr);
4296 }
4297
4298 return ret;
4299}
4300EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
4301
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004302/**
Grant Likely766e6a42012-04-09 14:50:06 -05004303 * of_clk_del_provider() - Remove a previously registered clock provider
4304 * @np: Device node pointer associated with clock provider
4305 */
4306void of_clk_del_provider(struct device_node *np)
4307{
4308 struct of_clk_provider *cp;
4309
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004310 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004311 list_for_each_entry(cp, &of_clk_providers, link) {
4312 if (cp->node == np) {
4313 list_del(&cp->link);
4314 of_node_put(cp->node);
4315 kfree(cp);
4316 break;
4317 }
4318 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004319 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004320}
4321EXPORT_SYMBOL_GPL(of_clk_del_provider);
4322
Stephen Boydaa795c42017-09-01 16:16:40 -07004323static int devm_clk_provider_match(struct device *dev, void *res, void *data)
4324{
4325 struct device_node **np = res;
4326
4327 if (WARN_ON(!np || !*np))
4328 return 0;
4329
4330 return *np == data;
4331}
4332
Matti Vaittinene45838b2018-12-04 13:33:48 +02004333/**
4334 * devm_of_clk_del_provider() - Remove clock provider registered using devm
4335 * @dev: Device to whose lifetime the clock provider was bound
4336 */
Stephen Boydaa795c42017-09-01 16:16:40 -07004337void devm_of_clk_del_provider(struct device *dev)
4338{
4339 int ret;
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004340 struct device_node *np = get_clk_provider_node(dev);
Stephen Boydaa795c42017-09-01 16:16:40 -07004341
4342 ret = devres_release(dev, devm_of_clk_release_provider,
Matti Vaittinen05502bf92018-12-04 13:34:53 +02004343 devm_clk_provider_match, np);
Stephen Boydaa795c42017-09-01 16:16:40 -07004344
4345 WARN_ON(ret);
4346}
4347EXPORT_SYMBOL(devm_of_clk_del_provider);
4348
Stephen Boyd5dc7e842019-03-08 10:35:01 -08004349/*
4350 * Beware the return values when np is valid, but no clock provider is found.
4351 * If name == NULL, the function returns -ENOENT.
4352 * If name != NULL, the function returns -EINVAL. This is because
4353 * of_parse_phandle_with_args() is called even if of_property_match_string()
4354 * returns an error.
4355 */
Stephen Boydcf13f282018-12-19 15:09:14 -08004356static int of_parse_clkspec(const struct device_node *np, int index,
4357 const char *name, struct of_phandle_args *out_args)
Stephen Boyd44722872018-12-19 10:59:55 -08004358{
4359 int ret = -ENOENT;
4360
4361 /* Walk up the tree of devices looking for a clock property that matches */
4362 while (np) {
4363 /*
4364 * For named clocks, first look up the name in the
4365 * "clock-names" property. If it cannot be found, then index
4366 * will be an error code and of_parse_phandle_with_args() will
4367 * return -EINVAL.
4368 */
4369 if (name)
4370 index = of_property_match_string(np, "clock-names", name);
4371 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
4372 index, out_args);
4373 if (!ret)
4374 break;
4375 if (name && index >= 0)
4376 break;
4377
4378 /*
4379 * No matching clock found on this node. If the parent node
4380 * has a "clock-ranges" property, then we can try one of its
4381 * clocks.
4382 */
4383 np = np->parent;
4384 if (np && !of_get_property(np, "clock-ranges", NULL))
4385 break;
4386 index = 0;
4387 }
4388
4389 return ret;
4390}
4391
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004392static struct clk_hw *
4393__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4394 struct of_phandle_args *clkspec)
4395{
4396 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004397
Stephen Boyd74002fc2016-08-25 13:35:36 -07004398 if (provider->get_hw)
4399 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004400
Stephen Boyd74002fc2016-08-25 13:35:36 -07004401 clk = provider->get(clkspec, provider->data);
4402 if (IS_ERR(clk))
4403 return ERR_CAST(clk);
4404 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004405}
4406
Stephen Boydcf13f282018-12-19 15:09:14 -08004407static struct clk_hw *
4408of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
Grant Likely766e6a42012-04-09 14:50:06 -05004409{
4410 struct of_clk_provider *provider;
Stephen Boyd1df40462018-12-11 08:32:04 -08004411 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05004412
Stephen Boyd306c3422015-02-05 15:39:11 -08004413 if (!clkspec)
4414 return ERR_PTR(-EINVAL);
4415
Stephen Boyd306c3422015-02-05 15:39:11 -08004416 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004417 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07004418 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004419 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd1df40462018-12-11 08:32:04 -08004420 if (!IS_ERR(hw))
4421 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08004422 }
Grant Likely766e6a42012-04-09 14:50:06 -05004423 }
Stephen Boyd306c3422015-02-05 15:39:11 -08004424 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004425
Stephen Boyd44722872018-12-19 10:59:55 -08004426 return hw;
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004427}
4428
Stephen Boyd306c3422015-02-05 15:39:11 -08004429/**
4430 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4431 * @clkspec: pointer to a clock specifier data structure
4432 *
4433 * This function looks up a struct clk from the registered list of clock
4434 * providers, an input is a clock specifier data structure as returned
4435 * from the of_parse_phandle_with_args() function call.
4436 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004437struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4438{
Stephen Boyd44722872018-12-19 10:59:55 -08004439 struct clk_hw *hw = of_clk_get_hw_from_clkspec(clkspec);
4440
Stephen Boydefa85042018-12-11 08:34:16 -08004441 return clk_hw_create_clk(NULL, hw, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05004442}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06004443EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05004444
Stephen Boydcf13f282018-12-19 15:09:14 -08004445struct clk_hw *of_clk_get_hw(struct device_node *np, int index,
4446 const char *con_id)
4447{
4448 int ret;
4449 struct clk_hw *hw;
4450 struct of_phandle_args clkspec;
4451
4452 ret = of_parse_clkspec(np, index, con_id, &clkspec);
4453 if (ret)
4454 return ERR_PTR(ret);
4455
4456 hw = of_clk_get_hw_from_clkspec(&clkspec);
4457 of_node_put(clkspec.np);
4458
4459 return hw;
4460}
4461
4462static struct clk *__of_clk_get(struct device_node *np,
4463 int index, const char *dev_id,
4464 const char *con_id)
4465{
4466 struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
4467
4468 return clk_hw_create_clk(NULL, hw, dev_id, con_id);
4469}
4470
4471struct clk *of_clk_get(struct device_node *np, int index)
4472{
4473 return __of_clk_get(np, index, np->full_name, NULL);
4474}
4475EXPORT_SYMBOL(of_clk_get);
4476
4477/**
4478 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
4479 * @np: pointer to clock consumer node
4480 * @name: name of consumer's clock input, or NULL for the first clock reference
4481 *
4482 * This function parses the clocks and clock-names properties,
4483 * and uses them to look up the struct clk from the registered list of clock
4484 * providers.
4485 */
4486struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
4487{
4488 if (!np)
4489 return ERR_PTR(-ENOENT);
4490
Kuninori Morimoto65cf20a2019-03-06 16:18:28 +09004491 return __of_clk_get(np, 0, np->full_name, name);
Stephen Boydcf13f282018-12-19 15:09:14 -08004492}
4493EXPORT_SYMBOL(of_clk_get_by_name);
4494
Stephen Boyd929e7f32016-02-19 15:52:32 -08004495/**
4496 * of_clk_get_parent_count() - Count the number of clocks a device node has
4497 * @np: device node to count
4498 *
4499 * Returns: The number of clocks that are possible parents of this node
4500 */
4501unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07004502{
Stephen Boyd929e7f32016-02-19 15:52:32 -08004503 int count;
4504
4505 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4506 if (count < 0)
4507 return 0;
4508
4509 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07004510}
4511EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4512
Grant Likely766e6a42012-04-09 14:50:06 -05004513const char *of_clk_get_parent_name(struct device_node *np, int index)
4514{
4515 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004516 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05004517 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004518 const __be32 *vp;
4519 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05004520 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004521 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004522 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05004523
Grant Likely766e6a42012-04-09 14:50:06 -05004524 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4525 &clkspec);
4526 if (rc)
4527 return NULL;
4528
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004529 index = clkspec.args_count ? clkspec.args[0] : 0;
4530 count = 0;
4531
4532 /* if there is an indices property, use it to transfer the index
4533 * specified into an array offset for the clock-output-names property.
4534 */
4535 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4536 if (index == pv) {
4537 index = count;
4538 break;
4539 }
4540 count++;
4541 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09004542 /* We went off the end of 'clock-indices' without finding it */
4543 if (prop && !vp)
4544 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004545
Grant Likely766e6a42012-04-09 14:50:06 -05004546 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004547 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004548 &clk_name) < 0) {
4549 /*
4550 * Best effort to get the name if the clock has been
4551 * registered with the framework. If the clock isn't
4552 * registered, we return the node name as the name of
4553 * the clock as long as #clock-cells = 0.
4554 */
4555 clk = of_clk_get_from_provider(&clkspec);
4556 if (IS_ERR(clk)) {
4557 if (clkspec.args_count == 0)
4558 clk_name = clkspec.np->name;
4559 else
4560 clk_name = NULL;
4561 } else {
4562 clk_name = __clk_get_name(clk);
4563 clk_put(clk);
4564 }
4565 }
4566
Grant Likely766e6a42012-04-09 14:50:06 -05004567
4568 of_node_put(clkspec.np);
4569 return clk_name;
4570}
4571EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4572
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05004573/**
4574 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4575 * number of parents
4576 * @np: Device node pointer associated with clock provider
4577 * @parents: pointer to char array that hold the parents' names
4578 * @size: size of the @parents array
4579 *
4580 * Return: number of parents for the clock node.
4581 */
4582int of_clk_parent_fill(struct device_node *np, const char **parents,
4583 unsigned int size)
4584{
4585 unsigned int i = 0;
4586
4587 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4588 i++;
4589
4590 return i;
4591}
4592EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4593
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004594struct clock_provider {
Geert Uytterhoevena59704332018-04-10 15:06:05 +02004595 void (*clk_init_cb)(struct device_node *);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004596 struct device_node *np;
4597 struct list_head node;
4598};
4599
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004600/*
4601 * This function looks for a parent clock. If there is one, then it
4602 * checks that the provider for this parent clock was initialized, in
4603 * this case the parent clock will be ready.
4604 */
4605static int parent_ready(struct device_node *np)
4606{
4607 int i = 0;
4608
4609 while (true) {
4610 struct clk *clk = of_clk_get(np, i);
4611
4612 /* this parent is ready we can check the next one */
4613 if (!IS_ERR(clk)) {
4614 clk_put(clk);
4615 i++;
4616 continue;
4617 }
4618
4619 /* at least one parent is not ready, we exit now */
4620 if (PTR_ERR(clk) == -EPROBE_DEFER)
4621 return 0;
4622
4623 /*
4624 * Here we make assumption that the device tree is
4625 * written correctly. So an error means that there is
4626 * no more parent. As we didn't exit yet, then the
4627 * previous parent are ready. If there is no clock
4628 * parent, no need to wait for them, then we can
4629 * consider their absence as being ready
4630 */
4631 return 1;
4632 }
4633}
4634
Grant Likely766e6a42012-04-09 14:50:06 -05004635/**
Lee Jonesd56f8992016-02-11 13:19:11 -08004636 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4637 * @np: Device node pointer associated with clock provider
4638 * @index: clock index
Geert Uytterhoevenf7ae7502018-01-03 12:06:14 +01004639 * @flags: pointer to top-level framework flags
Lee Jonesd56f8992016-02-11 13:19:11 -08004640 *
4641 * Detects if the clock-critical property exists and, if so, sets the
4642 * corresponding CLK_IS_CRITICAL flag.
4643 *
4644 * Do not use this function. It exists only for legacy Device Tree
4645 * bindings, such as the one-clock-per-node style that are outdated.
4646 * Those bindings typically put all clock data into .dts and the Linux
4647 * driver has no clock data, thus making it impossible to set this flag
4648 * correctly from the driver. Only those drivers may call
4649 * of_clk_detect_critical from their setup functions.
4650 *
4651 * Return: error code or zero on success
4652 */
4653int of_clk_detect_critical(struct device_node *np,
4654 int index, unsigned long *flags)
4655{
4656 struct property *prop;
4657 const __be32 *cur;
4658 uint32_t idx;
4659
4660 if (!np || !flags)
4661 return -EINVAL;
4662
4663 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4664 if (index == idx)
4665 *flags |= CLK_IS_CRITICAL;
4666
4667 return 0;
4668}
4669
4670/**
Grant Likely766e6a42012-04-09 14:50:06 -05004671 * of_clk_init() - Scan and init clock providers from the DT
4672 * @matches: array of compatible values and init functions for providers.
4673 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004674 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01004675 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004676 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05004677 */
4678void __init of_clk_init(const struct of_device_id *matches)
4679{
Alex Elder7f7ed582013-08-22 11:31:31 -05004680 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05004681 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004682 struct clock_provider *clk_provider, *next;
4683 bool is_init_done;
4684 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07004685 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05004686
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304687 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03004688 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304689
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004690 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05004691 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004692 struct clock_provider *parent;
4693
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01004694 if (!of_device_is_available(np))
4695 continue;
4696
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004697 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4698 if (!parent) {
4699 list_for_each_entry_safe(clk_provider, next,
4700 &clk_provider_list, node) {
4701 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004702 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004703 kfree(clk_provider);
4704 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004705 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004706 return;
4707 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004708
4709 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004710 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01004711 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004712 }
4713
4714 while (!list_empty(&clk_provider_list)) {
4715 is_init_done = false;
4716 list_for_each_entry_safe(clk_provider, next,
4717 &clk_provider_list, node) {
4718 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004719
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02004720 /* Don't populate platform devices */
4721 of_node_set_flag(clk_provider->np,
4722 OF_POPULATED);
4723
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004724 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004725 of_clk_set_defaults(clk_provider->np, true);
4726
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004727 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004728 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004729 kfree(clk_provider);
4730 is_init_done = true;
4731 }
4732 }
4733
4734 /*
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01004735 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004736 * remaining providers during the last loop, so now we
4737 * initialize all the remaining ones unconditionally
4738 * in case the clock parent was not mandatory
4739 */
4740 if (!is_init_done)
4741 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05004742 }
4743}
4744#endif