2 * Copyright (C) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/kernel.h>
20 #include <linux/device.h>
21 #include <linux/list.h>
22 #include <linux/math64.h>
23 #include <linux/err.h>
26 #include <mach/imx-regs.h>
29 * Very simple approach: We can't disable clocks, so we do
30 * not need refcounting
34 struct list_head node;
36 unsigned long (*get_rate)(void);
40 * get the system pll clock in Hz
42 * mfi + mfn / (mfd +1)
43 * f = 2 * f_ref * --------------------
46 static unsigned long imx_decode_pll(unsigned int pll, u32 f_ref)
48 unsigned long long ll;
51 u32 mfi = (pll >> 10) & 0xf;
52 u32 mfn = pll & 0x3ff;
53 u32 mfd = (pll >> 16) & 0x3ff;
54 u32 pd = (pll >> 26) & 0xf;
56 mfi = mfi <= 5 ? 5 : mfi;
58 ll = 2 * (unsigned long long)f_ref *
59 ((mfi << 16) + (mfn << 16) / (mfd + 1));
60 quot = (pd + 1) * (1 << 16);
63 return (unsigned long)ll;
66 static unsigned long imx_get_system_clk(void)
68 u32 f_ref = (CSCR & CSCR_SYSTEM_SEL) ? 16000000 : (CLK32 * 512);
70 return imx_decode_pll(SPCTL0, f_ref);
73 static unsigned long imx_get_mcu_clk(void)
75 return imx_decode_pll(MPCTL0, CLK32 * 512);
79 * get peripheral clock 1 ( UART[12], Timer[12], PWM )
81 static unsigned long imx_get_perclk1(void)
83 return imx_get_system_clk() / (((PCDR) & 0xf)+1);
87 * get peripheral clock 2 ( LCD, SD, SPI[12] )
89 static unsigned long imx_get_perclk2(void)
91 return imx_get_system_clk() / (((PCDR>>4) & 0xf)+1);
95 * get peripheral clock 3 ( SSI )
97 static unsigned long imx_get_perclk3(void)
99 return imx_get_system_clk() / (((PCDR>>16) & 0x7f)+1);
103 * get hclk ( SDRAM, CSI, Memory Stick, I2C, DMA )
105 static unsigned long imx_get_hclk(void)
107 return imx_get_system_clk() / (((CSCR>>10) & 0xf)+1);
110 static struct clk clk_system_clk = {
111 .name = "system_clk",
112 .get_rate = imx_get_system_clk,
115 static struct clk clk_hclk = {
117 .get_rate = imx_get_hclk,
120 static struct clk clk_mcu_clk = {
122 .get_rate = imx_get_mcu_clk,
125 static struct clk clk_perclk1 = {
127 .get_rate = imx_get_perclk1,
130 static struct clk clk_uart_clk = {
132 .get_rate = imx_get_perclk1,
135 static struct clk clk_perclk2 = {
137 .get_rate = imx_get_perclk2,
140 static struct clk clk_perclk3 = {
142 .get_rate = imx_get_perclk3,
145 static struct clk *clks[] = {
155 static LIST_HEAD(clocks);
156 static DEFINE_MUTEX(clocks_mutex);
158 struct clk *clk_get(struct device *dev, const char *id)
160 struct clk *p, *clk = ERR_PTR(-ENOENT);
162 mutex_lock(&clocks_mutex);
163 list_for_each_entry(p, &clocks, node) {
164 if (!strcmp(p->name, id)) {
171 mutex_unlock(&clocks_mutex);
175 EXPORT_SYMBOL(clk_get);
177 void clk_put(struct clk *clk)
180 EXPORT_SYMBOL(clk_put);
182 int clk_enable(struct clk *clk)
186 EXPORT_SYMBOL(clk_enable);
188 void clk_disable(struct clk *clk)
191 EXPORT_SYMBOL(clk_disable);
193 unsigned long clk_get_rate(struct clk *clk)
195 return clk->get_rate();
197 EXPORT_SYMBOL(clk_get_rate);
199 int imx_clocks_init(void)
203 mutex_lock(&clocks_mutex);
204 for (i = 0; i < ARRAY_SIZE(clks); i++)
205 list_add(&clks[i]->node, &clocks);
206 mutex_unlock(&clocks_mutex);