blob: 83db14168b1adcd9c442f0599e88fdd6b19b4b20 [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>
Stephen Boyd5cb05a12016-05-16 11:05:16 +05304 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
Mike Turquetteb24764902012-03-15 23:11:19 -07005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Standard functionality for the common clock API. See Documentation/clk.txt
11 */
12
Stephen Boyd3c373112015-06-19 15:00:46 -070013#include <linux/clk.h>
Michael Turquetteb09d6d92015-01-29 14:22:50 -080014#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020015#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070016#include <linux/module.h>
17#include <linux/mutex.h>
18#include <linux/spinlock.h>
19#include <linux/err.h>
20#include <linux/list.h>
21#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050022#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070023#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053024#include <linux/init.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>
Deepak Katragadda677bad12016-11-04 13:33:13 -070027#include <linux/of_platform.h>
28#include <linux/pm_opp.h>
Stephen Boyd5cb05a12016-05-16 11:05:16 +053029#include <linux/regulator/consumer.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070030
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020031#include "clk.h"
32
Mike Turquetteb24764902012-03-15 23:11:19 -070033static DEFINE_SPINLOCK(enable_lock);
34static DEFINE_MUTEX(prepare_lock);
35
Mike Turquette533ddeb2013-03-28 13:59:02 -070036static struct task_struct *prepare_owner;
37static struct task_struct *enable_owner;
38
39static int prepare_refcnt;
40static int enable_refcnt;
41
Mike Turquetteb24764902012-03-15 23:11:19 -070042static HLIST_HEAD(clk_root_list);
43static HLIST_HEAD(clk_orphan_list);
44static LIST_HEAD(clk_notifier_list);
45
Stephen Boyd5cb05a12016-05-16 11:05:16 +053046struct clk_handoff_vdd {
47 struct list_head list;
48 struct clk_vdd_class *vdd_class;
49};
50
51static LIST_HEAD(clk_handoff_vdd_list);
52
Michael Turquetteb09d6d92015-01-29 14:22:50 -080053/*** private data structures ***/
54
55struct clk_core {
56 const char *name;
57 const struct clk_ops *ops;
58 struct clk_hw *hw;
59 struct module *owner;
60 struct clk_core *parent;
61 const char **parent_names;
62 struct clk_core **parents;
63 u8 num_parents;
64 u8 new_parent_index;
65 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010066 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080067 unsigned long new_rate;
68 struct clk_core *new_parent;
69 struct clk_core *new_child;
70 unsigned long flags;
Heiko Stuebnere6500342015-04-22 22:53:05 +020071 bool orphan;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080072 unsigned int enable_count;
73 unsigned int prepare_count;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070074 unsigned long min_rate;
75 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080076 unsigned long accuracy;
77 int phase;
78 struct hlist_head children;
79 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010080 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080081 unsigned int notifier_count;
82#ifdef CONFIG_DEBUG_FS
83 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020084 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080085#endif
86 struct kref ref;
Stephen Boyd5cb05a12016-05-16 11:05:16 +053087 struct clk_vdd_class *vdd_class;
88 unsigned long *rate_max;
89 int num_rate_max;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080090};
91
Stephen Boyddfc202e2015-02-02 14:37:41 -080092#define CREATE_TRACE_POINTS
93#include <trace/events/clk.h>
94
Michael Turquetteb09d6d92015-01-29 14:22:50 -080095struct clk {
96 struct clk_core *core;
97 const char *dev_id;
98 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010099 unsigned long min_rate;
100 unsigned long max_rate;
Stephen Boyd50595f82015-02-06 11:42:44 -0800101 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -0800102};
103
Mike Turquetteeab89f62013-03-28 13:59:01 -0700104/*** locking ***/
105static void clk_prepare_lock(void)
106{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700107 if (!mutex_trylock(&prepare_lock)) {
108 if (prepare_owner == current) {
109 prepare_refcnt++;
110 return;
111 }
112 mutex_lock(&prepare_lock);
113 }
114 WARN_ON_ONCE(prepare_owner != NULL);
115 WARN_ON_ONCE(prepare_refcnt != 0);
116 prepare_owner = current;
117 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700118}
119
120static void clk_prepare_unlock(void)
121{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700122 WARN_ON_ONCE(prepare_owner != current);
123 WARN_ON_ONCE(prepare_refcnt == 0);
124
125 if (--prepare_refcnt)
126 return;
127 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700128 mutex_unlock(&prepare_lock);
129}
130
131static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700132 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700133{
134 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700135
136 if (!spin_trylock_irqsave(&enable_lock, flags)) {
137 if (enable_owner == current) {
138 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700139 __acquire(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700140 return flags;
141 }
142 spin_lock_irqsave(&enable_lock, flags);
143 }
144 WARN_ON_ONCE(enable_owner != NULL);
145 WARN_ON_ONCE(enable_refcnt != 0);
146 enable_owner = current;
147 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700148 return flags;
149}
150
151static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700152 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700153{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700154 WARN_ON_ONCE(enable_owner != current);
155 WARN_ON_ONCE(enable_refcnt == 0);
156
Stephen Boyda57aa182015-07-24 12:24:48 -0700157 if (--enable_refcnt) {
158 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700159 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700160 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700161 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700162 spin_unlock_irqrestore(&enable_lock, flags);
163}
164
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700165static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530166{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700167 /*
168 * .is_prepared is optional for clocks that can prepare
169 * fall back to software usage counter if it is missing
170 */
171 if (!core->ops->is_prepared)
172 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530173
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700174 return core->ops->is_prepared(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530175}
176
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700177static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530178{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700179 /*
180 * .is_enabled is only mandatory for clocks that gate
181 * fall back to software usage counter if .is_enabled is missing
182 */
183 if (!core->ops->is_enabled)
184 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530185
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700186 return core->ops->is_enabled(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530187}
188
Mike Turquetteb24764902012-03-15 23:11:19 -0700189/*** helper functions ***/
190
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200191const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700192{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100193 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700194}
Niels de Vos48950842012-12-13 13:12:25 +0100195EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700196
Stephen Boyde7df6f62015-08-12 13:04:56 -0700197const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700198{
199 return hw->core->name;
200}
201EXPORT_SYMBOL_GPL(clk_hw_get_name);
202
Russ Dill65800b22012-11-26 11:20:09 -0800203struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700204{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100205 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700206}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800207EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700208
Stephen Boyde7df6f62015-08-12 13:04:56 -0700209unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700210{
211 return hw->core->num_parents;
212}
213EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
214
Stephen Boyde7df6f62015-08-12 13:04:56 -0700215struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700216{
217 return hw->core->parent ? hw->core->parent->hw : NULL;
218}
219EXPORT_SYMBOL_GPL(clk_hw_get_parent);
220
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700221static struct clk_core *__clk_lookup_subtree(const char *name,
222 struct clk_core *core)
223{
224 struct clk_core *child;
225 struct clk_core *ret;
226
227 if (!strcmp(core->name, name))
228 return core;
229
230 hlist_for_each_entry(child, &core->children, child_node) {
231 ret = __clk_lookup_subtree(name, child);
232 if (ret)
233 return ret;
234 }
235
236 return NULL;
237}
238
239static struct clk_core *clk_core_lookup(const char *name)
240{
241 struct clk_core *root_clk;
242 struct clk_core *ret;
243
244 if (!name)
245 return NULL;
246
247 /* search the 'proper' clk tree first */
248 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
249 ret = __clk_lookup_subtree(name, root_clk);
250 if (ret)
251 return ret;
252 }
253
254 /* if not found, then search the orphan tree */
255 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
256 ret = __clk_lookup_subtree(name, root_clk);
257 if (ret)
258 return ret;
259 }
260
261 return NULL;
262}
263
Stephen Boydd6968fc2015-04-30 13:54:13 -0700264static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100265 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100266{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700267 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100268 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900269
270 if (!core->parents[index])
271 core->parents[index] =
272 clk_core_lookup(core->parent_names[index]);
273
274 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100275}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100276
Stephen Boyde7df6f62015-08-12 13:04:56 -0700277struct clk_hw *
278clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700279{
280 struct clk_core *parent;
281
282 parent = clk_core_get_parent_by_index(hw->core, index);
283
284 return !parent ? NULL : parent->hw;
285}
286EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
287
Russ Dill65800b22012-11-26 11:20:09 -0800288unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700289{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100290 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700291}
292
Stephen Boydd6968fc2015-04-30 13:54:13 -0700293static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700294{
295 unsigned long ret;
296
Stephen Boydd6968fc2015-04-30 13:54:13 -0700297 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530298 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700299 goto out;
300 }
301
Stephen Boydd6968fc2015-04-30 13:54:13 -0700302 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700303
Stephen Boyd47b0eeb2016-02-02 17:24:56 -0800304 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -0700305 goto out;
306
Stephen Boydd6968fc2015-04-30 13:54:13 -0700307 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530308 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700309
310out:
311 return ret;
312}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100313
Stephen Boyde7df6f62015-08-12 13:04:56 -0700314unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700315{
316 return clk_core_get_rate_nolock(hw->core);
317}
318EXPORT_SYMBOL_GPL(clk_hw_get_rate);
319
Stephen Boydd6968fc2015-04-30 13:54:13 -0700320static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100321{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700322 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100323 return 0;
324
Stephen Boydd6968fc2015-04-30 13:54:13 -0700325 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100326}
327
Russ Dill65800b22012-11-26 11:20:09 -0800328unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700329{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100330 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700331}
Thierry Redingb05c6832013-09-03 09:43:51 +0200332EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700333
Stephen Boyde7df6f62015-08-12 13:04:56 -0700334unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700335{
336 return hw->core->flags;
337}
338EXPORT_SYMBOL_GPL(clk_hw_get_flags);
339
Stephen Boyde7df6f62015-08-12 13:04:56 -0700340bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700341{
342 return clk_core_is_prepared(hw->core);
343}
344
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200345bool clk_hw_is_enabled(const struct clk_hw *hw)
346{
347 return clk_core_is_enabled(hw->core);
348}
349
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100350bool __clk_is_enabled(struct clk *clk)
351{
352 if (!clk)
353 return false;
354
355 return clk_core_is_enabled(clk->core);
356}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800357EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700358
Stephen Boyd15a02c12015-01-19 18:05:28 -0800359static bool mux_is_better_rate(unsigned long rate, unsigned long now,
360 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100361{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800362 if (flags & CLK_MUX_ROUND_CLOSEST)
363 return abs(now - rate) < abs(best - rate);
364
365 return now <= rate && now > best;
366}
367
Boris Brezillon0817b622015-07-07 20:48:08 +0200368static int
369clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800370 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100371{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100372 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200373 int i, num_parents, ret;
374 unsigned long best = 0;
375 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100376
377 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100378 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
379 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200380 if (core->flags & CLK_SET_RATE_PARENT) {
381 ret = __clk_determine_rate(parent ? parent->hw : NULL,
382 &parent_req);
383 if (ret)
384 return ret;
385
386 best = parent_req.rate;
387 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100388 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200389 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100390 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200391 }
392
James Hogane366fdd2013-07-29 12:25:02 +0100393 goto out;
394 }
395
396 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100397 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100398 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100399 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100400 if (!parent)
401 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200402
403 if (core->flags & CLK_SET_RATE_PARENT) {
404 parent_req = *req;
405 ret = __clk_determine_rate(parent->hw, &parent_req);
406 if (ret)
407 continue;
408 } else {
409 parent_req.rate = clk_core_get_rate_nolock(parent);
410 }
411
412 if (mux_is_better_rate(req->rate, parent_req.rate,
413 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100414 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200415 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100416 }
417 }
418
Boris Brezillon57d866e2015-07-09 22:39:38 +0200419 if (!best_parent)
420 return -EINVAL;
421
James Hogane366fdd2013-07-29 12:25:02 +0100422out:
423 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200424 req->best_parent_hw = best_parent->hw;
425 req->best_parent_rate = best;
426 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100427
Boris Brezillon0817b622015-07-07 20:48:08 +0200428 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100429}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800430
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100431struct clk *__clk_lookup(const char *name)
432{
433 struct clk_core *core = clk_core_lookup(name);
434
435 return !core ? NULL : core->hw->clk;
436}
437
Stephen Boydd6968fc2015-04-30 13:54:13 -0700438static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100439 unsigned long *min_rate,
440 unsigned long *max_rate)
441{
442 struct clk *clk_user;
443
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700444 *min_rate = core->min_rate;
445 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100446
Stephen Boydd6968fc2015-04-30 13:54:13 -0700447 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100448 *min_rate = max(*min_rate, clk_user->min_rate);
449
Stephen Boydd6968fc2015-04-30 13:54:13 -0700450 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100451 *max_rate = min(*max_rate, clk_user->max_rate);
452}
453
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700454void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
455 unsigned long max_rate)
456{
457 hw->core->min_rate = min_rate;
458 hw->core->max_rate = max_rate;
459}
460EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
461
Stephen Boyd15a02c12015-01-19 18:05:28 -0800462/*
Taniya Dasd9744c12016-08-19 10:08:28 +0530463 * Aggregate the rate of all child nodes which are enabled and exclude the
464 * child node which requests for clk_aggregate_rate.
465 */
466unsigned long clk_aggregate_rate(struct clk_hw *hw,
467 const struct clk_core *parent)
468{
469 struct clk_core *child;
470 unsigned long aggre_rate = 0;
471
472 hlist_for_each_entry(child, &parent->children, child_node) {
473 if (child->enable_count &&
474 strcmp(child->name, hw->init->name))
475 aggre_rate = max(child->rate, aggre_rate);
476 }
477
478 return aggre_rate;
479}
480EXPORT_SYMBOL_GPL(clk_aggregate_rate);
481
482/*
Stephen Boyd15a02c12015-01-19 18:05:28 -0800483 * Helper for finding best parent to provide a given frequency. This can be used
484 * directly as a determine_rate callback (e.g. for a mux), or from a more
485 * complex clock that may combine a mux with other operations.
486 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200487int __clk_mux_determine_rate(struct clk_hw *hw,
488 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800489{
Boris Brezillon0817b622015-07-07 20:48:08 +0200490 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800491}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800492EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100493
Boris Brezillon0817b622015-07-07 20:48:08 +0200494int __clk_mux_determine_rate_closest(struct clk_hw *hw,
495 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800496{
Boris Brezillon0817b622015-07-07 20:48:08 +0200497 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800498}
499EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
500
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530501/*
502 * Find the voltage level required for a given clock rate.
503 */
504static int clk_find_vdd_level(struct clk_core *clk, unsigned long rate)
505{
506 int level;
507
Deepak Katragaddaa755ecb2016-11-28 14:48:56 -0800508 /*
509 * For certain PLLs, due to the limitation in the bits allocated for
510 * programming the fractional divider, the actual rate of the PLL will
511 * be slightly higher than the requested rate (in the order of several
512 * Hz). To accommodate this difference, convert the FMAX rate and the
513 * clock frequency to KHz and use that for deriving the voltage level.
514 */
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530515 for (level = 0; level < clk->num_rate_max; level++)
Deepak Katragaddaa755ecb2016-11-28 14:48:56 -0800516 if (DIV_ROUND_CLOSEST(rate, 1000) <=
517 DIV_ROUND_CLOSEST(clk->rate_max[level], 1000))
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530518 break;
519
520 if (level == clk->num_rate_max) {
521 pr_err("Rate %lu for %s is greater than highest Fmax\n", rate,
522 clk->name);
523 return -EINVAL;
524 }
525
526 return level;
527}
528
529/*
530 * Update voltage level given the current votes.
531 */
532static int clk_update_vdd(struct clk_vdd_class *vdd_class)
533{
534 int level, rc = 0, i, ignore;
535 struct regulator **r = vdd_class->regulator;
536 int *uv = vdd_class->vdd_uv;
537 int n_reg = vdd_class->num_regulators;
538 int cur_lvl = vdd_class->cur_level;
539 int max_lvl = vdd_class->num_levels - 1;
540 int cur_base = cur_lvl * n_reg;
541 int new_base;
542
543 /* aggregate votes */
544 for (level = max_lvl; level > 0; level--)
545 if (vdd_class->level_votes[level])
546 break;
547
548 if (level == cur_lvl)
549 return 0;
550
551 max_lvl = max_lvl * n_reg;
552 new_base = level * n_reg;
553
554 for (i = 0; i < vdd_class->num_regulators; i++) {
555 pr_debug("Set Voltage level Min %d, Max %d\n", uv[new_base + i],
556 uv[max_lvl + i]);
557 rc = regulator_set_voltage(r[i], uv[new_base + i],
558 uv[max_lvl + i]);
559 if (rc)
560 goto set_voltage_fail;
561
562 if (cur_lvl == 0 || cur_lvl == vdd_class->num_levels)
563 rc = regulator_enable(r[i]);
564 else if (level == 0)
565 rc = regulator_disable(r[i]);
566 if (rc)
567 goto enable_disable_fail;
568 }
569
570 if (vdd_class->set_vdd && !vdd_class->num_regulators)
571 rc = vdd_class->set_vdd(vdd_class, level);
572
573 if (!rc)
574 vdd_class->cur_level = level;
575
576 return rc;
577
578enable_disable_fail:
579 regulator_set_voltage(r[i], uv[cur_base + i], uv[max_lvl + i]);
580
581set_voltage_fail:
582 for (i--; i >= 0; i--) {
583 regulator_set_voltage(r[i], uv[cur_base + i], uv[max_lvl + i]);
584 if (cur_lvl == 0 || cur_lvl == vdd_class->num_levels)
585 regulator_disable(r[i]);
586 else if (level == 0)
587 ignore = regulator_enable(r[i]);
588 }
589
590 return rc;
591}
592
593/*
594 * Vote for a voltage level.
595 */
596static int clk_vote_vdd_level(struct clk_vdd_class *vdd_class, int level)
597{
598 int rc = 0;
599
600 if (level >= vdd_class->num_levels)
601 return -EINVAL;
602
603 mutex_lock(&vdd_class->lock);
604
605 vdd_class->level_votes[level]++;
606
607 rc = clk_update_vdd(vdd_class);
608 if (rc)
609 vdd_class->level_votes[level]--;
610
611 mutex_unlock(&vdd_class->lock);
612
613 return rc;
614}
615
616/*
617 * Remove vote for a voltage level.
618 */
619static int clk_unvote_vdd_level(struct clk_vdd_class *vdd_class, int level)
620{
621 int rc = 0;
622
623 if (level >= vdd_class->num_levels)
624 return -EINVAL;
625
626 mutex_lock(&vdd_class->lock);
627
628 if (WARN(!vdd_class->level_votes[level],
629 "Reference counts are incorrect for %s level %d\n",
630 vdd_class->class_name, level))
631 goto out;
632
633 vdd_class->level_votes[level]--;
634
635 rc = clk_update_vdd(vdd_class);
636 if (rc)
637 vdd_class->level_votes[level]++;
638
639out:
640 mutex_unlock(&vdd_class->lock);
641 return rc;
642}
643
644/*
645 * Vote for a voltage level corresponding to a clock's rate.
646 */
647static int clk_vote_rate_vdd(struct clk_core *core, unsigned long rate)
648{
649 int level;
650
651 if (!core->vdd_class)
652 return 0;
653
654 level = clk_find_vdd_level(core, rate);
655 if (level < 0)
656 return level;
657
658 return clk_vote_vdd_level(core->vdd_class, level);
659}
660
661/*
662 * Remove vote for a voltage level corresponding to a clock's rate.
663 */
664static void clk_unvote_rate_vdd(struct clk_core *core, unsigned long rate)
665{
666 int level;
667
668 if (!core->vdd_class)
669 return;
670
671 level = clk_find_vdd_level(core, rate);
672 if (level < 0)
673 return;
674
675 clk_unvote_vdd_level(core->vdd_class, level);
676}
677
678static bool clk_is_rate_level_valid(struct clk_core *core, unsigned long rate)
679{
680 int level;
681
682 if (!core->vdd_class)
683 return true;
684
685 level = clk_find_vdd_level(core, rate);
686
687 return level >= 0;
688}
689
690static int clk_vdd_class_init(struct clk_vdd_class *vdd)
691{
692 struct clk_handoff_vdd *v;
693
694 list_for_each_entry(v, &clk_handoff_vdd_list, list) {
695 if (v->vdd_class == vdd)
696 return 0;
697 }
698
699 pr_debug("voting for vdd_class %s\n", vdd->class_name);
700
701 if (clk_vote_vdd_level(vdd, vdd->num_levels - 1))
702 pr_err("failed to vote for %s\n", vdd->class_name);
703
704 v = kmalloc(sizeof(*v), GFP_KERNEL);
705 if (!v)
706 return -ENOMEM;
707
708 v->vdd_class = vdd;
709
710 list_add_tail(&v->list, &clk_handoff_vdd_list);
711
712 return 0;
713}
714
Mike Turquetteb24764902012-03-15 23:11:19 -0700715/*** clk api ***/
716
Stephen Boydd6968fc2015-04-30 13:54:13 -0700717static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700718{
Stephen Boyda6334722015-05-06 17:00:54 -0700719 lockdep_assert_held(&prepare_lock);
720
Stephen Boydd6968fc2015-04-30 13:54:13 -0700721 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700722 return;
723
Stephen Boydd6968fc2015-04-30 13:54:13 -0700724 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700725 return;
726
Lee Jones2e20fbf2016-02-11 13:19:10 -0800727 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
728 return;
729
Stephen Boydd6968fc2015-04-30 13:54:13 -0700730 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700731 return;
732
Stephen Boydd6968fc2015-04-30 13:54:13 -0700733 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700734
Stephen Boydd6968fc2015-04-30 13:54:13 -0700735 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800736
Stephen Boydd6968fc2015-04-30 13:54:13 -0700737 if (core->ops->unprepare)
738 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700739
Stephen Boydd6968fc2015-04-30 13:54:13 -0700740 trace_clk_unprepare_complete(core);
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530741
742 clk_unvote_rate_vdd(core, core->rate);
743
Stephen Boydd6968fc2015-04-30 13:54:13 -0700744 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700745}
746
Dong Aishenga6adc302016-06-30 17:31:11 +0800747static void clk_core_unprepare_lock(struct clk_core *core)
748{
749 clk_prepare_lock();
750 clk_core_unprepare(core);
751 clk_prepare_unlock();
752}
753
Mike Turquetteb24764902012-03-15 23:11:19 -0700754/**
755 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200756 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700757 *
758 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
759 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
760 * if the operation may sleep. One example is a clk which is accessed over
761 * I2c. In the complex case a clk gate operation may require a fast and a slow
762 * part. It is this reason that clk_unprepare and clk_disable are not mutually
763 * exclusive. In fact clk_disable must be called before clk_unprepare.
764 */
765void clk_unprepare(struct clk *clk)
766{
Stephen Boyd63589e92014-03-26 16:06:37 -0700767 if (IS_ERR_OR_NULL(clk))
768 return;
769
Dong Aishenga6adc302016-06-30 17:31:11 +0800770 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700771}
772EXPORT_SYMBOL_GPL(clk_unprepare);
773
Stephen Boydd6968fc2015-04-30 13:54:13 -0700774static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700775{
776 int ret = 0;
777
Stephen Boyda6334722015-05-06 17:00:54 -0700778 lockdep_assert_held(&prepare_lock);
779
Stephen Boydd6968fc2015-04-30 13:54:13 -0700780 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700781 return 0;
782
Stephen Boydd6968fc2015-04-30 13:54:13 -0700783 if (core->prepare_count == 0) {
784 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700785 if (ret)
786 return ret;
787
Stephen Boydd6968fc2015-04-30 13:54:13 -0700788 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800789
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530790 ret = clk_vote_rate_vdd(core, core->rate);
791 if (ret) {
792 clk_core_unprepare(core->parent);
793 return ret;
794 }
795
Stephen Boydd6968fc2015-04-30 13:54:13 -0700796 if (core->ops->prepare)
797 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800798
Stephen Boydd6968fc2015-04-30 13:54:13 -0700799 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800800
801 if (ret) {
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530802 clk_unvote_rate_vdd(core, core->rate);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700803 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800804 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700805 }
806 }
807
Stephen Boydd6968fc2015-04-30 13:54:13 -0700808 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700809
810 return 0;
811}
812
Dong Aishenga6adc302016-06-30 17:31:11 +0800813static int clk_core_prepare_lock(struct clk_core *core)
814{
815 int ret;
816
817 clk_prepare_lock();
818 ret = clk_core_prepare(core);
819 clk_prepare_unlock();
820
821 return ret;
822}
823
Mike Turquetteb24764902012-03-15 23:11:19 -0700824/**
825 * clk_prepare - prepare a clock source
826 * @clk: the clk being prepared
827 *
828 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
829 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
830 * operation may sleep. One example is a clk which is accessed over I2c. In
831 * the complex case a clk ungate operation may require a fast and a slow part.
832 * It is this reason that clk_prepare and clk_enable are not mutually
833 * exclusive. In fact clk_prepare must be called before clk_enable.
834 * Returns 0 on success, -EERROR otherwise.
835 */
836int clk_prepare(struct clk *clk)
837{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100838 if (!clk)
839 return 0;
840
Dong Aishenga6adc302016-06-30 17:31:11 +0800841 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700842}
843EXPORT_SYMBOL_GPL(clk_prepare);
844
Stephen Boydd6968fc2015-04-30 13:54:13 -0700845static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700846{
Stephen Boyda6334722015-05-06 17:00:54 -0700847 lockdep_assert_held(&enable_lock);
848
Stephen Boydd6968fc2015-04-30 13:54:13 -0700849 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700850 return;
851
Stephen Boydd6968fc2015-04-30 13:54:13 -0700852 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700853 return;
854
Lee Jones2e20fbf2016-02-11 13:19:10 -0800855 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
856 return;
857
Stephen Boydd6968fc2015-04-30 13:54:13 -0700858 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700859 return;
860
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700861 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800862
Stephen Boydd6968fc2015-04-30 13:54:13 -0700863 if (core->ops->disable)
864 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700865
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700866 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800867
Stephen Boydd6968fc2015-04-30 13:54:13 -0700868 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100869}
870
Dong Aishenga6adc302016-06-30 17:31:11 +0800871static void clk_core_disable_lock(struct clk_core *core)
872{
873 unsigned long flags;
874
875 flags = clk_enable_lock();
876 clk_core_disable(core);
877 clk_enable_unlock(flags);
878}
879
Mike Turquetteb24764902012-03-15 23:11:19 -0700880/**
881 * clk_disable - gate a clock
882 * @clk: the clk being gated
883 *
884 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
885 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
886 * clk if the operation is fast and will never sleep. One example is a
887 * SoC-internal clk which is controlled via simple register writes. In the
888 * complex case a clk gate operation may require a fast and a slow part. It is
889 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
890 * In fact clk_disable must be called before clk_unprepare.
891 */
892void clk_disable(struct clk *clk)
893{
Stephen Boyd63589e92014-03-26 16:06:37 -0700894 if (IS_ERR_OR_NULL(clk))
895 return;
896
Dong Aishenga6adc302016-06-30 17:31:11 +0800897 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700898}
899EXPORT_SYMBOL_GPL(clk_disable);
900
Stephen Boydd6968fc2015-04-30 13:54:13 -0700901static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700902{
903 int ret = 0;
904
Stephen Boyda6334722015-05-06 17:00:54 -0700905 lockdep_assert_held(&enable_lock);
906
Stephen Boydd6968fc2015-04-30 13:54:13 -0700907 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700908 return 0;
909
Stephen Boydd6968fc2015-04-30 13:54:13 -0700910 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700911 return -ESHUTDOWN;
912
Stephen Boydd6968fc2015-04-30 13:54:13 -0700913 if (core->enable_count == 0) {
914 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700915
916 if (ret)
917 return ret;
918
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700919 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800920
Stephen Boydd6968fc2015-04-30 13:54:13 -0700921 if (core->ops->enable)
922 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800923
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700924 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800925
926 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700927 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800928 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700929 }
930 }
931
Stephen Boydd6968fc2015-04-30 13:54:13 -0700932 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700933 return 0;
934}
935
Dong Aishenga6adc302016-06-30 17:31:11 +0800936static int clk_core_enable_lock(struct clk_core *core)
937{
938 unsigned long flags;
939 int ret;
940
941 flags = clk_enable_lock();
942 ret = clk_core_enable(core);
943 clk_enable_unlock(flags);
944
945 return ret;
946}
947
Mike Turquetteb24764902012-03-15 23:11:19 -0700948/**
949 * clk_enable - ungate a clock
950 * @clk: the clk being ungated
951 *
952 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
953 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
954 * if the operation will never sleep. One example is a SoC-internal clk which
955 * is controlled via simple register writes. In the complex case a clk ungate
956 * operation may require a fast and a slow part. It is this reason that
957 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
958 * must be called before clk_enable. Returns 0 on success, -EERROR
959 * otherwise.
960 */
961int clk_enable(struct clk *clk)
962{
Dong Aisheng864e1602015-04-30 14:02:19 -0700963 if (!clk)
964 return 0;
965
Dong Aishenga6adc302016-06-30 17:31:11 +0800966 return clk_core_enable_lock(clk->core);
967}
968EXPORT_SYMBOL_GPL(clk_enable);
969
970static int clk_core_prepare_enable(struct clk_core *core)
971{
972 int ret;
973
974 ret = clk_core_prepare_lock(core);
975 if (ret)
976 return ret;
977
978 ret = clk_core_enable_lock(core);
979 if (ret)
980 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700981
982 return ret;
983}
Dong Aishenga6adc302016-06-30 17:31:11 +0800984
985static void clk_core_disable_unprepare(struct clk_core *core)
986{
987 clk_core_disable_lock(core);
988 clk_core_unprepare_lock(core);
989}
Mike Turquetteb24764902012-03-15 23:11:19 -0700990
Dong Aisheng7ec986e2016-06-30 17:31:12 +0800991static void clk_unprepare_unused_subtree(struct clk_core *core)
992{
993 struct clk_core *child;
994
995 lockdep_assert_held(&prepare_lock);
996
997 hlist_for_each_entry(child, &core->children, child_node)
998 clk_unprepare_unused_subtree(child);
999
1000 if (core->prepare_count)
1001 return;
1002
1003 if (core->flags & CLK_IGNORE_UNUSED)
1004 return;
1005
1006 if (clk_core_is_prepared(core)) {
1007 trace_clk_unprepare(core);
1008 if (core->ops->unprepare_unused)
1009 core->ops->unprepare_unused(core->hw);
1010 else if (core->ops->unprepare)
1011 core->ops->unprepare(core->hw);
1012 trace_clk_unprepare_complete(core);
1013 }
1014}
1015
1016static void clk_disable_unused_subtree(struct clk_core *core)
1017{
1018 struct clk_core *child;
1019 unsigned long flags;
1020
1021 lockdep_assert_held(&prepare_lock);
1022
1023 hlist_for_each_entry(child, &core->children, child_node)
1024 clk_disable_unused_subtree(child);
1025
Dong Aishenga4b35182016-06-30 17:31:13 +08001026 if (core->flags & CLK_OPS_PARENT_ENABLE)
1027 clk_core_prepare_enable(core->parent);
1028
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001029 flags = clk_enable_lock();
1030
1031 if (core->enable_count)
1032 goto unlock_out;
1033
1034 if (core->flags & CLK_IGNORE_UNUSED)
1035 goto unlock_out;
1036
1037 /*
1038 * some gate clocks have special needs during the disable-unused
1039 * sequence. call .disable_unused if available, otherwise fall
1040 * back to .disable
1041 */
1042 if (clk_core_is_enabled(core)) {
1043 trace_clk_disable(core);
1044 if (core->ops->disable_unused)
1045 core->ops->disable_unused(core->hw);
1046 else if (core->ops->disable)
1047 core->ops->disable(core->hw);
1048 trace_clk_disable_complete(core);
1049 }
1050
1051unlock_out:
1052 clk_enable_unlock(flags);
Dong Aishenga4b35182016-06-30 17:31:13 +08001053 if (core->flags & CLK_OPS_PARENT_ENABLE)
1054 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001055}
1056
1057static bool clk_ignore_unused;
1058static int __init clk_ignore_unused_setup(char *__unused)
1059{
1060 clk_ignore_unused = true;
1061 return 1;
1062}
1063__setup("clk_ignore_unused", clk_ignore_unused_setup);
1064
1065static int clk_disable_unused(void)
1066{
1067 struct clk_core *core;
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301068 struct clk_handoff_vdd *v, *v_temp;
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001069
1070 if (clk_ignore_unused) {
1071 pr_warn("clk: Not disabling unused clocks\n");
1072 return 0;
1073 }
1074
1075 clk_prepare_lock();
1076
1077 hlist_for_each_entry(core, &clk_root_list, child_node)
1078 clk_disable_unused_subtree(core);
1079
1080 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1081 clk_disable_unused_subtree(core);
1082
1083 hlist_for_each_entry(core, &clk_root_list, child_node)
1084 clk_unprepare_unused_subtree(core);
1085
1086 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1087 clk_unprepare_unused_subtree(core);
1088
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301089 list_for_each_entry_safe(v, v_temp, &clk_handoff_vdd_list, list) {
1090 clk_unvote_vdd_level(v->vdd_class,
1091 v->vdd_class->num_levels - 1);
1092 list_del(&v->list);
1093 kfree(v);
1094 };
1095
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001096 clk_prepare_unlock();
1097
1098 return 0;
1099}
1100late_initcall_sync(clk_disable_unused);
1101
Boris Brezillon0817b622015-07-07 20:48:08 +02001102static int clk_core_round_rate_nolock(struct clk_core *core,
1103 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001104{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001105 struct clk_core *parent;
Boris Brezillon0817b622015-07-07 20:48:08 +02001106 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001107
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001108 lockdep_assert_held(&prepare_lock);
1109
Stephen Boydd6968fc2015-04-30 13:54:13 -07001110 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001111 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001112
Stephen Boydd6968fc2015-04-30 13:54:13 -07001113 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +02001114 if (parent) {
1115 req->best_parent_hw = parent->hw;
1116 req->best_parent_rate = parent->rate;
1117 } else {
1118 req->best_parent_hw = NULL;
1119 req->best_parent_rate = 0;
1120 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001121
Stephen Boydd6968fc2015-04-30 13:54:13 -07001122 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001123 return core->ops->determine_rate(core->hw, req);
1124 } else if (core->ops->round_rate) {
1125 rate = core->ops->round_rate(core->hw, req->rate,
1126 &req->best_parent_rate);
1127 if (rate < 0)
1128 return rate;
1129
1130 req->rate = rate;
1131 } else if (core->flags & CLK_SET_RATE_PARENT) {
1132 return clk_core_round_rate_nolock(parent, req);
1133 } else {
1134 req->rate = core->rate;
1135 }
1136
1137 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001138}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001139
1140/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001141 * __clk_determine_rate - get the closest rate actually supported by a clock
1142 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001143 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001144 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001145 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001146 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001147int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001148{
Boris Brezillon0817b622015-07-07 20:48:08 +02001149 if (!hw) {
1150 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001151 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001152 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001153
Boris Brezillon0817b622015-07-07 20:48:08 +02001154 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001155}
1156EXPORT_SYMBOL_GPL(__clk_determine_rate);
1157
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001158unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1159{
1160 int ret;
1161 struct clk_rate_request req;
1162
1163 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1164 req.rate = rate;
1165
1166 ret = clk_core_round_rate_nolock(hw->core, &req);
1167 if (ret)
1168 return 0;
1169
1170 return req.rate;
1171}
1172EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1173
Mike Turquetteb24764902012-03-15 23:11:19 -07001174/**
1175 * clk_round_rate - round the given rate for a clk
1176 * @clk: the clk for which we are rounding a rate
1177 * @rate: the rate which is to be rounded
1178 *
1179 * Takes in a rate as input and rounds it to a rate that the clk can actually
1180 * use which is then returned. If clk doesn't support round_rate operation
1181 * then the parent rate is returned.
1182 */
1183long clk_round_rate(struct clk *clk, unsigned long rate)
1184{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001185 struct clk_rate_request req;
1186 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001187
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001188 if (!clk)
1189 return 0;
1190
Mike Turquetteeab89f62013-03-28 13:59:01 -07001191 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001192
1193 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1194 req.rate = rate;
1195
1196 ret = clk_core_round_rate_nolock(clk->core, &req);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001197 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001198
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001199 if (ret)
1200 return ret;
1201
1202 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001203}
1204EXPORT_SYMBOL_GPL(clk_round_rate);
1205
1206/**
1207 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001208 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001209 * @msg: clk notifier type (see include/linux/clk.h)
1210 * @old_rate: old clk rate
1211 * @new_rate: new clk rate
1212 *
1213 * Triggers a notifier call chain on the clk rate-change notification
1214 * for 'clk'. Passes a pointer to the struct clk and the previous
1215 * and current rates to the notifier callback. Intended to be called by
1216 * internal clock code only. Returns NOTIFY_DONE from the last driver
1217 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1218 * a driver returns that.
1219 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001220static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001221 unsigned long old_rate, unsigned long new_rate)
1222{
1223 struct clk_notifier *cn;
1224 struct clk_notifier_data cnd;
1225 int ret = NOTIFY_DONE;
1226
Mike Turquetteb24764902012-03-15 23:11:19 -07001227 cnd.old_rate = old_rate;
1228 cnd.new_rate = new_rate;
1229
1230 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001231 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001232 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001233 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1234 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -07001235 }
1236 }
1237
1238 return ret;
1239}
1240
1241/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001242 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001243 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001244 *
1245 * Walks the subtree of clks starting with clk and recalculates accuracies as
1246 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001247 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001248 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001249 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001250static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001251{
1252 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001253 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001254
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001255 lockdep_assert_held(&prepare_lock);
1256
Stephen Boydd6968fc2015-04-30 13:54:13 -07001257 if (core->parent)
1258 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001259
Stephen Boydd6968fc2015-04-30 13:54:13 -07001260 if (core->ops->recalc_accuracy)
1261 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001262 parent_accuracy);
1263 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001264 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001265
Stephen Boydd6968fc2015-04-30 13:54:13 -07001266 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001267 __clk_recalc_accuracies(child);
1268}
1269
Stephen Boydd6968fc2015-04-30 13:54:13 -07001270static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001271{
1272 unsigned long accuracy;
1273
1274 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001275 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1276 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001277
Stephen Boydd6968fc2015-04-30 13:54:13 -07001278 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001279 clk_prepare_unlock();
1280
1281 return accuracy;
1282}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001283
1284/**
1285 * clk_get_accuracy - return the accuracy of clk
1286 * @clk: the clk whose accuracy is being returned
1287 *
1288 * Simply returns the cached accuracy of the clk, unless
1289 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1290 * issued.
1291 * If clk is NULL then returns 0.
1292 */
1293long clk_get_accuracy(struct clk *clk)
1294{
1295 if (!clk)
1296 return 0;
1297
1298 return clk_core_get_accuracy(clk->core);
1299}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001300EXPORT_SYMBOL_GPL(clk_get_accuracy);
1301
Stephen Boydd6968fc2015-04-30 13:54:13 -07001302static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001303 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001304{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001305 if (core->ops->recalc_rate)
1306 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001307 return parent_rate;
1308}
1309
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001310/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001311 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001312 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001313 * @msg: notification type (see include/linux/clk.h)
1314 *
1315 * Walks the subtree of clks starting with clk and recalculates rates as it
1316 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001317 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001318 *
1319 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1320 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001321 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001322static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001323{
1324 unsigned long old_rate;
1325 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001326 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001327
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001328 lockdep_assert_held(&prepare_lock);
1329
Stephen Boydd6968fc2015-04-30 13:54:13 -07001330 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001331
Stephen Boydd6968fc2015-04-30 13:54:13 -07001332 if (core->parent)
1333 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001334
Stephen Boydd6968fc2015-04-30 13:54:13 -07001335 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001336
1337 /*
1338 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1339 * & ABORT_RATE_CHANGE notifiers
1340 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001341 if (core->notifier_count && msg)
1342 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001343
Stephen Boydd6968fc2015-04-30 13:54:13 -07001344 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001345 __clk_recalc_rates(child, msg);
1346}
1347
Stephen Boydd6968fc2015-04-30 13:54:13 -07001348static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001349{
1350 unsigned long rate;
1351
1352 clk_prepare_lock();
1353
Stephen Boydd6968fc2015-04-30 13:54:13 -07001354 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1355 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001356
Stephen Boydd6968fc2015-04-30 13:54:13 -07001357 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001358 clk_prepare_unlock();
1359
1360 return rate;
1361}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001362
Mike Turquetteb24764902012-03-15 23:11:19 -07001363/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001364 * clk_get_rate - return the rate of clk
1365 * @clk: the clk whose rate is being returned
1366 *
1367 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1368 * is set, which means a recalc_rate will be issued.
1369 * If clk is NULL then returns 0.
1370 */
1371unsigned long clk_get_rate(struct clk *clk)
1372{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001373 if (!clk)
1374 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001375
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001376 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001377}
1378EXPORT_SYMBOL_GPL(clk_get_rate);
1379
Stephen Boydd6968fc2015-04-30 13:54:13 -07001380static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001381 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001382{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001383 int i;
James Hogan4935b222013-07-29 12:24:59 +01001384
Masahiro Yamada508f8842015-12-28 19:23:08 +09001385 if (!parent)
1386 return -EINVAL;
1387
Masahiro Yamada470b5e22015-12-28 19:23:09 +09001388 for (i = 0; i < core->num_parents; i++)
1389 if (clk_core_get_parent_by_index(core, i) == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001390 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001391
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001392 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001393}
1394
Heiko Stuebnere6500342015-04-22 22:53:05 +02001395/*
1396 * Update the orphan status of @core and all its children.
1397 */
1398static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1399{
1400 struct clk_core *child;
1401
1402 core->orphan = is_orphan;
1403
1404 hlist_for_each_entry(child, &core->children, child_node)
1405 clk_core_update_orphan_status(child, is_orphan);
1406}
1407
Stephen Boydd6968fc2015-04-30 13:54:13 -07001408static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001409{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001410 bool was_orphan = core->orphan;
1411
Stephen Boydd6968fc2015-04-30 13:54:13 -07001412 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001413
James Hogan903efc52013-08-29 12:10:51 +01001414 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001415 bool becomes_orphan = new_parent->orphan;
1416
James Hogan903efc52013-08-29 12:10:51 +01001417 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001418 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001419 new_parent->new_child = NULL;
1420
Stephen Boydd6968fc2015-04-30 13:54:13 -07001421 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001422
1423 if (was_orphan != becomes_orphan)
1424 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001425 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001426 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001427 if (!was_orphan)
1428 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001429 }
James Hogan4935b222013-07-29 12:24:59 +01001430
Stephen Boydd6968fc2015-04-30 13:54:13 -07001431 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001432}
1433
Stephen Boydd6968fc2015-04-30 13:54:13 -07001434static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001435 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001436{
1437 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001438 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001439
1440 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001441 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1442 *
1443 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001444 * clk_enable().
1445 *
1446 * If the clock is not prepared, then a race with
1447 * clk_enable/disable() is impossible since we already have the
1448 * prepare lock (future calls to clk_enable() need to be preceded by
1449 * a clk_prepare()).
1450 *
1451 * If the clock is prepared, migrate the prepared state to the new
1452 * parent and also protect against a race with clk_enable() by
1453 * forcing the clock and the new parent on. This ensures that all
1454 * future calls to clk_enable() are practically NOPs with respect to
1455 * hardware and software states.
1456 *
1457 * See also: Comment for clk_set_parent() below.
1458 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001459
1460 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1461 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1462 clk_core_prepare_enable(old_parent);
1463 clk_core_prepare_enable(parent);
1464 }
1465
1466 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001467 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001468 clk_core_prepare_enable(parent);
1469 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001470 }
1471
1472 /* update the clk tree topology */
1473 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001474 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001475 clk_enable_unlock(flags);
1476
Stephen Boyd3fa22522014-01-15 10:47:22 -08001477 return old_parent;
1478}
1479
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001480static void __clk_set_parent_after(struct clk_core *core,
1481 struct clk_core *parent,
1482 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001483{
1484 /*
1485 * Finish the migration of prepare state and undo the changes done
1486 * for preventing a race with clk_enable().
1487 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001488 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001489 clk_core_disable_lock(core);
1490 clk_core_disable_unprepare(old_parent);
1491 }
1492
1493 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1494 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1495 clk_core_disable_unprepare(parent);
1496 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001497 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001498}
1499
Stephen Boydd6968fc2015-04-30 13:54:13 -07001500static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001501 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001502{
1503 unsigned long flags;
1504 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001505 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001506
Stephen Boydd6968fc2015-04-30 13:54:13 -07001507 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001508
Stephen Boydd6968fc2015-04-30 13:54:13 -07001509 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001510
James Hogan4935b222013-07-29 12:24:59 +01001511 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001512 if (parent && core->ops->set_parent)
1513 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001514
Stephen Boydd6968fc2015-04-30 13:54:13 -07001515 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001516
James Hogan4935b222013-07-29 12:24:59 +01001517 if (ret) {
1518 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001519 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001520 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001521 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001522
James Hogan4935b222013-07-29 12:24:59 +01001523 return ret;
1524 }
1525
Stephen Boydd6968fc2015-04-30 13:54:13 -07001526 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001527
James Hogan4935b222013-07-29 12:24:59 +01001528 return 0;
1529}
1530
Ulf Hanssona093bde2012-08-31 14:21:28 +02001531/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001532 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001533 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001534 * @parent_rate: the "future" rate of clk's parent
1535 *
1536 * Walks the subtree of clks starting with clk, speculating rates as it
1537 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1538 *
1539 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1540 * pre-rate change notifications and returns early if no clks in the
1541 * subtree have subscribed to the notifications. Note that if a clk does not
1542 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001543 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001544 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001545static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001546 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001547{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001548 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001549 unsigned long new_rate;
1550 int ret = NOTIFY_DONE;
1551
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001552 lockdep_assert_held(&prepare_lock);
1553
Stephen Boydd6968fc2015-04-30 13:54:13 -07001554 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001555
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001556 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001557 if (core->notifier_count)
1558 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001559
Mike Turquette86bcfa22014-02-24 16:08:41 -08001560 if (ret & NOTIFY_STOP_MASK) {
1561 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001562 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001563 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001564 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001565
Stephen Boydd6968fc2015-04-30 13:54:13 -07001566 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001567 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001568 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001569 break;
1570 }
1571
1572out:
1573 return ret;
1574}
1575
Stephen Boydd6968fc2015-04-30 13:54:13 -07001576static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001577 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001578{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001579 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001580
Stephen Boydd6968fc2015-04-30 13:54:13 -07001581 core->new_rate = new_rate;
1582 core->new_parent = new_parent;
1583 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001584 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001585 core->new_child = NULL;
1586 if (new_parent && new_parent != core->parent)
1587 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001588
Stephen Boydd6968fc2015-04-30 13:54:13 -07001589 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001590 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001591 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001592 }
1593}
1594
1595/*
1596 * calculate the new rates returning the topmost clock that has to be
1597 * changed.
1598 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001599static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001600 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001601{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001602 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001603 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001604 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001605 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001606 unsigned long min_rate;
1607 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001608 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001609 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001610
Mike Turquette7452b212012-03-26 14:45:36 -07001611 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001612 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001613 return NULL;
1614
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001615 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001616 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001617 if (parent)
1618 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001619
Stephen Boydd6968fc2015-04-30 13:54:13 -07001620 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001621
James Hogan71472c02013-07-29 12:25:00 +01001622 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001623 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001624 struct clk_rate_request req;
1625
1626 req.rate = rate;
1627 req.min_rate = min_rate;
1628 req.max_rate = max_rate;
1629 if (parent) {
1630 req.best_parent_hw = parent->hw;
1631 req.best_parent_rate = parent->rate;
1632 } else {
1633 req.best_parent_hw = NULL;
1634 req.best_parent_rate = 0;
1635 }
1636
1637 ret = core->ops->determine_rate(core->hw, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001638 if (ret < 0)
1639 return NULL;
1640
Boris Brezillon0817b622015-07-07 20:48:08 +02001641 best_parent_rate = req.best_parent_rate;
1642 new_rate = req.rate;
1643 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001644 } else if (core->ops->round_rate) {
1645 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon0817b622015-07-07 20:48:08 +02001646 &best_parent_rate);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001647 if (ret < 0)
1648 return NULL;
1649
1650 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001651 if (new_rate < min_rate || new_rate > max_rate)
1652 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001653 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001654 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001655 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001656 return NULL;
1657 } else {
1658 /* pass-through clock with adjustable parent */
1659 top = clk_calc_new_rates(parent, rate);
1660 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001661 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001662 }
1663
James Hogan71472c02013-07-29 12:25:00 +01001664 /* some clocks must be gated to change parent */
1665 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001666 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001667 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001668 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001669 return NULL;
1670 }
1671
James Hogan71472c02013-07-29 12:25:00 +01001672 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001673 if (parent && core->num_parents > 1) {
1674 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001675 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001676 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001677 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001678 return NULL;
1679 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001680 }
1681
Stephen Boydd6968fc2015-04-30 13:54:13 -07001682 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001683 best_parent_rate != parent->rate)
1684 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001685
1686out:
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301687 if (!clk_is_rate_level_valid(core, rate))
1688 return NULL;
1689
Stephen Boydd6968fc2015-04-30 13:54:13 -07001690 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001691
1692 return top;
1693}
1694
1695/*
1696 * Notify about rate changes in a subtree. Always walk down the whole tree
1697 * so that in case of an error we can walk down the whole tree again and
1698 * abort the change.
1699 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001700static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001701 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001702{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001703 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001704 int ret = NOTIFY_DONE;
1705
Stephen Boydd6968fc2015-04-30 13:54:13 -07001706 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301707 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001708
Stephen Boydd6968fc2015-04-30 13:54:13 -07001709 if (core->notifier_count) {
1710 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001711 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001712 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001713 }
1714
Stephen Boydd6968fc2015-04-30 13:54:13 -07001715 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001716 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001717 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001718 continue;
1719 tmp_clk = clk_propagate_rate_change(child, event);
1720 if (tmp_clk)
1721 fail_clk = tmp_clk;
1722 }
1723
Stephen Boydd6968fc2015-04-30 13:54:13 -07001724 /* handle the new child who might not be in core->children yet */
1725 if (core->new_child) {
1726 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001727 if (tmp_clk)
1728 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001729 }
1730
1731 return fail_clk;
1732}
1733
1734/*
1735 * walk down a subtree and set the new rates notifying the rate
1736 * change on the way
1737 */
Taniya Das083a1982016-06-14 16:39:56 +05301738static int clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001739{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001740 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001741 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001742 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001743 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001744 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001745 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001746 struct clk_core *parent = NULL;
Taniya Das083a1982016-06-14 16:39:56 +05301747 int rc = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001748
Stephen Boydd6968fc2015-04-30 13:54:13 -07001749 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001750
Dong Aishengfc8726a2016-06-30 17:31:14 +08001751 if (core->new_parent) {
1752 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001753 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001754 } else if (core->parent) {
1755 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001756 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001757 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001758
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001759 if (core->flags & CLK_SET_RATE_UNGATE) {
1760 unsigned long flags;
1761
1762 clk_core_prepare(core);
1763 flags = clk_enable_lock();
1764 clk_core_enable(core);
1765 clk_enable_unlock(flags);
1766 }
1767
Stephen Boydd6968fc2015-04-30 13:54:13 -07001768 if (core->new_parent && core->new_parent != core->parent) {
1769 old_parent = __clk_set_parent_before(core, core->new_parent);
1770 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001771
Stephen Boydd6968fc2015-04-30 13:54:13 -07001772 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001773 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001774 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001775 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001776 core->new_parent_index);
1777 } else if (core->ops->set_parent) {
1778 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001779 }
1780
Stephen Boydd6968fc2015-04-30 13:54:13 -07001781 trace_clk_set_parent_complete(core, core->new_parent);
1782 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001783 }
1784
Dong Aishengfc8726a2016-06-30 17:31:14 +08001785 if (core->flags & CLK_OPS_PARENT_ENABLE)
1786 clk_core_prepare_enable(parent);
1787
Stephen Boydd6968fc2015-04-30 13:54:13 -07001788 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001789
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301790 /* Enforce vdd requirements for new frequency. */
1791 if (core->prepare_count) {
1792 rc = clk_vote_rate_vdd(core, core->new_rate);
1793 if (rc)
1794 goto out;
1795 }
1796
Taniya Das083a1982016-06-14 16:39:56 +05301797 if (!skip_set_rate && core->ops->set_rate) {
1798 rc = core->ops->set_rate(core->hw, core->new_rate,
1799 best_parent_rate);
1800 if (rc)
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301801 goto err_set_rate;
Taniya Das083a1982016-06-14 16:39:56 +05301802 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001803
Stephen Boydd6968fc2015-04-30 13:54:13 -07001804 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001805
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301806 /* Release vdd requirements for old frequency. */
1807 if (core->prepare_count)
1808 clk_unvote_rate_vdd(core, old_rate);
1809
Stephen Boydd6968fc2015-04-30 13:54:13 -07001810 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001811
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001812 if (core->flags & CLK_SET_RATE_UNGATE) {
1813 unsigned long flags;
1814
1815 flags = clk_enable_lock();
1816 clk_core_disable(core);
1817 clk_enable_unlock(flags);
1818 clk_core_unprepare(core);
1819 }
1820
Dong Aishengfc8726a2016-06-30 17:31:14 +08001821 if (core->flags & CLK_OPS_PARENT_ENABLE)
1822 clk_core_disable_unprepare(parent);
1823
Stephen Boydd6968fc2015-04-30 13:54:13 -07001824 if (core->notifier_count && old_rate != core->rate)
1825 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001826
Michael Turquette85e88fa2015-06-20 12:18:03 -07001827 if (core->flags & CLK_RECALC_NEW_RATES)
1828 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001829
Tero Kristo067bb172014-08-21 16:47:45 +03001830 /*
1831 * Use safe iteration, as change_rate can actually swap parents
1832 * for certain clock types.
1833 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001834 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001835 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001836 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001837 continue;
Taniya Das083a1982016-06-14 16:39:56 +05301838 rc = clk_change_rate(child);
1839 if (rc)
1840 return rc;
James Hogan71472c02013-07-29 12:25:00 +01001841 }
1842
Stephen Boydd6968fc2015-04-30 13:54:13 -07001843 /* handle the new child who might not be in core->children yet */
1844 if (core->new_child)
Taniya Das083a1982016-06-14 16:39:56 +05301845 rc = clk_change_rate(core->new_child);
1846
1847 return rc;
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301848
1849err_set_rate:
1850 if (core->prepare_count)
1851 clk_unvote_rate_vdd(core, core->new_rate);
Taniya Das083a1982016-06-14 16:39:56 +05301852out:
1853 trace_clk_set_rate_complete(core, core->new_rate);
1854
1855 return rc;
Mike Turquetteb24764902012-03-15 23:11:19 -07001856}
1857
Stephen Boydd6968fc2015-04-30 13:54:13 -07001858static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001859 unsigned long req_rate)
1860{
1861 struct clk_core *top, *fail_clk;
1862 unsigned long rate = req_rate;
Taniya Das083a1982016-06-14 16:39:56 +05301863 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001864
Stephen Boydd6968fc2015-04-30 13:54:13 -07001865 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001866 return 0;
1867
1868 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001869 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001870 return 0;
1871
Stephen Boydd6968fc2015-04-30 13:54:13 -07001872 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001873 return -EBUSY;
1874
1875 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001876 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001877 if (!top)
1878 return -EINVAL;
1879
1880 /* notify that we are about to change rates */
1881 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1882 if (fail_clk) {
Taniya Das083a1982016-06-14 16:39:56 +05301883 pr_debug("%s: failed to set %s clock to run at %lu\n", __func__,
1884 fail_clk->name, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001885 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1886 return -EBUSY;
1887 }
1888
1889 /* change the rates */
Taniya Das083a1982016-06-14 16:39:56 +05301890 ret = clk_change_rate(top);
1891 if (ret) {
1892 pr_err("%s: failed to set %s clock to run at %lu\n", __func__,
1893 top->name, req_rate);
1894 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1895 return ret;
1896 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001897
Stephen Boydd6968fc2015-04-30 13:54:13 -07001898 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001899
Taniya Das083a1982016-06-14 16:39:56 +05301900 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001901}
1902
Mike Turquetteb24764902012-03-15 23:11:19 -07001903/**
1904 * clk_set_rate - specify a new rate for clk
1905 * @clk: the clk whose rate is being changed
1906 * @rate: the new rate for clk
1907 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001908 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001909 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001910 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1911 * propagate up to clk's parent; whether or not this happens depends on the
1912 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1913 * after calling .round_rate then upstream parent propagation is ignored. If
1914 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001915 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001916 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1917 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001918 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001919 * Rate changes are accomplished via tree traversal that also recalculates the
1920 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001921 *
1922 * Returns 0 on success, -EERROR otherwise.
1923 */
1924int clk_set_rate(struct clk *clk, unsigned long rate)
1925{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001926 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001927
Mike Turquette89ac8d72013-08-21 23:58:09 -07001928 if (!clk)
1929 return 0;
1930
Mike Turquetteb24764902012-03-15 23:11:19 -07001931 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001932 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001933
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001934 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001935
Mike Turquetteeab89f62013-03-28 13:59:01 -07001936 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001937
1938 return ret;
1939}
1940EXPORT_SYMBOL_GPL(clk_set_rate);
1941
1942/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001943 * clk_set_rate_range - set a rate range for a clock source
1944 * @clk: clock source
1945 * @min: desired minimum clock rate in Hz, inclusive
1946 * @max: desired maximum clock rate in Hz, inclusive
1947 *
1948 * Returns success (0) or negative errno.
1949 */
1950int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1951{
1952 int ret = 0;
1953
1954 if (!clk)
1955 return 0;
1956
1957 if (min > max) {
1958 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1959 __func__, clk->core->name, clk->dev_id, clk->con_id,
1960 min, max);
1961 return -EINVAL;
1962 }
1963
1964 clk_prepare_lock();
1965
1966 if (min != clk->min_rate || max != clk->max_rate) {
1967 clk->min_rate = min;
1968 clk->max_rate = max;
1969 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1970 }
1971
1972 clk_prepare_unlock();
1973
1974 return ret;
1975}
1976EXPORT_SYMBOL_GPL(clk_set_rate_range);
1977
1978/**
1979 * clk_set_min_rate - set a minimum clock rate for a clock source
1980 * @clk: clock source
1981 * @rate: desired minimum clock rate in Hz, inclusive
1982 *
1983 * Returns success (0) or negative errno.
1984 */
1985int clk_set_min_rate(struct clk *clk, unsigned long rate)
1986{
1987 if (!clk)
1988 return 0;
1989
1990 return clk_set_rate_range(clk, rate, clk->max_rate);
1991}
1992EXPORT_SYMBOL_GPL(clk_set_min_rate);
1993
1994/**
1995 * clk_set_max_rate - set a maximum clock rate for a clock source
1996 * @clk: clock source
1997 * @rate: desired maximum clock rate in Hz, inclusive
1998 *
1999 * Returns success (0) or negative errno.
2000 */
2001int clk_set_max_rate(struct clk *clk, unsigned long rate)
2002{
2003 if (!clk)
2004 return 0;
2005
2006 return clk_set_rate_range(clk, clk->min_rate, rate);
2007}
2008EXPORT_SYMBOL_GPL(clk_set_max_rate);
2009
2010/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002011 * clk_get_parent - return the parent of a clk
2012 * @clk: the clk whose parent gets returned
2013 *
2014 * Simply returns clk->parent. Returns NULL if clk is NULL.
2015 */
2016struct clk *clk_get_parent(struct clk *clk)
2017{
2018 struct clk *parent;
2019
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002020 if (!clk)
2021 return NULL;
2022
Mike Turquetteeab89f62013-03-28 13:59:01 -07002023 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002024 /* TODO: Create a per-user clk and change callers to call clk_put */
2025 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002026 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002027
2028 return parent;
2029}
2030EXPORT_SYMBOL_GPL(clk_get_parent);
2031
Stephen Boydd6968fc2015-04-30 13:54:13 -07002032static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002033{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002034 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002035
Masahiro Yamada2430a942016-02-09 20:19:14 +09002036 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002037 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002038
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002039 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002040}
2041
Stephen Boydd6968fc2015-04-30 13:54:13 -07002042static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002043 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002044{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002045 clk_reparent(core, new_parent);
2046 __clk_recalc_accuracies(core);
2047 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002048}
2049
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002050void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2051{
2052 if (!hw)
2053 return;
2054
2055 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2056}
2057
Mike Turquetteb24764902012-03-15 23:11:19 -07002058/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002059 * clk_has_parent - check if a clock is a possible parent for another
2060 * @clk: clock source
2061 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002062 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002063 * This function can be used in drivers that need to check that a clock can be
2064 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002065 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002066 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002067 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002068bool clk_has_parent(struct clk *clk, struct clk *parent)
2069{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002070 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002071 unsigned int i;
2072
2073 /* NULL clocks should be nops, so return success if either is NULL. */
2074 if (!clk || !parent)
2075 return true;
2076
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002077 core = clk->core;
2078 parent_core = parent->core;
2079
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002080 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002081 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002082 return true;
2083
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002084 for (i = 0; i < core->num_parents; i++)
2085 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002086 return true;
2087
2088 return false;
2089}
2090EXPORT_SYMBOL_GPL(clk_has_parent);
2091
Stephen Boydd6968fc2015-04-30 13:54:13 -07002092static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002093{
2094 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002095 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002096 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002097
Stephen Boydd6968fc2015-04-30 13:54:13 -07002098 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002099 return 0;
2100
Mike Turquetteb24764902012-03-15 23:11:19 -07002101 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002102 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002103
Stephen Boydd6968fc2015-04-30 13:54:13 -07002104 if (core->parent == parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002105 goto out;
2106
Stephen Boydb61c43c2015-02-02 14:11:25 -08002107 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002108 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08002109 ret = -ENOSYS;
2110 goto out;
2111 }
2112
Ulf Hansson031dcc92013-04-02 23:09:38 +02002113 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002114 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002115 ret = -EBUSY;
2116 goto out;
2117 }
2118
2119 /* try finding the new parent index */
2120 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002121 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002122 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002123 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002124 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002125 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002126 goto out;
2127 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002128 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002129 }
2130
Mike Turquetteb24764902012-03-15 23:11:19 -07002131 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002132 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002133
2134 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002135 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07002136 goto out;
2137
Ulf Hansson031dcc92013-04-02 23:09:38 +02002138 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002139 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002140
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002141 /* propagate rate an accuracy recalculation accordingly */
2142 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002143 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002144 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002145 __clk_recalc_rates(core, POST_RATE_CHANGE);
2146 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002147 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002148
2149out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002150 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002151
2152 return ret;
2153}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002154
2155/**
2156 * clk_set_parent - switch the parent of a mux clk
2157 * @clk: the mux clk whose input we are switching
2158 * @parent: the new input to clk
2159 *
2160 * Re-parent clk to use parent as its new input source. If clk is in
2161 * prepared state, the clk will get enabled for the duration of this call. If
2162 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2163 * that, the reparenting is glitchy in hardware, etc), use the
2164 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2165 *
2166 * After successfully changing clk's parent clk_set_parent will update the
2167 * clk topology, sysfs topology and propagate rate recalculation via
2168 * __clk_recalc_rates.
2169 *
2170 * Returns 0 on success, -EERROR otherwise.
2171 */
2172int clk_set_parent(struct clk *clk, struct clk *parent)
2173{
2174 if (!clk)
2175 return 0;
2176
2177 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
2178}
Mike Turquetteb24764902012-03-15 23:11:19 -07002179EXPORT_SYMBOL_GPL(clk_set_parent);
2180
2181/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002182 * clk_set_phase - adjust the phase shift of a clock signal
2183 * @clk: clock signal source
2184 * @degrees: number of degrees the signal is shifted
2185 *
2186 * Shifts the phase of a clock signal by the specified
2187 * degrees. Returns 0 on success, -EERROR otherwise.
2188 *
2189 * This function makes no distinction about the input or reference
2190 * signal that we adjust the clock signal phase against. For example
2191 * phase locked-loop clock signal generators we may shift phase with
2192 * respect to feedback clock signal input, but for other cases the
2193 * clock phase may be shifted with respect to some other, unspecified
2194 * signal.
2195 *
2196 * Additionally the concept of phase shift does not propagate through
2197 * the clock tree hierarchy, which sets it apart from clock rates and
2198 * clock accuracy. A parent clock phase attribute does not have an
2199 * impact on the phase attribute of a child clock.
2200 */
2201int clk_set_phase(struct clk *clk, int degrees)
2202{
Stephen Boyd08b95752015-02-02 14:09:43 -08002203 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08002204
2205 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002206 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002207
2208 /* sanity check degrees */
2209 degrees %= 360;
2210 if (degrees < 0)
2211 degrees += 360;
2212
2213 clk_prepare_lock();
2214
Stephen Boyddfc202e2015-02-02 14:37:41 -08002215 trace_clk_set_phase(clk->core, degrees);
2216
Stephen Boyd08b95752015-02-02 14:09:43 -08002217 if (clk->core->ops->set_phase)
2218 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002219
Stephen Boyddfc202e2015-02-02 14:37:41 -08002220 trace_clk_set_phase_complete(clk->core, degrees);
2221
Mike Turquettee59c5372014-02-18 21:21:25 -08002222 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002223 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08002224
Mike Turquettee59c5372014-02-18 21:21:25 -08002225 clk_prepare_unlock();
2226
Mike Turquettee59c5372014-02-18 21:21:25 -08002227 return ret;
2228}
Maxime Ripard9767b042015-01-20 22:23:43 +01002229EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002230
Stephen Boydd6968fc2015-04-30 13:54:13 -07002231static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002232{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002233 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002234
2235 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07002236 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002237 clk_prepare_unlock();
2238
Mike Turquettee59c5372014-02-18 21:21:25 -08002239 return ret;
2240}
2241
2242/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002243 * clk_get_phase - return the phase shift of a clock signal
2244 * @clk: clock signal source
2245 *
2246 * Returns the phase shift of a clock node in degrees, otherwise returns
2247 * -EERROR.
2248 */
2249int clk_get_phase(struct clk *clk)
2250{
2251 if (!clk)
2252 return 0;
2253
2254 return clk_core_get_phase(clk->core);
2255}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002256EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002257
2258/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002259 * clk_is_match - check if two clk's point to the same hardware clock
2260 * @p: clk compared against q
2261 * @q: clk compared against p
2262 *
2263 * Returns true if the two struct clk pointers both point to the same hardware
2264 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2265 * share the same struct clk_core object.
2266 *
2267 * Returns false otherwise. Note that two NULL clks are treated as matching.
2268 */
2269bool clk_is_match(const struct clk *p, const struct clk *q)
2270{
2271 /* trivial case: identical struct clk's or both NULL */
2272 if (p == q)
2273 return true;
2274
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002275 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002276 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2277 if (p->core == q->core)
2278 return true;
2279
2280 return false;
2281}
2282EXPORT_SYMBOL_GPL(clk_is_match);
2283
Taniya Das63c20c72016-06-15 12:15:01 +05302284int clk_set_flags(struct clk *clk, unsigned long flags)
2285{
2286 if (!clk)
2287 return 0;
2288
2289 if (!clk->core->ops->set_flags)
2290 return -EINVAL;
2291
2292 return clk->core->ops->set_flags(clk->core->hw, flags);
2293}
2294EXPORT_SYMBOL_GPL(clk_set_flags);
2295
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002296/*** debugfs support ***/
2297
2298#ifdef CONFIG_DEBUG_FS
2299#include <linux/debugfs.h>
2300
2301static struct dentry *rootdir;
2302static int inited = 0;
Taniya Das6c15e542016-11-07 10:08:30 +05302303static u32 debug_suspend;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002304static DEFINE_MUTEX(clk_debug_lock);
2305static HLIST_HEAD(clk_debug_list);
2306
2307static struct hlist_head *all_lists[] = {
2308 &clk_root_list,
2309 &clk_orphan_list,
2310 NULL,
2311};
2312
2313static struct hlist_head *orphan_list[] = {
2314 &clk_orphan_list,
2315 NULL,
2316};
2317
2318static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2319 int level)
2320{
2321 if (!c)
2322 return;
2323
2324 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
2325 level * 3 + 1, "",
2326 30 - level * 3, c->name,
2327 c->enable_count, c->prepare_count, clk_core_get_rate(c),
2328 clk_core_get_accuracy(c), clk_core_get_phase(c));
2329}
2330
2331static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2332 int level)
2333{
2334 struct clk_core *child;
2335
2336 if (!c)
2337 return;
2338
2339 clk_summary_show_one(s, c, level);
2340
2341 hlist_for_each_entry(child, &c->children, child_node)
2342 clk_summary_show_subtree(s, child, level + 1);
2343}
2344
2345static int clk_summary_show(struct seq_file *s, void *data)
2346{
2347 struct clk_core *c;
2348 struct hlist_head **lists = (struct hlist_head **)s->private;
2349
2350 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
2351 seq_puts(s, "----------------------------------------------------------------------------------------\n");
2352
2353 clk_prepare_lock();
2354
2355 for (; *lists; lists++)
2356 hlist_for_each_entry(c, *lists, child_node)
2357 clk_summary_show_subtree(s, c, 0);
2358
2359 clk_prepare_unlock();
2360
2361 return 0;
2362}
2363
2364
2365static int clk_summary_open(struct inode *inode, struct file *file)
2366{
2367 return single_open(file, clk_summary_show, inode->i_private);
2368}
2369
2370static const struct file_operations clk_summary_fops = {
2371 .open = clk_summary_open,
2372 .read = seq_read,
2373 .llseek = seq_lseek,
2374 .release = single_release,
2375};
2376
2377static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2378{
2379 if (!c)
2380 return;
2381
Stefan Wahren7cb81132015-04-29 16:36:43 +00002382 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002383 seq_printf(s, "\"%s\": { ", c->name);
2384 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2385 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002386 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2387 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002388 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2389}
2390
2391static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2392{
2393 struct clk_core *child;
2394
2395 if (!c)
2396 return;
2397
2398 clk_dump_one(s, c, level);
2399
2400 hlist_for_each_entry(child, &c->children, child_node) {
2401 seq_printf(s, ",");
2402 clk_dump_subtree(s, child, level + 1);
2403 }
2404
2405 seq_printf(s, "}");
2406}
2407
2408static int clk_dump(struct seq_file *s, void *data)
2409{
2410 struct clk_core *c;
2411 bool first_node = true;
2412 struct hlist_head **lists = (struct hlist_head **)s->private;
2413
2414 seq_printf(s, "{");
2415
2416 clk_prepare_lock();
2417
2418 for (; *lists; lists++) {
2419 hlist_for_each_entry(c, *lists, child_node) {
2420 if (!first_node)
2421 seq_puts(s, ",");
2422 first_node = false;
2423 clk_dump_subtree(s, c, 0);
2424 }
2425 }
2426
2427 clk_prepare_unlock();
2428
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002429 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002430 return 0;
2431}
2432
2433
2434static int clk_dump_open(struct inode *inode, struct file *file)
2435{
2436 return single_open(file, clk_dump, inode->i_private);
2437}
2438
2439static const struct file_operations clk_dump_fops = {
2440 .open = clk_dump_open,
2441 .read = seq_read,
2442 .llseek = seq_lseek,
2443 .release = single_release,
2444};
2445
Taniya Das2dd25722016-11-14 11:26:02 +05302446static int clock_debug_rate_set(void *data, u64 val)
2447{
2448 struct clk_core *core = data;
2449 int ret;
2450
2451 ret = clk_set_rate(core->hw->clk, val);
2452 if (ret)
2453 pr_err("clk_set_rate(%lu) failed (%d)\n",
2454 (unsigned long)val, ret);
2455
2456 return ret;
2457}
2458
2459static int clock_debug_rate_get(void *data, u64 *val)
2460{
2461 struct clk_core *core = data;
2462
2463 *val = core->hw->core->rate;
2464
2465 return 0;
2466}
2467
2468DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
2469 clock_debug_rate_set, "%llu\n");
2470
2471static ssize_t clock_parent_read(struct file *filp, char __user *ubuf,
2472 size_t cnt, loff_t *ppos)
2473{
2474 char name[256] = {0};
2475 struct clk_core *core = filp->private_data;
2476 struct clk_core *p = core->hw->core->parent;
2477
2478 snprintf(name, sizeof(name), "%s\n", p ? p->name : "None\n");
2479
2480 return simple_read_from_buffer(ubuf, cnt, ppos, name, strlen(name));
2481}
2482
2483static const struct file_operations clock_parent_fops = {
2484 .open = simple_open,
2485 .read = clock_parent_read,
2486};
2487
2488static int clock_debug_enable_set(void *data, u64 val)
2489{
2490 struct clk_core *core = data;
2491 int rc = 0;
2492
2493 if (val)
2494 rc = clk_prepare_enable(core->hw->clk);
2495 else
2496 clk_disable_unprepare(core->hw->clk);
2497
2498 return rc;
2499}
2500
2501static int clock_debug_enable_get(void *data, u64 *val)
2502{
2503 struct clk_core *core = data;
2504 int enabled = 0;
2505
2506 enabled = core->enable_count;
2507
2508 *val = enabled;
2509
2510 return 0;
2511}
2512
2513DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
2514 clock_debug_enable_set, "%lld\n");
2515
2516#define clock_debug_output(m, c, fmt, ...) \
2517do { \
2518 if (m) \
2519 seq_printf(m, fmt, ##__VA_ARGS__); \
2520 else if (c) \
2521 pr_cont(fmt, ##__VA_ARGS__); \
2522 else \
2523 pr_info(fmt, ##__VA_ARGS__); \
2524} while (0)
2525
2526int clock_debug_print_clock(struct clk_core *c, struct seq_file *s)
2527{
2528 char *start = "";
2529 struct clk *clk;
2530
2531 if (!c || !c->prepare_count)
2532 return 0;
2533
2534 clk = c->hw->clk;
2535
2536 clock_debug_output(s, 0, "\t");
2537
2538 do {
Taniya Das876112d2016-11-14 11:54:02 +05302539 if (clk->core->vdd_class)
2540 clock_debug_output(s, 1, "%s%s:%u:%u [%ld, %d]", start,
2541 clk->core->name,
2542 clk->core->prepare_count,
2543 clk->core->enable_count,
2544 clk->core->rate,
2545 clk_find_vdd_level(clk->core, clk->core->rate));
2546 else
2547 clock_debug_output(s, 1, "%s%s:%u:%u [%ld]", start,
Taniya Das2dd25722016-11-14 11:26:02 +05302548 clk->core->name,
2549 clk->core->prepare_count,
2550 clk->core->enable_count,
2551 clk->core->rate);
2552 start = " -> ";
2553 } while ((clk = clk_get_parent(clk)));
2554
2555 clock_debug_output(s, 1, "\n");
2556
2557 return 1;
2558}
2559
2560/*
2561 * clock_debug_print_enabled_clocks() - Print names of enabled clocks
2562 */
2563static void clock_debug_print_enabled_clocks(struct seq_file *s)
2564{
2565 struct clk_core *core;
2566 int cnt = 0;
2567
2568 clock_debug_output(s, 0, "Enabled clocks:\n");
2569
2570 mutex_lock(&clk_debug_lock);
2571
2572 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2573 cnt += clock_debug_print_clock(core, s);
2574
2575 mutex_unlock(&clk_debug_lock);
2576
2577 if (cnt)
2578 clock_debug_output(s, 0, "Enabled clock count: %d\n", cnt);
2579 else
2580 clock_debug_output(s, 0, "No clocks enabled.\n");
2581}
2582
2583static int enabled_clocks_show(struct seq_file *s, void *unused)
2584{
2585 clock_debug_print_enabled_clocks(s);
2586
2587 return 0;
2588}
2589
2590static int enabled_clocks_open(struct inode *inode, struct file *file)
2591{
2592 return single_open(file, enabled_clocks_show, inode->i_private);
2593}
2594
2595static const struct file_operations clk_enabled_list_fops = {
2596 .open = enabled_clocks_open,
2597 .read = seq_read,
2598 .llseek = seq_lseek,
2599 .release = seq_release,
2600};
2601
2602static void clk_debug_print_hw(struct clk_core *clk, struct seq_file *f)
2603{
2604 if (IS_ERR_OR_NULL(clk))
2605 return;
2606
2607 clk_debug_print_hw(clk->parent, f);
2608
2609 clock_debug_output(f, false, "%s\n", clk->name);
2610
2611 if (!clk->ops->list_registers)
2612 return;
2613
2614 clk->ops->list_registers(f, clk->hw);
2615}
2616
2617static int print_hw_show(struct seq_file *m, void *unused)
2618{
2619 struct clk_core *c = m->private;
2620
2621 clk_debug_print_hw(c, m);
2622
2623 return 0;
2624}
2625
2626static int print_hw_open(struct inode *inode, struct file *file)
2627{
2628 return single_open(file, print_hw_show, inode->i_private);
2629}
2630
2631static const struct file_operations clock_print_hw_fops = {
2632 .open = print_hw_open,
2633 .read = seq_read,
2634 .llseek = seq_lseek,
2635 .release = seq_release,
2636};
2637
Taniya Das876112d2016-11-14 11:54:02 +05302638static int list_rates_show(struct seq_file *s, void *unused)
2639{
2640 struct clk_core *core = s->private;
2641 int level = 0, i = 0;
2642 unsigned long rate, rate_max = 0;
2643
2644 /* Find max frequency supported within voltage constraints. */
2645 if (!core->vdd_class) {
2646 rate_max = ULONG_MAX;
2647 } else {
2648 for (level = 0; level < core->num_rate_max; level++)
2649 if (core->rate_max[level])
2650 rate_max = core->rate_max[level];
2651 }
2652
2653 /*
2654 * List supported frequencies <= rate_max. Higher frequencies may
2655 * appear in the frequency table, but are not valid and should not
2656 * be listed.
2657 */
2658 while (!IS_ERR_VALUE(rate =
2659 core->ops->list_rate(core->hw, i++, rate_max))) {
2660 if (rate <= 0)
2661 break;
2662 if (rate <= rate_max)
2663 seq_printf(s, "%lu\n", rate);
2664 }
2665
2666 return 0;
2667}
2668
2669static int list_rates_open(struct inode *inode, struct file *file)
2670{
2671 return single_open(file, list_rates_show, inode->i_private);
2672}
2673
2674static const struct file_operations list_rates_fops = {
2675 .open = list_rates_open,
2676 .read = seq_read,
2677 .llseek = seq_lseek,
2678 .release = seq_release,
2679};
2680
2681static void clock_print_rate_max_by_level(struct seq_file *s, int level)
2682{
2683 struct clk_core *core = s->private;
2684 struct clk_vdd_class *vdd_class = core->vdd_class;
2685 int off, i, vdd_level, nregs = vdd_class->num_regulators;
2686
2687 vdd_level = clk_find_vdd_level(core, core->rate);
2688
2689 seq_printf(s, "%2s%10lu", vdd_level == level ? "[" : "",
2690 core->rate_max[level]);
2691
2692 for (i = 0; i < nregs; i++) {
2693 off = nregs*level + i;
2694 if (vdd_class->vdd_uv)
2695 seq_printf(s, "%10u", vdd_class->vdd_uv[off]);
2696 }
2697
2698 if (vdd_level == level)
2699 seq_puts(s, "]");
2700
2701 seq_puts(s, "\n");
2702}
2703
2704static int rate_max_show(struct seq_file *s, void *unused)
2705{
2706 struct clk_core *core = s->private;
2707 struct clk_vdd_class *vdd_class = core->vdd_class;
2708 int level = 0, i, nregs = vdd_class->num_regulators;
2709 char reg_name[10];
2710
2711 int vdd_level = clk_find_vdd_level(core, core->rate);
2712
2713 if (vdd_level < 0) {
2714 seq_printf(s, "could not find_vdd_level for %s, %ld\n",
2715 core->name, core->rate);
2716 return 0;
2717 }
2718
2719 seq_printf(s, "%12s", "");
2720 for (i = 0; i < nregs; i++) {
2721 snprintf(reg_name, ARRAY_SIZE(reg_name), "reg %d", i);
2722 seq_printf(s, "%10s", reg_name);
2723 }
2724
2725 seq_printf(s, "\n%12s", "freq");
2726 for (i = 0; i < nregs; i++)
2727 seq_printf(s, "%10s", "uV");
2728
2729 seq_puts(s, "\n");
2730
2731 for (level = 0; level < core->num_rate_max; level++)
2732 clock_print_rate_max_by_level(s, level);
2733
2734 return 0;
2735}
2736
2737static int rate_max_open(struct inode *inode, struct file *file)
2738{
2739 return single_open(file, rate_max_show, inode->i_private);
2740}
2741
2742static const struct file_operations rate_max_fops = {
2743 .open = rate_max_open,
2744 .read = seq_read,
2745 .llseek = seq_lseek,
2746 .release = seq_release,
2747};
2748
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002749static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2750{
2751 struct dentry *d;
2752 int ret = -ENOMEM;
2753
2754 if (!core || !pdentry) {
2755 ret = -EINVAL;
2756 goto out;
2757 }
2758
2759 d = debugfs_create_dir(core->name, pdentry);
2760 if (!d)
2761 goto out;
2762
2763 core->dentry = d;
2764
Taniya Das2dd25722016-11-14 11:26:02 +05302765 d = debugfs_create_file("clk_rate", 0444, core->dentry, core,
2766 &clock_rate_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002767 if (!d)
2768 goto err_out;
2769
Taniya Das876112d2016-11-14 11:54:02 +05302770 if (core->ops->list_rate) {
2771 if (!debugfs_create_file("clk_list_rates",
2772 0444, core->dentry, core, &list_rates_fops))
2773 goto err_out;
2774 }
2775
2776 if (core->vdd_class && !debugfs_create_file("clk_rate_max",
2777 0444, core->dentry, core, &rate_max_fops))
2778 goto err_out;
2779
Taniya Das2dd25722016-11-14 11:26:02 +05302780 d = debugfs_create_u32("clk_accuracy", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002781 (u32 *)&core->accuracy);
2782 if (!d)
2783 goto err_out;
2784
Taniya Das2dd25722016-11-14 11:26:02 +05302785 d = debugfs_create_u32("clk_phase", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002786 (u32 *)&core->phase);
2787 if (!d)
2788 goto err_out;
2789
Taniya Das2dd25722016-11-14 11:26:02 +05302790 d = debugfs_create_x32("clk_flags", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002791 (u32 *)&core->flags);
2792 if (!d)
2793 goto err_out;
2794
Taniya Das2dd25722016-11-14 11:26:02 +05302795 d = debugfs_create_u32("clk_prepare_count", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002796 (u32 *)&core->prepare_count);
2797 if (!d)
2798 goto err_out;
2799
Taniya Das2dd25722016-11-14 11:26:02 +05302800 d = debugfs_create_file("clk_enable_count", 0444, core->dentry,
2801 core, &clock_enable_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002802 if (!d)
2803 goto err_out;
2804
Taniya Das2dd25722016-11-14 11:26:02 +05302805 d = debugfs_create_u32("clk_notifier_count", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002806 (u32 *)&core->notifier_count);
2807 if (!d)
2808 goto err_out;
2809
Taniya Das2dd25722016-11-14 11:26:02 +05302810 d = debugfs_create_file("clk_parent", 0444, core->dentry, core,
2811 &clock_parent_fops);
2812 if (!d)
2813 goto err_out;
2814
2815 d = debugfs_create_file("clk_print_regs", 0444, core->dentry,
2816 core, &clock_print_hw_fops);
2817 if (!d)
2818 goto err_out;
2819
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002820 if (core->ops->debug_init) {
2821 ret = core->ops->debug_init(core->hw, core->dentry);
2822 if (ret)
2823 goto err_out;
2824 }
2825
2826 ret = 0;
2827 goto out;
2828
2829err_out:
2830 debugfs_remove_recursive(core->dentry);
2831 core->dentry = NULL;
2832out:
2833 return ret;
2834}
2835
2836/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002837 * clk_debug_register - add a clk node to the debugfs clk directory
2838 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002839 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002840 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2841 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002842 * will be created lazily by clk_debug_init as part of a late_initcall.
2843 */
2844static int clk_debug_register(struct clk_core *core)
2845{
2846 int ret = 0;
2847
2848 mutex_lock(&clk_debug_lock);
2849 hlist_add_head(&core->debug_node, &clk_debug_list);
2850
2851 if (!inited)
2852 goto unlock;
2853
2854 ret = clk_debug_create_one(core, rootdir);
2855unlock:
2856 mutex_unlock(&clk_debug_lock);
2857
2858 return ret;
2859}
2860
2861 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002862 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2863 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002864 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002865 * Dynamically removes a clk and all its child nodes from the
2866 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08002867 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002868 */
2869static void clk_debug_unregister(struct clk_core *core)
2870{
2871 mutex_lock(&clk_debug_lock);
2872 hlist_del_init(&core->debug_node);
2873 debugfs_remove_recursive(core->dentry);
2874 core->dentry = NULL;
2875 mutex_unlock(&clk_debug_lock);
2876}
2877
2878struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2879 void *data, const struct file_operations *fops)
2880{
2881 struct dentry *d = NULL;
2882
2883 if (hw->core->dentry)
2884 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2885 fops);
2886
2887 return d;
2888}
2889EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2890
Taniya Das6c15e542016-11-07 10:08:30 +05302891/*
2892 * Print the names of all enabled clocks and their parents if
2893 * debug_suspend is set from debugfs.
2894 */
2895void clock_debug_print_enabled(void)
2896{
2897 if (likely(!debug_suspend))
2898 return;
2899
2900 clock_debug_print_enabled_clocks(NULL);
2901}
2902EXPORT_SYMBOL_GPL(clock_debug_print_enabled);
2903
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002904/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002905 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002906 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002907 * clks are often initialized very early during boot before memory can be
2908 * dynamically allocated and well before debugfs is setup. This function
2909 * populates the debugfs clk directory once at boot-time when we know that
2910 * debugfs is setup. It should only be called once at boot-time, all other clks
2911 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002912 */
2913static int __init clk_debug_init(void)
2914{
2915 struct clk_core *core;
2916 struct dentry *d;
2917
2918 rootdir = debugfs_create_dir("clk", NULL);
2919
2920 if (!rootdir)
2921 return -ENOMEM;
2922
Taniya Das2dd25722016-11-14 11:26:02 +05302923 d = debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002924 &clk_summary_fops);
2925 if (!d)
2926 return -ENOMEM;
2927
Taniya Das2dd25722016-11-14 11:26:02 +05302928 d = debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002929 &clk_dump_fops);
2930 if (!d)
2931 return -ENOMEM;
2932
Taniya Das2dd25722016-11-14 11:26:02 +05302933 d = debugfs_create_file("clk_orphan_summary", 0444, rootdir,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002934 &orphan_list, &clk_summary_fops);
2935 if (!d)
2936 return -ENOMEM;
2937
Taniya Das2dd25722016-11-14 11:26:02 +05302938 d = debugfs_create_file("clk_orphan_dump", 0444, rootdir,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002939 &orphan_list, &clk_dump_fops);
2940 if (!d)
2941 return -ENOMEM;
2942
Taniya Das2dd25722016-11-14 11:26:02 +05302943 d = debugfs_create_file("clk_enabled_list", 0444, rootdir,
2944 &clk_debug_list, &clk_enabled_list_fops);
2945 if (!d)
2946 return -ENOMEM;
2947
Taniya Das6c15e542016-11-07 10:08:30 +05302948
2949 d = debugfs_create_u32("debug_suspend", 0644, rootdir, &debug_suspend);
2950 if (!d)
2951 return -ENOMEM;
2952
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002953 mutex_lock(&clk_debug_lock);
2954 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2955 clk_debug_create_one(core, rootdir);
2956
2957 inited = 1;
2958 mutex_unlock(&clk_debug_lock);
2959
2960 return 0;
2961}
2962late_initcall(clk_debug_init);
2963#else
2964static inline int clk_debug_register(struct clk_core *core) { return 0; }
2965static inline void clk_debug_reparent(struct clk_core *core,
2966 struct clk_core *new_parent)
2967{
2968}
2969static inline void clk_debug_unregister(struct clk_core *core)
2970{
2971}
2972#endif
2973
Michael Turquette3d3801e2015-02-25 09:11:01 -08002974/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002975 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002976 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07002977 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002978 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002979 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002980 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09002981static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002982{
Mike Turquetted1302a32012-03-29 14:30:40 -07002983 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002984 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002985 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002986 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002987
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09002988 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07002989 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002990
Mike Turquetteeab89f62013-03-28 13:59:01 -07002991 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002992
2993 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002994 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002995 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002996 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002997 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002998 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002999 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003000
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003001 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003002 if (core->ops->set_rate &&
3003 !((core->ops->round_rate || core->ops->determine_rate) &&
3004 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003005 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3006 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003007 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003008 goto out;
3009 }
3010
Stephen Boydd6968fc2015-04-30 13:54:13 -07003011 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003012 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3013 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003014 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003015 goto out;
3016 }
3017
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09003018 if (core->num_parents > 1 && !core->ops->get_parent) {
3019 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3020 __func__, core->name);
3021 ret = -EINVAL;
3022 goto out;
3023 }
3024
Stephen Boydd6968fc2015-04-30 13:54:13 -07003025 if (core->ops->set_rate_and_parent &&
3026 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003027 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003028 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08003029 ret = -EINVAL;
3030 goto out;
3031 }
3032
Mike Turquetteb24764902012-03-15 23:11:19 -07003033 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003034 for (i = 0; i < core->num_parents; i++)
3035 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07003036 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003037 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07003038
Stephen Boydd6968fc2015-04-30 13:54:13 -07003039 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07003040
3041 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08003042 * Populate core->parent if parent has already been clk_core_init'd. If
3043 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003044 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07003045 * clk list.
3046 *
3047 * Every time a new clk is clk_init'd then we walk the list of orphan
3048 * clocks and re-parent any that are children of the clock currently
3049 * being clk_init'd.
3050 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02003051 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003052 hlist_add_head(&core->child_node,
3053 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003054 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003055 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003056 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003057 core->orphan = false;
3058 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003059 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003060 core->orphan = true;
3061 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003062
3063 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003064 * Set clk's accuracy. The preferred method is to use
3065 * .recalc_accuracy. For simple clocks and lazy developers the default
3066 * fallback is to use the parent's accuracy. If a clock doesn't have a
3067 * parent (or is orphaned) then accuracy is set to zero (perfect
3068 * clock).
3069 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003070 if (core->ops->recalc_accuracy)
3071 core->accuracy = core->ops->recalc_accuracy(core->hw,
3072 __clk_get_accuracy(core->parent));
3073 else if (core->parent)
3074 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003075 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003076 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003077
3078 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02003079 * Set clk's phase.
3080 * Since a phase is by definition relative to its parent, just
3081 * query the current clock phase, or just assume it's in phase.
3082 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003083 if (core->ops->get_phase)
3084 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02003085 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003086 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02003087
3088 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07003089 * Set clk's rate. The preferred method is to use .recalc_rate. For
3090 * simple clocks and lazy developers the default fallback is to use the
3091 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3092 * then rate is set to zero.
3093 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003094 if (core->ops->recalc_rate)
3095 rate = core->ops->recalc_rate(core->hw,
3096 clk_core_get_rate_nolock(core->parent));
3097 else if (core->parent)
3098 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003099 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003100 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003101 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003102
3103 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003104 * walk the list of orphan clocks and reparent any that newly finds a
3105 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07003106 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08003107 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003108 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01003109
Michael Turquette904e6ea2016-07-08 16:32:10 -07003110 /*
3111 * we could call __clk_set_parent, but that would result in a
3112 * redundant call to the .set_rate op, if it exists
3113 */
3114 if (parent) {
3115 __clk_set_parent_before(orphan, parent);
3116 __clk_set_parent_after(orphan, parent, NULL);
3117 __clk_recalc_accuracies(orphan);
3118 __clk_recalc_rates(orphan, 0);
3119 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003120 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003121
3122 /*
3123 * optional platform-specific magic
3124 *
3125 * The .init callback is not used by any of the basic clock types, but
3126 * exists for weird hardware that must perform initialization magic.
3127 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02003128 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07003129 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003130 if (core->ops->init)
3131 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07003132
Lee Jones32b9b102016-02-11 13:19:09 -08003133 if (core->flags & CLK_IS_CRITICAL) {
Maxime Ripardef56b792016-05-13 10:00:31 +02003134 unsigned long flags;
3135
Lee Jones32b9b102016-02-11 13:19:09 -08003136 clk_core_prepare(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02003137
3138 flags = clk_enable_lock();
Lee Jones32b9b102016-02-11 13:19:09 -08003139 clk_core_enable(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02003140 clk_enable_unlock(flags);
Lee Jones32b9b102016-02-11 13:19:09 -08003141 }
3142
Stephen Boydd6968fc2015-04-30 13:54:13 -07003143 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07003144out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003145 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003146
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003147 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003148 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003149
Mike Turquetted1302a32012-03-29 14:30:40 -07003150 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07003151}
3152
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003153struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
3154 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003155{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003156 struct clk *clk;
3157
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003158 /* This is to allow this function to be chained to others */
Masahiro Yamadac1de1352015-11-20 14:38:49 +09003159 if (IS_ERR_OR_NULL(hw))
Masahiro Yamada8a231332016-07-19 16:28:47 +09003160 return ERR_CAST(hw);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003161
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003162 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3163 if (!clk)
3164 return ERR_PTR(-ENOMEM);
3165
3166 clk->core = hw->core;
3167 clk->dev_id = dev_id;
3168 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003169 clk->max_rate = ULONG_MAX;
3170
3171 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08003172 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003173 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003174
3175 return clk;
3176}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003177
Stephen Boyd73e0e492015-02-06 11:42:43 -08003178void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003179{
3180 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08003181 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003182 clk_prepare_unlock();
3183
3184 kfree(clk);
3185}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003186
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003187/**
3188 * clk_register - allocate a new clock, register it and return an opaque cookie
3189 * @dev: device that is registering this clock
3190 * @hw: link to hardware-specific clock data
3191 *
3192 * clk_register is the primary interface for populating the clock tree with new
3193 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05303194 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003195 * rest of the clock API. In the event of an error clk_register will return an
3196 * error code; drivers must test for an error code after calling clk_register.
3197 */
3198struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07003199{
Mike Turquetted1302a32012-03-29 14:30:40 -07003200 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003201 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003202
Stephen Boydd6968fc2015-04-30 13:54:13 -07003203 core = kzalloc(sizeof(*core), GFP_KERNEL);
3204 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003205 ret = -ENOMEM;
3206 goto fail_out;
3207 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003208
Stephen Boydd6968fc2015-04-30 13:54:13 -07003209 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3210 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003211 ret = -ENOMEM;
3212 goto fail_name;
3213 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07003214 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003215 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003216 core->owner = dev->driver->owner;
3217 core->hw = hw;
3218 core->flags = hw->init->flags;
3219 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003220 core->min_rate = 0;
3221 core->max_rate = ULONG_MAX;
Stephen Boyd5cb05a12016-05-16 11:05:16 +05303222 core->vdd_class = hw->init->vdd_class;
3223 core->rate_max = hw->init->rate_max;
3224 core->num_rate_max = hw->init->num_rate_max;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003225 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003226
Stephen Boyd5cb05a12016-05-16 11:05:16 +05303227 if (core->vdd_class) {
3228 ret = clk_vdd_class_init(core->vdd_class);
3229 if (ret) {
3230 pr_err("Failed to initialize vdd class\n");
3231 goto fail_parent_names;
3232 }
3233 }
3234
Mike Turquetted1302a32012-03-29 14:30:40 -07003235 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003236 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02003237 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003238
Stephen Boydd6968fc2015-04-30 13:54:13 -07003239 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003240 ret = -ENOMEM;
3241 goto fail_parent_names;
3242 }
3243
3244
3245 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003246 for (i = 0; i < core->num_parents; i++) {
3247 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003248 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003249 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003250 ret = -ENOMEM;
3251 goto fail_parent_names_copy;
3252 }
3253 }
3254
Masahiro Yamada176d1162015-12-28 19:23:00 +09003255 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3256 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3257 GFP_KERNEL);
3258 if (!core->parents) {
3259 ret = -ENOMEM;
3260 goto fail_parents;
3261 };
3262
Stephen Boydd6968fc2015-04-30 13:54:13 -07003263 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003264
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003265 hw->clk = __clk_create_clk(hw, NULL, NULL);
3266 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003267 ret = PTR_ERR(hw->clk);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003268 goto fail_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003269 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003270
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003271 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003272 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003273 return hw->clk;
3274
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003275 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003276 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003277
Masahiro Yamada176d1162015-12-28 19:23:00 +09003278fail_parents:
3279 kfree(core->parents);
Mike Turquetted1302a32012-03-29 14:30:40 -07003280fail_parent_names_copy:
3281 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003282 kfree_const(core->parent_names[i]);
3283 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07003284fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003285 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003286fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003287 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003288fail_out:
3289 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003290}
3291EXPORT_SYMBOL_GPL(clk_register);
3292
Stephen Boyd41438042016-02-05 17:02:52 -08003293/**
3294 * clk_hw_register - register a clk_hw and return an error code
3295 * @dev: device that is registering this clock
3296 * @hw: link to hardware-specific clock data
3297 *
3298 * clk_hw_register is the primary interface for populating the clock tree with
3299 * new clock nodes. It returns an integer equal to zero indicating success or
3300 * less than zero indicating failure. Drivers must test for an error code after
3301 * calling clk_hw_register().
3302 */
3303int clk_hw_register(struct device *dev, struct clk_hw *hw)
3304{
3305 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3306}
3307EXPORT_SYMBOL_GPL(clk_hw_register);
3308
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003309/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003310static void __clk_release(struct kref *ref)
3311{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003312 struct clk_core *core = container_of(ref, struct clk_core, ref);
3313 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003314
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003315 lockdep_assert_held(&prepare_lock);
3316
Stephen Boydd6968fc2015-04-30 13:54:13 -07003317 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003318 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003319 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003320
Stephen Boydd6968fc2015-04-30 13:54:13 -07003321 kfree(core->parent_names);
3322 kfree_const(core->name);
3323 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003324}
3325
3326/*
3327 * Empty clk_ops for unregistered clocks. These are used temporarily
3328 * after clk_unregister() was called on a clock and until last clock
3329 * consumer calls clk_put() and the struct clk object is freed.
3330 */
3331static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3332{
3333 return -ENXIO;
3334}
3335
3336static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3337{
3338 WARN_ON_ONCE(1);
3339}
3340
3341static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3342 unsigned long parent_rate)
3343{
3344 return -ENXIO;
3345}
3346
3347static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3348{
3349 return -ENXIO;
3350}
3351
3352static const struct clk_ops clk_nodrv_ops = {
3353 .enable = clk_nodrv_prepare_enable,
3354 .disable = clk_nodrv_disable_unprepare,
3355 .prepare = clk_nodrv_prepare_enable,
3356 .unprepare = clk_nodrv_disable_unprepare,
3357 .set_rate = clk_nodrv_set_rate,
3358 .set_parent = clk_nodrv_set_parent,
3359};
3360
Mark Brown1df5c932012-04-18 09:07:12 +01003361/**
3362 * clk_unregister - unregister a currently registered clock
3363 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003364 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003365void clk_unregister(struct clk *clk)
3366{
3367 unsigned long flags;
3368
Stephen Boyd6314b672014-09-04 23:37:49 -07003369 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3370 return;
3371
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003372 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003373
3374 clk_prepare_lock();
3375
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003376 if (clk->core->ops == &clk_nodrv_ops) {
3377 pr_err("%s: unregistered clock: %s\n", __func__,
3378 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003379 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003380 }
3381 /*
3382 * Assign empty clock ops for consumers that might still hold
3383 * a reference to this clock.
3384 */
3385 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003386 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003387 clk_enable_unlock(flags);
3388
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003389 if (!hlist_empty(&clk->core->children)) {
3390 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003391 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003392
3393 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003394 hlist_for_each_entry_safe(child, t, &clk->core->children,
3395 child_node)
3396 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003397 }
3398
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003399 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003400
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003401 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003402 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003403 __func__, clk->core->name);
3404 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003405unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003406 clk_prepare_unlock();
3407}
Mark Brown1df5c932012-04-18 09:07:12 +01003408EXPORT_SYMBOL_GPL(clk_unregister);
3409
Stephen Boyd41438042016-02-05 17:02:52 -08003410/**
3411 * clk_hw_unregister - unregister a currently registered clk_hw
3412 * @hw: hardware-specific clock data to unregister
3413 */
3414void clk_hw_unregister(struct clk_hw *hw)
3415{
3416 clk_unregister(hw->clk);
3417}
3418EXPORT_SYMBOL_GPL(clk_hw_unregister);
3419
Stephen Boyd46c87732012-09-24 13:38:04 -07003420static void devm_clk_release(struct device *dev, void *res)
3421{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003422 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003423}
3424
Stephen Boyd41438042016-02-05 17:02:52 -08003425static void devm_clk_hw_release(struct device *dev, void *res)
3426{
3427 clk_hw_unregister(*(struct clk_hw **)res);
3428}
3429
Deepak Katragadda677bad12016-11-04 13:33:13 -07003430#define MAX_LEN_OPP_HANDLE 50
3431#define LEN_OPP_HANDLE 16
3432
3433static int derive_device_list(struct device **device_list,
3434 struct clk_core *core,
3435 struct device_node *np,
3436 char *clk_handle_name, int count)
3437{
3438 int j;
3439 struct platform_device *pdev;
3440 struct device_node *dev_node;
3441
3442 for (j = 0; j < count; j++) {
3443 device_list[j] = NULL;
3444 dev_node = of_parse_phandle(np, clk_handle_name, j);
3445 if (!dev_node) {
3446 pr_err("Unable to get device_node pointer for %s opp-handle (%s)\n",
3447 core->name, clk_handle_name);
3448 return -ENODEV;
3449 }
3450
3451 pdev = of_find_device_by_node(dev_node);
3452 if (!pdev) {
3453 pr_err("Unable to find platform_device node for %s opp-handle\n",
3454 core->name);
3455 return -ENODEV;
3456 }
3457 device_list[j] = &pdev->dev;
3458 }
3459 return 0;
3460}
3461
3462static int clk_get_voltage(struct clk_core *core, unsigned long rate, int n)
3463{
3464 struct clk_vdd_class *vdd;
3465 int level, corner;
3466
3467 /* Use the first regulator in the vdd class for the OPP table. */
3468 vdd = core->vdd_class;
3469 if (vdd->num_regulators > 1) {
3470 corner = vdd->vdd_uv[vdd->num_regulators * n];
3471 } else {
3472 level = clk_find_vdd_level(core, rate);
3473 if (level < 0) {
3474 pr_err("Could not find vdd level\n");
3475 return -EINVAL;
3476 }
3477 corner = vdd->vdd_uv[level];
3478 }
3479
3480 if (!corner) {
3481 pr_err("%s: Unable to find vdd level for rate %lu\n",
3482 core->name, rate);
3483 return -EINVAL;
3484 }
3485
3486 return corner;
3487}
3488
3489static int clk_add_and_print_opp(struct clk_hw *hw,
3490 struct device **device_list, int count,
3491 unsigned long rate, int uv, int n)
3492{
3493 struct clk_core *core = hw->core;
3494 int j, ret = 0;
3495
3496 for (j = 0; j < count; j++) {
3497 ret = dev_pm_opp_add(device_list[j], rate, uv);
3498 if (ret) {
3499 pr_err("%s: couldn't add OPP for %lu - err: %d\n",
3500 core->name, rate, ret);
3501 return ret;
3502 }
3503
3504 if (n == 0 || n == core->num_rate_max - 1 ||
3505 rate == clk_hw_round_rate(hw, INT_MAX))
3506 pr_info("%s: set OPP pair(%lu Hz: %u uV) on %s\n",
3507 core->name, rate, uv,
3508 dev_name(device_list[j]));
3509 }
3510 return ret;
3511}
3512
3513static void clk_populate_clock_opp_table(struct device_node *np,
3514 struct clk_hw *hw)
3515{
3516 struct device **device_list;
3517 struct clk_core *core = hw->core;
3518 char clk_handle_name[MAX_LEN_OPP_HANDLE];
3519 int n, len, count, uv;
3520 unsigned long rate = 0, ret = 0;
3521
3522 if (!core || !core->num_rate_max)
3523 return;
3524
3525 if (strlen(core->name) + LEN_OPP_HANDLE < MAX_LEN_OPP_HANDLE) {
3526 ret = snprintf(clk_handle_name, ARRAY_SIZE(clk_handle_name),
3527 "qcom,%s-opp-handle", core->name);
3528 if (ret < strlen(core->name) + LEN_OPP_HANDLE) {
3529 pr_err("%s: Failed to hold clk_handle_name\n",
3530 core->name);
3531 return;
3532 }
3533 } else {
3534 pr_err("clk name (%s) too large to fit in clk_handle_name\n",
3535 core->name);
3536 return;
3537 }
3538
3539 if (of_find_property(np, clk_handle_name, &len)) {
3540 count = len/sizeof(u32);
3541
3542 device_list = kmalloc_array(count, sizeof(struct device *),
3543 GFP_KERNEL);
3544 if (!device_list)
3545 return;
3546
3547 ret = derive_device_list(device_list, core, np,
3548 clk_handle_name, count);
3549 if (ret < 0) {
3550 pr_err("Failed to fill device_list for %s\n",
3551 clk_handle_name);
3552 goto err_derive_device_list;
3553 }
3554 } else {
3555 pr_debug("Unable to find %s\n", clk_handle_name);
3556 return;
3557 }
3558
3559 for (n = 0; ; n++) {
3560 ret = clk_hw_round_rate(hw, rate + 1);
3561 if (ret < 0) {
3562 pr_err("clk_round_rate failed for %s\n",
3563 core->name);
3564 goto err_derive_device_list;
3565 }
3566
3567 /*
3568 * If clk_hw_round_rate gives the same value on consecutive
3569 * iterations, exit the loop since we're at the maximum clock
3570 * frequency.
3571 */
3572 if (rate == ret)
3573 break;
3574 rate = ret;
3575
3576 uv = clk_get_voltage(core, rate, n);
3577 if (uv < 0)
3578 goto err_derive_device_list;
3579
3580 ret = clk_add_and_print_opp(hw, device_list, count,
3581 rate, uv, n);
3582 if (ret)
3583 goto err_derive_device_list;
3584 }
3585
3586err_derive_device_list:
3587 kfree(device_list);
3588}
3589
Stephen Boyd46c87732012-09-24 13:38:04 -07003590/**
3591 * devm_clk_register - resource managed clk_register()
3592 * @dev: device that is registering this clock
3593 * @hw: link to hardware-specific clock data
3594 *
3595 * Managed clk_register(). Clocks returned from this function are
3596 * automatically clk_unregister()ed on driver detach. See clk_register() for
3597 * more information.
3598 */
3599struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3600{
3601 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003602 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003603
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003604 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3605 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003606 return ERR_PTR(-ENOMEM);
3607
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003608 clk = clk_register(dev, hw);
3609 if (!IS_ERR(clk)) {
3610 *clkp = clk;
3611 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003612 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003613 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003614 }
3615
Deepak Katragadda677bad12016-11-04 13:33:13 -07003616 clk_populate_clock_opp_table(dev->of_node, hw);
Stephen Boyd46c87732012-09-24 13:38:04 -07003617 return clk;
3618}
3619EXPORT_SYMBOL_GPL(devm_clk_register);
3620
Stephen Boyd41438042016-02-05 17:02:52 -08003621/**
3622 * devm_clk_hw_register - resource managed clk_hw_register()
3623 * @dev: device that is registering this clock
3624 * @hw: link to hardware-specific clock data
3625 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003626 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003627 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3628 * for more information.
3629 */
3630int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3631{
3632 struct clk_hw **hwp;
3633 int ret;
3634
3635 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3636 if (!hwp)
3637 return -ENOMEM;
3638
3639 ret = clk_hw_register(dev, hw);
3640 if (!ret) {
3641 *hwp = hw;
3642 devres_add(dev, hwp);
3643 } else {
3644 devres_free(hwp);
3645 }
3646
Deepak Katragadda677bad12016-11-04 13:33:13 -07003647 clk_populate_clock_opp_table(dev->of_node, hw);
Stephen Boyd41438042016-02-05 17:02:52 -08003648 return ret;
3649}
3650EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3651
Stephen Boyd46c87732012-09-24 13:38:04 -07003652static int devm_clk_match(struct device *dev, void *res, void *data)
3653{
3654 struct clk *c = res;
3655 if (WARN_ON(!c))
3656 return 0;
3657 return c == data;
3658}
3659
Stephen Boyd41438042016-02-05 17:02:52 -08003660static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3661{
3662 struct clk_hw *hw = res;
3663
3664 if (WARN_ON(!hw))
3665 return 0;
3666 return hw == data;
3667}
3668
Stephen Boyd46c87732012-09-24 13:38:04 -07003669/**
3670 * devm_clk_unregister - resource managed clk_unregister()
3671 * @clk: clock to unregister
3672 *
3673 * Deallocate a clock allocated with devm_clk_register(). Normally
3674 * this function will not need to be called and the resource management
3675 * code will ensure that the resource is freed.
3676 */
3677void devm_clk_unregister(struct device *dev, struct clk *clk)
3678{
3679 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3680}
3681EXPORT_SYMBOL_GPL(devm_clk_unregister);
3682
Stephen Boyd41438042016-02-05 17:02:52 -08003683/**
3684 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3685 * @dev: device that is unregistering the hardware-specific clock data
3686 * @hw: link to hardware-specific clock data
3687 *
3688 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3689 * this function will not need to be called and the resource management
3690 * code will ensure that the resource is freed.
3691 */
3692void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3693{
3694 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3695 hw));
3696}
3697EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3698
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003699/*
3700 * clkdev helpers
3701 */
3702int __clk_get(struct clk *clk)
3703{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003704 struct clk_core *core = !clk ? NULL : clk->core;
3705
3706 if (core) {
3707 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003708 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003709
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003710 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003711 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003712 return 1;
3713}
3714
3715void __clk_put(struct clk *clk)
3716{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003717 struct module *owner;
3718
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003719 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003720 return;
3721
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003722 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003723
Stephen Boyd50595f82015-02-06 11:42:44 -08003724 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003725 if (clk->min_rate > clk->core->req_rate ||
3726 clk->max_rate < clk->core->req_rate)
3727 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3728
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003729 owner = clk->core->owner;
3730 kref_put(&clk->core->ref, __clk_release);
3731
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003732 clk_prepare_unlock();
3733
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003734 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003735
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003736 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003737}
3738
Mike Turquetteb24764902012-03-15 23:11:19 -07003739/*** clk rate change notifiers ***/
3740
3741/**
3742 * clk_notifier_register - add a clk rate change notifier
3743 * @clk: struct clk * to watch
3744 * @nb: struct notifier_block * with callback info
3745 *
3746 * Request notification when clk's rate changes. This uses an SRCU
3747 * notifier because we want it to block and notifier unregistrations are
3748 * uncommon. The callbacks associated with the notifier must not
3749 * re-enter into the clk framework by calling any top-level clk APIs;
3750 * this will cause a nested prepare_lock mutex.
3751 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003752 * In all notification cases (pre, post and abort rate change) the original
3753 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3754 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003755 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003756 * clk_notifier_register() must be called from non-atomic context.
3757 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3758 * allocation failure; otherwise, passes along the return value of
3759 * srcu_notifier_chain_register().
3760 */
3761int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3762{
3763 struct clk_notifier *cn;
3764 int ret = -ENOMEM;
3765
3766 if (!clk || !nb)
3767 return -EINVAL;
3768
Mike Turquetteeab89f62013-03-28 13:59:01 -07003769 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003770
3771 /* search the list of notifiers for this clk */
3772 list_for_each_entry(cn, &clk_notifier_list, node)
3773 if (cn->clk == clk)
3774 break;
3775
3776 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3777 if (cn->clk != clk) {
3778 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
3779 if (!cn)
3780 goto out;
3781
3782 cn->clk = clk;
3783 srcu_init_notifier_head(&cn->notifier_head);
3784
3785 list_add(&cn->node, &clk_notifier_list);
3786 }
3787
3788 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3789
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003790 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003791
3792out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003793 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003794
3795 return ret;
3796}
3797EXPORT_SYMBOL_GPL(clk_notifier_register);
3798
3799/**
3800 * clk_notifier_unregister - remove a clk rate change notifier
3801 * @clk: struct clk *
3802 * @nb: struct notifier_block * with callback info
3803 *
3804 * Request no further notification for changes to 'clk' and frees memory
3805 * allocated in clk_notifier_register.
3806 *
3807 * Returns -EINVAL if called with null arguments; otherwise, passes
3808 * along the return value of srcu_notifier_chain_unregister().
3809 */
3810int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3811{
3812 struct clk_notifier *cn = NULL;
3813 int ret = -EINVAL;
3814
3815 if (!clk || !nb)
3816 return -EINVAL;
3817
Mike Turquetteeab89f62013-03-28 13:59:01 -07003818 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003819
3820 list_for_each_entry(cn, &clk_notifier_list, node)
3821 if (cn->clk == clk)
3822 break;
3823
3824 if (cn->clk == clk) {
3825 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3826
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003827 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07003828
3829 /* XXX the notifier code should handle this better */
3830 if (!cn->notifier_head.head) {
3831 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08003832 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07003833 kfree(cn);
3834 }
3835
3836 } else {
3837 ret = -ENOENT;
3838 }
3839
Mike Turquetteeab89f62013-03-28 13:59:01 -07003840 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003841
3842 return ret;
3843}
3844EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05003845
3846#ifdef CONFIG_OF
3847/**
3848 * struct of_clk_provider - Clock provider registration structure
3849 * @link: Entry in global list of clock providers
3850 * @node: Pointer to device tree node of clock provider
3851 * @get: Get clock callback. Returns NULL or a struct clk for the
3852 * given clock specifier
3853 * @data: context pointer to be passed into @get callback
3854 */
3855struct of_clk_provider {
3856 struct list_head link;
3857
3858 struct device_node *node;
3859 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003860 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05003861 void *data;
3862};
3863
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303864static const struct of_device_id __clk_of_table_sentinel
3865 __used __section(__clk_of_table_end);
3866
Grant Likely766e6a42012-04-09 14:50:06 -05003867static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003868static DEFINE_MUTEX(of_clk_mutex);
3869
Grant Likely766e6a42012-04-09 14:50:06 -05003870struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3871 void *data)
3872{
3873 return data;
3874}
3875EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3876
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003877struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3878{
3879 return data;
3880}
3881EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3882
Shawn Guo494bfec2012-08-22 21:36:27 +08003883struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3884{
3885 struct clk_onecell_data *clk_data = data;
3886 unsigned int idx = clkspec->args[0];
3887
3888 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02003889 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08003890 return ERR_PTR(-EINVAL);
3891 }
3892
3893 return clk_data->clks[idx];
3894}
3895EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3896
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003897struct clk_hw *
3898of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3899{
3900 struct clk_hw_onecell_data *hw_data = data;
3901 unsigned int idx = clkspec->args[0];
3902
3903 if (idx >= hw_data->num) {
3904 pr_err("%s: invalid index %u\n", __func__, idx);
3905 return ERR_PTR(-EINVAL);
3906 }
3907
3908 return hw_data->hws[idx];
3909}
3910EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3911
Grant Likely766e6a42012-04-09 14:50:06 -05003912/**
3913 * of_clk_add_provider() - Register a clock provider for a node
3914 * @np: Device node pointer associated with clock provider
3915 * @clk_src_get: callback for decoding clock
3916 * @data: context pointer for @clk_src_get callback.
3917 */
3918int of_clk_add_provider(struct device_node *np,
3919 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3920 void *data),
3921 void *data)
3922{
3923 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003924 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003925
3926 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
3927 if (!cp)
3928 return -ENOMEM;
3929
3930 cp->node = of_node_get(np);
3931 cp->data = data;
3932 cp->get = clk_src_get;
3933
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003934 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003935 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003936 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003937 pr_debug("Added clock from %s\n", np->full_name);
3938
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003939 ret = of_clk_set_defaults(np, true);
3940 if (ret < 0)
3941 of_clk_del_provider(np);
3942
3943 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05003944}
3945EXPORT_SYMBOL_GPL(of_clk_add_provider);
3946
3947/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08003948 * of_clk_add_hw_provider() - Register a clock provider for a node
3949 * @np: Device node pointer associated with clock provider
3950 * @get: callback for decoding clk_hw
3951 * @data: context pointer for @get callback.
3952 */
3953int of_clk_add_hw_provider(struct device_node *np,
3954 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3955 void *data),
3956 void *data)
3957{
3958 struct of_clk_provider *cp;
3959 int ret;
3960
3961 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3962 if (!cp)
3963 return -ENOMEM;
3964
3965 cp->node = of_node_get(np);
3966 cp->data = data;
3967 cp->get_hw = get;
3968
3969 mutex_lock(&of_clk_mutex);
3970 list_add(&cp->link, &of_clk_providers);
3971 mutex_unlock(&of_clk_mutex);
3972 pr_debug("Added clk_hw provider from %s\n", np->full_name);
3973
3974 ret = of_clk_set_defaults(np, true);
3975 if (ret < 0)
3976 of_clk_del_provider(np);
3977
3978 return ret;
3979}
3980EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3981
3982/**
Grant Likely766e6a42012-04-09 14:50:06 -05003983 * of_clk_del_provider() - Remove a previously registered clock provider
3984 * @np: Device node pointer associated with clock provider
3985 */
3986void of_clk_del_provider(struct device_node *np)
3987{
3988 struct of_clk_provider *cp;
3989
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003990 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05003991 list_for_each_entry(cp, &of_clk_providers, link) {
3992 if (cp->node == np) {
3993 list_del(&cp->link);
3994 of_node_put(cp->node);
3995 kfree(cp);
3996 break;
3997 }
3998 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02003999 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004000}
4001EXPORT_SYMBOL_GPL(of_clk_del_provider);
4002
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004003static struct clk_hw *
4004__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4005 struct of_phandle_args *clkspec)
4006{
4007 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004008
Stephen Boyd74002fc2016-08-25 13:35:36 -07004009 if (provider->get_hw)
4010 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004011
Stephen Boyd74002fc2016-08-25 13:35:36 -07004012 clk = provider->get(clkspec, provider->data);
4013 if (IS_ERR(clk))
4014 return ERR_CAST(clk);
4015 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004016}
4017
Stephen Boyd73e0e492015-02-06 11:42:43 -08004018struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
4019 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05004020{
4021 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01004022 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Stephen Boydf155d152016-08-15 14:32:23 -07004023 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -05004024
Stephen Boyd306c3422015-02-05 15:39:11 -08004025 if (!clkspec)
4026 return ERR_PTR(-EINVAL);
4027
Grant Likely766e6a42012-04-09 14:50:06 -05004028 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08004029 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004030 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07004031 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004032 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004033 clk = __clk_create_clk(hw, dev_id, con_id);
Stephen Boydf155d152016-08-15 14:32:23 -07004034 }
Stephen Boyd73e0e492015-02-06 11:42:43 -08004035
Stephen Boydf155d152016-08-15 14:32:23 -07004036 if (!IS_ERR(clk)) {
4037 if (!__clk_get(clk)) {
Stephen Boyd73e0e492015-02-06 11:42:43 -08004038 __clk_free_clk(clk);
4039 clk = ERR_PTR(-ENOENT);
4040 }
4041
Grant Likely766e6a42012-04-09 14:50:06 -05004042 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08004043 }
Grant Likely766e6a42012-04-09 14:50:06 -05004044 }
Stephen Boyd306c3422015-02-05 15:39:11 -08004045 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004046
4047 return clk;
4048}
4049
Stephen Boyd306c3422015-02-05 15:39:11 -08004050/**
4051 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4052 * @clkspec: pointer to a clock specifier data structure
4053 *
4054 * This function looks up a struct clk from the registered list of clock
4055 * providers, an input is a clock specifier data structure as returned
4056 * from the of_parse_phandle_with_args() function call.
4057 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004058struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4059{
Stephen Boyd306c3422015-02-05 15:39:11 -08004060 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05004061}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06004062EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05004063
Stephen Boyd929e7f32016-02-19 15:52:32 -08004064/**
4065 * of_clk_get_parent_count() - Count the number of clocks a device node has
4066 * @np: device node to count
4067 *
4068 * Returns: The number of clocks that are possible parents of this node
4069 */
4070unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07004071{
Stephen Boyd929e7f32016-02-19 15:52:32 -08004072 int count;
4073
4074 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4075 if (count < 0)
4076 return 0;
4077
4078 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07004079}
4080EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4081
Grant Likely766e6a42012-04-09 14:50:06 -05004082const char *of_clk_get_parent_name(struct device_node *np, int index)
4083{
4084 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004085 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05004086 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004087 const __be32 *vp;
4088 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05004089 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004090 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004091 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05004092
Grant Likely766e6a42012-04-09 14:50:06 -05004093 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4094 &clkspec);
4095 if (rc)
4096 return NULL;
4097
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004098 index = clkspec.args_count ? clkspec.args[0] : 0;
4099 count = 0;
4100
4101 /* if there is an indices property, use it to transfer the index
4102 * specified into an array offset for the clock-output-names property.
4103 */
4104 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4105 if (index == pv) {
4106 index = count;
4107 break;
4108 }
4109 count++;
4110 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09004111 /* We went off the end of 'clock-indices' without finding it */
4112 if (prop && !vp)
4113 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004114
Grant Likely766e6a42012-04-09 14:50:06 -05004115 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004116 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004117 &clk_name) < 0) {
4118 /*
4119 * Best effort to get the name if the clock has been
4120 * registered with the framework. If the clock isn't
4121 * registered, we return the node name as the name of
4122 * the clock as long as #clock-cells = 0.
4123 */
4124 clk = of_clk_get_from_provider(&clkspec);
4125 if (IS_ERR(clk)) {
4126 if (clkspec.args_count == 0)
4127 clk_name = clkspec.np->name;
4128 else
4129 clk_name = NULL;
4130 } else {
4131 clk_name = __clk_get_name(clk);
4132 clk_put(clk);
4133 }
4134 }
4135
Grant Likely766e6a42012-04-09 14:50:06 -05004136
4137 of_node_put(clkspec.np);
4138 return clk_name;
4139}
4140EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4141
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05004142/**
4143 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4144 * number of parents
4145 * @np: Device node pointer associated with clock provider
4146 * @parents: pointer to char array that hold the parents' names
4147 * @size: size of the @parents array
4148 *
4149 * Return: number of parents for the clock node.
4150 */
4151int of_clk_parent_fill(struct device_node *np, const char **parents,
4152 unsigned int size)
4153{
4154 unsigned int i = 0;
4155
4156 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4157 i++;
4158
4159 return i;
4160}
4161EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4162
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004163struct clock_provider {
4164 of_clk_init_cb_t clk_init_cb;
4165 struct device_node *np;
4166 struct list_head node;
4167};
4168
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004169/*
4170 * This function looks for a parent clock. If there is one, then it
4171 * checks that the provider for this parent clock was initialized, in
4172 * this case the parent clock will be ready.
4173 */
4174static int parent_ready(struct device_node *np)
4175{
4176 int i = 0;
4177
4178 while (true) {
4179 struct clk *clk = of_clk_get(np, i);
4180
4181 /* this parent is ready we can check the next one */
4182 if (!IS_ERR(clk)) {
4183 clk_put(clk);
4184 i++;
4185 continue;
4186 }
4187
4188 /* at least one parent is not ready, we exit now */
4189 if (PTR_ERR(clk) == -EPROBE_DEFER)
4190 return 0;
4191
4192 /*
4193 * Here we make assumption that the device tree is
4194 * written correctly. So an error means that there is
4195 * no more parent. As we didn't exit yet, then the
4196 * previous parent are ready. If there is no clock
4197 * parent, no need to wait for them, then we can
4198 * consider their absence as being ready
4199 */
4200 return 1;
4201 }
4202}
4203
Grant Likely766e6a42012-04-09 14:50:06 -05004204/**
Lee Jonesd56f8992016-02-11 13:19:11 -08004205 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4206 * @np: Device node pointer associated with clock provider
4207 * @index: clock index
4208 * @flags: pointer to clk_core->flags
4209 *
4210 * Detects if the clock-critical property exists and, if so, sets the
4211 * corresponding CLK_IS_CRITICAL flag.
4212 *
4213 * Do not use this function. It exists only for legacy Device Tree
4214 * bindings, such as the one-clock-per-node style that are outdated.
4215 * Those bindings typically put all clock data into .dts and the Linux
4216 * driver has no clock data, thus making it impossible to set this flag
4217 * correctly from the driver. Only those drivers may call
4218 * of_clk_detect_critical from their setup functions.
4219 *
4220 * Return: error code or zero on success
4221 */
4222int of_clk_detect_critical(struct device_node *np,
4223 int index, unsigned long *flags)
4224{
4225 struct property *prop;
4226 const __be32 *cur;
4227 uint32_t idx;
4228
4229 if (!np || !flags)
4230 return -EINVAL;
4231
4232 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4233 if (index == idx)
4234 *flags |= CLK_IS_CRITICAL;
4235
4236 return 0;
4237}
4238
4239/**
Grant Likely766e6a42012-04-09 14:50:06 -05004240 * of_clk_init() - Scan and init clock providers from the DT
4241 * @matches: array of compatible values and init functions for providers.
4242 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004243 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01004244 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004245 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05004246 */
4247void __init of_clk_init(const struct of_device_id *matches)
4248{
Alex Elder7f7ed582013-08-22 11:31:31 -05004249 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05004250 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004251 struct clock_provider *clk_provider, *next;
4252 bool is_init_done;
4253 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07004254 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05004255
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304256 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03004257 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304258
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004259 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05004260 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004261 struct clock_provider *parent;
4262
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01004263 if (!of_device_is_available(np))
4264 continue;
4265
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004266 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4267 if (!parent) {
4268 list_for_each_entry_safe(clk_provider, next,
4269 &clk_provider_list, node) {
4270 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004271 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004272 kfree(clk_provider);
4273 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004274 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004275 return;
4276 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004277
4278 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004279 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01004280 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004281 }
4282
4283 while (!list_empty(&clk_provider_list)) {
4284 is_init_done = false;
4285 list_for_each_entry_safe(clk_provider, next,
4286 &clk_provider_list, node) {
4287 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004288
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02004289 /* Don't populate platform devices */
4290 of_node_set_flag(clk_provider->np,
4291 OF_POPULATED);
4292
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004293 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004294 of_clk_set_defaults(clk_provider->np, true);
4295
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004296 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004297 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004298 kfree(clk_provider);
4299 is_init_done = true;
4300 }
4301 }
4302
4303 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01004304 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004305 * remaining providers during the last loop, so now we
4306 * initialize all the remaining ones unconditionally
4307 * in case the clock parent was not mandatory
4308 */
4309 if (!is_init_done)
4310 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05004311 }
4312}
4313#endif