aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-mxs/clock.c
diff options
context:
space:
mode:
authorShawn Guo <shawn.guo@freescale.com>2010-12-18 08:39:33 -0500
committerUwe Kleine-König <u.kleine-koenig@pengutronix.de>2010-12-20 11:30:22 -0500
commit30a7585ff7464ceb1dceb941700654923becb769 (patch)
tree42c060756bdc2eb464ced6e34982da49b9311580 /arch/arm/mach-mxs/clock.c
parentbf985969e27b507f734435a99df8bf745a3dbb2b (diff)
ARM: mxs: Add clock support
Add clock for MXS-based SoCs, MX23 and MX28. Signed-off-by: Shawn Guo <shawn.guo@freescale.com> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Diffstat (limited to 'arch/arm/mach-mxs/clock.c')
-rw-r--r--arch/arm/mach-mxs/clock.c200
1 files changed, 200 insertions, 0 deletions
diff --git a/arch/arm/mach-mxs/clock.c b/arch/arm/mach-mxs/clock.c
new file mode 100644
index 000000000000..e7d2269cf70e
--- /dev/null
+++ b/arch/arm/mach-mxs/clock.c
@@ -0,0 +1,200 @@
1/*
2 * Based on arch/arm/plat-omap/clock.c
3 *
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
9 *
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.
18 *
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,
22 * MA 02110-1301, USA.
23 */
24
25/* #define DEBUG */
26
27#include <linux/clk.h>
28#include <linux/err.h>
29#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/io.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>
40
41#include <mach/clock.h>
42
43static LIST_HEAD(clocks);
44static DEFINE_MUTEX(clocks_mutex);
45
46/*-------------------------------------------------------------------------
47 * Standard clock functions defined in include/linux/clk.h
48 *-------------------------------------------------------------------------*/
49
50static void __clk_disable(struct clk *clk)
51{
52 if (clk == NULL || IS_ERR(clk))
53 return;
54 WARN_ON(!clk->usecount);
55
56 if (!(--clk->usecount)) {
57 if (clk->disable)
58 clk->disable(clk);
59 __clk_disable(clk->parent);
60 __clk_disable(clk->secondary);
61 }
62}
63
64static int __clk_enable(struct clk *clk)
65{
66 if (clk == NULL || IS_ERR(clk))
67 return -EINVAL;
68
69 if (clk->usecount++ == 0) {
70 __clk_enable(clk->parent);
71 __clk_enable(clk->secondary);
72
73 if (clk->enable)
74 clk->enable(clk);
75 }
76 return 0;
77}
78
79/* This function increments the reference count on the clock and enables the
80 * clock if not already enabled. The parent clock tree is recursively enabled
81 */
82int clk_enable(struct clk *clk)
83{
84 int ret = 0;
85
86 if (clk == NULL || IS_ERR(clk))
87 return -EINVAL;
88
89 mutex_lock(&clocks_mutex);
90 ret = __clk_enable(clk);
91 mutex_unlock(&clocks_mutex);
92
93 return ret;
94}
95EXPORT_SYMBOL(clk_enable);
96
97/* This function decrements the reference count on the clock and disables
98 * the clock when reference count is 0. The parent clock tree is
99 * recursively disabled
100 */
101void clk_disable(struct clk *clk)
102{
103 if (clk == NULL || IS_ERR(clk))
104 return;
105
106 mutex_lock(&clocks_mutex);
107 __clk_disable(clk);
108 mutex_unlock(&clocks_mutex);
109}
110EXPORT_SYMBOL(clk_disable);
111
112/* Retrieve the *current* clock rate. If the clock itself
113 * does not provide a special calculation routine, ask
114 * its parent and so on, until one is able to return
115 * a valid clock rate
116 */
117unsigned long clk_get_rate(struct clk *clk)
118{
119 if (clk == NULL || IS_ERR(clk))
120 return 0UL;
121
122 if (clk->get_rate)
123 return clk->get_rate(clk);
124
125 return clk_get_rate(clk->parent);
126}
127EXPORT_SYMBOL(clk_get_rate);
128
129/* Round the requested clock rate to the nearest supported
130 * rate that is less than or equal to the requested rate.
131 * This is dependent on the clock's current parent.
132 */
133long clk_round_rate(struct clk *clk, unsigned long rate)
134{
135 if (clk == NULL || IS_ERR(clk) || !clk->round_rate)
136 return 0;
137
138 return clk->round_rate(clk, rate);
139}
140EXPORT_SYMBOL(clk_round_rate);
141
142/* Set the clock to the requested clock rate. The rate must
143 * match a supported rate exactly based on what clk_round_rate returns
144 */
145int clk_set_rate(struct clk *clk, unsigned long rate)
146{
147 int ret = -EINVAL;
148
149 if (clk == NULL || IS_ERR(clk) || clk->set_rate == NULL || rate == 0)
150 return ret;
151
152 mutex_lock(&clocks_mutex);
153 ret = clk->set_rate(clk, rate);
154 mutex_unlock(&clocks_mutex);
155
156 return ret;
157}
158EXPORT_SYMBOL(clk_set_rate);
159
160/* Set the clock's parent to another clock source */
161int clk_set_parent(struct clk *clk, struct clk *parent)
162{
163 int ret = -EINVAL;
164 struct clk *old;
165
166 if (clk == NULL || IS_ERR(clk) || parent == NULL ||
167 IS_ERR(parent) || clk->set_parent == NULL)
168 return ret;
169
170 if (clk->usecount)
171 clk_enable(parent);
172
173 mutex_lock(&clocks_mutex);
174 ret = clk->set_parent(clk, parent);
175 if (ret == 0) {
176 old = clk->parent;
177 clk->parent = parent;
178 } else {
179 old = parent;
180 }
181 mutex_unlock(&clocks_mutex);
182
183 if (clk->usecount)
184 clk_disable(old);
185
186 return ret;
187}
188EXPORT_SYMBOL(clk_set_parent);
189
190/* Retrieve the clock's parent clock source */
191struct clk *clk_get_parent(struct clk *clk)
192{
193 struct clk *ret = NULL;
194
195 if (clk == NULL || IS_ERR(clk))
196 return ret;
197
198 return clk->parent;
199}
200EXPORT_SYMBOL(clk_get_parent);