blob: 929aef0bd0a00bc919f90dbb4f60c9ae79cf7bef [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>
Deepak Katragaddabfb96502018-02-07 10:37:41 -08004 * Copyright (c) 2016-2018, 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
Deepak Katragadda9abd7942017-06-13 14:20:09 -070013#define pr_fmt(fmt) "clk: " fmt
14
Stephen Boyd3c373112015-06-19 15:00:46 -070015#include <linux/clk.h>
Michael Turquetteb09d6d92015-01-29 14:22:50 -080016#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020017#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070018#include <linux/module.h>
19#include <linux/mutex.h>
20#include <linux/spinlock.h>
21#include <linux/err.h>
22#include <linux/list.h>
23#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050024#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070025#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053026#include <linux/init.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070027#include <linux/sched.h>
Stephen Boyd562ef0b2015-05-01 12:16:14 -070028#include <linux/clkdev.h>
Deepak Katragadda677bad12016-11-04 13:33:13 -070029#include <linux/of_platform.h>
30#include <linux/pm_opp.h>
Stephen Boyd5cb05a12016-05-16 11:05:16 +053031#include <linux/regulator/consumer.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070032
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020033#include "clk.h"
34
Shefali Jain0dc6e782017-11-27 13:06:27 +053035#if defined(CONFIG_COMMON_CLK)
36
Mike Turquetteb24764902012-03-15 23:11:19 -070037static DEFINE_SPINLOCK(enable_lock);
38static DEFINE_MUTEX(prepare_lock);
39
Mike Turquette533ddeb2013-03-28 13:59:02 -070040static struct task_struct *prepare_owner;
41static struct task_struct *enable_owner;
42
43static int prepare_refcnt;
44static int enable_refcnt;
45
Mike Turquetteb24764902012-03-15 23:11:19 -070046static HLIST_HEAD(clk_root_list);
47static HLIST_HEAD(clk_orphan_list);
48static LIST_HEAD(clk_notifier_list);
49
Stephen Boyd5cb05a12016-05-16 11:05:16 +053050struct clk_handoff_vdd {
51 struct list_head list;
52 struct clk_vdd_class *vdd_class;
53};
54
55static LIST_HEAD(clk_handoff_vdd_list);
56
Michael Turquetteb09d6d92015-01-29 14:22:50 -080057/*** private data structures ***/
58
59struct clk_core {
60 const char *name;
61 const struct clk_ops *ops;
62 struct clk_hw *hw;
63 struct module *owner;
64 struct clk_core *parent;
65 const char **parent_names;
66 struct clk_core **parents;
67 u8 num_parents;
68 u8 new_parent_index;
69 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010070 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080071 unsigned long new_rate;
72 struct clk_core *new_parent;
73 struct clk_core *new_child;
74 unsigned long flags;
Heiko Stuebnere6500342015-04-22 22:53:05 +020075 bool orphan;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080076 unsigned int enable_count;
77 unsigned int prepare_count;
Michael Turquettee9b8c592017-04-21 12:27:39 +053078 bool need_handoff_enable;
79 bool need_handoff_prepare;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070080 unsigned long min_rate;
81 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080082 unsigned long accuracy;
83 int phase;
84 struct hlist_head children;
85 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010086 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080087 unsigned int notifier_count;
88#ifdef CONFIG_DEBUG_FS
89 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020090 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080091#endif
92 struct kref ref;
Stephen Boyd5cb05a12016-05-16 11:05:16 +053093 struct clk_vdd_class *vdd_class;
94 unsigned long *rate_max;
95 int num_rate_max;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080096};
97
Stephen Boyddfc202e2015-02-02 14:37:41 -080098#define CREATE_TRACE_POINTS
99#include <trace/events/clk.h>
100
Michael Turquetteb09d6d92015-01-29 14:22:50 -0800101struct clk {
102 struct clk_core *core;
103 const char *dev_id;
104 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100105 unsigned long min_rate;
106 unsigned long max_rate;
Stephen Boyd50595f82015-02-06 11:42:44 -0800107 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -0800108};
109
Mike Turquetteeab89f62013-03-28 13:59:01 -0700110/*** locking ***/
111static void clk_prepare_lock(void)
112{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700113 if (!mutex_trylock(&prepare_lock)) {
114 if (prepare_owner == current) {
115 prepare_refcnt++;
116 return;
117 }
118 mutex_lock(&prepare_lock);
119 }
120 WARN_ON_ONCE(prepare_owner != NULL);
121 WARN_ON_ONCE(prepare_refcnt != 0);
122 prepare_owner = current;
123 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700124}
125
126static void clk_prepare_unlock(void)
127{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700128 WARN_ON_ONCE(prepare_owner != current);
129 WARN_ON_ONCE(prepare_refcnt == 0);
130
131 if (--prepare_refcnt)
132 return;
133 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700134 mutex_unlock(&prepare_lock);
135}
136
137static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700138 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700139{
140 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700141
142 if (!spin_trylock_irqsave(&enable_lock, flags)) {
143 if (enable_owner == current) {
144 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700145 __acquire(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700146 return flags;
147 }
148 spin_lock_irqsave(&enable_lock, flags);
149 }
150 WARN_ON_ONCE(enable_owner != NULL);
151 WARN_ON_ONCE(enable_refcnt != 0);
152 enable_owner = current;
153 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700154 return flags;
155}
156
157static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700158 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700159{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700160 WARN_ON_ONCE(enable_owner != current);
161 WARN_ON_ONCE(enable_refcnt == 0);
162
Stephen Boyda57aa182015-07-24 12:24:48 -0700163 if (--enable_refcnt) {
164 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700165 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700166 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700167 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700168 spin_unlock_irqrestore(&enable_lock, flags);
169}
170
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700171static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530172{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700173 /*
174 * .is_prepared is optional for clocks that can prepare
175 * fall back to software usage counter if it is missing
176 */
177 if (!core->ops->is_prepared)
178 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530179
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700180 return core->ops->is_prepared(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530181}
182
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700183static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530184{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700185 /*
186 * .is_enabled is only mandatory for clocks that gate
187 * fall back to software usage counter if .is_enabled is missing
188 */
189 if (!core->ops->is_enabled)
190 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530191
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700192 return core->ops->is_enabled(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530193}
194
Mike Turquetteb24764902012-03-15 23:11:19 -0700195/*** helper functions ***/
196
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200197const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700198{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100199 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700200}
Niels de Vos48950842012-12-13 13:12:25 +0100201EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700202
Stephen Boyde7df6f62015-08-12 13:04:56 -0700203const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700204{
205 return hw->core->name;
206}
207EXPORT_SYMBOL_GPL(clk_hw_get_name);
208
Russ Dill65800b22012-11-26 11:20:09 -0800209struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700210{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100211 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700212}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800213EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700214
Stephen Boyde7df6f62015-08-12 13:04:56 -0700215unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700216{
217 return hw->core->num_parents;
218}
219EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
220
Stephen Boyde7df6f62015-08-12 13:04:56 -0700221struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700222{
223 return hw->core->parent ? hw->core->parent->hw : NULL;
224}
225EXPORT_SYMBOL_GPL(clk_hw_get_parent);
226
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700227static struct clk_core *__clk_lookup_subtree(const char *name,
228 struct clk_core *core)
229{
230 struct clk_core *child;
231 struct clk_core *ret;
232
233 if (!strcmp(core->name, name))
234 return core;
235
236 hlist_for_each_entry(child, &core->children, child_node) {
237 ret = __clk_lookup_subtree(name, child);
238 if (ret)
239 return ret;
240 }
241
242 return NULL;
243}
244
245static struct clk_core *clk_core_lookup(const char *name)
246{
247 struct clk_core *root_clk;
248 struct clk_core *ret;
249
250 if (!name)
251 return NULL;
252
253 /* search the 'proper' clk tree first */
254 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
255 ret = __clk_lookup_subtree(name, root_clk);
256 if (ret)
257 return ret;
258 }
259
260 /* if not found, then search the orphan tree */
261 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
262 ret = __clk_lookup_subtree(name, root_clk);
263 if (ret)
264 return ret;
265 }
266
267 return NULL;
268}
269
Stephen Boydd6968fc2015-04-30 13:54:13 -0700270static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100271 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100272{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700273 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100274 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900275
276 if (!core->parents[index])
277 core->parents[index] =
278 clk_core_lookup(core->parent_names[index]);
279
280 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100281}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100282
Stephen Boyde7df6f62015-08-12 13:04:56 -0700283struct clk_hw *
284clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700285{
286 struct clk_core *parent;
287
288 parent = clk_core_get_parent_by_index(hw->core, index);
289
290 return !parent ? NULL : parent->hw;
291}
292EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
293
Russ Dill65800b22012-11-26 11:20:09 -0800294unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700295{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100296 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700297}
298
Stephen Boydd6968fc2015-04-30 13:54:13 -0700299static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700300{
301 unsigned long ret;
302
Stephen Boydd6968fc2015-04-30 13:54:13 -0700303 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530304 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700305 goto out;
306 }
307
Stephen Boydd6968fc2015-04-30 13:54:13 -0700308 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700309
Stephen Boyd47b0eeb2016-02-02 17:24:56 -0800310 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -0700311 goto out;
312
Stephen Boydd6968fc2015-04-30 13:54:13 -0700313 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530314 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700315
316out:
317 return ret;
318}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100319
Stephen Boyde7df6f62015-08-12 13:04:56 -0700320unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700321{
322 return clk_core_get_rate_nolock(hw->core);
323}
324EXPORT_SYMBOL_GPL(clk_hw_get_rate);
325
Stephen Boydd6968fc2015-04-30 13:54:13 -0700326static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100327{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700328 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100329 return 0;
330
Stephen Boydd6968fc2015-04-30 13:54:13 -0700331 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100332}
333
Russ Dill65800b22012-11-26 11:20:09 -0800334unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700335{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100336 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700337}
Thierry Redingb05c6832013-09-03 09:43:51 +0200338EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700339
Stephen Boyde7df6f62015-08-12 13:04:56 -0700340unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700341{
342 return hw->core->flags;
343}
344EXPORT_SYMBOL_GPL(clk_hw_get_flags);
345
Stephen Boyde7df6f62015-08-12 13:04:56 -0700346bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700347{
348 return clk_core_is_prepared(hw->core);
349}
350
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200351bool clk_hw_is_enabled(const struct clk_hw *hw)
352{
353 return clk_core_is_enabled(hw->core);
354}
355
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100356bool __clk_is_enabled(struct clk *clk)
357{
358 if (!clk)
359 return false;
360
361 return clk_core_is_enabled(clk->core);
362}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800363EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700364
Stephen Boyd15a02c12015-01-19 18:05:28 -0800365static bool mux_is_better_rate(unsigned long rate, unsigned long now,
366 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100367{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800368 if (flags & CLK_MUX_ROUND_CLOSEST)
369 return abs(now - rate) < abs(best - rate);
370
371 return now <= rate && now > best;
372}
373
Boris Brezillon0817b622015-07-07 20:48:08 +0200374static int
375clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800376 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100377{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100378 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200379 int i, num_parents, ret;
380 unsigned long best = 0;
381 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100382
383 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100384 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
385 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200386 if (core->flags & CLK_SET_RATE_PARENT) {
387 ret = __clk_determine_rate(parent ? parent->hw : NULL,
388 &parent_req);
389 if (ret)
390 return ret;
391
392 best = parent_req.rate;
393 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100394 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200395 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100396 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200397 }
398
James Hogane366fdd2013-07-29 12:25:02 +0100399 goto out;
400 }
401
402 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100403 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100404 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100405 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100406 if (!parent)
407 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200408
409 if (core->flags & CLK_SET_RATE_PARENT) {
410 parent_req = *req;
411 ret = __clk_determine_rate(parent->hw, &parent_req);
412 if (ret)
413 continue;
414 } else {
415 parent_req.rate = clk_core_get_rate_nolock(parent);
416 }
417
418 if (mux_is_better_rate(req->rate, parent_req.rate,
419 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100420 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200421 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100422 }
423 }
424
Boris Brezillon57d866e2015-07-09 22:39:38 +0200425 if (!best_parent)
426 return -EINVAL;
427
James Hogane366fdd2013-07-29 12:25:02 +0100428out:
429 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200430 req->best_parent_hw = best_parent->hw;
431 req->best_parent_rate = best;
432 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100433
Boris Brezillon0817b622015-07-07 20:48:08 +0200434 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100435}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800436
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100437struct clk *__clk_lookup(const char *name)
438{
439 struct clk_core *core = clk_core_lookup(name);
440
441 return !core ? NULL : core->hw->clk;
442}
443
Stephen Boydd6968fc2015-04-30 13:54:13 -0700444static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100445 unsigned long *min_rate,
446 unsigned long *max_rate)
447{
448 struct clk *clk_user;
449
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700450 *min_rate = core->min_rate;
451 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100452
Stephen Boydd6968fc2015-04-30 13:54:13 -0700453 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100454 *min_rate = max(*min_rate, clk_user->min_rate);
455
Stephen Boydd6968fc2015-04-30 13:54:13 -0700456 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100457 *max_rate = min(*max_rate, clk_user->max_rate);
458}
459
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700460void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
461 unsigned long max_rate)
462{
463 hw->core->min_rate = min_rate;
464 hw->core->max_rate = max_rate;
465}
466EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
467
Stephen Boyd15a02c12015-01-19 18:05:28 -0800468/*
Taniya Dasd9744c12016-08-19 10:08:28 +0530469 * Aggregate the rate of all child nodes which are enabled and exclude the
470 * child node which requests for clk_aggregate_rate.
471 */
472unsigned long clk_aggregate_rate(struct clk_hw *hw,
473 const struct clk_core *parent)
474{
475 struct clk_core *child;
476 unsigned long aggre_rate = 0;
477
478 hlist_for_each_entry(child, &parent->children, child_node) {
479 if (child->enable_count &&
480 strcmp(child->name, hw->init->name))
481 aggre_rate = max(child->rate, aggre_rate);
482 }
483
484 return aggre_rate;
485}
486EXPORT_SYMBOL_GPL(clk_aggregate_rate);
487
488/*
Stephen Boyd15a02c12015-01-19 18:05:28 -0800489 * Helper for finding best parent to provide a given frequency. This can be used
490 * directly as a determine_rate callback (e.g. for a mux), or from a more
491 * complex clock that may combine a mux with other operations.
492 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200493int __clk_mux_determine_rate(struct clk_hw *hw,
494 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800495{
Boris Brezillon0817b622015-07-07 20:48:08 +0200496 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800497}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800498EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100499
Boris Brezillon0817b622015-07-07 20:48:08 +0200500int __clk_mux_determine_rate_closest(struct clk_hw *hw,
501 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800502{
Boris Brezillon0817b622015-07-07 20:48:08 +0200503 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800504}
505EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
506
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530507/*
508 * Find the voltage level required for a given clock rate.
509 */
510static int clk_find_vdd_level(struct clk_core *clk, unsigned long rate)
511{
512 int level;
513
Deepak Katragaddaa755ecb2016-11-28 14:48:56 -0800514 /*
515 * For certain PLLs, due to the limitation in the bits allocated for
516 * programming the fractional divider, the actual rate of the PLL will
517 * be slightly higher than the requested rate (in the order of several
518 * Hz). To accommodate this difference, convert the FMAX rate and the
519 * clock frequency to KHz and use that for deriving the voltage level.
520 */
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530521 for (level = 0; level < clk->num_rate_max; level++)
Deepak Katragaddaa755ecb2016-11-28 14:48:56 -0800522 if (DIV_ROUND_CLOSEST(rate, 1000) <=
523 DIV_ROUND_CLOSEST(clk->rate_max[level], 1000))
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530524 break;
525
526 if (level == clk->num_rate_max) {
527 pr_err("Rate %lu for %s is greater than highest Fmax\n", rate,
528 clk->name);
529 return -EINVAL;
530 }
531
532 return level;
533}
534
535/*
536 * Update voltage level given the current votes.
537 */
538static int clk_update_vdd(struct clk_vdd_class *vdd_class)
539{
540 int level, rc = 0, i, ignore;
541 struct regulator **r = vdd_class->regulator;
542 int *uv = vdd_class->vdd_uv;
543 int n_reg = vdd_class->num_regulators;
544 int cur_lvl = vdd_class->cur_level;
545 int max_lvl = vdd_class->num_levels - 1;
546 int cur_base = cur_lvl * n_reg;
547 int new_base;
548
549 /* aggregate votes */
550 for (level = max_lvl; level > 0; level--)
551 if (vdd_class->level_votes[level])
552 break;
553
554 if (level == cur_lvl)
555 return 0;
556
557 max_lvl = max_lvl * n_reg;
558 new_base = level * n_reg;
559
560 for (i = 0; i < vdd_class->num_regulators; i++) {
561 pr_debug("Set Voltage level Min %d, Max %d\n", uv[new_base + i],
562 uv[max_lvl + i]);
563 rc = regulator_set_voltage(r[i], uv[new_base + i],
Taniya Daseee50c82016-12-03 19:06:59 +0530564 vdd_class->use_max_uV ? INT_MAX : uv[max_lvl + i]);
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530565 if (rc)
566 goto set_voltage_fail;
567
568 if (cur_lvl == 0 || cur_lvl == vdd_class->num_levels)
569 rc = regulator_enable(r[i]);
570 else if (level == 0)
571 rc = regulator_disable(r[i]);
572 if (rc)
573 goto enable_disable_fail;
574 }
575
576 if (vdd_class->set_vdd && !vdd_class->num_regulators)
577 rc = vdd_class->set_vdd(vdd_class, level);
578
579 if (!rc)
580 vdd_class->cur_level = level;
581
582 return rc;
583
584enable_disable_fail:
Taniya Daseee50c82016-12-03 19:06:59 +0530585 regulator_set_voltage(r[i], uv[cur_base + i],
586 vdd_class->use_max_uV ? INT_MAX : uv[max_lvl + i]);
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530587
588set_voltage_fail:
589 for (i--; i >= 0; i--) {
Taniya Daseee50c82016-12-03 19:06:59 +0530590 regulator_set_voltage(r[i], uv[cur_base + i],
591 vdd_class->use_max_uV ? INT_MAX : uv[max_lvl + i]);
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530592 if (cur_lvl == 0 || cur_lvl == vdd_class->num_levels)
593 regulator_disable(r[i]);
594 else if (level == 0)
595 ignore = regulator_enable(r[i]);
596 }
597
598 return rc;
599}
600
601/*
602 * Vote for a voltage level.
603 */
604static int clk_vote_vdd_level(struct clk_vdd_class *vdd_class, int level)
605{
606 int rc = 0;
607
608 if (level >= vdd_class->num_levels)
609 return -EINVAL;
610
611 mutex_lock(&vdd_class->lock);
612
613 vdd_class->level_votes[level]++;
614
615 rc = clk_update_vdd(vdd_class);
616 if (rc)
617 vdd_class->level_votes[level]--;
618
619 mutex_unlock(&vdd_class->lock);
620
621 return rc;
622}
623
624/*
625 * Remove vote for a voltage level.
626 */
627static int clk_unvote_vdd_level(struct clk_vdd_class *vdd_class, int level)
628{
629 int rc = 0;
630
631 if (level >= vdd_class->num_levels)
632 return -EINVAL;
633
634 mutex_lock(&vdd_class->lock);
635
636 if (WARN(!vdd_class->level_votes[level],
637 "Reference counts are incorrect for %s level %d\n",
638 vdd_class->class_name, level))
639 goto out;
640
641 vdd_class->level_votes[level]--;
642
643 rc = clk_update_vdd(vdd_class);
644 if (rc)
645 vdd_class->level_votes[level]++;
646
647out:
648 mutex_unlock(&vdd_class->lock);
649 return rc;
650}
651
652/*
653 * Vote for a voltage level corresponding to a clock's rate.
654 */
655static int clk_vote_rate_vdd(struct clk_core *core, unsigned long rate)
656{
657 int level;
658
659 if (!core->vdd_class)
660 return 0;
661
662 level = clk_find_vdd_level(core, rate);
663 if (level < 0)
664 return level;
665
666 return clk_vote_vdd_level(core->vdd_class, level);
667}
668
669/*
670 * Remove vote for a voltage level corresponding to a clock's rate.
671 */
672static void clk_unvote_rate_vdd(struct clk_core *core, unsigned long rate)
673{
674 int level;
675
676 if (!core->vdd_class)
677 return;
678
679 level = clk_find_vdd_level(core, rate);
680 if (level < 0)
681 return;
682
683 clk_unvote_vdd_level(core->vdd_class, level);
684}
685
686static bool clk_is_rate_level_valid(struct clk_core *core, unsigned long rate)
687{
688 int level;
689
690 if (!core->vdd_class)
691 return true;
692
693 level = clk_find_vdd_level(core, rate);
694
695 return level >= 0;
696}
697
698static int clk_vdd_class_init(struct clk_vdd_class *vdd)
699{
700 struct clk_handoff_vdd *v;
701
Taniya Daseee50c82016-12-03 19:06:59 +0530702 if (vdd->skip_handoff)
703 return 0;
704
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530705 list_for_each_entry(v, &clk_handoff_vdd_list, list) {
706 if (v->vdd_class == vdd)
707 return 0;
708 }
709
710 pr_debug("voting for vdd_class %s\n", vdd->class_name);
711
712 if (clk_vote_vdd_level(vdd, vdd->num_levels - 1))
713 pr_err("failed to vote for %s\n", vdd->class_name);
714
715 v = kmalloc(sizeof(*v), GFP_KERNEL);
716 if (!v)
717 return -ENOMEM;
718
719 v->vdd_class = vdd;
720
721 list_add_tail(&v->list, &clk_handoff_vdd_list);
722
723 return 0;
724}
725
Mike Turquetteb24764902012-03-15 23:11:19 -0700726/*** clk api ***/
727
Stephen Boydd6968fc2015-04-30 13:54:13 -0700728static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700729{
Stephen Boyda6334722015-05-06 17:00:54 -0700730 lockdep_assert_held(&prepare_lock);
731
Stephen Boydd6968fc2015-04-30 13:54:13 -0700732 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700733 return;
734
Stephen Boydd6968fc2015-04-30 13:54:13 -0700735 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700736 return;
737
Lee Jones2e20fbf2016-02-11 13:19:10 -0800738 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
739 return;
740
Stephen Boydd6968fc2015-04-30 13:54:13 -0700741 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700742 return;
743
Stephen Boydd6968fc2015-04-30 13:54:13 -0700744 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700745
Stephen Boydd6968fc2015-04-30 13:54:13 -0700746 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800747
Stephen Boydd6968fc2015-04-30 13:54:13 -0700748 if (core->ops->unprepare)
749 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700750
Stephen Boydd6968fc2015-04-30 13:54:13 -0700751 trace_clk_unprepare_complete(core);
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530752
753 clk_unvote_rate_vdd(core, core->rate);
754
Stephen Boydd6968fc2015-04-30 13:54:13 -0700755 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700756}
757
Dong Aishenga6adc302016-06-30 17:31:11 +0800758static void clk_core_unprepare_lock(struct clk_core *core)
759{
760 clk_prepare_lock();
761 clk_core_unprepare(core);
762 clk_prepare_unlock();
763}
764
Mike Turquetteb24764902012-03-15 23:11:19 -0700765/**
766 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200767 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700768 *
769 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
770 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
771 * if the operation may sleep. One example is a clk which is accessed over
772 * I2c. In the complex case a clk gate operation may require a fast and a slow
773 * part. It is this reason that clk_unprepare and clk_disable are not mutually
774 * exclusive. In fact clk_disable must be called before clk_unprepare.
775 */
776void clk_unprepare(struct clk *clk)
777{
Stephen Boyd63589e92014-03-26 16:06:37 -0700778 if (IS_ERR_OR_NULL(clk))
779 return;
780
Dong Aishenga6adc302016-06-30 17:31:11 +0800781 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700782}
783EXPORT_SYMBOL_GPL(clk_unprepare);
784
Stephen Boydd6968fc2015-04-30 13:54:13 -0700785static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700786{
787 int ret = 0;
788
Stephen Boyda6334722015-05-06 17:00:54 -0700789 lockdep_assert_held(&prepare_lock);
790
Stephen Boydd6968fc2015-04-30 13:54:13 -0700791 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700792 return 0;
793
Stephen Boydd6968fc2015-04-30 13:54:13 -0700794 if (core->prepare_count == 0) {
795 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700796 if (ret)
797 return ret;
798
Stephen Boydd6968fc2015-04-30 13:54:13 -0700799 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800800
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530801 ret = clk_vote_rate_vdd(core, core->rate);
802 if (ret) {
803 clk_core_unprepare(core->parent);
804 return ret;
805 }
806
Stephen Boydd6968fc2015-04-30 13:54:13 -0700807 if (core->ops->prepare)
808 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800809
Stephen Boydd6968fc2015-04-30 13:54:13 -0700810 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800811
812 if (ret) {
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530813 clk_unvote_rate_vdd(core, core->rate);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700814 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800815 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700816 }
817 }
818
Stephen Boydd6968fc2015-04-30 13:54:13 -0700819 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700820
821 return 0;
822}
823
Dong Aishenga6adc302016-06-30 17:31:11 +0800824static int clk_core_prepare_lock(struct clk_core *core)
825{
826 int ret;
827
828 clk_prepare_lock();
829 ret = clk_core_prepare(core);
830 clk_prepare_unlock();
831
832 return ret;
833}
834
Mike Turquetteb24764902012-03-15 23:11:19 -0700835/**
836 * clk_prepare - prepare a clock source
837 * @clk: the clk being prepared
838 *
839 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
840 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
841 * operation may sleep. One example is a clk which is accessed over I2c. In
842 * the complex case a clk ungate operation may require a fast and a slow part.
843 * It is this reason that clk_prepare and clk_enable are not mutually
844 * exclusive. In fact clk_prepare must be called before clk_enable.
845 * Returns 0 on success, -EERROR otherwise.
846 */
847int clk_prepare(struct clk *clk)
848{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100849 if (!clk)
850 return 0;
851
Dong Aishenga6adc302016-06-30 17:31:11 +0800852 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700853}
854EXPORT_SYMBOL_GPL(clk_prepare);
855
Stephen Boydd6968fc2015-04-30 13:54:13 -0700856static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700857{
Stephen Boyda6334722015-05-06 17:00:54 -0700858 lockdep_assert_held(&enable_lock);
859
Stephen Boydd6968fc2015-04-30 13:54:13 -0700860 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700861 return;
862
Stephen Boydd6968fc2015-04-30 13:54:13 -0700863 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700864 return;
865
Lee Jones2e20fbf2016-02-11 13:19:10 -0800866 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
867 return;
868
Stephen Boydd6968fc2015-04-30 13:54:13 -0700869 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700870 return;
871
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700872 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800873
Stephen Boydd6968fc2015-04-30 13:54:13 -0700874 if (core->ops->disable)
875 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700876
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700877 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800878
Stephen Boydd6968fc2015-04-30 13:54:13 -0700879 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100880}
881
Dong Aishenga6adc302016-06-30 17:31:11 +0800882static void clk_core_disable_lock(struct clk_core *core)
883{
884 unsigned long flags;
885
886 flags = clk_enable_lock();
887 clk_core_disable(core);
888 clk_enable_unlock(flags);
889}
890
Mike Turquetteb24764902012-03-15 23:11:19 -0700891/**
892 * clk_disable - gate a clock
893 * @clk: the clk being gated
894 *
895 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
896 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
897 * clk if the operation is fast and will never sleep. One example is a
898 * SoC-internal clk which is controlled via simple register writes. In the
899 * complex case a clk gate operation may require a fast and a slow part. It is
900 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
901 * In fact clk_disable must be called before clk_unprepare.
902 */
903void clk_disable(struct clk *clk)
904{
Stephen Boyd63589e92014-03-26 16:06:37 -0700905 if (IS_ERR_OR_NULL(clk))
906 return;
907
Dong Aishenga6adc302016-06-30 17:31:11 +0800908 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700909}
910EXPORT_SYMBOL_GPL(clk_disable);
911
Stephen Boydd6968fc2015-04-30 13:54:13 -0700912static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700913{
914 int ret = 0;
915
Stephen Boyda6334722015-05-06 17:00:54 -0700916 lockdep_assert_held(&enable_lock);
917
Stephen Boydd6968fc2015-04-30 13:54:13 -0700918 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700919 return 0;
920
Stephen Boydd6968fc2015-04-30 13:54:13 -0700921 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700922 return -ESHUTDOWN;
923
Stephen Boydd6968fc2015-04-30 13:54:13 -0700924 if (core->enable_count == 0) {
925 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700926
927 if (ret)
928 return ret;
929
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700930 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800931
Stephen Boydd6968fc2015-04-30 13:54:13 -0700932 if (core->ops->enable)
933 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800934
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700935 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800936
937 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700938 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800939 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700940 }
941 }
942
Stephen Boydd6968fc2015-04-30 13:54:13 -0700943 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700944 return 0;
945}
946
Dong Aishenga6adc302016-06-30 17:31:11 +0800947static int clk_core_enable_lock(struct clk_core *core)
948{
949 unsigned long flags;
950 int ret;
951
952 flags = clk_enable_lock();
953 ret = clk_core_enable(core);
954 clk_enable_unlock(flags);
955
956 return ret;
957}
958
Mike Turquetteb24764902012-03-15 23:11:19 -0700959/**
960 * clk_enable - ungate a clock
961 * @clk: the clk being ungated
962 *
963 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
964 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
965 * if the operation will never sleep. One example is a SoC-internal clk which
966 * is controlled via simple register writes. In the complex case a clk ungate
967 * operation may require a fast and a slow part. It is this reason that
968 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
969 * must be called before clk_enable. Returns 0 on success, -EERROR
970 * otherwise.
971 */
972int clk_enable(struct clk *clk)
973{
Dong Aisheng864e1602015-04-30 14:02:19 -0700974 if (!clk)
975 return 0;
976
Dong Aishenga6adc302016-06-30 17:31:11 +0800977 return clk_core_enable_lock(clk->core);
978}
979EXPORT_SYMBOL_GPL(clk_enable);
980
981static int clk_core_prepare_enable(struct clk_core *core)
982{
983 int ret;
984
985 ret = clk_core_prepare_lock(core);
986 if (ret)
987 return ret;
988
989 ret = clk_core_enable_lock(core);
990 if (ret)
991 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700992
993 return ret;
994}
Dong Aishenga6adc302016-06-30 17:31:11 +0800995
996static void clk_core_disable_unprepare(struct clk_core *core)
997{
998 clk_core_disable_lock(core);
999 clk_core_unprepare_lock(core);
1000}
Mike Turquetteb24764902012-03-15 23:11:19 -07001001
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001002static void clk_unprepare_unused_subtree(struct clk_core *core)
1003{
1004 struct clk_core *child;
1005
1006 lockdep_assert_held(&prepare_lock);
1007
1008 hlist_for_each_entry(child, &core->children, child_node)
1009 clk_unprepare_unused_subtree(child);
1010
Taniya Das30684a42017-04-21 13:33:20 +05301011 /*
1012 * setting CLK_ENABLE_HAND_OFF flag triggers this conditional
1013 *
1014 * need_handoff_prepare implies this clk was already prepared by
1015 * __clk_init. now we have a proper user, so unset the flag in our
1016 * internal bookkeeping. See CLK_ENABLE_HAND_OFF flag in clk-provider.h
1017 * for details.
1018 */
1019 if (core->need_handoff_prepare) {
1020 core->need_handoff_prepare = false;
1021 clk_core_unprepare(core);
1022 }
1023
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001024 if (core->prepare_count)
1025 return;
1026
1027 if (core->flags & CLK_IGNORE_UNUSED)
1028 return;
1029
1030 if (clk_core_is_prepared(core)) {
1031 trace_clk_unprepare(core);
1032 if (core->ops->unprepare_unused)
1033 core->ops->unprepare_unused(core->hw);
1034 else if (core->ops->unprepare)
1035 core->ops->unprepare(core->hw);
1036 trace_clk_unprepare_complete(core);
1037 }
1038}
1039
1040static void clk_disable_unused_subtree(struct clk_core *core)
1041{
1042 struct clk_core *child;
1043 unsigned long flags;
1044
1045 lockdep_assert_held(&prepare_lock);
1046
1047 hlist_for_each_entry(child, &core->children, child_node)
1048 clk_disable_unused_subtree(child);
1049
Taniya Das30684a42017-04-21 13:33:20 +05301050 /*
1051 * setting CLK_ENABLE_HAND_OFF flag triggers this conditional
1052 *
1053 * need_handoff_enable implies this clk was already enabled by
1054 * __clk_init. now we have a proper user, so unset the flag in our
1055 * internal bookkeeping. See CLK_ENABLE_HAND_OFF flag in clk-provider.h
1056 * for details.
1057 */
1058 if (core->need_handoff_enable) {
1059 core->need_handoff_enable = false;
1060 flags = clk_enable_lock();
1061 clk_core_disable(core);
1062 clk_enable_unlock(flags);
1063 }
1064
Dong Aishenga4b35182016-06-30 17:31:13 +08001065 if (core->flags & CLK_OPS_PARENT_ENABLE)
1066 clk_core_prepare_enable(core->parent);
1067
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001068 flags = clk_enable_lock();
1069
1070 if (core->enable_count)
1071 goto unlock_out;
1072
1073 if (core->flags & CLK_IGNORE_UNUSED)
1074 goto unlock_out;
1075
1076 /*
1077 * some gate clocks have special needs during the disable-unused
1078 * sequence. call .disable_unused if available, otherwise fall
1079 * back to .disable
1080 */
1081 if (clk_core_is_enabled(core)) {
1082 trace_clk_disable(core);
1083 if (core->ops->disable_unused)
1084 core->ops->disable_unused(core->hw);
1085 else if (core->ops->disable)
1086 core->ops->disable(core->hw);
1087 trace_clk_disable_complete(core);
1088 }
1089
1090unlock_out:
1091 clk_enable_unlock(flags);
Dong Aishenga4b35182016-06-30 17:31:13 +08001092 if (core->flags & CLK_OPS_PARENT_ENABLE)
1093 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001094}
1095
1096static bool clk_ignore_unused;
1097static int __init clk_ignore_unused_setup(char *__unused)
1098{
1099 clk_ignore_unused = true;
1100 return 1;
1101}
1102__setup("clk_ignore_unused", clk_ignore_unused_setup);
1103
1104static int clk_disable_unused(void)
1105{
1106 struct clk_core *core;
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301107 struct clk_handoff_vdd *v, *v_temp;
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001108
1109 if (clk_ignore_unused) {
1110 pr_warn("clk: Not disabling unused clocks\n");
1111 return 0;
1112 }
1113
1114 clk_prepare_lock();
1115
1116 hlist_for_each_entry(core, &clk_root_list, child_node)
1117 clk_disable_unused_subtree(core);
1118
1119 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1120 clk_disable_unused_subtree(core);
1121
1122 hlist_for_each_entry(core, &clk_root_list, child_node)
1123 clk_unprepare_unused_subtree(core);
1124
1125 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1126 clk_unprepare_unused_subtree(core);
1127
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301128 list_for_each_entry_safe(v, v_temp, &clk_handoff_vdd_list, list) {
1129 clk_unvote_vdd_level(v->vdd_class,
1130 v->vdd_class->num_levels - 1);
1131 list_del(&v->list);
1132 kfree(v);
1133 };
1134
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001135 clk_prepare_unlock();
1136
1137 return 0;
1138}
1139late_initcall_sync(clk_disable_unused);
1140
Boris Brezillon0817b622015-07-07 20:48:08 +02001141static int clk_core_round_rate_nolock(struct clk_core *core,
1142 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001143{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001144 struct clk_core *parent;
Boris Brezillon0817b622015-07-07 20:48:08 +02001145 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001146
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001147 lockdep_assert_held(&prepare_lock);
1148
Stephen Boydd6968fc2015-04-30 13:54:13 -07001149 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001150 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001151
Stephen Boydd6968fc2015-04-30 13:54:13 -07001152 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +02001153 if (parent) {
1154 req->best_parent_hw = parent->hw;
1155 req->best_parent_rate = parent->rate;
1156 } else {
1157 req->best_parent_hw = NULL;
1158 req->best_parent_rate = 0;
1159 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001160
Stephen Boydd6968fc2015-04-30 13:54:13 -07001161 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001162 return core->ops->determine_rate(core->hw, req);
1163 } else if (core->ops->round_rate) {
1164 rate = core->ops->round_rate(core->hw, req->rate,
1165 &req->best_parent_rate);
1166 if (rate < 0)
1167 return rate;
1168
1169 req->rate = rate;
1170 } else if (core->flags & CLK_SET_RATE_PARENT) {
1171 return clk_core_round_rate_nolock(parent, req);
1172 } else {
1173 req->rate = core->rate;
1174 }
1175
1176 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001177}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001178
1179/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001180 * __clk_determine_rate - get the closest rate actually supported by a clock
1181 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001182 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001183 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001184 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001185 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001186int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001187{
Boris Brezillon0817b622015-07-07 20:48:08 +02001188 if (!hw) {
1189 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001190 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001191 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001192
Boris Brezillon0817b622015-07-07 20:48:08 +02001193 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001194}
1195EXPORT_SYMBOL_GPL(__clk_determine_rate);
1196
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001197unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1198{
1199 int ret;
1200 struct clk_rate_request req;
1201
1202 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1203 req.rate = rate;
1204
1205 ret = clk_core_round_rate_nolock(hw->core, &req);
1206 if (ret)
1207 return 0;
1208
1209 return req.rate;
1210}
1211EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1212
Mike Turquetteb24764902012-03-15 23:11:19 -07001213/**
1214 * clk_round_rate - round the given rate for a clk
1215 * @clk: the clk for which we are rounding a rate
1216 * @rate: the rate which is to be rounded
1217 *
1218 * Takes in a rate as input and rounds it to a rate that the clk can actually
1219 * use which is then returned. If clk doesn't support round_rate operation
1220 * then the parent rate is returned.
1221 */
1222long clk_round_rate(struct clk *clk, unsigned long rate)
1223{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001224 struct clk_rate_request req;
1225 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001226
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001227 if (!clk)
1228 return 0;
1229
Mike Turquetteeab89f62013-03-28 13:59:01 -07001230 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001231
1232 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1233 req.rate = rate;
1234
1235 ret = clk_core_round_rate_nolock(clk->core, &req);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001236 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001237
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001238 if (ret)
1239 return ret;
1240
1241 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001242}
1243EXPORT_SYMBOL_GPL(clk_round_rate);
1244
1245/**
1246 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001247 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001248 * @msg: clk notifier type (see include/linux/clk.h)
1249 * @old_rate: old clk rate
1250 * @new_rate: new clk rate
1251 *
1252 * Triggers a notifier call chain on the clk rate-change notification
1253 * for 'clk'. Passes a pointer to the struct clk and the previous
1254 * and current rates to the notifier callback. Intended to be called by
1255 * internal clock code only. Returns NOTIFY_DONE from the last driver
1256 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1257 * a driver returns that.
1258 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001259static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001260 unsigned long old_rate, unsigned long new_rate)
1261{
1262 struct clk_notifier *cn;
1263 struct clk_notifier_data cnd;
1264 int ret = NOTIFY_DONE;
1265
Mike Turquetteb24764902012-03-15 23:11:19 -07001266 cnd.old_rate = old_rate;
1267 cnd.new_rate = new_rate;
1268
1269 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001270 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001271 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001272 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1273 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -07001274 }
1275 }
1276
1277 return ret;
1278}
1279
1280/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001281 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001282 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001283 *
1284 * Walks the subtree of clks starting with clk and recalculates accuracies as
1285 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001286 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001287 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001288 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001289static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001290{
1291 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001292 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001293
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001294 lockdep_assert_held(&prepare_lock);
1295
Stephen Boydd6968fc2015-04-30 13:54:13 -07001296 if (core->parent)
1297 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001298
Stephen Boydd6968fc2015-04-30 13:54:13 -07001299 if (core->ops->recalc_accuracy)
1300 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001301 parent_accuracy);
1302 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001303 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001304
Stephen Boydd6968fc2015-04-30 13:54:13 -07001305 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001306 __clk_recalc_accuracies(child);
1307}
1308
Stephen Boydd6968fc2015-04-30 13:54:13 -07001309static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001310{
1311 unsigned long accuracy;
1312
1313 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001314 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1315 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001316
Stephen Boydd6968fc2015-04-30 13:54:13 -07001317 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001318 clk_prepare_unlock();
1319
1320 return accuracy;
1321}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001322
1323/**
1324 * clk_get_accuracy - return the accuracy of clk
1325 * @clk: the clk whose accuracy is being returned
1326 *
1327 * Simply returns the cached accuracy of the clk, unless
1328 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1329 * issued.
1330 * If clk is NULL then returns 0.
1331 */
1332long clk_get_accuracy(struct clk *clk)
1333{
1334 if (!clk)
1335 return 0;
1336
1337 return clk_core_get_accuracy(clk->core);
1338}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001339EXPORT_SYMBOL_GPL(clk_get_accuracy);
1340
Stephen Boydd6968fc2015-04-30 13:54:13 -07001341static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001342 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001343{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001344 if (core->ops->recalc_rate)
1345 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001346 return parent_rate;
1347}
1348
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001349/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001350 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001351 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001352 * @msg: notification type (see include/linux/clk.h)
1353 *
1354 * Walks the subtree of clks starting with clk and recalculates rates as it
1355 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001356 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001357 *
1358 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1359 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001360 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001361static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001362{
1363 unsigned long old_rate;
1364 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001365 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001366
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001367 lockdep_assert_held(&prepare_lock);
1368
Stephen Boydd6968fc2015-04-30 13:54:13 -07001369 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001370
Stephen Boydd6968fc2015-04-30 13:54:13 -07001371 if (core->parent)
1372 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001373
Stephen Boydd6968fc2015-04-30 13:54:13 -07001374 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001375
1376 /*
1377 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1378 * & ABORT_RATE_CHANGE notifiers
1379 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001380 if (core->notifier_count && msg)
1381 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001382
Stephen Boydd6968fc2015-04-30 13:54:13 -07001383 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001384 __clk_recalc_rates(child, msg);
1385}
1386
Stephen Boydd6968fc2015-04-30 13:54:13 -07001387static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001388{
1389 unsigned long rate;
1390
1391 clk_prepare_lock();
1392
Stephen Boydd6968fc2015-04-30 13:54:13 -07001393 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1394 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001395
Stephen Boydd6968fc2015-04-30 13:54:13 -07001396 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001397 clk_prepare_unlock();
1398
1399 return rate;
1400}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001401
Mike Turquetteb24764902012-03-15 23:11:19 -07001402/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001403 * clk_get_rate - return the rate of clk
1404 * @clk: the clk whose rate is being returned
1405 *
1406 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1407 * is set, which means a recalc_rate will be issued.
1408 * If clk is NULL then returns 0.
1409 */
1410unsigned long clk_get_rate(struct clk *clk)
1411{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001412 if (!clk)
1413 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001414
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001415 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001416}
1417EXPORT_SYMBOL_GPL(clk_get_rate);
1418
Stephen Boydd6968fc2015-04-30 13:54:13 -07001419static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001420 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001421{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001422 int i;
James Hogan4935b222013-07-29 12:24:59 +01001423
Masahiro Yamada508f8842015-12-28 19:23:08 +09001424 if (!parent)
1425 return -EINVAL;
1426
Masahiro Yamada470b5e22015-12-28 19:23:09 +09001427 for (i = 0; i < core->num_parents; i++)
1428 if (clk_core_get_parent_by_index(core, i) == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001429 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001430
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001431 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001432}
1433
Heiko Stuebnere6500342015-04-22 22:53:05 +02001434/*
1435 * Update the orphan status of @core and all its children.
1436 */
1437static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1438{
1439 struct clk_core *child;
1440
1441 core->orphan = is_orphan;
1442
1443 hlist_for_each_entry(child, &core->children, child_node)
1444 clk_core_update_orphan_status(child, is_orphan);
1445}
1446
Stephen Boydd6968fc2015-04-30 13:54:13 -07001447static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001448{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001449 bool was_orphan = core->orphan;
1450
Stephen Boydd6968fc2015-04-30 13:54:13 -07001451 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001452
James Hogan903efc52013-08-29 12:10:51 +01001453 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001454 bool becomes_orphan = new_parent->orphan;
1455
James Hogan903efc52013-08-29 12:10:51 +01001456 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001457 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001458 new_parent->new_child = NULL;
1459
Stephen Boydd6968fc2015-04-30 13:54:13 -07001460 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001461
1462 if (was_orphan != becomes_orphan)
1463 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001464 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001465 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001466 if (!was_orphan)
1467 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001468 }
James Hogan4935b222013-07-29 12:24:59 +01001469
Stephen Boydd6968fc2015-04-30 13:54:13 -07001470 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001471}
1472
Stephen Boydd6968fc2015-04-30 13:54:13 -07001473static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001474 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001475{
1476 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001477 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001478
1479 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001480 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1481 *
1482 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001483 * clk_enable().
1484 *
1485 * If the clock is not prepared, then a race with
1486 * clk_enable/disable() is impossible since we already have the
1487 * prepare lock (future calls to clk_enable() need to be preceded by
1488 * a clk_prepare()).
1489 *
1490 * If the clock is prepared, migrate the prepared state to the new
1491 * parent and also protect against a race with clk_enable() by
1492 * forcing the clock and the new parent on. This ensures that all
1493 * future calls to clk_enable() are practically NOPs with respect to
1494 * hardware and software states.
1495 *
1496 * See also: Comment for clk_set_parent() below.
1497 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001498
1499 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1500 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1501 clk_core_prepare_enable(old_parent);
1502 clk_core_prepare_enable(parent);
1503 }
1504
1505 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001506 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001507 clk_core_prepare_enable(parent);
1508 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001509 }
1510
1511 /* update the clk tree topology */
1512 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001513 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001514 clk_enable_unlock(flags);
1515
Stephen Boyd3fa22522014-01-15 10:47:22 -08001516 return old_parent;
1517}
1518
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001519static void __clk_set_parent_after(struct clk_core *core,
1520 struct clk_core *parent,
1521 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001522{
1523 /*
1524 * Finish the migration of prepare state and undo the changes done
1525 * for preventing a race with clk_enable().
1526 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001527 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001528 clk_core_disable_lock(core);
1529 clk_core_disable_unprepare(old_parent);
1530 }
1531
1532 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1533 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1534 clk_core_disable_unprepare(parent);
1535 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001536 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001537}
1538
Stephen Boydd6968fc2015-04-30 13:54:13 -07001539static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001540 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001541{
1542 unsigned long flags;
1543 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001544 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001545
Stephen Boydd6968fc2015-04-30 13:54:13 -07001546 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001547
Stephen Boydd6968fc2015-04-30 13:54:13 -07001548 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001549
James Hogan4935b222013-07-29 12:24:59 +01001550 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001551 if (parent && core->ops->set_parent)
1552 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001553
Stephen Boydd6968fc2015-04-30 13:54:13 -07001554 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001555
James Hogan4935b222013-07-29 12:24:59 +01001556 if (ret) {
1557 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001558 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001559 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001560 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001561
James Hogan4935b222013-07-29 12:24:59 +01001562 return ret;
1563 }
1564
Stephen Boydd6968fc2015-04-30 13:54:13 -07001565 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001566
James Hogan4935b222013-07-29 12:24:59 +01001567 return 0;
1568}
1569
Ulf Hanssona093bde2012-08-31 14:21:28 +02001570/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001571 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001572 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001573 * @parent_rate: the "future" rate of clk's parent
1574 *
1575 * Walks the subtree of clks starting with clk, speculating rates as it
1576 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1577 *
1578 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1579 * pre-rate change notifications and returns early if no clks in the
1580 * subtree have subscribed to the notifications. Note that if a clk does not
1581 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001582 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001583 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001584static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001585 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001586{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001587 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001588 unsigned long new_rate;
1589 int ret = NOTIFY_DONE;
1590
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001591 lockdep_assert_held(&prepare_lock);
1592
Stephen Boydd6968fc2015-04-30 13:54:13 -07001593 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001594
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001595 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001596 if (core->notifier_count)
1597 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001598
Mike Turquette86bcfa22014-02-24 16:08:41 -08001599 if (ret & NOTIFY_STOP_MASK) {
1600 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001601 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001602 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001603 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001604
Stephen Boydd6968fc2015-04-30 13:54:13 -07001605 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001606 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001607 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001608 break;
1609 }
1610
1611out:
1612 return ret;
1613}
1614
Stephen Boydd6968fc2015-04-30 13:54:13 -07001615static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001616 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001617{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001618 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001619
Stephen Boydd6968fc2015-04-30 13:54:13 -07001620 core->new_rate = new_rate;
1621 core->new_parent = new_parent;
1622 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001623 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001624 core->new_child = NULL;
1625 if (new_parent && new_parent != core->parent)
1626 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001627
Stephen Boydd6968fc2015-04-30 13:54:13 -07001628 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001629 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001630 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001631 }
1632}
1633
1634/*
1635 * calculate the new rates returning the topmost clock that has to be
1636 * changed.
1637 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001638static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001639 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001640{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001641 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001642 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001643 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001644 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001645 unsigned long min_rate;
1646 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001647 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001648 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001649
Mike Turquette7452b212012-03-26 14:45:36 -07001650 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001651 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001652 return NULL;
1653
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001654 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001655 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001656 if (parent)
1657 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001658
Stephen Boydd6968fc2015-04-30 13:54:13 -07001659 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001660
James Hogan71472c02013-07-29 12:25:00 +01001661 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001662 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001663 struct clk_rate_request req;
1664
1665 req.rate = rate;
1666 req.min_rate = min_rate;
1667 req.max_rate = max_rate;
1668 if (parent) {
1669 req.best_parent_hw = parent->hw;
1670 req.best_parent_rate = parent->rate;
1671 } else {
1672 req.best_parent_hw = NULL;
1673 req.best_parent_rate = 0;
1674 }
1675
1676 ret = core->ops->determine_rate(core->hw, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001677 if (ret < 0)
1678 return NULL;
1679
Boris Brezillon0817b622015-07-07 20:48:08 +02001680 best_parent_rate = req.best_parent_rate;
1681 new_rate = req.rate;
1682 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001683 } else if (core->ops->round_rate) {
1684 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon0817b622015-07-07 20:48:08 +02001685 &best_parent_rate);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001686 if (ret < 0)
1687 return NULL;
1688
1689 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001690 if (new_rate < min_rate || new_rate > max_rate)
1691 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001692 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001693 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001694 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001695 return NULL;
1696 } else {
1697 /* pass-through clock with adjustable parent */
1698 top = clk_calc_new_rates(parent, rate);
1699 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001700 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001701 }
1702
James Hogan71472c02013-07-29 12:25:00 +01001703 /* some clocks must be gated to change parent */
1704 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001705 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001706 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001707 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001708 return NULL;
1709 }
1710
James Hogan71472c02013-07-29 12:25:00 +01001711 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001712 if (parent && core->num_parents > 1) {
1713 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001714 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001715 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001716 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001717 return NULL;
1718 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001719 }
1720
Deepak Katragadda1cabdbe2017-04-28 13:42:50 -07001721 /*
1722 * The Fabia PLLs only have 16 bits to program the fractional divider.
1723 * Hence the programmed rate might be slightly different than the
1724 * requested one.
1725 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001726 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
Deepak Katragadda1cabdbe2017-04-28 13:42:50 -07001727 (DIV_ROUND_CLOSEST(best_parent_rate, 1000) !=
1728 DIV_ROUND_CLOSEST(parent->rate, 1000)))
James Hogan71472c02013-07-29 12:25:00 +01001729 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001730
1731out:
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301732 if (!clk_is_rate_level_valid(core, rate))
1733 return NULL;
1734
Stephen Boydd6968fc2015-04-30 13:54:13 -07001735 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001736
1737 return top;
1738}
1739
1740/*
1741 * Notify about rate changes in a subtree. Always walk down the whole tree
1742 * so that in case of an error we can walk down the whole tree again and
1743 * abort the change.
1744 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001745static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001746 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001747{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001748 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001749 int ret = NOTIFY_DONE;
1750
Stephen Boydd6968fc2015-04-30 13:54:13 -07001751 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301752 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001753
Stephen Boydd6968fc2015-04-30 13:54:13 -07001754 if (core->notifier_count) {
1755 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001756 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001757 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001758 }
1759
Stephen Boydd6968fc2015-04-30 13:54:13 -07001760 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001761 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001762 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001763 continue;
1764 tmp_clk = clk_propagate_rate_change(child, event);
1765 if (tmp_clk)
1766 fail_clk = tmp_clk;
1767 }
1768
Stephen Boydd6968fc2015-04-30 13:54:13 -07001769 /* handle the new child who might not be in core->children yet */
1770 if (core->new_child) {
1771 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001772 if (tmp_clk)
1773 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001774 }
1775
1776 return fail_clk;
1777}
1778
1779/*
1780 * walk down a subtree and set the new rates notifying the rate
1781 * change on the way
1782 */
Taniya Das083a1982016-06-14 16:39:56 +05301783static int clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001784{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001785 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001786 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001787 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001788 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001789 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001790 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001791 struct clk_core *parent = NULL;
Taniya Das083a1982016-06-14 16:39:56 +05301792 int rc = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001793
Stephen Boydd6968fc2015-04-30 13:54:13 -07001794 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001795
Dong Aishengfc8726a2016-06-30 17:31:14 +08001796 if (core->new_parent) {
1797 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001798 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001799 } else if (core->parent) {
1800 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001801 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001802 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001803
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001804 if (core->flags & CLK_SET_RATE_UNGATE) {
1805 unsigned long flags;
1806
1807 clk_core_prepare(core);
1808 flags = clk_enable_lock();
1809 clk_core_enable(core);
1810 clk_enable_unlock(flags);
1811 }
1812
Taniya Das0624f562017-05-10 15:23:22 +05301813 trace_clk_set_rate(core, core->new_rate);
1814
1815 /* Enforce vdd requirements for new frequency. */
1816 if (core->prepare_count) {
1817 rc = clk_vote_rate_vdd(core, core->new_rate);
1818 if (rc)
1819 goto out;
1820 }
1821
Stephen Boydd6968fc2015-04-30 13:54:13 -07001822 if (core->new_parent && core->new_parent != core->parent) {
1823 old_parent = __clk_set_parent_before(core, core->new_parent);
1824 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001825
Stephen Boydd6968fc2015-04-30 13:54:13 -07001826 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001827 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001828 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001829 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001830 core->new_parent_index);
1831 } else if (core->ops->set_parent) {
1832 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001833 }
1834
Stephen Boydd6968fc2015-04-30 13:54:13 -07001835 trace_clk_set_parent_complete(core, core->new_parent);
1836 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001837 }
1838
Dong Aishengfc8726a2016-06-30 17:31:14 +08001839 if (core->flags & CLK_OPS_PARENT_ENABLE)
1840 clk_core_prepare_enable(parent);
1841
Taniya Das083a1982016-06-14 16:39:56 +05301842 if (!skip_set_rate && core->ops->set_rate) {
1843 rc = core->ops->set_rate(core->hw, core->new_rate,
1844 best_parent_rate);
1845 if (rc)
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301846 goto err_set_rate;
Taniya Das083a1982016-06-14 16:39:56 +05301847 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001848
Stephen Boydd6968fc2015-04-30 13:54:13 -07001849 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001850
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301851 /* Release vdd requirements for old frequency. */
1852 if (core->prepare_count)
1853 clk_unvote_rate_vdd(core, old_rate);
1854
Stephen Boydd6968fc2015-04-30 13:54:13 -07001855 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001856
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001857 if (core->flags & CLK_SET_RATE_UNGATE) {
1858 unsigned long flags;
1859
1860 flags = clk_enable_lock();
1861 clk_core_disable(core);
1862 clk_enable_unlock(flags);
1863 clk_core_unprepare(core);
1864 }
1865
Dong Aishengfc8726a2016-06-30 17:31:14 +08001866 if (core->flags & CLK_OPS_PARENT_ENABLE)
1867 clk_core_disable_unprepare(parent);
1868
Stephen Boydd6968fc2015-04-30 13:54:13 -07001869 if (core->notifier_count && old_rate != core->rate)
1870 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001871
Michael Turquette85e88fa2015-06-20 12:18:03 -07001872 if (core->flags & CLK_RECALC_NEW_RATES)
1873 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001874
Deepak Katragaddabfb96502018-02-07 10:37:41 -08001875 if (core->flags & CLK_CHILD_NO_RATE_PROP)
1876 return rc;
Tero Kristo067bb172014-08-21 16:47:45 +03001877 /*
1878 * Use safe iteration, as change_rate can actually swap parents
1879 * for certain clock types.
1880 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001881 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001882 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001883 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001884 continue;
Taniya Das083a1982016-06-14 16:39:56 +05301885 rc = clk_change_rate(child);
1886 if (rc)
1887 return rc;
James Hogan71472c02013-07-29 12:25:00 +01001888 }
1889
Stephen Boydd6968fc2015-04-30 13:54:13 -07001890 /* handle the new child who might not be in core->children yet */
1891 if (core->new_child)
Taniya Das083a1982016-06-14 16:39:56 +05301892 rc = clk_change_rate(core->new_child);
1893
1894 return rc;
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301895
1896err_set_rate:
1897 if (core->prepare_count)
1898 clk_unvote_rate_vdd(core, core->new_rate);
Taniya Das083a1982016-06-14 16:39:56 +05301899out:
1900 trace_clk_set_rate_complete(core, core->new_rate);
1901
1902 return rc;
Mike Turquetteb24764902012-03-15 23:11:19 -07001903}
1904
Stephen Boydd6968fc2015-04-30 13:54:13 -07001905static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001906 unsigned long req_rate)
1907{
1908 struct clk_core *top, *fail_clk;
1909 unsigned long rate = req_rate;
Taniya Das083a1982016-06-14 16:39:56 +05301910 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001911
Stephen Boydd6968fc2015-04-30 13:54:13 -07001912 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001913 return 0;
1914
1915 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001916 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001917 return 0;
1918
Stephen Boydd6968fc2015-04-30 13:54:13 -07001919 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001920 return -EBUSY;
1921
1922 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001923 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001924 if (!top)
1925 return -EINVAL;
1926
1927 /* notify that we are about to change rates */
1928 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1929 if (fail_clk) {
Taniya Das083a1982016-06-14 16:39:56 +05301930 pr_debug("%s: failed to set %s clock to run at %lu\n", __func__,
1931 fail_clk->name, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001932 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1933 return -EBUSY;
1934 }
1935
1936 /* change the rates */
Taniya Das083a1982016-06-14 16:39:56 +05301937 ret = clk_change_rate(top);
1938 if (ret) {
1939 pr_err("%s: failed to set %s clock to run at %lu\n", __func__,
1940 top->name, req_rate);
1941 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1942 return ret;
1943 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001944
Stephen Boydd6968fc2015-04-30 13:54:13 -07001945 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001946
Taniya Das083a1982016-06-14 16:39:56 +05301947 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001948}
1949
Mike Turquetteb24764902012-03-15 23:11:19 -07001950/**
1951 * clk_set_rate - specify a new rate for clk
1952 * @clk: the clk whose rate is being changed
1953 * @rate: the new rate for clk
1954 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001955 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001956 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001957 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1958 * propagate up to clk's parent; whether or not this happens depends on the
1959 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1960 * after calling .round_rate then upstream parent propagation is ignored. If
1961 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001962 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001963 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1964 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001965 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001966 * Rate changes are accomplished via tree traversal that also recalculates the
1967 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001968 *
1969 * Returns 0 on success, -EERROR otherwise.
1970 */
1971int clk_set_rate(struct clk *clk, unsigned long rate)
1972{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001973 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001974
Mike Turquette89ac8d72013-08-21 23:58:09 -07001975 if (!clk)
1976 return 0;
1977
Mike Turquetteb24764902012-03-15 23:11:19 -07001978 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001979 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001980
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001981 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001982
Mike Turquetteeab89f62013-03-28 13:59:01 -07001983 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001984
1985 return ret;
1986}
1987EXPORT_SYMBOL_GPL(clk_set_rate);
1988
1989/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001990 * clk_set_rate_range - set a rate range for a clock source
1991 * @clk: clock source
1992 * @min: desired minimum clock rate in Hz, inclusive
1993 * @max: desired maximum clock rate in Hz, inclusive
1994 *
1995 * Returns success (0) or negative errno.
1996 */
1997int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1998{
1999 int ret = 0;
2000
2001 if (!clk)
2002 return 0;
2003
2004 if (min > max) {
2005 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2006 __func__, clk->core->name, clk->dev_id, clk->con_id,
2007 min, max);
2008 return -EINVAL;
2009 }
2010
2011 clk_prepare_lock();
2012
2013 if (min != clk->min_rate || max != clk->max_rate) {
2014 clk->min_rate = min;
2015 clk->max_rate = max;
2016 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2017 }
2018
2019 clk_prepare_unlock();
2020
2021 return ret;
2022}
2023EXPORT_SYMBOL_GPL(clk_set_rate_range);
2024
2025/**
2026 * clk_set_min_rate - set a minimum clock rate for a clock source
2027 * @clk: clock source
2028 * @rate: desired minimum clock rate in Hz, inclusive
2029 *
2030 * Returns success (0) or negative errno.
2031 */
2032int clk_set_min_rate(struct clk *clk, unsigned long rate)
2033{
2034 if (!clk)
2035 return 0;
2036
2037 return clk_set_rate_range(clk, rate, clk->max_rate);
2038}
2039EXPORT_SYMBOL_GPL(clk_set_min_rate);
2040
2041/**
2042 * clk_set_max_rate - set a maximum clock rate for a clock source
2043 * @clk: clock source
2044 * @rate: desired maximum clock rate in Hz, inclusive
2045 *
2046 * Returns success (0) or negative errno.
2047 */
2048int clk_set_max_rate(struct clk *clk, unsigned long rate)
2049{
2050 if (!clk)
2051 return 0;
2052
2053 return clk_set_rate_range(clk, clk->min_rate, rate);
2054}
2055EXPORT_SYMBOL_GPL(clk_set_max_rate);
2056
2057/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002058 * clk_get_parent - return the parent of a clk
2059 * @clk: the clk whose parent gets returned
2060 *
2061 * Simply returns clk->parent. Returns NULL if clk is NULL.
2062 */
2063struct clk *clk_get_parent(struct clk *clk)
2064{
2065 struct clk *parent;
2066
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002067 if (!clk)
2068 return NULL;
2069
Mike Turquetteeab89f62013-03-28 13:59:01 -07002070 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002071 /* TODO: Create a per-user clk and change callers to call clk_put */
2072 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002073 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002074
2075 return parent;
2076}
2077EXPORT_SYMBOL_GPL(clk_get_parent);
2078
Stephen Boydd6968fc2015-04-30 13:54:13 -07002079static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002080{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002081 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002082
Masahiro Yamada2430a942016-02-09 20:19:14 +09002083 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002084 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002085
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002086 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002087}
2088
Stephen Boydd6968fc2015-04-30 13:54:13 -07002089static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002090 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002091{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002092 clk_reparent(core, new_parent);
2093 __clk_recalc_accuracies(core);
2094 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002095}
2096
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002097void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2098{
2099 if (!hw)
2100 return;
2101
2102 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2103}
2104
Mike Turquetteb24764902012-03-15 23:11:19 -07002105/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002106 * clk_has_parent - check if a clock is a possible parent for another
2107 * @clk: clock source
2108 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002109 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002110 * This function can be used in drivers that need to check that a clock can be
2111 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002112 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002113 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002114 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002115bool clk_has_parent(struct clk *clk, struct clk *parent)
2116{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002117 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002118 unsigned int i;
2119
2120 /* NULL clocks should be nops, so return success if either is NULL. */
2121 if (!clk || !parent)
2122 return true;
2123
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002124 core = clk->core;
2125 parent_core = parent->core;
2126
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002127 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002128 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002129 return true;
2130
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002131 for (i = 0; i < core->num_parents; i++)
2132 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002133 return true;
2134
2135 return false;
2136}
2137EXPORT_SYMBOL_GPL(clk_has_parent);
2138
Stephen Boydd6968fc2015-04-30 13:54:13 -07002139static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002140{
2141 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002142 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002143 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002144
Stephen Boydd6968fc2015-04-30 13:54:13 -07002145 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002146 return 0;
2147
Mike Turquetteb24764902012-03-15 23:11:19 -07002148 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002149 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002150
Taniya Das8436bd72016-11-21 17:50:13 +05302151 if (core->parent == parent && !(core->flags & CLK_IS_MEASURE))
Mike Turquetteb24764902012-03-15 23:11:19 -07002152 goto out;
2153
Stephen Boydb61c43c2015-02-02 14:11:25 -08002154 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002155 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08002156 ret = -ENOSYS;
2157 goto out;
2158 }
2159
Ulf Hansson031dcc92013-04-02 23:09:38 +02002160 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002161 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002162 ret = -EBUSY;
2163 goto out;
2164 }
2165
2166 /* try finding the new parent index */
2167 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002168 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002169 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002170 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002171 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002172 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002173 goto out;
2174 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002175 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002176 }
2177
Mike Turquetteb24764902012-03-15 23:11:19 -07002178 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002179 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002180
2181 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002182 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07002183 goto out;
2184
Ulf Hansson031dcc92013-04-02 23:09:38 +02002185 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002186 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002187
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002188 /* propagate rate an accuracy recalculation accordingly */
2189 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002190 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002191 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002192 __clk_recalc_rates(core, POST_RATE_CHANGE);
2193 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002194 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002195
2196out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002197 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002198
2199 return ret;
2200}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002201
2202/**
2203 * clk_set_parent - switch the parent of a mux clk
2204 * @clk: the mux clk whose input we are switching
2205 * @parent: the new input to clk
2206 *
2207 * Re-parent clk to use parent as its new input source. If clk is in
2208 * prepared state, the clk will get enabled for the duration of this call. If
2209 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2210 * that, the reparenting is glitchy in hardware, etc), use the
2211 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2212 *
2213 * After successfully changing clk's parent clk_set_parent will update the
2214 * clk topology, sysfs topology and propagate rate recalculation via
2215 * __clk_recalc_rates.
2216 *
2217 * Returns 0 on success, -EERROR otherwise.
2218 */
2219int clk_set_parent(struct clk *clk, struct clk *parent)
2220{
2221 if (!clk)
2222 return 0;
2223
2224 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
2225}
Mike Turquetteb24764902012-03-15 23:11:19 -07002226EXPORT_SYMBOL_GPL(clk_set_parent);
2227
2228/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002229 * clk_set_phase - adjust the phase shift of a clock signal
2230 * @clk: clock signal source
2231 * @degrees: number of degrees the signal is shifted
2232 *
2233 * Shifts the phase of a clock signal by the specified
2234 * degrees. Returns 0 on success, -EERROR otherwise.
2235 *
2236 * This function makes no distinction about the input or reference
2237 * signal that we adjust the clock signal phase against. For example
2238 * phase locked-loop clock signal generators we may shift phase with
2239 * respect to feedback clock signal input, but for other cases the
2240 * clock phase may be shifted with respect to some other, unspecified
2241 * signal.
2242 *
2243 * Additionally the concept of phase shift does not propagate through
2244 * the clock tree hierarchy, which sets it apart from clock rates and
2245 * clock accuracy. A parent clock phase attribute does not have an
2246 * impact on the phase attribute of a child clock.
2247 */
2248int clk_set_phase(struct clk *clk, int degrees)
2249{
Stephen Boyd08b95752015-02-02 14:09:43 -08002250 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08002251
2252 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002253 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002254
2255 /* sanity check degrees */
2256 degrees %= 360;
2257 if (degrees < 0)
2258 degrees += 360;
2259
2260 clk_prepare_lock();
2261
Stephen Boyddfc202e2015-02-02 14:37:41 -08002262 trace_clk_set_phase(clk->core, degrees);
2263
Stephen Boyd08b95752015-02-02 14:09:43 -08002264 if (clk->core->ops->set_phase)
2265 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002266
Stephen Boyddfc202e2015-02-02 14:37:41 -08002267 trace_clk_set_phase_complete(clk->core, degrees);
2268
Mike Turquettee59c5372014-02-18 21:21:25 -08002269 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002270 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08002271
Mike Turquettee59c5372014-02-18 21:21:25 -08002272 clk_prepare_unlock();
2273
Mike Turquettee59c5372014-02-18 21:21:25 -08002274 return ret;
2275}
Maxime Ripard9767b042015-01-20 22:23:43 +01002276EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002277
Stephen Boydd6968fc2015-04-30 13:54:13 -07002278static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002279{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002280 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002281
2282 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07002283 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002284 clk_prepare_unlock();
2285
Mike Turquettee59c5372014-02-18 21:21:25 -08002286 return ret;
2287}
2288
2289/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002290 * clk_get_phase - return the phase shift of a clock signal
2291 * @clk: clock signal source
2292 *
2293 * Returns the phase shift of a clock node in degrees, otherwise returns
2294 * -EERROR.
2295 */
2296int clk_get_phase(struct clk *clk)
2297{
2298 if (!clk)
2299 return 0;
2300
2301 return clk_core_get_phase(clk->core);
2302}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002303EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002304
2305/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002306 * clk_is_match - check if two clk's point to the same hardware clock
2307 * @p: clk compared against q
2308 * @q: clk compared against p
2309 *
2310 * Returns true if the two struct clk pointers both point to the same hardware
2311 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2312 * share the same struct clk_core object.
2313 *
2314 * Returns false otherwise. Note that two NULL clks are treated as matching.
2315 */
2316bool clk_is_match(const struct clk *p, const struct clk *q)
2317{
2318 /* trivial case: identical struct clk's or both NULL */
2319 if (p == q)
2320 return true;
2321
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002322 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002323 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2324 if (p->core == q->core)
2325 return true;
2326
2327 return false;
2328}
2329EXPORT_SYMBOL_GPL(clk_is_match);
2330
Taniya Das63c20c72016-06-15 12:15:01 +05302331int clk_set_flags(struct clk *clk, unsigned long flags)
2332{
2333 if (!clk)
2334 return 0;
2335
2336 if (!clk->core->ops->set_flags)
2337 return -EINVAL;
2338
2339 return clk->core->ops->set_flags(clk->core->hw, flags);
2340}
2341EXPORT_SYMBOL_GPL(clk_set_flags);
2342
Taniya Das08d79242017-04-13 12:24:51 +05302343unsigned long clk_list_frequency(struct clk *clk, unsigned int index)
2344{
2345 int ret = 0;
2346
2347 if (!clk || !clk->core->ops->list_rate)
2348 return -EINVAL;
2349
2350 clk_prepare_lock();
2351 ret = clk->core->ops->list_rate(clk->core->hw, index, ULONG_MAX);
2352 clk_prepare_unlock();
2353
2354 return ret;
2355}
2356EXPORT_SYMBOL_GPL(clk_list_frequency);
2357
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002358/*** debugfs support ***/
2359
2360#ifdef CONFIG_DEBUG_FS
2361#include <linux/debugfs.h>
2362
2363static struct dentry *rootdir;
2364static int inited = 0;
Taniya Das6c15e542016-11-07 10:08:30 +05302365static u32 debug_suspend;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002366static DEFINE_MUTEX(clk_debug_lock);
2367static HLIST_HEAD(clk_debug_list);
2368
2369static struct hlist_head *all_lists[] = {
2370 &clk_root_list,
2371 &clk_orphan_list,
2372 NULL,
2373};
2374
2375static struct hlist_head *orphan_list[] = {
2376 &clk_orphan_list,
2377 NULL,
2378};
2379
Amit Nischal15aca8c2017-03-06 16:40:58 +05302380static void clk_state_subtree(struct clk_core *c)
2381{
2382 int vdd_level = 0;
2383 struct clk_core *child;
2384
2385 if (!c)
2386 return;
2387
2388 if (c->vdd_class) {
2389 vdd_level = clk_find_vdd_level(c, c->rate);
2390 if (vdd_level < 0)
2391 vdd_level = 0;
2392 }
2393
2394 trace_clk_state(c->name, c->prepare_count, c->enable_count,
2395 c->rate, vdd_level);
2396
2397 hlist_for_each_entry(child, &c->children, child_node)
2398 clk_state_subtree(child);
2399}
2400
2401static int clk_state_show(struct seq_file *s, void *data)
2402{
2403 struct clk_core *c;
2404 struct hlist_head **lists = (struct hlist_head **)s->private;
2405
2406 clk_prepare_lock();
2407
2408 for (; *lists; lists++)
2409 hlist_for_each_entry(c, *lists, child_node)
2410 clk_state_subtree(c);
2411
2412 clk_prepare_unlock();
2413
2414 return 0;
2415}
2416
2417
2418static int clk_state_open(struct inode *inode, struct file *file)
2419{
2420 return single_open(file, clk_state_show, inode->i_private);
2421}
2422
2423static const struct file_operations clk_state_fops = {
2424 .open = clk_state_open,
2425 .read = seq_read,
2426 .llseek = seq_lseek,
2427 .release = single_release,
2428};
2429
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002430static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2431 int level)
2432{
2433 if (!c)
2434 return;
2435
2436 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
2437 level * 3 + 1, "",
2438 30 - level * 3, c->name,
2439 c->enable_count, c->prepare_count, clk_core_get_rate(c),
2440 clk_core_get_accuracy(c), clk_core_get_phase(c));
2441}
2442
2443static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2444 int level)
2445{
2446 struct clk_core *child;
2447
2448 if (!c)
2449 return;
2450
2451 clk_summary_show_one(s, c, level);
2452
2453 hlist_for_each_entry(child, &c->children, child_node)
2454 clk_summary_show_subtree(s, child, level + 1);
2455}
2456
2457static int clk_summary_show(struct seq_file *s, void *data)
2458{
2459 struct clk_core *c;
2460 struct hlist_head **lists = (struct hlist_head **)s->private;
2461
2462 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
2463 seq_puts(s, "----------------------------------------------------------------------------------------\n");
2464
2465 clk_prepare_lock();
2466
2467 for (; *lists; lists++)
2468 hlist_for_each_entry(c, *lists, child_node)
2469 clk_summary_show_subtree(s, c, 0);
2470
2471 clk_prepare_unlock();
2472
2473 return 0;
2474}
2475
2476
2477static int clk_summary_open(struct inode *inode, struct file *file)
2478{
2479 return single_open(file, clk_summary_show, inode->i_private);
2480}
2481
2482static const struct file_operations clk_summary_fops = {
2483 .open = clk_summary_open,
2484 .read = seq_read,
2485 .llseek = seq_lseek,
2486 .release = single_release,
2487};
2488
2489static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2490{
2491 if (!c)
2492 return;
2493
Stefan Wahren7cb81132015-04-29 16:36:43 +00002494 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002495 seq_printf(s, "\"%s\": { ", c->name);
2496 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2497 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002498 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2499 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002500 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2501}
2502
2503static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2504{
2505 struct clk_core *child;
2506
2507 if (!c)
2508 return;
2509
2510 clk_dump_one(s, c, level);
2511
2512 hlist_for_each_entry(child, &c->children, child_node) {
2513 seq_printf(s, ",");
2514 clk_dump_subtree(s, child, level + 1);
2515 }
2516
2517 seq_printf(s, "}");
2518}
2519
2520static int clk_dump(struct seq_file *s, void *data)
2521{
2522 struct clk_core *c;
2523 bool first_node = true;
2524 struct hlist_head **lists = (struct hlist_head **)s->private;
2525
2526 seq_printf(s, "{");
2527
2528 clk_prepare_lock();
2529
2530 for (; *lists; lists++) {
2531 hlist_for_each_entry(c, *lists, child_node) {
2532 if (!first_node)
2533 seq_puts(s, ",");
2534 first_node = false;
2535 clk_dump_subtree(s, c, 0);
2536 }
2537 }
2538
2539 clk_prepare_unlock();
2540
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002541 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002542 return 0;
2543}
2544
2545
2546static int clk_dump_open(struct inode *inode, struct file *file)
2547{
2548 return single_open(file, clk_dump, inode->i_private);
2549}
2550
2551static const struct file_operations clk_dump_fops = {
2552 .open = clk_dump_open,
2553 .read = seq_read,
2554 .llseek = seq_lseek,
2555 .release = single_release,
2556};
2557
Taniya Das2dd25722016-11-14 11:26:02 +05302558static int clock_debug_rate_set(void *data, u64 val)
2559{
2560 struct clk_core *core = data;
2561 int ret;
2562
2563 ret = clk_set_rate(core->hw->clk, val);
2564 if (ret)
2565 pr_err("clk_set_rate(%lu) failed (%d)\n",
2566 (unsigned long)val, ret);
2567
2568 return ret;
2569}
2570
2571static int clock_debug_rate_get(void *data, u64 *val)
2572{
2573 struct clk_core *core = data;
2574
2575 *val = core->hw->core->rate;
2576
2577 return 0;
2578}
2579
2580DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
2581 clock_debug_rate_set, "%llu\n");
2582
2583static ssize_t clock_parent_read(struct file *filp, char __user *ubuf,
2584 size_t cnt, loff_t *ppos)
2585{
2586 char name[256] = {0};
2587 struct clk_core *core = filp->private_data;
2588 struct clk_core *p = core->hw->core->parent;
2589
2590 snprintf(name, sizeof(name), "%s\n", p ? p->name : "None\n");
2591
2592 return simple_read_from_buffer(ubuf, cnt, ppos, name, strlen(name));
2593}
2594
2595static const struct file_operations clock_parent_fops = {
2596 .open = simple_open,
2597 .read = clock_parent_read,
2598};
2599
2600static int clock_debug_enable_set(void *data, u64 val)
2601{
2602 struct clk_core *core = data;
2603 int rc = 0;
2604
2605 if (val)
2606 rc = clk_prepare_enable(core->hw->clk);
2607 else
2608 clk_disable_unprepare(core->hw->clk);
2609
2610 return rc;
2611}
2612
2613static int clock_debug_enable_get(void *data, u64 *val)
2614{
2615 struct clk_core *core = data;
2616 int enabled = 0;
2617
2618 enabled = core->enable_count;
2619
2620 *val = enabled;
2621
2622 return 0;
2623}
2624
2625DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
2626 clock_debug_enable_set, "%lld\n");
2627
2628#define clock_debug_output(m, c, fmt, ...) \
2629do { \
2630 if (m) \
2631 seq_printf(m, fmt, ##__VA_ARGS__); \
2632 else if (c) \
2633 pr_cont(fmt, ##__VA_ARGS__); \
2634 else \
2635 pr_info(fmt, ##__VA_ARGS__); \
2636} while (0)
2637
Taniya Dasa5aadd72017-01-23 12:24:10 +05302638/*
2639 * clock_debug_print_enabled_debug_suspend() - Print names of enabled clocks
2640 * during suspend.
2641 */
2642static void clock_debug_print_enabled_debug_suspend(struct seq_file *s)
2643{
2644 struct clk_core *core;
2645 int cnt = 0;
2646
2647 if (!mutex_trylock(&clk_debug_lock))
2648 return;
2649
2650 clock_debug_output(s, 0, "Enabled clocks:\n");
2651
2652 hlist_for_each_entry(core, &clk_debug_list, debug_node) {
2653 if (!core || !core->prepare_count)
2654 continue;
2655
2656 if (core->vdd_class)
2657 clock_debug_output(s, 0, " %s:%u:%u [%ld, %d]",
2658 core->name, core->prepare_count,
2659 core->enable_count, core->rate,
2660 clk_find_vdd_level(core, core->rate));
2661
2662 else
2663 clock_debug_output(s, 0, " %s:%u:%u [%ld]",
2664 core->name, core->prepare_count,
2665 core->enable_count, core->rate);
2666 cnt++;
2667 }
2668
2669 mutex_unlock(&clk_debug_lock);
2670
2671 if (cnt)
2672 clock_debug_output(s, 0, "Enabled clock count: %d\n", cnt);
2673 else
2674 clock_debug_output(s, 0, "No clocks enabled.\n");
2675}
2676
2677static int clock_debug_print_clock(struct clk_core *c, struct seq_file *s)
Taniya Das2dd25722016-11-14 11:26:02 +05302678{
2679 char *start = "";
2680 struct clk *clk;
2681
2682 if (!c || !c->prepare_count)
2683 return 0;
2684
2685 clk = c->hw->clk;
2686
2687 clock_debug_output(s, 0, "\t");
2688
2689 do {
Taniya Das876112d2016-11-14 11:54:02 +05302690 if (clk->core->vdd_class)
2691 clock_debug_output(s, 1, "%s%s:%u:%u [%ld, %d]", start,
2692 clk->core->name,
2693 clk->core->prepare_count,
2694 clk->core->enable_count,
2695 clk->core->rate,
2696 clk_find_vdd_level(clk->core, clk->core->rate));
2697 else
2698 clock_debug_output(s, 1, "%s%s:%u:%u [%ld]", start,
Taniya Das2dd25722016-11-14 11:26:02 +05302699 clk->core->name,
2700 clk->core->prepare_count,
2701 clk->core->enable_count,
2702 clk->core->rate);
2703 start = " -> ";
2704 } while ((clk = clk_get_parent(clk)));
2705
2706 clock_debug_output(s, 1, "\n");
2707
2708 return 1;
2709}
2710
2711/*
2712 * clock_debug_print_enabled_clocks() - Print names of enabled clocks
2713 */
2714static void clock_debug_print_enabled_clocks(struct seq_file *s)
2715{
2716 struct clk_core *core;
2717 int cnt = 0;
2718
Shefali Jain5c15a992017-09-20 16:01:01 +05302719 if (!mutex_trylock(&clk_debug_lock))
2720 return;
Taniya Das2dd25722016-11-14 11:26:02 +05302721
Shefali Jain5c15a992017-09-20 16:01:01 +05302722 clock_debug_output(s, 0, "Enabled clocks:\n");
Taniya Das2dd25722016-11-14 11:26:02 +05302723
2724 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2725 cnt += clock_debug_print_clock(core, s);
2726
2727 mutex_unlock(&clk_debug_lock);
2728
2729 if (cnt)
2730 clock_debug_output(s, 0, "Enabled clock count: %d\n", cnt);
2731 else
2732 clock_debug_output(s, 0, "No clocks enabled.\n");
2733}
2734
2735static int enabled_clocks_show(struct seq_file *s, void *unused)
2736{
2737 clock_debug_print_enabled_clocks(s);
2738
2739 return 0;
2740}
2741
2742static int enabled_clocks_open(struct inode *inode, struct file *file)
2743{
2744 return single_open(file, enabled_clocks_show, inode->i_private);
2745}
2746
2747static const struct file_operations clk_enabled_list_fops = {
2748 .open = enabled_clocks_open,
2749 .read = seq_read,
2750 .llseek = seq_lseek,
2751 .release = seq_release,
2752};
2753
Taniya Das8436bd72016-11-21 17:50:13 +05302754void clk_debug_print_hw(struct clk_core *clk, struct seq_file *f)
Taniya Das2dd25722016-11-14 11:26:02 +05302755{
2756 if (IS_ERR_OR_NULL(clk))
2757 return;
2758
2759 clk_debug_print_hw(clk->parent, f);
2760
2761 clock_debug_output(f, false, "%s\n", clk->name);
2762
2763 if (!clk->ops->list_registers)
2764 return;
2765
2766 clk->ops->list_registers(f, clk->hw);
2767}
2768
2769static int print_hw_show(struct seq_file *m, void *unused)
2770{
2771 struct clk_core *c = m->private;
2772
2773 clk_debug_print_hw(c, m);
2774
2775 return 0;
2776}
2777
2778static int print_hw_open(struct inode *inode, struct file *file)
2779{
2780 return single_open(file, print_hw_show, inode->i_private);
2781}
2782
2783static const struct file_operations clock_print_hw_fops = {
2784 .open = print_hw_open,
2785 .read = seq_read,
2786 .llseek = seq_lseek,
2787 .release = seq_release,
2788};
2789
Taniya Das876112d2016-11-14 11:54:02 +05302790static int list_rates_show(struct seq_file *s, void *unused)
2791{
2792 struct clk_core *core = s->private;
2793 int level = 0, i = 0;
2794 unsigned long rate, rate_max = 0;
2795
2796 /* Find max frequency supported within voltage constraints. */
2797 if (!core->vdd_class) {
2798 rate_max = ULONG_MAX;
2799 } else {
2800 for (level = 0; level < core->num_rate_max; level++)
2801 if (core->rate_max[level])
2802 rate_max = core->rate_max[level];
2803 }
2804
2805 /*
2806 * List supported frequencies <= rate_max. Higher frequencies may
2807 * appear in the frequency table, but are not valid and should not
2808 * be listed.
2809 */
2810 while (!IS_ERR_VALUE(rate =
2811 core->ops->list_rate(core->hw, i++, rate_max))) {
2812 if (rate <= 0)
2813 break;
2814 if (rate <= rate_max)
2815 seq_printf(s, "%lu\n", rate);
2816 }
2817
2818 return 0;
2819}
2820
2821static int list_rates_open(struct inode *inode, struct file *file)
2822{
2823 return single_open(file, list_rates_show, inode->i_private);
2824}
2825
2826static const struct file_operations list_rates_fops = {
2827 .open = list_rates_open,
2828 .read = seq_read,
2829 .llseek = seq_lseek,
2830 .release = seq_release,
2831};
2832
2833static void clock_print_rate_max_by_level(struct seq_file *s, int level)
2834{
2835 struct clk_core *core = s->private;
2836 struct clk_vdd_class *vdd_class = core->vdd_class;
2837 int off, i, vdd_level, nregs = vdd_class->num_regulators;
2838
2839 vdd_level = clk_find_vdd_level(core, core->rate);
2840
2841 seq_printf(s, "%2s%10lu", vdd_level == level ? "[" : "",
2842 core->rate_max[level]);
2843
2844 for (i = 0; i < nregs; i++) {
2845 off = nregs*level + i;
2846 if (vdd_class->vdd_uv)
2847 seq_printf(s, "%10u", vdd_class->vdd_uv[off]);
2848 }
2849
2850 if (vdd_level == level)
2851 seq_puts(s, "]");
2852
2853 seq_puts(s, "\n");
2854}
2855
2856static int rate_max_show(struct seq_file *s, void *unused)
2857{
2858 struct clk_core *core = s->private;
2859 struct clk_vdd_class *vdd_class = core->vdd_class;
2860 int level = 0, i, nregs = vdd_class->num_regulators;
2861 char reg_name[10];
2862
2863 int vdd_level = clk_find_vdd_level(core, core->rate);
2864
2865 if (vdd_level < 0) {
2866 seq_printf(s, "could not find_vdd_level for %s, %ld\n",
2867 core->name, core->rate);
2868 return 0;
2869 }
2870
2871 seq_printf(s, "%12s", "");
2872 for (i = 0; i < nregs; i++) {
2873 snprintf(reg_name, ARRAY_SIZE(reg_name), "reg %d", i);
2874 seq_printf(s, "%10s", reg_name);
2875 }
2876
2877 seq_printf(s, "\n%12s", "freq");
2878 for (i = 0; i < nregs; i++)
2879 seq_printf(s, "%10s", "uV");
2880
2881 seq_puts(s, "\n");
2882
2883 for (level = 0; level < core->num_rate_max; level++)
2884 clock_print_rate_max_by_level(s, level);
2885
2886 return 0;
2887}
2888
2889static int rate_max_open(struct inode *inode, struct file *file)
2890{
2891 return single_open(file, rate_max_show, inode->i_private);
2892}
2893
2894static const struct file_operations rate_max_fops = {
2895 .open = rate_max_open,
2896 .read = seq_read,
2897 .llseek = seq_lseek,
2898 .release = seq_release,
2899};
2900
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002901static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2902{
2903 struct dentry *d;
2904 int ret = -ENOMEM;
2905
2906 if (!core || !pdentry) {
2907 ret = -EINVAL;
2908 goto out;
2909 }
2910
2911 d = debugfs_create_dir(core->name, pdentry);
2912 if (!d)
2913 goto out;
2914
2915 core->dentry = d;
2916
Taniya Das2dd25722016-11-14 11:26:02 +05302917 d = debugfs_create_file("clk_rate", 0444, core->dentry, core,
2918 &clock_rate_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002919 if (!d)
2920 goto err_out;
2921
Taniya Das876112d2016-11-14 11:54:02 +05302922 if (core->ops->list_rate) {
2923 if (!debugfs_create_file("clk_list_rates",
2924 0444, core->dentry, core, &list_rates_fops))
2925 goto err_out;
2926 }
2927
2928 if (core->vdd_class && !debugfs_create_file("clk_rate_max",
2929 0444, core->dentry, core, &rate_max_fops))
2930 goto err_out;
2931
Taniya Das2dd25722016-11-14 11:26:02 +05302932 d = debugfs_create_u32("clk_accuracy", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002933 (u32 *)&core->accuracy);
2934 if (!d)
2935 goto err_out;
2936
Taniya Das2dd25722016-11-14 11:26:02 +05302937 d = debugfs_create_u32("clk_phase", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002938 (u32 *)&core->phase);
2939 if (!d)
2940 goto err_out;
2941
Taniya Das2dd25722016-11-14 11:26:02 +05302942 d = debugfs_create_x32("clk_flags", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002943 (u32 *)&core->flags);
2944 if (!d)
2945 goto err_out;
2946
Taniya Das2dd25722016-11-14 11:26:02 +05302947 d = debugfs_create_u32("clk_prepare_count", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002948 (u32 *)&core->prepare_count);
2949 if (!d)
2950 goto err_out;
2951
Taniya Das2dd25722016-11-14 11:26:02 +05302952 d = debugfs_create_file("clk_enable_count", 0444, core->dentry,
2953 core, &clock_enable_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002954 if (!d)
2955 goto err_out;
2956
Taniya Das2dd25722016-11-14 11:26:02 +05302957 d = debugfs_create_u32("clk_notifier_count", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002958 (u32 *)&core->notifier_count);
2959 if (!d)
2960 goto err_out;
2961
Taniya Das2dd25722016-11-14 11:26:02 +05302962 d = debugfs_create_file("clk_parent", 0444, core->dentry, core,
2963 &clock_parent_fops);
2964 if (!d)
2965 goto err_out;
2966
2967 d = debugfs_create_file("clk_print_regs", 0444, core->dentry,
2968 core, &clock_print_hw_fops);
2969 if (!d)
2970 goto err_out;
2971
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002972 if (core->ops->debug_init) {
2973 ret = core->ops->debug_init(core->hw, core->dentry);
2974 if (ret)
2975 goto err_out;
2976 }
2977
2978 ret = 0;
2979 goto out;
2980
2981err_out:
2982 debugfs_remove_recursive(core->dentry);
2983 core->dentry = NULL;
2984out:
2985 return ret;
2986}
2987
2988/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002989 * clk_debug_register - add a clk node to the debugfs clk directory
2990 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002991 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002992 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2993 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002994 * will be created lazily by clk_debug_init as part of a late_initcall.
2995 */
2996static int clk_debug_register(struct clk_core *core)
2997{
2998 int ret = 0;
2999
3000 mutex_lock(&clk_debug_lock);
3001 hlist_add_head(&core->debug_node, &clk_debug_list);
3002
3003 if (!inited)
3004 goto unlock;
3005
3006 ret = clk_debug_create_one(core, rootdir);
3007unlock:
3008 mutex_unlock(&clk_debug_lock);
3009
3010 return ret;
3011}
3012
3013 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003014 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3015 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003016 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003017 * Dynamically removes a clk and all its child nodes from the
3018 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08003019 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003020 */
3021static void clk_debug_unregister(struct clk_core *core)
3022{
3023 mutex_lock(&clk_debug_lock);
3024 hlist_del_init(&core->debug_node);
3025 debugfs_remove_recursive(core->dentry);
3026 core->dentry = NULL;
3027 mutex_unlock(&clk_debug_lock);
3028}
3029
3030struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
3031 void *data, const struct file_operations *fops)
3032{
3033 struct dentry *d = NULL;
3034
3035 if (hw->core->dentry)
3036 d = debugfs_create_file(name, mode, hw->core->dentry, data,
3037 fops);
3038
3039 return d;
3040}
3041EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
3042
Taniya Das6c15e542016-11-07 10:08:30 +05303043/*
3044 * Print the names of all enabled clocks and their parents if
Shefali Jain5c15a992017-09-20 16:01:01 +05303045 * debug_suspend is set from debugfs along with print_parent flag set to 1.
3046 * Otherwise if print_parent set to 0, print only enabled clocks
3047 *
Taniya Das6c15e542016-11-07 10:08:30 +05303048 */
Shefali Jain5c15a992017-09-20 16:01:01 +05303049void clock_debug_print_enabled(bool print_parent)
Taniya Das6c15e542016-11-07 10:08:30 +05303050{
3051 if (likely(!debug_suspend))
3052 return;
3053
Shefali Jain5c15a992017-09-20 16:01:01 +05303054 if (print_parent)
3055 clock_debug_print_enabled_clocks(NULL);
3056 else
3057 clock_debug_print_enabled_debug_suspend(NULL);
Taniya Das6c15e542016-11-07 10:08:30 +05303058}
3059EXPORT_SYMBOL_GPL(clock_debug_print_enabled);
3060
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003061/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003062 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003063 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003064 * clks are often initialized very early during boot before memory can be
3065 * dynamically allocated and well before debugfs is setup. This function
3066 * populates the debugfs clk directory once at boot-time when we know that
3067 * debugfs is setup. It should only be called once at boot-time, all other clks
3068 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003069 */
3070static int __init clk_debug_init(void)
3071{
3072 struct clk_core *core;
3073 struct dentry *d;
3074
3075 rootdir = debugfs_create_dir("clk", NULL);
3076
3077 if (!rootdir)
3078 return -ENOMEM;
3079
Taniya Das2dd25722016-11-14 11:26:02 +05303080 d = debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003081 &clk_summary_fops);
3082 if (!d)
3083 return -ENOMEM;
3084
Taniya Das2dd25722016-11-14 11:26:02 +05303085 d = debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003086 &clk_dump_fops);
3087 if (!d)
3088 return -ENOMEM;
3089
Taniya Das2dd25722016-11-14 11:26:02 +05303090 d = debugfs_create_file("clk_orphan_summary", 0444, rootdir,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003091 &orphan_list, &clk_summary_fops);
3092 if (!d)
3093 return -ENOMEM;
3094
Taniya Das2dd25722016-11-14 11:26:02 +05303095 d = debugfs_create_file("clk_orphan_dump", 0444, rootdir,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003096 &orphan_list, &clk_dump_fops);
3097 if (!d)
3098 return -ENOMEM;
3099
Taniya Das2dd25722016-11-14 11:26:02 +05303100 d = debugfs_create_file("clk_enabled_list", 0444, rootdir,
3101 &clk_debug_list, &clk_enabled_list_fops);
3102 if (!d)
3103 return -ENOMEM;
3104
Taniya Das6c15e542016-11-07 10:08:30 +05303105
3106 d = debugfs_create_u32("debug_suspend", 0644, rootdir, &debug_suspend);
3107 if (!d)
3108 return -ENOMEM;
3109
Amit Nischal15aca8c2017-03-06 16:40:58 +05303110 d = debugfs_create_file("trace_clocks", 0444, rootdir, &all_lists,
3111 &clk_state_fops);
3112 if (!d)
3113 return -ENOMEM;
3114
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003115 mutex_lock(&clk_debug_lock);
3116 hlist_for_each_entry(core, &clk_debug_list, debug_node)
3117 clk_debug_create_one(core, rootdir);
3118
3119 inited = 1;
3120 mutex_unlock(&clk_debug_lock);
3121
3122 return 0;
3123}
3124late_initcall(clk_debug_init);
3125#else
3126static inline int clk_debug_register(struct clk_core *core) { return 0; }
3127static inline void clk_debug_reparent(struct clk_core *core,
3128 struct clk_core *new_parent)
3129{
3130}
3131static inline void clk_debug_unregister(struct clk_core *core)
3132{
3133}
3134#endif
3135
Michael Turquette3d3801e2015-02-25 09:11:01 -08003136/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003137 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003138 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07003139 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003140 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07003141 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07003142 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003143static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07003144{
Mike Turquetted1302a32012-03-29 14:30:40 -07003145 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003146 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003147 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003148 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003149
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003150 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07003151 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07003152
Mike Turquetteeab89f62013-03-28 13:59:01 -07003153 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003154
3155 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003156 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003157 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003158 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003159 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07003160 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07003161 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003162
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003163 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003164 if (core->ops->set_rate &&
3165 !((core->ops->round_rate || core->ops->determine_rate) &&
3166 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003167 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3168 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003169 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003170 goto out;
3171 }
3172
Stephen Boydd6968fc2015-04-30 13:54:13 -07003173 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003174 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3175 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003176 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003177 goto out;
3178 }
3179
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09003180 if (core->num_parents > 1 && !core->ops->get_parent) {
3181 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3182 __func__, core->name);
3183 ret = -EINVAL;
3184 goto out;
3185 }
3186
Stephen Boydd6968fc2015-04-30 13:54:13 -07003187 if (core->ops->set_rate_and_parent &&
3188 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003189 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003190 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08003191 ret = -EINVAL;
3192 goto out;
3193 }
3194
Mike Turquetteb24764902012-03-15 23:11:19 -07003195 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003196 for (i = 0; i < core->num_parents; i++)
3197 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07003198 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003199 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07003200
Stephen Boydd6968fc2015-04-30 13:54:13 -07003201 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07003202
3203 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08003204 * Populate core->parent if parent has already been clk_core_init'd. If
3205 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003206 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07003207 * clk list.
3208 *
3209 * Every time a new clk is clk_init'd then we walk the list of orphan
3210 * clocks and re-parent any that are children of the clock currently
3211 * being clk_init'd.
3212 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02003213 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003214 hlist_add_head(&core->child_node,
3215 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003216 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003217 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003218 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003219 core->orphan = false;
3220 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003221 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003222 core->orphan = true;
3223 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003224
3225 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003226 * Set clk's accuracy. The preferred method is to use
3227 * .recalc_accuracy. For simple clocks and lazy developers the default
3228 * fallback is to use the parent's accuracy. If a clock doesn't have a
3229 * parent (or is orphaned) then accuracy is set to zero (perfect
3230 * clock).
3231 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003232 if (core->ops->recalc_accuracy)
3233 core->accuracy = core->ops->recalc_accuracy(core->hw,
3234 __clk_get_accuracy(core->parent));
3235 else if (core->parent)
3236 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003237 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003238 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003239
3240 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02003241 * Set clk's phase.
3242 * Since a phase is by definition relative to its parent, just
3243 * query the current clock phase, or just assume it's in phase.
3244 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003245 if (core->ops->get_phase)
3246 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02003247 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003248 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02003249
3250 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07003251 * Set clk's rate. The preferred method is to use .recalc_rate. For
3252 * simple clocks and lazy developers the default fallback is to use the
3253 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3254 * then rate is set to zero.
3255 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003256 if (core->ops->recalc_rate)
3257 rate = core->ops->recalc_rate(core->hw,
3258 clk_core_get_rate_nolock(core->parent));
3259 else if (core->parent)
3260 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003261 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003262 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003263 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003264
3265 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003266 * walk the list of orphan clocks and reparent any that newly finds a
3267 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07003268 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08003269 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003270 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01003271
Michael Turquette904e6ea2016-07-08 16:32:10 -07003272 /*
3273 * we could call __clk_set_parent, but that would result in a
3274 * redundant call to the .set_rate op, if it exists
3275 */
3276 if (parent) {
3277 __clk_set_parent_before(orphan, parent);
3278 __clk_set_parent_after(orphan, parent, NULL);
3279 __clk_recalc_accuracies(orphan);
3280 __clk_recalc_rates(orphan, 0);
3281 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003282 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003283
3284 /*
3285 * optional platform-specific magic
3286 *
3287 * The .init callback is not used by any of the basic clock types, but
3288 * exists for weird hardware that must perform initialization magic.
3289 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02003290 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07003291 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003292 if (core->ops->init)
3293 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07003294
Lee Jones32b9b102016-02-11 13:19:09 -08003295 if (core->flags & CLK_IS_CRITICAL) {
Maxime Ripardef56b792016-05-13 10:00:31 +02003296 unsigned long flags;
3297
Lee Jones32b9b102016-02-11 13:19:09 -08003298 clk_core_prepare(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02003299
3300 flags = clk_enable_lock();
Lee Jones32b9b102016-02-11 13:19:09 -08003301 clk_core_enable(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02003302 clk_enable_unlock(flags);
Lee Jones32b9b102016-02-11 13:19:09 -08003303 }
3304
Michael Turquettee9b8c592017-04-21 12:27:39 +05303305 /*
3306 * enable clocks with the CLK_ENABLE_HAND_OFF flag set
3307 *
3308 * This flag causes the framework to enable the clock at registration
3309 * time, which is sometimes necessary for clocks that would cause a
3310 * system crash when gated (e.g. cpu, memory, etc). The prepare_count
3311 * is migrated over to the first clk consumer to call clk_prepare().
3312 * Similarly the clk's enable_count is migrated to the first consumer
3313 * to call clk_enable().
3314 */
3315 if (core->flags & CLK_ENABLE_HAND_OFF) {
3316 unsigned long flags;
3317
Taniya Das30684a42017-04-21 13:33:20 +05303318 /*
3319 * Few clocks might have hardware gating which would be
3320 * required to be ON before prepare/enabling the clocks. So
3321 * check if the clock has been turned ON earlier and we should
3322 * prepare/enable those clocks.
3323 */
3324 if (clk_core_is_enabled(core)) {
3325 core->need_handoff_prepare = true;
3326 core->need_handoff_enable = true;
3327 ret = clk_core_prepare(core);
3328 if (ret)
3329 goto out;
3330 flags = clk_enable_lock();
3331 clk_core_enable(core);
3332 clk_enable_unlock(flags);
3333 }
Michael Turquettee9b8c592017-04-21 12:27:39 +05303334 }
3335
Stephen Boydd6968fc2015-04-30 13:54:13 -07003336 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07003337out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003338 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003339
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003340 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003341 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003342
Mike Turquetted1302a32012-03-29 14:30:40 -07003343 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07003344}
3345
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003346struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
3347 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003348{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003349 struct clk *clk;
3350
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003351 /* This is to allow this function to be chained to others */
Masahiro Yamadac1de1352015-11-20 14:38:49 +09003352 if (IS_ERR_OR_NULL(hw))
Masahiro Yamada8a231332016-07-19 16:28:47 +09003353 return ERR_CAST(hw);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003354
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003355 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3356 if (!clk)
3357 return ERR_PTR(-ENOMEM);
3358
3359 clk->core = hw->core;
3360 clk->dev_id = dev_id;
3361 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003362 clk->max_rate = ULONG_MAX;
3363
3364 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08003365 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003366 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003367
3368 return clk;
3369}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003370
Stephen Boyd73e0e492015-02-06 11:42:43 -08003371void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003372{
3373 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08003374 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003375 clk_prepare_unlock();
3376
3377 kfree(clk);
3378}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003379
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003380/**
3381 * clk_register - allocate a new clock, register it and return an opaque cookie
3382 * @dev: device that is registering this clock
3383 * @hw: link to hardware-specific clock data
3384 *
3385 * clk_register is the primary interface for populating the clock tree with new
3386 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05303387 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003388 * rest of the clock API. In the event of an error clk_register will return an
3389 * error code; drivers must test for an error code after calling clk_register.
3390 */
3391struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07003392{
Mike Turquetted1302a32012-03-29 14:30:40 -07003393 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003394 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003395
Stephen Boydd6968fc2015-04-30 13:54:13 -07003396 core = kzalloc(sizeof(*core), GFP_KERNEL);
3397 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003398 ret = -ENOMEM;
3399 goto fail_out;
3400 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003401
Stephen Boydd6968fc2015-04-30 13:54:13 -07003402 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3403 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003404 ret = -ENOMEM;
3405 goto fail_name;
3406 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07003407 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003408 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003409 core->owner = dev->driver->owner;
3410 core->hw = hw;
3411 core->flags = hw->init->flags;
3412 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003413 core->min_rate = 0;
3414 core->max_rate = ULONG_MAX;
Stephen Boyd5cb05a12016-05-16 11:05:16 +05303415 core->vdd_class = hw->init->vdd_class;
3416 core->rate_max = hw->init->rate_max;
3417 core->num_rate_max = hw->init->num_rate_max;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003418 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003419
Stephen Boyd5cb05a12016-05-16 11:05:16 +05303420 if (core->vdd_class) {
3421 ret = clk_vdd_class_init(core->vdd_class);
3422 if (ret) {
3423 pr_err("Failed to initialize vdd class\n");
3424 goto fail_parent_names;
3425 }
3426 }
3427
Mike Turquetted1302a32012-03-29 14:30:40 -07003428 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003429 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02003430 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003431
Stephen Boydd6968fc2015-04-30 13:54:13 -07003432 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003433 ret = -ENOMEM;
3434 goto fail_parent_names;
3435 }
3436
3437
3438 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003439 for (i = 0; i < core->num_parents; i++) {
3440 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003441 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003442 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003443 ret = -ENOMEM;
3444 goto fail_parent_names_copy;
3445 }
3446 }
3447
Masahiro Yamada176d1162015-12-28 19:23:00 +09003448 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3449 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3450 GFP_KERNEL);
3451 if (!core->parents) {
3452 ret = -ENOMEM;
3453 goto fail_parents;
3454 };
3455
Stephen Boydd6968fc2015-04-30 13:54:13 -07003456 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003457
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003458 hw->clk = __clk_create_clk(hw, NULL, NULL);
3459 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003460 ret = PTR_ERR(hw->clk);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003461 goto fail_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003462 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003463
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003464 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003465 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003466 return hw->clk;
3467
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003468 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003469 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003470
Masahiro Yamada176d1162015-12-28 19:23:00 +09003471fail_parents:
3472 kfree(core->parents);
Mike Turquetted1302a32012-03-29 14:30:40 -07003473fail_parent_names_copy:
3474 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003475 kfree_const(core->parent_names[i]);
3476 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07003477fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003478 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003479fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003480 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003481fail_out:
3482 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003483}
3484EXPORT_SYMBOL_GPL(clk_register);
3485
Stephen Boyd41438042016-02-05 17:02:52 -08003486/**
3487 * clk_hw_register - register a clk_hw and return an error code
3488 * @dev: device that is registering this clock
3489 * @hw: link to hardware-specific clock data
3490 *
3491 * clk_hw_register is the primary interface for populating the clock tree with
3492 * new clock nodes. It returns an integer equal to zero indicating success or
3493 * less than zero indicating failure. Drivers must test for an error code after
3494 * calling clk_hw_register().
3495 */
3496int clk_hw_register(struct device *dev, struct clk_hw *hw)
3497{
3498 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3499}
3500EXPORT_SYMBOL_GPL(clk_hw_register);
3501
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003502/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003503static void __clk_release(struct kref *ref)
3504{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003505 struct clk_core *core = container_of(ref, struct clk_core, ref);
3506 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003507
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003508 lockdep_assert_held(&prepare_lock);
3509
Stephen Boydd6968fc2015-04-30 13:54:13 -07003510 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003511 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003512 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003513
Stephen Boydd6968fc2015-04-30 13:54:13 -07003514 kfree(core->parent_names);
3515 kfree_const(core->name);
3516 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003517}
3518
3519/*
3520 * Empty clk_ops for unregistered clocks. These are used temporarily
3521 * after clk_unregister() was called on a clock and until last clock
3522 * consumer calls clk_put() and the struct clk object is freed.
3523 */
3524static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3525{
3526 return -ENXIO;
3527}
3528
3529static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3530{
3531 WARN_ON_ONCE(1);
3532}
3533
3534static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3535 unsigned long parent_rate)
3536{
3537 return -ENXIO;
3538}
3539
3540static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3541{
3542 return -ENXIO;
3543}
3544
3545static const struct clk_ops clk_nodrv_ops = {
3546 .enable = clk_nodrv_prepare_enable,
3547 .disable = clk_nodrv_disable_unprepare,
3548 .prepare = clk_nodrv_prepare_enable,
3549 .unprepare = clk_nodrv_disable_unprepare,
3550 .set_rate = clk_nodrv_set_rate,
3551 .set_parent = clk_nodrv_set_parent,
3552};
3553
Mark Brown1df5c932012-04-18 09:07:12 +01003554/**
3555 * clk_unregister - unregister a currently registered clock
3556 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003557 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003558void clk_unregister(struct clk *clk)
3559{
3560 unsigned long flags;
3561
Stephen Boyd6314b672014-09-04 23:37:49 -07003562 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3563 return;
3564
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003565 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003566
3567 clk_prepare_lock();
3568
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003569 if (clk->core->ops == &clk_nodrv_ops) {
3570 pr_err("%s: unregistered clock: %s\n", __func__,
3571 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003572 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003573 }
3574 /*
3575 * Assign empty clock ops for consumers that might still hold
3576 * a reference to this clock.
3577 */
3578 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003579 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003580 clk_enable_unlock(flags);
3581
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003582 if (!hlist_empty(&clk->core->children)) {
3583 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003584 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003585
3586 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003587 hlist_for_each_entry_safe(child, t, &clk->core->children,
3588 child_node)
3589 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003590 }
3591
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003592 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003593
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003594 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003595 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003596 __func__, clk->core->name);
3597 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003598unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003599 clk_prepare_unlock();
3600}
Mark Brown1df5c932012-04-18 09:07:12 +01003601EXPORT_SYMBOL_GPL(clk_unregister);
3602
Stephen Boyd41438042016-02-05 17:02:52 -08003603/**
3604 * clk_hw_unregister - unregister a currently registered clk_hw
3605 * @hw: hardware-specific clock data to unregister
3606 */
3607void clk_hw_unregister(struct clk_hw *hw)
3608{
3609 clk_unregister(hw->clk);
3610}
3611EXPORT_SYMBOL_GPL(clk_hw_unregister);
3612
Stephen Boyd46c87732012-09-24 13:38:04 -07003613static void devm_clk_release(struct device *dev, void *res)
3614{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003615 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003616}
3617
Stephen Boyd41438042016-02-05 17:02:52 -08003618static void devm_clk_hw_release(struct device *dev, void *res)
3619{
3620 clk_hw_unregister(*(struct clk_hw **)res);
3621}
3622
Deepak Katragadda677bad12016-11-04 13:33:13 -07003623#define MAX_LEN_OPP_HANDLE 50
3624#define LEN_OPP_HANDLE 16
3625
3626static int derive_device_list(struct device **device_list,
3627 struct clk_core *core,
3628 struct device_node *np,
3629 char *clk_handle_name, int count)
3630{
3631 int j;
3632 struct platform_device *pdev;
3633 struct device_node *dev_node;
3634
3635 for (j = 0; j < count; j++) {
3636 device_list[j] = NULL;
3637 dev_node = of_parse_phandle(np, clk_handle_name, j);
3638 if (!dev_node) {
3639 pr_err("Unable to get device_node pointer for %s opp-handle (%s)\n",
3640 core->name, clk_handle_name);
3641 return -ENODEV;
3642 }
3643
3644 pdev = of_find_device_by_node(dev_node);
3645 if (!pdev) {
3646 pr_err("Unable to find platform_device node for %s opp-handle\n",
3647 core->name);
3648 return -ENODEV;
3649 }
3650 device_list[j] = &pdev->dev;
3651 }
3652 return 0;
3653}
3654
3655static int clk_get_voltage(struct clk_core *core, unsigned long rate, int n)
3656{
3657 struct clk_vdd_class *vdd;
3658 int level, corner;
3659
3660 /* Use the first regulator in the vdd class for the OPP table. */
3661 vdd = core->vdd_class;
3662 if (vdd->num_regulators > 1) {
3663 corner = vdd->vdd_uv[vdd->num_regulators * n];
3664 } else {
3665 level = clk_find_vdd_level(core, rate);
3666 if (level < 0) {
3667 pr_err("Could not find vdd level\n");
3668 return -EINVAL;
3669 }
3670 corner = vdd->vdd_uv[level];
3671 }
3672
3673 if (!corner) {
3674 pr_err("%s: Unable to find vdd level for rate %lu\n",
3675 core->name, rate);
3676 return -EINVAL;
3677 }
3678
3679 return corner;
3680}
3681
3682static int clk_add_and_print_opp(struct clk_hw *hw,
3683 struct device **device_list, int count,
3684 unsigned long rate, int uv, int n)
3685{
3686 struct clk_core *core = hw->core;
3687 int j, ret = 0;
3688
3689 for (j = 0; j < count; j++) {
3690 ret = dev_pm_opp_add(device_list[j], rate, uv);
3691 if (ret) {
3692 pr_err("%s: couldn't add OPP for %lu - err: %d\n",
3693 core->name, rate, ret);
3694 return ret;
3695 }
3696
3697 if (n == 0 || n == core->num_rate_max - 1 ||
3698 rate == clk_hw_round_rate(hw, INT_MAX))
3699 pr_info("%s: set OPP pair(%lu Hz: %u uV) on %s\n",
3700 core->name, rate, uv,
3701 dev_name(device_list[j]));
3702 }
3703 return ret;
3704}
3705
3706static void clk_populate_clock_opp_table(struct device_node *np,
3707 struct clk_hw *hw)
3708{
3709 struct device **device_list;
3710 struct clk_core *core = hw->core;
3711 char clk_handle_name[MAX_LEN_OPP_HANDLE];
3712 int n, len, count, uv;
3713 unsigned long rate = 0, ret = 0;
3714
3715 if (!core || !core->num_rate_max)
3716 return;
3717
3718 if (strlen(core->name) + LEN_OPP_HANDLE < MAX_LEN_OPP_HANDLE) {
3719 ret = snprintf(clk_handle_name, ARRAY_SIZE(clk_handle_name),
3720 "qcom,%s-opp-handle", core->name);
3721 if (ret < strlen(core->name) + LEN_OPP_HANDLE) {
3722 pr_err("%s: Failed to hold clk_handle_name\n",
3723 core->name);
3724 return;
3725 }
3726 } else {
3727 pr_err("clk name (%s) too large to fit in clk_handle_name\n",
3728 core->name);
3729 return;
3730 }
3731
3732 if (of_find_property(np, clk_handle_name, &len)) {
3733 count = len/sizeof(u32);
3734
3735 device_list = kmalloc_array(count, sizeof(struct device *),
3736 GFP_KERNEL);
3737 if (!device_list)
3738 return;
3739
3740 ret = derive_device_list(device_list, core, np,
3741 clk_handle_name, count);
3742 if (ret < 0) {
3743 pr_err("Failed to fill device_list for %s\n",
3744 clk_handle_name);
3745 goto err_derive_device_list;
3746 }
3747 } else {
3748 pr_debug("Unable to find %s\n", clk_handle_name);
3749 return;
3750 }
3751
3752 for (n = 0; ; n++) {
3753 ret = clk_hw_round_rate(hw, rate + 1);
3754 if (ret < 0) {
3755 pr_err("clk_round_rate failed for %s\n",
3756 core->name);
3757 goto err_derive_device_list;
3758 }
3759
3760 /*
3761 * If clk_hw_round_rate gives the same value on consecutive
3762 * iterations, exit the loop since we're at the maximum clock
3763 * frequency.
3764 */
3765 if (rate == ret)
3766 break;
3767 rate = ret;
3768
3769 uv = clk_get_voltage(core, rate, n);
3770 if (uv < 0)
3771 goto err_derive_device_list;
3772
3773 ret = clk_add_and_print_opp(hw, device_list, count,
3774 rate, uv, n);
3775 if (ret)
3776 goto err_derive_device_list;
3777 }
3778
3779err_derive_device_list:
3780 kfree(device_list);
3781}
3782
Stephen Boyd46c87732012-09-24 13:38:04 -07003783/**
3784 * devm_clk_register - resource managed clk_register()
3785 * @dev: device that is registering this clock
3786 * @hw: link to hardware-specific clock data
3787 *
3788 * Managed clk_register(). Clocks returned from this function are
3789 * automatically clk_unregister()ed on driver detach. See clk_register() for
3790 * more information.
3791 */
3792struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3793{
3794 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003795 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003796
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003797 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3798 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003799 return ERR_PTR(-ENOMEM);
3800
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003801 clk = clk_register(dev, hw);
3802 if (!IS_ERR(clk)) {
3803 *clkp = clk;
3804 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003805 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003806 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003807 }
3808
Deepak Katragadda677bad12016-11-04 13:33:13 -07003809 clk_populate_clock_opp_table(dev->of_node, hw);
Stephen Boyd46c87732012-09-24 13:38:04 -07003810 return clk;
3811}
3812EXPORT_SYMBOL_GPL(devm_clk_register);
3813
Stephen Boyd41438042016-02-05 17:02:52 -08003814/**
3815 * devm_clk_hw_register - resource managed clk_hw_register()
3816 * @dev: device that is registering this clock
3817 * @hw: link to hardware-specific clock data
3818 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003819 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003820 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3821 * for more information.
3822 */
3823int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3824{
3825 struct clk_hw **hwp;
3826 int ret;
3827
3828 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3829 if (!hwp)
3830 return -ENOMEM;
3831
3832 ret = clk_hw_register(dev, hw);
3833 if (!ret) {
3834 *hwp = hw;
3835 devres_add(dev, hwp);
3836 } else {
3837 devres_free(hwp);
3838 }
3839
Deepak Katragadda677bad12016-11-04 13:33:13 -07003840 clk_populate_clock_opp_table(dev->of_node, hw);
Stephen Boyd41438042016-02-05 17:02:52 -08003841 return ret;
3842}
3843EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3844
Stephen Boyd46c87732012-09-24 13:38:04 -07003845static int devm_clk_match(struct device *dev, void *res, void *data)
3846{
3847 struct clk *c = res;
3848 if (WARN_ON(!c))
3849 return 0;
3850 return c == data;
3851}
3852
Stephen Boyd41438042016-02-05 17:02:52 -08003853static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3854{
3855 struct clk_hw *hw = res;
3856
3857 if (WARN_ON(!hw))
3858 return 0;
3859 return hw == data;
3860}
3861
Stephen Boyd46c87732012-09-24 13:38:04 -07003862/**
3863 * devm_clk_unregister - resource managed clk_unregister()
3864 * @clk: clock to unregister
3865 *
3866 * Deallocate a clock allocated with devm_clk_register(). Normally
3867 * this function will not need to be called and the resource management
3868 * code will ensure that the resource is freed.
3869 */
3870void devm_clk_unregister(struct device *dev, struct clk *clk)
3871{
3872 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3873}
3874EXPORT_SYMBOL_GPL(devm_clk_unregister);
3875
Stephen Boyd41438042016-02-05 17:02:52 -08003876/**
3877 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3878 * @dev: device that is unregistering the hardware-specific clock data
3879 * @hw: link to hardware-specific clock data
3880 *
3881 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3882 * this function will not need to be called and the resource management
3883 * code will ensure that the resource is freed.
3884 */
3885void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3886{
3887 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3888 hw));
3889}
3890EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3891
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003892/*
3893 * clkdev helpers
3894 */
3895int __clk_get(struct clk *clk)
3896{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003897 struct clk_core *core = !clk ? NULL : clk->core;
3898
3899 if (core) {
3900 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003901 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003902
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003903 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003904 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003905 return 1;
3906}
3907
3908void __clk_put(struct clk *clk)
3909{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003910 struct module *owner;
3911
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003912 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003913 return;
3914
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003915 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003916
Stephen Boyd50595f82015-02-06 11:42:44 -08003917 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003918 if (clk->min_rate > clk->core->req_rate ||
3919 clk->max_rate < clk->core->req_rate)
3920 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3921
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003922 owner = clk->core->owner;
3923 kref_put(&clk->core->ref, __clk_release);
3924
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003925 clk_prepare_unlock();
3926
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003927 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003928
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003929 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003930}
3931
Mike Turquetteb24764902012-03-15 23:11:19 -07003932/*** clk rate change notifiers ***/
3933
3934/**
3935 * clk_notifier_register - add a clk rate change notifier
3936 * @clk: struct clk * to watch
3937 * @nb: struct notifier_block * with callback info
3938 *
3939 * Request notification when clk's rate changes. This uses an SRCU
3940 * notifier because we want it to block and notifier unregistrations are
3941 * uncommon. The callbacks associated with the notifier must not
3942 * re-enter into the clk framework by calling any top-level clk APIs;
3943 * this will cause a nested prepare_lock mutex.
3944 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003945 * In all notification cases (pre, post and abort rate change) the original
3946 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3947 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003948 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003949 * clk_notifier_register() must be called from non-atomic context.
3950 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3951 * allocation failure; otherwise, passes along the return value of
3952 * srcu_notifier_chain_register().
3953 */
3954int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3955{
3956 struct clk_notifier *cn;
3957 int ret = -ENOMEM;
3958
3959 if (!clk || !nb)
3960 return -EINVAL;
3961
Mike Turquetteeab89f62013-03-28 13:59:01 -07003962 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003963
3964 /* search the list of notifiers for this clk */
3965 list_for_each_entry(cn, &clk_notifier_list, node)
3966 if (cn->clk == clk)
3967 break;
3968
3969 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3970 if (cn->clk != clk) {
3971 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
3972 if (!cn)
3973 goto out;
3974
3975 cn->clk = clk;
3976 srcu_init_notifier_head(&cn->notifier_head);
3977
3978 list_add(&cn->node, &clk_notifier_list);
3979 }
3980
3981 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3982
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003983 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003984
3985out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003986 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003987
3988 return ret;
3989}
3990EXPORT_SYMBOL_GPL(clk_notifier_register);
3991
3992/**
3993 * clk_notifier_unregister - remove a clk rate change notifier
3994 * @clk: struct clk *
3995 * @nb: struct notifier_block * with callback info
3996 *
3997 * Request no further notification for changes to 'clk' and frees memory
3998 * allocated in clk_notifier_register.
3999 *
4000 * Returns -EINVAL if called with null arguments; otherwise, passes
4001 * along the return value of srcu_notifier_chain_unregister().
4002 */
4003int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
4004{
4005 struct clk_notifier *cn = NULL;
4006 int ret = -EINVAL;
4007
4008 if (!clk || !nb)
4009 return -EINVAL;
4010
Mike Turquetteeab89f62013-03-28 13:59:01 -07004011 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004012
4013 list_for_each_entry(cn, &clk_notifier_list, node)
4014 if (cn->clk == clk)
4015 break;
4016
4017 if (cn->clk == clk) {
4018 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
4019
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01004020 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07004021
4022 /* XXX the notifier code should handle this better */
4023 if (!cn->notifier_head.head) {
4024 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08004025 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07004026 kfree(cn);
4027 }
4028
4029 } else {
4030 ret = -ENOENT;
4031 }
4032
Mike Turquetteeab89f62013-03-28 13:59:01 -07004033 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07004034
4035 return ret;
4036}
4037EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05004038
Shefali Jain0dc6e782017-11-27 13:06:27 +05304039#endif /* CONFIG_COMMON_CLK */
4040
Grant Likely766e6a42012-04-09 14:50:06 -05004041#ifdef CONFIG_OF
4042/**
4043 * struct of_clk_provider - Clock provider registration structure
4044 * @link: Entry in global list of clock providers
4045 * @node: Pointer to device tree node of clock provider
4046 * @get: Get clock callback. Returns NULL or a struct clk for the
4047 * given clock specifier
4048 * @data: context pointer to be passed into @get callback
4049 */
4050struct of_clk_provider {
4051 struct list_head link;
4052
4053 struct device_node *node;
4054 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004055 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05004056 void *data;
4057};
4058
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304059static const struct of_device_id __clk_of_table_sentinel
4060 __used __section(__clk_of_table_end);
4061
Grant Likely766e6a42012-04-09 14:50:06 -05004062static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004063static DEFINE_MUTEX(of_clk_mutex);
4064
Grant Likely766e6a42012-04-09 14:50:06 -05004065struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
4066 void *data)
4067{
4068 return data;
4069}
4070EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
4071
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004072struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
4073{
4074 return data;
4075}
4076EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
4077
Shefali Jain0dc6e782017-11-27 13:06:27 +05304078#if defined(CONFIG_COMMON_CLK)
4079
Shawn Guo494bfec2012-08-22 21:36:27 +08004080struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
4081{
4082 struct clk_onecell_data *clk_data = data;
4083 unsigned int idx = clkspec->args[0];
4084
4085 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02004086 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08004087 return ERR_PTR(-EINVAL);
4088 }
4089
4090 return clk_data->clks[idx];
4091}
4092EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
4093
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004094struct clk_hw *
4095of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
4096{
4097 struct clk_hw_onecell_data *hw_data = data;
4098 unsigned int idx = clkspec->args[0];
4099
4100 if (idx >= hw_data->num) {
4101 pr_err("%s: invalid index %u\n", __func__, idx);
4102 return ERR_PTR(-EINVAL);
4103 }
4104
4105 return hw_data->hws[idx];
4106}
4107EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
4108
Shefali Jain0dc6e782017-11-27 13:06:27 +05304109#endif /* CONFIG_COMMON_CLK */
4110
4111/**
4112 * of_clk_del_provider() - Remove a previously registered clock provider
4113 * @np: Device node pointer associated with clock provider
4114 */
4115void of_clk_del_provider(struct device_node *np)
4116{
4117 struct of_clk_provider *cp;
4118
4119 mutex_lock(&of_clk_mutex);
4120 list_for_each_entry(cp, &of_clk_providers, link) {
4121 if (cp->node == np) {
4122 list_del(&cp->link);
4123 of_node_put(cp->node);
4124 kfree(cp);
4125 break;
4126 }
4127 }
4128 mutex_unlock(&of_clk_mutex);
4129}
4130EXPORT_SYMBOL_GPL(of_clk_del_provider);
4131
Grant Likely766e6a42012-04-09 14:50:06 -05004132/**
4133 * of_clk_add_provider() - Register a clock provider for a node
4134 * @np: Device node pointer associated with clock provider
4135 * @clk_src_get: callback for decoding clock
4136 * @data: context pointer for @clk_src_get callback.
4137 */
4138int of_clk_add_provider(struct device_node *np,
4139 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
4140 void *data),
4141 void *data)
4142{
4143 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004144 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004145
4146 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
4147 if (!cp)
4148 return -ENOMEM;
4149
4150 cp->node = of_node_get(np);
4151 cp->data = data;
4152 cp->get = clk_src_get;
4153
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004154 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004155 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004156 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004157 pr_debug("Added clock from %s\n", np->full_name);
4158
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004159 ret = of_clk_set_defaults(np, true);
4160 if (ret < 0)
4161 of_clk_del_provider(np);
4162
4163 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004164}
4165EXPORT_SYMBOL_GPL(of_clk_add_provider);
4166
4167/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004168 * of_clk_add_hw_provider() - Register a clock provider for a node
4169 * @np: Device node pointer associated with clock provider
4170 * @get: callback for decoding clk_hw
4171 * @data: context pointer for @get callback.
4172 */
4173int of_clk_add_hw_provider(struct device_node *np,
4174 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4175 void *data),
4176 void *data)
4177{
4178 struct of_clk_provider *cp;
4179 int ret;
4180
4181 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4182 if (!cp)
4183 return -ENOMEM;
4184
4185 cp->node = of_node_get(np);
4186 cp->data = data;
4187 cp->get_hw = get;
4188
4189 mutex_lock(&of_clk_mutex);
4190 list_add(&cp->link, &of_clk_providers);
4191 mutex_unlock(&of_clk_mutex);
4192 pr_debug("Added clk_hw provider from %s\n", np->full_name);
4193
4194 ret = of_clk_set_defaults(np, true);
4195 if (ret < 0)
4196 of_clk_del_provider(np);
4197
4198 return ret;
4199}
4200EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
4201
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004202static struct clk_hw *
4203__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4204 struct of_phandle_args *clkspec)
4205{
4206 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004207
Stephen Boyd74002fc2016-08-25 13:35:36 -07004208 if (provider->get_hw)
4209 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004210
Stephen Boyd74002fc2016-08-25 13:35:36 -07004211 clk = provider->get(clkspec, provider->data);
4212 if (IS_ERR(clk))
4213 return ERR_CAST(clk);
4214 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004215}
4216
Stephen Boyd73e0e492015-02-06 11:42:43 -08004217struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
4218 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05004219{
4220 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01004221 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Stephen Boydf155d152016-08-15 14:32:23 -07004222 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -05004223
Stephen Boyd306c3422015-02-05 15:39:11 -08004224 if (!clkspec)
4225 return ERR_PTR(-EINVAL);
4226
Grant Likely766e6a42012-04-09 14:50:06 -05004227 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08004228 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004229 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07004230 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004231 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004232 clk = __clk_create_clk(hw, dev_id, con_id);
Stephen Boydf155d152016-08-15 14:32:23 -07004233 }
Stephen Boyd73e0e492015-02-06 11:42:43 -08004234
Stephen Boydf155d152016-08-15 14:32:23 -07004235 if (!IS_ERR(clk)) {
4236 if (!__clk_get(clk)) {
Stephen Boyd73e0e492015-02-06 11:42:43 -08004237 __clk_free_clk(clk);
4238 clk = ERR_PTR(-ENOENT);
4239 }
4240
Grant Likely766e6a42012-04-09 14:50:06 -05004241 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08004242 }
Grant Likely766e6a42012-04-09 14:50:06 -05004243 }
Stephen Boyd306c3422015-02-05 15:39:11 -08004244 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004245
4246 return clk;
4247}
4248
Stephen Boyd306c3422015-02-05 15:39:11 -08004249/**
4250 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4251 * @clkspec: pointer to a clock specifier data structure
4252 *
4253 * This function looks up a struct clk from the registered list of clock
4254 * providers, an input is a clock specifier data structure as returned
4255 * from the of_parse_phandle_with_args() function call.
4256 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004257struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4258{
Stephen Boyd306c3422015-02-05 15:39:11 -08004259 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05004260}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06004261EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05004262
Stephen Boyd929e7f32016-02-19 15:52:32 -08004263/**
4264 * of_clk_get_parent_count() - Count the number of clocks a device node has
4265 * @np: device node to count
4266 *
4267 * Returns: The number of clocks that are possible parents of this node
4268 */
4269unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07004270{
Stephen Boyd929e7f32016-02-19 15:52:32 -08004271 int count;
4272
4273 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4274 if (count < 0)
4275 return 0;
4276
4277 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07004278}
4279EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4280
Grant Likely766e6a42012-04-09 14:50:06 -05004281const char *of_clk_get_parent_name(struct device_node *np, int index)
4282{
4283 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004284 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05004285 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004286 const __be32 *vp;
4287 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05004288 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004289 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004290 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05004291
Grant Likely766e6a42012-04-09 14:50:06 -05004292 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4293 &clkspec);
4294 if (rc)
4295 return NULL;
4296
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004297 index = clkspec.args_count ? clkspec.args[0] : 0;
4298 count = 0;
4299
4300 /* if there is an indices property, use it to transfer the index
4301 * specified into an array offset for the clock-output-names property.
4302 */
4303 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4304 if (index == pv) {
4305 index = count;
4306 break;
4307 }
4308 count++;
4309 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09004310 /* We went off the end of 'clock-indices' without finding it */
4311 if (prop && !vp)
4312 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004313
Grant Likely766e6a42012-04-09 14:50:06 -05004314 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004315 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004316 &clk_name) < 0) {
4317 /*
4318 * Best effort to get the name if the clock has been
4319 * registered with the framework. If the clock isn't
4320 * registered, we return the node name as the name of
4321 * the clock as long as #clock-cells = 0.
4322 */
4323 clk = of_clk_get_from_provider(&clkspec);
4324 if (IS_ERR(clk)) {
4325 if (clkspec.args_count == 0)
4326 clk_name = clkspec.np->name;
4327 else
4328 clk_name = NULL;
4329 } else {
Shefali Jain0dc6e782017-11-27 13:06:27 +05304330#if defined(CONFIG_COMMON_CLK)
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004331 clk_name = __clk_get_name(clk);
4332 clk_put(clk);
Shefali Jain0dc6e782017-11-27 13:06:27 +05304333#endif
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004334 }
4335 }
4336
Grant Likely766e6a42012-04-09 14:50:06 -05004337
4338 of_node_put(clkspec.np);
4339 return clk_name;
4340}
4341EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4342
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05004343/**
4344 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4345 * number of parents
4346 * @np: Device node pointer associated with clock provider
4347 * @parents: pointer to char array that hold the parents' names
4348 * @size: size of the @parents array
4349 *
4350 * Return: number of parents for the clock node.
4351 */
4352int of_clk_parent_fill(struct device_node *np, const char **parents,
4353 unsigned int size)
4354{
4355 unsigned int i = 0;
4356
4357 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4358 i++;
4359
4360 return i;
4361}
4362EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4363
Shefali Jain0dc6e782017-11-27 13:06:27 +05304364#if defined(CONFIG_COMMON_CLK)
4365
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004366struct clock_provider {
4367 of_clk_init_cb_t clk_init_cb;
4368 struct device_node *np;
4369 struct list_head node;
4370};
4371
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004372/*
4373 * This function looks for a parent clock. If there is one, then it
4374 * checks that the provider for this parent clock was initialized, in
4375 * this case the parent clock will be ready.
4376 */
4377static int parent_ready(struct device_node *np)
4378{
4379 int i = 0;
4380
4381 while (true) {
4382 struct clk *clk = of_clk_get(np, i);
4383
4384 /* this parent is ready we can check the next one */
4385 if (!IS_ERR(clk)) {
4386 clk_put(clk);
4387 i++;
4388 continue;
4389 }
4390
4391 /* at least one parent is not ready, we exit now */
4392 if (PTR_ERR(clk) == -EPROBE_DEFER)
4393 return 0;
4394
4395 /*
4396 * Here we make assumption that the device tree is
4397 * written correctly. So an error means that there is
4398 * no more parent. As we didn't exit yet, then the
4399 * previous parent are ready. If there is no clock
4400 * parent, no need to wait for them, then we can
4401 * consider their absence as being ready
4402 */
4403 return 1;
4404 }
4405}
4406
Grant Likely766e6a42012-04-09 14:50:06 -05004407/**
Lee Jonesd56f8992016-02-11 13:19:11 -08004408 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4409 * @np: Device node pointer associated with clock provider
4410 * @index: clock index
4411 * @flags: pointer to clk_core->flags
4412 *
4413 * Detects if the clock-critical property exists and, if so, sets the
4414 * corresponding CLK_IS_CRITICAL flag.
4415 *
4416 * Do not use this function. It exists only for legacy Device Tree
4417 * bindings, such as the one-clock-per-node style that are outdated.
4418 * Those bindings typically put all clock data into .dts and the Linux
4419 * driver has no clock data, thus making it impossible to set this flag
4420 * correctly from the driver. Only those drivers may call
4421 * of_clk_detect_critical from their setup functions.
4422 *
4423 * Return: error code or zero on success
4424 */
4425int of_clk_detect_critical(struct device_node *np,
4426 int index, unsigned long *flags)
4427{
4428 struct property *prop;
4429 const __be32 *cur;
4430 uint32_t idx;
4431
4432 if (!np || !flags)
4433 return -EINVAL;
4434
4435 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4436 if (index == idx)
4437 *flags |= CLK_IS_CRITICAL;
4438
4439 return 0;
4440}
4441
4442/**
Grant Likely766e6a42012-04-09 14:50:06 -05004443 * of_clk_init() - Scan and init clock providers from the DT
4444 * @matches: array of compatible values and init functions for providers.
4445 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004446 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01004447 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004448 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05004449 */
4450void __init of_clk_init(const struct of_device_id *matches)
4451{
Alex Elder7f7ed582013-08-22 11:31:31 -05004452 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05004453 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004454 struct clock_provider *clk_provider, *next;
4455 bool is_init_done;
4456 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07004457 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05004458
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304459 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03004460 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304461
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004462 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05004463 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004464 struct clock_provider *parent;
4465
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01004466 if (!of_device_is_available(np))
4467 continue;
4468
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004469 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4470 if (!parent) {
4471 list_for_each_entry_safe(clk_provider, next,
4472 &clk_provider_list, node) {
4473 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004474 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004475 kfree(clk_provider);
4476 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004477 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004478 return;
4479 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004480
4481 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004482 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01004483 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004484 }
4485
4486 while (!list_empty(&clk_provider_list)) {
4487 is_init_done = false;
4488 list_for_each_entry_safe(clk_provider, next,
4489 &clk_provider_list, node) {
4490 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004491
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02004492 /* Don't populate platform devices */
4493 of_node_set_flag(clk_provider->np,
4494 OF_POPULATED);
4495
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004496 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004497 of_clk_set_defaults(clk_provider->np, true);
4498
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004499 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004500 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004501 kfree(clk_provider);
4502 is_init_done = true;
4503 }
4504 }
4505
4506 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01004507 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004508 * remaining providers during the last loop, so now we
4509 * initialize all the remaining ones unconditionally
4510 * in case the clock parent was not mandatory
4511 */
4512 if (!is_init_done)
4513 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05004514 }
4515}
Shefali Jain0dc6e782017-11-27 13:06:27 +05304516
4517#endif /* CONFIG_COMMON_CLK */
4518
Grant Likely766e6a42012-04-09 14:50:06 -05004519#endif