blob: 7571b5054f3cebce0375d1451701e96dea551d75 [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
Ulf Hansson3d6ee282013-03-12 20:26:02 +0100454bool __clk_is_prepared(struct clk *clk)
455{
456 int ret;
457
458 if (!clk)
459 return false;
460
461 /*
462 * .is_prepared is optional for clocks that can prepare
463 * fall back to software usage counter if it is missing
464 */
465 if (!clk->ops->is_prepared) {
466 ret = clk->prepare_count ? 1 : 0;
467 goto out;
468 }
469
470 ret = clk->ops->is_prepared(clk->hw);
471out:
472 return !!ret;
473}
474
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700475bool __clk_is_enabled(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700476{
477 int ret;
478
479 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700480 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700481
482 /*
483 * .is_enabled is only mandatory for clocks that gate
484 * fall back to software usage counter if .is_enabled is missing
485 */
486 if (!clk->ops->is_enabled) {
487 ret = clk->enable_count ? 1 : 0;
488 goto out;
489 }
490
491 ret = clk->ops->is_enabled(clk->hw);
492out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700493 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700494}
495
496static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
497{
498 struct clk *child;
499 struct clk *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700500
501 if (!strcmp(clk->name, name))
502 return clk;
503
Sasha Levinb67bfe02013-02-27 17:06:00 -0800504 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700505 ret = __clk_lookup_subtree(name, child);
506 if (ret)
507 return ret;
508 }
509
510 return NULL;
511}
512
513struct clk *__clk_lookup(const char *name)
514{
515 struct clk *root_clk;
516 struct clk *ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700517
518 if (!name)
519 return NULL;
520
521 /* search the 'proper' clk tree first */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800522 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700523 ret = __clk_lookup_subtree(name, root_clk);
524 if (ret)
525 return ret;
526 }
527
528 /* if not found, then search the orphan tree */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800529 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700530 ret = __clk_lookup_subtree(name, root_clk);
531 if (ret)
532 return ret;
533 }
534
535 return NULL;
536}
537
538/*** clk api ***/
539
540void __clk_unprepare(struct clk *clk)
541{
542 if (!clk)
543 return;
544
545 if (WARN_ON(clk->prepare_count == 0))
546 return;
547
548 if (--clk->prepare_count > 0)
549 return;
550
551 WARN_ON(clk->enable_count > 0);
552
553 if (clk->ops->unprepare)
554 clk->ops->unprepare(clk->hw);
555
556 __clk_unprepare(clk->parent);
557}
558
559/**
560 * clk_unprepare - undo preparation of a clock source
561 * @clk: the clk being unprepare
562 *
563 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
564 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
565 * if the operation may sleep. One example is a clk which is accessed over
566 * I2c. In the complex case a clk gate operation may require a fast and a slow
567 * part. It is this reason that clk_unprepare and clk_disable are not mutually
568 * exclusive. In fact clk_disable must be called before clk_unprepare.
569 */
570void clk_unprepare(struct clk *clk)
571{
572 mutex_lock(&prepare_lock);
573 __clk_unprepare(clk);
574 mutex_unlock(&prepare_lock);
575}
576EXPORT_SYMBOL_GPL(clk_unprepare);
577
578int __clk_prepare(struct clk *clk)
579{
580 int ret = 0;
581
582 if (!clk)
583 return 0;
584
585 if (clk->prepare_count == 0) {
586 ret = __clk_prepare(clk->parent);
587 if (ret)
588 return ret;
589
590 if (clk->ops->prepare) {
591 ret = clk->ops->prepare(clk->hw);
592 if (ret) {
593 __clk_unprepare(clk->parent);
594 return ret;
595 }
596 }
597 }
598
599 clk->prepare_count++;
600
601 return 0;
602}
603
604/**
605 * clk_prepare - prepare a clock source
606 * @clk: the clk being prepared
607 *
608 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
609 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
610 * operation may sleep. One example is a clk which is accessed over I2c. In
611 * the complex case a clk ungate operation may require a fast and a slow part.
612 * It is this reason that clk_prepare and clk_enable are not mutually
613 * exclusive. In fact clk_prepare must be called before clk_enable.
614 * Returns 0 on success, -EERROR otherwise.
615 */
616int clk_prepare(struct clk *clk)
617{
618 int ret;
619
620 mutex_lock(&prepare_lock);
621 ret = __clk_prepare(clk);
622 mutex_unlock(&prepare_lock);
623
624 return ret;
625}
626EXPORT_SYMBOL_GPL(clk_prepare);
627
628static void __clk_disable(struct clk *clk)
629{
630 if (!clk)
631 return;
632
Fengguang Wue47c6a32012-07-30 14:39:54 -0700633 if (WARN_ON(IS_ERR(clk)))
634 return;
635
Mike Turquetteb24764902012-03-15 23:11:19 -0700636 if (WARN_ON(clk->enable_count == 0))
637 return;
638
639 if (--clk->enable_count > 0)
640 return;
641
642 if (clk->ops->disable)
643 clk->ops->disable(clk->hw);
644
645 __clk_disable(clk->parent);
646}
647
648/**
649 * clk_disable - gate a clock
650 * @clk: the clk being gated
651 *
652 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
653 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
654 * clk if the operation is fast and will never sleep. One example is a
655 * SoC-internal clk which is controlled via simple register writes. In the
656 * complex case a clk gate operation may require a fast and a slow part. It is
657 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
658 * In fact clk_disable must be called before clk_unprepare.
659 */
660void clk_disable(struct clk *clk)
661{
662 unsigned long flags;
663
664 spin_lock_irqsave(&enable_lock, flags);
665 __clk_disable(clk);
666 spin_unlock_irqrestore(&enable_lock, flags);
667}
668EXPORT_SYMBOL_GPL(clk_disable);
669
670static int __clk_enable(struct clk *clk)
671{
672 int ret = 0;
673
674 if (!clk)
675 return 0;
676
677 if (WARN_ON(clk->prepare_count == 0))
678 return -ESHUTDOWN;
679
680 if (clk->enable_count == 0) {
681 ret = __clk_enable(clk->parent);
682
683 if (ret)
684 return ret;
685
686 if (clk->ops->enable) {
687 ret = clk->ops->enable(clk->hw);
688 if (ret) {
689 __clk_disable(clk->parent);
690 return ret;
691 }
692 }
693 }
694
695 clk->enable_count++;
696 return 0;
697}
698
699/**
700 * clk_enable - ungate a clock
701 * @clk: the clk being ungated
702 *
703 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
704 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
705 * if the operation will never sleep. One example is a SoC-internal clk which
706 * is controlled via simple register writes. In the complex case a clk ungate
707 * operation may require a fast and a slow part. It is this reason that
708 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
709 * must be called before clk_enable. Returns 0 on success, -EERROR
710 * otherwise.
711 */
712int clk_enable(struct clk *clk)
713{
714 unsigned long flags;
715 int ret;
716
717 spin_lock_irqsave(&enable_lock, flags);
718 ret = __clk_enable(clk);
719 spin_unlock_irqrestore(&enable_lock, flags);
720
721 return ret;
722}
723EXPORT_SYMBOL_GPL(clk_enable);
724
725/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700726 * __clk_round_rate - round the given rate for a clk
727 * @clk: round the rate of this clock
728 *
729 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
730 */
731unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
732{
Shawn Guo81536e02012-04-12 20:50:17 +0800733 unsigned long parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700734
735 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700736 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700737
Shawn Guof4d8af22012-04-12 20:50:19 +0800738 if (!clk->ops->round_rate) {
739 if (clk->flags & CLK_SET_RATE_PARENT)
740 return __clk_round_rate(clk->parent, rate);
741 else
742 return clk->rate;
743 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700744
Shawn Guo81536e02012-04-12 20:50:17 +0800745 if (clk->parent)
746 parent_rate = clk->parent->rate;
747
748 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700749}
750
751/**
752 * clk_round_rate - round the given rate for a clk
753 * @clk: the clk for which we are rounding a rate
754 * @rate: the rate which is to be rounded
755 *
756 * Takes in a rate as input and rounds it to a rate that the clk can actually
757 * use which is then returned. If clk doesn't support round_rate operation
758 * then the parent rate is returned.
759 */
760long clk_round_rate(struct clk *clk, unsigned long rate)
761{
762 unsigned long ret;
763
764 mutex_lock(&prepare_lock);
765 ret = __clk_round_rate(clk, rate);
766 mutex_unlock(&prepare_lock);
767
768 return ret;
769}
770EXPORT_SYMBOL_GPL(clk_round_rate);
771
772/**
773 * __clk_notify - call clk notifier chain
774 * @clk: struct clk * that is changing rate
775 * @msg: clk notifier type (see include/linux/clk.h)
776 * @old_rate: old clk rate
777 * @new_rate: new clk rate
778 *
779 * Triggers a notifier call chain on the clk rate-change notification
780 * for 'clk'. Passes a pointer to the struct clk and the previous
781 * and current rates to the notifier callback. Intended to be called by
782 * internal clock code only. Returns NOTIFY_DONE from the last driver
783 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
784 * a driver returns that.
785 */
786static int __clk_notify(struct clk *clk, unsigned long msg,
787 unsigned long old_rate, unsigned long new_rate)
788{
789 struct clk_notifier *cn;
790 struct clk_notifier_data cnd;
791 int ret = NOTIFY_DONE;
792
793 cnd.clk = clk;
794 cnd.old_rate = old_rate;
795 cnd.new_rate = new_rate;
796
797 list_for_each_entry(cn, &clk_notifier_list, node) {
798 if (cn->clk == clk) {
799 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
800 &cnd);
801 break;
802 }
803 }
804
805 return ret;
806}
807
808/**
809 * __clk_recalc_rates
810 * @clk: first clk in the subtree
811 * @msg: notification type (see include/linux/clk.h)
812 *
813 * Walks the subtree of clks starting with clk and recalculates rates as it
814 * goes. Note that if a clk does not implement the .recalc_rate callback then
815 * it is assumed that the clock will take on the rate of it's parent.
816 *
817 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
818 * if necessary.
819 *
820 * Caller must hold prepare_lock.
821 */
822static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
823{
824 unsigned long old_rate;
825 unsigned long parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700826 struct clk *child;
827
828 old_rate = clk->rate;
829
830 if (clk->parent)
831 parent_rate = clk->parent->rate;
832
833 if (clk->ops->recalc_rate)
834 clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
835 else
836 clk->rate = parent_rate;
837
838 /*
839 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
840 * & ABORT_RATE_CHANGE notifiers
841 */
842 if (clk->notifier_count && msg)
843 __clk_notify(clk, msg, old_rate, clk->rate);
844
Sasha Levinb67bfe02013-02-27 17:06:00 -0800845 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -0700846 __clk_recalc_rates(child, msg);
847}
848
849/**
Ulf Hanssona093bde2012-08-31 14:21:28 +0200850 * clk_get_rate - return the rate of clk
851 * @clk: the clk whose rate is being returned
852 *
853 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
854 * is set, which means a recalc_rate will be issued.
855 * If clk is NULL then returns 0.
856 */
857unsigned long clk_get_rate(struct clk *clk)
858{
859 unsigned long rate;
860
861 mutex_lock(&prepare_lock);
862
863 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
864 __clk_recalc_rates(clk, 0);
865
866 rate = __clk_get_rate(clk);
867 mutex_unlock(&prepare_lock);
868
869 return rate;
870}
871EXPORT_SYMBOL_GPL(clk_get_rate);
872
873/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700874 * __clk_speculate_rates
875 * @clk: first clk in the subtree
876 * @parent_rate: the "future" rate of clk's parent
877 *
878 * Walks the subtree of clks starting with clk, speculating rates as it
879 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
880 *
881 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
882 * pre-rate change notifications and returns early if no clks in the
883 * subtree have subscribed to the notifications. Note that if a clk does not
884 * implement the .recalc_rate callback then it is assumed that the clock will
885 * take on the rate of it's parent.
886 *
887 * Caller must hold prepare_lock.
888 */
889static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
890{
Mike Turquetteb24764902012-03-15 23:11:19 -0700891 struct clk *child;
892 unsigned long new_rate;
893 int ret = NOTIFY_DONE;
894
895 if (clk->ops->recalc_rate)
896 new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
897 else
898 new_rate = parent_rate;
899
900 /* abort the rate change if a driver returns NOTIFY_BAD */
901 if (clk->notifier_count)
902 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
903
904 if (ret == NOTIFY_BAD)
905 goto out;
906
Sasha Levinb67bfe02013-02-27 17:06:00 -0800907 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700908 ret = __clk_speculate_rates(child, new_rate);
909 if (ret == NOTIFY_BAD)
910 break;
911 }
912
913out:
914 return ret;
915}
916
917static void clk_calc_subtree(struct clk *clk, unsigned long new_rate)
918{
919 struct clk *child;
Mike Turquetteb24764902012-03-15 23:11:19 -0700920
921 clk->new_rate = new_rate;
922
Sasha Levinb67bfe02013-02-27 17:06:00 -0800923 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700924 if (child->ops->recalc_rate)
925 child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
926 else
927 child->new_rate = new_rate;
928 clk_calc_subtree(child, child->new_rate);
929 }
930}
931
932/*
933 * calculate the new rates returning the topmost clock that has to be
934 * changed.
935 */
936static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
937{
938 struct clk *top = clk;
Shawn Guo81536e02012-04-12 20:50:17 +0800939 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700940 unsigned long new_rate;
941
Mike Turquette7452b212012-03-26 14:45:36 -0700942 /* sanity */
943 if (IS_ERR_OR_NULL(clk))
944 return NULL;
945
Mike Turquette63f5c3b2012-05-02 16:23:43 -0700946 /* save parent rate, if it exists */
947 if (clk->parent)
948 best_parent_rate = clk->parent->rate;
949
Mike Turquette7452b212012-03-26 14:45:36 -0700950 /* never propagate up to the parent */
951 if (!(clk->flags & CLK_SET_RATE_PARENT)) {
952 if (!clk->ops->round_rate) {
953 clk->new_rate = clk->rate;
954 return NULL;
Mike Turquette7452b212012-03-26 14:45:36 -0700955 }
Mike Turquette63f5c3b2012-05-02 16:23:43 -0700956 new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
957 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -0700958 }
959
960 /* need clk->parent from here on out */
961 if (!clk->parent) {
962 pr_debug("%s: %s has NULL parent\n", __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700963 return NULL;
964 }
965
Mike Turquette7452b212012-03-26 14:45:36 -0700966 if (!clk->ops->round_rate) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700967 top = clk_calc_new_rates(clk->parent, rate);
Viresh Kumar1b2f9902012-04-17 16:45:38 +0530968 new_rate = clk->parent->new_rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700969
970 goto out;
971 }
972
Mike Turquette7452b212012-03-26 14:45:36 -0700973 new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700974
975 if (best_parent_rate != clk->parent->rate) {
976 top = clk_calc_new_rates(clk->parent, best_parent_rate);
977
978 goto out;
979 }
980
981out:
982 clk_calc_subtree(clk, new_rate);
983
984 return top;
985}
986
987/*
988 * Notify about rate changes in a subtree. Always walk down the whole tree
989 * so that in case of an error we can walk down the whole tree again and
990 * abort the change.
991 */
992static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
993{
Mike Turquetteb24764902012-03-15 23:11:19 -0700994 struct clk *child, *fail_clk = NULL;
995 int ret = NOTIFY_DONE;
996
997 if (clk->rate == clk->new_rate)
998 return 0;
999
1000 if (clk->notifier_count) {
1001 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
1002 if (ret == NOTIFY_BAD)
1003 fail_clk = clk;
1004 }
1005
Sasha Levinb67bfe02013-02-27 17:06:00 -08001006 hlist_for_each_entry(child, &clk->children, child_node) {
Mike Turquetteb24764902012-03-15 23:11:19 -07001007 clk = clk_propagate_rate_change(child, event);
1008 if (clk)
1009 fail_clk = clk;
1010 }
1011
1012 return fail_clk;
1013}
1014
1015/*
1016 * walk down a subtree and set the new rates notifying the rate
1017 * change on the way
1018 */
1019static void clk_change_rate(struct clk *clk)
1020{
1021 struct clk *child;
1022 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001023 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001024
1025 old_rate = clk->rate;
1026
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001027 if (clk->parent)
1028 best_parent_rate = clk->parent->rate;
1029
Mike Turquetteb24764902012-03-15 23:11:19 -07001030 if (clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001031 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001032
1033 if (clk->ops->recalc_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001034 clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001035 else
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001036 clk->rate = best_parent_rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001037
1038 if (clk->notifier_count && old_rate != clk->rate)
1039 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1040
Sasha Levinb67bfe02013-02-27 17:06:00 -08001041 hlist_for_each_entry(child, &clk->children, child_node)
Mike Turquetteb24764902012-03-15 23:11:19 -07001042 clk_change_rate(child);
1043}
1044
1045/**
1046 * clk_set_rate - specify a new rate for clk
1047 * @clk: the clk whose rate is being changed
1048 * @rate: the new rate for clk
1049 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001050 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001051 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001052 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1053 * propagate up to clk's parent; whether or not this happens depends on the
1054 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1055 * after calling .round_rate then upstream parent propagation is ignored. If
1056 * *parent_rate comes back with a new rate for clk's parent then we propagate
1057 * up to clk's parent and set it's rate. Upward propagation will continue
1058 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1059 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001060 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001061 * Rate changes are accomplished via tree traversal that also recalculates the
1062 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001063 *
1064 * Returns 0 on success, -EERROR otherwise.
1065 */
1066int clk_set_rate(struct clk *clk, unsigned long rate)
1067{
1068 struct clk *top, *fail_clk;
1069 int ret = 0;
1070
1071 /* prevent racing with updates to the clock topology */
1072 mutex_lock(&prepare_lock);
1073
1074 /* bail early if nothing to do */
1075 if (rate == clk->rate)
1076 goto out;
1077
Saravana Kannan7e0fa1b2012-05-15 13:43:42 -07001078 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
Viresh Kumar0e1c0302012-04-11 16:03:42 +05301079 ret = -EBUSY;
1080 goto out;
1081 }
1082
Mike Turquetteb24764902012-03-15 23:11:19 -07001083 /* calculate new rates and get the topmost changed clock */
1084 top = clk_calc_new_rates(clk, rate);
1085 if (!top) {
1086 ret = -EINVAL;
1087 goto out;
1088 }
1089
1090 /* notify that we are about to change rates */
1091 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1092 if (fail_clk) {
1093 pr_warn("%s: failed to set %s rate\n", __func__,
1094 fail_clk->name);
1095 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1096 ret = -EBUSY;
1097 goto out;
1098 }
1099
1100 /* change the rates */
1101 clk_change_rate(top);
1102
Mike Turquetteb24764902012-03-15 23:11:19 -07001103out:
1104 mutex_unlock(&prepare_lock);
1105
1106 return ret;
1107}
1108EXPORT_SYMBOL_GPL(clk_set_rate);
1109
1110/**
1111 * clk_get_parent - return the parent of a clk
1112 * @clk: the clk whose parent gets returned
1113 *
1114 * Simply returns clk->parent. Returns NULL if clk is NULL.
1115 */
1116struct clk *clk_get_parent(struct clk *clk)
1117{
1118 struct clk *parent;
1119
1120 mutex_lock(&prepare_lock);
1121 parent = __clk_get_parent(clk);
1122 mutex_unlock(&prepare_lock);
1123
1124 return parent;
1125}
1126EXPORT_SYMBOL_GPL(clk_get_parent);
1127
1128/*
1129 * .get_parent is mandatory for clocks with multiple possible parents. It is
1130 * optional for single-parent clocks. Always call .get_parent if it is
1131 * available and WARN if it is missing for multi-parent clocks.
1132 *
1133 * For single-parent clocks without .get_parent, first check to see if the
1134 * .parents array exists, and if so use it to avoid an expensive tree
1135 * traversal. If .parents does not exist then walk the tree with __clk_lookup.
1136 */
1137static struct clk *__clk_init_parent(struct clk *clk)
1138{
1139 struct clk *ret = NULL;
1140 u8 index;
1141
1142 /* handle the trivial cases */
1143
1144 if (!clk->num_parents)
1145 goto out;
1146
1147 if (clk->num_parents == 1) {
1148 if (IS_ERR_OR_NULL(clk->parent))
1149 ret = clk->parent = __clk_lookup(clk->parent_names[0]);
1150 ret = clk->parent;
1151 goto out;
1152 }
1153
1154 if (!clk->ops->get_parent) {
1155 WARN(!clk->ops->get_parent,
1156 "%s: multi-parent clocks must implement .get_parent\n",
1157 __func__);
1158 goto out;
1159 };
1160
1161 /*
1162 * Do our best to cache parent clocks in clk->parents. This prevents
1163 * unnecessary and expensive calls to __clk_lookup. We don't set
1164 * clk->parent here; that is done by the calling function
1165 */
1166
1167 index = clk->ops->get_parent(clk->hw);
1168
1169 if (!clk->parents)
1170 clk->parents =
Rajendra Nayak79750592012-06-06 14:41:31 +05301171 kzalloc((sizeof(struct clk*) * clk->num_parents),
Mike Turquetteb24764902012-03-15 23:11:19 -07001172 GFP_KERNEL);
1173
1174 if (!clk->parents)
1175 ret = __clk_lookup(clk->parent_names[index]);
1176 else if (!clk->parents[index])
1177 ret = clk->parents[index] =
1178 __clk_lookup(clk->parent_names[index]);
1179 else
1180 ret = clk->parents[index];
1181
1182out:
1183 return ret;
1184}
1185
1186void __clk_reparent(struct clk *clk, struct clk *new_parent)
1187{
1188#ifdef CONFIG_COMMON_CLK_DEBUG
1189 struct dentry *d;
1190 struct dentry *new_parent_d;
1191#endif
1192
1193 if (!clk || !new_parent)
1194 return;
1195
1196 hlist_del(&clk->child_node);
1197
1198 if (new_parent)
1199 hlist_add_head(&clk->child_node, &new_parent->children);
1200 else
1201 hlist_add_head(&clk->child_node, &clk_orphan_list);
1202
1203#ifdef CONFIG_COMMON_CLK_DEBUG
1204 if (!inited)
1205 goto out;
1206
1207 if (new_parent)
1208 new_parent_d = new_parent->dentry;
1209 else
1210 new_parent_d = orphandir;
1211
1212 d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
1213 new_parent_d, clk->name);
1214 if (d)
1215 clk->dentry = d;
1216 else
1217 pr_debug("%s: failed to rename debugfs entry for %s\n",
1218 __func__, clk->name);
1219out:
1220#endif
1221
1222 clk->parent = new_parent;
1223
1224 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1225}
1226
1227static int __clk_set_parent(struct clk *clk, struct clk *parent)
1228{
1229 struct clk *old_parent;
1230 unsigned long flags;
1231 int ret = -EINVAL;
1232 u8 i;
1233
1234 old_parent = clk->parent;
1235
Rajendra Nayak863b1322012-07-03 12:11:41 +05301236 if (!clk->parents)
Rajendra Nayak79750592012-06-06 14:41:31 +05301237 clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
1238 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001239
1240 /*
Rajendra Nayak863b1322012-07-03 12:11:41 +05301241 * find index of new parent clock using cached parent ptrs,
1242 * or if not yet cached, use string name comparison and cache
1243 * them now to avoid future calls to __clk_lookup.
Mike Turquetteb24764902012-03-15 23:11:19 -07001244 */
Rajendra Nayak863b1322012-07-03 12:11:41 +05301245 for (i = 0; i < clk->num_parents; i++) {
1246 if (clk->parents && clk->parents[i] == parent)
1247 break;
1248 else if (!strcmp(clk->parent_names[i], parent->name)) {
1249 if (clk->parents)
1250 clk->parents[i] = __clk_lookup(parent->name);
1251 break;
1252 }
1253 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001254
1255 if (i == clk->num_parents) {
1256 pr_debug("%s: clock %s is not a possible parent of clock %s\n",
1257 __func__, parent->name, clk->name);
1258 goto out;
1259 }
1260
1261 /* migrate prepare and enable */
1262 if (clk->prepare_count)
1263 __clk_prepare(parent);
1264
1265 /* FIXME replace with clk_is_enabled(clk) someday */
1266 spin_lock_irqsave(&enable_lock, flags);
1267 if (clk->enable_count)
1268 __clk_enable(parent);
1269 spin_unlock_irqrestore(&enable_lock, flags);
1270
1271 /* change clock input source */
1272 ret = clk->ops->set_parent(clk->hw, i);
1273
1274 /* clean up old prepare and enable */
1275 spin_lock_irqsave(&enable_lock, flags);
1276 if (clk->enable_count)
1277 __clk_disable(old_parent);
1278 spin_unlock_irqrestore(&enable_lock, flags);
1279
1280 if (clk->prepare_count)
1281 __clk_unprepare(old_parent);
1282
1283out:
1284 return ret;
1285}
1286
1287/**
1288 * clk_set_parent - switch the parent of a mux clk
1289 * @clk: the mux clk whose input we are switching
1290 * @parent: the new input to clk
1291 *
1292 * Re-parent clk to use parent as it's new input source. If clk has the
1293 * CLK_SET_PARENT_GATE flag set then clk must be gated for this
1294 * operation to succeed. After successfully changing clk's parent
1295 * clk_set_parent will update the clk topology, sysfs topology and
1296 * propagate rate recalculation via __clk_recalc_rates. Returns 0 on
1297 * success, -EERROR otherwise.
1298 */
1299int clk_set_parent(struct clk *clk, struct clk *parent)
1300{
1301 int ret = 0;
1302
1303 if (!clk || !clk->ops)
1304 return -EINVAL;
1305
1306 if (!clk->ops->set_parent)
1307 return -ENOSYS;
1308
1309 /* prevent racing with updates to the clock topology */
1310 mutex_lock(&prepare_lock);
1311
1312 if (clk->parent == parent)
1313 goto out;
1314
1315 /* propagate PRE_RATE_CHANGE notifications */
1316 if (clk->notifier_count)
1317 ret = __clk_speculate_rates(clk, parent->rate);
1318
1319 /* abort if a driver objects */
1320 if (ret == NOTIFY_STOP)
1321 goto out;
1322
1323 /* only re-parent if the clock is not in use */
1324 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count)
1325 ret = -EBUSY;
1326 else
1327 ret = __clk_set_parent(clk, parent);
1328
1329 /* propagate ABORT_RATE_CHANGE if .set_parent failed */
1330 if (ret) {
1331 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
1332 goto out;
1333 }
1334
1335 /* propagate rate recalculation downstream */
1336 __clk_reparent(clk, parent);
1337
1338out:
1339 mutex_unlock(&prepare_lock);
1340
1341 return ret;
1342}
1343EXPORT_SYMBOL_GPL(clk_set_parent);
1344
1345/**
1346 * __clk_init - initialize the data structures in a struct clk
1347 * @dev: device initializing this clk, placeholder for now
1348 * @clk: clk being initialized
1349 *
1350 * Initializes the lists in struct clk, queries the hardware for the
1351 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07001352 */
Mike Turquetted1302a32012-03-29 14:30:40 -07001353int __clk_init(struct device *dev, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001354{
Mike Turquetted1302a32012-03-29 14:30:40 -07001355 int i, ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001356 struct clk *orphan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001357 struct hlist_node *tmp2;
Mike Turquetteb24764902012-03-15 23:11:19 -07001358
1359 if (!clk)
Mike Turquetted1302a32012-03-29 14:30:40 -07001360 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001361
1362 mutex_lock(&prepare_lock);
1363
1364 /* check to see if a clock with this name is already registered */
Mike Turquetted1302a32012-03-29 14:30:40 -07001365 if (__clk_lookup(clk->name)) {
1366 pr_debug("%s: clk %s already initialized\n",
1367 __func__, clk->name);
1368 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07001369 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07001370 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001371
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001372 /* check that clk_ops are sane. See Documentation/clk.txt */
1373 if (clk->ops->set_rate &&
1374 !(clk->ops->round_rate && clk->ops->recalc_rate)) {
1375 pr_warning("%s: %s must implement .round_rate & .recalc_rate\n",
1376 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001377 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001378 goto out;
1379 }
1380
1381 if (clk->ops->set_parent && !clk->ops->get_parent) {
1382 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
1383 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001384 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001385 goto out;
1386 }
1387
Mike Turquetteb24764902012-03-15 23:11:19 -07001388 /* throw a WARN if any entries in parent_names are NULL */
1389 for (i = 0; i < clk->num_parents; i++)
1390 WARN(!clk->parent_names[i],
1391 "%s: invalid NULL in %s's .parent_names\n",
1392 __func__, clk->name);
1393
1394 /*
1395 * Allocate an array of struct clk *'s to avoid unnecessary string
1396 * look-ups of clk's possible parents. This can fail for clocks passed
1397 * in to clk_init during early boot; thus any access to clk->parents[]
1398 * must always check for a NULL pointer and try to populate it if
1399 * necessary.
1400 *
1401 * If clk->parents is not NULL we skip this entire block. This allows
1402 * for clock drivers to statically initialize clk->parents.
1403 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05301404 if (clk->num_parents > 1 && !clk->parents) {
1405 clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
Mike Turquetteb24764902012-03-15 23:11:19 -07001406 GFP_KERNEL);
1407 /*
1408 * __clk_lookup returns NULL for parents that have not been
1409 * clk_init'd; thus any access to clk->parents[] must check
1410 * for a NULL pointer. We can always perform lazy lookups for
1411 * missing parents later on.
1412 */
1413 if (clk->parents)
1414 for (i = 0; i < clk->num_parents; i++)
1415 clk->parents[i] =
1416 __clk_lookup(clk->parent_names[i]);
1417 }
1418
1419 clk->parent = __clk_init_parent(clk);
1420
1421 /*
1422 * Populate clk->parent if parent has already been __clk_init'd. If
1423 * parent has not yet been __clk_init'd then place clk in the orphan
1424 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
1425 * clk list.
1426 *
1427 * Every time a new clk is clk_init'd then we walk the list of orphan
1428 * clocks and re-parent any that are children of the clock currently
1429 * being clk_init'd.
1430 */
1431 if (clk->parent)
1432 hlist_add_head(&clk->child_node,
1433 &clk->parent->children);
1434 else if (clk->flags & CLK_IS_ROOT)
1435 hlist_add_head(&clk->child_node, &clk_root_list);
1436 else
1437 hlist_add_head(&clk->child_node, &clk_orphan_list);
1438
1439 /*
1440 * Set clk's rate. The preferred method is to use .recalc_rate. For
1441 * simple clocks and lazy developers the default fallback is to use the
1442 * parent's rate. If a clock doesn't have a parent (or is orphaned)
1443 * then rate is set to zero.
1444 */
1445 if (clk->ops->recalc_rate)
1446 clk->rate = clk->ops->recalc_rate(clk->hw,
1447 __clk_get_rate(clk->parent));
1448 else if (clk->parent)
1449 clk->rate = clk->parent->rate;
1450 else
1451 clk->rate = 0;
1452
1453 /*
1454 * walk the list of orphan clocks and reparent any that are children of
1455 * this clock
1456 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001457 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001458 if (orphan->ops->get_parent) {
1459 i = orphan->ops->get_parent(orphan->hw);
1460 if (!strcmp(clk->name, orphan->parent_names[i]))
1461 __clk_reparent(orphan, clk);
1462 continue;
1463 }
1464
Mike Turquetteb24764902012-03-15 23:11:19 -07001465 for (i = 0; i < orphan->num_parents; i++)
1466 if (!strcmp(clk->name, orphan->parent_names[i])) {
1467 __clk_reparent(orphan, clk);
1468 break;
1469 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001470 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001471
1472 /*
1473 * optional platform-specific magic
1474 *
1475 * The .init callback is not used by any of the basic clock types, but
1476 * exists for weird hardware that must perform initialization magic.
1477 * Please consider other ways of solving initialization problems before
1478 * using this callback, as it's use is discouraged.
1479 */
1480 if (clk->ops->init)
1481 clk->ops->init(clk->hw);
1482
1483 clk_debug_register(clk);
1484
1485out:
1486 mutex_unlock(&prepare_lock);
1487
Mike Turquetted1302a32012-03-29 14:30:40 -07001488 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001489}
1490
1491/**
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001492 * __clk_register - register a clock and return a cookie.
1493 *
1494 * Same as clk_register, except that the .clk field inside hw shall point to a
1495 * preallocated (generally statically allocated) struct clk. None of the fields
1496 * of the struct clk need to be initialized.
1497 *
1498 * The data pointed to by .init and .clk field shall NOT be marked as init
1499 * data.
1500 *
1501 * __clk_register is only exposed via clk-private.h and is intended for use with
1502 * very large numbers of clocks that need to be statically initialized. It is
1503 * a layering violation to include clk-private.h from any code which implements
1504 * a clock's .ops; as such any statically initialized clock data MUST be in a
1505 * separate C file from the logic that implements it's operations. Returns 0
1506 * on success, otherwise an error code.
1507 */
1508struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
1509{
1510 int ret;
1511 struct clk *clk;
1512
1513 clk = hw->clk;
1514 clk->name = hw->init->name;
1515 clk->ops = hw->init->ops;
1516 clk->hw = hw;
1517 clk->flags = hw->init->flags;
1518 clk->parent_names = hw->init->parent_names;
1519 clk->num_parents = hw->init->num_parents;
1520
1521 ret = __clk_init(dev, clk);
1522 if (ret)
1523 return ERR_PTR(ret);
1524
1525 return clk;
1526}
1527EXPORT_SYMBOL_GPL(__clk_register);
1528
Stephen Boyd46c87732012-09-24 13:38:04 -07001529static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001530{
Mike Turquetted1302a32012-03-29 14:30:40 -07001531 int i, ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001532
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001533 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
1534 if (!clk->name) {
1535 pr_err("%s: could not allocate clk->name\n", __func__);
1536 ret = -ENOMEM;
1537 goto fail_name;
1538 }
1539 clk->ops = hw->init->ops;
Mike Turquetteb24764902012-03-15 23:11:19 -07001540 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001541 clk->flags = hw->init->flags;
1542 clk->num_parents = hw->init->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -07001543 hw->clk = clk;
1544
Mike Turquetted1302a32012-03-29 14:30:40 -07001545 /* allocate local copy in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001546 clk->parent_names = kzalloc((sizeof(char*) * clk->num_parents),
Mike Turquetted1302a32012-03-29 14:30:40 -07001547 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001548
Mike Turquetted1302a32012-03-29 14:30:40 -07001549 if (!clk->parent_names) {
1550 pr_err("%s: could not allocate clk->parent_names\n", __func__);
1551 ret = -ENOMEM;
1552 goto fail_parent_names;
1553 }
1554
1555
1556 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001557 for (i = 0; i < clk->num_parents; i++) {
1558 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
1559 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07001560 if (!clk->parent_names[i]) {
1561 pr_err("%s: could not copy parent_names\n", __func__);
1562 ret = -ENOMEM;
1563 goto fail_parent_names_copy;
1564 }
1565 }
1566
1567 ret = __clk_init(dev, clk);
1568 if (!ret)
Stephen Boyd46c87732012-09-24 13:38:04 -07001569 return 0;
Mike Turquetted1302a32012-03-29 14:30:40 -07001570
1571fail_parent_names_copy:
1572 while (--i >= 0)
1573 kfree(clk->parent_names[i]);
1574 kfree(clk->parent_names);
1575fail_parent_names:
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001576 kfree(clk->name);
1577fail_name:
Stephen Boyd46c87732012-09-24 13:38:04 -07001578 return ret;
1579}
1580
1581/**
1582 * clk_register - allocate a new clock, register it and return an opaque cookie
1583 * @dev: device that is registering this clock
1584 * @hw: link to hardware-specific clock data
1585 *
1586 * clk_register is the primary interface for populating the clock tree with new
1587 * clock nodes. It returns a pointer to the newly allocated struct clk which
1588 * cannot be dereferenced by driver code but may be used in conjuction with the
1589 * rest of the clock API. In the event of an error clk_register will return an
1590 * error code; drivers must test for an error code after calling clk_register.
1591 */
1592struct clk *clk_register(struct device *dev, struct clk_hw *hw)
1593{
1594 int ret;
1595 struct clk *clk;
1596
1597 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
1598 if (!clk) {
1599 pr_err("%s: could not allocate clk\n", __func__);
1600 ret = -ENOMEM;
1601 goto fail_out;
1602 }
1603
1604 ret = _clk_register(dev, hw, clk);
1605 if (!ret)
1606 return clk;
1607
Mike Turquetted1302a32012-03-29 14:30:40 -07001608 kfree(clk);
1609fail_out:
1610 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001611}
1612EXPORT_SYMBOL_GPL(clk_register);
1613
Mark Brown1df5c932012-04-18 09:07:12 +01001614/**
1615 * clk_unregister - unregister a currently registered clock
1616 * @clk: clock to unregister
1617 *
1618 * Currently unimplemented.
1619 */
1620void clk_unregister(struct clk *clk) {}
1621EXPORT_SYMBOL_GPL(clk_unregister);
1622
Stephen Boyd46c87732012-09-24 13:38:04 -07001623static void devm_clk_release(struct device *dev, void *res)
1624{
1625 clk_unregister(res);
1626}
1627
1628/**
1629 * devm_clk_register - resource managed clk_register()
1630 * @dev: device that is registering this clock
1631 * @hw: link to hardware-specific clock data
1632 *
1633 * Managed clk_register(). Clocks returned from this function are
1634 * automatically clk_unregister()ed on driver detach. See clk_register() for
1635 * more information.
1636 */
1637struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
1638{
1639 struct clk *clk;
1640 int ret;
1641
1642 clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL);
1643 if (!clk)
1644 return ERR_PTR(-ENOMEM);
1645
1646 ret = _clk_register(dev, hw, clk);
1647 if (!ret) {
1648 devres_add(dev, clk);
1649 } else {
1650 devres_free(clk);
1651 clk = ERR_PTR(ret);
1652 }
1653
1654 return clk;
1655}
1656EXPORT_SYMBOL_GPL(devm_clk_register);
1657
1658static int devm_clk_match(struct device *dev, void *res, void *data)
1659{
1660 struct clk *c = res;
1661 if (WARN_ON(!c))
1662 return 0;
1663 return c == data;
1664}
1665
1666/**
1667 * devm_clk_unregister - resource managed clk_unregister()
1668 * @clk: clock to unregister
1669 *
1670 * Deallocate a clock allocated with devm_clk_register(). Normally
1671 * this function will not need to be called and the resource management
1672 * code will ensure that the resource is freed.
1673 */
1674void devm_clk_unregister(struct device *dev, struct clk *clk)
1675{
1676 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
1677}
1678EXPORT_SYMBOL_GPL(devm_clk_unregister);
1679
Mike Turquetteb24764902012-03-15 23:11:19 -07001680/*** clk rate change notifiers ***/
1681
1682/**
1683 * clk_notifier_register - add a clk rate change notifier
1684 * @clk: struct clk * to watch
1685 * @nb: struct notifier_block * with callback info
1686 *
1687 * Request notification when clk's rate changes. This uses an SRCU
1688 * notifier because we want it to block and notifier unregistrations are
1689 * uncommon. The callbacks associated with the notifier must not
1690 * re-enter into the clk framework by calling any top-level clk APIs;
1691 * this will cause a nested prepare_lock mutex.
1692 *
1693 * Pre-change notifier callbacks will be passed the current, pre-change
1694 * rate of the clk via struct clk_notifier_data.old_rate. The new,
1695 * post-change rate of the clk is passed via struct
1696 * clk_notifier_data.new_rate.
1697 *
1698 * Post-change notifiers will pass the now-current, post-change rate of
1699 * the clk in both struct clk_notifier_data.old_rate and struct
1700 * clk_notifier_data.new_rate.
1701 *
1702 * Abort-change notifiers are effectively the opposite of pre-change
1703 * notifiers: the original pre-change clk rate is passed in via struct
1704 * clk_notifier_data.new_rate and the failed post-change rate is passed
1705 * in via struct clk_notifier_data.old_rate.
1706 *
1707 * clk_notifier_register() must be called from non-atomic context.
1708 * Returns -EINVAL if called with null arguments, -ENOMEM upon
1709 * allocation failure; otherwise, passes along the return value of
1710 * srcu_notifier_chain_register().
1711 */
1712int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
1713{
1714 struct clk_notifier *cn;
1715 int ret = -ENOMEM;
1716
1717 if (!clk || !nb)
1718 return -EINVAL;
1719
1720 mutex_lock(&prepare_lock);
1721
1722 /* search the list of notifiers for this clk */
1723 list_for_each_entry(cn, &clk_notifier_list, node)
1724 if (cn->clk == clk)
1725 break;
1726
1727 /* if clk wasn't in the notifier list, allocate new clk_notifier */
1728 if (cn->clk != clk) {
1729 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
1730 if (!cn)
1731 goto out;
1732
1733 cn->clk = clk;
1734 srcu_init_notifier_head(&cn->notifier_head);
1735
1736 list_add(&cn->node, &clk_notifier_list);
1737 }
1738
1739 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
1740
1741 clk->notifier_count++;
1742
1743out:
1744 mutex_unlock(&prepare_lock);
1745
1746 return ret;
1747}
1748EXPORT_SYMBOL_GPL(clk_notifier_register);
1749
1750/**
1751 * clk_notifier_unregister - remove a clk rate change notifier
1752 * @clk: struct clk *
1753 * @nb: struct notifier_block * with callback info
1754 *
1755 * Request no further notification for changes to 'clk' and frees memory
1756 * allocated in clk_notifier_register.
1757 *
1758 * Returns -EINVAL if called with null arguments; otherwise, passes
1759 * along the return value of srcu_notifier_chain_unregister().
1760 */
1761int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
1762{
1763 struct clk_notifier *cn = NULL;
1764 int ret = -EINVAL;
1765
1766 if (!clk || !nb)
1767 return -EINVAL;
1768
1769 mutex_lock(&prepare_lock);
1770
1771 list_for_each_entry(cn, &clk_notifier_list, node)
1772 if (cn->clk == clk)
1773 break;
1774
1775 if (cn->clk == clk) {
1776 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
1777
1778 clk->notifier_count--;
1779
1780 /* XXX the notifier code should handle this better */
1781 if (!cn->notifier_head.head) {
1782 srcu_cleanup_notifier_head(&cn->notifier_head);
1783 kfree(cn);
1784 }
1785
1786 } else {
1787 ret = -ENOENT;
1788 }
1789
1790 mutex_unlock(&prepare_lock);
1791
1792 return ret;
1793}
1794EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05001795
1796#ifdef CONFIG_OF
1797/**
1798 * struct of_clk_provider - Clock provider registration structure
1799 * @link: Entry in global list of clock providers
1800 * @node: Pointer to device tree node of clock provider
1801 * @get: Get clock callback. Returns NULL or a struct clk for the
1802 * given clock specifier
1803 * @data: context pointer to be passed into @get callback
1804 */
1805struct of_clk_provider {
1806 struct list_head link;
1807
1808 struct device_node *node;
1809 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
1810 void *data;
1811};
1812
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05301813extern struct of_device_id __clk_of_table[];
1814
1815static const struct of_device_id __clk_of_table_sentinel
1816 __used __section(__clk_of_table_end);
1817
Grant Likely766e6a42012-04-09 14:50:06 -05001818static LIST_HEAD(of_clk_providers);
1819static DEFINE_MUTEX(of_clk_lock);
1820
1821struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
1822 void *data)
1823{
1824 return data;
1825}
1826EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
1827
Shawn Guo494bfec2012-08-22 21:36:27 +08001828struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
1829{
1830 struct clk_onecell_data *clk_data = data;
1831 unsigned int idx = clkspec->args[0];
1832
1833 if (idx >= clk_data->clk_num) {
1834 pr_err("%s: invalid clock index %d\n", __func__, idx);
1835 return ERR_PTR(-EINVAL);
1836 }
1837
1838 return clk_data->clks[idx];
1839}
1840EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
1841
Grant Likely766e6a42012-04-09 14:50:06 -05001842/**
1843 * of_clk_add_provider() - Register a clock provider for a node
1844 * @np: Device node pointer associated with clock provider
1845 * @clk_src_get: callback for decoding clock
1846 * @data: context pointer for @clk_src_get callback.
1847 */
1848int of_clk_add_provider(struct device_node *np,
1849 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
1850 void *data),
1851 void *data)
1852{
1853 struct of_clk_provider *cp;
1854
1855 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
1856 if (!cp)
1857 return -ENOMEM;
1858
1859 cp->node = of_node_get(np);
1860 cp->data = data;
1861 cp->get = clk_src_get;
1862
1863 mutex_lock(&of_clk_lock);
1864 list_add(&cp->link, &of_clk_providers);
1865 mutex_unlock(&of_clk_lock);
1866 pr_debug("Added clock from %s\n", np->full_name);
1867
1868 return 0;
1869}
1870EXPORT_SYMBOL_GPL(of_clk_add_provider);
1871
1872/**
1873 * of_clk_del_provider() - Remove a previously registered clock provider
1874 * @np: Device node pointer associated with clock provider
1875 */
1876void of_clk_del_provider(struct device_node *np)
1877{
1878 struct of_clk_provider *cp;
1879
1880 mutex_lock(&of_clk_lock);
1881 list_for_each_entry(cp, &of_clk_providers, link) {
1882 if (cp->node == np) {
1883 list_del(&cp->link);
1884 of_node_put(cp->node);
1885 kfree(cp);
1886 break;
1887 }
1888 }
1889 mutex_unlock(&of_clk_lock);
1890}
1891EXPORT_SYMBOL_GPL(of_clk_del_provider);
1892
1893struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
1894{
1895 struct of_clk_provider *provider;
1896 struct clk *clk = ERR_PTR(-ENOENT);
1897
1898 /* Check if we have such a provider in our array */
1899 mutex_lock(&of_clk_lock);
1900 list_for_each_entry(provider, &of_clk_providers, link) {
1901 if (provider->node == clkspec->np)
1902 clk = provider->get(clkspec, provider->data);
1903 if (!IS_ERR(clk))
1904 break;
1905 }
1906 mutex_unlock(&of_clk_lock);
1907
1908 return clk;
1909}
1910
1911const char *of_clk_get_parent_name(struct device_node *np, int index)
1912{
1913 struct of_phandle_args clkspec;
1914 const char *clk_name;
1915 int rc;
1916
1917 if (index < 0)
1918 return NULL;
1919
1920 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
1921 &clkspec);
1922 if (rc)
1923 return NULL;
1924
1925 if (of_property_read_string_index(clkspec.np, "clock-output-names",
1926 clkspec.args_count ? clkspec.args[0] : 0,
1927 &clk_name) < 0)
1928 clk_name = clkspec.np->name;
1929
1930 of_node_put(clkspec.np);
1931 return clk_name;
1932}
1933EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
1934
1935/**
1936 * of_clk_init() - Scan and init clock providers from the DT
1937 * @matches: array of compatible values and init functions for providers.
1938 *
1939 * This function scans the device tree for matching clock providers and
1940 * calls their initialization functions
1941 */
1942void __init of_clk_init(const struct of_device_id *matches)
1943{
1944 struct device_node *np;
1945
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +05301946 if (!matches)
1947 matches = __clk_of_table;
1948
Grant Likely766e6a42012-04-09 14:50:06 -05001949 for_each_matching_node(np, matches) {
1950 const struct of_device_id *match = of_match_node(matches, np);
1951 of_clk_init_cb_t clk_init_cb = match->data;
1952 clk_init_cb(np);
1953 }
1954}
1955#endif