blob: cdc1fa58e4f1810500540fdd2d9aba4496dac4ca [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
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010040static long clk_core_get_accuracy(struct clk_core *clk);
41static unsigned long clk_core_get_rate(struct clk_core *clk);
42static int clk_core_get_phase(struct clk_core *clk);
43static bool clk_core_is_prepared(struct clk_core *clk);
44static bool clk_core_is_enabled(struct clk_core *clk);
45static unsigned long clk_core_round_rate_nolock(struct clk_core *clk,
46 unsigned long rate);
47static struct clk_core *clk_core_lookup(const char *name);
48
Michael Turquetteb09d6d92015-01-29 14:22:50 -080049/*** private data structures ***/
50
51struct clk_core {
52 const char *name;
53 const struct clk_ops *ops;
54 struct clk_hw *hw;
55 struct module *owner;
56 struct clk_core *parent;
57 const char **parent_names;
58 struct clk_core **parents;
59 u8 num_parents;
60 u8 new_parent_index;
61 unsigned long rate;
62 unsigned long new_rate;
63 struct clk_core *new_parent;
64 struct clk_core *new_child;
65 unsigned long flags;
66 unsigned int enable_count;
67 unsigned int prepare_count;
68 unsigned long accuracy;
69 int phase;
70 struct hlist_head children;
71 struct hlist_node child_node;
72 struct hlist_node debug_node;
73 unsigned int notifier_count;
74#ifdef CONFIG_DEBUG_FS
75 struct dentry *dentry;
76#endif
77 struct kref ref;
78};
79
80struct clk {
81 struct clk_core *core;
82 const char *dev_id;
83 const char *con_id;
84};
85
Mike Turquetteeab89f62013-03-28 13:59:01 -070086/*** locking ***/
87static void clk_prepare_lock(void)
88{
Mike Turquette533ddeb2013-03-28 13:59:02 -070089 if (!mutex_trylock(&prepare_lock)) {
90 if (prepare_owner == current) {
91 prepare_refcnt++;
92 return;
93 }
94 mutex_lock(&prepare_lock);
95 }
96 WARN_ON_ONCE(prepare_owner != NULL);
97 WARN_ON_ONCE(prepare_refcnt != 0);
98 prepare_owner = current;
99 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700100}
101
102static void clk_prepare_unlock(void)
103{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700104 WARN_ON_ONCE(prepare_owner != current);
105 WARN_ON_ONCE(prepare_refcnt == 0);
106
107 if (--prepare_refcnt)
108 return;
109 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700110 mutex_unlock(&prepare_lock);
111}
112
113static unsigned long clk_enable_lock(void)
114{
115 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -0700116
117 if (!spin_trylock_irqsave(&enable_lock, flags)) {
118 if (enable_owner == current) {
119 enable_refcnt++;
120 return flags;
121 }
122 spin_lock_irqsave(&enable_lock, flags);
123 }
124 WARN_ON_ONCE(enable_owner != NULL);
125 WARN_ON_ONCE(enable_refcnt != 0);
126 enable_owner = current;
127 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700128 return flags;
129}
130
131static void clk_enable_unlock(unsigned long flags)
132{
Mike Turquette533ddeb2013-03-28 13:59:02 -0700133 WARN_ON_ONCE(enable_owner != current);
134 WARN_ON_ONCE(enable_refcnt == 0);
135
136 if (--enable_refcnt)
137 return;
138 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700139 spin_unlock_irqrestore(&enable_lock, flags);
140}
141
Mike Turquetteb24764902012-03-15 23:11:19 -0700142/*** debugfs support ***/
143
Mike Turquetteea72dc22013-12-18 21:38:52 -0800144#ifdef CONFIG_DEBUG_FS
Mike Turquetteb24764902012-03-15 23:11:19 -0700145#include <linux/debugfs.h>
146
147static struct dentry *rootdir;
Mike Turquetteb24764902012-03-15 23:11:19 -0700148static int inited = 0;
Stephen Boyd6314b672014-09-04 23:37:49 -0700149static DEFINE_MUTEX(clk_debug_lock);
150static HLIST_HEAD(clk_debug_list);
Mike Turquetteb24764902012-03-15 23:11:19 -0700151
Sachin Kamat6b44c8542014-07-01 11:56:34 +0530152static struct hlist_head *all_lists[] = {
153 &clk_root_list,
154 &clk_orphan_list,
155 NULL,
156};
157
158static struct hlist_head *orphan_list[] = {
159 &clk_orphan_list,
160 NULL,
161};
162
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100163static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
164 int level)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530165{
166 if (!c)
167 return;
168
Mike Turquettee59c5372014-02-18 21:21:25 -0800169 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530170 level * 3 + 1, "",
171 30 - level * 3, c->name,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100172 c->enable_count, c->prepare_count, clk_core_get_rate(c),
173 clk_core_get_accuracy(c), clk_core_get_phase(c));
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530174}
175
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100176static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530177 int level)
178{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100179 struct clk_core *child;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530180
181 if (!c)
182 return;
183
184 clk_summary_show_one(s, c, level);
185
Sasha Levinb67bfe02013-02-27 17:06:00 -0800186 hlist_for_each_entry(child, &c->children, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530187 clk_summary_show_subtree(s, child, level + 1);
188}
189
190static int clk_summary_show(struct seq_file *s, void *data)
191{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100192 struct clk_core *c;
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300193 struct hlist_head **lists = (struct hlist_head **)s->private;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530194
Mike Turquettee59c5372014-02-18 21:21:25 -0800195 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
196 seq_puts(s, "----------------------------------------------------------------------------------------\n");
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530197
Mike Turquetteeab89f62013-03-28 13:59:01 -0700198 clk_prepare_lock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530199
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300200 for (; *lists; lists++)
201 hlist_for_each_entry(c, *lists, child_node)
202 clk_summary_show_subtree(s, c, 0);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530203
Mike Turquetteeab89f62013-03-28 13:59:01 -0700204 clk_prepare_unlock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530205
206 return 0;
207}
208
209
210static int clk_summary_open(struct inode *inode, struct file *file)
211{
212 return single_open(file, clk_summary_show, inode->i_private);
213}
214
215static const struct file_operations clk_summary_fops = {
216 .open = clk_summary_open,
217 .read = seq_read,
218 .llseek = seq_lseek,
219 .release = single_release,
220};
221
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100222static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530223{
224 if (!c)
225 return;
226
227 seq_printf(s, "\"%s\": { ", c->name);
228 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
229 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100230 seq_printf(s, "\"rate\": %lu", clk_core_get_rate(c));
231 seq_printf(s, "\"accuracy\": %lu", clk_core_get_accuracy(c));
232 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530233}
234
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100235static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530236{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100237 struct clk_core *child;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530238
239 if (!c)
240 return;
241
242 clk_dump_one(s, c, level);
243
Sasha Levinb67bfe02013-02-27 17:06:00 -0800244 hlist_for_each_entry(child, &c->children, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530245 seq_printf(s, ",");
246 clk_dump_subtree(s, child, level + 1);
247 }
248
249 seq_printf(s, "}");
250}
251
252static int clk_dump(struct seq_file *s, void *data)
253{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100254 struct clk_core *c;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530255 bool first_node = true;
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300256 struct hlist_head **lists = (struct hlist_head **)s->private;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530257
258 seq_printf(s, "{");
259
Mike Turquetteeab89f62013-03-28 13:59:01 -0700260 clk_prepare_lock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530261
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300262 for (; *lists; lists++) {
263 hlist_for_each_entry(c, *lists, child_node) {
264 if (!first_node)
265 seq_puts(s, ",");
266 first_node = false;
267 clk_dump_subtree(s, c, 0);
268 }
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530269 }
270
Mike Turquetteeab89f62013-03-28 13:59:01 -0700271 clk_prepare_unlock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530272
273 seq_printf(s, "}");
274 return 0;
275}
276
277
278static int clk_dump_open(struct inode *inode, struct file *file)
279{
280 return single_open(file, clk_dump, inode->i_private);
281}
282
283static const struct file_operations clk_dump_fops = {
284 .open = clk_dump_open,
285 .read = seq_read,
286 .llseek = seq_lseek,
287 .release = single_release,
288};
289
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100290static int clk_debug_create_one(struct clk_core *clk, struct dentry *pdentry)
Mike Turquetteb24764902012-03-15 23:11:19 -0700291{
292 struct dentry *d;
293 int ret = -ENOMEM;
294
295 if (!clk || !pdentry) {
296 ret = -EINVAL;
297 goto out;
298 }
299
300 d = debugfs_create_dir(clk->name, pdentry);
301 if (!d)
302 goto out;
303
304 clk->dentry = d;
305
306 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
307 (u32 *)&clk->rate);
308 if (!d)
309 goto err_out;
310
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100311 d = debugfs_create_u32("clk_accuracy", S_IRUGO, clk->dentry,
312 (u32 *)&clk->accuracy);
313 if (!d)
314 goto err_out;
315
Mike Turquettee59c5372014-02-18 21:21:25 -0800316 d = debugfs_create_u32("clk_phase", S_IRUGO, clk->dentry,
317 (u32 *)&clk->phase);
318 if (!d)
319 goto err_out;
320
Mike Turquetteb24764902012-03-15 23:11:19 -0700321 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
322 (u32 *)&clk->flags);
323 if (!d)
324 goto err_out;
325
326 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
327 (u32 *)&clk->prepare_count);
328 if (!d)
329 goto err_out;
330
331 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
332 (u32 *)&clk->enable_count);
333 if (!d)
334 goto err_out;
335
336 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
337 (u32 *)&clk->notifier_count);
338 if (!d)
339 goto err_out;
340
Chris Brandabeab452014-07-03 14:01:29 -0700341 if (clk->ops->debug_init) {
342 ret = clk->ops->debug_init(clk->hw, clk->dentry);
343 if (ret)
Alex Elderc646cbf2014-03-21 06:43:56 -0500344 goto err_out;
Chris Brandabeab452014-07-03 14:01:29 -0700345 }
Alex Elderc646cbf2014-03-21 06:43:56 -0500346
Mike Turquetteb24764902012-03-15 23:11:19 -0700347 ret = 0;
348 goto out;
349
350err_out:
Alex Elderb5f98e62013-11-27 09:39:49 -0600351 debugfs_remove_recursive(clk->dentry);
352 clk->dentry = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -0700353out:
354 return ret;
355}
356
Mike Turquetteb24764902012-03-15 23:11:19 -0700357/**
358 * clk_debug_register - add a clk node to the debugfs clk tree
359 * @clk: the clk being added to the debugfs clk tree
360 *
361 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
362 * initialized. Otherwise it bails out early since the debugfs clk tree
363 * will be created lazily by clk_debug_init as part of a late_initcall.
Mike Turquetteb24764902012-03-15 23:11:19 -0700364 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100365static int clk_debug_register(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700366{
Mike Turquetteb24764902012-03-15 23:11:19 -0700367 int ret = 0;
368
Stephen Boyd6314b672014-09-04 23:37:49 -0700369 mutex_lock(&clk_debug_lock);
370 hlist_add_head(&clk->debug_node, &clk_debug_list);
371
Mike Turquetteb24764902012-03-15 23:11:19 -0700372 if (!inited)
Stephen Boyd6314b672014-09-04 23:37:49 -0700373 goto unlock;
Mike Turquetteb24764902012-03-15 23:11:19 -0700374
Stephen Boyd6314b672014-09-04 23:37:49 -0700375 ret = clk_debug_create_one(clk, rootdir);
376unlock:
377 mutex_unlock(&clk_debug_lock);
Mike Turquetteb24764902012-03-15 23:11:19 -0700378
Mike Turquetteb24764902012-03-15 23:11:19 -0700379 return ret;
380}
381
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200382 /**
383 * clk_debug_unregister - remove a clk node from the debugfs clk tree
384 * @clk: the clk being removed from the debugfs clk tree
385 *
386 * Dynamically removes a clk and all it's children clk nodes from the
387 * debugfs clk tree if clk->dentry points to debugfs created by
388 * clk_debug_register in __clk_init.
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200389 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100390static void clk_debug_unregister(struct clk_core *clk)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200391{
Stephen Boyd6314b672014-09-04 23:37:49 -0700392 mutex_lock(&clk_debug_lock);
Stephen Boyd6314b672014-09-04 23:37:49 -0700393 hlist_del_init(&clk->debug_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200394 debugfs_remove_recursive(clk->dentry);
Stephen Boyd6314b672014-09-04 23:37:49 -0700395 clk->dentry = NULL;
Stephen Boyd6314b672014-09-04 23:37:49 -0700396 mutex_unlock(&clk_debug_lock);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200397}
398
Tomeu Vizoso61c7cdd2014-12-02 08:54:21 +0100399struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
Peter De Schrijverfb2b3c92014-06-26 18:00:53 +0300400 void *data, const struct file_operations *fops)
401{
402 struct dentry *d = NULL;
403
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100404 if (hw->core->dentry)
405 d = debugfs_create_file(name, mode, hw->core->dentry, data,
406 fops);
Peter De Schrijverfb2b3c92014-06-26 18:00:53 +0300407
408 return d;
409}
410EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
411
Mike Turquetteb24764902012-03-15 23:11:19 -0700412/**
413 * clk_debug_init - lazily create the debugfs clk tree visualization
414 *
415 * clks are often initialized very early during boot before memory can
416 * be dynamically allocated and well before debugfs is setup.
417 * clk_debug_init walks the clk tree hierarchy while holding
418 * prepare_lock and creates the topology as part of a late_initcall,
419 * thus insuring that clks initialized very early will still be
420 * represented in the debugfs clk tree. This function should only be
421 * called once at boot-time, and all other clks added dynamically will
422 * be done so with clk_debug_register.
423 */
424static int __init clk_debug_init(void)
425{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100426 struct clk_core *clk;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530427 struct dentry *d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700428
429 rootdir = debugfs_create_dir("clk", NULL);
430
431 if (!rootdir)
432 return -ENOMEM;
433
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300434 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530435 &clk_summary_fops);
436 if (!d)
437 return -ENOMEM;
438
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300439 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530440 &clk_dump_fops);
441 if (!d)
442 return -ENOMEM;
443
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300444 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
445 &orphan_list, &clk_summary_fops);
446 if (!d)
447 return -ENOMEM;
Mike Turquetteb24764902012-03-15 23:11:19 -0700448
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300449 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
450 &orphan_list, &clk_dump_fops);
451 if (!d)
Mike Turquetteb24764902012-03-15 23:11:19 -0700452 return -ENOMEM;
453
Stephen Boyd6314b672014-09-04 23:37:49 -0700454 mutex_lock(&clk_debug_lock);
455 hlist_for_each_entry(clk, &clk_debug_list, debug_node)
456 clk_debug_create_one(clk, rootdir);
Mike Turquetteb24764902012-03-15 23:11:19 -0700457
458 inited = 1;
Stephen Boyd6314b672014-09-04 23:37:49 -0700459 mutex_unlock(&clk_debug_lock);
Mike Turquetteb24764902012-03-15 23:11:19 -0700460
461 return 0;
462}
463late_initcall(clk_debug_init);
464#else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100465static inline int clk_debug_register(struct clk_core *clk) { return 0; }
466static inline void clk_debug_reparent(struct clk_core *clk,
467 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200468{
469}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100470static inline void clk_debug_unregister(struct clk_core *clk)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200471{
472}
Mike Turquette70d347e2012-03-26 11:53:47 -0700473#endif
Mike Turquetteb24764902012-03-15 23:11:19 -0700474
Mike Turquetteb24764902012-03-15 23:11:19 -0700475/* caller must hold prepare_lock */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100476static void clk_unprepare_unused_subtree(struct clk_core *clk)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100477{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100478 struct clk_core *child;
Ulf Hansson1c155b32013-03-12 20:26:03 +0100479
Ulf Hansson1c155b32013-03-12 20:26:03 +0100480 hlist_for_each_entry(child, &clk->children, child_node)
481 clk_unprepare_unused_subtree(child);
482
483 if (clk->prepare_count)
484 return;
485
486 if (clk->flags & CLK_IGNORE_UNUSED)
487 return;
488
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100489 if (clk_core_is_prepared(clk)) {
Ulf Hansson3cc82472013-03-12 20:26:04 +0100490 if (clk->ops->unprepare_unused)
491 clk->ops->unprepare_unused(clk->hw);
492 else if (clk->ops->unprepare)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100493 clk->ops->unprepare(clk->hw);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100494 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100495}
496
497/* caller must hold prepare_lock */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100498static void clk_disable_unused_subtree(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700499{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100500 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700501 unsigned long flags;
502
Sasha Levinb67bfe02013-02-27 17:06:00 -0800503 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700504 clk_disable_unused_subtree(child);
505
Mike Turquetteeab89f62013-03-28 13:59:01 -0700506 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700507
508 if (clk->enable_count)
509 goto unlock_out;
510
511 if (clk->flags & CLK_IGNORE_UNUSED)
512 goto unlock_out;
513
Mike Turquette7c045a52012-12-04 11:00:35 -0800514 /*
515 * some gate clocks have special needs during the disable-unused
516 * sequence. call .disable_unused if available, otherwise fall
517 * back to .disable
518 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100519 if (clk_core_is_enabled(clk)) {
Mike Turquette7c045a52012-12-04 11:00:35 -0800520 if (clk->ops->disable_unused)
521 clk->ops->disable_unused(clk->hw);
522 else if (clk->ops->disable)
523 clk->ops->disable(clk->hw);
524 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700525
526unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700527 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700528}
529
Olof Johansson1e435252013-04-27 14:10:18 -0700530static bool clk_ignore_unused;
531static int __init clk_ignore_unused_setup(char *__unused)
532{
533 clk_ignore_unused = true;
534 return 1;
535}
536__setup("clk_ignore_unused", clk_ignore_unused_setup);
537
Mike Turquetteb24764902012-03-15 23:11:19 -0700538static int clk_disable_unused(void)
539{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100540 struct clk_core *clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700541
Olof Johansson1e435252013-04-27 14:10:18 -0700542 if (clk_ignore_unused) {
543 pr_warn("clk: Not disabling unused clocks\n");
544 return 0;
545 }
546
Mike Turquetteeab89f62013-03-28 13:59:01 -0700547 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700548
Sasha Levinb67bfe02013-02-27 17:06:00 -0800549 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700550 clk_disable_unused_subtree(clk);
551
Sasha Levinb67bfe02013-02-27 17:06:00 -0800552 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700553 clk_disable_unused_subtree(clk);
554
Ulf Hansson1c155b32013-03-12 20:26:03 +0100555 hlist_for_each_entry(clk, &clk_root_list, child_node)
556 clk_unprepare_unused_subtree(clk);
557
558 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
559 clk_unprepare_unused_subtree(clk);
560
Mike Turquetteeab89f62013-03-28 13:59:01 -0700561 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700562
563 return 0;
564}
Saravana Kannand41d5802013-05-09 11:35:01 -0700565late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700566
567/*** helper functions ***/
568
Russ Dill65800b22012-11-26 11:20:09 -0800569const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700570{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100571 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700572}
Niels de Vos48950842012-12-13 13:12:25 +0100573EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700574
Russ Dill65800b22012-11-26 11:20:09 -0800575struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700576{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100577 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700578}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800579EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700580
Russ Dill65800b22012-11-26 11:20:09 -0800581u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700582{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100583 return !clk ? 0 : clk->core->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700584}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800585EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700586
Russ Dill65800b22012-11-26 11:20:09 -0800587struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700588{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100589 if (!clk)
590 return NULL;
591
592 /* TODO: Create a per-user clk and change callers to call clk_put */
593 return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700594}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800595EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700596
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100597static struct clk_core *clk_core_get_parent_by_index(struct clk_core *clk,
598 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100599{
600 if (!clk || index >= clk->num_parents)
601 return NULL;
602 else if (!clk->parents)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100603 return clk_core_lookup(clk->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100604 else if (!clk->parents[index])
605 return clk->parents[index] =
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100606 clk_core_lookup(clk->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100607 else
608 return clk->parents[index];
609}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100610
611struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
612{
613 struct clk_core *parent;
614
615 if (!clk)
616 return NULL;
617
618 parent = clk_core_get_parent_by_index(clk->core, index);
619
620 return !parent ? NULL : parent->hw->clk;
621}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800622EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100623
Russ Dill65800b22012-11-26 11:20:09 -0800624unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700625{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100626 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700627}
628
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100629static unsigned long clk_core_get_rate_nolock(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700630{
631 unsigned long ret;
632
633 if (!clk) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530634 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700635 goto out;
636 }
637
638 ret = clk->rate;
639
640 if (clk->flags & CLK_IS_ROOT)
641 goto out;
642
643 if (!clk->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530644 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700645
646out:
647 return ret;
648}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100649
650unsigned long __clk_get_rate(struct clk *clk)
651{
652 if (!clk)
653 return 0;
654
655 return clk_core_get_rate_nolock(clk->core);
656}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800657EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700658
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100659static unsigned long __clk_get_accuracy(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100660{
661 if (!clk)
662 return 0;
663
664 return clk->accuracy;
665}
666
Russ Dill65800b22012-11-26 11:20:09 -0800667unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700668{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100669 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700670}
Thierry Redingb05c6832013-09-03 09:43:51 +0200671EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700672
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100673static bool clk_core_is_prepared(struct clk_core *clk)
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100674{
675 int ret;
676
677 if (!clk)
678 return false;
679
680 /*
681 * .is_prepared is optional for clocks that can prepare
682 * fall back to software usage counter if it is missing
683 */
684 if (!clk->ops->is_prepared) {
685 ret = clk->prepare_count ? 1 : 0;
686 goto out;
687 }
688
689 ret = clk->ops->is_prepared(clk->hw);
690out:
691 return !!ret;
692}
693
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100694bool __clk_is_prepared(struct clk *clk)
695{
696 if (!clk)
697 return false;
698
699 return clk_core_is_prepared(clk->core);
700}
701
702static bool clk_core_is_enabled(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700703{
704 int ret;
705
706 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700707 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700708
709 /*
710 * .is_enabled is only mandatory for clocks that gate
711 * fall back to software usage counter if .is_enabled is missing
712 */
713 if (!clk->ops->is_enabled) {
714 ret = clk->enable_count ? 1 : 0;
715 goto out;
716 }
717
718 ret = clk->ops->is_enabled(clk->hw);
719out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700720 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700721}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100722
723bool __clk_is_enabled(struct clk *clk)
724{
725 if (!clk)
726 return false;
727
728 return clk_core_is_enabled(clk->core);
729}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800730EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700731
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100732static struct clk_core *__clk_lookup_subtree(const char *name,
733 struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700734{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100735 struct clk_core *child;
736 struct clk_core *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700737
738 if (!strcmp(clk->name, name))
739 return clk;
740
Sasha Levinb67bfe02013-02-27 17:06:00 -0800741 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700742 ret = __clk_lookup_subtree(name, child);
743 if (ret)
744 return ret;
745 }
746
747 return NULL;
748}
749
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100750static struct clk_core *clk_core_lookup(const char *name)
Mike Turquetteb24764902012-03-15 23:11:19 -0700751{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100752 struct clk_core *root_clk;
753 struct clk_core *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700754
755 if (!name)
756 return NULL;
757
758 /* search the 'proper' clk tree first */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800759 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700760 ret = __clk_lookup_subtree(name, root_clk);
761 if (ret)
762 return ret;
763 }
764
765 /* if not found, then search the orphan tree */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800766 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700767 ret = __clk_lookup_subtree(name, root_clk);
768 if (ret)
769 return ret;
770 }
771
772 return NULL;
773}
774
Stephen Boyd15a02c12015-01-19 18:05:28 -0800775static bool mux_is_better_rate(unsigned long rate, unsigned long now,
776 unsigned long best, unsigned long flags)
777{
778 if (flags & CLK_MUX_ROUND_CLOSEST)
779 return abs(now - rate) < abs(best - rate);
780
781 return now <= rate && now > best;
782}
783
784static long
785clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate,
786 unsigned long *best_parent_rate,
787 struct clk_hw **best_parent_p,
788 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100789{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100790 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
James Hogane366fdd2013-07-29 12:25:02 +0100791 int i, num_parents;
792 unsigned long parent_rate, best = 0;
793
794 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100795 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
796 parent = core->parent;
797 if (core->flags & CLK_SET_RATE_PARENT)
798 best = clk_core_round_rate_nolock(parent, rate);
James Hogane366fdd2013-07-29 12:25:02 +0100799 else if (parent)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100800 best = clk_core_get_rate_nolock(parent);
James Hogane366fdd2013-07-29 12:25:02 +0100801 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100802 best = clk_core_get_rate_nolock(core);
James Hogane366fdd2013-07-29 12:25:02 +0100803 goto out;
804 }
805
806 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100807 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100808 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100809 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100810 if (!parent)
811 continue;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100812 if (core->flags & CLK_SET_RATE_PARENT)
813 parent_rate = clk_core_round_rate_nolock(parent, rate);
James Hogane366fdd2013-07-29 12:25:02 +0100814 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100815 parent_rate = clk_core_get_rate_nolock(parent);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800816 if (mux_is_better_rate(rate, parent_rate, best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100817 best_parent = parent;
818 best = parent_rate;
819 }
820 }
821
822out:
823 if (best_parent)
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100824 *best_parent_p = best_parent->hw;
James Hogane366fdd2013-07-29 12:25:02 +0100825 *best_parent_rate = best;
826
827 return best;
828}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800829
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100830struct clk *__clk_lookup(const char *name)
831{
832 struct clk_core *core = clk_core_lookup(name);
833
834 return !core ? NULL : core->hw->clk;
835}
836
Stephen Boyd15a02c12015-01-19 18:05:28 -0800837/*
838 * Helper for finding best parent to provide a given frequency. This can be used
839 * directly as a determine_rate callback (e.g. for a mux), or from a more
840 * complex clock that may combine a mux with other operations.
841 */
842long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
843 unsigned long *best_parent_rate,
844 struct clk_hw **best_parent_p)
845{
846 return clk_mux_determine_rate_flags(hw, rate, best_parent_rate,
847 best_parent_p, 0);
848}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800849EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100850
Stephen Boyd15a02c12015-01-19 18:05:28 -0800851long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate,
852 unsigned long *best_parent_rate,
853 struct clk_hw **best_parent_p)
854{
855 return clk_mux_determine_rate_flags(hw, rate, best_parent_rate,
856 best_parent_p,
857 CLK_MUX_ROUND_CLOSEST);
858}
859EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
860
Mike Turquetteb24764902012-03-15 23:11:19 -0700861/*** clk api ***/
862
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100863static void clk_core_unprepare(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700864{
865 if (!clk)
866 return;
867
868 if (WARN_ON(clk->prepare_count == 0))
869 return;
870
871 if (--clk->prepare_count > 0)
872 return;
873
874 WARN_ON(clk->enable_count > 0);
875
876 if (clk->ops->unprepare)
877 clk->ops->unprepare(clk->hw);
878
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100879 clk_core_unprepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700880}
881
882/**
883 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200884 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700885 *
886 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
887 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
888 * if the operation may sleep. One example is a clk which is accessed over
889 * I2c. In the complex case a clk gate operation may require a fast and a slow
890 * part. It is this reason that clk_unprepare and clk_disable are not mutually
891 * exclusive. In fact clk_disable must be called before clk_unprepare.
892 */
893void clk_unprepare(struct clk *clk)
894{
Stephen Boyd63589e92014-03-26 16:06:37 -0700895 if (IS_ERR_OR_NULL(clk))
896 return;
897
Mike Turquetteeab89f62013-03-28 13:59:01 -0700898 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100899 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700900 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700901}
902EXPORT_SYMBOL_GPL(clk_unprepare);
903
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100904static int clk_core_prepare(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700905{
906 int ret = 0;
907
908 if (!clk)
909 return 0;
910
911 if (clk->prepare_count == 0) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100912 ret = clk_core_prepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700913 if (ret)
914 return ret;
915
916 if (clk->ops->prepare) {
917 ret = clk->ops->prepare(clk->hw);
918 if (ret) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100919 clk_core_unprepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700920 return ret;
921 }
922 }
923 }
924
925 clk->prepare_count++;
926
927 return 0;
928}
929
930/**
931 * clk_prepare - prepare a clock source
932 * @clk: the clk being prepared
933 *
934 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
935 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
936 * operation may sleep. One example is a clk which is accessed over I2c. In
937 * the complex case a clk ungate operation may require a fast and a slow part.
938 * It is this reason that clk_prepare and clk_enable are not mutually
939 * exclusive. In fact clk_prepare must be called before clk_enable.
940 * Returns 0 on success, -EERROR otherwise.
941 */
942int clk_prepare(struct clk *clk)
943{
944 int ret;
945
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100946 if (!clk)
947 return 0;
948
Mike Turquetteeab89f62013-03-28 13:59:01 -0700949 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100950 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700951 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700952
953 return ret;
954}
955EXPORT_SYMBOL_GPL(clk_prepare);
956
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100957static void clk_core_disable(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700958{
959 if (!clk)
960 return;
961
962 if (WARN_ON(clk->enable_count == 0))
963 return;
964
965 if (--clk->enable_count > 0)
966 return;
967
968 if (clk->ops->disable)
969 clk->ops->disable(clk->hw);
970
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100971 clk_core_disable(clk->parent);
972}
973
974static void __clk_disable(struct clk *clk)
975{
976 if (!clk)
977 return;
978
979 clk_core_disable(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700980}
981
982/**
983 * clk_disable - gate a clock
984 * @clk: the clk being gated
985 *
986 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
987 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
988 * clk if the operation is fast and will never sleep. One example is a
989 * SoC-internal clk which is controlled via simple register writes. In the
990 * complex case a clk gate operation may require a fast and a slow part. It is
991 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
992 * In fact clk_disable must be called before clk_unprepare.
993 */
994void clk_disable(struct clk *clk)
995{
996 unsigned long flags;
997
Stephen Boyd63589e92014-03-26 16:06:37 -0700998 if (IS_ERR_OR_NULL(clk))
999 return;
1000
Mike Turquetteeab89f62013-03-28 13:59:01 -07001001 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001002 __clk_disable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001003 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001004}
1005EXPORT_SYMBOL_GPL(clk_disable);
1006
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001007static int clk_core_enable(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001008{
1009 int ret = 0;
1010
1011 if (!clk)
1012 return 0;
1013
1014 if (WARN_ON(clk->prepare_count == 0))
1015 return -ESHUTDOWN;
1016
1017 if (clk->enable_count == 0) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001018 ret = clk_core_enable(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -07001019
1020 if (ret)
1021 return ret;
1022
1023 if (clk->ops->enable) {
1024 ret = clk->ops->enable(clk->hw);
1025 if (ret) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001026 clk_core_disable(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -07001027 return ret;
1028 }
1029 }
1030 }
1031
1032 clk->enable_count++;
1033 return 0;
1034}
1035
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001036static int __clk_enable(struct clk *clk)
1037{
1038 if (!clk)
1039 return 0;
1040
1041 return clk_core_enable(clk->core);
1042}
1043
Mike Turquetteb24764902012-03-15 23:11:19 -07001044/**
1045 * clk_enable - ungate a clock
1046 * @clk: the clk being ungated
1047 *
1048 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1049 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1050 * if the operation will never sleep. One example is a SoC-internal clk which
1051 * is controlled via simple register writes. In the complex case a clk ungate
1052 * operation may require a fast and a slow part. It is this reason that
1053 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1054 * must be called before clk_enable. Returns 0 on success, -EERROR
1055 * otherwise.
1056 */
1057int clk_enable(struct clk *clk)
1058{
1059 unsigned long flags;
1060 int ret;
1061
Mike Turquetteeab89f62013-03-28 13:59:01 -07001062 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001063 ret = __clk_enable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001064 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001065
1066 return ret;
1067}
1068EXPORT_SYMBOL_GPL(clk_enable);
1069
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001070static unsigned long clk_core_round_rate_nolock(struct clk_core *clk,
1071 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001072{
Shawn Guo81536e02012-04-12 20:50:17 +08001073 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001074 struct clk_core *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001075 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -07001076
1077 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001078 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001079
James Hogan71472c02013-07-29 12:25:00 +01001080 parent = clk->parent;
1081 if (parent)
1082 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001083
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001084 if (clk->ops->determine_rate) {
1085 parent_hw = parent ? parent->hw : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001086 return clk->ops->determine_rate(clk->hw, rate, &parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001087 &parent_hw);
1088 } else if (clk->ops->round_rate)
James Hogan71472c02013-07-29 12:25:00 +01001089 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
1090 else if (clk->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001091 return clk_core_round_rate_nolock(clk->parent, rate);
James Hogan71472c02013-07-29 12:25:00 +01001092 else
1093 return clk->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001094}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001095
1096/**
1097 * __clk_round_rate - round the given rate for a clk
1098 * @clk: round the rate of this clock
1099 * @rate: the rate which is to be rounded
1100 *
1101 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
1102 */
1103unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
1104{
1105 if (!clk)
1106 return 0;
1107
1108 return clk_core_round_rate_nolock(clk->core, rate);
1109}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +02001110EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001111
1112/**
1113 * clk_round_rate - round the given rate for a clk
1114 * @clk: the clk for which we are rounding a rate
1115 * @rate: the rate which is to be rounded
1116 *
1117 * Takes in a rate as input and rounds it to a rate that the clk can actually
1118 * use which is then returned. If clk doesn't support round_rate operation
1119 * then the parent rate is returned.
1120 */
1121long clk_round_rate(struct clk *clk, unsigned long rate)
1122{
1123 unsigned long ret;
1124
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001125 if (!clk)
1126 return 0;
1127
Mike Turquetteeab89f62013-03-28 13:59:01 -07001128 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001129 ret = clk_core_round_rate_nolock(clk->core, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001130 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001131
1132 return ret;
1133}
1134EXPORT_SYMBOL_GPL(clk_round_rate);
1135
1136/**
1137 * __clk_notify - call clk notifier chain
1138 * @clk: struct clk * that is changing rate
1139 * @msg: clk notifier type (see include/linux/clk.h)
1140 * @old_rate: old clk rate
1141 * @new_rate: new clk rate
1142 *
1143 * Triggers a notifier call chain on the clk rate-change notification
1144 * for 'clk'. Passes a pointer to the struct clk and the previous
1145 * and current rates to the notifier callback. Intended to be called by
1146 * internal clock code only. Returns NOTIFY_DONE from the last driver
1147 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1148 * a driver returns that.
1149 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001150static int __clk_notify(struct clk_core *clk, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001151 unsigned long old_rate, unsigned long new_rate)
1152{
1153 struct clk_notifier *cn;
1154 struct clk_notifier_data cnd;
1155 int ret = NOTIFY_DONE;
1156
Mike Turquetteb24764902012-03-15 23:11:19 -07001157 cnd.old_rate = old_rate;
1158 cnd.new_rate = new_rate;
1159
1160 list_for_each_entry(cn, &clk_notifier_list, node) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001161 if (cn->clk->core == clk) {
1162 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001163 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1164 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -07001165 }
1166 }
1167
1168 return ret;
1169}
1170
1171/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001172 * __clk_recalc_accuracies
1173 * @clk: first clk in the subtree
1174 *
1175 * Walks the subtree of clks starting with clk and recalculates accuracies as
1176 * it goes. Note that if a clk does not implement the .recalc_accuracy
1177 * callback then it is assumed that the clock will take on the accuracy of it's
1178 * parent.
1179 *
1180 * Caller must hold prepare_lock.
1181 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001182static void __clk_recalc_accuracies(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001183{
1184 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001185 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001186
1187 if (clk->parent)
1188 parent_accuracy = clk->parent->accuracy;
1189
1190 if (clk->ops->recalc_accuracy)
1191 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
1192 parent_accuracy);
1193 else
1194 clk->accuracy = parent_accuracy;
1195
1196 hlist_for_each_entry(child, &clk->children, child_node)
1197 __clk_recalc_accuracies(child);
1198}
1199
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001200static long clk_core_get_accuracy(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001201{
1202 unsigned long accuracy;
1203
1204 clk_prepare_lock();
1205 if (clk && (clk->flags & CLK_GET_ACCURACY_NOCACHE))
1206 __clk_recalc_accuracies(clk);
1207
1208 accuracy = __clk_get_accuracy(clk);
1209 clk_prepare_unlock();
1210
1211 return accuracy;
1212}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001213
1214/**
1215 * clk_get_accuracy - return the accuracy of clk
1216 * @clk: the clk whose accuracy is being returned
1217 *
1218 * Simply returns the cached accuracy of the clk, unless
1219 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1220 * issued.
1221 * If clk is NULL then returns 0.
1222 */
1223long clk_get_accuracy(struct clk *clk)
1224{
1225 if (!clk)
1226 return 0;
1227
1228 return clk_core_get_accuracy(clk->core);
1229}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001230EXPORT_SYMBOL_GPL(clk_get_accuracy);
1231
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001232static unsigned long clk_recalc(struct clk_core *clk,
1233 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001234{
1235 if (clk->ops->recalc_rate)
1236 return clk->ops->recalc_rate(clk->hw, parent_rate);
1237 return parent_rate;
1238}
1239
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001240/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001241 * __clk_recalc_rates
1242 * @clk: first clk in the subtree
1243 * @msg: notification type (see include/linux/clk.h)
1244 *
1245 * Walks the subtree of clks starting with clk and recalculates rates as it
1246 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001247 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001248 *
1249 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1250 * if necessary.
1251 *
1252 * Caller must hold prepare_lock.
1253 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001254static void __clk_recalc_rates(struct clk_core *clk, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001255{
1256 unsigned long old_rate;
1257 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001258 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001259
1260 old_rate = clk->rate;
1261
1262 if (clk->parent)
1263 parent_rate = clk->parent->rate;
1264
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001265 clk->rate = clk_recalc(clk, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001266
1267 /*
1268 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1269 * & ABORT_RATE_CHANGE notifiers
1270 */
1271 if (clk->notifier_count && msg)
1272 __clk_notify(clk, msg, old_rate, clk->rate);
1273
Sasha Levinb67bfe02013-02-27 17:06:00 -08001274 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001275 __clk_recalc_rates(child, msg);
1276}
1277
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001278static unsigned long clk_core_get_rate(struct clk_core *clk)
1279{
1280 unsigned long rate;
1281
1282 clk_prepare_lock();
1283
1284 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
1285 __clk_recalc_rates(clk, 0);
1286
1287 rate = clk_core_get_rate_nolock(clk);
1288 clk_prepare_unlock();
1289
1290 return rate;
1291}
1292EXPORT_SYMBOL_GPL(clk_core_get_rate);
1293
Mike Turquetteb24764902012-03-15 23:11:19 -07001294/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001295 * clk_get_rate - return the rate of clk
1296 * @clk: the clk whose rate is being returned
1297 *
1298 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1299 * is set, which means a recalc_rate will be issued.
1300 * If clk is NULL then returns 0.
1301 */
1302unsigned long clk_get_rate(struct clk *clk)
1303{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001304 if (!clk)
1305 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001306
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001307 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001308}
1309EXPORT_SYMBOL_GPL(clk_get_rate);
1310
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001311static int clk_fetch_parent_index(struct clk_core *clk,
1312 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001313{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001314 int i;
James Hogan4935b222013-07-29 12:24:59 +01001315
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001316 if (!clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001317 clk->parents = kcalloc(clk->num_parents,
1318 sizeof(struct clk *), GFP_KERNEL);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001319 if (!clk->parents)
1320 return -ENOMEM;
1321 }
James Hogan4935b222013-07-29 12:24:59 +01001322
1323 /*
1324 * find index of new parent clock using cached parent ptrs,
1325 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001326 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001327 */
1328 for (i = 0; i < clk->num_parents; i++) {
Tomasz Figada0f0b22013-09-29 02:37:16 +02001329 if (clk->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001330 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001331
1332 if (clk->parents[i])
1333 continue;
1334
1335 if (!strcmp(clk->parent_names[i], parent->name)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001336 clk->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001337 return i;
James Hogan4935b222013-07-29 12:24:59 +01001338 }
1339 }
1340
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001341 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001342}
1343
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001344static void clk_reparent(struct clk_core *clk, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001345{
1346 hlist_del(&clk->child_node);
1347
James Hogan903efc52013-08-29 12:10:51 +01001348 if (new_parent) {
1349 /* avoid duplicate POST_RATE_CHANGE notifications */
1350 if (new_parent->new_child == clk)
1351 new_parent->new_child = NULL;
1352
James Hogan4935b222013-07-29 12:24:59 +01001353 hlist_add_head(&clk->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001354 } else {
James Hogan4935b222013-07-29 12:24:59 +01001355 hlist_add_head(&clk->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001356 }
James Hogan4935b222013-07-29 12:24:59 +01001357
1358 clk->parent = new_parent;
1359}
1360
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001361static struct clk_core *__clk_set_parent_before(struct clk_core *clk,
1362 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001363{
1364 unsigned long flags;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001365 struct clk_core *old_parent = clk->parent;
James Hogan4935b222013-07-29 12:24:59 +01001366
1367 /*
1368 * Migrate prepare state between parents and prevent race with
1369 * clk_enable().
1370 *
1371 * If the clock is not prepared, then a race with
1372 * clk_enable/disable() is impossible since we already have the
1373 * prepare lock (future calls to clk_enable() need to be preceded by
1374 * a clk_prepare()).
1375 *
1376 * If the clock is prepared, migrate the prepared state to the new
1377 * parent and also protect against a race with clk_enable() by
1378 * forcing the clock and the new parent on. This ensures that all
1379 * future calls to clk_enable() are practically NOPs with respect to
1380 * hardware and software states.
1381 *
1382 * See also: Comment for clk_set_parent() below.
1383 */
1384 if (clk->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001385 clk_core_prepare(parent);
1386 clk_core_enable(parent);
1387 clk_core_enable(clk);
James Hogan4935b222013-07-29 12:24:59 +01001388 }
1389
1390 /* update the clk tree topology */
1391 flags = clk_enable_lock();
1392 clk_reparent(clk, parent);
1393 clk_enable_unlock(flags);
1394
Stephen Boyd3fa22522014-01-15 10:47:22 -08001395 return old_parent;
1396}
1397
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001398static void __clk_set_parent_after(struct clk_core *core,
1399 struct clk_core *parent,
1400 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001401{
1402 /*
1403 * Finish the migration of prepare state and undo the changes done
1404 * for preventing a race with clk_enable().
1405 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001406 if (core->prepare_count) {
1407 clk_core_disable(core);
1408 clk_core_disable(old_parent);
1409 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001410 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001411}
1412
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001413static int __clk_set_parent(struct clk_core *clk, struct clk_core *parent,
1414 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001415{
1416 unsigned long flags;
1417 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001418 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001419
1420 old_parent = __clk_set_parent_before(clk, parent);
1421
James Hogan4935b222013-07-29 12:24:59 +01001422 /* change clock input source */
1423 if (parent && clk->ops->set_parent)
1424 ret = clk->ops->set_parent(clk->hw, p_index);
1425
1426 if (ret) {
1427 flags = clk_enable_lock();
1428 clk_reparent(clk, old_parent);
1429 clk_enable_unlock(flags);
1430
1431 if (clk->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001432 clk_core_disable(clk);
1433 clk_core_disable(parent);
1434 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001435 }
1436 return ret;
1437 }
1438
Stephen Boyd3fa22522014-01-15 10:47:22 -08001439 __clk_set_parent_after(clk, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001440
James Hogan4935b222013-07-29 12:24:59 +01001441 return 0;
1442}
1443
Ulf Hanssona093bde2012-08-31 14:21:28 +02001444/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001445 * __clk_speculate_rates
1446 * @clk: first clk in the subtree
1447 * @parent_rate: the "future" rate of clk's parent
1448 *
1449 * Walks the subtree of clks starting with clk, speculating rates as it
1450 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1451 *
1452 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1453 * pre-rate change notifications and returns early if no clks in the
1454 * subtree have subscribed to the notifications. Note that if a clk does not
1455 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001456 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001457 *
1458 * Caller must hold prepare_lock.
1459 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001460static int __clk_speculate_rates(struct clk_core *clk,
1461 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001462{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001463 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001464 unsigned long new_rate;
1465 int ret = NOTIFY_DONE;
1466
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001467 new_rate = clk_recalc(clk, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001468
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001469 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Mike Turquetteb24764902012-03-15 23:11:19 -07001470 if (clk->notifier_count)
1471 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
1472
Mike Turquette86bcfa22014-02-24 16:08:41 -08001473 if (ret & NOTIFY_STOP_MASK) {
1474 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1475 __func__, clk->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001476 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001477 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001478
Sasha Levinb67bfe02013-02-27 17:06:00 -08001479 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001480 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001481 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001482 break;
1483 }
1484
1485out:
1486 return ret;
1487}
1488
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001489static void clk_calc_subtree(struct clk_core *clk, unsigned long new_rate,
1490 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001491{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001492 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001493
1494 clk->new_rate = new_rate;
James Hogan71472c02013-07-29 12:25:00 +01001495 clk->new_parent = new_parent;
1496 clk->new_parent_index = p_index;
1497 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1498 clk->new_child = NULL;
1499 if (new_parent && new_parent != clk->parent)
1500 new_parent->new_child = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001501
Sasha Levinb67bfe02013-02-27 17:06:00 -08001502 hlist_for_each_entry(child, &clk->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001503 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001504 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001505 }
1506}
1507
1508/*
1509 * calculate the new rates returning the topmost clock that has to be
1510 * changed.
1511 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001512static struct clk_core *clk_calc_new_rates(struct clk_core *clk,
1513 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001514{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001515 struct clk_core *top = clk;
1516 struct clk_core *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001517 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001518 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001519 unsigned long new_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001520 int p_index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001521
Mike Turquette7452b212012-03-26 14:45:36 -07001522 /* sanity */
1523 if (IS_ERR_OR_NULL(clk))
1524 return NULL;
1525
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001526 /* save parent rate, if it exists */
James Hogan71472c02013-07-29 12:25:00 +01001527 parent = old_parent = clk->parent;
1528 if (parent)
1529 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001530
James Hogan71472c02013-07-29 12:25:00 +01001531 /* find the closest rate and parent clk/rate */
1532 if (clk->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001533 parent_hw = parent ? parent->hw : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001534 new_rate = clk->ops->determine_rate(clk->hw, rate,
1535 &best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001536 &parent_hw);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001537 parent = parent_hw ? parent_hw->core : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001538 } else if (clk->ops->round_rate) {
1539 new_rate = clk->ops->round_rate(clk->hw, rate,
1540 &best_parent_rate);
1541 } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) {
1542 /* pass-through clock without adjustable parent */
1543 clk->new_rate = clk->rate;
1544 return NULL;
1545 } else {
1546 /* pass-through clock with adjustable parent */
1547 top = clk_calc_new_rates(parent, rate);
1548 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001549 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001550 }
1551
James Hogan71472c02013-07-29 12:25:00 +01001552 /* some clocks must be gated to change parent */
1553 if (parent != old_parent &&
1554 (clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1555 pr_debug("%s: %s not gated but wants to reparent\n",
1556 __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001557 return NULL;
1558 }
1559
James Hogan71472c02013-07-29 12:25:00 +01001560 /* try finding the new parent index */
Stephen Boyd4526e7b2014-12-22 11:26:42 -08001561 if (parent && clk->num_parents > 1) {
James Hogan71472c02013-07-29 12:25:00 +01001562 p_index = clk_fetch_parent_index(clk, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001563 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001564 pr_debug("%s: clk %s can not be parent of clk %s\n",
1565 __func__, parent->name, clk->name);
1566 return NULL;
1567 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001568 }
1569
James Hogan71472c02013-07-29 12:25:00 +01001570 if ((clk->flags & CLK_SET_RATE_PARENT) && parent &&
1571 best_parent_rate != parent->rate)
1572 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001573
1574out:
James Hogan71472c02013-07-29 12:25:00 +01001575 clk_calc_subtree(clk, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001576
1577 return top;
1578}
1579
1580/*
1581 * Notify about rate changes in a subtree. Always walk down the whole tree
1582 * so that in case of an error we can walk down the whole tree again and
1583 * abort the change.
1584 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001585static struct clk_core *clk_propagate_rate_change(struct clk_core *clk,
1586 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001587{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001588 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001589 int ret = NOTIFY_DONE;
1590
1591 if (clk->rate == clk->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301592 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001593
1594 if (clk->notifier_count) {
1595 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001596 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001597 fail_clk = clk;
1598 }
1599
Sasha Levinb67bfe02013-02-27 17:06:00 -08001600 hlist_for_each_entry(child, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001601 /* Skip children who will be reparented to another clock */
1602 if (child->new_parent && child->new_parent != clk)
1603 continue;
1604 tmp_clk = clk_propagate_rate_change(child, event);
1605 if (tmp_clk)
1606 fail_clk = tmp_clk;
1607 }
1608
1609 /* handle the new child who might not be in clk->children yet */
1610 if (clk->new_child) {
1611 tmp_clk = clk_propagate_rate_change(clk->new_child, event);
1612 if (tmp_clk)
1613 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001614 }
1615
1616 return fail_clk;
1617}
1618
1619/*
1620 * walk down a subtree and set the new rates notifying the rate
1621 * change on the way
1622 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001623static void clk_change_rate(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001624{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001625 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001626 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001627 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001628 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001629 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001630 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001631
1632 old_rate = clk->rate;
1633
Stephen Boyd3fa22522014-01-15 10:47:22 -08001634 if (clk->new_parent)
1635 best_parent_rate = clk->new_parent->rate;
1636 else if (clk->parent)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001637 best_parent_rate = clk->parent->rate;
1638
Stephen Boyd3fa22522014-01-15 10:47:22 -08001639 if (clk->new_parent && clk->new_parent != clk->parent) {
1640 old_parent = __clk_set_parent_before(clk, clk->new_parent);
1641
1642 if (clk->ops->set_rate_and_parent) {
1643 skip_set_rate = true;
1644 clk->ops->set_rate_and_parent(clk->hw, clk->new_rate,
1645 best_parent_rate,
1646 clk->new_parent_index);
1647 } else if (clk->ops->set_parent) {
1648 clk->ops->set_parent(clk->hw, clk->new_parent_index);
1649 }
1650
1651 __clk_set_parent_after(clk, clk->new_parent, old_parent);
1652 }
1653
1654 if (!skip_set_rate && clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001655 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001656
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001657 clk->rate = clk_recalc(clk, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001658
1659 if (clk->notifier_count && old_rate != clk->rate)
1660 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1661
Tero Kristo067bb172014-08-21 16:47:45 +03001662 /*
1663 * Use safe iteration, as change_rate can actually swap parents
1664 * for certain clock types.
1665 */
1666 hlist_for_each_entry_safe(child, tmp, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001667 /* Skip children who will be reparented to another clock */
1668 if (child->new_parent && child->new_parent != clk)
1669 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001670 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001671 }
1672
1673 /* handle the new child who might not be in clk->children yet */
1674 if (clk->new_child)
1675 clk_change_rate(clk->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001676}
1677
1678/**
1679 * clk_set_rate - specify a new rate for clk
1680 * @clk: the clk whose rate is being changed
1681 * @rate: the new rate for clk
1682 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001683 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001684 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001685 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1686 * propagate up to clk's parent; whether or not this happens depends on the
1687 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1688 * after calling .round_rate then upstream parent propagation is ignored. If
1689 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001690 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001691 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1692 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001693 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001694 * Rate changes are accomplished via tree traversal that also recalculates the
1695 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001696 *
1697 * Returns 0 on success, -EERROR otherwise.
1698 */
1699int clk_set_rate(struct clk *clk, unsigned long rate)
1700{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001701 struct clk_core *top, *fail_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001702 int ret = 0;
1703
Mike Turquette89ac8d72013-08-21 23:58:09 -07001704 if (!clk)
1705 return 0;
1706
Mike Turquetteb24764902012-03-15 23:11:19 -07001707 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001708 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001709
1710 /* bail early if nothing to do */
Peter De Schrijver34e452a2013-06-05 18:06:36 +03001711 if (rate == clk_get_rate(clk))
Mike Turquetteb24764902012-03-15 23:11:19 -07001712 goto out;
1713
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001714 if ((clk->core->flags & CLK_SET_RATE_GATE) &&
1715 clk->core->prepare_count) {
Viresh Kumar0e1c0302012-04-11 16:03:42 +05301716 ret = -EBUSY;
1717 goto out;
1718 }
1719
Mike Turquetteb24764902012-03-15 23:11:19 -07001720 /* calculate new rates and get the topmost changed clock */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001721 top = clk_calc_new_rates(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001722 if (!top) {
1723 ret = -EINVAL;
1724 goto out;
1725 }
1726
1727 /* notify that we are about to change rates */
1728 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1729 if (fail_clk) {
Sascha Hauerf7363862014-01-16 16:12:55 +01001730 pr_debug("%s: failed to set %s rate\n", __func__,
Mike Turquetteb24764902012-03-15 23:11:19 -07001731 fail_clk->name);
1732 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1733 ret = -EBUSY;
1734 goto out;
1735 }
1736
1737 /* change the rates */
1738 clk_change_rate(top);
1739
Mike Turquetteb24764902012-03-15 23:11:19 -07001740out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001741 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001742
1743 return ret;
1744}
1745EXPORT_SYMBOL_GPL(clk_set_rate);
1746
1747/**
1748 * clk_get_parent - return the parent of a clk
1749 * @clk: the clk whose parent gets returned
1750 *
1751 * Simply returns clk->parent. Returns NULL if clk is NULL.
1752 */
1753struct clk *clk_get_parent(struct clk *clk)
1754{
1755 struct clk *parent;
1756
Mike Turquetteeab89f62013-03-28 13:59:01 -07001757 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001758 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001759 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001760
1761 return parent;
1762}
1763EXPORT_SYMBOL_GPL(clk_get_parent);
1764
1765/*
1766 * .get_parent is mandatory for clocks with multiple possible parents. It is
1767 * optional for single-parent clocks. Always call .get_parent if it is
1768 * available and WARN if it is missing for multi-parent clocks.
1769 *
1770 * For single-parent clocks without .get_parent, first check to see if the
1771 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001772 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001773 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001774static struct clk_core *__clk_init_parent(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001775{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001776 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001777 u8 index;
1778
1779 /* handle the trivial cases */
1780
1781 if (!clk->num_parents)
1782 goto out;
1783
1784 if (clk->num_parents == 1) {
1785 if (IS_ERR_OR_NULL(clk->parent))
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001786 clk->parent = clk_core_lookup(clk->parent_names[0]);
Mike Turquetteb24764902012-03-15 23:11:19 -07001787 ret = clk->parent;
1788 goto out;
1789 }
1790
1791 if (!clk->ops->get_parent) {
1792 WARN(!clk->ops->get_parent,
1793 "%s: multi-parent clocks must implement .get_parent\n",
1794 __func__);
1795 goto out;
1796 };
1797
1798 /*
1799 * Do our best to cache parent clocks in clk->parents. This prevents
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001800 * unnecessary and expensive lookups. We don't set clk->parent here;
1801 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001802 */
1803
1804 index = clk->ops->get_parent(clk->hw);
1805
1806 if (!clk->parents)
1807 clk->parents =
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001808 kcalloc(clk->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001809 GFP_KERNEL);
1810
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001811 ret = clk_core_get_parent_by_index(clk, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001812
1813out:
1814 return ret;
1815}
1816
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001817static void clk_core_reparent(struct clk_core *clk,
1818 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001819{
1820 clk_reparent(clk, new_parent);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001821 __clk_recalc_accuracies(clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07001822 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1823}
1824
Mike Turquetteb24764902012-03-15 23:11:19 -07001825/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001826 * clk_has_parent - check if a clock is a possible parent for another
1827 * @clk: clock source
1828 * @parent: parent clock source
1829 *
1830 * This function can be used in drivers that need to check that a clock can be
1831 * the parent of another without actually changing the parent.
1832 *
1833 * Returns true if @parent is a possible parent for @clk, false otherwise.
1834 */
1835bool clk_has_parent(struct clk *clk, struct clk *parent)
1836{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001837 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001838 unsigned int i;
1839
1840 /* NULL clocks should be nops, so return success if either is NULL. */
1841 if (!clk || !parent)
1842 return true;
1843
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001844 core = clk->core;
1845 parent_core = parent->core;
1846
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001847 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001848 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001849 return true;
1850
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001851 for (i = 0; i < core->num_parents; i++)
1852 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001853 return true;
1854
1855 return false;
1856}
1857EXPORT_SYMBOL_GPL(clk_has_parent);
1858
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001859static int clk_core_set_parent(struct clk_core *clk, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001860{
1861 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001862 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001863 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001864
Mike Turquette89ac8d72013-08-21 23:58:09 -07001865 if (!clk)
1866 return 0;
1867
Ulf Hansson031dcc92013-04-02 23:09:38 +02001868 /* verify ops for for multi-parent clks */
1869 if ((clk->num_parents > 1) && (!clk->ops->set_parent))
Mike Turquetteb24764902012-03-15 23:11:19 -07001870 return -ENOSYS;
1871
1872 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001873 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001874
1875 if (clk->parent == parent)
1876 goto out;
1877
Ulf Hansson031dcc92013-04-02 23:09:38 +02001878 /* check that we are allowed to re-parent if the clock is in use */
1879 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1880 ret = -EBUSY;
1881 goto out;
1882 }
1883
1884 /* try finding the new parent index */
1885 if (parent) {
1886 p_index = clk_fetch_parent_index(clk, parent);
1887 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001888 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001889 pr_debug("%s: clk %s can not be parent of clk %s\n",
1890 __func__, parent->name, clk->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001891 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001892 goto out;
1893 }
1894 }
1895
Mike Turquetteb24764902012-03-15 23:11:19 -07001896 /* propagate PRE_RATE_CHANGE notifications */
Soren Brinkmannf3aab5d2013-04-16 10:06:50 -07001897 ret = __clk_speculate_rates(clk, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001898
1899 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001900 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001901 goto out;
1902
Ulf Hansson031dcc92013-04-02 23:09:38 +02001903 /* do the re-parent */
1904 ret = __clk_set_parent(clk, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001905
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001906 /* propagate rate an accuracy recalculation accordingly */
1907 if (ret) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001908 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001909 } else {
Ulf Hanssona68de8e2013-04-02 23:09:39 +02001910 __clk_recalc_rates(clk, POST_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001911 __clk_recalc_accuracies(clk);
1912 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001913
1914out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001915 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001916
1917 return ret;
1918}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001919
1920/**
1921 * clk_set_parent - switch the parent of a mux clk
1922 * @clk: the mux clk whose input we are switching
1923 * @parent: the new input to clk
1924 *
1925 * Re-parent clk to use parent as its new input source. If clk is in
1926 * prepared state, the clk will get enabled for the duration of this call. If
1927 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1928 * that, the reparenting is glitchy in hardware, etc), use the
1929 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1930 *
1931 * After successfully changing clk's parent clk_set_parent will update the
1932 * clk topology, sysfs topology and propagate rate recalculation via
1933 * __clk_recalc_rates.
1934 *
1935 * Returns 0 on success, -EERROR otherwise.
1936 */
1937int clk_set_parent(struct clk *clk, struct clk *parent)
1938{
1939 if (!clk)
1940 return 0;
1941
1942 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1943}
Mike Turquetteb24764902012-03-15 23:11:19 -07001944EXPORT_SYMBOL_GPL(clk_set_parent);
1945
1946/**
Mike Turquettee59c5372014-02-18 21:21:25 -08001947 * clk_set_phase - adjust the phase shift of a clock signal
1948 * @clk: clock signal source
1949 * @degrees: number of degrees the signal is shifted
1950 *
1951 * Shifts the phase of a clock signal by the specified
1952 * degrees. Returns 0 on success, -EERROR otherwise.
1953 *
1954 * This function makes no distinction about the input or reference
1955 * signal that we adjust the clock signal phase against. For example
1956 * phase locked-loop clock signal generators we may shift phase with
1957 * respect to feedback clock signal input, but for other cases the
1958 * clock phase may be shifted with respect to some other, unspecified
1959 * signal.
1960 *
1961 * Additionally the concept of phase shift does not propagate through
1962 * the clock tree hierarchy, which sets it apart from clock rates and
1963 * clock accuracy. A parent clock phase attribute does not have an
1964 * impact on the phase attribute of a child clock.
1965 */
1966int clk_set_phase(struct clk *clk, int degrees)
1967{
1968 int ret = 0;
1969
1970 if (!clk)
1971 goto out;
1972
1973 /* sanity check degrees */
1974 degrees %= 360;
1975 if (degrees < 0)
1976 degrees += 360;
1977
1978 clk_prepare_lock();
1979
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001980 if (!clk->core->ops->set_phase)
Mike Turquettee59c5372014-02-18 21:21:25 -08001981 goto out_unlock;
1982
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001983 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08001984
1985 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001986 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08001987
1988out_unlock:
1989 clk_prepare_unlock();
1990
1991out:
1992 return ret;
1993}
Maxime Ripard9767b042015-01-20 22:23:43 +01001994EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001995
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001996static int clk_core_get_phase(struct clk_core *clk)
Mike Turquettee59c5372014-02-18 21:21:25 -08001997{
1998 int ret = 0;
1999
2000 if (!clk)
2001 goto out;
2002
2003 clk_prepare_lock();
2004 ret = clk->phase;
2005 clk_prepare_unlock();
2006
2007out:
2008 return ret;
2009}
Maxime Ripard9767b042015-01-20 22:23:43 +01002010EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08002011
2012/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002013 * clk_get_phase - return the phase shift of a clock signal
2014 * @clk: clock signal source
2015 *
2016 * Returns the phase shift of a clock node in degrees, otherwise returns
2017 * -EERROR.
2018 */
2019int clk_get_phase(struct clk *clk)
2020{
2021 if (!clk)
2022 return 0;
2023
2024 return clk_core_get_phase(clk->core);
2025}
2026
2027/**
Mike Turquetteb24764902012-03-15 23:11:19 -07002028 * __clk_init - initialize the data structures in a struct clk
2029 * @dev: device initializing this clk, placeholder for now
2030 * @clk: clk being initialized
2031 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002032 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07002033 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07002034 */
Michael Turquetteb09d6d92015-01-29 14:22:50 -08002035static int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07002036{
Mike Turquetted1302a32012-03-29 14:30:40 -07002037 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002038 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002039 struct hlist_node *tmp2;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002040 struct clk_core *clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07002041
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002042 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002043 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002044
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002045 clk = clk_user->core;
2046
Mike Turquetteeab89f62013-03-28 13:59:01 -07002047 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002048
2049 /* check to see if a clock with this name is already registered */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002050 if (clk_core_lookup(clk->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002051 pr_debug("%s: clk %s already initialized\n",
2052 __func__, clk->name);
2053 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002054 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002055 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002056
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002057 /* check that clk_ops are sane. See Documentation/clk.txt */
2058 if (clk->ops->set_rate &&
James Hogan71472c02013-07-29 12:25:00 +01002059 !((clk->ops->round_rate || clk->ops->determine_rate) &&
2060 clk->ops->recalc_rate)) {
2061 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002062 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002063 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002064 goto out;
2065 }
2066
2067 if (clk->ops->set_parent && !clk->ops->get_parent) {
2068 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
2069 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002070 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002071 goto out;
2072 }
2073
Stephen Boyd3fa22522014-01-15 10:47:22 -08002074 if (clk->ops->set_rate_and_parent &&
2075 !(clk->ops->set_parent && clk->ops->set_rate)) {
2076 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
2077 __func__, clk->name);
2078 ret = -EINVAL;
2079 goto out;
2080 }
2081
Mike Turquetteb24764902012-03-15 23:11:19 -07002082 /* throw a WARN if any entries in parent_names are NULL */
2083 for (i = 0; i < clk->num_parents; i++)
2084 WARN(!clk->parent_names[i],
2085 "%s: invalid NULL in %s's .parent_names\n",
2086 __func__, clk->name);
2087
2088 /*
2089 * Allocate an array of struct clk *'s to avoid unnecessary string
2090 * look-ups of clk's possible parents. This can fail for clocks passed
2091 * in to clk_init during early boot; thus any access to clk->parents[]
2092 * must always check for a NULL pointer and try to populate it if
2093 * necessary.
2094 *
2095 * If clk->parents is not NULL we skip this entire block. This allows
2096 * for clock drivers to statically initialize clk->parents.
2097 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05302098 if (clk->num_parents > 1 && !clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002099 clk->parents = kcalloc(clk->num_parents, sizeof(struct clk *),
2100 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002101 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002102 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002103 * clk_init'd; thus any access to clk->parents[] must check
2104 * for a NULL pointer. We can always perform lazy lookups for
2105 * missing parents later on.
2106 */
2107 if (clk->parents)
2108 for (i = 0; i < clk->num_parents; i++)
2109 clk->parents[i] =
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002110 clk_core_lookup(clk->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002111 }
2112
2113 clk->parent = __clk_init_parent(clk);
2114
2115 /*
2116 * Populate clk->parent if parent has already been __clk_init'd. If
2117 * parent has not yet been __clk_init'd then place clk in the orphan
2118 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2119 * clk list.
2120 *
2121 * Every time a new clk is clk_init'd then we walk the list of orphan
2122 * clocks and re-parent any that are children of the clock currently
2123 * being clk_init'd.
2124 */
2125 if (clk->parent)
2126 hlist_add_head(&clk->child_node,
2127 &clk->parent->children);
2128 else if (clk->flags & CLK_IS_ROOT)
2129 hlist_add_head(&clk->child_node, &clk_root_list);
2130 else
2131 hlist_add_head(&clk->child_node, &clk_orphan_list);
2132
2133 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002134 * Set clk's accuracy. The preferred method is to use
2135 * .recalc_accuracy. For simple clocks and lazy developers the default
2136 * fallback is to use the parent's accuracy. If a clock doesn't have a
2137 * parent (or is orphaned) then accuracy is set to zero (perfect
2138 * clock).
2139 */
2140 if (clk->ops->recalc_accuracy)
2141 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
2142 __clk_get_accuracy(clk->parent));
2143 else if (clk->parent)
2144 clk->accuracy = clk->parent->accuracy;
2145 else
2146 clk->accuracy = 0;
2147
2148 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002149 * Set clk's phase.
2150 * Since a phase is by definition relative to its parent, just
2151 * query the current clock phase, or just assume it's in phase.
2152 */
2153 if (clk->ops->get_phase)
2154 clk->phase = clk->ops->get_phase(clk->hw);
2155 else
2156 clk->phase = 0;
2157
2158 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002159 * Set clk's rate. The preferred method is to use .recalc_rate. For
2160 * simple clocks and lazy developers the default fallback is to use the
2161 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2162 * then rate is set to zero.
2163 */
2164 if (clk->ops->recalc_rate)
2165 clk->rate = clk->ops->recalc_rate(clk->hw,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002166 clk_core_get_rate_nolock(clk->parent));
Mike Turquetteb24764902012-03-15 23:11:19 -07002167 else if (clk->parent)
2168 clk->rate = clk->parent->rate;
2169 else
2170 clk->rate = 0;
2171
2172 /*
2173 * walk the list of orphan clocks and reparent any that are children of
2174 * this clock
2175 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002176 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002177 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002178 i = orphan->ops->get_parent(orphan->hw);
2179 if (!strcmp(clk->name, orphan->parent_names[i]))
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002180 clk_core_reparent(orphan, clk);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002181 continue;
2182 }
2183
Mike Turquetteb24764902012-03-15 23:11:19 -07002184 for (i = 0; i < orphan->num_parents; i++)
2185 if (!strcmp(clk->name, orphan->parent_names[i])) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002186 clk_core_reparent(orphan, clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07002187 break;
2188 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002189 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002190
2191 /*
2192 * optional platform-specific magic
2193 *
2194 * The .init callback is not used by any of the basic clock types, but
2195 * exists for weird hardware that must perform initialization magic.
2196 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002197 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002198 */
2199 if (clk->ops->init)
2200 clk->ops->init(clk->hw);
2201
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002202 kref_init(&clk->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002203out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002204 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002205
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002206 if (!ret)
2207 clk_debug_register(clk);
2208
Mike Turquetted1302a32012-03-29 14:30:40 -07002209 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002210}
2211
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002212struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2213 const char *con_id)
2214{
2215 struct clk *clk;
2216
2217 /* This is to allow this function to be chained to others */
2218 if (!hw || IS_ERR(hw))
2219 return (struct clk *) hw;
2220
2221 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2222 if (!clk)
2223 return ERR_PTR(-ENOMEM);
2224
2225 clk->core = hw->core;
2226 clk->dev_id = dev_id;
2227 clk->con_id = con_id;
2228
2229 return clk;
2230}
2231
Mike Turquetteb24764902012-03-15 23:11:19 -07002232/**
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002233 * clk_register - allocate a new clock, register it and return an opaque cookie
2234 * @dev: device that is registering this clock
2235 * @hw: link to hardware-specific clock data
2236 *
2237 * clk_register is the primary interface for populating the clock tree with new
2238 * clock nodes. It returns a pointer to the newly allocated struct clk which
2239 * cannot be dereferenced by driver code but may be used in conjuction with the
2240 * rest of the clock API. In the event of an error clk_register will return an
2241 * error code; drivers must test for an error code after calling clk_register.
2242 */
2243struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002244{
Mike Turquetted1302a32012-03-29 14:30:40 -07002245 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002246 struct clk_core *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002247
2248 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2249 if (!clk) {
2250 pr_err("%s: could not allocate clk\n", __func__);
2251 ret = -ENOMEM;
2252 goto fail_out;
2253 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002254
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002255 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
2256 if (!clk->name) {
2257 pr_err("%s: could not allocate clk->name\n", __func__);
2258 ret = -ENOMEM;
2259 goto fail_name;
2260 }
2261 clk->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002262 if (dev && dev->driver)
2263 clk->owner = dev->driver->owner;
Mike Turquetteb24764902012-03-15 23:11:19 -07002264 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002265 clk->flags = hw->init->flags;
2266 clk->num_parents = hw->init->num_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002267 hw->core = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07002268
Mike Turquetted1302a32012-03-29 14:30:40 -07002269 /* allocate local copy in case parent_names is __initdata */
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002270 clk->parent_names = kcalloc(clk->num_parents, sizeof(char *),
2271 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002272
Mike Turquetted1302a32012-03-29 14:30:40 -07002273 if (!clk->parent_names) {
2274 pr_err("%s: could not allocate clk->parent_names\n", __func__);
2275 ret = -ENOMEM;
2276 goto fail_parent_names;
2277 }
2278
2279
2280 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002281 for (i = 0; i < clk->num_parents; i++) {
2282 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
2283 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07002284 if (!clk->parent_names[i]) {
2285 pr_err("%s: could not copy parent_names\n", __func__);
2286 ret = -ENOMEM;
2287 goto fail_parent_names_copy;
2288 }
2289 }
2290
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002291 hw->clk = __clk_create_clk(hw, NULL, NULL);
2292 if (IS_ERR(hw->clk)) {
2293 pr_err("%s: could not allocate per-user clk\n", __func__);
2294 ret = PTR_ERR(hw->clk);
2295 goto fail_parent_names_copy;
2296 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002297
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002298 ret = __clk_init(dev, hw->clk);
2299 if (!ret)
2300 return hw->clk;
2301
2302 kfree(hw->clk);
2303 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002304fail_parent_names_copy:
2305 while (--i >= 0)
2306 kfree(clk->parent_names[i]);
2307 kfree(clk->parent_names);
2308fail_parent_names:
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002309 kfree(clk->name);
2310fail_name:
Mike Turquetted1302a32012-03-29 14:30:40 -07002311 kfree(clk);
2312fail_out:
2313 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002314}
2315EXPORT_SYMBOL_GPL(clk_register);
2316
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002317/*
2318 * Free memory allocated for a clock.
2319 * Caller must hold prepare_lock.
2320 */
2321static void __clk_release(struct kref *ref)
2322{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002323 struct clk_core *clk = container_of(ref, struct clk_core, ref);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002324 int i = clk->num_parents;
2325
2326 kfree(clk->parents);
2327 while (--i >= 0)
2328 kfree(clk->parent_names[i]);
2329
2330 kfree(clk->parent_names);
2331 kfree(clk->name);
2332 kfree(clk);
2333}
2334
2335/*
2336 * Empty clk_ops for unregistered clocks. These are used temporarily
2337 * after clk_unregister() was called on a clock and until last clock
2338 * consumer calls clk_put() and the struct clk object is freed.
2339 */
2340static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2341{
2342 return -ENXIO;
2343}
2344
2345static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2346{
2347 WARN_ON_ONCE(1);
2348}
2349
2350static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2351 unsigned long parent_rate)
2352{
2353 return -ENXIO;
2354}
2355
2356static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2357{
2358 return -ENXIO;
2359}
2360
2361static const struct clk_ops clk_nodrv_ops = {
2362 .enable = clk_nodrv_prepare_enable,
2363 .disable = clk_nodrv_disable_unprepare,
2364 .prepare = clk_nodrv_prepare_enable,
2365 .unprepare = clk_nodrv_disable_unprepare,
2366 .set_rate = clk_nodrv_set_rate,
2367 .set_parent = clk_nodrv_set_parent,
2368};
2369
Mark Brown1df5c932012-04-18 09:07:12 +01002370/**
2371 * clk_unregister - unregister a currently registered clock
2372 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002373 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002374void clk_unregister(struct clk *clk)
2375{
2376 unsigned long flags;
2377
Stephen Boyd6314b672014-09-04 23:37:49 -07002378 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2379 return;
2380
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002381 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002382
2383 clk_prepare_lock();
2384
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002385 if (clk->core->ops == &clk_nodrv_ops) {
2386 pr_err("%s: unregistered clock: %s\n", __func__,
2387 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002388 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002389 }
2390 /*
2391 * Assign empty clock ops for consumers that might still hold
2392 * a reference to this clock.
2393 */
2394 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002395 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002396 clk_enable_unlock(flags);
2397
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002398 if (!hlist_empty(&clk->core->children)) {
2399 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002400 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002401
2402 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002403 hlist_for_each_entry_safe(child, t, &clk->core->children,
2404 child_node)
2405 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002406 }
2407
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002408 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002409
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002410 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002411 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002412 __func__, clk->core->name);
2413 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002414
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002415 clk_prepare_unlock();
2416}
Mark Brown1df5c932012-04-18 09:07:12 +01002417EXPORT_SYMBOL_GPL(clk_unregister);
2418
Stephen Boyd46c87732012-09-24 13:38:04 -07002419static void devm_clk_release(struct device *dev, void *res)
2420{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002421 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002422}
2423
2424/**
2425 * devm_clk_register - resource managed clk_register()
2426 * @dev: device that is registering this clock
2427 * @hw: link to hardware-specific clock data
2428 *
2429 * Managed clk_register(). Clocks returned from this function are
2430 * automatically clk_unregister()ed on driver detach. See clk_register() for
2431 * more information.
2432 */
2433struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2434{
2435 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002436 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002437
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002438 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2439 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002440 return ERR_PTR(-ENOMEM);
2441
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002442 clk = clk_register(dev, hw);
2443 if (!IS_ERR(clk)) {
2444 *clkp = clk;
2445 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002446 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002447 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002448 }
2449
2450 return clk;
2451}
2452EXPORT_SYMBOL_GPL(devm_clk_register);
2453
2454static int devm_clk_match(struct device *dev, void *res, void *data)
2455{
2456 struct clk *c = res;
2457 if (WARN_ON(!c))
2458 return 0;
2459 return c == data;
2460}
2461
2462/**
2463 * devm_clk_unregister - resource managed clk_unregister()
2464 * @clk: clock to unregister
2465 *
2466 * Deallocate a clock allocated with devm_clk_register(). Normally
2467 * this function will not need to be called and the resource management
2468 * code will ensure that the resource is freed.
2469 */
2470void devm_clk_unregister(struct device *dev, struct clk *clk)
2471{
2472 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2473}
2474EXPORT_SYMBOL_GPL(devm_clk_unregister);
2475
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002476/*
2477 * clkdev helpers
2478 */
2479int __clk_get(struct clk *clk)
2480{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002481 struct clk_core *core = !clk ? NULL : clk->core;
2482
2483 if (core) {
2484 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002485 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002486
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002487 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002488 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002489 return 1;
2490}
2491
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002492static void clk_core_put(struct clk_core *core)
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002493{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002494 struct module *owner;
2495
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002496 owner = core->owner;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002497
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002498 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002499 kref_put(&core->ref, __clk_release);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002500 clk_prepare_unlock();
2501
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002502 module_put(owner);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002503}
2504
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002505void __clk_put(struct clk *clk)
2506{
2507 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2508 return;
2509
2510 clk_core_put(clk->core);
2511 kfree(clk);
2512}
2513
Mike Turquetteb24764902012-03-15 23:11:19 -07002514/*** clk rate change notifiers ***/
2515
2516/**
2517 * clk_notifier_register - add a clk rate change notifier
2518 * @clk: struct clk * to watch
2519 * @nb: struct notifier_block * with callback info
2520 *
2521 * Request notification when clk's rate changes. This uses an SRCU
2522 * notifier because we want it to block and notifier unregistrations are
2523 * uncommon. The callbacks associated with the notifier must not
2524 * re-enter into the clk framework by calling any top-level clk APIs;
2525 * this will cause a nested prepare_lock mutex.
2526 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002527 * In all notification cases cases (pre, post and abort rate change) the
2528 * original clock rate is passed to the callback via struct
2529 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002530 * clk_notifier_data.new_rate.
2531 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002532 * clk_notifier_register() must be called from non-atomic context.
2533 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2534 * allocation failure; otherwise, passes along the return value of
2535 * srcu_notifier_chain_register().
2536 */
2537int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2538{
2539 struct clk_notifier *cn;
2540 int ret = -ENOMEM;
2541
2542 if (!clk || !nb)
2543 return -EINVAL;
2544
Mike Turquetteeab89f62013-03-28 13:59:01 -07002545 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002546
2547 /* search the list of notifiers for this clk */
2548 list_for_each_entry(cn, &clk_notifier_list, node)
2549 if (cn->clk == clk)
2550 break;
2551
2552 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2553 if (cn->clk != clk) {
2554 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2555 if (!cn)
2556 goto out;
2557
2558 cn->clk = clk;
2559 srcu_init_notifier_head(&cn->notifier_head);
2560
2561 list_add(&cn->node, &clk_notifier_list);
2562 }
2563
2564 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2565
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002566 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002567
2568out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002569 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002570
2571 return ret;
2572}
2573EXPORT_SYMBOL_GPL(clk_notifier_register);
2574
2575/**
2576 * clk_notifier_unregister - remove a clk rate change notifier
2577 * @clk: struct clk *
2578 * @nb: struct notifier_block * with callback info
2579 *
2580 * Request no further notification for changes to 'clk' and frees memory
2581 * allocated in clk_notifier_register.
2582 *
2583 * Returns -EINVAL if called with null arguments; otherwise, passes
2584 * along the return value of srcu_notifier_chain_unregister().
2585 */
2586int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2587{
2588 struct clk_notifier *cn = NULL;
2589 int ret = -EINVAL;
2590
2591 if (!clk || !nb)
2592 return -EINVAL;
2593
Mike Turquetteeab89f62013-03-28 13:59:01 -07002594 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002595
2596 list_for_each_entry(cn, &clk_notifier_list, node)
2597 if (cn->clk == clk)
2598 break;
2599
2600 if (cn->clk == clk) {
2601 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2602
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002603 clk->core->notifier_count--;
Mike Turquetteb24764902012-03-15 23:11:19 -07002604
2605 /* XXX the notifier code should handle this better */
2606 if (!cn->notifier_head.head) {
2607 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002608 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002609 kfree(cn);
2610 }
2611
2612 } else {
2613 ret = -ENOENT;
2614 }
2615
Mike Turquetteeab89f62013-03-28 13:59:01 -07002616 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002617
2618 return ret;
2619}
2620EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002621
2622#ifdef CONFIG_OF
2623/**
2624 * struct of_clk_provider - Clock provider registration structure
2625 * @link: Entry in global list of clock providers
2626 * @node: Pointer to device tree node of clock provider
2627 * @get: Get clock callback. Returns NULL or a struct clk for the
2628 * given clock specifier
2629 * @data: context pointer to be passed into @get callback
2630 */
2631struct of_clk_provider {
2632 struct list_head link;
2633
2634 struct device_node *node;
2635 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2636 void *data;
2637};
2638
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302639static const struct of_device_id __clk_of_table_sentinel
2640 __used __section(__clk_of_table_end);
2641
Grant Likely766e6a42012-04-09 14:50:06 -05002642static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002643static DEFINE_MUTEX(of_clk_mutex);
2644
2645/* of_clk_provider list locking helpers */
2646void of_clk_lock(void)
2647{
2648 mutex_lock(&of_clk_mutex);
2649}
2650
2651void of_clk_unlock(void)
2652{
2653 mutex_unlock(&of_clk_mutex);
2654}
Grant Likely766e6a42012-04-09 14:50:06 -05002655
2656struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2657 void *data)
2658{
2659 return data;
2660}
2661EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2662
Shawn Guo494bfec2012-08-22 21:36:27 +08002663struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2664{
2665 struct clk_onecell_data *clk_data = data;
2666 unsigned int idx = clkspec->args[0];
2667
2668 if (idx >= clk_data->clk_num) {
2669 pr_err("%s: invalid clock index %d\n", __func__, idx);
2670 return ERR_PTR(-EINVAL);
2671 }
2672
2673 return clk_data->clks[idx];
2674}
2675EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2676
Grant Likely766e6a42012-04-09 14:50:06 -05002677/**
2678 * of_clk_add_provider() - Register a clock provider for a node
2679 * @np: Device node pointer associated with clock provider
2680 * @clk_src_get: callback for decoding clock
2681 * @data: context pointer for @clk_src_get callback.
2682 */
2683int of_clk_add_provider(struct device_node *np,
2684 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2685 void *data),
2686 void *data)
2687{
2688 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002689 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002690
2691 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2692 if (!cp)
2693 return -ENOMEM;
2694
2695 cp->node = of_node_get(np);
2696 cp->data = data;
2697 cp->get = clk_src_get;
2698
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002699 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002700 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002701 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002702 pr_debug("Added clock from %s\n", np->full_name);
2703
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002704 ret = of_clk_set_defaults(np, true);
2705 if (ret < 0)
2706 of_clk_del_provider(np);
2707
2708 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002709}
2710EXPORT_SYMBOL_GPL(of_clk_add_provider);
2711
2712/**
2713 * of_clk_del_provider() - Remove a previously registered clock provider
2714 * @np: Device node pointer associated with clock provider
2715 */
2716void of_clk_del_provider(struct device_node *np)
2717{
2718 struct of_clk_provider *cp;
2719
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002720 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002721 list_for_each_entry(cp, &of_clk_providers, link) {
2722 if (cp->node == np) {
2723 list_del(&cp->link);
2724 of_node_put(cp->node);
2725 kfree(cp);
2726 break;
2727 }
2728 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002729 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002730}
2731EXPORT_SYMBOL_GPL(of_clk_del_provider);
2732
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002733struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec)
Grant Likely766e6a42012-04-09 14:50:06 -05002734{
2735 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002736 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002737
2738 /* Check if we have such a provider in our array */
Grant Likely766e6a42012-04-09 14:50:06 -05002739 list_for_each_entry(provider, &of_clk_providers, link) {
2740 if (provider->node == clkspec->np)
2741 clk = provider->get(clkspec, provider->data);
2742 if (!IS_ERR(clk))
2743 break;
2744 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002745
2746 return clk;
2747}
2748
2749struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2750{
2751 struct clk *clk;
2752
2753 mutex_lock(&of_clk_mutex);
2754 clk = __of_clk_get_from_provider(clkspec);
2755 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002756
2757 return clk;
2758}
2759
Mike Turquettef6102742013-10-07 23:12:13 -07002760int of_clk_get_parent_count(struct device_node *np)
2761{
2762 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2763}
2764EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2765
Grant Likely766e6a42012-04-09 14:50:06 -05002766const char *of_clk_get_parent_name(struct device_node *np, int index)
2767{
2768 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002769 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05002770 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002771 const __be32 *vp;
2772 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05002773 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002774 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05002775
2776 if (index < 0)
2777 return NULL;
2778
2779 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2780 &clkspec);
2781 if (rc)
2782 return NULL;
2783
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002784 index = clkspec.args_count ? clkspec.args[0] : 0;
2785 count = 0;
2786
2787 /* if there is an indices property, use it to transfer the index
2788 * specified into an array offset for the clock-output-names property.
2789 */
2790 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
2791 if (index == pv) {
2792 index = count;
2793 break;
2794 }
2795 count++;
2796 }
2797
Grant Likely766e6a42012-04-09 14:50:06 -05002798 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002799 index,
Grant Likely766e6a42012-04-09 14:50:06 -05002800 &clk_name) < 0)
2801 clk_name = clkspec.np->name;
2802
2803 of_node_put(clkspec.np);
2804 return clk_name;
2805}
2806EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
2807
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002808struct clock_provider {
2809 of_clk_init_cb_t clk_init_cb;
2810 struct device_node *np;
2811 struct list_head node;
2812};
2813
2814static LIST_HEAD(clk_provider_list);
2815
2816/*
2817 * This function looks for a parent clock. If there is one, then it
2818 * checks that the provider for this parent clock was initialized, in
2819 * this case the parent clock will be ready.
2820 */
2821static int parent_ready(struct device_node *np)
2822{
2823 int i = 0;
2824
2825 while (true) {
2826 struct clk *clk = of_clk_get(np, i);
2827
2828 /* this parent is ready we can check the next one */
2829 if (!IS_ERR(clk)) {
2830 clk_put(clk);
2831 i++;
2832 continue;
2833 }
2834
2835 /* at least one parent is not ready, we exit now */
2836 if (PTR_ERR(clk) == -EPROBE_DEFER)
2837 return 0;
2838
2839 /*
2840 * Here we make assumption that the device tree is
2841 * written correctly. So an error means that there is
2842 * no more parent. As we didn't exit yet, then the
2843 * previous parent are ready. If there is no clock
2844 * parent, no need to wait for them, then we can
2845 * consider their absence as being ready
2846 */
2847 return 1;
2848 }
2849}
2850
Grant Likely766e6a42012-04-09 14:50:06 -05002851/**
2852 * of_clk_init() - Scan and init clock providers from the DT
2853 * @matches: array of compatible values and init functions for providers.
2854 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002855 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01002856 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002857 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05002858 */
2859void __init of_clk_init(const struct of_device_id *matches)
2860{
Alex Elder7f7ed582013-08-22 11:31:31 -05002861 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05002862 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002863 struct clock_provider *clk_provider, *next;
2864 bool is_init_done;
2865 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05002866
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302867 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03002868 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302869
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002870 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05002871 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002872 struct clock_provider *parent =
2873 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
2874
2875 parent->clk_init_cb = match->data;
2876 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01002877 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002878 }
2879
2880 while (!list_empty(&clk_provider_list)) {
2881 is_init_done = false;
2882 list_for_each_entry_safe(clk_provider, next,
2883 &clk_provider_list, node) {
2884 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002885
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002886 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002887 of_clk_set_defaults(clk_provider->np, true);
2888
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002889 list_del(&clk_provider->node);
2890 kfree(clk_provider);
2891 is_init_done = true;
2892 }
2893 }
2894
2895 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01002896 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002897 * remaining providers during the last loop, so now we
2898 * initialize all the remaining ones unconditionally
2899 * in case the clock parent was not mandatory
2900 */
2901 if (!is_init_done)
2902 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05002903 }
2904}
2905#endif