2 * Based on arch/arm/plat-omap/clock.c
4 * Copyright (C) 2004 - 2005 Nokia corporation
5 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
6 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
7 * Copyright 2007 Freescale Semiconductor, Inc. All Rights Reserved.
8 * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 #include <linux/clk.h>
28 #include <linux/err.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/list.h>
34 #include <linux/module.h>
35 #include <linux/mutex.h>
36 #include <linux/platform_device.h>
37 #include <linux/proc_fs.h>
38 #include <linux/semaphore.h>
39 #include <linux/string.h>
41 #include <mach/clock.h>
43 static LIST_HEAD(clocks
);
44 static DEFINE_MUTEX(clocks_mutex
);
46 /*-------------------------------------------------------------------------
47 * Standard clock functions defined in include/linux/clk.h
48 *-------------------------------------------------------------------------*/
51 * Retrieve a clock by name.
53 * Note that we first try to use device id on the bus
54 * and clock name. If this fails, we try to use "<name>.<id>". If this fails,
55 * we try to use clock name only.
56 * The reference count to the clock's module owner ref count is incremented.
58 struct clk
*clk_get(struct device
*dev
, const char *id
)
60 struct clk
*p
, *clk
= ERR_PTR(-ENOENT
);
67 if (dev
== NULL
|| dev
->bus
!= &platform_bus_type
)
70 idno
= to_platform_device(dev
)->id
;
72 mutex_lock(&clocks_mutex
);
74 list_for_each_entry(p
, &clocks
, node
) {
76 strcmp(id
, p
->name
) == 0 && try_module_get(p
->owner
)) {
82 str
= strrchr(id
, '.');
86 idno
= simple_strtol(str
, NULL
, 10);
87 list_for_each_entry(p
, &clocks
, node
) {
89 strlen(p
->name
) == cnt
&&
90 strncmp(id
, p
->name
, cnt
) == 0 &&
91 try_module_get(p
->owner
)) {
98 list_for_each_entry(p
, &clocks
, node
) {
99 if (strcmp(id
, p
->name
) == 0 && try_module_get(p
->owner
)) {
105 printk(KERN_WARNING
"clk: Unable to get requested clock: %s\n", id
);
108 mutex_unlock(&clocks_mutex
);
112 EXPORT_SYMBOL(clk_get
);
114 static void __clk_disable(struct clk
*clk
)
116 if (clk
== NULL
|| IS_ERR(clk
))
119 __clk_disable(clk
->parent
);
120 __clk_disable(clk
->secondary
);
122 if (!(--clk
->usecount
) && clk
->disable
)
126 static int __clk_enable(struct clk
*clk
)
128 if (clk
== NULL
|| IS_ERR(clk
))
131 __clk_enable(clk
->parent
);
132 __clk_enable(clk
->secondary
);
134 if (clk
->usecount
++ == 0 && clk
->enable
)
140 /* This function increments the reference count on the clock and enables the
141 * clock if not already enabled. The parent clock tree is recursively enabled
143 int clk_enable(struct clk
*clk
)
147 if (clk
== NULL
|| IS_ERR(clk
))
150 mutex_lock(&clocks_mutex
);
151 ret
= __clk_enable(clk
);
152 mutex_unlock(&clocks_mutex
);
156 EXPORT_SYMBOL(clk_enable
);
158 /* This function decrements the reference count on the clock and disables
159 * the clock when reference count is 0. The parent clock tree is
160 * recursively disabled
162 void clk_disable(struct clk
*clk
)
164 if (clk
== NULL
|| IS_ERR(clk
))
167 mutex_lock(&clocks_mutex
);
169 mutex_unlock(&clocks_mutex
);
171 EXPORT_SYMBOL(clk_disable
);
173 /* Retrieve the *current* clock rate. If the clock itself
174 * does not provide a special calculation routine, ask
175 * its parent and so on, until one is able to return
178 unsigned long clk_get_rate(struct clk
*clk
)
180 if (clk
== NULL
|| IS_ERR(clk
))
184 return clk
->get_rate(clk
);
186 return clk_get_rate(clk
->parent
);
188 EXPORT_SYMBOL(clk_get_rate
);
190 /* Decrement the clock's module reference count */
191 void clk_put(struct clk
*clk
)
193 if (clk
&& !IS_ERR(clk
))
194 module_put(clk
->owner
);
196 EXPORT_SYMBOL(clk_put
);
198 /* Round the requested clock rate to the nearest supported
199 * rate that is less than or equal to the requested rate.
200 * This is dependent on the clock's current parent.
202 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
204 if (clk
== NULL
|| IS_ERR(clk
) || !clk
->round_rate
)
207 return clk
->round_rate(clk
, rate
);
209 EXPORT_SYMBOL(clk_round_rate
);
211 /* Set the clock to the requested clock rate. The rate must
212 * match a supported rate exactly based on what clk_round_rate returns
214 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
218 if (clk
== NULL
|| IS_ERR(clk
) || clk
->set_rate
== NULL
|| rate
== 0)
221 mutex_lock(&clocks_mutex
);
222 ret
= clk
->set_rate(clk
, rate
);
223 mutex_unlock(&clocks_mutex
);
227 EXPORT_SYMBOL(clk_set_rate
);
229 /* Set the clock's parent to another clock source */
230 int clk_set_parent(struct clk
*clk
, struct clk
*parent
)
234 if (clk
== NULL
|| IS_ERR(clk
) || parent
== NULL
||
235 IS_ERR(parent
) || clk
->set_parent
== NULL
)
238 mutex_lock(&clocks_mutex
);
239 ret
= clk
->set_parent(clk
, parent
);
241 clk
->parent
= parent
;
242 mutex_unlock(&clocks_mutex
);
246 EXPORT_SYMBOL(clk_set_parent
);
248 /* Retrieve the clock's parent clock source */
249 struct clk
*clk_get_parent(struct clk
*clk
)
251 struct clk
*ret
= NULL
;
253 if (clk
== NULL
|| IS_ERR(clk
))
258 EXPORT_SYMBOL(clk_get_parent
);
261 * Add a new clock to the clock tree.
263 int clk_register(struct clk
*clk
)
265 if (clk
== NULL
|| IS_ERR(clk
))
268 mutex_lock(&clocks_mutex
);
269 list_add(&clk
->node
, &clocks
);
270 mutex_unlock(&clocks_mutex
);
274 EXPORT_SYMBOL(clk_register
);
276 /* Remove a clock from the clock tree */
277 void clk_unregister(struct clk
*clk
)
279 if (clk
== NULL
|| IS_ERR(clk
))
282 mutex_lock(&clocks_mutex
);
283 list_del(&clk
->node
);
284 mutex_unlock(&clocks_mutex
);
286 EXPORT_SYMBOL(clk_unregister
);
288 #ifdef CONFIG_PROC_FS
289 static int mxc_clock_read_proc(char *page
, char **start
, off_t off
,
290 int count
, int *eof
, void *data
)
296 list_for_each_entry(clkp
, &clocks
, node
) {
297 p
+= sprintf(p
, "%s-%d:\t\t%lu, %d", clkp
->name
, clkp
->id
,
298 clk_get_rate(clkp
), clkp
->usecount
);
300 p
+= sprintf(p
, ", %s-%d\n", clkp
->parent
->name
,
303 p
+= sprintf(p
, "\n");
306 len
= (p
- page
) - off
;
310 *eof
= (len
<= count
) ? 1 : 0;
316 static int __init
mxc_setup_proc_entry(void)
318 struct proc_dir_entry
*res
;
320 res
= create_proc_read_entry("cpu/clocks", 0, NULL
,
321 mxc_clock_read_proc
, NULL
);
323 printk(KERN_ERR
"Failed to create proc/cpu/clocks\n");
329 late_initcall(mxc_setup_proc_entry
);