blob: 593a2e42d4afe984ac9ebf4a47c7a1457caf7fca [file] [log] [blame]
Mike Turquetteb24764902012-03-15 23:11:19 -07001/*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Standard functionality for the common clock API. See Documentation/clk.txt
10 */
11
12#include <linux/clk-private.h>
13#include <linux/module.h>
14#include <linux/mutex.h>
15#include <linux/spinlock.h>
16#include <linux/err.h>
17#include <linux/list.h>
18#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050019#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070020#include <linux/device.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070021
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
Prashant Gaikwad1af599d2012-12-26 19:16:22 +053038static void clk_summary_show_one(struct seq_file *s, struct clk *c, int level)
39{
40 if (!c)
41 return;
42
43 seq_printf(s, "%*s%-*s %-11d %-12d %-10lu",
44 level * 3 + 1, "",
45 30 - level * 3, c->name,
46 c->enable_count, c->prepare_count, c->rate);
47 seq_printf(s, "\n");
48}
49
50static void clk_summary_show_subtree(struct seq_file *s, struct clk *c,
51 int level)
52{
53 struct clk *child;
54 struct hlist_node *tmp;
55
56 if (!c)
57 return;
58
59 clk_summary_show_one(s, c, level);
60
61 hlist_for_each_entry(child, tmp, &c->children, child_node)
62 clk_summary_show_subtree(s, child, level + 1);
63}
64
65static int clk_summary_show(struct seq_file *s, void *data)
66{
67 struct clk *c;
68 struct hlist_node *tmp;
69
70 seq_printf(s, " clock enable_cnt prepare_cnt rate\n");
71 seq_printf(s, "---------------------------------------------------------------------\n");
72
73 mutex_lock(&prepare_lock);
74
75 hlist_for_each_entry(c, tmp, &clk_root_list, child_node)
76 clk_summary_show_subtree(s, c, 0);
77
78 hlist_for_each_entry(c, tmp, &clk_orphan_list, child_node)
79 clk_summary_show_subtree(s, c, 0);
80
81 mutex_unlock(&prepare_lock);
82
83 return 0;
84}
85
86
87static int clk_summary_open(struct inode *inode, struct file *file)
88{
89 return single_open(file, clk_summary_show, inode->i_private);
90}
91
92static const struct file_operations clk_summary_fops = {
93 .open = clk_summary_open,
94 .read = seq_read,
95 .llseek = seq_lseek,
96 .release = single_release,
97};
98
Prashant Gaikwadbddca892012-12-26 19:16:23 +053099static void clk_dump_one(struct seq_file *s, struct clk *c, int level)
100{
101 if (!c)
102 return;
103
104 seq_printf(s, "\"%s\": { ", c->name);
105 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
106 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
107 seq_printf(s, "\"rate\": %lu", c->rate);
108}
109
110static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level)
111{
112 struct clk *child;
113 struct hlist_node *tmp;
114
115 if (!c)
116 return;
117
118 clk_dump_one(s, c, level);
119
120 hlist_for_each_entry(child, tmp, &c->children, child_node) {
121 seq_printf(s, ",");
122 clk_dump_subtree(s, child, level + 1);
123 }
124
125 seq_printf(s, "}");
126}
127
128static int clk_dump(struct seq_file *s, void *data)
129{
130 struct clk *c;
131 struct hlist_node *tmp;
132 bool first_node = true;
133
134 seq_printf(s, "{");
135
136 mutex_lock(&prepare_lock);
137
138 hlist_for_each_entry(c, tmp, &clk_root_list, child_node) {
139 if (!first_node)
140 seq_printf(s, ",");
141 first_node = false;
142 clk_dump_subtree(s, c, 0);
143 }
144
145 hlist_for_each_entry(c, tmp, &clk_orphan_list, child_node) {
146 seq_printf(s, ",");
147 clk_dump_subtree(s, c, 0);
148 }
149
150 mutex_unlock(&prepare_lock);
151
152 seq_printf(s, "}");
153 return 0;
154}
155
156
157static int clk_dump_open(struct inode *inode, struct file *file)
158{
159 return single_open(file, clk_dump, inode->i_private);
160}
161
162static const struct file_operations clk_dump_fops = {
163 .open = clk_dump_open,
164 .read = seq_read,
165 .llseek = seq_lseek,
166 .release = single_release,
167};
168
Mike Turquetteb24764902012-03-15 23:11:19 -0700169/* caller must hold prepare_lock */
170static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
171{
172 struct dentry *d;
173 int ret = -ENOMEM;
174
175 if (!clk || !pdentry) {
176 ret = -EINVAL;
177 goto out;
178 }
179
180 d = debugfs_create_dir(clk->name, pdentry);
181 if (!d)
182 goto out;
183
184 clk->dentry = d;
185
186 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
187 (u32 *)&clk->rate);
188 if (!d)
189 goto err_out;
190
191 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
192 (u32 *)&clk->flags);
193 if (!d)
194 goto err_out;
195
196 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
197 (u32 *)&clk->prepare_count);
198 if (!d)
199 goto err_out;
200
201 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
202 (u32 *)&clk->enable_count);
203 if (!d)
204 goto err_out;
205
206 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
207 (u32 *)&clk->notifier_count);
208 if (!d)
209 goto err_out;
210
211 ret = 0;
212 goto out;
213
214err_out:
215 debugfs_remove(clk->dentry);
216out:
217 return ret;
218}
219
220/* caller must hold prepare_lock */
221static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry)
222{
223 struct clk *child;
224 struct hlist_node *tmp;
225 int ret = -EINVAL;;
226
227 if (!clk || !pdentry)
228 goto out;
229
230 ret = clk_debug_create_one(clk, pdentry);
231
232 if (ret)
233 goto out;
234
235 hlist_for_each_entry(child, tmp, &clk->children, child_node)
236 clk_debug_create_subtree(child, clk->dentry);
237
238 ret = 0;
239out:
240 return ret;
241}
242
243/**
244 * clk_debug_register - add a clk node to the debugfs clk tree
245 * @clk: the clk being added to the debugfs clk tree
246 *
247 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
248 * initialized. Otherwise it bails out early since the debugfs clk tree
249 * will be created lazily by clk_debug_init as part of a late_initcall.
250 *
251 * Caller must hold prepare_lock. Only clk_init calls this function (so
252 * far) so this is taken care.
253 */
254static int clk_debug_register(struct clk *clk)
255{
256 struct clk *parent;
257 struct dentry *pdentry;
258 int ret = 0;
259
260 if (!inited)
261 goto out;
262
263 parent = clk->parent;
264
265 /*
266 * Check to see if a clk is a root clk. Also check that it is
267 * safe to add this clk to debugfs
268 */
269 if (!parent)
270 if (clk->flags & CLK_IS_ROOT)
271 pdentry = rootdir;
272 else
273 pdentry = orphandir;
274 else
275 if (parent->dentry)
276 pdentry = parent->dentry;
277 else
278 goto out;
279
280 ret = clk_debug_create_subtree(clk, pdentry);
281
282out:
283 return ret;
284}
285
286/**
287 * clk_debug_init - lazily create the debugfs clk tree visualization
288 *
289 * clks are often initialized very early during boot before memory can
290 * be dynamically allocated and well before debugfs is setup.
291 * clk_debug_init walks the clk tree hierarchy while holding
292 * prepare_lock and creates the topology as part of a late_initcall,
293 * thus insuring that clks initialized very early will still be
294 * represented in the debugfs clk tree. This function should only be
295 * called once at boot-time, and all other clks added dynamically will
296 * be done so with clk_debug_register.
297 */
298static int __init clk_debug_init(void)
299{
300 struct clk *clk;
301 struct hlist_node *tmp;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530302 struct dentry *d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700303
304 rootdir = debugfs_create_dir("clk", NULL);
305
306 if (!rootdir)
307 return -ENOMEM;
308
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530309 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, NULL,
310 &clk_summary_fops);
311 if (!d)
312 return -ENOMEM;
313
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530314 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, NULL,
315 &clk_dump_fops);
316 if (!d)
317 return -ENOMEM;
318
Mike Turquetteb24764902012-03-15 23:11:19 -0700319 orphandir = debugfs_create_dir("orphans", rootdir);
320
321 if (!orphandir)
322 return -ENOMEM;
323
324 mutex_lock(&prepare_lock);
325
326 hlist_for_each_entry(clk, tmp, &clk_root_list, child_node)
327 clk_debug_create_subtree(clk, rootdir);
328
329 hlist_for_each_entry(clk, tmp, &clk_orphan_list, child_node)
330 clk_debug_create_subtree(clk, orphandir);
331
332 inited = 1;
333
334 mutex_unlock(&prepare_lock);
335
336 return 0;
337}
338late_initcall(clk_debug_init);
339#else
340static inline int clk_debug_register(struct clk *clk) { return 0; }
Mike Turquette70d347e2012-03-26 11:53:47 -0700341#endif
Mike Turquetteb24764902012-03-15 23:11:19 -0700342
Mike Turquetteb24764902012-03-15 23:11:19 -0700343/* caller must hold prepare_lock */
344static void clk_disable_unused_subtree(struct clk *clk)
345{
346 struct clk *child;
347 struct hlist_node *tmp;
348 unsigned long flags;
349
350 if (!clk)
351 goto out;
352
353 hlist_for_each_entry(child, tmp, &clk->children, child_node)
354 clk_disable_unused_subtree(child);
355
356 spin_lock_irqsave(&enable_lock, flags);
357
358 if (clk->enable_count)
359 goto unlock_out;
360
361 if (clk->flags & CLK_IGNORE_UNUSED)
362 goto unlock_out;
363
Mike Turquette7c045a52012-12-04 11:00:35 -0800364 /*
365 * some gate clocks have special needs during the disable-unused
366 * sequence. call .disable_unused if available, otherwise fall
367 * back to .disable
368 */
369 if (__clk_is_enabled(clk)) {
370 if (clk->ops->disable_unused)
371 clk->ops->disable_unused(clk->hw);
372 else if (clk->ops->disable)
373 clk->ops->disable(clk->hw);
374 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700375
376unlock_out:
377 spin_unlock_irqrestore(&enable_lock, flags);
378
379out:
380 return;
381}
382
383static int clk_disable_unused(void)
384{
385 struct clk *clk;
386 struct hlist_node *tmp;
387
388 mutex_lock(&prepare_lock);
389
390 hlist_for_each_entry(clk, tmp, &clk_root_list, child_node)
391 clk_disable_unused_subtree(clk);
392
393 hlist_for_each_entry(clk, tmp, &clk_orphan_list, child_node)
394 clk_disable_unused_subtree(clk);
395
396 mutex_unlock(&prepare_lock);
397
398 return 0;
399}
400late_initcall(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700401
402/*** helper functions ***/
403
Russ Dill65800b22012-11-26 11:20:09 -0800404const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700405{
406 return !clk ? NULL : clk->name;
407}
Niels de Vos48950842012-12-13 13:12:25 +0100408EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700409
Russ Dill65800b22012-11-26 11:20:09 -0800410struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700411{
412 return !clk ? NULL : clk->hw;
413}
414
Russ Dill65800b22012-11-26 11:20:09 -0800415u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700416{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700417 return !clk ? 0 : clk->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700418}
419
Russ Dill65800b22012-11-26 11:20:09 -0800420struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700421{
422 return !clk ? NULL : clk->parent;
423}
424
Russ Dill65800b22012-11-26 11:20:09 -0800425unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700426{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700427 return !clk ? 0 : clk->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700428}
429
Russ Dill65800b22012-11-26 11:20:09 -0800430unsigned int __clk_get_prepare_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700431{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700432 return !clk ? 0 : clk->prepare_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700433}
434
435unsigned long __clk_get_rate(struct clk *clk)
436{
437 unsigned long ret;
438
439 if (!clk) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530440 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700441 goto out;
442 }
443
444 ret = clk->rate;
445
446 if (clk->flags & CLK_IS_ROOT)
447 goto out;
448
449 if (!clk->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530450 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700451
452out:
453 return ret;
454}
455
Russ Dill65800b22012-11-26 11:20:09 -0800456unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700457{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700458 return !clk ? 0 : clk->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700459}
460
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700461bool __clk_is_enabled(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700462{
463 int ret;
464
465 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700466 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700467
468 /*
469 * .is_enabled is only mandatory for clocks that gate
470 * fall back to software usage counter if .is_enabled is missing
471 */
472 if (!clk->ops->is_enabled) {
473 ret = clk->enable_count ? 1 : 0;
474 goto out;
475 }
476
477 ret = clk->ops->is_enabled(clk->hw);
478out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700479 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700480}
481
482static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
483{
484 struct clk *child;
485 struct clk *ret;
486 struct hlist_node *tmp;
487
488 if (!strcmp(clk->name, name))
489 return clk;
490
491 hlist_for_each_entry(child, tmp, &clk->children, child_node) {
492 ret = __clk_lookup_subtree(name, child);
493 if (ret)
494 return ret;
495 }
496
497 return NULL;
498}
499
500struct clk *__clk_lookup(const char *name)
501{
502 struct clk *root_clk;
503 struct clk *ret;
504 struct hlist_node *tmp;
505
506 if (!name)
507 return NULL;
508
509 /* search the 'proper' clk tree first */
510 hlist_for_each_entry(root_clk, tmp, &clk_root_list, child_node) {
511 ret = __clk_lookup_subtree(name, root_clk);
512 if (ret)
513 return ret;
514 }
515
516 /* if not found, then search the orphan tree */
517 hlist_for_each_entry(root_clk, tmp, &clk_orphan_list, child_node) {
518 ret = __clk_lookup_subtree(name, root_clk);
519 if (ret)
520 return ret;
521 }
522
523 return NULL;
524}
525
526/*** clk api ***/
527
528void __clk_unprepare(struct clk *clk)
529{
530 if (!clk)
531 return;
532
533 if (WARN_ON(clk->prepare_count == 0))
534 return;
535
536 if (--clk->prepare_count > 0)
537 return;
538
539 WARN_ON(clk->enable_count > 0);
540
541 if (clk->ops->unprepare)
542 clk->ops->unprepare(clk->hw);
543
544 __clk_unprepare(clk->parent);
545}
546
547/**
548 * clk_unprepare - undo preparation of a clock source
549 * @clk: the clk being unprepare
550 *
551 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
552 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
553 * if the operation may sleep. One example is a clk which is accessed over
554 * I2c. In the complex case a clk gate operation may require a fast and a slow
555 * part. It is this reason that clk_unprepare and clk_disable are not mutually
556 * exclusive. In fact clk_disable must be called before clk_unprepare.
557 */
558void clk_unprepare(struct clk *clk)
559{
560 mutex_lock(&prepare_lock);
561 __clk_unprepare(clk);
562 mutex_unlock(&prepare_lock);
563}
564EXPORT_SYMBOL_GPL(clk_unprepare);
565
566int __clk_prepare(struct clk *clk)
567{
568 int ret = 0;
569
570 if (!clk)
571 return 0;
572
573 if (clk->prepare_count == 0) {
574 ret = __clk_prepare(clk->parent);
575 if (ret)
576 return ret;
577
578 if (clk->ops->prepare) {
579 ret = clk->ops->prepare(clk->hw);
580 if (ret) {
581 __clk_unprepare(clk->parent);
582 return ret;
583 }
584 }
585 }
586
587 clk->prepare_count++;
588
589 return 0;
590}
591
592/**
593 * clk_prepare - prepare a clock source
594 * @clk: the clk being prepared
595 *
596 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
597 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
598 * operation may sleep. One example is a clk which is accessed over I2c. In
599 * the complex case a clk ungate operation may require a fast and a slow part.
600 * It is this reason that clk_prepare and clk_enable are not mutually
601 * exclusive. In fact clk_prepare must be called before clk_enable.
602 * Returns 0 on success, -EERROR otherwise.
603 */
604int clk_prepare(struct clk *clk)
605{
606 int ret;
607
608 mutex_lock(&prepare_lock);
609 ret = __clk_prepare(clk);
610 mutex_unlock(&prepare_lock);
611
612 return ret;
613}
614EXPORT_SYMBOL_GPL(clk_prepare);
615
616static void __clk_disable(struct clk *clk)
617{
618 if (!clk)
619 return;
620
Fengguang Wue47c6a32012-07-30 14:39:54 -0700621 if (WARN_ON(IS_ERR(clk)))
622 return;
623
Mike Turquetteb24764902012-03-15 23:11:19 -0700624 if (WARN_ON(clk->enable_count == 0))
625 return;
626
627 if (--clk->enable_count > 0)
628 return;
629
630 if (clk->ops->disable)
631 clk->ops->disable(clk->hw);
632
633 __clk_disable(clk->parent);
634}
635
636/**
637 * clk_disable - gate a clock
638 * @clk: the clk being gated
639 *
640 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
641 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
642 * clk if the operation is fast and will never sleep. One example is a
643 * SoC-internal clk which is controlled via simple register writes. In the
644 * complex case a clk gate operation may require a fast and a slow part. It is
645 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
646 * In fact clk_disable must be called before clk_unprepare.
647 */
648void clk_disable(struct clk *clk)
649{
650 unsigned long flags;
651
652 spin_lock_irqsave(&enable_lock, flags);
653 __clk_disable(clk);
654 spin_unlock_irqrestore(&enable_lock, flags);
655}
656EXPORT_SYMBOL_GPL(clk_disable);
657
658static int __clk_enable(struct clk *clk)
659{
660 int ret = 0;
661
662 if (!clk)
663 return 0;
664
665 if (WARN_ON(clk->prepare_count == 0))
666 return -ESHUTDOWN;
667
668 if (clk->enable_count == 0) {
669 ret = __clk_enable(clk->parent);
670
671 if (ret)
672 return ret;
673
674 if (clk->ops->enable) {
675 ret = clk->ops->enable(clk->hw);
676 if (ret) {
677 __clk_disable(clk->parent);
678 return ret;
679 }
680 }
681 }
682
683 clk->enable_count++;
684 return 0;
685}
686
687/**
688 * clk_enable - ungate a clock
689 * @clk: the clk being ungated
690 *
691 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
692 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
693 * if the operation will never sleep. One example is a SoC-internal clk which
694 * is controlled via simple register writes. In the complex case a clk ungate
695 * operation may require a fast and a slow part. It is this reason that
696 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
697 * must be called before clk_enable. Returns 0 on success, -EERROR
698 * otherwise.
699 */
700int clk_enable(struct clk *clk)
701{
702 unsigned long flags;
703 int ret;
704
705 spin_lock_irqsave(&enable_lock, flags);
706 ret = __clk_enable(clk);
707 spin_unlock_irqrestore(&enable_lock, flags);
708
709 return ret;
710}
711EXPORT_SYMBOL_GPL(clk_enable);
712
713/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700714 * __clk_round_rate - round the given rate for a clk
715 * @clk: round the rate of this clock
716 *
717 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
718 */
719unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
720{
Shawn Guo81536e02012-04-12 20:50:17 +0800721 unsigned long parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700722
723 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700724 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700725
Shawn Guof4d8af22012-04-12 20:50:19 +0800726 if (!clk->ops->round_rate) {
727 if (clk->flags & CLK_SET_RATE_PARENT)
728 return __clk_round_rate(clk->parent, rate);
729 else
730 return clk->rate;
731 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700732
Shawn Guo81536e02012-04-12 20:50:17 +0800733 if (clk->parent)
734 parent_rate = clk->parent->rate;
735
736 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700737}
738
739/**
740 * clk_round_rate - round the given rate for a clk
741 * @clk: the clk for which we are rounding a rate
742 * @rate: the rate which is to be rounded
743 *
744 * Takes in a rate as input and rounds it to a rate that the clk can actually
745 * use which is then returned. If clk doesn't support round_rate operation
746 * then the parent rate is returned.
747 */
748long clk_round_rate(struct clk *clk, unsigned long rate)
749{
750 unsigned long ret;
751
752 mutex_lock(&prepare_lock);
753 ret = __clk_round_rate(clk, rate);
754 mutex_unlock(&prepare_lock);
755
756 return ret;
757}
758EXPORT_SYMBOL_GPL(clk_round_rate);
759
760/**
761 * __clk_notify - call clk notifier chain
762 * @clk: struct clk * that is changing rate
763 * @msg: clk notifier type (see include/linux/clk.h)
764 * @old_rate: old clk rate
765 * @new_rate: new clk rate
766 *
767 * Triggers a notifier call chain on the clk rate-change notification
768 * for 'clk'. Passes a pointer to the struct clk and the previous
769 * and current rates to the notifier callback. Intended to be called by
770 * internal clock code only. Returns NOTIFY_DONE from the last driver
771 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
772 * a driver returns that.
773 */
774static int __clk_notify(struct clk *clk, unsigned long msg,
775 unsigned long old_rate, unsigned long new_rate)
776{
777 struct clk_notifier *cn;
778 struct clk_notifier_data cnd;
779 int ret = NOTIFY_DONE;
780
781 cnd.clk = clk;
782 cnd.old_rate = old_rate;
783 cnd.new_rate = new_rate;
784
785 list_for_each_entry(cn, &clk_notifier_list, node) {
786 if (cn->clk == clk) {
787 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
788 &cnd);
789 break;
790 }
791 }
792
793 return ret;
794}
795
796/**
797 * __clk_recalc_rates
798 * @clk: first clk in the subtree
799 * @msg: notification type (see include/linux/clk.h)
800 *
801 * Walks the subtree of clks starting with clk and recalculates rates as it
802 * goes. Note that if a clk does not implement the .recalc_rate callback then
803 * it is assumed that the clock will take on the rate of it's parent.
804 *
805 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
806 * if necessary.
807 *
808 * Caller must hold prepare_lock.
809 */
810static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
811{
812 unsigned long old_rate;
813 unsigned long parent_rate = 0;
814 struct hlist_node *tmp;
815 struct clk *child;
816
817 old_rate = clk->rate;
818
819 if (clk->parent)
820 parent_rate = clk->parent->rate;
821
822 if (clk->ops->recalc_rate)
823 clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
824 else
825 clk->rate = parent_rate;
826
827 /*
828 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
829 * & ABORT_RATE_CHANGE notifiers
830 */
831 if (clk->notifier_count && msg)
832 __clk_notify(clk, msg, old_rate, clk->rate);
833
834 hlist_for_each_entry(child, tmp, &clk->children, child_node)
835 __clk_recalc_rates(child, msg);
836}
837
838/**
Ulf Hanssona093bde2012-08-31 14:21:28 +0200839 * clk_get_rate - return the rate of clk
840 * @clk: the clk whose rate is being returned
841 *
842 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
843 * is set, which means a recalc_rate will be issued.
844 * If clk is NULL then returns 0.
845 */
846unsigned long clk_get_rate(struct clk *clk)
847{
848 unsigned long rate;
849
850 mutex_lock(&prepare_lock);
851
852 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
853 __clk_recalc_rates(clk, 0);
854
855 rate = __clk_get_rate(clk);
856 mutex_unlock(&prepare_lock);
857
858 return rate;
859}
860EXPORT_SYMBOL_GPL(clk_get_rate);
861
862/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700863 * __clk_speculate_rates
864 * @clk: first clk in the subtree
865 * @parent_rate: the "future" rate of clk's parent
866 *
867 * Walks the subtree of clks starting with clk, speculating rates as it
868 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
869 *
870 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
871 * pre-rate change notifications and returns early if no clks in the
872 * subtree have subscribed to the notifications. Note that if a clk does not
873 * implement the .recalc_rate callback then it is assumed that the clock will
874 * take on the rate of it's parent.
875 *
876 * Caller must hold prepare_lock.
877 */
878static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
879{
880 struct hlist_node *tmp;
881 struct clk *child;
882 unsigned long new_rate;
883 int ret = NOTIFY_DONE;
884
885 if (clk->ops->recalc_rate)
886 new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
887 else
888 new_rate = parent_rate;
889
890 /* abort the rate change if a driver returns NOTIFY_BAD */
891 if (clk->notifier_count)
892 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
893
894 if (ret == NOTIFY_BAD)
895 goto out;
896
897 hlist_for_each_entry(child, tmp, &clk->children, child_node) {
898 ret = __clk_speculate_rates(child, new_rate);
899 if (ret == NOTIFY_BAD)
900 break;
901 }
902
903out:
904 return ret;
905}
906
907static void clk_calc_subtree(struct clk *clk, unsigned long new_rate)
908{
909 struct clk *child;
910 struct hlist_node *tmp;
911
912 clk->new_rate = new_rate;
913
914 hlist_for_each_entry(child, tmp, &clk->children, child_node) {
915 if (child->ops->recalc_rate)
916 child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
917 else
918 child->new_rate = new_rate;
919 clk_calc_subtree(child, child->new_rate);
920 }
921}
922
923/*
924 * calculate the new rates returning the topmost clock that has to be
925 * changed.
926 */
927static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
928{
929 struct clk *top = clk;
Shawn Guo81536e02012-04-12 20:50:17 +0800930 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700931 unsigned long new_rate;
932
Mike Turquette7452b212012-03-26 14:45:36 -0700933 /* sanity */
934 if (IS_ERR_OR_NULL(clk))
935 return NULL;
936
Mike Turquette63f5c3b2012-05-02 16:23:43 -0700937 /* save parent rate, if it exists */
938 if (clk->parent)
939 best_parent_rate = clk->parent->rate;
940
Mike Turquette7452b212012-03-26 14:45:36 -0700941 /* never propagate up to the parent */
942 if (!(clk->flags & CLK_SET_RATE_PARENT)) {
943 if (!clk->ops->round_rate) {
944 clk->new_rate = clk->rate;
945 return NULL;
Mike Turquette7452b212012-03-26 14:45:36 -0700946 }
Mike Turquette63f5c3b2012-05-02 16:23:43 -0700947 new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
948 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -0700949 }
950
951 /* need clk->parent from here on out */
952 if (!clk->parent) {
953 pr_debug("%s: %s has NULL parent\n", __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700954 return NULL;
955 }
956
Mike Turquette7452b212012-03-26 14:45:36 -0700957 if (!clk->ops->round_rate) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700958 top = clk_calc_new_rates(clk->parent, rate);
Viresh Kumar1b2f9902012-04-17 16:45:38 +0530959 new_rate = clk->parent->new_rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700960
961 goto out;
962 }
963
Mike Turquette7452b212012-03-26 14:45:36 -0700964 new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700965
966 if (best_parent_rate != clk->parent->rate) {
967 top = clk_calc_new_rates(clk->parent, best_parent_rate);
968
969 goto out;
970 }
971
972out:
973 clk_calc_subtree(clk, new_rate);
974
975 return top;
976}
977
978/*
979 * Notify about rate changes in a subtree. Always walk down the whole tree
980 * so that in case of an error we can walk down the whole tree again and
981 * abort the change.
982 */
983static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
984{
985 struct hlist_node *tmp;
986 struct clk *child, *fail_clk = NULL;
987 int ret = NOTIFY_DONE;
988
989 if (clk->rate == clk->new_rate)
990 return 0;
991
992 if (clk->notifier_count) {
993 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
994 if (ret == NOTIFY_BAD)
995 fail_clk = clk;
996 }
997
998 hlist_for_each_entry(child, tmp, &clk->children, child_node) {
999 clk = clk_propagate_rate_change(child, event);
1000 if (clk)
1001 fail_clk = clk;
1002 }
1003
1004 return fail_clk;
1005}
1006
1007/*
1008 * walk down a subtree and set the new rates notifying the rate
1009 * change on the way
1010 */
1011static void clk_change_rate(struct clk *clk)
1012{
1013 struct clk *child;
1014 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001015 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001016 struct hlist_node *tmp;
1017
1018 old_rate = clk->rate;
1019
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001020 if (clk->parent)
1021 best_parent_rate = clk->parent->rate;
1022
Mike Turquetteb24764902012-03-15 23:11:19 -07001023 if (clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001024 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001025
1026 if (clk->ops->recalc_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001027 clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001028 else
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001029 clk->rate = best_parent_rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001030
1031 if (clk->notifier_count && old_rate != clk->rate)
1032 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1033
1034 hlist_for_each_entry(child, tmp, &clk->children, child_node)
1035 clk_change_rate(child);
1036}
1037
1038/**
1039 * clk_set_rate - specify a new rate for clk
1040 * @clk: the clk whose rate is being changed
1041 * @rate: the new rate for clk
1042 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001043 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001044 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001045 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1046 * propagate up to clk's parent; whether or not this happens depends on the
1047 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1048 * after calling .round_rate then upstream parent propagation is ignored. If
1049 * *parent_rate comes back with a new rate for clk's parent then we propagate
1050 * up to clk's parent and set it's rate. Upward propagation will continue
1051 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1052 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001053 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001054 * Rate changes are accomplished via tree traversal that also recalculates the
1055 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001056 *
1057 * Returns 0 on success, -EERROR otherwise.
1058 */
1059int clk_set_rate(struct clk *clk, unsigned long rate)
1060{
1061 struct clk *top, *fail_clk;
1062 int ret = 0;
1063
1064 /* prevent racing with updates to the clock topology */
1065 mutex_lock(&prepare_lock);
1066
1067 /* bail early if nothing to do */
1068 if (rate == clk->rate)
1069 goto out;
1070
Saravana Kannan7e0fa1b2012-05-15 13:43:42 -07001071 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
Viresh Kumar0e1c0302012-04-11 16:03:42 +05301072 ret = -EBUSY;
1073 goto out;
1074 }
1075
Mike Turquetteb24764902012-03-15 23:11:19 -07001076 /* calculate new rates and get the topmost changed clock */
1077 top = clk_calc_new_rates(clk, rate);
1078 if (!top) {
1079 ret = -EINVAL;
1080 goto out;
1081 }
1082
1083 /* notify that we are about to change rates */
1084 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1085 if (fail_clk) {
1086 pr_warn("%s: failed to set %s rate\n", __func__,
1087 fail_clk->name);
1088 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1089 ret = -EBUSY;
1090 goto out;
1091 }
1092
1093 /* change the rates */
1094 clk_change_rate(top);
1095
1096 mutex_unlock(&prepare_lock);
1097
1098 return 0;
1099out:
1100 mutex_unlock(&prepare_lock);
1101
1102 return ret;
1103}
1104EXPORT_SYMBOL_GPL(clk_set_rate);
1105
1106/**
1107 * clk_get_parent - return the parent of a clk
1108 * @clk: the clk whose parent gets returned
1109 *
1110 * Simply returns clk->parent. Returns NULL if clk is NULL.
1111 */
1112struct clk *clk_get_parent(struct clk *clk)
1113{
1114 struct clk *parent;
1115
1116 mutex_lock(&prepare_lock);
1117 parent = __clk_get_parent(clk);
1118 mutex_unlock(&prepare_lock);
1119
1120 return parent;
1121}
1122EXPORT_SYMBOL_GPL(clk_get_parent);
1123
1124/*
1125 * .get_parent is mandatory for clocks with multiple possible parents. It is
1126 * optional for single-parent clocks. Always call .get_parent if it is
1127 * available and WARN if it is missing for multi-parent clocks.
1128 *
1129 * For single-parent clocks without .get_parent, first check to see if the
1130 * .parents array exists, and if so use it to avoid an expensive tree
1131 * traversal. If .parents does not exist then walk the tree with __clk_lookup.
1132 */
1133static struct clk *__clk_init_parent(struct clk *clk)
1134{
1135 struct clk *ret = NULL;
1136 u8 index;
1137
1138 /* handle the trivial cases */
1139
1140 if (!clk->num_parents)
1141 goto out;
1142
1143 if (clk->num_parents == 1) {
1144 if (IS_ERR_OR_NULL(clk->parent))
1145 ret = clk->parent = __clk_lookup(clk->parent_names[0]);
1146 ret = clk->parent;
1147 goto out;
1148 }
1149
1150 if (!clk->ops->get_parent) {
1151 WARN(!clk->ops->get_parent,
1152 "%s: multi-parent clocks must implement .get_parent\n",
1153 __func__);
1154 goto out;
1155 };
1156
1157 /*
1158 * Do our best to cache parent clocks in clk->parents. This prevents
1159 * unnecessary and expensive calls to __clk_lookup. We don't set
1160 * clk->parent here; that is done by the calling function
1161 */
1162
1163 index = clk->ops->get_parent(clk->hw);
1164
1165 if (!clk->parents)
1166 clk->parents =
Rajendra Nayak79750592012-06-06 14:41:31 +05301167 kzalloc((sizeof(struct clk*) * clk->num_parents),
Mike Turquetteb24764902012-03-15 23:11:19 -07001168 GFP_KERNEL);
1169
1170 if (!clk->parents)
1171 ret = __clk_lookup(clk->parent_names[index]);
1172 else if (!clk->parents[index])
1173 ret = clk->parents[index] =
1174 __clk_lookup(clk->parent_names[index]);
1175 else
1176 ret = clk->parents[index];
1177
1178out:
1179 return ret;
1180}
1181
1182void __clk_reparent(struct clk *clk, struct clk *new_parent)
1183{
1184#ifdef CONFIG_COMMON_CLK_DEBUG
1185 struct dentry *d;
1186 struct dentry *new_parent_d;
1187#endif
1188
1189 if (!clk || !new_parent)
1190 return;
1191
1192 hlist_del(&clk->child_node);
1193
1194 if (new_parent)
1195 hlist_add_head(&clk->child_node, &new_parent->children);
1196 else
1197 hlist_add_head(&clk->child_node, &clk_orphan_list);
1198
1199#ifdef CONFIG_COMMON_CLK_DEBUG
1200 if (!inited)
1201 goto out;
1202
1203 if (new_parent)
1204 new_parent_d = new_parent->dentry;
1205 else
1206 new_parent_d = orphandir;
1207
1208 d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
1209 new_parent_d, clk->name);
1210 if (d)
1211 clk->dentry = d;
1212 else
1213 pr_debug("%s: failed to rename debugfs entry for %s\n",
1214 __func__, clk->name);
1215out:
1216#endif
1217
1218 clk->parent = new_parent;
1219
1220 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1221}
1222
1223static int __clk_set_parent(struct clk *clk, struct clk *parent)
1224{
1225 struct clk *old_parent;
1226 unsigned long flags;
1227 int ret = -EINVAL;
1228 u8 i;
1229
1230 old_parent = clk->parent;
1231
Rajendra Nayak863b1322012-07-03 12:11:41 +05301232 if (!clk->parents)
Rajendra Nayak79750592012-06-06 14:41:31 +05301233 clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
1234 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001235
1236 /*
Rajendra Nayak863b1322012-07-03 12:11:41 +05301237 * find index of new parent clock using cached parent ptrs,
1238 * or if not yet cached, use string name comparison and cache
1239 * them now to avoid future calls to __clk_lookup.
Mike Turquetteb24764902012-03-15 23:11:19 -07001240 */
Rajendra Nayak863b1322012-07-03 12:11:41 +05301241 for (i = 0; i < clk->num_parents; i++) {
1242 if (clk->parents && clk->parents[i] == parent)
1243 break;
1244 else if (!strcmp(clk->parent_names[i], parent->name)) {
1245 if (clk->parents)
1246 clk->parents[i] = __clk_lookup(parent->name);
1247 break;
1248 }
1249 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001250
1251 if (i == clk->num_parents) {
1252 pr_debug("%s: clock %s is not a possible parent of clock %s\n",
1253 __func__, parent->name, clk->name);
1254 goto out;
1255 }
1256
1257 /* migrate prepare and enable */
1258 if (clk->prepare_count)
1259 __clk_prepare(parent);
1260
1261 /* FIXME replace with clk_is_enabled(clk) someday */
1262 spin_lock_irqsave(&enable_lock, flags);
1263 if (clk->enable_count)
1264 __clk_enable(parent);
1265 spin_unlock_irqrestore(&enable_lock, flags);
1266
1267 /* change clock input source */
1268 ret = clk->ops->set_parent(clk->hw, i);
1269
1270 /* clean up old prepare and enable */
1271 spin_lock_irqsave(&enable_lock, flags);
1272 if (clk->enable_count)
1273 __clk_disable(old_parent);
1274 spin_unlock_irqrestore(&enable_lock, flags);
1275
1276 if (clk->prepare_count)
1277 __clk_unprepare(old_parent);
1278
1279out:
1280 return ret;
1281}
1282
1283/**
1284 * clk_set_parent - switch the parent of a mux clk
1285 * @clk: the mux clk whose input we are switching
1286 * @parent: the new input to clk
1287 *
1288 * Re-parent clk to use parent as it's new input source. If clk has the
1289 * CLK_SET_PARENT_GATE flag set then clk must be gated for this
1290 * operation to succeed. After successfully changing clk's parent
1291 * clk_set_parent will update the clk topology, sysfs topology and
1292 * propagate rate recalculation via __clk_recalc_rates. Returns 0 on
1293 * success, -EERROR otherwise.
1294 */
1295int clk_set_parent(struct clk *clk, struct clk *parent)
1296{
1297 int ret = 0;
1298
1299 if (!clk || !clk->ops)
1300 return -EINVAL;
1301
1302 if (!clk->ops->set_parent)
1303 return -ENOSYS;
1304
1305 /* prevent racing with updates to the clock topology */
1306 mutex_lock(&prepare_lock);
1307
1308 if (clk->parent == parent)
1309 goto out;
1310
1311 /* propagate PRE_RATE_CHANGE notifications */
1312 if (clk->notifier_count)
1313 ret = __clk_speculate_rates(clk, parent->rate);
1314
1315 /* abort if a driver objects */
1316 if (ret == NOTIFY_STOP)
1317 goto out;
1318
1319 /* only re-parent if the clock is not in use */
1320 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count)
1321 ret = -EBUSY;
1322 else
1323 ret = __clk_set_parent(clk, parent);
1324
1325 /* propagate ABORT_RATE_CHANGE if .set_parent failed */
1326 if (ret) {
1327 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
1328 goto out;
1329 }
1330
1331 /* propagate rate recalculation downstream */
1332 __clk_reparent(clk, parent);
1333
1334out:
1335 mutex_unlock(&prepare_lock);
1336
1337 return ret;
1338}
1339EXPORT_SYMBOL_GPL(clk_set_parent);
1340
1341/**
1342 * __clk_init - initialize the data structures in a struct clk
1343 * @dev: device initializing this clk, placeholder for now
1344 * @clk: clk being initialized
1345 *
1346 * Initializes the lists in struct clk, queries the hardware for the
1347 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07001348 */
Mike Turquetted1302a32012-03-29 14:30:40 -07001349int __clk_init(struct device *dev, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001350{
Mike Turquetted1302a32012-03-29 14:30:40 -07001351 int i, ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001352 struct clk *orphan;
1353 struct hlist_node *tmp, *tmp2;
1354
1355 if (!clk)
Mike Turquetted1302a32012-03-29 14:30:40 -07001356 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001357
1358 mutex_lock(&prepare_lock);
1359
1360 /* check to see if a clock with this name is already registered */
Mike Turquetted1302a32012-03-29 14:30:40 -07001361 if (__clk_lookup(clk->name)) {
1362 pr_debug("%s: clk %s already initialized\n",
1363 __func__, clk->name);
1364 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07001365 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07001366 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001367
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001368 /* check that clk_ops are sane. See Documentation/clk.txt */
1369 if (clk->ops->set_rate &&
1370 !(clk->ops->round_rate && clk->ops->recalc_rate)) {
1371 pr_warning("%s: %s must implement .round_rate & .recalc_rate\n",
1372 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001373 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001374 goto out;
1375 }
1376
1377 if (clk->ops->set_parent && !clk->ops->get_parent) {
1378 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
1379 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001380 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001381 goto out;
1382 }
1383
Mike Turquetteb24764902012-03-15 23:11:19 -07001384 /* throw a WARN if any entries in parent_names are NULL */
1385 for (i = 0; i < clk->num_parents; i++)
1386 WARN(!clk->parent_names[i],
1387 "%s: invalid NULL in %s's .parent_names\n",
1388 __func__, clk->name);
1389
1390 /*
1391 * Allocate an array of struct clk *'s to avoid unnecessary string
1392 * look-ups of clk's possible parents. This can fail for clocks passed
1393 * in to clk_init during early boot; thus any access to clk->parents[]
1394 * must always check for a NULL pointer and try to populate it if
1395 * necessary.
1396 *
1397 * If clk->parents is not NULL we skip this entire block. This allows
1398 * for clock drivers to statically initialize clk->parents.
1399 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05301400 if (clk->num_parents > 1 && !clk->parents) {
1401 clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
Mike Turquetteb24764902012-03-15 23:11:19 -07001402 GFP_KERNEL);
1403 /*
1404 * __clk_lookup returns NULL for parents that have not been
1405 * clk_init'd; thus any access to clk->parents[] must check
1406 * for a NULL pointer. We can always perform lazy lookups for
1407 * missing parents later on.
1408 */
1409 if (clk->parents)
1410 for (i = 0; i < clk->num_parents; i++)
1411 clk->parents[i] =
1412 __clk_lookup(clk->parent_names[i]);
1413 }
1414
1415 clk->parent = __clk_init_parent(clk);
1416
1417 /*
1418 * Populate clk->parent if parent has already been __clk_init'd. If
1419 * parent has not yet been __clk_init'd then place clk in the orphan
1420 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
1421 * clk list.
1422 *
1423 * Every time a new clk is clk_init'd then we walk the list of orphan
1424 * clocks and re-parent any that are children of the clock currently
1425 * being clk_init'd.
1426 */
1427 if (clk->parent)
1428 hlist_add_head(&clk->child_node,
1429 &clk->parent->children);
1430 else if (clk->flags & CLK_IS_ROOT)
1431 hlist_add_head(&clk->child_node, &clk_root_list);
1432 else
1433 hlist_add_head(&clk->child_node, &clk_orphan_list);
1434
1435 /*
1436 * Set clk's rate. The preferred method is to use .recalc_rate. For
1437 * simple clocks and lazy developers the default fallback is to use the
1438 * parent's rate. If a clock doesn't have a parent (or is orphaned)
1439 * then rate is set to zero.
1440 */
1441 if (clk->ops->recalc_rate)
1442 clk->rate = clk->ops->recalc_rate(clk->hw,
1443 __clk_get_rate(clk->parent));
1444 else if (clk->parent)
1445 clk->rate = clk->parent->rate;
1446 else
1447 clk->rate = 0;
1448
1449 /*
1450 * walk the list of orphan clocks and reparent any that are children of
1451 * this clock
1452 */
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001453 hlist_for_each_entry_safe(orphan, tmp, tmp2, &clk_orphan_list, child_node) {
1454 if (orphan->ops->get_parent) {
1455 i = orphan->ops->get_parent(orphan->hw);
1456 if (!strcmp(clk->name, orphan->parent_names[i]))
1457 __clk_reparent(orphan, clk);
1458 continue;
1459 }
1460
Mike Turquetteb24764902012-03-15 23:11:19 -07001461 for (i = 0; i < orphan->num_parents; i++)
1462 if (!strcmp(clk->name, orphan->parent_names[i])) {
1463 __clk_reparent(orphan, clk);
1464 break;
1465 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001466 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001467
1468 /*
1469 * optional platform-specific magic
1470 *
1471 * The .init callback is not used by any of the basic clock types, but
1472 * exists for weird hardware that must perform initialization magic.
1473 * Please consider other ways of solving initialization problems before
1474 * using this callback, as it's use is discouraged.
1475 */
1476 if (clk->ops->init)
1477 clk->ops->init(clk->hw);
1478
1479 clk_debug_register(clk);
1480
1481out:
1482 mutex_unlock(&prepare_lock);
1483
Mike Turquetted1302a32012-03-29 14:30:40 -07001484 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001485}
1486
1487/**
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001488 * __clk_register - register a clock and return a cookie.
1489 *
1490 * Same as clk_register, except that the .clk field inside hw shall point to a
1491 * preallocated (generally statically allocated) struct clk. None of the fields
1492 * of the struct clk need to be initialized.
1493 *
1494 * The data pointed to by .init and .clk field shall NOT be marked as init
1495 * data.
1496 *
1497 * __clk_register is only exposed via clk-private.h and is intended for use with
1498 * very large numbers of clocks that need to be statically initialized. It is
1499 * a layering violation to include clk-private.h from any code which implements
1500 * a clock's .ops; as such any statically initialized clock data MUST be in a
1501 * separate C file from the logic that implements it's operations. Returns 0
1502 * on success, otherwise an error code.
1503 */
1504struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
1505{
1506 int ret;
1507 struct clk *clk;
1508
1509 clk = hw->clk;
1510 clk->name = hw->init->name;
1511 clk->ops = hw->init->ops;
1512 clk->hw = hw;
1513 clk->flags = hw->init->flags;
1514 clk->parent_names = hw->init->parent_names;
1515 clk->num_parents = hw->init->num_parents;
1516
1517 ret = __clk_init(dev, clk);
1518 if (ret)
1519 return ERR_PTR(ret);
1520
1521 return clk;
1522}
1523EXPORT_SYMBOL_GPL(__clk_register);
1524
Stephen Boyd46c87732012-09-24 13:38:04 -07001525static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001526{
Mike Turquetted1302a32012-03-29 14:30:40 -07001527 int i, ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001528
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001529 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
1530 if (!clk->name) {
1531 pr_err("%s: could not allocate clk->name\n", __func__);
1532 ret = -ENOMEM;
1533 goto fail_name;
1534 }
1535 clk->ops = hw->init->ops;
Mike Turquetteb24764902012-03-15 23:11:19 -07001536 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001537 clk->flags = hw->init->flags;
1538 clk->num_parents = hw->init->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -07001539 hw->clk = clk;
1540
Mike Turquetted1302a32012-03-29 14:30:40 -07001541 /* allocate local copy in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001542 clk->parent_names = kzalloc((sizeof(char*) * clk->num_parents),
Mike Turquetted1302a32012-03-29 14:30:40 -07001543 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001544
Mike Turquetted1302a32012-03-29 14:30:40 -07001545 if (!clk->parent_names) {
1546 pr_err("%s: could not allocate clk->parent_names\n", __func__);
1547 ret = -ENOMEM;
1548 goto fail_parent_names;
1549 }
1550
1551
1552 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001553 for (i = 0; i < clk->num_parents; i++) {
1554 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
1555 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07001556 if (!clk->parent_names[i]) {
1557 pr_err("%s: could not copy parent_names\n", __func__);
1558 ret = -ENOMEM;
1559 goto fail_parent_names_copy;
1560 }
1561 }
1562
1563 ret = __clk_init(dev, clk);
1564 if (!ret)
Stephen Boyd46c87732012-09-24 13:38:04 -07001565 return 0;
Mike Turquetted1302a32012-03-29 14:30:40 -07001566
1567fail_parent_names_copy:
1568 while (--i >= 0)
1569 kfree(clk->parent_names[i]);
1570 kfree(clk->parent_names);
1571fail_parent_names:
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001572 kfree(clk->name);
1573fail_name:
Stephen Boyd46c87732012-09-24 13:38:04 -07001574 return ret;
1575}
1576
1577/**
1578 * clk_register - allocate a new clock, register it and return an opaque cookie
1579 * @dev: device that is registering this clock
1580 * @hw: link to hardware-specific clock data
1581 *
1582 * clk_register is the primary interface for populating the clock tree with new
1583 * clock nodes. It returns a pointer to the newly allocated struct clk which
1584 * cannot be dereferenced by driver code but may be used in conjuction with the
1585 * rest of the clock API. In the event of an error clk_register will return an
1586 * error code; drivers must test for an error code after calling clk_register.
1587 */
1588struct clk *clk_register(struct device *dev, struct clk_hw *hw)
1589{
1590 int ret;
1591 struct clk *clk;
1592
1593 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
1594 if (!clk) {
1595 pr_err("%s: could not allocate clk\n", __func__);
1596 ret = -ENOMEM;
1597 goto fail_out;
1598 }
1599
1600 ret = _clk_register(dev, hw, clk);
1601 if (!ret)
1602 return clk;
1603
Mike Turquetted1302a32012-03-29 14:30:40 -07001604 kfree(clk);
1605fail_out:
1606 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001607}
1608EXPORT_SYMBOL_GPL(clk_register);
1609
Mark Brown1df5c932012-04-18 09:07:12 +01001610/**
1611 * clk_unregister - unregister a currently registered clock
1612 * @clk: clock to unregister
1613 *
1614 * Currently unimplemented.
1615 */
1616void clk_unregister(struct clk *clk) {}
1617EXPORT_SYMBOL_GPL(clk_unregister);
1618
Stephen Boyd46c87732012-09-24 13:38:04 -07001619static void devm_clk_release(struct device *dev, void *res)
1620{
1621 clk_unregister(res);
1622}
1623
1624/**
1625 * devm_clk_register - resource managed clk_register()
1626 * @dev: device that is registering this clock
1627 * @hw: link to hardware-specific clock data
1628 *
1629 * Managed clk_register(). Clocks returned from this function are
1630 * automatically clk_unregister()ed on driver detach. See clk_register() for
1631 * more information.
1632 */
1633struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
1634{
1635 struct clk *clk;
1636 int ret;
1637
1638 clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL);
1639 if (!clk)
1640 return ERR_PTR(-ENOMEM);
1641
1642 ret = _clk_register(dev, hw, clk);
1643 if (!ret) {
1644 devres_add(dev, clk);
1645 } else {
1646 devres_free(clk);
1647 clk = ERR_PTR(ret);
1648 }
1649
1650 return clk;
1651}
1652EXPORT_SYMBOL_GPL(devm_clk_register);
1653
1654static int devm_clk_match(struct device *dev, void *res, void *data)
1655{
1656 struct clk *c = res;
1657 if (WARN_ON(!c))
1658 return 0;
1659 return c == data;
1660}
1661
1662/**
1663 * devm_clk_unregister - resource managed clk_unregister()
1664 * @clk: clock to unregister
1665 *
1666 * Deallocate a clock allocated with devm_clk_register(). Normally
1667 * this function will not need to be called and the resource management
1668 * code will ensure that the resource is freed.
1669 */
1670void devm_clk_unregister(struct device *dev, struct clk *clk)
1671{
1672 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
1673}
1674EXPORT_SYMBOL_GPL(devm_clk_unregister);
1675
Mike Turquetteb24764902012-03-15 23:11:19 -07001676/*** clk rate change notifiers ***/
1677
1678/**
1679 * clk_notifier_register - add a clk rate change notifier
1680 * @clk: struct clk * to watch
1681 * @nb: struct notifier_block * with callback info
1682 *
1683 * Request notification when clk's rate changes. This uses an SRCU
1684 * notifier because we want it to block and notifier unregistrations are
1685 * uncommon. The callbacks associated with the notifier must not
1686 * re-enter into the clk framework by calling any top-level clk APIs;
1687 * this will cause a nested prepare_lock mutex.
1688 *
1689 * Pre-change notifier callbacks will be passed the current, pre-change
1690 * rate of the clk via struct clk_notifier_data.old_rate. The new,
1691 * post-change rate of the clk is passed via struct
1692 * clk_notifier_data.new_rate.
1693 *
1694 * Post-change notifiers will pass the now-current, post-change rate of
1695 * the clk in both struct clk_notifier_data.old_rate and struct
1696 * clk_notifier_data.new_rate.
1697 *
1698 * Abort-change notifiers are effectively the opposite of pre-change
1699 * notifiers: the original pre-change clk rate is passed in via struct
1700 * clk_notifier_data.new_rate and the failed post-change rate is passed
1701 * in via struct clk_notifier_data.old_rate.
1702 *
1703 * clk_notifier_register() must be called from non-atomic context.
1704 * Returns -EINVAL if called with null arguments, -ENOMEM upon
1705 * allocation failure; otherwise, passes along the return value of
1706 * srcu_notifier_chain_register().
1707 */
1708int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
1709{
1710 struct clk_notifier *cn;
1711 int ret = -ENOMEM;
1712
1713 if (!clk || !nb)
1714 return -EINVAL;
1715
1716 mutex_lock(&prepare_lock);
1717
1718 /* search the list of notifiers for this clk */
1719 list_for_each_entry(cn, &clk_notifier_list, node)
1720 if (cn->clk == clk)
1721 break;
1722
1723 /* if clk wasn't in the notifier list, allocate new clk_notifier */
1724 if (cn->clk != clk) {
1725 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
1726 if (!cn)
1727 goto out;
1728
1729 cn->clk = clk;
1730 srcu_init_notifier_head(&cn->notifier_head);
1731
1732 list_add(&cn->node, &clk_notifier_list);
1733 }
1734
1735 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
1736
1737 clk->notifier_count++;
1738
1739out:
1740 mutex_unlock(&prepare_lock);
1741
1742 return ret;
1743}
1744EXPORT_SYMBOL_GPL(clk_notifier_register);
1745
1746/**
1747 * clk_notifier_unregister - remove a clk rate change notifier
1748 * @clk: struct clk *
1749 * @nb: struct notifier_block * with callback info
1750 *
1751 * Request no further notification for changes to 'clk' and frees memory
1752 * allocated in clk_notifier_register.
1753 *
1754 * Returns -EINVAL if called with null arguments; otherwise, passes
1755 * along the return value of srcu_notifier_chain_unregister().
1756 */
1757int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
1758{
1759 struct clk_notifier *cn = NULL;
1760 int ret = -EINVAL;
1761
1762 if (!clk || !nb)
1763 return -EINVAL;
1764
1765 mutex_lock(&prepare_lock);
1766
1767 list_for_each_entry(cn, &clk_notifier_list, node)
1768 if (cn->clk == clk)
1769 break;
1770
1771 if (cn->clk == clk) {
1772 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
1773
1774 clk->notifier_count--;
1775
1776 /* XXX the notifier code should handle this better */
1777 if (!cn->notifier_head.head) {
1778 srcu_cleanup_notifier_head(&cn->notifier_head);
1779 kfree(cn);
1780 }
1781
1782 } else {
1783 ret = -ENOENT;
1784 }
1785
1786 mutex_unlock(&prepare_lock);
1787
1788 return ret;
1789}
1790EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05001791
1792#ifdef CONFIG_OF
1793/**
1794 * struct of_clk_provider - Clock provider registration structure
1795 * @link: Entry in global list of clock providers
1796 * @node: Pointer to device tree node of clock provider
1797 * @get: Get clock callback. Returns NULL or a struct clk for the
1798 * given clock specifier
1799 * @data: context pointer to be passed into @get callback
1800 */
1801struct of_clk_provider {
1802 struct list_head link;
1803
1804 struct device_node *node;
1805 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
1806 void *data;
1807};
1808
1809static LIST_HEAD(of_clk_providers);
1810static DEFINE_MUTEX(of_clk_lock);
1811
1812struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
1813 void *data)
1814{
1815 return data;
1816}
1817EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
1818
Shawn Guo494bfec2012-08-22 21:36:27 +08001819struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
1820{
1821 struct clk_onecell_data *clk_data = data;
1822 unsigned int idx = clkspec->args[0];
1823
1824 if (idx >= clk_data->clk_num) {
1825 pr_err("%s: invalid clock index %d\n", __func__, idx);
1826 return ERR_PTR(-EINVAL);
1827 }
1828
1829 return clk_data->clks[idx];
1830}
1831EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
1832
Grant Likely766e6a42012-04-09 14:50:06 -05001833/**
1834 * of_clk_add_provider() - Register a clock provider for a node
1835 * @np: Device node pointer associated with clock provider
1836 * @clk_src_get: callback for decoding clock
1837 * @data: context pointer for @clk_src_get callback.
1838 */
1839int of_clk_add_provider(struct device_node *np,
1840 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
1841 void *data),
1842 void *data)
1843{
1844 struct of_clk_provider *cp;
1845
1846 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
1847 if (!cp)
1848 return -ENOMEM;
1849
1850 cp->node = of_node_get(np);
1851 cp->data = data;
1852 cp->get = clk_src_get;
1853
1854 mutex_lock(&of_clk_lock);
1855 list_add(&cp->link, &of_clk_providers);
1856 mutex_unlock(&of_clk_lock);
1857 pr_debug("Added clock from %s\n", np->full_name);
1858
1859 return 0;
1860}
1861EXPORT_SYMBOL_GPL(of_clk_add_provider);
1862
1863/**
1864 * of_clk_del_provider() - Remove a previously registered clock provider
1865 * @np: Device node pointer associated with clock provider
1866 */
1867void of_clk_del_provider(struct device_node *np)
1868{
1869 struct of_clk_provider *cp;
1870
1871 mutex_lock(&of_clk_lock);
1872 list_for_each_entry(cp, &of_clk_providers, link) {
1873 if (cp->node == np) {
1874 list_del(&cp->link);
1875 of_node_put(cp->node);
1876 kfree(cp);
1877 break;
1878 }
1879 }
1880 mutex_unlock(&of_clk_lock);
1881}
1882EXPORT_SYMBOL_GPL(of_clk_del_provider);
1883
1884struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
1885{
1886 struct of_clk_provider *provider;
1887 struct clk *clk = ERR_PTR(-ENOENT);
1888
1889 /* Check if we have such a provider in our array */
1890 mutex_lock(&of_clk_lock);
1891 list_for_each_entry(provider, &of_clk_providers, link) {
1892 if (provider->node == clkspec->np)
1893 clk = provider->get(clkspec, provider->data);
1894 if (!IS_ERR(clk))
1895 break;
1896 }
1897 mutex_unlock(&of_clk_lock);
1898
1899 return clk;
1900}
1901
1902const char *of_clk_get_parent_name(struct device_node *np, int index)
1903{
1904 struct of_phandle_args clkspec;
1905 const char *clk_name;
1906 int rc;
1907
1908 if (index < 0)
1909 return NULL;
1910
1911 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
1912 &clkspec);
1913 if (rc)
1914 return NULL;
1915
1916 if (of_property_read_string_index(clkspec.np, "clock-output-names",
1917 clkspec.args_count ? clkspec.args[0] : 0,
1918 &clk_name) < 0)
1919 clk_name = clkspec.np->name;
1920
1921 of_node_put(clkspec.np);
1922 return clk_name;
1923}
1924EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
1925
1926/**
1927 * of_clk_init() - Scan and init clock providers from the DT
1928 * @matches: array of compatible values and init functions for providers.
1929 *
1930 * This function scans the device tree for matching clock providers and
1931 * calls their initialization functions
1932 */
1933void __init of_clk_init(const struct of_device_id *matches)
1934{
1935 struct device_node *np;
1936
1937 for_each_matching_node(np, matches) {
1938 const struct of_device_id *match = of_match_node(matches, np);
1939 of_clk_init_cb_t clk_init_cb = match->data;
1940 clk_init_cb(np);
1941 }
1942}
1943#endif