clk: Add common __clk_get(), __clk_put() implementations
[deliverable/linux.git] / drivers / clk / clk.c
CommitLineData
b2476490
MT
1/*
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>
766e6a4e 19#include <linux/of.h>
46c8773a 20#include <linux/device.h>
f2f6c255 21#include <linux/init.h>
533ddeb1 22#include <linux/sched.h>
b2476490 23
d6782c26
SN
24#include "clk.h"
25
b2476490
MT
26static DEFINE_SPINLOCK(enable_lock);
27static DEFINE_MUTEX(prepare_lock);
28
533ddeb1
MT
29static struct task_struct *prepare_owner;
30static struct task_struct *enable_owner;
31
32static int prepare_refcnt;
33static int enable_refcnt;
34
b2476490
MT
35static HLIST_HEAD(clk_root_list);
36static HLIST_HEAD(clk_orphan_list);
37static LIST_HEAD(clk_notifier_list);
38
eab89f69
MT
39/*** locking ***/
40static void clk_prepare_lock(void)
41{
533ddeb1
MT
42 if (!mutex_trylock(&prepare_lock)) {
43 if (prepare_owner == current) {
44 prepare_refcnt++;
45 return;
46 }
47 mutex_lock(&prepare_lock);
48 }
49 WARN_ON_ONCE(prepare_owner != NULL);
50 WARN_ON_ONCE(prepare_refcnt != 0);
51 prepare_owner = current;
52 prepare_refcnt = 1;
eab89f69
MT
53}
54
55static void clk_prepare_unlock(void)
56{
533ddeb1
MT
57 WARN_ON_ONCE(prepare_owner != current);
58 WARN_ON_ONCE(prepare_refcnt == 0);
59
60 if (--prepare_refcnt)
61 return;
62 prepare_owner = NULL;
eab89f69
MT
63 mutex_unlock(&prepare_lock);
64}
65
66static unsigned long clk_enable_lock(void)
67{
68 unsigned long flags;
533ddeb1
MT
69
70 if (!spin_trylock_irqsave(&enable_lock, flags)) {
71 if (enable_owner == current) {
72 enable_refcnt++;
73 return flags;
74 }
75 spin_lock_irqsave(&enable_lock, flags);
76 }
77 WARN_ON_ONCE(enable_owner != NULL);
78 WARN_ON_ONCE(enable_refcnt != 0);
79 enable_owner = current;
80 enable_refcnt = 1;
eab89f69
MT
81 return flags;
82}
83
84static void clk_enable_unlock(unsigned long flags)
85{
533ddeb1
MT
86 WARN_ON_ONCE(enable_owner != current);
87 WARN_ON_ONCE(enable_refcnt == 0);
88
89 if (--enable_refcnt)
90 return;
91 enable_owner = NULL;
eab89f69
MT
92 spin_unlock_irqrestore(&enable_lock, flags);
93}
94
b2476490
MT
95/*** debugfs support ***/
96
97#ifdef CONFIG_COMMON_CLK_DEBUG
98#include <linux/debugfs.h>
99
100static struct dentry *rootdir;
101static struct dentry *orphandir;
102static int inited = 0;
103
1af599df
PG
104static void clk_summary_show_one(struct seq_file *s, struct clk *c, int level)
105{
106 if (!c)
107 return;
108
109 seq_printf(s, "%*s%-*s %-11d %-12d %-10lu",
110 level * 3 + 1, "",
111 30 - level * 3, c->name,
670decdd 112 c->enable_count, c->prepare_count, clk_get_rate(c));
1af599df
PG
113 seq_printf(s, "\n");
114}
115
116static void clk_summary_show_subtree(struct seq_file *s, struct clk *c,
117 int level)
118{
119 struct clk *child;
1af599df
PG
120
121 if (!c)
122 return;
123
124 clk_summary_show_one(s, c, level);
125
b67bfe0d 126 hlist_for_each_entry(child, &c->children, child_node)
1af599df
PG
127 clk_summary_show_subtree(s, child, level + 1);
128}
129
130static int clk_summary_show(struct seq_file *s, void *data)
131{
132 struct clk *c;
1af599df
PG
133
134 seq_printf(s, " clock enable_cnt prepare_cnt rate\n");
135 seq_printf(s, "---------------------------------------------------------------------\n");
136
eab89f69 137 clk_prepare_lock();
1af599df 138
b67bfe0d 139 hlist_for_each_entry(c, &clk_root_list, child_node)
1af599df
PG
140 clk_summary_show_subtree(s, c, 0);
141
b67bfe0d 142 hlist_for_each_entry(c, &clk_orphan_list, child_node)
1af599df
PG
143 clk_summary_show_subtree(s, c, 0);
144
eab89f69 145 clk_prepare_unlock();
1af599df
PG
146
147 return 0;
148}
149
150
151static int clk_summary_open(struct inode *inode, struct file *file)
152{
153 return single_open(file, clk_summary_show, inode->i_private);
154}
155
156static const struct file_operations clk_summary_fops = {
157 .open = clk_summary_open,
158 .read = seq_read,
159 .llseek = seq_lseek,
160 .release = single_release,
161};
162
bddca894
PG
163static void clk_dump_one(struct seq_file *s, struct clk *c, int level)
164{
165 if (!c)
166 return;
167
168 seq_printf(s, "\"%s\": { ", c->name);
169 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
170 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
670decdd 171 seq_printf(s, "\"rate\": %lu", clk_get_rate(c));
bddca894
PG
172}
173
174static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level)
175{
176 struct clk *child;
bddca894
PG
177
178 if (!c)
179 return;
180
181 clk_dump_one(s, c, level);
182
b67bfe0d 183 hlist_for_each_entry(child, &c->children, child_node) {
bddca894
PG
184 seq_printf(s, ",");
185 clk_dump_subtree(s, child, level + 1);
186 }
187
188 seq_printf(s, "}");
189}
190
191static int clk_dump(struct seq_file *s, void *data)
192{
193 struct clk *c;
bddca894
PG
194 bool first_node = true;
195
196 seq_printf(s, "{");
197
eab89f69 198 clk_prepare_lock();
bddca894 199
b67bfe0d 200 hlist_for_each_entry(c, &clk_root_list, child_node) {
bddca894
PG
201 if (!first_node)
202 seq_printf(s, ",");
203 first_node = false;
204 clk_dump_subtree(s, c, 0);
205 }
206
b67bfe0d 207 hlist_for_each_entry(c, &clk_orphan_list, child_node) {
bddca894
PG
208 seq_printf(s, ",");
209 clk_dump_subtree(s, c, 0);
210 }
211
eab89f69 212 clk_prepare_unlock();
bddca894
PG
213
214 seq_printf(s, "}");
215 return 0;
216}
217
218
219static int clk_dump_open(struct inode *inode, struct file *file)
220{
221 return single_open(file, clk_dump, inode->i_private);
222}
223
224static const struct file_operations clk_dump_fops = {
225 .open = clk_dump_open,
226 .read = seq_read,
227 .llseek = seq_lseek,
228 .release = single_release,
229};
230
b2476490
MT
231/* caller must hold prepare_lock */
232static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
233{
234 struct dentry *d;
235 int ret = -ENOMEM;
236
237 if (!clk || !pdentry) {
238 ret = -EINVAL;
239 goto out;
240 }
241
242 d = debugfs_create_dir(clk->name, pdentry);
243 if (!d)
244 goto out;
245
246 clk->dentry = d;
247
248 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
249 (u32 *)&clk->rate);
250 if (!d)
251 goto err_out;
252
253 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
254 (u32 *)&clk->flags);
255 if (!d)
256 goto err_out;
257
258 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
259 (u32 *)&clk->prepare_count);
260 if (!d)
261 goto err_out;
262
263 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
264 (u32 *)&clk->enable_count);
265 if (!d)
266 goto err_out;
267
268 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
269 (u32 *)&clk->notifier_count);
270 if (!d)
271 goto err_out;
272
273 ret = 0;
274 goto out;
275
276err_out:
b5f98e65
AE
277 debugfs_remove_recursive(clk->dentry);
278 clk->dentry = NULL;
b2476490
MT
279out:
280 return ret;
281}
282
283/* caller must hold prepare_lock */
284static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry)
285{
286 struct clk *child;
b2476490
MT
287 int ret = -EINVAL;;
288
289 if (!clk || !pdentry)
290 goto out;
291
292 ret = clk_debug_create_one(clk, pdentry);
293
294 if (ret)
295 goto out;
296
b67bfe0d 297 hlist_for_each_entry(child, &clk->children, child_node)
b2476490
MT
298 clk_debug_create_subtree(child, clk->dentry);
299
300 ret = 0;
301out:
302 return ret;
303}
304
305/**
306 * clk_debug_register - add a clk node to the debugfs clk tree
307 * @clk: the clk being added to the debugfs clk tree
308 *
309 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
310 * initialized. Otherwise it bails out early since the debugfs clk tree
311 * will be created lazily by clk_debug_init as part of a late_initcall.
312 *
313 * Caller must hold prepare_lock. Only clk_init calls this function (so
314 * far) so this is taken care.
315 */
316static int clk_debug_register(struct clk *clk)
317{
318 struct clk *parent;
319 struct dentry *pdentry;
320 int ret = 0;
321
322 if (!inited)
323 goto out;
324
325 parent = clk->parent;
326
327 /*
328 * Check to see if a clk is a root clk. Also check that it is
329 * safe to add this clk to debugfs
330 */
331 if (!parent)
332 if (clk->flags & CLK_IS_ROOT)
333 pdentry = rootdir;
334 else
335 pdentry = orphandir;
336 else
337 if (parent->dentry)
338 pdentry = parent->dentry;
339 else
340 goto out;
341
342 ret = clk_debug_create_subtree(clk, pdentry);
343
344out:
345 return ret;
346}
347
b33d212f
UH
348/**
349 * clk_debug_reparent - reparent clk node in the debugfs clk tree
350 * @clk: the clk being reparented
351 * @new_parent: the new clk parent, may be NULL
352 *
353 * Rename clk entry in the debugfs clk tree if debugfs has been
354 * initialized. Otherwise it bails out early since the debugfs clk tree
355 * will be created lazily by clk_debug_init as part of a late_initcall.
356 *
357 * Caller must hold prepare_lock.
358 */
359static void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
360{
361 struct dentry *d;
362 struct dentry *new_parent_d;
363
364 if (!inited)
365 return;
366
367 if (new_parent)
368 new_parent_d = new_parent->dentry;
369 else
370 new_parent_d = orphandir;
371
372 d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
373 new_parent_d, clk->name);
374 if (d)
375 clk->dentry = d;
376 else
377 pr_debug("%s: failed to rename debugfs entry for %s\n",
378 __func__, clk->name);
379}
380
b2476490
MT
381/**
382 * clk_debug_init - lazily create the debugfs clk tree visualization
383 *
384 * clks are often initialized very early during boot before memory can
385 * be dynamically allocated and well before debugfs is setup.
386 * clk_debug_init walks the clk tree hierarchy while holding
387 * prepare_lock and creates the topology as part of a late_initcall,
388 * thus insuring that clks initialized very early will still be
389 * represented in the debugfs clk tree. This function should only be
390 * called once at boot-time, and all other clks added dynamically will
391 * be done so with clk_debug_register.
392 */
393static int __init clk_debug_init(void)
394{
395 struct clk *clk;
1af599df 396 struct dentry *d;
b2476490
MT
397
398 rootdir = debugfs_create_dir("clk", NULL);
399
400 if (!rootdir)
401 return -ENOMEM;
402
1af599df
PG
403 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, NULL,
404 &clk_summary_fops);
405 if (!d)
406 return -ENOMEM;
407
bddca894
PG
408 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, NULL,
409 &clk_dump_fops);
410 if (!d)
411 return -ENOMEM;
412
b2476490
MT
413 orphandir = debugfs_create_dir("orphans", rootdir);
414
415 if (!orphandir)
416 return -ENOMEM;
417
eab89f69 418 clk_prepare_lock();
b2476490 419
b67bfe0d 420 hlist_for_each_entry(clk, &clk_root_list, child_node)
b2476490
MT
421 clk_debug_create_subtree(clk, rootdir);
422
b67bfe0d 423 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
b2476490
MT
424 clk_debug_create_subtree(clk, orphandir);
425
426 inited = 1;
427
eab89f69 428 clk_prepare_unlock();
b2476490
MT
429
430 return 0;
431}
432late_initcall(clk_debug_init);
433#else
434static inline int clk_debug_register(struct clk *clk) { return 0; }
b33d212f
UH
435static inline void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
436{
437}
70d347e6 438#endif
b2476490 439
1c155b3d
UH
440/* caller must hold prepare_lock */
441static void clk_unprepare_unused_subtree(struct clk *clk)
442{
443 struct clk *child;
444
445 if (!clk)
446 return;
447
448 hlist_for_each_entry(child, &clk->children, child_node)
449 clk_unprepare_unused_subtree(child);
450
451 if (clk->prepare_count)
452 return;
453
454 if (clk->flags & CLK_IGNORE_UNUSED)
455 return;
456
3cc8247f
UH
457 if (__clk_is_prepared(clk)) {
458 if (clk->ops->unprepare_unused)
459 clk->ops->unprepare_unused(clk->hw);
460 else if (clk->ops->unprepare)
1c155b3d 461 clk->ops->unprepare(clk->hw);
3cc8247f 462 }
1c155b3d
UH
463}
464
b2476490
MT
465/* caller must hold prepare_lock */
466static void clk_disable_unused_subtree(struct clk *clk)
467{
468 struct clk *child;
b2476490
MT
469 unsigned long flags;
470
471 if (!clk)
472 goto out;
473
b67bfe0d 474 hlist_for_each_entry(child, &clk->children, child_node)
b2476490
MT
475 clk_disable_unused_subtree(child);
476
eab89f69 477 flags = clk_enable_lock();
b2476490
MT
478
479 if (clk->enable_count)
480 goto unlock_out;
481
482 if (clk->flags & CLK_IGNORE_UNUSED)
483 goto unlock_out;
484
7c045a55
MT
485 /*
486 * some gate clocks have special needs during the disable-unused
487 * sequence. call .disable_unused if available, otherwise fall
488 * back to .disable
489 */
490 if (__clk_is_enabled(clk)) {
491 if (clk->ops->disable_unused)
492 clk->ops->disable_unused(clk->hw);
493 else if (clk->ops->disable)
494 clk->ops->disable(clk->hw);
495 }
b2476490
MT
496
497unlock_out:
eab89f69 498 clk_enable_unlock(flags);
b2476490
MT
499
500out:
501 return;
502}
503
1e435256
OJ
504static bool clk_ignore_unused;
505static int __init clk_ignore_unused_setup(char *__unused)
506{
507 clk_ignore_unused = true;
508 return 1;
509}
510__setup("clk_ignore_unused", clk_ignore_unused_setup);
511
b2476490
MT
512static int clk_disable_unused(void)
513{
514 struct clk *clk;
b2476490 515
1e435256
OJ
516 if (clk_ignore_unused) {
517 pr_warn("clk: Not disabling unused clocks\n");
518 return 0;
519 }
520
eab89f69 521 clk_prepare_lock();
b2476490 522
b67bfe0d 523 hlist_for_each_entry(clk, &clk_root_list, child_node)
b2476490
MT
524 clk_disable_unused_subtree(clk);
525
b67bfe0d 526 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
b2476490
MT
527 clk_disable_unused_subtree(clk);
528
1c155b3d
UH
529 hlist_for_each_entry(clk, &clk_root_list, child_node)
530 clk_unprepare_unused_subtree(clk);
531
532 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
533 clk_unprepare_unused_subtree(clk);
534
eab89f69 535 clk_prepare_unlock();
b2476490
MT
536
537 return 0;
538}
d41d5805 539late_initcall_sync(clk_disable_unused);
b2476490
MT
540
541/*** helper functions ***/
542
65800b2c 543const char *__clk_get_name(struct clk *clk)
b2476490
MT
544{
545 return !clk ? NULL : clk->name;
546}
4895084c 547EXPORT_SYMBOL_GPL(__clk_get_name);
b2476490 548
65800b2c 549struct clk_hw *__clk_get_hw(struct clk *clk)
b2476490
MT
550{
551 return !clk ? NULL : clk->hw;
552}
553
65800b2c 554u8 __clk_get_num_parents(struct clk *clk)
b2476490 555{
2ac6b1f5 556 return !clk ? 0 : clk->num_parents;
b2476490
MT
557}
558
65800b2c 559struct clk *__clk_get_parent(struct clk *clk)
b2476490
MT
560{
561 return !clk ? NULL : clk->parent;
562}
563
7ef3dcc8
JH
564struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
565{
566 if (!clk || index >= clk->num_parents)
567 return NULL;
568 else if (!clk->parents)
569 return __clk_lookup(clk->parent_names[index]);
570 else if (!clk->parents[index])
571 return clk->parents[index] =
572 __clk_lookup(clk->parent_names[index]);
573 else
574 return clk->parents[index];
575}
576
65800b2c 577unsigned int __clk_get_enable_count(struct clk *clk)
b2476490 578{
2ac6b1f5 579 return !clk ? 0 : clk->enable_count;
b2476490
MT
580}
581
65800b2c 582unsigned int __clk_get_prepare_count(struct clk *clk)
b2476490 583{
2ac6b1f5 584 return !clk ? 0 : clk->prepare_count;
b2476490
MT
585}
586
587unsigned long __clk_get_rate(struct clk *clk)
588{
589 unsigned long ret;
590
591 if (!clk) {
34e44fe8 592 ret = 0;
b2476490
MT
593 goto out;
594 }
595
596 ret = clk->rate;
597
598 if (clk->flags & CLK_IS_ROOT)
599 goto out;
600
601 if (!clk->parent)
34e44fe8 602 ret = 0;
b2476490
MT
603
604out:
605 return ret;
606}
607
65800b2c 608unsigned long __clk_get_flags(struct clk *clk)
b2476490 609{
2ac6b1f5 610 return !clk ? 0 : clk->flags;
b2476490 611}
b05c6836 612EXPORT_SYMBOL_GPL(__clk_get_flags);
b2476490 613
3d6ee287
UH
614bool __clk_is_prepared(struct clk *clk)
615{
616 int ret;
617
618 if (!clk)
619 return false;
620
621 /*
622 * .is_prepared is optional for clocks that can prepare
623 * fall back to software usage counter if it is missing
624 */
625 if (!clk->ops->is_prepared) {
626 ret = clk->prepare_count ? 1 : 0;
627 goto out;
628 }
629
630 ret = clk->ops->is_prepared(clk->hw);
631out:
632 return !!ret;
633}
634
2ac6b1f5 635bool __clk_is_enabled(struct clk *clk)
b2476490
MT
636{
637 int ret;
638
639 if (!clk)
2ac6b1f5 640 return false;
b2476490
MT
641
642 /*
643 * .is_enabled is only mandatory for clocks that gate
644 * fall back to software usage counter if .is_enabled is missing
645 */
646 if (!clk->ops->is_enabled) {
647 ret = clk->enable_count ? 1 : 0;
648 goto out;
649 }
650
651 ret = clk->ops->is_enabled(clk->hw);
652out:
2ac6b1f5 653 return !!ret;
b2476490
MT
654}
655
656static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
657{
658 struct clk *child;
659 struct clk *ret;
b2476490
MT
660
661 if (!strcmp(clk->name, name))
662 return clk;
663
b67bfe0d 664 hlist_for_each_entry(child, &clk->children, child_node) {
b2476490
MT
665 ret = __clk_lookup_subtree(name, child);
666 if (ret)
667 return ret;
668 }
669
670 return NULL;
671}
672
673struct clk *__clk_lookup(const char *name)
674{
675 struct clk *root_clk;
676 struct clk *ret;
b2476490
MT
677
678 if (!name)
679 return NULL;
680
681 /* search the 'proper' clk tree first */
b67bfe0d 682 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
b2476490
MT
683 ret = __clk_lookup_subtree(name, root_clk);
684 if (ret)
685 return ret;
686 }
687
688 /* if not found, then search the orphan tree */
b67bfe0d 689 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
b2476490
MT
690 ret = __clk_lookup_subtree(name, root_clk);
691 if (ret)
692 return ret;
693 }
694
695 return NULL;
696}
697
e366fdd7
JH
698/*
699 * Helper for finding best parent to provide a given frequency. This can be used
700 * directly as a determine_rate callback (e.g. for a mux), or from a more
701 * complex clock that may combine a mux with other operations.
702 */
703long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
704 unsigned long *best_parent_rate,
705 struct clk **best_parent_p)
706{
707 struct clk *clk = hw->clk, *parent, *best_parent = NULL;
708 int i, num_parents;
709 unsigned long parent_rate, best = 0;
710
711 /* if NO_REPARENT flag set, pass through to current parent */
712 if (clk->flags & CLK_SET_RATE_NO_REPARENT) {
713 parent = clk->parent;
714 if (clk->flags & CLK_SET_RATE_PARENT)
715 best = __clk_round_rate(parent, rate);
716 else if (parent)
717 best = __clk_get_rate(parent);
718 else
719 best = __clk_get_rate(clk);
720 goto out;
721 }
722
723 /* find the parent that can provide the fastest rate <= rate */
724 num_parents = clk->num_parents;
725 for (i = 0; i < num_parents; i++) {
726 parent = clk_get_parent_by_index(clk, i);
727 if (!parent)
728 continue;
729 if (clk->flags & CLK_SET_RATE_PARENT)
730 parent_rate = __clk_round_rate(parent, rate);
731 else
732 parent_rate = __clk_get_rate(parent);
733 if (parent_rate <= rate && parent_rate > best) {
734 best_parent = parent;
735 best = parent_rate;
736 }
737 }
738
739out:
740 if (best_parent)
741 *best_parent_p = best_parent;
742 *best_parent_rate = best;
743
744 return best;
745}
746
b2476490
MT
747/*** clk api ***/
748
749void __clk_unprepare(struct clk *clk)
750{
751 if (!clk)
752 return;
753
754 if (WARN_ON(clk->prepare_count == 0))
755 return;
756
757 if (--clk->prepare_count > 0)
758 return;
759
760 WARN_ON(clk->enable_count > 0);
761
762 if (clk->ops->unprepare)
763 clk->ops->unprepare(clk->hw);
764
765 __clk_unprepare(clk->parent);
766}
767
768/**
769 * clk_unprepare - undo preparation of a clock source
24ee1a08 770 * @clk: the clk being unprepared
b2476490
MT
771 *
772 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
773 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
774 * if the operation may sleep. One example is a clk which is accessed over
775 * I2c. In the complex case a clk gate operation may require a fast and a slow
776 * part. It is this reason that clk_unprepare and clk_disable are not mutually
777 * exclusive. In fact clk_disable must be called before clk_unprepare.
778 */
779void clk_unprepare(struct clk *clk)
780{
eab89f69 781 clk_prepare_lock();
b2476490 782 __clk_unprepare(clk);
eab89f69 783 clk_prepare_unlock();
b2476490
MT
784}
785EXPORT_SYMBOL_GPL(clk_unprepare);
786
787int __clk_prepare(struct clk *clk)
788{
789 int ret = 0;
790
791 if (!clk)
792 return 0;
793
794 if (clk->prepare_count == 0) {
795 ret = __clk_prepare(clk->parent);
796 if (ret)
797 return ret;
798
799 if (clk->ops->prepare) {
800 ret = clk->ops->prepare(clk->hw);
801 if (ret) {
802 __clk_unprepare(clk->parent);
803 return ret;
804 }
805 }
806 }
807
808 clk->prepare_count++;
809
810 return 0;
811}
812
813/**
814 * clk_prepare - prepare a clock source
815 * @clk: the clk being prepared
816 *
817 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
818 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
819 * operation may sleep. One example is a clk which is accessed over I2c. In
820 * the complex case a clk ungate operation may require a fast and a slow part.
821 * It is this reason that clk_prepare and clk_enable are not mutually
822 * exclusive. In fact clk_prepare must be called before clk_enable.
823 * Returns 0 on success, -EERROR otherwise.
824 */
825int clk_prepare(struct clk *clk)
826{
827 int ret;
828
eab89f69 829 clk_prepare_lock();
b2476490 830 ret = __clk_prepare(clk);
eab89f69 831 clk_prepare_unlock();
b2476490
MT
832
833 return ret;
834}
835EXPORT_SYMBOL_GPL(clk_prepare);
836
837static void __clk_disable(struct clk *clk)
838{
839 if (!clk)
840 return;
841
e47c6a34
FW
842 if (WARN_ON(IS_ERR(clk)))
843 return;
844
b2476490
MT
845 if (WARN_ON(clk->enable_count == 0))
846 return;
847
848 if (--clk->enable_count > 0)
849 return;
850
851 if (clk->ops->disable)
852 clk->ops->disable(clk->hw);
853
854 __clk_disable(clk->parent);
855}
856
857/**
858 * clk_disable - gate a clock
859 * @clk: the clk being gated
860 *
861 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
862 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
863 * clk if the operation is fast and will never sleep. One example is a
864 * SoC-internal clk which is controlled via simple register writes. In the
865 * complex case a clk gate operation may require a fast and a slow part. It is
866 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
867 * In fact clk_disable must be called before clk_unprepare.
868 */
869void clk_disable(struct clk *clk)
870{
871 unsigned long flags;
872
eab89f69 873 flags = clk_enable_lock();
b2476490 874 __clk_disable(clk);
eab89f69 875 clk_enable_unlock(flags);
b2476490
MT
876}
877EXPORT_SYMBOL_GPL(clk_disable);
878
879static int __clk_enable(struct clk *clk)
880{
881 int ret = 0;
882
883 if (!clk)
884 return 0;
885
886 if (WARN_ON(clk->prepare_count == 0))
887 return -ESHUTDOWN;
888
889 if (clk->enable_count == 0) {
890 ret = __clk_enable(clk->parent);
891
892 if (ret)
893 return ret;
894
895 if (clk->ops->enable) {
896 ret = clk->ops->enable(clk->hw);
897 if (ret) {
898 __clk_disable(clk->parent);
899 return ret;
900 }
901 }
902 }
903
904 clk->enable_count++;
905 return 0;
906}
907
908/**
909 * clk_enable - ungate a clock
910 * @clk: the clk being ungated
911 *
912 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
913 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
914 * if the operation will never sleep. One example is a SoC-internal clk which
915 * is controlled via simple register writes. In the complex case a clk ungate
916 * operation may require a fast and a slow part. It is this reason that
917 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
918 * must be called before clk_enable. Returns 0 on success, -EERROR
919 * otherwise.
920 */
921int clk_enable(struct clk *clk)
922{
923 unsigned long flags;
924 int ret;
925
eab89f69 926 flags = clk_enable_lock();
b2476490 927 ret = __clk_enable(clk);
eab89f69 928 clk_enable_unlock(flags);
b2476490
MT
929
930 return ret;
931}
932EXPORT_SYMBOL_GPL(clk_enable);
933
b2476490
MT
934/**
935 * __clk_round_rate - round the given rate for a clk
936 * @clk: round the rate of this clock
24ee1a08 937 * @rate: the rate which is to be rounded
b2476490
MT
938 *
939 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
940 */
941unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
942{
81536e07 943 unsigned long parent_rate = 0;
71472c0c 944 struct clk *parent;
b2476490
MT
945
946 if (!clk)
2ac6b1f5 947 return 0;
b2476490 948
71472c0c
JH
949 parent = clk->parent;
950 if (parent)
951 parent_rate = parent->rate;
952
953 if (clk->ops->determine_rate)
954 return clk->ops->determine_rate(clk->hw, rate, &parent_rate,
955 &parent);
956 else if (clk->ops->round_rate)
957 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
958 else if (clk->flags & CLK_SET_RATE_PARENT)
959 return __clk_round_rate(clk->parent, rate);
960 else
961 return clk->rate;
b2476490
MT
962}
963
964/**
965 * clk_round_rate - round the given rate for a clk
966 * @clk: the clk for which we are rounding a rate
967 * @rate: the rate which is to be rounded
968 *
969 * Takes in a rate as input and rounds it to a rate that the clk can actually
970 * use which is then returned. If clk doesn't support round_rate operation
971 * then the parent rate is returned.
972 */
973long clk_round_rate(struct clk *clk, unsigned long rate)
974{
975 unsigned long ret;
976
eab89f69 977 clk_prepare_lock();
b2476490 978 ret = __clk_round_rate(clk, rate);
eab89f69 979 clk_prepare_unlock();
b2476490
MT
980
981 return ret;
982}
983EXPORT_SYMBOL_GPL(clk_round_rate);
984
985/**
986 * __clk_notify - call clk notifier chain
987 * @clk: struct clk * that is changing rate
988 * @msg: clk notifier type (see include/linux/clk.h)
989 * @old_rate: old clk rate
990 * @new_rate: new clk rate
991 *
992 * Triggers a notifier call chain on the clk rate-change notification
993 * for 'clk'. Passes a pointer to the struct clk and the previous
994 * and current rates to the notifier callback. Intended to be called by
995 * internal clock code only. Returns NOTIFY_DONE from the last driver
996 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
997 * a driver returns that.
998 */
999static int __clk_notify(struct clk *clk, unsigned long msg,
1000 unsigned long old_rate, unsigned long new_rate)
1001{
1002 struct clk_notifier *cn;
1003 struct clk_notifier_data cnd;
1004 int ret = NOTIFY_DONE;
1005
1006 cnd.clk = clk;
1007 cnd.old_rate = old_rate;
1008 cnd.new_rate = new_rate;
1009
1010 list_for_each_entry(cn, &clk_notifier_list, node) {
1011 if (cn->clk == clk) {
1012 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1013 &cnd);
1014 break;
1015 }
1016 }
1017
1018 return ret;
1019}
1020
1021/**
1022 * __clk_recalc_rates
1023 * @clk: first clk in the subtree
1024 * @msg: notification type (see include/linux/clk.h)
1025 *
1026 * Walks the subtree of clks starting with clk and recalculates rates as it
1027 * goes. Note that if a clk does not implement the .recalc_rate callback then
24ee1a08 1028 * it is assumed that the clock will take on the rate of its parent.
b2476490
MT
1029 *
1030 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1031 * if necessary.
1032 *
1033 * Caller must hold prepare_lock.
1034 */
1035static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
1036{
1037 unsigned long old_rate;
1038 unsigned long parent_rate = 0;
b2476490
MT
1039 struct clk *child;
1040
1041 old_rate = clk->rate;
1042
1043 if (clk->parent)
1044 parent_rate = clk->parent->rate;
1045
1046 if (clk->ops->recalc_rate)
1047 clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
1048 else
1049 clk->rate = parent_rate;
1050
1051 /*
1052 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1053 * & ABORT_RATE_CHANGE notifiers
1054 */
1055 if (clk->notifier_count && msg)
1056 __clk_notify(clk, msg, old_rate, clk->rate);
1057
b67bfe0d 1058 hlist_for_each_entry(child, &clk->children, child_node)
b2476490
MT
1059 __clk_recalc_rates(child, msg);
1060}
1061
a093bde2
UH
1062/**
1063 * clk_get_rate - return the rate of clk
1064 * @clk: the clk whose rate is being returned
1065 *
1066 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1067 * is set, which means a recalc_rate will be issued.
1068 * If clk is NULL then returns 0.
1069 */
1070unsigned long clk_get_rate(struct clk *clk)
1071{
1072 unsigned long rate;
1073
eab89f69 1074 clk_prepare_lock();
a093bde2
UH
1075
1076 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
1077 __clk_recalc_rates(clk, 0);
1078
1079 rate = __clk_get_rate(clk);
eab89f69 1080 clk_prepare_unlock();
a093bde2
UH
1081
1082 return rate;
1083}
1084EXPORT_SYMBOL_GPL(clk_get_rate);
1085
f1c8b2ed 1086static int clk_fetch_parent_index(struct clk *clk, struct clk *parent)
4935b22c 1087{
f1c8b2ed 1088 int i;
4935b22c 1089
f1c8b2ed 1090 if (!clk->parents) {
96a7ed90
TF
1091 clk->parents = kcalloc(clk->num_parents,
1092 sizeof(struct clk *), GFP_KERNEL);
f1c8b2ed
TF
1093 if (!clk->parents)
1094 return -ENOMEM;
1095 }
4935b22c
JH
1096
1097 /*
1098 * find index of new parent clock using cached parent ptrs,
1099 * or if not yet cached, use string name comparison and cache
1100 * them now to avoid future calls to __clk_lookup.
1101 */
1102 for (i = 0; i < clk->num_parents; i++) {
da0f0b2c 1103 if (clk->parents[i] == parent)
f1c8b2ed 1104 return i;
da0f0b2c
TF
1105
1106 if (clk->parents[i])
1107 continue;
1108
1109 if (!strcmp(clk->parent_names[i], parent->name)) {
1110 clk->parents[i] = __clk_lookup(parent->name);
f1c8b2ed 1111 return i;
4935b22c
JH
1112 }
1113 }
1114
f1c8b2ed 1115 return -EINVAL;
4935b22c
JH
1116}
1117
1118static void clk_reparent(struct clk *clk, struct clk *new_parent)
1119{
1120 hlist_del(&clk->child_node);
1121
903efc55
JH
1122 if (new_parent) {
1123 /* avoid duplicate POST_RATE_CHANGE notifications */
1124 if (new_parent->new_child == clk)
1125 new_parent->new_child = NULL;
1126
4935b22c 1127 hlist_add_head(&clk->child_node, &new_parent->children);
903efc55 1128 } else {
4935b22c 1129 hlist_add_head(&clk->child_node, &clk_orphan_list);
903efc55 1130 }
4935b22c
JH
1131
1132 clk->parent = new_parent;
1133}
1134
1135static int __clk_set_parent(struct clk *clk, struct clk *parent, u8 p_index)
1136{
1137 unsigned long flags;
1138 int ret = 0;
1139 struct clk *old_parent = clk->parent;
1140
1141 /*
1142 * Migrate prepare state between parents and prevent race with
1143 * clk_enable().
1144 *
1145 * If the clock is not prepared, then a race with
1146 * clk_enable/disable() is impossible since we already have the
1147 * prepare lock (future calls to clk_enable() need to be preceded by
1148 * a clk_prepare()).
1149 *
1150 * If the clock is prepared, migrate the prepared state to the new
1151 * parent and also protect against a race with clk_enable() by
1152 * forcing the clock and the new parent on. This ensures that all
1153 * future calls to clk_enable() are practically NOPs with respect to
1154 * hardware and software states.
1155 *
1156 * See also: Comment for clk_set_parent() below.
1157 */
1158 if (clk->prepare_count) {
1159 __clk_prepare(parent);
1160 clk_enable(parent);
1161 clk_enable(clk);
1162 }
1163
1164 /* update the clk tree topology */
1165 flags = clk_enable_lock();
1166 clk_reparent(clk, parent);
1167 clk_enable_unlock(flags);
1168
1169 /* change clock input source */
1170 if (parent && clk->ops->set_parent)
1171 ret = clk->ops->set_parent(clk->hw, p_index);
1172
1173 if (ret) {
1174 flags = clk_enable_lock();
1175 clk_reparent(clk, old_parent);
1176 clk_enable_unlock(flags);
1177
1178 if (clk->prepare_count) {
1179 clk_disable(clk);
1180 clk_disable(parent);
1181 __clk_unprepare(parent);
1182 }
1183 return ret;
1184 }
1185
1186 /*
1187 * Finish the migration of prepare state and undo the changes done
1188 * for preventing a race with clk_enable().
1189 */
1190 if (clk->prepare_count) {
1191 clk_disable(clk);
1192 clk_disable(old_parent);
1193 __clk_unprepare(old_parent);
1194 }
1195
1196 /* update debugfs with new clk tree topology */
1197 clk_debug_reparent(clk, parent);
1198 return 0;
1199}
1200
b2476490
MT
1201/**
1202 * __clk_speculate_rates
1203 * @clk: first clk in the subtree
1204 * @parent_rate: the "future" rate of clk's parent
1205 *
1206 * Walks the subtree of clks starting with clk, speculating rates as it
1207 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1208 *
1209 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1210 * pre-rate change notifications and returns early if no clks in the
1211 * subtree have subscribed to the notifications. Note that if a clk does not
1212 * implement the .recalc_rate callback then it is assumed that the clock will
24ee1a08 1213 * take on the rate of its parent.
b2476490
MT
1214 *
1215 * Caller must hold prepare_lock.
1216 */
1217static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
1218{
b2476490
MT
1219 struct clk *child;
1220 unsigned long new_rate;
1221 int ret = NOTIFY_DONE;
1222
1223 if (clk->ops->recalc_rate)
1224 new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
1225 else
1226 new_rate = parent_rate;
1227
fb72a059 1228 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
b2476490
MT
1229 if (clk->notifier_count)
1230 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
1231
fb72a059 1232 if (ret & NOTIFY_STOP_MASK)
b2476490
MT
1233 goto out;
1234
b67bfe0d 1235 hlist_for_each_entry(child, &clk->children, child_node) {
b2476490 1236 ret = __clk_speculate_rates(child, new_rate);
fb72a059 1237 if (ret & NOTIFY_STOP_MASK)
b2476490
MT
1238 break;
1239 }
1240
1241out:
1242 return ret;
1243}
1244
71472c0c
JH
1245static void clk_calc_subtree(struct clk *clk, unsigned long new_rate,
1246 struct clk *new_parent, u8 p_index)
b2476490
MT
1247{
1248 struct clk *child;
b2476490
MT
1249
1250 clk->new_rate = new_rate;
71472c0c
JH
1251 clk->new_parent = new_parent;
1252 clk->new_parent_index = p_index;
1253 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1254 clk->new_child = NULL;
1255 if (new_parent && new_parent != clk->parent)
1256 new_parent->new_child = clk;
b2476490 1257
b67bfe0d 1258 hlist_for_each_entry(child, &clk->children, child_node) {
b2476490
MT
1259 if (child->ops->recalc_rate)
1260 child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
1261 else
1262 child->new_rate = new_rate;
71472c0c 1263 clk_calc_subtree(child, child->new_rate, NULL, 0);
b2476490
MT
1264 }
1265}
1266
1267/*
1268 * calculate the new rates returning the topmost clock that has to be
1269 * changed.
1270 */
1271static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
1272{
1273 struct clk *top = clk;
71472c0c 1274 struct clk *old_parent, *parent;
81536e07 1275 unsigned long best_parent_rate = 0;
b2476490 1276 unsigned long new_rate;
f1c8b2ed 1277 int p_index = 0;
b2476490 1278
7452b219
MT
1279 /* sanity */
1280 if (IS_ERR_OR_NULL(clk))
1281 return NULL;
1282
63f5c3b2 1283 /* save parent rate, if it exists */
71472c0c
JH
1284 parent = old_parent = clk->parent;
1285 if (parent)
1286 best_parent_rate = parent->rate;
1287
1288 /* find the closest rate and parent clk/rate */
1289 if (clk->ops->determine_rate) {
1290 new_rate = clk->ops->determine_rate(clk->hw, rate,
1291 &best_parent_rate,
1292 &parent);
1293 } else if (clk->ops->round_rate) {
1294 new_rate = clk->ops->round_rate(clk->hw, rate,
1295 &best_parent_rate);
1296 } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) {
1297 /* pass-through clock without adjustable parent */
1298 clk->new_rate = clk->rate;
1299 return NULL;
1300 } else {
1301 /* pass-through clock with adjustable parent */
1302 top = clk_calc_new_rates(parent, rate);
1303 new_rate = parent->new_rate;
63f5c3b2 1304 goto out;
7452b219
MT
1305 }
1306
71472c0c
JH
1307 /* some clocks must be gated to change parent */
1308 if (parent != old_parent &&
1309 (clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1310 pr_debug("%s: %s not gated but wants to reparent\n",
1311 __func__, clk->name);
b2476490
MT
1312 return NULL;
1313 }
1314
71472c0c
JH
1315 /* try finding the new parent index */
1316 if (parent) {
1317 p_index = clk_fetch_parent_index(clk, parent);
f1c8b2ed 1318 if (p_index < 0) {
71472c0c
JH
1319 pr_debug("%s: clk %s can not be parent of clk %s\n",
1320 __func__, parent->name, clk->name);
1321 return NULL;
1322 }
b2476490
MT
1323 }
1324
71472c0c
JH
1325 if ((clk->flags & CLK_SET_RATE_PARENT) && parent &&
1326 best_parent_rate != parent->rate)
1327 top = clk_calc_new_rates(parent, best_parent_rate);
b2476490
MT
1328
1329out:
71472c0c 1330 clk_calc_subtree(clk, new_rate, parent, p_index);
b2476490
MT
1331
1332 return top;
1333}
1334
1335/*
1336 * Notify about rate changes in a subtree. Always walk down the whole tree
1337 * so that in case of an error we can walk down the whole tree again and
1338 * abort the change.
1339 */
1340static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
1341{
71472c0c 1342 struct clk *child, *tmp_clk, *fail_clk = NULL;
b2476490
MT
1343 int ret = NOTIFY_DONE;
1344
1345 if (clk->rate == clk->new_rate)
5fda6858 1346 return NULL;
b2476490
MT
1347
1348 if (clk->notifier_count) {
1349 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
fb72a059 1350 if (ret & NOTIFY_STOP_MASK)
b2476490
MT
1351 fail_clk = clk;
1352 }
1353
b67bfe0d 1354 hlist_for_each_entry(child, &clk->children, child_node) {
71472c0c
JH
1355 /* Skip children who will be reparented to another clock */
1356 if (child->new_parent && child->new_parent != clk)
1357 continue;
1358 tmp_clk = clk_propagate_rate_change(child, event);
1359 if (tmp_clk)
1360 fail_clk = tmp_clk;
1361 }
1362
1363 /* handle the new child who might not be in clk->children yet */
1364 if (clk->new_child) {
1365 tmp_clk = clk_propagate_rate_change(clk->new_child, event);
1366 if (tmp_clk)
1367 fail_clk = tmp_clk;
b2476490
MT
1368 }
1369
1370 return fail_clk;
1371}
1372
1373/*
1374 * walk down a subtree and set the new rates notifying the rate
1375 * change on the way
1376 */
1377static void clk_change_rate(struct clk *clk)
1378{
1379 struct clk *child;
1380 unsigned long old_rate;
bf47b4fd 1381 unsigned long best_parent_rate = 0;
b2476490
MT
1382
1383 old_rate = clk->rate;
1384
71472c0c
JH
1385 /* set parent */
1386 if (clk->new_parent && clk->new_parent != clk->parent)
1387 __clk_set_parent(clk, clk->new_parent, clk->new_parent_index);
1388
bf47b4fd
PM
1389 if (clk->parent)
1390 best_parent_rate = clk->parent->rate;
1391
b2476490 1392 if (clk->ops->set_rate)
bf47b4fd 1393 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
b2476490
MT
1394
1395 if (clk->ops->recalc_rate)
bf47b4fd 1396 clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate);
b2476490 1397 else
bf47b4fd 1398 clk->rate = best_parent_rate;
b2476490
MT
1399
1400 if (clk->notifier_count && old_rate != clk->rate)
1401 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1402
71472c0c
JH
1403 hlist_for_each_entry(child, &clk->children, child_node) {
1404 /* Skip children who will be reparented to another clock */
1405 if (child->new_parent && child->new_parent != clk)
1406 continue;
b2476490 1407 clk_change_rate(child);
71472c0c
JH
1408 }
1409
1410 /* handle the new child who might not be in clk->children yet */
1411 if (clk->new_child)
1412 clk_change_rate(clk->new_child);
b2476490
MT
1413}
1414
1415/**
1416 * clk_set_rate - specify a new rate for clk
1417 * @clk: the clk whose rate is being changed
1418 * @rate: the new rate for clk
1419 *
5654dc94 1420 * In the simplest case clk_set_rate will only adjust the rate of clk.
b2476490 1421 *
5654dc94
MT
1422 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1423 * propagate up to clk's parent; whether or not this happens depends on the
1424 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1425 * after calling .round_rate then upstream parent propagation is ignored. If
1426 * *parent_rate comes back with a new rate for clk's parent then we propagate
24ee1a08 1427 * up to clk's parent and set its rate. Upward propagation will continue
5654dc94
MT
1428 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1429 * .round_rate stops requesting changes to clk's parent_rate.
b2476490 1430 *
5654dc94
MT
1431 * Rate changes are accomplished via tree traversal that also recalculates the
1432 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
b2476490
MT
1433 *
1434 * Returns 0 on success, -EERROR otherwise.
1435 */
1436int clk_set_rate(struct clk *clk, unsigned long rate)
1437{
1438 struct clk *top, *fail_clk;
1439 int ret = 0;
1440
89ac8d7a
MT
1441 if (!clk)
1442 return 0;
1443
b2476490 1444 /* prevent racing with updates to the clock topology */
eab89f69 1445 clk_prepare_lock();
b2476490
MT
1446
1447 /* bail early if nothing to do */
34e452a1 1448 if (rate == clk_get_rate(clk))
b2476490
MT
1449 goto out;
1450
7e0fa1b5 1451 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
0e1c0301
VK
1452 ret = -EBUSY;
1453 goto out;
1454 }
1455
b2476490
MT
1456 /* calculate new rates and get the topmost changed clock */
1457 top = clk_calc_new_rates(clk, rate);
1458 if (!top) {
1459 ret = -EINVAL;
1460 goto out;
1461 }
1462
1463 /* notify that we are about to change rates */
1464 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1465 if (fail_clk) {
1466 pr_warn("%s: failed to set %s rate\n", __func__,
1467 fail_clk->name);
1468 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1469 ret = -EBUSY;
1470 goto out;
1471 }
1472
1473 /* change the rates */
1474 clk_change_rate(top);
1475
b2476490 1476out:
eab89f69 1477 clk_prepare_unlock();
b2476490
MT
1478
1479 return ret;
1480}
1481EXPORT_SYMBOL_GPL(clk_set_rate);
1482
1483/**
1484 * clk_get_parent - return the parent of a clk
1485 * @clk: the clk whose parent gets returned
1486 *
1487 * Simply returns clk->parent. Returns NULL if clk is NULL.
1488 */
1489struct clk *clk_get_parent(struct clk *clk)
1490{
1491 struct clk *parent;
1492
eab89f69 1493 clk_prepare_lock();
b2476490 1494 parent = __clk_get_parent(clk);
eab89f69 1495 clk_prepare_unlock();
b2476490
MT
1496
1497 return parent;
1498}
1499EXPORT_SYMBOL_GPL(clk_get_parent);
1500
1501/*
1502 * .get_parent is mandatory for clocks with multiple possible parents. It is
1503 * optional for single-parent clocks. Always call .get_parent if it is
1504 * available and WARN if it is missing for multi-parent clocks.
1505 *
1506 * For single-parent clocks without .get_parent, first check to see if the
1507 * .parents array exists, and if so use it to avoid an expensive tree
1508 * traversal. If .parents does not exist then walk the tree with __clk_lookup.
1509 */
1510static struct clk *__clk_init_parent(struct clk *clk)
1511{
1512 struct clk *ret = NULL;
1513 u8 index;
1514
1515 /* handle the trivial cases */
1516
1517 if (!clk->num_parents)
1518 goto out;
1519
1520 if (clk->num_parents == 1) {
1521 if (IS_ERR_OR_NULL(clk->parent))
1522 ret = clk->parent = __clk_lookup(clk->parent_names[0]);
1523 ret = clk->parent;
1524 goto out;
1525 }
1526
1527 if (!clk->ops->get_parent) {
1528 WARN(!clk->ops->get_parent,
1529 "%s: multi-parent clocks must implement .get_parent\n",
1530 __func__);
1531 goto out;
1532 };
1533
1534 /*
1535 * Do our best to cache parent clocks in clk->parents. This prevents
1536 * unnecessary and expensive calls to __clk_lookup. We don't set
1537 * clk->parent here; that is done by the calling function
1538 */
1539
1540 index = clk->ops->get_parent(clk->hw);
1541
1542 if (!clk->parents)
1543 clk->parents =
96a7ed90 1544 kcalloc(clk->num_parents, sizeof(struct clk *),
b2476490
MT
1545 GFP_KERNEL);
1546
7ef3dcc8 1547 ret = clk_get_parent_by_index(clk, index);
b2476490
MT
1548
1549out:
1550 return ret;
1551}
1552
b33d212f
UH
1553void __clk_reparent(struct clk *clk, struct clk *new_parent)
1554{
1555 clk_reparent(clk, new_parent);
1556 clk_debug_reparent(clk, new_parent);
b2476490
MT
1557 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1558}
1559
b2476490
MT
1560/**
1561 * clk_set_parent - switch the parent of a mux clk
1562 * @clk: the mux clk whose input we are switching
1563 * @parent: the new input to clk
1564 *
f8aa0bd5
SK
1565 * Re-parent clk to use parent as its new input source. If clk is in
1566 * prepared state, the clk will get enabled for the duration of this call. If
1567 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1568 * that, the reparenting is glitchy in hardware, etc), use the
1569 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1570 *
1571 * After successfully changing clk's parent clk_set_parent will update the
1572 * clk topology, sysfs topology and propagate rate recalculation via
1573 * __clk_recalc_rates.
1574 *
1575 * Returns 0 on success, -EERROR otherwise.
b2476490
MT
1576 */
1577int clk_set_parent(struct clk *clk, struct clk *parent)
1578{
1579 int ret = 0;
f1c8b2ed 1580 int p_index = 0;
031dcc9b 1581 unsigned long p_rate = 0;
b2476490 1582
89ac8d7a
MT
1583 if (!clk)
1584 return 0;
1585
1586 if (!clk->ops)
b2476490
MT
1587 return -EINVAL;
1588
031dcc9b
UH
1589 /* verify ops for for multi-parent clks */
1590 if ((clk->num_parents > 1) && (!clk->ops->set_parent))
b2476490
MT
1591 return -ENOSYS;
1592
1593 /* prevent racing with updates to the clock topology */
eab89f69 1594 clk_prepare_lock();
b2476490
MT
1595
1596 if (clk->parent == parent)
1597 goto out;
1598
031dcc9b
UH
1599 /* check that we are allowed to re-parent if the clock is in use */
1600 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
1601 ret = -EBUSY;
1602 goto out;
1603 }
1604
1605 /* try finding the new parent index */
1606 if (parent) {
1607 p_index = clk_fetch_parent_index(clk, parent);
1608 p_rate = parent->rate;
f1c8b2ed 1609 if (p_index < 0) {
031dcc9b
UH
1610 pr_debug("%s: clk %s can not be parent of clk %s\n",
1611 __func__, parent->name, clk->name);
f1c8b2ed 1612 ret = p_index;
031dcc9b
UH
1613 goto out;
1614 }
1615 }
1616
b2476490 1617 /* propagate PRE_RATE_CHANGE notifications */
f3aab5d6 1618 ret = __clk_speculate_rates(clk, p_rate);
b2476490
MT
1619
1620 /* abort if a driver objects */
fb72a059 1621 if (ret & NOTIFY_STOP_MASK)
b2476490
MT
1622 goto out;
1623
031dcc9b
UH
1624 /* do the re-parent */
1625 ret = __clk_set_parent(clk, parent, p_index);
b2476490 1626
a68de8e4
UH
1627 /* propagate rate recalculation accordingly */
1628 if (ret)
b2476490 1629 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
a68de8e4
UH
1630 else
1631 __clk_recalc_rates(clk, POST_RATE_CHANGE);
b2476490
MT
1632
1633out:
eab89f69 1634 clk_prepare_unlock();
b2476490
MT
1635
1636 return ret;
1637}
1638EXPORT_SYMBOL_GPL(clk_set_parent);
1639
1640/**
1641 * __clk_init - initialize the data structures in a struct clk
1642 * @dev: device initializing this clk, placeholder for now
1643 * @clk: clk being initialized
1644 *
1645 * Initializes the lists in struct clk, queries the hardware for the
1646 * parent and rate and sets them both.
b2476490 1647 */
d1302a36 1648int __clk_init(struct device *dev, struct clk *clk)
b2476490 1649{
d1302a36 1650 int i, ret = 0;
b2476490 1651 struct clk *orphan;
b67bfe0d 1652 struct hlist_node *tmp2;
b2476490
MT
1653
1654 if (!clk)
d1302a36 1655 return -EINVAL;
b2476490 1656
eab89f69 1657 clk_prepare_lock();
b2476490
MT
1658
1659 /* check to see if a clock with this name is already registered */
d1302a36
MT
1660 if (__clk_lookup(clk->name)) {
1661 pr_debug("%s: clk %s already initialized\n",
1662 __func__, clk->name);
1663 ret = -EEXIST;
b2476490 1664 goto out;
d1302a36 1665 }
b2476490 1666
d4d7e3dd
MT
1667 /* check that clk_ops are sane. See Documentation/clk.txt */
1668 if (clk->ops->set_rate &&
71472c0c
JH
1669 !((clk->ops->round_rate || clk->ops->determine_rate) &&
1670 clk->ops->recalc_rate)) {
1671 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
d4d7e3dd 1672 __func__, clk->name);
d1302a36 1673 ret = -EINVAL;
d4d7e3dd
MT
1674 goto out;
1675 }
1676
1677 if (clk->ops->set_parent && !clk->ops->get_parent) {
1678 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
1679 __func__, clk->name);
d1302a36 1680 ret = -EINVAL;
d4d7e3dd
MT
1681 goto out;
1682 }
1683
b2476490
MT
1684 /* throw a WARN if any entries in parent_names are NULL */
1685 for (i = 0; i < clk->num_parents; i++)
1686 WARN(!clk->parent_names[i],
1687 "%s: invalid NULL in %s's .parent_names\n",
1688 __func__, clk->name);
1689
1690 /*
1691 * Allocate an array of struct clk *'s to avoid unnecessary string
1692 * look-ups of clk's possible parents. This can fail for clocks passed
1693 * in to clk_init during early boot; thus any access to clk->parents[]
1694 * must always check for a NULL pointer and try to populate it if
1695 * necessary.
1696 *
1697 * If clk->parents is not NULL we skip this entire block. This allows
1698 * for clock drivers to statically initialize clk->parents.
1699 */
9ca1c5a4 1700 if (clk->num_parents > 1 && !clk->parents) {
96a7ed90
TF
1701 clk->parents = kcalloc(clk->num_parents, sizeof(struct clk *),
1702 GFP_KERNEL);
b2476490
MT
1703 /*
1704 * __clk_lookup returns NULL for parents that have not been
1705 * clk_init'd; thus any access to clk->parents[] must check
1706 * for a NULL pointer. We can always perform lazy lookups for
1707 * missing parents later on.
1708 */
1709 if (clk->parents)
1710 for (i = 0; i < clk->num_parents; i++)
1711 clk->parents[i] =
1712 __clk_lookup(clk->parent_names[i]);
1713 }
1714
1715 clk->parent = __clk_init_parent(clk);
1716
1717 /*
1718 * Populate clk->parent if parent has already been __clk_init'd. If
1719 * parent has not yet been __clk_init'd then place clk in the orphan
1720 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
1721 * clk list.
1722 *
1723 * Every time a new clk is clk_init'd then we walk the list of orphan
1724 * clocks and re-parent any that are children of the clock currently
1725 * being clk_init'd.
1726 */
1727 if (clk->parent)
1728 hlist_add_head(&clk->child_node,
1729 &clk->parent->children);
1730 else if (clk->flags & CLK_IS_ROOT)
1731 hlist_add_head(&clk->child_node, &clk_root_list);
1732 else
1733 hlist_add_head(&clk->child_node, &clk_orphan_list);
1734
1735 /*
1736 * Set clk's rate. The preferred method is to use .recalc_rate. For
1737 * simple clocks and lazy developers the default fallback is to use the
1738 * parent's rate. If a clock doesn't have a parent (or is orphaned)
1739 * then rate is set to zero.
1740 */
1741 if (clk->ops->recalc_rate)
1742 clk->rate = clk->ops->recalc_rate(clk->hw,
1743 __clk_get_rate(clk->parent));
1744 else if (clk->parent)
1745 clk->rate = clk->parent->rate;
1746 else
1747 clk->rate = 0;
1748
1749 /*
1750 * walk the list of orphan clocks and reparent any that are children of
1751 * this clock
1752 */
b67bfe0d 1753 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
12d29886 1754 if (orphan->num_parents && orphan->ops->get_parent) {
1f61e5f1
MF
1755 i = orphan->ops->get_parent(orphan->hw);
1756 if (!strcmp(clk->name, orphan->parent_names[i]))
1757 __clk_reparent(orphan, clk);
1758 continue;
1759 }
1760
b2476490
MT
1761 for (i = 0; i < orphan->num_parents; i++)
1762 if (!strcmp(clk->name, orphan->parent_names[i])) {
1763 __clk_reparent(orphan, clk);
1764 break;
1765 }
1f61e5f1 1766 }
b2476490
MT
1767
1768 /*
1769 * optional platform-specific magic
1770 *
1771 * The .init callback is not used by any of the basic clock types, but
1772 * exists for weird hardware that must perform initialization magic.
1773 * Please consider other ways of solving initialization problems before
24ee1a08 1774 * using this callback, as its use is discouraged.
b2476490
MT
1775 */
1776 if (clk->ops->init)
1777 clk->ops->init(clk->hw);
1778
1779 clk_debug_register(clk);
1780
1781out:
eab89f69 1782 clk_prepare_unlock();
b2476490 1783
d1302a36 1784 return ret;
b2476490
MT
1785}
1786
0197b3ea
SK
1787/**
1788 * __clk_register - register a clock and return a cookie.
1789 *
1790 * Same as clk_register, except that the .clk field inside hw shall point to a
1791 * preallocated (generally statically allocated) struct clk. None of the fields
1792 * of the struct clk need to be initialized.
1793 *
1794 * The data pointed to by .init and .clk field shall NOT be marked as init
1795 * data.
1796 *
1797 * __clk_register is only exposed via clk-private.h and is intended for use with
1798 * very large numbers of clocks that need to be statically initialized. It is
1799 * a layering violation to include clk-private.h from any code which implements
1800 * a clock's .ops; as such any statically initialized clock data MUST be in a
24ee1a08 1801 * separate C file from the logic that implements its operations. Returns 0
0197b3ea
SK
1802 * on success, otherwise an error code.
1803 */
1804struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
1805{
1806 int ret;
1807 struct clk *clk;
1808
1809 clk = hw->clk;
1810 clk->name = hw->init->name;
1811 clk->ops = hw->init->ops;
1812 clk->hw = hw;
1813 clk->flags = hw->init->flags;
1814 clk->parent_names = hw->init->parent_names;
1815 clk->num_parents = hw->init->num_parents;
ac2df527
SN
1816 if (dev && dev->driver)
1817 clk->owner = dev->driver->owner;
1818 else
1819 clk->owner = NULL;
0197b3ea
SK
1820
1821 ret = __clk_init(dev, clk);
1822 if (ret)
1823 return ERR_PTR(ret);
1824
1825 return clk;
1826}
1827EXPORT_SYMBOL_GPL(__clk_register);
1828
46c8773a 1829static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
b2476490 1830{
d1302a36 1831 int i, ret;
b2476490 1832
0197b3ea
SK
1833 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
1834 if (!clk->name) {
1835 pr_err("%s: could not allocate clk->name\n", __func__);
1836 ret = -ENOMEM;
1837 goto fail_name;
1838 }
1839 clk->ops = hw->init->ops;
ac2df527
SN
1840 if (dev && dev->driver)
1841 clk->owner = dev->driver->owner;
b2476490 1842 clk->hw = hw;
0197b3ea
SK
1843 clk->flags = hw->init->flags;
1844 clk->num_parents = hw->init->num_parents;
b2476490
MT
1845 hw->clk = clk;
1846
d1302a36 1847 /* allocate local copy in case parent_names is __initdata */
96a7ed90
TF
1848 clk->parent_names = kcalloc(clk->num_parents, sizeof(char *),
1849 GFP_KERNEL);
d1302a36
MT
1850
1851 if (!clk->parent_names) {
1852 pr_err("%s: could not allocate clk->parent_names\n", __func__);
1853 ret = -ENOMEM;
1854 goto fail_parent_names;
1855 }
1856
1857
1858 /* copy each string name in case parent_names is __initdata */
0197b3ea
SK
1859 for (i = 0; i < clk->num_parents; i++) {
1860 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
1861 GFP_KERNEL);
d1302a36
MT
1862 if (!clk->parent_names[i]) {
1863 pr_err("%s: could not copy parent_names\n", __func__);
1864 ret = -ENOMEM;
1865 goto fail_parent_names_copy;
1866 }
1867 }
1868
1869 ret = __clk_init(dev, clk);
1870 if (!ret)
46c8773a 1871 return 0;
b2476490 1872
d1302a36
MT
1873fail_parent_names_copy:
1874 while (--i >= 0)
1875 kfree(clk->parent_names[i]);
1876 kfree(clk->parent_names);
1877fail_parent_names:
0197b3ea
SK
1878 kfree(clk->name);
1879fail_name:
46c8773a
SB
1880 return ret;
1881}
1882
1883/**
1884 * clk_register - allocate a new clock, register it and return an opaque cookie
1885 * @dev: device that is registering this clock
1886 * @hw: link to hardware-specific clock data
1887 *
1888 * clk_register is the primary interface for populating the clock tree with new
1889 * clock nodes. It returns a pointer to the newly allocated struct clk which
1890 * cannot be dereferenced by driver code but may be used in conjuction with the
1891 * rest of the clock API. In the event of an error clk_register will return an
1892 * error code; drivers must test for an error code after calling clk_register.
1893 */
1894struct clk *clk_register(struct device *dev, struct clk_hw *hw)
1895{
1896 int ret;
1897 struct clk *clk;
1898
1899 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
1900 if (!clk) {
1901 pr_err("%s: could not allocate clk\n", __func__);
1902 ret = -ENOMEM;
1903 goto fail_out;
1904 }
1905
1906 ret = _clk_register(dev, hw, clk);
1907 if (!ret)
1908 return clk;
1909
d1302a36
MT
1910 kfree(clk);
1911fail_out:
1912 return ERR_PTR(ret);
b2476490
MT
1913}
1914EXPORT_SYMBOL_GPL(clk_register);
1915
1df5c939
MB
1916/**
1917 * clk_unregister - unregister a currently registered clock
1918 * @clk: clock to unregister
1919 *
1920 * Currently unimplemented.
1921 */
1922void clk_unregister(struct clk *clk) {}
1923EXPORT_SYMBOL_GPL(clk_unregister);
1924
46c8773a
SB
1925static void devm_clk_release(struct device *dev, void *res)
1926{
1927 clk_unregister(res);
1928}
1929
1930/**
1931 * devm_clk_register - resource managed clk_register()
1932 * @dev: device that is registering this clock
1933 * @hw: link to hardware-specific clock data
1934 *
1935 * Managed clk_register(). Clocks returned from this function are
1936 * automatically clk_unregister()ed on driver detach. See clk_register() for
1937 * more information.
1938 */
1939struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
1940{
1941 struct clk *clk;
1942 int ret;
1943
1944 clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL);
1945 if (!clk)
1946 return ERR_PTR(-ENOMEM);
1947
1948 ret = _clk_register(dev, hw, clk);
1949 if (!ret) {
1950 devres_add(dev, clk);
1951 } else {
1952 devres_free(clk);
1953 clk = ERR_PTR(ret);
1954 }
1955
1956 return clk;
1957}
1958EXPORT_SYMBOL_GPL(devm_clk_register);
1959
1960static int devm_clk_match(struct device *dev, void *res, void *data)
1961{
1962 struct clk *c = res;
1963 if (WARN_ON(!c))
1964 return 0;
1965 return c == data;
1966}
1967
1968/**
1969 * devm_clk_unregister - resource managed clk_unregister()
1970 * @clk: clock to unregister
1971 *
1972 * Deallocate a clock allocated with devm_clk_register(). Normally
1973 * this function will not need to be called and the resource management
1974 * code will ensure that the resource is freed.
1975 */
1976void devm_clk_unregister(struct device *dev, struct clk *clk)
1977{
1978 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
1979}
1980EXPORT_SYMBOL_GPL(devm_clk_unregister);
1981
ac2df527
SN
1982/*
1983 * clkdev helpers
1984 */
1985int __clk_get(struct clk *clk)
1986{
1987 if (clk && !try_module_get(clk->owner))
1988 return 0;
1989
1990 return 1;
1991}
1992
1993void __clk_put(struct clk *clk)
1994{
1995 if (WARN_ON_ONCE(IS_ERR(clk)))
1996 return;
1997
1998 if (clk)
1999 module_put(clk->owner);
2000}
2001
b2476490
MT
2002/*** clk rate change notifiers ***/
2003
2004/**
2005 * clk_notifier_register - add a clk rate change notifier
2006 * @clk: struct clk * to watch
2007 * @nb: struct notifier_block * with callback info
2008 *
2009 * Request notification when clk's rate changes. This uses an SRCU
2010 * notifier because we want it to block and notifier unregistrations are
2011 * uncommon. The callbacks associated with the notifier must not
2012 * re-enter into the clk framework by calling any top-level clk APIs;
2013 * this will cause a nested prepare_lock mutex.
2014 *
2015 * Pre-change notifier callbacks will be passed the current, pre-change
2016 * rate of the clk via struct clk_notifier_data.old_rate. The new,
2017 * post-change rate of the clk is passed via struct
2018 * clk_notifier_data.new_rate.
2019 *
2020 * Post-change notifiers will pass the now-current, post-change rate of
2021 * the clk in both struct clk_notifier_data.old_rate and struct
2022 * clk_notifier_data.new_rate.
2023 *
2024 * Abort-change notifiers are effectively the opposite of pre-change
2025 * notifiers: the original pre-change clk rate is passed in via struct
2026 * clk_notifier_data.new_rate and the failed post-change rate is passed
2027 * in via struct clk_notifier_data.old_rate.
2028 *
2029 * clk_notifier_register() must be called from non-atomic context.
2030 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2031 * allocation failure; otherwise, passes along the return value of
2032 * srcu_notifier_chain_register().
2033 */
2034int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2035{
2036 struct clk_notifier *cn;
2037 int ret = -ENOMEM;
2038
2039 if (!clk || !nb)
2040 return -EINVAL;
2041
eab89f69 2042 clk_prepare_lock();
b2476490
MT
2043
2044 /* search the list of notifiers for this clk */
2045 list_for_each_entry(cn, &clk_notifier_list, node)
2046 if (cn->clk == clk)
2047 break;
2048
2049 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2050 if (cn->clk != clk) {
2051 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2052 if (!cn)
2053 goto out;
2054
2055 cn->clk = clk;
2056 srcu_init_notifier_head(&cn->notifier_head);
2057
2058 list_add(&cn->node, &clk_notifier_list);
2059 }
2060
2061 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2062
2063 clk->notifier_count++;
2064
2065out:
eab89f69 2066 clk_prepare_unlock();
b2476490
MT
2067
2068 return ret;
2069}
2070EXPORT_SYMBOL_GPL(clk_notifier_register);
2071
2072/**
2073 * clk_notifier_unregister - remove a clk rate change notifier
2074 * @clk: struct clk *
2075 * @nb: struct notifier_block * with callback info
2076 *
2077 * Request no further notification for changes to 'clk' and frees memory
2078 * allocated in clk_notifier_register.
2079 *
2080 * Returns -EINVAL if called with null arguments; otherwise, passes
2081 * along the return value of srcu_notifier_chain_unregister().
2082 */
2083int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2084{
2085 struct clk_notifier *cn = NULL;
2086 int ret = -EINVAL;
2087
2088 if (!clk || !nb)
2089 return -EINVAL;
2090
eab89f69 2091 clk_prepare_lock();
b2476490
MT
2092
2093 list_for_each_entry(cn, &clk_notifier_list, node)
2094 if (cn->clk == clk)
2095 break;
2096
2097 if (cn->clk == clk) {
2098 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2099
2100 clk->notifier_count--;
2101
2102 /* XXX the notifier code should handle this better */
2103 if (!cn->notifier_head.head) {
2104 srcu_cleanup_notifier_head(&cn->notifier_head);
72b5322f 2105 list_del(&cn->node);
b2476490
MT
2106 kfree(cn);
2107 }
2108
2109 } else {
2110 ret = -ENOENT;
2111 }
2112
eab89f69 2113 clk_prepare_unlock();
b2476490
MT
2114
2115 return ret;
2116}
2117EXPORT_SYMBOL_GPL(clk_notifier_unregister);
766e6a4e
GL
2118
2119#ifdef CONFIG_OF
2120/**
2121 * struct of_clk_provider - Clock provider registration structure
2122 * @link: Entry in global list of clock providers
2123 * @node: Pointer to device tree node of clock provider
2124 * @get: Get clock callback. Returns NULL or a struct clk for the
2125 * given clock specifier
2126 * @data: context pointer to be passed into @get callback
2127 */
2128struct of_clk_provider {
2129 struct list_head link;
2130
2131 struct device_node *node;
2132 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2133 void *data;
2134};
2135
f2f6c255
PG
2136extern struct of_device_id __clk_of_table[];
2137
2138static const struct of_device_id __clk_of_table_sentinel
2139 __used __section(__clk_of_table_end);
2140
766e6a4e 2141static LIST_HEAD(of_clk_providers);
d6782c26
SN
2142static DEFINE_MUTEX(of_clk_mutex);
2143
2144/* of_clk_provider list locking helpers */
2145void of_clk_lock(void)
2146{
2147 mutex_lock(&of_clk_mutex);
2148}
2149
2150void of_clk_unlock(void)
2151{
2152 mutex_unlock(&of_clk_mutex);
2153}
766e6a4e
GL
2154
2155struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
2156 void *data)
2157{
2158 return data;
2159}
2160EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
2161
494bfec9
SG
2162struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
2163{
2164 struct clk_onecell_data *clk_data = data;
2165 unsigned int idx = clkspec->args[0];
2166
2167 if (idx >= clk_data->clk_num) {
2168 pr_err("%s: invalid clock index %d\n", __func__, idx);
2169 return ERR_PTR(-EINVAL);
2170 }
2171
2172 return clk_data->clks[idx];
2173}
2174EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
2175
766e6a4e
GL
2176/**
2177 * of_clk_add_provider() - Register a clock provider for a node
2178 * @np: Device node pointer associated with clock provider
2179 * @clk_src_get: callback for decoding clock
2180 * @data: context pointer for @clk_src_get callback.
2181 */
2182int of_clk_add_provider(struct device_node *np,
2183 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
2184 void *data),
2185 void *data)
2186{
2187 struct of_clk_provider *cp;
2188
2189 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
2190 if (!cp)
2191 return -ENOMEM;
2192
2193 cp->node = of_node_get(np);
2194 cp->data = data;
2195 cp->get = clk_src_get;
2196
d6782c26 2197 mutex_lock(&of_clk_mutex);
766e6a4e 2198 list_add(&cp->link, &of_clk_providers);
d6782c26 2199 mutex_unlock(&of_clk_mutex);
766e6a4e
GL
2200 pr_debug("Added clock from %s\n", np->full_name);
2201
2202 return 0;
2203}
2204EXPORT_SYMBOL_GPL(of_clk_add_provider);
2205
2206/**
2207 * of_clk_del_provider() - Remove a previously registered clock provider
2208 * @np: Device node pointer associated with clock provider
2209 */
2210void of_clk_del_provider(struct device_node *np)
2211{
2212 struct of_clk_provider *cp;
2213
d6782c26 2214 mutex_lock(&of_clk_mutex);
766e6a4e
GL
2215 list_for_each_entry(cp, &of_clk_providers, link) {
2216 if (cp->node == np) {
2217 list_del(&cp->link);
2218 of_node_put(cp->node);
2219 kfree(cp);
2220 break;
2221 }
2222 }
d6782c26 2223 mutex_unlock(&of_clk_mutex);
766e6a4e
GL
2224}
2225EXPORT_SYMBOL_GPL(of_clk_del_provider);
2226
d6782c26 2227struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec)
766e6a4e
GL
2228{
2229 struct of_clk_provider *provider;
2230 struct clk *clk = ERR_PTR(-ENOENT);
2231
2232 /* Check if we have such a provider in our array */
766e6a4e
GL
2233 list_for_each_entry(provider, &of_clk_providers, link) {
2234 if (provider->node == clkspec->np)
2235 clk = provider->get(clkspec, provider->data);
2236 if (!IS_ERR(clk))
2237 break;
2238 }
d6782c26
SN
2239
2240 return clk;
2241}
2242
2243struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
2244{
2245 struct clk *clk;
2246
2247 mutex_lock(&of_clk_mutex);
2248 clk = __of_clk_get_from_provider(clkspec);
2249 mutex_unlock(&of_clk_mutex);
766e6a4e
GL
2250
2251 return clk;
2252}
2253
f6102742
MT
2254int of_clk_get_parent_count(struct device_node *np)
2255{
2256 return of_count_phandle_with_args(np, "clocks", "#clock-cells");
2257}
2258EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
2259
766e6a4e
GL
2260const char *of_clk_get_parent_name(struct device_node *np, int index)
2261{
2262 struct of_phandle_args clkspec;
2263 const char *clk_name;
2264 int rc;
2265
2266 if (index < 0)
2267 return NULL;
2268
2269 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
2270 &clkspec);
2271 if (rc)
2272 return NULL;
2273
2274 if (of_property_read_string_index(clkspec.np, "clock-output-names",
2275 clkspec.args_count ? clkspec.args[0] : 0,
2276 &clk_name) < 0)
2277 clk_name = clkspec.np->name;
2278
2279 of_node_put(clkspec.np);
2280 return clk_name;
2281}
2282EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
2283
2284/**
2285 * of_clk_init() - Scan and init clock providers from the DT
2286 * @matches: array of compatible values and init functions for providers.
2287 *
2288 * This function scans the device tree for matching clock providers and
2289 * calls their initialization functions
2290 */
2291void __init of_clk_init(const struct of_device_id *matches)
2292{
7f7ed584 2293 const struct of_device_id *match;
766e6a4e
GL
2294 struct device_node *np;
2295
f2f6c255
PG
2296 if (!matches)
2297 matches = __clk_of_table;
2298
7f7ed584 2299 for_each_matching_node_and_match(np, matches, &match) {
766e6a4e
GL
2300 of_clk_init_cb_t clk_init_cb = match->data;
2301 clk_init_cb(np);
2302 }
2303}
2304#endif
This page took 0.211658 seconds and 5 git commands to generate.