1 /* linux/arch/arm/mach-rk30/clock.c
3 * Copyright (C) 2012 ROCKCHIP, Inc.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/clkdev.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/hardirq.h>
23 #include <linux/delay.h>
24 #include <mach/clock.h>
25 #include <mach/dvfs.h>
26 #include <linux/delay.h>
28 #define CLOCK_PRINTK_DBG(fmt, args...) pr_debug(fmt, ## args);
29 #define CLOCK_PRINTK_ERR(fmt, args...) pr_err(fmt, ## args);
30 #define CLOCK_PRINTK_LOG(fmt, args...) pr_debug(fmt, ## args);
34 #define RATE_FIXED (1 << 1) /* Fixed clock rate */
35 #define CONFIG_PARTICIPANT (1 << 10) /* Fundamental clock */
37 #define MHZ (1000*1000)
40 static void __clk_recalc(struct clk *clk);
41 static void __propagate_rate(struct clk *tclk);
42 static void __clk_reparent(struct clk *child, struct clk *parent);
44 static LIST_HEAD(clocks);
45 static DEFINE_MUTEX(clocks_mutex);
46 static DEFINE_SPINLOCK(clockfw_lock);
47 static LIST_HEAD(root_clks);
48 static void clk_notify(struct clk *clk, unsigned long msg,
49 unsigned long old_rate, unsigned long new_rate);
51 #define LOCK() do { WARN_ON(in_irq()); if (!irqs_disabled()) spin_lock_bh(&clockfw_lock); } while (0)
52 #define UNLOCK() do { if (!irqs_disabled()) spin_unlock_bh(&clockfw_lock); } while (0)
53 /**********************************************for clock data****************************************************/
54 struct list_head *get_rk_clocks_head(void)
59 static struct clk *def_ops_clk=NULL;
61 void clk_register_default_ops_clk(struct clk *clk)
66 static struct clk *clk_default_get_parent(struct clk *clk)
68 if(def_ops_clk&&def_ops_clk->get_parent)
69 return def_ops_clk->get_parent(clk);
75 static int clk_default_set_parent(struct clk *clk, struct clk *parent)
77 if(def_ops_clk&&def_ops_clk->set_parent)
78 return def_ops_clk->set_parent(clk,parent);
83 int __init clk_disable_unused(void)
86 list_for_each_entry(ck, &clocks, node) {
87 if (ck->usecount > 0 || ck->mode == NULL || (ck->flags & IS_PD))
90 clk_enable_nolock(ck);
91 clk_disable_nolock(ck);
97 * recalculate_root_clocks - recalculate and propagate all root clocks
99 * Recalculates all root clocks (clocks with no parent), which if the
100 * clock's .recalc is set correctly, should also propagate their rates.
103 void clk_recalculate_root_clocks_nolock(void)
107 list_for_each_entry(clkp, &root_clks, sibling) {
109 __propagate_rate(clkp);
113 void clk_recalculate_root_clocks(void)
116 clk_recalculate_root_clocks_nolock();
121 * clk_preinit - initialize any fields in the struct clk before clk init
122 * @clk: struct clk * to initialize
124 * Initialize any struct clk fields needed before normal clk initialization
125 * can run. No return value.
127 int clk_register(struct clk *clk)
129 if (clk == NULL || IS_ERR(clk))
131 //INIT_LIST_HEAD(&clk->sibling);
132 INIT_LIST_HEAD(&clk->children);
135 * trap out already registered clocks
137 if (clk->node.next || clk->node.prev)
140 mutex_lock(&clocks_mutex);
142 clk->parent = clk->get_parent(clk);
143 else if (clk->parents)
144 clk->parent =clk_default_get_parent(clk);
147 list_add(&clk->sibling, &clk->parent->children);
149 list_add(&clk->sibling, &root_clks);
150 list_add(&clk->node, &clocks);
151 mutex_unlock(&clocks_mutex);
155 /************************************************************/
156 static void __clk_recalc(struct clk *clk)
158 if (unlikely(clk->flags & RATE_FIXED))
161 clk->rate = clk->recalc(clk);
162 else if (clk->parent)
163 clk->rate = clk->parent->rate;
165 static void __clk_reparent(struct clk *child, struct clk *parent)
167 if (child->parent == parent)
169 //CLOCK_PRINTK_DBG("%s reparent to %s (was %s)\n", child->name, parent->name, ((child->parent) ? child->parent->name : "NULL"));
171 list_del_init(&child->sibling);
173 list_add(&child->sibling, &parent->children);
174 child->parent = parent;
177 /* Propagate rate to children */
178 static void __propagate_rate(struct clk *tclk)
182 //CLOCK_PRINTK_DBG("propagate_rate clk %s\n",clkp->name);
184 list_for_each_entry(clkp, &tclk->children, sibling) {
186 __propagate_rate(clkp);
188 //CLOCK_PRINTK_DBG("propagate_rate clk %s end\n",clkp->name);
191 int clk_enable_nolock(struct clk *clk)
195 if (clk->usecount == 0) {
197 ret = clk_enable_nolock(clk->parent);
202 if (clk->notifier_count)
203 clk_notify(clk, CLK_PRE_ENABLE, clk->rate, clk->rate);
205 ret = clk->mode(clk, 1);
206 if (clk->notifier_count)
207 clk_notify(clk, ret ? CLK_ABORT_ENABLE : CLK_POST_ENABLE, clk->rate, clk->rate);
210 clk_disable_nolock(clk->parent);
213 pr_debug("%s enabled\n", clk->name);
220 void clk_disable_nolock(struct clk *clk)
222 if (clk->usecount == 0) {
223 CLOCK_PRINTK_ERR(KERN_ERR "Trying disable clock %s with 0 usecount\n", clk->name);
227 if (--clk->usecount == 0) {
229 if (clk->notifier_count)
230 clk_notify(clk, CLK_PRE_DISABLE, clk->rate, clk->rate);
232 ret = clk->mode(clk, 0);
233 if (clk->notifier_count)
234 clk_notify(clk, ret ? CLK_ABORT_DISABLE : CLK_POST_DISABLE, clk->rate, clk->rate);
235 pr_debug("%s disabled\n", clk->name);
236 if (ret == 0 && clk->parent)
237 clk_disable_nolock(clk->parent);
240 /* Given a clock and a rate apply a clock specific rounding function */
241 long clk_round_rate_nolock(struct clk *clk, unsigned long rate)
244 return clk->round_rate(clk, rate);
246 if (clk->flags & RATE_FIXED)
247 CLOCK_PRINTK_ERR("clock: clk_round_rate called on fixed-rate clock %s\n", clk->name);
251 int is_suport_round_rate(struct clk *clk)
253 return (clk->round_rate) ? 0:(-1);
256 int clk_set_rate_nolock(struct clk *clk, unsigned long rate)
259 unsigned long old_rate;
261 if (rate == clk->rate)
263 if (clk->flags & CONFIG_PARTICIPANT)
269 //CLOCK_PRINTK_LOG("**will set %s rate %lu\n", clk->name, rate);
271 old_rate = clk->rate;
272 if (clk->notifier_count)
273 clk_notify(clk, CLK_PRE_RATE_CHANGE, old_rate, rate);
275 ret = clk->set_rate(clk, rate);
279 CLOCK_PRINTK_LOG("**set %s rate recalc=%lu\n",clk->name,clk->rate);
280 __propagate_rate(clk);
283 clk->last_set_rate = rate;
285 if (clk->notifier_count)
286 clk_notify(clk, ret ? CLK_ABORT_RATE_CHANGE : CLK_POST_RATE_CHANGE, old_rate, clk->rate);
291 int clk_set_parent_nolock(struct clk *clk, struct clk *parent)
294 int enabled = clk->usecount > 0;
295 struct clk *old_parent = clk->parent;
297 if (clk->parent == parent)
300 /* if clk is already enabled, enable new parent first and disable old parent later. */
302 clk_enable_nolock(parent);
305 ret = clk->set_parent(clk, parent);
307 ret = clk_default_set_parent(clk,parent);
312 //CLOCK_PRINTK_DBG("set_parent %s reparent\n",clk->name,parent->name);
313 __clk_reparent(clk, parent);
315 __propagate_rate(clk);
317 clk_disable_nolock(old_parent);
319 //CLOCK_PRINTK_DBG("set_parent err\n",clk->name,parent->name);
321 clk_disable_nolock(parent);
326 /**********************************dvfs****************************************************/
327 int clk_set_rate_locked(struct clk * clk,unsigned long rate)
330 //CLOCK_PRINTK_DBG("%s dvfs clk_set_locked\n",clk->name);
332 ret=clk_set_rate_nolock(clk, rate);;
337 void clk_register_dvfs(struct clk_node *dvfs_clk, struct clk *clk)
339 clk->dvfs_info = dvfs_clk;
341 int clk_set_enable_locked(struct clk * clk,int on)
346 ret=clk_enable_nolock(clk);
348 clk_disable_nolock(clk);
352 EXPORT_SYMBOL(clk_set_enable_locked);
353 /*-------------------------------------------------------------------------
354 * Optional clock functions defined in include/linux/clk.h
355 *-------------------------------------------------------------------------*/
356 #ifdef RK30_CLK_OFFBOARD_TEST
357 long rk30_clk_round_rate(struct clk *clk, unsigned long rate)
359 long clk_round_rate(struct clk *clk, unsigned long rate)
364 if (clk == NULL || IS_ERR(clk))
368 ret = clk_round_rate_nolock(clk, rate);
374 #ifdef RK30_CLK_OFFBOARD_TEST
375 EXPORT_SYMBOL(rk30_clk_round_rate);
377 EXPORT_SYMBOL(clk_round_rate);
380 #ifdef RK30_CLK_OFFBOARD_TEST
381 unsigned long rk30_clk_get_rate(struct clk *clk)
383 unsigned long clk_get_rate(struct clk *clk)
386 if (clk == NULL || IS_ERR(clk))
391 #ifdef RK30_CLK_OFFBOARD_TEST
392 EXPORT_SYMBOL(rk30_clk_get_rate);
394 EXPORT_SYMBOL(clk_get_rate);
398 /* Set the clock rate for a clock source */
399 #ifdef RK30_CLK_OFFBOARD_TEST
400 int rk30_clk_set_rate(struct clk *clk, unsigned long rate)
402 int clk_set_rate(struct clk *clk, unsigned long rate)
406 if (clk == NULL || IS_ERR(clk)){
409 if (rate == clk->rate)
411 if (dvfs_support_clk_set_rate(clk->dvfs_info)==true)
412 return dvfs_vd_clk_set_rate(clk, rate);
415 ret = clk_set_rate_nolock(clk, rate);
420 #ifdef RK30_CLK_OFFBOARD_TEST
421 EXPORT_SYMBOL(rk30_clk_set_rate);
423 EXPORT_SYMBOL(clk_set_rate);
427 #ifdef RK30_CLK_OFFBOARD_TEST
428 int rk30_clk_set_parent(struct clk *clk, struct clk *parent)
430 int clk_set_parent(struct clk *clk, struct clk *parent)
435 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
438 if (clk->parents == NULL)
442 if (clk->usecount == 0)
443 ret = clk_set_parent_nolock(clk, parent);
450 int clk_set_parent_force(struct clk *clk, struct clk *parent)
454 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
457 if (clk->parents == NULL)
460 ret = clk_set_parent_nolock(clk, parent);
465 #ifdef RK30_CLK_OFFBOARD_TEST
466 EXPORT_SYMBOL(rk30_clk_set_parent);
468 EXPORT_SYMBOL(clk_set_parent);
471 #ifdef RK30_CLK_OFFBOARD_TEST
472 struct clk *rk30_clk_get_parent(struct clk *clk)
474 struct clk *clk_get_parent(struct clk *clk)
477 if (clk == NULL || IS_ERR(clk)) {
478 return ERR_PTR(-EINVAL);
483 #ifdef RK30_CLK_OFFBOARD_TEST
484 EXPORT_SYMBOL(rk30_clk_get_parent);
486 EXPORT_SYMBOL(clk_get_parent);
489 #ifdef RK30_CLK_OFFBOARD_TEST
490 void rk30_clk_disable(struct clk *clk)
492 void clk_disable(struct clk *clk)
495 if (clk == NULL || IS_ERR(clk))
497 if (dvfs_support_clk_disable(clk->dvfs_info) == true) {
498 dvfs_vd_clk_disable(clk, 0);
503 clk_disable_nolock(clk);
506 #ifdef RK30_CLK_OFFBOARD_TEST
507 EXPORT_SYMBOL(rk30_clk_disable);
509 EXPORT_SYMBOL(clk_disable);
512 #ifdef RK30_CLK_OFFBOARD_TEST
513 int rk30_clk_enable(struct clk *clk)
515 int clk_enable(struct clk *clk)
520 if (clk == NULL || IS_ERR(clk))
522 if (dvfs_support_clk_disable(clk->dvfs_info)==true)
523 return dvfs_vd_clk_disable(clk, 1);
526 ret = clk_enable_nolock(clk);
531 #ifdef RK30_CLK_OFFBOARD_TEST
532 EXPORT_SYMBOL(rk30_clk_enable);
534 EXPORT_SYMBOL(clk_enable);
537 /* Clk notifier implementation */
540 * struct clk_notifier - associate a clk with a notifier
541 * @clk: struct clk * to associate the notifier with
542 * @notifier_head: a raw_notifier_head for this clk
543 * @node: linked list pointers
545 * A list of struct clk_notifier is maintained by the notifier code.
546 * An entry is created whenever code registers the first notifier on a
547 * particular @clk. Future notifiers on that @clk are added to the
550 struct clk_notifier {
552 struct raw_notifier_head notifier_head;
553 struct list_head node;
555 static LIST_HEAD(clk_notifier_list);
557 * _clk_free_notifier_chain - safely remove struct clk_notifier
558 * @cn: struct clk_notifier *
560 * Removes the struct clk_notifier @cn from the clk_notifier_list and
563 static void _clk_free_notifier_chain(struct clk_notifier *cn)
570 * clk_notify - call clk notifier chain
571 * @clk: struct clk * that is changing rate
572 * @msg: clk notifier type (i.e., CLK_POST_RATE_CHANGE; see mach/clock.h)
573 * @old_rate: old rate
574 * @new_rate: new rate
576 * Triggers a notifier call chain on the post-clk-rate-change notifier
577 * for clock 'clk'. Passes a pointer to the struct clk and the
578 * previous and current rates to the notifier callback. Intended to be
579 * called by internal clock code only. No return value.
581 static void clk_notify(struct clk *clk, unsigned long msg,
582 unsigned long old_rate, unsigned long new_rate)
584 struct clk_notifier *cn;
585 struct clk_notifier_data cnd;
588 cnd.old_rate = old_rate;
589 cnd.new_rate = new_rate;
592 list_for_each_entry(cn, &clk_notifier_list, node) {
593 if (cn->clk == clk) {
594 pr_debug("%s msg %lu rate %lu -> %lu\n", clk->name, msg, old_rate, new_rate);
595 raw_notifier_call_chain(&cn->notifier_head, msg, &cnd);
603 * clk_notifier_register - add a clock parameter change notifier
604 * @clk: struct clk * to watch
605 * @nb: struct notifier_block * with callback info
607 * Request notification for changes to the clock 'clk'. This uses a
608 * blocking notifier. Callback code must not call into the clock
609 * framework, as clocks_mutex is held. Pre-notifier callbacks will be
610 * passed the previous and new rate of the clock.
612 * clk_notifier_register() must be called from process
613 * context. Returns -EINVAL if called with null arguments, -ENOMEM
614 * upon allocation failure; otherwise, passes along the return value
615 * of blocking_notifier_chain_register().
617 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
619 struct clk_notifier *cn = NULL, *cn_new = NULL;
623 if (!clk || IS_ERR(clk) || !nb)
626 mutex_lock(&clocks_mutex);
628 list_for_each_entry(cn, &clk_notifier_list, node)
632 if (cn->clk != clk) {
633 cn_new = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
640 RAW_INIT_NOTIFIER_HEAD(&cn_new->notifier_head);
642 list_add(&cn_new->node, &clk_notifier_list);
646 r = raw_notifier_chain_register(&cn->notifier_head, nb);
647 if (!IS_ERR_VALUE(r)) {
650 clkp->notifier_count++;
651 } while ((clkp = clkp->parent));
654 _clk_free_notifier_chain(cn);
658 mutex_unlock(&clocks_mutex);
662 EXPORT_SYMBOL(clk_notifier_register);
665 * clk_notifier_unregister - remove a clock change notifier
667 * @nb: struct notifier_block * with callback info
669 * Request no further notification for changes to clock 'clk'.
670 * Returns -EINVAL if called with null arguments; otherwise, passes
671 * along the return value of blocking_notifier_chain_unregister().
673 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
675 struct clk_notifier *cn = NULL;
679 if (!clk || IS_ERR(clk) || !nb)
682 mutex_lock(&clocks_mutex);
684 list_for_each_entry(cn, &clk_notifier_list, node)
688 if (cn->clk != clk) {
693 r = raw_notifier_chain_unregister(&cn->notifier_head, nb);
694 if (!IS_ERR_VALUE(r)) {
697 clkp->notifier_count--;
698 } while ((clkp = clkp->parent));
702 * XXX ugh, layering violation. There should be some
703 * support in the notifier code for this.
705 if (!cn->notifier_head.head)
706 _clk_free_notifier_chain(cn);
709 mutex_unlock(&clocks_mutex);
713 EXPORT_SYMBOL(clk_notifier_unregister);
715 #ifdef CONFIG_PROC_FS
716 static struct clk_dump_ops *dump_def_ops;
718 void clk_register_dump_ops(struct clk_dump_ops *ops)
724 #ifdef CONFIG_RK_CLOCK_PROC
725 static int proc_clk_show(struct seq_file *s, void *v)
732 if(dump_def_ops->dump_clk)
734 mutex_lock(&clocks_mutex);
735 list_for_each_entry(clk, &clocks, node) {
738 dump_def_ops->dump_clk(s, clk, 0,&clocks);
741 mutex_unlock(&clocks_mutex);
743 if(dump_def_ops->dump_regs)
744 dump_def_ops->dump_regs(s);
749 static int proc_clk_open(struct inode *inode, struct file *file)
751 return single_open(file, proc_clk_show, NULL);
754 static const struct file_operations proc_clk_fops = {
755 .open = proc_clk_open,
758 .release = single_release,
761 static int __init clk_proc_init(void)
763 proc_create("clocks", S_IFREG | S_IRUSR | S_IRGRP, NULL, &proc_clk_fops);
767 late_initcall(clk_proc_init);
768 #endif /* CONFIG_RK_CLOCK_PROC */
770 static int clk_panic(struct notifier_block *this, unsigned long ev, void *ptr)
773 #define CRU_BASE RK30_CRU_BASE
774 #elif defined(RK2928_CRU_BASE)
775 #define CRU_BASE RK2928_CRU_BASE
778 print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_ADDRESS, 16, 4, CRU_BASE, 0x150, false);
783 static struct notifier_block clk_panic_block = {
784 .notifier_call = clk_panic,
787 static int __init clk_panic_init(void)
789 return atomic_notifier_chain_register(&panic_notifier_list, &clk_panic_block);
791 pure_initcall(clk_panic_init);