blob: d60c193b81aab495b789a108e88630068c294c3d [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
12#include <linux/clk-private.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
Mike Turquetteeab89f62013-03-28 13:59:01 -070049/*** locking ***/
50static void clk_prepare_lock(void)
51{
Mike Turquette533ddeb2013-03-28 13:59:02 -070052 if (!mutex_trylock(&prepare_lock)) {
53 if (prepare_owner == current) {
54 prepare_refcnt++;
55 return;
56 }
57 mutex_lock(&prepare_lock);
58 }
59 WARN_ON_ONCE(prepare_owner != NULL);
60 WARN_ON_ONCE(prepare_refcnt != 0);
61 prepare_owner = current;
62 prepare_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -070063}
64
65static void clk_prepare_unlock(void)
66{
Mike Turquette533ddeb2013-03-28 13:59:02 -070067 WARN_ON_ONCE(prepare_owner != current);
68 WARN_ON_ONCE(prepare_refcnt == 0);
69
70 if (--prepare_refcnt)
71 return;
72 prepare_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -070073 mutex_unlock(&prepare_lock);
74}
75
76static unsigned long clk_enable_lock(void)
77{
78 unsigned long flags;
Mike Turquette533ddeb2013-03-28 13:59:02 -070079
80 if (!spin_trylock_irqsave(&enable_lock, flags)) {
81 if (enable_owner == current) {
82 enable_refcnt++;
83 return flags;
84 }
85 spin_lock_irqsave(&enable_lock, flags);
86 }
87 WARN_ON_ONCE(enable_owner != NULL);
88 WARN_ON_ONCE(enable_refcnt != 0);
89 enable_owner = current;
90 enable_refcnt = 1;
Mike Turquetteeab89f62013-03-28 13:59:01 -070091 return flags;
92}
93
94static void clk_enable_unlock(unsigned long flags)
95{
Mike Turquette533ddeb2013-03-28 13:59:02 -070096 WARN_ON_ONCE(enable_owner != current);
97 WARN_ON_ONCE(enable_refcnt == 0);
98
99 if (--enable_refcnt)
100 return;
101 enable_owner = NULL;
Mike Turquetteeab89f62013-03-28 13:59:01 -0700102 spin_unlock_irqrestore(&enable_lock, flags);
103}
104
Mike Turquetteb24764902012-03-15 23:11:19 -0700105/*** debugfs support ***/
106
Mike Turquetteea72dc22013-12-18 21:38:52 -0800107#ifdef CONFIG_DEBUG_FS
Mike Turquetteb24764902012-03-15 23:11:19 -0700108#include <linux/debugfs.h>
109
110static struct dentry *rootdir;
Mike Turquetteb24764902012-03-15 23:11:19 -0700111static int inited = 0;
Stephen Boyd6314b672014-09-04 23:37:49 -0700112static DEFINE_MUTEX(clk_debug_lock);
113static HLIST_HEAD(clk_debug_list);
Mike Turquetteb24764902012-03-15 23:11:19 -0700114
Sachin Kamat6b44c8542014-07-01 11:56:34 +0530115static struct hlist_head *all_lists[] = {
116 &clk_root_list,
117 &clk_orphan_list,
118 NULL,
119};
120
121static struct hlist_head *orphan_list[] = {
122 &clk_orphan_list,
123 NULL,
124};
125
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100126static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
127 int level)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530128{
129 if (!c)
130 return;
131
Mike Turquettee59c5372014-02-18 21:21:25 -0800132 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530133 level * 3 + 1, "",
134 30 - level * 3, c->name,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100135 c->enable_count, c->prepare_count, clk_core_get_rate(c),
136 clk_core_get_accuracy(c), clk_core_get_phase(c));
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530137}
138
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100139static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530140 int level)
141{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100142 struct clk_core *child;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530143
144 if (!c)
145 return;
146
147 clk_summary_show_one(s, c, level);
148
Sasha Levinb67bfe02013-02-27 17:06:00 -0800149 hlist_for_each_entry(child, &c->children, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530150 clk_summary_show_subtree(s, child, level + 1);
151}
152
153static int clk_summary_show(struct seq_file *s, void *data)
154{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100155 struct clk_core *c;
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300156 struct hlist_head **lists = (struct hlist_head **)s->private;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530157
Mike Turquettee59c5372014-02-18 21:21:25 -0800158 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
159 seq_puts(s, "----------------------------------------------------------------------------------------\n");
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530160
Mike Turquetteeab89f62013-03-28 13:59:01 -0700161 clk_prepare_lock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530162
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300163 for (; *lists; lists++)
164 hlist_for_each_entry(c, *lists, child_node)
165 clk_summary_show_subtree(s, c, 0);
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530166
Mike Turquetteeab89f62013-03-28 13:59:01 -0700167 clk_prepare_unlock();
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530168
169 return 0;
170}
171
172
173static int clk_summary_open(struct inode *inode, struct file *file)
174{
175 return single_open(file, clk_summary_show, inode->i_private);
176}
177
178static const struct file_operations clk_summary_fops = {
179 .open = clk_summary_open,
180 .read = seq_read,
181 .llseek = seq_lseek,
182 .release = single_release,
183};
184
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100185static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530186{
187 if (!c)
188 return;
189
190 seq_printf(s, "\"%s\": { ", c->name);
191 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
192 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100193 seq_printf(s, "\"rate\": %lu", clk_core_get_rate(c));
194 seq_printf(s, "\"accuracy\": %lu", clk_core_get_accuracy(c));
195 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530196}
197
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100198static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530199{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100200 struct clk_core *child;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530201
202 if (!c)
203 return;
204
205 clk_dump_one(s, c, level);
206
Sasha Levinb67bfe02013-02-27 17:06:00 -0800207 hlist_for_each_entry(child, &c->children, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530208 seq_printf(s, ",");
209 clk_dump_subtree(s, child, level + 1);
210 }
211
212 seq_printf(s, "}");
213}
214
215static int clk_dump(struct seq_file *s, void *data)
216{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100217 struct clk_core *c;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530218 bool first_node = true;
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300219 struct hlist_head **lists = (struct hlist_head **)s->private;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530220
221 seq_printf(s, "{");
222
Mike Turquetteeab89f62013-03-28 13:59:01 -0700223 clk_prepare_lock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530224
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300225 for (; *lists; lists++) {
226 hlist_for_each_entry(c, *lists, child_node) {
227 if (!first_node)
228 seq_puts(s, ",");
229 first_node = false;
230 clk_dump_subtree(s, c, 0);
231 }
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530232 }
233
Mike Turquetteeab89f62013-03-28 13:59:01 -0700234 clk_prepare_unlock();
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530235
236 seq_printf(s, "}");
237 return 0;
238}
239
240
241static int clk_dump_open(struct inode *inode, struct file *file)
242{
243 return single_open(file, clk_dump, inode->i_private);
244}
245
246static const struct file_operations clk_dump_fops = {
247 .open = clk_dump_open,
248 .read = seq_read,
249 .llseek = seq_lseek,
250 .release = single_release,
251};
252
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100253static int clk_debug_create_one(struct clk_core *clk, struct dentry *pdentry)
Mike Turquetteb24764902012-03-15 23:11:19 -0700254{
255 struct dentry *d;
256 int ret = -ENOMEM;
257
258 if (!clk || !pdentry) {
259 ret = -EINVAL;
260 goto out;
261 }
262
263 d = debugfs_create_dir(clk->name, pdentry);
264 if (!d)
265 goto out;
266
267 clk->dentry = d;
268
269 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
270 (u32 *)&clk->rate);
271 if (!d)
272 goto err_out;
273
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100274 d = debugfs_create_u32("clk_accuracy", S_IRUGO, clk->dentry,
275 (u32 *)&clk->accuracy);
276 if (!d)
277 goto err_out;
278
Mike Turquettee59c5372014-02-18 21:21:25 -0800279 d = debugfs_create_u32("clk_phase", S_IRUGO, clk->dentry,
280 (u32 *)&clk->phase);
281 if (!d)
282 goto err_out;
283
Mike Turquetteb24764902012-03-15 23:11:19 -0700284 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
285 (u32 *)&clk->flags);
286 if (!d)
287 goto err_out;
288
289 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
290 (u32 *)&clk->prepare_count);
291 if (!d)
292 goto err_out;
293
294 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
295 (u32 *)&clk->enable_count);
296 if (!d)
297 goto err_out;
298
299 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
300 (u32 *)&clk->notifier_count);
301 if (!d)
302 goto err_out;
303
Chris Brandabeab452014-07-03 14:01:29 -0700304 if (clk->ops->debug_init) {
305 ret = clk->ops->debug_init(clk->hw, clk->dentry);
306 if (ret)
Alex Elderc646cbf2014-03-21 06:43:56 -0500307 goto err_out;
Chris Brandabeab452014-07-03 14:01:29 -0700308 }
Alex Elderc646cbf2014-03-21 06:43:56 -0500309
Mike Turquetteb24764902012-03-15 23:11:19 -0700310 ret = 0;
311 goto out;
312
313err_out:
Alex Elderb5f98e62013-11-27 09:39:49 -0600314 debugfs_remove_recursive(clk->dentry);
315 clk->dentry = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -0700316out:
317 return ret;
318}
319
Mike Turquetteb24764902012-03-15 23:11:19 -0700320/**
321 * clk_debug_register - add a clk node to the debugfs clk tree
322 * @clk: the clk being added to the debugfs clk tree
323 *
324 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
325 * initialized. Otherwise it bails out early since the debugfs clk tree
326 * will be created lazily by clk_debug_init as part of a late_initcall.
Mike Turquetteb24764902012-03-15 23:11:19 -0700327 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100328static int clk_debug_register(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700329{
Mike Turquetteb24764902012-03-15 23:11:19 -0700330 int ret = 0;
331
Stephen Boyd6314b672014-09-04 23:37:49 -0700332 mutex_lock(&clk_debug_lock);
333 hlist_add_head(&clk->debug_node, &clk_debug_list);
334
Mike Turquetteb24764902012-03-15 23:11:19 -0700335 if (!inited)
Stephen Boyd6314b672014-09-04 23:37:49 -0700336 goto unlock;
Mike Turquetteb24764902012-03-15 23:11:19 -0700337
Stephen Boyd6314b672014-09-04 23:37:49 -0700338 ret = clk_debug_create_one(clk, rootdir);
339unlock:
340 mutex_unlock(&clk_debug_lock);
Mike Turquetteb24764902012-03-15 23:11:19 -0700341
Mike Turquetteb24764902012-03-15 23:11:19 -0700342 return ret;
343}
344
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200345 /**
346 * clk_debug_unregister - remove a clk node from the debugfs clk tree
347 * @clk: the clk being removed from the debugfs clk tree
348 *
349 * Dynamically removes a clk and all it's children clk nodes from the
350 * debugfs clk tree if clk->dentry points to debugfs created by
351 * clk_debug_register in __clk_init.
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200352 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100353static void clk_debug_unregister(struct clk_core *clk)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200354{
Stephen Boyd6314b672014-09-04 23:37:49 -0700355 mutex_lock(&clk_debug_lock);
Stephen Boyd6314b672014-09-04 23:37:49 -0700356 hlist_del_init(&clk->debug_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200357 debugfs_remove_recursive(clk->dentry);
Stephen Boyd6314b672014-09-04 23:37:49 -0700358 clk->dentry = NULL;
Stephen Boyd6314b672014-09-04 23:37:49 -0700359 mutex_unlock(&clk_debug_lock);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200360}
361
Tomeu Vizoso61c7cdd2014-12-02 08:54:21 +0100362struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
Peter De Schrijverfb2b3c92014-06-26 18:00:53 +0300363 void *data, const struct file_operations *fops)
364{
365 struct dentry *d = NULL;
366
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100367 if (hw->core->dentry)
368 d = debugfs_create_file(name, mode, hw->core->dentry, data,
369 fops);
Peter De Schrijverfb2b3c92014-06-26 18:00:53 +0300370
371 return d;
372}
373EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
374
Mike Turquetteb24764902012-03-15 23:11:19 -0700375/**
376 * clk_debug_init - lazily create the debugfs clk tree visualization
377 *
378 * clks are often initialized very early during boot before memory can
379 * be dynamically allocated and well before debugfs is setup.
380 * clk_debug_init walks the clk tree hierarchy while holding
381 * prepare_lock and creates the topology as part of a late_initcall,
382 * thus insuring that clks initialized very early will still be
383 * represented in the debugfs clk tree. This function should only be
384 * called once at boot-time, and all other clks added dynamically will
385 * be done so with clk_debug_register.
386 */
387static int __init clk_debug_init(void)
388{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100389 struct clk_core *clk;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530390 struct dentry *d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700391
392 rootdir = debugfs_create_dir("clk", NULL);
393
394 if (!rootdir)
395 return -ENOMEM;
396
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300397 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530398 &clk_summary_fops);
399 if (!d)
400 return -ENOMEM;
401
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300402 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530403 &clk_dump_fops);
404 if (!d)
405 return -ENOMEM;
406
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300407 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
408 &orphan_list, &clk_summary_fops);
409 if (!d)
410 return -ENOMEM;
Mike Turquetteb24764902012-03-15 23:11:19 -0700411
Peter De Schrijver27b8d5f2014-05-30 18:03:57 +0300412 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
413 &orphan_list, &clk_dump_fops);
414 if (!d)
Mike Turquetteb24764902012-03-15 23:11:19 -0700415 return -ENOMEM;
416
Stephen Boyd6314b672014-09-04 23:37:49 -0700417 mutex_lock(&clk_debug_lock);
418 hlist_for_each_entry(clk, &clk_debug_list, debug_node)
419 clk_debug_create_one(clk, rootdir);
Mike Turquetteb24764902012-03-15 23:11:19 -0700420
421 inited = 1;
Stephen Boyd6314b672014-09-04 23:37:49 -0700422 mutex_unlock(&clk_debug_lock);
Mike Turquetteb24764902012-03-15 23:11:19 -0700423
424 return 0;
425}
426late_initcall(clk_debug_init);
427#else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100428static inline int clk_debug_register(struct clk_core *clk) { return 0; }
429static inline void clk_debug_reparent(struct clk_core *clk,
430 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +0200431{
432}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100433static inline void clk_debug_unregister(struct clk_core *clk)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +0200434{
435}
Mike Turquette70d347e2012-03-26 11:53:47 -0700436#endif
Mike Turquetteb24764902012-03-15 23:11:19 -0700437
Mike Turquetteb24764902012-03-15 23:11:19 -0700438/* caller must hold prepare_lock */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100439static void clk_unprepare_unused_subtree(struct clk_core *clk)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100440{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100441 struct clk_core *child;
Ulf Hansson1c155b32013-03-12 20:26:03 +0100442
Ulf Hansson1c155b32013-03-12 20:26:03 +0100443 hlist_for_each_entry(child, &clk->children, child_node)
444 clk_unprepare_unused_subtree(child);
445
446 if (clk->prepare_count)
447 return;
448
449 if (clk->flags & CLK_IGNORE_UNUSED)
450 return;
451
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100452 if (clk_core_is_prepared(clk)) {
Ulf Hansson3cc82472013-03-12 20:26:04 +0100453 if (clk->ops->unprepare_unused)
454 clk->ops->unprepare_unused(clk->hw);
455 else if (clk->ops->unprepare)
Ulf Hansson1c155b32013-03-12 20:26:03 +0100456 clk->ops->unprepare(clk->hw);
Ulf Hansson3cc82472013-03-12 20:26:04 +0100457 }
Ulf Hansson1c155b32013-03-12 20:26:03 +0100458}
459
460/* caller must hold prepare_lock */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100461static void clk_disable_unused_subtree(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700462{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100463 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700464 unsigned long flags;
465
Sasha Levinb67bfe02013-02-27 17:06:00 -0800466 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700467 clk_disable_unused_subtree(child);
468
Mike Turquetteeab89f62013-03-28 13:59:01 -0700469 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700470
471 if (clk->enable_count)
472 goto unlock_out;
473
474 if (clk->flags & CLK_IGNORE_UNUSED)
475 goto unlock_out;
476
Mike Turquette7c045a52012-12-04 11:00:35 -0800477 /*
478 * some gate clocks have special needs during the disable-unused
479 * sequence. call .disable_unused if available, otherwise fall
480 * back to .disable
481 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100482 if (clk_core_is_enabled(clk)) {
Mike Turquette7c045a52012-12-04 11:00:35 -0800483 if (clk->ops->disable_unused)
484 clk->ops->disable_unused(clk->hw);
485 else if (clk->ops->disable)
486 clk->ops->disable(clk->hw);
487 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700488
489unlock_out:
Mike Turquetteeab89f62013-03-28 13:59:01 -0700490 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700491}
492
Olof Johansson1e435252013-04-27 14:10:18 -0700493static bool clk_ignore_unused;
494static int __init clk_ignore_unused_setup(char *__unused)
495{
496 clk_ignore_unused = true;
497 return 1;
498}
499__setup("clk_ignore_unused", clk_ignore_unused_setup);
500
Mike Turquetteb24764902012-03-15 23:11:19 -0700501static int clk_disable_unused(void)
502{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100503 struct clk_core *clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700504
Olof Johansson1e435252013-04-27 14:10:18 -0700505 if (clk_ignore_unused) {
506 pr_warn("clk: Not disabling unused clocks\n");
507 return 0;
508 }
509
Mike Turquetteeab89f62013-03-28 13:59:01 -0700510 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700511
Sasha Levinb67bfe02013-02-27 17:06:00 -0800512 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700513 clk_disable_unused_subtree(clk);
514
Sasha Levinb67bfe02013-02-27 17:06:00 -0800515 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700516 clk_disable_unused_subtree(clk);
517
Ulf Hansson1c155b32013-03-12 20:26:03 +0100518 hlist_for_each_entry(clk, &clk_root_list, child_node)
519 clk_unprepare_unused_subtree(clk);
520
521 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
522 clk_unprepare_unused_subtree(clk);
523
Mike Turquetteeab89f62013-03-28 13:59:01 -0700524 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700525
526 return 0;
527}
Saravana Kannand41d5802013-05-09 11:35:01 -0700528late_initcall_sync(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700529
530/*** helper functions ***/
531
Russ Dill65800b22012-11-26 11:20:09 -0800532const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700533{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100534 return !clk ? NULL : clk->core->name;
Mike Turquetteb24764902012-03-15 23:11:19 -0700535}
Niels de Vos48950842012-12-13 13:12:25 +0100536EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700537
Russ Dill65800b22012-11-26 11:20:09 -0800538struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700539{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100540 return !clk ? NULL : clk->core->hw;
Mike Turquetteb24764902012-03-15 23:11:19 -0700541}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800542EXPORT_SYMBOL_GPL(__clk_get_hw);
Mike Turquetteb24764902012-03-15 23:11:19 -0700543
Russ Dill65800b22012-11-26 11:20:09 -0800544u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700545{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100546 return !clk ? 0 : clk->core->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700547}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800548EXPORT_SYMBOL_GPL(__clk_get_num_parents);
Mike Turquetteb24764902012-03-15 23:11:19 -0700549
Russ Dill65800b22012-11-26 11:20:09 -0800550struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700551{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100552 if (!clk)
553 return NULL;
554
555 /* TODO: Create a per-user clk and change callers to call clk_put */
556 return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700557}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800558EXPORT_SYMBOL_GPL(__clk_get_parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700559
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100560static struct clk_core *clk_core_get_parent_by_index(struct clk_core *clk,
561 u8 index)
James Hogan7ef3dcc2013-07-29 12:24:58 +0100562{
563 if (!clk || index >= clk->num_parents)
564 return NULL;
565 else if (!clk->parents)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100566 return clk_core_lookup(clk->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100567 else if (!clk->parents[index])
568 return clk->parents[index] =
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100569 clk_core_lookup(clk->parent_names[index]);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100570 else
571 return clk->parents[index];
572}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100573
574struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
575{
576 struct clk_core *parent;
577
578 if (!clk)
579 return NULL;
580
581 parent = clk_core_get_parent_by_index(clk->core, index);
582
583 return !parent ? NULL : parent->hw->clk;
584}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800585EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
James Hogan7ef3dcc2013-07-29 12:24:58 +0100586
Russ Dill65800b22012-11-26 11:20:09 -0800587unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700588{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100589 return !clk ? 0 : clk->core->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700590}
591
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100592static unsigned long clk_core_get_rate_nolock(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700593{
594 unsigned long ret;
595
596 if (!clk) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530597 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700598 goto out;
599 }
600
601 ret = clk->rate;
602
603 if (clk->flags & CLK_IS_ROOT)
604 goto out;
605
606 if (!clk->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530607 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700608
609out:
610 return ret;
611}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100612
613unsigned long __clk_get_rate(struct clk *clk)
614{
615 if (!clk)
616 return 0;
617
618 return clk_core_get_rate_nolock(clk->core);
619}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800620EXPORT_SYMBOL_GPL(__clk_get_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700621
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100622static unsigned long __clk_get_accuracy(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +0100623{
624 if (!clk)
625 return 0;
626
627 return clk->accuracy;
628}
629
Russ Dill65800b22012-11-26 11:20:09 -0800630unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700631{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100632 return !clk ? 0 : clk->core->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700633}
Thierry Redingb05c6832013-09-03 09:43:51 +0200634EXPORT_SYMBOL_GPL(__clk_get_flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700635
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100636static bool clk_core_is_prepared(struct clk_core *clk)
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100637{
638 int ret;
639
640 if (!clk)
641 return false;
642
643 /*
644 * .is_prepared is optional for clocks that can prepare
645 * fall back to software usage counter if it is missing
646 */
647 if (!clk->ops->is_prepared) {
648 ret = clk->prepare_count ? 1 : 0;
649 goto out;
650 }
651
652 ret = clk->ops->is_prepared(clk->hw);
653out:
654 return !!ret;
655}
656
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100657bool __clk_is_prepared(struct clk *clk)
658{
659 if (!clk)
660 return false;
661
662 return clk_core_is_prepared(clk->core);
663}
664
665static bool clk_core_is_enabled(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700666{
667 int ret;
668
669 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700670 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700671
672 /*
673 * .is_enabled is only mandatory for clocks that gate
674 * fall back to software usage counter if .is_enabled is missing
675 */
676 if (!clk->ops->is_enabled) {
677 ret = clk->enable_count ? 1 : 0;
678 goto out;
679 }
680
681 ret = clk->ops->is_enabled(clk->hw);
682out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700683 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700684}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100685
686bool __clk_is_enabled(struct clk *clk)
687{
688 if (!clk)
689 return false;
690
691 return clk_core_is_enabled(clk->core);
692}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800693EXPORT_SYMBOL_GPL(__clk_is_enabled);
Mike Turquetteb24764902012-03-15 23:11:19 -0700694
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100695static struct clk_core *__clk_lookup_subtree(const char *name,
696 struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700697{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100698 struct clk_core *child;
699 struct clk_core *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700700
701 if (!strcmp(clk->name, name))
702 return clk;
703
Sasha Levinb67bfe02013-02-27 17:06:00 -0800704 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700705 ret = __clk_lookup_subtree(name, child);
706 if (ret)
707 return ret;
708 }
709
710 return NULL;
711}
712
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100713static struct clk_core *clk_core_lookup(const char *name)
Mike Turquetteb24764902012-03-15 23:11:19 -0700714{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100715 struct clk_core *root_clk;
716 struct clk_core *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700717
718 if (!name)
719 return NULL;
720
721 /* search the 'proper' clk tree first */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800722 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700723 ret = __clk_lookup_subtree(name, root_clk);
724 if (ret)
725 return ret;
726 }
727
728 /* if not found, then search the orphan tree */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800729 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700730 ret = __clk_lookup_subtree(name, root_clk);
731 if (ret)
732 return ret;
733 }
734
735 return NULL;
736}
737
Stephen Boyd15a02c12015-01-19 18:05:28 -0800738static bool mux_is_better_rate(unsigned long rate, unsigned long now,
739 unsigned long best, unsigned long flags)
740{
741 if (flags & CLK_MUX_ROUND_CLOSEST)
742 return abs(now - rate) < abs(best - rate);
743
744 return now <= rate && now > best;
745}
746
747static long
748clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate,
749 unsigned long *best_parent_rate,
750 struct clk_hw **best_parent_p,
751 unsigned long flags)
James Hogane366fdd2013-07-29 12:25:02 +0100752{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100753 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
James Hogane366fdd2013-07-29 12:25:02 +0100754 int i, num_parents;
755 unsigned long parent_rate, best = 0;
756
757 /* if NO_REPARENT flag set, pass through to current parent */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100758 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
759 parent = core->parent;
760 if (core->flags & CLK_SET_RATE_PARENT)
761 best = clk_core_round_rate_nolock(parent, rate);
James Hogane366fdd2013-07-29 12:25:02 +0100762 else if (parent)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100763 best = clk_core_get_rate_nolock(parent);
James Hogane366fdd2013-07-29 12:25:02 +0100764 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100765 best = clk_core_get_rate_nolock(core);
James Hogane366fdd2013-07-29 12:25:02 +0100766 goto out;
767 }
768
769 /* find the parent that can provide the fastest rate <= rate */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100770 num_parents = core->num_parents;
James Hogane366fdd2013-07-29 12:25:02 +0100771 for (i = 0; i < num_parents; i++) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100772 parent = clk_core_get_parent_by_index(core, i);
James Hogane366fdd2013-07-29 12:25:02 +0100773 if (!parent)
774 continue;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100775 if (core->flags & CLK_SET_RATE_PARENT)
776 parent_rate = clk_core_round_rate_nolock(parent, rate);
James Hogane366fdd2013-07-29 12:25:02 +0100777 else
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100778 parent_rate = clk_core_get_rate_nolock(parent);
Stephen Boyd15a02c12015-01-19 18:05:28 -0800779 if (mux_is_better_rate(rate, parent_rate, best, flags)) {
James Hogane366fdd2013-07-29 12:25:02 +0100780 best_parent = parent;
781 best = parent_rate;
782 }
783 }
784
785out:
786 if (best_parent)
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100787 *best_parent_p = best_parent->hw;
James Hogane366fdd2013-07-29 12:25:02 +0100788 *best_parent_rate = best;
789
790 return best;
791}
Stephen Boyd15a02c12015-01-19 18:05:28 -0800792
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100793struct clk *__clk_lookup(const char *name)
794{
795 struct clk_core *core = clk_core_lookup(name);
796
797 return !core ? NULL : core->hw->clk;
798}
799
Stephen Boyd15a02c12015-01-19 18:05:28 -0800800/*
801 * Helper for finding best parent to provide a given frequency. This can be used
802 * directly as a determine_rate callback (e.g. for a mux), or from a more
803 * complex clock that may combine a mux with other operations.
804 */
805long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
806 unsigned long *best_parent_rate,
807 struct clk_hw **best_parent_p)
808{
809 return clk_mux_determine_rate_flags(hw, rate, best_parent_rate,
810 best_parent_p, 0);
811}
Stephen Boyd0b7f04b2014-01-17 19:47:17 -0800812EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
James Hogane366fdd2013-07-29 12:25:02 +0100813
Stephen Boyd15a02c12015-01-19 18:05:28 -0800814long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate,
815 unsigned long *best_parent_rate,
816 struct clk_hw **best_parent_p)
817{
818 return clk_mux_determine_rate_flags(hw, rate, best_parent_rate,
819 best_parent_p,
820 CLK_MUX_ROUND_CLOSEST);
821}
822EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
823
Mike Turquetteb24764902012-03-15 23:11:19 -0700824/*** clk api ***/
825
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100826static void clk_core_unprepare(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700827{
828 if (!clk)
829 return;
830
831 if (WARN_ON(clk->prepare_count == 0))
832 return;
833
834 if (--clk->prepare_count > 0)
835 return;
836
837 WARN_ON(clk->enable_count > 0);
838
839 if (clk->ops->unprepare)
840 clk->ops->unprepare(clk->hw);
841
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100842 clk_core_unprepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700843}
844
845/**
846 * clk_unprepare - undo preparation of a clock source
Peter Meerwald24ee1a02013-06-29 15:14:19 +0200847 * @clk: the clk being unprepared
Mike Turquetteb24764902012-03-15 23:11:19 -0700848 *
849 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
850 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
851 * if the operation may sleep. One example is a clk which is accessed over
852 * I2c. In the complex case a clk gate operation may require a fast and a slow
853 * part. It is this reason that clk_unprepare and clk_disable are not mutually
854 * exclusive. In fact clk_disable must be called before clk_unprepare.
855 */
856void clk_unprepare(struct clk *clk)
857{
Stephen Boyd63589e92014-03-26 16:06:37 -0700858 if (IS_ERR_OR_NULL(clk))
859 return;
860
Mike Turquetteeab89f62013-03-28 13:59:01 -0700861 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100862 clk_core_unprepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700863 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700864}
865EXPORT_SYMBOL_GPL(clk_unprepare);
866
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100867static int clk_core_prepare(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700868{
869 int ret = 0;
870
871 if (!clk)
872 return 0;
873
874 if (clk->prepare_count == 0) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100875 ret = clk_core_prepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700876 if (ret)
877 return ret;
878
879 if (clk->ops->prepare) {
880 ret = clk->ops->prepare(clk->hw);
881 if (ret) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100882 clk_core_unprepare(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700883 return ret;
884 }
885 }
886 }
887
888 clk->prepare_count++;
889
890 return 0;
891}
892
893/**
894 * clk_prepare - prepare a clock source
895 * @clk: the clk being prepared
896 *
897 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
898 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
899 * operation may sleep. One example is a clk which is accessed over I2c. In
900 * the complex case a clk ungate operation may require a fast and a slow part.
901 * It is this reason that clk_prepare and clk_enable are not mutually
902 * exclusive. In fact clk_prepare must be called before clk_enable.
903 * Returns 0 on success, -EERROR otherwise.
904 */
905int clk_prepare(struct clk *clk)
906{
907 int ret;
908
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100909 if (!clk)
910 return 0;
911
Mike Turquetteeab89f62013-03-28 13:59:01 -0700912 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100913 ret = clk_core_prepare(clk->core);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700914 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700915
916 return ret;
917}
918EXPORT_SYMBOL_GPL(clk_prepare);
919
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100920static void clk_core_disable(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700921{
922 if (!clk)
923 return;
924
925 if (WARN_ON(clk->enable_count == 0))
926 return;
927
928 if (--clk->enable_count > 0)
929 return;
930
931 if (clk->ops->disable)
932 clk->ops->disable(clk->hw);
933
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100934 clk_core_disable(clk->parent);
935}
936
937static void __clk_disable(struct clk *clk)
938{
939 if (!clk)
940 return;
941
942 clk_core_disable(clk->core);
Mike Turquetteb24764902012-03-15 23:11:19 -0700943}
944
945/**
946 * clk_disable - gate a clock
947 * @clk: the clk being gated
948 *
949 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
950 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
951 * clk if the operation is fast and will never sleep. One example is a
952 * SoC-internal clk which is controlled via simple register writes. In the
953 * complex case a clk gate operation may require a fast and a slow part. It is
954 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
955 * In fact clk_disable must be called before clk_unprepare.
956 */
957void clk_disable(struct clk *clk)
958{
959 unsigned long flags;
960
Stephen Boyd63589e92014-03-26 16:06:37 -0700961 if (IS_ERR_OR_NULL(clk))
962 return;
963
Mike Turquetteeab89f62013-03-28 13:59:01 -0700964 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -0700965 __clk_disable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -0700966 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -0700967}
968EXPORT_SYMBOL_GPL(clk_disable);
969
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100970static int clk_core_enable(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700971{
972 int ret = 0;
973
974 if (!clk)
975 return 0;
976
977 if (WARN_ON(clk->prepare_count == 0))
978 return -ESHUTDOWN;
979
980 if (clk->enable_count == 0) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100981 ret = clk_core_enable(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700982
983 if (ret)
984 return ret;
985
986 if (clk->ops->enable) {
987 ret = clk->ops->enable(clk->hw);
988 if (ret) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100989 clk_core_disable(clk->parent);
Mike Turquetteb24764902012-03-15 23:11:19 -0700990 return ret;
991 }
992 }
993 }
994
995 clk->enable_count++;
996 return 0;
997}
998
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100999static int __clk_enable(struct clk *clk)
1000{
1001 if (!clk)
1002 return 0;
1003
1004 return clk_core_enable(clk->core);
1005}
1006
Mike Turquetteb24764902012-03-15 23:11:19 -07001007/**
1008 * clk_enable - ungate a clock
1009 * @clk: the clk being ungated
1010 *
1011 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1012 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1013 * if the operation will never sleep. One example is a SoC-internal clk which
1014 * is controlled via simple register writes. In the complex case a clk ungate
1015 * operation may require a fast and a slow part. It is this reason that
1016 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1017 * must be called before clk_enable. Returns 0 on success, -EERROR
1018 * otherwise.
1019 */
1020int clk_enable(struct clk *clk)
1021{
1022 unsigned long flags;
1023 int ret;
1024
Mike Turquetteeab89f62013-03-28 13:59:01 -07001025 flags = clk_enable_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001026 ret = __clk_enable(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001027 clk_enable_unlock(flags);
Mike Turquetteb24764902012-03-15 23:11:19 -07001028
1029 return ret;
1030}
1031EXPORT_SYMBOL_GPL(clk_enable);
1032
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001033static unsigned long clk_core_round_rate_nolock(struct clk_core *clk,
1034 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001035{
Shawn Guo81536e02012-04-12 20:50:17 +08001036 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001037 struct clk_core *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001038 struct clk_hw *parent_hw;
Mike Turquetteb24764902012-03-15 23:11:19 -07001039
1040 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -07001041 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001042
James Hogan71472c02013-07-29 12:25:00 +01001043 parent = clk->parent;
1044 if (parent)
1045 parent_rate = parent->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001046
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001047 if (clk->ops->determine_rate) {
1048 parent_hw = parent ? parent->hw : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001049 return clk->ops->determine_rate(clk->hw, rate, &parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001050 &parent_hw);
1051 } else if (clk->ops->round_rate)
James Hogan71472c02013-07-29 12:25:00 +01001052 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
1053 else if (clk->flags & CLK_SET_RATE_PARENT)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001054 return clk_core_round_rate_nolock(clk->parent, rate);
James Hogan71472c02013-07-29 12:25:00 +01001055 else
1056 return clk->rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001057}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001058
1059/**
1060 * __clk_round_rate - round the given rate for a clk
1061 * @clk: round the rate of this clock
1062 * @rate: the rate which is to be rounded
1063 *
1064 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
1065 */
1066unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
1067{
1068 if (!clk)
1069 return 0;
1070
1071 return clk_core_round_rate_nolock(clk->core, rate);
1072}
Arnd Bergmann1cdf8ee2014-06-03 11:40:14 +02001073EXPORT_SYMBOL_GPL(__clk_round_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001074
1075/**
1076 * clk_round_rate - round the given rate for a clk
1077 * @clk: the clk for which we are rounding a rate
1078 * @rate: the rate which is to be rounded
1079 *
1080 * Takes in a rate as input and rounds it to a rate that the clk can actually
1081 * use which is then returned. If clk doesn't support round_rate operation
1082 * then the parent rate is returned.
1083 */
1084long clk_round_rate(struct clk *clk, unsigned long rate)
1085{
1086 unsigned long ret;
1087
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001088 if (!clk)
1089 return 0;
1090
Mike Turquetteeab89f62013-03-28 13:59:01 -07001091 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001092 ret = clk_core_round_rate_nolock(clk->core, rate);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001093 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001094
1095 return ret;
1096}
1097EXPORT_SYMBOL_GPL(clk_round_rate);
1098
1099/**
1100 * __clk_notify - call clk notifier chain
1101 * @clk: struct clk * that is changing rate
1102 * @msg: clk notifier type (see include/linux/clk.h)
1103 * @old_rate: old clk rate
1104 * @new_rate: new clk rate
1105 *
1106 * Triggers a notifier call chain on the clk rate-change notification
1107 * for 'clk'. Passes a pointer to the struct clk and the previous
1108 * and current rates to the notifier callback. Intended to be called by
1109 * internal clock code only. Returns NOTIFY_DONE from the last driver
1110 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1111 * a driver returns that.
1112 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001113static int __clk_notify(struct clk_core *clk, unsigned long msg,
Mike Turquetteb24764902012-03-15 23:11:19 -07001114 unsigned long old_rate, unsigned long new_rate)
1115{
1116 struct clk_notifier *cn;
1117 struct clk_notifier_data cnd;
1118 int ret = NOTIFY_DONE;
1119
Mike Turquetteb24764902012-03-15 23:11:19 -07001120 cnd.old_rate = old_rate;
1121 cnd.new_rate = new_rate;
1122
1123 list_for_each_entry(cn, &clk_notifier_list, node) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001124 if (cn->clk->core == clk) {
1125 cnd.clk = cn->clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001126 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1127 &cnd);
Mike Turquetteb24764902012-03-15 23:11:19 -07001128 }
1129 }
1130
1131 return ret;
1132}
1133
1134/**
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001135 * __clk_recalc_accuracies
1136 * @clk: first clk in the subtree
1137 *
1138 * Walks the subtree of clks starting with clk and recalculates accuracies as
1139 * it goes. Note that if a clk does not implement the .recalc_accuracy
1140 * callback then it is assumed that the clock will take on the accuracy of it's
1141 * parent.
1142 *
1143 * Caller must hold prepare_lock.
1144 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001145static void __clk_recalc_accuracies(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001146{
1147 unsigned long parent_accuracy = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001148 struct clk_core *child;
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001149
1150 if (clk->parent)
1151 parent_accuracy = clk->parent->accuracy;
1152
1153 if (clk->ops->recalc_accuracy)
1154 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
1155 parent_accuracy);
1156 else
1157 clk->accuracy = parent_accuracy;
1158
1159 hlist_for_each_entry(child, &clk->children, child_node)
1160 __clk_recalc_accuracies(child);
1161}
1162
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001163static long clk_core_get_accuracy(struct clk_core *clk)
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001164{
1165 unsigned long accuracy;
1166
1167 clk_prepare_lock();
1168 if (clk && (clk->flags & CLK_GET_ACCURACY_NOCACHE))
1169 __clk_recalc_accuracies(clk);
1170
1171 accuracy = __clk_get_accuracy(clk);
1172 clk_prepare_unlock();
1173
1174 return accuracy;
1175}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001176
1177/**
1178 * clk_get_accuracy - return the accuracy of clk
1179 * @clk: the clk whose accuracy is being returned
1180 *
1181 * Simply returns the cached accuracy of the clk, unless
1182 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1183 * issued.
1184 * If clk is NULL then returns 0.
1185 */
1186long clk_get_accuracy(struct clk *clk)
1187{
1188 if (!clk)
1189 return 0;
1190
1191 return clk_core_get_accuracy(clk->core);
1192}
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001193EXPORT_SYMBOL_GPL(clk_get_accuracy);
1194
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001195static unsigned long clk_recalc(struct clk_core *clk,
1196 unsigned long parent_rate)
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001197{
1198 if (clk->ops->recalc_rate)
1199 return clk->ops->recalc_rate(clk->hw, parent_rate);
1200 return parent_rate;
1201}
1202
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001203/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001204 * __clk_recalc_rates
1205 * @clk: first clk in the subtree
1206 * @msg: notification type (see include/linux/clk.h)
1207 *
1208 * Walks the subtree of clks starting with clk and recalculates rates as it
1209 * goes. Note that if a clk does not implement the .recalc_rate callback then
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001210 * it is assumed that the clock will take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001211 *
1212 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1213 * if necessary.
1214 *
1215 * Caller must hold prepare_lock.
1216 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001217static void __clk_recalc_rates(struct clk_core *clk, unsigned long msg)
Mike Turquetteb24764902012-03-15 23:11:19 -07001218{
1219 unsigned long old_rate;
1220 unsigned long parent_rate = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001221 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001222
1223 old_rate = clk->rate;
1224
1225 if (clk->parent)
1226 parent_rate = clk->parent->rate;
1227
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001228 clk->rate = clk_recalc(clk, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001229
1230 /*
1231 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1232 * & ABORT_RATE_CHANGE notifiers
1233 */
1234 if (clk->notifier_count && msg)
1235 __clk_notify(clk, msg, old_rate, clk->rate);
1236
Sasha Levinb67bfe02013-02-27 17:06:00 -08001237 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001238 __clk_recalc_rates(child, msg);
1239}
1240
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001241static unsigned long clk_core_get_rate(struct clk_core *clk)
1242{
1243 unsigned long rate;
1244
1245 clk_prepare_lock();
1246
1247 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
1248 __clk_recalc_rates(clk, 0);
1249
1250 rate = clk_core_get_rate_nolock(clk);
1251 clk_prepare_unlock();
1252
1253 return rate;
1254}
1255EXPORT_SYMBOL_GPL(clk_core_get_rate);
1256
Mike Turquetteb24764902012-03-15 23:11:19 -07001257/**
Ulf Hanssona093bde2012-08-31 14:21:28 +02001258 * clk_get_rate - return the rate of clk
1259 * @clk: the clk whose rate is being returned
1260 *
1261 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1262 * is set, which means a recalc_rate will be issued.
1263 * If clk is NULL then returns 0.
1264 */
1265unsigned long clk_get_rate(struct clk *clk)
1266{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001267 if (!clk)
1268 return 0;
Ulf Hanssona093bde2012-08-31 14:21:28 +02001269
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001270 return clk_core_get_rate(clk->core);
Ulf Hanssona093bde2012-08-31 14:21:28 +02001271}
1272EXPORT_SYMBOL_GPL(clk_get_rate);
1273
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001274static int clk_fetch_parent_index(struct clk_core *clk,
1275 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001276{
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001277 int i;
James Hogan4935b222013-07-29 12:24:59 +01001278
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001279 if (!clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001280 clk->parents = kcalloc(clk->num_parents,
1281 sizeof(struct clk *), GFP_KERNEL);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001282 if (!clk->parents)
1283 return -ENOMEM;
1284 }
James Hogan4935b222013-07-29 12:24:59 +01001285
1286 /*
1287 * find index of new parent clock using cached parent ptrs,
1288 * or if not yet cached, use string name comparison and cache
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001289 * them now to avoid future calls to clk_core_lookup.
James Hogan4935b222013-07-29 12:24:59 +01001290 */
1291 for (i = 0; i < clk->num_parents; i++) {
Tomasz Figada0f0b22013-09-29 02:37:16 +02001292 if (clk->parents[i] == parent)
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001293 return i;
Tomasz Figada0f0b22013-09-29 02:37:16 +02001294
1295 if (clk->parents[i])
1296 continue;
1297
1298 if (!strcmp(clk->parent_names[i], parent->name)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001299 clk->parents[i] = clk_core_lookup(parent->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001300 return i;
James Hogan4935b222013-07-29 12:24:59 +01001301 }
1302 }
1303
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001304 return -EINVAL;
James Hogan4935b222013-07-29 12:24:59 +01001305}
1306
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001307static void clk_reparent(struct clk_core *clk, struct clk_core *new_parent)
James Hogan4935b222013-07-29 12:24:59 +01001308{
1309 hlist_del(&clk->child_node);
1310
James Hogan903efc52013-08-29 12:10:51 +01001311 if (new_parent) {
1312 /* avoid duplicate POST_RATE_CHANGE notifications */
1313 if (new_parent->new_child == clk)
1314 new_parent->new_child = NULL;
1315
James Hogan4935b222013-07-29 12:24:59 +01001316 hlist_add_head(&clk->child_node, &new_parent->children);
James Hogan903efc52013-08-29 12:10:51 +01001317 } else {
James Hogan4935b222013-07-29 12:24:59 +01001318 hlist_add_head(&clk->child_node, &clk_orphan_list);
James Hogan903efc52013-08-29 12:10:51 +01001319 }
James Hogan4935b222013-07-29 12:24:59 +01001320
1321 clk->parent = new_parent;
1322}
1323
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001324static struct clk_core *__clk_set_parent_before(struct clk_core *clk,
1325 struct clk_core *parent)
James Hogan4935b222013-07-29 12:24:59 +01001326{
1327 unsigned long flags;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001328 struct clk_core *old_parent = clk->parent;
James Hogan4935b222013-07-29 12:24:59 +01001329
1330 /*
1331 * Migrate prepare state between parents and prevent race with
1332 * clk_enable().
1333 *
1334 * If the clock is not prepared, then a race with
1335 * clk_enable/disable() is impossible since we already have the
1336 * prepare lock (future calls to clk_enable() need to be preceded by
1337 * a clk_prepare()).
1338 *
1339 * If the clock is prepared, migrate the prepared state to the new
1340 * parent and also protect against a race with clk_enable() by
1341 * forcing the clock and the new parent on. This ensures that all
1342 * future calls to clk_enable() are practically NOPs with respect to
1343 * hardware and software states.
1344 *
1345 * See also: Comment for clk_set_parent() below.
1346 */
1347 if (clk->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001348 clk_core_prepare(parent);
1349 clk_core_enable(parent);
1350 clk_core_enable(clk);
James Hogan4935b222013-07-29 12:24:59 +01001351 }
1352
1353 /* update the clk tree topology */
1354 flags = clk_enable_lock();
1355 clk_reparent(clk, parent);
1356 clk_enable_unlock(flags);
1357
Stephen Boyd3fa22522014-01-15 10:47:22 -08001358 return old_parent;
1359}
1360
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001361static void __clk_set_parent_after(struct clk_core *core,
1362 struct clk_core *parent,
1363 struct clk_core *old_parent)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001364{
1365 /*
1366 * Finish the migration of prepare state and undo the changes done
1367 * for preventing a race with clk_enable().
1368 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001369 if (core->prepare_count) {
1370 clk_core_disable(core);
1371 clk_core_disable(old_parent);
1372 clk_core_unprepare(old_parent);
Stephen Boyd3fa22522014-01-15 10:47:22 -08001373 }
Stephen Boyd3fa22522014-01-15 10:47:22 -08001374}
1375
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001376static int __clk_set_parent(struct clk_core *clk, struct clk_core *parent,
1377 u8 p_index)
Stephen Boyd3fa22522014-01-15 10:47:22 -08001378{
1379 unsigned long flags;
1380 int ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001381 struct clk_core *old_parent;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001382
1383 old_parent = __clk_set_parent_before(clk, parent);
1384
James Hogan4935b222013-07-29 12:24:59 +01001385 /* change clock input source */
1386 if (parent && clk->ops->set_parent)
1387 ret = clk->ops->set_parent(clk->hw, p_index);
1388
1389 if (ret) {
1390 flags = clk_enable_lock();
1391 clk_reparent(clk, old_parent);
1392 clk_enable_unlock(flags);
1393
1394 if (clk->prepare_count) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001395 clk_core_disable(clk);
1396 clk_core_disable(parent);
1397 clk_core_unprepare(parent);
James Hogan4935b222013-07-29 12:24:59 +01001398 }
1399 return ret;
1400 }
1401
Stephen Boyd3fa22522014-01-15 10:47:22 -08001402 __clk_set_parent_after(clk, parent, old_parent);
James Hogan4935b222013-07-29 12:24:59 +01001403
James Hogan4935b222013-07-29 12:24:59 +01001404 return 0;
1405}
1406
Ulf Hanssona093bde2012-08-31 14:21:28 +02001407/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001408 * __clk_speculate_rates
1409 * @clk: first clk in the subtree
1410 * @parent_rate: the "future" rate of clk's parent
1411 *
1412 * Walks the subtree of clks starting with clk, speculating rates as it
1413 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1414 *
1415 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1416 * pre-rate change notifications and returns early if no clks in the
1417 * subtree have subscribed to the notifications. Note that if a clk does not
1418 * implement the .recalc_rate callback then it is assumed that the clock will
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001419 * take on the rate of its parent.
Mike Turquetteb24764902012-03-15 23:11:19 -07001420 *
1421 * Caller must hold prepare_lock.
1422 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001423static int __clk_speculate_rates(struct clk_core *clk,
1424 unsigned long parent_rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001425{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001426 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001427 unsigned long new_rate;
1428 int ret = NOTIFY_DONE;
1429
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001430 new_rate = clk_recalc(clk, parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001431
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001432 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
Mike Turquetteb24764902012-03-15 23:11:19 -07001433 if (clk->notifier_count)
1434 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
1435
Mike Turquette86bcfa22014-02-24 16:08:41 -08001436 if (ret & NOTIFY_STOP_MASK) {
1437 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1438 __func__, clk->name, ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001439 goto out;
Mike Turquette86bcfa22014-02-24 16:08:41 -08001440 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001441
Sasha Levinb67bfe02013-02-27 17:06:00 -08001442 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001443 ret = __clk_speculate_rates(child, new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001444 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001445 break;
1446 }
1447
1448out:
1449 return ret;
1450}
1451
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001452static void clk_calc_subtree(struct clk_core *clk, unsigned long new_rate,
1453 struct clk_core *new_parent, u8 p_index)
Mike Turquetteb24764902012-03-15 23:11:19 -07001454{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001455 struct clk_core *child;
Mike Turquetteb24764902012-03-15 23:11:19 -07001456
1457 clk->new_rate = new_rate;
James Hogan71472c02013-07-29 12:25:00 +01001458 clk->new_parent = new_parent;
1459 clk->new_parent_index = p_index;
1460 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1461 clk->new_child = NULL;
1462 if (new_parent && new_parent != clk->parent)
1463 new_parent->new_child = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001464
Sasha Levinb67bfe02013-02-27 17:06:00 -08001465 hlist_for_each_entry(child, &clk->children, child_node) {
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001466 child->new_rate = clk_recalc(child, new_rate);
James Hogan71472c02013-07-29 12:25:00 +01001467 clk_calc_subtree(child, child->new_rate, NULL, 0);
Mike Turquetteb24764902012-03-15 23:11:19 -07001468 }
1469}
1470
1471/*
1472 * calculate the new rates returning the topmost clock that has to be
1473 * changed.
1474 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001475static struct clk_core *clk_calc_new_rates(struct clk_core *clk,
1476 unsigned long rate)
Mike Turquetteb24764902012-03-15 23:11:19 -07001477{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001478 struct clk_core *top = clk;
1479 struct clk_core *old_parent, *parent;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001480 struct clk_hw *parent_hw;
Shawn Guo81536e02012-04-12 20:50:17 +08001481 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001482 unsigned long new_rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001483 int p_index = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001484
Mike Turquette7452b212012-03-26 14:45:36 -07001485 /* sanity */
1486 if (IS_ERR_OR_NULL(clk))
1487 return NULL;
1488
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001489 /* save parent rate, if it exists */
James Hogan71472c02013-07-29 12:25:00 +01001490 parent = old_parent = clk->parent;
1491 if (parent)
1492 best_parent_rate = parent->rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001493
James Hogan71472c02013-07-29 12:25:00 +01001494 /* find the closest rate and parent clk/rate */
1495 if (clk->ops->determine_rate) {
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001496 parent_hw = parent ? parent->hw : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001497 new_rate = clk->ops->determine_rate(clk->hw, rate,
1498 &best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +01001499 &parent_hw);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001500 parent = parent_hw ? parent_hw->core : NULL;
James Hogan71472c02013-07-29 12:25:00 +01001501 } else if (clk->ops->round_rate) {
1502 new_rate = clk->ops->round_rate(clk->hw, rate,
1503 &best_parent_rate);
1504 } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) {
1505 /* pass-through clock without adjustable parent */
1506 clk->new_rate = clk->rate;
1507 return NULL;
1508 } else {
1509 /* pass-through clock with adjustable parent */
1510 top = clk_calc_new_rates(parent, rate);
1511 new_rate = parent->new_rate;
Mike Turquette63f5c3b2012-05-02 16:23:43 -07001512 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -07001513 }
1514
James Hogan71472c02013-07-29 12:25:00 +01001515 /* some clocks must be gated to change parent */
1516 if (parent != old_parent &&
1517 (clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1518 pr_debug("%s: %s not gated but wants to reparent\n",
1519 __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -07001520 return NULL;
1521 }
1522
James Hogan71472c02013-07-29 12:25:00 +01001523 /* try finding the new parent index */
Stephen Boyd4526e7b2014-12-22 11:26:42 -08001524 if (parent && clk->num_parents > 1) {
James Hogan71472c02013-07-29 12:25:00 +01001525 p_index = clk_fetch_parent_index(clk, parent);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001526 if (p_index < 0) {
James Hogan71472c02013-07-29 12:25:00 +01001527 pr_debug("%s: clk %s can not be parent of clk %s\n",
1528 __func__, parent->name, clk->name);
1529 return NULL;
1530 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001531 }
1532
James Hogan71472c02013-07-29 12:25:00 +01001533 if ((clk->flags & CLK_SET_RATE_PARENT) && parent &&
1534 best_parent_rate != parent->rate)
1535 top = clk_calc_new_rates(parent, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001536
1537out:
James Hogan71472c02013-07-29 12:25:00 +01001538 clk_calc_subtree(clk, new_rate, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001539
1540 return top;
1541}
1542
1543/*
1544 * Notify about rate changes in a subtree. Always walk down the whole tree
1545 * so that in case of an error we can walk down the whole tree again and
1546 * abort the change.
1547 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001548static struct clk_core *clk_propagate_rate_change(struct clk_core *clk,
1549 unsigned long event)
Mike Turquetteb24764902012-03-15 23:11:19 -07001550{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001551 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001552 int ret = NOTIFY_DONE;
1553
1554 if (clk->rate == clk->new_rate)
Sachin Kamat5fda6852013-03-13 15:17:49 +05301555 return NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001556
1557 if (clk->notifier_count) {
1558 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001559 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001560 fail_clk = clk;
1561 }
1562
Sasha Levinb67bfe02013-02-27 17:06:00 -08001563 hlist_for_each_entry(child, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001564 /* Skip children who will be reparented to another clock */
1565 if (child->new_parent && child->new_parent != clk)
1566 continue;
1567 tmp_clk = clk_propagate_rate_change(child, event);
1568 if (tmp_clk)
1569 fail_clk = tmp_clk;
1570 }
1571
1572 /* handle the new child who might not be in clk->children yet */
1573 if (clk->new_child) {
1574 tmp_clk = clk_propagate_rate_change(clk->new_child, event);
1575 if (tmp_clk)
1576 fail_clk = tmp_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001577 }
1578
1579 return fail_clk;
1580}
1581
1582/*
1583 * walk down a subtree and set the new rates notifying the rate
1584 * change on the way
1585 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001586static void clk_change_rate(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001587{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001588 struct clk_core *child;
Tero Kristo067bb172014-08-21 16:47:45 +03001589 struct hlist_node *tmp;
Mike Turquetteb24764902012-03-15 23:11:19 -07001590 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001591 unsigned long best_parent_rate = 0;
Stephen Boyd3fa22522014-01-15 10:47:22 -08001592 bool skip_set_rate = false;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001593 struct clk_core *old_parent;
Mike Turquetteb24764902012-03-15 23:11:19 -07001594
1595 old_rate = clk->rate;
1596
Stephen Boyd3fa22522014-01-15 10:47:22 -08001597 if (clk->new_parent)
1598 best_parent_rate = clk->new_parent->rate;
1599 else if (clk->parent)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001600 best_parent_rate = clk->parent->rate;
1601
Stephen Boyd3fa22522014-01-15 10:47:22 -08001602 if (clk->new_parent && clk->new_parent != clk->parent) {
1603 old_parent = __clk_set_parent_before(clk, clk->new_parent);
1604
1605 if (clk->ops->set_rate_and_parent) {
1606 skip_set_rate = true;
1607 clk->ops->set_rate_and_parent(clk->hw, clk->new_rate,
1608 best_parent_rate,
1609 clk->new_parent_index);
1610 } else if (clk->ops->set_parent) {
1611 clk->ops->set_parent(clk->hw, clk->new_parent_index);
1612 }
1613
1614 __clk_set_parent_after(clk, clk->new_parent, old_parent);
1615 }
1616
1617 if (!skip_set_rate && clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001618 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001619
Stephen Boyd8f2c2db2014-03-26 16:06:36 -07001620 clk->rate = clk_recalc(clk, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001621
1622 if (clk->notifier_count && old_rate != clk->rate)
1623 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1624
Tero Kristo067bb172014-08-21 16:47:45 +03001625 /*
1626 * Use safe iteration, as change_rate can actually swap parents
1627 * for certain clock types.
1628 */
1629 hlist_for_each_entry_safe(child, tmp, &clk->children, child_node) {
James Hogan71472c02013-07-29 12:25:00 +01001630 /* Skip children who will be reparented to another clock */
1631 if (child->new_parent && child->new_parent != clk)
1632 continue;
Mike Turquetteb24764902012-03-15 23:11:19 -07001633 clk_change_rate(child);
James Hogan71472c02013-07-29 12:25:00 +01001634 }
1635
1636 /* handle the new child who might not be in clk->children yet */
1637 if (clk->new_child)
1638 clk_change_rate(clk->new_child);
Mike Turquetteb24764902012-03-15 23:11:19 -07001639}
1640
1641/**
1642 * clk_set_rate - specify a new rate for clk
1643 * @clk: the clk whose rate is being changed
1644 * @rate: the new rate for clk
1645 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001646 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001647 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001648 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1649 * propagate up to clk's parent; whether or not this happens depends on the
1650 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1651 * after calling .round_rate then upstream parent propagation is ignored. If
1652 * *parent_rate comes back with a new rate for clk's parent then we propagate
Peter Meerwald24ee1a02013-06-29 15:14:19 +02001653 * up to clk's parent and set its rate. Upward propagation will continue
Mike Turquette5654dc92012-03-26 11:51:34 -07001654 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1655 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001656 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001657 * Rate changes are accomplished via tree traversal that also recalculates the
1658 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001659 *
1660 * Returns 0 on success, -EERROR otherwise.
1661 */
1662int clk_set_rate(struct clk *clk, unsigned long rate)
1663{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001664 struct clk_core *top, *fail_clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07001665 int ret = 0;
1666
Mike Turquette89ac8d72013-08-21 23:58:09 -07001667 if (!clk)
1668 return 0;
1669
Mike Turquetteb24764902012-03-15 23:11:19 -07001670 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001671 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001672
1673 /* bail early if nothing to do */
Peter De Schrijver34e452a2013-06-05 18:06:36 +03001674 if (rate == clk_get_rate(clk))
Mike Turquetteb24764902012-03-15 23:11:19 -07001675 goto out;
1676
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001677 if ((clk->core->flags & CLK_SET_RATE_GATE) &&
1678 clk->core->prepare_count) {
Viresh Kumar0e1c0302012-04-11 16:03:42 +05301679 ret = -EBUSY;
1680 goto out;
1681 }
1682
Mike Turquetteb24764902012-03-15 23:11:19 -07001683 /* calculate new rates and get the topmost changed clock */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001684 top = clk_calc_new_rates(clk->core, rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001685 if (!top) {
1686 ret = -EINVAL;
1687 goto out;
1688 }
1689
1690 /* notify that we are about to change rates */
1691 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1692 if (fail_clk) {
Sascha Hauerf7363862014-01-16 16:12:55 +01001693 pr_debug("%s: failed to set %s rate\n", __func__,
Mike Turquetteb24764902012-03-15 23:11:19 -07001694 fail_clk->name);
1695 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1696 ret = -EBUSY;
1697 goto out;
1698 }
1699
1700 /* change the rates */
1701 clk_change_rate(top);
1702
Mike Turquetteb24764902012-03-15 23:11:19 -07001703out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001704 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001705
1706 return ret;
1707}
1708EXPORT_SYMBOL_GPL(clk_set_rate);
1709
1710/**
1711 * clk_get_parent - return the parent of a clk
1712 * @clk: the clk whose parent gets returned
1713 *
1714 * Simply returns clk->parent. Returns NULL if clk is NULL.
1715 */
1716struct clk *clk_get_parent(struct clk *clk)
1717{
1718 struct clk *parent;
1719
Mike Turquetteeab89f62013-03-28 13:59:01 -07001720 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001721 parent = __clk_get_parent(clk);
Mike Turquetteeab89f62013-03-28 13:59:01 -07001722 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001723
1724 return parent;
1725}
1726EXPORT_SYMBOL_GPL(clk_get_parent);
1727
1728/*
1729 * .get_parent is mandatory for clocks with multiple possible parents. It is
1730 * optional for single-parent clocks. Always call .get_parent if it is
1731 * available and WARN if it is missing for multi-parent clocks.
1732 *
1733 * For single-parent clocks without .get_parent, first check to see if the
1734 * .parents array exists, and if so use it to avoid an expensive tree
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001735 * traversal. If .parents does not exist then walk the tree.
Mike Turquetteb24764902012-03-15 23:11:19 -07001736 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001737static struct clk_core *__clk_init_parent(struct clk_core *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001738{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001739 struct clk_core *ret = NULL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001740 u8 index;
1741
1742 /* handle the trivial cases */
1743
1744 if (!clk->num_parents)
1745 goto out;
1746
1747 if (clk->num_parents == 1) {
1748 if (IS_ERR_OR_NULL(clk->parent))
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001749 clk->parent = clk_core_lookup(clk->parent_names[0]);
Mike Turquetteb24764902012-03-15 23:11:19 -07001750 ret = clk->parent;
1751 goto out;
1752 }
1753
1754 if (!clk->ops->get_parent) {
1755 WARN(!clk->ops->get_parent,
1756 "%s: multi-parent clocks must implement .get_parent\n",
1757 __func__);
1758 goto out;
1759 };
1760
1761 /*
1762 * Do our best to cache parent clocks in clk->parents. This prevents
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001763 * unnecessary and expensive lookups. We don't set clk->parent here;
1764 * that is done by the calling function.
Mike Turquetteb24764902012-03-15 23:11:19 -07001765 */
1766
1767 index = clk->ops->get_parent(clk->hw);
1768
1769 if (!clk->parents)
1770 clk->parents =
Tomasz Figa96a7ed92013-09-29 02:37:15 +02001771 kcalloc(clk->num_parents, sizeof(struct clk *),
Mike Turquetteb24764902012-03-15 23:11:19 -07001772 GFP_KERNEL);
1773
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001774 ret = clk_core_get_parent_by_index(clk, index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001775
1776out:
1777 return ret;
1778}
1779
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001780static void clk_core_reparent(struct clk_core *clk,
1781 struct clk_core *new_parent)
Ulf Hanssonb33d2122013-04-02 23:09:37 +02001782{
1783 clk_reparent(clk, new_parent);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001784 __clk_recalc_accuracies(clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07001785 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1786}
1787
Mike Turquetteb24764902012-03-15 23:11:19 -07001788/**
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001789 * clk_has_parent - check if a clock is a possible parent for another
1790 * @clk: clock source
1791 * @parent: parent clock source
1792 *
1793 * This function can be used in drivers that need to check that a clock can be
1794 * the parent of another without actually changing the parent.
1795 *
1796 * Returns true if @parent is a possible parent for @clk, false otherwise.
1797 */
1798bool clk_has_parent(struct clk *clk, struct clk *parent)
1799{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001800 struct clk_core *core, *parent_core;
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001801 unsigned int i;
1802
1803 /* NULL clocks should be nops, so return success if either is NULL. */
1804 if (!clk || !parent)
1805 return true;
1806
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001807 core = clk->core;
1808 parent_core = parent->core;
1809
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001810 /* Optimize for the case where the parent is already the parent. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001811 if (core->parent == parent_core)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001812 return true;
1813
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001814 for (i = 0; i < core->num_parents; i++)
1815 if (strcmp(core->parent_names[i], parent_core->name) == 0)
Thierry Reding4e88f3d2015-01-21 17:13:00 +01001816 return true;
1817
1818 return false;
1819}
1820EXPORT_SYMBOL_GPL(clk_has_parent);
1821
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001822static int clk_core_set_parent(struct clk_core *clk, struct clk_core *parent)
Mike Turquetteb24764902012-03-15 23:11:19 -07001823{
1824 int ret = 0;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001825 int p_index = 0;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001826 unsigned long p_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001827
Mike Turquette89ac8d72013-08-21 23:58:09 -07001828 if (!clk)
1829 return 0;
1830
Ulf Hansson031dcc92013-04-02 23:09:38 +02001831 /* verify ops for for multi-parent clks */
1832 if ((clk->num_parents > 1) && (!clk->ops->set_parent))
Mike Turquetteb24764902012-03-15 23:11:19 -07001833 return -ENOSYS;
1834
1835 /* prevent racing with updates to the clock topology */
Mike Turquetteeab89f62013-03-28 13:59:01 -07001836 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001837
1838 if (clk->parent == parent)
1839 goto out;
1840
Ulf Hansson031dcc92013-04-02 23:09:38 +02001841 /* check that we are allowed to re-parent if the clock is in use */
1842 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1843 ret = -EBUSY;
1844 goto out;
1845 }
1846
1847 /* try finding the new parent index */
1848 if (parent) {
1849 p_index = clk_fetch_parent_index(clk, parent);
1850 p_rate = parent->rate;
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001851 if (p_index < 0) {
Ulf Hansson031dcc92013-04-02 23:09:38 +02001852 pr_debug("%s: clk %s can not be parent of clk %s\n",
1853 __func__, parent->name, clk->name);
Tomasz Figaf1c8b2e2013-09-29 02:37:14 +02001854 ret = p_index;
Ulf Hansson031dcc92013-04-02 23:09:38 +02001855 goto out;
1856 }
1857 }
1858
Mike Turquetteb24764902012-03-15 23:11:19 -07001859 /* propagate PRE_RATE_CHANGE notifications */
Soren Brinkmannf3aab5d2013-04-16 10:06:50 -07001860 ret = __clk_speculate_rates(clk, p_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001861
1862 /* abort if a driver objects */
Soren Brinkmannfb72a052013-04-03 12:17:12 -07001863 if (ret & NOTIFY_STOP_MASK)
Mike Turquetteb24764902012-03-15 23:11:19 -07001864 goto out;
1865
Ulf Hansson031dcc92013-04-02 23:09:38 +02001866 /* do the re-parent */
1867 ret = __clk_set_parent(clk, parent, p_index);
Mike Turquetteb24764902012-03-15 23:11:19 -07001868
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001869 /* propagate rate an accuracy recalculation accordingly */
1870 if (ret) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001871 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001872 } else {
Ulf Hanssona68de8e2013-04-02 23:09:39 +02001873 __clk_recalc_rates(clk, POST_RATE_CHANGE);
Boris BREZILLON5279fc42013-12-21 10:34:47 +01001874 __clk_recalc_accuracies(clk);
1875 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001876
1877out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07001878 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07001879
1880 return ret;
1881}
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001882
1883/**
1884 * clk_set_parent - switch the parent of a mux clk
1885 * @clk: the mux clk whose input we are switching
1886 * @parent: the new input to clk
1887 *
1888 * Re-parent clk to use parent as its new input source. If clk is in
1889 * prepared state, the clk will get enabled for the duration of this call. If
1890 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1891 * that, the reparenting is glitchy in hardware, etc), use the
1892 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1893 *
1894 * After successfully changing clk's parent clk_set_parent will update the
1895 * clk topology, sysfs topology and propagate rate recalculation via
1896 * __clk_recalc_rates.
1897 *
1898 * Returns 0 on success, -EERROR otherwise.
1899 */
1900int clk_set_parent(struct clk *clk, struct clk *parent)
1901{
1902 if (!clk)
1903 return 0;
1904
1905 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1906}
Mike Turquetteb24764902012-03-15 23:11:19 -07001907EXPORT_SYMBOL_GPL(clk_set_parent);
1908
1909/**
Mike Turquettee59c5372014-02-18 21:21:25 -08001910 * clk_set_phase - adjust the phase shift of a clock signal
1911 * @clk: clock signal source
1912 * @degrees: number of degrees the signal is shifted
1913 *
1914 * Shifts the phase of a clock signal by the specified
1915 * degrees. Returns 0 on success, -EERROR otherwise.
1916 *
1917 * This function makes no distinction about the input or reference
1918 * signal that we adjust the clock signal phase against. For example
1919 * phase locked-loop clock signal generators we may shift phase with
1920 * respect to feedback clock signal input, but for other cases the
1921 * clock phase may be shifted with respect to some other, unspecified
1922 * signal.
1923 *
1924 * Additionally the concept of phase shift does not propagate through
1925 * the clock tree hierarchy, which sets it apart from clock rates and
1926 * clock accuracy. A parent clock phase attribute does not have an
1927 * impact on the phase attribute of a child clock.
1928 */
1929int clk_set_phase(struct clk *clk, int degrees)
1930{
1931 int ret = 0;
1932
1933 if (!clk)
1934 goto out;
1935
1936 /* sanity check degrees */
1937 degrees %= 360;
1938 if (degrees < 0)
1939 degrees += 360;
1940
1941 clk_prepare_lock();
1942
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001943 if (!clk->core->ops->set_phase)
Mike Turquettee59c5372014-02-18 21:21:25 -08001944 goto out_unlock;
1945
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001946 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
Mike Turquettee59c5372014-02-18 21:21:25 -08001947
1948 if (!ret)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001949 clk->core->phase = degrees;
Mike Turquettee59c5372014-02-18 21:21:25 -08001950
1951out_unlock:
1952 clk_prepare_unlock();
1953
1954out:
1955 return ret;
1956}
Maxime Ripard9767b042015-01-20 22:23:43 +01001957EXPORT_SYMBOL_GPL(clk_set_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001958
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001959static int clk_core_get_phase(struct clk_core *clk)
Mike Turquettee59c5372014-02-18 21:21:25 -08001960{
1961 int ret = 0;
1962
1963 if (!clk)
1964 goto out;
1965
1966 clk_prepare_lock();
1967 ret = clk->phase;
1968 clk_prepare_unlock();
1969
1970out:
1971 return ret;
1972}
Maxime Ripard9767b042015-01-20 22:23:43 +01001973EXPORT_SYMBOL_GPL(clk_get_phase);
Mike Turquettee59c5372014-02-18 21:21:25 -08001974
1975/**
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001976 * clk_get_phase - return the phase shift of a clock signal
1977 * @clk: clock signal source
1978 *
1979 * Returns the phase shift of a clock node in degrees, otherwise returns
1980 * -EERROR.
1981 */
1982int clk_get_phase(struct clk *clk)
1983{
1984 if (!clk)
1985 return 0;
1986
1987 return clk_core_get_phase(clk->core);
1988}
1989
1990/**
Mike Turquetteb24764902012-03-15 23:11:19 -07001991 * __clk_init - initialize the data structures in a struct clk
1992 * @dev: device initializing this clk, placeholder for now
1993 * @clk: clk being initialized
1994 *
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001995 * Initializes the lists in struct clk_core, queries the hardware for the
Mike Turquetteb24764902012-03-15 23:11:19 -07001996 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07001997 */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01001998int __clk_init(struct device *dev, struct clk *clk_user)
Mike Turquetteb24764902012-03-15 23:11:19 -07001999{
Mike Turquetted1302a32012-03-29 14:30:40 -07002000 int i, ret = 0;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002001 struct clk_core *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002002 struct hlist_node *tmp2;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002003 struct clk_core *clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07002004
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002005 if (!clk_user)
Mike Turquetted1302a32012-03-29 14:30:40 -07002006 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07002007
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002008 clk = clk_user->core;
2009
Mike Turquetteeab89f62013-03-28 13:59:01 -07002010 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002011
2012 /* check to see if a clock with this name is already registered */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002013 if (clk_core_lookup(clk->name)) {
Mike Turquetted1302a32012-03-29 14:30:40 -07002014 pr_debug("%s: clk %s already initialized\n",
2015 __func__, clk->name);
2016 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07002017 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07002018 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002019
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002020 /* check that clk_ops are sane. See Documentation/clk.txt */
2021 if (clk->ops->set_rate &&
James Hogan71472c02013-07-29 12:25:00 +01002022 !((clk->ops->round_rate || clk->ops->determine_rate) &&
2023 clk->ops->recalc_rate)) {
2024 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002025 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002026 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002027 goto out;
2028 }
2029
2030 if (clk->ops->set_parent && !clk->ops->get_parent) {
2031 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
2032 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07002033 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07002034 goto out;
2035 }
2036
Stephen Boyd3fa22522014-01-15 10:47:22 -08002037 if (clk->ops->set_rate_and_parent &&
2038 !(clk->ops->set_parent && clk->ops->set_rate)) {
2039 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
2040 __func__, clk->name);
2041 ret = -EINVAL;
2042 goto out;
2043 }
2044
Mike Turquetteb24764902012-03-15 23:11:19 -07002045 /* throw a WARN if any entries in parent_names are NULL */
2046 for (i = 0; i < clk->num_parents; i++)
2047 WARN(!clk->parent_names[i],
2048 "%s: invalid NULL in %s's .parent_names\n",
2049 __func__, clk->name);
2050
2051 /*
2052 * Allocate an array of struct clk *'s to avoid unnecessary string
2053 * look-ups of clk's possible parents. This can fail for clocks passed
2054 * in to clk_init during early boot; thus any access to clk->parents[]
2055 * must always check for a NULL pointer and try to populate it if
2056 * necessary.
2057 *
2058 * If clk->parents is not NULL we skip this entire block. This allows
2059 * for clock drivers to statically initialize clk->parents.
2060 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05302061 if (clk->num_parents > 1 && !clk->parents) {
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002062 clk->parents = kcalloc(clk->num_parents, sizeof(struct clk *),
2063 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002064 /*
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002065 * clk_core_lookup returns NULL for parents that have not been
Mike Turquetteb24764902012-03-15 23:11:19 -07002066 * clk_init'd; thus any access to clk->parents[] must check
2067 * for a NULL pointer. We can always perform lazy lookups for
2068 * missing parents later on.
2069 */
2070 if (clk->parents)
2071 for (i = 0; i < clk->num_parents; i++)
2072 clk->parents[i] =
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002073 clk_core_lookup(clk->parent_names[i]);
Mike Turquetteb24764902012-03-15 23:11:19 -07002074 }
2075
2076 clk->parent = __clk_init_parent(clk);
2077
2078 /*
2079 * Populate clk->parent if parent has already been __clk_init'd. If
2080 * parent has not yet been __clk_init'd then place clk in the orphan
2081 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2082 * clk list.
2083 *
2084 * Every time a new clk is clk_init'd then we walk the list of orphan
2085 * clocks and re-parent any that are children of the clock currently
2086 * being clk_init'd.
2087 */
2088 if (clk->parent)
2089 hlist_add_head(&clk->child_node,
2090 &clk->parent->children);
2091 else if (clk->flags & CLK_IS_ROOT)
2092 hlist_add_head(&clk->child_node, &clk_root_list);
2093 else
2094 hlist_add_head(&clk->child_node, &clk_orphan_list);
2095
2096 /*
Boris BREZILLON5279fc42013-12-21 10:34:47 +01002097 * Set clk's accuracy. The preferred method is to use
2098 * .recalc_accuracy. For simple clocks and lazy developers the default
2099 * fallback is to use the parent's accuracy. If a clock doesn't have a
2100 * parent (or is orphaned) then accuracy is set to zero (perfect
2101 * clock).
2102 */
2103 if (clk->ops->recalc_accuracy)
2104 clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
2105 __clk_get_accuracy(clk->parent));
2106 else if (clk->parent)
2107 clk->accuracy = clk->parent->accuracy;
2108 else
2109 clk->accuracy = 0;
2110
2111 /*
Maxime Ripard9824cf72014-07-14 13:53:27 +02002112 * Set clk's phase.
2113 * Since a phase is by definition relative to its parent, just
2114 * query the current clock phase, or just assume it's in phase.
2115 */
2116 if (clk->ops->get_phase)
2117 clk->phase = clk->ops->get_phase(clk->hw);
2118 else
2119 clk->phase = 0;
2120
2121 /*
Mike Turquetteb24764902012-03-15 23:11:19 -07002122 * Set clk's rate. The preferred method is to use .recalc_rate. For
2123 * simple clocks and lazy developers the default fallback is to use the
2124 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2125 * then rate is set to zero.
2126 */
2127 if (clk->ops->recalc_rate)
2128 clk->rate = clk->ops->recalc_rate(clk->hw,
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002129 clk_core_get_rate_nolock(clk->parent));
Mike Turquetteb24764902012-03-15 23:11:19 -07002130 else if (clk->parent)
2131 clk->rate = clk->parent->rate;
2132 else
2133 clk->rate = 0;
2134
2135 /*
2136 * walk the list of orphan clocks and reparent any that are children of
2137 * this clock
2138 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002139 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Alex Elder12d298862013-09-05 08:33:24 -05002140 if (orphan->num_parents && orphan->ops->get_parent) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002141 i = orphan->ops->get_parent(orphan->hw);
2142 if (!strcmp(clk->name, orphan->parent_names[i]))
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002143 clk_core_reparent(orphan, clk);
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002144 continue;
2145 }
2146
Mike Turquetteb24764902012-03-15 23:11:19 -07002147 for (i = 0; i < orphan->num_parents; i++)
2148 if (!strcmp(clk->name, orphan->parent_names[i])) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002149 clk_core_reparent(orphan, clk);
Mike Turquetteb24764902012-03-15 23:11:19 -07002150 break;
2151 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01002152 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002153
2154 /*
2155 * optional platform-specific magic
2156 *
2157 * The .init callback is not used by any of the basic clock types, but
2158 * exists for weird hardware that must perform initialization magic.
2159 * Please consider other ways of solving initialization problems before
Peter Meerwald24ee1a02013-06-29 15:14:19 +02002160 * using this callback, as its use is discouraged.
Mike Turquetteb24764902012-03-15 23:11:19 -07002161 */
2162 if (clk->ops->init)
2163 clk->ops->init(clk->hw);
2164
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002165 kref_init(&clk->ref);
Mike Turquetteb24764902012-03-15 23:11:19 -07002166out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002167 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002168
Stephen Boyd89f7e9d2014-12-12 15:04:16 -08002169 if (!ret)
2170 clk_debug_register(clk);
2171
Mike Turquetted1302a32012-03-29 14:30:40 -07002172 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07002173}
2174
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002175struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2176 const char *con_id)
2177{
2178 struct clk *clk;
2179
2180 /* This is to allow this function to be chained to others */
2181 if (!hw || IS_ERR(hw))
2182 return (struct clk *) hw;
2183
2184 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2185 if (!clk)
2186 return ERR_PTR(-ENOMEM);
2187
2188 clk->core = hw->core;
2189 clk->dev_id = dev_id;
2190 clk->con_id = con_id;
2191
2192 return clk;
2193}
2194
Mike Turquetteb24764902012-03-15 23:11:19 -07002195/**
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002196 * clk_register - allocate a new clock, register it and return an opaque cookie
2197 * @dev: device that is registering this clock
2198 * @hw: link to hardware-specific clock data
2199 *
2200 * clk_register is the primary interface for populating the clock tree with new
2201 * clock nodes. It returns a pointer to the newly allocated struct clk which
2202 * cannot be dereferenced by driver code but may be used in conjuction with the
2203 * rest of the clock API. In the event of an error clk_register will return an
2204 * error code; drivers must test for an error code after calling clk_register.
2205 */
2206struct clk *clk_register(struct device *dev, struct clk_hw *hw)
Mike Turquetteb24764902012-03-15 23:11:19 -07002207{
Mike Turquetted1302a32012-03-29 14:30:40 -07002208 int i, ret;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002209 struct clk_core *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002210
2211 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2212 if (!clk) {
2213 pr_err("%s: could not allocate clk\n", __func__);
2214 ret = -ENOMEM;
2215 goto fail_out;
2216 }
Mike Turquetteb24764902012-03-15 23:11:19 -07002217
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002218 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
2219 if (!clk->name) {
2220 pr_err("%s: could not allocate clk->name\n", __func__);
2221 ret = -ENOMEM;
2222 goto fail_name;
2223 }
2224 clk->ops = hw->init->ops;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002225 if (dev && dev->driver)
2226 clk->owner = dev->driver->owner;
Mike Turquetteb24764902012-03-15 23:11:19 -07002227 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002228 clk->flags = hw->init->flags;
2229 clk->num_parents = hw->init->num_parents;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002230 hw->core = clk;
Mike Turquetteb24764902012-03-15 23:11:19 -07002231
Mike Turquetted1302a32012-03-29 14:30:40 -07002232 /* allocate local copy in case parent_names is __initdata */
Tomasz Figa96a7ed92013-09-29 02:37:15 +02002233 clk->parent_names = kcalloc(clk->num_parents, sizeof(char *),
2234 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07002235
Mike Turquetted1302a32012-03-29 14:30:40 -07002236 if (!clk->parent_names) {
2237 pr_err("%s: could not allocate clk->parent_names\n", __func__);
2238 ret = -ENOMEM;
2239 goto fail_parent_names;
2240 }
2241
2242
2243 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002244 for (i = 0; i < clk->num_parents; i++) {
2245 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
2246 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07002247 if (!clk->parent_names[i]) {
2248 pr_err("%s: could not copy parent_names\n", __func__);
2249 ret = -ENOMEM;
2250 goto fail_parent_names_copy;
2251 }
2252 }
2253
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002254 hw->clk = __clk_create_clk(hw, NULL, NULL);
2255 if (IS_ERR(hw->clk)) {
2256 pr_err("%s: could not allocate per-user clk\n", __func__);
2257 ret = PTR_ERR(hw->clk);
2258 goto fail_parent_names_copy;
2259 }
Mike Turquetted1302a32012-03-29 14:30:40 -07002260
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002261 ret = __clk_init(dev, hw->clk);
2262 if (!ret)
2263 return hw->clk;
2264
2265 kfree(hw->clk);
2266 hw->clk = NULL;
Mike Turquetted1302a32012-03-29 14:30:40 -07002267fail_parent_names_copy:
2268 while (--i >= 0)
2269 kfree(clk->parent_names[i]);
2270 kfree(clk->parent_names);
2271fail_parent_names:
Saravana Kannan0197b3e2012-04-25 22:58:56 -07002272 kfree(clk->name);
2273fail_name:
Mike Turquetted1302a32012-03-29 14:30:40 -07002274 kfree(clk);
2275fail_out:
2276 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07002277}
2278EXPORT_SYMBOL_GPL(clk_register);
2279
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002280/*
2281 * Free memory allocated for a clock.
2282 * Caller must hold prepare_lock.
2283 */
2284static void __clk_release(struct kref *ref)
2285{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002286 struct clk_core *clk = container_of(ref, struct clk_core, ref);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002287 int i = clk->num_parents;
2288
2289 kfree(clk->parents);
2290 while (--i >= 0)
2291 kfree(clk->parent_names[i]);
2292
2293 kfree(clk->parent_names);
2294 kfree(clk->name);
2295 kfree(clk);
2296}
2297
2298/*
2299 * Empty clk_ops for unregistered clocks. These are used temporarily
2300 * after clk_unregister() was called on a clock and until last clock
2301 * consumer calls clk_put() and the struct clk object is freed.
2302 */
2303static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2304{
2305 return -ENXIO;
2306}
2307
2308static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2309{
2310 WARN_ON_ONCE(1);
2311}
2312
2313static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2314 unsigned long parent_rate)
2315{
2316 return -ENXIO;
2317}
2318
2319static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2320{
2321 return -ENXIO;
2322}
2323
2324static const struct clk_ops clk_nodrv_ops = {
2325 .enable = clk_nodrv_prepare_enable,
2326 .disable = clk_nodrv_disable_unprepare,
2327 .prepare = clk_nodrv_prepare_enable,
2328 .unprepare = clk_nodrv_disable_unprepare,
2329 .set_rate = clk_nodrv_set_rate,
2330 .set_parent = clk_nodrv_set_parent,
2331};
2332
Mark Brown1df5c932012-04-18 09:07:12 +01002333/**
2334 * clk_unregister - unregister a currently registered clock
2335 * @clk: clock to unregister
Mark Brown1df5c932012-04-18 09:07:12 +01002336 */
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002337void clk_unregister(struct clk *clk)
2338{
2339 unsigned long flags;
2340
Stephen Boyd6314b672014-09-04 23:37:49 -07002341 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2342 return;
2343
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002344 clk_debug_unregister(clk->core);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002345
2346 clk_prepare_lock();
2347
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002348 if (clk->core->ops == &clk_nodrv_ops) {
2349 pr_err("%s: unregistered clock: %s\n", __func__,
2350 clk->core->name);
Stephen Boyd6314b672014-09-04 23:37:49 -07002351 return;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002352 }
2353 /*
2354 * Assign empty clock ops for consumers that might still hold
2355 * a reference to this clock.
2356 */
2357 flags = clk_enable_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002358 clk->core->ops = &clk_nodrv_ops;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002359 clk_enable_unlock(flags);
2360
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002361 if (!hlist_empty(&clk->core->children)) {
2362 struct clk_core *child;
Stephen Boyd874f2242014-04-18 16:29:43 -07002363 struct hlist_node *t;
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002364
2365 /* Reparent all children to the orphan list. */
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002366 hlist_for_each_entry_safe(child, t, &clk->core->children,
2367 child_node)
2368 clk_core_set_parent(child, NULL);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002369 }
2370
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002371 hlist_del_init(&clk->core->child_node);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002372
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002373 if (clk->core->prepare_count)
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002374 pr_warn("%s: unregistering prepared clock: %s\n",
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002375 __func__, clk->core->name);
2376 kref_put(&clk->core->ref, __clk_release);
Stephen Boyd6314b672014-09-04 23:37:49 -07002377
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002378 clk_prepare_unlock();
2379}
Mark Brown1df5c932012-04-18 09:07:12 +01002380EXPORT_SYMBOL_GPL(clk_unregister);
2381
Stephen Boyd46c87732012-09-24 13:38:04 -07002382static void devm_clk_release(struct device *dev, void *res)
2383{
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002384 clk_unregister(*(struct clk **)res);
Stephen Boyd46c87732012-09-24 13:38:04 -07002385}
2386
2387/**
2388 * devm_clk_register - resource managed clk_register()
2389 * @dev: device that is registering this clock
2390 * @hw: link to hardware-specific clock data
2391 *
2392 * Managed clk_register(). Clocks returned from this function are
2393 * automatically clk_unregister()ed on driver detach. See clk_register() for
2394 * more information.
2395 */
2396struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2397{
2398 struct clk *clk;
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002399 struct clk **clkp;
Stephen Boyd46c87732012-09-24 13:38:04 -07002400
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002401 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2402 if (!clkp)
Stephen Boyd46c87732012-09-24 13:38:04 -07002403 return ERR_PTR(-ENOMEM);
2404
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002405 clk = clk_register(dev, hw);
2406 if (!IS_ERR(clk)) {
2407 *clkp = clk;
2408 devres_add(dev, clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002409 } else {
Stephen Boyd293ba3b2014-04-18 16:29:42 -07002410 devres_free(clkp);
Stephen Boyd46c87732012-09-24 13:38:04 -07002411 }
2412
2413 return clk;
2414}
2415EXPORT_SYMBOL_GPL(devm_clk_register);
2416
2417static int devm_clk_match(struct device *dev, void *res, void *data)
2418{
2419 struct clk *c = res;
2420 if (WARN_ON(!c))
2421 return 0;
2422 return c == data;
2423}
2424
2425/**
2426 * devm_clk_unregister - resource managed clk_unregister()
2427 * @clk: clock to unregister
2428 *
2429 * Deallocate a clock allocated with devm_clk_register(). Normally
2430 * this function will not need to be called and the resource management
2431 * code will ensure that the resource is freed.
2432 */
2433void devm_clk_unregister(struct device *dev, struct clk *clk)
2434{
2435 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2436}
2437EXPORT_SYMBOL_GPL(devm_clk_unregister);
2438
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002439/*
2440 * clkdev helpers
2441 */
2442int __clk_get(struct clk *clk)
2443{
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002444 struct clk_core *core = !clk ? NULL : clk->core;
2445
2446 if (core) {
2447 if (!try_module_get(core->owner))
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002448 return 0;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002449
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002450 kref_get(&core->ref);
Sylwester Nawrocki00efcb12014-01-07 13:03:43 +01002451 }
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002452 return 1;
2453}
2454
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002455static void clk_core_put(struct clk_core *core)
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002456{
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002457 struct module *owner;
2458
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002459 owner = core->owner;
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002460
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002461 clk_prepare_lock();
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002462 kref_put(&core->ref, __clk_release);
Sylwester Nawrockifcb0ee62013-08-24 15:00:10 +02002463 clk_prepare_unlock();
2464
Tomeu Vizoso10cdfe52014-12-02 08:54:19 +01002465 module_put(owner);
Sylwester Nawrockiac2df522013-08-24 20:10:41 +02002466}
2467
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002468void __clk_put(struct clk *clk)
2469{
2470 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2471 return;
2472
2473 clk_core_put(clk->core);
2474 kfree(clk);
2475}
2476
Mike Turquetteb24764902012-03-15 23:11:19 -07002477/*** clk rate change notifiers ***/
2478
2479/**
2480 * clk_notifier_register - add a clk rate change notifier
2481 * @clk: struct clk * to watch
2482 * @nb: struct notifier_block * with callback info
2483 *
2484 * Request notification when clk's rate changes. This uses an SRCU
2485 * notifier because we want it to block and notifier unregistrations are
2486 * uncommon. The callbacks associated with the notifier must not
2487 * re-enter into the clk framework by calling any top-level clk APIs;
2488 * this will cause a nested prepare_lock mutex.
2489 *
Soren Brinkmann5324fda2014-01-22 11:48:37 -08002490 * In all notification cases cases (pre, post and abort rate change) the
2491 * original clock rate is passed to the callback via struct
2492 * clk_notifier_data.old_rate and the new frequency is passed via struct
Mike Turquetteb24764902012-03-15 23:11:19 -07002493 * clk_notifier_data.new_rate.
2494 *
Mike Turquetteb24764902012-03-15 23:11:19 -07002495 * clk_notifier_register() must be called from non-atomic context.
2496 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2497 * allocation failure; otherwise, passes along the return value of
2498 * srcu_notifier_chain_register().
2499 */
2500int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2501{
2502 struct clk_notifier *cn;
2503 int ret = -ENOMEM;
2504
2505 if (!clk || !nb)
2506 return -EINVAL;
2507
Mike Turquetteeab89f62013-03-28 13:59:01 -07002508 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002509
2510 /* search the list of notifiers for this clk */
2511 list_for_each_entry(cn, &clk_notifier_list, node)
2512 if (cn->clk == clk)
2513 break;
2514
2515 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2516 if (cn->clk != clk) {
2517 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2518 if (!cn)
2519 goto out;
2520
2521 cn->clk = clk;
2522 srcu_init_notifier_head(&cn->notifier_head);
2523
2524 list_add(&cn->node, &clk_notifier_list);
2525 }
2526
2527 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2528
Tomeu Vizoso035a61c2015-01-23 12:03:30 +01002529 clk->core->notifier_count++;
Mike Turquetteb24764902012-03-15 23:11:19 -07002530
2531out:
Mike Turquetteeab89f62013-03-28 13:59:01 -07002532 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002533
2534 return ret;
2535}
2536EXPORT_SYMBOL_GPL(clk_notifier_register);
2537
2538/**
2539 * clk_notifier_unregister - remove a clk rate change notifier
2540 * @clk: struct clk *
2541 * @nb: struct notifier_block * with callback info
2542 *
2543 * Request no further notification for changes to 'clk' and frees memory
2544 * allocated in clk_notifier_register.
2545 *
2546 * Returns -EINVAL if called with null arguments; otherwise, passes
2547 * along the return value of srcu_notifier_chain_unregister().
2548 */
2549int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2550{
2551 struct clk_notifier *cn = NULL;
2552 int ret = -EINVAL;
2553
2554 if (!clk || !nb)
2555 return -EINVAL;
2556
Mike Turquetteeab89f62013-03-28 13:59:01 -07002557 clk_prepare_lock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002558
2559 list_for_each_entry(cn, &clk_notifier_list, node)
2560 if (cn->clk == clk)
2561 break;
2562
2563 if (cn->clk == clk) {
2564 ret = srcu_notifier_chain_unregister(&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
2568 /* XXX the notifier code should handle this better */
2569 if (!cn->notifier_head.head) {
2570 srcu_cleanup_notifier_head(&cn->notifier_head);
Lai Jiangshan72b53222013-06-03 17:17:15 +08002571 list_del(&cn->node);
Mike Turquetteb24764902012-03-15 23:11:19 -07002572 kfree(cn);
2573 }
2574
2575 } else {
2576 ret = -ENOENT;
2577 }
2578
Mike Turquetteeab89f62013-03-28 13:59:01 -07002579 clk_prepare_unlock();
Mike Turquetteb24764902012-03-15 23:11:19 -07002580
2581 return ret;
2582}
2583EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05002584
2585#ifdef CONFIG_OF
2586/**
2587 * struct of_clk_provider - Clock provider registration structure
2588 * @link: Entry in global list of clock providers
2589 * @node: Pointer to device tree node of clock provider
2590 * @get: Get clock callback. Returns NULL or a struct clk for the
2591 * given clock specifier
2592 * @data: context pointer to be passed into @get callback
2593 */
2594struct of_clk_provider {
2595 struct list_head link;
2596
2597 struct device_node *node;
2598 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2599 void *data;
2600};
2601
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302602static const struct of_device_id __clk_of_table_sentinel
2603 __used __section(__clk_of_table_end);
2604
Grant Likely766e6a42012-04-09 14:50:06 -05002605static LIST_HEAD(of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002606static DEFINE_MUTEX(of_clk_mutex);
2607
2608/* of_clk_provider list locking helpers */
2609void of_clk_lock(void)
2610{
2611 mutex_lock(&of_clk_mutex);
2612}
2613
2614void of_clk_unlock(void)
2615{
2616 mutex_unlock(&of_clk_mutex);
2617}
Grant Likely766e6a42012-04-09 14:50:06 -05002618
2619struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2620 void *data)
2621{
2622 return data;
2623}
2624EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2625
Shawn Guo494bfec2012-08-22 21:36:27 +08002626struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2627{
2628 struct clk_onecell_data *clk_data = data;
2629 unsigned int idx = clkspec->args[0];
2630
2631 if (idx >= clk_data->clk_num) {
2632 pr_err("%s: invalid clock index %d\n", __func__, idx);
2633 return ERR_PTR(-EINVAL);
2634 }
2635
2636 return clk_data->clks[idx];
2637}
2638EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2639
Grant Likely766e6a42012-04-09 14:50:06 -05002640/**
2641 * of_clk_add_provider() - Register a clock provider for a node
2642 * @np: Device node pointer associated with clock provider
2643 * @clk_src_get: callback for decoding clock
2644 * @data: context pointer for @clk_src_get callback.
2645 */
2646int of_clk_add_provider(struct device_node *np,
2647 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2648 void *data),
2649 void *data)
2650{
2651 struct of_clk_provider *cp;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002652 int ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002653
2654 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2655 if (!cp)
2656 return -ENOMEM;
2657
2658 cp->node = of_node_get(np);
2659 cp->data = data;
2660 cp->get = clk_src_get;
2661
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002662 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002663 list_add(&cp->link, &of_clk_providers);
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002664 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002665 pr_debug("Added clock from %s\n", np->full_name);
2666
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002667 ret = of_clk_set_defaults(np, true);
2668 if (ret < 0)
2669 of_clk_del_provider(np);
2670
2671 return ret;
Grant Likely766e6a42012-04-09 14:50:06 -05002672}
2673EXPORT_SYMBOL_GPL(of_clk_add_provider);
2674
2675/**
2676 * of_clk_del_provider() - Remove a previously registered clock provider
2677 * @np: Device node pointer associated with clock provider
2678 */
2679void of_clk_del_provider(struct device_node *np)
2680{
2681 struct of_clk_provider *cp;
2682
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002683 mutex_lock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002684 list_for_each_entry(cp, &of_clk_providers, link) {
2685 if (cp->node == np) {
2686 list_del(&cp->link);
2687 of_node_put(cp->node);
2688 kfree(cp);
2689 break;
2690 }
2691 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002692 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002693}
2694EXPORT_SYMBOL_GPL(of_clk_del_provider);
2695
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002696struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec)
Grant Likely766e6a42012-04-09 14:50:06 -05002697{
2698 struct of_clk_provider *provider;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +01002699 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
Grant Likely766e6a42012-04-09 14:50:06 -05002700
2701 /* Check if we have such a provider in our array */
Grant Likely766e6a42012-04-09 14:50:06 -05002702 list_for_each_entry(provider, &of_clk_providers, link) {
2703 if (provider->node == clkspec->np)
2704 clk = provider->get(clkspec, provider->data);
2705 if (!IS_ERR(clk))
2706 break;
2707 }
Sylwester Nawrockid6782c22013-08-23 17:03:43 +02002708
2709 return clk;
2710}
2711
2712struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2713{
2714 struct clk *clk;
2715
2716 mutex_lock(&of_clk_mutex);
2717 clk = __of_clk_get_from_provider(clkspec);
2718 mutex_unlock(&of_clk_mutex);
Grant Likely766e6a42012-04-09 14:50:06 -05002719
2720 return clk;
2721}
2722
Mike Turquettef6102742013-10-07 23:12:13 -07002723int of_clk_get_parent_count(struct device_node *np)
2724{
2725 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2726}
2727EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2728
Grant Likely766e6a42012-04-09 14:50:06 -05002729const char *of_clk_get_parent_name(struct device_node *np, int index)
2730{
2731 struct of_phandle_args clkspec;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002732 struct property *prop;
Grant Likely766e6a42012-04-09 14:50:06 -05002733 const char *clk_name;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002734 const __be32 *vp;
2735 u32 pv;
Grant Likely766e6a42012-04-09 14:50:06 -05002736 int rc;
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002737 int count;
Grant Likely766e6a42012-04-09 14:50:06 -05002738
2739 if (index < 0)
2740 return NULL;
2741
2742 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2743 &clkspec);
2744 if (rc)
2745 return NULL;
2746
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002747 index = clkspec.args_count ? clkspec.args[0] : 0;
2748 count = 0;
2749
2750 /* if there is an indices property, use it to transfer the index
2751 * specified into an array offset for the clock-output-names property.
2752 */
2753 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
2754 if (index == pv) {
2755 index = count;
2756 break;
2757 }
2758 count++;
2759 }
2760
Grant Likely766e6a42012-04-09 14:50:06 -05002761 if (of_property_read_string_index(clkspec.np, "clock-output-names",
Ben Dooks7a0fc1a2014-02-13 18:02:49 +00002762 index,
Grant Likely766e6a42012-04-09 14:50:06 -05002763 &clk_name) < 0)
2764 clk_name = clkspec.np->name;
2765
2766 of_node_put(clkspec.np);
2767 return clk_name;
2768}
2769EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
2770
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002771struct clock_provider {
2772 of_clk_init_cb_t clk_init_cb;
2773 struct device_node *np;
2774 struct list_head node;
2775};
2776
2777static LIST_HEAD(clk_provider_list);
2778
2779/*
2780 * This function looks for a parent clock. If there is one, then it
2781 * checks that the provider for this parent clock was initialized, in
2782 * this case the parent clock will be ready.
2783 */
2784static int parent_ready(struct device_node *np)
2785{
2786 int i = 0;
2787
2788 while (true) {
2789 struct clk *clk = of_clk_get(np, i);
2790
2791 /* this parent is ready we can check the next one */
2792 if (!IS_ERR(clk)) {
2793 clk_put(clk);
2794 i++;
2795 continue;
2796 }
2797
2798 /* at least one parent is not ready, we exit now */
2799 if (PTR_ERR(clk) == -EPROBE_DEFER)
2800 return 0;
2801
2802 /*
2803 * Here we make assumption that the device tree is
2804 * written correctly. So an error means that there is
2805 * no more parent. As we didn't exit yet, then the
2806 * previous parent are ready. If there is no clock
2807 * parent, no need to wait for them, then we can
2808 * consider their absence as being ready
2809 */
2810 return 1;
2811 }
2812}
2813
Grant Likely766e6a42012-04-09 14:50:06 -05002814/**
2815 * of_clk_init() - Scan and init clock providers from the DT
2816 * @matches: array of compatible values and init functions for providers.
2817 *
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002818 * This function scans the device tree for matching clock providers
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01002819 * and calls their initialization functions. It also does it by trying
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002820 * to follow the dependencies.
Grant Likely766e6a42012-04-09 14:50:06 -05002821 */
2822void __init of_clk_init(const struct of_device_id *matches)
2823{
Alex Elder7f7ed582013-08-22 11:31:31 -05002824 const struct of_device_id *match;
Grant Likely766e6a42012-04-09 14:50:06 -05002825 struct device_node *np;
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002826 struct clock_provider *clk_provider, *next;
2827 bool is_init_done;
2828 bool force = false;
Grant Likely766e6a42012-04-09 14:50:06 -05002829
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302830 if (!matches)
Tero Kristo819b4862013-10-22 11:39:36 +03002831 matches = &__clk_of_table;
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05302832
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002833 /* First prepare the list of the clocks providers */
Alex Elder7f7ed582013-08-22 11:31:31 -05002834 for_each_matching_node_and_match(np, matches, &match) {
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002835 struct clock_provider *parent =
2836 kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
2837
2838 parent->clk_init_cb = match->data;
2839 parent->np = np;
Sylwester Nawrocki3f6d4392014-03-27 11:43:32 +01002840 list_add_tail(&parent->node, &clk_provider_list);
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002841 }
2842
2843 while (!list_empty(&clk_provider_list)) {
2844 is_init_done = false;
2845 list_for_each_entry_safe(clk_provider, next,
2846 &clk_provider_list, node) {
2847 if (force || parent_ready(clk_provider->np)) {
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002848
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002849 clk_provider->clk_init_cb(clk_provider->np);
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02002850 of_clk_set_defaults(clk_provider->np, true);
2851
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002852 list_del(&clk_provider->node);
2853 kfree(clk_provider);
2854 is_init_done = true;
2855 }
2856 }
2857
2858 /*
Sylwester Nawrockie5ca8fb2014-03-27 12:08:36 +01002859 * We didn't manage to initialize any of the
Gregory CLEMENT1771b102014-02-24 19:10:13 +01002860 * remaining providers during the last loop, so now we
2861 * initialize all the remaining ones unconditionally
2862 * in case the clock parent was not mandatory
2863 */
2864 if (!is_init_done)
2865 force = true;
Grant Likely766e6a42012-04-09 14:50:06 -05002866 }
2867}
2868#endif