blob: 3e58b74530767c1530d819c4b8549ff3eeb618e1 [file] [log] [blame]
Mike Turquetteb24764902012-03-15 23:11:19 -07001/*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Standard functionality for the common clock API. See Documentation/clk.txt
10 */
11
Michael Turquetteb09d6d92015-01-29 14:22:50 -080012#include <linux/clk-provider.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020013#include <linux/clk/clk-conf.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070014#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/spinlock.h>
17#include <linux/err.h>
18#include <linux/list.h>
19#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050020#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070021#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053022#include <linux/init.h>
Mike Turquette533ddeb2013-03-28 13:59:02 -070023#include <linux/sched.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070024
Sylwester Nawrockid6782c22013-08-23 17:03:43 +020025#include "clk.h"
26
Mike Turquetteb24764902012-03-15 23:11:19 -070027static DEFINE_SPINLOCK(enable_lock);
28static DEFINE_MUTEX(prepare_lock);
29
Mike Turquette533ddeb2013-03-28 13:59:02 -070030static struct task_struct *prepare_owner;
31static struct task_struct *enable_owner;
32
33static int prepare_refcnt;
34static int enable_refcnt;
35
Mike Turquetteb24764902012-03-15 23:11:19 -070036static HLIST_HEAD(clk_root_list);
37static HLIST_HEAD(clk_orphan_list);
38static LIST_HEAD(clk_notifier_list);
39
Stephen Boydd6968fc2015-04-30 13:54:13 -070040static long clk_core_get_accuracy(struct clk_core *core);
41static unsigned long clk_core_get_rate(struct clk_core *core);
42static int clk_core_get_phase(struct clk_core *core);
43static bool clk_core_is_prepared(struct clk_core *core);
44static bool clk_core_is_enabled(struct clk_core *core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010045static struct clk_core *clk_core_lookup(const char *name);
46
Michael Turquetteb09d6d92015-01-29 14:22:50 -080047/*** private data structures ***/
48
49struct clk_core {
50 const char *name;
51 const struct clk_ops *ops;
52 struct clk_hw *hw;
53 struct module *owner;
54 struct clk_core *parent;
55 const char **parent_names;
56 struct clk_core **parents;
57 u8 num_parents;
58 u8 new_parent_index;
59 unsigned long rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010060 unsigned long req_rate;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080061 unsigned long new_rate;
62 struct clk_core *new_parent;
63 struct clk_core *new_child;
64 unsigned long flags;
65 unsigned int enable_count;
66 unsigned int prepare_count;
67 unsigned long accuracy;
68 int phase;
69 struct hlist_head children;
70 struct hlist_node child_node;
71 struct hlist_node debug_node;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010072 struct hlist_head clks;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080073 unsigned int notifier_count;
74#ifdef CONFIG_DEBUG_FS
75 struct dentry *dentry;
76#endif
77 struct kref ref;
78};
79
Stephen Boyddfc202e2015-02-02 14:37:41 -080080#define CREATE_TRACE_POINTS
81#include <trace/events/clk.h>
82
Michael Turquetteb09d6d92015-01-29 14:22:50 -080083struct clk {
84 struct clk_core *core;
85 const char *dev_id;
86 const char *con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +010087 unsigned long min_rate;
88 unsigned long max_rate;
Stephen Boyd50595f82015-02-06 11:42:44 -080089 struct hlist_node clks_node;
Michael Turquetteb09d6d92015-01-29 14:22:50 -080090};
91
Mike Turquetteeab89f62013-03-28 13:59:01 -070092/*** locking ***/
93static void clk_prepare_lock(void)
94{
Mike Turquette533ddeb2013-03-28 13:59:02 -070095 if (!mutex_trylock(&prepare_lock)) {
96 if (prepare_owner == current) {
97 prepare_refcnt++;
98 return;
99 }
100 mutex_lock(&prepare_lock);
101 }
102 WARN_ON_ONCE(prepare_owner != NULL);
103 WARN_ON_ONCE(prepare_refcnt != 0);
104 prepare_owner = current;
105 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700106}
107
108static void clk_prepare_unlock(void)
109{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700110 WARN_ON_ONCE(prepare_owner != current);
111 WARN_ON_ONCE(prepare_refcnt == 0);
112
113 if (--prepare_refcnt)
114 return;
115 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700116 mutex_unlock(&prepare_lock);
117}
118
119static unsigned long clk_enable_lock(void)
120{
121 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700122
123 if (!spin_trylock_irqsave(&enable_lock, flags)) {
124 if (enable_owner == current) {
125 enable_refcnt++;
126 return flags;
127 }
128 spin_lock_irqsave(&enable_lock, flags);
129 }
130 WARN_ON_ONCE(enable_owner != NULL);
131 WARN_ON_ONCE(enable_refcnt != 0);
132 enable_owner = current;
133 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700134 return flags;
135}
136
137static void clk_enable_unlock(unsigned long flags)
138{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700139 WARN_ON_ONCE(enable_owner != current);
140 WARN_ON_ONCE(enable_refcnt == 0);
141
142 if (--enable_refcnt)
143 return;
144 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700145 spin_unlock_irqrestore(&enable_lock, flags);
146}
147
Mike Turquetteb24764902012-03-15 23:11:19 -0700148/*** debugfs support ***/
149
Mike Turquetteea72dc22013-12-18 21:38:52 -0800150#ifdef CONFIG_DEBUG_FS
Mike Turquetteb24764902012-03-15 23:11:19 -0700151#include <linux/debugfs.h>
152
153static struct dentry *rootdir;
Mike Turquetteb24764902012-03-15 23:11:19 -0700154static int inited = 0;
Stephen Boyd6314b672014-09-04 23:37:49 -0700155static DEFINE_MUTEX(clk_debug_lock);
156static HLIST_HEAD(clk_debug_list);
Mike Turquetteb24764902012-03-15 23:11:19 -0700157
Sachin Kamat6b44c8542014-07-01 11:56:34 +0530158static struct hlist_head *all_lists[] = {
159 &clk_root_list,
160 &clk_orphan_list,
161 NULL,
162};
163
164static struct hlist_head *orphan_list[] = {
165 &clk_orphan_list,
166 NULL,
167};
168
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100169static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
170 int level)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530171{
172 if (!c)
173 return;
174
Mike Turquettee59c5372014-02-18 21:21:25 -0800175 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530176 level * 3 + 1, "",
177 30 - level * 3, c->name,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100178 c->enable_count, c->prepare_count, clk_core_get_rate(c),
179 clk_core_get_accuracy(c), clk_core_get_phase(c));
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530180}
181
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100182static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530183 int level)
184{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100185 struct clk_core *child;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530186
187 if (!c)
188 return;
189
190 clk_summary_show_one(s, c, level);
191
Sasha Levinb67bfe02013-02-27 17:06:00 -0800192 hlist_for_each_entry(child, &c->children, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530193 clk_summary_show_subtree(s, child, level + 1);
194}
195
196static int clk_summary_show(struct seq_file *s, void *data)
197{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100198 struct clk_core *c;
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300199 struct hlist_head **lists = (struct hlist_head **)s->private;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530200
Mike Turquettee59c5372014-02-18 21:21:25 -0800201 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
202 seq_puts(s, "----------------------------------------------------------------------------------------\n");
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530203
Mike Turquetteeab89f62013-03-28 13:59:01 -0700204 clk_prepare_lock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530205
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300206 for (; *lists; lists++)
207 hlist_for_each_entry(c, *lists, child_node)
208 clk_summary_show_subtree(s, c, 0);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530209
Mike Turquetteeab89f62013-03-28 13:59:01 -0700210 clk_prepare_unlock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530211
212 return 0;
213}
214
215
216static int clk_summary_open(struct inode *inode, struct file *file)
217{
218 return single_open(file, clk_summary_show, inode->i_private);
219}
220
221static const struct file_operations clk_summary_fops = {
222 .open = clk_summary_open,
223 .read = seq_read,
224 .llseek = seq_lseek,
225 .release = single_release,
226};
227
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100228static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530229{
230 if (!c)
231 return;
232
233 seq_printf(s, "\"%s\": { ", c->name);
234 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
235 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100236 seq_printf(s, "\"rate\": %lu", clk_core_get_rate(c));
237 seq_printf(s, "\"accuracy\": %lu", clk_core_get_accuracy(c));
238 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530239}
240
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100241static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530242{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100243 struct clk_core *child;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530244
245 if (!c)
246 return;
247
248 clk_dump_one(s, c, level);
249
Sasha Levinb67bfe02013-02-27 17:06:00 -0800250 hlist_for_each_entry(child, &c->children, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530251 seq_printf(s, ",");
252 clk_dump_subtree(s, child, level + 1);
253 }
254
255 seq_printf(s, "}");
256}
257
258static int clk_dump(struct seq_file *s, void *data)
259{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100260 struct clk_core *c;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530261 bool first_node = true;
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300262 struct hlist_head **lists = (struct hlist_head **)s->private;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530263
264 seq_printf(s, "{");
265
Mike Turquetteeab89f62013-03-28 13:59:01 -0700266 clk_prepare_lock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530267
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300268 for (; *lists; lists++) {
269 hlist_for_each_entry(c, *lists, child_node) {
270 if (!first_node)
271 seq_puts(s, ",");
272 first_node = false;
273 clk_dump_subtree(s, c, 0);
274 }
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530275 }
276
Mike Turquetteeab89f62013-03-28 13:59:01 -0700277 clk_prepare_unlock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530278
279 seq_printf(s, "}");
280 return 0;
281}
282
283
284static int clk_dump_open(struct inode *inode, struct file *file)
285{
286 return single_open(file, clk_dump, inode->i_private);
287}
288
289static const struct file_operations clk_dump_fops = {
290 .open = clk_dump_open,
291 .read = seq_read,
292 .llseek = seq_lseek,
293 .release = single_release,
294};
295
Stephen Boydd6968fc2015-04-30 13:54:13 -0700296static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
Mike Turquetteb24764902012-03-15 23:11:19 -0700297{
298 struct dentry *d;
299 int ret = -ENOMEM;
300
Stephen Boydd6968fc2015-04-30 13:54:13 -0700301 if (!core || !pdentry) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700302 ret = -EINVAL;
303 goto out;
304 }
305
Stephen Boydd6968fc2015-04-30 13:54:13 -0700306 d = debugfs_create_dir(core->name, pdentry);
Mike Turquetteb24764902012-03-15 23:11:19 -0700307 if (!d)
308 goto out;
309
Stephen Boydd6968fc2015-04-30 13:54:13 -0700310 core->dentry = d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700311
Stephen Boydd6968fc2015-04-30 13:54:13 -0700312 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
313 (u32 *)&core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700314 if (!d)
315 goto err_out;
316
Stephen Boydd6968fc2015-04-30 13:54:13 -0700317 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
318 (u32 *)&core->accuracy);
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100319 if (!d)
320 goto err_out;
321
Stephen Boydd6968fc2015-04-30 13:54:13 -0700322 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
323 (u32 *)&core->phase);
Mike Turquettee59c5372014-02-18 21:21:25 -0800324 if (!d)
325 goto err_out;
326
Stephen Boydd6968fc2015-04-30 13:54:13 -0700327 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
328 (u32 *)&core->flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700329 if (!d)
330 goto err_out;
331
Stephen Boydd6968fc2015-04-30 13:54:13 -0700332 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
333 (u32 *)&core->prepare_count);
Mike Turquetteb24764902012-03-15 23:11:19 -0700334 if (!d)
335 goto err_out;
336
Stephen Boydd6968fc2015-04-30 13:54:13 -0700337 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
338 (u32 *)&core->enable_count);
Mike Turquetteb24764902012-03-15 23:11:19 -0700339 if (!d)
340 goto err_out;
341
Stephen Boydd6968fc2015-04-30 13:54:13 -0700342 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
343 (u32 *)&core->notifier_count);
Mike Turquetteb24764902012-03-15 23:11:19 -0700344 if (!d)
345 goto err_out;
346
Stephen Boydd6968fc2015-04-30 13:54:13 -0700347 if (core->ops->debug_init) {
348 ret = core->ops->debug_init(core->hw, core->dentry);
Chris Brandabeab452014-07-03 14:01:29 -0700349 if (ret)
Alex Elderc646cbf2014-03-21 06:43:56 -0500350 goto err_out;
Chris Brandabeab452014-07-03 14:01:29 -0700351 }
Alex Elderc646cbf2014-03-21 06:43:56 -0500352
Mike Turquetteb24764902012-03-15 23:11:19 -0700353 ret = 0;
354 goto out;
355
356err_out:
Stephen Boydd6968fc2015-04-30 13:54:13 -0700357 debugfs_remove_recursive(core->dentry);
358 core->dentry = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -0700359out:
360 return ret;
361}
362
Mike Turquetteb24764902012-03-15 23:11:19 -0700363/**
364 * clk_debug_register - add a clk node to the debugfs clk tree
Stephen Boydd6968fc2015-04-30 13:54:13 -0700365 * @core: the clk being added to the debugfs clk tree
Mike Turquetteb24764902012-03-15 23:11:19 -0700366 *
367 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
368 * initialized. Otherwise it bails out early since the debugfs clk tree
369 * will be created lazily by clk_debug_init as part of a late_initcall.
Mike Turquetteb24764902012-03-15 23:11:19 -0700370 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700371static int clk_debug_register(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700372{
Mike Turquetteb24764902012-03-15 23:11:19 -0700373 int ret = 0;
374
Stephen Boyd6314b672014-09-04 23:37:49 -0700375 mutex_lock(&clk_debug_lock);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700376 hlist_add_head(&core->debug_node, &clk_debug_list);
Stephen Boyd6314b672014-09-04 23:37:49 -0700377
Mike Turquetteb24764902012-03-15 23:11:19 -0700378 if (!inited)
Stephen Boyd6314b672014-09-04 23:37:49 -0700379 goto unlock;
Mike Turquetteb24764902012-03-15 23:11:19 -0700380
Stephen Boydd6968fc2015-04-30 13:54:13 -0700381 ret = clk_debug_create_one(core, rootdir);
Stephen Boyd6314b672014-09-04 23:37:49 -0700382unlock:
383 mutex_unlock(&clk_debug_lock);
Mike Turquetteb24764902012-03-15 23:11:19 -0700384
Mike Turquetteb24764902012-03-15 23:11:19 -0700385 return ret;
386}
387
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200388 /**
389 * clk_debug_unregister - remove a clk node from the debugfs clk tree
Stephen Boydd6968fc2015-04-30 13:54:13 -0700390 * @core: the clk being removed from the debugfs clk tree
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200391 *
392 * Dynamically removes a clk and all it's children clk nodes from the
393 * debugfs clk tree if clk->dentry points to debugfs created by
394 * clk_debug_register in __clk_init.
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200395 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700396static void clk_debug_unregister(struct clk_core *core)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200397{
Stephen Boyd6314b672014-09-04 23:37:49 -0700398 mutex_lock(&clk_debug_lock);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700399 hlist_del_init(&core->debug_node);
400 debugfs_remove_recursive(core->dentry);
401 core->dentry = NULL;
Stephen Boyd6314b672014-09-04 23:37:49 -0700402 mutex_unlock(&clk_debug_lock);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200403}
404
Tomeu Vizoso61c7cdd2014-12-02 08:54:21 +0100405struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
Peter De Schrijverfb2b3c92014-06-26 18:00:53 +0300406 void *data, const struct file_operations *fops)
407{
408 struct dentry *d = NULL;
409
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100410 if (hw->core->dentry)
411 d = debugfs_create_file(name, mode, hw->core->dentry, data,
412 fops);
Peter De Schrijverfb2b3c92014-06-26 18:00:53 +0300413
414 return d;
415}
416EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
417
Mike Turquetteb24764902012-03-15 23:11:19 -0700418/**
419 * clk_debug_init - lazily create the debugfs clk tree visualization
420 *
421 * clks are often initialized very early during boot before memory can
422 * be dynamically allocated and well before debugfs is setup.
423 * clk_debug_init walks the clk tree hierarchy while holding
424 * prepare_lock and creates the topology as part of a late_initcall,
425 * thus insuring that clks initialized very early will still be
426 * represented in the debugfs clk tree. This function should only be
427 * called once at boot-time, and all other clks added dynamically will
428 * be done so with clk_debug_register.
429 */
430static int __init clk_debug_init(void)
431{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700432 struct clk_core *core;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530433 struct dentry *d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700434
435 rootdir = debugfs_create_dir("clk", NULL);
436
437 if (!rootdir)
438 return -ENOMEM;
439
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300440 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530441 &clk_summary_fops);
442 if (!d)
443 return -ENOMEM;
444
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300445 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530446 &clk_dump_fops);
447 if (!d)
448 return -ENOMEM;
449
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300450 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
451 &orphan_list, &clk_summary_fops);
452 if (!d)
453 return -ENOMEM;
Mike Turquetteb24764902012-03-15 23:11:19 -0700454
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300455 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
456 &orphan_list, &clk_dump_fops);
457 if (!d)
Mike Turquetteb24764902012-03-15 23:11:19 -0700458 return -ENOMEM;
459
Stephen Boyd6314b672014-09-04 23:37:49 -0700460 mutex_lock(&clk_debug_lock);
Stephen Boydd6968fc2015-04-30 13:54:13 -0700461 hlist_for_each_entry(core, &clk_debug_list, debug_node)
462 clk_debug_create_one(core, rootdir);
Mike Turquetteb24764902012-03-15 23:11:19 -0700463
464 inited = 1;
Stephen Boyd6314b672014-09-04 23:37:49 -0700465 mutex_unlock(&clk_debug_lock);
Mike Turquetteb24764902012-03-15 23:11:19 -0700466
467 return 0;
468}
469late_initcall(clk_debug_init);
470#else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700471static inline int clk_debug_register(struct clk_core *core) { return 0; }
472static inline void clk_debug_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100473 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200474{
475}
Stephen Boydd6968fc2015-04-30 13:54:13 -0700476static inline void clk_debug_unregister(struct clk_core *core)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200477{
478}
Mike Turquette70d347e2012-03-26 11:53:47 -0700479#endif
Mike Turquetteb24764902012-03-15 23:11:19 -0700480
Mike Turquetteb24764902012-03-15 23:11:19 -0700481/* caller must hold prepare_lock */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700482static void clk_unprepare_unused_subtree(struct clk_core *core)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100483{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100484 struct clk_core *child;
Ulf Hansson1c155b32013-03-12 20:26:03 +0100485
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100486 lockdep_assert_held(&prepare_lock);
487
Stephen Boydd6968fc2015-04-30 13:54:13 -0700488 hlist_for_each_entry(child, &core->children, child_node)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100489 clk_unprepare_unused_subtree(child);
490
Stephen Boydd6968fc2015-04-30 13:54:13 -0700491 if (core->prepare_count)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100492 return;
493
Stephen Boydd6968fc2015-04-30 13:54:13 -0700494 if (core->flags & CLK_IGNORE_UNUSED)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100495 return;
496
Stephen Boydd6968fc2015-04-30 13:54:13 -0700497 if (clk_core_is_prepared(core)) {
498 trace_clk_unprepare(core);
499 if (core->ops->unprepare_unused)
500 core->ops->unprepare_unused(core->hw);
501 else if (core->ops->unprepare)
502 core->ops->unprepare(core->hw);
503 trace_clk_unprepare_complete(core);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100504 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100505}
506
507/* caller must hold prepare_lock */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700508static void clk_disable_unused_subtree(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700509{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100510 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700511 unsigned long flags;
512
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +0100513 lockdep_assert_held(&prepare_lock);
514
Stephen Boydd6968fc2015-04-30 13:54:13 -0700515 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700516 clk_disable_unused_subtree(child);
517
Mike Turquetteeab89f62013-03-28 13:59:01 -0700518 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700519
Stephen Boydd6968fc2015-04-30 13:54:13 -0700520 if (core->enable_count)
Mike Turquetteb24764902012-03-15 23:11:19 -0700521 goto unlock_out;
522
Stephen Boydd6968fc2015-04-30 13:54:13 -0700523 if (core->flags & CLK_IGNORE_UNUSED)
Mike Turquetteb24764902012-03-15 23:11:19 -0700524 goto unlock_out;
525
Mike Turquette7c045a52012-12-04 11:00:35 -0800526 /*
527 * some gate clocks have special needs during the disable-unused
528 * sequence. call .disable_unused if available, otherwise fall
529 * back to .disable
530 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700531 if (clk_core_is_enabled(core)) {
532 trace_clk_disable(core);
533 if (core->ops->disable_unused)
534 core->ops->disable_unused(core->hw);
535 else if (core->ops->disable)
536 core->ops->disable(core->hw);
537 trace_clk_disable_complete(core);
Mike Turquette7c045a52012-12-04 11:00:35 -0800538 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700539
540unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700541 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700542}
543
Olof Johansson1e435252013-04-27 14:10:18 -0700544static bool clk_ignore_unused;
545static int __init clk_ignore_unused_setup(char *__unused)
546{
547 clk_ignore_unused = true;
548 return 1;
549}
550__setup("clk_ignore_unused", clk_ignore_unused_setup);
551
Mike Turquetteb24764902012-03-15 23:11:19 -0700552static int clk_disable_unused(void)
553{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700554 struct clk_core *core;
Mike Turquetteb24764902012-03-15 23:11:19 -0700555
Olof Johansson1e435252013-04-27 14:10:18 -0700556 if (clk_ignore_unused) {
557 pr_warn("clk: Not disabling unused clocks\n");
558 return 0;
559 }
560
Mike Turquetteeab89f62013-03-28 13:59:01 -0700561 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700562
Stephen Boydd6968fc2015-04-30 13:54:13 -0700563 hlist_for_each_entry(core, &clk_root_list, child_node)
564 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700565
Stephen Boydd6968fc2015-04-30 13:54:13 -0700566 hlist_for_each_entry(core, &clk_orphan_list, child_node)
567 clk_disable_unused_subtree(core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700568
Stephen Boydd6968fc2015-04-30 13:54:13 -0700569 hlist_for_each_entry(core, &clk_root_list, child_node)
570 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100571
Stephen Boydd6968fc2015-04-30 13:54:13 -0700572 hlist_for_each_entry(core, &clk_orphan_list, child_node)
573 clk_unprepare_unused_subtree(core);
Ulf Hansson1c155b32013-03-12 20:26:03 +0100574
Mike Turquetteeab89f62013-03-28 13:59:01 -0700575 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700576
577 return 0;
578}
Saravana Kannand41d5802013-05-09 11:35:01 -0700579late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700580
581/*** helper functions ***/
582
Russ Dill65800b22012-11-26 11:20:09 -0800583const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700584{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100585 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700586}
Niels de Vos48950842012-12-13 13:12:25 +0100587EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700588
Russ Dill65800b22012-11-26 11:20:09 -0800589struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700590{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100591 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700592}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800593EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700594
Russ Dill65800b22012-11-26 11:20:09 -0800595u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700596{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100597 return !clk ? 0 : clk->core->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700598}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800599EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700600
Russ Dill65800b22012-11-26 11:20:09 -0800601struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700602{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100603 if (!clk)
604 return NULL;
605
606 /* TODO: Create a per-user clk and change callers to call clk_put */
607 return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700608}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800609EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700610
Stephen Boydd6968fc2015-04-30 13:54:13 -0700611static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100612 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100613{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700614 if (!core || index >= core->num_parents)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100615 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -0700616 else if (!core->parents)
617 return clk_core_lookup(core->parent_names[index]);
618 else if (!core->parents[index])
619 return core->parents[index] =
620 clk_core_lookup(core->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100621 else
Stephen Boydd6968fc2015-04-30 13:54:13 -0700622 return core->parents[index];
James Hogan7ef3dcc2013-07-29 12:24:58 +0100623}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100624
625struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
626{
627 struct clk_core *parent;
628
629 if (!clk)
630 return NULL;
631
632 parent = clk_core_get_parent_by_index(clk->core, index);
633
634 return !parent ? NULL : parent->hw->clk;
635}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800636EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100637
Russ Dill65800b22012-11-26 11:20:09 -0800638unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700639{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100640 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700641}
642
Stephen Boydd6968fc2015-04-30 13:54:13 -0700643static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700644{
645 unsigned long ret;
646
Stephen Boydd6968fc2015-04-30 13:54:13 -0700647 if (!core) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530648 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700649 goto out;
650 }
651
Stephen Boydd6968fc2015-04-30 13:54:13 -0700652 ret = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700653
Stephen Boydd6968fc2015-04-30 13:54:13 -0700654 if (core->flags & CLK_IS_ROOT)
Mike Turquetteb24764902012-03-15 23:11:19 -0700655 goto out;
656
Stephen Boydd6968fc2015-04-30 13:54:13 -0700657 if (!core->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530658 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700659
660out:
661 return ret;
662}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100663
664unsigned long __clk_get_rate(struct clk *clk)
665{
666 if (!clk)
667 return 0;
668
669 return clk_core_get_rate_nolock(clk->core);
670}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800671EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700672
Stephen Boydd6968fc2015-04-30 13:54:13 -0700673static unsigned long __clk_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100674{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700675 if (!core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100676 return 0;
677
Stephen Boydd6968fc2015-04-30 13:54:13 -0700678 return core->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100679}
680
Russ Dill65800b22012-11-26 11:20:09 -0800681unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700682{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100683 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700684}
Thierry Redingb05c6832013-09-03 09:43:51 +0200685EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700686
Stephen Boydd6968fc2015-04-30 13:54:13 -0700687static bool clk_core_is_prepared(struct clk_core *core)
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100688{
689 int ret;
690
Stephen Boydd6968fc2015-04-30 13:54:13 -0700691 if (!core)
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100692 return false;
693
694 /*
695 * .is_prepared is optional for clocks that can prepare
696 * fall back to software usage counter if it is missing
697 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700698 if (!core->ops->is_prepared) {
699 ret = core->prepare_count ? 1 : 0;
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100700 goto out;
701 }
702
Stephen Boydd6968fc2015-04-30 13:54:13 -0700703 ret = core->ops->is_prepared(core->hw);
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100704out:
705 return !!ret;
706}
707
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100708bool __clk_is_prepared(struct clk *clk)
709{
710 if (!clk)
711 return false;
712
713 return clk_core_is_prepared(clk->core);
714}
715
Stephen Boydd6968fc2015-04-30 13:54:13 -0700716static bool clk_core_is_enabled(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700717{
718 int ret;
719
Stephen Boydd6968fc2015-04-30 13:54:13 -0700720 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700721 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700722
723 /*
724 * .is_enabled is only mandatory for clocks that gate
725 * fall back to software usage counter if .is_enabled is missing
726 */
Stephen Boydd6968fc2015-04-30 13:54:13 -0700727 if (!core->ops->is_enabled) {
728 ret = core->enable_count ? 1 : 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700729 goto out;
730 }
731
Stephen Boydd6968fc2015-04-30 13:54:13 -0700732 ret = core->ops->is_enabled(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700733out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700734 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700735}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100736
737bool __clk_is_enabled(struct clk *clk)
738{
739 if (!clk)
740 return false;
741
742 return clk_core_is_enabled(clk->core);
743}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800744EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700745
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100746static struct clk_core *__clk_lookup_subtree(const char *name,
Stephen Boydd6968fc2015-04-30 13:54:13 -0700747 struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700748{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100749 struct clk_core *child;
750 struct clk_core *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700751
Stephen Boydd6968fc2015-04-30 13:54:13 -0700752 if (!strcmp(core->name, name))
753 return core;
Mike Turquetteb24764902012-03-15 23:11:19 -0700754
Stephen Boydd6968fc2015-04-30 13:54:13 -0700755 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700756 ret = __clk_lookup_subtree(name, child);
757 if (ret)
758 return ret;
759 }
760
761 return NULL;
762}
763
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100764static struct clk_core *clk_core_lookup(const char *name)
Mike Turquetteb24764902012-03-15 23:11:19 -0700765{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100766 struct clk_core *root_clk;
767 struct clk_core *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700768
769 if (!name)
770 return NULL;
771
772 /* search the 'proper' clk tree first */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800773 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700774 ret = __clk_lookup_subtree(name, root_clk);
775 if (ret)
776 return ret;
777 }
778
779 /* if not found, then search the orphan tree */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800780 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700781 ret = __clk_lookup_subtree(name, root_clk);
782 if (ret)
783 return ret;
784 }
785
786 return NULL;
787}
788
Stephen Boyd15a02c12015-01-19 18:05:28 -0800789static bool mux_is_better_rate(unsigned long rate, unsigned long now,
790 unsigned long best, unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100791{
Stephen Boyd15a02c12015-01-19 18:05:28 -0800792 if (flags & CLK_MUX_ROUND_CLOSEST)
793 return abs(now - rate) < abs(best - rate);
794
795 return now <= rate && now > best;
796}
797
798static long
799clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100800 unsigned long min_rate,
801 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800802 unsigned long *best_parent_rate,
803 struct clk_hw **best_parent_p,
804 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100805{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100806 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
James Hogane366fdd2013-07-29 12:25:02 +0100807 int i, num_parents;
808 unsigned long parent_rate, best = 0;
809
810 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100811 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
812 parent = core->parent;
813 if (core->flags & CLK_SET_RATE_PARENT)
Javier Martinez Canillas9e0ad7d2015-02-12 14:58:28 +0100814 best = __clk_determine_rate(parent ? parent->hw : NULL,
815 rate, min_rate, max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100816 else if (parent)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100817 best = clk_core_get_rate_nolock(parent);
James Hogane366fdd2013-07-29 12:25:02 +0100818 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100819 best = clk_core_get_rate_nolock(core);
James Hogane366fdd2013-07-29 12:25:02 +0100820 goto out;
821 }
822
823 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100824 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100825 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100826 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100827 if (!parent)
828 continue;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100829 if (core->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100830 parent_rate = __clk_determine_rate(parent->hw, rate,
831 min_rate,
832 max_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100833 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100834 parent_rate = clk_core_get_rate_nolock(parent);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800835 if (mux_is_better_rate(rate, parent_rate, best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100836 best_parent = parent;
837 best = parent_rate;
838 }
839 }
840
841out:
842 if (best_parent)
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100843 *best_parent_p = best_parent->hw;
James Hogane366fdd2013-07-29 12:25:02 +0100844 *best_parent_rate = best;
845
846 return best;
847}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800848
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100849struct clk *__clk_lookup(const char *name)
850{
851 struct clk_core *core = clk_core_lookup(name);
852
853 return !core ? NULL : core->hw->clk;
854}
855
Stephen Boydd6968fc2015-04-30 13:54:13 -0700856static void clk_core_get_boundaries(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100857 unsigned long *min_rate,
858 unsigned long *max_rate)
859{
860 struct clk *clk_user;
861
862 *min_rate = 0;
863 *max_rate = ULONG_MAX;
864
Stephen Boydd6968fc2015-04-30 13:54:13 -0700865 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100866 *min_rate = max(*min_rate, clk_user->min_rate);
867
Stephen Boydd6968fc2015-04-30 13:54:13 -0700868 hlist_for_each_entry(clk_user, &core->clks, clks_node)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100869 *max_rate = min(*max_rate, clk_user->max_rate);
870}
871
Stephen Boyd15a02c12015-01-19 18:05:28 -0800872/*
873 * Helper for finding best parent to provide a given frequency. This can be used
874 * directly as a determine_rate callback (e.g. for a mux), or from a more
875 * complex clock that may combine a mux with other operations.
876 */
877long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100878 unsigned long min_rate,
879 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800880 unsigned long *best_parent_rate,
881 struct clk_hw **best_parent_p)
882{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100883 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
884 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800885 best_parent_p, 0);
886}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800887EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100888
Stephen Boyd15a02c12015-01-19 18:05:28 -0800889long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100890 unsigned long min_rate,
891 unsigned long max_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800892 unsigned long *best_parent_rate,
893 struct clk_hw **best_parent_p)
894{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100895 return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
896 best_parent_rate,
Stephen Boyd15a02c12015-01-19 18:05:28 -0800897 best_parent_p,
898 CLK_MUX_ROUND_CLOSEST);
899}
900EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
901
Mike Turquetteb24764902012-03-15 23:11:19 -0700902/*** clk api ***/
903
Stephen Boydd6968fc2015-04-30 13:54:13 -0700904static void clk_core_unprepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700905{
Stephen Boydd6968fc2015-04-30 13:54:13 -0700906 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700907 return;
908
Stephen Boydd6968fc2015-04-30 13:54:13 -0700909 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -0700910 return;
911
Stephen Boydd6968fc2015-04-30 13:54:13 -0700912 if (--core->prepare_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -0700913 return;
914
Stephen Boydd6968fc2015-04-30 13:54:13 -0700915 WARN_ON(core->enable_count > 0);
Mike Turquetteb24764902012-03-15 23:11:19 -0700916
Stephen Boydd6968fc2015-04-30 13:54:13 -0700917 trace_clk_unprepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800918
Stephen Boydd6968fc2015-04-30 13:54:13 -0700919 if (core->ops->unprepare)
920 core->ops->unprepare(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700921
Stephen Boydd6968fc2015-04-30 13:54:13 -0700922 trace_clk_unprepare_complete(core);
923 clk_core_unprepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700924}
925
926/**
927 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200928 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700929 *
930 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
931 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
932 * if the operation may sleep. One example is a clk which is accessed over
933 * I2c. In the complex case a clk gate operation may require a fast and a slow
934 * part. It is this reason that clk_unprepare and clk_disable are not mutually
935 * exclusive. In fact clk_disable must be called before clk_unprepare.
936 */
937void clk_unprepare(struct clk *clk)
938{
Stephen Boyd63589e92014-03-26 16:06:37 -0700939 if (IS_ERR_OR_NULL(clk))
940 return;
941
Mike Turquetteeab89f62013-03-28 13:59:01 -0700942 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100943 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700944 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700945}
946EXPORT_SYMBOL_GPL(clk_unprepare);
947
Stephen Boydd6968fc2015-04-30 13:54:13 -0700948static int clk_core_prepare(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700949{
950 int ret = 0;
951
Stephen Boydd6968fc2015-04-30 13:54:13 -0700952 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -0700953 return 0;
954
Stephen Boydd6968fc2015-04-30 13:54:13 -0700955 if (core->prepare_count == 0) {
956 ret = clk_core_prepare(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700957 if (ret)
958 return ret;
959
Stephen Boydd6968fc2015-04-30 13:54:13 -0700960 trace_clk_prepare(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800961
Stephen Boydd6968fc2015-04-30 13:54:13 -0700962 if (core->ops->prepare)
963 ret = core->ops->prepare(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800964
Stephen Boydd6968fc2015-04-30 13:54:13 -0700965 trace_clk_prepare_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800966
967 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -0700968 clk_core_unprepare(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -0800969 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700970 }
971 }
972
Stephen Boydd6968fc2015-04-30 13:54:13 -0700973 core->prepare_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -0700974
975 return 0;
976}
977
978/**
979 * clk_prepare - prepare a clock source
980 * @clk: the clk being prepared
981 *
982 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
983 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
984 * operation may sleep. One example is a clk which is accessed over I2c. In
985 * the complex case a clk ungate operation may require a fast and a slow part.
986 * It is this reason that clk_prepare and clk_enable are not mutually
987 * exclusive. In fact clk_prepare must be called before clk_enable.
988 * Returns 0 on success, -EERROR otherwise.
989 */
990int clk_prepare(struct clk *clk)
991{
992 int ret;
993
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100994 if (!clk)
995 return 0;
996
Mike Turquetteeab89f62013-03-28 13:59:01 -0700997 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100998 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700999 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001000
1001 return ret;
1002}
1003EXPORT_SYMBOL_GPL(clk_prepare);
1004
Stephen Boydd6968fc2015-04-30 13:54:13 -07001005static void clk_core_disable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001006{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001007 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001008 return;
1009
Stephen Boydd6968fc2015-04-30 13:54:13 -07001010 if (WARN_ON(core->enable_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -07001011 return;
1012
Stephen Boydd6968fc2015-04-30 13:54:13 -07001013 if (--core->enable_count > 0)
Mike Turquetteb24764902012-03-15 23:11:19 -07001014 return;
1015
Stephen Boydd6968fc2015-04-30 13:54:13 -07001016 trace_clk_disable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001017
Stephen Boydd6968fc2015-04-30 13:54:13 -07001018 if (core->ops->disable)
1019 core->ops->disable(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001020
Stephen Boydd6968fc2015-04-30 13:54:13 -07001021 trace_clk_disable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001022
Stephen Boydd6968fc2015-04-30 13:54:13 -07001023 clk_core_disable(core->parent);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001024}
1025
Mike Turquetteb24764902012-03-15 23:11:19 -07001026/**
1027 * clk_disable - gate a clock
1028 * @clk: the clk being gated
1029 *
1030 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
1031 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
1032 * clk if the operation is fast and will never sleep. One example is a
1033 * SoC-internal clk which is controlled via simple register writes. In the
1034 * complex case a clk gate operation may require a fast and a slow part. It is
1035 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
1036 * In fact clk_disable must be called before clk_unprepare.
1037 */
1038void clk_disable(struct clk *clk)
1039{
1040 unsigned long flags;
1041
Stephen Boyd63589e92014-03-26 16:06:37 -07001042 if (IS_ERR_OR_NULL(clk))
1043 return;
1044
Mike Turquetteeab89f62013-03-28 13:59:01 -07001045 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -07001046 clk_core_disable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001047 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001048}
1049EXPORT_SYMBOL_GPL(clk_disable);
1050
Stephen Boydd6968fc2015-04-30 13:54:13 -07001051static int clk_core_enable(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001052{
1053 int ret = 0;
1054
Stephen Boydd6968fc2015-04-30 13:54:13 -07001055 if (!core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001056 return 0;
1057
Stephen Boydd6968fc2015-04-30 13:54:13 -07001058 if (WARN_ON(core->prepare_count == 0))
Mike Turquetteb24764902012-03-15 23:11:19 -07001059 return -ESHUTDOWN;
1060
Stephen Boydd6968fc2015-04-30 13:54:13 -07001061 if (core->enable_count == 0) {
1062 ret = clk_core_enable(core->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -07001063
1064 if (ret)
1065 return ret;
1066
Stephen Boydd6968fc2015-04-30 13:54:13 -07001067 trace_clk_enable(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001068
Stephen Boydd6968fc2015-04-30 13:54:13 -07001069 if (core->ops->enable)
1070 ret = core->ops->enable(core->hw);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001071
Stephen Boydd6968fc2015-04-30 13:54:13 -07001072 trace_clk_enable_complete(core);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001073
1074 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001075 clk_core_disable(core->parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001076 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001077 }
1078 }
1079
Stephen Boydd6968fc2015-04-30 13:54:13 -07001080 core->enable_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07001081 return 0;
1082}
1083
1084/**
1085 * clk_enable - ungate a clock
1086 * @clk: the clk being ungated
1087 *
1088 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1089 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1090 * if the operation will never sleep. One example is a SoC-internal clk which
1091 * is controlled via simple register writes. In the complex case a clk ungate
1092 * operation may require a fast and a slow part. It is this reason that
1093 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1094 * must be called before clk_enable. Returns 0 on success, -EERROR
1095 * otherwise.
1096 */
1097int clk_enable(struct clk *clk)
1098{
1099 unsigned long flags;
1100 int ret;
1101
Dong Aisheng864e1602015-04-30 14:02:19 -07001102 if (!clk)
1103 return 0;
1104
Mike Turquetteeab89f62013-03-28 13:59:01 -07001105 flags = clk_enable_lock();
Dong Aisheng864e1602015-04-30 14:02:19 -07001106 ret = clk_core_enable(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001107 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001108
1109 return ret;
1110}
1111EXPORT_SYMBOL_GPL(clk_enable);
1112
Stephen Boydd6968fc2015-04-30 13:54:13 -07001113static unsigned long clk_core_round_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001114 unsigned long rate,
1115 unsigned long min_rate,
1116 unsigned long max_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001117{
Shawn Guo81536e02012-04-12 20:50:17 +08001118 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001119 struct clk_core *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001120 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -07001121
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001122 lockdep_assert_held(&prepare_lock);
1123
Stephen Boydd6968fc2015-04-30 13:54:13 -07001124 if (!core)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001125 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001126
Stephen Boydd6968fc2015-04-30 13:54:13 -07001127 parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001128 if (parent)
1129 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001130
Stephen Boydd6968fc2015-04-30 13:54:13 -07001131 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001132 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001133 return core->ops->determine_rate(core->hw, rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001134 min_rate, max_rate,
1135 &parent_rate, &parent_hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001136 } else if (core->ops->round_rate)
1137 return core->ops->round_rate(core->hw, rate, &parent_rate);
1138 else if (core->flags & CLK_SET_RATE_PARENT)
1139 return clk_core_round_rate_nolock(core->parent, rate, min_rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001140 max_rate);
James Hogan71472c02013-07-29 12:25:00 +01001141 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001142 return core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001143}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001144
1145/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001146 * __clk_determine_rate - get the closest rate actually supported by a clock
1147 * @hw: determine the rate of this clock
1148 * @rate: target rate
1149 * @min_rate: returned rate must be greater than this rate
1150 * @max_rate: returned rate must be less than this rate
1151 *
1152 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate and
1153 * .determine_rate.
1154 */
1155unsigned long __clk_determine_rate(struct clk_hw *hw,
1156 unsigned long rate,
1157 unsigned long min_rate,
1158 unsigned long max_rate)
1159{
1160 if (!hw)
1161 return 0;
1162
1163 return clk_core_round_rate_nolock(hw->core, rate, min_rate, max_rate);
1164}
1165EXPORT_SYMBOL_GPL(__clk_determine_rate);
1166
1167/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001168 * __clk_round_rate - round the given rate for a clk
1169 * @clk: round the rate of this clock
1170 * @rate: the rate which is to be rounded
1171 *
1172 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
1173 */
1174unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
1175{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001176 unsigned long min_rate;
1177 unsigned long max_rate;
1178
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001179 if (!clk)
1180 return 0;
1181
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001182 clk_core_get_boundaries(clk->core, &min_rate, &max_rate);
1183
1184 return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001185}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +02001186EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001187
1188/**
1189 * clk_round_rate - round the given rate for a clk
1190 * @clk: the clk for which we are rounding a rate
1191 * @rate: the rate which is to be rounded
1192 *
1193 * Takes in a rate as input and rounds it to a rate that the clk can actually
1194 * use which is then returned. If clk doesn't support round_rate operation
1195 * then the parent rate is returned.
1196 */
1197long clk_round_rate(struct clk *clk, unsigned long rate)
1198{
1199 unsigned long ret;
1200
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001201 if (!clk)
1202 return 0;
1203
Mike Turquetteeab89f62013-03-28 13:59:01 -07001204 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001205 ret = __clk_round_rate(clk, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001206 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001207
1208 return ret;
1209}
1210EXPORT_SYMBOL_GPL(clk_round_rate);
1211
1212/**
1213 * __clk_notify - call clk notifier chain
Stephen Boydd6968fc2015-04-30 13:54:13 -07001214 * @core: clk that is changing rate
Mike Turquetteb24764902012-03-15 23:11:19 -07001215 * @msg: clk notifier type (see include/linux/clk.h)
1216 * @old_rate: old clk rate
1217 * @new_rate: new clk rate
1218 *
1219 * Triggers a notifier call chain on the clk rate-change notification
1220 * for 'clk'. Passes a pointer to the struct clk and the previous
1221 * and current rates to the notifier callback. Intended to be called by
1222 * internal clock code only. Returns NOTIFY_DONE from the last driver
1223 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1224 * a driver returns that.
1225 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001226static int __clk_notify(struct clk_core *core, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001227 unsigned long old_rate, unsigned long new_rate)
1228{
1229 struct clk_notifier *cn;
1230 struct clk_notifier_data cnd;
1231 int ret = NOTIFY_DONE;
1232
Mike Turquetteb24764902012-03-15 23:11:19 -07001233 cnd.old_rate = old_rate;
1234 cnd.new_rate = new_rate;
1235
1236 list_for_each_entry(cn, &clk_notifier_list, node) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001237 if (cn->clk->core == core) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001238 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001239 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1240 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -07001241 }
1242 }
1243
1244 return ret;
1245}
1246
1247/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001248 * __clk_recalc_accuracies
Stephen Boydd6968fc2015-04-30 13:54:13 -07001249 * @core: first clk in the subtree
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001250 *
1251 * Walks the subtree of clks starting with clk and recalculates accuracies as
1252 * it goes. Note that if a clk does not implement the .recalc_accuracy
1253 * callback then it is assumed that the clock will take on the accuracy of it's
1254 * parent.
1255 *
1256 * Caller must hold prepare_lock.
1257 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001258static void __clk_recalc_accuracies(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001259{
1260 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001261 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001262
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001263 lockdep_assert_held(&prepare_lock);
1264
Stephen Boydd6968fc2015-04-30 13:54:13 -07001265 if (core->parent)
1266 parent_accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001267
Stephen Boydd6968fc2015-04-30 13:54:13 -07001268 if (core->ops->recalc_accuracy)
1269 core->accuracy = core->ops->recalc_accuracy(core->hw,
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001270 parent_accuracy);
1271 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07001272 core->accuracy = parent_accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001273
Stephen Boydd6968fc2015-04-30 13:54:13 -07001274 hlist_for_each_entry(child, &core->children, child_node)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001275 __clk_recalc_accuracies(child);
1276}
1277
Stephen Boydd6968fc2015-04-30 13:54:13 -07001278static long clk_core_get_accuracy(struct clk_core *core)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001279{
1280 unsigned long accuracy;
1281
1282 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001283 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1284 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001285
Stephen Boydd6968fc2015-04-30 13:54:13 -07001286 accuracy = __clk_get_accuracy(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001287 clk_prepare_unlock();
1288
1289 return accuracy;
1290}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001291
1292/**
1293 * clk_get_accuracy - return the accuracy of clk
1294 * @clk: the clk whose accuracy is being returned
1295 *
1296 * Simply returns the cached accuracy of the clk, unless
1297 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1298 * issued.
1299 * If clk is NULL then returns 0.
1300 */
1301long clk_get_accuracy(struct clk *clk)
1302{
1303 if (!clk)
1304 return 0;
1305
1306 return clk_core_get_accuracy(clk->core);
1307}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001308EXPORT_SYMBOL_GPL(clk_get_accuracy);
1309
Stephen Boydd6968fc2015-04-30 13:54:13 -07001310static unsigned long clk_recalc(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001311 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001312{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001313 if (core->ops->recalc_rate)
1314 return core->ops->recalc_rate(core->hw, parent_rate);
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001315 return parent_rate;
1316}
1317
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001318/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001319 * __clk_recalc_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001320 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001321 * @msg: notification type (see include/linux/clk.h)
1322 *
1323 * Walks the subtree of clks starting with clk and recalculates rates as it
1324 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001325 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001326 *
1327 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1328 * if necessary.
1329 *
1330 * Caller must hold prepare_lock.
1331 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001332static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001333{
1334 unsigned long old_rate;
1335 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001336 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001337
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001338 lockdep_assert_held(&prepare_lock);
1339
Stephen Boydd6968fc2015-04-30 13:54:13 -07001340 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001341
Stephen Boydd6968fc2015-04-30 13:54:13 -07001342 if (core->parent)
1343 parent_rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001344
Stephen Boydd6968fc2015-04-30 13:54:13 -07001345 core->rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001346
1347 /*
1348 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1349 * & ABORT_RATE_CHANGE notifiers
1350 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001351 if (core->notifier_count && msg)
1352 __clk_notify(core, msg, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001353
Stephen Boydd6968fc2015-04-30 13:54:13 -07001354 hlist_for_each_entry(child, &core->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001355 __clk_recalc_rates(child, msg);
1356}
1357
Stephen Boydd6968fc2015-04-30 13:54:13 -07001358static unsigned long clk_core_get_rate(struct clk_core *core)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001359{
1360 unsigned long rate;
1361
1362 clk_prepare_lock();
1363
Stephen Boydd6968fc2015-04-30 13:54:13 -07001364 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1365 __clk_recalc_rates(core, 0);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001366
Stephen Boydd6968fc2015-04-30 13:54:13 -07001367 rate = clk_core_get_rate_nolock(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001368 clk_prepare_unlock();
1369
1370 return rate;
1371}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001372
Mike Turquetteb24764902012-03-15 23:11:19 -07001373/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001374 * clk_get_rate - return the rate of clk
1375 * @clk: the clk whose rate is being returned
1376 *
1377 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1378 * is set, which means a recalc_rate will be issued.
1379 * If clk is NULL then returns 0.
1380 */
1381unsigned long clk_get_rate(struct clk *clk)
1382{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001383 if (!clk)
1384 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001385
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001386 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001387}
1388EXPORT_SYMBOL_GPL(clk_get_rate);
1389
Stephen Boydd6968fc2015-04-30 13:54:13 -07001390static int clk_fetch_parent_index(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001391 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001392{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001393 int i;
James Hogan4935b222013-07-29 12:24:59 +01001394
Stephen Boydd6968fc2015-04-30 13:54:13 -07001395 if (!core->parents) {
1396 core->parents = kcalloc(core->num_parents,
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001397 sizeof(struct clk *), GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001398 if (!core->parents)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001399 return -ENOMEM;
1400 }
James Hogan4935b222013-07-29 12:24:59 +01001401
1402 /*
1403 * find index of new parent clock using cached parent ptrs,
1404 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001405 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001406 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001407 for (i = 0; i < core->num_parents; i++) {
1408 if (core->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001409 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001410
Stephen Boydd6968fc2015-04-30 13:54:13 -07001411 if (core->parents[i])
Tomasz Figada0f0b22013-09-29 02:37:16 +02001412 continue;
1413
Stephen Boydd6968fc2015-04-30 13:54:13 -07001414 if (!strcmp(core->parent_names[i], parent->name)) {
1415 core->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001416 return i;
James Hogan4935b222013-07-29 12:24:59 +01001417 }
1418 }
1419
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001420 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001421}
1422
Stephen Boydd6968fc2015-04-30 13:54:13 -07001423static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001424{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001425 hlist_del(&core->child_node);
James Hogan4935b222013-07-29 12:24:59 +01001426
James Hogan903efc52013-08-29 12:10:51 +01001427 if (new_parent) {
1428 /* avoid duplicate POST_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001429 if (new_parent->new_child == core)
James Hogan903efc52013-08-29 12:10:51 +01001430 new_parent->new_child = NULL;
1431
Stephen Boydd6968fc2015-04-30 13:54:13 -07001432 hlist_add_head(&core->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001433 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07001434 hlist_add_head(&core->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001435 }
James Hogan4935b222013-07-29 12:24:59 +01001436
Stephen Boydd6968fc2015-04-30 13:54:13 -07001437 core->parent = new_parent;
James Hogan4935b222013-07-29 12:24:59 +01001438}
1439
Stephen Boydd6968fc2015-04-30 13:54:13 -07001440static struct clk_core *__clk_set_parent_before(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001441 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001442{
1443 unsigned long flags;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001444 struct clk_core *old_parent = core->parent;
James Hogan4935b222013-07-29 12:24:59 +01001445
1446 /*
1447 * Migrate prepare state between parents and prevent race with
1448 * clk_enable().
1449 *
1450 * If the clock is not prepared, then a race with
1451 * clk_enable/disable() is impossible since we already have the
1452 * prepare lock (future calls to clk_enable() need to be preceded by
1453 * a clk_prepare()).
1454 *
1455 * If the clock is prepared, migrate the prepared state to the new
1456 * parent and also protect against a race with clk_enable() by
1457 * forcing the clock and the new parent on. This ensures that all
1458 * future calls to clk_enable() are practically NOPs with respect to
1459 * hardware and software states.
1460 *
1461 * See also: Comment for clk_set_parent() below.
1462 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001463 if (core->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001464 clk_core_prepare(parent);
1465 clk_core_enable(parent);
Stephen Boydd6968fc2015-04-30 13:54:13 -07001466 clk_core_enable(core);
James Hogan4935b222013-07-29 12:24:59 +01001467 }
1468
1469 /* update the clk tree topology */
1470 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001471 clk_reparent(core, parent);
James Hogan4935b222013-07-29 12:24:59 +01001472 clk_enable_unlock(flags);
1473
Stephen Boyd3fa22522014-01-15 10:47:22 -08001474 return old_parent;
1475}
1476
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001477static void __clk_set_parent_after(struct clk_core *core,
1478 struct clk_core *parent,
1479 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001480{
1481 /*
1482 * Finish the migration of prepare state and undo the changes done
1483 * for preventing a race with clk_enable().
1484 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001485 if (core->prepare_count) {
1486 clk_core_disable(core);
1487 clk_core_disable(old_parent);
1488 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001489 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001490}
1491
Stephen Boydd6968fc2015-04-30 13:54:13 -07001492static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001493 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001494{
1495 unsigned long flags;
1496 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001497 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001498
Stephen Boydd6968fc2015-04-30 13:54:13 -07001499 old_parent = __clk_set_parent_before(core, parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001500
Stephen Boydd6968fc2015-04-30 13:54:13 -07001501 trace_clk_set_parent(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001502
James Hogan4935b222013-07-29 12:24:59 +01001503 /* change clock input source */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001504 if (parent && core->ops->set_parent)
1505 ret = core->ops->set_parent(core->hw, p_index);
James Hogan4935b222013-07-29 12:24:59 +01001506
Stephen Boydd6968fc2015-04-30 13:54:13 -07001507 trace_clk_set_parent_complete(core, parent);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001508
James Hogan4935b222013-07-29 12:24:59 +01001509 if (ret) {
1510 flags = clk_enable_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07001511 clk_reparent(core, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001512 clk_enable_unlock(flags);
1513
Stephen Boydd6968fc2015-04-30 13:54:13 -07001514 if (core->prepare_count) {
1515 clk_core_disable(core);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001516 clk_core_disable(parent);
1517 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001518 }
1519 return ret;
1520 }
1521
Stephen Boydd6968fc2015-04-30 13:54:13 -07001522 __clk_set_parent_after(core, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001523
James Hogan4935b222013-07-29 12:24:59 +01001524 return 0;
1525}
1526
Ulf Hanssona093bde2012-08-31 14:21:28 +02001527/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001528 * __clk_speculate_rates
Stephen Boydd6968fc2015-04-30 13:54:13 -07001529 * @core: first clk in the subtree
Mike Turquetteb24764902012-03-15 23:11:19 -07001530 * @parent_rate: the "future" rate of clk's parent
1531 *
1532 * Walks the subtree of clks starting with clk, speculating rates as it
1533 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1534 *
1535 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1536 * pre-rate change notifications and returns early if no clks in the
1537 * subtree have subscribed to the notifications. Note that if a clk does not
1538 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001539 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001540 *
1541 * Caller must hold prepare_lock.
1542 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001543static int __clk_speculate_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001544 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001545{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001546 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001547 unsigned long new_rate;
1548 int ret = NOTIFY_DONE;
1549
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01001550 lockdep_assert_held(&prepare_lock);
1551
Stephen Boydd6968fc2015-04-30 13:54:13 -07001552 new_rate = clk_recalc(core, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001553
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001554 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001555 if (core->notifier_count)
1556 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001557
Mike Turquette86bcfa22014-02-24 16:08:41 -08001558 if (ret & NOTIFY_STOP_MASK) {
1559 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001560 __func__, core->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001561 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001562 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001563
Stephen Boydd6968fc2015-04-30 13:54:13 -07001564 hlist_for_each_entry(child, &core->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001565 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001566 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001567 break;
1568 }
1569
1570out:
1571 return ret;
1572}
1573
Stephen Boydd6968fc2015-04-30 13:54:13 -07001574static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001575 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001576{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001577 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001578
Stephen Boydd6968fc2015-04-30 13:54:13 -07001579 core->new_rate = new_rate;
1580 core->new_parent = new_parent;
1581 core->new_parent_index = p_index;
James Hogan71472c02013-07-29 12:25:00 +01001582 /* include clk in new parent's PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001583 core->new_child = NULL;
1584 if (new_parent && new_parent != core->parent)
1585 new_parent->new_child = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001586
Stephen Boydd6968fc2015-04-30 13:54:13 -07001587 hlist_for_each_entry(child, &core->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001588 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001589 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001590 }
1591}
1592
1593/*
1594 * calculate the new rates returning the topmost clock that has to be
1595 * changed.
1596 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001597static struct clk_core *clk_calc_new_rates(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001598 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001599{
Stephen Boydd6968fc2015-04-30 13:54:13 -07001600 struct clk_core *top = core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001601 struct clk_core *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001602 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001603 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001604 unsigned long new_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001605 unsigned long min_rate;
1606 unsigned long max_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001607 int p_index = 0;
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001608 long ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001609
Mike Turquette7452b212012-03-26 14:45:36 -07001610 /* sanity */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001611 if (IS_ERR_OR_NULL(core))
Mike Turquette7452b212012-03-26 14:45:36 -07001612 return NULL;
1613
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001614 /* save parent rate, if it exists */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001615 parent = old_parent = core->parent;
James Hogan71472c02013-07-29 12:25:00 +01001616 if (parent)
1617 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001618
Stephen Boydd6968fc2015-04-30 13:54:13 -07001619 clk_core_get_boundaries(core, &min_rate, &max_rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001620
James Hogan71472c02013-07-29 12:25:00 +01001621 /* find the closest rate and parent clk/rate */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001622 if (core->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001623 parent_hw = parent ? parent->hw : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001624 ret = core->ops->determine_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001625 min_rate,
1626 max_rate,
1627 &best_parent_rate,
1628 &parent_hw);
1629 if (ret < 0)
1630 return NULL;
1631
1632 new_rate = ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001633 parent = parent_hw ? parent_hw->core : NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001634 } else if (core->ops->round_rate) {
1635 ret = core->ops->round_rate(core->hw, rate,
Boris Brezillon03bc10a2015-03-29 03:48:48 +02001636 &best_parent_rate);
1637 if (ret < 0)
1638 return NULL;
1639
1640 new_rate = ret;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001641 if (new_rate < min_rate || new_rate > max_rate)
1642 return NULL;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001643 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
James Hogan71472c02013-07-29 12:25:00 +01001644 /* pass-through clock without adjustable parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001645 core->new_rate = core->rate;
James Hogan71472c02013-07-29 12:25:00 +01001646 return NULL;
1647 } else {
1648 /* pass-through clock with adjustable parent */
1649 top = clk_calc_new_rates(parent, rate);
1650 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001651 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001652 }
1653
James Hogan71472c02013-07-29 12:25:00 +01001654 /* some clocks must be gated to change parent */
1655 if (parent != old_parent &&
Stephen Boydd6968fc2015-04-30 13:54:13 -07001656 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
James Hogan71472c02013-07-29 12:25:00 +01001657 pr_debug("%s: %s not gated but wants to reparent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001658 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001659 return NULL;
1660 }
1661
James Hogan71472c02013-07-29 12:25:00 +01001662 /* try finding the new parent index */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001663 if (parent && core->num_parents > 1) {
1664 p_index = clk_fetch_parent_index(core, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001665 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001666 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07001667 __func__, parent->name, core->name);
James Hogan71472c02013-07-29 12:25:00 +01001668 return NULL;
1669 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001670 }
1671
Stephen Boydd6968fc2015-04-30 13:54:13 -07001672 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
James Hogan71472c02013-07-29 12:25:00 +01001673 best_parent_rate != parent->rate)
1674 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001675
1676out:
Stephen Boydd6968fc2015-04-30 13:54:13 -07001677 clk_calc_subtree(core, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001678
1679 return top;
1680}
1681
1682/*
1683 * Notify about rate changes in a subtree. Always walk down the whole tree
1684 * so that in case of an error we can walk down the whole tree again and
1685 * abort the change.
1686 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001687static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001688 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001689{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001690 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001691 int ret = NOTIFY_DONE;
1692
Stephen Boydd6968fc2015-04-30 13:54:13 -07001693 if (core->rate == core->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301694 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001695
Stephen Boydd6968fc2015-04-30 13:54:13 -07001696 if (core->notifier_count) {
1697 ret = __clk_notify(core, event, core->rate, core->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001698 if (ret & NOTIFY_STOP_MASK)
Stephen Boydd6968fc2015-04-30 13:54:13 -07001699 fail_clk = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07001700 }
1701
Stephen Boydd6968fc2015-04-30 13:54:13 -07001702 hlist_for_each_entry(child, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001703 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001704 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001705 continue;
1706 tmp_clk = clk_propagate_rate_change(child, event);
1707 if (tmp_clk)
1708 fail_clk = tmp_clk;
1709 }
1710
Stephen Boydd6968fc2015-04-30 13:54:13 -07001711 /* handle the new child who might not be in core->children yet */
1712 if (core->new_child) {
1713 tmp_clk = clk_propagate_rate_change(core->new_child, event);
James Hogan71472c02013-07-29 12:25:00 +01001714 if (tmp_clk)
1715 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001716 }
1717
1718 return fail_clk;
1719}
1720
1721/*
1722 * walk down a subtree and set the new rates notifying the rate
1723 * change on the way
1724 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001725static void clk_change_rate(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001726{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001727 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001728 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001729 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001730 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001731 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001732 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001733
Stephen Boydd6968fc2015-04-30 13:54:13 -07001734 old_rate = core->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001735
Stephen Boydd6968fc2015-04-30 13:54:13 -07001736 if (core->new_parent)
1737 best_parent_rate = core->new_parent->rate;
1738 else if (core->parent)
1739 best_parent_rate = core->parent->rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001740
Stephen Boydd6968fc2015-04-30 13:54:13 -07001741 if (core->new_parent && core->new_parent != core->parent) {
1742 old_parent = __clk_set_parent_before(core, core->new_parent);
1743 trace_clk_set_parent(core, core->new_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001744
Stephen Boydd6968fc2015-04-30 13:54:13 -07001745 if (core->ops->set_rate_and_parent) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08001746 skip_set_rate = true;
Stephen Boydd6968fc2015-04-30 13:54:13 -07001747 core->ops->set_rate_and_parent(core->hw, core->new_rate,
Stephen Boyd3fa22522014-01-15 10:47:22 -08001748 best_parent_rate,
Stephen Boydd6968fc2015-04-30 13:54:13 -07001749 core->new_parent_index);
1750 } else if (core->ops->set_parent) {
1751 core->ops->set_parent(core->hw, core->new_parent_index);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001752 }
1753
Stephen Boydd6968fc2015-04-30 13:54:13 -07001754 trace_clk_set_parent_complete(core, core->new_parent);
1755 __clk_set_parent_after(core, core->new_parent, old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001756 }
1757
Stephen Boydd6968fc2015-04-30 13:54:13 -07001758 trace_clk_set_rate(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001759
Stephen Boydd6968fc2015-04-30 13:54:13 -07001760 if (!skip_set_rate && core->ops->set_rate)
1761 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001762
Stephen Boydd6968fc2015-04-30 13:54:13 -07001763 trace_clk_set_rate_complete(core, core->new_rate);
Stephen Boyddfc202e2015-02-02 14:37:41 -08001764
Stephen Boydd6968fc2015-04-30 13:54:13 -07001765 core->rate = clk_recalc(core, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001766
Stephen Boydd6968fc2015-04-30 13:54:13 -07001767 if (core->notifier_count && old_rate != core->rate)
1768 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001769
Tero Kristo067bb172014-08-21 16:47:45 +03001770 /*
1771 * Use safe iteration, as change_rate can actually swap parents
1772 * for certain clock types.
1773 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001774 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001775 /* Skip children who will be reparented to another clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001776 if (child->new_parent && child->new_parent != core)
James Hogan71472c02013-07-29 12:25:00 +01001777 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001778 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001779 }
1780
Stephen Boydd6968fc2015-04-30 13:54:13 -07001781 /* handle the new child who might not be in core->children yet */
1782 if (core->new_child)
1783 clk_change_rate(core->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001784}
1785
Stephen Boydd6968fc2015-04-30 13:54:13 -07001786static int clk_core_set_rate_nolock(struct clk_core *core,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001787 unsigned long req_rate)
1788{
1789 struct clk_core *top, *fail_clk;
1790 unsigned long rate = req_rate;
1791 int ret = 0;
1792
Stephen Boydd6968fc2015-04-30 13:54:13 -07001793 if (!core)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001794 return 0;
1795
1796 /* bail early if nothing to do */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001797 if (rate == clk_core_get_rate_nolock(core))
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001798 return 0;
1799
Stephen Boydd6968fc2015-04-30 13:54:13 -07001800 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001801 return -EBUSY;
1802
1803 /* calculate new rates and get the topmost changed clock */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001804 top = clk_calc_new_rates(core, rate);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001805 if (!top)
1806 return -EINVAL;
1807
1808 /* notify that we are about to change rates */
1809 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1810 if (fail_clk) {
1811 pr_debug("%s: failed to set %s rate\n", __func__,
1812 fail_clk->name);
1813 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1814 return -EBUSY;
1815 }
1816
1817 /* change the rates */
1818 clk_change_rate(top);
1819
Stephen Boydd6968fc2015-04-30 13:54:13 -07001820 core->req_rate = req_rate;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001821
1822 return ret;
1823}
1824
Mike Turquetteb24764902012-03-15 23:11:19 -07001825/**
1826 * clk_set_rate - specify a new rate for clk
1827 * @clk: the clk whose rate is being changed
1828 * @rate: the new rate for clk
1829 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001830 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001831 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001832 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1833 * propagate up to clk's parent; whether or not this happens depends on the
1834 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1835 * after calling .round_rate then upstream parent propagation is ignored. If
1836 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001837 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001838 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1839 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001840 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001841 * Rate changes are accomplished via tree traversal that also recalculates the
1842 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001843 *
1844 * Returns 0 on success, -EERROR otherwise.
1845 */
1846int clk_set_rate(struct clk *clk, unsigned long rate)
1847{
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001848 int ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001849
Mike Turquette89ac8d72013-08-21 23:58:09 -07001850 if (!clk)
1851 return 0;
1852
Mike Turquetteb24764902012-03-15 23:11:19 -07001853 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001854 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001855
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001856 ret = clk_core_set_rate_nolock(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001857
Mike Turquetteeab89f62013-03-28 13:59:01 -07001858 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001859
1860 return ret;
1861}
1862EXPORT_SYMBOL_GPL(clk_set_rate);
1863
1864/**
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01001865 * clk_set_rate_range - set a rate range for a clock source
1866 * @clk: clock source
1867 * @min: desired minimum clock rate in Hz, inclusive
1868 * @max: desired maximum clock rate in Hz, inclusive
1869 *
1870 * Returns success (0) or negative errno.
1871 */
1872int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1873{
1874 int ret = 0;
1875
1876 if (!clk)
1877 return 0;
1878
1879 if (min > max) {
1880 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1881 __func__, clk->core->name, clk->dev_id, clk->con_id,
1882 min, max);
1883 return -EINVAL;
1884 }
1885
1886 clk_prepare_lock();
1887
1888 if (min != clk->min_rate || max != clk->max_rate) {
1889 clk->min_rate = min;
1890 clk->max_rate = max;
1891 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1892 }
1893
1894 clk_prepare_unlock();
1895
1896 return ret;
1897}
1898EXPORT_SYMBOL_GPL(clk_set_rate_range);
1899
1900/**
1901 * clk_set_min_rate - set a minimum clock rate for a clock source
1902 * @clk: clock source
1903 * @rate: desired minimum clock rate in Hz, inclusive
1904 *
1905 * Returns success (0) or negative errno.
1906 */
1907int clk_set_min_rate(struct clk *clk, unsigned long rate)
1908{
1909 if (!clk)
1910 return 0;
1911
1912 return clk_set_rate_range(clk, rate, clk->max_rate);
1913}
1914EXPORT_SYMBOL_GPL(clk_set_min_rate);
1915
1916/**
1917 * clk_set_max_rate - set a maximum clock rate for a clock source
1918 * @clk: clock source
1919 * @rate: desired maximum clock rate in Hz, inclusive
1920 *
1921 * Returns success (0) or negative errno.
1922 */
1923int clk_set_max_rate(struct clk *clk, unsigned long rate)
1924{
1925 if (!clk)
1926 return 0;
1927
1928 return clk_set_rate_range(clk, clk->min_rate, rate);
1929}
1930EXPORT_SYMBOL_GPL(clk_set_max_rate);
1931
1932/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001933 * clk_get_parent - return the parent of a clk
1934 * @clk: the clk whose parent gets returned
1935 *
1936 * Simply returns clk->parent. Returns NULL if clk is NULL.
1937 */
1938struct clk *clk_get_parent(struct clk *clk)
1939{
1940 struct clk *parent;
1941
Mike Turquetteeab89f62013-03-28 13:59:01 -07001942 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001943 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001944 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001945
1946 return parent;
1947}
1948EXPORT_SYMBOL_GPL(clk_get_parent);
1949
1950/*
1951 * .get_parent is mandatory for clocks with multiple possible parents. It is
1952 * optional for single-parent clocks. Always call .get_parent if it is
1953 * available and WARN if it is missing for multi-parent clocks.
1954 *
1955 * For single-parent clocks without .get_parent, first check to see if the
1956 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001957 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001958 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07001959static struct clk_core *__clk_init_parent(struct clk_core *core)
Mike Turquetteb24764902012-03-15 23:11:19 -07001960{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001961 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001962 u8 index;
1963
1964 /* handle the trivial cases */
1965
Stephen Boydd6968fc2015-04-30 13:54:13 -07001966 if (!core->num_parents)
Mike Turquetteb24764902012-03-15 23:11:19 -07001967 goto out;
1968
Stephen Boydd6968fc2015-04-30 13:54:13 -07001969 if (core->num_parents == 1) {
1970 if (IS_ERR_OR_NULL(core->parent))
1971 core->parent = clk_core_lookup(core->parent_names[0]);
1972 ret = core->parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001973 goto out;
1974 }
1975
Stephen Boydd6968fc2015-04-30 13:54:13 -07001976 if (!core->ops->get_parent) {
1977 WARN(!core->ops->get_parent,
Mike Turquetteb24764902012-03-15 23:11:19 -07001978 "%s: multi-parent clocks must implement .get_parent\n",
1979 __func__);
1980 goto out;
1981 };
1982
1983 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07001984 * Do our best to cache parent clocks in core->parents. This prevents
1985 * unnecessary and expensive lookups. We don't set core->parent here;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001986 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001987 */
1988
Stephen Boydd6968fc2015-04-30 13:54:13 -07001989 index = core->ops->get_parent(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07001990
Stephen Boydd6968fc2015-04-30 13:54:13 -07001991 if (!core->parents)
1992 core->parents =
1993 kcalloc(core->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001994 GFP_KERNEL);
1995
Stephen Boydd6968fc2015-04-30 13:54:13 -07001996 ret = clk_core_get_parent_by_index(core, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001997
1998out:
1999 return ret;
2000}
2001
Stephen Boydd6968fc2015-04-30 13:54:13 -07002002static void clk_core_reparent(struct clk_core *core,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002003 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02002004{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002005 clk_reparent(core, new_parent);
2006 __clk_recalc_accuracies(core);
2007 __clk_recalc_rates(core, POST_RATE_CHANGE);
Mike Turquetteb24764902012-03-15 23:11:19 -07002008}
2009
Mike Turquetteb24764902012-03-15 23:11:19 -07002010/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002011 * clk_has_parent - check if a clock is a possible parent for another
2012 * @clk: clock source
2013 * @parent: parent clock source
Mike Turquetteb24764902012-03-15 23:11:19 -07002014 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002015 * This function can be used in drivers that need to check that a clock can be
2016 * the parent of another without actually changing the parent.
Saravana Kannanf8aa0bd2013-05-15 21:07:24 -07002017 *
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002018 * Returns true if @parent is a possible parent for @clk, false otherwise.
Mike Turquetteb24764902012-03-15 23:11:19 -07002019 */
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002020bool clk_has_parent(struct clk *clk, struct clk *parent)
2021{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002022 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002023 unsigned int i;
2024
2025 /* NULL clocks should be nops, so return success if either is NULL. */
2026 if (!clk || !parent)
2027 return true;
2028
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002029 core = clk->core;
2030 parent_core = parent->core;
2031
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002032 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002033 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002034 return true;
2035
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002036 for (i = 0; i < core->num_parents; i++)
2037 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01002038 return true;
2039
2040 return false;
2041}
2042EXPORT_SYMBOL_GPL(clk_has_parent);
2043
Stephen Boydd6968fc2015-04-30 13:54:13 -07002044static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002045{
2046 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002047 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002048 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07002049
Stephen Boydd6968fc2015-04-30 13:54:13 -07002050 if (!core)
Mike Turquette89ac8d72013-08-21 23:58:09 -07002051 return 0;
2052
Mike Turquetteb24764902012-03-15 23:11:19 -07002053 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07002054 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002055
Stephen Boydd6968fc2015-04-30 13:54:13 -07002056 if (core->parent == parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07002057 goto out;
2058
Stephen Boydb61c43c2015-02-02 14:11:25 -08002059 /* verify ops for for multi-parent clks */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002060 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
Stephen Boydb61c43c2015-02-02 14:11:25 -08002061 ret = -ENOSYS;
2062 goto out;
2063 }
2064
Ulf Hansson031dcc92013-04-02 23:09:38 +02002065 /* check that we are allowed to re-parent if the clock is in use */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002066 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002067 ret = -EBUSY;
2068 goto out;
2069 }
2070
2071 /* try finding the new parent index */
2072 if (parent) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002073 p_index = clk_fetch_parent_index(core, parent);
Ulf Hansson031dcc92013-04-02 23:09:38 +02002074 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002075 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02002076 pr_debug("%s: clk %s can not be parent of clk %s\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002077 __func__, parent->name, core->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02002078 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02002079 goto out;
2080 }
2081 }
2082
Mike Turquetteb24764902012-03-15 23:11:19 -07002083 /* propagate PRE_RATE_CHANGE notifications */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002084 ret = __clk_speculate_rates(core, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07002085
2086 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07002087 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07002088 goto out;
2089
Ulf Hansson031dcc92013-04-02 23:09:38 +02002090 /* do the re-parent */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002091 ret = __clk_set_parent(core, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07002092
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002093 /* propagate rate an accuracy recalculation accordingly */
2094 if (ret) {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002095 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002096 } else {
Stephen Boydd6968fc2015-04-30 13:54:13 -07002097 __clk_recalc_rates(core, POST_RATE_CHANGE);
2098 __clk_recalc_accuracies(core);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002099 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002100
2101out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002102 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002103
2104 return ret;
2105}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002106
2107/**
2108 * clk_set_parent - switch the parent of a mux clk
2109 * @clk: the mux clk whose input we are switching
2110 * @parent: the new input to clk
2111 *
2112 * Re-parent clk to use parent as its new input source. If clk is in
2113 * prepared state, the clk will get enabled for the duration of this call. If
2114 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2115 * that, the reparenting is glitchy in hardware, etc), use the
2116 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2117 *
2118 * After successfully changing clk's parent clk_set_parent will update the
2119 * clk topology, sysfs topology and propagate rate recalculation via
2120 * __clk_recalc_rates.
2121 *
2122 * Returns 0 on success, -EERROR otherwise.
2123 */
2124int clk_set_parent(struct clk *clk, struct clk *parent)
2125{
2126 if (!clk)
2127 return 0;
2128
2129 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
2130}
Mike Turquetteb24764902012-03-15 23:11:19 -07002131EXPORT_SYMBOL_GPL(clk_set_parent);
2132
2133/**
Mike Turquettee59c5372014-02-18 21:21:25 -08002134 * clk_set_phase - adjust the phase shift of a clock signal
2135 * @clk: clock signal source
2136 * @degrees: number of degrees the signal is shifted
2137 *
2138 * Shifts the phase of a clock signal by the specified
2139 * degrees. Returns 0 on success, -EERROR otherwise.
2140 *
2141 * This function makes no distinction about the input or reference
2142 * signal that we adjust the clock signal phase against. For example
2143 * phase locked-loop clock signal generators we may shift phase with
2144 * respect to feedback clock signal input, but for other cases the
2145 * clock phase may be shifted with respect to some other, unspecified
2146 * signal.
2147 *
2148 * Additionally the concept of phase shift does not propagate through
2149 * the clock tree hierarchy, which sets it apart from clock rates and
2150 * clock accuracy. A parent clock phase attribute does not have an
2151 * impact on the phase attribute of a child clock.
2152 */
2153int clk_set_phase(struct clk *clk, int degrees)
2154{
Stephen Boyd08b95752015-02-02 14:09:43 -08002155 int ret = -EINVAL;
Mike Turquettee59c5372014-02-18 21:21:25 -08002156
2157 if (!clk)
Stephen Boyd08b95752015-02-02 14:09:43 -08002158 return 0;
Mike Turquettee59c5372014-02-18 21:21:25 -08002159
2160 /* sanity check degrees */
2161 degrees %= 360;
2162 if (degrees < 0)
2163 degrees += 360;
2164
2165 clk_prepare_lock();
2166
Stephen Boyddfc202e2015-02-02 14:37:41 -08002167 trace_clk_set_phase(clk->core, degrees);
2168
Stephen Boyd08b95752015-02-02 14:09:43 -08002169 if (clk->core->ops->set_phase)
2170 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08002171
Stephen Boyddfc202e2015-02-02 14:37:41 -08002172 trace_clk_set_phase_complete(clk->core, degrees);
2173
Mike Turquettee59c5372014-02-18 21:21:25 -08002174 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002175 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08002176
Mike Turquettee59c5372014-02-18 21:21:25 -08002177 clk_prepare_unlock();
2178
Mike Turquettee59c5372014-02-18 21:21:25 -08002179 return ret;
2180}
Maxime Ripard9767b04f2015-01-20 22:23:43 +01002181EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002182
Stephen Boydd6968fc2015-04-30 13:54:13 -07002183static int clk_core_get_phase(struct clk_core *core)
Mike Turquettee59c5372014-02-18 21:21:25 -08002184{
Stephen Boyd1f3e1982015-04-30 14:21:56 -07002185 int ret;
Mike Turquettee59c5372014-02-18 21:21:25 -08002186
2187 clk_prepare_lock();
Stephen Boydd6968fc2015-04-30 13:54:13 -07002188 ret = core->phase;
Mike Turquettee59c5372014-02-18 21:21:25 -08002189 clk_prepare_unlock();
2190
Mike Turquettee59c5372014-02-18 21:21:25 -08002191 return ret;
2192}
Maxime Ripard9767b04f2015-01-20 22:23:43 +01002193EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002194
2195/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002196 * clk_get_phase - return the phase shift of a clock signal
2197 * @clk: clock signal source
2198 *
2199 * Returns the phase shift of a clock node in degrees, otherwise returns
2200 * -EERROR.
2201 */
2202int clk_get_phase(struct clk *clk)
2203{
2204 if (!clk)
2205 return 0;
2206
2207 return clk_core_get_phase(clk->core);
2208}
Mike Turquetteb24764902012-03-15 23:11:19 -07002209
2210/**
Michael Turquette3d3801e2015-02-25 09:11:01 -08002211 * clk_is_match - check if two clk's point to the same hardware clock
2212 * @p: clk compared against q
2213 * @q: clk compared against p
2214 *
2215 * Returns true if the two struct clk pointers both point to the same hardware
2216 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2217 * share the same struct clk_core object.
2218 *
2219 * Returns false otherwise. Note that two NULL clks are treated as matching.
2220 */
2221bool clk_is_match(const struct clk *p, const struct clk *q)
2222{
2223 /* trivial case: identical struct clk's or both NULL */
2224 if (p == q)
2225 return true;
2226
2227 /* true if clk->core pointers match. Avoid derefing garbage */
2228 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2229 if (p->core == q->core)
2230 return true;
2231
2232 return false;
2233}
2234EXPORT_SYMBOL_GPL(clk_is_match);
2235
2236/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002237 * __clk_init - initialize the data structures in a struct clk
2238 * @dev: device initializing this clk, placeholder for now
2239 * @clk: clk being initialized
2240 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002241 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002242 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002243 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002244static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002245{
Mike Turquetted1302a32012-03-29 14:30:40 -07002246 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002247 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002248 struct hlist_node *tmp2;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002249 struct clk_core *core;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002250 unsigned long rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002251
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002252 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002253 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002254
Stephen Boydd6968fc2015-04-30 13:54:13 -07002255 core = clk_user->core;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002256
Mike Turquetteeab89f62013-03-28 13:59:01 -07002257 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002258
2259 /* check to see if a clock with this name is already registered */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002260 if (clk_core_lookup(core->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002261 pr_debug("%s: clk %s already initialized\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002262 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002263 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002264 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002265 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002266
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002267 /* check that clk_ops are sane. See Documentation/clk.txt */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002268 if (core->ops->set_rate &&
2269 !((core->ops->round_rate || core->ops->determine_rate) &&
2270 core->ops->recalc_rate)) {
James Hogan71472c02013-07-29 12:25:00 +01002271 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002272 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002273 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002274 goto out;
2275 }
2276
Stephen Boydd6968fc2015-04-30 13:54:13 -07002277 if (core->ops->set_parent && !core->ops->get_parent) {
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002278 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002279 __func__, core->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002280 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002281 goto out;
2282 }
2283
Stephen Boydd6968fc2015-04-30 13:54:13 -07002284 if (core->ops->set_rate_and_parent &&
2285 !(core->ops->set_parent && core->ops->set_rate)) {
Stephen Boyd3fa22522014-01-15 10:47:22 -08002286 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002287 __func__, core->name);
Stephen Boyd3fa22522014-01-15 10:47:22 -08002288 ret = -EINVAL;
2289 goto out;
2290 }
2291
Mike Turquetteb24764902012-03-15 23:11:19 -07002292 /* throw a WARN if any entries in parent_names are NULL */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002293 for (i = 0; i < core->num_parents; i++)
2294 WARN(!core->parent_names[i],
Mike Turquetteb24764902012-03-15 23:11:19 -07002295 "%s: invalid NULL in %s's .parent_names\n",
Stephen Boydd6968fc2015-04-30 13:54:13 -07002296 __func__, core->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07002297
2298 /*
2299 * Allocate an array of struct clk *'s to avoid unnecessary string
2300 * look-ups of clk's possible parents. This can fail for clocks passed
Stephen Boydd6968fc2015-04-30 13:54:13 -07002301 * in to clk_init during early boot; thus any access to core->parents[]
Mike Turquetteb24764902012-03-15 23:11:19 -07002302 * must always check for a NULL pointer and try to populate it if
2303 * necessary.
2304 *
Stephen Boydd6968fc2015-04-30 13:54:13 -07002305 * If core->parents is not NULL we skip this entire block. This allows
2306 * for clock drivers to statically initialize core->parents.
Mike Turquetteb24764902012-03-15 23:11:19 -07002307 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002308 if (core->num_parents > 1 && !core->parents) {
2309 core->parents = kcalloc(core->num_parents, sizeof(struct clk *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002310 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002311 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002312 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002313 * clk_init'd; thus any access to clk->parents[] must check
2314 * for a NULL pointer. We can always perform lazy lookups for
2315 * missing parents later on.
2316 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002317 if (core->parents)
2318 for (i = 0; i < core->num_parents; i++)
2319 core->parents[i] =
2320 clk_core_lookup(core->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002321 }
2322
Stephen Boydd6968fc2015-04-30 13:54:13 -07002323 core->parent = __clk_init_parent(core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002324
2325 /*
Stephen Boydd6968fc2015-04-30 13:54:13 -07002326 * Populate core->parent if parent has already been __clk_init'd. If
Mike Turquetteb24764902012-03-15 23:11:19 -07002327 * parent has not yet been __clk_init'd then place clk in the orphan
2328 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2329 * clk list.
2330 *
2331 * Every time a new clk is clk_init'd then we walk the list of orphan
2332 * clocks and re-parent any that are children of the clock currently
2333 * being clk_init'd.
2334 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002335 if (core->parent)
2336 hlist_add_head(&core->child_node,
2337 &core->parent->children);
2338 else if (core->flags & CLK_IS_ROOT)
2339 hlist_add_head(&core->child_node, &clk_root_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002340 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002341 hlist_add_head(&core->child_node, &clk_orphan_list);
Mike Turquetteb24764902012-03-15 23:11:19 -07002342
2343 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002344 * Set clk's accuracy. The preferred method is to use
2345 * .recalc_accuracy. For simple clocks and lazy developers the default
2346 * fallback is to use the parent's accuracy. If a clock doesn't have a
2347 * parent (or is orphaned) then accuracy is set to zero (perfect
2348 * clock).
2349 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002350 if (core->ops->recalc_accuracy)
2351 core->accuracy = core->ops->recalc_accuracy(core->hw,
2352 __clk_get_accuracy(core->parent));
2353 else if (core->parent)
2354 core->accuracy = core->parent->accuracy;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002355 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002356 core->accuracy = 0;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002357
2358 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002359 * Set clk's phase.
2360 * Since a phase is by definition relative to its parent, just
2361 * query the current clock phase, or just assume it's in phase.
2362 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002363 if (core->ops->get_phase)
2364 core->phase = core->ops->get_phase(core->hw);
Maxime Ripard9824cf72014-07-14 13:53:27 +02002365 else
Stephen Boydd6968fc2015-04-30 13:54:13 -07002366 core->phase = 0;
Maxime Ripard9824cf72014-07-14 13:53:27 +02002367
2368 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002369 * Set clk's rate. The preferred method is to use .recalc_rate. For
2370 * simple clocks and lazy developers the default fallback is to use the
2371 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2372 * then rate is set to zero.
2373 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002374 if (core->ops->recalc_rate)
2375 rate = core->ops->recalc_rate(core->hw,
2376 clk_core_get_rate_nolock(core->parent));
2377 else if (core->parent)
2378 rate = core->parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002379 else
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002380 rate = 0;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002381 core->rate = core->req_rate = rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07002382
2383 /*
2384 * walk the list of orphan clocks and reparent any that are children of
2385 * this clock
2386 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002387 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002388 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002389 i = orphan->ops->get_parent(orphan->hw);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002390 if (!strcmp(core->name, orphan->parent_names[i]))
2391 clk_core_reparent(orphan, core);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002392 continue;
2393 }
2394
Mike Turquetteb24764902012-03-15 23:11:19 -07002395 for (i = 0; i < orphan->num_parents; i++)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002396 if (!strcmp(core->name, orphan->parent_names[i])) {
2397 clk_core_reparent(orphan, core);
Mike Turquetteb24764902012-03-15 23:11:19 -07002398 break;
2399 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002400 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002401
2402 /*
2403 * optional platform-specific magic
2404 *
2405 * The .init callback is not used by any of the basic clock types, but
2406 * exists for weird hardware that must perform initialization magic.
2407 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002408 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002409 */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002410 if (core->ops->init)
2411 core->ops->init(core->hw);
Mike Turquetteb24764902012-03-15 23:11:19 -07002412
Stephen Boydd6968fc2015-04-30 13:54:13 -07002413 kref_init(&core->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002414out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002415 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002416
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002417 if (!ret)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002418 clk_debug_register(core);
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002419
Mike Turquetted1302a32012-03-29 14:30:40 -07002420 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002421}
2422
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002423struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2424 const char *con_id)
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002425{
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002426 struct clk *clk;
2427
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002428 /* This is to allow this function to be chained to others */
2429 if (!hw || IS_ERR(hw))
2430 return (struct clk *) hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002431
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002432 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2433 if (!clk)
2434 return ERR_PTR(-ENOMEM);
2435
2436 clk->core = hw->core;
2437 clk->dev_id = dev_id;
2438 clk->con_id = con_id;
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002439 clk->max_rate = ULONG_MAX;
2440
2441 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002442 hlist_add_head(&clk->clks_node, &hw->core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002443 clk_prepare_unlock();
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002444
2445 return clk;
2446}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002447
Stephen Boyd73e0e492015-02-06 11:42:43 -08002448void __clk_free_clk(struct clk *clk)
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002449{
2450 clk_prepare_lock();
Stephen Boyd50595f82015-02-06 11:42:44 -08002451 hlist_del(&clk->clks_node);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002452 clk_prepare_unlock();
2453
2454 kfree(clk);
2455}
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002456
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002457/**
2458 * clk_register - allocate a new clock, register it and return an opaque cookie
2459 * @dev: device that is registering this clock
2460 * @hw: link to hardware-specific clock data
2461 *
2462 * clk_register is the primary interface for populating the clock tree with new
2463 * clock nodes. It returns a pointer to the newly allocated struct clk which
2464 * cannot be dereferenced by driver code but may be used in conjuction with the
2465 * rest of the clock API. In the event of an error clk_register will return an
2466 * error code; drivers must test for an error code after calling clk_register.
2467 */
2468struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002469{
Mike Turquetted1302a32012-03-29 14:30:40 -07002470 int i, ret;
Stephen Boydd6968fc2015-04-30 13:54:13 -07002471 struct clk_core *core;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002472
Stephen Boydd6968fc2015-04-30 13:54:13 -07002473 core = kzalloc(sizeof(*core), GFP_KERNEL);
2474 if (!core) {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002475 ret = -ENOMEM;
2476 goto fail_out;
2477 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002478
Stephen Boydd6968fc2015-04-30 13:54:13 -07002479 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2480 if (!core->name) {
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002481 ret = -ENOMEM;
2482 goto fail_name;
2483 }
Stephen Boydd6968fc2015-04-30 13:54:13 -07002484 core->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002485 if (dev && dev->driver)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002486 core->owner = dev->driver->owner;
2487 core->hw = hw;
2488 core->flags = hw->init->flags;
2489 core->num_parents = hw->init->num_parents;
2490 hw->core = core;
Mike Turquetteb24764902012-03-15 23:11:19 -07002491
Mike Turquetted1302a32012-03-29 14:30:40 -07002492 /* allocate local copy in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002493 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002494 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002495
Stephen Boydd6968fc2015-04-30 13:54:13 -07002496 if (!core->parent_names) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002497 ret = -ENOMEM;
2498 goto fail_parent_names;
2499 }
2500
2501
2502 /* copy each string name in case parent_names is __initdata */
Stephen Boydd6968fc2015-04-30 13:54:13 -07002503 for (i = 0; i < core->num_parents; i++) {
2504 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002505 GFP_KERNEL);
Stephen Boydd6968fc2015-04-30 13:54:13 -07002506 if (!core->parent_names[i]) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002507 ret = -ENOMEM;
2508 goto fail_parent_names_copy;
2509 }
2510 }
2511
Stephen Boydd6968fc2015-04-30 13:54:13 -07002512 INIT_HLIST_HEAD(&core->clks);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002513
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002514 hw->clk = __clk_create_clk(hw, NULL, NULL);
2515 if (IS_ERR(hw->clk)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002516 ret = PTR_ERR(hw->clk);
2517 goto fail_parent_names_copy;
2518 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002519
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002520 ret = __clk_init(dev, hw->clk);
Mike Turquetted1302a32012-03-29 14:30:40 -07002521 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002522 return hw->clk;
2523
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002524 __clk_free_clk(hw->clk);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002525 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002526
2527fail_parent_names_copy:
2528 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002529 kfree_const(core->parent_names[i]);
2530 kfree(core->parent_names);
Mike Turquetted1302a32012-03-29 14:30:40 -07002531fail_parent_names:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002532 kfree_const(core->name);
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002533fail_name:
Stephen Boydd6968fc2015-04-30 13:54:13 -07002534 kfree(core);
Mike Turquetted1302a32012-03-29 14:30:40 -07002535fail_out:
2536 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002537}
2538EXPORT_SYMBOL_GPL(clk_register);
2539
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002540/*
2541 * Free memory allocated for a clock.
2542 * Caller must hold prepare_lock.
2543 */
2544static void __clk_release(struct kref *ref)
2545{
Stephen Boydd6968fc2015-04-30 13:54:13 -07002546 struct clk_core *core = container_of(ref, struct clk_core, ref);
2547 int i = core->num_parents;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002548
Krzysztof Kozlowski496eadf2015-01-09 09:28:10 +01002549 lockdep_assert_held(&prepare_lock);
2550
Stephen Boydd6968fc2015-04-30 13:54:13 -07002551 kfree(core->parents);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002552 while (--i >= 0)
Stephen Boydd6968fc2015-04-30 13:54:13 -07002553 kfree_const(core->parent_names[i]);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002554
Stephen Boydd6968fc2015-04-30 13:54:13 -07002555 kfree(core->parent_names);
2556 kfree_const(core->name);
2557 kfree(core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002558}
2559
2560/*
2561 * Empty clk_ops for unregistered clocks. These are used temporarily
2562 * after clk_unregister() was called on a clock and until last clock
2563 * consumer calls clk_put() and the struct clk object is freed.
2564 */
2565static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2566{
2567 return -ENXIO;
2568}
2569
2570static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2571{
2572 WARN_ON_ONCE(1);
2573}
2574
2575static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2576 unsigned long parent_rate)
2577{
2578 return -ENXIO;
2579}
2580
2581static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2582{
2583 return -ENXIO;
2584}
2585
2586static const struct clk_ops clk_nodrv_ops = {
2587 .enable = clk_nodrv_prepare_enable,
2588 .disable = clk_nodrv_disable_unprepare,
2589 .prepare = clk_nodrv_prepare_enable,
2590 .unprepare = clk_nodrv_disable_unprepare,
2591 .set_rate = clk_nodrv_set_rate,
2592 .set_parent = clk_nodrv_set_parent,
2593};
2594
Mark Brown1df5c932012-04-18 09:07:12 +01002595/**
2596 * clk_unregister - unregister a currently registered clock
2597 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002598 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002599void clk_unregister(struct clk *clk)
2600{
2601 unsigned long flags;
2602
Stephen Boyd6314b672014-09-04 23:37:49 -07002603 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2604 return;
2605
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002606 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002607
2608 clk_prepare_lock();
2609
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002610 if (clk->core->ops == &clk_nodrv_ops) {
2611 pr_err("%s: unregistered clock: %s\n", __func__,
2612 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002613 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002614 }
2615 /*
2616 * Assign empty clock ops for consumers that might still hold
2617 * a reference to this clock.
2618 */
2619 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002620 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002621 clk_enable_unlock(flags);
2622
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002623 if (!hlist_empty(&clk->core->children)) {
2624 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002625 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002626
2627 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002628 hlist_for_each_entry_safe(child, t, &clk->core->children,
2629 child_node)
2630 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002631 }
2632
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002633 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002634
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002635 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002636 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002637 __func__, clk->core->name);
2638 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002639
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002640 clk_prepare_unlock();
2641}
Mark Brown1df5c932012-04-18 09:07:12 +01002642EXPORT_SYMBOL_GPL(clk_unregister);
2643
Stephen Boyd46c87732012-09-24 13:38:04 -07002644static void devm_clk_release(struct device *dev, void *res)
2645{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002646 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002647}
2648
2649/**
2650 * devm_clk_register - resource managed clk_register()
2651 * @dev: device that is registering this clock
2652 * @hw: link to hardware-specific clock data
2653 *
2654 * Managed clk_register(). Clocks returned from this function are
2655 * automatically clk_unregister()ed on driver detach. See clk_register() for
2656 * more information.
2657 */
2658struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2659{
2660 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002661 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002662
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002663 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2664 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002665 return ERR_PTR(-ENOMEM);
2666
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002667 clk = clk_register(dev, hw);
2668 if (!IS_ERR(clk)) {
2669 *clkp = clk;
2670 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002671 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002672 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002673 }
2674
2675 return clk;
2676}
2677EXPORT_SYMBOL_GPL(devm_clk_register);
2678
2679static int devm_clk_match(struct device *dev, void *res, void *data)
2680{
2681 struct clk *c = res;
2682 if (WARN_ON(!c))
2683 return 0;
2684 return c == data;
2685}
2686
2687/**
2688 * devm_clk_unregister - resource managed clk_unregister()
2689 * @clk: clock to unregister
2690 *
2691 * Deallocate a clock allocated with devm_clk_register(). Normally
2692 * this function will not need to be called and the resource management
2693 * code will ensure that the resource is freed.
2694 */
2695void devm_clk_unregister(struct device *dev, struct clk *clk)
2696{
2697 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2698}
2699EXPORT_SYMBOL_GPL(devm_clk_unregister);
2700
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002701/*
2702 * clkdev helpers
2703 */
2704int __clk_get(struct clk *clk)
2705{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002706 struct clk_core *core = !clk ? NULL : clk->core;
2707
2708 if (core) {
2709 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002710 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002711
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002712 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002713 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002714 return 1;
2715}
2716
2717void __clk_put(struct clk *clk)
2718{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002719 struct module *owner;
2720
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002721 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002722 return;
2723
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002724 clk_prepare_lock();
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002725
Stephen Boyd50595f82015-02-06 11:42:44 -08002726 hlist_del(&clk->clks_node);
Tomeu Vizosoec02ace2015-02-06 15:13:01 +01002727 if (clk->min_rate > clk->core->req_rate ||
2728 clk->max_rate < clk->core->req_rate)
2729 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2730
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002731 owner = clk->core->owner;
2732 kref_put(&clk->core->ref, __clk_release);
2733
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002734 clk_prepare_unlock();
2735
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002736 module_put(owner);
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +01002737
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002738 kfree(clk);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002739}
2740
Mike Turquetteb24764902012-03-15 23:11:19 -07002741/*** clk rate change notifiers ***/
2742
2743/**
2744 * clk_notifier_register - add a clk rate change notifier
2745 * @clk: struct clk * to watch
2746 * @nb: struct notifier_block * with callback info
2747 *
2748 * Request notification when clk's rate changes. This uses an SRCU
2749 * notifier because we want it to block and notifier unregistrations are
2750 * uncommon. The callbacks associated with the notifier must not
2751 * re-enter into the clk framework by calling any top-level clk APIs;
2752 * this will cause a nested prepare_lock mutex.
2753 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002754 * In all notification cases cases (pre, post and abort rate change) the
2755 * original clock rate is passed to the callback via struct
2756 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002757 * clk_notifier_data.new_rate.
2758 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002759 * clk_notifier_register() must be called from non-atomic context.
2760 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2761 * allocation failure; otherwise, passes along the return value of
2762 * srcu_notifier_chain_register().
2763 */
2764int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2765{
2766 struct clk_notifier *cn;
2767 int ret = -ENOMEM;
2768
2769 if (!clk || !nb)
2770 return -EINVAL;
2771
Mike Turquetteeab89f62013-03-28 13:59:01 -07002772 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002773
2774 /* search the list of notifiers for this clk */
2775 list_for_each_entry(cn, &clk_notifier_list, node)
2776 if (cn->clk == clk)
2777 break;
2778
2779 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2780 if (cn->clk != clk) {
2781 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2782 if (!cn)
2783 goto out;
2784
2785 cn->clk = clk;
2786 srcu_init_notifier_head(&cn->notifier_head);
2787
2788 list_add(&cn->node, &clk_notifier_list);
2789 }
2790
2791 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2792
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002793 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002794
2795out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002796 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002797
2798 return ret;
2799}
2800EXPORT_SYMBOL_GPL(clk_notifier_register);
2801
2802/**
2803 * clk_notifier_unregister - remove a clk rate change notifier
2804 * @clk: struct clk *
2805 * @nb: struct notifier_block * with callback info
2806 *
2807 * Request no further notification for changes to 'clk' and frees memory
2808 * allocated in clk_notifier_register.
2809 *
2810 * Returns -EINVAL if called with null arguments; otherwise, passes
2811 * along the return value of srcu_notifier_chain_unregister().
2812 */
2813int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2814{
2815 struct clk_notifier *cn = NULL;
2816 int ret = -EINVAL;
2817
2818 if (!clk || !nb)
2819 return -EINVAL;
2820
Mike Turquetteeab89f62013-03-28 13:59:01 -07002821 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002822
2823 list_for_each_entry(cn, &clk_notifier_list, node)
2824 if (cn->clk == clk)
2825 break;
2826
2827 if (cn->clk == clk) {
2828 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2829
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002830 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002831
2832 /* XXX the notifier code should handle this better */
2833 if (!cn->notifier_head.head) {
2834 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002835 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002836 kfree(cn);
2837 }
2838
2839 } else {
2840 ret = -ENOENT;
2841 }
2842
Mike Turquetteeab89f62013-03-28 13:59:01 -07002843 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002844
2845 return ret;
2846}
2847EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002848
2849#ifdef CONFIG_OF
2850/**
2851 * struct of_clk_provider - Clock provider registration structure
2852 * @link: Entry in global list of clock providers
2853 * @node: Pointer to device tree node of clock provider
2854 * @get: Get clock callback. Returns NULL or a struct clk for the
2855 * given clock specifier
2856 * @data: context pointer to be passed into @get callback
2857 */
2858struct of_clk_provider {
2859 struct list_head link;
2860
2861 struct device_node *node;
2862 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2863 void *data;
2864};
2865
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302866static const struct of_device_id __clk_of_table_sentinel
2867 __used __section(__clk_of_table_end);
2868
Grant Likely766e6a42012-04-09 14:50:06 -05002869static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002870static DEFINE_MUTEX(of_clk_mutex);
2871
Grant Likely766e6a42012-04-09 14:50:06 -05002872struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2873 void *data)
2874{
2875 return data;
2876}
2877EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2878
Shawn Guo494bfec2012-08-22 21:36:27 +08002879struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2880{
2881 struct clk_onecell_data *clk_data = data;
2882 unsigned int idx = clkspec->args[0];
2883
2884 if (idx >= clk_data->clk_num) {
2885 pr_err("%s: invalid clock index %d\n", __func__, idx);
2886 return ERR_PTR(-EINVAL);
2887 }
2888
2889 return clk_data->clks[idx];
2890}
2891EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2892
Grant Likely766e6a42012-04-09 14:50:06 -05002893/**
2894 * of_clk_add_provider() - Register a clock provider for a node
2895 * @np: Device node pointer associated with clock provider
2896 * @clk_src_get: callback for decoding clock
2897 * @data: context pointer for @clk_src_get callback.
2898 */
2899int of_clk_add_provider(struct device_node *np,
2900 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2901 void *data),
2902 void *data)
2903{
2904 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002905 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002906
2907 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2908 if (!cp)
2909 return -ENOMEM;
2910
2911 cp->node = of_node_get(np);
2912 cp->data = data;
2913 cp->get = clk_src_get;
2914
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002915 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002916 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002917 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002918 pr_debug("Added clock from %s\n", np->full_name);
2919
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002920 ret = of_clk_set_defaults(np, true);
2921 if (ret < 0)
2922 of_clk_del_provider(np);
2923
2924 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002925}
2926EXPORT_SYMBOL_GPL(of_clk_add_provider);
2927
2928/**
2929 * of_clk_del_provider() - Remove a previously registered clock provider
2930 * @np: Device node pointer associated with clock provider
2931 */
2932void of_clk_del_provider(struct device_node *np)
2933{
2934 struct of_clk_provider *cp;
2935
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002936 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002937 list_for_each_entry(cp, &of_clk_providers, link) {
2938 if (cp->node == np) {
2939 list_del(&cp->link);
2940 of_node_put(cp->node);
2941 kfree(cp);
2942 break;
2943 }
2944 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002945 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002946}
2947EXPORT_SYMBOL_GPL(of_clk_del_provider);
2948
Stephen Boyd73e0e492015-02-06 11:42:43 -08002949struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
2950 const char *dev_id, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -05002951{
2952 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002953 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002954
Stephen Boyd306c3422015-02-05 15:39:11 -08002955 if (!clkspec)
2956 return ERR_PTR(-EINVAL);
2957
Grant Likely766e6a42012-04-09 14:50:06 -05002958 /* Check if we have such a provider in our array */
Stephen Boyd306c3422015-02-05 15:39:11 -08002959 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002960 list_for_each_entry(provider, &of_clk_providers, link) {
2961 if (provider->node == clkspec->np)
2962 clk = provider->get(clkspec, provider->data);
Stephen Boyd73e0e492015-02-06 11:42:43 -08002963 if (!IS_ERR(clk)) {
2964 clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
2965 con_id);
2966
2967 if (!IS_ERR(clk) && !__clk_get(clk)) {
2968 __clk_free_clk(clk);
2969 clk = ERR_PTR(-ENOENT);
2970 }
2971
Grant Likely766e6a42012-04-09 14:50:06 -05002972 break;
Stephen Boyd73e0e492015-02-06 11:42:43 -08002973 }
Grant Likely766e6a42012-04-09 14:50:06 -05002974 }
Stephen Boyd306c3422015-02-05 15:39:11 -08002975 mutex_unlock(&of_clk_mutex);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002976
2977 return clk;
2978}
2979
Stephen Boyd306c3422015-02-05 15:39:11 -08002980/**
2981 * of_clk_get_from_provider() - Lookup a clock from a clock provider
2982 * @clkspec: pointer to a clock specifier data structure
2983 *
2984 * This function looks up a struct clk from the registered list of clock
2985 * providers, an input is a clock specifier data structure as returned
2986 * from the of_parse_phandle_with_args() function call.
2987 */
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002988struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2989{
Stephen Boyd306c3422015-02-05 15:39:11 -08002990 return __of_clk_get_from_provider(clkspec, NULL, __func__);
Grant Likely766e6a42012-04-09 14:50:06 -05002991}
2992
Mike Turquettef6102742013-10-07 23:12:13 -07002993int of_clk_get_parent_count(struct device_node *np)
2994{
2995 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2996}
2997EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2998
Grant Likely766e6a42012-04-09 14:50:06 -05002999const char *of_clk_get_parent_name(struct device_node *np, int index)
3000{
3001 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003002 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05003003 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003004 const __be32 *vp;
3005 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05003006 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003007 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05003008
3009 if (index < 0)
3010 return NULL;
3011
3012 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3013 &clkspec);
3014 if (rc)
3015 return NULL;
3016
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003017 index = clkspec.args_count ? clkspec.args[0] : 0;
3018 count = 0;
3019
3020 /* if there is an indices property, use it to transfer the index
3021 * specified into an array offset for the clock-output-names property.
3022 */
3023 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3024 if (index == pv) {
3025 index = count;
3026 break;
3027 }
3028 count++;
3029 }
3030
Grant Likely766e6a42012-04-09 14:50:06 -05003031 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00003032 index,
Grant Likely766e6a42012-04-09 14:50:06 -05003033 &clk_name) < 0)
3034 clk_name = clkspec.np->name;
3035
3036 of_node_put(clkspec.np);
3037 return clk_name;
3038}
3039EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3040
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003041struct clock_provider {
3042 of_clk_init_cb_t clk_init_cb;
3043 struct device_node *np;
3044 struct list_head node;
3045};
3046
3047static LIST_HEAD(clk_provider_list);
3048
3049/*
3050 * This function looks for a parent clock. If there is one, then it
3051 * checks that the provider for this parent clock was initialized, in
3052 * this case the parent clock will be ready.
3053 */
3054static int parent_ready(struct device_node *np)
3055{
3056 int i = 0;
3057
3058 while (true) {
3059 struct clk *clk = of_clk_get(np, i);
3060
3061 /* this parent is ready we can check the next one */
3062 if (!IS_ERR(clk)) {
3063 clk_put(clk);
3064 i++;
3065 continue;
3066 }
3067
3068 /* at least one parent is not ready, we exit now */
3069 if (PTR_ERR(clk) == -EPROBE_DEFER)
3070 return 0;
3071
3072 /*
3073 * Here we make assumption that the device tree is
3074 * written correctly. So an error means that there is
3075 * no more parent. As we didn't exit yet, then the
3076 * previous parent are ready. If there is no clock
3077 * parent, no need to wait for them, then we can
3078 * consider their absence as being ready
3079 */
3080 return 1;
3081 }
3082}
3083
Grant Likely766e6a42012-04-09 14:50:06 -05003084/**
3085 * of_clk_init() - Scan and init clock providers from the DT
3086 * @matches: array of compatible values and init functions for providers.
3087 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003088 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01003089 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003090 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05003091 */
3092void __init of_clk_init(const struct of_device_id *matches)
3093{
Alex Elder7f7ed582013-08-22 11:31:31 -05003094 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05003095 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003096 struct clock_provider *clk_provider, *next;
3097 bool is_init_done;
3098 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05003099
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303100 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03003101 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05303102
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003103 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05003104 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003105 struct clock_provider *parent =
3106 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
3107
3108 parent->clk_init_cb = match->data;
3109 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01003110 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003111 }
3112
3113 while (!list_empty(&clk_provider_list)) {
3114 is_init_done = false;
3115 list_for_each_entry_safe(clk_provider, next,
3116 &clk_provider_list, node) {
3117 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003118
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003119 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02003120 of_clk_set_defaults(clk_provider->np, true);
3121
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003122 list_del(&clk_provider->node);
3123 kfree(clk_provider);
3124 is_init_done = true;
3125 }
3126 }
3127
3128 /*
Sylwester Nawrockie5ca8fb42014-03-27 12:08:36 +01003129 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01003130 * remaining providers during the last loop, so now we
3131 * initialize all the remaining ones unconditionally
3132 * in case the clock parent was not mandatory
3133 */
3134 if (!is_init_done)
3135 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05003136 }
3137}
3138#endif