clk: max77686: Fix return value checking for devm_kzalloc
[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>
b2476490
MT
21
22static DEFINE_SPINLOCK(enable_lock);
23static DEFINE_MUTEX(prepare_lock);
24
25static HLIST_HEAD(clk_root_list);
26static HLIST_HEAD(clk_orphan_list);
27static LIST_HEAD(clk_notifier_list);
28
29/*** debugfs support ***/
30
31#ifdef CONFIG_COMMON_CLK_DEBUG
32#include <linux/debugfs.h>
33
34static struct dentry *rootdir;
35static struct dentry *orphandir;
36static int inited = 0;
37
38/* caller must hold prepare_lock */
39static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
40{
41 struct dentry *d;
42 int ret = -ENOMEM;
43
44 if (!clk || !pdentry) {
45 ret = -EINVAL;
46 goto out;
47 }
48
49 d = debugfs_create_dir(clk->name, pdentry);
50 if (!d)
51 goto out;
52
53 clk->dentry = d;
54
55 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
56 (u32 *)&clk->rate);
57 if (!d)
58 goto err_out;
59
60 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
61 (u32 *)&clk->flags);
62 if (!d)
63 goto err_out;
64
65 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
66 (u32 *)&clk->prepare_count);
67 if (!d)
68 goto err_out;
69
70 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
71 (u32 *)&clk->enable_count);
72 if (!d)
73 goto err_out;
74
75 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
76 (u32 *)&clk->notifier_count);
77 if (!d)
78 goto err_out;
79
80 ret = 0;
81 goto out;
82
83err_out:
84 debugfs_remove(clk->dentry);
85out:
86 return ret;
87}
88
89/* caller must hold prepare_lock */
90static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry)
91{
92 struct clk *child;
93 struct hlist_node *tmp;
94 int ret = -EINVAL;;
95
96 if (!clk || !pdentry)
97 goto out;
98
99 ret = clk_debug_create_one(clk, pdentry);
100
101 if (ret)
102 goto out;
103
104 hlist_for_each_entry(child, tmp, &clk->children, child_node)
105 clk_debug_create_subtree(child, clk->dentry);
106
107 ret = 0;
108out:
109 return ret;
110}
111
112/**
113 * clk_debug_register - add a clk node to the debugfs clk tree
114 * @clk: the clk being added to the debugfs clk tree
115 *
116 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
117 * initialized. Otherwise it bails out early since the debugfs clk tree
118 * will be created lazily by clk_debug_init as part of a late_initcall.
119 *
120 * Caller must hold prepare_lock. Only clk_init calls this function (so
121 * far) so this is taken care.
122 */
123static int clk_debug_register(struct clk *clk)
124{
125 struct clk *parent;
126 struct dentry *pdentry;
127 int ret = 0;
128
129 if (!inited)
130 goto out;
131
132 parent = clk->parent;
133
134 /*
135 * Check to see if a clk is a root clk. Also check that it is
136 * safe to add this clk to debugfs
137 */
138 if (!parent)
139 if (clk->flags & CLK_IS_ROOT)
140 pdentry = rootdir;
141 else
142 pdentry = orphandir;
143 else
144 if (parent->dentry)
145 pdentry = parent->dentry;
146 else
147 goto out;
148
149 ret = clk_debug_create_subtree(clk, pdentry);
150
151out:
152 return ret;
153}
154
155/**
156 * clk_debug_init - lazily create the debugfs clk tree visualization
157 *
158 * clks are often initialized very early during boot before memory can
159 * be dynamically allocated and well before debugfs is setup.
160 * clk_debug_init walks the clk tree hierarchy while holding
161 * prepare_lock and creates the topology as part of a late_initcall,
162 * thus insuring that clks initialized very early will still be
163 * represented in the debugfs clk tree. This function should only be
164 * called once at boot-time, and all other clks added dynamically will
165 * be done so with clk_debug_register.
166 */
167static int __init clk_debug_init(void)
168{
169 struct clk *clk;
170 struct hlist_node *tmp;
171
172 rootdir = debugfs_create_dir("clk", NULL);
173
174 if (!rootdir)
175 return -ENOMEM;
176
177 orphandir = debugfs_create_dir("orphans", rootdir);
178
179 if (!orphandir)
180 return -ENOMEM;
181
182 mutex_lock(&prepare_lock);
183
184 hlist_for_each_entry(clk, tmp, &clk_root_list, child_node)
185 clk_debug_create_subtree(clk, rootdir);
186
187 hlist_for_each_entry(clk, tmp, &clk_orphan_list, child_node)
188 clk_debug_create_subtree(clk, orphandir);
189
190 inited = 1;
191
192 mutex_unlock(&prepare_lock);
193
194 return 0;
195}
196late_initcall(clk_debug_init);
197#else
198static inline int clk_debug_register(struct clk *clk) { return 0; }
70d347e6 199#endif
b2476490 200
b2476490
MT
201/* caller must hold prepare_lock */
202static void clk_disable_unused_subtree(struct clk *clk)
203{
204 struct clk *child;
205 struct hlist_node *tmp;
206 unsigned long flags;
207
208 if (!clk)
209 goto out;
210
211 hlist_for_each_entry(child, tmp, &clk->children, child_node)
212 clk_disable_unused_subtree(child);
213
214 spin_lock_irqsave(&enable_lock, flags);
215
216 if (clk->enable_count)
217 goto unlock_out;
218
219 if (clk->flags & CLK_IGNORE_UNUSED)
220 goto unlock_out;
221
7c045a55
MT
222 /*
223 * some gate clocks have special needs during the disable-unused
224 * sequence. call .disable_unused if available, otherwise fall
225 * back to .disable
226 */
227 if (__clk_is_enabled(clk)) {
228 if (clk->ops->disable_unused)
229 clk->ops->disable_unused(clk->hw);
230 else if (clk->ops->disable)
231 clk->ops->disable(clk->hw);
232 }
b2476490
MT
233
234unlock_out:
235 spin_unlock_irqrestore(&enable_lock, flags);
236
237out:
238 return;
239}
240
241static int clk_disable_unused(void)
242{
243 struct clk *clk;
244 struct hlist_node *tmp;
245
246 mutex_lock(&prepare_lock);
247
248 hlist_for_each_entry(clk, tmp, &clk_root_list, child_node)
249 clk_disable_unused_subtree(clk);
250
251 hlist_for_each_entry(clk, tmp, &clk_orphan_list, child_node)
252 clk_disable_unused_subtree(clk);
253
254 mutex_unlock(&prepare_lock);
255
256 return 0;
257}
258late_initcall(clk_disable_unused);
b2476490
MT
259
260/*** helper functions ***/
261
65800b2c 262const char *__clk_get_name(struct clk *clk)
b2476490
MT
263{
264 return !clk ? NULL : clk->name;
265}
4895084c 266EXPORT_SYMBOL_GPL(__clk_get_name);
b2476490 267
65800b2c 268struct clk_hw *__clk_get_hw(struct clk *clk)
b2476490
MT
269{
270 return !clk ? NULL : clk->hw;
271}
272
65800b2c 273u8 __clk_get_num_parents(struct clk *clk)
b2476490 274{
2ac6b1f5 275 return !clk ? 0 : clk->num_parents;
b2476490
MT
276}
277
65800b2c 278struct clk *__clk_get_parent(struct clk *clk)
b2476490
MT
279{
280 return !clk ? NULL : clk->parent;
281}
282
65800b2c 283unsigned int __clk_get_enable_count(struct clk *clk)
b2476490 284{
2ac6b1f5 285 return !clk ? 0 : clk->enable_count;
b2476490
MT
286}
287
65800b2c 288unsigned int __clk_get_prepare_count(struct clk *clk)
b2476490 289{
2ac6b1f5 290 return !clk ? 0 : clk->prepare_count;
b2476490
MT
291}
292
293unsigned long __clk_get_rate(struct clk *clk)
294{
295 unsigned long ret;
296
297 if (!clk) {
34e44fe8 298 ret = 0;
b2476490
MT
299 goto out;
300 }
301
302 ret = clk->rate;
303
304 if (clk->flags & CLK_IS_ROOT)
305 goto out;
306
307 if (!clk->parent)
34e44fe8 308 ret = 0;
b2476490
MT
309
310out:
311 return ret;
312}
313
65800b2c 314unsigned long __clk_get_flags(struct clk *clk)
b2476490 315{
2ac6b1f5 316 return !clk ? 0 : clk->flags;
b2476490
MT
317}
318
2ac6b1f5 319bool __clk_is_enabled(struct clk *clk)
b2476490
MT
320{
321 int ret;
322
323 if (!clk)
2ac6b1f5 324 return false;
b2476490
MT
325
326 /*
327 * .is_enabled is only mandatory for clocks that gate
328 * fall back to software usage counter if .is_enabled is missing
329 */
330 if (!clk->ops->is_enabled) {
331 ret = clk->enable_count ? 1 : 0;
332 goto out;
333 }
334
335 ret = clk->ops->is_enabled(clk->hw);
336out:
2ac6b1f5 337 return !!ret;
b2476490
MT
338}
339
340static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
341{
342 struct clk *child;
343 struct clk *ret;
344 struct hlist_node *tmp;
345
346 if (!strcmp(clk->name, name))
347 return clk;
348
349 hlist_for_each_entry(child, tmp, &clk->children, child_node) {
350 ret = __clk_lookup_subtree(name, child);
351 if (ret)
352 return ret;
353 }
354
355 return NULL;
356}
357
358struct clk *__clk_lookup(const char *name)
359{
360 struct clk *root_clk;
361 struct clk *ret;
362 struct hlist_node *tmp;
363
364 if (!name)
365 return NULL;
366
367 /* search the 'proper' clk tree first */
368 hlist_for_each_entry(root_clk, tmp, &clk_root_list, child_node) {
369 ret = __clk_lookup_subtree(name, root_clk);
370 if (ret)
371 return ret;
372 }
373
374 /* if not found, then search the orphan tree */
375 hlist_for_each_entry(root_clk, tmp, &clk_orphan_list, child_node) {
376 ret = __clk_lookup_subtree(name, root_clk);
377 if (ret)
378 return ret;
379 }
380
381 return NULL;
382}
383
384/*** clk api ***/
385
386void __clk_unprepare(struct clk *clk)
387{
388 if (!clk)
389 return;
390
391 if (WARN_ON(clk->prepare_count == 0))
392 return;
393
394 if (--clk->prepare_count > 0)
395 return;
396
397 WARN_ON(clk->enable_count > 0);
398
399 if (clk->ops->unprepare)
400 clk->ops->unprepare(clk->hw);
401
402 __clk_unprepare(clk->parent);
403}
404
405/**
406 * clk_unprepare - undo preparation of a clock source
407 * @clk: the clk being unprepare
408 *
409 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
410 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
411 * if the operation may sleep. One example is a clk which is accessed over
412 * I2c. In the complex case a clk gate operation may require a fast and a slow
413 * part. It is this reason that clk_unprepare and clk_disable are not mutually
414 * exclusive. In fact clk_disable must be called before clk_unprepare.
415 */
416void clk_unprepare(struct clk *clk)
417{
418 mutex_lock(&prepare_lock);
419 __clk_unprepare(clk);
420 mutex_unlock(&prepare_lock);
421}
422EXPORT_SYMBOL_GPL(clk_unprepare);
423
424int __clk_prepare(struct clk *clk)
425{
426 int ret = 0;
427
428 if (!clk)
429 return 0;
430
431 if (clk->prepare_count == 0) {
432 ret = __clk_prepare(clk->parent);
433 if (ret)
434 return ret;
435
436 if (clk->ops->prepare) {
437 ret = clk->ops->prepare(clk->hw);
438 if (ret) {
439 __clk_unprepare(clk->parent);
440 return ret;
441 }
442 }
443 }
444
445 clk->prepare_count++;
446
447 return 0;
448}
449
450/**
451 * clk_prepare - prepare a clock source
452 * @clk: the clk being prepared
453 *
454 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
455 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
456 * operation may sleep. One example is a clk which is accessed over I2c. In
457 * the complex case a clk ungate operation may require a fast and a slow part.
458 * It is this reason that clk_prepare and clk_enable are not mutually
459 * exclusive. In fact clk_prepare must be called before clk_enable.
460 * Returns 0 on success, -EERROR otherwise.
461 */
462int clk_prepare(struct clk *clk)
463{
464 int ret;
465
466 mutex_lock(&prepare_lock);
467 ret = __clk_prepare(clk);
468 mutex_unlock(&prepare_lock);
469
470 return ret;
471}
472EXPORT_SYMBOL_GPL(clk_prepare);
473
474static void __clk_disable(struct clk *clk)
475{
476 if (!clk)
477 return;
478
e47c6a34
FW
479 if (WARN_ON(IS_ERR(clk)))
480 return;
481
b2476490
MT
482 if (WARN_ON(clk->enable_count == 0))
483 return;
484
485 if (--clk->enable_count > 0)
486 return;
487
488 if (clk->ops->disable)
489 clk->ops->disable(clk->hw);
490
491 __clk_disable(clk->parent);
492}
493
494/**
495 * clk_disable - gate a clock
496 * @clk: the clk being gated
497 *
498 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
499 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
500 * clk if the operation is fast and will never sleep. One example is a
501 * SoC-internal clk which is controlled via simple register writes. In the
502 * complex case a clk gate operation may require a fast and a slow part. It is
503 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
504 * In fact clk_disable must be called before clk_unprepare.
505 */
506void clk_disable(struct clk *clk)
507{
508 unsigned long flags;
509
510 spin_lock_irqsave(&enable_lock, flags);
511 __clk_disable(clk);
512 spin_unlock_irqrestore(&enable_lock, flags);
513}
514EXPORT_SYMBOL_GPL(clk_disable);
515
516static int __clk_enable(struct clk *clk)
517{
518 int ret = 0;
519
520 if (!clk)
521 return 0;
522
523 if (WARN_ON(clk->prepare_count == 0))
524 return -ESHUTDOWN;
525
526 if (clk->enable_count == 0) {
527 ret = __clk_enable(clk->parent);
528
529 if (ret)
530 return ret;
531
532 if (clk->ops->enable) {
533 ret = clk->ops->enable(clk->hw);
534 if (ret) {
535 __clk_disable(clk->parent);
536 return ret;
537 }
538 }
539 }
540
541 clk->enable_count++;
542 return 0;
543}
544
545/**
546 * clk_enable - ungate a clock
547 * @clk: the clk being ungated
548 *
549 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
550 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
551 * if the operation will never sleep. One example is a SoC-internal clk which
552 * is controlled via simple register writes. In the complex case a clk ungate
553 * operation may require a fast and a slow part. It is this reason that
554 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
555 * must be called before clk_enable. Returns 0 on success, -EERROR
556 * otherwise.
557 */
558int clk_enable(struct clk *clk)
559{
560 unsigned long flags;
561 int ret;
562
563 spin_lock_irqsave(&enable_lock, flags);
564 ret = __clk_enable(clk);
565 spin_unlock_irqrestore(&enable_lock, flags);
566
567 return ret;
568}
569EXPORT_SYMBOL_GPL(clk_enable);
570
b2476490
MT
571/**
572 * __clk_round_rate - round the given rate for a clk
573 * @clk: round the rate of this clock
574 *
575 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
576 */
577unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
578{
81536e07 579 unsigned long parent_rate = 0;
b2476490
MT
580
581 if (!clk)
2ac6b1f5 582 return 0;
b2476490 583
f4d8af2e
SG
584 if (!clk->ops->round_rate) {
585 if (clk->flags & CLK_SET_RATE_PARENT)
586 return __clk_round_rate(clk->parent, rate);
587 else
588 return clk->rate;
589 }
b2476490 590
81536e07
SG
591 if (clk->parent)
592 parent_rate = clk->parent->rate;
593
594 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
b2476490
MT
595}
596
597/**
598 * clk_round_rate - round the given rate for a clk
599 * @clk: the clk for which we are rounding a rate
600 * @rate: the rate which is to be rounded
601 *
602 * Takes in a rate as input and rounds it to a rate that the clk can actually
603 * use which is then returned. If clk doesn't support round_rate operation
604 * then the parent rate is returned.
605 */
606long clk_round_rate(struct clk *clk, unsigned long rate)
607{
608 unsigned long ret;
609
610 mutex_lock(&prepare_lock);
611 ret = __clk_round_rate(clk, rate);
612 mutex_unlock(&prepare_lock);
613
614 return ret;
615}
616EXPORT_SYMBOL_GPL(clk_round_rate);
617
618/**
619 * __clk_notify - call clk notifier chain
620 * @clk: struct clk * that is changing rate
621 * @msg: clk notifier type (see include/linux/clk.h)
622 * @old_rate: old clk rate
623 * @new_rate: new clk rate
624 *
625 * Triggers a notifier call chain on the clk rate-change notification
626 * for 'clk'. Passes a pointer to the struct clk and the previous
627 * and current rates to the notifier callback. Intended to be called by
628 * internal clock code only. Returns NOTIFY_DONE from the last driver
629 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
630 * a driver returns that.
631 */
632static int __clk_notify(struct clk *clk, unsigned long msg,
633 unsigned long old_rate, unsigned long new_rate)
634{
635 struct clk_notifier *cn;
636 struct clk_notifier_data cnd;
637 int ret = NOTIFY_DONE;
638
639 cnd.clk = clk;
640 cnd.old_rate = old_rate;
641 cnd.new_rate = new_rate;
642
643 list_for_each_entry(cn, &clk_notifier_list, node) {
644 if (cn->clk == clk) {
645 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
646 &cnd);
647 break;
648 }
649 }
650
651 return ret;
652}
653
654/**
655 * __clk_recalc_rates
656 * @clk: first clk in the subtree
657 * @msg: notification type (see include/linux/clk.h)
658 *
659 * Walks the subtree of clks starting with clk and recalculates rates as it
660 * goes. Note that if a clk does not implement the .recalc_rate callback then
661 * it is assumed that the clock will take on the rate of it's parent.
662 *
663 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
664 * if necessary.
665 *
666 * Caller must hold prepare_lock.
667 */
668static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
669{
670 unsigned long old_rate;
671 unsigned long parent_rate = 0;
672 struct hlist_node *tmp;
673 struct clk *child;
674
675 old_rate = clk->rate;
676
677 if (clk->parent)
678 parent_rate = clk->parent->rate;
679
680 if (clk->ops->recalc_rate)
681 clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
682 else
683 clk->rate = parent_rate;
684
685 /*
686 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
687 * & ABORT_RATE_CHANGE notifiers
688 */
689 if (clk->notifier_count && msg)
690 __clk_notify(clk, msg, old_rate, clk->rate);
691
692 hlist_for_each_entry(child, tmp, &clk->children, child_node)
693 __clk_recalc_rates(child, msg);
694}
695
a093bde2
UH
696/**
697 * clk_get_rate - return the rate of clk
698 * @clk: the clk whose rate is being returned
699 *
700 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
701 * is set, which means a recalc_rate will be issued.
702 * If clk is NULL then returns 0.
703 */
704unsigned long clk_get_rate(struct clk *clk)
705{
706 unsigned long rate;
707
708 mutex_lock(&prepare_lock);
709
710 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
711 __clk_recalc_rates(clk, 0);
712
713 rate = __clk_get_rate(clk);
714 mutex_unlock(&prepare_lock);
715
716 return rate;
717}
718EXPORT_SYMBOL_GPL(clk_get_rate);
719
b2476490
MT
720/**
721 * __clk_speculate_rates
722 * @clk: first clk in the subtree
723 * @parent_rate: the "future" rate of clk's parent
724 *
725 * Walks the subtree of clks starting with clk, speculating rates as it
726 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
727 *
728 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
729 * pre-rate change notifications and returns early if no clks in the
730 * subtree have subscribed to the notifications. Note that if a clk does not
731 * implement the .recalc_rate callback then it is assumed that the clock will
732 * take on the rate of it's parent.
733 *
734 * Caller must hold prepare_lock.
735 */
736static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
737{
738 struct hlist_node *tmp;
739 struct clk *child;
740 unsigned long new_rate;
741 int ret = NOTIFY_DONE;
742
743 if (clk->ops->recalc_rate)
744 new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
745 else
746 new_rate = parent_rate;
747
748 /* abort the rate change if a driver returns NOTIFY_BAD */
749 if (clk->notifier_count)
750 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
751
752 if (ret == NOTIFY_BAD)
753 goto out;
754
755 hlist_for_each_entry(child, tmp, &clk->children, child_node) {
756 ret = __clk_speculate_rates(child, new_rate);
757 if (ret == NOTIFY_BAD)
758 break;
759 }
760
761out:
762 return ret;
763}
764
765static void clk_calc_subtree(struct clk *clk, unsigned long new_rate)
766{
767 struct clk *child;
768 struct hlist_node *tmp;
769
770 clk->new_rate = new_rate;
771
772 hlist_for_each_entry(child, tmp, &clk->children, child_node) {
773 if (child->ops->recalc_rate)
774 child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
775 else
776 child->new_rate = new_rate;
777 clk_calc_subtree(child, child->new_rate);
778 }
779}
780
781/*
782 * calculate the new rates returning the topmost clock that has to be
783 * changed.
784 */
785static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
786{
787 struct clk *top = clk;
81536e07 788 unsigned long best_parent_rate = 0;
b2476490
MT
789 unsigned long new_rate;
790
7452b219
MT
791 /* sanity */
792 if (IS_ERR_OR_NULL(clk))
793 return NULL;
794
63f5c3b2
MT
795 /* save parent rate, if it exists */
796 if (clk->parent)
797 best_parent_rate = clk->parent->rate;
798
7452b219
MT
799 /* never propagate up to the parent */
800 if (!(clk->flags & CLK_SET_RATE_PARENT)) {
801 if (!clk->ops->round_rate) {
802 clk->new_rate = clk->rate;
803 return NULL;
7452b219 804 }
63f5c3b2
MT
805 new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
806 goto out;
7452b219
MT
807 }
808
809 /* need clk->parent from here on out */
810 if (!clk->parent) {
811 pr_debug("%s: %s has NULL parent\n", __func__, clk->name);
b2476490
MT
812 return NULL;
813 }
814
7452b219 815 if (!clk->ops->round_rate) {
b2476490 816 top = clk_calc_new_rates(clk->parent, rate);
1b2f9903 817 new_rate = clk->parent->new_rate;
b2476490
MT
818
819 goto out;
820 }
821
7452b219 822 new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
b2476490
MT
823
824 if (best_parent_rate != clk->parent->rate) {
825 top = clk_calc_new_rates(clk->parent, best_parent_rate);
826
827 goto out;
828 }
829
830out:
831 clk_calc_subtree(clk, new_rate);
832
833 return top;
834}
835
836/*
837 * Notify about rate changes in a subtree. Always walk down the whole tree
838 * so that in case of an error we can walk down the whole tree again and
839 * abort the change.
840 */
841static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
842{
843 struct hlist_node *tmp;
844 struct clk *child, *fail_clk = NULL;
845 int ret = NOTIFY_DONE;
846
847 if (clk->rate == clk->new_rate)
848 return 0;
849
850 if (clk->notifier_count) {
851 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
852 if (ret == NOTIFY_BAD)
853 fail_clk = clk;
854 }
855
856 hlist_for_each_entry(child, tmp, &clk->children, child_node) {
857 clk = clk_propagate_rate_change(child, event);
858 if (clk)
859 fail_clk = clk;
860 }
861
862 return fail_clk;
863}
864
865/*
866 * walk down a subtree and set the new rates notifying the rate
867 * change on the way
868 */
869static void clk_change_rate(struct clk *clk)
870{
871 struct clk *child;
872 unsigned long old_rate;
bf47b4fd 873 unsigned long best_parent_rate = 0;
b2476490
MT
874 struct hlist_node *tmp;
875
876 old_rate = clk->rate;
877
bf47b4fd
PM
878 if (clk->parent)
879 best_parent_rate = clk->parent->rate;
880
b2476490 881 if (clk->ops->set_rate)
bf47b4fd 882 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
b2476490
MT
883
884 if (clk->ops->recalc_rate)
bf47b4fd 885 clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate);
b2476490 886 else
bf47b4fd 887 clk->rate = best_parent_rate;
b2476490
MT
888
889 if (clk->notifier_count && old_rate != clk->rate)
890 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
891
892 hlist_for_each_entry(child, tmp, &clk->children, child_node)
893 clk_change_rate(child);
894}
895
896/**
897 * clk_set_rate - specify a new rate for clk
898 * @clk: the clk whose rate is being changed
899 * @rate: the new rate for clk
900 *
5654dc94 901 * In the simplest case clk_set_rate will only adjust the rate of clk.
b2476490 902 *
5654dc94
MT
903 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
904 * propagate up to clk's parent; whether or not this happens depends on the
905 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
906 * after calling .round_rate then upstream parent propagation is ignored. If
907 * *parent_rate comes back with a new rate for clk's parent then we propagate
908 * up to clk's parent and set it's rate. Upward propagation will continue
909 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
910 * .round_rate stops requesting changes to clk's parent_rate.
b2476490 911 *
5654dc94
MT
912 * Rate changes are accomplished via tree traversal that also recalculates the
913 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
b2476490
MT
914 *
915 * Returns 0 on success, -EERROR otherwise.
916 */
917int clk_set_rate(struct clk *clk, unsigned long rate)
918{
919 struct clk *top, *fail_clk;
920 int ret = 0;
921
922 /* prevent racing with updates to the clock topology */
923 mutex_lock(&prepare_lock);
924
925 /* bail early if nothing to do */
926 if (rate == clk->rate)
927 goto out;
928
7e0fa1b5 929 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
0e1c0301
VK
930 ret = -EBUSY;
931 goto out;
932 }
933
b2476490
MT
934 /* calculate new rates and get the topmost changed clock */
935 top = clk_calc_new_rates(clk, rate);
936 if (!top) {
937 ret = -EINVAL;
938 goto out;
939 }
940
941 /* notify that we are about to change rates */
942 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
943 if (fail_clk) {
944 pr_warn("%s: failed to set %s rate\n", __func__,
945 fail_clk->name);
946 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
947 ret = -EBUSY;
948 goto out;
949 }
950
951 /* change the rates */
952 clk_change_rate(top);
953
954 mutex_unlock(&prepare_lock);
955
956 return 0;
957out:
958 mutex_unlock(&prepare_lock);
959
960 return ret;
961}
962EXPORT_SYMBOL_GPL(clk_set_rate);
963
964/**
965 * clk_get_parent - return the parent of a clk
966 * @clk: the clk whose parent gets returned
967 *
968 * Simply returns clk->parent. Returns NULL if clk is NULL.
969 */
970struct clk *clk_get_parent(struct clk *clk)
971{
972 struct clk *parent;
973
974 mutex_lock(&prepare_lock);
975 parent = __clk_get_parent(clk);
976 mutex_unlock(&prepare_lock);
977
978 return parent;
979}
980EXPORT_SYMBOL_GPL(clk_get_parent);
981
982/*
983 * .get_parent is mandatory for clocks with multiple possible parents. It is
984 * optional for single-parent clocks. Always call .get_parent if it is
985 * available and WARN if it is missing for multi-parent clocks.
986 *
987 * For single-parent clocks without .get_parent, first check to see if the
988 * .parents array exists, and if so use it to avoid an expensive tree
989 * traversal. If .parents does not exist then walk the tree with __clk_lookup.
990 */
991static struct clk *__clk_init_parent(struct clk *clk)
992{
993 struct clk *ret = NULL;
994 u8 index;
995
996 /* handle the trivial cases */
997
998 if (!clk->num_parents)
999 goto out;
1000
1001 if (clk->num_parents == 1) {
1002 if (IS_ERR_OR_NULL(clk->parent))
1003 ret = clk->parent = __clk_lookup(clk->parent_names[0]);
1004 ret = clk->parent;
1005 goto out;
1006 }
1007
1008 if (!clk->ops->get_parent) {
1009 WARN(!clk->ops->get_parent,
1010 "%s: multi-parent clocks must implement .get_parent\n",
1011 __func__);
1012 goto out;
1013 };
1014
1015 /*
1016 * Do our best to cache parent clocks in clk->parents. This prevents
1017 * unnecessary and expensive calls to __clk_lookup. We don't set
1018 * clk->parent here; that is done by the calling function
1019 */
1020
1021 index = clk->ops->get_parent(clk->hw);
1022
1023 if (!clk->parents)
1024 clk->parents =
7975059d 1025 kzalloc((sizeof(struct clk*) * clk->num_parents),
b2476490
MT
1026 GFP_KERNEL);
1027
1028 if (!clk->parents)
1029 ret = __clk_lookup(clk->parent_names[index]);
1030 else if (!clk->parents[index])
1031 ret = clk->parents[index] =
1032 __clk_lookup(clk->parent_names[index]);
1033 else
1034 ret = clk->parents[index];
1035
1036out:
1037 return ret;
1038}
1039
1040void __clk_reparent(struct clk *clk, struct clk *new_parent)
1041{
1042#ifdef CONFIG_COMMON_CLK_DEBUG
1043 struct dentry *d;
1044 struct dentry *new_parent_d;
1045#endif
1046
1047 if (!clk || !new_parent)
1048 return;
1049
1050 hlist_del(&clk->child_node);
1051
1052 if (new_parent)
1053 hlist_add_head(&clk->child_node, &new_parent->children);
1054 else
1055 hlist_add_head(&clk->child_node, &clk_orphan_list);
1056
1057#ifdef CONFIG_COMMON_CLK_DEBUG
1058 if (!inited)
1059 goto out;
1060
1061 if (new_parent)
1062 new_parent_d = new_parent->dentry;
1063 else
1064 new_parent_d = orphandir;
1065
1066 d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
1067 new_parent_d, clk->name);
1068 if (d)
1069 clk->dentry = d;
1070 else
1071 pr_debug("%s: failed to rename debugfs entry for %s\n",
1072 __func__, clk->name);
1073out:
1074#endif
1075
1076 clk->parent = new_parent;
1077
1078 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1079}
1080
1081static int __clk_set_parent(struct clk *clk, struct clk *parent)
1082{
1083 struct clk *old_parent;
1084 unsigned long flags;
1085 int ret = -EINVAL;
1086 u8 i;
1087
1088 old_parent = clk->parent;
1089
863b1327 1090 if (!clk->parents)
7975059d
RN
1091 clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
1092 GFP_KERNEL);
b2476490
MT
1093
1094 /*
863b1327
RN
1095 * find index of new parent clock using cached parent ptrs,
1096 * or if not yet cached, use string name comparison and cache
1097 * them now to avoid future calls to __clk_lookup.
b2476490 1098 */
863b1327
RN
1099 for (i = 0; i < clk->num_parents; i++) {
1100 if (clk->parents && clk->parents[i] == parent)
1101 break;
1102 else if (!strcmp(clk->parent_names[i], parent->name)) {
1103 if (clk->parents)
1104 clk->parents[i] = __clk_lookup(parent->name);
1105 break;
1106 }
1107 }
b2476490
MT
1108
1109 if (i == clk->num_parents) {
1110 pr_debug("%s: clock %s is not a possible parent of clock %s\n",
1111 __func__, parent->name, clk->name);
1112 goto out;
1113 }
1114
1115 /* migrate prepare and enable */
1116 if (clk->prepare_count)
1117 __clk_prepare(parent);
1118
1119 /* FIXME replace with clk_is_enabled(clk) someday */
1120 spin_lock_irqsave(&enable_lock, flags);
1121 if (clk->enable_count)
1122 __clk_enable(parent);
1123 spin_unlock_irqrestore(&enable_lock, flags);
1124
1125 /* change clock input source */
1126 ret = clk->ops->set_parent(clk->hw, i);
1127
1128 /* clean up old prepare and enable */
1129 spin_lock_irqsave(&enable_lock, flags);
1130 if (clk->enable_count)
1131 __clk_disable(old_parent);
1132 spin_unlock_irqrestore(&enable_lock, flags);
1133
1134 if (clk->prepare_count)
1135 __clk_unprepare(old_parent);
1136
1137out:
1138 return ret;
1139}
1140
1141/**
1142 * clk_set_parent - switch the parent of a mux clk
1143 * @clk: the mux clk whose input we are switching
1144 * @parent: the new input to clk
1145 *
1146 * Re-parent clk to use parent as it's new input source. If clk has the
1147 * CLK_SET_PARENT_GATE flag set then clk must be gated for this
1148 * operation to succeed. After successfully changing clk's parent
1149 * clk_set_parent will update the clk topology, sysfs topology and
1150 * propagate rate recalculation via __clk_recalc_rates. Returns 0 on
1151 * success, -EERROR otherwise.
1152 */
1153int clk_set_parent(struct clk *clk, struct clk *parent)
1154{
1155 int ret = 0;
1156
1157 if (!clk || !clk->ops)
1158 return -EINVAL;
1159
1160 if (!clk->ops->set_parent)
1161 return -ENOSYS;
1162
1163 /* prevent racing with updates to the clock topology */
1164 mutex_lock(&prepare_lock);
1165
1166 if (clk->parent == parent)
1167 goto out;
1168
1169 /* propagate PRE_RATE_CHANGE notifications */
1170 if (clk->notifier_count)
1171 ret = __clk_speculate_rates(clk, parent->rate);
1172
1173 /* abort if a driver objects */
1174 if (ret == NOTIFY_STOP)
1175 goto out;
1176
1177 /* only re-parent if the clock is not in use */
1178 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count)
1179 ret = -EBUSY;
1180 else
1181 ret = __clk_set_parent(clk, parent);
1182
1183 /* propagate ABORT_RATE_CHANGE if .set_parent failed */
1184 if (ret) {
1185 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
1186 goto out;
1187 }
1188
1189 /* propagate rate recalculation downstream */
1190 __clk_reparent(clk, parent);
1191
1192out:
1193 mutex_unlock(&prepare_lock);
1194
1195 return ret;
1196}
1197EXPORT_SYMBOL_GPL(clk_set_parent);
1198
1199/**
1200 * __clk_init - initialize the data structures in a struct clk
1201 * @dev: device initializing this clk, placeholder for now
1202 * @clk: clk being initialized
1203 *
1204 * Initializes the lists in struct clk, queries the hardware for the
1205 * parent and rate and sets them both.
b2476490 1206 */
d1302a36 1207int __clk_init(struct device *dev, struct clk *clk)
b2476490 1208{
d1302a36 1209 int i, ret = 0;
b2476490
MT
1210 struct clk *orphan;
1211 struct hlist_node *tmp, *tmp2;
1212
1213 if (!clk)
d1302a36 1214 return -EINVAL;
b2476490
MT
1215
1216 mutex_lock(&prepare_lock);
1217
1218 /* check to see if a clock with this name is already registered */
d1302a36
MT
1219 if (__clk_lookup(clk->name)) {
1220 pr_debug("%s: clk %s already initialized\n",
1221 __func__, clk->name);
1222 ret = -EEXIST;
b2476490 1223 goto out;
d1302a36 1224 }
b2476490 1225
d4d7e3dd
MT
1226 /* check that clk_ops are sane. See Documentation/clk.txt */
1227 if (clk->ops->set_rate &&
1228 !(clk->ops->round_rate && clk->ops->recalc_rate)) {
1229 pr_warning("%s: %s must implement .round_rate & .recalc_rate\n",
1230 __func__, clk->name);
d1302a36 1231 ret = -EINVAL;
d4d7e3dd
MT
1232 goto out;
1233 }
1234
1235 if (clk->ops->set_parent && !clk->ops->get_parent) {
1236 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
1237 __func__, clk->name);
d1302a36 1238 ret = -EINVAL;
d4d7e3dd
MT
1239 goto out;
1240 }
1241
b2476490
MT
1242 /* throw a WARN if any entries in parent_names are NULL */
1243 for (i = 0; i < clk->num_parents; i++)
1244 WARN(!clk->parent_names[i],
1245 "%s: invalid NULL in %s's .parent_names\n",
1246 __func__, clk->name);
1247
1248 /*
1249 * Allocate an array of struct clk *'s to avoid unnecessary string
1250 * look-ups of clk's possible parents. This can fail for clocks passed
1251 * in to clk_init during early boot; thus any access to clk->parents[]
1252 * must always check for a NULL pointer and try to populate it if
1253 * necessary.
1254 *
1255 * If clk->parents is not NULL we skip this entire block. This allows
1256 * for clock drivers to statically initialize clk->parents.
1257 */
9ca1c5a4
RN
1258 if (clk->num_parents > 1 && !clk->parents) {
1259 clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
b2476490
MT
1260 GFP_KERNEL);
1261 /*
1262 * __clk_lookup returns NULL for parents that have not been
1263 * clk_init'd; thus any access to clk->parents[] must check
1264 * for a NULL pointer. We can always perform lazy lookups for
1265 * missing parents later on.
1266 */
1267 if (clk->parents)
1268 for (i = 0; i < clk->num_parents; i++)
1269 clk->parents[i] =
1270 __clk_lookup(clk->parent_names[i]);
1271 }
1272
1273 clk->parent = __clk_init_parent(clk);
1274
1275 /*
1276 * Populate clk->parent if parent has already been __clk_init'd. If
1277 * parent has not yet been __clk_init'd then place clk in the orphan
1278 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
1279 * clk list.
1280 *
1281 * Every time a new clk is clk_init'd then we walk the list of orphan
1282 * clocks and re-parent any that are children of the clock currently
1283 * being clk_init'd.
1284 */
1285 if (clk->parent)
1286 hlist_add_head(&clk->child_node,
1287 &clk->parent->children);
1288 else if (clk->flags & CLK_IS_ROOT)
1289 hlist_add_head(&clk->child_node, &clk_root_list);
1290 else
1291 hlist_add_head(&clk->child_node, &clk_orphan_list);
1292
1293 /*
1294 * Set clk's rate. The preferred method is to use .recalc_rate. For
1295 * simple clocks and lazy developers the default fallback is to use the
1296 * parent's rate. If a clock doesn't have a parent (or is orphaned)
1297 * then rate is set to zero.
1298 */
1299 if (clk->ops->recalc_rate)
1300 clk->rate = clk->ops->recalc_rate(clk->hw,
1301 __clk_get_rate(clk->parent));
1302 else if (clk->parent)
1303 clk->rate = clk->parent->rate;
1304 else
1305 clk->rate = 0;
1306
1307 /*
1308 * walk the list of orphan clocks and reparent any that are children of
1309 * this clock
1310 */
1f61e5f1
MF
1311 hlist_for_each_entry_safe(orphan, tmp, tmp2, &clk_orphan_list, child_node) {
1312 if (orphan->ops->get_parent) {
1313 i = orphan->ops->get_parent(orphan->hw);
1314 if (!strcmp(clk->name, orphan->parent_names[i]))
1315 __clk_reparent(orphan, clk);
1316 continue;
1317 }
1318
b2476490
MT
1319 for (i = 0; i < orphan->num_parents; i++)
1320 if (!strcmp(clk->name, orphan->parent_names[i])) {
1321 __clk_reparent(orphan, clk);
1322 break;
1323 }
1f61e5f1 1324 }
b2476490
MT
1325
1326 /*
1327 * optional platform-specific magic
1328 *
1329 * The .init callback is not used by any of the basic clock types, but
1330 * exists for weird hardware that must perform initialization magic.
1331 * Please consider other ways of solving initialization problems before
1332 * using this callback, as it's use is discouraged.
1333 */
1334 if (clk->ops->init)
1335 clk->ops->init(clk->hw);
1336
1337 clk_debug_register(clk);
1338
1339out:
1340 mutex_unlock(&prepare_lock);
1341
d1302a36 1342 return ret;
b2476490
MT
1343}
1344
0197b3ea
SK
1345/**
1346 * __clk_register - register a clock and return a cookie.
1347 *
1348 * Same as clk_register, except that the .clk field inside hw shall point to a
1349 * preallocated (generally statically allocated) struct clk. None of the fields
1350 * of the struct clk need to be initialized.
1351 *
1352 * The data pointed to by .init and .clk field shall NOT be marked as init
1353 * data.
1354 *
1355 * __clk_register is only exposed via clk-private.h and is intended for use with
1356 * very large numbers of clocks that need to be statically initialized. It is
1357 * a layering violation to include clk-private.h from any code which implements
1358 * a clock's .ops; as such any statically initialized clock data MUST be in a
1359 * separate C file from the logic that implements it's operations. Returns 0
1360 * on success, otherwise an error code.
1361 */
1362struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
1363{
1364 int ret;
1365 struct clk *clk;
1366
1367 clk = hw->clk;
1368 clk->name = hw->init->name;
1369 clk->ops = hw->init->ops;
1370 clk->hw = hw;
1371 clk->flags = hw->init->flags;
1372 clk->parent_names = hw->init->parent_names;
1373 clk->num_parents = hw->init->num_parents;
1374
1375 ret = __clk_init(dev, clk);
1376 if (ret)
1377 return ERR_PTR(ret);
1378
1379 return clk;
1380}
1381EXPORT_SYMBOL_GPL(__clk_register);
1382
46c8773a 1383static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
b2476490 1384{
d1302a36 1385 int i, ret;
b2476490 1386
0197b3ea
SK
1387 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
1388 if (!clk->name) {
1389 pr_err("%s: could not allocate clk->name\n", __func__);
1390 ret = -ENOMEM;
1391 goto fail_name;
1392 }
1393 clk->ops = hw->init->ops;
b2476490 1394 clk->hw = hw;
0197b3ea
SK
1395 clk->flags = hw->init->flags;
1396 clk->num_parents = hw->init->num_parents;
b2476490
MT
1397 hw->clk = clk;
1398
d1302a36 1399 /* allocate local copy in case parent_names is __initdata */
0197b3ea 1400 clk->parent_names = kzalloc((sizeof(char*) * clk->num_parents),
d1302a36
MT
1401 GFP_KERNEL);
1402
1403 if (!clk->parent_names) {
1404 pr_err("%s: could not allocate clk->parent_names\n", __func__);
1405 ret = -ENOMEM;
1406 goto fail_parent_names;
1407 }
1408
1409
1410 /* copy each string name in case parent_names is __initdata */
0197b3ea
SK
1411 for (i = 0; i < clk->num_parents; i++) {
1412 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
1413 GFP_KERNEL);
d1302a36
MT
1414 if (!clk->parent_names[i]) {
1415 pr_err("%s: could not copy parent_names\n", __func__);
1416 ret = -ENOMEM;
1417 goto fail_parent_names_copy;
1418 }
1419 }
1420
1421 ret = __clk_init(dev, clk);
1422 if (!ret)
46c8773a 1423 return 0;
b2476490 1424
d1302a36
MT
1425fail_parent_names_copy:
1426 while (--i >= 0)
1427 kfree(clk->parent_names[i]);
1428 kfree(clk->parent_names);
1429fail_parent_names:
0197b3ea
SK
1430 kfree(clk->name);
1431fail_name:
46c8773a
SB
1432 return ret;
1433}
1434
1435/**
1436 * clk_register - allocate a new clock, register it and return an opaque cookie
1437 * @dev: device that is registering this clock
1438 * @hw: link to hardware-specific clock data
1439 *
1440 * clk_register is the primary interface for populating the clock tree with new
1441 * clock nodes. It returns a pointer to the newly allocated struct clk which
1442 * cannot be dereferenced by driver code but may be used in conjuction with the
1443 * rest of the clock API. In the event of an error clk_register will return an
1444 * error code; drivers must test for an error code after calling clk_register.
1445 */
1446struct clk *clk_register(struct device *dev, struct clk_hw *hw)
1447{
1448 int ret;
1449 struct clk *clk;
1450
1451 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
1452 if (!clk) {
1453 pr_err("%s: could not allocate clk\n", __func__);
1454 ret = -ENOMEM;
1455 goto fail_out;
1456 }
1457
1458 ret = _clk_register(dev, hw, clk);
1459 if (!ret)
1460 return clk;
1461
d1302a36
MT
1462 kfree(clk);
1463fail_out:
1464 return ERR_PTR(ret);
b2476490
MT
1465}
1466EXPORT_SYMBOL_GPL(clk_register);
1467
1df5c939
MB
1468/**
1469 * clk_unregister - unregister a currently registered clock
1470 * @clk: clock to unregister
1471 *
1472 * Currently unimplemented.
1473 */
1474void clk_unregister(struct clk *clk) {}
1475EXPORT_SYMBOL_GPL(clk_unregister);
1476
46c8773a
SB
1477static void devm_clk_release(struct device *dev, void *res)
1478{
1479 clk_unregister(res);
1480}
1481
1482/**
1483 * devm_clk_register - resource managed clk_register()
1484 * @dev: device that is registering this clock
1485 * @hw: link to hardware-specific clock data
1486 *
1487 * Managed clk_register(). Clocks returned from this function are
1488 * automatically clk_unregister()ed on driver detach. See clk_register() for
1489 * more information.
1490 */
1491struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
1492{
1493 struct clk *clk;
1494 int ret;
1495
1496 clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL);
1497 if (!clk)
1498 return ERR_PTR(-ENOMEM);
1499
1500 ret = _clk_register(dev, hw, clk);
1501 if (!ret) {
1502 devres_add(dev, clk);
1503 } else {
1504 devres_free(clk);
1505 clk = ERR_PTR(ret);
1506 }
1507
1508 return clk;
1509}
1510EXPORT_SYMBOL_GPL(devm_clk_register);
1511
1512static int devm_clk_match(struct device *dev, void *res, void *data)
1513{
1514 struct clk *c = res;
1515 if (WARN_ON(!c))
1516 return 0;
1517 return c == data;
1518}
1519
1520/**
1521 * devm_clk_unregister - resource managed clk_unregister()
1522 * @clk: clock to unregister
1523 *
1524 * Deallocate a clock allocated with devm_clk_register(). Normally
1525 * this function will not need to be called and the resource management
1526 * code will ensure that the resource is freed.
1527 */
1528void devm_clk_unregister(struct device *dev, struct clk *clk)
1529{
1530 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
1531}
1532EXPORT_SYMBOL_GPL(devm_clk_unregister);
1533
b2476490
MT
1534/*** clk rate change notifiers ***/
1535
1536/**
1537 * clk_notifier_register - add a clk rate change notifier
1538 * @clk: struct clk * to watch
1539 * @nb: struct notifier_block * with callback info
1540 *
1541 * Request notification when clk's rate changes. This uses an SRCU
1542 * notifier because we want it to block and notifier unregistrations are
1543 * uncommon. The callbacks associated with the notifier must not
1544 * re-enter into the clk framework by calling any top-level clk APIs;
1545 * this will cause a nested prepare_lock mutex.
1546 *
1547 * Pre-change notifier callbacks will be passed the current, pre-change
1548 * rate of the clk via struct clk_notifier_data.old_rate. The new,
1549 * post-change rate of the clk is passed via struct
1550 * clk_notifier_data.new_rate.
1551 *
1552 * Post-change notifiers will pass the now-current, post-change rate of
1553 * the clk in both struct clk_notifier_data.old_rate and struct
1554 * clk_notifier_data.new_rate.
1555 *
1556 * Abort-change notifiers are effectively the opposite of pre-change
1557 * notifiers: the original pre-change clk rate is passed in via struct
1558 * clk_notifier_data.new_rate and the failed post-change rate is passed
1559 * in via struct clk_notifier_data.old_rate.
1560 *
1561 * clk_notifier_register() must be called from non-atomic context.
1562 * Returns -EINVAL if called with null arguments, -ENOMEM upon
1563 * allocation failure; otherwise, passes along the return value of
1564 * srcu_notifier_chain_register().
1565 */
1566int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
1567{
1568 struct clk_notifier *cn;
1569 int ret = -ENOMEM;
1570
1571 if (!clk || !nb)
1572 return -EINVAL;
1573
1574 mutex_lock(&prepare_lock);
1575
1576 /* search the list of notifiers for this clk */
1577 list_for_each_entry(cn, &clk_notifier_list, node)
1578 if (cn->clk == clk)
1579 break;
1580
1581 /* if clk wasn't in the notifier list, allocate new clk_notifier */
1582 if (cn->clk != clk) {
1583 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
1584 if (!cn)
1585 goto out;
1586
1587 cn->clk = clk;
1588 srcu_init_notifier_head(&cn->notifier_head);
1589
1590 list_add(&cn->node, &clk_notifier_list);
1591 }
1592
1593 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
1594
1595 clk->notifier_count++;
1596
1597out:
1598 mutex_unlock(&prepare_lock);
1599
1600 return ret;
1601}
1602EXPORT_SYMBOL_GPL(clk_notifier_register);
1603
1604/**
1605 * clk_notifier_unregister - remove a clk rate change notifier
1606 * @clk: struct clk *
1607 * @nb: struct notifier_block * with callback info
1608 *
1609 * Request no further notification for changes to 'clk' and frees memory
1610 * allocated in clk_notifier_register.
1611 *
1612 * Returns -EINVAL if called with null arguments; otherwise, passes
1613 * along the return value of srcu_notifier_chain_unregister().
1614 */
1615int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
1616{
1617 struct clk_notifier *cn = NULL;
1618 int ret = -EINVAL;
1619
1620 if (!clk || !nb)
1621 return -EINVAL;
1622
1623 mutex_lock(&prepare_lock);
1624
1625 list_for_each_entry(cn, &clk_notifier_list, node)
1626 if (cn->clk == clk)
1627 break;
1628
1629 if (cn->clk == clk) {
1630 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
1631
1632 clk->notifier_count--;
1633
1634 /* XXX the notifier code should handle this better */
1635 if (!cn->notifier_head.head) {
1636 srcu_cleanup_notifier_head(&cn->notifier_head);
1637 kfree(cn);
1638 }
1639
1640 } else {
1641 ret = -ENOENT;
1642 }
1643
1644 mutex_unlock(&prepare_lock);
1645
1646 return ret;
1647}
1648EXPORT_SYMBOL_GPL(clk_notifier_unregister);
766e6a4e
GL
1649
1650#ifdef CONFIG_OF
1651/**
1652 * struct of_clk_provider - Clock provider registration structure
1653 * @link: Entry in global list of clock providers
1654 * @node: Pointer to device tree node of clock provider
1655 * @get: Get clock callback. Returns NULL or a struct clk for the
1656 * given clock specifier
1657 * @data: context pointer to be passed into @get callback
1658 */
1659struct of_clk_provider {
1660 struct list_head link;
1661
1662 struct device_node *node;
1663 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
1664 void *data;
1665};
1666
1667static LIST_HEAD(of_clk_providers);
1668static DEFINE_MUTEX(of_clk_lock);
1669
1670struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
1671 void *data)
1672{
1673 return data;
1674}
1675EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
1676
494bfec9
SG
1677struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
1678{
1679 struct clk_onecell_data *clk_data = data;
1680 unsigned int idx = clkspec->args[0];
1681
1682 if (idx >= clk_data->clk_num) {
1683 pr_err("%s: invalid clock index %d\n", __func__, idx);
1684 return ERR_PTR(-EINVAL);
1685 }
1686
1687 return clk_data->clks[idx];
1688}
1689EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
1690
766e6a4e
GL
1691/**
1692 * of_clk_add_provider() - Register a clock provider for a node
1693 * @np: Device node pointer associated with clock provider
1694 * @clk_src_get: callback for decoding clock
1695 * @data: context pointer for @clk_src_get callback.
1696 */
1697int of_clk_add_provider(struct device_node *np,
1698 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
1699 void *data),
1700 void *data)
1701{
1702 struct of_clk_provider *cp;
1703
1704 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
1705 if (!cp)
1706 return -ENOMEM;
1707
1708 cp->node = of_node_get(np);
1709 cp->data = data;
1710 cp->get = clk_src_get;
1711
1712 mutex_lock(&of_clk_lock);
1713 list_add(&cp->link, &of_clk_providers);
1714 mutex_unlock(&of_clk_lock);
1715 pr_debug("Added clock from %s\n", np->full_name);
1716
1717 return 0;
1718}
1719EXPORT_SYMBOL_GPL(of_clk_add_provider);
1720
1721/**
1722 * of_clk_del_provider() - Remove a previously registered clock provider
1723 * @np: Device node pointer associated with clock provider
1724 */
1725void of_clk_del_provider(struct device_node *np)
1726{
1727 struct of_clk_provider *cp;
1728
1729 mutex_lock(&of_clk_lock);
1730 list_for_each_entry(cp, &of_clk_providers, link) {
1731 if (cp->node == np) {
1732 list_del(&cp->link);
1733 of_node_put(cp->node);
1734 kfree(cp);
1735 break;
1736 }
1737 }
1738 mutex_unlock(&of_clk_lock);
1739}
1740EXPORT_SYMBOL_GPL(of_clk_del_provider);
1741
1742struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
1743{
1744 struct of_clk_provider *provider;
1745 struct clk *clk = ERR_PTR(-ENOENT);
1746
1747 /* Check if we have such a provider in our array */
1748 mutex_lock(&of_clk_lock);
1749 list_for_each_entry(provider, &of_clk_providers, link) {
1750 if (provider->node == clkspec->np)
1751 clk = provider->get(clkspec, provider->data);
1752 if (!IS_ERR(clk))
1753 break;
1754 }
1755 mutex_unlock(&of_clk_lock);
1756
1757 return clk;
1758}
1759
1760const char *of_clk_get_parent_name(struct device_node *np, int index)
1761{
1762 struct of_phandle_args clkspec;
1763 const char *clk_name;
1764 int rc;
1765
1766 if (index < 0)
1767 return NULL;
1768
1769 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
1770 &clkspec);
1771 if (rc)
1772 return NULL;
1773
1774 if (of_property_read_string_index(clkspec.np, "clock-output-names",
1775 clkspec.args_count ? clkspec.args[0] : 0,
1776 &clk_name) < 0)
1777 clk_name = clkspec.np->name;
1778
1779 of_node_put(clkspec.np);
1780 return clk_name;
1781}
1782EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
1783
1784/**
1785 * of_clk_init() - Scan and init clock providers from the DT
1786 * @matches: array of compatible values and init functions for providers.
1787 *
1788 * This function scans the device tree for matching clock providers and
1789 * calls their initialization functions
1790 */
1791void __init of_clk_init(const struct of_device_id *matches)
1792{
1793 struct device_node *np;
1794
1795 for_each_matching_node(np, matches) {
1796 const struct of_device_id *match = of_match_node(matches, np);
1797 of_clk_init_cb_t clk_init_cb = match->data;
1798 clk_init_cb(np);
1799 }
1800}
1801#endif
This page took 0.2128 seconds and 5 git commands to generate.