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 if (clk->notifier_count)
284 clk_notify(clk, ret ? CLK_ABORT_RATE_CHANGE : CLK_POST_RATE_CHANGE, old_rate, clk->rate);
289 int clk_set_parent_nolock(struct clk *clk, struct clk *parent)
292 int enabled = clk->usecount > 0;
293 struct clk *old_parent = clk->parent;
295 if (clk->parent == parent)
298 /* if clk is already enabled, enable new parent first and disable old parent later. */
300 clk_enable_nolock(parent);
303 ret = clk->set_parent(clk, parent);
305 ret = clk_default_set_parent(clk,parent);
310 //CLOCK_PRINTK_DBG("set_parent %s reparent\n",clk->name,parent->name);
311 __clk_reparent(clk, parent);
313 __propagate_rate(clk);
315 clk_disable_nolock(old_parent);
317 //CLOCK_PRINTK_DBG("set_parent err\n",clk->name,parent->name);
319 clk_disable_nolock(parent);
324 /**********************************dvfs****************************************************/
325 int clk_set_rate_locked(struct clk * clk,unsigned long rate)
328 //CLOCK_PRINTK_DBG("%s dvfs clk_set_locked\n",clk->name);
330 ret=clk_set_rate_nolock(clk, rate);;
335 void clk_register_dvfs(struct clk_node *dvfs_clk, struct clk *clk)
337 clk->dvfs_info = dvfs_clk;
339 int clk_set_enable_locked(struct clk * clk,int on)
344 ret=clk_enable_nolock(clk);
346 clk_disable_nolock(clk);
350 EXPORT_SYMBOL(clk_set_enable_locked);
351 /*-------------------------------------------------------------------------
352 * Optional clock functions defined in include/linux/clk.h
353 *-------------------------------------------------------------------------*/
354 #ifdef RK30_CLK_OFFBOARD_TEST
355 long rk30_clk_round_rate(struct clk *clk, unsigned long rate)
357 long clk_round_rate(struct clk *clk, unsigned long rate)
362 if (clk == NULL || IS_ERR(clk))
366 ret = clk_round_rate_nolock(clk, rate);
372 #ifdef RK30_CLK_OFFBOARD_TEST
373 EXPORT_SYMBOL(rk30_clk_round_rate);
375 EXPORT_SYMBOL(clk_round_rate);
378 #ifdef RK30_CLK_OFFBOARD_TEST
379 unsigned long rk30_clk_get_rate(struct clk *clk)
381 unsigned long clk_get_rate(struct clk *clk)
384 if (clk == NULL || IS_ERR(clk))
389 #ifdef RK30_CLK_OFFBOARD_TEST
390 EXPORT_SYMBOL(rk30_clk_get_rate);
392 EXPORT_SYMBOL(clk_get_rate);
396 /* Set the clock rate for a clock source */
397 #ifdef RK30_CLK_OFFBOARD_TEST
398 int rk30_clk_set_rate(struct clk *clk, unsigned long rate)
400 int clk_set_rate(struct clk *clk, unsigned long rate)
404 if (clk == NULL || IS_ERR(clk)){
407 if (rate == clk->rate)
409 if (dvfs_support_clk_set_rate(clk->dvfs_info)==true)
410 return dvfs_vd_clk_set_rate(clk, rate);
413 ret = clk_set_rate_nolock(clk, rate);
418 #ifdef RK30_CLK_OFFBOARD_TEST
419 EXPORT_SYMBOL(rk30_clk_set_rate);
421 EXPORT_SYMBOL(clk_set_rate);
425 #ifdef RK30_CLK_OFFBOARD_TEST
426 int rk30_clk_set_parent(struct clk *clk, struct clk *parent)
428 int clk_set_parent(struct clk *clk, struct clk *parent)
433 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
436 if (clk->parents == NULL)
440 if (clk->usecount == 0)
441 ret = clk_set_parent_nolock(clk, parent);
448 int clk_set_parent_force(struct clk *clk, struct clk *parent)
452 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
455 if (clk->parents == NULL)
458 ret = clk_set_parent_nolock(clk, parent);
463 #ifdef RK30_CLK_OFFBOARD_TEST
464 EXPORT_SYMBOL(rk30_clk_set_parent);
466 EXPORT_SYMBOL(clk_set_parent);
469 #ifdef RK30_CLK_OFFBOARD_TEST
470 struct clk *rk30_clk_get_parent(struct clk *clk)
472 struct clk *clk_get_parent(struct clk *clk)
475 if (clk == NULL || IS_ERR(clk)) {
476 return ERR_PTR(-EINVAL);
481 #ifdef RK30_CLK_OFFBOARD_TEST
482 EXPORT_SYMBOL(rk30_clk_get_parent);
484 EXPORT_SYMBOL(clk_get_parent);
487 #ifdef RK30_CLK_OFFBOARD_TEST
488 void rk30_clk_disable(struct clk *clk)
490 void clk_disable(struct clk *clk)
493 if (clk == NULL || IS_ERR(clk))
495 if (dvfs_support_clk_disable(clk->dvfs_info) == true) {
496 dvfs_vd_clk_disable(clk, 0);
501 clk_disable_nolock(clk);
504 #ifdef RK30_CLK_OFFBOARD_TEST
505 EXPORT_SYMBOL(rk30_clk_disable);
507 EXPORT_SYMBOL(clk_disable);
510 #ifdef RK30_CLK_OFFBOARD_TEST
511 int rk30_clk_enable(struct clk *clk)
513 int clk_enable(struct clk *clk)
518 if (clk == NULL || IS_ERR(clk))
520 if (dvfs_support_clk_disable(clk->dvfs_info)==true)
521 return dvfs_vd_clk_disable(clk, 1);
524 ret = clk_enable_nolock(clk);
529 #ifdef RK30_CLK_OFFBOARD_TEST
530 EXPORT_SYMBOL(rk30_clk_enable);
532 EXPORT_SYMBOL(clk_enable);
535 /* Clk notifier implementation */
538 * struct clk_notifier - associate a clk with a notifier
539 * @clk: struct clk * to associate the notifier with
540 * @notifier_head: a raw_notifier_head for this clk
541 * @node: linked list pointers
543 * A list of struct clk_notifier is maintained by the notifier code.
544 * An entry is created whenever code registers the first notifier on a
545 * particular @clk. Future notifiers on that @clk are added to the
548 struct clk_notifier {
550 struct raw_notifier_head notifier_head;
551 struct list_head node;
553 static LIST_HEAD(clk_notifier_list);
555 * _clk_free_notifier_chain - safely remove struct clk_notifier
556 * @cn: struct clk_notifier *
558 * Removes the struct clk_notifier @cn from the clk_notifier_list and
561 static void _clk_free_notifier_chain(struct clk_notifier *cn)
568 * clk_notify - call clk notifier chain
569 * @clk: struct clk * that is changing rate
570 * @msg: clk notifier type (i.e., CLK_POST_RATE_CHANGE; see mach/clock.h)
571 * @old_rate: old rate
572 * @new_rate: new rate
574 * Triggers a notifier call chain on the post-clk-rate-change notifier
575 * for clock 'clk'. Passes a pointer to the struct clk and the
576 * previous and current rates to the notifier callback. Intended to be
577 * called by internal clock code only. No return value.
579 static void clk_notify(struct clk *clk, unsigned long msg,
580 unsigned long old_rate, unsigned long new_rate)
582 struct clk_notifier *cn;
583 struct clk_notifier_data cnd;
586 cnd.old_rate = old_rate;
587 cnd.new_rate = new_rate;
590 list_for_each_entry(cn, &clk_notifier_list, node) {
591 if (cn->clk == clk) {
592 pr_debug("%s msg %lu rate %lu -> %lu\n", clk->name, msg, old_rate, new_rate);
593 raw_notifier_call_chain(&cn->notifier_head, msg, &cnd);
601 * clk_notifier_register - add a clock parameter change notifier
602 * @clk: struct clk * to watch
603 * @nb: struct notifier_block * with callback info
605 * Request notification for changes to the clock 'clk'. This uses a
606 * blocking notifier. Callback code must not call into the clock
607 * framework, as clocks_mutex is held. Pre-notifier callbacks will be
608 * passed the previous and new rate of the clock.
610 * clk_notifier_register() must be called from process
611 * context. Returns -EINVAL if called with null arguments, -ENOMEM
612 * upon allocation failure; otherwise, passes along the return value
613 * of blocking_notifier_chain_register().
615 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
617 struct clk_notifier *cn = NULL, *cn_new = NULL;
621 if (!clk || IS_ERR(clk) || !nb)
624 mutex_lock(&clocks_mutex);
626 list_for_each_entry(cn, &clk_notifier_list, node)
630 if (cn->clk != clk) {
631 cn_new = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
638 RAW_INIT_NOTIFIER_HEAD(&cn_new->notifier_head);
640 list_add(&cn_new->node, &clk_notifier_list);
644 r = raw_notifier_chain_register(&cn->notifier_head, nb);
645 if (!IS_ERR_VALUE(r)) {
648 clkp->notifier_count++;
649 } while ((clkp = clkp->parent));
652 _clk_free_notifier_chain(cn);
656 mutex_unlock(&clocks_mutex);
660 EXPORT_SYMBOL(clk_notifier_register);
663 * clk_notifier_unregister - remove a clock change notifier
665 * @nb: struct notifier_block * with callback info
667 * Request no further notification for changes to clock 'clk'.
668 * Returns -EINVAL if called with null arguments; otherwise, passes
669 * along the return value of blocking_notifier_chain_unregister().
671 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
673 struct clk_notifier *cn = NULL;
677 if (!clk || IS_ERR(clk) || !nb)
680 mutex_lock(&clocks_mutex);
682 list_for_each_entry(cn, &clk_notifier_list, node)
686 if (cn->clk != clk) {
691 r = raw_notifier_chain_unregister(&cn->notifier_head, nb);
692 if (!IS_ERR_VALUE(r)) {
695 clkp->notifier_count--;
696 } while ((clkp = clkp->parent));
700 * XXX ugh, layering violation. There should be some
701 * support in the notifier code for this.
703 if (!cn->notifier_head.head)
704 _clk_free_notifier_chain(cn);
707 mutex_unlock(&clocks_mutex);
711 EXPORT_SYMBOL(clk_notifier_unregister);
713 #ifdef CONFIG_PROC_FS
714 static struct clk_dump_ops *dump_def_ops;
716 void clk_register_dump_ops(struct clk_dump_ops *ops)
722 #ifdef CONFIG_RK_CLOCK_PROC
723 static int proc_clk_show(struct seq_file *s, void *v)
730 if(dump_def_ops->dump_clk)
732 mutex_lock(&clocks_mutex);
733 list_for_each_entry(clk, &clocks, node) {
736 dump_def_ops->dump_clk(s, clk, 0,&clocks);
739 mutex_unlock(&clocks_mutex);
741 if(dump_def_ops->dump_regs)
742 dump_def_ops->dump_regs(s);
747 static int proc_clk_open(struct inode *inode, struct file *file)
749 return single_open(file, proc_clk_show, NULL);
752 static const struct file_operations proc_clk_fops = {
753 .open = proc_clk_open,
756 .release = single_release,
759 static int __init clk_proc_init(void)
761 proc_create("clocks", S_IFREG | S_IRUSR | S_IRGRP, NULL, &proc_clk_fops);
765 late_initcall(clk_proc_init);
766 #endif /* CONFIG_RK_CLOCK_PROC */