1 /* arch/arm/mach-msm/clock.c
3 * Copyright (C) 2007 Google, Inc.
4 * Copyright (c) 2007-2010, Code Aurora Forum. All rights reserved.
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/err.h>
20 #include <linux/spinlock.h>
21 #include <linux/pm_qos_params.h>
25 static DEFINE_MUTEX(clocks_mutex);
26 static DEFINE_SPINLOCK(clocks_lock);
27 static LIST_HEAD(clocks);
30 * Standard clock functions defined in include/linux/clk.h
32 struct clk *clk_get(struct device *dev, const char *id)
36 mutex_lock(&clocks_mutex);
38 list_for_each_entry(clk, &clocks, list)
39 if (!strcmp(id, clk->name) && clk->dev == dev)
42 list_for_each_entry(clk, &clocks, list)
43 if (!strcmp(id, clk->name) && clk->dev == NULL)
46 clk = ERR_PTR(-ENOENT);
48 mutex_unlock(&clocks_mutex);
51 EXPORT_SYMBOL(clk_get);
53 void clk_put(struct clk *clk)
56 EXPORT_SYMBOL(clk_put);
58 int clk_enable(struct clk *clk)
61 spin_lock_irqsave(&clocks_lock, flags);
64 clk->ops->enable(clk->id);
65 spin_unlock_irqrestore(&clocks_lock, flags);
68 EXPORT_SYMBOL(clk_enable);
70 void clk_disable(struct clk *clk)
73 spin_lock_irqsave(&clocks_lock, flags);
74 BUG_ON(clk->count == 0);
77 clk->ops->disable(clk->id);
78 spin_unlock_irqrestore(&clocks_lock, flags);
80 EXPORT_SYMBOL(clk_disable);
82 int clk_reset(struct clk *clk, enum clk_reset_action action)
84 return clk->ops->reset(clk->remote_id, action);
86 EXPORT_SYMBOL(clk_reset);
88 unsigned long clk_get_rate(struct clk *clk)
90 return clk->ops->get_rate(clk->id);
92 EXPORT_SYMBOL(clk_get_rate);
94 int clk_set_rate(struct clk *clk, unsigned long rate)
97 if (clk->flags & CLKFLAG_MAX) {
98 ret = clk->ops->set_max_rate(clk->id, rate);
102 if (clk->flags & CLKFLAG_MIN) {
103 ret = clk->ops->set_min_rate(clk->id, rate);
108 if (clk->flags & CLKFLAG_MAX || clk->flags & CLKFLAG_MIN)
111 return clk->ops->set_rate(clk->id, rate);
113 EXPORT_SYMBOL(clk_set_rate);
115 long clk_round_rate(struct clk *clk, unsigned long rate)
117 return clk->ops->round_rate(clk->id, rate);
119 EXPORT_SYMBOL(clk_round_rate);
121 int clk_set_min_rate(struct clk *clk, unsigned long rate)
123 return clk->ops->set_min_rate(clk->id, rate);
125 EXPORT_SYMBOL(clk_set_min_rate);
127 int clk_set_max_rate(struct clk *clk, unsigned long rate)
129 return clk->ops->set_max_rate(clk->id, rate);
131 EXPORT_SYMBOL(clk_set_max_rate);
133 int clk_set_parent(struct clk *clk, struct clk *parent)
137 EXPORT_SYMBOL(clk_set_parent);
139 struct clk *clk_get_parent(struct clk *clk)
141 return ERR_PTR(-ENOSYS);
143 EXPORT_SYMBOL(clk_get_parent);
145 int clk_set_flags(struct clk *clk, unsigned long flags)
147 if (clk == NULL || IS_ERR(clk))
149 return clk->ops->set_flags(clk->id, flags);
151 EXPORT_SYMBOL(clk_set_flags);
153 /* EBI1 is the only shared clock that several clients want to vote on as of
154 * this commit. If this changes in the future, then it might be better to
155 * make clk_min_rate handle the voting or make ebi1_clk_set_min_rate more
156 * generic to support different clocks.
158 static struct clk *ebi1_clk;
160 void __init msm_clock_init(struct clk *clock_tbl, unsigned num_clocks)
164 mutex_lock(&clocks_mutex);
165 for (n = 0; n < num_clocks; n++)
166 list_add_tail(&clock_tbl[n].list, &clocks);
167 mutex_unlock(&clocks_mutex);
169 ebi1_clk = clk_get(NULL, "ebi1_clk");
170 BUG_ON(ebi1_clk == NULL);
174 /* The bootloader and/or AMSS may have left various clocks enabled.
175 * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have
176 * not been explicitly enabled by a clk_enable() call.
178 static int __init clock_late_init(void)
185 mutex_lock(&clocks_mutex);
186 list_for_each_entry(clk, &clocks, list) {
187 clock_debug_add(clk);
188 if (clk->flags & CLKFLAG_AUTO_OFF) {
189 spin_lock_irqsave(&clocks_lock, flags);
192 clk->ops->auto_off(clk->id);
194 spin_unlock_irqrestore(&clocks_lock, flags);
197 mutex_unlock(&clocks_mutex);
198 pr_info("clock_late_init() disabled %d unused clocks\n", count);
202 late_initcall(clock_late_init);