blob: 8c2b4dd4a8778ccf8b7f7e02bec8c3e7a4d019a2 [file] [log] [blame]
Brian Swetland600f7cf2008-09-09 11:04:14 -07001/* arch/arm/mach-msm/clock.c
2 *
3 * Copyright (C) 2007 Google, Inc.
Daniel Walker5e96da52010-05-12 13:43:28 -07004 * Copyright (c) 2007-2010, Code Aurora Forum. All rights reserved.
Brian Swetland600f7cf2008-09-09 11:04:14 -07005 *
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.
9 *
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.
14 *
15 */
16
Brian Swetland600f7cf2008-09-09 11:04:14 -070017#include <linux/kernel.h>
Brian Swetland600f7cf2008-09-09 11:04:14 -070018#include <linux/list.h>
19#include <linux/err.h>
Brian Swetland600f7cf2008-09-09 11:04:14 -070020#include <linux/spinlock.h>
Daniel Walker5e96da52010-05-12 13:43:28 -070021#include <linux/pm_qos_params.h>
Brian Swetland600f7cf2008-09-09 11:04:14 -070022
23#include "clock.h"
24#include "proc_comm.h"
Gregory Bean37a298f2010-04-30 22:22:07 -070025#include "clock-7x30.h"
Stephen Boydce1c80f2011-01-26 16:20:53 -080026#include "clock-pcom.h"
Brian Swetland600f7cf2008-09-09 11:04:14 -070027
28static DEFINE_MUTEX(clocks_mutex);
29static DEFINE_SPINLOCK(clocks_lock);
30static LIST_HEAD(clocks);
31
32/*
Brian Swetland600f7cf2008-09-09 11:04:14 -070033 * Standard clock functions defined in include/linux/clk.h
34 */
35struct clk *clk_get(struct device *dev, const char *id)
36{
37 struct clk *clk;
38
39 mutex_lock(&clocks_mutex);
40
41 list_for_each_entry(clk, &clocks, list)
42 if (!strcmp(id, clk->name) && clk->dev == dev)
43 goto found_it;
44
45 list_for_each_entry(clk, &clocks, list)
46 if (!strcmp(id, clk->name) && clk->dev == NULL)
47 goto found_it;
48
49 clk = ERR_PTR(-ENOENT);
50found_it:
51 mutex_unlock(&clocks_mutex);
52 return clk;
53}
54EXPORT_SYMBOL(clk_get);
55
56void clk_put(struct clk *clk)
57{
58}
59EXPORT_SYMBOL(clk_put);
60
61int clk_enable(struct clk *clk)
62{
63 unsigned long flags;
64 spin_lock_irqsave(&clocks_lock, flags);
65 clk->count++;
Stephen Boyd437f6292011-01-26 16:20:52 -080066 if (clk->count == 1)
Daniel Walker5e96da52010-05-12 13:43:28 -070067 clk->ops->enable(clk->id);
Brian Swetland600f7cf2008-09-09 11:04:14 -070068 spin_unlock_irqrestore(&clocks_lock, flags);
69 return 0;
70}
71EXPORT_SYMBOL(clk_enable);
72
73void clk_disable(struct clk *clk)
74{
75 unsigned long flags;
76 spin_lock_irqsave(&clocks_lock, flags);
77 BUG_ON(clk->count == 0);
78 clk->count--;
Stephen Boyd437f6292011-01-26 16:20:52 -080079 if (clk->count == 0)
Daniel Walker5e96da52010-05-12 13:43:28 -070080 clk->ops->disable(clk->id);
Brian Swetland600f7cf2008-09-09 11:04:14 -070081 spin_unlock_irqrestore(&clocks_lock, flags);
82}
83EXPORT_SYMBOL(clk_disable);
84
Daniel Walker5e96da52010-05-12 13:43:28 -070085int clk_reset(struct clk *clk, enum clk_reset_action action)
86{
87 if (!clk->ops->reset)
88 clk->ops->reset = &pc_clk_reset;
89 return clk->ops->reset(clk->remote_id, action);
90}
91EXPORT_SYMBOL(clk_reset);
92
Brian Swetland600f7cf2008-09-09 11:04:14 -070093unsigned long clk_get_rate(struct clk *clk)
94{
Daniel Walker5e96da52010-05-12 13:43:28 -070095 return clk->ops->get_rate(clk->id);
Brian Swetland600f7cf2008-09-09 11:04:14 -070096}
97EXPORT_SYMBOL(clk_get_rate);
98
99int clk_set_rate(struct clk *clk, unsigned long rate)
100{
Daniel Walker3a790bb2010-12-13 14:35:10 -0800101 int ret;
102 if (clk->flags & CLKFLAG_MAX) {
103 ret = clk->ops->set_max_rate(clk->id, rate);
104 if (ret)
105 return ret;
106 }
107 if (clk->flags & CLKFLAG_MIN) {
108 ret = clk->ops->set_min_rate(clk->id, rate);
109 if (ret)
110 return ret;
111 }
112
113 if (clk->flags & CLKFLAG_MAX || clk->flags & CLKFLAG_MIN)
114 return ret;
115
Daniel Walker5e96da52010-05-12 13:43:28 -0700116 return clk->ops->set_rate(clk->id, rate);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700117}
118EXPORT_SYMBOL(clk_set_rate);
119
Daniel Walker5e96da52010-05-12 13:43:28 -0700120long clk_round_rate(struct clk *clk, unsigned long rate)
121{
122 return clk->ops->round_rate(clk->id, rate);
123}
124EXPORT_SYMBOL(clk_round_rate);
125
126int clk_set_min_rate(struct clk *clk, unsigned long rate)
127{
128 return clk->ops->set_min_rate(clk->id, rate);
129}
130EXPORT_SYMBOL(clk_set_min_rate);
131
132int clk_set_max_rate(struct clk *clk, unsigned long rate)
133{
134 return clk->ops->set_max_rate(clk->id, rate);
135}
136EXPORT_SYMBOL(clk_set_max_rate);
137
Brian Swetland600f7cf2008-09-09 11:04:14 -0700138int clk_set_parent(struct clk *clk, struct clk *parent)
139{
140 return -ENOSYS;
141}
142EXPORT_SYMBOL(clk_set_parent);
143
144struct clk *clk_get_parent(struct clk *clk)
145{
146 return ERR_PTR(-ENOSYS);
147}
148EXPORT_SYMBOL(clk_get_parent);
149
150int clk_set_flags(struct clk *clk, unsigned long flags)
151{
152 if (clk == NULL || IS_ERR(clk))
153 return -EINVAL;
Daniel Walker5e96da52010-05-12 13:43:28 -0700154 return clk->ops->set_flags(clk->id, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700155}
156EXPORT_SYMBOL(clk_set_flags);
157
Daniel Walker5e96da52010-05-12 13:43:28 -0700158/* EBI1 is the only shared clock that several clients want to vote on as of
159 * this commit. If this changes in the future, then it might be better to
160 * make clk_min_rate handle the voting or make ebi1_clk_set_min_rate more
161 * generic to support different clocks.
162 */
163static struct clk *ebi1_clk;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700164
Daniel Walker5e96da52010-05-12 13:43:28 -0700165static void __init set_clock_ops(struct clk *clk)
166{
167 if (!clk->ops) {
168 clk->ops = &clk_ops_pcom;
169 clk->id = clk->remote_id;
170 }
171}
172
173void __init msm_clock_init(struct clk *clock_tbl, unsigned num_clocks)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700174{
175 unsigned n;
176
Brian Swetland600f7cf2008-09-09 11:04:14 -0700177 mutex_lock(&clocks_mutex);
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800178 for (n = 0; n < num_clocks; n++) {
179 set_clock_ops(&clock_tbl[n]);
180 list_add_tail(&clock_tbl[n].list, &clocks);
Daniel Walker5e96da52010-05-12 13:43:28 -0700181 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700182 mutex_unlock(&clocks_mutex);
Daniel Walker5e96da52010-05-12 13:43:28 -0700183
184 ebi1_clk = clk_get(NULL, "ebi1_clk");
185 BUG_ON(ebi1_clk == NULL);
186
Brian Swetland600f7cf2008-09-09 11:04:14 -0700187}
188
189/* The bootloader and/or AMSS may have left various clocks enabled.
190 * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have
191 * not been explicitly enabled by a clk_enable() call.
192 */
193static int __init clock_late_init(void)
194{
195 unsigned long flags;
196 struct clk *clk;
197 unsigned count = 0;
198
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800199 clock_debug_init();
Brian Swetland600f7cf2008-09-09 11:04:14 -0700200 mutex_lock(&clocks_mutex);
201 list_for_each_entry(clk, &clocks, list) {
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800202 clock_debug_add(clk);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700203 if (clk->flags & CLKFLAG_AUTO_OFF) {
204 spin_lock_irqsave(&clocks_lock, flags);
205 if (!clk->count) {
206 count++;
Daniel Walker5e96da52010-05-12 13:43:28 -0700207 clk->ops->auto_off(clk->id);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700208 }
209 spin_unlock_irqrestore(&clocks_lock, flags);
210 }
211 }
212 mutex_unlock(&clocks_mutex);
213 pr_info("clock_late_init() disabled %d unused clocks\n", count);
214 return 0;
215}
216
217late_initcall(clock_late_init);
Daniel Walker5e96da52010-05-12 13:43:28 -0700218