blob: ed87b2405806f6a80f24b607be5d23ccc922ce26 [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>
13#include <linux/module.h>
14#include <linux/mutex.h>
15#include <linux/spinlock.h>
16#include <linux/err.h>
17#include <linux/list.h>
18#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050019#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070020#include <linux/device.h>
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +053021#include <linux/init.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070022
23static DEFINE_SPINLOCK(enable_lock);
24static DEFINE_MUTEX(prepare_lock);
25
26static HLIST_HEAD(clk_root_list);
27static HLIST_HEAD(clk_orphan_list);
28static LIST_HEAD(clk_notifier_list);
29
30/*** debugfs support ***/
31
32#ifdef CONFIG_COMMON_CLK_DEBUG
33#include <linux/debugfs.h>
34
35static struct dentry *rootdir;
36static struct dentry *orphandir;
37static int inited = 0;
38
Prashant Gaikwad1af599d2012-12-26 19:16:22 +053039static void clk_summary_show_one(struct seq_file *s, struct clk *c, int level)
40{
41 if (!c)
42 return;
43
44 seq_printf(s, "%*s%-*s %-11d %-12d %-10lu",
45 level * 3 + 1, "",
46 30 - level * 3, c->name,
47 c->enable_count, c->prepare_count, c->rate);
48 seq_printf(s, "\n");
49}
50
51static void clk_summary_show_subtree(struct seq_file *s, struct clk *c,
52 int level)
53{
54 struct clk *child;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +053055
56 if (!c)
57 return;
58
59 clk_summary_show_one(s, c, level);
60
Sasha Levinb67bfe02013-02-27 17:06:00 -080061 hlist_for_each_entry(child, &c->children, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +053062 clk_summary_show_subtree(s, child, level + 1);
63}
64
65static int clk_summary_show(struct seq_file *s, void *data)
66{
67 struct clk *c;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +053068
69 seq_printf(s, " clock enable_cnt prepare_cnt rate\n");
70 seq_printf(s, "---------------------------------------------------------------------\n");
71
72 mutex_lock(&prepare_lock);
73
Sasha Levinb67bfe02013-02-27 17:06:00 -080074 hlist_for_each_entry(c, &clk_root_list, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +053075 clk_summary_show_subtree(s, c, 0);
76
Sasha Levinb67bfe02013-02-27 17:06:00 -080077 hlist_for_each_entry(c, &clk_orphan_list, child_node)
Prashant Gaikwad1af599d2012-12-26 19:16:22 +053078 clk_summary_show_subtree(s, c, 0);
79
80 mutex_unlock(&prepare_lock);
81
82 return 0;
83}
84
85
86static int clk_summary_open(struct inode *inode, struct file *file)
87{
88 return single_open(file, clk_summary_show, inode->i_private);
89}
90
91static const struct file_operations clk_summary_fops = {
92 .open = clk_summary_open,
93 .read = seq_read,
94 .llseek = seq_lseek,
95 .release = single_release,
96};
97
Prashant Gaikwadbddca892012-12-26 19:16:23 +053098static void clk_dump_one(struct seq_file *s, struct clk *c, int level)
99{
100 if (!c)
101 return;
102
103 seq_printf(s, "\"%s\": { ", c->name);
104 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
105 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
106 seq_printf(s, "\"rate\": %lu", c->rate);
107}
108
109static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level)
110{
111 struct clk *child;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530112
113 if (!c)
114 return;
115
116 clk_dump_one(s, c, level);
117
Sasha Levinb67bfe02013-02-27 17:06:00 -0800118 hlist_for_each_entry(child, &c->children, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530119 seq_printf(s, ",");
120 clk_dump_subtree(s, child, level + 1);
121 }
122
123 seq_printf(s, "}");
124}
125
126static int clk_dump(struct seq_file *s, void *data)
127{
128 struct clk *c;
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530129 bool first_node = true;
130
131 seq_printf(s, "{");
132
133 mutex_lock(&prepare_lock);
134
Sasha Levinb67bfe02013-02-27 17:06:00 -0800135 hlist_for_each_entry(c, &clk_root_list, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530136 if (!first_node)
137 seq_printf(s, ",");
138 first_node = false;
139 clk_dump_subtree(s, c, 0);
140 }
141
Sasha Levinb67bfe02013-02-27 17:06:00 -0800142 hlist_for_each_entry(c, &clk_orphan_list, child_node) {
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530143 seq_printf(s, ",");
144 clk_dump_subtree(s, c, 0);
145 }
146
147 mutex_unlock(&prepare_lock);
148
149 seq_printf(s, "}");
150 return 0;
151}
152
153
154static int clk_dump_open(struct inode *inode, struct file *file)
155{
156 return single_open(file, clk_dump, inode->i_private);
157}
158
159static const struct file_operations clk_dump_fops = {
160 .open = clk_dump_open,
161 .read = seq_read,
162 .llseek = seq_lseek,
163 .release = single_release,
164};
165
Mike Turquetteb24764902012-03-15 23:11:19 -0700166/* caller must hold prepare_lock */
167static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
168{
169 struct dentry *d;
170 int ret = -ENOMEM;
171
172 if (!clk || !pdentry) {
173 ret = -EINVAL;
174 goto out;
175 }
176
177 d = debugfs_create_dir(clk->name, pdentry);
178 if (!d)
179 goto out;
180
181 clk->dentry = d;
182
183 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
184 (u32 *)&clk->rate);
185 if (!d)
186 goto err_out;
187
188 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
189 (u32 *)&clk->flags);
190 if (!d)
191 goto err_out;
192
193 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
194 (u32 *)&clk->prepare_count);
195 if (!d)
196 goto err_out;
197
198 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
199 (u32 *)&clk->enable_count);
200 if (!d)
201 goto err_out;
202
203 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
204 (u32 *)&clk->notifier_count);
205 if (!d)
206 goto err_out;
207
208 ret = 0;
209 goto out;
210
211err_out:
212 debugfs_remove(clk->dentry);
213out:
214 return ret;
215}
216
217/* caller must hold prepare_lock */
218static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry)
219{
220 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700221 int ret = -EINVAL;;
222
223 if (!clk || !pdentry)
224 goto out;
225
226 ret = clk_debug_create_one(clk, pdentry);
227
228 if (ret)
229 goto out;
230
Sasha Levinb67bfe02013-02-27 17:06:00 -0800231 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700232 clk_debug_create_subtree(child, clk->dentry);
233
234 ret = 0;
235out:
236 return ret;
237}
238
239/**
240 * clk_debug_register - add a clk node to the debugfs clk tree
241 * @clk: the clk being added to the debugfs clk tree
242 *
243 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
244 * initialized. Otherwise it bails out early since the debugfs clk tree
245 * will be created lazily by clk_debug_init as part of a late_initcall.
246 *
247 * Caller must hold prepare_lock. Only clk_init calls this function (so
248 * far) so this is taken care.
249 */
250static int clk_debug_register(struct clk *clk)
251{
252 struct clk *parent;
253 struct dentry *pdentry;
254 int ret = 0;
255
256 if (!inited)
257 goto out;
258
259 parent = clk->parent;
260
261 /*
262 * Check to see if a clk is a root clk. Also check that it is
263 * safe to add this clk to debugfs
264 */
265 if (!parent)
266 if (clk->flags & CLK_IS_ROOT)
267 pdentry = rootdir;
268 else
269 pdentry = orphandir;
270 else
271 if (parent->dentry)
272 pdentry = parent->dentry;
273 else
274 goto out;
275
276 ret = clk_debug_create_subtree(clk, pdentry);
277
278out:
279 return ret;
280}
281
282/**
283 * clk_debug_init - lazily create the debugfs clk tree visualization
284 *
285 * clks are often initialized very early during boot before memory can
286 * be dynamically allocated and well before debugfs is setup.
287 * clk_debug_init walks the clk tree hierarchy while holding
288 * prepare_lock and creates the topology as part of a late_initcall,
289 * thus insuring that clks initialized very early will still be
290 * represented in the debugfs clk tree. This function should only be
291 * called once at boot-time, and all other clks added dynamically will
292 * be done so with clk_debug_register.
293 */
294static int __init clk_debug_init(void)
295{
296 struct clk *clk;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530297 struct dentry *d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700298
299 rootdir = debugfs_create_dir("clk", NULL);
300
301 if (!rootdir)
302 return -ENOMEM;
303
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530304 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, NULL,
305 &clk_summary_fops);
306 if (!d)
307 return -ENOMEM;
308
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530309 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, NULL,
310 &clk_dump_fops);
311 if (!d)
312 return -ENOMEM;
313
Mike Turquetteb24764902012-03-15 23:11:19 -0700314 orphandir = debugfs_create_dir("orphans", rootdir);
315
316 if (!orphandir)
317 return -ENOMEM;
318
319 mutex_lock(&prepare_lock);
320
Sasha Levinb67bfe02013-02-27 17:06:00 -0800321 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700322 clk_debug_create_subtree(clk, rootdir);
323
Sasha Levinb67bfe02013-02-27 17:06:00 -0800324 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700325 clk_debug_create_subtree(clk, orphandir);
326
327 inited = 1;
328
329 mutex_unlock(&prepare_lock);
330
331 return 0;
332}
333late_initcall(clk_debug_init);
334#else
335static inline int clk_debug_register(struct clk *clk) { return 0; }
Mike Turquette70d347e2012-03-26 11:53:47 -0700336#endif
Mike Turquetteb24764902012-03-15 23:11:19 -0700337
Mike Turquetteb24764902012-03-15 23:11:19 -0700338/* caller must hold prepare_lock */
339static void clk_disable_unused_subtree(struct clk *clk)
340{
341 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700342 unsigned long flags;
343
344 if (!clk)
345 goto out;
346
Sasha Levinb67bfe02013-02-27 17:06:00 -0800347 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700348 clk_disable_unused_subtree(child);
349
350 spin_lock_irqsave(&enable_lock, flags);
351
352 if (clk->enable_count)
353 goto unlock_out;
354
355 if (clk->flags & CLK_IGNORE_UNUSED)
356 goto unlock_out;
357
Mike Turquette7c045a52012-12-04 11:00:35 -0800358 /*
359 * some gate clocks have special needs during the disable-unused
360 * sequence. call .disable_unused if available, otherwise fall
361 * back to .disable
362 */
363 if (__clk_is_enabled(clk)) {
364 if (clk->ops->disable_unused)
365 clk->ops->disable_unused(clk->hw);
366 else if (clk->ops->disable)
367 clk->ops->disable(clk->hw);
368 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700369
370unlock_out:
371 spin_unlock_irqrestore(&enable_lock, flags);
372
373out:
374 return;
375}
376
377static int clk_disable_unused(void)
378{
379 struct clk *clk;
Mike Turquetteb24764902012-03-15 23:11:19 -0700380
381 mutex_lock(&prepare_lock);
382
Sasha Levinb67bfe02013-02-27 17:06:00 -0800383 hlist_for_each_entry(clk, &clk_root_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700384 clk_disable_unused_subtree(clk);
385
Sasha Levinb67bfe02013-02-27 17:06:00 -0800386 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700387 clk_disable_unused_subtree(clk);
388
389 mutex_unlock(&prepare_lock);
390
391 return 0;
392}
393late_initcall(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700394
395/*** helper functions ***/
396
Russ Dill65800b22012-11-26 11:20:09 -0800397const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700398{
399 return !clk ? NULL : clk->name;
400}
Niels de Vos48950842012-12-13 13:12:25 +0100401EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700402
Russ Dill65800b22012-11-26 11:20:09 -0800403struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700404{
405 return !clk ? NULL : clk->hw;
406}
407
Russ Dill65800b22012-11-26 11:20:09 -0800408u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700409{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700410 return !clk ? 0 : clk->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700411}
412
Russ Dill65800b22012-11-26 11:20:09 -0800413struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700414{
415 return !clk ? NULL : clk->parent;
416}
417
Russ Dill65800b22012-11-26 11:20:09 -0800418unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700419{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700420 return !clk ? 0 : clk->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700421}
422
Russ Dill65800b22012-11-26 11:20:09 -0800423unsigned int __clk_get_prepare_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700424{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700425 return !clk ? 0 : clk->prepare_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700426}
427
428unsigned long __clk_get_rate(struct clk *clk)
429{
430 unsigned long ret;
431
432 if (!clk) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530433 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700434 goto out;
435 }
436
437 ret = clk->rate;
438
439 if (clk->flags & CLK_IS_ROOT)
440 goto out;
441
442 if (!clk->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530443 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700444
445out:
446 return ret;
447}
448
Russ Dill65800b22012-11-26 11:20:09 -0800449unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700450{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700451 return !clk ? 0 : clk->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700452}
453
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700454bool __clk_is_enabled(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700455{
456 int ret;
457
458 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700459 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700460
461 /*
462 * .is_enabled is only mandatory for clocks that gate
463 * fall back to software usage counter if .is_enabled is missing
464 */
465 if (!clk->ops->is_enabled) {
466 ret = clk->enable_count ? 1 : 0;
467 goto out;
468 }
469
470 ret = clk->ops->is_enabled(clk->hw);
471out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700472 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700473}
474
475static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
476{
477 struct clk *child;
478 struct clk *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700479
480 if (!strcmp(clk->name, name))
481 return clk;
482
Sasha Levinb67bfe02013-02-27 17:06:00 -0800483 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700484 ret = __clk_lookup_subtree(name, child);
485 if (ret)
486 return ret;
487 }
488
489 return NULL;
490}
491
492struct clk *__clk_lookup(const char *name)
493{
494 struct clk *root_clk;
495 struct clk *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700496
497 if (!name)
498 return NULL;
499
500 /* search the 'proper' clk tree first */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800501 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700502 ret = __clk_lookup_subtree(name, root_clk);
503 if (ret)
504 return ret;
505 }
506
507 /* if not found, then search the orphan tree */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800508 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700509 ret = __clk_lookup_subtree(name, root_clk);
510 if (ret)
511 return ret;
512 }
513
514 return NULL;
515}
516
517/*** clk api ***/
518
519void __clk_unprepare(struct clk *clk)
520{
521 if (!clk)
522 return;
523
524 if (WARN_ON(clk->prepare_count == 0))
525 return;
526
527 if (--clk->prepare_count > 0)
528 return;
529
530 WARN_ON(clk->enable_count > 0);
531
532 if (clk->ops->unprepare)
533 clk->ops->unprepare(clk->hw);
534
535 __clk_unprepare(clk->parent);
536}
537
538/**
539 * clk_unprepare - undo preparation of a clock source
540 * @clk: the clk being unprepare
541 *
542 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
543 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
544 * if the operation may sleep. One example is a clk which is accessed over
545 * I2c. In the complex case a clk gate operation may require a fast and a slow
546 * part. It is this reason that clk_unprepare and clk_disable are not mutually
547 * exclusive. In fact clk_disable must be called before clk_unprepare.
548 */
549void clk_unprepare(struct clk *clk)
550{
551 mutex_lock(&prepare_lock);
552 __clk_unprepare(clk);
553 mutex_unlock(&prepare_lock);
554}
555EXPORT_SYMBOL_GPL(clk_unprepare);
556
557int __clk_prepare(struct clk *clk)
558{
559 int ret = 0;
560
561 if (!clk)
562 return 0;
563
564 if (clk->prepare_count == 0) {
565 ret = __clk_prepare(clk->parent);
566 if (ret)
567 return ret;
568
569 if (clk->ops->prepare) {
570 ret = clk->ops->prepare(clk->hw);
571 if (ret) {
572 __clk_unprepare(clk->parent);
573 return ret;
574 }
575 }
576 }
577
578 clk->prepare_count++;
579
580 return 0;
581}
582
583/**
584 * clk_prepare - prepare a clock source
585 * @clk: the clk being prepared
586 *
587 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
588 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
589 * operation may sleep. One example is a clk which is accessed over I2c. In
590 * the complex case a clk ungate operation may require a fast and a slow part.
591 * It is this reason that clk_prepare and clk_enable are not mutually
592 * exclusive. In fact clk_prepare must be called before clk_enable.
593 * Returns 0 on success, -EERROR otherwise.
594 */
595int clk_prepare(struct clk *clk)
596{
597 int ret;
598
599 mutex_lock(&prepare_lock);
600 ret = __clk_prepare(clk);
601 mutex_unlock(&prepare_lock);
602
603 return ret;
604}
605EXPORT_SYMBOL_GPL(clk_prepare);
606
607static void __clk_disable(struct clk *clk)
608{
609 if (!clk)
610 return;
611
Fengguang Wue47c6a32012-07-30 14:39:54 -0700612 if (WARN_ON(IS_ERR(clk)))
613 return;
614
Mike Turquetteb24764902012-03-15 23:11:19 -0700615 if (WARN_ON(clk->enable_count == 0))
616 return;
617
618 if (--clk->enable_count > 0)
619 return;
620
621 if (clk->ops->disable)
622 clk->ops->disable(clk->hw);
623
624 __clk_disable(clk->parent);
625}
626
627/**
628 * clk_disable - gate a clock
629 * @clk: the clk being gated
630 *
631 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
632 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
633 * clk if the operation is fast and will never sleep. One example is a
634 * SoC-internal clk which is controlled via simple register writes. In the
635 * complex case a clk gate operation may require a fast and a slow part. It is
636 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
637 * In fact clk_disable must be called before clk_unprepare.
638 */
639void clk_disable(struct clk *clk)
640{
641 unsigned long flags;
642
643 spin_lock_irqsave(&enable_lock, flags);
644 __clk_disable(clk);
645 spin_unlock_irqrestore(&enable_lock, flags);
646}
647EXPORT_SYMBOL_GPL(clk_disable);
648
649static int __clk_enable(struct clk *clk)
650{
651 int ret = 0;
652
653 if (!clk)
654 return 0;
655
656 if (WARN_ON(clk->prepare_count == 0))
657 return -ESHUTDOWN;
658
659 if (clk->enable_count == 0) {
660 ret = __clk_enable(clk->parent);
661
662 if (ret)
663 return ret;
664
665 if (clk->ops->enable) {
666 ret = clk->ops->enable(clk->hw);
667 if (ret) {
668 __clk_disable(clk->parent);
669 return ret;
670 }
671 }
672 }
673
674 clk->enable_count++;
675 return 0;
676}
677
678/**
679 * clk_enable - ungate a clock
680 * @clk: the clk being ungated
681 *
682 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
683 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
684 * if the operation will never sleep. One example is a SoC-internal clk which
685 * is controlled via simple register writes. In the complex case a clk ungate
686 * operation may require a fast and a slow part. It is this reason that
687 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
688 * must be called before clk_enable. Returns 0 on success, -EERROR
689 * otherwise.
690 */
691int clk_enable(struct clk *clk)
692{
693 unsigned long flags;
694 int ret;
695
696 spin_lock_irqsave(&enable_lock, flags);
697 ret = __clk_enable(clk);
698 spin_unlock_irqrestore(&enable_lock, flags);
699
700 return ret;
701}
702EXPORT_SYMBOL_GPL(clk_enable);
703
704/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700705 * __clk_round_rate - round the given rate for a clk
706 * @clk: round the rate of this clock
707 *
708 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
709 */
710unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
711{
Shawn Guo81536e02012-04-12 20:50:17 +0800712 unsigned long parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700713
714 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700715 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700716
Shawn Guof4d8af22012-04-12 20:50:19 +0800717 if (!clk->ops->round_rate) {
718 if (clk->flags & CLK_SET_RATE_PARENT)
719 return __clk_round_rate(clk->parent, rate);
720 else
721 return clk->rate;
722 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700723
Shawn Guo81536e02012-04-12 20:50:17 +0800724 if (clk->parent)
725 parent_rate = clk->parent->rate;
726
727 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700728}
729
730/**
731 * clk_round_rate - round the given rate for a clk
732 * @clk: the clk for which we are rounding a rate
733 * @rate: the rate which is to be rounded
734 *
735 * Takes in a rate as input and rounds it to a rate that the clk can actually
736 * use which is then returned. If clk doesn't support round_rate operation
737 * then the parent rate is returned.
738 */
739long clk_round_rate(struct clk *clk, unsigned long rate)
740{
741 unsigned long ret;
742
743 mutex_lock(&prepare_lock);
744 ret = __clk_round_rate(clk, rate);
745 mutex_unlock(&prepare_lock);
746
747 return ret;
748}
749EXPORT_SYMBOL_GPL(clk_round_rate);
750
751/**
752 * __clk_notify - call clk notifier chain
753 * @clk: struct clk * that is changing rate
754 * @msg: clk notifier type (see include/linux/clk.h)
755 * @old_rate: old clk rate
756 * @new_rate: new clk rate
757 *
758 * Triggers a notifier call chain on the clk rate-change notification
759 * for 'clk'. Passes a pointer to the struct clk and the previous
760 * and current rates to the notifier callback. Intended to be called by
761 * internal clock code only. Returns NOTIFY_DONE from the last driver
762 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
763 * a driver returns that.
764 */
765static int __clk_notify(struct clk *clk, unsigned long msg,
766 unsigned long old_rate, unsigned long new_rate)
767{
768 struct clk_notifier *cn;
769 struct clk_notifier_data cnd;
770 int ret = NOTIFY_DONE;
771
772 cnd.clk = clk;
773 cnd.old_rate = old_rate;
774 cnd.new_rate = new_rate;
775
776 list_for_each_entry(cn, &clk_notifier_list, node) {
777 if (cn->clk == clk) {
778 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
779 &cnd);
780 break;
781 }
782 }
783
784 return ret;
785}
786
787/**
788 * __clk_recalc_rates
789 * @clk: first clk in the subtree
790 * @msg: notification type (see include/linux/clk.h)
791 *
792 * Walks the subtree of clks starting with clk and recalculates rates as it
793 * goes. Note that if a clk does not implement the .recalc_rate callback then
794 * it is assumed that the clock will take on the rate of it's parent.
795 *
796 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
797 * if necessary.
798 *
799 * Caller must hold prepare_lock.
800 */
801static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
802{
803 unsigned long old_rate;
804 unsigned long parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700805 struct clk *child;
806
807 old_rate = clk->rate;
808
809 if (clk->parent)
810 parent_rate = clk->parent->rate;
811
812 if (clk->ops->recalc_rate)
813 clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
814 else
815 clk->rate = parent_rate;
816
817 /*
818 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
819 * & ABORT_RATE_CHANGE notifiers
820 */
821 if (clk->notifier_count && msg)
822 __clk_notify(clk, msg, old_rate, clk->rate);
823
Sasha Levinb67bfe02013-02-27 17:06:00 -0800824 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700825 __clk_recalc_rates(child, msg);
826}
827
828/**
Ulf Hanssona093bde2012-08-31 14:21:28 +0200829 * clk_get_rate - return the rate of clk
830 * @clk: the clk whose rate is being returned
831 *
832 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
833 * is set, which means a recalc_rate will be issued.
834 * If clk is NULL then returns 0.
835 */
836unsigned long clk_get_rate(struct clk *clk)
837{
838 unsigned long rate;
839
840 mutex_lock(&prepare_lock);
841
842 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
843 __clk_recalc_rates(clk, 0);
844
845 rate = __clk_get_rate(clk);
846 mutex_unlock(&prepare_lock);
847
848 return rate;
849}
850EXPORT_SYMBOL_GPL(clk_get_rate);
851
852/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700853 * __clk_speculate_rates
854 * @clk: first clk in the subtree
855 * @parent_rate: the "future" rate of clk's parent
856 *
857 * Walks the subtree of clks starting with clk, speculating rates as it
858 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
859 *
860 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
861 * pre-rate change notifications and returns early if no clks in the
862 * subtree have subscribed to the notifications. Note that if a clk does not
863 * implement the .recalc_rate callback then it is assumed that the clock will
864 * take on the rate of it's parent.
865 *
866 * Caller must hold prepare_lock.
867 */
868static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
869{
Mike Turquetteb24764902012-03-15 23:11:19 -0700870 struct clk *child;
871 unsigned long new_rate;
872 int ret = NOTIFY_DONE;
873
874 if (clk->ops->recalc_rate)
875 new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
876 else
877 new_rate = parent_rate;
878
879 /* abort the rate change if a driver returns NOTIFY_BAD */
880 if (clk->notifier_count)
881 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
882
883 if (ret == NOTIFY_BAD)
884 goto out;
885
Sasha Levinb67bfe02013-02-27 17:06:00 -0800886 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700887 ret = __clk_speculate_rates(child, new_rate);
888 if (ret == NOTIFY_BAD)
889 break;
890 }
891
892out:
893 return ret;
894}
895
896static void clk_calc_subtree(struct clk *clk, unsigned long new_rate)
897{
898 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700899
900 clk->new_rate = new_rate;
901
Sasha Levinb67bfe02013-02-27 17:06:00 -0800902 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700903 if (child->ops->recalc_rate)
904 child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
905 else
906 child->new_rate = new_rate;
907 clk_calc_subtree(child, child->new_rate);
908 }
909}
910
911/*
912 * calculate the new rates returning the topmost clock that has to be
913 * changed.
914 */
915static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
916{
917 struct clk *top = clk;
Shawn Guo81536e02012-04-12 20:50:17 +0800918 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700919 unsigned long new_rate;
920
Mike Turquette7452b212012-03-26 14:45:36 -0700921 /* sanity */
922 if (IS_ERR_OR_NULL(clk))
923 return NULL;
924
Mike Turquette63f5c3b2012-05-02 16:23:43 -0700925 /* save parent rate, if it exists */
926 if (clk->parent)
927 best_parent_rate = clk->parent->rate;
928
Mike Turquette7452b212012-03-26 14:45:36 -0700929 /* never propagate up to the parent */
930 if (!(clk->flags & CLK_SET_RATE_PARENT)) {
931 if (!clk->ops->round_rate) {
932 clk->new_rate = clk->rate;
933 return NULL;
Mike Turquette7452b212012-03-26 14:45:36 -0700934 }
Mike Turquette63f5c3b2012-05-02 16:23:43 -0700935 new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
936 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -0700937 }
938
939 /* need clk->parent from here on out */
940 if (!clk->parent) {
941 pr_debug("%s: %s has NULL parent\n", __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700942 return NULL;
943 }
944
Mike Turquette7452b212012-03-26 14:45:36 -0700945 if (!clk->ops->round_rate) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700946 top = clk_calc_new_rates(clk->parent, rate);
Viresh Kumar1b2f9902012-04-17 16:45:38 +0530947 new_rate = clk->parent->new_rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700948
949 goto out;
950 }
951
Mike Turquette7452b212012-03-26 14:45:36 -0700952 new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700953
954 if (best_parent_rate != clk->parent->rate) {
955 top = clk_calc_new_rates(clk->parent, best_parent_rate);
956
957 goto out;
958 }
959
960out:
961 clk_calc_subtree(clk, new_rate);
962
963 return top;
964}
965
966/*
967 * Notify about rate changes in a subtree. Always walk down the whole tree
968 * so that in case of an error we can walk down the whole tree again and
969 * abort the change.
970 */
971static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
972{
Mike Turquetteb24764902012-03-15 23:11:19 -0700973 struct clk *child, *fail_clk = NULL;
974 int ret = NOTIFY_DONE;
975
976 if (clk->rate == clk->new_rate)
977 return 0;
978
979 if (clk->notifier_count) {
980 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
981 if (ret == NOTIFY_BAD)
982 fail_clk = clk;
983 }
984
Sasha Levinb67bfe02013-02-27 17:06:00 -0800985 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700986 clk = clk_propagate_rate_change(child, event);
987 if (clk)
988 fail_clk = clk;
989 }
990
991 return fail_clk;
992}
993
994/*
995 * walk down a subtree and set the new rates notifying the rate
996 * change on the way
997 */
998static void clk_change_rate(struct clk *clk)
999{
1000 struct clk *child;
1001 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001002 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001003
1004 old_rate = clk->rate;
1005
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001006 if (clk->parent)
1007 best_parent_rate = clk->parent->rate;
1008
Mike Turquetteb24764902012-03-15 23:11:19 -07001009 if (clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001010 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001011
1012 if (clk->ops->recalc_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001013 clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001014 else
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001015 clk->rate = best_parent_rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001016
1017 if (clk->notifier_count && old_rate != clk->rate)
1018 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1019
Sasha Levinb67bfe02013-02-27 17:06:00 -08001020 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001021 clk_change_rate(child);
1022}
1023
1024/**
1025 * clk_set_rate - specify a new rate for clk
1026 * @clk: the clk whose rate is being changed
1027 * @rate: the new rate for clk
1028 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001029 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001030 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001031 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1032 * propagate up to clk's parent; whether or not this happens depends on the
1033 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1034 * after calling .round_rate then upstream parent propagation is ignored. If
1035 * *parent_rate comes back with a new rate for clk's parent then we propagate
1036 * up to clk's parent and set it's rate. Upward propagation will continue
1037 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1038 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001039 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001040 * Rate changes are accomplished via tree traversal that also recalculates the
1041 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001042 *
1043 * Returns 0 on success, -EERROR otherwise.
1044 */
1045int clk_set_rate(struct clk *clk, unsigned long rate)
1046{
1047 struct clk *top, *fail_clk;
1048 int ret = 0;
1049
1050 /* prevent racing with updates to the clock topology */
1051 mutex_lock(&prepare_lock);
1052
1053 /* bail early if nothing to do */
1054 if (rate == clk->rate)
1055 goto out;
1056
Saravana Kannan7e0fa1b2012-05-15 13:43:42 -07001057 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
Viresh Kumar0e1c0302012-04-11 16:03:42 +05301058 ret = -EBUSY;
1059 goto out;
1060 }
1061
Mike Turquetteb24764902012-03-15 23:11:19 -07001062 /* calculate new rates and get the topmost changed clock */
1063 top = clk_calc_new_rates(clk, rate);
1064 if (!top) {
1065 ret = -EINVAL;
1066 goto out;
1067 }
1068
1069 /* notify that we are about to change rates */
1070 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1071 if (fail_clk) {
1072 pr_warn("%s: failed to set %s rate\n", __func__,
1073 fail_clk->name);
1074 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1075 ret = -EBUSY;
1076 goto out;
1077 }
1078
1079 /* change the rates */
1080 clk_change_rate(top);
1081
Mike Turquetteb24764902012-03-15 23:11:19 -07001082out:
1083 mutex_unlock(&prepare_lock);
1084
1085 return ret;
1086}
1087EXPORT_SYMBOL_GPL(clk_set_rate);
1088
1089/**
1090 * clk_get_parent - return the parent of a clk
1091 * @clk: the clk whose parent gets returned
1092 *
1093 * Simply returns clk->parent. Returns NULL if clk is NULL.
1094 */
1095struct clk *clk_get_parent(struct clk *clk)
1096{
1097 struct clk *parent;
1098
1099 mutex_lock(&prepare_lock);
1100 parent = __clk_get_parent(clk);
1101 mutex_unlock(&prepare_lock);
1102
1103 return parent;
1104}
1105EXPORT_SYMBOL_GPL(clk_get_parent);
1106
1107/*
1108 * .get_parent is mandatory for clocks with multiple possible parents. It is
1109 * optional for single-parent clocks. Always call .get_parent if it is
1110 * available and WARN if it is missing for multi-parent clocks.
1111 *
1112 * For single-parent clocks without .get_parent, first check to see if the
1113 * .parents array exists, and if so use it to avoid an expensive tree
1114 * traversal. If .parents does not exist then walk the tree with __clk_lookup.
1115 */
1116static struct clk *__clk_init_parent(struct clk *clk)
1117{
1118 struct clk *ret = NULL;
1119 u8 index;
1120
1121 /* handle the trivial cases */
1122
1123 if (!clk->num_parents)
1124 goto out;
1125
1126 if (clk->num_parents == 1) {
1127 if (IS_ERR_OR_NULL(clk->parent))
1128 ret = clk->parent = __clk_lookup(clk->parent_names[0]);
1129 ret = clk->parent;
1130 goto out;
1131 }
1132
1133 if (!clk->ops->get_parent) {
1134 WARN(!clk->ops->get_parent,
1135 "%s: multi-parent clocks must implement .get_parent\n",
1136 __func__);
1137 goto out;
1138 };
1139
1140 /*
1141 * Do our best to cache parent clocks in clk->parents. This prevents
1142 * unnecessary and expensive calls to __clk_lookup. We don't set
1143 * clk->parent here; that is done by the calling function
1144 */
1145
1146 index = clk->ops->get_parent(clk->hw);
1147
1148 if (!clk->parents)
1149 clk->parents =
Rajendra Nayak79750592012-06-06 14:41:31 +05301150 kzalloc((sizeof(struct clk*) * clk->num_parents),
Mike Turquetteb24764902012-03-15 23:11:19 -07001151 GFP_KERNEL);
1152
1153 if (!clk->parents)
1154 ret = __clk_lookup(clk->parent_names[index]);
1155 else if (!clk->parents[index])
1156 ret = clk->parents[index] =
1157 __clk_lookup(clk->parent_names[index]);
1158 else
1159 ret = clk->parents[index];
1160
1161out:
1162 return ret;
1163}
1164
1165void __clk_reparent(struct clk *clk, struct clk *new_parent)
1166{
1167#ifdef CONFIG_COMMON_CLK_DEBUG
1168 struct dentry *d;
1169 struct dentry *new_parent_d;
1170#endif
1171
1172 if (!clk || !new_parent)
1173 return;
1174
1175 hlist_del(&clk->child_node);
1176
1177 if (new_parent)
1178 hlist_add_head(&clk->child_node, &new_parent->children);
1179 else
1180 hlist_add_head(&clk->child_node, &clk_orphan_list);
1181
1182#ifdef CONFIG_COMMON_CLK_DEBUG
1183 if (!inited)
1184 goto out;
1185
1186 if (new_parent)
1187 new_parent_d = new_parent->dentry;
1188 else
1189 new_parent_d = orphandir;
1190
1191 d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
1192 new_parent_d, clk->name);
1193 if (d)
1194 clk->dentry = d;
1195 else
1196 pr_debug("%s: failed to rename debugfs entry for %s\n",
1197 __func__, clk->name);
1198out:
1199#endif
1200
1201 clk->parent = new_parent;
1202
1203 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1204}
1205
1206static int __clk_set_parent(struct clk *clk, struct clk *parent)
1207{
1208 struct clk *old_parent;
1209 unsigned long flags;
1210 int ret = -EINVAL;
1211 u8 i;
1212
1213 old_parent = clk->parent;
1214
Rajendra Nayak863b1322012-07-03 12:11:41 +05301215 if (!clk->parents)
Rajendra Nayak79750592012-06-06 14:41:31 +05301216 clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
1217 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001218
1219 /*
Rajendra Nayak863b1322012-07-03 12:11:41 +05301220 * find index of new parent clock using cached parent ptrs,
1221 * or if not yet cached, use string name comparison and cache
1222 * them now to avoid future calls to __clk_lookup.
Mike Turquetteb24764902012-03-15 23:11:19 -07001223 */
Rajendra Nayak863b1322012-07-03 12:11:41 +05301224 for (i = 0; i < clk->num_parents; i++) {
1225 if (clk->parents && clk->parents[i] == parent)
1226 break;
1227 else if (!strcmp(clk->parent_names[i], parent->name)) {
1228 if (clk->parents)
1229 clk->parents[i] = __clk_lookup(parent->name);
1230 break;
1231 }
1232 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001233
1234 if (i == clk->num_parents) {
1235 pr_debug("%s: clock %s is not a possible parent of clock %s\n",
1236 __func__, parent->name, clk->name);
1237 goto out;
1238 }
1239
1240 /* migrate prepare and enable */
1241 if (clk->prepare_count)
1242 __clk_prepare(parent);
1243
1244 /* FIXME replace with clk_is_enabled(clk) someday */
1245 spin_lock_irqsave(&enable_lock, flags);
1246 if (clk->enable_count)
1247 __clk_enable(parent);
1248 spin_unlock_irqrestore(&enable_lock, flags);
1249
1250 /* change clock input source */
1251 ret = clk->ops->set_parent(clk->hw, i);
1252
1253 /* clean up old prepare and enable */
1254 spin_lock_irqsave(&enable_lock, flags);
1255 if (clk->enable_count)
1256 __clk_disable(old_parent);
1257 spin_unlock_irqrestore(&enable_lock, flags);
1258
1259 if (clk->prepare_count)
1260 __clk_unprepare(old_parent);
1261
1262out:
1263 return ret;
1264}
1265
1266/**
1267 * clk_set_parent - switch the parent of a mux clk
1268 * @clk: the mux clk whose input we are switching
1269 * @parent: the new input to clk
1270 *
1271 * Re-parent clk to use parent as it's new input source. If clk has the
1272 * CLK_SET_PARENT_GATE flag set then clk must be gated for this
1273 * operation to succeed. After successfully changing clk's parent
1274 * clk_set_parent will update the clk topology, sysfs topology and
1275 * propagate rate recalculation via __clk_recalc_rates. Returns 0 on
1276 * success, -EERROR otherwise.
1277 */
1278int clk_set_parent(struct clk *clk, struct clk *parent)
1279{
1280 int ret = 0;
1281
1282 if (!clk || !clk->ops)
1283 return -EINVAL;
1284
1285 if (!clk->ops->set_parent)
1286 return -ENOSYS;
1287
1288 /* prevent racing with updates to the clock topology */
1289 mutex_lock(&prepare_lock);
1290
1291 if (clk->parent == parent)
1292 goto out;
1293
1294 /* propagate PRE_RATE_CHANGE notifications */
1295 if (clk->notifier_count)
1296 ret = __clk_speculate_rates(clk, parent->rate);
1297
1298 /* abort if a driver objects */
1299 if (ret == NOTIFY_STOP)
1300 goto out;
1301
1302 /* only re-parent if the clock is not in use */
1303 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count)
1304 ret = -EBUSY;
1305 else
1306 ret = __clk_set_parent(clk, parent);
1307
1308 /* propagate ABORT_RATE_CHANGE if .set_parent failed */
1309 if (ret) {
1310 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
1311 goto out;
1312 }
1313
1314 /* propagate rate recalculation downstream */
1315 __clk_reparent(clk, parent);
1316
1317out:
1318 mutex_unlock(&prepare_lock);
1319
1320 return ret;
1321}
1322EXPORT_SYMBOL_GPL(clk_set_parent);
1323
1324/**
1325 * __clk_init - initialize the data structures in a struct clk
1326 * @dev: device initializing this clk, placeholder for now
1327 * @clk: clk being initialized
1328 *
1329 * Initializes the lists in struct clk, queries the hardware for the
1330 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07001331 */
Mike Turquetted1302a32012-03-29 14:30:40 -07001332int __clk_init(struct device *dev, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001333{
Mike Turquetted1302a32012-03-29 14:30:40 -07001334 int i, ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001335 struct clk *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001336 struct hlist_node *tmp2;
Mike Turquetteb24764902012-03-15 23:11:19 -07001337
1338 if (!clk)
Mike Turquetted1302a32012-03-29 14:30:40 -07001339 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001340
1341 mutex_lock(&prepare_lock);
1342
1343 /* check to see if a clock with this name is already registered */
Mike Turquetted1302a32012-03-29 14:30:40 -07001344 if (__clk_lookup(clk->name)) {
1345 pr_debug("%s: clk %s already initialized\n",
1346 __func__, clk->name);
1347 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07001348 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07001349 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001350
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001351 /* check that clk_ops are sane. See Documentation/clk.txt */
1352 if (clk->ops->set_rate &&
1353 !(clk->ops->round_rate && clk->ops->recalc_rate)) {
1354 pr_warning("%s: %s must implement .round_rate & .recalc_rate\n",
1355 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001356 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001357 goto out;
1358 }
1359
1360 if (clk->ops->set_parent && !clk->ops->get_parent) {
1361 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
1362 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001363 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001364 goto out;
1365 }
1366
Mike Turquetteb24764902012-03-15 23:11:19 -07001367 /* throw a WARN if any entries in parent_names are NULL */
1368 for (i = 0; i < clk->num_parents; i++)
1369 WARN(!clk->parent_names[i],
1370 "%s: invalid NULL in %s's .parent_names\n",
1371 __func__, clk->name);
1372
1373 /*
1374 * Allocate an array of struct clk *'s to avoid unnecessary string
1375 * look-ups of clk's possible parents. This can fail for clocks passed
1376 * in to clk_init during early boot; thus any access to clk->parents[]
1377 * must always check for a NULL pointer and try to populate it if
1378 * necessary.
1379 *
1380 * If clk->parents is not NULL we skip this entire block. This allows
1381 * for clock drivers to statically initialize clk->parents.
1382 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05301383 if (clk->num_parents > 1 && !clk->parents) {
1384 clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
Mike Turquetteb24764902012-03-15 23:11:19 -07001385 GFP_KERNEL);
1386 /*
1387 * __clk_lookup returns NULL for parents that have not been
1388 * clk_init'd; thus any access to clk->parents[] must check
1389 * for a NULL pointer. We can always perform lazy lookups for
1390 * missing parents later on.
1391 */
1392 if (clk->parents)
1393 for (i = 0; i < clk->num_parents; i++)
1394 clk->parents[i] =
1395 __clk_lookup(clk->parent_names[i]);
1396 }
1397
1398 clk->parent = __clk_init_parent(clk);
1399
1400 /*
1401 * Populate clk->parent if parent has already been __clk_init'd. If
1402 * parent has not yet been __clk_init'd then place clk in the orphan
1403 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
1404 * clk list.
1405 *
1406 * Every time a new clk is clk_init'd then we walk the list of orphan
1407 * clocks and re-parent any that are children of the clock currently
1408 * being clk_init'd.
1409 */
1410 if (clk->parent)
1411 hlist_add_head(&clk->child_node,
1412 &clk->parent->children);
1413 else if (clk->flags & CLK_IS_ROOT)
1414 hlist_add_head(&clk->child_node, &clk_root_list);
1415 else
1416 hlist_add_head(&clk->child_node, &clk_orphan_list);
1417
1418 /*
1419 * Set clk's rate. The preferred method is to use .recalc_rate. For
1420 * simple clocks and lazy developers the default fallback is to use the
1421 * parent's rate. If a clock doesn't have a parent (or is orphaned)
1422 * then rate is set to zero.
1423 */
1424 if (clk->ops->recalc_rate)
1425 clk->rate = clk->ops->recalc_rate(clk->hw,
1426 __clk_get_rate(clk->parent));
1427 else if (clk->parent)
1428 clk->rate = clk->parent->rate;
1429 else
1430 clk->rate = 0;
1431
1432 /*
1433 * walk the list of orphan clocks and reparent any that are children of
1434 * this clock
1435 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001436 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001437 if (orphan->ops->get_parent) {
1438 i = orphan->ops->get_parent(orphan->hw);
1439 if (!strcmp(clk->name, orphan->parent_names[i]))
1440 __clk_reparent(orphan, clk);
1441 continue;
1442 }
1443
Mike Turquetteb24764902012-03-15 23:11:19 -07001444 for (i = 0; i < orphan->num_parents; i++)
1445 if (!strcmp(clk->name, orphan->parent_names[i])) {
1446 __clk_reparent(orphan, clk);
1447 break;
1448 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001449 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001450
1451 /*
1452 * optional platform-specific magic
1453 *
1454 * The .init callback is not used by any of the basic clock types, but
1455 * exists for weird hardware that must perform initialization magic.
1456 * Please consider other ways of solving initialization problems before
1457 * using this callback, as it's use is discouraged.
1458 */
1459 if (clk->ops->init)
1460 clk->ops->init(clk->hw);
1461
1462 clk_debug_register(clk);
1463
1464out:
1465 mutex_unlock(&prepare_lock);
1466
Mike Turquetted1302a32012-03-29 14:30:40 -07001467 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001468}
1469
1470/**
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001471 * __clk_register - register a clock and return a cookie.
1472 *
1473 * Same as clk_register, except that the .clk field inside hw shall point to a
1474 * preallocated (generally statically allocated) struct clk. None of the fields
1475 * of the struct clk need to be initialized.
1476 *
1477 * The data pointed to by .init and .clk field shall NOT be marked as init
1478 * data.
1479 *
1480 * __clk_register is only exposed via clk-private.h and is intended for use with
1481 * very large numbers of clocks that need to be statically initialized. It is
1482 * a layering violation to include clk-private.h from any code which implements
1483 * a clock's .ops; as such any statically initialized clock data MUST be in a
1484 * separate C file from the logic that implements it's operations. Returns 0
1485 * on success, otherwise an error code.
1486 */
1487struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
1488{
1489 int ret;
1490 struct clk *clk;
1491
1492 clk = hw->clk;
1493 clk->name = hw->init->name;
1494 clk->ops = hw->init->ops;
1495 clk->hw = hw;
1496 clk->flags = hw->init->flags;
1497 clk->parent_names = hw->init->parent_names;
1498 clk->num_parents = hw->init->num_parents;
1499
1500 ret = __clk_init(dev, clk);
1501 if (ret)
1502 return ERR_PTR(ret);
1503
1504 return clk;
1505}
1506EXPORT_SYMBOL_GPL(__clk_register);
1507
Stephen Boyd46c87732012-09-24 13:38:04 -07001508static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001509{
Mike Turquetted1302a32012-03-29 14:30:40 -07001510 int i, ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001511
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001512 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
1513 if (!clk->name) {
1514 pr_err("%s: could not allocate clk->name\n", __func__);
1515 ret = -ENOMEM;
1516 goto fail_name;
1517 }
1518 clk->ops = hw->init->ops;
Mike Turquetteb24764902012-03-15 23:11:19 -07001519 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001520 clk->flags = hw->init->flags;
1521 clk->num_parents = hw->init->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -07001522 hw->clk = clk;
1523
Mike Turquetted1302a32012-03-29 14:30:40 -07001524 /* allocate local copy in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001525 clk->parent_names = kzalloc((sizeof(char*) * clk->num_parents),
Mike Turquetted1302a32012-03-29 14:30:40 -07001526 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001527
Mike Turquetted1302a32012-03-29 14:30:40 -07001528 if (!clk->parent_names) {
1529 pr_err("%s: could not allocate clk->parent_names\n", __func__);
1530 ret = -ENOMEM;
1531 goto fail_parent_names;
1532 }
1533
1534
1535 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001536 for (i = 0; i < clk->num_parents; i++) {
1537 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
1538 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07001539 if (!clk->parent_names[i]) {
1540 pr_err("%s: could not copy parent_names\n", __func__);
1541 ret = -ENOMEM;
1542 goto fail_parent_names_copy;
1543 }
1544 }
1545
1546 ret = __clk_init(dev, clk);
1547 if (!ret)
Stephen Boyd46c87732012-09-24 13:38:04 -07001548 return 0;
Mike Turquetted1302a32012-03-29 14:30:40 -07001549
1550fail_parent_names_copy:
1551 while (--i >= 0)
1552 kfree(clk->parent_names[i]);
1553 kfree(clk->parent_names);
1554fail_parent_names:
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001555 kfree(clk->name);
1556fail_name:
Stephen Boyd46c87732012-09-24 13:38:04 -07001557 return ret;
1558}
1559
1560/**
1561 * clk_register - allocate a new clock, register it and return an opaque cookie
1562 * @dev: device that is registering this clock
1563 * @hw: link to hardware-specific clock data
1564 *
1565 * clk_register is the primary interface for populating the clock tree with new
1566 * clock nodes. It returns a pointer to the newly allocated struct clk which
1567 * cannot be dereferenced by driver code but may be used in conjuction with the
1568 * rest of the clock API. In the event of an error clk_register will return an
1569 * error code; drivers must test for an error code after calling clk_register.
1570 */
1571struct clk *clk_register(struct device *dev, struct clk_hw *hw)
1572{
1573 int ret;
1574 struct clk *clk;
1575
1576 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
1577 if (!clk) {
1578 pr_err("%s: could not allocate clk\n", __func__);
1579 ret = -ENOMEM;
1580 goto fail_out;
1581 }
1582
1583 ret = _clk_register(dev, hw, clk);
1584 if (!ret)
1585 return clk;
1586
Mike Turquetted1302a32012-03-29 14:30:40 -07001587 kfree(clk);
1588fail_out:
1589 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001590}
1591EXPORT_SYMBOL_GPL(clk_register);
1592
Mark Brown1df5c932012-04-18 09:07:12 +01001593/**
1594 * clk_unregister - unregister a currently registered clock
1595 * @clk: clock to unregister
1596 *
1597 * Currently unimplemented.
1598 */
1599void clk_unregister(struct clk *clk) {}
1600EXPORT_SYMBOL_GPL(clk_unregister);
1601
Stephen Boyd46c87732012-09-24 13:38:04 -07001602static void devm_clk_release(struct device *dev, void *res)
1603{
1604 clk_unregister(res);
1605}
1606
1607/**
1608 * devm_clk_register - resource managed clk_register()
1609 * @dev: device that is registering this clock
1610 * @hw: link to hardware-specific clock data
1611 *
1612 * Managed clk_register(). Clocks returned from this function are
1613 * automatically clk_unregister()ed on driver detach. See clk_register() for
1614 * more information.
1615 */
1616struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
1617{
1618 struct clk *clk;
1619 int ret;
1620
1621 clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL);
1622 if (!clk)
1623 return ERR_PTR(-ENOMEM);
1624
1625 ret = _clk_register(dev, hw, clk);
1626 if (!ret) {
1627 devres_add(dev, clk);
1628 } else {
1629 devres_free(clk);
1630 clk = ERR_PTR(ret);
1631 }
1632
1633 return clk;
1634}
1635EXPORT_SYMBOL_GPL(devm_clk_register);
1636
1637static int devm_clk_match(struct device *dev, void *res, void *data)
1638{
1639 struct clk *c = res;
1640 if (WARN_ON(!c))
1641 return 0;
1642 return c == data;
1643}
1644
1645/**
1646 * devm_clk_unregister - resource managed clk_unregister()
1647 * @clk: clock to unregister
1648 *
1649 * Deallocate a clock allocated with devm_clk_register(). Normally
1650 * this function will not need to be called and the resource management
1651 * code will ensure that the resource is freed.
1652 */
1653void devm_clk_unregister(struct device *dev, struct clk *clk)
1654{
1655 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
1656}
1657EXPORT_SYMBOL_GPL(devm_clk_unregister);
1658
Mike Turquetteb24764902012-03-15 23:11:19 -07001659/*** clk rate change notifiers ***/
1660
1661/**
1662 * clk_notifier_register - add a clk rate change notifier
1663 * @clk: struct clk * to watch
1664 * @nb: struct notifier_block * with callback info
1665 *
1666 * Request notification when clk's rate changes. This uses an SRCU
1667 * notifier because we want it to block and notifier unregistrations are
1668 * uncommon. The callbacks associated with the notifier must not
1669 * re-enter into the clk framework by calling any top-level clk APIs;
1670 * this will cause a nested prepare_lock mutex.
1671 *
1672 * Pre-change notifier callbacks will be passed the current, pre-change
1673 * rate of the clk via struct clk_notifier_data.old_rate. The new,
1674 * post-change rate of the clk is passed via struct
1675 * clk_notifier_data.new_rate.
1676 *
1677 * Post-change notifiers will pass the now-current, post-change rate of
1678 * the clk in both struct clk_notifier_data.old_rate and struct
1679 * clk_notifier_data.new_rate.
1680 *
1681 * Abort-change notifiers are effectively the opposite of pre-change
1682 * notifiers: the original pre-change clk rate is passed in via struct
1683 * clk_notifier_data.new_rate and the failed post-change rate is passed
1684 * in via struct clk_notifier_data.old_rate.
1685 *
1686 * clk_notifier_register() must be called from non-atomic context.
1687 * Returns -EINVAL if called with null arguments, -ENOMEM upon
1688 * allocation failure; otherwise, passes along the return value of
1689 * srcu_notifier_chain_register().
1690 */
1691int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
1692{
1693 struct clk_notifier *cn;
1694 int ret = -ENOMEM;
1695
1696 if (!clk || !nb)
1697 return -EINVAL;
1698
1699 mutex_lock(&prepare_lock);
1700
1701 /* search the list of notifiers for this clk */
1702 list_for_each_entry(cn, &clk_notifier_list, node)
1703 if (cn->clk == clk)
1704 break;
1705
1706 /* if clk wasn't in the notifier list, allocate new clk_notifier */
1707 if (cn->clk != clk) {
1708 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
1709 if (!cn)
1710 goto out;
1711
1712 cn->clk = clk;
1713 srcu_init_notifier_head(&cn->notifier_head);
1714
1715 list_add(&cn->node, &clk_notifier_list);
1716 }
1717
1718 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
1719
1720 clk->notifier_count++;
1721
1722out:
1723 mutex_unlock(&prepare_lock);
1724
1725 return ret;
1726}
1727EXPORT_SYMBOL_GPL(clk_notifier_register);
1728
1729/**
1730 * clk_notifier_unregister - remove a clk rate change notifier
1731 * @clk: struct clk *
1732 * @nb: struct notifier_block * with callback info
1733 *
1734 * Request no further notification for changes to 'clk' and frees memory
1735 * allocated in clk_notifier_register.
1736 *
1737 * Returns -EINVAL if called with null arguments; otherwise, passes
1738 * along the return value of srcu_notifier_chain_unregister().
1739 */
1740int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
1741{
1742 struct clk_notifier *cn = NULL;
1743 int ret = -EINVAL;
1744
1745 if (!clk || !nb)
1746 return -EINVAL;
1747
1748 mutex_lock(&prepare_lock);
1749
1750 list_for_each_entry(cn, &clk_notifier_list, node)
1751 if (cn->clk == clk)
1752 break;
1753
1754 if (cn->clk == clk) {
1755 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
1756
1757 clk->notifier_count--;
1758
1759 /* XXX the notifier code should handle this better */
1760 if (!cn->notifier_head.head) {
1761 srcu_cleanup_notifier_head(&cn->notifier_head);
1762 kfree(cn);
1763 }
1764
1765 } else {
1766 ret = -ENOENT;
1767 }
1768
1769 mutex_unlock(&prepare_lock);
1770
1771 return ret;
1772}
1773EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05001774
1775#ifdef CONFIG_OF
1776/**
1777 * struct of_clk_provider - Clock provider registration structure
1778 * @link: Entry in global list of clock providers
1779 * @node: Pointer to device tree node of clock provider
1780 * @get: Get clock callback. Returns NULL or a struct clk for the
1781 * given clock specifier
1782 * @data: context pointer to be passed into @get callback
1783 */
1784struct of_clk_provider {
1785 struct list_head link;
1786
1787 struct device_node *node;
1788 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
1789 void *data;
1790};
1791
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05301792extern struct of_device_id __clk_of_table[];
1793
1794static const struct of_device_id __clk_of_table_sentinel
1795 __used __section(__clk_of_table_end);
1796
Grant Likely766e6a42012-04-09 14:50:06 -05001797static LIST_HEAD(of_clk_providers);
1798static DEFINE_MUTEX(of_clk_lock);
1799
1800struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
1801 void *data)
1802{
1803 return data;
1804}
1805EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
1806
Shawn Guo494bfec2012-08-22 21:36:27 +08001807struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
1808{
1809 struct clk_onecell_data *clk_data = data;
1810 unsigned int idx = clkspec->args[0];
1811
1812 if (idx >= clk_data->clk_num) {
1813 pr_err("%s: invalid clock index %d\n", __func__, idx);
1814 return ERR_PTR(-EINVAL);
1815 }
1816
1817 return clk_data->clks[idx];
1818}
1819EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
1820
Grant Likely766e6a42012-04-09 14:50:06 -05001821/**
1822 * of_clk_add_provider() - Register a clock provider for a node
1823 * @np: Device node pointer associated with clock provider
1824 * @clk_src_get: callback for decoding clock
1825 * @data: context pointer for @clk_src_get callback.
1826 */
1827int of_clk_add_provider(struct device_node *np,
1828 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
1829 void *data),
1830 void *data)
1831{
1832 struct of_clk_provider *cp;
1833
1834 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
1835 if (!cp)
1836 return -ENOMEM;
1837
1838 cp->node = of_node_get(np);
1839 cp->data = data;
1840 cp->get = clk_src_get;
1841
1842 mutex_lock(&of_clk_lock);
1843 list_add(&cp->link, &of_clk_providers);
1844 mutex_unlock(&of_clk_lock);
1845 pr_debug("Added clock from %s\n", np->full_name);
1846
1847 return 0;
1848}
1849EXPORT_SYMBOL_GPL(of_clk_add_provider);
1850
1851/**
1852 * of_clk_del_provider() - Remove a previously registered clock provider
1853 * @np: Device node pointer associated with clock provider
1854 */
1855void of_clk_del_provider(struct device_node *np)
1856{
1857 struct of_clk_provider *cp;
1858
1859 mutex_lock(&of_clk_lock);
1860 list_for_each_entry(cp, &of_clk_providers, link) {
1861 if (cp->node == np) {
1862 list_del(&cp->link);
1863 of_node_put(cp->node);
1864 kfree(cp);
1865 break;
1866 }
1867 }
1868 mutex_unlock(&of_clk_lock);
1869}
1870EXPORT_SYMBOL_GPL(of_clk_del_provider);
1871
1872struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
1873{
1874 struct of_clk_provider *provider;
1875 struct clk *clk = ERR_PTR(-ENOENT);
1876
1877 /* Check if we have such a provider in our array */
1878 mutex_lock(&of_clk_lock);
1879 list_for_each_entry(provider, &of_clk_providers, link) {
1880 if (provider->node == clkspec->np)
1881 clk = provider->get(clkspec, provider->data);
1882 if (!IS_ERR(clk))
1883 break;
1884 }
1885 mutex_unlock(&of_clk_lock);
1886
1887 return clk;
1888}
1889
1890const char *of_clk_get_parent_name(struct device_node *np, int index)
1891{
1892 struct of_phandle_args clkspec;
1893 const char *clk_name;
1894 int rc;
1895
1896 if (index < 0)
1897 return NULL;
1898
1899 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
1900 &clkspec);
1901 if (rc)
1902 return NULL;
1903
1904 if (of_property_read_string_index(clkspec.np, "clock-output-names",
1905 clkspec.args_count ? clkspec.args[0] : 0,
1906 &clk_name) < 0)
1907 clk_name = clkspec.np->name;
1908
1909 of_node_put(clkspec.np);
1910 return clk_name;
1911}
1912EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
1913
1914/**
1915 * of_clk_init() - Scan and init clock providers from the DT
1916 * @matches: array of compatible values and init functions for providers.
1917 *
1918 * This function scans the device tree for matching clock providers and
1919 * calls their initialization functions
1920 */
1921void __init of_clk_init(const struct of_device_id *matches)
1922{
1923 struct device_node *np;
1924
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05301925 if (!matches)
1926 matches = __clk_of_table;
1927
Grant Likely766e6a42012-04-09 14:50:06 -05001928 for_each_matching_node(np, matches) {
1929 const struct of_device_id *match = of_match_node(matches, np);
1930 of_clk_init_cb_t clk_init_cb = match->data;
1931 clk_init_cb(np);
1932 }
1933}
1934#endif