blob: bbe90babdae466e34d14c89ac8153c639bf1a623 [file] [log] [blame]
Mike Turquetteb24764902012-03-15 23:11:19 -07001/*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Standard functionality for the common clock API. See Documentation/clk.txt
10 */
11
Stephen Boyd3c373112015-06-19 15:00:46 -070012#include <linux/clk.h>
Michael Turquetteb09d6d92015-01-29 14:22:50 -080013#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020014#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070015#include <linux/module.h>
16#include <linux/mutex.h>
17#include <linux/spinlock.h>
18#include <linux/err.h>
19#include <linux/list.h>
20#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050021#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070022#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053023#include <linux/init.h>
Marek Szyprowski9a34b452017-08-21 10:04:59 +020024#include <linux/pm_runtime.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070025#include <linux/sched.h>
Stephen Boyd562ef0b2015-05-01 12:16:14 -070026#include <linux/clkdev.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070027
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020028#include "clk.h"
29
Mike Turquetteb24764902012-03-15 23:11:19 -070030static DEFINE_SPINLOCK(enable_lock);
31static DEFINE_MUTEX(prepare_lock);
32
Mike Turquette533ddeb2013-03-28 13:59:02 -070033static struct task_struct *prepare_owner;
34static struct task_struct *enable_owner;
35
36static int prepare_refcnt;
37static int enable_refcnt;
38
Mike Turquetteb24764902012-03-15 23:11:19 -070039static HLIST_HEAD(clk_root_list);
40static HLIST_HEAD(clk_orphan_list);
41static LIST_HEAD(clk_notifier_list);
42
Michael Turquetteb09d6d92015-01-29 14:22:50 -080043/*** private data structures ***/
44
45struct clk_core {
46 const char *name;
47 const struct clk_ops *ops;
48 struct clk_hw *hw;
49 struct module *owner;
Marek Szyprowski9a34b452017-08-21 10:04:59 +020050 struct device *dev;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080051 struct clk_core *parent;
52 const char **parent_names;
53 struct clk_core **parents;
54 u8 num_parents;
55 u8 new_parent_index;
56 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010057 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080058 unsigned long new_rate;
59 struct clk_core *new_parent;
60 struct clk_core *new_child;
61 unsigned long flags;
Heiko Stuebnere6500342015-04-22 22:53:05 +020062 bool orphan;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080063 unsigned int enable_count;
64 unsigned int prepare_count;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070065 unsigned long min_rate;
66 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080067 unsigned long accuracy;
68 int phase;
69 struct hlist_head children;
70 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010071 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080072 unsigned int notifier_count;
73#ifdef CONFIG_DEBUG_FS
74 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020075 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080076#endif
77 struct kref ref;
78};
79
Stephen Boyddfc202e2015-02-02 14:37:41 -080080#define CREATE_TRACE_POINTS
81#include <trace/events/clk.h>
82
Michael Turquetteb09d6d92015-01-29 14:22:50 -080083struct clk {
84 struct clk_core *core;
85 const char *dev_id;
86 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010087 unsigned long min_rate;
88 unsigned long max_rate;
Stephen Boyd50595f82015-02-06 11:42:44 -080089 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080090};
91
Marek Szyprowski9a34b452017-08-21 10:04:59 +020092/*** runtime pm ***/
93static int clk_pm_runtime_get(struct clk_core *core)
94{
95 int ret = 0;
96
97 if (!core->dev)
98 return 0;
99
100 ret = pm_runtime_get_sync(core->dev);
101 return ret < 0 ? ret : 0;
102}
103
104static void clk_pm_runtime_put(struct clk_core *core)
105{
106 if (!core->dev)
107 return;
108
109 pm_runtime_put_sync(core->dev);
110}
111
Mike Turquetteeab89f62013-03-28 13:59:01 -0700112/*** locking ***/
113static void clk_prepare_lock(void)
114{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700115 if (!mutex_trylock(&prepare_lock)) {
116 if (prepare_owner == current) {
117 prepare_refcnt++;
118 return;
119 }
120 mutex_lock(&prepare_lock);
121 }
122 WARN_ON_ONCE(prepare_owner != NULL);
123 WARN_ON_ONCE(prepare_refcnt != 0);
124 prepare_owner = current;
125 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700126}
127
128static void clk_prepare_unlock(void)
129{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700130 WARN_ON_ONCE(prepare_owner != current);
131 WARN_ON_ONCE(prepare_refcnt == 0);
132
133 if (--prepare_refcnt)
134 return;
135 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700136 mutex_unlock(&prepare_lock);
137}
138
139static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700140 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700141{
142 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700143
144 if (!spin_trylock_irqsave(&enable_lock, flags)) {
145 if (enable_owner == current) {
146 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700147 __acquire(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700148 return flags;
149 }
150 spin_lock_irqsave(&enable_lock, flags);
151 }
152 WARN_ON_ONCE(enable_owner != NULL);
153 WARN_ON_ONCE(enable_refcnt != 0);
154 enable_owner = current;
155 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700156 return flags;
157}
158
159static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700160 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700161{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700162 WARN_ON_ONCE(enable_owner != current);
163 WARN_ON_ONCE(enable_refcnt == 0);
164
Stephen Boyda57aa182015-07-24 12:24:48 -0700165 if (--enable_refcnt) {
166 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700167 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700168 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700169 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700170 spin_unlock_irqrestore(&enable_lock, flags);
171}
172
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700173static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530174{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200175 bool ret = false;
176
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700177 /*
178 * .is_prepared is optional for clocks that can prepare
179 * fall back to software usage counter if it is missing
180 */
181 if (!core->ops->is_prepared)
182 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530183
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200184 if (!clk_pm_runtime_get(core)) {
185 ret = core->ops->is_prepared(core->hw);
186 clk_pm_runtime_put(core);
187 }
188
189 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530190}
191
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700192static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530193{
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200194 bool ret = false;
195
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700196 /*
197 * .is_enabled is only mandatory for clocks that gate
198 * fall back to software usage counter if .is_enabled is missing
199 */
200 if (!core->ops->is_enabled)
201 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530202
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200203 /*
204 * Check if clock controller's device is runtime active before
205 * calling .is_enabled callback. If not, assume that clock is
206 * disabled, because we might be called from atomic context, from
207 * which pm_runtime_get() is not allowed.
208 * This function is called mainly from clk_disable_unused_subtree,
209 * which ensures proper runtime pm activation of controller before
210 * taking enable spinlock, but the below check is needed if one tries
211 * to call it from other places.
212 */
213 if (core->dev) {
214 pm_runtime_get_noresume(core->dev);
215 if (!pm_runtime_active(core->dev)) {
216 ret = false;
217 goto done;
218 }
219 }
220
221 ret = core->ops->is_enabled(core->hw);
222done:
223 clk_pm_runtime_put(core);
224
225 return ret;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530226}
227
Mike Turquetteb24764902012-03-15 23:11:19 -0700228/*** helper functions ***/
229
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200230const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700231{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100232 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700233}
Niels de Vos48950842012-12-13 13:12:25 +0100234EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700235
Stephen Boyde7df6f62015-08-12 13:04:56 -0700236const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700237{
238 return hw->core->name;
239}
240EXPORT_SYMBOL_GPL(clk_hw_get_name);
241
Russ Dill65800b22012-11-26 11:20:09 -0800242struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700243{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100244 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700245}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800246EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700247
Stephen Boyde7df6f62015-08-12 13:04:56 -0700248unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700249{
250 return hw->core->num_parents;
251}
252EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
253
Stephen Boyde7df6f62015-08-12 13:04:56 -0700254struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700255{
256 return hw->core->parent ? hw->core->parent->hw : NULL;
257}
258EXPORT_SYMBOL_GPL(clk_hw_get_parent);
259
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700260static struct clk_core *__clk_lookup_subtree(const char *name,
261 struct clk_core *core)
262{
263 struct clk_core *child;
264 struct clk_core *ret;
265
266 if (!strcmp(core->name, name))
267 return core;
268
269 hlist_for_each_entry(child, &core->children, child_node) {
270 ret = __clk_lookup_subtree(name, child);
271 if (ret)
272 return ret;
273 }
274
275 return NULL;
276}
277
278static struct clk_core *clk_core_lookup(const char *name)
279{
280 struct clk_core *root_clk;
281 struct clk_core *ret;
282
283 if (!name)
284 return NULL;
285
286 /* search the 'proper' clk tree first */
287 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
288 ret = __clk_lookup_subtree(name, root_clk);
289 if (ret)
290 return ret;
291 }
292
293 /* if not found, then search the orphan tree */
294 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
295 ret = __clk_lookup_subtree(name, root_clk);
296 if (ret)
297 return ret;
298 }
299
300 return NULL;
301}
302
Stephen Boydd6968fc2015-04-30 13:54:13 -0700303static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100304 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100305{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700306 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100307 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900308
309 if (!core->parents[index])
310 core->parents[index] =
311 clk_core_lookup(core->parent_names[index]);
312
313 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100314}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100315
Stephen Boyde7df6f62015-08-12 13:04:56 -0700316struct clk_hw *
317clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700318{
319 struct clk_core *parent;
320
321 parent = clk_core_get_parent_by_index(hw->core, index);
322
323 return !parent ? NULL : parent->hw;
324}
325EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
326
Russ Dill65800b22012-11-26 11:20:09 -0800327unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700328{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100329 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700330}
331
Stephen Boydd6968fc2015-04-30 13:54:13 -0700332static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700333{
334 unsigned long ret;
335
Stephen Boydd6968fc2015-04-30 13:54:13 -0700336 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530337 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700338 goto out;
339 }
340
Stephen Boydd6968fc2015-04-30 13:54:13 -0700341 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700342
Stephen Boyd47b0eeb2016-02-02 17:24:56 -0800343 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -0700344 goto out;
345
Stephen Boydd6968fc2015-04-30 13:54:13 -0700346 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530347 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700348
349out:
350 return ret;
351}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100352
Stephen Boyde7df6f62015-08-12 13:04:56 -0700353unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700354{
355 return clk_core_get_rate_nolock(hw->core);
356}
357EXPORT_SYMBOL_GPL(clk_hw_get_rate);
358
Stephen Boydd6968fc2015-04-30 13:54:13 -0700359static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100360{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700361 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100362 return 0;
363
Stephen Boydd6968fc2015-04-30 13:54:13 -0700364 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100365}
366
Russ Dill65800b22012-11-26 11:20:09 -0800367unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700368{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100369 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700370}
Thierry Redingb05c6832013-09-03 09:43:51 +0200371EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700372
Stephen Boyde7df6f62015-08-12 13:04:56 -0700373unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700374{
375 return hw->core->flags;
376}
377EXPORT_SYMBOL_GPL(clk_hw_get_flags);
378
Stephen Boyde7df6f62015-08-12 13:04:56 -0700379bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700380{
381 return clk_core_is_prepared(hw->core);
382}
383
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200384bool clk_hw_is_enabled(const struct clk_hw *hw)
385{
386 return clk_core_is_enabled(hw->core);
387}
388
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100389bool __clk_is_enabled(struct clk *clk)
390{
391 if (!clk)
392 return false;
393
394 return clk_core_is_enabled(clk->core);
395}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800396EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700397
Stephen Boyd15a02c12015-01-19 18:05:28 -0800398static bool mux_is_better_rate(unsigned long rate, unsigned long now,
399 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100400{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800401 if (flags & CLK_MUX_ROUND_CLOSEST)
402 return abs(now - rate) < abs(best - rate);
403
404 return now <= rate && now > best;
405}
406
Boris Brezillon0817b622015-07-07 20:48:08 +0200407static int
408clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800409 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100410{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100411 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200412 int i, num_parents, ret;
413 unsigned long best = 0;
414 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100415
416 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100417 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
418 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200419 if (core->flags & CLK_SET_RATE_PARENT) {
420 ret = __clk_determine_rate(parent ? parent->hw : NULL,
421 &parent_req);
422 if (ret)
423 return ret;
424
425 best = parent_req.rate;
426 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100427 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200428 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100429 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200430 }
431
James Hogane366fdd2013-07-29 12:25:02 +0100432 goto out;
433 }
434
435 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100436 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100437 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100438 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100439 if (!parent)
440 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200441
442 if (core->flags & CLK_SET_RATE_PARENT) {
443 parent_req = *req;
444 ret = __clk_determine_rate(parent->hw, &parent_req);
445 if (ret)
446 continue;
447 } else {
448 parent_req.rate = clk_core_get_rate_nolock(parent);
449 }
450
451 if (mux_is_better_rate(req->rate, parent_req.rate,
452 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100453 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200454 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100455 }
456 }
457
Boris Brezillon57d866e2015-07-09 22:39:38 +0200458 if (!best_parent)
459 return -EINVAL;
460
James Hogane366fdd2013-07-29 12:25:02 +0100461out:
462 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200463 req->best_parent_hw = best_parent->hw;
464 req->best_parent_rate = best;
465 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100466
Boris Brezillon0817b622015-07-07 20:48:08 +0200467 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100468}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800469
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100470struct clk *__clk_lookup(const char *name)
471{
472 struct clk_core *core = clk_core_lookup(name);
473
474 return !core ? NULL : core->hw->clk;
475}
476
Stephen Boydd6968fc2015-04-30 13:54:13 -0700477static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100478 unsigned long *min_rate,
479 unsigned long *max_rate)
480{
481 struct clk *clk_user;
482
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700483 *min_rate = core->min_rate;
484 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100485
Stephen Boydd6968fc2015-04-30 13:54:13 -0700486 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100487 *min_rate = max(*min_rate, clk_user->min_rate);
488
Stephen Boydd6968fc2015-04-30 13:54:13 -0700489 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100490 *max_rate = min(*max_rate, clk_user->max_rate);
491}
492
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700493void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
494 unsigned long max_rate)
495{
496 hw->core->min_rate = min_rate;
497 hw->core->max_rate = max_rate;
498}
499EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
500
Stephen Boyd15a02c12015-01-19 18:05:28 -0800501/*
502 * Helper for finding best parent to provide a given frequency. This can be used
503 * directly as a determine_rate callback (e.g. for a mux), or from a more
504 * complex clock that may combine a mux with other operations.
505 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200506int __clk_mux_determine_rate(struct clk_hw *hw,
507 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800508{
Boris Brezillon0817b622015-07-07 20:48:08 +0200509 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800510}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800511EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100512
Boris Brezillon0817b622015-07-07 20:48:08 +0200513int __clk_mux_determine_rate_closest(struct clk_hw *hw,
514 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800515{
Boris Brezillon0817b622015-07-07 20:48:08 +0200516 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800517}
518EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
519
Mike Turquetteb24764902012-03-15 23:11:19 -0700520/*** clk api ***/
521
Stephen Boydd6968fc2015-04-30 13:54:13 -0700522static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700523{
Stephen Boyda6334722015-05-06 17:00:54 -0700524 lockdep_assert_held(&prepare_lock);
525
Stephen Boydd6968fc2015-04-30 13:54:13 -0700526 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700527 return;
528
Stephen Boydd6968fc2015-04-30 13:54:13 -0700529 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700530 return;
531
Lee Jones2e20fbf2016-02-11 13:19:10 -0800532 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
533 return;
534
Stephen Boydd6968fc2015-04-30 13:54:13 -0700535 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700536 return;
537
Stephen Boydd6968fc2015-04-30 13:54:13 -0700538 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700539
Stephen Boydd6968fc2015-04-30 13:54:13 -0700540 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800541
Stephen Boydd6968fc2015-04-30 13:54:13 -0700542 if (core->ops->unprepare)
543 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700544
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200545 clk_pm_runtime_put(core);
546
Stephen Boydd6968fc2015-04-30 13:54:13 -0700547 trace_clk_unprepare_complete(core);
548 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700549}
550
Dong Aishenga6adc302016-06-30 17:31:11 +0800551static void clk_core_unprepare_lock(struct clk_core *core)
552{
553 clk_prepare_lock();
554 clk_core_unprepare(core);
555 clk_prepare_unlock();
556}
557
Mike Turquetteb24764902012-03-15 23:11:19 -0700558/**
559 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200560 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700561 *
562 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
563 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
564 * if the operation may sleep. One example is a clk which is accessed over
565 * I2c. In the complex case a clk gate operation may require a fast and a slow
566 * part. It is this reason that clk_unprepare and clk_disable are not mutually
567 * exclusive. In fact clk_disable must be called before clk_unprepare.
568 */
569void clk_unprepare(struct clk *clk)
570{
Stephen Boyd63589e92014-03-26 16:06:37 -0700571 if (IS_ERR_OR_NULL(clk))
572 return;
573
Dong Aishenga6adc302016-06-30 17:31:11 +0800574 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700575}
576EXPORT_SYMBOL_GPL(clk_unprepare);
577
Stephen Boydd6968fc2015-04-30 13:54:13 -0700578static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700579{
580 int ret = 0;
581
Stephen Boyda6334722015-05-06 17:00:54 -0700582 lockdep_assert_held(&prepare_lock);
583
Stephen Boydd6968fc2015-04-30 13:54:13 -0700584 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700585 return 0;
586
Stephen Boydd6968fc2015-04-30 13:54:13 -0700587 if (core->prepare_count == 0) {
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200588 ret = clk_pm_runtime_get(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700589 if (ret)
590 return ret;
591
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200592 ret = clk_core_prepare(core->parent);
593 if (ret)
594 goto runtime_put;
595
Stephen Boydd6968fc2015-04-30 13:54:13 -0700596 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800597
Stephen Boydd6968fc2015-04-30 13:54:13 -0700598 if (core->ops->prepare)
599 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800600
Stephen Boydd6968fc2015-04-30 13:54:13 -0700601 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800602
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200603 if (ret)
604 goto unprepare;
Mike Turquetteb24764902012-03-15 23:11:19 -0700605 }
606
Stephen Boydd6968fc2015-04-30 13:54:13 -0700607 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700608
609 return 0;
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200610unprepare:
611 clk_core_unprepare(core->parent);
612runtime_put:
613 clk_pm_runtime_put(core);
614 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700615}
616
Dong Aishenga6adc302016-06-30 17:31:11 +0800617static int clk_core_prepare_lock(struct clk_core *core)
618{
619 int ret;
620
621 clk_prepare_lock();
622 ret = clk_core_prepare(core);
623 clk_prepare_unlock();
624
625 return ret;
626}
627
Mike Turquetteb24764902012-03-15 23:11:19 -0700628/**
629 * clk_prepare - prepare a clock source
630 * @clk: the clk being prepared
631 *
632 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
633 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
634 * operation may sleep. One example is a clk which is accessed over I2c. In
635 * the complex case a clk ungate operation may require a fast and a slow part.
636 * It is this reason that clk_prepare and clk_enable are not mutually
637 * exclusive. In fact clk_prepare must be called before clk_enable.
638 * Returns 0 on success, -EERROR otherwise.
639 */
640int clk_prepare(struct clk *clk)
641{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100642 if (!clk)
643 return 0;
644
Dong Aishenga6adc302016-06-30 17:31:11 +0800645 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700646}
647EXPORT_SYMBOL_GPL(clk_prepare);
648
Stephen Boydd6968fc2015-04-30 13:54:13 -0700649static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700650{
Stephen Boyda6334722015-05-06 17:00:54 -0700651 lockdep_assert_held(&enable_lock);
652
Stephen Boydd6968fc2015-04-30 13:54:13 -0700653 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700654 return;
655
Stephen Boydd6968fc2015-04-30 13:54:13 -0700656 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700657 return;
658
Lee Jones2e20fbf2016-02-11 13:19:10 -0800659 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
660 return;
661
Stephen Boydd6968fc2015-04-30 13:54:13 -0700662 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700663 return;
664
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700665 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800666
Stephen Boydd6968fc2015-04-30 13:54:13 -0700667 if (core->ops->disable)
668 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700669
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700670 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800671
Stephen Boydd6968fc2015-04-30 13:54:13 -0700672 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100673}
674
Dong Aishenga6adc302016-06-30 17:31:11 +0800675static void clk_core_disable_lock(struct clk_core *core)
676{
677 unsigned long flags;
678
679 flags = clk_enable_lock();
680 clk_core_disable(core);
681 clk_enable_unlock(flags);
682}
683
Mike Turquetteb24764902012-03-15 23:11:19 -0700684/**
685 * clk_disable - gate a clock
686 * @clk: the clk being gated
687 *
688 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
689 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
690 * clk if the operation is fast and will never sleep. One example is a
691 * SoC-internal clk which is controlled via simple register writes. In the
692 * complex case a clk gate operation may require a fast and a slow part. It is
693 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
694 * In fact clk_disable must be called before clk_unprepare.
695 */
696void clk_disable(struct clk *clk)
697{
Stephen Boyd63589e92014-03-26 16:06:37 -0700698 if (IS_ERR_OR_NULL(clk))
699 return;
700
Dong Aishenga6adc302016-06-30 17:31:11 +0800701 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700702}
703EXPORT_SYMBOL_GPL(clk_disable);
704
Stephen Boydd6968fc2015-04-30 13:54:13 -0700705static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700706{
707 int ret = 0;
708
Stephen Boyda6334722015-05-06 17:00:54 -0700709 lockdep_assert_held(&enable_lock);
710
Stephen Boydd6968fc2015-04-30 13:54:13 -0700711 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700712 return 0;
713
Stephen Boydd6968fc2015-04-30 13:54:13 -0700714 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700715 return -ESHUTDOWN;
716
Stephen Boydd6968fc2015-04-30 13:54:13 -0700717 if (core->enable_count == 0) {
718 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700719
720 if (ret)
721 return ret;
722
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700723 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800724
Stephen Boydd6968fc2015-04-30 13:54:13 -0700725 if (core->ops->enable)
726 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800727
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700728 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800729
730 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700731 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800732 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700733 }
734 }
735
Stephen Boydd6968fc2015-04-30 13:54:13 -0700736 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700737 return 0;
738}
739
Dong Aishenga6adc302016-06-30 17:31:11 +0800740static int clk_core_enable_lock(struct clk_core *core)
741{
742 unsigned long flags;
743 int ret;
744
745 flags = clk_enable_lock();
746 ret = clk_core_enable(core);
747 clk_enable_unlock(flags);
748
749 return ret;
750}
751
Mike Turquetteb24764902012-03-15 23:11:19 -0700752/**
753 * clk_enable - ungate a clock
754 * @clk: the clk being ungated
755 *
756 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
757 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
758 * if the operation will never sleep. One example is a SoC-internal clk which
759 * is controlled via simple register writes. In the complex case a clk ungate
760 * operation may require a fast and a slow part. It is this reason that
761 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
762 * must be called before clk_enable. Returns 0 on success, -EERROR
763 * otherwise.
764 */
765int clk_enable(struct clk *clk)
766{
Dong Aisheng864e1602015-04-30 14:02:19 -0700767 if (!clk)
768 return 0;
769
Dong Aishenga6adc302016-06-30 17:31:11 +0800770 return clk_core_enable_lock(clk->core);
771}
772EXPORT_SYMBOL_GPL(clk_enable);
773
774static int clk_core_prepare_enable(struct clk_core *core)
775{
776 int ret;
777
778 ret = clk_core_prepare_lock(core);
779 if (ret)
780 return ret;
781
782 ret = clk_core_enable_lock(core);
783 if (ret)
784 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700785
786 return ret;
787}
Dong Aishenga6adc302016-06-30 17:31:11 +0800788
789static void clk_core_disable_unprepare(struct clk_core *core)
790{
791 clk_core_disable_lock(core);
792 clk_core_unprepare_lock(core);
793}
Mike Turquetteb24764902012-03-15 23:11:19 -0700794
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800795static void clk_unprepare_unused_subtree(struct clk_core *core)
796{
797 struct clk_core *child;
798
799 lockdep_assert_held(&prepare_lock);
800
801 hlist_for_each_entry(child, &core->children, child_node)
802 clk_unprepare_unused_subtree(child);
803
804 if (core->prepare_count)
805 return;
806
807 if (core->flags & CLK_IGNORE_UNUSED)
808 return;
809
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200810 if (clk_pm_runtime_get(core))
811 return;
812
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800813 if (clk_core_is_prepared(core)) {
814 trace_clk_unprepare(core);
815 if (core->ops->unprepare_unused)
816 core->ops->unprepare_unused(core->hw);
817 else if (core->ops->unprepare)
818 core->ops->unprepare(core->hw);
819 trace_clk_unprepare_complete(core);
820 }
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200821
822 clk_pm_runtime_put(core);
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800823}
824
825static void clk_disable_unused_subtree(struct clk_core *core)
826{
827 struct clk_core *child;
828 unsigned long flags;
829
830 lockdep_assert_held(&prepare_lock);
831
832 hlist_for_each_entry(child, &core->children, child_node)
833 clk_disable_unused_subtree(child);
834
Dong Aishenga4b35182016-06-30 17:31:13 +0800835 if (core->flags & CLK_OPS_PARENT_ENABLE)
836 clk_core_prepare_enable(core->parent);
837
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200838 if (clk_pm_runtime_get(core))
839 goto unprepare_out;
840
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800841 flags = clk_enable_lock();
842
843 if (core->enable_count)
844 goto unlock_out;
845
846 if (core->flags & CLK_IGNORE_UNUSED)
847 goto unlock_out;
848
849 /*
850 * some gate clocks have special needs during the disable-unused
851 * sequence. call .disable_unused if available, otherwise fall
852 * back to .disable
853 */
854 if (clk_core_is_enabled(core)) {
855 trace_clk_disable(core);
856 if (core->ops->disable_unused)
857 core->ops->disable_unused(core->hw);
858 else if (core->ops->disable)
859 core->ops->disable(core->hw);
860 trace_clk_disable_complete(core);
861 }
862
863unlock_out:
864 clk_enable_unlock(flags);
Marek Szyprowski9a34b452017-08-21 10:04:59 +0200865 clk_pm_runtime_put(core);
866unprepare_out:
Dong Aishenga4b35182016-06-30 17:31:13 +0800867 if (core->flags & CLK_OPS_PARENT_ENABLE)
868 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800869}
870
871static bool clk_ignore_unused;
872static int __init clk_ignore_unused_setup(char *__unused)
873{
874 clk_ignore_unused = true;
875 return 1;
876}
877__setup("clk_ignore_unused", clk_ignore_unused_setup);
878
879static int clk_disable_unused(void)
880{
881 struct clk_core *core;
882
883 if (clk_ignore_unused) {
884 pr_warn("clk: Not disabling unused clocks\n");
885 return 0;
886 }
887
888 clk_prepare_lock();
889
890 hlist_for_each_entry(core, &clk_root_list, child_node)
891 clk_disable_unused_subtree(core);
892
893 hlist_for_each_entry(core, &clk_orphan_list, child_node)
894 clk_disable_unused_subtree(core);
895
896 hlist_for_each_entry(core, &clk_root_list, child_node)
897 clk_unprepare_unused_subtree(core);
898
899 hlist_for_each_entry(core, &clk_orphan_list, child_node)
900 clk_unprepare_unused_subtree(core);
901
902 clk_prepare_unlock();
903
904 return 0;
905}
906late_initcall_sync(clk_disable_unused);
907
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +0100908static int clk_core_determine_round_nolock(struct clk_core *core,
909 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -0700910{
Boris Brezillon0817b622015-07-07 20:48:08 +0200911 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700912
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100913 lockdep_assert_held(&prepare_lock);
914
Stephen Boydd6968fc2015-04-30 13:54:13 -0700915 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700916 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700917
Stephen Boydd6968fc2015-04-30 13:54:13 -0700918 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +0200919 return core->ops->determine_rate(core->hw, req);
920 } else if (core->ops->round_rate) {
921 rate = core->ops->round_rate(core->hw, req->rate,
922 &req->best_parent_rate);
923 if (rate < 0)
924 return rate;
925
926 req->rate = rate;
Boris Brezillon0817b622015-07-07 20:48:08 +0200927 } else {
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +0100928 return -EINVAL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200929 }
930
931 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700932}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100933
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +0100934static void clk_core_init_rate_req(struct clk_core * const core,
935 struct clk_rate_request *req)
936{
937 struct clk_core *parent;
938
939 if (WARN_ON(!core || !req))
940 return;
941
942 parent = core->parent;
943 if (parent) {
944 req->best_parent_hw = parent->hw;
945 req->best_parent_rate = parent->rate;
946 } else {
947 req->best_parent_hw = NULL;
948 req->best_parent_rate = 0;
949 }
950}
951
952static bool clk_core_can_round(struct clk_core * const core)
953{
954 if (core->ops->determine_rate || core->ops->round_rate)
955 return true;
956
957 return false;
958}
959
960static int clk_core_round_rate_nolock(struct clk_core *core,
961 struct clk_rate_request *req)
962{
963 lockdep_assert_held(&prepare_lock);
964
965 if (!core)
966 return 0;
967
968 clk_core_init_rate_req(core, req);
969
970 if (clk_core_can_round(core))
971 return clk_core_determine_round_nolock(core, req);
972 else if (core->flags & CLK_SET_RATE_PARENT)
973 return clk_core_round_rate_nolock(core->parent, req);
974
975 req->rate = core->rate;
976 return 0;
977}
978
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100979/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100980 * __clk_determine_rate - get the closest rate actually supported by a clock
981 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +0800982 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100983 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -0700984 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100985 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200986int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100987{
Boris Brezillon0817b622015-07-07 20:48:08 +0200988 if (!hw) {
989 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100990 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +0200991 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100992
Boris Brezillon0817b622015-07-07 20:48:08 +0200993 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100994}
995EXPORT_SYMBOL_GPL(__clk_determine_rate);
996
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700997unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
998{
999 int ret;
1000 struct clk_rate_request req;
1001
1002 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1003 req.rate = rate;
1004
1005 ret = clk_core_round_rate_nolock(hw->core, &req);
1006 if (ret)
1007 return 0;
1008
1009 return req.rate;
1010}
1011EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1012
Mike Turquetteb24764902012-03-15 23:11:19 -07001013/**
1014 * clk_round_rate - round the given rate for a clk
1015 * @clk: the clk for which we are rounding a rate
1016 * @rate: the rate which is to be rounded
1017 *
1018 * Takes in a rate as input and rounds it to a rate that the clk can actually
1019 * use which is then returned. If clk doesn't support round_rate operation
1020 * then the parent rate is returned.
1021 */
1022long clk_round_rate(struct clk *clk, unsigned long rate)
1023{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001024 struct clk_rate_request req;
1025 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001026
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001027 if (!clk)
1028 return 0;
1029
Mike Turquetteeab89f62013-03-28 13:59:01 -07001030 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001031
1032 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1033 req.rate = rate;
1034
1035 ret = clk_core_round_rate_nolock(clk->core, &req);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001036 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001037
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001038 if (ret)
1039 return ret;
1040
1041 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001042}
1043EXPORT_SYMBOL_GPL(clk_round_rate);
1044
1045/**
1046 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001047 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001048 * @msg: clk notifier type (see include/linux/clk.h)
1049 * @old_rate: old clk rate
1050 * @new_rate: new clk rate
1051 *
1052 * Triggers a notifier call chain on the clk rate-change notification
1053 * for 'clk'. Passes a pointer to the struct clk and the previous
1054 * and current rates to the notifier callback. Intended to be called by
1055 * internal clock code only. Returns NOTIFY_DONE from the last driver
1056 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1057 * a driver returns that.
1058 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001059static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001060 unsigned long old_rate, unsigned long new_rate)
1061{
1062 struct clk_notifier *cn;
1063 struct clk_notifier_data cnd;
1064 int ret = NOTIFY_DONE;
1065
Mike Turquetteb24764902012-03-15 23:11:19 -07001066 cnd.old_rate = old_rate;
1067 cnd.new_rate = new_rate;
1068
1069 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001070 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001071 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001072 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1073 &cnd);
Peter De Schrijver17c34c52017-03-21 12:16:26 +02001074 if (ret & NOTIFY_STOP_MASK)
1075 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001076 }
1077 }
1078
1079 return ret;
1080}
1081
1082/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001083 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001084 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001085 *
1086 * Walks the subtree of clks starting with clk and recalculates accuracies as
1087 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001088 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001089 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001090 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001091static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001092{
1093 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001094 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001095
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001096 lockdep_assert_held(&prepare_lock);
1097
Stephen Boydd6968fc2015-04-30 13:54:13 -07001098 if (core->parent)
1099 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001100
Stephen Boydd6968fc2015-04-30 13:54:13 -07001101 if (core->ops->recalc_accuracy)
1102 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001103 parent_accuracy);
1104 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001105 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001106
Stephen Boydd6968fc2015-04-30 13:54:13 -07001107 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001108 __clk_recalc_accuracies(child);
1109}
1110
Stephen Boydd6968fc2015-04-30 13:54:13 -07001111static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001112{
1113 unsigned long accuracy;
1114
1115 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001116 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1117 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001118
Stephen Boydd6968fc2015-04-30 13:54:13 -07001119 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001120 clk_prepare_unlock();
1121
1122 return accuracy;
1123}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001124
1125/**
1126 * clk_get_accuracy - return the accuracy of clk
1127 * @clk: the clk whose accuracy is being returned
1128 *
1129 * Simply returns the cached accuracy of the clk, unless
1130 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1131 * issued.
1132 * If clk is NULL then returns 0.
1133 */
1134long clk_get_accuracy(struct clk *clk)
1135{
1136 if (!clk)
1137 return 0;
1138
1139 return clk_core_get_accuracy(clk->core);
1140}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001141EXPORT_SYMBOL_GPL(clk_get_accuracy);
1142
Stephen Boydd6968fc2015-04-30 13:54:13 -07001143static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001144 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001145{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001146 unsigned long rate = parent_rate;
1147
1148 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1149 rate = core->ops->recalc_rate(core->hw, parent_rate);
1150 clk_pm_runtime_put(core);
1151 }
1152 return rate;
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001153}
1154
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001155/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001156 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001157 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001158 * @msg: notification type (see include/linux/clk.h)
1159 *
1160 * Walks the subtree of clks starting with clk and recalculates rates as it
1161 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001162 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001163 *
1164 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1165 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001166 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001167static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001168{
1169 unsigned long old_rate;
1170 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001171 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001172
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001173 lockdep_assert_held(&prepare_lock);
1174
Stephen Boydd6968fc2015-04-30 13:54:13 -07001175 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001176
Stephen Boydd6968fc2015-04-30 13:54:13 -07001177 if (core->parent)
1178 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001179
Stephen Boydd6968fc2015-04-30 13:54:13 -07001180 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001181
1182 /*
1183 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1184 * & ABORT_RATE_CHANGE notifiers
1185 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001186 if (core->notifier_count && msg)
1187 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001188
Stephen Boydd6968fc2015-04-30 13:54:13 -07001189 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001190 __clk_recalc_rates(child, msg);
1191}
1192
Stephen Boydd6968fc2015-04-30 13:54:13 -07001193static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001194{
1195 unsigned long rate;
1196
1197 clk_prepare_lock();
1198
Stephen Boydd6968fc2015-04-30 13:54:13 -07001199 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1200 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001201
Stephen Boydd6968fc2015-04-30 13:54:13 -07001202 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001203 clk_prepare_unlock();
1204
1205 return rate;
1206}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001207
Mike Turquetteb24764902012-03-15 23:11:19 -07001208/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001209 * clk_get_rate - return the rate of clk
1210 * @clk: the clk whose rate is being returned
1211 *
1212 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1213 * is set, which means a recalc_rate will be issued.
1214 * If clk is NULL then returns 0.
1215 */
1216unsigned long clk_get_rate(struct clk *clk)
1217{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001218 if (!clk)
1219 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001220
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001221 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001222}
1223EXPORT_SYMBOL_GPL(clk_get_rate);
1224
Stephen Boydd6968fc2015-04-30 13:54:13 -07001225static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001226 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001227{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001228 int i;
James Hogan4935b222013-07-29 12:24:59 +01001229
Masahiro Yamada508f8842015-12-28 19:23:08 +09001230 if (!parent)
1231 return -EINVAL;
1232
Masahiro Yamada470b5e22015-12-28 19:23:09 +09001233 for (i = 0; i < core->num_parents; i++)
1234 if (clk_core_get_parent_by_index(core, i) == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001235 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001236
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001237 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001238}
1239
Heiko Stuebnere6500342015-04-22 22:53:05 +02001240/*
1241 * Update the orphan status of @core and all its children.
1242 */
1243static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1244{
1245 struct clk_core *child;
1246
1247 core->orphan = is_orphan;
1248
1249 hlist_for_each_entry(child, &core->children, child_node)
1250 clk_core_update_orphan_status(child, is_orphan);
1251}
1252
Stephen Boydd6968fc2015-04-30 13:54:13 -07001253static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001254{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001255 bool was_orphan = core->orphan;
1256
Stephen Boydd6968fc2015-04-30 13:54:13 -07001257 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001258
James Hogan903efc52013-08-29 12:10:51 +01001259 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001260 bool becomes_orphan = new_parent->orphan;
1261
James Hogan903efc52013-08-29 12:10:51 +01001262 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001263 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001264 new_parent->new_child = NULL;
1265
Stephen Boydd6968fc2015-04-30 13:54:13 -07001266 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001267
1268 if (was_orphan != becomes_orphan)
1269 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001270 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001271 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001272 if (!was_orphan)
1273 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001274 }
James Hogan4935b222013-07-29 12:24:59 +01001275
Stephen Boydd6968fc2015-04-30 13:54:13 -07001276 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001277}
1278
Stephen Boydd6968fc2015-04-30 13:54:13 -07001279static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001280 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001281{
1282 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001283 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001284
1285 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001286 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1287 *
1288 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001289 * clk_enable().
1290 *
1291 * If the clock is not prepared, then a race with
1292 * clk_enable/disable() is impossible since we already have the
1293 * prepare lock (future calls to clk_enable() need to be preceded by
1294 * a clk_prepare()).
1295 *
1296 * If the clock is prepared, migrate the prepared state to the new
1297 * parent and also protect against a race with clk_enable() by
1298 * forcing the clock and the new parent on. This ensures that all
1299 * future calls to clk_enable() are practically NOPs with respect to
1300 * hardware and software states.
1301 *
1302 * See also: Comment for clk_set_parent() below.
1303 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001304
1305 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1306 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1307 clk_core_prepare_enable(old_parent);
1308 clk_core_prepare_enable(parent);
1309 }
1310
1311 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001312 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001313 clk_core_prepare_enable(parent);
1314 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001315 }
1316
1317 /* update the clk tree topology */
1318 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001319 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001320 clk_enable_unlock(flags);
1321
Stephen Boyd3fa22522014-01-15 10:47:22 -08001322 return old_parent;
1323}
1324
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001325static void __clk_set_parent_after(struct clk_core *core,
1326 struct clk_core *parent,
1327 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001328{
1329 /*
1330 * Finish the migration of prepare state and undo the changes done
1331 * for preventing a race with clk_enable().
1332 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001333 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001334 clk_core_disable_lock(core);
1335 clk_core_disable_unprepare(old_parent);
1336 }
1337
1338 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1339 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1340 clk_core_disable_unprepare(parent);
1341 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001342 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001343}
1344
Stephen Boydd6968fc2015-04-30 13:54:13 -07001345static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001346 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001347{
1348 unsigned long flags;
1349 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001350 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001351
Stephen Boydd6968fc2015-04-30 13:54:13 -07001352 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001353
Stephen Boydd6968fc2015-04-30 13:54:13 -07001354 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001355
James Hogan4935b222013-07-29 12:24:59 +01001356 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001357 if (parent && core->ops->set_parent)
1358 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001359
Stephen Boydd6968fc2015-04-30 13:54:13 -07001360 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001361
James Hogan4935b222013-07-29 12:24:59 +01001362 if (ret) {
1363 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001364 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001365 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001366 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001367
James Hogan4935b222013-07-29 12:24:59 +01001368 return ret;
1369 }
1370
Stephen Boydd6968fc2015-04-30 13:54:13 -07001371 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001372
James Hogan4935b222013-07-29 12:24:59 +01001373 return 0;
1374}
1375
Ulf Hanssona093bde2012-08-31 14:21:28 +02001376/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001377 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001378 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001379 * @parent_rate: the "future" rate of clk's parent
1380 *
1381 * Walks the subtree of clks starting with clk, speculating rates as it
1382 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1383 *
1384 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1385 * pre-rate change notifications and returns early if no clks in the
1386 * subtree have subscribed to the notifications. Note that if a clk does not
1387 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001388 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001389 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001390static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001391 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001392{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001393 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001394 unsigned long new_rate;
1395 int ret = NOTIFY_DONE;
1396
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001397 lockdep_assert_held(&prepare_lock);
1398
Stephen Boydd6968fc2015-04-30 13:54:13 -07001399 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001400
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001401 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001402 if (core->notifier_count)
1403 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001404
Mike Turquette86bcfa22014-02-24 16:08:41 -08001405 if (ret & NOTIFY_STOP_MASK) {
1406 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001407 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001408 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001409 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001410
Stephen Boydd6968fc2015-04-30 13:54:13 -07001411 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001412 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001413 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001414 break;
1415 }
1416
1417out:
1418 return ret;
1419}
1420
Stephen Boydd6968fc2015-04-30 13:54:13 -07001421static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001422 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001423{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001424 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001425
Stephen Boydd6968fc2015-04-30 13:54:13 -07001426 core->new_rate = new_rate;
1427 core->new_parent = new_parent;
1428 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001429 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001430 core->new_child = NULL;
1431 if (new_parent && new_parent != core->parent)
1432 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001433
Stephen Boydd6968fc2015-04-30 13:54:13 -07001434 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001435 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001436 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001437 }
1438}
1439
1440/*
1441 * calculate the new rates returning the topmost clock that has to be
1442 * changed.
1443 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001444static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001445 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001446{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001447 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001448 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001449 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001450 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001451 unsigned long min_rate;
1452 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001453 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001454 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001455
Mike Turquette7452b212012-03-26 14:45:36 -07001456 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001457 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001458 return NULL;
1459
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001460 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001461 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001462 if (parent)
1463 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001464
Stephen Boydd6968fc2015-04-30 13:54:13 -07001465 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001466
James Hogan71472c02013-07-29 12:25:00 +01001467 /* find the closest rate and parent clk/rate */
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001468 if (clk_core_can_round(core)) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001469 struct clk_rate_request req;
1470
1471 req.rate = rate;
1472 req.min_rate = min_rate;
1473 req.max_rate = max_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +02001474
Jerome Brunet0f6cc2b2017-12-01 22:51:54 +01001475 clk_core_init_rate_req(core, &req);
1476
1477 ret = clk_core_determine_round_nolock(core, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001478 if (ret < 0)
1479 return NULL;
1480
Boris Brezillon0817b622015-07-07 20:48:08 +02001481 best_parent_rate = req.best_parent_rate;
1482 new_rate = req.rate;
1483 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001484
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001485 if (new_rate < min_rate || new_rate > max_rate)
1486 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001487 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001488 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001489 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001490 return NULL;
1491 } else {
1492 /* pass-through clock with adjustable parent */
1493 top = clk_calc_new_rates(parent, rate);
1494 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001495 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001496 }
1497
James Hogan71472c02013-07-29 12:25:00 +01001498 /* some clocks must be gated to change parent */
1499 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001500 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001501 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001502 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001503 return NULL;
1504 }
1505
James Hogan71472c02013-07-29 12:25:00 +01001506 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001507 if (parent && core->num_parents > 1) {
1508 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001509 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001510 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001511 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001512 return NULL;
1513 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001514 }
1515
Stephen Boydd6968fc2015-04-30 13:54:13 -07001516 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001517 best_parent_rate != parent->rate)
1518 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001519
1520out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001521 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001522
1523 return top;
1524}
1525
1526/*
1527 * Notify about rate changes in a subtree. Always walk down the whole tree
1528 * so that in case of an error we can walk down the whole tree again and
1529 * abort the change.
1530 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001531static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001532 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001533{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001534 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001535 int ret = NOTIFY_DONE;
1536
Stephen Boydd6968fc2015-04-30 13:54:13 -07001537 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301538 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001539
Stephen Boydd6968fc2015-04-30 13:54:13 -07001540 if (core->notifier_count) {
1541 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001542 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001543 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001544 }
1545
Stephen Boydd6968fc2015-04-30 13:54:13 -07001546 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001547 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001548 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001549 continue;
1550 tmp_clk = clk_propagate_rate_change(child, event);
1551 if (tmp_clk)
1552 fail_clk = tmp_clk;
1553 }
1554
Stephen Boydd6968fc2015-04-30 13:54:13 -07001555 /* handle the new child who might not be in core->children yet */
1556 if (core->new_child) {
1557 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001558 if (tmp_clk)
1559 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001560 }
1561
1562 return fail_clk;
1563}
1564
1565/*
1566 * walk down a subtree and set the new rates notifying the rate
1567 * change on the way
1568 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001569static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001570{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001571 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001572 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001573 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001574 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001575 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001576 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001577 struct clk_core *parent = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001578
Stephen Boydd6968fc2015-04-30 13:54:13 -07001579 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001580
Dong Aishengfc8726a2016-06-30 17:31:14 +08001581 if (core->new_parent) {
1582 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001583 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001584 } else if (core->parent) {
1585 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001586 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001587 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001588
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001589 if (core->flags & CLK_SET_RATE_UNGATE) {
1590 unsigned long flags;
1591
1592 clk_core_prepare(core);
1593 flags = clk_enable_lock();
1594 clk_core_enable(core);
1595 clk_enable_unlock(flags);
1596 }
1597
Stephen Boydd6968fc2015-04-30 13:54:13 -07001598 if (core->new_parent && core->new_parent != core->parent) {
1599 old_parent = __clk_set_parent_before(core, core->new_parent);
1600 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001601
Stephen Boydd6968fc2015-04-30 13:54:13 -07001602 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001603 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001604 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001605 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001606 core->new_parent_index);
1607 } else if (core->ops->set_parent) {
1608 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001609 }
1610
Stephen Boydd6968fc2015-04-30 13:54:13 -07001611 trace_clk_set_parent_complete(core, core->new_parent);
1612 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001613 }
1614
Dong Aishengfc8726a2016-06-30 17:31:14 +08001615 if (core->flags & CLK_OPS_PARENT_ENABLE)
1616 clk_core_prepare_enable(parent);
1617
Stephen Boydd6968fc2015-04-30 13:54:13 -07001618 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001619
Stephen Boydd6968fc2015-04-30 13:54:13 -07001620 if (!skip_set_rate && core->ops->set_rate)
1621 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001622
Stephen Boydd6968fc2015-04-30 13:54:13 -07001623 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001624
Stephen Boydd6968fc2015-04-30 13:54:13 -07001625 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001626
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001627 if (core->flags & CLK_SET_RATE_UNGATE) {
1628 unsigned long flags;
1629
1630 flags = clk_enable_lock();
1631 clk_core_disable(core);
1632 clk_enable_unlock(flags);
1633 clk_core_unprepare(core);
1634 }
1635
Dong Aishengfc8726a2016-06-30 17:31:14 +08001636 if (core->flags & CLK_OPS_PARENT_ENABLE)
1637 clk_core_disable_unprepare(parent);
1638
Stephen Boydd6968fc2015-04-30 13:54:13 -07001639 if (core->notifier_count && old_rate != core->rate)
1640 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001641
Michael Turquette85e88fa2015-06-20 12:18:03 -07001642 if (core->flags & CLK_RECALC_NEW_RATES)
1643 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001644
Tero Kristo067bb172014-08-21 16:47:45 +03001645 /*
1646 * Use safe iteration, as change_rate can actually swap parents
1647 * for certain clock types.
1648 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001649 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001650 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001651 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001652 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001653 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001654 }
1655
Stephen Boydd6968fc2015-04-30 13:54:13 -07001656 /* handle the new child who might not be in core->children yet */
1657 if (core->new_child)
1658 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001659}
1660
Jerome Brunetca5e0892017-12-01 22:51:55 +01001661static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1662 unsigned long req_rate)
1663{
1664 int ret;
1665 struct clk_rate_request req;
1666
1667 lockdep_assert_held(&prepare_lock);
1668
1669 if (!core)
1670 return 0;
1671
1672 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1673 req.rate = req_rate;
1674
1675 ret = clk_core_round_rate_nolock(core, &req);
1676
1677 return ret ? 0 : req.rate;
1678}
1679
Stephen Boydd6968fc2015-04-30 13:54:13 -07001680static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001681 unsigned long req_rate)
1682{
1683 struct clk_core *top, *fail_clk;
Jerome Brunetca5e0892017-12-01 22:51:55 +01001684 unsigned long rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001685 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001686
Stephen Boydd6968fc2015-04-30 13:54:13 -07001687 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001688 return 0;
1689
Jerome Brunetca5e0892017-12-01 22:51:55 +01001690 rate = clk_core_req_round_rate_nolock(core, req_rate);
1691
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001692 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001693 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001694 return 0;
1695
Stephen Boydd6968fc2015-04-30 13:54:13 -07001696 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001697 return -EBUSY;
1698
1699 /* calculate new rates and get the topmost changed clock */
Jerome Brunetca5e0892017-12-01 22:51:55 +01001700 top = clk_calc_new_rates(core, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001701 if (!top)
1702 return -EINVAL;
1703
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001704 ret = clk_pm_runtime_get(core);
1705 if (ret)
1706 return ret;
1707
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001708 /* notify that we are about to change rates */
1709 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1710 if (fail_clk) {
1711 pr_debug("%s: failed to set %s rate\n", __func__,
1712 fail_clk->name);
1713 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001714 ret = -EBUSY;
1715 goto err;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001716 }
1717
1718 /* change the rates */
1719 clk_change_rate(top);
1720
Stephen Boydd6968fc2015-04-30 13:54:13 -07001721 core->req_rate = req_rate;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001722err:
1723 clk_pm_runtime_put(core);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001724
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001725 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001726}
1727
Mike Turquetteb24764902012-03-15 23:11:19 -07001728/**
1729 * clk_set_rate - specify a new rate for clk
1730 * @clk: the clk whose rate is being changed
1731 * @rate: the new rate for clk
1732 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001733 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001734 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001735 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1736 * propagate up to clk's parent; whether or not this happens depends on the
1737 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1738 * after calling .round_rate then upstream parent propagation is ignored. If
1739 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001740 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001741 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1742 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001743 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001744 * Rate changes are accomplished via tree traversal that also recalculates the
1745 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001746 *
1747 * Returns 0 on success, -EERROR otherwise.
1748 */
1749int clk_set_rate(struct clk *clk, unsigned long rate)
1750{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001751 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001752
Mike Turquette89ac8d72013-08-21 23:58:09 -07001753 if (!clk)
1754 return 0;
1755
Mike Turquetteb24764902012-03-15 23:11:19 -07001756 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001757 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001758
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001759 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001760
Mike Turquetteeab89f62013-03-28 13:59:01 -07001761 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001762
1763 return ret;
1764}
1765EXPORT_SYMBOL_GPL(clk_set_rate);
1766
1767/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001768 * clk_set_rate_range - set a rate range for a clock source
1769 * @clk: clock source
1770 * @min: desired minimum clock rate in Hz, inclusive
1771 * @max: desired maximum clock rate in Hz, inclusive
1772 *
1773 * Returns success (0) or negative errno.
1774 */
1775int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1776{
1777 int ret = 0;
1778
1779 if (!clk)
1780 return 0;
1781
1782 if (min > max) {
1783 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1784 __func__, clk->core->name, clk->dev_id, clk->con_id,
1785 min, max);
1786 return -EINVAL;
1787 }
1788
1789 clk_prepare_lock();
1790
1791 if (min != clk->min_rate || max != clk->max_rate) {
1792 clk->min_rate = min;
1793 clk->max_rate = max;
1794 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1795 }
1796
1797 clk_prepare_unlock();
1798
1799 return ret;
1800}
1801EXPORT_SYMBOL_GPL(clk_set_rate_range);
1802
1803/**
1804 * clk_set_min_rate - set a minimum clock rate for a clock source
1805 * @clk: clock source
1806 * @rate: desired minimum clock rate in Hz, inclusive
1807 *
1808 * Returns success (0) or negative errno.
1809 */
1810int clk_set_min_rate(struct clk *clk, unsigned long rate)
1811{
1812 if (!clk)
1813 return 0;
1814
1815 return clk_set_rate_range(clk, rate, clk->max_rate);
1816}
1817EXPORT_SYMBOL_GPL(clk_set_min_rate);
1818
1819/**
1820 * clk_set_max_rate - set a maximum clock rate for a clock source
1821 * @clk: clock source
1822 * @rate: desired maximum clock rate in Hz, inclusive
1823 *
1824 * Returns success (0) or negative errno.
1825 */
1826int clk_set_max_rate(struct clk *clk, unsigned long rate)
1827{
1828 if (!clk)
1829 return 0;
1830
1831 return clk_set_rate_range(clk, clk->min_rate, rate);
1832}
1833EXPORT_SYMBOL_GPL(clk_set_max_rate);
1834
1835/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001836 * clk_get_parent - return the parent of a clk
1837 * @clk: the clk whose parent gets returned
1838 *
1839 * Simply returns clk->parent. Returns NULL if clk is NULL.
1840 */
1841struct clk *clk_get_parent(struct clk *clk)
1842{
1843 struct clk *parent;
1844
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001845 if (!clk)
1846 return NULL;
1847
Mike Turquetteeab89f62013-03-28 13:59:01 -07001848 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001849 /* TODO: Create a per-user clk and change callers to call clk_put */
1850 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07001851 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001852
1853 return parent;
1854}
1855EXPORT_SYMBOL_GPL(clk_get_parent);
1856
Stephen Boydd6968fc2015-04-30 13:54:13 -07001857static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001858{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09001859 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001860
Masahiro Yamada2430a942016-02-09 20:19:14 +09001861 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09001862 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001863
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09001864 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001865}
1866
Stephen Boydd6968fc2015-04-30 13:54:13 -07001867static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001868 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001869{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001870 clk_reparent(core, new_parent);
1871 __clk_recalc_accuracies(core);
1872 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07001873}
1874
Tomeu Vizoso42c86542015-03-11 11:34:25 +01001875void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
1876{
1877 if (!hw)
1878 return;
1879
1880 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
1881}
1882
Mike Turquetteb24764902012-03-15 23:11:19 -07001883/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001884 * clk_has_parent - check if a clock is a possible parent for another
1885 * @clk: clock source
1886 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07001887 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001888 * This function can be used in drivers that need to check that a clock can be
1889 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07001890 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001891 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07001892 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001893bool clk_has_parent(struct clk *clk, struct clk *parent)
1894{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001895 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001896 unsigned int i;
1897
1898 /* NULL clocks should be nops, so return success if either is NULL. */
1899 if (!clk || !parent)
1900 return true;
1901
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001902 core = clk->core;
1903 parent_core = parent->core;
1904
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001905 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001906 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001907 return true;
1908
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001909 for (i = 0; i < core->num_parents; i++)
1910 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001911 return true;
1912
1913 return false;
1914}
1915EXPORT_SYMBOL_GPL(clk_has_parent);
1916
Jerome Brunet91baa9f2017-12-01 22:51:52 +01001917static int clk_core_set_parent_nolock(struct clk_core *core,
1918 struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001919{
1920 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001921 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001922 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001923
Jerome Brunet91baa9f2017-12-01 22:51:52 +01001924 lockdep_assert_held(&prepare_lock);
1925
Stephen Boydd6968fc2015-04-30 13:54:13 -07001926 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07001927 return 0;
1928
Stephen Boydd6968fc2015-04-30 13:54:13 -07001929 if (core->parent == parent)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01001930 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001931
Stephen Boydb61c43c2015-02-02 14:11:25 -08001932 /* verify ops for for multi-parent clks */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01001933 if (core->num_parents > 1 && !core->ops->set_parent)
1934 return -EPERM;
Stephen Boydb61c43c2015-02-02 14:11:25 -08001935
Ulf Hansson031dcc92013-04-02 23:09:38 +02001936 /* check that we are allowed to re-parent if the clock is in use */
Jerome Brunet91baa9f2017-12-01 22:51:52 +01001937 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
1938 return -EBUSY;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001939
1940 /* try finding the new parent index */
1941 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001942 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001943 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001944 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001945 __func__, parent->name, core->name);
Jerome Brunet91baa9f2017-12-01 22:51:52 +01001946 return p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001947 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09001948 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001949 }
1950
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001951 ret = clk_pm_runtime_get(core);
1952 if (ret)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01001953 return ret;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001954
Mike Turquetteb24764902012-03-15 23:11:19 -07001955 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001956 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001957
1958 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001959 if (ret & NOTIFY_STOP_MASK)
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001960 goto runtime_put;
Mike Turquetteb24764902012-03-15 23:11:19 -07001961
Ulf Hansson031dcc92013-04-02 23:09:38 +02001962 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001963 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001964
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001965 /* propagate rate an accuracy recalculation accordingly */
1966 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001967 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001968 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001969 __clk_recalc_rates(core, POST_RATE_CHANGE);
1970 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001971 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001972
Marek Szyprowski9a34b452017-08-21 10:04:59 +02001973runtime_put:
1974 clk_pm_runtime_put(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07001975
1976 return ret;
1977}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001978
1979/**
1980 * clk_set_parent - switch the parent of a mux clk
1981 * @clk: the mux clk whose input we are switching
1982 * @parent: the new input to clk
1983 *
1984 * Re-parent clk to use parent as its new input source. If clk is in
1985 * prepared state, the clk will get enabled for the duration of this call. If
1986 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1987 * that, the reparenting is glitchy in hardware, etc), use the
1988 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1989 *
1990 * After successfully changing clk's parent clk_set_parent will update the
1991 * clk topology, sysfs topology and propagate rate recalculation via
1992 * __clk_recalc_rates.
1993 *
1994 * Returns 0 on success, -EERROR otherwise.
1995 */
1996int clk_set_parent(struct clk *clk, struct clk *parent)
1997{
Jerome Brunet91baa9f2017-12-01 22:51:52 +01001998 int ret;
1999
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002000 if (!clk)
2001 return 0;
2002
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002003 clk_prepare_lock();
2004 ret = clk_core_set_parent_nolock(clk->core,
2005 parent ? parent->core : NULL);
2006 clk_prepare_unlock();
2007
2008 return ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002009}
Mike Turquetteb24764902012-03-15 23:11:19 -07002010EXPORT_SYMBOL_GPL(clk_set_parent);
2011
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002012static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2013{
2014 int ret = -EINVAL;
2015
2016 lockdep_assert_held(&prepare_lock);
2017
2018 if (!core)
2019 return 0;
2020
2021 trace_clk_set_phase(core, degrees);
2022
2023 if (core->ops->set_phase)
2024 ret = core->ops->set_phase(core->hw, degrees);
2025
2026 trace_clk_set_phase_complete(core, degrees);
2027
2028 return ret;
2029}
2030
Mike Turquetteb24764902012-03-15 23:11:19 -07002031/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002032 * clk_set_phase - adjust the phase shift of a clock signal
2033 * @clk: clock signal source
2034 * @degrees: number of degrees the signal is shifted
2035 *
2036 * Shifts the phase of a clock signal by the specified
2037 * degrees. Returns 0 on success, -EERROR otherwise.
2038 *
2039 * This function makes no distinction about the input or reference
2040 * signal that we adjust the clock signal phase against. For example
2041 * phase locked-loop clock signal generators we may shift phase with
2042 * respect to feedback clock signal input, but for other cases the
2043 * clock phase may be shifted with respect to some other, unspecified
2044 * signal.
2045 *
2046 * Additionally the concept of phase shift does not propagate through
2047 * the clock tree hierarchy, which sets it apart from clock rates and
2048 * clock accuracy. A parent clock phase attribute does not have an
2049 * impact on the phase attribute of a child clock.
2050 */
2051int clk_set_phase(struct clk *clk, int degrees)
2052{
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002053 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002054
2055 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002056 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002057
2058 /* sanity check degrees */
2059 degrees %= 360;
2060 if (degrees < 0)
2061 degrees += 360;
2062
2063 clk_prepare_lock();
Jerome Brunet9e4d04a2017-12-01 22:51:53 +01002064 ret = clk_core_set_phase_nolock(clk->core, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002065 clk_prepare_unlock();
2066
Mike Turquettee59c5372014-02-18 21:21:25 -08002067 return ret;
2068}
Maxime Ripard9767b042015-01-20 22:23:43 +01002069EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002070
Stephen Boydd6968fc2015-04-30 13:54:13 -07002071static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002072{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002073 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002074
2075 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07002076 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002077 clk_prepare_unlock();
2078
Mike Turquettee59c5372014-02-18 21:21:25 -08002079 return ret;
2080}
2081
2082/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002083 * clk_get_phase - return the phase shift of a clock signal
2084 * @clk: clock signal source
2085 *
2086 * Returns the phase shift of a clock node in degrees, otherwise returns
2087 * -EERROR.
2088 */
2089int clk_get_phase(struct clk *clk)
2090{
2091 if (!clk)
2092 return 0;
2093
2094 return clk_core_get_phase(clk->core);
2095}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002096EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002097
2098/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002099 * clk_is_match - check if two clk's point to the same hardware clock
2100 * @p: clk compared against q
2101 * @q: clk compared against p
2102 *
2103 * Returns true if the two struct clk pointers both point to the same hardware
2104 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2105 * share the same struct clk_core object.
2106 *
2107 * Returns false otherwise. Note that two NULL clks are treated as matching.
2108 */
2109bool clk_is_match(const struct clk *p, const struct clk *q)
2110{
2111 /* trivial case: identical struct clk's or both NULL */
2112 if (p == q)
2113 return true;
2114
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002115 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002116 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2117 if (p->core == q->core)
2118 return true;
2119
2120 return false;
2121}
2122EXPORT_SYMBOL_GPL(clk_is_match);
2123
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002124/*** debugfs support ***/
2125
2126#ifdef CONFIG_DEBUG_FS
2127#include <linux/debugfs.h>
2128
2129static struct dentry *rootdir;
2130static int inited = 0;
2131static DEFINE_MUTEX(clk_debug_lock);
2132static HLIST_HEAD(clk_debug_list);
2133
2134static struct hlist_head *all_lists[] = {
2135 &clk_root_list,
2136 &clk_orphan_list,
2137 NULL,
2138};
2139
2140static struct hlist_head *orphan_list[] = {
2141 &clk_orphan_list,
2142 NULL,
2143};
2144
2145static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2146 int level)
2147{
2148 if (!c)
2149 return;
2150
2151 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
2152 level * 3 + 1, "",
2153 30 - level * 3, c->name,
2154 c->enable_count, c->prepare_count, clk_core_get_rate(c),
2155 clk_core_get_accuracy(c), clk_core_get_phase(c));
2156}
2157
2158static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2159 int level)
2160{
2161 struct clk_core *child;
2162
2163 if (!c)
2164 return;
2165
2166 clk_summary_show_one(s, c, level);
2167
2168 hlist_for_each_entry(child, &c->children, child_node)
2169 clk_summary_show_subtree(s, child, level + 1);
2170}
2171
2172static int clk_summary_show(struct seq_file *s, void *data)
2173{
2174 struct clk_core *c;
2175 struct hlist_head **lists = (struct hlist_head **)s->private;
2176
2177 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
2178 seq_puts(s, "----------------------------------------------------------------------------------------\n");
2179
2180 clk_prepare_lock();
2181
2182 for (; *lists; lists++)
2183 hlist_for_each_entry(c, *lists, child_node)
2184 clk_summary_show_subtree(s, c, 0);
2185
2186 clk_prepare_unlock();
2187
2188 return 0;
2189}
2190
2191
2192static int clk_summary_open(struct inode *inode, struct file *file)
2193{
2194 return single_open(file, clk_summary_show, inode->i_private);
2195}
2196
2197static const struct file_operations clk_summary_fops = {
2198 .open = clk_summary_open,
2199 .read = seq_read,
2200 .llseek = seq_lseek,
2201 .release = single_release,
2202};
2203
2204static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2205{
2206 if (!c)
2207 return;
2208
Stefan Wahren7cb81132015-04-29 16:36:43 +00002209 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002210 seq_printf(s, "\"%s\": { ", c->name);
2211 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2212 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002213 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2214 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002215 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2216}
2217
2218static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2219{
2220 struct clk_core *child;
2221
2222 if (!c)
2223 return;
2224
2225 clk_dump_one(s, c, level);
2226
2227 hlist_for_each_entry(child, &c->children, child_node) {
Markus Elfring4d327582017-04-20 08:45:43 +02002228 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002229 clk_dump_subtree(s, child, level + 1);
2230 }
2231
Markus Elfring4d327582017-04-20 08:45:43 +02002232 seq_putc(s, '}');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002233}
2234
2235static int clk_dump(struct seq_file *s, void *data)
2236{
2237 struct clk_core *c;
2238 bool first_node = true;
2239 struct hlist_head **lists = (struct hlist_head **)s->private;
2240
Markus Elfring4d327582017-04-20 08:45:43 +02002241 seq_putc(s, '{');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002242 clk_prepare_lock();
2243
2244 for (; *lists; lists++) {
2245 hlist_for_each_entry(c, *lists, child_node) {
2246 if (!first_node)
Markus Elfring4d327582017-04-20 08:45:43 +02002247 seq_putc(s, ',');
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002248 first_node = false;
2249 clk_dump_subtree(s, c, 0);
2250 }
2251 }
2252
2253 clk_prepare_unlock();
2254
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002255 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002256 return 0;
2257}
2258
2259
2260static int clk_dump_open(struct inode *inode, struct file *file)
2261{
2262 return single_open(file, clk_dump, inode->i_private);
2263}
2264
2265static const struct file_operations clk_dump_fops = {
2266 .open = clk_dump_open,
2267 .read = seq_read,
2268 .llseek = seq_lseek,
2269 .release = single_release,
2270};
2271
Peter De Schrijver92031572017-03-21 15:20:31 +02002272static int possible_parents_dump(struct seq_file *s, void *data)
2273{
2274 struct clk_core *core = s->private;
2275 int i;
2276
2277 for (i = 0; i < core->num_parents - 1; i++)
2278 seq_printf(s, "%s ", core->parent_names[i]);
2279
2280 seq_printf(s, "%s\n", core->parent_names[i]);
2281
2282 return 0;
2283}
2284
2285static int possible_parents_open(struct inode *inode, struct file *file)
2286{
2287 return single_open(file, possible_parents_dump, inode->i_private);
2288}
2289
2290static const struct file_operations possible_parents_fops = {
2291 .open = possible_parents_open,
2292 .read = seq_read,
2293 .llseek = seq_lseek,
2294 .release = single_release,
2295};
2296
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002297static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2298{
2299 struct dentry *d;
2300 int ret = -ENOMEM;
2301
2302 if (!core || !pdentry) {
2303 ret = -EINVAL;
2304 goto out;
2305 }
2306
2307 d = debugfs_create_dir(core->name, pdentry);
2308 if (!d)
2309 goto out;
2310
2311 core->dentry = d;
2312
2313 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2314 (u32 *)&core->rate);
2315 if (!d)
2316 goto err_out;
2317
2318 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2319 (u32 *)&core->accuracy);
2320 if (!d)
2321 goto err_out;
2322
2323 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2324 (u32 *)&core->phase);
2325 if (!d)
2326 goto err_out;
2327
2328 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2329 (u32 *)&core->flags);
2330 if (!d)
2331 goto err_out;
2332
2333 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2334 (u32 *)&core->prepare_count);
2335 if (!d)
2336 goto err_out;
2337
2338 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2339 (u32 *)&core->enable_count);
2340 if (!d)
2341 goto err_out;
2342
2343 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2344 (u32 *)&core->notifier_count);
2345 if (!d)
2346 goto err_out;
2347
Peter De Schrijver92031572017-03-21 15:20:31 +02002348 if (core->num_parents > 1) {
2349 d = debugfs_create_file("clk_possible_parents", S_IRUGO,
2350 core->dentry, core, &possible_parents_fops);
2351 if (!d)
2352 goto err_out;
2353 }
2354
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002355 if (core->ops->debug_init) {
2356 ret = core->ops->debug_init(core->hw, core->dentry);
2357 if (ret)
2358 goto err_out;
2359 }
2360
2361 ret = 0;
2362 goto out;
2363
2364err_out:
2365 debugfs_remove_recursive(core->dentry);
2366 core->dentry = NULL;
2367out:
2368 return ret;
2369}
2370
2371/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002372 * clk_debug_register - add a clk node to the debugfs clk directory
2373 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002374 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002375 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2376 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002377 * will be created lazily by clk_debug_init as part of a late_initcall.
2378 */
2379static int clk_debug_register(struct clk_core *core)
2380{
2381 int ret = 0;
2382
2383 mutex_lock(&clk_debug_lock);
2384 hlist_add_head(&core->debug_node, &clk_debug_list);
2385
2386 if (!inited)
2387 goto unlock;
2388
2389 ret = clk_debug_create_one(core, rootdir);
2390unlock:
2391 mutex_unlock(&clk_debug_lock);
2392
2393 return ret;
2394}
2395
2396 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002397 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2398 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002399 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002400 * Dynamically removes a clk and all its child nodes from the
2401 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08002402 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002403 */
2404static void clk_debug_unregister(struct clk_core *core)
2405{
2406 mutex_lock(&clk_debug_lock);
2407 hlist_del_init(&core->debug_node);
2408 debugfs_remove_recursive(core->dentry);
2409 core->dentry = NULL;
2410 mutex_unlock(&clk_debug_lock);
2411}
2412
2413struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2414 void *data, const struct file_operations *fops)
2415{
2416 struct dentry *d = NULL;
2417
2418 if (hw->core->dentry)
2419 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2420 fops);
2421
2422 return d;
2423}
2424EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2425
2426/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002427 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002428 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002429 * clks are often initialized very early during boot before memory can be
2430 * dynamically allocated and well before debugfs is setup. This function
2431 * populates the debugfs clk directory once at boot-time when we know that
2432 * debugfs is setup. It should only be called once at boot-time, all other clks
2433 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002434 */
2435static int __init clk_debug_init(void)
2436{
2437 struct clk_core *core;
2438 struct dentry *d;
2439
2440 rootdir = debugfs_create_dir("clk", NULL);
2441
2442 if (!rootdir)
2443 return -ENOMEM;
2444
2445 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2446 &clk_summary_fops);
2447 if (!d)
2448 return -ENOMEM;
2449
2450 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2451 &clk_dump_fops);
2452 if (!d)
2453 return -ENOMEM;
2454
2455 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2456 &orphan_list, &clk_summary_fops);
2457 if (!d)
2458 return -ENOMEM;
2459
2460 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2461 &orphan_list, &clk_dump_fops);
2462 if (!d)
2463 return -ENOMEM;
2464
2465 mutex_lock(&clk_debug_lock);
2466 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2467 clk_debug_create_one(core, rootdir);
2468
2469 inited = 1;
2470 mutex_unlock(&clk_debug_lock);
2471
2472 return 0;
2473}
2474late_initcall(clk_debug_init);
2475#else
2476static inline int clk_debug_register(struct clk_core *core) { return 0; }
2477static inline void clk_debug_reparent(struct clk_core *core,
2478 struct clk_core *new_parent)
2479{
2480}
2481static inline void clk_debug_unregister(struct clk_core *core)
2482{
2483}
2484#endif
2485
Michael Turquette3d3801e2015-02-25 09:11:01 -08002486/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002487 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002488 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07002489 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002490 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002491 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002492 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002493static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002494{
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002495 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002496 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002497 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002498 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002499
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002500 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07002501 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002502
Mike Turquetteeab89f62013-03-28 13:59:01 -07002503 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002504
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002505 ret = clk_pm_runtime_get(core);
2506 if (ret)
2507 goto unlock;
2508
Mike Turquetteb24764902012-03-15 23:11:19 -07002509 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002510 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002511 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002512 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002513 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002514 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002515 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002516
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002517 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002518 if (core->ops->set_rate &&
2519 !((core->ops->round_rate || core->ops->determine_rate) &&
2520 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002521 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2522 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002523 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002524 goto out;
2525 }
2526
Stephen Boydd6968fc2015-04-30 13:54:13 -07002527 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002528 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2529 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002530 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002531 goto out;
2532 }
2533
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09002534 if (core->num_parents > 1 && !core->ops->get_parent) {
2535 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2536 __func__, core->name);
2537 ret = -EINVAL;
2538 goto out;
2539 }
2540
Stephen Boydd6968fc2015-04-30 13:54:13 -07002541 if (core->ops->set_rate_and_parent &&
2542 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09002543 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002544 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002545 ret = -EINVAL;
2546 goto out;
2547 }
2548
Mike Turquetteb24764902012-03-15 23:11:19 -07002549 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002550 for (i = 0; i < core->num_parents; i++)
2551 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002552 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002553 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002554
Stephen Boydd6968fc2015-04-30 13:54:13 -07002555 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002556
2557 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08002558 * Populate core->parent if parent has already been clk_core_init'd. If
2559 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002560 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07002561 * clk list.
2562 *
2563 * Every time a new clk is clk_init'd then we walk the list of orphan
2564 * clocks and re-parent any that are children of the clock currently
2565 * being clk_init'd.
2566 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02002567 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002568 hlist_add_head(&core->child_node,
2569 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002570 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08002571 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002572 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002573 core->orphan = false;
2574 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002575 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02002576 core->orphan = true;
2577 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002578
2579 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002580 * Set clk's accuracy. The preferred method is to use
2581 * .recalc_accuracy. For simple clocks and lazy developers the default
2582 * fallback is to use the parent's accuracy. If a clock doesn't have a
2583 * parent (or is orphaned) then accuracy is set to zero (perfect
2584 * clock).
2585 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002586 if (core->ops->recalc_accuracy)
2587 core->accuracy = core->ops->recalc_accuracy(core->hw,
2588 __clk_get_accuracy(core->parent));
2589 else if (core->parent)
2590 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002591 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002592 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002593
2594 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002595 * Set clk's phase.
2596 * Since a phase is by definition relative to its parent, just
2597 * query the current clock phase, or just assume it's in phase.
2598 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002599 if (core->ops->get_phase)
2600 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002601 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002602 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002603
2604 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002605 * Set clk's rate. The preferred method is to use .recalc_rate. For
2606 * simple clocks and lazy developers the default fallback is to use the
2607 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2608 * then rate is set to zero.
2609 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002610 if (core->ops->recalc_rate)
2611 rate = core->ops->recalc_rate(core->hw,
2612 clk_core_get_rate_nolock(core->parent));
2613 else if (core->parent)
2614 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002615 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002616 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002617 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002618
2619 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002620 * walk the list of orphan clocks and reparent any that newly finds a
2621 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07002622 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002623 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002624 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002625
Michael Turquette904e6ea2016-07-08 16:32:10 -07002626 /*
2627 * we could call __clk_set_parent, but that would result in a
2628 * redundant call to the .set_rate op, if it exists
2629 */
2630 if (parent) {
2631 __clk_set_parent_before(orphan, parent);
2632 __clk_set_parent_after(orphan, parent, NULL);
2633 __clk_recalc_accuracies(orphan);
2634 __clk_recalc_rates(orphan, 0);
2635 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09002636 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002637
2638 /*
2639 * optional platform-specific magic
2640 *
2641 * The .init callback is not used by any of the basic clock types, but
2642 * exists for weird hardware that must perform initialization magic.
2643 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002644 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002645 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002646 if (core->ops->init)
2647 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002648
Lee Jones32b9b102016-02-11 13:19:09 -08002649 if (core->flags & CLK_IS_CRITICAL) {
Maxime Ripardef56b792016-05-13 10:00:31 +02002650 unsigned long flags;
2651
Lee Jones32b9b102016-02-11 13:19:09 -08002652 clk_core_prepare(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02002653
2654 flags = clk_enable_lock();
Lee Jones32b9b102016-02-11 13:19:09 -08002655 clk_core_enable(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02002656 clk_enable_unlock(flags);
Lee Jones32b9b102016-02-11 13:19:09 -08002657 }
2658
Stephen Boydd6968fc2015-04-30 13:54:13 -07002659 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002660out:
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002661 clk_pm_runtime_put(core);
2662unlock:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002663 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002664
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002665 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002666 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002667
Mike Turquetted1302a32012-03-29 14:30:40 -07002668 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002669}
2670
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002671struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2672 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002673{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002674 struct clk *clk;
2675
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002676 /* This is to allow this function to be chained to others */
Masahiro Yamadac1de1352015-11-20 14:38:49 +09002677 if (IS_ERR_OR_NULL(hw))
Masahiro Yamada8a231332016-07-19 16:28:47 +09002678 return ERR_CAST(hw);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002679
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002680 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2681 if (!clk)
2682 return ERR_PTR(-ENOMEM);
2683
2684 clk->core = hw->core;
2685 clk->dev_id = dev_id;
Leonard Crestez253160a2017-02-20 15:20:56 +02002686 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002687 clk->max_rate = ULONG_MAX;
2688
2689 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002690 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002691 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002692
2693 return clk;
2694}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002695
Stephen Boyd73e0e492015-02-06 11:42:43 -08002696void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002697{
2698 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002699 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002700 clk_prepare_unlock();
2701
Leonard Crestez253160a2017-02-20 15:20:56 +02002702 kfree_const(clk->con_id);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002703 kfree(clk);
2704}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002705
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002706/**
2707 * clk_register - allocate a new clock, register it and return an opaque cookie
2708 * @dev: device that is registering this clock
2709 * @hw: link to hardware-specific clock data
2710 *
2711 * clk_register is the primary interface for populating the clock tree with new
2712 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05302713 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002714 * rest of the clock API. In the event of an error clk_register will return an
2715 * error code; drivers must test for an error code after calling clk_register.
2716 */
2717struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002718{
Mike Turquetted1302a32012-03-29 14:30:40 -07002719 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002720 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002721
Stephen Boydd6968fc2015-04-30 13:54:13 -07002722 core = kzalloc(sizeof(*core), GFP_KERNEL);
2723 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002724 ret = -ENOMEM;
2725 goto fail_out;
2726 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002727
Stephen Boydd6968fc2015-04-30 13:54:13 -07002728 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2729 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002730 ret = -ENOMEM;
2731 goto fail_name;
2732 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002733 core->ops = hw->init->ops;
Marek Szyprowski9a34b452017-08-21 10:04:59 +02002734 if (dev && pm_runtime_enabled(dev))
2735 core->dev = dev;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002736 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002737 core->owner = dev->driver->owner;
2738 core->hw = hw;
2739 core->flags = hw->init->flags;
2740 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07002741 core->min_rate = 0;
2742 core->max_rate = ULONG_MAX;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002743 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002744
Mike Turquetted1302a32012-03-29 14:30:40 -07002745 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002746 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002747 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002748
Stephen Boydd6968fc2015-04-30 13:54:13 -07002749 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002750 ret = -ENOMEM;
2751 goto fail_parent_names;
2752 }
2753
2754
2755 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002756 for (i = 0; i < core->num_parents; i++) {
2757 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002758 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002759 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002760 ret = -ENOMEM;
2761 goto fail_parent_names_copy;
2762 }
2763 }
2764
Masahiro Yamada176d1162015-12-28 19:23:00 +09002765 /* avoid unnecessary string look-ups of clk_core's possible parents. */
2766 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
2767 GFP_KERNEL);
2768 if (!core->parents) {
2769 ret = -ENOMEM;
2770 goto fail_parents;
2771 };
2772
Stephen Boydd6968fc2015-04-30 13:54:13 -07002773 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002774
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002775 hw->clk = __clk_create_clk(hw, NULL, NULL);
2776 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002777 ret = PTR_ERR(hw->clk);
Masahiro Yamada176d1162015-12-28 19:23:00 +09002778 goto fail_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002779 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002780
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002781 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002782 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002783 return hw->clk;
2784
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002785 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002786 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002787
Masahiro Yamada176d1162015-12-28 19:23:00 +09002788fail_parents:
2789 kfree(core->parents);
Mike Turquetted1302a32012-03-29 14:30:40 -07002790fail_parent_names_copy:
2791 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002792 kfree_const(core->parent_names[i]);
2793 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07002794fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002795 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002796fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002797 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002798fail_out:
2799 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002800}
2801EXPORT_SYMBOL_GPL(clk_register);
2802
Stephen Boyd41438042016-02-05 17:02:52 -08002803/**
2804 * clk_hw_register - register a clk_hw and return an error code
2805 * @dev: device that is registering this clock
2806 * @hw: link to hardware-specific clock data
2807 *
2808 * clk_hw_register is the primary interface for populating the clock tree with
2809 * new clock nodes. It returns an integer equal to zero indicating success or
2810 * less than zero indicating failure. Drivers must test for an error code after
2811 * calling clk_hw_register().
2812 */
2813int clk_hw_register(struct device *dev, struct clk_hw *hw)
2814{
2815 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
2816}
2817EXPORT_SYMBOL_GPL(clk_hw_register);
2818
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002819/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002820static void __clk_release(struct kref *ref)
2821{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002822 struct clk_core *core = container_of(ref, struct clk_core, ref);
2823 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002824
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002825 lockdep_assert_held(&prepare_lock);
2826
Stephen Boydd6968fc2015-04-30 13:54:13 -07002827 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002828 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002829 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002830
Stephen Boydd6968fc2015-04-30 13:54:13 -07002831 kfree(core->parent_names);
2832 kfree_const(core->name);
2833 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002834}
2835
2836/*
2837 * Empty clk_ops for unregistered clocks. These are used temporarily
2838 * after clk_unregister() was called on a clock and until last clock
2839 * consumer calls clk_put() and the struct clk object is freed.
2840 */
2841static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2842{
2843 return -ENXIO;
2844}
2845
2846static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2847{
2848 WARN_ON_ONCE(1);
2849}
2850
2851static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2852 unsigned long parent_rate)
2853{
2854 return -ENXIO;
2855}
2856
2857static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2858{
2859 return -ENXIO;
2860}
2861
2862static const struct clk_ops clk_nodrv_ops = {
2863 .enable = clk_nodrv_prepare_enable,
2864 .disable = clk_nodrv_disable_unprepare,
2865 .prepare = clk_nodrv_prepare_enable,
2866 .unprepare = clk_nodrv_disable_unprepare,
2867 .set_rate = clk_nodrv_set_rate,
2868 .set_parent = clk_nodrv_set_parent,
2869};
2870
Mark Brown1df5c932012-04-18 09:07:12 +01002871/**
2872 * clk_unregister - unregister a currently registered clock
2873 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002874 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002875void clk_unregister(struct clk *clk)
2876{
2877 unsigned long flags;
2878
Stephen Boyd6314b672014-09-04 23:37:49 -07002879 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2880 return;
2881
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002882 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002883
2884 clk_prepare_lock();
2885
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002886 if (clk->core->ops == &clk_nodrv_ops) {
2887 pr_err("%s: unregistered clock: %s\n", __func__,
2888 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05002889 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002890 }
2891 /*
2892 * Assign empty clock ops for consumers that might still hold
2893 * a reference to this clock.
2894 */
2895 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002896 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002897 clk_enable_unlock(flags);
2898
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002899 if (!hlist_empty(&clk->core->children)) {
2900 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002901 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002902
2903 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002904 hlist_for_each_entry_safe(child, t, &clk->core->children,
2905 child_node)
Jerome Brunet91baa9f2017-12-01 22:51:52 +01002906 clk_core_set_parent_nolock(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002907 }
2908
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002909 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002910
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002911 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002912 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002913 __func__, clk->core->name);
2914 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05002915unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002916 clk_prepare_unlock();
2917}
Mark Brown1df5c932012-04-18 09:07:12 +01002918EXPORT_SYMBOL_GPL(clk_unregister);
2919
Stephen Boyd41438042016-02-05 17:02:52 -08002920/**
2921 * clk_hw_unregister - unregister a currently registered clk_hw
2922 * @hw: hardware-specific clock data to unregister
2923 */
2924void clk_hw_unregister(struct clk_hw *hw)
2925{
2926 clk_unregister(hw->clk);
2927}
2928EXPORT_SYMBOL_GPL(clk_hw_unregister);
2929
Stephen Boyd46c87732012-09-24 13:38:04 -07002930static void devm_clk_release(struct device *dev, void *res)
2931{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002932 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002933}
2934
Stephen Boyd41438042016-02-05 17:02:52 -08002935static void devm_clk_hw_release(struct device *dev, void *res)
2936{
2937 clk_hw_unregister(*(struct clk_hw **)res);
2938}
2939
Stephen Boyd46c87732012-09-24 13:38:04 -07002940/**
2941 * devm_clk_register - resource managed clk_register()
2942 * @dev: device that is registering this clock
2943 * @hw: link to hardware-specific clock data
2944 *
2945 * Managed clk_register(). Clocks returned from this function are
2946 * automatically clk_unregister()ed on driver detach. See clk_register() for
2947 * more information.
2948 */
2949struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2950{
2951 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002952 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002953
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002954 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2955 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002956 return ERR_PTR(-ENOMEM);
2957
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002958 clk = clk_register(dev, hw);
2959 if (!IS_ERR(clk)) {
2960 *clkp = clk;
2961 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002962 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002963 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002964 }
2965
2966 return clk;
2967}
2968EXPORT_SYMBOL_GPL(devm_clk_register);
2969
Stephen Boyd41438042016-02-05 17:02:52 -08002970/**
2971 * devm_clk_hw_register - resource managed clk_hw_register()
2972 * @dev: device that is registering this clock
2973 * @hw: link to hardware-specific clock data
2974 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09002975 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08002976 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
2977 * for more information.
2978 */
2979int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
2980{
2981 struct clk_hw **hwp;
2982 int ret;
2983
2984 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
2985 if (!hwp)
2986 return -ENOMEM;
2987
2988 ret = clk_hw_register(dev, hw);
2989 if (!ret) {
2990 *hwp = hw;
2991 devres_add(dev, hwp);
2992 } else {
2993 devres_free(hwp);
2994 }
2995
2996 return ret;
2997}
2998EXPORT_SYMBOL_GPL(devm_clk_hw_register);
2999
Stephen Boyd46c87732012-09-24 13:38:04 -07003000static int devm_clk_match(struct device *dev, void *res, void *data)
3001{
3002 struct clk *c = res;
3003 if (WARN_ON(!c))
3004 return 0;
3005 return c == data;
3006}
3007
Stephen Boyd41438042016-02-05 17:02:52 -08003008static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3009{
3010 struct clk_hw *hw = res;
3011
3012 if (WARN_ON(!hw))
3013 return 0;
3014 return hw == data;
3015}
3016
Stephen Boyd46c87732012-09-24 13:38:04 -07003017/**
3018 * devm_clk_unregister - resource managed clk_unregister()
3019 * @clk: clock to unregister
3020 *
3021 * Deallocate a clock allocated with devm_clk_register(). Normally
3022 * this function will not need to be called and the resource management
3023 * code will ensure that the resource is freed.
3024 */
3025void devm_clk_unregister(struct device *dev, struct clk *clk)
3026{
3027 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3028}
3029EXPORT_SYMBOL_GPL(devm_clk_unregister);
3030
Stephen Boyd41438042016-02-05 17:02:52 -08003031/**
3032 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3033 * @dev: device that is unregistering the hardware-specific clock data
3034 * @hw: link to hardware-specific clock data
3035 *
3036 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3037 * this function will not need to be called and the resource management
3038 * code will ensure that the resource is freed.
3039 */
3040void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3041{
3042 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3043 hw));
3044}
3045EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3046
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003047/*
3048 * clkdev helpers
3049 */
3050int __clk_get(struct clk *clk)
3051{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003052 struct clk_core *core = !clk ? NULL : clk->core;
3053
3054 if (core) {
3055 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003056 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003057
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003058 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003059 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003060 return 1;
3061}
3062
3063void __clk_put(struct clk *clk)
3064{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003065 struct module *owner;
3066
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003067 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003068 return;
3069
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003070 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003071
Stephen Boyd50595f82015-02-06 11:42:44 -08003072 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003073 if (clk->min_rate > clk->core->req_rate ||
3074 clk->max_rate < clk->core->req_rate)
3075 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3076
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003077 owner = clk->core->owner;
3078 kref_put(&clk->core->ref, __clk_release);
3079
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003080 clk_prepare_unlock();
3081
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003082 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003083
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003084 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003085}
3086
Mike Turquetteb24764902012-03-15 23:11:19 -07003087/*** clk rate change notifiers ***/
3088
3089/**
3090 * clk_notifier_register - add a clk rate change notifier
3091 * @clk: struct clk * to watch
3092 * @nb: struct notifier_block * with callback info
3093 *
3094 * Request notification when clk's rate changes. This uses an SRCU
3095 * notifier because we want it to block and notifier unregistrations are
3096 * uncommon. The callbacks associated with the notifier must not
3097 * re-enter into the clk framework by calling any top-level clk APIs;
3098 * this will cause a nested prepare_lock mutex.
3099 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003100 * In all notification cases (pre, post and abort rate change) the original
3101 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3102 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003103 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003104 * clk_notifier_register() must be called from non-atomic context.
3105 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3106 * allocation failure; otherwise, passes along the return value of
3107 * srcu_notifier_chain_register().
3108 */
3109int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3110{
3111 struct clk_notifier *cn;
3112 int ret = -ENOMEM;
3113
3114 if (!clk || !nb)
3115 return -EINVAL;
3116
Mike Turquetteeab89f62013-03-28 13:59:01 -07003117 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003118
3119 /* search the list of notifiers for this clk */
3120 list_for_each_entry(cn, &clk_notifier_list, node)
3121 if (cn->clk == clk)
3122 break;
3123
3124 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3125 if (cn->clk != clk) {
Markus Elfring1808a322017-04-20 09:30:52 +02003126 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003127 if (!cn)
3128 goto out;
3129
3130 cn->clk = clk;
3131 srcu_init_notifier_head(&cn->notifier_head);
3132
3133 list_add(&cn->node, &clk_notifier_list);
3134 }
3135
3136 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3137
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003138 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003139
3140out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003141 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003142
3143 return ret;
3144}
3145EXPORT_SYMBOL_GPL(clk_notifier_register);
3146
3147/**
3148 * clk_notifier_unregister - remove a clk rate change notifier
3149 * @clk: struct clk *
3150 * @nb: struct notifier_block * with callback info
3151 *
3152 * Request no further notification for changes to 'clk' and frees memory
3153 * allocated in clk_notifier_register.
3154 *
3155 * Returns -EINVAL if called with null arguments; otherwise, passes
3156 * along the return value of srcu_notifier_chain_unregister().
3157 */
3158int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3159{
3160 struct clk_notifier *cn = NULL;
3161 int ret = -EINVAL;
3162
3163 if (!clk || !nb)
3164 return -EINVAL;
3165
Mike Turquetteeab89f62013-03-28 13:59:01 -07003166 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003167
3168 list_for_each_entry(cn, &clk_notifier_list, node)
3169 if (cn->clk == clk)
3170 break;
3171
3172 if (cn->clk == clk) {
3173 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3174
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003175 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07003176
3177 /* XXX the notifier code should handle this better */
3178 if (!cn->notifier_head.head) {
3179 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08003180 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07003181 kfree(cn);
3182 }
3183
3184 } else {
3185 ret = -ENOENT;
3186 }
3187
Mike Turquetteeab89f62013-03-28 13:59:01 -07003188 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003189
3190 return ret;
3191}
3192EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05003193
3194#ifdef CONFIG_OF
3195/**
3196 * struct of_clk_provider - Clock provider registration structure
3197 * @link: Entry in global list of clock providers
3198 * @node: Pointer to device tree node of clock provider
3199 * @get: Get clock callback. Returns NULL or a struct clk for the
3200 * given clock specifier
3201 * @data: context pointer to be passed into @get callback
3202 */
3203struct of_clk_provider {
3204 struct list_head link;
3205
3206 struct device_node *node;
3207 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003208 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05003209 void *data;
3210};
3211
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303212static const struct of_device_id __clk_of_table_sentinel
3213 __used __section(__clk_of_table_end);
3214
Grant Likely766e6a42012-04-09 14:50:06 -05003215static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003216static DEFINE_MUTEX(of_clk_mutex);
3217
Grant Likely766e6a42012-04-09 14:50:06 -05003218struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3219 void *data)
3220{
3221 return data;
3222}
3223EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3224
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003225struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3226{
3227 return data;
3228}
3229EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3230
Shawn Guo494bfec2012-08-22 21:36:27 +08003231struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3232{
3233 struct clk_onecell_data *clk_data = data;
3234 unsigned int idx = clkspec->args[0];
3235
3236 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02003237 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08003238 return ERR_PTR(-EINVAL);
3239 }
3240
3241 return clk_data->clks[idx];
3242}
3243EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3244
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003245struct clk_hw *
3246of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3247{
3248 struct clk_hw_onecell_data *hw_data = data;
3249 unsigned int idx = clkspec->args[0];
3250
3251 if (idx >= hw_data->num) {
3252 pr_err("%s: invalid index %u\n", __func__, idx);
3253 return ERR_PTR(-EINVAL);
3254 }
3255
3256 return hw_data->hws[idx];
3257}
3258EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3259
Grant Likely766e6a42012-04-09 14:50:06 -05003260/**
3261 * of_clk_add_provider() - Register a clock provider for a node
3262 * @np: Device node pointer associated with clock provider
3263 * @clk_src_get: callback for decoding clock
3264 * @data: context pointer for @clk_src_get callback.
3265 */
3266int of_clk_add_provider(struct device_node *np,
3267 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3268 void *data),
3269 void *data)
3270{
3271 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003272 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003273
Markus Elfring1808a322017-04-20 09:30:52 +02003274 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Grant Likely766e6a42012-04-09 14:50:06 -05003275 if (!cp)
3276 return -ENOMEM;
3277
3278 cp->node = of_node_get(np);
3279 cp->data = data;
3280 cp->get = clk_src_get;
3281
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003282 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003283 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003284 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003285 pr_debug("Added clock from %pOF\n", np);
Grant Likely766e6a42012-04-09 14:50:06 -05003286
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003287 ret = of_clk_set_defaults(np, true);
3288 if (ret < 0)
3289 of_clk_del_provider(np);
3290
3291 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003292}
3293EXPORT_SYMBOL_GPL(of_clk_add_provider);
3294
3295/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003296 * of_clk_add_hw_provider() - Register a clock provider for a node
3297 * @np: Device node pointer associated with clock provider
3298 * @get: callback for decoding clk_hw
3299 * @data: context pointer for @get callback.
3300 */
3301int of_clk_add_hw_provider(struct device_node *np,
3302 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3303 void *data),
3304 void *data)
3305{
3306 struct of_clk_provider *cp;
3307 int ret;
3308
3309 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3310 if (!cp)
3311 return -ENOMEM;
3312
3313 cp->node = of_node_get(np);
3314 cp->data = data;
3315 cp->get_hw = get;
3316
3317 mutex_lock(&of_clk_mutex);
3318 list_add(&cp->link, &of_clk_providers);
3319 mutex_unlock(&of_clk_mutex);
Rob Herring16673932017-07-18 16:42:52 -05003320 pr_debug("Added clk_hw provider from %pOF\n", np);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003321
3322 ret = of_clk_set_defaults(np, true);
3323 if (ret < 0)
3324 of_clk_del_provider(np);
3325
3326 return ret;
3327}
3328EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3329
Stephen Boydaa795c42017-09-01 16:16:40 -07003330static void devm_of_clk_release_provider(struct device *dev, void *res)
3331{
3332 of_clk_del_provider(*(struct device_node **)res);
3333}
3334
3335int devm_of_clk_add_hw_provider(struct device *dev,
3336 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3337 void *data),
3338 void *data)
3339{
3340 struct device_node **ptr, *np;
3341 int ret;
3342
3343 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3344 GFP_KERNEL);
3345 if (!ptr)
3346 return -ENOMEM;
3347
3348 np = dev->of_node;
3349 ret = of_clk_add_hw_provider(np, get, data);
3350 if (!ret) {
3351 *ptr = np;
3352 devres_add(dev, ptr);
3353 } else {
3354 devres_free(ptr);
3355 }
3356
3357 return ret;
3358}
3359EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3360
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003361/**
Grant Likely766e6a42012-04-09 14:50:06 -05003362 * of_clk_del_provider() - Remove a previously registered clock provider
3363 * @np: Device node pointer associated with clock provider
3364 */
3365void of_clk_del_provider(struct device_node *np)
3366{
3367 struct of_clk_provider *cp;
3368
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003369 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003370 list_for_each_entry(cp, &of_clk_providers, link) {
3371 if (cp->node == np) {
3372 list_del(&cp->link);
3373 of_node_put(cp->node);
3374 kfree(cp);
3375 break;
3376 }
3377 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003378 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003379}
3380EXPORT_SYMBOL_GPL(of_clk_del_provider);
3381
Stephen Boydaa795c42017-09-01 16:16:40 -07003382static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3383{
3384 struct device_node **np = res;
3385
3386 if (WARN_ON(!np || !*np))
3387 return 0;
3388
3389 return *np == data;
3390}
3391
3392void devm_of_clk_del_provider(struct device *dev)
3393{
3394 int ret;
3395
3396 ret = devres_release(dev, devm_of_clk_release_provider,
3397 devm_clk_provider_match, dev->of_node);
3398
3399 WARN_ON(ret);
3400}
3401EXPORT_SYMBOL(devm_of_clk_del_provider);
3402
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003403static struct clk_hw *
3404__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3405 struct of_phandle_args *clkspec)
3406{
3407 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003408
Stephen Boyd74002fc2016-08-25 13:35:36 -07003409 if (provider->get_hw)
3410 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003411
Stephen Boyd74002fc2016-08-25 13:35:36 -07003412 clk = provider->get(clkspec, provider->data);
3413 if (IS_ERR(clk))
3414 return ERR_CAST(clk);
3415 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003416}
3417
Stephen Boyd73e0e492015-02-06 11:42:43 -08003418struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3419 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05003420{
3421 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01003422 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Stephen Boydf155d152016-08-15 14:32:23 -07003423 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -05003424
Stephen Boyd306c3422015-02-05 15:39:11 -08003425 if (!clkspec)
3426 return ERR_PTR(-EINVAL);
3427
Grant Likely766e6a42012-04-09 14:50:06 -05003428 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08003429 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003430 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07003431 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003432 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003433 clk = __clk_create_clk(hw, dev_id, con_id);
Stephen Boydf155d152016-08-15 14:32:23 -07003434 }
Stephen Boyd73e0e492015-02-06 11:42:43 -08003435
Stephen Boydf155d152016-08-15 14:32:23 -07003436 if (!IS_ERR(clk)) {
3437 if (!__clk_get(clk)) {
Stephen Boyd73e0e492015-02-06 11:42:43 -08003438 __clk_free_clk(clk);
3439 clk = ERR_PTR(-ENOENT);
3440 }
3441
Grant Likely766e6a42012-04-09 14:50:06 -05003442 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08003443 }
Grant Likely766e6a42012-04-09 14:50:06 -05003444 }
Stephen Boyd306c3422015-02-05 15:39:11 -08003445 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003446
3447 return clk;
3448}
3449
Stephen Boyd306c3422015-02-05 15:39:11 -08003450/**
3451 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3452 * @clkspec: pointer to a clock specifier data structure
3453 *
3454 * This function looks up a struct clk from the registered list of clock
3455 * providers, an input is a clock specifier data structure as returned
3456 * from the of_parse_phandle_with_args() function call.
3457 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003458struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3459{
Stephen Boyd306c3422015-02-05 15:39:11 -08003460 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05003461}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06003462EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05003463
Stephen Boyd929e7f32016-02-19 15:52:32 -08003464/**
3465 * of_clk_get_parent_count() - Count the number of clocks a device node has
3466 * @np: device node to count
3467 *
3468 * Returns: The number of clocks that are possible parents of this node
3469 */
3470unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07003471{
Stephen Boyd929e7f32016-02-19 15:52:32 -08003472 int count;
3473
3474 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3475 if (count < 0)
3476 return 0;
3477
3478 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07003479}
3480EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3481
Grant Likely766e6a42012-04-09 14:50:06 -05003482const char *of_clk_get_parent_name(struct device_node *np, int index)
3483{
3484 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003485 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003486 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003487 const __be32 *vp;
3488 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003489 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003490 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003491 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05003492
Grant Likely766e6a42012-04-09 14:50:06 -05003493 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3494 &clkspec);
3495 if (rc)
3496 return NULL;
3497
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003498 index = clkspec.args_count ? clkspec.args[0] : 0;
3499 count = 0;
3500
3501 /* if there is an indices property, use it to transfer the index
3502 * specified into an array offset for the clock-output-names property.
3503 */
3504 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3505 if (index == pv) {
3506 index = count;
3507 break;
3508 }
3509 count++;
3510 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09003511 /* We went off the end of 'clock-indices' without finding it */
3512 if (prop && !vp)
3513 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003514
Grant Likely766e6a42012-04-09 14:50:06 -05003515 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003516 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07003517 &clk_name) < 0) {
3518 /*
3519 * Best effort to get the name if the clock has been
3520 * registered with the framework. If the clock isn't
3521 * registered, we return the node name as the name of
3522 * the clock as long as #clock-cells = 0.
3523 */
3524 clk = of_clk_get_from_provider(&clkspec);
3525 if (IS_ERR(clk)) {
3526 if (clkspec.args_count == 0)
3527 clk_name = clkspec.np->name;
3528 else
3529 clk_name = NULL;
3530 } else {
3531 clk_name = __clk_get_name(clk);
3532 clk_put(clk);
3533 }
3534 }
3535
Grant Likely766e6a42012-04-09 14:50:06 -05003536
3537 of_node_put(clkspec.np);
3538 return clk_name;
3539}
3540EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3541
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05003542/**
3543 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3544 * number of parents
3545 * @np: Device node pointer associated with clock provider
3546 * @parents: pointer to char array that hold the parents' names
3547 * @size: size of the @parents array
3548 *
3549 * Return: number of parents for the clock node.
3550 */
3551int of_clk_parent_fill(struct device_node *np, const char **parents,
3552 unsigned int size)
3553{
3554 unsigned int i = 0;
3555
3556 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3557 i++;
3558
3559 return i;
3560}
3561EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3562
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003563struct clock_provider {
3564 of_clk_init_cb_t clk_init_cb;
3565 struct device_node *np;
3566 struct list_head node;
3567};
3568
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003569/*
3570 * This function looks for a parent clock. If there is one, then it
3571 * checks that the provider for this parent clock was initialized, in
3572 * this case the parent clock will be ready.
3573 */
3574static int parent_ready(struct device_node *np)
3575{
3576 int i = 0;
3577
3578 while (true) {
3579 struct clk *clk = of_clk_get(np, i);
3580
3581 /* this parent is ready we can check the next one */
3582 if (!IS_ERR(clk)) {
3583 clk_put(clk);
3584 i++;
3585 continue;
3586 }
3587
3588 /* at least one parent is not ready, we exit now */
3589 if (PTR_ERR(clk) == -EPROBE_DEFER)
3590 return 0;
3591
3592 /*
3593 * Here we make assumption that the device tree is
3594 * written correctly. So an error means that there is
3595 * no more parent. As we didn't exit yet, then the
3596 * previous parent are ready. If there is no clock
3597 * parent, no need to wait for them, then we can
3598 * consider their absence as being ready
3599 */
3600 return 1;
3601 }
3602}
3603
Grant Likely766e6a42012-04-09 14:50:06 -05003604/**
Lee Jonesd56f8992016-02-11 13:19:11 -08003605 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3606 * @np: Device node pointer associated with clock provider
3607 * @index: clock index
3608 * @flags: pointer to clk_core->flags
3609 *
3610 * Detects if the clock-critical property exists and, if so, sets the
3611 * corresponding CLK_IS_CRITICAL flag.
3612 *
3613 * Do not use this function. It exists only for legacy Device Tree
3614 * bindings, such as the one-clock-per-node style that are outdated.
3615 * Those bindings typically put all clock data into .dts and the Linux
3616 * driver has no clock data, thus making it impossible to set this flag
3617 * correctly from the driver. Only those drivers may call
3618 * of_clk_detect_critical from their setup functions.
3619 *
3620 * Return: error code or zero on success
3621 */
3622int of_clk_detect_critical(struct device_node *np,
3623 int index, unsigned long *flags)
3624{
3625 struct property *prop;
3626 const __be32 *cur;
3627 uint32_t idx;
3628
3629 if (!np || !flags)
3630 return -EINVAL;
3631
3632 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
3633 if (index == idx)
3634 *flags |= CLK_IS_CRITICAL;
3635
3636 return 0;
3637}
3638
3639/**
Grant Likely766e6a42012-04-09 14:50:06 -05003640 * of_clk_init() - Scan and init clock providers from the DT
3641 * @matches: array of compatible values and init functions for providers.
3642 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003643 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003644 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003645 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003646 */
3647void __init of_clk_init(const struct of_device_id *matches)
3648{
Alex Elder7f7ed582013-08-22 11:31:31 -05003649 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003650 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003651 struct clock_provider *clk_provider, *next;
3652 bool is_init_done;
3653 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07003654 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05003655
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303656 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003657 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303658
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003659 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003660 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003661 struct clock_provider *parent;
3662
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01003663 if (!of_device_is_available(np))
3664 continue;
3665
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003666 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
3667 if (!parent) {
3668 list_for_each_entry_safe(clk_provider, next,
3669 &clk_provider_list, node) {
3670 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003671 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003672 kfree(clk_provider);
3673 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003674 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07003675 return;
3676 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003677
3678 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003679 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003680 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003681 }
3682
3683 while (!list_empty(&clk_provider_list)) {
3684 is_init_done = false;
3685 list_for_each_entry_safe(clk_provider, next,
3686 &clk_provider_list, node) {
3687 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003688
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02003689 /* Don't populate platform devices */
3690 of_node_set_flag(clk_provider->np,
3691 OF_POPULATED);
3692
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003693 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003694 of_clk_set_defaults(clk_provider->np, true);
3695
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003696 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02003697 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003698 kfree(clk_provider);
3699 is_init_done = true;
3700 }
3701 }
3702
3703 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01003704 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003705 * remaining providers during the last loop, so now we
3706 * initialize all the remaining ones unconditionally
3707 * in case the clock parent was not mandatory
3708 */
3709 if (!is_init_done)
3710 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003711 }
3712}
3713#endif