blob: 7cdf45b85bb69b6297b85b900c4ed9b6ae79573a [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>
Taniya Das8436bd72016-11-21 17:50:13 +05304 * Copyright (c) 2016-2017, 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
Mike Turquetteb24764902012-03-15 23:11:19 -070035static DEFINE_SPINLOCK(enable_lock);
36static DEFINE_MUTEX(prepare_lock);
37
Mike Turquette533ddeb2013-03-28 13:59:02 -070038static struct task_struct *prepare_owner;
39static struct task_struct *enable_owner;
40
41static int prepare_refcnt;
42static int enable_refcnt;
43
Mike Turquetteb24764902012-03-15 23:11:19 -070044static HLIST_HEAD(clk_root_list);
45static HLIST_HEAD(clk_orphan_list);
46static LIST_HEAD(clk_notifier_list);
47
Stephen Boyd5cb05a12016-05-16 11:05:16 +053048struct clk_handoff_vdd {
49 struct list_head list;
50 struct clk_vdd_class *vdd_class;
51};
52
53static LIST_HEAD(clk_handoff_vdd_list);
54
Michael Turquetteb09d6d92015-01-29 14:22:50 -080055/*** private data structures ***/
56
57struct clk_core {
58 const char *name;
59 const struct clk_ops *ops;
60 struct clk_hw *hw;
61 struct module *owner;
62 struct clk_core *parent;
63 const char **parent_names;
64 struct clk_core **parents;
65 u8 num_parents;
66 u8 new_parent_index;
67 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010068 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080069 unsigned long new_rate;
70 struct clk_core *new_parent;
71 struct clk_core *new_child;
72 unsigned long flags;
Heiko Stuebnere6500342015-04-22 22:53:05 +020073 bool orphan;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080074 unsigned int enable_count;
75 unsigned int prepare_count;
Michael Turquettee9b8c592017-04-21 12:27:39 +053076 bool need_handoff_enable;
77 bool need_handoff_prepare;
Stephen Boyd9783c0d2015-07-16 12:50:27 -070078 unsigned long min_rate;
79 unsigned long max_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080080 unsigned long accuracy;
81 int phase;
82 struct hlist_head children;
83 struct hlist_node child_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010084 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080085 unsigned int notifier_count;
86#ifdef CONFIG_DEBUG_FS
87 struct dentry *dentry;
Maxime Coquelin8c9a8a82015-06-10 13:28:27 +020088 struct hlist_node debug_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080089#endif
90 struct kref ref;
Stephen Boyd5cb05a12016-05-16 11:05:16 +053091 struct clk_vdd_class *vdd_class;
92 unsigned long *rate_max;
93 int num_rate_max;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080094};
95
Stephen Boyddfc202e2015-02-02 14:37:41 -080096#define CREATE_TRACE_POINTS
97#include <trace/events/clk.h>
98
Michael Turquetteb09d6d92015-01-29 14:22:50 -080099struct clk {
100 struct clk_core *core;
101 const char *dev_id;
102 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100103 unsigned long min_rate;
104 unsigned long max_rate;
Stephen Boyd50595f82015-02-06 11:42:44 -0800105 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -0800106};
107
Mike Turquetteeab89f62013-03-28 13:59:01 -0700108/*** locking ***/
109static void clk_prepare_lock(void)
110{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700111 if (!mutex_trylock(&prepare_lock)) {
112 if (prepare_owner == current) {
113 prepare_refcnt++;
114 return;
115 }
116 mutex_lock(&prepare_lock);
117 }
118 WARN_ON_ONCE(prepare_owner != NULL);
119 WARN_ON_ONCE(prepare_refcnt != 0);
120 prepare_owner = current;
121 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700122}
123
124static void clk_prepare_unlock(void)
125{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700126 WARN_ON_ONCE(prepare_owner != current);
127 WARN_ON_ONCE(prepare_refcnt == 0);
128
129 if (--prepare_refcnt)
130 return;
131 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700132 mutex_unlock(&prepare_lock);
133}
134
135static unsigned long clk_enable_lock(void)
Stephen Boyda57aa182015-07-24 12:24:48 -0700136 __acquires(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700137{
138 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700139
140 if (!spin_trylock_irqsave(&enable_lock, flags)) {
141 if (enable_owner == current) {
142 enable_refcnt++;
Stephen Boyda57aa182015-07-24 12:24:48 -0700143 __acquire(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700144 return flags;
145 }
146 spin_lock_irqsave(&enable_lock, flags);
147 }
148 WARN_ON_ONCE(enable_owner != NULL);
149 WARN_ON_ONCE(enable_refcnt != 0);
150 enable_owner = current;
151 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700152 return flags;
153}
154
155static void clk_enable_unlock(unsigned long flags)
Stephen Boyda57aa182015-07-24 12:24:48 -0700156 __releases(enable_lock)
Mike Turquetteeab89f62013-03-28 13:59:01 -0700157{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700158 WARN_ON_ONCE(enable_owner != current);
159 WARN_ON_ONCE(enable_refcnt == 0);
160
Stephen Boyda57aa182015-07-24 12:24:48 -0700161 if (--enable_refcnt) {
162 __release(enable_lock);
Mike Turquette533ddeb2013-03-28 13:59:02 -0700163 return;
Stephen Boyda57aa182015-07-24 12:24:48 -0700164 }
Mike Turquette533ddeb2013-03-28 13:59:02 -0700165 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700166 spin_unlock_irqrestore(&enable_lock, flags);
167}
168
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700169static bool clk_core_is_prepared(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530170{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700171 /*
172 * .is_prepared is optional for clocks that can prepare
173 * fall back to software usage counter if it is missing
174 */
175 if (!core->ops->is_prepared)
176 return core->prepare_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530177
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700178 return core->ops->is_prepared(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530179}
180
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700181static bool clk_core_is_enabled(struct clk_core *core)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530182{
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700183 /*
184 * .is_enabled is only mandatory for clocks that gate
185 * fall back to software usage counter if .is_enabled is missing
186 */
187 if (!core->ops->is_enabled)
188 return core->enable_count;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530189
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700190 return core->ops->is_enabled(core->hw);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530191}
192
Mike Turquetteb24764902012-03-15 23:11:19 -0700193/*** helper functions ***/
194
Geert Uytterhoevenb76281c2015-10-16 14:35:21 +0200195const char *__clk_get_name(const struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700196{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100197 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700198}
Niels de Vos48950842012-12-13 13:12:25 +0100199EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700200
Stephen Boyde7df6f62015-08-12 13:04:56 -0700201const char *clk_hw_get_name(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700202{
203 return hw->core->name;
204}
205EXPORT_SYMBOL_GPL(clk_hw_get_name);
206
Russ Dill65800b22012-11-26 11:20:09 -0800207struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700208{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100209 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700210}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800211EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700212
Stephen Boyde7df6f62015-08-12 13:04:56 -0700213unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700214{
215 return hw->core->num_parents;
216}
217EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
218
Stephen Boyde7df6f62015-08-12 13:04:56 -0700219struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700220{
221 return hw->core->parent ? hw->core->parent->hw : NULL;
222}
223EXPORT_SYMBOL_GPL(clk_hw_get_parent);
224
Stephen Boyd4dff95d2015-04-30 14:43:22 -0700225static struct clk_core *__clk_lookup_subtree(const char *name,
226 struct clk_core *core)
227{
228 struct clk_core *child;
229 struct clk_core *ret;
230
231 if (!strcmp(core->name, name))
232 return core;
233
234 hlist_for_each_entry(child, &core->children, child_node) {
235 ret = __clk_lookup_subtree(name, child);
236 if (ret)
237 return ret;
238 }
239
240 return NULL;
241}
242
243static struct clk_core *clk_core_lookup(const char *name)
244{
245 struct clk_core *root_clk;
246 struct clk_core *ret;
247
248 if (!name)
249 return NULL;
250
251 /* search the 'proper' clk tree first */
252 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
253 ret = __clk_lookup_subtree(name, root_clk);
254 if (ret)
255 return ret;
256 }
257
258 /* if not found, then search the orphan tree */
259 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
260 ret = __clk_lookup_subtree(name, root_clk);
261 if (ret)
262 return ret;
263 }
264
265 return NULL;
266}
267
Stephen Boydd6968fc2015-04-30 13:54:13 -0700268static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100269 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100270{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700271 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100272 return NULL;
Masahiro Yamada88cfbef2015-12-28 19:23:01 +0900273
274 if (!core->parents[index])
275 core->parents[index] =
276 clk_core_lookup(core->parent_names[index]);
277
278 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100279}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100280
Stephen Boyde7df6f62015-08-12 13:04:56 -0700281struct clk_hw *
282clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700283{
284 struct clk_core *parent;
285
286 parent = clk_core_get_parent_by_index(hw->core, index);
287
288 return !parent ? NULL : parent->hw;
289}
290EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
291
Russ Dill65800b22012-11-26 11:20:09 -0800292unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700293{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100294 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700295}
296
Stephen Boydd6968fc2015-04-30 13:54:13 -0700297static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700298{
299 unsigned long ret;
300
Stephen Boydd6968fc2015-04-30 13:54:13 -0700301 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530302 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700303 goto out;
304 }
305
Stephen Boydd6968fc2015-04-30 13:54:13 -0700306 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700307
Stephen Boyd47b0eeb2016-02-02 17:24:56 -0800308 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -0700309 goto out;
310
Stephen Boydd6968fc2015-04-30 13:54:13 -0700311 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530312 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700313
314out:
315 return ret;
316}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100317
Stephen Boyde7df6f62015-08-12 13:04:56 -0700318unsigned long clk_hw_get_rate(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700319{
320 return clk_core_get_rate_nolock(hw->core);
321}
322EXPORT_SYMBOL_GPL(clk_hw_get_rate);
323
Stephen Boydd6968fc2015-04-30 13:54:13 -0700324static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100325{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700326 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100327 return 0;
328
Stephen Boydd6968fc2015-04-30 13:54:13 -0700329 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100330}
331
Russ Dill65800b22012-11-26 11:20:09 -0800332unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700333{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100334 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700335}
Thierry Redingb05c6832013-09-03 09:43:51 +0200336EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700337
Stephen Boyde7df6f62015-08-12 13:04:56 -0700338unsigned long clk_hw_get_flags(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700339{
340 return hw->core->flags;
341}
342EXPORT_SYMBOL_GPL(clk_hw_get_flags);
343
Stephen Boyde7df6f62015-08-12 13:04:56 -0700344bool clk_hw_is_prepared(const struct clk_hw *hw)
Stephen Boyd1a9c0692015-06-25 15:55:14 -0700345{
346 return clk_core_is_prepared(hw->core);
347}
348
Joachim Eastwoodbe68bf82015-10-24 18:55:22 +0200349bool clk_hw_is_enabled(const struct clk_hw *hw)
350{
351 return clk_core_is_enabled(hw->core);
352}
353
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100354bool __clk_is_enabled(struct clk *clk)
355{
356 if (!clk)
357 return false;
358
359 return clk_core_is_enabled(clk->core);
360}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800361EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700362
Stephen Boyd15a02c12015-01-19 18:05:28 -0800363static bool mux_is_better_rate(unsigned long rate, unsigned long now,
364 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100365{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800366 if (flags & CLK_MUX_ROUND_CLOSEST)
367 return abs(now - rate) < abs(best - rate);
368
369 return now <= rate && now > best;
370}
371
Boris Brezillon0817b622015-07-07 20:48:08 +0200372static int
373clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800374 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100375{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100376 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
Boris Brezillon0817b622015-07-07 20:48:08 +0200377 int i, num_parents, ret;
378 unsigned long best = 0;
379 struct clk_rate_request parent_req = *req;
James Hogane366fdd2013-07-29 12:25:02 +0100380
381 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100382 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
383 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200384 if (core->flags & CLK_SET_RATE_PARENT) {
385 ret = __clk_determine_rate(parent ? parent->hw : NULL,
386 &parent_req);
387 if (ret)
388 return ret;
389
390 best = parent_req.rate;
391 } else if (parent) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100392 best = clk_core_get_rate_nolock(parent);
Boris Brezillon0817b622015-07-07 20:48:08 +0200393 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100394 best = clk_core_get_rate_nolock(core);
Boris Brezillon0817b622015-07-07 20:48:08 +0200395 }
396
James Hogane366fdd2013-07-29 12:25:02 +0100397 goto out;
398 }
399
400 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100401 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100402 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100403 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100404 if (!parent)
405 continue;
Boris Brezillon0817b622015-07-07 20:48:08 +0200406
407 if (core->flags & CLK_SET_RATE_PARENT) {
408 parent_req = *req;
409 ret = __clk_determine_rate(parent->hw, &parent_req);
410 if (ret)
411 continue;
412 } else {
413 parent_req.rate = clk_core_get_rate_nolock(parent);
414 }
415
416 if (mux_is_better_rate(req->rate, parent_req.rate,
417 best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100418 best_parent = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200419 best = parent_req.rate;
James Hogane366fdd2013-07-29 12:25:02 +0100420 }
421 }
422
Boris Brezillon57d866e2015-07-09 22:39:38 +0200423 if (!best_parent)
424 return -EINVAL;
425
James Hogane366fdd2013-07-29 12:25:02 +0100426out:
427 if (best_parent)
Boris Brezillon0817b622015-07-07 20:48:08 +0200428 req->best_parent_hw = best_parent->hw;
429 req->best_parent_rate = best;
430 req->rate = best;
James Hogane366fdd2013-07-29 12:25:02 +0100431
Boris Brezillon0817b622015-07-07 20:48:08 +0200432 return 0;
James Hogane366fdd2013-07-29 12:25:02 +0100433}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800434
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100435struct clk *__clk_lookup(const char *name)
436{
437 struct clk_core *core = clk_core_lookup(name);
438
439 return !core ? NULL : core->hw->clk;
440}
441
Stephen Boydd6968fc2015-04-30 13:54:13 -0700442static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100443 unsigned long *min_rate,
444 unsigned long *max_rate)
445{
446 struct clk *clk_user;
447
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700448 *min_rate = core->min_rate;
449 *max_rate = core->max_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100450
Stephen Boydd6968fc2015-04-30 13:54:13 -0700451 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100452 *min_rate = max(*min_rate, clk_user->min_rate);
453
Stephen Boydd6968fc2015-04-30 13:54:13 -0700454 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100455 *max_rate = min(*max_rate, clk_user->max_rate);
456}
457
Stephen Boyd9783c0d2015-07-16 12:50:27 -0700458void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
459 unsigned long max_rate)
460{
461 hw->core->min_rate = min_rate;
462 hw->core->max_rate = max_rate;
463}
464EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
465
Stephen Boyd15a02c12015-01-19 18:05:28 -0800466/*
Taniya Dasd9744c12016-08-19 10:08:28 +0530467 * Aggregate the rate of all child nodes which are enabled and exclude the
468 * child node which requests for clk_aggregate_rate.
469 */
470unsigned long clk_aggregate_rate(struct clk_hw *hw,
471 const struct clk_core *parent)
472{
473 struct clk_core *child;
474 unsigned long aggre_rate = 0;
475
476 hlist_for_each_entry(child, &parent->children, child_node) {
477 if (child->enable_count &&
478 strcmp(child->name, hw->init->name))
479 aggre_rate = max(child->rate, aggre_rate);
480 }
481
482 return aggre_rate;
483}
484EXPORT_SYMBOL_GPL(clk_aggregate_rate);
485
486/*
Stephen Boyd15a02c12015-01-19 18:05:28 -0800487 * Helper for finding best parent to provide a given frequency. This can be used
488 * directly as a determine_rate callback (e.g. for a mux), or from a more
489 * complex clock that may combine a mux with other operations.
490 */
Boris Brezillon0817b622015-07-07 20:48:08 +0200491int __clk_mux_determine_rate(struct clk_hw *hw,
492 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800493{
Boris Brezillon0817b622015-07-07 20:48:08 +0200494 return clk_mux_determine_rate_flags(hw, req, 0);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800495}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800496EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100497
Boris Brezillon0817b622015-07-07 20:48:08 +0200498int __clk_mux_determine_rate_closest(struct clk_hw *hw,
499 struct clk_rate_request *req)
Stephen Boyd15a02c12015-01-19 18:05:28 -0800500{
Boris Brezillon0817b622015-07-07 20:48:08 +0200501 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800502}
503EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
504
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530505/*
506 * Find the voltage level required for a given clock rate.
507 */
508static int clk_find_vdd_level(struct clk_core *clk, unsigned long rate)
509{
510 int level;
511
Deepak Katragaddaa755ecb2016-11-28 14:48:56 -0800512 /*
513 * For certain PLLs, due to the limitation in the bits allocated for
514 * programming the fractional divider, the actual rate of the PLL will
515 * be slightly higher than the requested rate (in the order of several
516 * Hz). To accommodate this difference, convert the FMAX rate and the
517 * clock frequency to KHz and use that for deriving the voltage level.
518 */
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530519 for (level = 0; level < clk->num_rate_max; level++)
Deepak Katragaddaa755ecb2016-11-28 14:48:56 -0800520 if (DIV_ROUND_CLOSEST(rate, 1000) <=
521 DIV_ROUND_CLOSEST(clk->rate_max[level], 1000))
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530522 break;
523
524 if (level == clk->num_rate_max) {
525 pr_err("Rate %lu for %s is greater than highest Fmax\n", rate,
526 clk->name);
527 return -EINVAL;
528 }
529
530 return level;
531}
532
533/*
534 * Update voltage level given the current votes.
535 */
536static int clk_update_vdd(struct clk_vdd_class *vdd_class)
537{
538 int level, rc = 0, i, ignore;
539 struct regulator **r = vdd_class->regulator;
540 int *uv = vdd_class->vdd_uv;
541 int n_reg = vdd_class->num_regulators;
542 int cur_lvl = vdd_class->cur_level;
543 int max_lvl = vdd_class->num_levels - 1;
544 int cur_base = cur_lvl * n_reg;
545 int new_base;
546
547 /* aggregate votes */
548 for (level = max_lvl; level > 0; level--)
549 if (vdd_class->level_votes[level])
550 break;
551
552 if (level == cur_lvl)
553 return 0;
554
555 max_lvl = max_lvl * n_reg;
556 new_base = level * n_reg;
557
558 for (i = 0; i < vdd_class->num_regulators; i++) {
559 pr_debug("Set Voltage level Min %d, Max %d\n", uv[new_base + i],
560 uv[max_lvl + i]);
561 rc = regulator_set_voltage(r[i], uv[new_base + i],
Taniya Daseee50c82016-12-03 19:06:59 +0530562 vdd_class->use_max_uV ? INT_MAX : uv[max_lvl + i]);
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530563 if (rc)
564 goto set_voltage_fail;
565
566 if (cur_lvl == 0 || cur_lvl == vdd_class->num_levels)
567 rc = regulator_enable(r[i]);
568 else if (level == 0)
569 rc = regulator_disable(r[i]);
570 if (rc)
571 goto enable_disable_fail;
572 }
573
574 if (vdd_class->set_vdd && !vdd_class->num_regulators)
575 rc = vdd_class->set_vdd(vdd_class, level);
576
577 if (!rc)
578 vdd_class->cur_level = level;
579
580 return rc;
581
582enable_disable_fail:
Taniya Daseee50c82016-12-03 19:06:59 +0530583 regulator_set_voltage(r[i], uv[cur_base + i],
584 vdd_class->use_max_uV ? INT_MAX : uv[max_lvl + i]);
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530585
586set_voltage_fail:
587 for (i--; i >= 0; i--) {
Taniya Daseee50c82016-12-03 19:06:59 +0530588 regulator_set_voltage(r[i], uv[cur_base + i],
589 vdd_class->use_max_uV ? INT_MAX : uv[max_lvl + i]);
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530590 if (cur_lvl == 0 || cur_lvl == vdd_class->num_levels)
591 regulator_disable(r[i]);
592 else if (level == 0)
593 ignore = regulator_enable(r[i]);
594 }
595
596 return rc;
597}
598
599/*
600 * Vote for a voltage level.
601 */
602static int clk_vote_vdd_level(struct clk_vdd_class *vdd_class, int level)
603{
604 int rc = 0;
605
606 if (level >= vdd_class->num_levels)
607 return -EINVAL;
608
609 mutex_lock(&vdd_class->lock);
610
611 vdd_class->level_votes[level]++;
612
613 rc = clk_update_vdd(vdd_class);
614 if (rc)
615 vdd_class->level_votes[level]--;
616
617 mutex_unlock(&vdd_class->lock);
618
619 return rc;
620}
621
622/*
623 * Remove vote for a voltage level.
624 */
625static int clk_unvote_vdd_level(struct clk_vdd_class *vdd_class, int level)
626{
627 int rc = 0;
628
629 if (level >= vdd_class->num_levels)
630 return -EINVAL;
631
632 mutex_lock(&vdd_class->lock);
633
634 if (WARN(!vdd_class->level_votes[level],
635 "Reference counts are incorrect for %s level %d\n",
636 vdd_class->class_name, level))
637 goto out;
638
639 vdd_class->level_votes[level]--;
640
641 rc = clk_update_vdd(vdd_class);
642 if (rc)
643 vdd_class->level_votes[level]++;
644
645out:
646 mutex_unlock(&vdd_class->lock);
647 return rc;
648}
649
650/*
651 * Vote for a voltage level corresponding to a clock's rate.
652 */
653static int clk_vote_rate_vdd(struct clk_core *core, unsigned long rate)
654{
655 int level;
656
657 if (!core->vdd_class)
658 return 0;
659
660 level = clk_find_vdd_level(core, rate);
661 if (level < 0)
662 return level;
663
664 return clk_vote_vdd_level(core->vdd_class, level);
665}
666
667/*
668 * Remove vote for a voltage level corresponding to a clock's rate.
669 */
670static void clk_unvote_rate_vdd(struct clk_core *core, unsigned long rate)
671{
672 int level;
673
674 if (!core->vdd_class)
675 return;
676
677 level = clk_find_vdd_level(core, rate);
678 if (level < 0)
679 return;
680
681 clk_unvote_vdd_level(core->vdd_class, level);
682}
683
684static bool clk_is_rate_level_valid(struct clk_core *core, unsigned long rate)
685{
686 int level;
687
688 if (!core->vdd_class)
689 return true;
690
691 level = clk_find_vdd_level(core, rate);
692
693 return level >= 0;
694}
695
696static int clk_vdd_class_init(struct clk_vdd_class *vdd)
697{
698 struct clk_handoff_vdd *v;
699
Taniya Daseee50c82016-12-03 19:06:59 +0530700 if (vdd->skip_handoff)
701 return 0;
702
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530703 list_for_each_entry(v, &clk_handoff_vdd_list, list) {
704 if (v->vdd_class == vdd)
705 return 0;
706 }
707
708 pr_debug("voting for vdd_class %s\n", vdd->class_name);
709
710 if (clk_vote_vdd_level(vdd, vdd->num_levels - 1))
711 pr_err("failed to vote for %s\n", vdd->class_name);
712
713 v = kmalloc(sizeof(*v), GFP_KERNEL);
714 if (!v)
715 return -ENOMEM;
716
717 v->vdd_class = vdd;
718
719 list_add_tail(&v->list, &clk_handoff_vdd_list);
720
721 return 0;
722}
723
Mike Turquetteb24764902012-03-15 23:11:19 -0700724/*** clk api ***/
725
Stephen Boydd6968fc2015-04-30 13:54:13 -0700726static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700727{
Stephen Boyda6334722015-05-06 17:00:54 -0700728 lockdep_assert_held(&prepare_lock);
729
Stephen Boydd6968fc2015-04-30 13:54:13 -0700730 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700731 return;
732
Stephen Boydd6968fc2015-04-30 13:54:13 -0700733 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700734 return;
735
Lee Jones2e20fbf2016-02-11 13:19:10 -0800736 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
737 return;
738
Stephen Boydd6968fc2015-04-30 13:54:13 -0700739 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700740 return;
741
Stephen Boydd6968fc2015-04-30 13:54:13 -0700742 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700743
Stephen Boydd6968fc2015-04-30 13:54:13 -0700744 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800745
Stephen Boydd6968fc2015-04-30 13:54:13 -0700746 if (core->ops->unprepare)
747 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700748
Stephen Boydd6968fc2015-04-30 13:54:13 -0700749 trace_clk_unprepare_complete(core);
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530750
751 clk_unvote_rate_vdd(core, core->rate);
752
Stephen Boydd6968fc2015-04-30 13:54:13 -0700753 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700754}
755
Dong Aishenga6adc302016-06-30 17:31:11 +0800756static void clk_core_unprepare_lock(struct clk_core *core)
757{
758 clk_prepare_lock();
759 clk_core_unprepare(core);
760 clk_prepare_unlock();
761}
762
Mike Turquetteb24764902012-03-15 23:11:19 -0700763/**
764 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200765 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700766 *
767 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
768 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
769 * if the operation may sleep. One example is a clk which is accessed over
770 * I2c. In the complex case a clk gate operation may require a fast and a slow
771 * part. It is this reason that clk_unprepare and clk_disable are not mutually
772 * exclusive. In fact clk_disable must be called before clk_unprepare.
773 */
774void clk_unprepare(struct clk *clk)
775{
Stephen Boyd63589e92014-03-26 16:06:37 -0700776 if (IS_ERR_OR_NULL(clk))
777 return;
778
Dong Aishenga6adc302016-06-30 17:31:11 +0800779 clk_core_unprepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700780}
781EXPORT_SYMBOL_GPL(clk_unprepare);
782
Stephen Boydd6968fc2015-04-30 13:54:13 -0700783static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700784{
785 int ret = 0;
786
Stephen Boyda6334722015-05-06 17:00:54 -0700787 lockdep_assert_held(&prepare_lock);
788
Stephen Boydd6968fc2015-04-30 13:54:13 -0700789 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700790 return 0;
791
Stephen Boydd6968fc2015-04-30 13:54:13 -0700792 if (core->prepare_count == 0) {
793 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700794 if (ret)
795 return ret;
796
Stephen Boydd6968fc2015-04-30 13:54:13 -0700797 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800798
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530799 ret = clk_vote_rate_vdd(core, core->rate);
800 if (ret) {
801 clk_core_unprepare(core->parent);
802 return ret;
803 }
804
Stephen Boydd6968fc2015-04-30 13:54:13 -0700805 if (core->ops->prepare)
806 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800807
Stephen Boydd6968fc2015-04-30 13:54:13 -0700808 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800809
810 if (ret) {
Stephen Boyd5cb05a12016-05-16 11:05:16 +0530811 clk_unvote_rate_vdd(core, core->rate);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700812 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800813 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700814 }
815 }
816
Stephen Boydd6968fc2015-04-30 13:54:13 -0700817 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700818
819 return 0;
820}
821
Dong Aishenga6adc302016-06-30 17:31:11 +0800822static int clk_core_prepare_lock(struct clk_core *core)
823{
824 int ret;
825
826 clk_prepare_lock();
827 ret = clk_core_prepare(core);
828 clk_prepare_unlock();
829
830 return ret;
831}
832
Mike Turquetteb24764902012-03-15 23:11:19 -0700833/**
834 * clk_prepare - prepare a clock source
835 * @clk: the clk being prepared
836 *
837 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
838 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
839 * operation may sleep. One example is a clk which is accessed over I2c. In
840 * the complex case a clk ungate operation may require a fast and a slow part.
841 * It is this reason that clk_prepare and clk_enable are not mutually
842 * exclusive. In fact clk_prepare must be called before clk_enable.
843 * Returns 0 on success, -EERROR otherwise.
844 */
845int clk_prepare(struct clk *clk)
846{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100847 if (!clk)
848 return 0;
849
Dong Aishenga6adc302016-06-30 17:31:11 +0800850 return clk_core_prepare_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700851}
852EXPORT_SYMBOL_GPL(clk_prepare);
853
Stephen Boydd6968fc2015-04-30 13:54:13 -0700854static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700855{
Stephen Boyda6334722015-05-06 17:00:54 -0700856 lockdep_assert_held(&enable_lock);
857
Stephen Boydd6968fc2015-04-30 13:54:13 -0700858 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700859 return;
860
Stephen Boydd6968fc2015-04-30 13:54:13 -0700861 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700862 return;
863
Lee Jones2e20fbf2016-02-11 13:19:10 -0800864 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
865 return;
866
Stephen Boydd6968fc2015-04-30 13:54:13 -0700867 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700868 return;
869
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700870 trace_clk_disable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800871
Stephen Boydd6968fc2015-04-30 13:54:13 -0700872 if (core->ops->disable)
873 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700874
Paul E. McKenney2f87a6e2016-04-26 12:43:57 -0700875 trace_clk_disable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800876
Stephen Boydd6968fc2015-04-30 13:54:13 -0700877 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100878}
879
Dong Aishenga6adc302016-06-30 17:31:11 +0800880static void clk_core_disable_lock(struct clk_core *core)
881{
882 unsigned long flags;
883
884 flags = clk_enable_lock();
885 clk_core_disable(core);
886 clk_enable_unlock(flags);
887}
888
Mike Turquetteb24764902012-03-15 23:11:19 -0700889/**
890 * clk_disable - gate a clock
891 * @clk: the clk being gated
892 *
893 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
894 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
895 * clk if the operation is fast and will never sleep. One example is a
896 * SoC-internal clk which is controlled via simple register writes. In the
897 * complex case a clk gate operation may require a fast and a slow part. It is
898 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
899 * In fact clk_disable must be called before clk_unprepare.
900 */
901void clk_disable(struct clk *clk)
902{
Stephen Boyd63589e92014-03-26 16:06:37 -0700903 if (IS_ERR_OR_NULL(clk))
904 return;
905
Dong Aishenga6adc302016-06-30 17:31:11 +0800906 clk_core_disable_lock(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700907}
908EXPORT_SYMBOL_GPL(clk_disable);
909
Stephen Boydd6968fc2015-04-30 13:54:13 -0700910static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700911{
912 int ret = 0;
913
Stephen Boyda6334722015-05-06 17:00:54 -0700914 lockdep_assert_held(&enable_lock);
915
Stephen Boydd6968fc2015-04-30 13:54:13 -0700916 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700917 return 0;
918
Stephen Boydd6968fc2015-04-30 13:54:13 -0700919 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700920 return -ESHUTDOWN;
921
Stephen Boydd6968fc2015-04-30 13:54:13 -0700922 if (core->enable_count == 0) {
923 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700924
925 if (ret)
926 return ret;
927
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700928 trace_clk_enable_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800929
Stephen Boydd6968fc2015-04-30 13:54:13 -0700930 if (core->ops->enable)
931 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800932
Paul E. McKenneyf17a0dd2016-04-26 14:02:23 -0700933 trace_clk_enable_complete_rcuidle(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800934
935 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700936 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800937 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700938 }
939 }
940
Stephen Boydd6968fc2015-04-30 13:54:13 -0700941 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700942 return 0;
943}
944
Dong Aishenga6adc302016-06-30 17:31:11 +0800945static int clk_core_enable_lock(struct clk_core *core)
946{
947 unsigned long flags;
948 int ret;
949
950 flags = clk_enable_lock();
951 ret = clk_core_enable(core);
952 clk_enable_unlock(flags);
953
954 return ret;
955}
956
Mike Turquetteb24764902012-03-15 23:11:19 -0700957/**
958 * clk_enable - ungate a clock
959 * @clk: the clk being ungated
960 *
961 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
962 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
963 * if the operation will never sleep. One example is a SoC-internal clk which
964 * is controlled via simple register writes. In the complex case a clk ungate
965 * operation may require a fast and a slow part. It is this reason that
966 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
967 * must be called before clk_enable. Returns 0 on success, -EERROR
968 * otherwise.
969 */
970int clk_enable(struct clk *clk)
971{
Dong Aisheng864e1602015-04-30 14:02:19 -0700972 if (!clk)
973 return 0;
974
Dong Aishenga6adc302016-06-30 17:31:11 +0800975 return clk_core_enable_lock(clk->core);
976}
977EXPORT_SYMBOL_GPL(clk_enable);
978
979static int clk_core_prepare_enable(struct clk_core *core)
980{
981 int ret;
982
983 ret = clk_core_prepare_lock(core);
984 if (ret)
985 return ret;
986
987 ret = clk_core_enable_lock(core);
988 if (ret)
989 clk_core_unprepare_lock(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700990
991 return ret;
992}
Dong Aishenga6adc302016-06-30 17:31:11 +0800993
994static void clk_core_disable_unprepare(struct clk_core *core)
995{
996 clk_core_disable_lock(core);
997 clk_core_unprepare_lock(core);
998}
Mike Turquetteb24764902012-03-15 23:11:19 -0700999
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001000static void clk_unprepare_unused_subtree(struct clk_core *core)
1001{
1002 struct clk_core *child;
1003
1004 lockdep_assert_held(&prepare_lock);
1005
1006 hlist_for_each_entry(child, &core->children, child_node)
1007 clk_unprepare_unused_subtree(child);
1008
Taniya Das30684a42017-04-21 13:33:20 +05301009 /*
1010 * setting CLK_ENABLE_HAND_OFF flag triggers this conditional
1011 *
1012 * need_handoff_prepare implies this clk was already prepared by
1013 * __clk_init. now we have a proper user, so unset the flag in our
1014 * internal bookkeeping. See CLK_ENABLE_HAND_OFF flag in clk-provider.h
1015 * for details.
1016 */
1017 if (core->need_handoff_prepare) {
1018 core->need_handoff_prepare = false;
1019 clk_core_unprepare(core);
1020 }
1021
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001022 if (core->prepare_count)
1023 return;
1024
1025 if (core->flags & CLK_IGNORE_UNUSED)
1026 return;
1027
1028 if (clk_core_is_prepared(core)) {
1029 trace_clk_unprepare(core);
1030 if (core->ops->unprepare_unused)
1031 core->ops->unprepare_unused(core->hw);
1032 else if (core->ops->unprepare)
1033 core->ops->unprepare(core->hw);
1034 trace_clk_unprepare_complete(core);
1035 }
1036}
1037
1038static void clk_disable_unused_subtree(struct clk_core *core)
1039{
1040 struct clk_core *child;
1041 unsigned long flags;
1042
1043 lockdep_assert_held(&prepare_lock);
1044
1045 hlist_for_each_entry(child, &core->children, child_node)
1046 clk_disable_unused_subtree(child);
1047
Taniya Das30684a42017-04-21 13:33:20 +05301048 /*
1049 * setting CLK_ENABLE_HAND_OFF flag triggers this conditional
1050 *
1051 * need_handoff_enable implies this clk was already enabled by
1052 * __clk_init. now we have a proper user, so unset the flag in our
1053 * internal bookkeeping. See CLK_ENABLE_HAND_OFF flag in clk-provider.h
1054 * for details.
1055 */
1056 if (core->need_handoff_enable) {
1057 core->need_handoff_enable = false;
1058 flags = clk_enable_lock();
1059 clk_core_disable(core);
1060 clk_enable_unlock(flags);
1061 }
1062
Dong Aishenga4b35182016-06-30 17:31:13 +08001063 if (core->flags & CLK_OPS_PARENT_ENABLE)
1064 clk_core_prepare_enable(core->parent);
1065
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001066 flags = clk_enable_lock();
1067
1068 if (core->enable_count)
1069 goto unlock_out;
1070
1071 if (core->flags & CLK_IGNORE_UNUSED)
1072 goto unlock_out;
1073
1074 /*
1075 * some gate clocks have special needs during the disable-unused
1076 * sequence. call .disable_unused if available, otherwise fall
1077 * back to .disable
1078 */
1079 if (clk_core_is_enabled(core)) {
1080 trace_clk_disable(core);
1081 if (core->ops->disable_unused)
1082 core->ops->disable_unused(core->hw);
1083 else if (core->ops->disable)
1084 core->ops->disable(core->hw);
1085 trace_clk_disable_complete(core);
1086 }
1087
1088unlock_out:
1089 clk_enable_unlock(flags);
Dong Aishenga4b35182016-06-30 17:31:13 +08001090 if (core->flags & CLK_OPS_PARENT_ENABLE)
1091 clk_core_disable_unprepare(core->parent);
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001092}
1093
1094static bool clk_ignore_unused;
1095static int __init clk_ignore_unused_setup(char *__unused)
1096{
1097 clk_ignore_unused = true;
1098 return 1;
1099}
1100__setup("clk_ignore_unused", clk_ignore_unused_setup);
1101
1102static int clk_disable_unused(void)
1103{
1104 struct clk_core *core;
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301105 struct clk_handoff_vdd *v, *v_temp;
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001106
1107 if (clk_ignore_unused) {
1108 pr_warn("clk: Not disabling unused clocks\n");
1109 return 0;
1110 }
1111
1112 clk_prepare_lock();
1113
1114 hlist_for_each_entry(core, &clk_root_list, child_node)
1115 clk_disable_unused_subtree(core);
1116
1117 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1118 clk_disable_unused_subtree(core);
1119
1120 hlist_for_each_entry(core, &clk_root_list, child_node)
1121 clk_unprepare_unused_subtree(core);
1122
1123 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1124 clk_unprepare_unused_subtree(core);
1125
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301126 list_for_each_entry_safe(v, v_temp, &clk_handoff_vdd_list, list) {
1127 clk_unvote_vdd_level(v->vdd_class,
1128 v->vdd_class->num_levels - 1);
1129 list_del(&v->list);
1130 kfree(v);
1131 };
1132
Dong Aisheng7ec986e2016-06-30 17:31:12 +08001133 clk_prepare_unlock();
1134
1135 return 0;
1136}
1137late_initcall_sync(clk_disable_unused);
1138
Boris Brezillon0817b622015-07-07 20:48:08 +02001139static int clk_core_round_rate_nolock(struct clk_core *core,
1140 struct clk_rate_request *req)
Mike Turquetteb24764902012-03-15 23:11:19 -07001141{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001142 struct clk_core *parent;
Boris Brezillon0817b622015-07-07 20:48:08 +02001143 long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001144
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001145 lockdep_assert_held(&prepare_lock);
1146
Stephen Boydd6968fc2015-04-30 13:54:13 -07001147 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001148 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001149
Stephen Boydd6968fc2015-04-30 13:54:13 -07001150 parent = core->parent;
Boris Brezillon0817b622015-07-07 20:48:08 +02001151 if (parent) {
1152 req->best_parent_hw = parent->hw;
1153 req->best_parent_rate = parent->rate;
1154 } else {
1155 req->best_parent_hw = NULL;
1156 req->best_parent_rate = 0;
1157 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001158
Stephen Boydd6968fc2015-04-30 13:54:13 -07001159 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001160 return core->ops->determine_rate(core->hw, req);
1161 } else if (core->ops->round_rate) {
1162 rate = core->ops->round_rate(core->hw, req->rate,
1163 &req->best_parent_rate);
1164 if (rate < 0)
1165 return rate;
1166
1167 req->rate = rate;
1168 } else if (core->flags & CLK_SET_RATE_PARENT) {
1169 return clk_core_round_rate_nolock(parent, req);
1170 } else {
1171 req->rate = core->rate;
1172 }
1173
1174 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001175}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001176
1177/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001178 * __clk_determine_rate - get the closest rate actually supported by a clock
1179 * @hw: determine the rate of this clock
Peng Fan2d5b5202016-06-13 19:34:21 +08001180 * @req: target rate request
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001181 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001182 * Useful for clk_ops such as .set_rate and .determine_rate.
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001183 */
Boris Brezillon0817b622015-07-07 20:48:08 +02001184int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001185{
Boris Brezillon0817b622015-07-07 20:48:08 +02001186 if (!hw) {
1187 req->rate = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001188 return 0;
Boris Brezillon0817b622015-07-07 20:48:08 +02001189 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001190
Boris Brezillon0817b622015-07-07 20:48:08 +02001191 return clk_core_round_rate_nolock(hw->core, req);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001192}
1193EXPORT_SYMBOL_GPL(__clk_determine_rate);
1194
Stephen Boyd1a9c0692015-06-25 15:55:14 -07001195unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1196{
1197 int ret;
1198 struct clk_rate_request req;
1199
1200 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1201 req.rate = rate;
1202
1203 ret = clk_core_round_rate_nolock(hw->core, &req);
1204 if (ret)
1205 return 0;
1206
1207 return req.rate;
1208}
1209EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1210
Mike Turquetteb24764902012-03-15 23:11:19 -07001211/**
1212 * clk_round_rate - round the given rate for a clk
1213 * @clk: the clk for which we are rounding a rate
1214 * @rate: the rate which is to be rounded
1215 *
1216 * Takes in a rate as input and rounds it to a rate that the clk can actually
1217 * use which is then returned. If clk doesn't support round_rate operation
1218 * then the parent rate is returned.
1219 */
1220long clk_round_rate(struct clk *clk, unsigned long rate)
1221{
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001222 struct clk_rate_request req;
1223 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001224
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001225 if (!clk)
1226 return 0;
1227
Mike Turquetteeab89f62013-03-28 13:59:01 -07001228 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001229
1230 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1231 req.rate = rate;
1232
1233 ret = clk_core_round_rate_nolock(clk->core, &req);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001234 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001235
Stephen Boydfc4a05d2015-06-25 17:24:15 -07001236 if (ret)
1237 return ret;
1238
1239 return req.rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001240}
1241EXPORT_SYMBOL_GPL(clk_round_rate);
1242
1243/**
1244 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001245 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001246 * @msg: clk notifier type (see include/linux/clk.h)
1247 * @old_rate: old clk rate
1248 * @new_rate: new clk rate
1249 *
1250 * Triggers a notifier call chain on the clk rate-change notification
1251 * for 'clk'. Passes a pointer to the struct clk and the previous
1252 * and current rates to the notifier callback. Intended to be called by
1253 * internal clock code only. Returns NOTIFY_DONE from the last driver
1254 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1255 * a driver returns that.
1256 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001257static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001258 unsigned long old_rate, unsigned long new_rate)
1259{
1260 struct clk_notifier *cn;
1261 struct clk_notifier_data cnd;
1262 int ret = NOTIFY_DONE;
1263
Mike Turquetteb24764902012-03-15 23:11:19 -07001264 cnd.old_rate = old_rate;
1265 cnd.new_rate = new_rate;
1266
1267 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001268 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001269 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001270 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1271 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -07001272 }
1273 }
1274
1275 return ret;
1276}
1277
1278/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001279 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001280 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001281 *
1282 * Walks the subtree of clks starting with clk and recalculates accuracies as
1283 * it goes. Note that if a clk does not implement the .recalc_accuracy
Stephen Boyd6e5ab412015-04-30 15:11:31 -07001284 * callback then it is assumed that the clock will take on the accuracy of its
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001285 * parent.
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001286 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001287static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001288{
1289 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001290 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001291
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001292 lockdep_assert_held(&prepare_lock);
1293
Stephen Boydd6968fc2015-04-30 13:54:13 -07001294 if (core->parent)
1295 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001296
Stephen Boydd6968fc2015-04-30 13:54:13 -07001297 if (core->ops->recalc_accuracy)
1298 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001299 parent_accuracy);
1300 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001301 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001302
Stephen Boydd6968fc2015-04-30 13:54:13 -07001303 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001304 __clk_recalc_accuracies(child);
1305}
1306
Stephen Boydd6968fc2015-04-30 13:54:13 -07001307static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001308{
1309 unsigned long accuracy;
1310
1311 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001312 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1313 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001314
Stephen Boydd6968fc2015-04-30 13:54:13 -07001315 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001316 clk_prepare_unlock();
1317
1318 return accuracy;
1319}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001320
1321/**
1322 * clk_get_accuracy - return the accuracy of clk
1323 * @clk: the clk whose accuracy is being returned
1324 *
1325 * Simply returns the cached accuracy of the clk, unless
1326 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1327 * issued.
1328 * If clk is NULL then returns 0.
1329 */
1330long clk_get_accuracy(struct clk *clk)
1331{
1332 if (!clk)
1333 return 0;
1334
1335 return clk_core_get_accuracy(clk->core);
1336}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001337EXPORT_SYMBOL_GPL(clk_get_accuracy);
1338
Stephen Boydd6968fc2015-04-30 13:54:13 -07001339static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001340 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001341{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001342 if (core->ops->recalc_rate)
1343 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001344 return parent_rate;
1345}
1346
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001347/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001348 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001349 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001350 * @msg: notification type (see include/linux/clk.h)
1351 *
1352 * Walks the subtree of clks starting with clk and recalculates rates as it
1353 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001354 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001355 *
1356 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1357 * if necessary.
Mike Turquetteb24764902012-03-15 23:11:19 -07001358 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001359static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001360{
1361 unsigned long old_rate;
1362 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001363 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001364
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001365 lockdep_assert_held(&prepare_lock);
1366
Stephen Boydd6968fc2015-04-30 13:54:13 -07001367 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001368
Stephen Boydd6968fc2015-04-30 13:54:13 -07001369 if (core->parent)
1370 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001371
Stephen Boydd6968fc2015-04-30 13:54:13 -07001372 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001373
1374 /*
1375 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1376 * & ABORT_RATE_CHANGE notifiers
1377 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001378 if (core->notifier_count && msg)
1379 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001380
Stephen Boydd6968fc2015-04-30 13:54:13 -07001381 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001382 __clk_recalc_rates(child, msg);
1383}
1384
Stephen Boydd6968fc2015-04-30 13:54:13 -07001385static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001386{
1387 unsigned long rate;
1388
1389 clk_prepare_lock();
1390
Stephen Boydd6968fc2015-04-30 13:54:13 -07001391 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1392 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001393
Stephen Boydd6968fc2015-04-30 13:54:13 -07001394 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001395 clk_prepare_unlock();
1396
1397 return rate;
1398}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001399
Mike Turquetteb24764902012-03-15 23:11:19 -07001400/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001401 * clk_get_rate - return the rate of clk
1402 * @clk: the clk whose rate is being returned
1403 *
1404 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1405 * is set, which means a recalc_rate will be issued.
1406 * If clk is NULL then returns 0.
1407 */
1408unsigned long clk_get_rate(struct clk *clk)
1409{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001410 if (!clk)
1411 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001412
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001413 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001414}
1415EXPORT_SYMBOL_GPL(clk_get_rate);
1416
Stephen Boydd6968fc2015-04-30 13:54:13 -07001417static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001418 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001419{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001420 int i;
James Hogan4935b222013-07-29 12:24:59 +01001421
Masahiro Yamada508f8842015-12-28 19:23:08 +09001422 if (!parent)
1423 return -EINVAL;
1424
Masahiro Yamada470b5e22015-12-28 19:23:09 +09001425 for (i = 0; i < core->num_parents; i++)
1426 if (clk_core_get_parent_by_index(core, i) == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001427 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001428
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001429 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001430}
1431
Heiko Stuebnere6500342015-04-22 22:53:05 +02001432/*
1433 * Update the orphan status of @core and all its children.
1434 */
1435static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1436{
1437 struct clk_core *child;
1438
1439 core->orphan = is_orphan;
1440
1441 hlist_for_each_entry(child, &core->children, child_node)
1442 clk_core_update_orphan_status(child, is_orphan);
1443}
1444
Stephen Boydd6968fc2015-04-30 13:54:13 -07001445static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001446{
Heiko Stuebnere6500342015-04-22 22:53:05 +02001447 bool was_orphan = core->orphan;
1448
Stephen Boydd6968fc2015-04-30 13:54:13 -07001449 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001450
James Hogan903efc52013-08-29 12:10:51 +01001451 if (new_parent) {
Heiko Stuebnere6500342015-04-22 22:53:05 +02001452 bool becomes_orphan = new_parent->orphan;
1453
James Hogan903efc52013-08-29 12:10:51 +01001454 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001455 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001456 new_parent->new_child = NULL;
1457
Stephen Boydd6968fc2015-04-30 13:54:13 -07001458 hlist_add_head(&core->child_node, &new_parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001459
1460 if (was_orphan != becomes_orphan)
1461 clk_core_update_orphan_status(core, becomes_orphan);
James Hogan903efc52013-08-29 12:10:51 +01001462 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001463 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02001464 if (!was_orphan)
1465 clk_core_update_orphan_status(core, true);
James Hogan903efc52013-08-29 12:10:51 +01001466 }
James Hogan4935b222013-07-29 12:24:59 +01001467
Stephen Boydd6968fc2015-04-30 13:54:13 -07001468 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001469}
1470
Stephen Boydd6968fc2015-04-30 13:54:13 -07001471static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001472 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001473{
1474 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001475 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001476
1477 /*
Dong Aishengfc8726a2016-06-30 17:31:14 +08001478 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1479 *
1480 * 2. Migrate prepare state between parents and prevent race with
James Hogan4935b222013-07-29 12:24:59 +01001481 * clk_enable().
1482 *
1483 * If the clock is not prepared, then a race with
1484 * clk_enable/disable() is impossible since we already have the
1485 * prepare lock (future calls to clk_enable() need to be preceded by
1486 * a clk_prepare()).
1487 *
1488 * If the clock is prepared, migrate the prepared state to the new
1489 * parent and also protect against a race with clk_enable() by
1490 * forcing the clock and the new parent on. This ensures that all
1491 * future calls to clk_enable() are practically NOPs with respect to
1492 * hardware and software states.
1493 *
1494 * See also: Comment for clk_set_parent() below.
1495 */
Dong Aishengfc8726a2016-06-30 17:31:14 +08001496
1497 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1498 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1499 clk_core_prepare_enable(old_parent);
1500 clk_core_prepare_enable(parent);
1501 }
1502
1503 /* migrate prepare count if > 0 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001504 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001505 clk_core_prepare_enable(parent);
1506 clk_core_enable_lock(core);
James Hogan4935b222013-07-29 12:24:59 +01001507 }
1508
1509 /* update the clk tree topology */
1510 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001511 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001512 clk_enable_unlock(flags);
1513
Stephen Boyd3fa22522014-01-15 10:47:22 -08001514 return old_parent;
1515}
1516
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001517static void __clk_set_parent_after(struct clk_core *core,
1518 struct clk_core *parent,
1519 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001520{
1521 /*
1522 * Finish the migration of prepare state and undo the changes done
1523 * for preventing a race with clk_enable().
1524 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001525 if (core->prepare_count) {
Dong Aishengfc8726a2016-06-30 17:31:14 +08001526 clk_core_disable_lock(core);
1527 clk_core_disable_unprepare(old_parent);
1528 }
1529
1530 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1531 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1532 clk_core_disable_unprepare(parent);
1533 clk_core_disable_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001534 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001535}
1536
Stephen Boydd6968fc2015-04-30 13:54:13 -07001537static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001538 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001539{
1540 unsigned long flags;
1541 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001542 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001543
Stephen Boydd6968fc2015-04-30 13:54:13 -07001544 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001545
Stephen Boydd6968fc2015-04-30 13:54:13 -07001546 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001547
James Hogan4935b222013-07-29 12:24:59 +01001548 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001549 if (parent && core->ops->set_parent)
1550 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001551
Stephen Boydd6968fc2015-04-30 13:54:13 -07001552 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001553
James Hogan4935b222013-07-29 12:24:59 +01001554 if (ret) {
1555 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001556 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001557 clk_enable_unlock(flags);
Dong Aishengc660b2eb2015-07-28 21:19:41 +08001558 __clk_set_parent_after(core, old_parent, parent);
James Hogan4935b222013-07-29 12:24:59 +01001559
James Hogan4935b222013-07-29 12:24:59 +01001560 return ret;
1561 }
1562
Stephen Boydd6968fc2015-04-30 13:54:13 -07001563 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001564
James Hogan4935b222013-07-29 12:24:59 +01001565 return 0;
1566}
1567
Ulf Hanssona093bde2012-08-31 14:21:28 +02001568/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001569 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001570 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001571 * @parent_rate: the "future" rate of clk's parent
1572 *
1573 * Walks the subtree of clks starting with clk, speculating rates as it
1574 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1575 *
1576 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1577 * pre-rate change notifications and returns early if no clks in the
1578 * subtree have subscribed to the notifications. Note that if a clk does not
1579 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001580 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001581 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001582static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001583 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001584{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001585 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001586 unsigned long new_rate;
1587 int ret = NOTIFY_DONE;
1588
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001589 lockdep_assert_held(&prepare_lock);
1590
Stephen Boydd6968fc2015-04-30 13:54:13 -07001591 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001592
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001593 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001594 if (core->notifier_count)
1595 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001596
Mike Turquette86bcfa22014-02-24 16:08:41 -08001597 if (ret & NOTIFY_STOP_MASK) {
1598 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001599 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001600 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001601 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001602
Stephen Boydd6968fc2015-04-30 13:54:13 -07001603 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001604 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001605 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001606 break;
1607 }
1608
1609out:
1610 return ret;
1611}
1612
Stephen Boydd6968fc2015-04-30 13:54:13 -07001613static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001614 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001615{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001616 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001617
Stephen Boydd6968fc2015-04-30 13:54:13 -07001618 core->new_rate = new_rate;
1619 core->new_parent = new_parent;
1620 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001621 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001622 core->new_child = NULL;
1623 if (new_parent && new_parent != core->parent)
1624 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001625
Stephen Boydd6968fc2015-04-30 13:54:13 -07001626 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001627 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001628 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001629 }
1630}
1631
1632/*
1633 * calculate the new rates returning the topmost clock that has to be
1634 * changed.
1635 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001636static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001637 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001638{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001639 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001640 struct clk_core *old_parent, *parent;
Shawn Guo81536e02012-04-12 20:50:17 +08001641 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001642 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001643 unsigned long min_rate;
1644 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001645 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001646 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001647
Mike Turquette7452b212012-03-26 14:45:36 -07001648 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001649 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001650 return NULL;
1651
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001652 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001653 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001654 if (parent)
1655 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001656
Stephen Boydd6968fc2015-04-30 13:54:13 -07001657 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001658
James Hogan71472c02013-07-29 12:25:00 +01001659 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001660 if (core->ops->determine_rate) {
Boris Brezillon0817b622015-07-07 20:48:08 +02001661 struct clk_rate_request req;
1662
1663 req.rate = rate;
1664 req.min_rate = min_rate;
1665 req.max_rate = max_rate;
1666 if (parent) {
1667 req.best_parent_hw = parent->hw;
1668 req.best_parent_rate = parent->rate;
1669 } else {
1670 req.best_parent_hw = NULL;
1671 req.best_parent_rate = 0;
1672 }
1673
1674 ret = core->ops->determine_rate(core->hw, &req);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001675 if (ret < 0)
1676 return NULL;
1677
Boris Brezillon0817b622015-07-07 20:48:08 +02001678 best_parent_rate = req.best_parent_rate;
1679 new_rate = req.rate;
1680 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001681 } else if (core->ops->round_rate) {
1682 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon0817b622015-07-07 20:48:08 +02001683 &best_parent_rate);
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001684 if (ret < 0)
1685 return NULL;
1686
1687 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001688 if (new_rate < min_rate || new_rate > max_rate)
1689 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001690 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001691 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001692 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001693 return NULL;
1694 } else {
1695 /* pass-through clock with adjustable parent */
1696 top = clk_calc_new_rates(parent, rate);
1697 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001698 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001699 }
1700
James Hogan71472c02013-07-29 12:25:00 +01001701 /* some clocks must be gated to change parent */
1702 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001703 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001704 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001705 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001706 return NULL;
1707 }
1708
James Hogan71472c02013-07-29 12:25:00 +01001709 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001710 if (parent && core->num_parents > 1) {
1711 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001712 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001713 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001714 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001715 return NULL;
1716 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001717 }
1718
Deepak Katragadda1cabdbe2017-04-28 13:42:50 -07001719 /*
1720 * The Fabia PLLs only have 16 bits to program the fractional divider.
1721 * Hence the programmed rate might be slightly different than the
1722 * requested one.
1723 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001724 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
Deepak Katragadda1cabdbe2017-04-28 13:42:50 -07001725 (DIV_ROUND_CLOSEST(best_parent_rate, 1000) !=
1726 DIV_ROUND_CLOSEST(parent->rate, 1000)))
James Hogan71472c02013-07-29 12:25:00 +01001727 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001728
1729out:
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301730 if (!clk_is_rate_level_valid(core, rate))
1731 return NULL;
1732
Stephen Boydd6968fc2015-04-30 13:54:13 -07001733 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001734
1735 return top;
1736}
1737
1738/*
1739 * Notify about rate changes in a subtree. Always walk down the whole tree
1740 * so that in case of an error we can walk down the whole tree again and
1741 * abort the change.
1742 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001743static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001744 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001745{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001746 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001747 int ret = NOTIFY_DONE;
1748
Stephen Boydd6968fc2015-04-30 13:54:13 -07001749 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301750 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001751
Stephen Boydd6968fc2015-04-30 13:54:13 -07001752 if (core->notifier_count) {
1753 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001754 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001755 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001756 }
1757
Stephen Boydd6968fc2015-04-30 13:54:13 -07001758 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001759 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001760 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001761 continue;
1762 tmp_clk = clk_propagate_rate_change(child, event);
1763 if (tmp_clk)
1764 fail_clk = tmp_clk;
1765 }
1766
Stephen Boydd6968fc2015-04-30 13:54:13 -07001767 /* handle the new child who might not be in core->children yet */
1768 if (core->new_child) {
1769 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001770 if (tmp_clk)
1771 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001772 }
1773
1774 return fail_clk;
1775}
1776
1777/*
1778 * walk down a subtree and set the new rates notifying the rate
1779 * change on the way
1780 */
Taniya Das083a1982016-06-14 16:39:56 +05301781static int clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001782{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001783 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001784 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001785 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001786 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001787 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001788 struct clk_core *old_parent;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001789 struct clk_core *parent = NULL;
Taniya Das083a1982016-06-14 16:39:56 +05301790 int rc = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001791
Stephen Boydd6968fc2015-04-30 13:54:13 -07001792 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001793
Dong Aishengfc8726a2016-06-30 17:31:14 +08001794 if (core->new_parent) {
1795 parent = core->new_parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001796 best_parent_rate = core->new_parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001797 } else if (core->parent) {
1798 parent = core->parent;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001799 best_parent_rate = core->parent->rate;
Dong Aishengfc8726a2016-06-30 17:31:14 +08001800 }
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001801
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001802 if (core->flags & CLK_SET_RATE_UNGATE) {
1803 unsigned long flags;
1804
1805 clk_core_prepare(core);
1806 flags = clk_enable_lock();
1807 clk_core_enable(core);
1808 clk_enable_unlock(flags);
1809 }
1810
Taniya Das0624f562017-05-10 15:23:22 +05301811 trace_clk_set_rate(core, core->new_rate);
1812
1813 /* Enforce vdd requirements for new frequency. */
1814 if (core->prepare_count) {
1815 rc = clk_vote_rate_vdd(core, core->new_rate);
1816 if (rc)
1817 goto out;
1818 }
1819
Stephen Boydd6968fc2015-04-30 13:54:13 -07001820 if (core->new_parent && core->new_parent != core->parent) {
1821 old_parent = __clk_set_parent_before(core, core->new_parent);
1822 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001823
Stephen Boydd6968fc2015-04-30 13:54:13 -07001824 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001825 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001826 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001827 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001828 core->new_parent_index);
1829 } else if (core->ops->set_parent) {
1830 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001831 }
1832
Stephen Boydd6968fc2015-04-30 13:54:13 -07001833 trace_clk_set_parent_complete(core, core->new_parent);
1834 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001835 }
1836
Dong Aishengfc8726a2016-06-30 17:31:14 +08001837 if (core->flags & CLK_OPS_PARENT_ENABLE)
1838 clk_core_prepare_enable(parent);
1839
Taniya Das083a1982016-06-14 16:39:56 +05301840 if (!skip_set_rate && core->ops->set_rate) {
1841 rc = core->ops->set_rate(core->hw, core->new_rate,
1842 best_parent_rate);
1843 if (rc)
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301844 goto err_set_rate;
Taniya Das083a1982016-06-14 16:39:56 +05301845 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001846
Stephen Boydd6968fc2015-04-30 13:54:13 -07001847 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001848
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301849 /* Release vdd requirements for old frequency. */
1850 if (core->prepare_count)
1851 clk_unvote_rate_vdd(core, old_rate);
1852
Stephen Boydd6968fc2015-04-30 13:54:13 -07001853 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001854
Heiko Stuebner2eb8c712015-12-22 22:27:58 +01001855 if (core->flags & CLK_SET_RATE_UNGATE) {
1856 unsigned long flags;
1857
1858 flags = clk_enable_lock();
1859 clk_core_disable(core);
1860 clk_enable_unlock(flags);
1861 clk_core_unprepare(core);
1862 }
1863
Dong Aishengfc8726a2016-06-30 17:31:14 +08001864 if (core->flags & CLK_OPS_PARENT_ENABLE)
1865 clk_core_disable_unprepare(parent);
1866
Stephen Boydd6968fc2015-04-30 13:54:13 -07001867 if (core->notifier_count && old_rate != core->rate)
1868 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001869
Michael Turquette85e88fa2015-06-20 12:18:03 -07001870 if (core->flags & CLK_RECALC_NEW_RATES)
1871 (void)clk_calc_new_rates(core, core->new_rate);
Bartlomiej Zolnierkiewiczd8d91982015-04-03 18:43:44 +02001872
Tero Kristo067bb172014-08-21 16:47:45 +03001873 /*
1874 * Use safe iteration, as change_rate can actually swap parents
1875 * for certain clock types.
1876 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001877 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001878 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001879 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001880 continue;
Taniya Das083a1982016-06-14 16:39:56 +05301881 rc = clk_change_rate(child);
1882 if (rc)
1883 return rc;
James Hogan71472c02013-07-29 12:25:00 +01001884 }
1885
Stephen Boydd6968fc2015-04-30 13:54:13 -07001886 /* handle the new child who might not be in core->children yet */
1887 if (core->new_child)
Taniya Das083a1982016-06-14 16:39:56 +05301888 rc = clk_change_rate(core->new_child);
1889
1890 return rc;
Stephen Boyd5cb05a12016-05-16 11:05:16 +05301891
1892err_set_rate:
1893 if (core->prepare_count)
1894 clk_unvote_rate_vdd(core, core->new_rate);
Taniya Das083a1982016-06-14 16:39:56 +05301895out:
1896 trace_clk_set_rate_complete(core, core->new_rate);
1897
1898 return rc;
Mike Turquetteb24764902012-03-15 23:11:19 -07001899}
1900
Stephen Boydd6968fc2015-04-30 13:54:13 -07001901static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001902 unsigned long req_rate)
1903{
1904 struct clk_core *top, *fail_clk;
1905 unsigned long rate = req_rate;
Taniya Das083a1982016-06-14 16:39:56 +05301906 int ret = 0;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001907
Stephen Boydd6968fc2015-04-30 13:54:13 -07001908 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001909 return 0;
1910
1911 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001912 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001913 return 0;
1914
Stephen Boydd6968fc2015-04-30 13:54:13 -07001915 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001916 return -EBUSY;
1917
1918 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001919 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001920 if (!top)
1921 return -EINVAL;
1922
1923 /* notify that we are about to change rates */
1924 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1925 if (fail_clk) {
Taniya Das083a1982016-06-14 16:39:56 +05301926 pr_debug("%s: failed to set %s clock to run at %lu\n", __func__,
1927 fail_clk->name, req_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001928 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1929 return -EBUSY;
1930 }
1931
1932 /* change the rates */
Taniya Das083a1982016-06-14 16:39:56 +05301933 ret = clk_change_rate(top);
1934 if (ret) {
1935 pr_err("%s: failed to set %s clock to run at %lu\n", __func__,
1936 top->name, req_rate);
1937 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1938 return ret;
1939 }
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001940
Stephen Boydd6968fc2015-04-30 13:54:13 -07001941 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001942
Taniya Das083a1982016-06-14 16:39:56 +05301943 return ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001944}
1945
Mike Turquetteb24764902012-03-15 23:11:19 -07001946/**
1947 * clk_set_rate - specify a new rate for clk
1948 * @clk: the clk whose rate is being changed
1949 * @rate: the new rate for clk
1950 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001951 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001952 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001953 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1954 * propagate up to clk's parent; whether or not this happens depends on the
1955 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1956 * after calling .round_rate then upstream parent propagation is ignored. If
1957 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001958 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001959 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1960 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001961 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001962 * Rate changes are accomplished via tree traversal that also recalculates the
1963 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001964 *
1965 * Returns 0 on success, -EERROR otherwise.
1966 */
1967int clk_set_rate(struct clk *clk, unsigned long rate)
1968{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001969 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001970
Mike Turquette89ac8d72013-08-21 23:58:09 -07001971 if (!clk)
1972 return 0;
1973
Mike Turquetteb24764902012-03-15 23:11:19 -07001974 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001975 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001976
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001977 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001978
Mike Turquetteeab89f62013-03-28 13:59:01 -07001979 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001980
1981 return ret;
1982}
1983EXPORT_SYMBOL_GPL(clk_set_rate);
1984
1985/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001986 * clk_set_rate_range - set a rate range for a clock source
1987 * @clk: clock source
1988 * @min: desired minimum clock rate in Hz, inclusive
1989 * @max: desired maximum clock rate in Hz, inclusive
1990 *
1991 * Returns success (0) or negative errno.
1992 */
1993int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1994{
1995 int ret = 0;
1996
1997 if (!clk)
1998 return 0;
1999
2000 if (min > max) {
2001 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2002 __func__, clk->core->name, clk->dev_id, clk->con_id,
2003 min, max);
2004 return -EINVAL;
2005 }
2006
2007 clk_prepare_lock();
2008
2009 if (min != clk->min_rate || max != clk->max_rate) {
2010 clk->min_rate = min;
2011 clk->max_rate = max;
2012 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2013 }
2014
2015 clk_prepare_unlock();
2016
2017 return ret;
2018}
2019EXPORT_SYMBOL_GPL(clk_set_rate_range);
2020
2021/**
2022 * clk_set_min_rate - set a minimum clock rate for a clock source
2023 * @clk: clock source
2024 * @rate: desired minimum clock rate in Hz, inclusive
2025 *
2026 * Returns success (0) or negative errno.
2027 */
2028int clk_set_min_rate(struct clk *clk, unsigned long rate)
2029{
2030 if (!clk)
2031 return 0;
2032
2033 return clk_set_rate_range(clk, rate, clk->max_rate);
2034}
2035EXPORT_SYMBOL_GPL(clk_set_min_rate);
2036
2037/**
2038 * clk_set_max_rate - set a maximum clock rate for a clock source
2039 * @clk: clock source
2040 * @rate: desired maximum clock rate in Hz, inclusive
2041 *
2042 * Returns success (0) or negative errno.
2043 */
2044int clk_set_max_rate(struct clk *clk, unsigned long rate)
2045{
2046 if (!clk)
2047 return 0;
2048
2049 return clk_set_rate_range(clk, clk->min_rate, rate);
2050}
2051EXPORT_SYMBOL_GPL(clk_set_max_rate);
2052
2053/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002054 * clk_get_parent - return the parent of a clk
2055 * @clk: the clk whose parent gets returned
2056 *
2057 * Simply returns clk->parent. Returns NULL if clk is NULL.
2058 */
2059struct clk *clk_get_parent(struct clk *clk)
2060{
2061 struct clk *parent;
2062
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002063 if (!clk)
2064 return NULL;
2065
Mike Turquetteeab89f62013-03-28 13:59:01 -07002066 clk_prepare_lock();
Stephen Boydfc4a05d2015-06-25 17:24:15 -07002067 /* TODO: Create a per-user clk and change callers to call clk_put */
2068 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteeab89f62013-03-28 13:59:01 -07002069 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002070
2071 return parent;
2072}
2073EXPORT_SYMBOL_GPL(clk_get_parent);
2074
Stephen Boydd6968fc2015-04-30 13:54:13 -07002075static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07002076{
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002077 u8 index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002078
Masahiro Yamada2430a942016-02-09 20:19:14 +09002079 if (core->num_parents > 1 && core->ops->get_parent)
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002080 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002081
Masahiro Yamada5146e0b2015-12-28 19:23:04 +09002082 return clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002083}
2084
Stephen Boydd6968fc2015-04-30 13:54:13 -07002085static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002086 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002087{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002088 clk_reparent(core, new_parent);
2089 __clk_recalc_accuracies(core);
2090 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002091}
2092
Tomeu Vizoso42c86542015-03-11 11:34:25 +01002093void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2094{
2095 if (!hw)
2096 return;
2097
2098 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2099}
2100
Mike Turquetteb24764902012-03-15 23:11:19 -07002101/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002102 * clk_has_parent - check if a clock is a possible parent for another
2103 * @clk: clock source
2104 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002105 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002106 * This function can be used in drivers that need to check that a clock can be
2107 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002108 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002109 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002110 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002111bool clk_has_parent(struct clk *clk, struct clk *parent)
2112{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002113 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002114 unsigned int i;
2115
2116 /* NULL clocks should be nops, so return success if either is NULL. */
2117 if (!clk || !parent)
2118 return true;
2119
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002120 core = clk->core;
2121 parent_core = parent->core;
2122
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002123 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002124 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002125 return true;
2126
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002127 for (i = 0; i < core->num_parents; i++)
2128 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002129 return true;
2130
2131 return false;
2132}
2133EXPORT_SYMBOL_GPL(clk_has_parent);
2134
Stephen Boydd6968fc2015-04-30 13:54:13 -07002135static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002136{
2137 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002138 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002139 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002140
Stephen Boydd6968fc2015-04-30 13:54:13 -07002141 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002142 return 0;
2143
Mike Turquetteb24764902012-03-15 23:11:19 -07002144 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002145 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002146
Taniya Das8436bd72016-11-21 17:50:13 +05302147 if (core->parent == parent && !(core->flags & CLK_IS_MEASURE))
Mike Turquetteb24764902012-03-15 23:11:19 -07002148 goto out;
2149
Stephen Boydb61c43c2015-02-02 14:11:25 -08002150 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002151 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08002152 ret = -ENOSYS;
2153 goto out;
2154 }
2155
Ulf Hansson031dcc92013-04-02 23:09:38 +02002156 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002157 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002158 ret = -EBUSY;
2159 goto out;
2160 }
2161
2162 /* try finding the new parent index */
2163 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002164 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002165 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002166 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002167 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002168 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002169 goto out;
2170 }
Masahiro Yamadae8f0e682015-12-28 19:23:10 +09002171 p_rate = parent->rate;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002172 }
2173
Mike Turquetteb24764902012-03-15 23:11:19 -07002174 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002175 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002176
2177 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002178 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07002179 goto out;
2180
Ulf Hansson031dcc92013-04-02 23:09:38 +02002181 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002182 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002183
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002184 /* propagate rate an accuracy recalculation accordingly */
2185 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002186 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002187 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002188 __clk_recalc_rates(core, POST_RATE_CHANGE);
2189 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002190 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002191
2192out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002193 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002194
2195 return ret;
2196}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002197
2198/**
2199 * clk_set_parent - switch the parent of a mux clk
2200 * @clk: the mux clk whose input we are switching
2201 * @parent: the new input to clk
2202 *
2203 * Re-parent clk to use parent as its new input source. If clk is in
2204 * prepared state, the clk will get enabled for the duration of this call. If
2205 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2206 * that, the reparenting is glitchy in hardware, etc), use the
2207 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2208 *
2209 * After successfully changing clk's parent clk_set_parent will update the
2210 * clk topology, sysfs topology and propagate rate recalculation via
2211 * __clk_recalc_rates.
2212 *
2213 * Returns 0 on success, -EERROR otherwise.
2214 */
2215int clk_set_parent(struct clk *clk, struct clk *parent)
2216{
2217 if (!clk)
2218 return 0;
2219
2220 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
2221}
Mike Turquetteb24764902012-03-15 23:11:19 -07002222EXPORT_SYMBOL_GPL(clk_set_parent);
2223
2224/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002225 * clk_set_phase - adjust the phase shift of a clock signal
2226 * @clk: clock signal source
2227 * @degrees: number of degrees the signal is shifted
2228 *
2229 * Shifts the phase of a clock signal by the specified
2230 * degrees. Returns 0 on success, -EERROR otherwise.
2231 *
2232 * This function makes no distinction about the input or reference
2233 * signal that we adjust the clock signal phase against. For example
2234 * phase locked-loop clock signal generators we may shift phase with
2235 * respect to feedback clock signal input, but for other cases the
2236 * clock phase may be shifted with respect to some other, unspecified
2237 * signal.
2238 *
2239 * Additionally the concept of phase shift does not propagate through
2240 * the clock tree hierarchy, which sets it apart from clock rates and
2241 * clock accuracy. A parent clock phase attribute does not have an
2242 * impact on the phase attribute of a child clock.
2243 */
2244int clk_set_phase(struct clk *clk, int degrees)
2245{
Stephen Boyd08b95752015-02-02 14:09:43 -08002246 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08002247
2248 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002249 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002250
2251 /* sanity check degrees */
2252 degrees %= 360;
2253 if (degrees < 0)
2254 degrees += 360;
2255
2256 clk_prepare_lock();
2257
Stephen Boyddfc202e2015-02-02 14:37:41 -08002258 trace_clk_set_phase(clk->core, degrees);
2259
Stephen Boyd08b95752015-02-02 14:09:43 -08002260 if (clk->core->ops->set_phase)
2261 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002262
Stephen Boyddfc202e2015-02-02 14:37:41 -08002263 trace_clk_set_phase_complete(clk->core, degrees);
2264
Mike Turquettee59c5372014-02-18 21:21:25 -08002265 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002266 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08002267
Mike Turquettee59c5372014-02-18 21:21:25 -08002268 clk_prepare_unlock();
2269
Mike Turquettee59c5372014-02-18 21:21:25 -08002270 return ret;
2271}
Maxime Ripard9767b042015-01-20 22:23:43 +01002272EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002273
Stephen Boydd6968fc2015-04-30 13:54:13 -07002274static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002275{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002276 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002277
2278 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07002279 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002280 clk_prepare_unlock();
2281
Mike Turquettee59c5372014-02-18 21:21:25 -08002282 return ret;
2283}
2284
2285/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002286 * clk_get_phase - return the phase shift of a clock signal
2287 * @clk: clock signal source
2288 *
2289 * Returns the phase shift of a clock node in degrees, otherwise returns
2290 * -EERROR.
2291 */
2292int clk_get_phase(struct clk *clk)
2293{
2294 if (!clk)
2295 return 0;
2296
2297 return clk_core_get_phase(clk->core);
2298}
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002299EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquetteb24764902012-03-15 23:11:19 -07002300
2301/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002302 * clk_is_match - check if two clk's point to the same hardware clock
2303 * @p: clk compared against q
2304 * @q: clk compared against p
2305 *
2306 * Returns true if the two struct clk pointers both point to the same hardware
2307 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2308 * share the same struct clk_core object.
2309 *
2310 * Returns false otherwise. Note that two NULL clks are treated as matching.
2311 */
2312bool clk_is_match(const struct clk *p, const struct clk *q)
2313{
2314 /* trivial case: identical struct clk's or both NULL */
2315 if (p == q)
2316 return true;
2317
Geert Uytterhoeven3fe003f2015-10-29 20:55:00 +01002318 /* true if clk->core pointers match. Avoid dereferencing garbage */
Michael Turquette3d3801e2015-02-25 09:11:01 -08002319 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2320 if (p->core == q->core)
2321 return true;
2322
2323 return false;
2324}
2325EXPORT_SYMBOL_GPL(clk_is_match);
2326
Taniya Das63c20c72016-06-15 12:15:01 +05302327int clk_set_flags(struct clk *clk, unsigned long flags)
2328{
2329 if (!clk)
2330 return 0;
2331
2332 if (!clk->core->ops->set_flags)
2333 return -EINVAL;
2334
2335 return clk->core->ops->set_flags(clk->core->hw, flags);
2336}
2337EXPORT_SYMBOL_GPL(clk_set_flags);
2338
Taniya Das08d79242017-04-13 12:24:51 +05302339unsigned long clk_list_frequency(struct clk *clk, unsigned int index)
2340{
2341 int ret = 0;
2342
2343 if (!clk || !clk->core->ops->list_rate)
2344 return -EINVAL;
2345
2346 clk_prepare_lock();
2347 ret = clk->core->ops->list_rate(clk->core->hw, index, ULONG_MAX);
2348 clk_prepare_unlock();
2349
2350 return ret;
2351}
2352EXPORT_SYMBOL_GPL(clk_list_frequency);
2353
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002354/*** debugfs support ***/
2355
2356#ifdef CONFIG_DEBUG_FS
2357#include <linux/debugfs.h>
2358
2359static struct dentry *rootdir;
2360static int inited = 0;
Taniya Das6c15e542016-11-07 10:08:30 +05302361static u32 debug_suspend;
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002362static DEFINE_MUTEX(clk_debug_lock);
2363static HLIST_HEAD(clk_debug_list);
2364
2365static struct hlist_head *all_lists[] = {
2366 &clk_root_list,
2367 &clk_orphan_list,
2368 NULL,
2369};
2370
2371static struct hlist_head *orphan_list[] = {
2372 &clk_orphan_list,
2373 NULL,
2374};
2375
Amit Nischal15aca8c2017-03-06 16:40:58 +05302376static void clk_state_subtree(struct clk_core *c)
2377{
2378 int vdd_level = 0;
2379 struct clk_core *child;
2380
2381 if (!c)
2382 return;
2383
2384 if (c->vdd_class) {
2385 vdd_level = clk_find_vdd_level(c, c->rate);
2386 if (vdd_level < 0)
2387 vdd_level = 0;
2388 }
2389
2390 trace_clk_state(c->name, c->prepare_count, c->enable_count,
2391 c->rate, vdd_level);
2392
2393 hlist_for_each_entry(child, &c->children, child_node)
2394 clk_state_subtree(child);
2395}
2396
2397static int clk_state_show(struct seq_file *s, void *data)
2398{
2399 struct clk_core *c;
2400 struct hlist_head **lists = (struct hlist_head **)s->private;
2401
2402 clk_prepare_lock();
2403
2404 for (; *lists; lists++)
2405 hlist_for_each_entry(c, *lists, child_node)
2406 clk_state_subtree(c);
2407
2408 clk_prepare_unlock();
2409
2410 return 0;
2411}
2412
2413
2414static int clk_state_open(struct inode *inode, struct file *file)
2415{
2416 return single_open(file, clk_state_show, inode->i_private);
2417}
2418
2419static const struct file_operations clk_state_fops = {
2420 .open = clk_state_open,
2421 .read = seq_read,
2422 .llseek = seq_lseek,
2423 .release = single_release,
2424};
2425
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002426static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2427 int level)
2428{
2429 if (!c)
2430 return;
2431
2432 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
2433 level * 3 + 1, "",
2434 30 - level * 3, c->name,
2435 c->enable_count, c->prepare_count, clk_core_get_rate(c),
2436 clk_core_get_accuracy(c), clk_core_get_phase(c));
2437}
2438
2439static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2440 int level)
2441{
2442 struct clk_core *child;
2443
2444 if (!c)
2445 return;
2446
2447 clk_summary_show_one(s, c, level);
2448
2449 hlist_for_each_entry(child, &c->children, child_node)
2450 clk_summary_show_subtree(s, child, level + 1);
2451}
2452
2453static int clk_summary_show(struct seq_file *s, void *data)
2454{
2455 struct clk_core *c;
2456 struct hlist_head **lists = (struct hlist_head **)s->private;
2457
2458 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
2459 seq_puts(s, "----------------------------------------------------------------------------------------\n");
2460
2461 clk_prepare_lock();
2462
2463 for (; *lists; lists++)
2464 hlist_for_each_entry(c, *lists, child_node)
2465 clk_summary_show_subtree(s, c, 0);
2466
2467 clk_prepare_unlock();
2468
2469 return 0;
2470}
2471
2472
2473static int clk_summary_open(struct inode *inode, struct file *file)
2474{
2475 return single_open(file, clk_summary_show, inode->i_private);
2476}
2477
2478static const struct file_operations clk_summary_fops = {
2479 .open = clk_summary_open,
2480 .read = seq_read,
2481 .llseek = seq_lseek,
2482 .release = single_release,
2483};
2484
2485static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2486{
2487 if (!c)
2488 return;
2489
Stefan Wahren7cb81132015-04-29 16:36:43 +00002490 /* This should be JSON format, i.e. elements separated with a comma */
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002491 seq_printf(s, "\"%s\": { ", c->name);
2492 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2493 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Stefan Wahren7cb81132015-04-29 16:36:43 +00002494 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2495 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002496 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2497}
2498
2499static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2500{
2501 struct clk_core *child;
2502
2503 if (!c)
2504 return;
2505
2506 clk_dump_one(s, c, level);
2507
2508 hlist_for_each_entry(child, &c->children, child_node) {
2509 seq_printf(s, ",");
2510 clk_dump_subtree(s, child, level + 1);
2511 }
2512
2513 seq_printf(s, "}");
2514}
2515
2516static int clk_dump(struct seq_file *s, void *data)
2517{
2518 struct clk_core *c;
2519 bool first_node = true;
2520 struct hlist_head **lists = (struct hlist_head **)s->private;
2521
2522 seq_printf(s, "{");
2523
2524 clk_prepare_lock();
2525
2526 for (; *lists; lists++) {
2527 hlist_for_each_entry(c, *lists, child_node) {
2528 if (!first_node)
2529 seq_puts(s, ",");
2530 first_node = false;
2531 clk_dump_subtree(s, c, 0);
2532 }
2533 }
2534
2535 clk_prepare_unlock();
2536
Felipe Balbi70e9f4d2015-05-01 09:48:37 -05002537 seq_puts(s, "}\n");
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002538 return 0;
2539}
2540
2541
2542static int clk_dump_open(struct inode *inode, struct file *file)
2543{
2544 return single_open(file, clk_dump, inode->i_private);
2545}
2546
2547static const struct file_operations clk_dump_fops = {
2548 .open = clk_dump_open,
2549 .read = seq_read,
2550 .llseek = seq_lseek,
2551 .release = single_release,
2552};
2553
Taniya Das2dd25722016-11-14 11:26:02 +05302554static int clock_debug_rate_set(void *data, u64 val)
2555{
2556 struct clk_core *core = data;
2557 int ret;
2558
2559 ret = clk_set_rate(core->hw->clk, val);
2560 if (ret)
2561 pr_err("clk_set_rate(%lu) failed (%d)\n",
2562 (unsigned long)val, ret);
2563
2564 return ret;
2565}
2566
2567static int clock_debug_rate_get(void *data, u64 *val)
2568{
2569 struct clk_core *core = data;
2570
2571 *val = core->hw->core->rate;
2572
2573 return 0;
2574}
2575
2576DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
2577 clock_debug_rate_set, "%llu\n");
2578
2579static ssize_t clock_parent_read(struct file *filp, char __user *ubuf,
2580 size_t cnt, loff_t *ppos)
2581{
2582 char name[256] = {0};
2583 struct clk_core *core = filp->private_data;
2584 struct clk_core *p = core->hw->core->parent;
2585
2586 snprintf(name, sizeof(name), "%s\n", p ? p->name : "None\n");
2587
2588 return simple_read_from_buffer(ubuf, cnt, ppos, name, strlen(name));
2589}
2590
2591static const struct file_operations clock_parent_fops = {
2592 .open = simple_open,
2593 .read = clock_parent_read,
2594};
2595
2596static int clock_debug_enable_set(void *data, u64 val)
2597{
2598 struct clk_core *core = data;
2599 int rc = 0;
2600
2601 if (val)
2602 rc = clk_prepare_enable(core->hw->clk);
2603 else
2604 clk_disable_unprepare(core->hw->clk);
2605
2606 return rc;
2607}
2608
2609static int clock_debug_enable_get(void *data, u64 *val)
2610{
2611 struct clk_core *core = data;
2612 int enabled = 0;
2613
2614 enabled = core->enable_count;
2615
2616 *val = enabled;
2617
2618 return 0;
2619}
2620
2621DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
2622 clock_debug_enable_set, "%lld\n");
2623
2624#define clock_debug_output(m, c, fmt, ...) \
2625do { \
2626 if (m) \
2627 seq_printf(m, fmt, ##__VA_ARGS__); \
2628 else if (c) \
2629 pr_cont(fmt, ##__VA_ARGS__); \
2630 else \
2631 pr_info(fmt, ##__VA_ARGS__); \
2632} while (0)
2633
2634int clock_debug_print_clock(struct clk_core *c, struct seq_file *s)
2635{
2636 char *start = "";
2637 struct clk *clk;
2638
2639 if (!c || !c->prepare_count)
2640 return 0;
2641
2642 clk = c->hw->clk;
2643
2644 clock_debug_output(s, 0, "\t");
2645
2646 do {
Taniya Das876112d2016-11-14 11:54:02 +05302647 if (clk->core->vdd_class)
2648 clock_debug_output(s, 1, "%s%s:%u:%u [%ld, %d]", start,
2649 clk->core->name,
2650 clk->core->prepare_count,
2651 clk->core->enable_count,
2652 clk->core->rate,
2653 clk_find_vdd_level(clk->core, clk->core->rate));
2654 else
2655 clock_debug_output(s, 1, "%s%s:%u:%u [%ld]", start,
Taniya Das2dd25722016-11-14 11:26:02 +05302656 clk->core->name,
2657 clk->core->prepare_count,
2658 clk->core->enable_count,
2659 clk->core->rate);
2660 start = " -> ";
2661 } while ((clk = clk_get_parent(clk)));
2662
2663 clock_debug_output(s, 1, "\n");
2664
2665 return 1;
2666}
2667
2668/*
2669 * clock_debug_print_enabled_clocks() - Print names of enabled clocks
2670 */
2671static void clock_debug_print_enabled_clocks(struct seq_file *s)
2672{
2673 struct clk_core *core;
2674 int cnt = 0;
2675
2676 clock_debug_output(s, 0, "Enabled clocks:\n");
2677
2678 mutex_lock(&clk_debug_lock);
2679
2680 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2681 cnt += clock_debug_print_clock(core, s);
2682
2683 mutex_unlock(&clk_debug_lock);
2684
2685 if (cnt)
2686 clock_debug_output(s, 0, "Enabled clock count: %d\n", cnt);
2687 else
2688 clock_debug_output(s, 0, "No clocks enabled.\n");
2689}
2690
2691static int enabled_clocks_show(struct seq_file *s, void *unused)
2692{
2693 clock_debug_print_enabled_clocks(s);
2694
2695 return 0;
2696}
2697
2698static int enabled_clocks_open(struct inode *inode, struct file *file)
2699{
2700 return single_open(file, enabled_clocks_show, inode->i_private);
2701}
2702
2703static const struct file_operations clk_enabled_list_fops = {
2704 .open = enabled_clocks_open,
2705 .read = seq_read,
2706 .llseek = seq_lseek,
2707 .release = seq_release,
2708};
2709
Taniya Das8436bd72016-11-21 17:50:13 +05302710void clk_debug_print_hw(struct clk_core *clk, struct seq_file *f)
Taniya Das2dd25722016-11-14 11:26:02 +05302711{
2712 if (IS_ERR_OR_NULL(clk))
2713 return;
2714
2715 clk_debug_print_hw(clk->parent, f);
2716
2717 clock_debug_output(f, false, "%s\n", clk->name);
2718
2719 if (!clk->ops->list_registers)
2720 return;
2721
2722 clk->ops->list_registers(f, clk->hw);
2723}
2724
2725static int print_hw_show(struct seq_file *m, void *unused)
2726{
2727 struct clk_core *c = m->private;
2728
2729 clk_debug_print_hw(c, m);
2730
2731 return 0;
2732}
2733
2734static int print_hw_open(struct inode *inode, struct file *file)
2735{
2736 return single_open(file, print_hw_show, inode->i_private);
2737}
2738
2739static const struct file_operations clock_print_hw_fops = {
2740 .open = print_hw_open,
2741 .read = seq_read,
2742 .llseek = seq_lseek,
2743 .release = seq_release,
2744};
2745
Taniya Das876112d2016-11-14 11:54:02 +05302746static int list_rates_show(struct seq_file *s, void *unused)
2747{
2748 struct clk_core *core = s->private;
2749 int level = 0, i = 0;
2750 unsigned long rate, rate_max = 0;
2751
2752 /* Find max frequency supported within voltage constraints. */
2753 if (!core->vdd_class) {
2754 rate_max = ULONG_MAX;
2755 } else {
2756 for (level = 0; level < core->num_rate_max; level++)
2757 if (core->rate_max[level])
2758 rate_max = core->rate_max[level];
2759 }
2760
2761 /*
2762 * List supported frequencies <= rate_max. Higher frequencies may
2763 * appear in the frequency table, but are not valid and should not
2764 * be listed.
2765 */
2766 while (!IS_ERR_VALUE(rate =
2767 core->ops->list_rate(core->hw, i++, rate_max))) {
2768 if (rate <= 0)
2769 break;
2770 if (rate <= rate_max)
2771 seq_printf(s, "%lu\n", rate);
2772 }
2773
2774 return 0;
2775}
2776
2777static int list_rates_open(struct inode *inode, struct file *file)
2778{
2779 return single_open(file, list_rates_show, inode->i_private);
2780}
2781
2782static const struct file_operations list_rates_fops = {
2783 .open = list_rates_open,
2784 .read = seq_read,
2785 .llseek = seq_lseek,
2786 .release = seq_release,
2787};
2788
2789static void clock_print_rate_max_by_level(struct seq_file *s, int level)
2790{
2791 struct clk_core *core = s->private;
2792 struct clk_vdd_class *vdd_class = core->vdd_class;
2793 int off, i, vdd_level, nregs = vdd_class->num_regulators;
2794
2795 vdd_level = clk_find_vdd_level(core, core->rate);
2796
2797 seq_printf(s, "%2s%10lu", vdd_level == level ? "[" : "",
2798 core->rate_max[level]);
2799
2800 for (i = 0; i < nregs; i++) {
2801 off = nregs*level + i;
2802 if (vdd_class->vdd_uv)
2803 seq_printf(s, "%10u", vdd_class->vdd_uv[off]);
2804 }
2805
2806 if (vdd_level == level)
2807 seq_puts(s, "]");
2808
2809 seq_puts(s, "\n");
2810}
2811
2812static int rate_max_show(struct seq_file *s, void *unused)
2813{
2814 struct clk_core *core = s->private;
2815 struct clk_vdd_class *vdd_class = core->vdd_class;
2816 int level = 0, i, nregs = vdd_class->num_regulators;
2817 char reg_name[10];
2818
2819 int vdd_level = clk_find_vdd_level(core, core->rate);
2820
2821 if (vdd_level < 0) {
2822 seq_printf(s, "could not find_vdd_level for %s, %ld\n",
2823 core->name, core->rate);
2824 return 0;
2825 }
2826
2827 seq_printf(s, "%12s", "");
2828 for (i = 0; i < nregs; i++) {
2829 snprintf(reg_name, ARRAY_SIZE(reg_name), "reg %d", i);
2830 seq_printf(s, "%10s", reg_name);
2831 }
2832
2833 seq_printf(s, "\n%12s", "freq");
2834 for (i = 0; i < nregs; i++)
2835 seq_printf(s, "%10s", "uV");
2836
2837 seq_puts(s, "\n");
2838
2839 for (level = 0; level < core->num_rate_max; level++)
2840 clock_print_rate_max_by_level(s, level);
2841
2842 return 0;
2843}
2844
2845static int rate_max_open(struct inode *inode, struct file *file)
2846{
2847 return single_open(file, rate_max_show, inode->i_private);
2848}
2849
2850static const struct file_operations rate_max_fops = {
2851 .open = rate_max_open,
2852 .read = seq_read,
2853 .llseek = seq_lseek,
2854 .release = seq_release,
2855};
2856
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002857static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2858{
2859 struct dentry *d;
2860 int ret = -ENOMEM;
2861
2862 if (!core || !pdentry) {
2863 ret = -EINVAL;
2864 goto out;
2865 }
2866
2867 d = debugfs_create_dir(core->name, pdentry);
2868 if (!d)
2869 goto out;
2870
2871 core->dentry = d;
2872
Taniya Das2dd25722016-11-14 11:26:02 +05302873 d = debugfs_create_file("clk_rate", 0444, core->dentry, core,
2874 &clock_rate_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002875 if (!d)
2876 goto err_out;
2877
Taniya Das876112d2016-11-14 11:54:02 +05302878 if (core->ops->list_rate) {
2879 if (!debugfs_create_file("clk_list_rates",
2880 0444, core->dentry, core, &list_rates_fops))
2881 goto err_out;
2882 }
2883
2884 if (core->vdd_class && !debugfs_create_file("clk_rate_max",
2885 0444, core->dentry, core, &rate_max_fops))
2886 goto err_out;
2887
Taniya Das2dd25722016-11-14 11:26:02 +05302888 d = debugfs_create_u32("clk_accuracy", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002889 (u32 *)&core->accuracy);
2890 if (!d)
2891 goto err_out;
2892
Taniya Das2dd25722016-11-14 11:26:02 +05302893 d = debugfs_create_u32("clk_phase", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002894 (u32 *)&core->phase);
2895 if (!d)
2896 goto err_out;
2897
Taniya Das2dd25722016-11-14 11:26:02 +05302898 d = debugfs_create_x32("clk_flags", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002899 (u32 *)&core->flags);
2900 if (!d)
2901 goto err_out;
2902
Taniya Das2dd25722016-11-14 11:26:02 +05302903 d = debugfs_create_u32("clk_prepare_count", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002904 (u32 *)&core->prepare_count);
2905 if (!d)
2906 goto err_out;
2907
Taniya Das2dd25722016-11-14 11:26:02 +05302908 d = debugfs_create_file("clk_enable_count", 0444, core->dentry,
2909 core, &clock_enable_fops);
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002910 if (!d)
2911 goto err_out;
2912
Taniya Das2dd25722016-11-14 11:26:02 +05302913 d = debugfs_create_u32("clk_notifier_count", 0444, core->dentry,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002914 (u32 *)&core->notifier_count);
2915 if (!d)
2916 goto err_out;
2917
Taniya Das2dd25722016-11-14 11:26:02 +05302918 d = debugfs_create_file("clk_parent", 0444, core->dentry, core,
2919 &clock_parent_fops);
2920 if (!d)
2921 goto err_out;
2922
2923 d = debugfs_create_file("clk_print_regs", 0444, core->dentry,
2924 core, &clock_print_hw_fops);
2925 if (!d)
2926 goto err_out;
2927
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002928 if (core->ops->debug_init) {
2929 ret = core->ops->debug_init(core->hw, core->dentry);
2930 if (ret)
2931 goto err_out;
2932 }
2933
2934 ret = 0;
2935 goto out;
2936
2937err_out:
2938 debugfs_remove_recursive(core->dentry);
2939 core->dentry = NULL;
2940out:
2941 return ret;
2942}
2943
2944/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002945 * clk_debug_register - add a clk node to the debugfs clk directory
2946 * @core: the clk being added to the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002947 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002948 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2949 * initialized. Otherwise it bails out early since the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002950 * will be created lazily by clk_debug_init as part of a late_initcall.
2951 */
2952static int clk_debug_register(struct clk_core *core)
2953{
2954 int ret = 0;
2955
2956 mutex_lock(&clk_debug_lock);
2957 hlist_add_head(&core->debug_node, &clk_debug_list);
2958
2959 if (!inited)
2960 goto unlock;
2961
2962 ret = clk_debug_create_one(core, rootdir);
2963unlock:
2964 mutex_unlock(&clk_debug_lock);
2965
2966 return ret;
2967}
2968
2969 /**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002970 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2971 * @core: the clk being removed from the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002972 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07002973 * Dynamically removes a clk and all its child nodes from the
2974 * debugfs clk directory if clk->dentry points to debugfs created by
Stephen Boyd706d5c72016-02-22 15:43:41 -08002975 * clk_debug_register in __clk_core_init.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07002976 */
2977static void clk_debug_unregister(struct clk_core *core)
2978{
2979 mutex_lock(&clk_debug_lock);
2980 hlist_del_init(&core->debug_node);
2981 debugfs_remove_recursive(core->dentry);
2982 core->dentry = NULL;
2983 mutex_unlock(&clk_debug_lock);
2984}
2985
2986struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2987 void *data, const struct file_operations *fops)
2988{
2989 struct dentry *d = NULL;
2990
2991 if (hw->core->dentry)
2992 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2993 fops);
2994
2995 return d;
2996}
2997EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2998
Taniya Das6c15e542016-11-07 10:08:30 +05302999/*
3000 * Print the names of all enabled clocks and their parents if
3001 * debug_suspend is set from debugfs.
3002 */
3003void clock_debug_print_enabled(void)
3004{
3005 if (likely(!debug_suspend))
3006 return;
3007
3008 clock_debug_print_enabled_clocks(NULL);
3009}
3010EXPORT_SYMBOL_GPL(clock_debug_print_enabled);
3011
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003012/**
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003013 * clk_debug_init - lazily populate the debugfs clk directory
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003014 *
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003015 * clks are often initialized very early during boot before memory can be
3016 * dynamically allocated and well before debugfs is setup. This function
3017 * populates the debugfs clk directory once at boot-time when we know that
3018 * debugfs is setup. It should only be called once at boot-time, all other clks
3019 * added dynamically will be done so with clk_debug_register.
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003020 */
3021static int __init clk_debug_init(void)
3022{
3023 struct clk_core *core;
3024 struct dentry *d;
3025
3026 rootdir = debugfs_create_dir("clk", NULL);
3027
3028 if (!rootdir)
3029 return -ENOMEM;
3030
Taniya Das2dd25722016-11-14 11:26:02 +05303031 d = debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003032 &clk_summary_fops);
3033 if (!d)
3034 return -ENOMEM;
3035
Taniya Das2dd25722016-11-14 11:26:02 +05303036 d = debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003037 &clk_dump_fops);
3038 if (!d)
3039 return -ENOMEM;
3040
Taniya Das2dd25722016-11-14 11:26:02 +05303041 d = debugfs_create_file("clk_orphan_summary", 0444, rootdir,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003042 &orphan_list, &clk_summary_fops);
3043 if (!d)
3044 return -ENOMEM;
3045
Taniya Das2dd25722016-11-14 11:26:02 +05303046 d = debugfs_create_file("clk_orphan_dump", 0444, rootdir,
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003047 &orphan_list, &clk_dump_fops);
3048 if (!d)
3049 return -ENOMEM;
3050
Taniya Das2dd25722016-11-14 11:26:02 +05303051 d = debugfs_create_file("clk_enabled_list", 0444, rootdir,
3052 &clk_debug_list, &clk_enabled_list_fops);
3053 if (!d)
3054 return -ENOMEM;
3055
Taniya Das6c15e542016-11-07 10:08:30 +05303056
3057 d = debugfs_create_u32("debug_suspend", 0644, rootdir, &debug_suspend);
3058 if (!d)
3059 return -ENOMEM;
3060
Amit Nischal15aca8c2017-03-06 16:40:58 +05303061 d = debugfs_create_file("trace_clocks", 0444, rootdir, &all_lists,
3062 &clk_state_fops);
3063 if (!d)
3064 return -ENOMEM;
3065
Stephen Boyd4dff95d2015-04-30 14:43:22 -07003066 mutex_lock(&clk_debug_lock);
3067 hlist_for_each_entry(core, &clk_debug_list, debug_node)
3068 clk_debug_create_one(core, rootdir);
3069
3070 inited = 1;
3071 mutex_unlock(&clk_debug_lock);
3072
3073 return 0;
3074}
3075late_initcall(clk_debug_init);
3076#else
3077static inline int clk_debug_register(struct clk_core *core) { return 0; }
3078static inline void clk_debug_reparent(struct clk_core *core,
3079 struct clk_core *new_parent)
3080{
3081}
3082static inline void clk_debug_unregister(struct clk_core *core)
3083{
3084}
3085#endif
3086
Michael Turquette3d3801e2015-02-25 09:11:01 -08003087/**
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003088 * __clk_core_init - initialize the data structures in a struct clk_core
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003089 * @core: clk_core being initialized
Mike Turquetteb24764902012-03-15 23:11:19 -07003090 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003091 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07003092 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07003093 */
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003094static int __clk_core_init(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07003095{
Mike Turquetted1302a32012-03-29 14:30:40 -07003096 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003097 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003098 struct hlist_node *tmp2;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003099 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003100
Masahiro Yamadad35c80c2015-12-28 19:22:56 +09003101 if (!core)
Mike Turquetted1302a32012-03-29 14:30:40 -07003102 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07003103
Mike Turquetteeab89f62013-03-28 13:59:01 -07003104 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003105
3106 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003107 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003108 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003109 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003110 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07003111 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07003112 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003113
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003114 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003115 if (core->ops->set_rate &&
3116 !((core->ops->round_rate || core->ops->determine_rate) &&
3117 core->ops->recalc_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003118 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3119 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003120 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003121 goto out;
3122 }
3123
Stephen Boydd6968fc2015-04-30 13:54:13 -07003124 if (core->ops->set_parent && !core->ops->get_parent) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003125 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3126 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07003127 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07003128 goto out;
3129 }
3130
Masahiro Yamada3c8e77d2015-12-28 19:23:04 +09003131 if (core->num_parents > 1 && !core->ops->get_parent) {
3132 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3133 __func__, core->name);
3134 ret = -EINVAL;
3135 goto out;
3136 }
3137
Stephen Boydd6968fc2015-04-30 13:54:13 -07003138 if (core->ops->set_rate_and_parent &&
3139 !(core->ops->set_parent && core->ops->set_rate)) {
Masahiro Yamadac44fccb2015-12-28 19:23:03 +09003140 pr_err("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003141 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08003142 ret = -EINVAL;
3143 goto out;
3144 }
3145
Mike Turquetteb24764902012-03-15 23:11:19 -07003146 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003147 for (i = 0; i < core->num_parents; i++)
3148 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07003149 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07003150 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07003151
Stephen Boydd6968fc2015-04-30 13:54:13 -07003152 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07003153
3154 /*
Stephen Boyd706d5c72016-02-22 15:43:41 -08003155 * Populate core->parent if parent has already been clk_core_init'd. If
3156 * parent has not yet been clk_core_init'd then place clk in the orphan
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003157 * list. If clk doesn't have any parents then place it in the root
Mike Turquetteb24764902012-03-15 23:11:19 -07003158 * clk list.
3159 *
3160 * Every time a new clk is clk_init'd then we walk the list of orphan
3161 * clocks and re-parent any that are children of the clock currently
3162 * being clk_init'd.
3163 */
Heiko Stuebnere6500342015-04-22 22:53:05 +02003164 if (core->parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003165 hlist_add_head(&core->child_node,
3166 &core->parent->children);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003167 core->orphan = core->parent->orphan;
Stephen Boyd47b0eeb2016-02-02 17:24:56 -08003168 } else if (!core->num_parents) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003169 hlist_add_head(&core->child_node, &clk_root_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003170 core->orphan = false;
3171 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07003172 hlist_add_head(&core->child_node, &clk_orphan_list);
Heiko Stuebnere6500342015-04-22 22:53:05 +02003173 core->orphan = true;
3174 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003175
3176 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003177 * Set clk's accuracy. The preferred method is to use
3178 * .recalc_accuracy. For simple clocks and lazy developers the default
3179 * fallback is to use the parent's accuracy. If a clock doesn't have a
3180 * parent (or is orphaned) then accuracy is set to zero (perfect
3181 * clock).
3182 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003183 if (core->ops->recalc_accuracy)
3184 core->accuracy = core->ops->recalc_accuracy(core->hw,
3185 __clk_get_accuracy(core->parent));
3186 else if (core->parent)
3187 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003188 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003189 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01003190
3191 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02003192 * Set clk's phase.
3193 * Since a phase is by definition relative to its parent, just
3194 * query the current clock phase, or just assume it's in phase.
3195 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003196 if (core->ops->get_phase)
3197 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02003198 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07003199 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02003200
3201 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07003202 * Set clk's rate. The preferred method is to use .recalc_rate. For
3203 * simple clocks and lazy developers the default fallback is to use the
3204 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3205 * then rate is set to zero.
3206 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003207 if (core->ops->recalc_rate)
3208 rate = core->ops->recalc_rate(core->hw,
3209 clk_core_get_rate_nolock(core->parent));
3210 else if (core->parent)
3211 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003212 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003213 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003214 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07003215
3216 /*
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003217 * walk the list of orphan clocks and reparent any that newly finds a
3218 * parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07003219 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08003220 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003221 struct clk_core *parent = __clk_init_parent(orphan);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01003222
Michael Turquette904e6ea2016-07-08 16:32:10 -07003223 /*
3224 * we could call __clk_set_parent, but that would result in a
3225 * redundant call to the .set_rate op, if it exists
3226 */
3227 if (parent) {
3228 __clk_set_parent_before(orphan, parent);
3229 __clk_set_parent_after(orphan, parent, NULL);
3230 __clk_recalc_accuracies(orphan);
3231 __clk_recalc_rates(orphan, 0);
3232 }
Masahiro Yamada0e8f6e42015-12-28 19:23:07 +09003233 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003234
3235 /*
3236 * optional platform-specific magic
3237 *
3238 * The .init callback is not used by any of the basic clock types, but
3239 * exists for weird hardware that must perform initialization magic.
3240 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02003241 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07003242 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003243 if (core->ops->init)
3244 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07003245
Lee Jones32b9b102016-02-11 13:19:09 -08003246 if (core->flags & CLK_IS_CRITICAL) {
Maxime Ripardef56b792016-05-13 10:00:31 +02003247 unsigned long flags;
3248
Lee Jones32b9b102016-02-11 13:19:09 -08003249 clk_core_prepare(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02003250
3251 flags = clk_enable_lock();
Lee Jones32b9b102016-02-11 13:19:09 -08003252 clk_core_enable(core);
Maxime Ripardef56b792016-05-13 10:00:31 +02003253 clk_enable_unlock(flags);
Lee Jones32b9b102016-02-11 13:19:09 -08003254 }
3255
Michael Turquettee9b8c592017-04-21 12:27:39 +05303256 /*
3257 * enable clocks with the CLK_ENABLE_HAND_OFF flag set
3258 *
3259 * This flag causes the framework to enable the clock at registration
3260 * time, which is sometimes necessary for clocks that would cause a
3261 * system crash when gated (e.g. cpu, memory, etc). The prepare_count
3262 * is migrated over to the first clk consumer to call clk_prepare().
3263 * Similarly the clk's enable_count is migrated to the first consumer
3264 * to call clk_enable().
3265 */
3266 if (core->flags & CLK_ENABLE_HAND_OFF) {
3267 unsigned long flags;
3268
Taniya Das30684a42017-04-21 13:33:20 +05303269 /*
3270 * Few clocks might have hardware gating which would be
3271 * required to be ON before prepare/enabling the clocks. So
3272 * check if the clock has been turned ON earlier and we should
3273 * prepare/enable those clocks.
3274 */
3275 if (clk_core_is_enabled(core)) {
3276 core->need_handoff_prepare = true;
3277 core->need_handoff_enable = true;
3278 ret = clk_core_prepare(core);
3279 if (ret)
3280 goto out;
3281 flags = clk_enable_lock();
3282 clk_core_enable(core);
3283 clk_enable_unlock(flags);
3284 }
Michael Turquettee9b8c592017-04-21 12:27:39 +05303285 }
3286
Stephen Boydd6968fc2015-04-30 13:54:13 -07003287 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07003288out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003289 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003290
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003291 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003292 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08003293
Mike Turquetted1302a32012-03-29 14:30:40 -07003294 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07003295}
3296
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003297struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
3298 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003299{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003300 struct clk *clk;
3301
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003302 /* This is to allow this function to be chained to others */
Masahiro Yamadac1de1352015-11-20 14:38:49 +09003303 if (IS_ERR_OR_NULL(hw))
Masahiro Yamada8a231332016-07-19 16:28:47 +09003304 return ERR_CAST(hw);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003305
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003306 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3307 if (!clk)
3308 return ERR_PTR(-ENOMEM);
3309
3310 clk->core = hw->core;
3311 clk->dev_id = dev_id;
3312 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003313 clk->max_rate = ULONG_MAX;
3314
3315 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08003316 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003317 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003318
3319 return clk;
3320}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003321
Stephen Boyd73e0e492015-02-06 11:42:43 -08003322void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003323{
3324 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08003325 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003326 clk_prepare_unlock();
3327
3328 kfree(clk);
3329}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003330
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003331/**
3332 * clk_register - allocate a new clock, register it and return an opaque cookie
3333 * @dev: device that is registering this clock
3334 * @hw: link to hardware-specific clock data
3335 *
3336 * clk_register is the primary interface for populating the clock tree with new
3337 * clock nodes. It returns a pointer to the newly allocated struct clk which
Shailendra Vermaa59a5162015-05-21 00:06:48 +05303338 * cannot be dereferenced by driver code but may be used in conjunction with the
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003339 * rest of the clock API. In the event of an error clk_register will return an
3340 * error code; drivers must test for an error code after calling clk_register.
3341 */
3342struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07003343{
Mike Turquetted1302a32012-03-29 14:30:40 -07003344 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003345 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003346
Stephen Boydd6968fc2015-04-30 13:54:13 -07003347 core = kzalloc(sizeof(*core), GFP_KERNEL);
3348 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003349 ret = -ENOMEM;
3350 goto fail_out;
3351 }
Mike Turquetteb24764902012-03-15 23:11:19 -07003352
Stephen Boydd6968fc2015-04-30 13:54:13 -07003353 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3354 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003355 ret = -ENOMEM;
3356 goto fail_name;
3357 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07003358 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003359 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003360 core->owner = dev->driver->owner;
3361 core->hw = hw;
3362 core->flags = hw->init->flags;
3363 core->num_parents = hw->init->num_parents;
Stephen Boyd9783c0d2015-07-16 12:50:27 -07003364 core->min_rate = 0;
3365 core->max_rate = ULONG_MAX;
Stephen Boyd5cb05a12016-05-16 11:05:16 +05303366 core->vdd_class = hw->init->vdd_class;
3367 core->rate_max = hw->init->rate_max;
3368 core->num_rate_max = hw->init->num_rate_max;
Stephen Boydd6968fc2015-04-30 13:54:13 -07003369 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07003370
Stephen Boyd5cb05a12016-05-16 11:05:16 +05303371 if (core->vdd_class) {
3372 ret = clk_vdd_class_init(core->vdd_class);
3373 if (ret) {
3374 pr_err("Failed to initialize vdd class\n");
3375 goto fail_parent_names;
3376 }
3377 }
3378
Mike Turquetted1302a32012-03-29 14:30:40 -07003379 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003380 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02003381 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07003382
Stephen Boydd6968fc2015-04-30 13:54:13 -07003383 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003384 ret = -ENOMEM;
3385 goto fail_parent_names;
3386 }
3387
3388
3389 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07003390 for (i = 0; i < core->num_parents; i++) {
3391 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003392 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07003393 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07003394 ret = -ENOMEM;
3395 goto fail_parent_names_copy;
3396 }
3397 }
3398
Masahiro Yamada176d1162015-12-28 19:23:00 +09003399 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3400 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3401 GFP_KERNEL);
3402 if (!core->parents) {
3403 ret = -ENOMEM;
3404 goto fail_parents;
3405 };
3406
Stephen Boydd6968fc2015-04-30 13:54:13 -07003407 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003408
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003409 hw->clk = __clk_create_clk(hw, NULL, NULL);
3410 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003411 ret = PTR_ERR(hw->clk);
Masahiro Yamada176d1162015-12-28 19:23:00 +09003412 goto fail_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003413 }
Mike Turquetted1302a32012-03-29 14:30:40 -07003414
Masahiro Yamadabe45ebf2015-12-28 19:22:57 +09003415 ret = __clk_core_init(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003416 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003417 return hw->clk;
3418
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003419 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003420 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07003421
Masahiro Yamada176d1162015-12-28 19:23:00 +09003422fail_parents:
3423 kfree(core->parents);
Mike Turquetted1302a32012-03-29 14:30:40 -07003424fail_parent_names_copy:
3425 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003426 kfree_const(core->parent_names[i]);
3427 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07003428fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003429 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07003430fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07003431 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07003432fail_out:
3433 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07003434}
3435EXPORT_SYMBOL_GPL(clk_register);
3436
Stephen Boyd41438042016-02-05 17:02:52 -08003437/**
3438 * clk_hw_register - register a clk_hw and return an error code
3439 * @dev: device that is registering this clock
3440 * @hw: link to hardware-specific clock data
3441 *
3442 * clk_hw_register is the primary interface for populating the clock tree with
3443 * new clock nodes. It returns an integer equal to zero indicating success or
3444 * less than zero indicating failure. Drivers must test for an error code after
3445 * calling clk_hw_register().
3446 */
3447int clk_hw_register(struct device *dev, struct clk_hw *hw)
3448{
3449 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3450}
3451EXPORT_SYMBOL_GPL(clk_hw_register);
3452
Stephen Boyd6e5ab412015-04-30 15:11:31 -07003453/* Free memory allocated for a clock. */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003454static void __clk_release(struct kref *ref)
3455{
Stephen Boydd6968fc2015-04-30 13:54:13 -07003456 struct clk_core *core = container_of(ref, struct clk_core, ref);
3457 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003458
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01003459 lockdep_assert_held(&prepare_lock);
3460
Stephen Boydd6968fc2015-04-30 13:54:13 -07003461 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003462 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07003463 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003464
Stephen Boydd6968fc2015-04-30 13:54:13 -07003465 kfree(core->parent_names);
3466 kfree_const(core->name);
3467 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003468}
3469
3470/*
3471 * Empty clk_ops for unregistered clocks. These are used temporarily
3472 * after clk_unregister() was called on a clock and until last clock
3473 * consumer calls clk_put() and the struct clk object is freed.
3474 */
3475static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3476{
3477 return -ENXIO;
3478}
3479
3480static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3481{
3482 WARN_ON_ONCE(1);
3483}
3484
3485static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3486 unsigned long parent_rate)
3487{
3488 return -ENXIO;
3489}
3490
3491static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3492{
3493 return -ENXIO;
3494}
3495
3496static const struct clk_ops clk_nodrv_ops = {
3497 .enable = clk_nodrv_prepare_enable,
3498 .disable = clk_nodrv_disable_unprepare,
3499 .prepare = clk_nodrv_prepare_enable,
3500 .unprepare = clk_nodrv_disable_unprepare,
3501 .set_rate = clk_nodrv_set_rate,
3502 .set_parent = clk_nodrv_set_parent,
3503};
3504
Mark Brown1df5c932012-04-18 09:07:12 +01003505/**
3506 * clk_unregister - unregister a currently registered clock
3507 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01003508 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003509void clk_unregister(struct clk *clk)
3510{
3511 unsigned long flags;
3512
Stephen Boyd6314b672014-09-04 23:37:49 -07003513 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3514 return;
3515
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003516 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003517
3518 clk_prepare_lock();
3519
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003520 if (clk->core->ops == &clk_nodrv_ops) {
3521 pr_err("%s: unregistered clock: %s\n", __func__,
3522 clk->core->name);
Insu Yun4106a3d2016-01-30 10:12:04 -05003523 goto unlock;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003524 }
3525 /*
3526 * Assign empty clock ops for consumers that might still hold
3527 * a reference to this clock.
3528 */
3529 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003530 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003531 clk_enable_unlock(flags);
3532
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003533 if (!hlist_empty(&clk->core->children)) {
3534 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07003535 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003536
3537 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003538 hlist_for_each_entry_safe(child, t, &clk->core->children,
3539 child_node)
3540 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003541 }
3542
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003543 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003544
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003545 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003546 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003547 __func__, clk->core->name);
3548 kref_put(&clk->core->ref, __clk_release);
Insu Yun4106a3d2016-01-30 10:12:04 -05003549unlock:
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003550 clk_prepare_unlock();
3551}
Mark Brown1df5c932012-04-18 09:07:12 +01003552EXPORT_SYMBOL_GPL(clk_unregister);
3553
Stephen Boyd41438042016-02-05 17:02:52 -08003554/**
3555 * clk_hw_unregister - unregister a currently registered clk_hw
3556 * @hw: hardware-specific clock data to unregister
3557 */
3558void clk_hw_unregister(struct clk_hw *hw)
3559{
3560 clk_unregister(hw->clk);
3561}
3562EXPORT_SYMBOL_GPL(clk_hw_unregister);
3563
Stephen Boyd46c87732012-09-24 13:38:04 -07003564static void devm_clk_release(struct device *dev, void *res)
3565{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003566 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07003567}
3568
Stephen Boyd41438042016-02-05 17:02:52 -08003569static void devm_clk_hw_release(struct device *dev, void *res)
3570{
3571 clk_hw_unregister(*(struct clk_hw **)res);
3572}
3573
Deepak Katragadda677bad12016-11-04 13:33:13 -07003574#define MAX_LEN_OPP_HANDLE 50
3575#define LEN_OPP_HANDLE 16
3576
3577static int derive_device_list(struct device **device_list,
3578 struct clk_core *core,
3579 struct device_node *np,
3580 char *clk_handle_name, int count)
3581{
3582 int j;
3583 struct platform_device *pdev;
3584 struct device_node *dev_node;
3585
3586 for (j = 0; j < count; j++) {
3587 device_list[j] = NULL;
3588 dev_node = of_parse_phandle(np, clk_handle_name, j);
3589 if (!dev_node) {
3590 pr_err("Unable to get device_node pointer for %s opp-handle (%s)\n",
3591 core->name, clk_handle_name);
3592 return -ENODEV;
3593 }
3594
3595 pdev = of_find_device_by_node(dev_node);
3596 if (!pdev) {
3597 pr_err("Unable to find platform_device node for %s opp-handle\n",
3598 core->name);
3599 return -ENODEV;
3600 }
3601 device_list[j] = &pdev->dev;
3602 }
3603 return 0;
3604}
3605
3606static int clk_get_voltage(struct clk_core *core, unsigned long rate, int n)
3607{
3608 struct clk_vdd_class *vdd;
3609 int level, corner;
3610
3611 /* Use the first regulator in the vdd class for the OPP table. */
3612 vdd = core->vdd_class;
3613 if (vdd->num_regulators > 1) {
3614 corner = vdd->vdd_uv[vdd->num_regulators * n];
3615 } else {
3616 level = clk_find_vdd_level(core, rate);
3617 if (level < 0) {
3618 pr_err("Could not find vdd level\n");
3619 return -EINVAL;
3620 }
3621 corner = vdd->vdd_uv[level];
3622 }
3623
3624 if (!corner) {
3625 pr_err("%s: Unable to find vdd level for rate %lu\n",
3626 core->name, rate);
3627 return -EINVAL;
3628 }
3629
3630 return corner;
3631}
3632
3633static int clk_add_and_print_opp(struct clk_hw *hw,
3634 struct device **device_list, int count,
3635 unsigned long rate, int uv, int n)
3636{
3637 struct clk_core *core = hw->core;
3638 int j, ret = 0;
3639
3640 for (j = 0; j < count; j++) {
3641 ret = dev_pm_opp_add(device_list[j], rate, uv);
3642 if (ret) {
3643 pr_err("%s: couldn't add OPP for %lu - err: %d\n",
3644 core->name, rate, ret);
3645 return ret;
3646 }
3647
3648 if (n == 0 || n == core->num_rate_max - 1 ||
3649 rate == clk_hw_round_rate(hw, INT_MAX))
3650 pr_info("%s: set OPP pair(%lu Hz: %u uV) on %s\n",
3651 core->name, rate, uv,
3652 dev_name(device_list[j]));
3653 }
3654 return ret;
3655}
3656
3657static void clk_populate_clock_opp_table(struct device_node *np,
3658 struct clk_hw *hw)
3659{
3660 struct device **device_list;
3661 struct clk_core *core = hw->core;
3662 char clk_handle_name[MAX_LEN_OPP_HANDLE];
3663 int n, len, count, uv;
3664 unsigned long rate = 0, ret = 0;
3665
3666 if (!core || !core->num_rate_max)
3667 return;
3668
3669 if (strlen(core->name) + LEN_OPP_HANDLE < MAX_LEN_OPP_HANDLE) {
3670 ret = snprintf(clk_handle_name, ARRAY_SIZE(clk_handle_name),
3671 "qcom,%s-opp-handle", core->name);
3672 if (ret < strlen(core->name) + LEN_OPP_HANDLE) {
3673 pr_err("%s: Failed to hold clk_handle_name\n",
3674 core->name);
3675 return;
3676 }
3677 } else {
3678 pr_err("clk name (%s) too large to fit in clk_handle_name\n",
3679 core->name);
3680 return;
3681 }
3682
3683 if (of_find_property(np, clk_handle_name, &len)) {
3684 count = len/sizeof(u32);
3685
3686 device_list = kmalloc_array(count, sizeof(struct device *),
3687 GFP_KERNEL);
3688 if (!device_list)
3689 return;
3690
3691 ret = derive_device_list(device_list, core, np,
3692 clk_handle_name, count);
3693 if (ret < 0) {
3694 pr_err("Failed to fill device_list for %s\n",
3695 clk_handle_name);
3696 goto err_derive_device_list;
3697 }
3698 } else {
3699 pr_debug("Unable to find %s\n", clk_handle_name);
3700 return;
3701 }
3702
3703 for (n = 0; ; n++) {
3704 ret = clk_hw_round_rate(hw, rate + 1);
3705 if (ret < 0) {
3706 pr_err("clk_round_rate failed for %s\n",
3707 core->name);
3708 goto err_derive_device_list;
3709 }
3710
3711 /*
3712 * If clk_hw_round_rate gives the same value on consecutive
3713 * iterations, exit the loop since we're at the maximum clock
3714 * frequency.
3715 */
3716 if (rate == ret)
3717 break;
3718 rate = ret;
3719
3720 uv = clk_get_voltage(core, rate, n);
3721 if (uv < 0)
3722 goto err_derive_device_list;
3723
3724 ret = clk_add_and_print_opp(hw, device_list, count,
3725 rate, uv, n);
3726 if (ret)
3727 goto err_derive_device_list;
3728 }
3729
3730err_derive_device_list:
3731 kfree(device_list);
3732}
3733
Stephen Boyd46c87732012-09-24 13:38:04 -07003734/**
3735 * devm_clk_register - resource managed clk_register()
3736 * @dev: device that is registering this clock
3737 * @hw: link to hardware-specific clock data
3738 *
3739 * Managed clk_register(). Clocks returned from this function are
3740 * automatically clk_unregister()ed on driver detach. See clk_register() for
3741 * more information.
3742 */
3743struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3744{
3745 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003746 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07003747
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003748 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3749 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07003750 return ERR_PTR(-ENOMEM);
3751
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003752 clk = clk_register(dev, hw);
3753 if (!IS_ERR(clk)) {
3754 *clkp = clk;
3755 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003756 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07003757 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07003758 }
3759
Deepak Katragadda677bad12016-11-04 13:33:13 -07003760 clk_populate_clock_opp_table(dev->of_node, hw);
Stephen Boyd46c87732012-09-24 13:38:04 -07003761 return clk;
3762}
3763EXPORT_SYMBOL_GPL(devm_clk_register);
3764
Stephen Boyd41438042016-02-05 17:02:52 -08003765/**
3766 * devm_clk_hw_register - resource managed clk_hw_register()
3767 * @dev: device that is registering this clock
3768 * @hw: link to hardware-specific clock data
3769 *
Masahiro Yamadac47265a2016-05-01 19:56:08 +09003770 * Managed clk_hw_register(). Clocks registered by this function are
Stephen Boyd41438042016-02-05 17:02:52 -08003771 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3772 * for more information.
3773 */
3774int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3775{
3776 struct clk_hw **hwp;
3777 int ret;
3778
3779 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3780 if (!hwp)
3781 return -ENOMEM;
3782
3783 ret = clk_hw_register(dev, hw);
3784 if (!ret) {
3785 *hwp = hw;
3786 devres_add(dev, hwp);
3787 } else {
3788 devres_free(hwp);
3789 }
3790
Deepak Katragadda677bad12016-11-04 13:33:13 -07003791 clk_populate_clock_opp_table(dev->of_node, hw);
Stephen Boyd41438042016-02-05 17:02:52 -08003792 return ret;
3793}
3794EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3795
Stephen Boyd46c87732012-09-24 13:38:04 -07003796static int devm_clk_match(struct device *dev, void *res, void *data)
3797{
3798 struct clk *c = res;
3799 if (WARN_ON(!c))
3800 return 0;
3801 return c == data;
3802}
3803
Stephen Boyd41438042016-02-05 17:02:52 -08003804static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3805{
3806 struct clk_hw *hw = res;
3807
3808 if (WARN_ON(!hw))
3809 return 0;
3810 return hw == data;
3811}
3812
Stephen Boyd46c87732012-09-24 13:38:04 -07003813/**
3814 * devm_clk_unregister - resource managed clk_unregister()
3815 * @clk: clock to unregister
3816 *
3817 * Deallocate a clock allocated with devm_clk_register(). Normally
3818 * this function will not need to be called and the resource management
3819 * code will ensure that the resource is freed.
3820 */
3821void devm_clk_unregister(struct device *dev, struct clk *clk)
3822{
3823 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3824}
3825EXPORT_SYMBOL_GPL(devm_clk_unregister);
3826
Stephen Boyd41438042016-02-05 17:02:52 -08003827/**
3828 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3829 * @dev: device that is unregistering the hardware-specific clock data
3830 * @hw: link to hardware-specific clock data
3831 *
3832 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3833 * this function will not need to be called and the resource management
3834 * code will ensure that the resource is freed.
3835 */
3836void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3837{
3838 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3839 hw));
3840}
3841EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3842
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003843/*
3844 * clkdev helpers
3845 */
3846int __clk_get(struct clk *clk)
3847{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003848 struct clk_core *core = !clk ? NULL : clk->core;
3849
3850 if (core) {
3851 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003852 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003853
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003854 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003855 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003856 return 1;
3857}
3858
3859void __clk_put(struct clk *clk)
3860{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003861 struct module *owner;
3862
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01003863 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003864 return;
3865
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003866 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003867
Stephen Boyd50595f82015-02-06 11:42:44 -08003868 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01003869 if (clk->min_rate > clk->core->req_rate ||
3870 clk->max_rate < clk->core->req_rate)
3871 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3872
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003873 owner = clk->core->owner;
3874 kref_put(&clk->core->ref, __clk_release);
3875
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02003876 clk_prepare_unlock();
3877
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01003878 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01003879
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003880 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02003881}
3882
Mike Turquetteb24764902012-03-15 23:11:19 -07003883/*** clk rate change notifiers ***/
3884
3885/**
3886 * clk_notifier_register - add a clk rate change notifier
3887 * @clk: struct clk * to watch
3888 * @nb: struct notifier_block * with callback info
3889 *
3890 * Request notification when clk's rate changes. This uses an SRCU
3891 * notifier because we want it to block and notifier unregistrations are
3892 * uncommon. The callbacks associated with the notifier must not
3893 * re-enter into the clk framework by calling any top-level clk APIs;
3894 * this will cause a nested prepare_lock mutex.
3895 *
Masahiro Yamada198bb592015-11-30 16:40:51 +09003896 * In all notification cases (pre, post and abort rate change) the original
3897 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3898 * and the new frequency is passed via struct clk_notifier_data.new_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07003899 *
Mike Turquetteb24764902012-03-15 23:11:19 -07003900 * clk_notifier_register() must be called from non-atomic context.
3901 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3902 * allocation failure; otherwise, passes along the return value of
3903 * srcu_notifier_chain_register().
3904 */
3905int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3906{
3907 struct clk_notifier *cn;
3908 int ret = -ENOMEM;
3909
3910 if (!clk || !nb)
3911 return -EINVAL;
3912
Mike Turquetteeab89f62013-03-28 13:59:01 -07003913 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003914
3915 /* search the list of notifiers for this clk */
3916 list_for_each_entry(cn, &clk_notifier_list, node)
3917 if (cn->clk == clk)
3918 break;
3919
3920 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3921 if (cn->clk != clk) {
3922 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
3923 if (!cn)
3924 goto out;
3925
3926 cn->clk = clk;
3927 srcu_init_notifier_head(&cn->notifier_head);
3928
3929 list_add(&cn->node, &clk_notifier_list);
3930 }
3931
3932 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3933
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003934 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07003935
3936out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07003937 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003938
3939 return ret;
3940}
3941EXPORT_SYMBOL_GPL(clk_notifier_register);
3942
3943/**
3944 * clk_notifier_unregister - remove a clk rate change notifier
3945 * @clk: struct clk *
3946 * @nb: struct notifier_block * with callback info
3947 *
3948 * Request no further notification for changes to 'clk' and frees memory
3949 * allocated in clk_notifier_register.
3950 *
3951 * Returns -EINVAL if called with null arguments; otherwise, passes
3952 * along the return value of srcu_notifier_chain_unregister().
3953 */
3954int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3955{
3956 struct clk_notifier *cn = NULL;
3957 int ret = -EINVAL;
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 list_for_each_entry(cn, &clk_notifier_list, node)
3965 if (cn->clk == clk)
3966 break;
3967
3968 if (cn->clk == clk) {
3969 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3970
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01003971 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07003972
3973 /* XXX the notifier code should handle this better */
3974 if (!cn->notifier_head.head) {
3975 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08003976 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07003977 kfree(cn);
3978 }
3979
3980 } else {
3981 ret = -ENOENT;
3982 }
3983
Mike Turquetteeab89f62013-03-28 13:59:01 -07003984 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07003985
3986 return ret;
3987}
3988EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05003989
3990#ifdef CONFIG_OF
3991/**
3992 * struct of_clk_provider - Clock provider registration structure
3993 * @link: Entry in global list of clock providers
3994 * @node: Pointer to device tree node of clock provider
3995 * @get: Get clock callback. Returns NULL or a struct clk for the
3996 * given clock specifier
3997 * @data: context pointer to be passed into @get callback
3998 */
3999struct of_clk_provider {
4000 struct list_head link;
4001
4002 struct device_node *node;
4003 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004004 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
Grant Likely766e6a42012-04-09 14:50:06 -05004005 void *data;
4006};
4007
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304008static const struct of_device_id __clk_of_table_sentinel
4009 __used __section(__clk_of_table_end);
4010
Grant Likely766e6a42012-04-09 14:50:06 -05004011static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004012static DEFINE_MUTEX(of_clk_mutex);
4013
Grant Likely766e6a42012-04-09 14:50:06 -05004014struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
4015 void *data)
4016{
4017 return data;
4018}
4019EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
4020
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004021struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
4022{
4023 return data;
4024}
4025EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
4026
Shawn Guo494bfec2012-08-22 21:36:27 +08004027struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
4028{
4029 struct clk_onecell_data *clk_data = data;
4030 unsigned int idx = clkspec->args[0];
4031
4032 if (idx >= clk_data->clk_num) {
Geert Uytterhoeven7e963532015-10-16 17:12:32 +02004033 pr_err("%s: invalid clock index %u\n", __func__, idx);
Shawn Guo494bfec2012-08-22 21:36:27 +08004034 return ERR_PTR(-EINVAL);
4035 }
4036
4037 return clk_data->clks[idx];
4038}
4039EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
4040
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004041struct clk_hw *
4042of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
4043{
4044 struct clk_hw_onecell_data *hw_data = data;
4045 unsigned int idx = clkspec->args[0];
4046
4047 if (idx >= hw_data->num) {
4048 pr_err("%s: invalid index %u\n", __func__, idx);
4049 return ERR_PTR(-EINVAL);
4050 }
4051
4052 return hw_data->hws[idx];
4053}
4054EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
4055
Grant Likely766e6a42012-04-09 14:50:06 -05004056/**
4057 * of_clk_add_provider() - Register a clock provider for a node
4058 * @np: Device node pointer associated with clock provider
4059 * @clk_src_get: callback for decoding clock
4060 * @data: context pointer for @clk_src_get callback.
4061 */
4062int of_clk_add_provider(struct device_node *np,
4063 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
4064 void *data),
4065 void *data)
4066{
4067 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004068 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004069
4070 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
4071 if (!cp)
4072 return -ENOMEM;
4073
4074 cp->node = of_node_get(np);
4075 cp->data = data;
4076 cp->get = clk_src_get;
4077
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004078 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004079 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004080 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004081 pr_debug("Added clock from %s\n", np->full_name);
4082
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004083 ret = of_clk_set_defaults(np, true);
4084 if (ret < 0)
4085 of_clk_del_provider(np);
4086
4087 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05004088}
4089EXPORT_SYMBOL_GPL(of_clk_add_provider);
4090
4091/**
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004092 * of_clk_add_hw_provider() - Register a clock provider for a node
4093 * @np: Device node pointer associated with clock provider
4094 * @get: callback for decoding clk_hw
4095 * @data: context pointer for @get callback.
4096 */
4097int of_clk_add_hw_provider(struct device_node *np,
4098 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4099 void *data),
4100 void *data)
4101{
4102 struct of_clk_provider *cp;
4103 int ret;
4104
4105 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4106 if (!cp)
4107 return -ENOMEM;
4108
4109 cp->node = of_node_get(np);
4110 cp->data = data;
4111 cp->get_hw = get;
4112
4113 mutex_lock(&of_clk_mutex);
4114 list_add(&cp->link, &of_clk_providers);
4115 mutex_unlock(&of_clk_mutex);
4116 pr_debug("Added clk_hw provider from %s\n", np->full_name);
4117
4118 ret = of_clk_set_defaults(np, true);
4119 if (ret < 0)
4120 of_clk_del_provider(np);
4121
4122 return ret;
4123}
4124EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
4125
4126/**
Grant Likely766e6a42012-04-09 14:50:06 -05004127 * of_clk_del_provider() - Remove a previously registered clock provider
4128 * @np: Device node pointer associated with clock provider
4129 */
4130void of_clk_del_provider(struct device_node *np)
4131{
4132 struct of_clk_provider *cp;
4133
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004134 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004135 list_for_each_entry(cp, &of_clk_providers, link) {
4136 if (cp->node == np) {
4137 list_del(&cp->link);
4138 of_node_put(cp->node);
4139 kfree(cp);
4140 break;
4141 }
4142 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004143 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004144}
4145EXPORT_SYMBOL_GPL(of_clk_del_provider);
4146
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004147static struct clk_hw *
4148__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4149 struct of_phandle_args *clkspec)
4150{
4151 struct clk *clk;
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004152
Stephen Boyd74002fc2016-08-25 13:35:36 -07004153 if (provider->get_hw)
4154 return provider->get_hw(clkspec, provider->data);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004155
Stephen Boyd74002fc2016-08-25 13:35:36 -07004156 clk = provider->get(clkspec, provider->data);
4157 if (IS_ERR(clk))
4158 return ERR_CAST(clk);
4159 return __clk_get_hw(clk);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004160}
4161
Stephen Boyd73e0e492015-02-06 11:42:43 -08004162struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
4163 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05004164{
4165 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01004166 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Stephen Boydf155d152016-08-15 14:32:23 -07004167 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -05004168
Stephen Boyd306c3422015-02-05 15:39:11 -08004169 if (!clkspec)
4170 return ERR_PTR(-EINVAL);
4171
Grant Likely766e6a42012-04-09 14:50:06 -05004172 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08004173 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05004174 list_for_each_entry(provider, &of_clk_providers, link) {
Stephen Boydf155d152016-08-15 14:32:23 -07004175 if (provider->node == clkspec->np) {
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004176 hw = __of_clk_get_hw_from_provider(provider, clkspec);
Stephen Boyd0861e5b2016-02-05 17:38:26 -08004177 clk = __clk_create_clk(hw, dev_id, con_id);
Stephen Boydf155d152016-08-15 14:32:23 -07004178 }
Stephen Boyd73e0e492015-02-06 11:42:43 -08004179
Stephen Boydf155d152016-08-15 14:32:23 -07004180 if (!IS_ERR(clk)) {
4181 if (!__clk_get(clk)) {
Stephen Boyd73e0e492015-02-06 11:42:43 -08004182 __clk_free_clk(clk);
4183 clk = ERR_PTR(-ENOENT);
4184 }
4185
Grant Likely766e6a42012-04-09 14:50:06 -05004186 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08004187 }
Grant Likely766e6a42012-04-09 14:50:06 -05004188 }
Stephen Boyd306c3422015-02-05 15:39:11 -08004189 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004190
4191 return clk;
4192}
4193
Stephen Boyd306c3422015-02-05 15:39:11 -08004194/**
4195 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4196 * @clkspec: pointer to a clock specifier data structure
4197 *
4198 * This function looks up a struct clk from the registered list of clock
4199 * providers, an input is a clock specifier data structure as returned
4200 * from the of_parse_phandle_with_args() function call.
4201 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02004202struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4203{
Stephen Boyd306c3422015-02-05 15:39:11 -08004204 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05004205}
Andrew F. Davisfb4dd222016-02-12 12:50:16 -06004206EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
Grant Likely766e6a42012-04-09 14:50:06 -05004207
Stephen Boyd929e7f32016-02-19 15:52:32 -08004208/**
4209 * of_clk_get_parent_count() - Count the number of clocks a device node has
4210 * @np: device node to count
4211 *
4212 * Returns: The number of clocks that are possible parents of this node
4213 */
4214unsigned int of_clk_get_parent_count(struct device_node *np)
Mike Turquettef6102742013-10-07 23:12:13 -07004215{
Stephen Boyd929e7f32016-02-19 15:52:32 -08004216 int count;
4217
4218 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4219 if (count < 0)
4220 return 0;
4221
4222 return count;
Mike Turquettef6102742013-10-07 23:12:13 -07004223}
4224EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4225
Grant Likely766e6a42012-04-09 14:50:06 -05004226const char *of_clk_get_parent_name(struct device_node *np, int index)
4227{
4228 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004229 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05004230 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004231 const __be32 *vp;
4232 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05004233 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004234 int count;
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004235 struct clk *clk;
Grant Likely766e6a42012-04-09 14:50:06 -05004236
Grant Likely766e6a42012-04-09 14:50:06 -05004237 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4238 &clkspec);
4239 if (rc)
4240 return NULL;
4241
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004242 index = clkspec.args_count ? clkspec.args[0] : 0;
4243 count = 0;
4244
4245 /* if there is an indices property, use it to transfer the index
4246 * specified into an array offset for the clock-output-names property.
4247 */
4248 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4249 if (index == pv) {
4250 index = count;
4251 break;
4252 }
4253 count++;
4254 }
Masahiro Yamada8da411c2015-12-03 11:20:35 +09004255 /* We went off the end of 'clock-indices' without finding it */
4256 if (prop && !vp)
4257 return NULL;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004258
Grant Likely766e6a42012-04-09 14:50:06 -05004259 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00004260 index,
Stephen Boyd0a4807c2015-10-14 14:03:07 -07004261 &clk_name) < 0) {
4262 /*
4263 * Best effort to get the name if the clock has been
4264 * registered with the framework. If the clock isn't
4265 * registered, we return the node name as the name of
4266 * the clock as long as #clock-cells = 0.
4267 */
4268 clk = of_clk_get_from_provider(&clkspec);
4269 if (IS_ERR(clk)) {
4270 if (clkspec.args_count == 0)
4271 clk_name = clkspec.np->name;
4272 else
4273 clk_name = NULL;
4274 } else {
4275 clk_name = __clk_get_name(clk);
4276 clk_put(clk);
4277 }
4278 }
4279
Grant Likely766e6a42012-04-09 14:50:06 -05004280
4281 of_node_put(clkspec.np);
4282 return clk_name;
4283}
4284EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4285
Dinh Nguyen2e61dfb2015-06-05 11:26:13 -05004286/**
4287 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4288 * number of parents
4289 * @np: Device node pointer associated with clock provider
4290 * @parents: pointer to char array that hold the parents' names
4291 * @size: size of the @parents array
4292 *
4293 * Return: number of parents for the clock node.
4294 */
4295int of_clk_parent_fill(struct device_node *np, const char **parents,
4296 unsigned int size)
4297{
4298 unsigned int i = 0;
4299
4300 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4301 i++;
4302
4303 return i;
4304}
4305EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4306
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004307struct clock_provider {
4308 of_clk_init_cb_t clk_init_cb;
4309 struct device_node *np;
4310 struct list_head node;
4311};
4312
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004313/*
4314 * This function looks for a parent clock. If there is one, then it
4315 * checks that the provider for this parent clock was initialized, in
4316 * this case the parent clock will be ready.
4317 */
4318static int parent_ready(struct device_node *np)
4319{
4320 int i = 0;
4321
4322 while (true) {
4323 struct clk *clk = of_clk_get(np, i);
4324
4325 /* this parent is ready we can check the next one */
4326 if (!IS_ERR(clk)) {
4327 clk_put(clk);
4328 i++;
4329 continue;
4330 }
4331
4332 /* at least one parent is not ready, we exit now */
4333 if (PTR_ERR(clk) == -EPROBE_DEFER)
4334 return 0;
4335
4336 /*
4337 * Here we make assumption that the device tree is
4338 * written correctly. So an error means that there is
4339 * no more parent. As we didn't exit yet, then the
4340 * previous parent are ready. If there is no clock
4341 * parent, no need to wait for them, then we can
4342 * consider their absence as being ready
4343 */
4344 return 1;
4345 }
4346}
4347
Grant Likely766e6a42012-04-09 14:50:06 -05004348/**
Lee Jonesd56f8992016-02-11 13:19:11 -08004349 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4350 * @np: Device node pointer associated with clock provider
4351 * @index: clock index
4352 * @flags: pointer to clk_core->flags
4353 *
4354 * Detects if the clock-critical property exists and, if so, sets the
4355 * corresponding CLK_IS_CRITICAL flag.
4356 *
4357 * Do not use this function. It exists only for legacy Device Tree
4358 * bindings, such as the one-clock-per-node style that are outdated.
4359 * Those bindings typically put all clock data into .dts and the Linux
4360 * driver has no clock data, thus making it impossible to set this flag
4361 * correctly from the driver. Only those drivers may call
4362 * of_clk_detect_critical from their setup functions.
4363 *
4364 * Return: error code or zero on success
4365 */
4366int of_clk_detect_critical(struct device_node *np,
4367 int index, unsigned long *flags)
4368{
4369 struct property *prop;
4370 const __be32 *cur;
4371 uint32_t idx;
4372
4373 if (!np || !flags)
4374 return -EINVAL;
4375
4376 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4377 if (index == idx)
4378 *flags |= CLK_IS_CRITICAL;
4379
4380 return 0;
4381}
4382
4383/**
Grant Likely766e6a42012-04-09 14:50:06 -05004384 * of_clk_init() - Scan and init clock providers from the DT
4385 * @matches: array of compatible values and init functions for providers.
4386 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004387 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01004388 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004389 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05004390 */
4391void __init of_clk_init(const struct of_device_id *matches)
4392{
Alex Elder7f7ed582013-08-22 11:31:31 -05004393 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05004394 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004395 struct clock_provider *clk_provider, *next;
4396 bool is_init_done;
4397 bool force = false;
Stephen Boyd2573a022015-07-06 16:50:00 -07004398 LIST_HEAD(clk_provider_list);
Grant Likely766e6a42012-04-09 14:50:06 -05004399
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304400 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03004401 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05304402
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004403 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05004404 for_each_matching_node_and_match(np, matches, &match) {
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004405 struct clock_provider *parent;
4406
Geert Uytterhoeven3e5dd6f2016-02-26 16:54:31 +01004407 if (!of_device_is_available(np))
4408 continue;
4409
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004410 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4411 if (!parent) {
4412 list_for_each_entry_safe(clk_provider, next,
4413 &clk_provider_list, node) {
4414 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004415 of_node_put(clk_provider->np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004416 kfree(clk_provider);
4417 }
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004418 of_node_put(np);
Stephen Boyd2e3b19f2015-07-06 16:48:19 -07004419 return;
4420 }
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004421
4422 parent->clk_init_cb = match->data;
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004423 parent->np = of_node_get(np);
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01004424 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004425 }
4426
4427 while (!list_empty(&clk_provider_list)) {
4428 is_init_done = false;
4429 list_for_each_entry_safe(clk_provider, next,
4430 &clk_provider_list, node) {
4431 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004432
Ricardo Ribalda Delgado989eafd2016-07-05 18:23:32 +02004433 /* Don't populate platform devices */
4434 of_node_set_flag(clk_provider->np,
4435 OF_POPULATED);
4436
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004437 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02004438 of_clk_set_defaults(clk_provider->np, true);
4439
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004440 list_del(&clk_provider->node);
Julia Lawall6bc9d9d2015-10-21 22:41:36 +02004441 of_node_put(clk_provider->np);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004442 kfree(clk_provider);
4443 is_init_done = true;
4444 }
4445 }
4446
4447 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01004448 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01004449 * remaining providers during the last loop, so now we
4450 * initialize all the remaining ones unconditionally
4451 * in case the clock parent was not mandatory
4452 */
4453 if (!is_init_done)
4454 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05004455 }
4456}
4457#endif