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